xref: /openbmc/linux/drivers/usb/dwc3/gadget.c (revision 8ebc80a25f9d9bf7a8e368b266d5b740c485c362)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2bfad65eeSFelipe Balbi /*
372246da4SFelipe Balbi  * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
472246da4SFelipe Balbi  *
510623b87SAlexander A. Klimov  * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com
672246da4SFelipe Balbi  *
772246da4SFelipe Balbi  * Authors: Felipe Balbi <balbi@ti.com>,
872246da4SFelipe Balbi  *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
972246da4SFelipe Balbi  */
1072246da4SFelipe Balbi 
1172246da4SFelipe Balbi #include <linux/kernel.h>
1272246da4SFelipe Balbi #include <linux/delay.h>
1372246da4SFelipe Balbi #include <linux/slab.h>
1472246da4SFelipe Balbi #include <linux/spinlock.h>
1572246da4SFelipe Balbi #include <linux/platform_device.h>
1672246da4SFelipe Balbi #include <linux/pm_runtime.h>
1772246da4SFelipe Balbi #include <linux/interrupt.h>
1872246da4SFelipe Balbi #include <linux/io.h>
1972246da4SFelipe Balbi #include <linux/list.h>
2072246da4SFelipe Balbi #include <linux/dma-mapping.h>
2172246da4SFelipe Balbi 
2272246da4SFelipe Balbi #include <linux/usb/ch9.h>
2372246da4SFelipe Balbi #include <linux/usb/gadget.h>
2472246da4SFelipe Balbi 
2580977dc9SFelipe Balbi #include "debug.h"
2672246da4SFelipe Balbi #include "core.h"
2772246da4SFelipe Balbi #include "gadget.h"
2872246da4SFelipe Balbi #include "io.h"
2972246da4SFelipe Balbi 
30d5370106SFelipe Balbi #define DWC3_ALIGN_FRAME(d, n)	(((d)->frame_number + ((d)->interval * (n))) \
31f62afb49SFelipe Balbi 					& ~((d)->interval - 1))
32f62afb49SFelipe Balbi 
3304a9bfcdSFelipe Balbi /**
34bfad65eeSFelipe Balbi  * dwc3_gadget_set_test_mode - enables usb2 test modes
3504a9bfcdSFelipe Balbi  * @dwc: pointer to our context structure
3604a9bfcdSFelipe Balbi  * @mode: the mode to set (J, K SE0 NAK, Force Enable)
3704a9bfcdSFelipe Balbi  *
38bfad65eeSFelipe Balbi  * Caller should take care of locking. This function will return 0 on
39bfad65eeSFelipe Balbi  * success or -EINVAL if wrong Test Selector is passed.
4004a9bfcdSFelipe Balbi  */
dwc3_gadget_set_test_mode(struct dwc3 * dwc,int mode)4104a9bfcdSFelipe Balbi int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
4204a9bfcdSFelipe Balbi {
4304a9bfcdSFelipe Balbi 	u32		reg;
4404a9bfcdSFelipe Balbi 
4504a9bfcdSFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4604a9bfcdSFelipe Balbi 	reg &= ~DWC3_DCTL_TSTCTRL_MASK;
4704a9bfcdSFelipe Balbi 
4804a9bfcdSFelipe Balbi 	switch (mode) {
4962fb45d3SGreg Kroah-Hartman 	case USB_TEST_J:
5062fb45d3SGreg Kroah-Hartman 	case USB_TEST_K:
5162fb45d3SGreg Kroah-Hartman 	case USB_TEST_SE0_NAK:
5262fb45d3SGreg Kroah-Hartman 	case USB_TEST_PACKET:
5362fb45d3SGreg Kroah-Hartman 	case USB_TEST_FORCE_ENABLE:
5404a9bfcdSFelipe Balbi 		reg |= mode << 1;
5504a9bfcdSFelipe Balbi 		break;
5604a9bfcdSFelipe Balbi 	default:
5704a9bfcdSFelipe Balbi 		return -EINVAL;
5804a9bfcdSFelipe Balbi 	}
5904a9bfcdSFelipe Balbi 
605b738211SThinh Nguyen 	dwc3_gadget_dctl_write_safe(dwc, reg);
6104a9bfcdSFelipe Balbi 
6204a9bfcdSFelipe Balbi 	return 0;
6304a9bfcdSFelipe Balbi }
6404a9bfcdSFelipe Balbi 
658598bde7SFelipe Balbi /**
66bfad65eeSFelipe Balbi  * dwc3_gadget_get_link_state - gets current state of usb link
67911f1f88SPaul Zimmerman  * @dwc: pointer to our context structure
68911f1f88SPaul Zimmerman  *
69911f1f88SPaul Zimmerman  * Caller should take care of locking. This function will
70911f1f88SPaul Zimmerman  * return the link state on success (>= 0) or -ETIMEDOUT.
71911f1f88SPaul Zimmerman  */
dwc3_gadget_get_link_state(struct dwc3 * dwc)72911f1f88SPaul Zimmerman int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73911f1f88SPaul Zimmerman {
74911f1f88SPaul Zimmerman 	u32		reg;
75911f1f88SPaul Zimmerman 
76911f1f88SPaul Zimmerman 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
77911f1f88SPaul Zimmerman 
78911f1f88SPaul Zimmerman 	return DWC3_DSTS_USBLNKST(reg);
79911f1f88SPaul Zimmerman }
80911f1f88SPaul Zimmerman 
81911f1f88SPaul Zimmerman /**
82bfad65eeSFelipe Balbi  * dwc3_gadget_set_link_state - sets usb link to a particular state
838598bde7SFelipe Balbi  * @dwc: pointer to our context structure
848598bde7SFelipe Balbi  * @state: the state to put link into
858598bde7SFelipe Balbi  *
868598bde7SFelipe Balbi  * Caller should take care of locking. This function will
87aee63e3cSPaul Zimmerman  * return 0 on success or -ETIMEDOUT.
888598bde7SFelipe Balbi  */
dwc3_gadget_set_link_state(struct dwc3 * dwc,enum dwc3_link_state state)898598bde7SFelipe Balbi int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
908598bde7SFelipe Balbi {
91aee63e3cSPaul Zimmerman 	int		retries = 10000;
928598bde7SFelipe Balbi 	u32		reg;
938598bde7SFelipe Balbi 
94802fde98SPaul Zimmerman 	/*
95802fde98SPaul Zimmerman 	 * Wait until device controller is ready. Only applies to 1.94a and
96802fde98SPaul Zimmerman 	 * later RTL.
97802fde98SPaul Zimmerman 	 */
989af21dd6SThinh Nguyen 	if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) {
99802fde98SPaul Zimmerman 		while (--retries) {
100802fde98SPaul Zimmerman 			reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101802fde98SPaul Zimmerman 			if (reg & DWC3_DSTS_DCNRD)
102802fde98SPaul Zimmerman 				udelay(5);
103802fde98SPaul Zimmerman 			else
104802fde98SPaul Zimmerman 				break;
105802fde98SPaul Zimmerman 		}
106802fde98SPaul Zimmerman 
107802fde98SPaul Zimmerman 		if (retries <= 0)
108802fde98SPaul Zimmerman 			return -ETIMEDOUT;
109802fde98SPaul Zimmerman 	}
110802fde98SPaul Zimmerman 
1118598bde7SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1128598bde7SFelipe Balbi 	reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1138598bde7SFelipe Balbi 
1142e708fa3SThinh Nguyen 	/* set no action before sending new link state change */
1152e708fa3SThinh Nguyen 	dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1162e708fa3SThinh Nguyen 
1178598bde7SFelipe Balbi 	/* set requested state */
1188598bde7SFelipe Balbi 	reg |= DWC3_DCTL_ULSTCHNGREQ(state);
1198598bde7SFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1208598bde7SFelipe Balbi 
121802fde98SPaul Zimmerman 	/*
122802fde98SPaul Zimmerman 	 * The following code is racy when called from dwc3_gadget_wakeup,
123802fde98SPaul Zimmerman 	 * and is not needed, at least on newer versions
124802fde98SPaul Zimmerman 	 */
1259af21dd6SThinh Nguyen 	if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
126802fde98SPaul Zimmerman 		return 0;
127802fde98SPaul Zimmerman 
1288598bde7SFelipe Balbi 	/* wait for a change in DSTS */
129aed430e5SPaul Zimmerman 	retries = 10000;
1308598bde7SFelipe Balbi 	while (--retries) {
1318598bde7SFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1328598bde7SFelipe Balbi 
1338598bde7SFelipe Balbi 		if (DWC3_DSTS_USBLNKST(reg) == state)
1348598bde7SFelipe Balbi 			return 0;
1358598bde7SFelipe Balbi 
136aee63e3cSPaul Zimmerman 		udelay(5);
1378598bde7SFelipe Balbi 	}
1388598bde7SFelipe Balbi 
1398598bde7SFelipe Balbi 	return -ETIMEDOUT;
1408598bde7SFelipe Balbi }
1418598bde7SFelipe Balbi 
dwc3_ep0_reset_state(struct dwc3 * dwc)1428f40fc08SWesley Cheng static void dwc3_ep0_reset_state(struct dwc3 *dwc)
1438f40fc08SWesley Cheng {
1448f40fc08SWesley Cheng 	unsigned int	dir;
1458f40fc08SWesley Cheng 
1468f40fc08SWesley Cheng 	if (dwc->ep0state != EP0_SETUP_PHASE) {
1478f40fc08SWesley Cheng 		dir = !!dwc->ep0_expect_in;
1488f40fc08SWesley Cheng 		if (dwc->ep0state == EP0_DATA_PHASE)
1498f40fc08SWesley Cheng 			dwc3_ep0_end_control_data(dwc, dwc->eps[dir]);
1508f40fc08SWesley Cheng 		else
1518f40fc08SWesley Cheng 			dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]);
1528f40fc08SWesley Cheng 
1538f40fc08SWesley Cheng 		dwc->eps[0]->trb_enqueue = 0;
1548f40fc08SWesley Cheng 		dwc->eps[1]->trb_enqueue = 0;
1558f40fc08SWesley Cheng 
1568f40fc08SWesley Cheng 		dwc3_ep0_stall_and_restart(dwc);
1578f40fc08SWesley Cheng 	}
1588f40fc08SWesley Cheng }
1598f40fc08SWesley Cheng 
160dca0119cSJohn Youn /**
161bfad65eeSFelipe Balbi  * dwc3_ep_inc_trb - increment a trb index.
162bfad65eeSFelipe Balbi  * @index: Pointer to the TRB index to increment.
163dca0119cSJohn Youn  *
164dca0119cSJohn Youn  * The index should never point to the link TRB. After incrementing,
165dca0119cSJohn Youn  * if it is point to the link TRB, wrap around to the beginning. The
166dca0119cSJohn Youn  * link TRB is always at the last TRB entry.
167dca0119cSJohn Youn  */
dwc3_ep_inc_trb(u8 * index)168dca0119cSJohn Youn static void dwc3_ep_inc_trb(u8 *index)
169dca0119cSJohn Youn {
170dca0119cSJohn Youn 	(*index)++;
171dca0119cSJohn Youn 	if (*index == (DWC3_TRB_NUM - 1))
172dca0119cSJohn Youn 		*index = 0;
173dca0119cSJohn Youn }
174dca0119cSJohn Youn 
175bfad65eeSFelipe Balbi /**
176bfad65eeSFelipe Balbi  * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
177bfad65eeSFelipe Balbi  * @dep: The endpoint whose enqueue pointer we're incrementing
178bfad65eeSFelipe Balbi  */
dwc3_ep_inc_enq(struct dwc3_ep * dep)179ef966b9dSFelipe Balbi static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
180457e84b6SFelipe Balbi {
181dca0119cSJohn Youn 	dwc3_ep_inc_trb(&dep->trb_enqueue);
182457e84b6SFelipe Balbi }
183457e84b6SFelipe Balbi 
184bfad65eeSFelipe Balbi /**
185bfad65eeSFelipe Balbi  * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
186bfad65eeSFelipe Balbi  * @dep: The endpoint whose enqueue pointer we're incrementing
187bfad65eeSFelipe Balbi  */
dwc3_ep_inc_deq(struct dwc3_ep * dep)188ef966b9dSFelipe Balbi static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
189ef966b9dSFelipe Balbi {
190dca0119cSJohn Youn 	dwc3_ep_inc_trb(&dep->trb_dequeue);
191457e84b6SFelipe Balbi }
192457e84b6SFelipe Balbi 
dwc3_gadget_del_and_unmap_request(struct dwc3_ep * dep,struct dwc3_request * req,int status)19369102510SWei Yongjun static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
194c91815b5SFelipe Balbi 		struct dwc3_request *req, int status)
19572246da4SFelipe Balbi {
19672246da4SFelipe Balbi 	struct dwc3			*dwc = dep->dwc;
19772246da4SFelipe Balbi 
19872246da4SFelipe Balbi 	list_del(&req->list);
199e62c5bc5SFelipe Balbi 	req->remaining = 0;
200bd674224SJack Pham 	req->needs_extra_trb = false;
20100f8205fSElson Roy Serrao 	req->num_trbs = 0;
20272246da4SFelipe Balbi 
20372246da4SFelipe Balbi 	if (req->request.status == -EINPROGRESS)
20472246da4SFelipe Balbi 		req->request.status = status;
20572246da4SFelipe Balbi 
2064a71fcb8SJack Pham 	if (req->trb)
207d64ff406SArnd Bergmann 		usb_gadget_unmap_request_by_dev(dwc->sysdev,
208d64ff406SArnd Bergmann 				&req->request, req->direction);
20972246da4SFelipe Balbi 
2104a71fcb8SJack Pham 	req->trb = NULL;
2112c4cbe6eSFelipe Balbi 	trace_dwc3_gadget_giveback(req);
21272246da4SFelipe Balbi 
213c91815b5SFelipe Balbi 	if (dep->number > 1)
214c91815b5SFelipe Balbi 		pm_runtime_put(dwc->dev);
215c91815b5SFelipe Balbi }
216c91815b5SFelipe Balbi 
217c91815b5SFelipe Balbi /**
218c91815b5SFelipe Balbi  * dwc3_gadget_giveback - call struct usb_request's ->complete callback
219c91815b5SFelipe Balbi  * @dep: The endpoint to whom the request belongs to
220c91815b5SFelipe Balbi  * @req: The request we're giving back
221c91815b5SFelipe Balbi  * @status: completion code for the request
222c91815b5SFelipe Balbi  *
223c91815b5SFelipe Balbi  * Must be called with controller's lock held and interrupts disabled. This
224c91815b5SFelipe Balbi  * function will unmap @req and call its ->complete() callback to notify upper
225c91815b5SFelipe Balbi  * layers that it has completed.
226c91815b5SFelipe Balbi  */
dwc3_gadget_giveback(struct dwc3_ep * dep,struct dwc3_request * req,int status)227c91815b5SFelipe Balbi void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
228c91815b5SFelipe Balbi 		int status)
229c91815b5SFelipe Balbi {
230c91815b5SFelipe Balbi 	struct dwc3			*dwc = dep->dwc;
231c91815b5SFelipe Balbi 
232c91815b5SFelipe Balbi 	dwc3_gadget_del_and_unmap_request(dep, req, status);
233a3af5e3aSFelipe Balbi 	req->status = DWC3_REQUEST_STATUS_COMPLETED;
234c91815b5SFelipe Balbi 
23572246da4SFelipe Balbi 	spin_unlock(&dwc->lock);
236304f7e5eSMichal Sojka 	usb_gadget_giveback_request(&dep->endpoint, &req->request);
23772246da4SFelipe Balbi 	spin_lock(&dwc->lock);
23872246da4SFelipe Balbi }
23972246da4SFelipe Balbi 
240bfad65eeSFelipe Balbi /**
241bfad65eeSFelipe Balbi  * dwc3_send_gadget_generic_command - issue a generic command for the controller
242bfad65eeSFelipe Balbi  * @dwc: pointer to the controller context
243bfad65eeSFelipe Balbi  * @cmd: the command to be issued
244bfad65eeSFelipe Balbi  * @param: command parameter
245bfad65eeSFelipe Balbi  *
246bfad65eeSFelipe Balbi  * Caller should take care of locking. Issue @cmd with a given @param to @dwc
247bfad65eeSFelipe Balbi  * and wait for its completion.
248bfad65eeSFelipe Balbi  */
dwc3_send_gadget_generic_command(struct dwc3 * dwc,unsigned int cmd,u32 param)249e319bd62SFelipe Balbi int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd,
250e319bd62SFelipe Balbi 		u32 param)
251b09bb642SFelipe Balbi {
252b09bb642SFelipe Balbi 	u32		timeout = 500;
25371f7e702SFelipe Balbi 	int		status = 0;
2540fe886cdSFelipe Balbi 	int		ret = 0;
255b09bb642SFelipe Balbi 	u32		reg;
256b09bb642SFelipe Balbi 
257b09bb642SFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
258b09bb642SFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
259b09bb642SFelipe Balbi 
260b09bb642SFelipe Balbi 	do {
261b09bb642SFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
262b09bb642SFelipe Balbi 		if (!(reg & DWC3_DGCMD_CMDACT)) {
26371f7e702SFelipe Balbi 			status = DWC3_DGCMD_STATUS(reg);
26471f7e702SFelipe Balbi 			if (status)
2650fe886cdSFelipe Balbi 				ret = -EINVAL;
2660fe886cdSFelipe Balbi 			break;
267b09bb642SFelipe Balbi 		}
268e3aee486SJanusz Dziedzic 	} while (--timeout);
2690fe886cdSFelipe Balbi 
27073815280SFelipe Balbi 	if (!timeout) {
2710fe886cdSFelipe Balbi 		ret = -ETIMEDOUT;
27271f7e702SFelipe Balbi 		status = -ETIMEDOUT;
27373815280SFelipe Balbi 	}
2740fe886cdSFelipe Balbi 
27571f7e702SFelipe Balbi 	trace_dwc3_gadget_generic_cmd(cmd, param, status);
27671f7e702SFelipe Balbi 
2770fe886cdSFelipe Balbi 	return ret;
278b09bb642SFelipe Balbi }
279b09bb642SFelipe Balbi 
28004716168SElson Roy Serrao static int __dwc3_gadget_wakeup(struct dwc3 *dwc, bool async);
281c36d8e94SFelipe Balbi 
282bfad65eeSFelipe Balbi /**
283bfad65eeSFelipe Balbi  * dwc3_send_gadget_ep_cmd - issue an endpoint command
284bfad65eeSFelipe Balbi  * @dep: the endpoint to which the command is going to be issued
285bfad65eeSFelipe Balbi  * @cmd: the command to be issued
286bfad65eeSFelipe Balbi  * @params: parameters to the command
287bfad65eeSFelipe Balbi  *
288bfad65eeSFelipe Balbi  * Caller should handle locking. This function will issue @cmd with given
289bfad65eeSFelipe Balbi  * @params to @dep and wait for its completion.
2907d301dd2SPrashanth K  *
2917d301dd2SPrashanth K  * According to the programming guide, if the link state is in L1/L2/U3,
2927d301dd2SPrashanth K  * then sending the Start Transfer command may not complete. The
2937d301dd2SPrashanth K  * programming guide suggested to bring the link state back to ON/U0 by
2947d301dd2SPrashanth K  * performing remote wakeup prior to sending the command. However, don't
2957d301dd2SPrashanth K  * initiate remote wakeup when the user/function does not send wakeup
2967d301dd2SPrashanth K  * request via wakeup ops. Send the command when it's allowed.
2977d301dd2SPrashanth K  *
2987d301dd2SPrashanth K  * Notes:
2997d301dd2SPrashanth K  * For L1 link state, issuing a command requires the clearing of
3007d301dd2SPrashanth K  * GUSB2PHYCFG.SUSPENDUSB2, which turns on the signal required to complete
3017d301dd2SPrashanth K  * the given command (usually within 50us). This should happen within the
3027d301dd2SPrashanth K  * command timeout set by driver. No additional step is needed.
3037d301dd2SPrashanth K  *
3047d301dd2SPrashanth K  * For L2 or U3 link state, the gadget is in USB suspend. Care should be
3057d301dd2SPrashanth K  * taken when sending Start Transfer command to ensure that it's done after
3067d301dd2SPrashanth K  * USB resume.
307bfad65eeSFelipe Balbi  */
dwc3_send_gadget_ep_cmd(struct dwc3_ep * dep,unsigned int cmd,struct dwc3_gadget_ep_cmd_params * params)308e319bd62SFelipe Balbi int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd,
3092cd4718dSFelipe Balbi 		struct dwc3_gadget_ep_cmd_params *params)
31072246da4SFelipe Balbi {
3118897a761SFelipe Balbi 	const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
3122cd4718dSFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
3131c0e69aeSYu Chen 	u32			timeout = 5000;
31487dd9611SThinh Nguyen 	u32			saved_config = 0;
31572246da4SFelipe Balbi 	u32			reg;
31672246da4SFelipe Balbi 
3170933df15SFelipe Balbi 	int			cmd_status = 0;
318c0ca324dSFelipe Balbi 	int			ret = -EINVAL;
31972246da4SFelipe Balbi 
3202b0f11dfSFelipe Balbi 	/*
32187dd9611SThinh Nguyen 	 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or
32287dd9611SThinh Nguyen 	 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an
32387dd9611SThinh Nguyen 	 * endpoint command.
3242b0f11dfSFelipe Balbi 	 *
32587dd9611SThinh Nguyen 	 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY
32687dd9611SThinh Nguyen 	 * settings. Restore them after the command is completed.
32787dd9611SThinh Nguyen 	 *
32887dd9611SThinh Nguyen 	 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2
3292b0f11dfSFelipe Balbi 	 */
3303aa07f72SThinh Nguyen 	if (dwc->gadget->speed <= USB_SPEED_HIGH ||
3313aa07f72SThinh Nguyen 	    DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER) {
3322b0f11dfSFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
3332b0f11dfSFelipe Balbi 		if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
33487dd9611SThinh Nguyen 			saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
3352b0f11dfSFelipe Balbi 			reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
3362b0f11dfSFelipe Balbi 		}
33787dd9611SThinh Nguyen 
33887dd9611SThinh Nguyen 		if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
33987dd9611SThinh Nguyen 			saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
34087dd9611SThinh Nguyen 			reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
34187dd9611SThinh Nguyen 		}
34287dd9611SThinh Nguyen 
34387dd9611SThinh Nguyen 		if (saved_config)
34487dd9611SThinh Nguyen 			dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
345ab2a92e7SFelipe Balbi 	}
3462b0f11dfSFelipe Balbi 
347a02a26ebSThinh Nguyen 	/*
348a02a26ebSThinh Nguyen 	 * For some commands such as Update Transfer command, DEPCMDPARn
349a02a26ebSThinh Nguyen 	 * registers are reserved. Since the driver often sends Update Transfer
350a02a26ebSThinh Nguyen 	 * command, don't write to DEPCMDPARn to avoid register write delays and
351a02a26ebSThinh Nguyen 	 * improve performance.
352a02a26ebSThinh Nguyen 	 */
353a02a26ebSThinh Nguyen 	if (DWC3_DEPCMD_CMD(cmd) != DWC3_DEPCMD_UPDATETRANSFER) {
3542eb88016SFelipe Balbi 		dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
3552eb88016SFelipe Balbi 		dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
3562eb88016SFelipe Balbi 		dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
357a02a26ebSThinh Nguyen 	}
35872246da4SFelipe Balbi 
3598897a761SFelipe Balbi 	/*
3608897a761SFelipe Balbi 	 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
3618897a761SFelipe Balbi 	 * not relying on XferNotReady, we can make use of a special "No
3628897a761SFelipe Balbi 	 * Response Update Transfer" command where we should clear both CmdAct
3638897a761SFelipe Balbi 	 * and CmdIOC bits.
3648897a761SFelipe Balbi 	 *
3658897a761SFelipe Balbi 	 * With this, we don't need to wait for command completion and can
3668897a761SFelipe Balbi 	 * straight away issue further commands to the endpoint.
3678897a761SFelipe Balbi 	 *
3688897a761SFelipe Balbi 	 * NOTICE: We're making an assumption that control endpoints will never
3698897a761SFelipe Balbi 	 * make use of Update Transfer command. This is a safe assumption
3708897a761SFelipe Balbi 	 * because we can never have more than one request at a time with
3718897a761SFelipe Balbi 	 * Control Endpoints. If anybody changes that assumption, this chunk
3728897a761SFelipe Balbi 	 * needs to be updated accordingly.
3738897a761SFelipe Balbi 	 */
3748897a761SFelipe Balbi 	if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
3758897a761SFelipe Balbi 			!usb_endpoint_xfer_isoc(desc))
3768897a761SFelipe Balbi 		cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
3778897a761SFelipe Balbi 	else
3788897a761SFelipe Balbi 		cmd |= DWC3_DEPCMD_CMDACT;
3798897a761SFelipe Balbi 
3808897a761SFelipe Balbi 	dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
381bc27117cSThinh Nguyen 
382b353eb6dSWesley Cheng 	if (!(cmd & DWC3_DEPCMD_CMDACT) ||
383b353eb6dSWesley Cheng 		(DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
384b353eb6dSWesley Cheng 		!(cmd & DWC3_DEPCMD_CMDIOC))) {
385bc27117cSThinh Nguyen 		ret = 0;
386bc27117cSThinh Nguyen 		goto skip_status;
387bc27117cSThinh Nguyen 	}
388bc27117cSThinh Nguyen 
38972246da4SFelipe Balbi 	do {
3902eb88016SFelipe Balbi 		reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
39172246da4SFelipe Balbi 		if (!(reg & DWC3_DEPCMD_CMDACT)) {
3920933df15SFelipe Balbi 			cmd_status = DWC3_DEPCMD_STATUS(reg);
3937b9cc7a2SKonrad Leszczynski 
3947b9cc7a2SKonrad Leszczynski 			switch (cmd_status) {
3957b9cc7a2SKonrad Leszczynski 			case 0:
396c0ca324dSFelipe Balbi 				ret = 0;
397c0ca324dSFelipe Balbi 				break;
3987b9cc7a2SKonrad Leszczynski 			case DEPEVT_TRANSFER_NO_RESOURCE:
399f7ac582eSThinh Nguyen 				dev_WARN(dwc->dev, "No resource for %s\n",
400f7ac582eSThinh Nguyen 					 dep->name);
4017b9cc7a2SKonrad Leszczynski 				ret = -EINVAL;
4027b9cc7a2SKonrad Leszczynski 				break;
4037b9cc7a2SKonrad Leszczynski 			case DEPEVT_TRANSFER_BUS_EXPIRY:
4047b9cc7a2SKonrad Leszczynski 				/*
4057b9cc7a2SKonrad Leszczynski 				 * SW issues START TRANSFER command to
4067b9cc7a2SKonrad Leszczynski 				 * isochronous ep with future frame interval. If
4077b9cc7a2SKonrad Leszczynski 				 * future interval time has already passed when
4087b9cc7a2SKonrad Leszczynski 				 * core receives the command, it will respond
4097b9cc7a2SKonrad Leszczynski 				 * with an error status of 'Bus Expiry'.
4107b9cc7a2SKonrad Leszczynski 				 *
4117b9cc7a2SKonrad Leszczynski 				 * Instead of always returning -EINVAL, let's
4127b9cc7a2SKonrad Leszczynski 				 * give a hint to the gadget driver that this is
4137b9cc7a2SKonrad Leszczynski 				 * the case by returning -EAGAIN.
4147b9cc7a2SKonrad Leszczynski 				 */
4157b9cc7a2SKonrad Leszczynski 				ret = -EAGAIN;
4167b9cc7a2SKonrad Leszczynski 				break;
4177b9cc7a2SKonrad Leszczynski 			default:
4187b9cc7a2SKonrad Leszczynski 				dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
4197b9cc7a2SKonrad Leszczynski 			}
4207b9cc7a2SKonrad Leszczynski 
4217b9cc7a2SKonrad Leszczynski 			break;
42272246da4SFelipe Balbi 		}
423f6bb225bSFelipe Balbi 	} while (--timeout);
42472246da4SFelipe Balbi 
425f6bb225bSFelipe Balbi 	if (timeout == 0) {
426c0ca324dSFelipe Balbi 		ret = -ETIMEDOUT;
4270933df15SFelipe Balbi 		cmd_status = -ETIMEDOUT;
42873815280SFelipe Balbi 	}
429c0ca324dSFelipe Balbi 
430bc27117cSThinh Nguyen skip_status:
4310933df15SFelipe Balbi 	trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
4320933df15SFelipe Balbi 
4339bc3395cSThinh Nguyen 	if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
4349bc3395cSThinh Nguyen 		if (ret == 0)
4356cb2e4e3SFelipe Balbi 			dep->flags |= DWC3_EP_TRANSFER_STARTED;
4369bc3395cSThinh Nguyen 
4379bc3395cSThinh Nguyen 		if (ret != -ETIMEDOUT)
438d7ca7e18SFelipe Balbi 			dwc3_gadget_ep_get_transfer_index(dep);
4396cb2e4e3SFelipe Balbi 	}
4406cb2e4e3SFelipe Balbi 
441c3d3501cSPrashanth K 	if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_ENDTRANSFER &&
442c3d3501cSPrashanth K 	    !(cmd & DWC3_DEPCMD_CMDIOC))
443c3d3501cSPrashanth K 		mdelay(1);
444c3d3501cSPrashanth K 
44587dd9611SThinh Nguyen 	if (saved_config) {
4462b0f11dfSFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
44787dd9611SThinh Nguyen 		reg |= saved_config;
4482b0f11dfSFelipe Balbi 		dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
4492b0f11dfSFelipe Balbi 	}
4502b0f11dfSFelipe Balbi 
451c0ca324dSFelipe Balbi 	return ret;
45272246da4SFelipe Balbi }
45372246da4SFelipe Balbi 
dwc3_send_clear_stall_ep_cmd(struct dwc3_ep * dep)45450c763f8SJohn Youn static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
45550c763f8SJohn Youn {
45650c763f8SJohn Youn 	struct dwc3 *dwc = dep->dwc;
45750c763f8SJohn Youn 	struct dwc3_gadget_ep_cmd_params params;
45850c763f8SJohn Youn 	u32 cmd = DWC3_DEPCMD_CLEARSTALL;
45950c763f8SJohn Youn 
46050c763f8SJohn Youn 	/*
46150c763f8SJohn Youn 	 * As of core revision 2.60a the recommended programming model
46250c763f8SJohn Youn 	 * is to set the ClearPendIN bit when issuing a Clear Stall EP
46350c763f8SJohn Youn 	 * command for IN endpoints. This is to prevent an issue where
46450c763f8SJohn Youn 	 * some (non-compliant) hosts may not send ACK TPs for pending
46550c763f8SJohn Youn 	 * IN transfers due to a mishandled error condition. Synopsys
46650c763f8SJohn Youn 	 * STAR 9000614252.
46750c763f8SJohn Youn 	 */
4689af21dd6SThinh Nguyen 	if (dep->direction &&
4699af21dd6SThinh Nguyen 	    !DWC3_VER_IS_PRIOR(DWC3, 260A) &&
470e81a7018SPeter Chen 	    (dwc->gadget->speed >= USB_SPEED_SUPER))
47150c763f8SJohn Youn 		cmd |= DWC3_DEPCMD_CLEARPENDIN;
47250c763f8SJohn Youn 
47350c763f8SJohn Youn 	memset(&params, 0, sizeof(params));
47450c763f8SJohn Youn 
4752cd4718dSFelipe Balbi 	return dwc3_send_gadget_ep_cmd(dep, cmd, &params);
47650c763f8SJohn Youn }
47750c763f8SJohn Youn 
dwc3_trb_dma_offset(struct dwc3_ep * dep,struct dwc3_trb * trb)47872246da4SFelipe Balbi static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
479f6bafc6aSFelipe Balbi 		struct dwc3_trb *trb)
48072246da4SFelipe Balbi {
481c439ef87SPaul Zimmerman 	u32		offset = (char *) trb - (char *) dep->trb_pool;
48272246da4SFelipe Balbi 
48372246da4SFelipe Balbi 	return dep->trb_pool_dma + offset;
48472246da4SFelipe Balbi }
48572246da4SFelipe Balbi 
dwc3_alloc_trb_pool(struct dwc3_ep * dep)48672246da4SFelipe Balbi static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
48772246da4SFelipe Balbi {
48872246da4SFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
48972246da4SFelipe Balbi 
49072246da4SFelipe Balbi 	if (dep->trb_pool)
49172246da4SFelipe Balbi 		return 0;
49272246da4SFelipe Balbi 
493d64ff406SArnd Bergmann 	dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
49472246da4SFelipe Balbi 			sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
49572246da4SFelipe Balbi 			&dep->trb_pool_dma, GFP_KERNEL);
49672246da4SFelipe Balbi 	if (!dep->trb_pool) {
49772246da4SFelipe Balbi 		dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
49872246da4SFelipe Balbi 				dep->name);
49972246da4SFelipe Balbi 		return -ENOMEM;
50072246da4SFelipe Balbi 	}
50172246da4SFelipe Balbi 
50272246da4SFelipe Balbi 	return 0;
50372246da4SFelipe Balbi }
50472246da4SFelipe Balbi 
dwc3_free_trb_pool(struct dwc3_ep * dep)50572246da4SFelipe Balbi static void dwc3_free_trb_pool(struct dwc3_ep *dep)
50672246da4SFelipe Balbi {
50772246da4SFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
50872246da4SFelipe Balbi 
509d64ff406SArnd Bergmann 	dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
51072246da4SFelipe Balbi 			dep->trb_pool, dep->trb_pool_dma);
51172246da4SFelipe Balbi 
51272246da4SFelipe Balbi 	dep->trb_pool = NULL;
51372246da4SFelipe Balbi 	dep->trb_pool_dma = 0;
51472246da4SFelipe Balbi }
51572246da4SFelipe Balbi 
dwc3_gadget_set_xfer_resource(struct dwc3_ep * dep)51620d1d43fSFelipe Balbi static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
51720d1d43fSFelipe Balbi {
51820d1d43fSFelipe Balbi 	struct dwc3_gadget_ep_cmd_params params;
51965b1f311SThinh Nguyen 	int ret;
52065b1f311SThinh Nguyen 
52165b1f311SThinh Nguyen 	if (dep->flags & DWC3_EP_RESOURCE_ALLOCATED)
52265b1f311SThinh Nguyen 		return 0;
52320d1d43fSFelipe Balbi 
52420d1d43fSFelipe Balbi 	memset(&params, 0x00, sizeof(params));
52520d1d43fSFelipe Balbi 
52620d1d43fSFelipe Balbi 	params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
52720d1d43fSFelipe Balbi 
52865b1f311SThinh Nguyen 	ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
52920d1d43fSFelipe Balbi 			&params);
53065b1f311SThinh Nguyen 	if (ret)
53165b1f311SThinh Nguyen 		return ret;
53265b1f311SThinh Nguyen 
53365b1f311SThinh Nguyen 	dep->flags |= DWC3_EP_RESOURCE_ALLOCATED;
53465b1f311SThinh Nguyen 	return 0;
53520d1d43fSFelipe Balbi }
536c4509601SJohn Youn 
537c4509601SJohn Youn /**
53865b1f311SThinh Nguyen  * dwc3_gadget_start_config - reset endpoint resources
53965b1f311SThinh Nguyen  * @dwc: pointer to the DWC3 context
54065b1f311SThinh Nguyen  * @resource_index: DEPSTARTCFG.XferRscIdx value (must be 0 or 2)
541c4509601SJohn Youn  *
54265b1f311SThinh Nguyen  * Set resource_index=0 to reset all endpoints' resources allocation. Do this as
54365b1f311SThinh Nguyen  * part of the power-on/soft-reset initialization.
544c4509601SJohn Youn  *
54565b1f311SThinh Nguyen  * Set resource_index=2 to reset only non-control endpoints' resources. Do this
54665b1f311SThinh Nguyen  * on receiving the SET_CONFIGURATION request or hibernation resume.
547c4509601SJohn Youn  */
dwc3_gadget_start_config(struct dwc3 * dwc,unsigned int resource_index)54865b1f311SThinh Nguyen int dwc3_gadget_start_config(struct dwc3 *dwc, unsigned int resource_index)
54972246da4SFelipe Balbi {
55072246da4SFelipe Balbi 	struct dwc3_gadget_ep_cmd_params params;
55172246da4SFelipe Balbi 	u32			cmd;
552c4509601SJohn Youn 	int			i;
553c4509601SJohn Youn 	int			ret;
554c4509601SJohn Youn 
55565b1f311SThinh Nguyen 	if (resource_index != 0 && resource_index != 2)
55665b1f311SThinh Nguyen 		return -EINVAL;
55772246da4SFelipe Balbi 
55872246da4SFelipe Balbi 	memset(&params, 0x00, sizeof(params));
55972246da4SFelipe Balbi 	cmd = DWC3_DEPCMD_DEPSTARTCFG;
56065b1f311SThinh Nguyen 	cmd |= DWC3_DEPCMD_PARAM(resource_index);
56172246da4SFelipe Balbi 
56265b1f311SThinh Nguyen 	ret = dwc3_send_gadget_ep_cmd(dwc->eps[0], cmd, &params);
563c4509601SJohn Youn 	if (ret)
564c4509601SJohn Youn 		return ret;
565c4509601SJohn Youn 
56665b1f311SThinh Nguyen 	/* Reset resource allocation flags */
56765b1f311SThinh Nguyen 	for (i = resource_index; i < dwc->num_eps && dwc->eps[i]; i++)
56865b1f311SThinh Nguyen 		dwc->eps[i]->flags &= ~DWC3_EP_RESOURCE_ALLOCATED;
56972246da4SFelipe Balbi 
57072246da4SFelipe Balbi 	return 0;
57172246da4SFelipe Balbi }
57272246da4SFelipe Balbi 
dwc3_gadget_set_ep_config(struct dwc3_ep * dep,unsigned int action)573b07c2db8SFelipe Balbi static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
57472246da4SFelipe Balbi {
57539ebb05cSJohn Youn 	const struct usb_ss_ep_comp_descriptor *comp_desc;
57639ebb05cSJohn Youn 	const struct usb_endpoint_descriptor *desc;
57772246da4SFelipe Balbi 	struct dwc3_gadget_ep_cmd_params params;
578b07c2db8SFelipe Balbi 	struct dwc3 *dwc = dep->dwc;
57972246da4SFelipe Balbi 
58039ebb05cSJohn Youn 	comp_desc = dep->endpoint.comp_desc;
58139ebb05cSJohn Youn 	desc = dep->endpoint.desc;
58239ebb05cSJohn Youn 
58372246da4SFelipe Balbi 	memset(&params, 0x00, sizeof(params));
58472246da4SFelipe Balbi 
585dc1c70a7SFelipe Balbi 	params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
586d2e9a13aSChanho Park 		| DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
587d2e9a13aSChanho Park 
588d2e9a13aSChanho Park 	/* Burst size is only needed in SuperSpeed mode */
589e81a7018SPeter Chen 	if (dwc->gadget->speed >= USB_SPEED_SUPER) {
590676e3497SFelipe Balbi 		u32 burst = dep->endpoint.maxburst;
591e319bd62SFelipe Balbi 
592676e3497SFelipe Balbi 		params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
593d2e9a13aSChanho Park 	}
59472246da4SFelipe Balbi 
595a2d23f08SFelipe Balbi 	params.param0 |= action;
596a2d23f08SFelipe Balbi 	if (action == DWC3_DEPCFG_ACTION_RESTORE)
597265b70a7SPaul Zimmerman 		params.param2 |= dep->saved_state;
598265b70a7SPaul Zimmerman 
5994bc48c97SFelipe Balbi 	if (usb_endpoint_xfer_control(desc))
60013fa2e69SFelipe Balbi 		params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
60113fa2e69SFelipe Balbi 
60213fa2e69SFelipe Balbi 	if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
60313fa2e69SFelipe Balbi 		params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
60472246da4SFelipe Balbi 
60518b7ede5SFelipe Balbi 	if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
606dc1c70a7SFelipe Balbi 		params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
607548f8b31SThinh Nguyen 			| DWC3_DEPCFG_XFER_COMPLETE_EN
608dc1c70a7SFelipe Balbi 			| DWC3_DEPCFG_STREAM_EVENT_EN;
609879631aaSFelipe Balbi 		dep->stream_capable = true;
610879631aaSFelipe Balbi 	}
611879631aaSFelipe Balbi 
6120b93a4c8SFelipe Balbi 	if (!usb_endpoint_xfer_control(desc))
613dc1c70a7SFelipe Balbi 		params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
61472246da4SFelipe Balbi 
61572246da4SFelipe Balbi 	/*
61672246da4SFelipe Balbi 	 * We are doing 1:1 mapping for endpoints, meaning
61772246da4SFelipe Balbi 	 * Physical Endpoints 2 maps to Logical Endpoint 2 and
61872246da4SFelipe Balbi 	 * so on. We consider the direction bit as part of the physical
61972246da4SFelipe Balbi 	 * endpoint number. So USB endpoint 0x81 is 0x03.
62072246da4SFelipe Balbi 	 */
621dc1c70a7SFelipe Balbi 	params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
62272246da4SFelipe Balbi 
62372246da4SFelipe Balbi 	/*
62472246da4SFelipe Balbi 	 * We must use the lower 16 TX FIFOs even though
62572246da4SFelipe Balbi 	 * HW might have more
62672246da4SFelipe Balbi 	 */
62772246da4SFelipe Balbi 	if (dep->direction)
628dc1c70a7SFelipe Balbi 		params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
62972246da4SFelipe Balbi 
63072246da4SFelipe Balbi 	if (desc->bInterval) {
631a1679af8SThinh Nguyen 		u8 bInterval_m1;
632a1679af8SThinh Nguyen 
633a1679af8SThinh Nguyen 		/*
6343232a3ceSThinh Nguyen 		 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13.
6353232a3ceSThinh Nguyen 		 *
6363232a3ceSThinh Nguyen 		 * NOTE: The programming guide incorrectly stated bInterval_m1
6373232a3ceSThinh Nguyen 		 * must be set to 0 when operating in fullspeed. Internally the
6383232a3ceSThinh Nguyen 		 * controller does not have this limitation. See DWC_usb3x
6393232a3ceSThinh Nguyen 		 * programming guide section 3.2.2.1.
640a1679af8SThinh Nguyen 		 */
641a1679af8SThinh Nguyen 		bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
642a1679af8SThinh Nguyen 
6434b049f55SThinh Nguyen 		if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
6444b049f55SThinh Nguyen 		    dwc->gadget->speed == USB_SPEED_FULL)
6454b049f55SThinh Nguyen 			dep->interval = desc->bInterval;
6464b049f55SThinh Nguyen 		else
64772246da4SFelipe Balbi 			dep->interval = 1 << (desc->bInterval - 1);
6484b049f55SThinh Nguyen 
6494b049f55SThinh Nguyen 		params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
65072246da4SFelipe Balbi 	}
65172246da4SFelipe Balbi 
6522cd4718dSFelipe Balbi 	return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, &params);
65372246da4SFelipe Balbi }
65472246da4SFelipe Balbi 
65572246da4SFelipe Balbi /**
6569f607a30SWesley Cheng  * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
6579f607a30SWesley Cheng  * @dwc: pointer to the DWC3 context
658babfcd94SMauro Carvalho Chehab  * @mult: multiplier to be used when calculating the fifo_size
6599f607a30SWesley Cheng  *
6609f607a30SWesley Cheng  * Calculates the size value based on the equation below:
6619f607a30SWesley Cheng  *
6629f607a30SWesley Cheng  * DWC3 revision 280A and prior:
6639f607a30SWesley Cheng  * fifo_size = mult * (max_packet / mdwidth) + 1;
6649f607a30SWesley Cheng  *
6659f607a30SWesley Cheng  * DWC3 revision 290A and onwards:
6669f607a30SWesley Cheng  * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
6679f607a30SWesley Cheng  *
6689f607a30SWesley Cheng  * The max packet size is set to 1024, as the txfifo requirements mainly apply
6699f607a30SWesley Cheng  * to super speed USB use cases.  However, it is safe to overestimate the fifo
6709f607a30SWesley Cheng  * allocations for other scenarios, i.e. high speed USB.
6719f607a30SWesley Cheng  */
dwc3_gadget_calc_tx_fifo_size(struct dwc3 * dwc,int mult)6729f607a30SWesley Cheng static int dwc3_gadget_calc_tx_fifo_size(struct dwc3 *dwc, int mult)
6739f607a30SWesley Cheng {
6749f607a30SWesley Cheng 	int max_packet = 1024;
6759f607a30SWesley Cheng 	int fifo_size;
6769f607a30SWesley Cheng 	int mdwidth;
6779f607a30SWesley Cheng 
6789f607a30SWesley Cheng 	mdwidth = dwc3_mdwidth(dwc);
6799f607a30SWesley Cheng 
6809f607a30SWesley Cheng 	/* MDWIDTH is represented in bits, we need it in bytes */
6819f607a30SWesley Cheng 	mdwidth >>= 3;
6829f607a30SWesley Cheng 
6839f607a30SWesley Cheng 	if (DWC3_VER_IS_PRIOR(DWC3, 290A))
6849f607a30SWesley Cheng 		fifo_size = mult * (max_packet / mdwidth) + 1;
6859f607a30SWesley Cheng 	else
6869f607a30SWesley Cheng 		fifo_size = mult * ((max_packet + mdwidth) / mdwidth) + 1;
6879f607a30SWesley Cheng 	return fifo_size;
6889f607a30SWesley Cheng }
6899f607a30SWesley Cheng 
6909f607a30SWesley Cheng /**
691106740e9SSelvarasu Ganesan  * dwc3_gadget_calc_ram_depth - calculates the ram depth for txfifo
692106740e9SSelvarasu Ganesan  * @dwc: pointer to the DWC3 context
693106740e9SSelvarasu Ganesan  */
dwc3_gadget_calc_ram_depth(struct dwc3 * dwc)694106740e9SSelvarasu Ganesan static int dwc3_gadget_calc_ram_depth(struct dwc3 *dwc)
695106740e9SSelvarasu Ganesan {
696106740e9SSelvarasu Ganesan 	int ram_depth;
697106740e9SSelvarasu Ganesan 	int fifo_0_start;
698106740e9SSelvarasu Ganesan 	bool is_single_port_ram;
699106740e9SSelvarasu Ganesan 
700106740e9SSelvarasu Ganesan 	/* Check supporting RAM type by HW */
701106740e9SSelvarasu Ganesan 	is_single_port_ram = DWC3_SPRAM_TYPE(dwc->hwparams.hwparams1);
702106740e9SSelvarasu Ganesan 
703106740e9SSelvarasu Ganesan 	/*
704106740e9SSelvarasu Ganesan 	 * If a single port RAM is utilized, then allocate TxFIFOs from
705106740e9SSelvarasu Ganesan 	 * RAM0. otherwise, allocate them from RAM1.
706106740e9SSelvarasu Ganesan 	 */
707106740e9SSelvarasu Ganesan 	ram_depth = is_single_port_ram ? DWC3_RAM0_DEPTH(dwc->hwparams.hwparams6) :
708106740e9SSelvarasu Ganesan 			DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
709106740e9SSelvarasu Ganesan 
710106740e9SSelvarasu Ganesan 	/*
711106740e9SSelvarasu Ganesan 	 * In a single port RAM configuration, the available RAM is shared
712106740e9SSelvarasu Ganesan 	 * between the RX and TX FIFOs. This means that the txfifo can begin
713106740e9SSelvarasu Ganesan 	 * at a non-zero address.
714106740e9SSelvarasu Ganesan 	 */
715106740e9SSelvarasu Ganesan 	if (is_single_port_ram) {
716106740e9SSelvarasu Ganesan 		u32 reg;
717106740e9SSelvarasu Ganesan 
718106740e9SSelvarasu Ganesan 		/* Check if TXFIFOs start at non-zero addr */
719106740e9SSelvarasu Ganesan 		reg = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
720106740e9SSelvarasu Ganesan 		fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(reg);
721106740e9SSelvarasu Ganesan 
722106740e9SSelvarasu Ganesan 		ram_depth -= (fifo_0_start >> 16);
723106740e9SSelvarasu Ganesan 	}
724106740e9SSelvarasu Ganesan 
725106740e9SSelvarasu Ganesan 	return ram_depth;
726106740e9SSelvarasu Ganesan }
727106740e9SSelvarasu Ganesan 
728106740e9SSelvarasu Ganesan /**
7290fee30abSKushagra Verma  * dwc3_gadget_clear_tx_fifos - Clears txfifo allocation
7309f607a30SWesley Cheng  * @dwc: pointer to the DWC3 context
7319f607a30SWesley Cheng  *
7329f607a30SWesley Cheng  * Iterates through all the endpoint registers and clears the previous txfifo
7339f607a30SWesley Cheng  * allocations.
7349f607a30SWesley Cheng  */
dwc3_gadget_clear_tx_fifos(struct dwc3 * dwc)7359f607a30SWesley Cheng void dwc3_gadget_clear_tx_fifos(struct dwc3 *dwc)
7369f607a30SWesley Cheng {
7379f607a30SWesley Cheng 	struct dwc3_ep *dep;
7389f607a30SWesley Cheng 	int fifo_depth;
7399f607a30SWesley Cheng 	int size;
7409f607a30SWesley Cheng 	int num;
7419f607a30SWesley Cheng 
7429f607a30SWesley Cheng 	if (!dwc->do_fifo_resize)
7439f607a30SWesley Cheng 		return;
7449f607a30SWesley Cheng 
7459f607a30SWesley Cheng 	/* Read ep0IN related TXFIFO size */
7469f607a30SWesley Cheng 	dep = dwc->eps[1];
7479f607a30SWesley Cheng 	size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
7489f607a30SWesley Cheng 	if (DWC3_IP_IS(DWC3))
7499f607a30SWesley Cheng 		fifo_depth = DWC3_GTXFIFOSIZ_TXFDEP(size);
7509f607a30SWesley Cheng 	else
7519f607a30SWesley Cheng 		fifo_depth = DWC31_GTXFIFOSIZ_TXFDEP(size);
7529f607a30SWesley Cheng 
7539f607a30SWesley Cheng 	dwc->last_fifo_depth = fifo_depth;
7549f607a30SWesley Cheng 	/* Clear existing TXFIFO for all IN eps except ep0 */
7559f607a30SWesley Cheng 	for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM);
7569f607a30SWesley Cheng 	     num += 2) {
7579f607a30SWesley Cheng 		dep = dwc->eps[num];
7589f607a30SWesley Cheng 		/* Don't change TXFRAMNUM on usb31 version */
7599f607a30SWesley Cheng 		size = DWC3_IP_IS(DWC3) ? 0 :
7609f607a30SWesley Cheng 			dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) &
7619f607a30SWesley Cheng 				   DWC31_GTXFIFOSIZ_TXFRAMNUM;
7629f607a30SWesley Cheng 
7639f607a30SWesley Cheng 		dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size);
764876a75cbSJack Pham 		dep->flags &= ~DWC3_EP_TXFIFO_RESIZED;
7659f607a30SWesley Cheng 	}
7669f607a30SWesley Cheng 	dwc->num_ep_resized = 0;
7679f607a30SWesley Cheng }
7689f607a30SWesley Cheng 
7699f607a30SWesley Cheng /*
7709f607a30SWesley Cheng  * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
7719f607a30SWesley Cheng  * @dwc: pointer to our context structure
7729f607a30SWesley Cheng  *
7739f607a30SWesley Cheng  * This function will a best effort FIFO allocation in order
7749f607a30SWesley Cheng  * to improve FIFO usage and throughput, while still allowing
7759f607a30SWesley Cheng  * us to enable as many endpoints as possible.
7769f607a30SWesley Cheng  *
7779f607a30SWesley Cheng  * Keep in mind that this operation will be highly dependent
7789f607a30SWesley Cheng  * on the configured size for RAM1 - which contains TxFifo -,
7799f607a30SWesley Cheng  * the amount of endpoints enabled on coreConsultant tool, and
7809f607a30SWesley Cheng  * the width of the Master Bus.
7819f607a30SWesley Cheng  *
7829f607a30SWesley Cheng  * In general, FIFO depths are represented with the following equation:
7839f607a30SWesley Cheng  *
7849f607a30SWesley Cheng  * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
7859f607a30SWesley Cheng  *
7869f607a30SWesley Cheng  * In conjunction with dwc3_gadget_check_config(), this resizing logic will
7879f607a30SWesley Cheng  * ensure that all endpoints will have enough internal memory for one max
7889f607a30SWesley Cheng  * packet per endpoint.
7899f607a30SWesley Cheng  */
dwc3_gadget_resize_tx_fifos(struct dwc3_ep * dep)7909f607a30SWesley Cheng static int dwc3_gadget_resize_tx_fifos(struct dwc3_ep *dep)
7919f607a30SWesley Cheng {
7929f607a30SWesley Cheng 	struct dwc3 *dwc = dep->dwc;
7939f607a30SWesley Cheng 	int fifo_0_start;
794106740e9SSelvarasu Ganesan 	int ram_depth;
7959f607a30SWesley Cheng 	int fifo_size;
7969f607a30SWesley Cheng 	int min_depth;
7979f607a30SWesley Cheng 	int num_in_ep;
7989f607a30SWesley Cheng 	int remaining;
7999f607a30SWesley Cheng 	int num_fifos = 1;
8009f607a30SWesley Cheng 	int fifo;
8019f607a30SWesley Cheng 	int tmp;
8029f607a30SWesley Cheng 
8039f607a30SWesley Cheng 	if (!dwc->do_fifo_resize)
8049f607a30SWesley Cheng 		return 0;
8059f607a30SWesley Cheng 
8069f607a30SWesley Cheng 	/* resize IN endpoints except ep0 */
8079f607a30SWesley Cheng 	if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1)
8089f607a30SWesley Cheng 		return 0;
8099f607a30SWesley Cheng 
810876a75cbSJack Pham 	/* bail if already resized */
811876a75cbSJack Pham 	if (dep->flags & DWC3_EP_TXFIFO_RESIZED)
812876a75cbSJack Pham 		return 0;
813876a75cbSJack Pham 
814106740e9SSelvarasu Ganesan 	ram_depth = dwc3_gadget_calc_ram_depth(dwc);
8159f607a30SWesley Cheng 
8169f607a30SWesley Cheng 	if ((dep->endpoint.maxburst > 1 &&
8179f607a30SWesley Cheng 	     usb_endpoint_xfer_bulk(dep->endpoint.desc)) ||
8189f607a30SWesley Cheng 	    usb_endpoint_xfer_isoc(dep->endpoint.desc))
8199f607a30SWesley Cheng 		num_fifos = 3;
8209f607a30SWesley Cheng 
8219f607a30SWesley Cheng 	if (dep->endpoint.maxburst > 6 &&
8226a7c7df9SDan Vacura 	    (usb_endpoint_xfer_bulk(dep->endpoint.desc) ||
8236a7c7df9SDan Vacura 	     usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31))
8249f607a30SWesley Cheng 		num_fifos = dwc->tx_fifo_resize_max_num;
8259f607a30SWesley Cheng 
8269f607a30SWesley Cheng 	/* FIFO size for a single buffer */
8279f607a30SWesley Cheng 	fifo = dwc3_gadget_calc_tx_fifo_size(dwc, 1);
8289f607a30SWesley Cheng 
8299f607a30SWesley Cheng 	/* Calculate the number of remaining EPs w/o any FIFO */
8309f607a30SWesley Cheng 	num_in_ep = dwc->max_cfg_eps;
8319f607a30SWesley Cheng 	num_in_ep -= dwc->num_ep_resized;
8329f607a30SWesley Cheng 
8339f607a30SWesley Cheng 	/* Reserve at least one FIFO for the number of IN EPs */
8349f607a30SWesley Cheng 	min_depth = num_in_ep * (fifo + 1);
835106740e9SSelvarasu Ganesan 	remaining = ram_depth - min_depth - dwc->last_fifo_depth;
8369f607a30SWesley Cheng 	remaining = max_t(int, 0, remaining);
8379f607a30SWesley Cheng 	/*
8389f607a30SWesley Cheng 	 * We've already reserved 1 FIFO per EP, so check what we can fit in
8399f607a30SWesley Cheng 	 * addition to it.  If there is not enough remaining space, allocate
8409f607a30SWesley Cheng 	 * all the remaining space to the EP.
8419f607a30SWesley Cheng 	 */
8429f607a30SWesley Cheng 	fifo_size = (num_fifos - 1) * fifo;
8439f607a30SWesley Cheng 	if (remaining < fifo_size)
8449f607a30SWesley Cheng 		fifo_size = remaining;
8459f607a30SWesley Cheng 
8469f607a30SWesley Cheng 	fifo_size += fifo;
8479f607a30SWesley Cheng 	/* Last increment according to the TX FIFO size equation */
8489f607a30SWesley Cheng 	fifo_size++;
8499f607a30SWesley Cheng 
8509f607a30SWesley Cheng 	/* Check if TXFIFOs start at non-zero addr */
8519f607a30SWesley Cheng 	tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0));
8529f607a30SWesley Cheng 	fifo_0_start = DWC3_GTXFIFOSIZ_TXFSTADDR(tmp);
8539f607a30SWesley Cheng 
8549f607a30SWesley Cheng 	fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16));
8559f607a30SWesley Cheng 	if (DWC3_IP_IS(DWC3))
8569f607a30SWesley Cheng 		dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
8579f607a30SWesley Cheng 	else
8589f607a30SWesley Cheng 		dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
8599f607a30SWesley Cheng 
8609f607a30SWesley Cheng 	/* Check fifo size allocation doesn't exceed available RAM size. */
861106740e9SSelvarasu Ganesan 	if (dwc->last_fifo_depth >= ram_depth) {
8629f607a30SWesley Cheng 		dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n",
863106740e9SSelvarasu Ganesan 			dwc->last_fifo_depth, ram_depth,
8649f607a30SWesley Cheng 			dep->endpoint.name, fifo_size);
8659f607a30SWesley Cheng 		if (DWC3_IP_IS(DWC3))
8669f607a30SWesley Cheng 			fifo_size = DWC3_GTXFIFOSIZ_TXFDEP(fifo_size);
8679f607a30SWesley Cheng 		else
8689f607a30SWesley Cheng 			fifo_size = DWC31_GTXFIFOSIZ_TXFDEP(fifo_size);
8699f607a30SWesley Cheng 
8709f607a30SWesley Cheng 		dwc->last_fifo_depth -= fifo_size;
8719f607a30SWesley Cheng 		return -ENOMEM;
8729f607a30SWesley Cheng 	}
8739f607a30SWesley Cheng 
8749f607a30SWesley Cheng 	dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size);
875876a75cbSJack Pham 	dep->flags |= DWC3_EP_TXFIFO_RESIZED;
8769f607a30SWesley Cheng 	dwc->num_ep_resized++;
8779f607a30SWesley Cheng 
8789f607a30SWesley Cheng 	return 0;
8799f607a30SWesley Cheng }
8809f607a30SWesley Cheng 
8819f607a30SWesley Cheng /**
882bfad65eeSFelipe Balbi  * __dwc3_gadget_ep_enable - initializes a hw endpoint
88372246da4SFelipe Balbi  * @dep: endpoint to be initialized
884a2d23f08SFelipe Balbi  * @action: one of INIT, MODIFY or RESTORE
88572246da4SFelipe Balbi  *
886bfad65eeSFelipe Balbi  * Caller should take care of locking. Execute all necessary commands to
887bfad65eeSFelipe Balbi  * initialize a HW endpoint so it can be used by a gadget driver.
88872246da4SFelipe Balbi  */
__dwc3_gadget_ep_enable(struct dwc3_ep * dep,unsigned int action)889a2d23f08SFelipe Balbi static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
89072246da4SFelipe Balbi {
89139ebb05cSJohn Youn 	const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
89272246da4SFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
89339ebb05cSJohn Youn 
89472246da4SFelipe Balbi 	u32			reg;
895b09e99eeSAndy Shevchenko 	int			ret;
89672246da4SFelipe Balbi 
89772246da4SFelipe Balbi 	if (!(dep->flags & DWC3_EP_ENABLED)) {
8989f607a30SWesley Cheng 		ret = dwc3_gadget_resize_tx_fifos(dep);
8999f607a30SWesley Cheng 		if (ret)
9009f607a30SWesley Cheng 			return ret;
90172246da4SFelipe Balbi 	}
90272246da4SFelipe Balbi 
903b07c2db8SFelipe Balbi 	ret = dwc3_gadget_set_ep_config(dep, action);
90472246da4SFelipe Balbi 	if (ret)
90572246da4SFelipe Balbi 		return ret;
90672246da4SFelipe Balbi 
90765b1f311SThinh Nguyen 	if (!(dep->flags & DWC3_EP_RESOURCE_ALLOCATED)) {
90865b1f311SThinh Nguyen 		ret = dwc3_gadget_set_xfer_resource(dep);
90965b1f311SThinh Nguyen 		if (ret)
91065b1f311SThinh Nguyen 			return ret;
91165b1f311SThinh Nguyen 	}
91265b1f311SThinh Nguyen 
91372246da4SFelipe Balbi 	if (!(dep->flags & DWC3_EP_ENABLED)) {
914f6bafc6aSFelipe Balbi 		struct dwc3_trb	*trb_st_hw;
915f6bafc6aSFelipe Balbi 		struct dwc3_trb	*trb_link;
91672246da4SFelipe Balbi 
91772246da4SFelipe Balbi 		dep->type = usb_endpoint_type(desc);
91872246da4SFelipe Balbi 		dep->flags |= DWC3_EP_ENABLED;
91972246da4SFelipe Balbi 
92072246da4SFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
92172246da4SFelipe Balbi 		reg |= DWC3_DALEPENA_EP(dep->number);
92272246da4SFelipe Balbi 		dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
92372246da4SFelipe Balbi 
9249d778f0cSMayank Rana 		dep->trb_dequeue = 0;
9259d778f0cSMayank Rana 		dep->trb_enqueue = 0;
9269d778f0cSMayank Rana 
92736b68aaeSFelipe Balbi 		if (usb_endpoint_xfer_control(desc))
9282870e501SFelipe Balbi 			goto out;
92972246da4SFelipe Balbi 
9300d25744aSJohn Youn 		/* Initialize the TRB ring */
9310d25744aSJohn Youn 		memset(dep->trb_pool, 0,
9320d25744aSJohn Youn 		       sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
9330d25744aSJohn Youn 
93436b68aaeSFelipe Balbi 		/* Link TRB. The HWO bit is never reset */
93572246da4SFelipe Balbi 		trb_st_hw = &dep->trb_pool[0];
93672246da4SFelipe Balbi 
937f6bafc6aSFelipe Balbi 		trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
938f6bafc6aSFelipe Balbi 		trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
939f6bafc6aSFelipe Balbi 		trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
940f6bafc6aSFelipe Balbi 		trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
941f6bafc6aSFelipe Balbi 		trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
94272246da4SFelipe Balbi 	}
94372246da4SFelipe Balbi 
944a97ea994SFelipe Balbi 	/*
945a97ea994SFelipe Balbi 	 * Issue StartTransfer here with no-op TRB so we can always rely on No
946a97ea994SFelipe Balbi 	 * Response Update Transfer command.
947a97ea994SFelipe Balbi 	 */
948140ca4cfSThinh Nguyen 	if (usb_endpoint_xfer_bulk(desc) ||
94952fcc0beSFelipe Balbi 			usb_endpoint_xfer_int(desc)) {
950a97ea994SFelipe Balbi 		struct dwc3_gadget_ep_cmd_params params;
951a97ea994SFelipe Balbi 		struct dwc3_trb	*trb;
952a97ea994SFelipe Balbi 		dma_addr_t trb_dma;
953a97ea994SFelipe Balbi 		u32 cmd;
954a97ea994SFelipe Balbi 
955a97ea994SFelipe Balbi 		memset(&params, 0, sizeof(params));
956a97ea994SFelipe Balbi 		trb = &dep->trb_pool[0];
957a97ea994SFelipe Balbi 		trb_dma = dwc3_trb_dma_offset(dep, trb);
958a97ea994SFelipe Balbi 
959a97ea994SFelipe Balbi 		params.param0 = upper_32_bits(trb_dma);
960a97ea994SFelipe Balbi 		params.param1 = lower_32_bits(trb_dma);
961a97ea994SFelipe Balbi 
962a97ea994SFelipe Balbi 		cmd = DWC3_DEPCMD_STARTTRANSFER;
963a97ea994SFelipe Balbi 
964a97ea994SFelipe Balbi 		ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
965a97ea994SFelipe Balbi 		if (ret < 0)
966a97ea994SFelipe Balbi 			return ret;
967140ca4cfSThinh Nguyen 
968140ca4cfSThinh Nguyen 		if (dep->stream_capable) {
969140ca4cfSThinh Nguyen 			/*
970140ca4cfSThinh Nguyen 			 * For streams, at start, there maybe a race where the
971140ca4cfSThinh Nguyen 			 * host primes the endpoint before the function driver
972140ca4cfSThinh Nguyen 			 * queues a request to initiate a stream. In that case,
973140ca4cfSThinh Nguyen 			 * the controller will not see the prime to generate the
974140ca4cfSThinh Nguyen 			 * ERDY and start stream. To workaround this, issue a
975140ca4cfSThinh Nguyen 			 * no-op TRB as normal, but end it immediately. As a
976140ca4cfSThinh Nguyen 			 * result, when the function driver queues the request,
977140ca4cfSThinh Nguyen 			 * the next START_TRANSFER command will cause the
978140ca4cfSThinh Nguyen 			 * controller to generate an ERDY to initiate the
979140ca4cfSThinh Nguyen 			 * stream.
980140ca4cfSThinh Nguyen 			 */
981140ca4cfSThinh Nguyen 			dwc3_stop_active_transfer(dep, true, true);
982140ca4cfSThinh Nguyen 
983140ca4cfSThinh Nguyen 			/*
984140ca4cfSThinh Nguyen 			 * All stream eps will reinitiate stream on NoStream
985140ca4cfSThinh Nguyen 			 * rejection until we can determine that the host can
986140ca4cfSThinh Nguyen 			 * prime after the first transfer.
987ddae7979SThinh Nguyen 			 *
988ddae7979SThinh Nguyen 			 * However, if the controller is capable of
989ddae7979SThinh Nguyen 			 * TXF_FLUSH_BYPASS, then IN direction endpoints will
990ddae7979SThinh Nguyen 			 * automatically restart the stream without the driver
991ddae7979SThinh Nguyen 			 * initiation.
992140ca4cfSThinh Nguyen 			 */
993ddae7979SThinh Nguyen 			if (!dep->direction ||
994ddae7979SThinh Nguyen 			    !(dwc->hwparams.hwparams9 &
995ddae7979SThinh Nguyen 			      DWC3_GHWPARAMS9_DEV_TXF_FLUSH_BYPASS))
996140ca4cfSThinh Nguyen 				dep->flags |= DWC3_EP_FORCE_RESTART_STREAM;
997140ca4cfSThinh Nguyen 		}
998a97ea994SFelipe Balbi 	}
999a97ea994SFelipe Balbi 
10002870e501SFelipe Balbi out:
10012870e501SFelipe Balbi 	trace_dwc3_gadget_ep_enable(dep);
10022870e501SFelipe Balbi 
100372246da4SFelipe Balbi 	return 0;
100472246da4SFelipe Balbi }
100572246da4SFelipe Balbi 
dwc3_remove_requests(struct dwc3 * dwc,struct dwc3_ep * dep,int status)10062b2da657SWesley Cheng void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep, int status)
100772246da4SFelipe Balbi {
100872246da4SFelipe Balbi 	struct dwc3_request		*req;
100972246da4SFelipe Balbi 
1010c5353b22SFelipe Balbi 	dwc3_stop_active_transfer(dep, true, false);
1011624407f9SSebastian Andrzej Siewior 
10122b2da657SWesley Cheng 	/* If endxfer is delayed, avoid unmapping requests */
10132b2da657SWesley Cheng 	if (dep->flags & DWC3_EP_DELAY_STOP)
10142b2da657SWesley Cheng 		return;
10152b2da657SWesley Cheng 
101657911504SPratyush Anand 	/* - giveback all requests to gadget driver */
1017aa3342c8SFelipe Balbi 	while (!list_empty(&dep->started_list)) {
1018aa3342c8SFelipe Balbi 		req = next_request(&dep->started_list);
10191591633eSPratyush Anand 
1020b44c0e7fSMichael Grzeschik 		dwc3_gadget_giveback(dep, req, status);
1021ea53b882SFelipe Balbi 	}
1022ea53b882SFelipe Balbi 
1023aa3342c8SFelipe Balbi 	while (!list_empty(&dep->pending_list)) {
1024aa3342c8SFelipe Balbi 		req = next_request(&dep->pending_list);
102572246da4SFelipe Balbi 
1026b44c0e7fSMichael Grzeschik 		dwc3_gadget_giveback(dep, req, status);
102772246da4SFelipe Balbi 	}
1028d8eca64eSFelipe Balbi 
1029d8eca64eSFelipe Balbi 	while (!list_empty(&dep->cancelled_list)) {
1030d8eca64eSFelipe Balbi 		req = next_request(&dep->cancelled_list);
1031d8eca64eSFelipe Balbi 
1032b44c0e7fSMichael Grzeschik 		dwc3_gadget_giveback(dep, req, status);
1033d8eca64eSFelipe Balbi 	}
103472246da4SFelipe Balbi }
103572246da4SFelipe Balbi 
103672246da4SFelipe Balbi /**
1037bfad65eeSFelipe Balbi  * __dwc3_gadget_ep_disable - disables a hw endpoint
103872246da4SFelipe Balbi  * @dep: the endpoint to disable
103972246da4SFelipe Balbi  *
1040bfad65eeSFelipe Balbi  * This function undoes what __dwc3_gadget_ep_enable did and also removes
1041bfad65eeSFelipe Balbi  * requests which are currently being processed by the hardware and those which
1042bfad65eeSFelipe Balbi  * are not yet scheduled.
1043bfad65eeSFelipe Balbi  *
1044624407f9SSebastian Andrzej Siewior  * Caller should take care of locking.
104572246da4SFelipe Balbi  */
__dwc3_gadget_ep_disable(struct dwc3_ep * dep)104672246da4SFelipe Balbi static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
104772246da4SFelipe Balbi {
104872246da4SFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
104972246da4SFelipe Balbi 	u32			reg;
105076bff31cSWesley Cheng 	u32			mask;
105172246da4SFelipe Balbi 
10522870e501SFelipe Balbi 	trace_dwc3_gadget_ep_disable(dep);
10537eaeac5cSFelipe Balbi 
1054687ef981SFelipe Balbi 	/* make sure HW endpoint isn't stalled */
1055687ef981SFelipe Balbi 	if (dep->flags & DWC3_EP_STALL)
10567a608559SFelipe Balbi 		__dwc3_gadget_ep_set_halt(dep, 0, false);
1057687ef981SFelipe Balbi 
105872246da4SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
105972246da4SFelipe Balbi 	reg &= ~DWC3_DALEPENA_EP(dep->number);
106072246da4SFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
106172246da4SFelipe Balbi 
1062ffb9da4aSThinh Nguyen 	dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
1063f09ddcfcSWesley Cheng 
10645aef6297SWesley Cheng 	dep->stream_capable = false;
10655aef6297SWesley Cheng 	dep->type = 0;
106665b1f311SThinh Nguyen 	mask = DWC3_EP_TXFIFO_RESIZED | DWC3_EP_RESOURCE_ALLOCATED;
106776bff31cSWesley Cheng 	/*
106876bff31cSWesley Cheng 	 * dwc3_remove_requests() can exit early if DWC3 EP delayed stop is
106976bff31cSWesley Cheng 	 * set.  Do not clear DEP flags, so that the end transfer command will
107076bff31cSWesley Cheng 	 * be reattempted during the next SETUP stage.
107176bff31cSWesley Cheng 	 */
107276bff31cSWesley Cheng 	if (dep->flags & DWC3_EP_DELAY_STOP)
107376bff31cSWesley Cheng 		mask |= (DWC3_EP_DELAY_STOP | DWC3_EP_TRANSFER_STARTED);
107476bff31cSWesley Cheng 	dep->flags &= mask;
10755aef6297SWesley Cheng 
1076f90f5afdSThinh Nguyen 	/* Clear out the ep descriptors for non-ep0 */
1077f90f5afdSThinh Nguyen 	if (dep->number > 1) {
1078f90f5afdSThinh Nguyen 		dep->endpoint.comp_desc = NULL;
1079f90f5afdSThinh Nguyen 		dep->endpoint.desc = NULL;
1080f90f5afdSThinh Nguyen 	}
1081f90f5afdSThinh Nguyen 
108272246da4SFelipe Balbi 	return 0;
108372246da4SFelipe Balbi }
108472246da4SFelipe Balbi 
108572246da4SFelipe Balbi /* -------------------------------------------------------------------------- */
108672246da4SFelipe Balbi 
dwc3_gadget_ep0_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)108772246da4SFelipe Balbi static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
108872246da4SFelipe Balbi 		const struct usb_endpoint_descriptor *desc)
108972246da4SFelipe Balbi {
109072246da4SFelipe Balbi 	return -EINVAL;
109172246da4SFelipe Balbi }
109272246da4SFelipe Balbi 
dwc3_gadget_ep0_disable(struct usb_ep * ep)109372246da4SFelipe Balbi static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
109472246da4SFelipe Balbi {
109572246da4SFelipe Balbi 	return -EINVAL;
109672246da4SFelipe Balbi }
109772246da4SFelipe Balbi 
109872246da4SFelipe Balbi /* -------------------------------------------------------------------------- */
109972246da4SFelipe Balbi 
dwc3_gadget_ep_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)110072246da4SFelipe Balbi static int dwc3_gadget_ep_enable(struct usb_ep *ep,
110172246da4SFelipe Balbi 		const struct usb_endpoint_descriptor *desc)
110272246da4SFelipe Balbi {
110372246da4SFelipe Balbi 	struct dwc3_ep			*dep;
110472246da4SFelipe Balbi 	struct dwc3			*dwc;
110572246da4SFelipe Balbi 	unsigned long			flags;
110672246da4SFelipe Balbi 	int				ret;
110772246da4SFelipe Balbi 
110872246da4SFelipe Balbi 	if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
110972246da4SFelipe Balbi 		pr_debug("dwc3: invalid parameters\n");
111072246da4SFelipe Balbi 		return -EINVAL;
111172246da4SFelipe Balbi 	}
111272246da4SFelipe Balbi 
111372246da4SFelipe Balbi 	if (!desc->wMaxPacketSize) {
111472246da4SFelipe Balbi 		pr_debug("dwc3: missing wMaxPacketSize\n");
111572246da4SFelipe Balbi 		return -EINVAL;
111672246da4SFelipe Balbi 	}
111772246da4SFelipe Balbi 
111872246da4SFelipe Balbi 	dep = to_dwc3_ep(ep);
111972246da4SFelipe Balbi 	dwc = dep->dwc;
112072246da4SFelipe Balbi 
112195ca961cSFelipe Balbi 	if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
112295ca961cSFelipe Balbi 					"%s is already enabled\n",
112395ca961cSFelipe Balbi 					dep->name))
1124c6f83f38SFelipe Balbi 		return 0;
1125c6f83f38SFelipe Balbi 
112672246da4SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
1127a2d23f08SFelipe Balbi 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
112872246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
112972246da4SFelipe Balbi 
113072246da4SFelipe Balbi 	return ret;
113172246da4SFelipe Balbi }
113272246da4SFelipe Balbi 
dwc3_gadget_ep_disable(struct usb_ep * ep)113372246da4SFelipe Balbi static int dwc3_gadget_ep_disable(struct usb_ep *ep)
113472246da4SFelipe Balbi {
113572246da4SFelipe Balbi 	struct dwc3_ep			*dep;
113672246da4SFelipe Balbi 	struct dwc3			*dwc;
113772246da4SFelipe Balbi 	unsigned long			flags;
113872246da4SFelipe Balbi 	int				ret;
113972246da4SFelipe Balbi 
114072246da4SFelipe Balbi 	if (!ep) {
114172246da4SFelipe Balbi 		pr_debug("dwc3: invalid parameters\n");
114272246da4SFelipe Balbi 		return -EINVAL;
114372246da4SFelipe Balbi 	}
114472246da4SFelipe Balbi 
114572246da4SFelipe Balbi 	dep = to_dwc3_ep(ep);
114672246da4SFelipe Balbi 	dwc = dep->dwc;
114772246da4SFelipe Balbi 
114895ca961cSFelipe Balbi 	if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
114995ca961cSFelipe Balbi 					"%s is already disabled\n",
115095ca961cSFelipe Balbi 					dep->name))
115172246da4SFelipe Balbi 		return 0;
115272246da4SFelipe Balbi 
115372246da4SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
115472246da4SFelipe Balbi 	ret = __dwc3_gadget_ep_disable(dep);
115572246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
115672246da4SFelipe Balbi 
115772246da4SFelipe Balbi 	return ret;
115872246da4SFelipe Balbi }
115972246da4SFelipe Balbi 
dwc3_gadget_ep_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)116072246da4SFelipe Balbi static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
116172246da4SFelipe Balbi 		gfp_t gfp_flags)
116272246da4SFelipe Balbi {
116372246da4SFelipe Balbi 	struct dwc3_request		*req;
116472246da4SFelipe Balbi 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
116572246da4SFelipe Balbi 
116672246da4SFelipe Balbi 	req = kzalloc(sizeof(*req), gfp_flags);
1167734d5a53SJingoo Han 	if (!req)
116872246da4SFelipe Balbi 		return NULL;
116972246da4SFelipe Balbi 
117031a2f5a7SFelipe Balbi 	req->direction	= dep->direction;
117172246da4SFelipe Balbi 	req->epnum	= dep->number;
117272246da4SFelipe Balbi 	req->dep	= dep;
1173a3af5e3aSFelipe Balbi 	req->status	= DWC3_REQUEST_STATUS_UNKNOWN;
117472246da4SFelipe Balbi 
11752c4cbe6eSFelipe Balbi 	trace_dwc3_alloc_request(req);
11762c4cbe6eSFelipe Balbi 
117772246da4SFelipe Balbi 	return &req->request;
117872246da4SFelipe Balbi }
117972246da4SFelipe Balbi 
dwc3_gadget_ep_free_request(struct usb_ep * ep,struct usb_request * request)118072246da4SFelipe Balbi static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
118172246da4SFelipe Balbi 		struct usb_request *request)
118272246da4SFelipe Balbi {
118372246da4SFelipe Balbi 	struct dwc3_request		*req = to_dwc3_request(request);
118472246da4SFelipe Balbi 
11852c4cbe6eSFelipe Balbi 	trace_dwc3_free_request(req);
118672246da4SFelipe Balbi 	kfree(req);
118772246da4SFelipe Balbi }
118872246da4SFelipe Balbi 
118942626919SFelipe Balbi /**
119042626919SFelipe Balbi  * dwc3_ep_prev_trb - returns the previous TRB in the ring
119142626919SFelipe Balbi  * @dep: The endpoint with the TRB ring
119242626919SFelipe Balbi  * @index: The index of the current TRB in the ring
119342626919SFelipe Balbi  *
119442626919SFelipe Balbi  * Returns the TRB prior to the one pointed to by the index. If the
119542626919SFelipe Balbi  * index is 0, we will wrap backwards, skip the link TRB, and return
119642626919SFelipe Balbi  * the one just before that.
119742626919SFelipe Balbi  */
dwc3_ep_prev_trb(struct dwc3_ep * dep,u8 index)119842626919SFelipe Balbi static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
119942626919SFelipe Balbi {
120042626919SFelipe Balbi 	u8 tmp = index;
120142626919SFelipe Balbi 
120242626919SFelipe Balbi 	if (!tmp)
120342626919SFelipe Balbi 		tmp = DWC3_TRB_NUM - 1;
120442626919SFelipe Balbi 
120542626919SFelipe Balbi 	return &dep->trb_pool[tmp - 1];
120642626919SFelipe Balbi }
120742626919SFelipe Balbi 
dwc3_calc_trbs_left(struct dwc3_ep * dep)120842626919SFelipe Balbi static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
120942626919SFelipe Balbi {
121042626919SFelipe Balbi 	u8			trbs_left;
121142626919SFelipe Balbi 
121242626919SFelipe Balbi 	/*
121351f1954aSThinh Nguyen 	 * If the enqueue & dequeue are equal then the TRB ring is either full
121451f1954aSThinh Nguyen 	 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs
121551f1954aSThinh Nguyen 	 * pending to be processed by the driver.
121642626919SFelipe Balbi 	 */
121742626919SFelipe Balbi 	if (dep->trb_enqueue == dep->trb_dequeue) {
1218bb6bf24fSThinh Nguyen 		struct dwc3_request *req;
1219bb6bf24fSThinh Nguyen 
122051f1954aSThinh Nguyen 		/*
1221bb6bf24fSThinh Nguyen 		 * If there is any request remained in the started_list with
1222bb6bf24fSThinh Nguyen 		 * active TRBs at this point, then there is no TRB available.
122351f1954aSThinh Nguyen 		 */
1224bb6bf24fSThinh Nguyen 		req = next_request(&dep->started_list);
1225bb6bf24fSThinh Nguyen 		if (req && req->num_trbs)
122642626919SFelipe Balbi 			return 0;
122742626919SFelipe Balbi 
122842626919SFelipe Balbi 		return DWC3_TRB_NUM - 1;
122942626919SFelipe Balbi 	}
123042626919SFelipe Balbi 
123142626919SFelipe Balbi 	trbs_left = dep->trb_dequeue - dep->trb_enqueue;
123242626919SFelipe Balbi 	trbs_left &= (DWC3_TRB_NUM - 1);
123342626919SFelipe Balbi 
123442626919SFelipe Balbi 	if (dep->trb_dequeue < dep->trb_enqueue)
123542626919SFelipe Balbi 		trbs_left--;
123642626919SFelipe Balbi 
123742626919SFelipe Balbi 	return trbs_left;
123842626919SFelipe Balbi }
12392c78c029SFelipe Balbi 
124023385cecSMichael Grzeschik /**
124123385cecSMichael Grzeschik  * dwc3_prepare_one_trb - setup one TRB from one request
124223385cecSMichael Grzeschik  * @dep: endpoint for which this request is prepared
124323385cecSMichael Grzeschik  * @req: dwc3_request pointer
124423385cecSMichael Grzeschik  * @trb_length: buffer size of the TRB
124523385cecSMichael Grzeschik  * @chain: should this TRB be chained to the next?
124623385cecSMichael Grzeschik  * @node: only for isochronous endpoints. First TRB needs different type.
124723385cecSMichael Grzeschik  * @use_bounce_buffer: set to use bounce buffer
124823385cecSMichael Grzeschik  * @must_interrupt: set to interrupt on TRB completion
124923385cecSMichael Grzeschik  */
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)125023385cecSMichael Grzeschik static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
125123385cecSMichael Grzeschik 		struct dwc3_request *req, unsigned int trb_length,
125223385cecSMichael Grzeschik 		unsigned int chain, unsigned int node, bool use_bounce_buffer,
125323385cecSMichael Grzeschik 		bool must_interrupt)
1254c71fc37cSFelipe Balbi {
125523385cecSMichael Grzeschik 	struct dwc3_trb		*trb;
125623385cecSMichael Grzeschik 	dma_addr_t		dma;
125723385cecSMichael Grzeschik 	unsigned int		stream_id = req->request.stream_id;
125823385cecSMichael Grzeschik 	unsigned int		short_not_ok = req->request.short_not_ok;
125923385cecSMichael Grzeschik 	unsigned int		no_interrupt = req->request.no_interrupt;
126023385cecSMichael Grzeschik 	unsigned int		is_last = req->request.is_last;
12616b9018d4SFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
1262e81a7018SPeter Chen 	struct usb_gadget	*gadget = dwc->gadget;
12636b9018d4SFelipe Balbi 	enum usb_device_speed	speed = gadget->speed;
1264c71fc37cSFelipe Balbi 
126523385cecSMichael Grzeschik 	if (use_bounce_buffer)
126623385cecSMichael Grzeschik 		dma = dep->dwc->bounce_addr;
126723385cecSMichael Grzeschik 	else if (req->request.num_sgs > 0)
126823385cecSMichael Grzeschik 		dma = sg_dma_address(req->start_sg);
126923385cecSMichael Grzeschik 	else
127023385cecSMichael Grzeschik 		dma = req->request.dma;
127123385cecSMichael Grzeschik 
127223385cecSMichael Grzeschik 	trb = &dep->trb_pool[dep->trb_enqueue];
127323385cecSMichael Grzeschik 
127423385cecSMichael Grzeschik 	if (!req->trb) {
127523385cecSMichael Grzeschik 		dwc3_gadget_move_started_request(req);
127623385cecSMichael Grzeschik 		req->trb = trb;
127723385cecSMichael Grzeschik 		req->trb_dma = dwc3_trb_dma_offset(dep, trb);
127823385cecSMichael Grzeschik 	}
127923385cecSMichael Grzeschik 
128023385cecSMichael Grzeschik 	req->num_trbs++;
128123385cecSMichael Grzeschik 
128223385cecSMichael Grzeschik 	trb->size = DWC3_TRB_SIZE_LENGTH(trb_length);
1283f6bafc6aSFelipe Balbi 	trb->bpl = lower_32_bits(dma);
1284f6bafc6aSFelipe Balbi 	trb->bph = upper_32_bits(dma);
1285c71fc37cSFelipe Balbi 
128616e78db7SIdo Shayevitz 	switch (usb_endpoint_type(dep->endpoint.desc)) {
1287c71fc37cSFelipe Balbi 	case USB_ENDPOINT_XFER_CONTROL:
1288f6bafc6aSFelipe Balbi 		trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
1289c71fc37cSFelipe Balbi 		break;
1290c71fc37cSFelipe Balbi 
1291c71fc37cSFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
12926b9018d4SFelipe Balbi 		if (!node) {
1293f6bafc6aSFelipe Balbi 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
12946b9018d4SFelipe Balbi 
129540d829fbSManu Gautam 			/*
129640d829fbSManu Gautam 			 * USB Specification 2.0 Section 5.9.2 states that: "If
129740d829fbSManu Gautam 			 * there is only a single transaction in the microframe,
129840d829fbSManu Gautam 			 * only a DATA0 data packet PID is used.  If there are
129940d829fbSManu Gautam 			 * two transactions per microframe, DATA1 is used for
130040d829fbSManu Gautam 			 * the first transaction data packet and DATA0 is used
130140d829fbSManu Gautam 			 * for the second transaction data packet.  If there are
130240d829fbSManu Gautam 			 * three transactions per microframe, DATA2 is used for
130340d829fbSManu Gautam 			 * the first transaction data packet, DATA1 is used for
130440d829fbSManu Gautam 			 * the second, and DATA0 is used for the third."
130540d829fbSManu Gautam 			 *
130640d829fbSManu Gautam 			 * IOW, we should satisfy the following cases:
130740d829fbSManu Gautam 			 *
130840d829fbSManu Gautam 			 * 1) length <= maxpacket
130940d829fbSManu Gautam 			 *	- DATA0
131040d829fbSManu Gautam 			 *
131140d829fbSManu Gautam 			 * 2) maxpacket < length <= (2 * maxpacket)
131240d829fbSManu Gautam 			 *	- DATA1, DATA0
131340d829fbSManu Gautam 			 *
131440d829fbSManu Gautam 			 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
131540d829fbSManu Gautam 			 *	- DATA2, DATA1, DATA0
131640d829fbSManu Gautam 			 */
13176b9018d4SFelipe Balbi 			if (speed == USB_SPEED_HIGH) {
13186b9018d4SFelipe Balbi 				struct usb_ep *ep = &dep->endpoint;
1319ec5bb87eSManu Gautam 				unsigned int mult = 2;
132040d829fbSManu Gautam 				unsigned int maxp = usb_endpoint_maxp(ep->desc);
132140d829fbSManu Gautam 
13228affe37cSMichael Grzeschik 				if (req->request.length <= (2 * maxp))
132340d829fbSManu Gautam 					mult--;
132440d829fbSManu Gautam 
13258affe37cSMichael Grzeschik 				if (req->request.length <= maxp)
132640d829fbSManu Gautam 					mult--;
132740d829fbSManu Gautam 
132840d829fbSManu Gautam 				trb->size |= DWC3_TRB_SIZE_PCM1(mult);
13296b9018d4SFelipe Balbi 			}
13306b9018d4SFelipe Balbi 		} else {
1331e5ba5ec8SPratyush Anand 			trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
13326b9018d4SFelipe Balbi 		}
1333ca4d44eaSFelipe Balbi 
1334308c316dSThinh Nguyen 		if (!no_interrupt && !chain)
1335ca4d44eaSFelipe Balbi 			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1336c71fc37cSFelipe Balbi 		break;
1337c71fc37cSFelipe Balbi 
1338c71fc37cSFelipe Balbi 	case USB_ENDPOINT_XFER_BULK:
1339c71fc37cSFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
1340f6bafc6aSFelipe Balbi 		trb->ctrl = DWC3_TRBCTL_NORMAL;
1341c71fc37cSFelipe Balbi 		break;
1342c71fc37cSFelipe Balbi 	default:
1343c71fc37cSFelipe Balbi 		/*
1344c71fc37cSFelipe Balbi 		 * This is only possible with faulty memory because we
1345c71fc37cSFelipe Balbi 		 * checked it already :)
1346c71fc37cSFelipe Balbi 		 */
13470a695d4cSFelipe Balbi 		dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
13480a695d4cSFelipe Balbi 				usb_endpoint_type(dep->endpoint.desc));
1349c71fc37cSFelipe Balbi 	}
1350c71fc37cSFelipe Balbi 
1351244add8eSTejas Joglekar 	/*
1352244add8eSTejas Joglekar 	 * Enable Continue on Short Packet
1353244add8eSTejas Joglekar 	 * when endpoint is not a stream capable
1354244add8eSTejas Joglekar 	 */
1355c9508c8cSFelipe Balbi 	if (usb_endpoint_dir_out(dep->endpoint.desc)) {
1356244add8eSTejas Joglekar 		if (!dep->stream_capable)
1357f6bafc6aSFelipe Balbi 			trb->ctrl |= DWC3_TRB_CTRL_CSP;
1358ca4d44eaSFelipe Balbi 
1359e49d3cf4SFelipe Balbi 		if (short_not_ok)
1360c9508c8cSFelipe Balbi 			trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1361c9508c8cSFelipe Balbi 	}
1362c9508c8cSFelipe Balbi 
1363666f3de7SThinh Nguyen 	/* All TRBs setup for MST must set CSP=1 when LST=0 */
1364666f3de7SThinh Nguyen 	if (dep->stream_capable && DWC3_MST_CAPABLE(&dwc->hwparams))
1365666f3de7SThinh Nguyen 		trb->ctrl |= DWC3_TRB_CTRL_CSP;
1366666f3de7SThinh Nguyen 
13678dbbe48cSThinh Nguyen 	if ((!no_interrupt && !chain) || must_interrupt)
1368c9508c8cSFelipe Balbi 		trb->ctrl |= DWC3_TRB_CTRL_IOC;
1369ca4d44eaSFelipe Balbi 
1370e5ba5ec8SPratyush Anand 	if (chain)
1371e5ba5ec8SPratyush Anand 		trb->ctrl |= DWC3_TRB_CTRL_CHN;
1372666f3de7SThinh Nguyen 	else if (dep->stream_capable && is_last &&
1373666f3de7SThinh Nguyen 		 !DWC3_MST_CAPABLE(&dwc->hwparams))
13743eaecd0cSThinh Nguyen 		trb->ctrl |= DWC3_TRB_CTRL_LST;
1375e5ba5ec8SPratyush Anand 
137616e78db7SIdo Shayevitz 	if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1377e49d3cf4SFelipe Balbi 		trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1378f6bafc6aSFelipe Balbi 
1379117b4e96SUdipto Goswami 	/*
1380117b4e96SUdipto Goswami 	 * As per data book 4.2.3.2TRB Control Bit Rules section
1381117b4e96SUdipto Goswami 	 *
1382117b4e96SUdipto Goswami 	 * The controller autonomously checks the HWO field of a TRB to determine if the
1383117b4e96SUdipto Goswami 	 * entire TRB is valid. Therefore, software must ensure that the rest of the TRB
1384117b4e96SUdipto Goswami 	 * is valid before setting the HWO field to '1'. In most systems, this means that
1385117b4e96SUdipto Goswami 	 * software must update the fourth DWORD of a TRB last.
1386117b4e96SUdipto Goswami 	 *
1387117b4e96SUdipto Goswami 	 * However there is a possibility of CPU re-ordering here which can cause
1388117b4e96SUdipto Goswami 	 * controller to observe the HWO bit set prematurely.
1389117b4e96SUdipto Goswami 	 * Add a write memory barrier to prevent CPU re-ordering.
1390117b4e96SUdipto Goswami 	 */
1391117b4e96SUdipto Goswami 	wmb();
1392f6bafc6aSFelipe Balbi 	trb->ctrl |= DWC3_TRB_CTRL_HWO;
13932c4cbe6eSFelipe Balbi 
1394b7a4fbe2SAnurag Kumar Vulisha 	dwc3_ep_inc_enq(dep);
1395b7a4fbe2SAnurag Kumar Vulisha 
13962c4cbe6eSFelipe Balbi 	trace_dwc3_prepare_trb(dep, trb);
1397c71fc37cSFelipe Balbi }
1398c71fc37cSFelipe Balbi 
dwc3_needs_extra_trb(struct dwc3_ep * dep,struct dwc3_request * req)1399f9cc581bSThinh Nguyen static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req)
1400f9cc581bSThinh Nguyen {
1401f9cc581bSThinh Nguyen 	unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1402f9cc581bSThinh Nguyen 	unsigned int rem = req->request.length % maxp;
1403f9cc581bSThinh Nguyen 
1404f9cc581bSThinh Nguyen 	if ((req->request.length && req->request.zero && !rem &&
1405f9cc581bSThinh Nguyen 			!usb_endpoint_xfer_isoc(dep->endpoint.desc)) ||
1406f9cc581bSThinh Nguyen 			(!req->direction && rem))
1407f9cc581bSThinh Nguyen 		return true;
1408f9cc581bSThinh Nguyen 
1409f9cc581bSThinh Nguyen 	return false;
1410e49d3cf4SFelipe Balbi }
1411e49d3cf4SFelipe Balbi 
1412cb1b3997SThinh Nguyen /**
1413cb1b3997SThinh Nguyen  * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1414cb1b3997SThinh Nguyen  * @dep: The endpoint that the request belongs to
1415cb1b3997SThinh Nguyen  * @req: The request to prepare
1416cb1b3997SThinh Nguyen  * @entry_length: The last SG entry size
1417cb1b3997SThinh Nguyen  * @node: Indicates whether this is not the first entry (for isoc only)
1418cb1b3997SThinh Nguyen  *
1419cb1b3997SThinh Nguyen  * Return the number of TRBs prepared.
1420cb1b3997SThinh Nguyen  */
dwc3_prepare_last_sg(struct dwc3_ep * dep,struct dwc3_request * req,unsigned int entry_length,unsigned int node)1421cb1b3997SThinh Nguyen static int dwc3_prepare_last_sg(struct dwc3_ep *dep,
1422cb1b3997SThinh Nguyen 		struct dwc3_request *req, unsigned int entry_length,
1423cb1b3997SThinh Nguyen 		unsigned int node)
1424cb1b3997SThinh Nguyen {
1425cb1b3997SThinh Nguyen 	unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1426cb1b3997SThinh Nguyen 	unsigned int rem = req->request.length % maxp;
1427cb1b3997SThinh Nguyen 	unsigned int num_trbs = 1;
1428cb1b3997SThinh Nguyen 
1429f9cc581bSThinh Nguyen 	if (dwc3_needs_extra_trb(dep, req))
1430cb1b3997SThinh Nguyen 		num_trbs++;
1431cb1b3997SThinh Nguyen 
1432cb1b3997SThinh Nguyen 	if (dwc3_calc_trbs_left(dep) < num_trbs)
1433cb1b3997SThinh Nguyen 		return 0;
1434cb1b3997SThinh Nguyen 
1435cb1b3997SThinh Nguyen 	req->needs_extra_trb = num_trbs > 1;
1436cb1b3997SThinh Nguyen 
1437cb1b3997SThinh Nguyen 	/* Prepare a normal TRB */
1438cb1b3997SThinh Nguyen 	if (req->direction || req->request.length)
1439cb1b3997SThinh Nguyen 		dwc3_prepare_one_trb(dep, req, entry_length,
1440f9cc581bSThinh Nguyen 				req->needs_extra_trb, node, false, false);
1441cb1b3997SThinh Nguyen 
1442cb1b3997SThinh Nguyen 	/* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */
1443cb1b3997SThinh Nguyen 	if ((!req->direction && !req->request.length) || req->needs_extra_trb)
1444cb1b3997SThinh Nguyen 		dwc3_prepare_one_trb(dep, req,
1445cb1b3997SThinh Nguyen 				req->direction ? 0 : maxp - rem,
1446f9cc581bSThinh Nguyen 				false, 1, true, false);
1447cb1b3997SThinh Nguyen 
1448cb1b3997SThinh Nguyen 	return num_trbs;
1449cb1b3997SThinh Nguyen }
1450cb1b3997SThinh Nguyen 
dwc3_prepare_trbs_sg(struct dwc3_ep * dep,struct dwc3_request * req)14517f2958d9SThinh Nguyen static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep,
14527ae7df49SFelipe Balbi 		struct dwc3_request *req)
14535ee85d89SFelipe Balbi {
1454a31e63b6SAnurag Kumar Vulisha 	struct scatterlist *sg = req->start_sg;
14555ee85d89SFelipe Balbi 	struct scatterlist *s;
14565ee85d89SFelipe Balbi 	int		i;
14575d187c04SThinh Nguyen 	unsigned int length = req->request.length;
14581534f6f6SThinh Nguyen 	unsigned int remaining = req->num_pending_sgs;
14591534f6f6SThinh Nguyen 	unsigned int num_queued_sgs = req->request.num_mapped_sgs - remaining;
146013111fcbSThinh Nguyen 	unsigned int num_trbs = req->num_trbs;
1461f9cc581bSThinh Nguyen 	bool needs_extra_trb = dwc3_needs_extra_trb(dep, req);
1462c96e6725SAnurag Kumar Vulisha 
14635d187c04SThinh Nguyen 	/*
14645d187c04SThinh Nguyen 	 * If we resume preparing the request, then get the remaining length of
14655d187c04SThinh Nguyen 	 * the request and resume where we left off.
14665d187c04SThinh Nguyen 	 */
14671534f6f6SThinh Nguyen 	for_each_sg(req->request.sg, s, num_queued_sgs, i)
14685d187c04SThinh Nguyen 		length -= sg_dma_len(s);
14695d187c04SThinh Nguyen 
1470c96e6725SAnurag Kumar Vulisha 	for_each_sg(sg, s, remaining, i) {
14718dbbe48cSThinh Nguyen 		unsigned int num_trbs_left = dwc3_calc_trbs_left(dep);
14725d187c04SThinh Nguyen 		unsigned int trb_length;
1473f9cc581bSThinh Nguyen 		bool must_interrupt = false;
1474cb1b3997SThinh Nguyen 		bool last_sg = false;
14755ee85d89SFelipe Balbi 
14765d187c04SThinh Nguyen 		trb_length = min_t(unsigned int, length, sg_dma_len(s));
14775d187c04SThinh Nguyen 
14785d187c04SThinh Nguyen 		length -= trb_length;
14795d187c04SThinh Nguyen 
1480dad2aff3SPratham Pratap 		/*
1481dad2aff3SPratham Pratap 		 * IOMMU driver is coalescing the list of sgs which shares a
1482dad2aff3SPratham Pratap 		 * page boundary into one and giving it to USB driver. With
1483dad2aff3SPratham Pratap 		 * this the number of sgs mapped is not equal to the number of
1484dad2aff3SPratham Pratap 		 * sgs passed. So mark the chain bit to false if it isthe last
1485dad2aff3SPratham Pratap 		 * mapped sg.
1486dad2aff3SPratham Pratap 		 */
14875d187c04SThinh Nguyen 		if ((i == remaining - 1) || !length)
1488cb1b3997SThinh Nguyen 			last_sg = true;
14895ee85d89SFelipe Balbi 
14908dbbe48cSThinh Nguyen 		if (!num_trbs_left)
149113111fcbSThinh Nguyen 			break;
149213111fcbSThinh Nguyen 
1493cb1b3997SThinh Nguyen 		if (last_sg) {
1494cb1b3997SThinh Nguyen 			if (!dwc3_prepare_last_sg(dep, req, trb_length, i))
1495f9cc581bSThinh Nguyen 				break;
1496c6267a51SFelipe Balbi 		} else {
1497f9cc581bSThinh Nguyen 			/*
1498f9cc581bSThinh Nguyen 			 * Look ahead to check if we have enough TRBs for the
14998dbbe48cSThinh Nguyen 			 * next SG entry. If not, set interrupt on this TRB to
15008dbbe48cSThinh Nguyen 			 * resume preparing the next SG entry when more TRBs are
1501f9cc581bSThinh Nguyen 			 * free.
1502f9cc581bSThinh Nguyen 			 */
15038dbbe48cSThinh Nguyen 			if (num_trbs_left == 1 || (needs_extra_trb &&
15048dbbe48cSThinh Nguyen 					num_trbs_left <= 2 &&
150504914233SThinh Nguyen 					sg_dma_len(sg_next(s)) >= length)) {
150604914233SThinh Nguyen 				struct dwc3_request *r;
150704914233SThinh Nguyen 
150804914233SThinh Nguyen 				/* Check if previous requests already set IOC */
150904914233SThinh Nguyen 				list_for_each_entry(r, &dep->started_list, list) {
151004914233SThinh Nguyen 					if (r != req && !r->request.no_interrupt)
151104914233SThinh Nguyen 						break;
151204914233SThinh Nguyen 
151304914233SThinh Nguyen 					if (r == req)
1514f9cc581bSThinh Nguyen 						must_interrupt = true;
151504914233SThinh Nguyen 				}
151604914233SThinh Nguyen 			}
1517f9cc581bSThinh Nguyen 
1518f9cc581bSThinh Nguyen 			dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false,
1519f9cc581bSThinh Nguyen 					must_interrupt);
1520c6267a51SFelipe Balbi 		}
15215ee85d89SFelipe Balbi 
1522a31e63b6SAnurag Kumar Vulisha 		/*
1523a31e63b6SAnurag Kumar Vulisha 		 * There can be a situation where all sgs in sglist are not
1524a31e63b6SAnurag Kumar Vulisha 		 * queued because of insufficient trb number. To handle this
1525a31e63b6SAnurag Kumar Vulisha 		 * case, update start_sg to next sg to be queued, so that
1526a31e63b6SAnurag Kumar Vulisha 		 * we have free trbs we can continue queuing from where we
1527a31e63b6SAnurag Kumar Vulisha 		 * previously stopped
1528a31e63b6SAnurag Kumar Vulisha 		 */
1529cb1b3997SThinh Nguyen 		if (!last_sg)
1530a31e63b6SAnurag Kumar Vulisha 			req->start_sg = sg_next(s);
1531a31e63b6SAnurag Kumar Vulisha 
1532c96e6725SAnurag Kumar Vulisha 		req->num_queued_sgs++;
153325dda9fcSThinh Nguyen 		req->num_pending_sgs--;
1534c96e6725SAnurag Kumar Vulisha 
15355d187c04SThinh Nguyen 		/*
15365d187c04SThinh Nguyen 		 * The number of pending SG entries may not correspond to the
15375d187c04SThinh Nguyen 		 * number of mapped SG entries. If all the data are queued, then
15385d187c04SThinh Nguyen 		 * don't include unused SG entries.
15395d187c04SThinh Nguyen 		 */
15405d187c04SThinh Nguyen 		if (length == 0) {
154125dda9fcSThinh Nguyen 			req->num_pending_sgs = 0;
15425d187c04SThinh Nguyen 			break;
15435d187c04SThinh Nguyen 		}
15445d187c04SThinh Nguyen 
15458dbbe48cSThinh Nguyen 		if (must_interrupt)
15465ee85d89SFelipe Balbi 			break;
15475ee85d89SFelipe Balbi 	}
154813111fcbSThinh Nguyen 
154930892cbaSThinh Nguyen 	return req->num_trbs - num_trbs;
15505ee85d89SFelipe Balbi }
15515ee85d89SFelipe Balbi 
dwc3_prepare_trbs_linear(struct dwc3_ep * dep,struct dwc3_request * req)15527f2958d9SThinh Nguyen static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep,
15537ae7df49SFelipe Balbi 		struct dwc3_request *req)
15545ee85d89SFelipe Balbi {
1555cb1b3997SThinh Nguyen 	return dwc3_prepare_last_sg(dep, req, req->request.length, 0);
1556c6267a51SFelipe Balbi }
15575ee85d89SFelipe Balbi 
155872246da4SFelipe Balbi /*
155972246da4SFelipe Balbi  * dwc3_prepare_trbs - setup TRBs from requests
156072246da4SFelipe Balbi  * @dep: endpoint for which requests are being prepared
156172246da4SFelipe Balbi  *
15621d046793SPaul Zimmerman  * The function goes through the requests list and sets up TRBs for the
15631d046793SPaul Zimmerman  * transfers. The function returns once there are no more TRBs available or
15641d046793SPaul Zimmerman  * it runs out of requests.
1565490410b2SThinh Nguyen  *
1566490410b2SThinh Nguyen  * Returns the number of TRBs prepared or negative errno.
156772246da4SFelipe Balbi  */
dwc3_prepare_trbs(struct dwc3_ep * dep)1568490410b2SThinh Nguyen static int dwc3_prepare_trbs(struct dwc3_ep *dep)
156972246da4SFelipe Balbi {
157068e823e2SFelipe Balbi 	struct dwc3_request	*req, *n;
1571490410b2SThinh Nguyen 	int			ret = 0;
157272246da4SFelipe Balbi 
157372246da4SFelipe Balbi 	BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
157472246da4SFelipe Balbi 
1575d86c5a67SFelipe Balbi 	/*
1576d86c5a67SFelipe Balbi 	 * We can get in a situation where there's a request in the started list
1577d86c5a67SFelipe Balbi 	 * but there weren't enough TRBs to fully kick it in the first time
1578d86c5a67SFelipe Balbi 	 * around, so it has been waiting for more TRBs to be freed up.
1579d86c5a67SFelipe Balbi 	 *
1580d86c5a67SFelipe Balbi 	 * In that case, we should check if we have a request with pending_sgs
1581d86c5a67SFelipe Balbi 	 * in the started list and prepare TRBs for that request first,
1582d86c5a67SFelipe Balbi 	 * otherwise we will prepare TRBs completely out of order and that will
1583d86c5a67SFelipe Balbi 	 * break things.
1584d86c5a67SFelipe Balbi 	 */
1585d86c5a67SFelipe Balbi 	list_for_each_entry(req, &dep->started_list, list) {
1586490410b2SThinh Nguyen 		if (req->num_pending_sgs > 0) {
15877f2958d9SThinh Nguyen 			ret = dwc3_prepare_trbs_sg(dep, req);
1588346a15cdSThinh Nguyen 			if (!ret || req->num_pending_sgs)
1589490410b2SThinh Nguyen 				return ret;
1590490410b2SThinh Nguyen 		}
1591d86c5a67SFelipe Balbi 
1592d86c5a67SFelipe Balbi 		if (!dwc3_calc_trbs_left(dep))
1593490410b2SThinh Nguyen 			return ret;
159463c7bb29SThinh Nguyen 
159563c7bb29SThinh Nguyen 		/*
159663c7bb29SThinh Nguyen 		 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
159763c7bb29SThinh Nguyen 		 * burst capability may try to read and use TRBs beyond the
159863c7bb29SThinh Nguyen 		 * active transfer instead of stopping.
159963c7bb29SThinh Nguyen 		 */
1600666f3de7SThinh Nguyen 		if (dep->stream_capable && req->request.is_last &&
1601666f3de7SThinh Nguyen 		    !DWC3_MST_CAPABLE(&dep->dwc->hwparams))
1602490410b2SThinh Nguyen 			return ret;
1603d86c5a67SFelipe Balbi 	}
1604d86c5a67SFelipe Balbi 
1605aa3342c8SFelipe Balbi 	list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1606cdb55b39SFelipe Balbi 		struct dwc3	*dwc = dep->dwc;
1607cdb55b39SFelipe Balbi 
1608cdb55b39SFelipe Balbi 		ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1609cdb55b39SFelipe Balbi 						    dep->direction);
1610cdb55b39SFelipe Balbi 		if (ret)
1611490410b2SThinh Nguyen 			return ret;
1612cdb55b39SFelipe Balbi 
1613cdb55b39SFelipe Balbi 		req->sg			= req->request.sg;
1614a31e63b6SAnurag Kumar Vulisha 		req->start_sg		= req->sg;
1615c96e6725SAnurag Kumar Vulisha 		req->num_queued_sgs	= 0;
1616cdb55b39SFelipe Balbi 		req->num_pending_sgs	= req->request.num_mapped_sgs;
1617cdb55b39SFelipe Balbi 
1618346a15cdSThinh Nguyen 		if (req->num_pending_sgs > 0) {
16197f2958d9SThinh Nguyen 			ret = dwc3_prepare_trbs_sg(dep, req);
1620346a15cdSThinh Nguyen 			if (req->num_pending_sgs)
1621346a15cdSThinh Nguyen 				return ret;
1622346a15cdSThinh Nguyen 		} else {
16237f2958d9SThinh Nguyen 			ret = dwc3_prepare_trbs_linear(dep, req);
1624346a15cdSThinh Nguyen 		}
162572246da4SFelipe Balbi 
1626490410b2SThinh Nguyen 		if (!ret || !dwc3_calc_trbs_left(dep))
1627490410b2SThinh Nguyen 			return ret;
1628aefe3d23SThinh Nguyen 
1629aefe3d23SThinh Nguyen 		/*
1630aefe3d23SThinh Nguyen 		 * Don't prepare beyond a transfer. In DWC_usb32, its transfer
1631aefe3d23SThinh Nguyen 		 * burst capability may try to read and use TRBs beyond the
1632aefe3d23SThinh Nguyen 		 * active transfer instead of stopping.
1633aefe3d23SThinh Nguyen 		 */
1634666f3de7SThinh Nguyen 		if (dep->stream_capable && req->request.is_last &&
1635666f3de7SThinh Nguyen 		    !DWC3_MST_CAPABLE(&dwc->hwparams))
1636490410b2SThinh Nguyen 			return ret;
163772246da4SFelipe Balbi 	}
1638490410b2SThinh Nguyen 
1639490410b2SThinh Nguyen 	return ret;
164072246da4SFelipe Balbi }
164172246da4SFelipe Balbi 
16428d99087cSThinh Nguyen static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep);
16438d99087cSThinh Nguyen 
__dwc3_gadget_kick_transfer(struct dwc3_ep * dep)16447fdca766SFelipe Balbi static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
164572246da4SFelipe Balbi {
164672246da4SFelipe Balbi 	struct dwc3_gadget_ep_cmd_params params;
164772246da4SFelipe Balbi 	struct dwc3_request		*req;
16484fae2e3eSFelipe Balbi 	int				starting;
164972246da4SFelipe Balbi 	int				ret;
165072246da4SFelipe Balbi 	u32				cmd;
165172246da4SFelipe Balbi 
1652d72ecc08SThinh Nguyen 	/*
1653d72ecc08SThinh Nguyen 	 * Note that it's normal to have no new TRBs prepared (i.e. ret == 0).
1654d72ecc08SThinh Nguyen 	 * This happens when we need to stop and restart a transfer such as in
1655d72ecc08SThinh Nguyen 	 * the case of reinitiating a stream or retrying an isoc transfer.
1656d72ecc08SThinh Nguyen 	 */
1657490410b2SThinh Nguyen 	ret = dwc3_prepare_trbs(dep);
1658d72ecc08SThinh Nguyen 	if (ret < 0)
1659490410b2SThinh Nguyen 		return ret;
1660ccb94ebfSFelipe Balbi 
16611912cbc6SFelipe Balbi 	starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
166272246da4SFelipe Balbi 
16632338484dSThinh Nguyen 	/*
16642338484dSThinh Nguyen 	 * If there's no new TRB prepared and we don't need to restart a
16652338484dSThinh Nguyen 	 * transfer, there's no need to update the transfer.
16662338484dSThinh Nguyen 	 */
16672338484dSThinh Nguyen 	if (!ret && !starting)
16682338484dSThinh Nguyen 		return ret;
16692338484dSThinh Nguyen 
1670aa3342c8SFelipe Balbi 	req = next_request(&dep->started_list);
167172246da4SFelipe Balbi 	if (!req) {
167272246da4SFelipe Balbi 		dep->flags |= DWC3_EP_PENDING_REQUEST;
167372246da4SFelipe Balbi 		return 0;
167472246da4SFelipe Balbi 	}
167572246da4SFelipe Balbi 
167672246da4SFelipe Balbi 	memset(&params, 0, sizeof(params));
16771877d6c9SPratyush Anand 
16784fae2e3eSFelipe Balbi 	if (starting) {
1679dc1c70a7SFelipe Balbi 		params.param0 = upper_32_bits(req->trb_dma);
1680dc1c70a7SFelipe Balbi 		params.param1 = lower_32_bits(req->trb_dma);
16817fdca766SFelipe Balbi 		cmd = DWC3_DEPCMD_STARTTRANSFER;
16827fdca766SFelipe Balbi 
1683a7351807SAnurag Kumar Vulisha 		if (dep->stream_capable)
1684a7351807SAnurag Kumar Vulisha 			cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id);
1685a7351807SAnurag Kumar Vulisha 
16867fdca766SFelipe Balbi 		if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
16877fdca766SFelipe Balbi 			cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
16881877d6c9SPratyush Anand 	} else {
1689b6b1c6dbSFelipe Balbi 		cmd = DWC3_DEPCMD_UPDATETRANSFER |
1690b6b1c6dbSFelipe Balbi 			DWC3_DEPCMD_PARAM(dep->resource_index);
16911877d6c9SPratyush Anand 	}
169272246da4SFelipe Balbi 
16932cd4718dSFelipe Balbi 	ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
169472246da4SFelipe Balbi 	if (ret < 0) {
16958d99087cSThinh Nguyen 		struct dwc3_request *tmp;
16968d99087cSThinh Nguyen 
16978d99087cSThinh Nguyen 		if (ret == -EAGAIN)
16988d99087cSThinh Nguyen 			return ret;
16998d99087cSThinh Nguyen 
17008d99087cSThinh Nguyen 		dwc3_stop_active_transfer(dep, true, true);
17018d99087cSThinh Nguyen 
17028d99087cSThinh Nguyen 		list_for_each_entry_safe(req, tmp, &dep->started_list, list)
170304dd6e76SRay Chi 			dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_DEQUEUED);
17048d99087cSThinh Nguyen 
17058d99087cSThinh Nguyen 		/* If ep isn't started, then there's no end transfer pending */
17068d99087cSThinh Nguyen 		if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
17078d99087cSThinh Nguyen 			dwc3_gadget_ep_cleanup_cancelled_requests(dep);
17088d99087cSThinh Nguyen 
170972246da4SFelipe Balbi 		return ret;
171072246da4SFelipe Balbi 	}
171172246da4SFelipe Balbi 
1712666f3de7SThinh Nguyen 	if (dep->stream_capable && req->request.is_last &&
1713666f3de7SThinh Nguyen 	    !DWC3_MST_CAPABLE(&dep->dwc->hwparams))
1714e0d19563SThinh Nguyen 		dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE;
1715e0d19563SThinh Nguyen 
171672246da4SFelipe Balbi 	return 0;
171772246da4SFelipe Balbi }
171872246da4SFelipe Balbi 
__dwc3_gadget_get_frame(struct dwc3 * dwc)17196cb2e4e3SFelipe Balbi static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
17206cb2e4e3SFelipe Balbi {
17216cb2e4e3SFelipe Balbi 	u32			reg;
17226cb2e4e3SFelipe Balbi 
17236cb2e4e3SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
17246cb2e4e3SFelipe Balbi 	return DWC3_DSTS_SOFFN(reg);
17256cb2e4e3SFelipe Balbi }
17266cb2e4e3SFelipe Balbi 
1727d92021f6SThinh Nguyen /**
1728e192cc7bSMichael Grzeschik  * __dwc3_stop_active_transfer - stop the current active transfer
1729e192cc7bSMichael Grzeschik  * @dep: isoc endpoint
1730e192cc7bSMichael Grzeschik  * @force: set forcerm bit in the command
1731e192cc7bSMichael Grzeschik  * @interrupt: command complete interrupt after End Transfer command
1732e192cc7bSMichael Grzeschik  *
1733e192cc7bSMichael Grzeschik  * When setting force, the ForceRM bit will be set. In that case
1734e192cc7bSMichael Grzeschik  * the controller won't update the TRB progress on command
1735e192cc7bSMichael Grzeschik  * completion. It also won't clear the HWO bit in the TRB.
1736e192cc7bSMichael Grzeschik  * The command will also not complete immediately in that case.
1737e192cc7bSMichael Grzeschik  */
__dwc3_stop_active_transfer(struct dwc3_ep * dep,bool force,bool interrupt)1738e192cc7bSMichael Grzeschik static int __dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, bool interrupt)
1739e192cc7bSMichael Grzeschik {
1740e192cc7bSMichael Grzeschik 	struct dwc3_gadget_ep_cmd_params params;
1741e192cc7bSMichael Grzeschik 	u32 cmd;
1742e192cc7bSMichael Grzeschik 	int ret;
1743e192cc7bSMichael Grzeschik 
1744e192cc7bSMichael Grzeschik 	cmd = DWC3_DEPCMD_ENDTRANSFER;
1745e192cc7bSMichael Grzeschik 	cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
1746e192cc7bSMichael Grzeschik 	cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0;
1747e192cc7bSMichael Grzeschik 	cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1748e192cc7bSMichael Grzeschik 	memset(&params, 0, sizeof(params));
1749e192cc7bSMichael Grzeschik 	ret = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
17504db0fbb6SThinh Nguyen 	/*
17514db0fbb6SThinh Nguyen 	 * If the End Transfer command was timed out while the device is
17524db0fbb6SThinh Nguyen 	 * not in SETUP phase, it's possible that an incoming Setup packet
17534db0fbb6SThinh Nguyen 	 * may prevent the command's completion. Let's retry when the
17544db0fbb6SThinh Nguyen 	 * ep0state returns to EP0_SETUP_PHASE.
17554db0fbb6SThinh Nguyen 	 */
17564db0fbb6SThinh Nguyen 	if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) {
17574db0fbb6SThinh Nguyen 		dep->flags |= DWC3_EP_DELAY_STOP;
17584db0fbb6SThinh Nguyen 		return 0;
17594db0fbb6SThinh Nguyen 	}
1760e192cc7bSMichael Grzeschik 	WARN_ON_ONCE(ret);
1761e192cc7bSMichael Grzeschik 	dep->resource_index = 0;
1762e192cc7bSMichael Grzeschik 
1763c3d3501cSPrashanth K 	if (!interrupt)
1764e192cc7bSMichael Grzeschik 		dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
1765c3d3501cSPrashanth K 	else if (!ret)
1766e192cc7bSMichael Grzeschik 		dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
1767e192cc7bSMichael Grzeschik 
1768c4e3ef56SThinh Nguyen 	dep->flags &= ~DWC3_EP_DELAY_STOP;
1769e192cc7bSMichael Grzeschik 	return ret;
1770e192cc7bSMichael Grzeschik }
1771e192cc7bSMichael Grzeschik 
1772e192cc7bSMichael Grzeschik /**
1773d92021f6SThinh Nguyen  * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1774d92021f6SThinh Nguyen  * @dep: isoc endpoint
1775d92021f6SThinh Nguyen  *
1776d92021f6SThinh Nguyen  * This function tests for the correct combination of BIT[15:14] from the 16-bit
1777d92021f6SThinh Nguyen  * microframe number reported by the XferNotReady event for the future frame
1778d92021f6SThinh Nguyen  * number to start the isoc transfer.
1779d92021f6SThinh Nguyen  *
1780d92021f6SThinh Nguyen  * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1781d92021f6SThinh Nguyen  * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1782d92021f6SThinh Nguyen  * XferNotReady event are invalid. The driver uses this number to schedule the
1783d92021f6SThinh Nguyen  * isochronous transfer and passes it to the START TRANSFER command. Because
1784d92021f6SThinh Nguyen  * this number is invalid, the command may fail. If BIT[15:14] matches the
1785d92021f6SThinh Nguyen  * internal 16-bit microframe, the START TRANSFER command will pass and the
1786d92021f6SThinh Nguyen  * transfer will start at the scheduled time, if it is off by 1, the command
1787d92021f6SThinh Nguyen  * will still pass, but the transfer will start 2 seconds in the future. For all
1788d92021f6SThinh Nguyen  * other conditions, the START TRANSFER command will fail with bus-expiry.
1789d92021f6SThinh Nguyen  *
1790d92021f6SThinh Nguyen  * In order to workaround this issue, we can test for the correct combination of
1791d92021f6SThinh Nguyen  * BIT[15:14] by sending START TRANSFER commands with different values of
1792d92021f6SThinh Nguyen  * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart
1793d92021f6SThinh Nguyen  * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1794d92021f6SThinh Nguyen  * As the result, within the 4 possible combinations for BIT[15:14], there will
1795d92021f6SThinh Nguyen  * be 2 successful and 2 failure START COMMAND status. One of the 2 successful
1796d92021f6SThinh Nguyen  * command status will result in a 2-second delay start. The smaller BIT[15:14]
1797d92021f6SThinh Nguyen  * value is the correct combination.
1798d92021f6SThinh Nguyen  *
1799d92021f6SThinh Nguyen  * Since there are only 4 outcomes and the results are ordered, we can simply
1800d92021f6SThinh Nguyen  * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to
1801d92021f6SThinh Nguyen  * deduce the smaller successful combination.
1802d92021f6SThinh Nguyen  *
1803d92021f6SThinh Nguyen  * Let test0 = test status for combination 'b00 and test1 = test status for 'b01
1804d92021f6SThinh Nguyen  * of BIT[15:14]. The correct combination is as follow:
1805d92021f6SThinh Nguyen  *
1806d92021f6SThinh Nguyen  * if test0 fails and test1 passes, BIT[15:14] is 'b01
1807d92021f6SThinh Nguyen  * if test0 fails and test1 fails, BIT[15:14] is 'b10
1808d92021f6SThinh Nguyen  * if test0 passes and test1 fails, BIT[15:14] is 'b11
1809d92021f6SThinh Nguyen  * if test0 passes and test1 passes, BIT[15:14] is 'b00
1810d92021f6SThinh Nguyen  *
1811d92021f6SThinh Nguyen  * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN
1812d92021f6SThinh Nguyen  * endpoints.
1813d92021f6SThinh Nguyen  */
dwc3_gadget_start_isoc_quirk(struct dwc3_ep * dep)181425abad6aSFelipe Balbi static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep)
1815d6d6ec7bSPratyush Anand {
1816d92021f6SThinh Nguyen 	int cmd_status = 0;
1817d92021f6SThinh Nguyen 	bool test0;
1818d92021f6SThinh Nguyen 	bool test1;
1819d92021f6SThinh Nguyen 
1820d92021f6SThinh Nguyen 	while (dep->combo_num < 2) {
1821d92021f6SThinh Nguyen 		struct dwc3_gadget_ep_cmd_params params;
1822d92021f6SThinh Nguyen 		u32 test_frame_number;
1823d92021f6SThinh Nguyen 		u32 cmd;
1824d92021f6SThinh Nguyen 
1825d92021f6SThinh Nguyen 		/*
1826d92021f6SThinh Nguyen 		 * Check if we can start isoc transfer on the next interval or
1827d92021f6SThinh Nguyen 		 * 4 uframes in the future with BIT[15:14] as dep->combo_num
1828d92021f6SThinh Nguyen 		 */
1829ca143785SMichael Grzeschik 		test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK;
1830d92021f6SThinh Nguyen 		test_frame_number |= dep->combo_num << 14;
1831d92021f6SThinh Nguyen 		test_frame_number += max_t(u32, 4, dep->interval);
1832d92021f6SThinh Nguyen 
1833d92021f6SThinh Nguyen 		params.param0 = upper_32_bits(dep->dwc->bounce_addr);
1834d92021f6SThinh Nguyen 		params.param1 = lower_32_bits(dep->dwc->bounce_addr);
1835d92021f6SThinh Nguyen 
1836d92021f6SThinh Nguyen 		cmd = DWC3_DEPCMD_STARTTRANSFER;
1837d92021f6SThinh Nguyen 		cmd |= DWC3_DEPCMD_PARAM(test_frame_number);
1838d92021f6SThinh Nguyen 		cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, &params);
1839d92021f6SThinh Nguyen 
1840d92021f6SThinh Nguyen 		/* Redo if some other failure beside bus-expiry is received */
1841d92021f6SThinh Nguyen 		if (cmd_status && cmd_status != -EAGAIN) {
1842d92021f6SThinh Nguyen 			dep->start_cmd_status = 0;
1843d92021f6SThinh Nguyen 			dep->combo_num = 0;
184425abad6aSFelipe Balbi 			return 0;
1845d6d6ec7bSPratyush Anand 		}
1846d6d6ec7bSPratyush Anand 
1847d92021f6SThinh Nguyen 		/* Store the first test status */
1848d92021f6SThinh Nguyen 		if (dep->combo_num == 0)
1849d92021f6SThinh Nguyen 			dep->start_cmd_status = cmd_status;
1850d92021f6SThinh Nguyen 
1851d92021f6SThinh Nguyen 		dep->combo_num++;
1852d92021f6SThinh Nguyen 
1853d92021f6SThinh Nguyen 		/*
1854d92021f6SThinh Nguyen 		 * End the transfer if the START_TRANSFER command is successful
1855d92021f6SThinh Nguyen 		 * to wait for the next XferNotReady to test the command again
1856d92021f6SThinh Nguyen 		 */
1857d92021f6SThinh Nguyen 		if (cmd_status == 0) {
1858c5353b22SFelipe Balbi 			dwc3_stop_active_transfer(dep, true, true);
185925abad6aSFelipe Balbi 			return 0;
1860d92021f6SThinh Nguyen 		}
1861d92021f6SThinh Nguyen 	}
1862d92021f6SThinh Nguyen 
1863d92021f6SThinh Nguyen 	/* test0 and test1 are both completed at this point */
1864d92021f6SThinh Nguyen 	test0 = (dep->start_cmd_status == 0);
1865d92021f6SThinh Nguyen 	test1 = (cmd_status == 0);
1866d92021f6SThinh Nguyen 
1867d92021f6SThinh Nguyen 	if (!test0 && test1)
1868d92021f6SThinh Nguyen 		dep->combo_num = 1;
1869d92021f6SThinh Nguyen 	else if (!test0 && !test1)
1870d92021f6SThinh Nguyen 		dep->combo_num = 2;
1871d92021f6SThinh Nguyen 	else if (test0 && !test1)
1872d92021f6SThinh Nguyen 		dep->combo_num = 3;
1873d92021f6SThinh Nguyen 	else if (test0 && test1)
1874d92021f6SThinh Nguyen 		dep->combo_num = 0;
1875d92021f6SThinh Nguyen 
1876ca143785SMichael Grzeschik 	dep->frame_number &= DWC3_FRNUMBER_MASK;
1877d92021f6SThinh Nguyen 	dep->frame_number |= dep->combo_num << 14;
1878d92021f6SThinh Nguyen 	dep->frame_number += max_t(u32, 4, dep->interval);
1879d92021f6SThinh Nguyen 
1880d92021f6SThinh Nguyen 	/* Reinitialize test variables */
1881d92021f6SThinh Nguyen 	dep->start_cmd_status = 0;
1882d92021f6SThinh Nguyen 	dep->combo_num = 0;
1883d92021f6SThinh Nguyen 
188425abad6aSFelipe Balbi 	return __dwc3_gadget_kick_transfer(dep);
1885d92021f6SThinh Nguyen }
1886d92021f6SThinh Nguyen 
__dwc3_gadget_start_isoc(struct dwc3_ep * dep)188725abad6aSFelipe Balbi static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1888d6d6ec7bSPratyush Anand {
1889c5a7092fSMichael Olbrich 	const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
1890d92021f6SThinh Nguyen 	struct dwc3 *dwc = dep->dwc;
1891d5370106SFelipe Balbi 	int ret;
1892d5370106SFelipe Balbi 	int i;
1893d92021f6SThinh Nguyen 
189436f05d36SThinh Nguyen 	if (list_empty(&dep->pending_list) &&
189536f05d36SThinh Nguyen 	    list_empty(&dep->started_list)) {
1896d6d6ec7bSPratyush Anand 		dep->flags |= DWC3_EP_PENDING_REQUEST;
189725abad6aSFelipe Balbi 		return -EAGAIN;
1898d6d6ec7bSPratyush Anand 	}
1899d6d6ec7bSPratyush Anand 
19009af21dd6SThinh Nguyen 	if (!dwc->dis_start_transfer_quirk &&
19019af21dd6SThinh Nguyen 	    (DWC3_VER_IS_PRIOR(DWC31, 170A) ||
19029af21dd6SThinh Nguyen 	     DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) {
1903e81a7018SPeter Chen 		if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction)
190425abad6aSFelipe Balbi 			return dwc3_gadget_start_isoc_quirk(dep);
1905d92021f6SThinh Nguyen 	}
1906d92021f6SThinh Nguyen 
1907c5a7092fSMichael Olbrich 	if (desc->bInterval <= 14 &&
1908e81a7018SPeter Chen 	    dwc->gadget->speed >= USB_SPEED_HIGH) {
1909c5a7092fSMichael Olbrich 		u32 frame = __dwc3_gadget_get_frame(dwc);
1910c5a7092fSMichael Olbrich 		bool rollover = frame <
1911c5a7092fSMichael Olbrich 				(dep->frame_number & DWC3_FRNUMBER_MASK);
1912c5a7092fSMichael Olbrich 
1913c5a7092fSMichael Olbrich 		/*
1914c5a7092fSMichael Olbrich 		 * frame_number is set from XferNotReady and may be already
1915c5a7092fSMichael Olbrich 		 * out of date. DSTS only provides the lower 14 bit of the
1916c5a7092fSMichael Olbrich 		 * current frame number. So add the upper two bits of
1917c5a7092fSMichael Olbrich 		 * frame_number and handle a possible rollover.
1918c5a7092fSMichael Olbrich 		 * This will provide the correct frame_number unless more than
1919c5a7092fSMichael Olbrich 		 * rollover has happened since XferNotReady.
1920c5a7092fSMichael Olbrich 		 */
1921c5a7092fSMichael Olbrich 
1922c5a7092fSMichael Olbrich 		dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) |
1923c5a7092fSMichael Olbrich 				     frame;
1924c5a7092fSMichael Olbrich 		if (rollover)
1925c5a7092fSMichael Olbrich 			dep->frame_number += BIT(14);
1926c5a7092fSMichael Olbrich 	}
1927c5a7092fSMichael Olbrich 
1928d5370106SFelipe Balbi 	for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) {
1929aa6812beSThinh Nguyen 		int future_interval = i + 1;
1930aa6812beSThinh Nguyen 
1931aa6812beSThinh Nguyen 		/* Give the controller at least 500us to schedule transfers */
1932aa6812beSThinh Nguyen 		if (desc->bInterval < 3)
1933aa6812beSThinh Nguyen 			future_interval += 3 - desc->bInterval;
1934aa6812beSThinh Nguyen 
1935aa6812beSThinh Nguyen 		dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval);
1936d5370106SFelipe Balbi 
1937d5370106SFelipe Balbi 		ret = __dwc3_gadget_kick_transfer(dep);
1938d5370106SFelipe Balbi 		if (ret != -EAGAIN)
1939d5370106SFelipe Balbi 			break;
1940d5370106SFelipe Balbi 	}
1941d5370106SFelipe Balbi 
194236f05d36SThinh Nguyen 	/*
194336f05d36SThinh Nguyen 	 * After a number of unsuccessful start attempts due to bus-expiry
194436f05d36SThinh Nguyen 	 * status, issue END_TRANSFER command and retry on the next XferNotReady
194536f05d36SThinh Nguyen 	 * event.
194636f05d36SThinh Nguyen 	 */
1947e192cc7bSMichael Grzeschik 	if (ret == -EAGAIN)
1948e192cc7bSMichael Grzeschik 		ret = __dwc3_stop_active_transfer(dep, false, true);
194936f05d36SThinh Nguyen 
1950d5370106SFelipe Balbi 	return ret;
1951d6d6ec7bSPratyush Anand }
1952d6d6ec7bSPratyush Anand 
__dwc3_gadget_ep_queue(struct dwc3_ep * dep,struct dwc3_request * req)195372246da4SFelipe Balbi static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
195472246da4SFelipe Balbi {
19550fc9a1beSFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
19560fc9a1beSFelipe Balbi 
1957f09ddcfcSWesley Cheng 	if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) {
1958b851f7c7SWesley Cheng 		dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n",
19595eb30cedSFelipe Balbi 				dep->name);
1960bb423984SFelipe Balbi 		return -ESHUTDOWN;
1961bb423984SFelipe Balbi 	}
1962bb423984SFelipe Balbi 
196304fb365cSFelipe Balbi 	if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
196404fb365cSFelipe Balbi 				&req->request, req->dep->name))
1965bb423984SFelipe Balbi 		return -EINVAL;
1966bb423984SFelipe Balbi 
1967b2b6d601SFelipe Balbi 	if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED,
1968b2b6d601SFelipe Balbi 				"%s: request %pK already in flight\n",
1969b2b6d601SFelipe Balbi 				dep->name, &req->request))
1970b2b6d601SFelipe Balbi 		return -EINVAL;
1971b2b6d601SFelipe Balbi 
1972fc8bb91bSFelipe Balbi 	pm_runtime_get(dwc->dev);
1973fc8bb91bSFelipe Balbi 
197472246da4SFelipe Balbi 	req->request.actual	= 0;
197572246da4SFelipe Balbi 	req->request.status	= -EINPROGRESS;
197672246da4SFelipe Balbi 
1977fe84f522SFelipe Balbi 	trace_dwc3_ep_queue(req);
1978fe84f522SFelipe Balbi 
1979aa3342c8SFelipe Balbi 	list_add_tail(&req->list, &dep->pending_list);
1980a3af5e3aSFelipe Balbi 	req->status = DWC3_REQUEST_STATUS_QUEUED;
198172246da4SFelipe Balbi 
1982e0d19563SThinh Nguyen 	if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)
1983e0d19563SThinh Nguyen 		return 0;
1984e0d19563SThinh Nguyen 
1985c503672aSThinh Nguyen 	/*
1986c503672aSThinh Nguyen 	 * Start the transfer only after the END_TRANSFER is completed
1987c503672aSThinh Nguyen 	 * and endpoint STALL is cleared.
1988c503672aSThinh Nguyen 	 */
1989c503672aSThinh Nguyen 	if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
1990c503672aSThinh Nguyen 	    (dep->flags & DWC3_EP_WEDGE) ||
1991e4cf6580SThinh Nguyen 	    (dep->flags & DWC3_EP_DELAY_STOP) ||
1992c503672aSThinh Nguyen 	    (dep->flags & DWC3_EP_STALL)) {
1993da10bcddSThinh Nguyen 		dep->flags |= DWC3_EP_DELAY_START;
1994da10bcddSThinh Nguyen 		return 0;
1995da10bcddSThinh Nguyen 	}
1996da10bcddSThinh Nguyen 
1997d889c23cSFelipe Balbi 	/*
1998d889c23cSFelipe Balbi 	 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1999d889c23cSFelipe Balbi 	 * wait for a XferNotReady event so we will know what's the current
2000d889c23cSFelipe Balbi 	 * (micro-)frame number.
2001d889c23cSFelipe Balbi 	 *
2002d889c23cSFelipe Balbi 	 * Without this trick, we are very, very likely gonna get Bus Expiry
2003d889c23cSFelipe Balbi 	 * errors which will force us issue EndTransfer command.
2004d889c23cSFelipe Balbi 	 */
2005d889c23cSFelipe Balbi 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
200626d27a10SMichael Grzeschik 		if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
200726d27a10SMichael Grzeschik 			if ((dep->flags & DWC3_EP_PENDING_REQUEST))
200825abad6aSFelipe Balbi 				return __dwc3_gadget_start_isoc(dep);
200926d27a10SMichael Grzeschik 
201026d27a10SMichael Grzeschik 			return 0;
2011f4a53c55SPratyush Anand 		}
2012fe990ceaSFelipe Balbi 	}
2013f1d6826cSRoger Quadros 
201418ffa988SWesley Cheng 	__dwc3_gadget_kick_transfer(dep);
201518ffa988SWesley Cheng 
201618ffa988SWesley Cheng 	return 0;
201772246da4SFelipe Balbi }
201872246da4SFelipe Balbi 
dwc3_gadget_ep_queue(struct usb_ep * ep,struct usb_request * request,gfp_t gfp_flags)201972246da4SFelipe Balbi static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
202072246da4SFelipe Balbi 	gfp_t gfp_flags)
202172246da4SFelipe Balbi {
202272246da4SFelipe Balbi 	struct dwc3_request		*req = to_dwc3_request(request);
202372246da4SFelipe Balbi 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
202472246da4SFelipe Balbi 	struct dwc3			*dwc = dep->dwc;
202572246da4SFelipe Balbi 
202672246da4SFelipe Balbi 	unsigned long			flags;
202772246da4SFelipe Balbi 
202872246da4SFelipe Balbi 	int				ret;
202972246da4SFelipe Balbi 
2030fdee4ebaSZhuang Jin Can 	spin_lock_irqsave(&dwc->lock, flags);
203172246da4SFelipe Balbi 	ret = __dwc3_gadget_ep_queue(dep, req);
203272246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
203372246da4SFelipe Balbi 
203472246da4SFelipe Balbi 	return ret;
203572246da4SFelipe Balbi }
203672246da4SFelipe Balbi 
dwc3_gadget_ep_skip_trbs(struct dwc3_ep * dep,struct dwc3_request * req)20377746a8dfSFelipe Balbi static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req)
20387746a8dfSFelipe Balbi {
20397746a8dfSFelipe Balbi 	int i;
20407746a8dfSFelipe Balbi 
2041cb11ea56SThinh Nguyen 	/* If req->trb is not set, then the request has not started */
2042cb11ea56SThinh Nguyen 	if (!req->trb)
2043cb11ea56SThinh Nguyen 		return;
2044cb11ea56SThinh Nguyen 
20457746a8dfSFelipe Balbi 	/*
20467746a8dfSFelipe Balbi 	 * If request was already started, this means we had to
20477746a8dfSFelipe Balbi 	 * stop the transfer. With that we also need to ignore
20487746a8dfSFelipe Balbi 	 * all TRBs used by the request, however TRBs can only
20497746a8dfSFelipe Balbi 	 * be modified after completion of END_TRANSFER
20507746a8dfSFelipe Balbi 	 * command. So what we do here is that we wait for
20517746a8dfSFelipe Balbi 	 * END_TRANSFER completion and only after that, we jump
20527746a8dfSFelipe Balbi 	 * over TRBs by clearing HWO and incrementing dequeue
20537746a8dfSFelipe Balbi 	 * pointer.
20547746a8dfSFelipe Balbi 	 */
20557746a8dfSFelipe Balbi 	for (i = 0; i < req->num_trbs; i++) {
20567746a8dfSFelipe Balbi 		struct dwc3_trb *trb;
20577746a8dfSFelipe Balbi 
20582dedea03SThinh Nguyen 		trb = &dep->trb_pool[dep->trb_dequeue];
20597746a8dfSFelipe Balbi 		trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
20607746a8dfSFelipe Balbi 		dwc3_ep_inc_deq(dep);
20617746a8dfSFelipe Balbi 	}
2062c7152763SThinh Nguyen 
2063c7152763SThinh Nguyen 	req->num_trbs = 0;
20647746a8dfSFelipe Balbi }
20657746a8dfSFelipe Balbi 
dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep * dep)2066d4f1afe5SFelipe Balbi static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
2067d4f1afe5SFelipe Balbi {
2068d4f1afe5SFelipe Balbi 	struct dwc3_request		*req;
206904dd6e76SRay Chi 	struct dwc3			*dwc = dep->dwc;
2070d4f1afe5SFelipe Balbi 
2071bf594d1dSWesley Cheng 	while (!list_empty(&dep->cancelled_list)) {
2072bf594d1dSWesley Cheng 		req = next_request(&dep->cancelled_list);
2073d4f1afe5SFelipe Balbi 		dwc3_gadget_ep_skip_trbs(dep, req);
207404dd6e76SRay Chi 		switch (req->status) {
207504dd6e76SRay Chi 		case DWC3_REQUEST_STATUS_DISCONNECTED:
207604dd6e76SRay Chi 			dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
207704dd6e76SRay Chi 			break;
207804dd6e76SRay Chi 		case DWC3_REQUEST_STATUS_DEQUEUED:
2079d4f1afe5SFelipe Balbi 			dwc3_gadget_giveback(dep, req, -ECONNRESET);
208004dd6e76SRay Chi 			break;
208104dd6e76SRay Chi 		case DWC3_REQUEST_STATUS_STALLED:
208204dd6e76SRay Chi 			dwc3_gadget_giveback(dep, req, -EPIPE);
208304dd6e76SRay Chi 			break;
208404dd6e76SRay Chi 		default:
208504dd6e76SRay Chi 			dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status);
208604dd6e76SRay Chi 			dwc3_gadget_giveback(dep, req, -ECONNRESET);
208704dd6e76SRay Chi 			break;
208804dd6e76SRay Chi 		}
2089bf594d1dSWesley Cheng 		/*
2090bf594d1dSWesley Cheng 		 * The endpoint is disabled, let the dwc3_remove_requests()
2091bf594d1dSWesley Cheng 		 * handle the cleanup.
2092bf594d1dSWesley Cheng 		 */
2093bf594d1dSWesley Cheng 		if (!dep->endpoint.desc)
2094bf594d1dSWesley Cheng 			break;
2095d4f1afe5SFelipe Balbi 	}
2096d4f1afe5SFelipe Balbi }
2097d4f1afe5SFelipe Balbi 
dwc3_gadget_ep_dequeue(struct usb_ep * ep,struct usb_request * request)209872246da4SFelipe Balbi static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
209972246da4SFelipe Balbi 		struct usb_request *request)
210072246da4SFelipe Balbi {
210172246da4SFelipe Balbi 	struct dwc3_request		*req = to_dwc3_request(request);
210272246da4SFelipe Balbi 	struct dwc3_request		*r = NULL;
210372246da4SFelipe Balbi 
210472246da4SFelipe Balbi 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
210572246da4SFelipe Balbi 	struct dwc3			*dwc = dep->dwc;
210672246da4SFelipe Balbi 
210772246da4SFelipe Balbi 	unsigned long			flags;
210872246da4SFelipe Balbi 	int				ret = 0;
210972246da4SFelipe Balbi 
21102c4cbe6eSFelipe Balbi 	trace_dwc3_ep_dequeue(req);
21112c4cbe6eSFelipe Balbi 
211272246da4SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
211372246da4SFelipe Balbi 
2114a7027ca6SThinh Nguyen 	list_for_each_entry(r, &dep->cancelled_list, list) {
211572246da4SFelipe Balbi 		if (r == req)
2116fcd2def6SThinh Nguyen 			goto out;
211772246da4SFelipe Balbi 	}
211872246da4SFelipe Balbi 
211972246da4SFelipe Balbi 	list_for_each_entry(r, &dep->pending_list, list) {
212072246da4SFelipe Balbi 		if (r == req) {
2121e9deab5bSWesley Cheng 			/*
2122e9deab5bSWesley Cheng 			 * Explicitly check for EP0/1 as dequeue for those
2123e9deab5bSWesley Cheng 			 * EPs need to be handled differently.  Control EP
2124e9deab5bSWesley Cheng 			 * only deals with one USB req, and giveback will
2125e9deab5bSWesley Cheng 			 * occur during dwc3_ep0_stall_and_restart().  EP0
2126e9deab5bSWesley Cheng 			 * requests are never added to started_list.
2127e9deab5bSWesley Cheng 			 */
2128e9deab5bSWesley Cheng 			if (dep->number > 1)
2129fcd2def6SThinh Nguyen 				dwc3_gadget_giveback(dep, req, -ECONNRESET);
2130e9deab5bSWesley Cheng 			else
2131e9deab5bSWesley Cheng 				dwc3_ep0_reset_state(dwc);
2132fcd2def6SThinh Nguyen 			goto out;
2133fcd2def6SThinh Nguyen 		}
213472246da4SFelipe Balbi 	}
213572246da4SFelipe Balbi 
213672246da4SFelipe Balbi 	list_for_each_entry(r, &dep->started_list, list) {
213772246da4SFelipe Balbi 		if (r == req) {
2138a7027ca6SThinh Nguyen 			struct dwc3_request *t;
2139a7027ca6SThinh Nguyen 
214072246da4SFelipe Balbi 			/* wait until it is processed */
2141c5353b22SFelipe Balbi 			dwc3_stop_active_transfer(dep, true, true);
2142cf3113d8SFelipe Balbi 
2143a7027ca6SThinh Nguyen 			/*
2144a7027ca6SThinh Nguyen 			 * Remove any started request if the transfer is
2145a7027ca6SThinh Nguyen 			 * cancelled.
2146a7027ca6SThinh Nguyen 			 */
2147a7027ca6SThinh Nguyen 			list_for_each_entry_safe(r, t, &dep->started_list, list)
214804dd6e76SRay Chi 				dwc3_gadget_move_cancelled_request(r,
214904dd6e76SRay Chi 						DWC3_REQUEST_STATUS_DEQUEUED);
2150cf3113d8SFelipe Balbi 
2151a5c7682aSThinh Nguyen 			dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
2152a5c7682aSThinh Nguyen 
2153fcd2def6SThinh Nguyen 			goto out;
215472246da4SFelipe Balbi 		}
2155fcd2def6SThinh Nguyen 	}
2156fcd2def6SThinh Nguyen 
215704fb365cSFelipe Balbi 	dev_err(dwc->dev, "request %pK was not queued to %s\n",
215872246da4SFelipe Balbi 		request, ep->name);
215972246da4SFelipe Balbi 	ret = -EINVAL;
2160fcd2def6SThinh Nguyen out:
216172246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
216272246da4SFelipe Balbi 
216372246da4SFelipe Balbi 	return ret;
216472246da4SFelipe Balbi }
216572246da4SFelipe Balbi 
__dwc3_gadget_ep_set_halt(struct dwc3_ep * dep,int value,int protocol)21667a608559SFelipe Balbi int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
216772246da4SFelipe Balbi {
216872246da4SFelipe Balbi 	struct dwc3_gadget_ep_cmd_params	params;
216972246da4SFelipe Balbi 	struct dwc3				*dwc = dep->dwc;
2170cb11ea56SThinh Nguyen 	struct dwc3_request			*req;
2171cb11ea56SThinh Nguyen 	struct dwc3_request			*tmp;
217272246da4SFelipe Balbi 	int					ret;
217372246da4SFelipe Balbi 
21745ad02fb8SFelipe Balbi 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
21755ad02fb8SFelipe Balbi 		dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
21765ad02fb8SFelipe Balbi 		return -EINVAL;
21775ad02fb8SFelipe Balbi 	}
21785ad02fb8SFelipe Balbi 
217972246da4SFelipe Balbi 	memset(&params, 0x00, sizeof(params));
218072246da4SFelipe Balbi 
218172246da4SFelipe Balbi 	if (value) {
218269450c4dSFelipe Balbi 		struct dwc3_trb *trb;
218369450c4dSFelipe Balbi 
2184e319bd62SFelipe Balbi 		unsigned int transfer_in_flight;
2185e319bd62SFelipe Balbi 		unsigned int started;
218669450c4dSFelipe Balbi 
218769450c4dSFelipe Balbi 		if (dep->number > 1)
218869450c4dSFelipe Balbi 			trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
218969450c4dSFelipe Balbi 		else
219069450c4dSFelipe Balbi 			trb = &dwc->ep0_trb[dep->trb_enqueue];
219169450c4dSFelipe Balbi 
219269450c4dSFelipe Balbi 		transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
219369450c4dSFelipe Balbi 		started = !list_empty(&dep->started_list);
219469450c4dSFelipe Balbi 
219569450c4dSFelipe Balbi 		if (!protocol && ((dep->direction && transfer_in_flight) ||
219669450c4dSFelipe Balbi 				(!dep->direction && started))) {
21977a608559SFelipe Balbi 			return -EAGAIN;
21987a608559SFelipe Balbi 		}
21997a608559SFelipe Balbi 
22002cd4718dSFelipe Balbi 		ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
22012cd4718dSFelipe Balbi 				&params);
220272246da4SFelipe Balbi 		if (ret)
22033f89204bSDan Carpenter 			dev_err(dwc->dev, "failed to set STALL on %s\n",
220472246da4SFelipe Balbi 					dep->name);
220572246da4SFelipe Balbi 		else
220672246da4SFelipe Balbi 			dep->flags |= DWC3_EP_STALL;
220772246da4SFelipe Balbi 	} else {
2208cb11ea56SThinh Nguyen 		/*
2209cb11ea56SThinh Nguyen 		 * Don't issue CLEAR_STALL command to control endpoints. The
2210cb11ea56SThinh Nguyen 		 * controller automatically clears the STALL when it receives
2211cb11ea56SThinh Nguyen 		 * the SETUP token.
2212cb11ea56SThinh Nguyen 		 */
2213cb11ea56SThinh Nguyen 		if (dep->number <= 1) {
2214cb11ea56SThinh Nguyen 			dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2215cb11ea56SThinh Nguyen 			return 0;
2216cb11ea56SThinh Nguyen 		}
22172cd4718dSFelipe Balbi 
2218d97c78a1SThinh Nguyen 		dwc3_stop_active_transfer(dep, true, true);
2219d97c78a1SThinh Nguyen 
2220d97c78a1SThinh Nguyen 		list_for_each_entry_safe(req, tmp, &dep->started_list, list)
222104dd6e76SRay Chi 			dwc3_gadget_move_cancelled_request(req, DWC3_REQUEST_STATUS_STALLED);
2222d97c78a1SThinh Nguyen 
2223e4cf6580SThinh Nguyen 		if (dep->flags & DWC3_EP_END_TRANSFER_PENDING ||
2224e4cf6580SThinh Nguyen 		    (dep->flags & DWC3_EP_DELAY_STOP)) {
2225d97c78a1SThinh Nguyen 			dep->flags |= DWC3_EP_PENDING_CLEAR_STALL;
22262840d6dfSWesley Cheng 			if (protocol)
22272840d6dfSWesley Cheng 				dwc->clear_stall_protocol = dep->number;
22282840d6dfSWesley Cheng 
2229d97c78a1SThinh Nguyen 			return 0;
2230d97c78a1SThinh Nguyen 		}
2231d97c78a1SThinh Nguyen 
2232d97c78a1SThinh Nguyen 		dwc3_gadget_ep_cleanup_cancelled_requests(dep);
2233d97c78a1SThinh Nguyen 
223450c763f8SJohn Youn 		ret = dwc3_send_clear_stall_ep_cmd(dep);
2235cb11ea56SThinh Nguyen 		if (ret) {
22363f89204bSDan Carpenter 			dev_err(dwc->dev, "failed to clear STALL on %s\n",
223772246da4SFelipe Balbi 					dep->name);
2238cb11ea56SThinh Nguyen 			return ret;
2239cb11ea56SThinh Nguyen 		}
2240cb11ea56SThinh Nguyen 
2241a535d81cSAlan Stern 		dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
2242cb11ea56SThinh Nguyen 
2243c503672aSThinh Nguyen 		if ((dep->flags & DWC3_EP_DELAY_START) &&
2244c503672aSThinh Nguyen 		    !usb_endpoint_xfer_isoc(dep->endpoint.desc))
2245c503672aSThinh Nguyen 			__dwc3_gadget_kick_transfer(dep);
2246c503672aSThinh Nguyen 
2247c503672aSThinh Nguyen 		dep->flags &= ~DWC3_EP_DELAY_START;
224872246da4SFelipe Balbi 	}
22495275455aSPaul Zimmerman 
225072246da4SFelipe Balbi 	return ret;
225172246da4SFelipe Balbi }
225272246da4SFelipe Balbi 
dwc3_gadget_ep_set_halt(struct usb_ep * ep,int value)225372246da4SFelipe Balbi static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
225472246da4SFelipe Balbi {
225572246da4SFelipe Balbi 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
225672246da4SFelipe Balbi 	struct dwc3			*dwc = dep->dwc;
225772246da4SFelipe Balbi 
225872246da4SFelipe Balbi 	unsigned long			flags;
225972246da4SFelipe Balbi 
226072246da4SFelipe Balbi 	int				ret;
226172246da4SFelipe Balbi 
226272246da4SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
22637a608559SFelipe Balbi 	ret = __dwc3_gadget_ep_set_halt(dep, value, false);
226472246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
226572246da4SFelipe Balbi 
226672246da4SFelipe Balbi 	return ret;
226772246da4SFelipe Balbi }
226872246da4SFelipe Balbi 
dwc3_gadget_ep_set_wedge(struct usb_ep * ep)226972246da4SFelipe Balbi static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
227072246da4SFelipe Balbi {
227172246da4SFelipe Balbi 	struct dwc3_ep			*dep = to_dwc3_ep(ep);
2272249a4569SPaul Zimmerman 	struct dwc3			*dwc = dep->dwc;
2273249a4569SPaul Zimmerman 	unsigned long			flags;
227495aa4e8dSFelipe Balbi 	int				ret;
227572246da4SFelipe Balbi 
2276249a4569SPaul Zimmerman 	spin_lock_irqsave(&dwc->lock, flags);
227772246da4SFelipe Balbi 	dep->flags |= DWC3_EP_WEDGE;
227872246da4SFelipe Balbi 
227908f0d966SPratyush Anand 	if (dep->number == 0 || dep->number == 1)
228095aa4e8dSFelipe Balbi 		ret = __dwc3_gadget_ep0_set_halt(ep, 1);
228108f0d966SPratyush Anand 	else
22827a608559SFelipe Balbi 		ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
228395aa4e8dSFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
228495aa4e8dSFelipe Balbi 
228595aa4e8dSFelipe Balbi 	return ret;
228672246da4SFelipe Balbi }
228772246da4SFelipe Balbi 
228872246da4SFelipe Balbi /* -------------------------------------------------------------------------- */
228972246da4SFelipe Balbi 
229072246da4SFelipe Balbi static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
229172246da4SFelipe Balbi 	.bLength	= USB_DT_ENDPOINT_SIZE,
229272246da4SFelipe Balbi 	.bDescriptorType = USB_DT_ENDPOINT,
229372246da4SFelipe Balbi 	.bmAttributes	= USB_ENDPOINT_XFER_CONTROL,
229472246da4SFelipe Balbi };
229572246da4SFelipe Balbi 
229672246da4SFelipe Balbi static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
229772246da4SFelipe Balbi 	.enable		= dwc3_gadget_ep0_enable,
229872246da4SFelipe Balbi 	.disable	= dwc3_gadget_ep0_disable,
229972246da4SFelipe Balbi 	.alloc_request	= dwc3_gadget_ep_alloc_request,
230072246da4SFelipe Balbi 	.free_request	= dwc3_gadget_ep_free_request,
230172246da4SFelipe Balbi 	.queue		= dwc3_gadget_ep0_queue,
230272246da4SFelipe Balbi 	.dequeue	= dwc3_gadget_ep_dequeue,
230308f0d966SPratyush Anand 	.set_halt	= dwc3_gadget_ep0_set_halt,
230472246da4SFelipe Balbi 	.set_wedge	= dwc3_gadget_ep_set_wedge,
230572246da4SFelipe Balbi };
230672246da4SFelipe Balbi 
230772246da4SFelipe Balbi static const struct usb_ep_ops dwc3_gadget_ep_ops = {
230872246da4SFelipe Balbi 	.enable		= dwc3_gadget_ep_enable,
230972246da4SFelipe Balbi 	.disable	= dwc3_gadget_ep_disable,
231072246da4SFelipe Balbi 	.alloc_request	= dwc3_gadget_ep_alloc_request,
231172246da4SFelipe Balbi 	.free_request	= dwc3_gadget_ep_free_request,
231272246da4SFelipe Balbi 	.queue		= dwc3_gadget_ep_queue,
231372246da4SFelipe Balbi 	.dequeue	= dwc3_gadget_ep_dequeue,
231472246da4SFelipe Balbi 	.set_halt	= dwc3_gadget_ep_set_halt,
231572246da4SFelipe Balbi 	.set_wedge	= dwc3_gadget_ep_set_wedge,
231672246da4SFelipe Balbi };
231772246da4SFelipe Balbi 
231872246da4SFelipe Balbi /* -------------------------------------------------------------------------- */
231972246da4SFelipe Balbi 
dwc3_gadget_enable_linksts_evts(struct dwc3 * dwc,bool set)232004716168SElson Roy Serrao static void dwc3_gadget_enable_linksts_evts(struct dwc3 *dwc, bool set)
232104716168SElson Roy Serrao {
232204716168SElson Roy Serrao 	u32 reg;
232304716168SElson Roy Serrao 
232404716168SElson Roy Serrao 	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
232504716168SElson Roy Serrao 		return;
232604716168SElson Roy Serrao 
232704716168SElson Roy Serrao 	reg = dwc3_readl(dwc->regs, DWC3_DEVTEN);
232804716168SElson Roy Serrao 	if (set)
232904716168SElson Roy Serrao 		reg |= DWC3_DEVTEN_ULSTCNGEN;
233004716168SElson Roy Serrao 	else
233104716168SElson Roy Serrao 		reg &= ~DWC3_DEVTEN_ULSTCNGEN;
233204716168SElson Roy Serrao 
233304716168SElson Roy Serrao 	dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
233404716168SElson Roy Serrao }
233504716168SElson Roy Serrao 
dwc3_gadget_get_frame(struct usb_gadget * g)233672246da4SFelipe Balbi static int dwc3_gadget_get_frame(struct usb_gadget *g)
233772246da4SFelipe Balbi {
233872246da4SFelipe Balbi 	struct dwc3		*dwc = gadget_to_dwc(g);
233972246da4SFelipe Balbi 
23406cb2e4e3SFelipe Balbi 	return __dwc3_gadget_get_frame(dwc);
234172246da4SFelipe Balbi }
234272246da4SFelipe Balbi 
__dwc3_gadget_wakeup(struct dwc3 * dwc,bool async)234304716168SElson Roy Serrao static int __dwc3_gadget_wakeup(struct dwc3 *dwc, bool async)
234472246da4SFelipe Balbi {
2345d6011f6fSNicolas Saenz Julienne 	int			retries;
234672246da4SFelipe Balbi 
2347218ef7b6SFelipe Balbi 	int			ret;
234872246da4SFelipe Balbi 	u32			reg;
234972246da4SFelipe Balbi 
235072246da4SFelipe Balbi 	u8			link_state;
235172246da4SFelipe Balbi 
235272246da4SFelipe Balbi 	/*
235372246da4SFelipe Balbi 	 * According to the Databook Remote wakeup request should
235472246da4SFelipe Balbi 	 * be issued only when the device is in early suspend state.
235572246da4SFelipe Balbi 	 *
235672246da4SFelipe Balbi 	 * We can check that via USB Link State bits in DSTS register.
235772246da4SFelipe Balbi 	 */
235872246da4SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
235972246da4SFelipe Balbi 
236072246da4SFelipe Balbi 	link_state = DWC3_DSTS_USBLNKST(reg);
236172246da4SFelipe Balbi 
236272246da4SFelipe Balbi 	switch (link_state) {
2363d0550cd2SThinh Nguyen 	case DWC3_LINK_STATE_RESET:
236472246da4SFelipe Balbi 	case DWC3_LINK_STATE_RX_DET:	/* in HS, means Early Suspend */
236572246da4SFelipe Balbi 	case DWC3_LINK_STATE_U3:	/* in HS, means SUSPEND */
2366c560e763SThinh Nguyen 	case DWC3_LINK_STATE_U2:	/* in HS, means Sleep (L1) */
2367c560e763SThinh Nguyen 	case DWC3_LINK_STATE_U1:
2368d0550cd2SThinh Nguyen 	case DWC3_LINK_STATE_RESUME:
236972246da4SFelipe Balbi 		break;
237072246da4SFelipe Balbi 	default:
2371218ef7b6SFelipe Balbi 		return -EINVAL;
237272246da4SFelipe Balbi 	}
237372246da4SFelipe Balbi 
237404716168SElson Roy Serrao 	if (async)
237504716168SElson Roy Serrao 		dwc3_gadget_enable_linksts_evts(dwc, true);
237604716168SElson Roy Serrao 
23778598bde7SFelipe Balbi 	ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
23788598bde7SFelipe Balbi 	if (ret < 0) {
23798598bde7SFelipe Balbi 		dev_err(dwc->dev, "failed to put link in Recovery\n");
238004716168SElson Roy Serrao 		dwc3_gadget_enable_linksts_evts(dwc, false);
2381218ef7b6SFelipe Balbi 		return ret;
23828598bde7SFelipe Balbi 	}
238372246da4SFelipe Balbi 
2384802fde98SPaul Zimmerman 	/* Recent versions do this automatically */
23859af21dd6SThinh Nguyen 	if (DWC3_VER_IS_PRIOR(DWC3, 194A)) {
238672246da4SFelipe Balbi 		/* write zeroes to Link Change Request */
2387fcc023c7SFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
238872246da4SFelipe Balbi 		reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
238972246da4SFelipe Balbi 		dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2390802fde98SPaul Zimmerman 	}
239172246da4SFelipe Balbi 
239204716168SElson Roy Serrao 	/*
239304716168SElson Roy Serrao 	 * Since link status change events are enabled we will receive
239404716168SElson Roy Serrao 	 * an U0 event when wakeup is successful. So bail out.
239504716168SElson Roy Serrao 	 */
239604716168SElson Roy Serrao 	if (async)
239704716168SElson Roy Serrao 		return 0;
239804716168SElson Roy Serrao 
23991d046793SPaul Zimmerman 	/* poll until Link State changes to ON */
2400d6011f6fSNicolas Saenz Julienne 	retries = 20000;
240172246da4SFelipe Balbi 
2402d6011f6fSNicolas Saenz Julienne 	while (retries--) {
240372246da4SFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
240472246da4SFelipe Balbi 
240572246da4SFelipe Balbi 		/* in HS, means ON */
240672246da4SFelipe Balbi 		if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
240772246da4SFelipe Balbi 			break;
240872246da4SFelipe Balbi 	}
240972246da4SFelipe Balbi 
241072246da4SFelipe Balbi 	if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
241172246da4SFelipe Balbi 		dev_err(dwc->dev, "failed to send remote wakeup\n");
2412218ef7b6SFelipe Balbi 		return -EINVAL;
241372246da4SFelipe Balbi 	}
241472246da4SFelipe Balbi 
2415218ef7b6SFelipe Balbi 	return 0;
2416218ef7b6SFelipe Balbi }
2417218ef7b6SFelipe Balbi 
dwc3_gadget_wakeup(struct usb_gadget * g)2418218ef7b6SFelipe Balbi static int dwc3_gadget_wakeup(struct usb_gadget *g)
2419218ef7b6SFelipe Balbi {
2420218ef7b6SFelipe Balbi 	struct dwc3		*dwc = gadget_to_dwc(g);
2421218ef7b6SFelipe Balbi 	unsigned long		flags;
2422218ef7b6SFelipe Balbi 	int			ret;
2423218ef7b6SFelipe Balbi 
242404716168SElson Roy Serrao 	if (!dwc->wakeup_configured) {
242504716168SElson Roy Serrao 		dev_err(dwc->dev, "remote wakeup not configured\n");
242604716168SElson Roy Serrao 		return -EINVAL;
242704716168SElson Roy Serrao 	}
242804716168SElson Roy Serrao 
2429218ef7b6SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
243004716168SElson Roy Serrao 	if (!dwc->gadget->wakeup_armed) {
243104716168SElson Roy Serrao 		dev_err(dwc->dev, "not armed for remote wakeup\n");
243204716168SElson Roy Serrao 		spin_unlock_irqrestore(&dwc->lock, flags);
243304716168SElson Roy Serrao 		return -EINVAL;
243404716168SElson Roy Serrao 	}
243504716168SElson Roy Serrao 	ret = __dwc3_gadget_wakeup(dwc, true);
243604716168SElson Roy Serrao 
243772246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
243872246da4SFelipe Balbi 
243972246da4SFelipe Balbi 	return ret;
244072246da4SFelipe Balbi }
244172246da4SFelipe Balbi 
244292c08a84SElson Roy Serrao static void dwc3_resume_gadget(struct dwc3 *dwc);
244392c08a84SElson Roy Serrao 
dwc3_gadget_func_wakeup(struct usb_gadget * g,int intf_id)244492c08a84SElson Roy Serrao static int dwc3_gadget_func_wakeup(struct usb_gadget *g, int intf_id)
244592c08a84SElson Roy Serrao {
244692c08a84SElson Roy Serrao 	struct  dwc3		*dwc = gadget_to_dwc(g);
244792c08a84SElson Roy Serrao 	unsigned long		flags;
244892c08a84SElson Roy Serrao 	int			ret;
244992c08a84SElson Roy Serrao 	int			link_state;
245092c08a84SElson Roy Serrao 
245192c08a84SElson Roy Serrao 	if (!dwc->wakeup_configured) {
245292c08a84SElson Roy Serrao 		dev_err(dwc->dev, "remote wakeup not configured\n");
245392c08a84SElson Roy Serrao 		return -EINVAL;
245492c08a84SElson Roy Serrao 	}
245592c08a84SElson Roy Serrao 
245692c08a84SElson Roy Serrao 	spin_lock_irqsave(&dwc->lock, flags);
245792c08a84SElson Roy Serrao 	/*
245892c08a84SElson Roy Serrao 	 * If the link is in U3, signal for remote wakeup and wait for the
245992c08a84SElson Roy Serrao 	 * link to transition to U0 before sending device notification.
246092c08a84SElson Roy Serrao 	 */
246192c08a84SElson Roy Serrao 	link_state = dwc3_gadget_get_link_state(dwc);
246292c08a84SElson Roy Serrao 	if (link_state == DWC3_LINK_STATE_U3) {
246392c08a84SElson Roy Serrao 		ret = __dwc3_gadget_wakeup(dwc, false);
246492c08a84SElson Roy Serrao 		if (ret) {
246592c08a84SElson Roy Serrao 			spin_unlock_irqrestore(&dwc->lock, flags);
246692c08a84SElson Roy Serrao 			return -EINVAL;
246792c08a84SElson Roy Serrao 		}
246892c08a84SElson Roy Serrao 		dwc3_resume_gadget(dwc);
24694e8ef34eSLinyu Yuan 		dwc->suspended = false;
247092c08a84SElson Roy Serrao 		dwc->link_state = DWC3_LINK_STATE_U0;
247192c08a84SElson Roy Serrao 	}
247292c08a84SElson Roy Serrao 
247392c08a84SElson Roy Serrao 	ret = dwc3_send_gadget_generic_command(dwc, DWC3_DGCMD_DEV_NOTIFICATION,
247492c08a84SElson Roy Serrao 					       DWC3_DGCMDPAR_DN_FUNC_WAKE |
247592c08a84SElson Roy Serrao 					       DWC3_DGCMDPAR_INTF_SEL(intf_id));
247692c08a84SElson Roy Serrao 	if (ret)
247792c08a84SElson Roy Serrao 		dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret);
247892c08a84SElson Roy Serrao 
247992c08a84SElson Roy Serrao 	spin_unlock_irqrestore(&dwc->lock, flags);
248092c08a84SElson Roy Serrao 
248192c08a84SElson Roy Serrao 	return ret;
248292c08a84SElson Roy Serrao }
248392c08a84SElson Roy Serrao 
dwc3_gadget_set_remote_wakeup(struct usb_gadget * g,int set)248404716168SElson Roy Serrao static int dwc3_gadget_set_remote_wakeup(struct usb_gadget *g, int set)
248504716168SElson Roy Serrao {
248604716168SElson Roy Serrao 	struct dwc3		*dwc = gadget_to_dwc(g);
248704716168SElson Roy Serrao 	unsigned long		flags;
248804716168SElson Roy Serrao 
248904716168SElson Roy Serrao 	spin_lock_irqsave(&dwc->lock, flags);
249004716168SElson Roy Serrao 	dwc->wakeup_configured = !!set;
249104716168SElson Roy Serrao 	spin_unlock_irqrestore(&dwc->lock, flags);
249204716168SElson Roy Serrao 
249304716168SElson Roy Serrao 	return 0;
249404716168SElson Roy Serrao }
249504716168SElson Roy Serrao 
dwc3_gadget_set_selfpowered(struct usb_gadget * g,int is_selfpowered)249672246da4SFelipe Balbi static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
249772246da4SFelipe Balbi 		int is_selfpowered)
249872246da4SFelipe Balbi {
249972246da4SFelipe Balbi 	struct dwc3		*dwc = gadget_to_dwc(g);
2500249a4569SPaul Zimmerman 	unsigned long		flags;
250172246da4SFelipe Balbi 
2502249a4569SPaul Zimmerman 	spin_lock_irqsave(&dwc->lock, flags);
2503bcdea503SPeter Chen 	g->is_selfpowered = !!is_selfpowered;
2504249a4569SPaul Zimmerman 	spin_unlock_irqrestore(&dwc->lock, flags);
250572246da4SFelipe Balbi 
250672246da4SFelipe Balbi 	return 0;
250772246da4SFelipe Balbi }
250872246da4SFelipe Balbi 
dwc3_stop_active_transfers(struct dwc3 * dwc)2509ae7e8610SWesley Cheng static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2510ae7e8610SWesley Cheng {
2511ae7e8610SWesley Cheng 	u32 epnum;
2512ae7e8610SWesley Cheng 
2513ae7e8610SWesley Cheng 	for (epnum = 2; epnum < dwc->num_eps; epnum++) {
2514ae7e8610SWesley Cheng 		struct dwc3_ep *dep;
2515ae7e8610SWesley Cheng 
2516ae7e8610SWesley Cheng 		dep = dwc->eps[epnum];
2517ae7e8610SWesley Cheng 		if (!dep)
2518ae7e8610SWesley Cheng 			continue;
2519ae7e8610SWesley Cheng 
2520b44c0e7fSMichael Grzeschik 		dwc3_remove_requests(dwc, dep, -ESHUTDOWN);
2521ae7e8610SWesley Cheng 	}
2522ae7e8610SWesley Cheng }
2523ae7e8610SWesley Cheng 
__dwc3_gadget_set_ssp_rate(struct dwc3 * dwc)2524072cab8aSThinh Nguyen static void __dwc3_gadget_set_ssp_rate(struct dwc3 *dwc)
2525072cab8aSThinh Nguyen {
2526072cab8aSThinh Nguyen 	enum usb_ssp_rate	ssp_rate = dwc->gadget_ssp_rate;
2527072cab8aSThinh Nguyen 	u32			reg;
2528072cab8aSThinh Nguyen 
2529072cab8aSThinh Nguyen 	if (ssp_rate == USB_SSP_GEN_UNKNOWN)
2530072cab8aSThinh Nguyen 		ssp_rate = dwc->max_ssp_rate;
2531072cab8aSThinh Nguyen 
2532072cab8aSThinh Nguyen 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2533072cab8aSThinh Nguyen 	reg &= ~DWC3_DCFG_SPEED_MASK;
2534072cab8aSThinh Nguyen 	reg &= ~DWC3_DCFG_NUMLANES(~0);
2535072cab8aSThinh Nguyen 
2536072cab8aSThinh Nguyen 	if (ssp_rate == USB_SSP_GEN_1x2)
2537072cab8aSThinh Nguyen 		reg |= DWC3_DCFG_SUPERSPEED;
2538072cab8aSThinh Nguyen 	else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2)
2539072cab8aSThinh Nguyen 		reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2540072cab8aSThinh Nguyen 
2541072cab8aSThinh Nguyen 	if (ssp_rate != USB_SSP_GEN_2x1 &&
2542072cab8aSThinh Nguyen 	    dwc->max_ssp_rate != USB_SSP_GEN_2x1)
2543072cab8aSThinh Nguyen 		reg |= DWC3_DCFG_NUMLANES(1);
2544072cab8aSThinh Nguyen 
2545072cab8aSThinh Nguyen 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2546072cab8aSThinh Nguyen }
2547072cab8aSThinh Nguyen 
__dwc3_gadget_set_speed(struct dwc3 * dwc)25487c9a2598SWesley Cheng static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
25497c9a2598SWesley Cheng {
2550450b9e9fSThinh Nguyen 	enum usb_device_speed	speed;
25517c9a2598SWesley Cheng 	u32			reg;
25527c9a2598SWesley Cheng 
2553450b9e9fSThinh Nguyen 	speed = dwc->gadget_max_speed;
255493f1d43cSThinh Nguyen 	if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed)
2555450b9e9fSThinh Nguyen 		speed = dwc->maximum_speed;
2556450b9e9fSThinh Nguyen 
2557450b9e9fSThinh Nguyen 	if (speed == USB_SPEED_SUPER_PLUS &&
2558072cab8aSThinh Nguyen 	    DWC3_IP_IS(DWC32)) {
2559072cab8aSThinh Nguyen 		__dwc3_gadget_set_ssp_rate(dwc);
2560072cab8aSThinh Nguyen 		return;
2561072cab8aSThinh Nguyen 	}
2562072cab8aSThinh Nguyen 
25637c9a2598SWesley Cheng 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
25647c9a2598SWesley Cheng 	reg &= ~(DWC3_DCFG_SPEED_MASK);
25657c9a2598SWesley Cheng 
25667c9a2598SWesley Cheng 	/*
25677c9a2598SWesley Cheng 	 * WORKAROUND: DWC3 revision < 2.20a have an issue
25687c9a2598SWesley Cheng 	 * which would cause metastability state on Run/Stop
25697c9a2598SWesley Cheng 	 * bit if we try to force the IP to USB2-only mode.
25707c9a2598SWesley Cheng 	 *
25717c9a2598SWesley Cheng 	 * Because of that, we cannot configure the IP to any
25727c9a2598SWesley Cheng 	 * speed other than the SuperSpeed
25737c9a2598SWesley Cheng 	 *
25747c9a2598SWesley Cheng 	 * Refers to:
25757c9a2598SWesley Cheng 	 *
25767c9a2598SWesley Cheng 	 * STAR#9000525659: Clock Domain Crossing on DCTL in
25777c9a2598SWesley Cheng 	 * USB 2.0 Mode
25787c9a2598SWesley Cheng 	 */
25797c9a2598SWesley Cheng 	if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
25807c9a2598SWesley Cheng 	    !dwc->dis_metastability_quirk) {
25817c9a2598SWesley Cheng 		reg |= DWC3_DCFG_SUPERSPEED;
25827c9a2598SWesley Cheng 	} else {
2583450b9e9fSThinh Nguyen 		switch (speed) {
25847c9a2598SWesley Cheng 		case USB_SPEED_FULL:
25857c9a2598SWesley Cheng 			reg |= DWC3_DCFG_FULLSPEED;
25867c9a2598SWesley Cheng 			break;
25877c9a2598SWesley Cheng 		case USB_SPEED_HIGH:
25887c9a2598SWesley Cheng 			reg |= DWC3_DCFG_HIGHSPEED;
25897c9a2598SWesley Cheng 			break;
25907c9a2598SWesley Cheng 		case USB_SPEED_SUPER:
25917c9a2598SWesley Cheng 			reg |= DWC3_DCFG_SUPERSPEED;
25927c9a2598SWesley Cheng 			break;
25937c9a2598SWesley Cheng 		case USB_SPEED_SUPER_PLUS:
25947c9a2598SWesley Cheng 			if (DWC3_IP_IS(DWC3))
25957c9a2598SWesley Cheng 				reg |= DWC3_DCFG_SUPERSPEED;
25967c9a2598SWesley Cheng 			else
25977c9a2598SWesley Cheng 				reg |= DWC3_DCFG_SUPERSPEED_PLUS;
25987c9a2598SWesley Cheng 			break;
25997c9a2598SWesley Cheng 		default:
2600450b9e9fSThinh Nguyen 			dev_err(dwc->dev, "invalid speed (%d)\n", speed);
26017c9a2598SWesley Cheng 
26027c9a2598SWesley Cheng 			if (DWC3_IP_IS(DWC3))
26037c9a2598SWesley Cheng 				reg |= DWC3_DCFG_SUPERSPEED;
26047c9a2598SWesley Cheng 			else
26057c9a2598SWesley Cheng 				reg |= DWC3_DCFG_SUPERSPEED_PLUS;
26067c9a2598SWesley Cheng 		}
26077c9a2598SWesley Cheng 	}
2608f551037cSThinh Nguyen 
2609f551037cSThinh Nguyen 	if (DWC3_IP_IS(DWC32) &&
2610450b9e9fSThinh Nguyen 	    speed > USB_SPEED_UNKNOWN &&
2611450b9e9fSThinh Nguyen 	    speed < USB_SPEED_SUPER_PLUS)
2612f551037cSThinh Nguyen 		reg &= ~DWC3_DCFG_NUMLANES(~0);
2613f551037cSThinh Nguyen 
26147c9a2598SWesley Cheng 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
26157c9a2598SWesley Cheng }
26167c9a2598SWesley Cheng 
dwc3_gadget_run_stop(struct dwc3 * dwc,int is_on)2617bdb19d01SJohan Hovold static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
261872246da4SFelipe Balbi {
261972246da4SFelipe Balbi 	u32			reg;
2620461ee467SWesley Cheng 	u32			timeout = 2000;
26212f71a89dSSelvarasu Ganesan 	u32			saved_config = 0;
262272246da4SFelipe Balbi 
2623fc8bb91bSFelipe Balbi 	if (pm_runtime_suspended(dwc->dev))
2624fc8bb91bSFelipe Balbi 		return 0;
2625fc8bb91bSFelipe Balbi 
26262f71a89dSSelvarasu Ganesan 	/*
26272f71a89dSSelvarasu Ganesan 	 * When operating in USB 2.0 speeds (HS/FS), ensure that
26282f71a89dSSelvarasu Ganesan 	 * GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY are cleared before starting
26292f71a89dSSelvarasu Ganesan 	 * or stopping the controller. This resolves timeout issues that occur
26302f71a89dSSelvarasu Ganesan 	 * during frequent role switches between host and device modes.
26312f71a89dSSelvarasu Ganesan 	 *
26322f71a89dSSelvarasu Ganesan 	 * Save and clear these settings, then restore them after completing the
26332f71a89dSSelvarasu Ganesan 	 * controller start or stop sequence.
26342f71a89dSSelvarasu Ganesan 	 *
26352f71a89dSSelvarasu Ganesan 	 * This solution was discovered through experimentation as it is not
26362f71a89dSSelvarasu Ganesan 	 * mentioned in the dwc3 programming guide. It has been tested on an
26372f71a89dSSelvarasu Ganesan 	 * Exynos platforms.
26382f71a89dSSelvarasu Ganesan 	 */
26392f71a89dSSelvarasu Ganesan 	reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
26402f71a89dSSelvarasu Ganesan 	if (reg & DWC3_GUSB2PHYCFG_SUSPHY) {
26412f71a89dSSelvarasu Ganesan 		saved_config |= DWC3_GUSB2PHYCFG_SUSPHY;
26422f71a89dSSelvarasu Ganesan 		reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
26432f71a89dSSelvarasu Ganesan 	}
26442f71a89dSSelvarasu Ganesan 
26452f71a89dSSelvarasu Ganesan 	if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) {
26462f71a89dSSelvarasu Ganesan 		saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM;
26472f71a89dSSelvarasu Ganesan 		reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
26482f71a89dSSelvarasu Ganesan 	}
26492f71a89dSSelvarasu Ganesan 
26502f71a89dSSelvarasu Ganesan 	if (saved_config)
26512f71a89dSSelvarasu Ganesan 		dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
26522f71a89dSSelvarasu Ganesan 
265372246da4SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
26548db7ed15SFelipe Balbi 	if (is_on) {
26559af21dd6SThinh Nguyen 		if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) {
26568db7ed15SFelipe Balbi 			reg &= ~DWC3_DCTL_TRGTULST_MASK;
2657802fde98SPaul Zimmerman 			reg |= DWC3_DCTL_TRGTULST_RX_DET;
2658802fde98SPaul Zimmerman 		}
2659802fde98SPaul Zimmerman 
26609af21dd6SThinh Nguyen 		if (!DWC3_VER_IS_PRIOR(DWC3, 194A))
2661802fde98SPaul Zimmerman 			reg &= ~DWC3_DCTL_KEEP_CONNECT;
2662802fde98SPaul Zimmerman 		reg |= DWC3_DCTL_RUN_STOP;
26637b2a0368SFelipe Balbi 
26647c9a2598SWesley Cheng 		__dwc3_gadget_set_speed(dwc);
26659fcb3bd8SFelipe Balbi 		dwc->pullups_connected = true;
26668db7ed15SFelipe Balbi 	} else {
266772246da4SFelipe Balbi 		reg &= ~DWC3_DCTL_RUN_STOP;
26687b2a0368SFelipe Balbi 
26699fcb3bd8SFelipe Balbi 		dwc->pullups_connected = false;
26708db7ed15SFelipe Balbi 	}
267172246da4SFelipe Balbi 
26725b738211SThinh Nguyen 	dwc3_gadget_dctl_write_safe(dwc, reg);
267372246da4SFelipe Balbi 
267472246da4SFelipe Balbi 	do {
2675461ee467SWesley Cheng 		usleep_range(1000, 2000);
267672246da4SFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2677b6d4e16eSFelipe Balbi 		reg &= DWC3_DSTS_DEVCTRLHLT;
2678b6d4e16eSFelipe Balbi 	} while (--timeout && !(!is_on ^ !reg));
2679f2df679bSFelipe Balbi 
26802f71a89dSSelvarasu Ganesan 	if (saved_config) {
26812f71a89dSSelvarasu Ganesan 		reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
26822f71a89dSSelvarasu Ganesan 		reg |= saved_config;
26832f71a89dSSelvarasu Ganesan 		dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
26842f71a89dSSelvarasu Ganesan 	}
26852f71a89dSSelvarasu Ganesan 
268672246da4SFelipe Balbi 	if (!timeout)
26876f17f74bSPratyush Anand 		return -ETIMEDOUT;
268872246da4SFelipe Balbi 
26896f17f74bSPratyush Anand 	return 0;
269072246da4SFelipe Balbi }
269172246da4SFelipe Balbi 
2692ae7e8610SWesley Cheng static void dwc3_gadget_disable_irq(struct dwc3 *dwc);
2693ae7e8610SWesley Cheng static void __dwc3_gadget_stop(struct dwc3 *dwc);
2694a1383b35SWesley Cheng static int __dwc3_gadget_start(struct dwc3 *dwc);
2695ae7e8610SWesley Cheng 
dwc3_gadget_soft_disconnect(struct dwc3 * dwc)2696861c010aSThinh Nguyen static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
2697861c010aSThinh Nguyen {
26988f8034f4SThinh Nguyen 	unsigned long flags;
269902435a73SWesley Cheng 	int ret;
2700861c010aSThinh Nguyen 
27018f8034f4SThinh Nguyen 	spin_lock_irqsave(&dwc->lock, flags);
2702d3999e34SThinh Nguyen 	if (!dwc->pullups_connected) {
2703d3999e34SThinh Nguyen 		spin_unlock_irqrestore(&dwc->lock, flags);
2704d3999e34SThinh Nguyen 		return 0;
2705d3999e34SThinh Nguyen 	}
2706d3999e34SThinh Nguyen 
2707861c010aSThinh Nguyen 	dwc->connected = false;
2708861c010aSThinh Nguyen 
2709861c010aSThinh Nguyen 	/*
271002435a73SWesley Cheng 	 * Attempt to end pending SETUP status phase, and not wait for the
271102435a73SWesley Cheng 	 * function to do so.
2712c9668379SThinh Nguyen 	 */
2713e1ee8434SWesley Cheng 	if (dwc->delayed_status)
2714e1ee8434SWesley Cheng 		dwc3_ep0_send_delayed_status(dwc);
2715e1ee8434SWesley Cheng 
2716c9668379SThinh Nguyen 	/*
2717861c010aSThinh Nguyen 	 * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a
2718861c010aSThinh Nguyen 	 * Section 4.1.8 Table 4-7, it states that for a device-initiated
2719861c010aSThinh Nguyen 	 * disconnect, the SW needs to ensure that it sends "a DEPENDXFER
2720861c010aSThinh Nguyen 	 * command for any active transfers" before clearing the RunStop
2721861c010aSThinh Nguyen 	 * bit.
2722861c010aSThinh Nguyen 	 */
2723861c010aSThinh Nguyen 	dwc3_stop_active_transfers(dwc);
27248f8034f4SThinh Nguyen 	spin_unlock_irqrestore(&dwc->lock, flags);
2725861c010aSThinh Nguyen 
2726861c010aSThinh Nguyen 	/*
272702435a73SWesley Cheng 	 * Per databook, when we want to stop the gadget, if a control transfer
272802435a73SWesley Cheng 	 * is still in process, complete it and get the core into setup phase.
272902435a73SWesley Cheng 	 * In case the host is unresponsive to a SETUP transaction, forcefully
273002435a73SWesley Cheng 	 * stall the transfer, and move back to the SETUP phase, so that any
273102435a73SWesley Cheng 	 * pending endxfers can be executed.
273202435a73SWesley Cheng 	 */
273302435a73SWesley Cheng 	if (dwc->ep0state != EP0_SETUP_PHASE) {
273402435a73SWesley Cheng 		reinit_completion(&dwc->ep0_in_setup);
273502435a73SWesley Cheng 
273602435a73SWesley Cheng 		ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
273702435a73SWesley Cheng 				msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
273802435a73SWesley Cheng 		if (ret == 0) {
273902435a73SWesley Cheng 			dev_warn(dwc->dev, "wait for SETUP phase timed out\n");
274002435a73SWesley Cheng 			spin_lock_irqsave(&dwc->lock, flags);
27418f40fc08SWesley Cheng 			dwc3_ep0_reset_state(dwc);
274202435a73SWesley Cheng 			spin_unlock_irqrestore(&dwc->lock, flags);
274302435a73SWesley Cheng 		}
274402435a73SWesley Cheng 	}
274502435a73SWesley Cheng 
274602435a73SWesley Cheng 	/*
27478f8034f4SThinh Nguyen 	 * Note: if the GEVNTCOUNT indicates events in the event buffer, the
27488f8034f4SThinh Nguyen 	 * driver needs to acknowledge them before the controller can halt.
27498f8034f4SThinh Nguyen 	 * Simply let the interrupt handler acknowledges and handle the
27508f8034f4SThinh Nguyen 	 * remaining event generated by the controller while polling for
27518f8034f4SThinh Nguyen 	 * DSTS.DEVCTLHLT.
2752861c010aSThinh Nguyen 	 */
275339674be5SWesley Cheng 	ret = dwc3_gadget_run_stop(dwc, false);
275439674be5SWesley Cheng 
275539674be5SWesley Cheng 	/*
275639674be5SWesley Cheng 	 * Stop the gadget after controller is halted, so that if needed, the
275739674be5SWesley Cheng 	 * events to update EP0 state can still occur while the run/stop
275839674be5SWesley Cheng 	 * routine polls for the halted state.  DEVTEN is cleared as part of
275939674be5SWesley Cheng 	 * gadget stop.
276039674be5SWesley Cheng 	 */
276139674be5SWesley Cheng 	spin_lock_irqsave(&dwc->lock, flags);
276239674be5SWesley Cheng 	__dwc3_gadget_stop(dwc);
276339674be5SWesley Cheng 	spin_unlock_irqrestore(&dwc->lock, flags);
276439674be5SWesley Cheng 
276539674be5SWesley Cheng 	return ret;
2766861c010aSThinh Nguyen }
2767861c010aSThinh Nguyen 
dwc3_gadget_soft_connect(struct dwc3 * dwc)2768c8540870SRoger Quadros static int dwc3_gadget_soft_connect(struct dwc3 *dwc)
2769c8540870SRoger Quadros {
2770b4a4be84SRoger Quadros 	int ret;
2771b4a4be84SRoger Quadros 
2772c8540870SRoger Quadros 	/*
2773c8540870SRoger Quadros 	 * In the Synopsys DWC_usb31 1.90a programming guide section
2774c8540870SRoger Quadros 	 * 4.1.9, it specifies that for a reconnect after a
2775c8540870SRoger Quadros 	 * device-initiated disconnect requires a core soft reset
2776c8540870SRoger Quadros 	 * (DCTL.CSftRst) before enabling the run/stop bit.
2777c8540870SRoger Quadros 	 */
2778b4a4be84SRoger Quadros 	ret = dwc3_core_soft_reset(dwc);
2779b4a4be84SRoger Quadros 	if (ret)
2780b4a4be84SRoger Quadros 		return ret;
2781c8540870SRoger Quadros 
2782c8540870SRoger Quadros 	dwc3_event_buffers_setup(dwc);
2783c8540870SRoger Quadros 	__dwc3_gadget_start(dwc);
2784c8540870SRoger Quadros 	return dwc3_gadget_run_stop(dwc, true);
2785c8540870SRoger Quadros }
2786c8540870SRoger Quadros 
dwc3_gadget_pullup(struct usb_gadget * g,int is_on)278772246da4SFelipe Balbi static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
278872246da4SFelipe Balbi {
278972246da4SFelipe Balbi 	struct dwc3		*dwc = gadget_to_dwc(g);
27906f17f74bSPratyush Anand 	int			ret;
279172246da4SFelipe Balbi 
279272246da4SFelipe Balbi 	is_on = !!is_on;
279369e131d1SThinh Nguyen 
27948217f07aSWesley Cheng 	dwc->softconnect = is_on;
2795bb014736SBaolin Wang 
2796ae7e8610SWesley Cheng 	/*
2797cb10f68aSWesley Cheng 	 * Avoid issuing a runtime resume if the device is already in the
2798cb10f68aSWesley Cheng 	 * suspended state during gadget disconnect.  DWC3 gadget was already
2799cb10f68aSWesley Cheng 	 * halted/stopped during runtime suspend.
2800cb10f68aSWesley Cheng 	 */
2801cb10f68aSWesley Cheng 	if (!is_on) {
2802cb10f68aSWesley Cheng 		pm_runtime_barrier(dwc->dev);
2803cb10f68aSWesley Cheng 		if (pm_runtime_suspended(dwc->dev))
2804cb10f68aSWesley Cheng 			return 0;
2805cb10f68aSWesley Cheng 	}
2806cb10f68aSWesley Cheng 
2807cb10f68aSWesley Cheng 	/*
280877adb8bdSWesley Cheng 	 * Check the return value for successful resume, or error.  For a
280977adb8bdSWesley Cheng 	 * successful resume, the DWC3 runtime PM resume routine will handle
281077adb8bdSWesley Cheng 	 * the run stop sequence, so avoid duplicate operations here.
281177adb8bdSWesley Cheng 	 */
281277adb8bdSWesley Cheng 	ret = pm_runtime_get_sync(dwc->dev);
281377adb8bdSWesley Cheng 	if (!ret || ret < 0) {
281477adb8bdSWesley Cheng 		pm_runtime_put(dwc->dev);
2815c0aabed9SKrishna Kurapati 		if (ret < 0)
2816c0aabed9SKrishna Kurapati 			pm_runtime_set_suspended(dwc->dev);
2817c0aabed9SKrishna Kurapati 		return ret;
281877adb8bdSWesley Cheng 	}
281977adb8bdSWesley Cheng 
2820040f2dbdSWesley Cheng 	if (dwc->pullups_connected == is_on) {
2821040f2dbdSWesley Cheng 		pm_runtime_put(dwc->dev);
2822040f2dbdSWesley Cheng 		return 0;
2823040f2dbdSWesley Cheng 	}
2824040f2dbdSWesley Cheng 
28259711c67dSWesley Cheng 	synchronize_irq(dwc->irq_gadget);
28269711c67dSWesley Cheng 
2827c8540870SRoger Quadros 	if (!is_on)
2828861c010aSThinh Nguyen 		ret = dwc3_gadget_soft_disconnect(dwc);
2829c8540870SRoger Quadros 	else
2830c8540870SRoger Quadros 		ret = dwc3_gadget_soft_connect(dwc);
283182129373SWesley Cheng 
283277adb8bdSWesley Cheng 	pm_runtime_put(dwc->dev);
283372246da4SFelipe Balbi 
28346f17f74bSPratyush Anand 	return ret;
283572246da4SFelipe Balbi }
283672246da4SFelipe Balbi 
dwc3_gadget_enable_irq(struct dwc3 * dwc)28378698e2acSFelipe Balbi static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
28388698e2acSFelipe Balbi {
28398698e2acSFelipe Balbi 	u32			reg;
28408698e2acSFelipe Balbi 
28418698e2acSFelipe Balbi 	/* Enable all but Start and End of Frame IRQs */
2842132ee0daSThinh Nguyen 	reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
28438698e2acSFelipe Balbi 			DWC3_DEVTEN_CMDCMPLTEN |
28448698e2acSFelipe Balbi 			DWC3_DEVTEN_ERRTICERREN |
28458698e2acSFelipe Balbi 			DWC3_DEVTEN_WKUPEVTEN |
28468698e2acSFelipe Balbi 			DWC3_DEVTEN_CONNECTDONEEN |
28478698e2acSFelipe Balbi 			DWC3_DEVTEN_USBRSTEN |
28488698e2acSFelipe Balbi 			DWC3_DEVTEN_DISCONNEVTEN);
28498698e2acSFelipe Balbi 
28509af21dd6SThinh Nguyen 	if (DWC3_VER_IS_PRIOR(DWC3, 250A))
2851799e9dc8SFelipe Balbi 		reg |= DWC3_DEVTEN_ULSTCNGEN;
2852799e9dc8SFelipe Balbi 
2853d1d90dd2SJack Pham 	/* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */
2854d1d90dd2SJack Pham 	if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
28556f26ebb7SJack Pham 		reg |= DWC3_DEVTEN_U3L2L1SUSPEN;
2856d1d90dd2SJack Pham 
28578698e2acSFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
28588698e2acSFelipe Balbi }
28598698e2acSFelipe Balbi 
dwc3_gadget_disable_irq(struct dwc3 * dwc)28608698e2acSFelipe Balbi static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
28618698e2acSFelipe Balbi {
28628698e2acSFelipe Balbi 	/* mask all interrupts */
28638698e2acSFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
28648698e2acSFelipe Balbi }
28658698e2acSFelipe Balbi 
28668698e2acSFelipe Balbi static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
2867b15a762fSFelipe Balbi static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
28688698e2acSFelipe Balbi 
28694e99472bSFelipe Balbi /**
2870bfad65eeSFelipe Balbi  * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2871bfad65eeSFelipe Balbi  * @dwc: pointer to our context structure
28724e99472bSFelipe Balbi  *
28734e99472bSFelipe Balbi  * The following looks like complex but it's actually very simple. In order to
28744e99472bSFelipe Balbi  * calculate the number of packets we can burst at once on OUT transfers, we're
28754e99472bSFelipe Balbi  * gonna use RxFIFO size.
28764e99472bSFelipe Balbi  *
28774e99472bSFelipe Balbi  * To calculate RxFIFO size we need two numbers:
28784e99472bSFelipe Balbi  * MDWIDTH = size, in bits, of the internal memory bus
28794e99472bSFelipe Balbi  * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
28804e99472bSFelipe Balbi  *
28814e99472bSFelipe Balbi  * Given these two numbers, the formula is simple:
28824e99472bSFelipe Balbi  *
28834e99472bSFelipe Balbi  * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
28844e99472bSFelipe Balbi  *
28854e99472bSFelipe Balbi  * 24 bytes is for 3x SETUP packets
28864e99472bSFelipe Balbi  * 16 bytes is a clock domain crossing tolerance
28874e99472bSFelipe Balbi  *
28884e99472bSFelipe Balbi  * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
28894e99472bSFelipe Balbi  */
dwc3_gadget_setup_nump(struct dwc3 * dwc)28904e99472bSFelipe Balbi static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
28914e99472bSFelipe Balbi {
28924e99472bSFelipe Balbi 	u32 ram2_depth;
28934e99472bSFelipe Balbi 	u32 mdwidth;
28944e99472bSFelipe Balbi 	u32 nump;
28954e99472bSFelipe Balbi 	u32 reg;
28964e99472bSFelipe Balbi 
28974e99472bSFelipe Balbi 	ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
2898d00be779SThinh Nguyen 	mdwidth = dwc3_mdwidth(dwc);
28994e99472bSFelipe Balbi 
29004e99472bSFelipe Balbi 	nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
29014e99472bSFelipe Balbi 	nump = min_t(u32, nump, 16);
29024e99472bSFelipe Balbi 
29034e99472bSFelipe Balbi 	/* update NumP */
29044e99472bSFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
29054e99472bSFelipe Balbi 	reg &= ~DWC3_DCFG_NUMP_MASK;
29064e99472bSFelipe Balbi 	reg |= nump << DWC3_DCFG_NUMP_SHIFT;
29074e99472bSFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
29084e99472bSFelipe Balbi }
29094e99472bSFelipe Balbi 
__dwc3_gadget_start(struct dwc3 * dwc)2910d7be2952SFelipe Balbi static int __dwc3_gadget_start(struct dwc3 *dwc)
291172246da4SFelipe Balbi {
291272246da4SFelipe Balbi 	struct dwc3_ep		*dep;
291372246da4SFelipe Balbi 	int			ret = 0;
291472246da4SFelipe Balbi 	u32			reg;
291572246da4SFelipe Balbi 
2916cf40b86bSJohn Youn 	/*
2917cf40b86bSJohn Youn 	 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
2918cf40b86bSJohn Youn 	 * the core supports IMOD, disable it.
2919cf40b86bSJohn Youn 	 */
2920cf40b86bSJohn Youn 	if (dwc->imod_interval) {
2921cf40b86bSJohn Youn 		dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
2922cf40b86bSJohn Youn 		dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
2923cf40b86bSJohn Youn 	} else if (dwc3_has_imod(dwc)) {
2924cf40b86bSJohn Youn 		dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
2925cf40b86bSJohn Youn 	}
2926cf40b86bSJohn Youn 
29272a58f9c1SFelipe Balbi 	/*
29282a58f9c1SFelipe Balbi 	 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
29292a58f9c1SFelipe Balbi 	 * field instead of letting dwc3 itself calculate that automatically.
29302a58f9c1SFelipe Balbi 	 *
29312a58f9c1SFelipe Balbi 	 * This way, we maximize the chances that we'll be able to get several
29322a58f9c1SFelipe Balbi 	 * bursts of data without going through any sort of endpoint throttling.
29332a58f9c1SFelipe Balbi 	 */
29342a58f9c1SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
29359af21dd6SThinh Nguyen 	if (DWC3_IP_IS(DWC3))
29362a58f9c1SFelipe Balbi 		reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
29379af21dd6SThinh Nguyen 	else
29389af21dd6SThinh Nguyen 		reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
293901b0e2ccSThinh Nguyen 
29402a58f9c1SFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
29412a58f9c1SFelipe Balbi 
29424e99472bSFelipe Balbi 	dwc3_gadget_setup_nump(dwc);
29434e99472bSFelipe Balbi 
2944e66bbfb0SThinh Nguyen 	/*
2945e66bbfb0SThinh Nguyen 	 * Currently the controller handles single stream only. So, Ignore
2946e66bbfb0SThinh Nguyen 	 * Packet Pending bit for stream selection and don't search for another
2947e66bbfb0SThinh Nguyen 	 * stream if the host sends Data Packet with PP=0 (for OUT direction) or
2948e66bbfb0SThinh Nguyen 	 * ACK with NumP=0 and PP=0 (for IN direction). This slightly improves
2949e66bbfb0SThinh Nguyen 	 * the stream performance.
2950e66bbfb0SThinh Nguyen 	 */
2951e66bbfb0SThinh Nguyen 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2952e66bbfb0SThinh Nguyen 	reg |= DWC3_DCFG_IGNSTRMPP;
2953e66bbfb0SThinh Nguyen 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2954e66bbfb0SThinh Nguyen 
2955666f3de7SThinh Nguyen 	/* Enable MST by default if the device is capable of MST */
2956666f3de7SThinh Nguyen 	if (DWC3_MST_CAPABLE(&dwc->hwparams)) {
2957666f3de7SThinh Nguyen 		reg = dwc3_readl(dwc->regs, DWC3_DCFG1);
2958666f3de7SThinh Nguyen 		reg &= ~DWC3_DCFG1_DIS_MST_ENH;
2959666f3de7SThinh Nguyen 		dwc3_writel(dwc->regs, DWC3_DCFG1, reg);
2960666f3de7SThinh Nguyen 	}
2961666f3de7SThinh Nguyen 
296272246da4SFelipe Balbi 	/* Start with SuperSpeed Default */
296372246da4SFelipe Balbi 	dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
296472246da4SFelipe Balbi 
296565b1f311SThinh Nguyen 	ret = dwc3_gadget_start_config(dwc, 0);
296665b1f311SThinh Nguyen 	if (ret) {
296765b1f311SThinh Nguyen 		dev_err(dwc->dev, "failed to config endpoints\n");
296865b1f311SThinh Nguyen 		return ret;
296965b1f311SThinh Nguyen 	}
297065b1f311SThinh Nguyen 
297172246da4SFelipe Balbi 	dep = dwc->eps[0];
2972dff98184SWesley Cheng 	dep->flags = 0;
2973a2d23f08SFelipe Balbi 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
297472246da4SFelipe Balbi 	if (ret) {
297572246da4SFelipe Balbi 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2976d7be2952SFelipe Balbi 		goto err0;
297772246da4SFelipe Balbi 	}
297872246da4SFelipe Balbi 
297972246da4SFelipe Balbi 	dep = dwc->eps[1];
2980dff98184SWesley Cheng 	dep->flags = 0;
2981a2d23f08SFelipe Balbi 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
298272246da4SFelipe Balbi 	if (ret) {
298372246da4SFelipe Balbi 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2984d7be2952SFelipe Balbi 		goto err1;
298572246da4SFelipe Balbi 	}
298672246da4SFelipe Balbi 
298772246da4SFelipe Balbi 	/* begin to receive SETUP packets */
2988c7fcdeb2SFelipe Balbi 	dwc->ep0state = EP0_SETUP_PHASE;
29899d778f0cSMayank Rana 	dwc->ep0_bounced = false;
299088b1bb1fSZeng Tao 	dwc->link_state = DWC3_LINK_STATE_SS_DIS;
29914a1e25c0SWesley Cheng 	dwc->delayed_status = false;
299272246da4SFelipe Balbi 	dwc3_ep0_out_start(dwc);
299372246da4SFelipe Balbi 
29948698e2acSFelipe Balbi 	dwc3_gadget_enable_irq(dwc);
2995000f9944SThinh Nguyen 	dwc3_enable_susphy(dwc, true);
29968698e2acSFelipe Balbi 
2997d7be2952SFelipe Balbi 	return 0;
2998d7be2952SFelipe Balbi 
2999d7be2952SFelipe Balbi err1:
3000d7be2952SFelipe Balbi 	__dwc3_gadget_ep_disable(dwc->eps[0]);
3001d7be2952SFelipe Balbi 
3002d7be2952SFelipe Balbi err0:
3003d7be2952SFelipe Balbi 	return ret;
3004d7be2952SFelipe Balbi }
3005d7be2952SFelipe Balbi 
dwc3_gadget_start(struct usb_gadget * g,struct usb_gadget_driver * driver)3006d7be2952SFelipe Balbi static int dwc3_gadget_start(struct usb_gadget *g,
3007d7be2952SFelipe Balbi 		struct usb_gadget_driver *driver)
3008d7be2952SFelipe Balbi {
3009d7be2952SFelipe Balbi 	struct dwc3		*dwc = gadget_to_dwc(g);
3010d7be2952SFelipe Balbi 	unsigned long		flags;
30118cf9045bSThinh Nguyen 	int			ret;
3012d7be2952SFelipe Balbi 	int			irq;
3013d7be2952SFelipe Balbi 
30149522def4SRoger Quadros 	irq = dwc->irq_gadget;
3015d7be2952SFelipe Balbi 	ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
3016d7be2952SFelipe Balbi 			IRQF_SHARED, "dwc3", dwc->ev_buf);
3017d7be2952SFelipe Balbi 	if (ret) {
3018d7be2952SFelipe Balbi 		dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
3019d7be2952SFelipe Balbi 				irq, ret);
30208cf9045bSThinh Nguyen 		return ret;
3021d7be2952SFelipe Balbi 	}
3022d7be2952SFelipe Balbi 
3023d7be2952SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
3024d7be2952SFelipe Balbi 	dwc->gadget_driver	= driver;
302572246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
302672246da4SFelipe Balbi 
3027fd2304f4SThinh Nguyen 	if (dwc->sys_wakeup)
3028fd2304f4SThinh Nguyen 		device_wakeup_enable(dwc->sysdev);
3029fd2304f4SThinh Nguyen 
303072246da4SFelipe Balbi 	return 0;
303172246da4SFelipe Balbi }
303272246da4SFelipe Balbi 
__dwc3_gadget_stop(struct dwc3 * dwc)3033d7be2952SFelipe Balbi static void __dwc3_gadget_stop(struct dwc3 *dwc)
3034d7be2952SFelipe Balbi {
3035d7be2952SFelipe Balbi 	dwc3_gadget_disable_irq(dwc);
3036d7be2952SFelipe Balbi 	__dwc3_gadget_ep_disable(dwc->eps[0]);
3037d7be2952SFelipe Balbi 	__dwc3_gadget_ep_disable(dwc->eps[1]);
3038d7be2952SFelipe Balbi }
3039d7be2952SFelipe Balbi 
dwc3_gadget_stop(struct usb_gadget * g)304022835b80SFelipe Balbi static int dwc3_gadget_stop(struct usb_gadget *g)
304172246da4SFelipe Balbi {
304272246da4SFelipe Balbi 	struct dwc3		*dwc = gadget_to_dwc(g);
304372246da4SFelipe Balbi 	unsigned long		flags;
304472246da4SFelipe Balbi 
3045fd2304f4SThinh Nguyen 	if (dwc->sys_wakeup)
3046fd2304f4SThinh Nguyen 		device_wakeup_disable(dwc->sysdev);
3047fd2304f4SThinh Nguyen 
304872246da4SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
304972246da4SFelipe Balbi 	dwc->gadget_driver	= NULL;
30509f607a30SWesley Cheng 	dwc->max_cfg_eps = 0;
305172246da4SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
305272246da4SFelipe Balbi 
30533f308d17SFelipe Balbi 	free_irq(dwc->irq_gadget, dwc->ev_buf);
3054b0d7ffd4SFelipe Balbi 
305572246da4SFelipe Balbi 	return 0;
305672246da4SFelipe Balbi }
3057802fde98SPaul Zimmerman 
dwc3_gadget_config_params(struct usb_gadget * g,struct usb_dcd_config_params * params)3058729dcffdSAnurag Kumar Vulisha static void dwc3_gadget_config_params(struct usb_gadget *g,
3059729dcffdSAnurag Kumar Vulisha 				      struct usb_dcd_config_params *params)
3060729dcffdSAnurag Kumar Vulisha {
3061729dcffdSAnurag Kumar Vulisha 	struct dwc3		*dwc = gadget_to_dwc(g);
3062729dcffdSAnurag Kumar Vulisha 
306354fb5ba6SThinh Nguyen 	params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED;
306454fb5ba6SThinh Nguyen 	params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED;
306554fb5ba6SThinh Nguyen 
306654fb5ba6SThinh Nguyen 	/* Recommended BESL */
306754fb5ba6SThinh Nguyen 	if (!dwc->dis_enblslpm_quirk) {
306817b63704SThinh Nguyen 		/*
306917b63704SThinh Nguyen 		 * If the recommended BESL baseline is 0 or if the BESL deep is
307017b63704SThinh Nguyen 		 * less than 2, Microsoft's Windows 10 host usb stack will issue
307117b63704SThinh Nguyen 		 * a usb reset immediately after it receives the extended BOS
307217b63704SThinh Nguyen 		 * descriptor and the enumeration will fail. To maintain
307317b63704SThinh Nguyen 		 * compatibility with the Windows' usb stack, let's set the
307417b63704SThinh Nguyen 		 * recommended BESL baseline to 1 and clamp the BESL deep to be
307517b63704SThinh Nguyen 		 * within 2 to 15.
307617b63704SThinh Nguyen 		 */
307717b63704SThinh Nguyen 		params->besl_baseline = 1;
307854fb5ba6SThinh Nguyen 		if (dwc->is_utmi_l1_suspend)
307917b63704SThinh Nguyen 			params->besl_deep =
308017b63704SThinh Nguyen 				clamp_t(u8, dwc->hird_threshold, 2, 15);
308154fb5ba6SThinh Nguyen 	}
308254fb5ba6SThinh Nguyen 
3083729dcffdSAnurag Kumar Vulisha 	/* U1 Device exit Latency */
3084729dcffdSAnurag Kumar Vulisha 	if (dwc->dis_u1_entry_quirk)
3085729dcffdSAnurag Kumar Vulisha 		params->bU1devExitLat = 0;
3086729dcffdSAnurag Kumar Vulisha 	else
3087729dcffdSAnurag Kumar Vulisha 		params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT;
3088729dcffdSAnurag Kumar Vulisha 
3089729dcffdSAnurag Kumar Vulisha 	/* U2 Device exit Latency */
3090729dcffdSAnurag Kumar Vulisha 	if (dwc->dis_u2_entry_quirk)
3091729dcffdSAnurag Kumar Vulisha 		params->bU2DevExitLat = 0;
3092729dcffdSAnurag Kumar Vulisha 	else
3093729dcffdSAnurag Kumar Vulisha 		params->bU2DevExitLat =
3094729dcffdSAnurag Kumar Vulisha 				cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT);
3095729dcffdSAnurag Kumar Vulisha }
3096729dcffdSAnurag Kumar Vulisha 
dwc3_gadget_set_speed(struct usb_gadget * g,enum usb_device_speed speed)30977d8d0639SFelipe Balbi static void dwc3_gadget_set_speed(struct usb_gadget *g,
30987d8d0639SFelipe Balbi 				  enum usb_device_speed speed)
30997d8d0639SFelipe Balbi {
31007d8d0639SFelipe Balbi 	struct dwc3		*dwc = gadget_to_dwc(g);
31017d8d0639SFelipe Balbi 	unsigned long		flags;
31027d8d0639SFelipe Balbi 
31037d8d0639SFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
31047c9a2598SWesley Cheng 	dwc->gadget_max_speed = speed;
31057d8d0639SFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
31067d8d0639SFelipe Balbi }
31077d8d0639SFelipe Balbi 
dwc3_gadget_set_ssp_rate(struct usb_gadget * g,enum usb_ssp_rate rate)3108072cab8aSThinh Nguyen static void dwc3_gadget_set_ssp_rate(struct usb_gadget *g,
3109072cab8aSThinh Nguyen 				     enum usb_ssp_rate rate)
3110072cab8aSThinh Nguyen {
3111072cab8aSThinh Nguyen 	struct dwc3		*dwc = gadget_to_dwc(g);
3112072cab8aSThinh Nguyen 	unsigned long		flags;
3113072cab8aSThinh Nguyen 
3114072cab8aSThinh Nguyen 	spin_lock_irqsave(&dwc->lock, flags);
3115cdb651b6SThinh Nguyen 	dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS;
3116072cab8aSThinh Nguyen 	dwc->gadget_ssp_rate = rate;
3117072cab8aSThinh Nguyen 	spin_unlock_irqrestore(&dwc->lock, flags);
3118072cab8aSThinh Nguyen }
3119072cab8aSThinh Nguyen 
dwc3_gadget_vbus_draw(struct usb_gadget * g,unsigned int mA)312082c46b8eSWesley Cheng static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA)
312182c46b8eSWesley Cheng {
312282c46b8eSWesley Cheng 	struct dwc3		*dwc = gadget_to_dwc(g);
312399288de3SRay Chi 	union power_supply_propval	val = {0};
312499288de3SRay Chi 	int				ret;
312582c46b8eSWesley Cheng 
312682c46b8eSWesley Cheng 	if (dwc->usb2_phy)
312782c46b8eSWesley Cheng 		return usb_phy_set_power(dwc->usb2_phy, mA);
312882c46b8eSWesley Cheng 
312999288de3SRay Chi 	if (!dwc->usb_psy)
313099288de3SRay Chi 		return -EOPNOTSUPP;
313199288de3SRay Chi 
31328a5b5c3cSRay Chi 	val.intval = 1000 * mA;
313399288de3SRay Chi 	ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
313499288de3SRay Chi 
313599288de3SRay Chi 	return ret;
313682c46b8eSWesley Cheng }
313782c46b8eSWesley Cheng 
31389f607a30SWesley Cheng /**
31399f607a30SWesley Cheng  * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
31409f607a30SWesley Cheng  * @g: pointer to the USB gadget
31419f607a30SWesley Cheng  *
31429f607a30SWesley Cheng  * Used to record the maximum number of endpoints being used in a USB composite
31439f607a30SWesley Cheng  * device. (across all configurations)  This is to be used in the calculation
31449f607a30SWesley Cheng  * of the TXFIFO sizes when resizing internal memory for individual endpoints.
31459f607a30SWesley Cheng  * It will help ensured that the resizing logic reserves enough space for at
31469f607a30SWesley Cheng  * least one max packet.
31479f607a30SWesley Cheng  */
dwc3_gadget_check_config(struct usb_gadget * g)31489f607a30SWesley Cheng static int dwc3_gadget_check_config(struct usb_gadget *g)
31499f607a30SWesley Cheng {
31509f607a30SWesley Cheng 	struct dwc3 *dwc = gadget_to_dwc(g);
31519f607a30SWesley Cheng 	struct usb_ep *ep;
31529f607a30SWesley Cheng 	int fifo_size = 0;
3153106740e9SSelvarasu Ganesan 	int ram_depth;
31549f607a30SWesley Cheng 	int ep_num = 0;
31559f607a30SWesley Cheng 
31569f607a30SWesley Cheng 	if (!dwc->do_fifo_resize)
31579f607a30SWesley Cheng 		return 0;
31589f607a30SWesley Cheng 
31599f607a30SWesley Cheng 	list_for_each_entry(ep, &g->ep_list, ep_list) {
31609f607a30SWesley Cheng 		/* Only interested in the IN endpoints */
31619f607a30SWesley Cheng 		if (ep->claimed && (ep->address & USB_DIR_IN))
31629f607a30SWesley Cheng 			ep_num++;
31639f607a30SWesley Cheng 	}
31649f607a30SWesley Cheng 
31659f607a30SWesley Cheng 	if (ep_num <= dwc->max_cfg_eps)
31669f607a30SWesley Cheng 		return 0;
31679f607a30SWesley Cheng 
31689f607a30SWesley Cheng 	/* Update the max number of eps in the composition */
31699f607a30SWesley Cheng 	dwc->max_cfg_eps = ep_num;
31709f607a30SWesley Cheng 
31719f607a30SWesley Cheng 	fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps);
31729f607a30SWesley Cheng 	/* Based on the equation, increment by one for every ep */
31739f607a30SWesley Cheng 	fifo_size += dwc->max_cfg_eps;
31749f607a30SWesley Cheng 
31759f607a30SWesley Cheng 	/* Check if we can fit a single fifo per endpoint */
3176106740e9SSelvarasu Ganesan 	ram_depth = dwc3_gadget_calc_ram_depth(dwc);
3177106740e9SSelvarasu Ganesan 	if (fifo_size > ram_depth)
31789f607a30SWesley Cheng 		return -ENOMEM;
31799f607a30SWesley Cheng 
31809f607a30SWesley Cheng 	return 0;
31819f607a30SWesley Cheng }
31829f607a30SWesley Cheng 
dwc3_gadget_async_callbacks(struct usb_gadget * g,bool enable)318340edb522SLinyu Yuan static void dwc3_gadget_async_callbacks(struct usb_gadget *g, bool enable)
318440edb522SLinyu Yuan {
318540edb522SLinyu Yuan 	struct dwc3		*dwc = gadget_to_dwc(g);
318640edb522SLinyu Yuan 	unsigned long		flags;
318740edb522SLinyu Yuan 
318840edb522SLinyu Yuan 	spin_lock_irqsave(&dwc->lock, flags);
318940edb522SLinyu Yuan 	dwc->async_callbacks = enable;
319040edb522SLinyu Yuan 	spin_unlock_irqrestore(&dwc->lock, flags);
319140edb522SLinyu Yuan }
319240edb522SLinyu Yuan 
319372246da4SFelipe Balbi static const struct usb_gadget_ops dwc3_gadget_ops = {
319472246da4SFelipe Balbi 	.get_frame		= dwc3_gadget_get_frame,
319572246da4SFelipe Balbi 	.wakeup			= dwc3_gadget_wakeup,
319692c08a84SElson Roy Serrao 	.func_wakeup		= dwc3_gadget_func_wakeup,
319704716168SElson Roy Serrao 	.set_remote_wakeup	= dwc3_gadget_set_remote_wakeup,
319872246da4SFelipe Balbi 	.set_selfpowered	= dwc3_gadget_set_selfpowered,
319972246da4SFelipe Balbi 	.pullup			= dwc3_gadget_pullup,
320072246da4SFelipe Balbi 	.udc_start		= dwc3_gadget_start,
320172246da4SFelipe Balbi 	.udc_stop		= dwc3_gadget_stop,
32027d8d0639SFelipe Balbi 	.udc_set_speed		= dwc3_gadget_set_speed,
3203072cab8aSThinh Nguyen 	.udc_set_ssp_rate	= dwc3_gadget_set_ssp_rate,
3204729dcffdSAnurag Kumar Vulisha 	.get_config_params	= dwc3_gadget_config_params,
320582c46b8eSWesley Cheng 	.vbus_draw		= dwc3_gadget_vbus_draw,
32069f607a30SWesley Cheng 	.check_config		= dwc3_gadget_check_config,
320740edb522SLinyu Yuan 	.udc_async_callbacks	= dwc3_gadget_async_callbacks,
320872246da4SFelipe Balbi };
320972246da4SFelipe Balbi 
321072246da4SFelipe Balbi /* -------------------------------------------------------------------------- */
321172246da4SFelipe Balbi 
dwc3_gadget_init_control_endpoint(struct dwc3_ep * dep)32128f1c99cdSFelipe Balbi static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
321372246da4SFelipe Balbi {
32148f1c99cdSFelipe Balbi 	struct dwc3 *dwc = dep->dwc;
321572246da4SFelipe Balbi 
3216e117e742SRobert Baldyga 	usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
32176048e4c6SPratyush Anand 	dep->endpoint.maxburst = 1;
321872246da4SFelipe Balbi 	dep->endpoint.ops = &dwc3_gadget_ep0_ops;
32198f1c99cdSFelipe Balbi 	if (!dep->direction)
3220e81a7018SPeter Chen 		dwc->gadget->ep0 = &dep->endpoint;
32218f1c99cdSFelipe Balbi 
32228f1c99cdSFelipe Balbi 	dep->endpoint.caps.type_control = true;
32238f1c99cdSFelipe Balbi 
32248f1c99cdSFelipe Balbi 	return 0;
32258f1c99cdSFelipe Balbi }
32268f1c99cdSFelipe Balbi 
dwc3_gadget_init_in_endpoint(struct dwc3_ep * dep)32278f1c99cdSFelipe Balbi static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
32288f1c99cdSFelipe Balbi {
32298f1c99cdSFelipe Balbi 	struct dwc3 *dwc = dep->dwc;
3230d00be779SThinh Nguyen 	u32 mdwidth;
323128781789SFelipe Balbi 	int size;
32329c1e9169SWesley Cheng 	int maxpacket;
323328781789SFelipe Balbi 
3234d00be779SThinh Nguyen 	mdwidth = dwc3_mdwidth(dwc);
32354244ba02SThinh Nguyen 
323628781789SFelipe Balbi 	/* MDWIDTH is represented in bits, we need it in bytes */
323728781789SFelipe Balbi 	mdwidth /= 8;
323828781789SFelipe Balbi 
32398f1c99cdSFelipe Balbi 	size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
32409af21dd6SThinh Nguyen 	if (DWC3_IP_IS(DWC3))
3241586f4335SThinh Nguyen 		size = DWC3_GTXFIFOSIZ_TXFDEP(size);
32429af21dd6SThinh Nguyen 	else
32439af21dd6SThinh Nguyen 		size = DWC31_GTXFIFOSIZ_TXFDEP(size);
324428781789SFelipe Balbi 
324528781789SFelipe Balbi 	/*
32469c1e9169SWesley Cheng 	 * maxpacket size is determined as part of the following, after assuming
32479c1e9169SWesley Cheng 	 * a mult value of one maxpacket:
32489c1e9169SWesley Cheng 	 * DWC3 revision 280A and prior:
32499c1e9169SWesley Cheng 	 * fifo_size = mult * (max_packet / mdwidth) + 1;
32509c1e9169SWesley Cheng 	 * maxpacket = mdwidth * (fifo_size - 1);
32519c1e9169SWesley Cheng 	 *
32529c1e9169SWesley Cheng 	 * DWC3 revision 290A and onwards:
32539c1e9169SWesley Cheng 	 * fifo_size = mult * ((max_packet + mdwidth)/mdwidth + 1) + 1
32549c1e9169SWesley Cheng 	 * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth;
325528781789SFelipe Balbi 	 */
32569c1e9169SWesley Cheng 	if (DWC3_VER_IS_PRIOR(DWC3, 290A))
32579c1e9169SWesley Cheng 		maxpacket = mdwidth * (size - 1);
3258d94ea531SThinh Nguyen 	else
32599c1e9169SWesley Cheng 		maxpacket = mdwidth * ((size - 1) - 1) - mdwidth;
326028781789SFelipe Balbi 
32619c1e9169SWesley Cheng 	/* Functionally, space for one max packet is sufficient */
32629c1e9169SWesley Cheng 	size = min_t(int, maxpacket, 1024);
326328781789SFelipe Balbi 	usb_ep_set_maxpacket_limit(&dep->endpoint, size);
326428781789SFelipe Balbi 
3265e0a93d98SThinh Nguyen 	dep->endpoint.max_streams = 16;
326628781789SFelipe Balbi 	dep->endpoint.ops = &dwc3_gadget_ep_ops;
326728781789SFelipe Balbi 	list_add_tail(&dep->endpoint.ep_list,
3268e81a7018SPeter Chen 			&dwc->gadget->ep_list);
32698f1c99cdSFelipe Balbi 	dep->endpoint.caps.type_iso = true;
32708f1c99cdSFelipe Balbi 	dep->endpoint.caps.type_bulk = true;
32718f1c99cdSFelipe Balbi 	dep->endpoint.caps.type_int = true;
327228781789SFelipe Balbi 
32738f1c99cdSFelipe Balbi 	return dwc3_alloc_trb_pool(dep);
32748f1c99cdSFelipe Balbi }
32758f1c99cdSFelipe Balbi 
dwc3_gadget_init_out_endpoint(struct dwc3_ep * dep)32768f1c99cdSFelipe Balbi static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
32778f1c99cdSFelipe Balbi {
32788f1c99cdSFelipe Balbi 	struct dwc3 *dwc = dep->dwc;
3279d00be779SThinh Nguyen 	u32 mdwidth;
3280d94ea531SThinh Nguyen 	int size;
328172246da4SFelipe Balbi 
3282d00be779SThinh Nguyen 	mdwidth = dwc3_mdwidth(dwc);
3283d94ea531SThinh Nguyen 
3284d94ea531SThinh Nguyen 	/* MDWIDTH is represented in bits, convert to bytes */
3285d94ea531SThinh Nguyen 	mdwidth /= 8;
3286d94ea531SThinh Nguyen 
3287d94ea531SThinh Nguyen 	/* All OUT endpoints share a single RxFIFO space */
3288d94ea531SThinh Nguyen 	size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0));
32899af21dd6SThinh Nguyen 	if (DWC3_IP_IS(DWC3))
3290d94ea531SThinh Nguyen 		size = DWC3_GRXFIFOSIZ_RXFDEP(size);
32919af21dd6SThinh Nguyen 	else
32929af21dd6SThinh Nguyen 		size = DWC31_GRXFIFOSIZ_RXFDEP(size);
3293d94ea531SThinh Nguyen 
3294d94ea531SThinh Nguyen 	/* FIFO depth is in MDWDITH bytes */
3295d94ea531SThinh Nguyen 	size *= mdwidth;
3296d94ea531SThinh Nguyen 
3297d94ea531SThinh Nguyen 	/*
3298d94ea531SThinh Nguyen 	 * To meet performance requirement, a minimum recommended RxFIFO size
3299d94ea531SThinh Nguyen 	 * is defined as follow:
3300d94ea531SThinh Nguyen 	 * RxFIFO size >= (3 x MaxPacketSize) +
3301d94ea531SThinh Nguyen 	 * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin)
3302d94ea531SThinh Nguyen 	 *
3303d94ea531SThinh Nguyen 	 * Then calculate the max packet limit as below.
3304d94ea531SThinh Nguyen 	 */
3305d94ea531SThinh Nguyen 	size -= (3 * 8) + 16;
3306d94ea531SThinh Nguyen 	if (size < 0)
3307d94ea531SThinh Nguyen 		size = 0;
3308d94ea531SThinh Nguyen 	else
3309d94ea531SThinh Nguyen 		size /= 3;
3310d94ea531SThinh Nguyen 
3311d94ea531SThinh Nguyen 	usb_ep_set_maxpacket_limit(&dep->endpoint, size);
3312e0a93d98SThinh Nguyen 	dep->endpoint.max_streams = 16;
331372246da4SFelipe Balbi 	dep->endpoint.ops = &dwc3_gadget_ep_ops;
331472246da4SFelipe Balbi 	list_add_tail(&dep->endpoint.ep_list,
3315e81a7018SPeter Chen 			&dwc->gadget->ep_list);
3316a474d3b7SRobert Baldyga 	dep->endpoint.caps.type_iso = true;
3317a474d3b7SRobert Baldyga 	dep->endpoint.caps.type_bulk = true;
3318a474d3b7SRobert Baldyga 	dep->endpoint.caps.type_int = true;
33198f1c99cdSFelipe Balbi 
33208f1c99cdSFelipe Balbi 	return dwc3_alloc_trb_pool(dep);
3321a474d3b7SRobert Baldyga }
3322a474d3b7SRobert Baldyga 
dwc3_gadget_init_endpoint(struct dwc3 * dwc,u8 epnum)33238f1c99cdSFelipe Balbi static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
33248f1c99cdSFelipe Balbi {
33258f1c99cdSFelipe Balbi 	struct dwc3_ep			*dep;
33268f1c99cdSFelipe Balbi 	bool				direction = epnum & 1;
33278f1c99cdSFelipe Balbi 	int				ret;
33288f1c99cdSFelipe Balbi 	u8				num = epnum >> 1;
33298f1c99cdSFelipe Balbi 
33308f1c99cdSFelipe Balbi 	dep = kzalloc(sizeof(*dep), GFP_KERNEL);
33318f1c99cdSFelipe Balbi 	if (!dep)
33328f1c99cdSFelipe Balbi 		return -ENOMEM;
33338f1c99cdSFelipe Balbi 
33348f1c99cdSFelipe Balbi 	dep->dwc = dwc;
33358f1c99cdSFelipe Balbi 	dep->number = epnum;
33368f1c99cdSFelipe Balbi 	dep->direction = direction;
33378f1c99cdSFelipe Balbi 	dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
33388f1c99cdSFelipe Balbi 	dwc->eps[epnum] = dep;
3339d92021f6SThinh Nguyen 	dep->combo_num = 0;
3340d92021f6SThinh Nguyen 	dep->start_cmd_status = 0;
33418f1c99cdSFelipe Balbi 
33428f1c99cdSFelipe Balbi 	snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
33438f1c99cdSFelipe Balbi 			direction ? "in" : "out");
33448f1c99cdSFelipe Balbi 
33458f1c99cdSFelipe Balbi 	dep->endpoint.name = dep->name;
33468f1c99cdSFelipe Balbi 
33478f1c99cdSFelipe Balbi 	if (!(dep->number > 1)) {
33488f1c99cdSFelipe Balbi 		dep->endpoint.desc = &dwc3_gadget_ep0_desc;
33498f1c99cdSFelipe Balbi 		dep->endpoint.comp_desc = NULL;
33508f1c99cdSFelipe Balbi 	}
33518f1c99cdSFelipe Balbi 
33528f1c99cdSFelipe Balbi 	if (num == 0)
33538f1c99cdSFelipe Balbi 		ret = dwc3_gadget_init_control_endpoint(dep);
33548f1c99cdSFelipe Balbi 	else if (direction)
33558f1c99cdSFelipe Balbi 		ret = dwc3_gadget_init_in_endpoint(dep);
33568f1c99cdSFelipe Balbi 	else
33578f1c99cdSFelipe Balbi 		ret = dwc3_gadget_init_out_endpoint(dep);
33588f1c99cdSFelipe Balbi 
33598f1c99cdSFelipe Balbi 	if (ret)
33608f1c99cdSFelipe Balbi 		return ret;
33618f1c99cdSFelipe Balbi 
336247d3946eSBryan O'Donoghue 	dep->endpoint.caps.dir_in = direction;
3363a474d3b7SRobert Baldyga 	dep->endpoint.caps.dir_out = !direction;
3364a474d3b7SRobert Baldyga 
3365aa3342c8SFelipe Balbi 	INIT_LIST_HEAD(&dep->pending_list);
3366aa3342c8SFelipe Balbi 	INIT_LIST_HEAD(&dep->started_list);
3367d5443bbfSFelipe Balbi 	INIT_LIST_HEAD(&dep->cancelled_list);
33688f1c99cdSFelipe Balbi 
33695ff90af9SJack Pham 	dwc3_debugfs_create_endpoint_dir(dep);
33705ff90af9SJack Pham 
33718f1c99cdSFelipe Balbi 	return 0;
33728f1c99cdSFelipe Balbi }
33738f1c99cdSFelipe Balbi 
dwc3_gadget_init_endpoints(struct dwc3 * dwc,u8 total)33748f1c99cdSFelipe Balbi static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
33758f1c99cdSFelipe Balbi {
33768f1c99cdSFelipe Balbi 	u8				epnum;
33778f1c99cdSFelipe Balbi 
3378e81a7018SPeter Chen 	INIT_LIST_HEAD(&dwc->gadget->ep_list);
33798f1c99cdSFelipe Balbi 
33808f1c99cdSFelipe Balbi 	for (epnum = 0; epnum < total; epnum++) {
33818f1c99cdSFelipe Balbi 		int			ret;
33828f1c99cdSFelipe Balbi 
33838f1c99cdSFelipe Balbi 		ret = dwc3_gadget_init_endpoint(dwc, epnum);
33848f1c99cdSFelipe Balbi 		if (ret)
33858f1c99cdSFelipe Balbi 			return ret;
338672246da4SFelipe Balbi 	}
338772246da4SFelipe Balbi 
338872246da4SFelipe Balbi 	return 0;
338972246da4SFelipe Balbi }
339072246da4SFelipe Balbi 
dwc3_gadget_free_endpoints(struct dwc3 * dwc)339172246da4SFelipe Balbi static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
339272246da4SFelipe Balbi {
339372246da4SFelipe Balbi 	struct dwc3_ep			*dep;
339472246da4SFelipe Balbi 	u8				epnum;
339572246da4SFelipe Balbi 
339672246da4SFelipe Balbi 	for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
339772246da4SFelipe Balbi 		dep = dwc->eps[epnum];
33986a1e3ef4SFelipe Balbi 		if (!dep)
33996a1e3ef4SFelipe Balbi 			continue;
34005bf8fae3SGeorge Cherian 		/*
34015bf8fae3SGeorge Cherian 		 * Physical endpoints 0 and 1 are special; they form the
34025bf8fae3SGeorge Cherian 		 * bi-directional USB endpoint 0.
34035bf8fae3SGeorge Cherian 		 *
34045bf8fae3SGeorge Cherian 		 * For those two physical endpoints, we don't allocate a TRB
34055bf8fae3SGeorge Cherian 		 * pool nor do we add them the endpoints list. Due to that, we
34065bf8fae3SGeorge Cherian 		 * shouldn't do these two operations otherwise we would end up
34075bf8fae3SGeorge Cherian 		 * with all sorts of bugs when removing dwc3.ko.
34085bf8fae3SGeorge Cherian 		 */
34095bf8fae3SGeorge Cherian 		if (epnum != 0 && epnum != 1) {
341072246da4SFelipe Balbi 			dwc3_free_trb_pool(dep);
341172246da4SFelipe Balbi 			list_del(&dep->endpoint.ep_list);
34125bf8fae3SGeorge Cherian 		}
341372246da4SFelipe Balbi 
3414be308d68SGreg Kroah-Hartman 		dwc3_debugfs_remove_endpoint_dir(dep);
341572246da4SFelipe Balbi 		kfree(dep);
341672246da4SFelipe Balbi 	}
341772246da4SFelipe Balbi }
341872246da4SFelipe Balbi 
341972246da4SFelipe Balbi /* -------------------------------------------------------------------------- */
3420e5caff68SFelipe Balbi 
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)34218f608e8aSFelipe Balbi static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
34228f608e8aSFelipe Balbi 		struct dwc3_request *req, struct dwc3_trb *trb,
34238f608e8aSFelipe Balbi 		const struct dwc3_event_depevt *event, int status, int chain)
342472246da4SFelipe Balbi {
342572246da4SFelipe Balbi 	unsigned int		count;
342672246da4SFelipe Balbi 
3427dc55c67eSFelipe Balbi 	dwc3_ep_inc_deq(dep);
3428a9c3ca5fSFelipe Balbi 
34292c4cbe6eSFelipe Balbi 	trace_dwc3_complete_trb(dep, trb);
343009fe1f8dSFelipe Balbi 	req->num_trbs--;
34312c4cbe6eSFelipe Balbi 
3432e5b36ae2SFelipe Balbi 	/*
3433e5b36ae2SFelipe Balbi 	 * If we're in the middle of series of chained TRBs and we
3434e5b36ae2SFelipe Balbi 	 * receive a short transfer along the way, DWC3 will skip
3435e5b36ae2SFelipe Balbi 	 * through all TRBs including the last TRB in the chain (the
3436e5b36ae2SFelipe Balbi 	 * where CHN bit is zero. DWC3 will also avoid clearing HWO
3437e5b36ae2SFelipe Balbi 	 * bit and SW has to do it manually.
3438e5b36ae2SFelipe Balbi 	 *
3439e5b36ae2SFelipe Balbi 	 * We're going to do that here to avoid problems of HW trying
3440e5b36ae2SFelipe Balbi 	 * to use bogus TRBs for transfers.
3441e5b36ae2SFelipe Balbi 	 */
3442e5b36ae2SFelipe Balbi 	if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
3443e5b36ae2SFelipe Balbi 		trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3444e5b36ae2SFelipe Balbi 
3445c6267a51SFelipe Balbi 	/*
34466abfa0f5SThinh Nguyen 	 * For isochronous transfers, the first TRB in a service interval must
34476abfa0f5SThinh Nguyen 	 * have the Isoc-First type. Track and report its interval frame number.
34486abfa0f5SThinh Nguyen 	 */
34496abfa0f5SThinh Nguyen 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
34506abfa0f5SThinh Nguyen 	    (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) {
34516abfa0f5SThinh Nguyen 		unsigned int frame_number;
34526abfa0f5SThinh Nguyen 
34536abfa0f5SThinh Nguyen 		frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl);
34546abfa0f5SThinh Nguyen 		frame_number &= ~(dep->interval - 1);
34556abfa0f5SThinh Nguyen 		req->request.frame_number = frame_number;
34566abfa0f5SThinh Nguyen 	}
34576abfa0f5SThinh Nguyen 
34586abfa0f5SThinh Nguyen 	/*
3459a2841f41SThinh Nguyen 	 * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If
3460a2841f41SThinh Nguyen 	 * this TRB points to the bounce buffer address, it's a MPS alignment
3461a2841f41SThinh Nguyen 	 * TRB. Don't add it to req->remaining calculation.
3462c6267a51SFelipe Balbi 	 */
3463a2841f41SThinh Nguyen 	if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) &&
3464a2841f41SThinh Nguyen 	    trb->bph == upper_32_bits(dep->dwc->bounce_addr)) {
3465c6267a51SFelipe Balbi 		trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
3466c6267a51SFelipe Balbi 		return 1;
3467c6267a51SFelipe Balbi 	}
3468c6267a51SFelipe Balbi 
3469f6bafc6aSFelipe Balbi 	count = trb->size & DWC3_TRB_SIZE_MASK;
3470e62c5bc5SFelipe Balbi 	req->remaining += count;
347172246da4SFelipe Balbi 
347235b2719eSFelipe Balbi 	if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
347335b2719eSFelipe Balbi 		return 1;
347435b2719eSFelipe Balbi 
3475d80fe1b6SFelipe Balbi 	if (event->status & DEPEVT_STATUS_SHORT && !chain)
3476e5ba5ec8SPratyush Anand 		return 1;
3477f99f53f2SFelipe Balbi 
3478f78961f8SThinh Nguyen 	if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) &&
3479f78961f8SThinh Nguyen 	    DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC)
3480f78961f8SThinh Nguyen 		return 1;
3481f78961f8SThinh Nguyen 
34825ee85897SAnurag Kumar Vulisha 	if ((trb->ctrl & DWC3_TRB_CTRL_IOC) ||
34835ee85897SAnurag Kumar Vulisha 	    (trb->ctrl & DWC3_TRB_CTRL_LST))
3484e5ba5ec8SPratyush Anand 		return 1;
3485f99f53f2SFelipe Balbi 
3486e5ba5ec8SPratyush Anand 	return 0;
3487e5ba5ec8SPratyush Anand }
3488e5ba5ec8SPratyush Anand 
dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep * dep,struct dwc3_request * req,const struct dwc3_event_depevt * event,int status)3489d3692953SFelipe Balbi static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
3490d3692953SFelipe Balbi 		struct dwc3_request *req, const struct dwc3_event_depevt *event,
3491d3692953SFelipe Balbi 		int status)
3492d3692953SFelipe Balbi {
3493d3692953SFelipe Balbi 	struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3494d3692953SFelipe Balbi 	struct scatterlist *sg = req->sg;
3495d3692953SFelipe Balbi 	struct scatterlist *s;
349625dda9fcSThinh Nguyen 	unsigned int num_queued = req->num_queued_sgs;
3497d3692953SFelipe Balbi 	unsigned int i;
3498d3692953SFelipe Balbi 	int ret = 0;
3499d3692953SFelipe Balbi 
350025dda9fcSThinh Nguyen 	for_each_sg(sg, s, num_queued, i) {
3501d3692953SFelipe Balbi 		trb = &dep->trb_pool[dep->trb_dequeue];
3502d3692953SFelipe Balbi 
3503d3692953SFelipe Balbi 		req->sg = sg_next(s);
350425dda9fcSThinh Nguyen 		req->num_queued_sgs--;
3505d3692953SFelipe Balbi 
3506d3692953SFelipe Balbi 		ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
3507d3692953SFelipe Balbi 				trb, event, status, true);
3508d3692953SFelipe Balbi 		if (ret)
3509d3692953SFelipe Balbi 			break;
3510d3692953SFelipe Balbi 	}
3511d3692953SFelipe Balbi 
3512d3692953SFelipe Balbi 	return ret;
3513d3692953SFelipe Balbi }
3514d3692953SFelipe Balbi 
dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep * dep,struct dwc3_request * req,const struct dwc3_event_depevt * event,int status)3515d3692953SFelipe Balbi static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
3516d3692953SFelipe Balbi 		struct dwc3_request *req, const struct dwc3_event_depevt *event,
3517d3692953SFelipe Balbi 		int status)
3518d3692953SFelipe Balbi {
3519d3692953SFelipe Balbi 	struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
3520d3692953SFelipe Balbi 
3521d3692953SFelipe Balbi 	return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
3522d3692953SFelipe Balbi 			event, status, false);
3523d3692953SFelipe Balbi }
3524d3692953SFelipe Balbi 
dwc3_gadget_ep_request_completed(struct dwc3_request * req)3525e0c42ce5SFelipe Balbi static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
3526e0c42ce5SFelipe Balbi {
352725dda9fcSThinh Nguyen 	return req->num_pending_sgs == 0 && req->num_queued_sgs == 0;
3528e0c42ce5SFelipe Balbi }
3529e0c42ce5SFelipe Balbi 
dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep * dep,const struct dwc3_event_depevt * event,struct dwc3_request * req,int status)3530f38e35ddSFelipe Balbi static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
3531f38e35ddSFelipe Balbi 		const struct dwc3_event_depevt *event,
3532f38e35ddSFelipe Balbi 		struct dwc3_request *req, int status)
3533e5ba5ec8SPratyush Anand {
3534c7428dbdSThinh Nguyen 	int request_status;
3535fee73e61SFelipe Balbi 	int ret;
3536e5b36ae2SFelipe Balbi 
353725dda9fcSThinh Nguyen 	if (req->request.num_mapped_sgs)
3538d3692953SFelipe Balbi 		ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
3539d3692953SFelipe Balbi 				status);
3540d3692953SFelipe Balbi 	else
3541d3692953SFelipe Balbi 		ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
3542d3692953SFelipe Balbi 				status);
3543e5ba5ec8SPratyush Anand 
3544690e5c2dSThinh Nguyen 	req->request.actual = req->request.length - req->remaining;
3545690e5c2dSThinh Nguyen 
3546690e5c2dSThinh Nguyen 	if (!dwc3_gadget_ep_request_completed(req))
3547690e5c2dSThinh Nguyen 		goto out;
3548690e5c2dSThinh Nguyen 
35491a22ec64SFelipe Balbi 	if (req->needs_extra_trb) {
35508b3b7b66SFelipe Balbi 		ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
35518b3b7b66SFelipe Balbi 				status);
35521a22ec64SFelipe Balbi 		req->needs_extra_trb = false;
3553c6267a51SFelipe Balbi 	}
3554c6267a51SFelipe Balbi 
3555c7428dbdSThinh Nguyen 	/*
3556c7428dbdSThinh Nguyen 	 * The event status only reflects the status of the TRB with IOC set.
3557c7428dbdSThinh Nguyen 	 * For the requests that don't set interrupt on completion, the driver
3558c7428dbdSThinh Nguyen 	 * needs to check and return the status of the completed TRBs associated
3559c7428dbdSThinh Nguyen 	 * with the request. Use the status of the last TRB of the request.
3560c7428dbdSThinh Nguyen 	 */
3561c7428dbdSThinh Nguyen 	if (req->request.no_interrupt) {
3562c7428dbdSThinh Nguyen 		struct dwc3_trb *trb;
3563c7428dbdSThinh Nguyen 
3564c7428dbdSThinh Nguyen 		trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue);
3565c7428dbdSThinh Nguyen 		switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) {
3566c7428dbdSThinh Nguyen 		case DWC3_TRBSTS_MISSED_ISOC:
3567c7428dbdSThinh Nguyen 			/* Isoc endpoint only */
3568c7428dbdSThinh Nguyen 			request_status = -EXDEV;
3569c7428dbdSThinh Nguyen 			break;
3570c7428dbdSThinh Nguyen 		case DWC3_TRB_STS_XFER_IN_PROG:
3571c7428dbdSThinh Nguyen 			/* Applicable when End Transfer with ForceRM=0 */
3572c7428dbdSThinh Nguyen 		case DWC3_TRBSTS_SETUP_PENDING:
3573c7428dbdSThinh Nguyen 			/* Control endpoint only */
3574c7428dbdSThinh Nguyen 		case DWC3_TRBSTS_OK:
3575c7428dbdSThinh Nguyen 		default:
3576c7428dbdSThinh Nguyen 			request_status = 0;
3577c7428dbdSThinh Nguyen 			break;
3578c7428dbdSThinh Nguyen 		}
3579c7428dbdSThinh Nguyen 	} else {
3580c7428dbdSThinh Nguyen 		request_status = status;
3581c7428dbdSThinh Nguyen 	}
3582c7428dbdSThinh Nguyen 
3583c7428dbdSThinh Nguyen 	dwc3_gadget_giveback(dep, req, request_status);
3584e5ba5ec8SPratyush Anand 
3585f38e35ddSFelipe Balbi out:
3586f38e35ddSFelipe Balbi 	return ret;
3587f38e35ddSFelipe Balbi }
3588f38e35ddSFelipe Balbi 
dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep * dep,const struct dwc3_event_depevt * event,int status)3589f38e35ddSFelipe Balbi static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
3590f38e35ddSFelipe Balbi 		const struct dwc3_event_depevt *event, int status)
3591f38e35ddSFelipe Balbi {
3592f38e35ddSFelipe Balbi 	struct dwc3_request	*req;
3593f38e35ddSFelipe Balbi 
3594bf594d1dSWesley Cheng 	while (!list_empty(&dep->started_list)) {
3595f38e35ddSFelipe Balbi 		int ret;
3596f38e35ddSFelipe Balbi 
3597bf594d1dSWesley Cheng 		req = next_request(&dep->started_list);
3598f38e35ddSFelipe Balbi 		ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
3599f38e35ddSFelipe Balbi 				req, status);
360058f0218aSFelipe Balbi 		if (ret)
3601d115d705SVille Syrjälä 			break;
3602bf594d1dSWesley Cheng 		/*
3603bf594d1dSWesley Cheng 		 * The endpoint is disabled, let the dwc3_remove_requests()
3604bf594d1dSWesley Cheng 		 * handle the cleanup.
3605bf594d1dSWesley Cheng 		 */
3606bf594d1dSWesley Cheng 		if (!dep->endpoint.desc)
3607bf594d1dSWesley Cheng 			break;
360831162af4SFelipe Balbi 	}
36097efea86cSPratyush Anand }
361072246da4SFelipe Balbi 
dwc3_gadget_ep_should_continue(struct dwc3_ep * dep)3611d9feef97SThinh Nguyen static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)
3612d9feef97SThinh Nguyen {
3613d9feef97SThinh Nguyen 	struct dwc3_request	*req;
361402fa4b98SWesley Cheng 	struct dwc3		*dwc = dep->dwc;
361502fa4b98SWesley Cheng 
361602fa4b98SWesley Cheng 	if (!dep->endpoint.desc || !dwc->pullups_connected ||
361702fa4b98SWesley Cheng 	    !dwc->connected)
361802fa4b98SWesley Cheng 		return false;
3619d9feef97SThinh Nguyen 
3620d9feef97SThinh Nguyen 	if (!list_empty(&dep->pending_list))
3621d9feef97SThinh Nguyen 		return true;
3622d9feef97SThinh Nguyen 
3623d9feef97SThinh Nguyen 	/*
3624d9feef97SThinh Nguyen 	 * We only need to check the first entry of the started list. We can
3625d9feef97SThinh Nguyen 	 * assume the completed requests are removed from the started list.
3626d9feef97SThinh Nguyen 	 */
3627d9feef97SThinh Nguyen 	req = next_request(&dep->started_list);
3628d9feef97SThinh Nguyen 	if (!req)
3629d9feef97SThinh Nguyen 		return false;
3630d9feef97SThinh Nguyen 
3631d9feef97SThinh Nguyen 	return !dwc3_gadget_ep_request_completed(req);
3632d9feef97SThinh Nguyen }
3633d9feef97SThinh Nguyen 
dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3634ee3638b8SFelipe Balbi static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
3635ee3638b8SFelipe Balbi 		const struct dwc3_event_depevt *event)
3636ee3638b8SFelipe Balbi {
3637f62afb49SFelipe Balbi 	dep->frame_number = event->parameters;
3638ee3638b8SFelipe Balbi }
3639ee3638b8SFelipe Balbi 
dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep * dep,const struct dwc3_event_depevt * event,int status)36402e6e9e4bSThinh Nguyen static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep,
36412e6e9e4bSThinh Nguyen 		const struct dwc3_event_depevt *event, int status)
364272246da4SFelipe Balbi {
36438f608e8aSFelipe Balbi 	struct dwc3		*dwc = dep->dwc;
36442e6e9e4bSThinh Nguyen 	bool			no_started_trb = true;
36456d8a0196SFelipe Balbi 
36465f2e7975SFelipe Balbi 	dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
3647fae2b904SFelipe Balbi 
3648b6842d49SThinh Nguyen 	if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
3649b6842d49SThinh Nguyen 		goto out;
36506d8a0196SFelipe Balbi 
36513c588074SAlbert Wang 	if (!dep->endpoint.desc)
36523c588074SAlbert Wang 		return no_started_trb;
36533c588074SAlbert Wang 
3654f5e46aa4SMichael Grzeschik 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
3655f5e46aa4SMichael Grzeschik 		list_empty(&dep->started_list) &&
3656f5e46aa4SMichael Grzeschik 		(list_empty(&dep->pending_list) || status == -EXDEV))
3657fae2b904SFelipe Balbi 		dwc3_stop_active_transfer(dep, true, true);
3658d9feef97SThinh Nguyen 	else if (dwc3_gadget_ep_should_continue(dep))
36592e6e9e4bSThinh Nguyen 		if (__dwc3_gadget_kick_transfer(dep) == 0)
36602e6e9e4bSThinh Nguyen 			no_started_trb = false;
3661fae2b904SFelipe Balbi 
3662b6842d49SThinh Nguyen out:
3663fae2b904SFelipe Balbi 	/*
3664fae2b904SFelipe Balbi 	 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
3665fae2b904SFelipe Balbi 	 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
3666fae2b904SFelipe Balbi 	 */
36679af21dd6SThinh Nguyen 	if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
3668fae2b904SFelipe Balbi 		u32		reg;
3669fae2b904SFelipe Balbi 		int		i;
3670fae2b904SFelipe Balbi 
3671fae2b904SFelipe Balbi 		for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
3672348e026fSMoiz Sonasath 			dep = dwc->eps[i];
3673fae2b904SFelipe Balbi 
3674fae2b904SFelipe Balbi 			if (!(dep->flags & DWC3_EP_ENABLED))
3675fae2b904SFelipe Balbi 				continue;
3676fae2b904SFelipe Balbi 
3677aa3342c8SFelipe Balbi 			if (!list_empty(&dep->started_list))
36782e6e9e4bSThinh Nguyen 				return no_started_trb;
3679fae2b904SFelipe Balbi 		}
3680fae2b904SFelipe Balbi 
3681fae2b904SFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3682fae2b904SFelipe Balbi 		reg |= dwc->u1u2;
3683fae2b904SFelipe Balbi 		dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3684fae2b904SFelipe Balbi 
3685fae2b904SFelipe Balbi 		dwc->u1u2 = 0;
3686fae2b904SFelipe Balbi 	}
36872e6e9e4bSThinh Nguyen 
36882e6e9e4bSThinh Nguyen 	return no_started_trb;
36892e6e9e4bSThinh Nguyen }
36902e6e9e4bSThinh Nguyen 
dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)36912e6e9e4bSThinh Nguyen static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
36922e6e9e4bSThinh Nguyen 		const struct dwc3_event_depevt *event)
36932e6e9e4bSThinh Nguyen {
36942e6e9e4bSThinh Nguyen 	int status = 0;
36952e6e9e4bSThinh Nguyen 
369626288448SAlbert Wang 	if (!dep->endpoint.desc)
369726288448SAlbert Wang 		return;
369826288448SAlbert Wang 
36992e6e9e4bSThinh Nguyen 	if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
37002e6e9e4bSThinh Nguyen 		dwc3_gadget_endpoint_frame_from_event(dep, event);
37012e6e9e4bSThinh Nguyen 
37022e6e9e4bSThinh Nguyen 	if (event->status & DEPEVT_STATUS_BUSERR)
37032e6e9e4bSThinh Nguyen 		status = -ECONNRESET;
37042e6e9e4bSThinh Nguyen 
37052e6e9e4bSThinh Nguyen 	if (event->status & DEPEVT_STATUS_MISSED_ISOC)
37062e6e9e4bSThinh Nguyen 		status = -EXDEV;
37072e6e9e4bSThinh Nguyen 
37082e6e9e4bSThinh Nguyen 	dwc3_gadget_endpoint_trbs_complete(dep, event, status);
370972246da4SFelipe Balbi }
371072246da4SFelipe Balbi 
dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)37113eaecd0cSThinh Nguyen static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep,
37123eaecd0cSThinh Nguyen 		const struct dwc3_event_depevt *event)
37133eaecd0cSThinh Nguyen {
37143eaecd0cSThinh Nguyen 	int status = 0;
37153eaecd0cSThinh Nguyen 
37163eaecd0cSThinh Nguyen 	dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
37173eaecd0cSThinh Nguyen 
37183eaecd0cSThinh Nguyen 	if (event->status & DEPEVT_STATUS_BUSERR)
37193eaecd0cSThinh Nguyen 		status = -ECONNRESET;
37203eaecd0cSThinh Nguyen 
3721e0d19563SThinh Nguyen 	if (dwc3_gadget_endpoint_trbs_complete(dep, event, status))
3722e0d19563SThinh Nguyen 		dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE;
372372246da4SFelipe Balbi }
372472246da4SFelipe Balbi 
dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)37258f608e8aSFelipe Balbi static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
37268f608e8aSFelipe Balbi 		const struct dwc3_event_depevt *event)
372732033865SFelipe Balbi {
3728ee3638b8SFelipe Balbi 	dwc3_gadget_endpoint_frame_from_event(dep, event);
372936f05d36SThinh Nguyen 
373036f05d36SThinh Nguyen 	/*
373136f05d36SThinh Nguyen 	 * The XferNotReady event is generated only once before the endpoint
373236f05d36SThinh Nguyen 	 * starts. It will be generated again when END_TRANSFER command is
373336f05d36SThinh Nguyen 	 * issued. For some controller versions, the XferNotReady event may be
373436f05d36SThinh Nguyen 	 * generated while the END_TRANSFER command is still in process. Ignore
373536f05d36SThinh Nguyen 	 * it and wait for the next XferNotReady event after the command is
373636f05d36SThinh Nguyen 	 * completed.
373736f05d36SThinh Nguyen 	 */
373836f05d36SThinh Nguyen 	if (dep->flags & DWC3_EP_END_TRANSFER_PENDING)
373936f05d36SThinh Nguyen 		return;
374036f05d36SThinh Nguyen 
374125abad6aSFelipe Balbi 	(void) __dwc3_gadget_start_isoc(dep);
374232033865SFelipe Balbi }
374332033865SFelipe Balbi 
dwc3_gadget_endpoint_command_complete(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)37448266b08eSThinh Nguyen static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep,
37458266b08eSThinh Nguyen 		const struct dwc3_event_depevt *event)
37468266b08eSThinh Nguyen {
37478266b08eSThinh Nguyen 	u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
37488266b08eSThinh Nguyen 
37498266b08eSThinh Nguyen 	if (cmd != DWC3_DEPCMD_ENDTRANSFER)
37508266b08eSThinh Nguyen 		return;
37518266b08eSThinh Nguyen 
3752d74dc3e9SThinh Nguyen 	/*
3753d74dc3e9SThinh Nguyen 	 * The END_TRANSFER command will cause the controller to generate a
3754d74dc3e9SThinh Nguyen 	 * NoStream Event, and it's not due to the host DP NoStream rejection.
3755d74dc3e9SThinh Nguyen 	 * Ignore the next NoStream event.
3756d74dc3e9SThinh Nguyen 	 */
3757d74dc3e9SThinh Nguyen 	if (dep->stream_capable)
3758d74dc3e9SThinh Nguyen 		dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM;
3759d74dc3e9SThinh Nguyen 
37608266b08eSThinh Nguyen 	dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
37618266b08eSThinh Nguyen 	dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
37628266b08eSThinh Nguyen 	dwc3_gadget_ep_cleanup_cancelled_requests(dep);
37638266b08eSThinh Nguyen 
37648266b08eSThinh Nguyen 	if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) {
37658266b08eSThinh Nguyen 		struct dwc3 *dwc = dep->dwc;
37668266b08eSThinh Nguyen 
37678266b08eSThinh Nguyen 		dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL;
37688266b08eSThinh Nguyen 		if (dwc3_send_clear_stall_ep_cmd(dep)) {
37698266b08eSThinh Nguyen 			struct usb_ep *ep0 = &dwc->eps[0]->endpoint;
37708266b08eSThinh Nguyen 
37718266b08eSThinh Nguyen 			dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name);
37728266b08eSThinh Nguyen 			if (dwc->delayed_status)
37738266b08eSThinh Nguyen 				__dwc3_gadget_ep0_set_halt(ep0, 1);
37748266b08eSThinh Nguyen 			return;
37758266b08eSThinh Nguyen 		}
37768266b08eSThinh Nguyen 
37778266b08eSThinh Nguyen 		dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
37782840d6dfSWesley Cheng 		if (dwc->clear_stall_protocol == dep->number)
37798266b08eSThinh Nguyen 			dwc3_ep0_send_delayed_status(dwc);
37808266b08eSThinh Nguyen 	}
37818266b08eSThinh Nguyen 
37828266b08eSThinh Nguyen 	if ((dep->flags & DWC3_EP_DELAY_START) &&
37838266b08eSThinh Nguyen 	    !usb_endpoint_xfer_isoc(dep->endpoint.desc))
37848266b08eSThinh Nguyen 		__dwc3_gadget_kick_transfer(dep);
37858266b08eSThinh Nguyen 
37868266b08eSThinh Nguyen 	dep->flags &= ~DWC3_EP_DELAY_START;
37878266b08eSThinh Nguyen }
37888266b08eSThinh Nguyen 
dwc3_gadget_endpoint_stream_event(struct dwc3_ep * dep,const struct dwc3_event_depevt * event)3789140ca4cfSThinh Nguyen static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep,
3790140ca4cfSThinh Nguyen 		const struct dwc3_event_depevt *event)
3791140ca4cfSThinh Nguyen {
3792140ca4cfSThinh Nguyen 	struct dwc3 *dwc = dep->dwc;
3793140ca4cfSThinh Nguyen 
3794140ca4cfSThinh Nguyen 	if (event->status == DEPEVT_STREAMEVT_FOUND) {
3795140ca4cfSThinh Nguyen 		dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3796140ca4cfSThinh Nguyen 		goto out;
3797140ca4cfSThinh Nguyen 	}
3798140ca4cfSThinh Nguyen 
3799140ca4cfSThinh Nguyen 	/* Note: NoStream rejection event param value is 0 and not 0xFFFF */
3800140ca4cfSThinh Nguyen 	switch (event->parameters) {
3801140ca4cfSThinh Nguyen 	case DEPEVT_STREAM_PRIME:
3802140ca4cfSThinh Nguyen 		/*
3803140ca4cfSThinh Nguyen 		 * If the host can properly transition the endpoint state from
3804140ca4cfSThinh Nguyen 		 * idle to prime after a NoStream rejection, there's no need to
3805140ca4cfSThinh Nguyen 		 * force restarting the endpoint to reinitiate the stream. To
3806140ca4cfSThinh Nguyen 		 * simplify the check, assume the host follows the USB spec if
3807140ca4cfSThinh Nguyen 		 * it primed the endpoint more than once.
3808140ca4cfSThinh Nguyen 		 */
3809140ca4cfSThinh Nguyen 		if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) {
3810140ca4cfSThinh Nguyen 			if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED)
3811140ca4cfSThinh Nguyen 				dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM;
3812140ca4cfSThinh Nguyen 			else
3813140ca4cfSThinh Nguyen 				dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED;
3814140ca4cfSThinh Nguyen 		}
3815140ca4cfSThinh Nguyen 
3816140ca4cfSThinh Nguyen 		break;
3817140ca4cfSThinh Nguyen 	case DEPEVT_STREAM_NOSTREAM:
3818140ca4cfSThinh Nguyen 		if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) ||
3819140ca4cfSThinh Nguyen 		    !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) ||
3820666f3de7SThinh Nguyen 		    (!DWC3_MST_CAPABLE(&dwc->hwparams) &&
3821666f3de7SThinh Nguyen 		     !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)))
3822140ca4cfSThinh Nguyen 			break;
3823140ca4cfSThinh Nguyen 
3824140ca4cfSThinh Nguyen 		/*
3825140ca4cfSThinh Nguyen 		 * If the host rejects a stream due to no active stream, by the
3826140ca4cfSThinh Nguyen 		 * USB and xHCI spec, the endpoint will be put back to idle
3827140ca4cfSThinh Nguyen 		 * state. When the host is ready (buffer added/updated), it will
3828140ca4cfSThinh Nguyen 		 * prime the endpoint to inform the usb device controller. This
3829140ca4cfSThinh Nguyen 		 * triggers the device controller to issue ERDY to restart the
3830140ca4cfSThinh Nguyen 		 * stream. However, some hosts don't follow this and keep the
3831140ca4cfSThinh Nguyen 		 * endpoint in the idle state. No prime will come despite host
3832140ca4cfSThinh Nguyen 		 * streams are updated, and the device controller will not be
3833140ca4cfSThinh Nguyen 		 * triggered to generate ERDY to move the next stream data. To
3834140ca4cfSThinh Nguyen 		 * workaround this and maintain compatibility with various
3835af870d93SKushagra Verma 		 * hosts, force to reinitiate the stream until the host is ready
3836140ca4cfSThinh Nguyen 		 * instead of waiting for the host to prime the endpoint.
3837140ca4cfSThinh Nguyen 		 */
3838b10e1c25SThinh Nguyen 		if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) {
3839b10e1c25SThinh Nguyen 			unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME;
3840b10e1c25SThinh Nguyen 
3841b10e1c25SThinh Nguyen 			dwc3_send_gadget_generic_command(dwc, cmd, dep->number);
3842b10e1c25SThinh Nguyen 		} else {
3843140ca4cfSThinh Nguyen 			dep->flags |= DWC3_EP_DELAY_START;
3844140ca4cfSThinh Nguyen 			dwc3_stop_active_transfer(dep, true, true);
3845140ca4cfSThinh Nguyen 			return;
3846140ca4cfSThinh Nguyen 		}
3847b10e1c25SThinh Nguyen 		break;
3848b10e1c25SThinh Nguyen 	}
3849140ca4cfSThinh Nguyen 
3850140ca4cfSThinh Nguyen out:
3851140ca4cfSThinh Nguyen 	dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM;
3852140ca4cfSThinh Nguyen }
3853140ca4cfSThinh Nguyen 
dwc3_endpoint_interrupt(struct dwc3 * dwc,const struct dwc3_event_depevt * event)385472246da4SFelipe Balbi static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
385572246da4SFelipe Balbi 		const struct dwc3_event_depevt *event)
385672246da4SFelipe Balbi {
385772246da4SFelipe Balbi 	struct dwc3_ep		*dep;
385872246da4SFelipe Balbi 	u8			epnum = event->endpoint_number;
385972246da4SFelipe Balbi 
386072246da4SFelipe Balbi 	dep = dwc->eps[epnum];
386172246da4SFelipe Balbi 
3862d7fd41c6SJanusz Dziedzic 	if (!(dep->flags & DWC3_EP_ENABLED)) {
3863dff98184SWesley Cheng 		if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED))
38643336abb5SFelipe Balbi 			return;
38653336abb5SFelipe Balbi 
3866d7fd41c6SJanusz Dziedzic 		/* Handle only EPCMDCMPLT when EP disabled */
3867dff98184SWesley Cheng 		if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) &&
3868dff98184SWesley Cheng 			!(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE))
3869d7fd41c6SJanusz Dziedzic 			return;
3870d7fd41c6SJanusz Dziedzic 	}
3871d7fd41c6SJanusz Dziedzic 
387272246da4SFelipe Balbi 	if (epnum == 0 || epnum == 1) {
387372246da4SFelipe Balbi 		dwc3_ep0_interrupt(dwc, event);
387472246da4SFelipe Balbi 		return;
387572246da4SFelipe Balbi 	}
387672246da4SFelipe Balbi 
387772246da4SFelipe Balbi 	switch (event->endpoint_event) {
387872246da4SFelipe Balbi 	case DWC3_DEPEVT_XFERINPROGRESS:
38798f608e8aSFelipe Balbi 		dwc3_gadget_endpoint_transfer_in_progress(dep, event);
388072246da4SFelipe Balbi 		break;
388172246da4SFelipe Balbi 	case DWC3_DEPEVT_XFERNOTREADY:
38828f608e8aSFelipe Balbi 		dwc3_gadget_endpoint_transfer_not_ready(dep, event);
388372246da4SFelipe Balbi 		break;
388472246da4SFelipe Balbi 	case DWC3_DEPEVT_EPCMDCMPLT:
38858266b08eSThinh Nguyen 		dwc3_gadget_endpoint_command_complete(dep, event);
388676a638f8SBaolin Wang 		break;
3887742a4fffSFelipe Balbi 	case DWC3_DEPEVT_XFERCOMPLETE:
38883eaecd0cSThinh Nguyen 		dwc3_gadget_endpoint_transfer_complete(dep, event);
38893eaecd0cSThinh Nguyen 		break;
38903eaecd0cSThinh Nguyen 	case DWC3_DEPEVT_STREAMEVT:
3891140ca4cfSThinh Nguyen 		dwc3_gadget_endpoint_stream_event(dep, event);
3892140ca4cfSThinh Nguyen 		break;
389376a638f8SBaolin Wang 	case DWC3_DEPEVT_RXTXFIFOEVT:
389472246da4SFelipe Balbi 		break;
38952f28c3c9SRoy Luo 	default:
38962f28c3c9SRoy Luo 		dev_err(dwc->dev, "unknown endpoint event %d\n", event->endpoint_event);
38972f28c3c9SRoy Luo 		break;
389872246da4SFelipe Balbi 	}
389972246da4SFelipe Balbi }
390072246da4SFelipe Balbi 
dwc3_disconnect_gadget(struct dwc3 * dwc)390172246da4SFelipe Balbi static void dwc3_disconnect_gadget(struct dwc3 *dwc)
390272246da4SFelipe Balbi {
390340edb522SLinyu Yuan 	if (dwc->async_callbacks && dwc->gadget_driver->disconnect) {
390472246da4SFelipe Balbi 		spin_unlock(&dwc->lock);
3905e81a7018SPeter Chen 		dwc->gadget_driver->disconnect(dwc->gadget);
390672246da4SFelipe Balbi 		spin_lock(&dwc->lock);
390772246da4SFelipe Balbi 	}
390872246da4SFelipe Balbi }
390972246da4SFelipe Balbi 
dwc3_suspend_gadget(struct dwc3 * dwc)3910bc5ba2e0SFelipe Balbi static void dwc3_suspend_gadget(struct dwc3 *dwc)
3911bc5ba2e0SFelipe Balbi {
391240edb522SLinyu Yuan 	if (dwc->async_callbacks && dwc->gadget_driver->suspend) {
3913bc5ba2e0SFelipe Balbi 		spin_unlock(&dwc->lock);
3914e81a7018SPeter Chen 		dwc->gadget_driver->suspend(dwc->gadget);
3915bc5ba2e0SFelipe Balbi 		spin_lock(&dwc->lock);
3916bc5ba2e0SFelipe Balbi 	}
3917bc5ba2e0SFelipe Balbi }
3918bc5ba2e0SFelipe Balbi 
dwc3_resume_gadget(struct dwc3 * dwc)3919bc5ba2e0SFelipe Balbi static void dwc3_resume_gadget(struct dwc3 *dwc)
3920bc5ba2e0SFelipe Balbi {
392140edb522SLinyu Yuan 	if (dwc->async_callbacks && dwc->gadget_driver->resume) {
3922bc5ba2e0SFelipe Balbi 		spin_unlock(&dwc->lock);
3923e81a7018SPeter Chen 		dwc->gadget_driver->resume(dwc->gadget);
39245c7b3b02SFelipe Balbi 		spin_lock(&dwc->lock);
39258e74475bSFelipe Balbi 	}
39268e74475bSFelipe Balbi }
39278e74475bSFelipe Balbi 
dwc3_reset_gadget(struct dwc3 * dwc)39288e74475bSFelipe Balbi static void dwc3_reset_gadget(struct dwc3 *dwc)
39298e74475bSFelipe Balbi {
39308e74475bSFelipe Balbi 	if (!dwc->gadget_driver)
39318e74475bSFelipe Balbi 		return;
39328e74475bSFelipe Balbi 
393340edb522SLinyu Yuan 	if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) {
39348e74475bSFelipe Balbi 		spin_unlock(&dwc->lock);
3935e81a7018SPeter Chen 		usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver);
3936bc5ba2e0SFelipe Balbi 		spin_lock(&dwc->lock);
3937bc5ba2e0SFelipe Balbi 	}
3938bc5ba2e0SFelipe Balbi }
3939bc5ba2e0SFelipe Balbi 
dwc3_stop_active_transfer(struct dwc3_ep * dep,bool force,bool interrupt)3940e4cf6580SThinh Nguyen void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force,
3941c5353b22SFelipe Balbi 	bool interrupt)
394272246da4SFelipe Balbi {
3943ace17b6eSThinh Nguyen 	struct dwc3 *dwc = dep->dwc;
3944ace17b6eSThinh Nguyen 
3945ace17b6eSThinh Nguyen 	/*
3946ace17b6eSThinh Nguyen 	 * Only issue End Transfer command to the control endpoint of a started
3947ace17b6eSThinh Nguyen 	 * Data Phase. Typically we should only do so in error cases such as
3948ace17b6eSThinh Nguyen 	 * invalid/unexpected direction as described in the control transfer
3949ace17b6eSThinh Nguyen 	 * flow of the programming guide.
3950ace17b6eSThinh Nguyen 	 */
3951ace17b6eSThinh Nguyen 	if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE)
3952ace17b6eSThinh Nguyen 		return;
3953ace17b6eSThinh Nguyen 
3954c4e3ef56SThinh Nguyen 	if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP))
3955c4e3ef56SThinh Nguyen 		return;
3956c4e3ef56SThinh Nguyen 
3957c58d8bfcSThinh Nguyen 	if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) ||
3958c58d8bfcSThinh Nguyen 	    (dep->flags & DWC3_EP_END_TRANSFER_PENDING))
39593daf74d7SPratyush Anand 		return;
39603daf74d7SPratyush Anand 
396157911504SPratyush Anand 	/*
3962f66eef8fSThinh Nguyen 	 * If a Setup packet is received but yet to DMA out, the controller will
3963f66eef8fSThinh Nguyen 	 * not process the End Transfer command of any endpoint. Polling of its
3964f66eef8fSThinh Nguyen 	 * DEPCMD.CmdAct may block setting up TRB for Setup packet, causing a
3965f66eef8fSThinh Nguyen 	 * timeout. Delay issuing the End Transfer command until the Setup TRB is
3966f66eef8fSThinh Nguyen 	 * prepared.
3967f66eef8fSThinh Nguyen 	 */
39684db0fbb6SThinh Nguyen 	if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) {
3969f66eef8fSThinh Nguyen 		dep->flags |= DWC3_EP_DELAY_STOP;
3970f66eef8fSThinh Nguyen 		return;
3971f66eef8fSThinh Nguyen 	}
3972f66eef8fSThinh Nguyen 
3973f66eef8fSThinh Nguyen 	/*
397457911504SPratyush Anand 	 * NOTICE: We are violating what the Databook says about the
397557911504SPratyush Anand 	 * EndTransfer command. Ideally we would _always_ wait for the
397657911504SPratyush Anand 	 * EndTransfer Command Completion IRQ, but that's causing too
397757911504SPratyush Anand 	 * much trouble synchronizing between us and gadget driver.
397857911504SPratyush Anand 	 *
397957911504SPratyush Anand 	 * We have discussed this with the IP Provider and it was
3980cf2f8b63SThinh Nguyen 	 * suggested to giveback all requests here.
398157911504SPratyush Anand 	 *
398257911504SPratyush Anand 	 * Note also that a similar handling was tested by Synopsys
398357911504SPratyush Anand 	 * (thanks a lot Paul) and nothing bad has come out of it.
3984cf2f8b63SThinh Nguyen 	 * In short, what we're doing is issuing EndTransfer with
3985cf2f8b63SThinh Nguyen 	 * CMDIOC bit set and delay kicking transfer until the
3986cf2f8b63SThinh Nguyen 	 * EndTransfer command had completed.
398706281d46SJohn Youn 	 *
398806281d46SJohn Youn 	 * As of IP version 3.10a of the DWC_usb3 IP, the controller
398906281d46SJohn Youn 	 * supports a mode to work around the above limitation. The
399006281d46SJohn Youn 	 * software can poll the CMDACT bit in the DEPCMD register
399106281d46SJohn Youn 	 * after issuing a EndTransfer command. This mode is enabled
399206281d46SJohn Youn 	 * by writing GUCTL2[14]. This polling is already done in the
399306281d46SJohn Youn 	 * dwc3_send_gadget_ep_cmd() function so if the mode is
399406281d46SJohn Youn 	 * enabled, the EndTransfer command will have completed upon
3995cf2f8b63SThinh Nguyen 	 * returning from this function.
399606281d46SJohn Youn 	 *
3997d8a2bb4eSWesley Cheng 	 * This mode is NOT available on the DWC_usb31 IP.  In this
3998d8a2bb4eSWesley Cheng 	 * case, if the IOC bit is not set, then delay by 1ms
3999d8a2bb4eSWesley Cheng 	 * after issuing the EndTransfer command.  This allows for the
4000d8a2bb4eSWesley Cheng 	 * controller to handle the command completely before DWC3
4001d8a2bb4eSWesley Cheng 	 * remove requests attempts to unmap USB request buffers.
400257911504SPratyush Anand 	 */
400357911504SPratyush Anand 
4004e192cc7bSMichael Grzeschik 	__dwc3_stop_active_transfer(dep, force, interrupt);
400572246da4SFelipe Balbi }
400672246da4SFelipe Balbi 
dwc3_clear_stall_all_ep(struct dwc3 * dwc)400772246da4SFelipe Balbi static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
400872246da4SFelipe Balbi {
400972246da4SFelipe Balbi 	u32 epnum;
401072246da4SFelipe Balbi 
401172246da4SFelipe Balbi 	for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
401272246da4SFelipe Balbi 		struct dwc3_ep *dep;
401372246da4SFelipe Balbi 		int ret;
401472246da4SFelipe Balbi 
401572246da4SFelipe Balbi 		dep = dwc->eps[epnum];
40166a1e3ef4SFelipe Balbi 		if (!dep)
40176a1e3ef4SFelipe Balbi 			continue;
401872246da4SFelipe Balbi 
401972246da4SFelipe Balbi 		if (!(dep->flags & DWC3_EP_STALL))
402072246da4SFelipe Balbi 			continue;
402172246da4SFelipe Balbi 
402272246da4SFelipe Balbi 		dep->flags &= ~DWC3_EP_STALL;
402372246da4SFelipe Balbi 
402450c763f8SJohn Youn 		ret = dwc3_send_clear_stall_ep_cmd(dep);
402572246da4SFelipe Balbi 		WARN_ON_ONCE(ret);
402672246da4SFelipe Balbi 	}
402772246da4SFelipe Balbi }
402872246da4SFelipe Balbi 
dwc3_gadget_disconnect_interrupt(struct dwc3 * dwc)402972246da4SFelipe Balbi static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
403072246da4SFelipe Balbi {
4031c4430a26SFelipe Balbi 	int			reg;
4032c4430a26SFelipe Balbi 
40334e8ef34eSLinyu Yuan 	dwc->suspended = false;
40344e8ef34eSLinyu Yuan 
40351b6009eaSThinh Nguyen 	dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET);
40361b6009eaSThinh Nguyen 
403772246da4SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
403872246da4SFelipe Balbi 	reg &= ~DWC3_DCTL_INITU1ENA;
403972246da4SFelipe Balbi 	reg &= ~DWC3_DCTL_INITU2ENA;
40405b738211SThinh Nguyen 	dwc3_gadget_dctl_write_safe(dwc, reg);
404172246da4SFelipe Balbi 
40428422b769SWesley Cheng 	dwc->connected = false;
40438422b769SWesley Cheng 
404472246da4SFelipe Balbi 	dwc3_disconnect_gadget(dwc);
404572246da4SFelipe Balbi 
4046e81a7018SPeter Chen 	dwc->gadget->speed = USB_SPEED_UNKNOWN;
4047df62df56SFelipe Balbi 	dwc->setup_packet_pending = false;
404804716168SElson Roy Serrao 	dwc->gadget->wakeup_armed = false;
404904716168SElson Roy Serrao 	dwc3_gadget_enable_linksts_evts(dwc, false);
4050e81a7018SPeter Chen 	usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
4051fc8bb91bSFelipe Balbi 
40528f40fc08SWesley Cheng 	dwc3_ep0_reset_state(dwc);
4053334bdf33SWesley Cheng 
4054334bdf33SWesley Cheng 	/*
4055334bdf33SWesley Cheng 	 * Request PM idle to address condition where usage count is
4056334bdf33SWesley Cheng 	 * already decremented to zero, but waiting for the disconnect
4057334bdf33SWesley Cheng 	 * interrupt to set dwc->connected to FALSE.
4058334bdf33SWesley Cheng 	 */
4059334bdf33SWesley Cheng 	pm_request_idle(dwc->dev);
406072246da4SFelipe Balbi }
406172246da4SFelipe Balbi 
dwc3_gadget_reset_interrupt(struct dwc3 * dwc)406272246da4SFelipe Balbi static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
406372246da4SFelipe Balbi {
406472246da4SFelipe Balbi 	u32			reg;
406572246da4SFelipe Balbi 
40664e8ef34eSLinyu Yuan 	dwc->suspended = false;
40674e8ef34eSLinyu Yuan 
4068df62df56SFelipe Balbi 	/*
406971ca43f3SWesley Cheng 	 * Ideally, dwc3_reset_gadget() would trigger the function
407071ca43f3SWesley Cheng 	 * drivers to stop any active transfers through ep disable.
407171ca43f3SWesley Cheng 	 * However, for functions which defer ep disable, such as mass
407271ca43f3SWesley Cheng 	 * storage, we will need to rely on the call to stop active
407371ca43f3SWesley Cheng 	 * transfers here, and avoid allowing of request queuing.
407471ca43f3SWesley Cheng 	 */
407571ca43f3SWesley Cheng 	dwc->connected = false;
407671ca43f3SWesley Cheng 
407771ca43f3SWesley Cheng 	/*
4078df62df56SFelipe Balbi 	 * WORKAROUND: DWC3 revisions <1.88a have an issue which
4079df62df56SFelipe Balbi 	 * would cause a missing Disconnect Event if there's a
4080df62df56SFelipe Balbi 	 * pending Setup Packet in the FIFO.
4081df62df56SFelipe Balbi 	 *
4082df62df56SFelipe Balbi 	 * There's no suggested workaround on the official Bug
4083df62df56SFelipe Balbi 	 * report, which states that "unless the driver/application
4084df62df56SFelipe Balbi 	 * is doing any special handling of a disconnect event,
4085df62df56SFelipe Balbi 	 * there is no functional issue".
4086df62df56SFelipe Balbi 	 *
4087df62df56SFelipe Balbi 	 * Unfortunately, it turns out that we _do_ some special
4088df62df56SFelipe Balbi 	 * handling of a disconnect event, namely complete all
4089df62df56SFelipe Balbi 	 * pending transfers, notify gadget driver of the
4090df62df56SFelipe Balbi 	 * disconnection, and so on.
4091df62df56SFelipe Balbi 	 *
4092df62df56SFelipe Balbi 	 * Our suggested workaround is to follow the Disconnect
4093df62df56SFelipe Balbi 	 * Event steps here, instead, based on a setup_packet_pending
4094b5d335e5SFelipe Balbi 	 * flag. Such flag gets set whenever we have a SETUP_PENDING
4095b5d335e5SFelipe Balbi 	 * status for EP0 TRBs and gets cleared on XferComplete for the
4096df62df56SFelipe Balbi 	 * same endpoint.
4097df62df56SFelipe Balbi 	 *
4098df62df56SFelipe Balbi 	 * Refers to:
4099df62df56SFelipe Balbi 	 *
4100df62df56SFelipe Balbi 	 * STAR#9000466709: RTL: Device : Disconnect event not
4101df62df56SFelipe Balbi 	 * generated if setup packet pending in FIFO
4102df62df56SFelipe Balbi 	 */
41039af21dd6SThinh Nguyen 	if (DWC3_VER_IS_PRIOR(DWC3, 188A)) {
4104df62df56SFelipe Balbi 		if (dwc->setup_packet_pending)
4105df62df56SFelipe Balbi 			dwc3_gadget_disconnect_interrupt(dwc);
4106df62df56SFelipe Balbi 	}
4107df62df56SFelipe Balbi 
41088e74475bSFelipe Balbi 	dwc3_reset_gadget(dwc);
41099d778f0cSMayank Rana 
41109d778f0cSMayank Rana 	/*
41119d778f0cSMayank Rana 	 * From SNPS databook section 8.1.2, the EP0 should be in setup
41129d778f0cSMayank Rana 	 * phase. So ensure that EP0 is in setup phase by issuing a stall
41139d778f0cSMayank Rana 	 * and restart if EP0 is not in setup phase.
41149d778f0cSMayank Rana 	 */
41158f40fc08SWesley Cheng 	dwc3_ep0_reset_state(dwc);
41169d778f0cSMayank Rana 
4117ae7e8610SWesley Cheng 	/*
4118ae7e8610SWesley Cheng 	 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a
4119ae7e8610SWesley Cheng 	 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW
4120ae7e8610SWesley Cheng 	 * needs to ensure that it sends "a DEPENDXFER command for any active
4121ae7e8610SWesley Cheng 	 * transfers."
4122ae7e8610SWesley Cheng 	 */
4123ae7e8610SWesley Cheng 	dwc3_stop_active_transfers(dwc);
4124f09ddcfcSWesley Cheng 	dwc->connected = true;
412572246da4SFelipe Balbi 
412672246da4SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DCTL);
412772246da4SFelipe Balbi 	reg &= ~DWC3_DCTL_TSTCTRL_MASK;
41285b738211SThinh Nguyen 	dwc3_gadget_dctl_write_safe(dwc, reg);
41293b637367SGerard Cauvy 	dwc->test_mode = false;
413004716168SElson Roy Serrao 	dwc->gadget->wakeup_armed = false;
413104716168SElson Roy Serrao 	dwc3_gadget_enable_linksts_evts(dwc, false);
413272246da4SFelipe Balbi 	dwc3_clear_stall_all_ep(dwc);
413372246da4SFelipe Balbi 
413472246da4SFelipe Balbi 	/* Reset device address to zero */
413572246da4SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DCFG);
413672246da4SFelipe Balbi 	reg &= ~(DWC3_DCFG_DEVADDR_MASK);
413772246da4SFelipe Balbi 	dwc3_writel(dwc->regs, DWC3_DCFG, reg);
413872246da4SFelipe Balbi }
413972246da4SFelipe Balbi 
dwc3_gadget_conndone_interrupt(struct dwc3 * dwc)414072246da4SFelipe Balbi static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
414172246da4SFelipe Balbi {
414272246da4SFelipe Balbi 	struct dwc3_ep		*dep;
414372246da4SFelipe Balbi 	int			ret;
414472246da4SFelipe Balbi 	u32			reg;
4145f551037cSThinh Nguyen 	u8			lanes = 1;
414672246da4SFelipe Balbi 	u8			speed;
414772246da4SFelipe Balbi 
4148359d5a85SWesley Cheng 	if (!dwc->softconnect)
4149359d5a85SWesley Cheng 		return;
4150359d5a85SWesley Cheng 
415172246da4SFelipe Balbi 	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
415272246da4SFelipe Balbi 	speed = reg & DWC3_DSTS_CONNECTSPD;
415372246da4SFelipe Balbi 	dwc->speed = speed;
415472246da4SFelipe Balbi 
4155f551037cSThinh Nguyen 	if (DWC3_IP_IS(DWC32))
4156f551037cSThinh Nguyen 		lanes = DWC3_DSTS_CONNLANES(reg) + 1;
4157f551037cSThinh Nguyen 
4158f551037cSThinh Nguyen 	dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN;
4159f551037cSThinh Nguyen 
41605fb6fdafSJohn Youn 	/*
41615fb6fdafSJohn Youn 	 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
41625fb6fdafSJohn Youn 	 * each time on Connect Done.
41635fb6fdafSJohn Youn 	 *
41645fb6fdafSJohn Youn 	 * Currently we always use the reset value. If any platform
41655fb6fdafSJohn Youn 	 * wants to set this to a different value, we need to add a
41665fb6fdafSJohn Youn 	 * setting and update GCTL.RAMCLKSEL here.
41675fb6fdafSJohn Youn 	 */
416872246da4SFelipe Balbi 
416972246da4SFelipe Balbi 	switch (speed) {
41702da9ad76SJohn Youn 	case DWC3_DSTS_SUPERSPEED_PLUS:
41717580862bSJohn Youn 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
4172e81a7018SPeter Chen 		dwc->gadget->ep0->maxpacket = 512;
4173e81a7018SPeter Chen 		dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4174f551037cSThinh Nguyen 
4175f551037cSThinh Nguyen 		if (lanes > 1)
4176f551037cSThinh Nguyen 			dwc->gadget->ssp_rate = USB_SSP_GEN_2x2;
4177f551037cSThinh Nguyen 		else
4178f551037cSThinh Nguyen 			dwc->gadget->ssp_rate = USB_SSP_GEN_2x1;
41797580862bSJohn Youn 		break;
41802da9ad76SJohn Youn 	case DWC3_DSTS_SUPERSPEED:
418105870c5bSFelipe Balbi 		/*
418205870c5bSFelipe Balbi 		 * WORKAROUND: DWC3 revisions <1.90a have an issue which
418305870c5bSFelipe Balbi 		 * would cause a missing USB3 Reset event.
418405870c5bSFelipe Balbi 		 *
418505870c5bSFelipe Balbi 		 * In such situations, we should force a USB3 Reset
418605870c5bSFelipe Balbi 		 * event by calling our dwc3_gadget_reset_interrupt()
418705870c5bSFelipe Balbi 		 * routine.
418805870c5bSFelipe Balbi 		 *
418905870c5bSFelipe Balbi 		 * Refers to:
419005870c5bSFelipe Balbi 		 *
419105870c5bSFelipe Balbi 		 * STAR#9000483510: RTL: SS : USB3 reset event may
419205870c5bSFelipe Balbi 		 * not be generated always when the link enters poll
419305870c5bSFelipe Balbi 		 */
41949af21dd6SThinh Nguyen 		if (DWC3_VER_IS_PRIOR(DWC3, 190A))
419505870c5bSFelipe Balbi 			dwc3_gadget_reset_interrupt(dwc);
419605870c5bSFelipe Balbi 
419772246da4SFelipe Balbi 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
4198e81a7018SPeter Chen 		dwc->gadget->ep0->maxpacket = 512;
4199e81a7018SPeter Chen 		dwc->gadget->speed = USB_SPEED_SUPER;
4200f551037cSThinh Nguyen 
4201f551037cSThinh Nguyen 		if (lanes > 1) {
4202f551037cSThinh Nguyen 			dwc->gadget->speed = USB_SPEED_SUPER_PLUS;
4203f551037cSThinh Nguyen 			dwc->gadget->ssp_rate = USB_SSP_GEN_1x2;
4204f551037cSThinh Nguyen 		}
420572246da4SFelipe Balbi 		break;
42062da9ad76SJohn Youn 	case DWC3_DSTS_HIGHSPEED:
420772246da4SFelipe Balbi 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
4208e81a7018SPeter Chen 		dwc->gadget->ep0->maxpacket = 64;
4209e81a7018SPeter Chen 		dwc->gadget->speed = USB_SPEED_HIGH;
421072246da4SFelipe Balbi 		break;
42119418ee15SRoger Quadros 	case DWC3_DSTS_FULLSPEED:
421272246da4SFelipe Balbi 		dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
4213e81a7018SPeter Chen 		dwc->gadget->ep0->maxpacket = 64;
4214e81a7018SPeter Chen 		dwc->gadget->speed = USB_SPEED_FULL;
421572246da4SFelipe Balbi 		break;
421672246da4SFelipe Balbi 	}
421772246da4SFelipe Balbi 
4218e81a7018SPeter Chen 	dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket;
421961800263SThinh Nguyen 
42202b758350SPratyush Anand 	/* Enable USB2 LPM Capability */
42212b758350SPratyush Anand 
42229af21dd6SThinh Nguyen 	if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) &&
4223475e8be5SThinh Nguyen 	    !dwc->usb2_gadget_lpm_disable &&
42242da9ad76SJohn Youn 	    (speed != DWC3_DSTS_SUPERSPEED) &&
42252da9ad76SJohn Youn 	    (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
42262b758350SPratyush Anand 		reg = dwc3_readl(dwc->regs, DWC3_DCFG);
42272b758350SPratyush Anand 		reg |= DWC3_DCFG_LPM_CAP;
42282b758350SPratyush Anand 		dwc3_writel(dwc->regs, DWC3_DCFG, reg);
42292b758350SPratyush Anand 
42302b758350SPratyush Anand 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
42312b758350SPratyush Anand 		reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
42322b758350SPratyush Anand 
423316fe4f30SThinh Nguyen 		reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold |
423416fe4f30SThinh Nguyen 					    (dwc->is_utmi_l1_suspend << 4));
42352b758350SPratyush Anand 
423680caf7d2SHuang Rui 		/*
423780caf7d2SHuang Rui 		 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
423880caf7d2SHuang Rui 		 * DCFG.LPMCap is set, core responses with an ACK and the
423980caf7d2SHuang Rui 		 * BESL value in the LPM token is less than or equal to LPM
424080caf7d2SHuang Rui 		 * NYET threshold.
424180caf7d2SHuang Rui 		 */
42429af21dd6SThinh Nguyen 		WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum,
42439165dabbSMasanari Iida 				"LPM Erratum not available on dwc3 revisions < 2.40a\n");
424480caf7d2SHuang Rui 
4245c995c81bSAndré Draszik 		if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) {
4246c995c81bSAndré Draszik 			reg &= ~DWC3_DCTL_NYET_THRES_MASK;
42472e487d28SThinh Nguyen 			reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold);
4248c995c81bSAndré Draszik 		}
424980caf7d2SHuang Rui 
42505b738211SThinh Nguyen 		dwc3_gadget_dctl_write_safe(dwc, reg);
4251356363bfSFelipe Balbi 	} else {
4252475e8be5SThinh Nguyen 		if (dwc->usb2_gadget_lpm_disable) {
4253475e8be5SThinh Nguyen 			reg = dwc3_readl(dwc->regs, DWC3_DCFG);
4254475e8be5SThinh Nguyen 			reg &= ~DWC3_DCFG_LPM_CAP;
4255475e8be5SThinh Nguyen 			dwc3_writel(dwc->regs, DWC3_DCFG, reg);
4256475e8be5SThinh Nguyen 		}
4257475e8be5SThinh Nguyen 
4258356363bfSFelipe Balbi 		reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4259356363bfSFelipe Balbi 		reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
42605b738211SThinh Nguyen 		dwc3_gadget_dctl_write_safe(dwc, reg);
42612b758350SPratyush Anand 	}
42622b758350SPratyush Anand 
426372246da4SFelipe Balbi 	dep = dwc->eps[0];
4264a2d23f08SFelipe Balbi 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
426572246da4SFelipe Balbi 	if (ret) {
426672246da4SFelipe Balbi 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
426772246da4SFelipe Balbi 		return;
426872246da4SFelipe Balbi 	}
426972246da4SFelipe Balbi 
427072246da4SFelipe Balbi 	dep = dwc->eps[1];
4271a2d23f08SFelipe Balbi 	ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
427272246da4SFelipe Balbi 	if (ret) {
427372246da4SFelipe Balbi 		dev_err(dwc->dev, "failed to enable %s\n", dep->name);
427472246da4SFelipe Balbi 		return;
427572246da4SFelipe Balbi 	}
427672246da4SFelipe Balbi 
427772246da4SFelipe Balbi 	/*
427872246da4SFelipe Balbi 	 * Configure PHY via GUSB3PIPECTLn if required.
427972246da4SFelipe Balbi 	 *
428072246da4SFelipe Balbi 	 * Update GTXFIFOSIZn
428172246da4SFelipe Balbi 	 *
428272246da4SFelipe Balbi 	 * In both cases reset values should be sufficient.
428372246da4SFelipe Balbi 	 */
428472246da4SFelipe Balbi }
428572246da4SFelipe Balbi 
dwc3_gadget_wakeup_interrupt(struct dwc3 * dwc,unsigned int evtinfo)428604716168SElson Roy Serrao static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, unsigned int evtinfo)
428772246da4SFelipe Balbi {
42884e8ef34eSLinyu Yuan 	dwc->suspended = false;
42894e8ef34eSLinyu Yuan 
429072246da4SFelipe Balbi 	/*
429172246da4SFelipe Balbi 	 * TODO take core out of low power mode when that's
429272246da4SFelipe Balbi 	 * implemented.
429372246da4SFelipe Balbi 	 */
429472246da4SFelipe Balbi 
429540edb522SLinyu Yuan 	if (dwc->async_callbacks && dwc->gadget_driver->resume) {
4296ad14d4e0SJiebing Li 		spin_unlock(&dwc->lock);
4297e81a7018SPeter Chen 		dwc->gadget_driver->resume(dwc->gadget);
4298ad14d4e0SJiebing Li 		spin_lock(&dwc->lock);
4299ad14d4e0SJiebing Li 	}
430004716168SElson Roy Serrao 
430104716168SElson Roy Serrao 	dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
430272246da4SFelipe Balbi }
430372246da4SFelipe Balbi 
dwc3_gadget_linksts_change_interrupt(struct dwc3 * dwc,unsigned int evtinfo)430472246da4SFelipe Balbi static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
430572246da4SFelipe Balbi 		unsigned int evtinfo)
430672246da4SFelipe Balbi {
4307fae2b904SFelipe Balbi 	enum dwc3_link_state	next = evtinfo & DWC3_LINK_STATE_MASK;
43080b0cc1cdSFelipe Balbi 	unsigned int		pwropt;
43090b0cc1cdSFelipe Balbi 
43100b0cc1cdSFelipe Balbi 	/*
43110b0cc1cdSFelipe Balbi 	 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
43120b0cc1cdSFelipe Balbi 	 * Hibernation mode enabled which would show up when device detects
43130b0cc1cdSFelipe Balbi 	 * host-initiated U3 exit.
43140b0cc1cdSFelipe Balbi 	 *
43150b0cc1cdSFelipe Balbi 	 * In that case, device will generate a Link State Change Interrupt
43160b0cc1cdSFelipe Balbi 	 * from U3 to RESUME which is only necessary if Hibernation is
43170b0cc1cdSFelipe Balbi 	 * configured in.
43180b0cc1cdSFelipe Balbi 	 *
43190b0cc1cdSFelipe Balbi 	 * There are no functional changes due to such spurious event and we
43200b0cc1cdSFelipe Balbi 	 * just need to ignore it.
43210b0cc1cdSFelipe Balbi 	 *
43220b0cc1cdSFelipe Balbi 	 * Refers to:
43230b0cc1cdSFelipe Balbi 	 *
43240b0cc1cdSFelipe Balbi 	 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
43250b0cc1cdSFelipe Balbi 	 * operational mode
43260b0cc1cdSFelipe Balbi 	 */
43270b0cc1cdSFelipe Balbi 	pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
43289af21dd6SThinh Nguyen 	if (DWC3_VER_IS_PRIOR(DWC3, 250A) &&
43290b0cc1cdSFelipe Balbi 			(pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
43300b0cc1cdSFelipe Balbi 		if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
43310b0cc1cdSFelipe Balbi 				(next == DWC3_LINK_STATE_RESUME)) {
43320b0cc1cdSFelipe Balbi 			return;
43330b0cc1cdSFelipe Balbi 		}
43340b0cc1cdSFelipe Balbi 	}
4335fae2b904SFelipe Balbi 
4336fae2b904SFelipe Balbi 	/*
4337fae2b904SFelipe Balbi 	 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
4338fae2b904SFelipe Balbi 	 * on the link partner, the USB session might do multiple entry/exit
4339fae2b904SFelipe Balbi 	 * of low power states before a transfer takes place.
4340fae2b904SFelipe Balbi 	 *
4341fae2b904SFelipe Balbi 	 * Due to this problem, we might experience lower throughput. The
4342fae2b904SFelipe Balbi 	 * suggested workaround is to disable DCTL[12:9] bits if we're
4343fae2b904SFelipe Balbi 	 * transitioning from U1/U2 to U0 and enable those bits again
4344fae2b904SFelipe Balbi 	 * after a transfer completes and there are no pending transfers
4345fae2b904SFelipe Balbi 	 * on any of the enabled endpoints.
4346fae2b904SFelipe Balbi 	 *
4347fae2b904SFelipe Balbi 	 * This is the first half of that workaround.
4348fae2b904SFelipe Balbi 	 *
4349fae2b904SFelipe Balbi 	 * Refers to:
4350fae2b904SFelipe Balbi 	 *
4351fae2b904SFelipe Balbi 	 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
4352fae2b904SFelipe Balbi 	 * core send LGO_Ux entering U0
4353fae2b904SFelipe Balbi 	 */
43549af21dd6SThinh Nguyen 	if (DWC3_VER_IS_PRIOR(DWC3, 183A)) {
4355fae2b904SFelipe Balbi 		if (next == DWC3_LINK_STATE_U0) {
4356fae2b904SFelipe Balbi 			u32	u1u2;
4357fae2b904SFelipe Balbi 			u32	reg;
4358fae2b904SFelipe Balbi 
4359fae2b904SFelipe Balbi 			switch (dwc->link_state) {
4360fae2b904SFelipe Balbi 			case DWC3_LINK_STATE_U1:
4361fae2b904SFelipe Balbi 			case DWC3_LINK_STATE_U2:
4362fae2b904SFelipe Balbi 				reg = dwc3_readl(dwc->regs, DWC3_DCTL);
4363fae2b904SFelipe Balbi 				u1u2 = reg & (DWC3_DCTL_INITU2ENA
4364fae2b904SFelipe Balbi 						| DWC3_DCTL_ACCEPTU2ENA
4365fae2b904SFelipe Balbi 						| DWC3_DCTL_INITU1ENA
4366fae2b904SFelipe Balbi 						| DWC3_DCTL_ACCEPTU1ENA);
4367fae2b904SFelipe Balbi 
4368fae2b904SFelipe Balbi 				if (!dwc->u1u2)
4369fae2b904SFelipe Balbi 					dwc->u1u2 = reg & u1u2;
4370fae2b904SFelipe Balbi 
4371fae2b904SFelipe Balbi 				reg &= ~u1u2;
4372fae2b904SFelipe Balbi 
43735b738211SThinh Nguyen 				dwc3_gadget_dctl_write_safe(dwc, reg);
4374fae2b904SFelipe Balbi 				break;
4375fae2b904SFelipe Balbi 			default:
4376fae2b904SFelipe Balbi 				/* do nothing */
4377fae2b904SFelipe Balbi 				break;
4378fae2b904SFelipe Balbi 			}
4379fae2b904SFelipe Balbi 		}
4380fae2b904SFelipe Balbi 	}
4381fae2b904SFelipe Balbi 
4382bc5ba2e0SFelipe Balbi 	switch (next) {
438304716168SElson Roy Serrao 	case DWC3_LINK_STATE_U0:
438404716168SElson Roy Serrao 		if (dwc->gadget->wakeup_armed) {
438504716168SElson Roy Serrao 			dwc3_gadget_enable_linksts_evts(dwc, false);
438604716168SElson Roy Serrao 			dwc3_resume_gadget(dwc);
43874e8ef34eSLinyu Yuan 			dwc->suspended = false;
438804716168SElson Roy Serrao 		}
438904716168SElson Roy Serrao 		break;
4390bc5ba2e0SFelipe Balbi 	case DWC3_LINK_STATE_U1:
4391bc5ba2e0SFelipe Balbi 		if (dwc->speed == USB_SPEED_SUPER)
4392bc5ba2e0SFelipe Balbi 			dwc3_suspend_gadget(dwc);
4393bc5ba2e0SFelipe Balbi 		break;
4394bc5ba2e0SFelipe Balbi 	case DWC3_LINK_STATE_U2:
4395bc5ba2e0SFelipe Balbi 	case DWC3_LINK_STATE_U3:
4396bc5ba2e0SFelipe Balbi 		dwc3_suspend_gadget(dwc);
4397bc5ba2e0SFelipe Balbi 		break;
4398bc5ba2e0SFelipe Balbi 	case DWC3_LINK_STATE_RESUME:
4399bc5ba2e0SFelipe Balbi 		dwc3_resume_gadget(dwc);
4400bc5ba2e0SFelipe Balbi 		break;
4401bc5ba2e0SFelipe Balbi 	default:
4402bc5ba2e0SFelipe Balbi 		/* do nothing */
4403bc5ba2e0SFelipe Balbi 		break;
4404bc5ba2e0SFelipe Balbi 	}
4405bc5ba2e0SFelipe Balbi 
4406e57ebc1dSFelipe Balbi 	dwc->link_state = next;
440772246da4SFelipe Balbi }
440872246da4SFelipe Balbi 
dwc3_gadget_suspend_interrupt(struct dwc3 * dwc,unsigned int evtinfo)440972704f87SBaolin Wang static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
441072704f87SBaolin Wang 					  unsigned int evtinfo)
441172704f87SBaolin Wang {
441272704f87SBaolin Wang 	enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
441372704f87SBaolin Wang 
44144e8ef34eSLinyu Yuan 	if (!dwc->suspended && next == DWC3_LINK_STATE_U3) {
44154e8ef34eSLinyu Yuan 		dwc->suspended = true;
441672704f87SBaolin Wang 		dwc3_suspend_gadget(dwc);
44174e8ef34eSLinyu Yuan 	}
441872704f87SBaolin Wang 
441972704f87SBaolin Wang 	dwc->link_state = next;
442072704f87SBaolin Wang }
442172704f87SBaolin Wang 
dwc3_gadget_interrupt(struct dwc3 * dwc,const struct dwc3_event_devt * event)442272246da4SFelipe Balbi static void dwc3_gadget_interrupt(struct dwc3 *dwc,
442372246da4SFelipe Balbi 		const struct dwc3_event_devt *event)
442472246da4SFelipe Balbi {
442572246da4SFelipe Balbi 	switch (event->type) {
442672246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_DISCONNECT:
442772246da4SFelipe Balbi 		dwc3_gadget_disconnect_interrupt(dwc);
442872246da4SFelipe Balbi 		break;
442972246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_RESET:
443072246da4SFelipe Balbi 		dwc3_gadget_reset_interrupt(dwc);
443172246da4SFelipe Balbi 		break;
443272246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_CONNECT_DONE:
443372246da4SFelipe Balbi 		dwc3_gadget_conndone_interrupt(dwc);
443472246da4SFelipe Balbi 		break;
443572246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_WAKEUP:
443604716168SElson Roy Serrao 		dwc3_gadget_wakeup_interrupt(dwc, event->event_info);
443772246da4SFelipe Balbi 		break;
4438e1dadd3bSFelipe Balbi 	case DWC3_DEVICE_EVENT_HIBER_REQ:
4439bdb19d01SJohan Hovold 		dev_WARN_ONCE(dwc->dev, true, "unexpected hibernation event\n");
4440e1dadd3bSFelipe Balbi 		break;
444172246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
444272246da4SFelipe Balbi 		dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
444372246da4SFelipe Balbi 		break;
44446f26ebb7SJack Pham 	case DWC3_DEVICE_EVENT_SUSPEND:
444572704f87SBaolin Wang 		/* It changed to be suspend event for version 2.30a and above */
44464decf406SPrashanth K 		if (!DWC3_VER_IS_PRIOR(DWC3, 230A))
44474decf406SPrashanth K 			dwc3_gadget_suspend_interrupt(dwc, event->event_info);
444872246da4SFelipe Balbi 		break;
444972246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_SOF:
445072246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
445172246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_CMD_CMPL:
445272246da4SFelipe Balbi 	case DWC3_DEVICE_EVENT_OVERFLOW:
445372246da4SFelipe Balbi 		break;
445472246da4SFelipe Balbi 	default:
4455e9f2aa87SFelipe Balbi 		dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
445672246da4SFelipe Balbi 	}
445772246da4SFelipe Balbi }
445872246da4SFelipe Balbi 
dwc3_process_event_entry(struct dwc3 * dwc,const union dwc3_event * event)445972246da4SFelipe Balbi static void dwc3_process_event_entry(struct dwc3 *dwc,
446072246da4SFelipe Balbi 		const union dwc3_event *event)
446172246da4SFelipe Balbi {
446243c96be1SFelipe Balbi 	trace_dwc3_event(event->raw, dwc);
44632c4cbe6eSFelipe Balbi 
4464dfc5e805SFelipe Balbi 	if (!event->type.is_devspec)
4465dfc5e805SFelipe Balbi 		dwc3_endpoint_interrupt(dwc, &event->depevt);
4466dfc5e805SFelipe Balbi 	else if (event->type.type == DWC3_EVENT_TYPE_DEV)
446772246da4SFelipe Balbi 		dwc3_gadget_interrupt(dwc, &event->devt);
4468dfc5e805SFelipe Balbi 	else
446972246da4SFelipe Balbi 		dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
447072246da4SFelipe Balbi }
447172246da4SFelipe Balbi 
dwc3_process_event_buf(struct dwc3_event_buffer * evt)4472dea520a4SFelipe Balbi static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
447372246da4SFelipe Balbi {
4474dea520a4SFelipe Balbi 	struct dwc3 *dwc = evt->dwc;
4475f42f2447SFelipe Balbi 	irqreturn_t ret = IRQ_NONE;
447672246da4SFelipe Balbi 	int left;
447772246da4SFelipe Balbi 
4478b15a762fSFelipe Balbi 	left = evt->count;
447972246da4SFelipe Balbi 
4480b15a762fSFelipe Balbi 	if (!(evt->flags & DWC3_EVENT_PENDING))
4481f42f2447SFelipe Balbi 		return IRQ_NONE;
448272246da4SFelipe Balbi 
448372246da4SFelipe Balbi 	while (left > 0) {
448472246da4SFelipe Balbi 		union dwc3_event event;
448572246da4SFelipe Balbi 
4486ebbb2d59SJohn Youn 		event.raw = *(u32 *) (evt->cache + evt->lpos);
4487d70d8442SFelipe Balbi 
448872246da4SFelipe Balbi 		dwc3_process_event_entry(dwc, &event);
4489b15a762fSFelipe Balbi 
449072246da4SFelipe Balbi 		/*
4491b15a762fSFelipe Balbi 		 * FIXME we wrap around correctly to the next entry as
4492b15a762fSFelipe Balbi 		 * almost all entries are 4 bytes in size. There is one
4493b15a762fSFelipe Balbi 		 * entry which has 12 bytes which is a regular entry
4494b15a762fSFelipe Balbi 		 * followed by 8 bytes data. ATM I don't know how
4495b15a762fSFelipe Balbi 		 * things are organized if we get next to the a
4496b15a762fSFelipe Balbi 		 * boundary so I worry about that once we try to handle
4497b15a762fSFelipe Balbi 		 * that.
449872246da4SFelipe Balbi 		 */
4499caefe6c7SFelipe Balbi 		evt->lpos = (evt->lpos + 4) % evt->length;
450072246da4SFelipe Balbi 		left -= 4;
450172246da4SFelipe Balbi 	}
450272246da4SFelipe Balbi 
4503b15a762fSFelipe Balbi 	evt->count = 0;
4504b15a762fSFelipe Balbi 	ret = IRQ_HANDLED;
4505e8adfc30SFelipe Balbi 
4506e8adfc30SFelipe Balbi 	/* Unmask interrupt */
450776c4c95dSThinh Nguyen 	dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
450876c4c95dSThinh Nguyen 		    DWC3_GEVNTSIZ_SIZE(evt->length));
4509f42f2447SFelipe Balbi 
4510*656a99b5SBadhri Jagan Sridharan 	evt->flags &= ~DWC3_EVENT_PENDING;
4511*656a99b5SBadhri Jagan Sridharan 	/*
4512*656a99b5SBadhri Jagan Sridharan 	 * Add an explicit write memory barrier to make sure that the update of
4513*656a99b5SBadhri Jagan Sridharan 	 * clearing DWC3_EVENT_PENDING is observed in dwc3_check_event_buf()
4514*656a99b5SBadhri Jagan Sridharan 	 */
4515*656a99b5SBadhri Jagan Sridharan 	wmb();
4516*656a99b5SBadhri Jagan Sridharan 
4517cf40b86bSJohn Youn 	if (dwc->imod_interval) {
4518cf40b86bSJohn Youn 		dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
4519cf40b86bSJohn Youn 		dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
4520cf40b86bSJohn Youn 	}
4521cf40b86bSJohn Youn 
4522f42f2447SFelipe Balbi 	return ret;
4523b15a762fSFelipe Balbi }
4524b15a762fSFelipe Balbi 
dwc3_thread_interrupt(int irq,void * _evt)4525dea520a4SFelipe Balbi static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
4526f42f2447SFelipe Balbi {
4527dea520a4SFelipe Balbi 	struct dwc3_event_buffer *evt = _evt;
4528dea520a4SFelipe Balbi 	struct dwc3 *dwc = evt->dwc;
4529e5f68b4aSFelipe Balbi 	unsigned long flags;
4530f42f2447SFelipe Balbi 	irqreturn_t ret = IRQ_NONE;
4531f42f2447SFelipe Balbi 
453284918a89SSebastian Andrzej Siewior 	local_bh_disable();
4533e5f68b4aSFelipe Balbi 	spin_lock_irqsave(&dwc->lock, flags);
4534dea520a4SFelipe Balbi 	ret = dwc3_process_event_buf(evt);
4535e5f68b4aSFelipe Balbi 	spin_unlock_irqrestore(&dwc->lock, flags);
453684918a89SSebastian Andrzej Siewior 	local_bh_enable();
4537b15a762fSFelipe Balbi 
4538b15a762fSFelipe Balbi 	return ret;
4539b15a762fSFelipe Balbi }
4540b15a762fSFelipe Balbi 
dwc3_check_event_buf(struct dwc3_event_buffer * evt)4541dea520a4SFelipe Balbi static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
4542b15a762fSFelipe Balbi {
4543dea520a4SFelipe Balbi 	struct dwc3 *dwc = evt->dwc;
4544ebbb2d59SJohn Youn 	u32 amount;
4545b15a762fSFelipe Balbi 	u32 count;
4546b15a762fSFelipe Balbi 
4547fc8bb91bSFelipe Balbi 	if (pm_runtime_suspended(dwc->dev)) {
45483ddaa6a2SElson Roy Serrao 		dwc->pending_events = true;
45493ddaa6a2SElson Roy Serrao 		/*
45503ddaa6a2SElson Roy Serrao 		 * Trigger runtime resume. The get() function will be balanced
45513ddaa6a2SElson Roy Serrao 		 * after processing the pending events in dwc3_process_pending
45523ddaa6a2SElson Roy Serrao 		 * events().
45533ddaa6a2SElson Roy Serrao 		 */
4554fc8bb91bSFelipe Balbi 		pm_runtime_get(dwc->dev);
4555fc8bb91bSFelipe Balbi 		disable_irq_nosync(dwc->irq_gadget);
4556fc8bb91bSFelipe Balbi 		return IRQ_HANDLED;
4557fc8bb91bSFelipe Balbi 	}
4558fc8bb91bSFelipe Balbi 
4559d325a1deSThinh Nguyen 	/*
4560d325a1deSThinh Nguyen 	 * With PCIe legacy interrupt, test shows that top-half irq handler can
4561d325a1deSThinh Nguyen 	 * be called again after HW interrupt deassertion. Check if bottom-half
4562d325a1deSThinh Nguyen 	 * irq event handler completes before caching new event to prevent
4563d325a1deSThinh Nguyen 	 * losing events.
4564d325a1deSThinh Nguyen 	 */
4565d325a1deSThinh Nguyen 	if (evt->flags & DWC3_EVENT_PENDING)
4566d325a1deSThinh Nguyen 		return IRQ_HANDLED;
4567d325a1deSThinh Nguyen 
4568660e9bdeSFelipe Balbi 	count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
4569b15a762fSFelipe Balbi 	count &= DWC3_GEVNTCOUNT_MASK;
4570b15a762fSFelipe Balbi 	if (!count)
4571b15a762fSFelipe Balbi 		return IRQ_NONE;
4572b15a762fSFelipe Balbi 
4573b15a762fSFelipe Balbi 	evt->count = count;
4574b15a762fSFelipe Balbi 	evt->flags |= DWC3_EVENT_PENDING;
4575b15a762fSFelipe Balbi 
4576e8adfc30SFelipe Balbi 	/* Mask interrupt */
457776c4c95dSThinh Nguyen 	dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0),
457876c4c95dSThinh Nguyen 		    DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length));
4579e8adfc30SFelipe Balbi 
4580ebbb2d59SJohn Youn 	amount = min(count, evt->length - evt->lpos);
4581ebbb2d59SJohn Youn 	memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
4582ebbb2d59SJohn Youn 
4583ebbb2d59SJohn Youn 	if (amount < count)
4584ebbb2d59SJohn Youn 		memcpy(evt->cache, evt->buf, count - amount);
4585ebbb2d59SJohn Youn 
458665aca320SJohn Youn 	dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
458765aca320SJohn Youn 
4588b15a762fSFelipe Balbi 	return IRQ_WAKE_THREAD;
458972246da4SFelipe Balbi }
459072246da4SFelipe Balbi 
dwc3_interrupt(int irq,void * _evt)4591dea520a4SFelipe Balbi static irqreturn_t dwc3_interrupt(int irq, void *_evt)
459272246da4SFelipe Balbi {
4593dea520a4SFelipe Balbi 	struct dwc3_event_buffer	*evt = _evt;
459472246da4SFelipe Balbi 
4595dea520a4SFelipe Balbi 	return dwc3_check_event_buf(evt);
459672246da4SFelipe Balbi }
459772246da4SFelipe Balbi 
dwc3_gadget_get_irq(struct dwc3 * dwc)45986db3812eSFelipe Balbi static int dwc3_gadget_get_irq(struct dwc3 *dwc)
45996db3812eSFelipe Balbi {
46006db3812eSFelipe Balbi 	struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
46016db3812eSFelipe Balbi 	int irq;
46026db3812eSFelipe Balbi 
4603f146b40bSHans de Goede 	irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral");
46046db3812eSFelipe Balbi 	if (irq > 0)
46056db3812eSFelipe Balbi 		goto out;
46066db3812eSFelipe Balbi 
46076db3812eSFelipe Balbi 	if (irq == -EPROBE_DEFER)
46086db3812eSFelipe Balbi 		goto out;
46096db3812eSFelipe Balbi 
4610f146b40bSHans de Goede 	irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
46116db3812eSFelipe Balbi 	if (irq > 0)
46126db3812eSFelipe Balbi 		goto out;
46136db3812eSFelipe Balbi 
46146db3812eSFelipe Balbi 	if (irq == -EPROBE_DEFER)
46156db3812eSFelipe Balbi 		goto out;
46166db3812eSFelipe Balbi 
46176db3812eSFelipe Balbi 	irq = platform_get_irq(dwc3_pdev, 0);
46186db3812eSFelipe Balbi 
46196db3812eSFelipe Balbi out:
46206db3812eSFelipe Balbi 	return irq;
46216db3812eSFelipe Balbi }
46226db3812eSFelipe Balbi 
dwc_gadget_release(struct device * dev)4623e81a7018SPeter Chen static void dwc_gadget_release(struct device *dev)
4624e81a7018SPeter Chen {
4625e81a7018SPeter Chen 	struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev);
4626e81a7018SPeter Chen 
4627e81a7018SPeter Chen 	kfree(gadget);
4628e81a7018SPeter Chen }
4629e81a7018SPeter Chen 
463072246da4SFelipe Balbi /**
4631bfad65eeSFelipe Balbi  * dwc3_gadget_init - initializes gadget related registers
46321d046793SPaul Zimmerman  * @dwc: pointer to our controller context structure
463372246da4SFelipe Balbi  *
463472246da4SFelipe Balbi  * Returns 0 on success otherwise negative errno.
463572246da4SFelipe Balbi  */
dwc3_gadget_init(struct dwc3 * dwc)463641ac7b3aSBill Pemberton int dwc3_gadget_init(struct dwc3 *dwc)
463772246da4SFelipe Balbi {
46386db3812eSFelipe Balbi 	int ret;
46396db3812eSFelipe Balbi 	int irq;
4640e81a7018SPeter Chen 	struct device *dev;
46419522def4SRoger Quadros 
46426db3812eSFelipe Balbi 	irq = dwc3_gadget_get_irq(dwc);
46436db3812eSFelipe Balbi 	if (irq < 0) {
46446db3812eSFelipe Balbi 		ret = irq;
46456db3812eSFelipe Balbi 		goto err0;
46469522def4SRoger Quadros 	}
46479522def4SRoger Quadros 
46489522def4SRoger Quadros 	dwc->irq_gadget = irq;
464972246da4SFelipe Balbi 
4650d64ff406SArnd Bergmann 	dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
4651d64ff406SArnd Bergmann 					  sizeof(*dwc->ep0_trb) * 2,
465272246da4SFelipe Balbi 					  &dwc->ep0_trb_addr, GFP_KERNEL);
465372246da4SFelipe Balbi 	if (!dwc->ep0_trb) {
465472246da4SFelipe Balbi 		dev_err(dwc->dev, "failed to allocate ep0 trb\n");
465572246da4SFelipe Balbi 		ret = -ENOMEM;
46567d5e650aSFelipe Balbi 		goto err0;
465772246da4SFelipe Balbi 	}
465872246da4SFelipe Balbi 
46594199c5f8SFelipe Balbi 	dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
466072246da4SFelipe Balbi 	if (!dwc->setup_buf) {
466172246da4SFelipe Balbi 		ret = -ENOMEM;
46627d5e650aSFelipe Balbi 		goto err1;
466372246da4SFelipe Balbi 	}
466472246da4SFelipe Balbi 
4665905dc04eSFelipe Balbi 	dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
4666905dc04eSFelipe Balbi 			&dwc->bounce_addr, GFP_KERNEL);
4667905dc04eSFelipe Balbi 	if (!dwc->bounce) {
4668905dc04eSFelipe Balbi 		ret = -ENOMEM;
4669d6e5a549SFelipe Balbi 		goto err2;
4670905dc04eSFelipe Balbi 	}
4671905dc04eSFelipe Balbi 
4672bb014736SBaolin Wang 	init_completion(&dwc->ep0_in_setup);
4673e81a7018SPeter Chen 	dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL);
4674e81a7018SPeter Chen 	if (!dwc->gadget) {
4675e81a7018SPeter Chen 		ret = -ENOMEM;
4676e81a7018SPeter Chen 		goto err3;
4677e81a7018SPeter Chen 	}
4678bb014736SBaolin Wang 
4679e81a7018SPeter Chen 
4680268bbde7SAndy Shevchenko 	usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release);
4681e81a7018SPeter Chen 	dev				= &dwc->gadget->dev;
4682e81a7018SPeter Chen 	dev->platform_data		= dwc;
4683e81a7018SPeter Chen 	dwc->gadget->ops		= &dwc3_gadget_ops;
4684e81a7018SPeter Chen 	dwc->gadget->speed		= USB_SPEED_UNKNOWN;
4685f551037cSThinh Nguyen 	dwc->gadget->ssp_rate		= USB_SSP_GEN_UNKNOWN;
4686e81a7018SPeter Chen 	dwc->gadget->sg_supported	= true;
4687e81a7018SPeter Chen 	dwc->gadget->name		= "dwc3-gadget";
4688475e8be5SThinh Nguyen 	dwc->gadget->lpm_capable	= !dwc->usb2_gadget_lpm_disable;
468904716168SElson Roy Serrao 	dwc->gadget->wakeup_capable	= true;
469072246da4SFelipe Balbi 
469172246da4SFelipe Balbi 	/*
4692b9e51b2bSBen McCauley 	 * FIXME We might be setting max_speed to <SUPER, however versions
4693b9e51b2bSBen McCauley 	 * <2.20a of dwc3 have an issue with metastability (documented
4694b9e51b2bSBen McCauley 	 * elsewhere in this driver) which tells us we can't set max speed to
4695b9e51b2bSBen McCauley 	 * anything lower than SUPER.
4696b9e51b2bSBen McCauley 	 *
4697b9e51b2bSBen McCauley 	 * Because gadget.max_speed is only used by composite.c and function
4698b9e51b2bSBen McCauley 	 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
4699b9e51b2bSBen McCauley 	 * to happen so we avoid sending SuperSpeed Capability descriptor
4700b9e51b2bSBen McCauley 	 * together with our BOS descriptor as that could confuse host into
4701b9e51b2bSBen McCauley 	 * thinking we can handle super speed.
4702b9e51b2bSBen McCauley 	 *
4703b9e51b2bSBen McCauley 	 * Note that, in fact, we won't even support GetBOS requests when speed
4704b9e51b2bSBen McCauley 	 * is less than super speed because we don't have means, yet, to tell
4705b9e51b2bSBen McCauley 	 * composite.c that we are USB 2.0 + LPM ECN.
4706b9e51b2bSBen McCauley 	 */
47079af21dd6SThinh Nguyen 	if (DWC3_VER_IS_PRIOR(DWC3, 220A) &&
470842bf02ecSRoger Quadros 	    !dwc->dis_metastability_quirk)
47095eb30cedSFelipe Balbi 		dev_info(dwc->dev, "changing max_speed on rev %08x\n",
4710b9e51b2bSBen McCauley 				dwc->revision);
4711b9e51b2bSBen McCauley 
4712e81a7018SPeter Chen 	dwc->gadget->max_speed		= dwc->maximum_speed;
471367848146SThinh Nguyen 	dwc->gadget->max_ssp_rate	= dwc->max_ssp_rate;
4714b9e51b2bSBen McCauley 
4715b9e51b2bSBen McCauley 	/*
471672246da4SFelipe Balbi 	 * REVISIT: Here we should clear all pending IRQs to be
471772246da4SFelipe Balbi 	 * sure we're starting from a well known location.
471872246da4SFelipe Balbi 	 */
471972246da4SFelipe Balbi 
4720f3bcfc7eSBryan O'Donoghue 	ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
472172246da4SFelipe Balbi 	if (ret)
4722d6e5a549SFelipe Balbi 		goto err4;
4723e81a7018SPeter Chen 
4724e81a7018SPeter Chen 	ret = usb_add_gadget(dwc->gadget);
4725e81a7018SPeter Chen 	if (ret) {
4726e81a7018SPeter Chen 		dev_err(dwc->dev, "failed to add gadget\n");
4727e81a7018SPeter Chen 		goto err5;
472872246da4SFelipe Balbi 	}
472972246da4SFelipe Balbi 
4730072cab8aSThinh Nguyen 	if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS)
4731072cab8aSThinh Nguyen 		dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate);
4732072cab8aSThinh Nguyen 	else
4733e81a7018SPeter Chen 		dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed);
4734169e3b68SRoger Quadros 
4735fd2304f4SThinh Nguyen 	/* No system wakeup if no gadget driver bound */
4736fd2304f4SThinh Nguyen 	if (dwc->sys_wakeup)
4737fd2304f4SThinh Nguyen 		device_wakeup_disable(dwc->sysdev);
4738fd2304f4SThinh Nguyen 
473972246da4SFelipe Balbi 	return 0;
47404199c5f8SFelipe Balbi 
4741e81a7018SPeter Chen err5:
4742d6e5a549SFelipe Balbi 	dwc3_gadget_free_endpoints(dwc);
4743e81a7018SPeter Chen err4:
4744e81a7018SPeter Chen 	usb_put_gadget(dwc->gadget);
474503715ea2SJack Pham 	dwc->gadget = NULL;
47467d5e650aSFelipe Balbi err3:
4747d6e5a549SFelipe Balbi 	dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4748d6e5a549SFelipe Balbi 			dwc->bounce_addr);
47495812b1c2SFelipe Balbi 
47507d5e650aSFelipe Balbi err2:
47510fc9a1beSFelipe Balbi 	kfree(dwc->setup_buf);
475272246da4SFelipe Balbi 
47537d5e650aSFelipe Balbi err1:
4754d64ff406SArnd Bergmann 	dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
475572246da4SFelipe Balbi 			dwc->ep0_trb, dwc->ep0_trb_addr);
475672246da4SFelipe Balbi 
475772246da4SFelipe Balbi err0:
475872246da4SFelipe Balbi 	return ret;
475972246da4SFelipe Balbi }
476072246da4SFelipe Balbi 
47617415f17cSFelipe Balbi /* -------------------------------------------------------------------------- */
47627415f17cSFelipe Balbi 
dwc3_gadget_exit(struct dwc3 * dwc)476372246da4SFelipe Balbi void dwc3_gadget_exit(struct dwc3 *dwc)
476472246da4SFelipe Balbi {
476503715ea2SJack Pham 	if (!dwc->gadget)
476603715ea2SJack Pham 		return;
476703715ea2SJack Pham 
4768000f9944SThinh Nguyen 	dwc3_enable_susphy(dwc, false);
4769bb9c74a5SJack Pham 	usb_del_gadget(dwc->gadget);
477072246da4SFelipe Balbi 	dwc3_gadget_free_endpoints(dwc);
4771bb9c74a5SJack Pham 	usb_put_gadget(dwc->gadget);
4772905dc04eSFelipe Balbi 	dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
4773905dc04eSFelipe Balbi 			  dwc->bounce_addr);
47740fc9a1beSFelipe Balbi 	kfree(dwc->setup_buf);
4775d64ff406SArnd Bergmann 	dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
477672246da4SFelipe Balbi 			  dwc->ep0_trb, dwc->ep0_trb_addr);
477772246da4SFelipe Balbi }
47787415f17cSFelipe Balbi 
dwc3_gadget_suspend(struct dwc3 * dwc)47790b0231aaSFelipe Balbi int dwc3_gadget_suspend(struct dwc3 *dwc)
47807415f17cSFelipe Balbi {
47815265397fSWesley Cheng 	unsigned long flags;
4782c8540870SRoger Quadros 	int ret;
47835265397fSWesley Cheng 
4784c8540870SRoger Quadros 	ret = dwc3_gadget_soft_disconnect(dwc);
4785c8540870SRoger Quadros 	if (ret)
4786c8540870SRoger Quadros 		goto err;
47875265397fSWesley Cheng 
47885265397fSWesley Cheng 	spin_lock_irqsave(&dwc->lock, flags);
4789c7ebd814SUttkarsh Aggarwal 	if (dwc->gadget_driver)
47909f8a67b6SFelipe Balbi 		dwc3_disconnect_gadget(dwc);
47915265397fSWesley Cheng 	spin_unlock_irqrestore(&dwc->lock, flags);
47927415f17cSFelipe Balbi 
47937415f17cSFelipe Balbi 	return 0;
4794c8540870SRoger Quadros 
4795c8540870SRoger Quadros err:
4796c8540870SRoger Quadros 	/*
4797c8540870SRoger Quadros 	 * Attempt to reset the controller's state. Likely no
4798c8540870SRoger Quadros 	 * communication can be established until the host
4799c8540870SRoger Quadros 	 * performs a port reset.
4800c8540870SRoger Quadros 	 */
4801c8540870SRoger Quadros 	if (dwc->softconnect)
4802c8540870SRoger Quadros 		dwc3_gadget_soft_connect(dwc);
4803c8540870SRoger Quadros 
4804c8540870SRoger Quadros 	return ret;
48057415f17cSFelipe Balbi }
48067415f17cSFelipe Balbi 
dwc3_gadget_resume(struct dwc3 * dwc)48077415f17cSFelipe Balbi int dwc3_gadget_resume(struct dwc3 *dwc)
48087415f17cSFelipe Balbi {
48098217f07aSWesley Cheng 	if (!dwc->gadget_driver || !dwc->softconnect)
48109772b47aSRoger Quadros 		return 0;
48119772b47aSRoger Quadros 
4812c8540870SRoger Quadros 	return dwc3_gadget_soft_connect(dwc);
48137415f17cSFelipe Balbi }
4814