xref: /linux/drivers/usb/dwc2/hcd.c (revision c363af9ce3db7e374b37e0509ccf31f8da4da404)
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:
141bea8e86cSJohn Youn 		if (hsotg->params.otg_cap ==
142b02038faSJohn Youn 				DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
143b02038faSJohn Youn 			usbcfg |= GUSBCFG_HNPCAP;
144bea8e86cSJohn Youn 		if (hsotg->params.otg_cap !=
145b02038faSJohn Youn 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
146b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
147b02038faSJohn Youn 		break;
148b02038faSJohn Youn 
149b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
150b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
151b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
152bea8e86cSJohn Youn 		if (hsotg->params.otg_cap !=
153b02038faSJohn Youn 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
154b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
155b02038faSJohn Youn 		break;
156b02038faSJohn Youn 
157b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
158b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
159b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
160b02038faSJohn Youn 	default:
161b02038faSJohn Youn 		break;
162b02038faSJohn Youn 	}
163b02038faSJohn Youn 
164f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
165b02038faSJohn Youn }
166b02038faSJohn Youn 
167531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
168531ef5ebSAmelie Delaunay {
169e0f681c2SFabrice Gasnier 	if (hsotg->vbus_supply)
170531ef5ebSAmelie Delaunay 		return regulator_enable(hsotg->vbus_supply);
171e0f681c2SFabrice Gasnier 
172e0f681c2SFabrice Gasnier 	return 0;
173531ef5ebSAmelie Delaunay }
174531ef5ebSAmelie Delaunay 
175531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
176531ef5ebSAmelie Delaunay {
177531ef5ebSAmelie Delaunay 	if (hsotg->vbus_supply)
178531ef5ebSAmelie Delaunay 		return regulator_disable(hsotg->vbus_supply);
179531ef5ebSAmelie Delaunay 
180531ef5ebSAmelie Delaunay 	return 0;
181531ef5ebSAmelie Delaunay }
182531ef5ebSAmelie Delaunay 
183b02038faSJohn Youn /**
184b02038faSJohn Youn  * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
185b02038faSJohn Youn  *
186b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
187b02038faSJohn Youn  */
188b02038faSJohn Youn static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
189b02038faSJohn Youn {
190b02038faSJohn Youn 	u32 intmsk;
191b02038faSJohn Youn 
192b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s()\n", __func__);
193b02038faSJohn Youn 
194b02038faSJohn Youn 	/* Disable all interrupts */
195f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
196f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HAINTMSK);
197b02038faSJohn Youn 
198b02038faSJohn Youn 	/* Enable the common interrupts */
199b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
200b02038faSJohn Youn 
201b02038faSJohn Youn 	/* Enable host mode interrupts without disturbing common interrupts */
202f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
203b02038faSJohn Youn 	intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
204f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
205b02038faSJohn Youn }
206b02038faSJohn Youn 
207b02038faSJohn Youn /**
208b02038faSJohn Youn  * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
209b02038faSJohn Youn  *
210b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
211b02038faSJohn Youn  */
212b02038faSJohn Youn static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
213b02038faSJohn Youn {
214f25c42b8SGevorg Sahakyan 	u32 intmsk = dwc2_readl(hsotg, GINTMSK);
215b02038faSJohn Youn 
216b02038faSJohn Youn 	/* Disable host mode interrupts without disturbing common interrupts */
217b02038faSJohn Youn 	intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
218b02038faSJohn Youn 		    GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
219f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
220b02038faSJohn Youn }
221b02038faSJohn Youn 
222b02038faSJohn Youn /*
223b02038faSJohn Youn  * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
224b02038faSJohn Youn  * For system that have a total fifo depth that is smaller than the default
225b02038faSJohn Youn  * RX + TX fifo size.
226b02038faSJohn Youn  *
227b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
228b02038faSJohn Youn  */
229b02038faSJohn Youn static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
230b02038faSJohn Youn {
231bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
232b02038faSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
233b02038faSJohn Youn 	u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
234b02038faSJohn Youn 
235b02038faSJohn Youn 	total_fifo_size = hw->total_fifo_size;
236b02038faSJohn Youn 	rxfsiz = params->host_rx_fifo_size;
237b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size;
238b02038faSJohn Youn 	ptxfsiz = params->host_perio_tx_fifo_size;
239b02038faSJohn Youn 
240b02038faSJohn Youn 	/*
241b02038faSJohn Youn 	 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
242b02038faSJohn Youn 	 * allocation with support for high bandwidth endpoints. Synopsys
243b02038faSJohn Youn 	 * defines MPS(Max Packet size) for a periodic EP=1024, and for
244b02038faSJohn Youn 	 * non-periodic as 512.
245b02038faSJohn Youn 	 */
246b02038faSJohn Youn 	if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
247b02038faSJohn Youn 		/*
248b02038faSJohn Youn 		 * For Buffer DMA mode/Scatter Gather DMA mode
249b02038faSJohn Youn 		 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
250b02038faSJohn Youn 		 * with n = number of host channel.
251b02038faSJohn Youn 		 * 2 * ((1024/4) + 2) = 516
252b02038faSJohn Youn 		 */
253b02038faSJohn Youn 		rxfsiz = 516 + hw->host_channels;
254b02038faSJohn Youn 
255b02038faSJohn Youn 		/*
256b02038faSJohn Youn 		 * min non-periodic tx fifo depth
257b02038faSJohn Youn 		 * 2 * (largest non-periodic USB packet used / 4)
258b02038faSJohn Youn 		 * 2 * (512/4) = 256
259b02038faSJohn Youn 		 */
260b02038faSJohn Youn 		nptxfsiz = 256;
261b02038faSJohn Youn 
262b02038faSJohn Youn 		/*
263b02038faSJohn Youn 		 * min periodic tx fifo depth
264b02038faSJohn Youn 		 * (largest packet size*MC)/4
265b02038faSJohn Youn 		 * (1024 * 3)/4 = 768
266b02038faSJohn Youn 		 */
267b02038faSJohn Youn 		ptxfsiz = 768;
268b02038faSJohn Youn 
269b02038faSJohn Youn 		params->host_rx_fifo_size = rxfsiz;
270b02038faSJohn Youn 		params->host_nperio_tx_fifo_size = nptxfsiz;
271b02038faSJohn Youn 		params->host_perio_tx_fifo_size = ptxfsiz;
272b02038faSJohn Youn 	}
273b02038faSJohn Youn 
274b02038faSJohn Youn 	/*
275b02038faSJohn Youn 	 * If the summation of RX, NPTX and PTX fifo sizes is still
276b02038faSJohn Youn 	 * bigger than the total_fifo_size, then we have a problem.
277b02038faSJohn Youn 	 *
278b02038faSJohn Youn 	 * We won't be able to allocate as many endpoints. Right now,
279b02038faSJohn Youn 	 * we're just printing an error message, but ideally this FIFO
280b02038faSJohn Youn 	 * allocation algorithm would be improved in the future.
281b02038faSJohn Youn 	 *
282b02038faSJohn Youn 	 * FIXME improve this FIFO allocation algorithm.
283b02038faSJohn Youn 	 */
284b02038faSJohn Youn 	if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
285b02038faSJohn Youn 		dev_err(hsotg->dev, "invalid fifo sizes\n");
286b02038faSJohn Youn }
287b02038faSJohn Youn 
288b02038faSJohn Youn static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
289b02038faSJohn Youn {
290bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
291b02038faSJohn Youn 	u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
292b02038faSJohn Youn 
293b02038faSJohn Youn 	if (!params->enable_dynamic_fifo)
294b02038faSJohn Youn 		return;
295b02038faSJohn Youn 
296b02038faSJohn Youn 	dwc2_calculate_dynamic_fifo(hsotg);
297b02038faSJohn Youn 
298b02038faSJohn Youn 	/* Rx FIFO */
299f25c42b8SGevorg Sahakyan 	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
300b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
301b02038faSJohn Youn 	grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
302b02038faSJohn Youn 	grxfsiz |= params->host_rx_fifo_size <<
303b02038faSJohn Youn 		   GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
304f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
305b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
306f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GRXFSIZ));
307b02038faSJohn Youn 
308b02038faSJohn Youn 	/* Non-periodic Tx FIFO */
309b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
310f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
311b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size <<
312b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
313b02038faSJohn Youn 	nptxfsiz |= params->host_rx_fifo_size <<
314b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
315f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
316b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
317f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
318b02038faSJohn Youn 
319b02038faSJohn Youn 	/* Periodic Tx FIFO */
320b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
321f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
322b02038faSJohn Youn 	hptxfsiz = params->host_perio_tx_fifo_size <<
323b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
324b02038faSJohn Youn 	hptxfsiz |= (params->host_rx_fifo_size +
325b02038faSJohn Youn 		     params->host_nperio_tx_fifo_size) <<
326b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
327f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
328b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
329f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
330b02038faSJohn Youn 
33195832c00SJohn Youn 	if (hsotg->params.en_multiple_tx_fifo &&
332e1f411d1SSevak Arakelyan 	    hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
333b02038faSJohn Youn 		/*
334e1f411d1SSevak Arakelyan 		 * This feature was implemented in 2.91a version
335b02038faSJohn Youn 		 * Global DFIFOCFG calculation for Host mode -
336b02038faSJohn Youn 		 * include RxFIFO, NPTXFIFO and HPTXFIFO
337b02038faSJohn Youn 		 */
338f25c42b8SGevorg Sahakyan 		dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
339b02038faSJohn Youn 		dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
340b02038faSJohn Youn 		dfifocfg |= (params->host_rx_fifo_size +
341b02038faSJohn Youn 			     params->host_nperio_tx_fifo_size +
342b02038faSJohn Youn 			     params->host_perio_tx_fifo_size) <<
343b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_SHIFT &
344b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_MASK;
345f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
346b02038faSJohn Youn 	}
347b02038faSJohn Youn }
348b02038faSJohn Youn 
349b02038faSJohn Youn /**
350b02038faSJohn Youn  * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
351b02038faSJohn Youn  * the HFIR register according to PHY type and speed
352b02038faSJohn Youn  *
353b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
354b02038faSJohn Youn  *
355b02038faSJohn Youn  * NOTE: The caller can modify the value of the HFIR register only after the
356b02038faSJohn Youn  * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
357b02038faSJohn Youn  * has been set
358b02038faSJohn Youn  */
359b02038faSJohn Youn u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
360b02038faSJohn Youn {
361b02038faSJohn Youn 	u32 usbcfg;
362b02038faSJohn Youn 	u32 hprt0;
363b02038faSJohn Youn 	int clock = 60;	/* default value */
364b02038faSJohn Youn 
365f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
366f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
367b02038faSJohn Youn 
368b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
369b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_PHYIF16))
370b02038faSJohn Youn 		clock = 60;
371b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
372b02038faSJohn Youn 	    GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
373b02038faSJohn Youn 		clock = 48;
374b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
375b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
376b02038faSJohn Youn 		clock = 30;
377b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
378b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
379b02038faSJohn Youn 		clock = 60;
380b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
381b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
382b02038faSJohn Youn 		clock = 48;
383b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
384b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
385b02038faSJohn Youn 		clock = 48;
386b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) &&
387b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
388b02038faSJohn Youn 		clock = 48;
389b02038faSJohn Youn 
390b02038faSJohn Youn 	if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
391b02038faSJohn Youn 		/* High speed case */
392b02038faSJohn Youn 		return 125 * clock - 1;
393b02038faSJohn Youn 
394b02038faSJohn Youn 	/* FS/LS case */
395b02038faSJohn Youn 	return 1000 * clock - 1;
396b02038faSJohn Youn }
397b02038faSJohn Youn 
398b02038faSJohn Youn /**
399b02038faSJohn Youn  * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
400b02038faSJohn Youn  * buffer
401b02038faSJohn Youn  *
4026fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of DWC_otg controller
403b02038faSJohn Youn  * @dest:    Destination buffer for the packet
404b02038faSJohn Youn  * @bytes:   Number of bytes to copy to the destination
405b02038faSJohn Youn  */
406b02038faSJohn Youn void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
407b02038faSJohn Youn {
408b02038faSJohn Youn 	u32 *data_buf = (u32 *)dest;
409b02038faSJohn Youn 	int word_count = (bytes + 3) / 4;
410b02038faSJohn Youn 	int i;
411b02038faSJohn Youn 
412b02038faSJohn Youn 	/*
413b02038faSJohn Youn 	 * Todo: Account for the case where dest is not dword aligned. This
414b02038faSJohn Youn 	 * requires reading data from the FIFO into a u32 temp buffer, then
415b02038faSJohn Youn 	 * moving it into the data buffer.
416b02038faSJohn Youn 	 */
417b02038faSJohn Youn 
418b02038faSJohn Youn 	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
419b02038faSJohn Youn 
420b02038faSJohn Youn 	for (i = 0; i < word_count; i++, data_buf++)
421f25c42b8SGevorg Sahakyan 		*data_buf = dwc2_readl(hsotg, HCFIFO(0));
422b02038faSJohn Youn }
423b02038faSJohn Youn 
424197ba5f4SPaul Zimmerman /**
425197ba5f4SPaul Zimmerman  * dwc2_dump_channel_info() - Prints the state of a host channel
426197ba5f4SPaul Zimmerman  *
427197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
428197ba5f4SPaul Zimmerman  * @chan:  Pointer to the channel to dump
429197ba5f4SPaul Zimmerman  *
430197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
431197ba5f4SPaul Zimmerman  *
432197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
433197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
434197ba5f4SPaul Zimmerman  */
435197ba5f4SPaul Zimmerman static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
436197ba5f4SPaul Zimmerman 				   struct dwc2_host_chan *chan)
437197ba5f4SPaul Zimmerman {
438197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
439bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
440197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
441197ba5f4SPaul Zimmerman 	u32 hcchar;
442197ba5f4SPaul Zimmerman 	u32 hcsplt;
443197ba5f4SPaul Zimmerman 	u32 hctsiz;
444197ba5f4SPaul Zimmerman 	u32 hc_dma;
445197ba5f4SPaul Zimmerman 	int i;
446197ba5f4SPaul Zimmerman 
447b02038faSJohn Youn 	if (!chan)
448197ba5f4SPaul Zimmerman 		return;
449197ba5f4SPaul Zimmerman 
450f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
451f25c42b8SGevorg Sahakyan 	hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
452f25c42b8SGevorg Sahakyan 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
453f25c42b8SGevorg Sahakyan 	hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
454197ba5f4SPaul Zimmerman 
455197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Assigned to channel %p:\n", chan);
456197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hcchar 0x%08x, hcsplt 0x%08x\n",
457197ba5f4SPaul Zimmerman 		hcchar, hcsplt);
458197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hctsiz 0x%08x, hc_dma 0x%08x\n",
459197ba5f4SPaul Zimmerman 		hctsiz, hc_dma);
460197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
461197ba5f4SPaul Zimmerman 		chan->dev_addr, chan->ep_num, chan->ep_is_in);
462197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
463197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
464197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    data_pid_start: %d\n", chan->data_pid_start);
465197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_started: %d\n", chan->xfer_started);
466197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
467197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
468197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
469197ba5f4SPaul Zimmerman 		(unsigned long)chan->xfer_dma);
470197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
471197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
472197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP inactive sched:\n");
473197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
474197ba5f4SPaul Zimmerman 			    qh_list_entry)
475197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
47638d2b5fbSDouglas Anderson 	dev_dbg(hsotg->dev, "  NP waiting sched:\n");
47738d2b5fbSDouglas Anderson 	list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
47838d2b5fbSDouglas Anderson 			    qh_list_entry)
47938d2b5fbSDouglas Anderson 		dev_dbg(hsotg->dev, "    %p\n", qh);
480197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP active sched:\n");
481197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
482197ba5f4SPaul Zimmerman 			    qh_list_entry)
483197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
484197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Channels:\n");
485197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
486197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
487197ba5f4SPaul Zimmerman 
488197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %2d: %p\n", i, chan);
489197ba5f4SPaul Zimmerman 	}
490197ba5f4SPaul Zimmerman #endif /* VERBOSE_DEBUG */
491197ba5f4SPaul Zimmerman }
492197ba5f4SPaul Zimmerman 
4934411bebaSRazmik Karapetyan static int _dwc2_hcd_start(struct usb_hcd *hcd);
4944411bebaSRazmik Karapetyan 
4954411bebaSRazmik Karapetyan static void dwc2_host_start(struct dwc2_hsotg *hsotg)
4964411bebaSRazmik Karapetyan {
4974411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
4984411bebaSRazmik Karapetyan 
4994411bebaSRazmik Karapetyan 	hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
5004411bebaSRazmik Karapetyan 	_dwc2_hcd_start(hcd);
5014411bebaSRazmik Karapetyan }
5024411bebaSRazmik Karapetyan 
5034411bebaSRazmik Karapetyan static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
5044411bebaSRazmik Karapetyan {
5054411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
5064411bebaSRazmik Karapetyan 
5074411bebaSRazmik Karapetyan 	hcd->self.is_b_host = 0;
5084411bebaSRazmik Karapetyan }
5094411bebaSRazmik Karapetyan 
5104411bebaSRazmik Karapetyan static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
5114411bebaSRazmik Karapetyan 			       int *hub_addr, int *hub_port)
5124411bebaSRazmik Karapetyan {
5134411bebaSRazmik Karapetyan 	struct urb *urb = context;
5144411bebaSRazmik Karapetyan 
5154411bebaSRazmik Karapetyan 	if (urb->dev->tt)
5164411bebaSRazmik Karapetyan 		*hub_addr = urb->dev->tt->hub->devnum;
5174411bebaSRazmik Karapetyan 	else
5184411bebaSRazmik Karapetyan 		*hub_addr = 0;
5194411bebaSRazmik Karapetyan 	*hub_port = urb->dev->ttport;
5204411bebaSRazmik Karapetyan }
5214411bebaSRazmik Karapetyan 
522197ba5f4SPaul Zimmerman /*
523b02038faSJohn Youn  * =========================================================================
524b02038faSJohn Youn  *  Low Level Host Channel Access Functions
525b02038faSJohn Youn  * =========================================================================
526b02038faSJohn Youn  */
527b02038faSJohn Youn 
528b02038faSJohn Youn static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
529b02038faSJohn Youn 				      struct dwc2_host_chan *chan)
530b02038faSJohn Youn {
531b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
532b02038faSJohn Youn 
533b02038faSJohn Youn 	switch (chan->ep_type) {
534b02038faSJohn Youn 	case USB_ENDPOINT_XFER_CONTROL:
535b02038faSJohn Youn 	case USB_ENDPOINT_XFER_BULK:
536b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "control/bulk\n");
537b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
538b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
539b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
540b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
541b02038faSJohn Youn 		if (chan->ep_is_in) {
542b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
543b02038faSJohn Youn 		} else {
544b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
545b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NYET;
546b02038faSJohn Youn 			if (chan->do_ping)
547b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
548b02038faSJohn Youn 		}
549b02038faSJohn Youn 
550b02038faSJohn Youn 		if (chan->do_split) {
551b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
552b02038faSJohn Youn 			if (chan->complete_split)
553b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
554b02038faSJohn Youn 			else
555b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
556b02038faSJohn Youn 		}
557b02038faSJohn Youn 
558b02038faSJohn Youn 		if (chan->error_state)
559b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
560b02038faSJohn Youn 		break;
561b02038faSJohn Youn 
562b02038faSJohn Youn 	case USB_ENDPOINT_XFER_INT:
563b02038faSJohn Youn 		if (dbg_perio())
564b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "intr\n");
565b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
566b02038faSJohn Youn 		hcintmsk |= HCINTMSK_NAK;
567b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
568b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
569b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
570b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
571b02038faSJohn Youn 
572b02038faSJohn Youn 		if (chan->ep_is_in)
573b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
574b02038faSJohn Youn 		if (chan->error_state)
575b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
576b02038faSJohn Youn 		if (chan->do_split) {
577b02038faSJohn Youn 			if (chan->complete_split)
578b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
579b02038faSJohn Youn 			else
580b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
581b02038faSJohn Youn 		}
582b02038faSJohn Youn 		break;
583b02038faSJohn Youn 
584b02038faSJohn Youn 	case USB_ENDPOINT_XFER_ISOC:
585b02038faSJohn Youn 		if (dbg_perio())
586b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "isoc\n");
587b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
588b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
589b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
590b02038faSJohn Youn 
591b02038faSJohn Youn 		if (chan->ep_is_in) {
592b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XACTERR;
593b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
594b02038faSJohn Youn 		}
595b02038faSJohn Youn 		break;
596b02038faSJohn Youn 	default:
597b02038faSJohn Youn 		dev_err(hsotg->dev, "## Unknown EP type ##\n");
598b02038faSJohn Youn 		break;
599b02038faSJohn Youn 	}
600b02038faSJohn Youn 
601f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
602b02038faSJohn Youn 	if (dbg_hc(chan))
603b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
604b02038faSJohn Youn }
605b02038faSJohn Youn 
606b02038faSJohn Youn static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
607b02038faSJohn Youn 				    struct dwc2_host_chan *chan)
608b02038faSJohn Youn {
609b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
610b02038faSJohn Youn 
611b02038faSJohn Youn 	/*
612b02038faSJohn Youn 	 * For Descriptor DMA mode core halts the channel on AHB error.
613b02038faSJohn Youn 	 * Interrupt is not required.
614b02038faSJohn Youn 	 */
61595832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
616b02038faSJohn Youn 		if (dbg_hc(chan))
617b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
618b02038faSJohn Youn 		hcintmsk |= HCINTMSK_AHBERR;
619b02038faSJohn Youn 	} else {
620b02038faSJohn Youn 		if (dbg_hc(chan))
621b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA enabled\n");
622b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
623b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XFERCOMPL;
624b02038faSJohn Youn 	}
625b02038faSJohn Youn 
626b02038faSJohn Youn 	if (chan->error_state && !chan->do_split &&
627b02038faSJohn Youn 	    chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
628b02038faSJohn Youn 		if (dbg_hc(chan))
629b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "setting ACK\n");
630b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
631b02038faSJohn Youn 		if (chan->ep_is_in) {
632b02038faSJohn Youn 			hcintmsk |= HCINTMSK_DATATGLERR;
633b02038faSJohn Youn 			if (chan->ep_type != USB_ENDPOINT_XFER_INT)
634b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NAK;
635b02038faSJohn Youn 		}
636b02038faSJohn Youn 	}
637b02038faSJohn Youn 
638f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
639b02038faSJohn Youn 	if (dbg_hc(chan))
640b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
641b02038faSJohn Youn }
642b02038faSJohn Youn 
643b02038faSJohn Youn static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
644b02038faSJohn Youn 				struct dwc2_host_chan *chan)
645b02038faSJohn Youn {
646b02038faSJohn Youn 	u32 intmsk;
647b02038faSJohn Youn 
64895832c00SJohn Youn 	if (hsotg->params.host_dma) {
649b02038faSJohn Youn 		if (dbg_hc(chan))
650b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
651b02038faSJohn Youn 		dwc2_hc_enable_dma_ints(hsotg, chan);
652b02038faSJohn Youn 	} else {
653b02038faSJohn Youn 		if (dbg_hc(chan))
654b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA disabled\n");
655b02038faSJohn Youn 		dwc2_hc_enable_slave_ints(hsotg, chan);
656b02038faSJohn Youn 	}
657b02038faSJohn Youn 
658b02038faSJohn Youn 	/* Enable the top level host channel interrupt */
659f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, HAINTMSK);
660b02038faSJohn Youn 	intmsk |= 1 << chan->hc_num;
661f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, HAINTMSK);
662b02038faSJohn Youn 	if (dbg_hc(chan))
663b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
664b02038faSJohn Youn 
665b02038faSJohn Youn 	/* Make sure host channel interrupts are enabled */
666f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
667b02038faSJohn Youn 	intmsk |= GINTSTS_HCHINT;
668f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
669b02038faSJohn Youn 	if (dbg_hc(chan))
670b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
671b02038faSJohn Youn }
672b02038faSJohn Youn 
673b02038faSJohn Youn /**
674b02038faSJohn Youn  * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
675b02038faSJohn Youn  * a specific endpoint
676b02038faSJohn Youn  *
677b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
678b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
679b02038faSJohn Youn  *
680b02038faSJohn Youn  * The HCCHARn register is set up with the characteristics specified in chan.
681b02038faSJohn Youn  * Host channel interrupts that may need to be serviced while this transfer is
682b02038faSJohn Youn  * in progress are enabled.
683b02038faSJohn Youn  */
684b02038faSJohn Youn static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
685b02038faSJohn Youn {
686b02038faSJohn Youn 	u8 hc_num = chan->hc_num;
687b02038faSJohn Youn 	u32 hcintmsk;
688b02038faSJohn Youn 	u32 hcchar;
689b02038faSJohn Youn 	u32 hcsplt = 0;
690b02038faSJohn Youn 
691b02038faSJohn Youn 	if (dbg_hc(chan))
692b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
693b02038faSJohn Youn 
694b02038faSJohn Youn 	/* Clear old interrupt conditions for this host channel */
695b02038faSJohn Youn 	hcintmsk = 0xffffffff;
696b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
697f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
698b02038faSJohn Youn 
699b02038faSJohn Youn 	/* Enable channel interrupts required for this transfer */
700b02038faSJohn Youn 	dwc2_hc_enable_ints(hsotg, chan);
701b02038faSJohn Youn 
702b02038faSJohn Youn 	/*
703b02038faSJohn Youn 	 * Program the HCCHARn register with the endpoint characteristics for
704b02038faSJohn Youn 	 * the current transfer
705b02038faSJohn Youn 	 */
706b02038faSJohn Youn 	hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
707b02038faSJohn Youn 	hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
708b02038faSJohn Youn 	if (chan->ep_is_in)
709b02038faSJohn Youn 		hcchar |= HCCHAR_EPDIR;
710b02038faSJohn Youn 	if (chan->speed == USB_SPEED_LOW)
711b02038faSJohn Youn 		hcchar |= HCCHAR_LSPDDEV;
712b02038faSJohn Youn 	hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
713b02038faSJohn Youn 	hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
714f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
715b02038faSJohn Youn 	if (dbg_hc(chan)) {
716b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
717b02038faSJohn Youn 			 hc_num, hcchar);
718b02038faSJohn Youn 
719b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n",
720b02038faSJohn Youn 			 __func__, hc_num);
721b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Dev Addr: %d\n",
722b02038faSJohn Youn 			 chan->dev_addr);
723b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Num: %d\n",
724b02038faSJohn Youn 			 chan->ep_num);
725b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is In: %d\n",
726b02038faSJohn Youn 			 chan->ep_is_in);
727b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is Low Speed: %d\n",
728b02038faSJohn Youn 			 chan->speed == USB_SPEED_LOW);
729b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Type: %d\n",
730b02038faSJohn Youn 			 chan->ep_type);
731b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Max Pkt: %d\n",
732b02038faSJohn Youn 			 chan->max_packet);
733b02038faSJohn Youn 	}
734b02038faSJohn Youn 
735b02038faSJohn Youn 	/* Program the HCSPLT register for SPLITs */
736b02038faSJohn Youn 	if (chan->do_split) {
737b02038faSJohn Youn 		if (dbg_hc(chan))
738b02038faSJohn Youn 			dev_vdbg(hsotg->dev,
739b02038faSJohn Youn 				 "Programming HC %d with split --> %s\n",
740b02038faSJohn Youn 				 hc_num,
741b02038faSJohn Youn 				 chan->complete_split ? "CSPLIT" : "SSPLIT");
742b02038faSJohn Youn 		if (chan->complete_split)
743b02038faSJohn Youn 			hcsplt |= HCSPLT_COMPSPLT;
744b02038faSJohn Youn 		hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
745b02038faSJohn Youn 			  HCSPLT_XACTPOS_MASK;
746b02038faSJohn Youn 		hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
747b02038faSJohn Youn 			  HCSPLT_HUBADDR_MASK;
748b02038faSJohn Youn 		hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
749b02038faSJohn Youn 			  HCSPLT_PRTADDR_MASK;
750b02038faSJohn Youn 		if (dbg_hc(chan)) {
751b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  comp split %d\n",
752b02038faSJohn Youn 				 chan->complete_split);
753b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xact pos %d\n",
754b02038faSJohn Youn 				 chan->xact_pos);
755b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub addr %d\n",
756b02038faSJohn Youn 				 chan->hub_addr);
757b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub port %d\n",
758b02038faSJohn Youn 				 chan->hub_port);
759b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  is_in %d\n",
760b02038faSJohn Youn 				 chan->ep_is_in);
761b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  Max Pkt %d\n",
762b02038faSJohn Youn 				 chan->max_packet);
763b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xferlen %d\n",
764b02038faSJohn Youn 				 chan->xfer_len);
765b02038faSJohn Youn 		}
766b02038faSJohn Youn 	}
767b02038faSJohn Youn 
768f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
769b02038faSJohn Youn }
770b02038faSJohn Youn 
771b02038faSJohn Youn /**
772b02038faSJohn Youn  * dwc2_hc_halt() - Attempts to halt a host channel
773b02038faSJohn Youn  *
774b02038faSJohn Youn  * @hsotg:       Controller register interface
775b02038faSJohn Youn  * @chan:        Host channel to halt
776b02038faSJohn Youn  * @halt_status: Reason for halting the channel
777b02038faSJohn Youn  *
778b02038faSJohn Youn  * This function should only be called in Slave mode or to abort a transfer in
779b02038faSJohn Youn  * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
780b02038faSJohn Youn  * controller halts the channel when the transfer is complete or a condition
781b02038faSJohn Youn  * occurs that requires application intervention.
782b02038faSJohn Youn  *
783b02038faSJohn Youn  * In slave mode, checks for a free request queue entry, then sets the Channel
784b02038faSJohn Youn  * Enable and Channel Disable bits of the Host Channel Characteristics
785b02038faSJohn Youn  * register of the specified channel to intiate the halt. If there is no free
786b02038faSJohn Youn  * request queue entry, sets only the Channel Disable bit of the HCCHARn
787b02038faSJohn Youn  * register to flush requests for this channel. In the latter case, sets a
788b02038faSJohn Youn  * flag to indicate that the host channel needs to be halted when a request
789b02038faSJohn Youn  * queue slot is open.
790b02038faSJohn Youn  *
791b02038faSJohn Youn  * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
792b02038faSJohn Youn  * HCCHARn register. The controller ensures there is space in the request
793b02038faSJohn Youn  * queue before submitting the halt request.
794b02038faSJohn Youn  *
795b02038faSJohn Youn  * Some time may elapse before the core flushes any posted requests for this
796b02038faSJohn Youn  * host channel and halts. The Channel Halted interrupt handler completes the
797b02038faSJohn Youn  * deactivation of the host channel.
798b02038faSJohn Youn  */
799b02038faSJohn Youn void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
800b02038faSJohn Youn 		  enum dwc2_halt_status halt_status)
801b02038faSJohn Youn {
802b02038faSJohn Youn 	u32 nptxsts, hptxsts, hcchar;
803b02038faSJohn Youn 
804b02038faSJohn Youn 	if (dbg_hc(chan))
805b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
806a82c7abdSMinas Harutyunyan 
807a82c7abdSMinas Harutyunyan 	/*
808a82c7abdSMinas Harutyunyan 	 * In buffer DMA or external DMA mode channel can't be halted
809a82c7abdSMinas Harutyunyan 	 * for non-split periodic channels. At the end of the next
810a82c7abdSMinas Harutyunyan 	 * uframe/frame (in the worst case), the core generates a channel
811a82c7abdSMinas Harutyunyan 	 * halted and disables the channel automatically.
812a82c7abdSMinas Harutyunyan 	 */
813a82c7abdSMinas Harutyunyan 	if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
814a82c7abdSMinas Harutyunyan 	    hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
815a82c7abdSMinas Harutyunyan 		if (!chan->do_split &&
816a82c7abdSMinas Harutyunyan 		    (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
817a82c7abdSMinas Harutyunyan 		     chan->ep_type == USB_ENDPOINT_XFER_INT)) {
818a82c7abdSMinas Harutyunyan 			dev_err(hsotg->dev, "%s() Channel can't be halted\n",
819a82c7abdSMinas Harutyunyan 				__func__);
820a82c7abdSMinas Harutyunyan 			return;
821a82c7abdSMinas Harutyunyan 		}
822a82c7abdSMinas Harutyunyan 	}
823a82c7abdSMinas Harutyunyan 
824b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
825b02038faSJohn Youn 		dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
826b02038faSJohn Youn 
827b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
828b02038faSJohn Youn 	    halt_status == DWC2_HC_XFER_AHB_ERR) {
829b02038faSJohn Youn 		/*
830b02038faSJohn Youn 		 * Disable all channel interrupts except Ch Halted. The QTD
831b02038faSJohn Youn 		 * and QH state associated with this transfer has been cleared
832b02038faSJohn Youn 		 * (in the case of URB_DEQUEUE), so the channel needs to be
833b02038faSJohn Youn 		 * shut down carefully to prevent crashes.
834b02038faSJohn Youn 		 */
835b02038faSJohn Youn 		u32 hcintmsk = HCINTMSK_CHHLTD;
836b02038faSJohn Youn 
837b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "dequeue/error\n");
838f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
839b02038faSJohn Youn 
840b02038faSJohn Youn 		/*
841b02038faSJohn Youn 		 * Make sure no other interrupts besides halt are currently
842b02038faSJohn Youn 		 * pending. Handling another interrupt could cause a crash due
843b02038faSJohn Youn 		 * to the QTD and QH state.
844b02038faSJohn Youn 		 */
845f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
846b02038faSJohn Youn 
847b02038faSJohn Youn 		/*
848b02038faSJohn Youn 		 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
849b02038faSJohn Youn 		 * even if the channel was already halted for some other
850b02038faSJohn Youn 		 * reason
851b02038faSJohn Youn 		 */
852b02038faSJohn Youn 		chan->halt_status = halt_status;
853b02038faSJohn Youn 
854f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
855b02038faSJohn Youn 		if (!(hcchar & HCCHAR_CHENA)) {
856b02038faSJohn Youn 			/*
857b02038faSJohn Youn 			 * The channel is either already halted or it hasn't
858b02038faSJohn Youn 			 * started yet. In DMA mode, the transfer may halt if
859b02038faSJohn Youn 			 * it finishes normally or a condition occurs that
860b02038faSJohn Youn 			 * requires driver intervention. Don't want to halt
861b02038faSJohn Youn 			 * the channel again. In either Slave or DMA mode,
862b02038faSJohn Youn 			 * it's possible that the transfer has been assigned
863b02038faSJohn Youn 			 * to a channel, but not started yet when an URB is
864b02038faSJohn Youn 			 * dequeued. Don't want to halt a channel that hasn't
865b02038faSJohn Youn 			 * started yet.
866b02038faSJohn Youn 			 */
867b02038faSJohn Youn 			return;
868b02038faSJohn Youn 		}
869b02038faSJohn Youn 	}
870b02038faSJohn Youn 	if (chan->halt_pending) {
871b02038faSJohn Youn 		/*
872b02038faSJohn Youn 		 * A halt has already been issued for this channel. This might
873b02038faSJohn Youn 		 * happen when a transfer is aborted by a higher level in
874b02038faSJohn Youn 		 * the stack.
875b02038faSJohn Youn 		 */
876b02038faSJohn Youn 		dev_vdbg(hsotg->dev,
877b02038faSJohn Youn 			 "*** %s: Channel %d, chan->halt_pending already set ***\n",
878b02038faSJohn Youn 			 __func__, chan->hc_num);
879b02038faSJohn Youn 		return;
880b02038faSJohn Youn 	}
881b02038faSJohn Youn 
882f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
883b02038faSJohn Youn 
884b02038faSJohn Youn 	/* No need to set the bit in DDMA for disabling the channel */
885b02038faSJohn Youn 	/* TODO check it everywhere channel is disabled */
88695832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
887b02038faSJohn Youn 		if (dbg_hc(chan))
888b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
889b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
890b02038faSJohn Youn 	} else {
891b02038faSJohn Youn 		if (dbg_hc(chan))
892b02038faSJohn Youn 			dev_dbg(hsotg->dev, "desc DMA enabled\n");
893b02038faSJohn Youn 	}
894b02038faSJohn Youn 	hcchar |= HCCHAR_CHDIS;
895b02038faSJohn Youn 
89695832c00SJohn Youn 	if (!hsotg->params.host_dma) {
897b02038faSJohn Youn 		if (dbg_hc(chan))
898b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA not enabled\n");
899b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
900b02038faSJohn Youn 
901b02038faSJohn Youn 		/* Check for space in the request queue to issue the halt */
902b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
903b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
904b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "control/bulk\n");
905f25c42b8SGevorg Sahakyan 			nptxsts = dwc2_readl(hsotg, GNPTXSTS);
906b02038faSJohn Youn 			if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
907b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "Disabling channel\n");
908b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
909b02038faSJohn Youn 			}
910b02038faSJohn Youn 		} else {
911b02038faSJohn Youn 			if (dbg_perio())
912b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "isoc/intr\n");
913f25c42b8SGevorg Sahakyan 			hptxsts = dwc2_readl(hsotg, HPTXSTS);
914b02038faSJohn Youn 			if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
915b02038faSJohn Youn 			    hsotg->queuing_high_bandwidth) {
916b02038faSJohn Youn 				if (dbg_perio())
917b02038faSJohn Youn 					dev_vdbg(hsotg->dev, "Disabling channel\n");
918b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
919b02038faSJohn Youn 			}
920b02038faSJohn Youn 		}
921b02038faSJohn Youn 	} else {
922b02038faSJohn Youn 		if (dbg_hc(chan))
923b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
924b02038faSJohn Youn 	}
925b02038faSJohn Youn 
926f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
927b02038faSJohn Youn 	chan->halt_status = halt_status;
928b02038faSJohn Youn 
929b02038faSJohn Youn 	if (hcchar & HCCHAR_CHENA) {
930b02038faSJohn Youn 		if (dbg_hc(chan))
931b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel enabled\n");
932b02038faSJohn Youn 		chan->halt_pending = 1;
933b02038faSJohn Youn 		chan->halt_on_queue = 0;
934b02038faSJohn Youn 	} else {
935b02038faSJohn Youn 		if (dbg_hc(chan))
936b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel disabled\n");
937b02038faSJohn Youn 		chan->halt_on_queue = 1;
938b02038faSJohn Youn 	}
939b02038faSJohn Youn 
940b02038faSJohn Youn 	if (dbg_hc(chan)) {
941b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
942b02038faSJohn Youn 			 chan->hc_num);
943b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 hcchar: 0x%08x\n",
944b02038faSJohn Youn 			 hcchar);
945b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_pending: %d\n",
946b02038faSJohn Youn 			 chan->halt_pending);
947b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_on_queue: %d\n",
948b02038faSJohn Youn 			 chan->halt_on_queue);
949b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_status: %d\n",
950b02038faSJohn Youn 			 chan->halt_status);
951b02038faSJohn Youn 	}
952b02038faSJohn Youn }
953b02038faSJohn Youn 
954b02038faSJohn Youn /**
955b02038faSJohn Youn  * dwc2_hc_cleanup() - Clears the transfer state for a host channel
956b02038faSJohn Youn  *
957b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
958b02038faSJohn Youn  * @chan:  Identifies the host channel to clean up
959b02038faSJohn Youn  *
960b02038faSJohn Youn  * This function is normally called after a transfer is done and the host
961b02038faSJohn Youn  * channel is being released
962b02038faSJohn Youn  */
963b02038faSJohn Youn void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
964b02038faSJohn Youn {
965b02038faSJohn Youn 	u32 hcintmsk;
966b02038faSJohn Youn 
967b02038faSJohn Youn 	chan->xfer_started = 0;
968b02038faSJohn Youn 
969b02038faSJohn Youn 	list_del_init(&chan->split_order_list_entry);
970b02038faSJohn Youn 
971b02038faSJohn Youn 	/*
972b02038faSJohn Youn 	 * Clear channel interrupt enables and any unhandled channel interrupt
973b02038faSJohn Youn 	 * conditions
974b02038faSJohn Youn 	 */
975f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
976b02038faSJohn Youn 	hcintmsk = 0xffffffff;
977b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
978f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
979b02038faSJohn Youn }
980b02038faSJohn Youn 
981b02038faSJohn Youn /**
982b02038faSJohn Youn  * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
983b02038faSJohn Youn  * which frame a periodic transfer should occur
984b02038faSJohn Youn  *
985b02038faSJohn Youn  * @hsotg:  Programming view of DWC_otg controller
986b02038faSJohn Youn  * @chan:   Identifies the host channel to set up and its properties
987b02038faSJohn Youn  * @hcchar: Current value of the HCCHAR register for the specified host channel
988b02038faSJohn Youn  *
989b02038faSJohn Youn  * This function has no effect on non-periodic transfers
990b02038faSJohn Youn  */
991b02038faSJohn Youn static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
992b02038faSJohn Youn 				       struct dwc2_host_chan *chan, u32 *hcchar)
993b02038faSJohn Youn {
994b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
995b02038faSJohn Youn 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
996b02038faSJohn Youn 		int host_speed;
997b02038faSJohn Youn 		int xfer_ns;
998b02038faSJohn Youn 		int xfer_us;
999b02038faSJohn Youn 		int bytes_in_fifo;
1000b02038faSJohn Youn 		u16 fifo_space;
1001b02038faSJohn Youn 		u16 frame_number;
1002b02038faSJohn Youn 		u16 wire_frame;
1003b02038faSJohn Youn 
1004b02038faSJohn Youn 		/*
1005b02038faSJohn Youn 		 * Try to figure out if we're an even or odd frame. If we set
1006b02038faSJohn Youn 		 * even and the current frame number is even the the transfer
1007b02038faSJohn Youn 		 * will happen immediately.  Similar if both are odd. If one is
1008b02038faSJohn Youn 		 * even and the other is odd then the transfer will happen when
1009b02038faSJohn Youn 		 * the frame number ticks.
1010b02038faSJohn Youn 		 *
1011b02038faSJohn Youn 		 * There's a bit of a balancing act to get this right.
1012b02038faSJohn Youn 		 * Sometimes we may want to send data in the current frame (AK
1013b02038faSJohn Youn 		 * right away).  We might want to do this if the frame number
1014b02038faSJohn Youn 		 * _just_ ticked, but we might also want to do this in order
1015b02038faSJohn Youn 		 * to continue a split transaction that happened late in a
1016b02038faSJohn Youn 		 * microframe (so we didn't know to queue the next transfer
1017b02038faSJohn Youn 		 * until the frame number had ticked).  The problem is that we
1018b02038faSJohn Youn 		 * need a lot of knowledge to know if there's actually still
1019b02038faSJohn Youn 		 * time to send things or if it would be better to wait until
1020b02038faSJohn Youn 		 * the next frame.
1021b02038faSJohn Youn 		 *
1022b02038faSJohn Youn 		 * We can look at how much time is left in the current frame
1023b02038faSJohn Youn 		 * and make a guess about whether we'll have time to transfer.
1024b02038faSJohn Youn 		 * We'll do that.
1025b02038faSJohn Youn 		 */
1026b02038faSJohn Youn 
1027b02038faSJohn Youn 		/* Get speed host is running at */
1028b02038faSJohn Youn 		host_speed = (chan->speed != USB_SPEED_HIGH &&
1029b02038faSJohn Youn 			      !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1030b02038faSJohn Youn 
1031b02038faSJohn Youn 		/* See how many bytes are in the periodic FIFO right now */
1032f25c42b8SGevorg Sahakyan 		fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
1033b02038faSJohn Youn 			      TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1034b02038faSJohn Youn 		bytes_in_fifo = sizeof(u32) *
1035bea8e86cSJohn Youn 				(hsotg->params.host_perio_tx_fifo_size -
1036b02038faSJohn Youn 				 fifo_space);
1037b02038faSJohn Youn 
1038b02038faSJohn Youn 		/*
1039b02038faSJohn Youn 		 * Roughly estimate bus time for everything in the periodic
1040b02038faSJohn Youn 		 * queue + our new transfer.  This is "rough" because we're
1041b02038faSJohn Youn 		 * using a function that makes takes into account IN/OUT
1042b02038faSJohn Youn 		 * and INT/ISO and we're just slamming in one value for all
1043b02038faSJohn Youn 		 * transfers.  This should be an over-estimate and that should
1044b02038faSJohn Youn 		 * be OK, but we can probably tighten it.
1045b02038faSJohn Youn 		 */
1046b02038faSJohn Youn 		xfer_ns = usb_calc_bus_time(host_speed, false, false,
1047b02038faSJohn Youn 					    chan->xfer_len + bytes_in_fifo);
1048b02038faSJohn Youn 		xfer_us = NS_TO_US(xfer_ns);
1049b02038faSJohn Youn 
1050b02038faSJohn Youn 		/* See what frame number we'll be at by the time we finish */
1051b02038faSJohn Youn 		frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1052b02038faSJohn Youn 
1053b02038faSJohn Youn 		/* This is when we were scheduled to be on the wire */
1054b02038faSJohn Youn 		wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1055b02038faSJohn Youn 
1056b02038faSJohn Youn 		/*
1057b02038faSJohn Youn 		 * If we'd finish _after_ the frame we're scheduled in then
1058b02038faSJohn Youn 		 * it's hopeless.  Just schedule right away and hope for the
1059b02038faSJohn Youn 		 * best.  Note that it _might_ be wise to call back into the
1060b02038faSJohn Youn 		 * scheduler to pick a better frame, but this is better than
1061b02038faSJohn Youn 		 * nothing.
1062b02038faSJohn Youn 		 */
1063b02038faSJohn Youn 		if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1064b02038faSJohn Youn 			dwc2_sch_vdbg(hsotg,
1065b02038faSJohn Youn 				      "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1066b02038faSJohn Youn 				      chan->qh, wire_frame, frame_number,
1067b02038faSJohn Youn 				      dwc2_frame_num_dec(frame_number,
1068b02038faSJohn Youn 							 wire_frame));
1069b02038faSJohn Youn 			wire_frame = frame_number;
1070b02038faSJohn Youn 
1071b02038faSJohn Youn 			/*
1072b02038faSJohn Youn 			 * We picked a different frame number; communicate this
1073b02038faSJohn Youn 			 * back to the scheduler so it doesn't try to schedule
1074b02038faSJohn Youn 			 * another in the same frame.
1075b02038faSJohn Youn 			 *
1076b02038faSJohn Youn 			 * Remember that next_active_frame is 1 before the wire
1077b02038faSJohn Youn 			 * frame.
1078b02038faSJohn Youn 			 */
1079b02038faSJohn Youn 			chan->qh->next_active_frame =
1080b02038faSJohn Youn 				dwc2_frame_num_dec(frame_number, 1);
1081b02038faSJohn Youn 		}
1082b02038faSJohn Youn 
1083b02038faSJohn Youn 		if (wire_frame & 1)
1084b02038faSJohn Youn 			*hcchar |= HCCHAR_ODDFRM;
1085b02038faSJohn Youn 		else
1086b02038faSJohn Youn 			*hcchar &= ~HCCHAR_ODDFRM;
1087b02038faSJohn Youn 	}
1088b02038faSJohn Youn }
1089b02038faSJohn Youn 
1090b02038faSJohn Youn static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1091b02038faSJohn Youn {
1092b02038faSJohn Youn 	/* Set up the initial PID for the transfer */
1093b02038faSJohn Youn 	if (chan->speed == USB_SPEED_HIGH) {
1094b02038faSJohn Youn 		if (chan->ep_is_in) {
1095b02038faSJohn Youn 			if (chan->multi_count == 1)
1096b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1097b02038faSJohn Youn 			else if (chan->multi_count == 2)
1098b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA1;
1099b02038faSJohn Youn 			else
1100b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA2;
1101b02038faSJohn Youn 		} else {
1102b02038faSJohn Youn 			if (chan->multi_count == 1)
1103b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1104b02038faSJohn Youn 			else
1105b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_MDATA;
1106b02038faSJohn Youn 		}
1107b02038faSJohn Youn 	} else {
1108b02038faSJohn Youn 		chan->data_pid_start = DWC2_HC_PID_DATA0;
1109b02038faSJohn Youn 	}
1110b02038faSJohn Youn }
1111b02038faSJohn Youn 
1112b02038faSJohn Youn /**
1113b02038faSJohn Youn  * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1114b02038faSJohn Youn  * the Host Channel
1115b02038faSJohn Youn  *
1116b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1117b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1118b02038faSJohn Youn  *
1119b02038faSJohn Youn  * This function should only be called in Slave mode. For a channel associated
1120b02038faSJohn Youn  * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1121b02038faSJohn Youn  * associated with a periodic EP, the periodic Tx FIFO is written.
1122b02038faSJohn Youn  *
1123b02038faSJohn Youn  * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1124b02038faSJohn Youn  * the number of bytes written to the Tx FIFO.
1125b02038faSJohn Youn  */
1126b02038faSJohn Youn static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1127b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1128b02038faSJohn Youn {
1129b02038faSJohn Youn 	u32 i;
1130b02038faSJohn Youn 	u32 remaining_count;
1131b02038faSJohn Youn 	u32 byte_count;
1132b02038faSJohn Youn 	u32 dword_count;
1133b02038faSJohn Youn 	u32 *data_buf = (u32 *)chan->xfer_buf;
1134b02038faSJohn Youn 
1135b02038faSJohn Youn 	if (dbg_hc(chan))
1136b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1137b02038faSJohn Youn 
1138b02038faSJohn Youn 	remaining_count = chan->xfer_len - chan->xfer_count;
1139b02038faSJohn Youn 	if (remaining_count > chan->max_packet)
1140b02038faSJohn Youn 		byte_count = chan->max_packet;
1141b02038faSJohn Youn 	else
1142b02038faSJohn Youn 		byte_count = remaining_count;
1143b02038faSJohn Youn 
1144b02038faSJohn Youn 	dword_count = (byte_count + 3) / 4;
1145b02038faSJohn Youn 
1146b02038faSJohn Youn 	if (((unsigned long)data_buf & 0x3) == 0) {
1147b02038faSJohn Youn 		/* xfer_buf is DWORD aligned */
1148b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++)
1149f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
1150b02038faSJohn Youn 	} else {
1151b02038faSJohn Youn 		/* xfer_buf is not DWORD aligned */
1152b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++) {
1153b02038faSJohn Youn 			u32 data = data_buf[0] | data_buf[1] << 8 |
1154b02038faSJohn Youn 				   data_buf[2] << 16 | data_buf[3] << 24;
1155f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
1156b02038faSJohn Youn 		}
1157b02038faSJohn Youn 	}
1158b02038faSJohn Youn 
1159b02038faSJohn Youn 	chan->xfer_count += byte_count;
1160b02038faSJohn Youn 	chan->xfer_buf += byte_count;
1161b02038faSJohn Youn }
1162b02038faSJohn Youn 
1163b02038faSJohn Youn /**
1164b02038faSJohn Youn  * dwc2_hc_do_ping() - Starts a PING transfer
1165b02038faSJohn Youn  *
1166b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1167b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1168b02038faSJohn Youn  *
1169b02038faSJohn Youn  * This function should only be called in Slave mode. The Do Ping bit is set in
1170b02038faSJohn Youn  * the HCTSIZ register, then the channel is enabled.
1171b02038faSJohn Youn  */
1172b02038faSJohn Youn static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1173b02038faSJohn Youn 			    struct dwc2_host_chan *chan)
1174b02038faSJohn Youn {
1175b02038faSJohn Youn 	u32 hcchar;
1176b02038faSJohn Youn 	u32 hctsiz;
1177b02038faSJohn Youn 
1178b02038faSJohn Youn 	if (dbg_hc(chan))
1179b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1180b02038faSJohn Youn 			 chan->hc_num);
1181b02038faSJohn Youn 
1182b02038faSJohn Youn 	hctsiz = TSIZ_DOPNG;
1183b02038faSJohn Youn 	hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1184f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1185b02038faSJohn Youn 
1186f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1187b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1188b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1189f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1190b02038faSJohn Youn }
1191b02038faSJohn Youn 
1192b02038faSJohn Youn /**
1193b02038faSJohn Youn  * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1194b02038faSJohn Youn  * channel and starts the transfer
1195b02038faSJohn Youn  *
1196b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1197b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel. The xfer_len value
1198b02038faSJohn Youn  *         may be reduced to accommodate the max widths of the XferSize and
1199b02038faSJohn Youn  *         PktCnt fields in the HCTSIZn register. The multi_count value may be
1200b02038faSJohn Youn  *         changed to reflect the final xfer_len value.
1201b02038faSJohn Youn  *
1202b02038faSJohn Youn  * This function may be called in either Slave mode or DMA mode. In Slave mode,
1203b02038faSJohn Youn  * the caller must ensure that there is sufficient space in the request queue
1204b02038faSJohn Youn  * and Tx Data FIFO.
1205b02038faSJohn Youn  *
1206b02038faSJohn Youn  * For an OUT transfer in Slave mode, it loads a data packet into the
1207b02038faSJohn Youn  * appropriate FIFO. If necessary, additional data packets are loaded in the
1208b02038faSJohn Youn  * Host ISR.
1209b02038faSJohn Youn  *
1210b02038faSJohn Youn  * For an IN transfer in Slave mode, a data packet is requested. The data
1211b02038faSJohn Youn  * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1212b02038faSJohn Youn  * additional data packets are requested in the Host ISR.
1213b02038faSJohn Youn  *
1214b02038faSJohn Youn  * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1215b02038faSJohn Youn  * register along with a packet count of 1 and the channel is enabled. This
1216b02038faSJohn Youn  * causes a single PING transaction to occur. Other fields in HCTSIZ are
1217b02038faSJohn Youn  * simply set to 0 since no data transfer occurs in this case.
1218b02038faSJohn Youn  *
1219b02038faSJohn Youn  * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1220b02038faSJohn Youn  * all the information required to perform the subsequent data transfer. In
1221b02038faSJohn Youn  * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1222b02038faSJohn Youn  * controller performs the entire PING protocol, then starts the data
1223b02038faSJohn Youn  * transfer.
1224b02038faSJohn Youn  */
1225b02038faSJohn Youn static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1226b02038faSJohn Youn 				   struct dwc2_host_chan *chan)
1227b02038faSJohn Youn {
1228bea8e86cSJohn Youn 	u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1229bea8e86cSJohn Youn 	u16 max_hc_pkt_count = hsotg->params.max_packet_count;
1230b02038faSJohn Youn 	u32 hcchar;
1231b02038faSJohn Youn 	u32 hctsiz = 0;
1232b02038faSJohn Youn 	u16 num_packets;
1233b02038faSJohn Youn 	u32 ec_mc;
1234b02038faSJohn Youn 
1235b02038faSJohn Youn 	if (dbg_hc(chan))
1236b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1237b02038faSJohn Youn 
1238b02038faSJohn Youn 	if (chan->do_ping) {
123995832c00SJohn Youn 		if (!hsotg->params.host_dma) {
1240b02038faSJohn Youn 			if (dbg_hc(chan))
1241b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "ping, no DMA\n");
1242b02038faSJohn Youn 			dwc2_hc_do_ping(hsotg, chan);
1243b02038faSJohn Youn 			chan->xfer_started = 1;
1244b02038faSJohn Youn 			return;
1245b02038faSJohn Youn 		}
1246b02038faSJohn Youn 
1247b02038faSJohn Youn 		if (dbg_hc(chan))
1248b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "ping, DMA\n");
1249b02038faSJohn Youn 
1250b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1251b02038faSJohn Youn 	}
1252b02038faSJohn Youn 
1253b02038faSJohn Youn 	if (chan->do_split) {
1254b02038faSJohn Youn 		if (dbg_hc(chan))
1255b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "split\n");
1256b02038faSJohn Youn 		num_packets = 1;
1257b02038faSJohn Youn 
1258b02038faSJohn Youn 		if (chan->complete_split && !chan->ep_is_in)
1259b02038faSJohn Youn 			/*
1260b02038faSJohn Youn 			 * For CSPLIT OUT Transfer, set the size to 0 so the
1261b02038faSJohn Youn 			 * core doesn't expect any data written to the FIFO
1262b02038faSJohn Youn 			 */
1263b02038faSJohn Youn 			chan->xfer_len = 0;
1264b02038faSJohn Youn 		else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1265b02038faSJohn Youn 			chan->xfer_len = chan->max_packet;
1266b02038faSJohn Youn 		else if (!chan->ep_is_in && chan->xfer_len > 188)
1267b02038faSJohn Youn 			chan->xfer_len = 188;
1268b02038faSJohn Youn 
1269b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1270b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1271b02038faSJohn Youn 
1272b02038faSJohn Youn 		/* For split set ec_mc for immediate retries */
1273b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1274b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1275b02038faSJohn Youn 			ec_mc = 3;
1276b02038faSJohn Youn 		else
1277b02038faSJohn Youn 			ec_mc = 1;
1278b02038faSJohn Youn 	} else {
1279b02038faSJohn Youn 		if (dbg_hc(chan))
1280b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "no split\n");
1281b02038faSJohn Youn 		/*
1282b02038faSJohn Youn 		 * Ensure that the transfer length and packet count will fit
1283b02038faSJohn Youn 		 * in the widths allocated for them in the HCTSIZn register
1284b02038faSJohn Youn 		 */
1285b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1286b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1287b02038faSJohn Youn 			/*
1288b02038faSJohn Youn 			 * Make sure the transfer size is no larger than one
1289b02038faSJohn Youn 			 * (micro)frame's worth of data. (A check was done
1290b02038faSJohn Youn 			 * when the periodic transfer was accepted to ensure
1291b02038faSJohn Youn 			 * that a (micro)frame's worth of data can be
1292b02038faSJohn Youn 			 * programmed into a channel.)
1293b02038faSJohn Youn 			 */
1294b02038faSJohn Youn 			u32 max_periodic_len =
1295b02038faSJohn Youn 				chan->multi_count * chan->max_packet;
1296b02038faSJohn Youn 
1297b02038faSJohn Youn 			if (chan->xfer_len > max_periodic_len)
1298b02038faSJohn Youn 				chan->xfer_len = max_periodic_len;
1299b02038faSJohn Youn 		} else if (chan->xfer_len > max_hc_xfer_size) {
1300b02038faSJohn Youn 			/*
1301b02038faSJohn Youn 			 * Make sure that xfer_len is a multiple of max packet
1302b02038faSJohn Youn 			 * size
1303b02038faSJohn Youn 			 */
1304b02038faSJohn Youn 			chan->xfer_len =
1305b02038faSJohn Youn 				max_hc_xfer_size - chan->max_packet + 1;
1306b02038faSJohn Youn 		}
1307b02038faSJohn Youn 
1308b02038faSJohn Youn 		if (chan->xfer_len > 0) {
1309b02038faSJohn Youn 			num_packets = (chan->xfer_len + chan->max_packet - 1) /
1310b02038faSJohn Youn 					chan->max_packet;
1311b02038faSJohn Youn 			if (num_packets > max_hc_pkt_count) {
1312b02038faSJohn Youn 				num_packets = max_hc_pkt_count;
1313b02038faSJohn Youn 				chan->xfer_len = num_packets * chan->max_packet;
1314415fa1c7SGuenter Roeck 			} else if (chan->ep_is_in) {
1315415fa1c7SGuenter Roeck 				/*
1316415fa1c7SGuenter Roeck 				 * Always program an integral # of max packets
1317415fa1c7SGuenter Roeck 				 * for IN transfers.
1318415fa1c7SGuenter Roeck 				 * Note: This assumes that the input buffer is
1319415fa1c7SGuenter Roeck 				 * aligned and sized accordingly.
1320415fa1c7SGuenter Roeck 				 */
1321415fa1c7SGuenter Roeck 				chan->xfer_len = num_packets * chan->max_packet;
1322b02038faSJohn Youn 			}
1323b02038faSJohn Youn 		} else {
1324b02038faSJohn Youn 			/* Need 1 packet for transfer length of 0 */
1325b02038faSJohn Youn 			num_packets = 1;
1326b02038faSJohn Youn 		}
1327b02038faSJohn Youn 
1328b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1329b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1330b02038faSJohn Youn 			/*
1331b02038faSJohn Youn 			 * Make sure that the multi_count field matches the
1332b02038faSJohn Youn 			 * actual transfer length
1333b02038faSJohn Youn 			 */
1334b02038faSJohn Youn 			chan->multi_count = num_packets;
1335b02038faSJohn Youn 
1336b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1337b02038faSJohn Youn 			dwc2_set_pid_isoc(chan);
1338b02038faSJohn Youn 
1339b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1340b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1341b02038faSJohn Youn 
1342b02038faSJohn Youn 		/* The ec_mc gets the multi_count for non-split */
1343b02038faSJohn Youn 		ec_mc = chan->multi_count;
1344b02038faSJohn Youn 	}
1345b02038faSJohn Youn 
1346b02038faSJohn Youn 	chan->start_pkt_count = num_packets;
1347b02038faSJohn Youn 	hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1348b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1349b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1350f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1351b02038faSJohn Youn 	if (dbg_hc(chan)) {
1352b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1353b02038faSJohn Youn 			 hctsiz, chan->hc_num);
1354b02038faSJohn Youn 
1355b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1356b02038faSJohn Youn 			 chan->hc_num);
1357b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Xfer Size: %d\n",
1358b02038faSJohn Youn 			 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1359b02038faSJohn Youn 			 TSIZ_XFERSIZE_SHIFT);
1360b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Num Pkts: %d\n",
1361b02038faSJohn Youn 			 (hctsiz & TSIZ_PKTCNT_MASK) >>
1362b02038faSJohn Youn 			 TSIZ_PKTCNT_SHIFT);
1363b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1364b02038faSJohn Youn 			 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1365b02038faSJohn Youn 			 TSIZ_SC_MC_PID_SHIFT);
1366b02038faSJohn Youn 	}
1367b02038faSJohn Youn 
136895832c00SJohn Youn 	if (hsotg->params.host_dma) {
1369af424a41SWilliam Wu 		dma_addr_t dma_addr;
1370af424a41SWilliam Wu 
1371af424a41SWilliam Wu 		if (chan->align_buf) {
1372af424a41SWilliam Wu 			if (dbg_hc(chan))
1373af424a41SWilliam Wu 				dev_vdbg(hsotg->dev, "align_buf\n");
1374af424a41SWilliam Wu 			dma_addr = chan->align_buf;
1375af424a41SWilliam Wu 		} else {
1376af424a41SWilliam Wu 			dma_addr = chan->xfer_dma;
1377af424a41SWilliam Wu 		}
1378f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
1379af424a41SWilliam Wu 
1380b02038faSJohn Youn 		if (dbg_hc(chan))
1381b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1382af424a41SWilliam Wu 				 (unsigned long)dma_addr, chan->hc_num);
1383b02038faSJohn Youn 	}
1384b02038faSJohn Youn 
1385b02038faSJohn Youn 	/* Start the split */
1386b02038faSJohn Youn 	if (chan->do_split) {
1387f25c42b8SGevorg Sahakyan 		u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
1388b02038faSJohn Youn 
1389b02038faSJohn Youn 		hcsplt |= HCSPLT_SPLTENA;
1390f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
1391b02038faSJohn Youn 	}
1392b02038faSJohn Youn 
1393f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1394b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1395b02038faSJohn Youn 	hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1396b02038faSJohn Youn 	dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1397b02038faSJohn Youn 
1398b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1399b02038faSJohn Youn 		dev_warn(hsotg->dev,
1400b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1401b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1402b02038faSJohn Youn 
1403b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1404b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1405b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1406b02038faSJohn Youn 
1407b02038faSJohn Youn 	if (dbg_hc(chan))
1408b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1409b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1410b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1411b02038faSJohn Youn 
1412f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1413b02038faSJohn Youn 	if (dbg_hc(chan))
1414b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1415b02038faSJohn Youn 			 chan->hc_num);
1416b02038faSJohn Youn 
1417b02038faSJohn Youn 	chan->xfer_started = 1;
1418b02038faSJohn Youn 	chan->requests++;
1419b02038faSJohn Youn 
142095832c00SJohn Youn 	if (!hsotg->params.host_dma &&
1421b02038faSJohn Youn 	    !chan->ep_is_in && chan->xfer_len > 0)
1422b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1423b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1424b02038faSJohn Youn }
1425b02038faSJohn Youn 
1426b02038faSJohn Youn /**
1427b02038faSJohn Youn  * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1428b02038faSJohn Youn  * host channel and starts the transfer in Descriptor DMA mode
1429b02038faSJohn Youn  *
1430b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1431b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1432b02038faSJohn Youn  *
1433b02038faSJohn Youn  * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1434b02038faSJohn Youn  * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1435b02038faSJohn Youn  * with micro-frame bitmap.
1436b02038faSJohn Youn  *
1437b02038faSJohn Youn  * Initializes HCDMA register with descriptor list address and CTD value then
1438b02038faSJohn Youn  * starts the transfer via enabling the channel.
1439b02038faSJohn Youn  */
1440b02038faSJohn Youn void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1441b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1442b02038faSJohn Youn {
1443b02038faSJohn Youn 	u32 hcchar;
1444b02038faSJohn Youn 	u32 hctsiz = 0;
1445b02038faSJohn Youn 
1446b02038faSJohn Youn 	if (chan->do_ping)
1447b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1448b02038faSJohn Youn 
1449b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1450b02038faSJohn Youn 		dwc2_set_pid_isoc(chan);
1451b02038faSJohn Youn 
1452b02038faSJohn Youn 	/* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1453b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1454b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1455b02038faSJohn Youn 
1456b02038faSJohn Youn 	/* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1457b02038faSJohn Youn 	hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1458b02038faSJohn Youn 
1459b02038faSJohn Youn 	/* Non-zero only for high-speed interrupt endpoints */
1460b02038faSJohn Youn 	hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1461b02038faSJohn Youn 
1462b02038faSJohn Youn 	if (dbg_hc(chan)) {
1463b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1464b02038faSJohn Youn 			 chan->hc_num);
1465b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1466b02038faSJohn Youn 			 chan->data_pid_start);
1467b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 NTD: %d\n", chan->ntd - 1);
1468b02038faSJohn Youn 	}
1469b02038faSJohn Youn 
1470f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1471b02038faSJohn Youn 
1472b02038faSJohn Youn 	dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1473b02038faSJohn Youn 				   chan->desc_list_sz, DMA_TO_DEVICE);
1474b02038faSJohn Youn 
1475f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
1476b02038faSJohn Youn 
1477b02038faSJohn Youn 	if (dbg_hc(chan))
1478b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1479b02038faSJohn Youn 			 &chan->desc_list_addr, chan->hc_num);
1480b02038faSJohn Youn 
1481f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1482b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1483b02038faSJohn Youn 	hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1484b02038faSJohn Youn 		  HCCHAR_MULTICNT_MASK;
1485b02038faSJohn Youn 
1486b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1487b02038faSJohn Youn 		dev_warn(hsotg->dev,
1488b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1489b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1490b02038faSJohn Youn 
1491b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1492b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1493b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1494b02038faSJohn Youn 
1495b02038faSJohn Youn 	if (dbg_hc(chan))
1496b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1497b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1498b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1499b02038faSJohn Youn 
1500f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1501b02038faSJohn Youn 	if (dbg_hc(chan))
1502b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1503b02038faSJohn Youn 			 chan->hc_num);
1504b02038faSJohn Youn 
1505b02038faSJohn Youn 	chan->xfer_started = 1;
1506b02038faSJohn Youn 	chan->requests++;
1507b02038faSJohn Youn }
1508b02038faSJohn Youn 
1509b02038faSJohn Youn /**
1510b02038faSJohn Youn  * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1511b02038faSJohn Youn  * a previous call to dwc2_hc_start_transfer()
1512b02038faSJohn Youn  *
1513b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1514b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1515b02038faSJohn Youn  *
1516b02038faSJohn Youn  * The caller must ensure there is sufficient space in the request queue and Tx
1517b02038faSJohn Youn  * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1518b02038faSJohn Youn  * the controller acts autonomously to complete transfers programmed to a host
1519b02038faSJohn Youn  * channel.
1520b02038faSJohn Youn  *
1521b02038faSJohn Youn  * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1522b02038faSJohn Youn  * if there is any data remaining to be queued. For an IN transfer, another
1523b02038faSJohn Youn  * data packet is always requested. For the SETUP phase of a control transfer,
1524b02038faSJohn Youn  * this function does nothing.
1525b02038faSJohn Youn  *
1526b02038faSJohn Youn  * Return: 1 if a new request is queued, 0 if no more requests are required
1527b02038faSJohn Youn  * for this transfer
1528b02038faSJohn Youn  */
1529b02038faSJohn Youn static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1530b02038faSJohn Youn 				     struct dwc2_host_chan *chan)
1531b02038faSJohn Youn {
1532b02038faSJohn Youn 	if (dbg_hc(chan))
1533b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1534b02038faSJohn Youn 			 chan->hc_num);
1535b02038faSJohn Youn 
1536b02038faSJohn Youn 	if (chan->do_split)
1537b02038faSJohn Youn 		/* SPLITs always queue just once per channel */
1538b02038faSJohn Youn 		return 0;
1539b02038faSJohn Youn 
1540b02038faSJohn Youn 	if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1541b02038faSJohn Youn 		/* SETUPs are queued only once since they can't be NAK'd */
1542b02038faSJohn Youn 		return 0;
1543b02038faSJohn Youn 
1544b02038faSJohn Youn 	if (chan->ep_is_in) {
1545b02038faSJohn Youn 		/*
1546b02038faSJohn Youn 		 * Always queue another request for other IN transfers. If
1547b02038faSJohn Youn 		 * back-to-back INs are issued and NAKs are received for both,
1548b02038faSJohn Youn 		 * the driver may still be processing the first NAK when the
1549b02038faSJohn Youn 		 * second NAK is received. When the interrupt handler clears
1550b02038faSJohn Youn 		 * the NAK interrupt for the first NAK, the second NAK will
1551b02038faSJohn Youn 		 * not be seen. So we can't depend on the NAK interrupt
1552b02038faSJohn Youn 		 * handler to requeue a NAK'd request. Instead, IN requests
1553b02038faSJohn Youn 		 * are issued each time this function is called. When the
1554b02038faSJohn Youn 		 * transfer completes, the extra requests for the channel will
1555b02038faSJohn Youn 		 * be flushed.
1556b02038faSJohn Youn 		 */
1557f25c42b8SGevorg Sahakyan 		u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1558b02038faSJohn Youn 
1559b02038faSJohn Youn 		dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1560b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
1561b02038faSJohn Youn 		hcchar &= ~HCCHAR_CHDIS;
1562b02038faSJohn Youn 		if (dbg_hc(chan))
1563b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	 IN xfer: hcchar = 0x%08x\n",
1564b02038faSJohn Youn 				 hcchar);
1565f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1566b02038faSJohn Youn 		chan->requests++;
1567b02038faSJohn Youn 		return 1;
1568b02038faSJohn Youn 	}
1569b02038faSJohn Youn 
1570b02038faSJohn Youn 	/* OUT transfers */
1571b02038faSJohn Youn 
1572b02038faSJohn Youn 	if (chan->xfer_count < chan->xfer_len) {
1573b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1574b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1575f25c42b8SGevorg Sahakyan 			u32 hcchar = dwc2_readl(hsotg,
1576b02038faSJohn Youn 						HCCHAR(chan->hc_num));
1577b02038faSJohn Youn 
1578b02038faSJohn Youn 			dwc2_hc_set_even_odd_frame(hsotg, chan,
1579b02038faSJohn Youn 						   &hcchar);
1580b02038faSJohn Youn 		}
1581b02038faSJohn Youn 
1582b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1583b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1584b02038faSJohn Youn 		chan->requests++;
1585b02038faSJohn Youn 		return 1;
1586b02038faSJohn Youn 	}
1587b02038faSJohn Youn 
1588b02038faSJohn Youn 	return 0;
1589b02038faSJohn Youn }
1590b02038faSJohn Youn 
1591b02038faSJohn Youn /*
1592b02038faSJohn Youn  * =========================================================================
1593b02038faSJohn Youn  *  HCD
1594b02038faSJohn Youn  * =========================================================================
1595b02038faSJohn Youn  */
1596b02038faSJohn Youn 
1597b02038faSJohn Youn /*
1598197ba5f4SPaul Zimmerman  * Processes all the URBs in a single list of QHs. Completes them with
1599197ba5f4SPaul Zimmerman  * -ETIMEDOUT and frees the QTD.
1600197ba5f4SPaul Zimmerman  *
1601197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1602197ba5f4SPaul Zimmerman  */
1603197ba5f4SPaul Zimmerman static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1604197ba5f4SPaul Zimmerman 				      struct list_head *qh_list)
1605197ba5f4SPaul Zimmerman {
1606197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1607197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1608197ba5f4SPaul Zimmerman 
1609197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1610197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1611197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
16122e84da6eSGregory Herrero 			dwc2_host_complete(hsotg, qtd, -ECONNRESET);
1613197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1614197ba5f4SPaul Zimmerman 		}
1615197ba5f4SPaul Zimmerman 	}
1616197ba5f4SPaul Zimmerman }
1617197ba5f4SPaul Zimmerman 
1618197ba5f4SPaul Zimmerman static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1619197ba5f4SPaul Zimmerman 			      struct list_head *qh_list)
1620197ba5f4SPaul Zimmerman {
1621197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1622197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1623197ba5f4SPaul Zimmerman 	unsigned long flags;
1624197ba5f4SPaul Zimmerman 
1625197ba5f4SPaul Zimmerman 	if (!qh_list->next)
1626197ba5f4SPaul Zimmerman 		/* The list hasn't been initialized yet */
1627197ba5f4SPaul Zimmerman 		return;
1628197ba5f4SPaul Zimmerman 
1629197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
1630197ba5f4SPaul Zimmerman 
1631197ba5f4SPaul Zimmerman 	/* Ensure there are no QTDs or URBs left */
1632197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1633197ba5f4SPaul Zimmerman 
1634197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1635197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1636197ba5f4SPaul Zimmerman 
1637197ba5f4SPaul Zimmerman 		/* Free each QTD in the QH's QTD list */
1638197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1639197ba5f4SPaul Zimmerman 					 qtd_list_entry)
1640197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1641197ba5f4SPaul Zimmerman 
164216e80218SDouglas Anderson 		if (qh->channel && qh->channel->qh == qh)
164316e80218SDouglas Anderson 			qh->channel->qh = NULL;
164416e80218SDouglas Anderson 
1645197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
1646197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_free(hsotg, qh);
1647197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
1648197ba5f4SPaul Zimmerman 	}
1649197ba5f4SPaul Zimmerman 
1650197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
1651197ba5f4SPaul Zimmerman }
1652197ba5f4SPaul Zimmerman 
1653197ba5f4SPaul Zimmerman /*
1654197ba5f4SPaul Zimmerman  * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1655197ba5f4SPaul Zimmerman  * and periodic schedules. The QTD associated with each URB is removed from
1656197ba5f4SPaul Zimmerman  * the schedule and freed. This function may be called when a disconnect is
1657197ba5f4SPaul Zimmerman  * detected or when the HCD is being stopped.
1658197ba5f4SPaul Zimmerman  *
1659197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1660197ba5f4SPaul Zimmerman  */
1661197ba5f4SPaul Zimmerman static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1662197ba5f4SPaul Zimmerman {
1663197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
166438d2b5fbSDouglas Anderson 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
1665197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1666197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1667197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1668197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1669197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1670197ba5f4SPaul Zimmerman }
1671197ba5f4SPaul Zimmerman 
1672197ba5f4SPaul Zimmerman /**
1673197ba5f4SPaul Zimmerman  * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1674197ba5f4SPaul Zimmerman  *
1675197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1676197ba5f4SPaul Zimmerman  */
1677197ba5f4SPaul Zimmerman void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1678197ba5f4SPaul Zimmerman {
1679197ba5f4SPaul Zimmerman 	u32 hprt0;
1680197ba5f4SPaul Zimmerman 
1681197ba5f4SPaul Zimmerman 	if (hsotg->op_state == OTG_STATE_B_HOST) {
1682197ba5f4SPaul Zimmerman 		/*
1683197ba5f4SPaul Zimmerman 		 * Reset the port. During a HNP mode switch the reset
1684197ba5f4SPaul Zimmerman 		 * needs to occur within 1ms and have a duration of at
1685197ba5f4SPaul Zimmerman 		 * least 50ms.
1686197ba5f4SPaul Zimmerman 		 */
1687197ba5f4SPaul Zimmerman 		hprt0 = dwc2_read_hprt0(hsotg);
1688197ba5f4SPaul Zimmerman 		hprt0 |= HPRT0_RST;
1689f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
1690197ba5f4SPaul Zimmerman 	}
1691197ba5f4SPaul Zimmerman 
1692197ba5f4SPaul Zimmerman 	queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1693197ba5f4SPaul Zimmerman 			   msecs_to_jiffies(50));
1694197ba5f4SPaul Zimmerman }
1695197ba5f4SPaul Zimmerman 
1696197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1697197ba5f4SPaul Zimmerman static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1698197ba5f4SPaul Zimmerman {
1699bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
1700197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
1701197ba5f4SPaul Zimmerman 	u32 hcchar;
1702197ba5f4SPaul Zimmerman 	int i;
1703197ba5f4SPaul Zimmerman 
170495832c00SJohn Youn 	if (!hsotg->params.host_dma) {
1705197ba5f4SPaul Zimmerman 		/* Flush out any channel requests in slave mode */
1706197ba5f4SPaul Zimmerman 		for (i = 0; i < num_channels; i++) {
1707197ba5f4SPaul Zimmerman 			channel = hsotg->hc_ptr_array[i];
1708197ba5f4SPaul Zimmerman 			if (!list_empty(&channel->hc_list_entry))
1709197ba5f4SPaul Zimmerman 				continue;
1710f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
1711197ba5f4SPaul Zimmerman 			if (hcchar & HCCHAR_CHENA) {
1712197ba5f4SPaul Zimmerman 				hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1713197ba5f4SPaul Zimmerman 				hcchar |= HCCHAR_CHDIS;
1714f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
1715197ba5f4SPaul Zimmerman 			}
1716197ba5f4SPaul Zimmerman 		}
1717197ba5f4SPaul Zimmerman 	}
1718197ba5f4SPaul Zimmerman 
1719197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
1720197ba5f4SPaul Zimmerman 		channel = hsotg->hc_ptr_array[i];
1721197ba5f4SPaul Zimmerman 		if (!list_empty(&channel->hc_list_entry))
1722197ba5f4SPaul Zimmerman 			continue;
1723f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(i));
1724197ba5f4SPaul Zimmerman 		if (hcchar & HCCHAR_CHENA) {
1725197ba5f4SPaul Zimmerman 			/* Halt the channel */
1726197ba5f4SPaul Zimmerman 			hcchar |= HCCHAR_CHDIS;
1727f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcchar, HCCHAR(i));
1728197ba5f4SPaul Zimmerman 		}
1729197ba5f4SPaul Zimmerman 
1730197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, channel);
1731197ba5f4SPaul Zimmerman 		list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1732197ba5f4SPaul Zimmerman 		/*
1733197ba5f4SPaul Zimmerman 		 * Added for Descriptor DMA to prevent channel double cleanup in
1734197ba5f4SPaul Zimmerman 		 * release_channel_ddma(), which is called from ep_disable when
1735197ba5f4SPaul Zimmerman 		 * device disconnects
1736197ba5f4SPaul Zimmerman 		 */
1737197ba5f4SPaul Zimmerman 		channel->qh = NULL;
1738197ba5f4SPaul Zimmerman 	}
17397252f1bfSVincent Palatin 	/* All channels have been freed, mark them available */
174095832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
17417252f1bfSVincent Palatin 		hsotg->available_host_channels =
1742bea8e86cSJohn Youn 			hsotg->params.host_channels;
17437252f1bfSVincent Palatin 	} else {
17447252f1bfSVincent Palatin 		hsotg->non_periodic_channels = 0;
17457252f1bfSVincent Palatin 		hsotg->periodic_channels = 0;
17467252f1bfSVincent Palatin 	}
1747197ba5f4SPaul Zimmerman }
1748197ba5f4SPaul Zimmerman 
1749197ba5f4SPaul Zimmerman /**
17506a659531SDouglas Anderson  * dwc2_hcd_connect() - Handles connect of the HCD
1751197ba5f4SPaul Zimmerman  *
1752197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1753197ba5f4SPaul Zimmerman  *
1754197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1755197ba5f4SPaul Zimmerman  */
17566a659531SDouglas Anderson void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
17576a659531SDouglas Anderson {
17586a659531SDouglas Anderson 	if (hsotg->lx_state != DWC2_L0)
17596a659531SDouglas Anderson 		usb_hcd_resume_root_hub(hsotg->priv);
17606a659531SDouglas Anderson 
17616a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status_change = 1;
17626a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status = 1;
17636a659531SDouglas Anderson }
17646a659531SDouglas Anderson 
17656a659531SDouglas Anderson /**
17666a659531SDouglas Anderson  * dwc2_hcd_disconnect() - Handles disconnect of the HCD
17676a659531SDouglas Anderson  *
17686a659531SDouglas Anderson  * @hsotg: Pointer to struct dwc2_hsotg
17696a659531SDouglas Anderson  * @force: If true, we won't try to reconnect even if we see device connected.
17706a659531SDouglas Anderson  *
17716a659531SDouglas Anderson  * Must be called with interrupt disabled and spinlock held
17726a659531SDouglas Anderson  */
17736a659531SDouglas Anderson void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
1774197ba5f4SPaul Zimmerman {
1775197ba5f4SPaul Zimmerman 	u32 intr;
17766a659531SDouglas Anderson 	u32 hprt0;
1777197ba5f4SPaul Zimmerman 
1778197ba5f4SPaul Zimmerman 	/* Set status flags for the hub driver */
1779197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status_change = 1;
1780197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status = 0;
1781197ba5f4SPaul Zimmerman 
1782197ba5f4SPaul Zimmerman 	/*
1783197ba5f4SPaul Zimmerman 	 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1784197ba5f4SPaul Zimmerman 	 * interrupt mask and status bits and disabling subsequent host
1785197ba5f4SPaul Zimmerman 	 * channel interrupts.
1786197ba5f4SPaul Zimmerman 	 */
1787f25c42b8SGevorg Sahakyan 	intr = dwc2_readl(hsotg, GINTMSK);
1788197ba5f4SPaul Zimmerman 	intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
1789f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTMSK);
1790197ba5f4SPaul Zimmerman 	intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
1791f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTSTS);
1792197ba5f4SPaul Zimmerman 
1793197ba5f4SPaul Zimmerman 	/*
1794197ba5f4SPaul Zimmerman 	 * Turn off the vbus power only if the core has transitioned to device
1795197ba5f4SPaul Zimmerman 	 * mode. If still in host mode, need to keep power on to detect a
1796197ba5f4SPaul Zimmerman 	 * reconnection.
1797197ba5f4SPaul Zimmerman 	 */
1798197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
1799197ba5f4SPaul Zimmerman 		if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1800197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
1801f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, HPRT0);
1802197ba5f4SPaul Zimmerman 		}
1803197ba5f4SPaul Zimmerman 
1804197ba5f4SPaul Zimmerman 		dwc2_disable_host_interrupts(hsotg);
1805197ba5f4SPaul Zimmerman 	}
1806197ba5f4SPaul Zimmerman 
1807197ba5f4SPaul Zimmerman 	/* Respond with an error status to all URBs in the schedule */
1808197ba5f4SPaul Zimmerman 	dwc2_kill_all_urbs(hsotg);
1809197ba5f4SPaul Zimmerman 
1810197ba5f4SPaul Zimmerman 	if (dwc2_is_host_mode(hsotg))
1811197ba5f4SPaul Zimmerman 		/* Clean up any host channels that were in use */
1812197ba5f4SPaul Zimmerman 		dwc2_hcd_cleanup_channels(hsotg);
1813197ba5f4SPaul Zimmerman 
1814197ba5f4SPaul Zimmerman 	dwc2_host_disconnect(hsotg);
18156a659531SDouglas Anderson 
18166a659531SDouglas Anderson 	/*
18176a659531SDouglas Anderson 	 * Add an extra check here to see if we're actually connected but
18186a659531SDouglas Anderson 	 * we don't have a detection interrupt pending.  This can happen if:
18196a659531SDouglas Anderson 	 *   1. hardware sees connect
18206a659531SDouglas Anderson 	 *   2. hardware sees disconnect
18216a659531SDouglas Anderson 	 *   3. hardware sees connect
18226a659531SDouglas Anderson 	 *   4. dwc2_port_intr() - clears connect interrupt
18236a659531SDouglas Anderson 	 *   5. dwc2_handle_common_intr() - calls here
18246a659531SDouglas Anderson 	 *
18256a659531SDouglas Anderson 	 * Without the extra check here we will end calling disconnect
18266a659531SDouglas Anderson 	 * and won't get any future interrupts to handle the connect.
18276a659531SDouglas Anderson 	 */
18286a659531SDouglas Anderson 	if (!force) {
1829f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
18306a659531SDouglas Anderson 		if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
18316a659531SDouglas Anderson 			dwc2_hcd_connect(hsotg);
18326a659531SDouglas Anderson 	}
1833197ba5f4SPaul Zimmerman }
1834197ba5f4SPaul Zimmerman 
1835197ba5f4SPaul Zimmerman /**
1836197ba5f4SPaul Zimmerman  * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
1837197ba5f4SPaul Zimmerman  *
1838197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1839197ba5f4SPaul Zimmerman  */
1840197ba5f4SPaul Zimmerman static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
1841197ba5f4SPaul Zimmerman {
18421fb7f12dSDouglas Anderson 	if (hsotg->bus_suspended) {
1843197ba5f4SPaul Zimmerman 		hsotg->flags.b.port_suspend_change = 1;
1844b46146d5SGregory Herrero 		usb_hcd_resume_root_hub(hsotg->priv);
1845197ba5f4SPaul Zimmerman 	}
18461fb7f12dSDouglas Anderson 
18471fb7f12dSDouglas Anderson 	if (hsotg->lx_state == DWC2_L1)
18481fb7f12dSDouglas Anderson 		hsotg->flags.b.port_l1_change = 1;
1849b46146d5SGregory Herrero }
1850197ba5f4SPaul Zimmerman 
1851197ba5f4SPaul Zimmerman /**
1852197ba5f4SPaul Zimmerman  * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
1853197ba5f4SPaul Zimmerman  *
1854197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1855197ba5f4SPaul Zimmerman  *
1856197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1857197ba5f4SPaul Zimmerman  */
1858197ba5f4SPaul Zimmerman void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
1859197ba5f4SPaul Zimmerman {
1860197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
1861197ba5f4SPaul Zimmerman 
1862197ba5f4SPaul Zimmerman 	/*
1863197ba5f4SPaul Zimmerman 	 * The root hub should be disconnected before this function is called.
1864197ba5f4SPaul Zimmerman 	 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
1865197ba5f4SPaul Zimmerman 	 * and the QH lists (via ..._hcd_endpoint_disable).
1866197ba5f4SPaul Zimmerman 	 */
1867197ba5f4SPaul Zimmerman 
1868197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
1869197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
1870197ba5f4SPaul Zimmerman 
1871197ba5f4SPaul Zimmerman 	/* Turn off the vbus power */
1872197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "PortPower off\n");
1873f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HPRT0);
1874197ba5f4SPaul Zimmerman }
1875197ba5f4SPaul Zimmerman 
187633ad261aSGregory Herrero /* Caller must hold driver lock */
1877197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
1878b58e6ceeSMian Yousaf Kaukab 				struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
1879b5a468a6SMian Yousaf Kaukab 				struct dwc2_qtd *qtd)
1880197ba5f4SPaul Zimmerman {
1881197ba5f4SPaul Zimmerman 	u32 intr_mask;
1882197ba5f4SPaul Zimmerman 	int retval;
1883197ba5f4SPaul Zimmerman 	int dev_speed;
1884197ba5f4SPaul Zimmerman 
1885197ba5f4SPaul Zimmerman 	if (!hsotg->flags.b.port_connect_status) {
1886197ba5f4SPaul Zimmerman 		/* No longer connected */
1887197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Not connected\n");
1888197ba5f4SPaul Zimmerman 		return -ENODEV;
1889197ba5f4SPaul Zimmerman 	}
1890197ba5f4SPaul Zimmerman 
1891197ba5f4SPaul Zimmerman 	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1892197ba5f4SPaul Zimmerman 
1893197ba5f4SPaul Zimmerman 	/* Some configurations cannot support LS traffic on a FS root port */
1894197ba5f4SPaul Zimmerman 	if ((dev_speed == USB_SPEED_LOW) &&
1895197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
1896197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
1897f25c42b8SGevorg Sahakyan 		u32 hprt0 = dwc2_readl(hsotg, HPRT0);
1898197ba5f4SPaul Zimmerman 		u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1899197ba5f4SPaul Zimmerman 
1900197ba5f4SPaul Zimmerman 		if (prtspd == HPRT0_SPD_FULL_SPEED)
1901197ba5f4SPaul Zimmerman 			return -ENODEV;
1902197ba5f4SPaul Zimmerman 	}
1903197ba5f4SPaul Zimmerman 
1904197ba5f4SPaul Zimmerman 	if (!qtd)
1905b5a468a6SMian Yousaf Kaukab 		return -EINVAL;
1906197ba5f4SPaul Zimmerman 
1907197ba5f4SPaul Zimmerman 	dwc2_hcd_qtd_init(qtd, urb);
1908b58e6ceeSMian Yousaf Kaukab 	retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
1909197ba5f4SPaul Zimmerman 	if (retval) {
1910197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
1911197ba5f4SPaul Zimmerman 			"DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
1912197ba5f4SPaul Zimmerman 			retval);
1913197ba5f4SPaul Zimmerman 		return retval;
1914197ba5f4SPaul Zimmerman 	}
1915197ba5f4SPaul Zimmerman 
1916f25c42b8SGevorg Sahakyan 	intr_mask = dwc2_readl(hsotg, GINTMSK);
1917197ba5f4SPaul Zimmerman 	if (!(intr_mask & GINTSTS_SOF)) {
1918197ba5f4SPaul Zimmerman 		enum dwc2_transaction_type tr_type;
1919197ba5f4SPaul Zimmerman 
1920197ba5f4SPaul Zimmerman 		if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
1921197ba5f4SPaul Zimmerman 		    !(qtd->urb->flags & URB_GIVEBACK_ASAP))
1922197ba5f4SPaul Zimmerman 			/*
1923197ba5f4SPaul Zimmerman 			 * Do not schedule SG transactions until qtd has
1924197ba5f4SPaul Zimmerman 			 * URB_GIVEBACK_ASAP set
1925197ba5f4SPaul Zimmerman 			 */
1926197ba5f4SPaul Zimmerman 			return 0;
1927197ba5f4SPaul Zimmerman 
1928197ba5f4SPaul Zimmerman 		tr_type = dwc2_hcd_select_transactions(hsotg);
1929197ba5f4SPaul Zimmerman 		if (tr_type != DWC2_TRANSACTION_NONE)
1930197ba5f4SPaul Zimmerman 			dwc2_hcd_queue_transactions(hsotg, tr_type);
1931197ba5f4SPaul Zimmerman 	}
1932197ba5f4SPaul Zimmerman 
1933197ba5f4SPaul Zimmerman 	return 0;
1934197ba5f4SPaul Zimmerman }
1935197ba5f4SPaul Zimmerman 
1936197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1937197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
1938197ba5f4SPaul Zimmerman 				struct dwc2_hcd_urb *urb)
1939197ba5f4SPaul Zimmerman {
1940197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
1941197ba5f4SPaul Zimmerman 	struct dwc2_qtd *urb_qtd;
1942197ba5f4SPaul Zimmerman 
1943197ba5f4SPaul Zimmerman 	urb_qtd = urb->qtd;
1944197ba5f4SPaul Zimmerman 	if (!urb_qtd) {
1945197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
1946197ba5f4SPaul Zimmerman 		return -EINVAL;
1947197ba5f4SPaul Zimmerman 	}
1948197ba5f4SPaul Zimmerman 
1949197ba5f4SPaul Zimmerman 	qh = urb_qtd->qh;
1950197ba5f4SPaul Zimmerman 	if (!qh) {
1951197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
1952197ba5f4SPaul Zimmerman 		return -EINVAL;
1953197ba5f4SPaul Zimmerman 	}
1954197ba5f4SPaul Zimmerman 
1955197ba5f4SPaul Zimmerman 	urb->priv = NULL;
1956197ba5f4SPaul Zimmerman 
1957197ba5f4SPaul Zimmerman 	if (urb_qtd->in_process && qh->channel) {
1958197ba5f4SPaul Zimmerman 		dwc2_dump_channel_info(hsotg, qh->channel);
1959197ba5f4SPaul Zimmerman 
1960197ba5f4SPaul Zimmerman 		/* The QTD is in process (it has been assigned to a channel) */
1961197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status)
1962197ba5f4SPaul Zimmerman 			/*
1963197ba5f4SPaul Zimmerman 			 * If still connected (i.e. in host mode), halt the
1964197ba5f4SPaul Zimmerman 			 * channel so it can be used for other transfers. If
1965197ba5f4SPaul Zimmerman 			 * no longer connected, the host registers can't be
1966197ba5f4SPaul Zimmerman 			 * written to halt the channel since the core is in
1967197ba5f4SPaul Zimmerman 			 * device mode.
1968197ba5f4SPaul Zimmerman 			 */
1969197ba5f4SPaul Zimmerman 			dwc2_hc_halt(hsotg, qh->channel,
1970197ba5f4SPaul Zimmerman 				     DWC2_HC_XFER_URB_DEQUEUE);
1971197ba5f4SPaul Zimmerman 	}
1972197ba5f4SPaul Zimmerman 
1973197ba5f4SPaul Zimmerman 	/*
1974197ba5f4SPaul Zimmerman 	 * Free the QTD and clean up the associated QH. Leave the QH in the
1975197ba5f4SPaul Zimmerman 	 * schedule if it has any remaining QTDs.
1976197ba5f4SPaul Zimmerman 	 */
197795832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
1978197ba5f4SPaul Zimmerman 		u8 in_process = urb_qtd->in_process;
1979197ba5f4SPaul Zimmerman 
1980197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1981197ba5f4SPaul Zimmerman 		if (in_process) {
1982197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_deactivate(hsotg, qh, 0);
1983197ba5f4SPaul Zimmerman 			qh->channel = NULL;
1984197ba5f4SPaul Zimmerman 		} else if (list_empty(&qh->qtd_list)) {
1985197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
1986197ba5f4SPaul Zimmerman 		}
1987197ba5f4SPaul Zimmerman 	} else {
1988197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1989197ba5f4SPaul Zimmerman 	}
1990197ba5f4SPaul Zimmerman 
1991197ba5f4SPaul Zimmerman 	return 0;
1992197ba5f4SPaul Zimmerman }
1993197ba5f4SPaul Zimmerman 
1994197ba5f4SPaul Zimmerman /* Must NOT be called with interrupt disabled or spinlock held */
1995197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
1996197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep, int retry)
1997197ba5f4SPaul Zimmerman {
1998197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1999197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2000197ba5f4SPaul Zimmerman 	unsigned long flags;
2001197ba5f4SPaul Zimmerman 	int rc;
2002197ba5f4SPaul Zimmerman 
2003197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
2004197ba5f4SPaul Zimmerman 
2005197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
2006197ba5f4SPaul Zimmerman 	if (!qh) {
2007197ba5f4SPaul Zimmerman 		rc = -EINVAL;
2008197ba5f4SPaul Zimmerman 		goto err;
2009197ba5f4SPaul Zimmerman 	}
2010197ba5f4SPaul Zimmerman 
2011197ba5f4SPaul Zimmerman 	while (!list_empty(&qh->qtd_list) && retry--) {
2012197ba5f4SPaul Zimmerman 		if (retry == 0) {
2013197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2014197ba5f4SPaul Zimmerman 				"## timeout in dwc2_hcd_endpoint_disable() ##\n");
2015197ba5f4SPaul Zimmerman 			rc = -EBUSY;
2016197ba5f4SPaul Zimmerman 			goto err;
2017197ba5f4SPaul Zimmerman 		}
2018197ba5f4SPaul Zimmerman 
2019197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
202004a9db79SNicholas Mc Guire 		msleep(20);
2021197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
2022197ba5f4SPaul Zimmerman 		qh = ep->hcpriv;
2023197ba5f4SPaul Zimmerman 		if (!qh) {
2024197ba5f4SPaul Zimmerman 			rc = -EINVAL;
2025197ba5f4SPaul Zimmerman 			goto err;
2026197ba5f4SPaul Zimmerman 		}
2027197ba5f4SPaul Zimmerman 	}
2028197ba5f4SPaul Zimmerman 
2029197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_unlink(hsotg, qh);
2030197ba5f4SPaul Zimmerman 
2031197ba5f4SPaul Zimmerman 	/* Free each QTD in the QH's QTD list */
2032197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2033197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2034197ba5f4SPaul Zimmerman 
2035197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
203616e80218SDouglas Anderson 
203716e80218SDouglas Anderson 	if (qh->channel && qh->channel->qh == qh)
203816e80218SDouglas Anderson 		qh->channel->qh = NULL;
203916e80218SDouglas Anderson 
2040197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
204116e80218SDouglas Anderson 
2042197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_free(hsotg, qh);
2043197ba5f4SPaul Zimmerman 
2044197ba5f4SPaul Zimmerman 	return 0;
2045197ba5f4SPaul Zimmerman 
2046197ba5f4SPaul Zimmerman err:
2047197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
2048197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
2049197ba5f4SPaul Zimmerman 
2050197ba5f4SPaul Zimmerman 	return rc;
2051197ba5f4SPaul Zimmerman }
2052197ba5f4SPaul Zimmerman 
2053197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
2054197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2055197ba5f4SPaul Zimmerman 				   struct usb_host_endpoint *ep)
2056197ba5f4SPaul Zimmerman {
2057197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = ep->hcpriv;
2058197ba5f4SPaul Zimmerman 
2059197ba5f4SPaul Zimmerman 	if (!qh)
2060197ba5f4SPaul Zimmerman 		return -EINVAL;
2061197ba5f4SPaul Zimmerman 
2062197ba5f4SPaul Zimmerman 	qh->data_toggle = DWC2_HC_PID_DATA0;
2063197ba5f4SPaul Zimmerman 
2064197ba5f4SPaul Zimmerman 	return 0;
2065197ba5f4SPaul Zimmerman }
2066197ba5f4SPaul Zimmerman 
2067b02038faSJohn Youn /**
2068b02038faSJohn Youn  * dwc2_core_init() - Initializes the DWC_otg controller registers and
2069b02038faSJohn Youn  * prepares the core for device mode or host mode operation
2070b02038faSJohn Youn  *
2071b02038faSJohn Youn  * @hsotg:         Programming view of the DWC_otg controller
2072b02038faSJohn Youn  * @initial_setup: If true then this is the first init for this instance.
2073b02038faSJohn Youn  */
207465c9c4c6SVardan Mikayelyan int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2075b02038faSJohn Youn {
2076b02038faSJohn Youn 	u32 usbcfg, otgctl;
2077b02038faSJohn Youn 	int retval;
2078b02038faSJohn Youn 
2079b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2080b02038faSJohn Youn 
2081f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
2082b02038faSJohn Youn 
2083b02038faSJohn Youn 	/* Set ULPI External VBUS bit if needed */
2084b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
208595832c00SJohn Youn 	if (hsotg->params.phy_ulpi_ext_vbus)
2086b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2087b02038faSJohn Youn 
2088b02038faSJohn Youn 	/* Set external TS Dline pulsing bit if needed */
2089b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
209095832c00SJohn Youn 	if (hsotg->params.ts_dline)
2091b02038faSJohn Youn 		usbcfg |= GUSBCFG_TERMSELDLPULSE;
2092b02038faSJohn Youn 
2093f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
2094b02038faSJohn Youn 
2095b02038faSJohn Youn 	/*
2096b02038faSJohn Youn 	 * Reset the Controller
2097b02038faSJohn Youn 	 *
2098b02038faSJohn Youn 	 * We only need to reset the controller if this is a re-init.
2099b02038faSJohn Youn 	 * For the first init we know for sure that earlier code reset us (it
2100b02038faSJohn Youn 	 * needed to in order to properly detect various parameters).
2101b02038faSJohn Youn 	 */
2102b02038faSJohn Youn 	if (!initial_setup) {
210313b1f8e2SVardan Mikayelyan 		retval = dwc2_core_reset(hsotg, false);
2104b02038faSJohn Youn 		if (retval) {
2105b02038faSJohn Youn 			dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2106b02038faSJohn Youn 				__func__);
2107b02038faSJohn Youn 			return retval;
2108b02038faSJohn Youn 		}
2109b02038faSJohn Youn 	}
2110b02038faSJohn Youn 
2111b02038faSJohn Youn 	/*
2112b02038faSJohn Youn 	 * This needs to happen in FS mode before any other programming occurs
2113b02038faSJohn Youn 	 */
2114b02038faSJohn Youn 	retval = dwc2_phy_init(hsotg, initial_setup);
2115b02038faSJohn Youn 	if (retval)
2116b02038faSJohn Youn 		return retval;
2117b02038faSJohn Youn 
2118b02038faSJohn Youn 	/* Program the GAHBCFG Register */
2119b02038faSJohn Youn 	retval = dwc2_gahbcfg_init(hsotg);
2120b02038faSJohn Youn 	if (retval)
2121b02038faSJohn Youn 		return retval;
2122b02038faSJohn Youn 
2123b02038faSJohn Youn 	/* Program the GUSBCFG register */
2124b02038faSJohn Youn 	dwc2_gusbcfg_init(hsotg);
2125b02038faSJohn Youn 
2126b02038faSJohn Youn 	/* Program the GOTGCTL register */
2127f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2128b02038faSJohn Youn 	otgctl &= ~GOTGCTL_OTGVER;
2129f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2130b02038faSJohn Youn 
2131b02038faSJohn Youn 	/* Clear the SRP success bit for FS-I2c */
2132b02038faSJohn Youn 	hsotg->srp_success = 0;
2133b02038faSJohn Youn 
2134b02038faSJohn Youn 	/* Enable common interrupts */
2135b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
2136b02038faSJohn Youn 
2137b02038faSJohn Youn 	/*
2138b02038faSJohn Youn 	 * Do device or host initialization based on mode during PCD and
2139b02038faSJohn Youn 	 * HCD initialization
2140b02038faSJohn Youn 	 */
2141b02038faSJohn Youn 	if (dwc2_is_host_mode(hsotg)) {
2142b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Host Mode\n");
2143b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_A_HOST;
2144b02038faSJohn Youn 	} else {
2145b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Device Mode\n");
2146b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2147b02038faSJohn Youn 	}
2148b02038faSJohn Youn 
2149b02038faSJohn Youn 	return 0;
2150b02038faSJohn Youn }
2151b02038faSJohn Youn 
2152b02038faSJohn Youn /**
2153b02038faSJohn Youn  * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2154b02038faSJohn Youn  * Host mode
2155b02038faSJohn Youn  *
2156b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
2157b02038faSJohn Youn  *
2158b02038faSJohn Youn  * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2159b02038faSJohn Youn  * request queues. Host channels are reset to ensure that they are ready for
2160b02038faSJohn Youn  * performing transfers.
2161b02038faSJohn Youn  */
2162b02038faSJohn Youn static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2163b02038faSJohn Youn {
216492a8dd26SMinas Harutyunyan 	u32 hcfg, hfir, otgctl, usbcfg;
2165b02038faSJohn Youn 
2166b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2167b02038faSJohn Youn 
216892a8dd26SMinas Harutyunyan 	/* Set HS/FS Timeout Calibration to 7 (max available value).
216992a8dd26SMinas Harutyunyan 	 * The number of PHY clocks that the application programs in
217092a8dd26SMinas Harutyunyan 	 * this field is added to the high/full speed interpacket timeout
217192a8dd26SMinas Harutyunyan 	 * duration in the core to account for any additional delays
217292a8dd26SMinas Harutyunyan 	 * introduced by the PHY. This can be required, because the delay
217392a8dd26SMinas Harutyunyan 	 * introduced by the PHY in generating the linestate condition
217492a8dd26SMinas Harutyunyan 	 * can vary from one PHY to another.
217592a8dd26SMinas Harutyunyan 	 */
2176f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
217792a8dd26SMinas Harutyunyan 	usbcfg |= GUSBCFG_TOUTCAL(7);
2178f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
217992a8dd26SMinas Harutyunyan 
2180b02038faSJohn Youn 	/* Restart the Phy Clock */
2181f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, PCGCTL);
2182b02038faSJohn Youn 
2183b02038faSJohn Youn 	/* Initialize Host Configuration Register */
2184b02038faSJohn Youn 	dwc2_init_fs_ls_pclk_sel(hsotg);
218538e9002bSVardan Mikayelyan 	if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
218638e9002bSVardan Mikayelyan 	    hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
2187f25c42b8SGevorg Sahakyan 		hcfg = dwc2_readl(hsotg, HCFG);
2188b02038faSJohn Youn 		hcfg |= HCFG_FSLSSUPP;
2189f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcfg, HCFG);
2190b02038faSJohn Youn 	}
2191b02038faSJohn Youn 
2192b02038faSJohn Youn 	/*
2193b02038faSJohn Youn 	 * This bit allows dynamic reloading of the HFIR register during
2194b02038faSJohn Youn 	 * runtime. This bit needs to be programmed during initial configuration
2195b02038faSJohn Youn 	 * and its value must not be changed during runtime.
2196b02038faSJohn Youn 	 */
219795832c00SJohn Youn 	if (hsotg->params.reload_ctl) {
2198f25c42b8SGevorg Sahakyan 		hfir = dwc2_readl(hsotg, HFIR);
2199b02038faSJohn Youn 		hfir |= HFIR_RLDCTRL;
2200f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hfir, HFIR);
2201b02038faSJohn Youn 	}
2202b02038faSJohn Youn 
220395832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2204b02038faSJohn Youn 		u32 op_mode = hsotg->hw_params.op_mode;
2205b02038faSJohn Youn 
2206b02038faSJohn Youn 		if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2207b02038faSJohn Youn 		    !hsotg->hw_params.dma_desc_enable ||
2208b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2209b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2210b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2211b02038faSJohn Youn 			dev_err(hsotg->dev,
2212b02038faSJohn Youn 				"Hardware does not support descriptor DMA mode -\n");
2213b02038faSJohn Youn 			dev_err(hsotg->dev,
2214b02038faSJohn Youn 				"falling back to buffer DMA mode.\n");
221595832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
2216b02038faSJohn Youn 		} else {
2217f25c42b8SGevorg Sahakyan 			hcfg = dwc2_readl(hsotg, HCFG);
2218b02038faSJohn Youn 			hcfg |= HCFG_DESCDMA;
2219f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcfg, HCFG);
2220b02038faSJohn Youn 		}
2221b02038faSJohn Youn 	}
2222b02038faSJohn Youn 
2223b02038faSJohn Youn 	/* Configure data FIFO sizes */
2224b02038faSJohn Youn 	dwc2_config_fifos(hsotg);
2225b02038faSJohn Youn 
2226b02038faSJohn Youn 	/* TODO - check this */
2227b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2228f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2229b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2230f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2231b02038faSJohn Youn 
2232b02038faSJohn Youn 	/* Make sure the FIFOs are flushed */
2233b02038faSJohn Youn 	dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2234b02038faSJohn Youn 	dwc2_flush_rx_fifo(hsotg);
2235b02038faSJohn Youn 
2236b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2237f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2238b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2239f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2240b02038faSJohn Youn 
224195832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
2242b02038faSJohn Youn 		int num_channels, i;
2243b02038faSJohn Youn 		u32 hcchar;
2244b02038faSJohn Youn 
2245b02038faSJohn Youn 		/* Flush out any leftover queued requests */
2246bea8e86cSJohn Youn 		num_channels = hsotg->params.host_channels;
2247b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2248f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22495799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2250b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
2251b02038faSJohn Youn 				hcchar |= HCCHAR_CHDIS;
2252b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2253f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2254b02038faSJohn Youn 			}
22555799aecdSMinas Harutyunyan 		}
2256b02038faSJohn Youn 
2257b02038faSJohn Youn 		/* Halt all channels to put them into a known state */
2258b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2259f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22605799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2261b02038faSJohn Youn 				hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2262b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2263f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2264b02038faSJohn Youn 				dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2265b02038faSJohn Youn 					__func__, i);
226679d6b8c5SSevak Arakelyan 
226779d6b8c5SSevak Arakelyan 				if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
22685799aecdSMinas Harutyunyan 							      HCCHAR_CHENA,
22695799aecdSMinas Harutyunyan 							      1000)) {
22705799aecdSMinas Harutyunyan 					dev_warn(hsotg->dev,
22715799aecdSMinas Harutyunyan 						 "Unable to clear enable on channel %d\n",
2272b02038faSJohn Youn 						 i);
2273b02038faSJohn Youn 				}
2274b02038faSJohn Youn 			}
2275b02038faSJohn Youn 		}
22765799aecdSMinas Harutyunyan 	}
2277b02038faSJohn Youn 
227866e77a24SRazmik Karapetyan 	/* Enable ACG feature in host mode, if supported */
227966e77a24SRazmik Karapetyan 	dwc2_enable_acg(hsotg);
228066e77a24SRazmik Karapetyan 
2281b02038faSJohn Youn 	/* Turn on the vbus power */
2282b02038faSJohn Youn 	dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2283b02038faSJohn Youn 	if (hsotg->op_state == OTG_STATE_A_HOST) {
2284b02038faSJohn Youn 		u32 hprt0 = dwc2_read_hprt0(hsotg);
2285b02038faSJohn Youn 
2286b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2287b02038faSJohn Youn 			!!(hprt0 & HPRT0_PWR));
2288b02038faSJohn Youn 		if (!(hprt0 & HPRT0_PWR)) {
2289b02038faSJohn Youn 			hprt0 |= HPRT0_PWR;
2290f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
2291b02038faSJohn Youn 		}
2292b02038faSJohn Youn 	}
2293b02038faSJohn Youn 
2294b02038faSJohn Youn 	dwc2_enable_host_interrupts(hsotg);
2295b02038faSJohn Youn }
2296b02038faSJohn Youn 
2297197ba5f4SPaul Zimmerman /*
2298197ba5f4SPaul Zimmerman  * Initializes dynamic portions of the DWC_otg HCD state
2299197ba5f4SPaul Zimmerman  *
2300197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2301197ba5f4SPaul Zimmerman  */
2302197ba5f4SPaul Zimmerman static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2303197ba5f4SPaul Zimmerman {
2304197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan, *chan_tmp;
2305197ba5f4SPaul Zimmerman 	int num_channels;
2306197ba5f4SPaul Zimmerman 	int i;
2307197ba5f4SPaul Zimmerman 
2308197ba5f4SPaul Zimmerman 	hsotg->flags.d32 = 0;
2309197ba5f4SPaul Zimmerman 	hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
2310197ba5f4SPaul Zimmerman 
231195832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
2312197ba5f4SPaul Zimmerman 		hsotg->available_host_channels =
2313bea8e86cSJohn Youn 			hsotg->params.host_channels;
2314197ba5f4SPaul Zimmerman 	} else {
2315197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels = 0;
2316197ba5f4SPaul Zimmerman 		hsotg->periodic_channels = 0;
2317197ba5f4SPaul Zimmerman 	}
2318197ba5f4SPaul Zimmerman 
2319197ba5f4SPaul Zimmerman 	/*
2320197ba5f4SPaul Zimmerman 	 * Put all channels in the free channel list and clean up channel
2321197ba5f4SPaul Zimmerman 	 * states
2322197ba5f4SPaul Zimmerman 	 */
2323197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2324197ba5f4SPaul Zimmerman 				 hc_list_entry)
2325197ba5f4SPaul Zimmerman 		list_del_init(&chan->hc_list_entry);
2326197ba5f4SPaul Zimmerman 
2327bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2328197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
2329197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
2330197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2331197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
2332197ba5f4SPaul Zimmerman 	}
2333197ba5f4SPaul Zimmerman 
2334197ba5f4SPaul Zimmerman 	/* Initialize the DWC core for host mode operation */
2335197ba5f4SPaul Zimmerman 	dwc2_core_host_init(hsotg);
2336197ba5f4SPaul Zimmerman }
2337197ba5f4SPaul Zimmerman 
2338197ba5f4SPaul Zimmerman static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2339197ba5f4SPaul Zimmerman 			       struct dwc2_host_chan *chan,
2340197ba5f4SPaul Zimmerman 			       struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2341197ba5f4SPaul Zimmerman {
2342197ba5f4SPaul Zimmerman 	int hub_addr, hub_port;
2343197ba5f4SPaul Zimmerman 
2344197ba5f4SPaul Zimmerman 	chan->do_split = 1;
2345197ba5f4SPaul Zimmerman 	chan->xact_pos = qtd->isoc_split_pos;
2346197ba5f4SPaul Zimmerman 	chan->complete_split = qtd->complete_split;
2347197ba5f4SPaul Zimmerman 	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2348197ba5f4SPaul Zimmerman 	chan->hub_addr = (u8)hub_addr;
2349197ba5f4SPaul Zimmerman 	chan->hub_port = (u8)hub_port;
2350197ba5f4SPaul Zimmerman }
2351197ba5f4SPaul Zimmerman 
23523bc04e28SDouglas Anderson static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2353197ba5f4SPaul Zimmerman 			      struct dwc2_host_chan *chan,
23543bc04e28SDouglas Anderson 			      struct dwc2_qtd *qtd)
2355197ba5f4SPaul Zimmerman {
2356197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
2357197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
2358197ba5f4SPaul Zimmerman 
2359197ba5f4SPaul Zimmerman 	switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2360197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
2361197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2362197ba5f4SPaul Zimmerman 
2363197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
2364197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
2365197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control setup transaction\n");
2366197ba5f4SPaul Zimmerman 			chan->do_ping = 0;
2367197ba5f4SPaul Zimmerman 			chan->ep_is_in = 0;
2368197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_SETUP;
236995832c00SJohn Youn 			if (hsotg->params.host_dma)
2370197ba5f4SPaul Zimmerman 				chan->xfer_dma = urb->setup_dma;
2371197ba5f4SPaul Zimmerman 			else
2372197ba5f4SPaul Zimmerman 				chan->xfer_buf = urb->setup_packet;
2373197ba5f4SPaul Zimmerman 			chan->xfer_len = 8;
2374197ba5f4SPaul Zimmerman 			break;
2375197ba5f4SPaul Zimmerman 
2376197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
2377197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control data transaction\n");
2378197ba5f4SPaul Zimmerman 			chan->data_pid_start = qtd->data_toggle;
2379197ba5f4SPaul Zimmerman 			break;
2380197ba5f4SPaul Zimmerman 
2381197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_STATUS:
2382197ba5f4SPaul Zimmerman 			/*
2383197ba5f4SPaul Zimmerman 			 * Direction is opposite of data direction or IN if no
2384197ba5f4SPaul Zimmerman 			 * data
2385197ba5f4SPaul Zimmerman 			 */
2386197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control status transaction\n");
2387197ba5f4SPaul Zimmerman 			if (urb->length == 0)
2388197ba5f4SPaul Zimmerman 				chan->ep_is_in = 1;
2389197ba5f4SPaul Zimmerman 			else
2390197ba5f4SPaul Zimmerman 				chan->ep_is_in =
2391197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_out(&urb->pipe_info);
2392197ba5f4SPaul Zimmerman 			if (chan->ep_is_in)
2393197ba5f4SPaul Zimmerman 				chan->do_ping = 0;
2394197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_DATA1;
2395197ba5f4SPaul Zimmerman 			chan->xfer_len = 0;
239695832c00SJohn Youn 			if (hsotg->params.host_dma)
2397197ba5f4SPaul Zimmerman 				chan->xfer_dma = hsotg->status_buf_dma;
2398197ba5f4SPaul Zimmerman 			else
2399197ba5f4SPaul Zimmerman 				chan->xfer_buf = hsotg->status_buf;
2400197ba5f4SPaul Zimmerman 			break;
2401197ba5f4SPaul Zimmerman 		}
2402197ba5f4SPaul Zimmerman 		break;
2403197ba5f4SPaul Zimmerman 
2404197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
2405197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_BULK;
2406197ba5f4SPaul Zimmerman 		break;
2407197ba5f4SPaul Zimmerman 
2408197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
2409197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_INT;
2410197ba5f4SPaul Zimmerman 		break;
2411197ba5f4SPaul Zimmerman 
2412197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
2413197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_ISOC;
241495832c00SJohn Youn 		if (hsotg->params.dma_desc_enable)
2415197ba5f4SPaul Zimmerman 			break;
2416197ba5f4SPaul Zimmerman 
2417197ba5f4SPaul Zimmerman 		frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2418197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
2419197ba5f4SPaul Zimmerman 
242095832c00SJohn Youn 		if (hsotg->params.host_dma) {
2421197ba5f4SPaul Zimmerman 			chan->xfer_dma = urb->dma;
2422197ba5f4SPaul Zimmerman 			chan->xfer_dma += frame_desc->offset +
2423197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2424197ba5f4SPaul Zimmerman 		} else {
2425197ba5f4SPaul Zimmerman 			chan->xfer_buf = urb->buf;
2426197ba5f4SPaul Zimmerman 			chan->xfer_buf += frame_desc->offset +
2427197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2428197ba5f4SPaul Zimmerman 		}
2429197ba5f4SPaul Zimmerman 
2430197ba5f4SPaul Zimmerman 		chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2431197ba5f4SPaul Zimmerman 
2432197ba5f4SPaul Zimmerman 		if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2433197ba5f4SPaul Zimmerman 			if (chan->xfer_len <= 188)
2434197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2435197ba5f4SPaul Zimmerman 			else
2436197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2437197ba5f4SPaul Zimmerman 		}
2438197ba5f4SPaul Zimmerman 		break;
2439197ba5f4SPaul Zimmerman 	}
2440197ba5f4SPaul Zimmerman }
2441197ba5f4SPaul Zimmerman 
2442af424a41SWilliam Wu static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
2443af424a41SWilliam Wu 					    struct dwc2_qh *qh,
2444af424a41SWilliam Wu 					    struct dwc2_host_chan *chan)
2445af424a41SWilliam Wu {
2446af424a41SWilliam Wu 	if (!hsotg->unaligned_cache ||
2447af424a41SWilliam Wu 	    chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
2448af424a41SWilliam Wu 		return -ENOMEM;
2449af424a41SWilliam Wu 
2450af424a41SWilliam Wu 	if (!qh->dw_align_buf) {
2451af424a41SWilliam Wu 		qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
2452af424a41SWilliam Wu 						    GFP_ATOMIC | GFP_DMA);
2453af424a41SWilliam Wu 		if (!qh->dw_align_buf)
2454af424a41SWilliam Wu 			return -ENOMEM;
2455af424a41SWilliam Wu 	}
2456af424a41SWilliam Wu 
2457af424a41SWilliam Wu 	qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
2458af424a41SWilliam Wu 					      DWC2_KMEM_UNALIGNED_BUF_SIZE,
2459af424a41SWilliam Wu 					      DMA_FROM_DEVICE);
2460af424a41SWilliam Wu 
2461af424a41SWilliam Wu 	if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
2462af424a41SWilliam Wu 		dev_err(hsotg->dev, "can't map align_buf\n");
2463af424a41SWilliam Wu 		chan->align_buf = 0;
2464af424a41SWilliam Wu 		return -EINVAL;
2465af424a41SWilliam Wu 	}
2466af424a41SWilliam Wu 
2467af424a41SWilliam Wu 	chan->align_buf = qh->dw_align_buf_dma;
2468af424a41SWilliam Wu 	return 0;
2469af424a41SWilliam Wu }
2470af424a41SWilliam Wu 
24713bc04e28SDouglas Anderson #define DWC2_USB_DMA_ALIGN 4
24723bc04e28SDouglas Anderson 
24733bc04e28SDouglas Anderson static void dwc2_free_dma_aligned_buffer(struct urb *urb)
2474197ba5f4SPaul Zimmerman {
247556406e01SAntti Seppälä 	void *stored_xfer_buffer;
24761e111e88SAntti Seppälä 	size_t length;
2477197ba5f4SPaul Zimmerman 
24783bc04e28SDouglas Anderson 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
24793bc04e28SDouglas Anderson 		return;
2480197ba5f4SPaul Zimmerman 
248156406e01SAntti Seppälä 	/* Restore urb->transfer_buffer from the end of the allocated area */
24824a4863bfSMartin Schiller 	memcpy(&stored_xfer_buffer,
24834a4863bfSMartin Schiller 	       PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
24844a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
24854a4863bfSMartin Schiller 	       sizeof(urb->transfer_buffer));
24863bc04e28SDouglas Anderson 
24871e111e88SAntti Seppälä 	if (usb_urb_dir_in(urb)) {
24881e111e88SAntti Seppälä 		if (usb_pipeisoc(urb->pipe))
24891e111e88SAntti Seppälä 			length = urb->transfer_buffer_length;
24901e111e88SAntti Seppälä 		else
24911e111e88SAntti Seppälä 			length = urb->actual_length;
24921e111e88SAntti Seppälä 
24931e111e88SAntti Seppälä 		memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
24941e111e88SAntti Seppälä 	}
249556406e01SAntti Seppälä 	kfree(urb->transfer_buffer);
249656406e01SAntti Seppälä 	urb->transfer_buffer = stored_xfer_buffer;
24973bc04e28SDouglas Anderson 
24983bc04e28SDouglas Anderson 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2499197ba5f4SPaul Zimmerman }
2500197ba5f4SPaul Zimmerman 
25013bc04e28SDouglas Anderson static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
25023bc04e28SDouglas Anderson {
250356406e01SAntti Seppälä 	void *kmalloc_ptr;
25043bc04e28SDouglas Anderson 	size_t kmalloc_size;
25055dce9555SPaul Zimmerman 
25063bc04e28SDouglas Anderson 	if (urb->num_sgs || urb->sg ||
25073bc04e28SDouglas Anderson 	    urb->transfer_buffer_length == 0 ||
25083bc04e28SDouglas Anderson 	    !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2509197ba5f4SPaul Zimmerman 		return 0;
25103bc04e28SDouglas Anderson 
251156406e01SAntti Seppälä 	/*
251256406e01SAntti Seppälä 	 * Allocate a buffer with enough padding for original transfer_buffer
251356406e01SAntti Seppälä 	 * pointer. This allocation is guaranteed to be aligned properly for
251456406e01SAntti Seppälä 	 * DMA
251556406e01SAntti Seppälä 	 */
25163bc04e28SDouglas Anderson 	kmalloc_size = urb->transfer_buffer_length +
25174a4863bfSMartin Schiller 		(dma_get_cache_alignment() - 1) +
251856406e01SAntti Seppälä 		sizeof(urb->transfer_buffer);
25193bc04e28SDouglas Anderson 
25203bc04e28SDouglas Anderson 	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
25213bc04e28SDouglas Anderson 	if (!kmalloc_ptr)
25223bc04e28SDouglas Anderson 		return -ENOMEM;
25233bc04e28SDouglas Anderson 
252456406e01SAntti Seppälä 	/*
252556406e01SAntti Seppälä 	 * Position value of original urb->transfer_buffer pointer to the end
252656406e01SAntti Seppälä 	 * of allocation for later referencing
252756406e01SAntti Seppälä 	 */
25284a4863bfSMartin Schiller 	memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
25294a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
253056406e01SAntti Seppälä 	       &urb->transfer_buffer, sizeof(urb->transfer_buffer));
253156406e01SAntti Seppälä 
25323bc04e28SDouglas Anderson 	if (usb_urb_dir_out(urb))
253356406e01SAntti Seppälä 		memcpy(kmalloc_ptr, urb->transfer_buffer,
25343bc04e28SDouglas Anderson 		       urb->transfer_buffer_length);
253556406e01SAntti Seppälä 	urb->transfer_buffer = kmalloc_ptr;
25363bc04e28SDouglas Anderson 
25373bc04e28SDouglas Anderson 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
25383bc04e28SDouglas Anderson 
25393bc04e28SDouglas Anderson 	return 0;
25403bc04e28SDouglas Anderson }
25413bc04e28SDouglas Anderson 
25423bc04e28SDouglas Anderson static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
25433bc04e28SDouglas Anderson 				gfp_t mem_flags)
25443bc04e28SDouglas Anderson {
25453bc04e28SDouglas Anderson 	int ret;
25463bc04e28SDouglas Anderson 
25473bc04e28SDouglas Anderson 	/* We assume setup_dma is always aligned; warn if not */
25483bc04e28SDouglas Anderson 	WARN_ON_ONCE(urb->setup_dma &&
25493bc04e28SDouglas Anderson 		     (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
25503bc04e28SDouglas Anderson 
25513bc04e28SDouglas Anderson 	ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
25523bc04e28SDouglas Anderson 	if (ret)
25533bc04e28SDouglas Anderson 		return ret;
25543bc04e28SDouglas Anderson 
25553bc04e28SDouglas Anderson 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
25563bc04e28SDouglas Anderson 	if (ret)
25573bc04e28SDouglas Anderson 		dwc2_free_dma_aligned_buffer(urb);
25583bc04e28SDouglas Anderson 
25593bc04e28SDouglas Anderson 	return ret;
25603bc04e28SDouglas Anderson }
25613bc04e28SDouglas Anderson 
25623bc04e28SDouglas Anderson static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
25633bc04e28SDouglas Anderson {
25643bc04e28SDouglas Anderson 	usb_hcd_unmap_urb_for_dma(hcd, urb);
25653bc04e28SDouglas Anderson 	dwc2_free_dma_aligned_buffer(urb);
2566197ba5f4SPaul Zimmerman }
2567197ba5f4SPaul Zimmerman 
2568197ba5f4SPaul Zimmerman /**
2569197ba5f4SPaul Zimmerman  * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2570197ba5f4SPaul Zimmerman  * channel and initializes the host channel to perform the transactions. The
2571197ba5f4SPaul Zimmerman  * host channel is removed from the free list.
2572197ba5f4SPaul Zimmerman  *
2573197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2574197ba5f4SPaul Zimmerman  * @qh:    Transactions from the first QTD for this QH are selected and assigned
2575197ba5f4SPaul Zimmerman  *         to a free host channel
2576197ba5f4SPaul Zimmerman  */
2577197ba5f4SPaul Zimmerman static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
2578197ba5f4SPaul Zimmerman {
2579197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
2580197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
2581197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
2582197ba5f4SPaul Zimmerman 
2583197ba5f4SPaul Zimmerman 	if (dbg_qh(qh))
2584197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
2585197ba5f4SPaul Zimmerman 
2586197ba5f4SPaul Zimmerman 	if (list_empty(&qh->qtd_list)) {
2587197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No QTDs in QH list\n");
2588197ba5f4SPaul Zimmerman 		return -ENOMEM;
2589197ba5f4SPaul Zimmerman 	}
2590197ba5f4SPaul Zimmerman 
2591197ba5f4SPaul Zimmerman 	if (list_empty(&hsotg->free_hc_list)) {
2592197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No free channel to assign\n");
2593197ba5f4SPaul Zimmerman 		return -ENOMEM;
2594197ba5f4SPaul Zimmerman 	}
2595197ba5f4SPaul Zimmerman 
2596197ba5f4SPaul Zimmerman 	chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2597197ba5f4SPaul Zimmerman 				hc_list_entry);
2598197ba5f4SPaul Zimmerman 
2599197ba5f4SPaul Zimmerman 	/* Remove host channel from free list */
2600197ba5f4SPaul Zimmerman 	list_del_init(&chan->hc_list_entry);
2601197ba5f4SPaul Zimmerman 
2602197ba5f4SPaul Zimmerman 	qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2603197ba5f4SPaul Zimmerman 	urb = qtd->urb;
2604197ba5f4SPaul Zimmerman 	qh->channel = chan;
2605197ba5f4SPaul Zimmerman 	qtd->in_process = 1;
2606197ba5f4SPaul Zimmerman 
2607197ba5f4SPaul Zimmerman 	/*
2608197ba5f4SPaul Zimmerman 	 * Use usb_pipedevice to determine device address. This address is
2609197ba5f4SPaul Zimmerman 	 * 0 before the SET_ADDRESS command and the correct address afterward.
2610197ba5f4SPaul Zimmerman 	 */
2611197ba5f4SPaul Zimmerman 	chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2612197ba5f4SPaul Zimmerman 	chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2613197ba5f4SPaul Zimmerman 	chan->speed = qh->dev_speed;
2614babd1839SDouglas Anderson 	chan->max_packet = qh->maxp;
2615197ba5f4SPaul Zimmerman 
2616197ba5f4SPaul Zimmerman 	chan->xfer_started = 0;
2617197ba5f4SPaul Zimmerman 	chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2618197ba5f4SPaul Zimmerman 	chan->error_state = (qtd->error_count > 0);
2619197ba5f4SPaul Zimmerman 	chan->halt_on_queue = 0;
2620197ba5f4SPaul Zimmerman 	chan->halt_pending = 0;
2621197ba5f4SPaul Zimmerman 	chan->requests = 0;
2622197ba5f4SPaul Zimmerman 
2623197ba5f4SPaul Zimmerman 	/*
2624197ba5f4SPaul Zimmerman 	 * The following values may be modified in the transfer type section
2625197ba5f4SPaul Zimmerman 	 * below. The xfer_len value may be reduced when the transfer is
2626197ba5f4SPaul Zimmerman 	 * started to accommodate the max widths of the XferSize and PktCnt
2627197ba5f4SPaul Zimmerman 	 * fields in the HCTSIZn register.
2628197ba5f4SPaul Zimmerman 	 */
2629197ba5f4SPaul Zimmerman 
2630197ba5f4SPaul Zimmerman 	chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2631197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
2632197ba5f4SPaul Zimmerman 		chan->do_ping = 0;
2633197ba5f4SPaul Zimmerman 	else
2634197ba5f4SPaul Zimmerman 		chan->do_ping = qh->ping_state;
2635197ba5f4SPaul Zimmerman 
2636197ba5f4SPaul Zimmerman 	chan->data_pid_start = qh->data_toggle;
2637197ba5f4SPaul Zimmerman 	chan->multi_count = 1;
2638197ba5f4SPaul Zimmerman 
2639197ba5f4SPaul Zimmerman 	if (urb->actual_length > urb->length &&
2640197ba5f4SPaul Zimmerman 	    !dwc2_hcd_is_pipe_in(&urb->pipe_info))
2641197ba5f4SPaul Zimmerman 		urb->actual_length = urb->length;
2642197ba5f4SPaul Zimmerman 
264395832c00SJohn Youn 	if (hsotg->params.host_dma)
2644197ba5f4SPaul Zimmerman 		chan->xfer_dma = urb->dma + urb->actual_length;
26453bc04e28SDouglas Anderson 	else
2646197ba5f4SPaul Zimmerman 		chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
2647197ba5f4SPaul Zimmerman 
2648197ba5f4SPaul Zimmerman 	chan->xfer_len = urb->length - urb->actual_length;
2649197ba5f4SPaul Zimmerman 	chan->xfer_count = 0;
2650197ba5f4SPaul Zimmerman 
2651197ba5f4SPaul Zimmerman 	/* Set the split attributes if required */
2652197ba5f4SPaul Zimmerman 	if (qh->do_split)
2653197ba5f4SPaul Zimmerman 		dwc2_hc_init_split(hsotg, chan, qtd, urb);
2654197ba5f4SPaul Zimmerman 	else
2655197ba5f4SPaul Zimmerman 		chan->do_split = 0;
2656197ba5f4SPaul Zimmerman 
2657197ba5f4SPaul Zimmerman 	/* Set the transfer attributes */
26583bc04e28SDouglas Anderson 	dwc2_hc_init_xfer(hsotg, chan, qtd);
2659197ba5f4SPaul Zimmerman 
2660af424a41SWilliam Wu 	/* For non-dword aligned buffers */
2661af424a41SWilliam Wu 	if (hsotg->params.host_dma && qh->do_split &&
2662af424a41SWilliam Wu 	    chan->ep_is_in && (chan->xfer_dma & 0x3)) {
2663af424a41SWilliam Wu 		dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
2664af424a41SWilliam Wu 		if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
2665af424a41SWilliam Wu 			dev_err(hsotg->dev,
2666af424a41SWilliam Wu 				"Failed to allocate memory to handle non-aligned buffer\n");
2667af424a41SWilliam Wu 			/* Add channel back to free list */
2668af424a41SWilliam Wu 			chan->align_buf = 0;
2669af424a41SWilliam Wu 			chan->multi_count = 0;
2670af424a41SWilliam Wu 			list_add_tail(&chan->hc_list_entry,
2671af424a41SWilliam Wu 				      &hsotg->free_hc_list);
2672af424a41SWilliam Wu 			qtd->in_process = 0;
2673af424a41SWilliam Wu 			qh->channel = NULL;
2674af424a41SWilliam Wu 			return -ENOMEM;
2675af424a41SWilliam Wu 		}
2676af424a41SWilliam Wu 	} else {
2677af424a41SWilliam Wu 		/*
2678af424a41SWilliam Wu 		 * We assume that DMA is always aligned in non-split
2679af424a41SWilliam Wu 		 * case or split out case. Warn if not.
2680af424a41SWilliam Wu 		 */
2681af424a41SWilliam Wu 		WARN_ON_ONCE(hsotg->params.host_dma &&
2682af424a41SWilliam Wu 			     (chan->xfer_dma & 0x3));
2683af424a41SWilliam Wu 		chan->align_buf = 0;
2684af424a41SWilliam Wu 	}
2685af424a41SWilliam Wu 
2686197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2687197ba5f4SPaul Zimmerman 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2688197ba5f4SPaul Zimmerman 		/*
2689197ba5f4SPaul Zimmerman 		 * This value may be modified when the transfer is started
2690197ba5f4SPaul Zimmerman 		 * to reflect the actual transfer length
2691197ba5f4SPaul Zimmerman 		 */
2692babd1839SDouglas Anderson 		chan->multi_count = qh->maxp_mult;
2693197ba5f4SPaul Zimmerman 
269495832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2695197ba5f4SPaul Zimmerman 		chan->desc_list_addr = qh->desc_list_dma;
269695105a99SGregory Herrero 		chan->desc_list_sz = qh->desc_list_sz;
269795105a99SGregory Herrero 	}
2698197ba5f4SPaul Zimmerman 
2699197ba5f4SPaul Zimmerman 	dwc2_hc_init(hsotg, chan);
2700197ba5f4SPaul Zimmerman 	chan->qh = qh;
2701197ba5f4SPaul Zimmerman 
2702197ba5f4SPaul Zimmerman 	return 0;
2703197ba5f4SPaul Zimmerman }
2704197ba5f4SPaul Zimmerman 
2705197ba5f4SPaul Zimmerman /**
2706197ba5f4SPaul Zimmerman  * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2707197ba5f4SPaul Zimmerman  * schedule and assigns them to available host channels. Called from the HCD
2708197ba5f4SPaul Zimmerman  * interrupt handler functions.
2709197ba5f4SPaul Zimmerman  *
2710197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2711197ba5f4SPaul Zimmerman  *
2712197ba5f4SPaul Zimmerman  * Return: The types of new transactions that were assigned to host channels
2713197ba5f4SPaul Zimmerman  */
2714197ba5f4SPaul Zimmerman enum dwc2_transaction_type dwc2_hcd_select_transactions(
2715197ba5f4SPaul Zimmerman 		struct dwc2_hsotg *hsotg)
2716197ba5f4SPaul Zimmerman {
2717197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2718197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2719197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2720197ba5f4SPaul Zimmerman 	int num_channels;
2721197ba5f4SPaul Zimmerman 
2722197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
2723197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Select Transactions\n");
2724197ba5f4SPaul Zimmerman #endif
2725197ba5f4SPaul Zimmerman 
2726197ba5f4SPaul Zimmerman 	/* Process entries in the periodic ready list */
2727197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_ready.next;
2728197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_ready) {
2729197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2730197ba5f4SPaul Zimmerman 			break;
273195832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2732197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels <= 1)
2733197ba5f4SPaul Zimmerman 				break;
2734197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2735197ba5f4SPaul Zimmerman 		}
2736197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2737197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2738197ba5f4SPaul Zimmerman 			break;
2739197ba5f4SPaul Zimmerman 
2740197ba5f4SPaul Zimmerman 		/*
2741197ba5f4SPaul Zimmerman 		 * Move the QH from the periodic ready schedule to the
2742197ba5f4SPaul Zimmerman 		 * periodic assigned schedule
2743197ba5f4SPaul Zimmerman 		 */
2744197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
274594ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
274694ef7aeeSDouglas Anderson 			       &hsotg->periodic_sched_assigned);
2747197ba5f4SPaul Zimmerman 		ret_val = DWC2_TRANSACTION_PERIODIC;
2748197ba5f4SPaul Zimmerman 	}
2749197ba5f4SPaul Zimmerman 
2750197ba5f4SPaul Zimmerman 	/*
2751197ba5f4SPaul Zimmerman 	 * Process entries in the inactive portion of the non-periodic
2752197ba5f4SPaul Zimmerman 	 * schedule. Some free host channels may not be used if they are
2753197ba5f4SPaul Zimmerman 	 * reserved for periodic transfers.
2754197ba5f4SPaul Zimmerman 	 */
2755bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2756197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->non_periodic_sched_inactive.next;
2757197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
275895832c00SJohn Youn 		if (!hsotg->params.uframe_sched &&
2759197ba5f4SPaul Zimmerman 		    hsotg->non_periodic_channels >= num_channels -
2760197ba5f4SPaul Zimmerman 						hsotg->periodic_channels)
2761197ba5f4SPaul Zimmerman 			break;
2762197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2763197ba5f4SPaul Zimmerman 			break;
2764197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
276595832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2766197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels < 1)
2767197ba5f4SPaul Zimmerman 				break;
2768197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2769197ba5f4SPaul Zimmerman 		}
2770197ba5f4SPaul Zimmerman 
2771197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2772197ba5f4SPaul Zimmerman 			break;
2773197ba5f4SPaul Zimmerman 
2774197ba5f4SPaul Zimmerman 		/*
2775197ba5f4SPaul Zimmerman 		 * Move the QH from the non-periodic inactive schedule to the
2776197ba5f4SPaul Zimmerman 		 * non-periodic active schedule
2777197ba5f4SPaul Zimmerman 		 */
2778197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
277994ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
2780197ba5f4SPaul Zimmerman 			       &hsotg->non_periodic_sched_active);
2781197ba5f4SPaul Zimmerman 
2782197ba5f4SPaul Zimmerman 		if (ret_val == DWC2_TRANSACTION_NONE)
2783197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2784197ba5f4SPaul Zimmerman 		else
2785197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_ALL;
2786197ba5f4SPaul Zimmerman 
278795832c00SJohn Youn 		if (!hsotg->params.uframe_sched)
2788197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels++;
2789197ba5f4SPaul Zimmerman 	}
2790197ba5f4SPaul Zimmerman 
2791197ba5f4SPaul Zimmerman 	return ret_val;
2792197ba5f4SPaul Zimmerman }
2793197ba5f4SPaul Zimmerman 
2794197ba5f4SPaul Zimmerman /**
2795197ba5f4SPaul Zimmerman  * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2796197ba5f4SPaul Zimmerman  * a host channel associated with either a periodic or non-periodic transfer
2797197ba5f4SPaul Zimmerman  *
2798197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2799197ba5f4SPaul Zimmerman  * @chan:  Host channel descriptor associated with either a periodic or
2800197ba5f4SPaul Zimmerman  *         non-periodic transfer
2801197ba5f4SPaul Zimmerman  * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2802197ba5f4SPaul Zimmerman  *                     for periodic transfers or the non-periodic Tx FIFO
2803197ba5f4SPaul Zimmerman  *                     for non-periodic transfers
2804197ba5f4SPaul Zimmerman  *
2805197ba5f4SPaul Zimmerman  * Return: 1 if a request is queued and more requests may be needed to
2806197ba5f4SPaul Zimmerman  * complete the transfer, 0 if no more requests are required for this
2807197ba5f4SPaul Zimmerman  * transfer, -1 if there is insufficient space in the Tx FIFO
2808197ba5f4SPaul Zimmerman  *
2809197ba5f4SPaul Zimmerman  * This function assumes that there is space available in the appropriate
2810197ba5f4SPaul Zimmerman  * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2811197ba5f4SPaul Zimmerman  * it checks whether space is available in the appropriate Tx FIFO.
2812197ba5f4SPaul Zimmerman  *
2813197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2814197ba5f4SPaul Zimmerman  */
2815197ba5f4SPaul Zimmerman static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2816197ba5f4SPaul Zimmerman 				  struct dwc2_host_chan *chan,
2817197ba5f4SPaul Zimmerman 				  u16 fifo_dwords_avail)
2818197ba5f4SPaul Zimmerman {
2819197ba5f4SPaul Zimmerman 	int retval = 0;
2820197ba5f4SPaul Zimmerman 
2821c9c8ac01SDouglas Anderson 	if (chan->do_split)
2822c9c8ac01SDouglas Anderson 		/* Put ourselves on the list to keep order straight */
2823c9c8ac01SDouglas Anderson 		list_move_tail(&chan->split_order_list_entry,
2824c9c8ac01SDouglas Anderson 			       &hsotg->split_order);
2825c9c8ac01SDouglas Anderson 
28267b813767SAlexandru M Stan 	if (hsotg->params.host_dma && chan->qh) {
282795832c00SJohn Youn 		if (hsotg->params.dma_desc_enable) {
2828197ba5f4SPaul Zimmerman 			if (!chan->xfer_started ||
2829197ba5f4SPaul Zimmerman 			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2830197ba5f4SPaul Zimmerman 				dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2831197ba5f4SPaul Zimmerman 				chan->qh->ping_state = 0;
2832197ba5f4SPaul Zimmerman 			}
2833197ba5f4SPaul Zimmerman 		} else if (!chan->xfer_started) {
2834197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2835197ba5f4SPaul Zimmerman 			chan->qh->ping_state = 0;
2836197ba5f4SPaul Zimmerman 		}
2837197ba5f4SPaul Zimmerman 	} else if (chan->halt_pending) {
2838197ba5f4SPaul Zimmerman 		/* Don't queue a request if the channel has been halted */
2839197ba5f4SPaul Zimmerman 	} else if (chan->halt_on_queue) {
2840197ba5f4SPaul Zimmerman 		dwc2_hc_halt(hsotg, chan, chan->halt_status);
2841197ba5f4SPaul Zimmerman 	} else if (chan->do_ping) {
2842197ba5f4SPaul Zimmerman 		if (!chan->xfer_started)
2843197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2844197ba5f4SPaul Zimmerman 	} else if (!chan->ep_is_in ||
2845197ba5f4SPaul Zimmerman 		   chan->data_pid_start == DWC2_HC_PID_SETUP) {
2846197ba5f4SPaul Zimmerman 		if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2847197ba5f4SPaul Zimmerman 			if (!chan->xfer_started) {
2848197ba5f4SPaul Zimmerman 				dwc2_hc_start_transfer(hsotg, chan);
2849197ba5f4SPaul Zimmerman 				retval = 1;
2850197ba5f4SPaul Zimmerman 			} else {
2851197ba5f4SPaul Zimmerman 				retval = dwc2_hc_continue_transfer(hsotg, chan);
2852197ba5f4SPaul Zimmerman 			}
2853197ba5f4SPaul Zimmerman 		} else {
2854197ba5f4SPaul Zimmerman 			retval = -1;
2855197ba5f4SPaul Zimmerman 		}
2856197ba5f4SPaul Zimmerman 	} else {
2857197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
2858197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2859197ba5f4SPaul Zimmerman 			retval = 1;
2860197ba5f4SPaul Zimmerman 		} else {
2861197ba5f4SPaul Zimmerman 			retval = dwc2_hc_continue_transfer(hsotg, chan);
2862197ba5f4SPaul Zimmerman 		}
2863197ba5f4SPaul Zimmerman 	}
2864197ba5f4SPaul Zimmerman 
2865197ba5f4SPaul Zimmerman 	return retval;
2866197ba5f4SPaul Zimmerman }
2867197ba5f4SPaul Zimmerman 
2868197ba5f4SPaul Zimmerman /*
2869197ba5f4SPaul Zimmerman  * Processes periodic channels for the next frame and queues transactions for
2870197ba5f4SPaul Zimmerman  * these channels to the DWC_otg controller. After queueing transactions, the
2871197ba5f4SPaul Zimmerman  * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2872197ba5f4SPaul Zimmerman  * to queue as Periodic Tx FIFO or request queue space becomes available.
2873197ba5f4SPaul Zimmerman  * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2874197ba5f4SPaul Zimmerman  *
2875197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2876197ba5f4SPaul Zimmerman  */
2877197ba5f4SPaul Zimmerman static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2878197ba5f4SPaul Zimmerman {
2879197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2880197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2881197ba5f4SPaul Zimmerman 	u32 tx_status;
2882197ba5f4SPaul Zimmerman 	u32 fspcavail;
2883197ba5f4SPaul Zimmerman 	u32 gintmsk;
2884197ba5f4SPaul Zimmerman 	int status;
28854e50e011SDouglas Anderson 	bool no_queue_space = false;
28864e50e011SDouglas Anderson 	bool no_fifo_space = false;
2887197ba5f4SPaul Zimmerman 	u32 qspcavail;
2888197ba5f4SPaul Zimmerman 
28894e50e011SDouglas Anderson 	/* If empty list then just adjust interrupt enables */
28904e50e011SDouglas Anderson 	if (list_empty(&hsotg->periodic_sched_assigned))
28914e50e011SDouglas Anderson 		goto exit;
28924e50e011SDouglas Anderson 
2893197ba5f4SPaul Zimmerman 	if (dbg_perio())
2894197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
2895197ba5f4SPaul Zimmerman 
2896f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, HPTXSTS);
2897197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2898197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
2899197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2900197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
2901197ba5f4SPaul Zimmerman 
2902197ba5f4SPaul Zimmerman 	if (dbg_perio()) {
2903197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx Req Queue Space Avail (before queue): %d\n",
2904197ba5f4SPaul Zimmerman 			 qspcavail);
2905197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx FIFO Space Avail (before queue): %d\n",
2906197ba5f4SPaul Zimmerman 			 fspcavail);
2907197ba5f4SPaul Zimmerman 	}
2908197ba5f4SPaul Zimmerman 
2909197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_assigned.next;
2910197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_assigned) {
2911f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, HPTXSTS);
2912197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2913197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
2914197ba5f4SPaul Zimmerman 		if (qspcavail == 0) {
2915fdb09b3eSNicholas Mc Guire 			no_queue_space = true;
2916197ba5f4SPaul Zimmerman 			break;
2917197ba5f4SPaul Zimmerman 		}
2918197ba5f4SPaul Zimmerman 
2919197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2920197ba5f4SPaul Zimmerman 		if (!qh->channel) {
2921197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2922197ba5f4SPaul Zimmerman 			continue;
2923197ba5f4SPaul Zimmerman 		}
2924197ba5f4SPaul Zimmerman 
2925197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
2926197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty) {
2927197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2928197ba5f4SPaul Zimmerman 			continue;
2929197ba5f4SPaul Zimmerman 		}
2930197ba5f4SPaul Zimmerman 
2931197ba5f4SPaul Zimmerman 		/*
2932197ba5f4SPaul Zimmerman 		 * Set a flag if we're queuing high-bandwidth in slave mode.
2933197ba5f4SPaul Zimmerman 		 * The flag prevents any halts to get into the request queue in
2934197ba5f4SPaul Zimmerman 		 * the middle of multiple high-bandwidth packets getting queued.
2935197ba5f4SPaul Zimmerman 		 */
293695832c00SJohn Youn 		if (!hsotg->params.host_dma &&
2937197ba5f4SPaul Zimmerman 		    qh->channel->multi_count > 1)
2938197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 1;
2939197ba5f4SPaul Zimmerman 
2940197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2941197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
2942197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
2943197ba5f4SPaul Zimmerman 		if (status < 0) {
2944fdb09b3eSNicholas Mc Guire 			no_fifo_space = true;
2945197ba5f4SPaul Zimmerman 			break;
2946197ba5f4SPaul Zimmerman 		}
2947197ba5f4SPaul Zimmerman 
2948197ba5f4SPaul Zimmerman 		/*
2949197ba5f4SPaul Zimmerman 		 * In Slave mode, stay on the current transfer until there is
2950197ba5f4SPaul Zimmerman 		 * nothing more to do or the high-bandwidth request count is
2951197ba5f4SPaul Zimmerman 		 * reached. In DMA mode, only need to queue one request. The
2952197ba5f4SPaul Zimmerman 		 * controller automatically handles multiple packets for
2953197ba5f4SPaul Zimmerman 		 * high-bandwidth transfers.
2954197ba5f4SPaul Zimmerman 		 */
295595832c00SJohn Youn 		if (hsotg->params.host_dma || status == 0 ||
2956197ba5f4SPaul Zimmerman 		    qh->channel->requests == qh->channel->multi_count) {
2957197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2958197ba5f4SPaul Zimmerman 			/*
2959197ba5f4SPaul Zimmerman 			 * Move the QH from the periodic assigned schedule to
2960197ba5f4SPaul Zimmerman 			 * the periodic queued schedule
2961197ba5f4SPaul Zimmerman 			 */
296294ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
2963197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_queued);
2964197ba5f4SPaul Zimmerman 
2965197ba5f4SPaul Zimmerman 			/* done queuing high bandwidth */
2966197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 0;
2967197ba5f4SPaul Zimmerman 		}
2968197ba5f4SPaul Zimmerman 	}
2969197ba5f4SPaul Zimmerman 
29704e50e011SDouglas Anderson exit:
29714e50e011SDouglas Anderson 	if (no_queue_space || no_fifo_space ||
297295832c00SJohn Youn 	    (!hsotg->params.host_dma &&
29734e50e011SDouglas Anderson 	     !list_empty(&hsotg->periodic_sched_assigned))) {
2974197ba5f4SPaul Zimmerman 		/*
2975197ba5f4SPaul Zimmerman 		 * May need to queue more transactions as the request
2976197ba5f4SPaul Zimmerman 		 * queue or Tx FIFO empties. Enable the periodic Tx
2977197ba5f4SPaul Zimmerman 		 * FIFO empty interrupt. (Always use the half-empty
2978197ba5f4SPaul Zimmerman 		 * level to ensure that new requests are loaded as
2979197ba5f4SPaul Zimmerman 		 * soon as possible.)
2980197ba5f4SPaul Zimmerman 		 */
2981f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29824e50e011SDouglas Anderson 		if (!(gintmsk & GINTSTS_PTXFEMP)) {
2983197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_PTXFEMP;
2984f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
29854e50e011SDouglas Anderson 		}
2986197ba5f4SPaul Zimmerman 	} else {
2987197ba5f4SPaul Zimmerman 		/*
2988197ba5f4SPaul Zimmerman 		 * Disable the Tx FIFO empty interrupt since there are
2989197ba5f4SPaul Zimmerman 		 * no more transactions that need to be queued right
2990197ba5f4SPaul Zimmerman 		 * now. This function is called from interrupt
2991197ba5f4SPaul Zimmerman 		 * handlers to queue more transactions as transfer
2992197ba5f4SPaul Zimmerman 		 * states change.
2993197ba5f4SPaul Zimmerman 		 */
2994f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29954e50e011SDouglas Anderson 		if (gintmsk & GINTSTS_PTXFEMP) {
2996197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_PTXFEMP;
2997f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
2998197ba5f4SPaul Zimmerman 		}
2999197ba5f4SPaul Zimmerman 	}
3000197ba5f4SPaul Zimmerman }
3001197ba5f4SPaul Zimmerman 
3002197ba5f4SPaul Zimmerman /*
3003197ba5f4SPaul Zimmerman  * Processes active non-periodic channels and queues transactions for these
3004197ba5f4SPaul Zimmerman  * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3005197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is enabled if there are more transactions to queue as
3006197ba5f4SPaul Zimmerman  * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3007197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is disabled.
3008197ba5f4SPaul Zimmerman  *
3009197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3010197ba5f4SPaul Zimmerman  */
3011197ba5f4SPaul Zimmerman static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3012197ba5f4SPaul Zimmerman {
3013197ba5f4SPaul Zimmerman 	struct list_head *orig_qh_ptr;
3014197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
3015197ba5f4SPaul Zimmerman 	u32 tx_status;
3016197ba5f4SPaul Zimmerman 	u32 qspcavail;
3017197ba5f4SPaul Zimmerman 	u32 fspcavail;
3018197ba5f4SPaul Zimmerman 	u32 gintmsk;
3019197ba5f4SPaul Zimmerman 	int status;
3020197ba5f4SPaul Zimmerman 	int no_queue_space = 0;
3021197ba5f4SPaul Zimmerman 	int no_fifo_space = 0;
3022197ba5f4SPaul Zimmerman 	int more_to_do = 0;
3023197ba5f4SPaul Zimmerman 
3024197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3025197ba5f4SPaul Zimmerman 
3026f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, GNPTXSTS);
3027197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3028197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
3029197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3030197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
3031197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx Req Queue Space Avail (before queue): %d\n",
3032197ba5f4SPaul Zimmerman 		 qspcavail);
3033197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx FIFO Space Avail (before queue): %d\n",
3034197ba5f4SPaul Zimmerman 		 fspcavail);
3035197ba5f4SPaul Zimmerman 
3036197ba5f4SPaul Zimmerman 	/*
3037197ba5f4SPaul Zimmerman 	 * Keep track of the starting point. Skip over the start-of-list
3038197ba5f4SPaul Zimmerman 	 * entry.
3039197ba5f4SPaul Zimmerman 	 */
3040197ba5f4SPaul Zimmerman 	if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3041197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3042197ba5f4SPaul Zimmerman 	orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3043197ba5f4SPaul Zimmerman 
3044197ba5f4SPaul Zimmerman 	/*
3045197ba5f4SPaul Zimmerman 	 * Process once through the active list or until no more space is
3046197ba5f4SPaul Zimmerman 	 * available in the request queue or the Tx FIFO
3047197ba5f4SPaul Zimmerman 	 */
3048197ba5f4SPaul Zimmerman 	do {
3049f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3050197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3051197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
305295832c00SJohn Youn 		if (!hsotg->params.host_dma && qspcavail == 0) {
3053197ba5f4SPaul Zimmerman 			no_queue_space = 1;
3054197ba5f4SPaul Zimmerman 			break;
3055197ba5f4SPaul Zimmerman 		}
3056197ba5f4SPaul Zimmerman 
3057197ba5f4SPaul Zimmerman 		qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3058197ba5f4SPaul Zimmerman 				qh_list_entry);
3059197ba5f4SPaul Zimmerman 		if (!qh->channel)
3060197ba5f4SPaul Zimmerman 			goto next;
3061197ba5f4SPaul Zimmerman 
3062197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
3063197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty)
3064197ba5f4SPaul Zimmerman 			goto next;
3065197ba5f4SPaul Zimmerman 
3066197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3067197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3068197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3069197ba5f4SPaul Zimmerman 
3070197ba5f4SPaul Zimmerman 		if (status > 0) {
3071197ba5f4SPaul Zimmerman 			more_to_do = 1;
3072197ba5f4SPaul Zimmerman 		} else if (status < 0) {
3073197ba5f4SPaul Zimmerman 			no_fifo_space = 1;
3074197ba5f4SPaul Zimmerman 			break;
3075197ba5f4SPaul Zimmerman 		}
3076197ba5f4SPaul Zimmerman next:
3077197ba5f4SPaul Zimmerman 		/* Advance to next QH, skipping start-of-list entry */
3078197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3079197ba5f4SPaul Zimmerman 		if (hsotg->non_periodic_qh_ptr ==
3080197ba5f4SPaul Zimmerman 				&hsotg->non_periodic_sched_active)
3081197ba5f4SPaul Zimmerman 			hsotg->non_periodic_qh_ptr =
3082197ba5f4SPaul Zimmerman 					hsotg->non_periodic_qh_ptr->next;
3083197ba5f4SPaul Zimmerman 	} while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3084197ba5f4SPaul Zimmerman 
308595832c00SJohn Youn 	if (!hsotg->params.host_dma) {
3086f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3087197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3088197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
3089197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3090197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3091197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3092197ba5f4SPaul Zimmerman 			 "  NP Tx Req Queue Space Avail (after queue): %d\n",
3093197ba5f4SPaul Zimmerman 			 qspcavail);
3094197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3095197ba5f4SPaul Zimmerman 			 "  NP Tx FIFO Space Avail (after queue): %d\n",
3096197ba5f4SPaul Zimmerman 			 fspcavail);
3097197ba5f4SPaul Zimmerman 
3098197ba5f4SPaul Zimmerman 		if (more_to_do || no_queue_space || no_fifo_space) {
3099197ba5f4SPaul Zimmerman 			/*
3100197ba5f4SPaul Zimmerman 			 * May need to queue more transactions as the request
3101197ba5f4SPaul Zimmerman 			 * queue or Tx FIFO empties. Enable the non-periodic
3102197ba5f4SPaul Zimmerman 			 * Tx FIFO empty interrupt. (Always use the half-empty
3103197ba5f4SPaul Zimmerman 			 * level to ensure that new requests are loaded as
3104197ba5f4SPaul Zimmerman 			 * soon as possible.)
3105197ba5f4SPaul Zimmerman 			 */
3106f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3107197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_NPTXFEMP;
3108f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3109197ba5f4SPaul Zimmerman 		} else {
3110197ba5f4SPaul Zimmerman 			/*
3111197ba5f4SPaul Zimmerman 			 * Disable the Tx FIFO empty interrupt since there are
3112197ba5f4SPaul Zimmerman 			 * no more transactions that need to be queued right
3113197ba5f4SPaul Zimmerman 			 * now. This function is called from interrupt
3114197ba5f4SPaul Zimmerman 			 * handlers to queue more transactions as transfer
3115197ba5f4SPaul Zimmerman 			 * states change.
3116197ba5f4SPaul Zimmerman 			 */
3117f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3118197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3119f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3120197ba5f4SPaul Zimmerman 		}
3121197ba5f4SPaul Zimmerman 	}
3122197ba5f4SPaul Zimmerman }
3123197ba5f4SPaul Zimmerman 
3124197ba5f4SPaul Zimmerman /**
3125197ba5f4SPaul Zimmerman  * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3126197ba5f4SPaul Zimmerman  * and queues transactions for these channels to the DWC_otg controller. Called
3127197ba5f4SPaul Zimmerman  * from the HCD interrupt handler functions.
3128197ba5f4SPaul Zimmerman  *
3129197ba5f4SPaul Zimmerman  * @hsotg:   The HCD state structure
3130197ba5f4SPaul Zimmerman  * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3131197ba5f4SPaul Zimmerman  *           or both)
3132197ba5f4SPaul Zimmerman  *
3133197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3134197ba5f4SPaul Zimmerman  */
3135197ba5f4SPaul Zimmerman void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3136197ba5f4SPaul Zimmerman 				 enum dwc2_transaction_type tr_type)
3137197ba5f4SPaul Zimmerman {
3138197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3139197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue Transactions\n");
3140197ba5f4SPaul Zimmerman #endif
3141197ba5f4SPaul Zimmerman 	/* Process host channels associated with periodic transfers */
31424e50e011SDouglas Anderson 	if (tr_type == DWC2_TRANSACTION_PERIODIC ||
31434e50e011SDouglas Anderson 	    tr_type == DWC2_TRANSACTION_ALL)
3144197ba5f4SPaul Zimmerman 		dwc2_process_periodic_channels(hsotg);
3145197ba5f4SPaul Zimmerman 
3146197ba5f4SPaul Zimmerman 	/* Process host channels associated with non-periodic transfers */
3147197ba5f4SPaul Zimmerman 	if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3148197ba5f4SPaul Zimmerman 	    tr_type == DWC2_TRANSACTION_ALL) {
3149197ba5f4SPaul Zimmerman 		if (!list_empty(&hsotg->non_periodic_sched_active)) {
3150197ba5f4SPaul Zimmerman 			dwc2_process_non_periodic_channels(hsotg);
3151197ba5f4SPaul Zimmerman 		} else {
3152197ba5f4SPaul Zimmerman 			/*
3153197ba5f4SPaul Zimmerman 			 * Ensure NP Tx FIFO empty interrupt is disabled when
3154197ba5f4SPaul Zimmerman 			 * there are no non-periodic transfers to process
3155197ba5f4SPaul Zimmerman 			 */
3156f25c42b8SGevorg Sahakyan 			u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
3157197ba5f4SPaul Zimmerman 
3158197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3159f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3160197ba5f4SPaul Zimmerman 		}
3161197ba5f4SPaul Zimmerman 	}
3162197ba5f4SPaul Zimmerman }
3163197ba5f4SPaul Zimmerman 
3164197ba5f4SPaul Zimmerman static void dwc2_conn_id_status_change(struct work_struct *work)
3165197ba5f4SPaul Zimmerman {
3166197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3167197ba5f4SPaul Zimmerman 						wf_otg);
3168197ba5f4SPaul Zimmerman 	u32 count = 0;
3169197ba5f4SPaul Zimmerman 	u32 gotgctl;
31705390d438SMian Yousaf Kaukab 	unsigned long flags;
3171197ba5f4SPaul Zimmerman 
3172197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3173197ba5f4SPaul Zimmerman 
3174f25c42b8SGevorg Sahakyan 	gotgctl = dwc2_readl(hsotg, GOTGCTL);
3175197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3176197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3177197ba5f4SPaul Zimmerman 		!!(gotgctl & GOTGCTL_CONID_B));
3178197ba5f4SPaul Zimmerman 
3179197ba5f4SPaul Zimmerman 	/* B-Device connector (Device Mode) */
3180197ba5f4SPaul Zimmerman 	if (gotgctl & GOTGCTL_CONID_B) {
3181531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
3182197ba5f4SPaul Zimmerman 		/* Wait for switch to device mode */
3183197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId B\n");
31849156a7efSChen Yu 		if (hsotg->bus_suspended) {
31859156a7efSChen Yu 			dev_info(hsotg->dev,
31869156a7efSChen Yu 				 "Do port resume before switching to device mode\n");
31879156a7efSChen Yu 			dwc2_port_resume(hsotg);
31889156a7efSChen Yu 		}
3189197ba5f4SPaul Zimmerman 		while (!dwc2_is_device_mode(hsotg)) {
3190197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev,
3191197ba5f4SPaul Zimmerman 				 "Waiting for Peripheral Mode, Mode=%s\n",
3192197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ? "Host" :
3193197ba5f4SPaul Zimmerman 				 "Peripheral");
319404a9db79SNicholas Mc Guire 			msleep(20);
3195fc30c4bbSJohn Stultz 			/*
3196fc30c4bbSJohn Stultz 			 * Sometimes the initial GOTGCTRL read is wrong, so
3197fc30c4bbSJohn Stultz 			 * check it again and jump to host mode if that was
3198fc30c4bbSJohn Stultz 			 * the case.
3199fc30c4bbSJohn Stultz 			 */
3200f25c42b8SGevorg Sahakyan 			gotgctl = dwc2_readl(hsotg, GOTGCTL);
3201fc30c4bbSJohn Stultz 			if (!(gotgctl & GOTGCTL_CONID_B))
3202fc30c4bbSJohn Stultz 				goto host;
3203197ba5f4SPaul Zimmerman 			if (++count > 250)
3204197ba5f4SPaul Zimmerman 				break;
3205197ba5f4SPaul Zimmerman 		}
3206197ba5f4SPaul Zimmerman 		if (count > 250)
3207197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3208197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
32094d4d99afSArtur Petrosyan 
32104d4d99afSArtur Petrosyan 		/*
32114d4d99afSArtur Petrosyan 		 * Exit Partial Power Down without restoring registers.
32124d4d99afSArtur Petrosyan 		 * No need to check the return value as registers
32134d4d99afSArtur Petrosyan 		 * are not being restored.
32144d4d99afSArtur Petrosyan 		 */
32154d4d99afSArtur Petrosyan 		if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
32164d4d99afSArtur Petrosyan 			dwc2_exit_partial_power_down(hsotg, 0, false);
32174d4d99afSArtur Petrosyan 
3218197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
32190fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3220197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
32215390d438SMian Yousaf Kaukab 		spin_lock_irqsave(&hsotg->lock, flags);
32221f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
32235390d438SMian Yousaf Kaukab 		spin_unlock_irqrestore(&hsotg->lock, flags);
322466e77a24SRazmik Karapetyan 		/* Enable ACG feature in device mode,if supported */
322566e77a24SRazmik Karapetyan 		dwc2_enable_acg(hsotg);
32261f91b4ccSFelipe Balbi 		dwc2_hsotg_core_connect(hsotg);
3227197ba5f4SPaul Zimmerman 	} else {
3228fc30c4bbSJohn Stultz host:
3229197ba5f4SPaul Zimmerman 		/* A-Device connector (Host Mode) */
3230197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId A\n");
3231197ba5f4SPaul Zimmerman 		while (!dwc2_is_host_mode(hsotg)) {
3232197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3233197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ?
3234197ba5f4SPaul Zimmerman 				 "Host" : "Peripheral");
323504a9db79SNicholas Mc Guire 			msleep(20);
3236197ba5f4SPaul Zimmerman 			if (++count > 250)
3237197ba5f4SPaul Zimmerman 				break;
3238197ba5f4SPaul Zimmerman 		}
3239197ba5f4SPaul Zimmerman 		if (count > 250)
3240197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3241197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3242197ba5f4SPaul Zimmerman 
3243d2471d4aSJohn Stultz 		spin_lock_irqsave(&hsotg->lock, flags);
3244d2471d4aSJohn Stultz 		dwc2_hsotg_disconnect(hsotg);
3245d2471d4aSJohn Stultz 		spin_unlock_irqrestore(&hsotg->lock, flags);
3246d2471d4aSJohn Stultz 
3247d2471d4aSJohn Stultz 		hsotg->op_state = OTG_STATE_A_HOST;
3248197ba5f4SPaul Zimmerman 		/* Initialize the Core for Host mode */
32490fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3250197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
3251197ba5f4SPaul Zimmerman 		dwc2_hcd_start(hsotg);
3252197ba5f4SPaul Zimmerman 	}
3253197ba5f4SPaul Zimmerman }
3254197ba5f4SPaul Zimmerman 
3255e99e88a9SKees Cook static void dwc2_wakeup_detected(struct timer_list *t)
3256197ba5f4SPaul Zimmerman {
3257e99e88a9SKees Cook 	struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
3258197ba5f4SPaul Zimmerman 	u32 hprt0;
3259197ba5f4SPaul Zimmerman 
3260197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3261197ba5f4SPaul Zimmerman 
3262197ba5f4SPaul Zimmerman 	/*
3263197ba5f4SPaul Zimmerman 	 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3264197ba5f4SPaul Zimmerman 	 * so that OPT tests pass with all PHYs.)
3265197ba5f4SPaul Zimmerman 	 */
3266197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3267197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3268197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RES;
3269f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
3270197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
3271f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPRT0));
3272197ba5f4SPaul Zimmerman 
3273197ba5f4SPaul Zimmerman 	dwc2_hcd_rem_wakeup(hsotg);
3274fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
3275197ba5f4SPaul Zimmerman 
3276197ba5f4SPaul Zimmerman 	/* Change to L0 state */
3277197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L0;
3278197ba5f4SPaul Zimmerman }
3279197ba5f4SPaul Zimmerman 
3280197ba5f4SPaul Zimmerman static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3281197ba5f4SPaul Zimmerman {
3282197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3283197ba5f4SPaul Zimmerman 
3284197ba5f4SPaul Zimmerman 	return hcd->self.b_hnp_enable;
3285197ba5f4SPaul Zimmerman }
3286197ba5f4SPaul Zimmerman 
3287139fae7aSArtur Petrosyan /**
3288139fae7aSArtur Petrosyan  * dwc2_port_suspend() - Put controller in suspend mode for host.
3289139fae7aSArtur Petrosyan  *
3290139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3291139fae7aSArtur Petrosyan  * @windex: The control request wIndex field
3292139fae7aSArtur Petrosyan  *
329322ff0c8eSArtur Petrosyan  * Return: non-zero if failed to enter suspend mode for host.
329422ff0c8eSArtur Petrosyan  *
3295139fae7aSArtur Petrosyan  * This function is for entering Host mode suspend.
3296139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3297139fae7aSArtur Petrosyan  */
329822ff0c8eSArtur Petrosyan int dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3299197ba5f4SPaul Zimmerman {
3300197ba5f4SPaul Zimmerman 	unsigned long flags;
3301197ba5f4SPaul Zimmerman 	u32 pcgctl;
3302197ba5f4SPaul Zimmerman 	u32 gotgctl;
330322ff0c8eSArtur Petrosyan 	int ret = 0;
3304197ba5f4SPaul Zimmerman 
3305197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3306197ba5f4SPaul Zimmerman 
3307197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
3308197ba5f4SPaul Zimmerman 
3309197ba5f4SPaul Zimmerman 	if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
3310f25c42b8SGevorg Sahakyan 		gotgctl = dwc2_readl(hsotg, GOTGCTL);
3311197ba5f4SPaul Zimmerman 		gotgctl |= GOTGCTL_HSTSETHNPEN;
3312f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gotgctl, GOTGCTL);
3313197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_A_SUSPEND;
3314197ba5f4SPaul Zimmerman 	}
3315197ba5f4SPaul Zimmerman 
331622ff0c8eSArtur Petrosyan 	switch (hsotg->params.power_down) {
331722ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
331822ff0c8eSArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
331922ff0c8eSArtur Petrosyan 		if (ret)
332022ff0c8eSArtur Petrosyan 			dev_err(hsotg->dev,
332122ff0c8eSArtur Petrosyan 				"enter partial_power_down failed.\n");
332222ff0c8eSArtur Petrosyan 		break;
332322ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
332422ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3325a2a23d3fSGregory Herrero 		/*
3326d37b939cSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
3327d37b939cSArtur Petrosyan 		 * clock gating is used to save power.
3328a2a23d3fSGregory Herrero 		 */
3329d37b939cSArtur Petrosyan 		dwc2_host_enter_clock_gating(hsotg);
3330d37b939cSArtur Petrosyan 		break;
333122ff0c8eSArtur Petrosyan 	}
3332197ba5f4SPaul Zimmerman 
3333197ba5f4SPaul Zimmerman 	/* For HNP the bus must be suspended for at least 200ms */
3334197ba5f4SPaul Zimmerman 	if (dwc2_host_is_b_hnp_enabled(hsotg)) {
3335f25c42b8SGevorg Sahakyan 		pcgctl = dwc2_readl(hsotg, PCGCTL);
3336197ba5f4SPaul Zimmerman 		pcgctl &= ~PCGCTL_STOPPCLK;
3337f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgctl, PCGCTL);
3338197ba5f4SPaul Zimmerman 
3339197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3340197ba5f4SPaul Zimmerman 
334104a9db79SNicholas Mc Guire 		msleep(200);
3342197ba5f4SPaul Zimmerman 	} else {
3343197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3344197ba5f4SPaul Zimmerman 	}
334522ff0c8eSArtur Petrosyan 
334622ff0c8eSArtur Petrosyan 	return ret;
3347197ba5f4SPaul Zimmerman }
3348197ba5f4SPaul Zimmerman 
3349139fae7aSArtur Petrosyan /**
3350139fae7aSArtur Petrosyan  * dwc2_port_resume() - Exit controller from suspend mode for host.
3351139fae7aSArtur Petrosyan  *
3352139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3353139fae7aSArtur Petrosyan  *
33541e0890ebSArtur Petrosyan  * Return: non-zero if failed to exit suspend mode for host.
33551e0890ebSArtur Petrosyan  *
3356139fae7aSArtur Petrosyan  * This function is for exiting Host mode suspend.
3357139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3358139fae7aSArtur Petrosyan  */
33591e0890ebSArtur Petrosyan int dwc2_port_resume(struct dwc2_hsotg *hsotg)
336030db103cSGregory Herrero {
336130db103cSGregory Herrero 	unsigned long flags;
33621e0890ebSArtur Petrosyan 	int ret = 0;
336330db103cSGregory Herrero 
33644d273c2aSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
33654d273c2aSDouglas Anderson 
33661e0890ebSArtur Petrosyan 	switch (hsotg->params.power_down) {
33671e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
33681e0890ebSArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
33691e0890ebSArtur Petrosyan 		if (ret)
33701e0890ebSArtur Petrosyan 			dev_err(hsotg->dev,
33711e0890ebSArtur Petrosyan 				"exit partial_power_down failed.\n");
33721e0890ebSArtur Petrosyan 		break;
33731e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
33741e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3375a2a23d3fSGregory Herrero 		/*
33763cf8143eSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
33773cf8143eSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
3378a2a23d3fSGregory Herrero 		 */
33794d273c2aSDouglas Anderson 		spin_unlock_irqrestore(&hsotg->lock, flags);
33803cf8143eSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
33814d273c2aSDouglas Anderson 		spin_lock_irqsave(&hsotg->lock, flags);
33823cf8143eSArtur Petrosyan 		break;
33831e0890ebSArtur Petrosyan 	}
33841e0890ebSArtur Petrosyan 
338530db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
33861e0890ebSArtur Petrosyan 
33871e0890ebSArtur Petrosyan 	return ret;
338830db103cSGregory Herrero }
338930db103cSGregory Herrero 
3390197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
3391197ba5f4SPaul Zimmerman static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3392197ba5f4SPaul Zimmerman 				u16 wvalue, u16 windex, char *buf, u16 wlength)
3393197ba5f4SPaul Zimmerman {
3394197ba5f4SPaul Zimmerman 	struct usb_hub_descriptor *hub_desc;
3395197ba5f4SPaul Zimmerman 	int retval = 0;
3396197ba5f4SPaul Zimmerman 	u32 hprt0;
3397197ba5f4SPaul Zimmerman 	u32 port_status;
3398197ba5f4SPaul Zimmerman 	u32 speed;
3399197ba5f4SPaul Zimmerman 	u32 pcgctl;
3400cd7cd0e6SFabrice Gasnier 	u32 pwr;
3401197ba5f4SPaul Zimmerman 
3402197ba5f4SPaul Zimmerman 	switch (typereq) {
3403197ba5f4SPaul Zimmerman 	case ClearHubFeature:
3404197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3405197ba5f4SPaul Zimmerman 
3406197ba5f4SPaul Zimmerman 		switch (wvalue) {
3407197ba5f4SPaul Zimmerman 		case C_HUB_LOCAL_POWER:
3408197ba5f4SPaul Zimmerman 		case C_HUB_OVER_CURRENT:
3409197ba5f4SPaul Zimmerman 			/* Nothing required here */
3410197ba5f4SPaul Zimmerman 			break;
3411197ba5f4SPaul Zimmerman 
3412197ba5f4SPaul Zimmerman 		default:
3413197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3414197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3415197ba5f4SPaul Zimmerman 				"ClearHubFeature request %1xh unknown\n",
3416197ba5f4SPaul Zimmerman 				wvalue);
3417197ba5f4SPaul Zimmerman 		}
3418197ba5f4SPaul Zimmerman 		break;
3419197ba5f4SPaul Zimmerman 
3420197ba5f4SPaul Zimmerman 	case ClearPortFeature:
3421197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_L1)
3422197ba5f4SPaul Zimmerman 			if (!windex || windex > 1)
3423197ba5f4SPaul Zimmerman 				goto error;
3424197ba5f4SPaul Zimmerman 		switch (wvalue) {
3425197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_ENABLE:
3426197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3427197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3428197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3429197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_ENA;
3430f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3431197ba5f4SPaul Zimmerman 			break;
3432197ba5f4SPaul Zimmerman 
3433197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3434197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3435197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
3436b0bb9bb6SPaul Zimmerman 
3437f260b250SVardan Mikayelyan 			if (hsotg->bus_suspended) {
3438f260b250SVardan Mikayelyan 				if (hsotg->hibernated)
3439f260b250SVardan Mikayelyan 					dwc2_exit_hibernation(hsotg, 0, 0, 1);
3440f260b250SVardan Mikayelyan 				else
344130db103cSGregory Herrero 					dwc2_port_resume(hsotg);
3442f260b250SVardan Mikayelyan 			}
3443197ba5f4SPaul Zimmerman 			break;
3444197ba5f4SPaul Zimmerman 
3445197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3446197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3447197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_POWER\n");
3448197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3449cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3450197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_PWR;
3451f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3452cd7cd0e6SFabrice Gasnier 			if (pwr)
3453cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_exit(hsotg);
3454197ba5f4SPaul Zimmerman 			break;
3455197ba5f4SPaul Zimmerman 
3456197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3457197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3458197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3459197ba5f4SPaul Zimmerman 			/* Port indicator not supported */
3460197ba5f4SPaul Zimmerman 			break;
3461197ba5f4SPaul Zimmerman 
3462197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_CONNECTION:
3463197ba5f4SPaul Zimmerman 			/*
3464197ba5f4SPaul Zimmerman 			 * Clears driver's internal Connect Status Change flag
3465197ba5f4SPaul Zimmerman 			 */
3466197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3467197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3468197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change = 0;
3469197ba5f4SPaul Zimmerman 			break;
3470197ba5f4SPaul Zimmerman 
3471197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_RESET:
3472197ba5f4SPaul Zimmerman 			/* Clears driver's internal Port Reset Change flag */
3473197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3474197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3475197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change = 0;
3476197ba5f4SPaul Zimmerman 			break;
3477197ba5f4SPaul Zimmerman 
3478197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_ENABLE:
3479197ba5f4SPaul Zimmerman 			/*
3480197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Enable/Disable
3481197ba5f4SPaul Zimmerman 			 * Change flag
3482197ba5f4SPaul Zimmerman 			 */
3483197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3484197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3485197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change = 0;
3486197ba5f4SPaul Zimmerman 			break;
3487197ba5f4SPaul Zimmerman 
3488197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_SUSPEND:
3489197ba5f4SPaul Zimmerman 			/*
3490197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Suspend Change
3491197ba5f4SPaul Zimmerman 			 * flag, which is set when resume signaling on the host
3492197ba5f4SPaul Zimmerman 			 * port is complete
3493197ba5f4SPaul Zimmerman 			 */
3494197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3495197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3496197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change = 0;
3497197ba5f4SPaul Zimmerman 			break;
3498197ba5f4SPaul Zimmerman 
3499197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_PORT_L1:
3500197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3501197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3502197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_l1_change = 0;
3503197ba5f4SPaul Zimmerman 			break;
3504197ba5f4SPaul Zimmerman 
3505197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_OVER_CURRENT:
3506197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3507197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3508197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change = 0;
3509197ba5f4SPaul Zimmerman 			break;
3510197ba5f4SPaul Zimmerman 
3511197ba5f4SPaul Zimmerman 		default:
3512197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3513197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3514197ba5f4SPaul Zimmerman 				"ClearPortFeature request %1xh unknown or unsupported\n",
3515197ba5f4SPaul Zimmerman 				wvalue);
3516197ba5f4SPaul Zimmerman 		}
3517197ba5f4SPaul Zimmerman 		break;
3518197ba5f4SPaul Zimmerman 
3519197ba5f4SPaul Zimmerman 	case GetHubDescriptor:
3520197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3521197ba5f4SPaul Zimmerman 		hub_desc = (struct usb_hub_descriptor *)buf;
3522197ba5f4SPaul Zimmerman 		hub_desc->bDescLength = 9;
3523a5dd0395SSergei Shtylyov 		hub_desc->bDescriptorType = USB_DT_HUB;
3524197ba5f4SPaul Zimmerman 		hub_desc->bNbrPorts = 1;
35253d040de8SSergei Shtylyov 		hub_desc->wHubCharacteristics =
35263d040de8SSergei Shtylyov 			cpu_to_le16(HUB_CHAR_COMMON_LPSM |
35273d040de8SSergei Shtylyov 				    HUB_CHAR_INDV_PORT_OCPM);
3528197ba5f4SPaul Zimmerman 		hub_desc->bPwrOn2PwrGood = 1;
3529197ba5f4SPaul Zimmerman 		hub_desc->bHubContrCurrent = 0;
3530197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[0] = 0;
3531197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3532197ba5f4SPaul Zimmerman 		break;
3533197ba5f4SPaul Zimmerman 
3534197ba5f4SPaul Zimmerman 	case GetHubStatus:
3535197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubStatus\n");
3536197ba5f4SPaul Zimmerman 		memset(buf, 0, 4);
3537197ba5f4SPaul Zimmerman 		break;
3538197ba5f4SPaul Zimmerman 
3539197ba5f4SPaul Zimmerman 	case GetPortStatus:
3540197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3541197ba5f4SPaul Zimmerman 			 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3542197ba5f4SPaul Zimmerman 			 hsotg->flags.d32);
3543197ba5f4SPaul Zimmerman 		if (!windex || windex > 1)
3544197ba5f4SPaul Zimmerman 			goto error;
3545197ba5f4SPaul Zimmerman 
3546197ba5f4SPaul Zimmerman 		port_status = 0;
3547197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status_change)
3548197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3549197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_enable_change)
3550197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_ENABLE << 16;
3551197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_suspend_change)
3552197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3553197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_l1_change)
3554197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_L1 << 16;
3555197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_reset_change)
3556197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_RESET << 16;
3557197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_over_current_change) {
3558197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "Overcurrent change detected\n");
3559197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3560197ba5f4SPaul Zimmerman 		}
3561197ba5f4SPaul Zimmerman 
3562197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3563197ba5f4SPaul Zimmerman 			/*
3564197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3565197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3566197ba5f4SPaul Zimmerman 			 * return 0's for the remainder of the port status
3567197ba5f4SPaul Zimmerman 			 * since the port register can't be read if the core
3568197ba5f4SPaul Zimmerman 			 * is in device mode.
3569197ba5f4SPaul Zimmerman 			 */
3570197ba5f4SPaul Zimmerman 			*(__le32 *)buf = cpu_to_le32(port_status);
3571197ba5f4SPaul Zimmerman 			break;
3572197ba5f4SPaul Zimmerman 		}
3573197ba5f4SPaul Zimmerman 
3574f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
3575197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  HPRT0: 0x%08x\n", hprt0);
3576197ba5f4SPaul Zimmerman 
3577197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_CONNSTS)
3578197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_CONNECTION;
3579197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_ENA)
3580197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_ENABLE;
3581197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_SUSP)
3582197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_SUSPEND;
3583197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_OVRCURRACT)
3584197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_OVERCURRENT;
3585197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_RST)
3586197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_RESET;
3587197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_PWR)
3588197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_POWER;
3589197ba5f4SPaul Zimmerman 
3590197ba5f4SPaul Zimmerman 		speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
3591197ba5f4SPaul Zimmerman 		if (speed == HPRT0_SPD_HIGH_SPEED)
3592197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_HIGH_SPEED;
3593197ba5f4SPaul Zimmerman 		else if (speed == HPRT0_SPD_LOW_SPEED)
3594197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_LOW_SPEED;
3595197ba5f4SPaul Zimmerman 
3596197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_TSTCTL_MASK)
3597197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_TEST;
3598197ba5f4SPaul Zimmerman 		/* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3599197ba5f4SPaul Zimmerman 
3600bea8e86cSJohn Youn 		if (hsotg->params.dma_desc_fs_enable) {
3601fbb9e22bSMian Yousaf Kaukab 			/*
3602fbb9e22bSMian Yousaf Kaukab 			 * Enable descriptor DMA only if a full speed
3603fbb9e22bSMian Yousaf Kaukab 			 * device is connected.
3604fbb9e22bSMian Yousaf Kaukab 			 */
3605fbb9e22bSMian Yousaf Kaukab 			if (hsotg->new_connection &&
3606fbb9e22bSMian Yousaf Kaukab 			    ((port_status &
3607fbb9e22bSMian Yousaf Kaukab 			      (USB_PORT_STAT_CONNECTION |
3608fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_HIGH_SPEED |
3609fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_LOW_SPEED)) ==
3610fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_CONNECTION)) {
3611fbb9e22bSMian Yousaf Kaukab 				u32 hcfg;
3612fbb9e22bSMian Yousaf Kaukab 
3613fbb9e22bSMian Yousaf Kaukab 				dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
361495832c00SJohn Youn 				hsotg->params.dma_desc_enable = true;
3615f25c42b8SGevorg Sahakyan 				hcfg = dwc2_readl(hsotg, HCFG);
3616fbb9e22bSMian Yousaf Kaukab 				hcfg |= HCFG_DESCDMA;
3617f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcfg, HCFG);
3618fbb9e22bSMian Yousaf Kaukab 				hsotg->new_connection = false;
3619fbb9e22bSMian Yousaf Kaukab 			}
3620fbb9e22bSMian Yousaf Kaukab 		}
3621fbb9e22bSMian Yousaf Kaukab 
3622197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
3623197ba5f4SPaul Zimmerman 		*(__le32 *)buf = cpu_to_le32(port_status);
3624197ba5f4SPaul Zimmerman 		break;
3625197ba5f4SPaul Zimmerman 
3626197ba5f4SPaul Zimmerman 	case SetHubFeature:
3627197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetHubFeature\n");
3628197ba5f4SPaul Zimmerman 		/* No HUB features supported */
3629197ba5f4SPaul Zimmerman 		break;
3630197ba5f4SPaul Zimmerman 
3631197ba5f4SPaul Zimmerman 	case SetPortFeature:
3632197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetPortFeature\n");
3633197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3634197ba5f4SPaul Zimmerman 			goto error;
3635197ba5f4SPaul Zimmerman 
3636197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3637197ba5f4SPaul Zimmerman 			/*
3638197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3639197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3640197ba5f4SPaul Zimmerman 			 * return without doing anything since the port
3641197ba5f4SPaul Zimmerman 			 * register can't be written if the core is in device
3642197ba5f4SPaul Zimmerman 			 * mode.
3643197ba5f4SPaul Zimmerman 			 */
3644197ba5f4SPaul Zimmerman 			break;
3645197ba5f4SPaul Zimmerman 		}
3646197ba5f4SPaul Zimmerman 
3647197ba5f4SPaul Zimmerman 		switch (wvalue) {
3648197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3649197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3650197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3651197ba5f4SPaul Zimmerman 			if (windex != hsotg->otg_port)
3652197ba5f4SPaul Zimmerman 				goto error;
365307d9878fSJisheng Zhang 			if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_HIBERNATION)
3654f260b250SVardan Mikayelyan 				dwc2_enter_hibernation(hsotg, 1);
3655f260b250SVardan Mikayelyan 			else
3656197ba5f4SPaul Zimmerman 				dwc2_port_suspend(hsotg, windex);
3657197ba5f4SPaul Zimmerman 			break;
3658197ba5f4SPaul Zimmerman 
3659197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3660197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3661197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_POWER\n");
3662197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3663cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3664197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_PWR;
3665f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3666cd7cd0e6SFabrice Gasnier 			if (!pwr)
3667cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_init(hsotg);
3668197ba5f4SPaul Zimmerman 			break;
3669197ba5f4SPaul Zimmerman 
3670197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_RESET:
3671*c363af9cSArtur Petrosyan 			dev_dbg(hsotg->dev,
3672*c363af9cSArtur Petrosyan 				"SetPortFeature - USB_PORT_FEAT_RESET\n");
3673*c363af9cSArtur Petrosyan 
3674*c363af9cSArtur Petrosyan 			hprt0 = dwc2_read_hprt0(hsotg);
3675*c363af9cSArtur Petrosyan 
3676*c363af9cSArtur Petrosyan 			if (hsotg->hibernated) {
3677*c363af9cSArtur Petrosyan 				retval = dwc2_exit_hibernation(hsotg, 0, 1, 1);
3678*c363af9cSArtur Petrosyan 				if (retval)
3679*c363af9cSArtur Petrosyan 					dev_err(hsotg->dev,
3680*c363af9cSArtur Petrosyan 						"exit hibernation failed\n");
3681*c363af9cSArtur Petrosyan 			}
3682e97570f7SArtur Petrosyan 
3683e97570f7SArtur Petrosyan 			if (hsotg->in_ppd) {
3684e97570f7SArtur Petrosyan 				retval = dwc2_exit_partial_power_down(hsotg, 1,
3685e97570f7SArtur Petrosyan 								      true);
3686e97570f7SArtur Petrosyan 				if (retval)
3687e97570f7SArtur Petrosyan 					dev_err(hsotg->dev,
3688e97570f7SArtur Petrosyan 						"exit partial_power_down failed\n");
3689e97570f7SArtur Petrosyan 			}
3690e97570f7SArtur Petrosyan 
36915f9e60c0SArtur Petrosyan 			if (hsotg->params.power_down ==
36925f9e60c0SArtur Petrosyan 			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended)
36935f9e60c0SArtur Petrosyan 				dwc2_host_exit_clock_gating(hsotg, 0);
36945f9e60c0SArtur Petrosyan 
3695f25c42b8SGevorg Sahakyan 			pcgctl = dwc2_readl(hsotg, PCGCTL);
3696197ba5f4SPaul Zimmerman 			pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
3697f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, pcgctl, PCGCTL);
3698197ba5f4SPaul Zimmerman 			/* ??? Original driver does this */
3699f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, PCGCTL);
3700197ba5f4SPaul Zimmerman 
3701197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3702cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3703197ba5f4SPaul Zimmerman 			/* Clear suspend bit if resetting from suspend state */
3704197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_SUSP;
3705197ba5f4SPaul Zimmerman 
3706197ba5f4SPaul Zimmerman 			/*
3707197ba5f4SPaul Zimmerman 			 * When B-Host the Port reset bit is set in the Start
3708197ba5f4SPaul Zimmerman 			 * HCD Callback function, so that the reset is started
3709197ba5f4SPaul Zimmerman 			 * within 1ms of the HNP success interrupt
3710197ba5f4SPaul Zimmerman 			 */
3711197ba5f4SPaul Zimmerman 			if (!dwc2_hcd_is_b_host(hsotg)) {
3712197ba5f4SPaul Zimmerman 				hprt0 |= HPRT0_PWR | HPRT0_RST;
3713197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3714197ba5f4SPaul Zimmerman 					"In host mode, hprt0=%08x\n", hprt0);
3715f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hprt0, HPRT0);
3716cd7cd0e6SFabrice Gasnier 				if (!pwr)
3717cd7cd0e6SFabrice Gasnier 					dwc2_vbus_supply_init(hsotg);
3718197ba5f4SPaul Zimmerman 			}
3719197ba5f4SPaul Zimmerman 
3720197ba5f4SPaul Zimmerman 			/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
372104a9db79SNicholas Mc Guire 			msleep(50);
3722197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_RST;
3723f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3724197ba5f4SPaul Zimmerman 			hsotg->lx_state = DWC2_L0; /* Now back to On state */
3725197ba5f4SPaul Zimmerman 			break;
3726197ba5f4SPaul Zimmerman 
3727197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3728197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3729197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3730197ba5f4SPaul Zimmerman 			/* Not supported */
3731197ba5f4SPaul Zimmerman 			break;
3732197ba5f4SPaul Zimmerman 
373396d480e6SJingwu Lin 		case USB_PORT_FEAT_TEST:
373496d480e6SJingwu Lin 			hprt0 = dwc2_read_hprt0(hsotg);
373596d480e6SJingwu Lin 			dev_dbg(hsotg->dev,
373696d480e6SJingwu Lin 				"SetPortFeature - USB_PORT_FEAT_TEST\n");
373796d480e6SJingwu Lin 			hprt0 &= ~HPRT0_TSTCTL_MASK;
373896d480e6SJingwu Lin 			hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
3739f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
374096d480e6SJingwu Lin 			break;
374196d480e6SJingwu Lin 
3742197ba5f4SPaul Zimmerman 		default:
3743197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3744197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3745197ba5f4SPaul Zimmerman 				"SetPortFeature %1xh unknown or unsupported\n",
3746197ba5f4SPaul Zimmerman 				wvalue);
3747197ba5f4SPaul Zimmerman 			break;
3748197ba5f4SPaul Zimmerman 		}
3749197ba5f4SPaul Zimmerman 		break;
3750197ba5f4SPaul Zimmerman 
3751197ba5f4SPaul Zimmerman 	default:
3752197ba5f4SPaul Zimmerman error:
3753197ba5f4SPaul Zimmerman 		retval = -EINVAL;
3754197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3755197ba5f4SPaul Zimmerman 			"Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3756197ba5f4SPaul Zimmerman 			typereq, windex, wvalue);
3757197ba5f4SPaul Zimmerman 		break;
3758197ba5f4SPaul Zimmerman 	}
3759197ba5f4SPaul Zimmerman 
3760197ba5f4SPaul Zimmerman 	return retval;
3761197ba5f4SPaul Zimmerman }
3762197ba5f4SPaul Zimmerman 
3763197ba5f4SPaul Zimmerman static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3764197ba5f4SPaul Zimmerman {
3765197ba5f4SPaul Zimmerman 	int retval;
3766197ba5f4SPaul Zimmerman 
3767197ba5f4SPaul Zimmerman 	if (port != 1)
3768197ba5f4SPaul Zimmerman 		return -EINVAL;
3769197ba5f4SPaul Zimmerman 
3770197ba5f4SPaul Zimmerman 	retval = (hsotg->flags.b.port_connect_status_change ||
3771197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_reset_change ||
3772197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_enable_change ||
3773197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_suspend_change ||
3774197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_over_current_change);
3775197ba5f4SPaul Zimmerman 
3776197ba5f4SPaul Zimmerman 	if (retval) {
3777197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3778197ba5f4SPaul Zimmerman 			"DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3779197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_connect_status_change: %d\n",
3780197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change);
3781197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_reset_change: %d\n",
3782197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change);
3783197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_enable_change: %d\n",
3784197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change);
3785197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_suspend_change: %d\n",
3786197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change);
3787197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_over_current_change: %d\n",
3788197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change);
3789197ba5f4SPaul Zimmerman 	}
3790197ba5f4SPaul Zimmerman 
3791197ba5f4SPaul Zimmerman 	return retval;
3792197ba5f4SPaul Zimmerman }
3793197ba5f4SPaul Zimmerman 
3794197ba5f4SPaul Zimmerman int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3795197ba5f4SPaul Zimmerman {
3796f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3797197ba5f4SPaul Zimmerman 
3798197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3799197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
3800197ba5f4SPaul Zimmerman 		 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
3801197ba5f4SPaul Zimmerman #endif
3802197ba5f4SPaul Zimmerman 	return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3803197ba5f4SPaul Zimmerman }
3804197ba5f4SPaul Zimmerman 
3805fae4e826SDouglas Anderson int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3806fae4e826SDouglas Anderson {
3807f25c42b8SGevorg Sahakyan 	u32 hprt = dwc2_readl(hsotg, HPRT0);
3808f25c42b8SGevorg Sahakyan 	u32 hfir = dwc2_readl(hsotg, HFIR);
3809f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3810fae4e826SDouglas Anderson 	unsigned int us_per_frame;
3811fae4e826SDouglas Anderson 	unsigned int frame_number;
3812fae4e826SDouglas Anderson 	unsigned int remaining;
3813fae4e826SDouglas Anderson 	unsigned int interval;
3814fae4e826SDouglas Anderson 	unsigned int phy_clks;
3815fae4e826SDouglas Anderson 
3816fae4e826SDouglas Anderson 	/* High speed has 125 us per (micro) frame; others are 1 ms per */
3817fae4e826SDouglas Anderson 	us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3818fae4e826SDouglas Anderson 
3819fae4e826SDouglas Anderson 	/* Extract fields */
3820fae4e826SDouglas Anderson 	frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3821fae4e826SDouglas Anderson 	remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3822fae4e826SDouglas Anderson 	interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3823fae4e826SDouglas Anderson 
3824fae4e826SDouglas Anderson 	/*
3825fae4e826SDouglas Anderson 	 * Number of phy clocks since the last tick of the frame number after
3826fae4e826SDouglas Anderson 	 * "us" has passed.
3827fae4e826SDouglas Anderson 	 */
3828fae4e826SDouglas Anderson 	phy_clks = (interval - remaining) +
3829fae4e826SDouglas Anderson 		   DIV_ROUND_UP(interval * us, us_per_frame);
3830fae4e826SDouglas Anderson 
3831fae4e826SDouglas Anderson 	return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3832fae4e826SDouglas Anderson }
3833fae4e826SDouglas Anderson 
3834197ba5f4SPaul Zimmerman int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3835197ba5f4SPaul Zimmerman {
3836197ba5f4SPaul Zimmerman 	return hsotg->op_state == OTG_STATE_B_HOST;
3837197ba5f4SPaul Zimmerman }
3838197ba5f4SPaul Zimmerman 
3839197ba5f4SPaul Zimmerman static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3840197ba5f4SPaul Zimmerman 					       int iso_desc_count,
3841197ba5f4SPaul Zimmerman 					       gfp_t mem_flags)
3842197ba5f4SPaul Zimmerman {
3843197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3844197ba5f4SPaul Zimmerman 
3845eeca7606SGustavo A. R. Silva 	urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
3846197ba5f4SPaul Zimmerman 	if (urb)
3847197ba5f4SPaul Zimmerman 		urb->packet_count = iso_desc_count;
3848197ba5f4SPaul Zimmerman 	return urb;
3849197ba5f4SPaul Zimmerman }
3850197ba5f4SPaul Zimmerman 
3851197ba5f4SPaul Zimmerman static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3852197ba5f4SPaul Zimmerman 				      struct dwc2_hcd_urb *urb, u8 dev_addr,
3853babd1839SDouglas Anderson 				      u8 ep_num, u8 ep_type, u8 ep_dir,
3854babd1839SDouglas Anderson 				      u16 maxp, u16 maxp_mult)
3855197ba5f4SPaul Zimmerman {
3856197ba5f4SPaul Zimmerman 	if (dbg_perio() ||
3857197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_BULK ||
3858197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_CONTROL)
3859197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3860babd1839SDouglas Anderson 			 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
3861babd1839SDouglas Anderson 			 dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
3862197ba5f4SPaul Zimmerman 	urb->pipe_info.dev_addr = dev_addr;
3863197ba5f4SPaul Zimmerman 	urb->pipe_info.ep_num = ep_num;
3864197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_type = ep_type;
3865197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_dir = ep_dir;
3866babd1839SDouglas Anderson 	urb->pipe_info.maxp = maxp;
3867babd1839SDouglas Anderson 	urb->pipe_info.maxp_mult = maxp_mult;
3868197ba5f4SPaul Zimmerman }
3869197ba5f4SPaul Zimmerman 
3870197ba5f4SPaul Zimmerman /*
3871197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
3872197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
3873197ba5f4SPaul Zimmerman  */
3874197ba5f4SPaul Zimmerman void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3875197ba5f4SPaul Zimmerman {
3876197ba5f4SPaul Zimmerman #ifdef DEBUG
3877197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
3878197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3879197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
3880197ba5f4SPaul Zimmerman 	int num_channels;
3881197ba5f4SPaul Zimmerman 	u32 np_tx_status;
3882197ba5f4SPaul Zimmerman 	u32 p_tx_status;
3883197ba5f4SPaul Zimmerman 	int i;
3884197ba5f4SPaul Zimmerman 
3885bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
3886197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3887197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3888197ba5f4SPaul Zimmerman 		"************************************************************\n");
3889197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HCD State:\n");
3890197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Num channels: %d\n", num_channels);
3891197ba5f4SPaul Zimmerman 
3892197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
3893197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
3894197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  Channel %d:\n", i);
3895197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3896197ba5f4SPaul Zimmerman 			"    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3897197ba5f4SPaul Zimmerman 			chan->dev_addr, chan->ep_num, chan->ep_is_in);
3898197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    speed: %d\n", chan->speed);
3899197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
3900197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
3901197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    data_pid_start: %d\n",
3902197ba5f4SPaul Zimmerman 			chan->data_pid_start);
3903197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    multi_count: %d\n", chan->multi_count);
3904197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_started: %d\n",
3905197ba5f4SPaul Zimmerman 			chan->xfer_started);
3906197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
3907197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
3908197ba5f4SPaul Zimmerman 			(unsigned long)chan->xfer_dma);
3909197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
3910197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_count: %d\n", chan->xfer_count);
3911197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_on_queue: %d\n",
3912197ba5f4SPaul Zimmerman 			chan->halt_on_queue);
3913197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_pending: %d\n",
3914197ba5f4SPaul Zimmerman 			chan->halt_pending);
3915197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
3916197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    do_split: %d\n", chan->do_split);
3917197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    complete_split: %d\n",
3918197ba5f4SPaul Zimmerman 			chan->complete_split);
3919197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_addr: %d\n", chan->hub_addr);
3920197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_port: %d\n", chan->hub_port);
3921197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xact_pos: %d\n", chan->xact_pos);
3922197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    requests: %d\n", chan->requests);
3923197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
3924197ba5f4SPaul Zimmerman 
3925197ba5f4SPaul Zimmerman 		if (chan->xfer_started) {
3926197ba5f4SPaul Zimmerman 			u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3927197ba5f4SPaul Zimmerman 
3928f25c42b8SGevorg Sahakyan 			hfnum = dwc2_readl(hsotg, HFNUM);
3929f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
3930f25c42b8SGevorg Sahakyan 			hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
3931f25c42b8SGevorg Sahakyan 			hcint = dwc2_readl(hsotg, HCINT(i));
3932f25c42b8SGevorg Sahakyan 			hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
3933197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hfnum: 0x%08x\n", hfnum);
3934197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcchar: 0x%08x\n", hcchar);
3935197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hctsiz: 0x%08x\n", hctsiz);
3936197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcint: 0x%08x\n", hcint);
3937197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcintmsk: 0x%08x\n", hcintmsk);
3938197ba5f4SPaul Zimmerman 		}
3939197ba5f4SPaul Zimmerman 
3940197ba5f4SPaul Zimmerman 		if (!(chan->xfer_started && chan->qh))
3941197ba5f4SPaul Zimmerman 			continue;
3942197ba5f4SPaul Zimmerman 
3943197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3944197ba5f4SPaul Zimmerman 			if (!qtd->in_process)
3945197ba5f4SPaul Zimmerman 				break;
3946197ba5f4SPaul Zimmerman 			urb = qtd->urb;
3947197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    URB Info:\n");
3948197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "      qtd: %p, urb: %p\n",
3949197ba5f4SPaul Zimmerman 				qtd, urb);
3950197ba5f4SPaul Zimmerman 			if (urb) {
3951197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3952197ba5f4SPaul Zimmerman 					"      Dev: %d, EP: %d %s\n",
3953197ba5f4SPaul Zimmerman 					dwc2_hcd_get_dev_addr(&urb->pipe_info),
3954197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_num(&urb->pipe_info),
3955197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3956197ba5f4SPaul Zimmerman 					"IN" : "OUT");
3957197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3958babd1839SDouglas Anderson 					"      Max packet size: %d (%d mult)\n",
3959babd1839SDouglas Anderson 					dwc2_hcd_get_maxp(&urb->pipe_info),
3960babd1839SDouglas Anderson 					dwc2_hcd_get_maxp_mult(&urb->pipe_info));
3961197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3962197ba5f4SPaul Zimmerman 					"      transfer_buffer: %p\n",
3963197ba5f4SPaul Zimmerman 					urb->buf);
3964197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3965197ba5f4SPaul Zimmerman 					"      transfer_dma: %08lx\n",
3966197ba5f4SPaul Zimmerman 					(unsigned long)urb->dma);
3967197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3968197ba5f4SPaul Zimmerman 					"      transfer_buffer_length: %d\n",
3969197ba5f4SPaul Zimmerman 					urb->length);
3970197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev, "      actual_length: %d\n",
3971197ba5f4SPaul Zimmerman 					urb->actual_length);
3972197ba5f4SPaul Zimmerman 			}
3973197ba5f4SPaul Zimmerman 		}
3974197ba5f4SPaul Zimmerman 	}
3975197ba5f4SPaul Zimmerman 
3976197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  non_periodic_channels: %d\n",
3977197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels);
3978197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_channels: %d\n",
3979197ba5f4SPaul Zimmerman 		hsotg->periodic_channels);
3980197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_usecs: %d\n", hsotg->periodic_usecs);
3981f25c42b8SGevorg Sahakyan 	np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
3982197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx Req Queue Space Avail: %d\n",
3983197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3984197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx FIFO Space Avail: %d\n",
3985197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3986f25c42b8SGevorg Sahakyan 	p_tx_status = dwc2_readl(hsotg, HPTXSTS);
3987197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx Req Queue Space Avail: %d\n",
3988197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3989197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx FIFO Space Avail: %d\n",
3990197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3991197ba5f4SPaul Zimmerman 	dwc2_dump_global_registers(hsotg);
3992197ba5f4SPaul Zimmerman 	dwc2_dump_host_registers(hsotg);
3993197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3994197ba5f4SPaul Zimmerman 		"************************************************************\n");
3995197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3996197ba5f4SPaul Zimmerman #endif
3997197ba5f4SPaul Zimmerman }
3998197ba5f4SPaul Zimmerman 
3999197ba5f4SPaul Zimmerman struct wrapper_priv_data {
4000197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg;
4001197ba5f4SPaul Zimmerman };
4002197ba5f4SPaul Zimmerman 
4003197ba5f4SPaul Zimmerman /* Gets the dwc2_hsotg from a usb_hcd */
4004197ba5f4SPaul Zimmerman static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4005197ba5f4SPaul Zimmerman {
4006197ba5f4SPaul Zimmerman 	struct wrapper_priv_data *p;
4007197ba5f4SPaul Zimmerman 
4008197ba5f4SPaul Zimmerman 	p = (struct wrapper_priv_data *)&hcd->hcd_priv;
4009197ba5f4SPaul Zimmerman 	return p->hsotg;
4010197ba5f4SPaul Zimmerman }
4011197ba5f4SPaul Zimmerman 
40129f9f09b0SDouglas Anderson /**
40139f9f09b0SDouglas Anderson  * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
40149f9f09b0SDouglas Anderson  *
40159f9f09b0SDouglas Anderson  * This will get the dwc2_tt structure (and ttport) associated with the given
40169f9f09b0SDouglas Anderson  * context (which is really just a struct urb pointer).
40179f9f09b0SDouglas Anderson  *
40189f9f09b0SDouglas Anderson  * The first time this is called for a given TT we allocate memory for our
40199f9f09b0SDouglas Anderson  * structure.  When everyone is done and has called dwc2_host_put_tt_info()
40209f9f09b0SDouglas Anderson  * then the refcount for the structure will go to 0 and we'll free it.
40219f9f09b0SDouglas Anderson  *
40229f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40239f9f09b0SDouglas Anderson  * @context:   The priv pointer from a struct dwc2_hcd_urb.
40249f9f09b0SDouglas Anderson  * @mem_flags: Flags for allocating memory.
40259f9f09b0SDouglas Anderson  * @ttport:    We'll return this device's port number here.  That's used to
40269f9f09b0SDouglas Anderson  *             reference into the bitmap if we're on a multi_tt hub.
40279f9f09b0SDouglas Anderson  *
40289f9f09b0SDouglas Anderson  * Return: a pointer to a struct dwc2_tt.  Don't forget to call
40299f9f09b0SDouglas Anderson  *         dwc2_host_put_tt_info()!  Returns NULL upon memory alloc failure.
40309f9f09b0SDouglas Anderson  */
40319f9f09b0SDouglas Anderson 
40329f9f09b0SDouglas Anderson struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
40339f9f09b0SDouglas Anderson 				      gfp_t mem_flags, int *ttport)
40349f9f09b0SDouglas Anderson {
40359f9f09b0SDouglas Anderson 	struct urb *urb = context;
40369f9f09b0SDouglas Anderson 	struct dwc2_tt *dwc_tt = NULL;
40379f9f09b0SDouglas Anderson 
40389f9f09b0SDouglas Anderson 	if (urb->dev->tt) {
40399f9f09b0SDouglas Anderson 		*ttport = urb->dev->ttport;
40409f9f09b0SDouglas Anderson 
40419f9f09b0SDouglas Anderson 		dwc_tt = urb->dev->tt->hcpriv;
40429da51974SJohn Youn 		if (!dwc_tt) {
40439f9f09b0SDouglas Anderson 			size_t bitmap_size;
40449f9f09b0SDouglas Anderson 
40459f9f09b0SDouglas Anderson 			/*
40469f9f09b0SDouglas Anderson 			 * For single_tt we need one schedule.  For multi_tt
40479f9f09b0SDouglas Anderson 			 * we need one per port.
40489f9f09b0SDouglas Anderson 			 */
40499f9f09b0SDouglas Anderson 			bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
40509f9f09b0SDouglas Anderson 				      sizeof(dwc_tt->periodic_bitmaps[0]);
40519f9f09b0SDouglas Anderson 			if (urb->dev->tt->multi)
40529f9f09b0SDouglas Anderson 				bitmap_size *= urb->dev->tt->hub->maxchild;
40539f9f09b0SDouglas Anderson 
40549f9f09b0SDouglas Anderson 			dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
40559f9f09b0SDouglas Anderson 					 mem_flags);
40569da51974SJohn Youn 			if (!dwc_tt)
40579f9f09b0SDouglas Anderson 				return NULL;
40589f9f09b0SDouglas Anderson 
40599f9f09b0SDouglas Anderson 			dwc_tt->usb_tt = urb->dev->tt;
40609f9f09b0SDouglas Anderson 			dwc_tt->usb_tt->hcpriv = dwc_tt;
40619f9f09b0SDouglas Anderson 		}
40629f9f09b0SDouglas Anderson 
40639f9f09b0SDouglas Anderson 		dwc_tt->refcount++;
40649f9f09b0SDouglas Anderson 	}
40659f9f09b0SDouglas Anderson 
40669f9f09b0SDouglas Anderson 	return dwc_tt;
40679f9f09b0SDouglas Anderson }
40689f9f09b0SDouglas Anderson 
40699f9f09b0SDouglas Anderson /**
40709f9f09b0SDouglas Anderson  * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
40719f9f09b0SDouglas Anderson  *
40729f9f09b0SDouglas Anderson  * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
40739f9f09b0SDouglas Anderson  * of the structure are done.
40749f9f09b0SDouglas Anderson  *
40759f9f09b0SDouglas Anderson  * It's OK to call this with NULL.
40769f9f09b0SDouglas Anderson  *
40779f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40789f9f09b0SDouglas Anderson  * @dwc_tt:    The pointer returned by dwc2_host_get_tt_info.
40799f9f09b0SDouglas Anderson  */
40809f9f09b0SDouglas Anderson void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
40819f9f09b0SDouglas Anderson {
40829f9f09b0SDouglas Anderson 	/* Model kfree and make put of NULL a no-op */
40839da51974SJohn Youn 	if (!dwc_tt)
40849f9f09b0SDouglas Anderson 		return;
40859f9f09b0SDouglas Anderson 
40869f9f09b0SDouglas Anderson 	WARN_ON(dwc_tt->refcount < 1);
40879f9f09b0SDouglas Anderson 
40889f9f09b0SDouglas Anderson 	dwc_tt->refcount--;
40899f9f09b0SDouglas Anderson 	if (!dwc_tt->refcount) {
40909f9f09b0SDouglas Anderson 		dwc_tt->usb_tt->hcpriv = NULL;
40919f9f09b0SDouglas Anderson 		kfree(dwc_tt);
40929f9f09b0SDouglas Anderson 	}
40939f9f09b0SDouglas Anderson }
40949f9f09b0SDouglas Anderson 
4095197ba5f4SPaul Zimmerman int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4096197ba5f4SPaul Zimmerman {
4097197ba5f4SPaul Zimmerman 	struct urb *urb = context;
4098197ba5f4SPaul Zimmerman 
4099197ba5f4SPaul Zimmerman 	return urb->dev->speed;
4100197ba5f4SPaul Zimmerman }
4101197ba5f4SPaul Zimmerman 
4102197ba5f4SPaul Zimmerman static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4103197ba5f4SPaul Zimmerman 					struct urb *urb)
4104197ba5f4SPaul Zimmerman {
4105197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4106197ba5f4SPaul Zimmerman 
4107197ba5f4SPaul Zimmerman 	if (urb->interval)
4108197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated += bw / urb->interval;
4109197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4110197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs++;
4111197ba5f4SPaul Zimmerman 	else
4112197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs++;
4113197ba5f4SPaul Zimmerman }
4114197ba5f4SPaul Zimmerman 
4115197ba5f4SPaul Zimmerman static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4116197ba5f4SPaul Zimmerman 				    struct urb *urb)
4117197ba5f4SPaul Zimmerman {
4118197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4119197ba5f4SPaul Zimmerman 
4120197ba5f4SPaul Zimmerman 	if (urb->interval)
4121197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated -= bw / urb->interval;
4122197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4123197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs--;
4124197ba5f4SPaul Zimmerman 	else
4125197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs--;
4126197ba5f4SPaul Zimmerman }
4127197ba5f4SPaul Zimmerman 
4128197ba5f4SPaul Zimmerman /*
4129197ba5f4SPaul Zimmerman  * Sets the final status of an URB and returns it to the upper layer. Any
4130197ba5f4SPaul Zimmerman  * required cleanup of the URB is performed.
4131197ba5f4SPaul Zimmerman  *
4132197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
4133197ba5f4SPaul Zimmerman  */
4134197ba5f4SPaul Zimmerman void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4135197ba5f4SPaul Zimmerman 			int status)
4136197ba5f4SPaul Zimmerman {
4137197ba5f4SPaul Zimmerman 	struct urb *urb;
4138197ba5f4SPaul Zimmerman 	int i;
4139197ba5f4SPaul Zimmerman 
4140197ba5f4SPaul Zimmerman 	if (!qtd) {
4141197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4142197ba5f4SPaul Zimmerman 		return;
4143197ba5f4SPaul Zimmerman 	}
4144197ba5f4SPaul Zimmerman 
4145197ba5f4SPaul Zimmerman 	if (!qtd->urb) {
4146197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4147197ba5f4SPaul Zimmerman 		return;
4148197ba5f4SPaul Zimmerman 	}
4149197ba5f4SPaul Zimmerman 
4150197ba5f4SPaul Zimmerman 	urb = qtd->urb->priv;
4151197ba5f4SPaul Zimmerman 	if (!urb) {
4152197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4153197ba5f4SPaul Zimmerman 		return;
4154197ba5f4SPaul Zimmerman 	}
4155197ba5f4SPaul Zimmerman 
4156197ba5f4SPaul Zimmerman 	urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
4157197ba5f4SPaul Zimmerman 
4158197ba5f4SPaul Zimmerman 	if (dbg_urb(urb))
4159197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
4160197ba5f4SPaul Zimmerman 			 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4161197ba5f4SPaul Zimmerman 			 __func__, urb, usb_pipedevice(urb->pipe),
4162197ba5f4SPaul Zimmerman 			 usb_pipeendpoint(urb->pipe),
4163197ba5f4SPaul Zimmerman 			 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4164197ba5f4SPaul Zimmerman 			 urb->actual_length);
4165197ba5f4SPaul Zimmerman 
4166197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4167197ba5f4SPaul Zimmerman 		urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
4168197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; ++i) {
4169197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].actual_length =
4170197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_actual_length(
4171197ba5f4SPaul Zimmerman 						qtd->urb, i);
4172197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].status =
4173197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
4174197ba5f4SPaul Zimmerman 		}
4175197ba5f4SPaul Zimmerman 	}
4176197ba5f4SPaul Zimmerman 
4177fe9b1773SGregory Herrero 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4178fe9b1773SGregory Herrero 		for (i = 0; i < urb->number_of_packets; i++)
4179fe9b1773SGregory Herrero 			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4180fe9b1773SGregory Herrero 				 i, urb->iso_frame_desc[i].status);
4181fe9b1773SGregory Herrero 	}
4182fe9b1773SGregory Herrero 
4183197ba5f4SPaul Zimmerman 	urb->status = status;
4184197ba5f4SPaul Zimmerman 	if (!status) {
4185197ba5f4SPaul Zimmerman 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4186197ba5f4SPaul Zimmerman 		    urb->actual_length < urb->transfer_buffer_length)
4187197ba5f4SPaul Zimmerman 			urb->status = -EREMOTEIO;
4188197ba5f4SPaul Zimmerman 	}
4189197ba5f4SPaul Zimmerman 
4190197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4191197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4192197ba5f4SPaul Zimmerman 		struct usb_host_endpoint *ep = urb->ep;
4193197ba5f4SPaul Zimmerman 
4194197ba5f4SPaul Zimmerman 		if (ep)
4195197ba5f4SPaul Zimmerman 			dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4196197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4197197ba5f4SPaul Zimmerman 					urb);
4198197ba5f4SPaul Zimmerman 	}
4199197ba5f4SPaul Zimmerman 
4200197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
4201197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4202197ba5f4SPaul Zimmerman 	kfree(qtd->urb);
4203197ba5f4SPaul Zimmerman 	qtd->urb = NULL;
4204197ba5f4SPaul Zimmerman 
4205197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
4206197ba5f4SPaul Zimmerman }
4207197ba5f4SPaul Zimmerman 
4208197ba5f4SPaul Zimmerman /*
4209197ba5f4SPaul Zimmerman  * Work queue function for starting the HCD when A-Cable is connected
4210197ba5f4SPaul Zimmerman  */
4211197ba5f4SPaul Zimmerman static void dwc2_hcd_start_func(struct work_struct *work)
4212197ba5f4SPaul Zimmerman {
4213197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4214197ba5f4SPaul Zimmerman 						start_work.work);
4215197ba5f4SPaul Zimmerman 
4216197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4217197ba5f4SPaul Zimmerman 	dwc2_host_start(hsotg);
4218197ba5f4SPaul Zimmerman }
4219197ba5f4SPaul Zimmerman 
4220197ba5f4SPaul Zimmerman /*
4221197ba5f4SPaul Zimmerman  * Reset work queue function
4222197ba5f4SPaul Zimmerman  */
4223197ba5f4SPaul Zimmerman static void dwc2_hcd_reset_func(struct work_struct *work)
4224197ba5f4SPaul Zimmerman {
4225197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4226197ba5f4SPaul Zimmerman 						reset_work.work);
42274a065c7bSDouglas Anderson 	unsigned long flags;
4228197ba5f4SPaul Zimmerman 	u32 hprt0;
4229197ba5f4SPaul Zimmerman 
4230197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "USB RESET function called\n");
42314a065c7bSDouglas Anderson 
42324a065c7bSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
42334a065c7bSDouglas Anderson 
4234197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
4235197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RST;
4236f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
4237197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_reset_change = 1;
42384a065c7bSDouglas Anderson 
42394a065c7bSDouglas Anderson 	spin_unlock_irqrestore(&hsotg->lock, flags);
4240197ba5f4SPaul Zimmerman }
4241197ba5f4SPaul Zimmerman 
4242c40cf770SDouglas Anderson static void dwc2_hcd_phy_reset_func(struct work_struct *work)
4243c40cf770SDouglas Anderson {
4244c40cf770SDouglas Anderson 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4245c40cf770SDouglas Anderson 						phy_reset_work);
4246c40cf770SDouglas Anderson 	int ret;
4247c40cf770SDouglas Anderson 
4248c40cf770SDouglas Anderson 	ret = phy_reset(hsotg->phy);
4249c40cf770SDouglas Anderson 	if (ret)
4250c40cf770SDouglas Anderson 		dev_warn(hsotg->dev, "PHY reset failed\n");
4251c40cf770SDouglas Anderson }
4252c40cf770SDouglas Anderson 
4253197ba5f4SPaul Zimmerman /*
4254197ba5f4SPaul Zimmerman  * =========================================================================
4255197ba5f4SPaul Zimmerman  *  Linux HC Driver Functions
4256197ba5f4SPaul Zimmerman  * =========================================================================
4257197ba5f4SPaul Zimmerman  */
4258197ba5f4SPaul Zimmerman 
4259197ba5f4SPaul Zimmerman /*
4260197ba5f4SPaul Zimmerman  * Initializes the DWC_otg controller and its root hub and prepares it for host
4261197ba5f4SPaul Zimmerman  * mode operation. Activates the root port. Returns 0 on success and a negative
4262197ba5f4SPaul Zimmerman  * error code on failure.
4263197ba5f4SPaul Zimmerman  */
4264197ba5f4SPaul Zimmerman static int _dwc2_hcd_start(struct usb_hcd *hcd)
4265197ba5f4SPaul Zimmerman {
4266197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4267197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4268197ba5f4SPaul Zimmerman 	unsigned long flags;
4269cd7cd0e6SFabrice Gasnier 	u32 hprt0;
427041ee1ea2SFabrice Gasnier 	int ret;
4271197ba5f4SPaul Zimmerman 
4272197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4273197ba5f4SPaul Zimmerman 
4274197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
427531927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L0;
4276197ba5f4SPaul Zimmerman 	hcd->state = HC_STATE_RUNNING;
427731927b6bSGregory Herrero 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4278197ba5f4SPaul Zimmerman 
4279197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
4280197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4281197ba5f4SPaul Zimmerman 		return 0;	/* why 0 ?? */
4282197ba5f4SPaul Zimmerman 	}
4283197ba5f4SPaul Zimmerman 
4284197ba5f4SPaul Zimmerman 	dwc2_hcd_reinit(hsotg);
4285197ba5f4SPaul Zimmerman 
4286cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4287cd7cd0e6SFabrice Gasnier 	/* Has vbus power been turned on in dwc2_core_host_init ? */
4288cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR) {
4289cd7cd0e6SFabrice Gasnier 		/* Enable external vbus supply before resuming root hub */
429041ee1ea2SFabrice Gasnier 		spin_unlock_irqrestore(&hsotg->lock, flags);
429141ee1ea2SFabrice Gasnier 		ret = dwc2_vbus_supply_init(hsotg);
429241ee1ea2SFabrice Gasnier 		if (ret)
429341ee1ea2SFabrice Gasnier 			return ret;
429441ee1ea2SFabrice Gasnier 		spin_lock_irqsave(&hsotg->lock, flags);
4295cd7cd0e6SFabrice Gasnier 	}
429641ee1ea2SFabrice Gasnier 
4297197ba5f4SPaul Zimmerman 	/* Initialize and connect root hub if one is not already attached */
4298197ba5f4SPaul Zimmerman 	if (bus->root_hub) {
4299197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4300197ba5f4SPaul Zimmerman 		/* Inform the HUB driver to resume */
4301197ba5f4SPaul Zimmerman 		usb_hcd_resume_root_hub(hcd);
4302197ba5f4SPaul Zimmerman 	}
4303197ba5f4SPaul Zimmerman 
4304197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4305531ef5ebSAmelie Delaunay 
430641ee1ea2SFabrice Gasnier 	return 0;
4307197ba5f4SPaul Zimmerman }
4308197ba5f4SPaul Zimmerman 
4309197ba5f4SPaul Zimmerman /*
4310197ba5f4SPaul Zimmerman  * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4311197ba5f4SPaul Zimmerman  * stopped.
4312197ba5f4SPaul Zimmerman  */
4313197ba5f4SPaul Zimmerman static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4314197ba5f4SPaul Zimmerman {
4315197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4316197ba5f4SPaul Zimmerman 	unsigned long flags;
4317cd7cd0e6SFabrice Gasnier 	u32 hprt0;
4318197ba5f4SPaul Zimmerman 
43195bbf6ce0SGregory Herrero 	/* Turn off all host-specific interrupts */
43205bbf6ce0SGregory Herrero 	dwc2_disable_host_interrupts(hsotg);
43215bbf6ce0SGregory Herrero 
4322091473adSGregory Herrero 	/* Wait for interrupt processing to finish */
4323091473adSGregory Herrero 	synchronize_irq(hcd->irq);
4324091473adSGregory Herrero 
4325197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4326cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4327091473adSGregory Herrero 	/* Ensure hcd is disconnected */
43286a659531SDouglas Anderson 	dwc2_hcd_disconnect(hsotg, true);
4329197ba5f4SPaul Zimmerman 	dwc2_hcd_stop(hsotg);
433031927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L3;
433131927b6bSGregory Herrero 	hcd->state = HC_STATE_HALT;
433231927b6bSGregory Herrero 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4333197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4334197ba5f4SPaul Zimmerman 
4335cd7cd0e6SFabrice Gasnier 	/* keep balanced supply init/exit by checking HPRT0_PWR */
4336cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR)
4337531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
4338531ef5ebSAmelie Delaunay 
4339197ba5f4SPaul Zimmerman 	usleep_range(1000, 3000);
4340197ba5f4SPaul Zimmerman }
4341197ba5f4SPaul Zimmerman 
434299a65798SGregory Herrero static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
434399a65798SGregory Herrero {
434499a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4345a2a23d3fSGregory Herrero 	unsigned long flags;
4346a2a23d3fSGregory Herrero 	int ret = 0;
434799a65798SGregory Herrero 
4348a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4349a2a23d3fSGregory Herrero 
4350f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4351f367b72cSMeng Dongyang 		goto unlock;
4352f367b72cSMeng Dongyang 
4353a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L0)
4354a2a23d3fSGregory Herrero 		goto unlock;
4355a2a23d3fSGregory Herrero 
4356a2a23d3fSGregory Herrero 	if (!HCD_HW_ACCESSIBLE(hcd))
4357a2a23d3fSGregory Herrero 		goto unlock;
4358a2a23d3fSGregory Herrero 
4359866932e2SJohn Stultz 	if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4360866932e2SJohn Stultz 		goto unlock;
4361866932e2SJohn Stultz 
4362113f86d0SArtur Petrosyan 	if (hsotg->bus_suspended)
4363a2a23d3fSGregory Herrero 		goto skip_power_saving;
4364a2a23d3fSGregory Herrero 
4365113f86d0SArtur Petrosyan 	if (hsotg->flags.b.port_connect_status == 0)
4366113f86d0SArtur Petrosyan 		goto skip_power_saving;
4367113f86d0SArtur Petrosyan 
4368113f86d0SArtur Petrosyan 	switch (hsotg->params.power_down) {
4369113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4370113f86d0SArtur Petrosyan 		/* Enter partial_power_down */
4371113f86d0SArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
4372113f86d0SArtur Petrosyan 		if (ret)
4373113f86d0SArtur Petrosyan 			dev_err(hsotg->dev,
4374113f86d0SArtur Petrosyan 				"enter partial_power_down failed\n");
4375113f86d0SArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
4376113f86d0SArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4377113f86d0SArtur Petrosyan 		break;
4378113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4379113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
438050fb0c12SArtur Petrosyan 		/*
438150fb0c12SArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
438250fb0c12SArtur Petrosyan 		 * clock gating is used to save power.
438350fb0c12SArtur Petrosyan 		 */
438450fb0c12SArtur Petrosyan 		dwc2_host_enter_clock_gating(hsotg);
438550fb0c12SArtur Petrosyan 
438650fb0c12SArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
438750fb0c12SArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
438850fb0c12SArtur Petrosyan 		break;
4389113f86d0SArtur Petrosyan 	default:
4390113f86d0SArtur Petrosyan 		goto skip_power_saving;
43916f6d7059SDouglas Anderson 	}
4392113f86d0SArtur Petrosyan 
43935aa678c7SFabrice Gasnier 	spin_unlock_irqrestore(&hsotg->lock, flags);
4394531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_exit(hsotg);
43955aa678c7SFabrice Gasnier 	spin_lock_irqsave(&hsotg->lock, flags);
43966f6d7059SDouglas Anderson 
4397a2a23d3fSGregory Herrero 	/* Ask phy to be suspended */
4398a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4399a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4400a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, true);
4401a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4402a2a23d3fSGregory Herrero 	}
4403a2a23d3fSGregory Herrero 
4404a2a23d3fSGregory Herrero skip_power_saving:
440599a65798SGregory Herrero 	hsotg->lx_state = DWC2_L2;
4406a2a23d3fSGregory Herrero unlock:
4407a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4408a2a23d3fSGregory Herrero 
4409a2a23d3fSGregory Herrero 	return ret;
441099a65798SGregory Herrero }
441199a65798SGregory Herrero 
441299a65798SGregory Herrero static int _dwc2_hcd_resume(struct usb_hcd *hcd)
441399a65798SGregory Herrero {
441499a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4415a2a23d3fSGregory Herrero 	unsigned long flags;
4416c74c26f6SArtur Petrosyan 	u32 hprt0;
4417a2a23d3fSGregory Herrero 	int ret = 0;
4418a2a23d3fSGregory Herrero 
4419a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4420a2a23d3fSGregory Herrero 
4421f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4422f367b72cSMeng Dongyang 		goto unlock;
4423f367b72cSMeng Dongyang 
4424a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L2)
4425a2a23d3fSGregory Herrero 		goto unlock;
4426a2a23d3fSGregory Herrero 
4427c74c26f6SArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
4428c74c26f6SArtur Petrosyan 
4429c74c26f6SArtur Petrosyan 	/*
4430c74c26f6SArtur Petrosyan 	 * Added port connection status checking which prevents exiting from
4431c74c26f6SArtur Petrosyan 	 * Partial Power Down mode from _dwc2_hcd_resume() if not in Partial
4432c74c26f6SArtur Petrosyan 	 * Power Down mode.
4433c74c26f6SArtur Petrosyan 	 */
4434c74c26f6SArtur Petrosyan 	if (hprt0 & HPRT0_CONNSTS) {
4435a2a23d3fSGregory Herrero 		hsotg->lx_state = DWC2_L0;
4436a2a23d3fSGregory Herrero 		goto unlock;
4437a2a23d3fSGregory Herrero 	}
4438a2a23d3fSGregory Herrero 
4439c74c26f6SArtur Petrosyan 	switch (hsotg->params.power_down) {
4440c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4441c74c26f6SArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
4442c74c26f6SArtur Petrosyan 		if (ret)
4443c74c26f6SArtur Petrosyan 			dev_err(hsotg->dev,
4444c74c26f6SArtur Petrosyan 				"exit partial_power_down failed\n");
4445c74c26f6SArtur Petrosyan 		/*
4446c74c26f6SArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4447c74c26f6SArtur Petrosyan 		 * since an interrupt may rise.
4448c74c26f6SArtur Petrosyan 		 */
4449c74c26f6SArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4450c74c26f6SArtur Petrosyan 		break;
4451c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4452c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
4453ef5e0eecSArtur Petrosyan 		/*
4454ef5e0eecSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
4455ef5e0eecSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
4456ef5e0eecSArtur Petrosyan 		 */
4457ef5e0eecSArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
4458ef5e0eecSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
4459ef5e0eecSArtur Petrosyan 
4460ef5e0eecSArtur Petrosyan 		/*
4461ef5e0eecSArtur Petrosyan 		 * Initialize the Core for Host mode, as after system resume
4462ef5e0eecSArtur Petrosyan 		 * the global interrupts are disabled.
4463ef5e0eecSArtur Petrosyan 		 */
4464ef5e0eecSArtur Petrosyan 		dwc2_core_init(hsotg, false);
4465ef5e0eecSArtur Petrosyan 		dwc2_enable_global_interrupts(hsotg);
4466ef5e0eecSArtur Petrosyan 		dwc2_hcd_reinit(hsotg);
4467ef5e0eecSArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
4468ef5e0eecSArtur Petrosyan 
4469ef5e0eecSArtur Petrosyan 		/*
4470ef5e0eecSArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4471ef5e0eecSArtur Petrosyan 		 * since an interrupt may rise.
4472ef5e0eecSArtur Petrosyan 		 */
4473ef5e0eecSArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4474ef5e0eecSArtur Petrosyan 		break;
4475c74c26f6SArtur Petrosyan 	default:
4476c74c26f6SArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
4477c74c26f6SArtur Petrosyan 		goto unlock;
4478c74c26f6SArtur Petrosyan 	}
4479c74c26f6SArtur Petrosyan 
4480c74c26f6SArtur Petrosyan 	/* Change Root port status, as port status change occurred after resume.*/
4481c74c26f6SArtur Petrosyan 	hsotg->flags.b.port_suspend_change = 1;
4482c74c26f6SArtur Petrosyan 
4483a2a23d3fSGregory Herrero 	/*
4484a2a23d3fSGregory Herrero 	 * Enable power if not already done.
4485a2a23d3fSGregory Herrero 	 * This must not be spinlocked since duration
4486a2a23d3fSGregory Herrero 	 * of this call is unknown.
4487a2a23d3fSGregory Herrero 	 */
4488a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4489a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4490a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, false);
4491a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4492a2a23d3fSGregory Herrero 	}
4493a2a23d3fSGregory Herrero 
4494c74c26f6SArtur Petrosyan 	/* Enable external vbus supply after resuming the port. */
4495a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4496531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_init(hsotg);
4497531ef5ebSAmelie Delaunay 
44985634e016SGregory Herrero 	/* Wait for controller to correctly update D+/D- level */
44995634e016SGregory Herrero 	usleep_range(3000, 5000);
4500c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
45015634e016SGregory Herrero 
4502a2a23d3fSGregory Herrero 	/*
4503a2a23d3fSGregory Herrero 	 * Clear Port Enable and Port Status changes.
4504a2a23d3fSGregory Herrero 	 * Enable Port Power.
4505a2a23d3fSGregory Herrero 	 */
4506f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
4507f25c42b8SGevorg Sahakyan 			HPRT0_ENACHG, HPRT0);
4508a2a23d3fSGregory Herrero 
4509c74c26f6SArtur Petrosyan 	/* Wait for controller to detect Port Connect */
4510c74c26f6SArtur Petrosyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
4511c74c26f6SArtur Petrosyan 	usleep_range(5000, 7000);
4512c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
4513a2a23d3fSGregory Herrero unlock:
4514a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4515a2a23d3fSGregory Herrero 
4516a2a23d3fSGregory Herrero 	return ret;
451799a65798SGregory Herrero }
451899a65798SGregory Herrero 
4519197ba5f4SPaul Zimmerman /* Returns the current frame number */
4520197ba5f4SPaul Zimmerman static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4521197ba5f4SPaul Zimmerman {
4522197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4523197ba5f4SPaul Zimmerman 
4524197ba5f4SPaul Zimmerman 	return dwc2_hcd_get_frame_number(hsotg);
4525197ba5f4SPaul Zimmerman }
4526197ba5f4SPaul Zimmerman 
4527197ba5f4SPaul Zimmerman static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4528197ba5f4SPaul Zimmerman 			       char *fn_name)
4529197ba5f4SPaul Zimmerman {
4530197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
4531197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4532efe357f4SNicholas Mc Guire 	char *pipetype = NULL;
4533efe357f4SNicholas Mc Guire 	char *speed = NULL;
4534197ba5f4SPaul Zimmerman 
4535197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4536197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Device address: %d\n",
4537197ba5f4SPaul Zimmerman 		 usb_pipedevice(urb->pipe));
4538197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint: %d, %s\n",
4539197ba5f4SPaul Zimmerman 		 usb_pipeendpoint(urb->pipe),
4540197ba5f4SPaul Zimmerman 		 usb_pipein(urb->pipe) ? "IN" : "OUT");
4541197ba5f4SPaul Zimmerman 
4542197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4543197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4544197ba5f4SPaul Zimmerman 		pipetype = "CONTROL";
4545197ba5f4SPaul Zimmerman 		break;
4546197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4547197ba5f4SPaul Zimmerman 		pipetype = "BULK";
4548197ba5f4SPaul Zimmerman 		break;
4549197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4550197ba5f4SPaul Zimmerman 		pipetype = "INTERRUPT";
4551197ba5f4SPaul Zimmerman 		break;
4552197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4553197ba5f4SPaul Zimmerman 		pipetype = "ISOCHRONOUS";
4554197ba5f4SPaul Zimmerman 		break;
4555197ba5f4SPaul Zimmerman 	}
4556197ba5f4SPaul Zimmerman 
4557197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint type: %s %s (%s)\n", pipetype,
4558197ba5f4SPaul Zimmerman 		 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4559197ba5f4SPaul Zimmerman 		 "IN" : "OUT");
4560197ba5f4SPaul Zimmerman 
4561197ba5f4SPaul Zimmerman 	switch (urb->dev->speed) {
4562197ba5f4SPaul Zimmerman 	case USB_SPEED_HIGH:
4563197ba5f4SPaul Zimmerman 		speed = "HIGH";
4564197ba5f4SPaul Zimmerman 		break;
4565197ba5f4SPaul Zimmerman 	case USB_SPEED_FULL:
4566197ba5f4SPaul Zimmerman 		speed = "FULL";
4567197ba5f4SPaul Zimmerman 		break;
4568197ba5f4SPaul Zimmerman 	case USB_SPEED_LOW:
4569197ba5f4SPaul Zimmerman 		speed = "LOW";
4570197ba5f4SPaul Zimmerman 		break;
4571197ba5f4SPaul Zimmerman 	default:
4572197ba5f4SPaul Zimmerman 		speed = "UNKNOWN";
4573197ba5f4SPaul Zimmerman 		break;
4574197ba5f4SPaul Zimmerman 	}
4575197ba5f4SPaul Zimmerman 
4576197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Speed: %s\n", speed);
4577babd1839SDouglas Anderson 	dev_vdbg(hsotg->dev, "  Max packet size: %d (%d mult)\n",
4578babd1839SDouglas Anderson 		 usb_endpoint_maxp(&urb->ep->desc),
4579babd1839SDouglas Anderson 		 usb_endpoint_maxp_mult(&urb->ep->desc));
4580babd1839SDouglas Anderson 
4581197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Data buffer length: %d\n",
4582197ba5f4SPaul Zimmerman 		 urb->transfer_buffer_length);
4583197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
4584197ba5f4SPaul Zimmerman 		 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4585197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
4586197ba5f4SPaul Zimmerman 		 urb->setup_packet, (unsigned long)urb->setup_dma);
4587197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Interval: %d\n", urb->interval);
4588197ba5f4SPaul Zimmerman 
4589197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4590197ba5f4SPaul Zimmerman 		int i;
4591197ba5f4SPaul Zimmerman 
4592197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; i++) {
4593197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  ISO Desc %d:\n", i);
4594197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "    offset: %d, length %d\n",
4595197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].offset,
4596197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].length);
4597197ba5f4SPaul Zimmerman 		}
4598197ba5f4SPaul Zimmerman 	}
4599197ba5f4SPaul Zimmerman #endif
4600197ba5f4SPaul Zimmerman }
4601197ba5f4SPaul Zimmerman 
4602197ba5f4SPaul Zimmerman /*
4603197ba5f4SPaul Zimmerman  * Starts processing a USB transfer request specified by a USB Request Block
4604197ba5f4SPaul Zimmerman  * (URB). mem_flags indicates the type of memory allocation to use while
4605197ba5f4SPaul Zimmerman  * processing this URB.
4606197ba5f4SPaul Zimmerman  */
4607197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4608197ba5f4SPaul Zimmerman 				 gfp_t mem_flags)
4609197ba5f4SPaul Zimmerman {
4610197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4611197ba5f4SPaul Zimmerman 	struct usb_host_endpoint *ep = urb->ep;
4612197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *dwc2_urb;
4613197ba5f4SPaul Zimmerman 	int i;
4614197ba5f4SPaul Zimmerman 	int retval;
4615197ba5f4SPaul Zimmerman 	int alloc_bandwidth = 0;
4616197ba5f4SPaul Zimmerman 	u8 ep_type = 0;
4617197ba5f4SPaul Zimmerman 	u32 tflags = 0;
4618197ba5f4SPaul Zimmerman 	void *buf;
4619197ba5f4SPaul Zimmerman 	unsigned long flags;
4620b58e6ceeSMian Yousaf Kaukab 	struct dwc2_qh *qh;
4621b58e6ceeSMian Yousaf Kaukab 	bool qh_allocated = false;
4622b5a468a6SMian Yousaf Kaukab 	struct dwc2_qtd *qtd;
4623197ba5f4SPaul Zimmerman 
4624197ba5f4SPaul Zimmerman 	if (dbg_urb(urb)) {
4625197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4626197ba5f4SPaul Zimmerman 		dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4627197ba5f4SPaul Zimmerman 	}
4628197ba5f4SPaul Zimmerman 
462975f43ac3SArtur Petrosyan 	if (hsotg->in_ppd) {
463075f43ac3SArtur Petrosyan 		retval = dwc2_exit_partial_power_down(hsotg, 0, true);
463175f43ac3SArtur Petrosyan 		if (retval)
463275f43ac3SArtur Petrosyan 			dev_err(hsotg->dev,
463375f43ac3SArtur Petrosyan 				"exit partial_power_down failed\n");
463475f43ac3SArtur Petrosyan 	}
463575f43ac3SArtur Petrosyan 
463616c729f9SArtur Petrosyan 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
463716c729f9SArtur Petrosyan 	    hsotg->bus_suspended) {
463816c729f9SArtur Petrosyan 		if (dwc2_is_device_mode(hsotg))
463916c729f9SArtur Petrosyan 			dwc2_gadget_exit_clock_gating(hsotg, 0);
464016c729f9SArtur Petrosyan 		else
464116c729f9SArtur Petrosyan 			dwc2_host_exit_clock_gating(hsotg, 0);
464216c729f9SArtur Petrosyan 	}
464316c729f9SArtur Petrosyan 
46449da51974SJohn Youn 	if (!ep)
4645197ba5f4SPaul Zimmerman 		return -EINVAL;
4646197ba5f4SPaul Zimmerman 
4647197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4648197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4649197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
4650197ba5f4SPaul Zimmerman 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4651197ba5f4SPaul Zimmerman 			alloc_bandwidth = 1;
4652197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4653197ba5f4SPaul Zimmerman 	}
4654197ba5f4SPaul Zimmerman 
4655197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4656197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4657197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_CONTROL;
4658197ba5f4SPaul Zimmerman 		break;
4659197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4660197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_ISOC;
4661197ba5f4SPaul Zimmerman 		break;
4662197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4663197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_BULK;
4664197ba5f4SPaul Zimmerman 		break;
4665197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4666197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_INT;
4667197ba5f4SPaul Zimmerman 		break;
4668197ba5f4SPaul Zimmerman 	}
4669197ba5f4SPaul Zimmerman 
4670197ba5f4SPaul Zimmerman 	dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4671197ba5f4SPaul Zimmerman 				      mem_flags);
4672197ba5f4SPaul Zimmerman 	if (!dwc2_urb)
4673197ba5f4SPaul Zimmerman 		return -ENOMEM;
4674197ba5f4SPaul Zimmerman 
4675197ba5f4SPaul Zimmerman 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4676197ba5f4SPaul Zimmerman 				  usb_pipeendpoint(urb->pipe), ep_type,
4677197ba5f4SPaul Zimmerman 				  usb_pipein(urb->pipe),
4678babd1839SDouglas Anderson 				  usb_endpoint_maxp(&ep->desc),
4679babd1839SDouglas Anderson 				  usb_endpoint_maxp_mult(&ep->desc));
4680197ba5f4SPaul Zimmerman 
4681197ba5f4SPaul Zimmerman 	buf = urb->transfer_buffer;
4682197ba5f4SPaul Zimmerman 
4683edfbcb32SChristoph Hellwig 	if (hcd_uses_dma(hcd)) {
4684197ba5f4SPaul Zimmerman 		if (!buf && (urb->transfer_dma & 3)) {
4685197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
4686197ba5f4SPaul Zimmerman 				"%s: unaligned transfer with no transfer_buffer",
4687197ba5f4SPaul Zimmerman 				__func__);
4688197ba5f4SPaul Zimmerman 			retval = -EINVAL;
468933ad261aSGregory Herrero 			goto fail0;
4690197ba5f4SPaul Zimmerman 		}
4691197ba5f4SPaul Zimmerman 	}
4692197ba5f4SPaul Zimmerman 
4693197ba5f4SPaul Zimmerman 	if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4694197ba5f4SPaul Zimmerman 		tflags |= URB_GIVEBACK_ASAP;
4695197ba5f4SPaul Zimmerman 	if (urb->transfer_flags & URB_ZERO_PACKET)
4696197ba5f4SPaul Zimmerman 		tflags |= URB_SEND_ZERO_PACKET;
4697197ba5f4SPaul Zimmerman 
4698197ba5f4SPaul Zimmerman 	dwc2_urb->priv = urb;
4699197ba5f4SPaul Zimmerman 	dwc2_urb->buf = buf;
4700197ba5f4SPaul Zimmerman 	dwc2_urb->dma = urb->transfer_dma;
4701197ba5f4SPaul Zimmerman 	dwc2_urb->length = urb->transfer_buffer_length;
4702197ba5f4SPaul Zimmerman 	dwc2_urb->setup_packet = urb->setup_packet;
4703197ba5f4SPaul Zimmerman 	dwc2_urb->setup_dma = urb->setup_dma;
4704197ba5f4SPaul Zimmerman 	dwc2_urb->flags = tflags;
4705197ba5f4SPaul Zimmerman 	dwc2_urb->interval = urb->interval;
4706197ba5f4SPaul Zimmerman 	dwc2_urb->status = -EINPROGRESS;
4707197ba5f4SPaul Zimmerman 
4708197ba5f4SPaul Zimmerman 	for (i = 0; i < urb->number_of_packets; ++i)
4709197ba5f4SPaul Zimmerman 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4710197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].offset,
4711197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].length);
4712197ba5f4SPaul Zimmerman 
4713197ba5f4SPaul Zimmerman 	urb->hcpriv = dwc2_urb;
4714b58e6ceeSMian Yousaf Kaukab 	qh = (struct dwc2_qh *)ep->hcpriv;
4715b58e6ceeSMian Yousaf Kaukab 	/* Create QH for the endpoint if it doesn't exist */
4716b58e6ceeSMian Yousaf Kaukab 	if (!qh) {
4717b58e6ceeSMian Yousaf Kaukab 		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4718b58e6ceeSMian Yousaf Kaukab 		if (!qh) {
4719b58e6ceeSMian Yousaf Kaukab 			retval = -ENOMEM;
4720b58e6ceeSMian Yousaf Kaukab 			goto fail0;
4721b58e6ceeSMian Yousaf Kaukab 		}
4722b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = qh;
4723b58e6ceeSMian Yousaf Kaukab 		qh_allocated = true;
4724b58e6ceeSMian Yousaf Kaukab 	}
4725197ba5f4SPaul Zimmerman 
4726b5a468a6SMian Yousaf Kaukab 	qtd = kzalloc(sizeof(*qtd), mem_flags);
4727b5a468a6SMian Yousaf Kaukab 	if (!qtd) {
4728b5a468a6SMian Yousaf Kaukab 		retval = -ENOMEM;
4729b5a468a6SMian Yousaf Kaukab 		goto fail1;
4730b5a468a6SMian Yousaf Kaukab 	}
4731b5a468a6SMian Yousaf Kaukab 
4732197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4733197ba5f4SPaul Zimmerman 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
4734197ba5f4SPaul Zimmerman 	if (retval)
4735197ba5f4SPaul Zimmerman 		goto fail2;
4736197ba5f4SPaul Zimmerman 
4737b5a468a6SMian Yousaf Kaukab 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4738b5a468a6SMian Yousaf Kaukab 	if (retval)
4739b5a468a6SMian Yousaf Kaukab 		goto fail3;
4740b5a468a6SMian Yousaf Kaukab 
4741197ba5f4SPaul Zimmerman 	if (alloc_bandwidth) {
4742197ba5f4SPaul Zimmerman 		dwc2_allocate_bus_bandwidth(hcd,
4743197ba5f4SPaul Zimmerman 				dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4744197ba5f4SPaul Zimmerman 				urb);
4745197ba5f4SPaul Zimmerman 	}
4746197ba5f4SPaul Zimmerman 
474733ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
474833ad261aSGregory Herrero 
4749197ba5f4SPaul Zimmerman 	return 0;
4750197ba5f4SPaul Zimmerman 
4751b5a468a6SMian Yousaf Kaukab fail3:
4752197ba5f4SPaul Zimmerman 	dwc2_urb->priv = NULL;
4753197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
475416e80218SDouglas Anderson 	if (qh_allocated && qh->channel && qh->channel->qh == qh)
475516e80218SDouglas Anderson 		qh->channel->qh = NULL;
4756b5a468a6SMian Yousaf Kaukab fail2:
475733ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4758197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4759b5a468a6SMian Yousaf Kaukab 	kfree(qtd);
4760b5a468a6SMian Yousaf Kaukab fail1:
4761b58e6ceeSMian Yousaf Kaukab 	if (qh_allocated) {
4762b58e6ceeSMian Yousaf Kaukab 		struct dwc2_qtd *qtd2, *qtd2_tmp;
4763b58e6ceeSMian Yousaf Kaukab 
4764b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = NULL;
4765b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_unlink(hsotg, qh);
4766b58e6ceeSMian Yousaf Kaukab 		/* Free each QTD in the QH's QTD list */
4767b58e6ceeSMian Yousaf Kaukab 		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
4768b58e6ceeSMian Yousaf Kaukab 					 qtd_list_entry)
4769b58e6ceeSMian Yousaf Kaukab 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4770b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_free(hsotg, qh);
4771b58e6ceeSMian Yousaf Kaukab 	}
477233ad261aSGregory Herrero fail0:
4773197ba5f4SPaul Zimmerman 	kfree(dwc2_urb);
4774197ba5f4SPaul Zimmerman 
4775197ba5f4SPaul Zimmerman 	return retval;
4776197ba5f4SPaul Zimmerman }
4777197ba5f4SPaul Zimmerman 
4778197ba5f4SPaul Zimmerman /*
4779197ba5f4SPaul Zimmerman  * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4780197ba5f4SPaul Zimmerman  */
4781197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4782197ba5f4SPaul Zimmerman 				 int status)
4783197ba5f4SPaul Zimmerman {
4784197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4785197ba5f4SPaul Zimmerman 	int rc;
4786197ba5f4SPaul Zimmerman 	unsigned long flags;
4787197ba5f4SPaul Zimmerman 
4788197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4789197ba5f4SPaul Zimmerman 	dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4790197ba5f4SPaul Zimmerman 
4791197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4792197ba5f4SPaul Zimmerman 
4793197ba5f4SPaul Zimmerman 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4794197ba5f4SPaul Zimmerman 	if (rc)
4795197ba5f4SPaul Zimmerman 		goto out;
4796197ba5f4SPaul Zimmerman 
4797197ba5f4SPaul Zimmerman 	if (!urb->hcpriv) {
4798197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4799197ba5f4SPaul Zimmerman 		goto out;
4800197ba5f4SPaul Zimmerman 	}
4801197ba5f4SPaul Zimmerman 
4802197ba5f4SPaul Zimmerman 	rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4803197ba5f4SPaul Zimmerman 
4804197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
4805197ba5f4SPaul Zimmerman 
4806197ba5f4SPaul Zimmerman 	kfree(urb->hcpriv);
4807197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4808197ba5f4SPaul Zimmerman 
4809197ba5f4SPaul Zimmerman 	/* Higher layer software sets URB status */
4810197ba5f4SPaul Zimmerman 	spin_unlock(&hsotg->lock);
4811197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(hcd, urb, status);
4812197ba5f4SPaul Zimmerman 	spin_lock(&hsotg->lock);
4813197ba5f4SPaul Zimmerman 
4814197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4815197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  urb->status = %d\n", urb->status);
4816197ba5f4SPaul Zimmerman out:
4817197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4818197ba5f4SPaul Zimmerman 
4819197ba5f4SPaul Zimmerman 	return rc;
4820197ba5f4SPaul Zimmerman }
4821197ba5f4SPaul Zimmerman 
4822197ba5f4SPaul Zimmerman /*
4823197ba5f4SPaul Zimmerman  * Frees resources in the DWC_otg controller related to a given endpoint. Also
4824197ba5f4SPaul Zimmerman  * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4825197ba5f4SPaul Zimmerman  * must already be dequeued.
4826197ba5f4SPaul Zimmerman  */
4827197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4828197ba5f4SPaul Zimmerman 				       struct usb_host_endpoint *ep)
4829197ba5f4SPaul Zimmerman {
4830197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4831197ba5f4SPaul Zimmerman 
4832197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4833197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4834197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress, ep->hcpriv);
4835197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4836197ba5f4SPaul Zimmerman }
4837197ba5f4SPaul Zimmerman 
4838197ba5f4SPaul Zimmerman /*
4839197ba5f4SPaul Zimmerman  * Resets endpoint specific parameter values, in current version used to reset
4840197ba5f4SPaul Zimmerman  * the data toggle (as a WA). This function can be called from usb_clear_halt
4841197ba5f4SPaul Zimmerman  * routine.
4842197ba5f4SPaul Zimmerman  */
4843197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4844197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep)
4845197ba5f4SPaul Zimmerman {
4846197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4847197ba5f4SPaul Zimmerman 	unsigned long flags;
4848197ba5f4SPaul Zimmerman 
4849197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4850197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4851197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress);
4852197ba5f4SPaul Zimmerman 
4853197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4854197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_reset(hsotg, ep);
4855197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4856197ba5f4SPaul Zimmerman }
4857197ba5f4SPaul Zimmerman 
4858197ba5f4SPaul Zimmerman /*
4859197ba5f4SPaul Zimmerman  * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4860197ba5f4SPaul Zimmerman  * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4861197ba5f4SPaul Zimmerman  * interrupt.
4862197ba5f4SPaul Zimmerman  *
4863197ba5f4SPaul Zimmerman  * This function is called by the USB core when an interrupt occurs
4864197ba5f4SPaul Zimmerman  */
4865197ba5f4SPaul Zimmerman static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4866197ba5f4SPaul Zimmerman {
4867197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4868197ba5f4SPaul Zimmerman 
4869197ba5f4SPaul Zimmerman 	return dwc2_handle_hcd_intr(hsotg);
4870197ba5f4SPaul Zimmerman }
4871197ba5f4SPaul Zimmerman 
4872197ba5f4SPaul Zimmerman /*
4873197ba5f4SPaul Zimmerman  * Creates Status Change bitmap for the root hub and root port. The bitmap is
4874197ba5f4SPaul Zimmerman  * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4875197ba5f4SPaul Zimmerman  * is the status change indicator for the single root port. Returns 1 if either
4876197ba5f4SPaul Zimmerman  * change indicator is 1, otherwise returns 0.
4877197ba5f4SPaul Zimmerman  */
4878197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4879197ba5f4SPaul Zimmerman {
4880197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4881197ba5f4SPaul Zimmerman 
4882197ba5f4SPaul Zimmerman 	buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4883197ba5f4SPaul Zimmerman 	return buf[0] != 0;
4884197ba5f4SPaul Zimmerman }
4885197ba5f4SPaul Zimmerman 
4886197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
4887197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4888197ba5f4SPaul Zimmerman 				 u16 windex, char *buf, u16 wlength)
4889197ba5f4SPaul Zimmerman {
4890197ba5f4SPaul Zimmerman 	int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4891197ba5f4SPaul Zimmerman 					  wvalue, windex, buf, wlength);
4892197ba5f4SPaul Zimmerman 	return retval;
4893197ba5f4SPaul Zimmerman }
4894197ba5f4SPaul Zimmerman 
4895197ba5f4SPaul Zimmerman /* Handles hub TT buffer clear completions */
4896197ba5f4SPaul Zimmerman static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4897197ba5f4SPaul Zimmerman 					       struct usb_host_endpoint *ep)
4898197ba5f4SPaul Zimmerman {
4899197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4900197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
4901197ba5f4SPaul Zimmerman 	unsigned long flags;
4902197ba5f4SPaul Zimmerman 
4903197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
4904197ba5f4SPaul Zimmerman 	if (!qh)
4905197ba5f4SPaul Zimmerman 		return;
4906197ba5f4SPaul Zimmerman 
4907197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4908197ba5f4SPaul Zimmerman 	qh->tt_buffer_dirty = 0;
4909197ba5f4SPaul Zimmerman 
4910197ba5f4SPaul Zimmerman 	if (hsotg->flags.b.port_connect_status)
4911197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4912197ba5f4SPaul Zimmerman 
4913197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4914197ba5f4SPaul Zimmerman }
4915197ba5f4SPaul Zimmerman 
4916ca8b0332SChen Yu /*
4917ca8b0332SChen Yu  * HPRT0_SPD_HIGH_SPEED: high speed
4918ca8b0332SChen Yu  * HPRT0_SPD_FULL_SPEED: full speed
4919ca8b0332SChen Yu  */
4920ca8b0332SChen Yu static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4921ca8b0332SChen Yu {
4922ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4923ca8b0332SChen Yu 
4924ca8b0332SChen Yu 	if (hsotg->params.speed == speed)
4925ca8b0332SChen Yu 		return;
4926ca8b0332SChen Yu 
4927ca8b0332SChen Yu 	hsotg->params.speed = speed;
4928ca8b0332SChen Yu 	queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4929ca8b0332SChen Yu }
4930ca8b0332SChen Yu 
4931ca8b0332SChen Yu static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4932ca8b0332SChen Yu {
4933ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4934ca8b0332SChen Yu 
4935ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4936ca8b0332SChen Yu 		return;
4937ca8b0332SChen Yu 
4938ca8b0332SChen Yu 	/*
4939ca8b0332SChen Yu 	 * On removal, set speed to default high-speed.
4940ca8b0332SChen Yu 	 */
4941ca8b0332SChen Yu 	if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4942ca8b0332SChen Yu 	    udev->parent->speed < USB_SPEED_HIGH) {
4943ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to default high-speed\n");
4944ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4945ca8b0332SChen Yu 	}
4946ca8b0332SChen Yu }
4947ca8b0332SChen Yu 
4948ca8b0332SChen Yu static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4949ca8b0332SChen Yu {
4950ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4951ca8b0332SChen Yu 
4952ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4953ca8b0332SChen Yu 		return 0;
4954ca8b0332SChen Yu 
4955ca8b0332SChen Yu 	if (udev->speed == USB_SPEED_HIGH) {
4956ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to high-speed\n");
4957ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4958ca8b0332SChen Yu 	} else if ((udev->speed == USB_SPEED_FULL ||
4959ca8b0332SChen Yu 				udev->speed == USB_SPEED_LOW)) {
4960ca8b0332SChen Yu 		/*
4961ca8b0332SChen Yu 		 * Change speed setting to full-speed if there's
4962ca8b0332SChen Yu 		 * a full-speed or low-speed device plugged in.
4963ca8b0332SChen Yu 		 */
4964ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to full-speed\n");
4965ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4966ca8b0332SChen Yu 	}
4967ca8b0332SChen Yu 
4968ca8b0332SChen Yu 	return 0;
4969ca8b0332SChen Yu }
4970ca8b0332SChen Yu 
4971197ba5f4SPaul Zimmerman static struct hc_driver dwc2_hc_driver = {
4972197ba5f4SPaul Zimmerman 	.description = "dwc2_hsotg",
4973197ba5f4SPaul Zimmerman 	.product_desc = "DWC OTG Controller",
4974197ba5f4SPaul Zimmerman 	.hcd_priv_size = sizeof(struct wrapper_priv_data),
4975197ba5f4SPaul Zimmerman 
4976197ba5f4SPaul Zimmerman 	.irq = _dwc2_hcd_irq,
49778add17cfSDouglas Anderson 	.flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
4978197ba5f4SPaul Zimmerman 
4979197ba5f4SPaul Zimmerman 	.start = _dwc2_hcd_start,
4980197ba5f4SPaul Zimmerman 	.stop = _dwc2_hcd_stop,
4981197ba5f4SPaul Zimmerman 	.urb_enqueue = _dwc2_hcd_urb_enqueue,
4982197ba5f4SPaul Zimmerman 	.urb_dequeue = _dwc2_hcd_urb_dequeue,
4983197ba5f4SPaul Zimmerman 	.endpoint_disable = _dwc2_hcd_endpoint_disable,
4984197ba5f4SPaul Zimmerman 	.endpoint_reset = _dwc2_hcd_endpoint_reset,
4985197ba5f4SPaul Zimmerman 	.get_frame_number = _dwc2_hcd_get_frame_number,
4986197ba5f4SPaul Zimmerman 
4987197ba5f4SPaul Zimmerman 	.hub_status_data = _dwc2_hcd_hub_status_data,
4988197ba5f4SPaul Zimmerman 	.hub_control = _dwc2_hcd_hub_control,
4989197ba5f4SPaul Zimmerman 	.clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
499099a65798SGregory Herrero 
499199a65798SGregory Herrero 	.bus_suspend = _dwc2_hcd_suspend,
499299a65798SGregory Herrero 	.bus_resume = _dwc2_hcd_resume,
49933bc04e28SDouglas Anderson 
49943bc04e28SDouglas Anderson 	.map_urb_for_dma	= dwc2_map_urb_for_dma,
49953bc04e28SDouglas Anderson 	.unmap_urb_for_dma	= dwc2_unmap_urb_for_dma,
4996197ba5f4SPaul Zimmerman };
4997197ba5f4SPaul Zimmerman 
4998197ba5f4SPaul Zimmerman /*
4999197ba5f4SPaul Zimmerman  * Frees secondary storage associated with the dwc2_hsotg structure contained
5000197ba5f4SPaul Zimmerman  * in the struct usb_hcd field
5001197ba5f4SPaul Zimmerman  */
5002197ba5f4SPaul Zimmerman static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
5003197ba5f4SPaul Zimmerman {
5004197ba5f4SPaul Zimmerman 	u32 ahbcfg;
5005197ba5f4SPaul Zimmerman 	u32 dctl;
5006197ba5f4SPaul Zimmerman 	int i;
5007197ba5f4SPaul Zimmerman 
5008197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
5009197ba5f4SPaul Zimmerman 
5010197ba5f4SPaul Zimmerman 	/* Free memory for QH/QTD lists */
5011197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
501238d2b5fbSDouglas Anderson 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
5013197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
5014197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
5015197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
5016197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
5017197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
5018197ba5f4SPaul Zimmerman 
5019197ba5f4SPaul Zimmerman 	/* Free memory for the host channels */
5020197ba5f4SPaul Zimmerman 	for (i = 0; i < MAX_EPS_CHANNELS; i++) {
5021197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
5022197ba5f4SPaul Zimmerman 
50239da51974SJohn Youn 		if (chan) {
5024197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
5025197ba5f4SPaul Zimmerman 				i, chan);
5026197ba5f4SPaul Zimmerman 			hsotg->hc_ptr_array[i] = NULL;
5027197ba5f4SPaul Zimmerman 			kfree(chan);
5028197ba5f4SPaul Zimmerman 		}
5029197ba5f4SPaul Zimmerman 	}
5030197ba5f4SPaul Zimmerman 
503195832c00SJohn Youn 	if (hsotg->params.host_dma) {
5032197ba5f4SPaul Zimmerman 		if (hsotg->status_buf) {
5033197ba5f4SPaul Zimmerman 			dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
5034197ba5f4SPaul Zimmerman 					  hsotg->status_buf,
5035197ba5f4SPaul Zimmerman 					  hsotg->status_buf_dma);
5036197ba5f4SPaul Zimmerman 			hsotg->status_buf = NULL;
5037197ba5f4SPaul Zimmerman 		}
5038197ba5f4SPaul Zimmerman 	} else {
5039197ba5f4SPaul Zimmerman 		kfree(hsotg->status_buf);
5040197ba5f4SPaul Zimmerman 		hsotg->status_buf = NULL;
5041197ba5f4SPaul Zimmerman 	}
5042197ba5f4SPaul Zimmerman 
5043f25c42b8SGevorg Sahakyan 	ahbcfg = dwc2_readl(hsotg, GAHBCFG);
5044197ba5f4SPaul Zimmerman 
5045197ba5f4SPaul Zimmerman 	/* Disable all interrupts */
5046197ba5f4SPaul Zimmerman 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
5047f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
5048f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
5049197ba5f4SPaul Zimmerman 
5050197ba5f4SPaul Zimmerman 	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
5051f25c42b8SGevorg Sahakyan 		dctl = dwc2_readl(hsotg, DCTL);
5052197ba5f4SPaul Zimmerman 		dctl |= DCTL_SFTDISCON;
5053f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dctl, DCTL);
5054197ba5f4SPaul Zimmerman 	}
5055197ba5f4SPaul Zimmerman 
5056197ba5f4SPaul Zimmerman 	if (hsotg->wq_otg) {
5057197ba5f4SPaul Zimmerman 		if (!cancel_work_sync(&hsotg->wf_otg))
5058197ba5f4SPaul Zimmerman 			flush_workqueue(hsotg->wq_otg);
5059197ba5f4SPaul Zimmerman 		destroy_workqueue(hsotg->wq_otg);
5060197ba5f4SPaul Zimmerman 	}
5061197ba5f4SPaul Zimmerman 
5062c40cf770SDouglas Anderson 	cancel_work_sync(&hsotg->phy_reset_work);
5063c40cf770SDouglas Anderson 
5064197ba5f4SPaul Zimmerman 	del_timer(&hsotg->wkp_timer);
5065197ba5f4SPaul Zimmerman }
5066197ba5f4SPaul Zimmerman 
5067197ba5f4SPaul Zimmerman static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5068197ba5f4SPaul Zimmerman {
5069197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
5070197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
5071197ba5f4SPaul Zimmerman 
5072197ba5f4SPaul Zimmerman 	dwc2_hcd_free(hsotg);
5073197ba5f4SPaul Zimmerman }
5074197ba5f4SPaul Zimmerman 
5075197ba5f4SPaul Zimmerman /*
5076197ba5f4SPaul Zimmerman  * Initializes the HCD. This function allocates memory for and initializes the
5077197ba5f4SPaul Zimmerman  * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5078197ba5f4SPaul Zimmerman  * USB bus with the core and calls the hc_driver->start() function. It returns
5079197ba5f4SPaul Zimmerman  * a negative error on failure.
5080197ba5f4SPaul Zimmerman  */
50814fe160d5SHeiner Kallweit int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5082197ba5f4SPaul Zimmerman {
5083348becdcSHeiner Kallweit 	struct platform_device *pdev = to_platform_device(hsotg->dev);
5084348becdcSHeiner Kallweit 	struct resource *res;
5085197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5086197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
5087197ba5f4SPaul Zimmerman 	u32 hcfg;
5088197ba5f4SPaul Zimmerman 	int i, num_channels;
5089197ba5f4SPaul Zimmerman 	int retval;
5090197ba5f4SPaul Zimmerman 
5091f5500eccSDinh Nguyen 	if (usb_disabled())
5092f5500eccSDinh Nguyen 		return -ENODEV;
5093f5500eccSDinh Nguyen 
5094197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
5095197ba5f4SPaul Zimmerman 
5096197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5097197ba5f4SPaul Zimmerman 
5098f25c42b8SGevorg Sahakyan 	hcfg = dwc2_readl(hsotg, HCFG);
5099197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5100197ba5f4SPaul Zimmerman 
5101197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
51026396bb22SKees Cook 	hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
51036396bb22SKees Cook 					 sizeof(*hsotg->frame_num_array),
51046396bb22SKees Cook 					 GFP_KERNEL);
5105197ba5f4SPaul Zimmerman 	if (!hsotg->frame_num_array)
5106197ba5f4SPaul Zimmerman 		goto error1;
51076396bb22SKees Cook 	hsotg->last_frame_num_array =
51086396bb22SKees Cook 		kcalloc(FRAME_NUM_ARRAY_SIZE,
51096396bb22SKees Cook 			sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
5110197ba5f4SPaul Zimmerman 	if (!hsotg->last_frame_num_array)
5111197ba5f4SPaul Zimmerman 		goto error1;
5112197ba5f4SPaul Zimmerman #endif
5113483bb254SDouglas Anderson 	hsotg->last_frame_num = HFNUM_MAX_FRNUM;
5114197ba5f4SPaul Zimmerman 
5115197ba5f4SPaul Zimmerman 	/* Check if the bus driver or platform code has setup a dma_mask */
511695832c00SJohn Youn 	if (hsotg->params.host_dma &&
51179da51974SJohn Youn 	    !hsotg->dev->dma_mask) {
5118197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
5119197ba5f4SPaul Zimmerman 			 "dma_mask not set, disabling DMA\n");
5120fdb09b3eSNicholas Mc Guire 		hsotg->params.host_dma = false;
512195832c00SJohn Youn 		hsotg->params.dma_desc_enable = false;
5122197ba5f4SPaul Zimmerman 	}
5123197ba5f4SPaul Zimmerman 
5124197ba5f4SPaul Zimmerman 	/* Set device flags indicating whether the HCD supports DMA */
512595832c00SJohn Youn 	if (hsotg->params.host_dma) {
5126197ba5f4SPaul Zimmerman 		if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5127197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set DMA mask\n");
5128197ba5f4SPaul Zimmerman 		if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5129197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
5130197ba5f4SPaul Zimmerman 	}
5131197ba5f4SPaul Zimmerman 
5132ca8b0332SChen Yu 	if (hsotg->params.change_speed_quirk) {
5133ca8b0332SChen Yu 		dwc2_hc_driver.free_dev = dwc2_free_dev;
5134ca8b0332SChen Yu 		dwc2_hc_driver.reset_device = dwc2_reset_device;
5135ca8b0332SChen Yu 	}
5136ca8b0332SChen Yu 
51377b81cb6bSChristoph Hellwig 	if (hsotg->params.host_dma)
51387b81cb6bSChristoph Hellwig 		dwc2_hc_driver.flags |= HCD_DMA;
51397b81cb6bSChristoph Hellwig 
5140197ba5f4SPaul Zimmerman 	hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5141197ba5f4SPaul Zimmerman 	if (!hcd)
5142197ba5f4SPaul Zimmerman 		goto error1;
5143197ba5f4SPaul Zimmerman 
5144197ba5f4SPaul Zimmerman 	hcd->has_tt = 1;
5145197ba5f4SPaul Zimmerman 
5146348becdcSHeiner Kallweit 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5147348becdcSHeiner Kallweit 	hcd->rsrc_start = res->start;
5148348becdcSHeiner Kallweit 	hcd->rsrc_len = resource_size(res);
5149348becdcSHeiner Kallweit 
5150197ba5f4SPaul Zimmerman 	((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
5151197ba5f4SPaul Zimmerman 	hsotg->priv = hcd;
5152197ba5f4SPaul Zimmerman 
5153197ba5f4SPaul Zimmerman 	/*
5154197ba5f4SPaul Zimmerman 	 * Disable the global interrupt until all the interrupt handlers are
5155197ba5f4SPaul Zimmerman 	 * installed
5156197ba5f4SPaul Zimmerman 	 */
5157197ba5f4SPaul Zimmerman 	dwc2_disable_global_interrupts(hsotg);
5158197ba5f4SPaul Zimmerman 
5159197ba5f4SPaul Zimmerman 	/* Initialize the DWC_otg core, and select the Phy type */
51600fe239bcSDouglas Anderson 	retval = dwc2_core_init(hsotg, true);
5161197ba5f4SPaul Zimmerman 	if (retval)
5162197ba5f4SPaul Zimmerman 		goto error2;
5163197ba5f4SPaul Zimmerman 
5164197ba5f4SPaul Zimmerman 	/* Create new workqueue and init work */
5165197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5166ec7b1268SBhaktipriya Shridhar 	hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
5167197ba5f4SPaul Zimmerman 	if (!hsotg->wq_otg) {
5168197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Failed to create workqueue\n");
5169197ba5f4SPaul Zimmerman 		goto error2;
5170197ba5f4SPaul Zimmerman 	}
5171197ba5f4SPaul Zimmerman 	INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5172197ba5f4SPaul Zimmerman 
5173e99e88a9SKees Cook 	timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
5174197ba5f4SPaul Zimmerman 
5175197ba5f4SPaul Zimmerman 	/* Initialize the non-periodic schedule */
5176197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
517738d2b5fbSDouglas Anderson 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
5178197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5179197ba5f4SPaul Zimmerman 
5180197ba5f4SPaul Zimmerman 	/* Initialize the periodic schedule */
5181197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5182197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5183197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5184197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5185197ba5f4SPaul Zimmerman 
5186c9c8ac01SDouglas Anderson 	INIT_LIST_HEAD(&hsotg->split_order);
5187c9c8ac01SDouglas Anderson 
5188197ba5f4SPaul Zimmerman 	/*
5189197ba5f4SPaul Zimmerman 	 * Create a host channel descriptor for each host channel implemented
5190197ba5f4SPaul Zimmerman 	 * in the controller. Initialize the channel descriptor array.
5191197ba5f4SPaul Zimmerman 	 */
5192197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->free_hc_list);
5193bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
5194197ba5f4SPaul Zimmerman 	memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5195197ba5f4SPaul Zimmerman 
5196197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
5197197ba5f4SPaul Zimmerman 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
51989da51974SJohn Youn 		if (!channel)
5199197ba5f4SPaul Zimmerman 			goto error3;
5200197ba5f4SPaul Zimmerman 		channel->hc_num = i;
5201c9c8ac01SDouglas Anderson 		INIT_LIST_HEAD(&channel->split_order_list_entry);
5202197ba5f4SPaul Zimmerman 		hsotg->hc_ptr_array[i] = channel;
5203197ba5f4SPaul Zimmerman 	}
5204197ba5f4SPaul Zimmerman 
5205c40cf770SDouglas Anderson 	/* Initialize work */
5206197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5207197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5208c40cf770SDouglas Anderson 	INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
5209197ba5f4SPaul Zimmerman 
5210197ba5f4SPaul Zimmerman 	/*
5211197ba5f4SPaul Zimmerman 	 * Allocate space for storing data on status transactions. Normally no
5212197ba5f4SPaul Zimmerman 	 * data is sent, but this space acts as a bit bucket. This must be
5213197ba5f4SPaul Zimmerman 	 * done after usb_add_hcd since that function allocates the DMA buffer
5214197ba5f4SPaul Zimmerman 	 * pool.
5215197ba5f4SPaul Zimmerman 	 */
521695832c00SJohn Youn 	if (hsotg->params.host_dma)
5217197ba5f4SPaul Zimmerman 		hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5218197ba5f4SPaul Zimmerman 					DWC2_HCD_STATUS_BUF_SIZE,
5219197ba5f4SPaul Zimmerman 					&hsotg->status_buf_dma, GFP_KERNEL);
5220197ba5f4SPaul Zimmerman 	else
5221197ba5f4SPaul Zimmerman 		hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5222197ba5f4SPaul Zimmerman 					  GFP_KERNEL);
5223197ba5f4SPaul Zimmerman 
5224197ba5f4SPaul Zimmerman 	if (!hsotg->status_buf)
5225197ba5f4SPaul Zimmerman 		goto error3;
5226197ba5f4SPaul Zimmerman 
52273b5fcc9aSGregory Herrero 	/*
52283b5fcc9aSGregory Herrero 	 * Create kmem caches to handle descriptor buffers in descriptor
52293b5fcc9aSGregory Herrero 	 * DMA mode.
52303b5fcc9aSGregory Herrero 	 * Alignment must be set to 512 bytes.
52313b5fcc9aSGregory Herrero 	 */
5232bea8e86cSJohn Youn 	if (hsotg->params.dma_desc_enable ||
5233bea8e86cSJohn Youn 	    hsotg->params.dma_desc_fs_enable) {
52343b5fcc9aSGregory Herrero 		hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
5235ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52363b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
52373b5fcc9aSGregory Herrero 				NULL);
52383b5fcc9aSGregory Herrero 		if (!hsotg->desc_gen_cache) {
52393b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52403b5fcc9aSGregory Herrero 				"unable to create dwc2 generic desc cache\n");
52413b5fcc9aSGregory Herrero 
52423b5fcc9aSGregory Herrero 			/*
52433b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52443b5fcc9aSGregory Herrero 			 * usable.
52453b5fcc9aSGregory Herrero 			 */
524695832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
524795832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
52483b5fcc9aSGregory Herrero 		}
52493b5fcc9aSGregory Herrero 
52503b5fcc9aSGregory Herrero 		hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
5251ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52523b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
52533b5fcc9aSGregory Herrero 		if (!hsotg->desc_hsisoc_cache) {
52543b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52553b5fcc9aSGregory Herrero 				"unable to create dwc2 hs isoc desc cache\n");
52563b5fcc9aSGregory Herrero 
52573b5fcc9aSGregory Herrero 			kmem_cache_destroy(hsotg->desc_gen_cache);
52583b5fcc9aSGregory Herrero 
52593b5fcc9aSGregory Herrero 			/*
52603b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52613b5fcc9aSGregory Herrero 			 * usable.
52623b5fcc9aSGregory Herrero 			 */
526395832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
526495832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
52653b5fcc9aSGregory Herrero 		}
52663b5fcc9aSGregory Herrero 	}
52673b5fcc9aSGregory Herrero 
5268af424a41SWilliam Wu 	if (hsotg->params.host_dma) {
5269af424a41SWilliam Wu 		/*
5270af424a41SWilliam Wu 		 * Create kmem caches to handle non-aligned buffer
5271af424a41SWilliam Wu 		 * in Buffer DMA mode.
5272af424a41SWilliam Wu 		 */
5273af424a41SWilliam Wu 		hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
5274af424a41SWilliam Wu 						DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
5275af424a41SWilliam Wu 						SLAB_CACHE_DMA, NULL);
5276af424a41SWilliam Wu 		if (!hsotg->unaligned_cache)
5277af424a41SWilliam Wu 			dev_err(hsotg->dev,
5278af424a41SWilliam Wu 				"unable to create dwc2 unaligned cache\n");
5279af424a41SWilliam Wu 	}
5280af424a41SWilliam Wu 
5281197ba5f4SPaul Zimmerman 	hsotg->otg_port = 1;
5282197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
5283197ba5f4SPaul Zimmerman 	hsotg->frame_list_dma = 0;
5284197ba5f4SPaul Zimmerman 	hsotg->periodic_qh_count = 0;
5285197ba5f4SPaul Zimmerman 
5286197ba5f4SPaul Zimmerman 	/* Initiate lx_state to L3 disconnected state */
5287197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L3;
5288197ba5f4SPaul Zimmerman 
5289197ba5f4SPaul Zimmerman 	hcd->self.otg_port = hsotg->otg_port;
5290197ba5f4SPaul Zimmerman 
5291197ba5f4SPaul Zimmerman 	/* Don't support SG list at this point */
5292197ba5f4SPaul Zimmerman 	hcd->self.sg_tablesize = 0;
5293197ba5f4SPaul Zimmerman 
52949df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
52959df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, &hcd->self);
52969df4ceacSMian Yousaf Kaukab 
5297197ba5f4SPaul Zimmerman 	/*
5298197ba5f4SPaul Zimmerman 	 * Finish generic HCD initialization and start the HCD. This function
5299197ba5f4SPaul Zimmerman 	 * allocates the DMA buffer pool, registers the USB bus, requests the
5300197ba5f4SPaul Zimmerman 	 * IRQ line, and calls hcd_start method.
5301197ba5f4SPaul Zimmerman 	 */
53024fe160d5SHeiner Kallweit 	retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
5303197ba5f4SPaul Zimmerman 	if (retval < 0)
53043b5fcc9aSGregory Herrero 		goto error4;
5305197ba5f4SPaul Zimmerman 
5306ec513b16SLinus Torvalds 	device_wakeup_enable(hcd->self.controller);
5307ec513b16SLinus Torvalds 
5308197ba5f4SPaul Zimmerman 	dwc2_hcd_dump_state(hsotg);
5309197ba5f4SPaul Zimmerman 
5310197ba5f4SPaul Zimmerman 	dwc2_enable_global_interrupts(hsotg);
5311197ba5f4SPaul Zimmerman 
5312197ba5f4SPaul Zimmerman 	return 0;
5313197ba5f4SPaul Zimmerman 
53143b5fcc9aSGregory Herrero error4:
5315af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53163b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5317af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
5318197ba5f4SPaul Zimmerman error3:
5319197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5320197ba5f4SPaul Zimmerman error2:
5321197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5322197ba5f4SPaul Zimmerman error1:
5323197ba5f4SPaul Zimmerman 
5324197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5325197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5326197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5327197ba5f4SPaul Zimmerman #endif
5328197ba5f4SPaul Zimmerman 
5329197ba5f4SPaul Zimmerman 	dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
5330197ba5f4SPaul Zimmerman 	return retval;
5331197ba5f4SPaul Zimmerman }
5332197ba5f4SPaul Zimmerman 
5333197ba5f4SPaul Zimmerman /*
5334197ba5f4SPaul Zimmerman  * Removes the HCD.
5335197ba5f4SPaul Zimmerman  * Frees memory and resources associated with the HCD and deregisters the bus.
5336197ba5f4SPaul Zimmerman  */
5337197ba5f4SPaul Zimmerman void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
5338197ba5f4SPaul Zimmerman {
5339197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5340197ba5f4SPaul Zimmerman 
5341197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
5342197ba5f4SPaul Zimmerman 
5343197ba5f4SPaul Zimmerman 	hcd = dwc2_hsotg_to_hcd(hsotg);
5344197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
5345197ba5f4SPaul Zimmerman 
5346197ba5f4SPaul Zimmerman 	if (!hcd) {
5347197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
5348197ba5f4SPaul Zimmerman 			__func__);
5349197ba5f4SPaul Zimmerman 		return;
5350197ba5f4SPaul Zimmerman 	}
5351197ba5f4SPaul Zimmerman 
53529df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
53539df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, NULL);
53549df4ceacSMian Yousaf Kaukab 
5355197ba5f4SPaul Zimmerman 	usb_remove_hcd(hcd);
5356197ba5f4SPaul Zimmerman 	hsotg->priv = NULL;
53573b5fcc9aSGregory Herrero 
5358af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53593b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5360af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
53613b5fcc9aSGregory Herrero 
5362197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5363197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5364197ba5f4SPaul Zimmerman 
5365197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5366197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5367197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5368197ba5f4SPaul Zimmerman #endif
5369197ba5f4SPaul Zimmerman }
537058e52ff6SJohn Youn 
537158e52ff6SJohn Youn /**
537258e52ff6SJohn Youn  * dwc2_backup_host_registers() - Backup controller host registers.
537358e52ff6SJohn Youn  * When suspending usb bus, registers needs to be backuped
537458e52ff6SJohn Youn  * if controller power is disabled once suspended.
537558e52ff6SJohn Youn  *
537658e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
537758e52ff6SJohn Youn  */
537858e52ff6SJohn Youn int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
537958e52ff6SJohn Youn {
538058e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
538158e52ff6SJohn Youn 	int i;
538258e52ff6SJohn Youn 
538358e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
538458e52ff6SJohn Youn 
538558e52ff6SJohn Youn 	/* Backup Host regs */
538658e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
5387f25c42b8SGevorg Sahakyan 	hr->hcfg = dwc2_readl(hsotg, HCFG);
5388f25c42b8SGevorg Sahakyan 	hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
5389bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5390f25c42b8SGevorg Sahakyan 		hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
539158e52ff6SJohn Youn 
539258e52ff6SJohn Youn 	hr->hprt0 = dwc2_read_hprt0(hsotg);
5393f25c42b8SGevorg Sahakyan 	hr->hfir = dwc2_readl(hsotg, HFIR);
5394f25c42b8SGevorg Sahakyan 	hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
539558e52ff6SJohn Youn 	hr->valid = true;
539658e52ff6SJohn Youn 
539758e52ff6SJohn Youn 	return 0;
539858e52ff6SJohn Youn }
539958e52ff6SJohn Youn 
540058e52ff6SJohn Youn /**
540158e52ff6SJohn Youn  * dwc2_restore_host_registers() - Restore controller host registers.
540258e52ff6SJohn Youn  * When resuming usb bus, device registers needs to be restored
540358e52ff6SJohn Youn  * if controller power were disabled.
540458e52ff6SJohn Youn  *
540558e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
540658e52ff6SJohn Youn  */
540758e52ff6SJohn Youn int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
540858e52ff6SJohn Youn {
540958e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
541058e52ff6SJohn Youn 	int i;
541158e52ff6SJohn Youn 
541258e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
541358e52ff6SJohn Youn 
541458e52ff6SJohn Youn 	/* Restore host regs */
541558e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
541658e52ff6SJohn Youn 	if (!hr->valid) {
541758e52ff6SJohn Youn 		dev_err(hsotg->dev, "%s: no host registers to restore\n",
541858e52ff6SJohn Youn 			__func__);
541958e52ff6SJohn Youn 		return -EINVAL;
542058e52ff6SJohn Youn 	}
542158e52ff6SJohn Youn 	hr->valid = false;
542258e52ff6SJohn Youn 
5423f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5424f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
542558e52ff6SJohn Youn 
5426bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5427f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
542858e52ff6SJohn Youn 
5429f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hprt0, HPRT0);
5430f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hfir, HFIR);
5431f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
543258e52ff6SJohn Youn 	hsotg->frame_number = 0;
543358e52ff6SJohn Youn 
543458e52ff6SJohn Youn 	return 0;
543558e52ff6SJohn Youn }
5436c5c403dcSVardan Mikayelyan 
5437c5c403dcSVardan Mikayelyan /**
5438c5c403dcSVardan Mikayelyan  * dwc2_host_enter_hibernation() - Put controller in Hibernation.
5439c5c403dcSVardan Mikayelyan  *
5440c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5441c5c403dcSVardan Mikayelyan  */
5442c5c403dcSVardan Mikayelyan int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
5443c5c403dcSVardan Mikayelyan {
5444c5c403dcSVardan Mikayelyan 	unsigned long flags;
5445c5c403dcSVardan Mikayelyan 	int ret = 0;
5446c5c403dcSVardan Mikayelyan 	u32 hprt0;
5447c5c403dcSVardan Mikayelyan 	u32 pcgcctl;
5448c5c403dcSVardan Mikayelyan 	u32 gusbcfg;
5449c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5450c5c403dcSVardan Mikayelyan 
5451c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
5452c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_global_registers(hsotg);
5453c5c403dcSVardan Mikayelyan 	if (ret) {
5454c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5455c5c403dcSVardan Mikayelyan 			__func__);
5456c5c403dcSVardan Mikayelyan 		return ret;
5457c5c403dcSVardan Mikayelyan 	}
5458c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_host_registers(hsotg);
5459c5c403dcSVardan Mikayelyan 	if (ret) {
5460c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5461c5c403dcSVardan Mikayelyan 			__func__);
5462c5c403dcSVardan Mikayelyan 		return ret;
5463c5c403dcSVardan Mikayelyan 	}
5464c5c403dcSVardan Mikayelyan 
5465c5c403dcSVardan Mikayelyan 	/* Enter USB Suspend Mode */
5466f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5467c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_SUSP;
5468c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5469f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5470c5c403dcSVardan Mikayelyan 
5471c5c403dcSVardan Mikayelyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
54725e3bbae8SArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
547307b8dc55SColin Ian King 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
5474c5c403dcSVardan Mikayelyan 
5475c5c403dcSVardan Mikayelyan 	/*
5476c5c403dcSVardan Mikayelyan 	 * We need to disable interrupts to prevent servicing of any IRQ
5477c5c403dcSVardan Mikayelyan 	 * during going to hibernation
5478c5c403dcSVardan Mikayelyan 	 */
5479c5c403dcSVardan Mikayelyan 	spin_lock_irqsave(&hsotg->lock, flags);
5480c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L2;
5481c5c403dcSVardan Mikayelyan 
5482f25c42b8SGevorg Sahakyan 	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
5483c5c403dcSVardan Mikayelyan 	if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
5484c5c403dcSVardan Mikayelyan 		/* ULPI interface */
5485c5c403dcSVardan Mikayelyan 		/* Suspend the Phy Clock */
5486f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5487c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5488f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5489c5c403dcSVardan Mikayelyan 		udelay(10);
5490c5c403dcSVardan Mikayelyan 
5491f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5492c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5493f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5494c5c403dcSVardan Mikayelyan 		udelay(10);
5495c5c403dcSVardan Mikayelyan 	} else {
5496c5c403dcSVardan Mikayelyan 		/* UTMI+ Interface */
5497f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5498c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5499f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5500c5c403dcSVardan Mikayelyan 		udelay(10);
5501c5c403dcSVardan Mikayelyan 
5502f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5503c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5504f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5505c5c403dcSVardan Mikayelyan 		udelay(10);
5506c5c403dcSVardan Mikayelyan 	}
5507c5c403dcSVardan Mikayelyan 
5508c5c403dcSVardan Mikayelyan 	/* Enable interrupts from wake up logic */
5509f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5510c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PMUINTSEL;
5511f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5512c5c403dcSVardan Mikayelyan 	udelay(10);
5513c5c403dcSVardan Mikayelyan 
5514c5c403dcSVardan Mikayelyan 	/* Unmask host mode interrupts in GPWRDN */
5515f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5516c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_DISCONN_DET_MSK;
5517c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5518c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5519f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5520c5c403dcSVardan Mikayelyan 	udelay(10);
5521c5c403dcSVardan Mikayelyan 
5522c5c403dcSVardan Mikayelyan 	/* Enable Power Down Clamp */
5523f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5524c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNCLMP;
5525f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5526c5c403dcSVardan Mikayelyan 	udelay(10);
5527c5c403dcSVardan Mikayelyan 
5528c5c403dcSVardan Mikayelyan 	/* Switch off VDD */
5529f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5530c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNSWTCH;
5531f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5532c5c403dcSVardan Mikayelyan 
5533c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 1;
5534c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 1;
5535c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation completed\n");
5536c5c403dcSVardan Mikayelyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
5537c5c403dcSVardan Mikayelyan 	return ret;
5538c5c403dcSVardan Mikayelyan }
5539c5c403dcSVardan Mikayelyan 
5540c5c403dcSVardan Mikayelyan /*
5541c5c403dcSVardan Mikayelyan  * dwc2_host_exit_hibernation()
5542c5c403dcSVardan Mikayelyan  *
5543c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5544c5c403dcSVardan Mikayelyan  * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5545c5c403dcSVardan Mikayelyan  * @param reset: indicates whether resume is initiated by Reset.
5546c5c403dcSVardan Mikayelyan  *
5547c5c403dcSVardan Mikayelyan  * Return: non-zero if failed to enter to hibernation.
5548c5c403dcSVardan Mikayelyan  *
5549c5c403dcSVardan Mikayelyan  * This function is for exiting from Host mode hibernation by
5550c5c403dcSVardan Mikayelyan  * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
5551c5c403dcSVardan Mikayelyan  */
5552c5c403dcSVardan Mikayelyan int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
5553c5c403dcSVardan Mikayelyan 			       int reset)
5554c5c403dcSVardan Mikayelyan {
5555c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5556c5c403dcSVardan Mikayelyan 	u32 hprt0;
5557c5c403dcSVardan Mikayelyan 	int ret = 0;
5558c5c403dcSVardan Mikayelyan 	struct dwc2_gregs_backup *gr;
5559c5c403dcSVardan Mikayelyan 	struct dwc2_hregs_backup *hr;
5560c5c403dcSVardan Mikayelyan 
5561c5c403dcSVardan Mikayelyan 	gr = &hsotg->gr_backup;
5562c5c403dcSVardan Mikayelyan 	hr = &hsotg->hr_backup;
5563c5c403dcSVardan Mikayelyan 
5564c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev,
5565c5c403dcSVardan Mikayelyan 		"%s: called with rem_wakeup = %d reset = %d\n",
5566c5c403dcSVardan Mikayelyan 		__func__, rem_wakeup, reset);
5567c5c403dcSVardan Mikayelyan 
5568c5c403dcSVardan Mikayelyan 	dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
5569c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5570c5c403dcSVardan Mikayelyan 
5571c5c403dcSVardan Mikayelyan 	/*
5572c5c403dcSVardan Mikayelyan 	 * This step is not described in functional spec but if not wait for
5573c5c403dcSVardan Mikayelyan 	 * this delay, mismatch interrupts occurred because just after restore
5574c5c403dcSVardan Mikayelyan 	 * core is in Device mode(gintsts.curmode == 0)
5575c5c403dcSVardan Mikayelyan 	 */
5576c5c403dcSVardan Mikayelyan 	mdelay(100);
5577c5c403dcSVardan Mikayelyan 
5578c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5579f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5580c5c403dcSVardan Mikayelyan 
5581c5c403dcSVardan Mikayelyan 	/* De-assert Restore */
5582f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5583c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_RESTORE;
5584f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5585c5c403dcSVardan Mikayelyan 	udelay(10);
5586c5c403dcSVardan Mikayelyan 
5587c5c403dcSVardan Mikayelyan 	/* Restore GUSBCFG, HCFG */
5588f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5589f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5590c5c403dcSVardan Mikayelyan 
5591c5c403dcSVardan Mikayelyan 	/* De-assert Wakeup Logic */
5592f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5593c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_PMUACTV;
5594f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5595c5c403dcSVardan Mikayelyan 	udelay(10);
5596c5c403dcSVardan Mikayelyan 
5597c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5598c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5599c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5600c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5601f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5602c5c403dcSVardan Mikayelyan 
5603c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5604c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5605c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5606c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5607c5c403dcSVardan Mikayelyan 
5608c5c403dcSVardan Mikayelyan 	if (reset) {
5609c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RST;
5610f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5611c5c403dcSVardan Mikayelyan 
5612c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5613c5c403dcSVardan Mikayelyan 		mdelay(60);
5614c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RST;
5615f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5616c5c403dcSVardan Mikayelyan 	} else {
5617c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RES;
5618f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5619c5c403dcSVardan Mikayelyan 
5620c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5621c5c403dcSVardan Mikayelyan 		mdelay(100);
5622c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RES;
5623f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5624c5c403dcSVardan Mikayelyan 	}
5625c5c403dcSVardan Mikayelyan 	/* Clear all interrupt status */
5626f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5627c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_CONNDET;
5628c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_ENACHG;
5629c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5630f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5631c5c403dcSVardan Mikayelyan 
5632f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5633c5c403dcSVardan Mikayelyan 
5634c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5635f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5636c5c403dcSVardan Mikayelyan 
5637c5c403dcSVardan Mikayelyan 	/* Restore global registers */
5638c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_global_registers(hsotg);
5639c5c403dcSVardan Mikayelyan 	if (ret) {
5640c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore registers\n",
5641c5c403dcSVardan Mikayelyan 			__func__);
5642c5c403dcSVardan Mikayelyan 		return ret;
5643c5c403dcSVardan Mikayelyan 	}
5644c5c403dcSVardan Mikayelyan 
5645c5c403dcSVardan Mikayelyan 	/* Restore host registers */
5646c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_host_registers(hsotg);
5647c5c403dcSVardan Mikayelyan 	if (ret) {
5648c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5649c5c403dcSVardan Mikayelyan 			__func__);
5650c5c403dcSVardan Mikayelyan 		return ret;
5651c5c403dcSVardan Mikayelyan 	}
5652c5c403dcSVardan Mikayelyan 
565322bb5cfdSArtur Petrosyan 	dwc2_hcd_rem_wakeup(hsotg);
565422bb5cfdSArtur Petrosyan 
5655c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5656c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 0;
5657c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L0;
5658c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
5659c5c403dcSVardan Mikayelyan 	return ret;
5660c5c403dcSVardan Mikayelyan }
5661c846b03fSDouglas Anderson 
5662c846b03fSDouglas Anderson bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
5663c846b03fSDouglas Anderson {
5664c846b03fSDouglas Anderson 	struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
5665c846b03fSDouglas Anderson 
5666c846b03fSDouglas Anderson 	/* If the controller isn't allowed to wakeup then we can power off. */
5667c846b03fSDouglas Anderson 	if (!device_may_wakeup(dwc2->dev))
5668c846b03fSDouglas Anderson 		return true;
5669c846b03fSDouglas Anderson 
5670c846b03fSDouglas Anderson 	/*
5671c846b03fSDouglas Anderson 	 * We don't want to power off the PHY if something under the
5672c846b03fSDouglas Anderson 	 * root hub has wakeup enabled.
5673c846b03fSDouglas Anderson 	 */
5674c846b03fSDouglas Anderson 	if (usb_wakeup_enabled_descendants(root_hub))
5675c846b03fSDouglas Anderson 		return false;
5676c846b03fSDouglas Anderson 
5677c846b03fSDouglas Anderson 	/* No reason to keep the PHY powered, so allow poweroff */
5678c846b03fSDouglas Anderson 	return true;
5679c846b03fSDouglas Anderson }
56809ce9e5adSArtur Petrosyan 
56819ce9e5adSArtur Petrosyan /**
56829ce9e5adSArtur Petrosyan  * dwc2_host_enter_partial_power_down() - Put controller in partial
56839ce9e5adSArtur Petrosyan  * power down.
56849ce9e5adSArtur Petrosyan  *
56859ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
56869ce9e5adSArtur Petrosyan  *
56879ce9e5adSArtur Petrosyan  * Return: non-zero if failed to enter host partial power down.
56889ce9e5adSArtur Petrosyan  *
56899ce9e5adSArtur Petrosyan  * This function is for entering Host mode partial power down.
56909ce9e5adSArtur Petrosyan  */
56919ce9e5adSArtur Petrosyan int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg)
56929ce9e5adSArtur Petrosyan {
56939ce9e5adSArtur Petrosyan 	u32 pcgcctl;
56949ce9e5adSArtur Petrosyan 	u32 hprt0;
56959ce9e5adSArtur Petrosyan 	int ret = 0;
56969ce9e5adSArtur Petrosyan 
56979ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down started.\n");
56989ce9e5adSArtur Petrosyan 
56999ce9e5adSArtur Petrosyan 	/* Put this port in suspend mode. */
57009ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
57019ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
57029ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
57039ce9e5adSArtur Petrosyan 	udelay(5);
57049ce9e5adSArtur Petrosyan 
57059ce9e5adSArtur Petrosyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
57069ce9e5adSArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
57079ce9e5adSArtur Petrosyan 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
57089ce9e5adSArtur Petrosyan 
57099ce9e5adSArtur Petrosyan 	/* Backup all registers */
57109ce9e5adSArtur Petrosyan 	ret = dwc2_backup_global_registers(hsotg);
57119ce9e5adSArtur Petrosyan 	if (ret) {
57129ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
57139ce9e5adSArtur Petrosyan 			__func__);
57149ce9e5adSArtur Petrosyan 		return ret;
57159ce9e5adSArtur Petrosyan 	}
57169ce9e5adSArtur Petrosyan 
57179ce9e5adSArtur Petrosyan 	ret = dwc2_backup_host_registers(hsotg);
57189ce9e5adSArtur Petrosyan 	if (ret) {
57199ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
57209ce9e5adSArtur Petrosyan 			__func__);
57219ce9e5adSArtur Petrosyan 		return ret;
57229ce9e5adSArtur Petrosyan 	}
57239ce9e5adSArtur Petrosyan 
57249ce9e5adSArtur Petrosyan 	/*
57259ce9e5adSArtur Petrosyan 	 * Clear any pending interrupts since dwc2 will not be able to
57269ce9e5adSArtur Petrosyan 	 * clear them after entering partial_power_down.
57279ce9e5adSArtur Petrosyan 	 */
57289ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
57299ce9e5adSArtur Petrosyan 
57309ce9e5adSArtur Petrosyan 	/* Put the controller in low power state */
57319ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
57329ce9e5adSArtur Petrosyan 
57339ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_PWRCLMP;
57349ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57359ce9e5adSArtur Petrosyan 	udelay(5);
57369ce9e5adSArtur Petrosyan 
57379ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
57389ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57399ce9e5adSArtur Petrosyan 	udelay(5);
57409ce9e5adSArtur Petrosyan 
57419ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_STOPPCLK;
57429ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57439ce9e5adSArtur Petrosyan 
57449ce9e5adSArtur Petrosyan 	/* Set in_ppd flag to 1 as here core enters suspend. */
57459ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 1;
57469ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
57479ce9e5adSArtur Petrosyan 	hsotg->bus_suspended = true;
57489ce9e5adSArtur Petrosyan 
57499ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down completed.\n");
57509ce9e5adSArtur Petrosyan 
57519ce9e5adSArtur Petrosyan 	return ret;
57529ce9e5adSArtur Petrosyan }
57539ce9e5adSArtur Petrosyan 
57549ce9e5adSArtur Petrosyan /*
57559ce9e5adSArtur Petrosyan  * dwc2_host_exit_partial_power_down() - Exit controller from host partial
57569ce9e5adSArtur Petrosyan  * power down.
57579ce9e5adSArtur Petrosyan  *
57589ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
57599ce9e5adSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by Reset.
57609ce9e5adSArtur Petrosyan  * @restore: indicates whether need to restore the registers or not.
57619ce9e5adSArtur Petrosyan  *
57629ce9e5adSArtur Petrosyan  * Return: non-zero if failed to exit host partial power down.
57639ce9e5adSArtur Petrosyan  *
57649ce9e5adSArtur Petrosyan  * This function is for exiting from Host mode partial power down.
57659ce9e5adSArtur Petrosyan  */
57669ce9e5adSArtur Petrosyan int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg,
57679ce9e5adSArtur Petrosyan 				      int rem_wakeup, bool restore)
57689ce9e5adSArtur Petrosyan {
57699ce9e5adSArtur Petrosyan 	u32 pcgcctl;
57709ce9e5adSArtur Petrosyan 	int ret = 0;
57719ce9e5adSArtur Petrosyan 	u32 hprt0;
57729ce9e5adSArtur Petrosyan 
57739ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down started.\n");
57749ce9e5adSArtur Petrosyan 
57759ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
57769ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_STOPPCLK;
57779ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57789ce9e5adSArtur Petrosyan 	udelay(5);
57799ce9e5adSArtur Petrosyan 
57809ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
57819ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_PWRCLMP;
57829ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57839ce9e5adSArtur Petrosyan 	udelay(5);
57849ce9e5adSArtur Petrosyan 
57859ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
57869ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
57879ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57889ce9e5adSArtur Petrosyan 
57899ce9e5adSArtur Petrosyan 	udelay(100);
57909ce9e5adSArtur Petrosyan 	if (restore) {
57919ce9e5adSArtur Petrosyan 		ret = dwc2_restore_global_registers(hsotg);
57929ce9e5adSArtur Petrosyan 		if (ret) {
57939ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore registers\n",
57949ce9e5adSArtur Petrosyan 				__func__);
57959ce9e5adSArtur Petrosyan 			return ret;
57969ce9e5adSArtur Petrosyan 		}
57979ce9e5adSArtur Petrosyan 
57989ce9e5adSArtur Petrosyan 		ret = dwc2_restore_host_registers(hsotg);
57999ce9e5adSArtur Petrosyan 		if (ret) {
58009ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore host registers\n",
58019ce9e5adSArtur Petrosyan 				__func__);
58029ce9e5adSArtur Petrosyan 			return ret;
58039ce9e5adSArtur Petrosyan 		}
58049ce9e5adSArtur Petrosyan 	}
58059ce9e5adSArtur Petrosyan 
58069ce9e5adSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
58079ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
58089ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_RES;
58099ce9e5adSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
58109ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
58119ce9e5adSArtur Petrosyan 	udelay(5);
58129ce9e5adSArtur Petrosyan 
58139ce9e5adSArtur Petrosyan 	if (!rem_wakeup) {
58149ce9e5adSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
58159ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58169ce9e5adSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
58179ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58189ce9e5adSArtur Petrosyan 
58199ce9e5adSArtur Petrosyan 		hsotg->bus_suspended = false;
58209ce9e5adSArtur Petrosyan 	} else {
58219ce9e5adSArtur Petrosyan 		/* Turn on the port power bit. */
58229ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58239ce9e5adSArtur Petrosyan 		hprt0 |= HPRT0_PWR;
58249ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58259ce9e5adSArtur Petrosyan 
58269ce9e5adSArtur Petrosyan 		/* Connect hcd. */
58279ce9e5adSArtur Petrosyan 		dwc2_hcd_connect(hsotg);
58289ce9e5adSArtur Petrosyan 
58299ce9e5adSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
58309ce9e5adSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
58319ce9e5adSArtur Petrosyan 	}
58329ce9e5adSArtur Petrosyan 
58339ce9e5adSArtur Petrosyan 	/* Set lx_state to and in_ppd to 0 as here core exits from suspend. */
58349ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 0;
58359ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L0;
58369ce9e5adSArtur Petrosyan 
58379ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n");
58389ce9e5adSArtur Petrosyan 	return ret;
58399ce9e5adSArtur Petrosyan }
584079c87c3cSArtur Petrosyan 
584179c87c3cSArtur Petrosyan /**
584279c87c3cSArtur Petrosyan  * dwc2_host_enter_clock_gating() - Put controller in clock gating.
584379c87c3cSArtur Petrosyan  *
584479c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
584579c87c3cSArtur Petrosyan  *
584679c87c3cSArtur Petrosyan  * This function is for entering Host mode clock gating.
584779c87c3cSArtur Petrosyan  */
584879c87c3cSArtur Petrosyan void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
584979c87c3cSArtur Petrosyan {
585079c87c3cSArtur Petrosyan 	u32 hprt0;
585179c87c3cSArtur Petrosyan 	u32 pcgctl;
585279c87c3cSArtur Petrosyan 
585379c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host clock gating.\n");
585479c87c3cSArtur Petrosyan 
585579c87c3cSArtur Petrosyan 	/* Put this port in suspend mode. */
585679c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
585779c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
585879c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
585979c87c3cSArtur Petrosyan 
586079c87c3cSArtur Petrosyan 	/* Set the Phy Clock bit as suspend is received. */
586179c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
586279c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_STOPPCLK;
586379c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
586479c87c3cSArtur Petrosyan 	udelay(5);
586579c87c3cSArtur Petrosyan 
586679c87c3cSArtur Petrosyan 	/* Set the Gate hclk as suspend is received. */
586779c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
586879c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_GATEHCLK;
586979c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
587079c87c3cSArtur Petrosyan 	udelay(5);
587179c87c3cSArtur Petrosyan 
587279c87c3cSArtur Petrosyan 	hsotg->bus_suspended = true;
587379c87c3cSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
587479c87c3cSArtur Petrosyan }
587579c87c3cSArtur Petrosyan 
587679c87c3cSArtur Petrosyan /**
587779c87c3cSArtur Petrosyan  * dwc2_host_exit_clock_gating() - Exit controller from clock gating.
587879c87c3cSArtur Petrosyan  *
587979c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
588079c87c3cSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by remote wakeup
588179c87c3cSArtur Petrosyan  *
588279c87c3cSArtur Petrosyan  * This function is for exiting Host mode clock gating.
588379c87c3cSArtur Petrosyan  */
588479c87c3cSArtur Petrosyan void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
588579c87c3cSArtur Petrosyan {
588679c87c3cSArtur Petrosyan 	u32 hprt0;
588779c87c3cSArtur Petrosyan 	u32 pcgctl;
588879c87c3cSArtur Petrosyan 
588979c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host clock gating.\n");
589079c87c3cSArtur Petrosyan 
589179c87c3cSArtur Petrosyan 	/* Clear the Gate hclk. */
589279c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
589379c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_GATEHCLK;
589479c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
589579c87c3cSArtur Petrosyan 	udelay(5);
589679c87c3cSArtur Petrosyan 
589779c87c3cSArtur Petrosyan 	/* Phy Clock bit. */
589879c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
589979c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_STOPPCLK;
590079c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
590179c87c3cSArtur Petrosyan 	udelay(5);
590279c87c3cSArtur Petrosyan 
590379c87c3cSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
590479c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
590579c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_RES;
590679c87c3cSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
590779c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
590879c87c3cSArtur Petrosyan 	udelay(5);
590979c87c3cSArtur Petrosyan 
591079c87c3cSArtur Petrosyan 	if (!rem_wakeup) {
591179c87c3cSArtur Petrosyan 		/* In case of port resume need to wait for 40 ms */
591279c87c3cSArtur Petrosyan 		msleep(USB_RESUME_TIMEOUT);
591379c87c3cSArtur Petrosyan 
591479c87c3cSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
591579c87c3cSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
591679c87c3cSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
591779c87c3cSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
591879c87c3cSArtur Petrosyan 
591979c87c3cSArtur Petrosyan 		hsotg->bus_suspended = false;
592079c87c3cSArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
592179c87c3cSArtur Petrosyan 	} else {
592279c87c3cSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
592379c87c3cSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
592479c87c3cSArtur Petrosyan 	}
592579c87c3cSArtur Petrosyan }
5926