blob: e20daf4e01ba0c3444fdd8d86b7bf4448c7617ea [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) {
876 if (dep->number == 0 || dep->number == 1)
877 dwc->ep0state = EP0_STALL;
878
879 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
880 DWC3_DEPCMD_SETSTALL, &params);
881 if (ret)
882 dev_err(dwc->dev, "failed to %s STALL on %s\n",
883 value ? "set" : "clear",
884 dep->name);
885 else
886 dep->flags |= DWC3_EP_STALL;
887 } else {
888 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
889 DWC3_DEPCMD_CLEARSTALL, &params);
890 if (ret)
891 dev_err(dwc->dev, "failed to %s STALL on %s\n",
892 value ? "set" : "clear",
893 dep->name);
894 else
895 dep->flags &= ~DWC3_EP_STALL;
896 }
897 return ret;
898}
899
900static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
901{
902 struct dwc3_ep *dep = to_dwc3_ep(ep);
903 struct dwc3 *dwc = dep->dwc;
904
905 unsigned long flags;
906
907 int ret;
908
909 spin_lock_irqsave(&dwc->lock, flags);
910
911 if (usb_endpoint_xfer_isoc(dep->desc)) {
912 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
913 ret = -EINVAL;
914 goto out;
915 }
916
917 ret = __dwc3_gadget_ep_set_halt(dep, value);
918out:
919 spin_unlock_irqrestore(&dwc->lock, flags);
920
921 return ret;
922}
923
924static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
925{
926 struct dwc3_ep *dep = to_dwc3_ep(ep);
927
928 dep->flags |= DWC3_EP_WEDGE;
929
930 return usb_ep_set_halt(ep);
931}
932
933/* -------------------------------------------------------------------------- */
934
935static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
936 .bLength = USB_DT_ENDPOINT_SIZE,
937 .bDescriptorType = USB_DT_ENDPOINT,
938 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
939};
940
941static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
942 .enable = dwc3_gadget_ep0_enable,
943 .disable = dwc3_gadget_ep0_disable,
944 .alloc_request = dwc3_gadget_ep_alloc_request,
945 .free_request = dwc3_gadget_ep_free_request,
946 .queue = dwc3_gadget_ep0_queue,
947 .dequeue = dwc3_gadget_ep_dequeue,
948 .set_halt = dwc3_gadget_ep_set_halt,
949 .set_wedge = dwc3_gadget_ep_set_wedge,
950};
951
952static const struct usb_ep_ops dwc3_gadget_ep_ops = {
953 .enable = dwc3_gadget_ep_enable,
954 .disable = dwc3_gadget_ep_disable,
955 .alloc_request = dwc3_gadget_ep_alloc_request,
956 .free_request = dwc3_gadget_ep_free_request,
957 .queue = dwc3_gadget_ep_queue,
958 .dequeue = dwc3_gadget_ep_dequeue,
959 .set_halt = dwc3_gadget_ep_set_halt,
960 .set_wedge = dwc3_gadget_ep_set_wedge,
961};
962
963/* -------------------------------------------------------------------------- */
964
965static int dwc3_gadget_get_frame(struct usb_gadget *g)
966{
967 struct dwc3 *dwc = gadget_to_dwc(g);
968 u32 reg;
969
970 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
971 return DWC3_DSTS_SOFFN(reg);
972}
973
974static int dwc3_gadget_wakeup(struct usb_gadget *g)
975{
976 struct dwc3 *dwc = gadget_to_dwc(g);
977
978 unsigned long timeout;
979 unsigned long flags;
980
981 u32 reg;
982
983 int ret = 0;
984
985 u8 link_state;
986 u8 speed;
987
988 spin_lock_irqsave(&dwc->lock, flags);
989
990 /*
991 * According to the Databook Remote wakeup request should
992 * be issued only when the device is in early suspend state.
993 *
994 * We can check that via USB Link State bits in DSTS register.
995 */
996 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
997
998 speed = reg & DWC3_DSTS_CONNECTSPD;
999 if (speed == DWC3_DSTS_SUPERSPEED) {
1000 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1001 ret = -EINVAL;
1002 goto out;
1003 }
1004
1005 link_state = DWC3_DSTS_USBLNKST(reg);
1006
1007 switch (link_state) {
1008 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1009 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1010 break;
1011 default:
1012 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1013 link_state);
1014 ret = -EINVAL;
1015 goto out;
1016 }
1017
1018 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1019
1020 /*
1021 * Switch link state to Recovery. In HS/FS/LS this means
1022 * RemoteWakeup Request
1023 */
1024 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
1025 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1026
1027 /* wait for at least 2000us */
1028 usleep_range(2000, 2500);
1029
1030 /* write zeroes to Link Change Request */
1031 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1032 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1033
1034 /* pool until Link State change to ON */
1035 timeout = jiffies + msecs_to_jiffies(100);
1036
1037 while (!(time_after(jiffies, timeout))) {
1038 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1039
1040 /* in HS, means ON */
1041 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1042 break;
1043 }
1044
1045 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1046 dev_err(dwc->dev, "failed to send remote wakeup\n");
1047 ret = -EINVAL;
1048 }
1049
1050out:
1051 spin_unlock_irqrestore(&dwc->lock, flags);
1052
1053 return ret;
1054}
1055
1056static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1057 int is_selfpowered)
1058{
1059 struct dwc3 *dwc = gadget_to_dwc(g);
1060
1061 dwc->is_selfpowered = !!is_selfpowered;
1062
1063 return 0;
1064}
1065
1066static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1067{
1068 u32 reg;
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +02001069 u32 timeout = 500;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001070
1071 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1072 if (is_on)
1073 reg |= DWC3_DCTL_RUN_STOP;
1074 else
1075 reg &= ~DWC3_DCTL_RUN_STOP;
1076
1077 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1078
1079 do {
1080 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1081 if (is_on) {
1082 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1083 break;
1084 } else {
1085 if (reg & DWC3_DSTS_DEVCTRLHLT)
1086 break;
1087 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001088 timeout--;
1089 if (!timeout)
1090 break;
Sebastian Andrzej Siewior6062cac2011-08-29 16:46:38 +02001091 udelay(1);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001092 } while (1);
1093
1094 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1095 dwc->gadget_driver
1096 ? dwc->gadget_driver->function : "no-function",
1097 is_on ? "connect" : "disconnect");
1098}
1099
1100static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1101{
1102 struct dwc3 *dwc = gadget_to_dwc(g);
1103 unsigned long flags;
1104
1105 is_on = !!is_on;
1106
1107 spin_lock_irqsave(&dwc->lock, flags);
1108 dwc3_gadget_run_stop(dwc, is_on);
1109 spin_unlock_irqrestore(&dwc->lock, flags);
1110
1111 return 0;
1112}
1113
1114static int dwc3_gadget_start(struct usb_gadget *g,
1115 struct usb_gadget_driver *driver)
1116{
1117 struct dwc3 *dwc = gadget_to_dwc(g);
1118 struct dwc3_ep *dep;
1119 unsigned long flags;
1120 int ret = 0;
1121 u32 reg;
1122
1123 spin_lock_irqsave(&dwc->lock, flags);
1124
1125 if (dwc->gadget_driver) {
1126 dev_err(dwc->dev, "%s is already bound to %s\n",
1127 dwc->gadget.name,
1128 dwc->gadget_driver->driver.name);
1129 ret = -EBUSY;
1130 goto err0;
1131 }
1132
1133 dwc->gadget_driver = driver;
1134 dwc->gadget.dev.driver = &driver->driver;
1135
1136 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1137
1138 /*
1139 * REVISIT: power down scale might be different
1140 * depending on PHY used, need to pass that via platform_data
1141 */
1142 reg |= DWC3_GCTL_PWRDNSCALE(0x61a)
1143 | DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
1144 reg &= ~DWC3_GCTL_DISSCRAMBLE;
1145
1146 /*
1147 * WORKAROUND: DWC3 revisions <1.90a have a bug
1148 * when The device fails to connect at SuperSpeed
1149 * and falls back to high-speed mode which causes
1150 * the device to enter in a Connect/Disconnect loop
1151 */
1152 if (dwc->revision < DWC3_REVISION_190A)
1153 reg |= DWC3_GCTL_U2RSTECN;
1154
1155 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1156
1157 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1158 reg &= ~(DWC3_DCFG_SPEED_MASK);
1159 reg |= DWC3_DCFG_SUPERSPEED;
1160 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1161
1162 /* Start with SuperSpeed Default */
1163 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1164
1165 dep = dwc->eps[0];
1166 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1167 if (ret) {
1168 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1169 goto err0;
1170 }
1171
1172 dep = dwc->eps[1];
1173 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1174 if (ret) {
1175 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1176 goto err1;
1177 }
1178
1179 /* begin to receive SETUP packets */
Felipe Balbi32e132e2011-08-27 22:28:36 +03001180 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001181 dwc3_ep0_out_start(dwc);
1182
1183 spin_unlock_irqrestore(&dwc->lock, flags);
1184
1185 return 0;
1186
1187err1:
1188 __dwc3_gadget_ep_disable(dwc->eps[0]);
1189
1190err0:
1191 spin_unlock_irqrestore(&dwc->lock, flags);
1192
1193 return ret;
1194}
1195
1196static int dwc3_gadget_stop(struct usb_gadget *g,
1197 struct usb_gadget_driver *driver)
1198{
1199 struct dwc3 *dwc = gadget_to_dwc(g);
1200 unsigned long flags;
1201
1202 spin_lock_irqsave(&dwc->lock, flags);
1203
1204 __dwc3_gadget_ep_disable(dwc->eps[0]);
1205 __dwc3_gadget_ep_disable(dwc->eps[1]);
1206
1207 dwc->gadget_driver = NULL;
1208 dwc->gadget.dev.driver = NULL;
1209
1210 spin_unlock_irqrestore(&dwc->lock, flags);
1211
1212 return 0;
1213}
1214static const struct usb_gadget_ops dwc3_gadget_ops = {
1215 .get_frame = dwc3_gadget_get_frame,
1216 .wakeup = dwc3_gadget_wakeup,
1217 .set_selfpowered = dwc3_gadget_set_selfpowered,
1218 .pullup = dwc3_gadget_pullup,
1219 .udc_start = dwc3_gadget_start,
1220 .udc_stop = dwc3_gadget_stop,
1221};
1222
1223/* -------------------------------------------------------------------------- */
1224
1225static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1226{
1227 struct dwc3_ep *dep;
1228 u8 epnum;
1229
1230 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1231
1232 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1233 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1234 if (!dep) {
1235 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1236 epnum);
1237 return -ENOMEM;
1238 }
1239
1240 dep->dwc = dwc;
1241 dep->number = epnum;
1242 dwc->eps[epnum] = dep;
1243
1244 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1245 (epnum & 1) ? "in" : "out");
1246 dep->endpoint.name = dep->name;
1247 dep->direction = (epnum & 1);
1248
1249 if (epnum == 0 || epnum == 1) {
1250 dep->endpoint.maxpacket = 512;
1251 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1252 if (!epnum)
1253 dwc->gadget.ep0 = &dep->endpoint;
1254 } else {
1255 int ret;
1256
1257 dep->endpoint.maxpacket = 1024;
1258 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1259 list_add_tail(&dep->endpoint.ep_list,
1260 &dwc->gadget.ep_list);
1261
1262 ret = dwc3_alloc_trb_pool(dep);
1263 if (ret) {
1264 dev_err(dwc->dev, "%s: failed to allocate TRB pool\n", dep->name);
1265 return ret;
1266 }
1267 }
1268 INIT_LIST_HEAD(&dep->request_list);
1269 INIT_LIST_HEAD(&dep->req_queued);
1270 }
1271
1272 return 0;
1273}
1274
1275static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1276{
1277 struct dwc3_ep *dep;
1278 u8 epnum;
1279
1280 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1281 dep = dwc->eps[epnum];
1282 dwc3_free_trb_pool(dep);
1283
1284 if (epnum != 0 && epnum != 1)
1285 list_del(&dep->endpoint.ep_list);
1286
1287 kfree(dep);
1288 }
1289}
1290
1291static void dwc3_gadget_release(struct device *dev)
1292{
1293 dev_dbg(dev, "%s\n", __func__);
1294}
1295
1296/* -------------------------------------------------------------------------- */
1297static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1298 const struct dwc3_event_depevt *event, int status)
1299{
1300 struct dwc3_request *req;
1301 struct dwc3_trb trb;
1302 unsigned int count;
1303 unsigned int s_pkt = 0;
1304
1305 do {
1306 req = next_request(&dep->req_queued);
1307 if (!req)
1308 break;
1309
1310 dwc3_trb_to_nat(req->trb, &trb);
1311
Sebastian Andrzej Siewior679dc462011-08-19 19:59:12 +02001312 if (trb.hwo && status != -ESHUTDOWN)
1313 /*
1314 * We continue despite the error. There is not much we
1315 * can do. If we don't clean in up we loop for ever. If
1316 * we skip the TRB than it gets overwritten reused after
1317 * a while since we use them in a ring buffer. a BUG()
1318 * would help. Lets hope that if this occures, someone
1319 * fixes the root cause instead of looking away :)
1320 */
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001321 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1322 dep->name, req->trb);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001323 count = trb.length;
1324
1325 if (dep->direction) {
1326 if (count) {
1327 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1328 dep->name);
1329 status = -ECONNRESET;
1330 }
1331 } else {
1332 if (count && (event->status & DEPEVT_STATUS_SHORT))
1333 s_pkt = 1;
1334 }
1335
1336 /*
1337 * We assume here we will always receive the entire data block
1338 * which we should receive. Meaning, if we program RX to
1339 * receive 4K but we receive only 2K, we assume that's all we
1340 * should receive and we simply bounce the request back to the
1341 * gadget driver for further processing.
1342 */
1343 req->request.actual += req->request.length - count;
1344 dwc3_gadget_giveback(dep, req, status);
1345 if (s_pkt)
1346 break;
1347 if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
1348 break;
1349 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1350 break;
1351 } while (1);
1352
1353 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1354 return 0;
1355 return 1;
1356}
1357
1358static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1359 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1360 int start_new)
1361{
1362 unsigned status = 0;
1363 int clean_busy;
1364
1365 if (event->status & DEPEVT_STATUS_BUSERR)
1366 status = -ECONNRESET;
1367
1368 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001369 if (clean_busy) {
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001370 dep->flags &= ~DWC3_EP_BUSY;
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001371 dep->res_trans_idx = 0;
1372 }
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001373}
1374
1375static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1376 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1377{
1378 u32 uf;
1379
1380 if (list_empty(&dep->request_list)) {
1381 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1382 dep->name);
1383 return;
1384 }
1385
1386 if (event->parameters) {
1387 u32 mask;
1388
1389 mask = ~(dep->interval - 1);
1390 uf = event->parameters & mask;
1391 /* 4 micro frames in the future */
1392 uf += dep->interval * 4;
1393 } else {
1394 uf = 0;
1395 }
1396
1397 __dwc3_gadget_kick_transfer(dep, uf, 1);
1398}
1399
1400static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1401 const struct dwc3_event_depevt *event)
1402{
1403 struct dwc3 *dwc = dep->dwc;
1404 struct dwc3_event_depevt mod_ev = *event;
1405
1406 /*
1407 * We were asked to remove one requests. It is possible that this
1408 * request and a few other were started together and have the same
1409 * transfer index. Since we stopped the complete endpoint we don't
1410 * know how many requests were already completed (and not yet)
1411 * reported and how could be done (later). We purge them all until
1412 * the end of the list.
1413 */
1414 mod_ev.status = DEPEVT_STATUS_LST;
1415 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1416 dep->flags &= ~DWC3_EP_BUSY;
1417 /* pending requets are ignored and are queued on XferNotReady */
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001418}
1419
1420static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1421 const struct dwc3_event_depevt *event)
1422{
1423 u32 param = event->parameters;
1424 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1425
1426 switch (cmd_type) {
1427 case DWC3_DEPCMD_ENDTRANSFER:
1428 dwc3_process_ep_cmd_complete(dep, event);
1429 break;
1430 case DWC3_DEPCMD_STARTTRANSFER:
1431 dep->res_trans_idx = param & 0x7f;
1432 break;
1433 default:
1434 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1435 __func__, cmd_type);
1436 break;
1437 };
1438}
1439
1440static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1441 const struct dwc3_event_depevt *event)
1442{
1443 struct dwc3_ep *dep;
1444 u8 epnum = event->endpoint_number;
1445
1446 dep = dwc->eps[epnum];
1447
1448 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1449 dwc3_ep_event_string(event->endpoint_event));
1450
1451 if (epnum == 0 || epnum == 1) {
1452 dwc3_ep0_interrupt(dwc, event);
1453 return;
1454 }
1455
1456 switch (event->endpoint_event) {
1457 case DWC3_DEPEVT_XFERCOMPLETE:
1458 if (usb_endpoint_xfer_isoc(dep->desc)) {
1459 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1460 dep->name);
1461 return;
1462 }
1463
1464 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1465 break;
1466 case DWC3_DEPEVT_XFERINPROGRESS:
1467 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1468 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1469 dep->name);
1470 return;
1471 }
1472
1473 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1474 break;
1475 case DWC3_DEPEVT_XFERNOTREADY:
1476 if (usb_endpoint_xfer_isoc(dep->desc)) {
1477 dwc3_gadget_start_isoc(dwc, dep, event);
1478 } else {
1479 int ret;
1480
1481 dev_vdbg(dwc->dev, "%s: reason %s\n",
1482 dep->name, event->status
1483 ? "Transfer Active"
1484 : "Transfer Not Active");
1485
1486 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1487 if (!ret || ret == -EBUSY)
1488 return;
1489
1490 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1491 dep->name);
1492 }
1493
1494 break;
1495 case DWC3_DEPEVT_RXTXFIFOEVT:
1496 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1497 break;
1498 case DWC3_DEPEVT_STREAMEVT:
1499 dev_dbg(dwc->dev, "%s Stream Event\n", dep->name);
1500 break;
1501 case DWC3_DEPEVT_EPCMDCMPLT:
1502 dwc3_ep_cmd_compl(dep, event);
1503 break;
1504 }
1505}
1506
1507static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1508{
1509 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1510 spin_unlock(&dwc->lock);
1511 dwc->gadget_driver->disconnect(&dwc->gadget);
1512 spin_lock(&dwc->lock);
1513 }
1514}
1515
1516static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1517{
1518 struct dwc3_ep *dep;
1519 struct dwc3_gadget_ep_cmd_params params;
1520 u32 cmd;
1521 int ret;
1522
1523 dep = dwc->eps[epnum];
1524
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +02001525 WARN_ON(!dep->res_trans_idx);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001526 if (dep->res_trans_idx) {
1527 cmd = DWC3_DEPCMD_ENDTRANSFER;
1528 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1529 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1530 memset(&params, 0, sizeof(params));
1531 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1532 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewior4df39772011-08-22 17:42:18 +02001533 dep->res_trans_idx = 0;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001534 }
1535}
1536
1537static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1538{
1539 u32 epnum;
1540
1541 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1542 struct dwc3_ep *dep;
1543
1544 dep = dwc->eps[epnum];
1545 if (!(dep->flags & DWC3_EP_ENABLED))
1546 continue;
1547
Sebastian Andrzej Siewiorb55db3b2011-08-29 13:56:37 +02001548 dwc3_remove_requests(dwc, dep);
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001549 }
1550}
1551
1552static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1553{
1554 u32 epnum;
1555
1556 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1557 struct dwc3_ep *dep;
1558 struct dwc3_gadget_ep_cmd_params params;
1559 int ret;
1560
1561 dep = dwc->eps[epnum];
1562
1563 if (!(dep->flags & DWC3_EP_STALL))
1564 continue;
1565
1566 dep->flags &= ~DWC3_EP_STALL;
1567
1568 memset(&params, 0, sizeof(params));
1569 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1570 DWC3_DEPCMD_CLEARSTALL, &params);
1571 WARN_ON_ONCE(ret);
1572 }
1573}
1574
1575static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1576{
1577 dev_vdbg(dwc->dev, "%s\n", __func__);
1578#if 0
1579 XXX
1580 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1581 enable it before we can disable it.
1582
1583 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1584 reg &= ~DWC3_DCTL_INITU1ENA;
1585 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1586
1587 reg &= ~DWC3_DCTL_INITU2ENA;
1588 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1589#endif
1590
1591 dwc3_stop_active_transfers(dwc);
1592 dwc3_disconnect_gadget(dwc);
1593
1594 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1595}
1596
1597static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1598{
1599 u32 reg;
1600
1601 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1602
1603 if (on)
1604 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1605 else
1606 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1607
1608 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1609}
1610
1611static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1612{
1613 u32 reg;
1614
1615 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1616
1617 if (on)
1618 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1619 else
1620 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1621
1622 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1623}
1624
1625static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1626{
1627 u32 reg;
1628
1629 dev_vdbg(dwc->dev, "%s\n", __func__);
1630
1631 /* Enable PHYs */
1632 dwc3_gadget_usb2_phy_power(dwc, true);
1633 dwc3_gadget_usb3_phy_power(dwc, true);
1634
1635 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1636 dwc3_disconnect_gadget(dwc);
1637
1638 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1639 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1640 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1641
1642 dwc3_stop_active_transfers(dwc);
1643 dwc3_clear_stall_all_ep(dwc);
1644
1645 /* Reset device address to zero */
1646 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1647 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1648 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1649
1650 /*
1651 * Wait for RxFifo to drain
1652 *
1653 * REVISIT probably shouldn't wait forever.
1654 * In case Hardware ends up in a screwed up
1655 * case, we error out, notify the user and,
1656 * maybe, WARN() or BUG() but leave the rest
1657 * of the kernel working fine.
1658 *
1659 * REVISIT the below is rather CPU intensive,
1660 * maybe we should read and if it doesn't work
1661 * sleep (not busy wait) for a few useconds.
1662 *
1663 * REVISIT why wait until the RXFIFO is empty anyway?
1664 */
1665 while (!(dwc3_readl(dwc->regs, DWC3_DSTS)
1666 & DWC3_DSTS_RXFIFOEMPTY))
1667 cpu_relax();
1668}
1669
1670static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1671{
1672 u32 reg;
1673 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1674
1675 /*
1676 * We change the clock only at SS but I dunno why I would want to do
1677 * this. Maybe it becomes part of the power saving plan.
1678 */
1679
1680 if (speed != DWC3_DSTS_SUPERSPEED)
1681 return;
1682
1683 /*
1684 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1685 * each time on Connect Done.
1686 */
1687 if (!usb30_clock)
1688 return;
1689
1690 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1691 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1692 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1693}
1694
1695static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1696{
1697 switch (speed) {
1698 case USB_SPEED_SUPER:
1699 dwc3_gadget_usb2_phy_power(dwc, false);
1700 break;
1701 case USB_SPEED_HIGH:
1702 case USB_SPEED_FULL:
1703 case USB_SPEED_LOW:
1704 dwc3_gadget_usb3_phy_power(dwc, false);
1705 break;
1706 }
1707}
1708
1709static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1710{
1711 struct dwc3_gadget_ep_cmd_params params;
1712 struct dwc3_ep *dep;
1713 int ret;
1714 u32 reg;
1715 u8 speed;
1716
1717 dev_vdbg(dwc->dev, "%s\n", __func__);
1718
1719 memset(&params, 0x00, sizeof(params));
1720
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001721 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1722 speed = reg & DWC3_DSTS_CONNECTSPD;
1723 dwc->speed = speed;
1724
1725 dwc3_update_ram_clk_sel(dwc, speed);
1726
1727 switch (speed) {
1728 case DWC3_DCFG_SUPERSPEED:
1729 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1730 dwc->gadget.ep0->maxpacket = 512;
1731 dwc->gadget.speed = USB_SPEED_SUPER;
1732 break;
1733 case DWC3_DCFG_HIGHSPEED:
1734 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1735 dwc->gadget.ep0->maxpacket = 64;
1736 dwc->gadget.speed = USB_SPEED_HIGH;
1737 break;
1738 case DWC3_DCFG_FULLSPEED2:
1739 case DWC3_DCFG_FULLSPEED1:
1740 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1741 dwc->gadget.ep0->maxpacket = 64;
1742 dwc->gadget.speed = USB_SPEED_FULL;
1743 break;
1744 case DWC3_DCFG_LOWSPEED:
1745 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
1746 dwc->gadget.ep0->maxpacket = 8;
1747 dwc->gadget.speed = USB_SPEED_LOW;
1748 break;
1749 }
1750
1751 /* Disable unneded PHY */
1752 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
1753
1754 dep = dwc->eps[0];
1755 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1756 if (ret) {
1757 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1758 return;
1759 }
1760
1761 dep = dwc->eps[1];
1762 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1763 if (ret) {
1764 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1765 return;
1766 }
1767
1768 /*
1769 * Configure PHY via GUSB3PIPECTLn if required.
1770 *
1771 * Update GTXFIFOSIZn
1772 *
1773 * In both cases reset values should be sufficient.
1774 */
1775}
1776
1777static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
1778{
1779 dev_vdbg(dwc->dev, "%s\n", __func__);
1780
1781 /*
1782 * TODO take core out of low power mode when that's
1783 * implemented.
1784 */
1785
1786 dwc->gadget_driver->resume(&dwc->gadget);
1787}
1788
1789static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
1790 unsigned int evtinfo)
1791{
1792 dev_vdbg(dwc->dev, "%s\n", __func__);
1793
1794 /* The fith bit says SuperSpeed yes or no. */
1795 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
1796}
1797
1798static void dwc3_gadget_interrupt(struct dwc3 *dwc,
1799 const struct dwc3_event_devt *event)
1800{
1801 switch (event->type) {
1802 case DWC3_DEVICE_EVENT_DISCONNECT:
1803 dwc3_gadget_disconnect_interrupt(dwc);
1804 break;
1805 case DWC3_DEVICE_EVENT_RESET:
1806 dwc3_gadget_reset_interrupt(dwc);
1807 break;
1808 case DWC3_DEVICE_EVENT_CONNECT_DONE:
1809 dwc3_gadget_conndone_interrupt(dwc);
1810 break;
1811 case DWC3_DEVICE_EVENT_WAKEUP:
1812 dwc3_gadget_wakeup_interrupt(dwc);
1813 break;
1814 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
1815 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
1816 break;
1817 case DWC3_DEVICE_EVENT_EOPF:
1818 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
1819 break;
1820 case DWC3_DEVICE_EVENT_SOF:
1821 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
1822 break;
1823 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
1824 dev_vdbg(dwc->dev, "Erratic Error\n");
1825 break;
1826 case DWC3_DEVICE_EVENT_CMD_CMPL:
1827 dev_vdbg(dwc->dev, "Command Complete\n");
1828 break;
1829 case DWC3_DEVICE_EVENT_OVERFLOW:
1830 dev_vdbg(dwc->dev, "Overflow\n");
1831 break;
1832 default:
1833 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
1834 }
1835}
1836
1837static void dwc3_process_event_entry(struct dwc3 *dwc,
1838 const union dwc3_event *event)
1839{
1840 /* Endpoint IRQ, handle it and return early */
1841 if (event->type.is_devspec == 0) {
1842 /* depevt */
1843 return dwc3_endpoint_interrupt(dwc, &event->depevt);
1844 }
1845
1846 switch (event->type.type) {
1847 case DWC3_EVENT_TYPE_DEV:
1848 dwc3_gadget_interrupt(dwc, &event->devt);
1849 break;
1850 /* REVISIT what to do with Carkit and I2C events ? */
1851 default:
1852 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
1853 }
1854}
1855
1856static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
1857{
1858 struct dwc3_event_buffer *evt;
1859 int left;
1860 u32 count;
1861
1862 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
1863 count &= DWC3_GEVNTCOUNT_MASK;
1864 if (!count)
1865 return IRQ_NONE;
1866
1867 evt = dwc->ev_buffs[buf];
1868 left = count;
1869
1870 while (left > 0) {
1871 union dwc3_event event;
1872
1873 memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
1874 dwc3_process_event_entry(dwc, &event);
1875 /*
1876 * XXX we wrap around correctly to the next entry as almost all
1877 * entries are 4 bytes in size. There is one entry which has 12
1878 * bytes which is a regular entry followed by 8 bytes data. ATM
1879 * I don't know how things are organized if were get next to the
1880 * a boundary so I worry about that once we try to handle that.
1881 */
1882 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
1883 left -= 4;
1884
1885 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
1886 }
1887
1888 return IRQ_HANDLED;
1889}
1890
1891static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
1892{
1893 struct dwc3 *dwc = _dwc;
1894 int i;
1895 irqreturn_t ret = IRQ_NONE;
1896
1897 spin_lock(&dwc->lock);
1898
1899 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
1900 irqreturn_t status;
1901
1902 status = dwc3_process_event_buf(dwc, i);
1903 if (status == IRQ_HANDLED)
1904 ret = status;
1905 }
1906
1907 spin_unlock(&dwc->lock);
1908
1909 return ret;
1910}
1911
1912/**
1913 * dwc3_gadget_init - Initializes gadget related registers
1914 * @dwc: Pointer to out controller context structure
1915 *
1916 * Returns 0 on success otherwise negative errno.
1917 */
1918int __devinit dwc3_gadget_init(struct dwc3 *dwc)
1919{
1920 u32 reg;
1921 int ret;
1922 int irq;
1923
1924 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
1925 &dwc->ctrl_req_addr, GFP_KERNEL);
1926 if (!dwc->ctrl_req) {
1927 dev_err(dwc->dev, "failed to allocate ctrl request\n");
1928 ret = -ENOMEM;
1929 goto err0;
1930 }
1931
1932 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
1933 &dwc->ep0_trb_addr, GFP_KERNEL);
1934 if (!dwc->ep0_trb) {
1935 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
1936 ret = -ENOMEM;
1937 goto err1;
1938 }
1939
1940 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
1941 sizeof(*dwc->setup_buf) * 2,
1942 &dwc->setup_buf_addr, GFP_KERNEL);
1943 if (!dwc->setup_buf) {
1944 dev_err(dwc->dev, "failed to allocate setup buffer\n");
1945 ret = -ENOMEM;
1946 goto err2;
1947 }
1948
Felipe Balbi64e96342011-08-27 22:07:53 +03001949 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
1950 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
1951 if (!dwc->ep0_bounce) {
1952 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
1953 ret = -ENOMEM;
1954 goto err3;
1955 }
1956
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001957 dev_set_name(&dwc->gadget.dev, "gadget");
1958
1959 dwc->gadget.ops = &dwc3_gadget_ops;
1960 dwc->gadget.is_dualspeed = true;
1961 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1962 dwc->gadget.dev.parent = dwc->dev;
1963
1964 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
1965
1966 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
1967 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
1968 dwc->gadget.dev.release = dwc3_gadget_release;
1969 dwc->gadget.name = "dwc3-gadget";
1970
1971 /*
1972 * REVISIT: Here we should clear all pending IRQs to be
1973 * sure we're starting from a well known location.
1974 */
1975
1976 ret = dwc3_gadget_init_endpoints(dwc);
1977 if (ret)
Felipe Balbi64e96342011-08-27 22:07:53 +03001978 goto err4;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001979
1980 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1981
1982 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
1983 "dwc3", dwc);
1984 if (ret) {
1985 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1986 irq, ret);
Felipe Balbi64e96342011-08-27 22:07:53 +03001987 goto err5;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03001988 }
1989
1990 /* Enable all but Start and End of Frame IRQs */
1991 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1992 DWC3_DEVTEN_EVNTOVERFLOWEN |
1993 DWC3_DEVTEN_CMDCMPLTEN |
1994 DWC3_DEVTEN_ERRTICERREN |
1995 DWC3_DEVTEN_WKUPEVTEN |
1996 DWC3_DEVTEN_ULSTCNGEN |
1997 DWC3_DEVTEN_CONNECTDONEEN |
1998 DWC3_DEVTEN_USBRSTEN |
1999 DWC3_DEVTEN_DISCONNEVTEN);
2000 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2001
2002 ret = device_register(&dwc->gadget.dev);
2003 if (ret) {
2004 dev_err(dwc->dev, "failed to register gadget device\n");
2005 put_device(&dwc->gadget.dev);
Felipe Balbi64e96342011-08-27 22:07:53 +03002006 goto err6;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002007 }
2008
2009 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2010 if (ret) {
2011 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi64e96342011-08-27 22:07:53 +03002012 goto err7;
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002013 }
2014
2015 return 0;
2016
Felipe Balbi64e96342011-08-27 22:07:53 +03002017err7:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002018 device_unregister(&dwc->gadget.dev);
2019
Felipe Balbi64e96342011-08-27 22:07:53 +03002020err6:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002021 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2022 free_irq(irq, dwc);
2023
Felipe Balbi64e96342011-08-27 22:07:53 +03002024err5:
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002025 dwc3_gadget_free_endpoints(dwc);
2026
Felipe Balbi64e96342011-08-27 22:07:53 +03002027err4:
2028 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2029 dwc->ep0_bounce_addr);
2030
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002031err3:
2032 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2033 dwc->setup_buf, dwc->setup_buf_addr);
2034
2035err2:
2036 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2037 dwc->ep0_trb, dwc->ep0_trb_addr);
2038
2039err1:
2040 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2041 dwc->ctrl_req, dwc->ctrl_req_addr);
2042
2043err0:
2044 return ret;
2045}
2046
2047void dwc3_gadget_exit(struct dwc3 *dwc)
2048{
2049 int irq;
2050 int i;
2051
2052 usb_del_gadget_udc(&dwc->gadget);
2053 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2054
2055 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2056 free_irq(irq, dwc);
2057
2058 for (i = 0; i < ARRAY_SIZE(dwc->eps); i++)
2059 __dwc3_gadget_ep_disable(dwc->eps[i]);
2060
2061 dwc3_gadget_free_endpoints(dwc);
2062
Felipe Balbi64e96342011-08-27 22:07:53 +03002063 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2064 dwc->ep0_bounce_addr);
2065
Felipe Balbi4dc64e52011-08-19 18:10:58 +03002066 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2067 dwc->setup_buf, dwc->setup_buf_addr);
2068
2069 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2070 dwc->ep0_trb, dwc->ep0_trb_addr);
2071
2072 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2073 dwc->ctrl_req, dwc->ctrl_req_addr);
2074
2075 device_unregister(&dwc->gadget.dev);
2076}