blob: ddc7a43592c08164ed19f2416c3e30b785883f1c [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/io.h>
47#include <linux/list.h>
48#include <linux/dma-mapping.h>
49
50#include <linux/usb/ch9.h>
51#include <linux/usb/gadget.h>
52
53#include "core.h"
54#include "gadget.h"
55#include "io.h"
56
57#define DMA_ADDR_INVALID (~(dma_addr_t)0)
58
59void dwc3_map_buffer_to_dma(struct dwc3_request *req)
60{
61 struct dwc3 *dwc = req->dep->dwc;
62
Sebastian Andrzej Siewior78c58a52011-08-31 17:12:02 +020063 if (req->request.length == 0) {
64 /* req->request.dma = dwc->setup_buf_addr; */
65 return;
66 }
67
Felipe Balbieeb720f2011-11-28 12:46:59 +020068 if (req->request.num_sgs) {
69 int mapped;
70
71 mapped = dma_map_sg(dwc->dev, req->request.sg,
72 req->request.num_sgs,
73 req->direction ? DMA_TO_DEVICE
74 : DMA_FROM_DEVICE);
75 if (mapped < 0) {
76 dev_err(dwc->dev, "failed to map SGs\n");
77 return;
78 }
79
80 req->request.num_mapped_sgs = mapped;
81 return;
82 }
83
Felipe Balbi72246da2011-08-19 18:10:58 +030084 if (req->request.dma == DMA_ADDR_INVALID) {
85 req->request.dma = dma_map_single(dwc->dev, req->request.buf,
86 req->request.length, req->direction
87 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
88 req->mapped = true;
Felipe Balbi72246da2011-08-19 18:10:58 +030089 }
90}
91
92void dwc3_unmap_buffer_from_dma(struct dwc3_request *req)
93{
94 struct dwc3 *dwc = req->dep->dwc;
95
Sebastian Andrzej Siewior78c58a52011-08-31 17:12:02 +020096 if (req->request.length == 0) {
97 req->request.dma = DMA_ADDR_INVALID;
98 return;
99 }
100
Felipe Balbieeb720f2011-11-28 12:46:59 +0200101 if (req->request.num_mapped_sgs) {
102 req->request.dma = DMA_ADDR_INVALID;
103 dma_unmap_sg(dwc->dev, req->request.sg,
104 req->request.num_sgs,
105 req->direction ? DMA_TO_DEVICE
106 : DMA_FROM_DEVICE);
107
108 req->request.num_mapped_sgs = 0;
109 return;
110 }
111
Felipe Balbi72246da2011-08-19 18:10:58 +0300112 if (req->mapped) {
113 dma_unmap_single(dwc->dev, req->request.dma,
114 req->request.length, req->direction
115 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
116 req->mapped = 0;
Felipe Balbif198ead2011-08-27 15:10:09 +0300117 req->request.dma = DMA_ADDR_INVALID;
Felipe Balbi72246da2011-08-19 18:10:58 +0300118 }
119}
120
121void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
122 int status)
123{
124 struct dwc3 *dwc = dep->dwc;
125
126 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200127 if (req->request.num_mapped_sgs)
128 dep->busy_slot += req->request.num_mapped_sgs;
129 else
130 dep->busy_slot++;
131
Felipe Balbi72246da2011-08-19 18:10:58 +0300132 /*
133 * Skip LINK TRB. We can't use req->trb and check for
134 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
135 * completed (not the LINK TRB).
136 */
137 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
138 usb_endpoint_xfer_isoc(dep->desc))
139 dep->busy_slot++;
140 }
141 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200142 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300143
144 if (req->request.status == -EINPROGRESS)
145 req->request.status = status;
146
147 dwc3_unmap_buffer_from_dma(req);
148
149 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
150 req, dep->name, req->request.actual,
151 req->request.length, status);
152
153 spin_unlock(&dwc->lock);
154 req->request.complete(&req->dep->endpoint, &req->request);
155 spin_lock(&dwc->lock);
156}
157
158static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
159{
160 switch (cmd) {
161 case DWC3_DEPCMD_DEPSTARTCFG:
162 return "Start New Configuration";
163 case DWC3_DEPCMD_ENDTRANSFER:
164 return "End Transfer";
165 case DWC3_DEPCMD_UPDATETRANSFER:
166 return "Update Transfer";
167 case DWC3_DEPCMD_STARTTRANSFER:
168 return "Start Transfer";
169 case DWC3_DEPCMD_CLEARSTALL:
170 return "Clear Stall";
171 case DWC3_DEPCMD_SETSTALL:
172 return "Set Stall";
173 case DWC3_DEPCMD_GETSEQNUMBER:
174 return "Get Data Sequence Number";
175 case DWC3_DEPCMD_SETTRANSFRESOURCE:
176 return "Set Endpoint Transfer Resource";
177 case DWC3_DEPCMD_SETEPCONFIG:
178 return "Set Endpoint Configuration";
179 default:
180 return "UNKNOWN command";
181 }
182}
183
184int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
185 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
186{
187 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200188 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300189 u32 reg;
190
191 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
192 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300193 dwc3_gadget_ep_cmd_string(cmd), params->param0,
194 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300195
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300196 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
197 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
198 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300199
200 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
201 do {
202 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
203 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300204 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
205 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300206 return 0;
207 }
208
209 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300210 * We can't sleep here, because it is also called from
211 * interrupt context.
212 */
213 timeout--;
214 if (!timeout)
215 return -ETIMEDOUT;
216
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200217 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300218 } while (1);
219}
220
221static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
222 struct dwc3_trb_hw *trb)
223{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300224 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300225
226 return dep->trb_pool_dma + offset;
227}
228
229static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
230{
231 struct dwc3 *dwc = dep->dwc;
232
233 if (dep->trb_pool)
234 return 0;
235
236 if (dep->number == 0 || dep->number == 1)
237 return 0;
238
239 dep->trb_pool = dma_alloc_coherent(dwc->dev,
240 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
241 &dep->trb_pool_dma, GFP_KERNEL);
242 if (!dep->trb_pool) {
243 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
244 dep->name);
245 return -ENOMEM;
246 }
247
248 return 0;
249}
250
251static void dwc3_free_trb_pool(struct dwc3_ep *dep)
252{
253 struct dwc3 *dwc = dep->dwc;
254
255 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
256 dep->trb_pool, dep->trb_pool_dma);
257
258 dep->trb_pool = NULL;
259 dep->trb_pool_dma = 0;
260}
261
262static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
263{
264 struct dwc3_gadget_ep_cmd_params params;
265 u32 cmd;
266
267 memset(&params, 0x00, sizeof(params));
268
269 if (dep->number != 1) {
270 cmd = DWC3_DEPCMD_DEPSTARTCFG;
271 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300272 if (dep->number > 1) {
273 if (dwc->start_config_issued)
274 return 0;
275 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300276 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300277 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300278
279 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
280 }
281
282 return 0;
283}
284
285static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
286 const struct usb_endpoint_descriptor *desc)
287{
288 struct dwc3_gadget_ep_cmd_params params;
289
290 memset(&params, 0x00, sizeof(params));
291
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300292 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
293 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
294 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300295
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300296 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
297 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300298
Felipe Balbi879631a2011-09-30 10:58:47 +0300299 if (usb_endpoint_xfer_bulk(desc) && dep->endpoint.max_streams) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300300 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
301 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300302 dep->stream_capable = true;
303 }
304
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300306 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300307
308 /*
309 * We are doing 1:1 mapping for endpoints, meaning
310 * Physical Endpoints 2 maps to Logical Endpoint 2 and
311 * so on. We consider the direction bit as part of the physical
312 * endpoint number. So USB endpoint 0x81 is 0x03.
313 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300314 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300315
316 /*
317 * We must use the lower 16 TX FIFOs even though
318 * HW might have more
319 */
320 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300321 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300322
323 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300324 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300325 dep->interval = 1 << (desc->bInterval - 1);
326 }
327
328 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
329 DWC3_DEPCMD_SETEPCONFIG, &params);
330}
331
332static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
333{
334 struct dwc3_gadget_ep_cmd_params params;
335
336 memset(&params, 0x00, sizeof(params));
337
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300338 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300339
340 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
341 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
342}
343
344/**
345 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
346 * @dep: endpoint to be initialized
347 * @desc: USB Endpoint Descriptor
348 *
349 * Caller should take care of locking
350 */
351static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
352 const struct usb_endpoint_descriptor *desc)
353{
354 struct dwc3 *dwc = dep->dwc;
355 u32 reg;
356 int ret = -ENOMEM;
357
358 if (!(dep->flags & DWC3_EP_ENABLED)) {
359 ret = dwc3_gadget_start_config(dwc, dep);
360 if (ret)
361 return ret;
362 }
363
364 ret = dwc3_gadget_set_ep_config(dwc, dep, desc);
365 if (ret)
366 return ret;
367
368 if (!(dep->flags & DWC3_EP_ENABLED)) {
369 struct dwc3_trb_hw *trb_st_hw;
370 struct dwc3_trb_hw *trb_link_hw;
371 struct dwc3_trb trb_link;
372
373 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
374 if (ret)
375 return ret;
376
377 dep->desc = desc;
378 dep->type = usb_endpoint_type(desc);
379 dep->flags |= DWC3_EP_ENABLED;
380
381 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
382 reg |= DWC3_DALEPENA_EP(dep->number);
383 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
384
385 if (!usb_endpoint_xfer_isoc(desc))
386 return 0;
387
388 memset(&trb_link, 0, sizeof(trb_link));
389
390 /* Link TRB for ISOC. The HWO but is never reset */
391 trb_st_hw = &dep->trb_pool[0];
392
393 trb_link.bplh = dwc3_trb_dma_offset(dep, trb_st_hw);
394 trb_link.trbctl = DWC3_TRBCTL_LINK_TRB;
395 trb_link.hwo = true;
396
397 trb_link_hw = &dep->trb_pool[DWC3_TRB_NUM - 1];
398 dwc3_trb_to_hw(&trb_link, trb_link_hw);
399 }
400
401 return 0;
402}
403
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200404static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
405static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300406{
407 struct dwc3_request *req;
408
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200409 if (!list_empty(&dep->req_queued))
410 dwc3_stop_active_transfer(dwc, dep->number);
411
Felipe Balbi72246da2011-08-19 18:10:58 +0300412 while (!list_empty(&dep->request_list)) {
413 req = next_request(&dep->request_list);
414
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200415 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300416 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300417}
418
419/**
420 * __dwc3_gadget_ep_disable - Disables a HW endpoint
421 * @dep: the endpoint to disable
422 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200423 * This function also removes requests which are currently processed ny the
424 * hardware and those which are not yet scheduled.
425 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300426 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300427static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
428{
429 struct dwc3 *dwc = dep->dwc;
430 u32 reg;
431
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200432 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300433
434 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
435 reg &= ~DWC3_DALEPENA_EP(dep->number);
436 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
437
Felipe Balbi879631a2011-09-30 10:58:47 +0300438 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300439 dep->desc = NULL;
440 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300441 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300442
443 return 0;
444}
445
446/* -------------------------------------------------------------------------- */
447
448static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
449 const struct usb_endpoint_descriptor *desc)
450{
451 return -EINVAL;
452}
453
454static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
455{
456 return -EINVAL;
457}
458
459/* -------------------------------------------------------------------------- */
460
461static int dwc3_gadget_ep_enable(struct usb_ep *ep,
462 const struct usb_endpoint_descriptor *desc)
463{
464 struct dwc3_ep *dep;
465 struct dwc3 *dwc;
466 unsigned long flags;
467 int ret;
468
469 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
470 pr_debug("dwc3: invalid parameters\n");
471 return -EINVAL;
472 }
473
474 if (!desc->wMaxPacketSize) {
475 pr_debug("dwc3: missing wMaxPacketSize\n");
476 return -EINVAL;
477 }
478
479 dep = to_dwc3_ep(ep);
480 dwc = dep->dwc;
481
482 switch (usb_endpoint_type(desc)) {
483 case USB_ENDPOINT_XFER_CONTROL:
484 strncat(dep->name, "-control", sizeof(dep->name));
485 break;
486 case USB_ENDPOINT_XFER_ISOC:
487 strncat(dep->name, "-isoc", sizeof(dep->name));
488 break;
489 case USB_ENDPOINT_XFER_BULK:
490 strncat(dep->name, "-bulk", sizeof(dep->name));
491 break;
492 case USB_ENDPOINT_XFER_INT:
493 strncat(dep->name, "-int", sizeof(dep->name));
494 break;
495 default:
496 dev_err(dwc->dev, "invalid endpoint transfer type\n");
497 }
498
499 if (dep->flags & DWC3_EP_ENABLED) {
500 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
501 dep->name);
502 return 0;
503 }
504
505 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
506
507 spin_lock_irqsave(&dwc->lock, flags);
508 ret = __dwc3_gadget_ep_enable(dep, desc);
509 spin_unlock_irqrestore(&dwc->lock, flags);
510
511 return ret;
512}
513
514static int dwc3_gadget_ep_disable(struct usb_ep *ep)
515{
516 struct dwc3_ep *dep;
517 struct dwc3 *dwc;
518 unsigned long flags;
519 int ret;
520
521 if (!ep) {
522 pr_debug("dwc3: invalid parameters\n");
523 return -EINVAL;
524 }
525
526 dep = to_dwc3_ep(ep);
527 dwc = dep->dwc;
528
529 if (!(dep->flags & DWC3_EP_ENABLED)) {
530 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
531 dep->name);
532 return 0;
533 }
534
535 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
536 dep->number >> 1,
537 (dep->number & 1) ? "in" : "out");
538
539 spin_lock_irqsave(&dwc->lock, flags);
540 ret = __dwc3_gadget_ep_disable(dep);
541 spin_unlock_irqrestore(&dwc->lock, flags);
542
543 return ret;
544}
545
546static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
547 gfp_t gfp_flags)
548{
549 struct dwc3_request *req;
550 struct dwc3_ep *dep = to_dwc3_ep(ep);
551 struct dwc3 *dwc = dep->dwc;
552
553 req = kzalloc(sizeof(*req), gfp_flags);
554 if (!req) {
555 dev_err(dwc->dev, "not enough memory\n");
556 return NULL;
557 }
558
559 req->epnum = dep->number;
560 req->dep = dep;
561 req->request.dma = DMA_ADDR_INVALID;
562
563 return &req->request;
564}
565
566static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
567 struct usb_request *request)
568{
569 struct dwc3_request *req = to_dwc3_request(request);
570
571 kfree(req);
572}
573
Felipe Balbic71fc372011-11-22 11:37:34 +0200574/**
575 * dwc3_prepare_one_trb - setup one TRB from one request
576 * @dep: endpoint for which this request is prepared
577 * @req: dwc3_request pointer
578 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200579static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200580 struct dwc3_request *req, dma_addr_t dma,
581 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200582{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200583 struct dwc3 *dwc = dep->dwc;
Felipe Balbic71fc372011-11-22 11:37:34 +0200584 struct dwc3_trb_hw *trb_hw;
585 struct dwc3_trb trb;
586
587 unsigned int cur_slot;
588
Felipe Balbieeb720f2011-11-28 12:46:59 +0200589 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
590 dep->name, req, (unsigned long long) dma,
591 length, last ? " last" : "",
592 chain ? " chain" : "");
593
Felipe Balbic71fc372011-11-22 11:37:34 +0200594 trb_hw = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
595 cur_slot = dep->free_slot;
596 dep->free_slot++;
597
598 /* Skip the LINK-TRB on ISOC */
599 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
600 usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200601 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200602
Felipe Balbic71fc372011-11-22 11:37:34 +0200603 memset(&trb, 0, sizeof(trb));
Felipe Balbieeb720f2011-11-28 12:46:59 +0200604 if (!req->trb) {
605 dwc3_gadget_move_request_queued(req);
606 req->trb = trb_hw;
607 req->trb_dma = dwc3_trb_dma_offset(dep, trb_hw);
608 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200609
610 if (usb_endpoint_xfer_isoc(dep->desc)) {
611 trb.isp_imi = true;
612 trb.csp = true;
613 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200614 trb.chn = chain;
Felipe Balbic71fc372011-11-22 11:37:34 +0200615 trb.lst = last;
616 }
617
618 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
619 trb.sid_sofn = req->request.stream_id;
620
621 switch (usb_endpoint_type(dep->desc)) {
622 case USB_ENDPOINT_XFER_CONTROL:
623 trb.trbctl = DWC3_TRBCTL_CONTROL_SETUP;
624 break;
625
626 case USB_ENDPOINT_XFER_ISOC:
627 trb.trbctl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
628
629 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
630 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
631 trb.ioc = last;
632 break;
633
634 case USB_ENDPOINT_XFER_BULK:
635 case USB_ENDPOINT_XFER_INT:
636 trb.trbctl = DWC3_TRBCTL_NORMAL;
637 break;
638 default:
639 /*
640 * This is only possible with faulty memory because we
641 * checked it already :)
642 */
643 BUG();
644 }
645
Felipe Balbieeb720f2011-11-28 12:46:59 +0200646 trb.length = length;
647 trb.bplh = dma;
Felipe Balbic71fc372011-11-22 11:37:34 +0200648 trb.hwo = true;
649
650 dwc3_trb_to_hw(&trb, trb_hw);
Felipe Balbic71fc372011-11-22 11:37:34 +0200651}
652
Felipe Balbi72246da2011-08-19 18:10:58 +0300653/*
654 * dwc3_prepare_trbs - setup TRBs from requests
655 * @dep: endpoint for which requests are being prepared
656 * @starting: true if the endpoint is idle and no requests are queued.
657 *
658 * The functions goes through the requests list and setups TRBs for the
659 * transfers. The functions returns once there are not more TRBs available or
660 * it run out of requests.
661 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200662static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300663{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200664 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300665 u32 trbs_left;
Felipe Balbic71fc372011-11-22 11:37:34 +0200666 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300667
668 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
669
670 /* the first request must not be queued */
671 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200672
Felipe Balbi72246da2011-08-19 18:10:58 +0300673 /*
674 * if busy & slot are equal than it is either full or empty. If we are
675 * starting to proceed requests then we are empty. Otherwise we ar
676 * full and don't do anything
677 */
678 if (!trbs_left) {
679 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200680 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 trbs_left = DWC3_TRB_NUM;
682 /*
683 * In case we start from scratch, we queue the ISOC requests
684 * starting from slot 1. This is done because we use ring
685 * buffer and have no LST bit to stop us. Instead, we place
686 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
687 * after the first request so we start at slot 1 and have
688 * 7 requests proceed before we hit the first IOC.
689 * Other transfer types don't use the ring buffer and are
690 * processed from the first TRB until the last one. Since we
691 * don't wrap around we have to start at the beginning.
692 */
693 if (usb_endpoint_xfer_isoc(dep->desc)) {
694 dep->busy_slot = 1;
695 dep->free_slot = 1;
696 } else {
697 dep->busy_slot = 0;
698 dep->free_slot = 0;
699 }
700 }
701
702 /* The last TRB is a link TRB, not used for xfer */
703 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200704 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300705
706 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200707 unsigned length;
708 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300709
Felipe Balbieeb720f2011-11-28 12:46:59 +0200710 if (req->request.num_mapped_sgs > 0) {
711 struct usb_request *request = &req->request;
712 struct scatterlist *sg = request->sg;
713 struct scatterlist *s;
714 int i;
Felipe Balbic71fc372011-11-22 11:37:34 +0200715
Felipe Balbieeb720f2011-11-28 12:46:59 +0200716 for_each_sg(sg, s, request->num_mapped_sgs, i) {
717 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300718
Felipe Balbieeb720f2011-11-28 12:46:59 +0200719 length = sg_dma_len(s);
720 dma = sg_dma_address(s);
Felipe Balbi42f8eb72011-11-28 12:27:17 +0200721
Felipe Balbieeb720f2011-11-28 12:46:59 +0200722 if (i == (request->num_mapped_sgs - 1)
723 || sg_is_last(s)) {
724 last_one = true;
725 chain = false;
726 }
727
728 trbs_left--;
729 if (!trbs_left)
730 last_one = true;
731
732 if (last_one)
733 chain = false;
734
735 dwc3_prepare_one_trb(dep, req, dma, length,
736 last_one, chain);
737
738 if (last_one)
739 break;
740 }
741 } else {
742 dma = req->request.dma;
743 length = req->request.length;
744 trbs_left--;
745
746 if (!trbs_left)
747 last_one = 1;
748
749 /* Is this the last request? */
750 if (list_is_last(&req->list, &dep->request_list))
751 last_one = 1;
752
753 dwc3_prepare_one_trb(dep, req, dma, length,
754 last_one, false);
755
756 if (last_one)
757 break;
758 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300759 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300760}
761
762static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
763 int start_new)
764{
765 struct dwc3_gadget_ep_cmd_params params;
766 struct dwc3_request *req;
767 struct dwc3 *dwc = dep->dwc;
768 int ret;
769 u32 cmd;
770
771 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
772 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
773 return -EBUSY;
774 }
775 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
776
777 /*
778 * If we are getting here after a short-out-packet we don't enqueue any
779 * new requests as we try to set the IOC bit only on the last request.
780 */
781 if (start_new) {
782 if (list_empty(&dep->req_queued))
783 dwc3_prepare_trbs(dep, start_new);
784
785 /* req points to the first request which will be sent */
786 req = next_request(&dep->req_queued);
787 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200788 dwc3_prepare_trbs(dep, start_new);
789
Felipe Balbi72246da2011-08-19 18:10:58 +0300790 /*
791 * req points to the first request where HWO changed
792 * from 0 to 1
793 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200794 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 }
796 if (!req) {
797 dep->flags |= DWC3_EP_PENDING_REQUEST;
798 return 0;
799 }
800
801 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300802 params.param0 = upper_32_bits(req->trb_dma);
803 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300804
805 if (start_new)
806 cmd = DWC3_DEPCMD_STARTTRANSFER;
807 else
808 cmd = DWC3_DEPCMD_UPDATETRANSFER;
809
810 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
811 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
812 if (ret < 0) {
813 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
814
815 /*
816 * FIXME we need to iterate over the list of requests
817 * here and stop, unmap, free and del each of the linked
818 * requests instead of we do now.
819 */
820 dwc3_unmap_buffer_from_dma(req);
821 list_del(&req->list);
822 return ret;
823 }
824
825 dep->flags |= DWC3_EP_BUSY;
826 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
827 dep->number);
828 if (!dep->res_trans_idx)
829 printk_once(KERN_ERR "%s() res_trans_idx is invalid\n", __func__);
830 return 0;
831}
832
833static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
834{
835 req->request.actual = 0;
836 req->request.status = -EINPROGRESS;
837 req->direction = dep->direction;
838 req->epnum = dep->number;
839
840 /*
841 * We only add to our list of requests now and
842 * start consuming the list once we get XferNotReady
843 * IRQ.
844 *
845 * That way, we avoid doing anything that we don't need
846 * to do now and defer it until the point we receive a
847 * particular token from the Host side.
848 *
849 * This will also avoid Host cancelling URBs due to too
850 * many NACKs.
851 */
852 dwc3_map_buffer_to_dma(req);
853 list_add_tail(&req->list, &dep->request_list);
854
855 /*
856 * There is one special case: XferNotReady with
857 * empty list of requests. We need to kick the
858 * transfer here in that situation, otherwise
859 * we will be NAKing forever.
860 *
861 * If we get XferNotReady before gadget driver
862 * has a chance to queue a request, we will ACK
863 * the IRQ but won't be able to receive the data
864 * until the next request is queued. The following
865 * code is handling exactly that.
866 */
867 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
868 int ret;
869 int start_trans;
870
871 start_trans = 1;
872 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
873 dep->flags & DWC3_EP_BUSY)
874 start_trans = 0;
875
876 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
877 if (ret && ret != -EBUSY) {
878 struct dwc3 *dwc = dep->dwc;
879
880 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
881 dep->name);
882 }
883 };
884
885 return 0;
886}
887
888static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
889 gfp_t gfp_flags)
890{
891 struct dwc3_request *req = to_dwc3_request(request);
892 struct dwc3_ep *dep = to_dwc3_ep(ep);
893 struct dwc3 *dwc = dep->dwc;
894
895 unsigned long flags;
896
897 int ret;
898
899 if (!dep->desc) {
900 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
901 request, ep->name);
902 return -ESHUTDOWN;
903 }
904
905 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
906 request, ep->name, request->length);
907
908 spin_lock_irqsave(&dwc->lock, flags);
909 ret = __dwc3_gadget_ep_queue(dep, req);
910 spin_unlock_irqrestore(&dwc->lock, flags);
911
912 return ret;
913}
914
915static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
916 struct usb_request *request)
917{
918 struct dwc3_request *req = to_dwc3_request(request);
919 struct dwc3_request *r = NULL;
920
921 struct dwc3_ep *dep = to_dwc3_ep(ep);
922 struct dwc3 *dwc = dep->dwc;
923
924 unsigned long flags;
925 int ret = 0;
926
927 spin_lock_irqsave(&dwc->lock, flags);
928
929 list_for_each_entry(r, &dep->request_list, list) {
930 if (r == req)
931 break;
932 }
933
934 if (r != req) {
935 list_for_each_entry(r, &dep->req_queued, list) {
936 if (r == req)
937 break;
938 }
939 if (r == req) {
940 /* wait until it is processed */
941 dwc3_stop_active_transfer(dwc, dep->number);
942 goto out0;
943 }
944 dev_err(dwc->dev, "request %p was not queued to %s\n",
945 request, ep->name);
946 ret = -EINVAL;
947 goto out0;
948 }
949
950 /* giveback the request */
951 dwc3_gadget_giveback(dep, req, -ECONNRESET);
952
953out0:
954 spin_unlock_irqrestore(&dwc->lock, flags);
955
956 return ret;
957}
958
959int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
960{
961 struct dwc3_gadget_ep_cmd_params params;
962 struct dwc3 *dwc = dep->dwc;
963 int ret;
964
965 memset(&params, 0x00, sizeof(params));
966
967 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +0300968 if (dep->number == 0 || dep->number == 1) {
969 /*
970 * Whenever EP0 is stalled, we will restart
971 * the state machine, thus moving back to
972 * Setup Phase
973 */
974 dwc->ep0state = EP0_SETUP_PHASE;
975 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300976
977 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
978 DWC3_DEPCMD_SETSTALL, &params);
979 if (ret)
980 dev_err(dwc->dev, "failed to %s STALL on %s\n",
981 value ? "set" : "clear",
982 dep->name);
983 else
984 dep->flags |= DWC3_EP_STALL;
985 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +0300986 if (dep->flags & DWC3_EP_WEDGE)
987 return 0;
988
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
990 DWC3_DEPCMD_CLEARSTALL, &params);
991 if (ret)
992 dev_err(dwc->dev, "failed to %s STALL on %s\n",
993 value ? "set" : "clear",
994 dep->name);
995 else
996 dep->flags &= ~DWC3_EP_STALL;
997 }
Paul Zimmerman52754552011-09-30 10:58:44 +0300998
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 return ret;
1000}
1001
1002static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1003{
1004 struct dwc3_ep *dep = to_dwc3_ep(ep);
1005 struct dwc3 *dwc = dep->dwc;
1006
1007 unsigned long flags;
1008
1009 int ret;
1010
1011 spin_lock_irqsave(&dwc->lock, flags);
1012
1013 if (usb_endpoint_xfer_isoc(dep->desc)) {
1014 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1015 ret = -EINVAL;
1016 goto out;
1017 }
1018
1019 ret = __dwc3_gadget_ep_set_halt(dep, value);
1020out:
1021 spin_unlock_irqrestore(&dwc->lock, flags);
1022
1023 return ret;
1024}
1025
1026static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1027{
1028 struct dwc3_ep *dep = to_dwc3_ep(ep);
1029
1030 dep->flags |= DWC3_EP_WEDGE;
1031
Paul Zimmerman52754552011-09-30 10:58:44 +03001032 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001033}
1034
1035/* -------------------------------------------------------------------------- */
1036
1037static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1038 .bLength = USB_DT_ENDPOINT_SIZE,
1039 .bDescriptorType = USB_DT_ENDPOINT,
1040 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1041};
1042
1043static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1044 .enable = dwc3_gadget_ep0_enable,
1045 .disable = dwc3_gadget_ep0_disable,
1046 .alloc_request = dwc3_gadget_ep_alloc_request,
1047 .free_request = dwc3_gadget_ep_free_request,
1048 .queue = dwc3_gadget_ep0_queue,
1049 .dequeue = dwc3_gadget_ep_dequeue,
1050 .set_halt = dwc3_gadget_ep_set_halt,
1051 .set_wedge = dwc3_gadget_ep_set_wedge,
1052};
1053
1054static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1055 .enable = dwc3_gadget_ep_enable,
1056 .disable = dwc3_gadget_ep_disable,
1057 .alloc_request = dwc3_gadget_ep_alloc_request,
1058 .free_request = dwc3_gadget_ep_free_request,
1059 .queue = dwc3_gadget_ep_queue,
1060 .dequeue = dwc3_gadget_ep_dequeue,
1061 .set_halt = dwc3_gadget_ep_set_halt,
1062 .set_wedge = dwc3_gadget_ep_set_wedge,
1063};
1064
1065/* -------------------------------------------------------------------------- */
1066
1067static int dwc3_gadget_get_frame(struct usb_gadget *g)
1068{
1069 struct dwc3 *dwc = gadget_to_dwc(g);
1070 u32 reg;
1071
1072 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1073 return DWC3_DSTS_SOFFN(reg);
1074}
1075
1076static int dwc3_gadget_wakeup(struct usb_gadget *g)
1077{
1078 struct dwc3 *dwc = gadget_to_dwc(g);
1079
1080 unsigned long timeout;
1081 unsigned long flags;
1082
1083 u32 reg;
1084
1085 int ret = 0;
1086
1087 u8 link_state;
1088 u8 speed;
1089
1090 spin_lock_irqsave(&dwc->lock, flags);
1091
1092 /*
1093 * According to the Databook Remote wakeup request should
1094 * be issued only when the device is in early suspend state.
1095 *
1096 * We can check that via USB Link State bits in DSTS register.
1097 */
1098 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1099
1100 speed = reg & DWC3_DSTS_CONNECTSPD;
1101 if (speed == DWC3_DSTS_SUPERSPEED) {
1102 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1103 ret = -EINVAL;
1104 goto out;
1105 }
1106
1107 link_state = DWC3_DSTS_USBLNKST(reg);
1108
1109 switch (link_state) {
1110 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1111 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1112 break;
1113 default:
1114 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1115 link_state);
1116 ret = -EINVAL;
1117 goto out;
1118 }
1119
1120 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1121
1122 /*
1123 * Switch link state to Recovery. In HS/FS/LS this means
1124 * RemoteWakeup Request
1125 */
1126 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
1127 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1128
1129 /* wait for at least 2000us */
1130 usleep_range(2000, 2500);
1131
1132 /* write zeroes to Link Change Request */
1133 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1134 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1135
1136 /* pool until Link State change to ON */
1137 timeout = jiffies + msecs_to_jiffies(100);
1138
1139 while (!(time_after(jiffies, timeout))) {
1140 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1141
1142 /* in HS, means ON */
1143 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1144 break;
1145 }
1146
1147 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1148 dev_err(dwc->dev, "failed to send remote wakeup\n");
1149 ret = -EINVAL;
1150 }
1151
1152out:
1153 spin_unlock_irqrestore(&dwc->lock, flags);
1154
1155 return ret;
1156}
1157
1158static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1159 int is_selfpowered)
1160{
1161 struct dwc3 *dwc = gadget_to_dwc(g);
1162
1163 dwc->is_selfpowered = !!is_selfpowered;
1164
1165 return 0;
1166}
1167
1168static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1169{
1170 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001171 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001172
1173 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1174 if (is_on)
1175 reg |= DWC3_DCTL_RUN_STOP;
1176 else
1177 reg &= ~DWC3_DCTL_RUN_STOP;
1178
1179 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1180
1181 do {
1182 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1183 if (is_on) {
1184 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1185 break;
1186 } else {
1187 if (reg & DWC3_DSTS_DEVCTRLHLT)
1188 break;
1189 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001190 timeout--;
1191 if (!timeout)
1192 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001193 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001194 } while (1);
1195
1196 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1197 dwc->gadget_driver
1198 ? dwc->gadget_driver->function : "no-function",
1199 is_on ? "connect" : "disconnect");
1200}
1201
1202static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1203{
1204 struct dwc3 *dwc = gadget_to_dwc(g);
1205 unsigned long flags;
1206
1207 is_on = !!is_on;
1208
1209 spin_lock_irqsave(&dwc->lock, flags);
1210 dwc3_gadget_run_stop(dwc, is_on);
1211 spin_unlock_irqrestore(&dwc->lock, flags);
1212
1213 return 0;
1214}
1215
1216static int dwc3_gadget_start(struct usb_gadget *g,
1217 struct usb_gadget_driver *driver)
1218{
1219 struct dwc3 *dwc = gadget_to_dwc(g);
1220 struct dwc3_ep *dep;
1221 unsigned long flags;
1222 int ret = 0;
1223 u32 reg;
1224
1225 spin_lock_irqsave(&dwc->lock, flags);
1226
1227 if (dwc->gadget_driver) {
1228 dev_err(dwc->dev, "%s is already bound to %s\n",
1229 dwc->gadget.name,
1230 dwc->gadget_driver->driver.name);
1231 ret = -EBUSY;
1232 goto err0;
1233 }
1234
1235 dwc->gadget_driver = driver;
1236 dwc->gadget.dev.driver = &driver->driver;
1237
1238 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1239
Felipe Balbi771f1842011-09-08 17:42:11 +03001240 reg &= ~DWC3_GCTL_SCALEDOWN(3);
1241 reg &= ~DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG);
Felipe Balbi72246da2011-08-19 18:10:58 +03001242 reg &= ~DWC3_GCTL_DISSCRAMBLE;
Felipe Balbi771f1842011-09-08 17:42:11 +03001243 reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001244
Felipe Balbiaabb7072011-09-30 10:58:50 +03001245 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams0)) {
1246 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
1247 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
1248 break;
1249 default:
1250 dev_dbg(dwc->dev, "No power optimization available\n");
1251 }
1252
Felipe Balbi72246da2011-08-19 18:10:58 +03001253 /*
1254 * WORKAROUND: DWC3 revisions <1.90a have a bug
1255 * when The device fails to connect at SuperSpeed
1256 * and falls back to high-speed mode which causes
1257 * the device to enter in a Connect/Disconnect loop
1258 */
1259 if (dwc->revision < DWC3_REVISION_190A)
1260 reg |= DWC3_GCTL_U2RSTECN;
1261
1262 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1263
1264 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1265 reg &= ~(DWC3_DCFG_SPEED_MASK);
1266 reg |= DWC3_DCFG_SUPERSPEED;
1267 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1268
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001269 dwc->start_config_issued = false;
1270
Felipe Balbi72246da2011-08-19 18:10:58 +03001271 /* Start with SuperSpeed Default */
1272 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1273
1274 dep = dwc->eps[0];
1275 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1276 if (ret) {
1277 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1278 goto err0;
1279 }
1280
1281 dep = dwc->eps[1];
1282 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1283 if (ret) {
1284 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1285 goto err1;
1286 }
1287
1288 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001289 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001290 dwc3_ep0_out_start(dwc);
1291
1292 spin_unlock_irqrestore(&dwc->lock, flags);
1293
1294 return 0;
1295
1296err1:
1297 __dwc3_gadget_ep_disable(dwc->eps[0]);
1298
1299err0:
1300 spin_unlock_irqrestore(&dwc->lock, flags);
1301
1302 return ret;
1303}
1304
1305static int dwc3_gadget_stop(struct usb_gadget *g,
1306 struct usb_gadget_driver *driver)
1307{
1308 struct dwc3 *dwc = gadget_to_dwc(g);
1309 unsigned long flags;
1310
1311 spin_lock_irqsave(&dwc->lock, flags);
1312
1313 __dwc3_gadget_ep_disable(dwc->eps[0]);
1314 __dwc3_gadget_ep_disable(dwc->eps[1]);
1315
1316 dwc->gadget_driver = NULL;
1317 dwc->gadget.dev.driver = NULL;
1318
1319 spin_unlock_irqrestore(&dwc->lock, flags);
1320
1321 return 0;
1322}
1323static const struct usb_gadget_ops dwc3_gadget_ops = {
1324 .get_frame = dwc3_gadget_get_frame,
1325 .wakeup = dwc3_gadget_wakeup,
1326 .set_selfpowered = dwc3_gadget_set_selfpowered,
1327 .pullup = dwc3_gadget_pullup,
1328 .udc_start = dwc3_gadget_start,
1329 .udc_stop = dwc3_gadget_stop,
1330};
1331
1332/* -------------------------------------------------------------------------- */
1333
1334static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1335{
1336 struct dwc3_ep *dep;
1337 u8 epnum;
1338
1339 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1340
1341 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1342 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1343 if (!dep) {
1344 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1345 epnum);
1346 return -ENOMEM;
1347 }
1348
1349 dep->dwc = dwc;
1350 dep->number = epnum;
1351 dwc->eps[epnum] = dep;
1352
1353 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1354 (epnum & 1) ? "in" : "out");
1355 dep->endpoint.name = dep->name;
1356 dep->direction = (epnum & 1);
1357
1358 if (epnum == 0 || epnum == 1) {
1359 dep->endpoint.maxpacket = 512;
1360 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1361 if (!epnum)
1362 dwc->gadget.ep0 = &dep->endpoint;
1363 } else {
1364 int ret;
1365
1366 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001367 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001368 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1369 list_add_tail(&dep->endpoint.ep_list,
1370 &dwc->gadget.ep_list);
1371
1372 ret = dwc3_alloc_trb_pool(dep);
1373 if (ret) {
1374 dev_err(dwc->dev, "%s: failed to allocate TRB pool\n", dep->name);
1375 return ret;
1376 }
1377 }
1378 INIT_LIST_HEAD(&dep->request_list);
1379 INIT_LIST_HEAD(&dep->req_queued);
1380 }
1381
1382 return 0;
1383}
1384
1385static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1386{
1387 struct dwc3_ep *dep;
1388 u8 epnum;
1389
1390 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1391 dep = dwc->eps[epnum];
1392 dwc3_free_trb_pool(dep);
1393
1394 if (epnum != 0 && epnum != 1)
1395 list_del(&dep->endpoint.ep_list);
1396
1397 kfree(dep);
1398 }
1399}
1400
1401static void dwc3_gadget_release(struct device *dev)
1402{
1403 dev_dbg(dev, "%s\n", __func__);
1404}
1405
1406/* -------------------------------------------------------------------------- */
1407static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1408 const struct dwc3_event_depevt *event, int status)
1409{
1410 struct dwc3_request *req;
1411 struct dwc3_trb trb;
1412 unsigned int count;
1413 unsigned int s_pkt = 0;
1414
1415 do {
1416 req = next_request(&dep->req_queued);
1417 if (!req)
1418 break;
1419
1420 dwc3_trb_to_nat(req->trb, &trb);
1421
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001422 if (trb.hwo && status != -ESHUTDOWN)
1423 /*
1424 * We continue despite the error. There is not much we
1425 * can do. If we don't clean in up we loop for ever. If
1426 * we skip the TRB than it gets overwritten reused after
1427 * a while since we use them in a ring buffer. a BUG()
1428 * would help. Lets hope that if this occures, someone
1429 * fixes the root cause instead of looking away :)
1430 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001431 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1432 dep->name, req->trb);
Felipe Balbi72246da2011-08-19 18:10:58 +03001433 count = trb.length;
1434
1435 if (dep->direction) {
1436 if (count) {
1437 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1438 dep->name);
1439 status = -ECONNRESET;
1440 }
1441 } else {
1442 if (count && (event->status & DEPEVT_STATUS_SHORT))
1443 s_pkt = 1;
1444 }
1445
1446 /*
1447 * We assume here we will always receive the entire data block
1448 * which we should receive. Meaning, if we program RX to
1449 * receive 4K but we receive only 2K, we assume that's all we
1450 * should receive and we simply bounce the request back to the
1451 * gadget driver for further processing.
1452 */
1453 req->request.actual += req->request.length - count;
1454 dwc3_gadget_giveback(dep, req, status);
1455 if (s_pkt)
1456 break;
1457 if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
1458 break;
1459 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1460 break;
1461 } while (1);
1462
1463 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1464 return 0;
1465 return 1;
1466}
1467
1468static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1469 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1470 int start_new)
1471{
1472 unsigned status = 0;
1473 int clean_busy;
1474
1475 if (event->status & DEPEVT_STATUS_BUSERR)
1476 status = -ECONNRESET;
1477
1478 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001479 if (clean_busy) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001480 dep->flags &= ~DWC3_EP_BUSY;
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001481 dep->res_trans_idx = 0;
1482 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001483}
1484
1485static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1486 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1487{
1488 u32 uf;
1489
1490 if (list_empty(&dep->request_list)) {
1491 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1492 dep->name);
1493 return;
1494 }
1495
1496 if (event->parameters) {
1497 u32 mask;
1498
1499 mask = ~(dep->interval - 1);
1500 uf = event->parameters & mask;
1501 /* 4 micro frames in the future */
1502 uf += dep->interval * 4;
1503 } else {
1504 uf = 0;
1505 }
1506
1507 __dwc3_gadget_kick_transfer(dep, uf, 1);
1508}
1509
1510static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1511 const struct dwc3_event_depevt *event)
1512{
1513 struct dwc3 *dwc = dep->dwc;
1514 struct dwc3_event_depevt mod_ev = *event;
1515
1516 /*
1517 * We were asked to remove one requests. It is possible that this
1518 * request and a few other were started together and have the same
1519 * transfer index. Since we stopped the complete endpoint we don't
1520 * know how many requests were already completed (and not yet)
1521 * reported and how could be done (later). We purge them all until
1522 * the end of the list.
1523 */
1524 mod_ev.status = DEPEVT_STATUS_LST;
1525 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1526 dep->flags &= ~DWC3_EP_BUSY;
1527 /* pending requets are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001528}
1529
1530static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1531 const struct dwc3_event_depevt *event)
1532{
1533 u32 param = event->parameters;
1534 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1535
1536 switch (cmd_type) {
1537 case DWC3_DEPCMD_ENDTRANSFER:
1538 dwc3_process_ep_cmd_complete(dep, event);
1539 break;
1540 case DWC3_DEPCMD_STARTTRANSFER:
1541 dep->res_trans_idx = param & 0x7f;
1542 break;
1543 default:
1544 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1545 __func__, cmd_type);
1546 break;
1547 };
1548}
1549
1550static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1551 const struct dwc3_event_depevt *event)
1552{
1553 struct dwc3_ep *dep;
1554 u8 epnum = event->endpoint_number;
1555
1556 dep = dwc->eps[epnum];
1557
1558 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1559 dwc3_ep_event_string(event->endpoint_event));
1560
1561 if (epnum == 0 || epnum == 1) {
1562 dwc3_ep0_interrupt(dwc, event);
1563 return;
1564 }
1565
1566 switch (event->endpoint_event) {
1567 case DWC3_DEPEVT_XFERCOMPLETE:
1568 if (usb_endpoint_xfer_isoc(dep->desc)) {
1569 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1570 dep->name);
1571 return;
1572 }
1573
1574 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1575 break;
1576 case DWC3_DEPEVT_XFERINPROGRESS:
1577 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1578 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1579 dep->name);
1580 return;
1581 }
1582
1583 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1584 break;
1585 case DWC3_DEPEVT_XFERNOTREADY:
1586 if (usb_endpoint_xfer_isoc(dep->desc)) {
1587 dwc3_gadget_start_isoc(dwc, dep, event);
1588 } else {
1589 int ret;
1590
1591 dev_vdbg(dwc->dev, "%s: reason %s\n",
1592 dep->name, event->status
1593 ? "Transfer Active"
1594 : "Transfer Not Active");
1595
1596 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1597 if (!ret || ret == -EBUSY)
1598 return;
1599
1600 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1601 dep->name);
1602 }
1603
1604 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001605 case DWC3_DEPEVT_STREAMEVT:
1606 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1607 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1608 dep->name);
1609 return;
1610 }
1611
1612 switch (event->status) {
1613 case DEPEVT_STREAMEVT_FOUND:
1614 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1615 event->parameters);
1616
1617 break;
1618 case DEPEVT_STREAMEVT_NOTFOUND:
1619 /* FALLTHROUGH */
1620 default:
1621 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1622 }
1623 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001624 case DWC3_DEPEVT_RXTXFIFOEVT:
1625 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1626 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001627 case DWC3_DEPEVT_EPCMDCMPLT:
1628 dwc3_ep_cmd_compl(dep, event);
1629 break;
1630 }
1631}
1632
1633static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1634{
1635 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1636 spin_unlock(&dwc->lock);
1637 dwc->gadget_driver->disconnect(&dwc->gadget);
1638 spin_lock(&dwc->lock);
1639 }
1640}
1641
1642static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1643{
1644 struct dwc3_ep *dep;
1645 struct dwc3_gadget_ep_cmd_params params;
1646 u32 cmd;
1647 int ret;
1648
1649 dep = dwc->eps[epnum];
1650
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001651 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001652 if (dep->res_trans_idx) {
1653 cmd = DWC3_DEPCMD_ENDTRANSFER;
1654 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1655 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1656 memset(&params, 0, sizeof(params));
1657 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1658 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001659 dep->res_trans_idx = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001660 }
1661}
1662
1663static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1664{
1665 u32 epnum;
1666
1667 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1668 struct dwc3_ep *dep;
1669
1670 dep = dwc->eps[epnum];
1671 if (!(dep->flags & DWC3_EP_ENABLED))
1672 continue;
1673
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001674 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001675 }
1676}
1677
1678static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1679{
1680 u32 epnum;
1681
1682 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1683 struct dwc3_ep *dep;
1684 struct dwc3_gadget_ep_cmd_params params;
1685 int ret;
1686
1687 dep = dwc->eps[epnum];
1688
1689 if (!(dep->flags & DWC3_EP_STALL))
1690 continue;
1691
1692 dep->flags &= ~DWC3_EP_STALL;
1693
1694 memset(&params, 0, sizeof(params));
1695 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1696 DWC3_DEPCMD_CLEARSTALL, &params);
1697 WARN_ON_ONCE(ret);
1698 }
1699}
1700
1701static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1702{
1703 dev_vdbg(dwc->dev, "%s\n", __func__);
1704#if 0
1705 XXX
1706 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1707 enable it before we can disable it.
1708
1709 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1710 reg &= ~DWC3_DCTL_INITU1ENA;
1711 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1712
1713 reg &= ~DWC3_DCTL_INITU2ENA;
1714 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1715#endif
1716
1717 dwc3_stop_active_transfers(dwc);
1718 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001719 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001720
1721 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1722}
1723
1724static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1725{
1726 u32 reg;
1727
1728 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1729
1730 if (on)
1731 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1732 else
1733 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1734
1735 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1736}
1737
1738static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1739{
1740 u32 reg;
1741
1742 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1743
1744 if (on)
1745 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1746 else
1747 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1748
1749 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1750}
1751
1752static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1753{
1754 u32 reg;
1755
1756 dev_vdbg(dwc->dev, "%s\n", __func__);
1757
1758 /* Enable PHYs */
1759 dwc3_gadget_usb2_phy_power(dwc, true);
1760 dwc3_gadget_usb3_phy_power(dwc, true);
1761
1762 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1763 dwc3_disconnect_gadget(dwc);
1764
1765 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1766 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1767 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1768
1769 dwc3_stop_active_transfers(dwc);
1770 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001771 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001772
1773 /* Reset device address to zero */
1774 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1775 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1776 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001777}
1778
1779static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1780{
1781 u32 reg;
1782 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1783
1784 /*
1785 * We change the clock only at SS but I dunno why I would want to do
1786 * this. Maybe it becomes part of the power saving plan.
1787 */
1788
1789 if (speed != DWC3_DSTS_SUPERSPEED)
1790 return;
1791
1792 /*
1793 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1794 * each time on Connect Done.
1795 */
1796 if (!usb30_clock)
1797 return;
1798
1799 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1800 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1801 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1802}
1803
1804static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1805{
1806 switch (speed) {
1807 case USB_SPEED_SUPER:
1808 dwc3_gadget_usb2_phy_power(dwc, false);
1809 break;
1810 case USB_SPEED_HIGH:
1811 case USB_SPEED_FULL:
1812 case USB_SPEED_LOW:
1813 dwc3_gadget_usb3_phy_power(dwc, false);
1814 break;
1815 }
1816}
1817
1818static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1819{
1820 struct dwc3_gadget_ep_cmd_params params;
1821 struct dwc3_ep *dep;
1822 int ret;
1823 u32 reg;
1824 u8 speed;
1825
1826 dev_vdbg(dwc->dev, "%s\n", __func__);
1827
1828 memset(&params, 0x00, sizeof(params));
1829
Felipe Balbi72246da2011-08-19 18:10:58 +03001830 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1831 speed = reg & DWC3_DSTS_CONNECTSPD;
1832 dwc->speed = speed;
1833
1834 dwc3_update_ram_clk_sel(dwc, speed);
1835
1836 switch (speed) {
1837 case DWC3_DCFG_SUPERSPEED:
1838 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1839 dwc->gadget.ep0->maxpacket = 512;
1840 dwc->gadget.speed = USB_SPEED_SUPER;
1841 break;
1842 case DWC3_DCFG_HIGHSPEED:
1843 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1844 dwc->gadget.ep0->maxpacket = 64;
1845 dwc->gadget.speed = USB_SPEED_HIGH;
1846 break;
1847 case DWC3_DCFG_FULLSPEED2:
1848 case DWC3_DCFG_FULLSPEED1:
1849 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1850 dwc->gadget.ep0->maxpacket = 64;
1851 dwc->gadget.speed = USB_SPEED_FULL;
1852 break;
1853 case DWC3_DCFG_LOWSPEED:
1854 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
1855 dwc->gadget.ep0->maxpacket = 8;
1856 dwc->gadget.speed = USB_SPEED_LOW;
1857 break;
1858 }
1859
1860 /* Disable unneded PHY */
1861 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
1862
1863 dep = dwc->eps[0];
1864 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1865 if (ret) {
1866 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1867 return;
1868 }
1869
1870 dep = dwc->eps[1];
1871 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1872 if (ret) {
1873 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1874 return;
1875 }
1876
1877 /*
1878 * Configure PHY via GUSB3PIPECTLn if required.
1879 *
1880 * Update GTXFIFOSIZn
1881 *
1882 * In both cases reset values should be sufficient.
1883 */
1884}
1885
1886static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
1887{
1888 dev_vdbg(dwc->dev, "%s\n", __func__);
1889
1890 /*
1891 * TODO take core out of low power mode when that's
1892 * implemented.
1893 */
1894
1895 dwc->gadget_driver->resume(&dwc->gadget);
1896}
1897
1898static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
1899 unsigned int evtinfo)
1900{
Felipe Balbi72246da2011-08-19 18:10:58 +03001901 /* The fith bit says SuperSpeed yes or no. */
1902 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi019ac832011-09-08 21:18:47 +03001903
1904 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03001905}
1906
1907static void dwc3_gadget_interrupt(struct dwc3 *dwc,
1908 const struct dwc3_event_devt *event)
1909{
1910 switch (event->type) {
1911 case DWC3_DEVICE_EVENT_DISCONNECT:
1912 dwc3_gadget_disconnect_interrupt(dwc);
1913 break;
1914 case DWC3_DEVICE_EVENT_RESET:
1915 dwc3_gadget_reset_interrupt(dwc);
1916 break;
1917 case DWC3_DEVICE_EVENT_CONNECT_DONE:
1918 dwc3_gadget_conndone_interrupt(dwc);
1919 break;
1920 case DWC3_DEVICE_EVENT_WAKEUP:
1921 dwc3_gadget_wakeup_interrupt(dwc);
1922 break;
1923 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
1924 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
1925 break;
1926 case DWC3_DEVICE_EVENT_EOPF:
1927 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
1928 break;
1929 case DWC3_DEVICE_EVENT_SOF:
1930 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
1931 break;
1932 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
1933 dev_vdbg(dwc->dev, "Erratic Error\n");
1934 break;
1935 case DWC3_DEVICE_EVENT_CMD_CMPL:
1936 dev_vdbg(dwc->dev, "Command Complete\n");
1937 break;
1938 case DWC3_DEVICE_EVENT_OVERFLOW:
1939 dev_vdbg(dwc->dev, "Overflow\n");
1940 break;
1941 default:
1942 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
1943 }
1944}
1945
1946static void dwc3_process_event_entry(struct dwc3 *dwc,
1947 const union dwc3_event *event)
1948{
1949 /* Endpoint IRQ, handle it and return early */
1950 if (event->type.is_devspec == 0) {
1951 /* depevt */
1952 return dwc3_endpoint_interrupt(dwc, &event->depevt);
1953 }
1954
1955 switch (event->type.type) {
1956 case DWC3_EVENT_TYPE_DEV:
1957 dwc3_gadget_interrupt(dwc, &event->devt);
1958 break;
1959 /* REVISIT what to do with Carkit and I2C events ? */
1960 default:
1961 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
1962 }
1963}
1964
1965static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
1966{
1967 struct dwc3_event_buffer *evt;
1968 int left;
1969 u32 count;
1970
1971 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
1972 count &= DWC3_GEVNTCOUNT_MASK;
1973 if (!count)
1974 return IRQ_NONE;
1975
1976 evt = dwc->ev_buffs[buf];
1977 left = count;
1978
1979 while (left > 0) {
1980 union dwc3_event event;
1981
1982 memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
1983 dwc3_process_event_entry(dwc, &event);
1984 /*
1985 * XXX we wrap around correctly to the next entry as almost all
1986 * entries are 4 bytes in size. There is one entry which has 12
1987 * bytes which is a regular entry followed by 8 bytes data. ATM
1988 * I don't know how things are organized if were get next to the
1989 * a boundary so I worry about that once we try to handle that.
1990 */
1991 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
1992 left -= 4;
1993
1994 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
1995 }
1996
1997 return IRQ_HANDLED;
1998}
1999
2000static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2001{
2002 struct dwc3 *dwc = _dwc;
2003 int i;
2004 irqreturn_t ret = IRQ_NONE;
2005
2006 spin_lock(&dwc->lock);
2007
2008 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
2009 irqreturn_t status;
2010
2011 status = dwc3_process_event_buf(dwc, i);
2012 if (status == IRQ_HANDLED)
2013 ret = status;
2014 }
2015
2016 spin_unlock(&dwc->lock);
2017
2018 return ret;
2019}
2020
2021/**
2022 * dwc3_gadget_init - Initializes gadget related registers
2023 * @dwc: Pointer to out controller context structure
2024 *
2025 * Returns 0 on success otherwise negative errno.
2026 */
2027int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2028{
2029 u32 reg;
2030 int ret;
2031 int irq;
2032
2033 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2034 &dwc->ctrl_req_addr, GFP_KERNEL);
2035 if (!dwc->ctrl_req) {
2036 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2037 ret = -ENOMEM;
2038 goto err0;
2039 }
2040
2041 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2042 &dwc->ep0_trb_addr, GFP_KERNEL);
2043 if (!dwc->ep0_trb) {
2044 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2045 ret = -ENOMEM;
2046 goto err1;
2047 }
2048
2049 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
2050 sizeof(*dwc->setup_buf) * 2,
2051 &dwc->setup_buf_addr, GFP_KERNEL);
2052 if (!dwc->setup_buf) {
2053 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2054 ret = -ENOMEM;
2055 goto err2;
2056 }
2057
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002058 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2059 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2060 if (!dwc->ep0_bounce) {
2061 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2062 ret = -ENOMEM;
2063 goto err3;
2064 }
2065
Felipe Balbi72246da2011-08-19 18:10:58 +03002066 dev_set_name(&dwc->gadget.dev, "gadget");
2067
2068 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002069 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002070 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2071 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002072 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002073
2074 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2075
2076 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2077 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2078 dwc->gadget.dev.release = dwc3_gadget_release;
2079 dwc->gadget.name = "dwc3-gadget";
2080
2081 /*
2082 * REVISIT: Here we should clear all pending IRQs to be
2083 * sure we're starting from a well known location.
2084 */
2085
2086 ret = dwc3_gadget_init_endpoints(dwc);
2087 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002088 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002089
2090 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2091
2092 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2093 "dwc3", dwc);
2094 if (ret) {
2095 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2096 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002097 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002098 }
2099
2100 /* Enable all but Start and End of Frame IRQs */
2101 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2102 DWC3_DEVTEN_EVNTOVERFLOWEN |
2103 DWC3_DEVTEN_CMDCMPLTEN |
2104 DWC3_DEVTEN_ERRTICERREN |
2105 DWC3_DEVTEN_WKUPEVTEN |
2106 DWC3_DEVTEN_ULSTCNGEN |
2107 DWC3_DEVTEN_CONNECTDONEEN |
2108 DWC3_DEVTEN_USBRSTEN |
2109 DWC3_DEVTEN_DISCONNEVTEN);
2110 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2111
2112 ret = device_register(&dwc->gadget.dev);
2113 if (ret) {
2114 dev_err(dwc->dev, "failed to register gadget device\n");
2115 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002116 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002117 }
2118
2119 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2120 if (ret) {
2121 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002122 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002123 }
2124
2125 return 0;
2126
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002127err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002128 device_unregister(&dwc->gadget.dev);
2129
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002130err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002131 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2132 free_irq(irq, dwc);
2133
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002134err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002135 dwc3_gadget_free_endpoints(dwc);
2136
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002137err4:
2138 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2139 dwc->ep0_bounce_addr);
2140
Felipe Balbi72246da2011-08-19 18:10:58 +03002141err3:
2142 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2143 dwc->setup_buf, dwc->setup_buf_addr);
2144
2145err2:
2146 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2147 dwc->ep0_trb, dwc->ep0_trb_addr);
2148
2149err1:
2150 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2151 dwc->ctrl_req, dwc->ctrl_req_addr);
2152
2153err0:
2154 return ret;
2155}
2156
2157void dwc3_gadget_exit(struct dwc3 *dwc)
2158{
2159 int irq;
2160 int i;
2161
2162 usb_del_gadget_udc(&dwc->gadget);
2163 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2164
2165 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2166 free_irq(irq, dwc);
2167
2168 for (i = 0; i < ARRAY_SIZE(dwc->eps); i++)
2169 __dwc3_gadget_ep_disable(dwc->eps[i]);
2170
2171 dwc3_gadget_free_endpoints(dwc);
2172
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002173 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2174 dwc->ep0_bounce_addr);
2175
Felipe Balbi72246da2011-08-19 18:10:58 +03002176 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2177 dwc->setup_buf, dwc->setup_buf_addr);
2178
2179 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2180 dwc->ep0_trb, dwc->ep0_trb_addr);
2181
2182 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2183 dwc->ctrl_req, dwc->ctrl_req_addr);
2184
2185 device_unregister(&dwc->gadget.dev);
2186}