2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/interrupt.h>
27 #include <linux/list.h>
28 #include <linux/dma-mapping.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
47 int dwc3_gadget_set_test_mode(struct dwc3
*dwc
, int mode
)
51 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
52 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
66 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
72 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
78 int dwc3_gadget_get_link_state(struct dwc3
*dwc
)
82 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
84 return DWC3_DSTS_USBLNKST(reg
);
88 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
92 * Caller should take care of locking. This function will
93 * return 0 on success or -ETIMEDOUT.
95 int dwc3_gadget_set_link_state(struct dwc3
*dwc
, enum dwc3_link_state state
)
101 * Wait until device controller is ready. Only applies to 1.94a and
104 if (dwc
->revision
>= DWC3_REVISION_194A
) {
106 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
107 if (reg
& DWC3_DSTS_DCNRD
)
117 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
118 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
120 /* set requested state */
121 reg
|= DWC3_DCTL_ULSTCHNGREQ(state
);
122 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
128 if (dwc
->revision
>= DWC3_REVISION_194A
)
131 /* wait for a change in DSTS */
134 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
136 if (DWC3_DSTS_USBLNKST(reg
) == state
)
142 dwc3_trace(trace_dwc3_gadget
,
143 "link state change request timed out");
148 static void dwc3_ep_inc_enq(struct dwc3_ep
*dep
)
151 dep
->trb_enqueue
%= DWC3_TRB_NUM
;
154 static void dwc3_ep_inc_deq(struct dwc3_ep
*dep
)
157 dep
->trb_dequeue
%= DWC3_TRB_NUM
;
160 static int dwc3_ep_is_last_trb(unsigned int index
)
162 return index
== DWC3_TRB_NUM
- 1;
165 void dwc3_gadget_giveback(struct dwc3_ep
*dep
, struct dwc3_request
*req
,
168 struct dwc3
*dwc
= dep
->dwc
;
174 dwc3_ep_inc_deq(dep
);
176 * Skip LINK TRB. We can't use req->trb and check for
177 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
178 * just completed (not the LINK TRB).
180 if (dwc3_ep_is_last_trb(dep
->trb_dequeue
))
181 dwc3_ep_inc_deq(dep
);
182 } while(++i
< req
->request
.num_mapped_sgs
);
183 req
->started
= false;
185 list_del(&req
->list
);
188 if (req
->request
.status
== -EINPROGRESS
)
189 req
->request
.status
= status
;
191 if (dwc
->ep0_bounced
&& dep
->number
== 0)
192 dwc
->ep0_bounced
= false;
194 usb_gadget_unmap_request(&dwc
->gadget
, &req
->request
,
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 int dwc3_send_gadget_generic_command(struct dwc3
*dwc
, unsigned cmd
, u32 param
)
209 trace_dwc3_gadget_generic_cmd(cmd
, param
);
211 dwc3_writel(dwc
->regs
, DWC3_DGCMDPAR
, param
);
212 dwc3_writel(dwc
->regs
, DWC3_DGCMD
, cmd
| DWC3_DGCMD_CMDACT
);
215 reg
= dwc3_readl(dwc
->regs
, DWC3_DGCMD
);
216 if (!(reg
& DWC3_DGCMD_CMDACT
)) {
217 dwc3_trace(trace_dwc3_gadget
,
218 "Command Complete --> %d",
219 DWC3_DGCMD_STATUS(reg
));
220 if (DWC3_DGCMD_STATUS(reg
))
226 * We can't sleep here, because it's also called from
231 dwc3_trace(trace_dwc3_gadget
,
232 "Command Timed Out");
239 static int __dwc3_gadget_wakeup(struct dwc3
*dwc
);
241 int dwc3_send_gadget_ep_cmd(struct dwc3
*dwc
, unsigned ep
,
242 unsigned cmd
, struct dwc3_gadget_ep_cmd_params
*params
)
244 struct dwc3_ep
*dep
= dwc
->eps
[ep
];
251 trace_dwc3_gadget_ep_cmd(dep
, cmd
, params
);
254 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
255 * we're issuing an endpoint command, we must check if
256 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
258 * We will also set SUSPHY bit to what it was before returning as stated
259 * by the same section on Synopsys databook.
261 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
262 if (unlikely(reg
& DWC3_GUSB2PHYCFG_SUSPHY
)) {
264 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
265 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
268 if (cmd
== DWC3_DEPCMD_STARTTRANSFER
) {
271 needs_wakeup
= (dwc
->link_state
== DWC3_LINK_STATE_U1
||
272 dwc
->link_state
== DWC3_LINK_STATE_U2
||
273 dwc
->link_state
== DWC3_LINK_STATE_U3
);
275 if (unlikely(needs_wakeup
)) {
276 ret
= __dwc3_gadget_wakeup(dwc
);
277 dev_WARN_ONCE(dwc
->dev
, ret
, "wakeup failed --> %d\n",
282 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR0(ep
), params
->param0
);
283 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR1(ep
), params
->param1
);
284 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR2(ep
), params
->param2
);
286 dwc3_writel(dwc
->regs
, DWC3_DEPCMD(ep
), cmd
| DWC3_DEPCMD_CMDACT
);
288 reg
= dwc3_readl(dwc
->regs
, DWC3_DEPCMD(ep
));
289 if (!(reg
& DWC3_DEPCMD_CMDACT
)) {
290 int cmd_status
= DWC3_DEPCMD_STATUS(reg
);
292 dwc3_trace(trace_dwc3_gadget
,
293 "Command Complete --> %d",
296 switch (cmd_status
) {
300 case DEPEVT_TRANSFER_NO_RESOURCE
:
301 dwc3_trace(trace_dwc3_gadget
, "%s: no resource available");
304 case DEPEVT_TRANSFER_BUS_EXPIRY
:
306 * SW issues START TRANSFER command to
307 * isochronous ep with future frame interval. If
308 * future interval time has already passed when
309 * core receives the command, it will respond
310 * with an error status of 'Bus Expiry'.
312 * Instead of always returning -EINVAL, let's
313 * give a hint to the gadget driver that this is
314 * the case by returning -EAGAIN.
316 dwc3_trace(trace_dwc3_gadget
, "%s: bus expiry");
320 dev_WARN(dwc
->dev
, "UNKNOWN cmd status\n");
327 * We can't sleep here, because it is also called from
332 dwc3_trace(trace_dwc3_gadget
,
333 "Command Timed Out");
341 if (unlikely(susphy
)) {
342 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
343 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
344 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
350 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep
*dep
)
352 struct dwc3
*dwc
= dep
->dwc
;
353 struct dwc3_gadget_ep_cmd_params params
;
354 u32 cmd
= DWC3_DEPCMD_CLEARSTALL
;
357 * As of core revision 2.60a the recommended programming model
358 * is to set the ClearPendIN bit when issuing a Clear Stall EP
359 * command for IN endpoints. This is to prevent an issue where
360 * some (non-compliant) hosts may not send ACK TPs for pending
361 * IN transfers due to a mishandled error condition. Synopsys
364 if (dep
->direction
&& (dwc
->revision
>= DWC3_REVISION_260A
))
365 cmd
|= DWC3_DEPCMD_CLEARPENDIN
;
367 memset(¶ms
, 0, sizeof(params
));
369 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
372 static dma_addr_t
dwc3_trb_dma_offset(struct dwc3_ep
*dep
,
373 struct dwc3_trb
*trb
)
375 u32 offset
= (char *) trb
- (char *) dep
->trb_pool
;
377 return dep
->trb_pool_dma
+ offset
;
380 static int dwc3_alloc_trb_pool(struct dwc3_ep
*dep
)
382 struct dwc3
*dwc
= dep
->dwc
;
387 dep
->trb_pool
= dma_alloc_coherent(dwc
->dev
,
388 sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
389 &dep
->trb_pool_dma
, GFP_KERNEL
);
390 if (!dep
->trb_pool
) {
391 dev_err(dep
->dwc
->dev
, "failed to allocate trb pool for %s\n",
399 static void dwc3_free_trb_pool(struct dwc3_ep
*dep
)
401 struct dwc3
*dwc
= dep
->dwc
;
403 dma_free_coherent(dwc
->dev
, sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
404 dep
->trb_pool
, dep
->trb_pool_dma
);
406 dep
->trb_pool
= NULL
;
407 dep
->trb_pool_dma
= 0;
410 static int dwc3_gadget_set_xfer_resource(struct dwc3
*dwc
, struct dwc3_ep
*dep
);
413 * dwc3_gadget_start_config - Configure EP resources
414 * @dwc: pointer to our controller context structure
415 * @dep: endpoint that is being enabled
417 * The assignment of transfer resources cannot perfectly follow the
418 * data book due to the fact that the controller driver does not have
419 * all knowledge of the configuration in advance. It is given this
420 * information piecemeal by the composite gadget framework after every
421 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
422 * programming model in this scenario can cause errors. For two
425 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
426 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
427 * multiple interfaces.
429 * 2) The databook does not mention doing more DEPXFERCFG for new
430 * endpoint on alt setting (8.1.6).
432 * The following simplified method is used instead:
434 * All hardware endpoints can be assigned a transfer resource and this
435 * setting will stay persistent until either a core reset or
436 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
437 * do DEPXFERCFG for every hardware endpoint as well. We are
438 * guaranteed that there are as many transfer resources as endpoints.
440 * This function is called for each endpoint when it is being enabled
441 * but is triggered only when called for EP0-out, which always happens
442 * first, and which should only happen in one of the above conditions.
444 static int dwc3_gadget_start_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
446 struct dwc3_gadget_ep_cmd_params params
;
454 memset(¶ms
, 0x00, sizeof(params
));
455 cmd
= DWC3_DEPCMD_DEPSTARTCFG
;
457 ret
= dwc3_send_gadget_ep_cmd(dwc
, 0, cmd
, ¶ms
);
461 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
462 struct dwc3_ep
*dep
= dwc
->eps
[i
];
467 ret
= dwc3_gadget_set_xfer_resource(dwc
, dep
);
475 static int dwc3_gadget_set_ep_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
476 const struct usb_endpoint_descriptor
*desc
,
477 const struct usb_ss_ep_comp_descriptor
*comp_desc
,
478 bool ignore
, bool restore
)
480 struct dwc3_gadget_ep_cmd_params params
;
482 memset(¶ms
, 0x00, sizeof(params
));
484 params
.param0
= DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc
))
485 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc
));
487 /* Burst size is only needed in SuperSpeed mode */
488 if (dwc
->gadget
.speed
>= USB_SPEED_SUPER
) {
489 u32 burst
= dep
->endpoint
.maxburst
;
494 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
495 nump
= DWC3_DCFG_NUMP(reg
);
496 nump
= max(nump
, burst
);
497 reg
&= ~DWC3_DCFG_NUMP_MASK
;
498 reg
|= nump
<< DWC3_DCFG_NUMP_SHIFT
;
499 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
501 params
.param0
|= DWC3_DEPCFG_BURST_SIZE(burst
- 1);
505 params
.param0
|= DWC3_DEPCFG_IGN_SEQ_NUM
;
508 params
.param0
|= DWC3_DEPCFG_ACTION_RESTORE
;
509 params
.param2
|= dep
->saved_state
;
512 params
.param1
= DWC3_DEPCFG_XFER_COMPLETE_EN
513 | DWC3_DEPCFG_XFER_NOT_READY_EN
;
515 if (usb_ss_max_streams(comp_desc
) && usb_endpoint_xfer_bulk(desc
)) {
516 params
.param1
|= DWC3_DEPCFG_STREAM_CAPABLE
517 | DWC3_DEPCFG_STREAM_EVENT_EN
;
518 dep
->stream_capable
= true;
521 if (!usb_endpoint_xfer_control(desc
))
522 params
.param1
|= DWC3_DEPCFG_XFER_IN_PROGRESS_EN
;
525 * We are doing 1:1 mapping for endpoints, meaning
526 * Physical Endpoints 2 maps to Logical Endpoint 2 and
527 * so on. We consider the direction bit as part of the physical
528 * endpoint number. So USB endpoint 0x81 is 0x03.
530 params
.param1
|= DWC3_DEPCFG_EP_NUMBER(dep
->number
);
533 * We must use the lower 16 TX FIFOs even though
537 params
.param0
|= DWC3_DEPCFG_FIFO_NUMBER(dep
->number
>> 1);
539 if (desc
->bInterval
) {
540 params
.param1
|= DWC3_DEPCFG_BINTERVAL_M1(desc
->bInterval
- 1);
541 dep
->interval
= 1 << (desc
->bInterval
- 1);
544 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
545 DWC3_DEPCMD_SETEPCONFIG
, ¶ms
);
548 static int dwc3_gadget_set_xfer_resource(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
550 struct dwc3_gadget_ep_cmd_params params
;
552 memset(¶ms
, 0x00, sizeof(params
));
554 params
.param0
= DWC3_DEPXFERCFG_NUM_XFER_RES(1);
556 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
557 DWC3_DEPCMD_SETTRANSFRESOURCE
, ¶ms
);
561 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
562 * @dep: endpoint to be initialized
563 * @desc: USB Endpoint Descriptor
565 * Caller should take care of locking
567 static int __dwc3_gadget_ep_enable(struct dwc3_ep
*dep
,
568 const struct usb_endpoint_descriptor
*desc
,
569 const struct usb_ss_ep_comp_descriptor
*comp_desc
,
570 bool ignore
, bool restore
)
572 struct dwc3
*dwc
= dep
->dwc
;
576 dwc3_trace(trace_dwc3_gadget
, "Enabling %s", dep
->name
);
578 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
579 ret
= dwc3_gadget_start_config(dwc
, dep
);
584 ret
= dwc3_gadget_set_ep_config(dwc
, dep
, desc
, comp_desc
, ignore
,
589 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
590 struct dwc3_trb
*trb_st_hw
;
591 struct dwc3_trb
*trb_link
;
593 dep
->endpoint
.desc
= desc
;
594 dep
->comp_desc
= comp_desc
;
595 dep
->type
= usb_endpoint_type(desc
);
596 dep
->flags
|= DWC3_EP_ENABLED
;
598 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
599 reg
|= DWC3_DALEPENA_EP(dep
->number
);
600 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
602 if (usb_endpoint_xfer_control(desc
))
605 /* Link TRB. The HWO bit is never reset */
606 trb_st_hw
= &dep
->trb_pool
[0];
608 trb_link
= &dep
->trb_pool
[DWC3_TRB_NUM
- 1];
609 memset(trb_link
, 0, sizeof(*trb_link
));
611 trb_link
->bpl
= lower_32_bits(dwc3_trb_dma_offset(dep
, trb_st_hw
));
612 trb_link
->bph
= upper_32_bits(dwc3_trb_dma_offset(dep
, trb_st_hw
));
613 trb_link
->ctrl
|= DWC3_TRBCTL_LINK_TRB
;
614 trb_link
->ctrl
|= DWC3_TRB_CTRL_HWO
;
618 switch (usb_endpoint_type(desc
)) {
619 case USB_ENDPOINT_XFER_CONTROL
:
620 /* don't change name */
622 case USB_ENDPOINT_XFER_ISOC
:
623 strlcat(dep
->name
, "-isoc", sizeof(dep
->name
));
625 case USB_ENDPOINT_XFER_BULK
:
626 strlcat(dep
->name
, "-bulk", sizeof(dep
->name
));
628 case USB_ENDPOINT_XFER_INT
:
629 strlcat(dep
->name
, "-int", sizeof(dep
->name
));
632 dev_err(dwc
->dev
, "invalid endpoint transfer type\n");
638 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
, bool force
);
639 static void dwc3_remove_requests(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
641 struct dwc3_request
*req
;
643 if (!list_empty(&dep
->started_list
)) {
644 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
646 /* - giveback all requests to gadget driver */
647 while (!list_empty(&dep
->started_list
)) {
648 req
= next_request(&dep
->started_list
);
650 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
654 while (!list_empty(&dep
->pending_list
)) {
655 req
= next_request(&dep
->pending_list
);
657 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
662 * __dwc3_gadget_ep_disable - Disables a HW endpoint
663 * @dep: the endpoint to disable
665 * This function also removes requests which are currently processed ny the
666 * hardware and those which are not yet scheduled.
667 * Caller should take care of locking.
669 static int __dwc3_gadget_ep_disable(struct dwc3_ep
*dep
)
671 struct dwc3
*dwc
= dep
->dwc
;
674 dwc3_trace(trace_dwc3_gadget
, "Disabling %s", dep
->name
);
676 dwc3_remove_requests(dwc
, dep
);
678 /* make sure HW endpoint isn't stalled */
679 if (dep
->flags
& DWC3_EP_STALL
)
680 __dwc3_gadget_ep_set_halt(dep
, 0, false);
682 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
683 reg
&= ~DWC3_DALEPENA_EP(dep
->number
);
684 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
686 dep
->stream_capable
= false;
687 dep
->endpoint
.desc
= NULL
;
688 dep
->comp_desc
= NULL
;
692 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s",
694 (dep
->number
& 1) ? "in" : "out");
699 /* -------------------------------------------------------------------------- */
701 static int dwc3_gadget_ep0_enable(struct usb_ep
*ep
,
702 const struct usb_endpoint_descriptor
*desc
)
707 static int dwc3_gadget_ep0_disable(struct usb_ep
*ep
)
712 /* -------------------------------------------------------------------------- */
714 static int dwc3_gadget_ep_enable(struct usb_ep
*ep
,
715 const struct usb_endpoint_descriptor
*desc
)
722 if (!ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
723 pr_debug("dwc3: invalid parameters\n");
727 if (!desc
->wMaxPacketSize
) {
728 pr_debug("dwc3: missing wMaxPacketSize\n");
732 dep
= to_dwc3_ep(ep
);
735 if (dev_WARN_ONCE(dwc
->dev
, dep
->flags
& DWC3_EP_ENABLED
,
736 "%s is already enabled\n",
740 spin_lock_irqsave(&dwc
->lock
, flags
);
741 ret
= __dwc3_gadget_ep_enable(dep
, desc
, ep
->comp_desc
, false, false);
742 spin_unlock_irqrestore(&dwc
->lock
, flags
);
747 static int dwc3_gadget_ep_disable(struct usb_ep
*ep
)
755 pr_debug("dwc3: invalid parameters\n");
759 dep
= to_dwc3_ep(ep
);
762 if (dev_WARN_ONCE(dwc
->dev
, !(dep
->flags
& DWC3_EP_ENABLED
),
763 "%s is already disabled\n",
767 spin_lock_irqsave(&dwc
->lock
, flags
);
768 ret
= __dwc3_gadget_ep_disable(dep
);
769 spin_unlock_irqrestore(&dwc
->lock
, flags
);
774 static struct usb_request
*dwc3_gadget_ep_alloc_request(struct usb_ep
*ep
,
777 struct dwc3_request
*req
;
778 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
780 req
= kzalloc(sizeof(*req
), gfp_flags
);
784 req
->epnum
= dep
->number
;
787 trace_dwc3_alloc_request(req
);
789 return &req
->request
;
792 static void dwc3_gadget_ep_free_request(struct usb_ep
*ep
,
793 struct usb_request
*request
)
795 struct dwc3_request
*req
= to_dwc3_request(request
);
797 trace_dwc3_free_request(req
);
802 * dwc3_prepare_one_trb - setup one TRB from one request
803 * @dep: endpoint for which this request is prepared
804 * @req: dwc3_request pointer
806 static void dwc3_prepare_one_trb(struct dwc3_ep
*dep
,
807 struct dwc3_request
*req
, dma_addr_t dma
,
808 unsigned length
, unsigned last
, unsigned chain
, unsigned node
)
810 struct dwc3_trb
*trb
;
812 dwc3_trace(trace_dwc3_gadget
, "%s: req %p dma %08llx length %d%s%s",
813 dep
->name
, req
, (unsigned long long) dma
,
814 length
, last
? " last" : "",
815 chain
? " chain" : "");
818 trb
= &dep
->trb_pool
[dep
->trb_enqueue
];
821 dwc3_gadget_move_started_request(req
);
823 req
->trb_dma
= dwc3_trb_dma_offset(dep
, trb
);
824 req
->first_trb_index
= dep
->trb_enqueue
;
827 dwc3_ep_inc_enq(dep
);
828 /* Skip the LINK-TRB */
829 if (dwc3_ep_is_last_trb(dep
->trb_enqueue
))
830 dwc3_ep_inc_enq(dep
);
832 trb
->size
= DWC3_TRB_SIZE_LENGTH(length
);
833 trb
->bpl
= lower_32_bits(dma
);
834 trb
->bph
= upper_32_bits(dma
);
836 switch (usb_endpoint_type(dep
->endpoint
.desc
)) {
837 case USB_ENDPOINT_XFER_CONTROL
:
838 trb
->ctrl
= DWC3_TRBCTL_CONTROL_SETUP
;
841 case USB_ENDPOINT_XFER_ISOC
:
843 trb
->ctrl
= DWC3_TRBCTL_ISOCHRONOUS_FIRST
;
845 trb
->ctrl
= DWC3_TRBCTL_ISOCHRONOUS
;
847 /* always enable Interrupt on Missed ISOC */
848 trb
->ctrl
|= DWC3_TRB_CTRL_ISP_IMI
;
851 case USB_ENDPOINT_XFER_BULK
:
852 case USB_ENDPOINT_XFER_INT
:
853 trb
->ctrl
= DWC3_TRBCTL_NORMAL
;
857 * This is only possible with faulty memory because we
858 * checked it already :)
863 /* always enable Continue on Short Packet */
864 trb
->ctrl
|= DWC3_TRB_CTRL_CSP
;
866 if (!req
->request
.no_interrupt
&& !chain
)
867 trb
->ctrl
|= DWC3_TRB_CTRL_IOC
| DWC3_TRB_CTRL_ISP_IMI
;
870 trb
->ctrl
|= DWC3_TRB_CTRL_LST
;
873 trb
->ctrl
|= DWC3_TRB_CTRL_CHN
;
875 if (usb_endpoint_xfer_bulk(dep
->endpoint
.desc
) && dep
->stream_capable
)
876 trb
->ctrl
|= DWC3_TRB_CTRL_SID_SOFN(req
->request
.stream_id
);
878 trb
->ctrl
|= DWC3_TRB_CTRL_HWO
;
880 trace_dwc3_prepare_trb(dep
, trb
);
884 * dwc3_prepare_trbs - setup TRBs from requests
885 * @dep: endpoint for which requests are being prepared
886 * @starting: true if the endpoint is idle and no requests are queued.
888 * The function goes through the requests list and sets up TRBs for the
889 * transfers. The function returns once there are no more TRBs available or
890 * it runs out of requests.
892 static void dwc3_prepare_trbs(struct dwc3_ep
*dep
, bool starting
)
894 struct dwc3_request
*req
, *n
;
896 unsigned int last_one
= 0;
898 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM
);
900 trbs_left
= dep
->trb_dequeue
- dep
->trb_enqueue
;
903 * If enqueue & dequeue are equal than it is either full or empty. If we
904 * are starting to process requests then we are empty. Otherwise we are
905 * full and don't do anything
911 trbs_left
= DWC3_TRB_NUM
;
914 /* The last TRB is a link TRB, not used for xfer */
918 list_for_each_entry_safe(req
, n
, &dep
->pending_list
, list
) {
923 if (req
->request
.num_mapped_sgs
> 0) {
924 struct usb_request
*request
= &req
->request
;
925 struct scatterlist
*sg
= request
->sg
;
926 struct scatterlist
*s
;
929 for_each_sg(sg
, s
, request
->num_mapped_sgs
, i
) {
930 unsigned chain
= true;
932 length
= sg_dma_len(s
);
933 dma
= sg_dma_address(s
);
935 if (i
== (request
->num_mapped_sgs
- 1) ||
937 if (list_empty(&dep
->pending_list
))
949 dwc3_prepare_one_trb(dep
, req
, dma
, length
,
959 dma
= req
->request
.dma
;
960 length
= req
->request
.length
;
966 /* Is this the last request? */
967 if (list_is_last(&req
->list
, &dep
->pending_list
))
970 dwc3_prepare_one_trb(dep
, req
, dma
, length
,
979 static int __dwc3_gadget_kick_transfer(struct dwc3_ep
*dep
, u16 cmd_param
,
982 struct dwc3_gadget_ep_cmd_params params
;
983 struct dwc3_request
*req
;
984 struct dwc3
*dwc
= dep
->dwc
;
988 if (start_new
&& (dep
->flags
& DWC3_EP_BUSY
)) {
989 dwc3_trace(trace_dwc3_gadget
, "%s: endpoint busy", dep
->name
);
994 * If we are getting here after a short-out-packet we don't enqueue any
995 * new requests as we try to set the IOC bit only on the last request.
998 if (list_empty(&dep
->started_list
))
999 dwc3_prepare_trbs(dep
, start_new
);
1001 /* req points to the first request which will be sent */
1002 req
= next_request(&dep
->started_list
);
1004 dwc3_prepare_trbs(dep
, start_new
);
1007 * req points to the first request where HWO changed from 0 to 1
1009 req
= next_request(&dep
->started_list
);
1012 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
1016 memset(¶ms
, 0, sizeof(params
));
1019 params
.param0
= upper_32_bits(req
->trb_dma
);
1020 params
.param1
= lower_32_bits(req
->trb_dma
);
1021 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
1023 cmd
= DWC3_DEPCMD_UPDATETRANSFER
;
1026 cmd
|= DWC3_DEPCMD_PARAM(cmd_param
);
1027 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
1030 * FIXME we need to iterate over the list of requests
1031 * here and stop, unmap, free and del each of the linked
1032 * requests instead of what we do now.
1034 usb_gadget_unmap_request(&dwc
->gadget
, &req
->request
,
1036 list_del(&req
->list
);
1040 dep
->flags
|= DWC3_EP_BUSY
;
1043 dep
->resource_index
= dwc3_gadget_ep_get_transfer_index(dwc
,
1045 WARN_ON_ONCE(!dep
->resource_index
);
1051 static void __dwc3_gadget_start_isoc(struct dwc3
*dwc
,
1052 struct dwc3_ep
*dep
, u32 cur_uf
)
1056 if (list_empty(&dep
->pending_list
)) {
1057 dwc3_trace(trace_dwc3_gadget
,
1058 "ISOC ep %s run out for requests",
1060 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
1064 /* 4 micro frames in the future */
1065 uf
= cur_uf
+ dep
->interval
* 4;
1067 __dwc3_gadget_kick_transfer(dep
, uf
, 1);
1070 static void dwc3_gadget_start_isoc(struct dwc3
*dwc
,
1071 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
)
1075 mask
= ~(dep
->interval
- 1);
1076 cur_uf
= event
->parameters
& mask
;
1078 __dwc3_gadget_start_isoc(dwc
, dep
, cur_uf
);
1081 static int __dwc3_gadget_ep_queue(struct dwc3_ep
*dep
, struct dwc3_request
*req
)
1083 struct dwc3
*dwc
= dep
->dwc
;
1086 if (!dep
->endpoint
.desc
) {
1087 dwc3_trace(trace_dwc3_gadget
,
1088 "trying to queue request %p to disabled %s\n",
1089 &req
->request
, dep
->endpoint
.name
);
1093 if (WARN(req
->dep
!= dep
, "request %p belongs to '%s'\n",
1094 &req
->request
, req
->dep
->name
)) {
1095 dwc3_trace(trace_dwc3_gadget
, "request %p belongs to '%s'\n",
1096 &req
->request
, req
->dep
->name
);
1100 req
->request
.actual
= 0;
1101 req
->request
.status
= -EINPROGRESS
;
1102 req
->direction
= dep
->direction
;
1103 req
->epnum
= dep
->number
;
1105 trace_dwc3_ep_queue(req
);
1108 * We only add to our list of requests now and
1109 * start consuming the list once we get XferNotReady
1112 * That way, we avoid doing anything that we don't need
1113 * to do now and defer it until the point we receive a
1114 * particular token from the Host side.
1116 * This will also avoid Host cancelling URBs due to too
1119 ret
= usb_gadget_map_request(&dwc
->gadget
, &req
->request
,
1124 list_add_tail(&req
->list
, &dep
->pending_list
);
1127 * If there are no pending requests and the endpoint isn't already
1128 * busy, we will just start the request straight away.
1130 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1131 * little bit faster.
1133 if (!usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
1134 !usb_endpoint_xfer_int(dep
->endpoint
.desc
) &&
1135 !(dep
->flags
& DWC3_EP_BUSY
)) {
1136 ret
= __dwc3_gadget_kick_transfer(dep
, 0, true);
1141 * There are a few special cases:
1143 * 1. XferNotReady with empty list of requests. We need to kick the
1144 * transfer here in that situation, otherwise we will be NAKing
1145 * forever. If we get XferNotReady before gadget driver has a
1146 * chance to queue a request, we will ACK the IRQ but won't be
1147 * able to receive the data until the next request is queued.
1148 * The following code is handling exactly that.
1151 if (dep
->flags
& DWC3_EP_PENDING_REQUEST
) {
1153 * If xfernotready is already elapsed and it is a case
1154 * of isoc transfer, then issue END TRANSFER, so that
1155 * you can receive xfernotready again and can have
1156 * notion of current microframe.
1158 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
1159 if (list_empty(&dep
->started_list
)) {
1160 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
1161 dep
->flags
= DWC3_EP_ENABLED
;
1166 ret
= __dwc3_gadget_kick_transfer(dep
, 0, true);
1168 dep
->flags
&= ~DWC3_EP_PENDING_REQUEST
;
1174 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1175 * kick the transfer here after queuing a request, otherwise the
1176 * core may not see the modified TRB(s).
1178 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
1179 (dep
->flags
& DWC3_EP_BUSY
) &&
1180 !(dep
->flags
& DWC3_EP_MISSED_ISOC
)) {
1181 WARN_ON_ONCE(!dep
->resource_index
);
1182 ret
= __dwc3_gadget_kick_transfer(dep
, dep
->resource_index
,
1188 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1189 * right away, otherwise host will not know we have streams to be
1192 if (dep
->stream_capable
)
1193 ret
= __dwc3_gadget_kick_transfer(dep
, 0, true);
1196 if (ret
&& ret
!= -EBUSY
)
1197 dwc3_trace(trace_dwc3_gadget
,
1198 "%s: failed to kick transfers\n",
1206 static void __dwc3_gadget_ep_zlp_complete(struct usb_ep
*ep
,
1207 struct usb_request
*request
)
1209 dwc3_gadget_ep_free_request(ep
, request
);
1212 static int __dwc3_gadget_ep_queue_zlp(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
1214 struct dwc3_request
*req
;
1215 struct usb_request
*request
;
1216 struct usb_ep
*ep
= &dep
->endpoint
;
1218 dwc3_trace(trace_dwc3_gadget
, "queueing ZLP\n");
1219 request
= dwc3_gadget_ep_alloc_request(ep
, GFP_ATOMIC
);
1223 request
->length
= 0;
1224 request
->buf
= dwc
->zlp_buf
;
1225 request
->complete
= __dwc3_gadget_ep_zlp_complete
;
1227 req
= to_dwc3_request(request
);
1229 return __dwc3_gadget_ep_queue(dep
, req
);
1232 static int dwc3_gadget_ep_queue(struct usb_ep
*ep
, struct usb_request
*request
,
1235 struct dwc3_request
*req
= to_dwc3_request(request
);
1236 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1237 struct dwc3
*dwc
= dep
->dwc
;
1239 unsigned long flags
;
1243 spin_lock_irqsave(&dwc
->lock
, flags
);
1244 ret
= __dwc3_gadget_ep_queue(dep
, req
);
1247 * Okay, here's the thing, if gadget driver has requested for a ZLP by
1248 * setting request->zero, instead of doing magic, we will just queue an
1249 * extra usb_request ourselves so that it gets handled the same way as
1250 * any other request.
1252 if (ret
== 0 && request
->zero
&& request
->length
&&
1253 (request
->length
% ep
->maxpacket
== 0))
1254 ret
= __dwc3_gadget_ep_queue_zlp(dwc
, dep
);
1256 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1261 static int dwc3_gadget_ep_dequeue(struct usb_ep
*ep
,
1262 struct usb_request
*request
)
1264 struct dwc3_request
*req
= to_dwc3_request(request
);
1265 struct dwc3_request
*r
= NULL
;
1267 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1268 struct dwc3
*dwc
= dep
->dwc
;
1270 unsigned long flags
;
1273 trace_dwc3_ep_dequeue(req
);
1275 spin_lock_irqsave(&dwc
->lock
, flags
);
1277 list_for_each_entry(r
, &dep
->pending_list
, list
) {
1283 list_for_each_entry(r
, &dep
->started_list
, list
) {
1288 /* wait until it is processed */
1289 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
1292 dev_err(dwc
->dev
, "request %p was not queued to %s\n",
1299 /* giveback the request */
1300 dwc3_gadget_giveback(dep
, req
, -ECONNRESET
);
1303 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1308 int __dwc3_gadget_ep_set_halt(struct dwc3_ep
*dep
, int value
, int protocol
)
1310 struct dwc3_gadget_ep_cmd_params params
;
1311 struct dwc3
*dwc
= dep
->dwc
;
1314 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
1315 dev_err(dwc
->dev
, "%s is of Isochronous type\n", dep
->name
);
1319 memset(¶ms
, 0x00, sizeof(params
));
1322 if (!protocol
&& ((dep
->direction
&& dep
->flags
& DWC3_EP_BUSY
) ||
1323 (!list_empty(&dep
->started_list
) ||
1324 !list_empty(&dep
->pending_list
)))) {
1325 dwc3_trace(trace_dwc3_gadget
,
1326 "%s: pending request, cannot halt",
1331 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
1332 DWC3_DEPCMD_SETSTALL
, ¶ms
);
1334 dev_err(dwc
->dev
, "failed to set STALL on %s\n",
1337 dep
->flags
|= DWC3_EP_STALL
;
1339 ret
= dwc3_send_clear_stall_ep_cmd(dep
);
1341 dev_err(dwc
->dev
, "failed to clear STALL on %s\n",
1344 dep
->flags
&= ~(DWC3_EP_STALL
| DWC3_EP_WEDGE
);
1350 static int dwc3_gadget_ep_set_halt(struct usb_ep
*ep
, int value
)
1352 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1353 struct dwc3
*dwc
= dep
->dwc
;
1355 unsigned long flags
;
1359 spin_lock_irqsave(&dwc
->lock
, flags
);
1360 ret
= __dwc3_gadget_ep_set_halt(dep
, value
, false);
1361 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1366 static int dwc3_gadget_ep_set_wedge(struct usb_ep
*ep
)
1368 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
1369 struct dwc3
*dwc
= dep
->dwc
;
1370 unsigned long flags
;
1373 spin_lock_irqsave(&dwc
->lock
, flags
);
1374 dep
->flags
|= DWC3_EP_WEDGE
;
1376 if (dep
->number
== 0 || dep
->number
== 1)
1377 ret
= __dwc3_gadget_ep0_set_halt(ep
, 1);
1379 ret
= __dwc3_gadget_ep_set_halt(dep
, 1, false);
1380 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1385 /* -------------------------------------------------------------------------- */
1387 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc
= {
1388 .bLength
= USB_DT_ENDPOINT_SIZE
,
1389 .bDescriptorType
= USB_DT_ENDPOINT
,
1390 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1393 static const struct usb_ep_ops dwc3_gadget_ep0_ops
= {
1394 .enable
= dwc3_gadget_ep0_enable
,
1395 .disable
= dwc3_gadget_ep0_disable
,
1396 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1397 .free_request
= dwc3_gadget_ep_free_request
,
1398 .queue
= dwc3_gadget_ep0_queue
,
1399 .dequeue
= dwc3_gadget_ep_dequeue
,
1400 .set_halt
= dwc3_gadget_ep0_set_halt
,
1401 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1404 static const struct usb_ep_ops dwc3_gadget_ep_ops
= {
1405 .enable
= dwc3_gadget_ep_enable
,
1406 .disable
= dwc3_gadget_ep_disable
,
1407 .alloc_request
= dwc3_gadget_ep_alloc_request
,
1408 .free_request
= dwc3_gadget_ep_free_request
,
1409 .queue
= dwc3_gadget_ep_queue
,
1410 .dequeue
= dwc3_gadget_ep_dequeue
,
1411 .set_halt
= dwc3_gadget_ep_set_halt
,
1412 .set_wedge
= dwc3_gadget_ep_set_wedge
,
1415 /* -------------------------------------------------------------------------- */
1417 static int dwc3_gadget_get_frame(struct usb_gadget
*g
)
1419 struct dwc3
*dwc
= gadget_to_dwc(g
);
1422 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1423 return DWC3_DSTS_SOFFN(reg
);
1426 static int __dwc3_gadget_wakeup(struct dwc3
*dwc
)
1428 unsigned long timeout
;
1437 * According to the Databook Remote wakeup request should
1438 * be issued only when the device is in early suspend state.
1440 * We can check that via USB Link State bits in DSTS register.
1442 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1444 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1445 if ((speed
== DWC3_DSTS_SUPERSPEED
) ||
1446 (speed
== DWC3_DSTS_SUPERSPEED_PLUS
)) {
1447 dwc3_trace(trace_dwc3_gadget
, "no wakeup on SuperSpeed\n");
1451 link_state
= DWC3_DSTS_USBLNKST(reg
);
1453 switch (link_state
) {
1454 case DWC3_LINK_STATE_RX_DET
: /* in HS, means Early Suspend */
1455 case DWC3_LINK_STATE_U3
: /* in HS, means SUSPEND */
1458 dwc3_trace(trace_dwc3_gadget
,
1459 "can't wakeup from '%s'\n",
1460 dwc3_gadget_link_string(link_state
));
1464 ret
= dwc3_gadget_set_link_state(dwc
, DWC3_LINK_STATE_RECOV
);
1466 dev_err(dwc
->dev
, "failed to put link in Recovery\n");
1470 /* Recent versions do this automatically */
1471 if (dwc
->revision
< DWC3_REVISION_194A
) {
1472 /* write zeroes to Link Change Request */
1473 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1474 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
1475 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1478 /* poll until Link State changes to ON */
1479 timeout
= jiffies
+ msecs_to_jiffies(100);
1481 while (!time_after(jiffies
, timeout
)) {
1482 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1484 /* in HS, means ON */
1485 if (DWC3_DSTS_USBLNKST(reg
) == DWC3_LINK_STATE_U0
)
1489 if (DWC3_DSTS_USBLNKST(reg
) != DWC3_LINK_STATE_U0
) {
1490 dev_err(dwc
->dev
, "failed to send remote wakeup\n");
1497 static int dwc3_gadget_wakeup(struct usb_gadget
*g
)
1499 struct dwc3
*dwc
= gadget_to_dwc(g
);
1500 unsigned long flags
;
1503 spin_lock_irqsave(&dwc
->lock
, flags
);
1504 ret
= __dwc3_gadget_wakeup(dwc
);
1505 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1510 static int dwc3_gadget_set_selfpowered(struct usb_gadget
*g
,
1513 struct dwc3
*dwc
= gadget_to_dwc(g
);
1514 unsigned long flags
;
1516 spin_lock_irqsave(&dwc
->lock
, flags
);
1517 g
->is_selfpowered
= !!is_selfpowered
;
1518 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1523 static int dwc3_gadget_run_stop(struct dwc3
*dwc
, int is_on
, int suspend
)
1528 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1530 if (dwc
->revision
<= DWC3_REVISION_187A
) {
1531 reg
&= ~DWC3_DCTL_TRGTULST_MASK
;
1532 reg
|= DWC3_DCTL_TRGTULST_RX_DET
;
1535 if (dwc
->revision
>= DWC3_REVISION_194A
)
1536 reg
&= ~DWC3_DCTL_KEEP_CONNECT
;
1537 reg
|= DWC3_DCTL_RUN_STOP
;
1539 if (dwc
->has_hibernation
)
1540 reg
|= DWC3_DCTL_KEEP_CONNECT
;
1542 dwc
->pullups_connected
= true;
1544 reg
&= ~DWC3_DCTL_RUN_STOP
;
1546 if (dwc
->has_hibernation
&& !suspend
)
1547 reg
&= ~DWC3_DCTL_KEEP_CONNECT
;
1549 dwc
->pullups_connected
= false;
1552 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1555 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1557 if (!(reg
& DWC3_DSTS_DEVCTRLHLT
))
1560 if (reg
& DWC3_DSTS_DEVCTRLHLT
)
1569 dwc3_trace(trace_dwc3_gadget
, "gadget %s data soft-%s",
1571 ? dwc
->gadget_driver
->function
: "no-function",
1572 is_on
? "connect" : "disconnect");
1577 static int dwc3_gadget_pullup(struct usb_gadget
*g
, int is_on
)
1579 struct dwc3
*dwc
= gadget_to_dwc(g
);
1580 unsigned long flags
;
1585 spin_lock_irqsave(&dwc
->lock
, flags
);
1586 ret
= dwc3_gadget_run_stop(dwc
, is_on
, false);
1587 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1592 static void dwc3_gadget_enable_irq(struct dwc3
*dwc
)
1596 /* Enable all but Start and End of Frame IRQs */
1597 reg
= (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN
|
1598 DWC3_DEVTEN_EVNTOVERFLOWEN
|
1599 DWC3_DEVTEN_CMDCMPLTEN
|
1600 DWC3_DEVTEN_ERRTICERREN
|
1601 DWC3_DEVTEN_WKUPEVTEN
|
1602 DWC3_DEVTEN_ULSTCNGEN
|
1603 DWC3_DEVTEN_CONNECTDONEEN
|
1604 DWC3_DEVTEN_USBRSTEN
|
1605 DWC3_DEVTEN_DISCONNEVTEN
);
1607 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, reg
);
1610 static void dwc3_gadget_disable_irq(struct dwc3
*dwc
)
1612 /* mask all interrupts */
1613 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
1616 static irqreturn_t
dwc3_interrupt(int irq
, void *_dwc
);
1617 static irqreturn_t
dwc3_thread_interrupt(int irq
, void *_dwc
);
1619 static int dwc3_gadget_start(struct usb_gadget
*g
,
1620 struct usb_gadget_driver
*driver
)
1622 struct dwc3
*dwc
= gadget_to_dwc(g
);
1623 struct dwc3_ep
*dep
;
1624 unsigned long flags
;
1629 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
1630 ret
= request_threaded_irq(irq
, dwc3_interrupt
, dwc3_thread_interrupt
,
1631 IRQF_SHARED
, "dwc3", dwc
->ev_buf
);
1633 dev_err(dwc
->dev
, "failed to request irq #%d --> %d\n",
1638 spin_lock_irqsave(&dwc
->lock
, flags
);
1640 if (dwc
->gadget_driver
) {
1641 dev_err(dwc
->dev
, "%s is already bound to %s\n",
1643 dwc
->gadget_driver
->driver
.name
);
1648 dwc
->gadget_driver
= driver
;
1650 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1651 reg
&= ~(DWC3_DCFG_SPEED_MASK
);
1654 * WORKAROUND: DWC3 revision < 2.20a have an issue
1655 * which would cause metastability state on Run/Stop
1656 * bit if we try to force the IP to USB2-only mode.
1658 * Because of that, we cannot configure the IP to any
1659 * speed other than the SuperSpeed
1663 * STAR#9000525659: Clock Domain Crossing on DCTL in
1666 if (dwc
->revision
< DWC3_REVISION_220A
) {
1667 reg
|= DWC3_DCFG_SUPERSPEED
;
1669 switch (dwc
->maximum_speed
) {
1671 reg
|= DWC3_DSTS_LOWSPEED
;
1673 case USB_SPEED_FULL
:
1674 reg
|= DWC3_DSTS_FULLSPEED1
;
1676 case USB_SPEED_HIGH
:
1677 reg
|= DWC3_DSTS_HIGHSPEED
;
1679 case USB_SPEED_SUPER_PLUS
:
1680 reg
|= DWC3_DSTS_SUPERSPEED_PLUS
;
1683 dev_err(dwc
->dev
, "invalid dwc->maximum_speed (%d)\n",
1684 dwc
->maximum_speed
);
1686 case USB_SPEED_SUPER
:
1687 reg
|= DWC3_DCFG_SUPERSPEED
;
1691 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1694 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1695 * field instead of letting dwc3 itself calculate that automatically.
1697 * This way, we maximize the chances that we'll be able to get several
1698 * bursts of data without going through any sort of endpoint throttling.
1700 reg
= dwc3_readl(dwc
->regs
, DWC3_GRXTHRCFG
);
1701 reg
&= ~DWC3_GRXTHRCFG_PKTCNTSEL
;
1702 dwc3_writel(dwc
->regs
, DWC3_GRXTHRCFG
, reg
);
1704 /* Start with SuperSpeed Default */
1705 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1708 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
, false,
1711 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1716 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
, false,
1719 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1723 /* begin to receive SETUP packets */
1724 dwc
->ep0state
= EP0_SETUP_PHASE
;
1725 dwc3_ep0_out_start(dwc
);
1727 dwc3_gadget_enable_irq(dwc
);
1729 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1734 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1737 dwc
->gadget_driver
= NULL
;
1740 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1742 free_irq(irq
, dwc
->ev_buf
);
1748 static int dwc3_gadget_stop(struct usb_gadget
*g
)
1750 struct dwc3
*dwc
= gadget_to_dwc(g
);
1751 unsigned long flags
;
1754 spin_lock_irqsave(&dwc
->lock
, flags
);
1756 dwc3_gadget_disable_irq(dwc
);
1757 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1758 __dwc3_gadget_ep_disable(dwc
->eps
[1]);
1760 dwc
->gadget_driver
= NULL
;
1762 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1764 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
1765 free_irq(irq
, dwc
->ev_buf
);
1770 static const struct usb_gadget_ops dwc3_gadget_ops
= {
1771 .get_frame
= dwc3_gadget_get_frame
,
1772 .wakeup
= dwc3_gadget_wakeup
,
1773 .set_selfpowered
= dwc3_gadget_set_selfpowered
,
1774 .pullup
= dwc3_gadget_pullup
,
1775 .udc_start
= dwc3_gadget_start
,
1776 .udc_stop
= dwc3_gadget_stop
,
1779 /* -------------------------------------------------------------------------- */
1781 static int dwc3_gadget_init_hw_endpoints(struct dwc3
*dwc
,
1782 u8 num
, u32 direction
)
1784 struct dwc3_ep
*dep
;
1787 for (i
= 0; i
< num
; i
++) {
1788 u8 epnum
= (i
<< 1) | (!!direction
);
1790 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
1795 dep
->number
= epnum
;
1796 dep
->direction
= !!direction
;
1797 dwc
->eps
[epnum
] = dep
;
1799 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s", epnum
>> 1,
1800 (epnum
& 1) ? "in" : "out");
1802 dep
->endpoint
.name
= dep
->name
;
1804 dwc3_trace(trace_dwc3_gadget
, "initializing %s", dep
->name
);
1806 if (epnum
== 0 || epnum
== 1) {
1807 usb_ep_set_maxpacket_limit(&dep
->endpoint
, 512);
1808 dep
->endpoint
.maxburst
= 1;
1809 dep
->endpoint
.ops
= &dwc3_gadget_ep0_ops
;
1811 dwc
->gadget
.ep0
= &dep
->endpoint
;
1815 usb_ep_set_maxpacket_limit(&dep
->endpoint
, 1024);
1816 dep
->endpoint
.max_streams
= 15;
1817 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
1818 list_add_tail(&dep
->endpoint
.ep_list
,
1819 &dwc
->gadget
.ep_list
);
1821 ret
= dwc3_alloc_trb_pool(dep
);
1826 if (epnum
== 0 || epnum
== 1) {
1827 dep
->endpoint
.caps
.type_control
= true;
1829 dep
->endpoint
.caps
.type_iso
= true;
1830 dep
->endpoint
.caps
.type_bulk
= true;
1831 dep
->endpoint
.caps
.type_int
= true;
1834 dep
->endpoint
.caps
.dir_in
= !!direction
;
1835 dep
->endpoint
.caps
.dir_out
= !direction
;
1837 INIT_LIST_HEAD(&dep
->pending_list
);
1838 INIT_LIST_HEAD(&dep
->started_list
);
1844 static int dwc3_gadget_init_endpoints(struct dwc3
*dwc
)
1848 INIT_LIST_HEAD(&dwc
->gadget
.ep_list
);
1850 ret
= dwc3_gadget_init_hw_endpoints(dwc
, dwc
->num_out_eps
, 0);
1852 dwc3_trace(trace_dwc3_gadget
,
1853 "failed to allocate OUT endpoints");
1857 ret
= dwc3_gadget_init_hw_endpoints(dwc
, dwc
->num_in_eps
, 1);
1859 dwc3_trace(trace_dwc3_gadget
,
1860 "failed to allocate IN endpoints");
1867 static void dwc3_gadget_free_endpoints(struct dwc3
*dwc
)
1869 struct dwc3_ep
*dep
;
1872 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1873 dep
= dwc
->eps
[epnum
];
1877 * Physical endpoints 0 and 1 are special; they form the
1878 * bi-directional USB endpoint 0.
1880 * For those two physical endpoints, we don't allocate a TRB
1881 * pool nor do we add them the endpoints list. Due to that, we
1882 * shouldn't do these two operations otherwise we would end up
1883 * with all sorts of bugs when removing dwc3.ko.
1885 if (epnum
!= 0 && epnum
!= 1) {
1886 dwc3_free_trb_pool(dep
);
1887 list_del(&dep
->endpoint
.ep_list
);
1894 /* -------------------------------------------------------------------------- */
1896 static int __dwc3_cleanup_done_trbs(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
1897 struct dwc3_request
*req
, struct dwc3_trb
*trb
,
1898 const struct dwc3_event_depevt
*event
, int status
)
1901 unsigned int s_pkt
= 0;
1902 unsigned int trb_status
;
1904 trace_dwc3_complete_trb(dep
, trb
);
1906 if ((trb
->ctrl
& DWC3_TRB_CTRL_HWO
) && status
!= -ESHUTDOWN
)
1908 * We continue despite the error. There is not much we
1909 * can do. If we don't clean it up we loop forever. If
1910 * we skip the TRB then it gets overwritten after a
1911 * while since we use them in a ring buffer. A BUG()
1912 * would help. Lets hope that if this occurs, someone
1913 * fixes the root cause instead of looking away :)
1915 dev_err(dwc
->dev
, "%s's TRB (%p) still owned by HW\n",
1917 count
= trb
->size
& DWC3_TRB_SIZE_MASK
;
1919 if (dep
->direction
) {
1921 trb_status
= DWC3_TRB_SIZE_TRBSTS(trb
->size
);
1922 if (trb_status
== DWC3_TRBSTS_MISSED_ISOC
) {
1923 dwc3_trace(trace_dwc3_gadget
,
1924 "%s: incomplete IN transfer\n",
1927 * If missed isoc occurred and there is
1928 * no request queued then issue END
1929 * TRANSFER, so that core generates
1930 * next xfernotready and we will issue
1931 * a fresh START TRANSFER.
1932 * If there are still queued request
1933 * then wait, do not issue either END
1934 * or UPDATE TRANSFER, just attach next
1935 * request in pending_list during
1936 * giveback.If any future queued request
1937 * is successfully transferred then we
1938 * will issue UPDATE TRANSFER for all
1939 * request in the pending_list.
1941 dep
->flags
|= DWC3_EP_MISSED_ISOC
;
1943 dev_err(dwc
->dev
, "incomplete IN transfer %s\n",
1945 status
= -ECONNRESET
;
1948 dep
->flags
&= ~DWC3_EP_MISSED_ISOC
;
1951 if (count
&& (event
->status
& DEPEVT_STATUS_SHORT
))
1956 * We assume here we will always receive the entire data block
1957 * which we should receive. Meaning, if we program RX to
1958 * receive 4K but we receive only 2K, we assume that's all we
1959 * should receive and we simply bounce the request back to the
1960 * gadget driver for further processing.
1962 req
->request
.actual
+= req
->request
.length
- count
;
1965 if ((event
->status
& DEPEVT_STATUS_LST
) &&
1966 (trb
->ctrl
& (DWC3_TRB_CTRL_LST
|
1967 DWC3_TRB_CTRL_HWO
)))
1969 if ((event
->status
& DEPEVT_STATUS_IOC
) &&
1970 (trb
->ctrl
& DWC3_TRB_CTRL_IOC
))
1975 static int dwc3_cleanup_done_reqs(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
1976 const struct dwc3_event_depevt
*event
, int status
)
1978 struct dwc3_request
*req
;
1979 struct dwc3_trb
*trb
;
1985 req
= next_request(&dep
->started_list
);
1986 if (WARN_ON_ONCE(!req
))
1991 slot
= req
->first_trb_index
+ i
;
1992 if (slot
== DWC3_TRB_NUM
- 1)
1994 slot
%= DWC3_TRB_NUM
;
1995 trb
= &dep
->trb_pool
[slot
];
1997 ret
= __dwc3_cleanup_done_trbs(dwc
, dep
, req
, trb
,
2001 } while (++i
< req
->request
.num_mapped_sgs
);
2003 dwc3_gadget_giveback(dep
, req
, status
);
2009 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
2010 list_empty(&dep
->started_list
)) {
2011 if (list_empty(&dep
->pending_list
)) {
2013 * If there is no entry in request list then do
2014 * not issue END TRANSFER now. Just set PENDING
2015 * flag, so that END TRANSFER is issued when an
2016 * entry is added into request list.
2018 dep
->flags
= DWC3_EP_PENDING_REQUEST
;
2020 dwc3_stop_active_transfer(dwc
, dep
->number
, true);
2021 dep
->flags
= DWC3_EP_ENABLED
;
2029 static void dwc3_endpoint_transfer_complete(struct dwc3
*dwc
,
2030 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
)
2032 unsigned status
= 0;
2034 u32 is_xfer_complete
;
2036 is_xfer_complete
= (event
->endpoint_event
== DWC3_DEPEVT_XFERCOMPLETE
);
2038 if (event
->status
& DEPEVT_STATUS_BUSERR
)
2039 status
= -ECONNRESET
;
2041 clean_busy
= dwc3_cleanup_done_reqs(dwc
, dep
, event
, status
);
2042 if (clean_busy
&& (is_xfer_complete
||
2043 usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)))
2044 dep
->flags
&= ~DWC3_EP_BUSY
;
2047 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2048 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2050 if (dwc
->revision
< DWC3_REVISION_183A
) {
2054 for (i
= 0; i
< DWC3_ENDPOINTS_NUM
; i
++) {
2057 if (!(dep
->flags
& DWC3_EP_ENABLED
))
2060 if (!list_empty(&dep
->started_list
))
2064 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2066 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2071 if (!usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
2074 ret
= __dwc3_gadget_kick_transfer(dep
, 0, is_xfer_complete
);
2075 if (!ret
|| ret
== -EBUSY
)
2080 static void dwc3_endpoint_interrupt(struct dwc3
*dwc
,
2081 const struct dwc3_event_depevt
*event
)
2083 struct dwc3_ep
*dep
;
2084 u8 epnum
= event
->endpoint_number
;
2086 dep
= dwc
->eps
[epnum
];
2088 if (!(dep
->flags
& DWC3_EP_ENABLED
))
2091 if (epnum
== 0 || epnum
== 1) {
2092 dwc3_ep0_interrupt(dwc
, event
);
2096 switch (event
->endpoint_event
) {
2097 case DWC3_DEPEVT_XFERCOMPLETE
:
2098 dep
->resource_index
= 0;
2100 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
2101 dwc3_trace(trace_dwc3_gadget
,
2102 "%s is an Isochronous endpoint\n",
2107 dwc3_endpoint_transfer_complete(dwc
, dep
, event
);
2109 case DWC3_DEPEVT_XFERINPROGRESS
:
2110 dwc3_endpoint_transfer_complete(dwc
, dep
, event
);
2112 case DWC3_DEPEVT_XFERNOTREADY
:
2113 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
)) {
2114 dwc3_gadget_start_isoc(dwc
, dep
, event
);
2119 active
= event
->status
& DEPEVT_STATUS_TRANSFER_ACTIVE
;
2121 dwc3_trace(trace_dwc3_gadget
, "%s: reason %s",
2122 dep
->name
, active
? "Transfer Active"
2123 : "Transfer Not Active");
2125 ret
= __dwc3_gadget_kick_transfer(dep
, 0, !active
);
2126 if (!ret
|| ret
== -EBUSY
)
2129 dwc3_trace(trace_dwc3_gadget
,
2130 "%s: failed to kick transfers\n",
2135 case DWC3_DEPEVT_STREAMEVT
:
2136 if (!usb_endpoint_xfer_bulk(dep
->endpoint
.desc
)) {
2137 dev_err(dwc
->dev
, "Stream event for non-Bulk %s\n",
2142 switch (event
->status
) {
2143 case DEPEVT_STREAMEVT_FOUND
:
2144 dwc3_trace(trace_dwc3_gadget
,
2145 "Stream %d found and started",
2149 case DEPEVT_STREAMEVT_NOTFOUND
:
2152 dwc3_trace(trace_dwc3_gadget
,
2153 "unable to find suitable stream\n");
2156 case DWC3_DEPEVT_RXTXFIFOEVT
:
2157 dwc3_trace(trace_dwc3_gadget
, "%s FIFO Overrun\n", dep
->name
);
2159 case DWC3_DEPEVT_EPCMDCMPLT
:
2160 dwc3_trace(trace_dwc3_gadget
, "Endpoint Command Complete");
2165 static void dwc3_disconnect_gadget(struct dwc3
*dwc
)
2167 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->disconnect
) {
2168 spin_unlock(&dwc
->lock
);
2169 dwc
->gadget_driver
->disconnect(&dwc
->gadget
);
2170 spin_lock(&dwc
->lock
);
2174 static void dwc3_suspend_gadget(struct dwc3
*dwc
)
2176 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->suspend
) {
2177 spin_unlock(&dwc
->lock
);
2178 dwc
->gadget_driver
->suspend(&dwc
->gadget
);
2179 spin_lock(&dwc
->lock
);
2183 static void dwc3_resume_gadget(struct dwc3
*dwc
)
2185 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->resume
) {
2186 spin_unlock(&dwc
->lock
);
2187 dwc
->gadget_driver
->resume(&dwc
->gadget
);
2188 spin_lock(&dwc
->lock
);
2192 static void dwc3_reset_gadget(struct dwc3
*dwc
)
2194 if (!dwc
->gadget_driver
)
2197 if (dwc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
2198 spin_unlock(&dwc
->lock
);
2199 usb_gadget_udc_reset(&dwc
->gadget
, dwc
->gadget_driver
);
2200 spin_lock(&dwc
->lock
);
2204 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
, bool force
)
2206 struct dwc3_ep
*dep
;
2207 struct dwc3_gadget_ep_cmd_params params
;
2211 dep
= dwc
->eps
[epnum
];
2213 if (!dep
->resource_index
)
2217 * NOTICE: We are violating what the Databook says about the
2218 * EndTransfer command. Ideally we would _always_ wait for the
2219 * EndTransfer Command Completion IRQ, but that's causing too
2220 * much trouble synchronizing between us and gadget driver.
2222 * We have discussed this with the IP Provider and it was
2223 * suggested to giveback all requests here, but give HW some
2224 * extra time to synchronize with the interconnect. We're using
2225 * an arbitrary 100us delay for that.
2227 * Note also that a similar handling was tested by Synopsys
2228 * (thanks a lot Paul) and nothing bad has come out of it.
2229 * In short, what we're doing is:
2231 * - Issue EndTransfer WITH CMDIOC bit set
2235 cmd
= DWC3_DEPCMD_ENDTRANSFER
;
2236 cmd
|= force
? DWC3_DEPCMD_HIPRI_FORCERM
: 0;
2237 cmd
|= DWC3_DEPCMD_CMDIOC
;
2238 cmd
|= DWC3_DEPCMD_PARAM(dep
->resource_index
);
2239 memset(¶ms
, 0, sizeof(params
));
2240 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
2242 dep
->resource_index
= 0;
2243 dep
->flags
&= ~DWC3_EP_BUSY
;
2247 static void dwc3_stop_active_transfers(struct dwc3
*dwc
)
2251 for (epnum
= 2; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
2252 struct dwc3_ep
*dep
;
2254 dep
= dwc
->eps
[epnum
];
2258 if (!(dep
->flags
& DWC3_EP_ENABLED
))
2261 dwc3_remove_requests(dwc
, dep
);
2265 static void dwc3_clear_stall_all_ep(struct dwc3
*dwc
)
2269 for (epnum
= 1; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
2270 struct dwc3_ep
*dep
;
2273 dep
= dwc
->eps
[epnum
];
2277 if (!(dep
->flags
& DWC3_EP_STALL
))
2280 dep
->flags
&= ~DWC3_EP_STALL
;
2282 ret
= dwc3_send_clear_stall_ep_cmd(dep
);
2287 static void dwc3_gadget_disconnect_interrupt(struct dwc3
*dwc
)
2291 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2292 reg
&= ~DWC3_DCTL_INITU1ENA
;
2293 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2295 reg
&= ~DWC3_DCTL_INITU2ENA
;
2296 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2298 dwc3_disconnect_gadget(dwc
);
2300 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2301 dwc
->setup_packet_pending
= false;
2302 usb_gadget_set_state(&dwc
->gadget
, USB_STATE_NOTATTACHED
);
2305 static void dwc3_gadget_reset_interrupt(struct dwc3
*dwc
)
2310 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2311 * would cause a missing Disconnect Event if there's a
2312 * pending Setup Packet in the FIFO.
2314 * There's no suggested workaround on the official Bug
2315 * report, which states that "unless the driver/application
2316 * is doing any special handling of a disconnect event,
2317 * there is no functional issue".
2319 * Unfortunately, it turns out that we _do_ some special
2320 * handling of a disconnect event, namely complete all
2321 * pending transfers, notify gadget driver of the
2322 * disconnection, and so on.
2324 * Our suggested workaround is to follow the Disconnect
2325 * Event steps here, instead, based on a setup_packet_pending
2326 * flag. Such flag gets set whenever we have a SETUP_PENDING
2327 * status for EP0 TRBs and gets cleared on XferComplete for the
2332 * STAR#9000466709: RTL: Device : Disconnect event not
2333 * generated if setup packet pending in FIFO
2335 if (dwc
->revision
< DWC3_REVISION_188A
) {
2336 if (dwc
->setup_packet_pending
)
2337 dwc3_gadget_disconnect_interrupt(dwc
);
2340 dwc3_reset_gadget(dwc
);
2342 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2343 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
2344 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2345 dwc
->test_mode
= false;
2347 dwc3_stop_active_transfers(dwc
);
2348 dwc3_clear_stall_all_ep(dwc
);
2350 /* Reset device address to zero */
2351 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2352 reg
&= ~(DWC3_DCFG_DEVADDR_MASK
);
2353 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2356 static void dwc3_update_ram_clk_sel(struct dwc3
*dwc
, u32 speed
)
2359 u32 usb30_clock
= DWC3_GCTL_CLK_BUS
;
2362 * We change the clock only at SS but I dunno why I would want to do
2363 * this. Maybe it becomes part of the power saving plan.
2366 if ((speed
!= DWC3_DSTS_SUPERSPEED
) &&
2367 (speed
!= DWC3_DSTS_SUPERSPEED_PLUS
))
2371 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2372 * each time on Connect Done.
2377 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
2378 reg
|= DWC3_GCTL_RAMCLKSEL(usb30_clock
);
2379 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
2382 static void dwc3_gadget_conndone_interrupt(struct dwc3
*dwc
)
2384 struct dwc3_ep
*dep
;
2389 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
2390 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
2393 dwc3_update_ram_clk_sel(dwc
, speed
);
2396 case DWC3_DCFG_SUPERSPEED_PLUS
:
2397 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
2398 dwc
->gadget
.ep0
->maxpacket
= 512;
2399 dwc
->gadget
.speed
= USB_SPEED_SUPER_PLUS
;
2401 case DWC3_DCFG_SUPERSPEED
:
2403 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2404 * would cause a missing USB3 Reset event.
2406 * In such situations, we should force a USB3 Reset
2407 * event by calling our dwc3_gadget_reset_interrupt()
2412 * STAR#9000483510: RTL: SS : USB3 reset event may
2413 * not be generated always when the link enters poll
2415 if (dwc
->revision
< DWC3_REVISION_190A
)
2416 dwc3_gadget_reset_interrupt(dwc
);
2418 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
2419 dwc
->gadget
.ep0
->maxpacket
= 512;
2420 dwc
->gadget
.speed
= USB_SPEED_SUPER
;
2422 case DWC3_DCFG_HIGHSPEED
:
2423 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
2424 dwc
->gadget
.ep0
->maxpacket
= 64;
2425 dwc
->gadget
.speed
= USB_SPEED_HIGH
;
2427 case DWC3_DCFG_FULLSPEED2
:
2428 case DWC3_DCFG_FULLSPEED1
:
2429 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
2430 dwc
->gadget
.ep0
->maxpacket
= 64;
2431 dwc
->gadget
.speed
= USB_SPEED_FULL
;
2433 case DWC3_DCFG_LOWSPEED
:
2434 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(8);
2435 dwc
->gadget
.ep0
->maxpacket
= 8;
2436 dwc
->gadget
.speed
= USB_SPEED_LOW
;
2440 /* Enable USB2 LPM Capability */
2442 if ((dwc
->revision
> DWC3_REVISION_194A
) &&
2443 (speed
!= DWC3_DCFG_SUPERSPEED
) &&
2444 (speed
!= DWC3_DCFG_SUPERSPEED_PLUS
)) {
2445 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2446 reg
|= DWC3_DCFG_LPM_CAP
;
2447 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
2449 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2450 reg
&= ~(DWC3_DCTL_HIRD_THRES_MASK
| DWC3_DCTL_L1_HIBER_EN
);
2452 reg
|= DWC3_DCTL_HIRD_THRES(dwc
->hird_threshold
);
2455 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2456 * DCFG.LPMCap is set, core responses with an ACK and the
2457 * BESL value in the LPM token is less than or equal to LPM
2460 WARN_ONCE(dwc
->revision
< DWC3_REVISION_240A
2461 && dwc
->has_lpm_erratum
,
2462 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2464 if (dwc
->has_lpm_erratum
&& dwc
->revision
>= DWC3_REVISION_240A
)
2465 reg
|= DWC3_DCTL_LPM_ERRATA(dwc
->lpm_nyet_threshold
);
2467 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2469 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2470 reg
&= ~DWC3_DCTL_HIRD_THRES_MASK
;
2471 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2475 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
, true,
2478 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
2483 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
, true,
2486 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
2491 * Configure PHY via GUSB3PIPECTLn if required.
2493 * Update GTXFIFOSIZn
2495 * In both cases reset values should be sufficient.
2499 static void dwc3_gadget_wakeup_interrupt(struct dwc3
*dwc
)
2502 * TODO take core out of low power mode when that's
2506 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->resume
) {
2507 spin_unlock(&dwc
->lock
);
2508 dwc
->gadget_driver
->resume(&dwc
->gadget
);
2509 spin_lock(&dwc
->lock
);
2513 static void dwc3_gadget_linksts_change_interrupt(struct dwc3
*dwc
,
2514 unsigned int evtinfo
)
2516 enum dwc3_link_state next
= evtinfo
& DWC3_LINK_STATE_MASK
;
2517 unsigned int pwropt
;
2520 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2521 * Hibernation mode enabled which would show up when device detects
2522 * host-initiated U3 exit.
2524 * In that case, device will generate a Link State Change Interrupt
2525 * from U3 to RESUME which is only necessary if Hibernation is
2528 * There are no functional changes due to such spurious event and we
2529 * just need to ignore it.
2533 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2536 pwropt
= DWC3_GHWPARAMS1_EN_PWROPT(dwc
->hwparams
.hwparams1
);
2537 if ((dwc
->revision
< DWC3_REVISION_250A
) &&
2538 (pwropt
!= DWC3_GHWPARAMS1_EN_PWROPT_HIB
)) {
2539 if ((dwc
->link_state
== DWC3_LINK_STATE_U3
) &&
2540 (next
== DWC3_LINK_STATE_RESUME
)) {
2541 dwc3_trace(trace_dwc3_gadget
,
2542 "ignoring transition U3 -> Resume");
2548 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2549 * on the link partner, the USB session might do multiple entry/exit
2550 * of low power states before a transfer takes place.
2552 * Due to this problem, we might experience lower throughput. The
2553 * suggested workaround is to disable DCTL[12:9] bits if we're
2554 * transitioning from U1/U2 to U0 and enable those bits again
2555 * after a transfer completes and there are no pending transfers
2556 * on any of the enabled endpoints.
2558 * This is the first half of that workaround.
2562 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2563 * core send LGO_Ux entering U0
2565 if (dwc
->revision
< DWC3_REVISION_183A
) {
2566 if (next
== DWC3_LINK_STATE_U0
) {
2570 switch (dwc
->link_state
) {
2571 case DWC3_LINK_STATE_U1
:
2572 case DWC3_LINK_STATE_U2
:
2573 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
2574 u1u2
= reg
& (DWC3_DCTL_INITU2ENA
2575 | DWC3_DCTL_ACCEPTU2ENA
2576 | DWC3_DCTL_INITU1ENA
2577 | DWC3_DCTL_ACCEPTU1ENA
);
2580 dwc
->u1u2
= reg
& u1u2
;
2584 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
2594 case DWC3_LINK_STATE_U1
:
2595 if (dwc
->speed
== USB_SPEED_SUPER
)
2596 dwc3_suspend_gadget(dwc
);
2598 case DWC3_LINK_STATE_U2
:
2599 case DWC3_LINK_STATE_U3
:
2600 dwc3_suspend_gadget(dwc
);
2602 case DWC3_LINK_STATE_RESUME
:
2603 dwc3_resume_gadget(dwc
);
2610 dwc
->link_state
= next
;
2613 static void dwc3_gadget_hibernation_interrupt(struct dwc3
*dwc
,
2614 unsigned int evtinfo
)
2616 unsigned int is_ss
= evtinfo
& BIT(4);
2619 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2620 * have a known issue which can cause USB CV TD.9.23 to fail
2623 * Because of this issue, core could generate bogus hibernation
2624 * events which SW needs to ignore.
2628 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2629 * Device Fallback from SuperSpeed
2631 if (is_ss
^ (dwc
->speed
== USB_SPEED_SUPER
))
2634 /* enter hibernation here */
2637 static void dwc3_gadget_interrupt(struct dwc3
*dwc
,
2638 const struct dwc3_event_devt
*event
)
2640 switch (event
->type
) {
2641 case DWC3_DEVICE_EVENT_DISCONNECT
:
2642 dwc3_gadget_disconnect_interrupt(dwc
);
2644 case DWC3_DEVICE_EVENT_RESET
:
2645 dwc3_gadget_reset_interrupt(dwc
);
2647 case DWC3_DEVICE_EVENT_CONNECT_DONE
:
2648 dwc3_gadget_conndone_interrupt(dwc
);
2650 case DWC3_DEVICE_EVENT_WAKEUP
:
2651 dwc3_gadget_wakeup_interrupt(dwc
);
2653 case DWC3_DEVICE_EVENT_HIBER_REQ
:
2654 if (dev_WARN_ONCE(dwc
->dev
, !dwc
->has_hibernation
,
2655 "unexpected hibernation event\n"))
2658 dwc3_gadget_hibernation_interrupt(dwc
, event
->event_info
);
2660 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE
:
2661 dwc3_gadget_linksts_change_interrupt(dwc
, event
->event_info
);
2663 case DWC3_DEVICE_EVENT_EOPF
:
2664 dwc3_trace(trace_dwc3_gadget
, "End of Periodic Frame");
2666 case DWC3_DEVICE_EVENT_SOF
:
2667 dwc3_trace(trace_dwc3_gadget
, "Start of Periodic Frame");
2669 case DWC3_DEVICE_EVENT_ERRATIC_ERROR
:
2670 dwc3_trace(trace_dwc3_gadget
, "Erratic Error");
2672 case DWC3_DEVICE_EVENT_CMD_CMPL
:
2673 dwc3_trace(trace_dwc3_gadget
, "Command Complete");
2675 case DWC3_DEVICE_EVENT_OVERFLOW
:
2676 dwc3_trace(trace_dwc3_gadget
, "Overflow");
2679 dev_WARN(dwc
->dev
, "UNKNOWN IRQ %d\n", event
->type
);
2683 static void dwc3_process_event_entry(struct dwc3
*dwc
,
2684 const union dwc3_event
*event
)
2686 trace_dwc3_event(event
->raw
);
2688 /* Endpoint IRQ, handle it and return early */
2689 if (event
->type
.is_devspec
== 0) {
2691 return dwc3_endpoint_interrupt(dwc
, &event
->depevt
);
2694 switch (event
->type
.type
) {
2695 case DWC3_EVENT_TYPE_DEV
:
2696 dwc3_gadget_interrupt(dwc
, &event
->devt
);
2698 /* REVISIT what to do with Carkit and I2C events ? */
2700 dev_err(dwc
->dev
, "UNKNOWN IRQ type %d\n", event
->raw
);
2704 static irqreturn_t
dwc3_process_event_buf(struct dwc3_event_buffer
*evt
)
2706 struct dwc3
*dwc
= evt
->dwc
;
2707 irqreturn_t ret
= IRQ_NONE
;
2713 if (!(evt
->flags
& DWC3_EVENT_PENDING
))
2717 union dwc3_event event
;
2719 event
.raw
= *(u32
*) (evt
->buf
+ evt
->lpos
);
2721 dwc3_process_event_entry(dwc
, &event
);
2724 * FIXME we wrap around correctly to the next entry as
2725 * almost all entries are 4 bytes in size. There is one
2726 * entry which has 12 bytes which is a regular entry
2727 * followed by 8 bytes data. ATM I don't know how
2728 * things are organized if we get next to the a
2729 * boundary so I worry about that once we try to handle
2732 evt
->lpos
= (evt
->lpos
+ 4) % DWC3_EVENT_BUFFERS_SIZE
;
2735 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(0), 4);
2739 evt
->flags
&= ~DWC3_EVENT_PENDING
;
2742 /* Unmask interrupt */
2743 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTSIZ(0));
2744 reg
&= ~DWC3_GEVNTSIZ_INTMASK
;
2745 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), reg
);
2750 static irqreturn_t
dwc3_thread_interrupt(int irq
, void *_evt
)
2752 struct dwc3_event_buffer
*evt
= _evt
;
2753 struct dwc3
*dwc
= evt
->dwc
;
2754 unsigned long flags
;
2755 irqreturn_t ret
= IRQ_NONE
;
2757 spin_lock_irqsave(&dwc
->lock
, flags
);
2758 ret
= dwc3_process_event_buf(evt
);
2759 spin_unlock_irqrestore(&dwc
->lock
, flags
);
2764 static irqreturn_t
dwc3_check_event_buf(struct dwc3_event_buffer
*evt
)
2766 struct dwc3
*dwc
= evt
->dwc
;
2770 count
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(0));
2771 count
&= DWC3_GEVNTCOUNT_MASK
;
2776 evt
->flags
|= DWC3_EVENT_PENDING
;
2778 /* Mask interrupt */
2779 reg
= dwc3_readl(dwc
->regs
, DWC3_GEVNTSIZ(0));
2780 reg
|= DWC3_GEVNTSIZ_INTMASK
;
2781 dwc3_writel(dwc
->regs
, DWC3_GEVNTSIZ(0), reg
);
2783 return IRQ_WAKE_THREAD
;
2786 static irqreturn_t
dwc3_interrupt(int irq
, void *_evt
)
2788 struct dwc3_event_buffer
*evt
= _evt
;
2790 return dwc3_check_event_buf(evt
);
2794 * dwc3_gadget_init - Initializes gadget related registers
2795 * @dwc: pointer to our controller context structure
2797 * Returns 0 on success otherwise negative errno.
2799 int dwc3_gadget_init(struct dwc3
*dwc
)
2803 dwc
->ctrl_req
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2804 &dwc
->ctrl_req_addr
, GFP_KERNEL
);
2805 if (!dwc
->ctrl_req
) {
2806 dev_err(dwc
->dev
, "failed to allocate ctrl request\n");
2811 dwc
->ep0_trb
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
) * 2,
2812 &dwc
->ep0_trb_addr
, GFP_KERNEL
);
2813 if (!dwc
->ep0_trb
) {
2814 dev_err(dwc
->dev
, "failed to allocate ep0 trb\n");
2819 dwc
->setup_buf
= kzalloc(DWC3_EP0_BOUNCE_SIZE
, GFP_KERNEL
);
2820 if (!dwc
->setup_buf
) {
2825 dwc
->ep0_bounce
= dma_alloc_coherent(dwc
->dev
,
2826 DWC3_EP0_BOUNCE_SIZE
, &dwc
->ep0_bounce_addr
,
2828 if (!dwc
->ep0_bounce
) {
2829 dev_err(dwc
->dev
, "failed to allocate ep0 bounce buffer\n");
2834 dwc
->zlp_buf
= kzalloc(DWC3_ZLP_BUF_SIZE
, GFP_KERNEL
);
2835 if (!dwc
->zlp_buf
) {
2840 dwc
->gadget
.ops
= &dwc3_gadget_ops
;
2841 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2842 dwc
->gadget
.sg_supported
= true;
2843 dwc
->gadget
.name
= "dwc3-gadget";
2844 dwc
->gadget
.is_otg
= dwc
->dr_mode
== USB_DR_MODE_OTG
;
2847 * FIXME We might be setting max_speed to <SUPER, however versions
2848 * <2.20a of dwc3 have an issue with metastability (documented
2849 * elsewhere in this driver) which tells us we can't set max speed to
2850 * anything lower than SUPER.
2852 * Because gadget.max_speed is only used by composite.c and function
2853 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2854 * to happen so we avoid sending SuperSpeed Capability descriptor
2855 * together with our BOS descriptor as that could confuse host into
2856 * thinking we can handle super speed.
2858 * Note that, in fact, we won't even support GetBOS requests when speed
2859 * is less than super speed because we don't have means, yet, to tell
2860 * composite.c that we are USB 2.0 + LPM ECN.
2862 if (dwc
->revision
< DWC3_REVISION_220A
)
2863 dwc3_trace(trace_dwc3_gadget
,
2864 "Changing max_speed on rev %08x\n",
2867 dwc
->gadget
.max_speed
= dwc
->maximum_speed
;
2870 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2873 dwc
->gadget
.quirk_ep_out_aligned_size
= true;
2876 * REVISIT: Here we should clear all pending IRQs to be
2877 * sure we're starting from a well known location.
2880 ret
= dwc3_gadget_init_endpoints(dwc
);
2884 ret
= usb_add_gadget_udc(dwc
->dev
, &dwc
->gadget
);
2886 dev_err(dwc
->dev
, "failed to register udc\n");
2893 kfree(dwc
->zlp_buf
);
2896 dwc3_gadget_free_endpoints(dwc
);
2897 dma_free_coherent(dwc
->dev
, DWC3_EP0_BOUNCE_SIZE
,
2898 dwc
->ep0_bounce
, dwc
->ep0_bounce_addr
);
2901 kfree(dwc
->setup_buf
);
2904 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2905 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2908 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2909 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2915 /* -------------------------------------------------------------------------- */
2917 void dwc3_gadget_exit(struct dwc3
*dwc
)
2919 usb_del_gadget_udc(&dwc
->gadget
);
2921 dwc3_gadget_free_endpoints(dwc
);
2923 dma_free_coherent(dwc
->dev
, DWC3_EP0_BOUNCE_SIZE
,
2924 dwc
->ep0_bounce
, dwc
->ep0_bounce_addr
);
2926 kfree(dwc
->setup_buf
);
2927 kfree(dwc
->zlp_buf
);
2929 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2930 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2932 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2933 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2936 int dwc3_gadget_suspend(struct dwc3
*dwc
)
2938 if (!dwc
->gadget_driver
)
2941 if (dwc
->pullups_connected
) {
2942 dwc3_gadget_disable_irq(dwc
);
2943 dwc3_gadget_run_stop(dwc
, true, true);
2946 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
2947 __dwc3_gadget_ep_disable(dwc
->eps
[1]);
2949 dwc
->dcfg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
2954 int dwc3_gadget_resume(struct dwc3
*dwc
)
2956 struct dwc3_ep
*dep
;
2959 if (!dwc
->gadget_driver
)
2962 /* Start with SuperSpeed Default */
2963 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
2966 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
, false,
2972 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
, NULL
, false,
2977 /* begin to receive SETUP packets */
2978 dwc
->ep0state
= EP0_SETUP_PHASE
;
2979 dwc3_ep0_out_start(dwc
);
2981 dwc3_writel(dwc
->regs
, DWC3_DCFG
, dwc
->dcfg
);
2983 if (dwc
->pullups_connected
) {
2984 dwc3_gadget_enable_irq(dwc
);
2985 dwc3_gadget_run_stop(dwc
, true, false);
2991 __dwc3_gadget_ep_disable(dwc
->eps
[0]);