xref: /linux/drivers/usb/dwc2/hcd.c (revision 1e111e885238bd861fd64273aab2ad1f803e34a3)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2197ba5f4SPaul Zimmerman /*
3197ba5f4SPaul Zimmerman  * hcd.c - DesignWare HS OTG Controller host-mode routines
4197ba5f4SPaul Zimmerman  *
5197ba5f4SPaul Zimmerman  * Copyright (C) 2004-2013 Synopsys, Inc.
6197ba5f4SPaul Zimmerman  *
7197ba5f4SPaul Zimmerman  * Redistribution and use in source and binary forms, with or without
8197ba5f4SPaul Zimmerman  * modification, are permitted provided that the following conditions
9197ba5f4SPaul Zimmerman  * are met:
10197ba5f4SPaul Zimmerman  * 1. Redistributions of source code must retain the above copyright
11197ba5f4SPaul Zimmerman  *    notice, this list of conditions, and the following disclaimer,
12197ba5f4SPaul Zimmerman  *    without modification.
13197ba5f4SPaul Zimmerman  * 2. Redistributions in binary form must reproduce the above copyright
14197ba5f4SPaul Zimmerman  *    notice, this list of conditions and the following disclaimer in the
15197ba5f4SPaul Zimmerman  *    documentation and/or other materials provided with the distribution.
16197ba5f4SPaul Zimmerman  * 3. The names of the above-listed copyright holders may not be used
17197ba5f4SPaul Zimmerman  *    to endorse or promote products derived from this software without
18197ba5f4SPaul Zimmerman  *    specific prior written permission.
19197ba5f4SPaul Zimmerman  *
20197ba5f4SPaul Zimmerman  * ALTERNATIVELY, this software may be distributed under the terms of the
21197ba5f4SPaul Zimmerman  * GNU General Public License ("GPL") as published by the Free Software
22197ba5f4SPaul Zimmerman  * Foundation; either version 2 of the License, or (at your option) any
23197ba5f4SPaul Zimmerman  * later version.
24197ba5f4SPaul Zimmerman  *
25197ba5f4SPaul Zimmerman  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26197ba5f4SPaul Zimmerman  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27197ba5f4SPaul Zimmerman  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28197ba5f4SPaul Zimmerman  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29197ba5f4SPaul Zimmerman  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30197ba5f4SPaul Zimmerman  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31197ba5f4SPaul Zimmerman  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32197ba5f4SPaul Zimmerman  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33197ba5f4SPaul Zimmerman  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34197ba5f4SPaul Zimmerman  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35197ba5f4SPaul Zimmerman  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36197ba5f4SPaul Zimmerman  */
37197ba5f4SPaul Zimmerman 
38197ba5f4SPaul Zimmerman /*
39197ba5f4SPaul Zimmerman  * This file contains the core HCD code, and implements the Linux hc_driver
40197ba5f4SPaul Zimmerman  * API
41197ba5f4SPaul Zimmerman  */
42197ba5f4SPaul Zimmerman #include <linux/kernel.h>
43197ba5f4SPaul Zimmerman #include <linux/module.h>
44197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
45197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
46348becdcSHeiner Kallweit #include <linux/platform_device.h>
47197ba5f4SPaul Zimmerman #include <linux/dma-mapping.h>
48197ba5f4SPaul Zimmerman #include <linux/delay.h>
49197ba5f4SPaul Zimmerman #include <linux/io.h>
50197ba5f4SPaul Zimmerman #include <linux/slab.h>
51197ba5f4SPaul Zimmerman #include <linux/usb.h>
52197ba5f4SPaul Zimmerman 
53197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
54197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
55197ba5f4SPaul Zimmerman 
56197ba5f4SPaul Zimmerman #include "core.h"
57197ba5f4SPaul Zimmerman #include "hcd.h"
58197ba5f4SPaul Zimmerman 
599156a7efSChen Yu static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
609156a7efSChen Yu 
61b02038faSJohn Youn /*
62b02038faSJohn Youn  * =========================================================================
63b02038faSJohn Youn  *  Host Core Layer Functions
64b02038faSJohn Youn  * =========================================================================
65b02038faSJohn Youn  */
66b02038faSJohn Youn 
67b02038faSJohn Youn /**
68b02038faSJohn Youn  * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
69b02038faSJohn Youn  * used in both device and host modes
70b02038faSJohn Youn  *
71b02038faSJohn Youn  * @hsotg: Programming view of the DWC_otg controller
72b02038faSJohn Youn  */
73b02038faSJohn Youn static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
74b02038faSJohn Youn {
75b02038faSJohn Youn 	u32 intmsk;
76b02038faSJohn Youn 
77b02038faSJohn Youn 	/* Clear any pending OTG Interrupts */
78b02038faSJohn Youn 	dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
79b02038faSJohn Youn 
80b02038faSJohn Youn 	/* Clear any pending interrupts */
81b02038faSJohn Youn 	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
82b02038faSJohn Youn 
83b02038faSJohn Youn 	/* Enable the interrupts in the GINTMSK */
84b02038faSJohn Youn 	intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
85b02038faSJohn Youn 
8695832c00SJohn Youn 	if (!hsotg->params.host_dma)
87b02038faSJohn Youn 		intmsk |= GINTSTS_RXFLVL;
8895832c00SJohn Youn 	if (!hsotg->params.external_id_pin_ctl)
89b02038faSJohn Youn 		intmsk |= GINTSTS_CONIDSTSCHNG;
90b02038faSJohn Youn 
91b02038faSJohn Youn 	intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
92b02038faSJohn Youn 		  GINTSTS_SESSREQINT;
93b02038faSJohn Youn 
94376f0401SSevak Arakelyan 	if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
95376f0401SSevak Arakelyan 		intmsk |= GINTSTS_LPMTRANRCVD;
96376f0401SSevak Arakelyan 
97b02038faSJohn Youn 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
98b02038faSJohn Youn }
99b02038faSJohn Youn 
100b02038faSJohn Youn /*
101b02038faSJohn Youn  * Initializes the FSLSPClkSel field of the HCFG register depending on the
102b02038faSJohn Youn  * PHY type
103b02038faSJohn Youn  */
104b02038faSJohn Youn static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
105b02038faSJohn Youn {
106b02038faSJohn Youn 	u32 hcfg, val;
107b02038faSJohn Youn 
108b02038faSJohn Youn 	if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
109b02038faSJohn Youn 	     hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
11095832c00SJohn Youn 	     hsotg->params.ulpi_fs_ls) ||
111bea8e86cSJohn Youn 	    hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
112b02038faSJohn Youn 		/* Full speed PHY */
113b02038faSJohn Youn 		val = HCFG_FSLSPCLKSEL_48_MHZ;
114b02038faSJohn Youn 	} else {
115b02038faSJohn Youn 		/* High speed PHY running at full speed or high speed */
116b02038faSJohn Youn 		val = HCFG_FSLSPCLKSEL_30_60_MHZ;
117b02038faSJohn Youn 	}
118b02038faSJohn Youn 
119b02038faSJohn Youn 	dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
120b02038faSJohn Youn 	hcfg = dwc2_readl(hsotg->regs + HCFG);
121b02038faSJohn Youn 	hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
122b02038faSJohn Youn 	hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
123b02038faSJohn Youn 	dwc2_writel(hcfg, hsotg->regs + HCFG);
124b02038faSJohn Youn }
125b02038faSJohn Youn 
126b02038faSJohn Youn static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
127b02038faSJohn Youn {
128e35b1350SBruno Herrera 	u32 usbcfg, ggpio, i2cctl;
129b02038faSJohn Youn 	int retval = 0;
130b02038faSJohn Youn 
131b02038faSJohn Youn 	/*
132b02038faSJohn Youn 	 * core_init() is now called on every switch so only call the
133b02038faSJohn Youn 	 * following for the first time through
134b02038faSJohn Youn 	 */
135b02038faSJohn Youn 	if (select_phy) {
136b02038faSJohn Youn 		dev_dbg(hsotg->dev, "FS PHY selected\n");
137b02038faSJohn Youn 
138b02038faSJohn Youn 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
139b02038faSJohn Youn 		if (!(usbcfg & GUSBCFG_PHYSEL)) {
140b02038faSJohn Youn 			usbcfg |= GUSBCFG_PHYSEL;
141b02038faSJohn Youn 			dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
142b02038faSJohn Youn 
143b02038faSJohn Youn 			/* Reset after a PHY select */
14413b1f8e2SVardan Mikayelyan 			retval = dwc2_core_reset(hsotg, false);
145b02038faSJohn Youn 
146b02038faSJohn Youn 			if (retval) {
147b02038faSJohn Youn 				dev_err(hsotg->dev,
148b02038faSJohn Youn 					"%s: Reset failed, aborting", __func__);
149b02038faSJohn Youn 				return retval;
150b02038faSJohn Youn 			}
151b02038faSJohn Youn 		}
152e35b1350SBruno Herrera 
153e35b1350SBruno Herrera 		if (hsotg->params.activate_stm_fs_transceiver) {
154e35b1350SBruno Herrera 			ggpio = dwc2_readl(hsotg->regs + GGPIO);
155e35b1350SBruno Herrera 			if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
156e35b1350SBruno Herrera 				dev_dbg(hsotg->dev, "Activating transceiver\n");
157e35b1350SBruno Herrera 				/*
158e35b1350SBruno Herrera 				 * STM32F4x9 uses the GGPIO register as general
159e35b1350SBruno Herrera 				 * core configuration register.
160e35b1350SBruno Herrera 				 */
161e35b1350SBruno Herrera 				ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
162e35b1350SBruno Herrera 				dwc2_writel(ggpio, hsotg->regs + GGPIO);
163e35b1350SBruno Herrera 			}
164e35b1350SBruno Herrera 		}
165b02038faSJohn Youn 	}
166b02038faSJohn Youn 
167b02038faSJohn Youn 	/*
168b02038faSJohn Youn 	 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
169b02038faSJohn Youn 	 * do this on HNP Dev/Host mode switches (done in dev_init and
170b02038faSJohn Youn 	 * host_init).
171b02038faSJohn Youn 	 */
172b02038faSJohn Youn 	if (dwc2_is_host_mode(hsotg))
173b02038faSJohn Youn 		dwc2_init_fs_ls_pclk_sel(hsotg);
174b02038faSJohn Youn 
17595832c00SJohn Youn 	if (hsotg->params.i2c_enable) {
176b02038faSJohn Youn 		dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
177b02038faSJohn Youn 
178b02038faSJohn Youn 		/* Program GUSBCFG.OtgUtmiFsSel to I2C */
179b02038faSJohn Youn 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
180b02038faSJohn Youn 		usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
181b02038faSJohn Youn 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
182b02038faSJohn Youn 
183b02038faSJohn Youn 		/* Program GI2CCTL.I2CEn */
184b02038faSJohn Youn 		i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
185b02038faSJohn Youn 		i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
186b02038faSJohn Youn 		i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
187b02038faSJohn Youn 		i2cctl &= ~GI2CCTL_I2CEN;
188b02038faSJohn Youn 		dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
189b02038faSJohn Youn 		i2cctl |= GI2CCTL_I2CEN;
190b02038faSJohn Youn 		dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
191b02038faSJohn Youn 	}
192b02038faSJohn Youn 
193b02038faSJohn Youn 	return retval;
194b02038faSJohn Youn }
195b02038faSJohn Youn 
196b02038faSJohn Youn static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
197b02038faSJohn Youn {
198b02038faSJohn Youn 	u32 usbcfg, usbcfg_old;
199b02038faSJohn Youn 	int retval = 0;
200b02038faSJohn Youn 
201b02038faSJohn Youn 	if (!select_phy)
202b02038faSJohn Youn 		return 0;
203b02038faSJohn Youn 
204b02038faSJohn Youn 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
205b02038faSJohn Youn 	usbcfg_old = usbcfg;
206b02038faSJohn Youn 
207b02038faSJohn Youn 	/*
208b02038faSJohn Youn 	 * HS PHY parameters. These parameters are preserved during soft reset
209b02038faSJohn Youn 	 * so only program the first time. Do a soft reset immediately after
210b02038faSJohn Youn 	 * setting phyif.
211b02038faSJohn Youn 	 */
212bea8e86cSJohn Youn 	switch (hsotg->params.phy_type) {
213b02038faSJohn Youn 	case DWC2_PHY_TYPE_PARAM_ULPI:
214b02038faSJohn Youn 		/* ULPI interface */
215b02038faSJohn Youn 		dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
216b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
217b02038faSJohn Youn 		usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
21895832c00SJohn Youn 		if (hsotg->params.phy_ulpi_ddr)
219b02038faSJohn Youn 			usbcfg |= GUSBCFG_DDRSEL;
220b11633c4SDinh Nguyen 
221b11633c4SDinh Nguyen 		/* Set external VBUS indicator as needed. */
222b11633c4SDinh Nguyen 		if (hsotg->params.oc_disable)
223b11633c4SDinh Nguyen 			usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
224b11633c4SDinh Nguyen 				   GUSBCFG_INDICATORPASSTHROUGH);
225b02038faSJohn Youn 		break;
226b02038faSJohn Youn 	case DWC2_PHY_TYPE_PARAM_UTMI:
227b02038faSJohn Youn 		/* UTMI+ interface */
228b02038faSJohn Youn 		dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
229b02038faSJohn Youn 		usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
230bea8e86cSJohn Youn 		if (hsotg->params.phy_utmi_width == 16)
231b02038faSJohn Youn 			usbcfg |= GUSBCFG_PHYIF16;
232b02038faSJohn Youn 		break;
233b02038faSJohn Youn 	default:
234b02038faSJohn Youn 		dev_err(hsotg->dev, "FS PHY selected at HS!\n");
235b02038faSJohn Youn 		break;
236b02038faSJohn Youn 	}
237b02038faSJohn Youn 
238b02038faSJohn Youn 	if (usbcfg != usbcfg_old) {
239b02038faSJohn Youn 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
240b02038faSJohn Youn 
241b02038faSJohn Youn 		/* Reset after setting the PHY parameters */
24213b1f8e2SVardan Mikayelyan 		retval = dwc2_core_reset(hsotg, false);
243b02038faSJohn Youn 		if (retval) {
244b02038faSJohn Youn 			dev_err(hsotg->dev,
245b02038faSJohn Youn 				"%s: Reset failed, aborting", __func__);
246b02038faSJohn Youn 			return retval;
247b02038faSJohn Youn 		}
248b02038faSJohn Youn 	}
249b02038faSJohn Youn 
250b02038faSJohn Youn 	return retval;
251b02038faSJohn Youn }
252b02038faSJohn Youn 
253b02038faSJohn Youn static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
254b02038faSJohn Youn {
255b02038faSJohn Youn 	u32 usbcfg;
256b02038faSJohn Youn 	int retval = 0;
257b02038faSJohn Youn 
25838e9002bSVardan Mikayelyan 	if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
25938e9002bSVardan Mikayelyan 	     hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
260bea8e86cSJohn Youn 	    hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
26138e9002bSVardan Mikayelyan 		/* If FS/LS mode with FS/LS PHY */
262b02038faSJohn Youn 		retval = dwc2_fs_phy_init(hsotg, select_phy);
263b02038faSJohn Youn 		if (retval)
264b02038faSJohn Youn 			return retval;
265b02038faSJohn Youn 	} else {
266b02038faSJohn Youn 		/* High speed PHY */
267b02038faSJohn Youn 		retval = dwc2_hs_phy_init(hsotg, select_phy);
268b02038faSJohn Youn 		if (retval)
269b02038faSJohn Youn 			return retval;
270b02038faSJohn Youn 	}
271b02038faSJohn Youn 
272b02038faSJohn Youn 	if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
273b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
27495832c00SJohn Youn 	    hsotg->params.ulpi_fs_ls) {
275b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
276b02038faSJohn Youn 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
277b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_FS_LS;
278b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
279b02038faSJohn Youn 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
280b02038faSJohn Youn 	} else {
281b02038faSJohn Youn 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
282b02038faSJohn Youn 		usbcfg &= ~GUSBCFG_ULPI_FS_LS;
283b02038faSJohn Youn 		usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
284b02038faSJohn Youn 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
285b02038faSJohn Youn 	}
286b02038faSJohn Youn 
287b02038faSJohn Youn 	return retval;
288b02038faSJohn Youn }
289b02038faSJohn Youn 
290b02038faSJohn Youn static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
291b02038faSJohn Youn {
292b02038faSJohn Youn 	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
293b02038faSJohn Youn 
294b02038faSJohn Youn 	switch (hsotg->hw_params.arch) {
295b02038faSJohn Youn 	case GHWCFG2_EXT_DMA_ARCH:
296b02038faSJohn Youn 		dev_err(hsotg->dev, "External DMA Mode not supported\n");
297b02038faSJohn Youn 		return -EINVAL;
298b02038faSJohn Youn 
299b02038faSJohn Youn 	case GHWCFG2_INT_DMA_ARCH:
300b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Internal DMA Mode\n");
301bea8e86cSJohn Youn 		if (hsotg->params.ahbcfg != -1) {
302b02038faSJohn Youn 			ahbcfg &= GAHBCFG_CTRL_MASK;
303bea8e86cSJohn Youn 			ahbcfg |= hsotg->params.ahbcfg &
304b02038faSJohn Youn 				  ~GAHBCFG_CTRL_MASK;
305b02038faSJohn Youn 		}
306b02038faSJohn Youn 		break;
307b02038faSJohn Youn 
308b02038faSJohn Youn 	case GHWCFG2_SLAVE_ONLY_ARCH:
309b02038faSJohn Youn 	default:
310b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Slave Only Mode\n");
311b02038faSJohn Youn 		break;
312b02038faSJohn Youn 	}
313b02038faSJohn Youn 
31495832c00SJohn Youn 	if (hsotg->params.host_dma)
315b02038faSJohn Youn 		ahbcfg |= GAHBCFG_DMA_EN;
3169d729a7aSRazmik Karapetyan 	else
3179d729a7aSRazmik Karapetyan 		hsotg->params.dma_desc_enable = false;
318b02038faSJohn Youn 
319b02038faSJohn Youn 	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
320b02038faSJohn Youn 
321b02038faSJohn Youn 	return 0;
322b02038faSJohn Youn }
323b02038faSJohn Youn 
324b02038faSJohn Youn static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
325b02038faSJohn Youn {
326b02038faSJohn Youn 	u32 usbcfg;
327b02038faSJohn Youn 
328b02038faSJohn Youn 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
329b02038faSJohn Youn 	usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
330b02038faSJohn Youn 
331b02038faSJohn Youn 	switch (hsotg->hw_params.op_mode) {
332b02038faSJohn Youn 	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
333bea8e86cSJohn Youn 		if (hsotg->params.otg_cap ==
334b02038faSJohn Youn 				DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
335b02038faSJohn Youn 			usbcfg |= GUSBCFG_HNPCAP;
336bea8e86cSJohn Youn 		if (hsotg->params.otg_cap !=
337b02038faSJohn Youn 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
338b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
339b02038faSJohn Youn 		break;
340b02038faSJohn Youn 
341b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
342b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
343b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
344bea8e86cSJohn Youn 		if (hsotg->params.otg_cap !=
345b02038faSJohn Youn 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
346b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
347b02038faSJohn Youn 		break;
348b02038faSJohn Youn 
349b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
350b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
351b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
352b02038faSJohn Youn 	default:
353b02038faSJohn Youn 		break;
354b02038faSJohn Youn 	}
355b02038faSJohn Youn 
356b02038faSJohn Youn 	dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
357b02038faSJohn Youn }
358b02038faSJohn Youn 
359531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
360531ef5ebSAmelie Delaunay {
361a7ef2074STomeu Vizoso 	int ret;
362a7ef2074STomeu Vizoso 
363531ef5ebSAmelie Delaunay 	hsotg->vbus_supply = devm_regulator_get_optional(hsotg->dev, "vbus");
364a7ef2074STomeu Vizoso 	if (IS_ERR(hsotg->vbus_supply)) {
365a7ef2074STomeu Vizoso 		ret = PTR_ERR(hsotg->vbus_supply);
366a7ef2074STomeu Vizoso 		hsotg->vbus_supply = NULL;
367a7ef2074STomeu Vizoso 		return ret == -ENODEV ? 0 : ret;
368a7ef2074STomeu Vizoso 	}
369531ef5ebSAmelie Delaunay 
370531ef5ebSAmelie Delaunay 	return regulator_enable(hsotg->vbus_supply);
371531ef5ebSAmelie Delaunay }
372531ef5ebSAmelie Delaunay 
373531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
374531ef5ebSAmelie Delaunay {
375531ef5ebSAmelie Delaunay 	if (hsotg->vbus_supply)
376531ef5ebSAmelie Delaunay 		return regulator_disable(hsotg->vbus_supply);
377531ef5ebSAmelie Delaunay 
378531ef5ebSAmelie Delaunay 	return 0;
379531ef5ebSAmelie Delaunay }
380531ef5ebSAmelie Delaunay 
381b02038faSJohn Youn /**
382b02038faSJohn Youn  * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
383b02038faSJohn Youn  *
384b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
385b02038faSJohn Youn  */
386b02038faSJohn Youn static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
387b02038faSJohn Youn {
388b02038faSJohn Youn 	u32 intmsk;
389b02038faSJohn Youn 
390b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s()\n", __func__);
391b02038faSJohn Youn 
392b02038faSJohn Youn 	/* Disable all interrupts */
393b02038faSJohn Youn 	dwc2_writel(0, hsotg->regs + GINTMSK);
394b02038faSJohn Youn 	dwc2_writel(0, hsotg->regs + HAINTMSK);
395b02038faSJohn Youn 
396b02038faSJohn Youn 	/* Enable the common interrupts */
397b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
398b02038faSJohn Youn 
399b02038faSJohn Youn 	/* Enable host mode interrupts without disturbing common interrupts */
400b02038faSJohn Youn 	intmsk = dwc2_readl(hsotg->regs + GINTMSK);
401b02038faSJohn Youn 	intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
402b02038faSJohn Youn 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
403b02038faSJohn Youn }
404b02038faSJohn Youn 
405b02038faSJohn Youn /**
406b02038faSJohn Youn  * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
407b02038faSJohn Youn  *
408b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
409b02038faSJohn Youn  */
410b02038faSJohn Youn static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
411b02038faSJohn Youn {
412b02038faSJohn Youn 	u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
413b02038faSJohn Youn 
414b02038faSJohn Youn 	/* Disable host mode interrupts without disturbing common interrupts */
415b02038faSJohn Youn 	intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
416b02038faSJohn Youn 		    GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
417b02038faSJohn Youn 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
418b02038faSJohn Youn }
419b02038faSJohn Youn 
420b02038faSJohn Youn /*
421b02038faSJohn Youn  * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
422b02038faSJohn Youn  * For system that have a total fifo depth that is smaller than the default
423b02038faSJohn Youn  * RX + TX fifo size.
424b02038faSJohn Youn  *
425b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
426b02038faSJohn Youn  */
427b02038faSJohn Youn static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
428b02038faSJohn Youn {
429bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
430b02038faSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
431b02038faSJohn Youn 	u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
432b02038faSJohn Youn 
433b02038faSJohn Youn 	total_fifo_size = hw->total_fifo_size;
434b02038faSJohn Youn 	rxfsiz = params->host_rx_fifo_size;
435b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size;
436b02038faSJohn Youn 	ptxfsiz = params->host_perio_tx_fifo_size;
437b02038faSJohn Youn 
438b02038faSJohn Youn 	/*
439b02038faSJohn Youn 	 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
440b02038faSJohn Youn 	 * allocation with support for high bandwidth endpoints. Synopsys
441b02038faSJohn Youn 	 * defines MPS(Max Packet size) for a periodic EP=1024, and for
442b02038faSJohn Youn 	 * non-periodic as 512.
443b02038faSJohn Youn 	 */
444b02038faSJohn Youn 	if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
445b02038faSJohn Youn 		/*
446b02038faSJohn Youn 		 * For Buffer DMA mode/Scatter Gather DMA mode
447b02038faSJohn Youn 		 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
448b02038faSJohn Youn 		 * with n = number of host channel.
449b02038faSJohn Youn 		 * 2 * ((1024/4) + 2) = 516
450b02038faSJohn Youn 		 */
451b02038faSJohn Youn 		rxfsiz = 516 + hw->host_channels;
452b02038faSJohn Youn 
453b02038faSJohn Youn 		/*
454b02038faSJohn Youn 		 * min non-periodic tx fifo depth
455b02038faSJohn Youn 		 * 2 * (largest non-periodic USB packet used / 4)
456b02038faSJohn Youn 		 * 2 * (512/4) = 256
457b02038faSJohn Youn 		 */
458b02038faSJohn Youn 		nptxfsiz = 256;
459b02038faSJohn Youn 
460b02038faSJohn Youn 		/*
461b02038faSJohn Youn 		 * min periodic tx fifo depth
462b02038faSJohn Youn 		 * (largest packet size*MC)/4
463b02038faSJohn Youn 		 * (1024 * 3)/4 = 768
464b02038faSJohn Youn 		 */
465b02038faSJohn Youn 		ptxfsiz = 768;
466b02038faSJohn Youn 
467b02038faSJohn Youn 		params->host_rx_fifo_size = rxfsiz;
468b02038faSJohn Youn 		params->host_nperio_tx_fifo_size = nptxfsiz;
469b02038faSJohn Youn 		params->host_perio_tx_fifo_size = ptxfsiz;
470b02038faSJohn Youn 	}
471b02038faSJohn Youn 
472b02038faSJohn Youn 	/*
473b02038faSJohn Youn 	 * If the summation of RX, NPTX and PTX fifo sizes is still
474b02038faSJohn Youn 	 * bigger than the total_fifo_size, then we have a problem.
475b02038faSJohn Youn 	 *
476b02038faSJohn Youn 	 * We won't be able to allocate as many endpoints. Right now,
477b02038faSJohn Youn 	 * we're just printing an error message, but ideally this FIFO
478b02038faSJohn Youn 	 * allocation algorithm would be improved in the future.
479b02038faSJohn Youn 	 *
480b02038faSJohn Youn 	 * FIXME improve this FIFO allocation algorithm.
481b02038faSJohn Youn 	 */
482b02038faSJohn Youn 	if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
483b02038faSJohn Youn 		dev_err(hsotg->dev, "invalid fifo sizes\n");
484b02038faSJohn Youn }
485b02038faSJohn Youn 
486b02038faSJohn Youn static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
487b02038faSJohn Youn {
488bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
489b02038faSJohn Youn 	u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
490b02038faSJohn Youn 
491b02038faSJohn Youn 	if (!params->enable_dynamic_fifo)
492b02038faSJohn Youn 		return;
493b02038faSJohn Youn 
494b02038faSJohn Youn 	dwc2_calculate_dynamic_fifo(hsotg);
495b02038faSJohn Youn 
496b02038faSJohn Youn 	/* Rx FIFO */
497b02038faSJohn Youn 	grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
498b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
499b02038faSJohn Youn 	grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
500b02038faSJohn Youn 	grxfsiz |= params->host_rx_fifo_size <<
501b02038faSJohn Youn 		   GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
502b02038faSJohn Youn 	dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
503b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
504b02038faSJohn Youn 		dwc2_readl(hsotg->regs + GRXFSIZ));
505b02038faSJohn Youn 
506b02038faSJohn Youn 	/* Non-periodic Tx FIFO */
507b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
508b02038faSJohn Youn 		dwc2_readl(hsotg->regs + GNPTXFSIZ));
509b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size <<
510b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
511b02038faSJohn Youn 	nptxfsiz |= params->host_rx_fifo_size <<
512b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
513b02038faSJohn Youn 	dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
514b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
515b02038faSJohn Youn 		dwc2_readl(hsotg->regs + GNPTXFSIZ));
516b02038faSJohn Youn 
517b02038faSJohn Youn 	/* Periodic Tx FIFO */
518b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
519b02038faSJohn Youn 		dwc2_readl(hsotg->regs + HPTXFSIZ));
520b02038faSJohn Youn 	hptxfsiz = params->host_perio_tx_fifo_size <<
521b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
522b02038faSJohn Youn 	hptxfsiz |= (params->host_rx_fifo_size +
523b02038faSJohn Youn 		     params->host_nperio_tx_fifo_size) <<
524b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
525b02038faSJohn Youn 	dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
526b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
527b02038faSJohn Youn 		dwc2_readl(hsotg->regs + HPTXFSIZ));
528b02038faSJohn Youn 
52995832c00SJohn Youn 	if (hsotg->params.en_multiple_tx_fifo &&
530e1f411d1SSevak Arakelyan 	    hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
531b02038faSJohn Youn 		/*
532e1f411d1SSevak Arakelyan 		 * This feature was implemented in 2.91a version
533b02038faSJohn Youn 		 * Global DFIFOCFG calculation for Host mode -
534b02038faSJohn Youn 		 * include RxFIFO, NPTXFIFO and HPTXFIFO
535b02038faSJohn Youn 		 */
536b02038faSJohn Youn 		dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
537b02038faSJohn Youn 		dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
538b02038faSJohn Youn 		dfifocfg |= (params->host_rx_fifo_size +
539b02038faSJohn Youn 			     params->host_nperio_tx_fifo_size +
540b02038faSJohn Youn 			     params->host_perio_tx_fifo_size) <<
541b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_SHIFT &
542b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_MASK;
543b02038faSJohn Youn 		dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
544b02038faSJohn Youn 	}
545b02038faSJohn Youn }
546b02038faSJohn Youn 
547b02038faSJohn Youn /**
548b02038faSJohn Youn  * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
549b02038faSJohn Youn  * the HFIR register according to PHY type and speed
550b02038faSJohn Youn  *
551b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
552b02038faSJohn Youn  *
553b02038faSJohn Youn  * NOTE: The caller can modify the value of the HFIR register only after the
554b02038faSJohn Youn  * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
555b02038faSJohn Youn  * has been set
556b02038faSJohn Youn  */
557b02038faSJohn Youn u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
558b02038faSJohn Youn {
559b02038faSJohn Youn 	u32 usbcfg;
560b02038faSJohn Youn 	u32 hprt0;
561b02038faSJohn Youn 	int clock = 60;	/* default value */
562b02038faSJohn Youn 
563b02038faSJohn Youn 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
564b02038faSJohn Youn 	hprt0 = dwc2_readl(hsotg->regs + HPRT0);
565b02038faSJohn Youn 
566b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
567b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_PHYIF16))
568b02038faSJohn Youn 		clock = 60;
569b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
570b02038faSJohn Youn 	    GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
571b02038faSJohn Youn 		clock = 48;
572b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
573b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
574b02038faSJohn Youn 		clock = 30;
575b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
576b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
577b02038faSJohn Youn 		clock = 60;
578b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
579b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
580b02038faSJohn Youn 		clock = 48;
581b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
582b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
583b02038faSJohn Youn 		clock = 48;
584b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) &&
585b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
586b02038faSJohn Youn 		clock = 48;
587b02038faSJohn Youn 
588b02038faSJohn Youn 	if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
589b02038faSJohn Youn 		/* High speed case */
590b02038faSJohn Youn 		return 125 * clock - 1;
591b02038faSJohn Youn 
592b02038faSJohn Youn 	/* FS/LS case */
593b02038faSJohn Youn 	return 1000 * clock - 1;
594b02038faSJohn Youn }
595b02038faSJohn Youn 
596b02038faSJohn Youn /**
597b02038faSJohn Youn  * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
598b02038faSJohn Youn  * buffer
599b02038faSJohn Youn  *
6006fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of DWC_otg controller
601b02038faSJohn Youn  * @dest:    Destination buffer for the packet
602b02038faSJohn Youn  * @bytes:   Number of bytes to copy to the destination
603b02038faSJohn Youn  */
604b02038faSJohn Youn void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
605b02038faSJohn Youn {
606b02038faSJohn Youn 	u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
607b02038faSJohn Youn 	u32 *data_buf = (u32 *)dest;
608b02038faSJohn Youn 	int word_count = (bytes + 3) / 4;
609b02038faSJohn Youn 	int i;
610b02038faSJohn Youn 
611b02038faSJohn Youn 	/*
612b02038faSJohn Youn 	 * Todo: Account for the case where dest is not dword aligned. This
613b02038faSJohn Youn 	 * requires reading data from the FIFO into a u32 temp buffer, then
614b02038faSJohn Youn 	 * moving it into the data buffer.
615b02038faSJohn Youn 	 */
616b02038faSJohn Youn 
617b02038faSJohn Youn 	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
618b02038faSJohn Youn 
619b02038faSJohn Youn 	for (i = 0; i < word_count; i++, data_buf++)
620b02038faSJohn Youn 		*data_buf = dwc2_readl(fifo);
621b02038faSJohn Youn }
622b02038faSJohn Youn 
623197ba5f4SPaul Zimmerman /**
624197ba5f4SPaul Zimmerman  * dwc2_dump_channel_info() - Prints the state of a host channel
625197ba5f4SPaul Zimmerman  *
626197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
627197ba5f4SPaul Zimmerman  * @chan:  Pointer to the channel to dump
628197ba5f4SPaul Zimmerman  *
629197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
630197ba5f4SPaul Zimmerman  *
631197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
632197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
633197ba5f4SPaul Zimmerman  */
634197ba5f4SPaul Zimmerman static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
635197ba5f4SPaul Zimmerman 				   struct dwc2_host_chan *chan)
636197ba5f4SPaul Zimmerman {
637197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
638bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
639197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
640197ba5f4SPaul Zimmerman 	u32 hcchar;
641197ba5f4SPaul Zimmerman 	u32 hcsplt;
642197ba5f4SPaul Zimmerman 	u32 hctsiz;
643197ba5f4SPaul Zimmerman 	u32 hc_dma;
644197ba5f4SPaul Zimmerman 	int i;
645197ba5f4SPaul Zimmerman 
646b02038faSJohn Youn 	if (!chan)
647197ba5f4SPaul Zimmerman 		return;
648197ba5f4SPaul Zimmerman 
64995c8bc36SAntti Seppälä 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
65095c8bc36SAntti Seppälä 	hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
65195c8bc36SAntti Seppälä 	hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
65295c8bc36SAntti Seppälä 	hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
653197ba5f4SPaul Zimmerman 
654197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Assigned to channel %p:\n", chan);
655197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hcchar 0x%08x, hcsplt 0x%08x\n",
656197ba5f4SPaul Zimmerman 		hcchar, hcsplt);
657197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hctsiz 0x%08x, hc_dma 0x%08x\n",
658197ba5f4SPaul Zimmerman 		hctsiz, hc_dma);
659197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
660197ba5f4SPaul Zimmerman 		chan->dev_addr, chan->ep_num, chan->ep_is_in);
661197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
662197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
663197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    data_pid_start: %d\n", chan->data_pid_start);
664197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_started: %d\n", chan->xfer_started);
665197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
666197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
667197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
668197ba5f4SPaul Zimmerman 		(unsigned long)chan->xfer_dma);
669197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
670197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
671197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP inactive sched:\n");
672197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
673197ba5f4SPaul Zimmerman 			    qh_list_entry)
674197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
67538d2b5fbSDouglas Anderson 	dev_dbg(hsotg->dev, "  NP waiting sched:\n");
67638d2b5fbSDouglas Anderson 	list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
67738d2b5fbSDouglas Anderson 			    qh_list_entry)
67838d2b5fbSDouglas Anderson 		dev_dbg(hsotg->dev, "    %p\n", qh);
679197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP active sched:\n");
680197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
681197ba5f4SPaul Zimmerman 			    qh_list_entry)
682197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
683197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Channels:\n");
684197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
685197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
686197ba5f4SPaul Zimmerman 
687197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %2d: %p\n", i, chan);
688197ba5f4SPaul Zimmerman 	}
689197ba5f4SPaul Zimmerman #endif /* VERBOSE_DEBUG */
690197ba5f4SPaul Zimmerman }
691197ba5f4SPaul Zimmerman 
6924411bebaSRazmik Karapetyan static int _dwc2_hcd_start(struct usb_hcd *hcd);
6934411bebaSRazmik Karapetyan 
6944411bebaSRazmik Karapetyan static void dwc2_host_start(struct dwc2_hsotg *hsotg)
6954411bebaSRazmik Karapetyan {
6964411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
6974411bebaSRazmik Karapetyan 
6984411bebaSRazmik Karapetyan 	hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
6994411bebaSRazmik Karapetyan 	_dwc2_hcd_start(hcd);
7004411bebaSRazmik Karapetyan }
7014411bebaSRazmik Karapetyan 
7024411bebaSRazmik Karapetyan static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
7034411bebaSRazmik Karapetyan {
7044411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
7054411bebaSRazmik Karapetyan 
7064411bebaSRazmik Karapetyan 	hcd->self.is_b_host = 0;
7074411bebaSRazmik Karapetyan }
7084411bebaSRazmik Karapetyan 
7094411bebaSRazmik Karapetyan static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
7104411bebaSRazmik Karapetyan 			       int *hub_addr, int *hub_port)
7114411bebaSRazmik Karapetyan {
7124411bebaSRazmik Karapetyan 	struct urb *urb = context;
7134411bebaSRazmik Karapetyan 
7144411bebaSRazmik Karapetyan 	if (urb->dev->tt)
7154411bebaSRazmik Karapetyan 		*hub_addr = urb->dev->tt->hub->devnum;
7164411bebaSRazmik Karapetyan 	else
7174411bebaSRazmik Karapetyan 		*hub_addr = 0;
7184411bebaSRazmik Karapetyan 	*hub_port = urb->dev->ttport;
7194411bebaSRazmik Karapetyan }
7204411bebaSRazmik Karapetyan 
721197ba5f4SPaul Zimmerman /*
722b02038faSJohn Youn  * =========================================================================
723b02038faSJohn Youn  *  Low Level Host Channel Access Functions
724b02038faSJohn Youn  * =========================================================================
725b02038faSJohn Youn  */
726b02038faSJohn Youn 
727b02038faSJohn Youn static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
728b02038faSJohn Youn 				      struct dwc2_host_chan *chan)
729b02038faSJohn Youn {
730b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
731b02038faSJohn Youn 
732b02038faSJohn Youn 	switch (chan->ep_type) {
733b02038faSJohn Youn 	case USB_ENDPOINT_XFER_CONTROL:
734b02038faSJohn Youn 	case USB_ENDPOINT_XFER_BULK:
735b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "control/bulk\n");
736b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
737b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
738b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
739b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
740b02038faSJohn Youn 		if (chan->ep_is_in) {
741b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
742b02038faSJohn Youn 		} else {
743b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
744b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NYET;
745b02038faSJohn Youn 			if (chan->do_ping)
746b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
747b02038faSJohn Youn 		}
748b02038faSJohn Youn 
749b02038faSJohn Youn 		if (chan->do_split) {
750b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
751b02038faSJohn Youn 			if (chan->complete_split)
752b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
753b02038faSJohn Youn 			else
754b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
755b02038faSJohn Youn 		}
756b02038faSJohn Youn 
757b02038faSJohn Youn 		if (chan->error_state)
758b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
759b02038faSJohn Youn 		break;
760b02038faSJohn Youn 
761b02038faSJohn Youn 	case USB_ENDPOINT_XFER_INT:
762b02038faSJohn Youn 		if (dbg_perio())
763b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "intr\n");
764b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
765b02038faSJohn Youn 		hcintmsk |= HCINTMSK_NAK;
766b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
767b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
768b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
769b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
770b02038faSJohn Youn 
771b02038faSJohn Youn 		if (chan->ep_is_in)
772b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
773b02038faSJohn Youn 		if (chan->error_state)
774b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
775b02038faSJohn Youn 		if (chan->do_split) {
776b02038faSJohn Youn 			if (chan->complete_split)
777b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
778b02038faSJohn Youn 			else
779b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
780b02038faSJohn Youn 		}
781b02038faSJohn Youn 		break;
782b02038faSJohn Youn 
783b02038faSJohn Youn 	case USB_ENDPOINT_XFER_ISOC:
784b02038faSJohn Youn 		if (dbg_perio())
785b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "isoc\n");
786b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
787b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
788b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
789b02038faSJohn Youn 
790b02038faSJohn Youn 		if (chan->ep_is_in) {
791b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XACTERR;
792b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
793b02038faSJohn Youn 		}
794b02038faSJohn Youn 		break;
795b02038faSJohn Youn 	default:
796b02038faSJohn Youn 		dev_err(hsotg->dev, "## Unknown EP type ##\n");
797b02038faSJohn Youn 		break;
798b02038faSJohn Youn 	}
799b02038faSJohn Youn 
800b02038faSJohn Youn 	dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
801b02038faSJohn Youn 	if (dbg_hc(chan))
802b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
803b02038faSJohn Youn }
804b02038faSJohn Youn 
805b02038faSJohn Youn static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
806b02038faSJohn Youn 				    struct dwc2_host_chan *chan)
807b02038faSJohn Youn {
808b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
809b02038faSJohn Youn 
810b02038faSJohn Youn 	/*
811b02038faSJohn Youn 	 * For Descriptor DMA mode core halts the channel on AHB error.
812b02038faSJohn Youn 	 * Interrupt is not required.
813b02038faSJohn Youn 	 */
81495832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
815b02038faSJohn Youn 		if (dbg_hc(chan))
816b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
817b02038faSJohn Youn 		hcintmsk |= HCINTMSK_AHBERR;
818b02038faSJohn Youn 	} else {
819b02038faSJohn Youn 		if (dbg_hc(chan))
820b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA enabled\n");
821b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
822b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XFERCOMPL;
823b02038faSJohn Youn 	}
824b02038faSJohn Youn 
825b02038faSJohn Youn 	if (chan->error_state && !chan->do_split &&
826b02038faSJohn Youn 	    chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
827b02038faSJohn Youn 		if (dbg_hc(chan))
828b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "setting ACK\n");
829b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
830b02038faSJohn Youn 		if (chan->ep_is_in) {
831b02038faSJohn Youn 			hcintmsk |= HCINTMSK_DATATGLERR;
832b02038faSJohn Youn 			if (chan->ep_type != USB_ENDPOINT_XFER_INT)
833b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NAK;
834b02038faSJohn Youn 		}
835b02038faSJohn Youn 	}
836b02038faSJohn Youn 
837b02038faSJohn Youn 	dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
838b02038faSJohn Youn 	if (dbg_hc(chan))
839b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
840b02038faSJohn Youn }
841b02038faSJohn Youn 
842b02038faSJohn Youn static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
843b02038faSJohn Youn 				struct dwc2_host_chan *chan)
844b02038faSJohn Youn {
845b02038faSJohn Youn 	u32 intmsk;
846b02038faSJohn Youn 
84795832c00SJohn Youn 	if (hsotg->params.host_dma) {
848b02038faSJohn Youn 		if (dbg_hc(chan))
849b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
850b02038faSJohn Youn 		dwc2_hc_enable_dma_ints(hsotg, chan);
851b02038faSJohn Youn 	} else {
852b02038faSJohn Youn 		if (dbg_hc(chan))
853b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA disabled\n");
854b02038faSJohn Youn 		dwc2_hc_enable_slave_ints(hsotg, chan);
855b02038faSJohn Youn 	}
856b02038faSJohn Youn 
857b02038faSJohn Youn 	/* Enable the top level host channel interrupt */
858b02038faSJohn Youn 	intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
859b02038faSJohn Youn 	intmsk |= 1 << chan->hc_num;
860b02038faSJohn Youn 	dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
861b02038faSJohn Youn 	if (dbg_hc(chan))
862b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
863b02038faSJohn Youn 
864b02038faSJohn Youn 	/* Make sure host channel interrupts are enabled */
865b02038faSJohn Youn 	intmsk = dwc2_readl(hsotg->regs + GINTMSK);
866b02038faSJohn Youn 	intmsk |= GINTSTS_HCHINT;
867b02038faSJohn Youn 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
868b02038faSJohn Youn 	if (dbg_hc(chan))
869b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
870b02038faSJohn Youn }
871b02038faSJohn Youn 
872b02038faSJohn Youn /**
873b02038faSJohn Youn  * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
874b02038faSJohn Youn  * a specific endpoint
875b02038faSJohn Youn  *
876b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
877b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
878b02038faSJohn Youn  *
879b02038faSJohn Youn  * The HCCHARn register is set up with the characteristics specified in chan.
880b02038faSJohn Youn  * Host channel interrupts that may need to be serviced while this transfer is
881b02038faSJohn Youn  * in progress are enabled.
882b02038faSJohn Youn  */
883b02038faSJohn Youn static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
884b02038faSJohn Youn {
885b02038faSJohn Youn 	u8 hc_num = chan->hc_num;
886b02038faSJohn Youn 	u32 hcintmsk;
887b02038faSJohn Youn 	u32 hcchar;
888b02038faSJohn Youn 	u32 hcsplt = 0;
889b02038faSJohn Youn 
890b02038faSJohn Youn 	if (dbg_hc(chan))
891b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
892b02038faSJohn Youn 
893b02038faSJohn Youn 	/* Clear old interrupt conditions for this host channel */
894b02038faSJohn Youn 	hcintmsk = 0xffffffff;
895b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
896b02038faSJohn Youn 	dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
897b02038faSJohn Youn 
898b02038faSJohn Youn 	/* Enable channel interrupts required for this transfer */
899b02038faSJohn Youn 	dwc2_hc_enable_ints(hsotg, chan);
900b02038faSJohn Youn 
901b02038faSJohn Youn 	/*
902b02038faSJohn Youn 	 * Program the HCCHARn register with the endpoint characteristics for
903b02038faSJohn Youn 	 * the current transfer
904b02038faSJohn Youn 	 */
905b02038faSJohn Youn 	hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
906b02038faSJohn Youn 	hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
907b02038faSJohn Youn 	if (chan->ep_is_in)
908b02038faSJohn Youn 		hcchar |= HCCHAR_EPDIR;
909b02038faSJohn Youn 	if (chan->speed == USB_SPEED_LOW)
910b02038faSJohn Youn 		hcchar |= HCCHAR_LSPDDEV;
911b02038faSJohn Youn 	hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
912b02038faSJohn Youn 	hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
913b02038faSJohn Youn 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
914b02038faSJohn Youn 	if (dbg_hc(chan)) {
915b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
916b02038faSJohn Youn 			 hc_num, hcchar);
917b02038faSJohn Youn 
918b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n",
919b02038faSJohn Youn 			 __func__, hc_num);
920b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Dev Addr: %d\n",
921b02038faSJohn Youn 			 chan->dev_addr);
922b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Num: %d\n",
923b02038faSJohn Youn 			 chan->ep_num);
924b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is In: %d\n",
925b02038faSJohn Youn 			 chan->ep_is_in);
926b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is Low Speed: %d\n",
927b02038faSJohn Youn 			 chan->speed == USB_SPEED_LOW);
928b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Type: %d\n",
929b02038faSJohn Youn 			 chan->ep_type);
930b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Max Pkt: %d\n",
931b02038faSJohn Youn 			 chan->max_packet);
932b02038faSJohn Youn 	}
933b02038faSJohn Youn 
934b02038faSJohn Youn 	/* Program the HCSPLT register for SPLITs */
935b02038faSJohn Youn 	if (chan->do_split) {
936b02038faSJohn Youn 		if (dbg_hc(chan))
937b02038faSJohn Youn 			dev_vdbg(hsotg->dev,
938b02038faSJohn Youn 				 "Programming HC %d with split --> %s\n",
939b02038faSJohn Youn 				 hc_num,
940b02038faSJohn Youn 				 chan->complete_split ? "CSPLIT" : "SSPLIT");
941b02038faSJohn Youn 		if (chan->complete_split)
942b02038faSJohn Youn 			hcsplt |= HCSPLT_COMPSPLT;
943b02038faSJohn Youn 		hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
944b02038faSJohn Youn 			  HCSPLT_XACTPOS_MASK;
945b02038faSJohn Youn 		hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
946b02038faSJohn Youn 			  HCSPLT_HUBADDR_MASK;
947b02038faSJohn Youn 		hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
948b02038faSJohn Youn 			  HCSPLT_PRTADDR_MASK;
949b02038faSJohn Youn 		if (dbg_hc(chan)) {
950b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  comp split %d\n",
951b02038faSJohn Youn 				 chan->complete_split);
952b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xact pos %d\n",
953b02038faSJohn Youn 				 chan->xact_pos);
954b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub addr %d\n",
955b02038faSJohn Youn 				 chan->hub_addr);
956b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub port %d\n",
957b02038faSJohn Youn 				 chan->hub_port);
958b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  is_in %d\n",
959b02038faSJohn Youn 				 chan->ep_is_in);
960b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  Max Pkt %d\n",
961b02038faSJohn Youn 				 chan->max_packet);
962b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xferlen %d\n",
963b02038faSJohn Youn 				 chan->xfer_len);
964b02038faSJohn Youn 		}
965b02038faSJohn Youn 	}
966b02038faSJohn Youn 
967b02038faSJohn Youn 	dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
968b02038faSJohn Youn }
969b02038faSJohn Youn 
970b02038faSJohn Youn /**
971b02038faSJohn Youn  * dwc2_hc_halt() - Attempts to halt a host channel
972b02038faSJohn Youn  *
973b02038faSJohn Youn  * @hsotg:       Controller register interface
974b02038faSJohn Youn  * @chan:        Host channel to halt
975b02038faSJohn Youn  * @halt_status: Reason for halting the channel
976b02038faSJohn Youn  *
977b02038faSJohn Youn  * This function should only be called in Slave mode or to abort a transfer in
978b02038faSJohn Youn  * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
979b02038faSJohn Youn  * controller halts the channel when the transfer is complete or a condition
980b02038faSJohn Youn  * occurs that requires application intervention.
981b02038faSJohn Youn  *
982b02038faSJohn Youn  * In slave mode, checks for a free request queue entry, then sets the Channel
983b02038faSJohn Youn  * Enable and Channel Disable bits of the Host Channel Characteristics
984b02038faSJohn Youn  * register of the specified channel to intiate the halt. If there is no free
985b02038faSJohn Youn  * request queue entry, sets only the Channel Disable bit of the HCCHARn
986b02038faSJohn Youn  * register to flush requests for this channel. In the latter case, sets a
987b02038faSJohn Youn  * flag to indicate that the host channel needs to be halted when a request
988b02038faSJohn Youn  * queue slot is open.
989b02038faSJohn Youn  *
990b02038faSJohn Youn  * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
991b02038faSJohn Youn  * HCCHARn register. The controller ensures there is space in the request
992b02038faSJohn Youn  * queue before submitting the halt request.
993b02038faSJohn Youn  *
994b02038faSJohn Youn  * Some time may elapse before the core flushes any posted requests for this
995b02038faSJohn Youn  * host channel and halts. The Channel Halted interrupt handler completes the
996b02038faSJohn Youn  * deactivation of the host channel.
997b02038faSJohn Youn  */
998b02038faSJohn Youn void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
999b02038faSJohn Youn 		  enum dwc2_halt_status halt_status)
1000b02038faSJohn Youn {
1001b02038faSJohn Youn 	u32 nptxsts, hptxsts, hcchar;
1002b02038faSJohn Youn 
1003b02038faSJohn Youn 	if (dbg_hc(chan))
1004b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1005a82c7abdSMinas Harutyunyan 
1006a82c7abdSMinas Harutyunyan 	/*
1007a82c7abdSMinas Harutyunyan 	 * In buffer DMA or external DMA mode channel can't be halted
1008a82c7abdSMinas Harutyunyan 	 * for non-split periodic channels. At the end of the next
1009a82c7abdSMinas Harutyunyan 	 * uframe/frame (in the worst case), the core generates a channel
1010a82c7abdSMinas Harutyunyan 	 * halted and disables the channel automatically.
1011a82c7abdSMinas Harutyunyan 	 */
1012a82c7abdSMinas Harutyunyan 	if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
1013a82c7abdSMinas Harutyunyan 	    hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
1014a82c7abdSMinas Harutyunyan 		if (!chan->do_split &&
1015a82c7abdSMinas Harutyunyan 		    (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
1016a82c7abdSMinas Harutyunyan 		     chan->ep_type == USB_ENDPOINT_XFER_INT)) {
1017a82c7abdSMinas Harutyunyan 			dev_err(hsotg->dev, "%s() Channel can't be halted\n",
1018a82c7abdSMinas Harutyunyan 				__func__);
1019a82c7abdSMinas Harutyunyan 			return;
1020a82c7abdSMinas Harutyunyan 		}
1021a82c7abdSMinas Harutyunyan 	}
1022a82c7abdSMinas Harutyunyan 
1023b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1024b02038faSJohn Youn 		dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1025b02038faSJohn Youn 
1026b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1027b02038faSJohn Youn 	    halt_status == DWC2_HC_XFER_AHB_ERR) {
1028b02038faSJohn Youn 		/*
1029b02038faSJohn Youn 		 * Disable all channel interrupts except Ch Halted. The QTD
1030b02038faSJohn Youn 		 * and QH state associated with this transfer has been cleared
1031b02038faSJohn Youn 		 * (in the case of URB_DEQUEUE), so the channel needs to be
1032b02038faSJohn Youn 		 * shut down carefully to prevent crashes.
1033b02038faSJohn Youn 		 */
1034b02038faSJohn Youn 		u32 hcintmsk = HCINTMSK_CHHLTD;
1035b02038faSJohn Youn 
1036b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "dequeue/error\n");
1037b02038faSJohn Youn 		dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1038b02038faSJohn Youn 
1039b02038faSJohn Youn 		/*
1040b02038faSJohn Youn 		 * Make sure no other interrupts besides halt are currently
1041b02038faSJohn Youn 		 * pending. Handling another interrupt could cause a crash due
1042b02038faSJohn Youn 		 * to the QTD and QH state.
1043b02038faSJohn Youn 		 */
1044b02038faSJohn Youn 		dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1045b02038faSJohn Youn 
1046b02038faSJohn Youn 		/*
1047b02038faSJohn Youn 		 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1048b02038faSJohn Youn 		 * even if the channel was already halted for some other
1049b02038faSJohn Youn 		 * reason
1050b02038faSJohn Youn 		 */
1051b02038faSJohn Youn 		chan->halt_status = halt_status;
1052b02038faSJohn Youn 
1053b02038faSJohn Youn 		hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1054b02038faSJohn Youn 		if (!(hcchar & HCCHAR_CHENA)) {
1055b02038faSJohn Youn 			/*
1056b02038faSJohn Youn 			 * The channel is either already halted or it hasn't
1057b02038faSJohn Youn 			 * started yet. In DMA mode, the transfer may halt if
1058b02038faSJohn Youn 			 * it finishes normally or a condition occurs that
1059b02038faSJohn Youn 			 * requires driver intervention. Don't want to halt
1060b02038faSJohn Youn 			 * the channel again. In either Slave or DMA mode,
1061b02038faSJohn Youn 			 * it's possible that the transfer has been assigned
1062b02038faSJohn Youn 			 * to a channel, but not started yet when an URB is
1063b02038faSJohn Youn 			 * dequeued. Don't want to halt a channel that hasn't
1064b02038faSJohn Youn 			 * started yet.
1065b02038faSJohn Youn 			 */
1066b02038faSJohn Youn 			return;
1067b02038faSJohn Youn 		}
1068b02038faSJohn Youn 	}
1069b02038faSJohn Youn 	if (chan->halt_pending) {
1070b02038faSJohn Youn 		/*
1071b02038faSJohn Youn 		 * A halt has already been issued for this channel. This might
1072b02038faSJohn Youn 		 * happen when a transfer is aborted by a higher level in
1073b02038faSJohn Youn 		 * the stack.
1074b02038faSJohn Youn 		 */
1075b02038faSJohn Youn 		dev_vdbg(hsotg->dev,
1076b02038faSJohn Youn 			 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1077b02038faSJohn Youn 			 __func__, chan->hc_num);
1078b02038faSJohn Youn 		return;
1079b02038faSJohn Youn 	}
1080b02038faSJohn Youn 
1081b02038faSJohn Youn 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1082b02038faSJohn Youn 
1083b02038faSJohn Youn 	/* No need to set the bit in DDMA for disabling the channel */
1084b02038faSJohn Youn 	/* TODO check it everywhere channel is disabled */
108595832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
1086b02038faSJohn Youn 		if (dbg_hc(chan))
1087b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1088b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
1089b02038faSJohn Youn 	} else {
1090b02038faSJohn Youn 		if (dbg_hc(chan))
1091b02038faSJohn Youn 			dev_dbg(hsotg->dev, "desc DMA enabled\n");
1092b02038faSJohn Youn 	}
1093b02038faSJohn Youn 	hcchar |= HCCHAR_CHDIS;
1094b02038faSJohn Youn 
109595832c00SJohn Youn 	if (!hsotg->params.host_dma) {
1096b02038faSJohn Youn 		if (dbg_hc(chan))
1097b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA not enabled\n");
1098b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
1099b02038faSJohn Youn 
1100b02038faSJohn Youn 		/* Check for space in the request queue to issue the halt */
1101b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1102b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1103b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "control/bulk\n");
1104b02038faSJohn Youn 			nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1105b02038faSJohn Youn 			if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1106b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "Disabling channel\n");
1107b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
1108b02038faSJohn Youn 			}
1109b02038faSJohn Youn 		} else {
1110b02038faSJohn Youn 			if (dbg_perio())
1111b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "isoc/intr\n");
1112b02038faSJohn Youn 			hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1113b02038faSJohn Youn 			if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1114b02038faSJohn Youn 			    hsotg->queuing_high_bandwidth) {
1115b02038faSJohn Youn 				if (dbg_perio())
1116b02038faSJohn Youn 					dev_vdbg(hsotg->dev, "Disabling channel\n");
1117b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
1118b02038faSJohn Youn 			}
1119b02038faSJohn Youn 		}
1120b02038faSJohn Youn 	} else {
1121b02038faSJohn Youn 		if (dbg_hc(chan))
1122b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
1123b02038faSJohn Youn 	}
1124b02038faSJohn Youn 
1125b02038faSJohn Youn 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1126b02038faSJohn Youn 	chan->halt_status = halt_status;
1127b02038faSJohn Youn 
1128b02038faSJohn Youn 	if (hcchar & HCCHAR_CHENA) {
1129b02038faSJohn Youn 		if (dbg_hc(chan))
1130b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel enabled\n");
1131b02038faSJohn Youn 		chan->halt_pending = 1;
1132b02038faSJohn Youn 		chan->halt_on_queue = 0;
1133b02038faSJohn Youn 	} else {
1134b02038faSJohn Youn 		if (dbg_hc(chan))
1135b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel disabled\n");
1136b02038faSJohn Youn 		chan->halt_on_queue = 1;
1137b02038faSJohn Youn 	}
1138b02038faSJohn Youn 
1139b02038faSJohn Youn 	if (dbg_hc(chan)) {
1140b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1141b02038faSJohn Youn 			 chan->hc_num);
1142b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 hcchar: 0x%08x\n",
1143b02038faSJohn Youn 			 hcchar);
1144b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_pending: %d\n",
1145b02038faSJohn Youn 			 chan->halt_pending);
1146b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_on_queue: %d\n",
1147b02038faSJohn Youn 			 chan->halt_on_queue);
1148b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_status: %d\n",
1149b02038faSJohn Youn 			 chan->halt_status);
1150b02038faSJohn Youn 	}
1151b02038faSJohn Youn }
1152b02038faSJohn Youn 
1153b02038faSJohn Youn /**
1154b02038faSJohn Youn  * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1155b02038faSJohn Youn  *
1156b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1157b02038faSJohn Youn  * @chan:  Identifies the host channel to clean up
1158b02038faSJohn Youn  *
1159b02038faSJohn Youn  * This function is normally called after a transfer is done and the host
1160b02038faSJohn Youn  * channel is being released
1161b02038faSJohn Youn  */
1162b02038faSJohn Youn void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1163b02038faSJohn Youn {
1164b02038faSJohn Youn 	u32 hcintmsk;
1165b02038faSJohn Youn 
1166b02038faSJohn Youn 	chan->xfer_started = 0;
1167b02038faSJohn Youn 
1168b02038faSJohn Youn 	list_del_init(&chan->split_order_list_entry);
1169b02038faSJohn Youn 
1170b02038faSJohn Youn 	/*
1171b02038faSJohn Youn 	 * Clear channel interrupt enables and any unhandled channel interrupt
1172b02038faSJohn Youn 	 * conditions
1173b02038faSJohn Youn 	 */
1174b02038faSJohn Youn 	dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1175b02038faSJohn Youn 	hcintmsk = 0xffffffff;
1176b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
1177b02038faSJohn Youn 	dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1178b02038faSJohn Youn }
1179b02038faSJohn Youn 
1180b02038faSJohn Youn /**
1181b02038faSJohn Youn  * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1182b02038faSJohn Youn  * which frame a periodic transfer should occur
1183b02038faSJohn Youn  *
1184b02038faSJohn Youn  * @hsotg:  Programming view of DWC_otg controller
1185b02038faSJohn Youn  * @chan:   Identifies the host channel to set up and its properties
1186b02038faSJohn Youn  * @hcchar: Current value of the HCCHAR register for the specified host channel
1187b02038faSJohn Youn  *
1188b02038faSJohn Youn  * This function has no effect on non-periodic transfers
1189b02038faSJohn Youn  */
1190b02038faSJohn Youn static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1191b02038faSJohn Youn 				       struct dwc2_host_chan *chan, u32 *hcchar)
1192b02038faSJohn Youn {
1193b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1194b02038faSJohn Youn 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1195b02038faSJohn Youn 		int host_speed;
1196b02038faSJohn Youn 		int xfer_ns;
1197b02038faSJohn Youn 		int xfer_us;
1198b02038faSJohn Youn 		int bytes_in_fifo;
1199b02038faSJohn Youn 		u16 fifo_space;
1200b02038faSJohn Youn 		u16 frame_number;
1201b02038faSJohn Youn 		u16 wire_frame;
1202b02038faSJohn Youn 
1203b02038faSJohn Youn 		/*
1204b02038faSJohn Youn 		 * Try to figure out if we're an even or odd frame. If we set
1205b02038faSJohn Youn 		 * even and the current frame number is even the the transfer
1206b02038faSJohn Youn 		 * will happen immediately.  Similar if both are odd. If one is
1207b02038faSJohn Youn 		 * even and the other is odd then the transfer will happen when
1208b02038faSJohn Youn 		 * the frame number ticks.
1209b02038faSJohn Youn 		 *
1210b02038faSJohn Youn 		 * There's a bit of a balancing act to get this right.
1211b02038faSJohn Youn 		 * Sometimes we may want to send data in the current frame (AK
1212b02038faSJohn Youn 		 * right away).  We might want to do this if the frame number
1213b02038faSJohn Youn 		 * _just_ ticked, but we might also want to do this in order
1214b02038faSJohn Youn 		 * to continue a split transaction that happened late in a
1215b02038faSJohn Youn 		 * microframe (so we didn't know to queue the next transfer
1216b02038faSJohn Youn 		 * until the frame number had ticked).  The problem is that we
1217b02038faSJohn Youn 		 * need a lot of knowledge to know if there's actually still
1218b02038faSJohn Youn 		 * time to send things or if it would be better to wait until
1219b02038faSJohn Youn 		 * the next frame.
1220b02038faSJohn Youn 		 *
1221b02038faSJohn Youn 		 * We can look at how much time is left in the current frame
1222b02038faSJohn Youn 		 * and make a guess about whether we'll have time to transfer.
1223b02038faSJohn Youn 		 * We'll do that.
1224b02038faSJohn Youn 		 */
1225b02038faSJohn Youn 
1226b02038faSJohn Youn 		/* Get speed host is running at */
1227b02038faSJohn Youn 		host_speed = (chan->speed != USB_SPEED_HIGH &&
1228b02038faSJohn Youn 			      !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1229b02038faSJohn Youn 
1230b02038faSJohn Youn 		/* See how many bytes are in the periodic FIFO right now */
1231b02038faSJohn Youn 		fifo_space = (dwc2_readl(hsotg->regs + HPTXSTS) &
1232b02038faSJohn Youn 			      TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1233b02038faSJohn Youn 		bytes_in_fifo = sizeof(u32) *
1234bea8e86cSJohn Youn 				(hsotg->params.host_perio_tx_fifo_size -
1235b02038faSJohn Youn 				 fifo_space);
1236b02038faSJohn Youn 
1237b02038faSJohn Youn 		/*
1238b02038faSJohn Youn 		 * Roughly estimate bus time for everything in the periodic
1239b02038faSJohn Youn 		 * queue + our new transfer.  This is "rough" because we're
1240b02038faSJohn Youn 		 * using a function that makes takes into account IN/OUT
1241b02038faSJohn Youn 		 * and INT/ISO and we're just slamming in one value for all
1242b02038faSJohn Youn 		 * transfers.  This should be an over-estimate and that should
1243b02038faSJohn Youn 		 * be OK, but we can probably tighten it.
1244b02038faSJohn Youn 		 */
1245b02038faSJohn Youn 		xfer_ns = usb_calc_bus_time(host_speed, false, false,
1246b02038faSJohn Youn 					    chan->xfer_len + bytes_in_fifo);
1247b02038faSJohn Youn 		xfer_us = NS_TO_US(xfer_ns);
1248b02038faSJohn Youn 
1249b02038faSJohn Youn 		/* See what frame number we'll be at by the time we finish */
1250b02038faSJohn Youn 		frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1251b02038faSJohn Youn 
1252b02038faSJohn Youn 		/* This is when we were scheduled to be on the wire */
1253b02038faSJohn Youn 		wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1254b02038faSJohn Youn 
1255b02038faSJohn Youn 		/*
1256b02038faSJohn Youn 		 * If we'd finish _after_ the frame we're scheduled in then
1257b02038faSJohn Youn 		 * it's hopeless.  Just schedule right away and hope for the
1258b02038faSJohn Youn 		 * best.  Note that it _might_ be wise to call back into the
1259b02038faSJohn Youn 		 * scheduler to pick a better frame, but this is better than
1260b02038faSJohn Youn 		 * nothing.
1261b02038faSJohn Youn 		 */
1262b02038faSJohn Youn 		if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1263b02038faSJohn Youn 			dwc2_sch_vdbg(hsotg,
1264b02038faSJohn Youn 				      "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1265b02038faSJohn Youn 				      chan->qh, wire_frame, frame_number,
1266b02038faSJohn Youn 				      dwc2_frame_num_dec(frame_number,
1267b02038faSJohn Youn 							 wire_frame));
1268b02038faSJohn Youn 			wire_frame = frame_number;
1269b02038faSJohn Youn 
1270b02038faSJohn Youn 			/*
1271b02038faSJohn Youn 			 * We picked a different frame number; communicate this
1272b02038faSJohn Youn 			 * back to the scheduler so it doesn't try to schedule
1273b02038faSJohn Youn 			 * another in the same frame.
1274b02038faSJohn Youn 			 *
1275b02038faSJohn Youn 			 * Remember that next_active_frame is 1 before the wire
1276b02038faSJohn Youn 			 * frame.
1277b02038faSJohn Youn 			 */
1278b02038faSJohn Youn 			chan->qh->next_active_frame =
1279b02038faSJohn Youn 				dwc2_frame_num_dec(frame_number, 1);
1280b02038faSJohn Youn 		}
1281b02038faSJohn Youn 
1282b02038faSJohn Youn 		if (wire_frame & 1)
1283b02038faSJohn Youn 			*hcchar |= HCCHAR_ODDFRM;
1284b02038faSJohn Youn 		else
1285b02038faSJohn Youn 			*hcchar &= ~HCCHAR_ODDFRM;
1286b02038faSJohn Youn 	}
1287b02038faSJohn Youn }
1288b02038faSJohn Youn 
1289b02038faSJohn Youn static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1290b02038faSJohn Youn {
1291b02038faSJohn Youn 	/* Set up the initial PID for the transfer */
1292b02038faSJohn Youn 	if (chan->speed == USB_SPEED_HIGH) {
1293b02038faSJohn Youn 		if (chan->ep_is_in) {
1294b02038faSJohn Youn 			if (chan->multi_count == 1)
1295b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1296b02038faSJohn Youn 			else if (chan->multi_count == 2)
1297b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA1;
1298b02038faSJohn Youn 			else
1299b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA2;
1300b02038faSJohn Youn 		} else {
1301b02038faSJohn Youn 			if (chan->multi_count == 1)
1302b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1303b02038faSJohn Youn 			else
1304b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_MDATA;
1305b02038faSJohn Youn 		}
1306b02038faSJohn Youn 	} else {
1307b02038faSJohn Youn 		chan->data_pid_start = DWC2_HC_PID_DATA0;
1308b02038faSJohn Youn 	}
1309b02038faSJohn Youn }
1310b02038faSJohn Youn 
1311b02038faSJohn Youn /**
1312b02038faSJohn Youn  * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1313b02038faSJohn Youn  * the Host Channel
1314b02038faSJohn Youn  *
1315b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1316b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1317b02038faSJohn Youn  *
1318b02038faSJohn Youn  * This function should only be called in Slave mode. For a channel associated
1319b02038faSJohn Youn  * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1320b02038faSJohn Youn  * associated with a periodic EP, the periodic Tx FIFO is written.
1321b02038faSJohn Youn  *
1322b02038faSJohn Youn  * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1323b02038faSJohn Youn  * the number of bytes written to the Tx FIFO.
1324b02038faSJohn Youn  */
1325b02038faSJohn Youn static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1326b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1327b02038faSJohn Youn {
1328b02038faSJohn Youn 	u32 i;
1329b02038faSJohn Youn 	u32 remaining_count;
1330b02038faSJohn Youn 	u32 byte_count;
1331b02038faSJohn Youn 	u32 dword_count;
1332b02038faSJohn Youn 	u32 __iomem *data_fifo;
1333b02038faSJohn Youn 	u32 *data_buf = (u32 *)chan->xfer_buf;
1334b02038faSJohn Youn 
1335b02038faSJohn Youn 	if (dbg_hc(chan))
1336b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1337b02038faSJohn Youn 
1338b02038faSJohn Youn 	data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1339b02038faSJohn Youn 
1340b02038faSJohn Youn 	remaining_count = chan->xfer_len - chan->xfer_count;
1341b02038faSJohn Youn 	if (remaining_count > chan->max_packet)
1342b02038faSJohn Youn 		byte_count = chan->max_packet;
1343b02038faSJohn Youn 	else
1344b02038faSJohn Youn 		byte_count = remaining_count;
1345b02038faSJohn Youn 
1346b02038faSJohn Youn 	dword_count = (byte_count + 3) / 4;
1347b02038faSJohn Youn 
1348b02038faSJohn Youn 	if (((unsigned long)data_buf & 0x3) == 0) {
1349b02038faSJohn Youn 		/* xfer_buf is DWORD aligned */
1350b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++)
1351b02038faSJohn Youn 			dwc2_writel(*data_buf, data_fifo);
1352b02038faSJohn Youn 	} else {
1353b02038faSJohn Youn 		/* xfer_buf is not DWORD aligned */
1354b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++) {
1355b02038faSJohn Youn 			u32 data = data_buf[0] | data_buf[1] << 8 |
1356b02038faSJohn Youn 				   data_buf[2] << 16 | data_buf[3] << 24;
1357b02038faSJohn Youn 			dwc2_writel(data, data_fifo);
1358b02038faSJohn Youn 		}
1359b02038faSJohn Youn 	}
1360b02038faSJohn Youn 
1361b02038faSJohn Youn 	chan->xfer_count += byte_count;
1362b02038faSJohn Youn 	chan->xfer_buf += byte_count;
1363b02038faSJohn Youn }
1364b02038faSJohn Youn 
1365b02038faSJohn Youn /**
1366b02038faSJohn Youn  * dwc2_hc_do_ping() - Starts a PING transfer
1367b02038faSJohn Youn  *
1368b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1369b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1370b02038faSJohn Youn  *
1371b02038faSJohn Youn  * This function should only be called in Slave mode. The Do Ping bit is set in
1372b02038faSJohn Youn  * the HCTSIZ register, then the channel is enabled.
1373b02038faSJohn Youn  */
1374b02038faSJohn Youn static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1375b02038faSJohn Youn 			    struct dwc2_host_chan *chan)
1376b02038faSJohn Youn {
1377b02038faSJohn Youn 	u32 hcchar;
1378b02038faSJohn Youn 	u32 hctsiz;
1379b02038faSJohn Youn 
1380b02038faSJohn Youn 	if (dbg_hc(chan))
1381b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1382b02038faSJohn Youn 			 chan->hc_num);
1383b02038faSJohn Youn 
1384b02038faSJohn Youn 	hctsiz = TSIZ_DOPNG;
1385b02038faSJohn Youn 	hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1386b02038faSJohn Youn 	dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1387b02038faSJohn Youn 
1388b02038faSJohn Youn 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1389b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1390b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1391b02038faSJohn Youn 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1392b02038faSJohn Youn }
1393b02038faSJohn Youn 
1394b02038faSJohn Youn /**
1395b02038faSJohn Youn  * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1396b02038faSJohn Youn  * channel and starts the transfer
1397b02038faSJohn Youn  *
1398b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1399b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel. The xfer_len value
1400b02038faSJohn Youn  *         may be reduced to accommodate the max widths of the XferSize and
1401b02038faSJohn Youn  *         PktCnt fields in the HCTSIZn register. The multi_count value may be
1402b02038faSJohn Youn  *         changed to reflect the final xfer_len value.
1403b02038faSJohn Youn  *
1404b02038faSJohn Youn  * This function may be called in either Slave mode or DMA mode. In Slave mode,
1405b02038faSJohn Youn  * the caller must ensure that there is sufficient space in the request queue
1406b02038faSJohn Youn  * and Tx Data FIFO.
1407b02038faSJohn Youn  *
1408b02038faSJohn Youn  * For an OUT transfer in Slave mode, it loads a data packet into the
1409b02038faSJohn Youn  * appropriate FIFO. If necessary, additional data packets are loaded in the
1410b02038faSJohn Youn  * Host ISR.
1411b02038faSJohn Youn  *
1412b02038faSJohn Youn  * For an IN transfer in Slave mode, a data packet is requested. The data
1413b02038faSJohn Youn  * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1414b02038faSJohn Youn  * additional data packets are requested in the Host ISR.
1415b02038faSJohn Youn  *
1416b02038faSJohn Youn  * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1417b02038faSJohn Youn  * register along with a packet count of 1 and the channel is enabled. This
1418b02038faSJohn Youn  * causes a single PING transaction to occur. Other fields in HCTSIZ are
1419b02038faSJohn Youn  * simply set to 0 since no data transfer occurs in this case.
1420b02038faSJohn Youn  *
1421b02038faSJohn Youn  * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1422b02038faSJohn Youn  * all the information required to perform the subsequent data transfer. In
1423b02038faSJohn Youn  * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1424b02038faSJohn Youn  * controller performs the entire PING protocol, then starts the data
1425b02038faSJohn Youn  * transfer.
1426b02038faSJohn Youn  */
1427b02038faSJohn Youn static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1428b02038faSJohn Youn 				   struct dwc2_host_chan *chan)
1429b02038faSJohn Youn {
1430bea8e86cSJohn Youn 	u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1431bea8e86cSJohn Youn 	u16 max_hc_pkt_count = hsotg->params.max_packet_count;
1432b02038faSJohn Youn 	u32 hcchar;
1433b02038faSJohn Youn 	u32 hctsiz = 0;
1434b02038faSJohn Youn 	u16 num_packets;
1435b02038faSJohn Youn 	u32 ec_mc;
1436b02038faSJohn Youn 
1437b02038faSJohn Youn 	if (dbg_hc(chan))
1438b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1439b02038faSJohn Youn 
1440b02038faSJohn Youn 	if (chan->do_ping) {
144195832c00SJohn Youn 		if (!hsotg->params.host_dma) {
1442b02038faSJohn Youn 			if (dbg_hc(chan))
1443b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "ping, no DMA\n");
1444b02038faSJohn Youn 			dwc2_hc_do_ping(hsotg, chan);
1445b02038faSJohn Youn 			chan->xfer_started = 1;
1446b02038faSJohn Youn 			return;
1447b02038faSJohn Youn 		}
1448b02038faSJohn Youn 
1449b02038faSJohn Youn 		if (dbg_hc(chan))
1450b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "ping, DMA\n");
1451b02038faSJohn Youn 
1452b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1453b02038faSJohn Youn 	}
1454b02038faSJohn Youn 
1455b02038faSJohn Youn 	if (chan->do_split) {
1456b02038faSJohn Youn 		if (dbg_hc(chan))
1457b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "split\n");
1458b02038faSJohn Youn 		num_packets = 1;
1459b02038faSJohn Youn 
1460b02038faSJohn Youn 		if (chan->complete_split && !chan->ep_is_in)
1461b02038faSJohn Youn 			/*
1462b02038faSJohn Youn 			 * For CSPLIT OUT Transfer, set the size to 0 so the
1463b02038faSJohn Youn 			 * core doesn't expect any data written to the FIFO
1464b02038faSJohn Youn 			 */
1465b02038faSJohn Youn 			chan->xfer_len = 0;
1466b02038faSJohn Youn 		else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1467b02038faSJohn Youn 			chan->xfer_len = chan->max_packet;
1468b02038faSJohn Youn 		else if (!chan->ep_is_in && chan->xfer_len > 188)
1469b02038faSJohn Youn 			chan->xfer_len = 188;
1470b02038faSJohn Youn 
1471b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1472b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1473b02038faSJohn Youn 
1474b02038faSJohn Youn 		/* For split set ec_mc for immediate retries */
1475b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1476b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1477b02038faSJohn Youn 			ec_mc = 3;
1478b02038faSJohn Youn 		else
1479b02038faSJohn Youn 			ec_mc = 1;
1480b02038faSJohn Youn 	} else {
1481b02038faSJohn Youn 		if (dbg_hc(chan))
1482b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "no split\n");
1483b02038faSJohn Youn 		/*
1484b02038faSJohn Youn 		 * Ensure that the transfer length and packet count will fit
1485b02038faSJohn Youn 		 * in the widths allocated for them in the HCTSIZn register
1486b02038faSJohn Youn 		 */
1487b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1488b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1489b02038faSJohn Youn 			/*
1490b02038faSJohn Youn 			 * Make sure the transfer size is no larger than one
1491b02038faSJohn Youn 			 * (micro)frame's worth of data. (A check was done
1492b02038faSJohn Youn 			 * when the periodic transfer was accepted to ensure
1493b02038faSJohn Youn 			 * that a (micro)frame's worth of data can be
1494b02038faSJohn Youn 			 * programmed into a channel.)
1495b02038faSJohn Youn 			 */
1496b02038faSJohn Youn 			u32 max_periodic_len =
1497b02038faSJohn Youn 				chan->multi_count * chan->max_packet;
1498b02038faSJohn Youn 
1499b02038faSJohn Youn 			if (chan->xfer_len > max_periodic_len)
1500b02038faSJohn Youn 				chan->xfer_len = max_periodic_len;
1501b02038faSJohn Youn 		} else if (chan->xfer_len > max_hc_xfer_size) {
1502b02038faSJohn Youn 			/*
1503b02038faSJohn Youn 			 * Make sure that xfer_len is a multiple of max packet
1504b02038faSJohn Youn 			 * size
1505b02038faSJohn Youn 			 */
1506b02038faSJohn Youn 			chan->xfer_len =
1507b02038faSJohn Youn 				max_hc_xfer_size - chan->max_packet + 1;
1508b02038faSJohn Youn 		}
1509b02038faSJohn Youn 
1510b02038faSJohn Youn 		if (chan->xfer_len > 0) {
1511b02038faSJohn Youn 			num_packets = (chan->xfer_len + chan->max_packet - 1) /
1512b02038faSJohn Youn 					chan->max_packet;
1513b02038faSJohn Youn 			if (num_packets > max_hc_pkt_count) {
1514b02038faSJohn Youn 				num_packets = max_hc_pkt_count;
1515b02038faSJohn Youn 				chan->xfer_len = num_packets * chan->max_packet;
1516b02038faSJohn Youn 			}
1517b02038faSJohn Youn 		} else {
1518b02038faSJohn Youn 			/* Need 1 packet for transfer length of 0 */
1519b02038faSJohn Youn 			num_packets = 1;
1520b02038faSJohn Youn 		}
1521b02038faSJohn Youn 
1522b02038faSJohn Youn 		if (chan->ep_is_in)
1523b02038faSJohn Youn 			/*
1524b02038faSJohn Youn 			 * Always program an integral # of max packets for IN
1525b02038faSJohn Youn 			 * transfers
1526b02038faSJohn Youn 			 */
1527b02038faSJohn Youn 			chan->xfer_len = num_packets * chan->max_packet;
1528b02038faSJohn Youn 
1529b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1530b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1531b02038faSJohn Youn 			/*
1532b02038faSJohn Youn 			 * Make sure that the multi_count field matches the
1533b02038faSJohn Youn 			 * actual transfer length
1534b02038faSJohn Youn 			 */
1535b02038faSJohn Youn 			chan->multi_count = num_packets;
1536b02038faSJohn Youn 
1537b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1538b02038faSJohn Youn 			dwc2_set_pid_isoc(chan);
1539b02038faSJohn Youn 
1540b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1541b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1542b02038faSJohn Youn 
1543b02038faSJohn Youn 		/* The ec_mc gets the multi_count for non-split */
1544b02038faSJohn Youn 		ec_mc = chan->multi_count;
1545b02038faSJohn Youn 	}
1546b02038faSJohn Youn 
1547b02038faSJohn Youn 	chan->start_pkt_count = num_packets;
1548b02038faSJohn Youn 	hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1549b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1550b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1551b02038faSJohn Youn 	dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1552b02038faSJohn Youn 	if (dbg_hc(chan)) {
1553b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1554b02038faSJohn Youn 			 hctsiz, chan->hc_num);
1555b02038faSJohn Youn 
1556b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1557b02038faSJohn Youn 			 chan->hc_num);
1558b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Xfer Size: %d\n",
1559b02038faSJohn Youn 			 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1560b02038faSJohn Youn 			 TSIZ_XFERSIZE_SHIFT);
1561b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Num Pkts: %d\n",
1562b02038faSJohn Youn 			 (hctsiz & TSIZ_PKTCNT_MASK) >>
1563b02038faSJohn Youn 			 TSIZ_PKTCNT_SHIFT);
1564b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1565b02038faSJohn Youn 			 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1566b02038faSJohn Youn 			 TSIZ_SC_MC_PID_SHIFT);
1567b02038faSJohn Youn 	}
1568b02038faSJohn Youn 
156995832c00SJohn Youn 	if (hsotg->params.host_dma) {
1570af424a41SWilliam Wu 		dma_addr_t dma_addr;
1571af424a41SWilliam Wu 
1572af424a41SWilliam Wu 		if (chan->align_buf) {
1573af424a41SWilliam Wu 			if (dbg_hc(chan))
1574af424a41SWilliam Wu 				dev_vdbg(hsotg->dev, "align_buf\n");
1575af424a41SWilliam Wu 			dma_addr = chan->align_buf;
1576af424a41SWilliam Wu 		} else {
1577af424a41SWilliam Wu 			dma_addr = chan->xfer_dma;
1578af424a41SWilliam Wu 		}
1579af424a41SWilliam Wu 		dwc2_writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
1580af424a41SWilliam Wu 
1581b02038faSJohn Youn 		if (dbg_hc(chan))
1582b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1583af424a41SWilliam Wu 				 (unsigned long)dma_addr, chan->hc_num);
1584b02038faSJohn Youn 	}
1585b02038faSJohn Youn 
1586b02038faSJohn Youn 	/* Start the split */
1587b02038faSJohn Youn 	if (chan->do_split) {
1588b02038faSJohn Youn 		u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1589b02038faSJohn Youn 
1590b02038faSJohn Youn 		hcsplt |= HCSPLT_SPLTENA;
1591b02038faSJohn Youn 		dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1592b02038faSJohn Youn 	}
1593b02038faSJohn Youn 
1594b02038faSJohn Youn 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1595b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1596b02038faSJohn Youn 	hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1597b02038faSJohn Youn 	dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1598b02038faSJohn Youn 
1599b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1600b02038faSJohn Youn 		dev_warn(hsotg->dev,
1601b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1602b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1603b02038faSJohn Youn 
1604b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1605b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1606b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1607b02038faSJohn Youn 
1608b02038faSJohn Youn 	if (dbg_hc(chan))
1609b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1610b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1611b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1612b02038faSJohn Youn 
1613b02038faSJohn Youn 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1614b02038faSJohn Youn 	if (dbg_hc(chan))
1615b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1616b02038faSJohn Youn 			 chan->hc_num);
1617b02038faSJohn Youn 
1618b02038faSJohn Youn 	chan->xfer_started = 1;
1619b02038faSJohn Youn 	chan->requests++;
1620b02038faSJohn Youn 
162195832c00SJohn Youn 	if (!hsotg->params.host_dma &&
1622b02038faSJohn Youn 	    !chan->ep_is_in && chan->xfer_len > 0)
1623b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1624b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1625b02038faSJohn Youn }
1626b02038faSJohn Youn 
1627b02038faSJohn Youn /**
1628b02038faSJohn Youn  * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1629b02038faSJohn Youn  * host channel and starts the transfer in Descriptor DMA mode
1630b02038faSJohn Youn  *
1631b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1632b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1633b02038faSJohn Youn  *
1634b02038faSJohn Youn  * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1635b02038faSJohn Youn  * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1636b02038faSJohn Youn  * with micro-frame bitmap.
1637b02038faSJohn Youn  *
1638b02038faSJohn Youn  * Initializes HCDMA register with descriptor list address and CTD value then
1639b02038faSJohn Youn  * starts the transfer via enabling the channel.
1640b02038faSJohn Youn  */
1641b02038faSJohn Youn void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1642b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1643b02038faSJohn Youn {
1644b02038faSJohn Youn 	u32 hcchar;
1645b02038faSJohn Youn 	u32 hctsiz = 0;
1646b02038faSJohn Youn 
1647b02038faSJohn Youn 	if (chan->do_ping)
1648b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1649b02038faSJohn Youn 
1650b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1651b02038faSJohn Youn 		dwc2_set_pid_isoc(chan);
1652b02038faSJohn Youn 
1653b02038faSJohn Youn 	/* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1654b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1655b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1656b02038faSJohn Youn 
1657b02038faSJohn Youn 	/* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1658b02038faSJohn Youn 	hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1659b02038faSJohn Youn 
1660b02038faSJohn Youn 	/* Non-zero only for high-speed interrupt endpoints */
1661b02038faSJohn Youn 	hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1662b02038faSJohn Youn 
1663b02038faSJohn Youn 	if (dbg_hc(chan)) {
1664b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1665b02038faSJohn Youn 			 chan->hc_num);
1666b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1667b02038faSJohn Youn 			 chan->data_pid_start);
1668b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 NTD: %d\n", chan->ntd - 1);
1669b02038faSJohn Youn 	}
1670b02038faSJohn Youn 
1671b02038faSJohn Youn 	dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1672b02038faSJohn Youn 
1673b02038faSJohn Youn 	dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1674b02038faSJohn Youn 				   chan->desc_list_sz, DMA_TO_DEVICE);
1675b02038faSJohn Youn 
1676b02038faSJohn Youn 	dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1677b02038faSJohn Youn 
1678b02038faSJohn Youn 	if (dbg_hc(chan))
1679b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1680b02038faSJohn Youn 			 &chan->desc_list_addr, chan->hc_num);
1681b02038faSJohn Youn 
1682b02038faSJohn Youn 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1683b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1684b02038faSJohn Youn 	hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1685b02038faSJohn Youn 		  HCCHAR_MULTICNT_MASK;
1686b02038faSJohn Youn 
1687b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1688b02038faSJohn Youn 		dev_warn(hsotg->dev,
1689b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1690b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1691b02038faSJohn Youn 
1692b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1693b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1694b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1695b02038faSJohn Youn 
1696b02038faSJohn Youn 	if (dbg_hc(chan))
1697b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1698b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1699b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1700b02038faSJohn Youn 
1701b02038faSJohn Youn 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1702b02038faSJohn Youn 	if (dbg_hc(chan))
1703b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1704b02038faSJohn Youn 			 chan->hc_num);
1705b02038faSJohn Youn 
1706b02038faSJohn Youn 	chan->xfer_started = 1;
1707b02038faSJohn Youn 	chan->requests++;
1708b02038faSJohn Youn }
1709b02038faSJohn Youn 
1710b02038faSJohn Youn /**
1711b02038faSJohn Youn  * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1712b02038faSJohn Youn  * a previous call to dwc2_hc_start_transfer()
1713b02038faSJohn Youn  *
1714b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1715b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1716b02038faSJohn Youn  *
1717b02038faSJohn Youn  * The caller must ensure there is sufficient space in the request queue and Tx
1718b02038faSJohn Youn  * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1719b02038faSJohn Youn  * the controller acts autonomously to complete transfers programmed to a host
1720b02038faSJohn Youn  * channel.
1721b02038faSJohn Youn  *
1722b02038faSJohn Youn  * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1723b02038faSJohn Youn  * if there is any data remaining to be queued. For an IN transfer, another
1724b02038faSJohn Youn  * data packet is always requested. For the SETUP phase of a control transfer,
1725b02038faSJohn Youn  * this function does nothing.
1726b02038faSJohn Youn  *
1727b02038faSJohn Youn  * Return: 1 if a new request is queued, 0 if no more requests are required
1728b02038faSJohn Youn  * for this transfer
1729b02038faSJohn Youn  */
1730b02038faSJohn Youn static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1731b02038faSJohn Youn 				     struct dwc2_host_chan *chan)
1732b02038faSJohn Youn {
1733b02038faSJohn Youn 	if (dbg_hc(chan))
1734b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1735b02038faSJohn Youn 			 chan->hc_num);
1736b02038faSJohn Youn 
1737b02038faSJohn Youn 	if (chan->do_split)
1738b02038faSJohn Youn 		/* SPLITs always queue just once per channel */
1739b02038faSJohn Youn 		return 0;
1740b02038faSJohn Youn 
1741b02038faSJohn Youn 	if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1742b02038faSJohn Youn 		/* SETUPs are queued only once since they can't be NAK'd */
1743b02038faSJohn Youn 		return 0;
1744b02038faSJohn Youn 
1745b02038faSJohn Youn 	if (chan->ep_is_in) {
1746b02038faSJohn Youn 		/*
1747b02038faSJohn Youn 		 * Always queue another request for other IN transfers. If
1748b02038faSJohn Youn 		 * back-to-back INs are issued and NAKs are received for both,
1749b02038faSJohn Youn 		 * the driver may still be processing the first NAK when the
1750b02038faSJohn Youn 		 * second NAK is received. When the interrupt handler clears
1751b02038faSJohn Youn 		 * the NAK interrupt for the first NAK, the second NAK will
1752b02038faSJohn Youn 		 * not be seen. So we can't depend on the NAK interrupt
1753b02038faSJohn Youn 		 * handler to requeue a NAK'd request. Instead, IN requests
1754b02038faSJohn Youn 		 * are issued each time this function is called. When the
1755b02038faSJohn Youn 		 * transfer completes, the extra requests for the channel will
1756b02038faSJohn Youn 		 * be flushed.
1757b02038faSJohn Youn 		 */
1758b02038faSJohn Youn 		u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1759b02038faSJohn Youn 
1760b02038faSJohn Youn 		dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1761b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
1762b02038faSJohn Youn 		hcchar &= ~HCCHAR_CHDIS;
1763b02038faSJohn Youn 		if (dbg_hc(chan))
1764b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	 IN xfer: hcchar = 0x%08x\n",
1765b02038faSJohn Youn 				 hcchar);
1766b02038faSJohn Youn 		dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1767b02038faSJohn Youn 		chan->requests++;
1768b02038faSJohn Youn 		return 1;
1769b02038faSJohn Youn 	}
1770b02038faSJohn Youn 
1771b02038faSJohn Youn 	/* OUT transfers */
1772b02038faSJohn Youn 
1773b02038faSJohn Youn 	if (chan->xfer_count < chan->xfer_len) {
1774b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1775b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1776b02038faSJohn Youn 			u32 hcchar = dwc2_readl(hsotg->regs +
1777b02038faSJohn Youn 						HCCHAR(chan->hc_num));
1778b02038faSJohn Youn 
1779b02038faSJohn Youn 			dwc2_hc_set_even_odd_frame(hsotg, chan,
1780b02038faSJohn Youn 						   &hcchar);
1781b02038faSJohn Youn 		}
1782b02038faSJohn Youn 
1783b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1784b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1785b02038faSJohn Youn 		chan->requests++;
1786b02038faSJohn Youn 		return 1;
1787b02038faSJohn Youn 	}
1788b02038faSJohn Youn 
1789b02038faSJohn Youn 	return 0;
1790b02038faSJohn Youn }
1791b02038faSJohn Youn 
1792b02038faSJohn Youn /*
1793b02038faSJohn Youn  * =========================================================================
1794b02038faSJohn Youn  *  HCD
1795b02038faSJohn Youn  * =========================================================================
1796b02038faSJohn Youn  */
1797b02038faSJohn Youn 
1798b02038faSJohn Youn /*
1799197ba5f4SPaul Zimmerman  * Processes all the URBs in a single list of QHs. Completes them with
1800197ba5f4SPaul Zimmerman  * -ETIMEDOUT and frees the QTD.
1801197ba5f4SPaul Zimmerman  *
1802197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1803197ba5f4SPaul Zimmerman  */
1804197ba5f4SPaul Zimmerman static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1805197ba5f4SPaul Zimmerman 				      struct list_head *qh_list)
1806197ba5f4SPaul Zimmerman {
1807197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1808197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1809197ba5f4SPaul Zimmerman 
1810197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1811197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1812197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
18132e84da6eSGregory Herrero 			dwc2_host_complete(hsotg, qtd, -ECONNRESET);
1814197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1815197ba5f4SPaul Zimmerman 		}
1816197ba5f4SPaul Zimmerman 	}
1817197ba5f4SPaul Zimmerman }
1818197ba5f4SPaul Zimmerman 
1819197ba5f4SPaul Zimmerman static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1820197ba5f4SPaul Zimmerman 			      struct list_head *qh_list)
1821197ba5f4SPaul Zimmerman {
1822197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1823197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1824197ba5f4SPaul Zimmerman 	unsigned long flags;
1825197ba5f4SPaul Zimmerman 
1826197ba5f4SPaul Zimmerman 	if (!qh_list->next)
1827197ba5f4SPaul Zimmerman 		/* The list hasn't been initialized yet */
1828197ba5f4SPaul Zimmerman 		return;
1829197ba5f4SPaul Zimmerman 
1830197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
1831197ba5f4SPaul Zimmerman 
1832197ba5f4SPaul Zimmerman 	/* Ensure there are no QTDs or URBs left */
1833197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1834197ba5f4SPaul Zimmerman 
1835197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1836197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1837197ba5f4SPaul Zimmerman 
1838197ba5f4SPaul Zimmerman 		/* Free each QTD in the QH's QTD list */
1839197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1840197ba5f4SPaul Zimmerman 					 qtd_list_entry)
1841197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1842197ba5f4SPaul Zimmerman 
184316e80218SDouglas Anderson 		if (qh->channel && qh->channel->qh == qh)
184416e80218SDouglas Anderson 			qh->channel->qh = NULL;
184516e80218SDouglas Anderson 
1846197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
1847197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_free(hsotg, qh);
1848197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
1849197ba5f4SPaul Zimmerman 	}
1850197ba5f4SPaul Zimmerman 
1851197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
1852197ba5f4SPaul Zimmerman }
1853197ba5f4SPaul Zimmerman 
1854197ba5f4SPaul Zimmerman /*
1855197ba5f4SPaul Zimmerman  * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1856197ba5f4SPaul Zimmerman  * and periodic schedules. The QTD associated with each URB is removed from
1857197ba5f4SPaul Zimmerman  * the schedule and freed. This function may be called when a disconnect is
1858197ba5f4SPaul Zimmerman  * detected or when the HCD is being stopped.
1859197ba5f4SPaul Zimmerman  *
1860197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1861197ba5f4SPaul Zimmerman  */
1862197ba5f4SPaul Zimmerman static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1863197ba5f4SPaul Zimmerman {
1864197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
186538d2b5fbSDouglas Anderson 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
1866197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1867197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1868197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1869197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1870197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1871197ba5f4SPaul Zimmerman }
1872197ba5f4SPaul Zimmerman 
1873197ba5f4SPaul Zimmerman /**
1874197ba5f4SPaul Zimmerman  * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1875197ba5f4SPaul Zimmerman  *
1876197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1877197ba5f4SPaul Zimmerman  */
1878197ba5f4SPaul Zimmerman void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1879197ba5f4SPaul Zimmerman {
1880197ba5f4SPaul Zimmerman 	u32 hprt0;
1881197ba5f4SPaul Zimmerman 
1882197ba5f4SPaul Zimmerman 	if (hsotg->op_state == OTG_STATE_B_HOST) {
1883197ba5f4SPaul Zimmerman 		/*
1884197ba5f4SPaul Zimmerman 		 * Reset the port. During a HNP mode switch the reset
1885197ba5f4SPaul Zimmerman 		 * needs to occur within 1ms and have a duration of at
1886197ba5f4SPaul Zimmerman 		 * least 50ms.
1887197ba5f4SPaul Zimmerman 		 */
1888197ba5f4SPaul Zimmerman 		hprt0 = dwc2_read_hprt0(hsotg);
1889197ba5f4SPaul Zimmerman 		hprt0 |= HPRT0_RST;
189095c8bc36SAntti Seppälä 		dwc2_writel(hprt0, hsotg->regs + HPRT0);
1891197ba5f4SPaul Zimmerman 	}
1892197ba5f4SPaul Zimmerman 
1893197ba5f4SPaul Zimmerman 	queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1894197ba5f4SPaul Zimmerman 			   msecs_to_jiffies(50));
1895197ba5f4SPaul Zimmerman }
1896197ba5f4SPaul Zimmerman 
1897197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1898197ba5f4SPaul Zimmerman static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1899197ba5f4SPaul Zimmerman {
1900bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
1901197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
1902197ba5f4SPaul Zimmerman 	u32 hcchar;
1903197ba5f4SPaul Zimmerman 	int i;
1904197ba5f4SPaul Zimmerman 
190595832c00SJohn Youn 	if (!hsotg->params.host_dma) {
1906197ba5f4SPaul Zimmerman 		/* Flush out any channel requests in slave mode */
1907197ba5f4SPaul Zimmerman 		for (i = 0; i < num_channels; i++) {
1908197ba5f4SPaul Zimmerman 			channel = hsotg->hc_ptr_array[i];
1909197ba5f4SPaul Zimmerman 			if (!list_empty(&channel->hc_list_entry))
1910197ba5f4SPaul Zimmerman 				continue;
191195c8bc36SAntti Seppälä 			hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1912197ba5f4SPaul Zimmerman 			if (hcchar & HCCHAR_CHENA) {
1913197ba5f4SPaul Zimmerman 				hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1914197ba5f4SPaul Zimmerman 				hcchar |= HCCHAR_CHDIS;
191595c8bc36SAntti Seppälä 				dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1916197ba5f4SPaul Zimmerman 			}
1917197ba5f4SPaul Zimmerman 		}
1918197ba5f4SPaul Zimmerman 	}
1919197ba5f4SPaul Zimmerman 
1920197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
1921197ba5f4SPaul Zimmerman 		channel = hsotg->hc_ptr_array[i];
1922197ba5f4SPaul Zimmerman 		if (!list_empty(&channel->hc_list_entry))
1923197ba5f4SPaul Zimmerman 			continue;
192495c8bc36SAntti Seppälä 		hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1925197ba5f4SPaul Zimmerman 		if (hcchar & HCCHAR_CHENA) {
1926197ba5f4SPaul Zimmerman 			/* Halt the channel */
1927197ba5f4SPaul Zimmerman 			hcchar |= HCCHAR_CHDIS;
192895c8bc36SAntti Seppälä 			dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1929197ba5f4SPaul Zimmerman 		}
1930197ba5f4SPaul Zimmerman 
1931197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, channel);
1932197ba5f4SPaul Zimmerman 		list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1933197ba5f4SPaul Zimmerman 		/*
1934197ba5f4SPaul Zimmerman 		 * Added for Descriptor DMA to prevent channel double cleanup in
1935197ba5f4SPaul Zimmerman 		 * release_channel_ddma(), which is called from ep_disable when
1936197ba5f4SPaul Zimmerman 		 * device disconnects
1937197ba5f4SPaul Zimmerman 		 */
1938197ba5f4SPaul Zimmerman 		channel->qh = NULL;
1939197ba5f4SPaul Zimmerman 	}
19407252f1bfSVincent Palatin 	/* All channels have been freed, mark them available */
194195832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
19427252f1bfSVincent Palatin 		hsotg->available_host_channels =
1943bea8e86cSJohn Youn 			hsotg->params.host_channels;
19447252f1bfSVincent Palatin 	} else {
19457252f1bfSVincent Palatin 		hsotg->non_periodic_channels = 0;
19467252f1bfSVincent Palatin 		hsotg->periodic_channels = 0;
19477252f1bfSVincent Palatin 	}
1948197ba5f4SPaul Zimmerman }
1949197ba5f4SPaul Zimmerman 
1950197ba5f4SPaul Zimmerman /**
19516a659531SDouglas Anderson  * dwc2_hcd_connect() - Handles connect of the HCD
1952197ba5f4SPaul Zimmerman  *
1953197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1954197ba5f4SPaul Zimmerman  *
1955197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1956197ba5f4SPaul Zimmerman  */
19576a659531SDouglas Anderson void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
19586a659531SDouglas Anderson {
19596a659531SDouglas Anderson 	if (hsotg->lx_state != DWC2_L0)
19606a659531SDouglas Anderson 		usb_hcd_resume_root_hub(hsotg->priv);
19616a659531SDouglas Anderson 
19626a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status_change = 1;
19636a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status = 1;
19646a659531SDouglas Anderson }
19656a659531SDouglas Anderson 
19666a659531SDouglas Anderson /**
19676a659531SDouglas Anderson  * dwc2_hcd_disconnect() - Handles disconnect of the HCD
19686a659531SDouglas Anderson  *
19696a659531SDouglas Anderson  * @hsotg: Pointer to struct dwc2_hsotg
19706a659531SDouglas Anderson  * @force: If true, we won't try to reconnect even if we see device connected.
19716a659531SDouglas Anderson  *
19726a659531SDouglas Anderson  * Must be called with interrupt disabled and spinlock held
19736a659531SDouglas Anderson  */
19746a659531SDouglas Anderson void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
1975197ba5f4SPaul Zimmerman {
1976197ba5f4SPaul Zimmerman 	u32 intr;
19776a659531SDouglas Anderson 	u32 hprt0;
1978197ba5f4SPaul Zimmerman 
1979197ba5f4SPaul Zimmerman 	/* Set status flags for the hub driver */
1980197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status_change = 1;
1981197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status = 0;
1982197ba5f4SPaul Zimmerman 
1983197ba5f4SPaul Zimmerman 	/*
1984197ba5f4SPaul Zimmerman 	 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1985197ba5f4SPaul Zimmerman 	 * interrupt mask and status bits and disabling subsequent host
1986197ba5f4SPaul Zimmerman 	 * channel interrupts.
1987197ba5f4SPaul Zimmerman 	 */
198895c8bc36SAntti Seppälä 	intr = dwc2_readl(hsotg->regs + GINTMSK);
1989197ba5f4SPaul Zimmerman 	intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
199095c8bc36SAntti Seppälä 	dwc2_writel(intr, hsotg->regs + GINTMSK);
1991197ba5f4SPaul Zimmerman 	intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
199295c8bc36SAntti Seppälä 	dwc2_writel(intr, hsotg->regs + GINTSTS);
1993197ba5f4SPaul Zimmerman 
1994197ba5f4SPaul Zimmerman 	/*
1995197ba5f4SPaul Zimmerman 	 * Turn off the vbus power only if the core has transitioned to device
1996197ba5f4SPaul Zimmerman 	 * mode. If still in host mode, need to keep power on to detect a
1997197ba5f4SPaul Zimmerman 	 * reconnection.
1998197ba5f4SPaul Zimmerman 	 */
1999197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
2000197ba5f4SPaul Zimmerman 		if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
2001197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
200295c8bc36SAntti Seppälä 			dwc2_writel(0, hsotg->regs + HPRT0);
2003197ba5f4SPaul Zimmerman 		}
2004197ba5f4SPaul Zimmerman 
2005197ba5f4SPaul Zimmerman 		dwc2_disable_host_interrupts(hsotg);
2006197ba5f4SPaul Zimmerman 	}
2007197ba5f4SPaul Zimmerman 
2008197ba5f4SPaul Zimmerman 	/* Respond with an error status to all URBs in the schedule */
2009197ba5f4SPaul Zimmerman 	dwc2_kill_all_urbs(hsotg);
2010197ba5f4SPaul Zimmerman 
2011197ba5f4SPaul Zimmerman 	if (dwc2_is_host_mode(hsotg))
2012197ba5f4SPaul Zimmerman 		/* Clean up any host channels that were in use */
2013197ba5f4SPaul Zimmerman 		dwc2_hcd_cleanup_channels(hsotg);
2014197ba5f4SPaul Zimmerman 
2015197ba5f4SPaul Zimmerman 	dwc2_host_disconnect(hsotg);
20166a659531SDouglas Anderson 
20176a659531SDouglas Anderson 	/*
20186a659531SDouglas Anderson 	 * Add an extra check here to see if we're actually connected but
20196a659531SDouglas Anderson 	 * we don't have a detection interrupt pending.  This can happen if:
20206a659531SDouglas Anderson 	 *   1. hardware sees connect
20216a659531SDouglas Anderson 	 *   2. hardware sees disconnect
20226a659531SDouglas Anderson 	 *   3. hardware sees connect
20236a659531SDouglas Anderson 	 *   4. dwc2_port_intr() - clears connect interrupt
20246a659531SDouglas Anderson 	 *   5. dwc2_handle_common_intr() - calls here
20256a659531SDouglas Anderson 	 *
20266a659531SDouglas Anderson 	 * Without the extra check here we will end calling disconnect
20276a659531SDouglas Anderson 	 * and won't get any future interrupts to handle the connect.
20286a659531SDouglas Anderson 	 */
20296a659531SDouglas Anderson 	if (!force) {
20306a659531SDouglas Anderson 		hprt0 = dwc2_readl(hsotg->regs + HPRT0);
20316a659531SDouglas Anderson 		if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
20326a659531SDouglas Anderson 			dwc2_hcd_connect(hsotg);
20336a659531SDouglas Anderson 	}
2034197ba5f4SPaul Zimmerman }
2035197ba5f4SPaul Zimmerman 
2036197ba5f4SPaul Zimmerman /**
2037197ba5f4SPaul Zimmerman  * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
2038197ba5f4SPaul Zimmerman  *
2039197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
2040197ba5f4SPaul Zimmerman  */
2041197ba5f4SPaul Zimmerman static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
2042197ba5f4SPaul Zimmerman {
20431fb7f12dSDouglas Anderson 	if (hsotg->bus_suspended) {
2044197ba5f4SPaul Zimmerman 		hsotg->flags.b.port_suspend_change = 1;
2045b46146d5SGregory Herrero 		usb_hcd_resume_root_hub(hsotg->priv);
2046197ba5f4SPaul Zimmerman 	}
20471fb7f12dSDouglas Anderson 
20481fb7f12dSDouglas Anderson 	if (hsotg->lx_state == DWC2_L1)
20491fb7f12dSDouglas Anderson 		hsotg->flags.b.port_l1_change = 1;
2050b46146d5SGregory Herrero }
2051197ba5f4SPaul Zimmerman 
2052197ba5f4SPaul Zimmerman /**
2053197ba5f4SPaul Zimmerman  * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
2054197ba5f4SPaul Zimmerman  *
2055197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
2056197ba5f4SPaul Zimmerman  *
2057197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2058197ba5f4SPaul Zimmerman  */
2059197ba5f4SPaul Zimmerman void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
2060197ba5f4SPaul Zimmerman {
2061197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
2062197ba5f4SPaul Zimmerman 
2063197ba5f4SPaul Zimmerman 	/*
2064197ba5f4SPaul Zimmerman 	 * The root hub should be disconnected before this function is called.
2065197ba5f4SPaul Zimmerman 	 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
2066197ba5f4SPaul Zimmerman 	 * and the QH lists (via ..._hcd_endpoint_disable).
2067197ba5f4SPaul Zimmerman 	 */
2068197ba5f4SPaul Zimmerman 
2069197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
2070197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
2071197ba5f4SPaul Zimmerman 
2072197ba5f4SPaul Zimmerman 	/* Turn off the vbus power */
2073197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "PortPower off\n");
207495c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + HPRT0);
2075197ba5f4SPaul Zimmerman }
2076197ba5f4SPaul Zimmerman 
207733ad261aSGregory Herrero /* Caller must hold driver lock */
2078197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
2079b58e6ceeSMian Yousaf Kaukab 				struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
2080b5a468a6SMian Yousaf Kaukab 				struct dwc2_qtd *qtd)
2081197ba5f4SPaul Zimmerman {
2082197ba5f4SPaul Zimmerman 	u32 intr_mask;
2083197ba5f4SPaul Zimmerman 	int retval;
2084197ba5f4SPaul Zimmerman 	int dev_speed;
2085197ba5f4SPaul Zimmerman 
2086197ba5f4SPaul Zimmerman 	if (!hsotg->flags.b.port_connect_status) {
2087197ba5f4SPaul Zimmerman 		/* No longer connected */
2088197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Not connected\n");
2089197ba5f4SPaul Zimmerman 		return -ENODEV;
2090197ba5f4SPaul Zimmerman 	}
2091197ba5f4SPaul Zimmerman 
2092197ba5f4SPaul Zimmerman 	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
2093197ba5f4SPaul Zimmerman 
2094197ba5f4SPaul Zimmerman 	/* Some configurations cannot support LS traffic on a FS root port */
2095197ba5f4SPaul Zimmerman 	if ((dev_speed == USB_SPEED_LOW) &&
2096197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
2097197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
209895c8bc36SAntti Seppälä 		u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
2099197ba5f4SPaul Zimmerman 		u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
2100197ba5f4SPaul Zimmerman 
2101197ba5f4SPaul Zimmerman 		if (prtspd == HPRT0_SPD_FULL_SPEED)
2102197ba5f4SPaul Zimmerman 			return -ENODEV;
2103197ba5f4SPaul Zimmerman 	}
2104197ba5f4SPaul Zimmerman 
2105197ba5f4SPaul Zimmerman 	if (!qtd)
2106b5a468a6SMian Yousaf Kaukab 		return -EINVAL;
2107197ba5f4SPaul Zimmerman 
2108197ba5f4SPaul Zimmerman 	dwc2_hcd_qtd_init(qtd, urb);
2109b58e6ceeSMian Yousaf Kaukab 	retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
2110197ba5f4SPaul Zimmerman 	if (retval) {
2111197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
2112197ba5f4SPaul Zimmerman 			"DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
2113197ba5f4SPaul Zimmerman 			retval);
2114197ba5f4SPaul Zimmerman 		return retval;
2115197ba5f4SPaul Zimmerman 	}
2116197ba5f4SPaul Zimmerman 
211795c8bc36SAntti Seppälä 	intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
2118197ba5f4SPaul Zimmerman 	if (!(intr_mask & GINTSTS_SOF)) {
2119197ba5f4SPaul Zimmerman 		enum dwc2_transaction_type tr_type;
2120197ba5f4SPaul Zimmerman 
2121197ba5f4SPaul Zimmerman 		if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
2122197ba5f4SPaul Zimmerman 		    !(qtd->urb->flags & URB_GIVEBACK_ASAP))
2123197ba5f4SPaul Zimmerman 			/*
2124197ba5f4SPaul Zimmerman 			 * Do not schedule SG transactions until qtd has
2125197ba5f4SPaul Zimmerman 			 * URB_GIVEBACK_ASAP set
2126197ba5f4SPaul Zimmerman 			 */
2127197ba5f4SPaul Zimmerman 			return 0;
2128197ba5f4SPaul Zimmerman 
2129197ba5f4SPaul Zimmerman 		tr_type = dwc2_hcd_select_transactions(hsotg);
2130197ba5f4SPaul Zimmerman 		if (tr_type != DWC2_TRANSACTION_NONE)
2131197ba5f4SPaul Zimmerman 			dwc2_hcd_queue_transactions(hsotg, tr_type);
2132197ba5f4SPaul Zimmerman 	}
2133197ba5f4SPaul Zimmerman 
2134197ba5f4SPaul Zimmerman 	return 0;
2135197ba5f4SPaul Zimmerman }
2136197ba5f4SPaul Zimmerman 
2137197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
2138197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
2139197ba5f4SPaul Zimmerman 				struct dwc2_hcd_urb *urb)
2140197ba5f4SPaul Zimmerman {
2141197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2142197ba5f4SPaul Zimmerman 	struct dwc2_qtd *urb_qtd;
2143197ba5f4SPaul Zimmerman 
2144197ba5f4SPaul Zimmerman 	urb_qtd = urb->qtd;
2145197ba5f4SPaul Zimmerman 	if (!urb_qtd) {
2146197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
2147197ba5f4SPaul Zimmerman 		return -EINVAL;
2148197ba5f4SPaul Zimmerman 	}
2149197ba5f4SPaul Zimmerman 
2150197ba5f4SPaul Zimmerman 	qh = urb_qtd->qh;
2151197ba5f4SPaul Zimmerman 	if (!qh) {
2152197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
2153197ba5f4SPaul Zimmerman 		return -EINVAL;
2154197ba5f4SPaul Zimmerman 	}
2155197ba5f4SPaul Zimmerman 
2156197ba5f4SPaul Zimmerman 	urb->priv = NULL;
2157197ba5f4SPaul Zimmerman 
2158197ba5f4SPaul Zimmerman 	if (urb_qtd->in_process && qh->channel) {
2159197ba5f4SPaul Zimmerman 		dwc2_dump_channel_info(hsotg, qh->channel);
2160197ba5f4SPaul Zimmerman 
2161197ba5f4SPaul Zimmerman 		/* The QTD is in process (it has been assigned to a channel) */
2162197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status)
2163197ba5f4SPaul Zimmerman 			/*
2164197ba5f4SPaul Zimmerman 			 * If still connected (i.e. in host mode), halt the
2165197ba5f4SPaul Zimmerman 			 * channel so it can be used for other transfers. If
2166197ba5f4SPaul Zimmerman 			 * no longer connected, the host registers can't be
2167197ba5f4SPaul Zimmerman 			 * written to halt the channel since the core is in
2168197ba5f4SPaul Zimmerman 			 * device mode.
2169197ba5f4SPaul Zimmerman 			 */
2170197ba5f4SPaul Zimmerman 			dwc2_hc_halt(hsotg, qh->channel,
2171197ba5f4SPaul Zimmerman 				     DWC2_HC_XFER_URB_DEQUEUE);
2172197ba5f4SPaul Zimmerman 	}
2173197ba5f4SPaul Zimmerman 
2174197ba5f4SPaul Zimmerman 	/*
2175197ba5f4SPaul Zimmerman 	 * Free the QTD and clean up the associated QH. Leave the QH in the
2176197ba5f4SPaul Zimmerman 	 * schedule if it has any remaining QTDs.
2177197ba5f4SPaul Zimmerman 	 */
217895832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
2179197ba5f4SPaul Zimmerman 		u8 in_process = urb_qtd->in_process;
2180197ba5f4SPaul Zimmerman 
2181197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2182197ba5f4SPaul Zimmerman 		if (in_process) {
2183197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_deactivate(hsotg, qh, 0);
2184197ba5f4SPaul Zimmerman 			qh->channel = NULL;
2185197ba5f4SPaul Zimmerman 		} else if (list_empty(&qh->qtd_list)) {
2186197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
2187197ba5f4SPaul Zimmerman 		}
2188197ba5f4SPaul Zimmerman 	} else {
2189197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2190197ba5f4SPaul Zimmerman 	}
2191197ba5f4SPaul Zimmerman 
2192197ba5f4SPaul Zimmerman 	return 0;
2193197ba5f4SPaul Zimmerman }
2194197ba5f4SPaul Zimmerman 
2195197ba5f4SPaul Zimmerman /* Must NOT be called with interrupt disabled or spinlock held */
2196197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
2197197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep, int retry)
2198197ba5f4SPaul Zimmerman {
2199197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
2200197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2201197ba5f4SPaul Zimmerman 	unsigned long flags;
2202197ba5f4SPaul Zimmerman 	int rc;
2203197ba5f4SPaul Zimmerman 
2204197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
2205197ba5f4SPaul Zimmerman 
2206197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
2207197ba5f4SPaul Zimmerman 	if (!qh) {
2208197ba5f4SPaul Zimmerman 		rc = -EINVAL;
2209197ba5f4SPaul Zimmerman 		goto err;
2210197ba5f4SPaul Zimmerman 	}
2211197ba5f4SPaul Zimmerman 
2212197ba5f4SPaul Zimmerman 	while (!list_empty(&qh->qtd_list) && retry--) {
2213197ba5f4SPaul Zimmerman 		if (retry == 0) {
2214197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2215197ba5f4SPaul Zimmerman 				"## timeout in dwc2_hcd_endpoint_disable() ##\n");
2216197ba5f4SPaul Zimmerman 			rc = -EBUSY;
2217197ba5f4SPaul Zimmerman 			goto err;
2218197ba5f4SPaul Zimmerman 		}
2219197ba5f4SPaul Zimmerman 
2220197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
222104a9db79SNicholas Mc Guire 		msleep(20);
2222197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
2223197ba5f4SPaul Zimmerman 		qh = ep->hcpriv;
2224197ba5f4SPaul Zimmerman 		if (!qh) {
2225197ba5f4SPaul Zimmerman 			rc = -EINVAL;
2226197ba5f4SPaul Zimmerman 			goto err;
2227197ba5f4SPaul Zimmerman 		}
2228197ba5f4SPaul Zimmerman 	}
2229197ba5f4SPaul Zimmerman 
2230197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_unlink(hsotg, qh);
2231197ba5f4SPaul Zimmerman 
2232197ba5f4SPaul Zimmerman 	/* Free each QTD in the QH's QTD list */
2233197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2234197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2235197ba5f4SPaul Zimmerman 
2236197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
223716e80218SDouglas Anderson 
223816e80218SDouglas Anderson 	if (qh->channel && qh->channel->qh == qh)
223916e80218SDouglas Anderson 		qh->channel->qh = NULL;
224016e80218SDouglas Anderson 
2241197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
224216e80218SDouglas Anderson 
2243197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_free(hsotg, qh);
2244197ba5f4SPaul Zimmerman 
2245197ba5f4SPaul Zimmerman 	return 0;
2246197ba5f4SPaul Zimmerman 
2247197ba5f4SPaul Zimmerman err:
2248197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
2249197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
2250197ba5f4SPaul Zimmerman 
2251197ba5f4SPaul Zimmerman 	return rc;
2252197ba5f4SPaul Zimmerman }
2253197ba5f4SPaul Zimmerman 
2254197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
2255197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2256197ba5f4SPaul Zimmerman 				   struct usb_host_endpoint *ep)
2257197ba5f4SPaul Zimmerman {
2258197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = ep->hcpriv;
2259197ba5f4SPaul Zimmerman 
2260197ba5f4SPaul Zimmerman 	if (!qh)
2261197ba5f4SPaul Zimmerman 		return -EINVAL;
2262197ba5f4SPaul Zimmerman 
2263197ba5f4SPaul Zimmerman 	qh->data_toggle = DWC2_HC_PID_DATA0;
2264197ba5f4SPaul Zimmerman 
2265197ba5f4SPaul Zimmerman 	return 0;
2266197ba5f4SPaul Zimmerman }
2267197ba5f4SPaul Zimmerman 
2268b02038faSJohn Youn /**
2269b02038faSJohn Youn  * dwc2_core_init() - Initializes the DWC_otg controller registers and
2270b02038faSJohn Youn  * prepares the core for device mode or host mode operation
2271b02038faSJohn Youn  *
2272b02038faSJohn Youn  * @hsotg:         Programming view of the DWC_otg controller
2273b02038faSJohn Youn  * @initial_setup: If true then this is the first init for this instance.
2274b02038faSJohn Youn  */
227565c9c4c6SVardan Mikayelyan int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2276b02038faSJohn Youn {
2277b02038faSJohn Youn 	u32 usbcfg, otgctl;
2278b02038faSJohn Youn 	int retval;
2279b02038faSJohn Youn 
2280b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2281b02038faSJohn Youn 
2282b02038faSJohn Youn 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2283b02038faSJohn Youn 
2284b02038faSJohn Youn 	/* Set ULPI External VBUS bit if needed */
2285b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
228695832c00SJohn Youn 	if (hsotg->params.phy_ulpi_ext_vbus)
2287b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2288b02038faSJohn Youn 
2289b02038faSJohn Youn 	/* Set external TS Dline pulsing bit if needed */
2290b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
229195832c00SJohn Youn 	if (hsotg->params.ts_dline)
2292b02038faSJohn Youn 		usbcfg |= GUSBCFG_TERMSELDLPULSE;
2293b02038faSJohn Youn 
2294b02038faSJohn Youn 	dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2295b02038faSJohn Youn 
2296b02038faSJohn Youn 	/*
2297b02038faSJohn Youn 	 * Reset the Controller
2298b02038faSJohn Youn 	 *
2299b02038faSJohn Youn 	 * We only need to reset the controller if this is a re-init.
2300b02038faSJohn Youn 	 * For the first init we know for sure that earlier code reset us (it
2301b02038faSJohn Youn 	 * needed to in order to properly detect various parameters).
2302b02038faSJohn Youn 	 */
2303b02038faSJohn Youn 	if (!initial_setup) {
230413b1f8e2SVardan Mikayelyan 		retval = dwc2_core_reset(hsotg, false);
2305b02038faSJohn Youn 		if (retval) {
2306b02038faSJohn Youn 			dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2307b02038faSJohn Youn 				__func__);
2308b02038faSJohn Youn 			return retval;
2309b02038faSJohn Youn 		}
2310b02038faSJohn Youn 	}
2311b02038faSJohn Youn 
2312b02038faSJohn Youn 	/*
2313b02038faSJohn Youn 	 * This needs to happen in FS mode before any other programming occurs
2314b02038faSJohn Youn 	 */
2315b02038faSJohn Youn 	retval = dwc2_phy_init(hsotg, initial_setup);
2316b02038faSJohn Youn 	if (retval)
2317b02038faSJohn Youn 		return retval;
2318b02038faSJohn Youn 
2319b02038faSJohn Youn 	/* Program the GAHBCFG Register */
2320b02038faSJohn Youn 	retval = dwc2_gahbcfg_init(hsotg);
2321b02038faSJohn Youn 	if (retval)
2322b02038faSJohn Youn 		return retval;
2323b02038faSJohn Youn 
2324b02038faSJohn Youn 	/* Program the GUSBCFG register */
2325b02038faSJohn Youn 	dwc2_gusbcfg_init(hsotg);
2326b02038faSJohn Youn 
2327b02038faSJohn Youn 	/* Program the GOTGCTL register */
2328b02038faSJohn Youn 	otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2329b02038faSJohn Youn 	otgctl &= ~GOTGCTL_OTGVER;
2330b02038faSJohn Youn 	dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2331b02038faSJohn Youn 
2332b02038faSJohn Youn 	/* Clear the SRP success bit for FS-I2c */
2333b02038faSJohn Youn 	hsotg->srp_success = 0;
2334b02038faSJohn Youn 
2335b02038faSJohn Youn 	/* Enable common interrupts */
2336b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
2337b02038faSJohn Youn 
2338b02038faSJohn Youn 	/*
2339b02038faSJohn Youn 	 * Do device or host initialization based on mode during PCD and
2340b02038faSJohn Youn 	 * HCD initialization
2341b02038faSJohn Youn 	 */
2342b02038faSJohn Youn 	if (dwc2_is_host_mode(hsotg)) {
2343b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Host Mode\n");
2344b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_A_HOST;
2345b02038faSJohn Youn 	} else {
2346b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Device Mode\n");
2347b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2348b02038faSJohn Youn 	}
2349b02038faSJohn Youn 
2350b02038faSJohn Youn 	return 0;
2351b02038faSJohn Youn }
2352b02038faSJohn Youn 
2353b02038faSJohn Youn /**
2354b02038faSJohn Youn  * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2355b02038faSJohn Youn  * Host mode
2356b02038faSJohn Youn  *
2357b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
2358b02038faSJohn Youn  *
2359b02038faSJohn Youn  * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2360b02038faSJohn Youn  * request queues. Host channels are reset to ensure that they are ready for
2361b02038faSJohn Youn  * performing transfers.
2362b02038faSJohn Youn  */
2363b02038faSJohn Youn static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2364b02038faSJohn Youn {
236592a8dd26SMinas Harutyunyan 	u32 hcfg, hfir, otgctl, usbcfg;
2366b02038faSJohn Youn 
2367b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2368b02038faSJohn Youn 
236992a8dd26SMinas Harutyunyan 	/* Set HS/FS Timeout Calibration to 7 (max available value).
237092a8dd26SMinas Harutyunyan 	 * The number of PHY clocks that the application programs in
237192a8dd26SMinas Harutyunyan 	 * this field is added to the high/full speed interpacket timeout
237292a8dd26SMinas Harutyunyan 	 * duration in the core to account for any additional delays
237392a8dd26SMinas Harutyunyan 	 * introduced by the PHY. This can be required, because the delay
237492a8dd26SMinas Harutyunyan 	 * introduced by the PHY in generating the linestate condition
237592a8dd26SMinas Harutyunyan 	 * can vary from one PHY to another.
237692a8dd26SMinas Harutyunyan 	 */
237792a8dd26SMinas Harutyunyan 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
237892a8dd26SMinas Harutyunyan 	usbcfg |= GUSBCFG_TOUTCAL(7);
237992a8dd26SMinas Harutyunyan 	dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
238092a8dd26SMinas Harutyunyan 
2381b02038faSJohn Youn 	/* Restart the Phy Clock */
2382b02038faSJohn Youn 	dwc2_writel(0, hsotg->regs + PCGCTL);
2383b02038faSJohn Youn 
2384b02038faSJohn Youn 	/* Initialize Host Configuration Register */
2385b02038faSJohn Youn 	dwc2_init_fs_ls_pclk_sel(hsotg);
238638e9002bSVardan Mikayelyan 	if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
238738e9002bSVardan Mikayelyan 	    hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
2388b02038faSJohn Youn 		hcfg = dwc2_readl(hsotg->regs + HCFG);
2389b02038faSJohn Youn 		hcfg |= HCFG_FSLSSUPP;
2390b02038faSJohn Youn 		dwc2_writel(hcfg, hsotg->regs + HCFG);
2391b02038faSJohn Youn 	}
2392b02038faSJohn Youn 
2393b02038faSJohn Youn 	/*
2394b02038faSJohn Youn 	 * This bit allows dynamic reloading of the HFIR register during
2395b02038faSJohn Youn 	 * runtime. This bit needs to be programmed during initial configuration
2396b02038faSJohn Youn 	 * and its value must not be changed during runtime.
2397b02038faSJohn Youn 	 */
239895832c00SJohn Youn 	if (hsotg->params.reload_ctl) {
2399b02038faSJohn Youn 		hfir = dwc2_readl(hsotg->regs + HFIR);
2400b02038faSJohn Youn 		hfir |= HFIR_RLDCTRL;
2401b02038faSJohn Youn 		dwc2_writel(hfir, hsotg->regs + HFIR);
2402b02038faSJohn Youn 	}
2403b02038faSJohn Youn 
240495832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2405b02038faSJohn Youn 		u32 op_mode = hsotg->hw_params.op_mode;
2406b02038faSJohn Youn 
2407b02038faSJohn Youn 		if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2408b02038faSJohn Youn 		    !hsotg->hw_params.dma_desc_enable ||
2409b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2410b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2411b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2412b02038faSJohn Youn 			dev_err(hsotg->dev,
2413b02038faSJohn Youn 				"Hardware does not support descriptor DMA mode -\n");
2414b02038faSJohn Youn 			dev_err(hsotg->dev,
2415b02038faSJohn Youn 				"falling back to buffer DMA mode.\n");
241695832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
2417b02038faSJohn Youn 		} else {
2418b02038faSJohn Youn 			hcfg = dwc2_readl(hsotg->regs + HCFG);
2419b02038faSJohn Youn 			hcfg |= HCFG_DESCDMA;
2420b02038faSJohn Youn 			dwc2_writel(hcfg, hsotg->regs + HCFG);
2421b02038faSJohn Youn 		}
2422b02038faSJohn Youn 	}
2423b02038faSJohn Youn 
2424b02038faSJohn Youn 	/* Configure data FIFO sizes */
2425b02038faSJohn Youn 	dwc2_config_fifos(hsotg);
2426b02038faSJohn Youn 
2427b02038faSJohn Youn 	/* TODO - check this */
2428b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2429b02038faSJohn Youn 	otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2430b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2431b02038faSJohn Youn 	dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2432b02038faSJohn Youn 
2433b02038faSJohn Youn 	/* Make sure the FIFOs are flushed */
2434b02038faSJohn Youn 	dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2435b02038faSJohn Youn 	dwc2_flush_rx_fifo(hsotg);
2436b02038faSJohn Youn 
2437b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2438b02038faSJohn Youn 	otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2439b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2440b02038faSJohn Youn 	dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2441b02038faSJohn Youn 
244295832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
2443b02038faSJohn Youn 		int num_channels, i;
2444b02038faSJohn Youn 		u32 hcchar;
2445b02038faSJohn Youn 
2446b02038faSJohn Youn 		/* Flush out any leftover queued requests */
2447bea8e86cSJohn Youn 		num_channels = hsotg->params.host_channels;
2448b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2449b02038faSJohn Youn 			hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2450b02038faSJohn Youn 			hcchar &= ~HCCHAR_CHENA;
2451b02038faSJohn Youn 			hcchar |= HCCHAR_CHDIS;
2452b02038faSJohn Youn 			hcchar &= ~HCCHAR_EPDIR;
2453b02038faSJohn Youn 			dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2454b02038faSJohn Youn 		}
2455b02038faSJohn Youn 
2456b02038faSJohn Youn 		/* Halt all channels to put them into a known state */
2457b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2458b02038faSJohn Youn 			hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2459b02038faSJohn Youn 			hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2460b02038faSJohn Youn 			hcchar &= ~HCCHAR_EPDIR;
2461b02038faSJohn Youn 			dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2462b02038faSJohn Youn 			dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2463b02038faSJohn Youn 				__func__, i);
246479d6b8c5SSevak Arakelyan 
246579d6b8c5SSevak Arakelyan 			if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
246679d6b8c5SSevak Arakelyan 						      HCCHAR_CHENA, 1000)) {
246779d6b8c5SSevak Arakelyan 				dev_warn(hsotg->dev, "Unable to clear enable on channel %d\n",
2468b02038faSJohn Youn 					 i);
2469b02038faSJohn Youn 			}
2470b02038faSJohn Youn 		}
2471b02038faSJohn Youn 	}
2472b02038faSJohn Youn 
247366e77a24SRazmik Karapetyan 	/* Enable ACG feature in host mode, if supported */
247466e77a24SRazmik Karapetyan 	dwc2_enable_acg(hsotg);
247566e77a24SRazmik Karapetyan 
2476b02038faSJohn Youn 	/* Turn on the vbus power */
2477b02038faSJohn Youn 	dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2478b02038faSJohn Youn 	if (hsotg->op_state == OTG_STATE_A_HOST) {
2479b02038faSJohn Youn 		u32 hprt0 = dwc2_read_hprt0(hsotg);
2480b02038faSJohn Youn 
2481b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2482b02038faSJohn Youn 			!!(hprt0 & HPRT0_PWR));
2483b02038faSJohn Youn 		if (!(hprt0 & HPRT0_PWR)) {
2484b02038faSJohn Youn 			hprt0 |= HPRT0_PWR;
2485b02038faSJohn Youn 			dwc2_writel(hprt0, hsotg->regs + HPRT0);
2486b02038faSJohn Youn 		}
2487b02038faSJohn Youn 	}
2488b02038faSJohn Youn 
2489b02038faSJohn Youn 	dwc2_enable_host_interrupts(hsotg);
2490b02038faSJohn Youn }
2491b02038faSJohn Youn 
2492197ba5f4SPaul Zimmerman /*
2493197ba5f4SPaul Zimmerman  * Initializes dynamic portions of the DWC_otg HCD state
2494197ba5f4SPaul Zimmerman  *
2495197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2496197ba5f4SPaul Zimmerman  */
2497197ba5f4SPaul Zimmerman static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2498197ba5f4SPaul Zimmerman {
2499197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan, *chan_tmp;
2500197ba5f4SPaul Zimmerman 	int num_channels;
2501197ba5f4SPaul Zimmerman 	int i;
2502197ba5f4SPaul Zimmerman 
2503197ba5f4SPaul Zimmerman 	hsotg->flags.d32 = 0;
2504197ba5f4SPaul Zimmerman 	hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
2505197ba5f4SPaul Zimmerman 
250695832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
2507197ba5f4SPaul Zimmerman 		hsotg->available_host_channels =
2508bea8e86cSJohn Youn 			hsotg->params.host_channels;
2509197ba5f4SPaul Zimmerman 	} else {
2510197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels = 0;
2511197ba5f4SPaul Zimmerman 		hsotg->periodic_channels = 0;
2512197ba5f4SPaul Zimmerman 	}
2513197ba5f4SPaul Zimmerman 
2514197ba5f4SPaul Zimmerman 	/*
2515197ba5f4SPaul Zimmerman 	 * Put all channels in the free channel list and clean up channel
2516197ba5f4SPaul Zimmerman 	 * states
2517197ba5f4SPaul Zimmerman 	 */
2518197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2519197ba5f4SPaul Zimmerman 				 hc_list_entry)
2520197ba5f4SPaul Zimmerman 		list_del_init(&chan->hc_list_entry);
2521197ba5f4SPaul Zimmerman 
2522bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2523197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
2524197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
2525197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2526197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
2527197ba5f4SPaul Zimmerman 	}
2528197ba5f4SPaul Zimmerman 
2529197ba5f4SPaul Zimmerman 	/* Initialize the DWC core for host mode operation */
2530197ba5f4SPaul Zimmerman 	dwc2_core_host_init(hsotg);
2531197ba5f4SPaul Zimmerman }
2532197ba5f4SPaul Zimmerman 
2533197ba5f4SPaul Zimmerman static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2534197ba5f4SPaul Zimmerman 			       struct dwc2_host_chan *chan,
2535197ba5f4SPaul Zimmerman 			       struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2536197ba5f4SPaul Zimmerman {
2537197ba5f4SPaul Zimmerman 	int hub_addr, hub_port;
2538197ba5f4SPaul Zimmerman 
2539197ba5f4SPaul Zimmerman 	chan->do_split = 1;
2540197ba5f4SPaul Zimmerman 	chan->xact_pos = qtd->isoc_split_pos;
2541197ba5f4SPaul Zimmerman 	chan->complete_split = qtd->complete_split;
2542197ba5f4SPaul Zimmerman 	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2543197ba5f4SPaul Zimmerman 	chan->hub_addr = (u8)hub_addr;
2544197ba5f4SPaul Zimmerman 	chan->hub_port = (u8)hub_port;
2545197ba5f4SPaul Zimmerman }
2546197ba5f4SPaul Zimmerman 
25473bc04e28SDouglas Anderson static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2548197ba5f4SPaul Zimmerman 			      struct dwc2_host_chan *chan,
25493bc04e28SDouglas Anderson 			      struct dwc2_qtd *qtd)
2550197ba5f4SPaul Zimmerman {
2551197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
2552197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
2553197ba5f4SPaul Zimmerman 
2554197ba5f4SPaul Zimmerman 	switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2555197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
2556197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2557197ba5f4SPaul Zimmerman 
2558197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
2559197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
2560197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control setup transaction\n");
2561197ba5f4SPaul Zimmerman 			chan->do_ping = 0;
2562197ba5f4SPaul Zimmerman 			chan->ep_is_in = 0;
2563197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_SETUP;
256495832c00SJohn Youn 			if (hsotg->params.host_dma)
2565197ba5f4SPaul Zimmerman 				chan->xfer_dma = urb->setup_dma;
2566197ba5f4SPaul Zimmerman 			else
2567197ba5f4SPaul Zimmerman 				chan->xfer_buf = urb->setup_packet;
2568197ba5f4SPaul Zimmerman 			chan->xfer_len = 8;
2569197ba5f4SPaul Zimmerman 			break;
2570197ba5f4SPaul Zimmerman 
2571197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
2572197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control data transaction\n");
2573197ba5f4SPaul Zimmerman 			chan->data_pid_start = qtd->data_toggle;
2574197ba5f4SPaul Zimmerman 			break;
2575197ba5f4SPaul Zimmerman 
2576197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_STATUS:
2577197ba5f4SPaul Zimmerman 			/*
2578197ba5f4SPaul Zimmerman 			 * Direction is opposite of data direction or IN if no
2579197ba5f4SPaul Zimmerman 			 * data
2580197ba5f4SPaul Zimmerman 			 */
2581197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control status transaction\n");
2582197ba5f4SPaul Zimmerman 			if (urb->length == 0)
2583197ba5f4SPaul Zimmerman 				chan->ep_is_in = 1;
2584197ba5f4SPaul Zimmerman 			else
2585197ba5f4SPaul Zimmerman 				chan->ep_is_in =
2586197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_out(&urb->pipe_info);
2587197ba5f4SPaul Zimmerman 			if (chan->ep_is_in)
2588197ba5f4SPaul Zimmerman 				chan->do_ping = 0;
2589197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_DATA1;
2590197ba5f4SPaul Zimmerman 			chan->xfer_len = 0;
259195832c00SJohn Youn 			if (hsotg->params.host_dma)
2592197ba5f4SPaul Zimmerman 				chan->xfer_dma = hsotg->status_buf_dma;
2593197ba5f4SPaul Zimmerman 			else
2594197ba5f4SPaul Zimmerman 				chan->xfer_buf = hsotg->status_buf;
2595197ba5f4SPaul Zimmerman 			break;
2596197ba5f4SPaul Zimmerman 		}
2597197ba5f4SPaul Zimmerman 		break;
2598197ba5f4SPaul Zimmerman 
2599197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
2600197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_BULK;
2601197ba5f4SPaul Zimmerman 		break;
2602197ba5f4SPaul Zimmerman 
2603197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
2604197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_INT;
2605197ba5f4SPaul Zimmerman 		break;
2606197ba5f4SPaul Zimmerman 
2607197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
2608197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_ISOC;
260995832c00SJohn Youn 		if (hsotg->params.dma_desc_enable)
2610197ba5f4SPaul Zimmerman 			break;
2611197ba5f4SPaul Zimmerman 
2612197ba5f4SPaul Zimmerman 		frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2613197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
2614197ba5f4SPaul Zimmerman 
261595832c00SJohn Youn 		if (hsotg->params.host_dma) {
2616197ba5f4SPaul Zimmerman 			chan->xfer_dma = urb->dma;
2617197ba5f4SPaul Zimmerman 			chan->xfer_dma += frame_desc->offset +
2618197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2619197ba5f4SPaul Zimmerman 		} else {
2620197ba5f4SPaul Zimmerman 			chan->xfer_buf = urb->buf;
2621197ba5f4SPaul Zimmerman 			chan->xfer_buf += frame_desc->offset +
2622197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2623197ba5f4SPaul Zimmerman 		}
2624197ba5f4SPaul Zimmerman 
2625197ba5f4SPaul Zimmerman 		chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2626197ba5f4SPaul Zimmerman 
2627197ba5f4SPaul Zimmerman 		if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2628197ba5f4SPaul Zimmerman 			if (chan->xfer_len <= 188)
2629197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2630197ba5f4SPaul Zimmerman 			else
2631197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2632197ba5f4SPaul Zimmerman 		}
2633197ba5f4SPaul Zimmerman 		break;
2634197ba5f4SPaul Zimmerman 	}
2635197ba5f4SPaul Zimmerman }
2636197ba5f4SPaul Zimmerman 
2637af424a41SWilliam Wu static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
2638af424a41SWilliam Wu 					    struct dwc2_qh *qh,
2639af424a41SWilliam Wu 					    struct dwc2_host_chan *chan)
2640af424a41SWilliam Wu {
2641af424a41SWilliam Wu 	if (!hsotg->unaligned_cache ||
2642af424a41SWilliam Wu 	    chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
2643af424a41SWilliam Wu 		return -ENOMEM;
2644af424a41SWilliam Wu 
2645af424a41SWilliam Wu 	if (!qh->dw_align_buf) {
2646af424a41SWilliam Wu 		qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
2647af424a41SWilliam Wu 						    GFP_ATOMIC | GFP_DMA);
2648af424a41SWilliam Wu 		if (!qh->dw_align_buf)
2649af424a41SWilliam Wu 			return -ENOMEM;
2650af424a41SWilliam Wu 	}
2651af424a41SWilliam Wu 
2652af424a41SWilliam Wu 	qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
2653af424a41SWilliam Wu 					      DWC2_KMEM_UNALIGNED_BUF_SIZE,
2654af424a41SWilliam Wu 					      DMA_FROM_DEVICE);
2655af424a41SWilliam Wu 
2656af424a41SWilliam Wu 	if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
2657af424a41SWilliam Wu 		dev_err(hsotg->dev, "can't map align_buf\n");
2658af424a41SWilliam Wu 		chan->align_buf = 0;
2659af424a41SWilliam Wu 		return -EINVAL;
2660af424a41SWilliam Wu 	}
2661af424a41SWilliam Wu 
2662af424a41SWilliam Wu 	chan->align_buf = qh->dw_align_buf_dma;
2663af424a41SWilliam Wu 	return 0;
2664af424a41SWilliam Wu }
2665af424a41SWilliam Wu 
26663bc04e28SDouglas Anderson #define DWC2_USB_DMA_ALIGN 4
26673bc04e28SDouglas Anderson 
26683bc04e28SDouglas Anderson static void dwc2_free_dma_aligned_buffer(struct urb *urb)
2669197ba5f4SPaul Zimmerman {
267056406e01SAntti Seppälä 	void *stored_xfer_buffer;
2671*1e111e88SAntti Seppälä 	size_t length;
2672197ba5f4SPaul Zimmerman 
26733bc04e28SDouglas Anderson 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
26743bc04e28SDouglas Anderson 		return;
2675197ba5f4SPaul Zimmerman 
267656406e01SAntti Seppälä 	/* Restore urb->transfer_buffer from the end of the allocated area */
267756406e01SAntti Seppälä 	memcpy(&stored_xfer_buffer, urb->transfer_buffer +
267856406e01SAntti Seppälä 	       urb->transfer_buffer_length, sizeof(urb->transfer_buffer));
26793bc04e28SDouglas Anderson 
2680*1e111e88SAntti Seppälä 	if (usb_urb_dir_in(urb)) {
2681*1e111e88SAntti Seppälä 		if (usb_pipeisoc(urb->pipe))
2682*1e111e88SAntti Seppälä 			length = urb->transfer_buffer_length;
2683*1e111e88SAntti Seppälä 		else
2684*1e111e88SAntti Seppälä 			length = urb->actual_length;
2685*1e111e88SAntti Seppälä 
2686*1e111e88SAntti Seppälä 		memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
2687*1e111e88SAntti Seppälä 	}
268856406e01SAntti Seppälä 	kfree(urb->transfer_buffer);
268956406e01SAntti Seppälä 	urb->transfer_buffer = stored_xfer_buffer;
26903bc04e28SDouglas Anderson 
26913bc04e28SDouglas Anderson 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2692197ba5f4SPaul Zimmerman }
2693197ba5f4SPaul Zimmerman 
26943bc04e28SDouglas Anderson static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
26953bc04e28SDouglas Anderson {
269656406e01SAntti Seppälä 	void *kmalloc_ptr;
26973bc04e28SDouglas Anderson 	size_t kmalloc_size;
26985dce9555SPaul Zimmerman 
26993bc04e28SDouglas Anderson 	if (urb->num_sgs || urb->sg ||
27003bc04e28SDouglas Anderson 	    urb->transfer_buffer_length == 0 ||
27013bc04e28SDouglas Anderson 	    !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2702197ba5f4SPaul Zimmerman 		return 0;
27033bc04e28SDouglas Anderson 
270456406e01SAntti Seppälä 	/*
270556406e01SAntti Seppälä 	 * Allocate a buffer with enough padding for original transfer_buffer
270656406e01SAntti Seppälä 	 * pointer. This allocation is guaranteed to be aligned properly for
270756406e01SAntti Seppälä 	 * DMA
270856406e01SAntti Seppälä 	 */
27093bc04e28SDouglas Anderson 	kmalloc_size = urb->transfer_buffer_length +
271056406e01SAntti Seppälä 		sizeof(urb->transfer_buffer);
27113bc04e28SDouglas Anderson 
27123bc04e28SDouglas Anderson 	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
27133bc04e28SDouglas Anderson 	if (!kmalloc_ptr)
27143bc04e28SDouglas Anderson 		return -ENOMEM;
27153bc04e28SDouglas Anderson 
271656406e01SAntti Seppälä 	/*
271756406e01SAntti Seppälä 	 * Position value of original urb->transfer_buffer pointer to the end
271856406e01SAntti Seppälä 	 * of allocation for later referencing
271956406e01SAntti Seppälä 	 */
272056406e01SAntti Seppälä 	memcpy(kmalloc_ptr + urb->transfer_buffer_length,
272156406e01SAntti Seppälä 	       &urb->transfer_buffer, sizeof(urb->transfer_buffer));
272256406e01SAntti Seppälä 
27233bc04e28SDouglas Anderson 	if (usb_urb_dir_out(urb))
272456406e01SAntti Seppälä 		memcpy(kmalloc_ptr, urb->transfer_buffer,
27253bc04e28SDouglas Anderson 		       urb->transfer_buffer_length);
272656406e01SAntti Seppälä 	urb->transfer_buffer = kmalloc_ptr;
27273bc04e28SDouglas Anderson 
27283bc04e28SDouglas Anderson 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
27293bc04e28SDouglas Anderson 
27303bc04e28SDouglas Anderson 	return 0;
27313bc04e28SDouglas Anderson }
27323bc04e28SDouglas Anderson 
27333bc04e28SDouglas Anderson static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
27343bc04e28SDouglas Anderson 				gfp_t mem_flags)
27353bc04e28SDouglas Anderson {
27363bc04e28SDouglas Anderson 	int ret;
27373bc04e28SDouglas Anderson 
27383bc04e28SDouglas Anderson 	/* We assume setup_dma is always aligned; warn if not */
27393bc04e28SDouglas Anderson 	WARN_ON_ONCE(urb->setup_dma &&
27403bc04e28SDouglas Anderson 		     (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
27413bc04e28SDouglas Anderson 
27423bc04e28SDouglas Anderson 	ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
27433bc04e28SDouglas Anderson 	if (ret)
27443bc04e28SDouglas Anderson 		return ret;
27453bc04e28SDouglas Anderson 
27463bc04e28SDouglas Anderson 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
27473bc04e28SDouglas Anderson 	if (ret)
27483bc04e28SDouglas Anderson 		dwc2_free_dma_aligned_buffer(urb);
27493bc04e28SDouglas Anderson 
27503bc04e28SDouglas Anderson 	return ret;
27513bc04e28SDouglas Anderson }
27523bc04e28SDouglas Anderson 
27533bc04e28SDouglas Anderson static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
27543bc04e28SDouglas Anderson {
27553bc04e28SDouglas Anderson 	usb_hcd_unmap_urb_for_dma(hcd, urb);
27563bc04e28SDouglas Anderson 	dwc2_free_dma_aligned_buffer(urb);
2757197ba5f4SPaul Zimmerman }
2758197ba5f4SPaul Zimmerman 
2759197ba5f4SPaul Zimmerman /**
2760197ba5f4SPaul Zimmerman  * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2761197ba5f4SPaul Zimmerman  * channel and initializes the host channel to perform the transactions. The
2762197ba5f4SPaul Zimmerman  * host channel is removed from the free list.
2763197ba5f4SPaul Zimmerman  *
2764197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2765197ba5f4SPaul Zimmerman  * @qh:    Transactions from the first QTD for this QH are selected and assigned
2766197ba5f4SPaul Zimmerman  *         to a free host channel
2767197ba5f4SPaul Zimmerman  */
2768197ba5f4SPaul Zimmerman static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
2769197ba5f4SPaul Zimmerman {
2770197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
2771197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
2772197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
2773197ba5f4SPaul Zimmerman 
2774197ba5f4SPaul Zimmerman 	if (dbg_qh(qh))
2775197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
2776197ba5f4SPaul Zimmerman 
2777197ba5f4SPaul Zimmerman 	if (list_empty(&qh->qtd_list)) {
2778197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No QTDs in QH list\n");
2779197ba5f4SPaul Zimmerman 		return -ENOMEM;
2780197ba5f4SPaul Zimmerman 	}
2781197ba5f4SPaul Zimmerman 
2782197ba5f4SPaul Zimmerman 	if (list_empty(&hsotg->free_hc_list)) {
2783197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No free channel to assign\n");
2784197ba5f4SPaul Zimmerman 		return -ENOMEM;
2785197ba5f4SPaul Zimmerman 	}
2786197ba5f4SPaul Zimmerman 
2787197ba5f4SPaul Zimmerman 	chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2788197ba5f4SPaul Zimmerman 				hc_list_entry);
2789197ba5f4SPaul Zimmerman 
2790197ba5f4SPaul Zimmerman 	/* Remove host channel from free list */
2791197ba5f4SPaul Zimmerman 	list_del_init(&chan->hc_list_entry);
2792197ba5f4SPaul Zimmerman 
2793197ba5f4SPaul Zimmerman 	qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2794197ba5f4SPaul Zimmerman 	urb = qtd->urb;
2795197ba5f4SPaul Zimmerman 	qh->channel = chan;
2796197ba5f4SPaul Zimmerman 	qtd->in_process = 1;
2797197ba5f4SPaul Zimmerman 
2798197ba5f4SPaul Zimmerman 	/*
2799197ba5f4SPaul Zimmerman 	 * Use usb_pipedevice to determine device address. This address is
2800197ba5f4SPaul Zimmerman 	 * 0 before the SET_ADDRESS command and the correct address afterward.
2801197ba5f4SPaul Zimmerman 	 */
2802197ba5f4SPaul Zimmerman 	chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2803197ba5f4SPaul Zimmerman 	chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2804197ba5f4SPaul Zimmerman 	chan->speed = qh->dev_speed;
2805197ba5f4SPaul Zimmerman 	chan->max_packet = dwc2_max_packet(qh->maxp);
2806197ba5f4SPaul Zimmerman 
2807197ba5f4SPaul Zimmerman 	chan->xfer_started = 0;
2808197ba5f4SPaul Zimmerman 	chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2809197ba5f4SPaul Zimmerman 	chan->error_state = (qtd->error_count > 0);
2810197ba5f4SPaul Zimmerman 	chan->halt_on_queue = 0;
2811197ba5f4SPaul Zimmerman 	chan->halt_pending = 0;
2812197ba5f4SPaul Zimmerman 	chan->requests = 0;
2813197ba5f4SPaul Zimmerman 
2814197ba5f4SPaul Zimmerman 	/*
2815197ba5f4SPaul Zimmerman 	 * The following values may be modified in the transfer type section
2816197ba5f4SPaul Zimmerman 	 * below. The xfer_len value may be reduced when the transfer is
2817197ba5f4SPaul Zimmerman 	 * started to accommodate the max widths of the XferSize and PktCnt
2818197ba5f4SPaul Zimmerman 	 * fields in the HCTSIZn register.
2819197ba5f4SPaul Zimmerman 	 */
2820197ba5f4SPaul Zimmerman 
2821197ba5f4SPaul Zimmerman 	chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2822197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
2823197ba5f4SPaul Zimmerman 		chan->do_ping = 0;
2824197ba5f4SPaul Zimmerman 	else
2825197ba5f4SPaul Zimmerman 		chan->do_ping = qh->ping_state;
2826197ba5f4SPaul Zimmerman 
2827197ba5f4SPaul Zimmerman 	chan->data_pid_start = qh->data_toggle;
2828197ba5f4SPaul Zimmerman 	chan->multi_count = 1;
2829197ba5f4SPaul Zimmerman 
2830197ba5f4SPaul Zimmerman 	if (urb->actual_length > urb->length &&
2831197ba5f4SPaul Zimmerman 	    !dwc2_hcd_is_pipe_in(&urb->pipe_info))
2832197ba5f4SPaul Zimmerman 		urb->actual_length = urb->length;
2833197ba5f4SPaul Zimmerman 
283495832c00SJohn Youn 	if (hsotg->params.host_dma)
2835197ba5f4SPaul Zimmerman 		chan->xfer_dma = urb->dma + urb->actual_length;
28363bc04e28SDouglas Anderson 	else
2837197ba5f4SPaul Zimmerman 		chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
2838197ba5f4SPaul Zimmerman 
2839197ba5f4SPaul Zimmerman 	chan->xfer_len = urb->length - urb->actual_length;
2840197ba5f4SPaul Zimmerman 	chan->xfer_count = 0;
2841197ba5f4SPaul Zimmerman 
2842197ba5f4SPaul Zimmerman 	/* Set the split attributes if required */
2843197ba5f4SPaul Zimmerman 	if (qh->do_split)
2844197ba5f4SPaul Zimmerman 		dwc2_hc_init_split(hsotg, chan, qtd, urb);
2845197ba5f4SPaul Zimmerman 	else
2846197ba5f4SPaul Zimmerman 		chan->do_split = 0;
2847197ba5f4SPaul Zimmerman 
2848197ba5f4SPaul Zimmerman 	/* Set the transfer attributes */
28493bc04e28SDouglas Anderson 	dwc2_hc_init_xfer(hsotg, chan, qtd);
2850197ba5f4SPaul Zimmerman 
2851af424a41SWilliam Wu 	/* For non-dword aligned buffers */
2852af424a41SWilliam Wu 	if (hsotg->params.host_dma && qh->do_split &&
2853af424a41SWilliam Wu 	    chan->ep_is_in && (chan->xfer_dma & 0x3)) {
2854af424a41SWilliam Wu 		dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
2855af424a41SWilliam Wu 		if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
2856af424a41SWilliam Wu 			dev_err(hsotg->dev,
2857af424a41SWilliam Wu 				"Failed to allocate memory to handle non-aligned buffer\n");
2858af424a41SWilliam Wu 			/* Add channel back to free list */
2859af424a41SWilliam Wu 			chan->align_buf = 0;
2860af424a41SWilliam Wu 			chan->multi_count = 0;
2861af424a41SWilliam Wu 			list_add_tail(&chan->hc_list_entry,
2862af424a41SWilliam Wu 				      &hsotg->free_hc_list);
2863af424a41SWilliam Wu 			qtd->in_process = 0;
2864af424a41SWilliam Wu 			qh->channel = NULL;
2865af424a41SWilliam Wu 			return -ENOMEM;
2866af424a41SWilliam Wu 		}
2867af424a41SWilliam Wu 	} else {
2868af424a41SWilliam Wu 		/*
2869af424a41SWilliam Wu 		 * We assume that DMA is always aligned in non-split
2870af424a41SWilliam Wu 		 * case or split out case. Warn if not.
2871af424a41SWilliam Wu 		 */
2872af424a41SWilliam Wu 		WARN_ON_ONCE(hsotg->params.host_dma &&
2873af424a41SWilliam Wu 			     (chan->xfer_dma & 0x3));
2874af424a41SWilliam Wu 		chan->align_buf = 0;
2875af424a41SWilliam Wu 	}
2876af424a41SWilliam Wu 
2877197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2878197ba5f4SPaul Zimmerman 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2879197ba5f4SPaul Zimmerman 		/*
2880197ba5f4SPaul Zimmerman 		 * This value may be modified when the transfer is started
2881197ba5f4SPaul Zimmerman 		 * to reflect the actual transfer length
2882197ba5f4SPaul Zimmerman 		 */
2883197ba5f4SPaul Zimmerman 		chan->multi_count = dwc2_hb_mult(qh->maxp);
2884197ba5f4SPaul Zimmerman 
288595832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2886197ba5f4SPaul Zimmerman 		chan->desc_list_addr = qh->desc_list_dma;
288795105a99SGregory Herrero 		chan->desc_list_sz = qh->desc_list_sz;
288895105a99SGregory Herrero 	}
2889197ba5f4SPaul Zimmerman 
2890197ba5f4SPaul Zimmerman 	dwc2_hc_init(hsotg, chan);
2891197ba5f4SPaul Zimmerman 	chan->qh = qh;
2892197ba5f4SPaul Zimmerman 
2893197ba5f4SPaul Zimmerman 	return 0;
2894197ba5f4SPaul Zimmerman }
2895197ba5f4SPaul Zimmerman 
2896197ba5f4SPaul Zimmerman /**
2897197ba5f4SPaul Zimmerman  * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2898197ba5f4SPaul Zimmerman  * schedule and assigns them to available host channels. Called from the HCD
2899197ba5f4SPaul Zimmerman  * interrupt handler functions.
2900197ba5f4SPaul Zimmerman  *
2901197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2902197ba5f4SPaul Zimmerman  *
2903197ba5f4SPaul Zimmerman  * Return: The types of new transactions that were assigned to host channels
2904197ba5f4SPaul Zimmerman  */
2905197ba5f4SPaul Zimmerman enum dwc2_transaction_type dwc2_hcd_select_transactions(
2906197ba5f4SPaul Zimmerman 		struct dwc2_hsotg *hsotg)
2907197ba5f4SPaul Zimmerman {
2908197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2909197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2910197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2911197ba5f4SPaul Zimmerman 	int num_channels;
2912197ba5f4SPaul Zimmerman 
2913197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
2914197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Select Transactions\n");
2915197ba5f4SPaul Zimmerman #endif
2916197ba5f4SPaul Zimmerman 
2917197ba5f4SPaul Zimmerman 	/* Process entries in the periodic ready list */
2918197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_ready.next;
2919197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_ready) {
2920197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2921197ba5f4SPaul Zimmerman 			break;
292295832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2923197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels <= 1)
2924197ba5f4SPaul Zimmerman 				break;
2925197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2926197ba5f4SPaul Zimmerman 		}
2927197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2928197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2929197ba5f4SPaul Zimmerman 			break;
2930197ba5f4SPaul Zimmerman 
2931197ba5f4SPaul Zimmerman 		/*
2932197ba5f4SPaul Zimmerman 		 * Move the QH from the periodic ready schedule to the
2933197ba5f4SPaul Zimmerman 		 * periodic assigned schedule
2934197ba5f4SPaul Zimmerman 		 */
2935197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
293694ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
293794ef7aeeSDouglas Anderson 			       &hsotg->periodic_sched_assigned);
2938197ba5f4SPaul Zimmerman 		ret_val = DWC2_TRANSACTION_PERIODIC;
2939197ba5f4SPaul Zimmerman 	}
2940197ba5f4SPaul Zimmerman 
2941197ba5f4SPaul Zimmerman 	/*
2942197ba5f4SPaul Zimmerman 	 * Process entries in the inactive portion of the non-periodic
2943197ba5f4SPaul Zimmerman 	 * schedule. Some free host channels may not be used if they are
2944197ba5f4SPaul Zimmerman 	 * reserved for periodic transfers.
2945197ba5f4SPaul Zimmerman 	 */
2946bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2947197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->non_periodic_sched_inactive.next;
2948197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
294995832c00SJohn Youn 		if (!hsotg->params.uframe_sched &&
2950197ba5f4SPaul Zimmerman 		    hsotg->non_periodic_channels >= num_channels -
2951197ba5f4SPaul Zimmerman 						hsotg->periodic_channels)
2952197ba5f4SPaul Zimmerman 			break;
2953197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2954197ba5f4SPaul Zimmerman 			break;
2955197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
295695832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2957197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels < 1)
2958197ba5f4SPaul Zimmerman 				break;
2959197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2960197ba5f4SPaul Zimmerman 		}
2961197ba5f4SPaul Zimmerman 
2962197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2963197ba5f4SPaul Zimmerman 			break;
2964197ba5f4SPaul Zimmerman 
2965197ba5f4SPaul Zimmerman 		/*
2966197ba5f4SPaul Zimmerman 		 * Move the QH from the non-periodic inactive schedule to the
2967197ba5f4SPaul Zimmerman 		 * non-periodic active schedule
2968197ba5f4SPaul Zimmerman 		 */
2969197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
297094ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
2971197ba5f4SPaul Zimmerman 			       &hsotg->non_periodic_sched_active);
2972197ba5f4SPaul Zimmerman 
2973197ba5f4SPaul Zimmerman 		if (ret_val == DWC2_TRANSACTION_NONE)
2974197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2975197ba5f4SPaul Zimmerman 		else
2976197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_ALL;
2977197ba5f4SPaul Zimmerman 
297895832c00SJohn Youn 		if (!hsotg->params.uframe_sched)
2979197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels++;
2980197ba5f4SPaul Zimmerman 	}
2981197ba5f4SPaul Zimmerman 
2982197ba5f4SPaul Zimmerman 	return ret_val;
2983197ba5f4SPaul Zimmerman }
2984197ba5f4SPaul Zimmerman 
2985197ba5f4SPaul Zimmerman /**
2986197ba5f4SPaul Zimmerman  * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2987197ba5f4SPaul Zimmerman  * a host channel associated with either a periodic or non-periodic transfer
2988197ba5f4SPaul Zimmerman  *
2989197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2990197ba5f4SPaul Zimmerman  * @chan:  Host channel descriptor associated with either a periodic or
2991197ba5f4SPaul Zimmerman  *         non-periodic transfer
2992197ba5f4SPaul Zimmerman  * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2993197ba5f4SPaul Zimmerman  *                     for periodic transfers or the non-periodic Tx FIFO
2994197ba5f4SPaul Zimmerman  *                     for non-periodic transfers
2995197ba5f4SPaul Zimmerman  *
2996197ba5f4SPaul Zimmerman  * Return: 1 if a request is queued and more requests may be needed to
2997197ba5f4SPaul Zimmerman  * complete the transfer, 0 if no more requests are required for this
2998197ba5f4SPaul Zimmerman  * transfer, -1 if there is insufficient space in the Tx FIFO
2999197ba5f4SPaul Zimmerman  *
3000197ba5f4SPaul Zimmerman  * This function assumes that there is space available in the appropriate
3001197ba5f4SPaul Zimmerman  * request queue. For an OUT transfer or SETUP transaction in Slave mode,
3002197ba5f4SPaul Zimmerman  * it checks whether space is available in the appropriate Tx FIFO.
3003197ba5f4SPaul Zimmerman  *
3004197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3005197ba5f4SPaul Zimmerman  */
3006197ba5f4SPaul Zimmerman static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
3007197ba5f4SPaul Zimmerman 				  struct dwc2_host_chan *chan,
3008197ba5f4SPaul Zimmerman 				  u16 fifo_dwords_avail)
3009197ba5f4SPaul Zimmerman {
3010197ba5f4SPaul Zimmerman 	int retval = 0;
3011197ba5f4SPaul Zimmerman 
3012c9c8ac01SDouglas Anderson 	if (chan->do_split)
3013c9c8ac01SDouglas Anderson 		/* Put ourselves on the list to keep order straight */
3014c9c8ac01SDouglas Anderson 		list_move_tail(&chan->split_order_list_entry,
3015c9c8ac01SDouglas Anderson 			       &hsotg->split_order);
3016c9c8ac01SDouglas Anderson 
301795832c00SJohn Youn 	if (hsotg->params.host_dma) {
301895832c00SJohn Youn 		if (hsotg->params.dma_desc_enable) {
3019197ba5f4SPaul Zimmerman 			if (!chan->xfer_started ||
3020197ba5f4SPaul Zimmerman 			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
3021197ba5f4SPaul Zimmerman 				dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
3022197ba5f4SPaul Zimmerman 				chan->qh->ping_state = 0;
3023197ba5f4SPaul Zimmerman 			}
3024197ba5f4SPaul Zimmerman 		} else if (!chan->xfer_started) {
3025197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
3026197ba5f4SPaul Zimmerman 			chan->qh->ping_state = 0;
3027197ba5f4SPaul Zimmerman 		}
3028197ba5f4SPaul Zimmerman 	} else if (chan->halt_pending) {
3029197ba5f4SPaul Zimmerman 		/* Don't queue a request if the channel has been halted */
3030197ba5f4SPaul Zimmerman 	} else if (chan->halt_on_queue) {
3031197ba5f4SPaul Zimmerman 		dwc2_hc_halt(hsotg, chan, chan->halt_status);
3032197ba5f4SPaul Zimmerman 	} else if (chan->do_ping) {
3033197ba5f4SPaul Zimmerman 		if (!chan->xfer_started)
3034197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
3035197ba5f4SPaul Zimmerman 	} else if (!chan->ep_is_in ||
3036197ba5f4SPaul Zimmerman 		   chan->data_pid_start == DWC2_HC_PID_SETUP) {
3037197ba5f4SPaul Zimmerman 		if ((fifo_dwords_avail * 4) >= chan->max_packet) {
3038197ba5f4SPaul Zimmerman 			if (!chan->xfer_started) {
3039197ba5f4SPaul Zimmerman 				dwc2_hc_start_transfer(hsotg, chan);
3040197ba5f4SPaul Zimmerman 				retval = 1;
3041197ba5f4SPaul Zimmerman 			} else {
3042197ba5f4SPaul Zimmerman 				retval = dwc2_hc_continue_transfer(hsotg, chan);
3043197ba5f4SPaul Zimmerman 			}
3044197ba5f4SPaul Zimmerman 		} else {
3045197ba5f4SPaul Zimmerman 			retval = -1;
3046197ba5f4SPaul Zimmerman 		}
3047197ba5f4SPaul Zimmerman 	} else {
3048197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
3049197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
3050197ba5f4SPaul Zimmerman 			retval = 1;
3051197ba5f4SPaul Zimmerman 		} else {
3052197ba5f4SPaul Zimmerman 			retval = dwc2_hc_continue_transfer(hsotg, chan);
3053197ba5f4SPaul Zimmerman 		}
3054197ba5f4SPaul Zimmerman 	}
3055197ba5f4SPaul Zimmerman 
3056197ba5f4SPaul Zimmerman 	return retval;
3057197ba5f4SPaul Zimmerman }
3058197ba5f4SPaul Zimmerman 
3059197ba5f4SPaul Zimmerman /*
3060197ba5f4SPaul Zimmerman  * Processes periodic channels for the next frame and queues transactions for
3061197ba5f4SPaul Zimmerman  * these channels to the DWC_otg controller. After queueing transactions, the
3062197ba5f4SPaul Zimmerman  * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
3063197ba5f4SPaul Zimmerman  * to queue as Periodic Tx FIFO or request queue space becomes available.
3064197ba5f4SPaul Zimmerman  * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
3065197ba5f4SPaul Zimmerman  *
3066197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3067197ba5f4SPaul Zimmerman  */
3068197ba5f4SPaul Zimmerman static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
3069197ba5f4SPaul Zimmerman {
3070197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
3071197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
3072197ba5f4SPaul Zimmerman 	u32 tx_status;
3073197ba5f4SPaul Zimmerman 	u32 fspcavail;
3074197ba5f4SPaul Zimmerman 	u32 gintmsk;
3075197ba5f4SPaul Zimmerman 	int status;
30764e50e011SDouglas Anderson 	bool no_queue_space = false;
30774e50e011SDouglas Anderson 	bool no_fifo_space = false;
3078197ba5f4SPaul Zimmerman 	u32 qspcavail;
3079197ba5f4SPaul Zimmerman 
30804e50e011SDouglas Anderson 	/* If empty list then just adjust interrupt enables */
30814e50e011SDouglas Anderson 	if (list_empty(&hsotg->periodic_sched_assigned))
30824e50e011SDouglas Anderson 		goto exit;
30834e50e011SDouglas Anderson 
3084197ba5f4SPaul Zimmerman 	if (dbg_perio())
3085197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
3086197ba5f4SPaul Zimmerman 
308795c8bc36SAntti Seppälä 	tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
3088197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3089197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
3090197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3091197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
3092197ba5f4SPaul Zimmerman 
3093197ba5f4SPaul Zimmerman 	if (dbg_perio()) {
3094197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx Req Queue Space Avail (before queue): %d\n",
3095197ba5f4SPaul Zimmerman 			 qspcavail);
3096197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx FIFO Space Avail (before queue): %d\n",
3097197ba5f4SPaul Zimmerman 			 fspcavail);
3098197ba5f4SPaul Zimmerman 	}
3099197ba5f4SPaul Zimmerman 
3100197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_assigned.next;
3101197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_assigned) {
310295c8bc36SAntti Seppälä 		tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
3103197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3104197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
3105197ba5f4SPaul Zimmerman 		if (qspcavail == 0) {
3106fdb09b3eSNicholas Mc Guire 			no_queue_space = true;
3107197ba5f4SPaul Zimmerman 			break;
3108197ba5f4SPaul Zimmerman 		}
3109197ba5f4SPaul Zimmerman 
3110197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
3111197ba5f4SPaul Zimmerman 		if (!qh->channel) {
3112197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
3113197ba5f4SPaul Zimmerman 			continue;
3114197ba5f4SPaul Zimmerman 		}
3115197ba5f4SPaul Zimmerman 
3116197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
3117197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty) {
3118197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
3119197ba5f4SPaul Zimmerman 			continue;
3120197ba5f4SPaul Zimmerman 		}
3121197ba5f4SPaul Zimmerman 
3122197ba5f4SPaul Zimmerman 		/*
3123197ba5f4SPaul Zimmerman 		 * Set a flag if we're queuing high-bandwidth in slave mode.
3124197ba5f4SPaul Zimmerman 		 * The flag prevents any halts to get into the request queue in
3125197ba5f4SPaul Zimmerman 		 * the middle of multiple high-bandwidth packets getting queued.
3126197ba5f4SPaul Zimmerman 		 */
312795832c00SJohn Youn 		if (!hsotg->params.host_dma &&
3128197ba5f4SPaul Zimmerman 		    qh->channel->multi_count > 1)
3129197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 1;
3130197ba5f4SPaul Zimmerman 
3131197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3132197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3133197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3134197ba5f4SPaul Zimmerman 		if (status < 0) {
3135fdb09b3eSNicholas Mc Guire 			no_fifo_space = true;
3136197ba5f4SPaul Zimmerman 			break;
3137197ba5f4SPaul Zimmerman 		}
3138197ba5f4SPaul Zimmerman 
3139197ba5f4SPaul Zimmerman 		/*
3140197ba5f4SPaul Zimmerman 		 * In Slave mode, stay on the current transfer until there is
3141197ba5f4SPaul Zimmerman 		 * nothing more to do or the high-bandwidth request count is
3142197ba5f4SPaul Zimmerman 		 * reached. In DMA mode, only need to queue one request. The
3143197ba5f4SPaul Zimmerman 		 * controller automatically handles multiple packets for
3144197ba5f4SPaul Zimmerman 		 * high-bandwidth transfers.
3145197ba5f4SPaul Zimmerman 		 */
314695832c00SJohn Youn 		if (hsotg->params.host_dma || status == 0 ||
3147197ba5f4SPaul Zimmerman 		    qh->channel->requests == qh->channel->multi_count) {
3148197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
3149197ba5f4SPaul Zimmerman 			/*
3150197ba5f4SPaul Zimmerman 			 * Move the QH from the periodic assigned schedule to
3151197ba5f4SPaul Zimmerman 			 * the periodic queued schedule
3152197ba5f4SPaul Zimmerman 			 */
315394ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
3154197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_queued);
3155197ba5f4SPaul Zimmerman 
3156197ba5f4SPaul Zimmerman 			/* done queuing high bandwidth */
3157197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 0;
3158197ba5f4SPaul Zimmerman 		}
3159197ba5f4SPaul Zimmerman 	}
3160197ba5f4SPaul Zimmerman 
31614e50e011SDouglas Anderson exit:
31624e50e011SDouglas Anderson 	if (no_queue_space || no_fifo_space ||
316395832c00SJohn Youn 	    (!hsotg->params.host_dma &&
31644e50e011SDouglas Anderson 	     !list_empty(&hsotg->periodic_sched_assigned))) {
3165197ba5f4SPaul Zimmerman 		/*
3166197ba5f4SPaul Zimmerman 		 * May need to queue more transactions as the request
3167197ba5f4SPaul Zimmerman 		 * queue or Tx FIFO empties. Enable the periodic Tx
3168197ba5f4SPaul Zimmerman 		 * FIFO empty interrupt. (Always use the half-empty
3169197ba5f4SPaul Zimmerman 		 * level to ensure that new requests are loaded as
3170197ba5f4SPaul Zimmerman 		 * soon as possible.)
3171197ba5f4SPaul Zimmerman 		 */
317295c8bc36SAntti Seppälä 		gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
31734e50e011SDouglas Anderson 		if (!(gintmsk & GINTSTS_PTXFEMP)) {
3174197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_PTXFEMP;
317595c8bc36SAntti Seppälä 			dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
31764e50e011SDouglas Anderson 		}
3177197ba5f4SPaul Zimmerman 	} else {
3178197ba5f4SPaul Zimmerman 		/*
3179197ba5f4SPaul Zimmerman 		 * Disable the Tx FIFO empty interrupt since there are
3180197ba5f4SPaul Zimmerman 		 * no more transactions that need to be queued right
3181197ba5f4SPaul Zimmerman 		 * now. This function is called from interrupt
3182197ba5f4SPaul Zimmerman 		 * handlers to queue more transactions as transfer
3183197ba5f4SPaul Zimmerman 		 * states change.
3184197ba5f4SPaul Zimmerman 		 */
318595c8bc36SAntti Seppälä 		gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
31864e50e011SDouglas Anderson 		if (gintmsk & GINTSTS_PTXFEMP) {
3187197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_PTXFEMP;
318895c8bc36SAntti Seppälä 			dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3189197ba5f4SPaul Zimmerman 		}
3190197ba5f4SPaul Zimmerman 	}
3191197ba5f4SPaul Zimmerman }
3192197ba5f4SPaul Zimmerman 
3193197ba5f4SPaul Zimmerman /*
3194197ba5f4SPaul Zimmerman  * Processes active non-periodic channels and queues transactions for these
3195197ba5f4SPaul Zimmerman  * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3196197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is enabled if there are more transactions to queue as
3197197ba5f4SPaul Zimmerman  * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3198197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is disabled.
3199197ba5f4SPaul Zimmerman  *
3200197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3201197ba5f4SPaul Zimmerman  */
3202197ba5f4SPaul Zimmerman static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3203197ba5f4SPaul Zimmerman {
3204197ba5f4SPaul Zimmerman 	struct list_head *orig_qh_ptr;
3205197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
3206197ba5f4SPaul Zimmerman 	u32 tx_status;
3207197ba5f4SPaul Zimmerman 	u32 qspcavail;
3208197ba5f4SPaul Zimmerman 	u32 fspcavail;
3209197ba5f4SPaul Zimmerman 	u32 gintmsk;
3210197ba5f4SPaul Zimmerman 	int status;
3211197ba5f4SPaul Zimmerman 	int no_queue_space = 0;
3212197ba5f4SPaul Zimmerman 	int no_fifo_space = 0;
3213197ba5f4SPaul Zimmerman 	int more_to_do = 0;
3214197ba5f4SPaul Zimmerman 
3215197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3216197ba5f4SPaul Zimmerman 
321795c8bc36SAntti Seppälä 	tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
3218197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3219197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
3220197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3221197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
3222197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx Req Queue Space Avail (before queue): %d\n",
3223197ba5f4SPaul Zimmerman 		 qspcavail);
3224197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx FIFO Space Avail (before queue): %d\n",
3225197ba5f4SPaul Zimmerman 		 fspcavail);
3226197ba5f4SPaul Zimmerman 
3227197ba5f4SPaul Zimmerman 	/*
3228197ba5f4SPaul Zimmerman 	 * Keep track of the starting point. Skip over the start-of-list
3229197ba5f4SPaul Zimmerman 	 * entry.
3230197ba5f4SPaul Zimmerman 	 */
3231197ba5f4SPaul Zimmerman 	if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3232197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3233197ba5f4SPaul Zimmerman 	orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3234197ba5f4SPaul Zimmerman 
3235197ba5f4SPaul Zimmerman 	/*
3236197ba5f4SPaul Zimmerman 	 * Process once through the active list or until no more space is
3237197ba5f4SPaul Zimmerman 	 * available in the request queue or the Tx FIFO
3238197ba5f4SPaul Zimmerman 	 */
3239197ba5f4SPaul Zimmerman 	do {
324095c8bc36SAntti Seppälä 		tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
3241197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3242197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
324395832c00SJohn Youn 		if (!hsotg->params.host_dma && qspcavail == 0) {
3244197ba5f4SPaul Zimmerman 			no_queue_space = 1;
3245197ba5f4SPaul Zimmerman 			break;
3246197ba5f4SPaul Zimmerman 		}
3247197ba5f4SPaul Zimmerman 
3248197ba5f4SPaul Zimmerman 		qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3249197ba5f4SPaul Zimmerman 				qh_list_entry);
3250197ba5f4SPaul Zimmerman 		if (!qh->channel)
3251197ba5f4SPaul Zimmerman 			goto next;
3252197ba5f4SPaul Zimmerman 
3253197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
3254197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty)
3255197ba5f4SPaul Zimmerman 			goto next;
3256197ba5f4SPaul Zimmerman 
3257197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3258197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3259197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3260197ba5f4SPaul Zimmerman 
3261197ba5f4SPaul Zimmerman 		if (status > 0) {
3262197ba5f4SPaul Zimmerman 			more_to_do = 1;
3263197ba5f4SPaul Zimmerman 		} else if (status < 0) {
3264197ba5f4SPaul Zimmerman 			no_fifo_space = 1;
3265197ba5f4SPaul Zimmerman 			break;
3266197ba5f4SPaul Zimmerman 		}
3267197ba5f4SPaul Zimmerman next:
3268197ba5f4SPaul Zimmerman 		/* Advance to next QH, skipping start-of-list entry */
3269197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3270197ba5f4SPaul Zimmerman 		if (hsotg->non_periodic_qh_ptr ==
3271197ba5f4SPaul Zimmerman 				&hsotg->non_periodic_sched_active)
3272197ba5f4SPaul Zimmerman 			hsotg->non_periodic_qh_ptr =
3273197ba5f4SPaul Zimmerman 					hsotg->non_periodic_qh_ptr->next;
3274197ba5f4SPaul Zimmerman 	} while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3275197ba5f4SPaul Zimmerman 
327695832c00SJohn Youn 	if (!hsotg->params.host_dma) {
327795c8bc36SAntti Seppälä 		tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
3278197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3279197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
3280197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3281197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3282197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3283197ba5f4SPaul Zimmerman 			 "  NP Tx Req Queue Space Avail (after queue): %d\n",
3284197ba5f4SPaul Zimmerman 			 qspcavail);
3285197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3286197ba5f4SPaul Zimmerman 			 "  NP Tx FIFO Space Avail (after queue): %d\n",
3287197ba5f4SPaul Zimmerman 			 fspcavail);
3288197ba5f4SPaul Zimmerman 
3289197ba5f4SPaul Zimmerman 		if (more_to_do || no_queue_space || no_fifo_space) {
3290197ba5f4SPaul Zimmerman 			/*
3291197ba5f4SPaul Zimmerman 			 * May need to queue more transactions as the request
3292197ba5f4SPaul Zimmerman 			 * queue or Tx FIFO empties. Enable the non-periodic
3293197ba5f4SPaul Zimmerman 			 * Tx FIFO empty interrupt. (Always use the half-empty
3294197ba5f4SPaul Zimmerman 			 * level to ensure that new requests are loaded as
3295197ba5f4SPaul Zimmerman 			 * soon as possible.)
3296197ba5f4SPaul Zimmerman 			 */
329795c8bc36SAntti Seppälä 			gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3298197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_NPTXFEMP;
329995c8bc36SAntti Seppälä 			dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3300197ba5f4SPaul Zimmerman 		} else {
3301197ba5f4SPaul Zimmerman 			/*
3302197ba5f4SPaul Zimmerman 			 * Disable the Tx FIFO empty interrupt since there are
3303197ba5f4SPaul Zimmerman 			 * no more transactions that need to be queued right
3304197ba5f4SPaul Zimmerman 			 * now. This function is called from interrupt
3305197ba5f4SPaul Zimmerman 			 * handlers to queue more transactions as transfer
3306197ba5f4SPaul Zimmerman 			 * states change.
3307197ba5f4SPaul Zimmerman 			 */
330895c8bc36SAntti Seppälä 			gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3309197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
331095c8bc36SAntti Seppälä 			dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3311197ba5f4SPaul Zimmerman 		}
3312197ba5f4SPaul Zimmerman 	}
3313197ba5f4SPaul Zimmerman }
3314197ba5f4SPaul Zimmerman 
3315197ba5f4SPaul Zimmerman /**
3316197ba5f4SPaul Zimmerman  * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3317197ba5f4SPaul Zimmerman  * and queues transactions for these channels to the DWC_otg controller. Called
3318197ba5f4SPaul Zimmerman  * from the HCD interrupt handler functions.
3319197ba5f4SPaul Zimmerman  *
3320197ba5f4SPaul Zimmerman  * @hsotg:   The HCD state structure
3321197ba5f4SPaul Zimmerman  * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3322197ba5f4SPaul Zimmerman  *           or both)
3323197ba5f4SPaul Zimmerman  *
3324197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3325197ba5f4SPaul Zimmerman  */
3326197ba5f4SPaul Zimmerman void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3327197ba5f4SPaul Zimmerman 				 enum dwc2_transaction_type tr_type)
3328197ba5f4SPaul Zimmerman {
3329197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3330197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue Transactions\n");
3331197ba5f4SPaul Zimmerman #endif
3332197ba5f4SPaul Zimmerman 	/* Process host channels associated with periodic transfers */
33334e50e011SDouglas Anderson 	if (tr_type == DWC2_TRANSACTION_PERIODIC ||
33344e50e011SDouglas Anderson 	    tr_type == DWC2_TRANSACTION_ALL)
3335197ba5f4SPaul Zimmerman 		dwc2_process_periodic_channels(hsotg);
3336197ba5f4SPaul Zimmerman 
3337197ba5f4SPaul Zimmerman 	/* Process host channels associated with non-periodic transfers */
3338197ba5f4SPaul Zimmerman 	if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3339197ba5f4SPaul Zimmerman 	    tr_type == DWC2_TRANSACTION_ALL) {
3340197ba5f4SPaul Zimmerman 		if (!list_empty(&hsotg->non_periodic_sched_active)) {
3341197ba5f4SPaul Zimmerman 			dwc2_process_non_periodic_channels(hsotg);
3342197ba5f4SPaul Zimmerman 		} else {
3343197ba5f4SPaul Zimmerman 			/*
3344197ba5f4SPaul Zimmerman 			 * Ensure NP Tx FIFO empty interrupt is disabled when
3345197ba5f4SPaul Zimmerman 			 * there are no non-periodic transfers to process
3346197ba5f4SPaul Zimmerman 			 */
334795c8bc36SAntti Seppälä 			u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3348197ba5f4SPaul Zimmerman 
3349197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
335095c8bc36SAntti Seppälä 			dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3351197ba5f4SPaul Zimmerman 		}
3352197ba5f4SPaul Zimmerman 	}
3353197ba5f4SPaul Zimmerman }
3354197ba5f4SPaul Zimmerman 
3355197ba5f4SPaul Zimmerman static void dwc2_conn_id_status_change(struct work_struct *work)
3356197ba5f4SPaul Zimmerman {
3357197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3358197ba5f4SPaul Zimmerman 						wf_otg);
3359197ba5f4SPaul Zimmerman 	u32 count = 0;
3360197ba5f4SPaul Zimmerman 	u32 gotgctl;
33615390d438SMian Yousaf Kaukab 	unsigned long flags;
3362197ba5f4SPaul Zimmerman 
3363197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3364197ba5f4SPaul Zimmerman 
336595c8bc36SAntti Seppälä 	gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3366197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3367197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3368197ba5f4SPaul Zimmerman 		!!(gotgctl & GOTGCTL_CONID_B));
3369197ba5f4SPaul Zimmerman 
3370197ba5f4SPaul Zimmerman 	/* B-Device connector (Device Mode) */
3371197ba5f4SPaul Zimmerman 	if (gotgctl & GOTGCTL_CONID_B) {
3372531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
3373197ba5f4SPaul Zimmerman 		/* Wait for switch to device mode */
3374197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId B\n");
33759156a7efSChen Yu 		if (hsotg->bus_suspended) {
33769156a7efSChen Yu 			dev_info(hsotg->dev,
33779156a7efSChen Yu 				 "Do port resume before switching to device mode\n");
33789156a7efSChen Yu 			dwc2_port_resume(hsotg);
33799156a7efSChen Yu 		}
3380197ba5f4SPaul Zimmerman 		while (!dwc2_is_device_mode(hsotg)) {
3381197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev,
3382197ba5f4SPaul Zimmerman 				 "Waiting for Peripheral Mode, Mode=%s\n",
3383197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ? "Host" :
3384197ba5f4SPaul Zimmerman 				 "Peripheral");
338504a9db79SNicholas Mc Guire 			msleep(20);
3386fc30c4bbSJohn Stultz 			/*
3387fc30c4bbSJohn Stultz 			 * Sometimes the initial GOTGCTRL read is wrong, so
3388fc30c4bbSJohn Stultz 			 * check it again and jump to host mode if that was
3389fc30c4bbSJohn Stultz 			 * the case.
3390fc30c4bbSJohn Stultz 			 */
3391fc30c4bbSJohn Stultz 			gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3392fc30c4bbSJohn Stultz 			if (!(gotgctl & GOTGCTL_CONID_B))
3393fc30c4bbSJohn Stultz 				goto host;
3394197ba5f4SPaul Zimmerman 			if (++count > 250)
3395197ba5f4SPaul Zimmerman 				break;
3396197ba5f4SPaul Zimmerman 		}
3397197ba5f4SPaul Zimmerman 		if (count > 250)
3398197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3399197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3400197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
34010fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3402197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
34035390d438SMian Yousaf Kaukab 		spin_lock_irqsave(&hsotg->lock, flags);
34041f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
34055390d438SMian Yousaf Kaukab 		spin_unlock_irqrestore(&hsotg->lock, flags);
340666e77a24SRazmik Karapetyan 		/* Enable ACG feature in device mode,if supported */
340766e77a24SRazmik Karapetyan 		dwc2_enable_acg(hsotg);
34081f91b4ccSFelipe Balbi 		dwc2_hsotg_core_connect(hsotg);
3409197ba5f4SPaul Zimmerman 	} else {
3410fc30c4bbSJohn Stultz host:
3411197ba5f4SPaul Zimmerman 		/* A-Device connector (Host Mode) */
3412197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId A\n");
3413197ba5f4SPaul Zimmerman 		while (!dwc2_is_host_mode(hsotg)) {
3414197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3415197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ?
3416197ba5f4SPaul Zimmerman 				 "Host" : "Peripheral");
341704a9db79SNicholas Mc Guire 			msleep(20);
3418197ba5f4SPaul Zimmerman 			if (++count > 250)
3419197ba5f4SPaul Zimmerman 				break;
3420197ba5f4SPaul Zimmerman 		}
3421197ba5f4SPaul Zimmerman 		if (count > 250)
3422197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3423197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3424197ba5f4SPaul Zimmerman 
3425d2471d4aSJohn Stultz 		spin_lock_irqsave(&hsotg->lock, flags);
3426d2471d4aSJohn Stultz 		dwc2_hsotg_disconnect(hsotg);
3427d2471d4aSJohn Stultz 		spin_unlock_irqrestore(&hsotg->lock, flags);
3428d2471d4aSJohn Stultz 
3429d2471d4aSJohn Stultz 		hsotg->op_state = OTG_STATE_A_HOST;
3430197ba5f4SPaul Zimmerman 		/* Initialize the Core for Host mode */
34310fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3432197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
3433197ba5f4SPaul Zimmerman 		dwc2_hcd_start(hsotg);
3434197ba5f4SPaul Zimmerman 	}
3435197ba5f4SPaul Zimmerman }
3436197ba5f4SPaul Zimmerman 
3437e99e88a9SKees Cook static void dwc2_wakeup_detected(struct timer_list *t)
3438197ba5f4SPaul Zimmerman {
3439e99e88a9SKees Cook 	struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
3440197ba5f4SPaul Zimmerman 	u32 hprt0;
3441197ba5f4SPaul Zimmerman 
3442197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3443197ba5f4SPaul Zimmerman 
3444197ba5f4SPaul Zimmerman 	/*
3445197ba5f4SPaul Zimmerman 	 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3446197ba5f4SPaul Zimmerman 	 * so that OPT tests pass with all PHYs.)
3447197ba5f4SPaul Zimmerman 	 */
3448197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3449197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3450197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RES;
345195c8bc36SAntti Seppälä 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
3452197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
345395c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + HPRT0));
3454197ba5f4SPaul Zimmerman 
3455197ba5f4SPaul Zimmerman 	dwc2_hcd_rem_wakeup(hsotg);
3456fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
3457197ba5f4SPaul Zimmerman 
3458197ba5f4SPaul Zimmerman 	/* Change to L0 state */
3459197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L0;
3460197ba5f4SPaul Zimmerman }
3461197ba5f4SPaul Zimmerman 
3462197ba5f4SPaul Zimmerman static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3463197ba5f4SPaul Zimmerman {
3464197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3465197ba5f4SPaul Zimmerman 
3466197ba5f4SPaul Zimmerman 	return hcd->self.b_hnp_enable;
3467197ba5f4SPaul Zimmerman }
3468197ba5f4SPaul Zimmerman 
3469197ba5f4SPaul Zimmerman /* Must NOT be called with interrupt disabled or spinlock held */
3470197ba5f4SPaul Zimmerman static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3471197ba5f4SPaul Zimmerman {
3472197ba5f4SPaul Zimmerman 	unsigned long flags;
3473197ba5f4SPaul Zimmerman 	u32 hprt0;
3474197ba5f4SPaul Zimmerman 	u32 pcgctl;
3475197ba5f4SPaul Zimmerman 	u32 gotgctl;
3476197ba5f4SPaul Zimmerman 
3477197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3478197ba5f4SPaul Zimmerman 
3479197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
3480197ba5f4SPaul Zimmerman 
3481197ba5f4SPaul Zimmerman 	if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
348295c8bc36SAntti Seppälä 		gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3483197ba5f4SPaul Zimmerman 		gotgctl |= GOTGCTL_HSTSETHNPEN;
348495c8bc36SAntti Seppälä 		dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
3485197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_A_SUSPEND;
3486197ba5f4SPaul Zimmerman 	}
3487197ba5f4SPaul Zimmerman 
3488197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3489197ba5f4SPaul Zimmerman 	hprt0 |= HPRT0_SUSP;
349095c8bc36SAntti Seppälä 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
3491197ba5f4SPaul Zimmerman 
3492fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = true;
3493197ba5f4SPaul Zimmerman 
3494a2a23d3fSGregory Herrero 	/*
349541ba9b9bSVardan Mikayelyan 	 * If power_down is supported, Phy clock will be suspended
3496a2a23d3fSGregory Herrero 	 * after registers are backuped.
3497a2a23d3fSGregory Herrero 	 */
349841ba9b9bSVardan Mikayelyan 	if (!hsotg->params.power_down) {
3499197ba5f4SPaul Zimmerman 		/* Suspend the Phy Clock */
350095c8bc36SAntti Seppälä 		pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3501197ba5f4SPaul Zimmerman 		pcgctl |= PCGCTL_STOPPCLK;
350295c8bc36SAntti Seppälä 		dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3503197ba5f4SPaul Zimmerman 		udelay(10);
3504a2a23d3fSGregory Herrero 	}
3505197ba5f4SPaul Zimmerman 
3506197ba5f4SPaul Zimmerman 	/* For HNP the bus must be suspended for at least 200ms */
3507197ba5f4SPaul Zimmerman 	if (dwc2_host_is_b_hnp_enabled(hsotg)) {
350895c8bc36SAntti Seppälä 		pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3509197ba5f4SPaul Zimmerman 		pcgctl &= ~PCGCTL_STOPPCLK;
351095c8bc36SAntti Seppälä 		dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3511197ba5f4SPaul Zimmerman 
3512197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3513197ba5f4SPaul Zimmerman 
351404a9db79SNicholas Mc Guire 		msleep(200);
3515197ba5f4SPaul Zimmerman 	} else {
3516197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3517197ba5f4SPaul Zimmerman 	}
3518197ba5f4SPaul Zimmerman }
3519197ba5f4SPaul Zimmerman 
352030db103cSGregory Herrero /* Must NOT be called with interrupt disabled or spinlock held */
352130db103cSGregory Herrero static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
352230db103cSGregory Herrero {
352330db103cSGregory Herrero 	unsigned long flags;
352430db103cSGregory Herrero 	u32 hprt0;
352530db103cSGregory Herrero 	u32 pcgctl;
352630db103cSGregory Herrero 
35274d273c2aSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
35284d273c2aSDouglas Anderson 
3529a2a23d3fSGregory Herrero 	/*
353041ba9b9bSVardan Mikayelyan 	 * If power_down is supported, Phy clock is already resumed
3531a2a23d3fSGregory Herrero 	 * after registers restore.
3532a2a23d3fSGregory Herrero 	 */
353341ba9b9bSVardan Mikayelyan 	if (!hsotg->params.power_down) {
353430db103cSGregory Herrero 		pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
353530db103cSGregory Herrero 		pcgctl &= ~PCGCTL_STOPPCLK;
353630db103cSGregory Herrero 		dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
35374d273c2aSDouglas Anderson 		spin_unlock_irqrestore(&hsotg->lock, flags);
353804a9db79SNicholas Mc Guire 		msleep(20);
35394d273c2aSDouglas Anderson 		spin_lock_irqsave(&hsotg->lock, flags);
3540a2a23d3fSGregory Herrero 	}
354130db103cSGregory Herrero 
354230db103cSGregory Herrero 	hprt0 = dwc2_read_hprt0(hsotg);
354330db103cSGregory Herrero 	hprt0 |= HPRT0_RES;
354430db103cSGregory Herrero 	hprt0 &= ~HPRT0_SUSP;
354530db103cSGregory Herrero 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
354630db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
354730db103cSGregory Herrero 
354830db103cSGregory Herrero 	msleep(USB_RESUME_TIMEOUT);
354930db103cSGregory Herrero 
355030db103cSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
355130db103cSGregory Herrero 	hprt0 = dwc2_read_hprt0(hsotg);
355230db103cSGregory Herrero 	hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
355330db103cSGregory Herrero 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
3554fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
355530db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
355630db103cSGregory Herrero }
355730db103cSGregory Herrero 
3558197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
3559197ba5f4SPaul Zimmerman static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3560197ba5f4SPaul Zimmerman 				u16 wvalue, u16 windex, char *buf, u16 wlength)
3561197ba5f4SPaul Zimmerman {
3562197ba5f4SPaul Zimmerman 	struct usb_hub_descriptor *hub_desc;
3563197ba5f4SPaul Zimmerman 	int retval = 0;
3564197ba5f4SPaul Zimmerman 	u32 hprt0;
3565197ba5f4SPaul Zimmerman 	u32 port_status;
3566197ba5f4SPaul Zimmerman 	u32 speed;
3567197ba5f4SPaul Zimmerman 	u32 pcgctl;
3568197ba5f4SPaul Zimmerman 
3569197ba5f4SPaul Zimmerman 	switch (typereq) {
3570197ba5f4SPaul Zimmerman 	case ClearHubFeature:
3571197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3572197ba5f4SPaul Zimmerman 
3573197ba5f4SPaul Zimmerman 		switch (wvalue) {
3574197ba5f4SPaul Zimmerman 		case C_HUB_LOCAL_POWER:
3575197ba5f4SPaul Zimmerman 		case C_HUB_OVER_CURRENT:
3576197ba5f4SPaul Zimmerman 			/* Nothing required here */
3577197ba5f4SPaul Zimmerman 			break;
3578197ba5f4SPaul Zimmerman 
3579197ba5f4SPaul Zimmerman 		default:
3580197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3581197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3582197ba5f4SPaul Zimmerman 				"ClearHubFeature request %1xh unknown\n",
3583197ba5f4SPaul Zimmerman 				wvalue);
3584197ba5f4SPaul Zimmerman 		}
3585197ba5f4SPaul Zimmerman 		break;
3586197ba5f4SPaul Zimmerman 
3587197ba5f4SPaul Zimmerman 	case ClearPortFeature:
3588197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_L1)
3589197ba5f4SPaul Zimmerman 			if (!windex || windex > 1)
3590197ba5f4SPaul Zimmerman 				goto error;
3591197ba5f4SPaul Zimmerman 		switch (wvalue) {
3592197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_ENABLE:
3593197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3594197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3595197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3596197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_ENA;
359795c8bc36SAntti Seppälä 			dwc2_writel(hprt0, hsotg->regs + HPRT0);
3598197ba5f4SPaul Zimmerman 			break;
3599197ba5f4SPaul Zimmerman 
3600197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3601197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3602197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
3603b0bb9bb6SPaul Zimmerman 
3604f260b250SVardan Mikayelyan 			if (hsotg->bus_suspended) {
3605f260b250SVardan Mikayelyan 				if (hsotg->hibernated)
3606f260b250SVardan Mikayelyan 					dwc2_exit_hibernation(hsotg, 0, 0, 1);
3607f260b250SVardan Mikayelyan 				else
360830db103cSGregory Herrero 					dwc2_port_resume(hsotg);
3609f260b250SVardan Mikayelyan 			}
3610197ba5f4SPaul Zimmerman 			break;
3611197ba5f4SPaul Zimmerman 
3612197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3613197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3614197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_POWER\n");
3615197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3616197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_PWR;
361795c8bc36SAntti Seppälä 			dwc2_writel(hprt0, hsotg->regs + HPRT0);
3618197ba5f4SPaul Zimmerman 			break;
3619197ba5f4SPaul Zimmerman 
3620197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3621197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3622197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3623197ba5f4SPaul Zimmerman 			/* Port indicator not supported */
3624197ba5f4SPaul Zimmerman 			break;
3625197ba5f4SPaul Zimmerman 
3626197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_CONNECTION:
3627197ba5f4SPaul Zimmerman 			/*
3628197ba5f4SPaul Zimmerman 			 * Clears driver's internal Connect Status Change flag
3629197ba5f4SPaul Zimmerman 			 */
3630197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3631197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3632197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change = 0;
3633197ba5f4SPaul Zimmerman 			break;
3634197ba5f4SPaul Zimmerman 
3635197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_RESET:
3636197ba5f4SPaul Zimmerman 			/* Clears driver's internal Port Reset Change flag */
3637197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3638197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3639197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change = 0;
3640197ba5f4SPaul Zimmerman 			break;
3641197ba5f4SPaul Zimmerman 
3642197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_ENABLE:
3643197ba5f4SPaul Zimmerman 			/*
3644197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Enable/Disable
3645197ba5f4SPaul Zimmerman 			 * Change flag
3646197ba5f4SPaul Zimmerman 			 */
3647197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3648197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3649197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change = 0;
3650197ba5f4SPaul Zimmerman 			break;
3651197ba5f4SPaul Zimmerman 
3652197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_SUSPEND:
3653197ba5f4SPaul Zimmerman 			/*
3654197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Suspend Change
3655197ba5f4SPaul Zimmerman 			 * flag, which is set when resume signaling on the host
3656197ba5f4SPaul Zimmerman 			 * port is complete
3657197ba5f4SPaul Zimmerman 			 */
3658197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3659197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3660197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change = 0;
3661197ba5f4SPaul Zimmerman 			break;
3662197ba5f4SPaul Zimmerman 
3663197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_PORT_L1:
3664197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3665197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3666197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_l1_change = 0;
3667197ba5f4SPaul Zimmerman 			break;
3668197ba5f4SPaul Zimmerman 
3669197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_OVER_CURRENT:
3670197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3671197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3672197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change = 0;
3673197ba5f4SPaul Zimmerman 			break;
3674197ba5f4SPaul Zimmerman 
3675197ba5f4SPaul Zimmerman 		default:
3676197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3677197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3678197ba5f4SPaul Zimmerman 				"ClearPortFeature request %1xh unknown or unsupported\n",
3679197ba5f4SPaul Zimmerman 				wvalue);
3680197ba5f4SPaul Zimmerman 		}
3681197ba5f4SPaul Zimmerman 		break;
3682197ba5f4SPaul Zimmerman 
3683197ba5f4SPaul Zimmerman 	case GetHubDescriptor:
3684197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3685197ba5f4SPaul Zimmerman 		hub_desc = (struct usb_hub_descriptor *)buf;
3686197ba5f4SPaul Zimmerman 		hub_desc->bDescLength = 9;
3687a5dd0395SSergei Shtylyov 		hub_desc->bDescriptorType = USB_DT_HUB;
3688197ba5f4SPaul Zimmerman 		hub_desc->bNbrPorts = 1;
36893d040de8SSergei Shtylyov 		hub_desc->wHubCharacteristics =
36903d040de8SSergei Shtylyov 			cpu_to_le16(HUB_CHAR_COMMON_LPSM |
36913d040de8SSergei Shtylyov 				    HUB_CHAR_INDV_PORT_OCPM);
3692197ba5f4SPaul Zimmerman 		hub_desc->bPwrOn2PwrGood = 1;
3693197ba5f4SPaul Zimmerman 		hub_desc->bHubContrCurrent = 0;
3694197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[0] = 0;
3695197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3696197ba5f4SPaul Zimmerman 		break;
3697197ba5f4SPaul Zimmerman 
3698197ba5f4SPaul Zimmerman 	case GetHubStatus:
3699197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubStatus\n");
3700197ba5f4SPaul Zimmerman 		memset(buf, 0, 4);
3701197ba5f4SPaul Zimmerman 		break;
3702197ba5f4SPaul Zimmerman 
3703197ba5f4SPaul Zimmerman 	case GetPortStatus:
3704197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3705197ba5f4SPaul Zimmerman 			 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3706197ba5f4SPaul Zimmerman 			 hsotg->flags.d32);
3707197ba5f4SPaul Zimmerman 		if (!windex || windex > 1)
3708197ba5f4SPaul Zimmerman 			goto error;
3709197ba5f4SPaul Zimmerman 
3710197ba5f4SPaul Zimmerman 		port_status = 0;
3711197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status_change)
3712197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3713197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_enable_change)
3714197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_ENABLE << 16;
3715197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_suspend_change)
3716197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3717197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_l1_change)
3718197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_L1 << 16;
3719197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_reset_change)
3720197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_RESET << 16;
3721197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_over_current_change) {
3722197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "Overcurrent change detected\n");
3723197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3724197ba5f4SPaul Zimmerman 		}
3725197ba5f4SPaul Zimmerman 
3726197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3727197ba5f4SPaul Zimmerman 			/*
3728197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3729197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3730197ba5f4SPaul Zimmerman 			 * return 0's for the remainder of the port status
3731197ba5f4SPaul Zimmerman 			 * since the port register can't be read if the core
3732197ba5f4SPaul Zimmerman 			 * is in device mode.
3733197ba5f4SPaul Zimmerman 			 */
3734197ba5f4SPaul Zimmerman 			*(__le32 *)buf = cpu_to_le32(port_status);
3735197ba5f4SPaul Zimmerman 			break;
3736197ba5f4SPaul Zimmerman 		}
3737197ba5f4SPaul Zimmerman 
373895c8bc36SAntti Seppälä 		hprt0 = dwc2_readl(hsotg->regs + HPRT0);
3739197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  HPRT0: 0x%08x\n", hprt0);
3740197ba5f4SPaul Zimmerman 
3741197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_CONNSTS)
3742197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_CONNECTION;
3743197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_ENA)
3744197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_ENABLE;
3745197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_SUSP)
3746197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_SUSPEND;
3747197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_OVRCURRACT)
3748197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_OVERCURRENT;
3749197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_RST)
3750197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_RESET;
3751197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_PWR)
3752197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_POWER;
3753197ba5f4SPaul Zimmerman 
3754197ba5f4SPaul Zimmerman 		speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
3755197ba5f4SPaul Zimmerman 		if (speed == HPRT0_SPD_HIGH_SPEED)
3756197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_HIGH_SPEED;
3757197ba5f4SPaul Zimmerman 		else if (speed == HPRT0_SPD_LOW_SPEED)
3758197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_LOW_SPEED;
3759197ba5f4SPaul Zimmerman 
3760197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_TSTCTL_MASK)
3761197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_TEST;
3762197ba5f4SPaul Zimmerman 		/* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3763197ba5f4SPaul Zimmerman 
3764bea8e86cSJohn Youn 		if (hsotg->params.dma_desc_fs_enable) {
3765fbb9e22bSMian Yousaf Kaukab 			/*
3766fbb9e22bSMian Yousaf Kaukab 			 * Enable descriptor DMA only if a full speed
3767fbb9e22bSMian Yousaf Kaukab 			 * device is connected.
3768fbb9e22bSMian Yousaf Kaukab 			 */
3769fbb9e22bSMian Yousaf Kaukab 			if (hsotg->new_connection &&
3770fbb9e22bSMian Yousaf Kaukab 			    ((port_status &
3771fbb9e22bSMian Yousaf Kaukab 			      (USB_PORT_STAT_CONNECTION |
3772fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_HIGH_SPEED |
3773fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_LOW_SPEED)) ==
3774fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_CONNECTION)) {
3775fbb9e22bSMian Yousaf Kaukab 				u32 hcfg;
3776fbb9e22bSMian Yousaf Kaukab 
3777fbb9e22bSMian Yousaf Kaukab 				dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
377895832c00SJohn Youn 				hsotg->params.dma_desc_enable = true;
3779fbb9e22bSMian Yousaf Kaukab 				hcfg = dwc2_readl(hsotg->regs + HCFG);
3780fbb9e22bSMian Yousaf Kaukab 				hcfg |= HCFG_DESCDMA;
3781fbb9e22bSMian Yousaf Kaukab 				dwc2_writel(hcfg, hsotg->regs + HCFG);
3782fbb9e22bSMian Yousaf Kaukab 				hsotg->new_connection = false;
3783fbb9e22bSMian Yousaf Kaukab 			}
3784fbb9e22bSMian Yousaf Kaukab 		}
3785fbb9e22bSMian Yousaf Kaukab 
3786197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
3787197ba5f4SPaul Zimmerman 		*(__le32 *)buf = cpu_to_le32(port_status);
3788197ba5f4SPaul Zimmerman 		break;
3789197ba5f4SPaul Zimmerman 
3790197ba5f4SPaul Zimmerman 	case SetHubFeature:
3791197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetHubFeature\n");
3792197ba5f4SPaul Zimmerman 		/* No HUB features supported */
3793197ba5f4SPaul Zimmerman 		break;
3794197ba5f4SPaul Zimmerman 
3795197ba5f4SPaul Zimmerman 	case SetPortFeature:
3796197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetPortFeature\n");
3797197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3798197ba5f4SPaul Zimmerman 			goto error;
3799197ba5f4SPaul Zimmerman 
3800197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3801197ba5f4SPaul Zimmerman 			/*
3802197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3803197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3804197ba5f4SPaul Zimmerman 			 * return without doing anything since the port
3805197ba5f4SPaul Zimmerman 			 * register can't be written if the core is in device
3806197ba5f4SPaul Zimmerman 			 * mode.
3807197ba5f4SPaul Zimmerman 			 */
3808197ba5f4SPaul Zimmerman 			break;
3809197ba5f4SPaul Zimmerman 		}
3810197ba5f4SPaul Zimmerman 
3811197ba5f4SPaul Zimmerman 		switch (wvalue) {
3812197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3813197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3814197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3815197ba5f4SPaul Zimmerman 			if (windex != hsotg->otg_port)
3816197ba5f4SPaul Zimmerman 				goto error;
3817f260b250SVardan Mikayelyan 			if (hsotg->params.power_down == 2)
3818f260b250SVardan Mikayelyan 				dwc2_enter_hibernation(hsotg, 1);
3819f260b250SVardan Mikayelyan 			else
3820197ba5f4SPaul Zimmerman 				dwc2_port_suspend(hsotg, windex);
3821197ba5f4SPaul Zimmerman 			break;
3822197ba5f4SPaul Zimmerman 
3823197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3824197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3825197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_POWER\n");
3826197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3827197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_PWR;
382895c8bc36SAntti Seppälä 			dwc2_writel(hprt0, hsotg->regs + HPRT0);
3829197ba5f4SPaul Zimmerman 			break;
3830197ba5f4SPaul Zimmerman 
3831197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_RESET:
3832f260b250SVardan Mikayelyan 			if (hsotg->params.power_down == 2 &&
3833f260b250SVardan Mikayelyan 			    hsotg->hibernated)
3834f260b250SVardan Mikayelyan 				dwc2_exit_hibernation(hsotg, 0, 1, 1);
3835197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3836197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3837197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_RESET\n");
383895c8bc36SAntti Seppälä 			pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3839197ba5f4SPaul Zimmerman 			pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
384095c8bc36SAntti Seppälä 			dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3841197ba5f4SPaul Zimmerman 			/* ??? Original driver does this */
384295c8bc36SAntti Seppälä 			dwc2_writel(0, hsotg->regs + PCGCTL);
3843197ba5f4SPaul Zimmerman 
3844197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3845197ba5f4SPaul Zimmerman 			/* Clear suspend bit if resetting from suspend state */
3846197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_SUSP;
3847197ba5f4SPaul Zimmerman 
3848197ba5f4SPaul Zimmerman 			/*
3849197ba5f4SPaul Zimmerman 			 * When B-Host the Port reset bit is set in the Start
3850197ba5f4SPaul Zimmerman 			 * HCD Callback function, so that the reset is started
3851197ba5f4SPaul Zimmerman 			 * within 1ms of the HNP success interrupt
3852197ba5f4SPaul Zimmerman 			 */
3853197ba5f4SPaul Zimmerman 			if (!dwc2_hcd_is_b_host(hsotg)) {
3854197ba5f4SPaul Zimmerman 				hprt0 |= HPRT0_PWR | HPRT0_RST;
3855197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3856197ba5f4SPaul Zimmerman 					"In host mode, hprt0=%08x\n", hprt0);
385795c8bc36SAntti Seppälä 				dwc2_writel(hprt0, hsotg->regs + HPRT0);
3858197ba5f4SPaul Zimmerman 			}
3859197ba5f4SPaul Zimmerman 
3860197ba5f4SPaul Zimmerman 			/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
386104a9db79SNicholas Mc Guire 			msleep(50);
3862197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_RST;
386395c8bc36SAntti Seppälä 			dwc2_writel(hprt0, hsotg->regs + HPRT0);
3864197ba5f4SPaul Zimmerman 			hsotg->lx_state = DWC2_L0; /* Now back to On state */
3865197ba5f4SPaul Zimmerman 			break;
3866197ba5f4SPaul Zimmerman 
3867197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3868197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3869197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3870197ba5f4SPaul Zimmerman 			/* Not supported */
3871197ba5f4SPaul Zimmerman 			break;
3872197ba5f4SPaul Zimmerman 
387396d480e6SJingwu Lin 		case USB_PORT_FEAT_TEST:
387496d480e6SJingwu Lin 			hprt0 = dwc2_read_hprt0(hsotg);
387596d480e6SJingwu Lin 			dev_dbg(hsotg->dev,
387696d480e6SJingwu Lin 				"SetPortFeature - USB_PORT_FEAT_TEST\n");
387796d480e6SJingwu Lin 			hprt0 &= ~HPRT0_TSTCTL_MASK;
387896d480e6SJingwu Lin 			hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
387995c8bc36SAntti Seppälä 			dwc2_writel(hprt0, hsotg->regs + HPRT0);
388096d480e6SJingwu Lin 			break;
388196d480e6SJingwu Lin 
3882197ba5f4SPaul Zimmerman 		default:
3883197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3884197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3885197ba5f4SPaul Zimmerman 				"SetPortFeature %1xh unknown or unsupported\n",
3886197ba5f4SPaul Zimmerman 				wvalue);
3887197ba5f4SPaul Zimmerman 			break;
3888197ba5f4SPaul Zimmerman 		}
3889197ba5f4SPaul Zimmerman 		break;
3890197ba5f4SPaul Zimmerman 
3891197ba5f4SPaul Zimmerman 	default:
3892197ba5f4SPaul Zimmerman error:
3893197ba5f4SPaul Zimmerman 		retval = -EINVAL;
3894197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3895197ba5f4SPaul Zimmerman 			"Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3896197ba5f4SPaul Zimmerman 			typereq, windex, wvalue);
3897197ba5f4SPaul Zimmerman 		break;
3898197ba5f4SPaul Zimmerman 	}
3899197ba5f4SPaul Zimmerman 
3900197ba5f4SPaul Zimmerman 	return retval;
3901197ba5f4SPaul Zimmerman }
3902197ba5f4SPaul Zimmerman 
3903197ba5f4SPaul Zimmerman static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3904197ba5f4SPaul Zimmerman {
3905197ba5f4SPaul Zimmerman 	int retval;
3906197ba5f4SPaul Zimmerman 
3907197ba5f4SPaul Zimmerman 	if (port != 1)
3908197ba5f4SPaul Zimmerman 		return -EINVAL;
3909197ba5f4SPaul Zimmerman 
3910197ba5f4SPaul Zimmerman 	retval = (hsotg->flags.b.port_connect_status_change ||
3911197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_reset_change ||
3912197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_enable_change ||
3913197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_suspend_change ||
3914197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_over_current_change);
3915197ba5f4SPaul Zimmerman 
3916197ba5f4SPaul Zimmerman 	if (retval) {
3917197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3918197ba5f4SPaul Zimmerman 			"DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3919197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_connect_status_change: %d\n",
3920197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change);
3921197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_reset_change: %d\n",
3922197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change);
3923197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_enable_change: %d\n",
3924197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change);
3925197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_suspend_change: %d\n",
3926197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change);
3927197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_over_current_change: %d\n",
3928197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change);
3929197ba5f4SPaul Zimmerman 	}
3930197ba5f4SPaul Zimmerman 
3931197ba5f4SPaul Zimmerman 	return retval;
3932197ba5f4SPaul Zimmerman }
3933197ba5f4SPaul Zimmerman 
3934197ba5f4SPaul Zimmerman int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3935197ba5f4SPaul Zimmerman {
393695c8bc36SAntti Seppälä 	u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3937197ba5f4SPaul Zimmerman 
3938197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3939197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
3940197ba5f4SPaul Zimmerman 		 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
3941197ba5f4SPaul Zimmerman #endif
3942197ba5f4SPaul Zimmerman 	return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3943197ba5f4SPaul Zimmerman }
3944197ba5f4SPaul Zimmerman 
3945fae4e826SDouglas Anderson int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3946fae4e826SDouglas Anderson {
3947fae4e826SDouglas Anderson 	u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3948fae4e826SDouglas Anderson 	u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3949fae4e826SDouglas Anderson 	u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3950fae4e826SDouglas Anderson 	unsigned int us_per_frame;
3951fae4e826SDouglas Anderson 	unsigned int frame_number;
3952fae4e826SDouglas Anderson 	unsigned int remaining;
3953fae4e826SDouglas Anderson 	unsigned int interval;
3954fae4e826SDouglas Anderson 	unsigned int phy_clks;
3955fae4e826SDouglas Anderson 
3956fae4e826SDouglas Anderson 	/* High speed has 125 us per (micro) frame; others are 1 ms per */
3957fae4e826SDouglas Anderson 	us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3958fae4e826SDouglas Anderson 
3959fae4e826SDouglas Anderson 	/* Extract fields */
3960fae4e826SDouglas Anderson 	frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3961fae4e826SDouglas Anderson 	remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3962fae4e826SDouglas Anderson 	interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3963fae4e826SDouglas Anderson 
3964fae4e826SDouglas Anderson 	/*
3965fae4e826SDouglas Anderson 	 * Number of phy clocks since the last tick of the frame number after
3966fae4e826SDouglas Anderson 	 * "us" has passed.
3967fae4e826SDouglas Anderson 	 */
3968fae4e826SDouglas Anderson 	phy_clks = (interval - remaining) +
3969fae4e826SDouglas Anderson 		   DIV_ROUND_UP(interval * us, us_per_frame);
3970fae4e826SDouglas Anderson 
3971fae4e826SDouglas Anderson 	return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3972fae4e826SDouglas Anderson }
3973fae4e826SDouglas Anderson 
3974197ba5f4SPaul Zimmerman int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3975197ba5f4SPaul Zimmerman {
3976197ba5f4SPaul Zimmerman 	return hsotg->op_state == OTG_STATE_B_HOST;
3977197ba5f4SPaul Zimmerman }
3978197ba5f4SPaul Zimmerman 
3979197ba5f4SPaul Zimmerman static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3980197ba5f4SPaul Zimmerman 					       int iso_desc_count,
3981197ba5f4SPaul Zimmerman 					       gfp_t mem_flags)
3982197ba5f4SPaul Zimmerman {
3983197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3984197ba5f4SPaul Zimmerman 	u32 size = sizeof(*urb) + iso_desc_count *
3985197ba5f4SPaul Zimmerman 		   sizeof(struct dwc2_hcd_iso_packet_desc);
3986197ba5f4SPaul Zimmerman 
3987197ba5f4SPaul Zimmerman 	urb = kzalloc(size, mem_flags);
3988197ba5f4SPaul Zimmerman 	if (urb)
3989197ba5f4SPaul Zimmerman 		urb->packet_count = iso_desc_count;
3990197ba5f4SPaul Zimmerman 	return urb;
3991197ba5f4SPaul Zimmerman }
3992197ba5f4SPaul Zimmerman 
3993197ba5f4SPaul Zimmerman static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3994197ba5f4SPaul Zimmerman 				      struct dwc2_hcd_urb *urb, u8 dev_addr,
3995197ba5f4SPaul Zimmerman 				      u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3996197ba5f4SPaul Zimmerman {
3997197ba5f4SPaul Zimmerman 	if (dbg_perio() ||
3998197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_BULK ||
3999197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_CONTROL)
4000197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
4001197ba5f4SPaul Zimmerman 			 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
4002197ba5f4SPaul Zimmerman 			 dev_addr, ep_num, ep_dir, ep_type, mps);
4003197ba5f4SPaul Zimmerman 	urb->pipe_info.dev_addr = dev_addr;
4004197ba5f4SPaul Zimmerman 	urb->pipe_info.ep_num = ep_num;
4005197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_type = ep_type;
4006197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_dir = ep_dir;
4007197ba5f4SPaul Zimmerman 	urb->pipe_info.mps = mps;
4008197ba5f4SPaul Zimmerman }
4009197ba5f4SPaul Zimmerman 
4010197ba5f4SPaul Zimmerman /*
4011197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
4012197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
4013197ba5f4SPaul Zimmerman  */
4014197ba5f4SPaul Zimmerman void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
4015197ba5f4SPaul Zimmerman {
4016197ba5f4SPaul Zimmerman #ifdef DEBUG
4017197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
4018197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
4019197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
4020197ba5f4SPaul Zimmerman 	int num_channels;
4021197ba5f4SPaul Zimmerman 	u32 np_tx_status;
4022197ba5f4SPaul Zimmerman 	u32 p_tx_status;
4023197ba5f4SPaul Zimmerman 	int i;
4024197ba5f4SPaul Zimmerman 
4025bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
4026197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
4027197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4028197ba5f4SPaul Zimmerman 		"************************************************************\n");
4029197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HCD State:\n");
4030197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Num channels: %d\n", num_channels);
4031197ba5f4SPaul Zimmerman 
4032197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
4033197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
4034197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  Channel %d:\n", i);
4035197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
4036197ba5f4SPaul Zimmerman 			"    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
4037197ba5f4SPaul Zimmerman 			chan->dev_addr, chan->ep_num, chan->ep_is_in);
4038197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    speed: %d\n", chan->speed);
4039197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
4040197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
4041197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    data_pid_start: %d\n",
4042197ba5f4SPaul Zimmerman 			chan->data_pid_start);
4043197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    multi_count: %d\n", chan->multi_count);
4044197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_started: %d\n",
4045197ba5f4SPaul Zimmerman 			chan->xfer_started);
4046197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
4047197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
4048197ba5f4SPaul Zimmerman 			(unsigned long)chan->xfer_dma);
4049197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
4050197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_count: %d\n", chan->xfer_count);
4051197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_on_queue: %d\n",
4052197ba5f4SPaul Zimmerman 			chan->halt_on_queue);
4053197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_pending: %d\n",
4054197ba5f4SPaul Zimmerman 			chan->halt_pending);
4055197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
4056197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    do_split: %d\n", chan->do_split);
4057197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    complete_split: %d\n",
4058197ba5f4SPaul Zimmerman 			chan->complete_split);
4059197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_addr: %d\n", chan->hub_addr);
4060197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_port: %d\n", chan->hub_port);
4061197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xact_pos: %d\n", chan->xact_pos);
4062197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    requests: %d\n", chan->requests);
4063197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
4064197ba5f4SPaul Zimmerman 
4065197ba5f4SPaul Zimmerman 		if (chan->xfer_started) {
4066197ba5f4SPaul Zimmerman 			u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
4067197ba5f4SPaul Zimmerman 
406895c8bc36SAntti Seppälä 			hfnum = dwc2_readl(hsotg->regs + HFNUM);
406995c8bc36SAntti Seppälä 			hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
407095c8bc36SAntti Seppälä 			hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
407195c8bc36SAntti Seppälä 			hcint = dwc2_readl(hsotg->regs + HCINT(i));
407295c8bc36SAntti Seppälä 			hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
4073197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hfnum: 0x%08x\n", hfnum);
4074197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcchar: 0x%08x\n", hcchar);
4075197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hctsiz: 0x%08x\n", hctsiz);
4076197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcint: 0x%08x\n", hcint);
4077197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcintmsk: 0x%08x\n", hcintmsk);
4078197ba5f4SPaul Zimmerman 		}
4079197ba5f4SPaul Zimmerman 
4080197ba5f4SPaul Zimmerman 		if (!(chan->xfer_started && chan->qh))
4081197ba5f4SPaul Zimmerman 			continue;
4082197ba5f4SPaul Zimmerman 
4083197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
4084197ba5f4SPaul Zimmerman 			if (!qtd->in_process)
4085197ba5f4SPaul Zimmerman 				break;
4086197ba5f4SPaul Zimmerman 			urb = qtd->urb;
4087197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    URB Info:\n");
4088197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "      qtd: %p, urb: %p\n",
4089197ba5f4SPaul Zimmerman 				qtd, urb);
4090197ba5f4SPaul Zimmerman 			if (urb) {
4091197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
4092197ba5f4SPaul Zimmerman 					"      Dev: %d, EP: %d %s\n",
4093197ba5f4SPaul Zimmerman 					dwc2_hcd_get_dev_addr(&urb->pipe_info),
4094197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_num(&urb->pipe_info),
4095197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
4096197ba5f4SPaul Zimmerman 					"IN" : "OUT");
4097197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
4098197ba5f4SPaul Zimmerman 					"      Max packet size: %d\n",
4099197ba5f4SPaul Zimmerman 					dwc2_hcd_get_mps(&urb->pipe_info));
4100197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
4101197ba5f4SPaul Zimmerman 					"      transfer_buffer: %p\n",
4102197ba5f4SPaul Zimmerman 					urb->buf);
4103197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
4104197ba5f4SPaul Zimmerman 					"      transfer_dma: %08lx\n",
4105197ba5f4SPaul Zimmerman 					(unsigned long)urb->dma);
4106197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
4107197ba5f4SPaul Zimmerman 					"      transfer_buffer_length: %d\n",
4108197ba5f4SPaul Zimmerman 					urb->length);
4109197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev, "      actual_length: %d\n",
4110197ba5f4SPaul Zimmerman 					urb->actual_length);
4111197ba5f4SPaul Zimmerman 			}
4112197ba5f4SPaul Zimmerman 		}
4113197ba5f4SPaul Zimmerman 	}
4114197ba5f4SPaul Zimmerman 
4115197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  non_periodic_channels: %d\n",
4116197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels);
4117197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_channels: %d\n",
4118197ba5f4SPaul Zimmerman 		hsotg->periodic_channels);
4119197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_usecs: %d\n", hsotg->periodic_usecs);
412095c8bc36SAntti Seppälä 	np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
4121197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx Req Queue Space Avail: %d\n",
4122197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
4123197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx FIFO Space Avail: %d\n",
4124197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
412595c8bc36SAntti Seppälä 	p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
4126197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx Req Queue Space Avail: %d\n",
4127197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
4128197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx FIFO Space Avail: %d\n",
4129197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
4130197ba5f4SPaul Zimmerman 	dwc2_dump_global_registers(hsotg);
4131197ba5f4SPaul Zimmerman 	dwc2_dump_host_registers(hsotg);
4132197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4133197ba5f4SPaul Zimmerman 		"************************************************************\n");
4134197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
4135197ba5f4SPaul Zimmerman #endif
4136197ba5f4SPaul Zimmerman }
4137197ba5f4SPaul Zimmerman 
4138197ba5f4SPaul Zimmerman struct wrapper_priv_data {
4139197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg;
4140197ba5f4SPaul Zimmerman };
4141197ba5f4SPaul Zimmerman 
4142197ba5f4SPaul Zimmerman /* Gets the dwc2_hsotg from a usb_hcd */
4143197ba5f4SPaul Zimmerman static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4144197ba5f4SPaul Zimmerman {
4145197ba5f4SPaul Zimmerman 	struct wrapper_priv_data *p;
4146197ba5f4SPaul Zimmerman 
4147197ba5f4SPaul Zimmerman 	p = (struct wrapper_priv_data *)&hcd->hcd_priv;
4148197ba5f4SPaul Zimmerman 	return p->hsotg;
4149197ba5f4SPaul Zimmerman }
4150197ba5f4SPaul Zimmerman 
41519f9f09b0SDouglas Anderson /**
41529f9f09b0SDouglas Anderson  * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
41539f9f09b0SDouglas Anderson  *
41549f9f09b0SDouglas Anderson  * This will get the dwc2_tt structure (and ttport) associated with the given
41559f9f09b0SDouglas Anderson  * context (which is really just a struct urb pointer).
41569f9f09b0SDouglas Anderson  *
41579f9f09b0SDouglas Anderson  * The first time this is called for a given TT we allocate memory for our
41589f9f09b0SDouglas Anderson  * structure.  When everyone is done and has called dwc2_host_put_tt_info()
41599f9f09b0SDouglas Anderson  * then the refcount for the structure will go to 0 and we'll free it.
41609f9f09b0SDouglas Anderson  *
41619f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
41629f9f09b0SDouglas Anderson  * @context:   The priv pointer from a struct dwc2_hcd_urb.
41639f9f09b0SDouglas Anderson  * @mem_flags: Flags for allocating memory.
41649f9f09b0SDouglas Anderson  * @ttport:    We'll return this device's port number here.  That's used to
41659f9f09b0SDouglas Anderson  *             reference into the bitmap if we're on a multi_tt hub.
41669f9f09b0SDouglas Anderson  *
41679f9f09b0SDouglas Anderson  * Return: a pointer to a struct dwc2_tt.  Don't forget to call
41689f9f09b0SDouglas Anderson  *         dwc2_host_put_tt_info()!  Returns NULL upon memory alloc failure.
41699f9f09b0SDouglas Anderson  */
41709f9f09b0SDouglas Anderson 
41719f9f09b0SDouglas Anderson struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
41729f9f09b0SDouglas Anderson 				      gfp_t mem_flags, int *ttport)
41739f9f09b0SDouglas Anderson {
41749f9f09b0SDouglas Anderson 	struct urb *urb = context;
41759f9f09b0SDouglas Anderson 	struct dwc2_tt *dwc_tt = NULL;
41769f9f09b0SDouglas Anderson 
41779f9f09b0SDouglas Anderson 	if (urb->dev->tt) {
41789f9f09b0SDouglas Anderson 		*ttport = urb->dev->ttport;
41799f9f09b0SDouglas Anderson 
41809f9f09b0SDouglas Anderson 		dwc_tt = urb->dev->tt->hcpriv;
41819da51974SJohn Youn 		if (!dwc_tt) {
41829f9f09b0SDouglas Anderson 			size_t bitmap_size;
41839f9f09b0SDouglas Anderson 
41849f9f09b0SDouglas Anderson 			/*
41859f9f09b0SDouglas Anderson 			 * For single_tt we need one schedule.  For multi_tt
41869f9f09b0SDouglas Anderson 			 * we need one per port.
41879f9f09b0SDouglas Anderson 			 */
41889f9f09b0SDouglas Anderson 			bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
41899f9f09b0SDouglas Anderson 				      sizeof(dwc_tt->periodic_bitmaps[0]);
41909f9f09b0SDouglas Anderson 			if (urb->dev->tt->multi)
41919f9f09b0SDouglas Anderson 				bitmap_size *= urb->dev->tt->hub->maxchild;
41929f9f09b0SDouglas Anderson 
41939f9f09b0SDouglas Anderson 			dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
41949f9f09b0SDouglas Anderson 					 mem_flags);
41959da51974SJohn Youn 			if (!dwc_tt)
41969f9f09b0SDouglas Anderson 				return NULL;
41979f9f09b0SDouglas Anderson 
41989f9f09b0SDouglas Anderson 			dwc_tt->usb_tt = urb->dev->tt;
41999f9f09b0SDouglas Anderson 			dwc_tt->usb_tt->hcpriv = dwc_tt;
42009f9f09b0SDouglas Anderson 		}
42019f9f09b0SDouglas Anderson 
42029f9f09b0SDouglas Anderson 		dwc_tt->refcount++;
42039f9f09b0SDouglas Anderson 	}
42049f9f09b0SDouglas Anderson 
42059f9f09b0SDouglas Anderson 	return dwc_tt;
42069f9f09b0SDouglas Anderson }
42079f9f09b0SDouglas Anderson 
42089f9f09b0SDouglas Anderson /**
42099f9f09b0SDouglas Anderson  * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
42109f9f09b0SDouglas Anderson  *
42119f9f09b0SDouglas Anderson  * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
42129f9f09b0SDouglas Anderson  * of the structure are done.
42139f9f09b0SDouglas Anderson  *
42149f9f09b0SDouglas Anderson  * It's OK to call this with NULL.
42159f9f09b0SDouglas Anderson  *
42169f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
42179f9f09b0SDouglas Anderson  * @dwc_tt:    The pointer returned by dwc2_host_get_tt_info.
42189f9f09b0SDouglas Anderson  */
42199f9f09b0SDouglas Anderson void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
42209f9f09b0SDouglas Anderson {
42219f9f09b0SDouglas Anderson 	/* Model kfree and make put of NULL a no-op */
42229da51974SJohn Youn 	if (!dwc_tt)
42239f9f09b0SDouglas Anderson 		return;
42249f9f09b0SDouglas Anderson 
42259f9f09b0SDouglas Anderson 	WARN_ON(dwc_tt->refcount < 1);
42269f9f09b0SDouglas Anderson 
42279f9f09b0SDouglas Anderson 	dwc_tt->refcount--;
42289f9f09b0SDouglas Anderson 	if (!dwc_tt->refcount) {
42299f9f09b0SDouglas Anderson 		dwc_tt->usb_tt->hcpriv = NULL;
42309f9f09b0SDouglas Anderson 		kfree(dwc_tt);
42319f9f09b0SDouglas Anderson 	}
42329f9f09b0SDouglas Anderson }
42339f9f09b0SDouglas Anderson 
4234197ba5f4SPaul Zimmerman int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4235197ba5f4SPaul Zimmerman {
4236197ba5f4SPaul Zimmerman 	struct urb *urb = context;
4237197ba5f4SPaul Zimmerman 
4238197ba5f4SPaul Zimmerman 	return urb->dev->speed;
4239197ba5f4SPaul Zimmerman }
4240197ba5f4SPaul Zimmerman 
4241197ba5f4SPaul Zimmerman static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4242197ba5f4SPaul Zimmerman 					struct urb *urb)
4243197ba5f4SPaul Zimmerman {
4244197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4245197ba5f4SPaul Zimmerman 
4246197ba5f4SPaul Zimmerman 	if (urb->interval)
4247197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated += bw / urb->interval;
4248197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4249197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs++;
4250197ba5f4SPaul Zimmerman 	else
4251197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs++;
4252197ba5f4SPaul Zimmerman }
4253197ba5f4SPaul Zimmerman 
4254197ba5f4SPaul Zimmerman static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4255197ba5f4SPaul Zimmerman 				    struct urb *urb)
4256197ba5f4SPaul Zimmerman {
4257197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4258197ba5f4SPaul Zimmerman 
4259197ba5f4SPaul Zimmerman 	if (urb->interval)
4260197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated -= bw / urb->interval;
4261197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4262197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs--;
4263197ba5f4SPaul Zimmerman 	else
4264197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs--;
4265197ba5f4SPaul Zimmerman }
4266197ba5f4SPaul Zimmerman 
4267197ba5f4SPaul Zimmerman /*
4268197ba5f4SPaul Zimmerman  * Sets the final status of an URB and returns it to the upper layer. Any
4269197ba5f4SPaul Zimmerman  * required cleanup of the URB is performed.
4270197ba5f4SPaul Zimmerman  *
4271197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
4272197ba5f4SPaul Zimmerman  */
4273197ba5f4SPaul Zimmerman void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4274197ba5f4SPaul Zimmerman 			int status)
4275197ba5f4SPaul Zimmerman {
4276197ba5f4SPaul Zimmerman 	struct urb *urb;
4277197ba5f4SPaul Zimmerman 	int i;
4278197ba5f4SPaul Zimmerman 
4279197ba5f4SPaul Zimmerman 	if (!qtd) {
4280197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4281197ba5f4SPaul Zimmerman 		return;
4282197ba5f4SPaul Zimmerman 	}
4283197ba5f4SPaul Zimmerman 
4284197ba5f4SPaul Zimmerman 	if (!qtd->urb) {
4285197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4286197ba5f4SPaul Zimmerman 		return;
4287197ba5f4SPaul Zimmerman 	}
4288197ba5f4SPaul Zimmerman 
4289197ba5f4SPaul Zimmerman 	urb = qtd->urb->priv;
4290197ba5f4SPaul Zimmerman 	if (!urb) {
4291197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4292197ba5f4SPaul Zimmerman 		return;
4293197ba5f4SPaul Zimmerman 	}
4294197ba5f4SPaul Zimmerman 
4295197ba5f4SPaul Zimmerman 	urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
4296197ba5f4SPaul Zimmerman 
4297197ba5f4SPaul Zimmerman 	if (dbg_urb(urb))
4298197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
4299197ba5f4SPaul Zimmerman 			 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4300197ba5f4SPaul Zimmerman 			 __func__, urb, usb_pipedevice(urb->pipe),
4301197ba5f4SPaul Zimmerman 			 usb_pipeendpoint(urb->pipe),
4302197ba5f4SPaul Zimmerman 			 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4303197ba5f4SPaul Zimmerman 			 urb->actual_length);
4304197ba5f4SPaul Zimmerman 
4305197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4306197ba5f4SPaul Zimmerman 		urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
4307197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; ++i) {
4308197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].actual_length =
4309197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_actual_length(
4310197ba5f4SPaul Zimmerman 						qtd->urb, i);
4311197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].status =
4312197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
4313197ba5f4SPaul Zimmerman 		}
4314197ba5f4SPaul Zimmerman 	}
4315197ba5f4SPaul Zimmerman 
4316fe9b1773SGregory Herrero 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4317fe9b1773SGregory Herrero 		for (i = 0; i < urb->number_of_packets; i++)
4318fe9b1773SGregory Herrero 			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4319fe9b1773SGregory Herrero 				 i, urb->iso_frame_desc[i].status);
4320fe9b1773SGregory Herrero 	}
4321fe9b1773SGregory Herrero 
4322197ba5f4SPaul Zimmerman 	urb->status = status;
4323197ba5f4SPaul Zimmerman 	if (!status) {
4324197ba5f4SPaul Zimmerman 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4325197ba5f4SPaul Zimmerman 		    urb->actual_length < urb->transfer_buffer_length)
4326197ba5f4SPaul Zimmerman 			urb->status = -EREMOTEIO;
4327197ba5f4SPaul Zimmerman 	}
4328197ba5f4SPaul Zimmerman 
4329197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4330197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4331197ba5f4SPaul Zimmerman 		struct usb_host_endpoint *ep = urb->ep;
4332197ba5f4SPaul Zimmerman 
4333197ba5f4SPaul Zimmerman 		if (ep)
4334197ba5f4SPaul Zimmerman 			dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4335197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4336197ba5f4SPaul Zimmerman 					urb);
4337197ba5f4SPaul Zimmerman 	}
4338197ba5f4SPaul Zimmerman 
4339197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
4340197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4341197ba5f4SPaul Zimmerman 	kfree(qtd->urb);
4342197ba5f4SPaul Zimmerman 	qtd->urb = NULL;
4343197ba5f4SPaul Zimmerman 
4344197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
4345197ba5f4SPaul Zimmerman }
4346197ba5f4SPaul Zimmerman 
4347197ba5f4SPaul Zimmerman /*
4348197ba5f4SPaul Zimmerman  * Work queue function for starting the HCD when A-Cable is connected
4349197ba5f4SPaul Zimmerman  */
4350197ba5f4SPaul Zimmerman static void dwc2_hcd_start_func(struct work_struct *work)
4351197ba5f4SPaul Zimmerman {
4352197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4353197ba5f4SPaul Zimmerman 						start_work.work);
4354197ba5f4SPaul Zimmerman 
4355197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4356197ba5f4SPaul Zimmerman 	dwc2_host_start(hsotg);
4357197ba5f4SPaul Zimmerman }
4358197ba5f4SPaul Zimmerman 
4359197ba5f4SPaul Zimmerman /*
4360197ba5f4SPaul Zimmerman  * Reset work queue function
4361197ba5f4SPaul Zimmerman  */
4362197ba5f4SPaul Zimmerman static void dwc2_hcd_reset_func(struct work_struct *work)
4363197ba5f4SPaul Zimmerman {
4364197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4365197ba5f4SPaul Zimmerman 						reset_work.work);
43664a065c7bSDouglas Anderson 	unsigned long flags;
4367197ba5f4SPaul Zimmerman 	u32 hprt0;
4368197ba5f4SPaul Zimmerman 
4369197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "USB RESET function called\n");
43704a065c7bSDouglas Anderson 
43714a065c7bSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
43724a065c7bSDouglas Anderson 
4373197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
4374197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RST;
437595c8bc36SAntti Seppälä 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
4376197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_reset_change = 1;
43774a065c7bSDouglas Anderson 
43784a065c7bSDouglas Anderson 	spin_unlock_irqrestore(&hsotg->lock, flags);
4379197ba5f4SPaul Zimmerman }
4380197ba5f4SPaul Zimmerman 
4381197ba5f4SPaul Zimmerman /*
4382197ba5f4SPaul Zimmerman  * =========================================================================
4383197ba5f4SPaul Zimmerman  *  Linux HC Driver Functions
4384197ba5f4SPaul Zimmerman  * =========================================================================
4385197ba5f4SPaul Zimmerman  */
4386197ba5f4SPaul Zimmerman 
4387197ba5f4SPaul Zimmerman /*
4388197ba5f4SPaul Zimmerman  * Initializes the DWC_otg controller and its root hub and prepares it for host
4389197ba5f4SPaul Zimmerman  * mode operation. Activates the root port. Returns 0 on success and a negative
4390197ba5f4SPaul Zimmerman  * error code on failure.
4391197ba5f4SPaul Zimmerman  */
4392197ba5f4SPaul Zimmerman static int _dwc2_hcd_start(struct usb_hcd *hcd)
4393197ba5f4SPaul Zimmerman {
4394197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4395197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4396197ba5f4SPaul Zimmerman 	unsigned long flags;
4397197ba5f4SPaul Zimmerman 
4398197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4399197ba5f4SPaul Zimmerman 
4400197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
440131927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L0;
4402197ba5f4SPaul Zimmerman 	hcd->state = HC_STATE_RUNNING;
440331927b6bSGregory Herrero 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4404197ba5f4SPaul Zimmerman 
4405197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
4406197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4407197ba5f4SPaul Zimmerman 		return 0;	/* why 0 ?? */
4408197ba5f4SPaul Zimmerman 	}
4409197ba5f4SPaul Zimmerman 
4410197ba5f4SPaul Zimmerman 	dwc2_hcd_reinit(hsotg);
4411197ba5f4SPaul Zimmerman 
4412197ba5f4SPaul Zimmerman 	/* Initialize and connect root hub if one is not already attached */
4413197ba5f4SPaul Zimmerman 	if (bus->root_hub) {
4414197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4415197ba5f4SPaul Zimmerman 		/* Inform the HUB driver to resume */
4416197ba5f4SPaul Zimmerman 		usb_hcd_resume_root_hub(hcd);
4417197ba5f4SPaul Zimmerman 	}
4418197ba5f4SPaul Zimmerman 
4419197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4420531ef5ebSAmelie Delaunay 
4421a7ef2074STomeu Vizoso 	return dwc2_vbus_supply_init(hsotg);
4422197ba5f4SPaul Zimmerman }
4423197ba5f4SPaul Zimmerman 
4424197ba5f4SPaul Zimmerman /*
4425197ba5f4SPaul Zimmerman  * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4426197ba5f4SPaul Zimmerman  * stopped.
4427197ba5f4SPaul Zimmerman  */
4428197ba5f4SPaul Zimmerman static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4429197ba5f4SPaul Zimmerman {
4430197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4431197ba5f4SPaul Zimmerman 	unsigned long flags;
4432197ba5f4SPaul Zimmerman 
44335bbf6ce0SGregory Herrero 	/* Turn off all host-specific interrupts */
44345bbf6ce0SGregory Herrero 	dwc2_disable_host_interrupts(hsotg);
44355bbf6ce0SGregory Herrero 
4436091473adSGregory Herrero 	/* Wait for interrupt processing to finish */
4437091473adSGregory Herrero 	synchronize_irq(hcd->irq);
4438091473adSGregory Herrero 
4439197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4440091473adSGregory Herrero 	/* Ensure hcd is disconnected */
44416a659531SDouglas Anderson 	dwc2_hcd_disconnect(hsotg, true);
4442197ba5f4SPaul Zimmerman 	dwc2_hcd_stop(hsotg);
444331927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L3;
444431927b6bSGregory Herrero 	hcd->state = HC_STATE_HALT;
444531927b6bSGregory Herrero 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4446197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4447197ba5f4SPaul Zimmerman 
4448531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_exit(hsotg);
4449531ef5ebSAmelie Delaunay 
4450197ba5f4SPaul Zimmerman 	usleep_range(1000, 3000);
4451197ba5f4SPaul Zimmerman }
4452197ba5f4SPaul Zimmerman 
445399a65798SGregory Herrero static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
445499a65798SGregory Herrero {
445599a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4456a2a23d3fSGregory Herrero 	unsigned long flags;
4457a2a23d3fSGregory Herrero 	int ret = 0;
4458a2a23d3fSGregory Herrero 	u32 hprt0;
445999a65798SGregory Herrero 
4460a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4461a2a23d3fSGregory Herrero 
4462f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4463f367b72cSMeng Dongyang 		goto unlock;
4464f367b72cSMeng Dongyang 
4465a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L0)
4466a2a23d3fSGregory Herrero 		goto unlock;
4467a2a23d3fSGregory Herrero 
4468a2a23d3fSGregory Herrero 	if (!HCD_HW_ACCESSIBLE(hcd))
4469a2a23d3fSGregory Herrero 		goto unlock;
4470a2a23d3fSGregory Herrero 
4471866932e2SJohn Stultz 	if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4472866932e2SJohn Stultz 		goto unlock;
4473866932e2SJohn Stultz 
4474631a2310SVardan Mikayelyan 	if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL)
4475a2a23d3fSGregory Herrero 		goto skip_power_saving;
4476a2a23d3fSGregory Herrero 
4477a2a23d3fSGregory Herrero 	/*
4478a2a23d3fSGregory Herrero 	 * Drive USB suspend and disable port Power
4479a2a23d3fSGregory Herrero 	 * if usb bus is not suspended.
4480a2a23d3fSGregory Herrero 	 */
4481a2a23d3fSGregory Herrero 	if (!hsotg->bus_suspended) {
4482a2a23d3fSGregory Herrero 		hprt0 = dwc2_read_hprt0(hsotg);
4483a2a23d3fSGregory Herrero 		hprt0 |= HPRT0_SUSP;
4484a2a23d3fSGregory Herrero 		hprt0 &= ~HPRT0_PWR;
4485a2a23d3fSGregory Herrero 		dwc2_writel(hprt0, hsotg->regs + HPRT0);
4486531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
4487a2a23d3fSGregory Herrero 	}
4488a2a23d3fSGregory Herrero 
448941ba9b9bSVardan Mikayelyan 	/* Enter partial_power_down */
449041ba9b9bSVardan Mikayelyan 	ret = dwc2_enter_partial_power_down(hsotg);
4491a2a23d3fSGregory Herrero 	if (ret) {
4492a2a23d3fSGregory Herrero 		if (ret != -ENOTSUPP)
4493a2a23d3fSGregory Herrero 			dev_err(hsotg->dev,
449441ba9b9bSVardan Mikayelyan 				"enter partial_power_down failed\n");
4495a2a23d3fSGregory Herrero 		goto skip_power_saving;
4496a2a23d3fSGregory Herrero 	}
4497a2a23d3fSGregory Herrero 
4498a2a23d3fSGregory Herrero 	/* Ask phy to be suspended */
4499a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4500a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4501a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, true);
4502a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4503a2a23d3fSGregory Herrero 	}
4504a2a23d3fSGregory Herrero 
450541ba9b9bSVardan Mikayelyan 	/* After entering partial_power_down, hardware is no more accessible */
4506a2a23d3fSGregory Herrero 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4507a2a23d3fSGregory Herrero 
4508a2a23d3fSGregory Herrero skip_power_saving:
450999a65798SGregory Herrero 	hsotg->lx_state = DWC2_L2;
4510a2a23d3fSGregory Herrero unlock:
4511a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4512a2a23d3fSGregory Herrero 
4513a2a23d3fSGregory Herrero 	return ret;
451499a65798SGregory Herrero }
451599a65798SGregory Herrero 
451699a65798SGregory Herrero static int _dwc2_hcd_resume(struct usb_hcd *hcd)
451799a65798SGregory Herrero {
451899a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4519a2a23d3fSGregory Herrero 	unsigned long flags;
4520a2a23d3fSGregory Herrero 	int ret = 0;
4521a2a23d3fSGregory Herrero 
4522a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4523a2a23d3fSGregory Herrero 
4524f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4525f367b72cSMeng Dongyang 		goto unlock;
4526f367b72cSMeng Dongyang 
4527a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L2)
4528a2a23d3fSGregory Herrero 		goto unlock;
4529a2a23d3fSGregory Herrero 
4530631a2310SVardan Mikayelyan 	if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL) {
4531a2a23d3fSGregory Herrero 		hsotg->lx_state = DWC2_L0;
4532a2a23d3fSGregory Herrero 		goto unlock;
4533a2a23d3fSGregory Herrero 	}
4534a2a23d3fSGregory Herrero 
4535a2a23d3fSGregory Herrero 	/*
4536a2a23d3fSGregory Herrero 	 * Set HW accessible bit before powering on the controller
4537a2a23d3fSGregory Herrero 	 * since an interrupt may rise.
4538a2a23d3fSGregory Herrero 	 */
4539a2a23d3fSGregory Herrero 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4540a2a23d3fSGregory Herrero 
4541a2a23d3fSGregory Herrero 	/*
4542a2a23d3fSGregory Herrero 	 * Enable power if not already done.
4543a2a23d3fSGregory Herrero 	 * This must not be spinlocked since duration
4544a2a23d3fSGregory Herrero 	 * of this call is unknown.
4545a2a23d3fSGregory Herrero 	 */
4546a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4547a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4548a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, false);
4549a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4550a2a23d3fSGregory Herrero 	}
4551a2a23d3fSGregory Herrero 
455241ba9b9bSVardan Mikayelyan 	/* Exit partial_power_down */
455341ba9b9bSVardan Mikayelyan 	ret = dwc2_exit_partial_power_down(hsotg, true);
4554a2a23d3fSGregory Herrero 	if (ret && (ret != -ENOTSUPP))
455541ba9b9bSVardan Mikayelyan 		dev_err(hsotg->dev, "exit partial_power_down failed\n");
455699a65798SGregory Herrero 
455799a65798SGregory Herrero 	hsotg->lx_state = DWC2_L0;
4558a2a23d3fSGregory Herrero 
4559a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4560a2a23d3fSGregory Herrero 
4561a2a23d3fSGregory Herrero 	if (hsotg->bus_suspended) {
4562a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4563a2a23d3fSGregory Herrero 		hsotg->flags.b.port_suspend_change = 1;
4564a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4565a2a23d3fSGregory Herrero 		dwc2_port_resume(hsotg);
4566a2a23d3fSGregory Herrero 	} else {
4567531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_init(hsotg);
4568531ef5ebSAmelie Delaunay 
45695634e016SGregory Herrero 		/* Wait for controller to correctly update D+/D- level */
45705634e016SGregory Herrero 		usleep_range(3000, 5000);
45715634e016SGregory Herrero 
4572a2a23d3fSGregory Herrero 		/*
4573a2a23d3fSGregory Herrero 		 * Clear Port Enable and Port Status changes.
4574a2a23d3fSGregory Herrero 		 * Enable Port Power.
4575a2a23d3fSGregory Herrero 		 */
4576a2a23d3fSGregory Herrero 		dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4577a2a23d3fSGregory Herrero 				HPRT0_ENACHG, hsotg->regs + HPRT0);
4578a2a23d3fSGregory Herrero 		/* Wait for controller to detect Port Connect */
45795634e016SGregory Herrero 		usleep_range(5000, 7000);
4580a2a23d3fSGregory Herrero 	}
4581a2a23d3fSGregory Herrero 
4582a2a23d3fSGregory Herrero 	return ret;
4583a2a23d3fSGregory Herrero unlock:
4584a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4585a2a23d3fSGregory Herrero 
4586a2a23d3fSGregory Herrero 	return ret;
458799a65798SGregory Herrero }
458899a65798SGregory Herrero 
4589197ba5f4SPaul Zimmerman /* Returns the current frame number */
4590197ba5f4SPaul Zimmerman static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4591197ba5f4SPaul Zimmerman {
4592197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4593197ba5f4SPaul Zimmerman 
4594197ba5f4SPaul Zimmerman 	return dwc2_hcd_get_frame_number(hsotg);
4595197ba5f4SPaul Zimmerman }
4596197ba5f4SPaul Zimmerman 
4597197ba5f4SPaul Zimmerman static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4598197ba5f4SPaul Zimmerman 			       char *fn_name)
4599197ba5f4SPaul Zimmerman {
4600197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
4601197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4602efe357f4SNicholas Mc Guire 	char *pipetype = NULL;
4603efe357f4SNicholas Mc Guire 	char *speed = NULL;
4604197ba5f4SPaul Zimmerman 
4605197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4606197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Device address: %d\n",
4607197ba5f4SPaul Zimmerman 		 usb_pipedevice(urb->pipe));
4608197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint: %d, %s\n",
4609197ba5f4SPaul Zimmerman 		 usb_pipeendpoint(urb->pipe),
4610197ba5f4SPaul Zimmerman 		 usb_pipein(urb->pipe) ? "IN" : "OUT");
4611197ba5f4SPaul Zimmerman 
4612197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4613197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4614197ba5f4SPaul Zimmerman 		pipetype = "CONTROL";
4615197ba5f4SPaul Zimmerman 		break;
4616197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4617197ba5f4SPaul Zimmerman 		pipetype = "BULK";
4618197ba5f4SPaul Zimmerman 		break;
4619197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4620197ba5f4SPaul Zimmerman 		pipetype = "INTERRUPT";
4621197ba5f4SPaul Zimmerman 		break;
4622197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4623197ba5f4SPaul Zimmerman 		pipetype = "ISOCHRONOUS";
4624197ba5f4SPaul Zimmerman 		break;
4625197ba5f4SPaul Zimmerman 	}
4626197ba5f4SPaul Zimmerman 
4627197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint type: %s %s (%s)\n", pipetype,
4628197ba5f4SPaul Zimmerman 		 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4629197ba5f4SPaul Zimmerman 		 "IN" : "OUT");
4630197ba5f4SPaul Zimmerman 
4631197ba5f4SPaul Zimmerman 	switch (urb->dev->speed) {
4632197ba5f4SPaul Zimmerman 	case USB_SPEED_HIGH:
4633197ba5f4SPaul Zimmerman 		speed = "HIGH";
4634197ba5f4SPaul Zimmerman 		break;
4635197ba5f4SPaul Zimmerman 	case USB_SPEED_FULL:
4636197ba5f4SPaul Zimmerman 		speed = "FULL";
4637197ba5f4SPaul Zimmerman 		break;
4638197ba5f4SPaul Zimmerman 	case USB_SPEED_LOW:
4639197ba5f4SPaul Zimmerman 		speed = "LOW";
4640197ba5f4SPaul Zimmerman 		break;
4641197ba5f4SPaul Zimmerman 	default:
4642197ba5f4SPaul Zimmerman 		speed = "UNKNOWN";
4643197ba5f4SPaul Zimmerman 		break;
4644197ba5f4SPaul Zimmerman 	}
4645197ba5f4SPaul Zimmerman 
4646197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Speed: %s\n", speed);
4647197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Max packet size: %d\n",
4648197ba5f4SPaul Zimmerman 		 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4649197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Data buffer length: %d\n",
4650197ba5f4SPaul Zimmerman 		 urb->transfer_buffer_length);
4651197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
4652197ba5f4SPaul Zimmerman 		 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4653197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
4654197ba5f4SPaul Zimmerman 		 urb->setup_packet, (unsigned long)urb->setup_dma);
4655197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Interval: %d\n", urb->interval);
4656197ba5f4SPaul Zimmerman 
4657197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4658197ba5f4SPaul Zimmerman 		int i;
4659197ba5f4SPaul Zimmerman 
4660197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; i++) {
4661197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  ISO Desc %d:\n", i);
4662197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "    offset: %d, length %d\n",
4663197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].offset,
4664197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].length);
4665197ba5f4SPaul Zimmerman 		}
4666197ba5f4SPaul Zimmerman 	}
4667197ba5f4SPaul Zimmerman #endif
4668197ba5f4SPaul Zimmerman }
4669197ba5f4SPaul Zimmerman 
4670197ba5f4SPaul Zimmerman /*
4671197ba5f4SPaul Zimmerman  * Starts processing a USB transfer request specified by a USB Request Block
4672197ba5f4SPaul Zimmerman  * (URB). mem_flags indicates the type of memory allocation to use while
4673197ba5f4SPaul Zimmerman  * processing this URB.
4674197ba5f4SPaul Zimmerman  */
4675197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4676197ba5f4SPaul Zimmerman 				 gfp_t mem_flags)
4677197ba5f4SPaul Zimmerman {
4678197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4679197ba5f4SPaul Zimmerman 	struct usb_host_endpoint *ep = urb->ep;
4680197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *dwc2_urb;
4681197ba5f4SPaul Zimmerman 	int i;
4682197ba5f4SPaul Zimmerman 	int retval;
4683197ba5f4SPaul Zimmerman 	int alloc_bandwidth = 0;
4684197ba5f4SPaul Zimmerman 	u8 ep_type = 0;
4685197ba5f4SPaul Zimmerman 	u32 tflags = 0;
4686197ba5f4SPaul Zimmerman 	void *buf;
4687197ba5f4SPaul Zimmerman 	unsigned long flags;
4688b58e6ceeSMian Yousaf Kaukab 	struct dwc2_qh *qh;
4689b58e6ceeSMian Yousaf Kaukab 	bool qh_allocated = false;
4690b5a468a6SMian Yousaf Kaukab 	struct dwc2_qtd *qtd;
4691197ba5f4SPaul Zimmerman 
4692197ba5f4SPaul Zimmerman 	if (dbg_urb(urb)) {
4693197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4694197ba5f4SPaul Zimmerman 		dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4695197ba5f4SPaul Zimmerman 	}
4696197ba5f4SPaul Zimmerman 
46979da51974SJohn Youn 	if (!ep)
4698197ba5f4SPaul Zimmerman 		return -EINVAL;
4699197ba5f4SPaul Zimmerman 
4700197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4701197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4702197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
4703197ba5f4SPaul Zimmerman 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4704197ba5f4SPaul Zimmerman 			alloc_bandwidth = 1;
4705197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4706197ba5f4SPaul Zimmerman 	}
4707197ba5f4SPaul Zimmerman 
4708197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4709197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4710197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_CONTROL;
4711197ba5f4SPaul Zimmerman 		break;
4712197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4713197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_ISOC;
4714197ba5f4SPaul Zimmerman 		break;
4715197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4716197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_BULK;
4717197ba5f4SPaul Zimmerman 		break;
4718197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4719197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_INT;
4720197ba5f4SPaul Zimmerman 		break;
4721197ba5f4SPaul Zimmerman 	}
4722197ba5f4SPaul Zimmerman 
4723197ba5f4SPaul Zimmerman 	dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4724197ba5f4SPaul Zimmerman 				      mem_flags);
4725197ba5f4SPaul Zimmerman 	if (!dwc2_urb)
4726197ba5f4SPaul Zimmerman 		return -ENOMEM;
4727197ba5f4SPaul Zimmerman 
4728197ba5f4SPaul Zimmerman 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4729197ba5f4SPaul Zimmerman 				  usb_pipeendpoint(urb->pipe), ep_type,
4730197ba5f4SPaul Zimmerman 				  usb_pipein(urb->pipe),
4731197ba5f4SPaul Zimmerman 				  usb_maxpacket(urb->dev, urb->pipe,
4732197ba5f4SPaul Zimmerman 						!(usb_pipein(urb->pipe))));
4733197ba5f4SPaul Zimmerman 
4734197ba5f4SPaul Zimmerman 	buf = urb->transfer_buffer;
4735197ba5f4SPaul Zimmerman 
4736197ba5f4SPaul Zimmerman 	if (hcd->self.uses_dma) {
4737197ba5f4SPaul Zimmerman 		if (!buf && (urb->transfer_dma & 3)) {
4738197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
4739197ba5f4SPaul Zimmerman 				"%s: unaligned transfer with no transfer_buffer",
4740197ba5f4SPaul Zimmerman 				__func__);
4741197ba5f4SPaul Zimmerman 			retval = -EINVAL;
474233ad261aSGregory Herrero 			goto fail0;
4743197ba5f4SPaul Zimmerman 		}
4744197ba5f4SPaul Zimmerman 	}
4745197ba5f4SPaul Zimmerman 
4746197ba5f4SPaul Zimmerman 	if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4747197ba5f4SPaul Zimmerman 		tflags |= URB_GIVEBACK_ASAP;
4748197ba5f4SPaul Zimmerman 	if (urb->transfer_flags & URB_ZERO_PACKET)
4749197ba5f4SPaul Zimmerman 		tflags |= URB_SEND_ZERO_PACKET;
4750197ba5f4SPaul Zimmerman 
4751197ba5f4SPaul Zimmerman 	dwc2_urb->priv = urb;
4752197ba5f4SPaul Zimmerman 	dwc2_urb->buf = buf;
4753197ba5f4SPaul Zimmerman 	dwc2_urb->dma = urb->transfer_dma;
4754197ba5f4SPaul Zimmerman 	dwc2_urb->length = urb->transfer_buffer_length;
4755197ba5f4SPaul Zimmerman 	dwc2_urb->setup_packet = urb->setup_packet;
4756197ba5f4SPaul Zimmerman 	dwc2_urb->setup_dma = urb->setup_dma;
4757197ba5f4SPaul Zimmerman 	dwc2_urb->flags = tflags;
4758197ba5f4SPaul Zimmerman 	dwc2_urb->interval = urb->interval;
4759197ba5f4SPaul Zimmerman 	dwc2_urb->status = -EINPROGRESS;
4760197ba5f4SPaul Zimmerman 
4761197ba5f4SPaul Zimmerman 	for (i = 0; i < urb->number_of_packets; ++i)
4762197ba5f4SPaul Zimmerman 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4763197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].offset,
4764197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].length);
4765197ba5f4SPaul Zimmerman 
4766197ba5f4SPaul Zimmerman 	urb->hcpriv = dwc2_urb;
4767b58e6ceeSMian Yousaf Kaukab 	qh = (struct dwc2_qh *)ep->hcpriv;
4768b58e6ceeSMian Yousaf Kaukab 	/* Create QH for the endpoint if it doesn't exist */
4769b58e6ceeSMian Yousaf Kaukab 	if (!qh) {
4770b58e6ceeSMian Yousaf Kaukab 		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4771b58e6ceeSMian Yousaf Kaukab 		if (!qh) {
4772b58e6ceeSMian Yousaf Kaukab 			retval = -ENOMEM;
4773b58e6ceeSMian Yousaf Kaukab 			goto fail0;
4774b58e6ceeSMian Yousaf Kaukab 		}
4775b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = qh;
4776b58e6ceeSMian Yousaf Kaukab 		qh_allocated = true;
4777b58e6ceeSMian Yousaf Kaukab 	}
4778197ba5f4SPaul Zimmerman 
4779b5a468a6SMian Yousaf Kaukab 	qtd = kzalloc(sizeof(*qtd), mem_flags);
4780b5a468a6SMian Yousaf Kaukab 	if (!qtd) {
4781b5a468a6SMian Yousaf Kaukab 		retval = -ENOMEM;
4782b5a468a6SMian Yousaf Kaukab 		goto fail1;
4783b5a468a6SMian Yousaf Kaukab 	}
4784b5a468a6SMian Yousaf Kaukab 
4785197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4786197ba5f4SPaul Zimmerman 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
4787197ba5f4SPaul Zimmerman 	if (retval)
4788197ba5f4SPaul Zimmerman 		goto fail2;
4789197ba5f4SPaul Zimmerman 
4790b5a468a6SMian Yousaf Kaukab 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4791b5a468a6SMian Yousaf Kaukab 	if (retval)
4792b5a468a6SMian Yousaf Kaukab 		goto fail3;
4793b5a468a6SMian Yousaf Kaukab 
4794197ba5f4SPaul Zimmerman 	if (alloc_bandwidth) {
4795197ba5f4SPaul Zimmerman 		dwc2_allocate_bus_bandwidth(hcd,
4796197ba5f4SPaul Zimmerman 				dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4797197ba5f4SPaul Zimmerman 				urb);
4798197ba5f4SPaul Zimmerman 	}
4799197ba5f4SPaul Zimmerman 
480033ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
480133ad261aSGregory Herrero 
4802197ba5f4SPaul Zimmerman 	return 0;
4803197ba5f4SPaul Zimmerman 
4804b5a468a6SMian Yousaf Kaukab fail3:
4805197ba5f4SPaul Zimmerman 	dwc2_urb->priv = NULL;
4806197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
480716e80218SDouglas Anderson 	if (qh_allocated && qh->channel && qh->channel->qh == qh)
480816e80218SDouglas Anderson 		qh->channel->qh = NULL;
4809b5a468a6SMian Yousaf Kaukab fail2:
481033ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4811197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4812b5a468a6SMian Yousaf Kaukab 	kfree(qtd);
4813b0d65902SVardan Mikayelyan 	qtd = NULL;
4814b5a468a6SMian Yousaf Kaukab fail1:
4815b58e6ceeSMian Yousaf Kaukab 	if (qh_allocated) {
4816b58e6ceeSMian Yousaf Kaukab 		struct dwc2_qtd *qtd2, *qtd2_tmp;
4817b58e6ceeSMian Yousaf Kaukab 
4818b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = NULL;
4819b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_unlink(hsotg, qh);
4820b58e6ceeSMian Yousaf Kaukab 		/* Free each QTD in the QH's QTD list */
4821b58e6ceeSMian Yousaf Kaukab 		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
4822b58e6ceeSMian Yousaf Kaukab 					 qtd_list_entry)
4823b58e6ceeSMian Yousaf Kaukab 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4824b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_free(hsotg, qh);
4825b58e6ceeSMian Yousaf Kaukab 	}
482633ad261aSGregory Herrero fail0:
4827197ba5f4SPaul Zimmerman 	kfree(dwc2_urb);
4828197ba5f4SPaul Zimmerman 
4829197ba5f4SPaul Zimmerman 	return retval;
4830197ba5f4SPaul Zimmerman }
4831197ba5f4SPaul Zimmerman 
4832197ba5f4SPaul Zimmerman /*
4833197ba5f4SPaul Zimmerman  * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4834197ba5f4SPaul Zimmerman  */
4835197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4836197ba5f4SPaul Zimmerman 				 int status)
4837197ba5f4SPaul Zimmerman {
4838197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4839197ba5f4SPaul Zimmerman 	int rc;
4840197ba5f4SPaul Zimmerman 	unsigned long flags;
4841197ba5f4SPaul Zimmerman 
4842197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4843197ba5f4SPaul Zimmerman 	dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4844197ba5f4SPaul Zimmerman 
4845197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4846197ba5f4SPaul Zimmerman 
4847197ba5f4SPaul Zimmerman 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4848197ba5f4SPaul Zimmerman 	if (rc)
4849197ba5f4SPaul Zimmerman 		goto out;
4850197ba5f4SPaul Zimmerman 
4851197ba5f4SPaul Zimmerman 	if (!urb->hcpriv) {
4852197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4853197ba5f4SPaul Zimmerman 		goto out;
4854197ba5f4SPaul Zimmerman 	}
4855197ba5f4SPaul Zimmerman 
4856197ba5f4SPaul Zimmerman 	rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4857197ba5f4SPaul Zimmerman 
4858197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
4859197ba5f4SPaul Zimmerman 
4860197ba5f4SPaul Zimmerman 	kfree(urb->hcpriv);
4861197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4862197ba5f4SPaul Zimmerman 
4863197ba5f4SPaul Zimmerman 	/* Higher layer software sets URB status */
4864197ba5f4SPaul Zimmerman 	spin_unlock(&hsotg->lock);
4865197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(hcd, urb, status);
4866197ba5f4SPaul Zimmerman 	spin_lock(&hsotg->lock);
4867197ba5f4SPaul Zimmerman 
4868197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4869197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  urb->status = %d\n", urb->status);
4870197ba5f4SPaul Zimmerman out:
4871197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4872197ba5f4SPaul Zimmerman 
4873197ba5f4SPaul Zimmerman 	return rc;
4874197ba5f4SPaul Zimmerman }
4875197ba5f4SPaul Zimmerman 
4876197ba5f4SPaul Zimmerman /*
4877197ba5f4SPaul Zimmerman  * Frees resources in the DWC_otg controller related to a given endpoint. Also
4878197ba5f4SPaul Zimmerman  * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4879197ba5f4SPaul Zimmerman  * must already be dequeued.
4880197ba5f4SPaul Zimmerman  */
4881197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4882197ba5f4SPaul Zimmerman 				       struct usb_host_endpoint *ep)
4883197ba5f4SPaul Zimmerman {
4884197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4885197ba5f4SPaul Zimmerman 
4886197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4887197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4888197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress, ep->hcpriv);
4889197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4890197ba5f4SPaul Zimmerman }
4891197ba5f4SPaul Zimmerman 
4892197ba5f4SPaul Zimmerman /*
4893197ba5f4SPaul Zimmerman  * Resets endpoint specific parameter values, in current version used to reset
4894197ba5f4SPaul Zimmerman  * the data toggle (as a WA). This function can be called from usb_clear_halt
4895197ba5f4SPaul Zimmerman  * routine.
4896197ba5f4SPaul Zimmerman  */
4897197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4898197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep)
4899197ba5f4SPaul Zimmerman {
4900197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4901197ba5f4SPaul Zimmerman 	unsigned long flags;
4902197ba5f4SPaul Zimmerman 
4903197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4904197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4905197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress);
4906197ba5f4SPaul Zimmerman 
4907197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4908197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_reset(hsotg, ep);
4909197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4910197ba5f4SPaul Zimmerman }
4911197ba5f4SPaul Zimmerman 
4912197ba5f4SPaul Zimmerman /*
4913197ba5f4SPaul Zimmerman  * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4914197ba5f4SPaul Zimmerman  * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4915197ba5f4SPaul Zimmerman  * interrupt.
4916197ba5f4SPaul Zimmerman  *
4917197ba5f4SPaul Zimmerman  * This function is called by the USB core when an interrupt occurs
4918197ba5f4SPaul Zimmerman  */
4919197ba5f4SPaul Zimmerman static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4920197ba5f4SPaul Zimmerman {
4921197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4922197ba5f4SPaul Zimmerman 
4923197ba5f4SPaul Zimmerman 	return dwc2_handle_hcd_intr(hsotg);
4924197ba5f4SPaul Zimmerman }
4925197ba5f4SPaul Zimmerman 
4926197ba5f4SPaul Zimmerman /*
4927197ba5f4SPaul Zimmerman  * Creates Status Change bitmap for the root hub and root port. The bitmap is
4928197ba5f4SPaul Zimmerman  * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4929197ba5f4SPaul Zimmerman  * is the status change indicator for the single root port. Returns 1 if either
4930197ba5f4SPaul Zimmerman  * change indicator is 1, otherwise returns 0.
4931197ba5f4SPaul Zimmerman  */
4932197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4933197ba5f4SPaul Zimmerman {
4934197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4935197ba5f4SPaul Zimmerman 
4936197ba5f4SPaul Zimmerman 	buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4937197ba5f4SPaul Zimmerman 	return buf[0] != 0;
4938197ba5f4SPaul Zimmerman }
4939197ba5f4SPaul Zimmerman 
4940197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
4941197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4942197ba5f4SPaul Zimmerman 				 u16 windex, char *buf, u16 wlength)
4943197ba5f4SPaul Zimmerman {
4944197ba5f4SPaul Zimmerman 	int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4945197ba5f4SPaul Zimmerman 					  wvalue, windex, buf, wlength);
4946197ba5f4SPaul Zimmerman 	return retval;
4947197ba5f4SPaul Zimmerman }
4948197ba5f4SPaul Zimmerman 
4949197ba5f4SPaul Zimmerman /* Handles hub TT buffer clear completions */
4950197ba5f4SPaul Zimmerman static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4951197ba5f4SPaul Zimmerman 					       struct usb_host_endpoint *ep)
4952197ba5f4SPaul Zimmerman {
4953197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4954197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
4955197ba5f4SPaul Zimmerman 	unsigned long flags;
4956197ba5f4SPaul Zimmerman 
4957197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
4958197ba5f4SPaul Zimmerman 	if (!qh)
4959197ba5f4SPaul Zimmerman 		return;
4960197ba5f4SPaul Zimmerman 
4961197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4962197ba5f4SPaul Zimmerman 	qh->tt_buffer_dirty = 0;
4963197ba5f4SPaul Zimmerman 
4964197ba5f4SPaul Zimmerman 	if (hsotg->flags.b.port_connect_status)
4965197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4966197ba5f4SPaul Zimmerman 
4967197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4968197ba5f4SPaul Zimmerman }
4969197ba5f4SPaul Zimmerman 
4970ca8b0332SChen Yu /*
4971ca8b0332SChen Yu  * HPRT0_SPD_HIGH_SPEED: high speed
4972ca8b0332SChen Yu  * HPRT0_SPD_FULL_SPEED: full speed
4973ca8b0332SChen Yu  */
4974ca8b0332SChen Yu static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4975ca8b0332SChen Yu {
4976ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4977ca8b0332SChen Yu 
4978ca8b0332SChen Yu 	if (hsotg->params.speed == speed)
4979ca8b0332SChen Yu 		return;
4980ca8b0332SChen Yu 
4981ca8b0332SChen Yu 	hsotg->params.speed = speed;
4982ca8b0332SChen Yu 	queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4983ca8b0332SChen Yu }
4984ca8b0332SChen Yu 
4985ca8b0332SChen Yu static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4986ca8b0332SChen Yu {
4987ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4988ca8b0332SChen Yu 
4989ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4990ca8b0332SChen Yu 		return;
4991ca8b0332SChen Yu 
4992ca8b0332SChen Yu 	/*
4993ca8b0332SChen Yu 	 * On removal, set speed to default high-speed.
4994ca8b0332SChen Yu 	 */
4995ca8b0332SChen Yu 	if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4996ca8b0332SChen Yu 	    udev->parent->speed < USB_SPEED_HIGH) {
4997ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to default high-speed\n");
4998ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4999ca8b0332SChen Yu 	}
5000ca8b0332SChen Yu }
5001ca8b0332SChen Yu 
5002ca8b0332SChen Yu static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
5003ca8b0332SChen Yu {
5004ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
5005ca8b0332SChen Yu 
5006ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
5007ca8b0332SChen Yu 		return 0;
5008ca8b0332SChen Yu 
5009ca8b0332SChen Yu 	if (udev->speed == USB_SPEED_HIGH) {
5010ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to high-speed\n");
5011ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
5012ca8b0332SChen Yu 	} else if ((udev->speed == USB_SPEED_FULL ||
5013ca8b0332SChen Yu 				udev->speed == USB_SPEED_LOW)) {
5014ca8b0332SChen Yu 		/*
5015ca8b0332SChen Yu 		 * Change speed setting to full-speed if there's
5016ca8b0332SChen Yu 		 * a full-speed or low-speed device plugged in.
5017ca8b0332SChen Yu 		 */
5018ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to full-speed\n");
5019ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
5020ca8b0332SChen Yu 	}
5021ca8b0332SChen Yu 
5022ca8b0332SChen Yu 	return 0;
5023ca8b0332SChen Yu }
5024ca8b0332SChen Yu 
5025197ba5f4SPaul Zimmerman static struct hc_driver dwc2_hc_driver = {
5026197ba5f4SPaul Zimmerman 	.description = "dwc2_hsotg",
5027197ba5f4SPaul Zimmerman 	.product_desc = "DWC OTG Controller",
5028197ba5f4SPaul Zimmerman 	.hcd_priv_size = sizeof(struct wrapper_priv_data),
5029197ba5f4SPaul Zimmerman 
5030197ba5f4SPaul Zimmerman 	.irq = _dwc2_hcd_irq,
50318add17cfSDouglas Anderson 	.flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
5032197ba5f4SPaul Zimmerman 
5033197ba5f4SPaul Zimmerman 	.start = _dwc2_hcd_start,
5034197ba5f4SPaul Zimmerman 	.stop = _dwc2_hcd_stop,
5035197ba5f4SPaul Zimmerman 	.urb_enqueue = _dwc2_hcd_urb_enqueue,
5036197ba5f4SPaul Zimmerman 	.urb_dequeue = _dwc2_hcd_urb_dequeue,
5037197ba5f4SPaul Zimmerman 	.endpoint_disable = _dwc2_hcd_endpoint_disable,
5038197ba5f4SPaul Zimmerman 	.endpoint_reset = _dwc2_hcd_endpoint_reset,
5039197ba5f4SPaul Zimmerman 	.get_frame_number = _dwc2_hcd_get_frame_number,
5040197ba5f4SPaul Zimmerman 
5041197ba5f4SPaul Zimmerman 	.hub_status_data = _dwc2_hcd_hub_status_data,
5042197ba5f4SPaul Zimmerman 	.hub_control = _dwc2_hcd_hub_control,
5043197ba5f4SPaul Zimmerman 	.clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
504499a65798SGregory Herrero 
504599a65798SGregory Herrero 	.bus_suspend = _dwc2_hcd_suspend,
504699a65798SGregory Herrero 	.bus_resume = _dwc2_hcd_resume,
50473bc04e28SDouglas Anderson 
50483bc04e28SDouglas Anderson 	.map_urb_for_dma	= dwc2_map_urb_for_dma,
50493bc04e28SDouglas Anderson 	.unmap_urb_for_dma	= dwc2_unmap_urb_for_dma,
5050197ba5f4SPaul Zimmerman };
5051197ba5f4SPaul Zimmerman 
5052197ba5f4SPaul Zimmerman /*
5053197ba5f4SPaul Zimmerman  * Frees secondary storage associated with the dwc2_hsotg structure contained
5054197ba5f4SPaul Zimmerman  * in the struct usb_hcd field
5055197ba5f4SPaul Zimmerman  */
5056197ba5f4SPaul Zimmerman static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
5057197ba5f4SPaul Zimmerman {
5058197ba5f4SPaul Zimmerman 	u32 ahbcfg;
5059197ba5f4SPaul Zimmerman 	u32 dctl;
5060197ba5f4SPaul Zimmerman 	int i;
5061197ba5f4SPaul Zimmerman 
5062197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
5063197ba5f4SPaul Zimmerman 
5064197ba5f4SPaul Zimmerman 	/* Free memory for QH/QTD lists */
5065197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
506638d2b5fbSDouglas Anderson 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
5067197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
5068197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
5069197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
5070197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
5071197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
5072197ba5f4SPaul Zimmerman 
5073197ba5f4SPaul Zimmerman 	/* Free memory for the host channels */
5074197ba5f4SPaul Zimmerman 	for (i = 0; i < MAX_EPS_CHANNELS; i++) {
5075197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
5076197ba5f4SPaul Zimmerman 
50779da51974SJohn Youn 		if (chan) {
5078197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
5079197ba5f4SPaul Zimmerman 				i, chan);
5080197ba5f4SPaul Zimmerman 			hsotg->hc_ptr_array[i] = NULL;
5081197ba5f4SPaul Zimmerman 			kfree(chan);
5082197ba5f4SPaul Zimmerman 		}
5083197ba5f4SPaul Zimmerman 	}
5084197ba5f4SPaul Zimmerman 
508595832c00SJohn Youn 	if (hsotg->params.host_dma) {
5086197ba5f4SPaul Zimmerman 		if (hsotg->status_buf) {
5087197ba5f4SPaul Zimmerman 			dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
5088197ba5f4SPaul Zimmerman 					  hsotg->status_buf,
5089197ba5f4SPaul Zimmerman 					  hsotg->status_buf_dma);
5090197ba5f4SPaul Zimmerman 			hsotg->status_buf = NULL;
5091197ba5f4SPaul Zimmerman 		}
5092197ba5f4SPaul Zimmerman 	} else {
5093197ba5f4SPaul Zimmerman 		kfree(hsotg->status_buf);
5094197ba5f4SPaul Zimmerman 		hsotg->status_buf = NULL;
5095197ba5f4SPaul Zimmerman 	}
5096197ba5f4SPaul Zimmerman 
509795c8bc36SAntti Seppälä 	ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
5098197ba5f4SPaul Zimmerman 
5099197ba5f4SPaul Zimmerman 	/* Disable all interrupts */
5100197ba5f4SPaul Zimmerman 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
510195c8bc36SAntti Seppälä 	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
510295c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + GINTMSK);
5103197ba5f4SPaul Zimmerman 
5104197ba5f4SPaul Zimmerman 	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
510595c8bc36SAntti Seppälä 		dctl = dwc2_readl(hsotg->regs + DCTL);
5106197ba5f4SPaul Zimmerman 		dctl |= DCTL_SFTDISCON;
510795c8bc36SAntti Seppälä 		dwc2_writel(dctl, hsotg->regs + DCTL);
5108197ba5f4SPaul Zimmerman 	}
5109197ba5f4SPaul Zimmerman 
5110197ba5f4SPaul Zimmerman 	if (hsotg->wq_otg) {
5111197ba5f4SPaul Zimmerman 		if (!cancel_work_sync(&hsotg->wf_otg))
5112197ba5f4SPaul Zimmerman 			flush_workqueue(hsotg->wq_otg);
5113197ba5f4SPaul Zimmerman 		destroy_workqueue(hsotg->wq_otg);
5114197ba5f4SPaul Zimmerman 	}
5115197ba5f4SPaul Zimmerman 
5116197ba5f4SPaul Zimmerman 	del_timer(&hsotg->wkp_timer);
5117197ba5f4SPaul Zimmerman }
5118197ba5f4SPaul Zimmerman 
5119197ba5f4SPaul Zimmerman static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5120197ba5f4SPaul Zimmerman {
5121197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
5122197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
5123197ba5f4SPaul Zimmerman 
5124197ba5f4SPaul Zimmerman 	dwc2_hcd_free(hsotg);
5125197ba5f4SPaul Zimmerman }
5126197ba5f4SPaul Zimmerman 
5127197ba5f4SPaul Zimmerman /*
5128197ba5f4SPaul Zimmerman  * Initializes the HCD. This function allocates memory for and initializes the
5129197ba5f4SPaul Zimmerman  * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5130197ba5f4SPaul Zimmerman  * USB bus with the core and calls the hc_driver->start() function. It returns
5131197ba5f4SPaul Zimmerman  * a negative error on failure.
5132197ba5f4SPaul Zimmerman  */
51334fe160d5SHeiner Kallweit int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5134197ba5f4SPaul Zimmerman {
5135348becdcSHeiner Kallweit 	struct platform_device *pdev = to_platform_device(hsotg->dev);
5136348becdcSHeiner Kallweit 	struct resource *res;
5137197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5138197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
5139197ba5f4SPaul Zimmerman 	u32 hcfg;
5140197ba5f4SPaul Zimmerman 	int i, num_channels;
5141197ba5f4SPaul Zimmerman 	int retval;
5142197ba5f4SPaul Zimmerman 
5143f5500eccSDinh Nguyen 	if (usb_disabled())
5144f5500eccSDinh Nguyen 		return -ENODEV;
5145f5500eccSDinh Nguyen 
5146197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
5147197ba5f4SPaul Zimmerman 
5148197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5149197ba5f4SPaul Zimmerman 
515095c8bc36SAntti Seppälä 	hcfg = dwc2_readl(hsotg->regs + HCFG);
5151197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5152197ba5f4SPaul Zimmerman 
5153197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
51546396bb22SKees Cook 	hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
51556396bb22SKees Cook 					 sizeof(*hsotg->frame_num_array),
51566396bb22SKees Cook 					 GFP_KERNEL);
5157197ba5f4SPaul Zimmerman 	if (!hsotg->frame_num_array)
5158197ba5f4SPaul Zimmerman 		goto error1;
51596396bb22SKees Cook 	hsotg->last_frame_num_array =
51606396bb22SKees Cook 		kcalloc(FRAME_NUM_ARRAY_SIZE,
51616396bb22SKees Cook 			sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
5162197ba5f4SPaul Zimmerman 	if (!hsotg->last_frame_num_array)
5163197ba5f4SPaul Zimmerman 		goto error1;
5164197ba5f4SPaul Zimmerman #endif
5165483bb254SDouglas Anderson 	hsotg->last_frame_num = HFNUM_MAX_FRNUM;
5166197ba5f4SPaul Zimmerman 
5167197ba5f4SPaul Zimmerman 	/* Check if the bus driver or platform code has setup a dma_mask */
516895832c00SJohn Youn 	if (hsotg->params.host_dma &&
51699da51974SJohn Youn 	    !hsotg->dev->dma_mask) {
5170197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
5171197ba5f4SPaul Zimmerman 			 "dma_mask not set, disabling DMA\n");
5172fdb09b3eSNicholas Mc Guire 		hsotg->params.host_dma = false;
517395832c00SJohn Youn 		hsotg->params.dma_desc_enable = false;
5174197ba5f4SPaul Zimmerman 	}
5175197ba5f4SPaul Zimmerman 
5176197ba5f4SPaul Zimmerman 	/* Set device flags indicating whether the HCD supports DMA */
517795832c00SJohn Youn 	if (hsotg->params.host_dma) {
5178197ba5f4SPaul Zimmerman 		if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5179197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set DMA mask\n");
5180197ba5f4SPaul Zimmerman 		if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5181197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
5182197ba5f4SPaul Zimmerman 	}
5183197ba5f4SPaul Zimmerman 
5184ca8b0332SChen Yu 	if (hsotg->params.change_speed_quirk) {
5185ca8b0332SChen Yu 		dwc2_hc_driver.free_dev = dwc2_free_dev;
5186ca8b0332SChen Yu 		dwc2_hc_driver.reset_device = dwc2_reset_device;
5187ca8b0332SChen Yu 	}
5188ca8b0332SChen Yu 
5189197ba5f4SPaul Zimmerman 	hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5190197ba5f4SPaul Zimmerman 	if (!hcd)
5191197ba5f4SPaul Zimmerman 		goto error1;
5192197ba5f4SPaul Zimmerman 
519395832c00SJohn Youn 	if (!hsotg->params.host_dma)
5194197ba5f4SPaul Zimmerman 		hcd->self.uses_dma = 0;
5195197ba5f4SPaul Zimmerman 
5196197ba5f4SPaul Zimmerman 	hcd->has_tt = 1;
5197197ba5f4SPaul Zimmerman 
5198348becdcSHeiner Kallweit 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5199348becdcSHeiner Kallweit 	hcd->rsrc_start = res->start;
5200348becdcSHeiner Kallweit 	hcd->rsrc_len = resource_size(res);
5201348becdcSHeiner Kallweit 
5202197ba5f4SPaul Zimmerman 	((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
5203197ba5f4SPaul Zimmerman 	hsotg->priv = hcd;
5204197ba5f4SPaul Zimmerman 
5205197ba5f4SPaul Zimmerman 	/*
5206197ba5f4SPaul Zimmerman 	 * Disable the global interrupt until all the interrupt handlers are
5207197ba5f4SPaul Zimmerman 	 * installed
5208197ba5f4SPaul Zimmerman 	 */
5209197ba5f4SPaul Zimmerman 	dwc2_disable_global_interrupts(hsotg);
5210197ba5f4SPaul Zimmerman 
5211197ba5f4SPaul Zimmerman 	/* Initialize the DWC_otg core, and select the Phy type */
52120fe239bcSDouglas Anderson 	retval = dwc2_core_init(hsotg, true);
5213197ba5f4SPaul Zimmerman 	if (retval)
5214197ba5f4SPaul Zimmerman 		goto error2;
5215197ba5f4SPaul Zimmerman 
5216197ba5f4SPaul Zimmerman 	/* Create new workqueue and init work */
5217197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5218ec7b1268SBhaktipriya Shridhar 	hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
5219197ba5f4SPaul Zimmerman 	if (!hsotg->wq_otg) {
5220197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Failed to create workqueue\n");
5221197ba5f4SPaul Zimmerman 		goto error2;
5222197ba5f4SPaul Zimmerman 	}
5223197ba5f4SPaul Zimmerman 	INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5224197ba5f4SPaul Zimmerman 
5225e99e88a9SKees Cook 	timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
5226197ba5f4SPaul Zimmerman 
5227197ba5f4SPaul Zimmerman 	/* Initialize the non-periodic schedule */
5228197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
522938d2b5fbSDouglas Anderson 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
5230197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5231197ba5f4SPaul Zimmerman 
5232197ba5f4SPaul Zimmerman 	/* Initialize the periodic schedule */
5233197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5234197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5235197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5236197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5237197ba5f4SPaul Zimmerman 
5238c9c8ac01SDouglas Anderson 	INIT_LIST_HEAD(&hsotg->split_order);
5239c9c8ac01SDouglas Anderson 
5240197ba5f4SPaul Zimmerman 	/*
5241197ba5f4SPaul Zimmerman 	 * Create a host channel descriptor for each host channel implemented
5242197ba5f4SPaul Zimmerman 	 * in the controller. Initialize the channel descriptor array.
5243197ba5f4SPaul Zimmerman 	 */
5244197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->free_hc_list);
5245bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
5246197ba5f4SPaul Zimmerman 	memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5247197ba5f4SPaul Zimmerman 
5248197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
5249197ba5f4SPaul Zimmerman 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
52509da51974SJohn Youn 		if (!channel)
5251197ba5f4SPaul Zimmerman 			goto error3;
5252197ba5f4SPaul Zimmerman 		channel->hc_num = i;
5253c9c8ac01SDouglas Anderson 		INIT_LIST_HEAD(&channel->split_order_list_entry);
5254197ba5f4SPaul Zimmerman 		hsotg->hc_ptr_array[i] = channel;
5255197ba5f4SPaul Zimmerman 	}
5256197ba5f4SPaul Zimmerman 
5257197ba5f4SPaul Zimmerman 	/* Initialize hsotg start work */
5258197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5259197ba5f4SPaul Zimmerman 
5260197ba5f4SPaul Zimmerman 	/* Initialize port reset work */
5261197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5262197ba5f4SPaul Zimmerman 
5263197ba5f4SPaul Zimmerman 	/*
5264197ba5f4SPaul Zimmerman 	 * Allocate space for storing data on status transactions. Normally no
5265197ba5f4SPaul Zimmerman 	 * data is sent, but this space acts as a bit bucket. This must be
5266197ba5f4SPaul Zimmerman 	 * done after usb_add_hcd since that function allocates the DMA buffer
5267197ba5f4SPaul Zimmerman 	 * pool.
5268197ba5f4SPaul Zimmerman 	 */
526995832c00SJohn Youn 	if (hsotg->params.host_dma)
5270197ba5f4SPaul Zimmerman 		hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5271197ba5f4SPaul Zimmerman 					DWC2_HCD_STATUS_BUF_SIZE,
5272197ba5f4SPaul Zimmerman 					&hsotg->status_buf_dma, GFP_KERNEL);
5273197ba5f4SPaul Zimmerman 	else
5274197ba5f4SPaul Zimmerman 		hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5275197ba5f4SPaul Zimmerman 					  GFP_KERNEL);
5276197ba5f4SPaul Zimmerman 
5277197ba5f4SPaul Zimmerman 	if (!hsotg->status_buf)
5278197ba5f4SPaul Zimmerman 		goto error3;
5279197ba5f4SPaul Zimmerman 
52803b5fcc9aSGregory Herrero 	/*
52813b5fcc9aSGregory Herrero 	 * Create kmem caches to handle descriptor buffers in descriptor
52823b5fcc9aSGregory Herrero 	 * DMA mode.
52833b5fcc9aSGregory Herrero 	 * Alignment must be set to 512 bytes.
52843b5fcc9aSGregory Herrero 	 */
5285bea8e86cSJohn Youn 	if (hsotg->params.dma_desc_enable ||
5286bea8e86cSJohn Youn 	    hsotg->params.dma_desc_fs_enable) {
52873b5fcc9aSGregory Herrero 		hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
5288ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52893b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
52903b5fcc9aSGregory Herrero 				NULL);
52913b5fcc9aSGregory Herrero 		if (!hsotg->desc_gen_cache) {
52923b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52933b5fcc9aSGregory Herrero 				"unable to create dwc2 generic desc cache\n");
52943b5fcc9aSGregory Herrero 
52953b5fcc9aSGregory Herrero 			/*
52963b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52973b5fcc9aSGregory Herrero 			 * usable.
52983b5fcc9aSGregory Herrero 			 */
529995832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
530095832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
53013b5fcc9aSGregory Herrero 		}
53023b5fcc9aSGregory Herrero 
53033b5fcc9aSGregory Herrero 		hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
5304ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
53053b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
53063b5fcc9aSGregory Herrero 		if (!hsotg->desc_hsisoc_cache) {
53073b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
53083b5fcc9aSGregory Herrero 				"unable to create dwc2 hs isoc desc cache\n");
53093b5fcc9aSGregory Herrero 
53103b5fcc9aSGregory Herrero 			kmem_cache_destroy(hsotg->desc_gen_cache);
53113b5fcc9aSGregory Herrero 
53123b5fcc9aSGregory Herrero 			/*
53133b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
53143b5fcc9aSGregory Herrero 			 * usable.
53153b5fcc9aSGregory Herrero 			 */
531695832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
531795832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
53183b5fcc9aSGregory Herrero 		}
53193b5fcc9aSGregory Herrero 	}
53203b5fcc9aSGregory Herrero 
5321af424a41SWilliam Wu 	if (hsotg->params.host_dma) {
5322af424a41SWilliam Wu 		/*
5323af424a41SWilliam Wu 		 * Create kmem caches to handle non-aligned buffer
5324af424a41SWilliam Wu 		 * in Buffer DMA mode.
5325af424a41SWilliam Wu 		 */
5326af424a41SWilliam Wu 		hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
5327af424a41SWilliam Wu 						DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
5328af424a41SWilliam Wu 						SLAB_CACHE_DMA, NULL);
5329af424a41SWilliam Wu 		if (!hsotg->unaligned_cache)
5330af424a41SWilliam Wu 			dev_err(hsotg->dev,
5331af424a41SWilliam Wu 				"unable to create dwc2 unaligned cache\n");
5332af424a41SWilliam Wu 	}
5333af424a41SWilliam Wu 
5334197ba5f4SPaul Zimmerman 	hsotg->otg_port = 1;
5335197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
5336197ba5f4SPaul Zimmerman 	hsotg->frame_list_dma = 0;
5337197ba5f4SPaul Zimmerman 	hsotg->periodic_qh_count = 0;
5338197ba5f4SPaul Zimmerman 
5339197ba5f4SPaul Zimmerman 	/* Initiate lx_state to L3 disconnected state */
5340197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L3;
5341197ba5f4SPaul Zimmerman 
5342197ba5f4SPaul Zimmerman 	hcd->self.otg_port = hsotg->otg_port;
5343197ba5f4SPaul Zimmerman 
5344197ba5f4SPaul Zimmerman 	/* Don't support SG list at this point */
5345197ba5f4SPaul Zimmerman 	hcd->self.sg_tablesize = 0;
5346197ba5f4SPaul Zimmerman 
53479df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
53489df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, &hcd->self);
53499df4ceacSMian Yousaf Kaukab 
5350197ba5f4SPaul Zimmerman 	/*
5351197ba5f4SPaul Zimmerman 	 * Finish generic HCD initialization and start the HCD. This function
5352197ba5f4SPaul Zimmerman 	 * allocates the DMA buffer pool, registers the USB bus, requests the
5353197ba5f4SPaul Zimmerman 	 * IRQ line, and calls hcd_start method.
5354197ba5f4SPaul Zimmerman 	 */
53554fe160d5SHeiner Kallweit 	retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
5356197ba5f4SPaul Zimmerman 	if (retval < 0)
53573b5fcc9aSGregory Herrero 		goto error4;
5358197ba5f4SPaul Zimmerman 
5359ec513b16SLinus Torvalds 	device_wakeup_enable(hcd->self.controller);
5360ec513b16SLinus Torvalds 
5361197ba5f4SPaul Zimmerman 	dwc2_hcd_dump_state(hsotg);
5362197ba5f4SPaul Zimmerman 
5363197ba5f4SPaul Zimmerman 	dwc2_enable_global_interrupts(hsotg);
5364197ba5f4SPaul Zimmerman 
5365197ba5f4SPaul Zimmerman 	return 0;
5366197ba5f4SPaul Zimmerman 
53673b5fcc9aSGregory Herrero error4:
5368af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53693b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5370af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
5371197ba5f4SPaul Zimmerman error3:
5372197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5373197ba5f4SPaul Zimmerman error2:
5374197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5375197ba5f4SPaul Zimmerman error1:
5376197ba5f4SPaul Zimmerman 
5377197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5378197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5379197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5380197ba5f4SPaul Zimmerman #endif
5381197ba5f4SPaul Zimmerman 
5382197ba5f4SPaul Zimmerman 	dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
5383197ba5f4SPaul Zimmerman 	return retval;
5384197ba5f4SPaul Zimmerman }
5385197ba5f4SPaul Zimmerman 
5386197ba5f4SPaul Zimmerman /*
5387197ba5f4SPaul Zimmerman  * Removes the HCD.
5388197ba5f4SPaul Zimmerman  * Frees memory and resources associated with the HCD and deregisters the bus.
5389197ba5f4SPaul Zimmerman  */
5390197ba5f4SPaul Zimmerman void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
5391197ba5f4SPaul Zimmerman {
5392197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5393197ba5f4SPaul Zimmerman 
5394197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
5395197ba5f4SPaul Zimmerman 
5396197ba5f4SPaul Zimmerman 	hcd = dwc2_hsotg_to_hcd(hsotg);
5397197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
5398197ba5f4SPaul Zimmerman 
5399197ba5f4SPaul Zimmerman 	if (!hcd) {
5400197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
5401197ba5f4SPaul Zimmerman 			__func__);
5402197ba5f4SPaul Zimmerman 		return;
5403197ba5f4SPaul Zimmerman 	}
5404197ba5f4SPaul Zimmerman 
54059df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
54069df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, NULL);
54079df4ceacSMian Yousaf Kaukab 
5408197ba5f4SPaul Zimmerman 	usb_remove_hcd(hcd);
5409197ba5f4SPaul Zimmerman 	hsotg->priv = NULL;
54103b5fcc9aSGregory Herrero 
5411af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
54123b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5413af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
54143b5fcc9aSGregory Herrero 
5415197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5416197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5417197ba5f4SPaul Zimmerman 
5418197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5419197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5420197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5421197ba5f4SPaul Zimmerman #endif
5422197ba5f4SPaul Zimmerman }
542358e52ff6SJohn Youn 
542458e52ff6SJohn Youn /**
542558e52ff6SJohn Youn  * dwc2_backup_host_registers() - Backup controller host registers.
542658e52ff6SJohn Youn  * When suspending usb bus, registers needs to be backuped
542758e52ff6SJohn Youn  * if controller power is disabled once suspended.
542858e52ff6SJohn Youn  *
542958e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
543058e52ff6SJohn Youn  */
543158e52ff6SJohn Youn int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
543258e52ff6SJohn Youn {
543358e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
543458e52ff6SJohn Youn 	int i;
543558e52ff6SJohn Youn 
543658e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
543758e52ff6SJohn Youn 
543858e52ff6SJohn Youn 	/* Backup Host regs */
543958e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
544058e52ff6SJohn Youn 	hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
544158e52ff6SJohn Youn 	hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
5442bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
544358e52ff6SJohn Youn 		hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
544458e52ff6SJohn Youn 
544558e52ff6SJohn Youn 	hr->hprt0 = dwc2_read_hprt0(hsotg);
544658e52ff6SJohn Youn 	hr->hfir = dwc2_readl(hsotg->regs + HFIR);
544766a36096SVardan Mikayelyan 	hr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
544858e52ff6SJohn Youn 	hr->valid = true;
544958e52ff6SJohn Youn 
545058e52ff6SJohn Youn 	return 0;
545158e52ff6SJohn Youn }
545258e52ff6SJohn Youn 
545358e52ff6SJohn Youn /**
545458e52ff6SJohn Youn  * dwc2_restore_host_registers() - Restore controller host registers.
545558e52ff6SJohn Youn  * When resuming usb bus, device registers needs to be restored
545658e52ff6SJohn Youn  * if controller power were disabled.
545758e52ff6SJohn Youn  *
545858e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
545958e52ff6SJohn Youn  */
546058e52ff6SJohn Youn int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
546158e52ff6SJohn Youn {
546258e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
546358e52ff6SJohn Youn 	int i;
546458e52ff6SJohn Youn 
546558e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
546658e52ff6SJohn Youn 
546758e52ff6SJohn Youn 	/* Restore host regs */
546858e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
546958e52ff6SJohn Youn 	if (!hr->valid) {
547058e52ff6SJohn Youn 		dev_err(hsotg->dev, "%s: no host registers to restore\n",
547158e52ff6SJohn Youn 			__func__);
547258e52ff6SJohn Youn 		return -EINVAL;
547358e52ff6SJohn Youn 	}
547458e52ff6SJohn Youn 	hr->valid = false;
547558e52ff6SJohn Youn 
547658e52ff6SJohn Youn 	dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
547758e52ff6SJohn Youn 	dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
547858e52ff6SJohn Youn 
5479bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
548058e52ff6SJohn Youn 		dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
548158e52ff6SJohn Youn 
548258e52ff6SJohn Youn 	dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
548358e52ff6SJohn Youn 	dwc2_writel(hr->hfir, hsotg->regs + HFIR);
548466a36096SVardan Mikayelyan 	dwc2_writel(hr->hptxfsiz, hsotg->regs + HPTXFSIZ);
548558e52ff6SJohn Youn 	hsotg->frame_number = 0;
548658e52ff6SJohn Youn 
548758e52ff6SJohn Youn 	return 0;
548858e52ff6SJohn Youn }
5489c5c403dcSVardan Mikayelyan 
5490c5c403dcSVardan Mikayelyan /**
5491c5c403dcSVardan Mikayelyan  * dwc2_host_enter_hibernation() - Put controller in Hibernation.
5492c5c403dcSVardan Mikayelyan  *
5493c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5494c5c403dcSVardan Mikayelyan  */
5495c5c403dcSVardan Mikayelyan int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
5496c5c403dcSVardan Mikayelyan {
5497c5c403dcSVardan Mikayelyan 	unsigned long flags;
5498c5c403dcSVardan Mikayelyan 	int ret = 0;
5499c5c403dcSVardan Mikayelyan 	u32 hprt0;
5500c5c403dcSVardan Mikayelyan 	u32 pcgcctl;
5501c5c403dcSVardan Mikayelyan 	u32 gusbcfg;
5502c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5503c5c403dcSVardan Mikayelyan 
5504c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
5505c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_global_registers(hsotg);
5506c5c403dcSVardan Mikayelyan 	if (ret) {
5507c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5508c5c403dcSVardan Mikayelyan 			__func__);
5509c5c403dcSVardan Mikayelyan 		return ret;
5510c5c403dcSVardan Mikayelyan 	}
5511c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_host_registers(hsotg);
5512c5c403dcSVardan Mikayelyan 	if (ret) {
5513c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5514c5c403dcSVardan Mikayelyan 			__func__);
5515c5c403dcSVardan Mikayelyan 		return ret;
5516c5c403dcSVardan Mikayelyan 	}
5517c5c403dcSVardan Mikayelyan 
5518c5c403dcSVardan Mikayelyan 	/* Enter USB Suspend Mode */
5519c5c403dcSVardan Mikayelyan 	hprt0 = dwc2_readl(hsotg->regs + HPRT0);
5520c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_SUSP;
5521c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5522c5c403dcSVardan Mikayelyan 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
5523c5c403dcSVardan Mikayelyan 
5524c5c403dcSVardan Mikayelyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
552522bb5cfdSArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
552607b8dc55SColin Ian King 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
5527c5c403dcSVardan Mikayelyan 
5528c5c403dcSVardan Mikayelyan 	/*
5529c5c403dcSVardan Mikayelyan 	 * We need to disable interrupts to prevent servicing of any IRQ
5530c5c403dcSVardan Mikayelyan 	 * during going to hibernation
5531c5c403dcSVardan Mikayelyan 	 */
5532c5c403dcSVardan Mikayelyan 	spin_lock_irqsave(&hsotg->lock, flags);
5533c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L2;
5534c5c403dcSVardan Mikayelyan 
5535c5c403dcSVardan Mikayelyan 	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
5536c5c403dcSVardan Mikayelyan 	if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
5537c5c403dcSVardan Mikayelyan 		/* ULPI interface */
5538c5c403dcSVardan Mikayelyan 		/* Suspend the Phy Clock */
5539c5c403dcSVardan Mikayelyan 		pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5540c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5541c5c403dcSVardan Mikayelyan 		dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5542c5c403dcSVardan Mikayelyan 		udelay(10);
5543c5c403dcSVardan Mikayelyan 
5544c5c403dcSVardan Mikayelyan 		gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5545c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5546c5c403dcSVardan Mikayelyan 		dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5547c5c403dcSVardan Mikayelyan 		udelay(10);
5548c5c403dcSVardan Mikayelyan 	} else {
5549c5c403dcSVardan Mikayelyan 		/* UTMI+ Interface */
5550c5c403dcSVardan Mikayelyan 		gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5551c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5552c5c403dcSVardan Mikayelyan 		dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5553c5c403dcSVardan Mikayelyan 		udelay(10);
5554c5c403dcSVardan Mikayelyan 
5555c5c403dcSVardan Mikayelyan 		pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5556c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5557c5c403dcSVardan Mikayelyan 		dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5558c5c403dcSVardan Mikayelyan 		udelay(10);
5559c5c403dcSVardan Mikayelyan 	}
5560c5c403dcSVardan Mikayelyan 
5561c5c403dcSVardan Mikayelyan 	/* Enable interrupts from wake up logic */
5562c5c403dcSVardan Mikayelyan 	gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5563c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PMUINTSEL;
5564c5c403dcSVardan Mikayelyan 	dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5565c5c403dcSVardan Mikayelyan 	udelay(10);
5566c5c403dcSVardan Mikayelyan 
5567c5c403dcSVardan Mikayelyan 	/* Unmask host mode interrupts in GPWRDN */
5568c5c403dcSVardan Mikayelyan 	gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5569c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_DISCONN_DET_MSK;
5570c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5571c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5572c5c403dcSVardan Mikayelyan 	dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5573c5c403dcSVardan Mikayelyan 	udelay(10);
5574c5c403dcSVardan Mikayelyan 
5575c5c403dcSVardan Mikayelyan 	/* Enable Power Down Clamp */
5576c5c403dcSVardan Mikayelyan 	gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5577c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNCLMP;
5578c5c403dcSVardan Mikayelyan 	dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5579c5c403dcSVardan Mikayelyan 	udelay(10);
5580c5c403dcSVardan Mikayelyan 
5581c5c403dcSVardan Mikayelyan 	/* Switch off VDD */
5582c5c403dcSVardan Mikayelyan 	gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5583c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNSWTCH;
5584c5c403dcSVardan Mikayelyan 	dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5585c5c403dcSVardan Mikayelyan 
5586c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 1;
5587c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 1;
5588c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation completed\n");
5589c5c403dcSVardan Mikayelyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
5590c5c403dcSVardan Mikayelyan 	return ret;
5591c5c403dcSVardan Mikayelyan }
5592c5c403dcSVardan Mikayelyan 
5593c5c403dcSVardan Mikayelyan /*
5594c5c403dcSVardan Mikayelyan  * dwc2_host_exit_hibernation()
5595c5c403dcSVardan Mikayelyan  *
5596c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5597c5c403dcSVardan Mikayelyan  * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5598c5c403dcSVardan Mikayelyan  * @param reset: indicates whether resume is initiated by Reset.
5599c5c403dcSVardan Mikayelyan  *
5600c5c403dcSVardan Mikayelyan  * Return: non-zero if failed to enter to hibernation.
5601c5c403dcSVardan Mikayelyan  *
5602c5c403dcSVardan Mikayelyan  * This function is for exiting from Host mode hibernation by
5603c5c403dcSVardan Mikayelyan  * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
5604c5c403dcSVardan Mikayelyan  */
5605c5c403dcSVardan Mikayelyan int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
5606c5c403dcSVardan Mikayelyan 			       int reset)
5607c5c403dcSVardan Mikayelyan {
5608c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5609c5c403dcSVardan Mikayelyan 	u32 hprt0;
5610c5c403dcSVardan Mikayelyan 	int ret = 0;
5611c5c403dcSVardan Mikayelyan 	struct dwc2_gregs_backup *gr;
5612c5c403dcSVardan Mikayelyan 	struct dwc2_hregs_backup *hr;
5613c5c403dcSVardan Mikayelyan 
5614c5c403dcSVardan Mikayelyan 	gr = &hsotg->gr_backup;
5615c5c403dcSVardan Mikayelyan 	hr = &hsotg->hr_backup;
5616c5c403dcSVardan Mikayelyan 
5617c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev,
5618c5c403dcSVardan Mikayelyan 		"%s: called with rem_wakeup = %d reset = %d\n",
5619c5c403dcSVardan Mikayelyan 		__func__, rem_wakeup, reset);
5620c5c403dcSVardan Mikayelyan 
5621c5c403dcSVardan Mikayelyan 	dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
5622c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5623c5c403dcSVardan Mikayelyan 
5624c5c403dcSVardan Mikayelyan 	/*
5625c5c403dcSVardan Mikayelyan 	 * This step is not described in functional spec but if not wait for
5626c5c403dcSVardan Mikayelyan 	 * this delay, mismatch interrupts occurred because just after restore
5627c5c403dcSVardan Mikayelyan 	 * core is in Device mode(gintsts.curmode == 0)
5628c5c403dcSVardan Mikayelyan 	 */
5629c5c403dcSVardan Mikayelyan 	mdelay(100);
5630c5c403dcSVardan Mikayelyan 
5631c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5632c5c403dcSVardan Mikayelyan 	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5633c5c403dcSVardan Mikayelyan 
5634c5c403dcSVardan Mikayelyan 	/* De-assert Restore */
5635c5c403dcSVardan Mikayelyan 	gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5636c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_RESTORE;
5637c5c403dcSVardan Mikayelyan 	dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5638c5c403dcSVardan Mikayelyan 	udelay(10);
5639c5c403dcSVardan Mikayelyan 
5640c5c403dcSVardan Mikayelyan 	/* Restore GUSBCFG, HCFG */
5641c5c403dcSVardan Mikayelyan 	dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
5642c5c403dcSVardan Mikayelyan 	dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5643c5c403dcSVardan Mikayelyan 
5644c5c403dcSVardan Mikayelyan 	/* De-assert Wakeup Logic */
5645c5c403dcSVardan Mikayelyan 	gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5646c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_PMUACTV;
5647c5c403dcSVardan Mikayelyan 	dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5648c5c403dcSVardan Mikayelyan 	udelay(10);
5649c5c403dcSVardan Mikayelyan 
5650c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5651c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5652c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5653c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5654c5c403dcSVardan Mikayelyan 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
5655c5c403dcSVardan Mikayelyan 
5656c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5657c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5658c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5659c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5660c5c403dcSVardan Mikayelyan 
5661c5c403dcSVardan Mikayelyan 	if (reset) {
5662c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RST;
5663c5c403dcSVardan Mikayelyan 		dwc2_writel(hprt0, hsotg->regs + HPRT0);
5664c5c403dcSVardan Mikayelyan 
5665c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5666c5c403dcSVardan Mikayelyan 		mdelay(60);
5667c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RST;
5668c5c403dcSVardan Mikayelyan 		dwc2_writel(hprt0, hsotg->regs + HPRT0);
5669c5c403dcSVardan Mikayelyan 	} else {
5670c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RES;
5671c5c403dcSVardan Mikayelyan 		dwc2_writel(hprt0, hsotg->regs + HPRT0);
5672c5c403dcSVardan Mikayelyan 
5673c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5674c5c403dcSVardan Mikayelyan 		mdelay(100);
5675c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RES;
5676c5c403dcSVardan Mikayelyan 		dwc2_writel(hprt0, hsotg->regs + HPRT0);
5677c5c403dcSVardan Mikayelyan 	}
5678c5c403dcSVardan Mikayelyan 	/* Clear all interrupt status */
5679c5c403dcSVardan Mikayelyan 	hprt0 = dwc2_readl(hsotg->regs + HPRT0);
5680c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_CONNDET;
5681c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_ENACHG;
5682c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5683c5c403dcSVardan Mikayelyan 	dwc2_writel(hprt0, hsotg->regs + HPRT0);
5684c5c403dcSVardan Mikayelyan 
5685c5c403dcSVardan Mikayelyan 	hprt0 = dwc2_readl(hsotg->regs + HPRT0);
5686c5c403dcSVardan Mikayelyan 
5687c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5688c5c403dcSVardan Mikayelyan 	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5689c5c403dcSVardan Mikayelyan 
5690c5c403dcSVardan Mikayelyan 	/* Restore global registers */
5691c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_global_registers(hsotg);
5692c5c403dcSVardan Mikayelyan 	if (ret) {
5693c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore registers\n",
5694c5c403dcSVardan Mikayelyan 			__func__);
5695c5c403dcSVardan Mikayelyan 		return ret;
5696c5c403dcSVardan Mikayelyan 	}
5697c5c403dcSVardan Mikayelyan 
5698c5c403dcSVardan Mikayelyan 	/* Restore host registers */
5699c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_host_registers(hsotg);
5700c5c403dcSVardan Mikayelyan 	if (ret) {
5701c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5702c5c403dcSVardan Mikayelyan 			__func__);
5703c5c403dcSVardan Mikayelyan 		return ret;
5704c5c403dcSVardan Mikayelyan 	}
5705c5c403dcSVardan Mikayelyan 
570622bb5cfdSArtur Petrosyan 	dwc2_hcd_rem_wakeup(hsotg);
570722bb5cfdSArtur Petrosyan 
5708c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5709c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 0;
5710c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L0;
5711c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
5712c5c403dcSVardan Mikayelyan 	return ret;
5713c5c403dcSVardan Mikayelyan }
5714