blob: b43c6f90b25ff1263715a8533b2ab79ca9571e13 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
5 *
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/**
58 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
59 * @dwc: pointer to our context structure
60 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
61 *
62 * Caller should take care of locking. This function will
63 * return 0 on success or -EINVAL if wrong Test Selector
64 * is passed
65 */
66int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
67{
68 u32 reg;
69
70 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
71 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
72
73 switch (mode) {
74 case TEST_J:
75 case TEST_K:
76 case TEST_SE0_NAK:
77 case TEST_PACKET:
78 case TEST_FORCE_EN:
79 reg |= mode << 1;
80 break;
81 default:
82 return -EINVAL;
83 }
84
85 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
86
87 return 0;
88}
89
90/**
91 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
92 * @dwc: pointer to our context structure
93 * @state: the state to put link into
94 *
95 * Caller should take care of locking. This function will
96 * return 0 on success or -ETIMEDOUT.
97 */
98int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
99{
100 int retries = 10000;
101 u32 reg;
102
103 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
104 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
105
106 /* set requested state */
107 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
108 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
109
110 /* wait for a change in DSTS */
111 while (--retries) {
112 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
113
114 if (DWC3_DSTS_USBLNKST(reg) == state)
115 return 0;
116
117 udelay(5);
118 }
119
120 dev_vdbg(dwc->dev, "link state change request timed out\n");
121
122 return -ETIMEDOUT;
123}
124
125/**
126 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
127 * @dwc: pointer to our context structure
128 *
129 * This function will a best effort FIFO allocation in order
130 * to improve FIFO usage and throughput, while still allowing
131 * us to enable as many endpoints as possible.
132 *
133 * Keep in mind that this operation will be highly dependent
134 * on the configured size for RAM1 - which contains TxFifo -,
135 * the amount of endpoints enabled on coreConsultant tool, and
136 * the width of the Master Bus.
137 *
138 * In the ideal world, we would always be able to satisfy the
139 * following equation:
140 *
141 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
142 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
143 *
144 * Unfortunately, due to many variables that's not always the case.
145 */
146int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
147{
148 int last_fifo_depth = 0;
149 int ram1_depth;
150 int fifo_size;
151 int mdwidth;
152 int num;
153
154 if (!dwc->needs_fifo_resize)
155 return 0;
156
157 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
158 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
159
160 /* MDWIDTH is represented in bits, we need it in bytes */
161 mdwidth >>= 3;
162
163 /*
164 * FIXME For now we will only allocate 1 wMaxPacketSize space
165 * for each enabled endpoint, later patches will come to
166 * improve this algorithm so that we better use the internal
167 * FIFO space
168 */
169 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
170 struct dwc3_ep *dep = dwc->eps[num];
171 int fifo_number = dep->number >> 1;
172 int mult = 1;
173 int tmp;
174
175 if (!(dep->number & 1))
176 continue;
177
178 if (!(dep->flags & DWC3_EP_ENABLED))
179 continue;
180
181 if (usb_endpoint_xfer_bulk(dep->desc)
182 || usb_endpoint_xfer_isoc(dep->desc))
183 mult = 3;
184
185 /*
186 * REVISIT: the following assumes we will always have enough
187 * space available on the FIFO RAM for all possible use cases.
188 * Make sure that's true somehow and change FIFO allocation
189 * accordingly.
190 *
191 * If we have Bulk or Isochronous endpoints, we want
192 * them to be able to be very, very fast. So we're giving
193 * those endpoints a fifo_size which is enough for 3 full
194 * packets
195 */
196 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
197 tmp += mdwidth;
198
199 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
200
201 fifo_size |= (last_fifo_depth << 16);
202
203 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
204 dep->name, last_fifo_depth, fifo_size & 0xffff);
205
206 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
207 fifo_size);
208
209 last_fifo_depth += (fifo_size & 0xffff);
210 }
211
212 return 0;
213}
214
215void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
216 int status)
217{
218 struct dwc3 *dwc = dep->dwc;
219
220 if (req->queued) {
221 if (req->request.num_mapped_sgs)
222 dep->busy_slot += req->request.num_mapped_sgs;
223 else
224 dep->busy_slot++;
225
226 /*
227 * Skip LINK TRB. We can't use req->trb and check for
228 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
229 * completed (not the LINK TRB).
230 */
231 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
232 usb_endpoint_xfer_isoc(dep->desc))
233 dep->busy_slot++;
234 }
235 list_del(&req->list);
236 req->trb = NULL;
237
238 if (req->request.status == -EINPROGRESS)
239 req->request.status = status;
240
241 if (dwc->ep0_bounced && dep->number == 0)
242 dwc->ep0_bounced = false;
243 else
244 usb_gadget_unmap_request(&dwc->gadget, &req->request,
245 req->direction);
246
247 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
248 req, dep->name, req->request.actual,
249 req->request.length, status);
250
251 spin_unlock(&dwc->lock);
252 req->request.complete(&dep->endpoint, &req->request);
253 spin_lock(&dwc->lock);
254}
255
256static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
257{
258 switch (cmd) {
259 case DWC3_DEPCMD_DEPSTARTCFG:
260 return "Start New Configuration";
261 case DWC3_DEPCMD_ENDTRANSFER:
262 return "End Transfer";
263 case DWC3_DEPCMD_UPDATETRANSFER:
264 return "Update Transfer";
265 case DWC3_DEPCMD_STARTTRANSFER:
266 return "Start Transfer";
267 case DWC3_DEPCMD_CLEARSTALL:
268 return "Clear Stall";
269 case DWC3_DEPCMD_SETSTALL:
270 return "Set Stall";
271 case DWC3_DEPCMD_GETSEQNUMBER:
272 return "Get Data Sequence Number";
273 case DWC3_DEPCMD_SETTRANSFRESOURCE:
274 return "Set Endpoint Transfer Resource";
275 case DWC3_DEPCMD_SETEPCONFIG:
276 return "Set Endpoint Configuration";
277 default:
278 return "UNKNOWN command";
279 }
280}
281
282int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
283 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
284{
285 struct dwc3_ep *dep = dwc->eps[ep];
286 u32 timeout = 500;
287 u32 reg;
288
289 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
290 dep->name,
291 dwc3_gadget_ep_cmd_string(cmd), params->param0,
292 params->param1, params->param2);
293
294 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
295 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
296 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
297
298 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
299 do {
300 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
301 if (!(reg & DWC3_DEPCMD_CMDACT)) {
302 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
303 DWC3_DEPCMD_STATUS(reg));
304 if (DWC3_DEPCMD_STATUS(reg))
305 return -EINVAL;
306 return 0;
307 }
308
309 /*
310 * We can't sleep here, because it is also called from
311 * interrupt context.
312 */
313 timeout--;
314 if (!timeout)
315 return -ETIMEDOUT;
316
317 udelay(1);
318 } while (1);
319}
320
321static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
322 struct dwc3_trb *trb)
323{
324 u32 offset = (char *) trb - (char *) dep->trb_pool;
325
326 return dep->trb_pool_dma + offset;
327}
328
329static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
330{
331 struct dwc3 *dwc = dep->dwc;
332
333 if (dep->trb_pool)
334 return 0;
335
336 if (dep->number == 0 || dep->number == 1)
337 return 0;
338
339 dep->trb_pool = dma_alloc_coherent(dwc->dev,
340 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
341 &dep->trb_pool_dma, GFP_KERNEL);
342 if (!dep->trb_pool) {
343 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
344 dep->name);
345 return -ENOMEM;
346 }
347
348 return 0;
349}
350
351static void dwc3_free_trb_pool(struct dwc3_ep *dep)
352{
353 struct dwc3 *dwc = dep->dwc;
354
355 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
356 dep->trb_pool, dep->trb_pool_dma);
357
358 dep->trb_pool = NULL;
359 dep->trb_pool_dma = 0;
360}
361
362static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
363{
364 struct dwc3_gadget_ep_cmd_params params;
365 u32 cmd;
366
367 memset(&params, 0x00, sizeof(params));
368
369 if (dep->number != 1) {
370 cmd = DWC3_DEPCMD_DEPSTARTCFG;
371 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
372 if (dep->number > 1) {
373 if (dwc->start_config_issued)
374 return 0;
375 dwc->start_config_issued = true;
376 cmd |= DWC3_DEPCMD_PARAM(2);
377 }
378
379 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
380 }
381
382 return 0;
383}
384
385static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
386 const struct usb_endpoint_descriptor *desc,
387 const struct usb_ss_ep_comp_descriptor *comp_desc)
388{
389 struct dwc3_gadget_ep_cmd_params params;
390
391 memset(&params, 0x00, sizeof(params));
392
393 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
394 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
395 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
396
397 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
398 | DWC3_DEPCFG_XFER_NOT_READY_EN;
399
400 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
401 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
402 | DWC3_DEPCFG_STREAM_EVENT_EN;
403 dep->stream_capable = true;
404 }
405
406 if (usb_endpoint_xfer_isoc(desc))
407 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
408
409 /*
410 * We are doing 1:1 mapping for endpoints, meaning
411 * Physical Endpoints 2 maps to Logical Endpoint 2 and
412 * so on. We consider the direction bit as part of the physical
413 * endpoint number. So USB endpoint 0x81 is 0x03.
414 */
415 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
416
417 /*
418 * We must use the lower 16 TX FIFOs even though
419 * HW might have more
420 */
421 if (dep->direction)
422 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
423
424 if (desc->bInterval) {
425 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
426 dep->interval = 1 << (desc->bInterval - 1);
427 }
428
429 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
430 DWC3_DEPCMD_SETEPCONFIG, &params);
431}
432
433static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
434{
435 struct dwc3_gadget_ep_cmd_params params;
436
437 memset(&params, 0x00, sizeof(params));
438
439 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
440
441 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
442 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
443}
444
445/**
446 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
447 * @dep: endpoint to be initialized
448 * @desc: USB Endpoint Descriptor
449 *
450 * Caller should take care of locking
451 */
452static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
453 const struct usb_endpoint_descriptor *desc,
454 const struct usb_ss_ep_comp_descriptor *comp_desc)
455{
456 struct dwc3 *dwc = dep->dwc;
457 u32 reg;
458 int ret = -ENOMEM;
459
460 if (!(dep->flags & DWC3_EP_ENABLED)) {
461 ret = dwc3_gadget_start_config(dwc, dep);
462 if (ret)
463 return ret;
464 }
465
466 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
467 if (ret)
468 return ret;
469
470 if (!(dep->flags & DWC3_EP_ENABLED)) {
471 struct dwc3_trb *trb_st_hw;
472 struct dwc3_trb *trb_link;
473
474 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
475 if (ret)
476 return ret;
477
478 dep->desc = desc;
479 dep->comp_desc = comp_desc;
480 dep->type = usb_endpoint_type(desc);
481 dep->flags |= DWC3_EP_ENABLED;
482
483 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
484 reg |= DWC3_DALEPENA_EP(dep->number);
485 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
486
487 if (!usb_endpoint_xfer_isoc(desc))
488 return 0;
489
490 /* Link TRB for ISOC. The HWO bit is never reset */
491 trb_st_hw = &dep->trb_pool[0];
492
493 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
494 memset(trb_link, 0, sizeof(*trb_link));
495
496 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
497 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
498 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
499 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
500 }
501
502 return 0;
503}
504
505static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
506static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
507{
508 struct dwc3_request *req;
509
510 if (!list_empty(&dep->req_queued))
511 dwc3_stop_active_transfer(dwc, dep->number);
512
513 while (!list_empty(&dep->request_list)) {
514 req = next_request(&dep->request_list);
515
516 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
517 }
518}
519
520/**
521 * __dwc3_gadget_ep_disable - Disables a HW endpoint
522 * @dep: the endpoint to disable
523 *
524 * This function also removes requests which are currently processed ny the
525 * hardware and those which are not yet scheduled.
526 * Caller should take care of locking.
527 */
528static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
529{
530 struct dwc3 *dwc = dep->dwc;
531 u32 reg;
532
533 dwc3_remove_requests(dwc, dep);
534
535 /* make sure HW endpoint isn't stalled */
536 if (dep->flags & DWC3_EP_STALL)
537 __dwc3_gadget_ep_set_halt(dep, 0, false);
538
539 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
540 reg &= ~DWC3_DALEPENA_EP(dep->number);
541 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
542
543 dep->stream_capable = false;
544 dep->desc = NULL;
545 dep->endpoint.desc = NULL;
546 dep->comp_desc = NULL;
547 dep->type = 0;
548 dep->flags = 0;
549
550 return 0;
551}
552
553/* -------------------------------------------------------------------------- */
554
555static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
556 const struct usb_endpoint_descriptor *desc)
557{
558 return -EINVAL;
559}
560
561static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
562{
563 return -EINVAL;
564}
565
566/* -------------------------------------------------------------------------- */
567
568static int dwc3_gadget_ep_enable(struct usb_ep *ep,
569 const struct usb_endpoint_descriptor *desc)
570{
571 struct dwc3_ep *dep;
572 struct dwc3 *dwc;
573 unsigned long flags;
574 int ret;
575
576 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
577 pr_debug("dwc3: invalid parameters\n");
578 return -EINVAL;
579 }
580
581 if (!desc->wMaxPacketSize) {
582 pr_debug("dwc3: missing wMaxPacketSize\n");
583 return -EINVAL;
584 }
585
586 dep = to_dwc3_ep(ep);
587 dwc = dep->dwc;
588
589 switch (usb_endpoint_type(desc)) {
590 case USB_ENDPOINT_XFER_CONTROL:
591 strlcat(dep->name, "-control", sizeof(dep->name));
592 break;
593 case USB_ENDPOINT_XFER_ISOC:
594 strlcat(dep->name, "-isoc", sizeof(dep->name));
595 break;
596 case USB_ENDPOINT_XFER_BULK:
597 strlcat(dep->name, "-bulk", sizeof(dep->name));
598 break;
599 case USB_ENDPOINT_XFER_INT:
600 strlcat(dep->name, "-int", sizeof(dep->name));
601 break;
602 default:
603 dev_err(dwc->dev, "invalid endpoint transfer type\n");
604 }
605
606 if (dep->flags & DWC3_EP_ENABLED) {
607 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
608 dep->name);
609 return 0;
610 }
611
612 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
613
614 spin_lock_irqsave(&dwc->lock, flags);
615 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
616 spin_unlock_irqrestore(&dwc->lock, flags);
617
618 return ret;
619}
620
621static int dwc3_gadget_ep_disable(struct usb_ep *ep)
622{
623 struct dwc3_ep *dep;
624 struct dwc3 *dwc;
625 unsigned long flags;
626 int ret;
627
628 if (!ep) {
629 pr_debug("dwc3: invalid parameters\n");
630 return -EINVAL;
631 }
632
633 dep = to_dwc3_ep(ep);
634 dwc = dep->dwc;
635
636 if (!(dep->flags & DWC3_EP_ENABLED)) {
637 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
638 dep->name);
639 return 0;
640 }
641
642 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
643 dep->number >> 1,
644 (dep->number & 1) ? "in" : "out");
645
646 spin_lock_irqsave(&dwc->lock, flags);
647 ret = __dwc3_gadget_ep_disable(dep);
648 spin_unlock_irqrestore(&dwc->lock, flags);
649
650 return ret;
651}
652
653static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
654 gfp_t gfp_flags)
655{
656 struct dwc3_request *req;
657 struct dwc3_ep *dep = to_dwc3_ep(ep);
658 struct dwc3 *dwc = dep->dwc;
659
660 req = kzalloc(sizeof(*req), gfp_flags);
661 if (!req) {
662 dev_err(dwc->dev, "not enough memory\n");
663 return NULL;
664 }
665
666 req->epnum = dep->number;
667 req->dep = dep;
668
669 return &req->request;
670}
671
672static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
673 struct usb_request *request)
674{
675 struct dwc3_request *req = to_dwc3_request(request);
676
677 kfree(req);
678}
679
680/**
681 * dwc3_prepare_one_trb - setup one TRB from one request
682 * @dep: endpoint for which this request is prepared
683 * @req: dwc3_request pointer
684 */
685static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
686 struct dwc3_request *req, dma_addr_t dma,
687 unsigned length, unsigned last, unsigned chain)
688{
689 struct dwc3 *dwc = dep->dwc;
690 struct dwc3_trb *trb;
691
692 unsigned int cur_slot;
693
694 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
695 dep->name, req, (unsigned long long) dma,
696 length, last ? " last" : "",
697 chain ? " chain" : "");
698
699 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
700 cur_slot = dep->free_slot;
701 dep->free_slot++;
702
703 /* Skip the LINK-TRB on ISOC */
704 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
705 usb_endpoint_xfer_isoc(dep->desc))
706 return;
707
708 if (!req->trb) {
709 dwc3_gadget_move_request_queued(req);
710 req->trb = trb;
711 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
712 }
713
714 trb->size = DWC3_TRB_SIZE_LENGTH(length);
715 trb->bpl = lower_32_bits(dma);
716 trb->bph = upper_32_bits(dma);
717
718 switch (usb_endpoint_type(dep->desc)) {
719 case USB_ENDPOINT_XFER_CONTROL:
720 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
721 break;
722
723 case USB_ENDPOINT_XFER_ISOC:
724 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
725
726 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
727 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
728 trb->ctrl |= DWC3_TRB_CTRL_IOC;
729 break;
730
731 case USB_ENDPOINT_XFER_BULK:
732 case USB_ENDPOINT_XFER_INT:
733 trb->ctrl = DWC3_TRBCTL_NORMAL;
734 break;
735 default:
736 /*
737 * This is only possible with faulty memory because we
738 * checked it already :)
739 */
740 BUG();
741 }
742
743 if (usb_endpoint_xfer_isoc(dep->desc)) {
744 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
745 trb->ctrl |= DWC3_TRB_CTRL_CSP;
746 } else {
747 if (chain)
748 trb->ctrl |= DWC3_TRB_CTRL_CHN;
749
750 if (last)
751 trb->ctrl |= DWC3_TRB_CTRL_LST;
752 }
753
754 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
755 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
756
757 trb->ctrl |= DWC3_TRB_CTRL_HWO;
758}
759
760/*
761 * dwc3_prepare_trbs - setup TRBs from requests
762 * @dep: endpoint for which requests are being prepared
763 * @starting: true if the endpoint is idle and no requests are queued.
764 *
765 * The function goes through the requests list and sets up TRBs for the
766 * transfers. The function returns once there are no more TRBs available or
767 * it runs out of requests.
768 */
769static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
770{
771 struct dwc3_request *req, *n;
772 u32 trbs_left;
773 u32 max;
774 unsigned int last_one = 0;
775
776 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
777
778 /* the first request must not be queued */
779 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
780
781 /* Can't wrap around on a non-isoc EP since there's no link TRB */
782 if (!usb_endpoint_xfer_isoc(dep->desc)) {
783 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
784 if (trbs_left > max)
785 trbs_left = max;
786 }
787
788 /*
789 * If busy & slot are equal than it is either full or empty. If we are
790 * starting to process requests then we are empty. Otherwise we are
791 * full and don't do anything
792 */
793 if (!trbs_left) {
794 if (!starting)
795 return;
796 trbs_left = DWC3_TRB_NUM;
797 /*
798 * In case we start from scratch, we queue the ISOC requests
799 * starting from slot 1. This is done because we use ring
800 * buffer and have no LST bit to stop us. Instead, we place
801 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
802 * after the first request so we start at slot 1 and have
803 * 7 requests proceed before we hit the first IOC.
804 * Other transfer types don't use the ring buffer and are
805 * processed from the first TRB until the last one. Since we
806 * don't wrap around we have to start at the beginning.
807 */
808 if (usb_endpoint_xfer_isoc(dep->desc)) {
809 dep->busy_slot = 1;
810 dep->free_slot = 1;
811 } else {
812 dep->busy_slot = 0;
813 dep->free_slot = 0;
814 }
815 }
816
817 /* The last TRB is a link TRB, not used for xfer */
818 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
819 return;
820
821 list_for_each_entry_safe(req, n, &dep->request_list, list) {
822 unsigned length;
823 dma_addr_t dma;
824
825 if (req->request.num_mapped_sgs > 0) {
826 struct usb_request *request = &req->request;
827 struct scatterlist *sg = request->sg;
828 struct scatterlist *s;
829 int i;
830
831 for_each_sg(sg, s, request->num_mapped_sgs, i) {
832 unsigned chain = true;
833
834 length = sg_dma_len(s);
835 dma = sg_dma_address(s);
836
837 if (i == (request->num_mapped_sgs - 1) ||
838 sg_is_last(s)) {
839 last_one = true;
840 chain = false;
841 }
842
843 trbs_left--;
844 if (!trbs_left)
845 last_one = true;
846
847 if (last_one)
848 chain = false;
849
850 dwc3_prepare_one_trb(dep, req, dma, length,
851 last_one, chain);
852
853 if (last_one)
854 break;
855 }
856
857 if (last_one)
858 break;
859 } else {
860 dma = req->request.dma;
861 length = req->request.length;
862 trbs_left--;
863
864 if (!trbs_left)
865 last_one = 1;
866
867 /* Is this the last request? */
868 if (list_is_last(&req->list, &dep->request_list))
869 last_one = 1;
870
871 dwc3_prepare_one_trb(dep, req, dma, length,
872 last_one, false);
873
874 if (last_one)
875 break;
876 }
877 }
878}
879
880static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
881 int start_new)
882{
883 struct dwc3_gadget_ep_cmd_params params;
884 struct dwc3_request *req;
885 struct dwc3 *dwc = dep->dwc;
886 int ret;
887 u32 cmd;
888
889 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
890 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
891 return -EBUSY;
892 }
893 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
894
895 /*
896 * If we are getting here after a short-out-packet we don't enqueue any
897 * new requests as we try to set the IOC bit only on the last request.
898 */
899 if (start_new) {
900 if (list_empty(&dep->req_queued))
901 dwc3_prepare_trbs(dep, start_new);
902
903 /* req points to the first request which will be sent */
904 req = next_request(&dep->req_queued);
905 } else {
906 dwc3_prepare_trbs(dep, start_new);
907
908 /*
909 * req points to the first request where HWO changed from 0 to 1
910 */
911 req = next_request(&dep->req_queued);
912 }
913 if (!req) {
914 dep->flags |= DWC3_EP_PENDING_REQUEST;
915 return 0;
916 }
917
918 memset(&params, 0, sizeof(params));
919 params.param0 = upper_32_bits(req->trb_dma);
920 params.param1 = lower_32_bits(req->trb_dma);
921
922 if (start_new)
923 cmd = DWC3_DEPCMD_STARTTRANSFER;
924 else
925 cmd = DWC3_DEPCMD_UPDATETRANSFER;
926
927 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
928 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
929 if (ret < 0) {
930 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
931
932 /*
933 * FIXME we need to iterate over the list of requests
934 * here and stop, unmap, free and del each of the linked
935 * requests instead of what we do now.
936 */
937 usb_gadget_unmap_request(&dwc->gadget, &req->request,
938 req->direction);
939 list_del(&req->list);
940 return ret;
941 }
942
943 dep->flags |= DWC3_EP_BUSY;
944 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
945 dep->number);
946
947 WARN_ON_ONCE(!dep->res_trans_idx);
948
949 return 0;
950}
951
952static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
953{
954 struct dwc3 *dwc = dep->dwc;
955 int ret;
956
957 req->request.actual = 0;
958 req->request.status = -EINPROGRESS;
959 req->direction = dep->direction;
960 req->epnum = dep->number;
961
962 /*
963 * We only add to our list of requests now and
964 * start consuming the list once we get XferNotReady
965 * IRQ.
966 *
967 * That way, we avoid doing anything that we don't need
968 * to do now and defer it until the point we receive a
969 * particular token from the Host side.
970 *
971 * This will also avoid Host cancelling URBs due to too
972 * many NAKs.
973 */
974 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
975 dep->direction);
976 if (ret)
977 return ret;
978
979 list_add_tail(&req->list, &dep->request_list);
980
981 /*
982 * There is one special case: XferNotReady with
983 * empty list of requests. We need to kick the
984 * transfer here in that situation, otherwise
985 * we will be NAKing forever.
986 *
987 * If we get XferNotReady before gadget driver
988 * has a chance to queue a request, we will ACK
989 * the IRQ but won't be able to receive the data
990 * until the next request is queued. The following
991 * code is handling exactly that.
992 */
993 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
994 int ret;
995 int start_trans;
996
997 start_trans = 1;
998 if (usb_endpoint_xfer_isoc(dep->desc) &&
999 (dep->flags & DWC3_EP_BUSY))
1000 start_trans = 0;
1001
1002 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
1003 if (ret && ret != -EBUSY) {
1004 struct dwc3 *dwc = dep->dwc;
1005
1006 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1007 dep->name);
1008 }
1009 };
1010
1011 return 0;
1012}
1013
1014static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1015 gfp_t gfp_flags)
1016{
1017 struct dwc3_request *req = to_dwc3_request(request);
1018 struct dwc3_ep *dep = to_dwc3_ep(ep);
1019 struct dwc3 *dwc = dep->dwc;
1020
1021 unsigned long flags;
1022
1023 int ret;
1024
1025 if (!dep->desc) {
1026 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1027 request, ep->name);
1028 return -ESHUTDOWN;
1029 }
1030
1031 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1032 request, ep->name, request->length);
1033
1034 spin_lock_irqsave(&dwc->lock, flags);
1035 ret = __dwc3_gadget_ep_queue(dep, req);
1036 spin_unlock_irqrestore(&dwc->lock, flags);
1037
1038 return ret;
1039}
1040
1041static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1042 struct usb_request *request)
1043{
1044 struct dwc3_request *req = to_dwc3_request(request);
1045 struct dwc3_request *r = NULL;
1046
1047 struct dwc3_ep *dep = to_dwc3_ep(ep);
1048 struct dwc3 *dwc = dep->dwc;
1049
1050 unsigned long flags;
1051 int ret = 0;
1052
1053 spin_lock_irqsave(&dwc->lock, flags);
1054
1055 list_for_each_entry(r, &dep->request_list, list) {
1056 if (r == req)
1057 break;
1058 }
1059
1060 if (r != req) {
1061 list_for_each_entry(r, &dep->req_queued, list) {
1062 if (r == req)
1063 break;
1064 }
1065 if (r == req) {
1066 /* wait until it is processed */
1067 dwc3_stop_active_transfer(dwc, dep->number);
1068 goto out0;
1069 }
1070 dev_err(dwc->dev, "request %p was not queued to %s\n",
1071 request, ep->name);
1072 ret = -EINVAL;
1073 goto out0;
1074 }
1075
1076 /* giveback the request */
1077 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1078
1079out0:
1080 spin_unlock_irqrestore(&dwc->lock, flags);
1081
1082 return ret;
1083}
1084
1085int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1086{
1087 struct dwc3_gadget_ep_cmd_params params;
1088 struct dwc3 *dwc = dep->dwc;
1089 int ret;
1090
1091 memset(&params, 0x00, sizeof(params));
1092
1093 if (value) {
1094 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
1095 (!list_empty(&dep->req_queued) ||
1096 !list_empty(&dep->request_list)))) {
1097 dev_dbg(dwc->dev, "%s: pending request, cannot halt\n",
1098 dep->name);
1099 return -EAGAIN;
1100 }
1101
1102 if (dep->number == 0 || dep->number == 1) {
1103 /*
1104 * Whenever EP0 is stalled, we will restart
1105 * the state machine, thus moving back to
1106 * Setup Phase
1107 */
1108 dwc->ep0state = EP0_SETUP_PHASE;
1109 }
1110
1111 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1112 DWC3_DEPCMD_SETSTALL, &params);
1113 if (ret)
1114 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1115 value ? "set" : "clear",
1116 dep->name);
1117 else
1118 dep->flags |= DWC3_EP_STALL;
1119 } else {
1120 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1121 DWC3_DEPCMD_CLEARSTALL, &params);
1122 if (ret)
1123 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1124 value ? "set" : "clear",
1125 dep->name);
1126 else
1127 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1128 }
1129
1130 return ret;
1131}
1132
1133static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1134{
1135 struct dwc3_ep *dep = to_dwc3_ep(ep);
1136 struct dwc3 *dwc = dep->dwc;
1137
1138 unsigned long flags;
1139
1140 int ret;
1141
1142 spin_lock_irqsave(&dwc->lock, flags);
1143
1144 if (usb_endpoint_xfer_isoc(dep->desc)) {
1145 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1146 ret = -EINVAL;
1147 goto out;
1148 }
1149
1150 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1151out:
1152 spin_unlock_irqrestore(&dwc->lock, flags);
1153
1154 return ret;
1155}
1156
1157static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1158{
1159 struct dwc3_ep *dep = to_dwc3_ep(ep);
1160 struct dwc3 *dwc = dep->dwc;
1161 unsigned long flags;
1162
1163 spin_lock_irqsave(&dwc->lock, flags);
1164 dep->flags |= DWC3_EP_WEDGE;
1165 spin_unlock_irqrestore(&dwc->lock, flags);
1166
1167 return dwc3_gadget_ep_set_halt(ep, 1);
1168}
1169
1170/* -------------------------------------------------------------------------- */
1171
1172static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1173 .bLength = USB_DT_ENDPOINT_SIZE,
1174 .bDescriptorType = USB_DT_ENDPOINT,
1175 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1176};
1177
1178static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1179 .enable = dwc3_gadget_ep0_enable,
1180 .disable = dwc3_gadget_ep0_disable,
1181 .alloc_request = dwc3_gadget_ep_alloc_request,
1182 .free_request = dwc3_gadget_ep_free_request,
1183 .queue = dwc3_gadget_ep0_queue,
1184 .dequeue = dwc3_gadget_ep_dequeue,
1185 .set_halt = dwc3_gadget_ep_set_halt,
1186 .set_wedge = dwc3_gadget_ep_set_wedge,
1187};
1188
1189static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1190 .enable = dwc3_gadget_ep_enable,
1191 .disable = dwc3_gadget_ep_disable,
1192 .alloc_request = dwc3_gadget_ep_alloc_request,
1193 .free_request = dwc3_gadget_ep_free_request,
1194 .queue = dwc3_gadget_ep_queue,
1195 .dequeue = dwc3_gadget_ep_dequeue,
1196 .set_halt = dwc3_gadget_ep_set_halt,
1197 .set_wedge = dwc3_gadget_ep_set_wedge,
1198};
1199
1200/* -------------------------------------------------------------------------- */
1201
1202static int dwc3_gadget_get_frame(struct usb_gadget *g)
1203{
1204 struct dwc3 *dwc = gadget_to_dwc(g);
1205 u32 reg;
1206
1207 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1208 return DWC3_DSTS_SOFFN(reg);
1209}
1210
1211static int dwc3_gadget_wakeup(struct usb_gadget *g)
1212{
1213 struct dwc3 *dwc = gadget_to_dwc(g);
1214
1215 unsigned long timeout;
1216 unsigned long flags;
1217
1218 u32 reg;
1219
1220 int ret = 0;
1221
1222 u8 link_state;
1223 u8 speed;
1224
1225 spin_lock_irqsave(&dwc->lock, flags);
1226
1227 /*
1228 * According to the Databook Remote wakeup request should
1229 * be issued only when the device is in early suspend state.
1230 *
1231 * We can check that via USB Link State bits in DSTS register.
1232 */
1233 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1234
1235 speed = reg & DWC3_DSTS_CONNECTSPD;
1236 if (speed == DWC3_DSTS_SUPERSPEED) {
1237 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1238 ret = -EINVAL;
1239 goto out;
1240 }
1241
1242 link_state = DWC3_DSTS_USBLNKST(reg);
1243
1244 switch (link_state) {
1245 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1246 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1247 break;
1248 default:
1249 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1250 link_state);
1251 ret = -EINVAL;
1252 goto out;
1253 }
1254
1255 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1256 if (ret < 0) {
1257 dev_err(dwc->dev, "failed to put link in Recovery\n");
1258 goto out;
1259 }
1260
1261 /* write zeroes to Link Change Request */
1262 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1263 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1264
1265 /* poll until Link State changes to ON */
1266 timeout = jiffies + msecs_to_jiffies(100);
1267
1268 while (!time_after(jiffies, timeout)) {
1269 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1270
1271 /* in HS, means ON */
1272 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1273 break;
1274 }
1275
1276 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1277 dev_err(dwc->dev, "failed to send remote wakeup\n");
1278 ret = -EINVAL;
1279 }
1280
1281out:
1282 spin_unlock_irqrestore(&dwc->lock, flags);
1283
1284 return ret;
1285}
1286
1287static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1288 int is_selfpowered)
1289{
1290 struct dwc3 *dwc = gadget_to_dwc(g);
1291 unsigned long flags;
1292
1293 spin_lock_irqsave(&dwc->lock, flags);
1294 dwc->is_selfpowered = !!is_selfpowered;
1295 spin_unlock_irqrestore(&dwc->lock, flags);
1296
1297 return 0;
1298}
1299
1300static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1301{
1302 u32 reg;
1303 u32 timeout = 500;
1304
1305 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1306 if (is_on) {
1307 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1308 reg |= (DWC3_DCTL_RUN_STOP
1309 | DWC3_DCTL_TRGTULST_RX_DET);
1310 } else {
1311 reg &= ~DWC3_DCTL_RUN_STOP;
1312 }
1313
1314 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1315
1316 do {
1317 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1318 if (is_on) {
1319 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1320 break;
1321 } else {
1322 if (reg & DWC3_DSTS_DEVCTRLHLT)
1323 break;
1324 }
1325 timeout--;
1326 if (!timeout)
1327 break;
1328 udelay(1);
1329 } while (1);
1330
1331 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1332 dwc->gadget_driver
1333 ? dwc->gadget_driver->function : "no-function",
1334 is_on ? "connect" : "disconnect");
1335}
1336
1337static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1338{
1339 struct dwc3 *dwc = gadget_to_dwc(g);
1340 unsigned long flags;
1341
1342 is_on = !!is_on;
1343
1344 spin_lock_irqsave(&dwc->lock, flags);
1345 dwc3_gadget_run_stop(dwc, is_on);
1346 spin_unlock_irqrestore(&dwc->lock, flags);
1347
1348 return 0;
1349}
1350
1351static int dwc3_gadget_start(struct usb_gadget *g,
1352 struct usb_gadget_driver *driver)
1353{
1354 struct dwc3 *dwc = gadget_to_dwc(g);
1355 struct dwc3_ep *dep;
1356 unsigned long flags;
1357 int ret = 0;
1358 u32 reg;
1359
1360 spin_lock_irqsave(&dwc->lock, flags);
1361
1362 if (dwc->gadget_driver) {
1363 dev_err(dwc->dev, "%s is already bound to %s\n",
1364 dwc->gadget.name,
1365 dwc->gadget_driver->driver.name);
1366 ret = -EBUSY;
1367 goto err0;
1368 }
1369
1370 dwc->gadget_driver = driver;
1371 dwc->gadget.dev.driver = &driver->driver;
1372
1373 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1374 reg &= ~(DWC3_DCFG_SPEED_MASK);
1375 reg |= dwc->maximum_speed;
1376 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1377
1378 dwc->start_config_issued = false;
1379
1380 /* Start with SuperSpeed Default */
1381 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1382
1383 dep = dwc->eps[0];
1384 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
1385 if (ret) {
1386 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1387 goto err0;
1388 }
1389
1390 dep = dwc->eps[1];
1391 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
1392 if (ret) {
1393 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1394 goto err1;
1395 }
1396
1397 /* begin to receive SETUP packets */
1398 dwc->ep0state = EP0_SETUP_PHASE;
1399 dwc3_ep0_out_start(dwc);
1400
1401 spin_unlock_irqrestore(&dwc->lock, flags);
1402
1403 return 0;
1404
1405err1:
1406 __dwc3_gadget_ep_disable(dwc->eps[0]);
1407
1408err0:
1409 dwc->gadget_driver = NULL;
1410 spin_unlock_irqrestore(&dwc->lock, flags);
1411
1412 return ret;
1413}
1414
1415static int dwc3_gadget_stop(struct usb_gadget *g,
1416 struct usb_gadget_driver *driver)
1417{
1418 struct dwc3 *dwc = gadget_to_dwc(g);
1419 unsigned long flags;
1420
1421 spin_lock_irqsave(&dwc->lock, flags);
1422
1423 __dwc3_gadget_ep_disable(dwc->eps[0]);
1424 __dwc3_gadget_ep_disable(dwc->eps[1]);
1425
1426 dwc->gadget_driver = NULL;
1427 dwc->gadget.dev.driver = NULL;
1428
1429 spin_unlock_irqrestore(&dwc->lock, flags);
1430
1431 return 0;
1432}
1433static const struct usb_gadget_ops dwc3_gadget_ops = {
1434 .get_frame = dwc3_gadget_get_frame,
1435 .wakeup = dwc3_gadget_wakeup,
1436 .set_selfpowered = dwc3_gadget_set_selfpowered,
1437 .pullup = dwc3_gadget_pullup,
1438 .udc_start = dwc3_gadget_start,
1439 .udc_stop = dwc3_gadget_stop,
1440};
1441
1442/* -------------------------------------------------------------------------- */
1443
1444static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1445{
1446 struct dwc3_ep *dep;
1447 u8 epnum;
1448
1449 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1450
1451 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1452 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1453 if (!dep) {
1454 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1455 epnum);
1456 return -ENOMEM;
1457 }
1458
1459 dep->dwc = dwc;
1460 dep->number = epnum;
1461 dwc->eps[epnum] = dep;
1462
1463 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1464 (epnum & 1) ? "in" : "out");
1465 dep->endpoint.name = dep->name;
1466 dep->direction = (epnum & 1);
1467
1468 if (epnum == 0 || epnum == 1) {
1469 dep->endpoint.maxpacket = 512;
1470 dep->endpoint.maxburst = 1;
1471 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1472 if (!epnum)
1473 dwc->gadget.ep0 = &dep->endpoint;
1474 } else {
1475 int ret;
1476
1477 dep->endpoint.maxpacket = 1024;
1478 dep->endpoint.max_streams = 15;
1479 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1480 list_add_tail(&dep->endpoint.ep_list,
1481 &dwc->gadget.ep_list);
1482
1483 ret = dwc3_alloc_trb_pool(dep);
1484 if (ret)
1485 return ret;
1486 }
1487
1488 INIT_LIST_HEAD(&dep->request_list);
1489 INIT_LIST_HEAD(&dep->req_queued);
1490 }
1491
1492 return 0;
1493}
1494
1495static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1496{
1497 struct dwc3_ep *dep;
1498 u8 epnum;
1499
1500 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1501 dep = dwc->eps[epnum];
1502 /*
1503 * Physical endpoints 0 and 1 are special; they form the
1504 * bi-directional USB endpoint 0.
1505 *
1506 * For those two physical endpoints, we don't allocate a TRB
1507 * pool nor do we add them the endpoints list. Due to that, we
1508 * shouldn't do these two operations otherwise we would end up
1509 * with all sorts of bugs when removing dwc3.ko.
1510 */
1511 if (epnum != 0 && epnum != 1) {
1512 dwc3_free_trb_pool(dep);
1513 list_del(&dep->endpoint.ep_list);
1514 }
1515
1516 kfree(dep);
1517 }
1518}
1519
1520static void dwc3_gadget_release(struct device *dev)
1521{
1522 dev_dbg(dev, "%s\n", __func__);
1523}
1524
1525/* -------------------------------------------------------------------------- */
1526static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1527 const struct dwc3_event_depevt *event, int status)
1528{
1529 struct dwc3_request *req;
1530 struct dwc3_trb *trb;
1531 unsigned int count;
1532 unsigned int s_pkt = 0;
1533
1534 do {
1535 req = next_request(&dep->req_queued);
1536 if (!req) {
1537 WARN_ON_ONCE(1);
1538 return 1;
1539 }
1540
1541 trb = req->trb;
1542
1543 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1544 /*
1545 * We continue despite the error. There is not much we
1546 * can do. If we don't clean it up we loop forever. If
1547 * we skip the TRB then it gets overwritten after a
1548 * while since we use them in a ring buffer. A BUG()
1549 * would help. Lets hope that if this occurs, someone
1550 * fixes the root cause instead of looking away :)
1551 */
1552 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1553 dep->name, req->trb);
1554 count = trb->size & DWC3_TRB_SIZE_MASK;
1555
1556 if (dep->direction) {
1557 if (count) {
1558 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1559 dep->name);
1560 status = -ECONNRESET;
1561 }
1562 } else {
1563 if (count && (event->status & DEPEVT_STATUS_SHORT))
1564 s_pkt = 1;
1565 }
1566
1567 /*
1568 * We assume here we will always receive the entire data block
1569 * which we should receive. Meaning, if we program RX to
1570 * receive 4K but we receive only 2K, we assume that's all we
1571 * should receive and we simply bounce the request back to the
1572 * gadget driver for further processing.
1573 */
1574 req->request.actual += req->request.length - count;
1575 dwc3_gadget_giveback(dep, req, status);
1576 if (s_pkt)
1577 break;
1578 if ((event->status & DEPEVT_STATUS_LST) &&
1579 (trb->ctrl & DWC3_TRB_CTRL_LST))
1580 break;
1581 if ((event->status & DEPEVT_STATUS_IOC) &&
1582 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1583 break;
1584 } while (1);
1585
1586 if ((event->status & DEPEVT_STATUS_IOC) &&
1587 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1588 return 0;
1589 return 1;
1590}
1591
1592static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1593 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1594 int start_new)
1595{
1596 unsigned status = 0;
1597 int clean_busy;
1598
1599 if (event->status & DEPEVT_STATUS_BUSERR)
1600 status = -ECONNRESET;
1601
1602 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1603 if (clean_busy)
1604 dep->flags &= ~DWC3_EP_BUSY;
1605
1606 /*
1607 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1608 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1609 */
1610 if (dwc->revision < DWC3_REVISION_183A) {
1611 u32 reg;
1612 int i;
1613
1614 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1615 struct dwc3_ep *dep = dwc->eps[i];
1616
1617 if (!(dep->flags & DWC3_EP_ENABLED))
1618 continue;
1619
1620 if (!list_empty(&dep->req_queued))
1621 return;
1622 }
1623
1624 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1625 reg |= dwc->u1u2;
1626 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1627
1628 dwc->u1u2 = 0;
1629 }
1630}
1631
1632static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1633 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1634{
1635 u32 uf, mask;
1636
1637 if (list_empty(&dep->request_list)) {
1638 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1639 dep->name);
1640 return;
1641 }
1642
1643 mask = ~(dep->interval - 1);
1644 uf = event->parameters & mask;
1645 /* 4 micro frames in the future */
1646 uf += dep->interval * 4;
1647
1648 __dwc3_gadget_kick_transfer(dep, uf, 1);
1649}
1650
1651static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1652 const struct dwc3_event_depevt *event)
1653{
1654 struct dwc3 *dwc = dep->dwc;
1655 struct dwc3_event_depevt mod_ev = *event;
1656
1657 /*
1658 * We were asked to remove one request. It is possible that this
1659 * request and a few others were started together and have the same
1660 * transfer index. Since we stopped the complete endpoint we don't
1661 * know how many requests were already completed (and not yet)
1662 * reported and how could be done (later). We purge them all until
1663 * the end of the list.
1664 */
1665 mod_ev.status = DEPEVT_STATUS_LST;
1666 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1667 dep->flags &= ~DWC3_EP_BUSY;
1668 /* pending requests are ignored and are queued on XferNotReady */
1669}
1670
1671static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1672 const struct dwc3_event_depevt *event)
1673{
1674 u32 param = event->parameters;
1675 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1676
1677 switch (cmd_type) {
1678 case DWC3_DEPCMD_ENDTRANSFER:
1679 dwc3_process_ep_cmd_complete(dep, event);
1680 break;
1681 case DWC3_DEPCMD_STARTTRANSFER:
1682 dep->res_trans_idx = param & 0x7f;
1683 break;
1684 default:
1685 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1686 __func__, cmd_type);
1687 break;
1688 };
1689}
1690
1691static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1692 const struct dwc3_event_depevt *event)
1693{
1694 struct dwc3_ep *dep;
1695 u8 epnum = event->endpoint_number;
1696
1697 dep = dwc->eps[epnum];
1698
1699 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1700 dwc3_ep_event_string(event->endpoint_event));
1701
1702 if (epnum == 0 || epnum == 1) {
1703 dwc3_ep0_interrupt(dwc, event);
1704 return;
1705 }
1706
1707 switch (event->endpoint_event) {
1708 case DWC3_DEPEVT_XFERCOMPLETE:
1709 dep->res_trans_idx = 0;
1710
1711 if (usb_endpoint_xfer_isoc(dep->desc)) {
1712 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1713 dep->name);
1714 return;
1715 }
1716
1717 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1718 break;
1719 case DWC3_DEPEVT_XFERINPROGRESS:
1720 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1721 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1722 dep->name);
1723 return;
1724 }
1725
1726 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1727 break;
1728 case DWC3_DEPEVT_XFERNOTREADY:
1729 if (usb_endpoint_xfer_isoc(dep->desc)) {
1730 dwc3_gadget_start_isoc(dwc, dep, event);
1731 } else {
1732 int ret;
1733
1734 dev_vdbg(dwc->dev, "%s: reason %s\n",
1735 dep->name, event->status &
1736 DEPEVT_STATUS_TRANSFER_ACTIVE
1737 ? "Transfer Active"
1738 : "Transfer Not Active");
1739
1740 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1741 if (!ret || ret == -EBUSY)
1742 return;
1743
1744 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1745 dep->name);
1746 }
1747
1748 break;
1749 case DWC3_DEPEVT_STREAMEVT:
1750 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1751 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1752 dep->name);
1753 return;
1754 }
1755
1756 switch (event->status) {
1757 case DEPEVT_STREAMEVT_FOUND:
1758 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1759 event->parameters);
1760
1761 break;
1762 case DEPEVT_STREAMEVT_NOTFOUND:
1763 /* FALLTHROUGH */
1764 default:
1765 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1766 }
1767 break;
1768 case DWC3_DEPEVT_RXTXFIFOEVT:
1769 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1770 break;
1771 case DWC3_DEPEVT_EPCMDCMPLT:
1772 dwc3_ep_cmd_compl(dep, event);
1773 break;
1774 }
1775}
1776
1777static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1778{
1779 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1780 spin_unlock(&dwc->lock);
1781 dwc->gadget_driver->disconnect(&dwc->gadget);
1782 spin_lock(&dwc->lock);
1783 }
1784}
1785
1786static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1787{
1788 struct dwc3_ep *dep;
1789 struct dwc3_gadget_ep_cmd_params params;
1790 u32 cmd;
1791 int ret;
1792
1793 dep = dwc->eps[epnum];
1794
1795 WARN_ON(!dep->res_trans_idx);
1796 if (dep->res_trans_idx) {
1797 cmd = DWC3_DEPCMD_ENDTRANSFER;
1798 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1799 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1800 memset(&params, 0, sizeof(params));
1801 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1802 WARN_ON_ONCE(ret);
1803 dep->res_trans_idx = 0;
1804 dep->flags &= ~DWC3_EP_BUSY;
1805 }
1806}
1807
1808static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1809{
1810 u32 epnum;
1811
1812 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1813 struct dwc3_ep *dep;
1814
1815 dep = dwc->eps[epnum];
1816 if (!(dep->flags & DWC3_EP_ENABLED))
1817 continue;
1818
1819 dwc3_remove_requests(dwc, dep);
1820 }
1821}
1822
1823static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1824{
1825 u32 epnum;
1826
1827 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1828 struct dwc3_ep *dep;
1829 struct dwc3_gadget_ep_cmd_params params;
1830 int ret;
1831
1832 dep = dwc->eps[epnum];
1833
1834 if (!(dep->flags & DWC3_EP_STALL))
1835 continue;
1836
1837 dep->flags &= ~DWC3_EP_STALL;
1838
1839 memset(&params, 0, sizeof(params));
1840 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1841 DWC3_DEPCMD_CLEARSTALL, &params);
1842 WARN_ON_ONCE(ret);
1843 }
1844}
1845
1846static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1847{
1848 dev_vdbg(dwc->dev, "%s\n", __func__);
1849#if 0
1850 XXX
1851 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1852 enable it before we can disable it.
1853
1854 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1855 reg &= ~DWC3_DCTL_INITU1ENA;
1856 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1857
1858 reg &= ~DWC3_DCTL_INITU2ENA;
1859 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1860#endif
1861
1862 dwc3_stop_active_transfers(dwc);
1863 dwc3_disconnect_gadget(dwc);
1864 dwc->start_config_issued = false;
1865
1866 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1867 dwc->setup_packet_pending = false;
1868}
1869
1870static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1871{
1872 u32 reg;
1873
1874 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1875
1876 if (on)
1877 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1878 else
1879 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1880
1881 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1882}
1883
1884static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1885{
1886 u32 reg;
1887
1888 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1889
1890 if (on)
1891 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1892 else
1893 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1894
1895 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1896}
1897
1898static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1899{
1900 u32 reg;
1901
1902 dev_vdbg(dwc->dev, "%s\n", __func__);
1903
1904 /*
1905 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1906 * would cause a missing Disconnect Event if there's a
1907 * pending Setup Packet in the FIFO.
1908 *
1909 * There's no suggested workaround on the official Bug
1910 * report, which states that "unless the driver/application
1911 * is doing any special handling of a disconnect event,
1912 * there is no functional issue".
1913 *
1914 * Unfortunately, it turns out that we _do_ some special
1915 * handling of a disconnect event, namely complete all
1916 * pending transfers, notify gadget driver of the
1917 * disconnection, and so on.
1918 *
1919 * Our suggested workaround is to follow the Disconnect
1920 * Event steps here, instead, based on a setup_packet_pending
1921 * flag. Such flag gets set whenever we have a XferNotReady
1922 * event on EP0 and gets cleared on XferComplete for the
1923 * same endpoint.
1924 *
1925 * Refers to:
1926 *
1927 * STAR#9000466709: RTL: Device : Disconnect event not
1928 * generated if setup packet pending in FIFO
1929 */
1930 if (dwc->revision < DWC3_REVISION_188A) {
1931 if (dwc->setup_packet_pending)
1932 dwc3_gadget_disconnect_interrupt(dwc);
1933 }
1934
1935 /* after reset -> Default State */
1936 dwc->dev_state = DWC3_DEFAULT_STATE;
1937
1938 /* Enable PHYs */
1939 dwc3_gadget_usb2_phy_power(dwc, true);
1940 dwc3_gadget_usb3_phy_power(dwc, true);
1941
1942 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1943 dwc3_disconnect_gadget(dwc);
1944
1945 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1946 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1947 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1948 dwc->test_mode = false;
1949
1950 dwc3_stop_active_transfers(dwc);
1951 dwc3_clear_stall_all_ep(dwc);
1952 dwc->start_config_issued = false;
1953
1954 /* Reset device address to zero */
1955 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1956 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1957 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1958}
1959
1960static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1961{
1962 u32 reg;
1963 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1964
1965 /*
1966 * We change the clock only at SS but I dunno why I would want to do
1967 * this. Maybe it becomes part of the power saving plan.
1968 */
1969
1970 if (speed != DWC3_DSTS_SUPERSPEED)
1971 return;
1972
1973 /*
1974 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1975 * each time on Connect Done.
1976 */
1977 if (!usb30_clock)
1978 return;
1979
1980 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1981 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1982 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1983}
1984
1985static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1986{
1987 switch (speed) {
1988 case USB_SPEED_SUPER:
1989 dwc3_gadget_usb2_phy_power(dwc, false);
1990 break;
1991 case USB_SPEED_HIGH:
1992 case USB_SPEED_FULL:
1993 case USB_SPEED_LOW:
1994 dwc3_gadget_usb3_phy_power(dwc, false);
1995 break;
1996 }
1997}
1998
1999static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2000{
2001 struct dwc3_gadget_ep_cmd_params params;
2002 struct dwc3_ep *dep;
2003 int ret;
2004 u32 reg;
2005 u8 speed;
2006
2007 dev_vdbg(dwc->dev, "%s\n", __func__);
2008
2009 memset(&params, 0x00, sizeof(params));
2010
2011 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2012 speed = reg & DWC3_DSTS_CONNECTSPD;
2013 dwc->speed = speed;
2014
2015 dwc3_update_ram_clk_sel(dwc, speed);
2016
2017 switch (speed) {
2018 case DWC3_DCFG_SUPERSPEED:
2019 /*
2020 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2021 * would cause a missing USB3 Reset event.
2022 *
2023 * In such situations, we should force a USB3 Reset
2024 * event by calling our dwc3_gadget_reset_interrupt()
2025 * routine.
2026 *
2027 * Refers to:
2028 *
2029 * STAR#9000483510: RTL: SS : USB3 reset event may
2030 * not be generated always when the link enters poll
2031 */
2032 if (dwc->revision < DWC3_REVISION_190A)
2033 dwc3_gadget_reset_interrupt(dwc);
2034
2035 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2036 dwc->gadget.ep0->maxpacket = 512;
2037 dwc->gadget.speed = USB_SPEED_SUPER;
2038 break;
2039 case DWC3_DCFG_HIGHSPEED:
2040 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2041 dwc->gadget.ep0->maxpacket = 64;
2042 dwc->gadget.speed = USB_SPEED_HIGH;
2043 break;
2044 case DWC3_DCFG_FULLSPEED2:
2045 case DWC3_DCFG_FULLSPEED1:
2046 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2047 dwc->gadget.ep0->maxpacket = 64;
2048 dwc->gadget.speed = USB_SPEED_FULL;
2049 break;
2050 case DWC3_DCFG_LOWSPEED:
2051 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2052 dwc->gadget.ep0->maxpacket = 8;
2053 dwc->gadget.speed = USB_SPEED_LOW;
2054 break;
2055 }
2056
2057 /* Disable unneded PHY */
2058 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2059
2060 dep = dwc->eps[0];
2061 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
2062 if (ret) {
2063 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2064 return;
2065 }
2066
2067 dep = dwc->eps[1];
2068 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
2069 if (ret) {
2070 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2071 return;
2072 }
2073
2074 /*
2075 * Configure PHY via GUSB3PIPECTLn if required.
2076 *
2077 * Update GTXFIFOSIZn
2078 *
2079 * In both cases reset values should be sufficient.
2080 */
2081}
2082
2083static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2084{
2085 dev_vdbg(dwc->dev, "%s\n", __func__);
2086
2087 /*
2088 * TODO take core out of low power mode when that's
2089 * implemented.
2090 */
2091
2092 dwc->gadget_driver->resume(&dwc->gadget);
2093}
2094
2095static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2096 unsigned int evtinfo)
2097{
2098 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2099
2100 /*
2101 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2102 * on the link partner, the USB session might do multiple entry/exit
2103 * of low power states before a transfer takes place.
2104 *
2105 * Due to this problem, we might experience lower throughput. The
2106 * suggested workaround is to disable DCTL[12:9] bits if we're
2107 * transitioning from U1/U2 to U0 and enable those bits again
2108 * after a transfer completes and there are no pending transfers
2109 * on any of the enabled endpoints.
2110 *
2111 * This is the first half of that workaround.
2112 *
2113 * Refers to:
2114 *
2115 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2116 * core send LGO_Ux entering U0
2117 */
2118 if (dwc->revision < DWC3_REVISION_183A) {
2119 if (next == DWC3_LINK_STATE_U0) {
2120 u32 u1u2;
2121 u32 reg;
2122
2123 switch (dwc->link_state) {
2124 case DWC3_LINK_STATE_U1:
2125 case DWC3_LINK_STATE_U2:
2126 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2127 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2128 | DWC3_DCTL_ACCEPTU2ENA
2129 | DWC3_DCTL_INITU1ENA
2130 | DWC3_DCTL_ACCEPTU1ENA);
2131
2132 if (!dwc->u1u2)
2133 dwc->u1u2 = reg & u1u2;
2134
2135 reg &= ~u1u2;
2136
2137 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2138 break;
2139 default:
2140 /* do nothing */
2141 break;
2142 }
2143 }
2144 }
2145
2146 dwc->link_state = next;
2147
2148 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
2149}
2150
2151static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2152 const struct dwc3_event_devt *event)
2153{
2154 switch (event->type) {
2155 case DWC3_DEVICE_EVENT_DISCONNECT:
2156 dwc3_gadget_disconnect_interrupt(dwc);
2157 break;
2158 case DWC3_DEVICE_EVENT_RESET:
2159 dwc3_gadget_reset_interrupt(dwc);
2160 break;
2161 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2162 dwc3_gadget_conndone_interrupt(dwc);
2163 break;
2164 case DWC3_DEVICE_EVENT_WAKEUP:
2165 dwc3_gadget_wakeup_interrupt(dwc);
2166 break;
2167 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2168 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2169 break;
2170 case DWC3_DEVICE_EVENT_EOPF:
2171 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2172 break;
2173 case DWC3_DEVICE_EVENT_SOF:
2174 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2175 break;
2176 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2177 dev_vdbg(dwc->dev, "Erratic Error\n");
2178 break;
2179 case DWC3_DEVICE_EVENT_CMD_CMPL:
2180 dev_vdbg(dwc->dev, "Command Complete\n");
2181 break;
2182 case DWC3_DEVICE_EVENT_OVERFLOW:
2183 dev_vdbg(dwc->dev, "Overflow\n");
2184 break;
2185 default:
2186 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2187 }
2188}
2189
2190static void dwc3_process_event_entry(struct dwc3 *dwc,
2191 const union dwc3_event *event)
2192{
2193 /* Endpoint IRQ, handle it and return early */
2194 if (event->type.is_devspec == 0) {
2195 /* depevt */
2196 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2197 }
2198
2199 switch (event->type.type) {
2200 case DWC3_EVENT_TYPE_DEV:
2201 dwc3_gadget_interrupt(dwc, &event->devt);
2202 break;
2203 /* REVISIT what to do with Carkit and I2C events ? */
2204 default:
2205 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2206 }
2207}
2208
2209static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2210{
2211 struct dwc3_event_buffer *evt;
2212 int left;
2213 u32 count;
2214
2215 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2216 count &= DWC3_GEVNTCOUNT_MASK;
2217 if (!count)
2218 return IRQ_NONE;
2219
2220 evt = dwc->ev_buffs[buf];
2221 left = count;
2222
2223 while (left > 0) {
2224 union dwc3_event event;
2225
2226 event.raw = *(u32 *) (evt->buf + evt->lpos);
2227
2228 dwc3_process_event_entry(dwc, &event);
2229 /*
2230 * XXX we wrap around correctly to the next entry as almost all
2231 * entries are 4 bytes in size. There is one entry which has 12
2232 * bytes which is a regular entry followed by 8 bytes data. ATM
2233 * I don't know how things are organized if were get next to the
2234 * a boundary so I worry about that once we try to handle that.
2235 */
2236 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2237 left -= 4;
2238
2239 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2240 }
2241
2242 return IRQ_HANDLED;
2243}
2244
2245static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2246{
2247 struct dwc3 *dwc = _dwc;
2248 int i;
2249 irqreturn_t ret = IRQ_NONE;
2250
2251 spin_lock(&dwc->lock);
2252
2253 for (i = 0; i < dwc->num_event_buffers; i++) {
2254 irqreturn_t status;
2255
2256 status = dwc3_process_event_buf(dwc, i);
2257 if (status == IRQ_HANDLED)
2258 ret = status;
2259 }
2260
2261 spin_unlock(&dwc->lock);
2262
2263 return ret;
2264}
2265
2266/**
2267 * dwc3_gadget_init - Initializes gadget related registers
2268 * @dwc: pointer to our controller context structure
2269 *
2270 * Returns 0 on success otherwise negative errno.
2271 */
2272int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2273{
2274 u32 reg;
2275 int ret;
2276 int irq;
2277
2278 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2279 &dwc->ctrl_req_addr, GFP_KERNEL);
2280 if (!dwc->ctrl_req) {
2281 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2282 ret = -ENOMEM;
2283 goto err0;
2284 }
2285
2286 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2287 &dwc->ep0_trb_addr, GFP_KERNEL);
2288 if (!dwc->ep0_trb) {
2289 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2290 ret = -ENOMEM;
2291 goto err1;
2292 }
2293
2294 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2295 GFP_KERNEL);
2296 if (!dwc->setup_buf) {
2297 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2298 ret = -ENOMEM;
2299 goto err2;
2300 }
2301
2302 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2303 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2304 if (!dwc->ep0_bounce) {
2305 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2306 ret = -ENOMEM;
2307 goto err3;
2308 }
2309
2310 dev_set_name(&dwc->gadget.dev, "gadget");
2311
2312 dwc->gadget.ops = &dwc3_gadget_ops;
2313 dwc->gadget.max_speed = USB_SPEED_SUPER;
2314 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2315 dwc->gadget.dev.parent = dwc->dev;
2316 dwc->gadget.sg_supported = true;
2317
2318 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2319
2320 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2321 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2322 dwc->gadget.dev.release = dwc3_gadget_release;
2323 dwc->gadget.name = "dwc3-gadget";
2324
2325 /*
2326 * REVISIT: Here we should clear all pending IRQs to be
2327 * sure we're starting from a well known location.
2328 */
2329
2330 ret = dwc3_gadget_init_endpoints(dwc);
2331 if (ret)
2332 goto err4;
2333
2334 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2335
2336 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2337 "dwc3", dwc);
2338 if (ret) {
2339 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2340 irq, ret);
2341 goto err5;
2342 }
2343
2344 /* Enable all but Start and End of Frame IRQs */
2345 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2346 DWC3_DEVTEN_EVNTOVERFLOWEN |
2347 DWC3_DEVTEN_CMDCMPLTEN |
2348 DWC3_DEVTEN_ERRTICERREN |
2349 DWC3_DEVTEN_WKUPEVTEN |
2350 DWC3_DEVTEN_ULSTCNGEN |
2351 DWC3_DEVTEN_CONNECTDONEEN |
2352 DWC3_DEVTEN_USBRSTEN |
2353 DWC3_DEVTEN_DISCONNEVTEN);
2354 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2355
2356 ret = device_register(&dwc->gadget.dev);
2357 if (ret) {
2358 dev_err(dwc->dev, "failed to register gadget device\n");
2359 put_device(&dwc->gadget.dev);
2360 goto err6;
2361 }
2362
2363 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2364 if (ret) {
2365 dev_err(dwc->dev, "failed to register udc\n");
2366 goto err7;
2367 }
2368
2369 return 0;
2370
2371err7:
2372 device_unregister(&dwc->gadget.dev);
2373
2374err6:
2375 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2376 free_irq(irq, dwc);
2377
2378err5:
2379 dwc3_gadget_free_endpoints(dwc);
2380
2381err4:
2382 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2383 dwc->ep0_bounce_addr);
2384
2385err3:
2386 kfree(dwc->setup_buf);
2387
2388err2:
2389 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2390 dwc->ep0_trb, dwc->ep0_trb_addr);
2391
2392err1:
2393 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2394 dwc->ctrl_req, dwc->ctrl_req_addr);
2395
2396err0:
2397 return ret;
2398}
2399
2400void dwc3_gadget_exit(struct dwc3 *dwc)
2401{
2402 int irq;
2403
2404 usb_del_gadget_udc(&dwc->gadget);
2405 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2406
2407 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2408 free_irq(irq, dwc);
2409
2410 dwc3_gadget_free_endpoints(dwc);
2411
2412 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2413 dwc->ep0_bounce_addr);
2414
2415 kfree(dwc->setup_buf);
2416
2417 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2418 dwc->ep0_trb, dwc->ep0_trb_addr);
2419
2420 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2421 dwc->ctrl_req, dwc->ctrl_req_addr);
2422
2423 device_unregister(&dwc->gadget.dev);
2424}