blob: e3ee85d9f5d9fe443440499e710a415edd18d2fc [file] [log] [blame]
Felipe Balbi4dc64e52011-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
5 * All rights reserved.
6 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the above-listed copyright holders may not be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2, as published by the Free
25 * Software Foundation.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <linux/kernel.h>
41#include <linux/delay.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/platform_device.h>
45#include <linux/pm_runtime.h>
46#include <linux/interrupt.h>
47#include <linux/io.h>
48#include <linux/list.h>
49#include <linux/dma-mapping.h>
50
51#include <linux/usb/ch9.h>
52#include <linux/usb/gadget.h>
53
54#include "core.h"
55#include "gadget.h"
56#include "io.h"
57
58#define DMA_ADDR_INVALID (~(dma_addr_t)0)
59
60void dwc3_map_buffer_to_dma(struct dwc3_request *req)
61{
62 struct dwc3 *dwc = req->dep->dwc;
63
64 if (req->request.dma == DMA_ADDR_INVALID) {
65 req->request.dma = dma_map_single(dwc->dev, req->request.buf,
66 req->request.length, req->direction
67 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
68 req->mapped = true;
69 } else {
70 dma_sync_single_for_device(dwc->dev, req->request.dma,
71 req->request.length, req->direction
72 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
73 req->mapped = false;
74 }
75}
76
77void dwc3_unmap_buffer_from_dma(struct dwc3_request *req)
78{
79 struct dwc3 *dwc = req->dep->dwc;
80
81 if (req->mapped) {
82 dma_unmap_single(dwc->dev, req->request.dma,
83 req->request.length, req->direction
84 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
85 req->mapped = 0;
Felipe Balbi162e1282011-08-27 15:10:09 +030086 req->request.dma = DMA_ADDR_INVALID;
Felipe Balbi4dc64e52011-08-19 18:10:58 +030087 } else {
88 dma_sync_single_for_cpu(dwc->dev, req->request.dma,
89 req->request.length, req->direction
90 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
91 }
92}
93
94void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
95 int status)
96{
97 struct dwc3 *dwc = dep->dwc;
98
99 if (req->queued) {
100 dep->busy_slot++;
101 /*
102 * Skip LINK TRB. We can't use req->trb and check for
103 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
104 * completed (not the LINK TRB).
105 */
106 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
107 usb_endpoint_xfer_isoc(dep->desc))
108 dep->busy_slot++;
109 }
110 list_del(&req->list);
111
112 if (req->request.status == -EINPROGRESS)
113 req->request.status = status;
114
115 dwc3_unmap_buffer_from_dma(req);
116
117 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
118 req, dep->name, req->request.actual,
119 req->request.length, status);
120
121 spin_unlock(&dwc->lock);
122 req->request.complete(&req->dep->endpoint, &req->request);
123 spin_lock(&dwc->lock);
124}
125
126static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
127{
128 switch (cmd) {
129 case DWC3_DEPCMD_DEPSTARTCFG:
130 return "Start New Configuration";
131 case DWC3_DEPCMD_ENDTRANSFER:
132 return "End Transfer";
133 case DWC3_DEPCMD_UPDATETRANSFER:
134 return "Update Transfer";
135 case DWC3_DEPCMD_STARTTRANSFER:
136 return "Start Transfer";
137 case DWC3_DEPCMD_CLEARSTALL:
138 return "Clear Stall";
139 case DWC3_DEPCMD_SETSTALL:
140 return "Set Stall";
141 case DWC3_DEPCMD_GETSEQNUMBER:
142 return "Get Data Sequence Number";
143 case DWC3_DEPCMD_SETTRANSFRESOURCE:
144 return "Set Endpoint Transfer Resource";
145 case DWC3_DEPCMD_SETEPCONFIG:
146 return "Set Endpoint Configuration";
147 default:
148 return "UNKNOWN command";
149 }
150}
151
152int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
153 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
154{
155 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +0200156 u32 timeout = 500;
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300157 u32 reg;
158
159 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
160 dep->name,
161 dwc3_gadget_ep_cmd_string(cmd), params->param0.raw,
162 params->param1.raw, params->param2.raw);
163
164 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0.raw);
165 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1.raw);
166 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2.raw);
167
168 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
169 do {
170 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
171 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbic7dbe4f2011-08-27 20:29:58 +0300172 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
173 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300174 return 0;
175 }
176
177 /*
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300178 * We can't sleep here, because it is also called from
179 * interrupt context.
180 */
181 timeout--;
182 if (!timeout)
183 return -ETIMEDOUT;
184
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +0200185 udelay(1);
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300186 } while (1);
187}
188
189static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
190 struct dwc3_trb_hw *trb)
191{
192 u32 offset = trb - dep->trb_pool;
193
194 return dep->trb_pool_dma + offset;
195}
196
197static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
198{
199 struct dwc3 *dwc = dep->dwc;
200
201 if (dep->trb_pool)
202 return 0;
203
204 if (dep->number == 0 || dep->number == 1)
205 return 0;
206
207 dep->trb_pool = dma_alloc_coherent(dwc->dev,
208 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
209 &dep->trb_pool_dma, GFP_KERNEL);
210 if (!dep->trb_pool) {
211 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
212 dep->name);
213 return -ENOMEM;
214 }
215
216 return 0;
217}
218
219static void dwc3_free_trb_pool(struct dwc3_ep *dep)
220{
221 struct dwc3 *dwc = dep->dwc;
222
223 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
224 dep->trb_pool, dep->trb_pool_dma);
225
226 dep->trb_pool = NULL;
227 dep->trb_pool_dma = 0;
228}
229
230static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
231{
232 struct dwc3_gadget_ep_cmd_params params;
233 u32 cmd;
234
235 memset(&params, 0x00, sizeof(params));
236
237 if (dep->number != 1) {
238 cmd = DWC3_DEPCMD_DEPSTARTCFG;
239 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
240 if (dep->number > 1)
241 cmd |= DWC3_DEPCMD_PARAM(2);
242
243 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
244 }
245
246 return 0;
247}
248
249static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
250 const struct usb_endpoint_descriptor *desc)
251{
252 struct dwc3_gadget_ep_cmd_params params;
253
254 memset(&params, 0x00, sizeof(params));
255
256 params.param0.depcfg.ep_type = usb_endpoint_type(desc);
257 params.param0.depcfg.max_packet_size =
258 le16_to_cpu(desc->wMaxPacketSize);
259
260 params.param1.depcfg.xfer_complete_enable = true;
261 params.param1.depcfg.xfer_not_ready_enable = true;
262
263 if (usb_endpoint_xfer_isoc(desc))
264 params.param1.depcfg.xfer_in_progress_enable = true;
265
266 /*
267 * We are doing 1:1 mapping for endpoints, meaning
268 * Physical Endpoints 2 maps to Logical Endpoint 2 and
269 * so on. We consider the direction bit as part of the physical
270 * endpoint number. So USB endpoint 0x81 is 0x03.
271 */
272 params.param1.depcfg.ep_number = dep->number;
273
274 /*
275 * We must use the lower 16 TX FIFOs even though
276 * HW might have more
277 */
278 if (dep->direction)
279 params.param0.depcfg.fifo_number = dep->number >> 1;
280
281 if (desc->bInterval) {
282 params.param1.depcfg.binterval_m1 = desc->bInterval - 1;
283 dep->interval = 1 << (desc->bInterval - 1);
284 }
285
286 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
287 DWC3_DEPCMD_SETEPCONFIG, &params);
288}
289
290static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
291{
292 struct dwc3_gadget_ep_cmd_params params;
293
294 memset(&params, 0x00, sizeof(params));
295
296 params.param0.depxfercfg.number_xfer_resources = 1;
297
298 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
299 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
300}
301
302/**
303 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
304 * @dep: endpoint to be initialized
305 * @desc: USB Endpoint Descriptor
306 *
307 * Caller should take care of locking
308 */
309static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
310 const struct usb_endpoint_descriptor *desc)
311{
312 struct dwc3 *dwc = dep->dwc;
313 u32 reg;
314 int ret = -ENOMEM;
315
316 if (!(dep->flags & DWC3_EP_ENABLED)) {
317 ret = dwc3_gadget_start_config(dwc, dep);
318 if (ret)
319 return ret;
320 }
321
322 ret = dwc3_gadget_set_ep_config(dwc, dep, desc);
323 if (ret)
324 return ret;
325
326 if (!(dep->flags & DWC3_EP_ENABLED)) {
327 struct dwc3_trb_hw *trb_st_hw;
328 struct dwc3_trb_hw *trb_link_hw;
329 struct dwc3_trb trb_link;
330
331 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
332 if (ret)
333 return ret;
334
335 dep->desc = desc;
336 dep->type = usb_endpoint_type(desc);
337 dep->flags |= DWC3_EP_ENABLED;
338
339 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
340 reg |= DWC3_DALEPENA_EP(dep->number);
341 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
342
343 if (!usb_endpoint_xfer_isoc(desc))
344 return 0;
345
346 memset(&trb_link, 0, sizeof(trb_link));
347
348 /* Link TRB for ISOC. The HWO but is never reset */
349 trb_st_hw = &dep->trb_pool[0];
350
351 trb_link.bplh = dwc3_trb_dma_offset(dep, trb_st_hw);
352 trb_link.trbctl = DWC3_TRBCTL_LINK_TRB;
353 trb_link.hwo = true;
354
355 trb_link_hw = &dep->trb_pool[DWC3_TRB_NUM - 1];
356 dwc3_trb_to_hw(&trb_link, trb_link_hw);
357 }
358
359 return 0;
360}
361
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200362static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
363static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300364{
365 struct dwc3_request *req;
366
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200367 if (!list_empty(&dep->req_queued))
368 dwc3_stop_active_transfer(dwc, dep->number);
369
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300370 while (!list_empty(&dep->request_list)) {
371 req = next_request(&dep->request_list);
372
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200373 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300374 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300375}
376
377/**
378 * __dwc3_gadget_ep_disable - Disables a HW endpoint
379 * @dep: the endpoint to disable
380 *
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200381 * This function also removes requests which are currently processed ny the
382 * hardware and those which are not yet scheduled.
383 * Caller should take care of locking.
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300384 */
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300385static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
386{
387 struct dwc3 *dwc = dep->dwc;
388 u32 reg;
389
390 dep->flags &= ~DWC3_EP_ENABLED;
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +0200391 dwc3_remove_requests(dwc, dep);
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300392
393 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
394 reg &= ~DWC3_DALEPENA_EP(dep->number);
395 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
396
397 dep->desc = NULL;
398 dep->type = 0;
399
400 return 0;
401}
402
403/* -------------------------------------------------------------------------- */
404
405static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
406 const struct usb_endpoint_descriptor *desc)
407{
408 return -EINVAL;
409}
410
411static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
412{
413 return -EINVAL;
414}
415
416/* -------------------------------------------------------------------------- */
417
418static int dwc3_gadget_ep_enable(struct usb_ep *ep,
419 const struct usb_endpoint_descriptor *desc)
420{
421 struct dwc3_ep *dep;
422 struct dwc3 *dwc;
423 unsigned long flags;
424 int ret;
425
426 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
427 pr_debug("dwc3: invalid parameters\n");
428 return -EINVAL;
429 }
430
431 if (!desc->wMaxPacketSize) {
432 pr_debug("dwc3: missing wMaxPacketSize\n");
433 return -EINVAL;
434 }
435
436 dep = to_dwc3_ep(ep);
437 dwc = dep->dwc;
438
439 switch (usb_endpoint_type(desc)) {
440 case USB_ENDPOINT_XFER_CONTROL:
441 strncat(dep->name, "-control", sizeof(dep->name));
442 break;
443 case USB_ENDPOINT_XFER_ISOC:
444 strncat(dep->name, "-isoc", sizeof(dep->name));
445 break;
446 case USB_ENDPOINT_XFER_BULK:
447 strncat(dep->name, "-bulk", sizeof(dep->name));
448 break;
449 case USB_ENDPOINT_XFER_INT:
450 strncat(dep->name, "-int", sizeof(dep->name));
451 break;
452 default:
453 dev_err(dwc->dev, "invalid endpoint transfer type\n");
454 }
455
456 if (dep->flags & DWC3_EP_ENABLED) {
457 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
458 dep->name);
459 return 0;
460 }
461
462 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
463
464 spin_lock_irqsave(&dwc->lock, flags);
465 ret = __dwc3_gadget_ep_enable(dep, desc);
466 spin_unlock_irqrestore(&dwc->lock, flags);
467
468 return ret;
469}
470
471static int dwc3_gadget_ep_disable(struct usb_ep *ep)
472{
473 struct dwc3_ep *dep;
474 struct dwc3 *dwc;
475 unsigned long flags;
476 int ret;
477
478 if (!ep) {
479 pr_debug("dwc3: invalid parameters\n");
480 return -EINVAL;
481 }
482
483 dep = to_dwc3_ep(ep);
484 dwc = dep->dwc;
485
486 if (!(dep->flags & DWC3_EP_ENABLED)) {
487 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
488 dep->name);
489 return 0;
490 }
491
492 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
493 dep->number >> 1,
494 (dep->number & 1) ? "in" : "out");
495
496 spin_lock_irqsave(&dwc->lock, flags);
497 ret = __dwc3_gadget_ep_disable(dep);
498 spin_unlock_irqrestore(&dwc->lock, flags);
499
500 return ret;
501}
502
503static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
504 gfp_t gfp_flags)
505{
506 struct dwc3_request *req;
507 struct dwc3_ep *dep = to_dwc3_ep(ep);
508 struct dwc3 *dwc = dep->dwc;
509
510 req = kzalloc(sizeof(*req), gfp_flags);
511 if (!req) {
512 dev_err(dwc->dev, "not enough memory\n");
513 return NULL;
514 }
515
516 req->epnum = dep->number;
517 req->dep = dep;
518 req->request.dma = DMA_ADDR_INVALID;
519
520 return &req->request;
521}
522
523static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
524 struct usb_request *request)
525{
526 struct dwc3_request *req = to_dwc3_request(request);
527
528 kfree(req);
529}
530
531/*
532 * dwc3_prepare_trbs - setup TRBs from requests
533 * @dep: endpoint for which requests are being prepared
534 * @starting: true if the endpoint is idle and no requests are queued.
535 *
536 * The functions goes through the requests list and setups TRBs for the
537 * transfers. The functions returns once there are not more TRBs available or
538 * it run out of requests.
539 */
540static struct dwc3_request *dwc3_prepare_trbs(struct dwc3_ep *dep,
541 bool starting)
542{
543 struct dwc3_request *req, *n, *ret = NULL;
544 struct dwc3_trb_hw *trb_hw;
545 struct dwc3_trb trb;
546 u32 trbs_left;
547
548 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
549
550 /* the first request must not be queued */
551 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
552 /*
553 * if busy & slot are equal than it is either full or empty. If we are
554 * starting to proceed requests then we are empty. Otherwise we ar
555 * full and don't do anything
556 */
557 if (!trbs_left) {
558 if (!starting)
559 return NULL;
560 trbs_left = DWC3_TRB_NUM;
561 /*
562 * In case we start from scratch, we queue the ISOC requests
563 * starting from slot 1. This is done because we use ring
564 * buffer and have no LST bit to stop us. Instead, we place
565 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
566 * after the first request so we start at slot 1 and have
567 * 7 requests proceed before we hit the first IOC.
568 * Other transfer types don't use the ring buffer and are
569 * processed from the first TRB until the last one. Since we
570 * don't wrap around we have to start at the beginning.
571 */
572 if (usb_endpoint_xfer_isoc(dep->desc)) {
573 dep->busy_slot = 1;
574 dep->free_slot = 1;
575 } else {
576 dep->busy_slot = 0;
577 dep->free_slot = 0;
578 }
579 }
580
581 /* The last TRB is a link TRB, not used for xfer */
582 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
583 return NULL;
584
585 list_for_each_entry_safe(req, n, &dep->request_list, list) {
586 unsigned int last_one = 0;
587 unsigned int cur_slot;
588
589 trb_hw = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
590 cur_slot = dep->free_slot;
591 dep->free_slot++;
592
593 /* Skip the LINK-TRB on ISOC */
594 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
595 usb_endpoint_xfer_isoc(dep->desc))
596 continue;
597
598 dwc3_gadget_move_request_queued(req);
599 memset(&trb, 0, sizeof(trb));
600 trbs_left--;
601
602 /* Is our TRB pool empty? */
603 if (!trbs_left)
604 last_one = 1;
605 /* Is this the last request? */
606 if (list_empty(&dep->request_list))
607 last_one = 1;
608
609 /*
610 * FIXME we shouldn't need to set LST bit always but we are
611 * facing some weird problem with the Hardware where it doesn't
612 * complete even though it has been previously started.
613 *
614 * While we're debugging the problem, as a workaround to
615 * multiple TRBs handling, use only one TRB at a time.
616 */
617 last_one = 1;
618
619 req->trb = trb_hw;
620 if (!ret)
621 ret = req;
622
623 trb.bplh = req->request.dma;
624
625 if (usb_endpoint_xfer_isoc(dep->desc)) {
626 trb.isp_imi = true;
627 trb.csp = true;
628 } else {
629 trb.lst = last_one;
630 }
631
632 switch (usb_endpoint_type(dep->desc)) {
633 case USB_ENDPOINT_XFER_CONTROL:
634 trb.trbctl = DWC3_TRBCTL_CONTROL_SETUP;
635 break;
636
637 case USB_ENDPOINT_XFER_ISOC:
Sebastian Andrzej Siewior15623d72011-08-22 17:42:19 +0200638 trb.trbctl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300639
640 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
641 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
642 trb.ioc = last_one;
643 break;
644
645 case USB_ENDPOINT_XFER_BULK:
646 case USB_ENDPOINT_XFER_INT:
647 trb.trbctl = DWC3_TRBCTL_NORMAL;
648 break;
649 default:
650 /*
651 * This is only possible with faulty memory because we
652 * checked it already :)
653 */
654 BUG();
655 }
656
657 trb.length = req->request.length;
658 trb.hwo = true;
659
660 dwc3_trb_to_hw(&trb, trb_hw);
661 req->trb_dma = dwc3_trb_dma_offset(dep, trb_hw);
662
663 if (last_one)
664 break;
665 }
666
667 return ret;
668}
669
670static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
671 int start_new)
672{
673 struct dwc3_gadget_ep_cmd_params params;
674 struct dwc3_request *req;
675 struct dwc3 *dwc = dep->dwc;
676 int ret;
677 u32 cmd;
678
679 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
680 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
681 return -EBUSY;
682 }
683 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
684
685 /*
686 * If we are getting here after a short-out-packet we don't enqueue any
687 * new requests as we try to set the IOC bit only on the last request.
688 */
689 if (start_new) {
690 if (list_empty(&dep->req_queued))
691 dwc3_prepare_trbs(dep, start_new);
692
693 /* req points to the first request which will be sent */
694 req = next_request(&dep->req_queued);
695 } else {
696 /*
697 * req points to the first request where HWO changed
698 * from 0 to 1
699 */
700 req = dwc3_prepare_trbs(dep, start_new);
701 }
702 if (!req) {
703 dep->flags |= DWC3_EP_PENDING_REQUEST;
704 return 0;
705 }
706
707 memset(&params, 0, sizeof(params));
708 params.param0.depstrtxfer.transfer_desc_addr_high =
709 upper_32_bits(req->trb_dma);
710 params.param1.depstrtxfer.transfer_desc_addr_low =
711 lower_32_bits(req->trb_dma);
712
713 if (start_new)
714 cmd = DWC3_DEPCMD_STARTTRANSFER;
715 else
716 cmd = DWC3_DEPCMD_UPDATETRANSFER;
717
718 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
719 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
720 if (ret < 0) {
721 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
722
723 /*
724 * FIXME we need to iterate over the list of requests
725 * here and stop, unmap, free and del each of the linked
726 * requests instead of we do now.
727 */
728 dwc3_unmap_buffer_from_dma(req);
729 list_del(&req->list);
730 return ret;
731 }
732
733 dep->flags |= DWC3_EP_BUSY;
734 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
735 dep->number);
736 if (!dep->res_trans_idx)
737 printk_once(KERN_ERR "%s() res_trans_idx is invalid\n", __func__);
738 return 0;
739}
740
741static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
742{
743 req->request.actual = 0;
744 req->request.status = -EINPROGRESS;
745 req->direction = dep->direction;
746 req->epnum = dep->number;
747
748 /*
749 * We only add to our list of requests now and
750 * start consuming the list once we get XferNotReady
751 * IRQ.
752 *
753 * That way, we avoid doing anything that we don't need
754 * to do now and defer it until the point we receive a
755 * particular token from the Host side.
756 *
757 * This will also avoid Host cancelling URBs due to too
758 * many NACKs.
759 */
760 dwc3_map_buffer_to_dma(req);
761 list_add_tail(&req->list, &dep->request_list);
762
763 /*
764 * There is one special case: XferNotReady with
765 * empty list of requests. We need to kick the
766 * transfer here in that situation, otherwise
767 * we will be NAKing forever.
768 *
769 * If we get XferNotReady before gadget driver
770 * has a chance to queue a request, we will ACK
771 * the IRQ but won't be able to receive the data
772 * until the next request is queued. The following
773 * code is handling exactly that.
774 */
775 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
776 int ret;
777 int start_trans;
778
779 start_trans = 1;
780 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
781 dep->flags & DWC3_EP_BUSY)
782 start_trans = 0;
783
784 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
785 if (ret && ret != -EBUSY) {
786 struct dwc3 *dwc = dep->dwc;
787
788 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
789 dep->name);
790 }
791 };
792
793 return 0;
794}
795
796static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
797 gfp_t gfp_flags)
798{
799 struct dwc3_request *req = to_dwc3_request(request);
800 struct dwc3_ep *dep = to_dwc3_ep(ep);
801 struct dwc3 *dwc = dep->dwc;
802
803 unsigned long flags;
804
805 int ret;
806
807 if (!dep->desc) {
808 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
809 request, ep->name);
810 return -ESHUTDOWN;
811 }
812
813 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
814 request, ep->name, request->length);
815
816 spin_lock_irqsave(&dwc->lock, flags);
817 ret = __dwc3_gadget_ep_queue(dep, req);
818 spin_unlock_irqrestore(&dwc->lock, flags);
819
820 return ret;
821}
822
823static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
824 struct usb_request *request)
825{
826 struct dwc3_request *req = to_dwc3_request(request);
827 struct dwc3_request *r = NULL;
828
829 struct dwc3_ep *dep = to_dwc3_ep(ep);
830 struct dwc3 *dwc = dep->dwc;
831
832 unsigned long flags;
833 int ret = 0;
834
835 spin_lock_irqsave(&dwc->lock, flags);
836
837 list_for_each_entry(r, &dep->request_list, list) {
838 if (r == req)
839 break;
840 }
841
842 if (r != req) {
843 list_for_each_entry(r, &dep->req_queued, list) {
844 if (r == req)
845 break;
846 }
847 if (r == req) {
848 /* wait until it is processed */
849 dwc3_stop_active_transfer(dwc, dep->number);
850 goto out0;
851 }
852 dev_err(dwc->dev, "request %p was not queued to %s\n",
853 request, ep->name);
854 ret = -EINVAL;
855 goto out0;
856 }
857
858 /* giveback the request */
859 dwc3_gadget_giveback(dep, req, -ECONNRESET);
860
861out0:
862 spin_unlock_irqrestore(&dwc->lock, flags);
863
864 return ret;
865}
866
867int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
868{
869 struct dwc3_gadget_ep_cmd_params params;
870 struct dwc3 *dwc = dep->dwc;
871 int ret;
872
873 memset(&params, 0x00, sizeof(params));
874
875 if (value) {
Felipe Balbiaa7b4d02011-08-30 15:48:08 +0300876 if (dep->number == 0 || dep->number == 1) {
877 /*
878 * Whenever EP0 is stalled, we will restart
879 * the state machine, thus moving back to
880 * Setup Phase
881 */
882 dwc->ep0state = EP0_SETUP_PHASE;
883 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +0300884
885 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
886 DWC3_DEPCMD_SETSTALL, &params);
887 if (ret)
888 dev_err(dwc->dev, "failed to %s STALL on %s\n",
889 value ? "set" : "clear",
890 dep->name);
891 else
892 dep->flags |= DWC3_EP_STALL;
893 } else {
894 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
895 DWC3_DEPCMD_CLEARSTALL, &params);
896 if (ret)
897 dev_err(dwc->dev, "failed to %s STALL on %s\n",
898 value ? "set" : "clear",
899 dep->name);
900 else
901 dep->flags &= ~DWC3_EP_STALL;
902 }
903 return ret;
904}
905
906static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
907{
908 struct dwc3_ep *dep = to_dwc3_ep(ep);
909 struct dwc3 *dwc = dep->dwc;
910
911 unsigned long flags;
912
913 int ret;
914
915 spin_lock_irqsave(&dwc->lock, flags);
916
917 if (usb_endpoint_xfer_isoc(dep->desc)) {
918 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
919 ret = -EINVAL;
920 goto out;
921 }
922
923 ret = __dwc3_gadget_ep_set_halt(dep, value);
924out:
925 spin_unlock_irqrestore(&dwc->lock, flags);
926
927 return ret;
928}
929
930static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
931{
932 struct dwc3_ep *dep = to_dwc3_ep(ep);
933
934 dep->flags |= DWC3_EP_WEDGE;
935
936 return usb_ep_set_halt(ep);
937}
938
939/* -------------------------------------------------------------------------- */
940
941static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
942 .bLength = USB_DT_ENDPOINT_SIZE,
943 .bDescriptorType = USB_DT_ENDPOINT,
944 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
945};
946
947static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
948 .enable = dwc3_gadget_ep0_enable,
949 .disable = dwc3_gadget_ep0_disable,
950 .alloc_request = dwc3_gadget_ep_alloc_request,
951 .free_request = dwc3_gadget_ep_free_request,
952 .queue = dwc3_gadget_ep0_queue,
953 .dequeue = dwc3_gadget_ep_dequeue,
954 .set_halt = dwc3_gadget_ep_set_halt,
955 .set_wedge = dwc3_gadget_ep_set_wedge,
956};
957
958static const struct usb_ep_ops dwc3_gadget_ep_ops = {
959 .enable = dwc3_gadget_ep_enable,
960 .disable = dwc3_gadget_ep_disable,
961 .alloc_request = dwc3_gadget_ep_alloc_request,
962 .free_request = dwc3_gadget_ep_free_request,
963 .queue = dwc3_gadget_ep_queue,
964 .dequeue = dwc3_gadget_ep_dequeue,
965 .set_halt = dwc3_gadget_ep_set_halt,
966 .set_wedge = dwc3_gadget_ep_set_wedge,
967};
968
969/* -------------------------------------------------------------------------- */
970
971static int dwc3_gadget_get_frame(struct usb_gadget *g)
972{
973 struct dwc3 *dwc = gadget_to_dwc(g);
974 u32 reg;
975
976 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
977 return DWC3_DSTS_SOFFN(reg);
978}
979
980static int dwc3_gadget_wakeup(struct usb_gadget *g)
981{
982 struct dwc3 *dwc = gadget_to_dwc(g);
983
984 unsigned long timeout;
985 unsigned long flags;
986
987 u32 reg;
988
989 int ret = 0;
990
991 u8 link_state;
992 u8 speed;
993
994 spin_lock_irqsave(&dwc->lock, flags);
995
996 /*
997 * According to the Databook Remote wakeup request should
998 * be issued only when the device is in early suspend state.
999 *
1000 * We can check that via USB Link State bits in DSTS register.
1001 */
1002 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1003
1004 speed = reg & DWC3_DSTS_CONNECTSPD;
1005 if (speed == DWC3_DSTS_SUPERSPEED) {
1006 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1007 ret = -EINVAL;
1008 goto out;
1009 }
1010
1011 link_state = DWC3_DSTS_USBLNKST(reg);
1012
1013 switch (link_state) {
1014 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1015 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1016 break;
1017 default:
1018 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1019 link_state);
1020 ret = -EINVAL;
1021 goto out;
1022 }
1023
1024 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1025
1026 /*
1027 * Switch link state to Recovery. In HS/FS/LS this means
1028 * RemoteWakeup Request
1029 */
1030 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
1031 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1032
1033 /* wait for at least 2000us */
1034 usleep_range(2000, 2500);
1035
1036 /* write zeroes to Link Change Request */
1037 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1038 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1039
1040 /* pool until Link State change to ON */
1041 timeout = jiffies + msecs_to_jiffies(100);
1042
1043 while (!(time_after(jiffies, timeout))) {
1044 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1045
1046 /* in HS, means ON */
1047 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1048 break;
1049 }
1050
1051 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1052 dev_err(dwc->dev, "failed to send remote wakeup\n");
1053 ret = -EINVAL;
1054 }
1055
1056out:
1057 spin_unlock_irqrestore(&dwc->lock, flags);
1058
1059 return ret;
1060}
1061
1062static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1063 int is_selfpowered)
1064{
1065 struct dwc3 *dwc = gadget_to_dwc(g);
1066
1067 dwc->is_selfpowered = !!is_selfpowered;
1068
1069 return 0;
1070}
1071
1072static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1073{
1074 u32 reg;
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +02001075 u32 timeout = 500;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001076
1077 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1078 if (is_on)
1079 reg |= DWC3_DCTL_RUN_STOP;
1080 else
1081 reg &= ~DWC3_DCTL_RUN_STOP;
1082
1083 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1084
1085 do {
1086 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1087 if (is_on) {
1088 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1089 break;
1090 } else {
1091 if (reg & DWC3_DSTS_DEVCTRLHLT)
1092 break;
1093 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001094 timeout--;
1095 if (!timeout)
1096 break;
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +02001097 udelay(1);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001098 } while (1);
1099
1100 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1101 dwc->gadget_driver
1102 ? dwc->gadget_driver->function : "no-function",
1103 is_on ? "connect" : "disconnect");
1104}
1105
1106static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1107{
1108 struct dwc3 *dwc = gadget_to_dwc(g);
1109 unsigned long flags;
1110
1111 is_on = !!is_on;
1112
1113 spin_lock_irqsave(&dwc->lock, flags);
1114 dwc3_gadget_run_stop(dwc, is_on);
1115 spin_unlock_irqrestore(&dwc->lock, flags);
1116
1117 return 0;
1118}
1119
1120static int dwc3_gadget_start(struct usb_gadget *g,
1121 struct usb_gadget_driver *driver)
1122{
1123 struct dwc3 *dwc = gadget_to_dwc(g);
1124 struct dwc3_ep *dep;
1125 unsigned long flags;
1126 int ret = 0;
1127 u32 reg;
1128
1129 spin_lock_irqsave(&dwc->lock, flags);
1130
1131 if (dwc->gadget_driver) {
1132 dev_err(dwc->dev, "%s is already bound to %s\n",
1133 dwc->gadget.name,
1134 dwc->gadget_driver->driver.name);
1135 ret = -EBUSY;
1136 goto err0;
1137 }
1138
1139 dwc->gadget_driver = driver;
1140 dwc->gadget.dev.driver = &driver->driver;
1141
1142 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1143
1144 /*
1145 * REVISIT: power down scale might be different
1146 * depending on PHY used, need to pass that via platform_data
1147 */
1148 reg |= DWC3_GCTL_PWRDNSCALE(0x61a)
1149 | DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
1150 reg &= ~DWC3_GCTL_DISSCRAMBLE;
1151
1152 /*
1153 * WORKAROUND: DWC3 revisions <1.90a have a bug
1154 * when The device fails to connect at SuperSpeed
1155 * and falls back to high-speed mode which causes
1156 * the device to enter in a Connect/Disconnect loop
1157 */
1158 if (dwc->revision < DWC3_REVISION_190A)
1159 reg |= DWC3_GCTL_U2RSTECN;
1160
1161 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1162
1163 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1164 reg &= ~(DWC3_DCFG_SPEED_MASK);
1165 reg |= DWC3_DCFG_SUPERSPEED;
1166 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1167
1168 /* Start with SuperSpeed Default */
1169 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1170
1171 dep = dwc->eps[0];
1172 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1173 if (ret) {
1174 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1175 goto err0;
1176 }
1177
1178 dep = dwc->eps[1];
1179 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1180 if (ret) {
1181 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1182 goto err1;
1183 }
1184
1185 /* begin to receive SETUP packets */
Felipe Balbi32e132e2011-08-27 22:28:36 +03001186 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001187 dwc3_ep0_out_start(dwc);
1188
1189 spin_unlock_irqrestore(&dwc->lock, flags);
1190
1191 return 0;
1192
1193err1:
1194 __dwc3_gadget_ep_disable(dwc->eps[0]);
1195
1196err0:
1197 spin_unlock_irqrestore(&dwc->lock, flags);
1198
1199 return ret;
1200}
1201
1202static int dwc3_gadget_stop(struct usb_gadget *g,
1203 struct usb_gadget_driver *driver)
1204{
1205 struct dwc3 *dwc = gadget_to_dwc(g);
1206 unsigned long flags;
1207
1208 spin_lock_irqsave(&dwc->lock, flags);
1209
1210 __dwc3_gadget_ep_disable(dwc->eps[0]);
1211 __dwc3_gadget_ep_disable(dwc->eps[1]);
1212
1213 dwc->gadget_driver = NULL;
1214 dwc->gadget.dev.driver = NULL;
1215
1216 spin_unlock_irqrestore(&dwc->lock, flags);
1217
1218 return 0;
1219}
1220static const struct usb_gadget_ops dwc3_gadget_ops = {
1221 .get_frame = dwc3_gadget_get_frame,
1222 .wakeup = dwc3_gadget_wakeup,
1223 .set_selfpowered = dwc3_gadget_set_selfpowered,
1224 .pullup = dwc3_gadget_pullup,
1225 .udc_start = dwc3_gadget_start,
1226 .udc_stop = dwc3_gadget_stop,
1227};
1228
1229/* -------------------------------------------------------------------------- */
1230
1231static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1232{
1233 struct dwc3_ep *dep;
1234 u8 epnum;
1235
1236 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1237
1238 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1239 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1240 if (!dep) {
1241 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1242 epnum);
1243 return -ENOMEM;
1244 }
1245
1246 dep->dwc = dwc;
1247 dep->number = epnum;
1248 dwc->eps[epnum] = dep;
1249
1250 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1251 (epnum & 1) ? "in" : "out");
1252 dep->endpoint.name = dep->name;
1253 dep->direction = (epnum & 1);
1254
1255 if (epnum == 0 || epnum == 1) {
1256 dep->endpoint.maxpacket = 512;
1257 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1258 if (!epnum)
1259 dwc->gadget.ep0 = &dep->endpoint;
1260 } else {
1261 int ret;
1262
1263 dep->endpoint.maxpacket = 1024;
1264 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1265 list_add_tail(&dep->endpoint.ep_list,
1266 &dwc->gadget.ep_list);
1267
1268 ret = dwc3_alloc_trb_pool(dep);
1269 if (ret) {
1270 dev_err(dwc->dev, "%s: failed to allocate TRB pool\n", dep->name);
1271 return ret;
1272 }
1273 }
1274 INIT_LIST_HEAD(&dep->request_list);
1275 INIT_LIST_HEAD(&dep->req_queued);
1276 }
1277
1278 return 0;
1279}
1280
1281static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1282{
1283 struct dwc3_ep *dep;
1284 u8 epnum;
1285
1286 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1287 dep = dwc->eps[epnum];
1288 dwc3_free_trb_pool(dep);
1289
1290 if (epnum != 0 && epnum != 1)
1291 list_del(&dep->endpoint.ep_list);
1292
1293 kfree(dep);
1294 }
1295}
1296
1297static void dwc3_gadget_release(struct device *dev)
1298{
1299 dev_dbg(dev, "%s\n", __func__);
1300}
1301
1302/* -------------------------------------------------------------------------- */
1303static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1304 const struct dwc3_event_depevt *event, int status)
1305{
1306 struct dwc3_request *req;
1307 struct dwc3_trb trb;
1308 unsigned int count;
1309 unsigned int s_pkt = 0;
1310
1311 do {
1312 req = next_request(&dep->req_queued);
1313 if (!req)
1314 break;
1315
1316 dwc3_trb_to_nat(req->trb, &trb);
1317
Sebastian Andrzej Siewior679dc462011-08-19 19:59:12 +02001318 if (trb.hwo && status != -ESHUTDOWN)
1319 /*
1320 * We continue despite the error. There is not much we
1321 * can do. If we don't clean in up we loop for ever. If
1322 * we skip the TRB than it gets overwritten reused after
1323 * a while since we use them in a ring buffer. a BUG()
1324 * would help. Lets hope that if this occures, someone
1325 * fixes the root cause instead of looking away :)
1326 */
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001327 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1328 dep->name, req->trb);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001329 count = trb.length;
1330
1331 if (dep->direction) {
1332 if (count) {
1333 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1334 dep->name);
1335 status = -ECONNRESET;
1336 }
1337 } else {
1338 if (count && (event->status & DEPEVT_STATUS_SHORT))
1339 s_pkt = 1;
1340 }
1341
1342 /*
1343 * We assume here we will always receive the entire data block
1344 * which we should receive. Meaning, if we program RX to
1345 * receive 4K but we receive only 2K, we assume that's all we
1346 * should receive and we simply bounce the request back to the
1347 * gadget driver for further processing.
1348 */
1349 req->request.actual += req->request.length - count;
1350 dwc3_gadget_giveback(dep, req, status);
1351 if (s_pkt)
1352 break;
1353 if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
1354 break;
1355 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1356 break;
1357 } while (1);
1358
1359 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1360 return 0;
1361 return 1;
1362}
1363
1364static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1365 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1366 int start_new)
1367{
1368 unsigned status = 0;
1369 int clean_busy;
1370
1371 if (event->status & DEPEVT_STATUS_BUSERR)
1372 status = -ECONNRESET;
1373
1374 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001375 if (clean_busy) {
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001376 dep->flags &= ~DWC3_EP_BUSY;
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001377 dep->res_trans_idx = 0;
1378 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001379}
1380
1381static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1382 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1383{
1384 u32 uf;
1385
1386 if (list_empty(&dep->request_list)) {
1387 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1388 dep->name);
1389 return;
1390 }
1391
1392 if (event->parameters) {
1393 u32 mask;
1394
1395 mask = ~(dep->interval - 1);
1396 uf = event->parameters & mask;
1397 /* 4 micro frames in the future */
1398 uf += dep->interval * 4;
1399 } else {
1400 uf = 0;
1401 }
1402
1403 __dwc3_gadget_kick_transfer(dep, uf, 1);
1404}
1405
1406static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1407 const struct dwc3_event_depevt *event)
1408{
1409 struct dwc3 *dwc = dep->dwc;
1410 struct dwc3_event_depevt mod_ev = *event;
1411
1412 /*
1413 * We were asked to remove one requests. It is possible that this
1414 * request and a few other were started together and have the same
1415 * transfer index. Since we stopped the complete endpoint we don't
1416 * know how many requests were already completed (and not yet)
1417 * reported and how could be done (later). We purge them all until
1418 * the end of the list.
1419 */
1420 mod_ev.status = DEPEVT_STATUS_LST;
1421 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1422 dep->flags &= ~DWC3_EP_BUSY;
1423 /* pending requets are ignored and are queued on XferNotReady */
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001424}
1425
1426static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1427 const struct dwc3_event_depevt *event)
1428{
1429 u32 param = event->parameters;
1430 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1431
1432 switch (cmd_type) {
1433 case DWC3_DEPCMD_ENDTRANSFER:
1434 dwc3_process_ep_cmd_complete(dep, event);
1435 break;
1436 case DWC3_DEPCMD_STARTTRANSFER:
1437 dep->res_trans_idx = param & 0x7f;
1438 break;
1439 default:
1440 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1441 __func__, cmd_type);
1442 break;
1443 };
1444}
1445
1446static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1447 const struct dwc3_event_depevt *event)
1448{
1449 struct dwc3_ep *dep;
1450 u8 epnum = event->endpoint_number;
1451
1452 dep = dwc->eps[epnum];
1453
1454 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1455 dwc3_ep_event_string(event->endpoint_event));
1456
1457 if (epnum == 0 || epnum == 1) {
1458 dwc3_ep0_interrupt(dwc, event);
1459 return;
1460 }
1461
1462 switch (event->endpoint_event) {
1463 case DWC3_DEPEVT_XFERCOMPLETE:
1464 if (usb_endpoint_xfer_isoc(dep->desc)) {
1465 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1466 dep->name);
1467 return;
1468 }
1469
1470 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1471 break;
1472 case DWC3_DEPEVT_XFERINPROGRESS:
1473 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1474 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1475 dep->name);
1476 return;
1477 }
1478
1479 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1480 break;
1481 case DWC3_DEPEVT_XFERNOTREADY:
1482 if (usb_endpoint_xfer_isoc(dep->desc)) {
1483 dwc3_gadget_start_isoc(dwc, dep, event);
1484 } else {
1485 int ret;
1486
1487 dev_vdbg(dwc->dev, "%s: reason %s\n",
1488 dep->name, event->status
1489 ? "Transfer Active"
1490 : "Transfer Not Active");
1491
1492 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1493 if (!ret || ret == -EBUSY)
1494 return;
1495
1496 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1497 dep->name);
1498 }
1499
1500 break;
1501 case DWC3_DEPEVT_RXTXFIFOEVT:
1502 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1503 break;
1504 case DWC3_DEPEVT_STREAMEVT:
1505 dev_dbg(dwc->dev, "%s Stream Event\n", dep->name);
1506 break;
1507 case DWC3_DEPEVT_EPCMDCMPLT:
1508 dwc3_ep_cmd_compl(dep, event);
1509 break;
1510 }
1511}
1512
1513static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1514{
1515 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1516 spin_unlock(&dwc->lock);
1517 dwc->gadget_driver->disconnect(&dwc->gadget);
1518 spin_lock(&dwc->lock);
1519 }
1520}
1521
1522static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1523{
1524 struct dwc3_ep *dep;
1525 struct dwc3_gadget_ep_cmd_params params;
1526 u32 cmd;
1527 int ret;
1528
1529 dep = dwc->eps[epnum];
1530
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +02001531 WARN_ON(!dep->res_trans_idx);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001532 if (dep->res_trans_idx) {
1533 cmd = DWC3_DEPCMD_ENDTRANSFER;
1534 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1535 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1536 memset(&params, 0, sizeof(params));
1537 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1538 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001539 dep->res_trans_idx = 0;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001540 }
1541}
1542
1543static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1544{
1545 u32 epnum;
1546
1547 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1548 struct dwc3_ep *dep;
1549
1550 dep = dwc->eps[epnum];
1551 if (!(dep->flags & DWC3_EP_ENABLED))
1552 continue;
1553
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +02001554 dwc3_remove_requests(dwc, dep);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001555 }
1556}
1557
1558static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1559{
1560 u32 epnum;
1561
1562 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1563 struct dwc3_ep *dep;
1564 struct dwc3_gadget_ep_cmd_params params;
1565 int ret;
1566
1567 dep = dwc->eps[epnum];
1568
1569 if (!(dep->flags & DWC3_EP_STALL))
1570 continue;
1571
1572 dep->flags &= ~DWC3_EP_STALL;
1573
1574 memset(&params, 0, sizeof(params));
1575 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1576 DWC3_DEPCMD_CLEARSTALL, &params);
1577 WARN_ON_ONCE(ret);
1578 }
1579}
1580
1581static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1582{
1583 dev_vdbg(dwc->dev, "%s\n", __func__);
1584#if 0
1585 XXX
1586 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1587 enable it before we can disable it.
1588
1589 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1590 reg &= ~DWC3_DCTL_INITU1ENA;
1591 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1592
1593 reg &= ~DWC3_DCTL_INITU2ENA;
1594 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1595#endif
1596
1597 dwc3_stop_active_transfers(dwc);
1598 dwc3_disconnect_gadget(dwc);
1599
1600 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1601}
1602
1603static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1604{
1605 u32 reg;
1606
1607 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1608
1609 if (on)
1610 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1611 else
1612 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1613
1614 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1615}
1616
1617static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1618{
1619 u32 reg;
1620
1621 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1622
1623 if (on)
1624 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1625 else
1626 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1627
1628 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1629}
1630
1631static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1632{
1633 u32 reg;
1634
1635 dev_vdbg(dwc->dev, "%s\n", __func__);
1636
1637 /* Enable PHYs */
1638 dwc3_gadget_usb2_phy_power(dwc, true);
1639 dwc3_gadget_usb3_phy_power(dwc, true);
1640
1641 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1642 dwc3_disconnect_gadget(dwc);
1643
1644 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1645 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1646 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1647
1648 dwc3_stop_active_transfers(dwc);
1649 dwc3_clear_stall_all_ep(dwc);
1650
1651 /* Reset device address to zero */
1652 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1653 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1654 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1655
1656 /*
1657 * Wait for RxFifo to drain
1658 *
1659 * REVISIT probably shouldn't wait forever.
1660 * In case Hardware ends up in a screwed up
1661 * case, we error out, notify the user and,
1662 * maybe, WARN() or BUG() but leave the rest
1663 * of the kernel working fine.
1664 *
1665 * REVISIT the below is rather CPU intensive,
1666 * maybe we should read and if it doesn't work
1667 * sleep (not busy wait) for a few useconds.
1668 *
1669 * REVISIT why wait until the RXFIFO is empty anyway?
1670 */
1671 while (!(dwc3_readl(dwc->regs, DWC3_DSTS)
1672 & DWC3_DSTS_RXFIFOEMPTY))
1673 cpu_relax();
1674}
1675
1676static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1677{
1678 u32 reg;
1679 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1680
1681 /*
1682 * We change the clock only at SS but I dunno why I would want to do
1683 * this. Maybe it becomes part of the power saving plan.
1684 */
1685
1686 if (speed != DWC3_DSTS_SUPERSPEED)
1687 return;
1688
1689 /*
1690 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1691 * each time on Connect Done.
1692 */
1693 if (!usb30_clock)
1694 return;
1695
1696 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1697 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1698 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1699}
1700
1701static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1702{
1703 switch (speed) {
1704 case USB_SPEED_SUPER:
1705 dwc3_gadget_usb2_phy_power(dwc, false);
1706 break;
1707 case USB_SPEED_HIGH:
1708 case USB_SPEED_FULL:
1709 case USB_SPEED_LOW:
1710 dwc3_gadget_usb3_phy_power(dwc, false);
1711 break;
1712 }
1713}
1714
1715static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1716{
1717 struct dwc3_gadget_ep_cmd_params params;
1718 struct dwc3_ep *dep;
1719 int ret;
1720 u32 reg;
1721 u8 speed;
1722
1723 dev_vdbg(dwc->dev, "%s\n", __func__);
1724
1725 memset(&params, 0x00, sizeof(params));
1726
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001727 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1728 speed = reg & DWC3_DSTS_CONNECTSPD;
1729 dwc->speed = speed;
1730
1731 dwc3_update_ram_clk_sel(dwc, speed);
1732
1733 switch (speed) {
1734 case DWC3_DCFG_SUPERSPEED:
1735 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1736 dwc->gadget.ep0->maxpacket = 512;
1737 dwc->gadget.speed = USB_SPEED_SUPER;
1738 break;
1739 case DWC3_DCFG_HIGHSPEED:
1740 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1741 dwc->gadget.ep0->maxpacket = 64;
1742 dwc->gadget.speed = USB_SPEED_HIGH;
1743 break;
1744 case DWC3_DCFG_FULLSPEED2:
1745 case DWC3_DCFG_FULLSPEED1:
1746 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1747 dwc->gadget.ep0->maxpacket = 64;
1748 dwc->gadget.speed = USB_SPEED_FULL;
1749 break;
1750 case DWC3_DCFG_LOWSPEED:
1751 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
1752 dwc->gadget.ep0->maxpacket = 8;
1753 dwc->gadget.speed = USB_SPEED_LOW;
1754 break;
1755 }
1756
1757 /* Disable unneded PHY */
1758 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
1759
1760 dep = dwc->eps[0];
1761 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1762 if (ret) {
1763 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1764 return;
1765 }
1766
1767 dep = dwc->eps[1];
1768 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1769 if (ret) {
1770 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1771 return;
1772 }
1773
1774 /*
1775 * Configure PHY via GUSB3PIPECTLn if required.
1776 *
1777 * Update GTXFIFOSIZn
1778 *
1779 * In both cases reset values should be sufficient.
1780 */
1781}
1782
1783static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
1784{
1785 dev_vdbg(dwc->dev, "%s\n", __func__);
1786
1787 /*
1788 * TODO take core out of low power mode when that's
1789 * implemented.
1790 */
1791
1792 dwc->gadget_driver->resume(&dwc->gadget);
1793}
1794
1795static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
1796 unsigned int evtinfo)
1797{
1798 dev_vdbg(dwc->dev, "%s\n", __func__);
1799
1800 /* The fith bit says SuperSpeed yes or no. */
1801 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
1802}
1803
1804static void dwc3_gadget_interrupt(struct dwc3 *dwc,
1805 const struct dwc3_event_devt *event)
1806{
1807 switch (event->type) {
1808 case DWC3_DEVICE_EVENT_DISCONNECT:
1809 dwc3_gadget_disconnect_interrupt(dwc);
1810 break;
1811 case DWC3_DEVICE_EVENT_RESET:
1812 dwc3_gadget_reset_interrupt(dwc);
1813 break;
1814 case DWC3_DEVICE_EVENT_CONNECT_DONE:
1815 dwc3_gadget_conndone_interrupt(dwc);
1816 break;
1817 case DWC3_DEVICE_EVENT_WAKEUP:
1818 dwc3_gadget_wakeup_interrupt(dwc);
1819 break;
1820 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
1821 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
1822 break;
1823 case DWC3_DEVICE_EVENT_EOPF:
1824 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
1825 break;
1826 case DWC3_DEVICE_EVENT_SOF:
1827 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
1828 break;
1829 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
1830 dev_vdbg(dwc->dev, "Erratic Error\n");
1831 break;
1832 case DWC3_DEVICE_EVENT_CMD_CMPL:
1833 dev_vdbg(dwc->dev, "Command Complete\n");
1834 break;
1835 case DWC3_DEVICE_EVENT_OVERFLOW:
1836 dev_vdbg(dwc->dev, "Overflow\n");
1837 break;
1838 default:
1839 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
1840 }
1841}
1842
1843static void dwc3_process_event_entry(struct dwc3 *dwc,
1844 const union dwc3_event *event)
1845{
1846 /* Endpoint IRQ, handle it and return early */
1847 if (event->type.is_devspec == 0) {
1848 /* depevt */
1849 return dwc3_endpoint_interrupt(dwc, &event->depevt);
1850 }
1851
1852 switch (event->type.type) {
1853 case DWC3_EVENT_TYPE_DEV:
1854 dwc3_gadget_interrupt(dwc, &event->devt);
1855 break;
1856 /* REVISIT what to do with Carkit and I2C events ? */
1857 default:
1858 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
1859 }
1860}
1861
1862static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
1863{
1864 struct dwc3_event_buffer *evt;
1865 int left;
1866 u32 count;
1867
1868 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
1869 count &= DWC3_GEVNTCOUNT_MASK;
1870 if (!count)
1871 return IRQ_NONE;
1872
1873 evt = dwc->ev_buffs[buf];
1874 left = count;
1875
1876 while (left > 0) {
1877 union dwc3_event event;
1878
1879 memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
1880 dwc3_process_event_entry(dwc, &event);
1881 /*
1882 * XXX we wrap around correctly to the next entry as almost all
1883 * entries are 4 bytes in size. There is one entry which has 12
1884 * bytes which is a regular entry followed by 8 bytes data. ATM
1885 * I don't know how things are organized if were get next to the
1886 * a boundary so I worry about that once we try to handle that.
1887 */
1888 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
1889 left -= 4;
1890
1891 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
1892 }
1893
1894 return IRQ_HANDLED;
1895}
1896
1897static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
1898{
1899 struct dwc3 *dwc = _dwc;
1900 int i;
1901 irqreturn_t ret = IRQ_NONE;
1902
1903 spin_lock(&dwc->lock);
1904
1905 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
1906 irqreturn_t status;
1907
1908 status = dwc3_process_event_buf(dwc, i);
1909 if (status == IRQ_HANDLED)
1910 ret = status;
1911 }
1912
1913 spin_unlock(&dwc->lock);
1914
1915 return ret;
1916}
1917
1918/**
1919 * dwc3_gadget_init - Initializes gadget related registers
1920 * @dwc: Pointer to out controller context structure
1921 *
1922 * Returns 0 on success otherwise negative errno.
1923 */
1924int __devinit dwc3_gadget_init(struct dwc3 *dwc)
1925{
1926 u32 reg;
1927 int ret;
1928 int irq;
1929
1930 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
1931 &dwc->ctrl_req_addr, GFP_KERNEL);
1932 if (!dwc->ctrl_req) {
1933 dev_err(dwc->dev, "failed to allocate ctrl request\n");
1934 ret = -ENOMEM;
1935 goto err0;
1936 }
1937
1938 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
1939 &dwc->ep0_trb_addr, GFP_KERNEL);
1940 if (!dwc->ep0_trb) {
1941 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
1942 ret = -ENOMEM;
1943 goto err1;
1944 }
1945
1946 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
1947 sizeof(*dwc->setup_buf) * 2,
1948 &dwc->setup_buf_addr, GFP_KERNEL);
1949 if (!dwc->setup_buf) {
1950 dev_err(dwc->dev, "failed to allocate setup buffer\n");
1951 ret = -ENOMEM;
1952 goto err2;
1953 }
1954
Felipe Balbi64e96342011-08-27 22:07:53 +03001955 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
1956 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
1957 if (!dwc->ep0_bounce) {
1958 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
1959 ret = -ENOMEM;
1960 goto err3;
1961 }
1962
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001963 dev_set_name(&dwc->gadget.dev, "gadget");
1964
1965 dwc->gadget.ops = &dwc3_gadget_ops;
1966 dwc->gadget.is_dualspeed = true;
1967 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1968 dwc->gadget.dev.parent = dwc->dev;
1969
1970 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
1971
1972 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
1973 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
1974 dwc->gadget.dev.release = dwc3_gadget_release;
1975 dwc->gadget.name = "dwc3-gadget";
1976
1977 /*
1978 * REVISIT: Here we should clear all pending IRQs to be
1979 * sure we're starting from a well known location.
1980 */
1981
1982 ret = dwc3_gadget_init_endpoints(dwc);
1983 if (ret)
Felipe Balbi64e96342011-08-27 22:07:53 +03001984 goto err4;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001985
1986 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1987
1988 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
1989 "dwc3", dwc);
1990 if (ret) {
1991 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1992 irq, ret);
Felipe Balbi64e96342011-08-27 22:07:53 +03001993 goto err5;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001994 }
1995
1996 /* Enable all but Start and End of Frame IRQs */
1997 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1998 DWC3_DEVTEN_EVNTOVERFLOWEN |
1999 DWC3_DEVTEN_CMDCMPLTEN |
2000 DWC3_DEVTEN_ERRTICERREN |
2001 DWC3_DEVTEN_WKUPEVTEN |
2002 DWC3_DEVTEN_ULSTCNGEN |
2003 DWC3_DEVTEN_CONNECTDONEEN |
2004 DWC3_DEVTEN_USBRSTEN |
2005 DWC3_DEVTEN_DISCONNEVTEN);
2006 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2007
2008 ret = device_register(&dwc->gadget.dev);
2009 if (ret) {
2010 dev_err(dwc->dev, "failed to register gadget device\n");
2011 put_device(&dwc->gadget.dev);
Felipe Balbi64e96342011-08-27 22:07:53 +03002012 goto err6;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002013 }
2014
2015 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2016 if (ret) {
2017 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi64e96342011-08-27 22:07:53 +03002018 goto err7;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002019 }
2020
2021 return 0;
2022
Felipe Balbi64e96342011-08-27 22:07:53 +03002023err7:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002024 device_unregister(&dwc->gadget.dev);
2025
Felipe Balbi64e96342011-08-27 22:07:53 +03002026err6:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002027 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2028 free_irq(irq, dwc);
2029
Felipe Balbi64e96342011-08-27 22:07:53 +03002030err5:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002031 dwc3_gadget_free_endpoints(dwc);
2032
Felipe Balbi64e96342011-08-27 22:07:53 +03002033err4:
2034 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2035 dwc->ep0_bounce_addr);
2036
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002037err3:
2038 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2039 dwc->setup_buf, dwc->setup_buf_addr);
2040
2041err2:
2042 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2043 dwc->ep0_trb, dwc->ep0_trb_addr);
2044
2045err1:
2046 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2047 dwc->ctrl_req, dwc->ctrl_req_addr);
2048
2049err0:
2050 return ret;
2051}
2052
2053void dwc3_gadget_exit(struct dwc3 *dwc)
2054{
2055 int irq;
2056 int i;
2057
2058 usb_del_gadget_udc(&dwc->gadget);
2059 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2060
2061 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2062 free_irq(irq, dwc);
2063
2064 for (i = 0; i < ARRAY_SIZE(dwc->eps); i++)
2065 __dwc3_gadget_ep_disable(dwc->eps[i]);
2066
2067 dwc3_gadget_free_endpoints(dwc);
2068
Felipe Balbi64e96342011-08-27 22:07:53 +03002069 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2070 dwc->ep0_bounce_addr);
2071
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002072 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2073 dwc->setup_buf, dwc->setup_buf_addr);
2074
2075 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2076 dwc->ep0_trb, dwc->ep0_trb_addr);
2077
2078 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2079 dwc->ctrl_req, dwc->ctrl_req_addr);
2080
2081 device_unregister(&dwc->gadget.dev);
2082}