blob: 0cab69e51380eeff5c2a0b8fa08124148ac63afa [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/io.h>
47#include <linux/list.h>
48#include <linux/dma-mapping.h>
49
50#include <linux/usb/ch9.h>
51#include <linux/usb/gadget.h>
52
53#include "core.h"
54#include "gadget.h"
55#include "io.h"
56
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020057/**
58 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
59 * @dwc: pointer to our context structure
60 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
61 *
62 * Caller should take care of locking. This function will
63 * return 0 on success or -EINVAL if wrong Test Selector
64 * is passed
65 */
66int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
67{
68 u32 reg;
69
70 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
71 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
72
73 switch (mode) {
74 case TEST_J:
75 case TEST_K:
76 case TEST_SE0_NAK:
77 case TEST_PACKET:
78 case TEST_FORCE_EN:
79 reg |= mode << 1;
80 break;
81 default:
82 return -EINVAL;
83 }
84
85 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
86
87 return 0;
88}
89
Felipe Balbi8598bde2012-01-02 18:55:57 +020090/**
91 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
92 * @dwc: pointer to our context structure
93 * @state: the state to put link into
94 *
95 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080096 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020097 */
98int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
99{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800100 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200101 u32 reg;
102
Paul Zimmerman802fde92012-04-27 13:10:52 +0300103 /*
104 * Wait until device controller is ready. Only applies to 1.94a and
105 * later RTL.
106 */
107 if (dwc->revision >= DWC3_REVISION_194A) {
108 while (--retries) {
109 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
110 if (reg & DWC3_DSTS_DCNRD)
111 udelay(5);
112 else
113 break;
114 }
115
116 if (retries <= 0)
117 return -ETIMEDOUT;
118 }
119
Felipe Balbi8598bde2012-01-02 18:55:57 +0200120 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
121 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
122
123 /* set requested state */
124 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
125 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
126
Paul Zimmerman802fde92012-04-27 13:10:52 +0300127 /*
128 * The following code is racy when called from dwc3_gadget_wakeup,
129 * and is not needed, at least on newer versions
130 */
131 if (dwc->revision >= DWC3_REVISION_194A)
132 return 0;
133
Felipe Balbi8598bde2012-01-02 18:55:57 +0200134 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300135 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 while (--retries) {
137 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
138
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 if (DWC3_DSTS_USBLNKST(reg) == state)
140 return 0;
141
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800142 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200143 }
144
145 dev_vdbg(dwc->dev, "link state change request timed out\n");
146
147 return -ETIMEDOUT;
148}
149
Felipe Balbi457e84b2012-01-18 18:04:09 +0200150/**
151 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
152 * @dwc: pointer to our context structure
153 *
154 * This function will a best effort FIFO allocation in order
155 * to improve FIFO usage and throughput, while still allowing
156 * us to enable as many endpoints as possible.
157 *
158 * Keep in mind that this operation will be highly dependent
159 * on the configured size for RAM1 - which contains TxFifo -,
160 * the amount of endpoints enabled on coreConsultant tool, and
161 * the width of the Master Bus.
162 *
163 * In the ideal world, we would always be able to satisfy the
164 * following equation:
165 *
166 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
167 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
168 *
169 * Unfortunately, due to many variables that's not always the case.
170 */
171int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
172{
173 int last_fifo_depth = 0;
174 int ram1_depth;
175 int fifo_size;
176 int mdwidth;
177 int num;
178
179 if (!dwc->needs_fifo_resize)
180 return 0;
181
182 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
183 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
184
185 /* MDWIDTH is represented in bits, we need it in bytes */
186 mdwidth >>= 3;
187
188 /*
189 * FIXME For now we will only allocate 1 wMaxPacketSize space
190 * for each enabled endpoint, later patches will come to
191 * improve this algorithm so that we better use the internal
192 * FIFO space
193 */
194 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
195 struct dwc3_ep *dep = dwc->eps[num];
196 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200197 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200198 int tmp;
199
200 if (!(dep->number & 1))
201 continue;
202
203 if (!(dep->flags & DWC3_EP_ENABLED))
204 continue;
205
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200206 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
207 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200208 mult = 3;
209
210 /*
211 * REVISIT: the following assumes we will always have enough
212 * space available on the FIFO RAM for all possible use cases.
213 * Make sure that's true somehow and change FIFO allocation
214 * accordingly.
215 *
216 * If we have Bulk or Isochronous endpoints, we want
217 * them to be able to be very, very fast. So we're giving
218 * those endpoints a fifo_size which is enough for 3 full
219 * packets
220 */
221 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200222 tmp += mdwidth;
223
224 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200225
Felipe Balbi457e84b2012-01-18 18:04:09 +0200226 fifo_size |= (last_fifo_depth << 16);
227
228 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
229 dep->name, last_fifo_depth, fifo_size & 0xffff);
230
231 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
232 fifo_size);
233
234 last_fifo_depth += (fifo_size & 0xffff);
235 }
236
237 return 0;
238}
239
Felipe Balbi72246da2011-08-19 18:10:58 +0300240void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
241 int status)
242{
243 struct dwc3 *dwc = dep->dwc;
244
245 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200246 if (req->request.num_mapped_sgs)
247 dep->busy_slot += req->request.num_mapped_sgs;
248 else
249 dep->busy_slot++;
250
Felipe Balbi72246da2011-08-19 18:10:58 +0300251 /*
252 * Skip LINK TRB. We can't use req->trb and check for
253 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
254 * completed (not the LINK TRB).
255 */
256 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200257 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300258 dep->busy_slot++;
259 }
260 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200261 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300262
263 if (req->request.status == -EINPROGRESS)
264 req->request.status = status;
265
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200266 usb_gadget_unmap_request(&dwc->gadget, &req->request,
267 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300268
269 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
270 req, dep->name, req->request.actual,
271 req->request.length, status);
272
273 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200274 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300275 spin_lock(&dwc->lock);
276}
277
278static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
279{
280 switch (cmd) {
281 case DWC3_DEPCMD_DEPSTARTCFG:
282 return "Start New Configuration";
283 case DWC3_DEPCMD_ENDTRANSFER:
284 return "End Transfer";
285 case DWC3_DEPCMD_UPDATETRANSFER:
286 return "Update Transfer";
287 case DWC3_DEPCMD_STARTTRANSFER:
288 return "Start Transfer";
289 case DWC3_DEPCMD_CLEARSTALL:
290 return "Clear Stall";
291 case DWC3_DEPCMD_SETSTALL:
292 return "Set Stall";
Paul Zimmerman802fde92012-04-27 13:10:52 +0300293 case DWC3_DEPCMD_GETEPSTATE:
294 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300295 case DWC3_DEPCMD_SETTRANSFRESOURCE:
296 return "Set Endpoint Transfer Resource";
297 case DWC3_DEPCMD_SETEPCONFIG:
298 return "Set Endpoint Configuration";
299 default:
300 return "UNKNOWN command";
301 }
302}
303
Felipe Balbib09bb642012-04-24 16:19:11 +0300304int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
305{
306 u32 timeout = 500;
307 u32 reg;
308
309 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
310 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
311
312 do {
313 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
314 if (!(reg & DWC3_DGCMD_CMDACT)) {
315 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
316 DWC3_DGCMD_STATUS(reg));
317 return 0;
318 }
319
320 /*
321 * We can't sleep here, because it's also called from
322 * interrupt context.
323 */
324 timeout--;
325 if (!timeout)
326 return -ETIMEDOUT;
327 udelay(1);
328 } while (1);
329}
330
Felipe Balbi72246da2011-08-19 18:10:58 +0300331int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
332 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
333{
334 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200335 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300336 u32 reg;
337
338 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
339 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300340 dwc3_gadget_ep_cmd_string(cmd), params->param0,
341 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300342
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300343 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
344 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
345 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300346
347 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
348 do {
349 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
350 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300351 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
352 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300353 return 0;
354 }
355
356 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300357 * We can't sleep here, because it is also called from
358 * interrupt context.
359 */
360 timeout--;
361 if (!timeout)
362 return -ETIMEDOUT;
363
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200364 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300365 } while (1);
366}
367
368static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200369 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300370{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300371 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300372
373 return dep->trb_pool_dma + offset;
374}
375
376static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
377{
378 struct dwc3 *dwc = dep->dwc;
379
380 if (dep->trb_pool)
381 return 0;
382
383 if (dep->number == 0 || dep->number == 1)
384 return 0;
385
386 dep->trb_pool = dma_alloc_coherent(dwc->dev,
387 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
388 &dep->trb_pool_dma, GFP_KERNEL);
389 if (!dep->trb_pool) {
390 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
391 dep->name);
392 return -ENOMEM;
393 }
394
395 return 0;
396}
397
398static void dwc3_free_trb_pool(struct dwc3_ep *dep)
399{
400 struct dwc3 *dwc = dep->dwc;
401
402 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
403 dep->trb_pool, dep->trb_pool_dma);
404
405 dep->trb_pool = NULL;
406 dep->trb_pool_dma = 0;
407}
408
409static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
410{
411 struct dwc3_gadget_ep_cmd_params params;
412 u32 cmd;
413
414 memset(&params, 0x00, sizeof(params));
415
416 if (dep->number != 1) {
417 cmd = DWC3_DEPCMD_DEPSTARTCFG;
418 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300419 if (dep->number > 1) {
420 if (dwc->start_config_issued)
421 return 0;
422 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300423 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300424 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300425
426 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
427 }
428
429 return 0;
430}
431
432static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200433 const struct usb_endpoint_descriptor *desc,
434 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300435{
436 struct dwc3_gadget_ep_cmd_params params;
437
438 memset(&params, 0x00, sizeof(params));
439
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300440 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
441 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
442 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300443
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300444 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
445 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300446
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200447 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300448 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
449 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300450 dep->stream_capable = true;
451 }
452
Felipe Balbi72246da2011-08-19 18:10:58 +0300453 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300454 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
456 /*
457 * We are doing 1:1 mapping for endpoints, meaning
458 * Physical Endpoints 2 maps to Logical Endpoint 2 and
459 * so on. We consider the direction bit as part of the physical
460 * endpoint number. So USB endpoint 0x81 is 0x03.
461 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300462 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300463
464 /*
465 * We must use the lower 16 TX FIFOs even though
466 * HW might have more
467 */
468 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300469 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300470
471 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300472 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300473 dep->interval = 1 << (desc->bInterval - 1);
474 }
475
476 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
477 DWC3_DEPCMD_SETEPCONFIG, &params);
478}
479
480static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
481{
482 struct dwc3_gadget_ep_cmd_params params;
483
484 memset(&params, 0x00, sizeof(params));
485
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300486 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300487
488 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
489 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
490}
491
492/**
493 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
494 * @dep: endpoint to be initialized
495 * @desc: USB Endpoint Descriptor
496 *
497 * Caller should take care of locking
498 */
499static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200500 const struct usb_endpoint_descriptor *desc,
501 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300502{
503 struct dwc3 *dwc = dep->dwc;
504 u32 reg;
505 int ret = -ENOMEM;
506
507 if (!(dep->flags & DWC3_EP_ENABLED)) {
508 ret = dwc3_gadget_start_config(dwc, dep);
509 if (ret)
510 return ret;
511 }
512
Felipe Balbic90bfae2011-11-29 13:11:21 +0200513 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300514 if (ret)
515 return ret;
516
517 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200518 struct dwc3_trb *trb_st_hw;
519 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520
521 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
522 if (ret)
523 return ret;
524
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200525 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200526 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300527 dep->type = usb_endpoint_type(desc);
528 dep->flags |= DWC3_EP_ENABLED;
529
530 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
531 reg |= DWC3_DALEPENA_EP(dep->number);
532 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
533
534 if (!usb_endpoint_xfer_isoc(desc))
535 return 0;
536
537 memset(&trb_link, 0, sizeof(trb_link));
538
Paul Zimmerman1d046792012-02-15 18:56:56 -0800539 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 trb_st_hw = &dep->trb_pool[0];
541
Felipe Balbif6bafc62012-02-06 11:04:53 +0200542 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300543
Felipe Balbif6bafc62012-02-06 11:04:53 +0200544 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
545 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
546 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
547 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548 }
549
550 return 0;
551}
552
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200553static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
554static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300555{
556 struct dwc3_request *req;
557
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200558 if (!list_empty(&dep->req_queued))
559 dwc3_stop_active_transfer(dwc, dep->number);
560
Felipe Balbi72246da2011-08-19 18:10:58 +0300561 while (!list_empty(&dep->request_list)) {
562 req = next_request(&dep->request_list);
563
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200564 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300565 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300566}
567
568/**
569 * __dwc3_gadget_ep_disable - Disables a HW endpoint
570 * @dep: the endpoint to disable
571 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200572 * This function also removes requests which are currently processed ny the
573 * hardware and those which are not yet scheduled.
574 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300575 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300576static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
577{
578 struct dwc3 *dwc = dep->dwc;
579 u32 reg;
580
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200581 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300582
583 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
584 reg &= ~DWC3_DALEPENA_EP(dep->number);
585 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
586
Felipe Balbi879631a2011-09-30 10:58:47 +0300587 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200588 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200589 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300590 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300591 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300592
593 return 0;
594}
595
596/* -------------------------------------------------------------------------- */
597
598static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
599 const struct usb_endpoint_descriptor *desc)
600{
601 return -EINVAL;
602}
603
604static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
605{
606 return -EINVAL;
607}
608
609/* -------------------------------------------------------------------------- */
610
611static int dwc3_gadget_ep_enable(struct usb_ep *ep,
612 const struct usb_endpoint_descriptor *desc)
613{
614 struct dwc3_ep *dep;
615 struct dwc3 *dwc;
616 unsigned long flags;
617 int ret;
618
619 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
620 pr_debug("dwc3: invalid parameters\n");
621 return -EINVAL;
622 }
623
624 if (!desc->wMaxPacketSize) {
625 pr_debug("dwc3: missing wMaxPacketSize\n");
626 return -EINVAL;
627 }
628
629 dep = to_dwc3_ep(ep);
630 dwc = dep->dwc;
631
632 switch (usb_endpoint_type(desc)) {
633 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900634 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 break;
636 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900637 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300638 break;
639 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900640 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300641 break;
642 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900643 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 break;
645 default:
646 dev_err(dwc->dev, "invalid endpoint transfer type\n");
647 }
648
649 if (dep->flags & DWC3_EP_ENABLED) {
650 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
651 dep->name);
652 return 0;
653 }
654
655 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
656
657 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200658 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300659 spin_unlock_irqrestore(&dwc->lock, flags);
660
661 return ret;
662}
663
664static int dwc3_gadget_ep_disable(struct usb_ep *ep)
665{
666 struct dwc3_ep *dep;
667 struct dwc3 *dwc;
668 unsigned long flags;
669 int ret;
670
671 if (!ep) {
672 pr_debug("dwc3: invalid parameters\n");
673 return -EINVAL;
674 }
675
676 dep = to_dwc3_ep(ep);
677 dwc = dep->dwc;
678
679 if (!(dep->flags & DWC3_EP_ENABLED)) {
680 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
681 dep->name);
682 return 0;
683 }
684
685 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
686 dep->number >> 1,
687 (dep->number & 1) ? "in" : "out");
688
689 spin_lock_irqsave(&dwc->lock, flags);
690 ret = __dwc3_gadget_ep_disable(dep);
691 spin_unlock_irqrestore(&dwc->lock, flags);
692
693 return ret;
694}
695
696static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
697 gfp_t gfp_flags)
698{
699 struct dwc3_request *req;
700 struct dwc3_ep *dep = to_dwc3_ep(ep);
701 struct dwc3 *dwc = dep->dwc;
702
703 req = kzalloc(sizeof(*req), gfp_flags);
704 if (!req) {
705 dev_err(dwc->dev, "not enough memory\n");
706 return NULL;
707 }
708
709 req->epnum = dep->number;
710 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300711
712 return &req->request;
713}
714
715static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
716 struct usb_request *request)
717{
718 struct dwc3_request *req = to_dwc3_request(request);
719
720 kfree(req);
721}
722
Felipe Balbic71fc372011-11-22 11:37:34 +0200723/**
724 * dwc3_prepare_one_trb - setup one TRB from one request
725 * @dep: endpoint for which this request is prepared
726 * @req: dwc3_request pointer
727 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200728static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200729 struct dwc3_request *req, dma_addr_t dma,
730 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200731{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200732 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200733 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200734
735 unsigned int cur_slot;
736
Felipe Balbieeb720f2011-11-28 12:46:59 +0200737 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
738 dep->name, req, (unsigned long long) dma,
739 length, last ? " last" : "",
740 chain ? " chain" : "");
741
Felipe Balbif6bafc62012-02-06 11:04:53 +0200742 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200743 cur_slot = dep->free_slot;
744 dep->free_slot++;
745
746 /* Skip the LINK-TRB on ISOC */
747 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200748 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200749 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200750
Felipe Balbieeb720f2011-11-28 12:46:59 +0200751 if (!req->trb) {
752 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200753 req->trb = trb;
754 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200755 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200756
Felipe Balbif6bafc62012-02-06 11:04:53 +0200757 trb->size = DWC3_TRB_SIZE_LENGTH(length);
758 trb->bpl = lower_32_bits(dma);
759 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200760
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200761 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200762 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200763 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200764 break;
765
766 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200767 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200768
Pratyush Anand206dd692012-05-21 12:42:54 +0530769 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200770 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200771 break;
772
773 case USB_ENDPOINT_XFER_BULK:
774 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200775 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200776 break;
777 default:
778 /*
779 * This is only possible with faulty memory because we
780 * checked it already :)
781 */
782 BUG();
783 }
784
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200785 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200786 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
787 trb->ctrl |= DWC3_TRB_CTRL_CSP;
788 } else {
789 if (chain)
790 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200791
Felipe Balbif6bafc62012-02-06 11:04:53 +0200792 if (last)
793 trb->ctrl |= DWC3_TRB_CTRL_LST;
794 }
795
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200796 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200797 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
798
799 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200800}
801
Felipe Balbi72246da2011-08-19 18:10:58 +0300802/*
803 * dwc3_prepare_trbs - setup TRBs from requests
804 * @dep: endpoint for which requests are being prepared
805 * @starting: true if the endpoint is idle and no requests are queued.
806 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800807 * The function goes through the requests list and sets up TRBs for the
808 * transfers. The function returns once there are no more TRBs available or
809 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300810 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200811static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300812{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200813 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300814 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200815 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200816 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300817
818 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
819
820 /* the first request must not be queued */
821 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200822
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200823 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200824 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200825 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
826 if (trbs_left > max)
827 trbs_left = max;
828 }
829
Felipe Balbi72246da2011-08-19 18:10:58 +0300830 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800831 * If busy & slot are equal than it is either full or empty. If we are
832 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300833 * full and don't do anything
834 */
835 if (!trbs_left) {
836 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200837 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300838 trbs_left = DWC3_TRB_NUM;
839 /*
840 * In case we start from scratch, we queue the ISOC requests
841 * starting from slot 1. This is done because we use ring
842 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800843 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300844 * after the first request so we start at slot 1 and have
845 * 7 requests proceed before we hit the first IOC.
846 * Other transfer types don't use the ring buffer and are
847 * processed from the first TRB until the last one. Since we
848 * don't wrap around we have to start at the beginning.
849 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200850 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300851 dep->busy_slot = 1;
852 dep->free_slot = 1;
853 } else {
854 dep->busy_slot = 0;
855 dep->free_slot = 0;
856 }
857 }
858
859 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200860 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200861 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300862
863 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200864 unsigned length;
865 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300866
Felipe Balbieeb720f2011-11-28 12:46:59 +0200867 if (req->request.num_mapped_sgs > 0) {
868 struct usb_request *request = &req->request;
869 struct scatterlist *sg = request->sg;
870 struct scatterlist *s;
871 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300872
Felipe Balbieeb720f2011-11-28 12:46:59 +0200873 for_each_sg(sg, s, request->num_mapped_sgs, i) {
874 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300875
Felipe Balbieeb720f2011-11-28 12:46:59 +0200876 length = sg_dma_len(s);
877 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300878
Paul Zimmerman1d046792012-02-15 18:56:56 -0800879 if (i == (request->num_mapped_sgs - 1) ||
880 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200881 last_one = true;
882 chain = false;
883 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300884
Felipe Balbieeb720f2011-11-28 12:46:59 +0200885 trbs_left--;
886 if (!trbs_left)
887 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300888
Felipe Balbieeb720f2011-11-28 12:46:59 +0200889 if (last_one)
890 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300891
Felipe Balbieeb720f2011-11-28 12:46:59 +0200892 dwc3_prepare_one_trb(dep, req, dma, length,
893 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300894
Felipe Balbieeb720f2011-11-28 12:46:59 +0200895 if (last_one)
896 break;
897 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300898 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200899 dma = req->request.dma;
900 length = req->request.length;
901 trbs_left--;
902
903 if (!trbs_left)
904 last_one = 1;
905
906 /* Is this the last request? */
907 if (list_is_last(&req->list, &dep->request_list))
908 last_one = 1;
909
910 dwc3_prepare_one_trb(dep, req, dma, length,
911 last_one, false);
912
913 if (last_one)
914 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300915 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300916 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300917}
918
919static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
920 int start_new)
921{
922 struct dwc3_gadget_ep_cmd_params params;
923 struct dwc3_request *req;
924 struct dwc3 *dwc = dep->dwc;
925 int ret;
926 u32 cmd;
927
928 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
929 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
930 return -EBUSY;
931 }
932 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
933
934 /*
935 * If we are getting here after a short-out-packet we don't enqueue any
936 * new requests as we try to set the IOC bit only on the last request.
937 */
938 if (start_new) {
939 if (list_empty(&dep->req_queued))
940 dwc3_prepare_trbs(dep, start_new);
941
942 /* req points to the first request which will be sent */
943 req = next_request(&dep->req_queued);
944 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200945 dwc3_prepare_trbs(dep, start_new);
946
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800948 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300949 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200950 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300951 }
952 if (!req) {
953 dep->flags |= DWC3_EP_PENDING_REQUEST;
954 return 0;
955 }
956
957 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300958 params.param0 = upper_32_bits(req->trb_dma);
959 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300960
961 if (start_new)
962 cmd = DWC3_DEPCMD_STARTTRANSFER;
963 else
964 cmd = DWC3_DEPCMD_UPDATETRANSFER;
965
966 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
967 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
968 if (ret < 0) {
969 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
970
971 /*
972 * FIXME we need to iterate over the list of requests
973 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800974 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300975 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200976 usb_gadget_unmap_request(&dwc->gadget, &req->request,
977 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300978 list_del(&req->list);
979 return ret;
980 }
981
982 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200983
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000984 if (start_new) {
985 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
986 dep->number);
987 WARN_ON_ONCE(!dep->res_trans_idx);
988 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200989
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 return 0;
991}
992
Pratyush Anandd6d6ec72012-05-25 18:54:56 +0530993static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
994 struct dwc3_ep *dep, u32 cur_uf)
995{
996 u32 uf;
997
998 if (list_empty(&dep->request_list)) {
999 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1000 dep->name);
1001 return;
1002 }
1003
1004 /* 4 micro frames in the future */
1005 uf = cur_uf + dep->interval * 4;
1006
1007 __dwc3_gadget_kick_transfer(dep, uf, 1);
1008}
1009
1010static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1011 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1012{
1013 u32 cur_uf, mask;
1014
1015 mask = ~(dep->interval - 1);
1016 cur_uf = event->parameters & mask;
1017
1018 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1019}
1020
Felipe Balbi72246da2011-08-19 18:10:58 +03001021static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1022{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001023 struct dwc3 *dwc = dep->dwc;
1024 int ret;
1025
Felipe Balbi72246da2011-08-19 18:10:58 +03001026 req->request.actual = 0;
1027 req->request.status = -EINPROGRESS;
1028 req->direction = dep->direction;
1029 req->epnum = dep->number;
1030
1031 /*
1032 * We only add to our list of requests now and
1033 * start consuming the list once we get XferNotReady
1034 * IRQ.
1035 *
1036 * That way, we avoid doing anything that we don't need
1037 * to do now and defer it until the point we receive a
1038 * particular token from the Host side.
1039 *
1040 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001041 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001042 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001043 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1044 dep->direction);
1045 if (ret)
1046 return ret;
1047
Felipe Balbi72246da2011-08-19 18:10:58 +03001048 list_add_tail(&req->list, &dep->request_list);
1049
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301050 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1051 if (dep->flags & DWC3_EP_BUSY) {
1052 dep->flags |= DWC3_EP_PENDING_REQUEST;
1053 } else if (dep->flags & DWC3_EP_MISSED_ISOC) {
1054 __dwc3_gadget_start_isoc(dwc, dep, dep->current_uf);
1055 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1056 }
1057 }
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001058
Felipe Balbi72246da2011-08-19 18:10:58 +03001059 /*
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001060 * There are two special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001061 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001062 * 1. XferNotReady with empty list of requests. We need to kick the
1063 * transfer here in that situation, otherwise we will be NAKing
1064 * forever. If we get XferNotReady before gadget driver has a
1065 * chance to queue a request, we will ACK the IRQ but won't be
1066 * able to receive the data until the next request is queued.
1067 * The following code is handling exactly that.
1068 *
1069 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1070 * kick the transfer here after queuing a request, otherwise the
1071 * core may not see the modified TRB(s).
Felipe Balbi72246da2011-08-19 18:10:58 +03001072 */
1073 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001074 int ret;
1075 int start_trans = 1;
1076 u8 trans_idx = dep->res_trans_idx;
Felipe Balbi72246da2011-08-19 18:10:58 +03001077
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001078 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001079 (dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001080 start_trans = 0;
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001081 WARN_ON_ONCE(!trans_idx);
1082 } else {
1083 trans_idx = 0;
1084 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001085
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001086 ret = __dwc3_gadget_kick_transfer(dep, trans_idx, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +03001087 if (ret && ret != -EBUSY) {
1088 struct dwc3 *dwc = dep->dwc;
1089
1090 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1091 dep->name);
1092 }
Felipe Balbia0925322012-05-22 10:24:11 +03001093 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001094
1095 return 0;
1096}
1097
1098static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1099 gfp_t gfp_flags)
1100{
1101 struct dwc3_request *req = to_dwc3_request(request);
1102 struct dwc3_ep *dep = to_dwc3_ep(ep);
1103 struct dwc3 *dwc = dep->dwc;
1104
1105 unsigned long flags;
1106
1107 int ret;
1108
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001109 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001110 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1111 request, ep->name);
1112 return -ESHUTDOWN;
1113 }
1114
1115 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1116 request, ep->name, request->length);
1117
1118 spin_lock_irqsave(&dwc->lock, flags);
1119 ret = __dwc3_gadget_ep_queue(dep, req);
1120 spin_unlock_irqrestore(&dwc->lock, flags);
1121
1122 return ret;
1123}
1124
1125static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1126 struct usb_request *request)
1127{
1128 struct dwc3_request *req = to_dwc3_request(request);
1129 struct dwc3_request *r = NULL;
1130
1131 struct dwc3_ep *dep = to_dwc3_ep(ep);
1132 struct dwc3 *dwc = dep->dwc;
1133
1134 unsigned long flags;
1135 int ret = 0;
1136
1137 spin_lock_irqsave(&dwc->lock, flags);
1138
1139 list_for_each_entry(r, &dep->request_list, list) {
1140 if (r == req)
1141 break;
1142 }
1143
1144 if (r != req) {
1145 list_for_each_entry(r, &dep->req_queued, list) {
1146 if (r == req)
1147 break;
1148 }
1149 if (r == req) {
1150 /* wait until it is processed */
1151 dwc3_stop_active_transfer(dwc, dep->number);
1152 goto out0;
1153 }
1154 dev_err(dwc->dev, "request %p was not queued to %s\n",
1155 request, ep->name);
1156 ret = -EINVAL;
1157 goto out0;
1158 }
1159
1160 /* giveback the request */
1161 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1162
1163out0:
1164 spin_unlock_irqrestore(&dwc->lock, flags);
1165
1166 return ret;
1167}
1168
1169int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1170{
1171 struct dwc3_gadget_ep_cmd_params params;
1172 struct dwc3 *dwc = dep->dwc;
1173 int ret;
1174
1175 memset(&params, 0x00, sizeof(params));
1176
1177 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001178 if (dep->number == 0 || dep->number == 1) {
1179 /*
1180 * Whenever EP0 is stalled, we will restart
1181 * the state machine, thus moving back to
1182 * Setup Phase
1183 */
1184 dwc->ep0state = EP0_SETUP_PHASE;
1185 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001186
1187 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1188 DWC3_DEPCMD_SETSTALL, &params);
1189 if (ret)
1190 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1191 value ? "set" : "clear",
1192 dep->name);
1193 else
1194 dep->flags |= DWC3_EP_STALL;
1195 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001196 if (dep->flags & DWC3_EP_WEDGE)
1197 return 0;
1198
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1200 DWC3_DEPCMD_CLEARSTALL, &params);
1201 if (ret)
1202 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1203 value ? "set" : "clear",
1204 dep->name);
1205 else
1206 dep->flags &= ~DWC3_EP_STALL;
1207 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001208
Felipe Balbi72246da2011-08-19 18:10:58 +03001209 return ret;
1210}
1211
1212static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1213{
1214 struct dwc3_ep *dep = to_dwc3_ep(ep);
1215 struct dwc3 *dwc = dep->dwc;
1216
1217 unsigned long flags;
1218
1219 int ret;
1220
1221 spin_lock_irqsave(&dwc->lock, flags);
1222
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001223 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001224 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1225 ret = -EINVAL;
1226 goto out;
1227 }
1228
1229 ret = __dwc3_gadget_ep_set_halt(dep, value);
1230out:
1231 spin_unlock_irqrestore(&dwc->lock, flags);
1232
1233 return ret;
1234}
1235
1236static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1237{
1238 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001239 struct dwc3 *dwc = dep->dwc;
1240 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001241
Paul Zimmerman249a4562012-02-24 17:32:16 -08001242 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001243 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001244 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001245
Paul Zimmerman52754552011-09-30 10:58:44 +03001246 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001247}
1248
1249/* -------------------------------------------------------------------------- */
1250
1251static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1252 .bLength = USB_DT_ENDPOINT_SIZE,
1253 .bDescriptorType = USB_DT_ENDPOINT,
1254 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1255};
1256
1257static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1258 .enable = dwc3_gadget_ep0_enable,
1259 .disable = dwc3_gadget_ep0_disable,
1260 .alloc_request = dwc3_gadget_ep_alloc_request,
1261 .free_request = dwc3_gadget_ep_free_request,
1262 .queue = dwc3_gadget_ep0_queue,
1263 .dequeue = dwc3_gadget_ep_dequeue,
1264 .set_halt = dwc3_gadget_ep_set_halt,
1265 .set_wedge = dwc3_gadget_ep_set_wedge,
1266};
1267
1268static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1269 .enable = dwc3_gadget_ep_enable,
1270 .disable = dwc3_gadget_ep_disable,
1271 .alloc_request = dwc3_gadget_ep_alloc_request,
1272 .free_request = dwc3_gadget_ep_free_request,
1273 .queue = dwc3_gadget_ep_queue,
1274 .dequeue = dwc3_gadget_ep_dequeue,
1275 .set_halt = dwc3_gadget_ep_set_halt,
1276 .set_wedge = dwc3_gadget_ep_set_wedge,
1277};
1278
1279/* -------------------------------------------------------------------------- */
1280
1281static int dwc3_gadget_get_frame(struct usb_gadget *g)
1282{
1283 struct dwc3 *dwc = gadget_to_dwc(g);
1284 u32 reg;
1285
1286 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1287 return DWC3_DSTS_SOFFN(reg);
1288}
1289
1290static int dwc3_gadget_wakeup(struct usb_gadget *g)
1291{
1292 struct dwc3 *dwc = gadget_to_dwc(g);
1293
1294 unsigned long timeout;
1295 unsigned long flags;
1296
1297 u32 reg;
1298
1299 int ret = 0;
1300
1301 u8 link_state;
1302 u8 speed;
1303
1304 spin_lock_irqsave(&dwc->lock, flags);
1305
1306 /*
1307 * According to the Databook Remote wakeup request should
1308 * be issued only when the device is in early suspend state.
1309 *
1310 * We can check that via USB Link State bits in DSTS register.
1311 */
1312 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1313
1314 speed = reg & DWC3_DSTS_CONNECTSPD;
1315 if (speed == DWC3_DSTS_SUPERSPEED) {
1316 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1317 ret = -EINVAL;
1318 goto out;
1319 }
1320
1321 link_state = DWC3_DSTS_USBLNKST(reg);
1322
1323 switch (link_state) {
1324 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1325 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1326 break;
1327 default:
1328 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1329 link_state);
1330 ret = -EINVAL;
1331 goto out;
1332 }
1333
Felipe Balbi8598bde2012-01-02 18:55:57 +02001334 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1335 if (ret < 0) {
1336 dev_err(dwc->dev, "failed to put link in Recovery\n");
1337 goto out;
1338 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001339
Paul Zimmerman802fde92012-04-27 13:10:52 +03001340 /* Recent versions do this automatically */
1341 if (dwc->revision < DWC3_REVISION_194A) {
1342 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001343 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001344 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1345 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1346 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001347
Paul Zimmerman1d046792012-02-15 18:56:56 -08001348 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001349 timeout = jiffies + msecs_to_jiffies(100);
1350
Paul Zimmerman1d046792012-02-15 18:56:56 -08001351 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001352 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1353
1354 /* in HS, means ON */
1355 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1356 break;
1357 }
1358
1359 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1360 dev_err(dwc->dev, "failed to send remote wakeup\n");
1361 ret = -EINVAL;
1362 }
1363
1364out:
1365 spin_unlock_irqrestore(&dwc->lock, flags);
1366
1367 return ret;
1368}
1369
1370static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1371 int is_selfpowered)
1372{
1373 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001374 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001375
Paul Zimmerman249a4562012-02-24 17:32:16 -08001376 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001377 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001378 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001379
1380 return 0;
1381}
1382
1383static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1384{
1385 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001386 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001387
1388 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001389 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001390 if (dwc->revision <= DWC3_REVISION_187A) {
1391 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1392 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1393 }
1394
1395 if (dwc->revision >= DWC3_REVISION_194A)
1396 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1397 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001398 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001399 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001400 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001401
1402 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1403
1404 do {
1405 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1406 if (is_on) {
1407 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1408 break;
1409 } else {
1410 if (reg & DWC3_DSTS_DEVCTRLHLT)
1411 break;
1412 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001413 timeout--;
1414 if (!timeout)
1415 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001416 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 } while (1);
1418
1419 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1420 dwc->gadget_driver
1421 ? dwc->gadget_driver->function : "no-function",
1422 is_on ? "connect" : "disconnect");
1423}
1424
1425static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1426{
1427 struct dwc3 *dwc = gadget_to_dwc(g);
1428 unsigned long flags;
1429
1430 is_on = !!is_on;
1431
1432 spin_lock_irqsave(&dwc->lock, flags);
1433 dwc3_gadget_run_stop(dwc, is_on);
1434 spin_unlock_irqrestore(&dwc->lock, flags);
1435
1436 return 0;
1437}
1438
1439static int dwc3_gadget_start(struct usb_gadget *g,
1440 struct usb_gadget_driver *driver)
1441{
1442 struct dwc3 *dwc = gadget_to_dwc(g);
1443 struct dwc3_ep *dep;
1444 unsigned long flags;
1445 int ret = 0;
1446 u32 reg;
1447
1448 spin_lock_irqsave(&dwc->lock, flags);
1449
1450 if (dwc->gadget_driver) {
1451 dev_err(dwc->dev, "%s is already bound to %s\n",
1452 dwc->gadget.name,
1453 dwc->gadget_driver->driver.name);
1454 ret = -EBUSY;
1455 goto err0;
1456 }
1457
1458 dwc->gadget_driver = driver;
1459 dwc->gadget.dev.driver = &driver->driver;
1460
Felipe Balbi72246da2011-08-19 18:10:58 +03001461 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1462 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001463
1464 /**
1465 * WORKAROUND: DWC3 revision < 2.20a have an issue
1466 * which would cause metastability state on Run/Stop
1467 * bit if we try to force the IP to USB2-only mode.
1468 *
1469 * Because of that, we cannot configure the IP to any
1470 * speed other than the SuperSpeed
1471 *
1472 * Refers to:
1473 *
1474 * STAR#9000525659: Clock Domain Crossing on DCTL in
1475 * USB 2.0 Mode
1476 */
1477 if (dwc->revision < DWC3_REVISION_220A)
1478 reg |= DWC3_DCFG_SUPERSPEED;
1479 else
1480 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001481 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1482
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001483 dwc->start_config_issued = false;
1484
Felipe Balbi72246da2011-08-19 18:10:58 +03001485 /* Start with SuperSpeed Default */
1486 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1487
1488 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001489 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001490 if (ret) {
1491 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1492 goto err0;
1493 }
1494
1495 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001496 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001497 if (ret) {
1498 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1499 goto err1;
1500 }
1501
1502 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001503 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001504 dwc3_ep0_out_start(dwc);
1505
1506 spin_unlock_irqrestore(&dwc->lock, flags);
1507
1508 return 0;
1509
1510err1:
1511 __dwc3_gadget_ep_disable(dwc->eps[0]);
1512
1513err0:
1514 spin_unlock_irqrestore(&dwc->lock, flags);
1515
1516 return ret;
1517}
1518
1519static int dwc3_gadget_stop(struct usb_gadget *g,
1520 struct usb_gadget_driver *driver)
1521{
1522 struct dwc3 *dwc = gadget_to_dwc(g);
1523 unsigned long flags;
1524
1525 spin_lock_irqsave(&dwc->lock, flags);
1526
1527 __dwc3_gadget_ep_disable(dwc->eps[0]);
1528 __dwc3_gadget_ep_disable(dwc->eps[1]);
1529
1530 dwc->gadget_driver = NULL;
1531 dwc->gadget.dev.driver = NULL;
1532
1533 spin_unlock_irqrestore(&dwc->lock, flags);
1534
1535 return 0;
1536}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001537
Felipe Balbi72246da2011-08-19 18:10:58 +03001538static const struct usb_gadget_ops dwc3_gadget_ops = {
1539 .get_frame = dwc3_gadget_get_frame,
1540 .wakeup = dwc3_gadget_wakeup,
1541 .set_selfpowered = dwc3_gadget_set_selfpowered,
1542 .pullup = dwc3_gadget_pullup,
1543 .udc_start = dwc3_gadget_start,
1544 .udc_stop = dwc3_gadget_stop,
1545};
1546
1547/* -------------------------------------------------------------------------- */
1548
1549static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1550{
1551 struct dwc3_ep *dep;
1552 u8 epnum;
1553
1554 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1555
1556 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1557 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1558 if (!dep) {
1559 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1560 epnum);
1561 return -ENOMEM;
1562 }
1563
1564 dep->dwc = dwc;
1565 dep->number = epnum;
1566 dwc->eps[epnum] = dep;
1567
1568 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1569 (epnum & 1) ? "in" : "out");
1570 dep->endpoint.name = dep->name;
1571 dep->direction = (epnum & 1);
1572
1573 if (epnum == 0 || epnum == 1) {
1574 dep->endpoint.maxpacket = 512;
1575 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1576 if (!epnum)
1577 dwc->gadget.ep0 = &dep->endpoint;
1578 } else {
1579 int ret;
1580
1581 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001582 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001583 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1584 list_add_tail(&dep->endpoint.ep_list,
1585 &dwc->gadget.ep_list);
1586
1587 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001588 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001589 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001590 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001591
Felipe Balbi72246da2011-08-19 18:10:58 +03001592 INIT_LIST_HEAD(&dep->request_list);
1593 INIT_LIST_HEAD(&dep->req_queued);
1594 }
1595
1596 return 0;
1597}
1598
1599static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1600{
1601 struct dwc3_ep *dep;
1602 u8 epnum;
1603
1604 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1605 dep = dwc->eps[epnum];
1606 dwc3_free_trb_pool(dep);
1607
1608 if (epnum != 0 && epnum != 1)
1609 list_del(&dep->endpoint.ep_list);
1610
1611 kfree(dep);
1612 }
1613}
1614
1615static void dwc3_gadget_release(struct device *dev)
1616{
1617 dev_dbg(dev, "%s\n", __func__);
1618}
1619
1620/* -------------------------------------------------------------------------- */
1621static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1622 const struct dwc3_event_depevt *event, int status)
1623{
1624 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001625 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001626 unsigned int count;
1627 unsigned int s_pkt = 0;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301628 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001629
1630 do {
1631 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001632 if (!req) {
1633 WARN_ON_ONCE(1);
1634 return 1;
1635 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001636
Felipe Balbif6bafc62012-02-06 11:04:53 +02001637 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001638
Felipe Balbif6bafc62012-02-06 11:04:53 +02001639 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001640 /*
1641 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001642 * can do. If we don't clean it up we loop forever. If
1643 * we skip the TRB then it gets overwritten after a
1644 * while since we use them in a ring buffer. A BUG()
1645 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001646 * fixes the root cause instead of looking away :)
1647 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001648 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1649 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001650 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001651
1652 if (dep->direction) {
1653 if (count) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301654 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1655 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1656 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1657 dep->name);
1658 dep->current_uf = event->parameters &
1659 ~(dep->interval - 1);
1660 dep->flags |= DWC3_EP_MISSED_ISOC;
1661 } else {
1662 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1663 dep->name);
1664 status = -ECONNRESET;
1665 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001666 }
1667 } else {
1668 if (count && (event->status & DEPEVT_STATUS_SHORT))
1669 s_pkt = 1;
1670 }
1671
1672 /*
1673 * We assume here we will always receive the entire data block
1674 * which we should receive. Meaning, if we program RX to
1675 * receive 4K but we receive only 2K, we assume that's all we
1676 * should receive and we simply bounce the request back to the
1677 * gadget driver for further processing.
1678 */
1679 req->request.actual += req->request.length - count;
1680 dwc3_gadget_giveback(dep, req, status);
1681 if (s_pkt)
1682 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001683 if ((event->status & DEPEVT_STATUS_LST) &&
1684 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001685 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001686 if ((event->status & DEPEVT_STATUS_IOC) &&
1687 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001688 break;
1689 } while (1);
1690
Felipe Balbif6bafc62012-02-06 11:04:53 +02001691 if ((event->status & DEPEVT_STATUS_IOC) &&
1692 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 return 0;
1694 return 1;
1695}
1696
1697static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1698 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1699 int start_new)
1700{
1701 unsigned status = 0;
1702 int clean_busy;
1703
1704 if (event->status & DEPEVT_STATUS_BUSERR)
1705 status = -ECONNRESET;
1706
Paul Zimmerman1d046792012-02-15 18:56:56 -08001707 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001708 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001709 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001710
1711 /*
1712 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1713 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1714 */
1715 if (dwc->revision < DWC3_REVISION_183A) {
1716 u32 reg;
1717 int i;
1718
1719 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1720 struct dwc3_ep *dep = dwc->eps[i];
1721
1722 if (!(dep->flags & DWC3_EP_ENABLED))
1723 continue;
1724
1725 if (!list_empty(&dep->req_queued))
1726 return;
1727 }
1728
1729 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1730 reg |= dwc->u1u2;
1731 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1732
1733 dwc->u1u2 = 0;
1734 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001735}
1736
Felipe Balbi72246da2011-08-19 18:10:58 +03001737static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1738 const struct dwc3_event_depevt *event)
1739{
1740 struct dwc3 *dwc = dep->dwc;
1741 struct dwc3_event_depevt mod_ev = *event;
1742
1743 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001744 * We were asked to remove one request. It is possible that this
1745 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 * transfer index. Since we stopped the complete endpoint we don't
1747 * know how many requests were already completed (and not yet)
1748 * reported and how could be done (later). We purge them all until
1749 * the end of the list.
1750 */
1751 mod_ev.status = DEPEVT_STATUS_LST;
1752 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1753 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001754 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001755}
1756
1757static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1758 const struct dwc3_event_depevt *event)
1759{
1760 u32 param = event->parameters;
1761 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1762
1763 switch (cmd_type) {
1764 case DWC3_DEPCMD_ENDTRANSFER:
1765 dwc3_process_ep_cmd_complete(dep, event);
1766 break;
1767 case DWC3_DEPCMD_STARTTRANSFER:
1768 dep->res_trans_idx = param & 0x7f;
1769 break;
1770 default:
1771 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1772 __func__, cmd_type);
1773 break;
1774 };
1775}
1776
1777static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1778 const struct dwc3_event_depevt *event)
1779{
1780 struct dwc3_ep *dep;
1781 u8 epnum = event->endpoint_number;
1782
1783 dep = dwc->eps[epnum];
1784
1785 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1786 dwc3_ep_event_string(event->endpoint_event));
1787
1788 if (epnum == 0 || epnum == 1) {
1789 dwc3_ep0_interrupt(dwc, event);
1790 return;
1791 }
1792
1793 switch (event->endpoint_event) {
1794 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001795 dep->res_trans_idx = 0;
1796
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001797 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001798 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1799 dep->name);
1800 return;
1801 }
1802
1803 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1804 break;
1805 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001806 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001807 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1808 dep->name);
1809 return;
1810 }
1811
1812 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1813 break;
1814 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001815 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001816 dwc3_gadget_start_isoc(dwc, dep, event);
1817 } else {
1818 int ret;
1819
1820 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001821 dep->name, event->status &
1822 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001823 ? "Transfer Active"
1824 : "Transfer Not Active");
1825
1826 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1827 if (!ret || ret == -EBUSY)
1828 return;
1829
1830 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1831 dep->name);
1832 }
1833
1834 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001835 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001836 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001837 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1838 dep->name);
1839 return;
1840 }
1841
1842 switch (event->status) {
1843 case DEPEVT_STREAMEVT_FOUND:
1844 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1845 event->parameters);
1846
1847 break;
1848 case DEPEVT_STREAMEVT_NOTFOUND:
1849 /* FALLTHROUGH */
1850 default:
1851 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1852 }
1853 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001854 case DWC3_DEPEVT_RXTXFIFOEVT:
1855 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1856 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001857 case DWC3_DEPEVT_EPCMDCMPLT:
1858 dwc3_ep_cmd_compl(dep, event);
1859 break;
1860 }
1861}
1862
1863static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1864{
1865 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1866 spin_unlock(&dwc->lock);
1867 dwc->gadget_driver->disconnect(&dwc->gadget);
1868 spin_lock(&dwc->lock);
1869 }
1870}
1871
1872static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1873{
1874 struct dwc3_ep *dep;
1875 struct dwc3_gadget_ep_cmd_params params;
1876 u32 cmd;
1877 int ret;
1878
1879 dep = dwc->eps[epnum];
1880
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001881 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001882 if (dep->res_trans_idx) {
1883 cmd = DWC3_DEPCMD_ENDTRANSFER;
1884 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1885 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1886 memset(&params, 0, sizeof(params));
1887 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1888 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001889 dep->res_trans_idx = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 }
1891}
1892
1893static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1894{
1895 u32 epnum;
1896
1897 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1898 struct dwc3_ep *dep;
1899
1900 dep = dwc->eps[epnum];
1901 if (!(dep->flags & DWC3_EP_ENABLED))
1902 continue;
1903
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001904 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001905 }
1906}
1907
1908static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1909{
1910 u32 epnum;
1911
1912 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1913 struct dwc3_ep *dep;
1914 struct dwc3_gadget_ep_cmd_params params;
1915 int ret;
1916
1917 dep = dwc->eps[epnum];
1918
1919 if (!(dep->flags & DWC3_EP_STALL))
1920 continue;
1921
1922 dep->flags &= ~DWC3_EP_STALL;
1923
1924 memset(&params, 0, sizeof(params));
1925 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1926 DWC3_DEPCMD_CLEARSTALL, &params);
1927 WARN_ON_ONCE(ret);
1928 }
1929}
1930
1931static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1932{
Felipe Balbic4430a22012-05-24 10:30:01 +03001933 int reg;
1934
Felipe Balbi72246da2011-08-19 18:10:58 +03001935 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03001936
1937 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1938 reg &= ~DWC3_DCTL_INITU1ENA;
1939 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1940
1941 reg &= ~DWC3_DCTL_INITU2ENA;
1942 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001943
1944 dwc3_stop_active_transfers(dwc);
1945 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001946 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001947
1948 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001949 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001950}
1951
Paul Zimmermand7a46a82012-04-27 12:54:05 +03001952static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001953{
1954 u32 reg;
1955
1956 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1957
Paul Zimmermand7a46a82012-04-27 12:54:05 +03001958 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001959 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermand7a46a82012-04-27 12:54:05 +03001960 else
1961 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001962
1963 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1964}
1965
Paul Zimmermand7a46a82012-04-27 12:54:05 +03001966static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001967{
1968 u32 reg;
1969
1970 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1971
Paul Zimmermand7a46a82012-04-27 12:54:05 +03001972 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001973 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermand7a46a82012-04-27 12:54:05 +03001974 else
1975 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001976
1977 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1978}
1979
1980static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1981{
1982 u32 reg;
1983
1984 dev_vdbg(dwc->dev, "%s\n", __func__);
1985
Felipe Balbidf62df52011-10-14 15:11:49 +03001986 /*
1987 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1988 * would cause a missing Disconnect Event if there's a
1989 * pending Setup Packet in the FIFO.
1990 *
1991 * There's no suggested workaround on the official Bug
1992 * report, which states that "unless the driver/application
1993 * is doing any special handling of a disconnect event,
1994 * there is no functional issue".
1995 *
1996 * Unfortunately, it turns out that we _do_ some special
1997 * handling of a disconnect event, namely complete all
1998 * pending transfers, notify gadget driver of the
1999 * disconnection, and so on.
2000 *
2001 * Our suggested workaround is to follow the Disconnect
2002 * Event steps here, instead, based on a setup_packet_pending
2003 * flag. Such flag gets set whenever we have a XferNotReady
2004 * event on EP0 and gets cleared on XferComplete for the
2005 * same endpoint.
2006 *
2007 * Refers to:
2008 *
2009 * STAR#9000466709: RTL: Device : Disconnect event not
2010 * generated if setup packet pending in FIFO
2011 */
2012 if (dwc->revision < DWC3_REVISION_188A) {
2013 if (dwc->setup_packet_pending)
2014 dwc3_gadget_disconnect_interrupt(dwc);
2015 }
2016
Felipe Balbi961906e2011-12-20 15:37:21 +02002017 /* after reset -> Default State */
2018 dwc->dev_state = DWC3_DEFAULT_STATE;
2019
Paul Zimmerman802fde92012-04-27 13:10:52 +03002020 /* Recent versions support automatic phy suspend and don't need this */
2021 if (dwc->revision < DWC3_REVISION_194A) {
2022 /* Resume PHYs */
2023 dwc3_gadget_usb2_phy_suspend(dwc, false);
2024 dwc3_gadget_usb3_phy_suspend(dwc, false);
2025 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002026
2027 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2028 dwc3_disconnect_gadget(dwc);
2029
2030 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2031 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +02002032 reg &= ~(DWC3_DCTL_INITU1ENA | DWC3_DCTL_INITU2ENA);
Gerard CAUVY5cbe8c22012-05-24 12:47:36 +03002033 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
Felipe Balbi72246da2011-08-19 18:10:58 +03002034 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002035 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002036
2037 dwc3_stop_active_transfers(dwc);
2038 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002039 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002040
2041 /* Reset device address to zero */
2042 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2043 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2044 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002045}
2046
2047static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2048{
2049 u32 reg;
2050 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2051
2052 /*
2053 * We change the clock only at SS but I dunno why I would want to do
2054 * this. Maybe it becomes part of the power saving plan.
2055 */
2056
2057 if (speed != DWC3_DSTS_SUPERSPEED)
2058 return;
2059
2060 /*
2061 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2062 * each time on Connect Done.
2063 */
2064 if (!usb30_clock)
2065 return;
2066
2067 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2068 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2069 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2070}
2071
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002072static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002073{
2074 switch (speed) {
2075 case USB_SPEED_SUPER:
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002076 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002077 break;
2078 case USB_SPEED_HIGH:
2079 case USB_SPEED_FULL:
2080 case USB_SPEED_LOW:
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002081 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002082 break;
2083 }
2084}
2085
2086static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2087{
2088 struct dwc3_gadget_ep_cmd_params params;
2089 struct dwc3_ep *dep;
2090 int ret;
2091 u32 reg;
2092 u8 speed;
2093
2094 dev_vdbg(dwc->dev, "%s\n", __func__);
2095
2096 memset(&params, 0x00, sizeof(params));
2097
Felipe Balbi72246da2011-08-19 18:10:58 +03002098 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2099 speed = reg & DWC3_DSTS_CONNECTSPD;
2100 dwc->speed = speed;
2101
2102 dwc3_update_ram_clk_sel(dwc, speed);
2103
2104 switch (speed) {
2105 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002106 /*
2107 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2108 * would cause a missing USB3 Reset event.
2109 *
2110 * In such situations, we should force a USB3 Reset
2111 * event by calling our dwc3_gadget_reset_interrupt()
2112 * routine.
2113 *
2114 * Refers to:
2115 *
2116 * STAR#9000483510: RTL: SS : USB3 reset event may
2117 * not be generated always when the link enters poll
2118 */
2119 if (dwc->revision < DWC3_REVISION_190A)
2120 dwc3_gadget_reset_interrupt(dwc);
2121
Felipe Balbi72246da2011-08-19 18:10:58 +03002122 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2123 dwc->gadget.ep0->maxpacket = 512;
2124 dwc->gadget.speed = USB_SPEED_SUPER;
2125 break;
2126 case DWC3_DCFG_HIGHSPEED:
2127 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2128 dwc->gadget.ep0->maxpacket = 64;
2129 dwc->gadget.speed = USB_SPEED_HIGH;
2130 break;
2131 case DWC3_DCFG_FULLSPEED2:
2132 case DWC3_DCFG_FULLSPEED1:
2133 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2134 dwc->gadget.ep0->maxpacket = 64;
2135 dwc->gadget.speed = USB_SPEED_FULL;
2136 break;
2137 case DWC3_DCFG_LOWSPEED:
2138 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2139 dwc->gadget.ep0->maxpacket = 8;
2140 dwc->gadget.speed = USB_SPEED_LOW;
2141 break;
2142 }
2143
Paul Zimmerman802fde92012-04-27 13:10:52 +03002144 /* Recent versions support automatic phy suspend and don't need this */
2145 if (dwc->revision < DWC3_REVISION_194A) {
2146 /* Suspend unneeded PHY */
2147 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2148 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002149
2150 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002151 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002152 if (ret) {
2153 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2154 return;
2155 }
2156
2157 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002158 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002159 if (ret) {
2160 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2161 return;
2162 }
2163
2164 /*
2165 * Configure PHY via GUSB3PIPECTLn if required.
2166 *
2167 * Update GTXFIFOSIZn
2168 *
2169 * In both cases reset values should be sufficient.
2170 */
2171}
2172
2173static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2174{
2175 dev_vdbg(dwc->dev, "%s\n", __func__);
2176
2177 /*
2178 * TODO take core out of low power mode when that's
2179 * implemented.
2180 */
2181
2182 dwc->gadget_driver->resume(&dwc->gadget);
2183}
2184
2185static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2186 unsigned int evtinfo)
2187{
Felipe Balbifae2b902011-10-14 13:00:30 +03002188 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2189
2190 /*
2191 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2192 * on the link partner, the USB session might do multiple entry/exit
2193 * of low power states before a transfer takes place.
2194 *
2195 * Due to this problem, we might experience lower throughput. The
2196 * suggested workaround is to disable DCTL[12:9] bits if we're
2197 * transitioning from U1/U2 to U0 and enable those bits again
2198 * after a transfer completes and there are no pending transfers
2199 * on any of the enabled endpoints.
2200 *
2201 * This is the first half of that workaround.
2202 *
2203 * Refers to:
2204 *
2205 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2206 * core send LGO_Ux entering U0
2207 */
2208 if (dwc->revision < DWC3_REVISION_183A) {
2209 if (next == DWC3_LINK_STATE_U0) {
2210 u32 u1u2;
2211 u32 reg;
2212
2213 switch (dwc->link_state) {
2214 case DWC3_LINK_STATE_U1:
2215 case DWC3_LINK_STATE_U2:
2216 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2217 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2218 | DWC3_DCTL_ACCEPTU2ENA
2219 | DWC3_DCTL_INITU1ENA
2220 | DWC3_DCTL_ACCEPTU1ENA);
2221
2222 if (!dwc->u1u2)
2223 dwc->u1u2 = reg & u1u2;
2224
2225 reg &= ~u1u2;
2226
2227 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2228 break;
2229 default:
2230 /* do nothing */
2231 break;
2232 }
2233 }
2234 }
2235
2236 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002237
2238 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002239}
2240
2241static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2242 const struct dwc3_event_devt *event)
2243{
2244 switch (event->type) {
2245 case DWC3_DEVICE_EVENT_DISCONNECT:
2246 dwc3_gadget_disconnect_interrupt(dwc);
2247 break;
2248 case DWC3_DEVICE_EVENT_RESET:
2249 dwc3_gadget_reset_interrupt(dwc);
2250 break;
2251 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2252 dwc3_gadget_conndone_interrupt(dwc);
2253 break;
2254 case DWC3_DEVICE_EVENT_WAKEUP:
2255 dwc3_gadget_wakeup_interrupt(dwc);
2256 break;
2257 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2258 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2259 break;
2260 case DWC3_DEVICE_EVENT_EOPF:
2261 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2262 break;
2263 case DWC3_DEVICE_EVENT_SOF:
2264 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2265 break;
2266 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2267 dev_vdbg(dwc->dev, "Erratic Error\n");
2268 break;
2269 case DWC3_DEVICE_EVENT_CMD_CMPL:
2270 dev_vdbg(dwc->dev, "Command Complete\n");
2271 break;
2272 case DWC3_DEVICE_EVENT_OVERFLOW:
2273 dev_vdbg(dwc->dev, "Overflow\n");
2274 break;
2275 default:
2276 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2277 }
2278}
2279
2280static void dwc3_process_event_entry(struct dwc3 *dwc,
2281 const union dwc3_event *event)
2282{
2283 /* Endpoint IRQ, handle it and return early */
2284 if (event->type.is_devspec == 0) {
2285 /* depevt */
2286 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2287 }
2288
2289 switch (event->type.type) {
2290 case DWC3_EVENT_TYPE_DEV:
2291 dwc3_gadget_interrupt(dwc, &event->devt);
2292 break;
2293 /* REVISIT what to do with Carkit and I2C events ? */
2294 default:
2295 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2296 }
2297}
2298
2299static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2300{
2301 struct dwc3_event_buffer *evt;
2302 int left;
2303 u32 count;
2304
2305 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2306 count &= DWC3_GEVNTCOUNT_MASK;
2307 if (!count)
2308 return IRQ_NONE;
2309
2310 evt = dwc->ev_buffs[buf];
2311 left = count;
2312
2313 while (left > 0) {
2314 union dwc3_event event;
2315
Felipe Balbid70d8442012-02-06 13:40:17 +02002316 event.raw = *(u32 *) (evt->buf + evt->lpos);
2317
Felipe Balbi72246da2011-08-19 18:10:58 +03002318 dwc3_process_event_entry(dwc, &event);
2319 /*
2320 * XXX we wrap around correctly to the next entry as almost all
2321 * entries are 4 bytes in size. There is one entry which has 12
2322 * bytes which is a regular entry followed by 8 bytes data. ATM
2323 * I don't know how things are organized if were get next to the
2324 * a boundary so I worry about that once we try to handle that.
2325 */
2326 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2327 left -= 4;
2328
2329 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2330 }
2331
2332 return IRQ_HANDLED;
2333}
2334
2335static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2336{
2337 struct dwc3 *dwc = _dwc;
2338 int i;
2339 irqreturn_t ret = IRQ_NONE;
2340
2341 spin_lock(&dwc->lock);
2342
Felipe Balbi9f622b22011-10-12 10:31:04 +03002343 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002344 irqreturn_t status;
2345
2346 status = dwc3_process_event_buf(dwc, i);
2347 if (status == IRQ_HANDLED)
2348 ret = status;
2349 }
2350
2351 spin_unlock(&dwc->lock);
2352
2353 return ret;
2354}
2355
2356/**
2357 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002358 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002359 *
2360 * Returns 0 on success otherwise negative errno.
2361 */
2362int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2363{
2364 u32 reg;
2365 int ret;
2366 int irq;
2367
2368 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2369 &dwc->ctrl_req_addr, GFP_KERNEL);
2370 if (!dwc->ctrl_req) {
2371 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2372 ret = -ENOMEM;
2373 goto err0;
2374 }
2375
2376 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2377 &dwc->ep0_trb_addr, GFP_KERNEL);
2378 if (!dwc->ep0_trb) {
2379 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2380 ret = -ENOMEM;
2381 goto err1;
2382 }
2383
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002384 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002385 if (!dwc->setup_buf) {
2386 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2387 ret = -ENOMEM;
2388 goto err2;
2389 }
2390
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002391 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002392 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2393 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002394 if (!dwc->ep0_bounce) {
2395 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2396 ret = -ENOMEM;
2397 goto err3;
2398 }
2399
Felipe Balbi72246da2011-08-19 18:10:58 +03002400 dev_set_name(&dwc->gadget.dev, "gadget");
2401
2402 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002403 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002404 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2405 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002406 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002407
2408 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2409
2410 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2411 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2412 dwc->gadget.dev.release = dwc3_gadget_release;
2413 dwc->gadget.name = "dwc3-gadget";
2414
2415 /*
2416 * REVISIT: Here we should clear all pending IRQs to be
2417 * sure we're starting from a well known location.
2418 */
2419
2420 ret = dwc3_gadget_init_endpoints(dwc);
2421 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002422 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002423
2424 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2425
2426 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2427 "dwc3", dwc);
2428 if (ret) {
2429 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2430 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002431 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002432 }
2433
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +02002434 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2435 reg |= DWC3_DCFG_LPM_CAP;
2436 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2437
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 /* Enable all but Start and End of Frame IRQs */
2439 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2440 DWC3_DEVTEN_EVNTOVERFLOWEN |
2441 DWC3_DEVTEN_CMDCMPLTEN |
2442 DWC3_DEVTEN_ERRTICERREN |
2443 DWC3_DEVTEN_WKUPEVTEN |
2444 DWC3_DEVTEN_ULSTCNGEN |
2445 DWC3_DEVTEN_CONNECTDONEEN |
2446 DWC3_DEVTEN_USBRSTEN |
2447 DWC3_DEVTEN_DISCONNEVTEN);
2448 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2449
Paul Zimmerman802fde92012-04-27 13:10:52 +03002450 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2451 if (dwc->revision >= DWC3_REVISION_194A) {
2452 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2453 reg |= DWC3_DCFG_LPM_CAP;
2454 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2455
2456 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2457 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2458
2459 /* TODO: This should be configurable */
2460 reg |= DWC3_DCTL_HIRD_THRES(31);
2461
2462 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2463
2464 dwc3_gadget_usb2_phy_suspend(dwc, true);
2465 dwc3_gadget_usb3_phy_suspend(dwc, true);
2466 }
2467
Felipe Balbi72246da2011-08-19 18:10:58 +03002468 ret = device_register(&dwc->gadget.dev);
2469 if (ret) {
2470 dev_err(dwc->dev, "failed to register gadget device\n");
2471 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002472 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002473 }
2474
2475 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2476 if (ret) {
2477 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002478 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002479 }
2480
2481 return 0;
2482
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002483err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002484 device_unregister(&dwc->gadget.dev);
2485
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002486err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002487 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2488 free_irq(irq, dwc);
2489
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002490err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002491 dwc3_gadget_free_endpoints(dwc);
2492
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002493err4:
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002494 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2495 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002496
Felipe Balbi72246da2011-08-19 18:10:58 +03002497err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002498 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002499
2500err2:
2501 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2502 dwc->ep0_trb, dwc->ep0_trb_addr);
2503
2504err1:
2505 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2506 dwc->ctrl_req, dwc->ctrl_req_addr);
2507
2508err0:
2509 return ret;
2510}
2511
2512void dwc3_gadget_exit(struct dwc3 *dwc)
2513{
2514 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002515
2516 usb_del_gadget_udc(&dwc->gadget);
2517 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2518
2519 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2520 free_irq(irq, dwc);
2521
Felipe Balbi72246da2011-08-19 18:10:58 +03002522 dwc3_gadget_free_endpoints(dwc);
2523
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002524 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2525 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002526
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002527 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002528
2529 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2530 dwc->ep0_trb, dwc->ep0_trb_addr);
2531
2532 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2533 dwc->ctrl_req, dwc->ctrl_req_addr);
2534
2535 device_unregister(&dwc->gadget.dev);
2536}