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>
30 #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
31 & ~((d)->interval - 1))
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)
38 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
41 int dwc3_gadget_set_test_mode(struct dwc3
*dwc
, int mode
)
45 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
46 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
60 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
66 * dwc3_gadget_get_link_state - gets current state of usb link
67 * @dwc: pointer to our context structure
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
72 int dwc3_gadget_get_link_state(struct dwc3
*dwc
)
76 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
78 return DWC3_DSTS_USBLNKST(reg
);
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
86 * Caller should take care of locking. This function will
87 * return 0 on success or -ETIMEDOUT.
89 int dwc3_gadget_set_link_state(struct dwc3
*dwc
, enum dwc3_link_state state
)
95 * Wait until device controller is ready. Only applies to 1.94a and
98 if (dwc
->revision
>= DWC3_REVISION_194A
) {
100 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
101 if (reg
& DWC3_DSTS_DCNRD
)
111 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
112 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
114 /* set requested state */
115 reg
|= DWC3_DCTL_ULSTCHNGREQ(state
);
116 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
119 * The following code is racy when called from dwc3_gadget_wakeup,
120 * and is not needed, at least on newer versions
122 if (dwc
->revision
>= DWC3_REVISION_194A
)
125 /* wait for a change in DSTS */
128 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
130 if (DWC3_DSTS_USBLNKST(reg
) == state
)
140 * dwc3_ep_inc_trb - increment a trb index.
141 * @index: Pointer to the TRB index to increment.
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.
147 static void dwc3_ep_inc_trb(u8
*index
)
150 if (*index
== (DWC3_TRB_NUM
- 1))
155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156 * @dep: The endpoint whose enqueue pointer we're incrementing
158 static void dwc3_ep_inc_enq(struct dwc3_ep
*dep
)
160 dwc3_ep_inc_trb(&dep
->trb_enqueue
);
164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165 * @dep: The endpoint whose enqueue pointer we're incrementing
167 static void dwc3_ep_inc_deq(struct dwc3_ep
*dep
)
169 dwc3_ep_inc_trb(&dep
->trb_dequeue
);
172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep
*dep
,
173 struct dwc3_request
*req
, int status
)
175 struct dwc3
*dwc
= dep
->dwc
;
177 list_del(&req
->list
);
179 req
->needs_extra_trb
= false;
181 if (req
->request
.status
== -EINPROGRESS
)
182 req
->request
.status
= status
;
185 usb_gadget_unmap_request_by_dev(dwc
->sysdev
,
186 &req
->request
, req
->direction
);
189 trace_dwc3_gadget_giveback(req
);
192 pm_runtime_put(dwc
->dev
);
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
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.
205 void dwc3_gadget_giveback(struct dwc3_ep
*dep
, struct dwc3_request
*req
,
208 struct dwc3
*dwc
= dep
->dwc
;
210 dwc3_gadget_del_and_unmap_request(dep
, req
, status
);
211 req
->status
= DWC3_REQUEST_STATUS_COMPLETED
;
213 spin_unlock(&dwc
->lock
);
214 usb_gadget_giveback_request(&dep
->endpoint
, &req
->request
);
215 spin_lock(&dwc
->lock
);
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
224 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
225 * and wait for its completion.
227 int dwc3_send_gadget_generic_command(struct dwc3
*dwc
, unsigned cmd
, u32 param
)
234 dwc3_writel(dwc
->regs
, DWC3_DGCMDPAR
, param
);
235 dwc3_writel(dwc
->regs
, DWC3_DGCMD
, cmd
| DWC3_DGCMD_CMDACT
);
238 reg
= dwc3_readl(dwc
->regs
, DWC3_DGCMD
);
239 if (!(reg
& DWC3_DGCMD_CMDACT
)) {
240 status
= DWC3_DGCMD_STATUS(reg
);
252 trace_dwc3_gadget_generic_cmd(cmd
, param
, status
);
257 static int __dwc3_gadget_wakeup(struct dwc3
*dwc
);
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
265 * Caller should handle locking. This function will issue @cmd with given
266 * @params to @dep and wait for its completion.
268 int dwc3_send_gadget_ep_cmd(struct dwc3_ep
*dep
, unsigned cmd
,
269 struct dwc3_gadget_ep_cmd_params
*params
)
271 const struct usb_endpoint_descriptor
*desc
= dep
->endpoint
.desc
;
272 struct dwc3
*dwc
= dep
->dwc
;
274 u32 saved_config
= 0;
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
285 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
286 * settings. Restore them after the command is completed.
288 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
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
;
297 if (reg
& DWC3_GUSB2PHYCFG_ENBLSLPM
) {
298 saved_config
|= DWC3_GUSB2PHYCFG_ENBLSLPM
;
299 reg
&= ~DWC3_GUSB2PHYCFG_ENBLSLPM
;
303 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
306 if (DWC3_DEPCMD_CMD(cmd
) == DWC3_DEPCMD_STARTTRANSFER
) {
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
);
313 if (unlikely(needs_wakeup
)) {
314 ret
= __dwc3_gadget_wakeup(dwc
);
315 dev_WARN_ONCE(dwc
->dev
, ret
, "wakeup failed --> %d\n",
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
);
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
330 * With this, we don't need to wait for command completion and can
331 * straight away issue further commands to the endpoint.
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.
339 if (DWC3_DEPCMD_CMD(cmd
) == DWC3_DEPCMD_UPDATETRANSFER
&&
340 !usb_endpoint_xfer_isoc(desc
))
341 cmd
&= ~(DWC3_DEPCMD_CMDIOC
| DWC3_DEPCMD_CMDACT
);
343 cmd
|= DWC3_DEPCMD_CMDACT
;
345 dwc3_writel(dep
->regs
, DWC3_DEPCMD
, cmd
);
347 reg
= dwc3_readl(dep
->regs
, DWC3_DEPCMD
);
348 if (!(reg
& DWC3_DEPCMD_CMDACT
)) {
349 cmd_status
= DWC3_DEPCMD_STATUS(reg
);
351 switch (cmd_status
) {
355 case DEPEVT_TRANSFER_NO_RESOURCE
:
358 case DEPEVT_TRANSFER_BUS_EXPIRY
:
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'.
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.
373 dev_WARN(dwc
->dev
, "UNKNOWN cmd status\n");
382 cmd_status
= -ETIMEDOUT
;
385 trace_dwc3_gadget_ep_cmd(dep
, cmd
, params
, cmd_status
);
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
);
393 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
395 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
401 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep
*dep
)
403 struct dwc3
*dwc
= dep
->dwc
;
404 struct dwc3_gadget_ep_cmd_params params
;
405 u32 cmd
= DWC3_DEPCMD_CLEARSTALL
;
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
415 if (dep
->direction
&& (dwc
->revision
>= DWC3_REVISION_260A
) &&
416 (dwc
->gadget
.speed
>= USB_SPEED_SUPER
))
417 cmd
|= DWC3_DEPCMD_CLEARPENDIN
;
419 memset(¶ms
, 0, sizeof(params
));
421 return dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
424 static dma_addr_t
dwc3_trb_dma_offset(struct dwc3_ep
*dep
,
425 struct dwc3_trb
*trb
)
427 u32 offset
= (char *) trb
- (char *) dep
->trb_pool
;
429 return dep
->trb_pool_dma
+ offset
;
432 static int dwc3_alloc_trb_pool(struct dwc3_ep
*dep
)
434 struct dwc3
*dwc
= dep
->dwc
;
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",
451 static void dwc3_free_trb_pool(struct dwc3_ep
*dep
)
453 struct dwc3
*dwc
= dep
->dwc
;
455 dma_free_coherent(dwc
->sysdev
, sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
456 dep
->trb_pool
, dep
->trb_pool_dma
);
458 dep
->trb_pool
= NULL
;
459 dep
->trb_pool_dma
= 0;
462 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep
*dep
)
464 struct dwc3_gadget_ep_cmd_params params
;
466 memset(¶ms
, 0x00, sizeof(params
));
468 params
.param0
= DWC3_DEPXFERCFG_NUM_XFER_RES(1);
470 return dwc3_send_gadget_ep_cmd(dep
, DWC3_DEPCMD_SETTRANSFRESOURCE
,
475 * dwc3_gadget_start_config - configure ep resources
476 * @dep: endpoint that is being enabled
478 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
479 * completion, it will set Transfer Resource for all available endpoints.
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:
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.
492 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
493 * endpoint on alt setting (8.1.6).
495 * The following simplified method is used instead:
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.
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.
507 static int dwc3_gadget_start_config(struct dwc3_ep
*dep
)
509 struct dwc3_gadget_ep_cmd_params params
;
518 memset(¶ms
, 0x00, sizeof(params
));
519 cmd
= DWC3_DEPCMD_DEPSTARTCFG
;
522 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
526 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
527 struct dwc3_ep
*dep
= dwc
->eps
[i
];
532 ret
= dwc3_gadget_set_xfer_resource(dep
);
540 static int dwc3_gadget_set_ep_config(struct dwc3_ep
*dep
, unsigned int action
)
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
;
547 comp_desc
= dep
->endpoint
.comp_desc
;
548 desc
= dep
->endpoint
.desc
;
550 memset(¶ms
, 0x00, sizeof(params
));
552 params
.param0
= DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc
))
553 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc
));
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);
561 params
.param0
|= action
;
562 if (action
== DWC3_DEPCFG_ACTION_RESTORE
)
563 params
.param2
|= dep
->saved_state
;
565 if (usb_endpoint_xfer_control(desc
))
566 params
.param1
= DWC3_DEPCFG_XFER_COMPLETE_EN
;
568 if (dep
->number
<= 1 || usb_endpoint_xfer_isoc(desc
))
569 params
.param1
|= DWC3_DEPCFG_XFER_NOT_READY_EN
;
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;
577 if (!usb_endpoint_xfer_control(desc
))
578 params
.param1
|= DWC3_DEPCFG_XFER_IN_PROGRESS_EN
;
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.
586 params
.param1
|= DWC3_DEPCFG_EP_NUMBER(dep
->number
);
589 * We must use the lower 16 TX FIFOs even though
593 params
.param0
|= DWC3_DEPCFG_FIFO_NUMBER(dep
->number
>> 1);
595 if (desc
->bInterval
) {
596 params
.param1
|= DWC3_DEPCFG_BINTERVAL_M1(desc
->bInterval
- 1);
597 dep
->interval
= 1 << (desc
->bInterval
- 1);
600 return dwc3_send_gadget_ep_cmd(dep
, DWC3_DEPCMD_SETEPCONFIG
, ¶ms
);
604 * __dwc3_gadget_ep_enable - initializes a hw endpoint
605 * @dep: endpoint to be initialized
606 * @action: one of INIT, MODIFY or RESTORE
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
, unsigned int action
)
613 const struct usb_endpoint_descriptor
*desc
= dep
->endpoint
.desc
;
614 struct dwc3
*dwc
= dep
->dwc
;
619 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
620 ret
= dwc3_gadget_start_config(dep
);
625 ret
= dwc3_gadget_set_ep_config(dep
, action
);
629 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
630 struct dwc3_trb
*trb_st_hw
;
631 struct dwc3_trb
*trb_link
;
633 dep
->type
= usb_endpoint_type(desc
);
634 dep
->flags
|= DWC3_EP_ENABLED
;
636 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
637 reg
|= DWC3_DALEPENA_EP(dep
->number
);
638 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
640 if (usb_endpoint_xfer_control(desc
))
643 /* Initialize the TRB ring */
644 dep
->trb_dequeue
= 0;
645 dep
->trb_enqueue
= 0;
646 memset(dep
->trb_pool
, 0,
647 sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
);
649 /* Link TRB. The HWO bit is never reset */
650 trb_st_hw
= &dep
->trb_pool
[0];
652 trb_link
= &dep
->trb_pool
[DWC3_TRB_NUM
- 1];
653 trb_link
->bpl
= lower_32_bits(dwc3_trb_dma_offset(dep
, trb_st_hw
));
654 trb_link
->bph
= upper_32_bits(dwc3_trb_dma_offset(dep
, trb_st_hw
));
655 trb_link
->ctrl
|= DWC3_TRBCTL_LINK_TRB
;
656 trb_link
->ctrl
|= DWC3_TRB_CTRL_HWO
;
660 * Issue StartTransfer here with no-op TRB so we can always rely on No
661 * Response Update Transfer command.
663 if ((usb_endpoint_xfer_bulk(desc
) && !dep
->stream_capable
) ||
664 usb_endpoint_xfer_int(desc
)) {
665 struct dwc3_gadget_ep_cmd_params params
;
666 struct dwc3_trb
*trb
;
670 memset(¶ms
, 0, sizeof(params
));
671 trb
= &dep
->trb_pool
[0];
672 trb_dma
= dwc3_trb_dma_offset(dep
, trb
);
674 params
.param0
= upper_32_bits(trb_dma
);
675 params
.param1
= lower_32_bits(trb_dma
);
677 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
679 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
685 trace_dwc3_gadget_ep_enable(dep
);
690 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
)
694 struct dwc3_request
*req
;
696 dwc3_stop_active_transfer(dep
, true, false);
698 /* - giveback all requests to gadget driver */
699 while (!list_empty(&dep
->started_list
)) {
700 req
= next_request(&dep
->started_list
);
702 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
705 while (!list_empty(&dep
->pending_list
)) {
706 req
= next_request(&dep
->pending_list
);
708 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
713 * __dwc3_gadget_ep_disable - disables a hw endpoint
714 * @dep: the endpoint to disable
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.
720 * Caller should take care of locking.
722 static int __dwc3_gadget_ep_disable(struct dwc3_ep
*dep
)
724 struct dwc3
*dwc
= dep
->dwc
;
727 trace_dwc3_gadget_ep_disable(dep
);
729 dwc3_remove_requests(dwc
, dep
);
731 /* make sure HW endpoint isn't stalled */
732 if (dep
->flags
& DWC3_EP_STALL
)
733 __dwc3_gadget_ep_set_halt(dep
, 0, false);
735 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
736 reg
&= ~DWC3_DALEPENA_EP(dep
->number
);
737 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
739 dep
->stream_capable
= false;
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
;
752 /* -------------------------------------------------------------------------- */
754 static int dwc3_gadget_ep0_enable(struct usb_ep
*ep
,
755 const struct usb_endpoint_descriptor
*desc
)
760 static int dwc3_gadget_ep0_disable(struct usb_ep
*ep
)
765 /* -------------------------------------------------------------------------- */
767 static int dwc3_gadget_ep_enable(struct usb_ep
*ep
,
768 const struct usb_endpoint_descriptor
*desc
)
775 if (!ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
776 pr_debug("dwc3: invalid parameters\n");
780 if (!desc
->wMaxPacketSize
) {
781 pr_debug("dwc3: missing wMaxPacketSize\n");
785 dep
= to_dwc3_ep(ep
);
788 if (dev_WARN_ONCE(dwc
->dev
, dep
->flags
& DWC3_EP_ENABLED
,
789 "%s is already enabled\n",
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
);
800 static int dwc3_gadget_ep_disable(struct usb_ep
*ep
)
808 pr_debug("dwc3: invalid parameters\n");
812 dep
= to_dwc3_ep(ep
);
815 if (dev_WARN_ONCE(dwc
->dev
, !(dep
->flags
& DWC3_EP_ENABLED
),
816 "%s is already disabled\n",
820 spin_lock_irqsave(&dwc
->lock
, flags
);
821 ret
= __dwc3_gadget_ep_disable(dep
);
822 spin_unlock_irqrestore(&dwc
->lock
, flags
);
827 static struct usb_request
*dwc3_gadget_ep_alloc_request(struct usb_ep
*ep
,
830 struct dwc3_request
*req
;
831 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
833 req
= kzalloc(sizeof(*req
), gfp_flags
);
837 req
->direction
= dep
->direction
;
838 req
->epnum
= dep
->number
;
840 req
->status
= DWC3_REQUEST_STATUS_UNKNOWN
;
842 trace_dwc3_alloc_request(req
);
844 return &req
->request
;
847 static void dwc3_gadget_ep_free_request(struct usb_ep
*ep
,
848 struct usb_request
*request
)
850 struct dwc3_request
*req
= to_dwc3_request(request
);
852 trace_dwc3_free_request(req
);
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
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.
865 static struct dwc3_trb
*dwc3_ep_prev_trb(struct dwc3_ep
*dep
, u8 index
)
870 tmp
= DWC3_TRB_NUM
- 1;
872 return &dep
->trb_pool
[tmp
- 1];
875 static u32
dwc3_calc_trbs_left(struct dwc3_ep
*dep
)
877 struct dwc3_trb
*tmp
;
881 * If enqueue & dequeue are equal than it is either full or empty.
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.
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
)
892 return DWC3_TRB_NUM
- 1;
895 trbs_left
= dep
->trb_dequeue
- dep
->trb_enqueue
;
896 trbs_left
&= (DWC3_TRB_NUM
- 1);
898 if (dep
->trb_dequeue
< dep
->trb_enqueue
)
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
)
908 struct dwc3
*dwc
= dep
->dwc
;
909 struct usb_gadget
*gadget
= &dwc
->gadget
;
910 enum usb_device_speed speed
= gadget
->speed
;
912 trb
->size
= DWC3_TRB_SIZE_LENGTH(length
);
913 trb
->bpl
= lower_32_bits(dma
);
914 trb
->bph
= upper_32_bits(dma
);
916 switch (usb_endpoint_type(dep
->endpoint
.desc
)) {
917 case USB_ENDPOINT_XFER_CONTROL
:
918 trb
->ctrl
= DWC3_TRBCTL_CONTROL_SETUP
;
921 case USB_ENDPOINT_XFER_ISOC
:
923 trb
->ctrl
= DWC3_TRBCTL_ISOCHRONOUS_FIRST
;
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."
936 * IOW, we should satisfy the following cases:
938 * 1) length <= maxpacket
941 * 2) maxpacket < length <= (2 * maxpacket)
944 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
945 * - DATA2, DATA1, DATA0
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
);
952 if (length
<= (2 * maxp
))
958 trb
->size
|= DWC3_TRB_SIZE_PCM1(mult
);
961 trb
->ctrl
= DWC3_TRBCTL_ISOCHRONOUS
;
964 /* always enable Interrupt on Missed ISOC */
965 trb
->ctrl
|= DWC3_TRB_CTRL_ISP_IMI
;
968 case USB_ENDPOINT_XFER_BULK
:
969 case USB_ENDPOINT_XFER_INT
:
970 trb
->ctrl
= DWC3_TRBCTL_NORMAL
;
974 * This is only possible with faulty memory because we
975 * checked it already :)
977 dev_WARN(dwc
->dev
, "Unknown endpoint type %d\n",
978 usb_endpoint_type(dep
->endpoint
.desc
));
982 * Enable Continue on Short Packet
983 * when endpoint is not a stream capable
985 if (usb_endpoint_dir_out(dep
->endpoint
.desc
)) {
986 if (!dep
->stream_capable
)
987 trb
->ctrl
|= DWC3_TRB_CTRL_CSP
;
990 trb
->ctrl
|= DWC3_TRB_CTRL_ISP_IMI
;
993 if ((!no_interrupt
&& !chain
) ||
994 (dwc3_calc_trbs_left(dep
) == 1))
995 trb
->ctrl
|= DWC3_TRB_CTRL_IOC
;
998 trb
->ctrl
|= DWC3_TRB_CTRL_CHN
;
1000 if (usb_endpoint_xfer_bulk(dep
->endpoint
.desc
) && dep
->stream_capable
)
1001 trb
->ctrl
|= DWC3_TRB_CTRL_SID_SOFN(stream_id
);
1003 trb
->ctrl
|= DWC3_TRB_CTRL_HWO
;
1005 dwc3_ep_inc_enq(dep
);
1007 trace_dwc3_prepare_trb(dep
, trb
);
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.
1017 static void dwc3_prepare_one_trb(struct dwc3_ep
*dep
,
1018 struct dwc3_request
*req
, unsigned chain
, unsigned node
)
1020 struct dwc3_trb
*trb
;
1021 unsigned int length
;
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
;
1027 if (req
->request
.num_sgs
> 0) {
1028 length
= sg_dma_len(req
->start_sg
);
1029 dma
= sg_dma_address(req
->start_sg
);
1031 length
= req
->request
.length
;
1032 dma
= req
->request
.dma
;
1035 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
1038 dwc3_gadget_move_started_request(req
);
1040 req
->trb_dma
= dwc3_trb_dma_offset(dep
, trb
);
1045 __dwc3_prepare_one_trb(dep
, trb
, dma
, length
, chain
, node
,
1046 stream_id
, short_not_ok
, no_interrupt
);
1049 static void dwc3_prepare_one_trb_sg(struct dwc3_ep
*dep
,
1050 struct dwc3_request
*req
)
1052 struct scatterlist
*sg
= req
->start_sg
;
1053 struct scatterlist
*s
;
1056 unsigned int remaining
= req
->request
.num_mapped_sgs
1057 - req
->num_queued_sgs
;
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;
1068 if (rem
&& usb_endpoint_dir_out(dep
->endpoint
.desc
) && !chain
) {
1069 struct dwc3
*dwc
= dep
->dwc
;
1070 struct dwc3_trb
*trb
;
1072 req
->needs_extra_trb
= true;
1074 /* prepare normal TRB */
1075 dwc3_prepare_one_trb(dep
, req
, true, i
);
1077 /* Now prepare one extra TRB to align transfer size */
1078 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
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
);
1086 dwc3_prepare_one_trb(dep
, req
, chain
, i
);
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
1097 req
->start_sg
= sg_next(s
);
1099 req
->num_queued_sgs
++;
1101 if (!dwc3_calc_trbs_left(dep
))
1106 static void dwc3_prepare_one_trb_linear(struct dwc3_ep
*dep
,
1107 struct dwc3_request
*req
)
1109 unsigned int length
= req
->request
.length
;
1110 unsigned int maxp
= usb_endpoint_maxp(dep
->endpoint
.desc
);
1111 unsigned int rem
= length
% maxp
;
1113 if ((!length
|| rem
) && usb_endpoint_dir_out(dep
->endpoint
.desc
)) {
1114 struct dwc3
*dwc
= dep
->dwc
;
1115 struct dwc3_trb
*trb
;
1117 req
->needs_extra_trb
= true;
1119 /* prepare normal TRB */
1120 dwc3_prepare_one_trb(dep
, req
, true, 0);
1122 /* Now prepare one extra TRB to align transfer size */
1123 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
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
;
1134 req
->needs_extra_trb
= true;
1136 /* prepare normal TRB */
1137 dwc3_prepare_one_trb(dep
, req
, true, 0);
1139 /* Now prepare one extra TRB to handle ZLP */
1140 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
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
);
1147 dwc3_prepare_one_trb(dep
, req
, false, 0);
1152 * dwc3_prepare_trbs - setup TRBs from requests
1153 * @dep: endpoint for which requests are being prepared
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.
1159 static void dwc3_prepare_trbs(struct dwc3_ep
*dep
)
1161 struct dwc3_request
*req
, *n
;
1163 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM
);
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.
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
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
);
1179 if (!dwc3_calc_trbs_left(dep
))
1183 list_for_each_entry_safe(req
, n
, &dep
->pending_list
, list
) {
1184 struct dwc3
*dwc
= dep
->dwc
;
1187 ret
= usb_gadget_map_request_by_dev(dwc
->sysdev
, &req
->request
,
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
;
1197 if (req
->num_pending_sgs
> 0)
1198 dwc3_prepare_one_trb_sg(dep
, req
);
1200 dwc3_prepare_one_trb_linear(dep
, req
);
1202 if (!dwc3_calc_trbs_left(dep
))
1207 static int __dwc3_gadget_kick_transfer(struct dwc3_ep
*dep
)
1209 struct dwc3_gadget_ep_cmd_params params
;
1210 struct dwc3_request
*req
;
1215 if (!dwc3_calc_trbs_left(dep
))
1218 starting
= !(dep
->flags
& DWC3_EP_TRANSFER_STARTED
);
1220 dwc3_prepare_trbs(dep
);
1221 req
= next_request(&dep
->started_list
);
1223 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
1227 memset(¶ms
, 0, sizeof(params
));
1230 params
.param0
= upper_32_bits(req
->trb_dma
);
1231 params
.param1
= lower_32_bits(req
->trb_dma
);
1232 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
1234 if (dep
->stream_capable
)
1235 cmd
|= DWC3_DEPCMD_PARAM(req
->request
.stream_id
);
1237 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
))
1238 cmd
|= DWC3_DEPCMD_PARAM(dep
->frame_number
);
1240 cmd
= DWC3_DEPCMD_UPDATETRANSFER
|
1241 DWC3_DEPCMD_PARAM(dep
->resource_index
);
1244 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
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.
1252 memset(req
->trb
, 0, sizeof(struct dwc3_trb
));
1253 dwc3_gadget_del_and_unmap_request(dep
, req
, ret
);
1260 static int __dwc3_gadget_get_frame(struct dwc3
*dwc
)
1264 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1265 return DWC3_DSTS_SOFFN(reg
);
1269 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1270 * @dep: isoc endpoint
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.
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.
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.
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.
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:
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
1307 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1310 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep
*dep
)
1316 while (dep
->combo_num
< 2) {
1317 struct dwc3_gadget_ep_cmd_params params
;
1318 u32 test_frame_number
;
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
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
);
1329 params
.param0
= upper_32_bits(dep
->dwc
->bounce_addr
);
1330 params
.param1
= lower_32_bits(dep
->dwc
->bounce_addr
);
1332 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
1333 cmd
|= DWC3_DEPCMD_PARAM(test_frame_number
);
1334 cmd_status
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
1336 /* Redo if some other failure beside bus-expiry is received */
1337 if (cmd_status
&& cmd_status
!= -EAGAIN
) {
1338 dep
->start_cmd_status
= 0;
1343 /* Store the first test status */
1344 if (dep
->combo_num
== 0)
1345 dep
->start_cmd_status
= cmd_status
;
1350 * End the transfer if the START_TRANSFER command is successful
1351 * to wait for the next XferNotReady to test the command again
1353 if (cmd_status
== 0) {
1354 dwc3_stop_active_transfer(dep
, true, true);
1359 /* test0 and test1 are both completed at this point */
1360 test0
= (dep
->start_cmd_status
== 0);
1361 test1
= (cmd_status
== 0);
1363 if (!test0
&& test1
)
1365 else if (!test0
&& !test1
)
1367 else if (test0
&& !test1
)
1369 else if (test0
&& test1
)
1372 dep
->frame_number
&= 0x3fff;
1373 dep
->frame_number
|= dep
->combo_num
<< 14;
1374 dep
->frame_number
+= max_t(u32
, 4, dep
->interval
);
1376 /* Reinitialize test variables */
1377 dep
->start_cmd_status
= 0;
1380 return __dwc3_gadget_kick_transfer(dep
);
1383 static int __dwc3_gadget_start_isoc(struct dwc3_ep
*dep
)
1385 struct dwc3
*dwc
= dep
->dwc
;
1389 if (list_empty(&dep
->pending_list
)) {
1390 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
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
))) {
1400 if (dwc
->gadget
.speed
<= USB_SPEED_HIGH
&& dep
->direction
)
1401 return dwc3_gadget_start_isoc_quirk(dep
);
1404 for (i
= 0; i
< DWC3_ISOC_MAX_RETRIES
; i
++) {
1405 dep
->frame_number
= DWC3_ALIGN_FRAME(dep
, i
+ 1);
1407 ret
= __dwc3_gadget_kick_transfer(dep
);
1415 static int __dwc3_gadget_ep_queue(struct dwc3_ep
*dep
, struct dwc3_request
*req
)
1417 struct dwc3
*dwc
= dep
->dwc
;
1419 if (!dep
->endpoint
.desc
) {
1420 dev_err(dwc
->dev
, "%s: can't queue to disabled endpoint\n",
1425 if (WARN(req
->dep
!= dep
, "request %pK belongs to '%s'\n",
1426 &req
->request
, req
->dep
->name
))
1429 if (WARN(req
->status
< DWC3_REQUEST_STATUS_COMPLETED
,
1430 "%s: request %pK already in flight\n",
1431 dep
->name
, &req
->request
))
1434 pm_runtime_get(dwc
->dev
);
1436 req
->request
.actual
= 0;
1437 req
->request
.status
= -EINPROGRESS
;
1439 trace_dwc3_ep_queue(req
);
1441 list_add_tail(&req
->list
, &dep
->pending_list
);
1442 req
->status
= DWC3_REQUEST_STATUS_QUEUED
;
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.
1449 * Without this trick, we are very, very likely gonna get Bus Expiry
1450 * errors which will force us issue EndTransfer command.
1452 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
1453 if (!(dep
->flags
& DWC3_EP_PENDING_REQUEST
) &&
1454 !(dep
->flags
& DWC3_EP_TRANSFER_STARTED
))
1457 if ((dep
->flags
& DWC3_EP_PENDING_REQUEST
)) {
1458 if (!(dep
->flags
& DWC3_EP_TRANSFER_STARTED
)) {
1459 return __dwc3_gadget_start_isoc(dep
);
1464 return __dwc3_gadget_kick_transfer(dep
);
1467 static int dwc3_gadget_ep_queue(struct usb_ep
*ep
, struct usb_request
*request
,
1470 struct dwc3_request
*req
= to_dwc3_request(request
);
1471 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1472 struct dwc3
*dwc
= dep
->dwc
;
1474 unsigned long flags
;
1478 spin_lock_irqsave(&dwc
->lock
, flags
);
1479 ret
= __dwc3_gadget_ep_queue(dep
, req
);
1480 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1485 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep
*dep
, struct dwc3_request
*req
)
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
1499 for (i
= 0; i
< req
->num_trbs
; i
++) {
1500 struct dwc3_trb
*trb
;
1503 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
1504 dwc3_ep_inc_deq(dep
);
1510 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep
*dep
)
1512 struct dwc3_request
*req
;
1513 struct dwc3_request
*tmp
;
1515 list_for_each_entry_safe(req
, tmp
, &dep
->cancelled_list
, list
) {
1516 dwc3_gadget_ep_skip_trbs(dep
, req
);
1517 dwc3_gadget_giveback(dep
, req
, -ECONNRESET
);
1521 static int dwc3_gadget_ep_dequeue(struct usb_ep
*ep
,
1522 struct usb_request
*request
)
1524 struct dwc3_request
*req
= to_dwc3_request(request
);
1525 struct dwc3_request
*r
= NULL
;
1527 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1528 struct dwc3
*dwc
= dep
->dwc
;
1530 unsigned long flags
;
1533 trace_dwc3_ep_dequeue(req
);
1535 spin_lock_irqsave(&dwc
->lock
, flags
);
1537 list_for_each_entry(r
, &dep
->pending_list
, list
) {
1543 list_for_each_entry(r
, &dep
->started_list
, list
) {
1548 /* wait until it is processed */
1549 dwc3_stop_active_transfer(dep
, true, true);
1554 dwc3_gadget_move_cancelled_request(req
);
1555 if (dep
->flags
& DWC3_EP_TRANSFER_STARTED
)
1560 dev_err(dwc
->dev
, "request %pK was not queued to %s\n",
1567 dwc3_gadget_giveback(dep
, req
, -ECONNRESET
);
1570 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1575 int __dwc3_gadget_ep_set_halt(struct dwc3_ep
*dep
, int value
, int protocol
)
1577 struct dwc3_gadget_ep_cmd_params params
;
1578 struct dwc3
*dwc
= dep
->dwc
;
1581 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
1582 dev_err(dwc
->dev
, "%s is of Isochronous type\n", dep
->name
);
1586 memset(¶ms
, 0x00, sizeof(params
));
1589 struct dwc3_trb
*trb
;
1591 unsigned transfer_in_flight
;
1594 if (dep
->number
> 1)
1595 trb
= dwc3_ep_prev_trb(dep
, dep
->trb_enqueue
);
1597 trb
= &dwc
->ep0_trb
[dep
->trb_enqueue
];
1599 transfer_in_flight
= trb
->ctrl
& DWC3_TRB_CTRL_HWO
;
1600 started
= !list_empty(&dep
->started_list
);
1602 if (!protocol
&& ((dep
->direction
&& transfer_in_flight
) ||
1603 (!dep
->direction
&& started
))) {
1607 ret
= dwc3_send_gadget_ep_cmd(dep
, DWC3_DEPCMD_SETSTALL
,
1610 dev_err(dwc
->dev
, "failed to set STALL on %s\n",
1613 dep
->flags
|= DWC3_EP_STALL
;
1616 ret
= dwc3_send_clear_stall_ep_cmd(dep
);
1618 dev_err(dwc
->dev
, "failed to clear STALL on %s\n",
1621 dep
->flags
&= ~(DWC3_EP_STALL
| DWC3_EP_WEDGE
);
1627 static int dwc3_gadget_ep_set_halt(struct usb_ep
*ep
, int value
)
1629 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1630 struct dwc3
*dwc
= dep
->dwc
;
1632 unsigned long flags
;
1636 spin_lock_irqsave(&dwc
->lock
, flags
);
1637 ret
= __dwc3_gadget_ep_set_halt(dep
, value
, false);
1638 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1643 static int dwc3_gadget_ep_set_wedge(struct usb_ep
*ep
)
1645 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1646 struct dwc3
*dwc
= dep
->dwc
;
1647 unsigned long flags
;
1650 spin_lock_irqsave(&dwc
->lock
, flags
);
1651 dep
->flags
|= DWC3_EP_WEDGE
;
1653 if (dep
->number
== 0 || dep
->number
== 1)
1654 ret
= __dwc3_gadget_ep0_set_halt(ep
, 1);
1656 ret
= __dwc3_gadget_ep_set_halt(dep
, 1, false);
1657 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1662 /* -------------------------------------------------------------------------- */
1664 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc
= {
1665 .bLength
= USB_DT_ENDPOINT_SIZE
,
1666 .bDescriptorType
= USB_DT_ENDPOINT
,
1667 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1670 static const struct usb_ep_ops dwc3_gadget_ep0_ops
= {
1671 .enable
= dwc3_gadget_ep0_enable
,
1672 .disable
= dwc3_gadget_ep0_disable
,
1673 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1674 .free_request
= dwc3_gadget_ep_free_request
,
1675 .queue
= dwc3_gadget_ep0_queue
,
1676 .dequeue
= dwc3_gadget_ep_dequeue
,
1677 .set_halt
= dwc3_gadget_ep0_set_halt
,
1678 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1681 static const struct usb_ep_ops dwc3_gadget_ep_ops
= {
1682 .enable
= dwc3_gadget_ep_enable
,
1683 .disable
= dwc3_gadget_ep_disable
,
1684 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1685 .free_request
= dwc3_gadget_ep_free_request
,
1686 .queue
= dwc3_gadget_ep_queue
,
1687 .dequeue
= dwc3_gadget_ep_dequeue
,
1688 .set_halt
= dwc3_gadget_ep_set_halt
,
1689 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1692 /* -------------------------------------------------------------------------- */
1694 static int dwc3_gadget_get_frame(struct usb_gadget
*g
)
1696 struct dwc3
*dwc
= gadget_to_dwc(g
);
1698 return __dwc3_gadget_get_frame(dwc
);
1701 static int __dwc3_gadget_wakeup(struct dwc3
*dwc
)
1712 * According to the Databook Remote wakeup request should
1713 * be issued only when the device is in early suspend state.
1715 * We can check that via USB Link State bits in DSTS register.
1717 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1719 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1720 if ((speed
== DWC3_DSTS_SUPERSPEED
) ||
1721 (speed
== DWC3_DSTS_SUPERSPEED_PLUS
))
1724 link_state
= DWC3_DSTS_USBLNKST(reg
);
1726 switch (link_state
) {
1727 case DWC3_LINK_STATE_RX_DET
: /* in HS, means Early Suspend */
1728 case DWC3_LINK_STATE_U3
: /* in HS, means SUSPEND */
1734 ret
= dwc3_gadget_set_link_state(dwc
, DWC3_LINK_STATE_RECOV
);
1736 dev_err(dwc
->dev
, "failed to put link in Recovery\n");
1740 /* Recent versions do this automatically */
1741 if (dwc
->revision
< DWC3_REVISION_194A
) {
1742 /* write zeroes to Link Change Request */
1743 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1744 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
1745 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1748 /* poll until Link State changes to ON */
1752 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1754 /* in HS, means ON */
1755 if (DWC3_DSTS_USBLNKST(reg
) == DWC3_LINK_STATE_U0
)
1759 if (DWC3_DSTS_USBLNKST(reg
) != DWC3_LINK_STATE_U0
) {
1760 dev_err(dwc
->dev
, "failed to send remote wakeup\n");
1767 static int dwc3_gadget_wakeup(struct usb_gadget
*g
)
1769 struct dwc3
*dwc
= gadget_to_dwc(g
);
1770 unsigned long flags
;
1773 spin_lock_irqsave(&dwc
->lock
, flags
);
1774 ret
= __dwc3_gadget_wakeup(dwc
);
1775 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1780 static int dwc3_gadget_set_selfpowered(struct usb_gadget
*g
,
1783 struct dwc3
*dwc
= gadget_to_dwc(g
);
1784 unsigned long flags
;
1786 spin_lock_irqsave(&dwc
->lock
, flags
);
1787 g
->is_selfpowered
= !!is_selfpowered
;
1788 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1793 static int dwc3_gadget_run_stop(struct dwc3
*dwc
, int is_on
, int suspend
)
1798 if (pm_runtime_suspended(dwc
->dev
))
1801 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1803 if (dwc
->revision
<= DWC3_REVISION_187A
) {
1804 reg
&= ~DWC3_DCTL_TRGTULST_MASK
;
1805 reg
|= DWC3_DCTL_TRGTULST_RX_DET
;
1808 if (dwc
->revision
>= DWC3_REVISION_194A
)
1809 reg
&= ~DWC3_DCTL_KEEP_CONNECT
;
1810 reg
|= DWC3_DCTL_RUN_STOP
;
1812 if (dwc
->has_hibernation
)
1813 reg
|= DWC3_DCTL_KEEP_CONNECT
;
1815 dwc
->pullups_connected
= true;
1817 reg
&= ~DWC3_DCTL_RUN_STOP
;
1819 if (dwc
->has_hibernation
&& !suspend
)
1820 reg
&= ~DWC3_DCTL_KEEP_CONNECT
;
1822 dwc
->pullups_connected
= false;
1825 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1828 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1829 reg
&= DWC3_DSTS_DEVCTRLHLT
;
1830 } while (--timeout
&& !(!is_on
^ !reg
));
1838 static int dwc3_gadget_pullup(struct usb_gadget
*g
, int is_on
)
1840 struct dwc3
*dwc
= gadget_to_dwc(g
);
1841 unsigned long flags
;
1847 * Per databook, when we want to stop the gadget, if a control transfer
1848 * is still in process, complete it and get the core into setup phase.
1850 if (!is_on
&& dwc
->ep0state
!= EP0_SETUP_PHASE
) {
1851 reinit_completion(&dwc
->ep0_in_setup
);
1853 ret
= wait_for_completion_timeout(&dwc
->ep0_in_setup
,
1854 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT
));
1856 dev_err(dwc
->dev
, "timed out waiting for SETUP phase\n");
1861 spin_lock_irqsave(&dwc
->lock
, flags
);
1862 ret
= dwc3_gadget_run_stop(dwc
, is_on
, false);
1863 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1868 static void dwc3_gadget_enable_irq(struct dwc3
*dwc
)
1872 /* Enable all but Start and End of Frame IRQs */
1873 reg
= (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN
|
1874 DWC3_DEVTEN_EVNTOVERFLOWEN
|
1875 DWC3_DEVTEN_CMDCMPLTEN
|
1876 DWC3_DEVTEN_ERRTICERREN
|
1877 DWC3_DEVTEN_WKUPEVTEN
|
1878 DWC3_DEVTEN_CONNECTDONEEN
|
1879 DWC3_DEVTEN_USBRSTEN
|
1880 DWC3_DEVTEN_DISCONNEVTEN
);
1882 if (dwc
->revision
< DWC3_REVISION_250A
)
1883 reg
|= DWC3_DEVTEN_ULSTCNGEN
;
1885 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, reg
);
1888 static void dwc3_gadget_disable_irq(struct dwc3
*dwc
)
1890 /* mask all interrupts */
1891 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
1894 static irqreturn_t
dwc3_interrupt(int irq
, void *_dwc
);
1895 static irqreturn_t
dwc3_thread_interrupt(int irq
, void *_dwc
);
1898 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1899 * @dwc: pointer to our context structure
1901 * The following looks like complex but it's actually very simple. In order to
1902 * calculate the number of packets we can burst at once on OUT transfers, we're
1903 * gonna use RxFIFO size.
1905 * To calculate RxFIFO size we need two numbers:
1906 * MDWIDTH = size, in bits, of the internal memory bus
1907 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1909 * Given these two numbers, the formula is simple:
1911 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1913 * 24 bytes is for 3x SETUP packets
1914 * 16 bytes is a clock domain crossing tolerance
1916 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1918 static void dwc3_gadget_setup_nump(struct dwc3
*dwc
)
1925 ram2_depth
= DWC3_GHWPARAMS7_RAM2_DEPTH(dwc
->hwparams
.hwparams7
);
1926 mdwidth
= DWC3_GHWPARAMS0_MDWIDTH(dwc
->hwparams
.hwparams0
);
1928 nump
= ((ram2_depth
* mdwidth
/ 8) - 24 - 16) / 1024;
1929 nump
= min_t(u32
, nump
, 16);
1932 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1933 reg
&= ~DWC3_DCFG_NUMP_MASK
;
1934 reg
|= nump
<< DWC3_DCFG_NUMP_SHIFT
;
1935 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1938 static int __dwc3_gadget_start(struct dwc3
*dwc
)
1940 struct dwc3_ep
*dep
;
1945 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1946 * the core supports IMOD, disable it.
1948 if (dwc
->imod_interval
) {
1949 dwc3_writel(dwc
->regs
, DWC3_DEV_IMOD(0), dwc
->imod_interval
);
1950 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB
);
1951 } else if (dwc3_has_imod(dwc
)) {
1952 dwc3_writel(dwc
->regs
, DWC3_DEV_IMOD(0), 0);
1956 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1957 * field instead of letting dwc3 itself calculate that automatically.
1959 * This way, we maximize the chances that we'll be able to get several
1960 * bursts of data without going through any sort of endpoint throttling.
1962 reg
= dwc3_readl(dwc
->regs
, DWC3_GRXTHRCFG
);
1963 if (dwc3_is_usb31(dwc
))
1964 reg
&= ~DWC31_GRXTHRCFG_PKTCNTSEL
;
1966 reg
&= ~DWC3_GRXTHRCFG_PKTCNTSEL
;
1968 dwc3_writel(dwc
->regs
, DWC3_GRXTHRCFG
, reg
);
1970 dwc3_gadget_setup_nump(dwc
);
1972 /* Start with SuperSpeed Default */
1973 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1976 ret
= __dwc3_gadget_ep_enable(dep
, DWC3_DEPCFG_ACTION_INIT
);
1978 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1983 ret
= __dwc3_gadget_ep_enable(dep
, DWC3_DEPCFG_ACTION_INIT
);
1985 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1989 /* begin to receive SETUP packets */
1990 dwc
->ep0state
= EP0_SETUP_PHASE
;
1991 dwc
->link_state
= DWC3_LINK_STATE_SS_DIS
;
1992 dwc3_ep0_out_start(dwc
);
1994 dwc3_gadget_enable_irq(dwc
);
1999 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
2005 static int dwc3_gadget_start(struct usb_gadget
*g
,
2006 struct usb_gadget_driver
*driver
)
2008 struct dwc3
*dwc
= gadget_to_dwc(g
);
2009 unsigned long flags
;
2013 irq
= dwc
->irq_gadget
;
2014 ret
= request_threaded_irq(irq
, dwc3_interrupt
, dwc3_thread_interrupt
,
2015 IRQF_SHARED
, "dwc3", dwc
->ev_buf
);
2017 dev_err(dwc
->dev
, "failed to request irq #%d --> %d\n",
2022 spin_lock_irqsave(&dwc
->lock
, flags
);
2023 if (dwc
->gadget_driver
) {
2024 dev_err(dwc
->dev
, "%s is already bound to %s\n",
2026 dwc
->gadget_driver
->driver
.name
);
2031 dwc
->gadget_driver
= driver
;
2033 if (pm_runtime_active(dwc
->dev
))
2034 __dwc3_gadget_start(dwc
);
2036 spin_unlock_irqrestore(&dwc
->lock
, flags
);
2041 spin_unlock_irqrestore(&dwc
->lock
, flags
);
2048 static void __dwc3_gadget_stop(struct dwc3
*dwc
)
2050 dwc3_gadget_disable_irq(dwc
);
2051 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
2052 __dwc3_gadget_ep_disable(dwc
->eps
[1]);
2055 static int dwc3_gadget_stop(struct usb_gadget
*g
)
2057 struct dwc3
*dwc
= gadget_to_dwc(g
);
2058 unsigned long flags
;
2060 spin_lock_irqsave(&dwc
->lock
, flags
);
2062 if (pm_runtime_suspended(dwc
->dev
))
2065 __dwc3_gadget_stop(dwc
);
2068 dwc
->gadget_driver
= NULL
;
2069 spin_unlock_irqrestore(&dwc
->lock
, flags
);
2071 free_irq(dwc
->irq_gadget
, dwc
->ev_buf
);
2076 static void dwc3_gadget_config_params(struct usb_gadget
*g
,
2077 struct usb_dcd_config_params
*params
)
2079 struct dwc3
*dwc
= gadget_to_dwc(g
);
2081 /* U1 Device exit Latency */
2082 if (dwc
->dis_u1_entry_quirk
)
2083 params
->bU1devExitLat
= 0;
2085 params
->bU1devExitLat
= DWC3_DEFAULT_U1_DEV_EXIT_LAT
;
2087 /* U2 Device exit Latency */
2088 if (dwc
->dis_u2_entry_quirk
)
2089 params
->bU2DevExitLat
= 0;
2091 params
->bU2DevExitLat
=
2092 cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT
);
2095 static void dwc3_gadget_set_speed(struct usb_gadget
*g
,
2096 enum usb_device_speed speed
)
2098 struct dwc3
*dwc
= gadget_to_dwc(g
);
2099 unsigned long flags
;
2102 spin_lock_irqsave(&dwc
->lock
, flags
);
2103 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2104 reg
&= ~(DWC3_DCFG_SPEED_MASK
);
2107 * WORKAROUND: DWC3 revision < 2.20a have an issue
2108 * which would cause metastability state on Run/Stop
2109 * bit if we try to force the IP to USB2-only mode.
2111 * Because of that, we cannot configure the IP to any
2112 * speed other than the SuperSpeed
2116 * STAR#9000525659: Clock Domain Crossing on DCTL in
2119 if (dwc
->revision
< DWC3_REVISION_220A
&&
2120 !dwc
->dis_metastability_quirk
) {
2121 reg
|= DWC3_DCFG_SUPERSPEED
;
2125 reg
|= DWC3_DCFG_LOWSPEED
;
2127 case USB_SPEED_FULL
:
2128 reg
|= DWC3_DCFG_FULLSPEED
;
2130 case USB_SPEED_HIGH
:
2131 reg
|= DWC3_DCFG_HIGHSPEED
;
2133 case USB_SPEED_SUPER
:
2134 reg
|= DWC3_DCFG_SUPERSPEED
;
2136 case USB_SPEED_SUPER_PLUS
:
2137 if (dwc3_is_usb31(dwc
))
2138 reg
|= DWC3_DCFG_SUPERSPEED_PLUS
;
2140 reg
|= DWC3_DCFG_SUPERSPEED
;
2143 dev_err(dwc
->dev
, "invalid speed (%d)\n", speed
);
2145 if (dwc
->revision
& DWC3_REVISION_IS_DWC31
)
2146 reg
|= DWC3_DCFG_SUPERSPEED_PLUS
;
2148 reg
|= DWC3_DCFG_SUPERSPEED
;
2151 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2153 spin_unlock_irqrestore(&dwc
->lock
, flags
);
2156 static const struct usb_gadget_ops dwc3_gadget_ops
= {
2157 .get_frame
= dwc3_gadget_get_frame
,
2158 .wakeup
= dwc3_gadget_wakeup
,
2159 .set_selfpowered
= dwc3_gadget_set_selfpowered
,
2160 .pullup
= dwc3_gadget_pullup
,
2161 .udc_start
= dwc3_gadget_start
,
2162 .udc_stop
= dwc3_gadget_stop
,
2163 .udc_set_speed
= dwc3_gadget_set_speed
,
2164 .get_config_params
= dwc3_gadget_config_params
,
2167 /* -------------------------------------------------------------------------- */
2169 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep
*dep
)
2171 struct dwc3
*dwc
= dep
->dwc
;
2173 usb_ep_set_maxpacket_limit(&dep
->endpoint
, 512);
2174 dep
->endpoint
.maxburst
= 1;
2175 dep
->endpoint
.ops
= &dwc3_gadget_ep0_ops
;
2176 if (!dep
->direction
)
2177 dwc
->gadget
.ep0
= &dep
->endpoint
;
2179 dep
->endpoint
.caps
.type_control
= true;
2184 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep
*dep
)
2186 struct dwc3
*dwc
= dep
->dwc
;
2191 mdwidth
= DWC3_MDWIDTH(dwc
->hwparams
.hwparams0
);
2192 /* MDWIDTH is represented in bits, we need it in bytes */
2195 size
= dwc3_readl(dwc
->regs
, DWC3_GTXFIFOSIZ(dep
->number
>> 1));
2196 if (dwc3_is_usb31(dwc
))
2197 size
= DWC31_GTXFIFOSIZ_TXFDEF(size
);
2199 size
= DWC3_GTXFIFOSIZ_TXFDEF(size
);
2201 /* FIFO Depth is in MDWDITH bytes. Multiply */
2204 kbytes
= size
/ 1024;
2209 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2210 * internal overhead. We don't really know how these are used,
2211 * but documentation say it exists.
2213 size
-= mdwidth
* (kbytes
+ 1);
2216 usb_ep_set_maxpacket_limit(&dep
->endpoint
, size
);
2218 dep
->endpoint
.max_streams
= 15;
2219 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
2220 list_add_tail(&dep
->endpoint
.ep_list
,
2221 &dwc
->gadget
.ep_list
);
2222 dep
->endpoint
.caps
.type_iso
= true;
2223 dep
->endpoint
.caps
.type_bulk
= true;
2224 dep
->endpoint
.caps
.type_int
= true;
2226 return dwc3_alloc_trb_pool(dep
);
2229 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep
*dep
)
2231 struct dwc3
*dwc
= dep
->dwc
;
2233 usb_ep_set_maxpacket_limit(&dep
->endpoint
, 1024);
2234 dep
->endpoint
.max_streams
= 15;
2235 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
2236 list_add_tail(&dep
->endpoint
.ep_list
,
2237 &dwc
->gadget
.ep_list
);
2238 dep
->endpoint
.caps
.type_iso
= true;
2239 dep
->endpoint
.caps
.type_bulk
= true;
2240 dep
->endpoint
.caps
.type_int
= true;
2242 return dwc3_alloc_trb_pool(dep
);
2245 static int dwc3_gadget_init_endpoint(struct dwc3
*dwc
, u8 epnum
)
2247 struct dwc3_ep
*dep
;
2248 bool direction
= epnum
& 1;
2250 u8 num
= epnum
>> 1;
2252 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
2257 dep
->number
= epnum
;
2258 dep
->direction
= direction
;
2259 dep
->regs
= dwc
->regs
+ DWC3_DEP_BASE(epnum
);
2260 dwc
->eps
[epnum
] = dep
;
2262 dep
->start_cmd_status
= 0;
2264 snprintf(dep
->name
, sizeof(dep
->name
), "ep%u%s", num
,
2265 direction
? "in" : "out");
2267 dep
->endpoint
.name
= dep
->name
;
2269 if (!(dep
->number
> 1)) {
2270 dep
->endpoint
.desc
= &dwc3_gadget_ep0_desc
;
2271 dep
->endpoint
.comp_desc
= NULL
;
2275 ret
= dwc3_gadget_init_control_endpoint(dep
);
2277 ret
= dwc3_gadget_init_in_endpoint(dep
);
2279 ret
= dwc3_gadget_init_out_endpoint(dep
);
2284 dep
->endpoint
.caps
.dir_in
= direction
;
2285 dep
->endpoint
.caps
.dir_out
= !direction
;
2287 INIT_LIST_HEAD(&dep
->pending_list
);
2288 INIT_LIST_HEAD(&dep
->started_list
);
2289 INIT_LIST_HEAD(&dep
->cancelled_list
);
2294 static int dwc3_gadget_init_endpoints(struct dwc3
*dwc
, u8 total
)
2298 INIT_LIST_HEAD(&dwc
->gadget
.ep_list
);
2300 for (epnum
= 0; epnum
< total
; epnum
++) {
2303 ret
= dwc3_gadget_init_endpoint(dwc
, epnum
);
2311 static void dwc3_gadget_free_endpoints(struct dwc3
*dwc
)
2313 struct dwc3_ep
*dep
;
2316 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
2317 dep
= dwc
->eps
[epnum
];
2321 * Physical endpoints 0 and 1 are special; they form the
2322 * bi-directional USB endpoint 0.
2324 * For those two physical endpoints, we don't allocate a TRB
2325 * pool nor do we add them the endpoints list. Due to that, we
2326 * shouldn't do these two operations otherwise we would end up
2327 * with all sorts of bugs when removing dwc3.ko.
2329 if (epnum
!= 0 && epnum
!= 1) {
2330 dwc3_free_trb_pool(dep
);
2331 list_del(&dep
->endpoint
.ep_list
);
2338 /* -------------------------------------------------------------------------- */
2340 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep
*dep
,
2341 struct dwc3_request
*req
, struct dwc3_trb
*trb
,
2342 const struct dwc3_event_depevt
*event
, int status
, int chain
)
2346 dwc3_ep_inc_deq(dep
);
2348 trace_dwc3_complete_trb(dep
, trb
);
2352 * If we're in the middle of series of chained TRBs and we
2353 * receive a short transfer along the way, DWC3 will skip
2354 * through all TRBs including the last TRB in the chain (the
2355 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2356 * bit and SW has to do it manually.
2358 * We're going to do that here to avoid problems of HW trying
2359 * to use bogus TRBs for transfers.
2361 if (chain
&& (trb
->ctrl
& DWC3_TRB_CTRL_HWO
))
2362 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
2365 * For isochronous transfers, the first TRB in a service interval must
2366 * have the Isoc-First type. Track and report its interval frame number.
2368 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
2369 (trb
->ctrl
& DWC3_TRBCTL_ISOCHRONOUS_FIRST
)) {
2370 unsigned int frame_number
;
2372 frame_number
= DWC3_TRB_CTRL_GET_SID_SOFN(trb
->ctrl
);
2373 frame_number
&= ~(dep
->interval
- 1);
2374 req
->request
.frame_number
= frame_number
;
2378 * If we're dealing with unaligned size OUT transfer, we will be left
2379 * with one TRB pending in the ring. We need to manually clear HWO bit
2383 if (req
->needs_extra_trb
&& !(trb
->ctrl
& DWC3_TRB_CTRL_CHN
)) {
2384 trb
->ctrl
&= ~DWC3_TRB_CTRL_HWO
;
2388 count
= trb
->size
& DWC3_TRB_SIZE_MASK
;
2389 req
->remaining
+= count
;
2391 if ((trb
->ctrl
& DWC3_TRB_CTRL_HWO
) && status
!= -ESHUTDOWN
)
2394 if (event
->status
& DEPEVT_STATUS_SHORT
&& !chain
)
2397 if (event
->status
& DEPEVT_STATUS_IOC
)
2403 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep
*dep
,
2404 struct dwc3_request
*req
, const struct dwc3_event_depevt
*event
,
2407 struct dwc3_trb
*trb
= &dep
->trb_pool
[dep
->trb_dequeue
];
2408 struct scatterlist
*sg
= req
->sg
;
2409 struct scatterlist
*s
;
2410 unsigned int pending
= req
->num_pending_sgs
;
2414 for_each_sg(sg
, s
, pending
, i
) {
2415 trb
= &dep
->trb_pool
[dep
->trb_dequeue
];
2417 if (trb
->ctrl
& DWC3_TRB_CTRL_HWO
)
2420 req
->sg
= sg_next(s
);
2421 req
->num_pending_sgs
--;
2423 ret
= dwc3_gadget_ep_reclaim_completed_trb(dep
, req
,
2424 trb
, event
, status
, true);
2432 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep
*dep
,
2433 struct dwc3_request
*req
, const struct dwc3_event_depevt
*event
,
2436 struct dwc3_trb
*trb
= &dep
->trb_pool
[dep
->trb_dequeue
];
2438 return dwc3_gadget_ep_reclaim_completed_trb(dep
, req
, trb
,
2439 event
, status
, false);
2442 static bool dwc3_gadget_ep_request_completed(struct dwc3_request
*req
)
2444 return req
->request
.actual
== req
->request
.length
;
2447 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep
*dep
,
2448 const struct dwc3_event_depevt
*event
,
2449 struct dwc3_request
*req
, int status
)
2453 if (req
->num_pending_sgs
)
2454 ret
= dwc3_gadget_ep_reclaim_trb_sg(dep
, req
, event
,
2457 ret
= dwc3_gadget_ep_reclaim_trb_linear(dep
, req
, event
,
2460 if (req
->needs_extra_trb
) {
2461 ret
= dwc3_gadget_ep_reclaim_trb_linear(dep
, req
, event
,
2463 req
->needs_extra_trb
= false;
2466 req
->request
.actual
= req
->request
.length
- req
->remaining
;
2468 if (!dwc3_gadget_ep_request_completed(req
) &&
2469 req
->num_pending_sgs
) {
2470 __dwc3_gadget_kick_transfer(dep
);
2474 dwc3_gadget_giveback(dep
, req
, status
);
2480 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep
*dep
,
2481 const struct dwc3_event_depevt
*event
, int status
)
2483 struct dwc3_request
*req
;
2484 struct dwc3_request
*tmp
;
2486 list_for_each_entry_safe(req
, tmp
, &dep
->started_list
, list
) {
2489 ret
= dwc3_gadget_ep_cleanup_completed_request(dep
, event
,
2496 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep
*dep
,
2497 const struct dwc3_event_depevt
*event
)
2499 dep
->frame_number
= event
->parameters
;
2502 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep
*dep
,
2503 const struct dwc3_event_depevt
*event
)
2505 struct dwc3
*dwc
= dep
->dwc
;
2506 unsigned status
= 0;
2509 dwc3_gadget_endpoint_frame_from_event(dep
, event
);
2511 if (event
->status
& DEPEVT_STATUS_BUSERR
)
2512 status
= -ECONNRESET
;
2514 if (event
->status
& DEPEVT_STATUS_MISSED_ISOC
) {
2517 if (list_empty(&dep
->started_list
))
2521 dwc3_gadget_ep_cleanup_completed_requests(dep
, event
, status
);
2524 dwc3_stop_active_transfer(dep
, true, true);
2525 dep
->flags
= DWC3_EP_ENABLED
;
2529 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2530 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2532 if (dwc
->revision
< DWC3_REVISION_183A
) {
2536 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
2539 if (!(dep
->flags
& DWC3_EP_ENABLED
))
2542 if (!list_empty(&dep
->started_list
))
2546 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2548 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2554 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep
*dep
,
2555 const struct dwc3_event_depevt
*event
)
2557 dwc3_gadget_endpoint_frame_from_event(dep
, event
);
2558 (void) __dwc3_gadget_start_isoc(dep
);
2561 static void dwc3_endpoint_interrupt(struct dwc3
*dwc
,
2562 const struct dwc3_event_depevt
*event
)
2564 struct dwc3_ep
*dep
;
2565 u8 epnum
= event
->endpoint_number
;
2568 dep
= dwc
->eps
[epnum
];
2570 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
2571 if (!(dep
->flags
& DWC3_EP_TRANSFER_STARTED
))
2574 /* Handle only EPCMDCMPLT when EP disabled */
2575 if (event
->endpoint_event
!= DWC3_DEPEVT_EPCMDCMPLT
)
2579 if (epnum
== 0 || epnum
== 1) {
2580 dwc3_ep0_interrupt(dwc
, event
);
2584 switch (event
->endpoint_event
) {
2585 case DWC3_DEPEVT_XFERINPROGRESS
:
2586 dwc3_gadget_endpoint_transfer_in_progress(dep
, event
);
2588 case DWC3_DEPEVT_XFERNOTREADY
:
2589 dwc3_gadget_endpoint_transfer_not_ready(dep
, event
);
2591 case DWC3_DEPEVT_EPCMDCMPLT
:
2592 cmd
= DEPEVT_PARAMETER_CMD(event
->parameters
);
2594 if (cmd
== DWC3_DEPCMD_ENDTRANSFER
) {
2595 dep
->flags
&= ~DWC3_EP_TRANSFER_STARTED
;
2596 dwc3_gadget_ep_cleanup_cancelled_requests(dep
);
2599 case DWC3_DEPEVT_STREAMEVT
:
2600 case DWC3_DEPEVT_XFERCOMPLETE
:
2601 case DWC3_DEPEVT_RXTXFIFOEVT
:
2606 static void dwc3_disconnect_gadget(struct dwc3
*dwc
)
2608 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->disconnect
) {
2609 spin_unlock(&dwc
->lock
);
2610 dwc
->gadget_driver
->disconnect(&dwc
->gadget
);
2611 spin_lock(&dwc
->lock
);
2615 static void dwc3_suspend_gadget(struct dwc3
*dwc
)
2617 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->suspend
) {
2618 spin_unlock(&dwc
->lock
);
2619 dwc
->gadget_driver
->suspend(&dwc
->gadget
);
2620 spin_lock(&dwc
->lock
);
2624 static void dwc3_resume_gadget(struct dwc3
*dwc
)
2626 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->resume
) {
2627 spin_unlock(&dwc
->lock
);
2628 dwc
->gadget_driver
->resume(&dwc
->gadget
);
2629 spin_lock(&dwc
->lock
);
2633 static void dwc3_reset_gadget(struct dwc3
*dwc
)
2635 if (!dwc
->gadget_driver
)
2638 if (dwc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
2639 spin_unlock(&dwc
->lock
);
2640 usb_gadget_udc_reset(&dwc
->gadget
, dwc
->gadget_driver
);
2641 spin_lock(&dwc
->lock
);
2645 static void dwc3_stop_active_transfer(struct dwc3_ep
*dep
, bool force
,
2648 struct dwc3
*dwc
= dep
->dwc
;
2649 struct dwc3_gadget_ep_cmd_params params
;
2653 if (!(dep
->flags
& DWC3_EP_TRANSFER_STARTED
))
2657 * NOTICE: We are violating what the Databook says about the
2658 * EndTransfer command. Ideally we would _always_ wait for the
2659 * EndTransfer Command Completion IRQ, but that's causing too
2660 * much trouble synchronizing between us and gadget driver.
2662 * We have discussed this with the IP Provider and it was
2663 * suggested to giveback all requests here, but give HW some
2664 * extra time to synchronize with the interconnect. We're using
2665 * an arbitrary 100us delay for that.
2667 * Note also that a similar handling was tested by Synopsys
2668 * (thanks a lot Paul) and nothing bad has come out of it.
2669 * In short, what we're doing is:
2671 * - Issue EndTransfer WITH CMDIOC bit set
2674 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2675 * supports a mode to work around the above limitation. The
2676 * software can poll the CMDACT bit in the DEPCMD register
2677 * after issuing a EndTransfer command. This mode is enabled
2678 * by writing GUCTL2[14]. This polling is already done in the
2679 * dwc3_send_gadget_ep_cmd() function so if the mode is
2680 * enabled, the EndTransfer command will have completed upon
2681 * returning from this function and we don't need to delay for
2684 * This mode is NOT available on the DWC_usb31 IP.
2687 cmd
= DWC3_DEPCMD_ENDTRANSFER
;
2688 cmd
|= force
? DWC3_DEPCMD_HIPRI_FORCERM
: 0;
2689 cmd
|= interrupt
? DWC3_DEPCMD_CMDIOC
: 0;
2690 cmd
|= DWC3_DEPCMD_PARAM(dep
->resource_index
);
2691 memset(¶ms
, 0, sizeof(params
));
2692 ret
= dwc3_send_gadget_ep_cmd(dep
, cmd
, ¶ms
);
2694 dep
->resource_index
= 0;
2696 if (dwc3_is_usb31(dwc
) || dwc
->revision
< DWC3_REVISION_310A
)
2700 static void dwc3_clear_stall_all_ep(struct dwc3
*dwc
)
2704 for (epnum
= 1; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
2705 struct dwc3_ep
*dep
;
2708 dep
= dwc
->eps
[epnum
];
2712 if (!(dep
->flags
& DWC3_EP_STALL
))
2715 dep
->flags
&= ~DWC3_EP_STALL
;
2717 ret
= dwc3_send_clear_stall_ep_cmd(dep
);
2722 static void dwc3_gadget_disconnect_interrupt(struct dwc3
*dwc
)
2726 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2727 reg
&= ~DWC3_DCTL_INITU1ENA
;
2728 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2730 reg
&= ~DWC3_DCTL_INITU2ENA
;
2731 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2733 dwc3_disconnect_gadget(dwc
);
2735 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2736 dwc
->setup_packet_pending
= false;
2737 usb_gadget_set_state(&dwc
->gadget
, USB_STATE_NOTATTACHED
);
2739 dwc
->connected
= false;
2742 static void dwc3_gadget_reset_interrupt(struct dwc3
*dwc
)
2746 dwc
->connected
= true;
2749 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2750 * would cause a missing Disconnect Event if there's a
2751 * pending Setup Packet in the FIFO.
2753 * There's no suggested workaround on the official Bug
2754 * report, which states that "unless the driver/application
2755 * is doing any special handling of a disconnect event,
2756 * there is no functional issue".
2758 * Unfortunately, it turns out that we _do_ some special
2759 * handling of a disconnect event, namely complete all
2760 * pending transfers, notify gadget driver of the
2761 * disconnection, and so on.
2763 * Our suggested workaround is to follow the Disconnect
2764 * Event steps here, instead, based on a setup_packet_pending
2765 * flag. Such flag gets set whenever we have a SETUP_PENDING
2766 * status for EP0 TRBs and gets cleared on XferComplete for the
2771 * STAR#9000466709: RTL: Device : Disconnect event not
2772 * generated if setup packet pending in FIFO
2774 if (dwc
->revision
< DWC3_REVISION_188A
) {
2775 if (dwc
->setup_packet_pending
)
2776 dwc3_gadget_disconnect_interrupt(dwc
);
2779 dwc3_reset_gadget(dwc
);
2781 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2782 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
2783 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2784 dwc
->test_mode
= false;
2785 dwc3_clear_stall_all_ep(dwc
);
2787 /* Reset device address to zero */
2788 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2789 reg
&= ~(DWC3_DCFG_DEVADDR_MASK
);
2790 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2793 static void dwc3_gadget_conndone_interrupt(struct dwc3
*dwc
)
2795 struct dwc3_ep
*dep
;
2800 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
2801 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
2805 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2806 * each time on Connect Done.
2808 * Currently we always use the reset value. If any platform
2809 * wants to set this to a different value, we need to add a
2810 * setting and update GCTL.RAMCLKSEL here.
2814 case DWC3_DSTS_SUPERSPEED_PLUS
:
2815 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
2816 dwc
->gadget
.ep0
->maxpacket
= 512;
2817 dwc
->gadget
.speed
= USB_SPEED_SUPER_PLUS
;
2819 case DWC3_DSTS_SUPERSPEED
:
2821 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2822 * would cause a missing USB3 Reset event.
2824 * In such situations, we should force a USB3 Reset
2825 * event by calling our dwc3_gadget_reset_interrupt()
2830 * STAR#9000483510: RTL: SS : USB3 reset event may
2831 * not be generated always when the link enters poll
2833 if (dwc
->revision
< DWC3_REVISION_190A
)
2834 dwc3_gadget_reset_interrupt(dwc
);
2836 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
2837 dwc
->gadget
.ep0
->maxpacket
= 512;
2838 dwc
->gadget
.speed
= USB_SPEED_SUPER
;
2840 case DWC3_DSTS_HIGHSPEED
:
2841 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
2842 dwc
->gadget
.ep0
->maxpacket
= 64;
2843 dwc
->gadget
.speed
= USB_SPEED_HIGH
;
2845 case DWC3_DSTS_FULLSPEED
:
2846 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
2847 dwc
->gadget
.ep0
->maxpacket
= 64;
2848 dwc
->gadget
.speed
= USB_SPEED_FULL
;
2850 case DWC3_DSTS_LOWSPEED
:
2851 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(8);
2852 dwc
->gadget
.ep0
->maxpacket
= 8;
2853 dwc
->gadget
.speed
= USB_SPEED_LOW
;
2857 dwc
->eps
[1]->endpoint
.maxpacket
= dwc
->gadget
.ep0
->maxpacket
;
2859 /* Enable USB2 LPM Capability */
2861 if ((dwc
->revision
> DWC3_REVISION_194A
) &&
2862 (speed
!= DWC3_DSTS_SUPERSPEED
) &&
2863 (speed
!= DWC3_DSTS_SUPERSPEED_PLUS
)) {
2864 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2865 reg
|= DWC3_DCFG_LPM_CAP
;
2866 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2868 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2869 reg
&= ~(DWC3_DCTL_HIRD_THRES_MASK
| DWC3_DCTL_L1_HIBER_EN
);
2871 reg
|= DWC3_DCTL_HIRD_THRES(dwc
->hird_threshold
);
2874 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2875 * DCFG.LPMCap is set, core responses with an ACK and the
2876 * BESL value in the LPM token is less than or equal to LPM
2879 WARN_ONCE(dwc
->revision
< DWC3_REVISION_240A
2880 && dwc
->has_lpm_erratum
,
2881 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2883 if (dwc
->has_lpm_erratum
&& dwc
->revision
>= DWC3_REVISION_240A
)
2884 reg
|= DWC3_DCTL_NYET_THRES(dwc
->lpm_nyet_threshold
);
2886 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2888 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2889 reg
&= ~DWC3_DCTL_HIRD_THRES_MASK
;
2890 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2894 ret
= __dwc3_gadget_ep_enable(dep
, DWC3_DEPCFG_ACTION_MODIFY
);
2896 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
2901 ret
= __dwc3_gadget_ep_enable(dep
, DWC3_DEPCFG_ACTION_MODIFY
);
2903 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
2908 * Configure PHY via GUSB3PIPECTLn if required.
2910 * Update GTXFIFOSIZn
2912 * In both cases reset values should be sufficient.
2916 static void dwc3_gadget_wakeup_interrupt(struct dwc3
*dwc
)
2919 * TODO take core out of low power mode when that's
2923 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->resume
) {
2924 spin_unlock(&dwc
->lock
);
2925 dwc
->gadget_driver
->resume(&dwc
->gadget
);
2926 spin_lock(&dwc
->lock
);
2930 static void dwc3_gadget_linksts_change_interrupt(struct dwc3
*dwc
,
2931 unsigned int evtinfo
)
2933 enum dwc3_link_state next
= evtinfo
& DWC3_LINK_STATE_MASK
;
2934 unsigned int pwropt
;
2937 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2938 * Hibernation mode enabled which would show up when device detects
2939 * host-initiated U3 exit.
2941 * In that case, device will generate a Link State Change Interrupt
2942 * from U3 to RESUME which is only necessary if Hibernation is
2945 * There are no functional changes due to such spurious event and we
2946 * just need to ignore it.
2950 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2953 pwropt
= DWC3_GHWPARAMS1_EN_PWROPT(dwc
->hwparams
.hwparams1
);
2954 if ((dwc
->revision
< DWC3_REVISION_250A
) &&
2955 (pwropt
!= DWC3_GHWPARAMS1_EN_PWROPT_HIB
)) {
2956 if ((dwc
->link_state
== DWC3_LINK_STATE_U3
) &&
2957 (next
== DWC3_LINK_STATE_RESUME
)) {
2963 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2964 * on the link partner, the USB session might do multiple entry/exit
2965 * of low power states before a transfer takes place.
2967 * Due to this problem, we might experience lower throughput. The
2968 * suggested workaround is to disable DCTL[12:9] bits if we're
2969 * transitioning from U1/U2 to U0 and enable those bits again
2970 * after a transfer completes and there are no pending transfers
2971 * on any of the enabled endpoints.
2973 * This is the first half of that workaround.
2977 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2978 * core send LGO_Ux entering U0
2980 if (dwc
->revision
< DWC3_REVISION_183A
) {
2981 if (next
== DWC3_LINK_STATE_U0
) {
2985 switch (dwc
->link_state
) {
2986 case DWC3_LINK_STATE_U1
:
2987 case DWC3_LINK_STATE_U2
:
2988 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2989 u1u2
= reg
& (DWC3_DCTL_INITU2ENA
2990 | DWC3_DCTL_ACCEPTU2ENA
2991 | DWC3_DCTL_INITU1ENA
2992 | DWC3_DCTL_ACCEPTU1ENA
);
2995 dwc
->u1u2
= reg
& u1u2
;
2999 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
3009 case DWC3_LINK_STATE_U1
:
3010 if (dwc
->speed
== USB_SPEED_SUPER
)
3011 dwc3_suspend_gadget(dwc
);
3013 case DWC3_LINK_STATE_U2
:
3014 case DWC3_LINK_STATE_U3
:
3015 dwc3_suspend_gadget(dwc
);
3017 case DWC3_LINK_STATE_RESUME
:
3018 dwc3_resume_gadget(dwc
);
3025 dwc
->link_state
= next
;
3028 static void dwc3_gadget_suspend_interrupt(struct dwc3
*dwc
,
3029 unsigned int evtinfo
)
3031 enum dwc3_link_state next
= evtinfo
& DWC3_LINK_STATE_MASK
;
3033 if (dwc
->link_state
!= next
&& next
== DWC3_LINK_STATE_U3
)
3034 dwc3_suspend_gadget(dwc
);
3036 dwc
->link_state
= next
;
3039 static void dwc3_gadget_hibernation_interrupt(struct dwc3
*dwc
,
3040 unsigned int evtinfo
)
3042 unsigned int is_ss
= evtinfo
& BIT(4);
3045 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3046 * have a known issue which can cause USB CV TD.9.23 to fail
3049 * Because of this issue, core could generate bogus hibernation
3050 * events which SW needs to ignore.
3054 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3055 * Device Fallback from SuperSpeed
3057 if (is_ss
^ (dwc
->speed
== USB_SPEED_SUPER
))
3060 /* enter hibernation here */
3063 static void dwc3_gadget_interrupt(struct dwc3
*dwc
,
3064 const struct dwc3_event_devt
*event
)
3066 switch (event
->type
) {
3067 case DWC3_DEVICE_EVENT_DISCONNECT
:
3068 dwc3_gadget_disconnect_interrupt(dwc
);
3070 case DWC3_DEVICE_EVENT_RESET
:
3071 dwc3_gadget_reset_interrupt(dwc
);
3073 case DWC3_DEVICE_EVENT_CONNECT_DONE
:
3074 dwc3_gadget_conndone_interrupt(dwc
);
3076 case DWC3_DEVICE_EVENT_WAKEUP
:
3077 dwc3_gadget_wakeup_interrupt(dwc
);
3079 case DWC3_DEVICE_EVENT_HIBER_REQ
:
3080 if (dev_WARN_ONCE(dwc
->dev
, !dwc
->has_hibernation
,
3081 "unexpected hibernation event\n"))
3084 dwc3_gadget_hibernation_interrupt(dwc
, event
->event_info
);
3086 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE
:
3087 dwc3_gadget_linksts_change_interrupt(dwc
, event
->event_info
);
3089 case DWC3_DEVICE_EVENT_EOPF
:
3090 /* It changed to be suspend event for version 2.30a and above */
3091 if (dwc
->revision
>= DWC3_REVISION_230A
) {
3093 * Ignore suspend event until the gadget enters into
3094 * USB_STATE_CONFIGURED state.
3096 if (dwc
->gadget
.state
>= USB_STATE_CONFIGURED
)
3097 dwc3_gadget_suspend_interrupt(dwc
,
3101 case DWC3_DEVICE_EVENT_SOF
:
3102 case DWC3_DEVICE_EVENT_ERRATIC_ERROR
:
3103 case DWC3_DEVICE_EVENT_CMD_CMPL
:
3104 case DWC3_DEVICE_EVENT_OVERFLOW
:
3107 dev_WARN(dwc
->dev
, "UNKNOWN IRQ %d\n", event
->type
);
3111 static void dwc3_process_event_entry(struct dwc3
*dwc
,
3112 const union dwc3_event
*event
)
3114 trace_dwc3_event(event
->raw
, dwc
);
3116 if (!event
->type
.is_devspec
)
3117 dwc3_endpoint_interrupt(dwc
, &event
->depevt
);
3118 else if (event
->type
.type
== DWC3_EVENT_TYPE_DEV
)
3119 dwc3_gadget_interrupt(dwc
, &event
->devt
);
3121 dev_err(dwc
->dev
, "UNKNOWN IRQ type %d\n", event
->raw
);
3124 static irqreturn_t
dwc3_process_event_buf(struct dwc3_event_buffer
*evt
)
3126 struct dwc3
*dwc
= evt
->dwc
;
3127 irqreturn_t ret
= IRQ_NONE
;
3133 if (!(evt
->flags
& DWC3_EVENT_PENDING
))
3137 union dwc3_event event
;
3139 event
.raw
= *(u32
*) (evt
->cache
+ evt
->lpos
);
3141 dwc3_process_event_entry(dwc
, &event
);
3144 * FIXME we wrap around correctly to the next entry as
3145 * almost all entries are 4 bytes in size. There is one
3146 * entry which has 12 bytes which is a regular entry
3147 * followed by 8 bytes data. ATM I don't know how
3148 * things are organized if we get next to the a
3149 * boundary so I worry about that once we try to handle
3152 evt
->lpos
= (evt
->lpos
+ 4) % evt
->length
;
3157 evt
->flags
&= ~DWC3_EVENT_PENDING
;
3160 /* Unmask interrupt */
3161 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTSIZ(0));
3162 reg
&= ~DWC3_GEVNTSIZ_INTMASK
;
3163 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), reg
);
3165 if (dwc
->imod_interval
) {
3166 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB
);
3167 dwc3_writel(dwc
->regs
, DWC3_DEV_IMOD(0), dwc
->imod_interval
);
3173 static irqreturn_t
dwc3_thread_interrupt(int irq
, void *_evt
)
3175 struct dwc3_event_buffer
*evt
= _evt
;
3176 struct dwc3
*dwc
= evt
->dwc
;
3177 unsigned long flags
;
3178 irqreturn_t ret
= IRQ_NONE
;
3180 spin_lock_irqsave(&dwc
->lock
, flags
);
3181 ret
= dwc3_process_event_buf(evt
);
3182 spin_unlock_irqrestore(&dwc
->lock
, flags
);
3187 static irqreturn_t
dwc3_check_event_buf(struct dwc3_event_buffer
*evt
)
3189 struct dwc3
*dwc
= evt
->dwc
;
3194 if (pm_runtime_suspended(dwc
->dev
)) {
3195 pm_runtime_get(dwc
->dev
);
3196 disable_irq_nosync(dwc
->irq_gadget
);
3197 dwc
->pending_events
= true;
3202 * With PCIe legacy interrupt, test shows that top-half irq handler can
3203 * be called again after HW interrupt deassertion. Check if bottom-half
3204 * irq event handler completes before caching new event to prevent
3207 if (evt
->flags
& DWC3_EVENT_PENDING
)
3210 count
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(0));
3211 count
&= DWC3_GEVNTCOUNT_MASK
;
3216 evt
->flags
|= DWC3_EVENT_PENDING
;
3218 /* Mask interrupt */
3219 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTSIZ(0));
3220 reg
|= DWC3_GEVNTSIZ_INTMASK
;
3221 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), reg
);
3223 amount
= min(count
, evt
->length
- evt
->lpos
);
3224 memcpy(evt
->cache
+ evt
->lpos
, evt
->buf
+ evt
->lpos
, amount
);
3227 memcpy(evt
->cache
, evt
->buf
, count
- amount
);
3229 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), count
);
3231 return IRQ_WAKE_THREAD
;
3234 static irqreturn_t
dwc3_interrupt(int irq
, void *_evt
)
3236 struct dwc3_event_buffer
*evt
= _evt
;
3238 return dwc3_check_event_buf(evt
);
3241 static int dwc3_gadget_get_irq(struct dwc3
*dwc
)
3243 struct platform_device
*dwc3_pdev
= to_platform_device(dwc
->dev
);
3246 irq
= platform_get_irq_byname(dwc3_pdev
, "peripheral");
3250 if (irq
== -EPROBE_DEFER
)
3253 irq
= platform_get_irq_byname(dwc3_pdev
, "dwc_usb3");
3257 if (irq
== -EPROBE_DEFER
)
3260 irq
= platform_get_irq(dwc3_pdev
, 0);
3264 if (irq
!= -EPROBE_DEFER
)
3265 dev_err(dwc
->dev
, "missing peripheral IRQ\n");
3275 * dwc3_gadget_init - initializes gadget related registers
3276 * @dwc: pointer to our controller context structure
3278 * Returns 0 on success otherwise negative errno.
3280 int dwc3_gadget_init(struct dwc3
*dwc
)
3285 irq
= dwc3_gadget_get_irq(dwc
);
3291 dwc
->irq_gadget
= irq
;
3293 dwc
->ep0_trb
= dma_alloc_coherent(dwc
->sysdev
,
3294 sizeof(*dwc
->ep0_trb
) * 2,
3295 &dwc
->ep0_trb_addr
, GFP_KERNEL
);
3296 if (!dwc
->ep0_trb
) {
3297 dev_err(dwc
->dev
, "failed to allocate ep0 trb\n");
3302 dwc
->setup_buf
= kzalloc(DWC3_EP0_SETUP_SIZE
, GFP_KERNEL
);
3303 if (!dwc
->setup_buf
) {
3308 dwc
->bounce
= dma_alloc_coherent(dwc
->sysdev
, DWC3_BOUNCE_SIZE
,
3309 &dwc
->bounce_addr
, GFP_KERNEL
);
3315 init_completion(&dwc
->ep0_in_setup
);
3317 dwc
->gadget
.ops
= &dwc3_gadget_ops
;
3318 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
3319 dwc
->gadget
.sg_supported
= true;
3320 dwc
->gadget
.name
= "dwc3-gadget";
3321 dwc
->gadget
.is_otg
= dwc
->dr_mode
== USB_DR_MODE_OTG
;
3322 dwc
->gadget
.lpm_capable
= true;
3325 * FIXME We might be setting max_speed to <SUPER, however versions
3326 * <2.20a of dwc3 have an issue with metastability (documented
3327 * elsewhere in this driver) which tells us we can't set max speed to
3328 * anything lower than SUPER.
3330 * Because gadget.max_speed is only used by composite.c and function
3331 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3332 * to happen so we avoid sending SuperSpeed Capability descriptor
3333 * together with our BOS descriptor as that could confuse host into
3334 * thinking we can handle super speed.
3336 * Note that, in fact, we won't even support GetBOS requests when speed
3337 * is less than super speed because we don't have means, yet, to tell
3338 * composite.c that we are USB 2.0 + LPM ECN.
3340 if (dwc
->revision
< DWC3_REVISION_220A
&&
3341 !dwc
->dis_metastability_quirk
)
3342 dev_info(dwc
->dev
, "changing max_speed on rev %08x\n",
3345 dwc
->gadget
.max_speed
= dwc
->maximum_speed
;
3348 * REVISIT: Here we should clear all pending IRQs to be
3349 * sure we're starting from a well known location.
3352 ret
= dwc3_gadget_init_endpoints(dwc
, dwc
->num_eps
);
3356 ret
= usb_add_gadget_udc(dwc
->dev
, &dwc
->gadget
);
3358 dev_err(dwc
->dev
, "failed to register udc\n");
3362 dwc3_gadget_set_speed(&dwc
->gadget
, dwc
->maximum_speed
);
3367 dwc3_gadget_free_endpoints(dwc
);
3370 dma_free_coherent(dwc
->sysdev
, DWC3_BOUNCE_SIZE
, dwc
->bounce
,
3374 kfree(dwc
->setup_buf
);
3377 dma_free_coherent(dwc
->sysdev
, sizeof(*dwc
->ep0_trb
) * 2,
3378 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
3384 /* -------------------------------------------------------------------------- */
3386 void dwc3_gadget_exit(struct dwc3
*dwc
)
3388 usb_del_gadget_udc(&dwc
->gadget
);
3389 dwc3_gadget_free_endpoints(dwc
);
3390 dma_free_coherent(dwc
->sysdev
, DWC3_BOUNCE_SIZE
, dwc
->bounce
,
3392 kfree(dwc
->setup_buf
);
3393 dma_free_coherent(dwc
->sysdev
, sizeof(*dwc
->ep0_trb
) * 2,
3394 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
3397 int dwc3_gadget_suspend(struct dwc3
*dwc
)
3399 if (!dwc
->gadget_driver
)
3402 dwc3_gadget_run_stop(dwc
, false, false);
3403 dwc3_disconnect_gadget(dwc
);
3404 __dwc3_gadget_stop(dwc
);
3409 int dwc3_gadget_resume(struct dwc3
*dwc
)
3413 if (!dwc
->gadget_driver
)
3416 ret
= __dwc3_gadget_start(dwc
);
3420 ret
= dwc3_gadget_run_stop(dwc
, true, false);
3427 __dwc3_gadget_stop(dwc
);
3433 void dwc3_gadget_process_pending_events(struct dwc3
*dwc
)
3435 if (dwc
->pending_events
) {
3436 dwc3_interrupt(dwc
->irq_gadget
, dwc
->ev_buf
);
3437 dwc
->pending_events
= false;
3438 enable_irq(dwc
->irq_gadget
);