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