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.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
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 - 1) / dep->endpoint.maxpacket + 1);
736 }
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
1570 spin_unlock_irqrestore(&dwc->lock, flags);
1571
1572 return 0;
1573 }
1574
1575 static const struct usb_gadget_ops dwc3_gadget_ops = {
1576 .get_frame = dwc3_gadget_get_frame,
1577 .wakeup = dwc3_gadget_wakeup,
1578 .set_selfpowered = dwc3_gadget_set_selfpowered,
1579 .pullup = dwc3_gadget_pullup,
1580 .udc_start = dwc3_gadget_start,
1581 .udc_stop = dwc3_gadget_stop,
1582 };
1583
1584 /* -------------------------------------------------------------------------- */
1585
dwc3_gadget_init_hw_endpoints(struct dwc3 * dwc,u8 num,u32 direction)1586 static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1587 u8 num, u32 direction)
1588 {
1589 struct dwc3_ep *dep;
1590 u8 i;
1591
1592 for (i = 0; i < num; i++) {
1593 u8 epnum = (i << 1) | (!!direction);
1594
1595 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1596 if (!dep)
1597 return -ENOMEM;
1598
1599 dep->dwc = dwc;
1600 dep->number = epnum;
1601 dep->direction = !!direction;
1602 dwc->eps[epnum] = dep;
1603
1604 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1605 (epnum & 1) ? "in" : "out");
1606
1607 dep->endpoint.name = dep->name;
1608
1609 dev_vdbg(dwc->dev, "initializing %s\n", dep->name);
1610
1611 if (epnum == 0 || epnum == 1) {
1612 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
1613 dep->endpoint.maxburst = 1;
1614 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1615 if (!epnum)
1616 dwc->gadget.ep0 = &dep->endpoint;
1617 } else {
1618 int ret;
1619
1620 if (dwc->maximum_speed < USB_SPEED_SUPER)
1621 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
1622 else
1623 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
1624 dep->endpoint.max_streams = 15;
1625 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1626 list_add_tail(&dep->endpoint.ep_list,
1627 &dwc->gadget.ep_list);
1628
1629 ret = dwc3_alloc_trb_pool(dep);
1630 if (ret)
1631 return ret;
1632 }
1633
1634 INIT_LIST_HEAD(&dep->request_list);
1635 INIT_LIST_HEAD(&dep->req_queued);
1636 }
1637
1638 return 0;
1639 }
1640
dwc3_gadget_init_endpoints(struct dwc3 * dwc)1641 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1642 {
1643 int ret;
1644
1645 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1646
1647 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1648 if (ret < 0) {
1649 dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
1650 return ret;
1651 }
1652
1653 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1654 if (ret < 0) {
1655 dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
1656 return ret;
1657 }
1658
1659 return 0;
1660 }
1661
dwc3_gadget_free_endpoints(struct dwc3 * dwc)1662 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1663 {
1664 struct dwc3_ep *dep;
1665 u8 epnum;
1666
1667 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1668 dep = dwc->eps[epnum];
1669 if (!dep)
1670 continue;
1671 /*
1672 * Physical endpoints 0 and 1 are special; they form the
1673 * bi-directional USB endpoint 0.
1674 *
1675 * For those two physical endpoints, we don't allocate a TRB
1676 * pool nor do we add them the endpoints list. Due to that, we
1677 * shouldn't do these two operations otherwise we would end up
1678 * with all sorts of bugs when removing dwc3.ko.
1679 */
1680 if (epnum != 0 && epnum != 1) {
1681 dwc3_free_trb_pool(dep);
1682 list_del(&dep->endpoint.ep_list);
1683 }
1684
1685 kfree(dep);
1686 }
1687 }
1688
1689 /* -------------------------------------------------------------------------- */
1690
__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)1691 static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1692 struct dwc3_request *req, struct dwc3_trb *trb,
1693 const struct dwc3_event_depevt *event, int status)
1694 {
1695 unsigned int count;
1696 unsigned int s_pkt = 0;
1697 unsigned int trb_status;
1698
1699 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1700 /*
1701 * We continue despite the error. There is not much we
1702 * can do. If we don't clean it up we loop forever. If
1703 * we skip the TRB then it gets overwritten after a
1704 * while since we use them in a ring buffer. A BUG()
1705 * would help. Lets hope that if this occurs, someone
1706 * fixes the root cause instead of looking away :)
1707 */
1708 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1709 dep->name, trb);
1710 count = trb->size & DWC3_TRB_SIZE_MASK;
1711
1712 if (dep->direction) {
1713 if (count) {
1714 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1715 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1716 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1717 dep->name);
1718 /*
1719 * If missed isoc occurred and there is
1720 * no request queued then issue END
1721 * TRANSFER, so that core generates
1722 * next xfernotready and we will issue
1723 * a fresh START TRANSFER.
1724 * If there are still queued request
1725 * then wait, do not issue either END
1726 * or UPDATE TRANSFER, just attach next
1727 * request in request_list during
1728 * giveback.If any future queued request
1729 * is successfully transferred then we
1730 * will issue UPDATE TRANSFER for all
1731 * request in the request_list.
1732 */
1733 dep->flags |= DWC3_EP_MISSED_ISOC;
1734 } else {
1735 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1736 dep->name);
1737 status = -ECONNRESET;
1738 }
1739 } else {
1740 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1741 }
1742 } else {
1743 if (count && (event->status & DEPEVT_STATUS_SHORT))
1744 s_pkt = 1;
1745 }
1746
1747 /*
1748 * We assume here we will always receive the entire data block
1749 * which we should receive. Meaning, if we program RX to
1750 * receive 4K but we receive only 2K, we assume that's all we
1751 * should receive and we simply bounce the request back to the
1752 * gadget driver for further processing.
1753 */
1754 req->request.actual += req->request.length - count;
1755 if (s_pkt)
1756 return 1;
1757 if ((event->status & DEPEVT_STATUS_LST) &&
1758 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1759 DWC3_TRB_CTRL_HWO)))
1760 return 1;
1761 if ((event->status & DEPEVT_STATUS_IOC) &&
1762 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1763 return 1;
1764 return 0;
1765 }
1766
dwc3_cleanup_done_reqs(struct dwc3 * dwc,struct dwc3_ep * dep,const struct dwc3_event_depevt * event,int status)1767 static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1768 const struct dwc3_event_depevt *event, int status)
1769 {
1770 struct dwc3_request *req;
1771 struct dwc3_trb *trb;
1772 unsigned int slot;
1773
1774 req = next_request(&dep->req_queued);
1775 if (!req) {
1776 WARN_ON_ONCE(1);
1777 return 1;
1778 }
1779
1780 slot = req->start_slot;
1781 if ((slot == DWC3_TRB_NUM - 1) &&
1782 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1783 slot++;
1784 slot %= DWC3_TRB_NUM;
1785 trb = &dep->trb_pool[slot];
1786
1787 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
1788 __dwc3_cleanup_done_trbs(dwc, dep, req, trb, event, status);
1789 dwc3_gadget_giveback(dep, req, status);
1790
1791 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1792 list_empty(&dep->req_queued)) {
1793 if (list_empty(&dep->request_list)) {
1794 /*
1795 * If there is no entry in request list then do
1796 * not issue END TRANSFER now. Just set PENDING
1797 * flag, so that END TRANSFER is issued when an
1798 * entry is added into request list.
1799 */
1800 dep->flags = DWC3_EP_PENDING_REQUEST;
1801 } else {
1802 dwc3_stop_active_transfer(dwc, dep->number, true);
1803 dep->flags = DWC3_EP_ENABLED;
1804 }
1805 return 1;
1806 }
1807
1808 return 1;
1809 }
1810
dwc3_endpoint_transfer_complete(struct dwc3 * dwc,struct dwc3_ep * dep,const struct dwc3_event_depevt * event)1811 static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1812 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1813 {
1814 unsigned status = 0;
1815 int clean_busy;
1816 u32 is_xfer_complete;
1817 int ret;
1818
1819 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
1820
1821 if (event->status & DEPEVT_STATUS_BUSERR)
1822 status = -ECONNRESET;
1823
1824 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1825 if (clean_busy && (is_xfer_complete ||
1826 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
1827 dep->flags &= ~DWC3_EP_BUSY;
1828
1829 /*
1830 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1831 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1832 */
1833 if (dwc->revision < DWC3_REVISION_183A) {
1834 u32 reg;
1835 int i;
1836
1837 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1838 dep = dwc->eps[i];
1839
1840 if (!(dep->flags & DWC3_EP_ENABLED))
1841 continue;
1842
1843 if (!list_empty(&dep->req_queued))
1844 return;
1845 }
1846
1847 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1848 reg |= dwc->u1u2;
1849 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1850
1851 dwc->u1u2 = 0;
1852 }
1853
1854 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1855 ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete);
1856 if (!ret || ret == -EBUSY)
1857 return;
1858 }
1859 }
1860
dwc3_endpoint_interrupt(struct dwc3 * dwc,const struct dwc3_event_depevt * event)1861 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1862 const struct dwc3_event_depevt *event)
1863 {
1864 struct dwc3_ep *dep;
1865 u8 epnum = event->endpoint_number;
1866
1867 dep = dwc->eps[epnum];
1868
1869 if (!(dep->flags & DWC3_EP_ENABLED))
1870 return;
1871
1872 if (epnum == 0 || epnum == 1) {
1873 dwc3_ep0_interrupt(dwc, event);
1874 return;
1875 }
1876
1877 switch (event->endpoint_event) {
1878 case DWC3_DEPEVT_XFERCOMPLETE:
1879 dep->resource_index = 0;
1880
1881 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1882 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1883 dep->name);
1884 return;
1885 }
1886
1887 dwc3_endpoint_transfer_complete(dwc, dep, event);
1888 break;
1889 case DWC3_DEPEVT_XFERINPROGRESS:
1890 dwc3_endpoint_transfer_complete(dwc, dep, event);
1891 break;
1892 case DWC3_DEPEVT_XFERNOTREADY:
1893 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1894 dwc3_gadget_start_isoc(dwc, dep, event);
1895 } else {
1896 int ret;
1897
1898 dev_vdbg(dwc->dev, "%s: reason %s\n",
1899 dep->name, event->status &
1900 DEPEVT_STATUS_TRANSFER_ACTIVE
1901 ? "Transfer Active"
1902 : "Transfer Not Active");
1903
1904 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1905 if (!ret || ret == -EBUSY)
1906 return;
1907
1908 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1909 dep->name);
1910 }
1911
1912 break;
1913 case DWC3_DEPEVT_STREAMEVT:
1914 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
1915 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1916 dep->name);
1917 return;
1918 }
1919
1920 switch (event->status) {
1921 case DEPEVT_STREAMEVT_FOUND:
1922 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1923 event->parameters);
1924
1925 break;
1926 case DEPEVT_STREAMEVT_NOTFOUND:
1927 /* FALLTHROUGH */
1928 default:
1929 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1930 }
1931 break;
1932 case DWC3_DEPEVT_RXTXFIFOEVT:
1933 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1934 break;
1935 case DWC3_DEPEVT_EPCMDCMPLT:
1936 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
1937 break;
1938 }
1939 }
1940
dwc3_disconnect_gadget(struct dwc3 * dwc)1941 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1942 {
1943 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1944 spin_unlock(&dwc->lock);
1945 dwc->gadget_driver->disconnect(&dwc->gadget);
1946 spin_lock(&dwc->lock);
1947 }
1948 }
1949
dwc3_suspend_gadget(struct dwc3 * dwc)1950 static void dwc3_suspend_gadget(struct dwc3 *dwc)
1951 {
1952 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
1953 spin_unlock(&dwc->lock);
1954 dwc->gadget_driver->suspend(&dwc->gadget);
1955 spin_lock(&dwc->lock);
1956 }
1957 }
1958
dwc3_resume_gadget(struct dwc3 * dwc)1959 static void dwc3_resume_gadget(struct dwc3 *dwc)
1960 {
1961 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
1962 spin_unlock(&dwc->lock);
1963 dwc->gadget_driver->resume(&dwc->gadget);
1964 }
1965 }
1966
dwc3_reset_gadget(struct dwc3 * dwc)1967 static void dwc3_reset_gadget(struct dwc3 *dwc)
1968 {
1969 if (!dwc->gadget_driver)
1970 return;
1971
1972 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
1973 spin_unlock(&dwc->lock);
1974 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
1975 spin_lock(&dwc->lock);
1976 }
1977 }
1978
dwc3_stop_active_transfer(struct dwc3 * dwc,u32 epnum,bool force)1979 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
1980 {
1981 struct dwc3_ep *dep;
1982 struct dwc3_gadget_ep_cmd_params params;
1983 u32 cmd;
1984 int ret;
1985
1986 dep = dwc->eps[epnum];
1987
1988 if (!dep->resource_index)
1989 return;
1990
1991 /*
1992 * NOTICE: We are violating what the Databook says about the
1993 * EndTransfer command. Ideally we would _always_ wait for the
1994 * EndTransfer Command Completion IRQ, but that's causing too
1995 * much trouble synchronizing between us and gadget driver.
1996 *
1997 * We have discussed this with the IP Provider and it was
1998 * suggested to giveback all requests here, but give HW some
1999 * extra time to synchronize with the interconnect. We're using
2000 * an arbitraty 100us delay for that.
2001 *
2002 * Note also that a similar handling was tested by Synopsys
2003 * (thanks a lot Paul) and nothing bad has come out of it.
2004 * In short, what we're doing is:
2005 *
2006 * - Issue EndTransfer WITH CMDIOC bit set
2007 * - Wait 100us
2008 */
2009
2010 cmd = DWC3_DEPCMD_ENDTRANSFER;
2011 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2012 cmd |= DWC3_DEPCMD_CMDIOC;
2013 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2014 memset(¶ms, 0, sizeof(params));
2015 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
2016 WARN_ON_ONCE(ret);
2017 dep->resource_index = 0;
2018 dep->flags &= ~DWC3_EP_BUSY;
2019 udelay(100);
2020 }
2021
dwc3_stop_active_transfers(struct dwc3 * dwc)2022 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2023 {
2024 u32 epnum;
2025
2026 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2027 struct dwc3_ep *dep;
2028
2029 dep = dwc->eps[epnum];
2030 if (!dep)
2031 continue;
2032
2033 if (!(dep->flags & DWC3_EP_ENABLED))
2034 continue;
2035
2036 dwc3_remove_requests(dwc, dep);
2037 }
2038 }
2039
dwc3_clear_stall_all_ep(struct dwc3 * dwc)2040 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2041 {
2042 u32 epnum;
2043
2044 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2045 struct dwc3_ep *dep;
2046 struct dwc3_gadget_ep_cmd_params params;
2047 int ret;
2048
2049 dep = dwc->eps[epnum];
2050 if (!dep)
2051 continue;
2052
2053 if (!(dep->flags & DWC3_EP_STALL))
2054 continue;
2055
2056 dep->flags &= ~DWC3_EP_STALL;
2057
2058 memset(¶ms, 0, sizeof(params));
2059 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2060 DWC3_DEPCMD_CLEARSTALL, ¶ms);
2061 WARN_ON_ONCE(ret);
2062 }
2063 }
2064
dwc3_gadget_disconnect_interrupt(struct dwc3 * dwc)2065 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2066 {
2067 int reg;
2068
2069 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2070 reg &= ~DWC3_DCTL_INITU1ENA;
2071 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2072
2073 reg &= ~DWC3_DCTL_INITU2ENA;
2074 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2075
2076 dwc3_disconnect_gadget(dwc);
2077 dwc->start_config_issued = false;
2078
2079 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2080 dwc->setup_packet_pending = false;
2081 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2082 }
2083
dwc3_gadget_reset_interrupt(struct dwc3 * dwc)2084 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2085 {
2086 u32 reg;
2087
2088 /*
2089 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2090 * would cause a missing Disconnect Event if there's a
2091 * pending Setup Packet in the FIFO.
2092 *
2093 * There's no suggested workaround on the official Bug
2094 * report, which states that "unless the driver/application
2095 * is doing any special handling of a disconnect event,
2096 * there is no functional issue".
2097 *
2098 * Unfortunately, it turns out that we _do_ some special
2099 * handling of a disconnect event, namely complete all
2100 * pending transfers, notify gadget driver of the
2101 * disconnection, and so on.
2102 *
2103 * Our suggested workaround is to follow the Disconnect
2104 * Event steps here, instead, based on a setup_packet_pending
2105 * flag. Such flag gets set whenever we have a XferNotReady
2106 * event on EP0 and gets cleared on XferComplete for the
2107 * same endpoint.
2108 *
2109 * Refers to:
2110 *
2111 * STAR#9000466709: RTL: Device : Disconnect event not
2112 * generated if setup packet pending in FIFO
2113 */
2114 if (dwc->revision < DWC3_REVISION_188A) {
2115 if (dwc->setup_packet_pending)
2116 dwc3_gadget_disconnect_interrupt(dwc);
2117 }
2118
2119 dwc3_reset_gadget(dwc);
2120
2121 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2122 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2123 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2124 dwc->test_mode = false;
2125
2126 dwc3_stop_active_transfers(dwc);
2127 dwc3_clear_stall_all_ep(dwc);
2128 dwc->start_config_issued = false;
2129
2130 /* Reset device address to zero */
2131 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2132 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2133 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2134 }
2135
dwc3_update_ram_clk_sel(struct dwc3 * dwc,u32 speed)2136 static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2137 {
2138 u32 reg;
2139 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2140
2141 /*
2142 * We change the clock only at SS but I dunno why I would want to do
2143 * this. Maybe it becomes part of the power saving plan.
2144 */
2145
2146 if (speed != DWC3_DSTS_SUPERSPEED)
2147 return;
2148
2149 /*
2150 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2151 * each time on Connect Done.
2152 */
2153 if (!usb30_clock)
2154 return;
2155
2156 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2157 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2158 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2159 }
2160
dwc3_gadget_conndone_interrupt(struct dwc3 * dwc)2161 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2162 {
2163 struct dwc3_ep *dep;
2164 int ret;
2165 u32 reg;
2166 u8 speed;
2167
2168 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2169 speed = reg & DWC3_DSTS_CONNECTSPD;
2170 dwc->speed = speed;
2171
2172 dwc3_update_ram_clk_sel(dwc, speed);
2173
2174 switch (speed) {
2175 case DWC3_DCFG_SUPERSPEED:
2176 /*
2177 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2178 * would cause a missing USB3 Reset event.
2179 *
2180 * In such situations, we should force a USB3 Reset
2181 * event by calling our dwc3_gadget_reset_interrupt()
2182 * routine.
2183 *
2184 * Refers to:
2185 *
2186 * STAR#9000483510: RTL: SS : USB3 reset event may
2187 * not be generated always when the link enters poll
2188 */
2189 if (dwc->revision < DWC3_REVISION_190A)
2190 dwc3_gadget_reset_interrupt(dwc);
2191
2192 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2193 dwc->gadget.ep0->maxpacket = 512;
2194 dwc->gadget.speed = USB_SPEED_SUPER;
2195 break;
2196 case DWC3_DCFG_HIGHSPEED:
2197 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2198 dwc->gadget.ep0->maxpacket = 64;
2199 dwc->gadget.speed = USB_SPEED_HIGH;
2200 break;
2201 case DWC3_DCFG_FULLSPEED2:
2202 case DWC3_DCFG_FULLSPEED1:
2203 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2204 dwc->gadget.ep0->maxpacket = 64;
2205 dwc->gadget.speed = USB_SPEED_FULL;
2206 break;
2207 case DWC3_DCFG_LOWSPEED:
2208 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2209 dwc->gadget.ep0->maxpacket = 8;
2210 dwc->gadget.speed = USB_SPEED_LOW;
2211 break;
2212 }
2213
2214 /* Enable USB2 LPM Capability */
2215
2216 if ((dwc->revision > DWC3_REVISION_194A)
2217 && (speed != DWC3_DCFG_SUPERSPEED)) {
2218 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2219 reg |= DWC3_DCFG_LPM_CAP;
2220 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2221
2222 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2223 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2224
2225 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2226
2227 /*
2228 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2229 * DCFG.LPMCap is set, core responses with an ACK and the
2230 * BESL value in the LPM token is less than or equal to LPM
2231 * NYET threshold.
2232 */
2233 if (dwc->revision < DWC3_REVISION_240A && dwc->has_lpm_erratum)
2234 WARN(true, "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2235
2236 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2237 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2238
2239 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2240 } else {
2241 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2242 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2243 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2244 }
2245
2246 dep = dwc->eps[0];
2247 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2248 false);
2249 if (ret) {
2250 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2251 return;
2252 }
2253
2254 dep = dwc->eps[1];
2255 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2256 false);
2257 if (ret) {
2258 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2259 return;
2260 }
2261
2262 /*
2263 * Configure PHY via GUSB3PIPECTLn if required.
2264 *
2265 * Update GTXFIFOSIZn
2266 *
2267 * In both cases reset values should be sufficient.
2268 */
2269 }
2270
dwc3_gadget_wakeup_interrupt(struct dwc3 * dwc)2271 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2272 {
2273 /*
2274 * TODO take core out of low power mode when that's
2275 * implemented.
2276 */
2277
2278 dwc->gadget_driver->resume(&dwc->gadget);
2279 }
2280
dwc3_gadget_linksts_change_interrupt(struct dwc3 * dwc,unsigned int evtinfo)2281 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2282 unsigned int evtinfo)
2283 {
2284 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2285 unsigned int pwropt;
2286
2287 /*
2288 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2289 * Hibernation mode enabled which would show up when device detects
2290 * host-initiated U3 exit.
2291 *
2292 * In that case, device will generate a Link State Change Interrupt
2293 * from U3 to RESUME which is only necessary if Hibernation is
2294 * configured in.
2295 *
2296 * There are no functional changes due to such spurious event and we
2297 * just need to ignore it.
2298 *
2299 * Refers to:
2300 *
2301 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2302 * operational mode
2303 */
2304 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2305 if ((dwc->revision < DWC3_REVISION_250A) &&
2306 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2307 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2308 (next == DWC3_LINK_STATE_RESUME)) {
2309 dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
2310 return;
2311 }
2312 }
2313
2314 /*
2315 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2316 * on the link partner, the USB session might do multiple entry/exit
2317 * of low power states before a transfer takes place.
2318 *
2319 * Due to this problem, we might experience lower throughput. The
2320 * suggested workaround is to disable DCTL[12:9] bits if we're
2321 * transitioning from U1/U2 to U0 and enable those bits again
2322 * after a transfer completes and there are no pending transfers
2323 * on any of the enabled endpoints.
2324 *
2325 * This is the first half of that workaround.
2326 *
2327 * Refers to:
2328 *
2329 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2330 * core send LGO_Ux entering U0
2331 */
2332 if (dwc->revision < DWC3_REVISION_183A) {
2333 if (next == DWC3_LINK_STATE_U0) {
2334 u32 u1u2;
2335 u32 reg;
2336
2337 switch (dwc->link_state) {
2338 case DWC3_LINK_STATE_U1:
2339 case DWC3_LINK_STATE_U2:
2340 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2341 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2342 | DWC3_DCTL_ACCEPTU2ENA
2343 | DWC3_DCTL_INITU1ENA
2344 | DWC3_DCTL_ACCEPTU1ENA);
2345
2346 if (!dwc->u1u2)
2347 dwc->u1u2 = reg & u1u2;
2348
2349 reg &= ~u1u2;
2350
2351 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2352 break;
2353 default:
2354 /* do nothing */
2355 break;
2356 }
2357 }
2358 }
2359
2360 switch (next) {
2361 case DWC3_LINK_STATE_U1:
2362 if (dwc->speed == USB_SPEED_SUPER)
2363 dwc3_suspend_gadget(dwc);
2364 break;
2365 case DWC3_LINK_STATE_U2:
2366 case DWC3_LINK_STATE_U3:
2367 dwc3_suspend_gadget(dwc);
2368 break;
2369 case DWC3_LINK_STATE_RESUME:
2370 dwc3_resume_gadget(dwc);
2371 break;
2372 default:
2373 /* do nothing */
2374 break;
2375 }
2376
2377 dwc->link_state = next;
2378 }
2379
dwc3_gadget_hibernation_interrupt(struct dwc3 * dwc,unsigned int evtinfo)2380 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2381 unsigned int evtinfo)
2382 {
2383 unsigned int is_ss = evtinfo & (1UL << 4);
2384
2385 /**
2386 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2387 * have a known issue which can cause USB CV TD.9.23 to fail
2388 * randomly.
2389 *
2390 * Because of this issue, core could generate bogus hibernation
2391 * events which SW needs to ignore.
2392 *
2393 * Refers to:
2394 *
2395 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2396 * Device Fallback from SuperSpeed
2397 */
2398 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2399 return;
2400
2401 /* enter hibernation here */
2402 }
2403
dwc3_gadget_interrupt(struct dwc3 * dwc,const struct dwc3_event_devt * event)2404 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2405 const struct dwc3_event_devt *event)
2406 {
2407 switch (event->type) {
2408 case DWC3_DEVICE_EVENT_DISCONNECT:
2409 dwc3_gadget_disconnect_interrupt(dwc);
2410 break;
2411 case DWC3_DEVICE_EVENT_RESET:
2412 dwc3_gadget_reset_interrupt(dwc);
2413 break;
2414 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2415 dwc3_gadget_conndone_interrupt(dwc);
2416 break;
2417 case DWC3_DEVICE_EVENT_WAKEUP:
2418 dwc3_gadget_wakeup_interrupt(dwc);
2419 break;
2420 case DWC3_DEVICE_EVENT_HIBER_REQ:
2421 if (!dwc->has_hibernation) {
2422 WARN(1 ,"unexpected hibernation event\n");
2423 break;
2424 }
2425 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2426 break;
2427 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2428 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2429 break;
2430 case DWC3_DEVICE_EVENT_EOPF:
2431 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2432 break;
2433 case DWC3_DEVICE_EVENT_SOF:
2434 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2435 break;
2436 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2437 dev_vdbg(dwc->dev, "Erratic Error\n");
2438 break;
2439 case DWC3_DEVICE_EVENT_CMD_CMPL:
2440 dev_vdbg(dwc->dev, "Command Complete\n");
2441 break;
2442 case DWC3_DEVICE_EVENT_OVERFLOW:
2443 dev_vdbg(dwc->dev, "Overflow\n");
2444 break;
2445 default:
2446 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2447 }
2448 }
2449
dwc3_process_event_entry(struct dwc3 * dwc,const union dwc3_event * event)2450 static void dwc3_process_event_entry(struct dwc3 *dwc,
2451 const union dwc3_event *event)
2452 {
2453 /* Endpoint IRQ, handle it and return early */
2454 if (event->type.is_devspec == 0) {
2455 /* depevt */
2456 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2457 }
2458
2459 switch (event->type.type) {
2460 case DWC3_EVENT_TYPE_DEV:
2461 dwc3_gadget_interrupt(dwc, &event->devt);
2462 break;
2463 /* REVISIT what to do with Carkit and I2C events ? */
2464 default:
2465 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2466 }
2467 }
2468
dwc3_process_event_buf(struct dwc3 * dwc,u32 buf)2469 static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2470 {
2471 struct dwc3_event_buffer *evt;
2472 irqreturn_t ret = IRQ_NONE;
2473 int left;
2474 u32 reg;
2475
2476 evt = dwc->ev_buffs[buf];
2477 left = evt->count;
2478
2479 if (!(evt->flags & DWC3_EVENT_PENDING))
2480 return IRQ_NONE;
2481
2482 while (left > 0) {
2483 union dwc3_event event;
2484
2485 event.raw = *(u32 *) (evt->buf + evt->lpos);
2486
2487 dwc3_process_event_entry(dwc, &event);
2488
2489 /*
2490 * FIXME we wrap around correctly to the next entry as
2491 * almost all entries are 4 bytes in size. There is one
2492 * entry which has 12 bytes which is a regular entry
2493 * followed by 8 bytes data. ATM I don't know how
2494 * things are organized if we get next to the a
2495 * boundary so I worry about that once we try to handle
2496 * that.
2497 */
2498 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2499 left -= 4;
2500
2501 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2502 }
2503
2504 evt->count = 0;
2505 evt->flags &= ~DWC3_EVENT_PENDING;
2506 ret = IRQ_HANDLED;
2507
2508 /* Unmask interrupt */
2509 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2510 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2511 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2512
2513 return ret;
2514 }
2515
dwc3_thread_interrupt(int irq,void * _dwc)2516 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2517 {
2518 struct dwc3 *dwc = _dwc;
2519 unsigned long flags = 0;
2520 irqreturn_t ret = IRQ_NONE;
2521 int i;
2522
2523 spin_lock_irqsave(&dwc->lock, flags);
2524
2525 for (i = 0; i < dwc->num_event_buffers; i++)
2526 ret |= dwc3_process_event_buf(dwc, i);
2527
2528 spin_unlock_irqrestore(&dwc->lock, flags);
2529
2530 return ret;
2531 }
2532
dwc3_check_event_buf(struct dwc3 * dwc,u32 buf)2533 static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
2534 {
2535 struct dwc3_event_buffer *evt;
2536 u32 count;
2537 u32 reg;
2538
2539 evt = dwc->ev_buffs[buf];
2540 dwc3_invalidate_cache((uintptr_t)evt->buf, evt->length);
2541
2542 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2543 count &= DWC3_GEVNTCOUNT_MASK;
2544 if (!count)
2545 return IRQ_NONE;
2546
2547 evt->count = count;
2548 evt->flags |= DWC3_EVENT_PENDING;
2549
2550 /* Mask interrupt */
2551 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2552 reg |= DWC3_GEVNTSIZ_INTMASK;
2553 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2554
2555 return IRQ_WAKE_THREAD;
2556 }
2557
dwc3_interrupt(int irq,void * _dwc)2558 static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2559 {
2560 struct dwc3 *dwc = _dwc;
2561 int i;
2562 irqreturn_t ret = IRQ_NONE;
2563
2564 spin_lock(&dwc->lock);
2565
2566 for (i = 0; i < dwc->num_event_buffers; i++) {
2567 irqreturn_t status;
2568
2569 status = dwc3_check_event_buf(dwc, i);
2570 if (status == IRQ_WAKE_THREAD)
2571 ret = status;
2572 }
2573
2574 spin_unlock(&dwc->lock);
2575
2576 return ret;
2577 }
2578
2579 /**
2580 * dwc3_gadget_init - Initializes gadget related registers
2581 * @dwc: pointer to our controller context structure
2582 *
2583 * Returns 0 on success otherwise negative errno.
2584 */
dwc3_gadget_init(struct dwc3 * dwc)2585 int dwc3_gadget_init(struct dwc3 *dwc)
2586 {
2587 int ret;
2588
2589 dwc->ctrl_req = dma_alloc_coherent(sizeof(*dwc->ctrl_req),
2590 (unsigned long *)&dwc->ctrl_req_addr);
2591 if (!dwc->ctrl_req) {
2592 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2593 ret = -ENOMEM;
2594 goto err0;
2595 }
2596
2597 dwc->ep0_trb = dma_alloc_coherent(sizeof(*dwc->ep0_trb) * 2,
2598 (unsigned long *)&dwc->ep0_trb_addr);
2599 if (!dwc->ep0_trb) {
2600 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2601 ret = -ENOMEM;
2602 goto err1;
2603 }
2604
2605 dwc->setup_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
2606 DWC3_EP0_BOUNCE_SIZE);
2607 if (!dwc->setup_buf) {
2608 ret = -ENOMEM;
2609 goto err2;
2610 }
2611
2612 dwc->ep0_bounce = dma_alloc_coherent(DWC3_EP0_BOUNCE_SIZE,
2613 (unsigned long *)&dwc->ep0_bounce_addr);
2614 if (!dwc->ep0_bounce) {
2615 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2616 ret = -ENOMEM;
2617 goto err3;
2618 }
2619
2620 dwc->gadget.ops = &dwc3_gadget_ops;
2621 dwc->gadget.max_speed = dwc->maximum_speed;
2622 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2623 dwc->gadget.name = "dwc3-gadget";
2624
2625 /*
2626 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2627 * on ep out.
2628 */
2629 dwc->gadget.quirk_ep_out_aligned_size = true;
2630
2631 /*
2632 * REVISIT: Here we should clear all pending IRQs to be
2633 * sure we're starting from a well known location.
2634 */
2635
2636 ret = dwc3_gadget_init_endpoints(dwc);
2637 if (ret)
2638 goto err4;
2639
2640 ret = usb_add_gadget_udc((struct device *)dwc->dev, &dwc->gadget);
2641 if (ret) {
2642 dev_err(dwc->dev, "failed to register udc\n");
2643 goto err4;
2644 }
2645
2646 return 0;
2647
2648 err4:
2649 dwc3_gadget_free_endpoints(dwc);
2650 dma_free_coherent(dwc->ep0_bounce);
2651
2652 err3:
2653 kfree(dwc->setup_buf);
2654
2655 err2:
2656 dma_free_coherent(dwc->ep0_trb);
2657
2658 err1:
2659 dma_free_coherent(dwc->ctrl_req);
2660
2661 err0:
2662 return ret;
2663 }
2664
2665 /* -------------------------------------------------------------------------- */
2666
dwc3_gadget_exit(struct dwc3 * dwc)2667 void dwc3_gadget_exit(struct dwc3 *dwc)
2668 {
2669 usb_del_gadget_udc(&dwc->gadget);
2670
2671 dwc3_gadget_free_endpoints(dwc);
2672
2673 dma_free_coherent(dwc->ep0_bounce);
2674
2675 kfree(dwc->setup_buf);
2676
2677 dma_free_coherent(dwc->ep0_trb);
2678
2679 dma_free_coherent(dwc->ctrl_req);
2680 }
2681
2682 /**
2683 * dwc3_gadget_uboot_handle_interrupt - handle dwc3 gadget interrupt
2684 * @dwc: struct dwce *
2685 *
2686 * Handles ep0 and gadget interrupt
2687 *
2688 * Should be called from dwc3 core.
2689 */
dwc3_gadget_uboot_handle_interrupt(struct dwc3 * dwc)2690 void dwc3_gadget_uboot_handle_interrupt(struct dwc3 *dwc)
2691 {
2692 int ret = dwc3_interrupt(0, dwc);
2693
2694 if (ret == IRQ_WAKE_THREAD) {
2695 int i;
2696 struct dwc3_event_buffer *evt;
2697
2698 dwc3_thread_interrupt(0, dwc);
2699
2700 /* Clean + Invalidate the buffers after touching them */
2701 for (i = 0; i < dwc->num_event_buffers; i++) {
2702 evt = dwc->ev_buffs[i];
2703 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
2704 }
2705 }
2706 }
2707