xref: /linux/drivers/usb/dwc2/hcd.c (revision 34146c68083f1aef6709196b3dc888c1ceffd357)
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 
59b02038faSJohn Youn /*
60b02038faSJohn Youn  * =========================================================================
61b02038faSJohn Youn  *  Host Core Layer Functions
62b02038faSJohn Youn  * =========================================================================
63b02038faSJohn Youn  */
64b02038faSJohn Youn 
65b02038faSJohn Youn /**
66b02038faSJohn Youn  * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
67b02038faSJohn Youn  * used in both device and host modes
68b02038faSJohn Youn  *
69b02038faSJohn Youn  * @hsotg: Programming view of the DWC_otg controller
70b02038faSJohn Youn  */
71b02038faSJohn Youn static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
72b02038faSJohn Youn {
73b02038faSJohn Youn 	u32 intmsk;
74b02038faSJohn Youn 
75b02038faSJohn Youn 	/* Clear any pending OTG Interrupts */
76f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GOTGINT);
77b02038faSJohn Youn 
78b02038faSJohn Youn 	/* Clear any pending interrupts */
79f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
80b02038faSJohn Youn 
81b02038faSJohn Youn 	/* Enable the interrupts in the GINTMSK */
82b02038faSJohn Youn 	intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
83b02038faSJohn Youn 
8495832c00SJohn Youn 	if (!hsotg->params.host_dma)
85b02038faSJohn Youn 		intmsk |= GINTSTS_RXFLVL;
8695832c00SJohn Youn 	if (!hsotg->params.external_id_pin_ctl)
87b02038faSJohn Youn 		intmsk |= GINTSTS_CONIDSTSCHNG;
88b02038faSJohn Youn 
89b02038faSJohn Youn 	intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
90b02038faSJohn Youn 		  GINTSTS_SESSREQINT;
91b02038faSJohn Youn 
92376f0401SSevak Arakelyan 	if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
93376f0401SSevak Arakelyan 		intmsk |= GINTSTS_LPMTRANRCVD;
94376f0401SSevak Arakelyan 
95f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
96b02038faSJohn Youn }
97b02038faSJohn Youn 
98b02038faSJohn Youn static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
99b02038faSJohn Youn {
100f25c42b8SGevorg Sahakyan 	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
101b02038faSJohn Youn 
102b02038faSJohn Youn 	switch (hsotg->hw_params.arch) {
103b02038faSJohn Youn 	case GHWCFG2_EXT_DMA_ARCH:
104b02038faSJohn Youn 		dev_err(hsotg->dev, "External DMA Mode not supported\n");
105b02038faSJohn Youn 		return -EINVAL;
106b02038faSJohn Youn 
107b02038faSJohn Youn 	case GHWCFG2_INT_DMA_ARCH:
108b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Internal DMA Mode\n");
109bea8e86cSJohn Youn 		if (hsotg->params.ahbcfg != -1) {
110b02038faSJohn Youn 			ahbcfg &= GAHBCFG_CTRL_MASK;
111bea8e86cSJohn Youn 			ahbcfg |= hsotg->params.ahbcfg &
112b02038faSJohn Youn 				  ~GAHBCFG_CTRL_MASK;
113b02038faSJohn Youn 		}
114b02038faSJohn Youn 		break;
115b02038faSJohn Youn 
116b02038faSJohn Youn 	case GHWCFG2_SLAVE_ONLY_ARCH:
117b02038faSJohn Youn 	default:
118b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Slave Only Mode\n");
119b02038faSJohn Youn 		break;
120b02038faSJohn Youn 	}
121b02038faSJohn Youn 
12295832c00SJohn Youn 	if (hsotg->params.host_dma)
123b02038faSJohn Youn 		ahbcfg |= GAHBCFG_DMA_EN;
1249d729a7aSRazmik Karapetyan 	else
1259d729a7aSRazmik Karapetyan 		hsotg->params.dma_desc_enable = false;
126b02038faSJohn Youn 
127f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
128b02038faSJohn Youn 
129b02038faSJohn Youn 	return 0;
130b02038faSJohn Youn }
131b02038faSJohn Youn 
132b02038faSJohn Youn static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
133b02038faSJohn Youn {
134b02038faSJohn Youn 	u32 usbcfg;
135b02038faSJohn Youn 
136f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
137b02038faSJohn Youn 	usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
138b02038faSJohn Youn 
139b02038faSJohn Youn 	switch (hsotg->hw_params.op_mode) {
140b02038faSJohn Youn 	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
141f5c8a6cbSFabrice Gasnier 		if (hsotg->params.otg_caps.hnp_support &&
142f5c8a6cbSFabrice Gasnier 		    hsotg->params.otg_caps.srp_support)
143b02038faSJohn Youn 			usbcfg |= GUSBCFG_HNPCAP;
144f5c8a6cbSFabrice Gasnier 		fallthrough;
145b02038faSJohn Youn 
146b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
147b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
148b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
149f5c8a6cbSFabrice Gasnier 		if (hsotg->params.otg_caps.srp_support)
150b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
151b02038faSJohn Youn 		break;
152b02038faSJohn Youn 
153b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
154b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
155b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
156b02038faSJohn Youn 	default:
157b02038faSJohn Youn 		break;
158b02038faSJohn Youn 	}
159b02038faSJohn Youn 
160f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
161b02038faSJohn Youn }
162b02038faSJohn Youn 
163531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
164531ef5ebSAmelie Delaunay {
165e0f681c2SFabrice Gasnier 	if (hsotg->vbus_supply)
166531ef5ebSAmelie Delaunay 		return regulator_enable(hsotg->vbus_supply);
167e0f681c2SFabrice Gasnier 
168e0f681c2SFabrice Gasnier 	return 0;
169531ef5ebSAmelie Delaunay }
170531ef5ebSAmelie Delaunay 
171531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
172531ef5ebSAmelie Delaunay {
173531ef5ebSAmelie Delaunay 	if (hsotg->vbus_supply)
174531ef5ebSAmelie Delaunay 		return regulator_disable(hsotg->vbus_supply);
175531ef5ebSAmelie Delaunay 
176531ef5ebSAmelie Delaunay 	return 0;
177531ef5ebSAmelie Delaunay }
178531ef5ebSAmelie Delaunay 
179b02038faSJohn Youn /**
180b02038faSJohn Youn  * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
181b02038faSJohn Youn  *
182b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
183b02038faSJohn Youn  */
184b02038faSJohn Youn static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
185b02038faSJohn Youn {
186b02038faSJohn Youn 	u32 intmsk;
187b02038faSJohn Youn 
188b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s()\n", __func__);
189b02038faSJohn Youn 
190b02038faSJohn Youn 	/* Disable all interrupts */
191f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
192f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HAINTMSK);
193b02038faSJohn Youn 
194b02038faSJohn Youn 	/* Enable the common interrupts */
195b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
196b02038faSJohn Youn 
197b02038faSJohn Youn 	/* Enable host mode interrupts without disturbing common interrupts */
198f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
199b02038faSJohn Youn 	intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
200f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
201b02038faSJohn Youn }
202b02038faSJohn Youn 
203b02038faSJohn Youn /**
204b02038faSJohn Youn  * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
205b02038faSJohn Youn  *
206b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
207b02038faSJohn Youn  */
208b02038faSJohn Youn static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
209b02038faSJohn Youn {
210f25c42b8SGevorg Sahakyan 	u32 intmsk = dwc2_readl(hsotg, GINTMSK);
211b02038faSJohn Youn 
212b02038faSJohn Youn 	/* Disable host mode interrupts without disturbing common interrupts */
213b02038faSJohn Youn 	intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
214b02038faSJohn Youn 		    GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
215f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
216b02038faSJohn Youn }
217b02038faSJohn Youn 
218b02038faSJohn Youn /*
219b02038faSJohn Youn  * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
220b02038faSJohn Youn  * For system that have a total fifo depth that is smaller than the default
221b02038faSJohn Youn  * RX + TX fifo size.
222b02038faSJohn Youn  *
223b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
224b02038faSJohn Youn  */
225b02038faSJohn Youn static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
226b02038faSJohn Youn {
227bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
228b02038faSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
229b02038faSJohn Youn 	u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
230b02038faSJohn Youn 
231b02038faSJohn Youn 	total_fifo_size = hw->total_fifo_size;
232b02038faSJohn Youn 	rxfsiz = params->host_rx_fifo_size;
233b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size;
234b02038faSJohn Youn 	ptxfsiz = params->host_perio_tx_fifo_size;
235b02038faSJohn Youn 
236b02038faSJohn Youn 	/*
237b02038faSJohn Youn 	 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
238b02038faSJohn Youn 	 * allocation with support for high bandwidth endpoints. Synopsys
239b02038faSJohn Youn 	 * defines MPS(Max Packet size) for a periodic EP=1024, and for
240b02038faSJohn Youn 	 * non-periodic as 512.
241b02038faSJohn Youn 	 */
242b02038faSJohn Youn 	if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
243b02038faSJohn Youn 		/*
244b02038faSJohn Youn 		 * For Buffer DMA mode/Scatter Gather DMA mode
245b02038faSJohn Youn 		 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
246b02038faSJohn Youn 		 * with n = number of host channel.
247b02038faSJohn Youn 		 * 2 * ((1024/4) + 2) = 516
248b02038faSJohn Youn 		 */
249b02038faSJohn Youn 		rxfsiz = 516 + hw->host_channels;
250b02038faSJohn Youn 
251b02038faSJohn Youn 		/*
252b02038faSJohn Youn 		 * min non-periodic tx fifo depth
253b02038faSJohn Youn 		 * 2 * (largest non-periodic USB packet used / 4)
254b02038faSJohn Youn 		 * 2 * (512/4) = 256
255b02038faSJohn Youn 		 */
256b02038faSJohn Youn 		nptxfsiz = 256;
257b02038faSJohn Youn 
258b02038faSJohn Youn 		/*
259b02038faSJohn Youn 		 * min periodic tx fifo depth
260b02038faSJohn Youn 		 * (largest packet size*MC)/4
261b02038faSJohn Youn 		 * (1024 * 3)/4 = 768
262b02038faSJohn Youn 		 */
263b02038faSJohn Youn 		ptxfsiz = 768;
264b02038faSJohn Youn 
265b02038faSJohn Youn 		params->host_rx_fifo_size = rxfsiz;
266b02038faSJohn Youn 		params->host_nperio_tx_fifo_size = nptxfsiz;
267b02038faSJohn Youn 		params->host_perio_tx_fifo_size = ptxfsiz;
268b02038faSJohn Youn 	}
269b02038faSJohn Youn 
270b02038faSJohn Youn 	/*
271b02038faSJohn Youn 	 * If the summation of RX, NPTX and PTX fifo sizes is still
272b02038faSJohn Youn 	 * bigger than the total_fifo_size, then we have a problem.
273b02038faSJohn Youn 	 *
274b02038faSJohn Youn 	 * We won't be able to allocate as many endpoints. Right now,
275b02038faSJohn Youn 	 * we're just printing an error message, but ideally this FIFO
276b02038faSJohn Youn 	 * allocation algorithm would be improved in the future.
277b02038faSJohn Youn 	 *
278b02038faSJohn Youn 	 * FIXME improve this FIFO allocation algorithm.
279b02038faSJohn Youn 	 */
280b02038faSJohn Youn 	if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
281b02038faSJohn Youn 		dev_err(hsotg->dev, "invalid fifo sizes\n");
282b02038faSJohn Youn }
283b02038faSJohn Youn 
284b02038faSJohn Youn static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
285b02038faSJohn Youn {
286bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
287b02038faSJohn Youn 	u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
288b02038faSJohn Youn 
289b02038faSJohn Youn 	if (!params->enable_dynamic_fifo)
290b02038faSJohn Youn 		return;
291b02038faSJohn Youn 
292b02038faSJohn Youn 	dwc2_calculate_dynamic_fifo(hsotg);
293b02038faSJohn Youn 
294b02038faSJohn Youn 	/* Rx FIFO */
295f25c42b8SGevorg Sahakyan 	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
296b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
297b02038faSJohn Youn 	grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
298b02038faSJohn Youn 	grxfsiz |= params->host_rx_fifo_size <<
299b02038faSJohn Youn 		   GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
300f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
301b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
302f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GRXFSIZ));
303b02038faSJohn Youn 
304b02038faSJohn Youn 	/* Non-periodic Tx FIFO */
305b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
306f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
307b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size <<
308b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
309b02038faSJohn Youn 	nptxfsiz |= params->host_rx_fifo_size <<
310b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
311f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
312b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
313f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
314b02038faSJohn Youn 
315b02038faSJohn Youn 	/* Periodic Tx FIFO */
316b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
317f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
318b02038faSJohn Youn 	hptxfsiz = params->host_perio_tx_fifo_size <<
319b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
320b02038faSJohn Youn 	hptxfsiz |= (params->host_rx_fifo_size +
321b02038faSJohn Youn 		     params->host_nperio_tx_fifo_size) <<
322b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
323f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
324b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
325f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
326b02038faSJohn Youn 
32795832c00SJohn Youn 	if (hsotg->params.en_multiple_tx_fifo &&
328e1f411d1SSevak Arakelyan 	    hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
329b02038faSJohn Youn 		/*
330e1f411d1SSevak Arakelyan 		 * This feature was implemented in 2.91a version
331b02038faSJohn Youn 		 * Global DFIFOCFG calculation for Host mode -
332b02038faSJohn Youn 		 * include RxFIFO, NPTXFIFO and HPTXFIFO
333b02038faSJohn Youn 		 */
334f25c42b8SGevorg Sahakyan 		dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
335b02038faSJohn Youn 		dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
336b02038faSJohn Youn 		dfifocfg |= (params->host_rx_fifo_size +
337b02038faSJohn Youn 			     params->host_nperio_tx_fifo_size +
338b02038faSJohn Youn 			     params->host_perio_tx_fifo_size) <<
339b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_SHIFT &
340b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_MASK;
341f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
342b02038faSJohn Youn 	}
343b02038faSJohn Youn }
344b02038faSJohn Youn 
345b02038faSJohn Youn /**
346b02038faSJohn Youn  * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
347b02038faSJohn Youn  * the HFIR register according to PHY type and speed
348b02038faSJohn Youn  *
349b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
350b02038faSJohn Youn  *
351b02038faSJohn Youn  * NOTE: The caller can modify the value of the HFIR register only after the
352b02038faSJohn Youn  * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
353b02038faSJohn Youn  * has been set
354b02038faSJohn Youn  */
355b02038faSJohn Youn u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
356b02038faSJohn Youn {
357b02038faSJohn Youn 	u32 usbcfg;
358b02038faSJohn Youn 	u32 hprt0;
359b02038faSJohn Youn 	int clock = 60;	/* default value */
360b02038faSJohn Youn 
361f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
362f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
363b02038faSJohn Youn 
364b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
365b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_PHYIF16))
366b02038faSJohn Youn 		clock = 60;
367b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
368b02038faSJohn Youn 	    GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
369b02038faSJohn Youn 		clock = 48;
370b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
371b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
372b02038faSJohn Youn 		clock = 30;
373b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
374b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
375b02038faSJohn Youn 		clock = 60;
376b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
377b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
378b02038faSJohn Youn 		clock = 48;
379b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
380b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
381b02038faSJohn Youn 		clock = 48;
382b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) &&
383b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
384b02038faSJohn Youn 		clock = 48;
385b02038faSJohn Youn 
386b02038faSJohn Youn 	if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
387b02038faSJohn Youn 		/* High speed case */
388b02038faSJohn Youn 		return 125 * clock - 1;
389b02038faSJohn Youn 
390b02038faSJohn Youn 	/* FS/LS case */
391b02038faSJohn Youn 	return 1000 * clock - 1;
392b02038faSJohn Youn }
393b02038faSJohn Youn 
394b02038faSJohn Youn /**
395b02038faSJohn Youn  * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
396b02038faSJohn Youn  * buffer
397b02038faSJohn Youn  *
3986fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of DWC_otg controller
399b02038faSJohn Youn  * @dest:    Destination buffer for the packet
400b02038faSJohn Youn  * @bytes:   Number of bytes to copy to the destination
401b02038faSJohn Youn  */
402b02038faSJohn Youn void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
403b02038faSJohn Youn {
404b02038faSJohn Youn 	u32 *data_buf = (u32 *)dest;
405b02038faSJohn Youn 	int word_count = (bytes + 3) / 4;
406b02038faSJohn Youn 	int i;
407b02038faSJohn Youn 
408b02038faSJohn Youn 	/*
409b02038faSJohn Youn 	 * Todo: Account for the case where dest is not dword aligned. This
410b02038faSJohn Youn 	 * requires reading data from the FIFO into a u32 temp buffer, then
411b02038faSJohn Youn 	 * moving it into the data buffer.
412b02038faSJohn Youn 	 */
413b02038faSJohn Youn 
414b02038faSJohn Youn 	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
415b02038faSJohn Youn 
416b02038faSJohn Youn 	for (i = 0; i < word_count; i++, data_buf++)
417f25c42b8SGevorg Sahakyan 		*data_buf = dwc2_readl(hsotg, HCFIFO(0));
418b02038faSJohn Youn }
419b02038faSJohn Youn 
420197ba5f4SPaul Zimmerman /**
421197ba5f4SPaul Zimmerman  * dwc2_dump_channel_info() - Prints the state of a host channel
422197ba5f4SPaul Zimmerman  *
423197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
424197ba5f4SPaul Zimmerman  * @chan:  Pointer to the channel to dump
425197ba5f4SPaul Zimmerman  *
426197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
427197ba5f4SPaul Zimmerman  *
428197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
429197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
430197ba5f4SPaul Zimmerman  */
431197ba5f4SPaul Zimmerman static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
432197ba5f4SPaul Zimmerman 				   struct dwc2_host_chan *chan)
433197ba5f4SPaul Zimmerman {
434197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
435bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
436197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
437197ba5f4SPaul Zimmerman 	u32 hcchar;
438197ba5f4SPaul Zimmerman 	u32 hcsplt;
439197ba5f4SPaul Zimmerman 	u32 hctsiz;
440197ba5f4SPaul Zimmerman 	u32 hc_dma;
441197ba5f4SPaul Zimmerman 	int i;
442197ba5f4SPaul Zimmerman 
443b02038faSJohn Youn 	if (!chan)
444197ba5f4SPaul Zimmerman 		return;
445197ba5f4SPaul Zimmerman 
446f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
447f25c42b8SGevorg Sahakyan 	hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
448f25c42b8SGevorg Sahakyan 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
449f25c42b8SGevorg Sahakyan 	hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
450197ba5f4SPaul Zimmerman 
451197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Assigned to channel %p:\n", chan);
452197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hcchar 0x%08x, hcsplt 0x%08x\n",
453197ba5f4SPaul Zimmerman 		hcchar, hcsplt);
454197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hctsiz 0x%08x, hc_dma 0x%08x\n",
455197ba5f4SPaul Zimmerman 		hctsiz, hc_dma);
456197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
457197ba5f4SPaul Zimmerman 		chan->dev_addr, chan->ep_num, chan->ep_is_in);
458197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
459197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
460197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    data_pid_start: %d\n", chan->data_pid_start);
461197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_started: %d\n", chan->xfer_started);
462197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
463197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
464197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
465197ba5f4SPaul Zimmerman 		(unsigned long)chan->xfer_dma);
466197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
467197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
468197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP inactive sched:\n");
469197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
470197ba5f4SPaul Zimmerman 			    qh_list_entry)
471197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
47238d2b5fbSDouglas Anderson 	dev_dbg(hsotg->dev, "  NP waiting sched:\n");
47338d2b5fbSDouglas Anderson 	list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
47438d2b5fbSDouglas Anderson 			    qh_list_entry)
47538d2b5fbSDouglas Anderson 		dev_dbg(hsotg->dev, "    %p\n", qh);
476197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP active sched:\n");
477197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
478197ba5f4SPaul Zimmerman 			    qh_list_entry)
479197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
480197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Channels:\n");
481197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
482197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
483197ba5f4SPaul Zimmerman 
484197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %2d: %p\n", i, chan);
485197ba5f4SPaul Zimmerman 	}
486197ba5f4SPaul Zimmerman #endif /* VERBOSE_DEBUG */
487197ba5f4SPaul Zimmerman }
488197ba5f4SPaul Zimmerman 
4894411bebaSRazmik Karapetyan static int _dwc2_hcd_start(struct usb_hcd *hcd);
4904411bebaSRazmik Karapetyan 
4914411bebaSRazmik Karapetyan static void dwc2_host_start(struct dwc2_hsotg *hsotg)
4924411bebaSRazmik Karapetyan {
4934411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
4944411bebaSRazmik Karapetyan 
4954411bebaSRazmik Karapetyan 	hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
4964411bebaSRazmik Karapetyan 	_dwc2_hcd_start(hcd);
4974411bebaSRazmik Karapetyan }
4984411bebaSRazmik Karapetyan 
4994411bebaSRazmik Karapetyan static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
5004411bebaSRazmik Karapetyan {
5014411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
5024411bebaSRazmik Karapetyan 
5034411bebaSRazmik Karapetyan 	hcd->self.is_b_host = 0;
5044411bebaSRazmik Karapetyan }
5054411bebaSRazmik Karapetyan 
5064411bebaSRazmik Karapetyan static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
5074411bebaSRazmik Karapetyan 			       int *hub_addr, int *hub_port)
5084411bebaSRazmik Karapetyan {
5094411bebaSRazmik Karapetyan 	struct urb *urb = context;
5104411bebaSRazmik Karapetyan 
5114411bebaSRazmik Karapetyan 	if (urb->dev->tt)
5124411bebaSRazmik Karapetyan 		*hub_addr = urb->dev->tt->hub->devnum;
5134411bebaSRazmik Karapetyan 	else
5144411bebaSRazmik Karapetyan 		*hub_addr = 0;
5154411bebaSRazmik Karapetyan 	*hub_port = urb->dev->ttport;
5164411bebaSRazmik Karapetyan }
5174411bebaSRazmik Karapetyan 
518197ba5f4SPaul Zimmerman /*
519b02038faSJohn Youn  * =========================================================================
520b02038faSJohn Youn  *  Low Level Host Channel Access Functions
521b02038faSJohn Youn  * =========================================================================
522b02038faSJohn Youn  */
523b02038faSJohn Youn 
524b02038faSJohn Youn static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
525b02038faSJohn Youn 				      struct dwc2_host_chan *chan)
526b02038faSJohn Youn {
527b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
528b02038faSJohn Youn 
529b02038faSJohn Youn 	switch (chan->ep_type) {
530b02038faSJohn Youn 	case USB_ENDPOINT_XFER_CONTROL:
531b02038faSJohn Youn 	case USB_ENDPOINT_XFER_BULK:
532b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "control/bulk\n");
533b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
534b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
535b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
536b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
537b02038faSJohn Youn 		if (chan->ep_is_in) {
538b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
539b02038faSJohn Youn 		} else {
540b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
541b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NYET;
542b02038faSJohn Youn 			if (chan->do_ping)
543b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
544b02038faSJohn Youn 		}
545b02038faSJohn Youn 
546b02038faSJohn Youn 		if (chan->do_split) {
547b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
548b02038faSJohn Youn 			if (chan->complete_split)
549b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
550b02038faSJohn Youn 			else
551b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
552b02038faSJohn Youn 		}
553b02038faSJohn Youn 
554b02038faSJohn Youn 		if (chan->error_state)
555b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
556b02038faSJohn Youn 		break;
557b02038faSJohn Youn 
558b02038faSJohn Youn 	case USB_ENDPOINT_XFER_INT:
559b02038faSJohn Youn 		if (dbg_perio())
560b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "intr\n");
561b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
562b02038faSJohn Youn 		hcintmsk |= HCINTMSK_NAK;
563b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
564b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
565b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
566b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
567b02038faSJohn Youn 
568b02038faSJohn Youn 		if (chan->ep_is_in)
569b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
570b02038faSJohn Youn 		if (chan->error_state)
571b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
572b02038faSJohn Youn 		if (chan->do_split) {
573b02038faSJohn Youn 			if (chan->complete_split)
574b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
575b02038faSJohn Youn 			else
576b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
577b02038faSJohn Youn 		}
578b02038faSJohn Youn 		break;
579b02038faSJohn Youn 
580b02038faSJohn Youn 	case USB_ENDPOINT_XFER_ISOC:
581b02038faSJohn Youn 		if (dbg_perio())
582b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "isoc\n");
583b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
584b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
585b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
586b02038faSJohn Youn 
587b02038faSJohn Youn 		if (chan->ep_is_in) {
588b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XACTERR;
589b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
590b02038faSJohn Youn 		}
591b02038faSJohn Youn 		break;
592b02038faSJohn Youn 	default:
593b02038faSJohn Youn 		dev_err(hsotg->dev, "## Unknown EP type ##\n");
594b02038faSJohn Youn 		break;
595b02038faSJohn Youn 	}
596b02038faSJohn Youn 
597f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
598b02038faSJohn Youn 	if (dbg_hc(chan))
599b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
600b02038faSJohn Youn }
601b02038faSJohn Youn 
602b02038faSJohn Youn static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
603b02038faSJohn Youn 				    struct dwc2_host_chan *chan)
604b02038faSJohn Youn {
605b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
606b02038faSJohn Youn 
607b02038faSJohn Youn 	/*
608b02038faSJohn Youn 	 * For Descriptor DMA mode core halts the channel on AHB error.
609b02038faSJohn Youn 	 * Interrupt is not required.
610b02038faSJohn Youn 	 */
61195832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
612b02038faSJohn Youn 		if (dbg_hc(chan))
613b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
614b02038faSJohn Youn 		hcintmsk |= HCINTMSK_AHBERR;
615b02038faSJohn Youn 	} else {
616b02038faSJohn Youn 		if (dbg_hc(chan))
617b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA enabled\n");
618b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
619b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XFERCOMPL;
620b02038faSJohn Youn 	}
621b02038faSJohn Youn 
622b02038faSJohn Youn 	if (chan->error_state && !chan->do_split &&
623b02038faSJohn Youn 	    chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
624b02038faSJohn Youn 		if (dbg_hc(chan))
625b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "setting ACK\n");
626b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
627b02038faSJohn Youn 		if (chan->ep_is_in) {
628b02038faSJohn Youn 			hcintmsk |= HCINTMSK_DATATGLERR;
629b02038faSJohn Youn 			if (chan->ep_type != USB_ENDPOINT_XFER_INT)
630b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NAK;
631b02038faSJohn Youn 		}
632b02038faSJohn Youn 	}
633b02038faSJohn Youn 
634f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
635b02038faSJohn Youn 	if (dbg_hc(chan))
636b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
637b02038faSJohn Youn }
638b02038faSJohn Youn 
639b02038faSJohn Youn static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
640b02038faSJohn Youn 				struct dwc2_host_chan *chan)
641b02038faSJohn Youn {
642b02038faSJohn Youn 	u32 intmsk;
643b02038faSJohn Youn 
64495832c00SJohn Youn 	if (hsotg->params.host_dma) {
645b02038faSJohn Youn 		if (dbg_hc(chan))
646b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
647b02038faSJohn Youn 		dwc2_hc_enable_dma_ints(hsotg, chan);
648b02038faSJohn Youn 	} else {
649b02038faSJohn Youn 		if (dbg_hc(chan))
650b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA disabled\n");
651b02038faSJohn Youn 		dwc2_hc_enable_slave_ints(hsotg, chan);
652b02038faSJohn Youn 	}
653b02038faSJohn Youn 
654b02038faSJohn Youn 	/* Enable the top level host channel interrupt */
655f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, HAINTMSK);
656b02038faSJohn Youn 	intmsk |= 1 << chan->hc_num;
657f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, HAINTMSK);
658b02038faSJohn Youn 	if (dbg_hc(chan))
659b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
660b02038faSJohn Youn 
661b02038faSJohn Youn 	/* Make sure host channel interrupts are enabled */
662f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
663b02038faSJohn Youn 	intmsk |= GINTSTS_HCHINT;
664f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
665b02038faSJohn Youn 	if (dbg_hc(chan))
666b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
667b02038faSJohn Youn }
668b02038faSJohn Youn 
669b02038faSJohn Youn /**
670b02038faSJohn Youn  * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
671b02038faSJohn Youn  * a specific endpoint
672b02038faSJohn Youn  *
673b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
674b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
675b02038faSJohn Youn  *
676b02038faSJohn Youn  * The HCCHARn register is set up with the characteristics specified in chan.
677b02038faSJohn Youn  * Host channel interrupts that may need to be serviced while this transfer is
678b02038faSJohn Youn  * in progress are enabled.
679b02038faSJohn Youn  */
680b02038faSJohn Youn static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
681b02038faSJohn Youn {
682b02038faSJohn Youn 	u8 hc_num = chan->hc_num;
683b02038faSJohn Youn 	u32 hcintmsk;
684b02038faSJohn Youn 	u32 hcchar;
685b02038faSJohn Youn 	u32 hcsplt = 0;
686b02038faSJohn Youn 
687b02038faSJohn Youn 	if (dbg_hc(chan))
688b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
689b02038faSJohn Youn 
690b02038faSJohn Youn 	/* Clear old interrupt conditions for this host channel */
691b02038faSJohn Youn 	hcintmsk = 0xffffffff;
692b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
693f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
694b02038faSJohn Youn 
695b02038faSJohn Youn 	/* Enable channel interrupts required for this transfer */
696b02038faSJohn Youn 	dwc2_hc_enable_ints(hsotg, chan);
697b02038faSJohn Youn 
698b02038faSJohn Youn 	/*
699b02038faSJohn Youn 	 * Program the HCCHARn register with the endpoint characteristics for
700b02038faSJohn Youn 	 * the current transfer
701b02038faSJohn Youn 	 */
702b02038faSJohn Youn 	hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
703b02038faSJohn Youn 	hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
704b02038faSJohn Youn 	if (chan->ep_is_in)
705b02038faSJohn Youn 		hcchar |= HCCHAR_EPDIR;
706b02038faSJohn Youn 	if (chan->speed == USB_SPEED_LOW)
707b02038faSJohn Youn 		hcchar |= HCCHAR_LSPDDEV;
708b02038faSJohn Youn 	hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
709b02038faSJohn Youn 	hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
710f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
711b02038faSJohn Youn 	if (dbg_hc(chan)) {
712b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
713b02038faSJohn Youn 			 hc_num, hcchar);
714b02038faSJohn Youn 
715b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n",
716b02038faSJohn Youn 			 __func__, hc_num);
717b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Dev Addr: %d\n",
718b02038faSJohn Youn 			 chan->dev_addr);
719b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Num: %d\n",
720b02038faSJohn Youn 			 chan->ep_num);
721b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is In: %d\n",
722b02038faSJohn Youn 			 chan->ep_is_in);
723b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is Low Speed: %d\n",
724b02038faSJohn Youn 			 chan->speed == USB_SPEED_LOW);
725b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Type: %d\n",
726b02038faSJohn Youn 			 chan->ep_type);
727b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Max Pkt: %d\n",
728b02038faSJohn Youn 			 chan->max_packet);
729b02038faSJohn Youn 	}
730b02038faSJohn Youn 
731b02038faSJohn Youn 	/* Program the HCSPLT register for SPLITs */
732b02038faSJohn Youn 	if (chan->do_split) {
733b02038faSJohn Youn 		if (dbg_hc(chan))
734b02038faSJohn Youn 			dev_vdbg(hsotg->dev,
735b02038faSJohn Youn 				 "Programming HC %d with split --> %s\n",
736b02038faSJohn Youn 				 hc_num,
737b02038faSJohn Youn 				 chan->complete_split ? "CSPLIT" : "SSPLIT");
738b02038faSJohn Youn 		if (chan->complete_split)
739b02038faSJohn Youn 			hcsplt |= HCSPLT_COMPSPLT;
740b02038faSJohn Youn 		hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
741b02038faSJohn Youn 			  HCSPLT_XACTPOS_MASK;
742b02038faSJohn Youn 		hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
743b02038faSJohn Youn 			  HCSPLT_HUBADDR_MASK;
744b02038faSJohn Youn 		hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
745b02038faSJohn Youn 			  HCSPLT_PRTADDR_MASK;
746b02038faSJohn Youn 		if (dbg_hc(chan)) {
747b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  comp split %d\n",
748b02038faSJohn Youn 				 chan->complete_split);
749b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xact pos %d\n",
750b02038faSJohn Youn 				 chan->xact_pos);
751b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub addr %d\n",
752b02038faSJohn Youn 				 chan->hub_addr);
753b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub port %d\n",
754b02038faSJohn Youn 				 chan->hub_port);
755b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  is_in %d\n",
756b02038faSJohn Youn 				 chan->ep_is_in);
757b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  Max Pkt %d\n",
758b02038faSJohn Youn 				 chan->max_packet);
759b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xferlen %d\n",
760b02038faSJohn Youn 				 chan->xfer_len);
761b02038faSJohn Youn 		}
762b02038faSJohn Youn 	}
763b02038faSJohn Youn 
764f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
765b02038faSJohn Youn }
766b02038faSJohn Youn 
767b02038faSJohn Youn /**
768b02038faSJohn Youn  * dwc2_hc_halt() - Attempts to halt a host channel
769b02038faSJohn Youn  *
770b02038faSJohn Youn  * @hsotg:       Controller register interface
771b02038faSJohn Youn  * @chan:        Host channel to halt
772b02038faSJohn Youn  * @halt_status: Reason for halting the channel
773b02038faSJohn Youn  *
774b02038faSJohn Youn  * This function should only be called in Slave mode or to abort a transfer in
775b02038faSJohn Youn  * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
776b02038faSJohn Youn  * controller halts the channel when the transfer is complete or a condition
777b02038faSJohn Youn  * occurs that requires application intervention.
778b02038faSJohn Youn  *
779b02038faSJohn Youn  * In slave mode, checks for a free request queue entry, then sets the Channel
780b02038faSJohn Youn  * Enable and Channel Disable bits of the Host Channel Characteristics
781b02038faSJohn Youn  * register of the specified channel to intiate the halt. If there is no free
782b02038faSJohn Youn  * request queue entry, sets only the Channel Disable bit of the HCCHARn
783b02038faSJohn Youn  * register to flush requests for this channel. In the latter case, sets a
784b02038faSJohn Youn  * flag to indicate that the host channel needs to be halted when a request
785b02038faSJohn Youn  * queue slot is open.
786b02038faSJohn Youn  *
787b02038faSJohn Youn  * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
788b02038faSJohn Youn  * HCCHARn register. The controller ensures there is space in the request
789b02038faSJohn Youn  * queue before submitting the halt request.
790b02038faSJohn Youn  *
791b02038faSJohn Youn  * Some time may elapse before the core flushes any posted requests for this
792b02038faSJohn Youn  * host channel and halts. The Channel Halted interrupt handler completes the
793b02038faSJohn Youn  * deactivation of the host channel.
794b02038faSJohn Youn  */
795b02038faSJohn Youn void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
796b02038faSJohn Youn 		  enum dwc2_halt_status halt_status)
797b02038faSJohn Youn {
798b02038faSJohn Youn 	u32 nptxsts, hptxsts, hcchar;
799b02038faSJohn Youn 
800b02038faSJohn Youn 	if (dbg_hc(chan))
801b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
802a82c7abdSMinas Harutyunyan 
803a82c7abdSMinas Harutyunyan 	/*
804a82c7abdSMinas Harutyunyan 	 * In buffer DMA or external DMA mode channel can't be halted
805a82c7abdSMinas Harutyunyan 	 * for non-split periodic channels. At the end of the next
806a82c7abdSMinas Harutyunyan 	 * uframe/frame (in the worst case), the core generates a channel
807a82c7abdSMinas Harutyunyan 	 * halted and disables the channel automatically.
808a82c7abdSMinas Harutyunyan 	 */
809a82c7abdSMinas Harutyunyan 	if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
810a82c7abdSMinas Harutyunyan 	    hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
811a82c7abdSMinas Harutyunyan 		if (!chan->do_split &&
812a82c7abdSMinas Harutyunyan 		    (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
813a82c7abdSMinas Harutyunyan 		     chan->ep_type == USB_ENDPOINT_XFER_INT)) {
814a82c7abdSMinas Harutyunyan 			dev_err(hsotg->dev, "%s() Channel can't be halted\n",
815a82c7abdSMinas Harutyunyan 				__func__);
816a82c7abdSMinas Harutyunyan 			return;
817a82c7abdSMinas Harutyunyan 		}
818a82c7abdSMinas Harutyunyan 	}
819a82c7abdSMinas Harutyunyan 
820b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
821b02038faSJohn Youn 		dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
822b02038faSJohn Youn 
823b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
824b02038faSJohn Youn 	    halt_status == DWC2_HC_XFER_AHB_ERR) {
825b02038faSJohn Youn 		/*
826b02038faSJohn Youn 		 * Disable all channel interrupts except Ch Halted. The QTD
827b02038faSJohn Youn 		 * and QH state associated with this transfer has been cleared
828b02038faSJohn Youn 		 * (in the case of URB_DEQUEUE), so the channel needs to be
829b02038faSJohn Youn 		 * shut down carefully to prevent crashes.
830b02038faSJohn Youn 		 */
831b02038faSJohn Youn 		u32 hcintmsk = HCINTMSK_CHHLTD;
832b02038faSJohn Youn 
833b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "dequeue/error\n");
834f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
835b02038faSJohn Youn 
836b02038faSJohn Youn 		/*
837b02038faSJohn Youn 		 * Make sure no other interrupts besides halt are currently
838b02038faSJohn Youn 		 * pending. Handling another interrupt could cause a crash due
839b02038faSJohn Youn 		 * to the QTD and QH state.
840b02038faSJohn Youn 		 */
841f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
842b02038faSJohn Youn 
843b02038faSJohn Youn 		/*
844b02038faSJohn Youn 		 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
845b02038faSJohn Youn 		 * even if the channel was already halted for some other
846b02038faSJohn Youn 		 * reason
847b02038faSJohn Youn 		 */
848b02038faSJohn Youn 		chan->halt_status = halt_status;
849b02038faSJohn Youn 
850f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
851b02038faSJohn Youn 		if (!(hcchar & HCCHAR_CHENA)) {
852b02038faSJohn Youn 			/*
853b02038faSJohn Youn 			 * The channel is either already halted or it hasn't
854b02038faSJohn Youn 			 * started yet. In DMA mode, the transfer may halt if
855b02038faSJohn Youn 			 * it finishes normally or a condition occurs that
856b02038faSJohn Youn 			 * requires driver intervention. Don't want to halt
857b02038faSJohn Youn 			 * the channel again. In either Slave or DMA mode,
858b02038faSJohn Youn 			 * it's possible that the transfer has been assigned
859b02038faSJohn Youn 			 * to a channel, but not started yet when an URB is
860b02038faSJohn Youn 			 * dequeued. Don't want to halt a channel that hasn't
861b02038faSJohn Youn 			 * started yet.
862b02038faSJohn Youn 			 */
863b02038faSJohn Youn 			return;
864b02038faSJohn Youn 		}
865b02038faSJohn Youn 	}
866b02038faSJohn Youn 	if (chan->halt_pending) {
867b02038faSJohn Youn 		/*
868b02038faSJohn Youn 		 * A halt has already been issued for this channel. This might
869b02038faSJohn Youn 		 * happen when a transfer is aborted by a higher level in
870b02038faSJohn Youn 		 * the stack.
871b02038faSJohn Youn 		 */
872b02038faSJohn Youn 		dev_vdbg(hsotg->dev,
873b02038faSJohn Youn 			 "*** %s: Channel %d, chan->halt_pending already set ***\n",
874b02038faSJohn Youn 			 __func__, chan->hc_num);
875b02038faSJohn Youn 		return;
876b02038faSJohn Youn 	}
877b02038faSJohn Youn 
878f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
879b02038faSJohn Youn 
880b02038faSJohn Youn 	/* No need to set the bit in DDMA for disabling the channel */
881b02038faSJohn Youn 	/* TODO check it everywhere channel is disabled */
88295832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
883b02038faSJohn Youn 		if (dbg_hc(chan))
884b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
885b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
886b02038faSJohn Youn 	} else {
887b02038faSJohn Youn 		if (dbg_hc(chan))
888b02038faSJohn Youn 			dev_dbg(hsotg->dev, "desc DMA enabled\n");
889b02038faSJohn Youn 	}
890b02038faSJohn Youn 	hcchar |= HCCHAR_CHDIS;
891b02038faSJohn Youn 
89295832c00SJohn Youn 	if (!hsotg->params.host_dma) {
893b02038faSJohn Youn 		if (dbg_hc(chan))
894b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA not enabled\n");
895b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
896b02038faSJohn Youn 
897b02038faSJohn Youn 		/* Check for space in the request queue to issue the halt */
898b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
899b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
900b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "control/bulk\n");
901f25c42b8SGevorg Sahakyan 			nptxsts = dwc2_readl(hsotg, GNPTXSTS);
902b02038faSJohn Youn 			if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
903b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "Disabling channel\n");
904b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
905b02038faSJohn Youn 			}
906b02038faSJohn Youn 		} else {
907b02038faSJohn Youn 			if (dbg_perio())
908b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "isoc/intr\n");
909f25c42b8SGevorg Sahakyan 			hptxsts = dwc2_readl(hsotg, HPTXSTS);
910b02038faSJohn Youn 			if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
911b02038faSJohn Youn 			    hsotg->queuing_high_bandwidth) {
912b02038faSJohn Youn 				if (dbg_perio())
913b02038faSJohn Youn 					dev_vdbg(hsotg->dev, "Disabling channel\n");
914b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
915b02038faSJohn Youn 			}
916b02038faSJohn Youn 		}
917b02038faSJohn Youn 	} else {
918b02038faSJohn Youn 		if (dbg_hc(chan))
919b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
920b02038faSJohn Youn 	}
921b02038faSJohn Youn 
922f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
923b02038faSJohn Youn 	chan->halt_status = halt_status;
924b02038faSJohn Youn 
925b02038faSJohn Youn 	if (hcchar & HCCHAR_CHENA) {
926b02038faSJohn Youn 		if (dbg_hc(chan))
927b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel enabled\n");
928b02038faSJohn Youn 		chan->halt_pending = 1;
929b02038faSJohn Youn 		chan->halt_on_queue = 0;
930b02038faSJohn Youn 	} else {
931b02038faSJohn Youn 		if (dbg_hc(chan))
932b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel disabled\n");
933b02038faSJohn Youn 		chan->halt_on_queue = 1;
934b02038faSJohn Youn 	}
935b02038faSJohn Youn 
936b02038faSJohn Youn 	if (dbg_hc(chan)) {
937b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
938b02038faSJohn Youn 			 chan->hc_num);
939b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 hcchar: 0x%08x\n",
940b02038faSJohn Youn 			 hcchar);
941b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_pending: %d\n",
942b02038faSJohn Youn 			 chan->halt_pending);
943b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_on_queue: %d\n",
944b02038faSJohn Youn 			 chan->halt_on_queue);
945b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_status: %d\n",
946b02038faSJohn Youn 			 chan->halt_status);
947b02038faSJohn Youn 	}
948b02038faSJohn Youn }
949b02038faSJohn Youn 
950b02038faSJohn Youn /**
951b02038faSJohn Youn  * dwc2_hc_cleanup() - Clears the transfer state for a host channel
952b02038faSJohn Youn  *
953b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
954b02038faSJohn Youn  * @chan:  Identifies the host channel to clean up
955b02038faSJohn Youn  *
956b02038faSJohn Youn  * This function is normally called after a transfer is done and the host
957b02038faSJohn Youn  * channel is being released
958b02038faSJohn Youn  */
959b02038faSJohn Youn void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
960b02038faSJohn Youn {
961b02038faSJohn Youn 	u32 hcintmsk;
962b02038faSJohn Youn 
963b02038faSJohn Youn 	chan->xfer_started = 0;
964b02038faSJohn Youn 
965b02038faSJohn Youn 	list_del_init(&chan->split_order_list_entry);
966b02038faSJohn Youn 
967b02038faSJohn Youn 	/*
968b02038faSJohn Youn 	 * Clear channel interrupt enables and any unhandled channel interrupt
969b02038faSJohn Youn 	 * conditions
970b02038faSJohn Youn 	 */
971f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
972b02038faSJohn Youn 	hcintmsk = 0xffffffff;
973b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
974f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
975b02038faSJohn Youn }
976b02038faSJohn Youn 
977b02038faSJohn Youn /**
978b02038faSJohn Youn  * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
979b02038faSJohn Youn  * which frame a periodic transfer should occur
980b02038faSJohn Youn  *
981b02038faSJohn Youn  * @hsotg:  Programming view of DWC_otg controller
982b02038faSJohn Youn  * @chan:   Identifies the host channel to set up and its properties
983b02038faSJohn Youn  * @hcchar: Current value of the HCCHAR register for the specified host channel
984b02038faSJohn Youn  *
985b02038faSJohn Youn  * This function has no effect on non-periodic transfers
986b02038faSJohn Youn  */
987b02038faSJohn Youn static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
988b02038faSJohn Youn 				       struct dwc2_host_chan *chan, u32 *hcchar)
989b02038faSJohn Youn {
990b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
991b02038faSJohn Youn 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
992b02038faSJohn Youn 		int host_speed;
993b02038faSJohn Youn 		int xfer_ns;
994b02038faSJohn Youn 		int xfer_us;
995b02038faSJohn Youn 		int bytes_in_fifo;
996b02038faSJohn Youn 		u16 fifo_space;
997b02038faSJohn Youn 		u16 frame_number;
998b02038faSJohn Youn 		u16 wire_frame;
999b02038faSJohn Youn 
1000b02038faSJohn Youn 		/*
1001b02038faSJohn Youn 		 * Try to figure out if we're an even or odd frame. If we set
1002b02038faSJohn Youn 		 * even and the current frame number is even the the transfer
1003b02038faSJohn Youn 		 * will happen immediately.  Similar if both are odd. If one is
1004b02038faSJohn Youn 		 * even and the other is odd then the transfer will happen when
1005b02038faSJohn Youn 		 * the frame number ticks.
1006b02038faSJohn Youn 		 *
1007b02038faSJohn Youn 		 * There's a bit of a balancing act to get this right.
1008b02038faSJohn Youn 		 * Sometimes we may want to send data in the current frame (AK
1009b02038faSJohn Youn 		 * right away).  We might want to do this if the frame number
1010b02038faSJohn Youn 		 * _just_ ticked, but we might also want to do this in order
1011b02038faSJohn Youn 		 * to continue a split transaction that happened late in a
1012b02038faSJohn Youn 		 * microframe (so we didn't know to queue the next transfer
1013b02038faSJohn Youn 		 * until the frame number had ticked).  The problem is that we
1014b02038faSJohn Youn 		 * need a lot of knowledge to know if there's actually still
1015b02038faSJohn Youn 		 * time to send things or if it would be better to wait until
1016b02038faSJohn Youn 		 * the next frame.
1017b02038faSJohn Youn 		 *
1018b02038faSJohn Youn 		 * We can look at how much time is left in the current frame
1019b02038faSJohn Youn 		 * and make a guess about whether we'll have time to transfer.
1020b02038faSJohn Youn 		 * We'll do that.
1021b02038faSJohn Youn 		 */
1022b02038faSJohn Youn 
1023b02038faSJohn Youn 		/* Get speed host is running at */
1024b02038faSJohn Youn 		host_speed = (chan->speed != USB_SPEED_HIGH &&
1025b02038faSJohn Youn 			      !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1026b02038faSJohn Youn 
1027b02038faSJohn Youn 		/* See how many bytes are in the periodic FIFO right now */
1028f25c42b8SGevorg Sahakyan 		fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
1029b02038faSJohn Youn 			      TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1030b02038faSJohn Youn 		bytes_in_fifo = sizeof(u32) *
1031bea8e86cSJohn Youn 				(hsotg->params.host_perio_tx_fifo_size -
1032b02038faSJohn Youn 				 fifo_space);
1033b02038faSJohn Youn 
1034b02038faSJohn Youn 		/*
1035b02038faSJohn Youn 		 * Roughly estimate bus time for everything in the periodic
1036b02038faSJohn Youn 		 * queue + our new transfer.  This is "rough" because we're
1037b02038faSJohn Youn 		 * using a function that makes takes into account IN/OUT
1038b02038faSJohn Youn 		 * and INT/ISO and we're just slamming in one value for all
1039b02038faSJohn Youn 		 * transfers.  This should be an over-estimate and that should
1040b02038faSJohn Youn 		 * be OK, but we can probably tighten it.
1041b02038faSJohn Youn 		 */
1042b02038faSJohn Youn 		xfer_ns = usb_calc_bus_time(host_speed, false, false,
1043b02038faSJohn Youn 					    chan->xfer_len + bytes_in_fifo);
1044b02038faSJohn Youn 		xfer_us = NS_TO_US(xfer_ns);
1045b02038faSJohn Youn 
1046b02038faSJohn Youn 		/* See what frame number we'll be at by the time we finish */
1047b02038faSJohn Youn 		frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1048b02038faSJohn Youn 
1049b02038faSJohn Youn 		/* This is when we were scheduled to be on the wire */
1050b02038faSJohn Youn 		wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1051b02038faSJohn Youn 
1052b02038faSJohn Youn 		/*
1053b02038faSJohn Youn 		 * If we'd finish _after_ the frame we're scheduled in then
1054b02038faSJohn Youn 		 * it's hopeless.  Just schedule right away and hope for the
1055b02038faSJohn Youn 		 * best.  Note that it _might_ be wise to call back into the
1056b02038faSJohn Youn 		 * scheduler to pick a better frame, but this is better than
1057b02038faSJohn Youn 		 * nothing.
1058b02038faSJohn Youn 		 */
1059b02038faSJohn Youn 		if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1060b02038faSJohn Youn 			dwc2_sch_vdbg(hsotg,
1061b02038faSJohn Youn 				      "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1062b02038faSJohn Youn 				      chan->qh, wire_frame, frame_number,
1063b02038faSJohn Youn 				      dwc2_frame_num_dec(frame_number,
1064b02038faSJohn Youn 							 wire_frame));
1065b02038faSJohn Youn 			wire_frame = frame_number;
1066b02038faSJohn Youn 
1067b02038faSJohn Youn 			/*
1068b02038faSJohn Youn 			 * We picked a different frame number; communicate this
1069b02038faSJohn Youn 			 * back to the scheduler so it doesn't try to schedule
1070b02038faSJohn Youn 			 * another in the same frame.
1071b02038faSJohn Youn 			 *
1072b02038faSJohn Youn 			 * Remember that next_active_frame is 1 before the wire
1073b02038faSJohn Youn 			 * frame.
1074b02038faSJohn Youn 			 */
1075b02038faSJohn Youn 			chan->qh->next_active_frame =
1076b02038faSJohn Youn 				dwc2_frame_num_dec(frame_number, 1);
1077b02038faSJohn Youn 		}
1078b02038faSJohn Youn 
1079b02038faSJohn Youn 		if (wire_frame & 1)
1080b02038faSJohn Youn 			*hcchar |= HCCHAR_ODDFRM;
1081b02038faSJohn Youn 		else
1082b02038faSJohn Youn 			*hcchar &= ~HCCHAR_ODDFRM;
1083b02038faSJohn Youn 	}
1084b02038faSJohn Youn }
1085b02038faSJohn Youn 
1086b02038faSJohn Youn static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1087b02038faSJohn Youn {
1088b02038faSJohn Youn 	/* Set up the initial PID for the transfer */
1089b02038faSJohn Youn 	if (chan->speed == USB_SPEED_HIGH) {
1090b02038faSJohn Youn 		if (chan->ep_is_in) {
1091b02038faSJohn Youn 			if (chan->multi_count == 1)
1092b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1093b02038faSJohn Youn 			else if (chan->multi_count == 2)
1094b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA1;
1095b02038faSJohn Youn 			else
1096b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA2;
1097b02038faSJohn Youn 		} else {
1098b02038faSJohn Youn 			if (chan->multi_count == 1)
1099b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1100b02038faSJohn Youn 			else
1101b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_MDATA;
1102b02038faSJohn Youn 		}
1103b02038faSJohn Youn 	} else {
1104b02038faSJohn Youn 		chan->data_pid_start = DWC2_HC_PID_DATA0;
1105b02038faSJohn Youn 	}
1106b02038faSJohn Youn }
1107b02038faSJohn Youn 
1108b02038faSJohn Youn /**
1109b02038faSJohn Youn  * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1110b02038faSJohn Youn  * the Host Channel
1111b02038faSJohn Youn  *
1112b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1113b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1114b02038faSJohn Youn  *
1115b02038faSJohn Youn  * This function should only be called in Slave mode. For a channel associated
1116b02038faSJohn Youn  * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1117b02038faSJohn Youn  * associated with a periodic EP, the periodic Tx FIFO is written.
1118b02038faSJohn Youn  *
1119b02038faSJohn Youn  * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1120b02038faSJohn Youn  * the number of bytes written to the Tx FIFO.
1121b02038faSJohn Youn  */
1122b02038faSJohn Youn static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1123b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1124b02038faSJohn Youn {
1125b02038faSJohn Youn 	u32 i;
1126b02038faSJohn Youn 	u32 remaining_count;
1127b02038faSJohn Youn 	u32 byte_count;
1128b02038faSJohn Youn 	u32 dword_count;
1129b02038faSJohn Youn 	u32 *data_buf = (u32 *)chan->xfer_buf;
1130b02038faSJohn Youn 
1131b02038faSJohn Youn 	if (dbg_hc(chan))
1132b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1133b02038faSJohn Youn 
1134b02038faSJohn Youn 	remaining_count = chan->xfer_len - chan->xfer_count;
1135b02038faSJohn Youn 	if (remaining_count > chan->max_packet)
1136b02038faSJohn Youn 		byte_count = chan->max_packet;
1137b02038faSJohn Youn 	else
1138b02038faSJohn Youn 		byte_count = remaining_count;
1139b02038faSJohn Youn 
1140b02038faSJohn Youn 	dword_count = (byte_count + 3) / 4;
1141b02038faSJohn Youn 
1142b02038faSJohn Youn 	if (((unsigned long)data_buf & 0x3) == 0) {
1143b02038faSJohn Youn 		/* xfer_buf is DWORD aligned */
1144b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++)
1145f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
1146b02038faSJohn Youn 	} else {
1147b02038faSJohn Youn 		/* xfer_buf is not DWORD aligned */
1148b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++) {
1149b02038faSJohn Youn 			u32 data = data_buf[0] | data_buf[1] << 8 |
1150b02038faSJohn Youn 				   data_buf[2] << 16 | data_buf[3] << 24;
1151f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
1152b02038faSJohn Youn 		}
1153b02038faSJohn Youn 	}
1154b02038faSJohn Youn 
1155b02038faSJohn Youn 	chan->xfer_count += byte_count;
1156b02038faSJohn Youn 	chan->xfer_buf += byte_count;
1157b02038faSJohn Youn }
1158b02038faSJohn Youn 
1159b02038faSJohn Youn /**
1160b02038faSJohn Youn  * dwc2_hc_do_ping() - Starts a PING transfer
1161b02038faSJohn Youn  *
1162b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1163b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1164b02038faSJohn Youn  *
1165b02038faSJohn Youn  * This function should only be called in Slave mode. The Do Ping bit is set in
1166b02038faSJohn Youn  * the HCTSIZ register, then the channel is enabled.
1167b02038faSJohn Youn  */
1168b02038faSJohn Youn static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1169b02038faSJohn Youn 			    struct dwc2_host_chan *chan)
1170b02038faSJohn Youn {
1171b02038faSJohn Youn 	u32 hcchar;
1172b02038faSJohn Youn 	u32 hctsiz;
1173b02038faSJohn Youn 
1174b02038faSJohn Youn 	if (dbg_hc(chan))
1175b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1176b02038faSJohn Youn 			 chan->hc_num);
1177b02038faSJohn Youn 
1178b02038faSJohn Youn 	hctsiz = TSIZ_DOPNG;
1179b02038faSJohn Youn 	hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1180f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1181b02038faSJohn Youn 
1182f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1183b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1184b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1185f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1186b02038faSJohn Youn }
1187b02038faSJohn Youn 
1188b02038faSJohn Youn /**
1189b02038faSJohn Youn  * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1190b02038faSJohn Youn  * channel and starts the transfer
1191b02038faSJohn Youn  *
1192b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1193b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel. The xfer_len value
1194b02038faSJohn Youn  *         may be reduced to accommodate the max widths of the XferSize and
1195b02038faSJohn Youn  *         PktCnt fields in the HCTSIZn register. The multi_count value may be
1196b02038faSJohn Youn  *         changed to reflect the final xfer_len value.
1197b02038faSJohn Youn  *
1198b02038faSJohn Youn  * This function may be called in either Slave mode or DMA mode. In Slave mode,
1199b02038faSJohn Youn  * the caller must ensure that there is sufficient space in the request queue
1200b02038faSJohn Youn  * and Tx Data FIFO.
1201b02038faSJohn Youn  *
1202b02038faSJohn Youn  * For an OUT transfer in Slave mode, it loads a data packet into the
1203b02038faSJohn Youn  * appropriate FIFO. If necessary, additional data packets are loaded in the
1204b02038faSJohn Youn  * Host ISR.
1205b02038faSJohn Youn  *
1206b02038faSJohn Youn  * For an IN transfer in Slave mode, a data packet is requested. The data
1207b02038faSJohn Youn  * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1208b02038faSJohn Youn  * additional data packets are requested in the Host ISR.
1209b02038faSJohn Youn  *
1210b02038faSJohn Youn  * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1211b02038faSJohn Youn  * register along with a packet count of 1 and the channel is enabled. This
1212b02038faSJohn Youn  * causes a single PING transaction to occur. Other fields in HCTSIZ are
1213b02038faSJohn Youn  * simply set to 0 since no data transfer occurs in this case.
1214b02038faSJohn Youn  *
1215b02038faSJohn Youn  * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1216b02038faSJohn Youn  * all the information required to perform the subsequent data transfer. In
1217b02038faSJohn Youn  * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1218b02038faSJohn Youn  * controller performs the entire PING protocol, then starts the data
1219b02038faSJohn Youn  * transfer.
1220b02038faSJohn Youn  */
1221b02038faSJohn Youn static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1222b02038faSJohn Youn 				   struct dwc2_host_chan *chan)
1223b02038faSJohn Youn {
1224bea8e86cSJohn Youn 	u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1225bea8e86cSJohn Youn 	u16 max_hc_pkt_count = hsotg->params.max_packet_count;
1226b02038faSJohn Youn 	u32 hcchar;
1227b02038faSJohn Youn 	u32 hctsiz = 0;
1228b02038faSJohn Youn 	u16 num_packets;
1229b02038faSJohn Youn 	u32 ec_mc;
1230b02038faSJohn Youn 
1231b02038faSJohn Youn 	if (dbg_hc(chan))
1232b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1233b02038faSJohn Youn 
1234b02038faSJohn Youn 	if (chan->do_ping) {
123595832c00SJohn Youn 		if (!hsotg->params.host_dma) {
1236b02038faSJohn Youn 			if (dbg_hc(chan))
1237b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "ping, no DMA\n");
1238b02038faSJohn Youn 			dwc2_hc_do_ping(hsotg, chan);
1239b02038faSJohn Youn 			chan->xfer_started = 1;
1240b02038faSJohn Youn 			return;
1241b02038faSJohn Youn 		}
1242b02038faSJohn Youn 
1243b02038faSJohn Youn 		if (dbg_hc(chan))
1244b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "ping, DMA\n");
1245b02038faSJohn Youn 
1246b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1247b02038faSJohn Youn 	}
1248b02038faSJohn Youn 
1249b02038faSJohn Youn 	if (chan->do_split) {
1250b02038faSJohn Youn 		if (dbg_hc(chan))
1251b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "split\n");
1252b02038faSJohn Youn 		num_packets = 1;
1253b02038faSJohn Youn 
1254b02038faSJohn Youn 		if (chan->complete_split && !chan->ep_is_in)
1255b02038faSJohn Youn 			/*
1256b02038faSJohn Youn 			 * For CSPLIT OUT Transfer, set the size to 0 so the
1257b02038faSJohn Youn 			 * core doesn't expect any data written to the FIFO
1258b02038faSJohn Youn 			 */
1259b02038faSJohn Youn 			chan->xfer_len = 0;
1260b02038faSJohn Youn 		else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1261b02038faSJohn Youn 			chan->xfer_len = chan->max_packet;
1262b02038faSJohn Youn 		else if (!chan->ep_is_in && chan->xfer_len > 188)
1263b02038faSJohn Youn 			chan->xfer_len = 188;
1264b02038faSJohn Youn 
1265b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1266b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1267b02038faSJohn Youn 
1268b02038faSJohn Youn 		/* For split set ec_mc for immediate retries */
1269b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1270b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1271b02038faSJohn Youn 			ec_mc = 3;
1272b02038faSJohn Youn 		else
1273b02038faSJohn Youn 			ec_mc = 1;
1274b02038faSJohn Youn 	} else {
1275b02038faSJohn Youn 		if (dbg_hc(chan))
1276b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "no split\n");
1277b02038faSJohn Youn 		/*
1278b02038faSJohn Youn 		 * Ensure that the transfer length and packet count will fit
1279b02038faSJohn Youn 		 * in the widths allocated for them in the HCTSIZn register
1280b02038faSJohn Youn 		 */
1281b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1282b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1283b02038faSJohn Youn 			/*
1284b02038faSJohn Youn 			 * Make sure the transfer size is no larger than one
1285b02038faSJohn Youn 			 * (micro)frame's worth of data. (A check was done
1286b02038faSJohn Youn 			 * when the periodic transfer was accepted to ensure
1287b02038faSJohn Youn 			 * that a (micro)frame's worth of data can be
1288b02038faSJohn Youn 			 * programmed into a channel.)
1289b02038faSJohn Youn 			 */
1290b02038faSJohn Youn 			u32 max_periodic_len =
1291b02038faSJohn Youn 				chan->multi_count * chan->max_packet;
1292b02038faSJohn Youn 
1293b02038faSJohn Youn 			if (chan->xfer_len > max_periodic_len)
1294b02038faSJohn Youn 				chan->xfer_len = max_periodic_len;
1295b02038faSJohn Youn 		} else if (chan->xfer_len > max_hc_xfer_size) {
1296b02038faSJohn Youn 			/*
1297b02038faSJohn Youn 			 * Make sure that xfer_len is a multiple of max packet
1298b02038faSJohn Youn 			 * size
1299b02038faSJohn Youn 			 */
1300b02038faSJohn Youn 			chan->xfer_len =
1301b02038faSJohn Youn 				max_hc_xfer_size - chan->max_packet + 1;
1302b02038faSJohn Youn 		}
1303b02038faSJohn Youn 
1304b02038faSJohn Youn 		if (chan->xfer_len > 0) {
1305b02038faSJohn Youn 			num_packets = (chan->xfer_len + chan->max_packet - 1) /
1306b02038faSJohn Youn 					chan->max_packet;
1307b02038faSJohn Youn 			if (num_packets > max_hc_pkt_count) {
1308b02038faSJohn Youn 				num_packets = max_hc_pkt_count;
1309b02038faSJohn Youn 				chan->xfer_len = num_packets * chan->max_packet;
1310415fa1c7SGuenter Roeck 			} else if (chan->ep_is_in) {
1311415fa1c7SGuenter Roeck 				/*
1312415fa1c7SGuenter Roeck 				 * Always program an integral # of max packets
1313415fa1c7SGuenter Roeck 				 * for IN transfers.
1314415fa1c7SGuenter Roeck 				 * Note: This assumes that the input buffer is
1315415fa1c7SGuenter Roeck 				 * aligned and sized accordingly.
1316415fa1c7SGuenter Roeck 				 */
1317415fa1c7SGuenter Roeck 				chan->xfer_len = num_packets * chan->max_packet;
1318b02038faSJohn Youn 			}
1319b02038faSJohn Youn 		} else {
1320b02038faSJohn Youn 			/* Need 1 packet for transfer length of 0 */
1321b02038faSJohn Youn 			num_packets = 1;
1322b02038faSJohn Youn 		}
1323b02038faSJohn Youn 
1324b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1325b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1326b02038faSJohn Youn 			/*
1327b02038faSJohn Youn 			 * Make sure that the multi_count field matches the
1328b02038faSJohn Youn 			 * actual transfer length
1329b02038faSJohn Youn 			 */
1330b02038faSJohn Youn 			chan->multi_count = num_packets;
1331b02038faSJohn Youn 
1332b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1333b02038faSJohn Youn 			dwc2_set_pid_isoc(chan);
1334b02038faSJohn Youn 
1335b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1336b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1337b02038faSJohn Youn 
1338b02038faSJohn Youn 		/* The ec_mc gets the multi_count for non-split */
1339b02038faSJohn Youn 		ec_mc = chan->multi_count;
1340b02038faSJohn Youn 	}
1341b02038faSJohn Youn 
1342b02038faSJohn Youn 	chan->start_pkt_count = num_packets;
1343b02038faSJohn Youn 	hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1344b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1345b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1346f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1347b02038faSJohn Youn 	if (dbg_hc(chan)) {
1348b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1349b02038faSJohn Youn 			 hctsiz, chan->hc_num);
1350b02038faSJohn Youn 
1351b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1352b02038faSJohn Youn 			 chan->hc_num);
1353b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Xfer Size: %d\n",
1354b02038faSJohn Youn 			 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1355b02038faSJohn Youn 			 TSIZ_XFERSIZE_SHIFT);
1356b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Num Pkts: %d\n",
1357b02038faSJohn Youn 			 (hctsiz & TSIZ_PKTCNT_MASK) >>
1358b02038faSJohn Youn 			 TSIZ_PKTCNT_SHIFT);
1359b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1360b02038faSJohn Youn 			 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1361b02038faSJohn Youn 			 TSIZ_SC_MC_PID_SHIFT);
1362b02038faSJohn Youn 	}
1363b02038faSJohn Youn 
136495832c00SJohn Youn 	if (hsotg->params.host_dma) {
1365af424a41SWilliam Wu 		dma_addr_t dma_addr;
1366af424a41SWilliam Wu 
1367af424a41SWilliam Wu 		if (chan->align_buf) {
1368af424a41SWilliam Wu 			if (dbg_hc(chan))
1369af424a41SWilliam Wu 				dev_vdbg(hsotg->dev, "align_buf\n");
1370af424a41SWilliam Wu 			dma_addr = chan->align_buf;
1371af424a41SWilliam Wu 		} else {
1372af424a41SWilliam Wu 			dma_addr = chan->xfer_dma;
1373af424a41SWilliam Wu 		}
1374f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
1375af424a41SWilliam Wu 
1376b02038faSJohn Youn 		if (dbg_hc(chan))
1377b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1378af424a41SWilliam Wu 				 (unsigned long)dma_addr, chan->hc_num);
1379b02038faSJohn Youn 	}
1380b02038faSJohn Youn 
1381b02038faSJohn Youn 	/* Start the split */
1382b02038faSJohn Youn 	if (chan->do_split) {
1383f25c42b8SGevorg Sahakyan 		u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
1384b02038faSJohn Youn 
1385b02038faSJohn Youn 		hcsplt |= HCSPLT_SPLTENA;
1386f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
1387b02038faSJohn Youn 	}
1388b02038faSJohn Youn 
1389f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1390b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1391b02038faSJohn Youn 	hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1392b02038faSJohn Youn 	dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1393b02038faSJohn Youn 
1394b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1395b02038faSJohn Youn 		dev_warn(hsotg->dev,
1396b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1397b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1398b02038faSJohn Youn 
1399b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1400b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1401b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1402b02038faSJohn Youn 
1403b02038faSJohn Youn 	if (dbg_hc(chan))
1404b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1405b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1406b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1407b02038faSJohn Youn 
1408f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1409b02038faSJohn Youn 	if (dbg_hc(chan))
1410b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1411b02038faSJohn Youn 			 chan->hc_num);
1412b02038faSJohn Youn 
1413b02038faSJohn Youn 	chan->xfer_started = 1;
1414b02038faSJohn Youn 	chan->requests++;
1415b02038faSJohn Youn 
141695832c00SJohn Youn 	if (!hsotg->params.host_dma &&
1417b02038faSJohn Youn 	    !chan->ep_is_in && chan->xfer_len > 0)
1418b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1419b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1420b02038faSJohn Youn }
1421b02038faSJohn Youn 
1422b02038faSJohn Youn /**
1423b02038faSJohn Youn  * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1424b02038faSJohn Youn  * host channel and starts the transfer in Descriptor DMA mode
1425b02038faSJohn Youn  *
1426b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1427b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1428b02038faSJohn Youn  *
1429b02038faSJohn Youn  * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1430b02038faSJohn Youn  * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1431b02038faSJohn Youn  * with micro-frame bitmap.
1432b02038faSJohn Youn  *
1433b02038faSJohn Youn  * Initializes HCDMA register with descriptor list address and CTD value then
1434b02038faSJohn Youn  * starts the transfer via enabling the channel.
1435b02038faSJohn Youn  */
1436b02038faSJohn Youn void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1437b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1438b02038faSJohn Youn {
1439b02038faSJohn Youn 	u32 hcchar;
1440b02038faSJohn Youn 	u32 hctsiz = 0;
1441b02038faSJohn Youn 
1442b02038faSJohn Youn 	if (chan->do_ping)
1443b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1444b02038faSJohn Youn 
1445b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1446b02038faSJohn Youn 		dwc2_set_pid_isoc(chan);
1447b02038faSJohn Youn 
1448b02038faSJohn Youn 	/* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1449b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1450b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1451b02038faSJohn Youn 
1452b02038faSJohn Youn 	/* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1453b02038faSJohn Youn 	hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1454b02038faSJohn Youn 
1455b02038faSJohn Youn 	/* Non-zero only for high-speed interrupt endpoints */
1456b02038faSJohn Youn 	hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1457b02038faSJohn Youn 
1458b02038faSJohn Youn 	if (dbg_hc(chan)) {
1459b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1460b02038faSJohn Youn 			 chan->hc_num);
1461b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1462b02038faSJohn Youn 			 chan->data_pid_start);
1463b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 NTD: %d\n", chan->ntd - 1);
1464b02038faSJohn Youn 	}
1465b02038faSJohn Youn 
1466f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1467b02038faSJohn Youn 
1468b02038faSJohn Youn 	dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1469b02038faSJohn Youn 				   chan->desc_list_sz, DMA_TO_DEVICE);
1470b02038faSJohn Youn 
1471f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
1472b02038faSJohn Youn 
1473b02038faSJohn Youn 	if (dbg_hc(chan))
1474b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1475b02038faSJohn Youn 			 &chan->desc_list_addr, chan->hc_num);
1476b02038faSJohn Youn 
1477f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1478b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1479b02038faSJohn Youn 	hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1480b02038faSJohn Youn 		  HCCHAR_MULTICNT_MASK;
1481b02038faSJohn Youn 
1482b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1483b02038faSJohn Youn 		dev_warn(hsotg->dev,
1484b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1485b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1486b02038faSJohn Youn 
1487b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1488b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1489b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1490b02038faSJohn Youn 
1491b02038faSJohn Youn 	if (dbg_hc(chan))
1492b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1493b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1494b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1495b02038faSJohn Youn 
1496f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1497b02038faSJohn Youn 	if (dbg_hc(chan))
1498b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1499b02038faSJohn Youn 			 chan->hc_num);
1500b02038faSJohn Youn 
1501b02038faSJohn Youn 	chan->xfer_started = 1;
1502b02038faSJohn Youn 	chan->requests++;
1503b02038faSJohn Youn }
1504b02038faSJohn Youn 
1505b02038faSJohn Youn /**
1506b02038faSJohn Youn  * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1507b02038faSJohn Youn  * a previous call to dwc2_hc_start_transfer()
1508b02038faSJohn Youn  *
1509b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1510b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1511b02038faSJohn Youn  *
1512b02038faSJohn Youn  * The caller must ensure there is sufficient space in the request queue and Tx
1513b02038faSJohn Youn  * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1514b02038faSJohn Youn  * the controller acts autonomously to complete transfers programmed to a host
1515b02038faSJohn Youn  * channel.
1516b02038faSJohn Youn  *
1517b02038faSJohn Youn  * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1518b02038faSJohn Youn  * if there is any data remaining to be queued. For an IN transfer, another
1519b02038faSJohn Youn  * data packet is always requested. For the SETUP phase of a control transfer,
1520b02038faSJohn Youn  * this function does nothing.
1521b02038faSJohn Youn  *
1522b02038faSJohn Youn  * Return: 1 if a new request is queued, 0 if no more requests are required
1523b02038faSJohn Youn  * for this transfer
1524b02038faSJohn Youn  */
1525b02038faSJohn Youn static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1526b02038faSJohn Youn 				     struct dwc2_host_chan *chan)
1527b02038faSJohn Youn {
1528b02038faSJohn Youn 	if (dbg_hc(chan))
1529b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1530b02038faSJohn Youn 			 chan->hc_num);
1531b02038faSJohn Youn 
1532b02038faSJohn Youn 	if (chan->do_split)
1533b02038faSJohn Youn 		/* SPLITs always queue just once per channel */
1534b02038faSJohn Youn 		return 0;
1535b02038faSJohn Youn 
1536b02038faSJohn Youn 	if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1537b02038faSJohn Youn 		/* SETUPs are queued only once since they can't be NAK'd */
1538b02038faSJohn Youn 		return 0;
1539b02038faSJohn Youn 
1540b02038faSJohn Youn 	if (chan->ep_is_in) {
1541b02038faSJohn Youn 		/*
1542b02038faSJohn Youn 		 * Always queue another request for other IN transfers. If
1543b02038faSJohn Youn 		 * back-to-back INs are issued and NAKs are received for both,
1544b02038faSJohn Youn 		 * the driver may still be processing the first NAK when the
1545b02038faSJohn Youn 		 * second NAK is received. When the interrupt handler clears
1546b02038faSJohn Youn 		 * the NAK interrupt for the first NAK, the second NAK will
1547b02038faSJohn Youn 		 * not be seen. So we can't depend on the NAK interrupt
1548b02038faSJohn Youn 		 * handler to requeue a NAK'd request. Instead, IN requests
1549b02038faSJohn Youn 		 * are issued each time this function is called. When the
1550b02038faSJohn Youn 		 * transfer completes, the extra requests for the channel will
1551b02038faSJohn Youn 		 * be flushed.
1552b02038faSJohn Youn 		 */
1553f25c42b8SGevorg Sahakyan 		u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1554b02038faSJohn Youn 
1555b02038faSJohn Youn 		dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1556b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
1557b02038faSJohn Youn 		hcchar &= ~HCCHAR_CHDIS;
1558b02038faSJohn Youn 		if (dbg_hc(chan))
1559b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	 IN xfer: hcchar = 0x%08x\n",
1560b02038faSJohn Youn 				 hcchar);
1561f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1562b02038faSJohn Youn 		chan->requests++;
1563b02038faSJohn Youn 		return 1;
1564b02038faSJohn Youn 	}
1565b02038faSJohn Youn 
1566b02038faSJohn Youn 	/* OUT transfers */
1567b02038faSJohn Youn 
1568b02038faSJohn Youn 	if (chan->xfer_count < chan->xfer_len) {
1569b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1570b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1571f25c42b8SGevorg Sahakyan 			u32 hcchar = dwc2_readl(hsotg,
1572b02038faSJohn Youn 						HCCHAR(chan->hc_num));
1573b02038faSJohn Youn 
1574b02038faSJohn Youn 			dwc2_hc_set_even_odd_frame(hsotg, chan,
1575b02038faSJohn Youn 						   &hcchar);
1576b02038faSJohn Youn 		}
1577b02038faSJohn Youn 
1578b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1579b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1580b02038faSJohn Youn 		chan->requests++;
1581b02038faSJohn Youn 		return 1;
1582b02038faSJohn Youn 	}
1583b02038faSJohn Youn 
1584b02038faSJohn Youn 	return 0;
1585b02038faSJohn Youn }
1586b02038faSJohn Youn 
1587b02038faSJohn Youn /*
1588b02038faSJohn Youn  * =========================================================================
1589b02038faSJohn Youn  *  HCD
1590b02038faSJohn Youn  * =========================================================================
1591b02038faSJohn Youn  */
1592b02038faSJohn Youn 
1593b02038faSJohn Youn /*
1594197ba5f4SPaul Zimmerman  * Processes all the URBs in a single list of QHs. Completes them with
1595197ba5f4SPaul Zimmerman  * -ETIMEDOUT and frees the QTD.
1596197ba5f4SPaul Zimmerman  *
1597197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1598197ba5f4SPaul Zimmerman  */
1599197ba5f4SPaul Zimmerman static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1600197ba5f4SPaul Zimmerman 				      struct list_head *qh_list)
1601197ba5f4SPaul Zimmerman {
1602197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1603197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1604197ba5f4SPaul Zimmerman 
1605197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1606197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1607197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
16082e84da6eSGregory Herrero 			dwc2_host_complete(hsotg, qtd, -ECONNRESET);
1609197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1610197ba5f4SPaul Zimmerman 		}
1611197ba5f4SPaul Zimmerman 	}
1612197ba5f4SPaul Zimmerman }
1613197ba5f4SPaul Zimmerman 
1614197ba5f4SPaul Zimmerman static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1615197ba5f4SPaul Zimmerman 			      struct list_head *qh_list)
1616197ba5f4SPaul Zimmerman {
1617197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1618197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1619197ba5f4SPaul Zimmerman 	unsigned long flags;
1620197ba5f4SPaul Zimmerman 
1621197ba5f4SPaul Zimmerman 	if (!qh_list->next)
1622197ba5f4SPaul Zimmerman 		/* The list hasn't been initialized yet */
1623197ba5f4SPaul Zimmerman 		return;
1624197ba5f4SPaul Zimmerman 
1625197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
1626197ba5f4SPaul Zimmerman 
1627197ba5f4SPaul Zimmerman 	/* Ensure there are no QTDs or URBs left */
1628197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1629197ba5f4SPaul Zimmerman 
1630197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1631197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1632197ba5f4SPaul Zimmerman 
1633197ba5f4SPaul Zimmerman 		/* Free each QTD in the QH's QTD list */
1634197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1635197ba5f4SPaul Zimmerman 					 qtd_list_entry)
1636197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1637197ba5f4SPaul Zimmerman 
163816e80218SDouglas Anderson 		if (qh->channel && qh->channel->qh == qh)
163916e80218SDouglas Anderson 			qh->channel->qh = NULL;
164016e80218SDouglas Anderson 
1641197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
1642197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_free(hsotg, qh);
1643197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
1644197ba5f4SPaul Zimmerman 	}
1645197ba5f4SPaul Zimmerman 
1646197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
1647197ba5f4SPaul Zimmerman }
1648197ba5f4SPaul Zimmerman 
1649197ba5f4SPaul Zimmerman /*
1650197ba5f4SPaul Zimmerman  * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1651197ba5f4SPaul Zimmerman  * and periodic schedules. The QTD associated with each URB is removed from
1652197ba5f4SPaul Zimmerman  * the schedule and freed. This function may be called when a disconnect is
1653197ba5f4SPaul Zimmerman  * detected or when the HCD is being stopped.
1654197ba5f4SPaul Zimmerman  *
1655197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1656197ba5f4SPaul Zimmerman  */
1657197ba5f4SPaul Zimmerman static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1658197ba5f4SPaul Zimmerman {
1659197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
166038d2b5fbSDouglas Anderson 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
1661197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1662197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1663197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1664197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1665197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1666197ba5f4SPaul Zimmerman }
1667197ba5f4SPaul Zimmerman 
1668197ba5f4SPaul Zimmerman /**
1669197ba5f4SPaul Zimmerman  * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1670197ba5f4SPaul Zimmerman  *
1671197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1672197ba5f4SPaul Zimmerman  */
1673197ba5f4SPaul Zimmerman void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1674197ba5f4SPaul Zimmerman {
1675197ba5f4SPaul Zimmerman 	u32 hprt0;
1676197ba5f4SPaul Zimmerman 
1677197ba5f4SPaul Zimmerman 	if (hsotg->op_state == OTG_STATE_B_HOST) {
1678197ba5f4SPaul Zimmerman 		/*
1679197ba5f4SPaul Zimmerman 		 * Reset the port. During a HNP mode switch the reset
1680197ba5f4SPaul Zimmerman 		 * needs to occur within 1ms and have a duration of at
1681197ba5f4SPaul Zimmerman 		 * least 50ms.
1682197ba5f4SPaul Zimmerman 		 */
1683197ba5f4SPaul Zimmerman 		hprt0 = dwc2_read_hprt0(hsotg);
1684197ba5f4SPaul Zimmerman 		hprt0 |= HPRT0_RST;
1685f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
1686197ba5f4SPaul Zimmerman 	}
1687197ba5f4SPaul Zimmerman 
1688197ba5f4SPaul Zimmerman 	queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1689197ba5f4SPaul Zimmerman 			   msecs_to_jiffies(50));
1690197ba5f4SPaul Zimmerman }
1691197ba5f4SPaul Zimmerman 
1692197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1693197ba5f4SPaul Zimmerman static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1694197ba5f4SPaul Zimmerman {
1695bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
1696197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
1697197ba5f4SPaul Zimmerman 	u32 hcchar;
1698197ba5f4SPaul Zimmerman 	int i;
1699197ba5f4SPaul Zimmerman 
170095832c00SJohn Youn 	if (!hsotg->params.host_dma) {
1701197ba5f4SPaul Zimmerman 		/* Flush out any channel requests in slave mode */
1702197ba5f4SPaul Zimmerman 		for (i = 0; i < num_channels; i++) {
1703197ba5f4SPaul Zimmerman 			channel = hsotg->hc_ptr_array[i];
1704197ba5f4SPaul Zimmerman 			if (!list_empty(&channel->hc_list_entry))
1705197ba5f4SPaul Zimmerman 				continue;
1706f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
1707197ba5f4SPaul Zimmerman 			if (hcchar & HCCHAR_CHENA) {
1708197ba5f4SPaul Zimmerman 				hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1709197ba5f4SPaul Zimmerman 				hcchar |= HCCHAR_CHDIS;
1710f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
1711197ba5f4SPaul Zimmerman 			}
1712197ba5f4SPaul Zimmerman 		}
1713197ba5f4SPaul Zimmerman 	}
1714197ba5f4SPaul Zimmerman 
1715197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
1716197ba5f4SPaul Zimmerman 		channel = hsotg->hc_ptr_array[i];
1717197ba5f4SPaul Zimmerman 		if (!list_empty(&channel->hc_list_entry))
1718197ba5f4SPaul Zimmerman 			continue;
1719f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(i));
1720197ba5f4SPaul Zimmerman 		if (hcchar & HCCHAR_CHENA) {
1721197ba5f4SPaul Zimmerman 			/* Halt the channel */
1722197ba5f4SPaul Zimmerman 			hcchar |= HCCHAR_CHDIS;
1723f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcchar, HCCHAR(i));
1724197ba5f4SPaul Zimmerman 		}
1725197ba5f4SPaul Zimmerman 
1726197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, channel);
1727197ba5f4SPaul Zimmerman 		list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1728197ba5f4SPaul Zimmerman 		/*
1729197ba5f4SPaul Zimmerman 		 * Added for Descriptor DMA to prevent channel double cleanup in
1730197ba5f4SPaul Zimmerman 		 * release_channel_ddma(), which is called from ep_disable when
1731197ba5f4SPaul Zimmerman 		 * device disconnects
1732197ba5f4SPaul Zimmerman 		 */
1733197ba5f4SPaul Zimmerman 		channel->qh = NULL;
1734197ba5f4SPaul Zimmerman 	}
17357252f1bfSVincent Palatin 	/* All channels have been freed, mark them available */
173695832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
17377252f1bfSVincent Palatin 		hsotg->available_host_channels =
1738bea8e86cSJohn Youn 			hsotg->params.host_channels;
17397252f1bfSVincent Palatin 	} else {
17407252f1bfSVincent Palatin 		hsotg->non_periodic_channels = 0;
17417252f1bfSVincent Palatin 		hsotg->periodic_channels = 0;
17427252f1bfSVincent Palatin 	}
1743197ba5f4SPaul Zimmerman }
1744197ba5f4SPaul Zimmerman 
1745197ba5f4SPaul Zimmerman /**
17466a659531SDouglas Anderson  * dwc2_hcd_connect() - Handles connect of the HCD
1747197ba5f4SPaul Zimmerman  *
1748197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1749197ba5f4SPaul Zimmerman  *
1750197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1751197ba5f4SPaul Zimmerman  */
17526a659531SDouglas Anderson void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
17536a659531SDouglas Anderson {
17546a659531SDouglas Anderson 	if (hsotg->lx_state != DWC2_L0)
17556a659531SDouglas Anderson 		usb_hcd_resume_root_hub(hsotg->priv);
17566a659531SDouglas Anderson 
17576a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status_change = 1;
17586a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status = 1;
17596a659531SDouglas Anderson }
17606a659531SDouglas Anderson 
17616a659531SDouglas Anderson /**
17626a659531SDouglas Anderson  * dwc2_hcd_disconnect() - Handles disconnect of the HCD
17636a659531SDouglas Anderson  *
17646a659531SDouglas Anderson  * @hsotg: Pointer to struct dwc2_hsotg
17656a659531SDouglas Anderson  * @force: If true, we won't try to reconnect even if we see device connected.
17666a659531SDouglas Anderson  *
17676a659531SDouglas Anderson  * Must be called with interrupt disabled and spinlock held
17686a659531SDouglas Anderson  */
17696a659531SDouglas Anderson void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
1770197ba5f4SPaul Zimmerman {
1771197ba5f4SPaul Zimmerman 	u32 intr;
17726a659531SDouglas Anderson 	u32 hprt0;
1773197ba5f4SPaul Zimmerman 
1774197ba5f4SPaul Zimmerman 	/* Set status flags for the hub driver */
1775197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status_change = 1;
1776197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status = 0;
1777197ba5f4SPaul Zimmerman 
1778197ba5f4SPaul Zimmerman 	/*
1779197ba5f4SPaul Zimmerman 	 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1780197ba5f4SPaul Zimmerman 	 * interrupt mask and status bits and disabling subsequent host
1781197ba5f4SPaul Zimmerman 	 * channel interrupts.
1782197ba5f4SPaul Zimmerman 	 */
1783f25c42b8SGevorg Sahakyan 	intr = dwc2_readl(hsotg, GINTMSK);
1784197ba5f4SPaul Zimmerman 	intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
1785f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTMSK);
1786197ba5f4SPaul Zimmerman 	intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
1787f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTSTS);
1788197ba5f4SPaul Zimmerman 
1789197ba5f4SPaul Zimmerman 	/*
1790197ba5f4SPaul Zimmerman 	 * Turn off the vbus power only if the core has transitioned to device
1791197ba5f4SPaul Zimmerman 	 * mode. If still in host mode, need to keep power on to detect a
1792197ba5f4SPaul Zimmerman 	 * reconnection.
1793197ba5f4SPaul Zimmerman 	 */
1794197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
1795197ba5f4SPaul Zimmerman 		if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1796197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
1797f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, HPRT0);
1798197ba5f4SPaul Zimmerman 		}
1799197ba5f4SPaul Zimmerman 
1800197ba5f4SPaul Zimmerman 		dwc2_disable_host_interrupts(hsotg);
1801197ba5f4SPaul Zimmerman 	}
1802197ba5f4SPaul Zimmerman 
1803197ba5f4SPaul Zimmerman 	/* Respond with an error status to all URBs in the schedule */
1804197ba5f4SPaul Zimmerman 	dwc2_kill_all_urbs(hsotg);
1805197ba5f4SPaul Zimmerman 
1806197ba5f4SPaul Zimmerman 	if (dwc2_is_host_mode(hsotg))
1807197ba5f4SPaul Zimmerman 		/* Clean up any host channels that were in use */
1808197ba5f4SPaul Zimmerman 		dwc2_hcd_cleanup_channels(hsotg);
1809197ba5f4SPaul Zimmerman 
1810197ba5f4SPaul Zimmerman 	dwc2_host_disconnect(hsotg);
18116a659531SDouglas Anderson 
18126a659531SDouglas Anderson 	/*
18136a659531SDouglas Anderson 	 * Add an extra check here to see if we're actually connected but
18146a659531SDouglas Anderson 	 * we don't have a detection interrupt pending.  This can happen if:
18156a659531SDouglas Anderson 	 *   1. hardware sees connect
18166a659531SDouglas Anderson 	 *   2. hardware sees disconnect
18176a659531SDouglas Anderson 	 *   3. hardware sees connect
18186a659531SDouglas Anderson 	 *   4. dwc2_port_intr() - clears connect interrupt
18196a659531SDouglas Anderson 	 *   5. dwc2_handle_common_intr() - calls here
18206a659531SDouglas Anderson 	 *
18216a659531SDouglas Anderson 	 * Without the extra check here we will end calling disconnect
18226a659531SDouglas Anderson 	 * and won't get any future interrupts to handle the connect.
18236a659531SDouglas Anderson 	 */
18246a659531SDouglas Anderson 	if (!force) {
1825f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
18266a659531SDouglas Anderson 		if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
18276a659531SDouglas Anderson 			dwc2_hcd_connect(hsotg);
18286a659531SDouglas Anderson 	}
1829197ba5f4SPaul Zimmerman }
1830197ba5f4SPaul Zimmerman 
1831197ba5f4SPaul Zimmerman /**
1832197ba5f4SPaul Zimmerman  * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
1833197ba5f4SPaul Zimmerman  *
1834197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1835197ba5f4SPaul Zimmerman  */
1836197ba5f4SPaul Zimmerman static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
1837197ba5f4SPaul Zimmerman {
18381fb7f12dSDouglas Anderson 	if (hsotg->bus_suspended) {
1839197ba5f4SPaul Zimmerman 		hsotg->flags.b.port_suspend_change = 1;
1840b46146d5SGregory Herrero 		usb_hcd_resume_root_hub(hsotg->priv);
1841197ba5f4SPaul Zimmerman 	}
18421fb7f12dSDouglas Anderson 
18431fb7f12dSDouglas Anderson 	if (hsotg->lx_state == DWC2_L1)
18441fb7f12dSDouglas Anderson 		hsotg->flags.b.port_l1_change = 1;
1845b46146d5SGregory Herrero }
1846197ba5f4SPaul Zimmerman 
1847197ba5f4SPaul Zimmerman /**
1848197ba5f4SPaul Zimmerman  * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
1849197ba5f4SPaul Zimmerman  *
1850197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1851197ba5f4SPaul Zimmerman  *
1852197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1853197ba5f4SPaul Zimmerman  */
1854197ba5f4SPaul Zimmerman void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
1855197ba5f4SPaul Zimmerman {
1856197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
1857197ba5f4SPaul Zimmerman 
1858197ba5f4SPaul Zimmerman 	/*
1859197ba5f4SPaul Zimmerman 	 * The root hub should be disconnected before this function is called.
1860197ba5f4SPaul Zimmerman 	 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
1861197ba5f4SPaul Zimmerman 	 * and the QH lists (via ..._hcd_endpoint_disable).
1862197ba5f4SPaul Zimmerman 	 */
1863197ba5f4SPaul Zimmerman 
1864197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
1865197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
1866197ba5f4SPaul Zimmerman 
1867197ba5f4SPaul Zimmerman 	/* Turn off the vbus power */
1868197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "PortPower off\n");
1869f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HPRT0);
1870197ba5f4SPaul Zimmerman }
1871197ba5f4SPaul Zimmerman 
187233ad261aSGregory Herrero /* Caller must hold driver lock */
1873197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
1874b58e6ceeSMian Yousaf Kaukab 				struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
1875b5a468a6SMian Yousaf Kaukab 				struct dwc2_qtd *qtd)
1876197ba5f4SPaul Zimmerman {
1877197ba5f4SPaul Zimmerman 	u32 intr_mask;
1878197ba5f4SPaul Zimmerman 	int retval;
1879197ba5f4SPaul Zimmerman 	int dev_speed;
1880197ba5f4SPaul Zimmerman 
1881197ba5f4SPaul Zimmerman 	if (!hsotg->flags.b.port_connect_status) {
1882197ba5f4SPaul Zimmerman 		/* No longer connected */
1883197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Not connected\n");
1884197ba5f4SPaul Zimmerman 		return -ENODEV;
1885197ba5f4SPaul Zimmerman 	}
1886197ba5f4SPaul Zimmerman 
1887197ba5f4SPaul Zimmerman 	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1888197ba5f4SPaul Zimmerman 
1889197ba5f4SPaul Zimmerman 	/* Some configurations cannot support LS traffic on a FS root port */
1890197ba5f4SPaul Zimmerman 	if ((dev_speed == USB_SPEED_LOW) &&
1891197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
1892197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
1893f25c42b8SGevorg Sahakyan 		u32 hprt0 = dwc2_readl(hsotg, HPRT0);
1894197ba5f4SPaul Zimmerman 		u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1895197ba5f4SPaul Zimmerman 
1896197ba5f4SPaul Zimmerman 		if (prtspd == HPRT0_SPD_FULL_SPEED)
1897197ba5f4SPaul Zimmerman 			return -ENODEV;
1898197ba5f4SPaul Zimmerman 	}
1899197ba5f4SPaul Zimmerman 
1900197ba5f4SPaul Zimmerman 	if (!qtd)
1901b5a468a6SMian Yousaf Kaukab 		return -EINVAL;
1902197ba5f4SPaul Zimmerman 
1903197ba5f4SPaul Zimmerman 	dwc2_hcd_qtd_init(qtd, urb);
1904b58e6ceeSMian Yousaf Kaukab 	retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
1905197ba5f4SPaul Zimmerman 	if (retval) {
1906197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
1907197ba5f4SPaul Zimmerman 			"DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
1908197ba5f4SPaul Zimmerman 			retval);
1909197ba5f4SPaul Zimmerman 		return retval;
1910197ba5f4SPaul Zimmerman 	}
1911197ba5f4SPaul Zimmerman 
1912f25c42b8SGevorg Sahakyan 	intr_mask = dwc2_readl(hsotg, GINTMSK);
1913197ba5f4SPaul Zimmerman 	if (!(intr_mask & GINTSTS_SOF)) {
1914197ba5f4SPaul Zimmerman 		enum dwc2_transaction_type tr_type;
1915197ba5f4SPaul Zimmerman 
1916197ba5f4SPaul Zimmerman 		if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
1917197ba5f4SPaul Zimmerman 		    !(qtd->urb->flags & URB_GIVEBACK_ASAP))
1918197ba5f4SPaul Zimmerman 			/*
1919197ba5f4SPaul Zimmerman 			 * Do not schedule SG transactions until qtd has
1920197ba5f4SPaul Zimmerman 			 * URB_GIVEBACK_ASAP set
1921197ba5f4SPaul Zimmerman 			 */
1922197ba5f4SPaul Zimmerman 			return 0;
1923197ba5f4SPaul Zimmerman 
1924197ba5f4SPaul Zimmerman 		tr_type = dwc2_hcd_select_transactions(hsotg);
1925197ba5f4SPaul Zimmerman 		if (tr_type != DWC2_TRANSACTION_NONE)
1926197ba5f4SPaul Zimmerman 			dwc2_hcd_queue_transactions(hsotg, tr_type);
1927197ba5f4SPaul Zimmerman 	}
1928197ba5f4SPaul Zimmerman 
1929197ba5f4SPaul Zimmerman 	return 0;
1930197ba5f4SPaul Zimmerman }
1931197ba5f4SPaul Zimmerman 
1932197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1933197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
1934197ba5f4SPaul Zimmerman 				struct dwc2_hcd_urb *urb)
1935197ba5f4SPaul Zimmerman {
1936197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
1937197ba5f4SPaul Zimmerman 	struct dwc2_qtd *urb_qtd;
1938197ba5f4SPaul Zimmerman 
1939197ba5f4SPaul Zimmerman 	urb_qtd = urb->qtd;
1940197ba5f4SPaul Zimmerman 	if (!urb_qtd) {
1941197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
1942197ba5f4SPaul Zimmerman 		return -EINVAL;
1943197ba5f4SPaul Zimmerman 	}
1944197ba5f4SPaul Zimmerman 
1945197ba5f4SPaul Zimmerman 	qh = urb_qtd->qh;
1946197ba5f4SPaul Zimmerman 	if (!qh) {
1947197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
1948197ba5f4SPaul Zimmerman 		return -EINVAL;
1949197ba5f4SPaul Zimmerman 	}
1950197ba5f4SPaul Zimmerman 
1951197ba5f4SPaul Zimmerman 	urb->priv = NULL;
1952197ba5f4SPaul Zimmerman 
1953197ba5f4SPaul Zimmerman 	if (urb_qtd->in_process && qh->channel) {
1954197ba5f4SPaul Zimmerman 		dwc2_dump_channel_info(hsotg, qh->channel);
1955197ba5f4SPaul Zimmerman 
1956197ba5f4SPaul Zimmerman 		/* The QTD is in process (it has been assigned to a channel) */
1957197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status)
1958197ba5f4SPaul Zimmerman 			/*
1959197ba5f4SPaul Zimmerman 			 * If still connected (i.e. in host mode), halt the
1960197ba5f4SPaul Zimmerman 			 * channel so it can be used for other transfers. If
1961197ba5f4SPaul Zimmerman 			 * no longer connected, the host registers can't be
1962197ba5f4SPaul Zimmerman 			 * written to halt the channel since the core is in
1963197ba5f4SPaul Zimmerman 			 * device mode.
1964197ba5f4SPaul Zimmerman 			 */
1965197ba5f4SPaul Zimmerman 			dwc2_hc_halt(hsotg, qh->channel,
1966197ba5f4SPaul Zimmerman 				     DWC2_HC_XFER_URB_DEQUEUE);
1967197ba5f4SPaul Zimmerman 	}
1968197ba5f4SPaul Zimmerman 
1969197ba5f4SPaul Zimmerman 	/*
1970197ba5f4SPaul Zimmerman 	 * Free the QTD and clean up the associated QH. Leave the QH in the
1971197ba5f4SPaul Zimmerman 	 * schedule if it has any remaining QTDs.
1972197ba5f4SPaul Zimmerman 	 */
197395832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
1974197ba5f4SPaul Zimmerman 		u8 in_process = urb_qtd->in_process;
1975197ba5f4SPaul Zimmerman 
1976197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1977197ba5f4SPaul Zimmerman 		if (in_process) {
1978197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_deactivate(hsotg, qh, 0);
1979197ba5f4SPaul Zimmerman 			qh->channel = NULL;
1980197ba5f4SPaul Zimmerman 		} else if (list_empty(&qh->qtd_list)) {
1981197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
1982197ba5f4SPaul Zimmerman 		}
1983197ba5f4SPaul Zimmerman 	} else {
1984197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1985197ba5f4SPaul Zimmerman 	}
1986197ba5f4SPaul Zimmerman 
1987197ba5f4SPaul Zimmerman 	return 0;
1988197ba5f4SPaul Zimmerman }
1989197ba5f4SPaul Zimmerman 
1990197ba5f4SPaul Zimmerman /* Must NOT be called with interrupt disabled or spinlock held */
1991197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
1992197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep, int retry)
1993197ba5f4SPaul Zimmerman {
1994197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1995197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
1996197ba5f4SPaul Zimmerman 	unsigned long flags;
1997197ba5f4SPaul Zimmerman 	int rc;
1998197ba5f4SPaul Zimmerman 
1999197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
2000197ba5f4SPaul Zimmerman 
2001197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
2002197ba5f4SPaul Zimmerman 	if (!qh) {
2003197ba5f4SPaul Zimmerman 		rc = -EINVAL;
2004197ba5f4SPaul Zimmerman 		goto err;
2005197ba5f4SPaul Zimmerman 	}
2006197ba5f4SPaul Zimmerman 
2007197ba5f4SPaul Zimmerman 	while (!list_empty(&qh->qtd_list) && retry--) {
2008197ba5f4SPaul Zimmerman 		if (retry == 0) {
2009197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2010197ba5f4SPaul Zimmerman 				"## timeout in dwc2_hcd_endpoint_disable() ##\n");
2011197ba5f4SPaul Zimmerman 			rc = -EBUSY;
2012197ba5f4SPaul Zimmerman 			goto err;
2013197ba5f4SPaul Zimmerman 		}
2014197ba5f4SPaul Zimmerman 
2015197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
201604a9db79SNicholas Mc Guire 		msleep(20);
2017197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
2018197ba5f4SPaul Zimmerman 		qh = ep->hcpriv;
2019197ba5f4SPaul Zimmerman 		if (!qh) {
2020197ba5f4SPaul Zimmerman 			rc = -EINVAL;
2021197ba5f4SPaul Zimmerman 			goto err;
2022197ba5f4SPaul Zimmerman 		}
2023197ba5f4SPaul Zimmerman 	}
2024197ba5f4SPaul Zimmerman 
2025197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_unlink(hsotg, qh);
2026197ba5f4SPaul Zimmerman 
2027197ba5f4SPaul Zimmerman 	/* Free each QTD in the QH's QTD list */
2028197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2029197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2030197ba5f4SPaul Zimmerman 
2031197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
203216e80218SDouglas Anderson 
203316e80218SDouglas Anderson 	if (qh->channel && qh->channel->qh == qh)
203416e80218SDouglas Anderson 		qh->channel->qh = NULL;
203516e80218SDouglas Anderson 
2036197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
203716e80218SDouglas Anderson 
2038197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_free(hsotg, qh);
2039197ba5f4SPaul Zimmerman 
2040197ba5f4SPaul Zimmerman 	return 0;
2041197ba5f4SPaul Zimmerman 
2042197ba5f4SPaul Zimmerman err:
2043197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
2044197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
2045197ba5f4SPaul Zimmerman 
2046197ba5f4SPaul Zimmerman 	return rc;
2047197ba5f4SPaul Zimmerman }
2048197ba5f4SPaul Zimmerman 
2049197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
2050197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2051197ba5f4SPaul Zimmerman 				   struct usb_host_endpoint *ep)
2052197ba5f4SPaul Zimmerman {
2053197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = ep->hcpriv;
2054197ba5f4SPaul Zimmerman 
2055197ba5f4SPaul Zimmerman 	if (!qh)
2056197ba5f4SPaul Zimmerman 		return -EINVAL;
2057197ba5f4SPaul Zimmerman 
2058197ba5f4SPaul Zimmerman 	qh->data_toggle = DWC2_HC_PID_DATA0;
2059197ba5f4SPaul Zimmerman 
2060197ba5f4SPaul Zimmerman 	return 0;
2061197ba5f4SPaul Zimmerman }
2062197ba5f4SPaul Zimmerman 
2063b02038faSJohn Youn /**
2064b02038faSJohn Youn  * dwc2_core_init() - Initializes the DWC_otg controller registers and
2065b02038faSJohn Youn  * prepares the core for device mode or host mode operation
2066b02038faSJohn Youn  *
2067b02038faSJohn Youn  * @hsotg:         Programming view of the DWC_otg controller
2068b02038faSJohn Youn  * @initial_setup: If true then this is the first init for this instance.
2069b02038faSJohn Youn  */
207065c9c4c6SVardan Mikayelyan int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2071b02038faSJohn Youn {
2072b02038faSJohn Youn 	u32 usbcfg, otgctl;
2073b02038faSJohn Youn 	int retval;
2074b02038faSJohn Youn 
2075b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2076b02038faSJohn Youn 
2077f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
2078b02038faSJohn Youn 
2079b02038faSJohn Youn 	/* Set ULPI External VBUS bit if needed */
2080b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
208195832c00SJohn Youn 	if (hsotg->params.phy_ulpi_ext_vbus)
2082b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2083b02038faSJohn Youn 
2084b02038faSJohn Youn 	/* Set external TS Dline pulsing bit if needed */
2085b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
208695832c00SJohn Youn 	if (hsotg->params.ts_dline)
2087b02038faSJohn Youn 		usbcfg |= GUSBCFG_TERMSELDLPULSE;
2088b02038faSJohn Youn 
2089f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
2090b02038faSJohn Youn 
2091b02038faSJohn Youn 	/*
2092b02038faSJohn Youn 	 * Reset the Controller
2093b02038faSJohn Youn 	 *
2094b02038faSJohn Youn 	 * We only need to reset the controller if this is a re-init.
2095b02038faSJohn Youn 	 * For the first init we know for sure that earlier code reset us (it
2096b02038faSJohn Youn 	 * needed to in order to properly detect various parameters).
2097b02038faSJohn Youn 	 */
2098b02038faSJohn Youn 	if (!initial_setup) {
209913b1f8e2SVardan Mikayelyan 		retval = dwc2_core_reset(hsotg, false);
2100b02038faSJohn Youn 		if (retval) {
2101b02038faSJohn Youn 			dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2102b02038faSJohn Youn 				__func__);
2103b02038faSJohn Youn 			return retval;
2104b02038faSJohn Youn 		}
2105b02038faSJohn Youn 	}
2106b02038faSJohn Youn 
2107b02038faSJohn Youn 	/*
2108b02038faSJohn Youn 	 * This needs to happen in FS mode before any other programming occurs
2109b02038faSJohn Youn 	 */
2110b02038faSJohn Youn 	retval = dwc2_phy_init(hsotg, initial_setup);
2111b02038faSJohn Youn 	if (retval)
2112b02038faSJohn Youn 		return retval;
2113b02038faSJohn Youn 
2114b02038faSJohn Youn 	/* Program the GAHBCFG Register */
2115b02038faSJohn Youn 	retval = dwc2_gahbcfg_init(hsotg);
2116b02038faSJohn Youn 	if (retval)
2117b02038faSJohn Youn 		return retval;
2118b02038faSJohn Youn 
2119b02038faSJohn Youn 	/* Program the GUSBCFG register */
2120b02038faSJohn Youn 	dwc2_gusbcfg_init(hsotg);
2121b02038faSJohn Youn 
2122b02038faSJohn Youn 	/* Program the GOTGCTL register */
2123f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2124b02038faSJohn Youn 	otgctl &= ~GOTGCTL_OTGVER;
2125f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2126b02038faSJohn Youn 
2127b02038faSJohn Youn 	/* Clear the SRP success bit for FS-I2c */
2128b02038faSJohn Youn 	hsotg->srp_success = 0;
2129b02038faSJohn Youn 
2130b02038faSJohn Youn 	/* Enable common interrupts */
2131b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
2132b02038faSJohn Youn 
2133b02038faSJohn Youn 	/*
2134b02038faSJohn Youn 	 * Do device or host initialization based on mode during PCD and
2135b02038faSJohn Youn 	 * HCD initialization
2136b02038faSJohn Youn 	 */
2137b02038faSJohn Youn 	if (dwc2_is_host_mode(hsotg)) {
2138b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Host Mode\n");
2139b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_A_HOST;
2140b02038faSJohn Youn 	} else {
2141b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Device Mode\n");
2142b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2143b02038faSJohn Youn 	}
2144b02038faSJohn Youn 
2145b02038faSJohn Youn 	return 0;
2146b02038faSJohn Youn }
2147b02038faSJohn Youn 
2148b02038faSJohn Youn /**
2149b02038faSJohn Youn  * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2150b02038faSJohn Youn  * Host mode
2151b02038faSJohn Youn  *
2152b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
2153b02038faSJohn Youn  *
2154b02038faSJohn Youn  * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2155b02038faSJohn Youn  * request queues. Host channels are reset to ensure that they are ready for
2156b02038faSJohn Youn  * performing transfers.
2157b02038faSJohn Youn  */
2158b02038faSJohn Youn static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2159b02038faSJohn Youn {
216092a8dd26SMinas Harutyunyan 	u32 hcfg, hfir, otgctl, usbcfg;
2161b02038faSJohn Youn 
2162b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2163b02038faSJohn Youn 
216492a8dd26SMinas Harutyunyan 	/* Set HS/FS Timeout Calibration to 7 (max available value).
216592a8dd26SMinas Harutyunyan 	 * The number of PHY clocks that the application programs in
216692a8dd26SMinas Harutyunyan 	 * this field is added to the high/full speed interpacket timeout
216792a8dd26SMinas Harutyunyan 	 * duration in the core to account for any additional delays
216892a8dd26SMinas Harutyunyan 	 * introduced by the PHY. This can be required, because the delay
216992a8dd26SMinas Harutyunyan 	 * introduced by the PHY in generating the linestate condition
217092a8dd26SMinas Harutyunyan 	 * can vary from one PHY to another.
217192a8dd26SMinas Harutyunyan 	 */
2172f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
217392a8dd26SMinas Harutyunyan 	usbcfg |= GUSBCFG_TOUTCAL(7);
2174f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
217592a8dd26SMinas Harutyunyan 
2176b02038faSJohn Youn 	/* Restart the Phy Clock */
2177f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, PCGCTL);
2178b02038faSJohn Youn 
2179b02038faSJohn Youn 	/* Initialize Host Configuration Register */
2180b02038faSJohn Youn 	dwc2_init_fs_ls_pclk_sel(hsotg);
218138e9002bSVardan Mikayelyan 	if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
218238e9002bSVardan Mikayelyan 	    hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
2183f25c42b8SGevorg Sahakyan 		hcfg = dwc2_readl(hsotg, HCFG);
2184b02038faSJohn Youn 		hcfg |= HCFG_FSLSSUPP;
2185f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcfg, HCFG);
2186b02038faSJohn Youn 	}
2187b02038faSJohn Youn 
2188b02038faSJohn Youn 	/*
2189b02038faSJohn Youn 	 * This bit allows dynamic reloading of the HFIR register during
2190b02038faSJohn Youn 	 * runtime. This bit needs to be programmed during initial configuration
2191b02038faSJohn Youn 	 * and its value must not be changed during runtime.
2192b02038faSJohn Youn 	 */
219395832c00SJohn Youn 	if (hsotg->params.reload_ctl) {
2194f25c42b8SGevorg Sahakyan 		hfir = dwc2_readl(hsotg, HFIR);
2195b02038faSJohn Youn 		hfir |= HFIR_RLDCTRL;
2196f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hfir, HFIR);
2197b02038faSJohn Youn 	}
2198b02038faSJohn Youn 
219995832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2200b02038faSJohn Youn 		u32 op_mode = hsotg->hw_params.op_mode;
2201b02038faSJohn Youn 
2202b02038faSJohn Youn 		if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2203b02038faSJohn Youn 		    !hsotg->hw_params.dma_desc_enable ||
2204b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2205b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2206b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2207b02038faSJohn Youn 			dev_err(hsotg->dev,
2208b02038faSJohn Youn 				"Hardware does not support descriptor DMA mode -\n");
2209b02038faSJohn Youn 			dev_err(hsotg->dev,
2210b02038faSJohn Youn 				"falling back to buffer DMA mode.\n");
221195832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
2212b02038faSJohn Youn 		} else {
2213f25c42b8SGevorg Sahakyan 			hcfg = dwc2_readl(hsotg, HCFG);
2214b02038faSJohn Youn 			hcfg |= HCFG_DESCDMA;
2215f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcfg, HCFG);
2216b02038faSJohn Youn 		}
2217b02038faSJohn Youn 	}
2218b02038faSJohn Youn 
2219b02038faSJohn Youn 	/* Configure data FIFO sizes */
2220b02038faSJohn Youn 	dwc2_config_fifos(hsotg);
2221b02038faSJohn Youn 
2222b02038faSJohn Youn 	/* TODO - check this */
2223b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2224f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2225b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2226f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2227b02038faSJohn Youn 
2228b02038faSJohn Youn 	/* Make sure the FIFOs are flushed */
2229b02038faSJohn Youn 	dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2230b02038faSJohn Youn 	dwc2_flush_rx_fifo(hsotg);
2231b02038faSJohn Youn 
2232b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2233f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2234b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2235f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2236b02038faSJohn Youn 
223795832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
2238b02038faSJohn Youn 		int num_channels, i;
2239b02038faSJohn Youn 		u32 hcchar;
2240b02038faSJohn Youn 
2241b02038faSJohn Youn 		/* Flush out any leftover queued requests */
2242bea8e86cSJohn Youn 		num_channels = hsotg->params.host_channels;
2243b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2244f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22455799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2246b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
2247b02038faSJohn Youn 				hcchar |= HCCHAR_CHDIS;
2248b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2249f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2250b02038faSJohn Youn 			}
22515799aecdSMinas Harutyunyan 		}
2252b02038faSJohn Youn 
2253b02038faSJohn Youn 		/* Halt all channels to put them into a known state */
2254b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2255f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22565799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2257b02038faSJohn Youn 				hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2258b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2259f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2260b02038faSJohn Youn 				dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2261b02038faSJohn Youn 					__func__, i);
226279d6b8c5SSevak Arakelyan 
226379d6b8c5SSevak Arakelyan 				if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
22645799aecdSMinas Harutyunyan 							      HCCHAR_CHENA,
22655799aecdSMinas Harutyunyan 							      1000)) {
22665799aecdSMinas Harutyunyan 					dev_warn(hsotg->dev,
22675799aecdSMinas Harutyunyan 						 "Unable to clear enable on channel %d\n",
2268b02038faSJohn Youn 						 i);
2269b02038faSJohn Youn 				}
2270b02038faSJohn Youn 			}
2271b02038faSJohn Youn 		}
22725799aecdSMinas Harutyunyan 	}
2273b02038faSJohn Youn 
227466e77a24SRazmik Karapetyan 	/* Enable ACG feature in host mode, if supported */
227566e77a24SRazmik Karapetyan 	dwc2_enable_acg(hsotg);
227666e77a24SRazmik Karapetyan 
2277b02038faSJohn Youn 	/* Turn on the vbus power */
2278b02038faSJohn Youn 	dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2279b02038faSJohn Youn 	if (hsotg->op_state == OTG_STATE_A_HOST) {
2280b02038faSJohn Youn 		u32 hprt0 = dwc2_read_hprt0(hsotg);
2281b02038faSJohn Youn 
2282b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2283b02038faSJohn Youn 			!!(hprt0 & HPRT0_PWR));
2284b02038faSJohn Youn 		if (!(hprt0 & HPRT0_PWR)) {
2285b02038faSJohn Youn 			hprt0 |= HPRT0_PWR;
2286f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
2287b02038faSJohn Youn 		}
2288b02038faSJohn Youn 	}
2289b02038faSJohn Youn 
2290b02038faSJohn Youn 	dwc2_enable_host_interrupts(hsotg);
2291b02038faSJohn Youn }
2292b02038faSJohn Youn 
2293197ba5f4SPaul Zimmerman /*
2294197ba5f4SPaul Zimmerman  * Initializes dynamic portions of the DWC_otg HCD state
2295197ba5f4SPaul Zimmerman  *
2296197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2297197ba5f4SPaul Zimmerman  */
2298197ba5f4SPaul Zimmerman static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2299197ba5f4SPaul Zimmerman {
2300197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan, *chan_tmp;
2301197ba5f4SPaul Zimmerman 	int num_channels;
2302197ba5f4SPaul Zimmerman 	int i;
2303197ba5f4SPaul Zimmerman 
2304197ba5f4SPaul Zimmerman 	hsotg->flags.d32 = 0;
2305197ba5f4SPaul Zimmerman 	hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
2306197ba5f4SPaul Zimmerman 
230795832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
2308197ba5f4SPaul Zimmerman 		hsotg->available_host_channels =
2309bea8e86cSJohn Youn 			hsotg->params.host_channels;
2310197ba5f4SPaul Zimmerman 	} else {
2311197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels = 0;
2312197ba5f4SPaul Zimmerman 		hsotg->periodic_channels = 0;
2313197ba5f4SPaul Zimmerman 	}
2314197ba5f4SPaul Zimmerman 
2315197ba5f4SPaul Zimmerman 	/*
2316197ba5f4SPaul Zimmerman 	 * Put all channels in the free channel list and clean up channel
2317197ba5f4SPaul Zimmerman 	 * states
2318197ba5f4SPaul Zimmerman 	 */
2319197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2320197ba5f4SPaul Zimmerman 				 hc_list_entry)
2321197ba5f4SPaul Zimmerman 		list_del_init(&chan->hc_list_entry);
2322197ba5f4SPaul Zimmerman 
2323bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2324197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
2325197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
2326197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2327197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
2328197ba5f4SPaul Zimmerman 	}
2329197ba5f4SPaul Zimmerman 
2330197ba5f4SPaul Zimmerman 	/* Initialize the DWC core for host mode operation */
2331197ba5f4SPaul Zimmerman 	dwc2_core_host_init(hsotg);
2332197ba5f4SPaul Zimmerman }
2333197ba5f4SPaul Zimmerman 
2334197ba5f4SPaul Zimmerman static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2335197ba5f4SPaul Zimmerman 			       struct dwc2_host_chan *chan,
2336197ba5f4SPaul Zimmerman 			       struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2337197ba5f4SPaul Zimmerman {
2338197ba5f4SPaul Zimmerman 	int hub_addr, hub_port;
2339197ba5f4SPaul Zimmerman 
2340197ba5f4SPaul Zimmerman 	chan->do_split = 1;
2341197ba5f4SPaul Zimmerman 	chan->xact_pos = qtd->isoc_split_pos;
2342197ba5f4SPaul Zimmerman 	chan->complete_split = qtd->complete_split;
2343197ba5f4SPaul Zimmerman 	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2344197ba5f4SPaul Zimmerman 	chan->hub_addr = (u8)hub_addr;
2345197ba5f4SPaul Zimmerman 	chan->hub_port = (u8)hub_port;
2346197ba5f4SPaul Zimmerman }
2347197ba5f4SPaul Zimmerman 
23483bc04e28SDouglas Anderson static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2349197ba5f4SPaul Zimmerman 			      struct dwc2_host_chan *chan,
23503bc04e28SDouglas Anderson 			      struct dwc2_qtd *qtd)
2351197ba5f4SPaul Zimmerman {
2352197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
2353197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
2354197ba5f4SPaul Zimmerman 
2355197ba5f4SPaul Zimmerman 	switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2356197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
2357197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2358197ba5f4SPaul Zimmerman 
2359197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
2360197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
2361197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control setup transaction\n");
2362197ba5f4SPaul Zimmerman 			chan->do_ping = 0;
2363197ba5f4SPaul Zimmerman 			chan->ep_is_in = 0;
2364197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_SETUP;
236595832c00SJohn Youn 			if (hsotg->params.host_dma)
2366197ba5f4SPaul Zimmerman 				chan->xfer_dma = urb->setup_dma;
2367197ba5f4SPaul Zimmerman 			else
2368197ba5f4SPaul Zimmerman 				chan->xfer_buf = urb->setup_packet;
2369197ba5f4SPaul Zimmerman 			chan->xfer_len = 8;
2370197ba5f4SPaul Zimmerman 			break;
2371197ba5f4SPaul Zimmerman 
2372197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
2373197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control data transaction\n");
2374197ba5f4SPaul Zimmerman 			chan->data_pid_start = qtd->data_toggle;
2375197ba5f4SPaul Zimmerman 			break;
2376197ba5f4SPaul Zimmerman 
2377197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_STATUS:
2378197ba5f4SPaul Zimmerman 			/*
2379197ba5f4SPaul Zimmerman 			 * Direction is opposite of data direction or IN if no
2380197ba5f4SPaul Zimmerman 			 * data
2381197ba5f4SPaul Zimmerman 			 */
2382197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control status transaction\n");
2383197ba5f4SPaul Zimmerman 			if (urb->length == 0)
2384197ba5f4SPaul Zimmerman 				chan->ep_is_in = 1;
2385197ba5f4SPaul Zimmerman 			else
2386197ba5f4SPaul Zimmerman 				chan->ep_is_in =
2387197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_out(&urb->pipe_info);
2388197ba5f4SPaul Zimmerman 			if (chan->ep_is_in)
2389197ba5f4SPaul Zimmerman 				chan->do_ping = 0;
2390197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_DATA1;
2391197ba5f4SPaul Zimmerman 			chan->xfer_len = 0;
239295832c00SJohn Youn 			if (hsotg->params.host_dma)
2393197ba5f4SPaul Zimmerman 				chan->xfer_dma = hsotg->status_buf_dma;
2394197ba5f4SPaul Zimmerman 			else
2395197ba5f4SPaul Zimmerman 				chan->xfer_buf = hsotg->status_buf;
2396197ba5f4SPaul Zimmerman 			break;
2397197ba5f4SPaul Zimmerman 		}
2398197ba5f4SPaul Zimmerman 		break;
2399197ba5f4SPaul Zimmerman 
2400197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
2401197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_BULK;
2402197ba5f4SPaul Zimmerman 		break;
2403197ba5f4SPaul Zimmerman 
2404197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
2405197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_INT;
2406197ba5f4SPaul Zimmerman 		break;
2407197ba5f4SPaul Zimmerman 
2408197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
2409197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_ISOC;
241095832c00SJohn Youn 		if (hsotg->params.dma_desc_enable)
2411197ba5f4SPaul Zimmerman 			break;
2412197ba5f4SPaul Zimmerman 
2413197ba5f4SPaul Zimmerman 		frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2414197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
2415197ba5f4SPaul Zimmerman 
241695832c00SJohn Youn 		if (hsotg->params.host_dma) {
2417197ba5f4SPaul Zimmerman 			chan->xfer_dma = urb->dma;
2418197ba5f4SPaul Zimmerman 			chan->xfer_dma += frame_desc->offset +
2419197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2420197ba5f4SPaul Zimmerman 		} else {
2421197ba5f4SPaul Zimmerman 			chan->xfer_buf = urb->buf;
2422197ba5f4SPaul Zimmerman 			chan->xfer_buf += frame_desc->offset +
2423197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2424197ba5f4SPaul Zimmerman 		}
2425197ba5f4SPaul Zimmerman 
2426197ba5f4SPaul Zimmerman 		chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2427197ba5f4SPaul Zimmerman 
2428197ba5f4SPaul Zimmerman 		if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2429197ba5f4SPaul Zimmerman 			if (chan->xfer_len <= 188)
2430197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2431197ba5f4SPaul Zimmerman 			else
2432197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2433197ba5f4SPaul Zimmerman 		}
2434197ba5f4SPaul Zimmerman 		break;
2435197ba5f4SPaul Zimmerman 	}
2436197ba5f4SPaul Zimmerman }
2437197ba5f4SPaul Zimmerman 
2438af424a41SWilliam Wu static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
2439af424a41SWilliam Wu 					    struct dwc2_qh *qh,
2440af424a41SWilliam Wu 					    struct dwc2_host_chan *chan)
2441af424a41SWilliam Wu {
2442af424a41SWilliam Wu 	if (!hsotg->unaligned_cache ||
2443af424a41SWilliam Wu 	    chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
2444af424a41SWilliam Wu 		return -ENOMEM;
2445af424a41SWilliam Wu 
2446af424a41SWilliam Wu 	if (!qh->dw_align_buf) {
2447af424a41SWilliam Wu 		qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
2448af424a41SWilliam Wu 						    GFP_ATOMIC | GFP_DMA);
2449af424a41SWilliam Wu 		if (!qh->dw_align_buf)
2450af424a41SWilliam Wu 			return -ENOMEM;
2451af424a41SWilliam Wu 	}
2452af424a41SWilliam Wu 
2453af424a41SWilliam Wu 	qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
2454af424a41SWilliam Wu 					      DWC2_KMEM_UNALIGNED_BUF_SIZE,
2455af424a41SWilliam Wu 					      DMA_FROM_DEVICE);
2456af424a41SWilliam Wu 
2457af424a41SWilliam Wu 	if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
2458af424a41SWilliam Wu 		dev_err(hsotg->dev, "can't map align_buf\n");
2459af424a41SWilliam Wu 		chan->align_buf = 0;
2460af424a41SWilliam Wu 		return -EINVAL;
2461af424a41SWilliam Wu 	}
2462af424a41SWilliam Wu 
2463af424a41SWilliam Wu 	chan->align_buf = qh->dw_align_buf_dma;
2464af424a41SWilliam Wu 	return 0;
2465af424a41SWilliam Wu }
2466af424a41SWilliam Wu 
24673bc04e28SDouglas Anderson #define DWC2_USB_DMA_ALIGN 4
24683bc04e28SDouglas Anderson 
24693bc04e28SDouglas Anderson static void dwc2_free_dma_aligned_buffer(struct urb *urb)
2470197ba5f4SPaul Zimmerman {
247156406e01SAntti Seppälä 	void *stored_xfer_buffer;
24721e111e88SAntti Seppälä 	size_t length;
2473197ba5f4SPaul Zimmerman 
24743bc04e28SDouglas Anderson 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
24753bc04e28SDouglas Anderson 		return;
2476197ba5f4SPaul Zimmerman 
247756406e01SAntti Seppälä 	/* Restore urb->transfer_buffer from the end of the allocated area */
24784a4863bfSMartin Schiller 	memcpy(&stored_xfer_buffer,
24794a4863bfSMartin Schiller 	       PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
24804a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
24814a4863bfSMartin Schiller 	       sizeof(urb->transfer_buffer));
24823bc04e28SDouglas Anderson 
24831e111e88SAntti Seppälä 	if (usb_urb_dir_in(urb)) {
24841e111e88SAntti Seppälä 		if (usb_pipeisoc(urb->pipe))
24851e111e88SAntti Seppälä 			length = urb->transfer_buffer_length;
24861e111e88SAntti Seppälä 		else
24871e111e88SAntti Seppälä 			length = urb->actual_length;
24881e111e88SAntti Seppälä 
24891e111e88SAntti Seppälä 		memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
24901e111e88SAntti Seppälä 	}
249156406e01SAntti Seppälä 	kfree(urb->transfer_buffer);
249256406e01SAntti Seppälä 	urb->transfer_buffer = stored_xfer_buffer;
24933bc04e28SDouglas Anderson 
24943bc04e28SDouglas Anderson 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2495197ba5f4SPaul Zimmerman }
2496197ba5f4SPaul Zimmerman 
24973bc04e28SDouglas Anderson static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
24983bc04e28SDouglas Anderson {
249956406e01SAntti Seppälä 	void *kmalloc_ptr;
25003bc04e28SDouglas Anderson 	size_t kmalloc_size;
25015dce9555SPaul Zimmerman 
25023bc04e28SDouglas Anderson 	if (urb->num_sgs || urb->sg ||
25033bc04e28SDouglas Anderson 	    urb->transfer_buffer_length == 0 ||
25043bc04e28SDouglas Anderson 	    !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2505197ba5f4SPaul Zimmerman 		return 0;
25063bc04e28SDouglas Anderson 
250756406e01SAntti Seppälä 	/*
250856406e01SAntti Seppälä 	 * Allocate a buffer with enough padding for original transfer_buffer
250956406e01SAntti Seppälä 	 * pointer. This allocation is guaranteed to be aligned properly for
251056406e01SAntti Seppälä 	 * DMA
251156406e01SAntti Seppälä 	 */
25123bc04e28SDouglas Anderson 	kmalloc_size = urb->transfer_buffer_length +
25134a4863bfSMartin Schiller 		(dma_get_cache_alignment() - 1) +
251456406e01SAntti Seppälä 		sizeof(urb->transfer_buffer);
25153bc04e28SDouglas Anderson 
25163bc04e28SDouglas Anderson 	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
25173bc04e28SDouglas Anderson 	if (!kmalloc_ptr)
25183bc04e28SDouglas Anderson 		return -ENOMEM;
25193bc04e28SDouglas Anderson 
252056406e01SAntti Seppälä 	/*
252156406e01SAntti Seppälä 	 * Position value of original urb->transfer_buffer pointer to the end
252256406e01SAntti Seppälä 	 * of allocation for later referencing
252356406e01SAntti Seppälä 	 */
25244a4863bfSMartin Schiller 	memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
25254a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
252656406e01SAntti Seppälä 	       &urb->transfer_buffer, sizeof(urb->transfer_buffer));
252756406e01SAntti Seppälä 
25283bc04e28SDouglas Anderson 	if (usb_urb_dir_out(urb))
252956406e01SAntti Seppälä 		memcpy(kmalloc_ptr, urb->transfer_buffer,
25303bc04e28SDouglas Anderson 		       urb->transfer_buffer_length);
253156406e01SAntti Seppälä 	urb->transfer_buffer = kmalloc_ptr;
25323bc04e28SDouglas Anderson 
25333bc04e28SDouglas Anderson 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
25343bc04e28SDouglas Anderson 
25353bc04e28SDouglas Anderson 	return 0;
25363bc04e28SDouglas Anderson }
25373bc04e28SDouglas Anderson 
25383bc04e28SDouglas Anderson static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
25393bc04e28SDouglas Anderson 				gfp_t mem_flags)
25403bc04e28SDouglas Anderson {
25413bc04e28SDouglas Anderson 	int ret;
25423bc04e28SDouglas Anderson 
25433bc04e28SDouglas Anderson 	/* We assume setup_dma is always aligned; warn if not */
25443bc04e28SDouglas Anderson 	WARN_ON_ONCE(urb->setup_dma &&
25453bc04e28SDouglas Anderson 		     (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
25463bc04e28SDouglas Anderson 
25473bc04e28SDouglas Anderson 	ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
25483bc04e28SDouglas Anderson 	if (ret)
25493bc04e28SDouglas Anderson 		return ret;
25503bc04e28SDouglas Anderson 
25513bc04e28SDouglas Anderson 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
25523bc04e28SDouglas Anderson 	if (ret)
25533bc04e28SDouglas Anderson 		dwc2_free_dma_aligned_buffer(urb);
25543bc04e28SDouglas Anderson 
25553bc04e28SDouglas Anderson 	return ret;
25563bc04e28SDouglas Anderson }
25573bc04e28SDouglas Anderson 
25583bc04e28SDouglas Anderson static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
25593bc04e28SDouglas Anderson {
25603bc04e28SDouglas Anderson 	usb_hcd_unmap_urb_for_dma(hcd, urb);
25613bc04e28SDouglas Anderson 	dwc2_free_dma_aligned_buffer(urb);
2562197ba5f4SPaul Zimmerman }
2563197ba5f4SPaul Zimmerman 
2564197ba5f4SPaul Zimmerman /**
2565197ba5f4SPaul Zimmerman  * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2566197ba5f4SPaul Zimmerman  * channel and initializes the host channel to perform the transactions. The
2567197ba5f4SPaul Zimmerman  * host channel is removed from the free list.
2568197ba5f4SPaul Zimmerman  *
2569197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2570197ba5f4SPaul Zimmerman  * @qh:    Transactions from the first QTD for this QH are selected and assigned
2571197ba5f4SPaul Zimmerman  *         to a free host channel
2572197ba5f4SPaul Zimmerman  */
2573197ba5f4SPaul Zimmerman static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
2574197ba5f4SPaul Zimmerman {
2575197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
2576197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
2577197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
2578197ba5f4SPaul Zimmerman 
2579197ba5f4SPaul Zimmerman 	if (dbg_qh(qh))
2580197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
2581197ba5f4SPaul Zimmerman 
2582197ba5f4SPaul Zimmerman 	if (list_empty(&qh->qtd_list)) {
2583197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No QTDs in QH list\n");
2584197ba5f4SPaul Zimmerman 		return -ENOMEM;
2585197ba5f4SPaul Zimmerman 	}
2586197ba5f4SPaul Zimmerman 
2587197ba5f4SPaul Zimmerman 	if (list_empty(&hsotg->free_hc_list)) {
2588197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No free channel to assign\n");
2589197ba5f4SPaul Zimmerman 		return -ENOMEM;
2590197ba5f4SPaul Zimmerman 	}
2591197ba5f4SPaul Zimmerman 
2592197ba5f4SPaul Zimmerman 	chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2593197ba5f4SPaul Zimmerman 				hc_list_entry);
2594197ba5f4SPaul Zimmerman 
2595197ba5f4SPaul Zimmerman 	/* Remove host channel from free list */
2596197ba5f4SPaul Zimmerman 	list_del_init(&chan->hc_list_entry);
2597197ba5f4SPaul Zimmerman 
2598197ba5f4SPaul Zimmerman 	qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2599197ba5f4SPaul Zimmerman 	urb = qtd->urb;
2600197ba5f4SPaul Zimmerman 	qh->channel = chan;
2601197ba5f4SPaul Zimmerman 	qtd->in_process = 1;
2602197ba5f4SPaul Zimmerman 
2603197ba5f4SPaul Zimmerman 	/*
2604197ba5f4SPaul Zimmerman 	 * Use usb_pipedevice to determine device address. This address is
2605197ba5f4SPaul Zimmerman 	 * 0 before the SET_ADDRESS command and the correct address afterward.
2606197ba5f4SPaul Zimmerman 	 */
2607197ba5f4SPaul Zimmerman 	chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2608197ba5f4SPaul Zimmerman 	chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2609197ba5f4SPaul Zimmerman 	chan->speed = qh->dev_speed;
2610babd1839SDouglas Anderson 	chan->max_packet = qh->maxp;
2611197ba5f4SPaul Zimmerman 
2612197ba5f4SPaul Zimmerman 	chan->xfer_started = 0;
2613197ba5f4SPaul Zimmerman 	chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2614197ba5f4SPaul Zimmerman 	chan->error_state = (qtd->error_count > 0);
2615197ba5f4SPaul Zimmerman 	chan->halt_on_queue = 0;
2616197ba5f4SPaul Zimmerman 	chan->halt_pending = 0;
2617197ba5f4SPaul Zimmerman 	chan->requests = 0;
2618197ba5f4SPaul Zimmerman 
2619197ba5f4SPaul Zimmerman 	/*
2620197ba5f4SPaul Zimmerman 	 * The following values may be modified in the transfer type section
2621197ba5f4SPaul Zimmerman 	 * below. The xfer_len value may be reduced when the transfer is
2622197ba5f4SPaul Zimmerman 	 * started to accommodate the max widths of the XferSize and PktCnt
2623197ba5f4SPaul Zimmerman 	 * fields in the HCTSIZn register.
2624197ba5f4SPaul Zimmerman 	 */
2625197ba5f4SPaul Zimmerman 
2626197ba5f4SPaul Zimmerman 	chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2627197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
2628197ba5f4SPaul Zimmerman 		chan->do_ping = 0;
2629197ba5f4SPaul Zimmerman 	else
2630197ba5f4SPaul Zimmerman 		chan->do_ping = qh->ping_state;
2631197ba5f4SPaul Zimmerman 
2632197ba5f4SPaul Zimmerman 	chan->data_pid_start = qh->data_toggle;
2633197ba5f4SPaul Zimmerman 	chan->multi_count = 1;
2634197ba5f4SPaul Zimmerman 
2635197ba5f4SPaul Zimmerman 	if (urb->actual_length > urb->length &&
2636197ba5f4SPaul Zimmerman 	    !dwc2_hcd_is_pipe_in(&urb->pipe_info))
2637197ba5f4SPaul Zimmerman 		urb->actual_length = urb->length;
2638197ba5f4SPaul Zimmerman 
263995832c00SJohn Youn 	if (hsotg->params.host_dma)
2640197ba5f4SPaul Zimmerman 		chan->xfer_dma = urb->dma + urb->actual_length;
26413bc04e28SDouglas Anderson 	else
2642197ba5f4SPaul Zimmerman 		chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
2643197ba5f4SPaul Zimmerman 
2644197ba5f4SPaul Zimmerman 	chan->xfer_len = urb->length - urb->actual_length;
2645197ba5f4SPaul Zimmerman 	chan->xfer_count = 0;
2646197ba5f4SPaul Zimmerman 
2647197ba5f4SPaul Zimmerman 	/* Set the split attributes if required */
2648197ba5f4SPaul Zimmerman 	if (qh->do_split)
2649197ba5f4SPaul Zimmerman 		dwc2_hc_init_split(hsotg, chan, qtd, urb);
2650197ba5f4SPaul Zimmerman 	else
2651197ba5f4SPaul Zimmerman 		chan->do_split = 0;
2652197ba5f4SPaul Zimmerman 
2653197ba5f4SPaul Zimmerman 	/* Set the transfer attributes */
26543bc04e28SDouglas Anderson 	dwc2_hc_init_xfer(hsotg, chan, qtd);
2655197ba5f4SPaul Zimmerman 
2656af424a41SWilliam Wu 	/* For non-dword aligned buffers */
2657af424a41SWilliam Wu 	if (hsotg->params.host_dma && qh->do_split &&
2658af424a41SWilliam Wu 	    chan->ep_is_in && (chan->xfer_dma & 0x3)) {
2659af424a41SWilliam Wu 		dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
2660af424a41SWilliam Wu 		if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
2661af424a41SWilliam Wu 			dev_err(hsotg->dev,
2662af424a41SWilliam Wu 				"Failed to allocate memory to handle non-aligned buffer\n");
2663af424a41SWilliam Wu 			/* Add channel back to free list */
2664af424a41SWilliam Wu 			chan->align_buf = 0;
2665af424a41SWilliam Wu 			chan->multi_count = 0;
2666af424a41SWilliam Wu 			list_add_tail(&chan->hc_list_entry,
2667af424a41SWilliam Wu 				      &hsotg->free_hc_list);
2668af424a41SWilliam Wu 			qtd->in_process = 0;
2669af424a41SWilliam Wu 			qh->channel = NULL;
2670af424a41SWilliam Wu 			return -ENOMEM;
2671af424a41SWilliam Wu 		}
2672af424a41SWilliam Wu 	} else {
2673af424a41SWilliam Wu 		/*
2674af424a41SWilliam Wu 		 * We assume that DMA is always aligned in non-split
2675af424a41SWilliam Wu 		 * case or split out case. Warn if not.
2676af424a41SWilliam Wu 		 */
2677af424a41SWilliam Wu 		WARN_ON_ONCE(hsotg->params.host_dma &&
2678af424a41SWilliam Wu 			     (chan->xfer_dma & 0x3));
2679af424a41SWilliam Wu 		chan->align_buf = 0;
2680af424a41SWilliam Wu 	}
2681af424a41SWilliam Wu 
2682197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2683197ba5f4SPaul Zimmerman 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2684197ba5f4SPaul Zimmerman 		/*
2685197ba5f4SPaul Zimmerman 		 * This value may be modified when the transfer is started
2686197ba5f4SPaul Zimmerman 		 * to reflect the actual transfer length
2687197ba5f4SPaul Zimmerman 		 */
2688babd1839SDouglas Anderson 		chan->multi_count = qh->maxp_mult;
2689197ba5f4SPaul Zimmerman 
269095832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2691197ba5f4SPaul Zimmerman 		chan->desc_list_addr = qh->desc_list_dma;
269295105a99SGregory Herrero 		chan->desc_list_sz = qh->desc_list_sz;
269395105a99SGregory Herrero 	}
2694197ba5f4SPaul Zimmerman 
2695197ba5f4SPaul Zimmerman 	dwc2_hc_init(hsotg, chan);
2696197ba5f4SPaul Zimmerman 	chan->qh = qh;
2697197ba5f4SPaul Zimmerman 
2698197ba5f4SPaul Zimmerman 	return 0;
2699197ba5f4SPaul Zimmerman }
2700197ba5f4SPaul Zimmerman 
2701197ba5f4SPaul Zimmerman /**
2702197ba5f4SPaul Zimmerman  * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2703197ba5f4SPaul Zimmerman  * schedule and assigns them to available host channels. Called from the HCD
2704197ba5f4SPaul Zimmerman  * interrupt handler functions.
2705197ba5f4SPaul Zimmerman  *
2706197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2707197ba5f4SPaul Zimmerman  *
2708197ba5f4SPaul Zimmerman  * Return: The types of new transactions that were assigned to host channels
2709197ba5f4SPaul Zimmerman  */
2710197ba5f4SPaul Zimmerman enum dwc2_transaction_type dwc2_hcd_select_transactions(
2711197ba5f4SPaul Zimmerman 		struct dwc2_hsotg *hsotg)
2712197ba5f4SPaul Zimmerman {
2713197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2714197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2715197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2716197ba5f4SPaul Zimmerman 	int num_channels;
2717197ba5f4SPaul Zimmerman 
2718197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
2719197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Select Transactions\n");
2720197ba5f4SPaul Zimmerman #endif
2721197ba5f4SPaul Zimmerman 
2722197ba5f4SPaul Zimmerman 	/* Process entries in the periodic ready list */
2723197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_ready.next;
2724197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_ready) {
2725197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2726197ba5f4SPaul Zimmerman 			break;
272795832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2728197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels <= 1)
2729197ba5f4SPaul Zimmerman 				break;
2730197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2731197ba5f4SPaul Zimmerman 		}
2732197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2733197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2734197ba5f4SPaul Zimmerman 			break;
2735197ba5f4SPaul Zimmerman 
2736197ba5f4SPaul Zimmerman 		/*
2737197ba5f4SPaul Zimmerman 		 * Move the QH from the periodic ready schedule to the
2738197ba5f4SPaul Zimmerman 		 * periodic assigned schedule
2739197ba5f4SPaul Zimmerman 		 */
2740197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
274194ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
274294ef7aeeSDouglas Anderson 			       &hsotg->periodic_sched_assigned);
2743197ba5f4SPaul Zimmerman 		ret_val = DWC2_TRANSACTION_PERIODIC;
2744197ba5f4SPaul Zimmerman 	}
2745197ba5f4SPaul Zimmerman 
2746197ba5f4SPaul Zimmerman 	/*
2747197ba5f4SPaul Zimmerman 	 * Process entries in the inactive portion of the non-periodic
2748197ba5f4SPaul Zimmerman 	 * schedule. Some free host channels may not be used if they are
2749197ba5f4SPaul Zimmerman 	 * reserved for periodic transfers.
2750197ba5f4SPaul Zimmerman 	 */
2751bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2752197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->non_periodic_sched_inactive.next;
2753197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
275495832c00SJohn Youn 		if (!hsotg->params.uframe_sched &&
2755197ba5f4SPaul Zimmerman 		    hsotg->non_periodic_channels >= num_channels -
2756197ba5f4SPaul Zimmerman 						hsotg->periodic_channels)
2757197ba5f4SPaul Zimmerman 			break;
2758197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2759197ba5f4SPaul Zimmerman 			break;
2760197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
276195832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2762197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels < 1)
2763197ba5f4SPaul Zimmerman 				break;
2764197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2765197ba5f4SPaul Zimmerman 		}
2766197ba5f4SPaul Zimmerman 
2767197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2768197ba5f4SPaul Zimmerman 			break;
2769197ba5f4SPaul Zimmerman 
2770197ba5f4SPaul Zimmerman 		/*
2771197ba5f4SPaul Zimmerman 		 * Move the QH from the non-periodic inactive schedule to the
2772197ba5f4SPaul Zimmerman 		 * non-periodic active schedule
2773197ba5f4SPaul Zimmerman 		 */
2774197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
277594ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
2776197ba5f4SPaul Zimmerman 			       &hsotg->non_periodic_sched_active);
2777197ba5f4SPaul Zimmerman 
2778197ba5f4SPaul Zimmerman 		if (ret_val == DWC2_TRANSACTION_NONE)
2779197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2780197ba5f4SPaul Zimmerman 		else
2781197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_ALL;
2782197ba5f4SPaul Zimmerman 
278395832c00SJohn Youn 		if (!hsotg->params.uframe_sched)
2784197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels++;
2785197ba5f4SPaul Zimmerman 	}
2786197ba5f4SPaul Zimmerman 
2787197ba5f4SPaul Zimmerman 	return ret_val;
2788197ba5f4SPaul Zimmerman }
2789197ba5f4SPaul Zimmerman 
2790197ba5f4SPaul Zimmerman /**
2791197ba5f4SPaul Zimmerman  * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2792197ba5f4SPaul Zimmerman  * a host channel associated with either a periodic or non-periodic transfer
2793197ba5f4SPaul Zimmerman  *
2794197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2795197ba5f4SPaul Zimmerman  * @chan:  Host channel descriptor associated with either a periodic or
2796197ba5f4SPaul Zimmerman  *         non-periodic transfer
2797197ba5f4SPaul Zimmerman  * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2798197ba5f4SPaul Zimmerman  *                     for periodic transfers or the non-periodic Tx FIFO
2799197ba5f4SPaul Zimmerman  *                     for non-periodic transfers
2800197ba5f4SPaul Zimmerman  *
2801197ba5f4SPaul Zimmerman  * Return: 1 if a request is queued and more requests may be needed to
2802197ba5f4SPaul Zimmerman  * complete the transfer, 0 if no more requests are required for this
2803197ba5f4SPaul Zimmerman  * transfer, -1 if there is insufficient space in the Tx FIFO
2804197ba5f4SPaul Zimmerman  *
2805197ba5f4SPaul Zimmerman  * This function assumes that there is space available in the appropriate
2806197ba5f4SPaul Zimmerman  * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2807197ba5f4SPaul Zimmerman  * it checks whether space is available in the appropriate Tx FIFO.
2808197ba5f4SPaul Zimmerman  *
2809197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2810197ba5f4SPaul Zimmerman  */
2811197ba5f4SPaul Zimmerman static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2812197ba5f4SPaul Zimmerman 				  struct dwc2_host_chan *chan,
2813197ba5f4SPaul Zimmerman 				  u16 fifo_dwords_avail)
2814197ba5f4SPaul Zimmerman {
2815197ba5f4SPaul Zimmerman 	int retval = 0;
2816197ba5f4SPaul Zimmerman 
2817c9c8ac01SDouglas Anderson 	if (chan->do_split)
2818c9c8ac01SDouglas Anderson 		/* Put ourselves on the list to keep order straight */
2819c9c8ac01SDouglas Anderson 		list_move_tail(&chan->split_order_list_entry,
2820c9c8ac01SDouglas Anderson 			       &hsotg->split_order);
2821c9c8ac01SDouglas Anderson 
28227b813767SAlexandru M Stan 	if (hsotg->params.host_dma && chan->qh) {
282395832c00SJohn Youn 		if (hsotg->params.dma_desc_enable) {
2824197ba5f4SPaul Zimmerman 			if (!chan->xfer_started ||
2825197ba5f4SPaul Zimmerman 			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2826197ba5f4SPaul Zimmerman 				dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2827197ba5f4SPaul Zimmerman 				chan->qh->ping_state = 0;
2828197ba5f4SPaul Zimmerman 			}
2829197ba5f4SPaul Zimmerman 		} else if (!chan->xfer_started) {
2830197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2831197ba5f4SPaul Zimmerman 			chan->qh->ping_state = 0;
2832197ba5f4SPaul Zimmerman 		}
2833197ba5f4SPaul Zimmerman 	} else if (chan->halt_pending) {
2834197ba5f4SPaul Zimmerman 		/* Don't queue a request if the channel has been halted */
2835197ba5f4SPaul Zimmerman 	} else if (chan->halt_on_queue) {
2836197ba5f4SPaul Zimmerman 		dwc2_hc_halt(hsotg, chan, chan->halt_status);
2837197ba5f4SPaul Zimmerman 	} else if (chan->do_ping) {
2838197ba5f4SPaul Zimmerman 		if (!chan->xfer_started)
2839197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2840197ba5f4SPaul Zimmerman 	} else if (!chan->ep_is_in ||
2841197ba5f4SPaul Zimmerman 		   chan->data_pid_start == DWC2_HC_PID_SETUP) {
2842197ba5f4SPaul Zimmerman 		if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2843197ba5f4SPaul Zimmerman 			if (!chan->xfer_started) {
2844197ba5f4SPaul Zimmerman 				dwc2_hc_start_transfer(hsotg, chan);
2845197ba5f4SPaul Zimmerman 				retval = 1;
2846197ba5f4SPaul Zimmerman 			} else {
2847197ba5f4SPaul Zimmerman 				retval = dwc2_hc_continue_transfer(hsotg, chan);
2848197ba5f4SPaul Zimmerman 			}
2849197ba5f4SPaul Zimmerman 		} else {
2850197ba5f4SPaul Zimmerman 			retval = -1;
2851197ba5f4SPaul Zimmerman 		}
2852197ba5f4SPaul Zimmerman 	} else {
2853197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
2854197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2855197ba5f4SPaul Zimmerman 			retval = 1;
2856197ba5f4SPaul Zimmerman 		} else {
2857197ba5f4SPaul Zimmerman 			retval = dwc2_hc_continue_transfer(hsotg, chan);
2858197ba5f4SPaul Zimmerman 		}
2859197ba5f4SPaul Zimmerman 	}
2860197ba5f4SPaul Zimmerman 
2861197ba5f4SPaul Zimmerman 	return retval;
2862197ba5f4SPaul Zimmerman }
2863197ba5f4SPaul Zimmerman 
2864197ba5f4SPaul Zimmerman /*
2865197ba5f4SPaul Zimmerman  * Processes periodic channels for the next frame and queues transactions for
2866197ba5f4SPaul Zimmerman  * these channels to the DWC_otg controller. After queueing transactions, the
2867197ba5f4SPaul Zimmerman  * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2868197ba5f4SPaul Zimmerman  * to queue as Periodic Tx FIFO or request queue space becomes available.
2869197ba5f4SPaul Zimmerman  * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2870197ba5f4SPaul Zimmerman  *
2871197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2872197ba5f4SPaul Zimmerman  */
2873197ba5f4SPaul Zimmerman static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2874197ba5f4SPaul Zimmerman {
2875197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2876197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2877197ba5f4SPaul Zimmerman 	u32 tx_status;
2878197ba5f4SPaul Zimmerman 	u32 fspcavail;
2879197ba5f4SPaul Zimmerman 	u32 gintmsk;
2880197ba5f4SPaul Zimmerman 	int status;
28814e50e011SDouglas Anderson 	bool no_queue_space = false;
28824e50e011SDouglas Anderson 	bool no_fifo_space = false;
2883197ba5f4SPaul Zimmerman 	u32 qspcavail;
2884197ba5f4SPaul Zimmerman 
28854e50e011SDouglas Anderson 	/* If empty list then just adjust interrupt enables */
28864e50e011SDouglas Anderson 	if (list_empty(&hsotg->periodic_sched_assigned))
28874e50e011SDouglas Anderson 		goto exit;
28884e50e011SDouglas Anderson 
2889197ba5f4SPaul Zimmerman 	if (dbg_perio())
2890197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
2891197ba5f4SPaul Zimmerman 
2892f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, HPTXSTS);
2893197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2894197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
2895197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2896197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
2897197ba5f4SPaul Zimmerman 
2898197ba5f4SPaul Zimmerman 	if (dbg_perio()) {
2899197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx Req Queue Space Avail (before queue): %d\n",
2900197ba5f4SPaul Zimmerman 			 qspcavail);
2901197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx FIFO Space Avail (before queue): %d\n",
2902197ba5f4SPaul Zimmerman 			 fspcavail);
2903197ba5f4SPaul Zimmerman 	}
2904197ba5f4SPaul Zimmerman 
2905197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_assigned.next;
2906197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_assigned) {
2907f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, HPTXSTS);
2908197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2909197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
2910197ba5f4SPaul Zimmerman 		if (qspcavail == 0) {
2911fdb09b3eSNicholas Mc Guire 			no_queue_space = true;
2912197ba5f4SPaul Zimmerman 			break;
2913197ba5f4SPaul Zimmerman 		}
2914197ba5f4SPaul Zimmerman 
2915197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2916197ba5f4SPaul Zimmerman 		if (!qh->channel) {
2917197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2918197ba5f4SPaul Zimmerman 			continue;
2919197ba5f4SPaul Zimmerman 		}
2920197ba5f4SPaul Zimmerman 
2921197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
2922197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty) {
2923197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2924197ba5f4SPaul Zimmerman 			continue;
2925197ba5f4SPaul Zimmerman 		}
2926197ba5f4SPaul Zimmerman 
2927197ba5f4SPaul Zimmerman 		/*
2928197ba5f4SPaul Zimmerman 		 * Set a flag if we're queuing high-bandwidth in slave mode.
2929197ba5f4SPaul Zimmerman 		 * The flag prevents any halts to get into the request queue in
2930197ba5f4SPaul Zimmerman 		 * the middle of multiple high-bandwidth packets getting queued.
2931197ba5f4SPaul Zimmerman 		 */
293295832c00SJohn Youn 		if (!hsotg->params.host_dma &&
2933197ba5f4SPaul Zimmerman 		    qh->channel->multi_count > 1)
2934197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 1;
2935197ba5f4SPaul Zimmerman 
2936197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2937197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
2938197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
2939197ba5f4SPaul Zimmerman 		if (status < 0) {
2940fdb09b3eSNicholas Mc Guire 			no_fifo_space = true;
2941197ba5f4SPaul Zimmerman 			break;
2942197ba5f4SPaul Zimmerman 		}
2943197ba5f4SPaul Zimmerman 
2944197ba5f4SPaul Zimmerman 		/*
2945197ba5f4SPaul Zimmerman 		 * In Slave mode, stay on the current transfer until there is
2946197ba5f4SPaul Zimmerman 		 * nothing more to do or the high-bandwidth request count is
2947197ba5f4SPaul Zimmerman 		 * reached. In DMA mode, only need to queue one request. The
2948197ba5f4SPaul Zimmerman 		 * controller automatically handles multiple packets for
2949197ba5f4SPaul Zimmerman 		 * high-bandwidth transfers.
2950197ba5f4SPaul Zimmerman 		 */
295195832c00SJohn Youn 		if (hsotg->params.host_dma || status == 0 ||
2952197ba5f4SPaul Zimmerman 		    qh->channel->requests == qh->channel->multi_count) {
2953197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2954197ba5f4SPaul Zimmerman 			/*
2955197ba5f4SPaul Zimmerman 			 * Move the QH from the periodic assigned schedule to
2956197ba5f4SPaul Zimmerman 			 * the periodic queued schedule
2957197ba5f4SPaul Zimmerman 			 */
295894ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
2959197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_queued);
2960197ba5f4SPaul Zimmerman 
2961197ba5f4SPaul Zimmerman 			/* done queuing high bandwidth */
2962197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 0;
2963197ba5f4SPaul Zimmerman 		}
2964197ba5f4SPaul Zimmerman 	}
2965197ba5f4SPaul Zimmerman 
29664e50e011SDouglas Anderson exit:
29674e50e011SDouglas Anderson 	if (no_queue_space || no_fifo_space ||
296895832c00SJohn Youn 	    (!hsotg->params.host_dma &&
29694e50e011SDouglas Anderson 	     !list_empty(&hsotg->periodic_sched_assigned))) {
2970197ba5f4SPaul Zimmerman 		/*
2971197ba5f4SPaul Zimmerman 		 * May need to queue more transactions as the request
2972197ba5f4SPaul Zimmerman 		 * queue or Tx FIFO empties. Enable the periodic Tx
2973197ba5f4SPaul Zimmerman 		 * FIFO empty interrupt. (Always use the half-empty
2974197ba5f4SPaul Zimmerman 		 * level to ensure that new requests are loaded as
2975197ba5f4SPaul Zimmerman 		 * soon as possible.)
2976197ba5f4SPaul Zimmerman 		 */
2977f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29784e50e011SDouglas Anderson 		if (!(gintmsk & GINTSTS_PTXFEMP)) {
2979197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_PTXFEMP;
2980f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
29814e50e011SDouglas Anderson 		}
2982197ba5f4SPaul Zimmerman 	} else {
2983197ba5f4SPaul Zimmerman 		/*
2984197ba5f4SPaul Zimmerman 		 * Disable the Tx FIFO empty interrupt since there are
2985197ba5f4SPaul Zimmerman 		 * no more transactions that need to be queued right
2986197ba5f4SPaul Zimmerman 		 * now. This function is called from interrupt
2987197ba5f4SPaul Zimmerman 		 * handlers to queue more transactions as transfer
2988197ba5f4SPaul Zimmerman 		 * states change.
2989197ba5f4SPaul Zimmerman 		 */
2990f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29914e50e011SDouglas Anderson 		if (gintmsk & GINTSTS_PTXFEMP) {
2992197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_PTXFEMP;
2993f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
2994197ba5f4SPaul Zimmerman 		}
2995197ba5f4SPaul Zimmerman 	}
2996197ba5f4SPaul Zimmerman }
2997197ba5f4SPaul Zimmerman 
2998197ba5f4SPaul Zimmerman /*
2999197ba5f4SPaul Zimmerman  * Processes active non-periodic channels and queues transactions for these
3000197ba5f4SPaul Zimmerman  * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3001197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is enabled if there are more transactions to queue as
3002197ba5f4SPaul Zimmerman  * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3003197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is disabled.
3004197ba5f4SPaul Zimmerman  *
3005197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3006197ba5f4SPaul Zimmerman  */
3007197ba5f4SPaul Zimmerman static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3008197ba5f4SPaul Zimmerman {
3009197ba5f4SPaul Zimmerman 	struct list_head *orig_qh_ptr;
3010197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
3011197ba5f4SPaul Zimmerman 	u32 tx_status;
3012197ba5f4SPaul Zimmerman 	u32 qspcavail;
3013197ba5f4SPaul Zimmerman 	u32 fspcavail;
3014197ba5f4SPaul Zimmerman 	u32 gintmsk;
3015197ba5f4SPaul Zimmerman 	int status;
3016197ba5f4SPaul Zimmerman 	int no_queue_space = 0;
3017197ba5f4SPaul Zimmerman 	int no_fifo_space = 0;
3018197ba5f4SPaul Zimmerman 	int more_to_do = 0;
3019197ba5f4SPaul Zimmerman 
3020197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3021197ba5f4SPaul Zimmerman 
3022f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, GNPTXSTS);
3023197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3024197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
3025197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3026197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
3027197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx Req Queue Space Avail (before queue): %d\n",
3028197ba5f4SPaul Zimmerman 		 qspcavail);
3029197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx FIFO Space Avail (before queue): %d\n",
3030197ba5f4SPaul Zimmerman 		 fspcavail);
3031197ba5f4SPaul Zimmerman 
3032197ba5f4SPaul Zimmerman 	/*
3033197ba5f4SPaul Zimmerman 	 * Keep track of the starting point. Skip over the start-of-list
3034197ba5f4SPaul Zimmerman 	 * entry.
3035197ba5f4SPaul Zimmerman 	 */
3036197ba5f4SPaul Zimmerman 	if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3037197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3038197ba5f4SPaul Zimmerman 	orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3039197ba5f4SPaul Zimmerman 
3040197ba5f4SPaul Zimmerman 	/*
3041197ba5f4SPaul Zimmerman 	 * Process once through the active list or until no more space is
3042197ba5f4SPaul Zimmerman 	 * available in the request queue or the Tx FIFO
3043197ba5f4SPaul Zimmerman 	 */
3044197ba5f4SPaul Zimmerman 	do {
3045f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3046197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3047197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
304895832c00SJohn Youn 		if (!hsotg->params.host_dma && qspcavail == 0) {
3049197ba5f4SPaul Zimmerman 			no_queue_space = 1;
3050197ba5f4SPaul Zimmerman 			break;
3051197ba5f4SPaul Zimmerman 		}
3052197ba5f4SPaul Zimmerman 
3053197ba5f4SPaul Zimmerman 		qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3054197ba5f4SPaul Zimmerman 				qh_list_entry);
3055197ba5f4SPaul Zimmerman 		if (!qh->channel)
3056197ba5f4SPaul Zimmerman 			goto next;
3057197ba5f4SPaul Zimmerman 
3058197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
3059197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty)
3060197ba5f4SPaul Zimmerman 			goto next;
3061197ba5f4SPaul Zimmerman 
3062197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3063197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3064197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3065197ba5f4SPaul Zimmerman 
3066197ba5f4SPaul Zimmerman 		if (status > 0) {
3067197ba5f4SPaul Zimmerman 			more_to_do = 1;
3068197ba5f4SPaul Zimmerman 		} else if (status < 0) {
3069197ba5f4SPaul Zimmerman 			no_fifo_space = 1;
3070197ba5f4SPaul Zimmerman 			break;
3071197ba5f4SPaul Zimmerman 		}
3072197ba5f4SPaul Zimmerman next:
3073197ba5f4SPaul Zimmerman 		/* Advance to next QH, skipping start-of-list entry */
3074197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3075197ba5f4SPaul Zimmerman 		if (hsotg->non_periodic_qh_ptr ==
3076197ba5f4SPaul Zimmerman 				&hsotg->non_periodic_sched_active)
3077197ba5f4SPaul Zimmerman 			hsotg->non_periodic_qh_ptr =
3078197ba5f4SPaul Zimmerman 					hsotg->non_periodic_qh_ptr->next;
3079197ba5f4SPaul Zimmerman 	} while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3080197ba5f4SPaul Zimmerman 
308195832c00SJohn Youn 	if (!hsotg->params.host_dma) {
3082f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3083197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3084197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
3085197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3086197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3087197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3088197ba5f4SPaul Zimmerman 			 "  NP Tx Req Queue Space Avail (after queue): %d\n",
3089197ba5f4SPaul Zimmerman 			 qspcavail);
3090197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3091197ba5f4SPaul Zimmerman 			 "  NP Tx FIFO Space Avail (after queue): %d\n",
3092197ba5f4SPaul Zimmerman 			 fspcavail);
3093197ba5f4SPaul Zimmerman 
3094197ba5f4SPaul Zimmerman 		if (more_to_do || no_queue_space || no_fifo_space) {
3095197ba5f4SPaul Zimmerman 			/*
3096197ba5f4SPaul Zimmerman 			 * May need to queue more transactions as the request
3097197ba5f4SPaul Zimmerman 			 * queue or Tx FIFO empties. Enable the non-periodic
3098197ba5f4SPaul Zimmerman 			 * Tx FIFO empty interrupt. (Always use the half-empty
3099197ba5f4SPaul Zimmerman 			 * level to ensure that new requests are loaded as
3100197ba5f4SPaul Zimmerman 			 * soon as possible.)
3101197ba5f4SPaul Zimmerman 			 */
3102f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3103197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_NPTXFEMP;
3104f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3105197ba5f4SPaul Zimmerman 		} else {
3106197ba5f4SPaul Zimmerman 			/*
3107197ba5f4SPaul Zimmerman 			 * Disable the Tx FIFO empty interrupt since there are
3108197ba5f4SPaul Zimmerman 			 * no more transactions that need to be queued right
3109197ba5f4SPaul Zimmerman 			 * now. This function is called from interrupt
3110197ba5f4SPaul Zimmerman 			 * handlers to queue more transactions as transfer
3111197ba5f4SPaul Zimmerman 			 * states change.
3112197ba5f4SPaul Zimmerman 			 */
3113f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3114197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3115f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3116197ba5f4SPaul Zimmerman 		}
3117197ba5f4SPaul Zimmerman 	}
3118197ba5f4SPaul Zimmerman }
3119197ba5f4SPaul Zimmerman 
3120197ba5f4SPaul Zimmerman /**
3121197ba5f4SPaul Zimmerman  * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3122197ba5f4SPaul Zimmerman  * and queues transactions for these channels to the DWC_otg controller. Called
3123197ba5f4SPaul Zimmerman  * from the HCD interrupt handler functions.
3124197ba5f4SPaul Zimmerman  *
3125197ba5f4SPaul Zimmerman  * @hsotg:   The HCD state structure
3126197ba5f4SPaul Zimmerman  * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3127197ba5f4SPaul Zimmerman  *           or both)
3128197ba5f4SPaul Zimmerman  *
3129197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3130197ba5f4SPaul Zimmerman  */
3131197ba5f4SPaul Zimmerman void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3132197ba5f4SPaul Zimmerman 				 enum dwc2_transaction_type tr_type)
3133197ba5f4SPaul Zimmerman {
3134197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3135197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue Transactions\n");
3136197ba5f4SPaul Zimmerman #endif
3137197ba5f4SPaul Zimmerman 	/* Process host channels associated with periodic transfers */
31384e50e011SDouglas Anderson 	if (tr_type == DWC2_TRANSACTION_PERIODIC ||
31394e50e011SDouglas Anderson 	    tr_type == DWC2_TRANSACTION_ALL)
3140197ba5f4SPaul Zimmerman 		dwc2_process_periodic_channels(hsotg);
3141197ba5f4SPaul Zimmerman 
3142197ba5f4SPaul Zimmerman 	/* Process host channels associated with non-periodic transfers */
3143197ba5f4SPaul Zimmerman 	if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3144197ba5f4SPaul Zimmerman 	    tr_type == DWC2_TRANSACTION_ALL) {
3145197ba5f4SPaul Zimmerman 		if (!list_empty(&hsotg->non_periodic_sched_active)) {
3146197ba5f4SPaul Zimmerman 			dwc2_process_non_periodic_channels(hsotg);
3147197ba5f4SPaul Zimmerman 		} else {
3148197ba5f4SPaul Zimmerman 			/*
3149197ba5f4SPaul Zimmerman 			 * Ensure NP Tx FIFO empty interrupt is disabled when
3150197ba5f4SPaul Zimmerman 			 * there are no non-periodic transfers to process
3151197ba5f4SPaul Zimmerman 			 */
3152f25c42b8SGevorg Sahakyan 			u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
3153197ba5f4SPaul Zimmerman 
3154197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3155f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3156197ba5f4SPaul Zimmerman 		}
3157197ba5f4SPaul Zimmerman 	}
3158197ba5f4SPaul Zimmerman }
3159197ba5f4SPaul Zimmerman 
3160197ba5f4SPaul Zimmerman static void dwc2_conn_id_status_change(struct work_struct *work)
3161197ba5f4SPaul Zimmerman {
3162197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3163197ba5f4SPaul Zimmerman 						wf_otg);
3164197ba5f4SPaul Zimmerman 	u32 count = 0;
3165197ba5f4SPaul Zimmerman 	u32 gotgctl;
31665390d438SMian Yousaf Kaukab 	unsigned long flags;
3167197ba5f4SPaul Zimmerman 
3168197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3169197ba5f4SPaul Zimmerman 
3170f25c42b8SGevorg Sahakyan 	gotgctl = dwc2_readl(hsotg, GOTGCTL);
3171197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3172197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3173197ba5f4SPaul Zimmerman 		!!(gotgctl & GOTGCTL_CONID_B));
3174197ba5f4SPaul Zimmerman 
3175197ba5f4SPaul Zimmerman 	/* B-Device connector (Device Mode) */
3176197ba5f4SPaul Zimmerman 	if (gotgctl & GOTGCTL_CONID_B) {
3177531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
3178197ba5f4SPaul Zimmerman 		/* Wait for switch to device mode */
3179197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId B\n");
31809156a7efSChen Yu 		if (hsotg->bus_suspended) {
31819156a7efSChen Yu 			dev_info(hsotg->dev,
31829156a7efSChen Yu 				 "Do port resume before switching to device mode\n");
31839156a7efSChen Yu 			dwc2_port_resume(hsotg);
31849156a7efSChen Yu 		}
3185197ba5f4SPaul Zimmerman 		while (!dwc2_is_device_mode(hsotg)) {
3186197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev,
3187197ba5f4SPaul Zimmerman 				 "Waiting for Peripheral Mode, Mode=%s\n",
3188197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ? "Host" :
3189197ba5f4SPaul Zimmerman 				 "Peripheral");
319004a9db79SNicholas Mc Guire 			msleep(20);
3191fc30c4bbSJohn Stultz 			/*
3192fc30c4bbSJohn Stultz 			 * Sometimes the initial GOTGCTRL read is wrong, so
3193fc30c4bbSJohn Stultz 			 * check it again and jump to host mode if that was
3194fc30c4bbSJohn Stultz 			 * the case.
3195fc30c4bbSJohn Stultz 			 */
3196f25c42b8SGevorg Sahakyan 			gotgctl = dwc2_readl(hsotg, GOTGCTL);
3197fc30c4bbSJohn Stultz 			if (!(gotgctl & GOTGCTL_CONID_B))
3198fc30c4bbSJohn Stultz 				goto host;
3199197ba5f4SPaul Zimmerman 			if (++count > 250)
3200197ba5f4SPaul Zimmerman 				break;
3201197ba5f4SPaul Zimmerman 		}
3202197ba5f4SPaul Zimmerman 		if (count > 250)
3203197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3204197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
32054d4d99afSArtur Petrosyan 
32064d4d99afSArtur Petrosyan 		/*
32074d4d99afSArtur Petrosyan 		 * Exit Partial Power Down without restoring registers.
32084d4d99afSArtur Petrosyan 		 * No need to check the return value as registers
32094d4d99afSArtur Petrosyan 		 * are not being restored.
32104d4d99afSArtur Petrosyan 		 */
32114d4d99afSArtur Petrosyan 		if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
32124d4d99afSArtur Petrosyan 			dwc2_exit_partial_power_down(hsotg, 0, false);
32134d4d99afSArtur Petrosyan 
3214197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
32150fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3216197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
32175390d438SMian Yousaf Kaukab 		spin_lock_irqsave(&hsotg->lock, flags);
32181f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
32195390d438SMian Yousaf Kaukab 		spin_unlock_irqrestore(&hsotg->lock, flags);
322066e77a24SRazmik Karapetyan 		/* Enable ACG feature in device mode,if supported */
322166e77a24SRazmik Karapetyan 		dwc2_enable_acg(hsotg);
32221f91b4ccSFelipe Balbi 		dwc2_hsotg_core_connect(hsotg);
3223197ba5f4SPaul Zimmerman 	} else {
3224fc30c4bbSJohn Stultz host:
3225197ba5f4SPaul Zimmerman 		/* A-Device connector (Host Mode) */
3226197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId A\n");
3227197ba5f4SPaul Zimmerman 		while (!dwc2_is_host_mode(hsotg)) {
3228197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3229197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ?
3230197ba5f4SPaul Zimmerman 				 "Host" : "Peripheral");
323104a9db79SNicholas Mc Guire 			msleep(20);
3232197ba5f4SPaul Zimmerman 			if (++count > 250)
3233197ba5f4SPaul Zimmerman 				break;
3234197ba5f4SPaul Zimmerman 		}
3235197ba5f4SPaul Zimmerman 		if (count > 250)
3236197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3237197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3238197ba5f4SPaul Zimmerman 
3239d2471d4aSJohn Stultz 		spin_lock_irqsave(&hsotg->lock, flags);
3240d2471d4aSJohn Stultz 		dwc2_hsotg_disconnect(hsotg);
3241d2471d4aSJohn Stultz 		spin_unlock_irqrestore(&hsotg->lock, flags);
3242d2471d4aSJohn Stultz 
3243d2471d4aSJohn Stultz 		hsotg->op_state = OTG_STATE_A_HOST;
3244197ba5f4SPaul Zimmerman 		/* Initialize the Core for Host mode */
32450fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3246197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
3247197ba5f4SPaul Zimmerman 		dwc2_hcd_start(hsotg);
3248197ba5f4SPaul Zimmerman 	}
3249197ba5f4SPaul Zimmerman }
3250197ba5f4SPaul Zimmerman 
3251e99e88a9SKees Cook static void dwc2_wakeup_detected(struct timer_list *t)
3252197ba5f4SPaul Zimmerman {
3253e99e88a9SKees Cook 	struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
3254197ba5f4SPaul Zimmerman 	u32 hprt0;
3255197ba5f4SPaul Zimmerman 
3256197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3257197ba5f4SPaul Zimmerman 
3258197ba5f4SPaul Zimmerman 	/*
3259197ba5f4SPaul Zimmerman 	 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3260197ba5f4SPaul Zimmerman 	 * so that OPT tests pass with all PHYs.)
3261197ba5f4SPaul Zimmerman 	 */
3262197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3263197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3264197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RES;
3265f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
3266197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
3267f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPRT0));
3268197ba5f4SPaul Zimmerman 
3269197ba5f4SPaul Zimmerman 	dwc2_hcd_rem_wakeup(hsotg);
3270fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
3271197ba5f4SPaul Zimmerman 
3272197ba5f4SPaul Zimmerman 	/* Change to L0 state */
3273197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L0;
3274197ba5f4SPaul Zimmerman }
3275197ba5f4SPaul Zimmerman 
3276197ba5f4SPaul Zimmerman static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3277197ba5f4SPaul Zimmerman {
3278197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3279197ba5f4SPaul Zimmerman 
3280197ba5f4SPaul Zimmerman 	return hcd->self.b_hnp_enable;
3281197ba5f4SPaul Zimmerman }
3282197ba5f4SPaul Zimmerman 
3283139fae7aSArtur Petrosyan /**
3284139fae7aSArtur Petrosyan  * dwc2_port_suspend() - Put controller in suspend mode for host.
3285139fae7aSArtur Petrosyan  *
3286139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3287139fae7aSArtur Petrosyan  * @windex: The control request wIndex field
3288139fae7aSArtur Petrosyan  *
328922ff0c8eSArtur Petrosyan  * Return: non-zero if failed to enter suspend mode for host.
329022ff0c8eSArtur Petrosyan  *
3291139fae7aSArtur Petrosyan  * This function is for entering Host mode suspend.
3292139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3293139fae7aSArtur Petrosyan  */
329422ff0c8eSArtur Petrosyan int dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3295197ba5f4SPaul Zimmerman {
3296197ba5f4SPaul Zimmerman 	unsigned long flags;
3297197ba5f4SPaul Zimmerman 	u32 pcgctl;
3298197ba5f4SPaul Zimmerman 	u32 gotgctl;
329922ff0c8eSArtur Petrosyan 	int ret = 0;
3300197ba5f4SPaul Zimmerman 
3301197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3302197ba5f4SPaul Zimmerman 
3303197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
3304197ba5f4SPaul Zimmerman 
3305197ba5f4SPaul Zimmerman 	if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
3306f25c42b8SGevorg Sahakyan 		gotgctl = dwc2_readl(hsotg, GOTGCTL);
3307197ba5f4SPaul Zimmerman 		gotgctl |= GOTGCTL_HSTSETHNPEN;
3308f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gotgctl, GOTGCTL);
3309197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_A_SUSPEND;
3310197ba5f4SPaul Zimmerman 	}
3311197ba5f4SPaul Zimmerman 
331222ff0c8eSArtur Petrosyan 	switch (hsotg->params.power_down) {
331322ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
331422ff0c8eSArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
331522ff0c8eSArtur Petrosyan 		if (ret)
331622ff0c8eSArtur Petrosyan 			dev_err(hsotg->dev,
331722ff0c8eSArtur Petrosyan 				"enter partial_power_down failed.\n");
331822ff0c8eSArtur Petrosyan 		break;
331922ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
33208f7f8689SArtur Petrosyan 		/*
33218f7f8689SArtur Petrosyan 		 * Perform spin unlock and lock because in
33228f7f8689SArtur Petrosyan 		 * "dwc2_host_enter_hibernation()" function there is a spinlock
33238f7f8689SArtur Petrosyan 		 * logic which prevents servicing of any IRQ during entering
33248f7f8689SArtur Petrosyan 		 * hibernation.
33258f7f8689SArtur Petrosyan 		 */
33268f7f8689SArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
33278f7f8689SArtur Petrosyan 		ret = dwc2_enter_hibernation(hsotg, 1);
33288f7f8689SArtur Petrosyan 		if (ret)
33298f7f8689SArtur Petrosyan 			dev_err(hsotg->dev, "enter hibernation failed.\n");
33308f7f8689SArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
33318f7f8689SArtur Petrosyan 		break;
333222ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3333a2a23d3fSGregory Herrero 		/*
3334d37b939cSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
3335d37b939cSArtur Petrosyan 		 * clock gating is used to save power.
3336a2a23d3fSGregory Herrero 		 */
3337c4a0f7a6SMarek Szyprowski 		if (!hsotg->params.no_clock_gating)
3338d37b939cSArtur Petrosyan 			dwc2_host_enter_clock_gating(hsotg);
3339d37b939cSArtur Petrosyan 		break;
334022ff0c8eSArtur Petrosyan 	}
3341197ba5f4SPaul Zimmerman 
3342197ba5f4SPaul Zimmerman 	/* For HNP the bus must be suspended for at least 200ms */
3343197ba5f4SPaul Zimmerman 	if (dwc2_host_is_b_hnp_enabled(hsotg)) {
3344f25c42b8SGevorg Sahakyan 		pcgctl = dwc2_readl(hsotg, PCGCTL);
3345197ba5f4SPaul Zimmerman 		pcgctl &= ~PCGCTL_STOPPCLK;
3346f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgctl, PCGCTL);
3347197ba5f4SPaul Zimmerman 
3348197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3349197ba5f4SPaul Zimmerman 
335004a9db79SNicholas Mc Guire 		msleep(200);
3351197ba5f4SPaul Zimmerman 	} else {
3352197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3353197ba5f4SPaul Zimmerman 	}
335422ff0c8eSArtur Petrosyan 
335522ff0c8eSArtur Petrosyan 	return ret;
3356197ba5f4SPaul Zimmerman }
3357197ba5f4SPaul Zimmerman 
3358139fae7aSArtur Petrosyan /**
3359139fae7aSArtur Petrosyan  * dwc2_port_resume() - Exit controller from suspend mode for host.
3360139fae7aSArtur Petrosyan  *
3361139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3362139fae7aSArtur Petrosyan  *
33631e0890ebSArtur Petrosyan  * Return: non-zero if failed to exit suspend mode for host.
33641e0890ebSArtur Petrosyan  *
3365139fae7aSArtur Petrosyan  * This function is for exiting Host mode suspend.
3366139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3367139fae7aSArtur Petrosyan  */
33681e0890ebSArtur Petrosyan int dwc2_port_resume(struct dwc2_hsotg *hsotg)
336930db103cSGregory Herrero {
337030db103cSGregory Herrero 	unsigned long flags;
33711e0890ebSArtur Petrosyan 	int ret = 0;
337230db103cSGregory Herrero 
33734d273c2aSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
33744d273c2aSDouglas Anderson 
33751e0890ebSArtur Petrosyan 	switch (hsotg->params.power_down) {
33761e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
33771e0890ebSArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
33781e0890ebSArtur Petrosyan 		if (ret)
33791e0890ebSArtur Petrosyan 			dev_err(hsotg->dev,
33801e0890ebSArtur Petrosyan 				"exit partial_power_down failed.\n");
33811e0890ebSArtur Petrosyan 		break;
33821e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
3383e358c215SArtur Petrosyan 		/* Exit host hibernation. */
3384e358c215SArtur Petrosyan 		ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
3385e358c215SArtur Petrosyan 		if (ret)
3386e358c215SArtur Petrosyan 			dev_err(hsotg->dev, "exit hibernation failed.\n");
3387e358c215SArtur Petrosyan 		break;
33881e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3389a2a23d3fSGregory Herrero 		/*
33903cf8143eSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
33913cf8143eSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
3392a2a23d3fSGregory Herrero 		 */
33934d273c2aSDouglas Anderson 		spin_unlock_irqrestore(&hsotg->lock, flags);
33943cf8143eSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
33954d273c2aSDouglas Anderson 		spin_lock_irqsave(&hsotg->lock, flags);
33963cf8143eSArtur Petrosyan 		break;
33971e0890ebSArtur Petrosyan 	}
33981e0890ebSArtur Petrosyan 
339930db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
34001e0890ebSArtur Petrosyan 
34011e0890ebSArtur Petrosyan 	return ret;
340230db103cSGregory Herrero }
340330db103cSGregory Herrero 
3404197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
3405197ba5f4SPaul Zimmerman static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3406197ba5f4SPaul Zimmerman 				u16 wvalue, u16 windex, char *buf, u16 wlength)
3407197ba5f4SPaul Zimmerman {
3408197ba5f4SPaul Zimmerman 	struct usb_hub_descriptor *hub_desc;
3409197ba5f4SPaul Zimmerman 	int retval = 0;
3410197ba5f4SPaul Zimmerman 	u32 hprt0;
3411197ba5f4SPaul Zimmerman 	u32 port_status;
3412197ba5f4SPaul Zimmerman 	u32 speed;
3413197ba5f4SPaul Zimmerman 	u32 pcgctl;
3414cd7cd0e6SFabrice Gasnier 	u32 pwr;
3415197ba5f4SPaul Zimmerman 
3416197ba5f4SPaul Zimmerman 	switch (typereq) {
3417197ba5f4SPaul Zimmerman 	case ClearHubFeature:
3418197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3419197ba5f4SPaul Zimmerman 
3420197ba5f4SPaul Zimmerman 		switch (wvalue) {
3421197ba5f4SPaul Zimmerman 		case C_HUB_LOCAL_POWER:
3422197ba5f4SPaul Zimmerman 		case C_HUB_OVER_CURRENT:
3423197ba5f4SPaul Zimmerman 			/* Nothing required here */
3424197ba5f4SPaul Zimmerman 			break;
3425197ba5f4SPaul Zimmerman 
3426197ba5f4SPaul Zimmerman 		default:
3427197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3428197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3429197ba5f4SPaul Zimmerman 				"ClearHubFeature request %1xh unknown\n",
3430197ba5f4SPaul Zimmerman 				wvalue);
3431197ba5f4SPaul Zimmerman 		}
3432197ba5f4SPaul Zimmerman 		break;
3433197ba5f4SPaul Zimmerman 
3434197ba5f4SPaul Zimmerman 	case ClearPortFeature:
3435197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_L1)
3436197ba5f4SPaul Zimmerman 			if (!windex || windex > 1)
3437197ba5f4SPaul Zimmerman 				goto error;
3438197ba5f4SPaul Zimmerman 		switch (wvalue) {
3439197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_ENABLE:
3440197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3441197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3442197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3443197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_ENA;
3444f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3445197ba5f4SPaul Zimmerman 			break;
3446197ba5f4SPaul Zimmerman 
3447197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3448197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3449197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
3450b0bb9bb6SPaul Zimmerman 
3451e358c215SArtur Petrosyan 			if (hsotg->bus_suspended)
3452e358c215SArtur Petrosyan 				retval = dwc2_port_resume(hsotg);
3453197ba5f4SPaul Zimmerman 			break;
3454197ba5f4SPaul Zimmerman 
3455197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3456197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3457197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_POWER\n");
3458197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3459cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3460197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_PWR;
3461f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3462cd7cd0e6SFabrice Gasnier 			if (pwr)
3463cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_exit(hsotg);
3464197ba5f4SPaul Zimmerman 			break;
3465197ba5f4SPaul Zimmerman 
3466197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3467197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3468197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3469197ba5f4SPaul Zimmerman 			/* Port indicator not supported */
3470197ba5f4SPaul Zimmerman 			break;
3471197ba5f4SPaul Zimmerman 
3472197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_CONNECTION:
3473197ba5f4SPaul Zimmerman 			/*
3474197ba5f4SPaul Zimmerman 			 * Clears driver's internal Connect Status Change flag
3475197ba5f4SPaul Zimmerman 			 */
3476197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3477197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3478197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change = 0;
3479197ba5f4SPaul Zimmerman 			break;
3480197ba5f4SPaul Zimmerman 
3481197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_RESET:
3482197ba5f4SPaul Zimmerman 			/* Clears driver's internal Port Reset Change flag */
3483197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3484197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3485197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change = 0;
3486197ba5f4SPaul Zimmerman 			break;
3487197ba5f4SPaul Zimmerman 
3488197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_ENABLE:
3489197ba5f4SPaul Zimmerman 			/*
3490197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Enable/Disable
3491197ba5f4SPaul Zimmerman 			 * Change flag
3492197ba5f4SPaul Zimmerman 			 */
3493197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3494197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3495197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change = 0;
3496197ba5f4SPaul Zimmerman 			break;
3497197ba5f4SPaul Zimmerman 
3498197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_SUSPEND:
3499197ba5f4SPaul Zimmerman 			/*
3500197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Suspend Change
3501197ba5f4SPaul Zimmerman 			 * flag, which is set when resume signaling on the host
3502197ba5f4SPaul Zimmerman 			 * port is complete
3503197ba5f4SPaul Zimmerman 			 */
3504197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3505197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3506197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change = 0;
3507197ba5f4SPaul Zimmerman 			break;
3508197ba5f4SPaul Zimmerman 
3509197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_PORT_L1:
3510197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3511197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3512197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_l1_change = 0;
3513197ba5f4SPaul Zimmerman 			break;
3514197ba5f4SPaul Zimmerman 
3515197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_OVER_CURRENT:
3516197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3517197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3518197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change = 0;
3519197ba5f4SPaul Zimmerman 			break;
3520197ba5f4SPaul Zimmerman 
3521197ba5f4SPaul Zimmerman 		default:
3522197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3523197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3524197ba5f4SPaul Zimmerman 				"ClearPortFeature request %1xh unknown or unsupported\n",
3525197ba5f4SPaul Zimmerman 				wvalue);
3526197ba5f4SPaul Zimmerman 		}
3527197ba5f4SPaul Zimmerman 		break;
3528197ba5f4SPaul Zimmerman 
3529197ba5f4SPaul Zimmerman 	case GetHubDescriptor:
3530197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3531197ba5f4SPaul Zimmerman 		hub_desc = (struct usb_hub_descriptor *)buf;
3532197ba5f4SPaul Zimmerman 		hub_desc->bDescLength = 9;
3533a5dd0395SSergei Shtylyov 		hub_desc->bDescriptorType = USB_DT_HUB;
3534197ba5f4SPaul Zimmerman 		hub_desc->bNbrPorts = 1;
35353d040de8SSergei Shtylyov 		hub_desc->wHubCharacteristics =
35363d040de8SSergei Shtylyov 			cpu_to_le16(HUB_CHAR_COMMON_LPSM |
35373d040de8SSergei Shtylyov 				    HUB_CHAR_INDV_PORT_OCPM);
3538197ba5f4SPaul Zimmerman 		hub_desc->bPwrOn2PwrGood = 1;
3539197ba5f4SPaul Zimmerman 		hub_desc->bHubContrCurrent = 0;
3540197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[0] = 0;
3541197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3542197ba5f4SPaul Zimmerman 		break;
3543197ba5f4SPaul Zimmerman 
3544197ba5f4SPaul Zimmerman 	case GetHubStatus:
3545197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubStatus\n");
3546197ba5f4SPaul Zimmerman 		memset(buf, 0, 4);
3547197ba5f4SPaul Zimmerman 		break;
3548197ba5f4SPaul Zimmerman 
3549197ba5f4SPaul Zimmerman 	case GetPortStatus:
3550197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3551197ba5f4SPaul Zimmerman 			 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3552197ba5f4SPaul Zimmerman 			 hsotg->flags.d32);
3553197ba5f4SPaul Zimmerman 		if (!windex || windex > 1)
3554197ba5f4SPaul Zimmerman 			goto error;
3555197ba5f4SPaul Zimmerman 
3556197ba5f4SPaul Zimmerman 		port_status = 0;
3557197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status_change)
3558197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3559197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_enable_change)
3560197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_ENABLE << 16;
3561197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_suspend_change)
3562197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3563197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_l1_change)
3564197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_L1 << 16;
3565197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_reset_change)
3566197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_RESET << 16;
3567197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_over_current_change) {
3568197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "Overcurrent change detected\n");
3569197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3570197ba5f4SPaul Zimmerman 		}
3571197ba5f4SPaul Zimmerman 
3572197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3573197ba5f4SPaul Zimmerman 			/*
3574197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3575197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3576197ba5f4SPaul Zimmerman 			 * return 0's for the remainder of the port status
3577197ba5f4SPaul Zimmerman 			 * since the port register can't be read if the core
3578197ba5f4SPaul Zimmerman 			 * is in device mode.
3579197ba5f4SPaul Zimmerman 			 */
3580197ba5f4SPaul Zimmerman 			*(__le32 *)buf = cpu_to_le32(port_status);
3581197ba5f4SPaul Zimmerman 			break;
3582197ba5f4SPaul Zimmerman 		}
3583197ba5f4SPaul Zimmerman 
3584f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
3585197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  HPRT0: 0x%08x\n", hprt0);
3586197ba5f4SPaul Zimmerman 
3587197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_CONNSTS)
3588197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_CONNECTION;
3589197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_ENA)
3590197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_ENABLE;
3591197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_SUSP)
3592197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_SUSPEND;
3593197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_OVRCURRACT)
3594197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_OVERCURRENT;
3595197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_RST)
3596197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_RESET;
3597197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_PWR)
3598197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_POWER;
3599197ba5f4SPaul Zimmerman 
3600197ba5f4SPaul Zimmerman 		speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
3601197ba5f4SPaul Zimmerman 		if (speed == HPRT0_SPD_HIGH_SPEED)
3602197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_HIGH_SPEED;
3603197ba5f4SPaul Zimmerman 		else if (speed == HPRT0_SPD_LOW_SPEED)
3604197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_LOW_SPEED;
3605197ba5f4SPaul Zimmerman 
3606197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_TSTCTL_MASK)
3607197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_TEST;
3608197ba5f4SPaul Zimmerman 		/* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3609197ba5f4SPaul Zimmerman 
3610bea8e86cSJohn Youn 		if (hsotg->params.dma_desc_fs_enable) {
3611fbb9e22bSMian Yousaf Kaukab 			/*
3612fbb9e22bSMian Yousaf Kaukab 			 * Enable descriptor DMA only if a full speed
3613fbb9e22bSMian Yousaf Kaukab 			 * device is connected.
3614fbb9e22bSMian Yousaf Kaukab 			 */
3615fbb9e22bSMian Yousaf Kaukab 			if (hsotg->new_connection &&
3616fbb9e22bSMian Yousaf Kaukab 			    ((port_status &
3617fbb9e22bSMian Yousaf Kaukab 			      (USB_PORT_STAT_CONNECTION |
3618fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_HIGH_SPEED |
3619fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_LOW_SPEED)) ==
3620fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_CONNECTION)) {
3621fbb9e22bSMian Yousaf Kaukab 				u32 hcfg;
3622fbb9e22bSMian Yousaf Kaukab 
3623fbb9e22bSMian Yousaf Kaukab 				dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
362495832c00SJohn Youn 				hsotg->params.dma_desc_enable = true;
3625f25c42b8SGevorg Sahakyan 				hcfg = dwc2_readl(hsotg, HCFG);
3626fbb9e22bSMian Yousaf Kaukab 				hcfg |= HCFG_DESCDMA;
3627f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcfg, HCFG);
3628fbb9e22bSMian Yousaf Kaukab 				hsotg->new_connection = false;
3629fbb9e22bSMian Yousaf Kaukab 			}
3630fbb9e22bSMian Yousaf Kaukab 		}
3631fbb9e22bSMian Yousaf Kaukab 
3632197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
3633197ba5f4SPaul Zimmerman 		*(__le32 *)buf = cpu_to_le32(port_status);
3634197ba5f4SPaul Zimmerman 		break;
3635197ba5f4SPaul Zimmerman 
3636197ba5f4SPaul Zimmerman 	case SetHubFeature:
3637197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetHubFeature\n");
3638197ba5f4SPaul Zimmerman 		/* No HUB features supported */
3639197ba5f4SPaul Zimmerman 		break;
3640197ba5f4SPaul Zimmerman 
3641197ba5f4SPaul Zimmerman 	case SetPortFeature:
3642197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetPortFeature\n");
3643197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3644197ba5f4SPaul Zimmerman 			goto error;
3645197ba5f4SPaul Zimmerman 
3646197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3647197ba5f4SPaul Zimmerman 			/*
3648197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3649197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3650197ba5f4SPaul Zimmerman 			 * return without doing anything since the port
3651197ba5f4SPaul Zimmerman 			 * register can't be written if the core is in device
3652197ba5f4SPaul Zimmerman 			 * mode.
3653197ba5f4SPaul Zimmerman 			 */
3654197ba5f4SPaul Zimmerman 			break;
3655197ba5f4SPaul Zimmerman 		}
3656197ba5f4SPaul Zimmerman 
3657197ba5f4SPaul Zimmerman 		switch (wvalue) {
3658197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3659197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3660197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3661197ba5f4SPaul Zimmerman 			if (windex != hsotg->otg_port)
3662197ba5f4SPaul Zimmerman 				goto error;
36638f7f8689SArtur Petrosyan 			if (!hsotg->bus_suspended)
36648f7f8689SArtur Petrosyan 				retval = dwc2_port_suspend(hsotg, windex);
3665197ba5f4SPaul Zimmerman 			break;
3666197ba5f4SPaul Zimmerman 
3667197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3668197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3669197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_POWER\n");
3670197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3671cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3672197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_PWR;
3673f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3674cd7cd0e6SFabrice Gasnier 			if (!pwr)
3675cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_init(hsotg);
3676197ba5f4SPaul Zimmerman 			break;
3677197ba5f4SPaul Zimmerman 
3678197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_RESET:
3679c363af9cSArtur Petrosyan 			dev_dbg(hsotg->dev,
3680c363af9cSArtur Petrosyan 				"SetPortFeature - USB_PORT_FEAT_RESET\n");
3681c363af9cSArtur Petrosyan 
3682c363af9cSArtur Petrosyan 			hprt0 = dwc2_read_hprt0(hsotg);
3683c363af9cSArtur Petrosyan 
3684c363af9cSArtur Petrosyan 			if (hsotg->hibernated) {
3685c363af9cSArtur Petrosyan 				retval = dwc2_exit_hibernation(hsotg, 0, 1, 1);
3686c363af9cSArtur Petrosyan 				if (retval)
3687c363af9cSArtur Petrosyan 					dev_err(hsotg->dev,
3688c363af9cSArtur Petrosyan 						"exit hibernation failed\n");
3689c363af9cSArtur Petrosyan 			}
3690e97570f7SArtur Petrosyan 
3691e97570f7SArtur Petrosyan 			if (hsotg->in_ppd) {
3692e97570f7SArtur Petrosyan 				retval = dwc2_exit_partial_power_down(hsotg, 1,
3693e97570f7SArtur Petrosyan 								      true);
3694e97570f7SArtur Petrosyan 				if (retval)
3695e97570f7SArtur Petrosyan 					dev_err(hsotg->dev,
3696e97570f7SArtur Petrosyan 						"exit partial_power_down failed\n");
3697e97570f7SArtur Petrosyan 			}
3698e97570f7SArtur Petrosyan 
36995f9e60c0SArtur Petrosyan 			if (hsotg->params.power_down ==
37005f9e60c0SArtur Petrosyan 			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended)
37015f9e60c0SArtur Petrosyan 				dwc2_host_exit_clock_gating(hsotg, 0);
37025f9e60c0SArtur Petrosyan 
3703f25c42b8SGevorg Sahakyan 			pcgctl = dwc2_readl(hsotg, PCGCTL);
3704197ba5f4SPaul Zimmerman 			pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
3705f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, pcgctl, PCGCTL);
3706197ba5f4SPaul Zimmerman 			/* ??? Original driver does this */
3707f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, PCGCTL);
3708197ba5f4SPaul Zimmerman 
3709197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3710cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3711197ba5f4SPaul Zimmerman 			/* Clear suspend bit if resetting from suspend state */
3712197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_SUSP;
3713197ba5f4SPaul Zimmerman 
3714197ba5f4SPaul Zimmerman 			/*
3715197ba5f4SPaul Zimmerman 			 * When B-Host the Port reset bit is set in the Start
3716197ba5f4SPaul Zimmerman 			 * HCD Callback function, so that the reset is started
3717197ba5f4SPaul Zimmerman 			 * within 1ms of the HNP success interrupt
3718197ba5f4SPaul Zimmerman 			 */
3719197ba5f4SPaul Zimmerman 			if (!dwc2_hcd_is_b_host(hsotg)) {
3720197ba5f4SPaul Zimmerman 				hprt0 |= HPRT0_PWR | HPRT0_RST;
3721197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3722197ba5f4SPaul Zimmerman 					"In host mode, hprt0=%08x\n", hprt0);
3723f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hprt0, HPRT0);
3724cd7cd0e6SFabrice Gasnier 				if (!pwr)
3725cd7cd0e6SFabrice Gasnier 					dwc2_vbus_supply_init(hsotg);
3726197ba5f4SPaul Zimmerman 			}
3727197ba5f4SPaul Zimmerman 
3728197ba5f4SPaul Zimmerman 			/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
372904a9db79SNicholas Mc Guire 			msleep(50);
3730197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_RST;
3731f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3732197ba5f4SPaul Zimmerman 			hsotg->lx_state = DWC2_L0; /* Now back to On state */
3733197ba5f4SPaul Zimmerman 			break;
3734197ba5f4SPaul Zimmerman 
3735197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3736197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3737197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3738197ba5f4SPaul Zimmerman 			/* Not supported */
3739197ba5f4SPaul Zimmerman 			break;
3740197ba5f4SPaul Zimmerman 
374196d480e6SJingwu Lin 		case USB_PORT_FEAT_TEST:
374296d480e6SJingwu Lin 			hprt0 = dwc2_read_hprt0(hsotg);
374396d480e6SJingwu Lin 			dev_dbg(hsotg->dev,
374496d480e6SJingwu Lin 				"SetPortFeature - USB_PORT_FEAT_TEST\n");
374596d480e6SJingwu Lin 			hprt0 &= ~HPRT0_TSTCTL_MASK;
374696d480e6SJingwu Lin 			hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
3747f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
374896d480e6SJingwu Lin 			break;
374996d480e6SJingwu Lin 
3750197ba5f4SPaul Zimmerman 		default:
3751197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3752197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3753197ba5f4SPaul Zimmerman 				"SetPortFeature %1xh unknown or unsupported\n",
3754197ba5f4SPaul Zimmerman 				wvalue);
3755197ba5f4SPaul Zimmerman 			break;
3756197ba5f4SPaul Zimmerman 		}
3757197ba5f4SPaul Zimmerman 		break;
3758197ba5f4SPaul Zimmerman 
3759197ba5f4SPaul Zimmerman 	default:
3760197ba5f4SPaul Zimmerman error:
3761197ba5f4SPaul Zimmerman 		retval = -EINVAL;
3762197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3763197ba5f4SPaul Zimmerman 			"Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3764197ba5f4SPaul Zimmerman 			typereq, windex, wvalue);
3765197ba5f4SPaul Zimmerman 		break;
3766197ba5f4SPaul Zimmerman 	}
3767197ba5f4SPaul Zimmerman 
3768197ba5f4SPaul Zimmerman 	return retval;
3769197ba5f4SPaul Zimmerman }
3770197ba5f4SPaul Zimmerman 
3771197ba5f4SPaul Zimmerman static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3772197ba5f4SPaul Zimmerman {
3773197ba5f4SPaul Zimmerman 	int retval;
3774197ba5f4SPaul Zimmerman 
3775197ba5f4SPaul Zimmerman 	if (port != 1)
3776197ba5f4SPaul Zimmerman 		return -EINVAL;
3777197ba5f4SPaul Zimmerman 
3778197ba5f4SPaul Zimmerman 	retval = (hsotg->flags.b.port_connect_status_change ||
3779197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_reset_change ||
3780197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_enable_change ||
3781197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_suspend_change ||
3782197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_over_current_change);
3783197ba5f4SPaul Zimmerman 
3784197ba5f4SPaul Zimmerman 	if (retval) {
3785197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3786197ba5f4SPaul Zimmerman 			"DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3787197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_connect_status_change: %d\n",
3788197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change);
3789197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_reset_change: %d\n",
3790197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change);
3791197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_enable_change: %d\n",
3792197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change);
3793197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_suspend_change: %d\n",
3794197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change);
3795197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_over_current_change: %d\n",
3796197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change);
3797197ba5f4SPaul Zimmerman 	}
3798197ba5f4SPaul Zimmerman 
3799197ba5f4SPaul Zimmerman 	return retval;
3800197ba5f4SPaul Zimmerman }
3801197ba5f4SPaul Zimmerman 
3802197ba5f4SPaul Zimmerman int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3803197ba5f4SPaul Zimmerman {
3804f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3805197ba5f4SPaul Zimmerman 
3806197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3807197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
3808197ba5f4SPaul Zimmerman 		 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
3809197ba5f4SPaul Zimmerman #endif
3810197ba5f4SPaul Zimmerman 	return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3811197ba5f4SPaul Zimmerman }
3812197ba5f4SPaul Zimmerman 
3813fae4e826SDouglas Anderson int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3814fae4e826SDouglas Anderson {
3815f25c42b8SGevorg Sahakyan 	u32 hprt = dwc2_readl(hsotg, HPRT0);
3816f25c42b8SGevorg Sahakyan 	u32 hfir = dwc2_readl(hsotg, HFIR);
3817f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3818fae4e826SDouglas Anderson 	unsigned int us_per_frame;
3819fae4e826SDouglas Anderson 	unsigned int frame_number;
3820fae4e826SDouglas Anderson 	unsigned int remaining;
3821fae4e826SDouglas Anderson 	unsigned int interval;
3822fae4e826SDouglas Anderson 	unsigned int phy_clks;
3823fae4e826SDouglas Anderson 
3824fae4e826SDouglas Anderson 	/* High speed has 125 us per (micro) frame; others are 1 ms per */
3825fae4e826SDouglas Anderson 	us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3826fae4e826SDouglas Anderson 
3827fae4e826SDouglas Anderson 	/* Extract fields */
3828fae4e826SDouglas Anderson 	frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3829fae4e826SDouglas Anderson 	remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3830fae4e826SDouglas Anderson 	interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3831fae4e826SDouglas Anderson 
3832fae4e826SDouglas Anderson 	/*
3833fae4e826SDouglas Anderson 	 * Number of phy clocks since the last tick of the frame number after
3834fae4e826SDouglas Anderson 	 * "us" has passed.
3835fae4e826SDouglas Anderson 	 */
3836fae4e826SDouglas Anderson 	phy_clks = (interval - remaining) +
3837fae4e826SDouglas Anderson 		   DIV_ROUND_UP(interval * us, us_per_frame);
3838fae4e826SDouglas Anderson 
3839fae4e826SDouglas Anderson 	return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3840fae4e826SDouglas Anderson }
3841fae4e826SDouglas Anderson 
3842197ba5f4SPaul Zimmerman int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3843197ba5f4SPaul Zimmerman {
3844197ba5f4SPaul Zimmerman 	return hsotg->op_state == OTG_STATE_B_HOST;
3845197ba5f4SPaul Zimmerman }
3846197ba5f4SPaul Zimmerman 
3847197ba5f4SPaul Zimmerman static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3848197ba5f4SPaul Zimmerman 					       int iso_desc_count,
3849197ba5f4SPaul Zimmerman 					       gfp_t mem_flags)
3850197ba5f4SPaul Zimmerman {
3851197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3852197ba5f4SPaul Zimmerman 
3853eeca7606SGustavo A. R. Silva 	urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
3854197ba5f4SPaul Zimmerman 	if (urb)
3855197ba5f4SPaul Zimmerman 		urb->packet_count = iso_desc_count;
3856197ba5f4SPaul Zimmerman 	return urb;
3857197ba5f4SPaul Zimmerman }
3858197ba5f4SPaul Zimmerman 
3859197ba5f4SPaul Zimmerman static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3860197ba5f4SPaul Zimmerman 				      struct dwc2_hcd_urb *urb, u8 dev_addr,
3861babd1839SDouglas Anderson 				      u8 ep_num, u8 ep_type, u8 ep_dir,
3862babd1839SDouglas Anderson 				      u16 maxp, u16 maxp_mult)
3863197ba5f4SPaul Zimmerman {
3864197ba5f4SPaul Zimmerman 	if (dbg_perio() ||
3865197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_BULK ||
3866197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_CONTROL)
3867197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3868babd1839SDouglas Anderson 			 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
3869babd1839SDouglas Anderson 			 dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
3870197ba5f4SPaul Zimmerman 	urb->pipe_info.dev_addr = dev_addr;
3871197ba5f4SPaul Zimmerman 	urb->pipe_info.ep_num = ep_num;
3872197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_type = ep_type;
3873197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_dir = ep_dir;
3874babd1839SDouglas Anderson 	urb->pipe_info.maxp = maxp;
3875babd1839SDouglas Anderson 	urb->pipe_info.maxp_mult = maxp_mult;
3876197ba5f4SPaul Zimmerman }
3877197ba5f4SPaul Zimmerman 
3878197ba5f4SPaul Zimmerman /*
3879197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
3880197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
3881197ba5f4SPaul Zimmerman  */
3882197ba5f4SPaul Zimmerman void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3883197ba5f4SPaul Zimmerman {
3884197ba5f4SPaul Zimmerman #ifdef DEBUG
3885197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
3886197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3887197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
3888197ba5f4SPaul Zimmerman 	int num_channels;
3889197ba5f4SPaul Zimmerman 	u32 np_tx_status;
3890197ba5f4SPaul Zimmerman 	u32 p_tx_status;
3891197ba5f4SPaul Zimmerman 	int i;
3892197ba5f4SPaul Zimmerman 
3893bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
3894197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3895197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3896197ba5f4SPaul Zimmerman 		"************************************************************\n");
3897197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HCD State:\n");
3898197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Num channels: %d\n", num_channels);
3899197ba5f4SPaul Zimmerman 
3900197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
3901197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
3902197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  Channel %d:\n", i);
3903197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3904197ba5f4SPaul Zimmerman 			"    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3905197ba5f4SPaul Zimmerman 			chan->dev_addr, chan->ep_num, chan->ep_is_in);
3906197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    speed: %d\n", chan->speed);
3907197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
3908197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
3909197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    data_pid_start: %d\n",
3910197ba5f4SPaul Zimmerman 			chan->data_pid_start);
3911197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    multi_count: %d\n", chan->multi_count);
3912197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_started: %d\n",
3913197ba5f4SPaul Zimmerman 			chan->xfer_started);
3914197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
3915197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
3916197ba5f4SPaul Zimmerman 			(unsigned long)chan->xfer_dma);
3917197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
3918197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_count: %d\n", chan->xfer_count);
3919197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_on_queue: %d\n",
3920197ba5f4SPaul Zimmerman 			chan->halt_on_queue);
3921197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_pending: %d\n",
3922197ba5f4SPaul Zimmerman 			chan->halt_pending);
3923197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
3924197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    do_split: %d\n", chan->do_split);
3925197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    complete_split: %d\n",
3926197ba5f4SPaul Zimmerman 			chan->complete_split);
3927197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_addr: %d\n", chan->hub_addr);
3928197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_port: %d\n", chan->hub_port);
3929197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xact_pos: %d\n", chan->xact_pos);
3930197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    requests: %d\n", chan->requests);
3931197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
3932197ba5f4SPaul Zimmerman 
3933197ba5f4SPaul Zimmerman 		if (chan->xfer_started) {
3934197ba5f4SPaul Zimmerman 			u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3935197ba5f4SPaul Zimmerman 
3936f25c42b8SGevorg Sahakyan 			hfnum = dwc2_readl(hsotg, HFNUM);
3937f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
3938f25c42b8SGevorg Sahakyan 			hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
3939f25c42b8SGevorg Sahakyan 			hcint = dwc2_readl(hsotg, HCINT(i));
3940f25c42b8SGevorg Sahakyan 			hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
3941197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hfnum: 0x%08x\n", hfnum);
3942197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcchar: 0x%08x\n", hcchar);
3943197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hctsiz: 0x%08x\n", hctsiz);
3944197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcint: 0x%08x\n", hcint);
3945197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcintmsk: 0x%08x\n", hcintmsk);
3946197ba5f4SPaul Zimmerman 		}
3947197ba5f4SPaul Zimmerman 
3948197ba5f4SPaul Zimmerman 		if (!(chan->xfer_started && chan->qh))
3949197ba5f4SPaul Zimmerman 			continue;
3950197ba5f4SPaul Zimmerman 
3951197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3952197ba5f4SPaul Zimmerman 			if (!qtd->in_process)
3953197ba5f4SPaul Zimmerman 				break;
3954197ba5f4SPaul Zimmerman 			urb = qtd->urb;
3955197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    URB Info:\n");
3956197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "      qtd: %p, urb: %p\n",
3957197ba5f4SPaul Zimmerman 				qtd, urb);
3958197ba5f4SPaul Zimmerman 			if (urb) {
3959197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3960197ba5f4SPaul Zimmerman 					"      Dev: %d, EP: %d %s\n",
3961197ba5f4SPaul Zimmerman 					dwc2_hcd_get_dev_addr(&urb->pipe_info),
3962197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_num(&urb->pipe_info),
3963197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3964197ba5f4SPaul Zimmerman 					"IN" : "OUT");
3965197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3966babd1839SDouglas Anderson 					"      Max packet size: %d (%d mult)\n",
3967babd1839SDouglas Anderson 					dwc2_hcd_get_maxp(&urb->pipe_info),
3968babd1839SDouglas Anderson 					dwc2_hcd_get_maxp_mult(&urb->pipe_info));
3969197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3970197ba5f4SPaul Zimmerman 					"      transfer_buffer: %p\n",
3971197ba5f4SPaul Zimmerman 					urb->buf);
3972197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3973197ba5f4SPaul Zimmerman 					"      transfer_dma: %08lx\n",
3974197ba5f4SPaul Zimmerman 					(unsigned long)urb->dma);
3975197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3976197ba5f4SPaul Zimmerman 					"      transfer_buffer_length: %d\n",
3977197ba5f4SPaul Zimmerman 					urb->length);
3978197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev, "      actual_length: %d\n",
3979197ba5f4SPaul Zimmerman 					urb->actual_length);
3980197ba5f4SPaul Zimmerman 			}
3981197ba5f4SPaul Zimmerman 		}
3982197ba5f4SPaul Zimmerman 	}
3983197ba5f4SPaul Zimmerman 
3984197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  non_periodic_channels: %d\n",
3985197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels);
3986197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_channels: %d\n",
3987197ba5f4SPaul Zimmerman 		hsotg->periodic_channels);
3988197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_usecs: %d\n", hsotg->periodic_usecs);
3989f25c42b8SGevorg Sahakyan 	np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
3990197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx Req Queue Space Avail: %d\n",
3991197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3992197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx FIFO Space Avail: %d\n",
3993197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3994f25c42b8SGevorg Sahakyan 	p_tx_status = dwc2_readl(hsotg, HPTXSTS);
3995197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx Req Queue Space Avail: %d\n",
3996197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3997197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx FIFO Space Avail: %d\n",
3998197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3999197ba5f4SPaul Zimmerman 	dwc2_dump_global_registers(hsotg);
4000197ba5f4SPaul Zimmerman 	dwc2_dump_host_registers(hsotg);
4001197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4002197ba5f4SPaul Zimmerman 		"************************************************************\n");
4003197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
4004197ba5f4SPaul Zimmerman #endif
4005197ba5f4SPaul Zimmerman }
4006197ba5f4SPaul Zimmerman 
4007197ba5f4SPaul Zimmerman struct wrapper_priv_data {
4008197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg;
4009197ba5f4SPaul Zimmerman };
4010197ba5f4SPaul Zimmerman 
4011197ba5f4SPaul Zimmerman /* Gets the dwc2_hsotg from a usb_hcd */
4012197ba5f4SPaul Zimmerman static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4013197ba5f4SPaul Zimmerman {
4014197ba5f4SPaul Zimmerman 	struct wrapper_priv_data *p;
4015197ba5f4SPaul Zimmerman 
4016197ba5f4SPaul Zimmerman 	p = (struct wrapper_priv_data *)&hcd->hcd_priv;
4017197ba5f4SPaul Zimmerman 	return p->hsotg;
4018197ba5f4SPaul Zimmerman }
4019197ba5f4SPaul Zimmerman 
40209f9f09b0SDouglas Anderson /**
40219f9f09b0SDouglas Anderson  * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
40229f9f09b0SDouglas Anderson  *
40239f9f09b0SDouglas Anderson  * This will get the dwc2_tt structure (and ttport) associated with the given
40249f9f09b0SDouglas Anderson  * context (which is really just a struct urb pointer).
40259f9f09b0SDouglas Anderson  *
40269f9f09b0SDouglas Anderson  * The first time this is called for a given TT we allocate memory for our
40279f9f09b0SDouglas Anderson  * structure.  When everyone is done and has called dwc2_host_put_tt_info()
40289f9f09b0SDouglas Anderson  * then the refcount for the structure will go to 0 and we'll free it.
40299f9f09b0SDouglas Anderson  *
40309f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40319f9f09b0SDouglas Anderson  * @context:   The priv pointer from a struct dwc2_hcd_urb.
40329f9f09b0SDouglas Anderson  * @mem_flags: Flags for allocating memory.
40339f9f09b0SDouglas Anderson  * @ttport:    We'll return this device's port number here.  That's used to
40349f9f09b0SDouglas Anderson  *             reference into the bitmap if we're on a multi_tt hub.
40359f9f09b0SDouglas Anderson  *
40369f9f09b0SDouglas Anderson  * Return: a pointer to a struct dwc2_tt.  Don't forget to call
40379f9f09b0SDouglas Anderson  *         dwc2_host_put_tt_info()!  Returns NULL upon memory alloc failure.
40389f9f09b0SDouglas Anderson  */
40399f9f09b0SDouglas Anderson 
40409f9f09b0SDouglas Anderson struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
40419f9f09b0SDouglas Anderson 				      gfp_t mem_flags, int *ttport)
40429f9f09b0SDouglas Anderson {
40439f9f09b0SDouglas Anderson 	struct urb *urb = context;
40449f9f09b0SDouglas Anderson 	struct dwc2_tt *dwc_tt = NULL;
40459f9f09b0SDouglas Anderson 
40469f9f09b0SDouglas Anderson 	if (urb->dev->tt) {
40479f9f09b0SDouglas Anderson 		*ttport = urb->dev->ttport;
40489f9f09b0SDouglas Anderson 
40499f9f09b0SDouglas Anderson 		dwc_tt = urb->dev->tt->hcpriv;
40509da51974SJohn Youn 		if (!dwc_tt) {
40519f9f09b0SDouglas Anderson 			size_t bitmap_size;
40529f9f09b0SDouglas Anderson 
40539f9f09b0SDouglas Anderson 			/*
40549f9f09b0SDouglas Anderson 			 * For single_tt we need one schedule.  For multi_tt
40559f9f09b0SDouglas Anderson 			 * we need one per port.
40569f9f09b0SDouglas Anderson 			 */
40579f9f09b0SDouglas Anderson 			bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
40589f9f09b0SDouglas Anderson 				      sizeof(dwc_tt->periodic_bitmaps[0]);
40599f9f09b0SDouglas Anderson 			if (urb->dev->tt->multi)
40609f9f09b0SDouglas Anderson 				bitmap_size *= urb->dev->tt->hub->maxchild;
40619f9f09b0SDouglas Anderson 
40629f9f09b0SDouglas Anderson 			dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
40639f9f09b0SDouglas Anderson 					 mem_flags);
40649da51974SJohn Youn 			if (!dwc_tt)
40659f9f09b0SDouglas Anderson 				return NULL;
40669f9f09b0SDouglas Anderson 
40679f9f09b0SDouglas Anderson 			dwc_tt->usb_tt = urb->dev->tt;
40689f9f09b0SDouglas Anderson 			dwc_tt->usb_tt->hcpriv = dwc_tt;
40699f9f09b0SDouglas Anderson 		}
40709f9f09b0SDouglas Anderson 
40719f9f09b0SDouglas Anderson 		dwc_tt->refcount++;
40729f9f09b0SDouglas Anderson 	}
40739f9f09b0SDouglas Anderson 
40749f9f09b0SDouglas Anderson 	return dwc_tt;
40759f9f09b0SDouglas Anderson }
40769f9f09b0SDouglas Anderson 
40779f9f09b0SDouglas Anderson /**
40789f9f09b0SDouglas Anderson  * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
40799f9f09b0SDouglas Anderson  *
40809f9f09b0SDouglas Anderson  * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
40819f9f09b0SDouglas Anderson  * of the structure are done.
40829f9f09b0SDouglas Anderson  *
40839f9f09b0SDouglas Anderson  * It's OK to call this with NULL.
40849f9f09b0SDouglas Anderson  *
40859f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40869f9f09b0SDouglas Anderson  * @dwc_tt:    The pointer returned by dwc2_host_get_tt_info.
40879f9f09b0SDouglas Anderson  */
40889f9f09b0SDouglas Anderson void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
40899f9f09b0SDouglas Anderson {
40909f9f09b0SDouglas Anderson 	/* Model kfree and make put of NULL a no-op */
40919da51974SJohn Youn 	if (!dwc_tt)
40929f9f09b0SDouglas Anderson 		return;
40939f9f09b0SDouglas Anderson 
40949f9f09b0SDouglas Anderson 	WARN_ON(dwc_tt->refcount < 1);
40959f9f09b0SDouglas Anderson 
40969f9f09b0SDouglas Anderson 	dwc_tt->refcount--;
40979f9f09b0SDouglas Anderson 	if (!dwc_tt->refcount) {
40989f9f09b0SDouglas Anderson 		dwc_tt->usb_tt->hcpriv = NULL;
40999f9f09b0SDouglas Anderson 		kfree(dwc_tt);
41009f9f09b0SDouglas Anderson 	}
41019f9f09b0SDouglas Anderson }
41029f9f09b0SDouglas Anderson 
4103197ba5f4SPaul Zimmerman int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4104197ba5f4SPaul Zimmerman {
4105197ba5f4SPaul Zimmerman 	struct urb *urb = context;
4106197ba5f4SPaul Zimmerman 
4107197ba5f4SPaul Zimmerman 	return urb->dev->speed;
4108197ba5f4SPaul Zimmerman }
4109197ba5f4SPaul Zimmerman 
4110197ba5f4SPaul Zimmerman static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4111197ba5f4SPaul Zimmerman 					struct urb *urb)
4112197ba5f4SPaul Zimmerman {
4113197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4114197ba5f4SPaul Zimmerman 
4115197ba5f4SPaul Zimmerman 	if (urb->interval)
4116197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated += bw / urb->interval;
4117197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4118197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs++;
4119197ba5f4SPaul Zimmerman 	else
4120197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs++;
4121197ba5f4SPaul Zimmerman }
4122197ba5f4SPaul Zimmerman 
4123197ba5f4SPaul Zimmerman static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4124197ba5f4SPaul Zimmerman 				    struct urb *urb)
4125197ba5f4SPaul Zimmerman {
4126197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4127197ba5f4SPaul Zimmerman 
4128197ba5f4SPaul Zimmerman 	if (urb->interval)
4129197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated -= bw / urb->interval;
4130197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4131197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs--;
4132197ba5f4SPaul Zimmerman 	else
4133197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs--;
4134197ba5f4SPaul Zimmerman }
4135197ba5f4SPaul Zimmerman 
4136197ba5f4SPaul Zimmerman /*
4137197ba5f4SPaul Zimmerman  * Sets the final status of an URB and returns it to the upper layer. Any
4138197ba5f4SPaul Zimmerman  * required cleanup of the URB is performed.
4139197ba5f4SPaul Zimmerman  *
4140197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
4141197ba5f4SPaul Zimmerman  */
4142197ba5f4SPaul Zimmerman void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4143197ba5f4SPaul Zimmerman 			int status)
4144197ba5f4SPaul Zimmerman {
4145197ba5f4SPaul Zimmerman 	struct urb *urb;
4146197ba5f4SPaul Zimmerman 	int i;
4147197ba5f4SPaul Zimmerman 
4148197ba5f4SPaul Zimmerman 	if (!qtd) {
4149197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4150197ba5f4SPaul Zimmerman 		return;
4151197ba5f4SPaul Zimmerman 	}
4152197ba5f4SPaul Zimmerman 
4153197ba5f4SPaul Zimmerman 	if (!qtd->urb) {
4154197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4155197ba5f4SPaul Zimmerman 		return;
4156197ba5f4SPaul Zimmerman 	}
4157197ba5f4SPaul Zimmerman 
4158197ba5f4SPaul Zimmerman 	urb = qtd->urb->priv;
4159197ba5f4SPaul Zimmerman 	if (!urb) {
4160197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4161197ba5f4SPaul Zimmerman 		return;
4162197ba5f4SPaul Zimmerman 	}
4163197ba5f4SPaul Zimmerman 
4164197ba5f4SPaul Zimmerman 	urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
4165197ba5f4SPaul Zimmerman 
4166197ba5f4SPaul Zimmerman 	if (dbg_urb(urb))
4167197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
4168197ba5f4SPaul Zimmerman 			 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4169197ba5f4SPaul Zimmerman 			 __func__, urb, usb_pipedevice(urb->pipe),
4170197ba5f4SPaul Zimmerman 			 usb_pipeendpoint(urb->pipe),
4171197ba5f4SPaul Zimmerman 			 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4172197ba5f4SPaul Zimmerman 			 urb->actual_length);
4173197ba5f4SPaul Zimmerman 
4174197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4175197ba5f4SPaul Zimmerman 		urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
4176197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; ++i) {
4177197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].actual_length =
4178197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_actual_length(
4179197ba5f4SPaul Zimmerman 						qtd->urb, i);
4180197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].status =
4181197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
4182197ba5f4SPaul Zimmerman 		}
4183197ba5f4SPaul Zimmerman 	}
4184197ba5f4SPaul Zimmerman 
4185fe9b1773SGregory Herrero 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4186fe9b1773SGregory Herrero 		for (i = 0; i < urb->number_of_packets; i++)
4187fe9b1773SGregory Herrero 			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4188fe9b1773SGregory Herrero 				 i, urb->iso_frame_desc[i].status);
4189fe9b1773SGregory Herrero 	}
4190fe9b1773SGregory Herrero 
4191197ba5f4SPaul Zimmerman 	urb->status = status;
4192197ba5f4SPaul Zimmerman 	if (!status) {
4193197ba5f4SPaul Zimmerman 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4194197ba5f4SPaul Zimmerman 		    urb->actual_length < urb->transfer_buffer_length)
4195197ba5f4SPaul Zimmerman 			urb->status = -EREMOTEIO;
4196197ba5f4SPaul Zimmerman 	}
4197197ba5f4SPaul Zimmerman 
4198197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4199197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4200197ba5f4SPaul Zimmerman 		struct usb_host_endpoint *ep = urb->ep;
4201197ba5f4SPaul Zimmerman 
4202197ba5f4SPaul Zimmerman 		if (ep)
4203197ba5f4SPaul Zimmerman 			dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4204197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4205197ba5f4SPaul Zimmerman 					urb);
4206197ba5f4SPaul Zimmerman 	}
4207197ba5f4SPaul Zimmerman 
4208197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
4209197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4210197ba5f4SPaul Zimmerman 	kfree(qtd->urb);
4211197ba5f4SPaul Zimmerman 	qtd->urb = NULL;
4212197ba5f4SPaul Zimmerman 
4213197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
4214197ba5f4SPaul Zimmerman }
4215197ba5f4SPaul Zimmerman 
4216197ba5f4SPaul Zimmerman /*
4217197ba5f4SPaul Zimmerman  * Work queue function for starting the HCD when A-Cable is connected
4218197ba5f4SPaul Zimmerman  */
4219197ba5f4SPaul Zimmerman static void dwc2_hcd_start_func(struct work_struct *work)
4220197ba5f4SPaul Zimmerman {
4221197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4222197ba5f4SPaul Zimmerman 						start_work.work);
4223197ba5f4SPaul Zimmerman 
4224197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4225197ba5f4SPaul Zimmerman 	dwc2_host_start(hsotg);
4226197ba5f4SPaul Zimmerman }
4227197ba5f4SPaul Zimmerman 
4228197ba5f4SPaul Zimmerman /*
4229197ba5f4SPaul Zimmerman  * Reset work queue function
4230197ba5f4SPaul Zimmerman  */
4231197ba5f4SPaul Zimmerman static void dwc2_hcd_reset_func(struct work_struct *work)
4232197ba5f4SPaul Zimmerman {
4233197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4234197ba5f4SPaul Zimmerman 						reset_work.work);
42354a065c7bSDouglas Anderson 	unsigned long flags;
4236197ba5f4SPaul Zimmerman 	u32 hprt0;
4237197ba5f4SPaul Zimmerman 
4238197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "USB RESET function called\n");
42394a065c7bSDouglas Anderson 
42404a065c7bSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
42414a065c7bSDouglas Anderson 
4242197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
4243197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RST;
4244f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
4245197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_reset_change = 1;
42464a065c7bSDouglas Anderson 
42474a065c7bSDouglas Anderson 	spin_unlock_irqrestore(&hsotg->lock, flags);
4248197ba5f4SPaul Zimmerman }
4249197ba5f4SPaul Zimmerman 
4250c40cf770SDouglas Anderson static void dwc2_hcd_phy_reset_func(struct work_struct *work)
4251c40cf770SDouglas Anderson {
4252c40cf770SDouglas Anderson 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4253c40cf770SDouglas Anderson 						phy_reset_work);
4254c40cf770SDouglas Anderson 	int ret;
4255c40cf770SDouglas Anderson 
4256c40cf770SDouglas Anderson 	ret = phy_reset(hsotg->phy);
4257c40cf770SDouglas Anderson 	if (ret)
4258c40cf770SDouglas Anderson 		dev_warn(hsotg->dev, "PHY reset failed\n");
4259c40cf770SDouglas Anderson }
4260c40cf770SDouglas Anderson 
4261197ba5f4SPaul Zimmerman /*
4262197ba5f4SPaul Zimmerman  * =========================================================================
4263197ba5f4SPaul Zimmerman  *  Linux HC Driver Functions
4264197ba5f4SPaul Zimmerman  * =========================================================================
4265197ba5f4SPaul Zimmerman  */
4266197ba5f4SPaul Zimmerman 
4267197ba5f4SPaul Zimmerman /*
4268197ba5f4SPaul Zimmerman  * Initializes the DWC_otg controller and its root hub and prepares it for host
4269197ba5f4SPaul Zimmerman  * mode operation. Activates the root port. Returns 0 on success and a negative
4270197ba5f4SPaul Zimmerman  * error code on failure.
4271197ba5f4SPaul Zimmerman  */
4272197ba5f4SPaul Zimmerman static int _dwc2_hcd_start(struct usb_hcd *hcd)
4273197ba5f4SPaul Zimmerman {
4274197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4275197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4276197ba5f4SPaul Zimmerman 	unsigned long flags;
4277cd7cd0e6SFabrice Gasnier 	u32 hprt0;
427841ee1ea2SFabrice Gasnier 	int ret;
4279197ba5f4SPaul Zimmerman 
4280197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4281197ba5f4SPaul Zimmerman 
4282197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
428331927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L0;
4284197ba5f4SPaul Zimmerman 	hcd->state = HC_STATE_RUNNING;
428531927b6bSGregory Herrero 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4286197ba5f4SPaul Zimmerman 
4287197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
4288197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4289197ba5f4SPaul Zimmerman 		return 0;	/* why 0 ?? */
4290197ba5f4SPaul Zimmerman 	}
4291197ba5f4SPaul Zimmerman 
4292197ba5f4SPaul Zimmerman 	dwc2_hcd_reinit(hsotg);
4293197ba5f4SPaul Zimmerman 
4294cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4295cd7cd0e6SFabrice Gasnier 	/* Has vbus power been turned on in dwc2_core_host_init ? */
4296cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR) {
4297cd7cd0e6SFabrice Gasnier 		/* Enable external vbus supply before resuming root hub */
429841ee1ea2SFabrice Gasnier 		spin_unlock_irqrestore(&hsotg->lock, flags);
429941ee1ea2SFabrice Gasnier 		ret = dwc2_vbus_supply_init(hsotg);
430041ee1ea2SFabrice Gasnier 		if (ret)
430141ee1ea2SFabrice Gasnier 			return ret;
430241ee1ea2SFabrice Gasnier 		spin_lock_irqsave(&hsotg->lock, flags);
4303cd7cd0e6SFabrice Gasnier 	}
430441ee1ea2SFabrice Gasnier 
4305197ba5f4SPaul Zimmerman 	/* Initialize and connect root hub if one is not already attached */
4306197ba5f4SPaul Zimmerman 	if (bus->root_hub) {
4307197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4308197ba5f4SPaul Zimmerman 		/* Inform the HUB driver to resume */
4309197ba5f4SPaul Zimmerman 		usb_hcd_resume_root_hub(hcd);
4310197ba5f4SPaul Zimmerman 	}
4311197ba5f4SPaul Zimmerman 
4312197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4313531ef5ebSAmelie Delaunay 
431441ee1ea2SFabrice Gasnier 	return 0;
4315197ba5f4SPaul Zimmerman }
4316197ba5f4SPaul Zimmerman 
4317197ba5f4SPaul Zimmerman /*
4318197ba5f4SPaul Zimmerman  * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4319197ba5f4SPaul Zimmerman  * stopped.
4320197ba5f4SPaul Zimmerman  */
4321197ba5f4SPaul Zimmerman static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4322197ba5f4SPaul Zimmerman {
4323197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4324197ba5f4SPaul Zimmerman 	unsigned long flags;
4325cd7cd0e6SFabrice Gasnier 	u32 hprt0;
4326197ba5f4SPaul Zimmerman 
43275bbf6ce0SGregory Herrero 	/* Turn off all host-specific interrupts */
43285bbf6ce0SGregory Herrero 	dwc2_disable_host_interrupts(hsotg);
43295bbf6ce0SGregory Herrero 
4330091473adSGregory Herrero 	/* Wait for interrupt processing to finish */
4331091473adSGregory Herrero 	synchronize_irq(hcd->irq);
4332091473adSGregory Herrero 
4333197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4334cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4335091473adSGregory Herrero 	/* Ensure hcd is disconnected */
43366a659531SDouglas Anderson 	dwc2_hcd_disconnect(hsotg, true);
4337197ba5f4SPaul Zimmerman 	dwc2_hcd_stop(hsotg);
433831927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L3;
433931927b6bSGregory Herrero 	hcd->state = HC_STATE_HALT;
434031927b6bSGregory Herrero 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4341197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4342197ba5f4SPaul Zimmerman 
4343cd7cd0e6SFabrice Gasnier 	/* keep balanced supply init/exit by checking HPRT0_PWR */
4344cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR)
4345531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
4346531ef5ebSAmelie Delaunay 
4347197ba5f4SPaul Zimmerman 	usleep_range(1000, 3000);
4348197ba5f4SPaul Zimmerman }
4349197ba5f4SPaul Zimmerman 
435099a65798SGregory Herrero static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
435199a65798SGregory Herrero {
435299a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4353a2a23d3fSGregory Herrero 	unsigned long flags;
4354a2a23d3fSGregory Herrero 	int ret = 0;
435599a65798SGregory Herrero 
4356a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4357a2a23d3fSGregory Herrero 
4358f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4359f367b72cSMeng Dongyang 		goto unlock;
4360f367b72cSMeng Dongyang 
4361a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L0)
4362a2a23d3fSGregory Herrero 		goto unlock;
4363a2a23d3fSGregory Herrero 
4364a2a23d3fSGregory Herrero 	if (!HCD_HW_ACCESSIBLE(hcd))
4365a2a23d3fSGregory Herrero 		goto unlock;
4366a2a23d3fSGregory Herrero 
4367866932e2SJohn Stultz 	if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4368866932e2SJohn Stultz 		goto unlock;
4369866932e2SJohn Stultz 
4370113f86d0SArtur Petrosyan 	if (hsotg->bus_suspended)
4371a2a23d3fSGregory Herrero 		goto skip_power_saving;
4372a2a23d3fSGregory Herrero 
4373113f86d0SArtur Petrosyan 	if (hsotg->flags.b.port_connect_status == 0)
4374113f86d0SArtur Petrosyan 		goto skip_power_saving;
4375113f86d0SArtur Petrosyan 
4376113f86d0SArtur Petrosyan 	switch (hsotg->params.power_down) {
4377113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4378113f86d0SArtur Petrosyan 		/* Enter partial_power_down */
4379113f86d0SArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
4380113f86d0SArtur Petrosyan 		if (ret)
4381113f86d0SArtur Petrosyan 			dev_err(hsotg->dev,
4382113f86d0SArtur Petrosyan 				"enter partial_power_down failed\n");
4383113f86d0SArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
4384113f86d0SArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4385113f86d0SArtur Petrosyan 		break;
4386113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4387755d0effSArtur Petrosyan 		/* Enter hibernation */
4388755d0effSArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
4389755d0effSArtur Petrosyan 		ret = dwc2_enter_hibernation(hsotg, 1);
4390755d0effSArtur Petrosyan 		if (ret)
4391755d0effSArtur Petrosyan 			dev_err(hsotg->dev, "enter hibernation failed\n");
4392755d0effSArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
4393755d0effSArtur Petrosyan 
4394755d0effSArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
4395755d0effSArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4396755d0effSArtur Petrosyan 		break;
4397113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
439850fb0c12SArtur Petrosyan 		/*
439950fb0c12SArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
440050fb0c12SArtur Petrosyan 		 * clock gating is used to save power.
440150fb0c12SArtur Petrosyan 		 */
4402*34146c68SDinh Nguyen 		if (!hsotg->params.no_clock_gating) {
440350fb0c12SArtur Petrosyan 			dwc2_host_enter_clock_gating(hsotg);
440450fb0c12SArtur Petrosyan 
440550fb0c12SArtur Petrosyan 			/* After entering suspend, hardware is not accessible */
440650fb0c12SArtur Petrosyan 			clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4407*34146c68SDinh Nguyen 		}
440850fb0c12SArtur Petrosyan 		break;
4409113f86d0SArtur Petrosyan 	default:
4410113f86d0SArtur Petrosyan 		goto skip_power_saving;
44116f6d7059SDouglas Anderson 	}
4412113f86d0SArtur Petrosyan 
44135aa678c7SFabrice Gasnier 	spin_unlock_irqrestore(&hsotg->lock, flags);
4414531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_exit(hsotg);
44155aa678c7SFabrice Gasnier 	spin_lock_irqsave(&hsotg->lock, flags);
44166f6d7059SDouglas Anderson 
4417a2a23d3fSGregory Herrero 	/* Ask phy to be suspended */
4418a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4419a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4420a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, true);
4421a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4422a2a23d3fSGregory Herrero 	}
4423a2a23d3fSGregory Herrero 
4424a2a23d3fSGregory Herrero skip_power_saving:
442599a65798SGregory Herrero 	hsotg->lx_state = DWC2_L2;
4426a2a23d3fSGregory Herrero unlock:
4427a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4428a2a23d3fSGregory Herrero 
4429a2a23d3fSGregory Herrero 	return ret;
443099a65798SGregory Herrero }
443199a65798SGregory Herrero 
443299a65798SGregory Herrero static int _dwc2_hcd_resume(struct usb_hcd *hcd)
443399a65798SGregory Herrero {
443499a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4435a2a23d3fSGregory Herrero 	unsigned long flags;
4436c74c26f6SArtur Petrosyan 	u32 hprt0;
4437a2a23d3fSGregory Herrero 	int ret = 0;
4438a2a23d3fSGregory Herrero 
4439a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4440a2a23d3fSGregory Herrero 
4441f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4442f367b72cSMeng Dongyang 		goto unlock;
4443f367b72cSMeng Dongyang 
4444a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L2)
4445a2a23d3fSGregory Herrero 		goto unlock;
4446a2a23d3fSGregory Herrero 
4447c74c26f6SArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
4448c74c26f6SArtur Petrosyan 
4449c74c26f6SArtur Petrosyan 	/*
4450c74c26f6SArtur Petrosyan 	 * Added port connection status checking which prevents exiting from
4451c74c26f6SArtur Petrosyan 	 * Partial Power Down mode from _dwc2_hcd_resume() if not in Partial
4452c74c26f6SArtur Petrosyan 	 * Power Down mode.
4453c74c26f6SArtur Petrosyan 	 */
4454c74c26f6SArtur Petrosyan 	if (hprt0 & HPRT0_CONNSTS) {
4455a2a23d3fSGregory Herrero 		hsotg->lx_state = DWC2_L0;
4456a2a23d3fSGregory Herrero 		goto unlock;
4457a2a23d3fSGregory Herrero 	}
4458a2a23d3fSGregory Herrero 
4459c74c26f6SArtur Petrosyan 	switch (hsotg->params.power_down) {
4460c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4461c74c26f6SArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
4462c74c26f6SArtur Petrosyan 		if (ret)
4463c74c26f6SArtur Petrosyan 			dev_err(hsotg->dev,
4464c74c26f6SArtur Petrosyan 				"exit partial_power_down failed\n");
4465c74c26f6SArtur Petrosyan 		/*
4466c74c26f6SArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4467c74c26f6SArtur Petrosyan 		 * since an interrupt may rise.
4468c74c26f6SArtur Petrosyan 		 */
4469c74c26f6SArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4470c74c26f6SArtur Petrosyan 		break;
4471c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4472ae0da4fdSArtur Petrosyan 		ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
4473ae0da4fdSArtur Petrosyan 		if (ret)
4474ae0da4fdSArtur Petrosyan 			dev_err(hsotg->dev, "exit hibernation failed.\n");
4475ae0da4fdSArtur Petrosyan 
4476ae0da4fdSArtur Petrosyan 		/*
4477ae0da4fdSArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4478ae0da4fdSArtur Petrosyan 		 * since an interrupt may rise.
4479ae0da4fdSArtur Petrosyan 		 */
4480ae0da4fdSArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4481ae0da4fdSArtur Petrosyan 		break;
4482c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
4483ef5e0eecSArtur Petrosyan 		/*
4484ef5e0eecSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
4485ef5e0eecSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
4486ef5e0eecSArtur Petrosyan 		 */
4487ef5e0eecSArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
4488ef5e0eecSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
4489ef5e0eecSArtur Petrosyan 
4490ef5e0eecSArtur Petrosyan 		/*
4491ef5e0eecSArtur Petrosyan 		 * Initialize the Core for Host mode, as after system resume
4492ef5e0eecSArtur Petrosyan 		 * the global interrupts are disabled.
4493ef5e0eecSArtur Petrosyan 		 */
4494ef5e0eecSArtur Petrosyan 		dwc2_core_init(hsotg, false);
4495ef5e0eecSArtur Petrosyan 		dwc2_enable_global_interrupts(hsotg);
4496ef5e0eecSArtur Petrosyan 		dwc2_hcd_reinit(hsotg);
4497ef5e0eecSArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
4498ef5e0eecSArtur Petrosyan 
4499ef5e0eecSArtur Petrosyan 		/*
4500ef5e0eecSArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4501ef5e0eecSArtur Petrosyan 		 * since an interrupt may rise.
4502ef5e0eecSArtur Petrosyan 		 */
4503ef5e0eecSArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4504ef5e0eecSArtur Petrosyan 		break;
4505c74c26f6SArtur Petrosyan 	default:
4506c74c26f6SArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
4507c74c26f6SArtur Petrosyan 		goto unlock;
4508c74c26f6SArtur Petrosyan 	}
4509c74c26f6SArtur Petrosyan 
4510c74c26f6SArtur Petrosyan 	/* Change Root port status, as port status change occurred after resume.*/
4511c74c26f6SArtur Petrosyan 	hsotg->flags.b.port_suspend_change = 1;
4512c74c26f6SArtur Petrosyan 
4513a2a23d3fSGregory Herrero 	/*
4514a2a23d3fSGregory Herrero 	 * Enable power if not already done.
4515a2a23d3fSGregory Herrero 	 * This must not be spinlocked since duration
4516a2a23d3fSGregory Herrero 	 * of this call is unknown.
4517a2a23d3fSGregory Herrero 	 */
4518a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4519a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4520a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, false);
4521a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4522a2a23d3fSGregory Herrero 	}
4523a2a23d3fSGregory Herrero 
4524c74c26f6SArtur Petrosyan 	/* Enable external vbus supply after resuming the port. */
4525a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4526531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_init(hsotg);
4527531ef5ebSAmelie Delaunay 
45285634e016SGregory Herrero 	/* Wait for controller to correctly update D+/D- level */
45295634e016SGregory Herrero 	usleep_range(3000, 5000);
4530c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
45315634e016SGregory Herrero 
4532a2a23d3fSGregory Herrero 	/*
4533a2a23d3fSGregory Herrero 	 * Clear Port Enable and Port Status changes.
4534a2a23d3fSGregory Herrero 	 * Enable Port Power.
4535a2a23d3fSGregory Herrero 	 */
4536f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
4537f25c42b8SGevorg Sahakyan 			HPRT0_ENACHG, HPRT0);
4538a2a23d3fSGregory Herrero 
4539c74c26f6SArtur Petrosyan 	/* Wait for controller to detect Port Connect */
4540c74c26f6SArtur Petrosyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
4541c74c26f6SArtur Petrosyan 	usleep_range(5000, 7000);
4542c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
4543a2a23d3fSGregory Herrero unlock:
4544a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4545a2a23d3fSGregory Herrero 
4546a2a23d3fSGregory Herrero 	return ret;
454799a65798SGregory Herrero }
454899a65798SGregory Herrero 
4549197ba5f4SPaul Zimmerman /* Returns the current frame number */
4550197ba5f4SPaul Zimmerman static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4551197ba5f4SPaul Zimmerman {
4552197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4553197ba5f4SPaul Zimmerman 
4554197ba5f4SPaul Zimmerman 	return dwc2_hcd_get_frame_number(hsotg);
4555197ba5f4SPaul Zimmerman }
4556197ba5f4SPaul Zimmerman 
4557197ba5f4SPaul Zimmerman static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4558197ba5f4SPaul Zimmerman 			       char *fn_name)
4559197ba5f4SPaul Zimmerman {
4560197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
4561197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4562efe357f4SNicholas Mc Guire 	char *pipetype = NULL;
4563efe357f4SNicholas Mc Guire 	char *speed = NULL;
4564197ba5f4SPaul Zimmerman 
4565197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4566197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Device address: %d\n",
4567197ba5f4SPaul Zimmerman 		 usb_pipedevice(urb->pipe));
4568197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint: %d, %s\n",
4569197ba5f4SPaul Zimmerman 		 usb_pipeendpoint(urb->pipe),
4570197ba5f4SPaul Zimmerman 		 usb_pipein(urb->pipe) ? "IN" : "OUT");
4571197ba5f4SPaul Zimmerman 
4572197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4573197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4574197ba5f4SPaul Zimmerman 		pipetype = "CONTROL";
4575197ba5f4SPaul Zimmerman 		break;
4576197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4577197ba5f4SPaul Zimmerman 		pipetype = "BULK";
4578197ba5f4SPaul Zimmerman 		break;
4579197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4580197ba5f4SPaul Zimmerman 		pipetype = "INTERRUPT";
4581197ba5f4SPaul Zimmerman 		break;
4582197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4583197ba5f4SPaul Zimmerman 		pipetype = "ISOCHRONOUS";
4584197ba5f4SPaul Zimmerman 		break;
4585197ba5f4SPaul Zimmerman 	}
4586197ba5f4SPaul Zimmerman 
4587197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint type: %s %s (%s)\n", pipetype,
4588197ba5f4SPaul Zimmerman 		 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4589197ba5f4SPaul Zimmerman 		 "IN" : "OUT");
4590197ba5f4SPaul Zimmerman 
4591197ba5f4SPaul Zimmerman 	switch (urb->dev->speed) {
4592197ba5f4SPaul Zimmerman 	case USB_SPEED_HIGH:
4593197ba5f4SPaul Zimmerman 		speed = "HIGH";
4594197ba5f4SPaul Zimmerman 		break;
4595197ba5f4SPaul Zimmerman 	case USB_SPEED_FULL:
4596197ba5f4SPaul Zimmerman 		speed = "FULL";
4597197ba5f4SPaul Zimmerman 		break;
4598197ba5f4SPaul Zimmerman 	case USB_SPEED_LOW:
4599197ba5f4SPaul Zimmerman 		speed = "LOW";
4600197ba5f4SPaul Zimmerman 		break;
4601197ba5f4SPaul Zimmerman 	default:
4602197ba5f4SPaul Zimmerman 		speed = "UNKNOWN";
4603197ba5f4SPaul Zimmerman 		break;
4604197ba5f4SPaul Zimmerman 	}
4605197ba5f4SPaul Zimmerman 
4606197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Speed: %s\n", speed);
4607babd1839SDouglas Anderson 	dev_vdbg(hsotg->dev, "  Max packet size: %d (%d mult)\n",
4608babd1839SDouglas Anderson 		 usb_endpoint_maxp(&urb->ep->desc),
4609babd1839SDouglas Anderson 		 usb_endpoint_maxp_mult(&urb->ep->desc));
4610babd1839SDouglas Anderson 
4611197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Data buffer length: %d\n",
4612197ba5f4SPaul Zimmerman 		 urb->transfer_buffer_length);
4613197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
4614197ba5f4SPaul Zimmerman 		 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4615197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
4616197ba5f4SPaul Zimmerman 		 urb->setup_packet, (unsigned long)urb->setup_dma);
4617197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Interval: %d\n", urb->interval);
4618197ba5f4SPaul Zimmerman 
4619197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4620197ba5f4SPaul Zimmerman 		int i;
4621197ba5f4SPaul Zimmerman 
4622197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; i++) {
4623197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  ISO Desc %d:\n", i);
4624197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "    offset: %d, length %d\n",
4625197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].offset,
4626197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].length);
4627197ba5f4SPaul Zimmerman 		}
4628197ba5f4SPaul Zimmerman 	}
4629197ba5f4SPaul Zimmerman #endif
4630197ba5f4SPaul Zimmerman }
4631197ba5f4SPaul Zimmerman 
4632197ba5f4SPaul Zimmerman /*
4633197ba5f4SPaul Zimmerman  * Starts processing a USB transfer request specified by a USB Request Block
4634197ba5f4SPaul Zimmerman  * (URB). mem_flags indicates the type of memory allocation to use while
4635197ba5f4SPaul Zimmerman  * processing this URB.
4636197ba5f4SPaul Zimmerman  */
4637197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4638197ba5f4SPaul Zimmerman 				 gfp_t mem_flags)
4639197ba5f4SPaul Zimmerman {
4640197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4641197ba5f4SPaul Zimmerman 	struct usb_host_endpoint *ep = urb->ep;
4642197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *dwc2_urb;
4643197ba5f4SPaul Zimmerman 	int i;
4644197ba5f4SPaul Zimmerman 	int retval;
4645197ba5f4SPaul Zimmerman 	int alloc_bandwidth = 0;
4646197ba5f4SPaul Zimmerman 	u8 ep_type = 0;
4647197ba5f4SPaul Zimmerman 	u32 tflags = 0;
4648197ba5f4SPaul Zimmerman 	void *buf;
4649197ba5f4SPaul Zimmerman 	unsigned long flags;
4650b58e6ceeSMian Yousaf Kaukab 	struct dwc2_qh *qh;
4651b58e6ceeSMian Yousaf Kaukab 	bool qh_allocated = false;
4652b5a468a6SMian Yousaf Kaukab 	struct dwc2_qtd *qtd;
4653c3595df7SArtur Petrosyan 	struct dwc2_gregs_backup *gr;
4654c3595df7SArtur Petrosyan 
4655c3595df7SArtur Petrosyan 	gr = &hsotg->gr_backup;
4656197ba5f4SPaul Zimmerman 
4657197ba5f4SPaul Zimmerman 	if (dbg_urb(urb)) {
4658197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4659197ba5f4SPaul Zimmerman 		dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4660197ba5f4SPaul Zimmerman 	}
4661197ba5f4SPaul Zimmerman 
4662c3595df7SArtur Petrosyan 	if (hsotg->hibernated) {
4663c3595df7SArtur Petrosyan 		if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
4664c3595df7SArtur Petrosyan 			retval = dwc2_exit_hibernation(hsotg, 0, 0, 1);
4665c3595df7SArtur Petrosyan 		else
4666c3595df7SArtur Petrosyan 			retval = dwc2_exit_hibernation(hsotg, 0, 0, 0);
4667c3595df7SArtur Petrosyan 
4668c3595df7SArtur Petrosyan 		if (retval)
4669c3595df7SArtur Petrosyan 			dev_err(hsotg->dev,
4670c3595df7SArtur Petrosyan 				"exit hibernation failed.\n");
4671c3595df7SArtur Petrosyan 	}
4672c3595df7SArtur Petrosyan 
467375f43ac3SArtur Petrosyan 	if (hsotg->in_ppd) {
467475f43ac3SArtur Petrosyan 		retval = dwc2_exit_partial_power_down(hsotg, 0, true);
467575f43ac3SArtur Petrosyan 		if (retval)
467675f43ac3SArtur Petrosyan 			dev_err(hsotg->dev,
467775f43ac3SArtur Petrosyan 				"exit partial_power_down failed\n");
467875f43ac3SArtur Petrosyan 	}
467975f43ac3SArtur Petrosyan 
468016c729f9SArtur Petrosyan 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
468116c729f9SArtur Petrosyan 	    hsotg->bus_suspended) {
468216c729f9SArtur Petrosyan 		if (dwc2_is_device_mode(hsotg))
468316c729f9SArtur Petrosyan 			dwc2_gadget_exit_clock_gating(hsotg, 0);
468416c729f9SArtur Petrosyan 		else
468516c729f9SArtur Petrosyan 			dwc2_host_exit_clock_gating(hsotg, 0);
468616c729f9SArtur Petrosyan 	}
468716c729f9SArtur Petrosyan 
46889da51974SJohn Youn 	if (!ep)
4689197ba5f4SPaul Zimmerman 		return -EINVAL;
4690197ba5f4SPaul Zimmerman 
4691197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4692197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4693197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
4694197ba5f4SPaul Zimmerman 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4695197ba5f4SPaul Zimmerman 			alloc_bandwidth = 1;
4696197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4697197ba5f4SPaul Zimmerman 	}
4698197ba5f4SPaul Zimmerman 
4699197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4700197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4701197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_CONTROL;
4702197ba5f4SPaul Zimmerman 		break;
4703197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4704197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_ISOC;
4705197ba5f4SPaul Zimmerman 		break;
4706197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4707197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_BULK;
4708197ba5f4SPaul Zimmerman 		break;
4709197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4710197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_INT;
4711197ba5f4SPaul Zimmerman 		break;
4712197ba5f4SPaul Zimmerman 	}
4713197ba5f4SPaul Zimmerman 
4714197ba5f4SPaul Zimmerman 	dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4715197ba5f4SPaul Zimmerman 				      mem_flags);
4716197ba5f4SPaul Zimmerman 	if (!dwc2_urb)
4717197ba5f4SPaul Zimmerman 		return -ENOMEM;
4718197ba5f4SPaul Zimmerman 
4719197ba5f4SPaul Zimmerman 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4720197ba5f4SPaul Zimmerman 				  usb_pipeendpoint(urb->pipe), ep_type,
4721197ba5f4SPaul Zimmerman 				  usb_pipein(urb->pipe),
4722babd1839SDouglas Anderson 				  usb_endpoint_maxp(&ep->desc),
4723babd1839SDouglas Anderson 				  usb_endpoint_maxp_mult(&ep->desc));
4724197ba5f4SPaul Zimmerman 
4725197ba5f4SPaul Zimmerman 	buf = urb->transfer_buffer;
4726197ba5f4SPaul Zimmerman 
4727edfbcb32SChristoph Hellwig 	if (hcd_uses_dma(hcd)) {
4728197ba5f4SPaul Zimmerman 		if (!buf && (urb->transfer_dma & 3)) {
4729197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
4730197ba5f4SPaul Zimmerman 				"%s: unaligned transfer with no transfer_buffer",
4731197ba5f4SPaul Zimmerman 				__func__);
4732197ba5f4SPaul Zimmerman 			retval = -EINVAL;
473333ad261aSGregory Herrero 			goto fail0;
4734197ba5f4SPaul Zimmerman 		}
4735197ba5f4SPaul Zimmerman 	}
4736197ba5f4SPaul Zimmerman 
4737197ba5f4SPaul Zimmerman 	if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4738197ba5f4SPaul Zimmerman 		tflags |= URB_GIVEBACK_ASAP;
4739197ba5f4SPaul Zimmerman 	if (urb->transfer_flags & URB_ZERO_PACKET)
4740197ba5f4SPaul Zimmerman 		tflags |= URB_SEND_ZERO_PACKET;
4741197ba5f4SPaul Zimmerman 
4742197ba5f4SPaul Zimmerman 	dwc2_urb->priv = urb;
4743197ba5f4SPaul Zimmerman 	dwc2_urb->buf = buf;
4744197ba5f4SPaul Zimmerman 	dwc2_urb->dma = urb->transfer_dma;
4745197ba5f4SPaul Zimmerman 	dwc2_urb->length = urb->transfer_buffer_length;
4746197ba5f4SPaul Zimmerman 	dwc2_urb->setup_packet = urb->setup_packet;
4747197ba5f4SPaul Zimmerman 	dwc2_urb->setup_dma = urb->setup_dma;
4748197ba5f4SPaul Zimmerman 	dwc2_urb->flags = tflags;
4749197ba5f4SPaul Zimmerman 	dwc2_urb->interval = urb->interval;
4750197ba5f4SPaul Zimmerman 	dwc2_urb->status = -EINPROGRESS;
4751197ba5f4SPaul Zimmerman 
4752197ba5f4SPaul Zimmerman 	for (i = 0; i < urb->number_of_packets; ++i)
4753197ba5f4SPaul Zimmerman 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4754197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].offset,
4755197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].length);
4756197ba5f4SPaul Zimmerman 
4757197ba5f4SPaul Zimmerman 	urb->hcpriv = dwc2_urb;
4758b58e6ceeSMian Yousaf Kaukab 	qh = (struct dwc2_qh *)ep->hcpriv;
4759b58e6ceeSMian Yousaf Kaukab 	/* Create QH for the endpoint if it doesn't exist */
4760b58e6ceeSMian Yousaf Kaukab 	if (!qh) {
4761b58e6ceeSMian Yousaf Kaukab 		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4762b58e6ceeSMian Yousaf Kaukab 		if (!qh) {
4763b58e6ceeSMian Yousaf Kaukab 			retval = -ENOMEM;
4764b58e6ceeSMian Yousaf Kaukab 			goto fail0;
4765b58e6ceeSMian Yousaf Kaukab 		}
4766b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = qh;
4767b58e6ceeSMian Yousaf Kaukab 		qh_allocated = true;
4768b58e6ceeSMian Yousaf Kaukab 	}
4769197ba5f4SPaul Zimmerman 
4770b5a468a6SMian Yousaf Kaukab 	qtd = kzalloc(sizeof(*qtd), mem_flags);
4771b5a468a6SMian Yousaf Kaukab 	if (!qtd) {
4772b5a468a6SMian Yousaf Kaukab 		retval = -ENOMEM;
4773b5a468a6SMian Yousaf Kaukab 		goto fail1;
4774b5a468a6SMian Yousaf Kaukab 	}
4775b5a468a6SMian Yousaf Kaukab 
4776197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4777197ba5f4SPaul Zimmerman 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
4778197ba5f4SPaul Zimmerman 	if (retval)
4779197ba5f4SPaul Zimmerman 		goto fail2;
4780197ba5f4SPaul Zimmerman 
4781b5a468a6SMian Yousaf Kaukab 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4782b5a468a6SMian Yousaf Kaukab 	if (retval)
4783b5a468a6SMian Yousaf Kaukab 		goto fail3;
4784b5a468a6SMian Yousaf Kaukab 
4785197ba5f4SPaul Zimmerman 	if (alloc_bandwidth) {
4786197ba5f4SPaul Zimmerman 		dwc2_allocate_bus_bandwidth(hcd,
4787197ba5f4SPaul Zimmerman 				dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4788197ba5f4SPaul Zimmerman 				urb);
4789197ba5f4SPaul Zimmerman 	}
4790197ba5f4SPaul Zimmerman 
479133ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
479233ad261aSGregory Herrero 
4793197ba5f4SPaul Zimmerman 	return 0;
4794197ba5f4SPaul Zimmerman 
4795b5a468a6SMian Yousaf Kaukab fail3:
4796197ba5f4SPaul Zimmerman 	dwc2_urb->priv = NULL;
4797197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
479816e80218SDouglas Anderson 	if (qh_allocated && qh->channel && qh->channel->qh == qh)
479916e80218SDouglas Anderson 		qh->channel->qh = NULL;
4800b5a468a6SMian Yousaf Kaukab fail2:
480133ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4802197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4803b5a468a6SMian Yousaf Kaukab 	kfree(qtd);
4804b5a468a6SMian Yousaf Kaukab fail1:
4805b58e6ceeSMian Yousaf Kaukab 	if (qh_allocated) {
4806b58e6ceeSMian Yousaf Kaukab 		struct dwc2_qtd *qtd2, *qtd2_tmp;
4807b58e6ceeSMian Yousaf Kaukab 
4808b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = NULL;
4809b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_unlink(hsotg, qh);
4810b58e6ceeSMian Yousaf Kaukab 		/* Free each QTD in the QH's QTD list */
4811b58e6ceeSMian Yousaf Kaukab 		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
4812b58e6ceeSMian Yousaf Kaukab 					 qtd_list_entry)
4813b58e6ceeSMian Yousaf Kaukab 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4814b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_free(hsotg, qh);
4815b58e6ceeSMian Yousaf Kaukab 	}
481633ad261aSGregory Herrero fail0:
4817197ba5f4SPaul Zimmerman 	kfree(dwc2_urb);
4818197ba5f4SPaul Zimmerman 
4819197ba5f4SPaul Zimmerman 	return retval;
4820197ba5f4SPaul Zimmerman }
4821197ba5f4SPaul Zimmerman 
4822197ba5f4SPaul Zimmerman /*
4823197ba5f4SPaul Zimmerman  * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4824197ba5f4SPaul Zimmerman  */
4825197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4826197ba5f4SPaul Zimmerman 				 int status)
4827197ba5f4SPaul Zimmerman {
4828197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4829197ba5f4SPaul Zimmerman 	int rc;
4830197ba5f4SPaul Zimmerman 	unsigned long flags;
4831197ba5f4SPaul Zimmerman 
4832197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4833197ba5f4SPaul Zimmerman 	dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4834197ba5f4SPaul Zimmerman 
4835197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4836197ba5f4SPaul Zimmerman 
4837197ba5f4SPaul Zimmerman 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4838197ba5f4SPaul Zimmerman 	if (rc)
4839197ba5f4SPaul Zimmerman 		goto out;
4840197ba5f4SPaul Zimmerman 
4841197ba5f4SPaul Zimmerman 	if (!urb->hcpriv) {
4842197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4843197ba5f4SPaul Zimmerman 		goto out;
4844197ba5f4SPaul Zimmerman 	}
4845197ba5f4SPaul Zimmerman 
4846197ba5f4SPaul Zimmerman 	rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4847197ba5f4SPaul Zimmerman 
4848197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
4849197ba5f4SPaul Zimmerman 
4850197ba5f4SPaul Zimmerman 	kfree(urb->hcpriv);
4851197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4852197ba5f4SPaul Zimmerman 
4853197ba5f4SPaul Zimmerman 	/* Higher layer software sets URB status */
4854197ba5f4SPaul Zimmerman 	spin_unlock(&hsotg->lock);
4855197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(hcd, urb, status);
4856197ba5f4SPaul Zimmerman 	spin_lock(&hsotg->lock);
4857197ba5f4SPaul Zimmerman 
4858197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4859197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  urb->status = %d\n", urb->status);
4860197ba5f4SPaul Zimmerman out:
4861197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4862197ba5f4SPaul Zimmerman 
4863197ba5f4SPaul Zimmerman 	return rc;
4864197ba5f4SPaul Zimmerman }
4865197ba5f4SPaul Zimmerman 
4866197ba5f4SPaul Zimmerman /*
4867197ba5f4SPaul Zimmerman  * Frees resources in the DWC_otg controller related to a given endpoint. Also
4868197ba5f4SPaul Zimmerman  * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4869197ba5f4SPaul Zimmerman  * must already be dequeued.
4870197ba5f4SPaul Zimmerman  */
4871197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4872197ba5f4SPaul Zimmerman 				       struct usb_host_endpoint *ep)
4873197ba5f4SPaul Zimmerman {
4874197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4875197ba5f4SPaul Zimmerman 
4876197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4877197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4878197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress, ep->hcpriv);
4879197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4880197ba5f4SPaul Zimmerman }
4881197ba5f4SPaul Zimmerman 
4882197ba5f4SPaul Zimmerman /*
4883197ba5f4SPaul Zimmerman  * Resets endpoint specific parameter values, in current version used to reset
4884197ba5f4SPaul Zimmerman  * the data toggle (as a WA). This function can be called from usb_clear_halt
4885197ba5f4SPaul Zimmerman  * routine.
4886197ba5f4SPaul Zimmerman  */
4887197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4888197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep)
4889197ba5f4SPaul Zimmerman {
4890197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4891197ba5f4SPaul Zimmerman 	unsigned long flags;
4892197ba5f4SPaul Zimmerman 
4893197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4894197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4895197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress);
4896197ba5f4SPaul Zimmerman 
4897197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4898197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_reset(hsotg, ep);
4899197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4900197ba5f4SPaul Zimmerman }
4901197ba5f4SPaul Zimmerman 
4902197ba5f4SPaul Zimmerman /*
4903197ba5f4SPaul Zimmerman  * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4904197ba5f4SPaul Zimmerman  * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4905197ba5f4SPaul Zimmerman  * interrupt.
4906197ba5f4SPaul Zimmerman  *
4907197ba5f4SPaul Zimmerman  * This function is called by the USB core when an interrupt occurs
4908197ba5f4SPaul Zimmerman  */
4909197ba5f4SPaul Zimmerman static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4910197ba5f4SPaul Zimmerman {
4911197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4912197ba5f4SPaul Zimmerman 
4913197ba5f4SPaul Zimmerman 	return dwc2_handle_hcd_intr(hsotg);
4914197ba5f4SPaul Zimmerman }
4915197ba5f4SPaul Zimmerman 
4916197ba5f4SPaul Zimmerman /*
4917197ba5f4SPaul Zimmerman  * Creates Status Change bitmap for the root hub and root port. The bitmap is
4918197ba5f4SPaul Zimmerman  * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4919197ba5f4SPaul Zimmerman  * is the status change indicator for the single root port. Returns 1 if either
4920197ba5f4SPaul Zimmerman  * change indicator is 1, otherwise returns 0.
4921197ba5f4SPaul Zimmerman  */
4922197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4923197ba5f4SPaul Zimmerman {
4924197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4925197ba5f4SPaul Zimmerman 
4926197ba5f4SPaul Zimmerman 	buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4927197ba5f4SPaul Zimmerman 	return buf[0] != 0;
4928197ba5f4SPaul Zimmerman }
4929197ba5f4SPaul Zimmerman 
4930197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
4931197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4932197ba5f4SPaul Zimmerman 				 u16 windex, char *buf, u16 wlength)
4933197ba5f4SPaul Zimmerman {
4934197ba5f4SPaul Zimmerman 	int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4935197ba5f4SPaul Zimmerman 					  wvalue, windex, buf, wlength);
4936197ba5f4SPaul Zimmerman 	return retval;
4937197ba5f4SPaul Zimmerman }
4938197ba5f4SPaul Zimmerman 
4939197ba5f4SPaul Zimmerman /* Handles hub TT buffer clear completions */
4940197ba5f4SPaul Zimmerman static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4941197ba5f4SPaul Zimmerman 					       struct usb_host_endpoint *ep)
4942197ba5f4SPaul Zimmerman {
4943197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4944197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
4945197ba5f4SPaul Zimmerman 	unsigned long flags;
4946197ba5f4SPaul Zimmerman 
4947197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
4948197ba5f4SPaul Zimmerman 	if (!qh)
4949197ba5f4SPaul Zimmerman 		return;
4950197ba5f4SPaul Zimmerman 
4951197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4952197ba5f4SPaul Zimmerman 	qh->tt_buffer_dirty = 0;
4953197ba5f4SPaul Zimmerman 
4954197ba5f4SPaul Zimmerman 	if (hsotg->flags.b.port_connect_status)
4955197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4956197ba5f4SPaul Zimmerman 
4957197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4958197ba5f4SPaul Zimmerman }
4959197ba5f4SPaul Zimmerman 
4960ca8b0332SChen Yu /*
4961ca8b0332SChen Yu  * HPRT0_SPD_HIGH_SPEED: high speed
4962ca8b0332SChen Yu  * HPRT0_SPD_FULL_SPEED: full speed
4963ca8b0332SChen Yu  */
4964ca8b0332SChen Yu static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4965ca8b0332SChen Yu {
4966ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4967ca8b0332SChen Yu 
4968ca8b0332SChen Yu 	if (hsotg->params.speed == speed)
4969ca8b0332SChen Yu 		return;
4970ca8b0332SChen Yu 
4971ca8b0332SChen Yu 	hsotg->params.speed = speed;
4972ca8b0332SChen Yu 	queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4973ca8b0332SChen Yu }
4974ca8b0332SChen Yu 
4975ca8b0332SChen Yu static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4976ca8b0332SChen Yu {
4977ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4978ca8b0332SChen Yu 
4979ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4980ca8b0332SChen Yu 		return;
4981ca8b0332SChen Yu 
4982ca8b0332SChen Yu 	/*
4983ca8b0332SChen Yu 	 * On removal, set speed to default high-speed.
4984ca8b0332SChen Yu 	 */
4985ca8b0332SChen Yu 	if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4986ca8b0332SChen Yu 	    udev->parent->speed < USB_SPEED_HIGH) {
4987ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to default high-speed\n");
4988ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4989ca8b0332SChen Yu 	}
4990ca8b0332SChen Yu }
4991ca8b0332SChen Yu 
4992ca8b0332SChen Yu static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4993ca8b0332SChen Yu {
4994ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4995ca8b0332SChen Yu 
4996ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4997ca8b0332SChen Yu 		return 0;
4998ca8b0332SChen Yu 
4999ca8b0332SChen Yu 	if (udev->speed == USB_SPEED_HIGH) {
5000ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to high-speed\n");
5001ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
5002ca8b0332SChen Yu 	} else if ((udev->speed == USB_SPEED_FULL ||
5003ca8b0332SChen Yu 				udev->speed == USB_SPEED_LOW)) {
5004ca8b0332SChen Yu 		/*
5005ca8b0332SChen Yu 		 * Change speed setting to full-speed if there's
5006ca8b0332SChen Yu 		 * a full-speed or low-speed device plugged in.
5007ca8b0332SChen Yu 		 */
5008ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to full-speed\n");
5009ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
5010ca8b0332SChen Yu 	}
5011ca8b0332SChen Yu 
5012ca8b0332SChen Yu 	return 0;
5013ca8b0332SChen Yu }
5014ca8b0332SChen Yu 
5015197ba5f4SPaul Zimmerman static struct hc_driver dwc2_hc_driver = {
5016197ba5f4SPaul Zimmerman 	.description = "dwc2_hsotg",
5017197ba5f4SPaul Zimmerman 	.product_desc = "DWC OTG Controller",
5018197ba5f4SPaul Zimmerman 	.hcd_priv_size = sizeof(struct wrapper_priv_data),
5019197ba5f4SPaul Zimmerman 
5020197ba5f4SPaul Zimmerman 	.irq = _dwc2_hcd_irq,
50218add17cfSDouglas Anderson 	.flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
5022197ba5f4SPaul Zimmerman 
5023197ba5f4SPaul Zimmerman 	.start = _dwc2_hcd_start,
5024197ba5f4SPaul Zimmerman 	.stop = _dwc2_hcd_stop,
5025197ba5f4SPaul Zimmerman 	.urb_enqueue = _dwc2_hcd_urb_enqueue,
5026197ba5f4SPaul Zimmerman 	.urb_dequeue = _dwc2_hcd_urb_dequeue,
5027197ba5f4SPaul Zimmerman 	.endpoint_disable = _dwc2_hcd_endpoint_disable,
5028197ba5f4SPaul Zimmerman 	.endpoint_reset = _dwc2_hcd_endpoint_reset,
5029197ba5f4SPaul Zimmerman 	.get_frame_number = _dwc2_hcd_get_frame_number,
5030197ba5f4SPaul Zimmerman 
5031197ba5f4SPaul Zimmerman 	.hub_status_data = _dwc2_hcd_hub_status_data,
5032197ba5f4SPaul Zimmerman 	.hub_control = _dwc2_hcd_hub_control,
5033197ba5f4SPaul Zimmerman 	.clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
503499a65798SGregory Herrero 
503599a65798SGregory Herrero 	.bus_suspend = _dwc2_hcd_suspend,
503699a65798SGregory Herrero 	.bus_resume = _dwc2_hcd_resume,
50373bc04e28SDouglas Anderson 
50383bc04e28SDouglas Anderson 	.map_urb_for_dma	= dwc2_map_urb_for_dma,
50393bc04e28SDouglas Anderson 	.unmap_urb_for_dma	= dwc2_unmap_urb_for_dma,
5040197ba5f4SPaul Zimmerman };
5041197ba5f4SPaul Zimmerman 
5042197ba5f4SPaul Zimmerman /*
5043197ba5f4SPaul Zimmerman  * Frees secondary storage associated with the dwc2_hsotg structure contained
5044197ba5f4SPaul Zimmerman  * in the struct usb_hcd field
5045197ba5f4SPaul Zimmerman  */
5046197ba5f4SPaul Zimmerman static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
5047197ba5f4SPaul Zimmerman {
5048197ba5f4SPaul Zimmerman 	u32 ahbcfg;
5049197ba5f4SPaul Zimmerman 	u32 dctl;
5050197ba5f4SPaul Zimmerman 	int i;
5051197ba5f4SPaul Zimmerman 
5052197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
5053197ba5f4SPaul Zimmerman 
5054197ba5f4SPaul Zimmerman 	/* Free memory for QH/QTD lists */
5055197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
505638d2b5fbSDouglas Anderson 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
5057197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
5058197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
5059197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
5060197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
5061197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
5062197ba5f4SPaul Zimmerman 
5063197ba5f4SPaul Zimmerman 	/* Free memory for the host channels */
5064197ba5f4SPaul Zimmerman 	for (i = 0; i < MAX_EPS_CHANNELS; i++) {
5065197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
5066197ba5f4SPaul Zimmerman 
50679da51974SJohn Youn 		if (chan) {
5068197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
5069197ba5f4SPaul Zimmerman 				i, chan);
5070197ba5f4SPaul Zimmerman 			hsotg->hc_ptr_array[i] = NULL;
5071197ba5f4SPaul Zimmerman 			kfree(chan);
5072197ba5f4SPaul Zimmerman 		}
5073197ba5f4SPaul Zimmerman 	}
5074197ba5f4SPaul Zimmerman 
507595832c00SJohn Youn 	if (hsotg->params.host_dma) {
5076197ba5f4SPaul Zimmerman 		if (hsotg->status_buf) {
5077197ba5f4SPaul Zimmerman 			dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
5078197ba5f4SPaul Zimmerman 					  hsotg->status_buf,
5079197ba5f4SPaul Zimmerman 					  hsotg->status_buf_dma);
5080197ba5f4SPaul Zimmerman 			hsotg->status_buf = NULL;
5081197ba5f4SPaul Zimmerman 		}
5082197ba5f4SPaul Zimmerman 	} else {
5083197ba5f4SPaul Zimmerman 		kfree(hsotg->status_buf);
5084197ba5f4SPaul Zimmerman 		hsotg->status_buf = NULL;
5085197ba5f4SPaul Zimmerman 	}
5086197ba5f4SPaul Zimmerman 
5087f25c42b8SGevorg Sahakyan 	ahbcfg = dwc2_readl(hsotg, GAHBCFG);
5088197ba5f4SPaul Zimmerman 
5089197ba5f4SPaul Zimmerman 	/* Disable all interrupts */
5090197ba5f4SPaul Zimmerman 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
5091f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
5092f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
5093197ba5f4SPaul Zimmerman 
5094197ba5f4SPaul Zimmerman 	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
5095f25c42b8SGevorg Sahakyan 		dctl = dwc2_readl(hsotg, DCTL);
5096197ba5f4SPaul Zimmerman 		dctl |= DCTL_SFTDISCON;
5097f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dctl, DCTL);
5098197ba5f4SPaul Zimmerman 	}
5099197ba5f4SPaul Zimmerman 
5100197ba5f4SPaul Zimmerman 	if (hsotg->wq_otg) {
5101197ba5f4SPaul Zimmerman 		if (!cancel_work_sync(&hsotg->wf_otg))
5102197ba5f4SPaul Zimmerman 			flush_workqueue(hsotg->wq_otg);
5103197ba5f4SPaul Zimmerman 		destroy_workqueue(hsotg->wq_otg);
5104197ba5f4SPaul Zimmerman 	}
5105197ba5f4SPaul Zimmerman 
5106c40cf770SDouglas Anderson 	cancel_work_sync(&hsotg->phy_reset_work);
5107c40cf770SDouglas Anderson 
5108197ba5f4SPaul Zimmerman 	del_timer(&hsotg->wkp_timer);
5109197ba5f4SPaul Zimmerman }
5110197ba5f4SPaul Zimmerman 
5111197ba5f4SPaul Zimmerman static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5112197ba5f4SPaul Zimmerman {
5113197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
5114197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
5115197ba5f4SPaul Zimmerman 
5116197ba5f4SPaul Zimmerman 	dwc2_hcd_free(hsotg);
5117197ba5f4SPaul Zimmerman }
5118197ba5f4SPaul Zimmerman 
5119197ba5f4SPaul Zimmerman /*
5120197ba5f4SPaul Zimmerman  * Initializes the HCD. This function allocates memory for and initializes the
5121197ba5f4SPaul Zimmerman  * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5122197ba5f4SPaul Zimmerman  * USB bus with the core and calls the hc_driver->start() function. It returns
5123197ba5f4SPaul Zimmerman  * a negative error on failure.
5124197ba5f4SPaul Zimmerman  */
51254fe160d5SHeiner Kallweit int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5126197ba5f4SPaul Zimmerman {
5127348becdcSHeiner Kallweit 	struct platform_device *pdev = to_platform_device(hsotg->dev);
5128348becdcSHeiner Kallweit 	struct resource *res;
5129197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5130197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
5131197ba5f4SPaul Zimmerman 	u32 hcfg;
5132197ba5f4SPaul Zimmerman 	int i, num_channels;
5133197ba5f4SPaul Zimmerman 	int retval;
5134197ba5f4SPaul Zimmerman 
5135f5500eccSDinh Nguyen 	if (usb_disabled())
5136f5500eccSDinh Nguyen 		return -ENODEV;
5137f5500eccSDinh Nguyen 
5138197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
5139197ba5f4SPaul Zimmerman 
5140197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5141197ba5f4SPaul Zimmerman 
5142f25c42b8SGevorg Sahakyan 	hcfg = dwc2_readl(hsotg, HCFG);
5143197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5144197ba5f4SPaul Zimmerman 
5145197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
51466396bb22SKees Cook 	hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
51476396bb22SKees Cook 					 sizeof(*hsotg->frame_num_array),
51486396bb22SKees Cook 					 GFP_KERNEL);
5149197ba5f4SPaul Zimmerman 	if (!hsotg->frame_num_array)
5150197ba5f4SPaul Zimmerman 		goto error1;
51516396bb22SKees Cook 	hsotg->last_frame_num_array =
51526396bb22SKees Cook 		kcalloc(FRAME_NUM_ARRAY_SIZE,
51536396bb22SKees Cook 			sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
5154197ba5f4SPaul Zimmerman 	if (!hsotg->last_frame_num_array)
5155197ba5f4SPaul Zimmerman 		goto error1;
5156197ba5f4SPaul Zimmerman #endif
5157483bb254SDouglas Anderson 	hsotg->last_frame_num = HFNUM_MAX_FRNUM;
5158197ba5f4SPaul Zimmerman 
5159197ba5f4SPaul Zimmerman 	/* Check if the bus driver or platform code has setup a dma_mask */
516095832c00SJohn Youn 	if (hsotg->params.host_dma &&
51619da51974SJohn Youn 	    !hsotg->dev->dma_mask) {
5162197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
5163197ba5f4SPaul Zimmerman 			 "dma_mask not set, disabling DMA\n");
5164fdb09b3eSNicholas Mc Guire 		hsotg->params.host_dma = false;
516595832c00SJohn Youn 		hsotg->params.dma_desc_enable = false;
5166197ba5f4SPaul Zimmerman 	}
5167197ba5f4SPaul Zimmerman 
5168197ba5f4SPaul Zimmerman 	/* Set device flags indicating whether the HCD supports DMA */
516995832c00SJohn Youn 	if (hsotg->params.host_dma) {
5170197ba5f4SPaul Zimmerman 		if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5171197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set DMA mask\n");
5172197ba5f4SPaul Zimmerman 		if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5173197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
5174197ba5f4SPaul Zimmerman 	}
5175197ba5f4SPaul Zimmerman 
5176ca8b0332SChen Yu 	if (hsotg->params.change_speed_quirk) {
5177ca8b0332SChen Yu 		dwc2_hc_driver.free_dev = dwc2_free_dev;
5178ca8b0332SChen Yu 		dwc2_hc_driver.reset_device = dwc2_reset_device;
5179ca8b0332SChen Yu 	}
5180ca8b0332SChen Yu 
51817b81cb6bSChristoph Hellwig 	if (hsotg->params.host_dma)
51827b81cb6bSChristoph Hellwig 		dwc2_hc_driver.flags |= HCD_DMA;
51837b81cb6bSChristoph Hellwig 
5184197ba5f4SPaul Zimmerman 	hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5185197ba5f4SPaul Zimmerman 	if (!hcd)
5186197ba5f4SPaul Zimmerman 		goto error1;
5187197ba5f4SPaul Zimmerman 
5188197ba5f4SPaul Zimmerman 	hcd->has_tt = 1;
5189197ba5f4SPaul Zimmerman 
5190348becdcSHeiner Kallweit 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5191856e6e8eSYang Yingliang 	if (!res) {
5192856e6e8eSYang Yingliang 		retval = -EINVAL;
5193856e6e8eSYang Yingliang 		goto error1;
5194856e6e8eSYang Yingliang 	}
5195348becdcSHeiner Kallweit 	hcd->rsrc_start = res->start;
5196348becdcSHeiner Kallweit 	hcd->rsrc_len = resource_size(res);
5197348becdcSHeiner Kallweit 
5198197ba5f4SPaul Zimmerman 	((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
5199197ba5f4SPaul Zimmerman 	hsotg->priv = hcd;
5200197ba5f4SPaul Zimmerman 
5201197ba5f4SPaul Zimmerman 	/*
5202197ba5f4SPaul Zimmerman 	 * Disable the global interrupt until all the interrupt handlers are
5203197ba5f4SPaul Zimmerman 	 * installed
5204197ba5f4SPaul Zimmerman 	 */
5205197ba5f4SPaul Zimmerman 	dwc2_disable_global_interrupts(hsotg);
5206197ba5f4SPaul Zimmerman 
5207197ba5f4SPaul Zimmerman 	/* Initialize the DWC_otg core, and select the Phy type */
52080fe239bcSDouglas Anderson 	retval = dwc2_core_init(hsotg, true);
5209197ba5f4SPaul Zimmerman 	if (retval)
5210197ba5f4SPaul Zimmerman 		goto error2;
5211197ba5f4SPaul Zimmerman 
5212197ba5f4SPaul Zimmerman 	/* Create new workqueue and init work */
5213197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5214ec7b1268SBhaktipriya Shridhar 	hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
5215197ba5f4SPaul Zimmerman 	if (!hsotg->wq_otg) {
5216197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Failed to create workqueue\n");
5217197ba5f4SPaul Zimmerman 		goto error2;
5218197ba5f4SPaul Zimmerman 	}
5219197ba5f4SPaul Zimmerman 	INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5220197ba5f4SPaul Zimmerman 
5221e99e88a9SKees Cook 	timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
5222197ba5f4SPaul Zimmerman 
5223197ba5f4SPaul Zimmerman 	/* Initialize the non-periodic schedule */
5224197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
522538d2b5fbSDouglas Anderson 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
5226197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5227197ba5f4SPaul Zimmerman 
5228197ba5f4SPaul Zimmerman 	/* Initialize the periodic schedule */
5229197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5230197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5231197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5232197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5233197ba5f4SPaul Zimmerman 
5234c9c8ac01SDouglas Anderson 	INIT_LIST_HEAD(&hsotg->split_order);
5235c9c8ac01SDouglas Anderson 
5236197ba5f4SPaul Zimmerman 	/*
5237197ba5f4SPaul Zimmerman 	 * Create a host channel descriptor for each host channel implemented
5238197ba5f4SPaul Zimmerman 	 * in the controller. Initialize the channel descriptor array.
5239197ba5f4SPaul Zimmerman 	 */
5240197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->free_hc_list);
5241bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
5242197ba5f4SPaul Zimmerman 	memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5243197ba5f4SPaul Zimmerman 
5244197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
5245197ba5f4SPaul Zimmerman 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
52469da51974SJohn Youn 		if (!channel)
5247197ba5f4SPaul Zimmerman 			goto error3;
5248197ba5f4SPaul Zimmerman 		channel->hc_num = i;
5249c9c8ac01SDouglas Anderson 		INIT_LIST_HEAD(&channel->split_order_list_entry);
5250197ba5f4SPaul Zimmerman 		hsotg->hc_ptr_array[i] = channel;
5251197ba5f4SPaul Zimmerman 	}
5252197ba5f4SPaul Zimmerman 
5253c40cf770SDouglas Anderson 	/* Initialize work */
5254197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5255197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5256c40cf770SDouglas Anderson 	INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
5257197ba5f4SPaul Zimmerman 
5258197ba5f4SPaul Zimmerman 	/*
5259197ba5f4SPaul Zimmerman 	 * Allocate space for storing data on status transactions. Normally no
5260197ba5f4SPaul Zimmerman 	 * data is sent, but this space acts as a bit bucket. This must be
5261197ba5f4SPaul Zimmerman 	 * done after usb_add_hcd since that function allocates the DMA buffer
5262197ba5f4SPaul Zimmerman 	 * pool.
5263197ba5f4SPaul Zimmerman 	 */
526495832c00SJohn Youn 	if (hsotg->params.host_dma)
5265197ba5f4SPaul Zimmerman 		hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5266197ba5f4SPaul Zimmerman 					DWC2_HCD_STATUS_BUF_SIZE,
5267197ba5f4SPaul Zimmerman 					&hsotg->status_buf_dma, GFP_KERNEL);
5268197ba5f4SPaul Zimmerman 	else
5269197ba5f4SPaul Zimmerman 		hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5270197ba5f4SPaul Zimmerman 					  GFP_KERNEL);
5271197ba5f4SPaul Zimmerman 
5272197ba5f4SPaul Zimmerman 	if (!hsotg->status_buf)
5273197ba5f4SPaul Zimmerman 		goto error3;
5274197ba5f4SPaul Zimmerman 
52753b5fcc9aSGregory Herrero 	/*
52763b5fcc9aSGregory Herrero 	 * Create kmem caches to handle descriptor buffers in descriptor
52773b5fcc9aSGregory Herrero 	 * DMA mode.
52783b5fcc9aSGregory Herrero 	 * Alignment must be set to 512 bytes.
52793b5fcc9aSGregory Herrero 	 */
5280bea8e86cSJohn Youn 	if (hsotg->params.dma_desc_enable ||
5281bea8e86cSJohn Youn 	    hsotg->params.dma_desc_fs_enable) {
52823b5fcc9aSGregory Herrero 		hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
5283ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52843b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
52853b5fcc9aSGregory Herrero 				NULL);
52863b5fcc9aSGregory Herrero 		if (!hsotg->desc_gen_cache) {
52873b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52883b5fcc9aSGregory Herrero 				"unable to create dwc2 generic desc cache\n");
52893b5fcc9aSGregory Herrero 
52903b5fcc9aSGregory Herrero 			/*
52913b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52923b5fcc9aSGregory Herrero 			 * usable.
52933b5fcc9aSGregory Herrero 			 */
529495832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
529595832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
52963b5fcc9aSGregory Herrero 		}
52973b5fcc9aSGregory Herrero 
52983b5fcc9aSGregory Herrero 		hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
5299ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
53003b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
53013b5fcc9aSGregory Herrero 		if (!hsotg->desc_hsisoc_cache) {
53023b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
53033b5fcc9aSGregory Herrero 				"unable to create dwc2 hs isoc desc cache\n");
53043b5fcc9aSGregory Herrero 
53053b5fcc9aSGregory Herrero 			kmem_cache_destroy(hsotg->desc_gen_cache);
53063b5fcc9aSGregory Herrero 
53073b5fcc9aSGregory Herrero 			/*
53083b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
53093b5fcc9aSGregory Herrero 			 * usable.
53103b5fcc9aSGregory Herrero 			 */
531195832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
531295832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
53133b5fcc9aSGregory Herrero 		}
53143b5fcc9aSGregory Herrero 	}
53153b5fcc9aSGregory Herrero 
5316af424a41SWilliam Wu 	if (hsotg->params.host_dma) {
5317af424a41SWilliam Wu 		/*
5318af424a41SWilliam Wu 		 * Create kmem caches to handle non-aligned buffer
5319af424a41SWilliam Wu 		 * in Buffer DMA mode.
5320af424a41SWilliam Wu 		 */
5321af424a41SWilliam Wu 		hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
5322af424a41SWilliam Wu 						DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
5323af424a41SWilliam Wu 						SLAB_CACHE_DMA, NULL);
5324af424a41SWilliam Wu 		if (!hsotg->unaligned_cache)
5325af424a41SWilliam Wu 			dev_err(hsotg->dev,
5326af424a41SWilliam Wu 				"unable to create dwc2 unaligned cache\n");
5327af424a41SWilliam Wu 	}
5328af424a41SWilliam Wu 
5329197ba5f4SPaul Zimmerman 	hsotg->otg_port = 1;
5330197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
5331197ba5f4SPaul Zimmerman 	hsotg->frame_list_dma = 0;
5332197ba5f4SPaul Zimmerman 	hsotg->periodic_qh_count = 0;
5333197ba5f4SPaul Zimmerman 
5334197ba5f4SPaul Zimmerman 	/* Initiate lx_state to L3 disconnected state */
5335197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L3;
5336197ba5f4SPaul Zimmerman 
5337197ba5f4SPaul Zimmerman 	hcd->self.otg_port = hsotg->otg_port;
5338197ba5f4SPaul Zimmerman 
5339197ba5f4SPaul Zimmerman 	/* Don't support SG list at this point */
5340197ba5f4SPaul Zimmerman 	hcd->self.sg_tablesize = 0;
5341197ba5f4SPaul Zimmerman 
53429df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
53439df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, &hcd->self);
53449df4ceacSMian Yousaf Kaukab 
5345197ba5f4SPaul Zimmerman 	/*
5346197ba5f4SPaul Zimmerman 	 * Finish generic HCD initialization and start the HCD. This function
5347197ba5f4SPaul Zimmerman 	 * allocates the DMA buffer pool, registers the USB bus, requests the
5348197ba5f4SPaul Zimmerman 	 * IRQ line, and calls hcd_start method.
5349197ba5f4SPaul Zimmerman 	 */
53504fe160d5SHeiner Kallweit 	retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
5351197ba5f4SPaul Zimmerman 	if (retval < 0)
53523b5fcc9aSGregory Herrero 		goto error4;
5353197ba5f4SPaul Zimmerman 
5354ec513b16SLinus Torvalds 	device_wakeup_enable(hcd->self.controller);
5355ec513b16SLinus Torvalds 
5356197ba5f4SPaul Zimmerman 	dwc2_hcd_dump_state(hsotg);
5357197ba5f4SPaul Zimmerman 
5358197ba5f4SPaul Zimmerman 	dwc2_enable_global_interrupts(hsotg);
5359197ba5f4SPaul Zimmerman 
5360197ba5f4SPaul Zimmerman 	return 0;
5361197ba5f4SPaul Zimmerman 
53623b5fcc9aSGregory Herrero error4:
5363af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53643b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5365af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
5366197ba5f4SPaul Zimmerman error3:
5367197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5368197ba5f4SPaul Zimmerman error2:
5369197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5370197ba5f4SPaul Zimmerman error1:
5371197ba5f4SPaul Zimmerman 
5372197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5373197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5374197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5375197ba5f4SPaul Zimmerman #endif
5376197ba5f4SPaul Zimmerman 
5377197ba5f4SPaul Zimmerman 	dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
5378197ba5f4SPaul Zimmerman 	return retval;
5379197ba5f4SPaul Zimmerman }
5380197ba5f4SPaul Zimmerman 
5381197ba5f4SPaul Zimmerman /*
5382197ba5f4SPaul Zimmerman  * Removes the HCD.
5383197ba5f4SPaul Zimmerman  * Frees memory and resources associated with the HCD and deregisters the bus.
5384197ba5f4SPaul Zimmerman  */
5385197ba5f4SPaul Zimmerman void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
5386197ba5f4SPaul Zimmerman {
5387197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5388197ba5f4SPaul Zimmerman 
5389197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
5390197ba5f4SPaul Zimmerman 
5391197ba5f4SPaul Zimmerman 	hcd = dwc2_hsotg_to_hcd(hsotg);
5392197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
5393197ba5f4SPaul Zimmerman 
5394197ba5f4SPaul Zimmerman 	if (!hcd) {
5395197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
5396197ba5f4SPaul Zimmerman 			__func__);
5397197ba5f4SPaul Zimmerman 		return;
5398197ba5f4SPaul Zimmerman 	}
5399197ba5f4SPaul Zimmerman 
54009df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
54019df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, NULL);
54029df4ceacSMian Yousaf Kaukab 
5403197ba5f4SPaul Zimmerman 	usb_remove_hcd(hcd);
5404197ba5f4SPaul Zimmerman 	hsotg->priv = NULL;
54053b5fcc9aSGregory Herrero 
5406af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
54073b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5408af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
54093b5fcc9aSGregory Herrero 
5410197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5411197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5412197ba5f4SPaul Zimmerman 
5413197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5414197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5415197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5416197ba5f4SPaul Zimmerman #endif
5417197ba5f4SPaul Zimmerman }
541858e52ff6SJohn Youn 
541958e52ff6SJohn Youn /**
542058e52ff6SJohn Youn  * dwc2_backup_host_registers() - Backup controller host registers.
542158e52ff6SJohn Youn  * When suspending usb bus, registers needs to be backuped
542258e52ff6SJohn Youn  * if controller power is disabled once suspended.
542358e52ff6SJohn Youn  *
542458e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
542558e52ff6SJohn Youn  */
542658e52ff6SJohn Youn int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
542758e52ff6SJohn Youn {
542858e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
542958e52ff6SJohn Youn 	int i;
543058e52ff6SJohn Youn 
543158e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
543258e52ff6SJohn Youn 
543358e52ff6SJohn Youn 	/* Backup Host regs */
543458e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
5435f25c42b8SGevorg Sahakyan 	hr->hcfg = dwc2_readl(hsotg, HCFG);
5436f25c42b8SGevorg Sahakyan 	hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
5437bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5438f25c42b8SGevorg Sahakyan 		hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
543958e52ff6SJohn Youn 
544058e52ff6SJohn Youn 	hr->hprt0 = dwc2_read_hprt0(hsotg);
5441f25c42b8SGevorg Sahakyan 	hr->hfir = dwc2_readl(hsotg, HFIR);
5442f25c42b8SGevorg Sahakyan 	hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
544358e52ff6SJohn Youn 	hr->valid = true;
544458e52ff6SJohn Youn 
544558e52ff6SJohn Youn 	return 0;
544658e52ff6SJohn Youn }
544758e52ff6SJohn Youn 
544858e52ff6SJohn Youn /**
544958e52ff6SJohn Youn  * dwc2_restore_host_registers() - Restore controller host registers.
545058e52ff6SJohn Youn  * When resuming usb bus, device registers needs to be restored
545158e52ff6SJohn Youn  * if controller power were disabled.
545258e52ff6SJohn Youn  *
545358e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
545458e52ff6SJohn Youn  */
545558e52ff6SJohn Youn int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
545658e52ff6SJohn Youn {
545758e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
545858e52ff6SJohn Youn 	int i;
545958e52ff6SJohn Youn 
546058e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
546158e52ff6SJohn Youn 
546258e52ff6SJohn Youn 	/* Restore host regs */
546358e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
546458e52ff6SJohn Youn 	if (!hr->valid) {
546558e52ff6SJohn Youn 		dev_err(hsotg->dev, "%s: no host registers to restore\n",
546658e52ff6SJohn Youn 			__func__);
546758e52ff6SJohn Youn 		return -EINVAL;
546858e52ff6SJohn Youn 	}
546958e52ff6SJohn Youn 	hr->valid = false;
547058e52ff6SJohn Youn 
5471f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5472f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
547358e52ff6SJohn Youn 
5474bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5475f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
547658e52ff6SJohn Youn 
5477f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hprt0, HPRT0);
5478f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hfir, HFIR);
5479f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
548058e52ff6SJohn Youn 	hsotg->frame_number = 0;
548158e52ff6SJohn Youn 
548258e52ff6SJohn Youn 	return 0;
548358e52ff6SJohn Youn }
5484c5c403dcSVardan Mikayelyan 
5485c5c403dcSVardan Mikayelyan /**
5486c5c403dcSVardan Mikayelyan  * dwc2_host_enter_hibernation() - Put controller in Hibernation.
5487c5c403dcSVardan Mikayelyan  *
5488c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5489c5c403dcSVardan Mikayelyan  */
5490c5c403dcSVardan Mikayelyan int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
5491c5c403dcSVardan Mikayelyan {
5492c5c403dcSVardan Mikayelyan 	unsigned long flags;
5493c5c403dcSVardan Mikayelyan 	int ret = 0;
5494c5c403dcSVardan Mikayelyan 	u32 hprt0;
5495c5c403dcSVardan Mikayelyan 	u32 pcgcctl;
5496c5c403dcSVardan Mikayelyan 	u32 gusbcfg;
5497c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5498c5c403dcSVardan Mikayelyan 
5499c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
5500c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_global_registers(hsotg);
5501c5c403dcSVardan Mikayelyan 	if (ret) {
5502c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5503c5c403dcSVardan Mikayelyan 			__func__);
5504c5c403dcSVardan Mikayelyan 		return ret;
5505c5c403dcSVardan Mikayelyan 	}
5506c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_host_registers(hsotg);
5507c5c403dcSVardan Mikayelyan 	if (ret) {
5508c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5509c5c403dcSVardan Mikayelyan 			__func__);
5510c5c403dcSVardan Mikayelyan 		return ret;
5511c5c403dcSVardan Mikayelyan 	}
5512c5c403dcSVardan Mikayelyan 
5513c5c403dcSVardan Mikayelyan 	/* Enter USB Suspend Mode */
5514f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5515c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_SUSP;
5516c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5517f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5518c5c403dcSVardan Mikayelyan 
5519c5c403dcSVardan Mikayelyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
55205e3bbae8SArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
552107b8dc55SColin Ian King 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
5522c5c403dcSVardan Mikayelyan 
5523c5c403dcSVardan Mikayelyan 	/*
5524c5c403dcSVardan Mikayelyan 	 * We need to disable interrupts to prevent servicing of any IRQ
5525c5c403dcSVardan Mikayelyan 	 * during going to hibernation
5526c5c403dcSVardan Mikayelyan 	 */
5527c5c403dcSVardan Mikayelyan 	spin_lock_irqsave(&hsotg->lock, flags);
5528c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L2;
5529c5c403dcSVardan Mikayelyan 
5530f25c42b8SGevorg Sahakyan 	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
5531c5c403dcSVardan Mikayelyan 	if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
5532c5c403dcSVardan Mikayelyan 		/* ULPI interface */
5533c5c403dcSVardan Mikayelyan 		/* Suspend the Phy Clock */
5534f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5535c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5536f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5537c5c403dcSVardan Mikayelyan 		udelay(10);
5538c5c403dcSVardan Mikayelyan 
5539f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5540c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5541f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5542c5c403dcSVardan Mikayelyan 		udelay(10);
5543c5c403dcSVardan Mikayelyan 	} else {
5544c5c403dcSVardan Mikayelyan 		/* UTMI+ Interface */
5545f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5546c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5547f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5548c5c403dcSVardan Mikayelyan 		udelay(10);
5549c5c403dcSVardan Mikayelyan 
5550f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5551c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5552f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5553c5c403dcSVardan Mikayelyan 		udelay(10);
5554c5c403dcSVardan Mikayelyan 	}
5555c5c403dcSVardan Mikayelyan 
5556c5c403dcSVardan Mikayelyan 	/* Enable interrupts from wake up logic */
5557f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5558c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PMUINTSEL;
5559f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5560c5c403dcSVardan Mikayelyan 	udelay(10);
5561c5c403dcSVardan Mikayelyan 
5562c5c403dcSVardan Mikayelyan 	/* Unmask host mode interrupts in GPWRDN */
5563f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5564c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_DISCONN_DET_MSK;
5565c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5566c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5567f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5568c5c403dcSVardan Mikayelyan 	udelay(10);
5569c5c403dcSVardan Mikayelyan 
5570c5c403dcSVardan Mikayelyan 	/* Enable Power Down Clamp */
5571f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5572c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNCLMP;
5573f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5574c5c403dcSVardan Mikayelyan 	udelay(10);
5575c5c403dcSVardan Mikayelyan 
5576c5c403dcSVardan Mikayelyan 	/* Switch off VDD */
5577f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5578c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNSWTCH;
5579f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5580c5c403dcSVardan Mikayelyan 
5581c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 1;
5582c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 1;
5583c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation completed\n");
5584c5c403dcSVardan Mikayelyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
5585c5c403dcSVardan Mikayelyan 	return ret;
5586c5c403dcSVardan Mikayelyan }
5587c5c403dcSVardan Mikayelyan 
5588c5c403dcSVardan Mikayelyan /*
5589c5c403dcSVardan Mikayelyan  * dwc2_host_exit_hibernation()
5590c5c403dcSVardan Mikayelyan  *
5591c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5592c5c403dcSVardan Mikayelyan  * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5593c5c403dcSVardan Mikayelyan  * @param reset: indicates whether resume is initiated by Reset.
5594c5c403dcSVardan Mikayelyan  *
5595c5c403dcSVardan Mikayelyan  * Return: non-zero if failed to enter to hibernation.
5596c5c403dcSVardan Mikayelyan  *
5597c5c403dcSVardan Mikayelyan  * This function is for exiting from Host mode hibernation by
5598c5c403dcSVardan Mikayelyan  * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
5599c5c403dcSVardan Mikayelyan  */
5600c5c403dcSVardan Mikayelyan int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
5601c5c403dcSVardan Mikayelyan 			       int reset)
5602c5c403dcSVardan Mikayelyan {
5603c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5604c5c403dcSVardan Mikayelyan 	u32 hprt0;
5605c5c403dcSVardan Mikayelyan 	int ret = 0;
5606c5c403dcSVardan Mikayelyan 	struct dwc2_gregs_backup *gr;
5607c5c403dcSVardan Mikayelyan 	struct dwc2_hregs_backup *hr;
5608c5c403dcSVardan Mikayelyan 
5609c5c403dcSVardan Mikayelyan 	gr = &hsotg->gr_backup;
5610c5c403dcSVardan Mikayelyan 	hr = &hsotg->hr_backup;
5611c5c403dcSVardan Mikayelyan 
5612c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev,
5613c5c403dcSVardan Mikayelyan 		"%s: called with rem_wakeup = %d reset = %d\n",
5614c5c403dcSVardan Mikayelyan 		__func__, rem_wakeup, reset);
5615c5c403dcSVardan Mikayelyan 
5616c5c403dcSVardan Mikayelyan 	dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
5617c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5618c5c403dcSVardan Mikayelyan 
5619c5c403dcSVardan Mikayelyan 	/*
5620c5c403dcSVardan Mikayelyan 	 * This step is not described in functional spec but if not wait for
5621c5c403dcSVardan Mikayelyan 	 * this delay, mismatch interrupts occurred because just after restore
5622c5c403dcSVardan Mikayelyan 	 * core is in Device mode(gintsts.curmode == 0)
5623c5c403dcSVardan Mikayelyan 	 */
5624c5c403dcSVardan Mikayelyan 	mdelay(100);
5625c5c403dcSVardan Mikayelyan 
5626c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5627f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5628c5c403dcSVardan Mikayelyan 
5629c5c403dcSVardan Mikayelyan 	/* De-assert Restore */
5630f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5631c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_RESTORE;
5632f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5633c5c403dcSVardan Mikayelyan 	udelay(10);
5634c5c403dcSVardan Mikayelyan 
5635c5c403dcSVardan Mikayelyan 	/* Restore GUSBCFG, HCFG */
5636f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5637f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5638c5c403dcSVardan Mikayelyan 
5639c5c403dcSVardan Mikayelyan 	/* De-assert Wakeup Logic */
5640f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5641c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_PMUACTV;
5642f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5643c5c403dcSVardan Mikayelyan 	udelay(10);
5644c5c403dcSVardan Mikayelyan 
5645c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5646c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5647c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5648c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5649f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5650c5c403dcSVardan Mikayelyan 
5651c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5652c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5653c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5654c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5655c5c403dcSVardan Mikayelyan 
5656c5c403dcSVardan Mikayelyan 	if (reset) {
5657c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RST;
5658f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5659c5c403dcSVardan Mikayelyan 
5660c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5661c5c403dcSVardan Mikayelyan 		mdelay(60);
5662c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RST;
5663f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5664c5c403dcSVardan Mikayelyan 	} else {
5665c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RES;
5666f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5667c5c403dcSVardan Mikayelyan 
5668c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5669c5c403dcSVardan Mikayelyan 		mdelay(100);
5670c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RES;
5671f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5672c5c403dcSVardan Mikayelyan 	}
5673c5c403dcSVardan Mikayelyan 	/* Clear all interrupt status */
5674f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5675c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_CONNDET;
5676c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_ENACHG;
5677c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5678f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5679c5c403dcSVardan Mikayelyan 
5680f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5681c5c403dcSVardan Mikayelyan 
5682c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5683f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5684c5c403dcSVardan Mikayelyan 
5685c5c403dcSVardan Mikayelyan 	/* Restore global registers */
5686c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_global_registers(hsotg);
5687c5c403dcSVardan Mikayelyan 	if (ret) {
5688c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore registers\n",
5689c5c403dcSVardan Mikayelyan 			__func__);
5690c5c403dcSVardan Mikayelyan 		return ret;
5691c5c403dcSVardan Mikayelyan 	}
5692c5c403dcSVardan Mikayelyan 
5693c5c403dcSVardan Mikayelyan 	/* Restore host registers */
5694c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_host_registers(hsotg);
5695c5c403dcSVardan Mikayelyan 	if (ret) {
5696c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5697c5c403dcSVardan Mikayelyan 			__func__);
5698c5c403dcSVardan Mikayelyan 		return ret;
5699c5c403dcSVardan Mikayelyan 	}
5700c5c403dcSVardan Mikayelyan 
5701c2db8d7bSArtur Petrosyan 	if (rem_wakeup) {
570222bb5cfdSArtur Petrosyan 		dwc2_hcd_rem_wakeup(hsotg);
5703c2db8d7bSArtur Petrosyan 		/*
5704c2db8d7bSArtur Petrosyan 		 * Change "port_connect_status_change" flag to re-enumerate,
5705c2db8d7bSArtur Petrosyan 		 * because after exit from hibernation port connection status
5706c2db8d7bSArtur Petrosyan 		 * is not detected.
5707c2db8d7bSArtur Petrosyan 		 */
5708c2db8d7bSArtur Petrosyan 		hsotg->flags.b.port_connect_status_change = 1;
5709c2db8d7bSArtur Petrosyan 	}
571022bb5cfdSArtur Petrosyan 
5711c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5712c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 0;
5713c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L0;
5714c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
5715c5c403dcSVardan Mikayelyan 	return ret;
5716c5c403dcSVardan Mikayelyan }
5717c846b03fSDouglas Anderson 
5718c846b03fSDouglas Anderson bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
5719c846b03fSDouglas Anderson {
5720c846b03fSDouglas Anderson 	struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
5721c846b03fSDouglas Anderson 
5722c846b03fSDouglas Anderson 	/* If the controller isn't allowed to wakeup then we can power off. */
5723c846b03fSDouglas Anderson 	if (!device_may_wakeup(dwc2->dev))
5724c846b03fSDouglas Anderson 		return true;
5725c846b03fSDouglas Anderson 
5726c846b03fSDouglas Anderson 	/*
5727c846b03fSDouglas Anderson 	 * We don't want to power off the PHY if something under the
5728c846b03fSDouglas Anderson 	 * root hub has wakeup enabled.
5729c846b03fSDouglas Anderson 	 */
5730c846b03fSDouglas Anderson 	if (usb_wakeup_enabled_descendants(root_hub))
5731c846b03fSDouglas Anderson 		return false;
5732c846b03fSDouglas Anderson 
5733c846b03fSDouglas Anderson 	/* No reason to keep the PHY powered, so allow poweroff */
5734c846b03fSDouglas Anderson 	return true;
5735c846b03fSDouglas Anderson }
57369ce9e5adSArtur Petrosyan 
57379ce9e5adSArtur Petrosyan /**
57389ce9e5adSArtur Petrosyan  * dwc2_host_enter_partial_power_down() - Put controller in partial
57399ce9e5adSArtur Petrosyan  * power down.
57409ce9e5adSArtur Petrosyan  *
57419ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
57429ce9e5adSArtur Petrosyan  *
57439ce9e5adSArtur Petrosyan  * Return: non-zero if failed to enter host partial power down.
57449ce9e5adSArtur Petrosyan  *
57459ce9e5adSArtur Petrosyan  * This function is for entering Host mode partial power down.
57469ce9e5adSArtur Petrosyan  */
57479ce9e5adSArtur Petrosyan int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg)
57489ce9e5adSArtur Petrosyan {
57499ce9e5adSArtur Petrosyan 	u32 pcgcctl;
57509ce9e5adSArtur Petrosyan 	u32 hprt0;
57519ce9e5adSArtur Petrosyan 	int ret = 0;
57529ce9e5adSArtur Petrosyan 
57539ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down started.\n");
57549ce9e5adSArtur Petrosyan 
57559ce9e5adSArtur Petrosyan 	/* Put this port in suspend mode. */
57569ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
57579ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
57589ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
57599ce9e5adSArtur Petrosyan 	udelay(5);
57609ce9e5adSArtur Petrosyan 
57619ce9e5adSArtur Petrosyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
57629ce9e5adSArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
57639ce9e5adSArtur Petrosyan 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
57649ce9e5adSArtur Petrosyan 
57659ce9e5adSArtur Petrosyan 	/* Backup all registers */
57669ce9e5adSArtur Petrosyan 	ret = dwc2_backup_global_registers(hsotg);
57679ce9e5adSArtur Petrosyan 	if (ret) {
57689ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
57699ce9e5adSArtur Petrosyan 			__func__);
57709ce9e5adSArtur Petrosyan 		return ret;
57719ce9e5adSArtur Petrosyan 	}
57729ce9e5adSArtur Petrosyan 
57739ce9e5adSArtur Petrosyan 	ret = dwc2_backup_host_registers(hsotg);
57749ce9e5adSArtur Petrosyan 	if (ret) {
57759ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
57769ce9e5adSArtur Petrosyan 			__func__);
57779ce9e5adSArtur Petrosyan 		return ret;
57789ce9e5adSArtur Petrosyan 	}
57799ce9e5adSArtur Petrosyan 
57809ce9e5adSArtur Petrosyan 	/*
57819ce9e5adSArtur Petrosyan 	 * Clear any pending interrupts since dwc2 will not be able to
57829ce9e5adSArtur Petrosyan 	 * clear them after entering partial_power_down.
57839ce9e5adSArtur Petrosyan 	 */
57849ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
57859ce9e5adSArtur Petrosyan 
57869ce9e5adSArtur Petrosyan 	/* Put the controller in low power state */
57879ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
57889ce9e5adSArtur Petrosyan 
57899ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_PWRCLMP;
57909ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57919ce9e5adSArtur Petrosyan 	udelay(5);
57929ce9e5adSArtur Petrosyan 
57939ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
57949ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57959ce9e5adSArtur Petrosyan 	udelay(5);
57969ce9e5adSArtur Petrosyan 
57979ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_STOPPCLK;
57989ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57999ce9e5adSArtur Petrosyan 
58009ce9e5adSArtur Petrosyan 	/* Set in_ppd flag to 1 as here core enters suspend. */
58019ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 1;
58029ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
58039ce9e5adSArtur Petrosyan 	hsotg->bus_suspended = true;
58049ce9e5adSArtur Petrosyan 
58059ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down completed.\n");
58069ce9e5adSArtur Petrosyan 
58079ce9e5adSArtur Petrosyan 	return ret;
58089ce9e5adSArtur Petrosyan }
58099ce9e5adSArtur Petrosyan 
58109ce9e5adSArtur Petrosyan /*
58119ce9e5adSArtur Petrosyan  * dwc2_host_exit_partial_power_down() - Exit controller from host partial
58129ce9e5adSArtur Petrosyan  * power down.
58139ce9e5adSArtur Petrosyan  *
58149ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
58159ce9e5adSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by Reset.
58169ce9e5adSArtur Petrosyan  * @restore: indicates whether need to restore the registers or not.
58179ce9e5adSArtur Petrosyan  *
58189ce9e5adSArtur Petrosyan  * Return: non-zero if failed to exit host partial power down.
58199ce9e5adSArtur Petrosyan  *
58209ce9e5adSArtur Petrosyan  * This function is for exiting from Host mode partial power down.
58219ce9e5adSArtur Petrosyan  */
58229ce9e5adSArtur Petrosyan int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg,
58239ce9e5adSArtur Petrosyan 				      int rem_wakeup, bool restore)
58249ce9e5adSArtur Petrosyan {
58259ce9e5adSArtur Petrosyan 	u32 pcgcctl;
58269ce9e5adSArtur Petrosyan 	int ret = 0;
58279ce9e5adSArtur Petrosyan 	u32 hprt0;
58289ce9e5adSArtur Petrosyan 
58299ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down started.\n");
58309ce9e5adSArtur Petrosyan 
58319ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58329ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_STOPPCLK;
58339ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58349ce9e5adSArtur Petrosyan 	udelay(5);
58359ce9e5adSArtur Petrosyan 
58369ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58379ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_PWRCLMP;
58389ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58399ce9e5adSArtur Petrosyan 	udelay(5);
58409ce9e5adSArtur Petrosyan 
58419ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58429ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
58439ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58449ce9e5adSArtur Petrosyan 
58459ce9e5adSArtur Petrosyan 	udelay(100);
58469ce9e5adSArtur Petrosyan 	if (restore) {
58479ce9e5adSArtur Petrosyan 		ret = dwc2_restore_global_registers(hsotg);
58489ce9e5adSArtur Petrosyan 		if (ret) {
58499ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore registers\n",
58509ce9e5adSArtur Petrosyan 				__func__);
58519ce9e5adSArtur Petrosyan 			return ret;
58529ce9e5adSArtur Petrosyan 		}
58539ce9e5adSArtur Petrosyan 
58549ce9e5adSArtur Petrosyan 		ret = dwc2_restore_host_registers(hsotg);
58559ce9e5adSArtur Petrosyan 		if (ret) {
58569ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore host registers\n",
58579ce9e5adSArtur Petrosyan 				__func__);
58589ce9e5adSArtur Petrosyan 			return ret;
58599ce9e5adSArtur Petrosyan 		}
58609ce9e5adSArtur Petrosyan 	}
58619ce9e5adSArtur Petrosyan 
58629ce9e5adSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
58639ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
58649ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_RES;
58659ce9e5adSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
58669ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
58679ce9e5adSArtur Petrosyan 	udelay(5);
58689ce9e5adSArtur Petrosyan 
58699ce9e5adSArtur Petrosyan 	if (!rem_wakeup) {
58709ce9e5adSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
58719ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58729ce9e5adSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
58739ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58749ce9e5adSArtur Petrosyan 
58759ce9e5adSArtur Petrosyan 		hsotg->bus_suspended = false;
58769ce9e5adSArtur Petrosyan 	} else {
58779ce9e5adSArtur Petrosyan 		/* Turn on the port power bit. */
58789ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58799ce9e5adSArtur Petrosyan 		hprt0 |= HPRT0_PWR;
58809ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58819ce9e5adSArtur Petrosyan 
58829ce9e5adSArtur Petrosyan 		/* Connect hcd. */
58839ce9e5adSArtur Petrosyan 		dwc2_hcd_connect(hsotg);
58849ce9e5adSArtur Petrosyan 
58859ce9e5adSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
58869ce9e5adSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
58879ce9e5adSArtur Petrosyan 	}
58889ce9e5adSArtur Petrosyan 
58899ce9e5adSArtur Petrosyan 	/* Set lx_state to and in_ppd to 0 as here core exits from suspend. */
58909ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 0;
58919ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L0;
58929ce9e5adSArtur Petrosyan 
58939ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n");
58949ce9e5adSArtur Petrosyan 	return ret;
58959ce9e5adSArtur Petrosyan }
589679c87c3cSArtur Petrosyan 
589779c87c3cSArtur Petrosyan /**
589879c87c3cSArtur Petrosyan  * dwc2_host_enter_clock_gating() - Put controller in clock gating.
589979c87c3cSArtur Petrosyan  *
590079c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
590179c87c3cSArtur Petrosyan  *
590279c87c3cSArtur Petrosyan  * This function is for entering Host mode clock gating.
590379c87c3cSArtur Petrosyan  */
590479c87c3cSArtur Petrosyan void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
590579c87c3cSArtur Petrosyan {
590679c87c3cSArtur Petrosyan 	u32 hprt0;
590779c87c3cSArtur Petrosyan 	u32 pcgctl;
590879c87c3cSArtur Petrosyan 
590979c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host clock gating.\n");
591079c87c3cSArtur Petrosyan 
591179c87c3cSArtur Petrosyan 	/* Put this port in suspend mode. */
591279c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
591379c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
591479c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
591579c87c3cSArtur Petrosyan 
591679c87c3cSArtur Petrosyan 	/* Set the Phy Clock bit as suspend is received. */
591779c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
591879c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_STOPPCLK;
591979c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
592079c87c3cSArtur Petrosyan 	udelay(5);
592179c87c3cSArtur Petrosyan 
592279c87c3cSArtur Petrosyan 	/* Set the Gate hclk as suspend is received. */
592379c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
592479c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_GATEHCLK;
592579c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
592679c87c3cSArtur Petrosyan 	udelay(5);
592779c87c3cSArtur Petrosyan 
592879c87c3cSArtur Petrosyan 	hsotg->bus_suspended = true;
592979c87c3cSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
593079c87c3cSArtur Petrosyan }
593179c87c3cSArtur Petrosyan 
593279c87c3cSArtur Petrosyan /**
593379c87c3cSArtur Petrosyan  * dwc2_host_exit_clock_gating() - Exit controller from clock gating.
593479c87c3cSArtur Petrosyan  *
593579c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
593679c87c3cSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by remote wakeup
593779c87c3cSArtur Petrosyan  *
593879c87c3cSArtur Petrosyan  * This function is for exiting Host mode clock gating.
593979c87c3cSArtur Petrosyan  */
594079c87c3cSArtur Petrosyan void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
594179c87c3cSArtur Petrosyan {
594279c87c3cSArtur Petrosyan 	u32 hprt0;
594379c87c3cSArtur Petrosyan 	u32 pcgctl;
594479c87c3cSArtur Petrosyan 
594579c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host clock gating.\n");
594679c87c3cSArtur Petrosyan 
594779c87c3cSArtur Petrosyan 	/* Clear the Gate hclk. */
594879c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
594979c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_GATEHCLK;
595079c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
595179c87c3cSArtur Petrosyan 	udelay(5);
595279c87c3cSArtur Petrosyan 
595379c87c3cSArtur Petrosyan 	/* Phy Clock bit. */
595479c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
595579c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_STOPPCLK;
595679c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
595779c87c3cSArtur Petrosyan 	udelay(5);
595879c87c3cSArtur Petrosyan 
595979c87c3cSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
596079c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
596179c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_RES;
596279c87c3cSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
596379c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
596479c87c3cSArtur Petrosyan 	udelay(5);
596579c87c3cSArtur Petrosyan 
596679c87c3cSArtur Petrosyan 	if (!rem_wakeup) {
596779c87c3cSArtur Petrosyan 		/* In case of port resume need to wait for 40 ms */
596879c87c3cSArtur Petrosyan 		msleep(USB_RESUME_TIMEOUT);
596979c87c3cSArtur Petrosyan 
597079c87c3cSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
597179c87c3cSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
597279c87c3cSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
597379c87c3cSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
597479c87c3cSArtur Petrosyan 
597579c87c3cSArtur Petrosyan 		hsotg->bus_suspended = false;
597679c87c3cSArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
597779c87c3cSArtur Petrosyan 	} else {
597879c87c3cSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
597979c87c3cSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
598079c87c3cSArtur Petrosyan 	}
598179c87c3cSArtur Petrosyan }
5982