]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/usb/dwc3/gadget.c
b82a8885747d6b99f515ee450050346312de2b99
[mirror_ubuntu-hirsute-kernel.git] / drivers / usb / dwc3 / gadget.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 *
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/list.h>
20 #include <linux/dma-mapping.h>
21
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
24
25 #include "debug.h"
26 #include "core.h"
27 #include "gadget.h"
28 #include "io.h"
29
30 #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
31 & ~((d)->interval - 1))
32
33 /**
34 * dwc3_gadget_set_test_mode - enables usb2 test modes
35 * @dwc: pointer to our context structure
36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37 *
38 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
40 */
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 */
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 */
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 return -ETIMEDOUT;
137 }
138
139 /**
140 * dwc3_ep_inc_trb - increment a trb index.
141 * @index: Pointer to the TRB index to increment.
142 *
143 * The index should never point to the link TRB. After incrementing,
144 * if it is point to the link TRB, wrap around to the beginning. The
145 * link TRB is always at the last TRB entry.
146 */
147 static void dwc3_ep_inc_trb(u8 *index)
148 {
149 (*index)++;
150 if (*index == (DWC3_TRB_NUM - 1))
151 *index = 0;
152 }
153
154 /**
155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156 * @dep: The endpoint whose enqueue pointer we're incrementing
157 */
158 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
159 {
160 dwc3_ep_inc_trb(&dep->trb_enqueue);
161 }
162
163 /**
164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165 * @dep: The endpoint whose enqueue pointer we're incrementing
166 */
167 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
168 {
169 dwc3_ep_inc_trb(&dep->trb_dequeue);
170 }
171
172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
173 struct dwc3_request *req, int status)
174 {
175 struct dwc3 *dwc = dep->dwc;
176
177 list_del(&req->list);
178 req->remaining = 0;
179 req->needs_extra_trb = false;
180
181 if (req->request.status == -EINPROGRESS)
182 req->request.status = status;
183
184 if (req->trb)
185 usb_gadget_unmap_request_by_dev(dwc->sysdev,
186 &req->request, req->direction);
187
188 req->trb = NULL;
189 trace_dwc3_gadget_giveback(req);
190
191 if (dep->number > 1)
192 pm_runtime_put(dwc->dev);
193 }
194
195 /**
196 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
197 * @dep: The endpoint to whom the request belongs to
198 * @req: The request we're giving back
199 * @status: completion code for the request
200 *
201 * Must be called with controller's lock held and interrupts disabled. This
202 * function will unmap @req and call its ->complete() callback to notify upper
203 * layers that it has completed.
204 */
205 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
206 int status)
207 {
208 struct dwc3 *dwc = dep->dwc;
209
210 dwc3_gadget_del_and_unmap_request(dep, req, status);
211 req->status = DWC3_REQUEST_STATUS_COMPLETED;
212
213 spin_unlock(&dwc->lock);
214 usb_gadget_giveback_request(&dep->endpoint, &req->request);
215 spin_lock(&dwc->lock);
216 }
217
218 /**
219 * dwc3_send_gadget_generic_command - issue a generic command for the controller
220 * @dwc: pointer to the controller context
221 * @cmd: the command to be issued
222 * @param: command parameter
223 *
224 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
225 * and wait for its completion.
226 */
227 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
228 {
229 u32 timeout = 500;
230 int status = 0;
231 int ret = 0;
232 u32 reg;
233
234 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
235 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
236
237 do {
238 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
239 if (!(reg & DWC3_DGCMD_CMDACT)) {
240 status = DWC3_DGCMD_STATUS(reg);
241 if (status)
242 ret = -EINVAL;
243 break;
244 }
245 } while (--timeout);
246
247 if (!timeout) {
248 ret = -ETIMEDOUT;
249 status = -ETIMEDOUT;
250 }
251
252 trace_dwc3_gadget_generic_cmd(cmd, param, status);
253
254 return ret;
255 }
256
257 static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
258
259 /**
260 * dwc3_send_gadget_ep_cmd - issue an endpoint command
261 * @dep: the endpoint to which the command is going to be issued
262 * @cmd: the command to be issued
263 * @params: parameters to the command
264 *
265 * Caller should handle locking. This function will issue @cmd with given
266 * @params to @dep and wait for its completion.
267 */
268 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
269 struct dwc3_gadget_ep_cmd_params *params)
270 {
271 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
272 struct dwc3 *dwc = dep->dwc;
273 u32 timeout = 1000;
274 u32 saved_config = 0;
275 u32 reg;
276
277 int cmd_status = 0;
278 int ret = -EINVAL;
279
280 /*
281 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
282 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
283 * endpoint command.
284 *
285 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
286 * settings. Restore them after the command is completed.
287 *
288 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
289 */
290 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
291 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
292 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
293 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
294 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
295 }
296
297 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
298 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
299 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
300 }
301
302 if (saved_config)
303 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
304 }
305
306 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
307 int needs_wakeup;
308
309 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
310 dwc->link_state == DWC3_LINK_STATE_U2 ||
311 dwc->link_state == DWC3_LINK_STATE_U3);
312
313 if (unlikely(needs_wakeup)) {
314 ret = __dwc3_gadget_wakeup(dwc);
315 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
316 ret);
317 }
318 }
319
320 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
321 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
322 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
323
324 /*
325 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
326 * not relying on XferNotReady, we can make use of a special "No
327 * Response Update Transfer" command where we should clear both CmdAct
328 * and CmdIOC bits.
329 *
330 * With this, we don't need to wait for command completion and can
331 * straight away issue further commands to the endpoint.
332 *
333 * NOTICE: We're making an assumption that control endpoints will never
334 * make use of Update Transfer command. This is a safe assumption
335 * because we can never have more than one request at a time with
336 * Control Endpoints. If anybody changes that assumption, this chunk
337 * needs to be updated accordingly.
338 */
339 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
340 !usb_endpoint_xfer_isoc(desc))
341 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
342 else
343 cmd |= DWC3_DEPCMD_CMDACT;
344
345 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
346 do {
347 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
348 if (!(reg & DWC3_DEPCMD_CMDACT)) {
349 cmd_status = DWC3_DEPCMD_STATUS(reg);
350
351 switch (cmd_status) {
352 case 0:
353 ret = 0;
354 break;
355 case DEPEVT_TRANSFER_NO_RESOURCE:
356 ret = -EINVAL;
357 break;
358 case DEPEVT_TRANSFER_BUS_EXPIRY:
359 /*
360 * SW issues START TRANSFER command to
361 * isochronous ep with future frame interval. If
362 * future interval time has already passed when
363 * core receives the command, it will respond
364 * with an error status of 'Bus Expiry'.
365 *
366 * Instead of always returning -EINVAL, let's
367 * give a hint to the gadget driver that this is
368 * the case by returning -EAGAIN.
369 */
370 ret = -EAGAIN;
371 break;
372 default:
373 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
374 }
375
376 break;
377 }
378 } while (--timeout);
379
380 if (timeout == 0) {
381 ret = -ETIMEDOUT;
382 cmd_status = -ETIMEDOUT;
383 }
384
385 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
386
387 if (ret == 0 && DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
388 dep->flags |= DWC3_EP_TRANSFER_STARTED;
389 dwc3_gadget_ep_get_transfer_index(dep);
390 }
391
392 if (saved_config) {
393 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
394 reg |= saved_config;
395 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
396 }
397
398 return ret;
399 }
400
401 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
402 {
403 struct dwc3 *dwc = dep->dwc;
404 struct dwc3_gadget_ep_cmd_params params;
405 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
406
407 /*
408 * As of core revision 2.60a the recommended programming model
409 * is to set the ClearPendIN bit when issuing a Clear Stall EP
410 * command for IN endpoints. This is to prevent an issue where
411 * some (non-compliant) hosts may not send ACK TPs for pending
412 * IN transfers due to a mishandled error condition. Synopsys
413 * STAR 9000614252.
414 */
415 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
416 (dwc->gadget.speed >= USB_SPEED_SUPER))
417 cmd |= DWC3_DEPCMD_CLEARPENDIN;
418
419 memset(&params, 0, sizeof(params));
420
421 return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
422 }
423
424 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
425 struct dwc3_trb *trb)
426 {
427 u32 offset = (char *) trb - (char *) dep->trb_pool;
428
429 return dep->trb_pool_dma + offset;
430 }
431
432 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
433 {
434 struct dwc3 *dwc = dep->dwc;
435
436 if (dep->trb_pool)
437 return 0;
438
439 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
440 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
441 &dep->trb_pool_dma, GFP_KERNEL);
442 if (!dep->trb_pool) {
443 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
444 dep->name);
445 return -ENOMEM;
446 }
447
448 return 0;
449 }
450
451 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
452 {
453 struct dwc3 *dwc = dep->dwc;
454
455 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
456 dep->trb_pool, dep->trb_pool_dma);
457
458 dep->trb_pool = NULL;
459 dep->trb_pool_dma = 0;
460 }
461
462 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
463 {
464 struct dwc3_gadget_ep_cmd_params params;
465
466 memset(&params, 0x00, sizeof(params));
467
468 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
469
470 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
471 &params);
472 }
473
474 /**
475 * dwc3_gadget_start_config - configure ep resources
476 * @dep: endpoint that is being enabled
477 *
478 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
479 * completion, it will set Transfer Resource for all available endpoints.
480 *
481 * The assignment of transfer resources cannot perfectly follow the data book
482 * due to the fact that the controller driver does not have all knowledge of the
483 * configuration in advance. It is given this information piecemeal by the
484 * composite gadget framework after every SET_CONFIGURATION and
485 * SET_INTERFACE. Trying to follow the databook programming model in this
486 * scenario can cause errors. For two reasons:
487 *
488 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
489 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
490 * incorrect in the scenario of multiple interfaces.
491 *
492 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
493 * endpoint on alt setting (8.1.6).
494 *
495 * The following simplified method is used instead:
496 *
497 * All hardware endpoints can be assigned a transfer resource and this setting
498 * will stay persistent until either a core reset or hibernation. So whenever we
499 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
500 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
501 * guaranteed that there are as many transfer resources as endpoints.
502 *
503 * This function is called for each endpoint when it is being enabled but is
504 * triggered only when called for EP0-out, which always happens first, and which
505 * should only happen in one of the above conditions.
506 */
507 static int dwc3_gadget_start_config(struct dwc3_ep *dep)
508 {
509 struct dwc3_gadget_ep_cmd_params params;
510 struct dwc3 *dwc;
511 u32 cmd;
512 int i;
513 int ret;
514
515 if (dep->number)
516 return 0;
517
518 memset(&params, 0x00, sizeof(params));
519 cmd = DWC3_DEPCMD_DEPSTARTCFG;
520 dwc = dep->dwc;
521
522 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
523 if (ret)
524 return ret;
525
526 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
527 struct dwc3_ep *dep = dwc->eps[i];
528
529 if (!dep)
530 continue;
531
532 ret = dwc3_gadget_set_xfer_resource(dep);
533 if (ret)
534 return ret;
535 }
536
537 return 0;
538 }
539
540 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
541 {
542 const struct usb_ss_ep_comp_descriptor *comp_desc;
543 const struct usb_endpoint_descriptor *desc;
544 struct dwc3_gadget_ep_cmd_params params;
545 struct dwc3 *dwc = dep->dwc;
546
547 comp_desc = dep->endpoint.comp_desc;
548 desc = dep->endpoint.desc;
549
550 memset(&params, 0x00, sizeof(params));
551
552 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
553 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
554
555 /* Burst size is only needed in SuperSpeed mode */
556 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
557 u32 burst = dep->endpoint.maxburst;
558 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
559 }
560
561 params.param0 |= action;
562 if (action == DWC3_DEPCFG_ACTION_RESTORE)
563 params.param2 |= dep->saved_state;
564
565 if (usb_endpoint_xfer_control(desc))
566 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
567
568 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
569 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
570
571 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
572 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
573 | DWC3_DEPCFG_STREAM_EVENT_EN;
574 dep->stream_capable = true;
575 }
576
577 if (!usb_endpoint_xfer_control(desc))
578 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
579
580 /*
581 * We are doing 1:1 mapping for endpoints, meaning
582 * Physical Endpoints 2 maps to Logical Endpoint 2 and
583 * so on. We consider the direction bit as part of the physical
584 * endpoint number. So USB endpoint 0x81 is 0x03.
585 */
586 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
587
588 /*
589 * We must use the lower 16 TX FIFOs even though
590 * HW might have more
591 */
592 if (dep->direction)
593 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
594
595 if (desc->bInterval) {
596 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
597 dep->interval = 1 << (desc->bInterval - 1);
598 }
599
600 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
601 }
602
603 /**
604 * __dwc3_gadget_ep_enable - initializes a hw endpoint
605 * @dep: endpoint to be initialized
606 * @action: one of INIT, MODIFY or RESTORE
607 *
608 * Caller should take care of locking. Execute all necessary commands to
609 * initialize a HW endpoint so it can be used by a gadget driver.
610 */
611 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
612 {
613 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
614 struct dwc3 *dwc = dep->dwc;
615
616 u32 reg;
617 int ret;
618
619 if (!(dep->flags & DWC3_EP_ENABLED)) {
620 ret = dwc3_gadget_start_config(dep);
621 if (ret)
622 return ret;
623 }
624
625 ret = dwc3_gadget_set_ep_config(dep, action);
626 if (ret)
627 return ret;
628
629 if (!(dep->flags & DWC3_EP_ENABLED)) {
630 struct dwc3_trb *trb_st_hw;
631 struct dwc3_trb *trb_link;
632
633 dep->type = usb_endpoint_type(desc);
634 dep->flags |= DWC3_EP_ENABLED;
635 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
636
637 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
638 reg |= DWC3_DALEPENA_EP(dep->number);
639 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
640
641 if (usb_endpoint_xfer_control(desc))
642 goto out;
643
644 /* Initialize the TRB ring */
645 dep->trb_dequeue = 0;
646 dep->trb_enqueue = 0;
647 memset(dep->trb_pool, 0,
648 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
649
650 /* Link TRB. The HWO bit is never reset */
651 trb_st_hw = &dep->trb_pool[0];
652
653 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
654 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
656 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
657 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
658 }
659
660 /*
661 * Issue StartTransfer here with no-op TRB so we can always rely on No
662 * Response Update Transfer command.
663 */
664 if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
665 usb_endpoint_xfer_int(desc)) {
666 struct dwc3_gadget_ep_cmd_params params;
667 struct dwc3_trb *trb;
668 dma_addr_t trb_dma;
669 u32 cmd;
670
671 memset(&params, 0, sizeof(params));
672 trb = &dep->trb_pool[0];
673 trb_dma = dwc3_trb_dma_offset(dep, trb);
674
675 params.param0 = upper_32_bits(trb_dma);
676 params.param1 = lower_32_bits(trb_dma);
677
678 cmd = DWC3_DEPCMD_STARTTRANSFER;
679
680 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
681 if (ret < 0)
682 return ret;
683 }
684
685 out:
686 trace_dwc3_gadget_ep_enable(dep);
687
688 return 0;
689 }
690
691 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force);
692 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
693 {
694 struct dwc3_request *req;
695
696 dwc3_stop_active_transfer(dep, true);
697
698 /* - giveback all requests to gadget driver */
699 while (!list_empty(&dep->started_list)) {
700 req = next_request(&dep->started_list);
701
702 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
703 }
704
705 while (!list_empty(&dep->pending_list)) {
706 req = next_request(&dep->pending_list);
707
708 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
709 }
710 }
711
712 /**
713 * __dwc3_gadget_ep_disable - disables a hw endpoint
714 * @dep: the endpoint to disable
715 *
716 * This function undoes what __dwc3_gadget_ep_enable did and also removes
717 * requests which are currently being processed by the hardware and those which
718 * are not yet scheduled.
719 *
720 * Caller should take care of locking.
721 */
722 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
723 {
724 struct dwc3 *dwc = dep->dwc;
725 u32 reg;
726
727 trace_dwc3_gadget_ep_disable(dep);
728
729 dwc3_remove_requests(dwc, dep);
730
731 /* make sure HW endpoint isn't stalled */
732 if (dep->flags & DWC3_EP_STALL)
733 __dwc3_gadget_ep_set_halt(dep, 0, false);
734
735 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
736 reg &= ~DWC3_DALEPENA_EP(dep->number);
737 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
738
739 dep->stream_capable = false;
740 dep->type = 0;
741 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
742
743 /* Clear out the ep descriptors for non-ep0 */
744 if (dep->number > 1) {
745 dep->endpoint.comp_desc = NULL;
746 dep->endpoint.desc = NULL;
747 }
748
749 return 0;
750 }
751
752 /* -------------------------------------------------------------------------- */
753
754 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
755 const struct usb_endpoint_descriptor *desc)
756 {
757 return -EINVAL;
758 }
759
760 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
761 {
762 return -EINVAL;
763 }
764
765 /* -------------------------------------------------------------------------- */
766
767 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
768 const struct usb_endpoint_descriptor *desc)
769 {
770 struct dwc3_ep *dep;
771 struct dwc3 *dwc;
772 unsigned long flags;
773 int ret;
774
775 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
776 pr_debug("dwc3: invalid parameters\n");
777 return -EINVAL;
778 }
779
780 if (!desc->wMaxPacketSize) {
781 pr_debug("dwc3: missing wMaxPacketSize\n");
782 return -EINVAL;
783 }
784
785 dep = to_dwc3_ep(ep);
786 dwc = dep->dwc;
787
788 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
789 "%s is already enabled\n",
790 dep->name))
791 return 0;
792
793 spin_lock_irqsave(&dwc->lock, flags);
794 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
795 spin_unlock_irqrestore(&dwc->lock, flags);
796
797 return ret;
798 }
799
800 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
801 {
802 struct dwc3_ep *dep;
803 struct dwc3 *dwc;
804 unsigned long flags;
805 int ret;
806
807 if (!ep) {
808 pr_debug("dwc3: invalid parameters\n");
809 return -EINVAL;
810 }
811
812 dep = to_dwc3_ep(ep);
813 dwc = dep->dwc;
814
815 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
816 "%s is already disabled\n",
817 dep->name))
818 return 0;
819
820 spin_lock_irqsave(&dwc->lock, flags);
821 ret = __dwc3_gadget_ep_disable(dep);
822 spin_unlock_irqrestore(&dwc->lock, flags);
823
824 return ret;
825 }
826
827 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
828 gfp_t gfp_flags)
829 {
830 struct dwc3_request *req;
831 struct dwc3_ep *dep = to_dwc3_ep(ep);
832
833 req = kzalloc(sizeof(*req), gfp_flags);
834 if (!req)
835 return NULL;
836
837 req->direction = dep->direction;
838 req->epnum = dep->number;
839 req->dep = dep;
840 req->status = DWC3_REQUEST_STATUS_UNKNOWN;
841
842 trace_dwc3_alloc_request(req);
843
844 return &req->request;
845 }
846
847 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
848 struct usb_request *request)
849 {
850 struct dwc3_request *req = to_dwc3_request(request);
851
852 trace_dwc3_free_request(req);
853 kfree(req);
854 }
855
856 /**
857 * dwc3_ep_prev_trb - returns the previous TRB in the ring
858 * @dep: The endpoint with the TRB ring
859 * @index: The index of the current TRB in the ring
860 *
861 * Returns the TRB prior to the one pointed to by the index. If the
862 * index is 0, we will wrap backwards, skip the link TRB, and return
863 * the one just before that.
864 */
865 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
866 {
867 u8 tmp = index;
868
869 if (!tmp)
870 tmp = DWC3_TRB_NUM - 1;
871
872 return &dep->trb_pool[tmp - 1];
873 }
874
875 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
876 {
877 struct dwc3_trb *tmp;
878 u8 trbs_left;
879
880 /*
881 * If enqueue & dequeue are equal than it is either full or empty.
882 *
883 * One way to know for sure is if the TRB right before us has HWO bit
884 * set or not. If it has, then we're definitely full and can't fit any
885 * more transfers in our ring.
886 */
887 if (dep->trb_enqueue == dep->trb_dequeue) {
888 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
889 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
890 return 0;
891
892 return DWC3_TRB_NUM - 1;
893 }
894
895 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
896 trbs_left &= (DWC3_TRB_NUM - 1);
897
898 if (dep->trb_dequeue < dep->trb_enqueue)
899 trbs_left--;
900
901 return trbs_left;
902 }
903
904 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
905 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
906 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
907 {
908 struct dwc3 *dwc = dep->dwc;
909 struct usb_gadget *gadget = &dwc->gadget;
910 enum usb_device_speed speed = gadget->speed;
911
912 trb->size = DWC3_TRB_SIZE_LENGTH(length);
913 trb->bpl = lower_32_bits(dma);
914 trb->bph = upper_32_bits(dma);
915
916 switch (usb_endpoint_type(dep->endpoint.desc)) {
917 case USB_ENDPOINT_XFER_CONTROL:
918 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
919 break;
920
921 case USB_ENDPOINT_XFER_ISOC:
922 if (!node) {
923 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
924
925 /*
926 * USB Specification 2.0 Section 5.9.2 states that: "If
927 * there is only a single transaction in the microframe,
928 * only a DATA0 data packet PID is used. If there are
929 * two transactions per microframe, DATA1 is used for
930 * the first transaction data packet and DATA0 is used
931 * for the second transaction data packet. If there are
932 * three transactions per microframe, DATA2 is used for
933 * the first transaction data packet, DATA1 is used for
934 * the second, and DATA0 is used for the third."
935 *
936 * IOW, we should satisfy the following cases:
937 *
938 * 1) length <= maxpacket
939 * - DATA0
940 *
941 * 2) maxpacket < length <= (2 * maxpacket)
942 * - DATA1, DATA0
943 *
944 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
945 * - DATA2, DATA1, DATA0
946 */
947 if (speed == USB_SPEED_HIGH) {
948 struct usb_ep *ep = &dep->endpoint;
949 unsigned int mult = 2;
950 unsigned int maxp = usb_endpoint_maxp(ep->desc);
951
952 if (length <= (2 * maxp))
953 mult--;
954
955 if (length <= maxp)
956 mult--;
957
958 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
959 }
960 } else {
961 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
962 }
963
964 /* always enable Interrupt on Missed ISOC */
965 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
966 break;
967
968 case USB_ENDPOINT_XFER_BULK:
969 case USB_ENDPOINT_XFER_INT:
970 trb->ctrl = DWC3_TRBCTL_NORMAL;
971 break;
972 default:
973 /*
974 * This is only possible with faulty memory because we
975 * checked it already :)
976 */
977 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
978 usb_endpoint_type(dep->endpoint.desc));
979 }
980
981 /*
982 * Enable Continue on Short Packet
983 * when endpoint is not a stream capable
984 */
985 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
986 if (!dep->stream_capable)
987 trb->ctrl |= DWC3_TRB_CTRL_CSP;
988
989 if (short_not_ok)
990 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
991 }
992
993 if ((!no_interrupt && !chain) ||
994 (dwc3_calc_trbs_left(dep) == 1))
995 trb->ctrl |= DWC3_TRB_CTRL_IOC;
996
997 if (chain)
998 trb->ctrl |= DWC3_TRB_CTRL_CHN;
999
1000 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1001 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1002
1003 trb->ctrl |= DWC3_TRB_CTRL_HWO;
1004
1005 dwc3_ep_inc_enq(dep);
1006
1007 trace_dwc3_prepare_trb(dep, trb);
1008 }
1009
1010 /**
1011 * dwc3_prepare_one_trb - setup one TRB from one request
1012 * @dep: endpoint for which this request is prepared
1013 * @req: dwc3_request pointer
1014 * @chain: should this TRB be chained to the next?
1015 * @node: only for isochronous endpoints. First TRB needs different type.
1016 */
1017 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1018 struct dwc3_request *req, unsigned chain, unsigned node)
1019 {
1020 struct dwc3_trb *trb;
1021 unsigned int length;
1022 dma_addr_t dma;
1023 unsigned stream_id = req->request.stream_id;
1024 unsigned short_not_ok = req->request.short_not_ok;
1025 unsigned no_interrupt = req->request.no_interrupt;
1026
1027 if (req->request.num_sgs > 0) {
1028 length = sg_dma_len(req->start_sg);
1029 dma = sg_dma_address(req->start_sg);
1030 } else {
1031 length = req->request.length;
1032 dma = req->request.dma;
1033 }
1034
1035 trb = &dep->trb_pool[dep->trb_enqueue];
1036
1037 if (!req->trb) {
1038 dwc3_gadget_move_started_request(req);
1039 req->trb = trb;
1040 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1041 }
1042
1043 req->num_trbs++;
1044
1045 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1046 stream_id, short_not_ok, no_interrupt);
1047 }
1048
1049 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1050 struct dwc3_request *req)
1051 {
1052 struct scatterlist *sg = req->start_sg;
1053 struct scatterlist *s;
1054 int i;
1055
1056 unsigned int remaining = req->request.num_mapped_sgs
1057 - req->num_queued_sgs;
1058
1059 for_each_sg(sg, s, remaining, i) {
1060 unsigned int length = req->request.length;
1061 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1062 unsigned int rem = length % maxp;
1063 unsigned chain = true;
1064
1065 if (sg_is_last(s))
1066 chain = false;
1067
1068 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1069 struct dwc3 *dwc = dep->dwc;
1070 struct dwc3_trb *trb;
1071
1072 req->needs_extra_trb = true;
1073
1074 /* prepare normal TRB */
1075 dwc3_prepare_one_trb(dep, req, true, i);
1076
1077 /* Now prepare one extra TRB to align transfer size */
1078 trb = &dep->trb_pool[dep->trb_enqueue];
1079 req->num_trbs++;
1080 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1081 maxp - rem, false, 1,
1082 req->request.stream_id,
1083 req->request.short_not_ok,
1084 req->request.no_interrupt);
1085 } else {
1086 dwc3_prepare_one_trb(dep, req, chain, i);
1087 }
1088
1089 /*
1090 * There can be a situation where all sgs in sglist are not
1091 * queued because of insufficient trb number. To handle this
1092 * case, update start_sg to next sg to be queued, so that
1093 * we have free trbs we can continue queuing from where we
1094 * previously stopped
1095 */
1096 if (chain)
1097 req->start_sg = sg_next(s);
1098
1099 req->num_queued_sgs++;
1100
1101 if (!dwc3_calc_trbs_left(dep))
1102 break;
1103 }
1104 }
1105
1106 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1107 struct dwc3_request *req)
1108 {
1109 unsigned int length = req->request.length;
1110 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1111 unsigned int rem = length % maxp;
1112
1113 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1114 struct dwc3 *dwc = dep->dwc;
1115 struct dwc3_trb *trb;
1116
1117 req->needs_extra_trb = true;
1118
1119 /* prepare normal TRB */
1120 dwc3_prepare_one_trb(dep, req, true, 0);
1121
1122 /* Now prepare one extra TRB to align transfer size */
1123 trb = &dep->trb_pool[dep->trb_enqueue];
1124 req->num_trbs++;
1125 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1126 false, 1, req->request.stream_id,
1127 req->request.short_not_ok,
1128 req->request.no_interrupt);
1129 } else if (req->request.zero && req->request.length &&
1130 (IS_ALIGNED(req->request.length, maxp))) {
1131 struct dwc3 *dwc = dep->dwc;
1132 struct dwc3_trb *trb;
1133
1134 req->needs_extra_trb = true;
1135
1136 /* prepare normal TRB */
1137 dwc3_prepare_one_trb(dep, req, true, 0);
1138
1139 /* Now prepare one extra TRB to handle ZLP */
1140 trb = &dep->trb_pool[dep->trb_enqueue];
1141 req->num_trbs++;
1142 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1143 false, 1, req->request.stream_id,
1144 req->request.short_not_ok,
1145 req->request.no_interrupt);
1146 } else {
1147 dwc3_prepare_one_trb(dep, req, false, 0);
1148 }
1149 }
1150
1151 /*
1152 * dwc3_prepare_trbs - setup TRBs from requests
1153 * @dep: endpoint for which requests are being prepared
1154 *
1155 * The function goes through the requests list and sets up TRBs for the
1156 * transfers. The function returns once there are no more TRBs available or
1157 * it runs out of requests.
1158 */
1159 static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1160 {
1161 struct dwc3_request *req, *n;
1162
1163 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1164
1165 /*
1166 * We can get in a situation where there's a request in the started list
1167 * but there weren't enough TRBs to fully kick it in the first time
1168 * around, so it has been waiting for more TRBs to be freed up.
1169 *
1170 * In that case, we should check if we have a request with pending_sgs
1171 * in the started list and prepare TRBs for that request first,
1172 * otherwise we will prepare TRBs completely out of order and that will
1173 * break things.
1174 */
1175 list_for_each_entry(req, &dep->started_list, list) {
1176 if (req->num_pending_sgs > 0)
1177 dwc3_prepare_one_trb_sg(dep, req);
1178
1179 if (!dwc3_calc_trbs_left(dep))
1180 return;
1181 }
1182
1183 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1184 struct dwc3 *dwc = dep->dwc;
1185 int ret;
1186
1187 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1188 dep->direction);
1189 if (ret)
1190 return;
1191
1192 req->sg = req->request.sg;
1193 req->start_sg = req->sg;
1194 req->num_queued_sgs = 0;
1195 req->num_pending_sgs = req->request.num_mapped_sgs;
1196
1197 if (req->num_pending_sgs > 0)
1198 dwc3_prepare_one_trb_sg(dep, req);
1199 else
1200 dwc3_prepare_one_trb_linear(dep, req);
1201
1202 if (!dwc3_calc_trbs_left(dep))
1203 return;
1204 }
1205 }
1206
1207 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1208 {
1209 struct dwc3_gadget_ep_cmd_params params;
1210 struct dwc3_request *req;
1211 int starting;
1212 int ret;
1213 u32 cmd;
1214
1215 if (!dwc3_calc_trbs_left(dep))
1216 return 0;
1217
1218 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1219
1220 dwc3_prepare_trbs(dep);
1221 req = next_request(&dep->started_list);
1222 if (!req) {
1223 dep->flags |= DWC3_EP_PENDING_REQUEST;
1224 return 0;
1225 }
1226
1227 memset(&params, 0, sizeof(params));
1228
1229 if (starting) {
1230 params.param0 = upper_32_bits(req->trb_dma);
1231 params.param1 = lower_32_bits(req->trb_dma);
1232 cmd = DWC3_DEPCMD_STARTTRANSFER;
1233
1234 if (dep->stream_capable)
1235 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1236
1237 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1238 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1239 } else {
1240 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1241 DWC3_DEPCMD_PARAM(dep->resource_index);
1242 }
1243
1244 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1245 if (ret < 0) {
1246 /*
1247 * FIXME we need to iterate over the list of requests
1248 * here and stop, unmap, free and del each of the linked
1249 * requests instead of what we do now.
1250 */
1251 if (req->trb)
1252 memset(req->trb, 0, sizeof(struct dwc3_trb));
1253 dwc3_gadget_del_and_unmap_request(dep, req, ret);
1254 return ret;
1255 }
1256
1257 return 0;
1258 }
1259
1260 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1261 {
1262 u32 reg;
1263
1264 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1265 return DWC3_DSTS_SOFFN(reg);
1266 }
1267
1268 /**
1269 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1270 * @dep: isoc endpoint
1271 *
1272 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1273 * microframe number reported by the XferNotReady event for the future frame
1274 * number to start the isoc transfer.
1275 *
1276 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1277 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1278 * XferNotReady event are invalid. The driver uses this number to schedule the
1279 * isochronous transfer and passes it to the START TRANSFER command. Because
1280 * this number is invalid, the command may fail. If BIT[15:14] matches the
1281 * internal 16-bit microframe, the START TRANSFER command will pass and the
1282 * transfer will start at the scheduled time, if it is off by 1, the command
1283 * will still pass, but the transfer will start 2 seconds in the future. For all
1284 * other conditions, the START TRANSFER command will fail with bus-expiry.
1285 *
1286 * In order to workaround this issue, we can test for the correct combination of
1287 * BIT[15:14] by sending START TRANSFER commands with different values of
1288 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1289 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1290 * As the result, within the 4 possible combinations for BIT[15:14], there will
1291 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1292 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1293 * value is the correct combination.
1294 *
1295 * Since there are only 4 outcomes and the results are ordered, we can simply
1296 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1297 * deduce the smaller successful combination.
1298 *
1299 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1300 * of BIT[15:14]. The correct combination is as follow:
1301 *
1302 * if test0 fails and test1 passes, BIT[15:14] is 'b01
1303 * if test0 fails and test1 fails, BIT[15:14] is 'b10
1304 * if test0 passes and test1 fails, BIT[15:14] is 'b11
1305 * if test0 passes and test1 passes, BIT[15:14] is 'b00
1306 *
1307 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1308 * endpoints.
1309 */
1310 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1311 {
1312 int cmd_status = 0;
1313 bool test0;
1314 bool test1;
1315
1316 while (dep->combo_num < 2) {
1317 struct dwc3_gadget_ep_cmd_params params;
1318 u32 test_frame_number;
1319 u32 cmd;
1320
1321 /*
1322 * Check if we can start isoc transfer on the next interval or
1323 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1324 */
1325 test_frame_number = dep->frame_number & 0x3fff;
1326 test_frame_number |= dep->combo_num << 14;
1327 test_frame_number += max_t(u32, 4, dep->interval);
1328
1329 params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1330 params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1331
1332 cmd = DWC3_DEPCMD_STARTTRANSFER;
1333 cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1334 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1335
1336 /* Redo if some other failure beside bus-expiry is received */
1337 if (cmd_status && cmd_status != -EAGAIN) {
1338 dep->start_cmd_status = 0;
1339 dep->combo_num = 0;
1340 return 0;
1341 }
1342
1343 /* Store the first test status */
1344 if (dep->combo_num == 0)
1345 dep->start_cmd_status = cmd_status;
1346
1347 dep->combo_num++;
1348
1349 /*
1350 * End the transfer if the START_TRANSFER command is successful
1351 * to wait for the next XferNotReady to test the command again
1352 */
1353 if (cmd_status == 0) {
1354 dwc3_stop_active_transfer(dep, true);
1355 return 0;
1356 }
1357 }
1358
1359 /* test0 and test1 are both completed at this point */
1360 test0 = (dep->start_cmd_status == 0);
1361 test1 = (cmd_status == 0);
1362
1363 if (!test0 && test1)
1364 dep->combo_num = 1;
1365 else if (!test0 && !test1)
1366 dep->combo_num = 2;
1367 else if (test0 && !test1)
1368 dep->combo_num = 3;
1369 else if (test0 && test1)
1370 dep->combo_num = 0;
1371
1372 dep->frame_number &= 0x3fff;
1373 dep->frame_number |= dep->combo_num << 14;
1374 dep->frame_number += max_t(u32, 4, dep->interval);
1375
1376 /* Reinitialize test variables */
1377 dep->start_cmd_status = 0;
1378 dep->combo_num = 0;
1379
1380 return __dwc3_gadget_kick_transfer(dep);
1381 }
1382
1383 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1384 {
1385 struct dwc3 *dwc = dep->dwc;
1386 int ret;
1387 int i;
1388
1389 if (list_empty(&dep->pending_list)) {
1390 dep->flags |= DWC3_EP_PENDING_REQUEST;
1391 return -EAGAIN;
1392 }
1393
1394 if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) &&
1395 (dwc->revision <= DWC3_USB31_REVISION_160A ||
1396 (dwc->revision == DWC3_USB31_REVISION_170A &&
1397 dwc->version_type >= DWC31_VERSIONTYPE_EA01 &&
1398 dwc->version_type <= DWC31_VERSIONTYPE_EA06))) {
1399
1400 if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction)
1401 return dwc3_gadget_start_isoc_quirk(dep);
1402 }
1403
1404 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1405 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1);
1406
1407 ret = __dwc3_gadget_kick_transfer(dep);
1408 if (ret != -EAGAIN)
1409 break;
1410 }
1411
1412 return ret;
1413 }
1414
1415 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1416 {
1417 struct dwc3 *dwc = dep->dwc;
1418
1419 if (!dep->endpoint.desc) {
1420 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1421 dep->name);
1422 return -ESHUTDOWN;
1423 }
1424
1425 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1426 &req->request, req->dep->name))
1427 return -EINVAL;
1428
1429 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1430 "%s: request %pK already in flight\n",
1431 dep->name, &req->request))
1432 return -EINVAL;
1433
1434 pm_runtime_get(dwc->dev);
1435
1436 req->request.actual = 0;
1437 req->request.status = -EINPROGRESS;
1438
1439 trace_dwc3_ep_queue(req);
1440
1441 list_add_tail(&req->list, &dep->pending_list);
1442 req->status = DWC3_REQUEST_STATUS_QUEUED;
1443
1444 /*
1445 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1446 * wait for a XferNotReady event so we will know what's the current
1447 * (micro-)frame number.
1448 *
1449 * Without this trick, we are very, very likely gonna get Bus Expiry
1450 * errors which will force us issue EndTransfer command.
1451 */
1452 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1453 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) &&
1454 !(dep->flags & DWC3_EP_TRANSFER_STARTED))
1455 return 0;
1456
1457 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1458 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1459 return __dwc3_gadget_start_isoc(dep);
1460 }
1461 }
1462 }
1463
1464 return __dwc3_gadget_kick_transfer(dep);
1465 }
1466
1467 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1468 gfp_t gfp_flags)
1469 {
1470 struct dwc3_request *req = to_dwc3_request(request);
1471 struct dwc3_ep *dep = to_dwc3_ep(ep);
1472 struct dwc3 *dwc = dep->dwc;
1473
1474 unsigned long flags;
1475
1476 int ret;
1477
1478 spin_lock_irqsave(&dwc->lock, flags);
1479 ret = __dwc3_gadget_ep_queue(dep, req);
1480 spin_unlock_irqrestore(&dwc->lock, flags);
1481
1482 return ret;
1483 }
1484
1485 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
1486 {
1487 int i;
1488
1489 /*
1490 * If request was already started, this means we had to
1491 * stop the transfer. With that we also need to ignore
1492 * all TRBs used by the request, however TRBs can only
1493 * be modified after completion of END_TRANSFER
1494 * command. So what we do here is that we wait for
1495 * END_TRANSFER completion and only after that, we jump
1496 * over TRBs by clearing HWO and incrementing dequeue
1497 * pointer.
1498 */
1499 for (i = 0; i < req->num_trbs; i++) {
1500 struct dwc3_trb *trb;
1501
1502 trb = req->trb + i;
1503 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1504 dwc3_ep_inc_deq(dep);
1505 }
1506 }
1507
1508 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
1509 {
1510 struct dwc3_request *req;
1511 struct dwc3_request *tmp;
1512
1513 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1514 dwc3_gadget_ep_skip_trbs(dep, req);
1515 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1516 }
1517 }
1518
1519 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1520 struct usb_request *request)
1521 {
1522 struct dwc3_request *req = to_dwc3_request(request);
1523 struct dwc3_request *r = NULL;
1524
1525 struct dwc3_ep *dep = to_dwc3_ep(ep);
1526 struct dwc3 *dwc = dep->dwc;
1527
1528 unsigned long flags;
1529 int ret = 0;
1530
1531 trace_dwc3_ep_dequeue(req);
1532
1533 spin_lock_irqsave(&dwc->lock, flags);
1534
1535 list_for_each_entry(r, &dep->pending_list, list) {
1536 if (r == req)
1537 break;
1538 }
1539
1540 if (r != req) {
1541 list_for_each_entry(r, &dep->started_list, list) {
1542 if (r == req)
1543 break;
1544 }
1545 if (r == req) {
1546 /* wait until it is processed */
1547 dwc3_stop_active_transfer(dep, true);
1548
1549 if (!r->trb)
1550 goto out0;
1551
1552 dwc3_gadget_move_cancelled_request(req);
1553 if (dep->flags & DWC3_EP_TRANSFER_STARTED)
1554 goto out0;
1555 else
1556 goto out1;
1557 }
1558 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1559 request, ep->name);
1560 ret = -EINVAL;
1561 goto out0;
1562 }
1563
1564 out1:
1565 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1566
1567 out0:
1568 spin_unlock_irqrestore(&dwc->lock, flags);
1569
1570 return ret;
1571 }
1572
1573 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1574 {
1575 struct dwc3_gadget_ep_cmd_params params;
1576 struct dwc3 *dwc = dep->dwc;
1577 int ret;
1578
1579 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1580 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1581 return -EINVAL;
1582 }
1583
1584 memset(&params, 0x00, sizeof(params));
1585
1586 if (value) {
1587 struct dwc3_trb *trb;
1588
1589 unsigned transfer_in_flight;
1590 unsigned started;
1591
1592 if (dep->number > 1)
1593 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1594 else
1595 trb = &dwc->ep0_trb[dep->trb_enqueue];
1596
1597 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1598 started = !list_empty(&dep->started_list);
1599
1600 if (!protocol && ((dep->direction && transfer_in_flight) ||
1601 (!dep->direction && started))) {
1602 return -EAGAIN;
1603 }
1604
1605 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1606 &params);
1607 if (ret)
1608 dev_err(dwc->dev, "failed to set STALL on %s\n",
1609 dep->name);
1610 else
1611 dep->flags |= DWC3_EP_STALL;
1612 } else {
1613
1614 ret = dwc3_send_clear_stall_ep_cmd(dep);
1615 if (ret)
1616 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1617 dep->name);
1618 else
1619 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1620 }
1621
1622 return ret;
1623 }
1624
1625 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1626 {
1627 struct dwc3_ep *dep = to_dwc3_ep(ep);
1628 struct dwc3 *dwc = dep->dwc;
1629
1630 unsigned long flags;
1631
1632 int ret;
1633
1634 spin_lock_irqsave(&dwc->lock, flags);
1635 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1636 spin_unlock_irqrestore(&dwc->lock, flags);
1637
1638 return ret;
1639 }
1640
1641 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1642 {
1643 struct dwc3_ep *dep = to_dwc3_ep(ep);
1644 struct dwc3 *dwc = dep->dwc;
1645 unsigned long flags;
1646 int ret;
1647
1648 spin_lock_irqsave(&dwc->lock, flags);
1649 dep->flags |= DWC3_EP_WEDGE;
1650
1651 if (dep->number == 0 || dep->number == 1)
1652 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1653 else
1654 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1655 spin_unlock_irqrestore(&dwc->lock, flags);
1656
1657 return ret;
1658 }
1659
1660 /* -------------------------------------------------------------------------- */
1661
1662 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1663 .bLength = USB_DT_ENDPOINT_SIZE,
1664 .bDescriptorType = USB_DT_ENDPOINT,
1665 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1666 };
1667
1668 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1669 .enable = dwc3_gadget_ep0_enable,
1670 .disable = dwc3_gadget_ep0_disable,
1671 .alloc_request = dwc3_gadget_ep_alloc_request,
1672 .free_request = dwc3_gadget_ep_free_request,
1673 .queue = dwc3_gadget_ep0_queue,
1674 .dequeue = dwc3_gadget_ep_dequeue,
1675 .set_halt = dwc3_gadget_ep0_set_halt,
1676 .set_wedge = dwc3_gadget_ep_set_wedge,
1677 };
1678
1679 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1680 .enable = dwc3_gadget_ep_enable,
1681 .disable = dwc3_gadget_ep_disable,
1682 .alloc_request = dwc3_gadget_ep_alloc_request,
1683 .free_request = dwc3_gadget_ep_free_request,
1684 .queue = dwc3_gadget_ep_queue,
1685 .dequeue = dwc3_gadget_ep_dequeue,
1686 .set_halt = dwc3_gadget_ep_set_halt,
1687 .set_wedge = dwc3_gadget_ep_set_wedge,
1688 };
1689
1690 /* -------------------------------------------------------------------------- */
1691
1692 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1693 {
1694 struct dwc3 *dwc = gadget_to_dwc(g);
1695
1696 return __dwc3_gadget_get_frame(dwc);
1697 }
1698
1699 static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1700 {
1701 int retries;
1702
1703 int ret;
1704 u32 reg;
1705
1706 u8 link_state;
1707 u8 speed;
1708
1709 /*
1710 * According to the Databook Remote wakeup request should
1711 * be issued only when the device is in early suspend state.
1712 *
1713 * We can check that via USB Link State bits in DSTS register.
1714 */
1715 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1716
1717 speed = reg & DWC3_DSTS_CONNECTSPD;
1718 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1719 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
1720 return 0;
1721
1722 link_state = DWC3_DSTS_USBLNKST(reg);
1723
1724 switch (link_state) {
1725 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1726 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1727 break;
1728 default:
1729 return -EINVAL;
1730 }
1731
1732 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1733 if (ret < 0) {
1734 dev_err(dwc->dev, "failed to put link in Recovery\n");
1735 return ret;
1736 }
1737
1738 /* Recent versions do this automatically */
1739 if (dwc->revision < DWC3_REVISION_194A) {
1740 /* write zeroes to Link Change Request */
1741 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1742 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1743 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1744 }
1745
1746 /* poll until Link State changes to ON */
1747 retries = 20000;
1748
1749 while (retries--) {
1750 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1751
1752 /* in HS, means ON */
1753 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1754 break;
1755 }
1756
1757 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1758 dev_err(dwc->dev, "failed to send remote wakeup\n");
1759 return -EINVAL;
1760 }
1761
1762 return 0;
1763 }
1764
1765 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1766 {
1767 struct dwc3 *dwc = gadget_to_dwc(g);
1768 unsigned long flags;
1769 int ret;
1770
1771 spin_lock_irqsave(&dwc->lock, flags);
1772 ret = __dwc3_gadget_wakeup(dwc);
1773 spin_unlock_irqrestore(&dwc->lock, flags);
1774
1775 return ret;
1776 }
1777
1778 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1779 int is_selfpowered)
1780 {
1781 struct dwc3 *dwc = gadget_to_dwc(g);
1782 unsigned long flags;
1783
1784 spin_lock_irqsave(&dwc->lock, flags);
1785 g->is_selfpowered = !!is_selfpowered;
1786 spin_unlock_irqrestore(&dwc->lock, flags);
1787
1788 return 0;
1789 }
1790
1791 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1792 {
1793 u32 reg;
1794 u32 timeout = 500;
1795
1796 if (pm_runtime_suspended(dwc->dev))
1797 return 0;
1798
1799 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1800 if (is_on) {
1801 if (dwc->revision <= DWC3_REVISION_187A) {
1802 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1803 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1804 }
1805
1806 if (dwc->revision >= DWC3_REVISION_194A)
1807 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1808 reg |= DWC3_DCTL_RUN_STOP;
1809
1810 if (dwc->has_hibernation)
1811 reg |= DWC3_DCTL_KEEP_CONNECT;
1812
1813 dwc->pullups_connected = true;
1814 } else {
1815 reg &= ~DWC3_DCTL_RUN_STOP;
1816
1817 if (dwc->has_hibernation && !suspend)
1818 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1819
1820 dwc->pullups_connected = false;
1821 }
1822
1823 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1824
1825 do {
1826 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1827 reg &= DWC3_DSTS_DEVCTRLHLT;
1828 } while (--timeout && !(!is_on ^ !reg));
1829
1830 if (!timeout)
1831 return -ETIMEDOUT;
1832
1833 return 0;
1834 }
1835
1836 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1837 {
1838 struct dwc3 *dwc = gadget_to_dwc(g);
1839 unsigned long flags;
1840 int ret;
1841
1842 is_on = !!is_on;
1843
1844 /*
1845 * Per databook, when we want to stop the gadget, if a control transfer
1846 * is still in process, complete it and get the core into setup phase.
1847 */
1848 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1849 reinit_completion(&dwc->ep0_in_setup);
1850
1851 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1852 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1853 if (ret == 0) {
1854 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1855 return -ETIMEDOUT;
1856 }
1857 }
1858
1859 spin_lock_irqsave(&dwc->lock, flags);
1860 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1861 spin_unlock_irqrestore(&dwc->lock, flags);
1862
1863 return ret;
1864 }
1865
1866 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1867 {
1868 u32 reg;
1869
1870 /* Enable all but Start and End of Frame IRQs */
1871 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1872 DWC3_DEVTEN_EVNTOVERFLOWEN |
1873 DWC3_DEVTEN_CMDCMPLTEN |
1874 DWC3_DEVTEN_ERRTICERREN |
1875 DWC3_DEVTEN_WKUPEVTEN |
1876 DWC3_DEVTEN_CONNECTDONEEN |
1877 DWC3_DEVTEN_USBRSTEN |
1878 DWC3_DEVTEN_DISCONNEVTEN);
1879
1880 if (dwc->revision < DWC3_REVISION_250A)
1881 reg |= DWC3_DEVTEN_ULSTCNGEN;
1882
1883 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1884 }
1885
1886 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1887 {
1888 /* mask all interrupts */
1889 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1890 }
1891
1892 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
1893 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
1894
1895 /**
1896 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1897 * @dwc: pointer to our context structure
1898 *
1899 * The following looks like complex but it's actually very simple. In order to
1900 * calculate the number of packets we can burst at once on OUT transfers, we're
1901 * gonna use RxFIFO size.
1902 *
1903 * To calculate RxFIFO size we need two numbers:
1904 * MDWIDTH = size, in bits, of the internal memory bus
1905 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1906 *
1907 * Given these two numbers, the formula is simple:
1908 *
1909 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1910 *
1911 * 24 bytes is for 3x SETUP packets
1912 * 16 bytes is a clock domain crossing tolerance
1913 *
1914 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1915 */
1916 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1917 {
1918 u32 ram2_depth;
1919 u32 mdwidth;
1920 u32 nump;
1921 u32 reg;
1922
1923 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1924 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1925
1926 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1927 nump = min_t(u32, nump, 16);
1928
1929 /* update NumP */
1930 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1931 reg &= ~DWC3_DCFG_NUMP_MASK;
1932 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1933 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1934 }
1935
1936 static int __dwc3_gadget_start(struct dwc3 *dwc)
1937 {
1938 struct dwc3_ep *dep;
1939 int ret = 0;
1940 u32 reg;
1941
1942 /*
1943 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1944 * the core supports IMOD, disable it.
1945 */
1946 if (dwc->imod_interval) {
1947 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1948 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1949 } else if (dwc3_has_imod(dwc)) {
1950 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1951 }
1952
1953 /*
1954 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1955 * field instead of letting dwc3 itself calculate that automatically.
1956 *
1957 * This way, we maximize the chances that we'll be able to get several
1958 * bursts of data without going through any sort of endpoint throttling.
1959 */
1960 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1961 if (dwc3_is_usb31(dwc))
1962 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1963 else
1964 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1965
1966 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1967
1968 dwc3_gadget_setup_nump(dwc);
1969
1970 /* Start with SuperSpeed Default */
1971 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1972
1973 dep = dwc->eps[0];
1974 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1975 if (ret) {
1976 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1977 goto err0;
1978 }
1979
1980 dep = dwc->eps[1];
1981 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1982 if (ret) {
1983 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1984 goto err1;
1985 }
1986
1987 /* begin to receive SETUP packets */
1988 dwc->ep0state = EP0_SETUP_PHASE;
1989 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
1990 dwc3_ep0_out_start(dwc);
1991
1992 dwc3_gadget_enable_irq(dwc);
1993
1994 return 0;
1995
1996 err1:
1997 __dwc3_gadget_ep_disable(dwc->eps[0]);
1998
1999 err0:
2000 return ret;
2001 }
2002
2003 static int dwc3_gadget_start(struct usb_gadget *g,
2004 struct usb_gadget_driver *driver)
2005 {
2006 struct dwc3 *dwc = gadget_to_dwc(g);
2007 unsigned long flags;
2008 int ret = 0;
2009 int irq;
2010
2011 irq = dwc->irq_gadget;
2012 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2013 IRQF_SHARED, "dwc3", dwc->ev_buf);
2014 if (ret) {
2015 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2016 irq, ret);
2017 goto err0;
2018 }
2019
2020 spin_lock_irqsave(&dwc->lock, flags);
2021 if (dwc->gadget_driver) {
2022 dev_err(dwc->dev, "%s is already bound to %s\n",
2023 dwc->gadget.name,
2024 dwc->gadget_driver->driver.name);
2025 ret = -EBUSY;
2026 goto err1;
2027 }
2028
2029 dwc->gadget_driver = driver;
2030
2031 if (pm_runtime_active(dwc->dev))
2032 __dwc3_gadget_start(dwc);
2033
2034 spin_unlock_irqrestore(&dwc->lock, flags);
2035
2036 return 0;
2037
2038 err1:
2039 spin_unlock_irqrestore(&dwc->lock, flags);
2040 free_irq(irq, dwc);
2041
2042 err0:
2043 return ret;
2044 }
2045
2046 static void __dwc3_gadget_stop(struct dwc3 *dwc)
2047 {
2048 dwc3_gadget_disable_irq(dwc);
2049 __dwc3_gadget_ep_disable(dwc->eps[0]);
2050 __dwc3_gadget_ep_disable(dwc->eps[1]);
2051 }
2052
2053 static int dwc3_gadget_stop(struct usb_gadget *g)
2054 {
2055 struct dwc3 *dwc = gadget_to_dwc(g);
2056 unsigned long flags;
2057
2058 spin_lock_irqsave(&dwc->lock, flags);
2059
2060 if (pm_runtime_suspended(dwc->dev))
2061 goto out;
2062
2063 __dwc3_gadget_stop(dwc);
2064
2065 out:
2066 dwc->gadget_driver = NULL;
2067 spin_unlock_irqrestore(&dwc->lock, flags);
2068
2069 free_irq(dwc->irq_gadget, dwc->ev_buf);
2070
2071 return 0;
2072 }
2073
2074 static void dwc3_gadget_set_speed(struct usb_gadget *g,
2075 enum usb_device_speed speed)
2076 {
2077 struct dwc3 *dwc = gadget_to_dwc(g);
2078 unsigned long flags;
2079 u32 reg;
2080
2081 spin_lock_irqsave(&dwc->lock, flags);
2082 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2083 reg &= ~(DWC3_DCFG_SPEED_MASK);
2084
2085 /*
2086 * WORKAROUND: DWC3 revision < 2.20a have an issue
2087 * which would cause metastability state on Run/Stop
2088 * bit if we try to force the IP to USB2-only mode.
2089 *
2090 * Because of that, we cannot configure the IP to any
2091 * speed other than the SuperSpeed
2092 *
2093 * Refers to:
2094 *
2095 * STAR#9000525659: Clock Domain Crossing on DCTL in
2096 * USB 2.0 Mode
2097 */
2098 if (dwc->revision < DWC3_REVISION_220A &&
2099 !dwc->dis_metastability_quirk) {
2100 reg |= DWC3_DCFG_SUPERSPEED;
2101 } else {
2102 switch (speed) {
2103 case USB_SPEED_LOW:
2104 reg |= DWC3_DCFG_LOWSPEED;
2105 break;
2106 case USB_SPEED_FULL:
2107 reg |= DWC3_DCFG_FULLSPEED;
2108 break;
2109 case USB_SPEED_HIGH:
2110 reg |= DWC3_DCFG_HIGHSPEED;
2111 break;
2112 case USB_SPEED_SUPER:
2113 reg |= DWC3_DCFG_SUPERSPEED;
2114 break;
2115 case USB_SPEED_SUPER_PLUS:
2116 if (dwc3_is_usb31(dwc))
2117 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2118 else
2119 reg |= DWC3_DCFG_SUPERSPEED;
2120 break;
2121 default:
2122 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2123
2124 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2125 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2126 else
2127 reg |= DWC3_DCFG_SUPERSPEED;
2128 }
2129 }
2130 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2131
2132 spin_unlock_irqrestore(&dwc->lock, flags);
2133 }
2134
2135 static const struct usb_gadget_ops dwc3_gadget_ops = {
2136 .get_frame = dwc3_gadget_get_frame,
2137 .wakeup = dwc3_gadget_wakeup,
2138 .set_selfpowered = dwc3_gadget_set_selfpowered,
2139 .pullup = dwc3_gadget_pullup,
2140 .udc_start = dwc3_gadget_start,
2141 .udc_stop = dwc3_gadget_stop,
2142 .udc_set_speed = dwc3_gadget_set_speed,
2143 };
2144
2145 /* -------------------------------------------------------------------------- */
2146
2147 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2148 {
2149 struct dwc3 *dwc = dep->dwc;
2150
2151 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2152 dep->endpoint.maxburst = 1;
2153 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2154 if (!dep->direction)
2155 dwc->gadget.ep0 = &dep->endpoint;
2156
2157 dep->endpoint.caps.type_control = true;
2158
2159 return 0;
2160 }
2161
2162 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2163 {
2164 struct dwc3 *dwc = dep->dwc;
2165 int mdwidth;
2166 int kbytes;
2167 int size;
2168
2169 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2170 /* MDWIDTH is represented in bits, we need it in bytes */
2171 mdwidth /= 8;
2172
2173 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2174 if (dwc3_is_usb31(dwc))
2175 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2176 else
2177 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2178
2179 /* FIFO Depth is in MDWDITH bytes. Multiply */
2180 size *= mdwidth;
2181
2182 kbytes = size / 1024;
2183 if (kbytes == 0)
2184 kbytes = 1;
2185
2186 /*
2187 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2188 * internal overhead. We don't really know how these are used,
2189 * but documentation say it exists.
2190 */
2191 size -= mdwidth * (kbytes + 1);
2192 size /= kbytes;
2193
2194 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2195
2196 dep->endpoint.max_streams = 15;
2197 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2198 list_add_tail(&dep->endpoint.ep_list,
2199 &dwc->gadget.ep_list);
2200 dep->endpoint.caps.type_iso = true;
2201 dep->endpoint.caps.type_bulk = true;
2202 dep->endpoint.caps.type_int = true;
2203
2204 return dwc3_alloc_trb_pool(dep);
2205 }
2206
2207 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2208 {
2209 struct dwc3 *dwc = dep->dwc;
2210
2211 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2212 dep->endpoint.max_streams = 15;
2213 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2214 list_add_tail(&dep->endpoint.ep_list,
2215 &dwc->gadget.ep_list);
2216 dep->endpoint.caps.type_iso = true;
2217 dep->endpoint.caps.type_bulk = true;
2218 dep->endpoint.caps.type_int = true;
2219
2220 return dwc3_alloc_trb_pool(dep);
2221 }
2222
2223 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2224 {
2225 struct dwc3_ep *dep;
2226 bool direction = epnum & 1;
2227 int ret;
2228 u8 num = epnum >> 1;
2229
2230 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2231 if (!dep)
2232 return -ENOMEM;
2233
2234 dep->dwc = dwc;
2235 dep->number = epnum;
2236 dep->direction = direction;
2237 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2238 dwc->eps[epnum] = dep;
2239 dep->combo_num = 0;
2240 dep->start_cmd_status = 0;
2241
2242 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2243 direction ? "in" : "out");
2244
2245 dep->endpoint.name = dep->name;
2246
2247 if (!(dep->number > 1)) {
2248 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2249 dep->endpoint.comp_desc = NULL;
2250 }
2251
2252 spin_lock_init(&dep->lock);
2253
2254 if (num == 0)
2255 ret = dwc3_gadget_init_control_endpoint(dep);
2256 else if (direction)
2257 ret = dwc3_gadget_init_in_endpoint(dep);
2258 else
2259 ret = dwc3_gadget_init_out_endpoint(dep);
2260
2261 if (ret)
2262 return ret;
2263
2264 dep->endpoint.caps.dir_in = direction;
2265 dep->endpoint.caps.dir_out = !direction;
2266
2267 INIT_LIST_HEAD(&dep->pending_list);
2268 INIT_LIST_HEAD(&dep->started_list);
2269 INIT_LIST_HEAD(&dep->cancelled_list);
2270
2271 return 0;
2272 }
2273
2274 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2275 {
2276 u8 epnum;
2277
2278 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2279
2280 for (epnum = 0; epnum < total; epnum++) {
2281 int ret;
2282
2283 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2284 if (ret)
2285 return ret;
2286 }
2287
2288 return 0;
2289 }
2290
2291 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2292 {
2293 struct dwc3_ep *dep;
2294 u8 epnum;
2295
2296 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2297 dep = dwc->eps[epnum];
2298 if (!dep)
2299 continue;
2300 /*
2301 * Physical endpoints 0 and 1 are special; they form the
2302 * bi-directional USB endpoint 0.
2303 *
2304 * For those two physical endpoints, we don't allocate a TRB
2305 * pool nor do we add them the endpoints list. Due to that, we
2306 * shouldn't do these two operations otherwise we would end up
2307 * with all sorts of bugs when removing dwc3.ko.
2308 */
2309 if (epnum != 0 && epnum != 1) {
2310 dwc3_free_trb_pool(dep);
2311 list_del(&dep->endpoint.ep_list);
2312 }
2313
2314 kfree(dep);
2315 }
2316 }
2317
2318 /* -------------------------------------------------------------------------- */
2319
2320 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2321 struct dwc3_request *req, struct dwc3_trb *trb,
2322 const struct dwc3_event_depevt *event, int status, int chain)
2323 {
2324 unsigned int count;
2325
2326 dwc3_ep_inc_deq(dep);
2327
2328 trace_dwc3_complete_trb(dep, trb);
2329 req->num_trbs--;
2330
2331 /*
2332 * If we're in the middle of series of chained TRBs and we
2333 * receive a short transfer along the way, DWC3 will skip
2334 * through all TRBs including the last TRB in the chain (the
2335 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2336 * bit and SW has to do it manually.
2337 *
2338 * We're going to do that here to avoid problems of HW trying
2339 * to use bogus TRBs for transfers.
2340 */
2341 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2342 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2343
2344 /*
2345 * For isochronous transfers, the first TRB in a service interval must
2346 * have the Isoc-First type. Track and report its interval frame number.
2347 */
2348 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2349 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
2350 unsigned int frame_number;
2351
2352 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
2353 frame_number &= ~(dep->interval - 1);
2354 req->request.frame_number = frame_number;
2355 }
2356
2357 /*
2358 * If we're dealing with unaligned size OUT transfer, we will be left
2359 * with one TRB pending in the ring. We need to manually clear HWO bit
2360 * from that TRB.
2361 */
2362
2363 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) {
2364 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2365 return 1;
2366 }
2367
2368 count = trb->size & DWC3_TRB_SIZE_MASK;
2369 req->remaining += count;
2370
2371 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2372 return 1;
2373
2374 if (event->status & DEPEVT_STATUS_SHORT && !chain)
2375 return 1;
2376
2377 if (event->status & DEPEVT_STATUS_IOC)
2378 return 1;
2379
2380 return 0;
2381 }
2382
2383 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2384 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2385 int status)
2386 {
2387 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2388 struct scatterlist *sg = req->sg;
2389 struct scatterlist *s;
2390 unsigned int pending = req->num_pending_sgs;
2391 unsigned int i;
2392 int ret = 0;
2393
2394 for_each_sg(sg, s, pending, i) {
2395 trb = &dep->trb_pool[dep->trb_dequeue];
2396
2397 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2398 break;
2399
2400 req->sg = sg_next(s);
2401 req->num_pending_sgs--;
2402
2403 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2404 trb, event, status, true);
2405 if (ret)
2406 break;
2407 }
2408
2409 return ret;
2410 }
2411
2412 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2413 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2414 int status)
2415 {
2416 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2417
2418 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2419 event, status, false);
2420 }
2421
2422 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2423 {
2424 return req->request.actual == req->request.length;
2425 }
2426
2427 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2428 const struct dwc3_event_depevt *event,
2429 struct dwc3_request *req, int status)
2430 {
2431 int ret;
2432
2433 if (req->num_pending_sgs)
2434 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2435 status);
2436 else
2437 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2438 status);
2439
2440 if (req->needs_extra_trb) {
2441 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2442 status);
2443 req->needs_extra_trb = false;
2444 }
2445
2446 req->request.actual = req->request.length - req->remaining;
2447
2448 if (!dwc3_gadget_ep_request_completed(req) &&
2449 req->num_pending_sgs) {
2450 __dwc3_gadget_kick_transfer(dep);
2451 goto out;
2452 }
2453
2454 dwc3_gadget_giveback(dep, req, status);
2455
2456 out:
2457 return ret;
2458 }
2459
2460 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2461 const struct dwc3_event_depevt *event, int status)
2462 {
2463 struct dwc3_request *req;
2464 struct dwc3_request *tmp;
2465
2466 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2467 int ret;
2468
2469 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2470 req, status);
2471 if (ret)
2472 break;
2473 }
2474 }
2475
2476 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2477 const struct dwc3_event_depevt *event)
2478 {
2479 dep->frame_number = event->parameters;
2480 }
2481
2482 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2483 const struct dwc3_event_depevt *event)
2484 {
2485 struct dwc3 *dwc = dep->dwc;
2486 unsigned status = 0;
2487 bool stop = false;
2488
2489 dwc3_gadget_endpoint_frame_from_event(dep, event);
2490
2491 if (event->status & DEPEVT_STATUS_BUSERR)
2492 status = -ECONNRESET;
2493
2494 if (event->status & DEPEVT_STATUS_MISSED_ISOC) {
2495 status = -EXDEV;
2496
2497 if (list_empty(&dep->started_list))
2498 stop = true;
2499 }
2500
2501 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2502
2503 if (stop) {
2504 dwc3_stop_active_transfer(dep, true);
2505 dep->flags = DWC3_EP_ENABLED;
2506 }
2507
2508 /*
2509 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2510 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2511 */
2512 if (dwc->revision < DWC3_REVISION_183A) {
2513 u32 reg;
2514 int i;
2515
2516 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2517 dep = dwc->eps[i];
2518
2519 if (!(dep->flags & DWC3_EP_ENABLED))
2520 continue;
2521
2522 if (!list_empty(&dep->started_list))
2523 return;
2524 }
2525
2526 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2527 reg |= dwc->u1u2;
2528 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2529
2530 dwc->u1u2 = 0;
2531 }
2532 }
2533
2534 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2535 const struct dwc3_event_depevt *event)
2536 {
2537 dwc3_gadget_endpoint_frame_from_event(dep, event);
2538 (void) __dwc3_gadget_start_isoc(dep);
2539 }
2540
2541 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2542 const struct dwc3_event_depevt *event)
2543 {
2544 struct dwc3_ep *dep;
2545 u8 epnum = event->endpoint_number;
2546 u8 cmd;
2547
2548 dep = dwc->eps[epnum];
2549
2550 if (!(dep->flags & DWC3_EP_ENABLED)) {
2551 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2552 return;
2553
2554 /* Handle only EPCMDCMPLT when EP disabled */
2555 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2556 return;
2557 }
2558
2559 if (epnum == 0 || epnum == 1) {
2560 dwc3_ep0_interrupt(dwc, event);
2561 return;
2562 }
2563
2564 switch (event->endpoint_event) {
2565 case DWC3_DEPEVT_XFERINPROGRESS:
2566 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
2567 break;
2568 case DWC3_DEPEVT_XFERNOTREADY:
2569 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
2570 break;
2571 case DWC3_DEPEVT_EPCMDCMPLT:
2572 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2573
2574 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2575 dep->flags &= ~(DWC3_EP_END_TRANSFER_PENDING |
2576 DWC3_EP_TRANSFER_STARTED);
2577 dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2578 }
2579 break;
2580 case DWC3_DEPEVT_STREAMEVT:
2581 case DWC3_DEPEVT_XFERCOMPLETE:
2582 case DWC3_DEPEVT_RXTXFIFOEVT:
2583 break;
2584 }
2585 }
2586
2587 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2588 {
2589 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2590 spin_unlock(&dwc->lock);
2591 dwc->gadget_driver->disconnect(&dwc->gadget);
2592 spin_lock(&dwc->lock);
2593 }
2594 }
2595
2596 static void dwc3_suspend_gadget(struct dwc3 *dwc)
2597 {
2598 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2599 spin_unlock(&dwc->lock);
2600 dwc->gadget_driver->suspend(&dwc->gadget);
2601 spin_lock(&dwc->lock);
2602 }
2603 }
2604
2605 static void dwc3_resume_gadget(struct dwc3 *dwc)
2606 {
2607 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2608 spin_unlock(&dwc->lock);
2609 dwc->gadget_driver->resume(&dwc->gadget);
2610 spin_lock(&dwc->lock);
2611 }
2612 }
2613
2614 static void dwc3_reset_gadget(struct dwc3 *dwc)
2615 {
2616 if (!dwc->gadget_driver)
2617 return;
2618
2619 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2620 spin_unlock(&dwc->lock);
2621 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2622 spin_lock(&dwc->lock);
2623 }
2624 }
2625
2626 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
2627 {
2628 struct dwc3 *dwc = dep->dwc;
2629 struct dwc3_gadget_ep_cmd_params params;
2630 u32 cmd;
2631 int ret;
2632
2633 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING)
2634 || !(dep->flags & DWC3_EP_TRANSFER_STARTED))
2635 return;
2636
2637 /*
2638 * NOTICE: We are violating what the Databook says about the
2639 * EndTransfer command. Ideally we would _always_ wait for the
2640 * EndTransfer Command Completion IRQ, but that's causing too
2641 * much trouble synchronizing between us and gadget driver.
2642 *
2643 * We have discussed this with the IP Provider and it was
2644 * suggested to giveback all requests here, but give HW some
2645 * extra time to synchronize with the interconnect. We're using
2646 * an arbitrary 100us delay for that.
2647 *
2648 * Note also that a similar handling was tested by Synopsys
2649 * (thanks a lot Paul) and nothing bad has come out of it.
2650 * In short, what we're doing is:
2651 *
2652 * - Issue EndTransfer WITH CMDIOC bit set
2653 * - Wait 100us
2654 *
2655 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2656 * supports a mode to work around the above limitation. The
2657 * software can poll the CMDACT bit in the DEPCMD register
2658 * after issuing a EndTransfer command. This mode is enabled
2659 * by writing GUCTL2[14]. This polling is already done in the
2660 * dwc3_send_gadget_ep_cmd() function so if the mode is
2661 * enabled, the EndTransfer command will have completed upon
2662 * returning from this function and we don't need to delay for
2663 * 100us.
2664 *
2665 * This mode is NOT available on the DWC_usb31 IP.
2666 */
2667
2668 cmd = DWC3_DEPCMD_ENDTRANSFER;
2669 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2670 cmd |= DWC3_DEPCMD_CMDIOC;
2671 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2672 memset(&params, 0, sizeof(params));
2673 ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
2674 WARN_ON_ONCE(ret);
2675 dep->resource_index = 0;
2676
2677 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2678 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
2679 udelay(100);
2680 }
2681 }
2682
2683 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2684 {
2685 u32 epnum;
2686
2687 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2688 struct dwc3_ep *dep;
2689 int ret;
2690
2691 dep = dwc->eps[epnum];
2692 if (!dep)
2693 continue;
2694
2695 if (!(dep->flags & DWC3_EP_STALL))
2696 continue;
2697
2698 dep->flags &= ~DWC3_EP_STALL;
2699
2700 ret = dwc3_send_clear_stall_ep_cmd(dep);
2701 WARN_ON_ONCE(ret);
2702 }
2703 }
2704
2705 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2706 {
2707 int reg;
2708
2709 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2710 reg &= ~DWC3_DCTL_INITU1ENA;
2711 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2712
2713 reg &= ~DWC3_DCTL_INITU2ENA;
2714 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2715
2716 dwc3_disconnect_gadget(dwc);
2717
2718 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2719 dwc->setup_packet_pending = false;
2720 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2721
2722 dwc->connected = false;
2723 }
2724
2725 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2726 {
2727 u32 reg;
2728
2729 dwc->connected = true;
2730
2731 /*
2732 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2733 * would cause a missing Disconnect Event if there's a
2734 * pending Setup Packet in the FIFO.
2735 *
2736 * There's no suggested workaround on the official Bug
2737 * report, which states that "unless the driver/application
2738 * is doing any special handling of a disconnect event,
2739 * there is no functional issue".
2740 *
2741 * Unfortunately, it turns out that we _do_ some special
2742 * handling of a disconnect event, namely complete all
2743 * pending transfers, notify gadget driver of the
2744 * disconnection, and so on.
2745 *
2746 * Our suggested workaround is to follow the Disconnect
2747 * Event steps here, instead, based on a setup_packet_pending
2748 * flag. Such flag gets set whenever we have a SETUP_PENDING
2749 * status for EP0 TRBs and gets cleared on XferComplete for the
2750 * same endpoint.
2751 *
2752 * Refers to:
2753 *
2754 * STAR#9000466709: RTL: Device : Disconnect event not
2755 * generated if setup packet pending in FIFO
2756 */
2757 if (dwc->revision < DWC3_REVISION_188A) {
2758 if (dwc->setup_packet_pending)
2759 dwc3_gadget_disconnect_interrupt(dwc);
2760 }
2761
2762 dwc3_reset_gadget(dwc);
2763
2764 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2765 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2766 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2767 dwc->test_mode = false;
2768 dwc3_clear_stall_all_ep(dwc);
2769
2770 /* Reset device address to zero */
2771 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2772 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2773 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2774 }
2775
2776 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2777 {
2778 struct dwc3_ep *dep;
2779 int ret;
2780 u32 reg;
2781 u8 speed;
2782
2783 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2784 speed = reg & DWC3_DSTS_CONNECTSPD;
2785 dwc->speed = speed;
2786
2787 /*
2788 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2789 * each time on Connect Done.
2790 *
2791 * Currently we always use the reset value. If any platform
2792 * wants to set this to a different value, we need to add a
2793 * setting and update GCTL.RAMCLKSEL here.
2794 */
2795
2796 switch (speed) {
2797 case DWC3_DSTS_SUPERSPEED_PLUS:
2798 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2799 dwc->gadget.ep0->maxpacket = 512;
2800 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2801 break;
2802 case DWC3_DSTS_SUPERSPEED:
2803 /*
2804 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2805 * would cause a missing USB3 Reset event.
2806 *
2807 * In such situations, we should force a USB3 Reset
2808 * event by calling our dwc3_gadget_reset_interrupt()
2809 * routine.
2810 *
2811 * Refers to:
2812 *
2813 * STAR#9000483510: RTL: SS : USB3 reset event may
2814 * not be generated always when the link enters poll
2815 */
2816 if (dwc->revision < DWC3_REVISION_190A)
2817 dwc3_gadget_reset_interrupt(dwc);
2818
2819 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2820 dwc->gadget.ep0->maxpacket = 512;
2821 dwc->gadget.speed = USB_SPEED_SUPER;
2822 break;
2823 case DWC3_DSTS_HIGHSPEED:
2824 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2825 dwc->gadget.ep0->maxpacket = 64;
2826 dwc->gadget.speed = USB_SPEED_HIGH;
2827 break;
2828 case DWC3_DSTS_FULLSPEED:
2829 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2830 dwc->gadget.ep0->maxpacket = 64;
2831 dwc->gadget.speed = USB_SPEED_FULL;
2832 break;
2833 case DWC3_DSTS_LOWSPEED:
2834 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2835 dwc->gadget.ep0->maxpacket = 8;
2836 dwc->gadget.speed = USB_SPEED_LOW;
2837 break;
2838 }
2839
2840 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2841
2842 /* Enable USB2 LPM Capability */
2843
2844 if ((dwc->revision > DWC3_REVISION_194A) &&
2845 (speed != DWC3_DSTS_SUPERSPEED) &&
2846 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
2847 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2848 reg |= DWC3_DCFG_LPM_CAP;
2849 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2850
2851 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2852 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2853
2854 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2855
2856 /*
2857 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2858 * DCFG.LPMCap is set, core responses with an ACK and the
2859 * BESL value in the LPM token is less than or equal to LPM
2860 * NYET threshold.
2861 */
2862 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2863 && dwc->has_lpm_erratum,
2864 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2865
2866 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2867 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2868
2869 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2870 } else {
2871 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2872 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2873 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2874 }
2875
2876 dep = dwc->eps[0];
2877 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2878 if (ret) {
2879 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2880 return;
2881 }
2882
2883 dep = dwc->eps[1];
2884 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2885 if (ret) {
2886 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2887 return;
2888 }
2889
2890 /*
2891 * Configure PHY via GUSB3PIPECTLn if required.
2892 *
2893 * Update GTXFIFOSIZn
2894 *
2895 * In both cases reset values should be sufficient.
2896 */
2897 }
2898
2899 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2900 {
2901 /*
2902 * TODO take core out of low power mode when that's
2903 * implemented.
2904 */
2905
2906 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2907 spin_unlock(&dwc->lock);
2908 dwc->gadget_driver->resume(&dwc->gadget);
2909 spin_lock(&dwc->lock);
2910 }
2911 }
2912
2913 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2914 unsigned int evtinfo)
2915 {
2916 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2917 unsigned int pwropt;
2918
2919 /*
2920 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2921 * Hibernation mode enabled which would show up when device detects
2922 * host-initiated U3 exit.
2923 *
2924 * In that case, device will generate a Link State Change Interrupt
2925 * from U3 to RESUME which is only necessary if Hibernation is
2926 * configured in.
2927 *
2928 * There are no functional changes due to such spurious event and we
2929 * just need to ignore it.
2930 *
2931 * Refers to:
2932 *
2933 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2934 * operational mode
2935 */
2936 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2937 if ((dwc->revision < DWC3_REVISION_250A) &&
2938 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2939 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2940 (next == DWC3_LINK_STATE_RESUME)) {
2941 return;
2942 }
2943 }
2944
2945 /*
2946 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2947 * on the link partner, the USB session might do multiple entry/exit
2948 * of low power states before a transfer takes place.
2949 *
2950 * Due to this problem, we might experience lower throughput. The
2951 * suggested workaround is to disable DCTL[12:9] bits if we're
2952 * transitioning from U1/U2 to U0 and enable those bits again
2953 * after a transfer completes and there are no pending transfers
2954 * on any of the enabled endpoints.
2955 *
2956 * This is the first half of that workaround.
2957 *
2958 * Refers to:
2959 *
2960 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2961 * core send LGO_Ux entering U0
2962 */
2963 if (dwc->revision < DWC3_REVISION_183A) {
2964 if (next == DWC3_LINK_STATE_U0) {
2965 u32 u1u2;
2966 u32 reg;
2967
2968 switch (dwc->link_state) {
2969 case DWC3_LINK_STATE_U1:
2970 case DWC3_LINK_STATE_U2:
2971 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2972 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2973 | DWC3_DCTL_ACCEPTU2ENA
2974 | DWC3_DCTL_INITU1ENA
2975 | DWC3_DCTL_ACCEPTU1ENA);
2976
2977 if (!dwc->u1u2)
2978 dwc->u1u2 = reg & u1u2;
2979
2980 reg &= ~u1u2;
2981
2982 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2983 break;
2984 default:
2985 /* do nothing */
2986 break;
2987 }
2988 }
2989 }
2990
2991 switch (next) {
2992 case DWC3_LINK_STATE_U1:
2993 if (dwc->speed == USB_SPEED_SUPER)
2994 dwc3_suspend_gadget(dwc);
2995 break;
2996 case DWC3_LINK_STATE_U2:
2997 case DWC3_LINK_STATE_U3:
2998 dwc3_suspend_gadget(dwc);
2999 break;
3000 case DWC3_LINK_STATE_RESUME:
3001 dwc3_resume_gadget(dwc);
3002 break;
3003 default:
3004 /* do nothing */
3005 break;
3006 }
3007
3008 dwc->link_state = next;
3009 }
3010
3011 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3012 unsigned int evtinfo)
3013 {
3014 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3015
3016 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3017 dwc3_suspend_gadget(dwc);
3018
3019 dwc->link_state = next;
3020 }
3021
3022 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3023 unsigned int evtinfo)
3024 {
3025 unsigned int is_ss = evtinfo & BIT(4);
3026
3027 /*
3028 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3029 * have a known issue which can cause USB CV TD.9.23 to fail
3030 * randomly.
3031 *
3032 * Because of this issue, core could generate bogus hibernation
3033 * events which SW needs to ignore.
3034 *
3035 * Refers to:
3036 *
3037 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3038 * Device Fallback from SuperSpeed
3039 */
3040 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
3041 return;
3042
3043 /* enter hibernation here */
3044 }
3045
3046 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3047 const struct dwc3_event_devt *event)
3048 {
3049 switch (event->type) {
3050 case DWC3_DEVICE_EVENT_DISCONNECT:
3051 dwc3_gadget_disconnect_interrupt(dwc);
3052 break;
3053 case DWC3_DEVICE_EVENT_RESET:
3054 dwc3_gadget_reset_interrupt(dwc);
3055 break;
3056 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3057 dwc3_gadget_conndone_interrupt(dwc);
3058 break;
3059 case DWC3_DEVICE_EVENT_WAKEUP:
3060 dwc3_gadget_wakeup_interrupt(dwc);
3061 break;
3062 case DWC3_DEVICE_EVENT_HIBER_REQ:
3063 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3064 "unexpected hibernation event\n"))
3065 break;
3066
3067 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3068 break;
3069 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3070 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3071 break;
3072 case DWC3_DEVICE_EVENT_EOPF:
3073 /* It changed to be suspend event for version 2.30a and above */
3074 if (dwc->revision >= DWC3_REVISION_230A) {
3075 /*
3076 * Ignore suspend event until the gadget enters into
3077 * USB_STATE_CONFIGURED state.
3078 */
3079 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3080 dwc3_gadget_suspend_interrupt(dwc,
3081 event->event_info);
3082 }
3083 break;
3084 case DWC3_DEVICE_EVENT_SOF:
3085 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
3086 case DWC3_DEVICE_EVENT_CMD_CMPL:
3087 case DWC3_DEVICE_EVENT_OVERFLOW:
3088 break;
3089 default:
3090 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
3091 }
3092 }
3093
3094 static void dwc3_process_event_entry(struct dwc3 *dwc,
3095 const union dwc3_event *event)
3096 {
3097 trace_dwc3_event(event->raw, dwc);
3098
3099 if (!event->type.is_devspec)
3100 dwc3_endpoint_interrupt(dwc, &event->depevt);
3101 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
3102 dwc3_gadget_interrupt(dwc, &event->devt);
3103 else
3104 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3105 }
3106
3107 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
3108 {
3109 struct dwc3 *dwc = evt->dwc;
3110 irqreturn_t ret = IRQ_NONE;
3111 int left;
3112 u32 reg;
3113
3114 left = evt->count;
3115
3116 if (!(evt->flags & DWC3_EVENT_PENDING))
3117 return IRQ_NONE;
3118
3119 while (left > 0) {
3120 union dwc3_event event;
3121
3122 event.raw = *(u32 *) (evt->cache + evt->lpos);
3123
3124 dwc3_process_event_entry(dwc, &event);
3125
3126 /*
3127 * FIXME we wrap around correctly to the next entry as
3128 * almost all entries are 4 bytes in size. There is one
3129 * entry which has 12 bytes which is a regular entry
3130 * followed by 8 bytes data. ATM I don't know how
3131 * things are organized if we get next to the a
3132 * boundary so I worry about that once we try to handle
3133 * that.
3134 */
3135 evt->lpos = (evt->lpos + 4) % evt->length;
3136 left -= 4;
3137 }
3138
3139 evt->count = 0;
3140 evt->flags &= ~DWC3_EVENT_PENDING;
3141 ret = IRQ_HANDLED;
3142
3143 /* Unmask interrupt */
3144 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3145 reg &= ~DWC3_GEVNTSIZ_INTMASK;
3146 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3147
3148 if (dwc->imod_interval) {
3149 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3150 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3151 }
3152
3153 return ret;
3154 }
3155
3156 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3157 {
3158 struct dwc3_event_buffer *evt = _evt;
3159 struct dwc3 *dwc = evt->dwc;
3160 unsigned long flags;
3161 irqreturn_t ret = IRQ_NONE;
3162
3163 spin_lock_irqsave(&dwc->lock, flags);
3164 ret = dwc3_process_event_buf(evt);
3165 spin_unlock_irqrestore(&dwc->lock, flags);
3166
3167 return ret;
3168 }
3169
3170 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3171 {
3172 struct dwc3 *dwc = evt->dwc;
3173 u32 amount;
3174 u32 count;
3175 u32 reg;
3176
3177 if (pm_runtime_suspended(dwc->dev)) {
3178 pm_runtime_get(dwc->dev);
3179 disable_irq_nosync(dwc->irq_gadget);
3180 dwc->pending_events = true;
3181 return IRQ_HANDLED;
3182 }
3183
3184 /*
3185 * With PCIe legacy interrupt, test shows that top-half irq handler can
3186 * be called again after HW interrupt deassertion. Check if bottom-half
3187 * irq event handler completes before caching new event to prevent
3188 * losing events.
3189 */
3190 if (evt->flags & DWC3_EVENT_PENDING)
3191 return IRQ_HANDLED;
3192
3193 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3194 count &= DWC3_GEVNTCOUNT_MASK;
3195 if (!count)
3196 return IRQ_NONE;
3197
3198 evt->count = count;
3199 evt->flags |= DWC3_EVENT_PENDING;
3200
3201 /* Mask interrupt */
3202 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3203 reg |= DWC3_GEVNTSIZ_INTMASK;
3204 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3205
3206 amount = min(count, evt->length - evt->lpos);
3207 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3208
3209 if (amount < count)
3210 memcpy(evt->cache, evt->buf, count - amount);
3211
3212 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3213
3214 return IRQ_WAKE_THREAD;
3215 }
3216
3217 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3218 {
3219 struct dwc3_event_buffer *evt = _evt;
3220
3221 return dwc3_check_event_buf(evt);
3222 }
3223
3224 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3225 {
3226 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3227 int irq;
3228
3229 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3230 if (irq > 0)
3231 goto out;
3232
3233 if (irq == -EPROBE_DEFER)
3234 goto out;
3235
3236 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3237 if (irq > 0)
3238 goto out;
3239
3240 if (irq == -EPROBE_DEFER)
3241 goto out;
3242
3243 irq = platform_get_irq(dwc3_pdev, 0);
3244 if (irq > 0)
3245 goto out;
3246
3247 if (irq != -EPROBE_DEFER)
3248 dev_err(dwc->dev, "missing peripheral IRQ\n");
3249
3250 if (!irq)
3251 irq = -EINVAL;
3252
3253 out:
3254 return irq;
3255 }
3256
3257 /**
3258 * dwc3_gadget_init - initializes gadget related registers
3259 * @dwc: pointer to our controller context structure
3260 *
3261 * Returns 0 on success otherwise negative errno.
3262 */
3263 int dwc3_gadget_init(struct dwc3 *dwc)
3264 {
3265 int ret;
3266 int irq;
3267
3268 irq = dwc3_gadget_get_irq(dwc);
3269 if (irq < 0) {
3270 ret = irq;
3271 goto err0;
3272 }
3273
3274 dwc->irq_gadget = irq;
3275
3276 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3277 sizeof(*dwc->ep0_trb) * 2,
3278 &dwc->ep0_trb_addr, GFP_KERNEL);
3279 if (!dwc->ep0_trb) {
3280 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3281 ret = -ENOMEM;
3282 goto err0;
3283 }
3284
3285 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3286 if (!dwc->setup_buf) {
3287 ret = -ENOMEM;
3288 goto err1;
3289 }
3290
3291 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3292 &dwc->bounce_addr, GFP_KERNEL);
3293 if (!dwc->bounce) {
3294 ret = -ENOMEM;
3295 goto err2;
3296 }
3297
3298 init_completion(&dwc->ep0_in_setup);
3299
3300 dwc->gadget.ops = &dwc3_gadget_ops;
3301 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3302 dwc->gadget.sg_supported = true;
3303 dwc->gadget.name = "dwc3-gadget";
3304 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
3305
3306 /*
3307 * FIXME We might be setting max_speed to <SUPER, however versions
3308 * <2.20a of dwc3 have an issue with metastability (documented
3309 * elsewhere in this driver) which tells us we can't set max speed to
3310 * anything lower than SUPER.
3311 *
3312 * Because gadget.max_speed is only used by composite.c and function
3313 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3314 * to happen so we avoid sending SuperSpeed Capability descriptor
3315 * together with our BOS descriptor as that could confuse host into
3316 * thinking we can handle super speed.
3317 *
3318 * Note that, in fact, we won't even support GetBOS requests when speed
3319 * is less than super speed because we don't have means, yet, to tell
3320 * composite.c that we are USB 2.0 + LPM ECN.
3321 */
3322 if (dwc->revision < DWC3_REVISION_220A &&
3323 !dwc->dis_metastability_quirk)
3324 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
3325 dwc->revision);
3326
3327 dwc->gadget.max_speed = dwc->maximum_speed;
3328
3329 /*
3330 * REVISIT: Here we should clear all pending IRQs to be
3331 * sure we're starting from a well known location.
3332 */
3333
3334 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
3335 if (ret)
3336 goto err3;
3337
3338 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3339 if (ret) {
3340 dev_err(dwc->dev, "failed to register udc\n");
3341 goto err4;
3342 }
3343
3344 dwc3_gadget_set_speed(&dwc->gadget, dwc->maximum_speed);
3345
3346 return 0;
3347
3348 err4:
3349 dwc3_gadget_free_endpoints(dwc);
3350
3351 err3:
3352 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3353 dwc->bounce_addr);
3354
3355 err2:
3356 kfree(dwc->setup_buf);
3357
3358 err1:
3359 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3360 dwc->ep0_trb, dwc->ep0_trb_addr);
3361
3362 err0:
3363 return ret;
3364 }
3365
3366 /* -------------------------------------------------------------------------- */
3367
3368 void dwc3_gadget_exit(struct dwc3 *dwc)
3369 {
3370 usb_del_gadget_udc(&dwc->gadget);
3371 dwc3_gadget_free_endpoints(dwc);
3372 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3373 dwc->bounce_addr);
3374 kfree(dwc->setup_buf);
3375 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3376 dwc->ep0_trb, dwc->ep0_trb_addr);
3377 }
3378
3379 int dwc3_gadget_suspend(struct dwc3 *dwc)
3380 {
3381 if (!dwc->gadget_driver)
3382 return 0;
3383
3384 dwc3_gadget_run_stop(dwc, false, false);
3385 dwc3_disconnect_gadget(dwc);
3386 __dwc3_gadget_stop(dwc);
3387
3388 synchronize_irq(dwc->irq_gadget);
3389
3390 return 0;
3391 }
3392
3393 int dwc3_gadget_resume(struct dwc3 *dwc)
3394 {
3395 int ret;
3396
3397 if (!dwc->gadget_driver)
3398 return 0;
3399
3400 ret = __dwc3_gadget_start(dwc);
3401 if (ret < 0)
3402 goto err0;
3403
3404 ret = dwc3_gadget_run_stop(dwc, true, false);
3405 if (ret < 0)
3406 goto err1;
3407
3408 return 0;
3409
3410 err1:
3411 __dwc3_gadget_stop(dwc);
3412
3413 err0:
3414 return ret;
3415 }
3416
3417 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3418 {
3419 if (dwc->pending_events) {
3420 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3421 dwc->pending_events = false;
3422 enable_irq(dwc->irq_gadget);
3423 }
3424 }