blob: 060144f7a43243519dce42522accc3355fd6a1a0 [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
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200242 usb_gadget_unmap_request(&dwc->gadget, &req->request,
243 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300244
245 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
246 req, dep->name, req->request.actual,
247 req->request.length, status);
248
249 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200250 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300251 spin_lock(&dwc->lock);
252}
253
254static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
255{
256 switch (cmd) {
257 case DWC3_DEPCMD_DEPSTARTCFG:
258 return "Start New Configuration";
259 case DWC3_DEPCMD_ENDTRANSFER:
260 return "End Transfer";
261 case DWC3_DEPCMD_UPDATETRANSFER:
262 return "Update Transfer";
263 case DWC3_DEPCMD_STARTTRANSFER:
264 return "Start Transfer";
265 case DWC3_DEPCMD_CLEARSTALL:
266 return "Clear Stall";
267 case DWC3_DEPCMD_SETSTALL:
268 return "Set Stall";
269 case DWC3_DEPCMD_GETSEQNUMBER:
270 return "Get Data Sequence Number";
271 case DWC3_DEPCMD_SETTRANSFRESOURCE:
272 return "Set Endpoint Transfer Resource";
273 case DWC3_DEPCMD_SETEPCONFIG:
274 return "Set Endpoint Configuration";
275 default:
276 return "UNKNOWN command";
277 }
278}
279
280int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
281 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
282{
283 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200284 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300285 u32 reg;
286
287 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
288 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300289 dwc3_gadget_ep_cmd_string(cmd), params->param0,
290 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300291
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300292 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
293 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
294 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300295
296 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
297 do {
298 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
299 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300300 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
301 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300302 return 0;
303 }
304
305 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300306 * We can't sleep here, because it is also called from
307 * interrupt context.
308 */
309 timeout--;
310 if (!timeout)
311 return -ETIMEDOUT;
312
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200313 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300314 } while (1);
315}
316
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300317dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200318 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300319{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300320 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300321
322 return dep->trb_pool_dma + offset;
323}
324
325static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
326{
327 struct dwc3 *dwc = dep->dwc;
328
329 if (dep->trb_pool)
330 return 0;
331
332 if (dep->number == 0 || dep->number == 1)
333 return 0;
334
335 dep->trb_pool = dma_alloc_coherent(dwc->dev,
336 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
337 &dep->trb_pool_dma, GFP_KERNEL);
338 if (!dep->trb_pool) {
339 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
340 dep->name);
341 return -ENOMEM;
342 }
343
344 return 0;
345}
346
347static void dwc3_free_trb_pool(struct dwc3_ep *dep)
348{
349 struct dwc3 *dwc = dep->dwc;
350
351 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
352 dep->trb_pool, dep->trb_pool_dma);
353
354 dep->trb_pool = NULL;
355 dep->trb_pool_dma = 0;
356}
357
358static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
359{
360 struct dwc3_gadget_ep_cmd_params params;
361 u32 cmd;
362
363 memset(&params, 0x00, sizeof(params));
364
365 if (dep->number != 1) {
366 cmd = DWC3_DEPCMD_DEPSTARTCFG;
367 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300368 if (dep->number > 1) {
369 if (dwc->start_config_issued)
370 return 0;
371 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300372 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300373 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300374
375 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
376 }
377
378 return 0;
379}
380
381static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200382 const struct usb_endpoint_descriptor *desc,
383 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300384{
385 struct dwc3_gadget_ep_cmd_params params;
386
387 memset(&params, 0x00, sizeof(params));
388
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300389 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
390 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
391 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300392
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300393 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
394 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300395
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200396 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300397 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
398 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300399 dep->stream_capable = true;
400 }
401
Felipe Balbi72246da2011-08-19 18:10:58 +0300402 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300403 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300404
405 /*
406 * We are doing 1:1 mapping for endpoints, meaning
407 * Physical Endpoints 2 maps to Logical Endpoint 2 and
408 * so on. We consider the direction bit as part of the physical
409 * endpoint number. So USB endpoint 0x81 is 0x03.
410 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300411 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300412
413 /*
414 * We must use the lower 16 TX FIFOs even though
415 * HW might have more
416 */
417 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300418 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300419
420 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300421 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300422 dep->interval = 1 << (desc->bInterval - 1);
423 }
424
425 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
426 DWC3_DEPCMD_SETEPCONFIG, &params);
427}
428
429static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
430{
431 struct dwc3_gadget_ep_cmd_params params;
432
433 memset(&params, 0x00, sizeof(params));
434
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300435 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300436
437 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
438 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
439}
440
441/**
442 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
443 * @dep: endpoint to be initialized
444 * @desc: USB Endpoint Descriptor
445 *
446 * Caller should take care of locking
447 */
448static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200449 const struct usb_endpoint_descriptor *desc,
450 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300451{
452 struct dwc3 *dwc = dep->dwc;
453 u32 reg;
454 int ret = -ENOMEM;
455
456 if (!(dep->flags & DWC3_EP_ENABLED)) {
457 ret = dwc3_gadget_start_config(dwc, dep);
458 if (ret)
459 return ret;
460 }
461
Felipe Balbic90bfae2011-11-29 13:11:21 +0200462 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300463 if (ret)
464 return ret;
465
466 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200467 struct dwc3_trb *trb_st_hw;
468 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300469
470 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
471 if (ret)
472 return ret;
473
474 dep->desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200475 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300476 dep->type = usb_endpoint_type(desc);
477 dep->flags |= DWC3_EP_ENABLED;
478
479 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
480 reg |= DWC3_DALEPENA_EP(dep->number);
481 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
482
483 if (!usb_endpoint_xfer_isoc(desc))
484 return 0;
485
486 memset(&trb_link, 0, sizeof(trb_link));
487
Paul Zimmerman1d046792012-02-15 18:56:56 -0800488 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300489 trb_st_hw = &dep->trb_pool[0];
490
Felipe Balbif6bafc62012-02-06 11:04:53 +0200491 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300492
Felipe Balbif6bafc62012-02-06 11:04:53 +0200493 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
494 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
495 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
496 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300497 }
498
499 return 0;
500}
501
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200502static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
503static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300504{
505 struct dwc3_request *req;
506
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200507 if (!list_empty(&dep->req_queued))
508 dwc3_stop_active_transfer(dwc, dep->number);
509
Felipe Balbi72246da2011-08-19 18:10:58 +0300510 while (!list_empty(&dep->request_list)) {
511 req = next_request(&dep->request_list);
512
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200513 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300514 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300515}
516
517/**
518 * __dwc3_gadget_ep_disable - Disables a HW endpoint
519 * @dep: the endpoint to disable
520 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200521 * This function also removes requests which are currently processed ny the
522 * hardware and those which are not yet scheduled.
523 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300524 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300525static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
526{
527 struct dwc3 *dwc = dep->dwc;
528 u32 reg;
529
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200530 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300531
532 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
533 reg &= ~DWC3_DALEPENA_EP(dep->number);
534 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
535
Felipe Balbi879631a2011-09-30 10:58:47 +0300536 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300537 dep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200538 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200539 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300541 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300542
543 return 0;
544}
545
546/* -------------------------------------------------------------------------- */
547
548static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
549 const struct usb_endpoint_descriptor *desc)
550{
551 return -EINVAL;
552}
553
554static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
555{
556 return -EINVAL;
557}
558
559/* -------------------------------------------------------------------------- */
560
561static int dwc3_gadget_ep_enable(struct usb_ep *ep,
562 const struct usb_endpoint_descriptor *desc)
563{
564 struct dwc3_ep *dep;
565 struct dwc3 *dwc;
566 unsigned long flags;
567 int ret;
568
569 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
570 pr_debug("dwc3: invalid parameters\n");
571 return -EINVAL;
572 }
573
574 if (!desc->wMaxPacketSize) {
575 pr_debug("dwc3: missing wMaxPacketSize\n");
576 return -EINVAL;
577 }
578
579 dep = to_dwc3_ep(ep);
580 dwc = dep->dwc;
581
582 switch (usb_endpoint_type(desc)) {
583 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900584 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300585 break;
586 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900587 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300588 break;
589 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900590 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 break;
592 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900593 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 break;
595 default:
596 dev_err(dwc->dev, "invalid endpoint transfer type\n");
597 }
598
599 if (dep->flags & DWC3_EP_ENABLED) {
600 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
601 dep->name);
602 return 0;
603 }
604
605 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
606
607 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200608 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300609 spin_unlock_irqrestore(&dwc->lock, flags);
610
611 return ret;
612}
613
614static int dwc3_gadget_ep_disable(struct usb_ep *ep)
615{
616 struct dwc3_ep *dep;
617 struct dwc3 *dwc;
618 unsigned long flags;
619 int ret;
620
621 if (!ep) {
622 pr_debug("dwc3: invalid parameters\n");
623 return -EINVAL;
624 }
625
626 dep = to_dwc3_ep(ep);
627 dwc = dep->dwc;
628
629 if (!(dep->flags & DWC3_EP_ENABLED)) {
630 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
631 dep->name);
632 return 0;
633 }
634
635 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
636 dep->number >> 1,
637 (dep->number & 1) ? "in" : "out");
638
639 spin_lock_irqsave(&dwc->lock, flags);
640 ret = __dwc3_gadget_ep_disable(dep);
641 spin_unlock_irqrestore(&dwc->lock, flags);
642
643 return ret;
644}
645
646static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
647 gfp_t gfp_flags)
648{
649 struct dwc3_request *req;
650 struct dwc3_ep *dep = to_dwc3_ep(ep);
651 struct dwc3 *dwc = dep->dwc;
652
653 req = kzalloc(sizeof(*req), gfp_flags);
654 if (!req) {
655 dev_err(dwc->dev, "not enough memory\n");
656 return NULL;
657 }
658
659 req->epnum = dep->number;
660 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300661
662 return &req->request;
663}
664
665static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
666 struct usb_request *request)
667{
668 struct dwc3_request *req = to_dwc3_request(request);
669
670 kfree(req);
671}
672
Felipe Balbic71fc372011-11-22 11:37:34 +0200673/**
674 * dwc3_prepare_one_trb - setup one TRB from one request
675 * @dep: endpoint for which this request is prepared
676 * @req: dwc3_request pointer
677 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200678static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200679 struct dwc3_request *req, dma_addr_t dma,
680 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200681{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200682 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200683 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200684
685 unsigned int cur_slot;
686
Felipe Balbieeb720f2011-11-28 12:46:59 +0200687 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
688 dep->name, req, (unsigned long long) dma,
689 length, last ? " last" : "",
690 chain ? " chain" : "");
691
Felipe Balbif6bafc62012-02-06 11:04:53 +0200692 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200693 cur_slot = dep->free_slot;
694 dep->free_slot++;
695
696 /* Skip the LINK-TRB on ISOC */
697 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
698 usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200699 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200700
Felipe Balbieeb720f2011-11-28 12:46:59 +0200701 if (!req->trb) {
702 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200703 req->trb = trb;
704 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200705 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200706
Felipe Balbif6bafc62012-02-06 11:04:53 +0200707 trb->size = DWC3_TRB_SIZE_LENGTH(length);
708 trb->bpl = lower_32_bits(dma);
709 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200710
711 switch (usb_endpoint_type(dep->desc)) {
712 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200713 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200714 break;
715
716 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200717 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200718
719 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
720 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
Felipe Balbif6bafc62012-02-06 11:04:53 +0200721 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200722 break;
723
724 case USB_ENDPOINT_XFER_BULK:
725 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200726 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200727 break;
728 default:
729 /*
730 * This is only possible with faulty memory because we
731 * checked it already :)
732 */
733 BUG();
734 }
735
Felipe Balbif6bafc62012-02-06 11:04:53 +0200736 if (usb_endpoint_xfer_isoc(dep->desc)) {
737 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
738 trb->ctrl |= DWC3_TRB_CTRL_CSP;
739 } else {
740 if (chain)
741 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200742
Felipe Balbif6bafc62012-02-06 11:04:53 +0200743 if (last)
744 trb->ctrl |= DWC3_TRB_CTRL_LST;
745 }
746
747 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
748 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
749
750 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200751}
752
Felipe Balbi72246da2011-08-19 18:10:58 +0300753/*
754 * dwc3_prepare_trbs - setup TRBs from requests
755 * @dep: endpoint for which requests are being prepared
756 * @starting: true if the endpoint is idle and no requests are queued.
757 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800758 * The function goes through the requests list and sets up TRBs for the
759 * transfers. The function returns once there are no more TRBs available or
760 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200762static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300763{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200764 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300765 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200766 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200767 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300768
769 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
770
771 /* the first request must not be queued */
772 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200773
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200774 /* Can't wrap around on a non-isoc EP since there's no link TRB */
775 if (!usb_endpoint_xfer_isoc(dep->desc)) {
776 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
777 if (trbs_left > max)
778 trbs_left = max;
779 }
780
Felipe Balbi72246da2011-08-19 18:10:58 +0300781 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800782 * If busy & slot are equal than it is either full or empty. If we are
783 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300784 * full and don't do anything
785 */
786 if (!trbs_left) {
787 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200788 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300789 trbs_left = DWC3_TRB_NUM;
790 /*
791 * In case we start from scratch, we queue the ISOC requests
792 * starting from slot 1. This is done because we use ring
793 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800794 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 * after the first request so we start at slot 1 and have
796 * 7 requests proceed before we hit the first IOC.
797 * Other transfer types don't use the ring buffer and are
798 * processed from the first TRB until the last one. Since we
799 * don't wrap around we have to start at the beginning.
800 */
801 if (usb_endpoint_xfer_isoc(dep->desc)) {
802 dep->busy_slot = 1;
803 dep->free_slot = 1;
804 } else {
805 dep->busy_slot = 0;
806 dep->free_slot = 0;
807 }
808 }
809
810 /* The last TRB is a link TRB, not used for xfer */
811 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200812 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300813
814 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200815 unsigned length;
816 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300817
Felipe Balbieeb720f2011-11-28 12:46:59 +0200818 if (req->request.num_mapped_sgs > 0) {
819 struct usb_request *request = &req->request;
820 struct scatterlist *sg = request->sg;
821 struct scatterlist *s;
822 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300823
Felipe Balbieeb720f2011-11-28 12:46:59 +0200824 for_each_sg(sg, s, request->num_mapped_sgs, i) {
825 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300826
Felipe Balbieeb720f2011-11-28 12:46:59 +0200827 length = sg_dma_len(s);
828 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300829
Paul Zimmerman1d046792012-02-15 18:56:56 -0800830 if (i == (request->num_mapped_sgs - 1) ||
831 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200832 last_one = true;
833 chain = false;
834 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300835
Felipe Balbieeb720f2011-11-28 12:46:59 +0200836 trbs_left--;
837 if (!trbs_left)
838 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300839
Felipe Balbieeb720f2011-11-28 12:46:59 +0200840 if (last_one)
841 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300842
Felipe Balbieeb720f2011-11-28 12:46:59 +0200843 dwc3_prepare_one_trb(dep, req, dma, length,
844 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300845
Felipe Balbieeb720f2011-11-28 12:46:59 +0200846 if (last_one)
847 break;
848 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300849 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200850 dma = req->request.dma;
851 length = req->request.length;
852 trbs_left--;
853
854 if (!trbs_left)
855 last_one = 1;
856
857 /* Is this the last request? */
858 if (list_is_last(&req->list, &dep->request_list))
859 last_one = 1;
860
861 dwc3_prepare_one_trb(dep, req, dma, length,
862 last_one, false);
863
864 if (last_one)
865 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300866 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300867 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300868}
869
870static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
871 int start_new)
872{
873 struct dwc3_gadget_ep_cmd_params params;
874 struct dwc3_request *req;
875 struct dwc3 *dwc = dep->dwc;
876 int ret;
877 u32 cmd;
878
879 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
880 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
881 return -EBUSY;
882 }
883 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
884
885 /*
886 * If we are getting here after a short-out-packet we don't enqueue any
887 * new requests as we try to set the IOC bit only on the last request.
888 */
889 if (start_new) {
890 if (list_empty(&dep->req_queued))
891 dwc3_prepare_trbs(dep, start_new);
892
893 /* req points to the first request which will be sent */
894 req = next_request(&dep->req_queued);
895 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200896 dwc3_prepare_trbs(dep, start_new);
897
Felipe Balbi72246da2011-08-19 18:10:58 +0300898 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800899 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300900 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200901 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300902 }
903 if (!req) {
904 dep->flags |= DWC3_EP_PENDING_REQUEST;
905 return 0;
906 }
907
908 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300909 params.param0 = upper_32_bits(req->trb_dma);
910 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300911
912 if (start_new)
913 cmd = DWC3_DEPCMD_STARTTRANSFER;
914 else
915 cmd = DWC3_DEPCMD_UPDATETRANSFER;
916
917 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
918 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
919 if (ret < 0) {
920 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
921
922 /*
923 * FIXME we need to iterate over the list of requests
924 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800925 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300926 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200927 usb_gadget_unmap_request(&dwc->gadget, &req->request,
928 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300929 list_del(&req->list);
930 return ret;
931 }
932
933 dep->flags |= DWC3_EP_BUSY;
934 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
935 dep->number);
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200936
937 WARN_ON_ONCE(!dep->res_trans_idx);
938
Felipe Balbi72246da2011-08-19 18:10:58 +0300939 return 0;
940}
941
942static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
943{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200944 struct dwc3 *dwc = dep->dwc;
945 int ret;
946
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 req->request.actual = 0;
948 req->request.status = -EINPROGRESS;
949 req->direction = dep->direction;
950 req->epnum = dep->number;
951
952 /*
953 * We only add to our list of requests now and
954 * start consuming the list once we get XferNotReady
955 * IRQ.
956 *
957 * That way, we avoid doing anything that we don't need
958 * to do now and defer it until the point we receive a
959 * particular token from the Host side.
960 *
961 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -0800962 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +0300963 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200964 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
965 dep->direction);
966 if (ret)
967 return ret;
968
Felipe Balbi72246da2011-08-19 18:10:58 +0300969 list_add_tail(&req->list, &dep->request_list);
970
971 /*
972 * There is one special case: XferNotReady with
973 * empty list of requests. We need to kick the
974 * transfer here in that situation, otherwise
975 * we will be NAKing forever.
976 *
977 * If we get XferNotReady before gadget driver
978 * has a chance to queue a request, we will ACK
979 * the IRQ but won't be able to receive the data
980 * until the next request is queued. The following
981 * code is handling exactly that.
982 */
983 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
984 int ret;
985 int start_trans;
986
987 start_trans = 1;
Felipe Balbi7b7dd022012-01-18 17:09:17 +0200988 if (usb_endpoint_xfer_isoc(dep->desc) &&
Paul Zimmerman1d046792012-02-15 18:56:56 -0800989 (dep->flags & DWC3_EP_BUSY))
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 start_trans = 0;
991
Paul Zimmerman1d046792012-02-15 18:56:56 -0800992 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +0300993 if (ret && ret != -EBUSY) {
994 struct dwc3 *dwc = dep->dwc;
995
996 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
997 dep->name);
998 }
999 };
1000
1001 return 0;
1002}
1003
1004static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1005 gfp_t gfp_flags)
1006{
1007 struct dwc3_request *req = to_dwc3_request(request);
1008 struct dwc3_ep *dep = to_dwc3_ep(ep);
1009 struct dwc3 *dwc = dep->dwc;
1010
1011 unsigned long flags;
1012
1013 int ret;
1014
1015 if (!dep->desc) {
1016 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1017 request, ep->name);
1018 return -ESHUTDOWN;
1019 }
1020
1021 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1022 request, ep->name, request->length);
1023
1024 spin_lock_irqsave(&dwc->lock, flags);
1025 ret = __dwc3_gadget_ep_queue(dep, req);
1026 spin_unlock_irqrestore(&dwc->lock, flags);
1027
1028 return ret;
1029}
1030
1031static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1032 struct usb_request *request)
1033{
1034 struct dwc3_request *req = to_dwc3_request(request);
1035 struct dwc3_request *r = NULL;
1036
1037 struct dwc3_ep *dep = to_dwc3_ep(ep);
1038 struct dwc3 *dwc = dep->dwc;
1039
1040 unsigned long flags;
1041 int ret = 0;
1042
1043 spin_lock_irqsave(&dwc->lock, flags);
1044
1045 list_for_each_entry(r, &dep->request_list, list) {
1046 if (r == req)
1047 break;
1048 }
1049
1050 if (r != req) {
1051 list_for_each_entry(r, &dep->req_queued, list) {
1052 if (r == req)
1053 break;
1054 }
1055 if (r == req) {
1056 /* wait until it is processed */
1057 dwc3_stop_active_transfer(dwc, dep->number);
1058 goto out0;
1059 }
1060 dev_err(dwc->dev, "request %p was not queued to %s\n",
1061 request, ep->name);
1062 ret = -EINVAL;
1063 goto out0;
1064 }
1065
1066 /* giveback the request */
1067 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1068
1069out0:
1070 spin_unlock_irqrestore(&dwc->lock, flags);
1071
1072 return ret;
1073}
1074
1075int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1076{
1077 struct dwc3_gadget_ep_cmd_params params;
1078 struct dwc3 *dwc = dep->dwc;
1079 int ret;
1080
1081 memset(&params, 0x00, sizeof(params));
1082
1083 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001084 if (dep->number == 0 || dep->number == 1) {
1085 /*
1086 * Whenever EP0 is stalled, we will restart
1087 * the state machine, thus moving back to
1088 * Setup Phase
1089 */
1090 dwc->ep0state = EP0_SETUP_PHASE;
1091 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001092
1093 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1094 DWC3_DEPCMD_SETSTALL, &params);
1095 if (ret)
1096 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1097 value ? "set" : "clear",
1098 dep->name);
1099 else
1100 dep->flags |= DWC3_EP_STALL;
1101 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001102 if (dep->flags & DWC3_EP_WEDGE)
1103 return 0;
1104
Felipe Balbi72246da2011-08-19 18:10:58 +03001105 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1106 DWC3_DEPCMD_CLEARSTALL, &params);
1107 if (ret)
1108 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1109 value ? "set" : "clear",
1110 dep->name);
1111 else
1112 dep->flags &= ~DWC3_EP_STALL;
1113 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001114
Felipe Balbi72246da2011-08-19 18:10:58 +03001115 return ret;
1116}
1117
1118static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1119{
1120 struct dwc3_ep *dep = to_dwc3_ep(ep);
1121 struct dwc3 *dwc = dep->dwc;
1122
1123 unsigned long flags;
1124
1125 int ret;
1126
1127 spin_lock_irqsave(&dwc->lock, flags);
1128
1129 if (usb_endpoint_xfer_isoc(dep->desc)) {
1130 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1131 ret = -EINVAL;
1132 goto out;
1133 }
1134
1135 ret = __dwc3_gadget_ep_set_halt(dep, value);
1136out:
1137 spin_unlock_irqrestore(&dwc->lock, flags);
1138
1139 return ret;
1140}
1141
1142static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1143{
1144 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001145 struct dwc3 *dwc = dep->dwc;
1146 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001147
Paul Zimmerman249a4562012-02-24 17:32:16 -08001148 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001149 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001150 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001151
Paul Zimmerman52754552011-09-30 10:58:44 +03001152 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001153}
1154
1155/* -------------------------------------------------------------------------- */
1156
1157static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1158 .bLength = USB_DT_ENDPOINT_SIZE,
1159 .bDescriptorType = USB_DT_ENDPOINT,
1160 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1161};
1162
1163static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1164 .enable = dwc3_gadget_ep0_enable,
1165 .disable = dwc3_gadget_ep0_disable,
1166 .alloc_request = dwc3_gadget_ep_alloc_request,
1167 .free_request = dwc3_gadget_ep_free_request,
1168 .queue = dwc3_gadget_ep0_queue,
1169 .dequeue = dwc3_gadget_ep_dequeue,
1170 .set_halt = dwc3_gadget_ep_set_halt,
1171 .set_wedge = dwc3_gadget_ep_set_wedge,
1172};
1173
1174static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1175 .enable = dwc3_gadget_ep_enable,
1176 .disable = dwc3_gadget_ep_disable,
1177 .alloc_request = dwc3_gadget_ep_alloc_request,
1178 .free_request = dwc3_gadget_ep_free_request,
1179 .queue = dwc3_gadget_ep_queue,
1180 .dequeue = dwc3_gadget_ep_dequeue,
1181 .set_halt = dwc3_gadget_ep_set_halt,
1182 .set_wedge = dwc3_gadget_ep_set_wedge,
1183};
1184
1185/* -------------------------------------------------------------------------- */
1186
1187static int dwc3_gadget_get_frame(struct usb_gadget *g)
1188{
1189 struct dwc3 *dwc = gadget_to_dwc(g);
1190 u32 reg;
1191
1192 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1193 return DWC3_DSTS_SOFFN(reg);
1194}
1195
1196static int dwc3_gadget_wakeup(struct usb_gadget *g)
1197{
1198 struct dwc3 *dwc = gadget_to_dwc(g);
1199
1200 unsigned long timeout;
1201 unsigned long flags;
1202
1203 u32 reg;
1204
1205 int ret = 0;
1206
1207 u8 link_state;
1208 u8 speed;
1209
1210 spin_lock_irqsave(&dwc->lock, flags);
1211
1212 /*
1213 * According to the Databook Remote wakeup request should
1214 * be issued only when the device is in early suspend state.
1215 *
1216 * We can check that via USB Link State bits in DSTS register.
1217 */
1218 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1219
1220 speed = reg & DWC3_DSTS_CONNECTSPD;
1221 if (speed == DWC3_DSTS_SUPERSPEED) {
1222 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1223 ret = -EINVAL;
1224 goto out;
1225 }
1226
1227 link_state = DWC3_DSTS_USBLNKST(reg);
1228
1229 switch (link_state) {
1230 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1231 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1232 break;
1233 default:
1234 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1235 link_state);
1236 ret = -EINVAL;
1237 goto out;
1238 }
1239
Felipe Balbi8598bde2012-01-02 18:55:57 +02001240 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1241 if (ret < 0) {
1242 dev_err(dwc->dev, "failed to put link in Recovery\n");
1243 goto out;
1244 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001245
1246 /* write zeroes to Link Change Request */
1247 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1248 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1249
Paul Zimmerman1d046792012-02-15 18:56:56 -08001250 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001251 timeout = jiffies + msecs_to_jiffies(100);
1252
Paul Zimmerman1d046792012-02-15 18:56:56 -08001253 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001254 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1255
1256 /* in HS, means ON */
1257 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1258 break;
1259 }
1260
1261 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1262 dev_err(dwc->dev, "failed to send remote wakeup\n");
1263 ret = -EINVAL;
1264 }
1265
1266out:
1267 spin_unlock_irqrestore(&dwc->lock, flags);
1268
1269 return ret;
1270}
1271
1272static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1273 int is_selfpowered)
1274{
1275 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001276 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001277
Paul Zimmerman249a4562012-02-24 17:32:16 -08001278 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001279 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001280 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001281
1282 return 0;
1283}
1284
1285static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1286{
1287 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001288 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001289
1290 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001291 if (is_on) {
1292 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1293 reg |= (DWC3_DCTL_RUN_STOP
1294 | DWC3_DCTL_TRGTULST_RX_DET);
1295 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001296 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001297 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001298
1299 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1300
1301 do {
1302 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1303 if (is_on) {
1304 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1305 break;
1306 } else {
1307 if (reg & DWC3_DSTS_DEVCTRLHLT)
1308 break;
1309 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001310 timeout--;
1311 if (!timeout)
1312 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001313 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 } while (1);
1315
1316 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1317 dwc->gadget_driver
1318 ? dwc->gadget_driver->function : "no-function",
1319 is_on ? "connect" : "disconnect");
1320}
1321
1322static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1323{
1324 struct dwc3 *dwc = gadget_to_dwc(g);
1325 unsigned long flags;
1326
1327 is_on = !!is_on;
1328
1329 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001330
1331 dwc->softconnect = is_on;
1332
1333 if ((dwc->dotg && !dwc->vbus_active) ||
1334 !dwc->gadget_driver) {
1335
1336 spin_unlock_irqrestore(&dwc->lock, flags);
1337
1338 /*
1339 * Need to wait for vbus_session(on) from otg driver or to
1340 * the udc_start.
1341 */
1342 return 0;
1343 }
1344
Felipe Balbi72246da2011-08-19 18:10:58 +03001345 dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001346
1347 spin_unlock_irqrestore(&dwc->lock, flags);
1348
1349 return 0;
1350}
1351
1352static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1353{
1354 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1355 unsigned long flags;
1356
1357 if (!dwc->dotg)
1358 return -EPERM;
1359
1360 is_active = !!is_active;
1361
1362 spin_lock_irqsave(&dwc->lock, flags);
1363
1364 /* Mark that the vbus was powered */
1365 dwc->vbus_active = is_active;
1366
1367 /*
1368 * Check if upper level usb_gadget_driver was already registerd with
1369 * this udc controller driver (if dwc3_gadget_start was called)
1370 */
1371 if (dwc->gadget_driver && dwc->softconnect) {
1372 if (dwc->vbus_active) {
1373 /*
1374 * Both vbus was activated by otg and pullup was
1375 * signaled by the gadget driver.
1376 */
1377 dwc3_gadget_run_stop(dwc, 1);
1378 } else {
1379 dwc3_gadget_run_stop(dwc, 0);
1380 }
1381 }
1382
Felipe Balbi72246da2011-08-19 18:10:58 +03001383 spin_unlock_irqrestore(&dwc->lock, flags);
1384
1385 return 0;
1386}
1387
1388static int dwc3_gadget_start(struct usb_gadget *g,
1389 struct usb_gadget_driver *driver)
1390{
1391 struct dwc3 *dwc = gadget_to_dwc(g);
1392 struct dwc3_ep *dep;
1393 unsigned long flags;
1394 int ret = 0;
1395 u32 reg;
1396
1397 spin_lock_irqsave(&dwc->lock, flags);
1398
1399 if (dwc->gadget_driver) {
1400 dev_err(dwc->dev, "%s is already bound to %s\n",
1401 dwc->gadget.name,
1402 dwc->gadget_driver->driver.name);
1403 ret = -EBUSY;
1404 goto err0;
1405 }
1406
1407 dwc->gadget_driver = driver;
1408 dwc->gadget.dev.driver = &driver->driver;
1409
Felipe Balbi72246da2011-08-19 18:10:58 +03001410 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1411 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi6c167fc2011-10-07 22:55:04 +03001412 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001413 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1414
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001415 dwc->start_config_issued = false;
1416
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 /* Start with SuperSpeed Default */
1418 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1419
1420 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001421 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001422 if (ret) {
1423 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1424 goto err0;
1425 }
1426
1427 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001428 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001429 if (ret) {
1430 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1431 goto err1;
1432 }
1433
1434 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001435 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001436 dwc3_ep0_out_start(dwc);
1437
1438 spin_unlock_irqrestore(&dwc->lock, flags);
1439
1440 return 0;
1441
1442err1:
1443 __dwc3_gadget_ep_disable(dwc->eps[0]);
1444
1445err0:
1446 spin_unlock_irqrestore(&dwc->lock, flags);
1447
1448 return ret;
1449}
1450
1451static int dwc3_gadget_stop(struct usb_gadget *g,
1452 struct usb_gadget_driver *driver)
1453{
1454 struct dwc3 *dwc = gadget_to_dwc(g);
1455 unsigned long flags;
1456
1457 spin_lock_irqsave(&dwc->lock, flags);
1458
1459 __dwc3_gadget_ep_disable(dwc->eps[0]);
1460 __dwc3_gadget_ep_disable(dwc->eps[1]);
1461
1462 dwc->gadget_driver = NULL;
1463 dwc->gadget.dev.driver = NULL;
1464
1465 spin_unlock_irqrestore(&dwc->lock, flags);
1466
1467 return 0;
1468}
1469static const struct usb_gadget_ops dwc3_gadget_ops = {
1470 .get_frame = dwc3_gadget_get_frame,
1471 .wakeup = dwc3_gadget_wakeup,
1472 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001473 .vbus_session = dwc3_gadget_vbus_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03001474 .pullup = dwc3_gadget_pullup,
1475 .udc_start = dwc3_gadget_start,
1476 .udc_stop = dwc3_gadget_stop,
1477};
1478
1479/* -------------------------------------------------------------------------- */
1480
1481static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1482{
1483 struct dwc3_ep *dep;
1484 u8 epnum;
1485
1486 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1487
1488 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1489 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1490 if (!dep) {
1491 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1492 epnum);
1493 return -ENOMEM;
1494 }
1495
1496 dep->dwc = dwc;
1497 dep->number = epnum;
1498 dwc->eps[epnum] = dep;
1499
1500 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1501 (epnum & 1) ? "in" : "out");
1502 dep->endpoint.name = dep->name;
1503 dep->direction = (epnum & 1);
1504
1505 if (epnum == 0 || epnum == 1) {
1506 dep->endpoint.maxpacket = 512;
1507 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1508 if (!epnum)
1509 dwc->gadget.ep0 = &dep->endpoint;
1510 } else {
1511 int ret;
1512
1513 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001514 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001515 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1516 list_add_tail(&dep->endpoint.ep_list,
1517 &dwc->gadget.ep_list);
1518
1519 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001520 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001521 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001522 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001523
Felipe Balbi72246da2011-08-19 18:10:58 +03001524 INIT_LIST_HEAD(&dep->request_list);
1525 INIT_LIST_HEAD(&dep->req_queued);
1526 }
1527
1528 return 0;
1529}
1530
1531static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1532{
1533 struct dwc3_ep *dep;
1534 u8 epnum;
1535
1536 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1537 dep = dwc->eps[epnum];
1538 dwc3_free_trb_pool(dep);
1539
1540 if (epnum != 0 && epnum != 1)
1541 list_del(&dep->endpoint.ep_list);
1542
1543 kfree(dep);
1544 }
1545}
1546
1547static void dwc3_gadget_release(struct device *dev)
1548{
1549 dev_dbg(dev, "%s\n", __func__);
1550}
1551
1552/* -------------------------------------------------------------------------- */
1553static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1554 const struct dwc3_event_depevt *event, int status)
1555{
1556 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001557 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001558 unsigned int count;
1559 unsigned int s_pkt = 0;
1560
1561 do {
1562 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001563 if (!req) {
1564 WARN_ON_ONCE(1);
1565 return 1;
1566 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001567
Felipe Balbif6bafc62012-02-06 11:04:53 +02001568 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001569
Felipe Balbif6bafc62012-02-06 11:04:53 +02001570 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001571 /*
1572 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001573 * can do. If we don't clean it up we loop forever. If
1574 * we skip the TRB then it gets overwritten after a
1575 * while since we use them in a ring buffer. A BUG()
1576 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001577 * fixes the root cause instead of looking away :)
1578 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001579 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1580 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001581 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001582
1583 if (dep->direction) {
1584 if (count) {
1585 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1586 dep->name);
1587 status = -ECONNRESET;
1588 }
1589 } else {
1590 if (count && (event->status & DEPEVT_STATUS_SHORT))
1591 s_pkt = 1;
1592 }
1593
1594 /*
1595 * We assume here we will always receive the entire data block
1596 * which we should receive. Meaning, if we program RX to
1597 * receive 4K but we receive only 2K, we assume that's all we
1598 * should receive and we simply bounce the request back to the
1599 * gadget driver for further processing.
1600 */
1601 req->request.actual += req->request.length - count;
1602 dwc3_gadget_giveback(dep, req, status);
1603 if (s_pkt)
1604 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001605 if ((event->status & DEPEVT_STATUS_LST) &&
1606 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001607 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001608 if ((event->status & DEPEVT_STATUS_IOC) &&
1609 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001610 break;
1611 } while (1);
1612
Felipe Balbif6bafc62012-02-06 11:04:53 +02001613 if ((event->status & DEPEVT_STATUS_IOC) &&
1614 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001615 return 0;
1616 return 1;
1617}
1618
1619static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1620 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1621 int start_new)
1622{
1623 unsigned status = 0;
1624 int clean_busy;
1625
1626 if (event->status & DEPEVT_STATUS_BUSERR)
1627 status = -ECONNRESET;
1628
Paul Zimmerman1d046792012-02-15 18:56:56 -08001629 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001630 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001631 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001632
1633 /*
1634 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1635 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1636 */
1637 if (dwc->revision < DWC3_REVISION_183A) {
1638 u32 reg;
1639 int i;
1640
1641 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1642 struct dwc3_ep *dep = dwc->eps[i];
1643
1644 if (!(dep->flags & DWC3_EP_ENABLED))
1645 continue;
1646
1647 if (!list_empty(&dep->req_queued))
1648 return;
1649 }
1650
1651 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1652 reg |= dwc->u1u2;
1653 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1654
1655 dwc->u1u2 = 0;
1656 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001657}
1658
1659static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1660 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1661{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001662 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001663
1664 if (list_empty(&dep->request_list)) {
1665 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1666 dep->name);
1667 return;
1668 }
1669
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001670 mask = ~(dep->interval - 1);
1671 uf = event->parameters & mask;
1672 /* 4 micro frames in the future */
1673 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001674
1675 __dwc3_gadget_kick_transfer(dep, uf, 1);
1676}
1677
1678static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1679 const struct dwc3_event_depevt *event)
1680{
1681 struct dwc3 *dwc = dep->dwc;
1682 struct dwc3_event_depevt mod_ev = *event;
1683
1684 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001685 * We were asked to remove one request. It is possible that this
1686 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001687 * transfer index. Since we stopped the complete endpoint we don't
1688 * know how many requests were already completed (and not yet)
1689 * reported and how could be done (later). We purge them all until
1690 * the end of the list.
1691 */
1692 mod_ev.status = DEPEVT_STATUS_LST;
1693 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1694 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001695 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001696}
1697
1698static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1699 const struct dwc3_event_depevt *event)
1700{
1701 u32 param = event->parameters;
1702 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1703
1704 switch (cmd_type) {
1705 case DWC3_DEPCMD_ENDTRANSFER:
1706 dwc3_process_ep_cmd_complete(dep, event);
1707 break;
1708 case DWC3_DEPCMD_STARTTRANSFER:
1709 dep->res_trans_idx = param & 0x7f;
1710 break;
1711 default:
1712 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1713 __func__, cmd_type);
1714 break;
1715 };
1716}
1717
1718static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1719 const struct dwc3_event_depevt *event)
1720{
1721 struct dwc3_ep *dep;
1722 u8 epnum = event->endpoint_number;
1723
1724 dep = dwc->eps[epnum];
1725
1726 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1727 dwc3_ep_event_string(event->endpoint_event));
1728
1729 if (epnum == 0 || epnum == 1) {
1730 dwc3_ep0_interrupt(dwc, event);
1731 return;
1732 }
1733
1734 switch (event->endpoint_event) {
1735 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001736 dep->res_trans_idx = 0;
1737
Felipe Balbi72246da2011-08-19 18:10:58 +03001738 if (usb_endpoint_xfer_isoc(dep->desc)) {
1739 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1740 dep->name);
1741 return;
1742 }
1743
1744 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1745 break;
1746 case DWC3_DEPEVT_XFERINPROGRESS:
1747 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1748 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1749 dep->name);
1750 return;
1751 }
1752
1753 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1754 break;
1755 case DWC3_DEPEVT_XFERNOTREADY:
1756 if (usb_endpoint_xfer_isoc(dep->desc)) {
1757 dwc3_gadget_start_isoc(dwc, dep, event);
1758 } else {
1759 int ret;
1760
1761 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001762 dep->name, event->status &
1763 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001764 ? "Transfer Active"
1765 : "Transfer Not Active");
1766
1767 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1768 if (!ret || ret == -EBUSY)
1769 return;
1770
1771 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1772 dep->name);
1773 }
1774
1775 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001776 case DWC3_DEPEVT_STREAMEVT:
1777 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1778 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1779 dep->name);
1780 return;
1781 }
1782
1783 switch (event->status) {
1784 case DEPEVT_STREAMEVT_FOUND:
1785 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1786 event->parameters);
1787
1788 break;
1789 case DEPEVT_STREAMEVT_NOTFOUND:
1790 /* FALLTHROUGH */
1791 default:
1792 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1793 }
1794 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 case DWC3_DEPEVT_RXTXFIFOEVT:
1796 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1797 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001798 case DWC3_DEPEVT_EPCMDCMPLT:
1799 dwc3_ep_cmd_compl(dep, event);
1800 break;
1801 }
1802}
1803
1804static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1805{
1806 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1807 spin_unlock(&dwc->lock);
1808 dwc->gadget_driver->disconnect(&dwc->gadget);
1809 spin_lock(&dwc->lock);
1810 }
1811}
1812
1813static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1814{
1815 struct dwc3_ep *dep;
1816 struct dwc3_gadget_ep_cmd_params params;
1817 u32 cmd;
1818 int ret;
1819
1820 dep = dwc->eps[epnum];
1821
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001822 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001823 if (dep->res_trans_idx) {
1824 cmd = DWC3_DEPCMD_ENDTRANSFER;
1825 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1826 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1827 memset(&params, 0, sizeof(params));
1828 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1829 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001830 dep->res_trans_idx = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001831 }
1832}
1833
1834static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1835{
1836 u32 epnum;
1837
1838 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1839 struct dwc3_ep *dep;
1840
1841 dep = dwc->eps[epnum];
1842 if (!(dep->flags & DWC3_EP_ENABLED))
1843 continue;
1844
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001845 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 }
1847}
1848
1849static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1850{
1851 u32 epnum;
1852
1853 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1854 struct dwc3_ep *dep;
1855 struct dwc3_gadget_ep_cmd_params params;
1856 int ret;
1857
1858 dep = dwc->eps[epnum];
1859
1860 if (!(dep->flags & DWC3_EP_STALL))
1861 continue;
1862
1863 dep->flags &= ~DWC3_EP_STALL;
1864
1865 memset(&params, 0, sizeof(params));
1866 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1867 DWC3_DEPCMD_CLEARSTALL, &params);
1868 WARN_ON_ONCE(ret);
1869 }
1870}
1871
1872static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1873{
1874 dev_vdbg(dwc->dev, "%s\n", __func__);
1875#if 0
1876 XXX
1877 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1878 enable it before we can disable it.
1879
1880 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1881 reg &= ~DWC3_DCTL_INITU1ENA;
1882 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1883
1884 reg &= ~DWC3_DCTL_INITU2ENA;
1885 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1886#endif
1887
1888 dwc3_stop_active_transfers(dwc);
1889 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001890 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001891
1892 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001893 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001894}
1895
1896static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1897{
1898 u32 reg;
1899
1900 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1901
1902 if (on)
1903 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1904 else
1905 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1906
1907 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1908}
1909
1910static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1911{
1912 u32 reg;
1913
1914 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1915
1916 if (on)
1917 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1918 else
1919 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1920
1921 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1922}
1923
1924static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1925{
1926 u32 reg;
1927
1928 dev_vdbg(dwc->dev, "%s\n", __func__);
1929
Felipe Balbidf62df52011-10-14 15:11:49 +03001930 /*
1931 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1932 * would cause a missing Disconnect Event if there's a
1933 * pending Setup Packet in the FIFO.
1934 *
1935 * There's no suggested workaround on the official Bug
1936 * report, which states that "unless the driver/application
1937 * is doing any special handling of a disconnect event,
1938 * there is no functional issue".
1939 *
1940 * Unfortunately, it turns out that we _do_ some special
1941 * handling of a disconnect event, namely complete all
1942 * pending transfers, notify gadget driver of the
1943 * disconnection, and so on.
1944 *
1945 * Our suggested workaround is to follow the Disconnect
1946 * Event steps here, instead, based on a setup_packet_pending
1947 * flag. Such flag gets set whenever we have a XferNotReady
1948 * event on EP0 and gets cleared on XferComplete for the
1949 * same endpoint.
1950 *
1951 * Refers to:
1952 *
1953 * STAR#9000466709: RTL: Device : Disconnect event not
1954 * generated if setup packet pending in FIFO
1955 */
1956 if (dwc->revision < DWC3_REVISION_188A) {
1957 if (dwc->setup_packet_pending)
1958 dwc3_gadget_disconnect_interrupt(dwc);
1959 }
1960
Felipe Balbi961906e2011-12-20 15:37:21 +02001961 /* after reset -> Default State */
1962 dwc->dev_state = DWC3_DEFAULT_STATE;
1963
Felipe Balbi72246da2011-08-19 18:10:58 +03001964 /* Enable PHYs */
1965 dwc3_gadget_usb2_phy_power(dwc, true);
1966 dwc3_gadget_usb3_phy_power(dwc, true);
1967
1968 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1969 dwc3_disconnect_gadget(dwc);
1970
1971 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1972 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1973 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001974 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001975
1976 dwc3_stop_active_transfers(dwc);
1977 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001978 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001979
1980 /* Reset device address to zero */
1981 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1982 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1983 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001984}
1985
1986static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1987{
1988 u32 reg;
1989 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1990
1991 /*
1992 * We change the clock only at SS but I dunno why I would want to do
1993 * this. Maybe it becomes part of the power saving plan.
1994 */
1995
1996 if (speed != DWC3_DSTS_SUPERSPEED)
1997 return;
1998
1999 /*
2000 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2001 * each time on Connect Done.
2002 */
2003 if (!usb30_clock)
2004 return;
2005
2006 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2007 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2008 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2009}
2010
2011static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
2012{
2013 switch (speed) {
2014 case USB_SPEED_SUPER:
2015 dwc3_gadget_usb2_phy_power(dwc, false);
2016 break;
2017 case USB_SPEED_HIGH:
2018 case USB_SPEED_FULL:
2019 case USB_SPEED_LOW:
2020 dwc3_gadget_usb3_phy_power(dwc, false);
2021 break;
2022 }
2023}
2024
2025static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2026{
2027 struct dwc3_gadget_ep_cmd_params params;
2028 struct dwc3_ep *dep;
2029 int ret;
2030 u32 reg;
2031 u8 speed;
2032
2033 dev_vdbg(dwc->dev, "%s\n", __func__);
2034
2035 memset(&params, 0x00, sizeof(params));
2036
Felipe Balbi72246da2011-08-19 18:10:58 +03002037 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2038 speed = reg & DWC3_DSTS_CONNECTSPD;
2039 dwc->speed = speed;
2040
2041 dwc3_update_ram_clk_sel(dwc, speed);
2042
2043 switch (speed) {
2044 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002045 /*
2046 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2047 * would cause a missing USB3 Reset event.
2048 *
2049 * In such situations, we should force a USB3 Reset
2050 * event by calling our dwc3_gadget_reset_interrupt()
2051 * routine.
2052 *
2053 * Refers to:
2054 *
2055 * STAR#9000483510: RTL: SS : USB3 reset event may
2056 * not be generated always when the link enters poll
2057 */
2058 if (dwc->revision < DWC3_REVISION_190A)
2059 dwc3_gadget_reset_interrupt(dwc);
2060
Felipe Balbi72246da2011-08-19 18:10:58 +03002061 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2062 dwc->gadget.ep0->maxpacket = 512;
2063 dwc->gadget.speed = USB_SPEED_SUPER;
2064 break;
2065 case DWC3_DCFG_HIGHSPEED:
2066 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2067 dwc->gadget.ep0->maxpacket = 64;
2068 dwc->gadget.speed = USB_SPEED_HIGH;
2069 break;
2070 case DWC3_DCFG_FULLSPEED2:
2071 case DWC3_DCFG_FULLSPEED1:
2072 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2073 dwc->gadget.ep0->maxpacket = 64;
2074 dwc->gadget.speed = USB_SPEED_FULL;
2075 break;
2076 case DWC3_DCFG_LOWSPEED:
2077 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2078 dwc->gadget.ep0->maxpacket = 8;
2079 dwc->gadget.speed = USB_SPEED_LOW;
2080 break;
2081 }
2082
2083 /* Disable unneded PHY */
2084 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2085
2086 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002087 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002088 if (ret) {
2089 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2090 return;
2091 }
2092
2093 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002094 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002095 if (ret) {
2096 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2097 return;
2098 }
2099
2100 /*
2101 * Configure PHY via GUSB3PIPECTLn if required.
2102 *
2103 * Update GTXFIFOSIZn
2104 *
2105 * In both cases reset values should be sufficient.
2106 */
2107}
2108
2109static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2110{
2111 dev_vdbg(dwc->dev, "%s\n", __func__);
2112
2113 /*
2114 * TODO take core out of low power mode when that's
2115 * implemented.
2116 */
2117
2118 dwc->gadget_driver->resume(&dwc->gadget);
2119}
2120
2121static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2122 unsigned int evtinfo)
2123{
Felipe Balbifae2b902011-10-14 13:00:30 +03002124 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2125
2126 /*
2127 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2128 * on the link partner, the USB session might do multiple entry/exit
2129 * of low power states before a transfer takes place.
2130 *
2131 * Due to this problem, we might experience lower throughput. The
2132 * suggested workaround is to disable DCTL[12:9] bits if we're
2133 * transitioning from U1/U2 to U0 and enable those bits again
2134 * after a transfer completes and there are no pending transfers
2135 * on any of the enabled endpoints.
2136 *
2137 * This is the first half of that workaround.
2138 *
2139 * Refers to:
2140 *
2141 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2142 * core send LGO_Ux entering U0
2143 */
2144 if (dwc->revision < DWC3_REVISION_183A) {
2145 if (next == DWC3_LINK_STATE_U0) {
2146 u32 u1u2;
2147 u32 reg;
2148
2149 switch (dwc->link_state) {
2150 case DWC3_LINK_STATE_U1:
2151 case DWC3_LINK_STATE_U2:
2152 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2153 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2154 | DWC3_DCTL_ACCEPTU2ENA
2155 | DWC3_DCTL_INITU1ENA
2156 | DWC3_DCTL_ACCEPTU1ENA);
2157
2158 if (!dwc->u1u2)
2159 dwc->u1u2 = reg & u1u2;
2160
2161 reg &= ~u1u2;
2162
2163 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2164 break;
2165 default:
2166 /* do nothing */
2167 break;
2168 }
2169 }
2170 }
2171
2172 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002173
2174 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002175}
2176
2177static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2178 const struct dwc3_event_devt *event)
2179{
2180 switch (event->type) {
2181 case DWC3_DEVICE_EVENT_DISCONNECT:
2182 dwc3_gadget_disconnect_interrupt(dwc);
2183 break;
2184 case DWC3_DEVICE_EVENT_RESET:
2185 dwc3_gadget_reset_interrupt(dwc);
2186 break;
2187 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2188 dwc3_gadget_conndone_interrupt(dwc);
2189 break;
2190 case DWC3_DEVICE_EVENT_WAKEUP:
2191 dwc3_gadget_wakeup_interrupt(dwc);
2192 break;
2193 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2194 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2195 break;
2196 case DWC3_DEVICE_EVENT_EOPF:
2197 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2198 break;
2199 case DWC3_DEVICE_EVENT_SOF:
2200 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2201 break;
2202 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2203 dev_vdbg(dwc->dev, "Erratic Error\n");
2204 break;
2205 case DWC3_DEVICE_EVENT_CMD_CMPL:
2206 dev_vdbg(dwc->dev, "Command Complete\n");
2207 break;
2208 case DWC3_DEVICE_EVENT_OVERFLOW:
2209 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302210 /*
2211 * Controllers prior to 2.30a revision has a bug where
2212 * Overflow Event may overwrite an unacknowledged event
2213 * in the event buffer. The severity of the issue depends
2214 * on the overwritten event type. Add a warning message
2215 * saying that an event is overwritten.
2216 *
2217 * TODO: In future we may need to see if we can re-enumerate
2218 * with host.
2219 */
2220 if (dwc->revision < DWC3_REVISION_230A)
2221 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002222 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302223 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2224 /*
2225 * Controllers prior to 2.30a revision has a bug, due to which
2226 * a vendor device test LMP event can not be filtered. But
2227 * this event is not handled in the current code. This is a
2228 * special event and 8 bytes of data will follow the event.
2229 * Handling this event is tricky when event buffer is almost
2230 * full. Moreover this event will not occur in normal scenario
2231 * and can only happen with special hosts in testing scenarios.
2232 * Add a warning message to indicate that this event is received
2233 * which means that event buffer might have corrupted.
2234 */
2235 if (dwc->revision < DWC3_REVISION_230A)
2236 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2237 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002238 default:
2239 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2240 }
2241}
2242
2243static void dwc3_process_event_entry(struct dwc3 *dwc,
2244 const union dwc3_event *event)
2245{
2246 /* Endpoint IRQ, handle it and return early */
2247 if (event->type.is_devspec == 0) {
2248 /* depevt */
2249 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2250 }
2251
2252 switch (event->type.type) {
2253 case DWC3_EVENT_TYPE_DEV:
2254 dwc3_gadget_interrupt(dwc, &event->devt);
2255 break;
2256 /* REVISIT what to do with Carkit and I2C events ? */
2257 default:
2258 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2259 }
2260}
2261
2262static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2263{
2264 struct dwc3_event_buffer *evt;
2265 int left;
2266 u32 count;
2267
2268 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2269 count &= DWC3_GEVNTCOUNT_MASK;
2270 if (!count)
2271 return IRQ_NONE;
2272
2273 evt = dwc->ev_buffs[buf];
2274 left = count;
2275
2276 while (left > 0) {
2277 union dwc3_event event;
2278
Felipe Balbid70d8442012-02-06 13:40:17 +02002279 event.raw = *(u32 *) (evt->buf + evt->lpos);
2280
Felipe Balbi72246da2011-08-19 18:10:58 +03002281 dwc3_process_event_entry(dwc, &event);
2282 /*
2283 * XXX we wrap around correctly to the next entry as almost all
2284 * entries are 4 bytes in size. There is one entry which has 12
2285 * bytes which is a regular entry followed by 8 bytes data. ATM
2286 * I don't know how things are organized if were get next to the
2287 * a boundary so I worry about that once we try to handle that.
2288 */
2289 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2290 left -= 4;
2291
2292 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2293 }
2294
2295 return IRQ_HANDLED;
2296}
2297
2298static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2299{
2300 struct dwc3 *dwc = _dwc;
2301 int i;
2302 irqreturn_t ret = IRQ_NONE;
2303
2304 spin_lock(&dwc->lock);
2305
Felipe Balbi9f622b22011-10-12 10:31:04 +03002306 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002307 irqreturn_t status;
2308
2309 status = dwc3_process_event_buf(dwc, i);
2310 if (status == IRQ_HANDLED)
2311 ret = status;
2312 }
2313
2314 spin_unlock(&dwc->lock);
2315
2316 return ret;
2317}
2318
2319/**
2320 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002321 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002322 *
2323 * Returns 0 on success otherwise negative errno.
2324 */
2325int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2326{
2327 u32 reg;
2328 int ret;
2329 int irq;
2330
2331 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2332 &dwc->ctrl_req_addr, GFP_KERNEL);
2333 if (!dwc->ctrl_req) {
2334 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2335 ret = -ENOMEM;
2336 goto err0;
2337 }
2338
2339 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2340 &dwc->ep0_trb_addr, GFP_KERNEL);
2341 if (!dwc->ep0_trb) {
2342 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2343 ret = -ENOMEM;
2344 goto err1;
2345 }
2346
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002347 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2348 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002349 if (!dwc->setup_buf) {
2350 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2351 ret = -ENOMEM;
2352 goto err2;
2353 }
2354
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002355 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2356 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2357 if (!dwc->ep0_bounce) {
2358 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2359 ret = -ENOMEM;
2360 goto err3;
2361 }
2362
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 dev_set_name(&dwc->gadget.dev, "gadget");
2364
2365 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002366 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002367 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2368 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002369 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002370
2371 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2372
2373 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2374 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2375 dwc->gadget.dev.release = dwc3_gadget_release;
2376 dwc->gadget.name = "dwc3-gadget";
2377
2378 /*
2379 * REVISIT: Here we should clear all pending IRQs to be
2380 * sure we're starting from a well known location.
2381 */
2382
2383 ret = dwc3_gadget_init_endpoints(dwc);
2384 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002385 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002386
2387 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2388
2389 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2390 "dwc3", dwc);
2391 if (ret) {
2392 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2393 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002394 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002395 }
2396
2397 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302398 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002399 DWC3_DEVTEN_CMDCMPLTEN |
2400 DWC3_DEVTEN_ERRTICERREN |
2401 DWC3_DEVTEN_WKUPEVTEN |
2402 DWC3_DEVTEN_ULSTCNGEN |
2403 DWC3_DEVTEN_CONNECTDONEEN |
2404 DWC3_DEVTEN_USBRSTEN |
2405 DWC3_DEVTEN_DISCONNEVTEN);
2406 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2407
2408 ret = device_register(&dwc->gadget.dev);
2409 if (ret) {
2410 dev_err(dwc->dev, "failed to register gadget device\n");
2411 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002412 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002413 }
2414
2415 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2416 if (ret) {
2417 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002418 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002419 }
2420
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002421 if (dwc->dotg) {
2422 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2423 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2424 if (ret) {
2425 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2426 goto err7;
2427 }
2428 }
2429
Felipe Balbi72246da2011-08-19 18:10:58 +03002430 return 0;
2431
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002432err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002433 device_unregister(&dwc->gadget.dev);
2434
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002435err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002436 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2437 free_irq(irq, dwc);
2438
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002439err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002440 dwc3_gadget_free_endpoints(dwc);
2441
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002442err4:
2443 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2444 dwc->ep0_bounce_addr);
2445
Felipe Balbi72246da2011-08-19 18:10:58 +03002446err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002447 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002448
2449err2:
2450 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2451 dwc->ep0_trb, dwc->ep0_trb_addr);
2452
2453err1:
2454 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2455 dwc->ctrl_req, dwc->ctrl_req_addr);
2456
2457err0:
2458 return ret;
2459}
2460
2461void dwc3_gadget_exit(struct dwc3 *dwc)
2462{
2463 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002464
2465 usb_del_gadget_udc(&dwc->gadget);
2466 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2467
2468 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2469 free_irq(irq, dwc);
2470
Felipe Balbi72246da2011-08-19 18:10:58 +03002471 dwc3_gadget_free_endpoints(dwc);
2472
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002473 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2474 dwc->ep0_bounce_addr);
2475
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002476 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002477
2478 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2479 dwc->ep0_trb, dwc->ep0_trb_addr);
2480
2481 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2482 dwc->ctrl_req, dwc->ctrl_req_addr);
2483
2484 device_unregister(&dwc->gadget.dev);
2485}