1 // SPDX-License-Identifier: GPL-2.0
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
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>
19 #include <linux/list.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
31 * dwc3_gadget_set_test_mode - enables usb2 test modes
32 * @dwc: pointer to our context structure
33 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
35 * Caller should take care of locking. This function will return 0 on
36 * success or -EINVAL if wrong Test Selector is passed.
38 int dwc3_gadget_set_test_mode(struct dwc3
*dwc
, int mode
)
42 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
43 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
57 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
63 * dwc3_gadget_get_link_state - gets current state of usb link
64 * @dwc: pointer to our context structure
66 * Caller should take care of locking. This function will
67 * return the link state on success (>= 0) or -ETIMEDOUT.
69 int dwc3_gadget_get_link_state(struct dwc3
*dwc
)
73 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
75 return DWC3_DSTS_USBLNKST(reg
);
79 * dwc3_gadget_set_link_state - sets usb link to a particular state
80 * @dwc: pointer to our context structure
81 * @state: the state to put link into
83 * Caller should take care of locking. This function will
84 * return 0 on success or -ETIMEDOUT.
86 int dwc3_gadget_set_link_state(struct dwc3
*dwc
, enum dwc3_link_state state
)
92 * Wait until device controller is ready. Only applies to 1.94a and
95 if (dwc
->revision
>= DWC3_REVISION_194A
) {
97 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
98 if (reg
& DWC3_DSTS_DCNRD
)
108 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
109 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
111 /* set requested state */
112 reg
|= DWC3_DCTL_ULSTCHNGREQ(state
);
113 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
116 * The following code is racy when called from dwc3_gadget_wakeup,
117 * and is not needed, at least on newer versions
119 if (dwc
->revision
>= DWC3_REVISION_194A
)
122 /* wait for a change in DSTS */
125 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
127 if (DWC3_DSTS_USBLNKST(reg
) == state
)
137 * dwc3_ep_inc_trb - increment a trb index.
138 * @index: Pointer to the TRB index to increment.
140 * The index should never point to the link TRB. After incrementing,
141 * if it is point to the link TRB, wrap around to the beginning. The
142 * link TRB is always at the last TRB entry.
144 static void dwc3_ep_inc_trb(u8
*index
)
147 if (*index
== (DWC3_TRB_NUM
- 1))
152 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
153 * @dep: The endpoint whose enqueue pointer we're incrementing
155 static void dwc3_ep_inc_enq(struct dwc3_ep
*dep
)
157 dwc3_ep_inc_trb(&dep
->trb_enqueue
);
161 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
164 static void dwc3_ep_inc_deq(struct dwc3_ep
*dep
)
166 dwc3_ep_inc_trb(&dep
->trb_dequeue
);
170 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
171 * @dep: The endpoint to whom the request belongs to
172 * @req: The request we're giving back
173 * @status: completion code for the request
175 * Must be called with controller's lock held and interrupts disabled. This
176 * function will unmap @req and call its ->complete() callback to notify upper
177 * layers that it has completed.
179 void dwc3_gadget_giveback(struct dwc3_ep
*dep
, struct dwc3_request
*req
,
182 struct dwc3
*dwc
= dep
->dwc
;
184 req
->started
= false;
185 list_del(&req
->list
);
188 if (req
->request
.status
== -EINPROGRESS
)
189 req
->request
.status
= status
;
192 usb_gadget_unmap_request_by_dev(dwc
->sysdev
,
193 &req
->request
, req
->direction
);
197 trace_dwc3_gadget_giveback(req
);
199 spin_unlock(&dwc
->lock
);
200 usb_gadget_giveback_request(&dep
->endpoint
, &req
->request
);
201 spin_lock(&dwc
->lock
);
204 pm_runtime_put(dwc
->dev
);
208 * dwc3_send_gadget_generic_command - issue a generic command for the controller
209 * @dwc: pointer to the controller context
210 * @cmd: the command to be issued
211 * @param: command parameter
213 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
214 * and wait for its completion.
216 int dwc3_send_gadget_generic_command(struct dwc3
*dwc
, unsigned cmd
, u32 param
)
223 dwc3_writel(dwc
->regs
, DWC3_DGCMDPAR
, param
);
224 dwc3_writel(dwc
->regs
, DWC3_DGCMD
, cmd
| DWC3_DGCMD_CMDACT
);
227 reg
= dwc3_readl(dwc
->regs
, DWC3_DGCMD
);
228 if (!(reg
& DWC3_DGCMD_CMDACT
)) {
229 status
= DWC3_DGCMD_STATUS(reg
);
241 trace_dwc3_gadget_generic_cmd(cmd
, param
, status
);
246 static int __dwc3_gadget_wakeup(struct dwc3
*dwc
);
249 * dwc3_send_gadget_ep_cmd - issue an endpoint command
250 * @dep: the endpoint to which the command is going to be issued
251 * @cmd: the command to be issued
252 * @params: parameters to the command
254 * Caller should handle locking. This function will issue @cmd with given
255 * @params to @dep and wait for its completion.
257 int dwc3_send_gadget_ep_cmd(struct dwc3_ep
*dep
, unsigned cmd
,
258 struct dwc3_gadget_ep_cmd_params
*params
)
260 const struct usb_endpoint_descriptor
*desc
= dep
->endpoint
.desc
;
261 struct dwc3
*dwc
= dep
->dwc
;
270 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
271 * we're issuing an endpoint command, we must check if
272 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
274 * We will also set SUSPHY bit to what it was before returning as stated
275 * by the same section on Synopsys databook.
277 if (dwc
->gadget
.speed
<= USB_SPEED_HIGH
) {
278 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
279 if (unlikely(reg
& DWC3_GUSB2PHYCFG_SUSPHY
)) {
281 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
282 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
286 if (DWC3_DEPCMD_CMD(cmd
) == DWC3_DEPCMD_STARTTRANSFER
) {
289 needs_wakeup
= (dwc
->link_state
== DWC3_LINK_STATE_U1
||
290 dwc
->link_state
== DWC3_LINK_STATE_U2
||
291 dwc
->link_state
== DWC3_LINK_STATE_U3
);
293 if (unlikely(needs_wakeup
)) {
294 ret
= __dwc3_gadget_wakeup(dwc
);
295 dev_WARN_ONCE(dwc
->dev
, ret
, "wakeup failed --> %d\n",
300 dwc3_writel(dep
->regs
, DWC3_DEPCMDPAR0
, params
->param0
);
301 dwc3_writel(dep
->regs
, DWC3_DEPCMDPAR1
, params
->param1
);
302 dwc3_writel(dep
->regs
, DWC3_DEPCMDPAR2
, params
->param2
);
305 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
306 * not relying on XferNotReady, we can make use of a special "No
307 * Response Update Transfer" command where we should clear both CmdAct
310 * With this, we don't need to wait for command completion and can
311 * straight away issue further commands to the endpoint.
313 * NOTICE: We're making an assumption that control endpoints will never
314 * make use of Update Transfer command. This is a safe assumption
315 * because we can never have more than one request at a time with
316 * Control Endpoints. If anybody changes that assumption, this chunk
317 * needs to be updated accordingly.
319 if (DWC3_DEPCMD_CMD(cmd
) == DWC3_DEPCMD_UPDATETRANSFER
&&
320 !usb_endpoint_xfer_isoc(desc
))
321 cmd
&= ~(DWC3_DEPCMD_CMDIOC
| DWC3_DEPCMD_CMDACT
);
323 cmd
|= DWC3_DEPCMD_CMDACT
;
325 dwc3_writel(dep
->regs
, DWC3_DEPCMD
, cmd
);
327 reg
= dwc3_readl(dep
->regs
, DWC3_DEPCMD
);
328 if (!(reg
& DWC3_DEPCMD_CMDACT
)) {
329 cmd_status
= DWC3_DEPCMD_STATUS(reg
);
331 switch (cmd_status
) {
335 case DEPEVT_TRANSFER_NO_RESOURCE
:
338 case DEPEVT_TRANSFER_BUS_EXPIRY
:
340 * SW issues START TRANSFER command to
341 * isochronous ep with future frame interval. If
342 * future interval time has already passed when
343 * core receives the command, it will respond
344 * with an error status of 'Bus Expiry'.
346 * Instead of always returning -EINVAL, let's
347 * give a hint to the gadget driver that this is
348 * the case by returning -EAGAIN.
353 dev_WARN(dwc
->dev
, "UNKNOWN cmd status\n");
362 cmd_status
= -ETIMEDOUT
;
365 trace_dwc3_gadget_ep_cmd(dep
, cmd
, params
, cmd_status
);
368 switch (DWC3_DEPCMD_CMD(cmd
)) {
369 case DWC3_DEPCMD_STARTTRANSFER
:
370 dep
->flags
|= DWC3_EP_TRANSFER_STARTED
;
372 case DWC3_DEPCMD_ENDTRANSFER
:
373 dep
->flags
&= ~DWC3_EP_TRANSFER_STARTED
;
381 if (unlikely(susphy
)) {
382 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
383 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
384 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
390 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep
*dep
)
392 struct dwc3
*dwc
= dep
->dwc
;
393 struct dwc3_gadget_ep_cmd_params params
;
394 u32 cmd
= DWC3_DEPCMD_CLEARSTALL
;
397 * As of core revision 2.60a the recommended programming model
398 * is to set the ClearPendIN bit when issuing a Clear Stall EP
399 * command for IN endpoints. This is to prevent an issue where
400 * some (non-compliant) hosts may not send ACK TPs for pending
401 * IN transfers due to a mishandled error condition. Synopsys
404 if (dep
->direction
&& (dwc
->revision
>= DWC3_REVISION_260A
) &&
405 (dwc
->gadget
.speed
>= USB_SPEED_SUPER
))
406 cmd
|= DWC3_DEPCMD_CLEARPENDIN
;
408 memset(¶ms
, 0, sizeof(params
));
410 return dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
413 static dma_addr_t
dwc3_trb_dma_offset(struct dwc3_ep
*dep
,
414 struct dwc3_trb
*trb
)
416 u32 offset
= (char *) trb
- (char *) dep
->trb_pool
;
418 return dep
->trb_pool_dma
+ offset
;
421 static int dwc3_alloc_trb_pool(struct dwc3_ep
*dep
)
423 struct dwc3
*dwc
= dep
->dwc
;
428 dep
->trb_pool
= dma_alloc_coherent(dwc
->sysdev
,
429 sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
430 &dep
->trb_pool_dma
, GFP_KERNEL
);
431 if (!dep
->trb_pool
) {
432 dev_err(dep
->dwc
->dev
, "failed to allocate trb pool for %s\n",
440 static void dwc3_free_trb_pool(struct dwc3_ep
*dep
)
442 struct dwc3
*dwc
= dep
->dwc
;
444 dma_free_coherent(dwc
->sysdev
, sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
445 dep
->trb_pool
, dep
->trb_pool_dma
);
447 dep
->trb_pool
= NULL
;
448 dep
->trb_pool_dma
= 0;
451 static int dwc3_gadget_set_xfer_resource(struct dwc3
*dwc
, struct dwc3_ep
*dep
);
454 * dwc3_gadget_start_config - configure ep resources
455 * @dwc: pointer to our controller context structure
456 * @dep: endpoint that is being enabled
458 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
459 * completion, it will set Transfer Resource for all available endpoints.
461 * The assignment of transfer resources cannot perfectly follow the data book
462 * due to the fact that the controller driver does not have all knowledge of the
463 * configuration in advance. It is given this information piecemeal by the
464 * composite gadget framework after every SET_CONFIGURATION and
465 * SET_INTERFACE. Trying to follow the databook programming model in this
466 * scenario can cause errors. For two reasons:
468 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
469 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
470 * incorrect in the scenario of multiple interfaces.
472 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
473 * endpoint on alt setting (8.1.6).
475 * The following simplified method is used instead:
477 * All hardware endpoints can be assigned a transfer resource and this setting
478 * will stay persistent until either a core reset or hibernation. So whenever we
479 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
480 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
481 * guaranteed that there are as many transfer resources as endpoints.
483 * This function is called for each endpoint when it is being enabled but is
484 * triggered only when called for EP0-out, which always happens first, and which
485 * should only happen in one of the above conditions.
487 static int dwc3_gadget_start_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
489 struct dwc3_gadget_ep_cmd_params params
;
497 memset(¶ms
, 0x00, sizeof(params
));
498 cmd
= DWC3_DEPCMD_DEPSTARTCFG
;
500 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
504 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
505 struct dwc3_ep
*dep
= dwc
->eps
[i
];
510 ret
= dwc3_gadget_set_xfer_resource(dwc
, dep
);
518 static int dwc3_gadget_set_ep_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
519 bool modify
, bool restore
)
521 const struct usb_ss_ep_comp_descriptor
*comp_desc
;
522 const struct usb_endpoint_descriptor
*desc
;
523 struct dwc3_gadget_ep_cmd_params params
;
525 if (dev_WARN_ONCE(dwc
->dev
, modify
&& restore
,
526 "Can't modify and restore\n"))
529 comp_desc
= dep
->endpoint
.comp_desc
;
530 desc
= dep
->endpoint
.desc
;
532 memset(¶ms
, 0x00, sizeof(params
));
534 params
.param0
= DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc
))
535 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc
));
537 /* Burst size is only needed in SuperSpeed mode */
538 if (dwc
->gadget
.speed
>= USB_SPEED_SUPER
) {
539 u32 burst
= dep
->endpoint
.maxburst
;
540 params
.param0
|= DWC3_DEPCFG_BURST_SIZE(burst
- 1);
544 params
.param0
|= DWC3_DEPCFG_ACTION_MODIFY
;
545 } else if (restore
) {
546 params
.param0
|= DWC3_DEPCFG_ACTION_RESTORE
;
547 params
.param2
|= dep
->saved_state
;
549 params
.param0
|= DWC3_DEPCFG_ACTION_INIT
;
552 if (usb_endpoint_xfer_control(desc
))
553 params
.param1
= DWC3_DEPCFG_XFER_COMPLETE_EN
;
555 if (dep
->number
<= 1 || usb_endpoint_xfer_isoc(desc
))
556 params
.param1
|= DWC3_DEPCFG_XFER_NOT_READY_EN
;
558 if (usb_ss_max_streams(comp_desc
) && usb_endpoint_xfer_bulk(desc
)) {
559 params
.param1
|= DWC3_DEPCFG_STREAM_CAPABLE
560 | DWC3_DEPCFG_STREAM_EVENT_EN
;
561 dep
->stream_capable
= true;
564 if (!usb_endpoint_xfer_control(desc
))
565 params
.param1
|= DWC3_DEPCFG_XFER_IN_PROGRESS_EN
;
568 * We are doing 1:1 mapping for endpoints, meaning
569 * Physical Endpoints 2 maps to Logical Endpoint 2 and
570 * so on. We consider the direction bit as part of the physical
571 * endpoint number. So USB endpoint 0x81 is 0x03.
573 params
.param1
|= DWC3_DEPCFG_EP_NUMBER(dep
->number
);
576 * We must use the lower 16 TX FIFOs even though
580 params
.param0
|= DWC3_DEPCFG_FIFO_NUMBER(dep
->number
>> 1);
582 if (desc
->bInterval
) {
583 params
.param1
|= DWC3_DEPCFG_BINTERVAL_M1(desc
->bInterval
- 1);
584 dep
->interval
= 1 << (desc
->bInterval
- 1);
587 return dwc3_send_gadget_ep_cmd(dep
, DWC3_DEPCMD_SETEPCONFIG
, ¶ms
);
590 static int dwc3_gadget_set_xfer_resource(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
592 struct dwc3_gadget_ep_cmd_params params
;
594 memset(¶ms
, 0x00, sizeof(params
));
596 params
.param0
= DWC3_DEPXFERCFG_NUM_XFER_RES(1);
598 return dwc3_send_gadget_ep_cmd(dep
, DWC3_DEPCMD_SETTRANSFRESOURCE
,
603 * __dwc3_gadget_ep_enable - initializes a hw endpoint
604 * @dep: endpoint to be initialized
605 * @modify: if true, modify existing endpoint configuration
606 * @restore: if true, restore endpoint configuration from scratch buffer
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.
611 static int __dwc3_gadget_ep_enable(struct dwc3_ep
*dep
,
612 bool modify
, bool restore
)
614 const struct usb_endpoint_descriptor
*desc
= dep
->endpoint
.desc
;
615 struct dwc3
*dwc
= dep
->dwc
;
620 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
621 ret
= dwc3_gadget_start_config(dwc
, dep
);
626 ret
= dwc3_gadget_set_ep_config(dwc
, dep
, modify
, restore
);
630 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
631 struct dwc3_trb
*trb_st_hw
;
632 struct dwc3_trb
*trb_link
;
634 dep
->type
= usb_endpoint_type(desc
);
635 dep
->flags
|= DWC3_EP_ENABLED
;
636 dep
->flags
&= ~DWC3_EP_END_TRANSFER_PENDING
;
638 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
639 reg
|= DWC3_DALEPENA_EP(dep
->number
);
640 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
642 init_waitqueue_head(&dep
->wait_end_transfer
);
644 if (usb_endpoint_xfer_control(desc
))
647 /* Initialize the TRB ring */
648 dep
->trb_dequeue
= 0;
649 dep
->trb_enqueue
= 0;
650 memset(dep
->trb_pool
, 0,
651 sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
);
653 /* Link TRB. The HWO bit is never reset */
654 trb_st_hw
= &dep
->trb_pool
[0];
656 trb_link
= &dep
->trb_pool
[DWC3_TRB_NUM
- 1];
657 trb_link
->bpl
= lower_32_bits(dwc3_trb_dma_offset(dep
, trb_st_hw
));
658 trb_link
->bph
= upper_32_bits(dwc3_trb_dma_offset(dep
, trb_st_hw
));
659 trb_link
->ctrl
|= DWC3_TRBCTL_LINK_TRB
;
660 trb_link
->ctrl
|= DWC3_TRB_CTRL_HWO
;
664 * Issue StartTransfer here with no-op TRB so we can always rely on No
665 * Response Update Transfer command.
667 if (usb_endpoint_xfer_bulk(desc
)) {
668 struct dwc3_gadget_ep_cmd_params params
;
669 struct dwc3_trb
*trb
;
673 memset(¶ms
, 0, sizeof(params
));
674 trb
= &dep
->trb_pool
[0];
675 trb_dma
= dwc3_trb_dma_offset(dep
, trb
);
677 params
.param0
= upper_32_bits(trb_dma
);
678 params
.param1
= lower_32_bits(trb_dma
);
680 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
682 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
686 dep
->flags
|= DWC3_EP_BUSY
;
688 dep
->resource_index
= dwc3_gadget_ep_get_transfer_index(dep
);
689 WARN_ON_ONCE(!dep
->resource_index
);
694 trace_dwc3_gadget_ep_enable(dep
);
699 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
, bool force
);
700 static void dwc3_remove_requests(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
702 struct dwc3_request
*req
;
704 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
706 /* - giveback all requests to gadget driver */
707 while (!list_empty(&dep
->started_list
)) {
708 req
= next_request(&dep
->started_list
);
710 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
713 while (!list_empty(&dep
->pending_list
)) {
714 req
= next_request(&dep
->pending_list
);
716 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
721 * __dwc3_gadget_ep_disable - disables a hw endpoint
722 * @dep: the endpoint to disable
724 * This function undoes what __dwc3_gadget_ep_enable did and also removes
725 * requests which are currently being processed by the hardware and those which
726 * are not yet scheduled.
728 * Caller should take care of locking.
730 static int __dwc3_gadget_ep_disable(struct dwc3_ep
*dep
)
732 struct dwc3
*dwc
= dep
->dwc
;
735 trace_dwc3_gadget_ep_disable(dep
);
737 dwc3_remove_requests(dwc
, dep
);
739 /* make sure HW endpoint isn't stalled */
740 if (dep
->flags
& DWC3_EP_STALL
)
741 __dwc3_gadget_ep_set_halt(dep
, 0, false);
743 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
744 reg
&= ~DWC3_DALEPENA_EP(dep
->number
);
745 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
747 dep
->stream_capable
= false;
749 dep
->flags
&= DWC3_EP_END_TRANSFER_PENDING
;
751 /* Clear out the ep descriptors for non-ep0 */
752 if (dep
->number
> 1) {
753 dep
->endpoint
.comp_desc
= NULL
;
754 dep
->endpoint
.desc
= NULL
;
760 /* -------------------------------------------------------------------------- */
762 static int dwc3_gadget_ep0_enable(struct usb_ep
*ep
,
763 const struct usb_endpoint_descriptor
*desc
)
768 static int dwc3_gadget_ep0_disable(struct usb_ep
*ep
)
773 /* -------------------------------------------------------------------------- */
775 static int dwc3_gadget_ep_enable(struct usb_ep
*ep
,
776 const struct usb_endpoint_descriptor
*desc
)
783 if (!ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
784 pr_debug("dwc3: invalid parameters\n");
788 if (!desc
->wMaxPacketSize
) {
789 pr_debug("dwc3: missing wMaxPacketSize\n");
793 dep
= to_dwc3_ep(ep
);
796 if (dev_WARN_ONCE(dwc
->dev
, dep
->flags
& DWC3_EP_ENABLED
,
797 "%s is already enabled\n",
801 spin_lock_irqsave(&dwc
->lock
, flags
);
802 ret
= __dwc3_gadget_ep_enable(dep
, false, false);
803 spin_unlock_irqrestore(&dwc
->lock
, flags
);
808 static int dwc3_gadget_ep_disable(struct usb_ep
*ep
)
816 pr_debug("dwc3: invalid parameters\n");
820 dep
= to_dwc3_ep(ep
);
823 if (dev_WARN_ONCE(dwc
->dev
, !(dep
->flags
& DWC3_EP_ENABLED
),
824 "%s is already disabled\n",
828 spin_lock_irqsave(&dwc
->lock
, flags
);
829 ret
= __dwc3_gadget_ep_disable(dep
);
830 spin_unlock_irqrestore(&dwc
->lock
, flags
);
835 static struct usb_request
*dwc3_gadget_ep_alloc_request(struct usb_ep
*ep
,
838 struct dwc3_request
*req
;
839 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
841 req
= kzalloc(sizeof(*req
), gfp_flags
);
845 req
->epnum
= dep
->number
;
848 dep
->allocated_requests
++;
850 trace_dwc3_alloc_request(req
);
852 return &req
->request
;
855 static void dwc3_gadget_ep_free_request(struct usb_ep
*ep
,
856 struct usb_request
*request
)
858 struct dwc3_request
*req
= to_dwc3_request(request
);
859 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
861 dep
->allocated_requests
--;
862 trace_dwc3_free_request(req
);
866 static u32
dwc3_calc_trbs_left(struct dwc3_ep
*dep
);
868 static void __dwc3_prepare_one_trb(struct dwc3_ep
*dep
, struct dwc3_trb
*trb
,
869 dma_addr_t dma
, unsigned length
, unsigned chain
, unsigned node
,
870 unsigned stream_id
, unsigned short_not_ok
, unsigned no_interrupt
)
872 struct dwc3
*dwc
= dep
->dwc
;
873 struct usb_gadget
*gadget
= &dwc
->gadget
;
874 enum usb_device_speed speed
= gadget
->speed
;
876 dwc3_ep_inc_enq(dep
);
878 trb
->size
= DWC3_TRB_SIZE_LENGTH(length
);
879 trb
->bpl
= lower_32_bits(dma
);
880 trb
->bph
= upper_32_bits(dma
);
882 switch (usb_endpoint_type(dep
->endpoint
.desc
)) {
883 case USB_ENDPOINT_XFER_CONTROL
:
884 trb
->ctrl
= DWC3_TRBCTL_CONTROL_SETUP
;
887 case USB_ENDPOINT_XFER_ISOC
:
889 trb
->ctrl
= DWC3_TRBCTL_ISOCHRONOUS_FIRST
;
892 * USB Specification 2.0 Section 5.9.2 states that: "If
893 * there is only a single transaction in the microframe,
894 * only a DATA0 data packet PID is used. If there are
895 * two transactions per microframe, DATA1 is used for
896 * the first transaction data packet and DATA0 is used
897 * for the second transaction data packet. If there are
898 * three transactions per microframe, DATA2 is used for
899 * the first transaction data packet, DATA1 is used for
900 * the second, and DATA0 is used for the third."
902 * IOW, we should satisfy the following cases:
904 * 1) length <= maxpacket
907 * 2) maxpacket < length <= (2 * maxpacket)
910 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
911 * - DATA2, DATA1, DATA0
913 if (speed
== USB_SPEED_HIGH
) {
914 struct usb_ep
*ep
= &dep
->endpoint
;
915 unsigned int mult
= 2;
916 unsigned int maxp
= usb_endpoint_maxp(ep
->desc
);
918 if (length
<= (2 * maxp
))
924 trb
->size
|= DWC3_TRB_SIZE_PCM1(mult
);
927 trb
->ctrl
= DWC3_TRBCTL_ISOCHRONOUS
;
930 /* always enable Interrupt on Missed ISOC */
931 trb
->ctrl
|= DWC3_TRB_CTRL_ISP_IMI
;
934 case USB_ENDPOINT_XFER_BULK
:
935 case USB_ENDPOINT_XFER_INT
:
936 trb
->ctrl
= DWC3_TRBCTL_NORMAL
;
940 * This is only possible with faulty memory because we
941 * checked it already :)
943 dev_WARN(dwc
->dev
, "Unknown endpoint type %d\n",
944 usb_endpoint_type(dep
->endpoint
.desc
));
947 /* always enable Continue on Short Packet */
948 if (usb_endpoint_dir_out(dep
->endpoint
.desc
)) {
949 trb
->ctrl
|= DWC3_TRB_CTRL_CSP
;
952 trb
->ctrl
|= DWC3_TRB_CTRL_ISP_IMI
;
955 if ((!no_interrupt
&& !chain
) ||
956 (dwc3_calc_trbs_left(dep
) == 0))
957 trb
->ctrl
|= DWC3_TRB_CTRL_IOC
;
960 trb
->ctrl
|= DWC3_TRB_CTRL_CHN
;
962 if (usb_endpoint_xfer_bulk(dep
->endpoint
.desc
) && dep
->stream_capable
)
963 trb
->ctrl
|= DWC3_TRB_CTRL_SID_SOFN(stream_id
);
965 trb
->ctrl
|= DWC3_TRB_CTRL_HWO
;
967 trace_dwc3_prepare_trb(dep
, trb
);
971 * dwc3_prepare_one_trb - setup one TRB from one request
972 * @dep: endpoint for which this request is prepared
973 * @req: dwc3_request pointer
974 * @chain: should this TRB be chained to the next?
975 * @node: only for isochronous endpoints. First TRB needs different type.
977 static void dwc3_prepare_one_trb(struct dwc3_ep
*dep
,
978 struct dwc3_request
*req
, unsigned chain
, unsigned node
)
980 struct dwc3_trb
*trb
;
981 unsigned length
= req
->request
.length
;
982 unsigned stream_id
= req
->request
.stream_id
;
983 unsigned short_not_ok
= req
->request
.short_not_ok
;
984 unsigned no_interrupt
= req
->request
.no_interrupt
;
985 dma_addr_t dma
= req
->request
.dma
;
987 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
990 dwc3_gadget_move_started_request(req
);
992 req
->trb_dma
= dwc3_trb_dma_offset(dep
, trb
);
993 dep
->queued_requests
++;
996 __dwc3_prepare_one_trb(dep
, trb
, dma
, length
, chain
, node
,
997 stream_id
, short_not_ok
, no_interrupt
);
1001 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1002 * @dep: The endpoint with the TRB ring
1003 * @index: The index of the current TRB in the ring
1005 * Returns the TRB prior to the one pointed to by the index. If the
1006 * index is 0, we will wrap backwards, skip the link TRB, and return
1007 * the one just before that.
1009 static struct dwc3_trb
*dwc3_ep_prev_trb(struct dwc3_ep
*dep
, u8 index
)
1014 tmp
= DWC3_TRB_NUM
- 1;
1016 return &dep
->trb_pool
[tmp
- 1];
1019 static u32
dwc3_calc_trbs_left(struct dwc3_ep
*dep
)
1021 struct dwc3_trb
*tmp
;
1025 * If enqueue & dequeue are equal than it is either full or empty.
1027 * One way to know for sure is if the TRB right before us has HWO bit
1028 * set or not. If it has, then we're definitely full and can't fit any
1029 * more transfers in our ring.
1031 if (dep
->trb_enqueue
== dep
->trb_dequeue
) {
1032 tmp
= dwc3_ep_prev_trb(dep
, dep
->trb_enqueue
);
1033 if (tmp
->ctrl
& DWC3_TRB_CTRL_HWO
)
1036 return DWC3_TRB_NUM
- 1;
1039 trbs_left
= dep
->trb_dequeue
- dep
->trb_enqueue
;
1040 trbs_left
&= (DWC3_TRB_NUM
- 1);
1042 if (dep
->trb_dequeue
< dep
->trb_enqueue
)
1048 static void dwc3_prepare_one_trb_sg(struct dwc3_ep
*dep
,
1049 struct dwc3_request
*req
)
1051 struct scatterlist
*sg
= req
->sg
;
1052 struct scatterlist
*s
;
1055 for_each_sg(sg
, s
, req
->num_pending_sgs
, i
) {
1056 unsigned int length
= req
->request
.length
;
1057 unsigned int maxp
= usb_endpoint_maxp(dep
->endpoint
.desc
);
1058 unsigned int rem
= length
% maxp
;
1059 unsigned chain
= true;
1064 if (rem
&& usb_endpoint_dir_out(dep
->endpoint
.desc
) && !chain
) {
1065 struct dwc3
*dwc
= dep
->dwc
;
1066 struct dwc3_trb
*trb
;
1068 req
->unaligned
= true;
1070 /* prepare normal TRB */
1071 dwc3_prepare_one_trb(dep
, req
, true, i
);
1073 /* Now prepare one extra TRB to align transfer size */
1074 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
1075 __dwc3_prepare_one_trb(dep
, trb
, dwc
->bounce_addr
,
1076 maxp
- rem
, false, 0,
1077 req
->request
.stream_id
,
1078 req
->request
.short_not_ok
,
1079 req
->request
.no_interrupt
);
1081 dwc3_prepare_one_trb(dep
, req
, chain
, i
);
1084 if (!dwc3_calc_trbs_left(dep
))
1089 static void dwc3_prepare_one_trb_linear(struct dwc3_ep
*dep
,
1090 struct dwc3_request
*req
)
1092 unsigned int length
= req
->request
.length
;
1093 unsigned int maxp
= usb_endpoint_maxp(dep
->endpoint
.desc
);
1094 unsigned int rem
= length
% maxp
;
1096 if (rem
&& usb_endpoint_dir_out(dep
->endpoint
.desc
)) {
1097 struct dwc3
*dwc
= dep
->dwc
;
1098 struct dwc3_trb
*trb
;
1100 req
->unaligned
= true;
1102 /* prepare normal TRB */
1103 dwc3_prepare_one_trb(dep
, req
, true, 0);
1105 /* Now prepare one extra TRB to align transfer size */
1106 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
1107 __dwc3_prepare_one_trb(dep
, trb
, dwc
->bounce_addr
, maxp
- rem
,
1108 false, 0, req
->request
.stream_id
,
1109 req
->request
.short_not_ok
,
1110 req
->request
.no_interrupt
);
1111 } else if (req
->request
.zero
&& req
->request
.length
&&
1112 (IS_ALIGNED(req
->request
.length
,dep
->endpoint
.maxpacket
))) {
1113 struct dwc3
*dwc
= dep
->dwc
;
1114 struct dwc3_trb
*trb
;
1118 /* prepare normal TRB */
1119 dwc3_prepare_one_trb(dep
, req
, true, 0);
1121 /* Now prepare one extra TRB to handle ZLP */
1122 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
1123 __dwc3_prepare_one_trb(dep
, trb
, dwc
->bounce_addr
, 0,
1124 false, 0, req
->request
.stream_id
,
1125 req
->request
.short_not_ok
,
1126 req
->request
.no_interrupt
);
1128 dwc3_prepare_one_trb(dep
, req
, false, 0);
1133 * dwc3_prepare_trbs - setup TRBs from requests
1134 * @dep: endpoint for which requests are being prepared
1136 * The function goes through the requests list and sets up TRBs for the
1137 * transfers. The function returns once there are no more TRBs available or
1138 * it runs out of requests.
1140 static void dwc3_prepare_trbs(struct dwc3_ep
*dep
)
1142 struct dwc3_request
*req
, *n
;
1144 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM
);
1147 * We can get in a situation where there's a request in the started list
1148 * but there weren't enough TRBs to fully kick it in the first time
1149 * around, so it has been waiting for more TRBs to be freed up.
1151 * In that case, we should check if we have a request with pending_sgs
1152 * in the started list and prepare TRBs for that request first,
1153 * otherwise we will prepare TRBs completely out of order and that will
1156 list_for_each_entry(req
, &dep
->started_list
, list
) {
1157 if (req
->num_pending_sgs
> 0)
1158 dwc3_prepare_one_trb_sg(dep
, req
);
1160 if (!dwc3_calc_trbs_left(dep
))
1164 list_for_each_entry_safe(req
, n
, &dep
->pending_list
, list
) {
1165 struct dwc3
*dwc
= dep
->dwc
;
1168 ret
= usb_gadget_map_request_by_dev(dwc
->sysdev
, &req
->request
,
1173 req
->sg
= req
->request
.sg
;
1174 req
->num_pending_sgs
= req
->request
.num_mapped_sgs
;
1176 if (req
->num_pending_sgs
> 0)
1177 dwc3_prepare_one_trb_sg(dep
, req
);
1179 dwc3_prepare_one_trb_linear(dep
, req
);
1181 if (!dwc3_calc_trbs_left(dep
))
1186 static int __dwc3_gadget_kick_transfer(struct dwc3_ep
*dep
)
1188 struct dwc3_gadget_ep_cmd_params params
;
1189 struct dwc3_request
*req
;
1194 if (!dwc3_calc_trbs_left(dep
))
1197 starting
= !(dep
->flags
& DWC3_EP_BUSY
);
1199 dwc3_prepare_trbs(dep
);
1200 req
= next_request(&dep
->started_list
);
1202 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
1206 memset(¶ms
, 0, sizeof(params
));
1209 params
.param0
= upper_32_bits(req
->trb_dma
);
1210 params
.param1
= lower_32_bits(req
->trb_dma
);
1211 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
1213 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
))
1214 cmd
|= DWC3_DEPCMD_PARAM(dep
->frame_number
);
1216 cmd
= DWC3_DEPCMD_UPDATETRANSFER
|
1217 DWC3_DEPCMD_PARAM(dep
->resource_index
);
1220 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
1223 * FIXME we need to iterate over the list of requests
1224 * here and stop, unmap, free and del each of the linked
1225 * requests instead of what we do now.
1228 memset(req
->trb
, 0, sizeof(struct dwc3_trb
));
1229 dep
->queued_requests
--;
1230 dwc3_gadget_giveback(dep
, req
, ret
);
1234 dep
->flags
|= DWC3_EP_BUSY
;
1237 dep
->resource_index
= dwc3_gadget_ep_get_transfer_index(dep
);
1238 WARN_ON_ONCE(!dep
->resource_index
);
1244 static int __dwc3_gadget_get_frame(struct dwc3
*dwc
)
1248 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1249 return DWC3_DSTS_SOFFN(reg
);
1252 static void __dwc3_gadget_start_isoc(struct dwc3
*dwc
,
1253 struct dwc3_ep
*dep
, u32 cur_uf
)
1255 if (list_empty(&dep
->pending_list
)) {
1256 dev_info(dwc
->dev
, "%s: ran out of requests\n",
1258 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
1263 * Schedule the first trb for one interval in the future or at
1264 * least 4 microframes.
1266 dep
->frame_number
= cur_uf
+ max_t(u32
, 4, dep
->interval
);
1267 __dwc3_gadget_kick_transfer(dep
);
1270 static void dwc3_gadget_start_isoc(struct dwc3
*dwc
,
1271 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
)
1275 mask
= ~(dep
->interval
- 1);
1276 cur_uf
= event
->parameters
& mask
;
1278 __dwc3_gadget_start_isoc(dwc
, dep
, cur_uf
);
1281 static int __dwc3_gadget_ep_queue(struct dwc3_ep
*dep
, struct dwc3_request
*req
)
1283 struct dwc3
*dwc
= dep
->dwc
;
1285 if (!dep
->endpoint
.desc
) {
1286 dev_err(dwc
->dev
, "%s: can't queue to disabled endpoint\n",
1291 if (WARN(req
->dep
!= dep
, "request %pK belongs to '%s'\n",
1292 &req
->request
, req
->dep
->name
))
1295 pm_runtime_get(dwc
->dev
);
1297 req
->request
.actual
= 0;
1298 req
->request
.status
= -EINPROGRESS
;
1299 req
->direction
= dep
->direction
;
1300 req
->epnum
= dep
->number
;
1302 trace_dwc3_ep_queue(req
);
1304 list_add_tail(&req
->list
, &dep
->pending_list
);
1307 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1308 * wait for a XferNotReady event so we will know what's the current
1309 * (micro-)frame number.
1311 * Without this trick, we are very, very likely gonna get Bus Expiry
1312 * errors which will force us issue EndTransfer command.
1314 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
1315 if ((dep
->flags
& DWC3_EP_PENDING_REQUEST
)) {
1316 if (dep
->flags
& DWC3_EP_TRANSFER_STARTED
) {
1317 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
1318 dep
->flags
= DWC3_EP_ENABLED
;
1322 cur_uf
= __dwc3_gadget_get_frame(dwc
);
1323 __dwc3_gadget_start_isoc(dwc
, dep
, cur_uf
);
1324 dep
->flags
&= ~DWC3_EP_PENDING_REQUEST
;
1329 if ((dep
->flags
& DWC3_EP_BUSY
) &&
1330 !(dep
->flags
& DWC3_EP_MISSED_ISOC
))
1337 return __dwc3_gadget_kick_transfer(dep
);
1340 static int dwc3_gadget_ep_queue(struct usb_ep
*ep
, struct usb_request
*request
,
1343 struct dwc3_request
*req
= to_dwc3_request(request
);
1344 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1345 struct dwc3
*dwc
= dep
->dwc
;
1347 unsigned long flags
;
1351 spin_lock_irqsave(&dwc
->lock
, flags
);
1352 ret
= __dwc3_gadget_ep_queue(dep
, req
);
1353 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1358 static int dwc3_gadget_ep_dequeue(struct usb_ep
*ep
,
1359 struct usb_request
*request
)
1361 struct dwc3_request
*req
= to_dwc3_request(request
);
1362 struct dwc3_request
*r
= NULL
;
1364 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1365 struct dwc3
*dwc
= dep
->dwc
;
1367 unsigned long flags
;
1370 trace_dwc3_ep_dequeue(req
);
1372 spin_lock_irqsave(&dwc
->lock
, flags
);
1374 list_for_each_entry(r
, &dep
->pending_list
, list
) {
1380 list_for_each_entry(r
, &dep
->started_list
, list
) {
1385 /* wait until it is processed */
1386 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
1389 * If request was already started, this means we had to
1390 * stop the transfer. With that we also need to ignore
1391 * all TRBs used by the request, however TRBs can only
1392 * be modified after completion of END_TRANSFER
1393 * command. So what we do here is that we wait for
1394 * END_TRANSFER completion and only after that, we jump
1395 * over TRBs by clearing HWO and incrementing dequeue
1398 * Note that we have 2 possible types of transfers here:
1400 * i) Linear buffer request
1401 * ii) SG-list based request
1403 * SG-list based requests will have r->num_pending_sgs
1404 * set to a valid number (> 0). Linear requests,
1405 * normally use a single TRB.
1407 * For each of these two cases, if r->unaligned flag is
1408 * set, one extra TRB has been used to align transfer
1409 * size to wMaxPacketSize.
1411 * All of these cases need to be taken into
1412 * consideration so we don't mess up our TRB ring
1415 wait_event_lock_irq(dep
->wait_end_transfer
,
1416 !(dep
->flags
& DWC3_EP_END_TRANSFER_PENDING
),
1422 if (r
->num_pending_sgs
) {
1423 struct dwc3_trb
*trb
;
1426 for (i
= 0; i
< r
->num_pending_sgs
; i
++) {
1428 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
1429 dwc3_ep_inc_deq(dep
);
1432 if (r
->unaligned
|| r
->zero
) {
1433 trb
= r
->trb
+ r
->num_pending_sgs
+ 1;
1434 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
1435 dwc3_ep_inc_deq(dep
);
1438 struct dwc3_trb
*trb
= r
->trb
;
1440 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
1441 dwc3_ep_inc_deq(dep
);
1443 if (r
->unaligned
|| r
->zero
) {
1445 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
1446 dwc3_ep_inc_deq(dep
);
1451 dev_err(dwc
->dev
, "request %pK was not queued to %s\n",
1458 /* giveback the request */
1459 dep
->queued_requests
--;
1460 dwc3_gadget_giveback(dep
, req
, -ECONNRESET
);
1463 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1468 int __dwc3_gadget_ep_set_halt(struct dwc3_ep
*dep
, int value
, int protocol
)
1470 struct dwc3_gadget_ep_cmd_params params
;
1471 struct dwc3
*dwc
= dep
->dwc
;
1474 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
1475 dev_err(dwc
->dev
, "%s is of Isochronous type\n", dep
->name
);
1479 memset(¶ms
, 0x00, sizeof(params
));
1482 struct dwc3_trb
*trb
;
1484 unsigned transfer_in_flight
;
1487 if (dep
->flags
& DWC3_EP_STALL
)
1490 if (dep
->number
> 1)
1491 trb
= dwc3_ep_prev_trb(dep
, dep
->trb_enqueue
);
1493 trb
= &dwc
->ep0_trb
[dep
->trb_enqueue
];
1495 transfer_in_flight
= trb
->ctrl
& DWC3_TRB_CTRL_HWO
;
1496 started
= !list_empty(&dep
->started_list
);
1498 if (!protocol
&& ((dep
->direction
&& transfer_in_flight
) ||
1499 (!dep
->direction
&& started
))) {
1503 ret
= dwc3_send_gadget_ep_cmd(dep
, DWC3_DEPCMD_SETSTALL
,
1506 dev_err(dwc
->dev
, "failed to set STALL on %s\n",
1509 dep
->flags
|= DWC3_EP_STALL
;
1511 if (!(dep
->flags
& DWC3_EP_STALL
))
1514 ret
= dwc3_send_clear_stall_ep_cmd(dep
);
1516 dev_err(dwc
->dev
, "failed to clear STALL on %s\n",
1519 dep
->flags
&= ~(DWC3_EP_STALL
| DWC3_EP_WEDGE
);
1525 static int dwc3_gadget_ep_set_halt(struct usb_ep
*ep
, int value
)
1527 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1528 struct dwc3
*dwc
= dep
->dwc
;
1530 unsigned long flags
;
1534 spin_lock_irqsave(&dwc
->lock
, flags
);
1535 ret
= __dwc3_gadget_ep_set_halt(dep
, value
, false);
1536 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1541 static int dwc3_gadget_ep_set_wedge(struct usb_ep
*ep
)
1543 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1544 struct dwc3
*dwc
= dep
->dwc
;
1545 unsigned long flags
;
1548 spin_lock_irqsave(&dwc
->lock
, flags
);
1549 dep
->flags
|= DWC3_EP_WEDGE
;
1551 if (dep
->number
== 0 || dep
->number
== 1)
1552 ret
= __dwc3_gadget_ep0_set_halt(ep
, 1);
1554 ret
= __dwc3_gadget_ep_set_halt(dep
, 1, false);
1555 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1560 /* -------------------------------------------------------------------------- */
1562 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc
= {
1563 .bLength
= USB_DT_ENDPOINT_SIZE
,
1564 .bDescriptorType
= USB_DT_ENDPOINT
,
1565 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1568 static const struct usb_ep_ops dwc3_gadget_ep0_ops
= {
1569 .enable
= dwc3_gadget_ep0_enable
,
1570 .disable
= dwc3_gadget_ep0_disable
,
1571 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1572 .free_request
= dwc3_gadget_ep_free_request
,
1573 .queue
= dwc3_gadget_ep0_queue
,
1574 .dequeue
= dwc3_gadget_ep_dequeue
,
1575 .set_halt
= dwc3_gadget_ep0_set_halt
,
1576 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1579 static const struct usb_ep_ops dwc3_gadget_ep_ops
= {
1580 .enable
= dwc3_gadget_ep_enable
,
1581 .disable
= dwc3_gadget_ep_disable
,
1582 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1583 .free_request
= dwc3_gadget_ep_free_request
,
1584 .queue
= dwc3_gadget_ep_queue
,
1585 .dequeue
= dwc3_gadget_ep_dequeue
,
1586 .set_halt
= dwc3_gadget_ep_set_halt
,
1587 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1590 /* -------------------------------------------------------------------------- */
1592 static int dwc3_gadget_get_frame(struct usb_gadget
*g
)
1594 struct dwc3
*dwc
= gadget_to_dwc(g
);
1596 return __dwc3_gadget_get_frame(dwc
);
1599 static int __dwc3_gadget_wakeup(struct dwc3
*dwc
)
1610 * According to the Databook Remote wakeup request should
1611 * be issued only when the device is in early suspend state.
1613 * We can check that via USB Link State bits in DSTS register.
1615 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1617 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1618 if ((speed
== DWC3_DSTS_SUPERSPEED
) ||
1619 (speed
== DWC3_DSTS_SUPERSPEED_PLUS
))
1622 link_state
= DWC3_DSTS_USBLNKST(reg
);
1624 switch (link_state
) {
1625 case DWC3_LINK_STATE_RX_DET
: /* in HS, means Early Suspend */
1626 case DWC3_LINK_STATE_U3
: /* in HS, means SUSPEND */
1632 ret
= dwc3_gadget_set_link_state(dwc
, DWC3_LINK_STATE_RECOV
);
1634 dev_err(dwc
->dev
, "failed to put link in Recovery\n");
1638 /* Recent versions do this automatically */
1639 if (dwc
->revision
< DWC3_REVISION_194A
) {
1640 /* write zeroes to Link Change Request */
1641 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1642 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
1643 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1646 /* poll until Link State changes to ON */
1650 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1652 /* in HS, means ON */
1653 if (DWC3_DSTS_USBLNKST(reg
) == DWC3_LINK_STATE_U0
)
1657 if (DWC3_DSTS_USBLNKST(reg
) != DWC3_LINK_STATE_U0
) {
1658 dev_err(dwc
->dev
, "failed to send remote wakeup\n");
1665 static int dwc3_gadget_wakeup(struct usb_gadget
*g
)
1667 struct dwc3
*dwc
= gadget_to_dwc(g
);
1668 unsigned long flags
;
1671 spin_lock_irqsave(&dwc
->lock
, flags
);
1672 ret
= __dwc3_gadget_wakeup(dwc
);
1673 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1678 static int dwc3_gadget_set_selfpowered(struct usb_gadget
*g
,
1681 struct dwc3
*dwc
= gadget_to_dwc(g
);
1682 unsigned long flags
;
1684 spin_lock_irqsave(&dwc
->lock
, flags
);
1685 g
->is_selfpowered
= !!is_selfpowered
;
1686 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1691 static int dwc3_gadget_run_stop(struct dwc3
*dwc
, int is_on
, int suspend
)
1696 if (pm_runtime_suspended(dwc
->dev
))
1699 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1701 if (dwc
->revision
<= DWC3_REVISION_187A
) {
1702 reg
&= ~DWC3_DCTL_TRGTULST_MASK
;
1703 reg
|= DWC3_DCTL_TRGTULST_RX_DET
;
1706 if (dwc
->revision
>= DWC3_REVISION_194A
)
1707 reg
&= ~DWC3_DCTL_KEEP_CONNECT
;
1708 reg
|= DWC3_DCTL_RUN_STOP
;
1710 if (dwc
->has_hibernation
)
1711 reg
|= DWC3_DCTL_KEEP_CONNECT
;
1713 dwc
->pullups_connected
= true;
1715 reg
&= ~DWC3_DCTL_RUN_STOP
;
1717 if (dwc
->has_hibernation
&& !suspend
)
1718 reg
&= ~DWC3_DCTL_KEEP_CONNECT
;
1720 dwc
->pullups_connected
= false;
1723 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1726 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1727 reg
&= DWC3_DSTS_DEVCTRLHLT
;
1728 } while (--timeout
&& !(!is_on
^ !reg
));
1736 static int dwc3_gadget_pullup(struct usb_gadget
*g
, int is_on
)
1738 struct dwc3
*dwc
= gadget_to_dwc(g
);
1739 unsigned long flags
;
1745 * Per databook, when we want to stop the gadget, if a control transfer
1746 * is still in process, complete it and get the core into setup phase.
1748 if (!is_on
&& dwc
->ep0state
!= EP0_SETUP_PHASE
) {
1749 reinit_completion(&dwc
->ep0_in_setup
);
1751 ret
= wait_for_completion_timeout(&dwc
->ep0_in_setup
,
1752 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT
));
1754 dev_err(dwc
->dev
, "timed out waiting for SETUP phase\n");
1759 spin_lock_irqsave(&dwc
->lock
, flags
);
1760 ret
= dwc3_gadget_run_stop(dwc
, is_on
, false);
1761 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1766 static void dwc3_gadget_enable_irq(struct dwc3
*dwc
)
1770 /* Enable all but Start and End of Frame IRQs */
1771 reg
= (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN
|
1772 DWC3_DEVTEN_EVNTOVERFLOWEN
|
1773 DWC3_DEVTEN_CMDCMPLTEN
|
1774 DWC3_DEVTEN_ERRTICERREN
|
1775 DWC3_DEVTEN_WKUPEVTEN
|
1776 DWC3_DEVTEN_CONNECTDONEEN
|
1777 DWC3_DEVTEN_USBRSTEN
|
1778 DWC3_DEVTEN_DISCONNEVTEN
);
1780 if (dwc
->revision
< DWC3_REVISION_250A
)
1781 reg
|= DWC3_DEVTEN_ULSTCNGEN
;
1783 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, reg
);
1786 static void dwc3_gadget_disable_irq(struct dwc3
*dwc
)
1788 /* mask all interrupts */
1789 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
1792 static irqreturn_t
dwc3_interrupt(int irq
, void *_dwc
);
1793 static irqreturn_t
dwc3_thread_interrupt(int irq
, void *_dwc
);
1796 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1797 * @dwc: pointer to our context structure
1799 * The following looks like complex but it's actually very simple. In order to
1800 * calculate the number of packets we can burst at once on OUT transfers, we're
1801 * gonna use RxFIFO size.
1803 * To calculate RxFIFO size we need two numbers:
1804 * MDWIDTH = size, in bits, of the internal memory bus
1805 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1807 * Given these two numbers, the formula is simple:
1809 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1811 * 24 bytes is for 3x SETUP packets
1812 * 16 bytes is a clock domain crossing tolerance
1814 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1816 static void dwc3_gadget_setup_nump(struct dwc3
*dwc
)
1823 ram2_depth
= DWC3_GHWPARAMS7_RAM2_DEPTH(dwc
->hwparams
.hwparams7
);
1824 mdwidth
= DWC3_GHWPARAMS0_MDWIDTH(dwc
->hwparams
.hwparams0
);
1826 nump
= ((ram2_depth
* mdwidth
/ 8) - 24 - 16) / 1024;
1827 nump
= min_t(u32
, nump
, 16);
1830 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1831 reg
&= ~DWC3_DCFG_NUMP_MASK
;
1832 reg
|= nump
<< DWC3_DCFG_NUMP_SHIFT
;
1833 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1836 static int __dwc3_gadget_start(struct dwc3
*dwc
)
1838 struct dwc3_ep
*dep
;
1843 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1844 * the core supports IMOD, disable it.
1846 if (dwc
->imod_interval
) {
1847 dwc3_writel(dwc
->regs
, DWC3_DEV_IMOD(0), dwc
->imod_interval
);
1848 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB
);
1849 } else if (dwc3_has_imod(dwc
)) {
1850 dwc3_writel(dwc
->regs
, DWC3_DEV_IMOD(0), 0);
1854 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1855 * field instead of letting dwc3 itself calculate that automatically.
1857 * This way, we maximize the chances that we'll be able to get several
1858 * bursts of data without going through any sort of endpoint throttling.
1860 reg
= dwc3_readl(dwc
->regs
, DWC3_GRXTHRCFG
);
1861 reg
&= ~DWC3_GRXTHRCFG_PKTCNTSEL
;
1862 dwc3_writel(dwc
->regs
, DWC3_GRXTHRCFG
, reg
);
1864 dwc3_gadget_setup_nump(dwc
);
1866 /* Start with SuperSpeed Default */
1867 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1870 ret
= __dwc3_gadget_ep_enable(dep
, false, false);
1872 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1877 ret
= __dwc3_gadget_ep_enable(dep
, false, false);
1879 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1883 /* begin to receive SETUP packets */
1884 dwc
->ep0state
= EP0_SETUP_PHASE
;
1885 dwc3_ep0_out_start(dwc
);
1887 dwc3_gadget_enable_irq(dwc
);
1892 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1898 static int dwc3_gadget_start(struct usb_gadget
*g
,
1899 struct usb_gadget_driver
*driver
)
1901 struct dwc3
*dwc
= gadget_to_dwc(g
);
1902 unsigned long flags
;
1906 irq
= dwc
->irq_gadget
;
1907 ret
= request_threaded_irq(irq
, dwc3_interrupt
, dwc3_thread_interrupt
,
1908 IRQF_SHARED
, "dwc3", dwc
->ev_buf
);
1910 dev_err(dwc
->dev
, "failed to request irq #%d --> %d\n",
1915 spin_lock_irqsave(&dwc
->lock
, flags
);
1916 if (dwc
->gadget_driver
) {
1917 dev_err(dwc
->dev
, "%s is already bound to %s\n",
1919 dwc
->gadget_driver
->driver
.name
);
1924 dwc
->gadget_driver
= driver
;
1926 if (pm_runtime_active(dwc
->dev
))
1927 __dwc3_gadget_start(dwc
);
1929 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1934 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1941 static void __dwc3_gadget_stop(struct dwc3
*dwc
)
1943 dwc3_gadget_disable_irq(dwc
);
1944 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1945 __dwc3_gadget_ep_disable(dwc
->eps
[1]);
1948 static int dwc3_gadget_stop(struct usb_gadget
*g
)
1950 struct dwc3
*dwc
= gadget_to_dwc(g
);
1951 unsigned long flags
;
1954 spin_lock_irqsave(&dwc
->lock
, flags
);
1956 if (pm_runtime_suspended(dwc
->dev
))
1959 __dwc3_gadget_stop(dwc
);
1961 for (epnum
= 2; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1962 struct dwc3_ep
*dep
= dwc
->eps
[epnum
];
1967 if (!(dep
->flags
& DWC3_EP_END_TRANSFER_PENDING
))
1970 wait_event_lock_irq(dep
->wait_end_transfer
,
1971 !(dep
->flags
& DWC3_EP_END_TRANSFER_PENDING
),
1976 dwc
->gadget_driver
= NULL
;
1977 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1979 free_irq(dwc
->irq_gadget
, dwc
->ev_buf
);
1984 static void dwc3_gadget_set_speed(struct usb_gadget
*g
,
1985 enum usb_device_speed speed
)
1987 struct dwc3
*dwc
= gadget_to_dwc(g
);
1988 unsigned long flags
;
1991 spin_lock_irqsave(&dwc
->lock
, flags
);
1992 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1993 reg
&= ~(DWC3_DCFG_SPEED_MASK
);
1996 * WORKAROUND: DWC3 revision < 2.20a have an issue
1997 * which would cause metastability state on Run/Stop
1998 * bit if we try to force the IP to USB2-only mode.
2000 * Because of that, we cannot configure the IP to any
2001 * speed other than the SuperSpeed
2005 * STAR#9000525659: Clock Domain Crossing on DCTL in
2008 if (dwc
->revision
< DWC3_REVISION_220A
&&
2009 !dwc
->dis_metastability_quirk
) {
2010 reg
|= DWC3_DCFG_SUPERSPEED
;
2014 reg
|= DWC3_DCFG_LOWSPEED
;
2016 case USB_SPEED_FULL
:
2017 reg
|= DWC3_DCFG_FULLSPEED
;
2019 case USB_SPEED_HIGH
:
2020 reg
|= DWC3_DCFG_HIGHSPEED
;
2022 case USB_SPEED_SUPER
:
2023 reg
|= DWC3_DCFG_SUPERSPEED
;
2025 case USB_SPEED_SUPER_PLUS
:
2026 reg
|= DWC3_DCFG_SUPERSPEED_PLUS
;
2029 dev_err(dwc
->dev
, "invalid speed (%d)\n", speed
);
2031 if (dwc
->revision
& DWC3_REVISION_IS_DWC31
)
2032 reg
|= DWC3_DCFG_SUPERSPEED_PLUS
;
2034 reg
|= DWC3_DCFG_SUPERSPEED
;
2037 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2039 spin_unlock_irqrestore(&dwc
->lock
, flags
);
2042 static const struct usb_gadget_ops dwc3_gadget_ops
= {
2043 .get_frame
= dwc3_gadget_get_frame
,
2044 .wakeup
= dwc3_gadget_wakeup
,
2045 .set_selfpowered
= dwc3_gadget_set_selfpowered
,
2046 .pullup
= dwc3_gadget_pullup
,
2047 .udc_start
= dwc3_gadget_start
,
2048 .udc_stop
= dwc3_gadget_stop
,
2049 .udc_set_speed
= dwc3_gadget_set_speed
,
2052 /* -------------------------------------------------------------------------- */
2054 static int dwc3_gadget_init_endpoints(struct dwc3
*dwc
, u8 total
)
2056 struct dwc3_ep
*dep
;
2059 INIT_LIST_HEAD(&dwc
->gadget
.ep_list
);
2061 for (epnum
= 0; epnum
< total
; epnum
++) {
2062 bool direction
= epnum
& 1;
2063 u8 num
= epnum
>> 1;
2065 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
2070 dep
->number
= epnum
;
2071 dep
->direction
= direction
;
2072 dep
->regs
= dwc
->regs
+ DWC3_DEP_BASE(epnum
);
2073 dwc
->eps
[epnum
] = dep
;
2075 snprintf(dep
->name
, sizeof(dep
->name
), "ep%u%s", num
,
2076 direction
? "in" : "out");
2078 dep
->endpoint
.name
= dep
->name
;
2080 if (!(dep
->number
> 1)) {
2081 dep
->endpoint
.desc
= &dwc3_gadget_ep0_desc
;
2082 dep
->endpoint
.comp_desc
= NULL
;
2085 spin_lock_init(&dep
->lock
);
2088 usb_ep_set_maxpacket_limit(&dep
->endpoint
, 512);
2089 dep
->endpoint
.maxburst
= 1;
2090 dep
->endpoint
.ops
= &dwc3_gadget_ep0_ops
;
2092 dwc
->gadget
.ep0
= &dep
->endpoint
;
2093 } else if (direction
) {
2099 mdwidth
= DWC3_MDWIDTH(dwc
->hwparams
.hwparams0
);
2100 /* MDWIDTH is represented in bits, we need it in bytes */
2103 size
= dwc3_readl(dwc
->regs
, DWC3_GTXFIFOSIZ(num
));
2104 size
= DWC3_GTXFIFOSIZ_TXFDEF(size
);
2106 /* FIFO Depth is in MDWDITH bytes. Multiply */
2109 kbytes
= size
/ 1024;
2114 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2115 * internal overhead. We don't really know how these are used,
2116 * but documentation say it exists.
2118 size
-= mdwidth
* (kbytes
+ 1);
2121 usb_ep_set_maxpacket_limit(&dep
->endpoint
, size
);
2123 dep
->endpoint
.max_streams
= 15;
2124 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
2125 list_add_tail(&dep
->endpoint
.ep_list
,
2126 &dwc
->gadget
.ep_list
);
2128 ret
= dwc3_alloc_trb_pool(dep
);
2134 usb_ep_set_maxpacket_limit(&dep
->endpoint
, 1024);
2135 dep
->endpoint
.max_streams
= 15;
2136 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
2137 list_add_tail(&dep
->endpoint
.ep_list
,
2138 &dwc
->gadget
.ep_list
);
2140 ret
= dwc3_alloc_trb_pool(dep
);
2146 dep
->endpoint
.caps
.type_control
= true;
2148 dep
->endpoint
.caps
.type_iso
= true;
2149 dep
->endpoint
.caps
.type_bulk
= true;
2150 dep
->endpoint
.caps
.type_int
= true;
2153 dep
->endpoint
.caps
.dir_in
= direction
;
2154 dep
->endpoint
.caps
.dir_out
= !direction
;
2156 INIT_LIST_HEAD(&dep
->pending_list
);
2157 INIT_LIST_HEAD(&dep
->started_list
);
2163 static void dwc3_gadget_free_endpoints(struct dwc3
*dwc
)
2165 struct dwc3_ep
*dep
;
2168 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
2169 dep
= dwc
->eps
[epnum
];
2173 * Physical endpoints 0 and 1 are special; they form the
2174 * bi-directional USB endpoint 0.
2176 * For those two physical endpoints, we don't allocate a TRB
2177 * pool nor do we add them the endpoints list. Due to that, we
2178 * shouldn't do these two operations otherwise we would end up
2179 * with all sorts of bugs when removing dwc3.ko.
2181 if (epnum
!= 0 && epnum
!= 1) {
2182 dwc3_free_trb_pool(dep
);
2183 list_del(&dep
->endpoint
.ep_list
);
2190 /* -------------------------------------------------------------------------- */
2192 static int __dwc3_cleanup_done_trbs(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
2193 struct dwc3_request
*req
, struct dwc3_trb
*trb
,
2194 const struct dwc3_event_depevt
*event
, int status
,
2198 unsigned int s_pkt
= 0;
2199 unsigned int trb_status
;
2201 dwc3_ep_inc_deq(dep
);
2203 if (req
->trb
== trb
)
2204 dep
->queued_requests
--;
2206 trace_dwc3_complete_trb(dep
, trb
);
2209 * If we're in the middle of series of chained TRBs and we
2210 * receive a short transfer along the way, DWC3 will skip
2211 * through all TRBs including the last TRB in the chain (the
2212 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2213 * bit and SW has to do it manually.
2215 * We're going to do that here to avoid problems of HW trying
2216 * to use bogus TRBs for transfers.
2218 if (chain
&& (trb
->ctrl
& DWC3_TRB_CTRL_HWO
))
2219 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
2222 * If we're dealing with unaligned size OUT transfer, we will be left
2223 * with one TRB pending in the ring. We need to manually clear HWO bit
2226 if ((req
->zero
|| req
->unaligned
) && (trb
->ctrl
& DWC3_TRB_CTRL_HWO
)) {
2227 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
2231 count
= trb
->size
& DWC3_TRB_SIZE_MASK
;
2232 req
->remaining
+= count
;
2234 if ((trb
->ctrl
& DWC3_TRB_CTRL_HWO
) && status
!= -ESHUTDOWN
)
2237 if (dep
->direction
) {
2239 trb_status
= DWC3_TRB_SIZE_TRBSTS(trb
->size
);
2240 if (trb_status
== DWC3_TRBSTS_MISSED_ISOC
) {
2242 * If missed isoc occurred and there is
2243 * no request queued then issue END
2244 * TRANSFER, so that core generates
2245 * next xfernotready and we will issue
2246 * a fresh START TRANSFER.
2247 * If there are still queued request
2248 * then wait, do not issue either END
2249 * or UPDATE TRANSFER, just attach next
2250 * request in pending_list during
2251 * giveback.If any future queued request
2252 * is successfully transferred then we
2253 * will issue UPDATE TRANSFER for all
2254 * request in the pending_list.
2256 dep
->flags
|= DWC3_EP_MISSED_ISOC
;
2258 dev_err(dwc
->dev
, "incomplete IN transfer %s\n",
2260 status
= -ECONNRESET
;
2263 dep
->flags
&= ~DWC3_EP_MISSED_ISOC
;
2266 if (count
&& (event
->status
& DEPEVT_STATUS_SHORT
))
2270 if (s_pkt
&& !chain
)
2273 if ((event
->status
& DEPEVT_STATUS_IOC
) &&
2274 (trb
->ctrl
& DWC3_TRB_CTRL_IOC
))
2280 static int dwc3_cleanup_done_reqs(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
2281 const struct dwc3_event_depevt
*event
, int status
)
2283 struct dwc3_request
*req
, *n
;
2284 struct dwc3_trb
*trb
;
2288 list_for_each_entry_safe(req
, n
, &dep
->started_list
, list
) {
2292 length
= req
->request
.length
;
2293 chain
= req
->num_pending_sgs
> 0;
2295 struct scatterlist
*sg
= req
->sg
;
2296 struct scatterlist
*s
;
2297 unsigned int pending
= req
->num_pending_sgs
;
2300 for_each_sg(sg
, s
, pending
, i
) {
2301 trb
= &dep
->trb_pool
[dep
->trb_dequeue
];
2303 if (trb
->ctrl
& DWC3_TRB_CTRL_HWO
)
2306 req
->sg
= sg_next(s
);
2307 req
->num_pending_sgs
--;
2309 ret
= __dwc3_cleanup_done_trbs(dwc
, dep
, req
, trb
,
2310 event
, status
, chain
);
2315 trb
= &dep
->trb_pool
[dep
->trb_dequeue
];
2316 ret
= __dwc3_cleanup_done_trbs(dwc
, dep
, req
, trb
,
2317 event
, status
, chain
);
2320 if (req
->unaligned
|| req
->zero
) {
2321 trb
= &dep
->trb_pool
[dep
->trb_dequeue
];
2322 ret
= __dwc3_cleanup_done_trbs(dwc
, dep
, req
, trb
,
2323 event
, status
, false);
2324 req
->unaligned
= false;
2328 req
->request
.actual
= length
- req
->remaining
;
2330 if ((req
->request
.actual
< length
) && req
->num_pending_sgs
)
2331 return __dwc3_gadget_kick_transfer(dep
);
2333 dwc3_gadget_giveback(dep
, req
, status
);
2336 if ((event
->status
& DEPEVT_STATUS_IOC
) &&
2337 (trb
->ctrl
& DWC3_TRB_CTRL_IOC
))
2344 * Our endpoint might get disabled by another thread during
2345 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2346 * early on so DWC3_EP_BUSY flag gets cleared
2348 if (!dep
->endpoint
.desc
)
2351 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
2352 list_empty(&dep
->started_list
)) {
2353 if (list_empty(&dep
->pending_list
)) {
2355 * If there is no entry in request list then do
2356 * not issue END TRANSFER now. Just set PENDING
2357 * flag, so that END TRANSFER is issued when an
2358 * entry is added into request list.
2360 dep
->flags
= DWC3_EP_PENDING_REQUEST
;
2362 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
2363 dep
->flags
= DWC3_EP_ENABLED
;
2368 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) && ioc
)
2374 static void dwc3_endpoint_transfer_complete(struct dwc3
*dwc
,
2375 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
)
2377 unsigned status
= 0;
2379 u32 is_xfer_complete
;
2381 is_xfer_complete
= (event
->endpoint_event
== DWC3_DEPEVT_XFERCOMPLETE
);
2383 if (event
->status
& DEPEVT_STATUS_BUSERR
)
2384 status
= -ECONNRESET
;
2386 clean_busy
= dwc3_cleanup_done_reqs(dwc
, dep
, event
, status
);
2387 if (clean_busy
&& (!dep
->endpoint
.desc
|| is_xfer_complete
||
2388 usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)))
2389 dep
->flags
&= ~DWC3_EP_BUSY
;
2392 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2393 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2395 if (dwc
->revision
< DWC3_REVISION_183A
) {
2399 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
2402 if (!(dep
->flags
& DWC3_EP_ENABLED
))
2405 if (!list_empty(&dep
->started_list
))
2409 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2411 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2417 * Our endpoint might get disabled by another thread during
2418 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2419 * early on so DWC3_EP_BUSY flag gets cleared
2421 if (!dep
->endpoint
.desc
)
2424 if (!usb_endpoint_xfer_isoc(dep
->endpoint
.desc
))
2425 __dwc3_gadget_kick_transfer(dep
);
2428 static void dwc3_endpoint_interrupt(struct dwc3
*dwc
,
2429 const struct dwc3_event_depevt
*event
)
2431 struct dwc3_ep
*dep
;
2432 u8 epnum
= event
->endpoint_number
;
2435 dep
= dwc
->eps
[epnum
];
2437 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
2438 if (!(dep
->flags
& DWC3_EP_END_TRANSFER_PENDING
))
2441 /* Handle only EPCMDCMPLT when EP disabled */
2442 if (event
->endpoint_event
!= DWC3_DEPEVT_EPCMDCMPLT
)
2446 if (epnum
== 0 || epnum
== 1) {
2447 dwc3_ep0_interrupt(dwc
, event
);
2451 switch (event
->endpoint_event
) {
2452 case DWC3_DEPEVT_XFERCOMPLETE
:
2453 dep
->resource_index
= 0;
2455 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
2456 dev_err(dwc
->dev
, "XferComplete for Isochronous endpoint\n");
2460 dwc3_endpoint_transfer_complete(dwc
, dep
, event
);
2462 case DWC3_DEPEVT_XFERINPROGRESS
:
2463 dwc3_endpoint_transfer_complete(dwc
, dep
, event
);
2465 case DWC3_DEPEVT_XFERNOTREADY
:
2466 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
))
2467 dwc3_gadget_start_isoc(dwc
, dep
, event
);
2469 __dwc3_gadget_kick_transfer(dep
);
2472 case DWC3_DEPEVT_STREAMEVT
:
2473 if (!usb_endpoint_xfer_bulk(dep
->endpoint
.desc
)) {
2474 dev_err(dwc
->dev
, "Stream event for non-Bulk %s\n",
2479 case DWC3_DEPEVT_EPCMDCMPLT
:
2480 cmd
= DEPEVT_PARAMETER_CMD(event
->parameters
);
2482 if (cmd
== DWC3_DEPCMD_ENDTRANSFER
) {
2483 dep
->flags
&= ~DWC3_EP_END_TRANSFER_PENDING
;
2484 wake_up(&dep
->wait_end_transfer
);
2487 case DWC3_DEPEVT_RXTXFIFOEVT
:
2492 static void dwc3_disconnect_gadget(struct dwc3
*dwc
)
2494 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->disconnect
) {
2495 spin_unlock(&dwc
->lock
);
2496 dwc
->gadget_driver
->disconnect(&dwc
->gadget
);
2497 spin_lock(&dwc
->lock
);
2501 static void dwc3_suspend_gadget(struct dwc3
*dwc
)
2503 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->suspend
) {
2504 spin_unlock(&dwc
->lock
);
2505 dwc
->gadget_driver
->suspend(&dwc
->gadget
);
2506 spin_lock(&dwc
->lock
);
2510 static void dwc3_resume_gadget(struct dwc3
*dwc
)
2512 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->resume
) {
2513 spin_unlock(&dwc
->lock
);
2514 dwc
->gadget_driver
->resume(&dwc
->gadget
);
2515 spin_lock(&dwc
->lock
);
2519 static void dwc3_reset_gadget(struct dwc3
*dwc
)
2521 if (!dwc
->gadget_driver
)
2524 if (dwc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
2525 spin_unlock(&dwc
->lock
);
2526 usb_gadget_udc_reset(&dwc
->gadget
, dwc
->gadget_driver
);
2527 spin_lock(&dwc
->lock
);
2531 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
, bool force
)
2533 struct dwc3_ep
*dep
;
2534 struct dwc3_gadget_ep_cmd_params params
;
2538 dep
= dwc
->eps
[epnum
];
2540 if ((dep
->flags
& DWC3_EP_END_TRANSFER_PENDING
) ||
2541 !dep
->resource_index
)
2545 * NOTICE: We are violating what the Databook says about the
2546 * EndTransfer command. Ideally we would _always_ wait for the
2547 * EndTransfer Command Completion IRQ, but that's causing too
2548 * much trouble synchronizing between us and gadget driver.
2550 * We have discussed this with the IP Provider and it was
2551 * suggested to giveback all requests here, but give HW some
2552 * extra time to synchronize with the interconnect. We're using
2553 * an arbitrary 100us delay for that.
2555 * Note also that a similar handling was tested by Synopsys
2556 * (thanks a lot Paul) and nothing bad has come out of it.
2557 * In short, what we're doing is:
2559 * - Issue EndTransfer WITH CMDIOC bit set
2562 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2563 * supports a mode to work around the above limitation. The
2564 * software can poll the CMDACT bit in the DEPCMD register
2565 * after issuing a EndTransfer command. This mode is enabled
2566 * by writing GUCTL2[14]. This polling is already done in the
2567 * dwc3_send_gadget_ep_cmd() function so if the mode is
2568 * enabled, the EndTransfer command will have completed upon
2569 * returning from this function and we don't need to delay for
2572 * This mode is NOT available on the DWC_usb31 IP.
2575 cmd
= DWC3_DEPCMD_ENDTRANSFER
;
2576 cmd
|= force
? DWC3_DEPCMD_HIPRI_FORCERM
: 0;
2577 cmd
|= DWC3_DEPCMD_CMDIOC
;
2578 cmd
|= DWC3_DEPCMD_PARAM(dep
->resource_index
);
2579 memset(¶ms
, 0, sizeof(params
));
2580 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
2582 dep
->resource_index
= 0;
2583 dep
->flags
&= ~DWC3_EP_BUSY
;
2585 if (dwc3_is_usb31(dwc
) || dwc
->revision
< DWC3_REVISION_310A
) {
2586 dep
->flags
|= DWC3_EP_END_TRANSFER_PENDING
;
2591 static void dwc3_clear_stall_all_ep(struct dwc3
*dwc
)
2595 for (epnum
= 1; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
2596 struct dwc3_ep
*dep
;
2599 dep
= dwc
->eps
[epnum
];
2603 if (!(dep
->flags
& DWC3_EP_STALL
))
2606 dep
->flags
&= ~DWC3_EP_STALL
;
2608 ret
= dwc3_send_clear_stall_ep_cmd(dep
);
2613 static void dwc3_gadget_disconnect_interrupt(struct dwc3
*dwc
)
2617 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2618 reg
&= ~DWC3_DCTL_INITU1ENA
;
2619 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2621 reg
&= ~DWC3_DCTL_INITU2ENA
;
2622 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2624 dwc3_disconnect_gadget(dwc
);
2626 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2627 dwc
->setup_packet_pending
= false;
2628 usb_gadget_set_state(&dwc
->gadget
, USB_STATE_NOTATTACHED
);
2630 dwc
->connected
= false;
2633 static void dwc3_gadget_reset_interrupt(struct dwc3
*dwc
)
2637 dwc
->connected
= true;
2640 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2641 * would cause a missing Disconnect Event if there's a
2642 * pending Setup Packet in the FIFO.
2644 * There's no suggested workaround on the official Bug
2645 * report, which states that "unless the driver/application
2646 * is doing any special handling of a disconnect event,
2647 * there is no functional issue".
2649 * Unfortunately, it turns out that we _do_ some special
2650 * handling of a disconnect event, namely complete all
2651 * pending transfers, notify gadget driver of the
2652 * disconnection, and so on.
2654 * Our suggested workaround is to follow the Disconnect
2655 * Event steps here, instead, based on a setup_packet_pending
2656 * flag. Such flag gets set whenever we have a SETUP_PENDING
2657 * status for EP0 TRBs and gets cleared on XferComplete for the
2662 * STAR#9000466709: RTL: Device : Disconnect event not
2663 * generated if setup packet pending in FIFO
2665 if (dwc
->revision
< DWC3_REVISION_188A
) {
2666 if (dwc
->setup_packet_pending
)
2667 dwc3_gadget_disconnect_interrupt(dwc
);
2670 dwc3_reset_gadget(dwc
);
2672 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2673 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
2674 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2675 dwc
->test_mode
= false;
2676 dwc3_clear_stall_all_ep(dwc
);
2678 /* Reset device address to zero */
2679 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2680 reg
&= ~(DWC3_DCFG_DEVADDR_MASK
);
2681 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2684 static void dwc3_gadget_conndone_interrupt(struct dwc3
*dwc
)
2686 struct dwc3_ep
*dep
;
2691 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
2692 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
2696 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2697 * each time on Connect Done.
2699 * Currently we always use the reset value. If any platform
2700 * wants to set this to a different value, we need to add a
2701 * setting and update GCTL.RAMCLKSEL here.
2705 case DWC3_DSTS_SUPERSPEED_PLUS
:
2706 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
2707 dwc
->gadget
.ep0
->maxpacket
= 512;
2708 dwc
->gadget
.speed
= USB_SPEED_SUPER_PLUS
;
2710 case DWC3_DSTS_SUPERSPEED
:
2712 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2713 * would cause a missing USB3 Reset event.
2715 * In such situations, we should force a USB3 Reset
2716 * event by calling our dwc3_gadget_reset_interrupt()
2721 * STAR#9000483510: RTL: SS : USB3 reset event may
2722 * not be generated always when the link enters poll
2724 if (dwc
->revision
< DWC3_REVISION_190A
)
2725 dwc3_gadget_reset_interrupt(dwc
);
2727 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
2728 dwc
->gadget
.ep0
->maxpacket
= 512;
2729 dwc
->gadget
.speed
= USB_SPEED_SUPER
;
2731 case DWC3_DSTS_HIGHSPEED
:
2732 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
2733 dwc
->gadget
.ep0
->maxpacket
= 64;
2734 dwc
->gadget
.speed
= USB_SPEED_HIGH
;
2736 case DWC3_DSTS_FULLSPEED
:
2737 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
2738 dwc
->gadget
.ep0
->maxpacket
= 64;
2739 dwc
->gadget
.speed
= USB_SPEED_FULL
;
2741 case DWC3_DSTS_LOWSPEED
:
2742 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(8);
2743 dwc
->gadget
.ep0
->maxpacket
= 8;
2744 dwc
->gadget
.speed
= USB_SPEED_LOW
;
2748 /* Enable USB2 LPM Capability */
2750 if ((dwc
->revision
> DWC3_REVISION_194A
) &&
2751 (speed
!= DWC3_DSTS_SUPERSPEED
) &&
2752 (speed
!= DWC3_DSTS_SUPERSPEED_PLUS
)) {
2753 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2754 reg
|= DWC3_DCFG_LPM_CAP
;
2755 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2757 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2758 reg
&= ~(DWC3_DCTL_HIRD_THRES_MASK
| DWC3_DCTL_L1_HIBER_EN
);
2760 reg
|= DWC3_DCTL_HIRD_THRES(dwc
->hird_threshold
);
2763 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2764 * DCFG.LPMCap is set, core responses with an ACK and the
2765 * BESL value in the LPM token is less than or equal to LPM
2768 WARN_ONCE(dwc
->revision
< DWC3_REVISION_240A
2769 && dwc
->has_lpm_erratum
,
2770 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2772 if (dwc
->has_lpm_erratum
&& dwc
->revision
>= DWC3_REVISION_240A
)
2773 reg
|= DWC3_DCTL_LPM_ERRATA(dwc
->lpm_nyet_threshold
);
2775 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2777 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2778 reg
&= ~DWC3_DCTL_HIRD_THRES_MASK
;
2779 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2783 ret
= __dwc3_gadget_ep_enable(dep
, true, false);
2785 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
2790 ret
= __dwc3_gadget_ep_enable(dep
, true, false);
2792 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
2797 * Configure PHY via GUSB3PIPECTLn if required.
2799 * Update GTXFIFOSIZn
2801 * In both cases reset values should be sufficient.
2805 static void dwc3_gadget_wakeup_interrupt(struct dwc3
*dwc
)
2808 * TODO take core out of low power mode when that's
2812 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->resume
) {
2813 spin_unlock(&dwc
->lock
);
2814 dwc
->gadget_driver
->resume(&dwc
->gadget
);
2815 spin_lock(&dwc
->lock
);
2819 static void dwc3_gadget_linksts_change_interrupt(struct dwc3
*dwc
,
2820 unsigned int evtinfo
)
2822 enum dwc3_link_state next
= evtinfo
& DWC3_LINK_STATE_MASK
;
2823 unsigned int pwropt
;
2826 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2827 * Hibernation mode enabled which would show up when device detects
2828 * host-initiated U3 exit.
2830 * In that case, device will generate a Link State Change Interrupt
2831 * from U3 to RESUME which is only necessary if Hibernation is
2834 * There are no functional changes due to such spurious event and we
2835 * just need to ignore it.
2839 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2842 pwropt
= DWC3_GHWPARAMS1_EN_PWROPT(dwc
->hwparams
.hwparams1
);
2843 if ((dwc
->revision
< DWC3_REVISION_250A
) &&
2844 (pwropt
!= DWC3_GHWPARAMS1_EN_PWROPT_HIB
)) {
2845 if ((dwc
->link_state
== DWC3_LINK_STATE_U3
) &&
2846 (next
== DWC3_LINK_STATE_RESUME
)) {
2852 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2853 * on the link partner, the USB session might do multiple entry/exit
2854 * of low power states before a transfer takes place.
2856 * Due to this problem, we might experience lower throughput. The
2857 * suggested workaround is to disable DCTL[12:9] bits if we're
2858 * transitioning from U1/U2 to U0 and enable those bits again
2859 * after a transfer completes and there are no pending transfers
2860 * on any of the enabled endpoints.
2862 * This is the first half of that workaround.
2866 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2867 * core send LGO_Ux entering U0
2869 if (dwc
->revision
< DWC3_REVISION_183A
) {
2870 if (next
== DWC3_LINK_STATE_U0
) {
2874 switch (dwc
->link_state
) {
2875 case DWC3_LINK_STATE_U1
:
2876 case DWC3_LINK_STATE_U2
:
2877 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2878 u1u2
= reg
& (DWC3_DCTL_INITU2ENA
2879 | DWC3_DCTL_ACCEPTU2ENA
2880 | DWC3_DCTL_INITU1ENA
2881 | DWC3_DCTL_ACCEPTU1ENA
);
2884 dwc
->u1u2
= reg
& u1u2
;
2888 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2898 case DWC3_LINK_STATE_U1
:
2899 if (dwc
->speed
== USB_SPEED_SUPER
)
2900 dwc3_suspend_gadget(dwc
);
2902 case DWC3_LINK_STATE_U2
:
2903 case DWC3_LINK_STATE_U3
:
2904 dwc3_suspend_gadget(dwc
);
2906 case DWC3_LINK_STATE_RESUME
:
2907 dwc3_resume_gadget(dwc
);
2914 dwc
->link_state
= next
;
2917 static void dwc3_gadget_suspend_interrupt(struct dwc3
*dwc
,
2918 unsigned int evtinfo
)
2920 enum dwc3_link_state next
= evtinfo
& DWC3_LINK_STATE_MASK
;
2922 if (dwc
->link_state
!= next
&& next
== DWC3_LINK_STATE_U3
)
2923 dwc3_suspend_gadget(dwc
);
2925 dwc
->link_state
= next
;
2928 static void dwc3_gadget_hibernation_interrupt(struct dwc3
*dwc
,
2929 unsigned int evtinfo
)
2931 unsigned int is_ss
= evtinfo
& BIT(4);
2934 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2935 * have a known issue which can cause USB CV TD.9.23 to fail
2938 * Because of this issue, core could generate bogus hibernation
2939 * events which SW needs to ignore.
2943 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2944 * Device Fallback from SuperSpeed
2946 if (is_ss
^ (dwc
->speed
== USB_SPEED_SUPER
))
2949 /* enter hibernation here */
2952 static void dwc3_gadget_interrupt(struct dwc3
*dwc
,
2953 const struct dwc3_event_devt
*event
)
2955 switch (event
->type
) {
2956 case DWC3_DEVICE_EVENT_DISCONNECT
:
2957 dwc3_gadget_disconnect_interrupt(dwc
);
2959 case DWC3_DEVICE_EVENT_RESET
:
2960 dwc3_gadget_reset_interrupt(dwc
);
2962 case DWC3_DEVICE_EVENT_CONNECT_DONE
:
2963 dwc3_gadget_conndone_interrupt(dwc
);
2965 case DWC3_DEVICE_EVENT_WAKEUP
:
2966 dwc3_gadget_wakeup_interrupt(dwc
);
2968 case DWC3_DEVICE_EVENT_HIBER_REQ
:
2969 if (dev_WARN_ONCE(dwc
->dev
, !dwc
->has_hibernation
,
2970 "unexpected hibernation event\n"))
2973 dwc3_gadget_hibernation_interrupt(dwc
, event
->event_info
);
2975 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE
:
2976 dwc3_gadget_linksts_change_interrupt(dwc
, event
->event_info
);
2978 case DWC3_DEVICE_EVENT_EOPF
:
2979 /* It changed to be suspend event for version 2.30a and above */
2980 if (dwc
->revision
>= DWC3_REVISION_230A
) {
2982 * Ignore suspend event until the gadget enters into
2983 * USB_STATE_CONFIGURED state.
2985 if (dwc
->gadget
.state
>= USB_STATE_CONFIGURED
)
2986 dwc3_gadget_suspend_interrupt(dwc
,
2990 case DWC3_DEVICE_EVENT_SOF
:
2991 case DWC3_DEVICE_EVENT_ERRATIC_ERROR
:
2992 case DWC3_DEVICE_EVENT_CMD_CMPL
:
2993 case DWC3_DEVICE_EVENT_OVERFLOW
:
2996 dev_WARN(dwc
->dev
, "UNKNOWN IRQ %d\n", event
->type
);
3000 static void dwc3_process_event_entry(struct dwc3
*dwc
,
3001 const union dwc3_event
*event
)
3003 trace_dwc3_event(event
->raw
, dwc
);
3005 if (!event
->type
.is_devspec
)
3006 dwc3_endpoint_interrupt(dwc
, &event
->depevt
);
3007 else if (event
->type
.type
== DWC3_EVENT_TYPE_DEV
)
3008 dwc3_gadget_interrupt(dwc
, &event
->devt
);
3010 dev_err(dwc
->dev
, "UNKNOWN IRQ type %d\n", event
->raw
);
3013 static irqreturn_t
dwc3_process_event_buf(struct dwc3_event_buffer
*evt
)
3015 struct dwc3
*dwc
= evt
->dwc
;
3016 irqreturn_t ret
= IRQ_NONE
;
3022 if (!(evt
->flags
& DWC3_EVENT_PENDING
))
3026 union dwc3_event event
;
3028 event
.raw
= *(u32
*) (evt
->cache
+ evt
->lpos
);
3030 dwc3_process_event_entry(dwc
, &event
);
3033 * FIXME we wrap around correctly to the next entry as
3034 * almost all entries are 4 bytes in size. There is one
3035 * entry which has 12 bytes which is a regular entry
3036 * followed by 8 bytes data. ATM I don't know how
3037 * things are organized if we get next to the a
3038 * boundary so I worry about that once we try to handle
3041 evt
->lpos
= (evt
->lpos
+ 4) % evt
->length
;
3046 evt
->flags
&= ~DWC3_EVENT_PENDING
;
3049 /* Unmask interrupt */
3050 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTSIZ(0));
3051 reg
&= ~DWC3_GEVNTSIZ_INTMASK
;
3052 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), reg
);
3054 if (dwc
->imod_interval
) {
3055 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB
);
3056 dwc3_writel(dwc
->regs
, DWC3_DEV_IMOD(0), dwc
->imod_interval
);
3062 static irqreturn_t
dwc3_thread_interrupt(int irq
, void *_evt
)
3064 struct dwc3_event_buffer
*evt
= _evt
;
3065 struct dwc3
*dwc
= evt
->dwc
;
3066 unsigned long flags
;
3067 irqreturn_t ret
= IRQ_NONE
;
3069 spin_lock_irqsave(&dwc
->lock
, flags
);
3070 ret
= dwc3_process_event_buf(evt
);
3071 spin_unlock_irqrestore(&dwc
->lock
, flags
);
3076 static irqreturn_t
dwc3_check_event_buf(struct dwc3_event_buffer
*evt
)
3078 struct dwc3
*dwc
= evt
->dwc
;
3083 if (pm_runtime_suspended(dwc
->dev
)) {
3084 pm_runtime_get(dwc
->dev
);
3085 disable_irq_nosync(dwc
->irq_gadget
);
3086 dwc
->pending_events
= true;
3091 * With PCIe legacy interrupt, test shows that top-half irq handler can
3092 * be called again after HW interrupt deassertion. Check if bottom-half
3093 * irq event handler completes before caching new event to prevent
3096 if (evt
->flags
& DWC3_EVENT_PENDING
)
3099 count
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(0));
3100 count
&= DWC3_GEVNTCOUNT_MASK
;
3105 evt
->flags
|= DWC3_EVENT_PENDING
;
3107 /* Mask interrupt */
3108 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTSIZ(0));
3109 reg
|= DWC3_GEVNTSIZ_INTMASK
;
3110 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), reg
);
3112 amount
= min(count
, evt
->length
- evt
->lpos
);
3113 memcpy(evt
->cache
+ evt
->lpos
, evt
->buf
+ evt
->lpos
, amount
);
3116 memcpy(evt
->cache
, evt
->buf
, count
- amount
);
3118 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), count
);
3120 return IRQ_WAKE_THREAD
;
3123 static irqreturn_t
dwc3_interrupt(int irq
, void *_evt
)
3125 struct dwc3_event_buffer
*evt
= _evt
;
3127 return dwc3_check_event_buf(evt
);
3130 static int dwc3_gadget_get_irq(struct dwc3
*dwc
)
3132 struct platform_device
*dwc3_pdev
= to_platform_device(dwc
->dev
);
3135 irq
= platform_get_irq_byname(dwc3_pdev
, "peripheral");
3139 if (irq
== -EPROBE_DEFER
)
3142 irq
= platform_get_irq_byname(dwc3_pdev
, "dwc_usb3");
3146 if (irq
== -EPROBE_DEFER
)
3149 irq
= platform_get_irq(dwc3_pdev
, 0);
3153 if (irq
!= -EPROBE_DEFER
)
3154 dev_err(dwc
->dev
, "missing peripheral IRQ\n");
3164 * dwc3_gadget_init - initializes gadget related registers
3165 * @dwc: pointer to our controller context structure
3167 * Returns 0 on success otherwise negative errno.
3169 int dwc3_gadget_init(struct dwc3
*dwc
)
3174 irq
= dwc3_gadget_get_irq(dwc
);
3180 dwc
->irq_gadget
= irq
;
3182 dwc
->ep0_trb
= dma_alloc_coherent(dwc
->sysdev
,
3183 sizeof(*dwc
->ep0_trb
) * 2,
3184 &dwc
->ep0_trb_addr
, GFP_KERNEL
);
3185 if (!dwc
->ep0_trb
) {
3186 dev_err(dwc
->dev
, "failed to allocate ep0 trb\n");
3191 dwc
->setup_buf
= kzalloc(DWC3_EP0_SETUP_SIZE
, GFP_KERNEL
);
3192 if (!dwc
->setup_buf
) {
3197 dwc
->bounce
= dma_alloc_coherent(dwc
->sysdev
, DWC3_BOUNCE_SIZE
,
3198 &dwc
->bounce_addr
, GFP_KERNEL
);
3204 init_completion(&dwc
->ep0_in_setup
);
3206 dwc
->gadget
.ops
= &dwc3_gadget_ops
;
3207 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
3208 dwc
->gadget
.sg_supported
= true;
3209 dwc
->gadget
.name
= "dwc3-gadget";
3210 dwc
->gadget
.is_otg
= dwc
->dr_mode
== USB_DR_MODE_OTG
;
3213 * FIXME We might be setting max_speed to <SUPER, however versions
3214 * <2.20a of dwc3 have an issue with metastability (documented
3215 * elsewhere in this driver) which tells us we can't set max speed to
3216 * anything lower than SUPER.
3218 * Because gadget.max_speed is only used by composite.c and function
3219 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3220 * to happen so we avoid sending SuperSpeed Capability descriptor
3221 * together with our BOS descriptor as that could confuse host into
3222 * thinking we can handle super speed.
3224 * Note that, in fact, we won't even support GetBOS requests when speed
3225 * is less than super speed because we don't have means, yet, to tell
3226 * composite.c that we are USB 2.0 + LPM ECN.
3228 if (dwc
->revision
< DWC3_REVISION_220A
&&
3229 !dwc
->dis_metastability_quirk
)
3230 dev_info(dwc
->dev
, "changing max_speed on rev %08x\n",
3233 dwc
->gadget
.max_speed
= dwc
->maximum_speed
;
3236 * REVISIT: Here we should clear all pending IRQs to be
3237 * sure we're starting from a well known location.
3240 ret
= dwc3_gadget_init_endpoints(dwc
, dwc
->num_eps
);
3244 ret
= usb_add_gadget_udc(dwc
->dev
, &dwc
->gadget
);
3246 dev_err(dwc
->dev
, "failed to register udc\n");
3253 dwc3_gadget_free_endpoints(dwc
);
3256 dma_free_coherent(dwc
->sysdev
, DWC3_BOUNCE_SIZE
, dwc
->bounce
,
3260 kfree(dwc
->setup_buf
);
3263 dma_free_coherent(dwc
->sysdev
, sizeof(*dwc
->ep0_trb
) * 2,
3264 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
3270 /* -------------------------------------------------------------------------- */
3272 void dwc3_gadget_exit(struct dwc3
*dwc
)
3274 usb_del_gadget_udc(&dwc
->gadget
);
3275 dwc3_gadget_free_endpoints(dwc
);
3276 dma_free_coherent(dwc
->sysdev
, DWC3_BOUNCE_SIZE
, dwc
->bounce
,
3278 kfree(dwc
->setup_buf
);
3279 dma_free_coherent(dwc
->sysdev
, sizeof(*dwc
->ep0_trb
) * 2,
3280 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
3283 int dwc3_gadget_suspend(struct dwc3
*dwc
)
3285 if (!dwc
->gadget_driver
)
3288 dwc3_gadget_run_stop(dwc
, false, false);
3289 dwc3_disconnect_gadget(dwc
);
3290 __dwc3_gadget_stop(dwc
);
3295 int dwc3_gadget_resume(struct dwc3
*dwc
)
3299 if (!dwc
->gadget_driver
)
3302 ret
= __dwc3_gadget_start(dwc
);
3306 ret
= dwc3_gadget_run_stop(dwc
, true, false);
3313 __dwc3_gadget_stop(dwc
);
3319 void dwc3_gadget_process_pending_events(struct dwc3
*dwc
)
3321 if (dwc
->pending_events
) {
3322 dwc3_interrupt(dwc
->irq_gadget
, dwc
->ev_buf
);
3323 dwc
->pending_events
= false;
3324 enable_irq(dwc
->irq_gadget
);