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