1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Authors: Felipe Balbi <balbi@ti.com>,
8*4882a593Smuzhiyun * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/spinlock.h>
15*4882a593Smuzhiyun #include <linux/platform_device.h>
16*4882a593Smuzhiyun #include <linux/pm_runtime.h>
17*4882a593Smuzhiyun #include <linux/interrupt.h>
18*4882a593Smuzhiyun #include <linux/io.h>
19*4882a593Smuzhiyun #include <linux/list.h>
20*4882a593Smuzhiyun #include <linux/dma-mapping.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <linux/usb/ch9.h>
23*4882a593Smuzhiyun #include <linux/usb/gadget.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include "debug.h"
26*4882a593Smuzhiyun #include "core.h"
27*4882a593Smuzhiyun #include "gadget.h"
28*4882a593Smuzhiyun #include "io.h"
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
31*4882a593Smuzhiyun & ~((d)->interval - 1))
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun * dwc3_gadget_set_test_mode - enables usb2 test modes
35*4882a593Smuzhiyun * @dwc: pointer to our context structure
36*4882a593Smuzhiyun * @mode: the mode to set (J, K SE0 NAK, Force Enable)
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Caller should take care of locking. This function will return 0 on
39*4882a593Smuzhiyun * success or -EINVAL if wrong Test Selector is passed.
40*4882a593Smuzhiyun */
dwc3_gadget_set_test_mode(struct dwc3 * dwc,int mode)41*4882a593Smuzhiyun int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun u32 reg;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46*4882a593Smuzhiyun reg &= ~DWC3_DCTL_TSTCTRL_MASK;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun switch (mode) {
49*4882a593Smuzhiyun case USB_TEST_J:
50*4882a593Smuzhiyun case USB_TEST_K:
51*4882a593Smuzhiyun case USB_TEST_SE0_NAK:
52*4882a593Smuzhiyun case USB_TEST_PACKET:
53*4882a593Smuzhiyun case USB_TEST_FORCE_ENABLE:
54*4882a593Smuzhiyun reg |= mode << 1;
55*4882a593Smuzhiyun break;
56*4882a593Smuzhiyun default:
57*4882a593Smuzhiyun return -EINVAL;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun dwc3_gadget_dctl_write_safe(dwc, reg);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun return 0;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /**
66*4882a593Smuzhiyun * dwc3_gadget_get_link_state - gets current state of usb link
67*4882a593Smuzhiyun * @dwc: pointer to our context structure
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * Caller should take care of locking. This function will
70*4882a593Smuzhiyun * return the link state on success (>= 0) or -ETIMEDOUT.
71*4882a593Smuzhiyun */
dwc3_gadget_get_link_state(struct dwc3 * dwc)72*4882a593Smuzhiyun int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun u32 reg;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun return DWC3_DSTS_USBLNKST(reg);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /**
82*4882a593Smuzhiyun * dwc3_gadget_set_link_state - sets usb link to a particular state
83*4882a593Smuzhiyun * @dwc: pointer to our context structure
84*4882a593Smuzhiyun * @state: the state to put link into
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * Caller should take care of locking. This function will
87*4882a593Smuzhiyun * return 0 on success or -ETIMEDOUT.
88*4882a593Smuzhiyun */
dwc3_gadget_set_link_state(struct dwc3 * dwc,enum dwc3_link_state state)89*4882a593Smuzhiyun int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun int retries = 10000;
92*4882a593Smuzhiyun u32 reg;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * Wait until device controller is ready. Only applies to 1.94a and
96*4882a593Smuzhiyun * later RTL.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
99*4882a593Smuzhiyun while (--retries) {
100*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101*4882a593Smuzhiyun if (reg & DWC3_DSTS_DCNRD)
102*4882a593Smuzhiyun udelay(5);
103*4882a593Smuzhiyun else
104*4882a593Smuzhiyun break;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (retries <= 0)
108*4882a593Smuzhiyun return -ETIMEDOUT;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112*4882a593Smuzhiyun reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /* set no action before sending new link state change */
115*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* set requested state */
118*4882a593Smuzhiyun reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCTL, reg);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * The following code is racy when called from dwc3_gadget_wakeup,
123*4882a593Smuzhiyun * and is not needed, at least on newer versions
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
126*4882a593Smuzhiyun return 0;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* wait for a change in DSTS */
129*4882a593Smuzhiyun retries = 10000;
130*4882a593Smuzhiyun while (--retries) {
131*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (DWC3_DSTS_USBLNKST(reg) == state)
134*4882a593Smuzhiyun return 0;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun udelay(5);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun return -ETIMEDOUT;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun * dwc3_ep_inc_trb - increment a trb index.
144*4882a593Smuzhiyun * @index: Pointer to the TRB index to increment.
145*4882a593Smuzhiyun *
146*4882a593Smuzhiyun * The index should never point to the link TRB. After incrementing,
147*4882a593Smuzhiyun * if it is point to the link TRB, wrap around to the beginning. The
148*4882a593Smuzhiyun * link TRB is always at the last TRB entry.
149*4882a593Smuzhiyun */
dwc3_ep_inc_trb(u8 * index)150*4882a593Smuzhiyun static void dwc3_ep_inc_trb(u8 *index)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun (*index)++;
153*4882a593Smuzhiyun if (*index == (DWC3_TRB_NUM - 1))
154*4882a593Smuzhiyun *index = 0;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /**
158*4882a593Smuzhiyun * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
159*4882a593Smuzhiyun * @dep: The endpoint whose enqueue pointer we're incrementing
160*4882a593Smuzhiyun */
dwc3_ep_inc_enq(struct dwc3_ep * dep)161*4882a593Smuzhiyun static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun dwc3_ep_inc_trb(&dep->trb_enqueue);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /**
167*4882a593Smuzhiyun * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
168*4882a593Smuzhiyun * @dep: The endpoint whose enqueue pointer we're incrementing
169*4882a593Smuzhiyun */
dwc3_ep_inc_deq(struct dwc3_ep * dep)170*4882a593Smuzhiyun static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun dwc3_ep_inc_trb(&dep->trb_dequeue);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
dwc3_gadget_del_and_unmap_request(struct dwc3_ep * dep,struct dwc3_request * req,int status)175*4882a593Smuzhiyun static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
176*4882a593Smuzhiyun struct dwc3_request *req, int status)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun list_del(&req->list);
181*4882a593Smuzhiyun req->remaining = 0;
182*4882a593Smuzhiyun req->needs_extra_trb = false;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun if (req->request.status == -EINPROGRESS)
185*4882a593Smuzhiyun req->request.status = status;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (req->trb)
188*4882a593Smuzhiyun usb_gadget_unmap_request_by_dev(dwc->sysdev,
189*4882a593Smuzhiyun &req->request, req->direction);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun req->trb = NULL;
192*4882a593Smuzhiyun trace_dwc3_gadget_giveback(req);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (dep->number > 1)
195*4882a593Smuzhiyun pm_runtime_put(dwc->dev);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /**
199*4882a593Smuzhiyun * dwc3_gadget_giveback - call struct usb_request's ->complete callback
200*4882a593Smuzhiyun * @dep: The endpoint to whom the request belongs to
201*4882a593Smuzhiyun * @req: The request we're giving back
202*4882a593Smuzhiyun * @status: completion code for the request
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun * Must be called with controller's lock held and interrupts disabled. This
205*4882a593Smuzhiyun * function will unmap @req and call its ->complete() callback to notify upper
206*4882a593Smuzhiyun * layers that it has completed.
207*4882a593Smuzhiyun */
dwc3_gadget_giveback(struct dwc3_ep * dep,struct dwc3_request * req,int status)208*4882a593Smuzhiyun void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
209*4882a593Smuzhiyun int status)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun dwc3_gadget_del_and_unmap_request(dep, req, status);
214*4882a593Smuzhiyun req->status = DWC3_REQUEST_STATUS_COMPLETED;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun spin_unlock(&dwc->lock);
217*4882a593Smuzhiyun usb_gadget_giveback_request(&dep->endpoint, &req->request);
218*4882a593Smuzhiyun spin_lock(&dwc->lock);
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /**
222*4882a593Smuzhiyun * dwc3_send_gadget_generic_command - issue a generic command for the controller
223*4882a593Smuzhiyun * @dwc: pointer to the controller context
224*4882a593Smuzhiyun * @cmd: the command to be issued
225*4882a593Smuzhiyun * @param: command parameter
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * Caller should take care of locking. Issue @cmd with a given @param to @dwc
228*4882a593Smuzhiyun * and wait for its completion.
229*4882a593Smuzhiyun */
dwc3_send_gadget_generic_command(struct dwc3 * dwc,unsigned int cmd,u32 param)230*4882a593Smuzhiyun int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
231*4882a593Smuzhiyun u32 param)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun u32 timeout = 500;
234*4882a593Smuzhiyun int status = 0;
235*4882a593Smuzhiyun int ret = 0;
236*4882a593Smuzhiyun u32 reg;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
239*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun do {
242*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
243*4882a593Smuzhiyun if (!(reg & DWC3_DGCMD_CMDACT)) {
244*4882a593Smuzhiyun status = DWC3_DGCMD_STATUS(reg);
245*4882a593Smuzhiyun if (status)
246*4882a593Smuzhiyun ret = -EINVAL;
247*4882a593Smuzhiyun break;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun } while (--timeout);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun if (!timeout) {
252*4882a593Smuzhiyun ret = -ETIMEDOUT;
253*4882a593Smuzhiyun status = -ETIMEDOUT;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun trace_dwc3_gadget_generic_cmd(cmd, param, status);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun return ret;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /**
264*4882a593Smuzhiyun * dwc3_send_gadget_ep_cmd - issue an endpoint command
265*4882a593Smuzhiyun * @dep: the endpoint to which the command is going to be issued
266*4882a593Smuzhiyun * @cmd: the command to be issued
267*4882a593Smuzhiyun * @params: parameters to the command
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * Caller should handle locking. This function will issue @cmd with given
270*4882a593Smuzhiyun * @params to @dep and wait for its completion.
271*4882a593Smuzhiyun */
dwc3_send_gadget_ep_cmd(struct dwc3_ep * dep,unsigned int cmd,struct dwc3_gadget_ep_cmd_params * params)272*4882a593Smuzhiyun int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
273*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params *params)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
276*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
277*4882a593Smuzhiyun u32 timeout = 5000;
278*4882a593Smuzhiyun u32 saved_config = 0;
279*4882a593Smuzhiyun u32 reg;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun int cmd_status = 0;
282*4882a593Smuzhiyun int ret = -EINVAL;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /*
285*4882a593Smuzhiyun * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
286*4882a593Smuzhiyun * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
287*4882a593Smuzhiyun * endpoint command.
288*4882a593Smuzhiyun *
289*4882a593Smuzhiyun * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
290*4882a593Smuzhiyun * settings. Restore them after the command is completed.
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
293*4882a593Smuzhiyun */
294*4882a593Smuzhiyun if (dwc->gadget->speed <= USB_SPEED_HIGH ||
295*4882a593Smuzhiyun DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) {
296*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
297*4882a593Smuzhiyun if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
298*4882a593Smuzhiyun saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
299*4882a593Smuzhiyun reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
303*4882a593Smuzhiyun saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
304*4882a593Smuzhiyun reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if (saved_config)
308*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
312*4882a593Smuzhiyun int link_state;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /*
315*4882a593Smuzhiyun * Initiate remote wakeup if the link state is in U3 when
316*4882a593Smuzhiyun * operating in SS/SSP or L1/L2 when operating in HS/FS. If the
317*4882a593Smuzhiyun * link state is in U1/U2, no remote wakeup is needed. The Start
318*4882a593Smuzhiyun * Transfer command will initiate the link recovery.
319*4882a593Smuzhiyun */
320*4882a593Smuzhiyun link_state = dwc3_gadget_get_link_state(dwc);
321*4882a593Smuzhiyun switch (link_state) {
322*4882a593Smuzhiyun case DWC3_LINK_STATE_U2:
323*4882a593Smuzhiyun if (dwc->gadget->speed >= USB_SPEED_SUPER)
324*4882a593Smuzhiyun break;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun fallthrough;
327*4882a593Smuzhiyun case DWC3_LINK_STATE_U3:
328*4882a593Smuzhiyun ret = __dwc3_gadget_wakeup(dwc);
329*4882a593Smuzhiyun dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
330*4882a593Smuzhiyun ret);
331*4882a593Smuzhiyun break;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun * For some commands such as Update Transfer command, DEPCMDPARn
337*4882a593Smuzhiyun * registers are reserved. Since the driver often sends Update Transfer
338*4882a593Smuzhiyun * command, don't write to DEPCMDPARn to avoid register write delays and
339*4882a593Smuzhiyun * improve performance.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) {
342*4882a593Smuzhiyun dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
343*4882a593Smuzhiyun dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
344*4882a593Smuzhiyun dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
349*4882a593Smuzhiyun * not relying on XferNotReady, we can make use of a special "No
350*4882a593Smuzhiyun * Response Update Transfer" command where we should clear both CmdAct
351*4882a593Smuzhiyun * and CmdIOC bits.
352*4882a593Smuzhiyun *
353*4882a593Smuzhiyun * With this, we don't need to wait for command completion and can
354*4882a593Smuzhiyun * straight away issue further commands to the endpoint.
355*4882a593Smuzhiyun *
356*4882a593Smuzhiyun * NOTICE: We're making an assumption that control endpoints will never
357*4882a593Smuzhiyun * make use of Update Transfer command. This is a safe assumption
358*4882a593Smuzhiyun * because we can never have more than one request at a time with
359*4882a593Smuzhiyun * Control Endpoints. If anybody changes that assumption, this chunk
360*4882a593Smuzhiyun * needs to be updated accordingly.
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
363*4882a593Smuzhiyun !usb_endpoint_xfer_isoc(desc))
364*4882a593Smuzhiyun cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
365*4882a593Smuzhiyun else
366*4882a593Smuzhiyun cmd |= DWC3_DEPCMD_CMDACT;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun if (!(cmd & DWC3_DEPCMD_CMDACT) ||
371*4882a593Smuzhiyun (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
372*4882a593Smuzhiyun !(cmd & DWC3_DEPCMD_CMDIOC))) {
373*4882a593Smuzhiyun ret = 0;
374*4882a593Smuzhiyun goto skip_status;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun do {
378*4882a593Smuzhiyun reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
379*4882a593Smuzhiyun if (!(reg & DWC3_DEPCMD_CMDACT)) {
380*4882a593Smuzhiyun cmd_status = DWC3_DEPCMD_STATUS(reg);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun switch (cmd_status) {
383*4882a593Smuzhiyun case 0:
384*4882a593Smuzhiyun ret = 0;
385*4882a593Smuzhiyun break;
386*4882a593Smuzhiyun case DEPEVT_TRANSFER_NO_RESOURCE:
387*4882a593Smuzhiyun dev_WARN(dwc->dev, "No resource for %s\n",
388*4882a593Smuzhiyun dep->name);
389*4882a593Smuzhiyun ret = -EINVAL;
390*4882a593Smuzhiyun break;
391*4882a593Smuzhiyun case DEPEVT_TRANSFER_BUS_EXPIRY:
392*4882a593Smuzhiyun /*
393*4882a593Smuzhiyun * SW issues START TRANSFER command to
394*4882a593Smuzhiyun * isochronous ep with future frame interval. If
395*4882a593Smuzhiyun * future interval time has already passed when
396*4882a593Smuzhiyun * core receives the command, it will respond
397*4882a593Smuzhiyun * with an error status of 'Bus Expiry'.
398*4882a593Smuzhiyun *
399*4882a593Smuzhiyun * Instead of always returning -EINVAL, let's
400*4882a593Smuzhiyun * give a hint to the gadget driver that this is
401*4882a593Smuzhiyun * the case by returning -EAGAIN.
402*4882a593Smuzhiyun */
403*4882a593Smuzhiyun ret = -EAGAIN;
404*4882a593Smuzhiyun break;
405*4882a593Smuzhiyun default:
406*4882a593Smuzhiyun dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun break;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun } while (--timeout);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun if (timeout == 0) {
414*4882a593Smuzhiyun ret = -ETIMEDOUT;
415*4882a593Smuzhiyun cmd_status = -ETIMEDOUT;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun skip_status:
419*4882a593Smuzhiyun trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
422*4882a593Smuzhiyun if (ret == 0)
423*4882a593Smuzhiyun dep->flags |= DWC3_EP_TRANSFER_STARTED;
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun if (ret != -ETIMEDOUT)
426*4882a593Smuzhiyun dwc3_gadget_ep_get_transfer_index(dep);
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun if (saved_config) {
430*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
431*4882a593Smuzhiyun reg |= saved_config;
432*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun return ret;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dwc3_send_gadget_ep_cmd);
438*4882a593Smuzhiyun
dwc3_send_clear_stall_ep_cmd(struct dwc3_ep * dep)439*4882a593Smuzhiyun static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
442*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
443*4882a593Smuzhiyun u32 cmd = DWC3_DEPCMD_CLEARSTALL;
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /*
446*4882a593Smuzhiyun * As of core revision 2.60a the recommended programming model
447*4882a593Smuzhiyun * is to set the ClearPendIN bit when issuing a Clear Stall EP
448*4882a593Smuzhiyun * command for IN endpoints. This is to prevent an issue where
449*4882a593Smuzhiyun * some (non-compliant) hosts may not send ACK TPs for pending
450*4882a593Smuzhiyun * IN transfers due to a mishandled error condition. Synopsys
451*4882a593Smuzhiyun * STAR 9000614252.
452*4882a593Smuzhiyun */
453*4882a593Smuzhiyun if (dep->direction &&
454*4882a593Smuzhiyun !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
455*4882a593Smuzhiyun (dwc->gadget->speed >= USB_SPEED_SUPER))
456*4882a593Smuzhiyun cmd |= DWC3_DEPCMD_CLEARPENDIN;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun memset(¶ms, 0, sizeof(params));
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
dwc3_trb_dma_offset(struct dwc3_ep * dep,struct dwc3_trb * trb)463*4882a593Smuzhiyun static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
464*4882a593Smuzhiyun struct dwc3_trb *trb)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun u32 offset = (char *) trb - (char *) dep->trb_pool;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun return dep->trb_pool_dma + offset;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
dwc3_alloc_trb_pool(struct dwc3_ep * dep)471*4882a593Smuzhiyun static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun if (dep->trb_pool)
476*4882a593Smuzhiyun return 0;
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
479*4882a593Smuzhiyun sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
480*4882a593Smuzhiyun &dep->trb_pool_dma, GFP_KERNEL);
481*4882a593Smuzhiyun if (!dep->trb_pool) {
482*4882a593Smuzhiyun dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
483*4882a593Smuzhiyun dep->name);
484*4882a593Smuzhiyun return -ENOMEM;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun return 0;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun
dwc3_free_trb_pool(struct dwc3_ep * dep)490*4882a593Smuzhiyun static void dwc3_free_trb_pool(struct dwc3_ep *dep)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
495*4882a593Smuzhiyun dep->trb_pool, dep->trb_pool_dma);
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun dep->trb_pool = NULL;
498*4882a593Smuzhiyun dep->trb_pool_dma = 0;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun
dwc3_gadget_set_xfer_resource(struct dwc3_ep * dep)501*4882a593Smuzhiyun static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun memset(¶ms, 0x00, sizeof(params));
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
510*4882a593Smuzhiyun ¶ms);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun /**
514*4882a593Smuzhiyun * dwc3_gadget_start_config - configure ep resources
515*4882a593Smuzhiyun * @dep: endpoint that is being enabled
516*4882a593Smuzhiyun *
517*4882a593Smuzhiyun * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
518*4882a593Smuzhiyun * completion, it will set Transfer Resource for all available endpoints.
519*4882a593Smuzhiyun *
520*4882a593Smuzhiyun * The assignment of transfer resources cannot perfectly follow the data book
521*4882a593Smuzhiyun * due to the fact that the controller driver does not have all knowledge of the
522*4882a593Smuzhiyun * configuration in advance. It is given this information piecemeal by the
523*4882a593Smuzhiyun * composite gadget framework after every SET_CONFIGURATION and
524*4882a593Smuzhiyun * SET_INTERFACE. Trying to follow the databook programming model in this
525*4882a593Smuzhiyun * scenario can cause errors. For two reasons:
526*4882a593Smuzhiyun *
527*4882a593Smuzhiyun * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
528*4882a593Smuzhiyun * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
529*4882a593Smuzhiyun * incorrect in the scenario of multiple interfaces.
530*4882a593Smuzhiyun *
531*4882a593Smuzhiyun * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
532*4882a593Smuzhiyun * endpoint on alt setting (8.1.6).
533*4882a593Smuzhiyun *
534*4882a593Smuzhiyun * The following simplified method is used instead:
535*4882a593Smuzhiyun *
536*4882a593Smuzhiyun * All hardware endpoints can be assigned a transfer resource and this setting
537*4882a593Smuzhiyun * will stay persistent until either a core reset or hibernation. So whenever we
538*4882a593Smuzhiyun * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
539*4882a593Smuzhiyun * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
540*4882a593Smuzhiyun * guaranteed that there are as many transfer resources as endpoints.
541*4882a593Smuzhiyun *
542*4882a593Smuzhiyun * This function is called for each endpoint when it is being enabled but is
543*4882a593Smuzhiyun * triggered only when called for EP0-out, which always happens first, and which
544*4882a593Smuzhiyun * should only happen in one of the above conditions.
545*4882a593Smuzhiyun */
dwc3_gadget_start_config(struct dwc3_ep * dep)546*4882a593Smuzhiyun static int dwc3_gadget_start_config(struct dwc3_ep *dep)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
549*4882a593Smuzhiyun struct dwc3 *dwc;
550*4882a593Smuzhiyun u32 cmd;
551*4882a593Smuzhiyun int i;
552*4882a593Smuzhiyun int ret;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun if (dep->number)
555*4882a593Smuzhiyun return 0;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun memset(¶ms, 0x00, sizeof(params));
558*4882a593Smuzhiyun cmd = DWC3_DEPCMD_DEPSTARTCFG;
559*4882a593Smuzhiyun dwc = dep->dwc;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
562*4882a593Smuzhiyun if (ret)
563*4882a593Smuzhiyun return ret;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
566*4882a593Smuzhiyun struct dwc3_ep *dep = dwc->eps[i];
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun if (!dep)
569*4882a593Smuzhiyun continue;
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun ret = dwc3_gadget_set_xfer_resource(dep);
572*4882a593Smuzhiyun if (ret)
573*4882a593Smuzhiyun return ret;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun return 0;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun
dwc3_gadget_set_ep_config(struct dwc3_ep * dep,unsigned int action)579*4882a593Smuzhiyun static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun const struct usb_ss_ep_comp_descriptor *comp_desc;
582*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc;
583*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
584*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun comp_desc = dep->endpoint.comp_desc;
587*4882a593Smuzhiyun desc = dep->endpoint.desc;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun memset(¶ms, 0x00, sizeof(params));
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
592*4882a593Smuzhiyun | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun /* Burst size is only needed in SuperSpeed mode */
595*4882a593Smuzhiyun if (dwc->gadget->speed >= USB_SPEED_SUPER) {
596*4882a593Smuzhiyun u32 burst = dep->endpoint.maxburst;
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun params.param0 |= action;
602*4882a593Smuzhiyun if (action == DWC3_DEPCFG_ACTION_RESTORE)
603*4882a593Smuzhiyun params.param2 |= dep->saved_state;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun if (usb_endpoint_xfer_control(desc))
606*4882a593Smuzhiyun params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
609*4882a593Smuzhiyun params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
612*4882a593Smuzhiyun params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
613*4882a593Smuzhiyun | DWC3_DEPCFG_XFER_COMPLETE_EN
614*4882a593Smuzhiyun | DWC3_DEPCFG_STREAM_EVENT_EN;
615*4882a593Smuzhiyun dep->stream_capable = true;
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun if (!usb_endpoint_xfer_control(desc))
619*4882a593Smuzhiyun params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun * We are doing 1:1 mapping for endpoints, meaning
623*4882a593Smuzhiyun * Physical Endpoints 2 maps to Logical Endpoint 2 and
624*4882a593Smuzhiyun * so on. We consider the direction bit as part of the physical
625*4882a593Smuzhiyun * endpoint number. So USB endpoint 0x81 is 0x03.
626*4882a593Smuzhiyun */
627*4882a593Smuzhiyun params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun /*
630*4882a593Smuzhiyun * We must use the lower 16 TX FIFOs even though
631*4882a593Smuzhiyun * HW might have more
632*4882a593Smuzhiyun */
633*4882a593Smuzhiyun if (dep->direction)
634*4882a593Smuzhiyun params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun if (desc->bInterval) {
637*4882a593Smuzhiyun u8 bInterval_m1;
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun /*
640*4882a593Smuzhiyun * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
641*4882a593Smuzhiyun *
642*4882a593Smuzhiyun * NOTE: The programming guide incorrectly stated bInterval_m1
643*4882a593Smuzhiyun * must be set to 0 when operating in fullspeed. Internally the
644*4882a593Smuzhiyun * controller does not have this limitation. See DWC_usb3x
645*4882a593Smuzhiyun * programming guide section 3.2.2.1.
646*4882a593Smuzhiyun */
647*4882a593Smuzhiyun bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
650*4882a593Smuzhiyun dwc->gadget->speed == USB_SPEED_FULL)
651*4882a593Smuzhiyun dep->interval = desc->bInterval;
652*4882a593Smuzhiyun else
653*4882a593Smuzhiyun dep->interval = 1 << (desc->bInterval - 1);
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun /**
662*4882a593Smuzhiyun * dwc3_gadget_get_tx_fifos_size - Get the txfifos total size
663*4882a593Smuzhiyun * @dwc: pointer to the DWC3 context
664*4882a593Smuzhiyun *
665*4882a593Smuzhiyun * 3-RAM configuration:
666*4882a593Smuzhiyun * RAM0 depth = Descriptor Cache depth
667*4882a593Smuzhiyun * RAM1 depth = TxFIFOs depth
668*4882a593Smuzhiyun * RAM2 depth = RxFIFOs depth
669*4882a593Smuzhiyun *
670*4882a593Smuzhiyun * 2-RAM configuration:
671*4882a593Smuzhiyun * RAM0 depth = Descriptor Cache depth + RxFIFOs depth
672*4882a593Smuzhiyun * RAM1 depth = TxFIFOs depth
673*4882a593Smuzhiyun *
674*4882a593Smuzhiyun * 1-RAM configuration:
675*4882a593Smuzhiyun * RAM0 depth = Descriptor Cache depth + RxFIFOs depth + TxFIFOs depth
676*4882a593Smuzhiyun */
dwc3_gadget_get_tx_fifos_size(struct dwc3 * dwc)677*4882a593Smuzhiyun static int dwc3_gadget_get_tx_fifos_size(struct dwc3 *dwc)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun int txfifo_depth = 0;
680*4882a593Smuzhiyun int ram0_depth, rxfifo_size;
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun /* Get the depth of the TxFIFOs */
683*4882a593Smuzhiyun if (DWC3_NUM_RAMS(dwc->hwparams.hwparams1) > 1) {
684*4882a593Smuzhiyun /* For 2 or 3-RAM, RAM1 contains TxFIFOs */
685*4882a593Smuzhiyun txfifo_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
686*4882a593Smuzhiyun } else {
687*4882a593Smuzhiyun /* For 1-RAM, RAM0 contains Descriptor Cache, RxFIFOs, and TxFIFOs */
688*4882a593Smuzhiyun ram0_depth = DWC3_GHWPARAMS6_RAM0_DEPTH(dwc->hwparams.hwparams6);
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /* All OUT endpoints share a single RxFIFO space */
691*4882a593Smuzhiyun rxfifo_size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
692*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
693*4882a593Smuzhiyun txfifo_depth = ram0_depth - DWC3_GRXFIFOSIZ_RXFDEP(rxfifo_size);
694*4882a593Smuzhiyun else
695*4882a593Smuzhiyun txfifo_depth = ram0_depth - DWC31_GRXFIFOSIZ_RXFDEP(rxfifo_size);
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun /* The value of GRxFIFOSIZ0[31:16] is the depth of Descriptor Cache */
698*4882a593Smuzhiyun txfifo_depth -= DWC3_GRXFIFOSIZ_RXFSTADDR(rxfifo_size) >> 16;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun return txfifo_depth;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun /**
705*4882a593Smuzhiyun * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
706*4882a593Smuzhiyun * @dwc: pointer to the DWC3 context
707*4882a593Smuzhiyun * @nfifos: number of fifos to calculate for
708*4882a593Smuzhiyun *
709*4882a593Smuzhiyun * Calculates the size value based on the equation below:
710*4882a593Smuzhiyun *
711*4882a593Smuzhiyun * DWC3 revision 280A and prior:
712*4882a593Smuzhiyun * fifo_size = mult * (max_packet / mdwidth) + 1;
713*4882a593Smuzhiyun *
714*4882a593Smuzhiyun * DWC3 revision 290A and onwards:
715*4882a593Smuzhiyun * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
716*4882a593Smuzhiyun *
717*4882a593Smuzhiyun * The max packet size is set to 1024, as the txfifo requirements mainly apply
718*4882a593Smuzhiyun * to super speed USB use cases. However, it is safe to overestimate the fifo
719*4882a593Smuzhiyun * allocations for other scenarios, i.e. high speed USB.
720*4882a593Smuzhiyun */
dwc3_gadget_calc_tx_fifo_size(struct dwc3 * dwc,int mult)721*4882a593Smuzhiyun static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun int max_packet = 1024;
724*4882a593Smuzhiyun int fifo_size;
725*4882a593Smuzhiyun int mdwidth;
726*4882a593Smuzhiyun
727*4882a593Smuzhiyun mdwidth = dwc3_mdwidth(dwc);
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun /* MDWIDTH is represented in bits, we need it in bytes */
730*4882a593Smuzhiyun mdwidth >>= 3;
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 290A))
733*4882a593Smuzhiyun fifo_size = mult * (max_packet / mdwidth) + 1;
734*4882a593Smuzhiyun else
735*4882a593Smuzhiyun fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1;
736*4882a593Smuzhiyun return fifo_size;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun /**
740*4882a593Smuzhiyun * dwc3_gadget_clear_tx_fifo_size - Clears txfifo allocation
741*4882a593Smuzhiyun * @dwc: pointer to the DWC3 context
742*4882a593Smuzhiyun *
743*4882a593Smuzhiyun * Iterates through all the endpoint registers and clears the previous txfifo
744*4882a593Smuzhiyun * allocations.
745*4882a593Smuzhiyun */
dwc3_gadget_clear_tx_fifos(struct dwc3 * dwc)746*4882a593Smuzhiyun void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun struct dwc3_ep *dep;
749*4882a593Smuzhiyun int fifo_depth;
750*4882a593Smuzhiyun int size;
751*4882a593Smuzhiyun int num;
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun if (!dwc->do_fifo_resize)
754*4882a593Smuzhiyun return;
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun /* Read ep0IN related TXFIFO size */
757*4882a593Smuzhiyun dep = dwc->eps[1];
758*4882a593Smuzhiyun size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
759*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
760*4882a593Smuzhiyun fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size);
761*4882a593Smuzhiyun else
762*4882a593Smuzhiyun fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size);
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun dwc->last_fifo_depth = fifo_depth;
765*4882a593Smuzhiyun /* Clear existing TXFIFO for all IN eps except ep0 */
766*4882a593Smuzhiyun for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM);
767*4882a593Smuzhiyun num += 2) {
768*4882a593Smuzhiyun dep = dwc->eps[num];
769*4882a593Smuzhiyun /* Don't change TXFRAMNUM on usb31 version */
770*4882a593Smuzhiyun size = DWC3_IP_IS(DWC3) ? 0 :
771*4882a593Smuzhiyun dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) &
772*4882a593Smuzhiyun DWC31_GTXFIFOSIZ_TXFRAMNUM;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size);
775*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_TXFIFO_RESIZED;
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun dwc->num_ep_resized = 0;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun /**
781*4882a593Smuzhiyun * __dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for Rockchip platform
782*4882a593Smuzhiyun *
783*4882a593Smuzhiyun * @dep: pointer to dwc3_ep structure
784*4882a593Smuzhiyun *
785*4882a593Smuzhiyun * According to the different USB transfer type and Speed,
786*4882a593Smuzhiyun * this function will a best effort FIFO allocation in order
787*4882a593Smuzhiyun * to improve FIFO usage and throughput, while still allowing
788*4882a593Smuzhiyun * us to enable as many endpoints as possible.
789*4882a593Smuzhiyun */
__dwc3_gadget_resize_tx_fifos(struct dwc3_ep * dep)790*4882a593Smuzhiyun static int __dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
793*4882a593Smuzhiyun u32 fifo_0_start, last_fifo_depth, ram1_depth;
794*4882a593Smuzhiyun u32 fifo_size, maxpacket, mdwidth, mult;
795*4882a593Smuzhiyun u32 tmp;
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
798*4882a593Smuzhiyun /*
799*4882a593Smuzhiyun * Set enough tx fifos for Isochronous endpoints to get better
800*4882a593Smuzhiyun * performance and more compliance with bus latency.
801*4882a593Smuzhiyun */
802*4882a593Smuzhiyun maxpacket = dep->endpoint.maxpacket;
803*4882a593Smuzhiyun if (gadget_is_superspeed(dwc->gadget))
804*4882a593Smuzhiyun mult = dep->endpoint.mult * dep->endpoint.maxburst;
805*4882a593Smuzhiyun else
806*4882a593Smuzhiyun mult = dep->endpoint.mult;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun mult = mult > 0 ? mult * 2 : 3;
809*4882a593Smuzhiyun if (mult > 6)
810*4882a593Smuzhiyun mult = 6;
811*4882a593Smuzhiyun } else if (usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
812*4882a593Smuzhiyun /*
813*4882a593Smuzhiyun * Set enough tx fifos for Bulk endpoints to get
814*4882a593Smuzhiyun * better transmission performance.
815*4882a593Smuzhiyun */
816*4882a593Smuzhiyun mult = 3;
817*4882a593Smuzhiyun if (gadget_is_superspeed(dwc->gadget)) {
818*4882a593Smuzhiyun if (dep->endpoint.maxburst > mult) {
819*4882a593Smuzhiyun mult = dep->endpoint.maxburst;
820*4882a593Smuzhiyun if (mult > 6)
821*4882a593Smuzhiyun mult = 6;
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun maxpacket = 1024;
824*4882a593Smuzhiyun } else {
825*4882a593Smuzhiyun maxpacket = 512;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun } else if (usb_endpoint_xfer_int(dep->endpoint.desc)) {
828*4882a593Smuzhiyun /*
829*4882a593Smuzhiyun * REVIST: we assume that the maxpacket of interrupt
830*4882a593Smuzhiyun * endpoint is 64 Bytes for MTP and the other functions.
831*4882a593Smuzhiyun */
832*4882a593Smuzhiyun mult = 1;
833*4882a593Smuzhiyun maxpacket = 64;
834*4882a593Smuzhiyun } else {
835*4882a593Smuzhiyun goto out;
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun mdwidth = dwc3_mdwidth(dwc);
839*4882a593Smuzhiyun mdwidth >>= 3; /* bits convert to bytes */
840*4882a593Smuzhiyun ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
841*4882a593Smuzhiyun last_fifo_depth = dwc->last_fifo_depth;
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun /* Calculate the fifo size for this EP */
844*4882a593Smuzhiyun tmp = mult * (maxpacket + mdwidth);
845*4882a593Smuzhiyun tmp += mdwidth;
846*4882a593Smuzhiyun fifo_size = DIV_ROUND_UP(tmp, mdwidth);
847*4882a593Smuzhiyun
848*4882a593Smuzhiyun /* Check if TXFIFOs start at non-zero addr */
849*4882a593Smuzhiyun tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
850*4882a593Smuzhiyun fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
851*4882a593Smuzhiyun fifo_size |= (fifo_0_start + (last_fifo_depth << 16));
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
854*4882a593Smuzhiyun last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
855*4882a593Smuzhiyun else
856*4882a593Smuzhiyun last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun /* Check fifo size allocation doesn't exceed available RAM size. */
859*4882a593Smuzhiyun if (last_fifo_depth >= ram1_depth) {
860*4882a593Smuzhiyun dev_err(dwc->dev, "Fifosize(0x%x) > RAM size(0x%x) %s depth(0x%x)\n",
861*4882a593Smuzhiyun last_fifo_depth, ram1_depth,
862*4882a593Smuzhiyun dep->endpoint.name, fifo_size & 0xfff);
863*4882a593Smuzhiyun return -ENOMEM;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
867*4882a593Smuzhiyun dep->flags |= DWC3_EP_TXFIFO_RESIZED;
868*4882a593Smuzhiyun dwc->last_fifo_depth = last_fifo_depth;
869*4882a593Smuzhiyun dwc->num_ep_resized++;
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun out:
872*4882a593Smuzhiyun return 0;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun /*
876*4882a593Smuzhiyun * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
877*4882a593Smuzhiyun * @dwc: pointer to our context structure
878*4882a593Smuzhiyun *
879*4882a593Smuzhiyun * This function will a best effort FIFO allocation in order
880*4882a593Smuzhiyun * to improve FIFO usage and throughput, while still allowing
881*4882a593Smuzhiyun * us to enable as many endpoints as possible.
882*4882a593Smuzhiyun *
883*4882a593Smuzhiyun * Keep in mind that this operation will be highly dependent
884*4882a593Smuzhiyun * on the configured size for RAM1 - which contains TxFifo -,
885*4882a593Smuzhiyun * the amount of endpoints enabled on coreConsultant tool, and
886*4882a593Smuzhiyun * the width of the Master Bus.
887*4882a593Smuzhiyun *
888*4882a593Smuzhiyun * In general, FIFO depths are represented with the following equation:
889*4882a593Smuzhiyun *
890*4882a593Smuzhiyun * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
891*4882a593Smuzhiyun *
892*4882a593Smuzhiyun * In conjunction with dwc3_gadget_check_config(), this resizing logic will
893*4882a593Smuzhiyun * ensure that all endpoints will have enough internal memory for one max
894*4882a593Smuzhiyun * packet per endpoint.
895*4882a593Smuzhiyun */
dwc3_gadget_resize_tx_fifos(struct dwc3_ep * dep)896*4882a593Smuzhiyun static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
897*4882a593Smuzhiyun {
898*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
899*4882a593Smuzhiyun int fifo_0_start;
900*4882a593Smuzhiyun int ram1_depth;
901*4882a593Smuzhiyun int fifo_size;
902*4882a593Smuzhiyun int min_depth;
903*4882a593Smuzhiyun int num_in_ep;
904*4882a593Smuzhiyun int remaining;
905*4882a593Smuzhiyun int num_fifos = 1;
906*4882a593Smuzhiyun int fifo;
907*4882a593Smuzhiyun int tmp;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun if (!dwc->do_fifo_resize)
910*4882a593Smuzhiyun return 0;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun /* resize IN endpoints except ep0 */
913*4882a593Smuzhiyun if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1)
914*4882a593Smuzhiyun return 0;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun /* bail if already resized */
917*4882a593Smuzhiyun if (dep->flags & DWC3_EP_TXFIFO_RESIZED)
918*4882a593Smuzhiyun return 0;
919*4882a593Smuzhiyun
920*4882a593Smuzhiyun if (IS_REACHABLE(CONFIG_ARCH_ROCKCHIP))
921*4882a593Smuzhiyun return __dwc3_gadget_resize_tx_fifos(dep);
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun if ((dep->endpoint.maxburst > 1 &&
926*4882a593Smuzhiyun usb_endpoint_xfer_bulk(dep->endpoint.desc)) ||
927*4882a593Smuzhiyun usb_endpoint_xfer_isoc(dep->endpoint.desc))
928*4882a593Smuzhiyun num_fifos = 3;
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun if (dep->endpoint.maxburst > 6 &&
931*4882a593Smuzhiyun (usb_endpoint_xfer_bulk(dep->endpoint.desc) ||
932*4882a593Smuzhiyun usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31))
933*4882a593Smuzhiyun num_fifos = dwc->tx_fifo_resize_max_num;
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun /* FIFO size for a single buffer */
936*4882a593Smuzhiyun fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1);
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun /* Calculate the number of remaining EPs w/o any FIFO */
939*4882a593Smuzhiyun num_in_ep = dwc->max_cfg_eps;
940*4882a593Smuzhiyun num_in_ep -= dwc->num_ep_resized;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun /* Reserve at least one FIFO for the number of IN EPs */
943*4882a593Smuzhiyun min_depth = num_in_ep * (fifo + 1);
944*4882a593Smuzhiyun remaining = ram1_depth - min_depth - dwc->last_fifo_depth;
945*4882a593Smuzhiyun remaining = max_t(int, 0, remaining);
946*4882a593Smuzhiyun /*
947*4882a593Smuzhiyun * We've already reserved 1 FIFO per EP, so check what we can fit in
948*4882a593Smuzhiyun * addition to it. If there is not enough remaining space, allocate
949*4882a593Smuzhiyun * all the remaining space to the EP.
950*4882a593Smuzhiyun */
951*4882a593Smuzhiyun fifo_size = (num_fifos - 1) * fifo;
952*4882a593Smuzhiyun if (remaining < fifo_size)
953*4882a593Smuzhiyun fifo_size = remaining;
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun fifo_size += fifo;
956*4882a593Smuzhiyun /* Last increment according to the TX FIFO size equation */
957*4882a593Smuzhiyun fifo_size++;
958*4882a593Smuzhiyun
959*4882a593Smuzhiyun /* Check if TXFIFOs start at non-zero addr */
960*4882a593Smuzhiyun tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
961*4882a593Smuzhiyun fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16));
964*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
965*4882a593Smuzhiyun dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
966*4882a593Smuzhiyun else
967*4882a593Smuzhiyun dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun /* Check fifo size allocation doesn't exceed available RAM size. */
970*4882a593Smuzhiyun if (dwc->last_fifo_depth >= ram1_depth) {
971*4882a593Smuzhiyun dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
972*4882a593Smuzhiyun dwc->last_fifo_depth, ram1_depth,
973*4882a593Smuzhiyun dep->endpoint.name, fifo_size);
974*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
975*4882a593Smuzhiyun fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
976*4882a593Smuzhiyun else
977*4882a593Smuzhiyun fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun dwc->last_fifo_depth -= fifo_size;
980*4882a593Smuzhiyun return -ENOMEM;
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
984*4882a593Smuzhiyun dep->flags |= DWC3_EP_TXFIFO_RESIZED;
985*4882a593Smuzhiyun dwc->num_ep_resized++;
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun return 0;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun /**
991*4882a593Smuzhiyun * __dwc3_gadget_ep_enable - initializes a hw endpoint
992*4882a593Smuzhiyun * @dep: endpoint to be initialized
993*4882a593Smuzhiyun * @action: one of INIT, MODIFY or RESTORE
994*4882a593Smuzhiyun *
995*4882a593Smuzhiyun * Caller should take care of locking. Execute all necessary commands to
996*4882a593Smuzhiyun * initialize a HW endpoint so it can be used by a gadget driver.
997*4882a593Smuzhiyun */
__dwc3_gadget_ep_enable(struct dwc3_ep * dep,unsigned int action)998*4882a593Smuzhiyun static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
999*4882a593Smuzhiyun {
1000*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
1001*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
1002*4882a593Smuzhiyun
1003*4882a593Smuzhiyun u32 reg;
1004*4882a593Smuzhiyun int ret;
1005*4882a593Smuzhiyun
1006*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_ENABLED)) {
1007*4882a593Smuzhiyun ret = dwc3_gadget_resize_tx_fifos(dep);
1008*4882a593Smuzhiyun if (ret)
1009*4882a593Smuzhiyun return ret;
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun ret = dwc3_gadget_start_config(dep);
1012*4882a593Smuzhiyun if (ret)
1013*4882a593Smuzhiyun return ret;
1014*4882a593Smuzhiyun }
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun ret = dwc3_gadget_set_ep_config(dep, action);
1017*4882a593Smuzhiyun if (ret)
1018*4882a593Smuzhiyun return ret;
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_ENABLED)) {
1021*4882a593Smuzhiyun struct dwc3_trb *trb_st_hw;
1022*4882a593Smuzhiyun struct dwc3_trb *trb_link;
1023*4882a593Smuzhiyun
1024*4882a593Smuzhiyun dep->type = usb_endpoint_type(desc);
1025*4882a593Smuzhiyun dep->flags |= DWC3_EP_ENABLED;
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
1028*4882a593Smuzhiyun reg |= DWC3_DALEPENA_EP(dep->number);
1029*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun dep->trb_dequeue = 0;
1032*4882a593Smuzhiyun dep->trb_enqueue = 0;
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun if (usb_endpoint_xfer_control(desc))
1035*4882a593Smuzhiyun goto out;
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun /* Initialize the TRB ring */
1038*4882a593Smuzhiyun memset(dep->trb_pool, 0,
1039*4882a593Smuzhiyun sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun /* Link TRB. The HWO bit is never reset */
1042*4882a593Smuzhiyun trb_st_hw = &dep->trb_pool[0];
1043*4882a593Smuzhiyun
1044*4882a593Smuzhiyun trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
1045*4882a593Smuzhiyun trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
1046*4882a593Smuzhiyun trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
1047*4882a593Smuzhiyun trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
1048*4882a593Smuzhiyun trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun /*
1052*4882a593Smuzhiyun * Issue StartTransfer here with no-op TRB so we can always rely on No
1053*4882a593Smuzhiyun * Response Update Transfer command.
1054*4882a593Smuzhiyun */
1055*4882a593Smuzhiyun if (usb_endpoint_xfer_bulk(desc) ||
1056*4882a593Smuzhiyun usb_endpoint_xfer_int(desc)) {
1057*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
1058*4882a593Smuzhiyun struct dwc3_trb *trb;
1059*4882a593Smuzhiyun dma_addr_t trb_dma;
1060*4882a593Smuzhiyun u32 cmd;
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun memset(¶ms, 0, sizeof(params));
1063*4882a593Smuzhiyun trb = &dep->trb_pool[0];
1064*4882a593Smuzhiyun trb_dma = dwc3_trb_dma_offset(dep, trb);
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun params.param0 = upper_32_bits(trb_dma);
1067*4882a593Smuzhiyun params.param1 = lower_32_bits(trb_dma);
1068*4882a593Smuzhiyun
1069*4882a593Smuzhiyun cmd = DWC3_DEPCMD_STARTTRANSFER;
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1072*4882a593Smuzhiyun if (ret < 0)
1073*4882a593Smuzhiyun return ret;
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun if (dep->stream_capable) {
1076*4882a593Smuzhiyun /*
1077*4882a593Smuzhiyun * For streams, at start, there maybe a race where the
1078*4882a593Smuzhiyun * host primes the endpoint before the function driver
1079*4882a593Smuzhiyun * queues a request to initiate a stream. In that case,
1080*4882a593Smuzhiyun * the controller will not see the prime to generate the
1081*4882a593Smuzhiyun * ERDY and start stream. To workaround this, issue a
1082*4882a593Smuzhiyun * no-op TRB as normal, but end it immediately. As a
1083*4882a593Smuzhiyun * result, when the function driver queues the request,
1084*4882a593Smuzhiyun * the next START_TRANSFER command will cause the
1085*4882a593Smuzhiyun * controller to generate an ERDY to initiate the
1086*4882a593Smuzhiyun * stream.
1087*4882a593Smuzhiyun */
1088*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, true);
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun /*
1091*4882a593Smuzhiyun * All stream eps will reinitiate stream on NoStream
1092*4882a593Smuzhiyun * rejection until we can determine that the host can
1093*4882a593Smuzhiyun * prime after the first transfer.
1094*4882a593Smuzhiyun *
1095*4882a593Smuzhiyun * However, if the controller is capable of
1096*4882a593Smuzhiyun * TXF_FLUSH_BYPASS, then IN direction endpoints will
1097*4882a593Smuzhiyun * automatically restart the stream without the driver
1098*4882a593Smuzhiyun * initiation.
1099*4882a593Smuzhiyun */
1100*4882a593Smuzhiyun if (!dep->direction ||
1101*4882a593Smuzhiyun !(dwc->hwparams.hwparams9 &
1102*4882a593Smuzhiyun DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
1103*4882a593Smuzhiyun dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun }
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun out:
1108*4882a593Smuzhiyun trace_dwc3_gadget_ep_enable(dep);
1109*4882a593Smuzhiyun
1110*4882a593Smuzhiyun return 0;
1111*4882a593Smuzhiyun }
1112*4882a593Smuzhiyun
dwc3_remove_requests(struct dwc3 * dwc,struct dwc3_ep * dep,int status)1113*4882a593Smuzhiyun void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun struct dwc3_request *req;
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, false);
1118*4882a593Smuzhiyun
1119*4882a593Smuzhiyun /* If endxfer is delayed, avoid unmapping requests */
1120*4882a593Smuzhiyun if (dep->flags & DWC3_EP_DELAY_STOP)
1121*4882a593Smuzhiyun return;
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun /* - giveback all requests to gadget driver */
1124*4882a593Smuzhiyun while (!list_empty(&dep->started_list)) {
1125*4882a593Smuzhiyun req = next_request(&dep->started_list);
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, status);
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun while (!list_empty(&dep->pending_list)) {
1131*4882a593Smuzhiyun req = next_request(&dep->pending_list);
1132*4882a593Smuzhiyun
1133*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, status);
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun while (!list_empty(&dep->cancelled_list)) {
1137*4882a593Smuzhiyun req = next_request(&dep->cancelled_list);
1138*4882a593Smuzhiyun
1139*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, status);
1140*4882a593Smuzhiyun }
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun /**
1144*4882a593Smuzhiyun * __dwc3_gadget_ep_disable - disables a hw endpoint
1145*4882a593Smuzhiyun * @dep: the endpoint to disable
1146*4882a593Smuzhiyun *
1147*4882a593Smuzhiyun * This function undoes what __dwc3_gadget_ep_enable did and also removes
1148*4882a593Smuzhiyun * requests which are currently being processed by the hardware and those which
1149*4882a593Smuzhiyun * are not yet scheduled.
1150*4882a593Smuzhiyun *
1151*4882a593Smuzhiyun * Caller should take care of locking.
1152*4882a593Smuzhiyun */
__dwc3_gadget_ep_disable(struct dwc3_ep * dep)1153*4882a593Smuzhiyun static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
1154*4882a593Smuzhiyun {
1155*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
1156*4882a593Smuzhiyun u32 reg;
1157*4882a593Smuzhiyun u32 mask;
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun trace_dwc3_gadget_ep_disable(dep);
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun /* make sure HW endpoint isn't stalled */
1162*4882a593Smuzhiyun if (dep->flags & DWC3_EP_STALL)
1163*4882a593Smuzhiyun __dwc3_gadget_ep_set_halt(dep, 0, false);
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
1166*4882a593Smuzhiyun reg &= ~DWC3_DALEPENA_EP(dep->number);
1167*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun dep->stream_capable = false;
1172*4882a593Smuzhiyun dep->type = 0;
1173*4882a593Smuzhiyun mask = DWC3_EP_TXFIFO_RESIZED;
1174*4882a593Smuzhiyun /*
1175*4882a593Smuzhiyun * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is
1176*4882a593Smuzhiyun * set. Do not clear DEP flags, so that the end transfer command will
1177*4882a593Smuzhiyun * be reattempted during the next SETUP stage.
1178*4882a593Smuzhiyun */
1179*4882a593Smuzhiyun if (dep->flags & DWC3_EP_DELAY_STOP)
1180*4882a593Smuzhiyun mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
1181*4882a593Smuzhiyun dep->flags &= mask;
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun /* Clear out the ep descriptors for non-ep0 */
1184*4882a593Smuzhiyun if (dep->number > 1) {
1185*4882a593Smuzhiyun dep->endpoint.comp_desc = NULL;
1186*4882a593Smuzhiyun dep->endpoint.desc = NULL;
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun return 0;
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun /* -------------------------------------------------------------------------- */
1193*4882a593Smuzhiyun
dwc3_gadget_ep0_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)1194*4882a593Smuzhiyun static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
1195*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc)
1196*4882a593Smuzhiyun {
1197*4882a593Smuzhiyun return -EINVAL;
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun
dwc3_gadget_ep0_disable(struct usb_ep * ep)1200*4882a593Smuzhiyun static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
1201*4882a593Smuzhiyun {
1202*4882a593Smuzhiyun return -EINVAL;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun /* -------------------------------------------------------------------------- */
1206*4882a593Smuzhiyun
dwc3_gadget_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)1207*4882a593Smuzhiyun static int dwc3_gadget_ep_enable(struct usb_ep *ep,
1208*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc)
1209*4882a593Smuzhiyun {
1210*4882a593Smuzhiyun struct dwc3_ep *dep;
1211*4882a593Smuzhiyun struct dwc3 *dwc;
1212*4882a593Smuzhiyun unsigned long flags;
1213*4882a593Smuzhiyun int ret;
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
1216*4882a593Smuzhiyun pr_debug("dwc3: invalid parameters\n");
1217*4882a593Smuzhiyun return -EINVAL;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun if (!desc->wMaxPacketSize) {
1221*4882a593Smuzhiyun pr_debug("dwc3: missing wMaxPacketSize\n");
1222*4882a593Smuzhiyun return -EINVAL;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun dep = to_dwc3_ep(ep);
1226*4882a593Smuzhiyun dwc = dep->dwc;
1227*4882a593Smuzhiyun
1228*4882a593Smuzhiyun if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
1229*4882a593Smuzhiyun "%s is already enabled\n",
1230*4882a593Smuzhiyun dep->name))
1231*4882a593Smuzhiyun return 0;
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
1234*4882a593Smuzhiyun ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1235*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun return ret;
1238*4882a593Smuzhiyun }
1239*4882a593Smuzhiyun
dwc3_gadget_ep_disable(struct usb_ep * ep)1240*4882a593Smuzhiyun static int dwc3_gadget_ep_disable(struct usb_ep *ep)
1241*4882a593Smuzhiyun {
1242*4882a593Smuzhiyun struct dwc3_ep *dep;
1243*4882a593Smuzhiyun struct dwc3 *dwc;
1244*4882a593Smuzhiyun unsigned long flags;
1245*4882a593Smuzhiyun int ret;
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun if (!ep) {
1248*4882a593Smuzhiyun pr_debug("dwc3: invalid parameters\n");
1249*4882a593Smuzhiyun return -EINVAL;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun dep = to_dwc3_ep(ep);
1253*4882a593Smuzhiyun dwc = dep->dwc;
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
1256*4882a593Smuzhiyun "%s is already disabled\n",
1257*4882a593Smuzhiyun dep->name))
1258*4882a593Smuzhiyun return 0;
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
1261*4882a593Smuzhiyun ret = __dwc3_gadget_ep_disable(dep);
1262*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun return ret;
1265*4882a593Smuzhiyun }
1266*4882a593Smuzhiyun
dwc3_gadget_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)1267*4882a593Smuzhiyun static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
1268*4882a593Smuzhiyun gfp_t gfp_flags)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun struct dwc3_request *req;
1271*4882a593Smuzhiyun struct dwc3_ep *dep = to_dwc3_ep(ep);
1272*4882a593Smuzhiyun
1273*4882a593Smuzhiyun req = kzalloc(sizeof(*req), gfp_flags);
1274*4882a593Smuzhiyun if (!req)
1275*4882a593Smuzhiyun return NULL;
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun req->direction = dep->direction;
1278*4882a593Smuzhiyun req->epnum = dep->number;
1279*4882a593Smuzhiyun req->dep = dep;
1280*4882a593Smuzhiyun req->status = DWC3_REQUEST_STATUS_UNKNOWN;
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun trace_dwc3_alloc_request(req);
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun return &req->request;
1285*4882a593Smuzhiyun }
1286*4882a593Smuzhiyun
dwc3_gadget_ep_free_request(struct usb_ep * ep,struct usb_request * request)1287*4882a593Smuzhiyun static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
1288*4882a593Smuzhiyun struct usb_request *request)
1289*4882a593Smuzhiyun {
1290*4882a593Smuzhiyun struct dwc3_request *req = to_dwc3_request(request);
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun trace_dwc3_free_request(req);
1293*4882a593Smuzhiyun kfree(req);
1294*4882a593Smuzhiyun }
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun /**
1297*4882a593Smuzhiyun * dwc3_ep_prev_trb - returns the previous TRB in the ring
1298*4882a593Smuzhiyun * @dep: The endpoint with the TRB ring
1299*4882a593Smuzhiyun * @index: The index of the current TRB in the ring
1300*4882a593Smuzhiyun *
1301*4882a593Smuzhiyun * Returns the TRB prior to the one pointed to by the index. If the
1302*4882a593Smuzhiyun * index is 0, we will wrap backwards, skip the link TRB, and return
1303*4882a593Smuzhiyun * the one just before that.
1304*4882a593Smuzhiyun */
dwc3_ep_prev_trb(struct dwc3_ep * dep,u8 index)1305*4882a593Smuzhiyun static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1306*4882a593Smuzhiyun {
1307*4882a593Smuzhiyun u8 tmp = index;
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun if (!tmp)
1310*4882a593Smuzhiyun tmp = DWC3_TRB_NUM - 1;
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun return &dep->trb_pool[tmp - 1];
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun
dwc3_calc_trbs_left(struct dwc3_ep * dep)1315*4882a593Smuzhiyun static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1316*4882a593Smuzhiyun {
1317*4882a593Smuzhiyun u8 trbs_left;
1318*4882a593Smuzhiyun
1319*4882a593Smuzhiyun /*
1320*4882a593Smuzhiyun * If the enqueue & dequeue are equal then the TRB ring is either full
1321*4882a593Smuzhiyun * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs
1322*4882a593Smuzhiyun * pending to be processed by the driver.
1323*4882a593Smuzhiyun */
1324*4882a593Smuzhiyun if (dep->trb_enqueue == dep->trb_dequeue) {
1325*4882a593Smuzhiyun /*
1326*4882a593Smuzhiyun * If there is any request remained in the started_list at
1327*4882a593Smuzhiyun * this point, that means there is no TRB available.
1328*4882a593Smuzhiyun */
1329*4882a593Smuzhiyun if (!list_empty(&dep->started_list))
1330*4882a593Smuzhiyun return 0;
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun return DWC3_TRB_NUM - 1;
1333*4882a593Smuzhiyun }
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun trbs_left = dep->trb_dequeue - dep->trb_enqueue;
1336*4882a593Smuzhiyun trbs_left &= (DWC3_TRB_NUM - 1);
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun if (dep->trb_dequeue < dep->trb_enqueue)
1339*4882a593Smuzhiyun trbs_left--;
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun return trbs_left;
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun /**
1345*4882a593Smuzhiyun * dwc3_prepare_one_trb - setup one TRB from one request
1346*4882a593Smuzhiyun * @dep: endpoint for which this request is prepared
1347*4882a593Smuzhiyun * @req: dwc3_request pointer
1348*4882a593Smuzhiyun * @trb_length: buffer size of the TRB
1349*4882a593Smuzhiyun * @chain: should this TRB be chained to the next?
1350*4882a593Smuzhiyun * @node: only for isochronous endpoints. First TRB needs different type.
1351*4882a593Smuzhiyun * @use_bounce_buffer: set to use bounce buffer
1352*4882a593Smuzhiyun * @must_interrupt: set to interrupt on TRB completion
1353*4882a593Smuzhiyun */
dwc3_prepare_one_trb(struct dwc3_ep * dep,struct dwc3_request * req,unsigned int trb_length,unsigned int chain,unsigned int node,bool use_bounce_buffer,bool must_interrupt)1354*4882a593Smuzhiyun static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1355*4882a593Smuzhiyun struct dwc3_request *req, unsigned int trb_length,
1356*4882a593Smuzhiyun unsigned int chain, unsigned int node, bool use_bounce_buffer,
1357*4882a593Smuzhiyun bool must_interrupt)
1358*4882a593Smuzhiyun {
1359*4882a593Smuzhiyun struct dwc3_trb *trb;
1360*4882a593Smuzhiyun dma_addr_t dma;
1361*4882a593Smuzhiyun unsigned int stream_id = req->request.stream_id;
1362*4882a593Smuzhiyun unsigned int short_not_ok = req->request.short_not_ok;
1363*4882a593Smuzhiyun unsigned int no_interrupt = req->request.no_interrupt;
1364*4882a593Smuzhiyun unsigned int is_last = req->request.is_last;
1365*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
1366*4882a593Smuzhiyun struct usb_gadget *gadget = dwc->gadget;
1367*4882a593Smuzhiyun enum usb_device_speed speed = gadget->speed;
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun if (use_bounce_buffer)
1370*4882a593Smuzhiyun dma = dep->dwc->bounce_addr;
1371*4882a593Smuzhiyun else if (req->request.num_sgs > 0)
1372*4882a593Smuzhiyun dma = sg_dma_address(req->start_sg);
1373*4882a593Smuzhiyun else
1374*4882a593Smuzhiyun dma = req->request.dma;
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun trb = &dep->trb_pool[dep->trb_enqueue];
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun if (!req->trb) {
1379*4882a593Smuzhiyun dwc3_gadget_move_started_request(req);
1380*4882a593Smuzhiyun req->trb = trb;
1381*4882a593Smuzhiyun req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun req->num_trbs++;
1385*4882a593Smuzhiyun
1386*4882a593Smuzhiyun trb->size = DWC3_TRB_SIZE_LENGTH(trb_length);
1387*4882a593Smuzhiyun trb->bpl = lower_32_bits(dma);
1388*4882a593Smuzhiyun trb->bph = upper_32_bits(dma);
1389*4882a593Smuzhiyun
1390*4882a593Smuzhiyun switch (usb_endpoint_type(dep->endpoint.desc)) {
1391*4882a593Smuzhiyun case USB_ENDPOINT_XFER_CONTROL:
1392*4882a593Smuzhiyun trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
1393*4882a593Smuzhiyun break;
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun case USB_ENDPOINT_XFER_ISOC:
1396*4882a593Smuzhiyun if (!node) {
1397*4882a593Smuzhiyun trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun /*
1400*4882a593Smuzhiyun * USB Specification 2.0 Section 5.9.2 states that: "If
1401*4882a593Smuzhiyun * there is only a single transaction in the microframe,
1402*4882a593Smuzhiyun * only a DATA0 data packet PID is used. If there are
1403*4882a593Smuzhiyun * two transactions per microframe, DATA1 is used for
1404*4882a593Smuzhiyun * the first transaction data packet and DATA0 is used
1405*4882a593Smuzhiyun * for the second transaction data packet. If there are
1406*4882a593Smuzhiyun * three transactions per microframe, DATA2 is used for
1407*4882a593Smuzhiyun * the first transaction data packet, DATA1 is used for
1408*4882a593Smuzhiyun * the second, and DATA0 is used for the third."
1409*4882a593Smuzhiyun *
1410*4882a593Smuzhiyun * IOW, we should satisfy the following cases:
1411*4882a593Smuzhiyun *
1412*4882a593Smuzhiyun * 1) length <= maxpacket
1413*4882a593Smuzhiyun * - DATA0
1414*4882a593Smuzhiyun *
1415*4882a593Smuzhiyun * 2) maxpacket < length <= (2 * maxpacket)
1416*4882a593Smuzhiyun * - DATA1, DATA0
1417*4882a593Smuzhiyun *
1418*4882a593Smuzhiyun * 3) (2 * maxpacket) < length <= (3 * maxpacket)
1419*4882a593Smuzhiyun * - DATA2, DATA1, DATA0
1420*4882a593Smuzhiyun */
1421*4882a593Smuzhiyun if (speed == USB_SPEED_HIGH) {
1422*4882a593Smuzhiyun struct usb_ep *ep = &dep->endpoint;
1423*4882a593Smuzhiyun unsigned int mult = 2;
1424*4882a593Smuzhiyun unsigned int maxp = usb_endpoint_maxp(ep->desc);
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun if (req->request.length <= (2 * maxp))
1427*4882a593Smuzhiyun mult--;
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun if (req->request.length <= maxp)
1430*4882a593Smuzhiyun mult--;
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun trb->size |= DWC3_TRB_SIZE_PCM1(mult);
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun } else {
1435*4882a593Smuzhiyun trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun if (!no_interrupt && !chain)
1439*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1440*4882a593Smuzhiyun break;
1441*4882a593Smuzhiyun
1442*4882a593Smuzhiyun case USB_ENDPOINT_XFER_BULK:
1443*4882a593Smuzhiyun case USB_ENDPOINT_XFER_INT:
1444*4882a593Smuzhiyun trb->ctrl = DWC3_TRBCTL_NORMAL;
1445*4882a593Smuzhiyun break;
1446*4882a593Smuzhiyun default:
1447*4882a593Smuzhiyun /*
1448*4882a593Smuzhiyun * This is only possible with faulty memory because we
1449*4882a593Smuzhiyun * checked it already :)
1450*4882a593Smuzhiyun */
1451*4882a593Smuzhiyun dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
1452*4882a593Smuzhiyun usb_endpoint_type(dep->endpoint.desc));
1453*4882a593Smuzhiyun }
1454*4882a593Smuzhiyun
1455*4882a593Smuzhiyun /*
1456*4882a593Smuzhiyun * Enable Continue on Short Packet
1457*4882a593Smuzhiyun * when endpoint is not a stream capable
1458*4882a593Smuzhiyun */
1459*4882a593Smuzhiyun if (usb_endpoint_dir_out(dep->endpoint.desc)) {
1460*4882a593Smuzhiyun if (!dep->stream_capable)
1461*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_CSP;
1462*4882a593Smuzhiyun
1463*4882a593Smuzhiyun if (short_not_ok)
1464*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun if ((!no_interrupt && !chain) || must_interrupt)
1468*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_IOC;
1469*4882a593Smuzhiyun
1470*4882a593Smuzhiyun if (chain)
1471*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_CHN;
1472*4882a593Smuzhiyun else if (dep->stream_capable && is_last)
1473*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_LST;
1474*4882a593Smuzhiyun
1475*4882a593Smuzhiyun if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1476*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun /*
1479*4882a593Smuzhiyun * As per data book 4.2.3.2TRB Control Bit Rules section
1480*4882a593Smuzhiyun *
1481*4882a593Smuzhiyun * The controller autonomously checks the HWO field of a TRB to determine if the
1482*4882a593Smuzhiyun * entire TRB is valid. Therefore, software must ensure that the rest of the TRB
1483*4882a593Smuzhiyun * is valid before setting the HWO field to '1'. In most systems, this means that
1484*4882a593Smuzhiyun * software must update the fourth DWORD of a TRB last.
1485*4882a593Smuzhiyun *
1486*4882a593Smuzhiyun * However there is a possibility of CPU re-ordering here which can cause
1487*4882a593Smuzhiyun * controller to observe the HWO bit set prematurely.
1488*4882a593Smuzhiyun * Add a write memory barrier to prevent CPU re-ordering.
1489*4882a593Smuzhiyun */
1490*4882a593Smuzhiyun wmb();
1491*4882a593Smuzhiyun trb->ctrl |= DWC3_TRB_CTRL_HWO;
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun dwc3_ep_inc_enq(dep);
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun trace_dwc3_prepare_trb(dep, trb);
1496*4882a593Smuzhiyun }
1497*4882a593Smuzhiyun
dwc3_needs_extra_trb(struct dwc3_ep * dep,struct dwc3_request * req)1498*4882a593Smuzhiyun static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1499*4882a593Smuzhiyun {
1500*4882a593Smuzhiyun unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1501*4882a593Smuzhiyun unsigned int rem = req->request.length % maxp;
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun if ((req->request.length && req->request.zero && !rem &&
1504*4882a593Smuzhiyun !usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1505*4882a593Smuzhiyun (!req->direction && rem))
1506*4882a593Smuzhiyun return true;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun return false;
1509*4882a593Smuzhiyun }
1510*4882a593Smuzhiyun
1511*4882a593Smuzhiyun /**
1512*4882a593Smuzhiyun * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1513*4882a593Smuzhiyun * @dep: The endpoint that the request belongs to
1514*4882a593Smuzhiyun * @req: The request to prepare
1515*4882a593Smuzhiyun * @entry_length: The last SG entry size
1516*4882a593Smuzhiyun * @node: Indicates whether this is not the first entry (for isoc only)
1517*4882a593Smuzhiyun *
1518*4882a593Smuzhiyun * Return the number of TRBs prepared.
1519*4882a593Smuzhiyun */
dwc3_prepare_last_sg(struct dwc3_ep * dep,struct dwc3_request * req,unsigned int entry_length,unsigned int node)1520*4882a593Smuzhiyun static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1521*4882a593Smuzhiyun struct dwc3_request *req, unsigned int entry_length,
1522*4882a593Smuzhiyun unsigned int node)
1523*4882a593Smuzhiyun {
1524*4882a593Smuzhiyun unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1525*4882a593Smuzhiyun unsigned int rem = req->request.length % maxp;
1526*4882a593Smuzhiyun unsigned int num_trbs = 1;
1527*4882a593Smuzhiyun
1528*4882a593Smuzhiyun if (dwc3_needs_extra_trb(dep, req))
1529*4882a593Smuzhiyun num_trbs++;
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun if (dwc3_calc_trbs_left(dep) < num_trbs)
1532*4882a593Smuzhiyun return 0;
1533*4882a593Smuzhiyun
1534*4882a593Smuzhiyun req->needs_extra_trb = num_trbs > 1;
1535*4882a593Smuzhiyun
1536*4882a593Smuzhiyun /* Prepare a normal TRB */
1537*4882a593Smuzhiyun if (req->direction || req->request.length)
1538*4882a593Smuzhiyun dwc3_prepare_one_trb(dep, req, entry_length,
1539*4882a593Smuzhiyun req->needs_extra_trb, node, false, false);
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1542*4882a593Smuzhiyun if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1543*4882a593Smuzhiyun dwc3_prepare_one_trb(dep, req,
1544*4882a593Smuzhiyun req->direction ? 0 : maxp - rem,
1545*4882a593Smuzhiyun false, 1, true, false);
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun return num_trbs;
1548*4882a593Smuzhiyun }
1549*4882a593Smuzhiyun
dwc3_prepare_trbs_sg(struct dwc3_ep * dep,struct dwc3_request * req)1550*4882a593Smuzhiyun static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
1551*4882a593Smuzhiyun struct dwc3_request *req)
1552*4882a593Smuzhiyun {
1553*4882a593Smuzhiyun struct scatterlist *sg = req->start_sg;
1554*4882a593Smuzhiyun struct scatterlist *s;
1555*4882a593Smuzhiyun int i;
1556*4882a593Smuzhiyun unsigned int length = req->request.length;
1557*4882a593Smuzhiyun unsigned int remaining = req->request.num_mapped_sgs
1558*4882a593Smuzhiyun - req->num_queued_sgs;
1559*4882a593Smuzhiyun unsigned int num_trbs = req->num_trbs;
1560*4882a593Smuzhiyun bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun /*
1563*4882a593Smuzhiyun * If we resume preparing the request, then get the remaining length of
1564*4882a593Smuzhiyun * the request and resume where we left off.
1565*4882a593Smuzhiyun */
1566*4882a593Smuzhiyun for_each_sg(req->request.sg, s, req->num_queued_sgs, i)
1567*4882a593Smuzhiyun length -= sg_dma_len(s);
1568*4882a593Smuzhiyun
1569*4882a593Smuzhiyun for_each_sg(sg, s, remaining, i) {
1570*4882a593Smuzhiyun unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
1571*4882a593Smuzhiyun unsigned int trb_length;
1572*4882a593Smuzhiyun bool must_interrupt = false;
1573*4882a593Smuzhiyun bool last_sg = false;
1574*4882a593Smuzhiyun
1575*4882a593Smuzhiyun trb_length = min_t(unsigned int, length, sg_dma_len(s));
1576*4882a593Smuzhiyun
1577*4882a593Smuzhiyun length -= trb_length;
1578*4882a593Smuzhiyun
1579*4882a593Smuzhiyun /*
1580*4882a593Smuzhiyun * IOMMU driver is coalescing the list of sgs which shares a
1581*4882a593Smuzhiyun * page boundary into one and giving it to USB driver. With
1582*4882a593Smuzhiyun * this the number of sgs mapped is not equal to the number of
1583*4882a593Smuzhiyun * sgs passed. So mark the chain bit to false if it isthe last
1584*4882a593Smuzhiyun * mapped sg.
1585*4882a593Smuzhiyun */
1586*4882a593Smuzhiyun if ((i == remaining - 1) || !length)
1587*4882a593Smuzhiyun last_sg = true;
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun if (!num_trbs_left)
1590*4882a593Smuzhiyun break;
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun if (last_sg) {
1593*4882a593Smuzhiyun if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
1594*4882a593Smuzhiyun break;
1595*4882a593Smuzhiyun } else {
1596*4882a593Smuzhiyun /*
1597*4882a593Smuzhiyun * Look ahead to check if we have enough TRBs for the
1598*4882a593Smuzhiyun * next SG entry. If not, set interrupt on this TRB to
1599*4882a593Smuzhiyun * resume preparing the next SG entry when more TRBs are
1600*4882a593Smuzhiyun * free.
1601*4882a593Smuzhiyun */
1602*4882a593Smuzhiyun if (num_trbs_left == 1 || (needs_extra_trb &&
1603*4882a593Smuzhiyun num_trbs_left <= 2 &&
1604*4882a593Smuzhiyun sg_dma_len(sg_next(s)) >= length))
1605*4882a593Smuzhiyun must_interrupt = true;
1606*4882a593Smuzhiyun
1607*4882a593Smuzhiyun dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1608*4882a593Smuzhiyun must_interrupt);
1609*4882a593Smuzhiyun }
1610*4882a593Smuzhiyun
1611*4882a593Smuzhiyun /*
1612*4882a593Smuzhiyun * There can be a situation where all sgs in sglist are not
1613*4882a593Smuzhiyun * queued because of insufficient trb number. To handle this
1614*4882a593Smuzhiyun * case, update start_sg to next sg to be queued, so that
1615*4882a593Smuzhiyun * we have free trbs we can continue queuing from where we
1616*4882a593Smuzhiyun * previously stopped
1617*4882a593Smuzhiyun */
1618*4882a593Smuzhiyun if (!last_sg)
1619*4882a593Smuzhiyun req->start_sg = sg_next(s);
1620*4882a593Smuzhiyun
1621*4882a593Smuzhiyun req->num_queued_sgs++;
1622*4882a593Smuzhiyun req->num_pending_sgs--;
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun /*
1625*4882a593Smuzhiyun * The number of pending SG entries may not correspond to the
1626*4882a593Smuzhiyun * number of mapped SG entries. If all the data are queued, then
1627*4882a593Smuzhiyun * don't include unused SG entries.
1628*4882a593Smuzhiyun */
1629*4882a593Smuzhiyun if (length == 0) {
1630*4882a593Smuzhiyun req->num_pending_sgs = 0;
1631*4882a593Smuzhiyun break;
1632*4882a593Smuzhiyun }
1633*4882a593Smuzhiyun
1634*4882a593Smuzhiyun if (must_interrupt)
1635*4882a593Smuzhiyun break;
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun
1638*4882a593Smuzhiyun return req->num_trbs - num_trbs;
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun
dwc3_prepare_trbs_linear(struct dwc3_ep * dep,struct dwc3_request * req)1641*4882a593Smuzhiyun static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
1642*4882a593Smuzhiyun struct dwc3_request *req)
1643*4882a593Smuzhiyun {
1644*4882a593Smuzhiyun return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
1645*4882a593Smuzhiyun }
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun /*
1648*4882a593Smuzhiyun * dwc3_prepare_trbs - setup TRBs from requests
1649*4882a593Smuzhiyun * @dep: endpoint for which requests are being prepared
1650*4882a593Smuzhiyun *
1651*4882a593Smuzhiyun * The function goes through the requests list and sets up TRBs for the
1652*4882a593Smuzhiyun * transfers. The function returns once there are no more TRBs available or
1653*4882a593Smuzhiyun * it runs out of requests.
1654*4882a593Smuzhiyun *
1655*4882a593Smuzhiyun * Returns the number of TRBs prepared or negative errno.
1656*4882a593Smuzhiyun */
dwc3_prepare_trbs(struct dwc3_ep * dep)1657*4882a593Smuzhiyun static int dwc3_prepare_trbs(struct dwc3_ep *dep)
1658*4882a593Smuzhiyun {
1659*4882a593Smuzhiyun struct dwc3_request *req, *n;
1660*4882a593Smuzhiyun int ret = 0;
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1663*4882a593Smuzhiyun
1664*4882a593Smuzhiyun /*
1665*4882a593Smuzhiyun * We can get in a situation where there's a request in the started list
1666*4882a593Smuzhiyun * but there weren't enough TRBs to fully kick it in the first time
1667*4882a593Smuzhiyun * around, so it has been waiting for more TRBs to be freed up.
1668*4882a593Smuzhiyun *
1669*4882a593Smuzhiyun * In that case, we should check if we have a request with pending_sgs
1670*4882a593Smuzhiyun * in the started list and prepare TRBs for that request first,
1671*4882a593Smuzhiyun * otherwise we will prepare TRBs completely out of order and that will
1672*4882a593Smuzhiyun * break things.
1673*4882a593Smuzhiyun */
1674*4882a593Smuzhiyun list_for_each_entry(req, &dep->started_list, list) {
1675*4882a593Smuzhiyun if (req->num_pending_sgs > 0) {
1676*4882a593Smuzhiyun ret = dwc3_prepare_trbs_sg(dep, req);
1677*4882a593Smuzhiyun if (!ret || req->num_pending_sgs)
1678*4882a593Smuzhiyun return ret;
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun if (!dwc3_calc_trbs_left(dep))
1682*4882a593Smuzhiyun return ret;
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun /*
1685*4882a593Smuzhiyun * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1686*4882a593Smuzhiyun * burst capability may try to read and use TRBs beyond the
1687*4882a593Smuzhiyun * active transfer instead of stopping.
1688*4882a593Smuzhiyun */
1689*4882a593Smuzhiyun if (dep->stream_capable && req->request.is_last)
1690*4882a593Smuzhiyun return ret;
1691*4882a593Smuzhiyun }
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1694*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
1695*4882a593Smuzhiyun
1696*4882a593Smuzhiyun ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1697*4882a593Smuzhiyun dep->direction);
1698*4882a593Smuzhiyun if (ret)
1699*4882a593Smuzhiyun return ret;
1700*4882a593Smuzhiyun
1701*4882a593Smuzhiyun req->sg = req->request.sg;
1702*4882a593Smuzhiyun req->start_sg = req->sg;
1703*4882a593Smuzhiyun req->num_queued_sgs = 0;
1704*4882a593Smuzhiyun req->num_pending_sgs = req->request.num_mapped_sgs;
1705*4882a593Smuzhiyun
1706*4882a593Smuzhiyun if (req->num_pending_sgs > 0) {
1707*4882a593Smuzhiyun ret = dwc3_prepare_trbs_sg(dep, req);
1708*4882a593Smuzhiyun if (req->num_pending_sgs)
1709*4882a593Smuzhiyun return ret;
1710*4882a593Smuzhiyun } else {
1711*4882a593Smuzhiyun ret = dwc3_prepare_trbs_linear(dep, req);
1712*4882a593Smuzhiyun }
1713*4882a593Smuzhiyun
1714*4882a593Smuzhiyun if (!ret || !dwc3_calc_trbs_left(dep))
1715*4882a593Smuzhiyun return ret;
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun /*
1718*4882a593Smuzhiyun * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1719*4882a593Smuzhiyun * burst capability may try to read and use TRBs beyond the
1720*4882a593Smuzhiyun * active transfer instead of stopping.
1721*4882a593Smuzhiyun */
1722*4882a593Smuzhiyun if (dep->stream_capable && req->request.is_last)
1723*4882a593Smuzhiyun return ret;
1724*4882a593Smuzhiyun }
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun return ret;
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun
1729*4882a593Smuzhiyun static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
1730*4882a593Smuzhiyun
__dwc3_gadget_kick_transfer(struct dwc3_ep * dep)1731*4882a593Smuzhiyun static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1732*4882a593Smuzhiyun {
1733*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
1734*4882a593Smuzhiyun struct dwc3_request *req;
1735*4882a593Smuzhiyun int starting;
1736*4882a593Smuzhiyun int ret;
1737*4882a593Smuzhiyun u32 cmd;
1738*4882a593Smuzhiyun
1739*4882a593Smuzhiyun /*
1740*4882a593Smuzhiyun * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1741*4882a593Smuzhiyun * This happens when we need to stop and restart a transfer such as in
1742*4882a593Smuzhiyun * the case of reinitiating a stream or retrying an isoc transfer.
1743*4882a593Smuzhiyun */
1744*4882a593Smuzhiyun ret = dwc3_prepare_trbs(dep);
1745*4882a593Smuzhiyun if (ret < 0)
1746*4882a593Smuzhiyun return ret;
1747*4882a593Smuzhiyun
1748*4882a593Smuzhiyun starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1749*4882a593Smuzhiyun
1750*4882a593Smuzhiyun /*
1751*4882a593Smuzhiyun * If there's no new TRB prepared and we don't need to restart a
1752*4882a593Smuzhiyun * transfer, there's no need to update the transfer.
1753*4882a593Smuzhiyun */
1754*4882a593Smuzhiyun if (!ret && !starting)
1755*4882a593Smuzhiyun return ret;
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun req = next_request(&dep->started_list);
1758*4882a593Smuzhiyun if (!req) {
1759*4882a593Smuzhiyun dep->flags |= DWC3_EP_PENDING_REQUEST;
1760*4882a593Smuzhiyun return 0;
1761*4882a593Smuzhiyun }
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun memset(¶ms, 0, sizeof(params));
1764*4882a593Smuzhiyun
1765*4882a593Smuzhiyun if (starting) {
1766*4882a593Smuzhiyun params.param0 = upper_32_bits(req->trb_dma);
1767*4882a593Smuzhiyun params.param1 = lower_32_bits(req->trb_dma);
1768*4882a593Smuzhiyun cmd = DWC3_DEPCMD_STARTTRANSFER;
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun if (dep->stream_capable)
1771*4882a593Smuzhiyun cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1772*4882a593Smuzhiyun
1773*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1774*4882a593Smuzhiyun cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1775*4882a593Smuzhiyun } else {
1776*4882a593Smuzhiyun cmd = DWC3_DEPCMD_UPDATETRANSFER |
1777*4882a593Smuzhiyun DWC3_DEPCMD_PARAM(dep->resource_index);
1778*4882a593Smuzhiyun }
1779*4882a593Smuzhiyun
1780*4882a593Smuzhiyun ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1781*4882a593Smuzhiyun if (ret < 0) {
1782*4882a593Smuzhiyun struct dwc3_request *tmp;
1783*4882a593Smuzhiyun
1784*4882a593Smuzhiyun if (ret == -EAGAIN)
1785*4882a593Smuzhiyun return ret;
1786*4882a593Smuzhiyun
1787*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, true);
1788*4882a593Smuzhiyun
1789*4882a593Smuzhiyun list_for_each_entry_safe(req, tmp, &dep->started_list, list)
1790*4882a593Smuzhiyun dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
1791*4882a593Smuzhiyun
1792*4882a593Smuzhiyun /* If ep isn't started, then there's no end transfer pending */
1793*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
1794*4882a593Smuzhiyun dwc3_gadget_ep_cleanup_cancelled_requests(dep);
1795*4882a593Smuzhiyun
1796*4882a593Smuzhiyun return ret;
1797*4882a593Smuzhiyun }
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun if (dep->stream_capable && req->request.is_last)
1800*4882a593Smuzhiyun dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1801*4882a593Smuzhiyun
1802*4882a593Smuzhiyun return 0;
1803*4882a593Smuzhiyun }
1804*4882a593Smuzhiyun
__dwc3_gadget_get_frame(struct dwc3 * dwc)1805*4882a593Smuzhiyun static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1806*4882a593Smuzhiyun {
1807*4882a593Smuzhiyun u32 reg;
1808*4882a593Smuzhiyun
1809*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1810*4882a593Smuzhiyun return DWC3_DSTS_SOFFN(reg);
1811*4882a593Smuzhiyun }
1812*4882a593Smuzhiyun
1813*4882a593Smuzhiyun /**
1814*4882a593Smuzhiyun * __dwc3_stop_active_transfer - stop the current active transfer
1815*4882a593Smuzhiyun * @dep: isoc endpoint
1816*4882a593Smuzhiyun * @force: set forcerm bit in the command
1817*4882a593Smuzhiyun * @interrupt: command complete interrupt after End Transfer command
1818*4882a593Smuzhiyun *
1819*4882a593Smuzhiyun * When setting force, the ForceRM bit will be set. In that case
1820*4882a593Smuzhiyun * the controller won't update the TRB progress on command
1821*4882a593Smuzhiyun * completion. It also won't clear the HWO bit in the TRB.
1822*4882a593Smuzhiyun * The command will also not complete immediately in that case.
1823*4882a593Smuzhiyun */
__dwc3_stop_active_transfer(struct dwc3_ep * dep,bool force,bool interrupt)1824*4882a593Smuzhiyun static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
1825*4882a593Smuzhiyun {
1826*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
1827*4882a593Smuzhiyun u32 cmd;
1828*4882a593Smuzhiyun int ret;
1829*4882a593Smuzhiyun
1830*4882a593Smuzhiyun cmd = DWC3_DEPCMD_ENDTRANSFER;
1831*4882a593Smuzhiyun cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
1832*4882a593Smuzhiyun cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
1833*4882a593Smuzhiyun cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1834*4882a593Smuzhiyun memset(¶ms, 0, sizeof(params));
1835*4882a593Smuzhiyun ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1836*4882a593Smuzhiyun /*
1837*4882a593Smuzhiyun * If the End Transfer command was timed out while the device is
1838*4882a593Smuzhiyun * not in SETUP phase, it's possible that an incoming Setup packet
1839*4882a593Smuzhiyun * may prevent the command's completion. Let's retry when the
1840*4882a593Smuzhiyun * ep0state returns to EP0_SETUP_PHASE.
1841*4882a593Smuzhiyun */
1842*4882a593Smuzhiyun if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) {
1843*4882a593Smuzhiyun dep->flags |= DWC3_EP_DELAY_STOP;
1844*4882a593Smuzhiyun return 0;
1845*4882a593Smuzhiyun }
1846*4882a593Smuzhiyun WARN_ON_ONCE(ret);
1847*4882a593Smuzhiyun dep->resource_index = 0;
1848*4882a593Smuzhiyun
1849*4882a593Smuzhiyun if (!interrupt)
1850*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1851*4882a593Smuzhiyun else
1852*4882a593Smuzhiyun dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1853*4882a593Smuzhiyun
1854*4882a593Smuzhiyun return ret;
1855*4882a593Smuzhiyun }
1856*4882a593Smuzhiyun
1857*4882a593Smuzhiyun /**
1858*4882a593Smuzhiyun * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1859*4882a593Smuzhiyun * @dep: isoc endpoint
1860*4882a593Smuzhiyun *
1861*4882a593Smuzhiyun * This function tests for the correct combination of BIT[15:14] from the 16-bit
1862*4882a593Smuzhiyun * microframe number reported by the XferNotReady event for the future frame
1863*4882a593Smuzhiyun * number to start the isoc transfer.
1864*4882a593Smuzhiyun *
1865*4882a593Smuzhiyun * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1866*4882a593Smuzhiyun * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1867*4882a593Smuzhiyun * XferNotReady event are invalid. The driver uses this number to schedule the
1868*4882a593Smuzhiyun * isochronous transfer and passes it to the START TRANSFER command. Because
1869*4882a593Smuzhiyun * this number is invalid, the command may fail. If BIT[15:14] matches the
1870*4882a593Smuzhiyun * internal 16-bit microframe, the START TRANSFER command will pass and the
1871*4882a593Smuzhiyun * transfer will start at the scheduled time, if it is off by 1, the command
1872*4882a593Smuzhiyun * will still pass, but the transfer will start 2 seconds in the future. For all
1873*4882a593Smuzhiyun * other conditions, the START TRANSFER command will fail with bus-expiry.
1874*4882a593Smuzhiyun *
1875*4882a593Smuzhiyun * In order to workaround this issue, we can test for the correct combination of
1876*4882a593Smuzhiyun * BIT[15:14] by sending START TRANSFER commands with different values of
1877*4882a593Smuzhiyun * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1878*4882a593Smuzhiyun * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1879*4882a593Smuzhiyun * As the result, within the 4 possible combinations for BIT[15:14], there will
1880*4882a593Smuzhiyun * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1881*4882a593Smuzhiyun * command status will result in a 2-second delay start. The smaller BIT[15:14]
1882*4882a593Smuzhiyun * value is the correct combination.
1883*4882a593Smuzhiyun *
1884*4882a593Smuzhiyun * Since there are only 4 outcomes and the results are ordered, we can simply
1885*4882a593Smuzhiyun * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1886*4882a593Smuzhiyun * deduce the smaller successful combination.
1887*4882a593Smuzhiyun *
1888*4882a593Smuzhiyun * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1889*4882a593Smuzhiyun * of BIT[15:14]. The correct combination is as follow:
1890*4882a593Smuzhiyun *
1891*4882a593Smuzhiyun * if test0 fails and test1 passes, BIT[15:14] is 'b01
1892*4882a593Smuzhiyun * if test0 fails and test1 fails, BIT[15:14] is 'b10
1893*4882a593Smuzhiyun * if test0 passes and test1 fails, BIT[15:14] is 'b11
1894*4882a593Smuzhiyun * if test0 passes and test1 passes, BIT[15:14] is 'b00
1895*4882a593Smuzhiyun *
1896*4882a593Smuzhiyun * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1897*4882a593Smuzhiyun * endpoints.
1898*4882a593Smuzhiyun */
dwc3_gadget_start_isoc_quirk(struct dwc3_ep * dep)1899*4882a593Smuzhiyun static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1900*4882a593Smuzhiyun {
1901*4882a593Smuzhiyun int cmd_status = 0;
1902*4882a593Smuzhiyun bool test0;
1903*4882a593Smuzhiyun bool test1;
1904*4882a593Smuzhiyun
1905*4882a593Smuzhiyun while (dep->combo_num < 2) {
1906*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
1907*4882a593Smuzhiyun u32 test_frame_number;
1908*4882a593Smuzhiyun u32 cmd;
1909*4882a593Smuzhiyun
1910*4882a593Smuzhiyun /*
1911*4882a593Smuzhiyun * Check if we can start isoc transfer on the next interval or
1912*4882a593Smuzhiyun * 4 uframes in the future with BIT[15:14] as dep->combo_num
1913*4882a593Smuzhiyun */
1914*4882a593Smuzhiyun test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
1915*4882a593Smuzhiyun test_frame_number |= dep->combo_num << 14;
1916*4882a593Smuzhiyun test_frame_number += max_t(u32, 4, dep->interval);
1917*4882a593Smuzhiyun
1918*4882a593Smuzhiyun params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1919*4882a593Smuzhiyun params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1920*4882a593Smuzhiyun
1921*4882a593Smuzhiyun cmd = DWC3_DEPCMD_STARTTRANSFER;
1922*4882a593Smuzhiyun cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1923*4882a593Smuzhiyun cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun /* Redo if some other failure beside bus-expiry is received */
1926*4882a593Smuzhiyun if (cmd_status && cmd_status != -EAGAIN) {
1927*4882a593Smuzhiyun dep->start_cmd_status = 0;
1928*4882a593Smuzhiyun dep->combo_num = 0;
1929*4882a593Smuzhiyun return 0;
1930*4882a593Smuzhiyun }
1931*4882a593Smuzhiyun
1932*4882a593Smuzhiyun /* Store the first test status */
1933*4882a593Smuzhiyun if (dep->combo_num == 0)
1934*4882a593Smuzhiyun dep->start_cmd_status = cmd_status;
1935*4882a593Smuzhiyun
1936*4882a593Smuzhiyun dep->combo_num++;
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun /*
1939*4882a593Smuzhiyun * End the transfer if the START_TRANSFER command is successful
1940*4882a593Smuzhiyun * to wait for the next XferNotReady to test the command again
1941*4882a593Smuzhiyun */
1942*4882a593Smuzhiyun if (cmd_status == 0) {
1943*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, true);
1944*4882a593Smuzhiyun return 0;
1945*4882a593Smuzhiyun }
1946*4882a593Smuzhiyun }
1947*4882a593Smuzhiyun
1948*4882a593Smuzhiyun /* test0 and test1 are both completed at this point */
1949*4882a593Smuzhiyun test0 = (dep->start_cmd_status == 0);
1950*4882a593Smuzhiyun test1 = (cmd_status == 0);
1951*4882a593Smuzhiyun
1952*4882a593Smuzhiyun if (!test0 && test1)
1953*4882a593Smuzhiyun dep->combo_num = 1;
1954*4882a593Smuzhiyun else if (!test0 && !test1)
1955*4882a593Smuzhiyun dep->combo_num = 2;
1956*4882a593Smuzhiyun else if (test0 && !test1)
1957*4882a593Smuzhiyun dep->combo_num = 3;
1958*4882a593Smuzhiyun else if (test0 && test1)
1959*4882a593Smuzhiyun dep->combo_num = 0;
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun dep->frame_number &= DWC3_FRNUMBER_MASK;
1962*4882a593Smuzhiyun dep->frame_number |= dep->combo_num << 14;
1963*4882a593Smuzhiyun dep->frame_number += max_t(u32, 4, dep->interval);
1964*4882a593Smuzhiyun
1965*4882a593Smuzhiyun /* Reinitialize test variables */
1966*4882a593Smuzhiyun dep->start_cmd_status = 0;
1967*4882a593Smuzhiyun dep->combo_num = 0;
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun return __dwc3_gadget_kick_transfer(dep);
1970*4882a593Smuzhiyun }
1971*4882a593Smuzhiyun
__dwc3_gadget_start_isoc(struct dwc3_ep * dep)1972*4882a593Smuzhiyun static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1973*4882a593Smuzhiyun {
1974*4882a593Smuzhiyun const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
1975*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
1976*4882a593Smuzhiyun int ret;
1977*4882a593Smuzhiyun int i;
1978*4882a593Smuzhiyun
1979*4882a593Smuzhiyun if (list_empty(&dep->pending_list) &&
1980*4882a593Smuzhiyun list_empty(&dep->started_list)) {
1981*4882a593Smuzhiyun dep->flags |= DWC3_EP_PENDING_REQUEST;
1982*4882a593Smuzhiyun return -EAGAIN;
1983*4882a593Smuzhiyun }
1984*4882a593Smuzhiyun
1985*4882a593Smuzhiyun if (!dwc->dis_start_transfer_quirk &&
1986*4882a593Smuzhiyun (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
1987*4882a593Smuzhiyun DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
1988*4882a593Smuzhiyun if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
1989*4882a593Smuzhiyun return dwc3_gadget_start_isoc_quirk(dep);
1990*4882a593Smuzhiyun }
1991*4882a593Smuzhiyun
1992*4882a593Smuzhiyun if (desc->bInterval <= 14 &&
1993*4882a593Smuzhiyun dwc->gadget->speed >= USB_SPEED_HIGH) {
1994*4882a593Smuzhiyun u32 frame = __dwc3_gadget_get_frame(dwc);
1995*4882a593Smuzhiyun bool rollover = frame <
1996*4882a593Smuzhiyun (dep->frame_number & DWC3_FRNUMBER_MASK);
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun /*
1999*4882a593Smuzhiyun * frame_number is set from XferNotReady and may be already
2000*4882a593Smuzhiyun * out of date. DSTS only provides the lower 14 bit of the
2001*4882a593Smuzhiyun * current frame number. So add the upper two bits of
2002*4882a593Smuzhiyun * frame_number and handle a possible rollover.
2003*4882a593Smuzhiyun * This will provide the correct frame_number unless more than
2004*4882a593Smuzhiyun * rollover has happened since XferNotReady.
2005*4882a593Smuzhiyun */
2006*4882a593Smuzhiyun
2007*4882a593Smuzhiyun dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
2008*4882a593Smuzhiyun frame;
2009*4882a593Smuzhiyun if (rollover)
2010*4882a593Smuzhiyun dep->frame_number += BIT(14);
2011*4882a593Smuzhiyun }
2012*4882a593Smuzhiyun
2013*4882a593Smuzhiyun for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
2014*4882a593Smuzhiyun int future_interval = i + 1;
2015*4882a593Smuzhiyun
2016*4882a593Smuzhiyun /* Give the controller at least 500us to schedule transfers */
2017*4882a593Smuzhiyun if (desc->bInterval < 3)
2018*4882a593Smuzhiyun future_interval += 3 - desc->bInterval;
2019*4882a593Smuzhiyun
2020*4882a593Smuzhiyun dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval);
2021*4882a593Smuzhiyun
2022*4882a593Smuzhiyun ret = __dwc3_gadget_kick_transfer(dep);
2023*4882a593Smuzhiyun if (ret != -EAGAIN)
2024*4882a593Smuzhiyun break;
2025*4882a593Smuzhiyun }
2026*4882a593Smuzhiyun
2027*4882a593Smuzhiyun /*
2028*4882a593Smuzhiyun * After a number of unsuccessful start attempts due to bus-expiry
2029*4882a593Smuzhiyun * status, issue END_TRANSFER command and retry on the next XferNotReady
2030*4882a593Smuzhiyun * event.
2031*4882a593Smuzhiyun */
2032*4882a593Smuzhiyun if (ret == -EAGAIN) {
2033*4882a593Smuzhiyun ret = __dwc3_stop_active_transfer(dep, false, true);
2034*4882a593Smuzhiyun if (ret)
2035*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2036*4882a593Smuzhiyun }
2037*4882a593Smuzhiyun
2038*4882a593Smuzhiyun return ret;
2039*4882a593Smuzhiyun }
2040*4882a593Smuzhiyun
__dwc3_gadget_ep_queue(struct dwc3_ep * dep,struct dwc3_request * req)2041*4882a593Smuzhiyun static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
2042*4882a593Smuzhiyun {
2043*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
2044*4882a593Smuzhiyun
2045*4882a593Smuzhiyun if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
2046*4882a593Smuzhiyun dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
2047*4882a593Smuzhiyun dep->name);
2048*4882a593Smuzhiyun return -ESHUTDOWN;
2049*4882a593Smuzhiyun }
2050*4882a593Smuzhiyun
2051*4882a593Smuzhiyun if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
2052*4882a593Smuzhiyun &req->request, req->dep->name))
2053*4882a593Smuzhiyun return -EINVAL;
2054*4882a593Smuzhiyun
2055*4882a593Smuzhiyun if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
2056*4882a593Smuzhiyun "%s: request %pK already in flight\n",
2057*4882a593Smuzhiyun dep->name, &req->request))
2058*4882a593Smuzhiyun return -EINVAL;
2059*4882a593Smuzhiyun
2060*4882a593Smuzhiyun pm_runtime_get(dwc->dev);
2061*4882a593Smuzhiyun
2062*4882a593Smuzhiyun req->request.actual = 0;
2063*4882a593Smuzhiyun req->request.status = -EINPROGRESS;
2064*4882a593Smuzhiyun
2065*4882a593Smuzhiyun trace_dwc3_ep_queue(req);
2066*4882a593Smuzhiyun
2067*4882a593Smuzhiyun list_add_tail(&req->list, &dep->pending_list);
2068*4882a593Smuzhiyun req->status = DWC3_REQUEST_STATUS_QUEUED;
2069*4882a593Smuzhiyun
2070*4882a593Smuzhiyun if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
2071*4882a593Smuzhiyun return 0;
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun /*
2074*4882a593Smuzhiyun * Start the transfer only after the END_TRANSFER is completed
2075*4882a593Smuzhiyun * and endpoint STALL is cleared.
2076*4882a593Smuzhiyun */
2077*4882a593Smuzhiyun if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2078*4882a593Smuzhiyun (dep->flags & DWC3_EP_WEDGE) ||
2079*4882a593Smuzhiyun (dep->flags & DWC3_EP_DELAY_STOP) ||
2080*4882a593Smuzhiyun (dep->flags & DWC3_EP_STALL)) {
2081*4882a593Smuzhiyun dep->flags |= DWC3_EP_DELAY_START;
2082*4882a593Smuzhiyun return 0;
2083*4882a593Smuzhiyun }
2084*4882a593Smuzhiyun
2085*4882a593Smuzhiyun /*
2086*4882a593Smuzhiyun * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
2087*4882a593Smuzhiyun * wait for a XferNotReady event so we will know what's the current
2088*4882a593Smuzhiyun * (micro-)frame number.
2089*4882a593Smuzhiyun *
2090*4882a593Smuzhiyun * Without this trick, we are very, very likely gonna get Bus Expiry
2091*4882a593Smuzhiyun * errors which will force us issue EndTransfer command.
2092*4882a593Smuzhiyun */
2093*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2094*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
2095*4882a593Smuzhiyun if ((dep->flags & DWC3_EP_PENDING_REQUEST))
2096*4882a593Smuzhiyun return __dwc3_gadget_start_isoc(dep);
2097*4882a593Smuzhiyun
2098*4882a593Smuzhiyun return 0;
2099*4882a593Smuzhiyun }
2100*4882a593Smuzhiyun }
2101*4882a593Smuzhiyun
2102*4882a593Smuzhiyun __dwc3_gadget_kick_transfer(dep);
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun return 0;
2105*4882a593Smuzhiyun }
2106*4882a593Smuzhiyun
dwc3_gadget_ep_queue(struct usb_ep * ep,struct usb_request * request,gfp_t gfp_flags)2107*4882a593Smuzhiyun static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
2108*4882a593Smuzhiyun gfp_t gfp_flags)
2109*4882a593Smuzhiyun {
2110*4882a593Smuzhiyun struct dwc3_request *req = to_dwc3_request(request);
2111*4882a593Smuzhiyun struct dwc3_ep *dep = to_dwc3_ep(ep);
2112*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
2113*4882a593Smuzhiyun
2114*4882a593Smuzhiyun unsigned long flags;
2115*4882a593Smuzhiyun
2116*4882a593Smuzhiyun int ret;
2117*4882a593Smuzhiyun
2118*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2119*4882a593Smuzhiyun ret = __dwc3_gadget_ep_queue(dep, req);
2120*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun return ret;
2123*4882a593Smuzhiyun }
2124*4882a593Smuzhiyun
dwc3_gadget_ep_skip_trbs(struct dwc3_ep * dep,struct dwc3_request * req)2125*4882a593Smuzhiyun static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
2126*4882a593Smuzhiyun {
2127*4882a593Smuzhiyun int i;
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun /* If req->trb is not set, then the request has not started */
2130*4882a593Smuzhiyun if (!req->trb)
2131*4882a593Smuzhiyun return;
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun /*
2134*4882a593Smuzhiyun * If request was already started, this means we had to
2135*4882a593Smuzhiyun * stop the transfer. With that we also need to ignore
2136*4882a593Smuzhiyun * all TRBs used by the request, however TRBs can only
2137*4882a593Smuzhiyun * be modified after completion of END_TRANSFER
2138*4882a593Smuzhiyun * command. So what we do here is that we wait for
2139*4882a593Smuzhiyun * END_TRANSFER completion and only after that, we jump
2140*4882a593Smuzhiyun * over TRBs by clearing HWO and incrementing dequeue
2141*4882a593Smuzhiyun * pointer.
2142*4882a593Smuzhiyun */
2143*4882a593Smuzhiyun for (i = 0; i < req->num_trbs; i++) {
2144*4882a593Smuzhiyun struct dwc3_trb *trb;
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun trb = &dep->trb_pool[dep->trb_dequeue];
2147*4882a593Smuzhiyun trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2148*4882a593Smuzhiyun dwc3_ep_inc_deq(dep);
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun
2151*4882a593Smuzhiyun req->num_trbs = 0;
2152*4882a593Smuzhiyun }
2153*4882a593Smuzhiyun
dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep * dep)2154*4882a593Smuzhiyun static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
2155*4882a593Smuzhiyun {
2156*4882a593Smuzhiyun struct dwc3_request *req;
2157*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
2158*4882a593Smuzhiyun
2159*4882a593Smuzhiyun while (!list_empty(&dep->cancelled_list)) {
2160*4882a593Smuzhiyun req = next_request(&dep->cancelled_list);
2161*4882a593Smuzhiyun dwc3_gadget_ep_skip_trbs(dep, req);
2162*4882a593Smuzhiyun switch (req->status) {
2163*4882a593Smuzhiyun case DWC3_REQUEST_STATUS_DISCONNECTED:
2164*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
2165*4882a593Smuzhiyun break;
2166*4882a593Smuzhiyun case DWC3_REQUEST_STATUS_DEQUEUED:
2167*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, -ECONNRESET);
2168*4882a593Smuzhiyun break;
2169*4882a593Smuzhiyun case DWC3_REQUEST_STATUS_STALLED:
2170*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, -EPIPE);
2171*4882a593Smuzhiyun break;
2172*4882a593Smuzhiyun default:
2173*4882a593Smuzhiyun dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
2174*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, -ECONNRESET);
2175*4882a593Smuzhiyun break;
2176*4882a593Smuzhiyun }
2177*4882a593Smuzhiyun /*
2178*4882a593Smuzhiyun * The endpoint is disabled, let the dwc3_remove_requests()
2179*4882a593Smuzhiyun * handle the cleanup.
2180*4882a593Smuzhiyun */
2181*4882a593Smuzhiyun if (!dep->endpoint.desc)
2182*4882a593Smuzhiyun break;
2183*4882a593Smuzhiyun }
2184*4882a593Smuzhiyun }
2185*4882a593Smuzhiyun
dwc3_gadget_ep_dequeue(struct usb_ep * ep,struct usb_request * request)2186*4882a593Smuzhiyun static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
2187*4882a593Smuzhiyun struct usb_request *request)
2188*4882a593Smuzhiyun {
2189*4882a593Smuzhiyun struct dwc3_request *req = to_dwc3_request(request);
2190*4882a593Smuzhiyun struct dwc3_request *r = NULL;
2191*4882a593Smuzhiyun
2192*4882a593Smuzhiyun struct dwc3_ep *dep = to_dwc3_ep(ep);
2193*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
2194*4882a593Smuzhiyun
2195*4882a593Smuzhiyun unsigned long flags;
2196*4882a593Smuzhiyun int ret = 0;
2197*4882a593Smuzhiyun
2198*4882a593Smuzhiyun trace_dwc3_ep_dequeue(req);
2199*4882a593Smuzhiyun
2200*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2201*4882a593Smuzhiyun
2202*4882a593Smuzhiyun list_for_each_entry(r, &dep->cancelled_list, list) {
2203*4882a593Smuzhiyun if (r == req)
2204*4882a593Smuzhiyun goto out;
2205*4882a593Smuzhiyun }
2206*4882a593Smuzhiyun
2207*4882a593Smuzhiyun list_for_each_entry(r, &dep->pending_list, list) {
2208*4882a593Smuzhiyun if (r == req) {
2209*4882a593Smuzhiyun dwc3_gadget_ep_skip_trbs(dep, req);
2210*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, -ECONNRESET);
2211*4882a593Smuzhiyun goto out;
2212*4882a593Smuzhiyun }
2213*4882a593Smuzhiyun }
2214*4882a593Smuzhiyun
2215*4882a593Smuzhiyun list_for_each_entry(r, &dep->started_list, list) {
2216*4882a593Smuzhiyun if (r == req) {
2217*4882a593Smuzhiyun /* wait until it is processed */
2218*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, true);
2219*4882a593Smuzhiyun
2220*4882a593Smuzhiyun /*
2221*4882a593Smuzhiyun * Remove any started request if the transfer is
2222*4882a593Smuzhiyun * cancelled.
2223*4882a593Smuzhiyun */
2224*4882a593Smuzhiyun dwc3_gadget_move_cancelled_request(r, DWC3_REQUEST_STATUS_DEQUEUED);
2225*4882a593Smuzhiyun
2226*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
2227*4882a593Smuzhiyun
2228*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
2229*4882a593Smuzhiyun dwc3_gadget_ep_skip_trbs(dep, req);
2230*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, -ECONNRESET);
2231*4882a593Smuzhiyun }
2232*4882a593Smuzhiyun
2233*4882a593Smuzhiyun goto out;
2234*4882a593Smuzhiyun }
2235*4882a593Smuzhiyun }
2236*4882a593Smuzhiyun
2237*4882a593Smuzhiyun dev_err(dwc->dev, "request %pK was not queued to %s\n",
2238*4882a593Smuzhiyun request, ep->name);
2239*4882a593Smuzhiyun ret = -EINVAL;
2240*4882a593Smuzhiyun out:
2241*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2242*4882a593Smuzhiyun
2243*4882a593Smuzhiyun return ret;
2244*4882a593Smuzhiyun }
2245*4882a593Smuzhiyun
__dwc3_gadget_ep_set_halt(struct dwc3_ep * dep,int value,int protocol)2246*4882a593Smuzhiyun int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
2247*4882a593Smuzhiyun {
2248*4882a593Smuzhiyun struct dwc3_gadget_ep_cmd_params params;
2249*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
2250*4882a593Smuzhiyun int ret;
2251*4882a593Smuzhiyun struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2254*4882a593Smuzhiyun dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
2255*4882a593Smuzhiyun return -EINVAL;
2256*4882a593Smuzhiyun }
2257*4882a593Smuzhiyun
2258*4882a593Smuzhiyun memset(¶ms, 0x00, sizeof(params));
2259*4882a593Smuzhiyun
2260*4882a593Smuzhiyun if (value) {
2261*4882a593Smuzhiyun struct dwc3_trb *trb;
2262*4882a593Smuzhiyun
2263*4882a593Smuzhiyun unsigned int transfer_in_flight;
2264*4882a593Smuzhiyun unsigned int started;
2265*4882a593Smuzhiyun
2266*4882a593Smuzhiyun if (dep->number > 1)
2267*4882a593Smuzhiyun trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
2268*4882a593Smuzhiyun else
2269*4882a593Smuzhiyun trb = &dwc->ep0_trb[dep->trb_enqueue];
2270*4882a593Smuzhiyun
2271*4882a593Smuzhiyun transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
2272*4882a593Smuzhiyun started = !list_empty(&dep->started_list);
2273*4882a593Smuzhiyun
2274*4882a593Smuzhiyun if (!protocol && ((dep->direction && transfer_in_flight) ||
2275*4882a593Smuzhiyun (!dep->direction && started))) {
2276*4882a593Smuzhiyun return -EAGAIN;
2277*4882a593Smuzhiyun }
2278*4882a593Smuzhiyun
2279*4882a593Smuzhiyun ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
2280*4882a593Smuzhiyun ¶ms);
2281*4882a593Smuzhiyun if (ret)
2282*4882a593Smuzhiyun dev_err(dwc->dev, "failed to set STALL on %s\n",
2283*4882a593Smuzhiyun dep->name);
2284*4882a593Smuzhiyun else
2285*4882a593Smuzhiyun dep->flags |= DWC3_EP_STALL;
2286*4882a593Smuzhiyun } else {
2287*4882a593Smuzhiyun /*
2288*4882a593Smuzhiyun * Don't issue CLEAR_STALL command to control endpoints. The
2289*4882a593Smuzhiyun * controller automatically clears the STALL when it receives
2290*4882a593Smuzhiyun * the SETUP token.
2291*4882a593Smuzhiyun */
2292*4882a593Smuzhiyun if (dep->number <= 1) {
2293*4882a593Smuzhiyun dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2294*4882a593Smuzhiyun return 0;
2295*4882a593Smuzhiyun }
2296*4882a593Smuzhiyun
2297*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, true);
2298*4882a593Smuzhiyun
2299*4882a593Smuzhiyun if (!list_empty(&dep->started_list))
2300*4882a593Smuzhiyun dep->flags |= DWC3_EP_DELAY_START;
2301*4882a593Smuzhiyun
2302*4882a593Smuzhiyun if (dep->flags & DWC3_EP_END_TRANSFER_PENDING ||
2303*4882a593Smuzhiyun (dep->flags & DWC3_EP_DELAY_STOP)) {
2304*4882a593Smuzhiyun dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
2305*4882a593Smuzhiyun if (protocol)
2306*4882a593Smuzhiyun vdwc->clear_stall_protocol = dep->number;
2307*4882a593Smuzhiyun
2308*4882a593Smuzhiyun return 0;
2309*4882a593Smuzhiyun }
2310*4882a593Smuzhiyun
2311*4882a593Smuzhiyun ret = dwc3_send_clear_stall_ep_cmd(dep);
2312*4882a593Smuzhiyun if (ret) {
2313*4882a593Smuzhiyun dev_err(dwc->dev, "failed to clear STALL on %s\n",
2314*4882a593Smuzhiyun dep->name);
2315*4882a593Smuzhiyun return ret;
2316*4882a593Smuzhiyun }
2317*4882a593Smuzhiyun
2318*4882a593Smuzhiyun dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2319*4882a593Smuzhiyun
2320*4882a593Smuzhiyun if ((dep->flags & DWC3_EP_DELAY_START) &&
2321*4882a593Smuzhiyun !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2322*4882a593Smuzhiyun __dwc3_gadget_kick_transfer(dep);
2323*4882a593Smuzhiyun
2324*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_DELAY_START;
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun
2327*4882a593Smuzhiyun return ret;
2328*4882a593Smuzhiyun }
2329*4882a593Smuzhiyun
dwc3_gadget_ep_set_halt(struct usb_ep * ep,int value)2330*4882a593Smuzhiyun static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
2331*4882a593Smuzhiyun {
2332*4882a593Smuzhiyun struct dwc3_ep *dep = to_dwc3_ep(ep);
2333*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
2334*4882a593Smuzhiyun
2335*4882a593Smuzhiyun unsigned long flags;
2336*4882a593Smuzhiyun
2337*4882a593Smuzhiyun int ret;
2338*4882a593Smuzhiyun
2339*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2340*4882a593Smuzhiyun ret = __dwc3_gadget_ep_set_halt(dep, value, false);
2341*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2342*4882a593Smuzhiyun
2343*4882a593Smuzhiyun return ret;
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun
dwc3_gadget_ep_set_wedge(struct usb_ep * ep)2346*4882a593Smuzhiyun static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
2347*4882a593Smuzhiyun {
2348*4882a593Smuzhiyun struct dwc3_ep *dep = to_dwc3_ep(ep);
2349*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
2350*4882a593Smuzhiyun unsigned long flags;
2351*4882a593Smuzhiyun int ret;
2352*4882a593Smuzhiyun
2353*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2354*4882a593Smuzhiyun dep->flags |= DWC3_EP_WEDGE;
2355*4882a593Smuzhiyun
2356*4882a593Smuzhiyun if (dep->number == 0 || dep->number == 1)
2357*4882a593Smuzhiyun ret = __dwc3_gadget_ep0_set_halt(ep, 1);
2358*4882a593Smuzhiyun else
2359*4882a593Smuzhiyun ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
2360*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2361*4882a593Smuzhiyun
2362*4882a593Smuzhiyun return ret;
2363*4882a593Smuzhiyun }
2364*4882a593Smuzhiyun
2365*4882a593Smuzhiyun /* -------------------------------------------------------------------------- */
2366*4882a593Smuzhiyun
2367*4882a593Smuzhiyun static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
2368*4882a593Smuzhiyun .bLength = USB_DT_ENDPOINT_SIZE,
2369*4882a593Smuzhiyun .bDescriptorType = USB_DT_ENDPOINT,
2370*4882a593Smuzhiyun .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
2371*4882a593Smuzhiyun };
2372*4882a593Smuzhiyun
2373*4882a593Smuzhiyun static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
2374*4882a593Smuzhiyun .enable = dwc3_gadget_ep0_enable,
2375*4882a593Smuzhiyun .disable = dwc3_gadget_ep0_disable,
2376*4882a593Smuzhiyun .alloc_request = dwc3_gadget_ep_alloc_request,
2377*4882a593Smuzhiyun .free_request = dwc3_gadget_ep_free_request,
2378*4882a593Smuzhiyun .queue = dwc3_gadget_ep0_queue,
2379*4882a593Smuzhiyun .dequeue = dwc3_gadget_ep_dequeue,
2380*4882a593Smuzhiyun .set_halt = dwc3_gadget_ep0_set_halt,
2381*4882a593Smuzhiyun .set_wedge = dwc3_gadget_ep_set_wedge,
2382*4882a593Smuzhiyun };
2383*4882a593Smuzhiyun
2384*4882a593Smuzhiyun static const struct usb_ep_ops dwc3_gadget_ep_ops = {
2385*4882a593Smuzhiyun .enable = dwc3_gadget_ep_enable,
2386*4882a593Smuzhiyun .disable = dwc3_gadget_ep_disable,
2387*4882a593Smuzhiyun .alloc_request = dwc3_gadget_ep_alloc_request,
2388*4882a593Smuzhiyun .free_request = dwc3_gadget_ep_free_request,
2389*4882a593Smuzhiyun .queue = dwc3_gadget_ep_queue,
2390*4882a593Smuzhiyun .dequeue = dwc3_gadget_ep_dequeue,
2391*4882a593Smuzhiyun .set_halt = dwc3_gadget_ep_set_halt,
2392*4882a593Smuzhiyun .set_wedge = dwc3_gadget_ep_set_wedge,
2393*4882a593Smuzhiyun };
2394*4882a593Smuzhiyun
2395*4882a593Smuzhiyun /* -------------------------------------------------------------------------- */
2396*4882a593Smuzhiyun
dwc3_gadget_get_frame(struct usb_gadget * g)2397*4882a593Smuzhiyun static int dwc3_gadget_get_frame(struct usb_gadget *g)
2398*4882a593Smuzhiyun {
2399*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
2400*4882a593Smuzhiyun
2401*4882a593Smuzhiyun return __dwc3_gadget_get_frame(dwc);
2402*4882a593Smuzhiyun }
2403*4882a593Smuzhiyun
__dwc3_gadget_wakeup(struct dwc3 * dwc)2404*4882a593Smuzhiyun static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
2405*4882a593Smuzhiyun {
2406*4882a593Smuzhiyun int retries;
2407*4882a593Smuzhiyun
2408*4882a593Smuzhiyun int ret;
2409*4882a593Smuzhiyun u32 reg;
2410*4882a593Smuzhiyun
2411*4882a593Smuzhiyun u8 link_state;
2412*4882a593Smuzhiyun
2413*4882a593Smuzhiyun /*
2414*4882a593Smuzhiyun * According to the Databook Remote wakeup request should
2415*4882a593Smuzhiyun * be issued only when the device is in early suspend state.
2416*4882a593Smuzhiyun *
2417*4882a593Smuzhiyun * We can check that via USB Link State bits in DSTS register.
2418*4882a593Smuzhiyun */
2419*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun link_state = DWC3_DSTS_USBLNKST(reg);
2422*4882a593Smuzhiyun
2423*4882a593Smuzhiyun switch (link_state) {
2424*4882a593Smuzhiyun case DWC3_LINK_STATE_RESET:
2425*4882a593Smuzhiyun case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
2426*4882a593Smuzhiyun case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
2427*4882a593Smuzhiyun case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */
2428*4882a593Smuzhiyun case DWC3_LINK_STATE_U1:
2429*4882a593Smuzhiyun case DWC3_LINK_STATE_RESUME:
2430*4882a593Smuzhiyun break;
2431*4882a593Smuzhiyun default:
2432*4882a593Smuzhiyun return -EINVAL;
2433*4882a593Smuzhiyun }
2434*4882a593Smuzhiyun
2435*4882a593Smuzhiyun ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
2436*4882a593Smuzhiyun if (ret < 0) {
2437*4882a593Smuzhiyun dev_err(dwc->dev, "failed to put link in Recovery\n");
2438*4882a593Smuzhiyun return ret;
2439*4882a593Smuzhiyun }
2440*4882a593Smuzhiyun
2441*4882a593Smuzhiyun /* Recent versions do this automatically */
2442*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
2443*4882a593Smuzhiyun /* write zeroes to Link Change Request */
2444*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2445*4882a593Smuzhiyun reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
2446*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2447*4882a593Smuzhiyun }
2448*4882a593Smuzhiyun
2449*4882a593Smuzhiyun /* poll until Link State changes to ON */
2450*4882a593Smuzhiyun retries = 20000;
2451*4882a593Smuzhiyun
2452*4882a593Smuzhiyun while (retries--) {
2453*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2454*4882a593Smuzhiyun
2455*4882a593Smuzhiyun /* in HS, means ON */
2456*4882a593Smuzhiyun if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
2457*4882a593Smuzhiyun break;
2458*4882a593Smuzhiyun }
2459*4882a593Smuzhiyun
2460*4882a593Smuzhiyun if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
2461*4882a593Smuzhiyun dev_err(dwc->dev, "failed to send remote wakeup\n");
2462*4882a593Smuzhiyun return -EINVAL;
2463*4882a593Smuzhiyun }
2464*4882a593Smuzhiyun
2465*4882a593Smuzhiyun return 0;
2466*4882a593Smuzhiyun }
2467*4882a593Smuzhiyun
dwc3_gadget_wakeup(struct usb_gadget * g)2468*4882a593Smuzhiyun static int dwc3_gadget_wakeup(struct usb_gadget *g)
2469*4882a593Smuzhiyun {
2470*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
2471*4882a593Smuzhiyun unsigned long flags;
2472*4882a593Smuzhiyun int ret;
2473*4882a593Smuzhiyun
2474*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2475*4882a593Smuzhiyun ret = __dwc3_gadget_wakeup(dwc);
2476*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2477*4882a593Smuzhiyun
2478*4882a593Smuzhiyun return ret;
2479*4882a593Smuzhiyun }
2480*4882a593Smuzhiyun
dwc3_gadget_set_selfpowered(struct usb_gadget * g,int is_selfpowered)2481*4882a593Smuzhiyun static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
2482*4882a593Smuzhiyun int is_selfpowered)
2483*4882a593Smuzhiyun {
2484*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
2485*4882a593Smuzhiyun unsigned long flags;
2486*4882a593Smuzhiyun
2487*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2488*4882a593Smuzhiyun g->is_selfpowered = !!is_selfpowered;
2489*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2490*4882a593Smuzhiyun
2491*4882a593Smuzhiyun return 0;
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun
dwc3_stop_active_transfers(struct dwc3 * dwc)2494*4882a593Smuzhiyun static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2495*4882a593Smuzhiyun {
2496*4882a593Smuzhiyun u32 epnum;
2497*4882a593Smuzhiyun
2498*4882a593Smuzhiyun for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2499*4882a593Smuzhiyun struct dwc3_ep *dep;
2500*4882a593Smuzhiyun
2501*4882a593Smuzhiyun dep = dwc->eps[epnum];
2502*4882a593Smuzhiyun if (!dep)
2503*4882a593Smuzhiyun continue;
2504*4882a593Smuzhiyun
2505*4882a593Smuzhiyun dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
2506*4882a593Smuzhiyun }
2507*4882a593Smuzhiyun }
2508*4882a593Smuzhiyun
__dwc3_gadget_set_ssp_rate(struct dwc3 * dwc)2509*4882a593Smuzhiyun static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2510*4882a593Smuzhiyun {
2511*4882a593Smuzhiyun enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate;
2512*4882a593Smuzhiyun u32 reg;
2513*4882a593Smuzhiyun
2514*4882a593Smuzhiyun if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2515*4882a593Smuzhiyun ssp_rate = dwc->max_ssp_rate;
2516*4882a593Smuzhiyun
2517*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2518*4882a593Smuzhiyun reg &= ~DWC3_DCFG_SPEED_MASK;
2519*4882a593Smuzhiyun reg &= ~DWC3_DCFG_NUMLANES(~0);
2520*4882a593Smuzhiyun
2521*4882a593Smuzhiyun if (ssp_rate == USB_SSP_GEN_1x2)
2522*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED;
2523*4882a593Smuzhiyun else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2524*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2525*4882a593Smuzhiyun
2526*4882a593Smuzhiyun if (ssp_rate != USB_SSP_GEN_2x1 &&
2527*4882a593Smuzhiyun dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2528*4882a593Smuzhiyun reg |= DWC3_DCFG_NUMLANES(1);
2529*4882a593Smuzhiyun
2530*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2531*4882a593Smuzhiyun }
2532*4882a593Smuzhiyun
__dwc3_gadget_set_speed(struct dwc3 * dwc)2533*4882a593Smuzhiyun static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
2534*4882a593Smuzhiyun {
2535*4882a593Smuzhiyun enum usb_device_speed speed;
2536*4882a593Smuzhiyun u32 reg;
2537*4882a593Smuzhiyun
2538*4882a593Smuzhiyun speed = dwc->gadget_max_speed;
2539*4882a593Smuzhiyun if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
2540*4882a593Smuzhiyun speed = dwc->maximum_speed;
2541*4882a593Smuzhiyun
2542*4882a593Smuzhiyun if (speed == USB_SPEED_SUPER_PLUS &&
2543*4882a593Smuzhiyun DWC3_IP_IS(DWC32)) {
2544*4882a593Smuzhiyun __dwc3_gadget_set_ssp_rate(dwc);
2545*4882a593Smuzhiyun return;
2546*4882a593Smuzhiyun }
2547*4882a593Smuzhiyun
2548*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2549*4882a593Smuzhiyun reg &= ~(DWC3_DCFG_SPEED_MASK);
2550*4882a593Smuzhiyun
2551*4882a593Smuzhiyun /*
2552*4882a593Smuzhiyun * WORKAROUND: DWC3 revision < 2.20a have an issue
2553*4882a593Smuzhiyun * which would cause metastability state on Run/Stop
2554*4882a593Smuzhiyun * bit if we try to force the IP to USB2-only mode.
2555*4882a593Smuzhiyun *
2556*4882a593Smuzhiyun * Because of that, we cannot configure the IP to any
2557*4882a593Smuzhiyun * speed other than the SuperSpeed
2558*4882a593Smuzhiyun *
2559*4882a593Smuzhiyun * Refers to:
2560*4882a593Smuzhiyun *
2561*4882a593Smuzhiyun * STAR#9000525659: Clock Domain Crossing on DCTL in
2562*4882a593Smuzhiyun * USB 2.0 Mode
2563*4882a593Smuzhiyun */
2564*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
2565*4882a593Smuzhiyun !dwc->dis_metastability_quirk) {
2566*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED;
2567*4882a593Smuzhiyun } else {
2568*4882a593Smuzhiyun switch (speed) {
2569*4882a593Smuzhiyun case USB_SPEED_LOW:
2570*4882a593Smuzhiyun reg |= DWC3_DCFG_LOWSPEED;
2571*4882a593Smuzhiyun break;
2572*4882a593Smuzhiyun case USB_SPEED_FULL:
2573*4882a593Smuzhiyun reg |= DWC3_DCFG_FULLSPEED;
2574*4882a593Smuzhiyun break;
2575*4882a593Smuzhiyun case USB_SPEED_HIGH:
2576*4882a593Smuzhiyun reg |= DWC3_DCFG_HIGHSPEED;
2577*4882a593Smuzhiyun break;
2578*4882a593Smuzhiyun case USB_SPEED_SUPER:
2579*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED;
2580*4882a593Smuzhiyun break;
2581*4882a593Smuzhiyun case USB_SPEED_SUPER_PLUS:
2582*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
2583*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED;
2584*4882a593Smuzhiyun else
2585*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2586*4882a593Smuzhiyun break;
2587*4882a593Smuzhiyun default:
2588*4882a593Smuzhiyun dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2589*4882a593Smuzhiyun
2590*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
2591*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED;
2592*4882a593Smuzhiyun else
2593*4882a593Smuzhiyun reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2594*4882a593Smuzhiyun }
2595*4882a593Smuzhiyun }
2596*4882a593Smuzhiyun
2597*4882a593Smuzhiyun if (DWC3_IP_IS(DWC32) &&
2598*4882a593Smuzhiyun speed > USB_SPEED_UNKNOWN &&
2599*4882a593Smuzhiyun speed < USB_SPEED_SUPER_PLUS)
2600*4882a593Smuzhiyun reg &= ~DWC3_DCFG_NUMLANES(~0);
2601*4882a593Smuzhiyun
2602*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2603*4882a593Smuzhiyun }
2604*4882a593Smuzhiyun
dwc3_gadget_run_stop(struct dwc3 * dwc,int is_on,int suspend)2605*4882a593Smuzhiyun static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
2606*4882a593Smuzhiyun {
2607*4882a593Smuzhiyun u32 reg;
2608*4882a593Smuzhiyun u32 timeout = 2000;
2609*4882a593Smuzhiyun
2610*4882a593Smuzhiyun if (pm_runtime_suspended(dwc->dev))
2611*4882a593Smuzhiyun return 0;
2612*4882a593Smuzhiyun
2613*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2614*4882a593Smuzhiyun if (is_on) {
2615*4882a593Smuzhiyun if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
2616*4882a593Smuzhiyun reg &= ~DWC3_DCTL_TRGTULST_MASK;
2617*4882a593Smuzhiyun reg |= DWC3_DCTL_TRGTULST_RX_DET;
2618*4882a593Smuzhiyun }
2619*4882a593Smuzhiyun
2620*4882a593Smuzhiyun if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
2621*4882a593Smuzhiyun reg &= ~DWC3_DCTL_KEEP_CONNECT;
2622*4882a593Smuzhiyun reg |= DWC3_DCTL_RUN_STOP;
2623*4882a593Smuzhiyun
2624*4882a593Smuzhiyun if (dwc->has_hibernation)
2625*4882a593Smuzhiyun reg |= DWC3_DCTL_KEEP_CONNECT;
2626*4882a593Smuzhiyun
2627*4882a593Smuzhiyun __dwc3_gadget_set_speed(dwc);
2628*4882a593Smuzhiyun dwc->pullups_connected = true;
2629*4882a593Smuzhiyun } else {
2630*4882a593Smuzhiyun reg &= ~DWC3_DCTL_RUN_STOP;
2631*4882a593Smuzhiyun
2632*4882a593Smuzhiyun if (dwc->has_hibernation && !suspend)
2633*4882a593Smuzhiyun reg &= ~DWC3_DCTL_KEEP_CONNECT;
2634*4882a593Smuzhiyun
2635*4882a593Smuzhiyun dwc->pullups_connected = false;
2636*4882a593Smuzhiyun }
2637*4882a593Smuzhiyun
2638*4882a593Smuzhiyun dwc3_gadget_dctl_write_safe(dwc, reg);
2639*4882a593Smuzhiyun
2640*4882a593Smuzhiyun do {
2641*4882a593Smuzhiyun usleep_range(1000, 2000);
2642*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2643*4882a593Smuzhiyun reg &= DWC3_DSTS_DEVCTRLHLT;
2644*4882a593Smuzhiyun } while (--timeout && !(!is_on ^ !reg));
2645*4882a593Smuzhiyun
2646*4882a593Smuzhiyun if (!timeout)
2647*4882a593Smuzhiyun return -ETIMEDOUT;
2648*4882a593Smuzhiyun
2649*4882a593Smuzhiyun return 0;
2650*4882a593Smuzhiyun }
2651*4882a593Smuzhiyun
2652*4882a593Smuzhiyun static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2653*4882a593Smuzhiyun static void __dwc3_gadget_stop(struct dwc3 *dwc);
2654*4882a593Smuzhiyun static int __dwc3_gadget_start(struct dwc3 *dwc);
2655*4882a593Smuzhiyun
dwc3_gadget_soft_disconnect(struct dwc3 * dwc)2656*4882a593Smuzhiyun static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
2657*4882a593Smuzhiyun {
2658*4882a593Smuzhiyun unsigned long flags;
2659*4882a593Smuzhiyun
2660*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2661*4882a593Smuzhiyun dwc->connected = false;
2662*4882a593Smuzhiyun
2663*4882a593Smuzhiyun /*
2664*4882a593Smuzhiyun * Per databook, when we want to stop the gadget, if a control transfer
2665*4882a593Smuzhiyun * is still in process, complete it and get the core into setup phase.
2666*4882a593Smuzhiyun */
2667*4882a593Smuzhiyun if (dwc->ep0state != EP0_SETUP_PHASE &&
2668*4882a593Smuzhiyun dwc->ep0state != EP0_UNCONNECTED) {
2669*4882a593Smuzhiyun int ret;
2670*4882a593Smuzhiyun
2671*4882a593Smuzhiyun if (dwc->delayed_status)
2672*4882a593Smuzhiyun dwc3_ep0_send_delayed_status(dwc);
2673*4882a593Smuzhiyun
2674*4882a593Smuzhiyun reinit_completion(&dwc->ep0_in_setup);
2675*4882a593Smuzhiyun
2676*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2677*4882a593Smuzhiyun ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
2678*4882a593Smuzhiyun msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
2679*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2680*4882a593Smuzhiyun if (ret == 0)
2681*4882a593Smuzhiyun dev_warn(dwc->dev, "timed out waiting for SETUP phase\n");
2682*4882a593Smuzhiyun }
2683*4882a593Smuzhiyun
2684*4882a593Smuzhiyun /*
2685*4882a593Smuzhiyun * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
2686*4882a593Smuzhiyun * Section 4.1.8 Table 4-7, it states that for a device-initiated
2687*4882a593Smuzhiyun * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2688*4882a593Smuzhiyun * command for any active transfers" before clearing the RunStop
2689*4882a593Smuzhiyun * bit.
2690*4882a593Smuzhiyun */
2691*4882a593Smuzhiyun dwc3_stop_active_transfers(dwc);
2692*4882a593Smuzhiyun __dwc3_gadget_stop(dwc);
2693*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2694*4882a593Smuzhiyun
2695*4882a593Smuzhiyun /*
2696*4882a593Smuzhiyun * Note: if the GEVNTCOUNT indicates events in the event buffer, the
2697*4882a593Smuzhiyun * driver needs to acknowledge them before the controller can halt.
2698*4882a593Smuzhiyun * Simply let the interrupt handler acknowledges and handle the
2699*4882a593Smuzhiyun * remaining event generated by the controller while polling for
2700*4882a593Smuzhiyun * DSTS.DEVCTLHLT.
2701*4882a593Smuzhiyun */
2702*4882a593Smuzhiyun return dwc3_gadget_run_stop(dwc, false, false);
2703*4882a593Smuzhiyun }
2704*4882a593Smuzhiyun
dwc3_gadget_pullup(struct usb_gadget * g,int is_on)2705*4882a593Smuzhiyun static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
2706*4882a593Smuzhiyun {
2707*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
2708*4882a593Smuzhiyun struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
2709*4882a593Smuzhiyun int ret;
2710*4882a593Smuzhiyun
2711*4882a593Smuzhiyun is_on = !!is_on;
2712*4882a593Smuzhiyun
2713*4882a593Smuzhiyun vdwc->softconnect = is_on;
2714*4882a593Smuzhiyun
2715*4882a593Smuzhiyun /*
2716*4882a593Smuzhiyun * Avoid issuing a runtime resume if the device is already in the
2717*4882a593Smuzhiyun * suspended state during gadget disconnect. DWC3 gadget was already
2718*4882a593Smuzhiyun * halted/stopped during runtime suspend.
2719*4882a593Smuzhiyun */
2720*4882a593Smuzhiyun if (!is_on) {
2721*4882a593Smuzhiyun pm_runtime_barrier(dwc->dev);
2722*4882a593Smuzhiyun if (pm_runtime_suspended(dwc->dev))
2723*4882a593Smuzhiyun return 0;
2724*4882a593Smuzhiyun }
2725*4882a593Smuzhiyun
2726*4882a593Smuzhiyun /*
2727*4882a593Smuzhiyun * Check the return value for successful resume, or error. For a
2728*4882a593Smuzhiyun * successful resume, the DWC3 runtime PM resume routine will handle
2729*4882a593Smuzhiyun * the run stop sequence, so avoid duplicate operations here.
2730*4882a593Smuzhiyun */
2731*4882a593Smuzhiyun ret = pm_runtime_get_sync(dwc->dev);
2732*4882a593Smuzhiyun if (!ret || ret < 0) {
2733*4882a593Smuzhiyun pm_runtime_put(dwc->dev);
2734*4882a593Smuzhiyun return 0;
2735*4882a593Smuzhiyun }
2736*4882a593Smuzhiyun
2737*4882a593Smuzhiyun if (dwc->pullups_connected == is_on) {
2738*4882a593Smuzhiyun pm_runtime_put(dwc->dev);
2739*4882a593Smuzhiyun return 0;
2740*4882a593Smuzhiyun }
2741*4882a593Smuzhiyun
2742*4882a593Smuzhiyun synchronize_irq(dwc->irq_gadget);
2743*4882a593Smuzhiyun
2744*4882a593Smuzhiyun if (!is_on) {
2745*4882a593Smuzhiyun ret = dwc3_gadget_soft_disconnect(dwc);
2746*4882a593Smuzhiyun } else {
2747*4882a593Smuzhiyun /*
2748*4882a593Smuzhiyun * In the Synopsys DWC_usb31 1.90a programming guide section
2749*4882a593Smuzhiyun * 4.1.9, it specifies that for a reconnect after a
2750*4882a593Smuzhiyun * device-initiated disconnect requires a core soft reset
2751*4882a593Smuzhiyun * (DCTL.CSftRst) before enabling the run/stop bit.
2752*4882a593Smuzhiyun */
2753*4882a593Smuzhiyun dwc3_core_soft_reset(dwc);
2754*4882a593Smuzhiyun
2755*4882a593Smuzhiyun dwc3_event_buffers_setup(dwc);
2756*4882a593Smuzhiyun __dwc3_gadget_start(dwc);
2757*4882a593Smuzhiyun ret = dwc3_gadget_run_stop(dwc, true, false);
2758*4882a593Smuzhiyun }
2759*4882a593Smuzhiyun
2760*4882a593Smuzhiyun pm_runtime_put(dwc->dev);
2761*4882a593Smuzhiyun
2762*4882a593Smuzhiyun return ret;
2763*4882a593Smuzhiyun }
2764*4882a593Smuzhiyun
dwc3_gadget_enable_irq(struct dwc3 * dwc)2765*4882a593Smuzhiyun static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
2766*4882a593Smuzhiyun {
2767*4882a593Smuzhiyun u32 reg;
2768*4882a593Smuzhiyun
2769*4882a593Smuzhiyun /* Enable all but Start and End of Frame IRQs */
2770*4882a593Smuzhiyun reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
2771*4882a593Smuzhiyun DWC3_DEVTEN_CMDCMPLTEN |
2772*4882a593Smuzhiyun DWC3_DEVTEN_ERRTICERREN |
2773*4882a593Smuzhiyun DWC3_DEVTEN_WKUPEVTEN |
2774*4882a593Smuzhiyun DWC3_DEVTEN_CONNECTDONEEN |
2775*4882a593Smuzhiyun DWC3_DEVTEN_USBRSTEN |
2776*4882a593Smuzhiyun DWC3_DEVTEN_DISCONNEVTEN);
2777*4882a593Smuzhiyun
2778*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 250A))
2779*4882a593Smuzhiyun reg |= DWC3_DEVTEN_ULSTCNGEN;
2780*4882a593Smuzhiyun
2781*4882a593Smuzhiyun /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2782*4882a593Smuzhiyun if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
2783*4882a593Smuzhiyun reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
2784*4882a593Smuzhiyun
2785*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2786*4882a593Smuzhiyun }
2787*4882a593Smuzhiyun
dwc3_gadget_disable_irq(struct dwc3 * dwc)2788*4882a593Smuzhiyun static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
2789*4882a593Smuzhiyun {
2790*4882a593Smuzhiyun /* mask all interrupts */
2791*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2792*4882a593Smuzhiyun }
2793*4882a593Smuzhiyun
2794*4882a593Smuzhiyun static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
2795*4882a593Smuzhiyun static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
2796*4882a593Smuzhiyun
2797*4882a593Smuzhiyun /**
2798*4882a593Smuzhiyun * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2799*4882a593Smuzhiyun * @dwc: pointer to our context structure
2800*4882a593Smuzhiyun *
2801*4882a593Smuzhiyun * The following looks like complex but it's actually very simple. In order to
2802*4882a593Smuzhiyun * calculate the number of packets we can burst at once on OUT transfers, we're
2803*4882a593Smuzhiyun * gonna use RxFIFO size.
2804*4882a593Smuzhiyun *
2805*4882a593Smuzhiyun * To calculate RxFIFO size we need two numbers:
2806*4882a593Smuzhiyun * MDWIDTH = size, in bits, of the internal memory bus
2807*4882a593Smuzhiyun * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
2808*4882a593Smuzhiyun *
2809*4882a593Smuzhiyun * Given these two numbers, the formula is simple:
2810*4882a593Smuzhiyun *
2811*4882a593Smuzhiyun * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2812*4882a593Smuzhiyun *
2813*4882a593Smuzhiyun * 24 bytes is for 3x SETUP packets
2814*4882a593Smuzhiyun * 16 bytes is a clock domain crossing tolerance
2815*4882a593Smuzhiyun *
2816*4882a593Smuzhiyun * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
2817*4882a593Smuzhiyun */
dwc3_gadget_setup_nump(struct dwc3 * dwc)2818*4882a593Smuzhiyun static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
2819*4882a593Smuzhiyun {
2820*4882a593Smuzhiyun u32 ram2_depth;
2821*4882a593Smuzhiyun u32 mdwidth;
2822*4882a593Smuzhiyun u32 nump;
2823*4882a593Smuzhiyun u32 reg;
2824*4882a593Smuzhiyun
2825*4882a593Smuzhiyun ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2826*4882a593Smuzhiyun mdwidth = dwc3_mdwidth(dwc);
2827*4882a593Smuzhiyun
2828*4882a593Smuzhiyun nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
2829*4882a593Smuzhiyun nump = min_t(u32, nump, 16);
2830*4882a593Smuzhiyun
2831*4882a593Smuzhiyun /* update NumP */
2832*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2833*4882a593Smuzhiyun reg &= ~DWC3_DCFG_NUMP_MASK;
2834*4882a593Smuzhiyun reg |= nump << DWC3_DCFG_NUMP_SHIFT;
2835*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2836*4882a593Smuzhiyun }
2837*4882a593Smuzhiyun
__dwc3_gadget_start(struct dwc3 * dwc)2838*4882a593Smuzhiyun static int __dwc3_gadget_start(struct dwc3 *dwc)
2839*4882a593Smuzhiyun {
2840*4882a593Smuzhiyun struct dwc3_ep *dep;
2841*4882a593Smuzhiyun int ret = 0;
2842*4882a593Smuzhiyun u32 reg;
2843*4882a593Smuzhiyun
2844*4882a593Smuzhiyun /*
2845*4882a593Smuzhiyun * If the DWC3 is in runtime suspend, the clocks maybe
2846*4882a593Smuzhiyun * disabled, so avoid enable the DWC3 endpoints here.
2847*4882a593Smuzhiyun * The DWC3 runtime PM resume routine will handle the
2848*4882a593Smuzhiyun * gadget start sequence.
2849*4882a593Smuzhiyun */
2850*4882a593Smuzhiyun if (pm_runtime_suspended(dwc->dev))
2851*4882a593Smuzhiyun return ret;
2852*4882a593Smuzhiyun
2853*4882a593Smuzhiyun /*
2854*4882a593Smuzhiyun * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2855*4882a593Smuzhiyun * the core supports IMOD, disable it.
2856*4882a593Smuzhiyun */
2857*4882a593Smuzhiyun if (dwc->imod_interval) {
2858*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2859*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2860*4882a593Smuzhiyun } else if (dwc3_has_imod(dwc)) {
2861*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2862*4882a593Smuzhiyun }
2863*4882a593Smuzhiyun
2864*4882a593Smuzhiyun /*
2865*4882a593Smuzhiyun * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
2866*4882a593Smuzhiyun * field instead of letting dwc3 itself calculate that automatically.
2867*4882a593Smuzhiyun *
2868*4882a593Smuzhiyun * This way, we maximize the chances that we'll be able to get several
2869*4882a593Smuzhiyun * bursts of data without going through any sort of endpoint throttling.
2870*4882a593Smuzhiyun */
2871*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
2872*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
2873*4882a593Smuzhiyun reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
2874*4882a593Smuzhiyun else
2875*4882a593Smuzhiyun reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
2876*4882a593Smuzhiyun
2877*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
2878*4882a593Smuzhiyun
2879*4882a593Smuzhiyun dwc3_gadget_setup_nump(dwc);
2880*4882a593Smuzhiyun
2881*4882a593Smuzhiyun /*
2882*4882a593Smuzhiyun * Currently the controller handles single stream only. So, Ignore
2883*4882a593Smuzhiyun * Packet Pending bit for stream selection and don't search for another
2884*4882a593Smuzhiyun * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2885*4882a593Smuzhiyun * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2886*4882a593Smuzhiyun * the stream performance.
2887*4882a593Smuzhiyun */
2888*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2889*4882a593Smuzhiyun reg |= DWC3_DCFG_IGNSTRMPP;
2890*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2891*4882a593Smuzhiyun
2892*4882a593Smuzhiyun /* Start with SuperSpeed Default */
2893*4882a593Smuzhiyun dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2894*4882a593Smuzhiyun
2895*4882a593Smuzhiyun dep = dwc->eps[0];
2896*4882a593Smuzhiyun dep->flags = 0;
2897*4882a593Smuzhiyun ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2898*4882a593Smuzhiyun if (ret) {
2899*4882a593Smuzhiyun dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2900*4882a593Smuzhiyun goto err0;
2901*4882a593Smuzhiyun }
2902*4882a593Smuzhiyun
2903*4882a593Smuzhiyun dep = dwc->eps[1];
2904*4882a593Smuzhiyun dep->flags = 0;
2905*4882a593Smuzhiyun ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
2906*4882a593Smuzhiyun if (ret) {
2907*4882a593Smuzhiyun dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2908*4882a593Smuzhiyun goto err1;
2909*4882a593Smuzhiyun }
2910*4882a593Smuzhiyun
2911*4882a593Smuzhiyun /* begin to receive SETUP packets */
2912*4882a593Smuzhiyun dwc->ep0state = EP0_SETUP_PHASE;
2913*4882a593Smuzhiyun dwc->ep0_bounced = false;
2914*4882a593Smuzhiyun dwc->link_state = DWC3_LINK_STATE_SS_DIS;
2915*4882a593Smuzhiyun dwc->delayed_status = false;
2916*4882a593Smuzhiyun dwc3_ep0_out_start(dwc);
2917*4882a593Smuzhiyun
2918*4882a593Smuzhiyun dwc3_gadget_enable_irq(dwc);
2919*4882a593Smuzhiyun
2920*4882a593Smuzhiyun return 0;
2921*4882a593Smuzhiyun
2922*4882a593Smuzhiyun err1:
2923*4882a593Smuzhiyun __dwc3_gadget_ep_disable(dwc->eps[0]);
2924*4882a593Smuzhiyun
2925*4882a593Smuzhiyun err0:
2926*4882a593Smuzhiyun return ret;
2927*4882a593Smuzhiyun }
2928*4882a593Smuzhiyun
dwc3_gadget_start(struct usb_gadget * g,struct usb_gadget_driver * driver)2929*4882a593Smuzhiyun static int dwc3_gadget_start(struct usb_gadget *g,
2930*4882a593Smuzhiyun struct usb_gadget_driver *driver)
2931*4882a593Smuzhiyun {
2932*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
2933*4882a593Smuzhiyun unsigned long flags;
2934*4882a593Smuzhiyun int ret = 0;
2935*4882a593Smuzhiyun int irq;
2936*4882a593Smuzhiyun
2937*4882a593Smuzhiyun irq = dwc->irq_gadget;
2938*4882a593Smuzhiyun ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
2939*4882a593Smuzhiyun IRQF_SHARED, "dwc3", dwc->ev_buf);
2940*4882a593Smuzhiyun if (ret) {
2941*4882a593Smuzhiyun dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2942*4882a593Smuzhiyun irq, ret);
2943*4882a593Smuzhiyun goto err0;
2944*4882a593Smuzhiyun }
2945*4882a593Smuzhiyun
2946*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2947*4882a593Smuzhiyun if (dwc->gadget_driver) {
2948*4882a593Smuzhiyun dev_err(dwc->dev, "%s is already bound to %s\n",
2949*4882a593Smuzhiyun dwc->gadget->name,
2950*4882a593Smuzhiyun dwc->gadget_driver->driver.name);
2951*4882a593Smuzhiyun ret = -EBUSY;
2952*4882a593Smuzhiyun goto err1;
2953*4882a593Smuzhiyun }
2954*4882a593Smuzhiyun
2955*4882a593Smuzhiyun dwc->gadget_driver = driver;
2956*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2957*4882a593Smuzhiyun
2958*4882a593Smuzhiyun return 0;
2959*4882a593Smuzhiyun
2960*4882a593Smuzhiyun err1:
2961*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2962*4882a593Smuzhiyun free_irq(irq, dwc);
2963*4882a593Smuzhiyun
2964*4882a593Smuzhiyun err0:
2965*4882a593Smuzhiyun return ret;
2966*4882a593Smuzhiyun }
2967*4882a593Smuzhiyun
__dwc3_gadget_stop(struct dwc3 * dwc)2968*4882a593Smuzhiyun static void __dwc3_gadget_stop(struct dwc3 *dwc)
2969*4882a593Smuzhiyun {
2970*4882a593Smuzhiyun dwc3_gadget_disable_irq(dwc);
2971*4882a593Smuzhiyun __dwc3_gadget_ep_disable(dwc->eps[0]);
2972*4882a593Smuzhiyun __dwc3_gadget_ep_disable(dwc->eps[1]);
2973*4882a593Smuzhiyun }
2974*4882a593Smuzhiyun
dwc3_gadget_stop(struct usb_gadget * g)2975*4882a593Smuzhiyun static int dwc3_gadget_stop(struct usb_gadget *g)
2976*4882a593Smuzhiyun {
2977*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
2978*4882a593Smuzhiyun unsigned long flags;
2979*4882a593Smuzhiyun
2980*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
2981*4882a593Smuzhiyun if (!dwc->gadget_driver) {
2982*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2983*4882a593Smuzhiyun dev_warn(dwc->dev, "%s is already stopped\n",
2984*4882a593Smuzhiyun dwc->gadget->name);
2985*4882a593Smuzhiyun goto out;
2986*4882a593Smuzhiyun }
2987*4882a593Smuzhiyun dwc->gadget_driver = NULL;
2988*4882a593Smuzhiyun dwc->max_cfg_eps = 0;
2989*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
2990*4882a593Smuzhiyun
2991*4882a593Smuzhiyun free_irq(dwc->irq_gadget, dwc->ev_buf);
2992*4882a593Smuzhiyun
2993*4882a593Smuzhiyun out:
2994*4882a593Smuzhiyun return 0;
2995*4882a593Smuzhiyun }
2996*4882a593Smuzhiyun
dwc3_gadget_config_params(struct usb_gadget * g,struct usb_dcd_config_params * params)2997*4882a593Smuzhiyun static void dwc3_gadget_config_params(struct usb_gadget *g,
2998*4882a593Smuzhiyun struct usb_dcd_config_params *params)
2999*4882a593Smuzhiyun {
3000*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
3001*4882a593Smuzhiyun
3002*4882a593Smuzhiyun params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
3003*4882a593Smuzhiyun params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
3004*4882a593Smuzhiyun
3005*4882a593Smuzhiyun /* Recommended BESL */
3006*4882a593Smuzhiyun if (!dwc->dis_enblslpm_quirk) {
3007*4882a593Smuzhiyun /*
3008*4882a593Smuzhiyun * If the recommended BESL baseline is 0 or if the BESL deep is
3009*4882a593Smuzhiyun * less than 2, Microsoft's Windows 10 host usb stack will issue
3010*4882a593Smuzhiyun * a usb reset immediately after it receives the extended BOS
3011*4882a593Smuzhiyun * descriptor and the enumeration will fail. To maintain
3012*4882a593Smuzhiyun * compatibility with the Windows' usb stack, let's set the
3013*4882a593Smuzhiyun * recommended BESL baseline to 1 and clamp the BESL deep to be
3014*4882a593Smuzhiyun * within 2 to 15.
3015*4882a593Smuzhiyun */
3016*4882a593Smuzhiyun params->besl_baseline = 1;
3017*4882a593Smuzhiyun if (dwc->is_utmi_l1_suspend)
3018*4882a593Smuzhiyun params->besl_deep =
3019*4882a593Smuzhiyun clamp_t(u8, dwc->hird_threshold, 2, 15);
3020*4882a593Smuzhiyun }
3021*4882a593Smuzhiyun
3022*4882a593Smuzhiyun /* U1 Device exit Latency */
3023*4882a593Smuzhiyun if (dwc->dis_u1_entry_quirk)
3024*4882a593Smuzhiyun params->bU1devExitLat = 0;
3025*4882a593Smuzhiyun else
3026*4882a593Smuzhiyun params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
3027*4882a593Smuzhiyun
3028*4882a593Smuzhiyun /* U2 Device exit Latency */
3029*4882a593Smuzhiyun if (dwc->dis_u2_entry_quirk)
3030*4882a593Smuzhiyun params->bU2DevExitLat = 0;
3031*4882a593Smuzhiyun else
3032*4882a593Smuzhiyun params->bU2DevExitLat =
3033*4882a593Smuzhiyun cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
3034*4882a593Smuzhiyun }
3035*4882a593Smuzhiyun
dwc3_gadget_set_speed(struct usb_gadget * g,enum usb_device_speed speed)3036*4882a593Smuzhiyun static void dwc3_gadget_set_speed(struct usb_gadget *g,
3037*4882a593Smuzhiyun enum usb_device_speed speed)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
3040*4882a593Smuzhiyun unsigned long flags;
3041*4882a593Smuzhiyun
3042*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
3043*4882a593Smuzhiyun dwc->gadget_max_speed = speed;
3044*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
3045*4882a593Smuzhiyun }
3046*4882a593Smuzhiyun
dwc3_gadget_set_ssp_rate(struct usb_gadget * g,enum usb_ssp_rate rate)3047*4882a593Smuzhiyun static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
3048*4882a593Smuzhiyun enum usb_ssp_rate rate)
3049*4882a593Smuzhiyun {
3050*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
3051*4882a593Smuzhiyun unsigned long flags;
3052*4882a593Smuzhiyun
3053*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
3054*4882a593Smuzhiyun dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
3055*4882a593Smuzhiyun dwc->gadget_ssp_rate = rate;
3056*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
3057*4882a593Smuzhiyun }
3058*4882a593Smuzhiyun
dwc3_gadget_vbus_draw(struct usb_gadget * g,unsigned int mA)3059*4882a593Smuzhiyun static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
3060*4882a593Smuzhiyun {
3061*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
3062*4882a593Smuzhiyun union power_supply_propval val = {0};
3063*4882a593Smuzhiyun int ret;
3064*4882a593Smuzhiyun
3065*4882a593Smuzhiyun if (dwc->usb2_phy)
3066*4882a593Smuzhiyun return usb_phy_set_power(dwc->usb2_phy, mA);
3067*4882a593Smuzhiyun
3068*4882a593Smuzhiyun if (!dwc->usb_psy)
3069*4882a593Smuzhiyun return -EOPNOTSUPP;
3070*4882a593Smuzhiyun
3071*4882a593Smuzhiyun val.intval = 1000 * mA;
3072*4882a593Smuzhiyun ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
3073*4882a593Smuzhiyun
3074*4882a593Smuzhiyun return ret;
3075*4882a593Smuzhiyun }
3076*4882a593Smuzhiyun
3077*4882a593Smuzhiyun /**
3078*4882a593Smuzhiyun * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
3079*4882a593Smuzhiyun * @g: pointer to the USB gadget
3080*4882a593Smuzhiyun *
3081*4882a593Smuzhiyun * Used to record the maximum number of endpoints being used in a USB composite
3082*4882a593Smuzhiyun * device. (across all configurations) This is to be used in the calculation
3083*4882a593Smuzhiyun * of the TXFIFO sizes when resizing internal memory for individual endpoints.
3084*4882a593Smuzhiyun * It will help ensured that the resizing logic reserves enough space for at
3085*4882a593Smuzhiyun * least one max packet.
3086*4882a593Smuzhiyun */
dwc3_gadget_check_config(struct usb_gadget * g)3087*4882a593Smuzhiyun static int dwc3_gadget_check_config(struct usb_gadget *g)
3088*4882a593Smuzhiyun {
3089*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
3090*4882a593Smuzhiyun struct usb_ep *ep;
3091*4882a593Smuzhiyun int fifo_size = 0;
3092*4882a593Smuzhiyun int ram1_depth;
3093*4882a593Smuzhiyun int ep_num = 0;
3094*4882a593Smuzhiyun
3095*4882a593Smuzhiyun if (!dwc->do_fifo_resize)
3096*4882a593Smuzhiyun return 0;
3097*4882a593Smuzhiyun
3098*4882a593Smuzhiyun list_for_each_entry(ep, &g->ep_list, ep_list) {
3099*4882a593Smuzhiyun /* Only interested in the IN endpoints */
3100*4882a593Smuzhiyun if (ep->claimed && (ep->address & USB_DIR_IN))
3101*4882a593Smuzhiyun ep_num++;
3102*4882a593Smuzhiyun }
3103*4882a593Smuzhiyun
3104*4882a593Smuzhiyun if (ep_num <= dwc->max_cfg_eps)
3105*4882a593Smuzhiyun return 0;
3106*4882a593Smuzhiyun
3107*4882a593Smuzhiyun /* Update the max number of eps in the composition */
3108*4882a593Smuzhiyun dwc->max_cfg_eps = ep_num;
3109*4882a593Smuzhiyun
3110*4882a593Smuzhiyun fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps);
3111*4882a593Smuzhiyun /* Based on the equation, increment by one for every ep */
3112*4882a593Smuzhiyun fifo_size += dwc->max_cfg_eps;
3113*4882a593Smuzhiyun
3114*4882a593Smuzhiyun /* Check if we can fit a single fifo per endpoint */
3115*4882a593Smuzhiyun ram1_depth = dwc3_gadget_get_tx_fifos_size(dwc);
3116*4882a593Smuzhiyun if (fifo_size > ram1_depth)
3117*4882a593Smuzhiyun return -ENOMEM;
3118*4882a593Smuzhiyun
3119*4882a593Smuzhiyun return 0;
3120*4882a593Smuzhiyun }
3121*4882a593Smuzhiyun
dwc3_gadget_async_callbacks(struct usb_gadget * g,bool enable)3122*4882a593Smuzhiyun static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
3123*4882a593Smuzhiyun {
3124*4882a593Smuzhiyun struct dwc3 *dwc = gadget_to_dwc(g);
3125*4882a593Smuzhiyun unsigned long flags;
3126*4882a593Smuzhiyun
3127*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
3128*4882a593Smuzhiyun dwc->async_callbacks = enable;
3129*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
3130*4882a593Smuzhiyun }
3131*4882a593Smuzhiyun
3132*4882a593Smuzhiyun static const struct usb_gadget_ops dwc3_gadget_ops = {
3133*4882a593Smuzhiyun .get_frame = dwc3_gadget_get_frame,
3134*4882a593Smuzhiyun .wakeup = dwc3_gadget_wakeup,
3135*4882a593Smuzhiyun .set_selfpowered = dwc3_gadget_set_selfpowered,
3136*4882a593Smuzhiyun .pullup = dwc3_gadget_pullup,
3137*4882a593Smuzhiyun .udc_start = dwc3_gadget_start,
3138*4882a593Smuzhiyun .udc_stop = dwc3_gadget_stop,
3139*4882a593Smuzhiyun .udc_set_speed = dwc3_gadget_set_speed,
3140*4882a593Smuzhiyun .udc_set_ssp_rate = dwc3_gadget_set_ssp_rate,
3141*4882a593Smuzhiyun .get_config_params = dwc3_gadget_config_params,
3142*4882a593Smuzhiyun .vbus_draw = dwc3_gadget_vbus_draw,
3143*4882a593Smuzhiyun .check_config = dwc3_gadget_check_config,
3144*4882a593Smuzhiyun .udc_async_callbacks = dwc3_gadget_async_callbacks,
3145*4882a593Smuzhiyun };
3146*4882a593Smuzhiyun
3147*4882a593Smuzhiyun /* -------------------------------------------------------------------------- */
3148*4882a593Smuzhiyun
dwc3_gadget_init_control_endpoint(struct dwc3_ep * dep)3149*4882a593Smuzhiyun static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
3150*4882a593Smuzhiyun {
3151*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3152*4882a593Smuzhiyun
3153*4882a593Smuzhiyun usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
3154*4882a593Smuzhiyun dep->endpoint.maxburst = 1;
3155*4882a593Smuzhiyun dep->endpoint.ops = &dwc3_gadget_ep0_ops;
3156*4882a593Smuzhiyun if (!dep->direction)
3157*4882a593Smuzhiyun dwc->gadget->ep0 = &dep->endpoint;
3158*4882a593Smuzhiyun
3159*4882a593Smuzhiyun dep->endpoint.caps.type_control = true;
3160*4882a593Smuzhiyun
3161*4882a593Smuzhiyun return 0;
3162*4882a593Smuzhiyun }
3163*4882a593Smuzhiyun
dwc3_gadget_init_in_endpoint(struct dwc3_ep * dep)3164*4882a593Smuzhiyun static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
3165*4882a593Smuzhiyun {
3166*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3167*4882a593Smuzhiyun u32 mdwidth;
3168*4882a593Smuzhiyun int size;
3169*4882a593Smuzhiyun int maxpacket;
3170*4882a593Smuzhiyun
3171*4882a593Smuzhiyun mdwidth = dwc3_mdwidth(dwc);
3172*4882a593Smuzhiyun
3173*4882a593Smuzhiyun /* MDWIDTH is represented in bits, we need it in bytes */
3174*4882a593Smuzhiyun mdwidth /= 8;
3175*4882a593Smuzhiyun
3176*4882a593Smuzhiyun size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
3177*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
3178*4882a593Smuzhiyun size = DWC3_GTXFIFOSIZ_TXFDEP(size);
3179*4882a593Smuzhiyun else
3180*4882a593Smuzhiyun size = DWC31_GTXFIFOSIZ_TXFDEP(size);
3181*4882a593Smuzhiyun
3182*4882a593Smuzhiyun /*
3183*4882a593Smuzhiyun * maxpacket size is determined as part of the following, after assuming
3184*4882a593Smuzhiyun * a mult value of one maxpacket:
3185*4882a593Smuzhiyun * DWC3 revision 280A and prior:
3186*4882a593Smuzhiyun * fifo_size = mult * (max_packet / mdwidth) + 1;
3187*4882a593Smuzhiyun * maxpacket = mdwidth * (fifo_size - 1);
3188*4882a593Smuzhiyun *
3189*4882a593Smuzhiyun * DWC3 revision 290A and onwards:
3190*4882a593Smuzhiyun * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
3191*4882a593Smuzhiyun * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth;
3192*4882a593Smuzhiyun */
3193*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 290A))
3194*4882a593Smuzhiyun maxpacket = mdwidth * (size - 1);
3195*4882a593Smuzhiyun else
3196*4882a593Smuzhiyun maxpacket = mdwidth * ((size - 1) - 1) - mdwidth;
3197*4882a593Smuzhiyun
3198*4882a593Smuzhiyun
3199*4882a593Smuzhiyun /*
3200*4882a593Smuzhiyun * To meet performance requirement, a minimum TxFIFO size of 2x
3201*4882a593Smuzhiyun * MaxPacketSize is recommended for endpoints that support for
3202*4882a593Smuzhiyun * Rockchip platform with UVC function.
3203*4882a593Smuzhiyun */
3204*4882a593Smuzhiyun if (IS_REACHABLE(CONFIG_ARCH_ROCKCHIP) &&
3205*4882a593Smuzhiyun (dwc->maximum_speed >= USB_SPEED_HIGH))
3206*4882a593Smuzhiyun maxpacket /= 2;
3207*4882a593Smuzhiyun
3208*4882a593Smuzhiyun /* Functionally, space for one max packet is sufficient */
3209*4882a593Smuzhiyun size = min_t(int, maxpacket, 1024);
3210*4882a593Smuzhiyun /*
3211*4882a593Smuzhiyun * If enable tx fifos resize, set each in ep maxpacket
3212*4882a593Smuzhiyun * to 1024, it can avoid being dependent on the default
3213*4882a593Smuzhiyun * fifo size, and more flexible use of endpoints.
3214*4882a593Smuzhiyun */
3215*4882a593Smuzhiyun if (dwc->do_fifo_resize)
3216*4882a593Smuzhiyun size = 1024;
3217*4882a593Smuzhiyun usb_ep_set_maxpacket_limit(&dep->endpoint, size);
3218*4882a593Smuzhiyun
3219*4882a593Smuzhiyun dep->endpoint.max_streams = 16;
3220*4882a593Smuzhiyun dep->endpoint.ops = &dwc3_gadget_ep_ops;
3221*4882a593Smuzhiyun list_add_tail(&dep->endpoint.ep_list,
3222*4882a593Smuzhiyun &dwc->gadget->ep_list);
3223*4882a593Smuzhiyun dep->endpoint.caps.type_iso = true;
3224*4882a593Smuzhiyun dep->endpoint.caps.type_bulk = true;
3225*4882a593Smuzhiyun dep->endpoint.caps.type_int = true;
3226*4882a593Smuzhiyun
3227*4882a593Smuzhiyun return dwc3_alloc_trb_pool(dep);
3228*4882a593Smuzhiyun }
3229*4882a593Smuzhiyun
dwc3_gadget_init_out_endpoint(struct dwc3_ep * dep)3230*4882a593Smuzhiyun static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
3231*4882a593Smuzhiyun {
3232*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3233*4882a593Smuzhiyun u32 mdwidth;
3234*4882a593Smuzhiyun int size;
3235*4882a593Smuzhiyun
3236*4882a593Smuzhiyun mdwidth = dwc3_mdwidth(dwc);
3237*4882a593Smuzhiyun
3238*4882a593Smuzhiyun /* MDWIDTH is represented in bits, convert to bytes */
3239*4882a593Smuzhiyun mdwidth /= 8;
3240*4882a593Smuzhiyun
3241*4882a593Smuzhiyun /* All OUT endpoints share a single RxFIFO space */
3242*4882a593Smuzhiyun size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
3243*4882a593Smuzhiyun if (DWC3_IP_IS(DWC3))
3244*4882a593Smuzhiyun size = DWC3_GRXFIFOSIZ_RXFDEP(size);
3245*4882a593Smuzhiyun else
3246*4882a593Smuzhiyun size = DWC31_GRXFIFOSIZ_RXFDEP(size);
3247*4882a593Smuzhiyun
3248*4882a593Smuzhiyun /* FIFO depth is in MDWDITH bytes */
3249*4882a593Smuzhiyun size *= mdwidth;
3250*4882a593Smuzhiyun
3251*4882a593Smuzhiyun /*
3252*4882a593Smuzhiyun * To meet performance requirement, a minimum recommended RxFIFO size
3253*4882a593Smuzhiyun * is defined as follow:
3254*4882a593Smuzhiyun * RxFIFO size >= (3 x MaxPacketSize) +
3255*4882a593Smuzhiyun * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
3256*4882a593Smuzhiyun *
3257*4882a593Smuzhiyun * Then calculate the max packet limit as below.
3258*4882a593Smuzhiyun */
3259*4882a593Smuzhiyun size -= (3 * 8) + 16;
3260*4882a593Smuzhiyun if (size < 0)
3261*4882a593Smuzhiyun size = 0;
3262*4882a593Smuzhiyun else
3263*4882a593Smuzhiyun size /= 3;
3264*4882a593Smuzhiyun
3265*4882a593Smuzhiyun usb_ep_set_maxpacket_limit(&dep->endpoint, size);
3266*4882a593Smuzhiyun dep->endpoint.max_streams = 16;
3267*4882a593Smuzhiyun dep->endpoint.ops = &dwc3_gadget_ep_ops;
3268*4882a593Smuzhiyun list_add_tail(&dep->endpoint.ep_list,
3269*4882a593Smuzhiyun &dwc->gadget->ep_list);
3270*4882a593Smuzhiyun dep->endpoint.caps.type_iso = true;
3271*4882a593Smuzhiyun dep->endpoint.caps.type_bulk = true;
3272*4882a593Smuzhiyun dep->endpoint.caps.type_int = true;
3273*4882a593Smuzhiyun
3274*4882a593Smuzhiyun return dwc3_alloc_trb_pool(dep);
3275*4882a593Smuzhiyun }
3276*4882a593Smuzhiyun
dwc3_gadget_init_endpoint(struct dwc3 * dwc,u8 epnum)3277*4882a593Smuzhiyun static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
3278*4882a593Smuzhiyun {
3279*4882a593Smuzhiyun struct dwc3_ep *dep;
3280*4882a593Smuzhiyun bool direction = epnum & 1;
3281*4882a593Smuzhiyun int ret;
3282*4882a593Smuzhiyun u8 num = epnum >> 1;
3283*4882a593Smuzhiyun u8 num_in_eps, num_out_eps, min_eps;
3284*4882a593Smuzhiyun
3285*4882a593Smuzhiyun dep = kzalloc(sizeof(*dep), GFP_KERNEL);
3286*4882a593Smuzhiyun if (!dep)
3287*4882a593Smuzhiyun return -ENOMEM;
3288*4882a593Smuzhiyun
3289*4882a593Smuzhiyun num_in_eps = DWC3_NUM_IN_EPS(&dwc->hwparams);
3290*4882a593Smuzhiyun num_out_eps = dwc->num_eps - num_in_eps;
3291*4882a593Smuzhiyun min_eps = min_t(u8, num_in_eps, num_out_eps);
3292*4882a593Smuzhiyun
3293*4882a593Smuzhiyun /* reconfig direction and num if num_out_eps != num_in_eps */
3294*4882a593Smuzhiyun if (num + 1 > min_eps && num_in_eps != num_out_eps) {
3295*4882a593Smuzhiyun num = epnum - min_eps;
3296*4882a593Smuzhiyun direction = num + 1 > num_out_eps ? 1 : 0;
3297*4882a593Smuzhiyun }
3298*4882a593Smuzhiyun
3299*4882a593Smuzhiyun dep->dwc = dwc;
3300*4882a593Smuzhiyun dep->number = num << 1 | direction;
3301*4882a593Smuzhiyun dep->direction = direction;
3302*4882a593Smuzhiyun dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
3303*4882a593Smuzhiyun dwc->eps[epnum] = dep;
3304*4882a593Smuzhiyun dep->combo_num = 0;
3305*4882a593Smuzhiyun dep->start_cmd_status = 0;
3306*4882a593Smuzhiyun
3307*4882a593Smuzhiyun snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
3308*4882a593Smuzhiyun direction ? "in" : "out");
3309*4882a593Smuzhiyun
3310*4882a593Smuzhiyun dep->endpoint.name = dep->name;
3311*4882a593Smuzhiyun
3312*4882a593Smuzhiyun if (!(dep->number > 1)) {
3313*4882a593Smuzhiyun dep->endpoint.desc = &dwc3_gadget_ep0_desc;
3314*4882a593Smuzhiyun dep->endpoint.comp_desc = NULL;
3315*4882a593Smuzhiyun }
3316*4882a593Smuzhiyun
3317*4882a593Smuzhiyun if (num == 0)
3318*4882a593Smuzhiyun ret = dwc3_gadget_init_control_endpoint(dep);
3319*4882a593Smuzhiyun else if (direction)
3320*4882a593Smuzhiyun ret = dwc3_gadget_init_in_endpoint(dep);
3321*4882a593Smuzhiyun else
3322*4882a593Smuzhiyun ret = dwc3_gadget_init_out_endpoint(dep);
3323*4882a593Smuzhiyun
3324*4882a593Smuzhiyun if (ret)
3325*4882a593Smuzhiyun return ret;
3326*4882a593Smuzhiyun
3327*4882a593Smuzhiyun dep->endpoint.caps.dir_in = direction;
3328*4882a593Smuzhiyun dep->endpoint.caps.dir_out = !direction;
3329*4882a593Smuzhiyun
3330*4882a593Smuzhiyun INIT_LIST_HEAD(&dep->pending_list);
3331*4882a593Smuzhiyun INIT_LIST_HEAD(&dep->started_list);
3332*4882a593Smuzhiyun INIT_LIST_HEAD(&dep->cancelled_list);
3333*4882a593Smuzhiyun
3334*4882a593Smuzhiyun dwc3_debugfs_create_endpoint_dir(dep);
3335*4882a593Smuzhiyun
3336*4882a593Smuzhiyun return 0;
3337*4882a593Smuzhiyun }
3338*4882a593Smuzhiyun
dwc3_gadget_init_endpoints(struct dwc3 * dwc,u8 total)3339*4882a593Smuzhiyun static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
3340*4882a593Smuzhiyun {
3341*4882a593Smuzhiyun u8 epnum;
3342*4882a593Smuzhiyun
3343*4882a593Smuzhiyun INIT_LIST_HEAD(&dwc->gadget->ep_list);
3344*4882a593Smuzhiyun
3345*4882a593Smuzhiyun for (epnum = 0; epnum < total; epnum++) {
3346*4882a593Smuzhiyun int ret;
3347*4882a593Smuzhiyun
3348*4882a593Smuzhiyun ret = dwc3_gadget_init_endpoint(dwc, epnum);
3349*4882a593Smuzhiyun if (ret)
3350*4882a593Smuzhiyun return ret;
3351*4882a593Smuzhiyun }
3352*4882a593Smuzhiyun
3353*4882a593Smuzhiyun return 0;
3354*4882a593Smuzhiyun }
3355*4882a593Smuzhiyun
dwc3_gadget_free_endpoints(struct dwc3 * dwc)3356*4882a593Smuzhiyun static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
3357*4882a593Smuzhiyun {
3358*4882a593Smuzhiyun struct dwc3_ep *dep;
3359*4882a593Smuzhiyun u8 epnum;
3360*4882a593Smuzhiyun
3361*4882a593Smuzhiyun for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
3362*4882a593Smuzhiyun dep = dwc->eps[epnum];
3363*4882a593Smuzhiyun if (!dep)
3364*4882a593Smuzhiyun continue;
3365*4882a593Smuzhiyun /*
3366*4882a593Smuzhiyun * Physical endpoints 0 and 1 are special; they form the
3367*4882a593Smuzhiyun * bi-directional USB endpoint 0.
3368*4882a593Smuzhiyun *
3369*4882a593Smuzhiyun * For those two physical endpoints, we don't allocate a TRB
3370*4882a593Smuzhiyun * pool nor do we add them the endpoints list. Due to that, we
3371*4882a593Smuzhiyun * shouldn't do these two operations otherwise we would end up
3372*4882a593Smuzhiyun * with all sorts of bugs when removing dwc3.ko.
3373*4882a593Smuzhiyun */
3374*4882a593Smuzhiyun if (epnum != 0 && epnum != 1) {
3375*4882a593Smuzhiyun dwc3_free_trb_pool(dep);
3376*4882a593Smuzhiyun list_del(&dep->endpoint.ep_list);
3377*4882a593Smuzhiyun }
3378*4882a593Smuzhiyun
3379*4882a593Smuzhiyun debugfs_remove_recursive(debugfs_lookup(dep->name, dwc->root));
3380*4882a593Smuzhiyun kfree(dep);
3381*4882a593Smuzhiyun }
3382*4882a593Smuzhiyun }
3383*4882a593Smuzhiyun
3384*4882a593Smuzhiyun /* -------------------------------------------------------------------------- */
3385*4882a593Smuzhiyun
dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep * dep,struct dwc3_request * req,struct dwc3_trb * trb,const struct dwc3_event_depevt * event,int status,int chain)3386*4882a593Smuzhiyun static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
3387*4882a593Smuzhiyun struct dwc3_request *req, struct dwc3_trb *trb,
3388*4882a593Smuzhiyun const struct dwc3_event_depevt *event, int status, int chain)
3389*4882a593Smuzhiyun {
3390*4882a593Smuzhiyun unsigned int count;
3391*4882a593Smuzhiyun
3392*4882a593Smuzhiyun dwc3_ep_inc_deq(dep);
3393*4882a593Smuzhiyun
3394*4882a593Smuzhiyun trace_dwc3_complete_trb(dep, trb);
3395*4882a593Smuzhiyun req->num_trbs--;
3396*4882a593Smuzhiyun
3397*4882a593Smuzhiyun /*
3398*4882a593Smuzhiyun * If we're in the middle of series of chained TRBs and we
3399*4882a593Smuzhiyun * receive a short transfer along the way, DWC3 will skip
3400*4882a593Smuzhiyun * through all TRBs including the last TRB in the chain (the
3401*4882a593Smuzhiyun * where CHN bit is zero. DWC3 will also avoid clearing HWO
3402*4882a593Smuzhiyun * bit and SW has to do it manually.
3403*4882a593Smuzhiyun *
3404*4882a593Smuzhiyun * We're going to do that here to avoid problems of HW trying
3405*4882a593Smuzhiyun * to use bogus TRBs for transfers.
3406*4882a593Smuzhiyun */
3407*4882a593Smuzhiyun if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
3408*4882a593Smuzhiyun trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3409*4882a593Smuzhiyun
3410*4882a593Smuzhiyun /*
3411*4882a593Smuzhiyun * For isochronous transfers, the first TRB in a service interval must
3412*4882a593Smuzhiyun * have the Isoc-First type. Track and report its interval frame number.
3413*4882a593Smuzhiyun */
3414*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3415*4882a593Smuzhiyun (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
3416*4882a593Smuzhiyun unsigned int frame_number;
3417*4882a593Smuzhiyun
3418*4882a593Smuzhiyun frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
3419*4882a593Smuzhiyun frame_number &= ~(dep->interval - 1);
3420*4882a593Smuzhiyun req->request.frame_number = frame_number;
3421*4882a593Smuzhiyun }
3422*4882a593Smuzhiyun
3423*4882a593Smuzhiyun /*
3424*4882a593Smuzhiyun * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
3425*4882a593Smuzhiyun * this TRB points to the bounce buffer address, it's a MPS alignment
3426*4882a593Smuzhiyun * TRB. Don't add it to req->remaining calculation.
3427*4882a593Smuzhiyun */
3428*4882a593Smuzhiyun if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
3429*4882a593Smuzhiyun trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
3430*4882a593Smuzhiyun trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3431*4882a593Smuzhiyun return 1;
3432*4882a593Smuzhiyun }
3433*4882a593Smuzhiyun
3434*4882a593Smuzhiyun count = trb->size & DWC3_TRB_SIZE_MASK;
3435*4882a593Smuzhiyun req->remaining += count;
3436*4882a593Smuzhiyun
3437*4882a593Smuzhiyun if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
3438*4882a593Smuzhiyun return 1;
3439*4882a593Smuzhiyun
3440*4882a593Smuzhiyun if (event->status & DEPEVT_STATUS_SHORT && !chain)
3441*4882a593Smuzhiyun return 1;
3442*4882a593Smuzhiyun
3443*4882a593Smuzhiyun if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
3444*4882a593Smuzhiyun DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
3445*4882a593Smuzhiyun return 1;
3446*4882a593Smuzhiyun
3447*4882a593Smuzhiyun if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
3448*4882a593Smuzhiyun (trb->ctrl & DWC3_TRB_CTRL_LST))
3449*4882a593Smuzhiyun return 1;
3450*4882a593Smuzhiyun
3451*4882a593Smuzhiyun return 0;
3452*4882a593Smuzhiyun }
3453*4882a593Smuzhiyun
dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep * dep,struct dwc3_request * req,const struct dwc3_event_depevt * event,int status)3454*4882a593Smuzhiyun static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
3455*4882a593Smuzhiyun struct dwc3_request *req, const struct dwc3_event_depevt *event,
3456*4882a593Smuzhiyun int status)
3457*4882a593Smuzhiyun {
3458*4882a593Smuzhiyun struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3459*4882a593Smuzhiyun struct scatterlist *sg = req->sg;
3460*4882a593Smuzhiyun struct scatterlist *s;
3461*4882a593Smuzhiyun unsigned int num_queued = req->num_queued_sgs;
3462*4882a593Smuzhiyun unsigned int i;
3463*4882a593Smuzhiyun int ret = 0;
3464*4882a593Smuzhiyun
3465*4882a593Smuzhiyun for_each_sg(sg, s, num_queued, i) {
3466*4882a593Smuzhiyun trb = &dep->trb_pool[dep->trb_dequeue];
3467*4882a593Smuzhiyun
3468*4882a593Smuzhiyun req->sg = sg_next(s);
3469*4882a593Smuzhiyun req->num_queued_sgs--;
3470*4882a593Smuzhiyun
3471*4882a593Smuzhiyun ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
3472*4882a593Smuzhiyun trb, event, status, true);
3473*4882a593Smuzhiyun if (ret)
3474*4882a593Smuzhiyun break;
3475*4882a593Smuzhiyun }
3476*4882a593Smuzhiyun
3477*4882a593Smuzhiyun return ret;
3478*4882a593Smuzhiyun }
3479*4882a593Smuzhiyun
dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep * dep,struct dwc3_request * req,const struct dwc3_event_depevt * event,int status)3480*4882a593Smuzhiyun static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
3481*4882a593Smuzhiyun struct dwc3_request *req, const struct dwc3_event_depevt *event,
3482*4882a593Smuzhiyun int status)
3483*4882a593Smuzhiyun {
3484*4882a593Smuzhiyun struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3485*4882a593Smuzhiyun
3486*4882a593Smuzhiyun return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
3487*4882a593Smuzhiyun event, status, false);
3488*4882a593Smuzhiyun }
3489*4882a593Smuzhiyun
dwc3_gadget_ep_request_completed(struct dwc3_request * req)3490*4882a593Smuzhiyun static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
3491*4882a593Smuzhiyun {
3492*4882a593Smuzhiyun return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
3493*4882a593Smuzhiyun }
3494*4882a593Smuzhiyun
dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep * dep,const struct dwc3_event_depevt * event,struct dwc3_request * req,int status)3495*4882a593Smuzhiyun static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
3496*4882a593Smuzhiyun const struct dwc3_event_depevt *event,
3497*4882a593Smuzhiyun struct dwc3_request *req, int status)
3498*4882a593Smuzhiyun {
3499*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3500*4882a593Smuzhiyun int request_status;
3501*4882a593Smuzhiyun int ret;
3502*4882a593Smuzhiyun
3503*4882a593Smuzhiyun if (req->request.num_mapped_sgs)
3504*4882a593Smuzhiyun ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
3505*4882a593Smuzhiyun status);
3506*4882a593Smuzhiyun else
3507*4882a593Smuzhiyun ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
3508*4882a593Smuzhiyun status);
3509*4882a593Smuzhiyun
3510*4882a593Smuzhiyun req->request.actual = req->request.length - req->remaining;
3511*4882a593Smuzhiyun
3512*4882a593Smuzhiyun if (!dwc3_gadget_ep_request_completed(req))
3513*4882a593Smuzhiyun goto out;
3514*4882a593Smuzhiyun
3515*4882a593Smuzhiyun if (req->needs_extra_trb) {
3516*4882a593Smuzhiyun ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
3517*4882a593Smuzhiyun status);
3518*4882a593Smuzhiyun req->needs_extra_trb = false;
3519*4882a593Smuzhiyun }
3520*4882a593Smuzhiyun
3521*4882a593Smuzhiyun /*
3522*4882a593Smuzhiyun * If MISS ISOC happens, we need to move the req from started_list
3523*4882a593Smuzhiyun * to cancelled_list, then unmap the req and clear the HWO of trb.
3524*4882a593Smuzhiyun * Later in the dwc3_gadget_endpoint_trbs_complete(), it will move
3525*4882a593Smuzhiyun * the req from the cancelled_list to the pending_list, and restart
3526*4882a593Smuzhiyun * the req for isoc transfer.
3527*4882a593Smuzhiyun */
3528*4882a593Smuzhiyun if (status == -EXDEV && usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
3529*4882a593Smuzhiyun req->remaining = 0;
3530*4882a593Smuzhiyun req->needs_extra_trb = false;
3531*4882a593Smuzhiyun dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
3532*4882a593Smuzhiyun if (req->trb) {
3533*4882a593Smuzhiyun usb_gadget_unmap_request_by_dev(dwc->sysdev,
3534*4882a593Smuzhiyun &req->request,
3535*4882a593Smuzhiyun req->direction);
3536*4882a593Smuzhiyun req->trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3537*4882a593Smuzhiyun req->trb = NULL;
3538*4882a593Smuzhiyun }
3539*4882a593Smuzhiyun ret = 0;
3540*4882a593Smuzhiyun goto out;
3541*4882a593Smuzhiyun }
3542*4882a593Smuzhiyun
3543*4882a593Smuzhiyun /*
3544*4882a593Smuzhiyun * The event status only reflects the status of the TRB with IOC set.
3545*4882a593Smuzhiyun * For the requests that don't set interrupt on completion, the driver
3546*4882a593Smuzhiyun * needs to check and return the status of the completed TRBs associated
3547*4882a593Smuzhiyun * with the request. Use the status of the last TRB of the request.
3548*4882a593Smuzhiyun */
3549*4882a593Smuzhiyun if (req->request.no_interrupt) {
3550*4882a593Smuzhiyun struct dwc3_trb *trb;
3551*4882a593Smuzhiyun
3552*4882a593Smuzhiyun trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue);
3553*4882a593Smuzhiyun switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) {
3554*4882a593Smuzhiyun case DWC3_TRBSTS_MISSED_ISOC:
3555*4882a593Smuzhiyun /* Isoc endpoint only */
3556*4882a593Smuzhiyun request_status = -EXDEV;
3557*4882a593Smuzhiyun break;
3558*4882a593Smuzhiyun case DWC3_TRB_STS_XFER_IN_PROG:
3559*4882a593Smuzhiyun /* Applicable when End Transfer with ForceRM=0 */
3560*4882a593Smuzhiyun case DWC3_TRBSTS_SETUP_PENDING:
3561*4882a593Smuzhiyun /* Control endpoint only */
3562*4882a593Smuzhiyun case DWC3_TRBSTS_OK:
3563*4882a593Smuzhiyun default:
3564*4882a593Smuzhiyun request_status = 0;
3565*4882a593Smuzhiyun break;
3566*4882a593Smuzhiyun }
3567*4882a593Smuzhiyun } else {
3568*4882a593Smuzhiyun request_status = status;
3569*4882a593Smuzhiyun }
3570*4882a593Smuzhiyun
3571*4882a593Smuzhiyun dwc3_gadget_giveback(dep, req, request_status);
3572*4882a593Smuzhiyun
3573*4882a593Smuzhiyun out:
3574*4882a593Smuzhiyun return ret;
3575*4882a593Smuzhiyun }
3576*4882a593Smuzhiyun
dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep * dep,const struct dwc3_event_depevt * event,int status)3577*4882a593Smuzhiyun static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
3578*4882a593Smuzhiyun const struct dwc3_event_depevt *event, int status)
3579*4882a593Smuzhiyun {
3580*4882a593Smuzhiyun struct dwc3_request *req;
3581*4882a593Smuzhiyun
3582*4882a593Smuzhiyun while (!list_empty(&dep->started_list)) {
3583*4882a593Smuzhiyun int ret;
3584*4882a593Smuzhiyun
3585*4882a593Smuzhiyun req = next_request(&dep->started_list);
3586*4882a593Smuzhiyun ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
3587*4882a593Smuzhiyun req, status);
3588*4882a593Smuzhiyun if (ret)
3589*4882a593Smuzhiyun break;
3590*4882a593Smuzhiyun /*
3591*4882a593Smuzhiyun * The endpoint is disabled, let the dwc3_remove_requests()
3592*4882a593Smuzhiyun * handle the cleanup.
3593*4882a593Smuzhiyun */
3594*4882a593Smuzhiyun if (!dep->endpoint.desc)
3595*4882a593Smuzhiyun break;
3596*4882a593Smuzhiyun }
3597*4882a593Smuzhiyun }
3598*4882a593Smuzhiyun
dwc3_gadget_ep_should_continue(struct dwc3_ep * dep)3599*4882a593Smuzhiyun static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
3600*4882a593Smuzhiyun {
3601*4882a593Smuzhiyun struct dwc3_request *req;
3602*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3603*4882a593Smuzhiyun
3604*4882a593Smuzhiyun if (!dep->endpoint.desc || !dwc->pullups_connected ||
3605*4882a593Smuzhiyun !dwc->connected)
3606*4882a593Smuzhiyun return false;
3607*4882a593Smuzhiyun
3608*4882a593Smuzhiyun if (!list_empty(&dep->pending_list))
3609*4882a593Smuzhiyun return true;
3610*4882a593Smuzhiyun
3611*4882a593Smuzhiyun /*
3612*4882a593Smuzhiyun * We only need to check the first entry of the started list. We can
3613*4882a593Smuzhiyun * assume the completed requests are removed from the started list.
3614*4882a593Smuzhiyun */
3615*4882a593Smuzhiyun req = next_request(&dep->started_list);
3616*4882a593Smuzhiyun if (!req)
3617*4882a593Smuzhiyun return false;
3618*4882a593Smuzhiyun
3619*4882a593Smuzhiyun return !dwc3_gadget_ep_request_completed(req);
3620*4882a593Smuzhiyun }
3621*4882a593Smuzhiyun
dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3622*4882a593Smuzhiyun static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
3623*4882a593Smuzhiyun const struct dwc3_event_depevt *event)
3624*4882a593Smuzhiyun {
3625*4882a593Smuzhiyun dep->frame_number = event->parameters;
3626*4882a593Smuzhiyun }
3627*4882a593Smuzhiyun
dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep * dep,const struct dwc3_event_depevt * event,int status)3628*4882a593Smuzhiyun static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
3629*4882a593Smuzhiyun const struct dwc3_event_depevt *event, int status)
3630*4882a593Smuzhiyun {
3631*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3632*4882a593Smuzhiyun struct dwc3_request *req, *tmp;
3633*4882a593Smuzhiyun bool no_started_trb = true;
3634*4882a593Smuzhiyun
3635*4882a593Smuzhiyun dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
3636*4882a593Smuzhiyun
3637*4882a593Smuzhiyun if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3638*4882a593Smuzhiyun goto out;
3639*4882a593Smuzhiyun
3640*4882a593Smuzhiyun if (!dep->endpoint.desc)
3641*4882a593Smuzhiyun return no_started_trb;
3642*4882a593Smuzhiyun
3643*4882a593Smuzhiyun /*
3644*4882a593Smuzhiyun * If MISS ISOC happens, we need to do the following three steps
3645*4882a593Smuzhiyun * to restart the reqs in the cancelled_list and pending_list
3646*4882a593Smuzhiyun * in order.
3647*4882a593Smuzhiyun * Step1. Move all the reqs from pending_list to the tail of
3648*4882a593Smuzhiyun * cancelled_list.
3649*4882a593Smuzhiyun * Step2. Move all the reqs from cancelled_list to the tail
3650*4882a593Smuzhiyun * of pending_list.
3651*4882a593Smuzhiyun * Step3. Stop and restart an isoc transfer.
3652*4882a593Smuzhiyun */
3653*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && status == -EXDEV &&
3654*4882a593Smuzhiyun !list_empty(&dep->cancelled_list) &&
3655*4882a593Smuzhiyun !list_empty(&dep->pending_list)) {
3656*4882a593Smuzhiyun list_for_each_entry_safe(req, tmp, &dep->pending_list, list)
3657*4882a593Smuzhiyun dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
3658*4882a593Smuzhiyun }
3659*4882a593Smuzhiyun
3660*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && status == -EXDEV &&
3661*4882a593Smuzhiyun !list_empty(&dep->cancelled_list)) {
3662*4882a593Smuzhiyun list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list)
3663*4882a593Smuzhiyun dwc3_gadget_move_queued_request(req);
3664*4882a593Smuzhiyun }
3665*4882a593Smuzhiyun
3666*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3667*4882a593Smuzhiyun list_empty(&dep->started_list) &&
3668*4882a593Smuzhiyun (list_empty(&dep->pending_list) || status == -EXDEV))
3669*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, true);
3670*4882a593Smuzhiyun else if (dwc3_gadget_ep_should_continue(dep))
3671*4882a593Smuzhiyun if (__dwc3_gadget_kick_transfer(dep) == 0)
3672*4882a593Smuzhiyun no_started_trb = false;
3673*4882a593Smuzhiyun
3674*4882a593Smuzhiyun out:
3675*4882a593Smuzhiyun /*
3676*4882a593Smuzhiyun * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
3677*4882a593Smuzhiyun * See dwc3_gadget_linksts_change_interrupt() for 1st half.
3678*4882a593Smuzhiyun */
3679*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
3680*4882a593Smuzhiyun u32 reg;
3681*4882a593Smuzhiyun int i;
3682*4882a593Smuzhiyun
3683*4882a593Smuzhiyun for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
3684*4882a593Smuzhiyun dep = dwc->eps[i];
3685*4882a593Smuzhiyun
3686*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_ENABLED))
3687*4882a593Smuzhiyun continue;
3688*4882a593Smuzhiyun
3689*4882a593Smuzhiyun if (!list_empty(&dep->started_list))
3690*4882a593Smuzhiyun return no_started_trb;
3691*4882a593Smuzhiyun }
3692*4882a593Smuzhiyun
3693*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3694*4882a593Smuzhiyun reg |= dwc->u1u2;
3695*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3696*4882a593Smuzhiyun
3697*4882a593Smuzhiyun dwc->u1u2 = 0;
3698*4882a593Smuzhiyun }
3699*4882a593Smuzhiyun
3700*4882a593Smuzhiyun return no_started_trb;
3701*4882a593Smuzhiyun }
3702*4882a593Smuzhiyun
dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3703*4882a593Smuzhiyun static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
3704*4882a593Smuzhiyun const struct dwc3_event_depevt *event)
3705*4882a593Smuzhiyun {
3706*4882a593Smuzhiyun int status = 0;
3707*4882a593Smuzhiyun
3708*4882a593Smuzhiyun if (!dep->endpoint.desc)
3709*4882a593Smuzhiyun return;
3710*4882a593Smuzhiyun
3711*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
3712*4882a593Smuzhiyun dwc3_gadget_endpoint_frame_from_event(dep, event);
3713*4882a593Smuzhiyun
3714*4882a593Smuzhiyun if (event->status & DEPEVT_STATUS_BUSERR)
3715*4882a593Smuzhiyun status = -ECONNRESET;
3716*4882a593Smuzhiyun
3717*4882a593Smuzhiyun if (event->status & DEPEVT_STATUS_MISSED_ISOC)
3718*4882a593Smuzhiyun status = -EXDEV;
3719*4882a593Smuzhiyun
3720*4882a593Smuzhiyun dwc3_gadget_endpoint_trbs_complete(dep, event, status);
3721*4882a593Smuzhiyun }
3722*4882a593Smuzhiyun
dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3723*4882a593Smuzhiyun static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
3724*4882a593Smuzhiyun const struct dwc3_event_depevt *event)
3725*4882a593Smuzhiyun {
3726*4882a593Smuzhiyun int status = 0;
3727*4882a593Smuzhiyun
3728*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3729*4882a593Smuzhiyun
3730*4882a593Smuzhiyun if (event->status & DEPEVT_STATUS_BUSERR)
3731*4882a593Smuzhiyun status = -ECONNRESET;
3732*4882a593Smuzhiyun
3733*4882a593Smuzhiyun if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3734*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
3735*4882a593Smuzhiyun }
3736*4882a593Smuzhiyun
dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3737*4882a593Smuzhiyun static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
3738*4882a593Smuzhiyun const struct dwc3_event_depevt *event)
3739*4882a593Smuzhiyun {
3740*4882a593Smuzhiyun dwc3_gadget_endpoint_frame_from_event(dep, event);
3741*4882a593Smuzhiyun
3742*4882a593Smuzhiyun /*
3743*4882a593Smuzhiyun * The XferNotReady event is generated only once before the endpoint
3744*4882a593Smuzhiyun * starts. It will be generated again when END_TRANSFER command is
3745*4882a593Smuzhiyun * issued. For some controller versions, the XferNotReady event may be
3746*4882a593Smuzhiyun * generated while the END_TRANSFER command is still in process. Ignore
3747*4882a593Smuzhiyun * it and wait for the next XferNotReady event after the command is
3748*4882a593Smuzhiyun * completed.
3749*4882a593Smuzhiyun */
3750*4882a593Smuzhiyun if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3751*4882a593Smuzhiyun return;
3752*4882a593Smuzhiyun
3753*4882a593Smuzhiyun (void) __dwc3_gadget_start_isoc(dep);
3754*4882a593Smuzhiyun }
3755*4882a593Smuzhiyun
dwc3_gadget_endpoint_command_complete(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3756*4882a593Smuzhiyun static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
3757*4882a593Smuzhiyun const struct dwc3_event_depevt *event)
3758*4882a593Smuzhiyun {
3759*4882a593Smuzhiyun u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
3760*4882a593Smuzhiyun
3761*4882a593Smuzhiyun if (cmd != DWC3_DEPCMD_ENDTRANSFER)
3762*4882a593Smuzhiyun return;
3763*4882a593Smuzhiyun
3764*4882a593Smuzhiyun /*
3765*4882a593Smuzhiyun * The END_TRANSFER command will cause the controller to generate a
3766*4882a593Smuzhiyun * NoStream Event, and it's not due to the host DP NoStream rejection.
3767*4882a593Smuzhiyun * Ignore the next NoStream event.
3768*4882a593Smuzhiyun */
3769*4882a593Smuzhiyun if (dep->stream_capable)
3770*4882a593Smuzhiyun dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3771*4882a593Smuzhiyun
3772*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
3773*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
3774*4882a593Smuzhiyun dwc3_gadget_ep_cleanup_cancelled_requests(dep);
3775*4882a593Smuzhiyun
3776*4882a593Smuzhiyun if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
3777*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3778*4882a593Smuzhiyun struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
3779*4882a593Smuzhiyun
3780*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
3781*4882a593Smuzhiyun if (dwc3_send_clear_stall_ep_cmd(dep)) {
3782*4882a593Smuzhiyun struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
3783*4882a593Smuzhiyun
3784*4882a593Smuzhiyun dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
3785*4882a593Smuzhiyun if (dwc->delayed_status)
3786*4882a593Smuzhiyun __dwc3_gadget_ep0_set_halt(ep0, 1);
3787*4882a593Smuzhiyun return;
3788*4882a593Smuzhiyun }
3789*4882a593Smuzhiyun
3790*4882a593Smuzhiyun dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
3791*4882a593Smuzhiyun if (vdwc->clear_stall_protocol == dep->number)
3792*4882a593Smuzhiyun dwc3_ep0_send_delayed_status(dwc);
3793*4882a593Smuzhiyun }
3794*4882a593Smuzhiyun
3795*4882a593Smuzhiyun if ((dep->flags & DWC3_EP_DELAY_START) &&
3796*4882a593Smuzhiyun !usb_endpoint_xfer_isoc(dep->endpoint.desc))
3797*4882a593Smuzhiyun __dwc3_gadget_kick_transfer(dep);
3798*4882a593Smuzhiyun
3799*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_DELAY_START;
3800*4882a593Smuzhiyun }
3801*4882a593Smuzhiyun
dwc3_gadget_endpoint_stream_event(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3802*4882a593Smuzhiyun static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3803*4882a593Smuzhiyun const struct dwc3_event_depevt *event)
3804*4882a593Smuzhiyun {
3805*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3806*4882a593Smuzhiyun
3807*4882a593Smuzhiyun if (event->status == DEPEVT_STREAMEVT_FOUND) {
3808*4882a593Smuzhiyun dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3809*4882a593Smuzhiyun goto out;
3810*4882a593Smuzhiyun }
3811*4882a593Smuzhiyun
3812*4882a593Smuzhiyun /* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3813*4882a593Smuzhiyun switch (event->parameters) {
3814*4882a593Smuzhiyun case DEPEVT_STREAM_PRIME:
3815*4882a593Smuzhiyun /*
3816*4882a593Smuzhiyun * If the host can properly transition the endpoint state from
3817*4882a593Smuzhiyun * idle to prime after a NoStream rejection, there's no need to
3818*4882a593Smuzhiyun * force restarting the endpoint to reinitiate the stream. To
3819*4882a593Smuzhiyun * simplify the check, assume the host follows the USB spec if
3820*4882a593Smuzhiyun * it primed the endpoint more than once.
3821*4882a593Smuzhiyun */
3822*4882a593Smuzhiyun if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3823*4882a593Smuzhiyun if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3824*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3825*4882a593Smuzhiyun else
3826*4882a593Smuzhiyun dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3827*4882a593Smuzhiyun }
3828*4882a593Smuzhiyun
3829*4882a593Smuzhiyun break;
3830*4882a593Smuzhiyun case DEPEVT_STREAM_NOSTREAM:
3831*4882a593Smuzhiyun if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3832*4882a593Smuzhiyun !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3833*4882a593Smuzhiyun !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))
3834*4882a593Smuzhiyun break;
3835*4882a593Smuzhiyun
3836*4882a593Smuzhiyun /*
3837*4882a593Smuzhiyun * If the host rejects a stream due to no active stream, by the
3838*4882a593Smuzhiyun * USB and xHCI spec, the endpoint will be put back to idle
3839*4882a593Smuzhiyun * state. When the host is ready (buffer added/updated), it will
3840*4882a593Smuzhiyun * prime the endpoint to inform the usb device controller. This
3841*4882a593Smuzhiyun * triggers the device controller to issue ERDY to restart the
3842*4882a593Smuzhiyun * stream. However, some hosts don't follow this and keep the
3843*4882a593Smuzhiyun * endpoint in the idle state. No prime will come despite host
3844*4882a593Smuzhiyun * streams are updated, and the device controller will not be
3845*4882a593Smuzhiyun * triggered to generate ERDY to move the next stream data. To
3846*4882a593Smuzhiyun * workaround this and maintain compatibility with various
3847*4882a593Smuzhiyun * hosts, force to reinitate the stream until the host is ready
3848*4882a593Smuzhiyun * instead of waiting for the host to prime the endpoint.
3849*4882a593Smuzhiyun */
3850*4882a593Smuzhiyun if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3851*4882a593Smuzhiyun unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3852*4882a593Smuzhiyun
3853*4882a593Smuzhiyun dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3854*4882a593Smuzhiyun } else {
3855*4882a593Smuzhiyun dep->flags |= DWC3_EP_DELAY_START;
3856*4882a593Smuzhiyun dwc3_stop_active_transfer(dep, true, true);
3857*4882a593Smuzhiyun return;
3858*4882a593Smuzhiyun }
3859*4882a593Smuzhiyun break;
3860*4882a593Smuzhiyun }
3861*4882a593Smuzhiyun
3862*4882a593Smuzhiyun out:
3863*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3864*4882a593Smuzhiyun }
3865*4882a593Smuzhiyun
dwc3_endpoint_interrupt(struct dwc3 * dwc,const struct dwc3_event_depevt * event)3866*4882a593Smuzhiyun static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
3867*4882a593Smuzhiyun const struct dwc3_event_depevt *event)
3868*4882a593Smuzhiyun {
3869*4882a593Smuzhiyun struct dwc3_ep *dep;
3870*4882a593Smuzhiyun u8 epnum = event->endpoint_number;
3871*4882a593Smuzhiyun
3872*4882a593Smuzhiyun dep = dwc->eps[epnum];
3873*4882a593Smuzhiyun
3874*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_ENABLED)) {
3875*4882a593Smuzhiyun if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED))
3876*4882a593Smuzhiyun return;
3877*4882a593Smuzhiyun
3878*4882a593Smuzhiyun /* Handle only EPCMDCMPLT when EP disabled */
3879*4882a593Smuzhiyun if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) &&
3880*4882a593Smuzhiyun !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE))
3881*4882a593Smuzhiyun return;
3882*4882a593Smuzhiyun }
3883*4882a593Smuzhiyun
3884*4882a593Smuzhiyun if (epnum == 0 || epnum == 1) {
3885*4882a593Smuzhiyun dwc3_ep0_interrupt(dwc, event);
3886*4882a593Smuzhiyun return;
3887*4882a593Smuzhiyun }
3888*4882a593Smuzhiyun
3889*4882a593Smuzhiyun switch (event->endpoint_event) {
3890*4882a593Smuzhiyun case DWC3_DEPEVT_XFERINPROGRESS:
3891*4882a593Smuzhiyun dwc3_gadget_endpoint_transfer_in_progress(dep, event);
3892*4882a593Smuzhiyun break;
3893*4882a593Smuzhiyun case DWC3_DEPEVT_XFERNOTREADY:
3894*4882a593Smuzhiyun dwc3_gadget_endpoint_transfer_not_ready(dep, event);
3895*4882a593Smuzhiyun break;
3896*4882a593Smuzhiyun case DWC3_DEPEVT_EPCMDCMPLT:
3897*4882a593Smuzhiyun dwc3_gadget_endpoint_command_complete(dep, event);
3898*4882a593Smuzhiyun break;
3899*4882a593Smuzhiyun case DWC3_DEPEVT_XFERCOMPLETE:
3900*4882a593Smuzhiyun dwc3_gadget_endpoint_transfer_complete(dep, event);
3901*4882a593Smuzhiyun break;
3902*4882a593Smuzhiyun case DWC3_DEPEVT_STREAMEVT:
3903*4882a593Smuzhiyun dwc3_gadget_endpoint_stream_event(dep, event);
3904*4882a593Smuzhiyun break;
3905*4882a593Smuzhiyun case DWC3_DEPEVT_RXTXFIFOEVT:
3906*4882a593Smuzhiyun break;
3907*4882a593Smuzhiyun }
3908*4882a593Smuzhiyun }
3909*4882a593Smuzhiyun
dwc3_disconnect_gadget(struct dwc3 * dwc)3910*4882a593Smuzhiyun static void dwc3_disconnect_gadget(struct dwc3 *dwc)
3911*4882a593Smuzhiyun {
3912*4882a593Smuzhiyun if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
3913*4882a593Smuzhiyun spin_unlock(&dwc->lock);
3914*4882a593Smuzhiyun dwc->gadget_driver->disconnect(dwc->gadget);
3915*4882a593Smuzhiyun spin_lock(&dwc->lock);
3916*4882a593Smuzhiyun }
3917*4882a593Smuzhiyun }
3918*4882a593Smuzhiyun
dwc3_suspend_gadget(struct dwc3 * dwc)3919*4882a593Smuzhiyun static void dwc3_suspend_gadget(struct dwc3 *dwc)
3920*4882a593Smuzhiyun {
3921*4882a593Smuzhiyun if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
3922*4882a593Smuzhiyun spin_unlock(&dwc->lock);
3923*4882a593Smuzhiyun dwc->gadget_driver->suspend(dwc->gadget);
3924*4882a593Smuzhiyun spin_lock(&dwc->lock);
3925*4882a593Smuzhiyun }
3926*4882a593Smuzhiyun }
3927*4882a593Smuzhiyun
dwc3_resume_gadget(struct dwc3 * dwc)3928*4882a593Smuzhiyun static void dwc3_resume_gadget(struct dwc3 *dwc)
3929*4882a593Smuzhiyun {
3930*4882a593Smuzhiyun if (dwc->async_callbacks && dwc->gadget_driver->resume) {
3931*4882a593Smuzhiyun spin_unlock(&dwc->lock);
3932*4882a593Smuzhiyun dwc->gadget_driver->resume(dwc->gadget);
3933*4882a593Smuzhiyun spin_lock(&dwc->lock);
3934*4882a593Smuzhiyun }
3935*4882a593Smuzhiyun }
3936*4882a593Smuzhiyun
dwc3_reset_gadget(struct dwc3 * dwc)3937*4882a593Smuzhiyun static void dwc3_reset_gadget(struct dwc3 *dwc)
3938*4882a593Smuzhiyun {
3939*4882a593Smuzhiyun if (!dwc->gadget_driver)
3940*4882a593Smuzhiyun return;
3941*4882a593Smuzhiyun
3942*4882a593Smuzhiyun if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
3943*4882a593Smuzhiyun spin_unlock(&dwc->lock);
3944*4882a593Smuzhiyun usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
3945*4882a593Smuzhiyun spin_lock(&dwc->lock);
3946*4882a593Smuzhiyun }
3947*4882a593Smuzhiyun }
3948*4882a593Smuzhiyun
dwc3_stop_active_transfer(struct dwc3_ep * dep,bool force,bool interrupt)3949*4882a593Smuzhiyun void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3950*4882a593Smuzhiyun bool interrupt)
3951*4882a593Smuzhiyun {
3952*4882a593Smuzhiyun struct dwc3 *dwc = dep->dwc;
3953*4882a593Smuzhiyun
3954*4882a593Smuzhiyun /*
3955*4882a593Smuzhiyun * Only issue End Transfer command to the control endpoint of a started
3956*4882a593Smuzhiyun * Data Phase. Typically we should only do so in error cases such as
3957*4882a593Smuzhiyun * invalid/unexpected direction as described in the control transfer
3958*4882a593Smuzhiyun * flow of the programming guide.
3959*4882a593Smuzhiyun */
3960*4882a593Smuzhiyun if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE)
3961*4882a593Smuzhiyun return;
3962*4882a593Smuzhiyun
3963*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3964*4882a593Smuzhiyun (dep->flags & DWC3_EP_DELAY_STOP) ||
3965*4882a593Smuzhiyun (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
3966*4882a593Smuzhiyun return;
3967*4882a593Smuzhiyun
3968*4882a593Smuzhiyun /*
3969*4882a593Smuzhiyun * If a Setup packet is received but yet to DMA out, the controller will
3970*4882a593Smuzhiyun * not process the End Transfer command of any endpoint. Polling of its
3971*4882a593Smuzhiyun * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
3972*4882a593Smuzhiyun * timeout. Delay issuing the End Transfer command until the Setup TRB is
3973*4882a593Smuzhiyun * prepared.
3974*4882a593Smuzhiyun */
3975*4882a593Smuzhiyun if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) {
3976*4882a593Smuzhiyun dep->flags |= DWC3_EP_DELAY_STOP;
3977*4882a593Smuzhiyun return;
3978*4882a593Smuzhiyun }
3979*4882a593Smuzhiyun
3980*4882a593Smuzhiyun /*
3981*4882a593Smuzhiyun * NOTICE: We are violating what the Databook says about the
3982*4882a593Smuzhiyun * EndTransfer command. Ideally we would _always_ wait for the
3983*4882a593Smuzhiyun * EndTransfer Command Completion IRQ, but that's causing too
3984*4882a593Smuzhiyun * much trouble synchronizing between us and gadget driver.
3985*4882a593Smuzhiyun *
3986*4882a593Smuzhiyun * We have discussed this with the IP Provider and it was
3987*4882a593Smuzhiyun * suggested to giveback all requests here.
3988*4882a593Smuzhiyun *
3989*4882a593Smuzhiyun * Note also that a similar handling was tested by Synopsys
3990*4882a593Smuzhiyun * (thanks a lot Paul) and nothing bad has come out of it.
3991*4882a593Smuzhiyun * In short, what we're doing is issuing EndTransfer with
3992*4882a593Smuzhiyun * CMDIOC bit set and delay kicking transfer until the
3993*4882a593Smuzhiyun * EndTransfer command had completed.
3994*4882a593Smuzhiyun *
3995*4882a593Smuzhiyun * As of IP version 3.10a of the DWC_usb3 IP, the controller
3996*4882a593Smuzhiyun * supports a mode to work around the above limitation. The
3997*4882a593Smuzhiyun * software can poll the CMDACT bit in the DEPCMD register
3998*4882a593Smuzhiyun * after issuing a EndTransfer command. This mode is enabled
3999*4882a593Smuzhiyun * by writing GUCTL2[14]. This polling is already done in the
4000*4882a593Smuzhiyun * dwc3_send_gadget_ep_cmd() function so if the mode is
4001*4882a593Smuzhiyun * enabled, the EndTransfer command will have completed upon
4002*4882a593Smuzhiyun * returning from this function.
4003*4882a593Smuzhiyun *
4004*4882a593Smuzhiyun * This mode is NOT available on the DWC_usb31 IP.
4005*4882a593Smuzhiyun */
4006*4882a593Smuzhiyun
4007*4882a593Smuzhiyun __dwc3_stop_active_transfer(dep, force, interrupt);
4008*4882a593Smuzhiyun }
4009*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(dwc3_stop_active_transfer);
4010*4882a593Smuzhiyun
dwc3_clear_stall_all_ep(struct dwc3 * dwc)4011*4882a593Smuzhiyun static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
4012*4882a593Smuzhiyun {
4013*4882a593Smuzhiyun u32 epnum;
4014*4882a593Smuzhiyun
4015*4882a593Smuzhiyun for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
4016*4882a593Smuzhiyun struct dwc3_ep *dep;
4017*4882a593Smuzhiyun int ret;
4018*4882a593Smuzhiyun
4019*4882a593Smuzhiyun dep = dwc->eps[epnum];
4020*4882a593Smuzhiyun if (!dep)
4021*4882a593Smuzhiyun continue;
4022*4882a593Smuzhiyun
4023*4882a593Smuzhiyun if (!(dep->flags & DWC3_EP_STALL))
4024*4882a593Smuzhiyun continue;
4025*4882a593Smuzhiyun
4026*4882a593Smuzhiyun dep->flags &= ~DWC3_EP_STALL;
4027*4882a593Smuzhiyun
4028*4882a593Smuzhiyun ret = dwc3_send_clear_stall_ep_cmd(dep);
4029*4882a593Smuzhiyun WARN_ON_ONCE(ret);
4030*4882a593Smuzhiyun }
4031*4882a593Smuzhiyun }
4032*4882a593Smuzhiyun
dwc3_gadget_disconnect_interrupt(struct dwc3 * dwc)4033*4882a593Smuzhiyun static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
4034*4882a593Smuzhiyun {
4035*4882a593Smuzhiyun int reg;
4036*4882a593Smuzhiyun
4037*4882a593Smuzhiyun dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
4038*4882a593Smuzhiyun
4039*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4040*4882a593Smuzhiyun reg &= ~DWC3_DCTL_INITU1ENA;
4041*4882a593Smuzhiyun reg &= ~DWC3_DCTL_INITU2ENA;
4042*4882a593Smuzhiyun dwc3_gadget_dctl_write_safe(dwc, reg);
4043*4882a593Smuzhiyun
4044*4882a593Smuzhiyun dwc->connected = false;
4045*4882a593Smuzhiyun
4046*4882a593Smuzhiyun dwc3_disconnect_gadget(dwc);
4047*4882a593Smuzhiyun
4048*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_UNKNOWN;
4049*4882a593Smuzhiyun dwc->setup_packet_pending = false;
4050*4882a593Smuzhiyun usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
4051*4882a593Smuzhiyun
4052*4882a593Smuzhiyun if (dwc->ep0state != EP0_SETUP_PHASE) {
4053*4882a593Smuzhiyun unsigned int dir;
4054*4882a593Smuzhiyun
4055*4882a593Smuzhiyun dir = !!dwc->ep0_expect_in;
4056*4882a593Smuzhiyun if (dwc->ep0state == EP0_DATA_PHASE)
4057*4882a593Smuzhiyun dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
4058*4882a593Smuzhiyun else
4059*4882a593Smuzhiyun dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
4060*4882a593Smuzhiyun dwc3_ep0_stall_and_restart(dwc);
4061*4882a593Smuzhiyun }
4062*4882a593Smuzhiyun }
4063*4882a593Smuzhiyun
dwc3_gadget_reset_interrupt(struct dwc3 * dwc)4064*4882a593Smuzhiyun static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
4065*4882a593Smuzhiyun {
4066*4882a593Smuzhiyun u32 reg;
4067*4882a593Smuzhiyun
4068*4882a593Smuzhiyun /*
4069*4882a593Smuzhiyun * Ideally, dwc3_reset_gadget() would trigger the function
4070*4882a593Smuzhiyun * drivers to stop any active transfers through ep disable.
4071*4882a593Smuzhiyun * However, for functions which defer ep disable, such as mass
4072*4882a593Smuzhiyun * storage, we will need to rely on the call to stop active
4073*4882a593Smuzhiyun * transfers here, and avoid allowing of request queuing.
4074*4882a593Smuzhiyun */
4075*4882a593Smuzhiyun dwc->connected = false;
4076*4882a593Smuzhiyun
4077*4882a593Smuzhiyun /*
4078*4882a593Smuzhiyun * WORKAROUND: DWC3 revisions <1.88a have an issue which
4079*4882a593Smuzhiyun * would cause a missing Disconnect Event if there's a
4080*4882a593Smuzhiyun * pending Setup Packet in the FIFO.
4081*4882a593Smuzhiyun *
4082*4882a593Smuzhiyun * There's no suggested workaround on the official Bug
4083*4882a593Smuzhiyun * report, which states that "unless the driver/application
4084*4882a593Smuzhiyun * is doing any special handling of a disconnect event,
4085*4882a593Smuzhiyun * there is no functional issue".
4086*4882a593Smuzhiyun *
4087*4882a593Smuzhiyun * Unfortunately, it turns out that we _do_ some special
4088*4882a593Smuzhiyun * handling of a disconnect event, namely complete all
4089*4882a593Smuzhiyun * pending transfers, notify gadget driver of the
4090*4882a593Smuzhiyun * disconnection, and so on.
4091*4882a593Smuzhiyun *
4092*4882a593Smuzhiyun * Our suggested workaround is to follow the Disconnect
4093*4882a593Smuzhiyun * Event steps here, instead, based on a setup_packet_pending
4094*4882a593Smuzhiyun * flag. Such flag gets set whenever we have a SETUP_PENDING
4095*4882a593Smuzhiyun * status for EP0 TRBs and gets cleared on XferComplete for the
4096*4882a593Smuzhiyun * same endpoint.
4097*4882a593Smuzhiyun *
4098*4882a593Smuzhiyun * Refers to:
4099*4882a593Smuzhiyun *
4100*4882a593Smuzhiyun * STAR#9000466709: RTL: Device : Disconnect event not
4101*4882a593Smuzhiyun * generated if setup packet pending in FIFO
4102*4882a593Smuzhiyun */
4103*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
4104*4882a593Smuzhiyun if (dwc->setup_packet_pending)
4105*4882a593Smuzhiyun dwc3_gadget_disconnect_interrupt(dwc);
4106*4882a593Smuzhiyun }
4107*4882a593Smuzhiyun
4108*4882a593Smuzhiyun dwc3_reset_gadget(dwc);
4109*4882a593Smuzhiyun
4110*4882a593Smuzhiyun /*
4111*4882a593Smuzhiyun * From SNPS databook section 8.1.2, the EP0 should be in setup
4112*4882a593Smuzhiyun * phase. So ensure that EP0 is in setup phase by issuing a stall
4113*4882a593Smuzhiyun * and restart if EP0 is not in setup phase.
4114*4882a593Smuzhiyun */
4115*4882a593Smuzhiyun if (dwc->ep0state != EP0_SETUP_PHASE) {
4116*4882a593Smuzhiyun unsigned int dir;
4117*4882a593Smuzhiyun
4118*4882a593Smuzhiyun dir = !!dwc->ep0_expect_in;
4119*4882a593Smuzhiyun if (dwc->ep0state == EP0_DATA_PHASE)
4120*4882a593Smuzhiyun dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
4121*4882a593Smuzhiyun else
4122*4882a593Smuzhiyun dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
4123*4882a593Smuzhiyun
4124*4882a593Smuzhiyun dwc->eps[0]->trb_enqueue = 0;
4125*4882a593Smuzhiyun dwc->eps[1]->trb_enqueue = 0;
4126*4882a593Smuzhiyun
4127*4882a593Smuzhiyun dwc3_ep0_stall_and_restart(dwc);
4128*4882a593Smuzhiyun }
4129*4882a593Smuzhiyun
4130*4882a593Smuzhiyun /*
4131*4882a593Smuzhiyun * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
4132*4882a593Smuzhiyun * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
4133*4882a593Smuzhiyun * needs to ensure that it sends "a DEPENDXFER command for any active
4134*4882a593Smuzhiyun * transfers."
4135*4882a593Smuzhiyun */
4136*4882a593Smuzhiyun dwc3_stop_active_transfers(dwc);
4137*4882a593Smuzhiyun dwc->connected = true;
4138*4882a593Smuzhiyun
4139*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4140*4882a593Smuzhiyun reg &= ~DWC3_DCTL_TSTCTRL_MASK;
4141*4882a593Smuzhiyun dwc3_gadget_dctl_write_safe(dwc, reg);
4142*4882a593Smuzhiyun dwc->test_mode = false;
4143*4882a593Smuzhiyun dwc3_clear_stall_all_ep(dwc);
4144*4882a593Smuzhiyun
4145*4882a593Smuzhiyun /* Reset device address to zero */
4146*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4147*4882a593Smuzhiyun reg &= ~(DWC3_DCFG_DEVADDR_MASK);
4148*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4149*4882a593Smuzhiyun }
4150*4882a593Smuzhiyun
dwc3_gadget_conndone_interrupt(struct dwc3 * dwc)4151*4882a593Smuzhiyun static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
4152*4882a593Smuzhiyun {
4153*4882a593Smuzhiyun struct dwc3_ep *dep;
4154*4882a593Smuzhiyun int ret;
4155*4882a593Smuzhiyun u32 reg;
4156*4882a593Smuzhiyun u8 lanes = 1;
4157*4882a593Smuzhiyun u8 speed;
4158*4882a593Smuzhiyun struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
4159*4882a593Smuzhiyun
4160*4882a593Smuzhiyun if (!vdwc->softconnect)
4161*4882a593Smuzhiyun return;
4162*4882a593Smuzhiyun
4163*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DSTS);
4164*4882a593Smuzhiyun speed = reg & DWC3_DSTS_CONNECTSPD;
4165*4882a593Smuzhiyun dwc->speed = speed;
4166*4882a593Smuzhiyun
4167*4882a593Smuzhiyun if (DWC3_IP_IS(DWC32))
4168*4882a593Smuzhiyun lanes = DWC3_DSTS_CONNLANES(reg) + 1;
4169*4882a593Smuzhiyun
4170*4882a593Smuzhiyun dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
4171*4882a593Smuzhiyun
4172*4882a593Smuzhiyun /*
4173*4882a593Smuzhiyun * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
4174*4882a593Smuzhiyun * each time on Connect Done.
4175*4882a593Smuzhiyun *
4176*4882a593Smuzhiyun * Currently we always use the reset value. If any platform
4177*4882a593Smuzhiyun * wants to set this to a different value, we need to add a
4178*4882a593Smuzhiyun * setting and update GCTL.RAMCLKSEL here.
4179*4882a593Smuzhiyun */
4180*4882a593Smuzhiyun
4181*4882a593Smuzhiyun switch (speed) {
4182*4882a593Smuzhiyun case DWC3_DSTS_SUPERSPEED_PLUS:
4183*4882a593Smuzhiyun dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
4184*4882a593Smuzhiyun dwc->gadget->ep0->maxpacket = 512;
4185*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4186*4882a593Smuzhiyun
4187*4882a593Smuzhiyun if (lanes > 1)
4188*4882a593Smuzhiyun dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
4189*4882a593Smuzhiyun else
4190*4882a593Smuzhiyun dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
4191*4882a593Smuzhiyun break;
4192*4882a593Smuzhiyun case DWC3_DSTS_SUPERSPEED:
4193*4882a593Smuzhiyun /*
4194*4882a593Smuzhiyun * WORKAROUND: DWC3 revisions <1.90a have an issue which
4195*4882a593Smuzhiyun * would cause a missing USB3 Reset event.
4196*4882a593Smuzhiyun *
4197*4882a593Smuzhiyun * In such situations, we should force a USB3 Reset
4198*4882a593Smuzhiyun * event by calling our dwc3_gadget_reset_interrupt()
4199*4882a593Smuzhiyun * routine.
4200*4882a593Smuzhiyun *
4201*4882a593Smuzhiyun * Refers to:
4202*4882a593Smuzhiyun *
4203*4882a593Smuzhiyun * STAR#9000483510: RTL: SS : USB3 reset event may
4204*4882a593Smuzhiyun * not be generated always when the link enters poll
4205*4882a593Smuzhiyun */
4206*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 190A))
4207*4882a593Smuzhiyun dwc3_gadget_reset_interrupt(dwc);
4208*4882a593Smuzhiyun
4209*4882a593Smuzhiyun dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
4210*4882a593Smuzhiyun dwc->gadget->ep0->maxpacket = 512;
4211*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_SUPER;
4212*4882a593Smuzhiyun
4213*4882a593Smuzhiyun if (lanes > 1) {
4214*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4215*4882a593Smuzhiyun dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
4216*4882a593Smuzhiyun }
4217*4882a593Smuzhiyun break;
4218*4882a593Smuzhiyun case DWC3_DSTS_HIGHSPEED:
4219*4882a593Smuzhiyun dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
4220*4882a593Smuzhiyun dwc->gadget->ep0->maxpacket = 64;
4221*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_HIGH;
4222*4882a593Smuzhiyun break;
4223*4882a593Smuzhiyun case DWC3_DSTS_FULLSPEED:
4224*4882a593Smuzhiyun dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
4225*4882a593Smuzhiyun dwc->gadget->ep0->maxpacket = 64;
4226*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_FULL;
4227*4882a593Smuzhiyun break;
4228*4882a593Smuzhiyun case DWC3_DSTS_LOWSPEED:
4229*4882a593Smuzhiyun dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
4230*4882a593Smuzhiyun dwc->gadget->ep0->maxpacket = 8;
4231*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_LOW;
4232*4882a593Smuzhiyun break;
4233*4882a593Smuzhiyun }
4234*4882a593Smuzhiyun
4235*4882a593Smuzhiyun dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
4236*4882a593Smuzhiyun
4237*4882a593Smuzhiyun /* Enable USB2 LPM Capability */
4238*4882a593Smuzhiyun
4239*4882a593Smuzhiyun if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
4240*4882a593Smuzhiyun !dwc->usb2_gadget_lpm_disable &&
4241*4882a593Smuzhiyun (speed != DWC3_DSTS_SUPERSPEED) &&
4242*4882a593Smuzhiyun (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
4243*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4244*4882a593Smuzhiyun reg |= DWC3_DCFG_LPM_CAP;
4245*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4246*4882a593Smuzhiyun
4247*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4248*4882a593Smuzhiyun reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
4249*4882a593Smuzhiyun
4250*4882a593Smuzhiyun reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
4251*4882a593Smuzhiyun (dwc->is_utmi_l1_suspend << 4));
4252*4882a593Smuzhiyun
4253*4882a593Smuzhiyun /*
4254*4882a593Smuzhiyun * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
4255*4882a593Smuzhiyun * DCFG.LPMCap is set, core responses with an ACK and the
4256*4882a593Smuzhiyun * BESL value in the LPM token is less than or equal to LPM
4257*4882a593Smuzhiyun * NYET threshold.
4258*4882a593Smuzhiyun */
4259*4882a593Smuzhiyun WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
4260*4882a593Smuzhiyun "LPM Erratum not available on dwc3 revisions < 2.40a\n");
4261*4882a593Smuzhiyun
4262*4882a593Smuzhiyun if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A))
4263*4882a593Smuzhiyun reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
4264*4882a593Smuzhiyun
4265*4882a593Smuzhiyun dwc3_gadget_dctl_write_safe(dwc, reg);
4266*4882a593Smuzhiyun } else {
4267*4882a593Smuzhiyun if (dwc->usb2_gadget_lpm_disable) {
4268*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4269*4882a593Smuzhiyun reg &= ~DWC3_DCFG_LPM_CAP;
4270*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4271*4882a593Smuzhiyun }
4272*4882a593Smuzhiyun
4273*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4274*4882a593Smuzhiyun reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
4275*4882a593Smuzhiyun dwc3_gadget_dctl_write_safe(dwc, reg);
4276*4882a593Smuzhiyun }
4277*4882a593Smuzhiyun
4278*4882a593Smuzhiyun dep = dwc->eps[0];
4279*4882a593Smuzhiyun ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
4280*4882a593Smuzhiyun if (ret) {
4281*4882a593Smuzhiyun dev_err(dwc->dev, "failed to enable %s\n", dep->name);
4282*4882a593Smuzhiyun return;
4283*4882a593Smuzhiyun }
4284*4882a593Smuzhiyun
4285*4882a593Smuzhiyun dep = dwc->eps[1];
4286*4882a593Smuzhiyun ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
4287*4882a593Smuzhiyun if (ret) {
4288*4882a593Smuzhiyun dev_err(dwc->dev, "failed to enable %s\n", dep->name);
4289*4882a593Smuzhiyun return;
4290*4882a593Smuzhiyun }
4291*4882a593Smuzhiyun
4292*4882a593Smuzhiyun /*
4293*4882a593Smuzhiyun * Configure PHY via GUSB3PIPECTLn if required.
4294*4882a593Smuzhiyun *
4295*4882a593Smuzhiyun * Update GTXFIFOSIZn
4296*4882a593Smuzhiyun *
4297*4882a593Smuzhiyun * In both cases reset values should be sufficient.
4298*4882a593Smuzhiyun */
4299*4882a593Smuzhiyun }
4300*4882a593Smuzhiyun
dwc3_gadget_wakeup_interrupt(struct dwc3 * dwc)4301*4882a593Smuzhiyun static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
4302*4882a593Smuzhiyun {
4303*4882a593Smuzhiyun /*
4304*4882a593Smuzhiyun * TODO take core out of low power mode when that's
4305*4882a593Smuzhiyun * implemented.
4306*4882a593Smuzhiyun */
4307*4882a593Smuzhiyun
4308*4882a593Smuzhiyun if (dwc->async_callbacks && dwc->gadget_driver->resume) {
4309*4882a593Smuzhiyun spin_unlock(&dwc->lock);
4310*4882a593Smuzhiyun dwc->gadget_driver->resume(dwc->gadget);
4311*4882a593Smuzhiyun spin_lock(&dwc->lock);
4312*4882a593Smuzhiyun }
4313*4882a593Smuzhiyun }
4314*4882a593Smuzhiyun
dwc3_gadget_linksts_change_interrupt(struct dwc3 * dwc,unsigned int evtinfo)4315*4882a593Smuzhiyun static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
4316*4882a593Smuzhiyun unsigned int evtinfo)
4317*4882a593Smuzhiyun {
4318*4882a593Smuzhiyun enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
4319*4882a593Smuzhiyun unsigned int pwropt;
4320*4882a593Smuzhiyun
4321*4882a593Smuzhiyun /*
4322*4882a593Smuzhiyun * WORKAROUND: DWC3 < 2.50a have an issue when configured without
4323*4882a593Smuzhiyun * Hibernation mode enabled which would show up when device detects
4324*4882a593Smuzhiyun * host-initiated U3 exit.
4325*4882a593Smuzhiyun *
4326*4882a593Smuzhiyun * In that case, device will generate a Link State Change Interrupt
4327*4882a593Smuzhiyun * from U3 to RESUME which is only necessary if Hibernation is
4328*4882a593Smuzhiyun * configured in.
4329*4882a593Smuzhiyun *
4330*4882a593Smuzhiyun * There are no functional changes due to such spurious event and we
4331*4882a593Smuzhiyun * just need to ignore it.
4332*4882a593Smuzhiyun *
4333*4882a593Smuzhiyun * Refers to:
4334*4882a593Smuzhiyun *
4335*4882a593Smuzhiyun * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
4336*4882a593Smuzhiyun * operational mode
4337*4882a593Smuzhiyun */
4338*4882a593Smuzhiyun pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
4339*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
4340*4882a593Smuzhiyun (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
4341*4882a593Smuzhiyun if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
4342*4882a593Smuzhiyun (next == DWC3_LINK_STATE_RESUME)) {
4343*4882a593Smuzhiyun return;
4344*4882a593Smuzhiyun }
4345*4882a593Smuzhiyun }
4346*4882a593Smuzhiyun
4347*4882a593Smuzhiyun /*
4348*4882a593Smuzhiyun * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
4349*4882a593Smuzhiyun * on the link partner, the USB session might do multiple entry/exit
4350*4882a593Smuzhiyun * of low power states before a transfer takes place.
4351*4882a593Smuzhiyun *
4352*4882a593Smuzhiyun * Due to this problem, we might experience lower throughput. The
4353*4882a593Smuzhiyun * suggested workaround is to disable DCTL[12:9] bits if we're
4354*4882a593Smuzhiyun * transitioning from U1/U2 to U0 and enable those bits again
4355*4882a593Smuzhiyun * after a transfer completes and there are no pending transfers
4356*4882a593Smuzhiyun * on any of the enabled endpoints.
4357*4882a593Smuzhiyun *
4358*4882a593Smuzhiyun * This is the first half of that workaround.
4359*4882a593Smuzhiyun *
4360*4882a593Smuzhiyun * Refers to:
4361*4882a593Smuzhiyun *
4362*4882a593Smuzhiyun * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
4363*4882a593Smuzhiyun * core send LGO_Ux entering U0
4364*4882a593Smuzhiyun */
4365*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
4366*4882a593Smuzhiyun if (next == DWC3_LINK_STATE_U0) {
4367*4882a593Smuzhiyun u32 u1u2;
4368*4882a593Smuzhiyun u32 reg;
4369*4882a593Smuzhiyun
4370*4882a593Smuzhiyun switch (dwc->link_state) {
4371*4882a593Smuzhiyun case DWC3_LINK_STATE_U1:
4372*4882a593Smuzhiyun case DWC3_LINK_STATE_U2:
4373*4882a593Smuzhiyun reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4374*4882a593Smuzhiyun u1u2 = reg & (DWC3_DCTL_INITU2ENA
4375*4882a593Smuzhiyun | DWC3_DCTL_ACCEPTU2ENA
4376*4882a593Smuzhiyun | DWC3_DCTL_INITU1ENA
4377*4882a593Smuzhiyun | DWC3_DCTL_ACCEPTU1ENA);
4378*4882a593Smuzhiyun
4379*4882a593Smuzhiyun if (!dwc->u1u2)
4380*4882a593Smuzhiyun dwc->u1u2 = reg & u1u2;
4381*4882a593Smuzhiyun
4382*4882a593Smuzhiyun reg &= ~u1u2;
4383*4882a593Smuzhiyun
4384*4882a593Smuzhiyun dwc3_gadget_dctl_write_safe(dwc, reg);
4385*4882a593Smuzhiyun break;
4386*4882a593Smuzhiyun default:
4387*4882a593Smuzhiyun /* do nothing */
4388*4882a593Smuzhiyun break;
4389*4882a593Smuzhiyun }
4390*4882a593Smuzhiyun }
4391*4882a593Smuzhiyun }
4392*4882a593Smuzhiyun
4393*4882a593Smuzhiyun switch (next) {
4394*4882a593Smuzhiyun case DWC3_LINK_STATE_U1:
4395*4882a593Smuzhiyun if (dwc->speed == USB_SPEED_SUPER)
4396*4882a593Smuzhiyun dwc3_suspend_gadget(dwc);
4397*4882a593Smuzhiyun break;
4398*4882a593Smuzhiyun case DWC3_LINK_STATE_U2:
4399*4882a593Smuzhiyun case DWC3_LINK_STATE_U3:
4400*4882a593Smuzhiyun dwc3_suspend_gadget(dwc);
4401*4882a593Smuzhiyun break;
4402*4882a593Smuzhiyun case DWC3_LINK_STATE_RESUME:
4403*4882a593Smuzhiyun dwc3_resume_gadget(dwc);
4404*4882a593Smuzhiyun break;
4405*4882a593Smuzhiyun default:
4406*4882a593Smuzhiyun /* do nothing */
4407*4882a593Smuzhiyun break;
4408*4882a593Smuzhiyun }
4409*4882a593Smuzhiyun
4410*4882a593Smuzhiyun dwc->link_state = next;
4411*4882a593Smuzhiyun }
4412*4882a593Smuzhiyun
dwc3_gadget_suspend_interrupt(struct dwc3 * dwc,unsigned int evtinfo)4413*4882a593Smuzhiyun static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
4414*4882a593Smuzhiyun unsigned int evtinfo)
4415*4882a593Smuzhiyun {
4416*4882a593Smuzhiyun enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
4417*4882a593Smuzhiyun
4418*4882a593Smuzhiyun if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
4419*4882a593Smuzhiyun dwc3_suspend_gadget(dwc);
4420*4882a593Smuzhiyun
4421*4882a593Smuzhiyun dwc->link_state = next;
4422*4882a593Smuzhiyun }
4423*4882a593Smuzhiyun
dwc3_gadget_hibernation_interrupt(struct dwc3 * dwc,unsigned int evtinfo)4424*4882a593Smuzhiyun static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
4425*4882a593Smuzhiyun unsigned int evtinfo)
4426*4882a593Smuzhiyun {
4427*4882a593Smuzhiyun unsigned int is_ss = evtinfo & BIT(4);
4428*4882a593Smuzhiyun
4429*4882a593Smuzhiyun /*
4430*4882a593Smuzhiyun * WORKAROUND: DWC3 revison 2.20a with hibernation support
4431*4882a593Smuzhiyun * have a known issue which can cause USB CV TD.9.23 to fail
4432*4882a593Smuzhiyun * randomly.
4433*4882a593Smuzhiyun *
4434*4882a593Smuzhiyun * Because of this issue, core could generate bogus hibernation
4435*4882a593Smuzhiyun * events which SW needs to ignore.
4436*4882a593Smuzhiyun *
4437*4882a593Smuzhiyun * Refers to:
4438*4882a593Smuzhiyun *
4439*4882a593Smuzhiyun * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
4440*4882a593Smuzhiyun * Device Fallback from SuperSpeed
4441*4882a593Smuzhiyun */
4442*4882a593Smuzhiyun if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
4443*4882a593Smuzhiyun return;
4444*4882a593Smuzhiyun
4445*4882a593Smuzhiyun /* enter hibernation here */
4446*4882a593Smuzhiyun }
4447*4882a593Smuzhiyun
dwc3_gadget_interrupt(struct dwc3 * dwc,const struct dwc3_event_devt * event)4448*4882a593Smuzhiyun static void dwc3_gadget_interrupt(struct dwc3 *dwc,
4449*4882a593Smuzhiyun const struct dwc3_event_devt *event)
4450*4882a593Smuzhiyun {
4451*4882a593Smuzhiyun switch (event->type) {
4452*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_DISCONNECT:
4453*4882a593Smuzhiyun dev_info(dwc->dev, "device disconnect\n");
4454*4882a593Smuzhiyun dwc3_gadget_disconnect_interrupt(dwc);
4455*4882a593Smuzhiyun break;
4456*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_RESET:
4457*4882a593Smuzhiyun dev_info(dwc->dev, "device reset\n");
4458*4882a593Smuzhiyun dwc3_gadget_reset_interrupt(dwc);
4459*4882a593Smuzhiyun break;
4460*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_CONNECT_DONE:
4461*4882a593Smuzhiyun dwc3_gadget_conndone_interrupt(dwc);
4462*4882a593Smuzhiyun break;
4463*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_WAKEUP:
4464*4882a593Smuzhiyun dwc3_gadget_wakeup_interrupt(dwc);
4465*4882a593Smuzhiyun break;
4466*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_HIBER_REQ:
4467*4882a593Smuzhiyun if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
4468*4882a593Smuzhiyun "unexpected hibernation event\n"))
4469*4882a593Smuzhiyun break;
4470*4882a593Smuzhiyun
4471*4882a593Smuzhiyun dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
4472*4882a593Smuzhiyun break;
4473*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
4474*4882a593Smuzhiyun dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
4475*4882a593Smuzhiyun break;
4476*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_SUSPEND:
4477*4882a593Smuzhiyun /* It changed to be suspend event for version 2.30a and above */
4478*4882a593Smuzhiyun if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) {
4479*4882a593Smuzhiyun /*
4480*4882a593Smuzhiyun * Ignore suspend event until the gadget enters into
4481*4882a593Smuzhiyun * USB_STATE_CONFIGURED state.
4482*4882a593Smuzhiyun */
4483*4882a593Smuzhiyun if (dwc->gadget->state >= USB_STATE_CONFIGURED)
4484*4882a593Smuzhiyun dwc3_gadget_suspend_interrupt(dwc,
4485*4882a593Smuzhiyun event->event_info);
4486*4882a593Smuzhiyun }
4487*4882a593Smuzhiyun break;
4488*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_SOF:
4489*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
4490*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_CMD_CMPL:
4491*4882a593Smuzhiyun case DWC3_DEVICE_EVENT_OVERFLOW:
4492*4882a593Smuzhiyun break;
4493*4882a593Smuzhiyun default:
4494*4882a593Smuzhiyun dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
4495*4882a593Smuzhiyun }
4496*4882a593Smuzhiyun }
4497*4882a593Smuzhiyun
dwc3_process_event_entry(struct dwc3 * dwc,const union dwc3_event * event)4498*4882a593Smuzhiyun static void dwc3_process_event_entry(struct dwc3 *dwc,
4499*4882a593Smuzhiyun const union dwc3_event *event)
4500*4882a593Smuzhiyun {
4501*4882a593Smuzhiyun trace_dwc3_event(event->raw, dwc);
4502*4882a593Smuzhiyun
4503*4882a593Smuzhiyun if (!event->type.is_devspec)
4504*4882a593Smuzhiyun dwc3_endpoint_interrupt(dwc, &event->depevt);
4505*4882a593Smuzhiyun else if (event->type.type == DWC3_EVENT_TYPE_DEV)
4506*4882a593Smuzhiyun dwc3_gadget_interrupt(dwc, &event->devt);
4507*4882a593Smuzhiyun else
4508*4882a593Smuzhiyun dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
4509*4882a593Smuzhiyun }
4510*4882a593Smuzhiyun
dwc3_process_event_buf(struct dwc3_event_buffer * evt)4511*4882a593Smuzhiyun static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
4512*4882a593Smuzhiyun {
4513*4882a593Smuzhiyun struct dwc3 *dwc = evt->dwc;
4514*4882a593Smuzhiyun irqreturn_t ret = IRQ_NONE;
4515*4882a593Smuzhiyun int left;
4516*4882a593Smuzhiyun
4517*4882a593Smuzhiyun left = evt->count;
4518*4882a593Smuzhiyun
4519*4882a593Smuzhiyun if (!(evt->flags & DWC3_EVENT_PENDING))
4520*4882a593Smuzhiyun return IRQ_NONE;
4521*4882a593Smuzhiyun
4522*4882a593Smuzhiyun while (left > 0) {
4523*4882a593Smuzhiyun union dwc3_event event;
4524*4882a593Smuzhiyun
4525*4882a593Smuzhiyun event.raw = *(u32 *) (evt->cache + evt->lpos);
4526*4882a593Smuzhiyun
4527*4882a593Smuzhiyun dwc3_process_event_entry(dwc, &event);
4528*4882a593Smuzhiyun
4529*4882a593Smuzhiyun /*
4530*4882a593Smuzhiyun * FIXME we wrap around correctly to the next entry as
4531*4882a593Smuzhiyun * almost all entries are 4 bytes in size. There is one
4532*4882a593Smuzhiyun * entry which has 12 bytes which is a regular entry
4533*4882a593Smuzhiyun * followed by 8 bytes data. ATM I don't know how
4534*4882a593Smuzhiyun * things are organized if we get next to the a
4535*4882a593Smuzhiyun * boundary so I worry about that once we try to handle
4536*4882a593Smuzhiyun * that.
4537*4882a593Smuzhiyun */
4538*4882a593Smuzhiyun evt->lpos = (evt->lpos + 4) % evt->length;
4539*4882a593Smuzhiyun left -= 4;
4540*4882a593Smuzhiyun }
4541*4882a593Smuzhiyun
4542*4882a593Smuzhiyun evt->count = 0;
4543*4882a593Smuzhiyun ret = IRQ_HANDLED;
4544*4882a593Smuzhiyun
4545*4882a593Smuzhiyun /* Unmask interrupt */
4546*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4547*4882a593Smuzhiyun DWC3_GEVNTSIZ_SIZE(evt->length));
4548*4882a593Smuzhiyun
4549*4882a593Smuzhiyun if (dwc->imod_interval) {
4550*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
4551*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
4552*4882a593Smuzhiyun }
4553*4882a593Smuzhiyun
4554*4882a593Smuzhiyun /* Keep the clearing of DWC3_EVENT_PENDING at the end */
4555*4882a593Smuzhiyun evt->flags &= ~DWC3_EVENT_PENDING;
4556*4882a593Smuzhiyun
4557*4882a593Smuzhiyun return ret;
4558*4882a593Smuzhiyun }
4559*4882a593Smuzhiyun
dwc3_thread_interrupt(int irq,void * _evt)4560*4882a593Smuzhiyun static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
4561*4882a593Smuzhiyun {
4562*4882a593Smuzhiyun struct dwc3_event_buffer *evt = _evt;
4563*4882a593Smuzhiyun struct dwc3 *dwc = evt->dwc;
4564*4882a593Smuzhiyun unsigned long flags;
4565*4882a593Smuzhiyun irqreturn_t ret = IRQ_NONE;
4566*4882a593Smuzhiyun
4567*4882a593Smuzhiyun local_bh_disable();
4568*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
4569*4882a593Smuzhiyun ret = dwc3_process_event_buf(evt);
4570*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
4571*4882a593Smuzhiyun local_bh_enable();
4572*4882a593Smuzhiyun
4573*4882a593Smuzhiyun return ret;
4574*4882a593Smuzhiyun }
4575*4882a593Smuzhiyun
dwc3_check_event_buf(struct dwc3_event_buffer * evt)4576*4882a593Smuzhiyun static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
4577*4882a593Smuzhiyun {
4578*4882a593Smuzhiyun struct dwc3 *dwc = evt->dwc;
4579*4882a593Smuzhiyun u32 amount;
4580*4882a593Smuzhiyun u32 count;
4581*4882a593Smuzhiyun
4582*4882a593Smuzhiyun if (pm_runtime_suspended(dwc->dev)) {
4583*4882a593Smuzhiyun pm_runtime_get(dwc->dev);
4584*4882a593Smuzhiyun disable_irq_nosync(dwc->irq_gadget);
4585*4882a593Smuzhiyun dwc->pending_events = true;
4586*4882a593Smuzhiyun return IRQ_HANDLED;
4587*4882a593Smuzhiyun }
4588*4882a593Smuzhiyun
4589*4882a593Smuzhiyun /*
4590*4882a593Smuzhiyun * With PCIe legacy interrupt, test shows that top-half irq handler can
4591*4882a593Smuzhiyun * be called again after HW interrupt deassertion. Check if bottom-half
4592*4882a593Smuzhiyun * irq event handler completes before caching new event to prevent
4593*4882a593Smuzhiyun * losing events.
4594*4882a593Smuzhiyun */
4595*4882a593Smuzhiyun if (evt->flags & DWC3_EVENT_PENDING)
4596*4882a593Smuzhiyun return IRQ_HANDLED;
4597*4882a593Smuzhiyun
4598*4882a593Smuzhiyun count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
4599*4882a593Smuzhiyun count &= DWC3_GEVNTCOUNT_MASK;
4600*4882a593Smuzhiyun if (!count)
4601*4882a593Smuzhiyun return IRQ_NONE;
4602*4882a593Smuzhiyun
4603*4882a593Smuzhiyun evt->count = count;
4604*4882a593Smuzhiyun evt->flags |= DWC3_EVENT_PENDING;
4605*4882a593Smuzhiyun
4606*4882a593Smuzhiyun /* Mask interrupt */
4607*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
4608*4882a593Smuzhiyun DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length));
4609*4882a593Smuzhiyun
4610*4882a593Smuzhiyun amount = min(count, evt->length - evt->lpos);
4611*4882a593Smuzhiyun memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
4612*4882a593Smuzhiyun
4613*4882a593Smuzhiyun if (amount < count)
4614*4882a593Smuzhiyun memcpy(evt->cache, evt->buf, count - amount);
4615*4882a593Smuzhiyun
4616*4882a593Smuzhiyun dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
4617*4882a593Smuzhiyun
4618*4882a593Smuzhiyun return IRQ_WAKE_THREAD;
4619*4882a593Smuzhiyun }
4620*4882a593Smuzhiyun
dwc3_interrupt(int irq,void * _evt)4621*4882a593Smuzhiyun static irqreturn_t dwc3_interrupt(int irq, void *_evt)
4622*4882a593Smuzhiyun {
4623*4882a593Smuzhiyun struct dwc3_event_buffer *evt = _evt;
4624*4882a593Smuzhiyun
4625*4882a593Smuzhiyun return dwc3_check_event_buf(evt);
4626*4882a593Smuzhiyun }
4627*4882a593Smuzhiyun
dwc3_gadget_get_irq(struct dwc3 * dwc)4628*4882a593Smuzhiyun static int dwc3_gadget_get_irq(struct dwc3 *dwc)
4629*4882a593Smuzhiyun {
4630*4882a593Smuzhiyun struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
4631*4882a593Smuzhiyun int irq;
4632*4882a593Smuzhiyun
4633*4882a593Smuzhiyun irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
4634*4882a593Smuzhiyun if (irq > 0)
4635*4882a593Smuzhiyun goto out;
4636*4882a593Smuzhiyun
4637*4882a593Smuzhiyun if (irq == -EPROBE_DEFER)
4638*4882a593Smuzhiyun goto out;
4639*4882a593Smuzhiyun
4640*4882a593Smuzhiyun irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
4641*4882a593Smuzhiyun if (irq > 0)
4642*4882a593Smuzhiyun goto out;
4643*4882a593Smuzhiyun
4644*4882a593Smuzhiyun if (irq == -EPROBE_DEFER)
4645*4882a593Smuzhiyun goto out;
4646*4882a593Smuzhiyun
4647*4882a593Smuzhiyun irq = platform_get_irq(dwc3_pdev, 0);
4648*4882a593Smuzhiyun if (irq > 0)
4649*4882a593Smuzhiyun goto out;
4650*4882a593Smuzhiyun
4651*4882a593Smuzhiyun if (!irq)
4652*4882a593Smuzhiyun irq = -EINVAL;
4653*4882a593Smuzhiyun
4654*4882a593Smuzhiyun out:
4655*4882a593Smuzhiyun return irq;
4656*4882a593Smuzhiyun }
4657*4882a593Smuzhiyun
dwc_gadget_release(struct device * dev)4658*4882a593Smuzhiyun static void dwc_gadget_release(struct device *dev)
4659*4882a593Smuzhiyun {
4660*4882a593Smuzhiyun struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
4661*4882a593Smuzhiyun
4662*4882a593Smuzhiyun kfree(gadget);
4663*4882a593Smuzhiyun }
4664*4882a593Smuzhiyun
4665*4882a593Smuzhiyun /**
4666*4882a593Smuzhiyun * dwc3_gadget_init - initializes gadget related registers
4667*4882a593Smuzhiyun * @dwc: pointer to our controller context structure
4668*4882a593Smuzhiyun *
4669*4882a593Smuzhiyun * Returns 0 on success otherwise negative errno.
4670*4882a593Smuzhiyun */
dwc3_gadget_init(struct dwc3 * dwc)4671*4882a593Smuzhiyun int dwc3_gadget_init(struct dwc3 *dwc)
4672*4882a593Smuzhiyun {
4673*4882a593Smuzhiyun int ret;
4674*4882a593Smuzhiyun int irq;
4675*4882a593Smuzhiyun struct device *dev;
4676*4882a593Smuzhiyun
4677*4882a593Smuzhiyun irq = dwc3_gadget_get_irq(dwc);
4678*4882a593Smuzhiyun if (irq < 0) {
4679*4882a593Smuzhiyun ret = irq;
4680*4882a593Smuzhiyun goto err0;
4681*4882a593Smuzhiyun }
4682*4882a593Smuzhiyun
4683*4882a593Smuzhiyun dwc->irq_gadget = irq;
4684*4882a593Smuzhiyun
4685*4882a593Smuzhiyun dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
4686*4882a593Smuzhiyun sizeof(*dwc->ep0_trb) * 2,
4687*4882a593Smuzhiyun &dwc->ep0_trb_addr, GFP_KERNEL);
4688*4882a593Smuzhiyun if (!dwc->ep0_trb) {
4689*4882a593Smuzhiyun dev_err(dwc->dev, "failed to allocate ep0 trb\n");
4690*4882a593Smuzhiyun ret = -ENOMEM;
4691*4882a593Smuzhiyun goto err0;
4692*4882a593Smuzhiyun }
4693*4882a593Smuzhiyun
4694*4882a593Smuzhiyun dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
4695*4882a593Smuzhiyun if (!dwc->setup_buf) {
4696*4882a593Smuzhiyun ret = -ENOMEM;
4697*4882a593Smuzhiyun goto err1;
4698*4882a593Smuzhiyun }
4699*4882a593Smuzhiyun
4700*4882a593Smuzhiyun dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
4701*4882a593Smuzhiyun &dwc->bounce_addr, GFP_KERNEL);
4702*4882a593Smuzhiyun if (!dwc->bounce) {
4703*4882a593Smuzhiyun ret = -ENOMEM;
4704*4882a593Smuzhiyun goto err2;
4705*4882a593Smuzhiyun }
4706*4882a593Smuzhiyun
4707*4882a593Smuzhiyun init_completion(&dwc->ep0_in_setup);
4708*4882a593Smuzhiyun dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
4709*4882a593Smuzhiyun if (!dwc->gadget) {
4710*4882a593Smuzhiyun ret = -ENOMEM;
4711*4882a593Smuzhiyun goto err3;
4712*4882a593Smuzhiyun }
4713*4882a593Smuzhiyun
4714*4882a593Smuzhiyun
4715*4882a593Smuzhiyun usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
4716*4882a593Smuzhiyun dev = &dwc->gadget->dev;
4717*4882a593Smuzhiyun dev->platform_data = dwc;
4718*4882a593Smuzhiyun dwc->gadget->ops = &dwc3_gadget_ops;
4719*4882a593Smuzhiyun dwc->gadget->speed = USB_SPEED_UNKNOWN;
4720*4882a593Smuzhiyun dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
4721*4882a593Smuzhiyun dwc->gadget->sg_supported = true;
4722*4882a593Smuzhiyun dwc->gadget->name = "dwc3-gadget";
4723*4882a593Smuzhiyun dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable;
4724*4882a593Smuzhiyun
4725*4882a593Smuzhiyun /*
4726*4882a593Smuzhiyun * FIXME We might be setting max_speed to <SUPER, however versions
4727*4882a593Smuzhiyun * <2.20a of dwc3 have an issue with metastability (documented
4728*4882a593Smuzhiyun * elsewhere in this driver) which tells us we can't set max speed to
4729*4882a593Smuzhiyun * anything lower than SUPER.
4730*4882a593Smuzhiyun *
4731*4882a593Smuzhiyun * Because gadget.max_speed is only used by composite.c and function
4732*4882a593Smuzhiyun * drivers (i.e. it won't go into dwc3's registers) we are allowing this
4733*4882a593Smuzhiyun * to happen so we avoid sending SuperSpeed Capability descriptor
4734*4882a593Smuzhiyun * together with our BOS descriptor as that could confuse host into
4735*4882a593Smuzhiyun * thinking we can handle super speed.
4736*4882a593Smuzhiyun *
4737*4882a593Smuzhiyun * Note that, in fact, we won't even support GetBOS requests when speed
4738*4882a593Smuzhiyun * is less than super speed because we don't have means, yet, to tell
4739*4882a593Smuzhiyun * composite.c that we are USB 2.0 + LPM ECN.
4740*4882a593Smuzhiyun */
4741*4882a593Smuzhiyun if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
4742*4882a593Smuzhiyun !dwc->dis_metastability_quirk)
4743*4882a593Smuzhiyun dev_info(dwc->dev, "changing max_speed on rev %08x\n",
4744*4882a593Smuzhiyun dwc->revision);
4745*4882a593Smuzhiyun
4746*4882a593Smuzhiyun dwc->gadget->max_speed = dwc->maximum_speed;
4747*4882a593Smuzhiyun dwc->gadget->max_ssp_rate = dwc->max_ssp_rate;
4748*4882a593Smuzhiyun
4749*4882a593Smuzhiyun /*
4750*4882a593Smuzhiyun * REVISIT: Here we should clear all pending IRQs to be
4751*4882a593Smuzhiyun * sure we're starting from a well known location.
4752*4882a593Smuzhiyun */
4753*4882a593Smuzhiyun
4754*4882a593Smuzhiyun ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
4755*4882a593Smuzhiyun if (ret)
4756*4882a593Smuzhiyun goto err4;
4757*4882a593Smuzhiyun
4758*4882a593Smuzhiyun ret = usb_add_gadget(dwc->gadget);
4759*4882a593Smuzhiyun if (ret) {
4760*4882a593Smuzhiyun dev_err(dwc->dev, "failed to add gadget\n");
4761*4882a593Smuzhiyun goto err5;
4762*4882a593Smuzhiyun }
4763*4882a593Smuzhiyun
4764*4882a593Smuzhiyun if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4765*4882a593Smuzhiyun dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4766*4882a593Smuzhiyun else
4767*4882a593Smuzhiyun dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
4768*4882a593Smuzhiyun
4769*4882a593Smuzhiyun return 0;
4770*4882a593Smuzhiyun
4771*4882a593Smuzhiyun err5:
4772*4882a593Smuzhiyun dwc3_gadget_free_endpoints(dwc);
4773*4882a593Smuzhiyun err4:
4774*4882a593Smuzhiyun usb_put_gadget(dwc->gadget);
4775*4882a593Smuzhiyun dwc->gadget = NULL;
4776*4882a593Smuzhiyun err3:
4777*4882a593Smuzhiyun dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4778*4882a593Smuzhiyun dwc->bounce_addr);
4779*4882a593Smuzhiyun
4780*4882a593Smuzhiyun err2:
4781*4882a593Smuzhiyun kfree(dwc->setup_buf);
4782*4882a593Smuzhiyun
4783*4882a593Smuzhiyun err1:
4784*4882a593Smuzhiyun dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4785*4882a593Smuzhiyun dwc->ep0_trb, dwc->ep0_trb_addr);
4786*4882a593Smuzhiyun
4787*4882a593Smuzhiyun err0:
4788*4882a593Smuzhiyun return ret;
4789*4882a593Smuzhiyun }
4790*4882a593Smuzhiyun
4791*4882a593Smuzhiyun /* -------------------------------------------------------------------------- */
4792*4882a593Smuzhiyun
dwc3_gadget_exit(struct dwc3 * dwc)4793*4882a593Smuzhiyun void dwc3_gadget_exit(struct dwc3 *dwc)
4794*4882a593Smuzhiyun {
4795*4882a593Smuzhiyun if (!dwc->gadget)
4796*4882a593Smuzhiyun return;
4797*4882a593Smuzhiyun
4798*4882a593Smuzhiyun usb_del_gadget(dwc->gadget);
4799*4882a593Smuzhiyun dwc3_gadget_free_endpoints(dwc);
4800*4882a593Smuzhiyun usb_put_gadget(dwc->gadget);
4801*4882a593Smuzhiyun dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4802*4882a593Smuzhiyun dwc->bounce_addr);
4803*4882a593Smuzhiyun kfree(dwc->setup_buf);
4804*4882a593Smuzhiyun dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
4805*4882a593Smuzhiyun dwc->ep0_trb, dwc->ep0_trb_addr);
4806*4882a593Smuzhiyun }
4807*4882a593Smuzhiyun
dwc3_gadget_suspend(struct dwc3 * dwc)4808*4882a593Smuzhiyun int dwc3_gadget_suspend(struct dwc3 *dwc)
4809*4882a593Smuzhiyun {
4810*4882a593Smuzhiyun unsigned long flags;
4811*4882a593Smuzhiyun
4812*4882a593Smuzhiyun if (!dwc->gadget_driver)
4813*4882a593Smuzhiyun return 0;
4814*4882a593Smuzhiyun
4815*4882a593Smuzhiyun dwc3_gadget_run_stop(dwc, false, false);
4816*4882a593Smuzhiyun
4817*4882a593Smuzhiyun spin_lock_irqsave(&dwc->lock, flags);
4818*4882a593Smuzhiyun dwc3_disconnect_gadget(dwc);
4819*4882a593Smuzhiyun __dwc3_gadget_stop(dwc);
4820*4882a593Smuzhiyun spin_unlock_irqrestore(&dwc->lock, flags);
4821*4882a593Smuzhiyun
4822*4882a593Smuzhiyun return 0;
4823*4882a593Smuzhiyun }
4824*4882a593Smuzhiyun
dwc3_gadget_resume(struct dwc3 * dwc)4825*4882a593Smuzhiyun int dwc3_gadget_resume(struct dwc3 *dwc)
4826*4882a593Smuzhiyun {
4827*4882a593Smuzhiyun struct dwc3_vendor *vdwc = container_of(dwc, struct dwc3_vendor, dwc);
4828*4882a593Smuzhiyun int ret;
4829*4882a593Smuzhiyun
4830*4882a593Smuzhiyun if (!dwc->gadget_driver || !vdwc->softconnect)
4831*4882a593Smuzhiyun return 0;
4832*4882a593Smuzhiyun
4833*4882a593Smuzhiyun ret = __dwc3_gadget_start(dwc);
4834*4882a593Smuzhiyun if (ret < 0)
4835*4882a593Smuzhiyun goto err0;
4836*4882a593Smuzhiyun
4837*4882a593Smuzhiyun ret = dwc3_gadget_run_stop(dwc, true, false);
4838*4882a593Smuzhiyun if (ret < 0)
4839*4882a593Smuzhiyun goto err1;
4840*4882a593Smuzhiyun
4841*4882a593Smuzhiyun return 0;
4842*4882a593Smuzhiyun
4843*4882a593Smuzhiyun err1:
4844*4882a593Smuzhiyun __dwc3_gadget_stop(dwc);
4845*4882a593Smuzhiyun
4846*4882a593Smuzhiyun err0:
4847*4882a593Smuzhiyun return ret;
4848*4882a593Smuzhiyun }
4849*4882a593Smuzhiyun
dwc3_gadget_process_pending_events(struct dwc3 * dwc)4850*4882a593Smuzhiyun void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
4851*4882a593Smuzhiyun {
4852*4882a593Smuzhiyun if (dwc->pending_events) {
4853*4882a593Smuzhiyun dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
4854*4882a593Smuzhiyun dwc->pending_events = false;
4855*4882a593Smuzhiyun enable_irq(dwc->irq_gadget);
4856*4882a593Smuzhiyun }
4857*4882a593Smuzhiyun }
4858