xref: /linux/drivers/usb/dwc2/hcd.c (revision 9ce9e5ad17d6c7cf943c9d365e8e12d6759e01db)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2197ba5f4SPaul Zimmerman /*
3197ba5f4SPaul Zimmerman  * hcd.c - DesignWare HS OTG Controller host-mode routines
4197ba5f4SPaul Zimmerman  *
5197ba5f4SPaul Zimmerman  * Copyright (C) 2004-2013 Synopsys, Inc.
6197ba5f4SPaul Zimmerman  *
7197ba5f4SPaul Zimmerman  * Redistribution and use in source and binary forms, with or without
8197ba5f4SPaul Zimmerman  * modification, are permitted provided that the following conditions
9197ba5f4SPaul Zimmerman  * are met:
10197ba5f4SPaul Zimmerman  * 1. Redistributions of source code must retain the above copyright
11197ba5f4SPaul Zimmerman  *    notice, this list of conditions, and the following disclaimer,
12197ba5f4SPaul Zimmerman  *    without modification.
13197ba5f4SPaul Zimmerman  * 2. Redistributions in binary form must reproduce the above copyright
14197ba5f4SPaul Zimmerman  *    notice, this list of conditions and the following disclaimer in the
15197ba5f4SPaul Zimmerman  *    documentation and/or other materials provided with the distribution.
16197ba5f4SPaul Zimmerman  * 3. The names of the above-listed copyright holders may not be used
17197ba5f4SPaul Zimmerman  *    to endorse or promote products derived from this software without
18197ba5f4SPaul Zimmerman  *    specific prior written permission.
19197ba5f4SPaul Zimmerman  *
20197ba5f4SPaul Zimmerman  * ALTERNATIVELY, this software may be distributed under the terms of the
21197ba5f4SPaul Zimmerman  * GNU General Public License ("GPL") as published by the Free Software
22197ba5f4SPaul Zimmerman  * Foundation; either version 2 of the License, or (at your option) any
23197ba5f4SPaul Zimmerman  * later version.
24197ba5f4SPaul Zimmerman  *
25197ba5f4SPaul Zimmerman  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26197ba5f4SPaul Zimmerman  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27197ba5f4SPaul Zimmerman  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28197ba5f4SPaul Zimmerman  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29197ba5f4SPaul Zimmerman  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30197ba5f4SPaul Zimmerman  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31197ba5f4SPaul Zimmerman  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32197ba5f4SPaul Zimmerman  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33197ba5f4SPaul Zimmerman  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34197ba5f4SPaul Zimmerman  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35197ba5f4SPaul Zimmerman  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36197ba5f4SPaul Zimmerman  */
37197ba5f4SPaul Zimmerman 
38197ba5f4SPaul Zimmerman /*
39197ba5f4SPaul Zimmerman  * This file contains the core HCD code, and implements the Linux hc_driver
40197ba5f4SPaul Zimmerman  * API
41197ba5f4SPaul Zimmerman  */
42197ba5f4SPaul Zimmerman #include <linux/kernel.h>
43197ba5f4SPaul Zimmerman #include <linux/module.h>
44197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
45197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
46348becdcSHeiner Kallweit #include <linux/platform_device.h>
47197ba5f4SPaul Zimmerman #include <linux/dma-mapping.h>
48197ba5f4SPaul Zimmerman #include <linux/delay.h>
49197ba5f4SPaul Zimmerman #include <linux/io.h>
50197ba5f4SPaul Zimmerman #include <linux/slab.h>
51197ba5f4SPaul Zimmerman #include <linux/usb.h>
52197ba5f4SPaul Zimmerman 
53197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
54197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
55197ba5f4SPaul Zimmerman 
56197ba5f4SPaul Zimmerman #include "core.h"
57197ba5f4SPaul Zimmerman #include "hcd.h"
58197ba5f4SPaul Zimmerman 
599156a7efSChen Yu static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
609156a7efSChen Yu 
61b02038faSJohn Youn /*
62b02038faSJohn Youn  * =========================================================================
63b02038faSJohn Youn  *  Host Core Layer Functions
64b02038faSJohn Youn  * =========================================================================
65b02038faSJohn Youn  */
66b02038faSJohn Youn 
67b02038faSJohn Youn /**
68b02038faSJohn Youn  * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
69b02038faSJohn Youn  * used in both device and host modes
70b02038faSJohn Youn  *
71b02038faSJohn Youn  * @hsotg: Programming view of the DWC_otg controller
72b02038faSJohn Youn  */
73b02038faSJohn Youn static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
74b02038faSJohn Youn {
75b02038faSJohn Youn 	u32 intmsk;
76b02038faSJohn Youn 
77b02038faSJohn Youn 	/* Clear any pending OTG Interrupts */
78f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GOTGINT);
79b02038faSJohn Youn 
80b02038faSJohn Youn 	/* Clear any pending interrupts */
81f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
82b02038faSJohn Youn 
83b02038faSJohn Youn 	/* Enable the interrupts in the GINTMSK */
84b02038faSJohn Youn 	intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
85b02038faSJohn Youn 
8695832c00SJohn Youn 	if (!hsotg->params.host_dma)
87b02038faSJohn Youn 		intmsk |= GINTSTS_RXFLVL;
8895832c00SJohn Youn 	if (!hsotg->params.external_id_pin_ctl)
89b02038faSJohn Youn 		intmsk |= GINTSTS_CONIDSTSCHNG;
90b02038faSJohn Youn 
91b02038faSJohn Youn 	intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
92b02038faSJohn Youn 		  GINTSTS_SESSREQINT;
93b02038faSJohn Youn 
94376f0401SSevak Arakelyan 	if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
95376f0401SSevak Arakelyan 		intmsk |= GINTSTS_LPMTRANRCVD;
96376f0401SSevak Arakelyan 
97f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
98b02038faSJohn Youn }
99b02038faSJohn Youn 
100b02038faSJohn Youn static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
101b02038faSJohn Youn {
102f25c42b8SGevorg Sahakyan 	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
103b02038faSJohn Youn 
104b02038faSJohn Youn 	switch (hsotg->hw_params.arch) {
105b02038faSJohn Youn 	case GHWCFG2_EXT_DMA_ARCH:
106b02038faSJohn Youn 		dev_err(hsotg->dev, "External DMA Mode not supported\n");
107b02038faSJohn Youn 		return -EINVAL;
108b02038faSJohn Youn 
109b02038faSJohn Youn 	case GHWCFG2_INT_DMA_ARCH:
110b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Internal DMA Mode\n");
111bea8e86cSJohn Youn 		if (hsotg->params.ahbcfg != -1) {
112b02038faSJohn Youn 			ahbcfg &= GAHBCFG_CTRL_MASK;
113bea8e86cSJohn Youn 			ahbcfg |= hsotg->params.ahbcfg &
114b02038faSJohn Youn 				  ~GAHBCFG_CTRL_MASK;
115b02038faSJohn Youn 		}
116b02038faSJohn Youn 		break;
117b02038faSJohn Youn 
118b02038faSJohn Youn 	case GHWCFG2_SLAVE_ONLY_ARCH:
119b02038faSJohn Youn 	default:
120b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Slave Only Mode\n");
121b02038faSJohn Youn 		break;
122b02038faSJohn Youn 	}
123b02038faSJohn Youn 
12495832c00SJohn Youn 	if (hsotg->params.host_dma)
125b02038faSJohn Youn 		ahbcfg |= GAHBCFG_DMA_EN;
1269d729a7aSRazmik Karapetyan 	else
1279d729a7aSRazmik Karapetyan 		hsotg->params.dma_desc_enable = false;
128b02038faSJohn Youn 
129f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
130b02038faSJohn Youn 
131b02038faSJohn Youn 	return 0;
132b02038faSJohn Youn }
133b02038faSJohn Youn 
134b02038faSJohn Youn static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
135b02038faSJohn Youn {
136b02038faSJohn Youn 	u32 usbcfg;
137b02038faSJohn Youn 
138f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
139b02038faSJohn Youn 	usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
140b02038faSJohn Youn 
141b02038faSJohn Youn 	switch (hsotg->hw_params.op_mode) {
142b02038faSJohn Youn 	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
143bea8e86cSJohn Youn 		if (hsotg->params.otg_cap ==
144b02038faSJohn Youn 				DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
145b02038faSJohn Youn 			usbcfg |= GUSBCFG_HNPCAP;
146bea8e86cSJohn Youn 		if (hsotg->params.otg_cap !=
147b02038faSJohn Youn 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
148b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
149b02038faSJohn Youn 		break;
150b02038faSJohn Youn 
151b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
152b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
153b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
154bea8e86cSJohn Youn 		if (hsotg->params.otg_cap !=
155b02038faSJohn Youn 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
156b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
157b02038faSJohn Youn 		break;
158b02038faSJohn Youn 
159b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
160b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
161b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
162b02038faSJohn Youn 	default:
163b02038faSJohn Youn 		break;
164b02038faSJohn Youn 	}
165b02038faSJohn Youn 
166f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
167b02038faSJohn Youn }
168b02038faSJohn Youn 
169531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
170531ef5ebSAmelie Delaunay {
171e0f681c2SFabrice Gasnier 	if (hsotg->vbus_supply)
172531ef5ebSAmelie Delaunay 		return regulator_enable(hsotg->vbus_supply);
173e0f681c2SFabrice Gasnier 
174e0f681c2SFabrice Gasnier 	return 0;
175531ef5ebSAmelie Delaunay }
176531ef5ebSAmelie Delaunay 
177531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
178531ef5ebSAmelie Delaunay {
179531ef5ebSAmelie Delaunay 	if (hsotg->vbus_supply)
180531ef5ebSAmelie Delaunay 		return regulator_disable(hsotg->vbus_supply);
181531ef5ebSAmelie Delaunay 
182531ef5ebSAmelie Delaunay 	return 0;
183531ef5ebSAmelie Delaunay }
184531ef5ebSAmelie Delaunay 
185b02038faSJohn Youn /**
186b02038faSJohn Youn  * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
187b02038faSJohn Youn  *
188b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
189b02038faSJohn Youn  */
190b02038faSJohn Youn static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
191b02038faSJohn Youn {
192b02038faSJohn Youn 	u32 intmsk;
193b02038faSJohn Youn 
194b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s()\n", __func__);
195b02038faSJohn Youn 
196b02038faSJohn Youn 	/* Disable all interrupts */
197f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
198f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HAINTMSK);
199b02038faSJohn Youn 
200b02038faSJohn Youn 	/* Enable the common interrupts */
201b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
202b02038faSJohn Youn 
203b02038faSJohn Youn 	/* Enable host mode interrupts without disturbing common interrupts */
204f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
205b02038faSJohn Youn 	intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
206f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
207b02038faSJohn Youn }
208b02038faSJohn Youn 
209b02038faSJohn Youn /**
210b02038faSJohn Youn  * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
211b02038faSJohn Youn  *
212b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
213b02038faSJohn Youn  */
214b02038faSJohn Youn static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
215b02038faSJohn Youn {
216f25c42b8SGevorg Sahakyan 	u32 intmsk = dwc2_readl(hsotg, GINTMSK);
217b02038faSJohn Youn 
218b02038faSJohn Youn 	/* Disable host mode interrupts without disturbing common interrupts */
219b02038faSJohn Youn 	intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
220b02038faSJohn Youn 		    GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
221f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
222b02038faSJohn Youn }
223b02038faSJohn Youn 
224b02038faSJohn Youn /*
225b02038faSJohn Youn  * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
226b02038faSJohn Youn  * For system that have a total fifo depth that is smaller than the default
227b02038faSJohn Youn  * RX + TX fifo size.
228b02038faSJohn Youn  *
229b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
230b02038faSJohn Youn  */
231b02038faSJohn Youn static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
232b02038faSJohn Youn {
233bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
234b02038faSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
235b02038faSJohn Youn 	u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
236b02038faSJohn Youn 
237b02038faSJohn Youn 	total_fifo_size = hw->total_fifo_size;
238b02038faSJohn Youn 	rxfsiz = params->host_rx_fifo_size;
239b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size;
240b02038faSJohn Youn 	ptxfsiz = params->host_perio_tx_fifo_size;
241b02038faSJohn Youn 
242b02038faSJohn Youn 	/*
243b02038faSJohn Youn 	 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
244b02038faSJohn Youn 	 * allocation with support for high bandwidth endpoints. Synopsys
245b02038faSJohn Youn 	 * defines MPS(Max Packet size) for a periodic EP=1024, and for
246b02038faSJohn Youn 	 * non-periodic as 512.
247b02038faSJohn Youn 	 */
248b02038faSJohn Youn 	if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
249b02038faSJohn Youn 		/*
250b02038faSJohn Youn 		 * For Buffer DMA mode/Scatter Gather DMA mode
251b02038faSJohn Youn 		 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
252b02038faSJohn Youn 		 * with n = number of host channel.
253b02038faSJohn Youn 		 * 2 * ((1024/4) + 2) = 516
254b02038faSJohn Youn 		 */
255b02038faSJohn Youn 		rxfsiz = 516 + hw->host_channels;
256b02038faSJohn Youn 
257b02038faSJohn Youn 		/*
258b02038faSJohn Youn 		 * min non-periodic tx fifo depth
259b02038faSJohn Youn 		 * 2 * (largest non-periodic USB packet used / 4)
260b02038faSJohn Youn 		 * 2 * (512/4) = 256
261b02038faSJohn Youn 		 */
262b02038faSJohn Youn 		nptxfsiz = 256;
263b02038faSJohn Youn 
264b02038faSJohn Youn 		/*
265b02038faSJohn Youn 		 * min periodic tx fifo depth
266b02038faSJohn Youn 		 * (largest packet size*MC)/4
267b02038faSJohn Youn 		 * (1024 * 3)/4 = 768
268b02038faSJohn Youn 		 */
269b02038faSJohn Youn 		ptxfsiz = 768;
270b02038faSJohn Youn 
271b02038faSJohn Youn 		params->host_rx_fifo_size = rxfsiz;
272b02038faSJohn Youn 		params->host_nperio_tx_fifo_size = nptxfsiz;
273b02038faSJohn Youn 		params->host_perio_tx_fifo_size = ptxfsiz;
274b02038faSJohn Youn 	}
275b02038faSJohn Youn 
276b02038faSJohn Youn 	/*
277b02038faSJohn Youn 	 * If the summation of RX, NPTX and PTX fifo sizes is still
278b02038faSJohn Youn 	 * bigger than the total_fifo_size, then we have a problem.
279b02038faSJohn Youn 	 *
280b02038faSJohn Youn 	 * We won't be able to allocate as many endpoints. Right now,
281b02038faSJohn Youn 	 * we're just printing an error message, but ideally this FIFO
282b02038faSJohn Youn 	 * allocation algorithm would be improved in the future.
283b02038faSJohn Youn 	 *
284b02038faSJohn Youn 	 * FIXME improve this FIFO allocation algorithm.
285b02038faSJohn Youn 	 */
286b02038faSJohn Youn 	if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
287b02038faSJohn Youn 		dev_err(hsotg->dev, "invalid fifo sizes\n");
288b02038faSJohn Youn }
289b02038faSJohn Youn 
290b02038faSJohn Youn static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
291b02038faSJohn Youn {
292bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
293b02038faSJohn Youn 	u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
294b02038faSJohn Youn 
295b02038faSJohn Youn 	if (!params->enable_dynamic_fifo)
296b02038faSJohn Youn 		return;
297b02038faSJohn Youn 
298b02038faSJohn Youn 	dwc2_calculate_dynamic_fifo(hsotg);
299b02038faSJohn Youn 
300b02038faSJohn Youn 	/* Rx FIFO */
301f25c42b8SGevorg Sahakyan 	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
302b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
303b02038faSJohn Youn 	grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
304b02038faSJohn Youn 	grxfsiz |= params->host_rx_fifo_size <<
305b02038faSJohn Youn 		   GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
306f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
307b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
308f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GRXFSIZ));
309b02038faSJohn Youn 
310b02038faSJohn Youn 	/* Non-periodic Tx FIFO */
311b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
312f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
313b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size <<
314b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
315b02038faSJohn Youn 	nptxfsiz |= params->host_rx_fifo_size <<
316b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
317f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
318b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
319f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
320b02038faSJohn Youn 
321b02038faSJohn Youn 	/* Periodic Tx FIFO */
322b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
323f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
324b02038faSJohn Youn 	hptxfsiz = params->host_perio_tx_fifo_size <<
325b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
326b02038faSJohn Youn 	hptxfsiz |= (params->host_rx_fifo_size +
327b02038faSJohn Youn 		     params->host_nperio_tx_fifo_size) <<
328b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
329f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
330b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
331f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
332b02038faSJohn Youn 
33395832c00SJohn Youn 	if (hsotg->params.en_multiple_tx_fifo &&
334e1f411d1SSevak Arakelyan 	    hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
335b02038faSJohn Youn 		/*
336e1f411d1SSevak Arakelyan 		 * This feature was implemented in 2.91a version
337b02038faSJohn Youn 		 * Global DFIFOCFG calculation for Host mode -
338b02038faSJohn Youn 		 * include RxFIFO, NPTXFIFO and HPTXFIFO
339b02038faSJohn Youn 		 */
340f25c42b8SGevorg Sahakyan 		dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
341b02038faSJohn Youn 		dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
342b02038faSJohn Youn 		dfifocfg |= (params->host_rx_fifo_size +
343b02038faSJohn Youn 			     params->host_nperio_tx_fifo_size +
344b02038faSJohn Youn 			     params->host_perio_tx_fifo_size) <<
345b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_SHIFT &
346b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_MASK;
347f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
348b02038faSJohn Youn 	}
349b02038faSJohn Youn }
350b02038faSJohn Youn 
351b02038faSJohn Youn /**
352b02038faSJohn Youn  * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
353b02038faSJohn Youn  * the HFIR register according to PHY type and speed
354b02038faSJohn Youn  *
355b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
356b02038faSJohn Youn  *
357b02038faSJohn Youn  * NOTE: The caller can modify the value of the HFIR register only after the
358b02038faSJohn Youn  * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
359b02038faSJohn Youn  * has been set
360b02038faSJohn Youn  */
361b02038faSJohn Youn u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
362b02038faSJohn Youn {
363b02038faSJohn Youn 	u32 usbcfg;
364b02038faSJohn Youn 	u32 hprt0;
365b02038faSJohn Youn 	int clock = 60;	/* default value */
366b02038faSJohn Youn 
367f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
368f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
369b02038faSJohn Youn 
370b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
371b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_PHYIF16))
372b02038faSJohn Youn 		clock = 60;
373b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
374b02038faSJohn Youn 	    GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
375b02038faSJohn Youn 		clock = 48;
376b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
377b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
378b02038faSJohn Youn 		clock = 30;
379b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
380b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
381b02038faSJohn Youn 		clock = 60;
382b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
383b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
384b02038faSJohn Youn 		clock = 48;
385b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
386b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
387b02038faSJohn Youn 		clock = 48;
388b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) &&
389b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
390b02038faSJohn Youn 		clock = 48;
391b02038faSJohn Youn 
392b02038faSJohn Youn 	if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
393b02038faSJohn Youn 		/* High speed case */
394b02038faSJohn Youn 		return 125 * clock - 1;
395b02038faSJohn Youn 
396b02038faSJohn Youn 	/* FS/LS case */
397b02038faSJohn Youn 	return 1000 * clock - 1;
398b02038faSJohn Youn }
399b02038faSJohn Youn 
400b02038faSJohn Youn /**
401b02038faSJohn Youn  * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
402b02038faSJohn Youn  * buffer
403b02038faSJohn Youn  *
4046fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of DWC_otg controller
405b02038faSJohn Youn  * @dest:    Destination buffer for the packet
406b02038faSJohn Youn  * @bytes:   Number of bytes to copy to the destination
407b02038faSJohn Youn  */
408b02038faSJohn Youn void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
409b02038faSJohn Youn {
410b02038faSJohn Youn 	u32 *data_buf = (u32 *)dest;
411b02038faSJohn Youn 	int word_count = (bytes + 3) / 4;
412b02038faSJohn Youn 	int i;
413b02038faSJohn Youn 
414b02038faSJohn Youn 	/*
415b02038faSJohn Youn 	 * Todo: Account for the case where dest is not dword aligned. This
416b02038faSJohn Youn 	 * requires reading data from the FIFO into a u32 temp buffer, then
417b02038faSJohn Youn 	 * moving it into the data buffer.
418b02038faSJohn Youn 	 */
419b02038faSJohn Youn 
420b02038faSJohn Youn 	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
421b02038faSJohn Youn 
422b02038faSJohn Youn 	for (i = 0; i < word_count; i++, data_buf++)
423f25c42b8SGevorg Sahakyan 		*data_buf = dwc2_readl(hsotg, HCFIFO(0));
424b02038faSJohn Youn }
425b02038faSJohn Youn 
426197ba5f4SPaul Zimmerman /**
427197ba5f4SPaul Zimmerman  * dwc2_dump_channel_info() - Prints the state of a host channel
428197ba5f4SPaul Zimmerman  *
429197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
430197ba5f4SPaul Zimmerman  * @chan:  Pointer to the channel to dump
431197ba5f4SPaul Zimmerman  *
432197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
433197ba5f4SPaul Zimmerman  *
434197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
435197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
436197ba5f4SPaul Zimmerman  */
437197ba5f4SPaul Zimmerman static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
438197ba5f4SPaul Zimmerman 				   struct dwc2_host_chan *chan)
439197ba5f4SPaul Zimmerman {
440197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
441bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
442197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
443197ba5f4SPaul Zimmerman 	u32 hcchar;
444197ba5f4SPaul Zimmerman 	u32 hcsplt;
445197ba5f4SPaul Zimmerman 	u32 hctsiz;
446197ba5f4SPaul Zimmerman 	u32 hc_dma;
447197ba5f4SPaul Zimmerman 	int i;
448197ba5f4SPaul Zimmerman 
449b02038faSJohn Youn 	if (!chan)
450197ba5f4SPaul Zimmerman 		return;
451197ba5f4SPaul Zimmerman 
452f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
453f25c42b8SGevorg Sahakyan 	hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
454f25c42b8SGevorg Sahakyan 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
455f25c42b8SGevorg Sahakyan 	hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
456197ba5f4SPaul Zimmerman 
457197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Assigned to channel %p:\n", chan);
458197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hcchar 0x%08x, hcsplt 0x%08x\n",
459197ba5f4SPaul Zimmerman 		hcchar, hcsplt);
460197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hctsiz 0x%08x, hc_dma 0x%08x\n",
461197ba5f4SPaul Zimmerman 		hctsiz, hc_dma);
462197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
463197ba5f4SPaul Zimmerman 		chan->dev_addr, chan->ep_num, chan->ep_is_in);
464197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
465197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
466197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    data_pid_start: %d\n", chan->data_pid_start);
467197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_started: %d\n", chan->xfer_started);
468197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
469197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
470197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
471197ba5f4SPaul Zimmerman 		(unsigned long)chan->xfer_dma);
472197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
473197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
474197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP inactive sched:\n");
475197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
476197ba5f4SPaul Zimmerman 			    qh_list_entry)
477197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
47838d2b5fbSDouglas Anderson 	dev_dbg(hsotg->dev, "  NP waiting sched:\n");
47938d2b5fbSDouglas Anderson 	list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
48038d2b5fbSDouglas Anderson 			    qh_list_entry)
48138d2b5fbSDouglas Anderson 		dev_dbg(hsotg->dev, "    %p\n", qh);
482197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP active sched:\n");
483197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
484197ba5f4SPaul Zimmerman 			    qh_list_entry)
485197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
486197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Channels:\n");
487197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
488197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
489197ba5f4SPaul Zimmerman 
490197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %2d: %p\n", i, chan);
491197ba5f4SPaul Zimmerman 	}
492197ba5f4SPaul Zimmerman #endif /* VERBOSE_DEBUG */
493197ba5f4SPaul Zimmerman }
494197ba5f4SPaul Zimmerman 
4954411bebaSRazmik Karapetyan static int _dwc2_hcd_start(struct usb_hcd *hcd);
4964411bebaSRazmik Karapetyan 
4974411bebaSRazmik Karapetyan static void dwc2_host_start(struct dwc2_hsotg *hsotg)
4984411bebaSRazmik Karapetyan {
4994411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
5004411bebaSRazmik Karapetyan 
5014411bebaSRazmik Karapetyan 	hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
5024411bebaSRazmik Karapetyan 	_dwc2_hcd_start(hcd);
5034411bebaSRazmik Karapetyan }
5044411bebaSRazmik Karapetyan 
5054411bebaSRazmik Karapetyan static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
5064411bebaSRazmik Karapetyan {
5074411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
5084411bebaSRazmik Karapetyan 
5094411bebaSRazmik Karapetyan 	hcd->self.is_b_host = 0;
5104411bebaSRazmik Karapetyan }
5114411bebaSRazmik Karapetyan 
5124411bebaSRazmik Karapetyan static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
5134411bebaSRazmik Karapetyan 			       int *hub_addr, int *hub_port)
5144411bebaSRazmik Karapetyan {
5154411bebaSRazmik Karapetyan 	struct urb *urb = context;
5164411bebaSRazmik Karapetyan 
5174411bebaSRazmik Karapetyan 	if (urb->dev->tt)
5184411bebaSRazmik Karapetyan 		*hub_addr = urb->dev->tt->hub->devnum;
5194411bebaSRazmik Karapetyan 	else
5204411bebaSRazmik Karapetyan 		*hub_addr = 0;
5214411bebaSRazmik Karapetyan 	*hub_port = urb->dev->ttport;
5224411bebaSRazmik Karapetyan }
5234411bebaSRazmik Karapetyan 
524197ba5f4SPaul Zimmerman /*
525b02038faSJohn Youn  * =========================================================================
526b02038faSJohn Youn  *  Low Level Host Channel Access Functions
527b02038faSJohn Youn  * =========================================================================
528b02038faSJohn Youn  */
529b02038faSJohn Youn 
530b02038faSJohn Youn static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
531b02038faSJohn Youn 				      struct dwc2_host_chan *chan)
532b02038faSJohn Youn {
533b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
534b02038faSJohn Youn 
535b02038faSJohn Youn 	switch (chan->ep_type) {
536b02038faSJohn Youn 	case USB_ENDPOINT_XFER_CONTROL:
537b02038faSJohn Youn 	case USB_ENDPOINT_XFER_BULK:
538b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "control/bulk\n");
539b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
540b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
541b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
542b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
543b02038faSJohn Youn 		if (chan->ep_is_in) {
544b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
545b02038faSJohn Youn 		} else {
546b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
547b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NYET;
548b02038faSJohn Youn 			if (chan->do_ping)
549b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
550b02038faSJohn Youn 		}
551b02038faSJohn Youn 
552b02038faSJohn Youn 		if (chan->do_split) {
553b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
554b02038faSJohn Youn 			if (chan->complete_split)
555b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
556b02038faSJohn Youn 			else
557b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
558b02038faSJohn Youn 		}
559b02038faSJohn Youn 
560b02038faSJohn Youn 		if (chan->error_state)
561b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
562b02038faSJohn Youn 		break;
563b02038faSJohn Youn 
564b02038faSJohn Youn 	case USB_ENDPOINT_XFER_INT:
565b02038faSJohn Youn 		if (dbg_perio())
566b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "intr\n");
567b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
568b02038faSJohn Youn 		hcintmsk |= HCINTMSK_NAK;
569b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
570b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
571b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
572b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
573b02038faSJohn Youn 
574b02038faSJohn Youn 		if (chan->ep_is_in)
575b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
576b02038faSJohn Youn 		if (chan->error_state)
577b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
578b02038faSJohn Youn 		if (chan->do_split) {
579b02038faSJohn Youn 			if (chan->complete_split)
580b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
581b02038faSJohn Youn 			else
582b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
583b02038faSJohn Youn 		}
584b02038faSJohn Youn 		break;
585b02038faSJohn Youn 
586b02038faSJohn Youn 	case USB_ENDPOINT_XFER_ISOC:
587b02038faSJohn Youn 		if (dbg_perio())
588b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "isoc\n");
589b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
590b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
591b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
592b02038faSJohn Youn 
593b02038faSJohn Youn 		if (chan->ep_is_in) {
594b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XACTERR;
595b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
596b02038faSJohn Youn 		}
597b02038faSJohn Youn 		break;
598b02038faSJohn Youn 	default:
599b02038faSJohn Youn 		dev_err(hsotg->dev, "## Unknown EP type ##\n");
600b02038faSJohn Youn 		break;
601b02038faSJohn Youn 	}
602b02038faSJohn Youn 
603f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
604b02038faSJohn Youn 	if (dbg_hc(chan))
605b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
606b02038faSJohn Youn }
607b02038faSJohn Youn 
608b02038faSJohn Youn static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
609b02038faSJohn Youn 				    struct dwc2_host_chan *chan)
610b02038faSJohn Youn {
611b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
612b02038faSJohn Youn 
613b02038faSJohn Youn 	/*
614b02038faSJohn Youn 	 * For Descriptor DMA mode core halts the channel on AHB error.
615b02038faSJohn Youn 	 * Interrupt is not required.
616b02038faSJohn Youn 	 */
61795832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
618b02038faSJohn Youn 		if (dbg_hc(chan))
619b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
620b02038faSJohn Youn 		hcintmsk |= HCINTMSK_AHBERR;
621b02038faSJohn Youn 	} else {
622b02038faSJohn Youn 		if (dbg_hc(chan))
623b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA enabled\n");
624b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
625b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XFERCOMPL;
626b02038faSJohn Youn 	}
627b02038faSJohn Youn 
628b02038faSJohn Youn 	if (chan->error_state && !chan->do_split &&
629b02038faSJohn Youn 	    chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
630b02038faSJohn Youn 		if (dbg_hc(chan))
631b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "setting ACK\n");
632b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
633b02038faSJohn Youn 		if (chan->ep_is_in) {
634b02038faSJohn Youn 			hcintmsk |= HCINTMSK_DATATGLERR;
635b02038faSJohn Youn 			if (chan->ep_type != USB_ENDPOINT_XFER_INT)
636b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NAK;
637b02038faSJohn Youn 		}
638b02038faSJohn Youn 	}
639b02038faSJohn Youn 
640f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
641b02038faSJohn Youn 	if (dbg_hc(chan))
642b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
643b02038faSJohn Youn }
644b02038faSJohn Youn 
645b02038faSJohn Youn static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
646b02038faSJohn Youn 				struct dwc2_host_chan *chan)
647b02038faSJohn Youn {
648b02038faSJohn Youn 	u32 intmsk;
649b02038faSJohn Youn 
65095832c00SJohn Youn 	if (hsotg->params.host_dma) {
651b02038faSJohn Youn 		if (dbg_hc(chan))
652b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
653b02038faSJohn Youn 		dwc2_hc_enable_dma_ints(hsotg, chan);
654b02038faSJohn Youn 	} else {
655b02038faSJohn Youn 		if (dbg_hc(chan))
656b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA disabled\n");
657b02038faSJohn Youn 		dwc2_hc_enable_slave_ints(hsotg, chan);
658b02038faSJohn Youn 	}
659b02038faSJohn Youn 
660b02038faSJohn Youn 	/* Enable the top level host channel interrupt */
661f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, HAINTMSK);
662b02038faSJohn Youn 	intmsk |= 1 << chan->hc_num;
663f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, HAINTMSK);
664b02038faSJohn Youn 	if (dbg_hc(chan))
665b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
666b02038faSJohn Youn 
667b02038faSJohn Youn 	/* Make sure host channel interrupts are enabled */
668f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
669b02038faSJohn Youn 	intmsk |= GINTSTS_HCHINT;
670f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
671b02038faSJohn Youn 	if (dbg_hc(chan))
672b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
673b02038faSJohn Youn }
674b02038faSJohn Youn 
675b02038faSJohn Youn /**
676b02038faSJohn Youn  * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
677b02038faSJohn Youn  * a specific endpoint
678b02038faSJohn Youn  *
679b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
680b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
681b02038faSJohn Youn  *
682b02038faSJohn Youn  * The HCCHARn register is set up with the characteristics specified in chan.
683b02038faSJohn Youn  * Host channel interrupts that may need to be serviced while this transfer is
684b02038faSJohn Youn  * in progress are enabled.
685b02038faSJohn Youn  */
686b02038faSJohn Youn static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
687b02038faSJohn Youn {
688b02038faSJohn Youn 	u8 hc_num = chan->hc_num;
689b02038faSJohn Youn 	u32 hcintmsk;
690b02038faSJohn Youn 	u32 hcchar;
691b02038faSJohn Youn 	u32 hcsplt = 0;
692b02038faSJohn Youn 
693b02038faSJohn Youn 	if (dbg_hc(chan))
694b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
695b02038faSJohn Youn 
696b02038faSJohn Youn 	/* Clear old interrupt conditions for this host channel */
697b02038faSJohn Youn 	hcintmsk = 0xffffffff;
698b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
699f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
700b02038faSJohn Youn 
701b02038faSJohn Youn 	/* Enable channel interrupts required for this transfer */
702b02038faSJohn Youn 	dwc2_hc_enable_ints(hsotg, chan);
703b02038faSJohn Youn 
704b02038faSJohn Youn 	/*
705b02038faSJohn Youn 	 * Program the HCCHARn register with the endpoint characteristics for
706b02038faSJohn Youn 	 * the current transfer
707b02038faSJohn Youn 	 */
708b02038faSJohn Youn 	hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
709b02038faSJohn Youn 	hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
710b02038faSJohn Youn 	if (chan->ep_is_in)
711b02038faSJohn Youn 		hcchar |= HCCHAR_EPDIR;
712b02038faSJohn Youn 	if (chan->speed == USB_SPEED_LOW)
713b02038faSJohn Youn 		hcchar |= HCCHAR_LSPDDEV;
714b02038faSJohn Youn 	hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
715b02038faSJohn Youn 	hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
716f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
717b02038faSJohn Youn 	if (dbg_hc(chan)) {
718b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
719b02038faSJohn Youn 			 hc_num, hcchar);
720b02038faSJohn Youn 
721b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n",
722b02038faSJohn Youn 			 __func__, hc_num);
723b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Dev Addr: %d\n",
724b02038faSJohn Youn 			 chan->dev_addr);
725b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Num: %d\n",
726b02038faSJohn Youn 			 chan->ep_num);
727b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is In: %d\n",
728b02038faSJohn Youn 			 chan->ep_is_in);
729b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is Low Speed: %d\n",
730b02038faSJohn Youn 			 chan->speed == USB_SPEED_LOW);
731b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Type: %d\n",
732b02038faSJohn Youn 			 chan->ep_type);
733b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Max Pkt: %d\n",
734b02038faSJohn Youn 			 chan->max_packet);
735b02038faSJohn Youn 	}
736b02038faSJohn Youn 
737b02038faSJohn Youn 	/* Program the HCSPLT register for SPLITs */
738b02038faSJohn Youn 	if (chan->do_split) {
739b02038faSJohn Youn 		if (dbg_hc(chan))
740b02038faSJohn Youn 			dev_vdbg(hsotg->dev,
741b02038faSJohn Youn 				 "Programming HC %d with split --> %s\n",
742b02038faSJohn Youn 				 hc_num,
743b02038faSJohn Youn 				 chan->complete_split ? "CSPLIT" : "SSPLIT");
744b02038faSJohn Youn 		if (chan->complete_split)
745b02038faSJohn Youn 			hcsplt |= HCSPLT_COMPSPLT;
746b02038faSJohn Youn 		hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
747b02038faSJohn Youn 			  HCSPLT_XACTPOS_MASK;
748b02038faSJohn Youn 		hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
749b02038faSJohn Youn 			  HCSPLT_HUBADDR_MASK;
750b02038faSJohn Youn 		hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
751b02038faSJohn Youn 			  HCSPLT_PRTADDR_MASK;
752b02038faSJohn Youn 		if (dbg_hc(chan)) {
753b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  comp split %d\n",
754b02038faSJohn Youn 				 chan->complete_split);
755b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xact pos %d\n",
756b02038faSJohn Youn 				 chan->xact_pos);
757b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub addr %d\n",
758b02038faSJohn Youn 				 chan->hub_addr);
759b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub port %d\n",
760b02038faSJohn Youn 				 chan->hub_port);
761b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  is_in %d\n",
762b02038faSJohn Youn 				 chan->ep_is_in);
763b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  Max Pkt %d\n",
764b02038faSJohn Youn 				 chan->max_packet);
765b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xferlen %d\n",
766b02038faSJohn Youn 				 chan->xfer_len);
767b02038faSJohn Youn 		}
768b02038faSJohn Youn 	}
769b02038faSJohn Youn 
770f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
771b02038faSJohn Youn }
772b02038faSJohn Youn 
773b02038faSJohn Youn /**
774b02038faSJohn Youn  * dwc2_hc_halt() - Attempts to halt a host channel
775b02038faSJohn Youn  *
776b02038faSJohn Youn  * @hsotg:       Controller register interface
777b02038faSJohn Youn  * @chan:        Host channel to halt
778b02038faSJohn Youn  * @halt_status: Reason for halting the channel
779b02038faSJohn Youn  *
780b02038faSJohn Youn  * This function should only be called in Slave mode or to abort a transfer in
781b02038faSJohn Youn  * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
782b02038faSJohn Youn  * controller halts the channel when the transfer is complete or a condition
783b02038faSJohn Youn  * occurs that requires application intervention.
784b02038faSJohn Youn  *
785b02038faSJohn Youn  * In slave mode, checks for a free request queue entry, then sets the Channel
786b02038faSJohn Youn  * Enable and Channel Disable bits of the Host Channel Characteristics
787b02038faSJohn Youn  * register of the specified channel to intiate the halt. If there is no free
788b02038faSJohn Youn  * request queue entry, sets only the Channel Disable bit of the HCCHARn
789b02038faSJohn Youn  * register to flush requests for this channel. In the latter case, sets a
790b02038faSJohn Youn  * flag to indicate that the host channel needs to be halted when a request
791b02038faSJohn Youn  * queue slot is open.
792b02038faSJohn Youn  *
793b02038faSJohn Youn  * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
794b02038faSJohn Youn  * HCCHARn register. The controller ensures there is space in the request
795b02038faSJohn Youn  * queue before submitting the halt request.
796b02038faSJohn Youn  *
797b02038faSJohn Youn  * Some time may elapse before the core flushes any posted requests for this
798b02038faSJohn Youn  * host channel and halts. The Channel Halted interrupt handler completes the
799b02038faSJohn Youn  * deactivation of the host channel.
800b02038faSJohn Youn  */
801b02038faSJohn Youn void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
802b02038faSJohn Youn 		  enum dwc2_halt_status halt_status)
803b02038faSJohn Youn {
804b02038faSJohn Youn 	u32 nptxsts, hptxsts, hcchar;
805b02038faSJohn Youn 
806b02038faSJohn Youn 	if (dbg_hc(chan))
807b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
808a82c7abdSMinas Harutyunyan 
809a82c7abdSMinas Harutyunyan 	/*
810a82c7abdSMinas Harutyunyan 	 * In buffer DMA or external DMA mode channel can't be halted
811a82c7abdSMinas Harutyunyan 	 * for non-split periodic channels. At the end of the next
812a82c7abdSMinas Harutyunyan 	 * uframe/frame (in the worst case), the core generates a channel
813a82c7abdSMinas Harutyunyan 	 * halted and disables the channel automatically.
814a82c7abdSMinas Harutyunyan 	 */
815a82c7abdSMinas Harutyunyan 	if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
816a82c7abdSMinas Harutyunyan 	    hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
817a82c7abdSMinas Harutyunyan 		if (!chan->do_split &&
818a82c7abdSMinas Harutyunyan 		    (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
819a82c7abdSMinas Harutyunyan 		     chan->ep_type == USB_ENDPOINT_XFER_INT)) {
820a82c7abdSMinas Harutyunyan 			dev_err(hsotg->dev, "%s() Channel can't be halted\n",
821a82c7abdSMinas Harutyunyan 				__func__);
822a82c7abdSMinas Harutyunyan 			return;
823a82c7abdSMinas Harutyunyan 		}
824a82c7abdSMinas Harutyunyan 	}
825a82c7abdSMinas Harutyunyan 
826b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
827b02038faSJohn Youn 		dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
828b02038faSJohn Youn 
829b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
830b02038faSJohn Youn 	    halt_status == DWC2_HC_XFER_AHB_ERR) {
831b02038faSJohn Youn 		/*
832b02038faSJohn Youn 		 * Disable all channel interrupts except Ch Halted. The QTD
833b02038faSJohn Youn 		 * and QH state associated with this transfer has been cleared
834b02038faSJohn Youn 		 * (in the case of URB_DEQUEUE), so the channel needs to be
835b02038faSJohn Youn 		 * shut down carefully to prevent crashes.
836b02038faSJohn Youn 		 */
837b02038faSJohn Youn 		u32 hcintmsk = HCINTMSK_CHHLTD;
838b02038faSJohn Youn 
839b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "dequeue/error\n");
840f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
841b02038faSJohn Youn 
842b02038faSJohn Youn 		/*
843b02038faSJohn Youn 		 * Make sure no other interrupts besides halt are currently
844b02038faSJohn Youn 		 * pending. Handling another interrupt could cause a crash due
845b02038faSJohn Youn 		 * to the QTD and QH state.
846b02038faSJohn Youn 		 */
847f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
848b02038faSJohn Youn 
849b02038faSJohn Youn 		/*
850b02038faSJohn Youn 		 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
851b02038faSJohn Youn 		 * even if the channel was already halted for some other
852b02038faSJohn Youn 		 * reason
853b02038faSJohn Youn 		 */
854b02038faSJohn Youn 		chan->halt_status = halt_status;
855b02038faSJohn Youn 
856f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
857b02038faSJohn Youn 		if (!(hcchar & HCCHAR_CHENA)) {
858b02038faSJohn Youn 			/*
859b02038faSJohn Youn 			 * The channel is either already halted or it hasn't
860b02038faSJohn Youn 			 * started yet. In DMA mode, the transfer may halt if
861b02038faSJohn Youn 			 * it finishes normally or a condition occurs that
862b02038faSJohn Youn 			 * requires driver intervention. Don't want to halt
863b02038faSJohn Youn 			 * the channel again. In either Slave or DMA mode,
864b02038faSJohn Youn 			 * it's possible that the transfer has been assigned
865b02038faSJohn Youn 			 * to a channel, but not started yet when an URB is
866b02038faSJohn Youn 			 * dequeued. Don't want to halt a channel that hasn't
867b02038faSJohn Youn 			 * started yet.
868b02038faSJohn Youn 			 */
869b02038faSJohn Youn 			return;
870b02038faSJohn Youn 		}
871b02038faSJohn Youn 	}
872b02038faSJohn Youn 	if (chan->halt_pending) {
873b02038faSJohn Youn 		/*
874b02038faSJohn Youn 		 * A halt has already been issued for this channel. This might
875b02038faSJohn Youn 		 * happen when a transfer is aborted by a higher level in
876b02038faSJohn Youn 		 * the stack.
877b02038faSJohn Youn 		 */
878b02038faSJohn Youn 		dev_vdbg(hsotg->dev,
879b02038faSJohn Youn 			 "*** %s: Channel %d, chan->halt_pending already set ***\n",
880b02038faSJohn Youn 			 __func__, chan->hc_num);
881b02038faSJohn Youn 		return;
882b02038faSJohn Youn 	}
883b02038faSJohn Youn 
884f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
885b02038faSJohn Youn 
886b02038faSJohn Youn 	/* No need to set the bit in DDMA for disabling the channel */
887b02038faSJohn Youn 	/* TODO check it everywhere channel is disabled */
88895832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
889b02038faSJohn Youn 		if (dbg_hc(chan))
890b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
891b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
892b02038faSJohn Youn 	} else {
893b02038faSJohn Youn 		if (dbg_hc(chan))
894b02038faSJohn Youn 			dev_dbg(hsotg->dev, "desc DMA enabled\n");
895b02038faSJohn Youn 	}
896b02038faSJohn Youn 	hcchar |= HCCHAR_CHDIS;
897b02038faSJohn Youn 
89895832c00SJohn Youn 	if (!hsotg->params.host_dma) {
899b02038faSJohn Youn 		if (dbg_hc(chan))
900b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA not enabled\n");
901b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
902b02038faSJohn Youn 
903b02038faSJohn Youn 		/* Check for space in the request queue to issue the halt */
904b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
905b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
906b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "control/bulk\n");
907f25c42b8SGevorg Sahakyan 			nptxsts = dwc2_readl(hsotg, GNPTXSTS);
908b02038faSJohn Youn 			if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
909b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "Disabling channel\n");
910b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
911b02038faSJohn Youn 			}
912b02038faSJohn Youn 		} else {
913b02038faSJohn Youn 			if (dbg_perio())
914b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "isoc/intr\n");
915f25c42b8SGevorg Sahakyan 			hptxsts = dwc2_readl(hsotg, HPTXSTS);
916b02038faSJohn Youn 			if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
917b02038faSJohn Youn 			    hsotg->queuing_high_bandwidth) {
918b02038faSJohn Youn 				if (dbg_perio())
919b02038faSJohn Youn 					dev_vdbg(hsotg->dev, "Disabling channel\n");
920b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
921b02038faSJohn Youn 			}
922b02038faSJohn Youn 		}
923b02038faSJohn Youn 	} else {
924b02038faSJohn Youn 		if (dbg_hc(chan))
925b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
926b02038faSJohn Youn 	}
927b02038faSJohn Youn 
928f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
929b02038faSJohn Youn 	chan->halt_status = halt_status;
930b02038faSJohn Youn 
931b02038faSJohn Youn 	if (hcchar & HCCHAR_CHENA) {
932b02038faSJohn Youn 		if (dbg_hc(chan))
933b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel enabled\n");
934b02038faSJohn Youn 		chan->halt_pending = 1;
935b02038faSJohn Youn 		chan->halt_on_queue = 0;
936b02038faSJohn Youn 	} else {
937b02038faSJohn Youn 		if (dbg_hc(chan))
938b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel disabled\n");
939b02038faSJohn Youn 		chan->halt_on_queue = 1;
940b02038faSJohn Youn 	}
941b02038faSJohn Youn 
942b02038faSJohn Youn 	if (dbg_hc(chan)) {
943b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
944b02038faSJohn Youn 			 chan->hc_num);
945b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 hcchar: 0x%08x\n",
946b02038faSJohn Youn 			 hcchar);
947b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_pending: %d\n",
948b02038faSJohn Youn 			 chan->halt_pending);
949b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_on_queue: %d\n",
950b02038faSJohn Youn 			 chan->halt_on_queue);
951b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_status: %d\n",
952b02038faSJohn Youn 			 chan->halt_status);
953b02038faSJohn Youn 	}
954b02038faSJohn Youn }
955b02038faSJohn Youn 
956b02038faSJohn Youn /**
957b02038faSJohn Youn  * dwc2_hc_cleanup() - Clears the transfer state for a host channel
958b02038faSJohn Youn  *
959b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
960b02038faSJohn Youn  * @chan:  Identifies the host channel to clean up
961b02038faSJohn Youn  *
962b02038faSJohn Youn  * This function is normally called after a transfer is done and the host
963b02038faSJohn Youn  * channel is being released
964b02038faSJohn Youn  */
965b02038faSJohn Youn void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
966b02038faSJohn Youn {
967b02038faSJohn Youn 	u32 hcintmsk;
968b02038faSJohn Youn 
969b02038faSJohn Youn 	chan->xfer_started = 0;
970b02038faSJohn Youn 
971b02038faSJohn Youn 	list_del_init(&chan->split_order_list_entry);
972b02038faSJohn Youn 
973b02038faSJohn Youn 	/*
974b02038faSJohn Youn 	 * Clear channel interrupt enables and any unhandled channel interrupt
975b02038faSJohn Youn 	 * conditions
976b02038faSJohn Youn 	 */
977f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
978b02038faSJohn Youn 	hcintmsk = 0xffffffff;
979b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
980f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
981b02038faSJohn Youn }
982b02038faSJohn Youn 
983b02038faSJohn Youn /**
984b02038faSJohn Youn  * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
985b02038faSJohn Youn  * which frame a periodic transfer should occur
986b02038faSJohn Youn  *
987b02038faSJohn Youn  * @hsotg:  Programming view of DWC_otg controller
988b02038faSJohn Youn  * @chan:   Identifies the host channel to set up and its properties
989b02038faSJohn Youn  * @hcchar: Current value of the HCCHAR register for the specified host channel
990b02038faSJohn Youn  *
991b02038faSJohn Youn  * This function has no effect on non-periodic transfers
992b02038faSJohn Youn  */
993b02038faSJohn Youn static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
994b02038faSJohn Youn 				       struct dwc2_host_chan *chan, u32 *hcchar)
995b02038faSJohn Youn {
996b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
997b02038faSJohn Youn 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
998b02038faSJohn Youn 		int host_speed;
999b02038faSJohn Youn 		int xfer_ns;
1000b02038faSJohn Youn 		int xfer_us;
1001b02038faSJohn Youn 		int bytes_in_fifo;
1002b02038faSJohn Youn 		u16 fifo_space;
1003b02038faSJohn Youn 		u16 frame_number;
1004b02038faSJohn Youn 		u16 wire_frame;
1005b02038faSJohn Youn 
1006b02038faSJohn Youn 		/*
1007b02038faSJohn Youn 		 * Try to figure out if we're an even or odd frame. If we set
1008b02038faSJohn Youn 		 * even and the current frame number is even the the transfer
1009b02038faSJohn Youn 		 * will happen immediately.  Similar if both are odd. If one is
1010b02038faSJohn Youn 		 * even and the other is odd then the transfer will happen when
1011b02038faSJohn Youn 		 * the frame number ticks.
1012b02038faSJohn Youn 		 *
1013b02038faSJohn Youn 		 * There's a bit of a balancing act to get this right.
1014b02038faSJohn Youn 		 * Sometimes we may want to send data in the current frame (AK
1015b02038faSJohn Youn 		 * right away).  We might want to do this if the frame number
1016b02038faSJohn Youn 		 * _just_ ticked, but we might also want to do this in order
1017b02038faSJohn Youn 		 * to continue a split transaction that happened late in a
1018b02038faSJohn Youn 		 * microframe (so we didn't know to queue the next transfer
1019b02038faSJohn Youn 		 * until the frame number had ticked).  The problem is that we
1020b02038faSJohn Youn 		 * need a lot of knowledge to know if there's actually still
1021b02038faSJohn Youn 		 * time to send things or if it would be better to wait until
1022b02038faSJohn Youn 		 * the next frame.
1023b02038faSJohn Youn 		 *
1024b02038faSJohn Youn 		 * We can look at how much time is left in the current frame
1025b02038faSJohn Youn 		 * and make a guess about whether we'll have time to transfer.
1026b02038faSJohn Youn 		 * We'll do that.
1027b02038faSJohn Youn 		 */
1028b02038faSJohn Youn 
1029b02038faSJohn Youn 		/* Get speed host is running at */
1030b02038faSJohn Youn 		host_speed = (chan->speed != USB_SPEED_HIGH &&
1031b02038faSJohn Youn 			      !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1032b02038faSJohn Youn 
1033b02038faSJohn Youn 		/* See how many bytes are in the periodic FIFO right now */
1034f25c42b8SGevorg Sahakyan 		fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
1035b02038faSJohn Youn 			      TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1036b02038faSJohn Youn 		bytes_in_fifo = sizeof(u32) *
1037bea8e86cSJohn Youn 				(hsotg->params.host_perio_tx_fifo_size -
1038b02038faSJohn Youn 				 fifo_space);
1039b02038faSJohn Youn 
1040b02038faSJohn Youn 		/*
1041b02038faSJohn Youn 		 * Roughly estimate bus time for everything in the periodic
1042b02038faSJohn Youn 		 * queue + our new transfer.  This is "rough" because we're
1043b02038faSJohn Youn 		 * using a function that makes takes into account IN/OUT
1044b02038faSJohn Youn 		 * and INT/ISO and we're just slamming in one value for all
1045b02038faSJohn Youn 		 * transfers.  This should be an over-estimate and that should
1046b02038faSJohn Youn 		 * be OK, but we can probably tighten it.
1047b02038faSJohn Youn 		 */
1048b02038faSJohn Youn 		xfer_ns = usb_calc_bus_time(host_speed, false, false,
1049b02038faSJohn Youn 					    chan->xfer_len + bytes_in_fifo);
1050b02038faSJohn Youn 		xfer_us = NS_TO_US(xfer_ns);
1051b02038faSJohn Youn 
1052b02038faSJohn Youn 		/* See what frame number we'll be at by the time we finish */
1053b02038faSJohn Youn 		frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1054b02038faSJohn Youn 
1055b02038faSJohn Youn 		/* This is when we were scheduled to be on the wire */
1056b02038faSJohn Youn 		wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1057b02038faSJohn Youn 
1058b02038faSJohn Youn 		/*
1059b02038faSJohn Youn 		 * If we'd finish _after_ the frame we're scheduled in then
1060b02038faSJohn Youn 		 * it's hopeless.  Just schedule right away and hope for the
1061b02038faSJohn Youn 		 * best.  Note that it _might_ be wise to call back into the
1062b02038faSJohn Youn 		 * scheduler to pick a better frame, but this is better than
1063b02038faSJohn Youn 		 * nothing.
1064b02038faSJohn Youn 		 */
1065b02038faSJohn Youn 		if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1066b02038faSJohn Youn 			dwc2_sch_vdbg(hsotg,
1067b02038faSJohn Youn 				      "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1068b02038faSJohn Youn 				      chan->qh, wire_frame, frame_number,
1069b02038faSJohn Youn 				      dwc2_frame_num_dec(frame_number,
1070b02038faSJohn Youn 							 wire_frame));
1071b02038faSJohn Youn 			wire_frame = frame_number;
1072b02038faSJohn Youn 
1073b02038faSJohn Youn 			/*
1074b02038faSJohn Youn 			 * We picked a different frame number; communicate this
1075b02038faSJohn Youn 			 * back to the scheduler so it doesn't try to schedule
1076b02038faSJohn Youn 			 * another in the same frame.
1077b02038faSJohn Youn 			 *
1078b02038faSJohn Youn 			 * Remember that next_active_frame is 1 before the wire
1079b02038faSJohn Youn 			 * frame.
1080b02038faSJohn Youn 			 */
1081b02038faSJohn Youn 			chan->qh->next_active_frame =
1082b02038faSJohn Youn 				dwc2_frame_num_dec(frame_number, 1);
1083b02038faSJohn Youn 		}
1084b02038faSJohn Youn 
1085b02038faSJohn Youn 		if (wire_frame & 1)
1086b02038faSJohn Youn 			*hcchar |= HCCHAR_ODDFRM;
1087b02038faSJohn Youn 		else
1088b02038faSJohn Youn 			*hcchar &= ~HCCHAR_ODDFRM;
1089b02038faSJohn Youn 	}
1090b02038faSJohn Youn }
1091b02038faSJohn Youn 
1092b02038faSJohn Youn static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1093b02038faSJohn Youn {
1094b02038faSJohn Youn 	/* Set up the initial PID for the transfer */
1095b02038faSJohn Youn 	if (chan->speed == USB_SPEED_HIGH) {
1096b02038faSJohn Youn 		if (chan->ep_is_in) {
1097b02038faSJohn Youn 			if (chan->multi_count == 1)
1098b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1099b02038faSJohn Youn 			else if (chan->multi_count == 2)
1100b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA1;
1101b02038faSJohn Youn 			else
1102b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA2;
1103b02038faSJohn Youn 		} else {
1104b02038faSJohn Youn 			if (chan->multi_count == 1)
1105b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1106b02038faSJohn Youn 			else
1107b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_MDATA;
1108b02038faSJohn Youn 		}
1109b02038faSJohn Youn 	} else {
1110b02038faSJohn Youn 		chan->data_pid_start = DWC2_HC_PID_DATA0;
1111b02038faSJohn Youn 	}
1112b02038faSJohn Youn }
1113b02038faSJohn Youn 
1114b02038faSJohn Youn /**
1115b02038faSJohn Youn  * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1116b02038faSJohn Youn  * the Host Channel
1117b02038faSJohn Youn  *
1118b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1119b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1120b02038faSJohn Youn  *
1121b02038faSJohn Youn  * This function should only be called in Slave mode. For a channel associated
1122b02038faSJohn Youn  * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1123b02038faSJohn Youn  * associated with a periodic EP, the periodic Tx FIFO is written.
1124b02038faSJohn Youn  *
1125b02038faSJohn Youn  * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1126b02038faSJohn Youn  * the number of bytes written to the Tx FIFO.
1127b02038faSJohn Youn  */
1128b02038faSJohn Youn static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1129b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1130b02038faSJohn Youn {
1131b02038faSJohn Youn 	u32 i;
1132b02038faSJohn Youn 	u32 remaining_count;
1133b02038faSJohn Youn 	u32 byte_count;
1134b02038faSJohn Youn 	u32 dword_count;
1135b02038faSJohn Youn 	u32 *data_buf = (u32 *)chan->xfer_buf;
1136b02038faSJohn Youn 
1137b02038faSJohn Youn 	if (dbg_hc(chan))
1138b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1139b02038faSJohn Youn 
1140b02038faSJohn Youn 	remaining_count = chan->xfer_len - chan->xfer_count;
1141b02038faSJohn Youn 	if (remaining_count > chan->max_packet)
1142b02038faSJohn Youn 		byte_count = chan->max_packet;
1143b02038faSJohn Youn 	else
1144b02038faSJohn Youn 		byte_count = remaining_count;
1145b02038faSJohn Youn 
1146b02038faSJohn Youn 	dword_count = (byte_count + 3) / 4;
1147b02038faSJohn Youn 
1148b02038faSJohn Youn 	if (((unsigned long)data_buf & 0x3) == 0) {
1149b02038faSJohn Youn 		/* xfer_buf is DWORD aligned */
1150b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++)
1151f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
1152b02038faSJohn Youn 	} else {
1153b02038faSJohn Youn 		/* xfer_buf is not DWORD aligned */
1154b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++) {
1155b02038faSJohn Youn 			u32 data = data_buf[0] | data_buf[1] << 8 |
1156b02038faSJohn Youn 				   data_buf[2] << 16 | data_buf[3] << 24;
1157f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
1158b02038faSJohn Youn 		}
1159b02038faSJohn Youn 	}
1160b02038faSJohn Youn 
1161b02038faSJohn Youn 	chan->xfer_count += byte_count;
1162b02038faSJohn Youn 	chan->xfer_buf += byte_count;
1163b02038faSJohn Youn }
1164b02038faSJohn Youn 
1165b02038faSJohn Youn /**
1166b02038faSJohn Youn  * dwc2_hc_do_ping() - Starts a PING transfer
1167b02038faSJohn Youn  *
1168b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1169b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1170b02038faSJohn Youn  *
1171b02038faSJohn Youn  * This function should only be called in Slave mode. The Do Ping bit is set in
1172b02038faSJohn Youn  * the HCTSIZ register, then the channel is enabled.
1173b02038faSJohn Youn  */
1174b02038faSJohn Youn static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1175b02038faSJohn Youn 			    struct dwc2_host_chan *chan)
1176b02038faSJohn Youn {
1177b02038faSJohn Youn 	u32 hcchar;
1178b02038faSJohn Youn 	u32 hctsiz;
1179b02038faSJohn Youn 
1180b02038faSJohn Youn 	if (dbg_hc(chan))
1181b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1182b02038faSJohn Youn 			 chan->hc_num);
1183b02038faSJohn Youn 
1184b02038faSJohn Youn 	hctsiz = TSIZ_DOPNG;
1185b02038faSJohn Youn 	hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1186f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1187b02038faSJohn Youn 
1188f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1189b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1190b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1191f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1192b02038faSJohn Youn }
1193b02038faSJohn Youn 
1194b02038faSJohn Youn /**
1195b02038faSJohn Youn  * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1196b02038faSJohn Youn  * channel and starts the transfer
1197b02038faSJohn Youn  *
1198b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1199b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel. The xfer_len value
1200b02038faSJohn Youn  *         may be reduced to accommodate the max widths of the XferSize and
1201b02038faSJohn Youn  *         PktCnt fields in the HCTSIZn register. The multi_count value may be
1202b02038faSJohn Youn  *         changed to reflect the final xfer_len value.
1203b02038faSJohn Youn  *
1204b02038faSJohn Youn  * This function may be called in either Slave mode or DMA mode. In Slave mode,
1205b02038faSJohn Youn  * the caller must ensure that there is sufficient space in the request queue
1206b02038faSJohn Youn  * and Tx Data FIFO.
1207b02038faSJohn Youn  *
1208b02038faSJohn Youn  * For an OUT transfer in Slave mode, it loads a data packet into the
1209b02038faSJohn Youn  * appropriate FIFO. If necessary, additional data packets are loaded in the
1210b02038faSJohn Youn  * Host ISR.
1211b02038faSJohn Youn  *
1212b02038faSJohn Youn  * For an IN transfer in Slave mode, a data packet is requested. The data
1213b02038faSJohn Youn  * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1214b02038faSJohn Youn  * additional data packets are requested in the Host ISR.
1215b02038faSJohn Youn  *
1216b02038faSJohn Youn  * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1217b02038faSJohn Youn  * register along with a packet count of 1 and the channel is enabled. This
1218b02038faSJohn Youn  * causes a single PING transaction to occur. Other fields in HCTSIZ are
1219b02038faSJohn Youn  * simply set to 0 since no data transfer occurs in this case.
1220b02038faSJohn Youn  *
1221b02038faSJohn Youn  * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1222b02038faSJohn Youn  * all the information required to perform the subsequent data transfer. In
1223b02038faSJohn Youn  * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1224b02038faSJohn Youn  * controller performs the entire PING protocol, then starts the data
1225b02038faSJohn Youn  * transfer.
1226b02038faSJohn Youn  */
1227b02038faSJohn Youn static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1228b02038faSJohn Youn 				   struct dwc2_host_chan *chan)
1229b02038faSJohn Youn {
1230bea8e86cSJohn Youn 	u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1231bea8e86cSJohn Youn 	u16 max_hc_pkt_count = hsotg->params.max_packet_count;
1232b02038faSJohn Youn 	u32 hcchar;
1233b02038faSJohn Youn 	u32 hctsiz = 0;
1234b02038faSJohn Youn 	u16 num_packets;
1235b02038faSJohn Youn 	u32 ec_mc;
1236b02038faSJohn Youn 
1237b02038faSJohn Youn 	if (dbg_hc(chan))
1238b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1239b02038faSJohn Youn 
1240b02038faSJohn Youn 	if (chan->do_ping) {
124195832c00SJohn Youn 		if (!hsotg->params.host_dma) {
1242b02038faSJohn Youn 			if (dbg_hc(chan))
1243b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "ping, no DMA\n");
1244b02038faSJohn Youn 			dwc2_hc_do_ping(hsotg, chan);
1245b02038faSJohn Youn 			chan->xfer_started = 1;
1246b02038faSJohn Youn 			return;
1247b02038faSJohn Youn 		}
1248b02038faSJohn Youn 
1249b02038faSJohn Youn 		if (dbg_hc(chan))
1250b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "ping, DMA\n");
1251b02038faSJohn Youn 
1252b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1253b02038faSJohn Youn 	}
1254b02038faSJohn Youn 
1255b02038faSJohn Youn 	if (chan->do_split) {
1256b02038faSJohn Youn 		if (dbg_hc(chan))
1257b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "split\n");
1258b02038faSJohn Youn 		num_packets = 1;
1259b02038faSJohn Youn 
1260b02038faSJohn Youn 		if (chan->complete_split && !chan->ep_is_in)
1261b02038faSJohn Youn 			/*
1262b02038faSJohn Youn 			 * For CSPLIT OUT Transfer, set the size to 0 so the
1263b02038faSJohn Youn 			 * core doesn't expect any data written to the FIFO
1264b02038faSJohn Youn 			 */
1265b02038faSJohn Youn 			chan->xfer_len = 0;
1266b02038faSJohn Youn 		else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1267b02038faSJohn Youn 			chan->xfer_len = chan->max_packet;
1268b02038faSJohn Youn 		else if (!chan->ep_is_in && chan->xfer_len > 188)
1269b02038faSJohn Youn 			chan->xfer_len = 188;
1270b02038faSJohn Youn 
1271b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1272b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1273b02038faSJohn Youn 
1274b02038faSJohn Youn 		/* For split set ec_mc for immediate retries */
1275b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1276b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1277b02038faSJohn Youn 			ec_mc = 3;
1278b02038faSJohn Youn 		else
1279b02038faSJohn Youn 			ec_mc = 1;
1280b02038faSJohn Youn 	} else {
1281b02038faSJohn Youn 		if (dbg_hc(chan))
1282b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "no split\n");
1283b02038faSJohn Youn 		/*
1284b02038faSJohn Youn 		 * Ensure that the transfer length and packet count will fit
1285b02038faSJohn Youn 		 * in the widths allocated for them in the HCTSIZn register
1286b02038faSJohn Youn 		 */
1287b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1288b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1289b02038faSJohn Youn 			/*
1290b02038faSJohn Youn 			 * Make sure the transfer size is no larger than one
1291b02038faSJohn Youn 			 * (micro)frame's worth of data. (A check was done
1292b02038faSJohn Youn 			 * when the periodic transfer was accepted to ensure
1293b02038faSJohn Youn 			 * that a (micro)frame's worth of data can be
1294b02038faSJohn Youn 			 * programmed into a channel.)
1295b02038faSJohn Youn 			 */
1296b02038faSJohn Youn 			u32 max_periodic_len =
1297b02038faSJohn Youn 				chan->multi_count * chan->max_packet;
1298b02038faSJohn Youn 
1299b02038faSJohn Youn 			if (chan->xfer_len > max_periodic_len)
1300b02038faSJohn Youn 				chan->xfer_len = max_periodic_len;
1301b02038faSJohn Youn 		} else if (chan->xfer_len > max_hc_xfer_size) {
1302b02038faSJohn Youn 			/*
1303b02038faSJohn Youn 			 * Make sure that xfer_len is a multiple of max packet
1304b02038faSJohn Youn 			 * size
1305b02038faSJohn Youn 			 */
1306b02038faSJohn Youn 			chan->xfer_len =
1307b02038faSJohn Youn 				max_hc_xfer_size - chan->max_packet + 1;
1308b02038faSJohn Youn 		}
1309b02038faSJohn Youn 
1310b02038faSJohn Youn 		if (chan->xfer_len > 0) {
1311b02038faSJohn Youn 			num_packets = (chan->xfer_len + chan->max_packet - 1) /
1312b02038faSJohn Youn 					chan->max_packet;
1313b02038faSJohn Youn 			if (num_packets > max_hc_pkt_count) {
1314b02038faSJohn Youn 				num_packets = max_hc_pkt_count;
1315b02038faSJohn Youn 				chan->xfer_len = num_packets * chan->max_packet;
1316415fa1c7SGuenter Roeck 			} else if (chan->ep_is_in) {
1317415fa1c7SGuenter Roeck 				/*
1318415fa1c7SGuenter Roeck 				 * Always program an integral # of max packets
1319415fa1c7SGuenter Roeck 				 * for IN transfers.
1320415fa1c7SGuenter Roeck 				 * Note: This assumes that the input buffer is
1321415fa1c7SGuenter Roeck 				 * aligned and sized accordingly.
1322415fa1c7SGuenter Roeck 				 */
1323415fa1c7SGuenter Roeck 				chan->xfer_len = num_packets * chan->max_packet;
1324b02038faSJohn Youn 			}
1325b02038faSJohn Youn 		} else {
1326b02038faSJohn Youn 			/* Need 1 packet for transfer length of 0 */
1327b02038faSJohn Youn 			num_packets = 1;
1328b02038faSJohn Youn 		}
1329b02038faSJohn Youn 
1330b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1331b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1332b02038faSJohn Youn 			/*
1333b02038faSJohn Youn 			 * Make sure that the multi_count field matches the
1334b02038faSJohn Youn 			 * actual transfer length
1335b02038faSJohn Youn 			 */
1336b02038faSJohn Youn 			chan->multi_count = num_packets;
1337b02038faSJohn Youn 
1338b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1339b02038faSJohn Youn 			dwc2_set_pid_isoc(chan);
1340b02038faSJohn Youn 
1341b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1342b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1343b02038faSJohn Youn 
1344b02038faSJohn Youn 		/* The ec_mc gets the multi_count for non-split */
1345b02038faSJohn Youn 		ec_mc = chan->multi_count;
1346b02038faSJohn Youn 	}
1347b02038faSJohn Youn 
1348b02038faSJohn Youn 	chan->start_pkt_count = num_packets;
1349b02038faSJohn Youn 	hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1350b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1351b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1352f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1353b02038faSJohn Youn 	if (dbg_hc(chan)) {
1354b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1355b02038faSJohn Youn 			 hctsiz, chan->hc_num);
1356b02038faSJohn Youn 
1357b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1358b02038faSJohn Youn 			 chan->hc_num);
1359b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Xfer Size: %d\n",
1360b02038faSJohn Youn 			 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1361b02038faSJohn Youn 			 TSIZ_XFERSIZE_SHIFT);
1362b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Num Pkts: %d\n",
1363b02038faSJohn Youn 			 (hctsiz & TSIZ_PKTCNT_MASK) >>
1364b02038faSJohn Youn 			 TSIZ_PKTCNT_SHIFT);
1365b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1366b02038faSJohn Youn 			 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1367b02038faSJohn Youn 			 TSIZ_SC_MC_PID_SHIFT);
1368b02038faSJohn Youn 	}
1369b02038faSJohn Youn 
137095832c00SJohn Youn 	if (hsotg->params.host_dma) {
1371af424a41SWilliam Wu 		dma_addr_t dma_addr;
1372af424a41SWilliam Wu 
1373af424a41SWilliam Wu 		if (chan->align_buf) {
1374af424a41SWilliam Wu 			if (dbg_hc(chan))
1375af424a41SWilliam Wu 				dev_vdbg(hsotg->dev, "align_buf\n");
1376af424a41SWilliam Wu 			dma_addr = chan->align_buf;
1377af424a41SWilliam Wu 		} else {
1378af424a41SWilliam Wu 			dma_addr = chan->xfer_dma;
1379af424a41SWilliam Wu 		}
1380f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
1381af424a41SWilliam Wu 
1382b02038faSJohn Youn 		if (dbg_hc(chan))
1383b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1384af424a41SWilliam Wu 				 (unsigned long)dma_addr, chan->hc_num);
1385b02038faSJohn Youn 	}
1386b02038faSJohn Youn 
1387b02038faSJohn Youn 	/* Start the split */
1388b02038faSJohn Youn 	if (chan->do_split) {
1389f25c42b8SGevorg Sahakyan 		u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
1390b02038faSJohn Youn 
1391b02038faSJohn Youn 		hcsplt |= HCSPLT_SPLTENA;
1392f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
1393b02038faSJohn Youn 	}
1394b02038faSJohn Youn 
1395f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1396b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1397b02038faSJohn Youn 	hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1398b02038faSJohn Youn 	dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1399b02038faSJohn Youn 
1400b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1401b02038faSJohn Youn 		dev_warn(hsotg->dev,
1402b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1403b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1404b02038faSJohn Youn 
1405b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1406b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1407b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1408b02038faSJohn Youn 
1409b02038faSJohn Youn 	if (dbg_hc(chan))
1410b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1411b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1412b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1413b02038faSJohn Youn 
1414f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1415b02038faSJohn Youn 	if (dbg_hc(chan))
1416b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1417b02038faSJohn Youn 			 chan->hc_num);
1418b02038faSJohn Youn 
1419b02038faSJohn Youn 	chan->xfer_started = 1;
1420b02038faSJohn Youn 	chan->requests++;
1421b02038faSJohn Youn 
142295832c00SJohn Youn 	if (!hsotg->params.host_dma &&
1423b02038faSJohn Youn 	    !chan->ep_is_in && chan->xfer_len > 0)
1424b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1425b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1426b02038faSJohn Youn }
1427b02038faSJohn Youn 
1428b02038faSJohn Youn /**
1429b02038faSJohn Youn  * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1430b02038faSJohn Youn  * host channel and starts the transfer in Descriptor DMA mode
1431b02038faSJohn Youn  *
1432b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1433b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1434b02038faSJohn Youn  *
1435b02038faSJohn Youn  * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1436b02038faSJohn Youn  * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1437b02038faSJohn Youn  * with micro-frame bitmap.
1438b02038faSJohn Youn  *
1439b02038faSJohn Youn  * Initializes HCDMA register with descriptor list address and CTD value then
1440b02038faSJohn Youn  * starts the transfer via enabling the channel.
1441b02038faSJohn Youn  */
1442b02038faSJohn Youn void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1443b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1444b02038faSJohn Youn {
1445b02038faSJohn Youn 	u32 hcchar;
1446b02038faSJohn Youn 	u32 hctsiz = 0;
1447b02038faSJohn Youn 
1448b02038faSJohn Youn 	if (chan->do_ping)
1449b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1450b02038faSJohn Youn 
1451b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1452b02038faSJohn Youn 		dwc2_set_pid_isoc(chan);
1453b02038faSJohn Youn 
1454b02038faSJohn Youn 	/* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1455b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1456b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1457b02038faSJohn Youn 
1458b02038faSJohn Youn 	/* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1459b02038faSJohn Youn 	hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1460b02038faSJohn Youn 
1461b02038faSJohn Youn 	/* Non-zero only for high-speed interrupt endpoints */
1462b02038faSJohn Youn 	hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1463b02038faSJohn Youn 
1464b02038faSJohn Youn 	if (dbg_hc(chan)) {
1465b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1466b02038faSJohn Youn 			 chan->hc_num);
1467b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1468b02038faSJohn Youn 			 chan->data_pid_start);
1469b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 NTD: %d\n", chan->ntd - 1);
1470b02038faSJohn Youn 	}
1471b02038faSJohn Youn 
1472f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1473b02038faSJohn Youn 
1474b02038faSJohn Youn 	dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1475b02038faSJohn Youn 				   chan->desc_list_sz, DMA_TO_DEVICE);
1476b02038faSJohn Youn 
1477f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
1478b02038faSJohn Youn 
1479b02038faSJohn Youn 	if (dbg_hc(chan))
1480b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1481b02038faSJohn Youn 			 &chan->desc_list_addr, chan->hc_num);
1482b02038faSJohn Youn 
1483f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1484b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1485b02038faSJohn Youn 	hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1486b02038faSJohn Youn 		  HCCHAR_MULTICNT_MASK;
1487b02038faSJohn Youn 
1488b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1489b02038faSJohn Youn 		dev_warn(hsotg->dev,
1490b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1491b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1492b02038faSJohn Youn 
1493b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1494b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1495b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1496b02038faSJohn Youn 
1497b02038faSJohn Youn 	if (dbg_hc(chan))
1498b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1499b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1500b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1501b02038faSJohn Youn 
1502f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1503b02038faSJohn Youn 	if (dbg_hc(chan))
1504b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1505b02038faSJohn Youn 			 chan->hc_num);
1506b02038faSJohn Youn 
1507b02038faSJohn Youn 	chan->xfer_started = 1;
1508b02038faSJohn Youn 	chan->requests++;
1509b02038faSJohn Youn }
1510b02038faSJohn Youn 
1511b02038faSJohn Youn /**
1512b02038faSJohn Youn  * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1513b02038faSJohn Youn  * a previous call to dwc2_hc_start_transfer()
1514b02038faSJohn Youn  *
1515b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1516b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1517b02038faSJohn Youn  *
1518b02038faSJohn Youn  * The caller must ensure there is sufficient space in the request queue and Tx
1519b02038faSJohn Youn  * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1520b02038faSJohn Youn  * the controller acts autonomously to complete transfers programmed to a host
1521b02038faSJohn Youn  * channel.
1522b02038faSJohn Youn  *
1523b02038faSJohn Youn  * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1524b02038faSJohn Youn  * if there is any data remaining to be queued. For an IN transfer, another
1525b02038faSJohn Youn  * data packet is always requested. For the SETUP phase of a control transfer,
1526b02038faSJohn Youn  * this function does nothing.
1527b02038faSJohn Youn  *
1528b02038faSJohn Youn  * Return: 1 if a new request is queued, 0 if no more requests are required
1529b02038faSJohn Youn  * for this transfer
1530b02038faSJohn Youn  */
1531b02038faSJohn Youn static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1532b02038faSJohn Youn 				     struct dwc2_host_chan *chan)
1533b02038faSJohn Youn {
1534b02038faSJohn Youn 	if (dbg_hc(chan))
1535b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1536b02038faSJohn Youn 			 chan->hc_num);
1537b02038faSJohn Youn 
1538b02038faSJohn Youn 	if (chan->do_split)
1539b02038faSJohn Youn 		/* SPLITs always queue just once per channel */
1540b02038faSJohn Youn 		return 0;
1541b02038faSJohn Youn 
1542b02038faSJohn Youn 	if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1543b02038faSJohn Youn 		/* SETUPs are queued only once since they can't be NAK'd */
1544b02038faSJohn Youn 		return 0;
1545b02038faSJohn Youn 
1546b02038faSJohn Youn 	if (chan->ep_is_in) {
1547b02038faSJohn Youn 		/*
1548b02038faSJohn Youn 		 * Always queue another request for other IN transfers. If
1549b02038faSJohn Youn 		 * back-to-back INs are issued and NAKs are received for both,
1550b02038faSJohn Youn 		 * the driver may still be processing the first NAK when the
1551b02038faSJohn Youn 		 * second NAK is received. When the interrupt handler clears
1552b02038faSJohn Youn 		 * the NAK interrupt for the first NAK, the second NAK will
1553b02038faSJohn Youn 		 * not be seen. So we can't depend on the NAK interrupt
1554b02038faSJohn Youn 		 * handler to requeue a NAK'd request. Instead, IN requests
1555b02038faSJohn Youn 		 * are issued each time this function is called. When the
1556b02038faSJohn Youn 		 * transfer completes, the extra requests for the channel will
1557b02038faSJohn Youn 		 * be flushed.
1558b02038faSJohn Youn 		 */
1559f25c42b8SGevorg Sahakyan 		u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1560b02038faSJohn Youn 
1561b02038faSJohn Youn 		dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1562b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
1563b02038faSJohn Youn 		hcchar &= ~HCCHAR_CHDIS;
1564b02038faSJohn Youn 		if (dbg_hc(chan))
1565b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	 IN xfer: hcchar = 0x%08x\n",
1566b02038faSJohn Youn 				 hcchar);
1567f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1568b02038faSJohn Youn 		chan->requests++;
1569b02038faSJohn Youn 		return 1;
1570b02038faSJohn Youn 	}
1571b02038faSJohn Youn 
1572b02038faSJohn Youn 	/* OUT transfers */
1573b02038faSJohn Youn 
1574b02038faSJohn Youn 	if (chan->xfer_count < chan->xfer_len) {
1575b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1576b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1577f25c42b8SGevorg Sahakyan 			u32 hcchar = dwc2_readl(hsotg,
1578b02038faSJohn Youn 						HCCHAR(chan->hc_num));
1579b02038faSJohn Youn 
1580b02038faSJohn Youn 			dwc2_hc_set_even_odd_frame(hsotg, chan,
1581b02038faSJohn Youn 						   &hcchar);
1582b02038faSJohn Youn 		}
1583b02038faSJohn Youn 
1584b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1585b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1586b02038faSJohn Youn 		chan->requests++;
1587b02038faSJohn Youn 		return 1;
1588b02038faSJohn Youn 	}
1589b02038faSJohn Youn 
1590b02038faSJohn Youn 	return 0;
1591b02038faSJohn Youn }
1592b02038faSJohn Youn 
1593b02038faSJohn Youn /*
1594b02038faSJohn Youn  * =========================================================================
1595b02038faSJohn Youn  *  HCD
1596b02038faSJohn Youn  * =========================================================================
1597b02038faSJohn Youn  */
1598b02038faSJohn Youn 
1599b02038faSJohn Youn /*
1600197ba5f4SPaul Zimmerman  * Processes all the URBs in a single list of QHs. Completes them with
1601197ba5f4SPaul Zimmerman  * -ETIMEDOUT and frees the QTD.
1602197ba5f4SPaul Zimmerman  *
1603197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1604197ba5f4SPaul Zimmerman  */
1605197ba5f4SPaul Zimmerman static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1606197ba5f4SPaul Zimmerman 				      struct list_head *qh_list)
1607197ba5f4SPaul Zimmerman {
1608197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1609197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1610197ba5f4SPaul Zimmerman 
1611197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1612197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1613197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
16142e84da6eSGregory Herrero 			dwc2_host_complete(hsotg, qtd, -ECONNRESET);
1615197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1616197ba5f4SPaul Zimmerman 		}
1617197ba5f4SPaul Zimmerman 	}
1618197ba5f4SPaul Zimmerman }
1619197ba5f4SPaul Zimmerman 
1620197ba5f4SPaul Zimmerman static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1621197ba5f4SPaul Zimmerman 			      struct list_head *qh_list)
1622197ba5f4SPaul Zimmerman {
1623197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1624197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1625197ba5f4SPaul Zimmerman 	unsigned long flags;
1626197ba5f4SPaul Zimmerman 
1627197ba5f4SPaul Zimmerman 	if (!qh_list->next)
1628197ba5f4SPaul Zimmerman 		/* The list hasn't been initialized yet */
1629197ba5f4SPaul Zimmerman 		return;
1630197ba5f4SPaul Zimmerman 
1631197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
1632197ba5f4SPaul Zimmerman 
1633197ba5f4SPaul Zimmerman 	/* Ensure there are no QTDs or URBs left */
1634197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1635197ba5f4SPaul Zimmerman 
1636197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1637197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1638197ba5f4SPaul Zimmerman 
1639197ba5f4SPaul Zimmerman 		/* Free each QTD in the QH's QTD list */
1640197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1641197ba5f4SPaul Zimmerman 					 qtd_list_entry)
1642197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1643197ba5f4SPaul Zimmerman 
164416e80218SDouglas Anderson 		if (qh->channel && qh->channel->qh == qh)
164516e80218SDouglas Anderson 			qh->channel->qh = NULL;
164616e80218SDouglas Anderson 
1647197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
1648197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_free(hsotg, qh);
1649197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
1650197ba5f4SPaul Zimmerman 	}
1651197ba5f4SPaul Zimmerman 
1652197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
1653197ba5f4SPaul Zimmerman }
1654197ba5f4SPaul Zimmerman 
1655197ba5f4SPaul Zimmerman /*
1656197ba5f4SPaul Zimmerman  * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1657197ba5f4SPaul Zimmerman  * and periodic schedules. The QTD associated with each URB is removed from
1658197ba5f4SPaul Zimmerman  * the schedule and freed. This function may be called when a disconnect is
1659197ba5f4SPaul Zimmerman  * detected or when the HCD is being stopped.
1660197ba5f4SPaul Zimmerman  *
1661197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1662197ba5f4SPaul Zimmerman  */
1663197ba5f4SPaul Zimmerman static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1664197ba5f4SPaul Zimmerman {
1665197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
166638d2b5fbSDouglas Anderson 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
1667197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1668197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1669197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1670197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1671197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1672197ba5f4SPaul Zimmerman }
1673197ba5f4SPaul Zimmerman 
1674197ba5f4SPaul Zimmerman /**
1675197ba5f4SPaul Zimmerman  * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1676197ba5f4SPaul Zimmerman  *
1677197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1678197ba5f4SPaul Zimmerman  */
1679197ba5f4SPaul Zimmerman void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1680197ba5f4SPaul Zimmerman {
1681197ba5f4SPaul Zimmerman 	u32 hprt0;
1682197ba5f4SPaul Zimmerman 
1683197ba5f4SPaul Zimmerman 	if (hsotg->op_state == OTG_STATE_B_HOST) {
1684197ba5f4SPaul Zimmerman 		/*
1685197ba5f4SPaul Zimmerman 		 * Reset the port. During a HNP mode switch the reset
1686197ba5f4SPaul Zimmerman 		 * needs to occur within 1ms and have a duration of at
1687197ba5f4SPaul Zimmerman 		 * least 50ms.
1688197ba5f4SPaul Zimmerman 		 */
1689197ba5f4SPaul Zimmerman 		hprt0 = dwc2_read_hprt0(hsotg);
1690197ba5f4SPaul Zimmerman 		hprt0 |= HPRT0_RST;
1691f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
1692197ba5f4SPaul Zimmerman 	}
1693197ba5f4SPaul Zimmerman 
1694197ba5f4SPaul Zimmerman 	queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1695197ba5f4SPaul Zimmerman 			   msecs_to_jiffies(50));
1696197ba5f4SPaul Zimmerman }
1697197ba5f4SPaul Zimmerman 
1698197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1699197ba5f4SPaul Zimmerman static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1700197ba5f4SPaul Zimmerman {
1701bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
1702197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
1703197ba5f4SPaul Zimmerman 	u32 hcchar;
1704197ba5f4SPaul Zimmerman 	int i;
1705197ba5f4SPaul Zimmerman 
170695832c00SJohn Youn 	if (!hsotg->params.host_dma) {
1707197ba5f4SPaul Zimmerman 		/* Flush out any channel requests in slave mode */
1708197ba5f4SPaul Zimmerman 		for (i = 0; i < num_channels; i++) {
1709197ba5f4SPaul Zimmerman 			channel = hsotg->hc_ptr_array[i];
1710197ba5f4SPaul Zimmerman 			if (!list_empty(&channel->hc_list_entry))
1711197ba5f4SPaul Zimmerman 				continue;
1712f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
1713197ba5f4SPaul Zimmerman 			if (hcchar & HCCHAR_CHENA) {
1714197ba5f4SPaul Zimmerman 				hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1715197ba5f4SPaul Zimmerman 				hcchar |= HCCHAR_CHDIS;
1716f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
1717197ba5f4SPaul Zimmerman 			}
1718197ba5f4SPaul Zimmerman 		}
1719197ba5f4SPaul Zimmerman 	}
1720197ba5f4SPaul Zimmerman 
1721197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
1722197ba5f4SPaul Zimmerman 		channel = hsotg->hc_ptr_array[i];
1723197ba5f4SPaul Zimmerman 		if (!list_empty(&channel->hc_list_entry))
1724197ba5f4SPaul Zimmerman 			continue;
1725f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(i));
1726197ba5f4SPaul Zimmerman 		if (hcchar & HCCHAR_CHENA) {
1727197ba5f4SPaul Zimmerman 			/* Halt the channel */
1728197ba5f4SPaul Zimmerman 			hcchar |= HCCHAR_CHDIS;
1729f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcchar, HCCHAR(i));
1730197ba5f4SPaul Zimmerman 		}
1731197ba5f4SPaul Zimmerman 
1732197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, channel);
1733197ba5f4SPaul Zimmerman 		list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1734197ba5f4SPaul Zimmerman 		/*
1735197ba5f4SPaul Zimmerman 		 * Added for Descriptor DMA to prevent channel double cleanup in
1736197ba5f4SPaul Zimmerman 		 * release_channel_ddma(), which is called from ep_disable when
1737197ba5f4SPaul Zimmerman 		 * device disconnects
1738197ba5f4SPaul Zimmerman 		 */
1739197ba5f4SPaul Zimmerman 		channel->qh = NULL;
1740197ba5f4SPaul Zimmerman 	}
17417252f1bfSVincent Palatin 	/* All channels have been freed, mark them available */
174295832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
17437252f1bfSVincent Palatin 		hsotg->available_host_channels =
1744bea8e86cSJohn Youn 			hsotg->params.host_channels;
17457252f1bfSVincent Palatin 	} else {
17467252f1bfSVincent Palatin 		hsotg->non_periodic_channels = 0;
17477252f1bfSVincent Palatin 		hsotg->periodic_channels = 0;
17487252f1bfSVincent Palatin 	}
1749197ba5f4SPaul Zimmerman }
1750197ba5f4SPaul Zimmerman 
1751197ba5f4SPaul Zimmerman /**
17526a659531SDouglas Anderson  * dwc2_hcd_connect() - Handles connect of the HCD
1753197ba5f4SPaul Zimmerman  *
1754197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1755197ba5f4SPaul Zimmerman  *
1756197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1757197ba5f4SPaul Zimmerman  */
17586a659531SDouglas Anderson void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
17596a659531SDouglas Anderson {
17606a659531SDouglas Anderson 	if (hsotg->lx_state != DWC2_L0)
17616a659531SDouglas Anderson 		usb_hcd_resume_root_hub(hsotg->priv);
17626a659531SDouglas Anderson 
17636a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status_change = 1;
17646a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status = 1;
17656a659531SDouglas Anderson }
17666a659531SDouglas Anderson 
17676a659531SDouglas Anderson /**
17686a659531SDouglas Anderson  * dwc2_hcd_disconnect() - Handles disconnect of the HCD
17696a659531SDouglas Anderson  *
17706a659531SDouglas Anderson  * @hsotg: Pointer to struct dwc2_hsotg
17716a659531SDouglas Anderson  * @force: If true, we won't try to reconnect even if we see device connected.
17726a659531SDouglas Anderson  *
17736a659531SDouglas Anderson  * Must be called with interrupt disabled and spinlock held
17746a659531SDouglas Anderson  */
17756a659531SDouglas Anderson void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
1776197ba5f4SPaul Zimmerman {
1777197ba5f4SPaul Zimmerman 	u32 intr;
17786a659531SDouglas Anderson 	u32 hprt0;
1779197ba5f4SPaul Zimmerman 
1780197ba5f4SPaul Zimmerman 	/* Set status flags for the hub driver */
1781197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status_change = 1;
1782197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status = 0;
1783197ba5f4SPaul Zimmerman 
1784197ba5f4SPaul Zimmerman 	/*
1785197ba5f4SPaul Zimmerman 	 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1786197ba5f4SPaul Zimmerman 	 * interrupt mask and status bits and disabling subsequent host
1787197ba5f4SPaul Zimmerman 	 * channel interrupts.
1788197ba5f4SPaul Zimmerman 	 */
1789f25c42b8SGevorg Sahakyan 	intr = dwc2_readl(hsotg, GINTMSK);
1790197ba5f4SPaul Zimmerman 	intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
1791f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTMSK);
1792197ba5f4SPaul Zimmerman 	intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
1793f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTSTS);
1794197ba5f4SPaul Zimmerman 
1795197ba5f4SPaul Zimmerman 	/*
1796197ba5f4SPaul Zimmerman 	 * Turn off the vbus power only if the core has transitioned to device
1797197ba5f4SPaul Zimmerman 	 * mode. If still in host mode, need to keep power on to detect a
1798197ba5f4SPaul Zimmerman 	 * reconnection.
1799197ba5f4SPaul Zimmerman 	 */
1800197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
1801197ba5f4SPaul Zimmerman 		if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1802197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
1803f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, HPRT0);
1804197ba5f4SPaul Zimmerman 		}
1805197ba5f4SPaul Zimmerman 
1806197ba5f4SPaul Zimmerman 		dwc2_disable_host_interrupts(hsotg);
1807197ba5f4SPaul Zimmerman 	}
1808197ba5f4SPaul Zimmerman 
1809197ba5f4SPaul Zimmerman 	/* Respond with an error status to all URBs in the schedule */
1810197ba5f4SPaul Zimmerman 	dwc2_kill_all_urbs(hsotg);
1811197ba5f4SPaul Zimmerman 
1812197ba5f4SPaul Zimmerman 	if (dwc2_is_host_mode(hsotg))
1813197ba5f4SPaul Zimmerman 		/* Clean up any host channels that were in use */
1814197ba5f4SPaul Zimmerman 		dwc2_hcd_cleanup_channels(hsotg);
1815197ba5f4SPaul Zimmerman 
1816197ba5f4SPaul Zimmerman 	dwc2_host_disconnect(hsotg);
18176a659531SDouglas Anderson 
18186a659531SDouglas Anderson 	/*
18196a659531SDouglas Anderson 	 * Add an extra check here to see if we're actually connected but
18206a659531SDouglas Anderson 	 * we don't have a detection interrupt pending.  This can happen if:
18216a659531SDouglas Anderson 	 *   1. hardware sees connect
18226a659531SDouglas Anderson 	 *   2. hardware sees disconnect
18236a659531SDouglas Anderson 	 *   3. hardware sees connect
18246a659531SDouglas Anderson 	 *   4. dwc2_port_intr() - clears connect interrupt
18256a659531SDouglas Anderson 	 *   5. dwc2_handle_common_intr() - calls here
18266a659531SDouglas Anderson 	 *
18276a659531SDouglas Anderson 	 * Without the extra check here we will end calling disconnect
18286a659531SDouglas Anderson 	 * and won't get any future interrupts to handle the connect.
18296a659531SDouglas Anderson 	 */
18306a659531SDouglas Anderson 	if (!force) {
1831f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
18326a659531SDouglas Anderson 		if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
18336a659531SDouglas Anderson 			dwc2_hcd_connect(hsotg);
18346a659531SDouglas Anderson 	}
1835197ba5f4SPaul Zimmerman }
1836197ba5f4SPaul Zimmerman 
1837197ba5f4SPaul Zimmerman /**
1838197ba5f4SPaul Zimmerman  * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
1839197ba5f4SPaul Zimmerman  *
1840197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1841197ba5f4SPaul Zimmerman  */
1842197ba5f4SPaul Zimmerman static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
1843197ba5f4SPaul Zimmerman {
18441fb7f12dSDouglas Anderson 	if (hsotg->bus_suspended) {
1845197ba5f4SPaul Zimmerman 		hsotg->flags.b.port_suspend_change = 1;
1846b46146d5SGregory Herrero 		usb_hcd_resume_root_hub(hsotg->priv);
1847197ba5f4SPaul Zimmerman 	}
18481fb7f12dSDouglas Anderson 
18491fb7f12dSDouglas Anderson 	if (hsotg->lx_state == DWC2_L1)
18501fb7f12dSDouglas Anderson 		hsotg->flags.b.port_l1_change = 1;
1851b46146d5SGregory Herrero }
1852197ba5f4SPaul Zimmerman 
1853197ba5f4SPaul Zimmerman /**
1854197ba5f4SPaul Zimmerman  * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
1855197ba5f4SPaul Zimmerman  *
1856197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1857197ba5f4SPaul Zimmerman  *
1858197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1859197ba5f4SPaul Zimmerman  */
1860197ba5f4SPaul Zimmerman void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
1861197ba5f4SPaul Zimmerman {
1862197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
1863197ba5f4SPaul Zimmerman 
1864197ba5f4SPaul Zimmerman 	/*
1865197ba5f4SPaul Zimmerman 	 * The root hub should be disconnected before this function is called.
1866197ba5f4SPaul Zimmerman 	 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
1867197ba5f4SPaul Zimmerman 	 * and the QH lists (via ..._hcd_endpoint_disable).
1868197ba5f4SPaul Zimmerman 	 */
1869197ba5f4SPaul Zimmerman 
1870197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
1871197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
1872197ba5f4SPaul Zimmerman 
1873197ba5f4SPaul Zimmerman 	/* Turn off the vbus power */
1874197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "PortPower off\n");
1875f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HPRT0);
1876197ba5f4SPaul Zimmerman }
1877197ba5f4SPaul Zimmerman 
187833ad261aSGregory Herrero /* Caller must hold driver lock */
1879197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
1880b58e6ceeSMian Yousaf Kaukab 				struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
1881b5a468a6SMian Yousaf Kaukab 				struct dwc2_qtd *qtd)
1882197ba5f4SPaul Zimmerman {
1883197ba5f4SPaul Zimmerman 	u32 intr_mask;
1884197ba5f4SPaul Zimmerman 	int retval;
1885197ba5f4SPaul Zimmerman 	int dev_speed;
1886197ba5f4SPaul Zimmerman 
1887197ba5f4SPaul Zimmerman 	if (!hsotg->flags.b.port_connect_status) {
1888197ba5f4SPaul Zimmerman 		/* No longer connected */
1889197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Not connected\n");
1890197ba5f4SPaul Zimmerman 		return -ENODEV;
1891197ba5f4SPaul Zimmerman 	}
1892197ba5f4SPaul Zimmerman 
1893197ba5f4SPaul Zimmerman 	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1894197ba5f4SPaul Zimmerman 
1895197ba5f4SPaul Zimmerman 	/* Some configurations cannot support LS traffic on a FS root port */
1896197ba5f4SPaul Zimmerman 	if ((dev_speed == USB_SPEED_LOW) &&
1897197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
1898197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
1899f25c42b8SGevorg Sahakyan 		u32 hprt0 = dwc2_readl(hsotg, HPRT0);
1900197ba5f4SPaul Zimmerman 		u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1901197ba5f4SPaul Zimmerman 
1902197ba5f4SPaul Zimmerman 		if (prtspd == HPRT0_SPD_FULL_SPEED)
1903197ba5f4SPaul Zimmerman 			return -ENODEV;
1904197ba5f4SPaul Zimmerman 	}
1905197ba5f4SPaul Zimmerman 
1906197ba5f4SPaul Zimmerman 	if (!qtd)
1907b5a468a6SMian Yousaf Kaukab 		return -EINVAL;
1908197ba5f4SPaul Zimmerman 
1909197ba5f4SPaul Zimmerman 	dwc2_hcd_qtd_init(qtd, urb);
1910b58e6ceeSMian Yousaf Kaukab 	retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
1911197ba5f4SPaul Zimmerman 	if (retval) {
1912197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
1913197ba5f4SPaul Zimmerman 			"DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
1914197ba5f4SPaul Zimmerman 			retval);
1915197ba5f4SPaul Zimmerman 		return retval;
1916197ba5f4SPaul Zimmerman 	}
1917197ba5f4SPaul Zimmerman 
1918f25c42b8SGevorg Sahakyan 	intr_mask = dwc2_readl(hsotg, GINTMSK);
1919197ba5f4SPaul Zimmerman 	if (!(intr_mask & GINTSTS_SOF)) {
1920197ba5f4SPaul Zimmerman 		enum dwc2_transaction_type tr_type;
1921197ba5f4SPaul Zimmerman 
1922197ba5f4SPaul Zimmerman 		if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
1923197ba5f4SPaul Zimmerman 		    !(qtd->urb->flags & URB_GIVEBACK_ASAP))
1924197ba5f4SPaul Zimmerman 			/*
1925197ba5f4SPaul Zimmerman 			 * Do not schedule SG transactions until qtd has
1926197ba5f4SPaul Zimmerman 			 * URB_GIVEBACK_ASAP set
1927197ba5f4SPaul Zimmerman 			 */
1928197ba5f4SPaul Zimmerman 			return 0;
1929197ba5f4SPaul Zimmerman 
1930197ba5f4SPaul Zimmerman 		tr_type = dwc2_hcd_select_transactions(hsotg);
1931197ba5f4SPaul Zimmerman 		if (tr_type != DWC2_TRANSACTION_NONE)
1932197ba5f4SPaul Zimmerman 			dwc2_hcd_queue_transactions(hsotg, tr_type);
1933197ba5f4SPaul Zimmerman 	}
1934197ba5f4SPaul Zimmerman 
1935197ba5f4SPaul Zimmerman 	return 0;
1936197ba5f4SPaul Zimmerman }
1937197ba5f4SPaul Zimmerman 
1938197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1939197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
1940197ba5f4SPaul Zimmerman 				struct dwc2_hcd_urb *urb)
1941197ba5f4SPaul Zimmerman {
1942197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
1943197ba5f4SPaul Zimmerman 	struct dwc2_qtd *urb_qtd;
1944197ba5f4SPaul Zimmerman 
1945197ba5f4SPaul Zimmerman 	urb_qtd = urb->qtd;
1946197ba5f4SPaul Zimmerman 	if (!urb_qtd) {
1947197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
1948197ba5f4SPaul Zimmerman 		return -EINVAL;
1949197ba5f4SPaul Zimmerman 	}
1950197ba5f4SPaul Zimmerman 
1951197ba5f4SPaul Zimmerman 	qh = urb_qtd->qh;
1952197ba5f4SPaul Zimmerman 	if (!qh) {
1953197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
1954197ba5f4SPaul Zimmerman 		return -EINVAL;
1955197ba5f4SPaul Zimmerman 	}
1956197ba5f4SPaul Zimmerman 
1957197ba5f4SPaul Zimmerman 	urb->priv = NULL;
1958197ba5f4SPaul Zimmerman 
1959197ba5f4SPaul Zimmerman 	if (urb_qtd->in_process && qh->channel) {
1960197ba5f4SPaul Zimmerman 		dwc2_dump_channel_info(hsotg, qh->channel);
1961197ba5f4SPaul Zimmerman 
1962197ba5f4SPaul Zimmerman 		/* The QTD is in process (it has been assigned to a channel) */
1963197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status)
1964197ba5f4SPaul Zimmerman 			/*
1965197ba5f4SPaul Zimmerman 			 * If still connected (i.e. in host mode), halt the
1966197ba5f4SPaul Zimmerman 			 * channel so it can be used for other transfers. If
1967197ba5f4SPaul Zimmerman 			 * no longer connected, the host registers can't be
1968197ba5f4SPaul Zimmerman 			 * written to halt the channel since the core is in
1969197ba5f4SPaul Zimmerman 			 * device mode.
1970197ba5f4SPaul Zimmerman 			 */
1971197ba5f4SPaul Zimmerman 			dwc2_hc_halt(hsotg, qh->channel,
1972197ba5f4SPaul Zimmerman 				     DWC2_HC_XFER_URB_DEQUEUE);
1973197ba5f4SPaul Zimmerman 	}
1974197ba5f4SPaul Zimmerman 
1975197ba5f4SPaul Zimmerman 	/*
1976197ba5f4SPaul Zimmerman 	 * Free the QTD and clean up the associated QH. Leave the QH in the
1977197ba5f4SPaul Zimmerman 	 * schedule if it has any remaining QTDs.
1978197ba5f4SPaul Zimmerman 	 */
197995832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
1980197ba5f4SPaul Zimmerman 		u8 in_process = urb_qtd->in_process;
1981197ba5f4SPaul Zimmerman 
1982197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1983197ba5f4SPaul Zimmerman 		if (in_process) {
1984197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_deactivate(hsotg, qh, 0);
1985197ba5f4SPaul Zimmerman 			qh->channel = NULL;
1986197ba5f4SPaul Zimmerman 		} else if (list_empty(&qh->qtd_list)) {
1987197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
1988197ba5f4SPaul Zimmerman 		}
1989197ba5f4SPaul Zimmerman 	} else {
1990197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1991197ba5f4SPaul Zimmerman 	}
1992197ba5f4SPaul Zimmerman 
1993197ba5f4SPaul Zimmerman 	return 0;
1994197ba5f4SPaul Zimmerman }
1995197ba5f4SPaul Zimmerman 
1996197ba5f4SPaul Zimmerman /* Must NOT be called with interrupt disabled or spinlock held */
1997197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
1998197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep, int retry)
1999197ba5f4SPaul Zimmerman {
2000197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
2001197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2002197ba5f4SPaul Zimmerman 	unsigned long flags;
2003197ba5f4SPaul Zimmerman 	int rc;
2004197ba5f4SPaul Zimmerman 
2005197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
2006197ba5f4SPaul Zimmerman 
2007197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
2008197ba5f4SPaul Zimmerman 	if (!qh) {
2009197ba5f4SPaul Zimmerman 		rc = -EINVAL;
2010197ba5f4SPaul Zimmerman 		goto err;
2011197ba5f4SPaul Zimmerman 	}
2012197ba5f4SPaul Zimmerman 
2013197ba5f4SPaul Zimmerman 	while (!list_empty(&qh->qtd_list) && retry--) {
2014197ba5f4SPaul Zimmerman 		if (retry == 0) {
2015197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2016197ba5f4SPaul Zimmerman 				"## timeout in dwc2_hcd_endpoint_disable() ##\n");
2017197ba5f4SPaul Zimmerman 			rc = -EBUSY;
2018197ba5f4SPaul Zimmerman 			goto err;
2019197ba5f4SPaul Zimmerman 		}
2020197ba5f4SPaul Zimmerman 
2021197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
202204a9db79SNicholas Mc Guire 		msleep(20);
2023197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
2024197ba5f4SPaul Zimmerman 		qh = ep->hcpriv;
2025197ba5f4SPaul Zimmerman 		if (!qh) {
2026197ba5f4SPaul Zimmerman 			rc = -EINVAL;
2027197ba5f4SPaul Zimmerman 			goto err;
2028197ba5f4SPaul Zimmerman 		}
2029197ba5f4SPaul Zimmerman 	}
2030197ba5f4SPaul Zimmerman 
2031197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_unlink(hsotg, qh);
2032197ba5f4SPaul Zimmerman 
2033197ba5f4SPaul Zimmerman 	/* Free each QTD in the QH's QTD list */
2034197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2035197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2036197ba5f4SPaul Zimmerman 
2037197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
203816e80218SDouglas Anderson 
203916e80218SDouglas Anderson 	if (qh->channel && qh->channel->qh == qh)
204016e80218SDouglas Anderson 		qh->channel->qh = NULL;
204116e80218SDouglas Anderson 
2042197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
204316e80218SDouglas Anderson 
2044197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_free(hsotg, qh);
2045197ba5f4SPaul Zimmerman 
2046197ba5f4SPaul Zimmerman 	return 0;
2047197ba5f4SPaul Zimmerman 
2048197ba5f4SPaul Zimmerman err:
2049197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
2050197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
2051197ba5f4SPaul Zimmerman 
2052197ba5f4SPaul Zimmerman 	return rc;
2053197ba5f4SPaul Zimmerman }
2054197ba5f4SPaul Zimmerman 
2055197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
2056197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2057197ba5f4SPaul Zimmerman 				   struct usb_host_endpoint *ep)
2058197ba5f4SPaul Zimmerman {
2059197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = ep->hcpriv;
2060197ba5f4SPaul Zimmerman 
2061197ba5f4SPaul Zimmerman 	if (!qh)
2062197ba5f4SPaul Zimmerman 		return -EINVAL;
2063197ba5f4SPaul Zimmerman 
2064197ba5f4SPaul Zimmerman 	qh->data_toggle = DWC2_HC_PID_DATA0;
2065197ba5f4SPaul Zimmerman 
2066197ba5f4SPaul Zimmerman 	return 0;
2067197ba5f4SPaul Zimmerman }
2068197ba5f4SPaul Zimmerman 
2069b02038faSJohn Youn /**
2070b02038faSJohn Youn  * dwc2_core_init() - Initializes the DWC_otg controller registers and
2071b02038faSJohn Youn  * prepares the core for device mode or host mode operation
2072b02038faSJohn Youn  *
2073b02038faSJohn Youn  * @hsotg:         Programming view of the DWC_otg controller
2074b02038faSJohn Youn  * @initial_setup: If true then this is the first init for this instance.
2075b02038faSJohn Youn  */
207665c9c4c6SVardan Mikayelyan int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2077b02038faSJohn Youn {
2078b02038faSJohn Youn 	u32 usbcfg, otgctl;
2079b02038faSJohn Youn 	int retval;
2080b02038faSJohn Youn 
2081b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2082b02038faSJohn Youn 
2083f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
2084b02038faSJohn Youn 
2085b02038faSJohn Youn 	/* Set ULPI External VBUS bit if needed */
2086b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
208795832c00SJohn Youn 	if (hsotg->params.phy_ulpi_ext_vbus)
2088b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2089b02038faSJohn Youn 
2090b02038faSJohn Youn 	/* Set external TS Dline pulsing bit if needed */
2091b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
209295832c00SJohn Youn 	if (hsotg->params.ts_dline)
2093b02038faSJohn Youn 		usbcfg |= GUSBCFG_TERMSELDLPULSE;
2094b02038faSJohn Youn 
2095f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
2096b02038faSJohn Youn 
2097b02038faSJohn Youn 	/*
2098b02038faSJohn Youn 	 * Reset the Controller
2099b02038faSJohn Youn 	 *
2100b02038faSJohn Youn 	 * We only need to reset the controller if this is a re-init.
2101b02038faSJohn Youn 	 * For the first init we know for sure that earlier code reset us (it
2102b02038faSJohn Youn 	 * needed to in order to properly detect various parameters).
2103b02038faSJohn Youn 	 */
2104b02038faSJohn Youn 	if (!initial_setup) {
210513b1f8e2SVardan Mikayelyan 		retval = dwc2_core_reset(hsotg, false);
2106b02038faSJohn Youn 		if (retval) {
2107b02038faSJohn Youn 			dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2108b02038faSJohn Youn 				__func__);
2109b02038faSJohn Youn 			return retval;
2110b02038faSJohn Youn 		}
2111b02038faSJohn Youn 	}
2112b02038faSJohn Youn 
2113b02038faSJohn Youn 	/*
2114b02038faSJohn Youn 	 * This needs to happen in FS mode before any other programming occurs
2115b02038faSJohn Youn 	 */
2116b02038faSJohn Youn 	retval = dwc2_phy_init(hsotg, initial_setup);
2117b02038faSJohn Youn 	if (retval)
2118b02038faSJohn Youn 		return retval;
2119b02038faSJohn Youn 
2120b02038faSJohn Youn 	/* Program the GAHBCFG Register */
2121b02038faSJohn Youn 	retval = dwc2_gahbcfg_init(hsotg);
2122b02038faSJohn Youn 	if (retval)
2123b02038faSJohn Youn 		return retval;
2124b02038faSJohn Youn 
2125b02038faSJohn Youn 	/* Program the GUSBCFG register */
2126b02038faSJohn Youn 	dwc2_gusbcfg_init(hsotg);
2127b02038faSJohn Youn 
2128b02038faSJohn Youn 	/* Program the GOTGCTL register */
2129f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2130b02038faSJohn Youn 	otgctl &= ~GOTGCTL_OTGVER;
2131f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2132b02038faSJohn Youn 
2133b02038faSJohn Youn 	/* Clear the SRP success bit for FS-I2c */
2134b02038faSJohn Youn 	hsotg->srp_success = 0;
2135b02038faSJohn Youn 
2136b02038faSJohn Youn 	/* Enable common interrupts */
2137b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
2138b02038faSJohn Youn 
2139b02038faSJohn Youn 	/*
2140b02038faSJohn Youn 	 * Do device or host initialization based on mode during PCD and
2141b02038faSJohn Youn 	 * HCD initialization
2142b02038faSJohn Youn 	 */
2143b02038faSJohn Youn 	if (dwc2_is_host_mode(hsotg)) {
2144b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Host Mode\n");
2145b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_A_HOST;
2146b02038faSJohn Youn 	} else {
2147b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Device Mode\n");
2148b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2149b02038faSJohn Youn 	}
2150b02038faSJohn Youn 
2151b02038faSJohn Youn 	return 0;
2152b02038faSJohn Youn }
2153b02038faSJohn Youn 
2154b02038faSJohn Youn /**
2155b02038faSJohn Youn  * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2156b02038faSJohn Youn  * Host mode
2157b02038faSJohn Youn  *
2158b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
2159b02038faSJohn Youn  *
2160b02038faSJohn Youn  * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2161b02038faSJohn Youn  * request queues. Host channels are reset to ensure that they are ready for
2162b02038faSJohn Youn  * performing transfers.
2163b02038faSJohn Youn  */
2164b02038faSJohn Youn static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2165b02038faSJohn Youn {
216692a8dd26SMinas Harutyunyan 	u32 hcfg, hfir, otgctl, usbcfg;
2167b02038faSJohn Youn 
2168b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2169b02038faSJohn Youn 
217092a8dd26SMinas Harutyunyan 	/* Set HS/FS Timeout Calibration to 7 (max available value).
217192a8dd26SMinas Harutyunyan 	 * The number of PHY clocks that the application programs in
217292a8dd26SMinas Harutyunyan 	 * this field is added to the high/full speed interpacket timeout
217392a8dd26SMinas Harutyunyan 	 * duration in the core to account for any additional delays
217492a8dd26SMinas Harutyunyan 	 * introduced by the PHY. This can be required, because the delay
217592a8dd26SMinas Harutyunyan 	 * introduced by the PHY in generating the linestate condition
217692a8dd26SMinas Harutyunyan 	 * can vary from one PHY to another.
217792a8dd26SMinas Harutyunyan 	 */
2178f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
217992a8dd26SMinas Harutyunyan 	usbcfg |= GUSBCFG_TOUTCAL(7);
2180f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
218192a8dd26SMinas Harutyunyan 
2182b02038faSJohn Youn 	/* Restart the Phy Clock */
2183f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, PCGCTL);
2184b02038faSJohn Youn 
2185b02038faSJohn Youn 	/* Initialize Host Configuration Register */
2186b02038faSJohn Youn 	dwc2_init_fs_ls_pclk_sel(hsotg);
218738e9002bSVardan Mikayelyan 	if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
218838e9002bSVardan Mikayelyan 	    hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
2189f25c42b8SGevorg Sahakyan 		hcfg = dwc2_readl(hsotg, HCFG);
2190b02038faSJohn Youn 		hcfg |= HCFG_FSLSSUPP;
2191f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcfg, HCFG);
2192b02038faSJohn Youn 	}
2193b02038faSJohn Youn 
2194b02038faSJohn Youn 	/*
2195b02038faSJohn Youn 	 * This bit allows dynamic reloading of the HFIR register during
2196b02038faSJohn Youn 	 * runtime. This bit needs to be programmed during initial configuration
2197b02038faSJohn Youn 	 * and its value must not be changed during runtime.
2198b02038faSJohn Youn 	 */
219995832c00SJohn Youn 	if (hsotg->params.reload_ctl) {
2200f25c42b8SGevorg Sahakyan 		hfir = dwc2_readl(hsotg, HFIR);
2201b02038faSJohn Youn 		hfir |= HFIR_RLDCTRL;
2202f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hfir, HFIR);
2203b02038faSJohn Youn 	}
2204b02038faSJohn Youn 
220595832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2206b02038faSJohn Youn 		u32 op_mode = hsotg->hw_params.op_mode;
2207b02038faSJohn Youn 
2208b02038faSJohn Youn 		if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2209b02038faSJohn Youn 		    !hsotg->hw_params.dma_desc_enable ||
2210b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2211b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2212b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2213b02038faSJohn Youn 			dev_err(hsotg->dev,
2214b02038faSJohn Youn 				"Hardware does not support descriptor DMA mode -\n");
2215b02038faSJohn Youn 			dev_err(hsotg->dev,
2216b02038faSJohn Youn 				"falling back to buffer DMA mode.\n");
221795832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
2218b02038faSJohn Youn 		} else {
2219f25c42b8SGevorg Sahakyan 			hcfg = dwc2_readl(hsotg, HCFG);
2220b02038faSJohn Youn 			hcfg |= HCFG_DESCDMA;
2221f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcfg, HCFG);
2222b02038faSJohn Youn 		}
2223b02038faSJohn Youn 	}
2224b02038faSJohn Youn 
2225b02038faSJohn Youn 	/* Configure data FIFO sizes */
2226b02038faSJohn Youn 	dwc2_config_fifos(hsotg);
2227b02038faSJohn Youn 
2228b02038faSJohn Youn 	/* TODO - check this */
2229b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2230f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2231b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2232f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2233b02038faSJohn Youn 
2234b02038faSJohn Youn 	/* Make sure the FIFOs are flushed */
2235b02038faSJohn Youn 	dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2236b02038faSJohn Youn 	dwc2_flush_rx_fifo(hsotg);
2237b02038faSJohn Youn 
2238b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2239f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2240b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2241f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2242b02038faSJohn Youn 
224395832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
2244b02038faSJohn Youn 		int num_channels, i;
2245b02038faSJohn Youn 		u32 hcchar;
2246b02038faSJohn Youn 
2247b02038faSJohn Youn 		/* Flush out any leftover queued requests */
2248bea8e86cSJohn Youn 		num_channels = hsotg->params.host_channels;
2249b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2250f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22515799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2252b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
2253b02038faSJohn Youn 				hcchar |= HCCHAR_CHDIS;
2254b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2255f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2256b02038faSJohn Youn 			}
22575799aecdSMinas Harutyunyan 		}
2258b02038faSJohn Youn 
2259b02038faSJohn Youn 		/* Halt all channels to put them into a known state */
2260b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2261f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22625799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2263b02038faSJohn Youn 				hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2264b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2265f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2266b02038faSJohn Youn 				dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2267b02038faSJohn Youn 					__func__, i);
226879d6b8c5SSevak Arakelyan 
226979d6b8c5SSevak Arakelyan 				if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
22705799aecdSMinas Harutyunyan 							      HCCHAR_CHENA,
22715799aecdSMinas Harutyunyan 							      1000)) {
22725799aecdSMinas Harutyunyan 					dev_warn(hsotg->dev,
22735799aecdSMinas Harutyunyan 						 "Unable to clear enable on channel %d\n",
2274b02038faSJohn Youn 						 i);
2275b02038faSJohn Youn 				}
2276b02038faSJohn Youn 			}
2277b02038faSJohn Youn 		}
22785799aecdSMinas Harutyunyan 	}
2279b02038faSJohn Youn 
228066e77a24SRazmik Karapetyan 	/* Enable ACG feature in host mode, if supported */
228166e77a24SRazmik Karapetyan 	dwc2_enable_acg(hsotg);
228266e77a24SRazmik Karapetyan 
2283b02038faSJohn Youn 	/* Turn on the vbus power */
2284b02038faSJohn Youn 	dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2285b02038faSJohn Youn 	if (hsotg->op_state == OTG_STATE_A_HOST) {
2286b02038faSJohn Youn 		u32 hprt0 = dwc2_read_hprt0(hsotg);
2287b02038faSJohn Youn 
2288b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2289b02038faSJohn Youn 			!!(hprt0 & HPRT0_PWR));
2290b02038faSJohn Youn 		if (!(hprt0 & HPRT0_PWR)) {
2291b02038faSJohn Youn 			hprt0 |= HPRT0_PWR;
2292f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
2293b02038faSJohn Youn 		}
2294b02038faSJohn Youn 	}
2295b02038faSJohn Youn 
2296b02038faSJohn Youn 	dwc2_enable_host_interrupts(hsotg);
2297b02038faSJohn Youn }
2298b02038faSJohn Youn 
2299197ba5f4SPaul Zimmerman /*
2300197ba5f4SPaul Zimmerman  * Initializes dynamic portions of the DWC_otg HCD state
2301197ba5f4SPaul Zimmerman  *
2302197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2303197ba5f4SPaul Zimmerman  */
2304197ba5f4SPaul Zimmerman static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2305197ba5f4SPaul Zimmerman {
2306197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan, *chan_tmp;
2307197ba5f4SPaul Zimmerman 	int num_channels;
2308197ba5f4SPaul Zimmerman 	int i;
2309197ba5f4SPaul Zimmerman 
2310197ba5f4SPaul Zimmerman 	hsotg->flags.d32 = 0;
2311197ba5f4SPaul Zimmerman 	hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
2312197ba5f4SPaul Zimmerman 
231395832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
2314197ba5f4SPaul Zimmerman 		hsotg->available_host_channels =
2315bea8e86cSJohn Youn 			hsotg->params.host_channels;
2316197ba5f4SPaul Zimmerman 	} else {
2317197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels = 0;
2318197ba5f4SPaul Zimmerman 		hsotg->periodic_channels = 0;
2319197ba5f4SPaul Zimmerman 	}
2320197ba5f4SPaul Zimmerman 
2321197ba5f4SPaul Zimmerman 	/*
2322197ba5f4SPaul Zimmerman 	 * Put all channels in the free channel list and clean up channel
2323197ba5f4SPaul Zimmerman 	 * states
2324197ba5f4SPaul Zimmerman 	 */
2325197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2326197ba5f4SPaul Zimmerman 				 hc_list_entry)
2327197ba5f4SPaul Zimmerman 		list_del_init(&chan->hc_list_entry);
2328197ba5f4SPaul Zimmerman 
2329bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2330197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
2331197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
2332197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2333197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
2334197ba5f4SPaul Zimmerman 	}
2335197ba5f4SPaul Zimmerman 
2336197ba5f4SPaul Zimmerman 	/* Initialize the DWC core for host mode operation */
2337197ba5f4SPaul Zimmerman 	dwc2_core_host_init(hsotg);
2338197ba5f4SPaul Zimmerman }
2339197ba5f4SPaul Zimmerman 
2340197ba5f4SPaul Zimmerman static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2341197ba5f4SPaul Zimmerman 			       struct dwc2_host_chan *chan,
2342197ba5f4SPaul Zimmerman 			       struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2343197ba5f4SPaul Zimmerman {
2344197ba5f4SPaul Zimmerman 	int hub_addr, hub_port;
2345197ba5f4SPaul Zimmerman 
2346197ba5f4SPaul Zimmerman 	chan->do_split = 1;
2347197ba5f4SPaul Zimmerman 	chan->xact_pos = qtd->isoc_split_pos;
2348197ba5f4SPaul Zimmerman 	chan->complete_split = qtd->complete_split;
2349197ba5f4SPaul Zimmerman 	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2350197ba5f4SPaul Zimmerman 	chan->hub_addr = (u8)hub_addr;
2351197ba5f4SPaul Zimmerman 	chan->hub_port = (u8)hub_port;
2352197ba5f4SPaul Zimmerman }
2353197ba5f4SPaul Zimmerman 
23543bc04e28SDouglas Anderson static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2355197ba5f4SPaul Zimmerman 			      struct dwc2_host_chan *chan,
23563bc04e28SDouglas Anderson 			      struct dwc2_qtd *qtd)
2357197ba5f4SPaul Zimmerman {
2358197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
2359197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
2360197ba5f4SPaul Zimmerman 
2361197ba5f4SPaul Zimmerman 	switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2362197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
2363197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2364197ba5f4SPaul Zimmerman 
2365197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
2366197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
2367197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control setup transaction\n");
2368197ba5f4SPaul Zimmerman 			chan->do_ping = 0;
2369197ba5f4SPaul Zimmerman 			chan->ep_is_in = 0;
2370197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_SETUP;
237195832c00SJohn Youn 			if (hsotg->params.host_dma)
2372197ba5f4SPaul Zimmerman 				chan->xfer_dma = urb->setup_dma;
2373197ba5f4SPaul Zimmerman 			else
2374197ba5f4SPaul Zimmerman 				chan->xfer_buf = urb->setup_packet;
2375197ba5f4SPaul Zimmerman 			chan->xfer_len = 8;
2376197ba5f4SPaul Zimmerman 			break;
2377197ba5f4SPaul Zimmerman 
2378197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
2379197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control data transaction\n");
2380197ba5f4SPaul Zimmerman 			chan->data_pid_start = qtd->data_toggle;
2381197ba5f4SPaul Zimmerman 			break;
2382197ba5f4SPaul Zimmerman 
2383197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_STATUS:
2384197ba5f4SPaul Zimmerman 			/*
2385197ba5f4SPaul Zimmerman 			 * Direction is opposite of data direction or IN if no
2386197ba5f4SPaul Zimmerman 			 * data
2387197ba5f4SPaul Zimmerman 			 */
2388197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control status transaction\n");
2389197ba5f4SPaul Zimmerman 			if (urb->length == 0)
2390197ba5f4SPaul Zimmerman 				chan->ep_is_in = 1;
2391197ba5f4SPaul Zimmerman 			else
2392197ba5f4SPaul Zimmerman 				chan->ep_is_in =
2393197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_out(&urb->pipe_info);
2394197ba5f4SPaul Zimmerman 			if (chan->ep_is_in)
2395197ba5f4SPaul Zimmerman 				chan->do_ping = 0;
2396197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_DATA1;
2397197ba5f4SPaul Zimmerman 			chan->xfer_len = 0;
239895832c00SJohn Youn 			if (hsotg->params.host_dma)
2399197ba5f4SPaul Zimmerman 				chan->xfer_dma = hsotg->status_buf_dma;
2400197ba5f4SPaul Zimmerman 			else
2401197ba5f4SPaul Zimmerman 				chan->xfer_buf = hsotg->status_buf;
2402197ba5f4SPaul Zimmerman 			break;
2403197ba5f4SPaul Zimmerman 		}
2404197ba5f4SPaul Zimmerman 		break;
2405197ba5f4SPaul Zimmerman 
2406197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
2407197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_BULK;
2408197ba5f4SPaul Zimmerman 		break;
2409197ba5f4SPaul Zimmerman 
2410197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
2411197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_INT;
2412197ba5f4SPaul Zimmerman 		break;
2413197ba5f4SPaul Zimmerman 
2414197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
2415197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_ISOC;
241695832c00SJohn Youn 		if (hsotg->params.dma_desc_enable)
2417197ba5f4SPaul Zimmerman 			break;
2418197ba5f4SPaul Zimmerman 
2419197ba5f4SPaul Zimmerman 		frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2420197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
2421197ba5f4SPaul Zimmerman 
242295832c00SJohn Youn 		if (hsotg->params.host_dma) {
2423197ba5f4SPaul Zimmerman 			chan->xfer_dma = urb->dma;
2424197ba5f4SPaul Zimmerman 			chan->xfer_dma += frame_desc->offset +
2425197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2426197ba5f4SPaul Zimmerman 		} else {
2427197ba5f4SPaul Zimmerman 			chan->xfer_buf = urb->buf;
2428197ba5f4SPaul Zimmerman 			chan->xfer_buf += frame_desc->offset +
2429197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2430197ba5f4SPaul Zimmerman 		}
2431197ba5f4SPaul Zimmerman 
2432197ba5f4SPaul Zimmerman 		chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2433197ba5f4SPaul Zimmerman 
2434197ba5f4SPaul Zimmerman 		if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2435197ba5f4SPaul Zimmerman 			if (chan->xfer_len <= 188)
2436197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2437197ba5f4SPaul Zimmerman 			else
2438197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2439197ba5f4SPaul Zimmerman 		}
2440197ba5f4SPaul Zimmerman 		break;
2441197ba5f4SPaul Zimmerman 	}
2442197ba5f4SPaul Zimmerman }
2443197ba5f4SPaul Zimmerman 
2444af424a41SWilliam Wu static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
2445af424a41SWilliam Wu 					    struct dwc2_qh *qh,
2446af424a41SWilliam Wu 					    struct dwc2_host_chan *chan)
2447af424a41SWilliam Wu {
2448af424a41SWilliam Wu 	if (!hsotg->unaligned_cache ||
2449af424a41SWilliam Wu 	    chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
2450af424a41SWilliam Wu 		return -ENOMEM;
2451af424a41SWilliam Wu 
2452af424a41SWilliam Wu 	if (!qh->dw_align_buf) {
2453af424a41SWilliam Wu 		qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
2454af424a41SWilliam Wu 						    GFP_ATOMIC | GFP_DMA);
2455af424a41SWilliam Wu 		if (!qh->dw_align_buf)
2456af424a41SWilliam Wu 			return -ENOMEM;
2457af424a41SWilliam Wu 	}
2458af424a41SWilliam Wu 
2459af424a41SWilliam Wu 	qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
2460af424a41SWilliam Wu 					      DWC2_KMEM_UNALIGNED_BUF_SIZE,
2461af424a41SWilliam Wu 					      DMA_FROM_DEVICE);
2462af424a41SWilliam Wu 
2463af424a41SWilliam Wu 	if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
2464af424a41SWilliam Wu 		dev_err(hsotg->dev, "can't map align_buf\n");
2465af424a41SWilliam Wu 		chan->align_buf = 0;
2466af424a41SWilliam Wu 		return -EINVAL;
2467af424a41SWilliam Wu 	}
2468af424a41SWilliam Wu 
2469af424a41SWilliam Wu 	chan->align_buf = qh->dw_align_buf_dma;
2470af424a41SWilliam Wu 	return 0;
2471af424a41SWilliam Wu }
2472af424a41SWilliam Wu 
24733bc04e28SDouglas Anderson #define DWC2_USB_DMA_ALIGN 4
24743bc04e28SDouglas Anderson 
24753bc04e28SDouglas Anderson static void dwc2_free_dma_aligned_buffer(struct urb *urb)
2476197ba5f4SPaul Zimmerman {
247756406e01SAntti Seppälä 	void *stored_xfer_buffer;
24781e111e88SAntti Seppälä 	size_t length;
2479197ba5f4SPaul Zimmerman 
24803bc04e28SDouglas Anderson 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
24813bc04e28SDouglas Anderson 		return;
2482197ba5f4SPaul Zimmerman 
248356406e01SAntti Seppälä 	/* Restore urb->transfer_buffer from the end of the allocated area */
24844a4863bfSMartin Schiller 	memcpy(&stored_xfer_buffer,
24854a4863bfSMartin Schiller 	       PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
24864a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
24874a4863bfSMartin Schiller 	       sizeof(urb->transfer_buffer));
24883bc04e28SDouglas Anderson 
24891e111e88SAntti Seppälä 	if (usb_urb_dir_in(urb)) {
24901e111e88SAntti Seppälä 		if (usb_pipeisoc(urb->pipe))
24911e111e88SAntti Seppälä 			length = urb->transfer_buffer_length;
24921e111e88SAntti Seppälä 		else
24931e111e88SAntti Seppälä 			length = urb->actual_length;
24941e111e88SAntti Seppälä 
24951e111e88SAntti Seppälä 		memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
24961e111e88SAntti Seppälä 	}
249756406e01SAntti Seppälä 	kfree(urb->transfer_buffer);
249856406e01SAntti Seppälä 	urb->transfer_buffer = stored_xfer_buffer;
24993bc04e28SDouglas Anderson 
25003bc04e28SDouglas Anderson 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2501197ba5f4SPaul Zimmerman }
2502197ba5f4SPaul Zimmerman 
25033bc04e28SDouglas Anderson static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
25043bc04e28SDouglas Anderson {
250556406e01SAntti Seppälä 	void *kmalloc_ptr;
25063bc04e28SDouglas Anderson 	size_t kmalloc_size;
25075dce9555SPaul Zimmerman 
25083bc04e28SDouglas Anderson 	if (urb->num_sgs || urb->sg ||
25093bc04e28SDouglas Anderson 	    urb->transfer_buffer_length == 0 ||
25103bc04e28SDouglas Anderson 	    !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2511197ba5f4SPaul Zimmerman 		return 0;
25123bc04e28SDouglas Anderson 
251356406e01SAntti Seppälä 	/*
251456406e01SAntti Seppälä 	 * Allocate a buffer with enough padding for original transfer_buffer
251556406e01SAntti Seppälä 	 * pointer. This allocation is guaranteed to be aligned properly for
251656406e01SAntti Seppälä 	 * DMA
251756406e01SAntti Seppälä 	 */
25183bc04e28SDouglas Anderson 	kmalloc_size = urb->transfer_buffer_length +
25194a4863bfSMartin Schiller 		(dma_get_cache_alignment() - 1) +
252056406e01SAntti Seppälä 		sizeof(urb->transfer_buffer);
25213bc04e28SDouglas Anderson 
25223bc04e28SDouglas Anderson 	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
25233bc04e28SDouglas Anderson 	if (!kmalloc_ptr)
25243bc04e28SDouglas Anderson 		return -ENOMEM;
25253bc04e28SDouglas Anderson 
252656406e01SAntti Seppälä 	/*
252756406e01SAntti Seppälä 	 * Position value of original urb->transfer_buffer pointer to the end
252856406e01SAntti Seppälä 	 * of allocation for later referencing
252956406e01SAntti Seppälä 	 */
25304a4863bfSMartin Schiller 	memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
25314a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
253256406e01SAntti Seppälä 	       &urb->transfer_buffer, sizeof(urb->transfer_buffer));
253356406e01SAntti Seppälä 
25343bc04e28SDouglas Anderson 	if (usb_urb_dir_out(urb))
253556406e01SAntti Seppälä 		memcpy(kmalloc_ptr, urb->transfer_buffer,
25363bc04e28SDouglas Anderson 		       urb->transfer_buffer_length);
253756406e01SAntti Seppälä 	urb->transfer_buffer = kmalloc_ptr;
25383bc04e28SDouglas Anderson 
25393bc04e28SDouglas Anderson 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
25403bc04e28SDouglas Anderson 
25413bc04e28SDouglas Anderson 	return 0;
25423bc04e28SDouglas Anderson }
25433bc04e28SDouglas Anderson 
25443bc04e28SDouglas Anderson static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
25453bc04e28SDouglas Anderson 				gfp_t mem_flags)
25463bc04e28SDouglas Anderson {
25473bc04e28SDouglas Anderson 	int ret;
25483bc04e28SDouglas Anderson 
25493bc04e28SDouglas Anderson 	/* We assume setup_dma is always aligned; warn if not */
25503bc04e28SDouglas Anderson 	WARN_ON_ONCE(urb->setup_dma &&
25513bc04e28SDouglas Anderson 		     (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
25523bc04e28SDouglas Anderson 
25533bc04e28SDouglas Anderson 	ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
25543bc04e28SDouglas Anderson 	if (ret)
25553bc04e28SDouglas Anderson 		return ret;
25563bc04e28SDouglas Anderson 
25573bc04e28SDouglas Anderson 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
25583bc04e28SDouglas Anderson 	if (ret)
25593bc04e28SDouglas Anderson 		dwc2_free_dma_aligned_buffer(urb);
25603bc04e28SDouglas Anderson 
25613bc04e28SDouglas Anderson 	return ret;
25623bc04e28SDouglas Anderson }
25633bc04e28SDouglas Anderson 
25643bc04e28SDouglas Anderson static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
25653bc04e28SDouglas Anderson {
25663bc04e28SDouglas Anderson 	usb_hcd_unmap_urb_for_dma(hcd, urb);
25673bc04e28SDouglas Anderson 	dwc2_free_dma_aligned_buffer(urb);
2568197ba5f4SPaul Zimmerman }
2569197ba5f4SPaul Zimmerman 
2570197ba5f4SPaul Zimmerman /**
2571197ba5f4SPaul Zimmerman  * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2572197ba5f4SPaul Zimmerman  * channel and initializes the host channel to perform the transactions. The
2573197ba5f4SPaul Zimmerman  * host channel is removed from the free list.
2574197ba5f4SPaul Zimmerman  *
2575197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2576197ba5f4SPaul Zimmerman  * @qh:    Transactions from the first QTD for this QH are selected and assigned
2577197ba5f4SPaul Zimmerman  *         to a free host channel
2578197ba5f4SPaul Zimmerman  */
2579197ba5f4SPaul Zimmerman static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
2580197ba5f4SPaul Zimmerman {
2581197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
2582197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
2583197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
2584197ba5f4SPaul Zimmerman 
2585197ba5f4SPaul Zimmerman 	if (dbg_qh(qh))
2586197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
2587197ba5f4SPaul Zimmerman 
2588197ba5f4SPaul Zimmerman 	if (list_empty(&qh->qtd_list)) {
2589197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No QTDs in QH list\n");
2590197ba5f4SPaul Zimmerman 		return -ENOMEM;
2591197ba5f4SPaul Zimmerman 	}
2592197ba5f4SPaul Zimmerman 
2593197ba5f4SPaul Zimmerman 	if (list_empty(&hsotg->free_hc_list)) {
2594197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No free channel to assign\n");
2595197ba5f4SPaul Zimmerman 		return -ENOMEM;
2596197ba5f4SPaul Zimmerman 	}
2597197ba5f4SPaul Zimmerman 
2598197ba5f4SPaul Zimmerman 	chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2599197ba5f4SPaul Zimmerman 				hc_list_entry);
2600197ba5f4SPaul Zimmerman 
2601197ba5f4SPaul Zimmerman 	/* Remove host channel from free list */
2602197ba5f4SPaul Zimmerman 	list_del_init(&chan->hc_list_entry);
2603197ba5f4SPaul Zimmerman 
2604197ba5f4SPaul Zimmerman 	qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2605197ba5f4SPaul Zimmerman 	urb = qtd->urb;
2606197ba5f4SPaul Zimmerman 	qh->channel = chan;
2607197ba5f4SPaul Zimmerman 	qtd->in_process = 1;
2608197ba5f4SPaul Zimmerman 
2609197ba5f4SPaul Zimmerman 	/*
2610197ba5f4SPaul Zimmerman 	 * Use usb_pipedevice to determine device address. This address is
2611197ba5f4SPaul Zimmerman 	 * 0 before the SET_ADDRESS command and the correct address afterward.
2612197ba5f4SPaul Zimmerman 	 */
2613197ba5f4SPaul Zimmerman 	chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2614197ba5f4SPaul Zimmerman 	chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2615197ba5f4SPaul Zimmerman 	chan->speed = qh->dev_speed;
2616babd1839SDouglas Anderson 	chan->max_packet = qh->maxp;
2617197ba5f4SPaul Zimmerman 
2618197ba5f4SPaul Zimmerman 	chan->xfer_started = 0;
2619197ba5f4SPaul Zimmerman 	chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2620197ba5f4SPaul Zimmerman 	chan->error_state = (qtd->error_count > 0);
2621197ba5f4SPaul Zimmerman 	chan->halt_on_queue = 0;
2622197ba5f4SPaul Zimmerman 	chan->halt_pending = 0;
2623197ba5f4SPaul Zimmerman 	chan->requests = 0;
2624197ba5f4SPaul Zimmerman 
2625197ba5f4SPaul Zimmerman 	/*
2626197ba5f4SPaul Zimmerman 	 * The following values may be modified in the transfer type section
2627197ba5f4SPaul Zimmerman 	 * below. The xfer_len value may be reduced when the transfer is
2628197ba5f4SPaul Zimmerman 	 * started to accommodate the max widths of the XferSize and PktCnt
2629197ba5f4SPaul Zimmerman 	 * fields in the HCTSIZn register.
2630197ba5f4SPaul Zimmerman 	 */
2631197ba5f4SPaul Zimmerman 
2632197ba5f4SPaul Zimmerman 	chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2633197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
2634197ba5f4SPaul Zimmerman 		chan->do_ping = 0;
2635197ba5f4SPaul Zimmerman 	else
2636197ba5f4SPaul Zimmerman 		chan->do_ping = qh->ping_state;
2637197ba5f4SPaul Zimmerman 
2638197ba5f4SPaul Zimmerman 	chan->data_pid_start = qh->data_toggle;
2639197ba5f4SPaul Zimmerman 	chan->multi_count = 1;
2640197ba5f4SPaul Zimmerman 
2641197ba5f4SPaul Zimmerman 	if (urb->actual_length > urb->length &&
2642197ba5f4SPaul Zimmerman 	    !dwc2_hcd_is_pipe_in(&urb->pipe_info))
2643197ba5f4SPaul Zimmerman 		urb->actual_length = urb->length;
2644197ba5f4SPaul Zimmerman 
264595832c00SJohn Youn 	if (hsotg->params.host_dma)
2646197ba5f4SPaul Zimmerman 		chan->xfer_dma = urb->dma + urb->actual_length;
26473bc04e28SDouglas Anderson 	else
2648197ba5f4SPaul Zimmerman 		chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
2649197ba5f4SPaul Zimmerman 
2650197ba5f4SPaul Zimmerman 	chan->xfer_len = urb->length - urb->actual_length;
2651197ba5f4SPaul Zimmerman 	chan->xfer_count = 0;
2652197ba5f4SPaul Zimmerman 
2653197ba5f4SPaul Zimmerman 	/* Set the split attributes if required */
2654197ba5f4SPaul Zimmerman 	if (qh->do_split)
2655197ba5f4SPaul Zimmerman 		dwc2_hc_init_split(hsotg, chan, qtd, urb);
2656197ba5f4SPaul Zimmerman 	else
2657197ba5f4SPaul Zimmerman 		chan->do_split = 0;
2658197ba5f4SPaul Zimmerman 
2659197ba5f4SPaul Zimmerman 	/* Set the transfer attributes */
26603bc04e28SDouglas Anderson 	dwc2_hc_init_xfer(hsotg, chan, qtd);
2661197ba5f4SPaul Zimmerman 
2662af424a41SWilliam Wu 	/* For non-dword aligned buffers */
2663af424a41SWilliam Wu 	if (hsotg->params.host_dma && qh->do_split &&
2664af424a41SWilliam Wu 	    chan->ep_is_in && (chan->xfer_dma & 0x3)) {
2665af424a41SWilliam Wu 		dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
2666af424a41SWilliam Wu 		if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
2667af424a41SWilliam Wu 			dev_err(hsotg->dev,
2668af424a41SWilliam Wu 				"Failed to allocate memory to handle non-aligned buffer\n");
2669af424a41SWilliam Wu 			/* Add channel back to free list */
2670af424a41SWilliam Wu 			chan->align_buf = 0;
2671af424a41SWilliam Wu 			chan->multi_count = 0;
2672af424a41SWilliam Wu 			list_add_tail(&chan->hc_list_entry,
2673af424a41SWilliam Wu 				      &hsotg->free_hc_list);
2674af424a41SWilliam Wu 			qtd->in_process = 0;
2675af424a41SWilliam Wu 			qh->channel = NULL;
2676af424a41SWilliam Wu 			return -ENOMEM;
2677af424a41SWilliam Wu 		}
2678af424a41SWilliam Wu 	} else {
2679af424a41SWilliam Wu 		/*
2680af424a41SWilliam Wu 		 * We assume that DMA is always aligned in non-split
2681af424a41SWilliam Wu 		 * case or split out case. Warn if not.
2682af424a41SWilliam Wu 		 */
2683af424a41SWilliam Wu 		WARN_ON_ONCE(hsotg->params.host_dma &&
2684af424a41SWilliam Wu 			     (chan->xfer_dma & 0x3));
2685af424a41SWilliam Wu 		chan->align_buf = 0;
2686af424a41SWilliam Wu 	}
2687af424a41SWilliam Wu 
2688197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2689197ba5f4SPaul Zimmerman 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2690197ba5f4SPaul Zimmerman 		/*
2691197ba5f4SPaul Zimmerman 		 * This value may be modified when the transfer is started
2692197ba5f4SPaul Zimmerman 		 * to reflect the actual transfer length
2693197ba5f4SPaul Zimmerman 		 */
2694babd1839SDouglas Anderson 		chan->multi_count = qh->maxp_mult;
2695197ba5f4SPaul Zimmerman 
269695832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2697197ba5f4SPaul Zimmerman 		chan->desc_list_addr = qh->desc_list_dma;
269895105a99SGregory Herrero 		chan->desc_list_sz = qh->desc_list_sz;
269995105a99SGregory Herrero 	}
2700197ba5f4SPaul Zimmerman 
2701197ba5f4SPaul Zimmerman 	dwc2_hc_init(hsotg, chan);
2702197ba5f4SPaul Zimmerman 	chan->qh = qh;
2703197ba5f4SPaul Zimmerman 
2704197ba5f4SPaul Zimmerman 	return 0;
2705197ba5f4SPaul Zimmerman }
2706197ba5f4SPaul Zimmerman 
2707197ba5f4SPaul Zimmerman /**
2708197ba5f4SPaul Zimmerman  * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2709197ba5f4SPaul Zimmerman  * schedule and assigns them to available host channels. Called from the HCD
2710197ba5f4SPaul Zimmerman  * interrupt handler functions.
2711197ba5f4SPaul Zimmerman  *
2712197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2713197ba5f4SPaul Zimmerman  *
2714197ba5f4SPaul Zimmerman  * Return: The types of new transactions that were assigned to host channels
2715197ba5f4SPaul Zimmerman  */
2716197ba5f4SPaul Zimmerman enum dwc2_transaction_type dwc2_hcd_select_transactions(
2717197ba5f4SPaul Zimmerman 		struct dwc2_hsotg *hsotg)
2718197ba5f4SPaul Zimmerman {
2719197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2720197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2721197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2722197ba5f4SPaul Zimmerman 	int num_channels;
2723197ba5f4SPaul Zimmerman 
2724197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
2725197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Select Transactions\n");
2726197ba5f4SPaul Zimmerman #endif
2727197ba5f4SPaul Zimmerman 
2728197ba5f4SPaul Zimmerman 	/* Process entries in the periodic ready list */
2729197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_ready.next;
2730197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_ready) {
2731197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2732197ba5f4SPaul Zimmerman 			break;
273395832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2734197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels <= 1)
2735197ba5f4SPaul Zimmerman 				break;
2736197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2737197ba5f4SPaul Zimmerman 		}
2738197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2739197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2740197ba5f4SPaul Zimmerman 			break;
2741197ba5f4SPaul Zimmerman 
2742197ba5f4SPaul Zimmerman 		/*
2743197ba5f4SPaul Zimmerman 		 * Move the QH from the periodic ready schedule to the
2744197ba5f4SPaul Zimmerman 		 * periodic assigned schedule
2745197ba5f4SPaul Zimmerman 		 */
2746197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
274794ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
274894ef7aeeSDouglas Anderson 			       &hsotg->periodic_sched_assigned);
2749197ba5f4SPaul Zimmerman 		ret_val = DWC2_TRANSACTION_PERIODIC;
2750197ba5f4SPaul Zimmerman 	}
2751197ba5f4SPaul Zimmerman 
2752197ba5f4SPaul Zimmerman 	/*
2753197ba5f4SPaul Zimmerman 	 * Process entries in the inactive portion of the non-periodic
2754197ba5f4SPaul Zimmerman 	 * schedule. Some free host channels may not be used if they are
2755197ba5f4SPaul Zimmerman 	 * reserved for periodic transfers.
2756197ba5f4SPaul Zimmerman 	 */
2757bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2758197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->non_periodic_sched_inactive.next;
2759197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
276095832c00SJohn Youn 		if (!hsotg->params.uframe_sched &&
2761197ba5f4SPaul Zimmerman 		    hsotg->non_periodic_channels >= num_channels -
2762197ba5f4SPaul Zimmerman 						hsotg->periodic_channels)
2763197ba5f4SPaul Zimmerman 			break;
2764197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2765197ba5f4SPaul Zimmerman 			break;
2766197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
276795832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2768197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels < 1)
2769197ba5f4SPaul Zimmerman 				break;
2770197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2771197ba5f4SPaul Zimmerman 		}
2772197ba5f4SPaul Zimmerman 
2773197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2774197ba5f4SPaul Zimmerman 			break;
2775197ba5f4SPaul Zimmerman 
2776197ba5f4SPaul Zimmerman 		/*
2777197ba5f4SPaul Zimmerman 		 * Move the QH from the non-periodic inactive schedule to the
2778197ba5f4SPaul Zimmerman 		 * non-periodic active schedule
2779197ba5f4SPaul Zimmerman 		 */
2780197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
278194ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
2782197ba5f4SPaul Zimmerman 			       &hsotg->non_periodic_sched_active);
2783197ba5f4SPaul Zimmerman 
2784197ba5f4SPaul Zimmerman 		if (ret_val == DWC2_TRANSACTION_NONE)
2785197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2786197ba5f4SPaul Zimmerman 		else
2787197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_ALL;
2788197ba5f4SPaul Zimmerman 
278995832c00SJohn Youn 		if (!hsotg->params.uframe_sched)
2790197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels++;
2791197ba5f4SPaul Zimmerman 	}
2792197ba5f4SPaul Zimmerman 
2793197ba5f4SPaul Zimmerman 	return ret_val;
2794197ba5f4SPaul Zimmerman }
2795197ba5f4SPaul Zimmerman 
2796197ba5f4SPaul Zimmerman /**
2797197ba5f4SPaul Zimmerman  * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2798197ba5f4SPaul Zimmerman  * a host channel associated with either a periodic or non-periodic transfer
2799197ba5f4SPaul Zimmerman  *
2800197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2801197ba5f4SPaul Zimmerman  * @chan:  Host channel descriptor associated with either a periodic or
2802197ba5f4SPaul Zimmerman  *         non-periodic transfer
2803197ba5f4SPaul Zimmerman  * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2804197ba5f4SPaul Zimmerman  *                     for periodic transfers or the non-periodic Tx FIFO
2805197ba5f4SPaul Zimmerman  *                     for non-periodic transfers
2806197ba5f4SPaul Zimmerman  *
2807197ba5f4SPaul Zimmerman  * Return: 1 if a request is queued and more requests may be needed to
2808197ba5f4SPaul Zimmerman  * complete the transfer, 0 if no more requests are required for this
2809197ba5f4SPaul Zimmerman  * transfer, -1 if there is insufficient space in the Tx FIFO
2810197ba5f4SPaul Zimmerman  *
2811197ba5f4SPaul Zimmerman  * This function assumes that there is space available in the appropriate
2812197ba5f4SPaul Zimmerman  * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2813197ba5f4SPaul Zimmerman  * it checks whether space is available in the appropriate Tx FIFO.
2814197ba5f4SPaul Zimmerman  *
2815197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2816197ba5f4SPaul Zimmerman  */
2817197ba5f4SPaul Zimmerman static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2818197ba5f4SPaul Zimmerman 				  struct dwc2_host_chan *chan,
2819197ba5f4SPaul Zimmerman 				  u16 fifo_dwords_avail)
2820197ba5f4SPaul Zimmerman {
2821197ba5f4SPaul Zimmerman 	int retval = 0;
2822197ba5f4SPaul Zimmerman 
2823c9c8ac01SDouglas Anderson 	if (chan->do_split)
2824c9c8ac01SDouglas Anderson 		/* Put ourselves on the list to keep order straight */
2825c9c8ac01SDouglas Anderson 		list_move_tail(&chan->split_order_list_entry,
2826c9c8ac01SDouglas Anderson 			       &hsotg->split_order);
2827c9c8ac01SDouglas Anderson 
28287b813767SAlexandru M Stan 	if (hsotg->params.host_dma && chan->qh) {
282995832c00SJohn Youn 		if (hsotg->params.dma_desc_enable) {
2830197ba5f4SPaul Zimmerman 			if (!chan->xfer_started ||
2831197ba5f4SPaul Zimmerman 			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2832197ba5f4SPaul Zimmerman 				dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2833197ba5f4SPaul Zimmerman 				chan->qh->ping_state = 0;
2834197ba5f4SPaul Zimmerman 			}
2835197ba5f4SPaul Zimmerman 		} else if (!chan->xfer_started) {
2836197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2837197ba5f4SPaul Zimmerman 			chan->qh->ping_state = 0;
2838197ba5f4SPaul Zimmerman 		}
2839197ba5f4SPaul Zimmerman 	} else if (chan->halt_pending) {
2840197ba5f4SPaul Zimmerman 		/* Don't queue a request if the channel has been halted */
2841197ba5f4SPaul Zimmerman 	} else if (chan->halt_on_queue) {
2842197ba5f4SPaul Zimmerman 		dwc2_hc_halt(hsotg, chan, chan->halt_status);
2843197ba5f4SPaul Zimmerman 	} else if (chan->do_ping) {
2844197ba5f4SPaul Zimmerman 		if (!chan->xfer_started)
2845197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2846197ba5f4SPaul Zimmerman 	} else if (!chan->ep_is_in ||
2847197ba5f4SPaul Zimmerman 		   chan->data_pid_start == DWC2_HC_PID_SETUP) {
2848197ba5f4SPaul Zimmerman 		if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2849197ba5f4SPaul Zimmerman 			if (!chan->xfer_started) {
2850197ba5f4SPaul Zimmerman 				dwc2_hc_start_transfer(hsotg, chan);
2851197ba5f4SPaul Zimmerman 				retval = 1;
2852197ba5f4SPaul Zimmerman 			} else {
2853197ba5f4SPaul Zimmerman 				retval = dwc2_hc_continue_transfer(hsotg, chan);
2854197ba5f4SPaul Zimmerman 			}
2855197ba5f4SPaul Zimmerman 		} else {
2856197ba5f4SPaul Zimmerman 			retval = -1;
2857197ba5f4SPaul Zimmerman 		}
2858197ba5f4SPaul Zimmerman 	} else {
2859197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
2860197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2861197ba5f4SPaul Zimmerman 			retval = 1;
2862197ba5f4SPaul Zimmerman 		} else {
2863197ba5f4SPaul Zimmerman 			retval = dwc2_hc_continue_transfer(hsotg, chan);
2864197ba5f4SPaul Zimmerman 		}
2865197ba5f4SPaul Zimmerman 	}
2866197ba5f4SPaul Zimmerman 
2867197ba5f4SPaul Zimmerman 	return retval;
2868197ba5f4SPaul Zimmerman }
2869197ba5f4SPaul Zimmerman 
2870197ba5f4SPaul Zimmerman /*
2871197ba5f4SPaul Zimmerman  * Processes periodic channels for the next frame and queues transactions for
2872197ba5f4SPaul Zimmerman  * these channels to the DWC_otg controller. After queueing transactions, the
2873197ba5f4SPaul Zimmerman  * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2874197ba5f4SPaul Zimmerman  * to queue as Periodic Tx FIFO or request queue space becomes available.
2875197ba5f4SPaul Zimmerman  * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2876197ba5f4SPaul Zimmerman  *
2877197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2878197ba5f4SPaul Zimmerman  */
2879197ba5f4SPaul Zimmerman static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2880197ba5f4SPaul Zimmerman {
2881197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2882197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2883197ba5f4SPaul Zimmerman 	u32 tx_status;
2884197ba5f4SPaul Zimmerman 	u32 fspcavail;
2885197ba5f4SPaul Zimmerman 	u32 gintmsk;
2886197ba5f4SPaul Zimmerman 	int status;
28874e50e011SDouglas Anderson 	bool no_queue_space = false;
28884e50e011SDouglas Anderson 	bool no_fifo_space = false;
2889197ba5f4SPaul Zimmerman 	u32 qspcavail;
2890197ba5f4SPaul Zimmerman 
28914e50e011SDouglas Anderson 	/* If empty list then just adjust interrupt enables */
28924e50e011SDouglas Anderson 	if (list_empty(&hsotg->periodic_sched_assigned))
28934e50e011SDouglas Anderson 		goto exit;
28944e50e011SDouglas Anderson 
2895197ba5f4SPaul Zimmerman 	if (dbg_perio())
2896197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
2897197ba5f4SPaul Zimmerman 
2898f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, HPTXSTS);
2899197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2900197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
2901197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2902197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
2903197ba5f4SPaul Zimmerman 
2904197ba5f4SPaul Zimmerman 	if (dbg_perio()) {
2905197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx Req Queue Space Avail (before queue): %d\n",
2906197ba5f4SPaul Zimmerman 			 qspcavail);
2907197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx FIFO Space Avail (before queue): %d\n",
2908197ba5f4SPaul Zimmerman 			 fspcavail);
2909197ba5f4SPaul Zimmerman 	}
2910197ba5f4SPaul Zimmerman 
2911197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_assigned.next;
2912197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_assigned) {
2913f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, HPTXSTS);
2914197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2915197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
2916197ba5f4SPaul Zimmerman 		if (qspcavail == 0) {
2917fdb09b3eSNicholas Mc Guire 			no_queue_space = true;
2918197ba5f4SPaul Zimmerman 			break;
2919197ba5f4SPaul Zimmerman 		}
2920197ba5f4SPaul Zimmerman 
2921197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2922197ba5f4SPaul Zimmerman 		if (!qh->channel) {
2923197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2924197ba5f4SPaul Zimmerman 			continue;
2925197ba5f4SPaul Zimmerman 		}
2926197ba5f4SPaul Zimmerman 
2927197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
2928197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty) {
2929197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2930197ba5f4SPaul Zimmerman 			continue;
2931197ba5f4SPaul Zimmerman 		}
2932197ba5f4SPaul Zimmerman 
2933197ba5f4SPaul Zimmerman 		/*
2934197ba5f4SPaul Zimmerman 		 * Set a flag if we're queuing high-bandwidth in slave mode.
2935197ba5f4SPaul Zimmerman 		 * The flag prevents any halts to get into the request queue in
2936197ba5f4SPaul Zimmerman 		 * the middle of multiple high-bandwidth packets getting queued.
2937197ba5f4SPaul Zimmerman 		 */
293895832c00SJohn Youn 		if (!hsotg->params.host_dma &&
2939197ba5f4SPaul Zimmerman 		    qh->channel->multi_count > 1)
2940197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 1;
2941197ba5f4SPaul Zimmerman 
2942197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2943197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
2944197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
2945197ba5f4SPaul Zimmerman 		if (status < 0) {
2946fdb09b3eSNicholas Mc Guire 			no_fifo_space = true;
2947197ba5f4SPaul Zimmerman 			break;
2948197ba5f4SPaul Zimmerman 		}
2949197ba5f4SPaul Zimmerman 
2950197ba5f4SPaul Zimmerman 		/*
2951197ba5f4SPaul Zimmerman 		 * In Slave mode, stay on the current transfer until there is
2952197ba5f4SPaul Zimmerman 		 * nothing more to do or the high-bandwidth request count is
2953197ba5f4SPaul Zimmerman 		 * reached. In DMA mode, only need to queue one request. The
2954197ba5f4SPaul Zimmerman 		 * controller automatically handles multiple packets for
2955197ba5f4SPaul Zimmerman 		 * high-bandwidth transfers.
2956197ba5f4SPaul Zimmerman 		 */
295795832c00SJohn Youn 		if (hsotg->params.host_dma || status == 0 ||
2958197ba5f4SPaul Zimmerman 		    qh->channel->requests == qh->channel->multi_count) {
2959197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2960197ba5f4SPaul Zimmerman 			/*
2961197ba5f4SPaul Zimmerman 			 * Move the QH from the periodic assigned schedule to
2962197ba5f4SPaul Zimmerman 			 * the periodic queued schedule
2963197ba5f4SPaul Zimmerman 			 */
296494ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
2965197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_queued);
2966197ba5f4SPaul Zimmerman 
2967197ba5f4SPaul Zimmerman 			/* done queuing high bandwidth */
2968197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 0;
2969197ba5f4SPaul Zimmerman 		}
2970197ba5f4SPaul Zimmerman 	}
2971197ba5f4SPaul Zimmerman 
29724e50e011SDouglas Anderson exit:
29734e50e011SDouglas Anderson 	if (no_queue_space || no_fifo_space ||
297495832c00SJohn Youn 	    (!hsotg->params.host_dma &&
29754e50e011SDouglas Anderson 	     !list_empty(&hsotg->periodic_sched_assigned))) {
2976197ba5f4SPaul Zimmerman 		/*
2977197ba5f4SPaul Zimmerman 		 * May need to queue more transactions as the request
2978197ba5f4SPaul Zimmerman 		 * queue or Tx FIFO empties. Enable the periodic Tx
2979197ba5f4SPaul Zimmerman 		 * FIFO empty interrupt. (Always use the half-empty
2980197ba5f4SPaul Zimmerman 		 * level to ensure that new requests are loaded as
2981197ba5f4SPaul Zimmerman 		 * soon as possible.)
2982197ba5f4SPaul Zimmerman 		 */
2983f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29844e50e011SDouglas Anderson 		if (!(gintmsk & GINTSTS_PTXFEMP)) {
2985197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_PTXFEMP;
2986f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
29874e50e011SDouglas Anderson 		}
2988197ba5f4SPaul Zimmerman 	} else {
2989197ba5f4SPaul Zimmerman 		/*
2990197ba5f4SPaul Zimmerman 		 * Disable the Tx FIFO empty interrupt since there are
2991197ba5f4SPaul Zimmerman 		 * no more transactions that need to be queued right
2992197ba5f4SPaul Zimmerman 		 * now. This function is called from interrupt
2993197ba5f4SPaul Zimmerman 		 * handlers to queue more transactions as transfer
2994197ba5f4SPaul Zimmerman 		 * states change.
2995197ba5f4SPaul Zimmerman 		 */
2996f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29974e50e011SDouglas Anderson 		if (gintmsk & GINTSTS_PTXFEMP) {
2998197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_PTXFEMP;
2999f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3000197ba5f4SPaul Zimmerman 		}
3001197ba5f4SPaul Zimmerman 	}
3002197ba5f4SPaul Zimmerman }
3003197ba5f4SPaul Zimmerman 
3004197ba5f4SPaul Zimmerman /*
3005197ba5f4SPaul Zimmerman  * Processes active non-periodic channels and queues transactions for these
3006197ba5f4SPaul Zimmerman  * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3007197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is enabled if there are more transactions to queue as
3008197ba5f4SPaul Zimmerman  * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3009197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is disabled.
3010197ba5f4SPaul Zimmerman  *
3011197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3012197ba5f4SPaul Zimmerman  */
3013197ba5f4SPaul Zimmerman static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3014197ba5f4SPaul Zimmerman {
3015197ba5f4SPaul Zimmerman 	struct list_head *orig_qh_ptr;
3016197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
3017197ba5f4SPaul Zimmerman 	u32 tx_status;
3018197ba5f4SPaul Zimmerman 	u32 qspcavail;
3019197ba5f4SPaul Zimmerman 	u32 fspcavail;
3020197ba5f4SPaul Zimmerman 	u32 gintmsk;
3021197ba5f4SPaul Zimmerman 	int status;
3022197ba5f4SPaul Zimmerman 	int no_queue_space = 0;
3023197ba5f4SPaul Zimmerman 	int no_fifo_space = 0;
3024197ba5f4SPaul Zimmerman 	int more_to_do = 0;
3025197ba5f4SPaul Zimmerman 
3026197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3027197ba5f4SPaul Zimmerman 
3028f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, GNPTXSTS);
3029197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3030197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
3031197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3032197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
3033197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx Req Queue Space Avail (before queue): %d\n",
3034197ba5f4SPaul Zimmerman 		 qspcavail);
3035197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx FIFO Space Avail (before queue): %d\n",
3036197ba5f4SPaul Zimmerman 		 fspcavail);
3037197ba5f4SPaul Zimmerman 
3038197ba5f4SPaul Zimmerman 	/*
3039197ba5f4SPaul Zimmerman 	 * Keep track of the starting point. Skip over the start-of-list
3040197ba5f4SPaul Zimmerman 	 * entry.
3041197ba5f4SPaul Zimmerman 	 */
3042197ba5f4SPaul Zimmerman 	if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3043197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3044197ba5f4SPaul Zimmerman 	orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3045197ba5f4SPaul Zimmerman 
3046197ba5f4SPaul Zimmerman 	/*
3047197ba5f4SPaul Zimmerman 	 * Process once through the active list or until no more space is
3048197ba5f4SPaul Zimmerman 	 * available in the request queue or the Tx FIFO
3049197ba5f4SPaul Zimmerman 	 */
3050197ba5f4SPaul Zimmerman 	do {
3051f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3052197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3053197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
305495832c00SJohn Youn 		if (!hsotg->params.host_dma && qspcavail == 0) {
3055197ba5f4SPaul Zimmerman 			no_queue_space = 1;
3056197ba5f4SPaul Zimmerman 			break;
3057197ba5f4SPaul Zimmerman 		}
3058197ba5f4SPaul Zimmerman 
3059197ba5f4SPaul Zimmerman 		qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3060197ba5f4SPaul Zimmerman 				qh_list_entry);
3061197ba5f4SPaul Zimmerman 		if (!qh->channel)
3062197ba5f4SPaul Zimmerman 			goto next;
3063197ba5f4SPaul Zimmerman 
3064197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
3065197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty)
3066197ba5f4SPaul Zimmerman 			goto next;
3067197ba5f4SPaul Zimmerman 
3068197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3069197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3070197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3071197ba5f4SPaul Zimmerman 
3072197ba5f4SPaul Zimmerman 		if (status > 0) {
3073197ba5f4SPaul Zimmerman 			more_to_do = 1;
3074197ba5f4SPaul Zimmerman 		} else if (status < 0) {
3075197ba5f4SPaul Zimmerman 			no_fifo_space = 1;
3076197ba5f4SPaul Zimmerman 			break;
3077197ba5f4SPaul Zimmerman 		}
3078197ba5f4SPaul Zimmerman next:
3079197ba5f4SPaul Zimmerman 		/* Advance to next QH, skipping start-of-list entry */
3080197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3081197ba5f4SPaul Zimmerman 		if (hsotg->non_periodic_qh_ptr ==
3082197ba5f4SPaul Zimmerman 				&hsotg->non_periodic_sched_active)
3083197ba5f4SPaul Zimmerman 			hsotg->non_periodic_qh_ptr =
3084197ba5f4SPaul Zimmerman 					hsotg->non_periodic_qh_ptr->next;
3085197ba5f4SPaul Zimmerman 	} while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3086197ba5f4SPaul Zimmerman 
308795832c00SJohn Youn 	if (!hsotg->params.host_dma) {
3088f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3089197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3090197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
3091197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3092197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3093197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3094197ba5f4SPaul Zimmerman 			 "  NP Tx Req Queue Space Avail (after queue): %d\n",
3095197ba5f4SPaul Zimmerman 			 qspcavail);
3096197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3097197ba5f4SPaul Zimmerman 			 "  NP Tx FIFO Space Avail (after queue): %d\n",
3098197ba5f4SPaul Zimmerman 			 fspcavail);
3099197ba5f4SPaul Zimmerman 
3100197ba5f4SPaul Zimmerman 		if (more_to_do || no_queue_space || no_fifo_space) {
3101197ba5f4SPaul Zimmerman 			/*
3102197ba5f4SPaul Zimmerman 			 * May need to queue more transactions as the request
3103197ba5f4SPaul Zimmerman 			 * queue or Tx FIFO empties. Enable the non-periodic
3104197ba5f4SPaul Zimmerman 			 * Tx FIFO empty interrupt. (Always use the half-empty
3105197ba5f4SPaul Zimmerman 			 * level to ensure that new requests are loaded as
3106197ba5f4SPaul Zimmerman 			 * soon as possible.)
3107197ba5f4SPaul Zimmerman 			 */
3108f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3109197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_NPTXFEMP;
3110f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3111197ba5f4SPaul Zimmerman 		} else {
3112197ba5f4SPaul Zimmerman 			/*
3113197ba5f4SPaul Zimmerman 			 * Disable the Tx FIFO empty interrupt since there are
3114197ba5f4SPaul Zimmerman 			 * no more transactions that need to be queued right
3115197ba5f4SPaul Zimmerman 			 * now. This function is called from interrupt
3116197ba5f4SPaul Zimmerman 			 * handlers to queue more transactions as transfer
3117197ba5f4SPaul Zimmerman 			 * states change.
3118197ba5f4SPaul Zimmerman 			 */
3119f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3120197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3121f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3122197ba5f4SPaul Zimmerman 		}
3123197ba5f4SPaul Zimmerman 	}
3124197ba5f4SPaul Zimmerman }
3125197ba5f4SPaul Zimmerman 
3126197ba5f4SPaul Zimmerman /**
3127197ba5f4SPaul Zimmerman  * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3128197ba5f4SPaul Zimmerman  * and queues transactions for these channels to the DWC_otg controller. Called
3129197ba5f4SPaul Zimmerman  * from the HCD interrupt handler functions.
3130197ba5f4SPaul Zimmerman  *
3131197ba5f4SPaul Zimmerman  * @hsotg:   The HCD state structure
3132197ba5f4SPaul Zimmerman  * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3133197ba5f4SPaul Zimmerman  *           or both)
3134197ba5f4SPaul Zimmerman  *
3135197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3136197ba5f4SPaul Zimmerman  */
3137197ba5f4SPaul Zimmerman void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3138197ba5f4SPaul Zimmerman 				 enum dwc2_transaction_type tr_type)
3139197ba5f4SPaul Zimmerman {
3140197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3141197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue Transactions\n");
3142197ba5f4SPaul Zimmerman #endif
3143197ba5f4SPaul Zimmerman 	/* Process host channels associated with periodic transfers */
31444e50e011SDouglas Anderson 	if (tr_type == DWC2_TRANSACTION_PERIODIC ||
31454e50e011SDouglas Anderson 	    tr_type == DWC2_TRANSACTION_ALL)
3146197ba5f4SPaul Zimmerman 		dwc2_process_periodic_channels(hsotg);
3147197ba5f4SPaul Zimmerman 
3148197ba5f4SPaul Zimmerman 	/* Process host channels associated with non-periodic transfers */
3149197ba5f4SPaul Zimmerman 	if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3150197ba5f4SPaul Zimmerman 	    tr_type == DWC2_TRANSACTION_ALL) {
3151197ba5f4SPaul Zimmerman 		if (!list_empty(&hsotg->non_periodic_sched_active)) {
3152197ba5f4SPaul Zimmerman 			dwc2_process_non_periodic_channels(hsotg);
3153197ba5f4SPaul Zimmerman 		} else {
3154197ba5f4SPaul Zimmerman 			/*
3155197ba5f4SPaul Zimmerman 			 * Ensure NP Tx FIFO empty interrupt is disabled when
3156197ba5f4SPaul Zimmerman 			 * there are no non-periodic transfers to process
3157197ba5f4SPaul Zimmerman 			 */
3158f25c42b8SGevorg Sahakyan 			u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
3159197ba5f4SPaul Zimmerman 
3160197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3161f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3162197ba5f4SPaul Zimmerman 		}
3163197ba5f4SPaul Zimmerman 	}
3164197ba5f4SPaul Zimmerman }
3165197ba5f4SPaul Zimmerman 
3166197ba5f4SPaul Zimmerman static void dwc2_conn_id_status_change(struct work_struct *work)
3167197ba5f4SPaul Zimmerman {
3168197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3169197ba5f4SPaul Zimmerman 						wf_otg);
3170197ba5f4SPaul Zimmerman 	u32 count = 0;
3171197ba5f4SPaul Zimmerman 	u32 gotgctl;
31725390d438SMian Yousaf Kaukab 	unsigned long flags;
3173197ba5f4SPaul Zimmerman 
3174197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3175197ba5f4SPaul Zimmerman 
3176f25c42b8SGevorg Sahakyan 	gotgctl = dwc2_readl(hsotg, GOTGCTL);
3177197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3178197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3179197ba5f4SPaul Zimmerman 		!!(gotgctl & GOTGCTL_CONID_B));
3180197ba5f4SPaul Zimmerman 
3181197ba5f4SPaul Zimmerman 	/* B-Device connector (Device Mode) */
3182197ba5f4SPaul Zimmerman 	if (gotgctl & GOTGCTL_CONID_B) {
3183531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
3184197ba5f4SPaul Zimmerman 		/* Wait for switch to device mode */
3185197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId B\n");
31869156a7efSChen Yu 		if (hsotg->bus_suspended) {
31879156a7efSChen Yu 			dev_info(hsotg->dev,
31889156a7efSChen Yu 				 "Do port resume before switching to device mode\n");
31899156a7efSChen Yu 			dwc2_port_resume(hsotg);
31909156a7efSChen Yu 		}
3191197ba5f4SPaul Zimmerman 		while (!dwc2_is_device_mode(hsotg)) {
3192197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev,
3193197ba5f4SPaul Zimmerman 				 "Waiting for Peripheral Mode, Mode=%s\n",
3194197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ? "Host" :
3195197ba5f4SPaul Zimmerman 				 "Peripheral");
319604a9db79SNicholas Mc Guire 			msleep(20);
3197fc30c4bbSJohn Stultz 			/*
3198fc30c4bbSJohn Stultz 			 * Sometimes the initial GOTGCTRL read is wrong, so
3199fc30c4bbSJohn Stultz 			 * check it again and jump to host mode if that was
3200fc30c4bbSJohn Stultz 			 * the case.
3201fc30c4bbSJohn Stultz 			 */
3202f25c42b8SGevorg Sahakyan 			gotgctl = dwc2_readl(hsotg, GOTGCTL);
3203fc30c4bbSJohn Stultz 			if (!(gotgctl & GOTGCTL_CONID_B))
3204fc30c4bbSJohn Stultz 				goto host;
3205197ba5f4SPaul Zimmerman 			if (++count > 250)
3206197ba5f4SPaul Zimmerman 				break;
3207197ba5f4SPaul Zimmerman 		}
3208197ba5f4SPaul Zimmerman 		if (count > 250)
3209197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3210197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3211197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
32120fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3213197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
32145390d438SMian Yousaf Kaukab 		spin_lock_irqsave(&hsotg->lock, flags);
32151f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
32165390d438SMian Yousaf Kaukab 		spin_unlock_irqrestore(&hsotg->lock, flags);
321766e77a24SRazmik Karapetyan 		/* Enable ACG feature in device mode,if supported */
321866e77a24SRazmik Karapetyan 		dwc2_enable_acg(hsotg);
32191f91b4ccSFelipe Balbi 		dwc2_hsotg_core_connect(hsotg);
3220197ba5f4SPaul Zimmerman 	} else {
3221fc30c4bbSJohn Stultz host:
3222197ba5f4SPaul Zimmerman 		/* A-Device connector (Host Mode) */
3223197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId A\n");
3224197ba5f4SPaul Zimmerman 		while (!dwc2_is_host_mode(hsotg)) {
3225197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3226197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ?
3227197ba5f4SPaul Zimmerman 				 "Host" : "Peripheral");
322804a9db79SNicholas Mc Guire 			msleep(20);
3229197ba5f4SPaul Zimmerman 			if (++count > 250)
3230197ba5f4SPaul Zimmerman 				break;
3231197ba5f4SPaul Zimmerman 		}
3232197ba5f4SPaul Zimmerman 		if (count > 250)
3233197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3234197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3235197ba5f4SPaul Zimmerman 
3236d2471d4aSJohn Stultz 		spin_lock_irqsave(&hsotg->lock, flags);
3237d2471d4aSJohn Stultz 		dwc2_hsotg_disconnect(hsotg);
3238d2471d4aSJohn Stultz 		spin_unlock_irqrestore(&hsotg->lock, flags);
3239d2471d4aSJohn Stultz 
3240d2471d4aSJohn Stultz 		hsotg->op_state = OTG_STATE_A_HOST;
3241197ba5f4SPaul Zimmerman 		/* Initialize the Core for Host mode */
32420fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3243197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
3244197ba5f4SPaul Zimmerman 		dwc2_hcd_start(hsotg);
3245197ba5f4SPaul Zimmerman 	}
3246197ba5f4SPaul Zimmerman }
3247197ba5f4SPaul Zimmerman 
3248e99e88a9SKees Cook static void dwc2_wakeup_detected(struct timer_list *t)
3249197ba5f4SPaul Zimmerman {
3250e99e88a9SKees Cook 	struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
3251197ba5f4SPaul Zimmerman 	u32 hprt0;
3252197ba5f4SPaul Zimmerman 
3253197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3254197ba5f4SPaul Zimmerman 
3255197ba5f4SPaul Zimmerman 	/*
3256197ba5f4SPaul Zimmerman 	 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3257197ba5f4SPaul Zimmerman 	 * so that OPT tests pass with all PHYs.)
3258197ba5f4SPaul Zimmerman 	 */
3259197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3260197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3261197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RES;
3262f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
3263197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
3264f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPRT0));
3265197ba5f4SPaul Zimmerman 
3266197ba5f4SPaul Zimmerman 	dwc2_hcd_rem_wakeup(hsotg);
3267fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
3268197ba5f4SPaul Zimmerman 
3269197ba5f4SPaul Zimmerman 	/* Change to L0 state */
3270197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L0;
3271197ba5f4SPaul Zimmerman }
3272197ba5f4SPaul Zimmerman 
3273197ba5f4SPaul Zimmerman static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3274197ba5f4SPaul Zimmerman {
3275197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3276197ba5f4SPaul Zimmerman 
3277197ba5f4SPaul Zimmerman 	return hcd->self.b_hnp_enable;
3278197ba5f4SPaul Zimmerman }
3279197ba5f4SPaul Zimmerman 
3280197ba5f4SPaul Zimmerman /* Must NOT be called with interrupt disabled or spinlock held */
3281197ba5f4SPaul Zimmerman static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3282197ba5f4SPaul Zimmerman {
3283197ba5f4SPaul Zimmerman 	unsigned long flags;
3284197ba5f4SPaul Zimmerman 	u32 hprt0;
3285197ba5f4SPaul Zimmerman 	u32 pcgctl;
3286197ba5f4SPaul Zimmerman 	u32 gotgctl;
3287197ba5f4SPaul Zimmerman 
3288197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3289197ba5f4SPaul Zimmerman 
3290197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
3291197ba5f4SPaul Zimmerman 
3292197ba5f4SPaul Zimmerman 	if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
3293f25c42b8SGevorg Sahakyan 		gotgctl = dwc2_readl(hsotg, GOTGCTL);
3294197ba5f4SPaul Zimmerman 		gotgctl |= GOTGCTL_HSTSETHNPEN;
3295f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gotgctl, GOTGCTL);
3296197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_A_SUSPEND;
3297197ba5f4SPaul Zimmerman 	}
3298197ba5f4SPaul Zimmerman 
3299197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3300197ba5f4SPaul Zimmerman 	hprt0 |= HPRT0_SUSP;
3301f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
3302197ba5f4SPaul Zimmerman 
3303fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = true;
3304197ba5f4SPaul Zimmerman 
3305a2a23d3fSGregory Herrero 	/*
330641ba9b9bSVardan Mikayelyan 	 * If power_down is supported, Phy clock will be suspended
3307a2a23d3fSGregory Herrero 	 * after registers are backuped.
3308a2a23d3fSGregory Herrero 	 */
330941ba9b9bSVardan Mikayelyan 	if (!hsotg->params.power_down) {
3310197ba5f4SPaul Zimmerman 		/* Suspend the Phy Clock */
3311f25c42b8SGevorg Sahakyan 		pcgctl = dwc2_readl(hsotg, PCGCTL);
3312197ba5f4SPaul Zimmerman 		pcgctl |= PCGCTL_STOPPCLK;
3313f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgctl, PCGCTL);
3314197ba5f4SPaul Zimmerman 		udelay(10);
3315a2a23d3fSGregory Herrero 	}
3316197ba5f4SPaul Zimmerman 
3317197ba5f4SPaul Zimmerman 	/* For HNP the bus must be suspended for at least 200ms */
3318197ba5f4SPaul Zimmerman 	if (dwc2_host_is_b_hnp_enabled(hsotg)) {
3319f25c42b8SGevorg Sahakyan 		pcgctl = dwc2_readl(hsotg, PCGCTL);
3320197ba5f4SPaul Zimmerman 		pcgctl &= ~PCGCTL_STOPPCLK;
3321f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgctl, PCGCTL);
3322197ba5f4SPaul Zimmerman 
3323197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3324197ba5f4SPaul Zimmerman 
332504a9db79SNicholas Mc Guire 		msleep(200);
3326197ba5f4SPaul Zimmerman 	} else {
3327197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3328197ba5f4SPaul Zimmerman 	}
3329197ba5f4SPaul Zimmerman }
3330197ba5f4SPaul Zimmerman 
333130db103cSGregory Herrero /* Must NOT be called with interrupt disabled or spinlock held */
333230db103cSGregory Herrero static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
333330db103cSGregory Herrero {
333430db103cSGregory Herrero 	unsigned long flags;
333530db103cSGregory Herrero 	u32 hprt0;
333630db103cSGregory Herrero 	u32 pcgctl;
333730db103cSGregory Herrero 
33384d273c2aSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
33394d273c2aSDouglas Anderson 
3340a2a23d3fSGregory Herrero 	/*
334141ba9b9bSVardan Mikayelyan 	 * If power_down is supported, Phy clock is already resumed
3342a2a23d3fSGregory Herrero 	 * after registers restore.
3343a2a23d3fSGregory Herrero 	 */
334441ba9b9bSVardan Mikayelyan 	if (!hsotg->params.power_down) {
3345f25c42b8SGevorg Sahakyan 		pcgctl = dwc2_readl(hsotg, PCGCTL);
334630db103cSGregory Herrero 		pcgctl &= ~PCGCTL_STOPPCLK;
3347f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgctl, PCGCTL);
33484d273c2aSDouglas Anderson 		spin_unlock_irqrestore(&hsotg->lock, flags);
334904a9db79SNicholas Mc Guire 		msleep(20);
33504d273c2aSDouglas Anderson 		spin_lock_irqsave(&hsotg->lock, flags);
3351a2a23d3fSGregory Herrero 	}
335230db103cSGregory Herrero 
335330db103cSGregory Herrero 	hprt0 = dwc2_read_hprt0(hsotg);
335430db103cSGregory Herrero 	hprt0 |= HPRT0_RES;
335530db103cSGregory Herrero 	hprt0 &= ~HPRT0_SUSP;
3356f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
335730db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
335830db103cSGregory Herrero 
335930db103cSGregory Herrero 	msleep(USB_RESUME_TIMEOUT);
336030db103cSGregory Herrero 
336130db103cSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
336230db103cSGregory Herrero 	hprt0 = dwc2_read_hprt0(hsotg);
336330db103cSGregory Herrero 	hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3364f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
3365fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
336630db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
336730db103cSGregory Herrero }
336830db103cSGregory Herrero 
3369197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
3370197ba5f4SPaul Zimmerman static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3371197ba5f4SPaul Zimmerman 				u16 wvalue, u16 windex, char *buf, u16 wlength)
3372197ba5f4SPaul Zimmerman {
3373197ba5f4SPaul Zimmerman 	struct usb_hub_descriptor *hub_desc;
3374197ba5f4SPaul Zimmerman 	int retval = 0;
3375197ba5f4SPaul Zimmerman 	u32 hprt0;
3376197ba5f4SPaul Zimmerman 	u32 port_status;
3377197ba5f4SPaul Zimmerman 	u32 speed;
3378197ba5f4SPaul Zimmerman 	u32 pcgctl;
3379cd7cd0e6SFabrice Gasnier 	u32 pwr;
3380197ba5f4SPaul Zimmerman 
3381197ba5f4SPaul Zimmerman 	switch (typereq) {
3382197ba5f4SPaul Zimmerman 	case ClearHubFeature:
3383197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3384197ba5f4SPaul Zimmerman 
3385197ba5f4SPaul Zimmerman 		switch (wvalue) {
3386197ba5f4SPaul Zimmerman 		case C_HUB_LOCAL_POWER:
3387197ba5f4SPaul Zimmerman 		case C_HUB_OVER_CURRENT:
3388197ba5f4SPaul Zimmerman 			/* Nothing required here */
3389197ba5f4SPaul Zimmerman 			break;
3390197ba5f4SPaul Zimmerman 
3391197ba5f4SPaul Zimmerman 		default:
3392197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3393197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3394197ba5f4SPaul Zimmerman 				"ClearHubFeature request %1xh unknown\n",
3395197ba5f4SPaul Zimmerman 				wvalue);
3396197ba5f4SPaul Zimmerman 		}
3397197ba5f4SPaul Zimmerman 		break;
3398197ba5f4SPaul Zimmerman 
3399197ba5f4SPaul Zimmerman 	case ClearPortFeature:
3400197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_L1)
3401197ba5f4SPaul Zimmerman 			if (!windex || windex > 1)
3402197ba5f4SPaul Zimmerman 				goto error;
3403197ba5f4SPaul Zimmerman 		switch (wvalue) {
3404197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_ENABLE:
3405197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3406197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3407197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3408197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_ENA;
3409f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3410197ba5f4SPaul Zimmerman 			break;
3411197ba5f4SPaul Zimmerman 
3412197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3413197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3414197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
3415b0bb9bb6SPaul Zimmerman 
3416f260b250SVardan Mikayelyan 			if (hsotg->bus_suspended) {
3417f260b250SVardan Mikayelyan 				if (hsotg->hibernated)
3418f260b250SVardan Mikayelyan 					dwc2_exit_hibernation(hsotg, 0, 0, 1);
3419f260b250SVardan Mikayelyan 				else
342030db103cSGregory Herrero 					dwc2_port_resume(hsotg);
3421f260b250SVardan Mikayelyan 			}
3422197ba5f4SPaul Zimmerman 			break;
3423197ba5f4SPaul Zimmerman 
3424197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3425197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3426197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_POWER\n");
3427197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3428cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3429197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_PWR;
3430f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3431cd7cd0e6SFabrice Gasnier 			if (pwr)
3432cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_exit(hsotg);
3433197ba5f4SPaul Zimmerman 			break;
3434197ba5f4SPaul Zimmerman 
3435197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3436197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3437197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3438197ba5f4SPaul Zimmerman 			/* Port indicator not supported */
3439197ba5f4SPaul Zimmerman 			break;
3440197ba5f4SPaul Zimmerman 
3441197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_CONNECTION:
3442197ba5f4SPaul Zimmerman 			/*
3443197ba5f4SPaul Zimmerman 			 * Clears driver's internal Connect Status Change flag
3444197ba5f4SPaul Zimmerman 			 */
3445197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3446197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3447197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change = 0;
3448197ba5f4SPaul Zimmerman 			break;
3449197ba5f4SPaul Zimmerman 
3450197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_RESET:
3451197ba5f4SPaul Zimmerman 			/* Clears driver's internal Port Reset Change flag */
3452197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3453197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3454197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change = 0;
3455197ba5f4SPaul Zimmerman 			break;
3456197ba5f4SPaul Zimmerman 
3457197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_ENABLE:
3458197ba5f4SPaul Zimmerman 			/*
3459197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Enable/Disable
3460197ba5f4SPaul Zimmerman 			 * Change flag
3461197ba5f4SPaul Zimmerman 			 */
3462197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3463197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3464197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change = 0;
3465197ba5f4SPaul Zimmerman 			break;
3466197ba5f4SPaul Zimmerman 
3467197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_SUSPEND:
3468197ba5f4SPaul Zimmerman 			/*
3469197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Suspend Change
3470197ba5f4SPaul Zimmerman 			 * flag, which is set when resume signaling on the host
3471197ba5f4SPaul Zimmerman 			 * port is complete
3472197ba5f4SPaul Zimmerman 			 */
3473197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3474197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3475197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change = 0;
3476197ba5f4SPaul Zimmerman 			break;
3477197ba5f4SPaul Zimmerman 
3478197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_PORT_L1:
3479197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3480197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3481197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_l1_change = 0;
3482197ba5f4SPaul Zimmerman 			break;
3483197ba5f4SPaul Zimmerman 
3484197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_OVER_CURRENT:
3485197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3486197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3487197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change = 0;
3488197ba5f4SPaul Zimmerman 			break;
3489197ba5f4SPaul Zimmerman 
3490197ba5f4SPaul Zimmerman 		default:
3491197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3492197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3493197ba5f4SPaul Zimmerman 				"ClearPortFeature request %1xh unknown or unsupported\n",
3494197ba5f4SPaul Zimmerman 				wvalue);
3495197ba5f4SPaul Zimmerman 		}
3496197ba5f4SPaul Zimmerman 		break;
3497197ba5f4SPaul Zimmerman 
3498197ba5f4SPaul Zimmerman 	case GetHubDescriptor:
3499197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3500197ba5f4SPaul Zimmerman 		hub_desc = (struct usb_hub_descriptor *)buf;
3501197ba5f4SPaul Zimmerman 		hub_desc->bDescLength = 9;
3502a5dd0395SSergei Shtylyov 		hub_desc->bDescriptorType = USB_DT_HUB;
3503197ba5f4SPaul Zimmerman 		hub_desc->bNbrPorts = 1;
35043d040de8SSergei Shtylyov 		hub_desc->wHubCharacteristics =
35053d040de8SSergei Shtylyov 			cpu_to_le16(HUB_CHAR_COMMON_LPSM |
35063d040de8SSergei Shtylyov 				    HUB_CHAR_INDV_PORT_OCPM);
3507197ba5f4SPaul Zimmerman 		hub_desc->bPwrOn2PwrGood = 1;
3508197ba5f4SPaul Zimmerman 		hub_desc->bHubContrCurrent = 0;
3509197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[0] = 0;
3510197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3511197ba5f4SPaul Zimmerman 		break;
3512197ba5f4SPaul Zimmerman 
3513197ba5f4SPaul Zimmerman 	case GetHubStatus:
3514197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubStatus\n");
3515197ba5f4SPaul Zimmerman 		memset(buf, 0, 4);
3516197ba5f4SPaul Zimmerman 		break;
3517197ba5f4SPaul Zimmerman 
3518197ba5f4SPaul Zimmerman 	case GetPortStatus:
3519197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3520197ba5f4SPaul Zimmerman 			 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3521197ba5f4SPaul Zimmerman 			 hsotg->flags.d32);
3522197ba5f4SPaul Zimmerman 		if (!windex || windex > 1)
3523197ba5f4SPaul Zimmerman 			goto error;
3524197ba5f4SPaul Zimmerman 
3525197ba5f4SPaul Zimmerman 		port_status = 0;
3526197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status_change)
3527197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3528197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_enable_change)
3529197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_ENABLE << 16;
3530197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_suspend_change)
3531197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3532197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_l1_change)
3533197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_L1 << 16;
3534197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_reset_change)
3535197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_RESET << 16;
3536197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_over_current_change) {
3537197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "Overcurrent change detected\n");
3538197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3539197ba5f4SPaul Zimmerman 		}
3540197ba5f4SPaul Zimmerman 
3541197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3542197ba5f4SPaul Zimmerman 			/*
3543197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3544197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3545197ba5f4SPaul Zimmerman 			 * return 0's for the remainder of the port status
3546197ba5f4SPaul Zimmerman 			 * since the port register can't be read if the core
3547197ba5f4SPaul Zimmerman 			 * is in device mode.
3548197ba5f4SPaul Zimmerman 			 */
3549197ba5f4SPaul Zimmerman 			*(__le32 *)buf = cpu_to_le32(port_status);
3550197ba5f4SPaul Zimmerman 			break;
3551197ba5f4SPaul Zimmerman 		}
3552197ba5f4SPaul Zimmerman 
3553f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
3554197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  HPRT0: 0x%08x\n", hprt0);
3555197ba5f4SPaul Zimmerman 
3556197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_CONNSTS)
3557197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_CONNECTION;
3558197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_ENA)
3559197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_ENABLE;
3560197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_SUSP)
3561197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_SUSPEND;
3562197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_OVRCURRACT)
3563197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_OVERCURRENT;
3564197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_RST)
3565197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_RESET;
3566197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_PWR)
3567197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_POWER;
3568197ba5f4SPaul Zimmerman 
3569197ba5f4SPaul Zimmerman 		speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
3570197ba5f4SPaul Zimmerman 		if (speed == HPRT0_SPD_HIGH_SPEED)
3571197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_HIGH_SPEED;
3572197ba5f4SPaul Zimmerman 		else if (speed == HPRT0_SPD_LOW_SPEED)
3573197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_LOW_SPEED;
3574197ba5f4SPaul Zimmerman 
3575197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_TSTCTL_MASK)
3576197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_TEST;
3577197ba5f4SPaul Zimmerman 		/* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3578197ba5f4SPaul Zimmerman 
3579bea8e86cSJohn Youn 		if (hsotg->params.dma_desc_fs_enable) {
3580fbb9e22bSMian Yousaf Kaukab 			/*
3581fbb9e22bSMian Yousaf Kaukab 			 * Enable descriptor DMA only if a full speed
3582fbb9e22bSMian Yousaf Kaukab 			 * device is connected.
3583fbb9e22bSMian Yousaf Kaukab 			 */
3584fbb9e22bSMian Yousaf Kaukab 			if (hsotg->new_connection &&
3585fbb9e22bSMian Yousaf Kaukab 			    ((port_status &
3586fbb9e22bSMian Yousaf Kaukab 			      (USB_PORT_STAT_CONNECTION |
3587fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_HIGH_SPEED |
3588fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_LOW_SPEED)) ==
3589fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_CONNECTION)) {
3590fbb9e22bSMian Yousaf Kaukab 				u32 hcfg;
3591fbb9e22bSMian Yousaf Kaukab 
3592fbb9e22bSMian Yousaf Kaukab 				dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
359395832c00SJohn Youn 				hsotg->params.dma_desc_enable = true;
3594f25c42b8SGevorg Sahakyan 				hcfg = dwc2_readl(hsotg, HCFG);
3595fbb9e22bSMian Yousaf Kaukab 				hcfg |= HCFG_DESCDMA;
3596f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcfg, HCFG);
3597fbb9e22bSMian Yousaf Kaukab 				hsotg->new_connection = false;
3598fbb9e22bSMian Yousaf Kaukab 			}
3599fbb9e22bSMian Yousaf Kaukab 		}
3600fbb9e22bSMian Yousaf Kaukab 
3601197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
3602197ba5f4SPaul Zimmerman 		*(__le32 *)buf = cpu_to_le32(port_status);
3603197ba5f4SPaul Zimmerman 		break;
3604197ba5f4SPaul Zimmerman 
3605197ba5f4SPaul Zimmerman 	case SetHubFeature:
3606197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetHubFeature\n");
3607197ba5f4SPaul Zimmerman 		/* No HUB features supported */
3608197ba5f4SPaul Zimmerman 		break;
3609197ba5f4SPaul Zimmerman 
3610197ba5f4SPaul Zimmerman 	case SetPortFeature:
3611197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetPortFeature\n");
3612197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3613197ba5f4SPaul Zimmerman 			goto error;
3614197ba5f4SPaul Zimmerman 
3615197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3616197ba5f4SPaul Zimmerman 			/*
3617197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3618197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3619197ba5f4SPaul Zimmerman 			 * return without doing anything since the port
3620197ba5f4SPaul Zimmerman 			 * register can't be written if the core is in device
3621197ba5f4SPaul Zimmerman 			 * mode.
3622197ba5f4SPaul Zimmerman 			 */
3623197ba5f4SPaul Zimmerman 			break;
3624197ba5f4SPaul Zimmerman 		}
3625197ba5f4SPaul Zimmerman 
3626197ba5f4SPaul Zimmerman 		switch (wvalue) {
3627197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3628197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3629197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3630197ba5f4SPaul Zimmerman 			if (windex != hsotg->otg_port)
3631197ba5f4SPaul Zimmerman 				goto error;
363207d9878fSJisheng Zhang 			if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_HIBERNATION)
3633f260b250SVardan Mikayelyan 				dwc2_enter_hibernation(hsotg, 1);
3634f260b250SVardan Mikayelyan 			else
3635197ba5f4SPaul Zimmerman 				dwc2_port_suspend(hsotg, windex);
3636197ba5f4SPaul Zimmerman 			break;
3637197ba5f4SPaul Zimmerman 
3638197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3639197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3640197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_POWER\n");
3641197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3642cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3643197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_PWR;
3644f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3645cd7cd0e6SFabrice Gasnier 			if (!pwr)
3646cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_init(hsotg);
3647197ba5f4SPaul Zimmerman 			break;
3648197ba5f4SPaul Zimmerman 
3649197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_RESET:
365007d9878fSJisheng Zhang 			if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_HIBERNATION &&
3651f260b250SVardan Mikayelyan 			    hsotg->hibernated)
3652f260b250SVardan Mikayelyan 				dwc2_exit_hibernation(hsotg, 0, 1, 1);
3653197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3654197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3655197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_RESET\n");
3656f25c42b8SGevorg Sahakyan 			pcgctl = dwc2_readl(hsotg, PCGCTL);
3657197ba5f4SPaul Zimmerman 			pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
3658f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, pcgctl, PCGCTL);
3659197ba5f4SPaul Zimmerman 			/* ??? Original driver does this */
3660f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, PCGCTL);
3661197ba5f4SPaul Zimmerman 
3662197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3663cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3664197ba5f4SPaul Zimmerman 			/* Clear suspend bit if resetting from suspend state */
3665197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_SUSP;
3666197ba5f4SPaul Zimmerman 
3667197ba5f4SPaul Zimmerman 			/*
3668197ba5f4SPaul Zimmerman 			 * When B-Host the Port reset bit is set in the Start
3669197ba5f4SPaul Zimmerman 			 * HCD Callback function, so that the reset is started
3670197ba5f4SPaul Zimmerman 			 * within 1ms of the HNP success interrupt
3671197ba5f4SPaul Zimmerman 			 */
3672197ba5f4SPaul Zimmerman 			if (!dwc2_hcd_is_b_host(hsotg)) {
3673197ba5f4SPaul Zimmerman 				hprt0 |= HPRT0_PWR | HPRT0_RST;
3674197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3675197ba5f4SPaul Zimmerman 					"In host mode, hprt0=%08x\n", hprt0);
3676f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hprt0, HPRT0);
3677cd7cd0e6SFabrice Gasnier 				if (!pwr)
3678cd7cd0e6SFabrice Gasnier 					dwc2_vbus_supply_init(hsotg);
3679197ba5f4SPaul Zimmerman 			}
3680197ba5f4SPaul Zimmerman 
3681197ba5f4SPaul Zimmerman 			/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
368204a9db79SNicholas Mc Guire 			msleep(50);
3683197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_RST;
3684f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3685197ba5f4SPaul Zimmerman 			hsotg->lx_state = DWC2_L0; /* Now back to On state */
3686197ba5f4SPaul Zimmerman 			break;
3687197ba5f4SPaul Zimmerman 
3688197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3689197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3690197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3691197ba5f4SPaul Zimmerman 			/* Not supported */
3692197ba5f4SPaul Zimmerman 			break;
3693197ba5f4SPaul Zimmerman 
369496d480e6SJingwu Lin 		case USB_PORT_FEAT_TEST:
369596d480e6SJingwu Lin 			hprt0 = dwc2_read_hprt0(hsotg);
369696d480e6SJingwu Lin 			dev_dbg(hsotg->dev,
369796d480e6SJingwu Lin 				"SetPortFeature - USB_PORT_FEAT_TEST\n");
369896d480e6SJingwu Lin 			hprt0 &= ~HPRT0_TSTCTL_MASK;
369996d480e6SJingwu Lin 			hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
3700f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
370196d480e6SJingwu Lin 			break;
370296d480e6SJingwu Lin 
3703197ba5f4SPaul Zimmerman 		default:
3704197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3705197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3706197ba5f4SPaul Zimmerman 				"SetPortFeature %1xh unknown or unsupported\n",
3707197ba5f4SPaul Zimmerman 				wvalue);
3708197ba5f4SPaul Zimmerman 			break;
3709197ba5f4SPaul Zimmerman 		}
3710197ba5f4SPaul Zimmerman 		break;
3711197ba5f4SPaul Zimmerman 
3712197ba5f4SPaul Zimmerman 	default:
3713197ba5f4SPaul Zimmerman error:
3714197ba5f4SPaul Zimmerman 		retval = -EINVAL;
3715197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3716197ba5f4SPaul Zimmerman 			"Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3717197ba5f4SPaul Zimmerman 			typereq, windex, wvalue);
3718197ba5f4SPaul Zimmerman 		break;
3719197ba5f4SPaul Zimmerman 	}
3720197ba5f4SPaul Zimmerman 
3721197ba5f4SPaul Zimmerman 	return retval;
3722197ba5f4SPaul Zimmerman }
3723197ba5f4SPaul Zimmerman 
3724197ba5f4SPaul Zimmerman static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3725197ba5f4SPaul Zimmerman {
3726197ba5f4SPaul Zimmerman 	int retval;
3727197ba5f4SPaul Zimmerman 
3728197ba5f4SPaul Zimmerman 	if (port != 1)
3729197ba5f4SPaul Zimmerman 		return -EINVAL;
3730197ba5f4SPaul Zimmerman 
3731197ba5f4SPaul Zimmerman 	retval = (hsotg->flags.b.port_connect_status_change ||
3732197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_reset_change ||
3733197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_enable_change ||
3734197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_suspend_change ||
3735197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_over_current_change);
3736197ba5f4SPaul Zimmerman 
3737197ba5f4SPaul Zimmerman 	if (retval) {
3738197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3739197ba5f4SPaul Zimmerman 			"DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3740197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_connect_status_change: %d\n",
3741197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change);
3742197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_reset_change: %d\n",
3743197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change);
3744197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_enable_change: %d\n",
3745197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change);
3746197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_suspend_change: %d\n",
3747197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change);
3748197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_over_current_change: %d\n",
3749197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change);
3750197ba5f4SPaul Zimmerman 	}
3751197ba5f4SPaul Zimmerman 
3752197ba5f4SPaul Zimmerman 	return retval;
3753197ba5f4SPaul Zimmerman }
3754197ba5f4SPaul Zimmerman 
3755197ba5f4SPaul Zimmerman int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3756197ba5f4SPaul Zimmerman {
3757f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3758197ba5f4SPaul Zimmerman 
3759197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3760197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
3761197ba5f4SPaul Zimmerman 		 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
3762197ba5f4SPaul Zimmerman #endif
3763197ba5f4SPaul Zimmerman 	return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3764197ba5f4SPaul Zimmerman }
3765197ba5f4SPaul Zimmerman 
3766fae4e826SDouglas Anderson int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3767fae4e826SDouglas Anderson {
3768f25c42b8SGevorg Sahakyan 	u32 hprt = dwc2_readl(hsotg, HPRT0);
3769f25c42b8SGevorg Sahakyan 	u32 hfir = dwc2_readl(hsotg, HFIR);
3770f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3771fae4e826SDouglas Anderson 	unsigned int us_per_frame;
3772fae4e826SDouglas Anderson 	unsigned int frame_number;
3773fae4e826SDouglas Anderson 	unsigned int remaining;
3774fae4e826SDouglas Anderson 	unsigned int interval;
3775fae4e826SDouglas Anderson 	unsigned int phy_clks;
3776fae4e826SDouglas Anderson 
3777fae4e826SDouglas Anderson 	/* High speed has 125 us per (micro) frame; others are 1 ms per */
3778fae4e826SDouglas Anderson 	us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3779fae4e826SDouglas Anderson 
3780fae4e826SDouglas Anderson 	/* Extract fields */
3781fae4e826SDouglas Anderson 	frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3782fae4e826SDouglas Anderson 	remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3783fae4e826SDouglas Anderson 	interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3784fae4e826SDouglas Anderson 
3785fae4e826SDouglas Anderson 	/*
3786fae4e826SDouglas Anderson 	 * Number of phy clocks since the last tick of the frame number after
3787fae4e826SDouglas Anderson 	 * "us" has passed.
3788fae4e826SDouglas Anderson 	 */
3789fae4e826SDouglas Anderson 	phy_clks = (interval - remaining) +
3790fae4e826SDouglas Anderson 		   DIV_ROUND_UP(interval * us, us_per_frame);
3791fae4e826SDouglas Anderson 
3792fae4e826SDouglas Anderson 	return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3793fae4e826SDouglas Anderson }
3794fae4e826SDouglas Anderson 
3795197ba5f4SPaul Zimmerman int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3796197ba5f4SPaul Zimmerman {
3797197ba5f4SPaul Zimmerman 	return hsotg->op_state == OTG_STATE_B_HOST;
3798197ba5f4SPaul Zimmerman }
3799197ba5f4SPaul Zimmerman 
3800197ba5f4SPaul Zimmerman static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3801197ba5f4SPaul Zimmerman 					       int iso_desc_count,
3802197ba5f4SPaul Zimmerman 					       gfp_t mem_flags)
3803197ba5f4SPaul Zimmerman {
3804197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3805197ba5f4SPaul Zimmerman 
3806eeca7606SGustavo A. R. Silva 	urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
3807197ba5f4SPaul Zimmerman 	if (urb)
3808197ba5f4SPaul Zimmerman 		urb->packet_count = iso_desc_count;
3809197ba5f4SPaul Zimmerman 	return urb;
3810197ba5f4SPaul Zimmerman }
3811197ba5f4SPaul Zimmerman 
3812197ba5f4SPaul Zimmerman static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3813197ba5f4SPaul Zimmerman 				      struct dwc2_hcd_urb *urb, u8 dev_addr,
3814babd1839SDouglas Anderson 				      u8 ep_num, u8 ep_type, u8 ep_dir,
3815babd1839SDouglas Anderson 				      u16 maxp, u16 maxp_mult)
3816197ba5f4SPaul Zimmerman {
3817197ba5f4SPaul Zimmerman 	if (dbg_perio() ||
3818197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_BULK ||
3819197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_CONTROL)
3820197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3821babd1839SDouglas Anderson 			 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
3822babd1839SDouglas Anderson 			 dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
3823197ba5f4SPaul Zimmerman 	urb->pipe_info.dev_addr = dev_addr;
3824197ba5f4SPaul Zimmerman 	urb->pipe_info.ep_num = ep_num;
3825197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_type = ep_type;
3826197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_dir = ep_dir;
3827babd1839SDouglas Anderson 	urb->pipe_info.maxp = maxp;
3828babd1839SDouglas Anderson 	urb->pipe_info.maxp_mult = maxp_mult;
3829197ba5f4SPaul Zimmerman }
3830197ba5f4SPaul Zimmerman 
3831197ba5f4SPaul Zimmerman /*
3832197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
3833197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
3834197ba5f4SPaul Zimmerman  */
3835197ba5f4SPaul Zimmerman void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3836197ba5f4SPaul Zimmerman {
3837197ba5f4SPaul Zimmerman #ifdef DEBUG
3838197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
3839197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3840197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
3841197ba5f4SPaul Zimmerman 	int num_channels;
3842197ba5f4SPaul Zimmerman 	u32 np_tx_status;
3843197ba5f4SPaul Zimmerman 	u32 p_tx_status;
3844197ba5f4SPaul Zimmerman 	int i;
3845197ba5f4SPaul Zimmerman 
3846bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
3847197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3848197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3849197ba5f4SPaul Zimmerman 		"************************************************************\n");
3850197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HCD State:\n");
3851197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Num channels: %d\n", num_channels);
3852197ba5f4SPaul Zimmerman 
3853197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
3854197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
3855197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  Channel %d:\n", i);
3856197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3857197ba5f4SPaul Zimmerman 			"    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3858197ba5f4SPaul Zimmerman 			chan->dev_addr, chan->ep_num, chan->ep_is_in);
3859197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    speed: %d\n", chan->speed);
3860197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
3861197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
3862197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    data_pid_start: %d\n",
3863197ba5f4SPaul Zimmerman 			chan->data_pid_start);
3864197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    multi_count: %d\n", chan->multi_count);
3865197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_started: %d\n",
3866197ba5f4SPaul Zimmerman 			chan->xfer_started);
3867197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
3868197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
3869197ba5f4SPaul Zimmerman 			(unsigned long)chan->xfer_dma);
3870197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
3871197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_count: %d\n", chan->xfer_count);
3872197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_on_queue: %d\n",
3873197ba5f4SPaul Zimmerman 			chan->halt_on_queue);
3874197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_pending: %d\n",
3875197ba5f4SPaul Zimmerman 			chan->halt_pending);
3876197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
3877197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    do_split: %d\n", chan->do_split);
3878197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    complete_split: %d\n",
3879197ba5f4SPaul Zimmerman 			chan->complete_split);
3880197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_addr: %d\n", chan->hub_addr);
3881197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_port: %d\n", chan->hub_port);
3882197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xact_pos: %d\n", chan->xact_pos);
3883197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    requests: %d\n", chan->requests);
3884197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
3885197ba5f4SPaul Zimmerman 
3886197ba5f4SPaul Zimmerman 		if (chan->xfer_started) {
3887197ba5f4SPaul Zimmerman 			u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3888197ba5f4SPaul Zimmerman 
3889f25c42b8SGevorg Sahakyan 			hfnum = dwc2_readl(hsotg, HFNUM);
3890f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
3891f25c42b8SGevorg Sahakyan 			hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
3892f25c42b8SGevorg Sahakyan 			hcint = dwc2_readl(hsotg, HCINT(i));
3893f25c42b8SGevorg Sahakyan 			hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
3894197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hfnum: 0x%08x\n", hfnum);
3895197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcchar: 0x%08x\n", hcchar);
3896197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hctsiz: 0x%08x\n", hctsiz);
3897197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcint: 0x%08x\n", hcint);
3898197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcintmsk: 0x%08x\n", hcintmsk);
3899197ba5f4SPaul Zimmerman 		}
3900197ba5f4SPaul Zimmerman 
3901197ba5f4SPaul Zimmerman 		if (!(chan->xfer_started && chan->qh))
3902197ba5f4SPaul Zimmerman 			continue;
3903197ba5f4SPaul Zimmerman 
3904197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3905197ba5f4SPaul Zimmerman 			if (!qtd->in_process)
3906197ba5f4SPaul Zimmerman 				break;
3907197ba5f4SPaul Zimmerman 			urb = qtd->urb;
3908197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    URB Info:\n");
3909197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "      qtd: %p, urb: %p\n",
3910197ba5f4SPaul Zimmerman 				qtd, urb);
3911197ba5f4SPaul Zimmerman 			if (urb) {
3912197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3913197ba5f4SPaul Zimmerman 					"      Dev: %d, EP: %d %s\n",
3914197ba5f4SPaul Zimmerman 					dwc2_hcd_get_dev_addr(&urb->pipe_info),
3915197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_num(&urb->pipe_info),
3916197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3917197ba5f4SPaul Zimmerman 					"IN" : "OUT");
3918197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3919babd1839SDouglas Anderson 					"      Max packet size: %d (%d mult)\n",
3920babd1839SDouglas Anderson 					dwc2_hcd_get_maxp(&urb->pipe_info),
3921babd1839SDouglas Anderson 					dwc2_hcd_get_maxp_mult(&urb->pipe_info));
3922197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3923197ba5f4SPaul Zimmerman 					"      transfer_buffer: %p\n",
3924197ba5f4SPaul Zimmerman 					urb->buf);
3925197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3926197ba5f4SPaul Zimmerman 					"      transfer_dma: %08lx\n",
3927197ba5f4SPaul Zimmerman 					(unsigned long)urb->dma);
3928197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3929197ba5f4SPaul Zimmerman 					"      transfer_buffer_length: %d\n",
3930197ba5f4SPaul Zimmerman 					urb->length);
3931197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev, "      actual_length: %d\n",
3932197ba5f4SPaul Zimmerman 					urb->actual_length);
3933197ba5f4SPaul Zimmerman 			}
3934197ba5f4SPaul Zimmerman 		}
3935197ba5f4SPaul Zimmerman 	}
3936197ba5f4SPaul Zimmerman 
3937197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  non_periodic_channels: %d\n",
3938197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels);
3939197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_channels: %d\n",
3940197ba5f4SPaul Zimmerman 		hsotg->periodic_channels);
3941197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_usecs: %d\n", hsotg->periodic_usecs);
3942f25c42b8SGevorg Sahakyan 	np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
3943197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx Req Queue Space Avail: %d\n",
3944197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3945197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx FIFO Space Avail: %d\n",
3946197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3947f25c42b8SGevorg Sahakyan 	p_tx_status = dwc2_readl(hsotg, HPTXSTS);
3948197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx Req Queue Space Avail: %d\n",
3949197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3950197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx FIFO Space Avail: %d\n",
3951197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3952197ba5f4SPaul Zimmerman 	dwc2_dump_global_registers(hsotg);
3953197ba5f4SPaul Zimmerman 	dwc2_dump_host_registers(hsotg);
3954197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3955197ba5f4SPaul Zimmerman 		"************************************************************\n");
3956197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3957197ba5f4SPaul Zimmerman #endif
3958197ba5f4SPaul Zimmerman }
3959197ba5f4SPaul Zimmerman 
3960197ba5f4SPaul Zimmerman struct wrapper_priv_data {
3961197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg;
3962197ba5f4SPaul Zimmerman };
3963197ba5f4SPaul Zimmerman 
3964197ba5f4SPaul Zimmerman /* Gets the dwc2_hsotg from a usb_hcd */
3965197ba5f4SPaul Zimmerman static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
3966197ba5f4SPaul Zimmerman {
3967197ba5f4SPaul Zimmerman 	struct wrapper_priv_data *p;
3968197ba5f4SPaul Zimmerman 
3969197ba5f4SPaul Zimmerman 	p = (struct wrapper_priv_data *)&hcd->hcd_priv;
3970197ba5f4SPaul Zimmerman 	return p->hsotg;
3971197ba5f4SPaul Zimmerman }
3972197ba5f4SPaul Zimmerman 
39739f9f09b0SDouglas Anderson /**
39749f9f09b0SDouglas Anderson  * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
39759f9f09b0SDouglas Anderson  *
39769f9f09b0SDouglas Anderson  * This will get the dwc2_tt structure (and ttport) associated with the given
39779f9f09b0SDouglas Anderson  * context (which is really just a struct urb pointer).
39789f9f09b0SDouglas Anderson  *
39799f9f09b0SDouglas Anderson  * The first time this is called for a given TT we allocate memory for our
39809f9f09b0SDouglas Anderson  * structure.  When everyone is done and has called dwc2_host_put_tt_info()
39819f9f09b0SDouglas Anderson  * then the refcount for the structure will go to 0 and we'll free it.
39829f9f09b0SDouglas Anderson  *
39839f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
39849f9f09b0SDouglas Anderson  * @context:   The priv pointer from a struct dwc2_hcd_urb.
39859f9f09b0SDouglas Anderson  * @mem_flags: Flags for allocating memory.
39869f9f09b0SDouglas Anderson  * @ttport:    We'll return this device's port number here.  That's used to
39879f9f09b0SDouglas Anderson  *             reference into the bitmap if we're on a multi_tt hub.
39889f9f09b0SDouglas Anderson  *
39899f9f09b0SDouglas Anderson  * Return: a pointer to a struct dwc2_tt.  Don't forget to call
39909f9f09b0SDouglas Anderson  *         dwc2_host_put_tt_info()!  Returns NULL upon memory alloc failure.
39919f9f09b0SDouglas Anderson  */
39929f9f09b0SDouglas Anderson 
39939f9f09b0SDouglas Anderson struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
39949f9f09b0SDouglas Anderson 				      gfp_t mem_flags, int *ttport)
39959f9f09b0SDouglas Anderson {
39969f9f09b0SDouglas Anderson 	struct urb *urb = context;
39979f9f09b0SDouglas Anderson 	struct dwc2_tt *dwc_tt = NULL;
39989f9f09b0SDouglas Anderson 
39999f9f09b0SDouglas Anderson 	if (urb->dev->tt) {
40009f9f09b0SDouglas Anderson 		*ttport = urb->dev->ttport;
40019f9f09b0SDouglas Anderson 
40029f9f09b0SDouglas Anderson 		dwc_tt = urb->dev->tt->hcpriv;
40039da51974SJohn Youn 		if (!dwc_tt) {
40049f9f09b0SDouglas Anderson 			size_t bitmap_size;
40059f9f09b0SDouglas Anderson 
40069f9f09b0SDouglas Anderson 			/*
40079f9f09b0SDouglas Anderson 			 * For single_tt we need one schedule.  For multi_tt
40089f9f09b0SDouglas Anderson 			 * we need one per port.
40099f9f09b0SDouglas Anderson 			 */
40109f9f09b0SDouglas Anderson 			bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
40119f9f09b0SDouglas Anderson 				      sizeof(dwc_tt->periodic_bitmaps[0]);
40129f9f09b0SDouglas Anderson 			if (urb->dev->tt->multi)
40139f9f09b0SDouglas Anderson 				bitmap_size *= urb->dev->tt->hub->maxchild;
40149f9f09b0SDouglas Anderson 
40159f9f09b0SDouglas Anderson 			dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
40169f9f09b0SDouglas Anderson 					 mem_flags);
40179da51974SJohn Youn 			if (!dwc_tt)
40189f9f09b0SDouglas Anderson 				return NULL;
40199f9f09b0SDouglas Anderson 
40209f9f09b0SDouglas Anderson 			dwc_tt->usb_tt = urb->dev->tt;
40219f9f09b0SDouglas Anderson 			dwc_tt->usb_tt->hcpriv = dwc_tt;
40229f9f09b0SDouglas Anderson 		}
40239f9f09b0SDouglas Anderson 
40249f9f09b0SDouglas Anderson 		dwc_tt->refcount++;
40259f9f09b0SDouglas Anderson 	}
40269f9f09b0SDouglas Anderson 
40279f9f09b0SDouglas Anderson 	return dwc_tt;
40289f9f09b0SDouglas Anderson }
40299f9f09b0SDouglas Anderson 
40309f9f09b0SDouglas Anderson /**
40319f9f09b0SDouglas Anderson  * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
40329f9f09b0SDouglas Anderson  *
40339f9f09b0SDouglas Anderson  * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
40349f9f09b0SDouglas Anderson  * of the structure are done.
40359f9f09b0SDouglas Anderson  *
40369f9f09b0SDouglas Anderson  * It's OK to call this with NULL.
40379f9f09b0SDouglas Anderson  *
40389f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40399f9f09b0SDouglas Anderson  * @dwc_tt:    The pointer returned by dwc2_host_get_tt_info.
40409f9f09b0SDouglas Anderson  */
40419f9f09b0SDouglas Anderson void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
40429f9f09b0SDouglas Anderson {
40439f9f09b0SDouglas Anderson 	/* Model kfree and make put of NULL a no-op */
40449da51974SJohn Youn 	if (!dwc_tt)
40459f9f09b0SDouglas Anderson 		return;
40469f9f09b0SDouglas Anderson 
40479f9f09b0SDouglas Anderson 	WARN_ON(dwc_tt->refcount < 1);
40489f9f09b0SDouglas Anderson 
40499f9f09b0SDouglas Anderson 	dwc_tt->refcount--;
40509f9f09b0SDouglas Anderson 	if (!dwc_tt->refcount) {
40519f9f09b0SDouglas Anderson 		dwc_tt->usb_tt->hcpriv = NULL;
40529f9f09b0SDouglas Anderson 		kfree(dwc_tt);
40539f9f09b0SDouglas Anderson 	}
40549f9f09b0SDouglas Anderson }
40559f9f09b0SDouglas Anderson 
4056197ba5f4SPaul Zimmerman int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4057197ba5f4SPaul Zimmerman {
4058197ba5f4SPaul Zimmerman 	struct urb *urb = context;
4059197ba5f4SPaul Zimmerman 
4060197ba5f4SPaul Zimmerman 	return urb->dev->speed;
4061197ba5f4SPaul Zimmerman }
4062197ba5f4SPaul Zimmerman 
4063197ba5f4SPaul Zimmerman static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4064197ba5f4SPaul Zimmerman 					struct urb *urb)
4065197ba5f4SPaul Zimmerman {
4066197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4067197ba5f4SPaul Zimmerman 
4068197ba5f4SPaul Zimmerman 	if (urb->interval)
4069197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated += bw / urb->interval;
4070197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4071197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs++;
4072197ba5f4SPaul Zimmerman 	else
4073197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs++;
4074197ba5f4SPaul Zimmerman }
4075197ba5f4SPaul Zimmerman 
4076197ba5f4SPaul Zimmerman static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4077197ba5f4SPaul Zimmerman 				    struct urb *urb)
4078197ba5f4SPaul Zimmerman {
4079197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4080197ba5f4SPaul Zimmerman 
4081197ba5f4SPaul Zimmerman 	if (urb->interval)
4082197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated -= bw / urb->interval;
4083197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4084197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs--;
4085197ba5f4SPaul Zimmerman 	else
4086197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs--;
4087197ba5f4SPaul Zimmerman }
4088197ba5f4SPaul Zimmerman 
4089197ba5f4SPaul Zimmerman /*
4090197ba5f4SPaul Zimmerman  * Sets the final status of an URB and returns it to the upper layer. Any
4091197ba5f4SPaul Zimmerman  * required cleanup of the URB is performed.
4092197ba5f4SPaul Zimmerman  *
4093197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
4094197ba5f4SPaul Zimmerman  */
4095197ba5f4SPaul Zimmerman void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4096197ba5f4SPaul Zimmerman 			int status)
4097197ba5f4SPaul Zimmerman {
4098197ba5f4SPaul Zimmerman 	struct urb *urb;
4099197ba5f4SPaul Zimmerman 	int i;
4100197ba5f4SPaul Zimmerman 
4101197ba5f4SPaul Zimmerman 	if (!qtd) {
4102197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4103197ba5f4SPaul Zimmerman 		return;
4104197ba5f4SPaul Zimmerman 	}
4105197ba5f4SPaul Zimmerman 
4106197ba5f4SPaul Zimmerman 	if (!qtd->urb) {
4107197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4108197ba5f4SPaul Zimmerman 		return;
4109197ba5f4SPaul Zimmerman 	}
4110197ba5f4SPaul Zimmerman 
4111197ba5f4SPaul Zimmerman 	urb = qtd->urb->priv;
4112197ba5f4SPaul Zimmerman 	if (!urb) {
4113197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4114197ba5f4SPaul Zimmerman 		return;
4115197ba5f4SPaul Zimmerman 	}
4116197ba5f4SPaul Zimmerman 
4117197ba5f4SPaul Zimmerman 	urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
4118197ba5f4SPaul Zimmerman 
4119197ba5f4SPaul Zimmerman 	if (dbg_urb(urb))
4120197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
4121197ba5f4SPaul Zimmerman 			 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4122197ba5f4SPaul Zimmerman 			 __func__, urb, usb_pipedevice(urb->pipe),
4123197ba5f4SPaul Zimmerman 			 usb_pipeendpoint(urb->pipe),
4124197ba5f4SPaul Zimmerman 			 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4125197ba5f4SPaul Zimmerman 			 urb->actual_length);
4126197ba5f4SPaul Zimmerman 
4127197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4128197ba5f4SPaul Zimmerman 		urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
4129197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; ++i) {
4130197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].actual_length =
4131197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_actual_length(
4132197ba5f4SPaul Zimmerman 						qtd->urb, i);
4133197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].status =
4134197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
4135197ba5f4SPaul Zimmerman 		}
4136197ba5f4SPaul Zimmerman 	}
4137197ba5f4SPaul Zimmerman 
4138fe9b1773SGregory Herrero 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4139fe9b1773SGregory Herrero 		for (i = 0; i < urb->number_of_packets; i++)
4140fe9b1773SGregory Herrero 			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4141fe9b1773SGregory Herrero 				 i, urb->iso_frame_desc[i].status);
4142fe9b1773SGregory Herrero 	}
4143fe9b1773SGregory Herrero 
4144197ba5f4SPaul Zimmerman 	urb->status = status;
4145197ba5f4SPaul Zimmerman 	if (!status) {
4146197ba5f4SPaul Zimmerman 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4147197ba5f4SPaul Zimmerman 		    urb->actual_length < urb->transfer_buffer_length)
4148197ba5f4SPaul Zimmerman 			urb->status = -EREMOTEIO;
4149197ba5f4SPaul Zimmerman 	}
4150197ba5f4SPaul Zimmerman 
4151197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4152197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4153197ba5f4SPaul Zimmerman 		struct usb_host_endpoint *ep = urb->ep;
4154197ba5f4SPaul Zimmerman 
4155197ba5f4SPaul Zimmerman 		if (ep)
4156197ba5f4SPaul Zimmerman 			dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4157197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4158197ba5f4SPaul Zimmerman 					urb);
4159197ba5f4SPaul Zimmerman 	}
4160197ba5f4SPaul Zimmerman 
4161197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
4162197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4163197ba5f4SPaul Zimmerman 	kfree(qtd->urb);
4164197ba5f4SPaul Zimmerman 	qtd->urb = NULL;
4165197ba5f4SPaul Zimmerman 
4166197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
4167197ba5f4SPaul Zimmerman }
4168197ba5f4SPaul Zimmerman 
4169197ba5f4SPaul Zimmerman /*
4170197ba5f4SPaul Zimmerman  * Work queue function for starting the HCD when A-Cable is connected
4171197ba5f4SPaul Zimmerman  */
4172197ba5f4SPaul Zimmerman static void dwc2_hcd_start_func(struct work_struct *work)
4173197ba5f4SPaul Zimmerman {
4174197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4175197ba5f4SPaul Zimmerman 						start_work.work);
4176197ba5f4SPaul Zimmerman 
4177197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4178197ba5f4SPaul Zimmerman 	dwc2_host_start(hsotg);
4179197ba5f4SPaul Zimmerman }
4180197ba5f4SPaul Zimmerman 
4181197ba5f4SPaul Zimmerman /*
4182197ba5f4SPaul Zimmerman  * Reset work queue function
4183197ba5f4SPaul Zimmerman  */
4184197ba5f4SPaul Zimmerman static void dwc2_hcd_reset_func(struct work_struct *work)
4185197ba5f4SPaul Zimmerman {
4186197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4187197ba5f4SPaul Zimmerman 						reset_work.work);
41884a065c7bSDouglas Anderson 	unsigned long flags;
4189197ba5f4SPaul Zimmerman 	u32 hprt0;
4190197ba5f4SPaul Zimmerman 
4191197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "USB RESET function called\n");
41924a065c7bSDouglas Anderson 
41934a065c7bSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
41944a065c7bSDouglas Anderson 
4195197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
4196197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RST;
4197f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
4198197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_reset_change = 1;
41994a065c7bSDouglas Anderson 
42004a065c7bSDouglas Anderson 	spin_unlock_irqrestore(&hsotg->lock, flags);
4201197ba5f4SPaul Zimmerman }
4202197ba5f4SPaul Zimmerman 
4203c40cf770SDouglas Anderson static void dwc2_hcd_phy_reset_func(struct work_struct *work)
4204c40cf770SDouglas Anderson {
4205c40cf770SDouglas Anderson 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4206c40cf770SDouglas Anderson 						phy_reset_work);
4207c40cf770SDouglas Anderson 	int ret;
4208c40cf770SDouglas Anderson 
4209c40cf770SDouglas Anderson 	ret = phy_reset(hsotg->phy);
4210c40cf770SDouglas Anderson 	if (ret)
4211c40cf770SDouglas Anderson 		dev_warn(hsotg->dev, "PHY reset failed\n");
4212c40cf770SDouglas Anderson }
4213c40cf770SDouglas Anderson 
4214197ba5f4SPaul Zimmerman /*
4215197ba5f4SPaul Zimmerman  * =========================================================================
4216197ba5f4SPaul Zimmerman  *  Linux HC Driver Functions
4217197ba5f4SPaul Zimmerman  * =========================================================================
4218197ba5f4SPaul Zimmerman  */
4219197ba5f4SPaul Zimmerman 
4220197ba5f4SPaul Zimmerman /*
4221197ba5f4SPaul Zimmerman  * Initializes the DWC_otg controller and its root hub and prepares it for host
4222197ba5f4SPaul Zimmerman  * mode operation. Activates the root port. Returns 0 on success and a negative
4223197ba5f4SPaul Zimmerman  * error code on failure.
4224197ba5f4SPaul Zimmerman  */
4225197ba5f4SPaul Zimmerman static int _dwc2_hcd_start(struct usb_hcd *hcd)
4226197ba5f4SPaul Zimmerman {
4227197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4228197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4229197ba5f4SPaul Zimmerman 	unsigned long flags;
4230cd7cd0e6SFabrice Gasnier 	u32 hprt0;
423141ee1ea2SFabrice Gasnier 	int ret;
4232197ba5f4SPaul Zimmerman 
4233197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4234197ba5f4SPaul Zimmerman 
4235197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
423631927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L0;
4237197ba5f4SPaul Zimmerman 	hcd->state = HC_STATE_RUNNING;
423831927b6bSGregory Herrero 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4239197ba5f4SPaul Zimmerman 
4240197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
4241197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4242197ba5f4SPaul Zimmerman 		return 0;	/* why 0 ?? */
4243197ba5f4SPaul Zimmerman 	}
4244197ba5f4SPaul Zimmerman 
4245197ba5f4SPaul Zimmerman 	dwc2_hcd_reinit(hsotg);
4246197ba5f4SPaul Zimmerman 
4247cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4248cd7cd0e6SFabrice Gasnier 	/* Has vbus power been turned on in dwc2_core_host_init ? */
4249cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR) {
4250cd7cd0e6SFabrice Gasnier 		/* Enable external vbus supply before resuming root hub */
425141ee1ea2SFabrice Gasnier 		spin_unlock_irqrestore(&hsotg->lock, flags);
425241ee1ea2SFabrice Gasnier 		ret = dwc2_vbus_supply_init(hsotg);
425341ee1ea2SFabrice Gasnier 		if (ret)
425441ee1ea2SFabrice Gasnier 			return ret;
425541ee1ea2SFabrice Gasnier 		spin_lock_irqsave(&hsotg->lock, flags);
4256cd7cd0e6SFabrice Gasnier 	}
425741ee1ea2SFabrice Gasnier 
4258197ba5f4SPaul Zimmerman 	/* Initialize and connect root hub if one is not already attached */
4259197ba5f4SPaul Zimmerman 	if (bus->root_hub) {
4260197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4261197ba5f4SPaul Zimmerman 		/* Inform the HUB driver to resume */
4262197ba5f4SPaul Zimmerman 		usb_hcd_resume_root_hub(hcd);
4263197ba5f4SPaul Zimmerman 	}
4264197ba5f4SPaul Zimmerman 
4265197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4266531ef5ebSAmelie Delaunay 
426741ee1ea2SFabrice Gasnier 	return 0;
4268197ba5f4SPaul Zimmerman }
4269197ba5f4SPaul Zimmerman 
4270197ba5f4SPaul Zimmerman /*
4271197ba5f4SPaul Zimmerman  * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4272197ba5f4SPaul Zimmerman  * stopped.
4273197ba5f4SPaul Zimmerman  */
4274197ba5f4SPaul Zimmerman static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4275197ba5f4SPaul Zimmerman {
4276197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4277197ba5f4SPaul Zimmerman 	unsigned long flags;
4278cd7cd0e6SFabrice Gasnier 	u32 hprt0;
4279197ba5f4SPaul Zimmerman 
42805bbf6ce0SGregory Herrero 	/* Turn off all host-specific interrupts */
42815bbf6ce0SGregory Herrero 	dwc2_disable_host_interrupts(hsotg);
42825bbf6ce0SGregory Herrero 
4283091473adSGregory Herrero 	/* Wait for interrupt processing to finish */
4284091473adSGregory Herrero 	synchronize_irq(hcd->irq);
4285091473adSGregory Herrero 
4286197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4287cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4288091473adSGregory Herrero 	/* Ensure hcd is disconnected */
42896a659531SDouglas Anderson 	dwc2_hcd_disconnect(hsotg, true);
4290197ba5f4SPaul Zimmerman 	dwc2_hcd_stop(hsotg);
429131927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L3;
429231927b6bSGregory Herrero 	hcd->state = HC_STATE_HALT;
429331927b6bSGregory Herrero 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4294197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4295197ba5f4SPaul Zimmerman 
4296cd7cd0e6SFabrice Gasnier 	/* keep balanced supply init/exit by checking HPRT0_PWR */
4297cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR)
4298531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
4299531ef5ebSAmelie Delaunay 
4300197ba5f4SPaul Zimmerman 	usleep_range(1000, 3000);
4301197ba5f4SPaul Zimmerman }
4302197ba5f4SPaul Zimmerman 
430399a65798SGregory Herrero static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
430499a65798SGregory Herrero {
430599a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4306a2a23d3fSGregory Herrero 	unsigned long flags;
4307a2a23d3fSGregory Herrero 	int ret = 0;
4308a2a23d3fSGregory Herrero 	u32 hprt0;
43096f6d7059SDouglas Anderson 	u32 pcgctl;
431099a65798SGregory Herrero 
4311a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4312a2a23d3fSGregory Herrero 
4313f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4314f367b72cSMeng Dongyang 		goto unlock;
4315f367b72cSMeng Dongyang 
4316a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L0)
4317a2a23d3fSGregory Herrero 		goto unlock;
4318a2a23d3fSGregory Herrero 
4319a2a23d3fSGregory Herrero 	if (!HCD_HW_ACCESSIBLE(hcd))
4320a2a23d3fSGregory Herrero 		goto unlock;
4321a2a23d3fSGregory Herrero 
4322866932e2SJohn Stultz 	if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4323866932e2SJohn Stultz 		goto unlock;
4324866932e2SJohn Stultz 
432593f67280SArtur Petrosyan 	if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL ||
432693f67280SArtur Petrosyan 	    hsotg->flags.b.port_connect_status == 0)
4327a2a23d3fSGregory Herrero 		goto skip_power_saving;
4328a2a23d3fSGregory Herrero 
4329a2a23d3fSGregory Herrero 	/*
4330a2a23d3fSGregory Herrero 	 * Drive USB suspend and disable port Power
4331a2a23d3fSGregory Herrero 	 * if usb bus is not suspended.
4332a2a23d3fSGregory Herrero 	 */
4333a2a23d3fSGregory Herrero 	if (!hsotg->bus_suspended) {
4334a2a23d3fSGregory Herrero 		hprt0 = dwc2_read_hprt0(hsotg);
43356f6d7059SDouglas Anderson 		if (hprt0 & HPRT0_CONNSTS) {
4336a2a23d3fSGregory Herrero 			hprt0 |= HPRT0_SUSP;
43376f6d7059SDouglas Anderson 			if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL)
4338a2a23d3fSGregory Herrero 				hprt0 &= ~HPRT0_PWR;
4339f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
43406f6d7059SDouglas Anderson 		}
43416f6d7059SDouglas Anderson 		if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
43425aa678c7SFabrice Gasnier 			spin_unlock_irqrestore(&hsotg->lock, flags);
4343531ef5ebSAmelie Delaunay 			dwc2_vbus_supply_exit(hsotg);
43445aa678c7SFabrice Gasnier 			spin_lock_irqsave(&hsotg->lock, flags);
43456f6d7059SDouglas Anderson 		} else {
43466f6d7059SDouglas Anderson 			pcgctl = readl(hsotg->regs + PCGCTL);
43476f6d7059SDouglas Anderson 			pcgctl |= PCGCTL_STOPPCLK;
43486f6d7059SDouglas Anderson 			writel(pcgctl, hsotg->regs + PCGCTL);
43496f6d7059SDouglas Anderson 		}
4350a2a23d3fSGregory Herrero 	}
4351a2a23d3fSGregory Herrero 
43526f6d7059SDouglas Anderson 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
435341ba9b9bSVardan Mikayelyan 		/* Enter partial_power_down */
435441ba9b9bSVardan Mikayelyan 		ret = dwc2_enter_partial_power_down(hsotg);
4355a2a23d3fSGregory Herrero 		if (ret) {
4356a2a23d3fSGregory Herrero 			if (ret != -ENOTSUPP)
4357a2a23d3fSGregory Herrero 				dev_err(hsotg->dev,
435841ba9b9bSVardan Mikayelyan 					"enter partial_power_down failed\n");
4359a2a23d3fSGregory Herrero 			goto skip_power_saving;
4360a2a23d3fSGregory Herrero 		}
4361a2a23d3fSGregory Herrero 
43626f6d7059SDouglas Anderson 		/* After entering partial_power_down, hardware is no more accessible */
43636f6d7059SDouglas Anderson 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
43646f6d7059SDouglas Anderson 	}
43656f6d7059SDouglas Anderson 
4366a2a23d3fSGregory Herrero 	/* Ask phy to be suspended */
4367a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4368a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4369a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, true);
4370a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4371a2a23d3fSGregory Herrero 	}
4372a2a23d3fSGregory Herrero 
4373a2a23d3fSGregory Herrero skip_power_saving:
437499a65798SGregory Herrero 	hsotg->lx_state = DWC2_L2;
4375a2a23d3fSGregory Herrero unlock:
4376a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4377a2a23d3fSGregory Herrero 
4378a2a23d3fSGregory Herrero 	return ret;
437999a65798SGregory Herrero }
438099a65798SGregory Herrero 
438199a65798SGregory Herrero static int _dwc2_hcd_resume(struct usb_hcd *hcd)
438299a65798SGregory Herrero {
438399a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4384a2a23d3fSGregory Herrero 	unsigned long flags;
43856f6d7059SDouglas Anderson 	u32 pcgctl;
4386a2a23d3fSGregory Herrero 	int ret = 0;
4387a2a23d3fSGregory Herrero 
4388a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4389a2a23d3fSGregory Herrero 
4390f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4391f367b72cSMeng Dongyang 		goto unlock;
4392f367b72cSMeng Dongyang 
4393a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L2)
4394a2a23d3fSGregory Herrero 		goto unlock;
4395a2a23d3fSGregory Herrero 
43966f6d7059SDouglas Anderson 	if (hsotg->params.power_down > DWC2_POWER_DOWN_PARAM_PARTIAL) {
4397a2a23d3fSGregory Herrero 		hsotg->lx_state = DWC2_L0;
4398a2a23d3fSGregory Herrero 		goto unlock;
4399a2a23d3fSGregory Herrero 	}
4400a2a23d3fSGregory Herrero 
4401a2a23d3fSGregory Herrero 	/*
4402a2a23d3fSGregory Herrero 	 * Enable power if not already done.
4403a2a23d3fSGregory Herrero 	 * This must not be spinlocked since duration
4404a2a23d3fSGregory Herrero 	 * of this call is unknown.
4405a2a23d3fSGregory Herrero 	 */
4406a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4407a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4408a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, false);
4409a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4410a2a23d3fSGregory Herrero 	}
4411a2a23d3fSGregory Herrero 
44126f6d7059SDouglas Anderson 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
44136f6d7059SDouglas Anderson 		/*
44146f6d7059SDouglas Anderson 		 * Set HW accessible bit before powering on the controller
44156f6d7059SDouglas Anderson 		 * since an interrupt may rise.
44166f6d7059SDouglas Anderson 		 */
44176f6d7059SDouglas Anderson 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
44186f6d7059SDouglas Anderson 
44196f6d7059SDouglas Anderson 
442041ba9b9bSVardan Mikayelyan 		/* Exit partial_power_down */
442141ba9b9bSVardan Mikayelyan 		ret = dwc2_exit_partial_power_down(hsotg, true);
4422a2a23d3fSGregory Herrero 		if (ret && (ret != -ENOTSUPP))
442341ba9b9bSVardan Mikayelyan 			dev_err(hsotg->dev, "exit partial_power_down failed\n");
44246f6d7059SDouglas Anderson 	} else {
44256f6d7059SDouglas Anderson 		pcgctl = readl(hsotg->regs + PCGCTL);
44266f6d7059SDouglas Anderson 		pcgctl &= ~PCGCTL_STOPPCLK;
44276f6d7059SDouglas Anderson 		writel(pcgctl, hsotg->regs + PCGCTL);
44286f6d7059SDouglas Anderson 	}
442999a65798SGregory Herrero 
443099a65798SGregory Herrero 	hsotg->lx_state = DWC2_L0;
4431a2a23d3fSGregory Herrero 
4432a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4433a2a23d3fSGregory Herrero 
4434a2a23d3fSGregory Herrero 	if (hsotg->bus_suspended) {
4435a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4436a2a23d3fSGregory Herrero 		hsotg->flags.b.port_suspend_change = 1;
4437a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4438a2a23d3fSGregory Herrero 		dwc2_port_resume(hsotg);
4439a2a23d3fSGregory Herrero 	} else {
44406f6d7059SDouglas Anderson 		if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_PARTIAL) {
4441531ef5ebSAmelie Delaunay 			dwc2_vbus_supply_init(hsotg);
4442531ef5ebSAmelie Delaunay 
44435634e016SGregory Herrero 			/* Wait for controller to correctly update D+/D- level */
44445634e016SGregory Herrero 			usleep_range(3000, 5000);
44456f6d7059SDouglas Anderson 		}
44465634e016SGregory Herrero 
4447a2a23d3fSGregory Herrero 		/*
4448a2a23d3fSGregory Herrero 		 * Clear Port Enable and Port Status changes.
4449a2a23d3fSGregory Herrero 		 * Enable Port Power.
4450a2a23d3fSGregory Herrero 		 */
4451f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
4452f25c42b8SGevorg Sahakyan 				HPRT0_ENACHG, HPRT0);
4453a2a23d3fSGregory Herrero 		/* Wait for controller to detect Port Connect */
44545634e016SGregory Herrero 		usleep_range(5000, 7000);
4455a2a23d3fSGregory Herrero 	}
4456a2a23d3fSGregory Herrero 
4457a2a23d3fSGregory Herrero 	return ret;
4458a2a23d3fSGregory Herrero unlock:
4459a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4460a2a23d3fSGregory Herrero 
4461a2a23d3fSGregory Herrero 	return ret;
446299a65798SGregory Herrero }
446399a65798SGregory Herrero 
4464197ba5f4SPaul Zimmerman /* Returns the current frame number */
4465197ba5f4SPaul Zimmerman static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4466197ba5f4SPaul Zimmerman {
4467197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4468197ba5f4SPaul Zimmerman 
4469197ba5f4SPaul Zimmerman 	return dwc2_hcd_get_frame_number(hsotg);
4470197ba5f4SPaul Zimmerman }
4471197ba5f4SPaul Zimmerman 
4472197ba5f4SPaul Zimmerman static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4473197ba5f4SPaul Zimmerman 			       char *fn_name)
4474197ba5f4SPaul Zimmerman {
4475197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
4476197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4477efe357f4SNicholas Mc Guire 	char *pipetype = NULL;
4478efe357f4SNicholas Mc Guire 	char *speed = NULL;
4479197ba5f4SPaul Zimmerman 
4480197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4481197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Device address: %d\n",
4482197ba5f4SPaul Zimmerman 		 usb_pipedevice(urb->pipe));
4483197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint: %d, %s\n",
4484197ba5f4SPaul Zimmerman 		 usb_pipeendpoint(urb->pipe),
4485197ba5f4SPaul Zimmerman 		 usb_pipein(urb->pipe) ? "IN" : "OUT");
4486197ba5f4SPaul Zimmerman 
4487197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4488197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4489197ba5f4SPaul Zimmerman 		pipetype = "CONTROL";
4490197ba5f4SPaul Zimmerman 		break;
4491197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4492197ba5f4SPaul Zimmerman 		pipetype = "BULK";
4493197ba5f4SPaul Zimmerman 		break;
4494197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4495197ba5f4SPaul Zimmerman 		pipetype = "INTERRUPT";
4496197ba5f4SPaul Zimmerman 		break;
4497197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4498197ba5f4SPaul Zimmerman 		pipetype = "ISOCHRONOUS";
4499197ba5f4SPaul Zimmerman 		break;
4500197ba5f4SPaul Zimmerman 	}
4501197ba5f4SPaul Zimmerman 
4502197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint type: %s %s (%s)\n", pipetype,
4503197ba5f4SPaul Zimmerman 		 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4504197ba5f4SPaul Zimmerman 		 "IN" : "OUT");
4505197ba5f4SPaul Zimmerman 
4506197ba5f4SPaul Zimmerman 	switch (urb->dev->speed) {
4507197ba5f4SPaul Zimmerman 	case USB_SPEED_HIGH:
4508197ba5f4SPaul Zimmerman 		speed = "HIGH";
4509197ba5f4SPaul Zimmerman 		break;
4510197ba5f4SPaul Zimmerman 	case USB_SPEED_FULL:
4511197ba5f4SPaul Zimmerman 		speed = "FULL";
4512197ba5f4SPaul Zimmerman 		break;
4513197ba5f4SPaul Zimmerman 	case USB_SPEED_LOW:
4514197ba5f4SPaul Zimmerman 		speed = "LOW";
4515197ba5f4SPaul Zimmerman 		break;
4516197ba5f4SPaul Zimmerman 	default:
4517197ba5f4SPaul Zimmerman 		speed = "UNKNOWN";
4518197ba5f4SPaul Zimmerman 		break;
4519197ba5f4SPaul Zimmerman 	}
4520197ba5f4SPaul Zimmerman 
4521197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Speed: %s\n", speed);
4522babd1839SDouglas Anderson 	dev_vdbg(hsotg->dev, "  Max packet size: %d (%d mult)\n",
4523babd1839SDouglas Anderson 		 usb_endpoint_maxp(&urb->ep->desc),
4524babd1839SDouglas Anderson 		 usb_endpoint_maxp_mult(&urb->ep->desc));
4525babd1839SDouglas Anderson 
4526197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Data buffer length: %d\n",
4527197ba5f4SPaul Zimmerman 		 urb->transfer_buffer_length);
4528197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
4529197ba5f4SPaul Zimmerman 		 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4530197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
4531197ba5f4SPaul Zimmerman 		 urb->setup_packet, (unsigned long)urb->setup_dma);
4532197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Interval: %d\n", urb->interval);
4533197ba5f4SPaul Zimmerman 
4534197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4535197ba5f4SPaul Zimmerman 		int i;
4536197ba5f4SPaul Zimmerman 
4537197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; i++) {
4538197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  ISO Desc %d:\n", i);
4539197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "    offset: %d, length %d\n",
4540197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].offset,
4541197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].length);
4542197ba5f4SPaul Zimmerman 		}
4543197ba5f4SPaul Zimmerman 	}
4544197ba5f4SPaul Zimmerman #endif
4545197ba5f4SPaul Zimmerman }
4546197ba5f4SPaul Zimmerman 
4547197ba5f4SPaul Zimmerman /*
4548197ba5f4SPaul Zimmerman  * Starts processing a USB transfer request specified by a USB Request Block
4549197ba5f4SPaul Zimmerman  * (URB). mem_flags indicates the type of memory allocation to use while
4550197ba5f4SPaul Zimmerman  * processing this URB.
4551197ba5f4SPaul Zimmerman  */
4552197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4553197ba5f4SPaul Zimmerman 				 gfp_t mem_flags)
4554197ba5f4SPaul Zimmerman {
4555197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4556197ba5f4SPaul Zimmerman 	struct usb_host_endpoint *ep = urb->ep;
4557197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *dwc2_urb;
4558197ba5f4SPaul Zimmerman 	int i;
4559197ba5f4SPaul Zimmerman 	int retval;
4560197ba5f4SPaul Zimmerman 	int alloc_bandwidth = 0;
4561197ba5f4SPaul Zimmerman 	u8 ep_type = 0;
4562197ba5f4SPaul Zimmerman 	u32 tflags = 0;
4563197ba5f4SPaul Zimmerman 	void *buf;
4564197ba5f4SPaul Zimmerman 	unsigned long flags;
4565b58e6ceeSMian Yousaf Kaukab 	struct dwc2_qh *qh;
4566b58e6ceeSMian Yousaf Kaukab 	bool qh_allocated = false;
4567b5a468a6SMian Yousaf Kaukab 	struct dwc2_qtd *qtd;
4568197ba5f4SPaul Zimmerman 
4569197ba5f4SPaul Zimmerman 	if (dbg_urb(urb)) {
4570197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4571197ba5f4SPaul Zimmerman 		dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4572197ba5f4SPaul Zimmerman 	}
4573197ba5f4SPaul Zimmerman 
45749da51974SJohn Youn 	if (!ep)
4575197ba5f4SPaul Zimmerman 		return -EINVAL;
4576197ba5f4SPaul Zimmerman 
4577197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4578197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4579197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
4580197ba5f4SPaul Zimmerman 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4581197ba5f4SPaul Zimmerman 			alloc_bandwidth = 1;
4582197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4583197ba5f4SPaul Zimmerman 	}
4584197ba5f4SPaul Zimmerman 
4585197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4586197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4587197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_CONTROL;
4588197ba5f4SPaul Zimmerman 		break;
4589197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4590197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_ISOC;
4591197ba5f4SPaul Zimmerman 		break;
4592197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4593197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_BULK;
4594197ba5f4SPaul Zimmerman 		break;
4595197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4596197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_INT;
4597197ba5f4SPaul Zimmerman 		break;
4598197ba5f4SPaul Zimmerman 	}
4599197ba5f4SPaul Zimmerman 
4600197ba5f4SPaul Zimmerman 	dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4601197ba5f4SPaul Zimmerman 				      mem_flags);
4602197ba5f4SPaul Zimmerman 	if (!dwc2_urb)
4603197ba5f4SPaul Zimmerman 		return -ENOMEM;
4604197ba5f4SPaul Zimmerman 
4605197ba5f4SPaul Zimmerman 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4606197ba5f4SPaul Zimmerman 				  usb_pipeendpoint(urb->pipe), ep_type,
4607197ba5f4SPaul Zimmerman 				  usb_pipein(urb->pipe),
4608babd1839SDouglas Anderson 				  usb_endpoint_maxp(&ep->desc),
4609babd1839SDouglas Anderson 				  usb_endpoint_maxp_mult(&ep->desc));
4610197ba5f4SPaul Zimmerman 
4611197ba5f4SPaul Zimmerman 	buf = urb->transfer_buffer;
4612197ba5f4SPaul Zimmerman 
4613edfbcb32SChristoph Hellwig 	if (hcd_uses_dma(hcd)) {
4614197ba5f4SPaul Zimmerman 		if (!buf && (urb->transfer_dma & 3)) {
4615197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
4616197ba5f4SPaul Zimmerman 				"%s: unaligned transfer with no transfer_buffer",
4617197ba5f4SPaul Zimmerman 				__func__);
4618197ba5f4SPaul Zimmerman 			retval = -EINVAL;
461933ad261aSGregory Herrero 			goto fail0;
4620197ba5f4SPaul Zimmerman 		}
4621197ba5f4SPaul Zimmerman 	}
4622197ba5f4SPaul Zimmerman 
4623197ba5f4SPaul Zimmerman 	if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4624197ba5f4SPaul Zimmerman 		tflags |= URB_GIVEBACK_ASAP;
4625197ba5f4SPaul Zimmerman 	if (urb->transfer_flags & URB_ZERO_PACKET)
4626197ba5f4SPaul Zimmerman 		tflags |= URB_SEND_ZERO_PACKET;
4627197ba5f4SPaul Zimmerman 
4628197ba5f4SPaul Zimmerman 	dwc2_urb->priv = urb;
4629197ba5f4SPaul Zimmerman 	dwc2_urb->buf = buf;
4630197ba5f4SPaul Zimmerman 	dwc2_urb->dma = urb->transfer_dma;
4631197ba5f4SPaul Zimmerman 	dwc2_urb->length = urb->transfer_buffer_length;
4632197ba5f4SPaul Zimmerman 	dwc2_urb->setup_packet = urb->setup_packet;
4633197ba5f4SPaul Zimmerman 	dwc2_urb->setup_dma = urb->setup_dma;
4634197ba5f4SPaul Zimmerman 	dwc2_urb->flags = tflags;
4635197ba5f4SPaul Zimmerman 	dwc2_urb->interval = urb->interval;
4636197ba5f4SPaul Zimmerman 	dwc2_urb->status = -EINPROGRESS;
4637197ba5f4SPaul Zimmerman 
4638197ba5f4SPaul Zimmerman 	for (i = 0; i < urb->number_of_packets; ++i)
4639197ba5f4SPaul Zimmerman 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4640197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].offset,
4641197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].length);
4642197ba5f4SPaul Zimmerman 
4643197ba5f4SPaul Zimmerman 	urb->hcpriv = dwc2_urb;
4644b58e6ceeSMian Yousaf Kaukab 	qh = (struct dwc2_qh *)ep->hcpriv;
4645b58e6ceeSMian Yousaf Kaukab 	/* Create QH for the endpoint if it doesn't exist */
4646b58e6ceeSMian Yousaf Kaukab 	if (!qh) {
4647b58e6ceeSMian Yousaf Kaukab 		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4648b58e6ceeSMian Yousaf Kaukab 		if (!qh) {
4649b58e6ceeSMian Yousaf Kaukab 			retval = -ENOMEM;
4650b58e6ceeSMian Yousaf Kaukab 			goto fail0;
4651b58e6ceeSMian Yousaf Kaukab 		}
4652b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = qh;
4653b58e6ceeSMian Yousaf Kaukab 		qh_allocated = true;
4654b58e6ceeSMian Yousaf Kaukab 	}
4655197ba5f4SPaul Zimmerman 
4656b5a468a6SMian Yousaf Kaukab 	qtd = kzalloc(sizeof(*qtd), mem_flags);
4657b5a468a6SMian Yousaf Kaukab 	if (!qtd) {
4658b5a468a6SMian Yousaf Kaukab 		retval = -ENOMEM;
4659b5a468a6SMian Yousaf Kaukab 		goto fail1;
4660b5a468a6SMian Yousaf Kaukab 	}
4661b5a468a6SMian Yousaf Kaukab 
4662197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4663197ba5f4SPaul Zimmerman 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
4664197ba5f4SPaul Zimmerman 	if (retval)
4665197ba5f4SPaul Zimmerman 		goto fail2;
4666197ba5f4SPaul Zimmerman 
4667b5a468a6SMian Yousaf Kaukab 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4668b5a468a6SMian Yousaf Kaukab 	if (retval)
4669b5a468a6SMian Yousaf Kaukab 		goto fail3;
4670b5a468a6SMian Yousaf Kaukab 
4671197ba5f4SPaul Zimmerman 	if (alloc_bandwidth) {
4672197ba5f4SPaul Zimmerman 		dwc2_allocate_bus_bandwidth(hcd,
4673197ba5f4SPaul Zimmerman 				dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4674197ba5f4SPaul Zimmerman 				urb);
4675197ba5f4SPaul Zimmerman 	}
4676197ba5f4SPaul Zimmerman 
467733ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
467833ad261aSGregory Herrero 
4679197ba5f4SPaul Zimmerman 	return 0;
4680197ba5f4SPaul Zimmerman 
4681b5a468a6SMian Yousaf Kaukab fail3:
4682197ba5f4SPaul Zimmerman 	dwc2_urb->priv = NULL;
4683197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
468416e80218SDouglas Anderson 	if (qh_allocated && qh->channel && qh->channel->qh == qh)
468516e80218SDouglas Anderson 		qh->channel->qh = NULL;
4686b5a468a6SMian Yousaf Kaukab fail2:
468733ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4688197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4689b5a468a6SMian Yousaf Kaukab 	kfree(qtd);
4690b5a468a6SMian Yousaf Kaukab fail1:
4691b58e6ceeSMian Yousaf Kaukab 	if (qh_allocated) {
4692b58e6ceeSMian Yousaf Kaukab 		struct dwc2_qtd *qtd2, *qtd2_tmp;
4693b58e6ceeSMian Yousaf Kaukab 
4694b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = NULL;
4695b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_unlink(hsotg, qh);
4696b58e6ceeSMian Yousaf Kaukab 		/* Free each QTD in the QH's QTD list */
4697b58e6ceeSMian Yousaf Kaukab 		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
4698b58e6ceeSMian Yousaf Kaukab 					 qtd_list_entry)
4699b58e6ceeSMian Yousaf Kaukab 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4700b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_free(hsotg, qh);
4701b58e6ceeSMian Yousaf Kaukab 	}
470233ad261aSGregory Herrero fail0:
4703197ba5f4SPaul Zimmerman 	kfree(dwc2_urb);
4704197ba5f4SPaul Zimmerman 
4705197ba5f4SPaul Zimmerman 	return retval;
4706197ba5f4SPaul Zimmerman }
4707197ba5f4SPaul Zimmerman 
4708197ba5f4SPaul Zimmerman /*
4709197ba5f4SPaul Zimmerman  * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4710197ba5f4SPaul Zimmerman  */
4711197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4712197ba5f4SPaul Zimmerman 				 int status)
4713197ba5f4SPaul Zimmerman {
4714197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4715197ba5f4SPaul Zimmerman 	int rc;
4716197ba5f4SPaul Zimmerman 	unsigned long flags;
4717197ba5f4SPaul Zimmerman 
4718197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4719197ba5f4SPaul Zimmerman 	dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4720197ba5f4SPaul Zimmerman 
4721197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4722197ba5f4SPaul Zimmerman 
4723197ba5f4SPaul Zimmerman 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4724197ba5f4SPaul Zimmerman 	if (rc)
4725197ba5f4SPaul Zimmerman 		goto out;
4726197ba5f4SPaul Zimmerman 
4727197ba5f4SPaul Zimmerman 	if (!urb->hcpriv) {
4728197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4729197ba5f4SPaul Zimmerman 		goto out;
4730197ba5f4SPaul Zimmerman 	}
4731197ba5f4SPaul Zimmerman 
4732197ba5f4SPaul Zimmerman 	rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4733197ba5f4SPaul Zimmerman 
4734197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
4735197ba5f4SPaul Zimmerman 
4736197ba5f4SPaul Zimmerman 	kfree(urb->hcpriv);
4737197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4738197ba5f4SPaul Zimmerman 
4739197ba5f4SPaul Zimmerman 	/* Higher layer software sets URB status */
4740197ba5f4SPaul Zimmerman 	spin_unlock(&hsotg->lock);
4741197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(hcd, urb, status);
4742197ba5f4SPaul Zimmerman 	spin_lock(&hsotg->lock);
4743197ba5f4SPaul Zimmerman 
4744197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4745197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  urb->status = %d\n", urb->status);
4746197ba5f4SPaul Zimmerman out:
4747197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4748197ba5f4SPaul Zimmerman 
4749197ba5f4SPaul Zimmerman 	return rc;
4750197ba5f4SPaul Zimmerman }
4751197ba5f4SPaul Zimmerman 
4752197ba5f4SPaul Zimmerman /*
4753197ba5f4SPaul Zimmerman  * Frees resources in the DWC_otg controller related to a given endpoint. Also
4754197ba5f4SPaul Zimmerman  * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4755197ba5f4SPaul Zimmerman  * must already be dequeued.
4756197ba5f4SPaul Zimmerman  */
4757197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4758197ba5f4SPaul Zimmerman 				       struct usb_host_endpoint *ep)
4759197ba5f4SPaul Zimmerman {
4760197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4761197ba5f4SPaul Zimmerman 
4762197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4763197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4764197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress, ep->hcpriv);
4765197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4766197ba5f4SPaul Zimmerman }
4767197ba5f4SPaul Zimmerman 
4768197ba5f4SPaul Zimmerman /*
4769197ba5f4SPaul Zimmerman  * Resets endpoint specific parameter values, in current version used to reset
4770197ba5f4SPaul Zimmerman  * the data toggle (as a WA). This function can be called from usb_clear_halt
4771197ba5f4SPaul Zimmerman  * routine.
4772197ba5f4SPaul Zimmerman  */
4773197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4774197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep)
4775197ba5f4SPaul Zimmerman {
4776197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4777197ba5f4SPaul Zimmerman 	unsigned long flags;
4778197ba5f4SPaul Zimmerman 
4779197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4780197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4781197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress);
4782197ba5f4SPaul Zimmerman 
4783197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4784197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_reset(hsotg, ep);
4785197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4786197ba5f4SPaul Zimmerman }
4787197ba5f4SPaul Zimmerman 
4788197ba5f4SPaul Zimmerman /*
4789197ba5f4SPaul Zimmerman  * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4790197ba5f4SPaul Zimmerman  * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4791197ba5f4SPaul Zimmerman  * interrupt.
4792197ba5f4SPaul Zimmerman  *
4793197ba5f4SPaul Zimmerman  * This function is called by the USB core when an interrupt occurs
4794197ba5f4SPaul Zimmerman  */
4795197ba5f4SPaul Zimmerman static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4796197ba5f4SPaul Zimmerman {
4797197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4798197ba5f4SPaul Zimmerman 
4799197ba5f4SPaul Zimmerman 	return dwc2_handle_hcd_intr(hsotg);
4800197ba5f4SPaul Zimmerman }
4801197ba5f4SPaul Zimmerman 
4802197ba5f4SPaul Zimmerman /*
4803197ba5f4SPaul Zimmerman  * Creates Status Change bitmap for the root hub and root port. The bitmap is
4804197ba5f4SPaul Zimmerman  * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4805197ba5f4SPaul Zimmerman  * is the status change indicator for the single root port. Returns 1 if either
4806197ba5f4SPaul Zimmerman  * change indicator is 1, otherwise returns 0.
4807197ba5f4SPaul Zimmerman  */
4808197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4809197ba5f4SPaul Zimmerman {
4810197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4811197ba5f4SPaul Zimmerman 
4812197ba5f4SPaul Zimmerman 	buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4813197ba5f4SPaul Zimmerman 	return buf[0] != 0;
4814197ba5f4SPaul Zimmerman }
4815197ba5f4SPaul Zimmerman 
4816197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
4817197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4818197ba5f4SPaul Zimmerman 				 u16 windex, char *buf, u16 wlength)
4819197ba5f4SPaul Zimmerman {
4820197ba5f4SPaul Zimmerman 	int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4821197ba5f4SPaul Zimmerman 					  wvalue, windex, buf, wlength);
4822197ba5f4SPaul Zimmerman 	return retval;
4823197ba5f4SPaul Zimmerman }
4824197ba5f4SPaul Zimmerman 
4825197ba5f4SPaul Zimmerman /* Handles hub TT buffer clear completions */
4826197ba5f4SPaul Zimmerman static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4827197ba5f4SPaul Zimmerman 					       struct usb_host_endpoint *ep)
4828197ba5f4SPaul Zimmerman {
4829197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4830197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
4831197ba5f4SPaul Zimmerman 	unsigned long flags;
4832197ba5f4SPaul Zimmerman 
4833197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
4834197ba5f4SPaul Zimmerman 	if (!qh)
4835197ba5f4SPaul Zimmerman 		return;
4836197ba5f4SPaul Zimmerman 
4837197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4838197ba5f4SPaul Zimmerman 	qh->tt_buffer_dirty = 0;
4839197ba5f4SPaul Zimmerman 
4840197ba5f4SPaul Zimmerman 	if (hsotg->flags.b.port_connect_status)
4841197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4842197ba5f4SPaul Zimmerman 
4843197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4844197ba5f4SPaul Zimmerman }
4845197ba5f4SPaul Zimmerman 
4846ca8b0332SChen Yu /*
4847ca8b0332SChen Yu  * HPRT0_SPD_HIGH_SPEED: high speed
4848ca8b0332SChen Yu  * HPRT0_SPD_FULL_SPEED: full speed
4849ca8b0332SChen Yu  */
4850ca8b0332SChen Yu static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4851ca8b0332SChen Yu {
4852ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4853ca8b0332SChen Yu 
4854ca8b0332SChen Yu 	if (hsotg->params.speed == speed)
4855ca8b0332SChen Yu 		return;
4856ca8b0332SChen Yu 
4857ca8b0332SChen Yu 	hsotg->params.speed = speed;
4858ca8b0332SChen Yu 	queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4859ca8b0332SChen Yu }
4860ca8b0332SChen Yu 
4861ca8b0332SChen Yu static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4862ca8b0332SChen Yu {
4863ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4864ca8b0332SChen Yu 
4865ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4866ca8b0332SChen Yu 		return;
4867ca8b0332SChen Yu 
4868ca8b0332SChen Yu 	/*
4869ca8b0332SChen Yu 	 * On removal, set speed to default high-speed.
4870ca8b0332SChen Yu 	 */
4871ca8b0332SChen Yu 	if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4872ca8b0332SChen Yu 	    udev->parent->speed < USB_SPEED_HIGH) {
4873ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to default high-speed\n");
4874ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4875ca8b0332SChen Yu 	}
4876ca8b0332SChen Yu }
4877ca8b0332SChen Yu 
4878ca8b0332SChen Yu static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4879ca8b0332SChen Yu {
4880ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4881ca8b0332SChen Yu 
4882ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4883ca8b0332SChen Yu 		return 0;
4884ca8b0332SChen Yu 
4885ca8b0332SChen Yu 	if (udev->speed == USB_SPEED_HIGH) {
4886ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to high-speed\n");
4887ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4888ca8b0332SChen Yu 	} else if ((udev->speed == USB_SPEED_FULL ||
4889ca8b0332SChen Yu 				udev->speed == USB_SPEED_LOW)) {
4890ca8b0332SChen Yu 		/*
4891ca8b0332SChen Yu 		 * Change speed setting to full-speed if there's
4892ca8b0332SChen Yu 		 * a full-speed or low-speed device plugged in.
4893ca8b0332SChen Yu 		 */
4894ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to full-speed\n");
4895ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4896ca8b0332SChen Yu 	}
4897ca8b0332SChen Yu 
4898ca8b0332SChen Yu 	return 0;
4899ca8b0332SChen Yu }
4900ca8b0332SChen Yu 
4901197ba5f4SPaul Zimmerman static struct hc_driver dwc2_hc_driver = {
4902197ba5f4SPaul Zimmerman 	.description = "dwc2_hsotg",
4903197ba5f4SPaul Zimmerman 	.product_desc = "DWC OTG Controller",
4904197ba5f4SPaul Zimmerman 	.hcd_priv_size = sizeof(struct wrapper_priv_data),
4905197ba5f4SPaul Zimmerman 
4906197ba5f4SPaul Zimmerman 	.irq = _dwc2_hcd_irq,
49078add17cfSDouglas Anderson 	.flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
4908197ba5f4SPaul Zimmerman 
4909197ba5f4SPaul Zimmerman 	.start = _dwc2_hcd_start,
4910197ba5f4SPaul Zimmerman 	.stop = _dwc2_hcd_stop,
4911197ba5f4SPaul Zimmerman 	.urb_enqueue = _dwc2_hcd_urb_enqueue,
4912197ba5f4SPaul Zimmerman 	.urb_dequeue = _dwc2_hcd_urb_dequeue,
4913197ba5f4SPaul Zimmerman 	.endpoint_disable = _dwc2_hcd_endpoint_disable,
4914197ba5f4SPaul Zimmerman 	.endpoint_reset = _dwc2_hcd_endpoint_reset,
4915197ba5f4SPaul Zimmerman 	.get_frame_number = _dwc2_hcd_get_frame_number,
4916197ba5f4SPaul Zimmerman 
4917197ba5f4SPaul Zimmerman 	.hub_status_data = _dwc2_hcd_hub_status_data,
4918197ba5f4SPaul Zimmerman 	.hub_control = _dwc2_hcd_hub_control,
4919197ba5f4SPaul Zimmerman 	.clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
492099a65798SGregory Herrero 
492199a65798SGregory Herrero 	.bus_suspend = _dwc2_hcd_suspend,
492299a65798SGregory Herrero 	.bus_resume = _dwc2_hcd_resume,
49233bc04e28SDouglas Anderson 
49243bc04e28SDouglas Anderson 	.map_urb_for_dma	= dwc2_map_urb_for_dma,
49253bc04e28SDouglas Anderson 	.unmap_urb_for_dma	= dwc2_unmap_urb_for_dma,
4926197ba5f4SPaul Zimmerman };
4927197ba5f4SPaul Zimmerman 
4928197ba5f4SPaul Zimmerman /*
4929197ba5f4SPaul Zimmerman  * Frees secondary storage associated with the dwc2_hsotg structure contained
4930197ba5f4SPaul Zimmerman  * in the struct usb_hcd field
4931197ba5f4SPaul Zimmerman  */
4932197ba5f4SPaul Zimmerman static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
4933197ba5f4SPaul Zimmerman {
4934197ba5f4SPaul Zimmerman 	u32 ahbcfg;
4935197ba5f4SPaul Zimmerman 	u32 dctl;
4936197ba5f4SPaul Zimmerman 	int i;
4937197ba5f4SPaul Zimmerman 
4938197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
4939197ba5f4SPaul Zimmerman 
4940197ba5f4SPaul Zimmerman 	/* Free memory for QH/QTD lists */
4941197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
494238d2b5fbSDouglas Anderson 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
4943197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
4944197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
4945197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
4946197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
4947197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
4948197ba5f4SPaul Zimmerman 
4949197ba5f4SPaul Zimmerman 	/* Free memory for the host channels */
4950197ba5f4SPaul Zimmerman 	for (i = 0; i < MAX_EPS_CHANNELS; i++) {
4951197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
4952197ba5f4SPaul Zimmerman 
49539da51974SJohn Youn 		if (chan) {
4954197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
4955197ba5f4SPaul Zimmerman 				i, chan);
4956197ba5f4SPaul Zimmerman 			hsotg->hc_ptr_array[i] = NULL;
4957197ba5f4SPaul Zimmerman 			kfree(chan);
4958197ba5f4SPaul Zimmerman 		}
4959197ba5f4SPaul Zimmerman 	}
4960197ba5f4SPaul Zimmerman 
496195832c00SJohn Youn 	if (hsotg->params.host_dma) {
4962197ba5f4SPaul Zimmerman 		if (hsotg->status_buf) {
4963197ba5f4SPaul Zimmerman 			dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
4964197ba5f4SPaul Zimmerman 					  hsotg->status_buf,
4965197ba5f4SPaul Zimmerman 					  hsotg->status_buf_dma);
4966197ba5f4SPaul Zimmerman 			hsotg->status_buf = NULL;
4967197ba5f4SPaul Zimmerman 		}
4968197ba5f4SPaul Zimmerman 	} else {
4969197ba5f4SPaul Zimmerman 		kfree(hsotg->status_buf);
4970197ba5f4SPaul Zimmerman 		hsotg->status_buf = NULL;
4971197ba5f4SPaul Zimmerman 	}
4972197ba5f4SPaul Zimmerman 
4973f25c42b8SGevorg Sahakyan 	ahbcfg = dwc2_readl(hsotg, GAHBCFG);
4974197ba5f4SPaul Zimmerman 
4975197ba5f4SPaul Zimmerman 	/* Disable all interrupts */
4976197ba5f4SPaul Zimmerman 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
4977f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
4978f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
4979197ba5f4SPaul Zimmerman 
4980197ba5f4SPaul Zimmerman 	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
4981f25c42b8SGevorg Sahakyan 		dctl = dwc2_readl(hsotg, DCTL);
4982197ba5f4SPaul Zimmerman 		dctl |= DCTL_SFTDISCON;
4983f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dctl, DCTL);
4984197ba5f4SPaul Zimmerman 	}
4985197ba5f4SPaul Zimmerman 
4986197ba5f4SPaul Zimmerman 	if (hsotg->wq_otg) {
4987197ba5f4SPaul Zimmerman 		if (!cancel_work_sync(&hsotg->wf_otg))
4988197ba5f4SPaul Zimmerman 			flush_workqueue(hsotg->wq_otg);
4989197ba5f4SPaul Zimmerman 		destroy_workqueue(hsotg->wq_otg);
4990197ba5f4SPaul Zimmerman 	}
4991197ba5f4SPaul Zimmerman 
4992c40cf770SDouglas Anderson 	cancel_work_sync(&hsotg->phy_reset_work);
4993c40cf770SDouglas Anderson 
4994197ba5f4SPaul Zimmerman 	del_timer(&hsotg->wkp_timer);
4995197ba5f4SPaul Zimmerman }
4996197ba5f4SPaul Zimmerman 
4997197ba5f4SPaul Zimmerman static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
4998197ba5f4SPaul Zimmerman {
4999197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
5000197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
5001197ba5f4SPaul Zimmerman 
5002197ba5f4SPaul Zimmerman 	dwc2_hcd_free(hsotg);
5003197ba5f4SPaul Zimmerman }
5004197ba5f4SPaul Zimmerman 
5005197ba5f4SPaul Zimmerman /*
5006197ba5f4SPaul Zimmerman  * Initializes the HCD. This function allocates memory for and initializes the
5007197ba5f4SPaul Zimmerman  * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5008197ba5f4SPaul Zimmerman  * USB bus with the core and calls the hc_driver->start() function. It returns
5009197ba5f4SPaul Zimmerman  * a negative error on failure.
5010197ba5f4SPaul Zimmerman  */
50114fe160d5SHeiner Kallweit int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5012197ba5f4SPaul Zimmerman {
5013348becdcSHeiner Kallweit 	struct platform_device *pdev = to_platform_device(hsotg->dev);
5014348becdcSHeiner Kallweit 	struct resource *res;
5015197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5016197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
5017197ba5f4SPaul Zimmerman 	u32 hcfg;
5018197ba5f4SPaul Zimmerman 	int i, num_channels;
5019197ba5f4SPaul Zimmerman 	int retval;
5020197ba5f4SPaul Zimmerman 
5021f5500eccSDinh Nguyen 	if (usb_disabled())
5022f5500eccSDinh Nguyen 		return -ENODEV;
5023f5500eccSDinh Nguyen 
5024197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
5025197ba5f4SPaul Zimmerman 
5026197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5027197ba5f4SPaul Zimmerman 
5028f25c42b8SGevorg Sahakyan 	hcfg = dwc2_readl(hsotg, HCFG);
5029197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5030197ba5f4SPaul Zimmerman 
5031197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
50326396bb22SKees Cook 	hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
50336396bb22SKees Cook 					 sizeof(*hsotg->frame_num_array),
50346396bb22SKees Cook 					 GFP_KERNEL);
5035197ba5f4SPaul Zimmerman 	if (!hsotg->frame_num_array)
5036197ba5f4SPaul Zimmerman 		goto error1;
50376396bb22SKees Cook 	hsotg->last_frame_num_array =
50386396bb22SKees Cook 		kcalloc(FRAME_NUM_ARRAY_SIZE,
50396396bb22SKees Cook 			sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
5040197ba5f4SPaul Zimmerman 	if (!hsotg->last_frame_num_array)
5041197ba5f4SPaul Zimmerman 		goto error1;
5042197ba5f4SPaul Zimmerman #endif
5043483bb254SDouglas Anderson 	hsotg->last_frame_num = HFNUM_MAX_FRNUM;
5044197ba5f4SPaul Zimmerman 
5045197ba5f4SPaul Zimmerman 	/* Check if the bus driver or platform code has setup a dma_mask */
504695832c00SJohn Youn 	if (hsotg->params.host_dma &&
50479da51974SJohn Youn 	    !hsotg->dev->dma_mask) {
5048197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
5049197ba5f4SPaul Zimmerman 			 "dma_mask not set, disabling DMA\n");
5050fdb09b3eSNicholas Mc Guire 		hsotg->params.host_dma = false;
505195832c00SJohn Youn 		hsotg->params.dma_desc_enable = false;
5052197ba5f4SPaul Zimmerman 	}
5053197ba5f4SPaul Zimmerman 
5054197ba5f4SPaul Zimmerman 	/* Set device flags indicating whether the HCD supports DMA */
505595832c00SJohn Youn 	if (hsotg->params.host_dma) {
5056197ba5f4SPaul Zimmerman 		if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5057197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set DMA mask\n");
5058197ba5f4SPaul Zimmerman 		if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5059197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
5060197ba5f4SPaul Zimmerman 	}
5061197ba5f4SPaul Zimmerman 
5062ca8b0332SChen Yu 	if (hsotg->params.change_speed_quirk) {
5063ca8b0332SChen Yu 		dwc2_hc_driver.free_dev = dwc2_free_dev;
5064ca8b0332SChen Yu 		dwc2_hc_driver.reset_device = dwc2_reset_device;
5065ca8b0332SChen Yu 	}
5066ca8b0332SChen Yu 
50677b81cb6bSChristoph Hellwig 	if (hsotg->params.host_dma)
50687b81cb6bSChristoph Hellwig 		dwc2_hc_driver.flags |= HCD_DMA;
50697b81cb6bSChristoph Hellwig 
5070197ba5f4SPaul Zimmerman 	hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5071197ba5f4SPaul Zimmerman 	if (!hcd)
5072197ba5f4SPaul Zimmerman 		goto error1;
5073197ba5f4SPaul Zimmerman 
5074197ba5f4SPaul Zimmerman 	hcd->has_tt = 1;
5075197ba5f4SPaul Zimmerman 
5076348becdcSHeiner Kallweit 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5077348becdcSHeiner Kallweit 	hcd->rsrc_start = res->start;
5078348becdcSHeiner Kallweit 	hcd->rsrc_len = resource_size(res);
5079348becdcSHeiner Kallweit 
5080197ba5f4SPaul Zimmerman 	((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
5081197ba5f4SPaul Zimmerman 	hsotg->priv = hcd;
5082197ba5f4SPaul Zimmerman 
5083197ba5f4SPaul Zimmerman 	/*
5084197ba5f4SPaul Zimmerman 	 * Disable the global interrupt until all the interrupt handlers are
5085197ba5f4SPaul Zimmerman 	 * installed
5086197ba5f4SPaul Zimmerman 	 */
5087197ba5f4SPaul Zimmerman 	dwc2_disable_global_interrupts(hsotg);
5088197ba5f4SPaul Zimmerman 
5089197ba5f4SPaul Zimmerman 	/* Initialize the DWC_otg core, and select the Phy type */
50900fe239bcSDouglas Anderson 	retval = dwc2_core_init(hsotg, true);
5091197ba5f4SPaul Zimmerman 	if (retval)
5092197ba5f4SPaul Zimmerman 		goto error2;
5093197ba5f4SPaul Zimmerman 
5094197ba5f4SPaul Zimmerman 	/* Create new workqueue and init work */
5095197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5096ec7b1268SBhaktipriya Shridhar 	hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
5097197ba5f4SPaul Zimmerman 	if (!hsotg->wq_otg) {
5098197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Failed to create workqueue\n");
5099197ba5f4SPaul Zimmerman 		goto error2;
5100197ba5f4SPaul Zimmerman 	}
5101197ba5f4SPaul Zimmerman 	INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5102197ba5f4SPaul Zimmerman 
5103e99e88a9SKees Cook 	timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
5104197ba5f4SPaul Zimmerman 
5105197ba5f4SPaul Zimmerman 	/* Initialize the non-periodic schedule */
5106197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
510738d2b5fbSDouglas Anderson 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
5108197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5109197ba5f4SPaul Zimmerman 
5110197ba5f4SPaul Zimmerman 	/* Initialize the periodic schedule */
5111197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5112197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5113197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5114197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5115197ba5f4SPaul Zimmerman 
5116c9c8ac01SDouglas Anderson 	INIT_LIST_HEAD(&hsotg->split_order);
5117c9c8ac01SDouglas Anderson 
5118197ba5f4SPaul Zimmerman 	/*
5119197ba5f4SPaul Zimmerman 	 * Create a host channel descriptor for each host channel implemented
5120197ba5f4SPaul Zimmerman 	 * in the controller. Initialize the channel descriptor array.
5121197ba5f4SPaul Zimmerman 	 */
5122197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->free_hc_list);
5123bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
5124197ba5f4SPaul Zimmerman 	memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5125197ba5f4SPaul Zimmerman 
5126197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
5127197ba5f4SPaul Zimmerman 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
51289da51974SJohn Youn 		if (!channel)
5129197ba5f4SPaul Zimmerman 			goto error3;
5130197ba5f4SPaul Zimmerman 		channel->hc_num = i;
5131c9c8ac01SDouglas Anderson 		INIT_LIST_HEAD(&channel->split_order_list_entry);
5132197ba5f4SPaul Zimmerman 		hsotg->hc_ptr_array[i] = channel;
5133197ba5f4SPaul Zimmerman 	}
5134197ba5f4SPaul Zimmerman 
5135c40cf770SDouglas Anderson 	/* Initialize work */
5136197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5137197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5138c40cf770SDouglas Anderson 	INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
5139197ba5f4SPaul Zimmerman 
5140197ba5f4SPaul Zimmerman 	/*
5141197ba5f4SPaul Zimmerman 	 * Allocate space for storing data on status transactions. Normally no
5142197ba5f4SPaul Zimmerman 	 * data is sent, but this space acts as a bit bucket. This must be
5143197ba5f4SPaul Zimmerman 	 * done after usb_add_hcd since that function allocates the DMA buffer
5144197ba5f4SPaul Zimmerman 	 * pool.
5145197ba5f4SPaul Zimmerman 	 */
514695832c00SJohn Youn 	if (hsotg->params.host_dma)
5147197ba5f4SPaul Zimmerman 		hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5148197ba5f4SPaul Zimmerman 					DWC2_HCD_STATUS_BUF_SIZE,
5149197ba5f4SPaul Zimmerman 					&hsotg->status_buf_dma, GFP_KERNEL);
5150197ba5f4SPaul Zimmerman 	else
5151197ba5f4SPaul Zimmerman 		hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5152197ba5f4SPaul Zimmerman 					  GFP_KERNEL);
5153197ba5f4SPaul Zimmerman 
5154197ba5f4SPaul Zimmerman 	if (!hsotg->status_buf)
5155197ba5f4SPaul Zimmerman 		goto error3;
5156197ba5f4SPaul Zimmerman 
51573b5fcc9aSGregory Herrero 	/*
51583b5fcc9aSGregory Herrero 	 * Create kmem caches to handle descriptor buffers in descriptor
51593b5fcc9aSGregory Herrero 	 * DMA mode.
51603b5fcc9aSGregory Herrero 	 * Alignment must be set to 512 bytes.
51613b5fcc9aSGregory Herrero 	 */
5162bea8e86cSJohn Youn 	if (hsotg->params.dma_desc_enable ||
5163bea8e86cSJohn Youn 	    hsotg->params.dma_desc_fs_enable) {
51643b5fcc9aSGregory Herrero 		hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
5165ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
51663b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
51673b5fcc9aSGregory Herrero 				NULL);
51683b5fcc9aSGregory Herrero 		if (!hsotg->desc_gen_cache) {
51693b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
51703b5fcc9aSGregory Herrero 				"unable to create dwc2 generic desc cache\n");
51713b5fcc9aSGregory Herrero 
51723b5fcc9aSGregory Herrero 			/*
51733b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
51743b5fcc9aSGregory Herrero 			 * usable.
51753b5fcc9aSGregory Herrero 			 */
517695832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
517795832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
51783b5fcc9aSGregory Herrero 		}
51793b5fcc9aSGregory Herrero 
51803b5fcc9aSGregory Herrero 		hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
5181ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
51823b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
51833b5fcc9aSGregory Herrero 		if (!hsotg->desc_hsisoc_cache) {
51843b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
51853b5fcc9aSGregory Herrero 				"unable to create dwc2 hs isoc desc cache\n");
51863b5fcc9aSGregory Herrero 
51873b5fcc9aSGregory Herrero 			kmem_cache_destroy(hsotg->desc_gen_cache);
51883b5fcc9aSGregory Herrero 
51893b5fcc9aSGregory Herrero 			/*
51903b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
51913b5fcc9aSGregory Herrero 			 * usable.
51923b5fcc9aSGregory Herrero 			 */
519395832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
519495832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
51953b5fcc9aSGregory Herrero 		}
51963b5fcc9aSGregory Herrero 	}
51973b5fcc9aSGregory Herrero 
5198af424a41SWilliam Wu 	if (hsotg->params.host_dma) {
5199af424a41SWilliam Wu 		/*
5200af424a41SWilliam Wu 		 * Create kmem caches to handle non-aligned buffer
5201af424a41SWilliam Wu 		 * in Buffer DMA mode.
5202af424a41SWilliam Wu 		 */
5203af424a41SWilliam Wu 		hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
5204af424a41SWilliam Wu 						DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
5205af424a41SWilliam Wu 						SLAB_CACHE_DMA, NULL);
5206af424a41SWilliam Wu 		if (!hsotg->unaligned_cache)
5207af424a41SWilliam Wu 			dev_err(hsotg->dev,
5208af424a41SWilliam Wu 				"unable to create dwc2 unaligned cache\n");
5209af424a41SWilliam Wu 	}
5210af424a41SWilliam Wu 
5211197ba5f4SPaul Zimmerman 	hsotg->otg_port = 1;
5212197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
5213197ba5f4SPaul Zimmerman 	hsotg->frame_list_dma = 0;
5214197ba5f4SPaul Zimmerman 	hsotg->periodic_qh_count = 0;
5215197ba5f4SPaul Zimmerman 
5216197ba5f4SPaul Zimmerman 	/* Initiate lx_state to L3 disconnected state */
5217197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L3;
5218197ba5f4SPaul Zimmerman 
5219197ba5f4SPaul Zimmerman 	hcd->self.otg_port = hsotg->otg_port;
5220197ba5f4SPaul Zimmerman 
5221197ba5f4SPaul Zimmerman 	/* Don't support SG list at this point */
5222197ba5f4SPaul Zimmerman 	hcd->self.sg_tablesize = 0;
5223197ba5f4SPaul Zimmerman 
52249df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
52259df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, &hcd->self);
52269df4ceacSMian Yousaf Kaukab 
5227197ba5f4SPaul Zimmerman 	/*
5228197ba5f4SPaul Zimmerman 	 * Finish generic HCD initialization and start the HCD. This function
5229197ba5f4SPaul Zimmerman 	 * allocates the DMA buffer pool, registers the USB bus, requests the
5230197ba5f4SPaul Zimmerman 	 * IRQ line, and calls hcd_start method.
5231197ba5f4SPaul Zimmerman 	 */
52324fe160d5SHeiner Kallweit 	retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
5233197ba5f4SPaul Zimmerman 	if (retval < 0)
52343b5fcc9aSGregory Herrero 		goto error4;
5235197ba5f4SPaul Zimmerman 
5236ec513b16SLinus Torvalds 	device_wakeup_enable(hcd->self.controller);
5237ec513b16SLinus Torvalds 
5238197ba5f4SPaul Zimmerman 	dwc2_hcd_dump_state(hsotg);
5239197ba5f4SPaul Zimmerman 
5240197ba5f4SPaul Zimmerman 	dwc2_enable_global_interrupts(hsotg);
5241197ba5f4SPaul Zimmerman 
5242197ba5f4SPaul Zimmerman 	return 0;
5243197ba5f4SPaul Zimmerman 
52443b5fcc9aSGregory Herrero error4:
5245af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
52463b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5247af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
5248197ba5f4SPaul Zimmerman error3:
5249197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5250197ba5f4SPaul Zimmerman error2:
5251197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5252197ba5f4SPaul Zimmerman error1:
5253197ba5f4SPaul Zimmerman 
5254197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5255197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5256197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5257197ba5f4SPaul Zimmerman #endif
5258197ba5f4SPaul Zimmerman 
5259197ba5f4SPaul Zimmerman 	dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
5260197ba5f4SPaul Zimmerman 	return retval;
5261197ba5f4SPaul Zimmerman }
5262197ba5f4SPaul Zimmerman 
5263197ba5f4SPaul Zimmerman /*
5264197ba5f4SPaul Zimmerman  * Removes the HCD.
5265197ba5f4SPaul Zimmerman  * Frees memory and resources associated with the HCD and deregisters the bus.
5266197ba5f4SPaul Zimmerman  */
5267197ba5f4SPaul Zimmerman void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
5268197ba5f4SPaul Zimmerman {
5269197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5270197ba5f4SPaul Zimmerman 
5271197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
5272197ba5f4SPaul Zimmerman 
5273197ba5f4SPaul Zimmerman 	hcd = dwc2_hsotg_to_hcd(hsotg);
5274197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
5275197ba5f4SPaul Zimmerman 
5276197ba5f4SPaul Zimmerman 	if (!hcd) {
5277197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
5278197ba5f4SPaul Zimmerman 			__func__);
5279197ba5f4SPaul Zimmerman 		return;
5280197ba5f4SPaul Zimmerman 	}
5281197ba5f4SPaul Zimmerman 
52829df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
52839df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, NULL);
52849df4ceacSMian Yousaf Kaukab 
5285197ba5f4SPaul Zimmerman 	usb_remove_hcd(hcd);
5286197ba5f4SPaul Zimmerman 	hsotg->priv = NULL;
52873b5fcc9aSGregory Herrero 
5288af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
52893b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5290af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
52913b5fcc9aSGregory Herrero 
5292197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5293197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5294197ba5f4SPaul Zimmerman 
5295197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5296197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5297197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5298197ba5f4SPaul Zimmerman #endif
5299197ba5f4SPaul Zimmerman }
530058e52ff6SJohn Youn 
530158e52ff6SJohn Youn /**
530258e52ff6SJohn Youn  * dwc2_backup_host_registers() - Backup controller host registers.
530358e52ff6SJohn Youn  * When suspending usb bus, registers needs to be backuped
530458e52ff6SJohn Youn  * if controller power is disabled once suspended.
530558e52ff6SJohn Youn  *
530658e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
530758e52ff6SJohn Youn  */
530858e52ff6SJohn Youn int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
530958e52ff6SJohn Youn {
531058e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
531158e52ff6SJohn Youn 	int i;
531258e52ff6SJohn Youn 
531358e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
531458e52ff6SJohn Youn 
531558e52ff6SJohn Youn 	/* Backup Host regs */
531658e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
5317f25c42b8SGevorg Sahakyan 	hr->hcfg = dwc2_readl(hsotg, HCFG);
5318f25c42b8SGevorg Sahakyan 	hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
5319bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5320f25c42b8SGevorg Sahakyan 		hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
532158e52ff6SJohn Youn 
532258e52ff6SJohn Youn 	hr->hprt0 = dwc2_read_hprt0(hsotg);
5323f25c42b8SGevorg Sahakyan 	hr->hfir = dwc2_readl(hsotg, HFIR);
5324f25c42b8SGevorg Sahakyan 	hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
532558e52ff6SJohn Youn 	hr->valid = true;
532658e52ff6SJohn Youn 
532758e52ff6SJohn Youn 	return 0;
532858e52ff6SJohn Youn }
532958e52ff6SJohn Youn 
533058e52ff6SJohn Youn /**
533158e52ff6SJohn Youn  * dwc2_restore_host_registers() - Restore controller host registers.
533258e52ff6SJohn Youn  * When resuming usb bus, device registers needs to be restored
533358e52ff6SJohn Youn  * if controller power were disabled.
533458e52ff6SJohn Youn  *
533558e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
533658e52ff6SJohn Youn  */
533758e52ff6SJohn Youn int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
533858e52ff6SJohn Youn {
533958e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
534058e52ff6SJohn Youn 	int i;
534158e52ff6SJohn Youn 
534258e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
534358e52ff6SJohn Youn 
534458e52ff6SJohn Youn 	/* Restore host regs */
534558e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
534658e52ff6SJohn Youn 	if (!hr->valid) {
534758e52ff6SJohn Youn 		dev_err(hsotg->dev, "%s: no host registers to restore\n",
534858e52ff6SJohn Youn 			__func__);
534958e52ff6SJohn Youn 		return -EINVAL;
535058e52ff6SJohn Youn 	}
535158e52ff6SJohn Youn 	hr->valid = false;
535258e52ff6SJohn Youn 
5353f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5354f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
535558e52ff6SJohn Youn 
5356bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5357f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
535858e52ff6SJohn Youn 
5359f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hprt0, HPRT0);
5360f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hfir, HFIR);
5361f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
536258e52ff6SJohn Youn 	hsotg->frame_number = 0;
536358e52ff6SJohn Youn 
536458e52ff6SJohn Youn 	return 0;
536558e52ff6SJohn Youn }
5366c5c403dcSVardan Mikayelyan 
5367c5c403dcSVardan Mikayelyan /**
5368c5c403dcSVardan Mikayelyan  * dwc2_host_enter_hibernation() - Put controller in Hibernation.
5369c5c403dcSVardan Mikayelyan  *
5370c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5371c5c403dcSVardan Mikayelyan  */
5372c5c403dcSVardan Mikayelyan int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
5373c5c403dcSVardan Mikayelyan {
5374c5c403dcSVardan Mikayelyan 	unsigned long flags;
5375c5c403dcSVardan Mikayelyan 	int ret = 0;
5376c5c403dcSVardan Mikayelyan 	u32 hprt0;
5377c5c403dcSVardan Mikayelyan 	u32 pcgcctl;
5378c5c403dcSVardan Mikayelyan 	u32 gusbcfg;
5379c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5380c5c403dcSVardan Mikayelyan 
5381c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
5382c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_global_registers(hsotg);
5383c5c403dcSVardan Mikayelyan 	if (ret) {
5384c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5385c5c403dcSVardan Mikayelyan 			__func__);
5386c5c403dcSVardan Mikayelyan 		return ret;
5387c5c403dcSVardan Mikayelyan 	}
5388c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_host_registers(hsotg);
5389c5c403dcSVardan Mikayelyan 	if (ret) {
5390c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5391c5c403dcSVardan Mikayelyan 			__func__);
5392c5c403dcSVardan Mikayelyan 		return ret;
5393c5c403dcSVardan Mikayelyan 	}
5394c5c403dcSVardan Mikayelyan 
5395c5c403dcSVardan Mikayelyan 	/* Enter USB Suspend Mode */
5396f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5397c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_SUSP;
5398c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5399f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5400c5c403dcSVardan Mikayelyan 
5401c5c403dcSVardan Mikayelyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
54025e3bbae8SArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
540307b8dc55SColin Ian King 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
5404c5c403dcSVardan Mikayelyan 
5405c5c403dcSVardan Mikayelyan 	/*
5406c5c403dcSVardan Mikayelyan 	 * We need to disable interrupts to prevent servicing of any IRQ
5407c5c403dcSVardan Mikayelyan 	 * during going to hibernation
5408c5c403dcSVardan Mikayelyan 	 */
5409c5c403dcSVardan Mikayelyan 	spin_lock_irqsave(&hsotg->lock, flags);
5410c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L2;
5411c5c403dcSVardan Mikayelyan 
5412f25c42b8SGevorg Sahakyan 	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
5413c5c403dcSVardan Mikayelyan 	if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
5414c5c403dcSVardan Mikayelyan 		/* ULPI interface */
5415c5c403dcSVardan Mikayelyan 		/* Suspend the Phy Clock */
5416f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5417c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5418f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5419c5c403dcSVardan Mikayelyan 		udelay(10);
5420c5c403dcSVardan Mikayelyan 
5421f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5422c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5423f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5424c5c403dcSVardan Mikayelyan 		udelay(10);
5425c5c403dcSVardan Mikayelyan 	} else {
5426c5c403dcSVardan Mikayelyan 		/* UTMI+ Interface */
5427f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5428c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5429f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5430c5c403dcSVardan Mikayelyan 		udelay(10);
5431c5c403dcSVardan Mikayelyan 
5432f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5433c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5434f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5435c5c403dcSVardan Mikayelyan 		udelay(10);
5436c5c403dcSVardan Mikayelyan 	}
5437c5c403dcSVardan Mikayelyan 
5438c5c403dcSVardan Mikayelyan 	/* Enable interrupts from wake up logic */
5439f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5440c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PMUINTSEL;
5441f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5442c5c403dcSVardan Mikayelyan 	udelay(10);
5443c5c403dcSVardan Mikayelyan 
5444c5c403dcSVardan Mikayelyan 	/* Unmask host mode interrupts in GPWRDN */
5445f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5446c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_DISCONN_DET_MSK;
5447c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5448c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5449f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5450c5c403dcSVardan Mikayelyan 	udelay(10);
5451c5c403dcSVardan Mikayelyan 
5452c5c403dcSVardan Mikayelyan 	/* Enable Power Down Clamp */
5453f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5454c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNCLMP;
5455f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5456c5c403dcSVardan Mikayelyan 	udelay(10);
5457c5c403dcSVardan Mikayelyan 
5458c5c403dcSVardan Mikayelyan 	/* Switch off VDD */
5459f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5460c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNSWTCH;
5461f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5462c5c403dcSVardan Mikayelyan 
5463c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 1;
5464c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 1;
5465c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation completed\n");
5466c5c403dcSVardan Mikayelyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
5467c5c403dcSVardan Mikayelyan 	return ret;
5468c5c403dcSVardan Mikayelyan }
5469c5c403dcSVardan Mikayelyan 
5470c5c403dcSVardan Mikayelyan /*
5471c5c403dcSVardan Mikayelyan  * dwc2_host_exit_hibernation()
5472c5c403dcSVardan Mikayelyan  *
5473c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5474c5c403dcSVardan Mikayelyan  * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5475c5c403dcSVardan Mikayelyan  * @param reset: indicates whether resume is initiated by Reset.
5476c5c403dcSVardan Mikayelyan  *
5477c5c403dcSVardan Mikayelyan  * Return: non-zero if failed to enter to hibernation.
5478c5c403dcSVardan Mikayelyan  *
5479c5c403dcSVardan Mikayelyan  * This function is for exiting from Host mode hibernation by
5480c5c403dcSVardan Mikayelyan  * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
5481c5c403dcSVardan Mikayelyan  */
5482c5c403dcSVardan Mikayelyan int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
5483c5c403dcSVardan Mikayelyan 			       int reset)
5484c5c403dcSVardan Mikayelyan {
5485c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5486c5c403dcSVardan Mikayelyan 	u32 hprt0;
5487c5c403dcSVardan Mikayelyan 	int ret = 0;
5488c5c403dcSVardan Mikayelyan 	struct dwc2_gregs_backup *gr;
5489c5c403dcSVardan Mikayelyan 	struct dwc2_hregs_backup *hr;
5490c5c403dcSVardan Mikayelyan 
5491c5c403dcSVardan Mikayelyan 	gr = &hsotg->gr_backup;
5492c5c403dcSVardan Mikayelyan 	hr = &hsotg->hr_backup;
5493c5c403dcSVardan Mikayelyan 
5494c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev,
5495c5c403dcSVardan Mikayelyan 		"%s: called with rem_wakeup = %d reset = %d\n",
5496c5c403dcSVardan Mikayelyan 		__func__, rem_wakeup, reset);
5497c5c403dcSVardan Mikayelyan 
5498c5c403dcSVardan Mikayelyan 	dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
5499c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5500c5c403dcSVardan Mikayelyan 
5501c5c403dcSVardan Mikayelyan 	/*
5502c5c403dcSVardan Mikayelyan 	 * This step is not described in functional spec but if not wait for
5503c5c403dcSVardan Mikayelyan 	 * this delay, mismatch interrupts occurred because just after restore
5504c5c403dcSVardan Mikayelyan 	 * core is in Device mode(gintsts.curmode == 0)
5505c5c403dcSVardan Mikayelyan 	 */
5506c5c403dcSVardan Mikayelyan 	mdelay(100);
5507c5c403dcSVardan Mikayelyan 
5508c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5509f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5510c5c403dcSVardan Mikayelyan 
5511c5c403dcSVardan Mikayelyan 	/* De-assert Restore */
5512f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5513c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_RESTORE;
5514f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5515c5c403dcSVardan Mikayelyan 	udelay(10);
5516c5c403dcSVardan Mikayelyan 
5517c5c403dcSVardan Mikayelyan 	/* Restore GUSBCFG, HCFG */
5518f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5519f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5520c5c403dcSVardan Mikayelyan 
5521c5c403dcSVardan Mikayelyan 	/* De-assert Wakeup Logic */
5522f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5523c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_PMUACTV;
5524f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5525c5c403dcSVardan Mikayelyan 	udelay(10);
5526c5c403dcSVardan Mikayelyan 
5527c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5528c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5529c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5530c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5531f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5532c5c403dcSVardan Mikayelyan 
5533c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5534c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5535c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5536c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5537c5c403dcSVardan Mikayelyan 
5538c5c403dcSVardan Mikayelyan 	if (reset) {
5539c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RST;
5540f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5541c5c403dcSVardan Mikayelyan 
5542c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5543c5c403dcSVardan Mikayelyan 		mdelay(60);
5544c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RST;
5545f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5546c5c403dcSVardan Mikayelyan 	} else {
5547c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RES;
5548f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5549c5c403dcSVardan Mikayelyan 
5550c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5551c5c403dcSVardan Mikayelyan 		mdelay(100);
5552c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RES;
5553f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5554c5c403dcSVardan Mikayelyan 	}
5555c5c403dcSVardan Mikayelyan 	/* Clear all interrupt status */
5556f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5557c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_CONNDET;
5558c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_ENACHG;
5559c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5560f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5561c5c403dcSVardan Mikayelyan 
5562f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5563c5c403dcSVardan Mikayelyan 
5564c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5565f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5566c5c403dcSVardan Mikayelyan 
5567c5c403dcSVardan Mikayelyan 	/* Restore global registers */
5568c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_global_registers(hsotg);
5569c5c403dcSVardan Mikayelyan 	if (ret) {
5570c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore registers\n",
5571c5c403dcSVardan Mikayelyan 			__func__);
5572c5c403dcSVardan Mikayelyan 		return ret;
5573c5c403dcSVardan Mikayelyan 	}
5574c5c403dcSVardan Mikayelyan 
5575c5c403dcSVardan Mikayelyan 	/* Restore host registers */
5576c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_host_registers(hsotg);
5577c5c403dcSVardan Mikayelyan 	if (ret) {
5578c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5579c5c403dcSVardan Mikayelyan 			__func__);
5580c5c403dcSVardan Mikayelyan 		return ret;
5581c5c403dcSVardan Mikayelyan 	}
5582c5c403dcSVardan Mikayelyan 
558322bb5cfdSArtur Petrosyan 	dwc2_hcd_rem_wakeup(hsotg);
558422bb5cfdSArtur Petrosyan 
5585c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5586c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 0;
5587c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L0;
5588c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
5589c5c403dcSVardan Mikayelyan 	return ret;
5590c5c403dcSVardan Mikayelyan }
5591c846b03fSDouglas Anderson 
5592c846b03fSDouglas Anderson bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
5593c846b03fSDouglas Anderson {
5594c846b03fSDouglas Anderson 	struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
5595c846b03fSDouglas Anderson 
5596c846b03fSDouglas Anderson 	/* If the controller isn't allowed to wakeup then we can power off. */
5597c846b03fSDouglas Anderson 	if (!device_may_wakeup(dwc2->dev))
5598c846b03fSDouglas Anderson 		return true;
5599c846b03fSDouglas Anderson 
5600c846b03fSDouglas Anderson 	/*
5601c846b03fSDouglas Anderson 	 * We don't want to power off the PHY if something under the
5602c846b03fSDouglas Anderson 	 * root hub has wakeup enabled.
5603c846b03fSDouglas Anderson 	 */
5604c846b03fSDouglas Anderson 	if (usb_wakeup_enabled_descendants(root_hub))
5605c846b03fSDouglas Anderson 		return false;
5606c846b03fSDouglas Anderson 
5607c846b03fSDouglas Anderson 	/* No reason to keep the PHY powered, so allow poweroff */
5608c846b03fSDouglas Anderson 	return true;
5609c846b03fSDouglas Anderson }
5610*9ce9e5adSArtur Petrosyan 
5611*9ce9e5adSArtur Petrosyan /**
5612*9ce9e5adSArtur Petrosyan  * dwc2_host_enter_partial_power_down() - Put controller in partial
5613*9ce9e5adSArtur Petrosyan  * power down.
5614*9ce9e5adSArtur Petrosyan  *
5615*9ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
5616*9ce9e5adSArtur Petrosyan  *
5617*9ce9e5adSArtur Petrosyan  * Return: non-zero if failed to enter host partial power down.
5618*9ce9e5adSArtur Petrosyan  *
5619*9ce9e5adSArtur Petrosyan  * This function is for entering Host mode partial power down.
5620*9ce9e5adSArtur Petrosyan  */
5621*9ce9e5adSArtur Petrosyan int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg)
5622*9ce9e5adSArtur Petrosyan {
5623*9ce9e5adSArtur Petrosyan 	u32 pcgcctl;
5624*9ce9e5adSArtur Petrosyan 	u32 hprt0;
5625*9ce9e5adSArtur Petrosyan 	int ret = 0;
5626*9ce9e5adSArtur Petrosyan 
5627*9ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down started.\n");
5628*9ce9e5adSArtur Petrosyan 
5629*9ce9e5adSArtur Petrosyan 	/* Put this port in suspend mode. */
5630*9ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
5631*9ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
5632*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5633*9ce9e5adSArtur Petrosyan 	udelay(5);
5634*9ce9e5adSArtur Petrosyan 
5635*9ce9e5adSArtur Petrosyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
5636*9ce9e5adSArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
5637*9ce9e5adSArtur Petrosyan 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
5638*9ce9e5adSArtur Petrosyan 
5639*9ce9e5adSArtur Petrosyan 	/* Backup all registers */
5640*9ce9e5adSArtur Petrosyan 	ret = dwc2_backup_global_registers(hsotg);
5641*9ce9e5adSArtur Petrosyan 	if (ret) {
5642*9ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5643*9ce9e5adSArtur Petrosyan 			__func__);
5644*9ce9e5adSArtur Petrosyan 		return ret;
5645*9ce9e5adSArtur Petrosyan 	}
5646*9ce9e5adSArtur Petrosyan 
5647*9ce9e5adSArtur Petrosyan 	ret = dwc2_backup_host_registers(hsotg);
5648*9ce9e5adSArtur Petrosyan 	if (ret) {
5649*9ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5650*9ce9e5adSArtur Petrosyan 			__func__);
5651*9ce9e5adSArtur Petrosyan 		return ret;
5652*9ce9e5adSArtur Petrosyan 	}
5653*9ce9e5adSArtur Petrosyan 
5654*9ce9e5adSArtur Petrosyan 	/*
5655*9ce9e5adSArtur Petrosyan 	 * Clear any pending interrupts since dwc2 will not be able to
5656*9ce9e5adSArtur Petrosyan 	 * clear them after entering partial_power_down.
5657*9ce9e5adSArtur Petrosyan 	 */
5658*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5659*9ce9e5adSArtur Petrosyan 
5660*9ce9e5adSArtur Petrosyan 	/* Put the controller in low power state */
5661*9ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
5662*9ce9e5adSArtur Petrosyan 
5663*9ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_PWRCLMP;
5664*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
5665*9ce9e5adSArtur Petrosyan 	udelay(5);
5666*9ce9e5adSArtur Petrosyan 
5667*9ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
5668*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
5669*9ce9e5adSArtur Petrosyan 	udelay(5);
5670*9ce9e5adSArtur Petrosyan 
5671*9ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_STOPPCLK;
5672*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
5673*9ce9e5adSArtur Petrosyan 
5674*9ce9e5adSArtur Petrosyan 	/* Set in_ppd flag to 1 as here core enters suspend. */
5675*9ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 1;
5676*9ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
5677*9ce9e5adSArtur Petrosyan 	hsotg->bus_suspended = true;
5678*9ce9e5adSArtur Petrosyan 
5679*9ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down completed.\n");
5680*9ce9e5adSArtur Petrosyan 
5681*9ce9e5adSArtur Petrosyan 	return ret;
5682*9ce9e5adSArtur Petrosyan }
5683*9ce9e5adSArtur Petrosyan 
5684*9ce9e5adSArtur Petrosyan /*
5685*9ce9e5adSArtur Petrosyan  * dwc2_host_exit_partial_power_down() - Exit controller from host partial
5686*9ce9e5adSArtur Petrosyan  * power down.
5687*9ce9e5adSArtur Petrosyan  *
5688*9ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
5689*9ce9e5adSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by Reset.
5690*9ce9e5adSArtur Petrosyan  * @restore: indicates whether need to restore the registers or not.
5691*9ce9e5adSArtur Petrosyan  *
5692*9ce9e5adSArtur Petrosyan  * Return: non-zero if failed to exit host partial power down.
5693*9ce9e5adSArtur Petrosyan  *
5694*9ce9e5adSArtur Petrosyan  * This function is for exiting from Host mode partial power down.
5695*9ce9e5adSArtur Petrosyan  */
5696*9ce9e5adSArtur Petrosyan int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg,
5697*9ce9e5adSArtur Petrosyan 				      int rem_wakeup, bool restore)
5698*9ce9e5adSArtur Petrosyan {
5699*9ce9e5adSArtur Petrosyan 	u32 pcgcctl;
5700*9ce9e5adSArtur Petrosyan 	int ret = 0;
5701*9ce9e5adSArtur Petrosyan 	u32 hprt0;
5702*9ce9e5adSArtur Petrosyan 
5703*9ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down started.\n");
5704*9ce9e5adSArtur Petrosyan 
5705*9ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
5706*9ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_STOPPCLK;
5707*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
5708*9ce9e5adSArtur Petrosyan 	udelay(5);
5709*9ce9e5adSArtur Petrosyan 
5710*9ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
5711*9ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_PWRCLMP;
5712*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
5713*9ce9e5adSArtur Petrosyan 	udelay(5);
5714*9ce9e5adSArtur Petrosyan 
5715*9ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
5716*9ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
5717*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
5718*9ce9e5adSArtur Petrosyan 
5719*9ce9e5adSArtur Petrosyan 	udelay(100);
5720*9ce9e5adSArtur Petrosyan 	if (restore) {
5721*9ce9e5adSArtur Petrosyan 		ret = dwc2_restore_global_registers(hsotg);
5722*9ce9e5adSArtur Petrosyan 		if (ret) {
5723*9ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore registers\n",
5724*9ce9e5adSArtur Petrosyan 				__func__);
5725*9ce9e5adSArtur Petrosyan 			return ret;
5726*9ce9e5adSArtur Petrosyan 		}
5727*9ce9e5adSArtur Petrosyan 
5728*9ce9e5adSArtur Petrosyan 		ret = dwc2_restore_host_registers(hsotg);
5729*9ce9e5adSArtur Petrosyan 		if (ret) {
5730*9ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5731*9ce9e5adSArtur Petrosyan 				__func__);
5732*9ce9e5adSArtur Petrosyan 			return ret;
5733*9ce9e5adSArtur Petrosyan 		}
5734*9ce9e5adSArtur Petrosyan 	}
5735*9ce9e5adSArtur Petrosyan 
5736*9ce9e5adSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
5737*9ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
5738*9ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_RES;
5739*9ce9e5adSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
5740*9ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5741*9ce9e5adSArtur Petrosyan 	udelay(5);
5742*9ce9e5adSArtur Petrosyan 
5743*9ce9e5adSArtur Petrosyan 	if (!rem_wakeup) {
5744*9ce9e5adSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
5745*9ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
5746*9ce9e5adSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
5747*9ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5748*9ce9e5adSArtur Petrosyan 
5749*9ce9e5adSArtur Petrosyan 		hsotg->bus_suspended = false;
5750*9ce9e5adSArtur Petrosyan 	} else {
5751*9ce9e5adSArtur Petrosyan 		/* Turn on the port power bit. */
5752*9ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
5753*9ce9e5adSArtur Petrosyan 		hprt0 |= HPRT0_PWR;
5754*9ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5755*9ce9e5adSArtur Petrosyan 
5756*9ce9e5adSArtur Petrosyan 		/* Connect hcd. */
5757*9ce9e5adSArtur Petrosyan 		dwc2_hcd_connect(hsotg);
5758*9ce9e5adSArtur Petrosyan 
5759*9ce9e5adSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
5760*9ce9e5adSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
5761*9ce9e5adSArtur Petrosyan 	}
5762*9ce9e5adSArtur Petrosyan 
5763*9ce9e5adSArtur Petrosyan 	/* Set lx_state to and in_ppd to 0 as here core exits from suspend. */
5764*9ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 0;
5765*9ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L0;
5766*9ce9e5adSArtur Petrosyan 
5767*9ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n");
5768*9ce9e5adSArtur Petrosyan 	return ret;
5769*9ce9e5adSArtur Petrosyan }
5770