xref: /linux/drivers/usb/dwc2/hcd.c (revision a23e1966932464e1c5226cb9ac4ce1d5fc10ba22)
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 
8197ba5f4SPaul Zimmerman /*
9197ba5f4SPaul Zimmerman  * This file contains the core HCD code, and implements the Linux hc_driver
10197ba5f4SPaul Zimmerman  * API
11197ba5f4SPaul Zimmerman  */
12197ba5f4SPaul Zimmerman #include <linux/kernel.h>
13197ba5f4SPaul Zimmerman #include <linux/module.h>
14197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
15197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
16348becdcSHeiner Kallweit #include <linux/platform_device.h>
17197ba5f4SPaul Zimmerman #include <linux/dma-mapping.h>
18197ba5f4SPaul Zimmerman #include <linux/delay.h>
19197ba5f4SPaul Zimmerman #include <linux/io.h>
20197ba5f4SPaul Zimmerman #include <linux/slab.h>
21197ba5f4SPaul Zimmerman #include <linux/usb.h>
22197ba5f4SPaul Zimmerman 
23197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
24197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
252c8845feSAmelie Delaunay #include <linux/usb/of.h>
26197ba5f4SPaul Zimmerman 
27197ba5f4SPaul Zimmerman #include "core.h"
28197ba5f4SPaul Zimmerman #include "hcd.h"
29197ba5f4SPaul Zimmerman 
30b02038faSJohn Youn /*
31b02038faSJohn Youn  * =========================================================================
32b02038faSJohn Youn  *  Host Core Layer Functions
33b02038faSJohn Youn  * =========================================================================
34b02038faSJohn Youn  */
35b02038faSJohn Youn 
36b02038faSJohn Youn /**
37b02038faSJohn Youn  * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
38b02038faSJohn Youn  * used in both device and host modes
39b02038faSJohn Youn  *
40b02038faSJohn Youn  * @hsotg: Programming view of the DWC_otg controller
41b02038faSJohn Youn  */
42b02038faSJohn Youn static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
43b02038faSJohn Youn {
44b02038faSJohn Youn 	u32 intmsk;
45b02038faSJohn Youn 
46b02038faSJohn Youn 	/* Clear any pending OTG Interrupts */
47f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GOTGINT);
48b02038faSJohn Youn 
49b02038faSJohn Youn 	/* Clear any pending interrupts */
50f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
51b02038faSJohn Youn 
52b02038faSJohn Youn 	/* Enable the interrupts in the GINTMSK */
53b02038faSJohn Youn 	intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
54b02038faSJohn Youn 
5595832c00SJohn Youn 	if (!hsotg->params.host_dma)
56b02038faSJohn Youn 		intmsk |= GINTSTS_RXFLVL;
5795832c00SJohn Youn 	if (!hsotg->params.external_id_pin_ctl)
58b02038faSJohn Youn 		intmsk |= GINTSTS_CONIDSTSCHNG;
59b02038faSJohn Youn 
60b02038faSJohn Youn 	intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
61b02038faSJohn Youn 		  GINTSTS_SESSREQINT;
62b02038faSJohn Youn 
63376f0401SSevak Arakelyan 	if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
64376f0401SSevak Arakelyan 		intmsk |= GINTSTS_LPMTRANRCVD;
65376f0401SSevak Arakelyan 
66f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
67b02038faSJohn Youn }
68b02038faSJohn Youn 
69b02038faSJohn Youn static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
70b02038faSJohn Youn {
71f25c42b8SGevorg Sahakyan 	u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
72b02038faSJohn Youn 
73b02038faSJohn Youn 	switch (hsotg->hw_params.arch) {
74b02038faSJohn Youn 	case GHWCFG2_EXT_DMA_ARCH:
75b02038faSJohn Youn 		dev_err(hsotg->dev, "External DMA Mode not supported\n");
76b02038faSJohn Youn 		return -EINVAL;
77b02038faSJohn Youn 
78b02038faSJohn Youn 	case GHWCFG2_INT_DMA_ARCH:
79b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Internal DMA Mode\n");
80bea8e86cSJohn Youn 		if (hsotg->params.ahbcfg != -1) {
81b02038faSJohn Youn 			ahbcfg &= GAHBCFG_CTRL_MASK;
82bea8e86cSJohn Youn 			ahbcfg |= hsotg->params.ahbcfg &
83b02038faSJohn Youn 				  ~GAHBCFG_CTRL_MASK;
84b02038faSJohn Youn 		}
85b02038faSJohn Youn 		break;
86b02038faSJohn Youn 
87b02038faSJohn Youn 	case GHWCFG2_SLAVE_ONLY_ARCH:
88b02038faSJohn Youn 	default:
89b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Slave Only Mode\n");
90b02038faSJohn Youn 		break;
91b02038faSJohn Youn 	}
92b02038faSJohn Youn 
9395832c00SJohn Youn 	if (hsotg->params.host_dma)
94b02038faSJohn Youn 		ahbcfg |= GAHBCFG_DMA_EN;
959d729a7aSRazmik Karapetyan 	else
969d729a7aSRazmik Karapetyan 		hsotg->params.dma_desc_enable = false;
97b02038faSJohn Youn 
98f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
99b02038faSJohn Youn 
100b02038faSJohn Youn 	return 0;
101b02038faSJohn Youn }
102b02038faSJohn Youn 
103b02038faSJohn Youn static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
104b02038faSJohn Youn {
105b02038faSJohn Youn 	u32 usbcfg;
106b02038faSJohn Youn 
107f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
108b02038faSJohn Youn 	usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
109b02038faSJohn Youn 
110b02038faSJohn Youn 	switch (hsotg->hw_params.op_mode) {
111b02038faSJohn Youn 	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
112f5c8a6cbSFabrice Gasnier 		if (hsotg->params.otg_caps.hnp_support &&
113f5c8a6cbSFabrice Gasnier 		    hsotg->params.otg_caps.srp_support)
114b02038faSJohn Youn 			usbcfg |= GUSBCFG_HNPCAP;
115f5c8a6cbSFabrice Gasnier 		fallthrough;
116b02038faSJohn Youn 
117b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
118b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
119b02038faSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
120f5c8a6cbSFabrice Gasnier 		if (hsotg->params.otg_caps.srp_support)
121b02038faSJohn Youn 			usbcfg |= GUSBCFG_SRPCAP;
122b02038faSJohn Youn 		break;
123b02038faSJohn Youn 
124b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
125b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
126b02038faSJohn Youn 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
127b02038faSJohn Youn 	default:
128b02038faSJohn Youn 		break;
129b02038faSJohn Youn 	}
130b02038faSJohn Youn 
131f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
132b02038faSJohn Youn }
133b02038faSJohn Youn 
134531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
135531ef5ebSAmelie Delaunay {
136e0f681c2SFabrice Gasnier 	if (hsotg->vbus_supply)
137531ef5ebSAmelie Delaunay 		return regulator_enable(hsotg->vbus_supply);
138e0f681c2SFabrice Gasnier 
139e0f681c2SFabrice Gasnier 	return 0;
140531ef5ebSAmelie Delaunay }
141531ef5ebSAmelie Delaunay 
142531ef5ebSAmelie Delaunay static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
143531ef5ebSAmelie Delaunay {
144531ef5ebSAmelie Delaunay 	if (hsotg->vbus_supply)
145531ef5ebSAmelie Delaunay 		return regulator_disable(hsotg->vbus_supply);
146531ef5ebSAmelie Delaunay 
147531ef5ebSAmelie Delaunay 	return 0;
148531ef5ebSAmelie Delaunay }
149531ef5ebSAmelie Delaunay 
150b02038faSJohn Youn /**
151b02038faSJohn Youn  * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
152b02038faSJohn Youn  *
153b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
154b02038faSJohn Youn  */
155b02038faSJohn Youn static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
156b02038faSJohn Youn {
157b02038faSJohn Youn 	u32 intmsk;
158b02038faSJohn Youn 
159b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s()\n", __func__);
160b02038faSJohn Youn 
161b02038faSJohn Youn 	/* Disable all interrupts */
162f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
163f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HAINTMSK);
164b02038faSJohn Youn 
165b02038faSJohn Youn 	/* Enable the common interrupts */
166b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
167b02038faSJohn Youn 
168b02038faSJohn Youn 	/* Enable host mode interrupts without disturbing common interrupts */
169f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
170b02038faSJohn Youn 	intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
171f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
172b02038faSJohn Youn }
173b02038faSJohn Youn 
174b02038faSJohn Youn /**
175b02038faSJohn Youn  * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
176b02038faSJohn Youn  *
177b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
178b02038faSJohn Youn  */
179b02038faSJohn Youn static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
180b02038faSJohn Youn {
181f25c42b8SGevorg Sahakyan 	u32 intmsk = dwc2_readl(hsotg, GINTMSK);
182b02038faSJohn Youn 
183b02038faSJohn Youn 	/* Disable host mode interrupts without disturbing common interrupts */
184b02038faSJohn Youn 	intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
185b02038faSJohn Youn 		    GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
186f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
187b02038faSJohn Youn }
188b02038faSJohn Youn 
189b02038faSJohn Youn /*
190b02038faSJohn Youn  * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
191b02038faSJohn Youn  * For system that have a total fifo depth that is smaller than the default
192b02038faSJohn Youn  * RX + TX fifo size.
193b02038faSJohn Youn  *
194b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
195b02038faSJohn Youn  */
196b02038faSJohn Youn static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
197b02038faSJohn Youn {
198bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
199b02038faSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
200b02038faSJohn Youn 	u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
201b02038faSJohn Youn 
202b02038faSJohn Youn 	total_fifo_size = hw->total_fifo_size;
203b02038faSJohn Youn 	rxfsiz = params->host_rx_fifo_size;
204b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size;
205b02038faSJohn Youn 	ptxfsiz = params->host_perio_tx_fifo_size;
206b02038faSJohn Youn 
207b02038faSJohn Youn 	/*
208b02038faSJohn Youn 	 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
209b02038faSJohn Youn 	 * allocation with support for high bandwidth endpoints. Synopsys
210b02038faSJohn Youn 	 * defines MPS(Max Packet size) for a periodic EP=1024, and for
211b02038faSJohn Youn 	 * non-periodic as 512.
212b02038faSJohn Youn 	 */
213b02038faSJohn Youn 	if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
214b02038faSJohn Youn 		/*
215b02038faSJohn Youn 		 * For Buffer DMA mode/Scatter Gather DMA mode
216b02038faSJohn Youn 		 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
217b02038faSJohn Youn 		 * with n = number of host channel.
218b02038faSJohn Youn 		 * 2 * ((1024/4) + 2) = 516
219b02038faSJohn Youn 		 */
220b02038faSJohn Youn 		rxfsiz = 516 + hw->host_channels;
221b02038faSJohn Youn 
222b02038faSJohn Youn 		/*
223b02038faSJohn Youn 		 * min non-periodic tx fifo depth
224b02038faSJohn Youn 		 * 2 * (largest non-periodic USB packet used / 4)
225b02038faSJohn Youn 		 * 2 * (512/4) = 256
226b02038faSJohn Youn 		 */
227b02038faSJohn Youn 		nptxfsiz = 256;
228b02038faSJohn Youn 
229b02038faSJohn Youn 		/*
230b02038faSJohn Youn 		 * min periodic tx fifo depth
231b02038faSJohn Youn 		 * (largest packet size*MC)/4
232b02038faSJohn Youn 		 * (1024 * 3)/4 = 768
233b02038faSJohn Youn 		 */
234b02038faSJohn Youn 		ptxfsiz = 768;
235b02038faSJohn Youn 
236b02038faSJohn Youn 		params->host_rx_fifo_size = rxfsiz;
237b02038faSJohn Youn 		params->host_nperio_tx_fifo_size = nptxfsiz;
238b02038faSJohn Youn 		params->host_perio_tx_fifo_size = ptxfsiz;
239b02038faSJohn Youn 	}
240b02038faSJohn Youn 
241b02038faSJohn Youn 	/*
242b02038faSJohn Youn 	 * If the summation of RX, NPTX and PTX fifo sizes is still
243b02038faSJohn Youn 	 * bigger than the total_fifo_size, then we have a problem.
244b02038faSJohn Youn 	 *
245b02038faSJohn Youn 	 * We won't be able to allocate as many endpoints. Right now,
246b02038faSJohn Youn 	 * we're just printing an error message, but ideally this FIFO
247b02038faSJohn Youn 	 * allocation algorithm would be improved in the future.
248b02038faSJohn Youn 	 *
249b02038faSJohn Youn 	 * FIXME improve this FIFO allocation algorithm.
250b02038faSJohn Youn 	 */
251b02038faSJohn Youn 	if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
252b02038faSJohn Youn 		dev_err(hsotg->dev, "invalid fifo sizes\n");
253b02038faSJohn Youn }
254b02038faSJohn Youn 
255b02038faSJohn Youn static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
256b02038faSJohn Youn {
257bea8e86cSJohn Youn 	struct dwc2_core_params *params = &hsotg->params;
258b02038faSJohn Youn 	u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
259b02038faSJohn Youn 
260b02038faSJohn Youn 	if (!params->enable_dynamic_fifo)
261b02038faSJohn Youn 		return;
262b02038faSJohn Youn 
263b02038faSJohn Youn 	dwc2_calculate_dynamic_fifo(hsotg);
264b02038faSJohn Youn 
265b02038faSJohn Youn 	/* Rx FIFO */
266f25c42b8SGevorg Sahakyan 	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
267b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
268b02038faSJohn Youn 	grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
269b02038faSJohn Youn 	grxfsiz |= params->host_rx_fifo_size <<
270b02038faSJohn Youn 		   GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
271f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, grxfsiz, GRXFSIZ);
272b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
273f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GRXFSIZ));
274b02038faSJohn Youn 
275b02038faSJohn Youn 	/* Non-periodic Tx FIFO */
276b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
277f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
278b02038faSJohn Youn 	nptxfsiz = params->host_nperio_tx_fifo_size <<
279b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
280b02038faSJohn Youn 	nptxfsiz |= params->host_rx_fifo_size <<
281b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
282f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, nptxfsiz, GNPTXFSIZ);
283b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
284f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, GNPTXFSIZ));
285b02038faSJohn Youn 
286b02038faSJohn Youn 	/* Periodic Tx FIFO */
287b02038faSJohn Youn 	dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
288f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
289b02038faSJohn Youn 	hptxfsiz = params->host_perio_tx_fifo_size <<
290b02038faSJohn Youn 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
291b02038faSJohn Youn 	hptxfsiz |= (params->host_rx_fifo_size +
292b02038faSJohn Youn 		     params->host_nperio_tx_fifo_size) <<
293b02038faSJohn Youn 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
294f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hptxfsiz, HPTXFSIZ);
295b02038faSJohn Youn 	dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
296f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPTXFSIZ));
297b02038faSJohn Youn 
29895832c00SJohn Youn 	if (hsotg->params.en_multiple_tx_fifo &&
299e1f411d1SSevak Arakelyan 	    hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
300b02038faSJohn Youn 		/*
301e1f411d1SSevak Arakelyan 		 * This feature was implemented in 2.91a version
302b02038faSJohn Youn 		 * Global DFIFOCFG calculation for Host mode -
303b02038faSJohn Youn 		 * include RxFIFO, NPTXFIFO and HPTXFIFO
304b02038faSJohn Youn 		 */
305f25c42b8SGevorg Sahakyan 		dfifocfg = dwc2_readl(hsotg, GDFIFOCFG);
306b02038faSJohn Youn 		dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
307b02038faSJohn Youn 		dfifocfg |= (params->host_rx_fifo_size +
308b02038faSJohn Youn 			     params->host_nperio_tx_fifo_size +
309b02038faSJohn Youn 			     params->host_perio_tx_fifo_size) <<
310b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_SHIFT &
311b02038faSJohn Youn 			    GDFIFOCFG_EPINFOBASE_MASK;
312f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dfifocfg, GDFIFOCFG);
313b02038faSJohn Youn 	}
314b02038faSJohn Youn }
315b02038faSJohn Youn 
316b02038faSJohn Youn /**
317b02038faSJohn Youn  * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
318b02038faSJohn Youn  * the HFIR register according to PHY type and speed
319b02038faSJohn Youn  *
320b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
321b02038faSJohn Youn  *
322b02038faSJohn Youn  * NOTE: The caller can modify the value of the HFIR register only after the
323b02038faSJohn Youn  * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
324b02038faSJohn Youn  * has been set
325b02038faSJohn Youn  */
326b02038faSJohn Youn u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
327b02038faSJohn Youn {
328b02038faSJohn Youn 	u32 usbcfg;
329b02038faSJohn Youn 	u32 hprt0;
330b02038faSJohn Youn 	int clock = 60;	/* default value */
331b02038faSJohn Youn 
332f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
333f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
334b02038faSJohn Youn 
335b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
336b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_PHYIF16))
337b02038faSJohn Youn 		clock = 60;
338b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
339b02038faSJohn Youn 	    GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
340b02038faSJohn Youn 		clock = 48;
341b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
342b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
343b02038faSJohn Youn 		clock = 30;
344b02038faSJohn Youn 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
345b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
346b02038faSJohn Youn 		clock = 60;
347b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
348b02038faSJohn Youn 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
349b02038faSJohn Youn 		clock = 48;
350b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
351b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
352b02038faSJohn Youn 		clock = 48;
353b02038faSJohn Youn 	if ((usbcfg & GUSBCFG_PHYSEL) &&
354b02038faSJohn Youn 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
355b02038faSJohn Youn 		clock = 48;
356b02038faSJohn Youn 
357b02038faSJohn Youn 	if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
358b02038faSJohn Youn 		/* High speed case */
359b02038faSJohn Youn 		return 125 * clock - 1;
360b02038faSJohn Youn 
361b02038faSJohn Youn 	/* FS/LS case */
362b02038faSJohn Youn 	return 1000 * clock - 1;
363b02038faSJohn Youn }
364b02038faSJohn Youn 
365b02038faSJohn Youn /**
366b02038faSJohn Youn  * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
367b02038faSJohn Youn  * buffer
368b02038faSJohn Youn  *
3696fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of DWC_otg controller
370b02038faSJohn Youn  * @dest:    Destination buffer for the packet
371b02038faSJohn Youn  * @bytes:   Number of bytes to copy to the destination
372b02038faSJohn Youn  */
373b02038faSJohn Youn void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
374b02038faSJohn Youn {
375b02038faSJohn Youn 	u32 *data_buf = (u32 *)dest;
376b02038faSJohn Youn 	int word_count = (bytes + 3) / 4;
377b02038faSJohn Youn 	int i;
378b02038faSJohn Youn 
379b02038faSJohn Youn 	/*
380b02038faSJohn Youn 	 * Todo: Account for the case where dest is not dword aligned. This
381b02038faSJohn Youn 	 * requires reading data from the FIFO into a u32 temp buffer, then
382b02038faSJohn Youn 	 * moving it into the data buffer.
383b02038faSJohn Youn 	 */
384b02038faSJohn Youn 
385b02038faSJohn Youn 	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
386b02038faSJohn Youn 
387b02038faSJohn Youn 	for (i = 0; i < word_count; i++, data_buf++)
388f25c42b8SGevorg Sahakyan 		*data_buf = dwc2_readl(hsotg, HCFIFO(0));
389b02038faSJohn Youn }
390b02038faSJohn Youn 
391197ba5f4SPaul Zimmerman /**
392197ba5f4SPaul Zimmerman  * dwc2_dump_channel_info() - Prints the state of a host channel
393197ba5f4SPaul Zimmerman  *
394197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
395197ba5f4SPaul Zimmerman  * @chan:  Pointer to the channel to dump
396197ba5f4SPaul Zimmerman  *
397197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
398197ba5f4SPaul Zimmerman  *
399197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
400197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
401197ba5f4SPaul Zimmerman  */
402197ba5f4SPaul Zimmerman static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
403197ba5f4SPaul Zimmerman 				   struct dwc2_host_chan *chan)
404197ba5f4SPaul Zimmerman {
405197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
406bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
407197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
408197ba5f4SPaul Zimmerman 	u32 hcchar;
409197ba5f4SPaul Zimmerman 	u32 hcsplt;
410197ba5f4SPaul Zimmerman 	u32 hctsiz;
411197ba5f4SPaul Zimmerman 	u32 hc_dma;
412197ba5f4SPaul Zimmerman 	int i;
413197ba5f4SPaul Zimmerman 
414b02038faSJohn Youn 	if (!chan)
415197ba5f4SPaul Zimmerman 		return;
416197ba5f4SPaul Zimmerman 
417f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
418f25c42b8SGevorg Sahakyan 	hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
419f25c42b8SGevorg Sahakyan 	hctsiz = dwc2_readl(hsotg, HCTSIZ(chan->hc_num));
420f25c42b8SGevorg Sahakyan 	hc_dma = dwc2_readl(hsotg, HCDMA(chan->hc_num));
421197ba5f4SPaul Zimmerman 
422197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Assigned to channel %p:\n", chan);
423197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hcchar 0x%08x, hcsplt 0x%08x\n",
424197ba5f4SPaul Zimmerman 		hcchar, hcsplt);
425197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    hctsiz 0x%08x, hc_dma 0x%08x\n",
426197ba5f4SPaul Zimmerman 		hctsiz, hc_dma);
427197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
428197ba5f4SPaul Zimmerman 		chan->dev_addr, chan->ep_num, chan->ep_is_in);
429197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
430197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
431197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    data_pid_start: %d\n", chan->data_pid_start);
432197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_started: %d\n", chan->xfer_started);
433197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
434197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
435197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
436197ba5f4SPaul Zimmerman 		(unsigned long)chan->xfer_dma);
437197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
438197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
439197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP inactive sched:\n");
440197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
441197ba5f4SPaul Zimmerman 			    qh_list_entry)
442197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
44338d2b5fbSDouglas Anderson 	dev_dbg(hsotg->dev, "  NP waiting sched:\n");
44438d2b5fbSDouglas Anderson 	list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
44538d2b5fbSDouglas Anderson 			    qh_list_entry)
44638d2b5fbSDouglas Anderson 		dev_dbg(hsotg->dev, "    %p\n", qh);
447197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP active sched:\n");
448197ba5f4SPaul Zimmerman 	list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
449197ba5f4SPaul Zimmerman 			    qh_list_entry)
450197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %p\n", qh);
451197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Channels:\n");
452197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
453197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
454197ba5f4SPaul Zimmerman 
455197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    %2d: %p\n", i, chan);
456197ba5f4SPaul Zimmerman 	}
457197ba5f4SPaul Zimmerman #endif /* VERBOSE_DEBUG */
458197ba5f4SPaul Zimmerman }
459197ba5f4SPaul Zimmerman 
4604411bebaSRazmik Karapetyan static int _dwc2_hcd_start(struct usb_hcd *hcd);
4614411bebaSRazmik Karapetyan 
4624411bebaSRazmik Karapetyan static void dwc2_host_start(struct dwc2_hsotg *hsotg)
4634411bebaSRazmik Karapetyan {
4644411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
4654411bebaSRazmik Karapetyan 
4664411bebaSRazmik Karapetyan 	hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
4674411bebaSRazmik Karapetyan 	_dwc2_hcd_start(hcd);
4684411bebaSRazmik Karapetyan }
4694411bebaSRazmik Karapetyan 
4704411bebaSRazmik Karapetyan static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
4714411bebaSRazmik Karapetyan {
4724411bebaSRazmik Karapetyan 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
4734411bebaSRazmik Karapetyan 
4744411bebaSRazmik Karapetyan 	hcd->self.is_b_host = 0;
4754411bebaSRazmik Karapetyan }
4764411bebaSRazmik Karapetyan 
4774411bebaSRazmik Karapetyan static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
4784411bebaSRazmik Karapetyan 			       int *hub_addr, int *hub_port)
4794411bebaSRazmik Karapetyan {
4804411bebaSRazmik Karapetyan 	struct urb *urb = context;
4814411bebaSRazmik Karapetyan 
4824411bebaSRazmik Karapetyan 	if (urb->dev->tt)
4834411bebaSRazmik Karapetyan 		*hub_addr = urb->dev->tt->hub->devnum;
4844411bebaSRazmik Karapetyan 	else
4854411bebaSRazmik Karapetyan 		*hub_addr = 0;
4864411bebaSRazmik Karapetyan 	*hub_port = urb->dev->ttport;
4874411bebaSRazmik Karapetyan }
4884411bebaSRazmik Karapetyan 
489197ba5f4SPaul Zimmerman /*
490b02038faSJohn Youn  * =========================================================================
491b02038faSJohn Youn  *  Low Level Host Channel Access Functions
492b02038faSJohn Youn  * =========================================================================
493b02038faSJohn Youn  */
494b02038faSJohn Youn 
495b02038faSJohn Youn static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
496b02038faSJohn Youn 				      struct dwc2_host_chan *chan)
497b02038faSJohn Youn {
498b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
499b02038faSJohn Youn 
500b02038faSJohn Youn 	switch (chan->ep_type) {
501b02038faSJohn Youn 	case USB_ENDPOINT_XFER_CONTROL:
502b02038faSJohn Youn 	case USB_ENDPOINT_XFER_BULK:
503b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "control/bulk\n");
504b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
505b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
506b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
507b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
508b02038faSJohn Youn 		if (chan->ep_is_in) {
509b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
510b02038faSJohn Youn 		} else {
511b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
512b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NYET;
513b02038faSJohn Youn 			if (chan->do_ping)
514b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
515b02038faSJohn Youn 		}
516b02038faSJohn Youn 
517b02038faSJohn Youn 		if (chan->do_split) {
518b02038faSJohn Youn 			hcintmsk |= HCINTMSK_NAK;
519b02038faSJohn Youn 			if (chan->complete_split)
520b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
521b02038faSJohn Youn 			else
522b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
523b02038faSJohn Youn 		}
524b02038faSJohn Youn 
525b02038faSJohn Youn 		if (chan->error_state)
526b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
527b02038faSJohn Youn 		break;
528b02038faSJohn Youn 
529b02038faSJohn Youn 	case USB_ENDPOINT_XFER_INT:
530b02038faSJohn Youn 		if (dbg_perio())
531b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "intr\n");
532b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
533b02038faSJohn Youn 		hcintmsk |= HCINTMSK_NAK;
534b02038faSJohn Youn 		hcintmsk |= HCINTMSK_STALL;
535b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XACTERR;
536b02038faSJohn Youn 		hcintmsk |= HCINTMSK_DATATGLERR;
537b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
538b02038faSJohn Youn 
539b02038faSJohn Youn 		if (chan->ep_is_in)
540b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
541b02038faSJohn Youn 		if (chan->error_state)
542b02038faSJohn Youn 			hcintmsk |= HCINTMSK_ACK;
543b02038faSJohn Youn 		if (chan->do_split) {
544b02038faSJohn Youn 			if (chan->complete_split)
545b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NYET;
546b02038faSJohn Youn 			else
547b02038faSJohn Youn 				hcintmsk |= HCINTMSK_ACK;
548b02038faSJohn Youn 		}
549b02038faSJohn Youn 		break;
550b02038faSJohn Youn 
551b02038faSJohn Youn 	case USB_ENDPOINT_XFER_ISOC:
552b02038faSJohn Youn 		if (dbg_perio())
553b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "isoc\n");
554b02038faSJohn Youn 		hcintmsk |= HCINTMSK_XFERCOMPL;
555b02038faSJohn Youn 		hcintmsk |= HCINTMSK_FRMOVRUN;
556b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
557b02038faSJohn Youn 
558b02038faSJohn Youn 		if (chan->ep_is_in) {
559b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XACTERR;
560b02038faSJohn Youn 			hcintmsk |= HCINTMSK_BBLERR;
561b02038faSJohn Youn 		}
562b02038faSJohn Youn 		break;
563b02038faSJohn Youn 	default:
564b02038faSJohn Youn 		dev_err(hsotg->dev, "## Unknown EP type ##\n");
565b02038faSJohn Youn 		break;
566b02038faSJohn Youn 	}
567b02038faSJohn Youn 
568f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
569b02038faSJohn Youn 	if (dbg_hc(chan))
570b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
571b02038faSJohn Youn }
572b02038faSJohn Youn 
573b02038faSJohn Youn static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
574b02038faSJohn Youn 				    struct dwc2_host_chan *chan)
575b02038faSJohn Youn {
576b02038faSJohn Youn 	u32 hcintmsk = HCINTMSK_CHHLTD;
577b02038faSJohn Youn 
578b02038faSJohn Youn 	/*
579b02038faSJohn Youn 	 * For Descriptor DMA mode core halts the channel on AHB error.
580b02038faSJohn Youn 	 * Interrupt is not required.
581b02038faSJohn Youn 	 */
58295832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
583b02038faSJohn Youn 		if (dbg_hc(chan))
584b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
585b02038faSJohn Youn 		hcintmsk |= HCINTMSK_AHBERR;
586b02038faSJohn Youn 	} else {
587b02038faSJohn Youn 		if (dbg_hc(chan))
588b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA enabled\n");
589b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
590b02038faSJohn Youn 			hcintmsk |= HCINTMSK_XFERCOMPL;
591b02038faSJohn Youn 	}
592b02038faSJohn Youn 
593b02038faSJohn Youn 	if (chan->error_state && !chan->do_split &&
594b02038faSJohn Youn 	    chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
595b02038faSJohn Youn 		if (dbg_hc(chan))
596b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "setting ACK\n");
597b02038faSJohn Youn 		hcintmsk |= HCINTMSK_ACK;
598b02038faSJohn Youn 		if (chan->ep_is_in) {
599b02038faSJohn Youn 			hcintmsk |= HCINTMSK_DATATGLERR;
600b02038faSJohn Youn 			if (chan->ep_type != USB_ENDPOINT_XFER_INT)
601b02038faSJohn Youn 				hcintmsk |= HCINTMSK_NAK;
602b02038faSJohn Youn 		}
603b02038faSJohn Youn 	}
604b02038faSJohn Youn 
605f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
606b02038faSJohn Youn 	if (dbg_hc(chan))
607b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
608b02038faSJohn Youn }
609b02038faSJohn Youn 
610b02038faSJohn Youn static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
611b02038faSJohn Youn 				struct dwc2_host_chan *chan)
612b02038faSJohn Youn {
613b02038faSJohn Youn 	u32 intmsk;
614b02038faSJohn Youn 
61595832c00SJohn Youn 	if (hsotg->params.host_dma) {
616b02038faSJohn Youn 		if (dbg_hc(chan))
617b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
618b02038faSJohn Youn 		dwc2_hc_enable_dma_ints(hsotg, chan);
619b02038faSJohn Youn 	} else {
620b02038faSJohn Youn 		if (dbg_hc(chan))
621b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA disabled\n");
622b02038faSJohn Youn 		dwc2_hc_enable_slave_ints(hsotg, chan);
623b02038faSJohn Youn 	}
624b02038faSJohn Youn 
625b02038faSJohn Youn 	/* Enable the top level host channel interrupt */
626f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, HAINTMSK);
627b02038faSJohn Youn 	intmsk |= 1 << chan->hc_num;
628f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, HAINTMSK);
629b02038faSJohn Youn 	if (dbg_hc(chan))
630b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
631b02038faSJohn Youn 
632b02038faSJohn Youn 	/* Make sure host channel interrupts are enabled */
633f25c42b8SGevorg Sahakyan 	intmsk = dwc2_readl(hsotg, GINTMSK);
634b02038faSJohn Youn 	intmsk |= GINTSTS_HCHINT;
635f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intmsk, GINTMSK);
636b02038faSJohn Youn 	if (dbg_hc(chan))
637b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
638b02038faSJohn Youn }
639b02038faSJohn Youn 
640b02038faSJohn Youn /**
641b02038faSJohn Youn  * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
642b02038faSJohn Youn  * a specific endpoint
643b02038faSJohn Youn  *
644b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
645b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
646b02038faSJohn Youn  *
647b02038faSJohn Youn  * The HCCHARn register is set up with the characteristics specified in chan.
648b02038faSJohn Youn  * Host channel interrupts that may need to be serviced while this transfer is
649b02038faSJohn Youn  * in progress are enabled.
650b02038faSJohn Youn  */
651b02038faSJohn Youn static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
652b02038faSJohn Youn {
653b02038faSJohn Youn 	u8 hc_num = chan->hc_num;
654b02038faSJohn Youn 	u32 hcintmsk;
655b02038faSJohn Youn 	u32 hcchar;
656b02038faSJohn Youn 	u32 hcsplt = 0;
657b02038faSJohn Youn 
658b02038faSJohn Youn 	if (dbg_hc(chan))
659b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
660b02038faSJohn Youn 
661b02038faSJohn Youn 	/* Clear old interrupt conditions for this host channel */
662b02038faSJohn Youn 	hcintmsk = 0xffffffff;
663b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
664f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(hc_num));
665b02038faSJohn Youn 
666b02038faSJohn Youn 	/* Enable channel interrupts required for this transfer */
667b02038faSJohn Youn 	dwc2_hc_enable_ints(hsotg, chan);
668b02038faSJohn Youn 
669b02038faSJohn Youn 	/*
670b02038faSJohn Youn 	 * Program the HCCHARn register with the endpoint characteristics for
671b02038faSJohn Youn 	 * the current transfer
672b02038faSJohn Youn 	 */
673b02038faSJohn Youn 	hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
674b02038faSJohn Youn 	hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
675b02038faSJohn Youn 	if (chan->ep_is_in)
676b02038faSJohn Youn 		hcchar |= HCCHAR_EPDIR;
677b02038faSJohn Youn 	if (chan->speed == USB_SPEED_LOW)
678b02038faSJohn Youn 		hcchar |= HCCHAR_LSPDDEV;
679b02038faSJohn Youn 	hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
680b02038faSJohn Youn 	hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
681f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(hc_num));
682b02038faSJohn Youn 	if (dbg_hc(chan)) {
683b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
684b02038faSJohn Youn 			 hc_num, hcchar);
685b02038faSJohn Youn 
686b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n",
687b02038faSJohn Youn 			 __func__, hc_num);
688b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Dev Addr: %d\n",
689b02038faSJohn Youn 			 chan->dev_addr);
690b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Num: %d\n",
691b02038faSJohn Youn 			 chan->ep_num);
692b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is In: %d\n",
693b02038faSJohn Youn 			 chan->ep_is_in);
694b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Is Low Speed: %d\n",
695b02038faSJohn Youn 			 chan->speed == USB_SPEED_LOW);
696b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Ep Type: %d\n",
697b02038faSJohn Youn 			 chan->ep_type);
698b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Max Pkt: %d\n",
699b02038faSJohn Youn 			 chan->max_packet);
700b02038faSJohn Youn 	}
701b02038faSJohn Youn 
702b02038faSJohn Youn 	/* Program the HCSPLT register for SPLITs */
703b02038faSJohn Youn 	if (chan->do_split) {
704b02038faSJohn Youn 		if (dbg_hc(chan))
705b02038faSJohn Youn 			dev_vdbg(hsotg->dev,
706b02038faSJohn Youn 				 "Programming HC %d with split --> %s\n",
707b02038faSJohn Youn 				 hc_num,
708b02038faSJohn Youn 				 chan->complete_split ? "CSPLIT" : "SSPLIT");
709b02038faSJohn Youn 		if (chan->complete_split)
710b02038faSJohn Youn 			hcsplt |= HCSPLT_COMPSPLT;
711b02038faSJohn Youn 		hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
712b02038faSJohn Youn 			  HCSPLT_XACTPOS_MASK;
713b02038faSJohn Youn 		hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
714b02038faSJohn Youn 			  HCSPLT_HUBADDR_MASK;
715b02038faSJohn Youn 		hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
716b02038faSJohn Youn 			  HCSPLT_PRTADDR_MASK;
717b02038faSJohn Youn 		if (dbg_hc(chan)) {
718b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  comp split %d\n",
719b02038faSJohn Youn 				 chan->complete_split);
720b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xact pos %d\n",
721b02038faSJohn Youn 				 chan->xact_pos);
722b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub addr %d\n",
723b02038faSJohn Youn 				 chan->hub_addr);
724b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  hub port %d\n",
725b02038faSJohn Youn 				 chan->hub_port);
726b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  is_in %d\n",
727b02038faSJohn Youn 				 chan->ep_is_in);
728b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  Max Pkt %d\n",
729b02038faSJohn Youn 				 chan->max_packet);
730b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	  xferlen %d\n",
731b02038faSJohn Youn 				 chan->xfer_len);
732b02038faSJohn Youn 		}
733b02038faSJohn Youn 	}
734b02038faSJohn Youn 
735f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcsplt, HCSPLT(hc_num));
736b02038faSJohn Youn }
737b02038faSJohn Youn 
738b02038faSJohn Youn /**
739b02038faSJohn Youn  * dwc2_hc_halt() - Attempts to halt a host channel
740b02038faSJohn Youn  *
741b02038faSJohn Youn  * @hsotg:       Controller register interface
742b02038faSJohn Youn  * @chan:        Host channel to halt
743b02038faSJohn Youn  * @halt_status: Reason for halting the channel
744b02038faSJohn Youn  *
745b02038faSJohn Youn  * This function should only be called in Slave mode or to abort a transfer in
746b02038faSJohn Youn  * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
747b02038faSJohn Youn  * controller halts the channel when the transfer is complete or a condition
748b02038faSJohn Youn  * occurs that requires application intervention.
749b02038faSJohn Youn  *
750b02038faSJohn Youn  * In slave mode, checks for a free request queue entry, then sets the Channel
751b02038faSJohn Youn  * Enable and Channel Disable bits of the Host Channel Characteristics
752b02038faSJohn Youn  * register of the specified channel to intiate the halt. If there is no free
753b02038faSJohn Youn  * request queue entry, sets only the Channel Disable bit of the HCCHARn
754b02038faSJohn Youn  * register to flush requests for this channel. In the latter case, sets a
755b02038faSJohn Youn  * flag to indicate that the host channel needs to be halted when a request
756b02038faSJohn Youn  * queue slot is open.
757b02038faSJohn Youn  *
758b02038faSJohn Youn  * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
759b02038faSJohn Youn  * HCCHARn register. The controller ensures there is space in the request
760b02038faSJohn Youn  * queue before submitting the halt request.
761b02038faSJohn Youn  *
762b02038faSJohn Youn  * Some time may elapse before the core flushes any posted requests for this
763b02038faSJohn Youn  * host channel and halts. The Channel Halted interrupt handler completes the
764b02038faSJohn Youn  * deactivation of the host channel.
765b02038faSJohn Youn  */
766b02038faSJohn Youn void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
767b02038faSJohn Youn 		  enum dwc2_halt_status halt_status)
768b02038faSJohn Youn {
769b02038faSJohn Youn 	u32 nptxsts, hptxsts, hcchar;
770b02038faSJohn Youn 
771b02038faSJohn Youn 	if (dbg_hc(chan))
772b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
773a82c7abdSMinas Harutyunyan 
774a82c7abdSMinas Harutyunyan 	/*
775a82c7abdSMinas Harutyunyan 	 * In buffer DMA or external DMA mode channel can't be halted
776a82c7abdSMinas Harutyunyan 	 * for non-split periodic channels. At the end of the next
777a82c7abdSMinas Harutyunyan 	 * uframe/frame (in the worst case), the core generates a channel
778a82c7abdSMinas Harutyunyan 	 * halted and disables the channel automatically.
779a82c7abdSMinas Harutyunyan 	 */
780a82c7abdSMinas Harutyunyan 	if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
781a82c7abdSMinas Harutyunyan 	    hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
782a82c7abdSMinas Harutyunyan 		if (!chan->do_split &&
783a82c7abdSMinas Harutyunyan 		    (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
784a82c7abdSMinas Harutyunyan 		     chan->ep_type == USB_ENDPOINT_XFER_INT)) {
785a82c7abdSMinas Harutyunyan 			dev_err(hsotg->dev, "%s() Channel can't be halted\n",
786a82c7abdSMinas Harutyunyan 				__func__);
787a82c7abdSMinas Harutyunyan 			return;
788a82c7abdSMinas Harutyunyan 		}
789a82c7abdSMinas Harutyunyan 	}
790a82c7abdSMinas Harutyunyan 
791b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
792b02038faSJohn Youn 		dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
793b02038faSJohn Youn 
794b02038faSJohn Youn 	if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
795b02038faSJohn Youn 	    halt_status == DWC2_HC_XFER_AHB_ERR) {
796b02038faSJohn Youn 		/*
797b02038faSJohn Youn 		 * Disable all channel interrupts except Ch Halted. The QTD
798b02038faSJohn Youn 		 * and QH state associated with this transfer has been cleared
799b02038faSJohn Youn 		 * (in the case of URB_DEQUEUE), so the channel needs to be
800b02038faSJohn Youn 		 * shut down carefully to prevent crashes.
801b02038faSJohn Youn 		 */
802b02038faSJohn Youn 		u32 hcintmsk = HCINTMSK_CHHLTD;
803b02038faSJohn Youn 
804b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "dequeue/error\n");
805f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcintmsk, HCINTMSK(chan->hc_num));
806b02038faSJohn Youn 
807b02038faSJohn Youn 		/*
808b02038faSJohn Youn 		 * Make sure no other interrupts besides halt are currently
809b02038faSJohn Youn 		 * pending. Handling another interrupt could cause a crash due
810b02038faSJohn Youn 		 * to the QTD and QH state.
811b02038faSJohn Youn 		 */
812f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, ~hcintmsk, HCINT(chan->hc_num));
813b02038faSJohn Youn 
814b02038faSJohn Youn 		/*
815b02038faSJohn Youn 		 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
816b02038faSJohn Youn 		 * even if the channel was already halted for some other
817b02038faSJohn Youn 		 * reason
818b02038faSJohn Youn 		 */
819b02038faSJohn Youn 		chan->halt_status = halt_status;
820b02038faSJohn Youn 
821f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
822b02038faSJohn Youn 		if (!(hcchar & HCCHAR_CHENA)) {
823b02038faSJohn Youn 			/*
824b02038faSJohn Youn 			 * The channel is either already halted or it hasn't
825b02038faSJohn Youn 			 * started yet. In DMA mode, the transfer may halt if
826b02038faSJohn Youn 			 * it finishes normally or a condition occurs that
827b02038faSJohn Youn 			 * requires driver intervention. Don't want to halt
828b02038faSJohn Youn 			 * the channel again. In either Slave or DMA mode,
829b02038faSJohn Youn 			 * it's possible that the transfer has been assigned
830b02038faSJohn Youn 			 * to a channel, but not started yet when an URB is
831b02038faSJohn Youn 			 * dequeued. Don't want to halt a channel that hasn't
832b02038faSJohn Youn 			 * started yet.
833b02038faSJohn Youn 			 */
834b02038faSJohn Youn 			return;
835b02038faSJohn Youn 		}
836b02038faSJohn Youn 	}
837b02038faSJohn Youn 	if (chan->halt_pending) {
838b02038faSJohn Youn 		/*
839b02038faSJohn Youn 		 * A halt has already been issued for this channel. This might
840b02038faSJohn Youn 		 * happen when a transfer is aborted by a higher level in
841b02038faSJohn Youn 		 * the stack.
842b02038faSJohn Youn 		 */
843b02038faSJohn Youn 		dev_vdbg(hsotg->dev,
844b02038faSJohn Youn 			 "*** %s: Channel %d, chan->halt_pending already set ***\n",
845b02038faSJohn Youn 			 __func__, chan->hc_num);
846b02038faSJohn Youn 		return;
847b02038faSJohn Youn 	}
848b02038faSJohn Youn 
849f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
850b02038faSJohn Youn 
851b02038faSJohn Youn 	/* No need to set the bit in DDMA for disabling the channel */
852b02038faSJohn Youn 	/* TODO check it everywhere channel is disabled */
85395832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
854b02038faSJohn Youn 		if (dbg_hc(chan))
855b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
856b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
857b02038faSJohn Youn 	} else {
858b02038faSJohn Youn 		if (dbg_hc(chan))
859b02038faSJohn Youn 			dev_dbg(hsotg->dev, "desc DMA enabled\n");
860b02038faSJohn Youn 	}
861b02038faSJohn Youn 	hcchar |= HCCHAR_CHDIS;
862b02038faSJohn Youn 
86395832c00SJohn Youn 	if (!hsotg->params.host_dma) {
864b02038faSJohn Youn 		if (dbg_hc(chan))
865b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA not enabled\n");
866b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
867b02038faSJohn Youn 
868b02038faSJohn Youn 		/* Check for space in the request queue to issue the halt */
869b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
870b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
871b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "control/bulk\n");
872f25c42b8SGevorg Sahakyan 			nptxsts = dwc2_readl(hsotg, GNPTXSTS);
873b02038faSJohn Youn 			if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
874b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "Disabling channel\n");
875b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
876b02038faSJohn Youn 			}
877b02038faSJohn Youn 		} else {
878b02038faSJohn Youn 			if (dbg_perio())
879b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "isoc/intr\n");
880f25c42b8SGevorg Sahakyan 			hptxsts = dwc2_readl(hsotg, HPTXSTS);
881b02038faSJohn Youn 			if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
882b02038faSJohn Youn 			    hsotg->queuing_high_bandwidth) {
883b02038faSJohn Youn 				if (dbg_perio())
884b02038faSJohn Youn 					dev_vdbg(hsotg->dev, "Disabling channel\n");
885b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
886b02038faSJohn Youn 			}
887b02038faSJohn Youn 		}
888b02038faSJohn Youn 	} else {
889b02038faSJohn Youn 		if (dbg_hc(chan))
890b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "DMA enabled\n");
891b02038faSJohn Youn 	}
892b02038faSJohn Youn 
893f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
894b02038faSJohn Youn 	chan->halt_status = halt_status;
895b02038faSJohn Youn 
896b02038faSJohn Youn 	if (hcchar & HCCHAR_CHENA) {
897b02038faSJohn Youn 		if (dbg_hc(chan))
898b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel enabled\n");
899b02038faSJohn Youn 		chan->halt_pending = 1;
900b02038faSJohn Youn 		chan->halt_on_queue = 0;
901b02038faSJohn Youn 	} else {
902b02038faSJohn Youn 		if (dbg_hc(chan))
903b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Channel disabled\n");
904b02038faSJohn Youn 		chan->halt_on_queue = 1;
905b02038faSJohn Youn 	}
906b02038faSJohn Youn 
907b02038faSJohn Youn 	if (dbg_hc(chan)) {
908b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
909b02038faSJohn Youn 			 chan->hc_num);
910b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 hcchar: 0x%08x\n",
911b02038faSJohn Youn 			 hcchar);
912b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_pending: %d\n",
913b02038faSJohn Youn 			 chan->halt_pending);
914b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_on_queue: %d\n",
915b02038faSJohn Youn 			 chan->halt_on_queue);
916b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 halt_status: %d\n",
917b02038faSJohn Youn 			 chan->halt_status);
918b02038faSJohn Youn 	}
919b02038faSJohn Youn }
920b02038faSJohn Youn 
921b02038faSJohn Youn /**
922b02038faSJohn Youn  * dwc2_hc_cleanup() - Clears the transfer state for a host channel
923b02038faSJohn Youn  *
924b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
925b02038faSJohn Youn  * @chan:  Identifies the host channel to clean up
926b02038faSJohn Youn  *
927b02038faSJohn Youn  * This function is normally called after a transfer is done and the host
928b02038faSJohn Youn  * channel is being released
929b02038faSJohn Youn  */
930b02038faSJohn Youn void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
931b02038faSJohn Youn {
932b02038faSJohn Youn 	u32 hcintmsk;
933b02038faSJohn Youn 
934b02038faSJohn Youn 	chan->xfer_started = 0;
935b02038faSJohn Youn 
936b02038faSJohn Youn 	list_del_init(&chan->split_order_list_entry);
937b02038faSJohn Youn 
938b02038faSJohn Youn 	/*
939b02038faSJohn Youn 	 * Clear channel interrupt enables and any unhandled channel interrupt
940b02038faSJohn Youn 	 * conditions
941b02038faSJohn Youn 	 */
942f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HCINTMSK(chan->hc_num));
943b02038faSJohn Youn 	hcintmsk = 0xffffffff;
944b02038faSJohn Youn 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
945f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcintmsk, HCINT(chan->hc_num));
946b02038faSJohn Youn }
947b02038faSJohn Youn 
948b02038faSJohn Youn /**
949b02038faSJohn Youn  * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
950b02038faSJohn Youn  * which frame a periodic transfer should occur
951b02038faSJohn Youn  *
952b02038faSJohn Youn  * @hsotg:  Programming view of DWC_otg controller
953b02038faSJohn Youn  * @chan:   Identifies the host channel to set up and its properties
954b02038faSJohn Youn  * @hcchar: Current value of the HCCHAR register for the specified host channel
955b02038faSJohn Youn  *
956b02038faSJohn Youn  * This function has no effect on non-periodic transfers
957b02038faSJohn Youn  */
958b02038faSJohn Youn static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
959b02038faSJohn Youn 				       struct dwc2_host_chan *chan, u32 *hcchar)
960b02038faSJohn Youn {
961b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
962b02038faSJohn Youn 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
963b02038faSJohn Youn 		int host_speed;
964b02038faSJohn Youn 		int xfer_ns;
965b02038faSJohn Youn 		int xfer_us;
966b02038faSJohn Youn 		int bytes_in_fifo;
967b02038faSJohn Youn 		u16 fifo_space;
968b02038faSJohn Youn 		u16 frame_number;
969b02038faSJohn Youn 		u16 wire_frame;
970b02038faSJohn Youn 
971b02038faSJohn Youn 		/*
972b02038faSJohn Youn 		 * Try to figure out if we're an even or odd frame. If we set
9732958d494SJiang Jian 		 * even and the current frame number is even the transfer
974b02038faSJohn Youn 		 * will happen immediately.  Similar if both are odd. If one is
975b02038faSJohn Youn 		 * even and the other is odd then the transfer will happen when
976b02038faSJohn Youn 		 * the frame number ticks.
977b02038faSJohn Youn 		 *
978b02038faSJohn Youn 		 * There's a bit of a balancing act to get this right.
979b02038faSJohn Youn 		 * Sometimes we may want to send data in the current frame (AK
980b02038faSJohn Youn 		 * right away).  We might want to do this if the frame number
981b02038faSJohn Youn 		 * _just_ ticked, but we might also want to do this in order
982b02038faSJohn Youn 		 * to continue a split transaction that happened late in a
983b02038faSJohn Youn 		 * microframe (so we didn't know to queue the next transfer
984b02038faSJohn Youn 		 * until the frame number had ticked).  The problem is that we
985b02038faSJohn Youn 		 * need a lot of knowledge to know if there's actually still
986b02038faSJohn Youn 		 * time to send things or if it would be better to wait until
987b02038faSJohn Youn 		 * the next frame.
988b02038faSJohn Youn 		 *
989b02038faSJohn Youn 		 * We can look at how much time is left in the current frame
990b02038faSJohn Youn 		 * and make a guess about whether we'll have time to transfer.
991b02038faSJohn Youn 		 * We'll do that.
992b02038faSJohn Youn 		 */
993b02038faSJohn Youn 
994b02038faSJohn Youn 		/* Get speed host is running at */
995b02038faSJohn Youn 		host_speed = (chan->speed != USB_SPEED_HIGH &&
996b02038faSJohn Youn 			      !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
997b02038faSJohn Youn 
998b02038faSJohn Youn 		/* See how many bytes are in the periodic FIFO right now */
999f25c42b8SGevorg Sahakyan 		fifo_space = (dwc2_readl(hsotg, HPTXSTS) &
1000b02038faSJohn Youn 			      TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1001b02038faSJohn Youn 		bytes_in_fifo = sizeof(u32) *
1002bea8e86cSJohn Youn 				(hsotg->params.host_perio_tx_fifo_size -
1003b02038faSJohn Youn 				 fifo_space);
1004b02038faSJohn Youn 
1005b02038faSJohn Youn 		/*
1006b02038faSJohn Youn 		 * Roughly estimate bus time for everything in the periodic
1007b02038faSJohn Youn 		 * queue + our new transfer.  This is "rough" because we're
1008b02038faSJohn Youn 		 * using a function that makes takes into account IN/OUT
1009b02038faSJohn Youn 		 * and INT/ISO and we're just slamming in one value for all
1010b02038faSJohn Youn 		 * transfers.  This should be an over-estimate and that should
1011b02038faSJohn Youn 		 * be OK, but we can probably tighten it.
1012b02038faSJohn Youn 		 */
1013b02038faSJohn Youn 		xfer_ns = usb_calc_bus_time(host_speed, false, false,
1014b02038faSJohn Youn 					    chan->xfer_len + bytes_in_fifo);
1015b02038faSJohn Youn 		xfer_us = NS_TO_US(xfer_ns);
1016b02038faSJohn Youn 
1017b02038faSJohn Youn 		/* See what frame number we'll be at by the time we finish */
1018b02038faSJohn Youn 		frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1019b02038faSJohn Youn 
1020b02038faSJohn Youn 		/* This is when we were scheduled to be on the wire */
1021b02038faSJohn Youn 		wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1022b02038faSJohn Youn 
1023b02038faSJohn Youn 		/*
1024b02038faSJohn Youn 		 * If we'd finish _after_ the frame we're scheduled in then
1025b02038faSJohn Youn 		 * it's hopeless.  Just schedule right away and hope for the
1026b02038faSJohn Youn 		 * best.  Note that it _might_ be wise to call back into the
1027b02038faSJohn Youn 		 * scheduler to pick a better frame, but this is better than
1028b02038faSJohn Youn 		 * nothing.
1029b02038faSJohn Youn 		 */
1030b02038faSJohn Youn 		if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1031b02038faSJohn Youn 			dwc2_sch_vdbg(hsotg,
1032b02038faSJohn Youn 				      "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1033b02038faSJohn Youn 				      chan->qh, wire_frame, frame_number,
1034b02038faSJohn Youn 				      dwc2_frame_num_dec(frame_number,
1035b02038faSJohn Youn 							 wire_frame));
1036b02038faSJohn Youn 			wire_frame = frame_number;
1037b02038faSJohn Youn 
1038b02038faSJohn Youn 			/*
1039b02038faSJohn Youn 			 * We picked a different frame number; communicate this
1040b02038faSJohn Youn 			 * back to the scheduler so it doesn't try to schedule
1041b02038faSJohn Youn 			 * another in the same frame.
1042b02038faSJohn Youn 			 *
1043b02038faSJohn Youn 			 * Remember that next_active_frame is 1 before the wire
1044b02038faSJohn Youn 			 * frame.
1045b02038faSJohn Youn 			 */
1046b02038faSJohn Youn 			chan->qh->next_active_frame =
1047b02038faSJohn Youn 				dwc2_frame_num_dec(frame_number, 1);
1048b02038faSJohn Youn 		}
1049b02038faSJohn Youn 
1050b02038faSJohn Youn 		if (wire_frame & 1)
1051b02038faSJohn Youn 			*hcchar |= HCCHAR_ODDFRM;
1052b02038faSJohn Youn 		else
1053b02038faSJohn Youn 			*hcchar &= ~HCCHAR_ODDFRM;
1054b02038faSJohn Youn 	}
1055b02038faSJohn Youn }
1056b02038faSJohn Youn 
1057b02038faSJohn Youn static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1058b02038faSJohn Youn {
1059b02038faSJohn Youn 	/* Set up the initial PID for the transfer */
1060b02038faSJohn Youn 	if (chan->speed == USB_SPEED_HIGH) {
1061b02038faSJohn Youn 		if (chan->ep_is_in) {
1062b02038faSJohn Youn 			if (chan->multi_count == 1)
1063b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1064b02038faSJohn Youn 			else if (chan->multi_count == 2)
1065b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA1;
1066b02038faSJohn Youn 			else
1067b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA2;
1068b02038faSJohn Youn 		} else {
1069b02038faSJohn Youn 			if (chan->multi_count == 1)
1070b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1071b02038faSJohn Youn 			else
1072b02038faSJohn Youn 				chan->data_pid_start = DWC2_HC_PID_MDATA;
1073b02038faSJohn Youn 		}
1074b02038faSJohn Youn 	} else {
1075b02038faSJohn Youn 		chan->data_pid_start = DWC2_HC_PID_DATA0;
1076b02038faSJohn Youn 	}
1077b02038faSJohn Youn }
1078b02038faSJohn Youn 
1079b02038faSJohn Youn /**
1080b02038faSJohn Youn  * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1081b02038faSJohn Youn  * the Host Channel
1082b02038faSJohn Youn  *
1083b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1084b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1085b02038faSJohn Youn  *
1086b02038faSJohn Youn  * This function should only be called in Slave mode. For a channel associated
1087b02038faSJohn Youn  * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1088b02038faSJohn Youn  * associated with a periodic EP, the periodic Tx FIFO is written.
1089b02038faSJohn Youn  *
1090b02038faSJohn Youn  * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1091b02038faSJohn Youn  * the number of bytes written to the Tx FIFO.
1092b02038faSJohn Youn  */
1093b02038faSJohn Youn static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1094b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1095b02038faSJohn Youn {
1096b02038faSJohn Youn 	u32 i;
1097b02038faSJohn Youn 	u32 remaining_count;
1098b02038faSJohn Youn 	u32 byte_count;
1099b02038faSJohn Youn 	u32 dword_count;
1100b02038faSJohn Youn 	u32 *data_buf = (u32 *)chan->xfer_buf;
1101b02038faSJohn Youn 
1102b02038faSJohn Youn 	if (dbg_hc(chan))
1103b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1104b02038faSJohn Youn 
1105b02038faSJohn Youn 	remaining_count = chan->xfer_len - chan->xfer_count;
1106b02038faSJohn Youn 	if (remaining_count > chan->max_packet)
1107b02038faSJohn Youn 		byte_count = chan->max_packet;
1108b02038faSJohn Youn 	else
1109b02038faSJohn Youn 		byte_count = remaining_count;
1110b02038faSJohn Youn 
1111b02038faSJohn Youn 	dword_count = (byte_count + 3) / 4;
1112b02038faSJohn Youn 
1113b02038faSJohn Youn 	if (((unsigned long)data_buf & 0x3) == 0) {
1114b02038faSJohn Youn 		/* xfer_buf is DWORD aligned */
1115b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++)
1116f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, *data_buf, HCFIFO(chan->hc_num));
1117b02038faSJohn Youn 	} else {
1118b02038faSJohn Youn 		/* xfer_buf is not DWORD aligned */
1119b02038faSJohn Youn 		for (i = 0; i < dword_count; i++, data_buf++) {
1120b02038faSJohn Youn 			u32 data = data_buf[0] | data_buf[1] << 8 |
1121b02038faSJohn Youn 				   data_buf[2] << 16 | data_buf[3] << 24;
1122f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, data, HCFIFO(chan->hc_num));
1123b02038faSJohn Youn 		}
1124b02038faSJohn Youn 	}
1125b02038faSJohn Youn 
1126b02038faSJohn Youn 	chan->xfer_count += byte_count;
1127b02038faSJohn Youn 	chan->xfer_buf += byte_count;
1128b02038faSJohn Youn }
1129b02038faSJohn Youn 
1130b02038faSJohn Youn /**
1131b02038faSJohn Youn  * dwc2_hc_do_ping() - Starts a PING transfer
1132b02038faSJohn Youn  *
1133b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1134b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1135b02038faSJohn Youn  *
1136b02038faSJohn Youn  * This function should only be called in Slave mode. The Do Ping bit is set in
1137b02038faSJohn Youn  * the HCTSIZ register, then the channel is enabled.
1138b02038faSJohn Youn  */
1139b02038faSJohn Youn static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1140b02038faSJohn Youn 			    struct dwc2_host_chan *chan)
1141b02038faSJohn Youn {
1142b02038faSJohn Youn 	u32 hcchar;
1143b02038faSJohn Youn 	u32 hctsiz;
1144b02038faSJohn Youn 
1145b02038faSJohn Youn 	if (dbg_hc(chan))
1146b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1147b02038faSJohn Youn 			 chan->hc_num);
1148b02038faSJohn Youn 
1149b02038faSJohn Youn 	hctsiz = TSIZ_DOPNG;
1150b02038faSJohn Youn 	hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1151f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1152b02038faSJohn Youn 
1153f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1154b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1155b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1156f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1157b02038faSJohn Youn }
1158b02038faSJohn Youn 
1159b02038faSJohn Youn /**
1160b02038faSJohn Youn  * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1161b02038faSJohn Youn  * channel and starts the transfer
1162b02038faSJohn Youn  *
1163b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1164b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel. The xfer_len value
1165b02038faSJohn Youn  *         may be reduced to accommodate the max widths of the XferSize and
1166b02038faSJohn Youn  *         PktCnt fields in the HCTSIZn register. The multi_count value may be
1167b02038faSJohn Youn  *         changed to reflect the final xfer_len value.
1168b02038faSJohn Youn  *
1169b02038faSJohn Youn  * This function may be called in either Slave mode or DMA mode. In Slave mode,
1170b02038faSJohn Youn  * the caller must ensure that there is sufficient space in the request queue
1171b02038faSJohn Youn  * and Tx Data FIFO.
1172b02038faSJohn Youn  *
1173b02038faSJohn Youn  * For an OUT transfer in Slave mode, it loads a data packet into the
1174b02038faSJohn Youn  * appropriate FIFO. If necessary, additional data packets are loaded in the
1175b02038faSJohn Youn  * Host ISR.
1176b02038faSJohn Youn  *
1177b02038faSJohn Youn  * For an IN transfer in Slave mode, a data packet is requested. The data
1178b02038faSJohn Youn  * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1179b02038faSJohn Youn  * additional data packets are requested in the Host ISR.
1180b02038faSJohn Youn  *
1181b02038faSJohn Youn  * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1182b02038faSJohn Youn  * register along with a packet count of 1 and the channel is enabled. This
1183b02038faSJohn Youn  * causes a single PING transaction to occur. Other fields in HCTSIZ are
1184b02038faSJohn Youn  * simply set to 0 since no data transfer occurs in this case.
1185b02038faSJohn Youn  *
1186b02038faSJohn Youn  * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1187b02038faSJohn Youn  * all the information required to perform the subsequent data transfer. In
1188b02038faSJohn Youn  * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1189b02038faSJohn Youn  * controller performs the entire PING protocol, then starts the data
1190b02038faSJohn Youn  * transfer.
1191b02038faSJohn Youn  */
1192b02038faSJohn Youn static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1193b02038faSJohn Youn 				   struct dwc2_host_chan *chan)
1194b02038faSJohn Youn {
1195bea8e86cSJohn Youn 	u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1196bea8e86cSJohn Youn 	u16 max_hc_pkt_count = hsotg->params.max_packet_count;
1197b02038faSJohn Youn 	u32 hcchar;
1198b02038faSJohn Youn 	u32 hctsiz = 0;
1199b02038faSJohn Youn 	u16 num_packets;
1200b02038faSJohn Youn 	u32 ec_mc;
1201b02038faSJohn Youn 
1202b02038faSJohn Youn 	if (dbg_hc(chan))
1203b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1204b02038faSJohn Youn 
1205b02038faSJohn Youn 	if (chan->do_ping) {
120695832c00SJohn Youn 		if (!hsotg->params.host_dma) {
1207b02038faSJohn Youn 			if (dbg_hc(chan))
1208b02038faSJohn Youn 				dev_vdbg(hsotg->dev, "ping, no DMA\n");
1209b02038faSJohn Youn 			dwc2_hc_do_ping(hsotg, chan);
1210b02038faSJohn Youn 			chan->xfer_started = 1;
1211b02038faSJohn Youn 			return;
1212b02038faSJohn Youn 		}
1213b02038faSJohn Youn 
1214b02038faSJohn Youn 		if (dbg_hc(chan))
1215b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "ping, DMA\n");
1216b02038faSJohn Youn 
1217b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1218b02038faSJohn Youn 	}
1219b02038faSJohn Youn 
1220b02038faSJohn Youn 	if (chan->do_split) {
1221b02038faSJohn Youn 		if (dbg_hc(chan))
1222b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "split\n");
1223b02038faSJohn Youn 		num_packets = 1;
1224b02038faSJohn Youn 
1225b02038faSJohn Youn 		if (chan->complete_split && !chan->ep_is_in)
1226b02038faSJohn Youn 			/*
1227b02038faSJohn Youn 			 * For CSPLIT OUT Transfer, set the size to 0 so the
1228b02038faSJohn Youn 			 * core doesn't expect any data written to the FIFO
1229b02038faSJohn Youn 			 */
1230b02038faSJohn Youn 			chan->xfer_len = 0;
1231b02038faSJohn Youn 		else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1232b02038faSJohn Youn 			chan->xfer_len = chan->max_packet;
1233b02038faSJohn Youn 		else if (!chan->ep_is_in && chan->xfer_len > 188)
1234b02038faSJohn Youn 			chan->xfer_len = 188;
1235b02038faSJohn Youn 
1236b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1237b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1238b02038faSJohn Youn 
1239b02038faSJohn Youn 		/* For split set ec_mc for immediate retries */
1240b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1241b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1242b02038faSJohn Youn 			ec_mc = 3;
1243b02038faSJohn Youn 		else
1244b02038faSJohn Youn 			ec_mc = 1;
1245b02038faSJohn Youn 	} else {
1246b02038faSJohn Youn 		if (dbg_hc(chan))
1247b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "no split\n");
1248b02038faSJohn Youn 		/*
1249b02038faSJohn Youn 		 * Ensure that the transfer length and packet count will fit
1250b02038faSJohn Youn 		 * in the widths allocated for them in the HCTSIZn register
1251b02038faSJohn Youn 		 */
1252b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1253b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1254b02038faSJohn Youn 			/*
1255b02038faSJohn Youn 			 * Make sure the transfer size is no larger than one
1256b02038faSJohn Youn 			 * (micro)frame's worth of data. (A check was done
1257b02038faSJohn Youn 			 * when the periodic transfer was accepted to ensure
1258b02038faSJohn Youn 			 * that a (micro)frame's worth of data can be
1259b02038faSJohn Youn 			 * programmed into a channel.)
1260b02038faSJohn Youn 			 */
1261b02038faSJohn Youn 			u32 max_periodic_len =
1262b02038faSJohn Youn 				chan->multi_count * chan->max_packet;
1263b02038faSJohn Youn 
1264b02038faSJohn Youn 			if (chan->xfer_len > max_periodic_len)
1265b02038faSJohn Youn 				chan->xfer_len = max_periodic_len;
1266b02038faSJohn Youn 		} else if (chan->xfer_len > max_hc_xfer_size) {
1267b02038faSJohn Youn 			/*
1268b02038faSJohn Youn 			 * Make sure that xfer_len is a multiple of max packet
1269b02038faSJohn Youn 			 * size
1270b02038faSJohn Youn 			 */
1271b02038faSJohn Youn 			chan->xfer_len =
1272b02038faSJohn Youn 				max_hc_xfer_size - chan->max_packet + 1;
1273b02038faSJohn Youn 		}
1274b02038faSJohn Youn 
1275b02038faSJohn Youn 		if (chan->xfer_len > 0) {
1276b02038faSJohn Youn 			num_packets = (chan->xfer_len + chan->max_packet - 1) /
1277b02038faSJohn Youn 					chan->max_packet;
1278b02038faSJohn Youn 			if (num_packets > max_hc_pkt_count) {
1279b02038faSJohn Youn 				num_packets = max_hc_pkt_count;
1280b02038faSJohn Youn 				chan->xfer_len = num_packets * chan->max_packet;
1281415fa1c7SGuenter Roeck 			} else if (chan->ep_is_in) {
1282415fa1c7SGuenter Roeck 				/*
1283415fa1c7SGuenter Roeck 				 * Always program an integral # of max packets
1284415fa1c7SGuenter Roeck 				 * for IN transfers.
1285415fa1c7SGuenter Roeck 				 * Note: This assumes that the input buffer is
1286415fa1c7SGuenter Roeck 				 * aligned and sized accordingly.
1287415fa1c7SGuenter Roeck 				 */
1288415fa1c7SGuenter Roeck 				chan->xfer_len = num_packets * chan->max_packet;
1289b02038faSJohn Youn 			}
1290b02038faSJohn Youn 		} else {
1291b02038faSJohn Youn 			/* Need 1 packet for transfer length of 0 */
1292b02038faSJohn Youn 			num_packets = 1;
1293b02038faSJohn Youn 		}
1294b02038faSJohn Youn 
1295b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1296b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1297b02038faSJohn Youn 			/*
1298b02038faSJohn Youn 			 * Make sure that the multi_count field matches the
1299b02038faSJohn Youn 			 * actual transfer length
1300b02038faSJohn Youn 			 */
1301b02038faSJohn Youn 			chan->multi_count = num_packets;
1302b02038faSJohn Youn 
1303b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1304b02038faSJohn Youn 			dwc2_set_pid_isoc(chan);
1305b02038faSJohn Youn 
1306b02038faSJohn Youn 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1307b02038faSJohn Youn 			  TSIZ_XFERSIZE_MASK;
1308b02038faSJohn Youn 
1309b02038faSJohn Youn 		/* The ec_mc gets the multi_count for non-split */
1310b02038faSJohn Youn 		ec_mc = chan->multi_count;
1311b02038faSJohn Youn 	}
1312b02038faSJohn Youn 
1313b02038faSJohn Youn 	chan->start_pkt_count = num_packets;
1314b02038faSJohn Youn 	hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1315b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1316b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1317f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1318b02038faSJohn Youn 	if (dbg_hc(chan)) {
1319b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1320b02038faSJohn Youn 			 hctsiz, chan->hc_num);
1321b02038faSJohn Youn 
1322b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1323b02038faSJohn Youn 			 chan->hc_num);
1324b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Xfer Size: %d\n",
1325b02038faSJohn Youn 			 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1326b02038faSJohn Youn 			 TSIZ_XFERSIZE_SHIFT);
1327b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Num Pkts: %d\n",
1328b02038faSJohn Youn 			 (hctsiz & TSIZ_PKTCNT_MASK) >>
1329b02038faSJohn Youn 			 TSIZ_PKTCNT_SHIFT);
1330b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1331b02038faSJohn Youn 			 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1332b02038faSJohn Youn 			 TSIZ_SC_MC_PID_SHIFT);
1333b02038faSJohn Youn 	}
1334b02038faSJohn Youn 
133595832c00SJohn Youn 	if (hsotg->params.host_dma) {
1336af424a41SWilliam Wu 		dma_addr_t dma_addr;
1337af424a41SWilliam Wu 
1338af424a41SWilliam Wu 		if (chan->align_buf) {
1339af424a41SWilliam Wu 			if (dbg_hc(chan))
1340af424a41SWilliam Wu 				dev_vdbg(hsotg->dev, "align_buf\n");
1341af424a41SWilliam Wu 			dma_addr = chan->align_buf;
1342af424a41SWilliam Wu 		} else {
1343af424a41SWilliam Wu 			dma_addr = chan->xfer_dma;
1344af424a41SWilliam Wu 		}
1345f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, (u32)dma_addr, HCDMA(chan->hc_num));
1346af424a41SWilliam Wu 
1347b02038faSJohn Youn 		if (dbg_hc(chan))
1348b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1349af424a41SWilliam Wu 				 (unsigned long)dma_addr, chan->hc_num);
1350b02038faSJohn Youn 	}
1351b02038faSJohn Youn 
1352b02038faSJohn Youn 	/* Start the split */
1353b02038faSJohn Youn 	if (chan->do_split) {
1354f25c42b8SGevorg Sahakyan 		u32 hcsplt = dwc2_readl(hsotg, HCSPLT(chan->hc_num));
1355b02038faSJohn Youn 
1356b02038faSJohn Youn 		hcsplt |= HCSPLT_SPLTENA;
1357f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcsplt, HCSPLT(chan->hc_num));
1358b02038faSJohn Youn 	}
1359b02038faSJohn Youn 
1360f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1361b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1362b02038faSJohn Youn 	hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1363b02038faSJohn Youn 	dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1364b02038faSJohn Youn 
1365b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1366b02038faSJohn Youn 		dev_warn(hsotg->dev,
1367b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1368b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1369b02038faSJohn Youn 
1370b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1371b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1372b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1373b02038faSJohn Youn 
1374b02038faSJohn Youn 	if (dbg_hc(chan))
1375b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1376b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1377b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1378b02038faSJohn Youn 
1379f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1380b02038faSJohn Youn 	if (dbg_hc(chan))
1381b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1382b02038faSJohn Youn 			 chan->hc_num);
1383b02038faSJohn Youn 
1384b02038faSJohn Youn 	chan->xfer_started = 1;
1385b02038faSJohn Youn 	chan->requests++;
1386b02038faSJohn Youn 
138795832c00SJohn Youn 	if (!hsotg->params.host_dma &&
1388b02038faSJohn Youn 	    !chan->ep_is_in && chan->xfer_len > 0)
1389b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1390b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1391b02038faSJohn Youn }
1392b02038faSJohn Youn 
1393b02038faSJohn Youn /**
1394b02038faSJohn Youn  * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1395b02038faSJohn Youn  * host channel and starts the transfer in Descriptor DMA mode
1396b02038faSJohn Youn  *
1397b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1398b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1399b02038faSJohn Youn  *
1400b02038faSJohn Youn  * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1401b02038faSJohn Youn  * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1402b02038faSJohn Youn  * with micro-frame bitmap.
1403b02038faSJohn Youn  *
1404b02038faSJohn Youn  * Initializes HCDMA register with descriptor list address and CTD value then
1405b02038faSJohn Youn  * starts the transfer via enabling the channel.
1406b02038faSJohn Youn  */
1407b02038faSJohn Youn void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1408b02038faSJohn Youn 				 struct dwc2_host_chan *chan)
1409b02038faSJohn Youn {
1410b02038faSJohn Youn 	u32 hcchar;
1411b02038faSJohn Youn 	u32 hctsiz = 0;
1412b02038faSJohn Youn 
1413b02038faSJohn Youn 	if (chan->do_ping)
1414b02038faSJohn Youn 		hctsiz |= TSIZ_DOPNG;
1415b02038faSJohn Youn 
1416b02038faSJohn Youn 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1417b02038faSJohn Youn 		dwc2_set_pid_isoc(chan);
1418b02038faSJohn Youn 
1419b02038faSJohn Youn 	/* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1420b02038faSJohn Youn 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1421b02038faSJohn Youn 		  TSIZ_SC_MC_PID_MASK;
1422b02038faSJohn Youn 
1423b02038faSJohn Youn 	/* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1424b02038faSJohn Youn 	hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1425b02038faSJohn Youn 
1426b02038faSJohn Youn 	/* Non-zero only for high-speed interrupt endpoints */
1427b02038faSJohn Youn 	hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1428b02038faSJohn Youn 
1429b02038faSJohn Youn 	if (dbg_hc(chan)) {
1430b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1431b02038faSJohn Youn 			 chan->hc_num);
1432b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1433b02038faSJohn Youn 			 chan->data_pid_start);
1434b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 NTD: %d\n", chan->ntd - 1);
1435b02038faSJohn Youn 	}
1436b02038faSJohn Youn 
1437f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hctsiz, HCTSIZ(chan->hc_num));
1438b02038faSJohn Youn 
1439b02038faSJohn Youn 	dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1440b02038faSJohn Youn 				   chan->desc_list_sz, DMA_TO_DEVICE);
1441b02038faSJohn Youn 
1442f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, chan->desc_list_addr, HCDMA(chan->hc_num));
1443b02038faSJohn Youn 
1444b02038faSJohn Youn 	if (dbg_hc(chan))
1445b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1446b02038faSJohn Youn 			 &chan->desc_list_addr, chan->hc_num);
1447b02038faSJohn Youn 
1448f25c42b8SGevorg Sahakyan 	hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1449b02038faSJohn Youn 	hcchar &= ~HCCHAR_MULTICNT_MASK;
1450b02038faSJohn Youn 	hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1451b02038faSJohn Youn 		  HCCHAR_MULTICNT_MASK;
1452b02038faSJohn Youn 
1453b02038faSJohn Youn 	if (hcchar & HCCHAR_CHDIS)
1454b02038faSJohn Youn 		dev_warn(hsotg->dev,
1455b02038faSJohn Youn 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1456b02038faSJohn Youn 			 __func__, chan->hc_num, hcchar);
1457b02038faSJohn Youn 
1458b02038faSJohn Youn 	/* Set host channel enable after all other setup is complete */
1459b02038faSJohn Youn 	hcchar |= HCCHAR_CHENA;
1460b02038faSJohn Youn 	hcchar &= ~HCCHAR_CHDIS;
1461b02038faSJohn Youn 
1462b02038faSJohn Youn 	if (dbg_hc(chan))
1463b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
1464b02038faSJohn Youn 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
1465b02038faSJohn Youn 			 HCCHAR_MULTICNT_SHIFT);
1466b02038faSJohn Youn 
1467f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1468b02038faSJohn Youn 	if (dbg_hc(chan))
1469b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1470b02038faSJohn Youn 			 chan->hc_num);
1471b02038faSJohn Youn 
1472b02038faSJohn Youn 	chan->xfer_started = 1;
1473b02038faSJohn Youn 	chan->requests++;
1474b02038faSJohn Youn }
1475b02038faSJohn Youn 
1476b02038faSJohn Youn /**
1477b02038faSJohn Youn  * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1478b02038faSJohn Youn  * a previous call to dwc2_hc_start_transfer()
1479b02038faSJohn Youn  *
1480b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
1481b02038faSJohn Youn  * @chan:  Information needed to initialize the host channel
1482b02038faSJohn Youn  *
1483b02038faSJohn Youn  * The caller must ensure there is sufficient space in the request queue and Tx
1484b02038faSJohn Youn  * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1485b02038faSJohn Youn  * the controller acts autonomously to complete transfers programmed to a host
1486b02038faSJohn Youn  * channel.
1487b02038faSJohn Youn  *
1488b02038faSJohn Youn  * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1489b02038faSJohn Youn  * if there is any data remaining to be queued. For an IN transfer, another
1490b02038faSJohn Youn  * data packet is always requested. For the SETUP phase of a control transfer,
1491b02038faSJohn Youn  * this function does nothing.
1492b02038faSJohn Youn  *
1493b02038faSJohn Youn  * Return: 1 if a new request is queued, 0 if no more requests are required
1494b02038faSJohn Youn  * for this transfer
1495b02038faSJohn Youn  */
1496b02038faSJohn Youn static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1497b02038faSJohn Youn 				     struct dwc2_host_chan *chan)
1498b02038faSJohn Youn {
1499b02038faSJohn Youn 	if (dbg_hc(chan))
1500b02038faSJohn Youn 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1501b02038faSJohn Youn 			 chan->hc_num);
1502b02038faSJohn Youn 
1503b02038faSJohn Youn 	if (chan->do_split)
1504b02038faSJohn Youn 		/* SPLITs always queue just once per channel */
1505b02038faSJohn Youn 		return 0;
1506b02038faSJohn Youn 
1507b02038faSJohn Youn 	if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1508b02038faSJohn Youn 		/* SETUPs are queued only once since they can't be NAK'd */
1509b02038faSJohn Youn 		return 0;
1510b02038faSJohn Youn 
1511b02038faSJohn Youn 	if (chan->ep_is_in) {
1512b02038faSJohn Youn 		/*
1513b02038faSJohn Youn 		 * Always queue another request for other IN transfers. If
1514b02038faSJohn Youn 		 * back-to-back INs are issued and NAKs are received for both,
1515b02038faSJohn Youn 		 * the driver may still be processing the first NAK when the
1516b02038faSJohn Youn 		 * second NAK is received. When the interrupt handler clears
1517b02038faSJohn Youn 		 * the NAK interrupt for the first NAK, the second NAK will
1518b02038faSJohn Youn 		 * not be seen. So we can't depend on the NAK interrupt
1519b02038faSJohn Youn 		 * handler to requeue a NAK'd request. Instead, IN requests
1520b02038faSJohn Youn 		 * are issued each time this function is called. When the
1521b02038faSJohn Youn 		 * transfer completes, the extra requests for the channel will
1522b02038faSJohn Youn 		 * be flushed.
1523b02038faSJohn Youn 		 */
1524f25c42b8SGevorg Sahakyan 		u32 hcchar = dwc2_readl(hsotg, HCCHAR(chan->hc_num));
1525b02038faSJohn Youn 
1526b02038faSJohn Youn 		dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1527b02038faSJohn Youn 		hcchar |= HCCHAR_CHENA;
1528b02038faSJohn Youn 		hcchar &= ~HCCHAR_CHDIS;
1529b02038faSJohn Youn 		if (dbg_hc(chan))
1530b02038faSJohn Youn 			dev_vdbg(hsotg->dev, "	 IN xfer: hcchar = 0x%08x\n",
1531b02038faSJohn Youn 				 hcchar);
1532f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcchar, HCCHAR(chan->hc_num));
1533b02038faSJohn Youn 		chan->requests++;
1534b02038faSJohn Youn 		return 1;
1535b02038faSJohn Youn 	}
1536b02038faSJohn Youn 
1537b02038faSJohn Youn 	/* OUT transfers */
1538b02038faSJohn Youn 
1539b02038faSJohn Youn 	if (chan->xfer_count < chan->xfer_len) {
1540b02038faSJohn Youn 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1541b02038faSJohn Youn 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1542f25c42b8SGevorg Sahakyan 			u32 hcchar = dwc2_readl(hsotg,
1543b02038faSJohn Youn 						HCCHAR(chan->hc_num));
1544b02038faSJohn Youn 
1545b02038faSJohn Youn 			dwc2_hc_set_even_odd_frame(hsotg, chan,
1546b02038faSJohn Youn 						   &hcchar);
1547b02038faSJohn Youn 		}
1548b02038faSJohn Youn 
1549b02038faSJohn Youn 		/* Load OUT packet into the appropriate Tx FIFO */
1550b02038faSJohn Youn 		dwc2_hc_write_packet(hsotg, chan);
1551b02038faSJohn Youn 		chan->requests++;
1552b02038faSJohn Youn 		return 1;
1553b02038faSJohn Youn 	}
1554b02038faSJohn Youn 
1555b02038faSJohn Youn 	return 0;
1556b02038faSJohn Youn }
1557b02038faSJohn Youn 
1558b02038faSJohn Youn /*
1559b02038faSJohn Youn  * =========================================================================
1560b02038faSJohn Youn  *  HCD
1561b02038faSJohn Youn  * =========================================================================
1562b02038faSJohn Youn  */
1563b02038faSJohn Youn 
1564b02038faSJohn Youn /*
1565197ba5f4SPaul Zimmerman  * Processes all the URBs in a single list of QHs. Completes them with
1566197ba5f4SPaul Zimmerman  * -ETIMEDOUT and frees the QTD.
1567197ba5f4SPaul Zimmerman  *
1568197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1569197ba5f4SPaul Zimmerman  */
1570197ba5f4SPaul Zimmerman static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1571197ba5f4SPaul Zimmerman 				      struct list_head *qh_list)
1572197ba5f4SPaul Zimmerman {
1573197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1574197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1575197ba5f4SPaul Zimmerman 
1576197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1577197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1578197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
15792e84da6eSGregory Herrero 			dwc2_host_complete(hsotg, qtd, -ECONNRESET);
1580197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1581197ba5f4SPaul Zimmerman 		}
1582197ba5f4SPaul Zimmerman 	}
1583197ba5f4SPaul Zimmerman }
1584197ba5f4SPaul Zimmerman 
1585197ba5f4SPaul Zimmerman static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1586197ba5f4SPaul Zimmerman 			      struct list_head *qh_list)
1587197ba5f4SPaul Zimmerman {
1588197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1589197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh, *qh_tmp;
1590197ba5f4SPaul Zimmerman 	unsigned long flags;
1591197ba5f4SPaul Zimmerman 
1592197ba5f4SPaul Zimmerman 	if (!qh_list->next)
1593197ba5f4SPaul Zimmerman 		/* The list hasn't been initialized yet */
1594197ba5f4SPaul Zimmerman 		return;
1595197ba5f4SPaul Zimmerman 
1596197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
1597197ba5f4SPaul Zimmerman 
1598197ba5f4SPaul Zimmerman 	/* Ensure there are no QTDs or URBs left */
1599197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1600197ba5f4SPaul Zimmerman 
1601197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1602197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1603197ba5f4SPaul Zimmerman 
1604197ba5f4SPaul Zimmerman 		/* Free each QTD in the QH's QTD list */
1605197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1606197ba5f4SPaul Zimmerman 					 qtd_list_entry)
1607197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1608197ba5f4SPaul Zimmerman 
160916e80218SDouglas Anderson 		if (qh->channel && qh->channel->qh == qh)
161016e80218SDouglas Anderson 			qh->channel->qh = NULL;
161116e80218SDouglas Anderson 
1612197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
1613197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_free(hsotg, qh);
1614197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
1615197ba5f4SPaul Zimmerman 	}
1616197ba5f4SPaul Zimmerman 
1617197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
1618197ba5f4SPaul Zimmerman }
1619197ba5f4SPaul Zimmerman 
1620197ba5f4SPaul Zimmerman /*
1621197ba5f4SPaul Zimmerman  * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1622197ba5f4SPaul Zimmerman  * and periodic schedules. The QTD associated with each URB is removed from
1623197ba5f4SPaul Zimmerman  * the schedule and freed. This function may be called when a disconnect is
1624197ba5f4SPaul Zimmerman  * detected or when the HCD is being stopped.
1625197ba5f4SPaul Zimmerman  *
1626197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1627197ba5f4SPaul Zimmerman  */
1628197ba5f4SPaul Zimmerman static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1629197ba5f4SPaul Zimmerman {
1630197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
163138d2b5fbSDouglas Anderson 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
1632197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1633197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1634197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1635197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1636197ba5f4SPaul Zimmerman 	dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1637197ba5f4SPaul Zimmerman }
1638197ba5f4SPaul Zimmerman 
1639197ba5f4SPaul Zimmerman /**
1640197ba5f4SPaul Zimmerman  * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1641197ba5f4SPaul Zimmerman  *
1642197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1643197ba5f4SPaul Zimmerman  */
1644197ba5f4SPaul Zimmerman void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1645197ba5f4SPaul Zimmerman {
1646197ba5f4SPaul Zimmerman 	u32 hprt0;
1647197ba5f4SPaul Zimmerman 
1648197ba5f4SPaul Zimmerman 	if (hsotg->op_state == OTG_STATE_B_HOST) {
1649197ba5f4SPaul Zimmerman 		/*
1650197ba5f4SPaul Zimmerman 		 * Reset the port. During a HNP mode switch the reset
1651197ba5f4SPaul Zimmerman 		 * needs to occur within 1ms and have a duration of at
1652197ba5f4SPaul Zimmerman 		 * least 50ms.
1653197ba5f4SPaul Zimmerman 		 */
1654197ba5f4SPaul Zimmerman 		hprt0 = dwc2_read_hprt0(hsotg);
1655197ba5f4SPaul Zimmerman 		hprt0 |= HPRT0_RST;
1656f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
1657197ba5f4SPaul Zimmerman 	}
1658197ba5f4SPaul Zimmerman 
1659197ba5f4SPaul Zimmerman 	queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1660197ba5f4SPaul Zimmerman 			   msecs_to_jiffies(50));
1661197ba5f4SPaul Zimmerman }
1662197ba5f4SPaul Zimmerman 
1663197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1664197ba5f4SPaul Zimmerman static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1665197ba5f4SPaul Zimmerman {
1666bea8e86cSJohn Youn 	int num_channels = hsotg->params.host_channels;
1667197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
1668197ba5f4SPaul Zimmerman 	u32 hcchar;
1669197ba5f4SPaul Zimmerman 	int i;
1670197ba5f4SPaul Zimmerman 
167195832c00SJohn Youn 	if (!hsotg->params.host_dma) {
1672197ba5f4SPaul Zimmerman 		/* Flush out any channel requests in slave mode */
1673197ba5f4SPaul Zimmerman 		for (i = 0; i < num_channels; i++) {
1674197ba5f4SPaul Zimmerman 			channel = hsotg->hc_ptr_array[i];
1675197ba5f4SPaul Zimmerman 			if (!list_empty(&channel->hc_list_entry))
1676197ba5f4SPaul Zimmerman 				continue;
1677f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
1678197ba5f4SPaul Zimmerman 			if (hcchar & HCCHAR_CHENA) {
1679197ba5f4SPaul Zimmerman 				hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1680197ba5f4SPaul Zimmerman 				hcchar |= HCCHAR_CHDIS;
1681f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
1682197ba5f4SPaul Zimmerman 			}
1683197ba5f4SPaul Zimmerman 		}
1684197ba5f4SPaul Zimmerman 	}
1685197ba5f4SPaul Zimmerman 
1686197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
1687197ba5f4SPaul Zimmerman 		channel = hsotg->hc_ptr_array[i];
1688197ba5f4SPaul Zimmerman 		if (!list_empty(&channel->hc_list_entry))
1689197ba5f4SPaul Zimmerman 			continue;
1690f25c42b8SGevorg Sahakyan 		hcchar = dwc2_readl(hsotg, HCCHAR(i));
1691197ba5f4SPaul Zimmerman 		if (hcchar & HCCHAR_CHENA) {
1692197ba5f4SPaul Zimmerman 			/* Halt the channel */
1693197ba5f4SPaul Zimmerman 			hcchar |= HCCHAR_CHDIS;
1694f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcchar, HCCHAR(i));
1695197ba5f4SPaul Zimmerman 		}
1696197ba5f4SPaul Zimmerman 
1697197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, channel);
1698197ba5f4SPaul Zimmerman 		list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1699197ba5f4SPaul Zimmerman 		/*
1700197ba5f4SPaul Zimmerman 		 * Added for Descriptor DMA to prevent channel double cleanup in
1701197ba5f4SPaul Zimmerman 		 * release_channel_ddma(), which is called from ep_disable when
1702197ba5f4SPaul Zimmerman 		 * device disconnects
1703197ba5f4SPaul Zimmerman 		 */
1704197ba5f4SPaul Zimmerman 		channel->qh = NULL;
1705197ba5f4SPaul Zimmerman 	}
17067252f1bfSVincent Palatin 	/* All channels have been freed, mark them available */
170795832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
17087252f1bfSVincent Palatin 		hsotg->available_host_channels =
1709bea8e86cSJohn Youn 			hsotg->params.host_channels;
17107252f1bfSVincent Palatin 	} else {
17117252f1bfSVincent Palatin 		hsotg->non_periodic_channels = 0;
17127252f1bfSVincent Palatin 		hsotg->periodic_channels = 0;
17137252f1bfSVincent Palatin 	}
1714197ba5f4SPaul Zimmerman }
1715197ba5f4SPaul Zimmerman 
1716197ba5f4SPaul Zimmerman /**
17176a659531SDouglas Anderson  * dwc2_hcd_connect() - Handles connect of the HCD
1718197ba5f4SPaul Zimmerman  *
1719197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1720197ba5f4SPaul Zimmerman  *
1721197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1722197ba5f4SPaul Zimmerman  */
17236a659531SDouglas Anderson void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
17246a659531SDouglas Anderson {
17256a659531SDouglas Anderson 	if (hsotg->lx_state != DWC2_L0)
17266a659531SDouglas Anderson 		usb_hcd_resume_root_hub(hsotg->priv);
17276a659531SDouglas Anderson 
17286a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status_change = 1;
17296a659531SDouglas Anderson 	hsotg->flags.b.port_connect_status = 1;
17306a659531SDouglas Anderson }
17316a659531SDouglas Anderson 
17326a659531SDouglas Anderson /**
17336a659531SDouglas Anderson  * dwc2_hcd_disconnect() - Handles disconnect of the HCD
17346a659531SDouglas Anderson  *
17356a659531SDouglas Anderson  * @hsotg: Pointer to struct dwc2_hsotg
17366a659531SDouglas Anderson  * @force: If true, we won't try to reconnect even if we see device connected.
17376a659531SDouglas Anderson  *
17386a659531SDouglas Anderson  * Must be called with interrupt disabled and spinlock held
17396a659531SDouglas Anderson  */
17406a659531SDouglas Anderson void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
1741197ba5f4SPaul Zimmerman {
1742197ba5f4SPaul Zimmerman 	u32 intr;
17436a659531SDouglas Anderson 	u32 hprt0;
1744197ba5f4SPaul Zimmerman 
1745197ba5f4SPaul Zimmerman 	/* Set status flags for the hub driver */
1746197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status_change = 1;
1747197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_connect_status = 0;
1748197ba5f4SPaul Zimmerman 
1749197ba5f4SPaul Zimmerman 	/*
1750197ba5f4SPaul Zimmerman 	 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1751197ba5f4SPaul Zimmerman 	 * interrupt mask and status bits and disabling subsequent host
1752197ba5f4SPaul Zimmerman 	 * channel interrupts.
1753197ba5f4SPaul Zimmerman 	 */
1754f25c42b8SGevorg Sahakyan 	intr = dwc2_readl(hsotg, GINTMSK);
1755197ba5f4SPaul Zimmerman 	intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
1756f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTMSK);
1757197ba5f4SPaul Zimmerman 	intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
1758f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, intr, GINTSTS);
1759197ba5f4SPaul Zimmerman 
1760197ba5f4SPaul Zimmerman 	/*
1761197ba5f4SPaul Zimmerman 	 * Turn off the vbus power only if the core has transitioned to device
1762197ba5f4SPaul Zimmerman 	 * mode. If still in host mode, need to keep power on to detect a
1763197ba5f4SPaul Zimmerman 	 * reconnection.
1764197ba5f4SPaul Zimmerman 	 */
1765197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
1766197ba5f4SPaul Zimmerman 		if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1767197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
1768f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, HPRT0);
1769197ba5f4SPaul Zimmerman 		}
1770197ba5f4SPaul Zimmerman 
1771197ba5f4SPaul Zimmerman 		dwc2_disable_host_interrupts(hsotg);
1772197ba5f4SPaul Zimmerman 	}
1773197ba5f4SPaul Zimmerman 
1774197ba5f4SPaul Zimmerman 	/* Respond with an error status to all URBs in the schedule */
1775197ba5f4SPaul Zimmerman 	dwc2_kill_all_urbs(hsotg);
1776197ba5f4SPaul Zimmerman 
1777197ba5f4SPaul Zimmerman 	if (dwc2_is_host_mode(hsotg))
1778197ba5f4SPaul Zimmerman 		/* Clean up any host channels that were in use */
1779197ba5f4SPaul Zimmerman 		dwc2_hcd_cleanup_channels(hsotg);
1780197ba5f4SPaul Zimmerman 
1781197ba5f4SPaul Zimmerman 	dwc2_host_disconnect(hsotg);
17826a659531SDouglas Anderson 
17836a659531SDouglas Anderson 	/*
17846a659531SDouglas Anderson 	 * Add an extra check here to see if we're actually connected but
17856a659531SDouglas Anderson 	 * we don't have a detection interrupt pending.  This can happen if:
17866a659531SDouglas Anderson 	 *   1. hardware sees connect
17876a659531SDouglas Anderson 	 *   2. hardware sees disconnect
17886a659531SDouglas Anderson 	 *   3. hardware sees connect
17896a659531SDouglas Anderson 	 *   4. dwc2_port_intr() - clears connect interrupt
17906a659531SDouglas Anderson 	 *   5. dwc2_handle_common_intr() - calls here
17916a659531SDouglas Anderson 	 *
17926a659531SDouglas Anderson 	 * Without the extra check here we will end calling disconnect
17936a659531SDouglas Anderson 	 * and won't get any future interrupts to handle the connect.
17946a659531SDouglas Anderson 	 */
17956a659531SDouglas Anderson 	if (!force) {
1796f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
17976a659531SDouglas Anderson 		if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
17986a659531SDouglas Anderson 			dwc2_hcd_connect(hsotg);
17996a659531SDouglas Anderson 	}
1800197ba5f4SPaul Zimmerman }
1801197ba5f4SPaul Zimmerman 
1802197ba5f4SPaul Zimmerman /**
1803197ba5f4SPaul Zimmerman  * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
1804197ba5f4SPaul Zimmerman  *
1805197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1806197ba5f4SPaul Zimmerman  */
1807197ba5f4SPaul Zimmerman static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
1808197ba5f4SPaul Zimmerman {
18091fb7f12dSDouglas Anderson 	if (hsotg->bus_suspended) {
1810197ba5f4SPaul Zimmerman 		hsotg->flags.b.port_suspend_change = 1;
1811b46146d5SGregory Herrero 		usb_hcd_resume_root_hub(hsotg->priv);
1812197ba5f4SPaul Zimmerman 	}
18131fb7f12dSDouglas Anderson 
18141fb7f12dSDouglas Anderson 	if (hsotg->lx_state == DWC2_L1)
18151fb7f12dSDouglas Anderson 		hsotg->flags.b.port_l1_change = 1;
1816b46146d5SGregory Herrero }
1817197ba5f4SPaul Zimmerman 
1818197ba5f4SPaul Zimmerman /**
1819197ba5f4SPaul Zimmerman  * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
1820197ba5f4SPaul Zimmerman  *
1821197ba5f4SPaul Zimmerman  * @hsotg: Pointer to struct dwc2_hsotg
1822197ba5f4SPaul Zimmerman  *
1823197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
1824197ba5f4SPaul Zimmerman  */
1825197ba5f4SPaul Zimmerman void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
1826197ba5f4SPaul Zimmerman {
1827197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
1828197ba5f4SPaul Zimmerman 
1829197ba5f4SPaul Zimmerman 	/*
1830197ba5f4SPaul Zimmerman 	 * The root hub should be disconnected before this function is called.
1831197ba5f4SPaul Zimmerman 	 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
1832197ba5f4SPaul Zimmerman 	 * and the QH lists (via ..._hcd_endpoint_disable).
1833197ba5f4SPaul Zimmerman 	 */
1834197ba5f4SPaul Zimmerman 
1835197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
1836197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
1837197ba5f4SPaul Zimmerman 
1838197ba5f4SPaul Zimmerman 	/* Turn off the vbus power */
1839197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "PortPower off\n");
1840f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, HPRT0);
1841197ba5f4SPaul Zimmerman }
1842197ba5f4SPaul Zimmerman 
184333ad261aSGregory Herrero /* Caller must hold driver lock */
1844197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
1845b58e6ceeSMian Yousaf Kaukab 				struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
1846b5a468a6SMian Yousaf Kaukab 				struct dwc2_qtd *qtd)
1847197ba5f4SPaul Zimmerman {
1848197ba5f4SPaul Zimmerman 	u32 intr_mask;
1849197ba5f4SPaul Zimmerman 	int retval;
1850197ba5f4SPaul Zimmerman 	int dev_speed;
1851197ba5f4SPaul Zimmerman 
1852197ba5f4SPaul Zimmerman 	if (!hsotg->flags.b.port_connect_status) {
1853197ba5f4SPaul Zimmerman 		/* No longer connected */
1854197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Not connected\n");
1855197ba5f4SPaul Zimmerman 		return -ENODEV;
1856197ba5f4SPaul Zimmerman 	}
1857197ba5f4SPaul Zimmerman 
1858197ba5f4SPaul Zimmerman 	dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1859197ba5f4SPaul Zimmerman 
1860197ba5f4SPaul Zimmerman 	/* Some configurations cannot support LS traffic on a FS root port */
1861197ba5f4SPaul Zimmerman 	if ((dev_speed == USB_SPEED_LOW) &&
1862197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
1863197ba5f4SPaul Zimmerman 	    (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
1864f25c42b8SGevorg Sahakyan 		u32 hprt0 = dwc2_readl(hsotg, HPRT0);
1865197ba5f4SPaul Zimmerman 		u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1866197ba5f4SPaul Zimmerman 
1867197ba5f4SPaul Zimmerman 		if (prtspd == HPRT0_SPD_FULL_SPEED)
1868197ba5f4SPaul Zimmerman 			return -ENODEV;
1869197ba5f4SPaul Zimmerman 	}
1870197ba5f4SPaul Zimmerman 
1871197ba5f4SPaul Zimmerman 	if (!qtd)
1872b5a468a6SMian Yousaf Kaukab 		return -EINVAL;
1873197ba5f4SPaul Zimmerman 
1874197ba5f4SPaul Zimmerman 	dwc2_hcd_qtd_init(qtd, urb);
1875b58e6ceeSMian Yousaf Kaukab 	retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
1876197ba5f4SPaul Zimmerman 	if (retval) {
1877197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
1878197ba5f4SPaul Zimmerman 			"DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
1879197ba5f4SPaul Zimmerman 			retval);
1880197ba5f4SPaul Zimmerman 		return retval;
1881197ba5f4SPaul Zimmerman 	}
1882197ba5f4SPaul Zimmerman 
1883f25c42b8SGevorg Sahakyan 	intr_mask = dwc2_readl(hsotg, GINTMSK);
1884197ba5f4SPaul Zimmerman 	if (!(intr_mask & GINTSTS_SOF)) {
1885197ba5f4SPaul Zimmerman 		enum dwc2_transaction_type tr_type;
1886197ba5f4SPaul Zimmerman 
1887197ba5f4SPaul Zimmerman 		if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
1888197ba5f4SPaul Zimmerman 		    !(qtd->urb->flags & URB_GIVEBACK_ASAP))
1889197ba5f4SPaul Zimmerman 			/*
1890197ba5f4SPaul Zimmerman 			 * Do not schedule SG transactions until qtd has
1891197ba5f4SPaul Zimmerman 			 * URB_GIVEBACK_ASAP set
1892197ba5f4SPaul Zimmerman 			 */
1893197ba5f4SPaul Zimmerman 			return 0;
1894197ba5f4SPaul Zimmerman 
1895197ba5f4SPaul Zimmerman 		tr_type = dwc2_hcd_select_transactions(hsotg);
1896197ba5f4SPaul Zimmerman 		if (tr_type != DWC2_TRANSACTION_NONE)
1897197ba5f4SPaul Zimmerman 			dwc2_hcd_queue_transactions(hsotg, tr_type);
1898197ba5f4SPaul Zimmerman 	}
1899197ba5f4SPaul Zimmerman 
1900197ba5f4SPaul Zimmerman 	return 0;
1901197ba5f4SPaul Zimmerman }
1902197ba5f4SPaul Zimmerman 
1903197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
1904197ba5f4SPaul Zimmerman static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
1905197ba5f4SPaul Zimmerman 				struct dwc2_hcd_urb *urb)
1906197ba5f4SPaul Zimmerman {
1907197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
1908197ba5f4SPaul Zimmerman 	struct dwc2_qtd *urb_qtd;
1909197ba5f4SPaul Zimmerman 
1910197ba5f4SPaul Zimmerman 	urb_qtd = urb->qtd;
1911197ba5f4SPaul Zimmerman 	if (!urb_qtd) {
1912197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
1913197ba5f4SPaul Zimmerman 		return -EINVAL;
1914197ba5f4SPaul Zimmerman 	}
1915197ba5f4SPaul Zimmerman 
1916197ba5f4SPaul Zimmerman 	qh = urb_qtd->qh;
1917197ba5f4SPaul Zimmerman 	if (!qh) {
1918197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
1919197ba5f4SPaul Zimmerman 		return -EINVAL;
1920197ba5f4SPaul Zimmerman 	}
1921197ba5f4SPaul Zimmerman 
1922197ba5f4SPaul Zimmerman 	urb->priv = NULL;
1923197ba5f4SPaul Zimmerman 
1924197ba5f4SPaul Zimmerman 	if (urb_qtd->in_process && qh->channel) {
1925197ba5f4SPaul Zimmerman 		dwc2_dump_channel_info(hsotg, qh->channel);
1926197ba5f4SPaul Zimmerman 
1927197ba5f4SPaul Zimmerman 		/* The QTD is in process (it has been assigned to a channel) */
1928197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status)
1929197ba5f4SPaul Zimmerman 			/*
1930197ba5f4SPaul Zimmerman 			 * If still connected (i.e. in host mode), halt the
1931197ba5f4SPaul Zimmerman 			 * channel so it can be used for other transfers. If
1932197ba5f4SPaul Zimmerman 			 * no longer connected, the host registers can't be
1933197ba5f4SPaul Zimmerman 			 * written to halt the channel since the core is in
1934197ba5f4SPaul Zimmerman 			 * device mode.
1935197ba5f4SPaul Zimmerman 			 */
1936197ba5f4SPaul Zimmerman 			dwc2_hc_halt(hsotg, qh->channel,
1937197ba5f4SPaul Zimmerman 				     DWC2_HC_XFER_URB_DEQUEUE);
1938197ba5f4SPaul Zimmerman 	}
1939197ba5f4SPaul Zimmerman 
1940197ba5f4SPaul Zimmerman 	/*
1941197ba5f4SPaul Zimmerman 	 * Free the QTD and clean up the associated QH. Leave the QH in the
1942197ba5f4SPaul Zimmerman 	 * schedule if it has any remaining QTDs.
1943197ba5f4SPaul Zimmerman 	 */
194495832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
1945197ba5f4SPaul Zimmerman 		u8 in_process = urb_qtd->in_process;
1946197ba5f4SPaul Zimmerman 
1947197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1948197ba5f4SPaul Zimmerman 		if (in_process) {
1949197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_deactivate(hsotg, qh, 0);
1950197ba5f4SPaul Zimmerman 			qh->channel = NULL;
1951197ba5f4SPaul Zimmerman 		} else if (list_empty(&qh->qtd_list)) {
1952197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
1953197ba5f4SPaul Zimmerman 		}
1954197ba5f4SPaul Zimmerman 	} else {
1955197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
1956197ba5f4SPaul Zimmerman 	}
1957197ba5f4SPaul Zimmerman 
1958197ba5f4SPaul Zimmerman 	return 0;
1959197ba5f4SPaul Zimmerman }
1960197ba5f4SPaul Zimmerman 
1961197ba5f4SPaul Zimmerman /* Must NOT be called with interrupt disabled or spinlock held */
1962197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
1963197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep, int retry)
1964197ba5f4SPaul Zimmerman {
1965197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
1966197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
1967197ba5f4SPaul Zimmerman 	unsigned long flags;
1968197ba5f4SPaul Zimmerman 	int rc;
1969197ba5f4SPaul Zimmerman 
1970197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
1971197ba5f4SPaul Zimmerman 
1972197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
1973197ba5f4SPaul Zimmerman 	if (!qh) {
1974197ba5f4SPaul Zimmerman 		rc = -EINVAL;
1975197ba5f4SPaul Zimmerman 		goto err;
1976197ba5f4SPaul Zimmerman 	}
1977197ba5f4SPaul Zimmerman 
1978197ba5f4SPaul Zimmerman 	while (!list_empty(&qh->qtd_list) && retry--) {
1979197ba5f4SPaul Zimmerman 		if (retry == 0) {
1980197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
1981197ba5f4SPaul Zimmerman 				"## timeout in dwc2_hcd_endpoint_disable() ##\n");
1982197ba5f4SPaul Zimmerman 			rc = -EBUSY;
1983197ba5f4SPaul Zimmerman 			goto err;
1984197ba5f4SPaul Zimmerman 		}
1985197ba5f4SPaul Zimmerman 
1986197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
198704a9db79SNicholas Mc Guire 		msleep(20);
1988197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
1989197ba5f4SPaul Zimmerman 		qh = ep->hcpriv;
1990197ba5f4SPaul Zimmerman 		if (!qh) {
1991197ba5f4SPaul Zimmerman 			rc = -EINVAL;
1992197ba5f4SPaul Zimmerman 			goto err;
1993197ba5f4SPaul Zimmerman 		}
1994197ba5f4SPaul Zimmerman 	}
1995197ba5f4SPaul Zimmerman 
1996197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_unlink(hsotg, qh);
1997197ba5f4SPaul Zimmerman 
1998197ba5f4SPaul Zimmerman 	/* Free each QTD in the QH's QTD list */
1999197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2000197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2001197ba5f4SPaul Zimmerman 
2002197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
200316e80218SDouglas Anderson 
200416e80218SDouglas Anderson 	if (qh->channel && qh->channel->qh == qh)
200516e80218SDouglas Anderson 		qh->channel->qh = NULL;
200616e80218SDouglas Anderson 
2007197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
200816e80218SDouglas Anderson 
2009197ba5f4SPaul Zimmerman 	dwc2_hcd_qh_free(hsotg, qh);
2010197ba5f4SPaul Zimmerman 
2011197ba5f4SPaul Zimmerman 	return 0;
2012197ba5f4SPaul Zimmerman 
2013197ba5f4SPaul Zimmerman err:
2014197ba5f4SPaul Zimmerman 	ep->hcpriv = NULL;
2015197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
2016197ba5f4SPaul Zimmerman 
2017197ba5f4SPaul Zimmerman 	return rc;
2018197ba5f4SPaul Zimmerman }
2019197ba5f4SPaul Zimmerman 
2020197ba5f4SPaul Zimmerman /* Must be called with interrupt disabled and spinlock held */
2021197ba5f4SPaul Zimmerman static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2022197ba5f4SPaul Zimmerman 				   struct usb_host_endpoint *ep)
2023197ba5f4SPaul Zimmerman {
2024197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = ep->hcpriv;
2025197ba5f4SPaul Zimmerman 
2026197ba5f4SPaul Zimmerman 	if (!qh)
2027197ba5f4SPaul Zimmerman 		return -EINVAL;
2028197ba5f4SPaul Zimmerman 
2029197ba5f4SPaul Zimmerman 	qh->data_toggle = DWC2_HC_PID_DATA0;
2030197ba5f4SPaul Zimmerman 
2031197ba5f4SPaul Zimmerman 	return 0;
2032197ba5f4SPaul Zimmerman }
2033197ba5f4SPaul Zimmerman 
2034b02038faSJohn Youn /**
2035b02038faSJohn Youn  * dwc2_core_init() - Initializes the DWC_otg controller registers and
2036b02038faSJohn Youn  * prepares the core for device mode or host mode operation
2037b02038faSJohn Youn  *
2038b02038faSJohn Youn  * @hsotg:         Programming view of the DWC_otg controller
2039b02038faSJohn Youn  * @initial_setup: If true then this is the first init for this instance.
2040b02038faSJohn Youn  */
204165c9c4c6SVardan Mikayelyan int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2042b02038faSJohn Youn {
2043b02038faSJohn Youn 	u32 usbcfg, otgctl;
2044b02038faSJohn Youn 	int retval;
2045b02038faSJohn Youn 
2046b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2047b02038faSJohn Youn 
2048f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
2049b02038faSJohn Youn 
2050b02038faSJohn Youn 	/* Set ULPI External VBUS bit if needed */
2051b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
205295832c00SJohn Youn 	if (hsotg->params.phy_ulpi_ext_vbus)
2053b02038faSJohn Youn 		usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2054b02038faSJohn Youn 
2055b02038faSJohn Youn 	/* Set external TS Dline pulsing bit if needed */
2056b02038faSJohn Youn 	usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
205795832c00SJohn Youn 	if (hsotg->params.ts_dline)
2058b02038faSJohn Youn 		usbcfg |= GUSBCFG_TERMSELDLPULSE;
2059b02038faSJohn Youn 
2060f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
2061b02038faSJohn Youn 
2062b02038faSJohn Youn 	/*
2063b02038faSJohn Youn 	 * Reset the Controller
2064b02038faSJohn Youn 	 *
2065b02038faSJohn Youn 	 * We only need to reset the controller if this is a re-init.
2066b02038faSJohn Youn 	 * For the first init we know for sure that earlier code reset us (it
2067b02038faSJohn Youn 	 * needed to in order to properly detect various parameters).
2068b02038faSJohn Youn 	 */
2069b02038faSJohn Youn 	if (!initial_setup) {
207013b1f8e2SVardan Mikayelyan 		retval = dwc2_core_reset(hsotg, false);
2071b02038faSJohn Youn 		if (retval) {
2072b02038faSJohn Youn 			dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2073b02038faSJohn Youn 				__func__);
2074b02038faSJohn Youn 			return retval;
2075b02038faSJohn Youn 		}
2076b02038faSJohn Youn 	}
2077b02038faSJohn Youn 
2078b02038faSJohn Youn 	/*
2079b02038faSJohn Youn 	 * This needs to happen in FS mode before any other programming occurs
2080b02038faSJohn Youn 	 */
2081b02038faSJohn Youn 	retval = dwc2_phy_init(hsotg, initial_setup);
2082b02038faSJohn Youn 	if (retval)
2083b02038faSJohn Youn 		return retval;
2084b02038faSJohn Youn 
2085b02038faSJohn Youn 	/* Program the GAHBCFG Register */
2086b02038faSJohn Youn 	retval = dwc2_gahbcfg_init(hsotg);
2087b02038faSJohn Youn 	if (retval)
2088b02038faSJohn Youn 		return retval;
2089b02038faSJohn Youn 
2090b02038faSJohn Youn 	/* Program the GUSBCFG register */
2091b02038faSJohn Youn 	dwc2_gusbcfg_init(hsotg);
2092b02038faSJohn Youn 
2093b02038faSJohn Youn 	/* Program the GOTGCTL register */
2094f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2095b02038faSJohn Youn 	otgctl &= ~GOTGCTL_OTGVER;
2096f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2097b02038faSJohn Youn 
2098b02038faSJohn Youn 	/* Clear the SRP success bit for FS-I2c */
2099b02038faSJohn Youn 	hsotg->srp_success = 0;
2100b02038faSJohn Youn 
2101b02038faSJohn Youn 	/* Enable common interrupts */
2102b02038faSJohn Youn 	dwc2_enable_common_interrupts(hsotg);
2103b02038faSJohn Youn 
2104b02038faSJohn Youn 	/*
2105b02038faSJohn Youn 	 * Do device or host initialization based on mode during PCD and
2106b02038faSJohn Youn 	 * HCD initialization
2107b02038faSJohn Youn 	 */
2108b02038faSJohn Youn 	if (dwc2_is_host_mode(hsotg)) {
2109b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Host Mode\n");
2110b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_A_HOST;
2111b02038faSJohn Youn 	} else {
2112b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Device Mode\n");
2113b02038faSJohn Youn 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2114b02038faSJohn Youn 	}
2115b02038faSJohn Youn 
2116b02038faSJohn Youn 	return 0;
2117b02038faSJohn Youn }
2118b02038faSJohn Youn 
2119b02038faSJohn Youn /**
2120b02038faSJohn Youn  * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2121b02038faSJohn Youn  * Host mode
2122b02038faSJohn Youn  *
2123b02038faSJohn Youn  * @hsotg: Programming view of DWC_otg controller
2124b02038faSJohn Youn  *
2125b02038faSJohn Youn  * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2126b02038faSJohn Youn  * request queues. Host channels are reset to ensure that they are ready for
2127b02038faSJohn Youn  * performing transfers.
2128b02038faSJohn Youn  */
2129b02038faSJohn Youn static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2130b02038faSJohn Youn {
213192a8dd26SMinas Harutyunyan 	u32 hcfg, hfir, otgctl, usbcfg;
2132b02038faSJohn Youn 
2133b02038faSJohn Youn 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2134b02038faSJohn Youn 
213592a8dd26SMinas Harutyunyan 	/* Set HS/FS Timeout Calibration to 7 (max available value).
213692a8dd26SMinas Harutyunyan 	 * The number of PHY clocks that the application programs in
213792a8dd26SMinas Harutyunyan 	 * this field is added to the high/full speed interpacket timeout
213892a8dd26SMinas Harutyunyan 	 * duration in the core to account for any additional delays
213992a8dd26SMinas Harutyunyan 	 * introduced by the PHY. This can be required, because the delay
214092a8dd26SMinas Harutyunyan 	 * introduced by the PHY in generating the linestate condition
214192a8dd26SMinas Harutyunyan 	 * can vary from one PHY to another.
214292a8dd26SMinas Harutyunyan 	 */
2143f25c42b8SGevorg Sahakyan 	usbcfg = dwc2_readl(hsotg, GUSBCFG);
214492a8dd26SMinas Harutyunyan 	usbcfg |= GUSBCFG_TOUTCAL(7);
2145f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, usbcfg, GUSBCFG);
214692a8dd26SMinas Harutyunyan 
2147b02038faSJohn Youn 	/* Restart the Phy Clock */
2148f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, PCGCTL);
2149b02038faSJohn Youn 
2150b02038faSJohn Youn 	/* Initialize Host Configuration Register */
2151b02038faSJohn Youn 	dwc2_init_fs_ls_pclk_sel(hsotg);
215238e9002bSVardan Mikayelyan 	if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
215338e9002bSVardan Mikayelyan 	    hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
2154f25c42b8SGevorg Sahakyan 		hcfg = dwc2_readl(hsotg, HCFG);
2155b02038faSJohn Youn 		hcfg |= HCFG_FSLSSUPP;
2156f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hcfg, HCFG);
2157b02038faSJohn Youn 	}
2158b02038faSJohn Youn 
2159b02038faSJohn Youn 	/*
2160b02038faSJohn Youn 	 * This bit allows dynamic reloading of the HFIR register during
2161b02038faSJohn Youn 	 * runtime. This bit needs to be programmed during initial configuration
2162b02038faSJohn Youn 	 * and its value must not be changed during runtime.
2163b02038faSJohn Youn 	 */
216495832c00SJohn Youn 	if (hsotg->params.reload_ctl) {
2165f25c42b8SGevorg Sahakyan 		hfir = dwc2_readl(hsotg, HFIR);
2166b02038faSJohn Youn 		hfir |= HFIR_RLDCTRL;
2167f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hfir, HFIR);
2168b02038faSJohn Youn 	}
2169b02038faSJohn Youn 
217095832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2171b02038faSJohn Youn 		u32 op_mode = hsotg->hw_params.op_mode;
2172b02038faSJohn Youn 
2173b02038faSJohn Youn 		if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2174b02038faSJohn Youn 		    !hsotg->hw_params.dma_desc_enable ||
2175b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2176b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2177b02038faSJohn Youn 		    op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2178b02038faSJohn Youn 			dev_err(hsotg->dev,
2179b02038faSJohn Youn 				"Hardware does not support descriptor DMA mode -\n");
2180b02038faSJohn Youn 			dev_err(hsotg->dev,
2181b02038faSJohn Youn 				"falling back to buffer DMA mode.\n");
218295832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
2183b02038faSJohn Youn 		} else {
2184f25c42b8SGevorg Sahakyan 			hcfg = dwc2_readl(hsotg, HCFG);
2185b02038faSJohn Youn 			hcfg |= HCFG_DESCDMA;
2186f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hcfg, HCFG);
2187b02038faSJohn Youn 		}
2188b02038faSJohn Youn 	}
2189b02038faSJohn Youn 
2190b02038faSJohn Youn 	/* Configure data FIFO sizes */
2191b02038faSJohn Youn 	dwc2_config_fifos(hsotg);
2192b02038faSJohn Youn 
2193b02038faSJohn Youn 	/* TODO - check this */
2194b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2195f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2196b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2197f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2198b02038faSJohn Youn 
2199b02038faSJohn Youn 	/* Make sure the FIFOs are flushed */
2200b02038faSJohn Youn 	dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2201b02038faSJohn Youn 	dwc2_flush_rx_fifo(hsotg);
2202b02038faSJohn Youn 
2203b02038faSJohn Youn 	/* Clear Host Set HNP Enable in the OTG Control Register */
2204f25c42b8SGevorg Sahakyan 	otgctl = dwc2_readl(hsotg, GOTGCTL);
2205b02038faSJohn Youn 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
2206f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, otgctl, GOTGCTL);
2207b02038faSJohn Youn 
220895832c00SJohn Youn 	if (!hsotg->params.dma_desc_enable) {
2209b02038faSJohn Youn 		int num_channels, i;
2210b02038faSJohn Youn 		u32 hcchar;
2211b02038faSJohn Youn 
2212b02038faSJohn Youn 		/* Flush out any leftover queued requests */
2213bea8e86cSJohn Youn 		num_channels = hsotg->params.host_channels;
2214b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2215f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22165799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2217b02038faSJohn Youn 				hcchar &= ~HCCHAR_CHENA;
2218b02038faSJohn Youn 				hcchar |= HCCHAR_CHDIS;
2219b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2220f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2221b02038faSJohn Youn 			}
22225799aecdSMinas Harutyunyan 		}
2223b02038faSJohn Youn 
2224b02038faSJohn Youn 		/* Halt all channels to put them into a known state */
2225b02038faSJohn Youn 		for (i = 0; i < num_channels; i++) {
2226f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
22275799aecdSMinas Harutyunyan 			if (hcchar & HCCHAR_CHENA) {
2228b02038faSJohn Youn 				hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2229b02038faSJohn Youn 				hcchar &= ~HCCHAR_EPDIR;
2230f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcchar, HCCHAR(i));
2231b02038faSJohn Youn 				dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2232b02038faSJohn Youn 					__func__, i);
223379d6b8c5SSevak Arakelyan 
223479d6b8c5SSevak Arakelyan 				if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
22355799aecdSMinas Harutyunyan 							      HCCHAR_CHENA,
22365799aecdSMinas Harutyunyan 							      1000)) {
22375799aecdSMinas Harutyunyan 					dev_warn(hsotg->dev,
22385799aecdSMinas Harutyunyan 						 "Unable to clear enable on channel %d\n",
2239b02038faSJohn Youn 						 i);
2240b02038faSJohn Youn 				}
2241b02038faSJohn Youn 			}
2242b02038faSJohn Youn 		}
22435799aecdSMinas Harutyunyan 	}
2244b02038faSJohn Youn 
224566e77a24SRazmik Karapetyan 	/* Enable ACG feature in host mode, if supported */
224666e77a24SRazmik Karapetyan 	dwc2_enable_acg(hsotg);
224766e77a24SRazmik Karapetyan 
2248b02038faSJohn Youn 	/* Turn on the vbus power */
2249b02038faSJohn Youn 	dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2250b02038faSJohn Youn 	if (hsotg->op_state == OTG_STATE_A_HOST) {
2251b02038faSJohn Youn 		u32 hprt0 = dwc2_read_hprt0(hsotg);
2252b02038faSJohn Youn 
2253b02038faSJohn Youn 		dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2254b02038faSJohn Youn 			!!(hprt0 & HPRT0_PWR));
2255b02038faSJohn Youn 		if (!(hprt0 & HPRT0_PWR)) {
2256b02038faSJohn Youn 			hprt0 |= HPRT0_PWR;
2257f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
2258b02038faSJohn Youn 		}
2259b02038faSJohn Youn 	}
2260b02038faSJohn Youn 
2261b02038faSJohn Youn 	dwc2_enable_host_interrupts(hsotg);
2262b02038faSJohn Youn }
2263b02038faSJohn Youn 
2264197ba5f4SPaul Zimmerman /*
2265197ba5f4SPaul Zimmerman  * Initializes dynamic portions of the DWC_otg HCD state
2266197ba5f4SPaul Zimmerman  *
2267197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2268197ba5f4SPaul Zimmerman  */
2269197ba5f4SPaul Zimmerman static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2270197ba5f4SPaul Zimmerman {
2271197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan, *chan_tmp;
2272197ba5f4SPaul Zimmerman 	int num_channels;
2273197ba5f4SPaul Zimmerman 	int i;
2274197ba5f4SPaul Zimmerman 
2275197ba5f4SPaul Zimmerman 	hsotg->flags.d32 = 0;
2276197ba5f4SPaul Zimmerman 	hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
2277197ba5f4SPaul Zimmerman 
227895832c00SJohn Youn 	if (hsotg->params.uframe_sched) {
2279197ba5f4SPaul Zimmerman 		hsotg->available_host_channels =
2280bea8e86cSJohn Youn 			hsotg->params.host_channels;
2281197ba5f4SPaul Zimmerman 	} else {
2282197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels = 0;
2283197ba5f4SPaul Zimmerman 		hsotg->periodic_channels = 0;
2284197ba5f4SPaul Zimmerman 	}
2285197ba5f4SPaul Zimmerman 
2286197ba5f4SPaul Zimmerman 	/*
2287197ba5f4SPaul Zimmerman 	 * Put all channels in the free channel list and clean up channel
2288197ba5f4SPaul Zimmerman 	 * states
2289197ba5f4SPaul Zimmerman 	 */
2290197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2291197ba5f4SPaul Zimmerman 				 hc_list_entry)
2292197ba5f4SPaul Zimmerman 		list_del_init(&chan->hc_list_entry);
2293197ba5f4SPaul Zimmerman 
2294bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2295197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
2296197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
2297197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2298197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
2299197ba5f4SPaul Zimmerman 	}
2300197ba5f4SPaul Zimmerman 
2301197ba5f4SPaul Zimmerman 	/* Initialize the DWC core for host mode operation */
2302197ba5f4SPaul Zimmerman 	dwc2_core_host_init(hsotg);
2303197ba5f4SPaul Zimmerman }
2304197ba5f4SPaul Zimmerman 
2305197ba5f4SPaul Zimmerman static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2306197ba5f4SPaul Zimmerman 			       struct dwc2_host_chan *chan,
2307197ba5f4SPaul Zimmerman 			       struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2308197ba5f4SPaul Zimmerman {
2309197ba5f4SPaul Zimmerman 	int hub_addr, hub_port;
2310197ba5f4SPaul Zimmerman 
2311197ba5f4SPaul Zimmerman 	chan->do_split = 1;
2312197ba5f4SPaul Zimmerman 	chan->xact_pos = qtd->isoc_split_pos;
2313197ba5f4SPaul Zimmerman 	chan->complete_split = qtd->complete_split;
2314197ba5f4SPaul Zimmerman 	dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2315197ba5f4SPaul Zimmerman 	chan->hub_addr = (u8)hub_addr;
2316197ba5f4SPaul Zimmerman 	chan->hub_port = (u8)hub_port;
2317197ba5f4SPaul Zimmerman }
2318197ba5f4SPaul Zimmerman 
23193bc04e28SDouglas Anderson static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2320197ba5f4SPaul Zimmerman 			      struct dwc2_host_chan *chan,
23213bc04e28SDouglas Anderson 			      struct dwc2_qtd *qtd)
2322197ba5f4SPaul Zimmerman {
2323197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
2324197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
2325197ba5f4SPaul Zimmerman 
2326197ba5f4SPaul Zimmerman 	switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2327197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
2328197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2329197ba5f4SPaul Zimmerman 
2330197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
2331197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
2332197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control setup transaction\n");
2333197ba5f4SPaul Zimmerman 			chan->do_ping = 0;
2334197ba5f4SPaul Zimmerman 			chan->ep_is_in = 0;
2335197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_SETUP;
233695832c00SJohn Youn 			if (hsotg->params.host_dma)
2337197ba5f4SPaul Zimmerman 				chan->xfer_dma = urb->setup_dma;
2338197ba5f4SPaul Zimmerman 			else
2339197ba5f4SPaul Zimmerman 				chan->xfer_buf = urb->setup_packet;
2340197ba5f4SPaul Zimmerman 			chan->xfer_len = 8;
2341197ba5f4SPaul Zimmerman 			break;
2342197ba5f4SPaul Zimmerman 
2343197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
2344197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control data transaction\n");
2345197ba5f4SPaul Zimmerman 			chan->data_pid_start = qtd->data_toggle;
2346197ba5f4SPaul Zimmerman 			break;
2347197ba5f4SPaul Zimmerman 
2348197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_STATUS:
2349197ba5f4SPaul Zimmerman 			/*
2350197ba5f4SPaul Zimmerman 			 * Direction is opposite of data direction or IN if no
2351197ba5f4SPaul Zimmerman 			 * data
2352197ba5f4SPaul Zimmerman 			 */
2353197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  Control status transaction\n");
2354197ba5f4SPaul Zimmerman 			if (urb->length == 0)
2355197ba5f4SPaul Zimmerman 				chan->ep_is_in = 1;
2356197ba5f4SPaul Zimmerman 			else
2357197ba5f4SPaul Zimmerman 				chan->ep_is_in =
2358197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_out(&urb->pipe_info);
2359197ba5f4SPaul Zimmerman 			if (chan->ep_is_in)
2360197ba5f4SPaul Zimmerman 				chan->do_ping = 0;
2361197ba5f4SPaul Zimmerman 			chan->data_pid_start = DWC2_HC_PID_DATA1;
2362197ba5f4SPaul Zimmerman 			chan->xfer_len = 0;
236395832c00SJohn Youn 			if (hsotg->params.host_dma)
2364197ba5f4SPaul Zimmerman 				chan->xfer_dma = hsotg->status_buf_dma;
2365197ba5f4SPaul Zimmerman 			else
2366197ba5f4SPaul Zimmerman 				chan->xfer_buf = hsotg->status_buf;
2367197ba5f4SPaul Zimmerman 			break;
2368197ba5f4SPaul Zimmerman 		}
2369197ba5f4SPaul Zimmerman 		break;
2370197ba5f4SPaul Zimmerman 
2371197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
2372197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_BULK;
2373197ba5f4SPaul Zimmerman 		break;
2374197ba5f4SPaul Zimmerman 
2375197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
2376197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_INT;
2377197ba5f4SPaul Zimmerman 		break;
2378197ba5f4SPaul Zimmerman 
2379197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
2380197ba5f4SPaul Zimmerman 		chan->ep_type = USB_ENDPOINT_XFER_ISOC;
238195832c00SJohn Youn 		if (hsotg->params.dma_desc_enable)
2382197ba5f4SPaul Zimmerman 			break;
2383197ba5f4SPaul Zimmerman 
2384197ba5f4SPaul Zimmerman 		frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2385197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
2386197ba5f4SPaul Zimmerman 
238795832c00SJohn Youn 		if (hsotg->params.host_dma) {
2388197ba5f4SPaul Zimmerman 			chan->xfer_dma = urb->dma;
2389197ba5f4SPaul Zimmerman 			chan->xfer_dma += frame_desc->offset +
2390197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2391197ba5f4SPaul Zimmerman 		} else {
2392197ba5f4SPaul Zimmerman 			chan->xfer_buf = urb->buf;
2393197ba5f4SPaul Zimmerman 			chan->xfer_buf += frame_desc->offset +
2394197ba5f4SPaul Zimmerman 					qtd->isoc_split_offset;
2395197ba5f4SPaul Zimmerman 		}
2396197ba5f4SPaul Zimmerman 
2397197ba5f4SPaul Zimmerman 		chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2398197ba5f4SPaul Zimmerman 
2399197ba5f4SPaul Zimmerman 		if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2400197ba5f4SPaul Zimmerman 			if (chan->xfer_len <= 188)
2401197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2402197ba5f4SPaul Zimmerman 			else
2403197ba5f4SPaul Zimmerman 				chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2404197ba5f4SPaul Zimmerman 		}
2405197ba5f4SPaul Zimmerman 		break;
2406197ba5f4SPaul Zimmerman 	}
2407197ba5f4SPaul Zimmerman }
2408197ba5f4SPaul Zimmerman 
2409af424a41SWilliam Wu static int dwc2_alloc_split_dma_aligned_buf(struct dwc2_hsotg *hsotg,
2410af424a41SWilliam Wu 					    struct dwc2_qh *qh,
2411af424a41SWilliam Wu 					    struct dwc2_host_chan *chan)
2412af424a41SWilliam Wu {
2413af424a41SWilliam Wu 	if (!hsotg->unaligned_cache ||
2414af424a41SWilliam Wu 	    chan->max_packet > DWC2_KMEM_UNALIGNED_BUF_SIZE)
2415af424a41SWilliam Wu 		return -ENOMEM;
2416af424a41SWilliam Wu 
2417af424a41SWilliam Wu 	if (!qh->dw_align_buf) {
2418af424a41SWilliam Wu 		qh->dw_align_buf = kmem_cache_alloc(hsotg->unaligned_cache,
2419af424a41SWilliam Wu 						    GFP_ATOMIC | GFP_DMA);
2420af424a41SWilliam Wu 		if (!qh->dw_align_buf)
2421af424a41SWilliam Wu 			return -ENOMEM;
2422af424a41SWilliam Wu 	}
2423af424a41SWilliam Wu 
2424af424a41SWilliam Wu 	qh->dw_align_buf_dma = dma_map_single(hsotg->dev, qh->dw_align_buf,
2425af424a41SWilliam Wu 					      DWC2_KMEM_UNALIGNED_BUF_SIZE,
2426af424a41SWilliam Wu 					      DMA_FROM_DEVICE);
2427af424a41SWilliam Wu 
2428af424a41SWilliam Wu 	if (dma_mapping_error(hsotg->dev, qh->dw_align_buf_dma)) {
2429af424a41SWilliam Wu 		dev_err(hsotg->dev, "can't map align_buf\n");
2430af424a41SWilliam Wu 		chan->align_buf = 0;
2431af424a41SWilliam Wu 		return -EINVAL;
2432af424a41SWilliam Wu 	}
2433af424a41SWilliam Wu 
2434af424a41SWilliam Wu 	chan->align_buf = qh->dw_align_buf_dma;
2435af424a41SWilliam Wu 	return 0;
2436af424a41SWilliam Wu }
2437af424a41SWilliam Wu 
24383bc04e28SDouglas Anderson #define DWC2_USB_DMA_ALIGN 4
24393bc04e28SDouglas Anderson 
24403bc04e28SDouglas Anderson static void dwc2_free_dma_aligned_buffer(struct urb *urb)
2441197ba5f4SPaul Zimmerman {
244256406e01SAntti Seppälä 	void *stored_xfer_buffer;
24431e111e88SAntti Seppälä 	size_t length;
2444197ba5f4SPaul Zimmerman 
24453bc04e28SDouglas Anderson 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
24463bc04e28SDouglas Anderson 		return;
2447197ba5f4SPaul Zimmerman 
244856406e01SAntti Seppälä 	/* Restore urb->transfer_buffer from the end of the allocated area */
24494a4863bfSMartin Schiller 	memcpy(&stored_xfer_buffer,
24504a4863bfSMartin Schiller 	       PTR_ALIGN(urb->transfer_buffer + urb->transfer_buffer_length,
24514a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
24524a4863bfSMartin Schiller 	       sizeof(urb->transfer_buffer));
24533bc04e28SDouglas Anderson 
24541e111e88SAntti Seppälä 	if (usb_urb_dir_in(urb)) {
24551e111e88SAntti Seppälä 		if (usb_pipeisoc(urb->pipe))
24561e111e88SAntti Seppälä 			length = urb->transfer_buffer_length;
24571e111e88SAntti Seppälä 		else
24581e111e88SAntti Seppälä 			length = urb->actual_length;
24591e111e88SAntti Seppälä 
24601e111e88SAntti Seppälä 		memcpy(stored_xfer_buffer, urb->transfer_buffer, length);
24611e111e88SAntti Seppälä 	}
246256406e01SAntti Seppälä 	kfree(urb->transfer_buffer);
246356406e01SAntti Seppälä 	urb->transfer_buffer = stored_xfer_buffer;
24643bc04e28SDouglas Anderson 
24653bc04e28SDouglas Anderson 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2466197ba5f4SPaul Zimmerman }
2467197ba5f4SPaul Zimmerman 
24683bc04e28SDouglas Anderson static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
24693bc04e28SDouglas Anderson {
247056406e01SAntti Seppälä 	void *kmalloc_ptr;
24713bc04e28SDouglas Anderson 	size_t kmalloc_size;
24725dce9555SPaul Zimmerman 
24733bc04e28SDouglas Anderson 	if (urb->num_sgs || urb->sg ||
24743bc04e28SDouglas Anderson 	    urb->transfer_buffer_length == 0 ||
24753bc04e28SDouglas Anderson 	    !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2476197ba5f4SPaul Zimmerman 		return 0;
24773bc04e28SDouglas Anderson 
247856406e01SAntti Seppälä 	/*
247956406e01SAntti Seppälä 	 * Allocate a buffer with enough padding for original transfer_buffer
248056406e01SAntti Seppälä 	 * pointer. This allocation is guaranteed to be aligned properly for
248156406e01SAntti Seppälä 	 * DMA
248256406e01SAntti Seppälä 	 */
24833bc04e28SDouglas Anderson 	kmalloc_size = urb->transfer_buffer_length +
24844a4863bfSMartin Schiller 		(dma_get_cache_alignment() - 1) +
248556406e01SAntti Seppälä 		sizeof(urb->transfer_buffer);
24863bc04e28SDouglas Anderson 
24873bc04e28SDouglas Anderson 	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
24883bc04e28SDouglas Anderson 	if (!kmalloc_ptr)
24893bc04e28SDouglas Anderson 		return -ENOMEM;
24903bc04e28SDouglas Anderson 
249156406e01SAntti Seppälä 	/*
249256406e01SAntti Seppälä 	 * Position value of original urb->transfer_buffer pointer to the end
249356406e01SAntti Seppälä 	 * of allocation for later referencing
249456406e01SAntti Seppälä 	 */
24954a4863bfSMartin Schiller 	memcpy(PTR_ALIGN(kmalloc_ptr + urb->transfer_buffer_length,
24964a4863bfSMartin Schiller 			 dma_get_cache_alignment()),
249756406e01SAntti Seppälä 	       &urb->transfer_buffer, sizeof(urb->transfer_buffer));
249856406e01SAntti Seppälä 
24993bc04e28SDouglas Anderson 	if (usb_urb_dir_out(urb))
250056406e01SAntti Seppälä 		memcpy(kmalloc_ptr, urb->transfer_buffer,
25013bc04e28SDouglas Anderson 		       urb->transfer_buffer_length);
250256406e01SAntti Seppälä 	urb->transfer_buffer = kmalloc_ptr;
25033bc04e28SDouglas Anderson 
25043bc04e28SDouglas Anderson 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
25053bc04e28SDouglas Anderson 
25063bc04e28SDouglas Anderson 	return 0;
25073bc04e28SDouglas Anderson }
25083bc04e28SDouglas Anderson 
25093bc04e28SDouglas Anderson static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
25103bc04e28SDouglas Anderson 				gfp_t mem_flags)
25113bc04e28SDouglas Anderson {
25123bc04e28SDouglas Anderson 	int ret;
25133bc04e28SDouglas Anderson 
25143bc04e28SDouglas Anderson 	/* We assume setup_dma is always aligned; warn if not */
25153bc04e28SDouglas Anderson 	WARN_ON_ONCE(urb->setup_dma &&
25163bc04e28SDouglas Anderson 		     (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
25173bc04e28SDouglas Anderson 
25183bc04e28SDouglas Anderson 	ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
25193bc04e28SDouglas Anderson 	if (ret)
25203bc04e28SDouglas Anderson 		return ret;
25213bc04e28SDouglas Anderson 
25223bc04e28SDouglas Anderson 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
25233bc04e28SDouglas Anderson 	if (ret)
25243bc04e28SDouglas Anderson 		dwc2_free_dma_aligned_buffer(urb);
25253bc04e28SDouglas Anderson 
25263bc04e28SDouglas Anderson 	return ret;
25273bc04e28SDouglas Anderson }
25283bc04e28SDouglas Anderson 
25293bc04e28SDouglas Anderson static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
25303bc04e28SDouglas Anderson {
25313bc04e28SDouglas Anderson 	usb_hcd_unmap_urb_for_dma(hcd, urb);
25323bc04e28SDouglas Anderson 	dwc2_free_dma_aligned_buffer(urb);
2533197ba5f4SPaul Zimmerman }
2534197ba5f4SPaul Zimmerman 
2535197ba5f4SPaul Zimmerman /**
2536197ba5f4SPaul Zimmerman  * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2537197ba5f4SPaul Zimmerman  * channel and initializes the host channel to perform the transactions. The
2538197ba5f4SPaul Zimmerman  * host channel is removed from the free list.
2539197ba5f4SPaul Zimmerman  *
2540197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2541197ba5f4SPaul Zimmerman  * @qh:    Transactions from the first QTD for this QH are selected and assigned
2542197ba5f4SPaul Zimmerman  *         to a free host channel
2543197ba5f4SPaul Zimmerman  */
2544197ba5f4SPaul Zimmerman static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
2545197ba5f4SPaul Zimmerman {
2546197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
2547197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
2548197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
2549197ba5f4SPaul Zimmerman 
2550197ba5f4SPaul Zimmerman 	if (dbg_qh(qh))
2551197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
2552197ba5f4SPaul Zimmerman 
2553197ba5f4SPaul Zimmerman 	if (list_empty(&qh->qtd_list)) {
2554197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No QTDs in QH list\n");
2555197ba5f4SPaul Zimmerman 		return -ENOMEM;
2556197ba5f4SPaul Zimmerman 	}
2557197ba5f4SPaul Zimmerman 
2558197ba5f4SPaul Zimmerman 	if (list_empty(&hsotg->free_hc_list)) {
2559197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "No free channel to assign\n");
2560197ba5f4SPaul Zimmerman 		return -ENOMEM;
2561197ba5f4SPaul Zimmerman 	}
2562197ba5f4SPaul Zimmerman 
2563197ba5f4SPaul Zimmerman 	chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2564197ba5f4SPaul Zimmerman 				hc_list_entry);
2565197ba5f4SPaul Zimmerman 
2566197ba5f4SPaul Zimmerman 	/* Remove host channel from free list */
2567197ba5f4SPaul Zimmerman 	list_del_init(&chan->hc_list_entry);
2568197ba5f4SPaul Zimmerman 
2569197ba5f4SPaul Zimmerman 	qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2570197ba5f4SPaul Zimmerman 	urb = qtd->urb;
2571197ba5f4SPaul Zimmerman 	qh->channel = chan;
2572197ba5f4SPaul Zimmerman 	qtd->in_process = 1;
2573197ba5f4SPaul Zimmerman 
2574197ba5f4SPaul Zimmerman 	/*
2575197ba5f4SPaul Zimmerman 	 * Use usb_pipedevice to determine device address. This address is
2576197ba5f4SPaul Zimmerman 	 * 0 before the SET_ADDRESS command and the correct address afterward.
2577197ba5f4SPaul Zimmerman 	 */
2578197ba5f4SPaul Zimmerman 	chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2579197ba5f4SPaul Zimmerman 	chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2580197ba5f4SPaul Zimmerman 	chan->speed = qh->dev_speed;
2581babd1839SDouglas Anderson 	chan->max_packet = qh->maxp;
2582197ba5f4SPaul Zimmerman 
2583197ba5f4SPaul Zimmerman 	chan->xfer_started = 0;
2584197ba5f4SPaul Zimmerman 	chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2585197ba5f4SPaul Zimmerman 	chan->error_state = (qtd->error_count > 0);
2586197ba5f4SPaul Zimmerman 	chan->halt_on_queue = 0;
2587197ba5f4SPaul Zimmerman 	chan->halt_pending = 0;
2588197ba5f4SPaul Zimmerman 	chan->requests = 0;
2589197ba5f4SPaul Zimmerman 
2590197ba5f4SPaul Zimmerman 	/*
2591197ba5f4SPaul Zimmerman 	 * The following values may be modified in the transfer type section
2592197ba5f4SPaul Zimmerman 	 * below. The xfer_len value may be reduced when the transfer is
2593197ba5f4SPaul Zimmerman 	 * started to accommodate the max widths of the XferSize and PktCnt
2594197ba5f4SPaul Zimmerman 	 * fields in the HCTSIZn register.
2595197ba5f4SPaul Zimmerman 	 */
2596197ba5f4SPaul Zimmerman 
2597197ba5f4SPaul Zimmerman 	chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2598197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
2599197ba5f4SPaul Zimmerman 		chan->do_ping = 0;
2600197ba5f4SPaul Zimmerman 	else
2601197ba5f4SPaul Zimmerman 		chan->do_ping = qh->ping_state;
2602197ba5f4SPaul Zimmerman 
2603197ba5f4SPaul Zimmerman 	chan->data_pid_start = qh->data_toggle;
2604197ba5f4SPaul Zimmerman 	chan->multi_count = 1;
2605197ba5f4SPaul Zimmerman 
2606197ba5f4SPaul Zimmerman 	if (urb->actual_length > urb->length &&
2607197ba5f4SPaul Zimmerman 	    !dwc2_hcd_is_pipe_in(&urb->pipe_info))
2608197ba5f4SPaul Zimmerman 		urb->actual_length = urb->length;
2609197ba5f4SPaul Zimmerman 
261095832c00SJohn Youn 	if (hsotg->params.host_dma)
2611197ba5f4SPaul Zimmerman 		chan->xfer_dma = urb->dma + urb->actual_length;
26123bc04e28SDouglas Anderson 	else
2613197ba5f4SPaul Zimmerman 		chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
2614197ba5f4SPaul Zimmerman 
2615197ba5f4SPaul Zimmerman 	chan->xfer_len = urb->length - urb->actual_length;
2616197ba5f4SPaul Zimmerman 	chan->xfer_count = 0;
2617197ba5f4SPaul Zimmerman 
2618197ba5f4SPaul Zimmerman 	/* Set the split attributes if required */
2619197ba5f4SPaul Zimmerman 	if (qh->do_split)
2620197ba5f4SPaul Zimmerman 		dwc2_hc_init_split(hsotg, chan, qtd, urb);
2621197ba5f4SPaul Zimmerman 	else
2622197ba5f4SPaul Zimmerman 		chan->do_split = 0;
2623197ba5f4SPaul Zimmerman 
2624197ba5f4SPaul Zimmerman 	/* Set the transfer attributes */
26253bc04e28SDouglas Anderson 	dwc2_hc_init_xfer(hsotg, chan, qtd);
2626197ba5f4SPaul Zimmerman 
2627af424a41SWilliam Wu 	/* For non-dword aligned buffers */
2628af424a41SWilliam Wu 	if (hsotg->params.host_dma && qh->do_split &&
2629af424a41SWilliam Wu 	    chan->ep_is_in && (chan->xfer_dma & 0x3)) {
2630af424a41SWilliam Wu 		dev_vdbg(hsotg->dev, "Non-aligned buffer\n");
2631af424a41SWilliam Wu 		if (dwc2_alloc_split_dma_aligned_buf(hsotg, qh, chan)) {
2632af424a41SWilliam Wu 			dev_err(hsotg->dev,
2633af424a41SWilliam Wu 				"Failed to allocate memory to handle non-aligned buffer\n");
2634af424a41SWilliam Wu 			/* Add channel back to free list */
2635af424a41SWilliam Wu 			chan->align_buf = 0;
2636af424a41SWilliam Wu 			chan->multi_count = 0;
2637af424a41SWilliam Wu 			list_add_tail(&chan->hc_list_entry,
2638af424a41SWilliam Wu 				      &hsotg->free_hc_list);
2639af424a41SWilliam Wu 			qtd->in_process = 0;
2640af424a41SWilliam Wu 			qh->channel = NULL;
2641af424a41SWilliam Wu 			return -ENOMEM;
2642af424a41SWilliam Wu 		}
2643af424a41SWilliam Wu 	} else {
2644af424a41SWilliam Wu 		/*
2645af424a41SWilliam Wu 		 * We assume that DMA is always aligned in non-split
2646af424a41SWilliam Wu 		 * case or split out case. Warn if not.
2647af424a41SWilliam Wu 		 */
2648af424a41SWilliam Wu 		WARN_ON_ONCE(hsotg->params.host_dma &&
2649af424a41SWilliam Wu 			     (chan->xfer_dma & 0x3));
2650af424a41SWilliam Wu 		chan->align_buf = 0;
2651af424a41SWilliam Wu 	}
2652af424a41SWilliam Wu 
2653197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2654197ba5f4SPaul Zimmerman 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2655197ba5f4SPaul Zimmerman 		/*
2656197ba5f4SPaul Zimmerman 		 * This value may be modified when the transfer is started
2657197ba5f4SPaul Zimmerman 		 * to reflect the actual transfer length
2658197ba5f4SPaul Zimmerman 		 */
2659babd1839SDouglas Anderson 		chan->multi_count = qh->maxp_mult;
2660197ba5f4SPaul Zimmerman 
266195832c00SJohn Youn 	if (hsotg->params.dma_desc_enable) {
2662197ba5f4SPaul Zimmerman 		chan->desc_list_addr = qh->desc_list_dma;
266395105a99SGregory Herrero 		chan->desc_list_sz = qh->desc_list_sz;
266495105a99SGregory Herrero 	}
2665197ba5f4SPaul Zimmerman 
2666197ba5f4SPaul Zimmerman 	dwc2_hc_init(hsotg, chan);
2667197ba5f4SPaul Zimmerman 	chan->qh = qh;
2668197ba5f4SPaul Zimmerman 
2669197ba5f4SPaul Zimmerman 	return 0;
2670197ba5f4SPaul Zimmerman }
2671197ba5f4SPaul Zimmerman 
2672197ba5f4SPaul Zimmerman /**
2673197ba5f4SPaul Zimmerman  * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2674197ba5f4SPaul Zimmerman  * schedule and assigns them to available host channels. Called from the HCD
2675197ba5f4SPaul Zimmerman  * interrupt handler functions.
2676197ba5f4SPaul Zimmerman  *
2677197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2678197ba5f4SPaul Zimmerman  *
2679197ba5f4SPaul Zimmerman  * Return: The types of new transactions that were assigned to host channels
2680197ba5f4SPaul Zimmerman  */
2681197ba5f4SPaul Zimmerman enum dwc2_transaction_type dwc2_hcd_select_transactions(
2682197ba5f4SPaul Zimmerman 		struct dwc2_hsotg *hsotg)
2683197ba5f4SPaul Zimmerman {
2684197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2685197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2686197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2687197ba5f4SPaul Zimmerman 	int num_channels;
2688197ba5f4SPaul Zimmerman 
2689197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
2690197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Select Transactions\n");
2691197ba5f4SPaul Zimmerman #endif
2692197ba5f4SPaul Zimmerman 
2693197ba5f4SPaul Zimmerman 	/* Process entries in the periodic ready list */
2694197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_ready.next;
2695197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_ready) {
2696197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2697197ba5f4SPaul Zimmerman 			break;
269895832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2699197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels <= 1)
2700197ba5f4SPaul Zimmerman 				break;
2701197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2702197ba5f4SPaul Zimmerman 		}
2703197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2704b258e426SMinas Harutyunyan 		if (dwc2_assign_and_init_hc(hsotg, qh)) {
2705b258e426SMinas Harutyunyan 			if (hsotg->params.uframe_sched)
2706b258e426SMinas Harutyunyan 				hsotg->available_host_channels++;
2707197ba5f4SPaul Zimmerman 			break;
2708b258e426SMinas Harutyunyan 		}
2709197ba5f4SPaul Zimmerman 
2710197ba5f4SPaul Zimmerman 		/*
2711197ba5f4SPaul Zimmerman 		 * Move the QH from the periodic ready schedule to the
2712197ba5f4SPaul Zimmerman 		 * periodic assigned schedule
2713197ba5f4SPaul Zimmerman 		 */
2714197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
271594ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
271694ef7aeeSDouglas Anderson 			       &hsotg->periodic_sched_assigned);
2717197ba5f4SPaul Zimmerman 		ret_val = DWC2_TRANSACTION_PERIODIC;
2718197ba5f4SPaul Zimmerman 	}
2719197ba5f4SPaul Zimmerman 
2720197ba5f4SPaul Zimmerman 	/*
2721197ba5f4SPaul Zimmerman 	 * Process entries in the inactive portion of the non-periodic
2722197ba5f4SPaul Zimmerman 	 * schedule. Some free host channels may not be used if they are
2723197ba5f4SPaul Zimmerman 	 * reserved for periodic transfers.
2724197ba5f4SPaul Zimmerman 	 */
2725bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2726197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->non_periodic_sched_inactive.next;
2727197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
272895832c00SJohn Youn 		if (!hsotg->params.uframe_sched &&
2729197ba5f4SPaul Zimmerman 		    hsotg->non_periodic_channels >= num_channels -
2730197ba5f4SPaul Zimmerman 						hsotg->periodic_channels)
2731197ba5f4SPaul Zimmerman 			break;
2732197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2733197ba5f4SPaul Zimmerman 			break;
2734197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
273595832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2736197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels < 1)
2737197ba5f4SPaul Zimmerman 				break;
2738197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2739197ba5f4SPaul Zimmerman 		}
2740197ba5f4SPaul Zimmerman 
2741b258e426SMinas Harutyunyan 		if (dwc2_assign_and_init_hc(hsotg, qh)) {
2742b258e426SMinas Harutyunyan 			if (hsotg->params.uframe_sched)
2743b258e426SMinas Harutyunyan 				hsotg->available_host_channels++;
2744197ba5f4SPaul Zimmerman 			break;
2745b258e426SMinas Harutyunyan 		}
2746197ba5f4SPaul Zimmerman 
2747197ba5f4SPaul Zimmerman 		/*
2748197ba5f4SPaul Zimmerman 		 * Move the QH from the non-periodic inactive schedule to the
2749197ba5f4SPaul Zimmerman 		 * non-periodic active schedule
2750197ba5f4SPaul Zimmerman 		 */
2751197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
275294ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
2753197ba5f4SPaul Zimmerman 			       &hsotg->non_periodic_sched_active);
2754197ba5f4SPaul Zimmerman 
2755197ba5f4SPaul Zimmerman 		if (ret_val == DWC2_TRANSACTION_NONE)
2756197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2757197ba5f4SPaul Zimmerman 		else
2758197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_ALL;
2759197ba5f4SPaul Zimmerman 
276095832c00SJohn Youn 		if (!hsotg->params.uframe_sched)
2761197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels++;
2762197ba5f4SPaul Zimmerman 	}
2763197ba5f4SPaul Zimmerman 
2764197ba5f4SPaul Zimmerman 	return ret_val;
2765197ba5f4SPaul Zimmerman }
2766197ba5f4SPaul Zimmerman 
2767197ba5f4SPaul Zimmerman /**
2768197ba5f4SPaul Zimmerman  * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2769197ba5f4SPaul Zimmerman  * a host channel associated with either a periodic or non-periodic transfer
2770197ba5f4SPaul Zimmerman  *
2771197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2772197ba5f4SPaul Zimmerman  * @chan:  Host channel descriptor associated with either a periodic or
2773197ba5f4SPaul Zimmerman  *         non-periodic transfer
2774197ba5f4SPaul Zimmerman  * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2775197ba5f4SPaul Zimmerman  *                     for periodic transfers or the non-periodic Tx FIFO
2776197ba5f4SPaul Zimmerman  *                     for non-periodic transfers
2777197ba5f4SPaul Zimmerman  *
2778197ba5f4SPaul Zimmerman  * Return: 1 if a request is queued and more requests may be needed to
2779197ba5f4SPaul Zimmerman  * complete the transfer, 0 if no more requests are required for this
2780197ba5f4SPaul Zimmerman  * transfer, -1 if there is insufficient space in the Tx FIFO
2781197ba5f4SPaul Zimmerman  *
2782197ba5f4SPaul Zimmerman  * This function assumes that there is space available in the appropriate
2783197ba5f4SPaul Zimmerman  * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2784197ba5f4SPaul Zimmerman  * it checks whether space is available in the appropriate Tx FIFO.
2785197ba5f4SPaul Zimmerman  *
2786197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2787197ba5f4SPaul Zimmerman  */
2788197ba5f4SPaul Zimmerman static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2789197ba5f4SPaul Zimmerman 				  struct dwc2_host_chan *chan,
2790197ba5f4SPaul Zimmerman 				  u16 fifo_dwords_avail)
2791197ba5f4SPaul Zimmerman {
2792197ba5f4SPaul Zimmerman 	int retval = 0;
2793197ba5f4SPaul Zimmerman 
2794c9c8ac01SDouglas Anderson 	if (chan->do_split)
2795c9c8ac01SDouglas Anderson 		/* Put ourselves on the list to keep order straight */
2796c9c8ac01SDouglas Anderson 		list_move_tail(&chan->split_order_list_entry,
2797c9c8ac01SDouglas Anderson 			       &hsotg->split_order);
2798c9c8ac01SDouglas Anderson 
27997b813767SAlexandru M Stan 	if (hsotg->params.host_dma && chan->qh) {
280095832c00SJohn Youn 		if (hsotg->params.dma_desc_enable) {
2801197ba5f4SPaul Zimmerman 			if (!chan->xfer_started ||
2802197ba5f4SPaul Zimmerman 			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2803197ba5f4SPaul Zimmerman 				dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2804197ba5f4SPaul Zimmerman 				chan->qh->ping_state = 0;
2805197ba5f4SPaul Zimmerman 			}
2806197ba5f4SPaul Zimmerman 		} else if (!chan->xfer_started) {
2807197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2808197ba5f4SPaul Zimmerman 			chan->qh->ping_state = 0;
2809197ba5f4SPaul Zimmerman 		}
2810197ba5f4SPaul Zimmerman 	} else if (chan->halt_pending) {
2811197ba5f4SPaul Zimmerman 		/* Don't queue a request if the channel has been halted */
2812197ba5f4SPaul Zimmerman 	} else if (chan->halt_on_queue) {
2813197ba5f4SPaul Zimmerman 		dwc2_hc_halt(hsotg, chan, chan->halt_status);
2814197ba5f4SPaul Zimmerman 	} else if (chan->do_ping) {
2815197ba5f4SPaul Zimmerman 		if (!chan->xfer_started)
2816197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2817197ba5f4SPaul Zimmerman 	} else if (!chan->ep_is_in ||
2818197ba5f4SPaul Zimmerman 		   chan->data_pid_start == DWC2_HC_PID_SETUP) {
2819197ba5f4SPaul Zimmerman 		if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2820197ba5f4SPaul Zimmerman 			if (!chan->xfer_started) {
2821197ba5f4SPaul Zimmerman 				dwc2_hc_start_transfer(hsotg, chan);
2822197ba5f4SPaul Zimmerman 				retval = 1;
2823197ba5f4SPaul Zimmerman 			} else {
2824197ba5f4SPaul Zimmerman 				retval = dwc2_hc_continue_transfer(hsotg, chan);
2825197ba5f4SPaul Zimmerman 			}
2826197ba5f4SPaul Zimmerman 		} else {
2827197ba5f4SPaul Zimmerman 			retval = -1;
2828197ba5f4SPaul Zimmerman 		}
2829197ba5f4SPaul Zimmerman 	} else {
2830197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
2831197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2832197ba5f4SPaul Zimmerman 			retval = 1;
2833197ba5f4SPaul Zimmerman 		} else {
2834197ba5f4SPaul Zimmerman 			retval = dwc2_hc_continue_transfer(hsotg, chan);
2835197ba5f4SPaul Zimmerman 		}
2836197ba5f4SPaul Zimmerman 	}
2837197ba5f4SPaul Zimmerman 
2838197ba5f4SPaul Zimmerman 	return retval;
2839197ba5f4SPaul Zimmerman }
2840197ba5f4SPaul Zimmerman 
2841197ba5f4SPaul Zimmerman /*
2842197ba5f4SPaul Zimmerman  * Processes periodic channels for the next frame and queues transactions for
2843197ba5f4SPaul Zimmerman  * these channels to the DWC_otg controller. After queueing transactions, the
2844197ba5f4SPaul Zimmerman  * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2845197ba5f4SPaul Zimmerman  * to queue as Periodic Tx FIFO or request queue space becomes available.
2846197ba5f4SPaul Zimmerman  * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2847197ba5f4SPaul Zimmerman  *
2848197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2849197ba5f4SPaul Zimmerman  */
2850197ba5f4SPaul Zimmerman static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2851197ba5f4SPaul Zimmerman {
2852197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2853197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2854197ba5f4SPaul Zimmerman 	u32 tx_status;
2855197ba5f4SPaul Zimmerman 	u32 fspcavail;
2856197ba5f4SPaul Zimmerman 	u32 gintmsk;
2857197ba5f4SPaul Zimmerman 	int status;
28584e50e011SDouglas Anderson 	bool no_queue_space = false;
28594e50e011SDouglas Anderson 	bool no_fifo_space = false;
2860197ba5f4SPaul Zimmerman 	u32 qspcavail;
2861197ba5f4SPaul Zimmerman 
28624e50e011SDouglas Anderson 	/* If empty list then just adjust interrupt enables */
28634e50e011SDouglas Anderson 	if (list_empty(&hsotg->periodic_sched_assigned))
28644e50e011SDouglas Anderson 		goto exit;
28654e50e011SDouglas Anderson 
2866197ba5f4SPaul Zimmerman 	if (dbg_perio())
2867197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
2868197ba5f4SPaul Zimmerman 
2869f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, HPTXSTS);
2870197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2871197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
2872197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2873197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
2874197ba5f4SPaul Zimmerman 
2875197ba5f4SPaul Zimmerman 	if (dbg_perio()) {
2876197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx Req Queue Space Avail (before queue): %d\n",
2877197ba5f4SPaul Zimmerman 			 qspcavail);
2878197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx FIFO Space Avail (before queue): %d\n",
2879197ba5f4SPaul Zimmerman 			 fspcavail);
2880197ba5f4SPaul Zimmerman 	}
2881197ba5f4SPaul Zimmerman 
2882197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_assigned.next;
2883197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_assigned) {
2884f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, HPTXSTS);
2885197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2886197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
2887197ba5f4SPaul Zimmerman 		if (qspcavail == 0) {
2888fdb09b3eSNicholas Mc Guire 			no_queue_space = true;
2889197ba5f4SPaul Zimmerman 			break;
2890197ba5f4SPaul Zimmerman 		}
2891197ba5f4SPaul Zimmerman 
2892197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2893197ba5f4SPaul Zimmerman 		if (!qh->channel) {
2894197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2895197ba5f4SPaul Zimmerman 			continue;
2896197ba5f4SPaul Zimmerman 		}
2897197ba5f4SPaul Zimmerman 
2898197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
2899197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty) {
2900197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2901197ba5f4SPaul Zimmerman 			continue;
2902197ba5f4SPaul Zimmerman 		}
2903197ba5f4SPaul Zimmerman 
2904197ba5f4SPaul Zimmerman 		/*
2905197ba5f4SPaul Zimmerman 		 * Set a flag if we're queuing high-bandwidth in slave mode.
2906197ba5f4SPaul Zimmerman 		 * The flag prevents any halts to get into the request queue in
2907197ba5f4SPaul Zimmerman 		 * the middle of multiple high-bandwidth packets getting queued.
2908197ba5f4SPaul Zimmerman 		 */
290995832c00SJohn Youn 		if (!hsotg->params.host_dma &&
2910197ba5f4SPaul Zimmerman 		    qh->channel->multi_count > 1)
2911197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 1;
2912197ba5f4SPaul Zimmerman 
2913197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2914197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
2915197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
2916197ba5f4SPaul Zimmerman 		if (status < 0) {
2917fdb09b3eSNicholas Mc Guire 			no_fifo_space = true;
2918197ba5f4SPaul Zimmerman 			break;
2919197ba5f4SPaul Zimmerman 		}
2920197ba5f4SPaul Zimmerman 
2921197ba5f4SPaul Zimmerman 		/*
2922197ba5f4SPaul Zimmerman 		 * In Slave mode, stay on the current transfer until there is
2923197ba5f4SPaul Zimmerman 		 * nothing more to do or the high-bandwidth request count is
2924197ba5f4SPaul Zimmerman 		 * reached. In DMA mode, only need to queue one request. The
2925197ba5f4SPaul Zimmerman 		 * controller automatically handles multiple packets for
2926197ba5f4SPaul Zimmerman 		 * high-bandwidth transfers.
2927197ba5f4SPaul Zimmerman 		 */
292895832c00SJohn Youn 		if (hsotg->params.host_dma || status == 0 ||
2929197ba5f4SPaul Zimmerman 		    qh->channel->requests == qh->channel->multi_count) {
2930197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2931197ba5f4SPaul Zimmerman 			/*
2932197ba5f4SPaul Zimmerman 			 * Move the QH from the periodic assigned schedule to
2933197ba5f4SPaul Zimmerman 			 * the periodic queued schedule
2934197ba5f4SPaul Zimmerman 			 */
293594ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
2936197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_queued);
2937197ba5f4SPaul Zimmerman 
2938197ba5f4SPaul Zimmerman 			/* done queuing high bandwidth */
2939197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 0;
2940197ba5f4SPaul Zimmerman 		}
2941197ba5f4SPaul Zimmerman 	}
2942197ba5f4SPaul Zimmerman 
29434e50e011SDouglas Anderson exit:
29444e50e011SDouglas Anderson 	if (no_queue_space || no_fifo_space ||
294595832c00SJohn Youn 	    (!hsotg->params.host_dma &&
29464e50e011SDouglas Anderson 	     !list_empty(&hsotg->periodic_sched_assigned))) {
2947197ba5f4SPaul Zimmerman 		/*
2948197ba5f4SPaul Zimmerman 		 * May need to queue more transactions as the request
2949197ba5f4SPaul Zimmerman 		 * queue or Tx FIFO empties. Enable the periodic Tx
2950197ba5f4SPaul Zimmerman 		 * FIFO empty interrupt. (Always use the half-empty
2951197ba5f4SPaul Zimmerman 		 * level to ensure that new requests are loaded as
2952197ba5f4SPaul Zimmerman 		 * soon as possible.)
2953197ba5f4SPaul Zimmerman 		 */
2954f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29554e50e011SDouglas Anderson 		if (!(gintmsk & GINTSTS_PTXFEMP)) {
2956197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_PTXFEMP;
2957f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
29584e50e011SDouglas Anderson 		}
2959197ba5f4SPaul Zimmerman 	} else {
2960197ba5f4SPaul Zimmerman 		/*
2961197ba5f4SPaul Zimmerman 		 * Disable the Tx FIFO empty interrupt since there are
2962197ba5f4SPaul Zimmerman 		 * no more transactions that need to be queued right
2963197ba5f4SPaul Zimmerman 		 * now. This function is called from interrupt
2964197ba5f4SPaul Zimmerman 		 * handlers to queue more transactions as transfer
2965197ba5f4SPaul Zimmerman 		 * states change.
2966197ba5f4SPaul Zimmerman 		 */
2967f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29684e50e011SDouglas Anderson 		if (gintmsk & GINTSTS_PTXFEMP) {
2969197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_PTXFEMP;
2970f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
2971197ba5f4SPaul Zimmerman 		}
2972197ba5f4SPaul Zimmerman 	}
2973197ba5f4SPaul Zimmerman }
2974197ba5f4SPaul Zimmerman 
2975197ba5f4SPaul Zimmerman /*
2976197ba5f4SPaul Zimmerman  * Processes active non-periodic channels and queues transactions for these
2977197ba5f4SPaul Zimmerman  * channels to the DWC_otg controller. After queueing transactions, the NP Tx
2978197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is enabled if there are more transactions to queue as
2979197ba5f4SPaul Zimmerman  * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
2980197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is disabled.
2981197ba5f4SPaul Zimmerman  *
2982197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2983197ba5f4SPaul Zimmerman  */
2984197ba5f4SPaul Zimmerman static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
2985197ba5f4SPaul Zimmerman {
2986197ba5f4SPaul Zimmerman 	struct list_head *orig_qh_ptr;
2987197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2988197ba5f4SPaul Zimmerman 	u32 tx_status;
2989197ba5f4SPaul Zimmerman 	u32 qspcavail;
2990197ba5f4SPaul Zimmerman 	u32 fspcavail;
2991197ba5f4SPaul Zimmerman 	u32 gintmsk;
2992197ba5f4SPaul Zimmerman 	int status;
2993197ba5f4SPaul Zimmerman 	int no_queue_space = 0;
2994197ba5f4SPaul Zimmerman 	int no_fifo_space = 0;
2995197ba5f4SPaul Zimmerman 	int more_to_do = 0;
2996197ba5f4SPaul Zimmerman 
2997197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
2998197ba5f4SPaul Zimmerman 
2999f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, GNPTXSTS);
3000197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3001197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
3002197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3003197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
3004197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx Req Queue Space Avail (before queue): %d\n",
3005197ba5f4SPaul Zimmerman 		 qspcavail);
3006197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx FIFO Space Avail (before queue): %d\n",
3007197ba5f4SPaul Zimmerman 		 fspcavail);
3008197ba5f4SPaul Zimmerman 
3009197ba5f4SPaul Zimmerman 	/*
3010197ba5f4SPaul Zimmerman 	 * Keep track of the starting point. Skip over the start-of-list
3011197ba5f4SPaul Zimmerman 	 * entry.
3012197ba5f4SPaul Zimmerman 	 */
3013197ba5f4SPaul Zimmerman 	if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3014197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3015197ba5f4SPaul Zimmerman 	orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3016197ba5f4SPaul Zimmerman 
3017197ba5f4SPaul Zimmerman 	/*
3018197ba5f4SPaul Zimmerman 	 * Process once through the active list or until no more space is
3019197ba5f4SPaul Zimmerman 	 * available in the request queue or the Tx FIFO
3020197ba5f4SPaul Zimmerman 	 */
3021197ba5f4SPaul Zimmerman 	do {
3022f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3023197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3024197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
302595832c00SJohn Youn 		if (!hsotg->params.host_dma && qspcavail == 0) {
3026197ba5f4SPaul Zimmerman 			no_queue_space = 1;
3027197ba5f4SPaul Zimmerman 			break;
3028197ba5f4SPaul Zimmerman 		}
3029197ba5f4SPaul Zimmerman 
3030197ba5f4SPaul Zimmerman 		qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3031197ba5f4SPaul Zimmerman 				qh_list_entry);
3032197ba5f4SPaul Zimmerman 		if (!qh->channel)
3033197ba5f4SPaul Zimmerman 			goto next;
3034197ba5f4SPaul Zimmerman 
3035197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
3036197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty)
3037197ba5f4SPaul Zimmerman 			goto next;
3038197ba5f4SPaul Zimmerman 
3039197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3040197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3041197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3042197ba5f4SPaul Zimmerman 
3043197ba5f4SPaul Zimmerman 		if (status > 0) {
3044197ba5f4SPaul Zimmerman 			more_to_do = 1;
3045197ba5f4SPaul Zimmerman 		} else if (status < 0) {
3046197ba5f4SPaul Zimmerman 			no_fifo_space = 1;
3047197ba5f4SPaul Zimmerman 			break;
3048197ba5f4SPaul Zimmerman 		}
3049197ba5f4SPaul Zimmerman next:
3050197ba5f4SPaul Zimmerman 		/* Advance to next QH, skipping start-of-list entry */
3051197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3052197ba5f4SPaul Zimmerman 		if (hsotg->non_periodic_qh_ptr ==
3053197ba5f4SPaul Zimmerman 				&hsotg->non_periodic_sched_active)
3054197ba5f4SPaul Zimmerman 			hsotg->non_periodic_qh_ptr =
3055197ba5f4SPaul Zimmerman 					hsotg->non_periodic_qh_ptr->next;
3056197ba5f4SPaul Zimmerman 	} while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3057197ba5f4SPaul Zimmerman 
305895832c00SJohn Youn 	if (!hsotg->params.host_dma) {
3059f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3060197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3061197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
3062197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3063197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3064197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3065197ba5f4SPaul Zimmerman 			 "  NP Tx Req Queue Space Avail (after queue): %d\n",
3066197ba5f4SPaul Zimmerman 			 qspcavail);
3067197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3068197ba5f4SPaul Zimmerman 			 "  NP Tx FIFO Space Avail (after queue): %d\n",
3069197ba5f4SPaul Zimmerman 			 fspcavail);
3070197ba5f4SPaul Zimmerman 
3071197ba5f4SPaul Zimmerman 		if (more_to_do || no_queue_space || no_fifo_space) {
3072197ba5f4SPaul Zimmerman 			/*
3073197ba5f4SPaul Zimmerman 			 * May need to queue more transactions as the request
3074197ba5f4SPaul Zimmerman 			 * queue or Tx FIFO empties. Enable the non-periodic
3075197ba5f4SPaul Zimmerman 			 * Tx FIFO empty interrupt. (Always use the half-empty
3076197ba5f4SPaul Zimmerman 			 * level to ensure that new requests are loaded as
3077197ba5f4SPaul Zimmerman 			 * soon as possible.)
3078197ba5f4SPaul Zimmerman 			 */
3079f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3080197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_NPTXFEMP;
3081f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3082197ba5f4SPaul Zimmerman 		} else {
3083197ba5f4SPaul Zimmerman 			/*
3084197ba5f4SPaul Zimmerman 			 * Disable the Tx FIFO empty interrupt since there are
3085197ba5f4SPaul Zimmerman 			 * no more transactions that need to be queued right
3086197ba5f4SPaul Zimmerman 			 * now. This function is called from interrupt
3087197ba5f4SPaul Zimmerman 			 * handlers to queue more transactions as transfer
3088197ba5f4SPaul Zimmerman 			 * states change.
3089197ba5f4SPaul Zimmerman 			 */
3090f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3091197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3092f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3093197ba5f4SPaul Zimmerman 		}
3094197ba5f4SPaul Zimmerman 	}
3095197ba5f4SPaul Zimmerman }
3096197ba5f4SPaul Zimmerman 
3097197ba5f4SPaul Zimmerman /**
3098197ba5f4SPaul Zimmerman  * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3099197ba5f4SPaul Zimmerman  * and queues transactions for these channels to the DWC_otg controller. Called
3100197ba5f4SPaul Zimmerman  * from the HCD interrupt handler functions.
3101197ba5f4SPaul Zimmerman  *
3102197ba5f4SPaul Zimmerman  * @hsotg:   The HCD state structure
3103197ba5f4SPaul Zimmerman  * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3104197ba5f4SPaul Zimmerman  *           or both)
3105197ba5f4SPaul Zimmerman  *
3106197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3107197ba5f4SPaul Zimmerman  */
3108197ba5f4SPaul Zimmerman void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3109197ba5f4SPaul Zimmerman 				 enum dwc2_transaction_type tr_type)
3110197ba5f4SPaul Zimmerman {
3111197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3112197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue Transactions\n");
3113197ba5f4SPaul Zimmerman #endif
3114197ba5f4SPaul Zimmerman 	/* Process host channels associated with periodic transfers */
31154e50e011SDouglas Anderson 	if (tr_type == DWC2_TRANSACTION_PERIODIC ||
31164e50e011SDouglas Anderson 	    tr_type == DWC2_TRANSACTION_ALL)
3117197ba5f4SPaul Zimmerman 		dwc2_process_periodic_channels(hsotg);
3118197ba5f4SPaul Zimmerman 
3119197ba5f4SPaul Zimmerman 	/* Process host channels associated with non-periodic transfers */
3120197ba5f4SPaul Zimmerman 	if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3121197ba5f4SPaul Zimmerman 	    tr_type == DWC2_TRANSACTION_ALL) {
3122197ba5f4SPaul Zimmerman 		if (!list_empty(&hsotg->non_periodic_sched_active)) {
3123197ba5f4SPaul Zimmerman 			dwc2_process_non_periodic_channels(hsotg);
3124197ba5f4SPaul Zimmerman 		} else {
3125197ba5f4SPaul Zimmerman 			/*
3126197ba5f4SPaul Zimmerman 			 * Ensure NP Tx FIFO empty interrupt is disabled when
3127197ba5f4SPaul Zimmerman 			 * there are no non-periodic transfers to process
3128197ba5f4SPaul Zimmerman 			 */
3129f25c42b8SGevorg Sahakyan 			u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
3130197ba5f4SPaul Zimmerman 
3131197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3132f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3133197ba5f4SPaul Zimmerman 		}
3134197ba5f4SPaul Zimmerman 	}
3135197ba5f4SPaul Zimmerman }
3136197ba5f4SPaul Zimmerman 
3137197ba5f4SPaul Zimmerman static void dwc2_conn_id_status_change(struct work_struct *work)
3138197ba5f4SPaul Zimmerman {
3139197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3140197ba5f4SPaul Zimmerman 						wf_otg);
3141197ba5f4SPaul Zimmerman 	u32 count = 0;
3142197ba5f4SPaul Zimmerman 	u32 gotgctl;
31435390d438SMian Yousaf Kaukab 	unsigned long flags;
3144197ba5f4SPaul Zimmerman 
3145197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3146197ba5f4SPaul Zimmerman 
3147f25c42b8SGevorg Sahakyan 	gotgctl = dwc2_readl(hsotg, GOTGCTL);
3148197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3149197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3150197ba5f4SPaul Zimmerman 		!!(gotgctl & GOTGCTL_CONID_B));
3151197ba5f4SPaul Zimmerman 
3152197ba5f4SPaul Zimmerman 	/* B-Device connector (Device Mode) */
3153197ba5f4SPaul Zimmerman 	if (gotgctl & GOTGCTL_CONID_B) {
3154531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
3155197ba5f4SPaul Zimmerman 		/* Wait for switch to device mode */
3156197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId B\n");
31579156a7efSChen Yu 		if (hsotg->bus_suspended) {
31589156a7efSChen Yu 			dev_info(hsotg->dev,
31599156a7efSChen Yu 				 "Do port resume before switching to device mode\n");
31609156a7efSChen Yu 			dwc2_port_resume(hsotg);
31619156a7efSChen Yu 		}
3162197ba5f4SPaul Zimmerman 		while (!dwc2_is_device_mode(hsotg)) {
3163197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev,
3164197ba5f4SPaul Zimmerman 				 "Waiting for Peripheral Mode, Mode=%s\n",
3165197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ? "Host" :
3166197ba5f4SPaul Zimmerman 				 "Peripheral");
316704a9db79SNicholas Mc Guire 			msleep(20);
3168fc30c4bbSJohn Stultz 			/*
3169fc30c4bbSJohn Stultz 			 * Sometimes the initial GOTGCTRL read is wrong, so
3170fc30c4bbSJohn Stultz 			 * check it again and jump to host mode if that was
3171fc30c4bbSJohn Stultz 			 * the case.
3172fc30c4bbSJohn Stultz 			 */
3173f25c42b8SGevorg Sahakyan 			gotgctl = dwc2_readl(hsotg, GOTGCTL);
3174fc30c4bbSJohn Stultz 			if (!(gotgctl & GOTGCTL_CONID_B))
3175fc30c4bbSJohn Stultz 				goto host;
3176197ba5f4SPaul Zimmerman 			if (++count > 250)
3177197ba5f4SPaul Zimmerman 				break;
3178197ba5f4SPaul Zimmerman 		}
3179197ba5f4SPaul Zimmerman 		if (count > 250)
3180197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3181197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
31824d4d99afSArtur Petrosyan 
31834d4d99afSArtur Petrosyan 		/*
31844d4d99afSArtur Petrosyan 		 * Exit Partial Power Down without restoring registers.
31854d4d99afSArtur Petrosyan 		 * No need to check the return value as registers
31864d4d99afSArtur Petrosyan 		 * are not being restored.
31874d4d99afSArtur Petrosyan 		 */
31884d4d99afSArtur Petrosyan 		if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
31894d4d99afSArtur Petrosyan 			dwc2_exit_partial_power_down(hsotg, 0, false);
31904d4d99afSArtur Petrosyan 
3191197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
31920fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3193197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
31945390d438SMian Yousaf Kaukab 		spin_lock_irqsave(&hsotg->lock, flags);
31951f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
31965390d438SMian Yousaf Kaukab 		spin_unlock_irqrestore(&hsotg->lock, flags);
319766e77a24SRazmik Karapetyan 		/* Enable ACG feature in device mode,if supported */
319866e77a24SRazmik Karapetyan 		dwc2_enable_acg(hsotg);
31991f91b4ccSFelipe Balbi 		dwc2_hsotg_core_connect(hsotg);
3200197ba5f4SPaul Zimmerman 	} else {
3201fc30c4bbSJohn Stultz host:
3202197ba5f4SPaul Zimmerman 		/* A-Device connector (Host Mode) */
3203197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId A\n");
3204197ba5f4SPaul Zimmerman 		while (!dwc2_is_host_mode(hsotg)) {
3205197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3206197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ?
3207197ba5f4SPaul Zimmerman 				 "Host" : "Peripheral");
320804a9db79SNicholas Mc Guire 			msleep(20);
3209197ba5f4SPaul Zimmerman 			if (++count > 250)
3210197ba5f4SPaul Zimmerman 				break;
3211197ba5f4SPaul Zimmerman 		}
3212197ba5f4SPaul Zimmerman 		if (count > 250)
3213197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3214197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3215197ba5f4SPaul Zimmerman 
3216d2471d4aSJohn Stultz 		spin_lock_irqsave(&hsotg->lock, flags);
3217d2471d4aSJohn Stultz 		dwc2_hsotg_disconnect(hsotg);
3218d2471d4aSJohn Stultz 		spin_unlock_irqrestore(&hsotg->lock, flags);
3219d2471d4aSJohn Stultz 
3220d2471d4aSJohn Stultz 		hsotg->op_state = OTG_STATE_A_HOST;
3221197ba5f4SPaul Zimmerman 		/* Initialize the Core for Host mode */
32220fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3223197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
3224197ba5f4SPaul Zimmerman 		dwc2_hcd_start(hsotg);
3225197ba5f4SPaul Zimmerman 	}
3226197ba5f4SPaul Zimmerman }
3227197ba5f4SPaul Zimmerman 
3228e99e88a9SKees Cook static void dwc2_wakeup_detected(struct timer_list *t)
3229197ba5f4SPaul Zimmerman {
3230e99e88a9SKees Cook 	struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
3231197ba5f4SPaul Zimmerman 	u32 hprt0;
3232197ba5f4SPaul Zimmerman 
3233197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3234197ba5f4SPaul Zimmerman 
3235197ba5f4SPaul Zimmerman 	/*
3236197ba5f4SPaul Zimmerman 	 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3237197ba5f4SPaul Zimmerman 	 * so that OPT tests pass with all PHYs.)
3238197ba5f4SPaul Zimmerman 	 */
3239197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3240197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3241197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RES;
3242f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
3243197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
3244f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPRT0));
3245197ba5f4SPaul Zimmerman 
3246197ba5f4SPaul Zimmerman 	dwc2_hcd_rem_wakeup(hsotg);
3247fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
3248197ba5f4SPaul Zimmerman 
3249197ba5f4SPaul Zimmerman 	/* Change to L0 state */
3250197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L0;
3251197ba5f4SPaul Zimmerman }
3252197ba5f4SPaul Zimmerman 
3253197ba5f4SPaul Zimmerman static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3254197ba5f4SPaul Zimmerman {
3255197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3256197ba5f4SPaul Zimmerman 
3257197ba5f4SPaul Zimmerman 	return hcd->self.b_hnp_enable;
3258197ba5f4SPaul Zimmerman }
3259197ba5f4SPaul Zimmerman 
3260139fae7aSArtur Petrosyan /**
3261139fae7aSArtur Petrosyan  * dwc2_port_suspend() - Put controller in suspend mode for host.
3262139fae7aSArtur Petrosyan  *
3263139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3264139fae7aSArtur Petrosyan  * @windex: The control request wIndex field
3265139fae7aSArtur Petrosyan  *
326622ff0c8eSArtur Petrosyan  * Return: non-zero if failed to enter suspend mode for host.
326722ff0c8eSArtur Petrosyan  *
3268139fae7aSArtur Petrosyan  * This function is for entering Host mode suspend.
3269139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3270139fae7aSArtur Petrosyan  */
327122ff0c8eSArtur Petrosyan int dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3272197ba5f4SPaul Zimmerman {
3273197ba5f4SPaul Zimmerman 	unsigned long flags;
3274197ba5f4SPaul Zimmerman 	u32 pcgctl;
3275197ba5f4SPaul Zimmerman 	u32 gotgctl;
327622ff0c8eSArtur Petrosyan 	int ret = 0;
3277197ba5f4SPaul Zimmerman 
3278197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3279197ba5f4SPaul Zimmerman 
3280197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
3281197ba5f4SPaul Zimmerman 
3282197ba5f4SPaul Zimmerman 	if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
3283f25c42b8SGevorg Sahakyan 		gotgctl = dwc2_readl(hsotg, GOTGCTL);
3284197ba5f4SPaul Zimmerman 		gotgctl |= GOTGCTL_HSTSETHNPEN;
3285f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gotgctl, GOTGCTL);
3286197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_A_SUSPEND;
3287197ba5f4SPaul Zimmerman 	}
3288197ba5f4SPaul Zimmerman 
328922ff0c8eSArtur Petrosyan 	switch (hsotg->params.power_down) {
329022ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
329122ff0c8eSArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
329222ff0c8eSArtur Petrosyan 		if (ret)
329322ff0c8eSArtur Petrosyan 			dev_err(hsotg->dev,
329422ff0c8eSArtur Petrosyan 				"enter partial_power_down failed.\n");
329522ff0c8eSArtur Petrosyan 		break;
329622ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
32978f7f8689SArtur Petrosyan 		/*
32988f7f8689SArtur Petrosyan 		 * Perform spin unlock and lock because in
32998f7f8689SArtur Petrosyan 		 * "dwc2_host_enter_hibernation()" function there is a spinlock
33008f7f8689SArtur Petrosyan 		 * logic which prevents servicing of any IRQ during entering
33018f7f8689SArtur Petrosyan 		 * hibernation.
33028f7f8689SArtur Petrosyan 		 */
33038f7f8689SArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
33048f7f8689SArtur Petrosyan 		ret = dwc2_enter_hibernation(hsotg, 1);
33058f7f8689SArtur Petrosyan 		if (ret)
33068f7f8689SArtur Petrosyan 			dev_err(hsotg->dev, "enter hibernation failed.\n");
33078f7f8689SArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
33088f7f8689SArtur Petrosyan 		break;
330922ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3310a2a23d3fSGregory Herrero 		/*
3311d37b939cSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
3312d37b939cSArtur Petrosyan 		 * clock gating is used to save power.
3313a2a23d3fSGregory Herrero 		 */
3314c4a0f7a6SMarek Szyprowski 		if (!hsotg->params.no_clock_gating)
3315d37b939cSArtur Petrosyan 			dwc2_host_enter_clock_gating(hsotg);
3316d37b939cSArtur Petrosyan 		break;
331722ff0c8eSArtur Petrosyan 	}
3318197ba5f4SPaul Zimmerman 
3319197ba5f4SPaul Zimmerman 	/* For HNP the bus must be suspended for at least 200ms */
3320197ba5f4SPaul Zimmerman 	if (dwc2_host_is_b_hnp_enabled(hsotg)) {
3321f25c42b8SGevorg Sahakyan 		pcgctl = dwc2_readl(hsotg, PCGCTL);
3322197ba5f4SPaul Zimmerman 		pcgctl &= ~PCGCTL_STOPPCLK;
3323f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgctl, PCGCTL);
3324197ba5f4SPaul Zimmerman 
3325197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3326197ba5f4SPaul Zimmerman 
332704a9db79SNicholas Mc Guire 		msleep(200);
3328197ba5f4SPaul Zimmerman 	} else {
3329197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3330197ba5f4SPaul Zimmerman 	}
333122ff0c8eSArtur Petrosyan 
333222ff0c8eSArtur Petrosyan 	return ret;
3333197ba5f4SPaul Zimmerman }
3334197ba5f4SPaul Zimmerman 
3335139fae7aSArtur Petrosyan /**
3336139fae7aSArtur Petrosyan  * dwc2_port_resume() - Exit controller from suspend mode for host.
3337139fae7aSArtur Petrosyan  *
3338139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3339139fae7aSArtur Petrosyan  *
33401e0890ebSArtur Petrosyan  * Return: non-zero if failed to exit suspend mode for host.
33411e0890ebSArtur Petrosyan  *
3342139fae7aSArtur Petrosyan  * This function is for exiting Host mode suspend.
3343139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3344139fae7aSArtur Petrosyan  */
33451e0890ebSArtur Petrosyan int dwc2_port_resume(struct dwc2_hsotg *hsotg)
334630db103cSGregory Herrero {
334730db103cSGregory Herrero 	unsigned long flags;
33481e0890ebSArtur Petrosyan 	int ret = 0;
334930db103cSGregory Herrero 
33504d273c2aSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
33514d273c2aSDouglas Anderson 
33521e0890ebSArtur Petrosyan 	switch (hsotg->params.power_down) {
33531e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
33541e0890ebSArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
33551e0890ebSArtur Petrosyan 		if (ret)
33561e0890ebSArtur Petrosyan 			dev_err(hsotg->dev,
33571e0890ebSArtur Petrosyan 				"exit partial_power_down failed.\n");
33581e0890ebSArtur Petrosyan 		break;
33591e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
3360e358c215SArtur Petrosyan 		/* Exit host hibernation. */
3361e358c215SArtur Petrosyan 		ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
3362e358c215SArtur Petrosyan 		if (ret)
3363e358c215SArtur Petrosyan 			dev_err(hsotg->dev, "exit hibernation failed.\n");
3364e358c215SArtur Petrosyan 		break;
33651e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3366a2a23d3fSGregory Herrero 		/*
33673cf8143eSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
33683cf8143eSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
3369a2a23d3fSGregory Herrero 		 */
33704d273c2aSDouglas Anderson 		spin_unlock_irqrestore(&hsotg->lock, flags);
33713cf8143eSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
33724d273c2aSDouglas Anderson 		spin_lock_irqsave(&hsotg->lock, flags);
33733cf8143eSArtur Petrosyan 		break;
33741e0890ebSArtur Petrosyan 	}
33751e0890ebSArtur Petrosyan 
337630db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
33771e0890ebSArtur Petrosyan 
33781e0890ebSArtur Petrosyan 	return ret;
337930db103cSGregory Herrero }
338030db103cSGregory Herrero 
3381197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
3382197ba5f4SPaul Zimmerman static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3383197ba5f4SPaul Zimmerman 				u16 wvalue, u16 windex, char *buf, u16 wlength)
3384197ba5f4SPaul Zimmerman {
3385197ba5f4SPaul Zimmerman 	struct usb_hub_descriptor *hub_desc;
3386197ba5f4SPaul Zimmerman 	int retval = 0;
3387197ba5f4SPaul Zimmerman 	u32 hprt0;
3388197ba5f4SPaul Zimmerman 	u32 port_status;
3389197ba5f4SPaul Zimmerman 	u32 speed;
3390197ba5f4SPaul Zimmerman 	u32 pcgctl;
3391cd7cd0e6SFabrice Gasnier 	u32 pwr;
3392197ba5f4SPaul Zimmerman 
3393197ba5f4SPaul Zimmerman 	switch (typereq) {
3394197ba5f4SPaul Zimmerman 	case ClearHubFeature:
3395197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3396197ba5f4SPaul Zimmerman 
3397197ba5f4SPaul Zimmerman 		switch (wvalue) {
3398197ba5f4SPaul Zimmerman 		case C_HUB_LOCAL_POWER:
3399197ba5f4SPaul Zimmerman 		case C_HUB_OVER_CURRENT:
3400197ba5f4SPaul Zimmerman 			/* Nothing required here */
3401197ba5f4SPaul Zimmerman 			break;
3402197ba5f4SPaul Zimmerman 
3403197ba5f4SPaul Zimmerman 		default:
3404197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3405197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3406197ba5f4SPaul Zimmerman 				"ClearHubFeature request %1xh unknown\n",
3407197ba5f4SPaul Zimmerman 				wvalue);
3408197ba5f4SPaul Zimmerman 		}
3409197ba5f4SPaul Zimmerman 		break;
3410197ba5f4SPaul Zimmerman 
3411197ba5f4SPaul Zimmerman 	case ClearPortFeature:
3412197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_L1)
3413197ba5f4SPaul Zimmerman 			if (!windex || windex > 1)
3414197ba5f4SPaul Zimmerman 				goto error;
3415197ba5f4SPaul Zimmerman 		switch (wvalue) {
3416197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_ENABLE:
3417197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3418197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3419197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3420197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_ENA;
3421f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3422197ba5f4SPaul Zimmerman 			break;
3423197ba5f4SPaul Zimmerman 
3424197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3425197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3426197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
3427b0bb9bb6SPaul Zimmerman 
3428e358c215SArtur Petrosyan 			if (hsotg->bus_suspended)
3429e358c215SArtur Petrosyan 				retval = dwc2_port_resume(hsotg);
3430197ba5f4SPaul Zimmerman 			break;
3431197ba5f4SPaul Zimmerman 
3432197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3433197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3434197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_POWER\n");
3435197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3436cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3437197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_PWR;
3438f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3439cd7cd0e6SFabrice Gasnier 			if (pwr)
3440cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_exit(hsotg);
3441197ba5f4SPaul Zimmerman 			break;
3442197ba5f4SPaul Zimmerman 
3443197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3444197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3445197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3446197ba5f4SPaul Zimmerman 			/* Port indicator not supported */
3447197ba5f4SPaul Zimmerman 			break;
3448197ba5f4SPaul Zimmerman 
3449197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_CONNECTION:
3450197ba5f4SPaul Zimmerman 			/*
3451197ba5f4SPaul Zimmerman 			 * Clears driver's internal Connect Status Change flag
3452197ba5f4SPaul Zimmerman 			 */
3453197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3454197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3455197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change = 0;
3456197ba5f4SPaul Zimmerman 			break;
3457197ba5f4SPaul Zimmerman 
3458197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_RESET:
3459197ba5f4SPaul Zimmerman 			/* Clears driver's internal Port Reset Change flag */
3460197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3461197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3462197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change = 0;
3463197ba5f4SPaul Zimmerman 			break;
3464197ba5f4SPaul Zimmerman 
3465197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_ENABLE:
3466197ba5f4SPaul Zimmerman 			/*
3467197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Enable/Disable
3468197ba5f4SPaul Zimmerman 			 * Change flag
3469197ba5f4SPaul Zimmerman 			 */
3470197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3471197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3472197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change = 0;
3473197ba5f4SPaul Zimmerman 			break;
3474197ba5f4SPaul Zimmerman 
3475197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_SUSPEND:
3476197ba5f4SPaul Zimmerman 			/*
3477197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Suspend Change
3478197ba5f4SPaul Zimmerman 			 * flag, which is set when resume signaling on the host
3479197ba5f4SPaul Zimmerman 			 * port is complete
3480197ba5f4SPaul Zimmerman 			 */
3481197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3482197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3483197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change = 0;
3484197ba5f4SPaul Zimmerman 			break;
3485197ba5f4SPaul Zimmerman 
3486197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_PORT_L1:
3487197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3488197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3489197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_l1_change = 0;
3490197ba5f4SPaul Zimmerman 			break;
3491197ba5f4SPaul Zimmerman 
3492197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_OVER_CURRENT:
3493197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3494197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3495197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change = 0;
3496197ba5f4SPaul Zimmerman 			break;
3497197ba5f4SPaul Zimmerman 
3498197ba5f4SPaul Zimmerman 		default:
3499197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3500197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3501197ba5f4SPaul Zimmerman 				"ClearPortFeature request %1xh unknown or unsupported\n",
3502197ba5f4SPaul Zimmerman 				wvalue);
3503197ba5f4SPaul Zimmerman 		}
3504197ba5f4SPaul Zimmerman 		break;
3505197ba5f4SPaul Zimmerman 
3506197ba5f4SPaul Zimmerman 	case GetHubDescriptor:
3507197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3508197ba5f4SPaul Zimmerman 		hub_desc = (struct usb_hub_descriptor *)buf;
3509197ba5f4SPaul Zimmerman 		hub_desc->bDescLength = 9;
3510a5dd0395SSergei Shtylyov 		hub_desc->bDescriptorType = USB_DT_HUB;
3511197ba5f4SPaul Zimmerman 		hub_desc->bNbrPorts = 1;
35123d040de8SSergei Shtylyov 		hub_desc->wHubCharacteristics =
35133d040de8SSergei Shtylyov 			cpu_to_le16(HUB_CHAR_COMMON_LPSM |
35143d040de8SSergei Shtylyov 				    HUB_CHAR_INDV_PORT_OCPM);
3515197ba5f4SPaul Zimmerman 		hub_desc->bPwrOn2PwrGood = 1;
3516197ba5f4SPaul Zimmerman 		hub_desc->bHubContrCurrent = 0;
3517197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[0] = 0;
3518197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3519197ba5f4SPaul Zimmerman 		break;
3520197ba5f4SPaul Zimmerman 
3521197ba5f4SPaul Zimmerman 	case GetHubStatus:
3522197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubStatus\n");
3523197ba5f4SPaul Zimmerman 		memset(buf, 0, 4);
3524197ba5f4SPaul Zimmerman 		break;
3525197ba5f4SPaul Zimmerman 
3526197ba5f4SPaul Zimmerman 	case GetPortStatus:
3527197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3528197ba5f4SPaul Zimmerman 			 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3529197ba5f4SPaul Zimmerman 			 hsotg->flags.d32);
3530197ba5f4SPaul Zimmerman 		if (!windex || windex > 1)
3531197ba5f4SPaul Zimmerman 			goto error;
3532197ba5f4SPaul Zimmerman 
3533197ba5f4SPaul Zimmerman 		port_status = 0;
3534197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status_change)
3535197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3536197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_enable_change)
3537197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_ENABLE << 16;
3538197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_suspend_change)
3539197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3540197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_l1_change)
3541197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_L1 << 16;
3542197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_reset_change)
3543197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_RESET << 16;
3544197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_over_current_change) {
3545197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "Overcurrent change detected\n");
3546197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3547197ba5f4SPaul Zimmerman 		}
3548197ba5f4SPaul Zimmerman 
3549197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3550197ba5f4SPaul Zimmerman 			/*
3551197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3552197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3553197ba5f4SPaul Zimmerman 			 * return 0's for the remainder of the port status
3554197ba5f4SPaul Zimmerman 			 * since the port register can't be read if the core
3555197ba5f4SPaul Zimmerman 			 * is in device mode.
3556197ba5f4SPaul Zimmerman 			 */
3557197ba5f4SPaul Zimmerman 			*(__le32 *)buf = cpu_to_le32(port_status);
3558197ba5f4SPaul Zimmerman 			break;
3559197ba5f4SPaul Zimmerman 		}
3560197ba5f4SPaul Zimmerman 
3561f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
3562197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  HPRT0: 0x%08x\n", hprt0);
3563197ba5f4SPaul Zimmerman 
3564197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_CONNSTS)
3565197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_CONNECTION;
3566197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_ENA)
3567197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_ENABLE;
3568197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_SUSP)
3569197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_SUSPEND;
3570197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_OVRCURRACT)
3571197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_OVERCURRENT;
3572197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_RST)
3573197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_RESET;
3574197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_PWR)
3575197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_POWER;
3576197ba5f4SPaul Zimmerman 
3577197ba5f4SPaul Zimmerman 		speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
3578197ba5f4SPaul Zimmerman 		if (speed == HPRT0_SPD_HIGH_SPEED)
3579197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_HIGH_SPEED;
3580197ba5f4SPaul Zimmerman 		else if (speed == HPRT0_SPD_LOW_SPEED)
3581197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_LOW_SPEED;
3582197ba5f4SPaul Zimmerman 
3583197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_TSTCTL_MASK)
3584197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_TEST;
3585197ba5f4SPaul Zimmerman 		/* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3586197ba5f4SPaul Zimmerman 
3587bea8e86cSJohn Youn 		if (hsotg->params.dma_desc_fs_enable) {
3588fbb9e22bSMian Yousaf Kaukab 			/*
3589fbb9e22bSMian Yousaf Kaukab 			 * Enable descriptor DMA only if a full speed
3590fbb9e22bSMian Yousaf Kaukab 			 * device is connected.
3591fbb9e22bSMian Yousaf Kaukab 			 */
3592fbb9e22bSMian Yousaf Kaukab 			if (hsotg->new_connection &&
3593fbb9e22bSMian Yousaf Kaukab 			    ((port_status &
3594fbb9e22bSMian Yousaf Kaukab 			      (USB_PORT_STAT_CONNECTION |
3595fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_HIGH_SPEED |
3596fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_LOW_SPEED)) ==
3597fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_CONNECTION)) {
3598fbb9e22bSMian Yousaf Kaukab 				u32 hcfg;
3599fbb9e22bSMian Yousaf Kaukab 
3600fbb9e22bSMian Yousaf Kaukab 				dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
360195832c00SJohn Youn 				hsotg->params.dma_desc_enable = true;
3602f25c42b8SGevorg Sahakyan 				hcfg = dwc2_readl(hsotg, HCFG);
3603fbb9e22bSMian Yousaf Kaukab 				hcfg |= HCFG_DESCDMA;
3604f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcfg, HCFG);
3605fbb9e22bSMian Yousaf Kaukab 				hsotg->new_connection = false;
3606fbb9e22bSMian Yousaf Kaukab 			}
3607fbb9e22bSMian Yousaf Kaukab 		}
3608fbb9e22bSMian Yousaf Kaukab 
3609197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
3610197ba5f4SPaul Zimmerman 		*(__le32 *)buf = cpu_to_le32(port_status);
3611197ba5f4SPaul Zimmerman 		break;
3612197ba5f4SPaul Zimmerman 
3613197ba5f4SPaul Zimmerman 	case SetHubFeature:
3614197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetHubFeature\n");
3615197ba5f4SPaul Zimmerman 		/* No HUB features supported */
3616197ba5f4SPaul Zimmerman 		break;
3617197ba5f4SPaul Zimmerman 
3618197ba5f4SPaul Zimmerman 	case SetPortFeature:
3619197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetPortFeature\n");
3620197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3621197ba5f4SPaul Zimmerman 			goto error;
3622197ba5f4SPaul Zimmerman 
3623197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3624197ba5f4SPaul Zimmerman 			/*
3625197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3626197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3627197ba5f4SPaul Zimmerman 			 * return without doing anything since the port
3628197ba5f4SPaul Zimmerman 			 * register can't be written if the core is in device
3629197ba5f4SPaul Zimmerman 			 * mode.
3630197ba5f4SPaul Zimmerman 			 */
3631197ba5f4SPaul Zimmerman 			break;
3632197ba5f4SPaul Zimmerman 		}
3633197ba5f4SPaul Zimmerman 
3634197ba5f4SPaul Zimmerman 		switch (wvalue) {
3635197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3636197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3637197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3638197ba5f4SPaul Zimmerman 			if (windex != hsotg->otg_port)
3639197ba5f4SPaul Zimmerman 				goto error;
36408f7f8689SArtur Petrosyan 			if (!hsotg->bus_suspended)
36418f7f8689SArtur Petrosyan 				retval = dwc2_port_suspend(hsotg, windex);
3642197ba5f4SPaul Zimmerman 			break;
3643197ba5f4SPaul Zimmerman 
3644197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3645197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3646197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_POWER\n");
3647197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3648cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3649197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_PWR;
3650f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3651cd7cd0e6SFabrice Gasnier 			if (!pwr)
3652cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_init(hsotg);
3653197ba5f4SPaul Zimmerman 			break;
3654197ba5f4SPaul Zimmerman 
3655197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_RESET:
3656c363af9cSArtur Petrosyan 			dev_dbg(hsotg->dev,
3657c363af9cSArtur Petrosyan 				"SetPortFeature - USB_PORT_FEAT_RESET\n");
3658c363af9cSArtur Petrosyan 
3659c363af9cSArtur Petrosyan 			hprt0 = dwc2_read_hprt0(hsotg);
3660c363af9cSArtur Petrosyan 
3661c363af9cSArtur Petrosyan 			if (hsotg->hibernated) {
3662c363af9cSArtur Petrosyan 				retval = dwc2_exit_hibernation(hsotg, 0, 1, 1);
3663c363af9cSArtur Petrosyan 				if (retval)
3664c363af9cSArtur Petrosyan 					dev_err(hsotg->dev,
3665c363af9cSArtur Petrosyan 						"exit hibernation failed\n");
3666c363af9cSArtur Petrosyan 			}
3667e97570f7SArtur Petrosyan 
3668e97570f7SArtur Petrosyan 			if (hsotg->in_ppd) {
3669e97570f7SArtur Petrosyan 				retval = dwc2_exit_partial_power_down(hsotg, 1,
3670e97570f7SArtur Petrosyan 								      true);
3671e97570f7SArtur Petrosyan 				if (retval)
3672e97570f7SArtur Petrosyan 					dev_err(hsotg->dev,
3673e97570f7SArtur Petrosyan 						"exit partial_power_down failed\n");
3674e97570f7SArtur Petrosyan 			}
3675e97570f7SArtur Petrosyan 
36765f9e60c0SArtur Petrosyan 			if (hsotg->params.power_down ==
36775f9e60c0SArtur Petrosyan 			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended)
36785f9e60c0SArtur Petrosyan 				dwc2_host_exit_clock_gating(hsotg, 0);
36795f9e60c0SArtur Petrosyan 
3680f25c42b8SGevorg Sahakyan 			pcgctl = dwc2_readl(hsotg, PCGCTL);
3681197ba5f4SPaul Zimmerman 			pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
3682f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, pcgctl, PCGCTL);
3683197ba5f4SPaul Zimmerman 			/* ??? Original driver does this */
3684f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, PCGCTL);
3685197ba5f4SPaul Zimmerman 
3686197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3687cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3688197ba5f4SPaul Zimmerman 			/* Clear suspend bit if resetting from suspend state */
3689197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_SUSP;
3690197ba5f4SPaul Zimmerman 
3691197ba5f4SPaul Zimmerman 			/*
3692197ba5f4SPaul Zimmerman 			 * When B-Host the Port reset bit is set in the Start
3693197ba5f4SPaul Zimmerman 			 * HCD Callback function, so that the reset is started
3694197ba5f4SPaul Zimmerman 			 * within 1ms of the HNP success interrupt
3695197ba5f4SPaul Zimmerman 			 */
3696197ba5f4SPaul Zimmerman 			if (!dwc2_hcd_is_b_host(hsotg)) {
3697197ba5f4SPaul Zimmerman 				hprt0 |= HPRT0_PWR | HPRT0_RST;
3698197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3699197ba5f4SPaul Zimmerman 					"In host mode, hprt0=%08x\n", hprt0);
3700f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hprt0, HPRT0);
3701cd7cd0e6SFabrice Gasnier 				if (!pwr)
3702cd7cd0e6SFabrice Gasnier 					dwc2_vbus_supply_init(hsotg);
3703197ba5f4SPaul Zimmerman 			}
3704197ba5f4SPaul Zimmerman 
3705197ba5f4SPaul Zimmerman 			/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
370604a9db79SNicholas Mc Guire 			msleep(50);
3707197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_RST;
3708f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3709197ba5f4SPaul Zimmerman 			hsotg->lx_state = DWC2_L0; /* Now back to On state */
3710197ba5f4SPaul Zimmerman 			break;
3711197ba5f4SPaul Zimmerman 
3712197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3713197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3714197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3715197ba5f4SPaul Zimmerman 			/* Not supported */
3716197ba5f4SPaul Zimmerman 			break;
3717197ba5f4SPaul Zimmerman 
371896d480e6SJingwu Lin 		case USB_PORT_FEAT_TEST:
371996d480e6SJingwu Lin 			hprt0 = dwc2_read_hprt0(hsotg);
372096d480e6SJingwu Lin 			dev_dbg(hsotg->dev,
372196d480e6SJingwu Lin 				"SetPortFeature - USB_PORT_FEAT_TEST\n");
372296d480e6SJingwu Lin 			hprt0 &= ~HPRT0_TSTCTL_MASK;
372396d480e6SJingwu Lin 			hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
3724f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
372596d480e6SJingwu Lin 			break;
372696d480e6SJingwu Lin 
3727197ba5f4SPaul Zimmerman 		default:
3728197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3729197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3730197ba5f4SPaul Zimmerman 				"SetPortFeature %1xh unknown or unsupported\n",
3731197ba5f4SPaul Zimmerman 				wvalue);
3732197ba5f4SPaul Zimmerman 			break;
3733197ba5f4SPaul Zimmerman 		}
3734197ba5f4SPaul Zimmerman 		break;
3735197ba5f4SPaul Zimmerman 
3736197ba5f4SPaul Zimmerman 	default:
3737197ba5f4SPaul Zimmerman error:
3738197ba5f4SPaul Zimmerman 		retval = -EINVAL;
3739197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3740197ba5f4SPaul Zimmerman 			"Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3741197ba5f4SPaul Zimmerman 			typereq, windex, wvalue);
3742197ba5f4SPaul Zimmerman 		break;
3743197ba5f4SPaul Zimmerman 	}
3744197ba5f4SPaul Zimmerman 
3745197ba5f4SPaul Zimmerman 	return retval;
3746197ba5f4SPaul Zimmerman }
3747197ba5f4SPaul Zimmerman 
3748197ba5f4SPaul Zimmerman static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3749197ba5f4SPaul Zimmerman {
3750197ba5f4SPaul Zimmerman 	int retval;
3751197ba5f4SPaul Zimmerman 
3752197ba5f4SPaul Zimmerman 	if (port != 1)
3753197ba5f4SPaul Zimmerman 		return -EINVAL;
3754197ba5f4SPaul Zimmerman 
3755197ba5f4SPaul Zimmerman 	retval = (hsotg->flags.b.port_connect_status_change ||
3756197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_reset_change ||
3757197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_enable_change ||
3758197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_suspend_change ||
3759197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_over_current_change);
3760197ba5f4SPaul Zimmerman 
3761197ba5f4SPaul Zimmerman 	if (retval) {
3762197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3763197ba5f4SPaul Zimmerman 			"DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3764197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_connect_status_change: %d\n",
3765197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change);
3766197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_reset_change: %d\n",
3767197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change);
3768197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_enable_change: %d\n",
3769197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change);
3770197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_suspend_change: %d\n",
3771197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change);
3772197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_over_current_change: %d\n",
3773197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change);
3774197ba5f4SPaul Zimmerman 	}
3775197ba5f4SPaul Zimmerman 
3776197ba5f4SPaul Zimmerman 	return retval;
3777197ba5f4SPaul Zimmerman }
3778197ba5f4SPaul Zimmerman 
3779197ba5f4SPaul Zimmerman int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3780197ba5f4SPaul Zimmerman {
3781f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3782197ba5f4SPaul Zimmerman 
3783197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3784197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
3785197ba5f4SPaul Zimmerman 		 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
3786197ba5f4SPaul Zimmerman #endif
3787197ba5f4SPaul Zimmerman 	return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3788197ba5f4SPaul Zimmerman }
3789197ba5f4SPaul Zimmerman 
3790fae4e826SDouglas Anderson int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3791fae4e826SDouglas Anderson {
3792f25c42b8SGevorg Sahakyan 	u32 hprt = dwc2_readl(hsotg, HPRT0);
3793f25c42b8SGevorg Sahakyan 	u32 hfir = dwc2_readl(hsotg, HFIR);
3794f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3795fae4e826SDouglas Anderson 	unsigned int us_per_frame;
3796fae4e826SDouglas Anderson 	unsigned int frame_number;
3797fae4e826SDouglas Anderson 	unsigned int remaining;
3798fae4e826SDouglas Anderson 	unsigned int interval;
3799fae4e826SDouglas Anderson 	unsigned int phy_clks;
3800fae4e826SDouglas Anderson 
3801fae4e826SDouglas Anderson 	/* High speed has 125 us per (micro) frame; others are 1 ms per */
3802fae4e826SDouglas Anderson 	us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3803fae4e826SDouglas Anderson 
3804fae4e826SDouglas Anderson 	/* Extract fields */
3805fae4e826SDouglas Anderson 	frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3806fae4e826SDouglas Anderson 	remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3807fae4e826SDouglas Anderson 	interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3808fae4e826SDouglas Anderson 
3809fae4e826SDouglas Anderson 	/*
3810fae4e826SDouglas Anderson 	 * Number of phy clocks since the last tick of the frame number after
3811fae4e826SDouglas Anderson 	 * "us" has passed.
3812fae4e826SDouglas Anderson 	 */
3813fae4e826SDouglas Anderson 	phy_clks = (interval - remaining) +
3814fae4e826SDouglas Anderson 		   DIV_ROUND_UP(interval * us, us_per_frame);
3815fae4e826SDouglas Anderson 
3816fae4e826SDouglas Anderson 	return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3817fae4e826SDouglas Anderson }
3818fae4e826SDouglas Anderson 
3819197ba5f4SPaul Zimmerman int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3820197ba5f4SPaul Zimmerman {
3821197ba5f4SPaul Zimmerman 	return hsotg->op_state == OTG_STATE_B_HOST;
3822197ba5f4SPaul Zimmerman }
3823197ba5f4SPaul Zimmerman 
3824197ba5f4SPaul Zimmerman static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3825197ba5f4SPaul Zimmerman 					       int iso_desc_count,
3826197ba5f4SPaul Zimmerman 					       gfp_t mem_flags)
3827197ba5f4SPaul Zimmerman {
3828197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3829197ba5f4SPaul Zimmerman 
3830eeca7606SGustavo A. R. Silva 	urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
3831197ba5f4SPaul Zimmerman 	if (urb)
3832197ba5f4SPaul Zimmerman 		urb->packet_count = iso_desc_count;
3833197ba5f4SPaul Zimmerman 	return urb;
3834197ba5f4SPaul Zimmerman }
3835197ba5f4SPaul Zimmerman 
3836197ba5f4SPaul Zimmerman static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3837197ba5f4SPaul Zimmerman 				      struct dwc2_hcd_urb *urb, u8 dev_addr,
3838babd1839SDouglas Anderson 				      u8 ep_num, u8 ep_type, u8 ep_dir,
3839babd1839SDouglas Anderson 				      u16 maxp, u16 maxp_mult)
3840197ba5f4SPaul Zimmerman {
3841197ba5f4SPaul Zimmerman 	if (dbg_perio() ||
3842197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_BULK ||
3843197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_CONTROL)
3844197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3845babd1839SDouglas Anderson 			 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
3846babd1839SDouglas Anderson 			 dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
3847197ba5f4SPaul Zimmerman 	urb->pipe_info.dev_addr = dev_addr;
3848197ba5f4SPaul Zimmerman 	urb->pipe_info.ep_num = ep_num;
3849197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_type = ep_type;
3850197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_dir = ep_dir;
3851babd1839SDouglas Anderson 	urb->pipe_info.maxp = maxp;
3852babd1839SDouglas Anderson 	urb->pipe_info.maxp_mult = maxp_mult;
3853197ba5f4SPaul Zimmerman }
3854197ba5f4SPaul Zimmerman 
3855197ba5f4SPaul Zimmerman /*
3856197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
3857197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
3858197ba5f4SPaul Zimmerman  */
3859197ba5f4SPaul Zimmerman void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3860197ba5f4SPaul Zimmerman {
3861197ba5f4SPaul Zimmerman #ifdef DEBUG
3862197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
3863197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3864197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
3865197ba5f4SPaul Zimmerman 	int num_channels;
3866197ba5f4SPaul Zimmerman 	u32 np_tx_status;
3867197ba5f4SPaul Zimmerman 	u32 p_tx_status;
3868197ba5f4SPaul Zimmerman 	int i;
3869197ba5f4SPaul Zimmerman 
3870bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
3871197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3872197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3873197ba5f4SPaul Zimmerman 		"************************************************************\n");
3874197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HCD State:\n");
3875197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Num channels: %d\n", num_channels);
3876197ba5f4SPaul Zimmerman 
3877197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
3878197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
3879197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  Channel %d:\n", i);
3880197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3881197ba5f4SPaul Zimmerman 			"    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3882197ba5f4SPaul Zimmerman 			chan->dev_addr, chan->ep_num, chan->ep_is_in);
3883197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    speed: %d\n", chan->speed);
3884197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
3885197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
3886197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    data_pid_start: %d\n",
3887197ba5f4SPaul Zimmerman 			chan->data_pid_start);
3888197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    multi_count: %d\n", chan->multi_count);
3889197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_started: %d\n",
3890197ba5f4SPaul Zimmerman 			chan->xfer_started);
3891197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
3892197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
3893197ba5f4SPaul Zimmerman 			(unsigned long)chan->xfer_dma);
3894197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
3895197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_count: %d\n", chan->xfer_count);
3896197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_on_queue: %d\n",
3897197ba5f4SPaul Zimmerman 			chan->halt_on_queue);
3898197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_pending: %d\n",
3899197ba5f4SPaul Zimmerman 			chan->halt_pending);
3900197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
3901197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    do_split: %d\n", chan->do_split);
3902197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    complete_split: %d\n",
3903197ba5f4SPaul Zimmerman 			chan->complete_split);
3904197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_addr: %d\n", chan->hub_addr);
3905197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_port: %d\n", chan->hub_port);
3906197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xact_pos: %d\n", chan->xact_pos);
3907197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    requests: %d\n", chan->requests);
3908197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
3909197ba5f4SPaul Zimmerman 
3910197ba5f4SPaul Zimmerman 		if (chan->xfer_started) {
3911197ba5f4SPaul Zimmerman 			u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3912197ba5f4SPaul Zimmerman 
3913f25c42b8SGevorg Sahakyan 			hfnum = dwc2_readl(hsotg, HFNUM);
3914f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
3915f25c42b8SGevorg Sahakyan 			hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
3916f25c42b8SGevorg Sahakyan 			hcint = dwc2_readl(hsotg, HCINT(i));
3917f25c42b8SGevorg Sahakyan 			hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
3918197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hfnum: 0x%08x\n", hfnum);
3919197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcchar: 0x%08x\n", hcchar);
3920197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hctsiz: 0x%08x\n", hctsiz);
3921197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcint: 0x%08x\n", hcint);
3922197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcintmsk: 0x%08x\n", hcintmsk);
3923197ba5f4SPaul Zimmerman 		}
3924197ba5f4SPaul Zimmerman 
3925197ba5f4SPaul Zimmerman 		if (!(chan->xfer_started && chan->qh))
3926197ba5f4SPaul Zimmerman 			continue;
3927197ba5f4SPaul Zimmerman 
3928197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3929197ba5f4SPaul Zimmerman 			if (!qtd->in_process)
3930197ba5f4SPaul Zimmerman 				break;
3931197ba5f4SPaul Zimmerman 			urb = qtd->urb;
3932197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    URB Info:\n");
3933197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "      qtd: %p, urb: %p\n",
3934197ba5f4SPaul Zimmerman 				qtd, urb);
3935197ba5f4SPaul Zimmerman 			if (urb) {
3936197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3937197ba5f4SPaul Zimmerman 					"      Dev: %d, EP: %d %s\n",
3938197ba5f4SPaul Zimmerman 					dwc2_hcd_get_dev_addr(&urb->pipe_info),
3939197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_num(&urb->pipe_info),
3940197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3941197ba5f4SPaul Zimmerman 					"IN" : "OUT");
3942197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3943babd1839SDouglas Anderson 					"      Max packet size: %d (%d mult)\n",
3944babd1839SDouglas Anderson 					dwc2_hcd_get_maxp(&urb->pipe_info),
3945babd1839SDouglas Anderson 					dwc2_hcd_get_maxp_mult(&urb->pipe_info));
3946197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3947197ba5f4SPaul Zimmerman 					"      transfer_buffer: %p\n",
3948197ba5f4SPaul Zimmerman 					urb->buf);
3949197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3950197ba5f4SPaul Zimmerman 					"      transfer_dma: %08lx\n",
3951197ba5f4SPaul Zimmerman 					(unsigned long)urb->dma);
3952197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3953197ba5f4SPaul Zimmerman 					"      transfer_buffer_length: %d\n",
3954197ba5f4SPaul Zimmerman 					urb->length);
3955197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev, "      actual_length: %d\n",
3956197ba5f4SPaul Zimmerman 					urb->actual_length);
3957197ba5f4SPaul Zimmerman 			}
3958197ba5f4SPaul Zimmerman 		}
3959197ba5f4SPaul Zimmerman 	}
3960197ba5f4SPaul Zimmerman 
3961197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  non_periodic_channels: %d\n",
3962197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels);
3963197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_channels: %d\n",
3964197ba5f4SPaul Zimmerman 		hsotg->periodic_channels);
3965197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_usecs: %d\n", hsotg->periodic_usecs);
3966f25c42b8SGevorg Sahakyan 	np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
3967197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx Req Queue Space Avail: %d\n",
3968197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3969197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx FIFO Space Avail: %d\n",
3970197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3971f25c42b8SGevorg Sahakyan 	p_tx_status = dwc2_readl(hsotg, HPTXSTS);
3972197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx Req Queue Space Avail: %d\n",
3973197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3974197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx FIFO Space Avail: %d\n",
3975197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3976197ba5f4SPaul Zimmerman 	dwc2_dump_global_registers(hsotg);
3977197ba5f4SPaul Zimmerman 	dwc2_dump_host_registers(hsotg);
3978197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3979197ba5f4SPaul Zimmerman 		"************************************************************\n");
3980197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3981197ba5f4SPaul Zimmerman #endif
3982197ba5f4SPaul Zimmerman }
3983197ba5f4SPaul Zimmerman 
3984197ba5f4SPaul Zimmerman struct wrapper_priv_data {
3985197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg;
3986197ba5f4SPaul Zimmerman };
3987197ba5f4SPaul Zimmerman 
3988197ba5f4SPaul Zimmerman /* Gets the dwc2_hsotg from a usb_hcd */
3989197ba5f4SPaul Zimmerman static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
3990197ba5f4SPaul Zimmerman {
3991197ba5f4SPaul Zimmerman 	struct wrapper_priv_data *p;
3992197ba5f4SPaul Zimmerman 
3993197ba5f4SPaul Zimmerman 	p = (struct wrapper_priv_data *)&hcd->hcd_priv;
3994197ba5f4SPaul Zimmerman 	return p->hsotg;
3995197ba5f4SPaul Zimmerman }
3996197ba5f4SPaul Zimmerman 
39979f9f09b0SDouglas Anderson /**
39989f9f09b0SDouglas Anderson  * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
39999f9f09b0SDouglas Anderson  *
40009f9f09b0SDouglas Anderson  * This will get the dwc2_tt structure (and ttport) associated with the given
40019f9f09b0SDouglas Anderson  * context (which is really just a struct urb pointer).
40029f9f09b0SDouglas Anderson  *
40039f9f09b0SDouglas Anderson  * The first time this is called for a given TT we allocate memory for our
40049f9f09b0SDouglas Anderson  * structure.  When everyone is done and has called dwc2_host_put_tt_info()
40059f9f09b0SDouglas Anderson  * then the refcount for the structure will go to 0 and we'll free it.
40069f9f09b0SDouglas Anderson  *
40079f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40089f9f09b0SDouglas Anderson  * @context:   The priv pointer from a struct dwc2_hcd_urb.
40099f9f09b0SDouglas Anderson  * @mem_flags: Flags for allocating memory.
40109f9f09b0SDouglas Anderson  * @ttport:    We'll return this device's port number here.  That's used to
40119f9f09b0SDouglas Anderson  *             reference into the bitmap if we're on a multi_tt hub.
40129f9f09b0SDouglas Anderson  *
40139f9f09b0SDouglas Anderson  * Return: a pointer to a struct dwc2_tt.  Don't forget to call
40149f9f09b0SDouglas Anderson  *         dwc2_host_put_tt_info()!  Returns NULL upon memory alloc failure.
40159f9f09b0SDouglas Anderson  */
40169f9f09b0SDouglas Anderson 
40179f9f09b0SDouglas Anderson struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
40189f9f09b0SDouglas Anderson 				      gfp_t mem_flags, int *ttport)
40199f9f09b0SDouglas Anderson {
40209f9f09b0SDouglas Anderson 	struct urb *urb = context;
40219f9f09b0SDouglas Anderson 	struct dwc2_tt *dwc_tt = NULL;
40229f9f09b0SDouglas Anderson 
40239f9f09b0SDouglas Anderson 	if (urb->dev->tt) {
40249f9f09b0SDouglas Anderson 		*ttport = urb->dev->ttport;
40259f9f09b0SDouglas Anderson 
40269f9f09b0SDouglas Anderson 		dwc_tt = urb->dev->tt->hcpriv;
40279da51974SJohn Youn 		if (!dwc_tt) {
40289f9f09b0SDouglas Anderson 			size_t bitmap_size;
40299f9f09b0SDouglas Anderson 
40309f9f09b0SDouglas Anderson 			/*
40319f9f09b0SDouglas Anderson 			 * For single_tt we need one schedule.  For multi_tt
40329f9f09b0SDouglas Anderson 			 * we need one per port.
40339f9f09b0SDouglas Anderson 			 */
40349f9f09b0SDouglas Anderson 			bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
40359f9f09b0SDouglas Anderson 				      sizeof(dwc_tt->periodic_bitmaps[0]);
40369f9f09b0SDouglas Anderson 			if (urb->dev->tt->multi)
40379f9f09b0SDouglas Anderson 				bitmap_size *= urb->dev->tt->hub->maxchild;
40389f9f09b0SDouglas Anderson 
40399f9f09b0SDouglas Anderson 			dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
40409f9f09b0SDouglas Anderson 					 mem_flags);
40419da51974SJohn Youn 			if (!dwc_tt)
40429f9f09b0SDouglas Anderson 				return NULL;
40439f9f09b0SDouglas Anderson 
40449f9f09b0SDouglas Anderson 			dwc_tt->usb_tt = urb->dev->tt;
40459f9f09b0SDouglas Anderson 			dwc_tt->usb_tt->hcpriv = dwc_tt;
40469f9f09b0SDouglas Anderson 		}
40479f9f09b0SDouglas Anderson 
40489f9f09b0SDouglas Anderson 		dwc_tt->refcount++;
40499f9f09b0SDouglas Anderson 	}
40509f9f09b0SDouglas Anderson 
40519f9f09b0SDouglas Anderson 	return dwc_tt;
40529f9f09b0SDouglas Anderson }
40539f9f09b0SDouglas Anderson 
40549f9f09b0SDouglas Anderson /**
40559f9f09b0SDouglas Anderson  * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
40569f9f09b0SDouglas Anderson  *
40579f9f09b0SDouglas Anderson  * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
40589f9f09b0SDouglas Anderson  * of the structure are done.
40599f9f09b0SDouglas Anderson  *
40609f9f09b0SDouglas Anderson  * It's OK to call this with NULL.
40619f9f09b0SDouglas Anderson  *
40629f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40639f9f09b0SDouglas Anderson  * @dwc_tt:    The pointer returned by dwc2_host_get_tt_info.
40649f9f09b0SDouglas Anderson  */
40659f9f09b0SDouglas Anderson void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
40669f9f09b0SDouglas Anderson {
40679f9f09b0SDouglas Anderson 	/* Model kfree and make put of NULL a no-op */
40689da51974SJohn Youn 	if (!dwc_tt)
40699f9f09b0SDouglas Anderson 		return;
40709f9f09b0SDouglas Anderson 
40719f9f09b0SDouglas Anderson 	WARN_ON(dwc_tt->refcount < 1);
40729f9f09b0SDouglas Anderson 
40739f9f09b0SDouglas Anderson 	dwc_tt->refcount--;
40749f9f09b0SDouglas Anderson 	if (!dwc_tt->refcount) {
40759f9f09b0SDouglas Anderson 		dwc_tt->usb_tt->hcpriv = NULL;
40769f9f09b0SDouglas Anderson 		kfree(dwc_tt);
40779f9f09b0SDouglas Anderson 	}
40789f9f09b0SDouglas Anderson }
40799f9f09b0SDouglas Anderson 
4080197ba5f4SPaul Zimmerman int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4081197ba5f4SPaul Zimmerman {
4082197ba5f4SPaul Zimmerman 	struct urb *urb = context;
4083197ba5f4SPaul Zimmerman 
4084197ba5f4SPaul Zimmerman 	return urb->dev->speed;
4085197ba5f4SPaul Zimmerman }
4086197ba5f4SPaul Zimmerman 
4087197ba5f4SPaul Zimmerman static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4088197ba5f4SPaul Zimmerman 					struct urb *urb)
4089197ba5f4SPaul Zimmerman {
4090197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4091197ba5f4SPaul Zimmerman 
4092197ba5f4SPaul Zimmerman 	if (urb->interval)
4093197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated += bw / urb->interval;
4094197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4095197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs++;
4096197ba5f4SPaul Zimmerman 	else
4097197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs++;
4098197ba5f4SPaul Zimmerman }
4099197ba5f4SPaul Zimmerman 
4100197ba5f4SPaul Zimmerman static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4101197ba5f4SPaul Zimmerman 				    struct urb *urb)
4102197ba5f4SPaul Zimmerman {
4103197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4104197ba5f4SPaul Zimmerman 
4105197ba5f4SPaul Zimmerman 	if (urb->interval)
4106197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated -= bw / urb->interval;
4107197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4108197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs--;
4109197ba5f4SPaul Zimmerman 	else
4110197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs--;
4111197ba5f4SPaul Zimmerman }
4112197ba5f4SPaul Zimmerman 
4113197ba5f4SPaul Zimmerman /*
4114197ba5f4SPaul Zimmerman  * Sets the final status of an URB and returns it to the upper layer. Any
4115197ba5f4SPaul Zimmerman  * required cleanup of the URB is performed.
4116197ba5f4SPaul Zimmerman  *
4117197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
4118197ba5f4SPaul Zimmerman  */
4119197ba5f4SPaul Zimmerman void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4120197ba5f4SPaul Zimmerman 			int status)
4121197ba5f4SPaul Zimmerman {
4122197ba5f4SPaul Zimmerman 	struct urb *urb;
4123197ba5f4SPaul Zimmerman 	int i;
4124197ba5f4SPaul Zimmerman 
4125197ba5f4SPaul Zimmerman 	if (!qtd) {
4126197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4127197ba5f4SPaul Zimmerman 		return;
4128197ba5f4SPaul Zimmerman 	}
4129197ba5f4SPaul Zimmerman 
4130197ba5f4SPaul Zimmerman 	if (!qtd->urb) {
4131197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4132197ba5f4SPaul Zimmerman 		return;
4133197ba5f4SPaul Zimmerman 	}
4134197ba5f4SPaul Zimmerman 
4135197ba5f4SPaul Zimmerman 	urb = qtd->urb->priv;
4136197ba5f4SPaul Zimmerman 	if (!urb) {
4137197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4138197ba5f4SPaul Zimmerman 		return;
4139197ba5f4SPaul Zimmerman 	}
4140197ba5f4SPaul Zimmerman 
4141197ba5f4SPaul Zimmerman 	urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
4142197ba5f4SPaul Zimmerman 
4143197ba5f4SPaul Zimmerman 	if (dbg_urb(urb))
4144197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
4145197ba5f4SPaul Zimmerman 			 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4146197ba5f4SPaul Zimmerman 			 __func__, urb, usb_pipedevice(urb->pipe),
4147197ba5f4SPaul Zimmerman 			 usb_pipeendpoint(urb->pipe),
4148197ba5f4SPaul Zimmerman 			 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4149197ba5f4SPaul Zimmerman 			 urb->actual_length);
4150197ba5f4SPaul Zimmerman 
4151197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4152b258e426SMinas Harutyunyan 		if (!hsotg->params.dma_desc_enable)
4153b258e426SMinas Harutyunyan 			urb->start_frame = qtd->qh->start_active_frame;
4154197ba5f4SPaul Zimmerman 		urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
4155197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; ++i) {
4156197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].actual_length =
4157197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_actual_length(
4158197ba5f4SPaul Zimmerman 						qtd->urb, i);
4159197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].status =
4160197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
4161197ba5f4SPaul Zimmerman 		}
4162197ba5f4SPaul Zimmerman 	}
4163197ba5f4SPaul Zimmerman 
4164fe9b1773SGregory Herrero 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4165fe9b1773SGregory Herrero 		for (i = 0; i < urb->number_of_packets; i++)
4166fe9b1773SGregory Herrero 			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4167fe9b1773SGregory Herrero 				 i, urb->iso_frame_desc[i].status);
4168fe9b1773SGregory Herrero 	}
4169fe9b1773SGregory Herrero 
4170197ba5f4SPaul Zimmerman 	urb->status = status;
4171197ba5f4SPaul Zimmerman 	if (!status) {
4172197ba5f4SPaul Zimmerman 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4173197ba5f4SPaul Zimmerman 		    urb->actual_length < urb->transfer_buffer_length)
4174197ba5f4SPaul Zimmerman 			urb->status = -EREMOTEIO;
4175197ba5f4SPaul Zimmerman 	}
4176197ba5f4SPaul Zimmerman 
4177197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4178197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4179197ba5f4SPaul Zimmerman 		struct usb_host_endpoint *ep = urb->ep;
4180197ba5f4SPaul Zimmerman 
4181197ba5f4SPaul Zimmerman 		if (ep)
4182197ba5f4SPaul Zimmerman 			dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4183197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4184197ba5f4SPaul Zimmerman 					urb);
4185197ba5f4SPaul Zimmerman 	}
4186197ba5f4SPaul Zimmerman 
4187197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
4188197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4189197ba5f4SPaul Zimmerman 	kfree(qtd->urb);
4190197ba5f4SPaul Zimmerman 	qtd->urb = NULL;
4191197ba5f4SPaul Zimmerman 
4192197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
4193197ba5f4SPaul Zimmerman }
4194197ba5f4SPaul Zimmerman 
4195197ba5f4SPaul Zimmerman /*
4196197ba5f4SPaul Zimmerman  * Work queue function for starting the HCD when A-Cable is connected
4197197ba5f4SPaul Zimmerman  */
4198197ba5f4SPaul Zimmerman static void dwc2_hcd_start_func(struct work_struct *work)
4199197ba5f4SPaul Zimmerman {
4200197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4201197ba5f4SPaul Zimmerman 						start_work.work);
4202197ba5f4SPaul Zimmerman 
4203197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4204197ba5f4SPaul Zimmerman 	dwc2_host_start(hsotg);
4205197ba5f4SPaul Zimmerman }
4206197ba5f4SPaul Zimmerman 
4207197ba5f4SPaul Zimmerman /*
4208197ba5f4SPaul Zimmerman  * Reset work queue function
4209197ba5f4SPaul Zimmerman  */
4210197ba5f4SPaul Zimmerman static void dwc2_hcd_reset_func(struct work_struct *work)
4211197ba5f4SPaul Zimmerman {
4212197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4213197ba5f4SPaul Zimmerman 						reset_work.work);
42144a065c7bSDouglas Anderson 	unsigned long flags;
4215197ba5f4SPaul Zimmerman 	u32 hprt0;
4216197ba5f4SPaul Zimmerman 
4217197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "USB RESET function called\n");
42184a065c7bSDouglas Anderson 
42194a065c7bSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
42204a065c7bSDouglas Anderson 
4221197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
4222197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RST;
4223f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
4224197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_reset_change = 1;
42254a065c7bSDouglas Anderson 
42264a065c7bSDouglas Anderson 	spin_unlock_irqrestore(&hsotg->lock, flags);
4227197ba5f4SPaul Zimmerman }
4228197ba5f4SPaul Zimmerman 
4229c40cf770SDouglas Anderson static void dwc2_hcd_phy_reset_func(struct work_struct *work)
4230c40cf770SDouglas Anderson {
4231c40cf770SDouglas Anderson 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4232c40cf770SDouglas Anderson 						phy_reset_work);
4233c40cf770SDouglas Anderson 	int ret;
4234c40cf770SDouglas Anderson 
4235c40cf770SDouglas Anderson 	ret = phy_reset(hsotg->phy);
4236c40cf770SDouglas Anderson 	if (ret)
4237c40cf770SDouglas Anderson 		dev_warn(hsotg->dev, "PHY reset failed\n");
4238c40cf770SDouglas Anderson }
4239c40cf770SDouglas Anderson 
4240197ba5f4SPaul Zimmerman /*
4241197ba5f4SPaul Zimmerman  * =========================================================================
4242197ba5f4SPaul Zimmerman  *  Linux HC Driver Functions
4243197ba5f4SPaul Zimmerman  * =========================================================================
4244197ba5f4SPaul Zimmerman  */
4245197ba5f4SPaul Zimmerman 
4246197ba5f4SPaul Zimmerman /*
4247197ba5f4SPaul Zimmerman  * Initializes the DWC_otg controller and its root hub and prepares it for host
4248197ba5f4SPaul Zimmerman  * mode operation. Activates the root port. Returns 0 on success and a negative
4249197ba5f4SPaul Zimmerman  * error code on failure.
4250197ba5f4SPaul Zimmerman  */
4251197ba5f4SPaul Zimmerman static int _dwc2_hcd_start(struct usb_hcd *hcd)
4252197ba5f4SPaul Zimmerman {
4253197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4254197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4255197ba5f4SPaul Zimmerman 	unsigned long flags;
4256cd7cd0e6SFabrice Gasnier 	u32 hprt0;
425741ee1ea2SFabrice Gasnier 	int ret;
4258197ba5f4SPaul Zimmerman 
4259197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4260197ba5f4SPaul Zimmerman 
4261197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
426231927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L0;
4263197ba5f4SPaul Zimmerman 	hcd->state = HC_STATE_RUNNING;
426431927b6bSGregory Herrero 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4265197ba5f4SPaul Zimmerman 
4266197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
4267197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4268197ba5f4SPaul Zimmerman 		return 0;	/* why 0 ?? */
4269197ba5f4SPaul Zimmerman 	}
4270197ba5f4SPaul Zimmerman 
4271197ba5f4SPaul Zimmerman 	dwc2_hcd_reinit(hsotg);
4272197ba5f4SPaul Zimmerman 
4273cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4274cd7cd0e6SFabrice Gasnier 	/* Has vbus power been turned on in dwc2_core_host_init ? */
4275cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR) {
4276cd7cd0e6SFabrice Gasnier 		/* Enable external vbus supply before resuming root hub */
427741ee1ea2SFabrice Gasnier 		spin_unlock_irqrestore(&hsotg->lock, flags);
427841ee1ea2SFabrice Gasnier 		ret = dwc2_vbus_supply_init(hsotg);
427941ee1ea2SFabrice Gasnier 		if (ret)
428041ee1ea2SFabrice Gasnier 			return ret;
428141ee1ea2SFabrice Gasnier 		spin_lock_irqsave(&hsotg->lock, flags);
4282cd7cd0e6SFabrice Gasnier 	}
428341ee1ea2SFabrice Gasnier 
4284197ba5f4SPaul Zimmerman 	/* Initialize and connect root hub if one is not already attached */
4285197ba5f4SPaul Zimmerman 	if (bus->root_hub) {
4286197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4287197ba5f4SPaul Zimmerman 		/* Inform the HUB driver to resume */
4288197ba5f4SPaul Zimmerman 		usb_hcd_resume_root_hub(hcd);
4289197ba5f4SPaul Zimmerman 	}
4290197ba5f4SPaul Zimmerman 
4291197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4292531ef5ebSAmelie Delaunay 
429341ee1ea2SFabrice Gasnier 	return 0;
4294197ba5f4SPaul Zimmerman }
4295197ba5f4SPaul Zimmerman 
4296197ba5f4SPaul Zimmerman /*
4297197ba5f4SPaul Zimmerman  * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4298197ba5f4SPaul Zimmerman  * stopped.
4299197ba5f4SPaul Zimmerman  */
4300197ba5f4SPaul Zimmerman static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4301197ba5f4SPaul Zimmerman {
4302197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4303197ba5f4SPaul Zimmerman 	unsigned long flags;
4304cd7cd0e6SFabrice Gasnier 	u32 hprt0;
4305197ba5f4SPaul Zimmerman 
43065bbf6ce0SGregory Herrero 	/* Turn off all host-specific interrupts */
43075bbf6ce0SGregory Herrero 	dwc2_disable_host_interrupts(hsotg);
43085bbf6ce0SGregory Herrero 
4309091473adSGregory Herrero 	/* Wait for interrupt processing to finish */
4310091473adSGregory Herrero 	synchronize_irq(hcd->irq);
4311091473adSGregory Herrero 
4312197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4313cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4314091473adSGregory Herrero 	/* Ensure hcd is disconnected */
43156a659531SDouglas Anderson 	dwc2_hcd_disconnect(hsotg, true);
4316197ba5f4SPaul Zimmerman 	dwc2_hcd_stop(hsotg);
431731927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L3;
431831927b6bSGregory Herrero 	hcd->state = HC_STATE_HALT;
431931927b6bSGregory Herrero 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4320197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4321197ba5f4SPaul Zimmerman 
4322cd7cd0e6SFabrice Gasnier 	/* keep balanced supply init/exit by checking HPRT0_PWR */
4323cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR)
4324531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
4325531ef5ebSAmelie Delaunay 
4326197ba5f4SPaul Zimmerman 	usleep_range(1000, 3000);
4327197ba5f4SPaul Zimmerman }
4328197ba5f4SPaul Zimmerman 
432999a65798SGregory Herrero static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
433099a65798SGregory Herrero {
433199a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4332a2a23d3fSGregory Herrero 	unsigned long flags;
4333a2a23d3fSGregory Herrero 	int ret = 0;
433499a65798SGregory Herrero 
4335a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4336a2a23d3fSGregory Herrero 
4337f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4338f367b72cSMeng Dongyang 		goto unlock;
4339f367b72cSMeng Dongyang 
4340a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L0)
4341a2a23d3fSGregory Herrero 		goto unlock;
4342a2a23d3fSGregory Herrero 
4343a2a23d3fSGregory Herrero 	if (!HCD_HW_ACCESSIBLE(hcd))
4344a2a23d3fSGregory Herrero 		goto unlock;
4345a2a23d3fSGregory Herrero 
4346866932e2SJohn Stultz 	if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4347866932e2SJohn Stultz 		goto unlock;
4348866932e2SJohn Stultz 
4349113f86d0SArtur Petrosyan 	if (hsotg->bus_suspended)
4350a2a23d3fSGregory Herrero 		goto skip_power_saving;
4351a2a23d3fSGregory Herrero 
4352113f86d0SArtur Petrosyan 	if (hsotg->flags.b.port_connect_status == 0)
4353113f86d0SArtur Petrosyan 		goto skip_power_saving;
4354113f86d0SArtur Petrosyan 
4355113f86d0SArtur Petrosyan 	switch (hsotg->params.power_down) {
4356113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4357113f86d0SArtur Petrosyan 		/* Enter partial_power_down */
4358113f86d0SArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
4359113f86d0SArtur Petrosyan 		if (ret)
4360113f86d0SArtur Petrosyan 			dev_err(hsotg->dev,
4361113f86d0SArtur Petrosyan 				"enter partial_power_down failed\n");
4362113f86d0SArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
4363113f86d0SArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4364113f86d0SArtur Petrosyan 		break;
4365113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4366755d0effSArtur Petrosyan 		/* Enter hibernation */
4367755d0effSArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
4368755d0effSArtur Petrosyan 		ret = dwc2_enter_hibernation(hsotg, 1);
4369755d0effSArtur Petrosyan 		if (ret)
4370755d0effSArtur Petrosyan 			dev_err(hsotg->dev, "enter hibernation failed\n");
4371755d0effSArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
4372755d0effSArtur Petrosyan 
4373755d0effSArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
4374755d0effSArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4375755d0effSArtur Petrosyan 		break;
4376113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
437750fb0c12SArtur Petrosyan 		/*
437850fb0c12SArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
437950fb0c12SArtur Petrosyan 		 * clock gating is used to save power.
438050fb0c12SArtur Petrosyan 		 */
438134146c68SDinh Nguyen 		if (!hsotg->params.no_clock_gating) {
438250fb0c12SArtur Petrosyan 			dwc2_host_enter_clock_gating(hsotg);
438350fb0c12SArtur Petrosyan 
438450fb0c12SArtur Petrosyan 			/* After entering suspend, hardware is not accessible */
438550fb0c12SArtur Petrosyan 			clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
438634146c68SDinh Nguyen 		}
438750fb0c12SArtur Petrosyan 		break;
4388113f86d0SArtur Petrosyan 	default:
4389113f86d0SArtur Petrosyan 		goto skip_power_saving;
43906f6d7059SDouglas Anderson 	}
4391113f86d0SArtur Petrosyan 
43925aa678c7SFabrice Gasnier 	spin_unlock_irqrestore(&hsotg->lock, flags);
4393531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_exit(hsotg);
43945aa678c7SFabrice Gasnier 	spin_lock_irqsave(&hsotg->lock, flags);
43956f6d7059SDouglas Anderson 
4396a2a23d3fSGregory Herrero 	/* Ask phy to be suspended */
4397a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4398a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4399a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, true);
4400a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4401a2a23d3fSGregory Herrero 	}
4402a2a23d3fSGregory Herrero 
4403a2a23d3fSGregory Herrero skip_power_saving:
440499a65798SGregory Herrero 	hsotg->lx_state = DWC2_L2;
4405a2a23d3fSGregory Herrero unlock:
4406a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4407a2a23d3fSGregory Herrero 
4408a2a23d3fSGregory Herrero 	return ret;
440999a65798SGregory Herrero }
441099a65798SGregory Herrero 
441199a65798SGregory Herrero static int _dwc2_hcd_resume(struct usb_hcd *hcd)
441299a65798SGregory Herrero {
441399a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4414a2a23d3fSGregory Herrero 	unsigned long flags;
4415c74c26f6SArtur Petrosyan 	u32 hprt0;
4416a2a23d3fSGregory Herrero 	int ret = 0;
4417a2a23d3fSGregory Herrero 
4418a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4419a2a23d3fSGregory Herrero 
4420f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4421f367b72cSMeng Dongyang 		goto unlock;
4422f367b72cSMeng Dongyang 
4423a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L2)
4424a2a23d3fSGregory Herrero 		goto unlock;
4425a2a23d3fSGregory Herrero 
4426c74c26f6SArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
4427c74c26f6SArtur Petrosyan 
4428c74c26f6SArtur Petrosyan 	/*
4429c74c26f6SArtur Petrosyan 	 * Added port connection status checking which prevents exiting from
4430c74c26f6SArtur Petrosyan 	 * Partial Power Down mode from _dwc2_hcd_resume() if not in Partial
4431c74c26f6SArtur Petrosyan 	 * Power Down mode.
4432c74c26f6SArtur Petrosyan 	 */
4433c74c26f6SArtur Petrosyan 	if (hprt0 & HPRT0_CONNSTS) {
4434a2a23d3fSGregory Herrero 		hsotg->lx_state = DWC2_L0;
4435a2a23d3fSGregory Herrero 		goto unlock;
4436a2a23d3fSGregory Herrero 	}
4437a2a23d3fSGregory Herrero 
4438c74c26f6SArtur Petrosyan 	switch (hsotg->params.power_down) {
4439c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4440c74c26f6SArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
4441c74c26f6SArtur Petrosyan 		if (ret)
4442c74c26f6SArtur Petrosyan 			dev_err(hsotg->dev,
4443c74c26f6SArtur Petrosyan 				"exit partial_power_down failed\n");
4444c74c26f6SArtur Petrosyan 		/*
4445c74c26f6SArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4446c74c26f6SArtur Petrosyan 		 * since an interrupt may rise.
4447c74c26f6SArtur Petrosyan 		 */
4448c74c26f6SArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4449c74c26f6SArtur Petrosyan 		break;
4450c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4451ae0da4fdSArtur Petrosyan 		ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
4452ae0da4fdSArtur Petrosyan 		if (ret)
4453ae0da4fdSArtur Petrosyan 			dev_err(hsotg->dev, "exit hibernation failed.\n");
4454ae0da4fdSArtur Petrosyan 
4455ae0da4fdSArtur Petrosyan 		/*
4456ae0da4fdSArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4457ae0da4fdSArtur Petrosyan 		 * since an interrupt may rise.
4458ae0da4fdSArtur Petrosyan 		 */
4459ae0da4fdSArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4460ae0da4fdSArtur Petrosyan 		break;
4461c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
4462ef5e0eecSArtur Petrosyan 		/*
4463ef5e0eecSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
4464ef5e0eecSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
4465ef5e0eecSArtur Petrosyan 		 */
4466ef5e0eecSArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
4467ef5e0eecSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
4468ef5e0eecSArtur Petrosyan 
4469ef5e0eecSArtur Petrosyan 		/*
4470ef5e0eecSArtur Petrosyan 		 * Initialize the Core for Host mode, as after system resume
4471ef5e0eecSArtur Petrosyan 		 * the global interrupts are disabled.
4472ef5e0eecSArtur Petrosyan 		 */
4473ef5e0eecSArtur Petrosyan 		dwc2_core_init(hsotg, false);
4474ef5e0eecSArtur Petrosyan 		dwc2_enable_global_interrupts(hsotg);
4475ef5e0eecSArtur Petrosyan 		dwc2_hcd_reinit(hsotg);
4476ef5e0eecSArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
4477ef5e0eecSArtur Petrosyan 
4478ef5e0eecSArtur Petrosyan 		/*
4479ef5e0eecSArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4480ef5e0eecSArtur Petrosyan 		 * since an interrupt may rise.
4481ef5e0eecSArtur Petrosyan 		 */
4482ef5e0eecSArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4483ef5e0eecSArtur Petrosyan 		break;
4484c74c26f6SArtur Petrosyan 	default:
4485c74c26f6SArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
4486c74c26f6SArtur Petrosyan 		goto unlock;
4487c74c26f6SArtur Petrosyan 	}
4488c74c26f6SArtur Petrosyan 
4489c74c26f6SArtur Petrosyan 	/* Change Root port status, as port status change occurred after resume.*/
4490c74c26f6SArtur Petrosyan 	hsotg->flags.b.port_suspend_change = 1;
4491c74c26f6SArtur Petrosyan 
4492a2a23d3fSGregory Herrero 	/*
4493a2a23d3fSGregory Herrero 	 * Enable power if not already done.
4494a2a23d3fSGregory Herrero 	 * This must not be spinlocked since duration
4495a2a23d3fSGregory Herrero 	 * of this call is unknown.
4496a2a23d3fSGregory Herrero 	 */
4497a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4498a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4499a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, false);
4500a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4501a2a23d3fSGregory Herrero 	}
4502a2a23d3fSGregory Herrero 
4503c74c26f6SArtur Petrosyan 	/* Enable external vbus supply after resuming the port. */
4504a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4505531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_init(hsotg);
4506531ef5ebSAmelie Delaunay 
45075634e016SGregory Herrero 	/* Wait for controller to correctly update D+/D- level */
45085634e016SGregory Herrero 	usleep_range(3000, 5000);
4509c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
45105634e016SGregory Herrero 
4511a2a23d3fSGregory Herrero 	/*
4512a2a23d3fSGregory Herrero 	 * Clear Port Enable and Port Status changes.
4513a2a23d3fSGregory Herrero 	 * Enable Port Power.
4514a2a23d3fSGregory Herrero 	 */
4515f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
4516f25c42b8SGevorg Sahakyan 			HPRT0_ENACHG, HPRT0);
4517a2a23d3fSGregory Herrero 
4518c74c26f6SArtur Petrosyan 	/* Wait for controller to detect Port Connect */
4519c74c26f6SArtur Petrosyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
4520c74c26f6SArtur Petrosyan 	usleep_range(5000, 7000);
4521c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
4522a2a23d3fSGregory Herrero unlock:
4523a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4524a2a23d3fSGregory Herrero 
4525a2a23d3fSGregory Herrero 	return ret;
452699a65798SGregory Herrero }
452799a65798SGregory Herrero 
4528197ba5f4SPaul Zimmerman /* Returns the current frame number */
4529197ba5f4SPaul Zimmerman static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4530197ba5f4SPaul Zimmerman {
4531197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4532197ba5f4SPaul Zimmerman 
4533197ba5f4SPaul Zimmerman 	return dwc2_hcd_get_frame_number(hsotg);
4534197ba5f4SPaul Zimmerman }
4535197ba5f4SPaul Zimmerman 
4536197ba5f4SPaul Zimmerman static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4537197ba5f4SPaul Zimmerman 			       char *fn_name)
4538197ba5f4SPaul Zimmerman {
4539197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
4540197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4541efe357f4SNicholas Mc Guire 	char *pipetype = NULL;
4542efe357f4SNicholas Mc Guire 	char *speed = NULL;
4543197ba5f4SPaul Zimmerman 
4544197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4545197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Device address: %d\n",
4546197ba5f4SPaul Zimmerman 		 usb_pipedevice(urb->pipe));
4547197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint: %d, %s\n",
4548197ba5f4SPaul Zimmerman 		 usb_pipeendpoint(urb->pipe),
4549197ba5f4SPaul Zimmerman 		 usb_pipein(urb->pipe) ? "IN" : "OUT");
4550197ba5f4SPaul Zimmerman 
4551197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4552197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4553197ba5f4SPaul Zimmerman 		pipetype = "CONTROL";
4554197ba5f4SPaul Zimmerman 		break;
4555197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4556197ba5f4SPaul Zimmerman 		pipetype = "BULK";
4557197ba5f4SPaul Zimmerman 		break;
4558197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4559197ba5f4SPaul Zimmerman 		pipetype = "INTERRUPT";
4560197ba5f4SPaul Zimmerman 		break;
4561197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4562197ba5f4SPaul Zimmerman 		pipetype = "ISOCHRONOUS";
4563197ba5f4SPaul Zimmerman 		break;
4564197ba5f4SPaul Zimmerman 	}
4565197ba5f4SPaul Zimmerman 
4566197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint type: %s %s (%s)\n", pipetype,
4567197ba5f4SPaul Zimmerman 		 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4568197ba5f4SPaul Zimmerman 		 "IN" : "OUT");
4569197ba5f4SPaul Zimmerman 
4570197ba5f4SPaul Zimmerman 	switch (urb->dev->speed) {
4571197ba5f4SPaul Zimmerman 	case USB_SPEED_HIGH:
4572197ba5f4SPaul Zimmerman 		speed = "HIGH";
4573197ba5f4SPaul Zimmerman 		break;
4574197ba5f4SPaul Zimmerman 	case USB_SPEED_FULL:
4575197ba5f4SPaul Zimmerman 		speed = "FULL";
4576197ba5f4SPaul Zimmerman 		break;
4577197ba5f4SPaul Zimmerman 	case USB_SPEED_LOW:
4578197ba5f4SPaul Zimmerman 		speed = "LOW";
4579197ba5f4SPaul Zimmerman 		break;
4580197ba5f4SPaul Zimmerman 	default:
4581197ba5f4SPaul Zimmerman 		speed = "UNKNOWN";
4582197ba5f4SPaul Zimmerman 		break;
4583197ba5f4SPaul Zimmerman 	}
4584197ba5f4SPaul Zimmerman 
4585197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Speed: %s\n", speed);
4586babd1839SDouglas Anderson 	dev_vdbg(hsotg->dev, "  Max packet size: %d (%d mult)\n",
4587babd1839SDouglas Anderson 		 usb_endpoint_maxp(&urb->ep->desc),
4588babd1839SDouglas Anderson 		 usb_endpoint_maxp_mult(&urb->ep->desc));
4589babd1839SDouglas Anderson 
4590197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Data buffer length: %d\n",
4591197ba5f4SPaul Zimmerman 		 urb->transfer_buffer_length);
4592197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
4593197ba5f4SPaul Zimmerman 		 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4594197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
4595197ba5f4SPaul Zimmerman 		 urb->setup_packet, (unsigned long)urb->setup_dma);
4596197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Interval: %d\n", urb->interval);
4597197ba5f4SPaul Zimmerman 
4598197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4599197ba5f4SPaul Zimmerman 		int i;
4600197ba5f4SPaul Zimmerman 
4601197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; i++) {
4602197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  ISO Desc %d:\n", i);
4603197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "    offset: %d, length %d\n",
4604197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].offset,
4605197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].length);
4606197ba5f4SPaul Zimmerman 		}
4607197ba5f4SPaul Zimmerman 	}
4608197ba5f4SPaul Zimmerman #endif
4609197ba5f4SPaul Zimmerman }
4610197ba5f4SPaul Zimmerman 
4611197ba5f4SPaul Zimmerman /*
4612197ba5f4SPaul Zimmerman  * Starts processing a USB transfer request specified by a USB Request Block
4613197ba5f4SPaul Zimmerman  * (URB). mem_flags indicates the type of memory allocation to use while
4614197ba5f4SPaul Zimmerman  * processing this URB.
4615197ba5f4SPaul Zimmerman  */
4616197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4617197ba5f4SPaul Zimmerman 				 gfp_t mem_flags)
4618197ba5f4SPaul Zimmerman {
4619197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4620197ba5f4SPaul Zimmerman 	struct usb_host_endpoint *ep = urb->ep;
4621197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *dwc2_urb;
4622197ba5f4SPaul Zimmerman 	int i;
4623197ba5f4SPaul Zimmerman 	int retval;
4624197ba5f4SPaul Zimmerman 	int alloc_bandwidth = 0;
4625197ba5f4SPaul Zimmerman 	u8 ep_type = 0;
4626197ba5f4SPaul Zimmerman 	u32 tflags = 0;
4627197ba5f4SPaul Zimmerman 	void *buf;
4628197ba5f4SPaul Zimmerman 	unsigned long flags;
4629b58e6ceeSMian Yousaf Kaukab 	struct dwc2_qh *qh;
4630b58e6ceeSMian Yousaf Kaukab 	bool qh_allocated = false;
4631b5a468a6SMian Yousaf Kaukab 	struct dwc2_qtd *qtd;
4632c3595df7SArtur Petrosyan 	struct dwc2_gregs_backup *gr;
4633c3595df7SArtur Petrosyan 
4634c3595df7SArtur Petrosyan 	gr = &hsotg->gr_backup;
4635197ba5f4SPaul Zimmerman 
4636197ba5f4SPaul Zimmerman 	if (dbg_urb(urb)) {
4637197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4638197ba5f4SPaul Zimmerman 		dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4639197ba5f4SPaul Zimmerman 	}
4640197ba5f4SPaul Zimmerman 
4641c3595df7SArtur Petrosyan 	if (hsotg->hibernated) {
4642c3595df7SArtur Petrosyan 		if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
4643c3595df7SArtur Petrosyan 			retval = dwc2_exit_hibernation(hsotg, 0, 0, 1);
4644c3595df7SArtur Petrosyan 		else
4645c3595df7SArtur Petrosyan 			retval = dwc2_exit_hibernation(hsotg, 0, 0, 0);
4646c3595df7SArtur Petrosyan 
4647c3595df7SArtur Petrosyan 		if (retval)
4648c3595df7SArtur Petrosyan 			dev_err(hsotg->dev,
4649c3595df7SArtur Petrosyan 				"exit hibernation failed.\n");
4650c3595df7SArtur Petrosyan 	}
4651c3595df7SArtur Petrosyan 
465275f43ac3SArtur Petrosyan 	if (hsotg->in_ppd) {
465375f43ac3SArtur Petrosyan 		retval = dwc2_exit_partial_power_down(hsotg, 0, true);
465475f43ac3SArtur Petrosyan 		if (retval)
465575f43ac3SArtur Petrosyan 			dev_err(hsotg->dev,
465675f43ac3SArtur Petrosyan 				"exit partial_power_down failed\n");
465775f43ac3SArtur Petrosyan 	}
465875f43ac3SArtur Petrosyan 
465916c729f9SArtur Petrosyan 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
4660*31f42da3SMinas Harutyunyan 	    hsotg->bus_suspended && !hsotg->params.no_clock_gating) {
466116c729f9SArtur Petrosyan 		if (dwc2_is_device_mode(hsotg))
466216c729f9SArtur Petrosyan 			dwc2_gadget_exit_clock_gating(hsotg, 0);
466316c729f9SArtur Petrosyan 		else
466416c729f9SArtur Petrosyan 			dwc2_host_exit_clock_gating(hsotg, 0);
466516c729f9SArtur Petrosyan 	}
466616c729f9SArtur Petrosyan 
46679da51974SJohn Youn 	if (!ep)
4668197ba5f4SPaul Zimmerman 		return -EINVAL;
4669197ba5f4SPaul Zimmerman 
4670197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4671197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4672197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
4673197ba5f4SPaul Zimmerman 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4674197ba5f4SPaul Zimmerman 			alloc_bandwidth = 1;
4675197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4676197ba5f4SPaul Zimmerman 	}
4677197ba5f4SPaul Zimmerman 
4678197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4679197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4680197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_CONTROL;
4681197ba5f4SPaul Zimmerman 		break;
4682197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4683197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_ISOC;
4684197ba5f4SPaul Zimmerman 		break;
4685197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4686197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_BULK;
4687197ba5f4SPaul Zimmerman 		break;
4688197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4689197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_INT;
4690197ba5f4SPaul Zimmerman 		break;
4691197ba5f4SPaul Zimmerman 	}
4692197ba5f4SPaul Zimmerman 
4693197ba5f4SPaul Zimmerman 	dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4694197ba5f4SPaul Zimmerman 				      mem_flags);
4695197ba5f4SPaul Zimmerman 	if (!dwc2_urb)
4696197ba5f4SPaul Zimmerman 		return -ENOMEM;
4697197ba5f4SPaul Zimmerman 
4698197ba5f4SPaul Zimmerman 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4699197ba5f4SPaul Zimmerman 				  usb_pipeendpoint(urb->pipe), ep_type,
4700197ba5f4SPaul Zimmerman 				  usb_pipein(urb->pipe),
4701babd1839SDouglas Anderson 				  usb_endpoint_maxp(&ep->desc),
4702babd1839SDouglas Anderson 				  usb_endpoint_maxp_mult(&ep->desc));
4703197ba5f4SPaul Zimmerman 
4704197ba5f4SPaul Zimmerman 	buf = urb->transfer_buffer;
4705197ba5f4SPaul Zimmerman 
4706edfbcb32SChristoph Hellwig 	if (hcd_uses_dma(hcd)) {
4707197ba5f4SPaul Zimmerman 		if (!buf && (urb->transfer_dma & 3)) {
4708197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
4709197ba5f4SPaul Zimmerman 				"%s: unaligned transfer with no transfer_buffer",
4710197ba5f4SPaul Zimmerman 				__func__);
4711197ba5f4SPaul Zimmerman 			retval = -EINVAL;
471233ad261aSGregory Herrero 			goto fail0;
4713197ba5f4SPaul Zimmerman 		}
4714197ba5f4SPaul Zimmerman 	}
4715197ba5f4SPaul Zimmerman 
4716197ba5f4SPaul Zimmerman 	if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4717197ba5f4SPaul Zimmerman 		tflags |= URB_GIVEBACK_ASAP;
4718197ba5f4SPaul Zimmerman 	if (urb->transfer_flags & URB_ZERO_PACKET)
4719197ba5f4SPaul Zimmerman 		tflags |= URB_SEND_ZERO_PACKET;
4720197ba5f4SPaul Zimmerman 
4721197ba5f4SPaul Zimmerman 	dwc2_urb->priv = urb;
4722197ba5f4SPaul Zimmerman 	dwc2_urb->buf = buf;
4723197ba5f4SPaul Zimmerman 	dwc2_urb->dma = urb->transfer_dma;
4724197ba5f4SPaul Zimmerman 	dwc2_urb->length = urb->transfer_buffer_length;
4725197ba5f4SPaul Zimmerman 	dwc2_urb->setup_packet = urb->setup_packet;
4726197ba5f4SPaul Zimmerman 	dwc2_urb->setup_dma = urb->setup_dma;
4727197ba5f4SPaul Zimmerman 	dwc2_urb->flags = tflags;
4728197ba5f4SPaul Zimmerman 	dwc2_urb->interval = urb->interval;
4729197ba5f4SPaul Zimmerman 	dwc2_urb->status = -EINPROGRESS;
4730197ba5f4SPaul Zimmerman 
4731197ba5f4SPaul Zimmerman 	for (i = 0; i < urb->number_of_packets; ++i)
4732197ba5f4SPaul Zimmerman 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4733197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].offset,
4734197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].length);
4735197ba5f4SPaul Zimmerman 
4736197ba5f4SPaul Zimmerman 	urb->hcpriv = dwc2_urb;
4737b58e6ceeSMian Yousaf Kaukab 	qh = (struct dwc2_qh *)ep->hcpriv;
4738b58e6ceeSMian Yousaf Kaukab 	/* Create QH for the endpoint if it doesn't exist */
4739b58e6ceeSMian Yousaf Kaukab 	if (!qh) {
4740b58e6ceeSMian Yousaf Kaukab 		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4741b58e6ceeSMian Yousaf Kaukab 		if (!qh) {
4742b58e6ceeSMian Yousaf Kaukab 			retval = -ENOMEM;
4743b58e6ceeSMian Yousaf Kaukab 			goto fail0;
4744b58e6ceeSMian Yousaf Kaukab 		}
4745b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = qh;
4746b58e6ceeSMian Yousaf Kaukab 		qh_allocated = true;
4747b58e6ceeSMian Yousaf Kaukab 	}
4748197ba5f4SPaul Zimmerman 
4749b5a468a6SMian Yousaf Kaukab 	qtd = kzalloc(sizeof(*qtd), mem_flags);
4750b5a468a6SMian Yousaf Kaukab 	if (!qtd) {
4751b5a468a6SMian Yousaf Kaukab 		retval = -ENOMEM;
4752b5a468a6SMian Yousaf Kaukab 		goto fail1;
4753b5a468a6SMian Yousaf Kaukab 	}
4754b5a468a6SMian Yousaf Kaukab 
4755197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4756197ba5f4SPaul Zimmerman 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
4757197ba5f4SPaul Zimmerman 	if (retval)
4758197ba5f4SPaul Zimmerman 		goto fail2;
4759197ba5f4SPaul Zimmerman 
4760b5a468a6SMian Yousaf Kaukab 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4761b5a468a6SMian Yousaf Kaukab 	if (retval)
4762b5a468a6SMian Yousaf Kaukab 		goto fail3;
4763b5a468a6SMian Yousaf Kaukab 
4764197ba5f4SPaul Zimmerman 	if (alloc_bandwidth) {
4765197ba5f4SPaul Zimmerman 		dwc2_allocate_bus_bandwidth(hcd,
4766197ba5f4SPaul Zimmerman 				dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4767197ba5f4SPaul Zimmerman 				urb);
4768197ba5f4SPaul Zimmerman 	}
4769197ba5f4SPaul Zimmerman 
477033ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
477133ad261aSGregory Herrero 
4772197ba5f4SPaul Zimmerman 	return 0;
4773197ba5f4SPaul Zimmerman 
4774b5a468a6SMian Yousaf Kaukab fail3:
4775197ba5f4SPaul Zimmerman 	dwc2_urb->priv = NULL;
4776197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
477716e80218SDouglas Anderson 	if (qh_allocated && qh->channel && qh->channel->qh == qh)
477816e80218SDouglas Anderson 		qh->channel->qh = NULL;
4779b5a468a6SMian Yousaf Kaukab fail2:
4780197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4781ef307bc6SJia-Ju Bai 	spin_unlock_irqrestore(&hsotg->lock, flags);
4782b5a468a6SMian Yousaf Kaukab 	kfree(qtd);
4783b5a468a6SMian Yousaf Kaukab fail1:
4784b58e6ceeSMian Yousaf Kaukab 	if (qh_allocated) {
4785b58e6ceeSMian Yousaf Kaukab 		struct dwc2_qtd *qtd2, *qtd2_tmp;
4786b58e6ceeSMian Yousaf Kaukab 
4787b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = NULL;
4788b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_unlink(hsotg, qh);
4789b58e6ceeSMian Yousaf Kaukab 		/* Free each QTD in the QH's QTD list */
4790b58e6ceeSMian Yousaf Kaukab 		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
4791b58e6ceeSMian Yousaf Kaukab 					 qtd_list_entry)
4792b58e6ceeSMian Yousaf Kaukab 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4793b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_free(hsotg, qh);
4794b58e6ceeSMian Yousaf Kaukab 	}
479533ad261aSGregory Herrero fail0:
4796197ba5f4SPaul Zimmerman 	kfree(dwc2_urb);
4797197ba5f4SPaul Zimmerman 
4798197ba5f4SPaul Zimmerman 	return retval;
4799197ba5f4SPaul Zimmerman }
4800197ba5f4SPaul Zimmerman 
4801197ba5f4SPaul Zimmerman /*
4802197ba5f4SPaul Zimmerman  * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4803197ba5f4SPaul Zimmerman  */
4804197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4805197ba5f4SPaul Zimmerman 				 int status)
4806197ba5f4SPaul Zimmerman {
4807197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4808197ba5f4SPaul Zimmerman 	int rc;
4809197ba5f4SPaul Zimmerman 	unsigned long flags;
4810197ba5f4SPaul Zimmerman 
4811197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4812197ba5f4SPaul Zimmerman 	dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4813197ba5f4SPaul Zimmerman 
4814197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4815197ba5f4SPaul Zimmerman 
4816197ba5f4SPaul Zimmerman 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4817197ba5f4SPaul Zimmerman 	if (rc)
4818197ba5f4SPaul Zimmerman 		goto out;
4819197ba5f4SPaul Zimmerman 
4820197ba5f4SPaul Zimmerman 	if (!urb->hcpriv) {
4821197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4822197ba5f4SPaul Zimmerman 		goto out;
4823197ba5f4SPaul Zimmerman 	}
4824197ba5f4SPaul Zimmerman 
4825197ba5f4SPaul Zimmerman 	rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4826197ba5f4SPaul Zimmerman 
4827197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
4828197ba5f4SPaul Zimmerman 
4829197ba5f4SPaul Zimmerman 	kfree(urb->hcpriv);
4830197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4831197ba5f4SPaul Zimmerman 
4832197ba5f4SPaul Zimmerman 	/* Higher layer software sets URB status */
4833197ba5f4SPaul Zimmerman 	spin_unlock(&hsotg->lock);
4834197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(hcd, urb, status);
4835197ba5f4SPaul Zimmerman 	spin_lock(&hsotg->lock);
4836197ba5f4SPaul Zimmerman 
4837197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4838197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  urb->status = %d\n", urb->status);
4839197ba5f4SPaul Zimmerman out:
4840197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4841197ba5f4SPaul Zimmerman 
4842197ba5f4SPaul Zimmerman 	return rc;
4843197ba5f4SPaul Zimmerman }
4844197ba5f4SPaul Zimmerman 
4845197ba5f4SPaul Zimmerman /*
4846197ba5f4SPaul Zimmerman  * Frees resources in the DWC_otg controller related to a given endpoint. Also
4847197ba5f4SPaul Zimmerman  * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4848197ba5f4SPaul Zimmerman  * must already be dequeued.
4849197ba5f4SPaul Zimmerman  */
4850197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4851197ba5f4SPaul Zimmerman 				       struct usb_host_endpoint *ep)
4852197ba5f4SPaul Zimmerman {
4853197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4854197ba5f4SPaul Zimmerman 
4855197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4856197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4857197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress, ep->hcpriv);
4858197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4859197ba5f4SPaul Zimmerman }
4860197ba5f4SPaul Zimmerman 
4861197ba5f4SPaul Zimmerman /*
4862197ba5f4SPaul Zimmerman  * Resets endpoint specific parameter values, in current version used to reset
4863197ba5f4SPaul Zimmerman  * the data toggle (as a WA). This function can be called from usb_clear_halt
4864197ba5f4SPaul Zimmerman  * routine.
4865197ba5f4SPaul Zimmerman  */
4866197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4867197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep)
4868197ba5f4SPaul Zimmerman {
4869197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4870197ba5f4SPaul Zimmerman 	unsigned long flags;
4871197ba5f4SPaul Zimmerman 
4872197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4873197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4874197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress);
4875197ba5f4SPaul Zimmerman 
4876197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4877197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_reset(hsotg, ep);
4878197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4879197ba5f4SPaul Zimmerman }
4880197ba5f4SPaul Zimmerman 
4881197ba5f4SPaul Zimmerman /*
4882197ba5f4SPaul Zimmerman  * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4883197ba5f4SPaul Zimmerman  * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4884197ba5f4SPaul Zimmerman  * interrupt.
4885197ba5f4SPaul Zimmerman  *
4886197ba5f4SPaul Zimmerman  * This function is called by the USB core when an interrupt occurs
4887197ba5f4SPaul Zimmerman  */
4888197ba5f4SPaul Zimmerman static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4889197ba5f4SPaul Zimmerman {
4890197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4891197ba5f4SPaul Zimmerman 
4892197ba5f4SPaul Zimmerman 	return dwc2_handle_hcd_intr(hsotg);
4893197ba5f4SPaul Zimmerman }
4894197ba5f4SPaul Zimmerman 
4895197ba5f4SPaul Zimmerman /*
4896197ba5f4SPaul Zimmerman  * Creates Status Change bitmap for the root hub and root port. The bitmap is
4897197ba5f4SPaul Zimmerman  * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4898197ba5f4SPaul Zimmerman  * is the status change indicator for the single root port. Returns 1 if either
4899197ba5f4SPaul Zimmerman  * change indicator is 1, otherwise returns 0.
4900197ba5f4SPaul Zimmerman  */
4901197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4902197ba5f4SPaul Zimmerman {
4903197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4904197ba5f4SPaul Zimmerman 
4905197ba5f4SPaul Zimmerman 	buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4906197ba5f4SPaul Zimmerman 	return buf[0] != 0;
4907197ba5f4SPaul Zimmerman }
4908197ba5f4SPaul Zimmerman 
4909197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
4910197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4911197ba5f4SPaul Zimmerman 				 u16 windex, char *buf, u16 wlength)
4912197ba5f4SPaul Zimmerman {
4913197ba5f4SPaul Zimmerman 	int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4914197ba5f4SPaul Zimmerman 					  wvalue, windex, buf, wlength);
4915197ba5f4SPaul Zimmerman 	return retval;
4916197ba5f4SPaul Zimmerman }
4917197ba5f4SPaul Zimmerman 
4918197ba5f4SPaul Zimmerman /* Handles hub TT buffer clear completions */
4919197ba5f4SPaul Zimmerman static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4920197ba5f4SPaul Zimmerman 					       struct usb_host_endpoint *ep)
4921197ba5f4SPaul Zimmerman {
4922197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4923197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
4924197ba5f4SPaul Zimmerman 	unsigned long flags;
4925197ba5f4SPaul Zimmerman 
4926197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
4927197ba5f4SPaul Zimmerman 	if (!qh)
4928197ba5f4SPaul Zimmerman 		return;
4929197ba5f4SPaul Zimmerman 
4930197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4931197ba5f4SPaul Zimmerman 	qh->tt_buffer_dirty = 0;
4932197ba5f4SPaul Zimmerman 
4933197ba5f4SPaul Zimmerman 	if (hsotg->flags.b.port_connect_status)
4934197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4935197ba5f4SPaul Zimmerman 
4936197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4937197ba5f4SPaul Zimmerman }
4938197ba5f4SPaul Zimmerman 
4939ca8b0332SChen Yu /*
4940ca8b0332SChen Yu  * HPRT0_SPD_HIGH_SPEED: high speed
4941ca8b0332SChen Yu  * HPRT0_SPD_FULL_SPEED: full speed
4942ca8b0332SChen Yu  */
4943ca8b0332SChen Yu static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4944ca8b0332SChen Yu {
4945ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4946ca8b0332SChen Yu 
4947ca8b0332SChen Yu 	if (hsotg->params.speed == speed)
4948ca8b0332SChen Yu 		return;
4949ca8b0332SChen Yu 
4950ca8b0332SChen Yu 	hsotg->params.speed = speed;
4951ca8b0332SChen Yu 	queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4952ca8b0332SChen Yu }
4953ca8b0332SChen Yu 
4954ca8b0332SChen Yu static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4955ca8b0332SChen Yu {
4956ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4957ca8b0332SChen Yu 
4958ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4959ca8b0332SChen Yu 		return;
4960ca8b0332SChen Yu 
4961ca8b0332SChen Yu 	/*
4962ca8b0332SChen Yu 	 * On removal, set speed to default high-speed.
4963ca8b0332SChen Yu 	 */
4964ca8b0332SChen Yu 	if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4965ca8b0332SChen Yu 	    udev->parent->speed < USB_SPEED_HIGH) {
4966ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to default high-speed\n");
4967ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4968ca8b0332SChen Yu 	}
4969ca8b0332SChen Yu }
4970ca8b0332SChen Yu 
4971ca8b0332SChen Yu static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4972ca8b0332SChen Yu {
4973ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4974ca8b0332SChen Yu 
4975ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4976ca8b0332SChen Yu 		return 0;
4977ca8b0332SChen Yu 
4978ca8b0332SChen Yu 	if (udev->speed == USB_SPEED_HIGH) {
4979ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to high-speed\n");
4980ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4981ca8b0332SChen Yu 	} else if ((udev->speed == USB_SPEED_FULL ||
4982ca8b0332SChen Yu 				udev->speed == USB_SPEED_LOW)) {
4983ca8b0332SChen Yu 		/*
4984ca8b0332SChen Yu 		 * Change speed setting to full-speed if there's
4985ca8b0332SChen Yu 		 * a full-speed or low-speed device plugged in.
4986ca8b0332SChen Yu 		 */
4987ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to full-speed\n");
4988ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4989ca8b0332SChen Yu 	}
4990ca8b0332SChen Yu 
4991ca8b0332SChen Yu 	return 0;
4992ca8b0332SChen Yu }
4993ca8b0332SChen Yu 
4994197ba5f4SPaul Zimmerman static struct hc_driver dwc2_hc_driver = {
4995197ba5f4SPaul Zimmerman 	.description = "dwc2_hsotg",
4996197ba5f4SPaul Zimmerman 	.product_desc = "DWC OTG Controller",
4997197ba5f4SPaul Zimmerman 	.hcd_priv_size = sizeof(struct wrapper_priv_data),
4998197ba5f4SPaul Zimmerman 
4999197ba5f4SPaul Zimmerman 	.irq = _dwc2_hcd_irq,
50008add17cfSDouglas Anderson 	.flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
5001197ba5f4SPaul Zimmerman 
5002197ba5f4SPaul Zimmerman 	.start = _dwc2_hcd_start,
5003197ba5f4SPaul Zimmerman 	.stop = _dwc2_hcd_stop,
5004197ba5f4SPaul Zimmerman 	.urb_enqueue = _dwc2_hcd_urb_enqueue,
5005197ba5f4SPaul Zimmerman 	.urb_dequeue = _dwc2_hcd_urb_dequeue,
5006197ba5f4SPaul Zimmerman 	.endpoint_disable = _dwc2_hcd_endpoint_disable,
5007197ba5f4SPaul Zimmerman 	.endpoint_reset = _dwc2_hcd_endpoint_reset,
5008197ba5f4SPaul Zimmerman 	.get_frame_number = _dwc2_hcd_get_frame_number,
5009197ba5f4SPaul Zimmerman 
5010197ba5f4SPaul Zimmerman 	.hub_status_data = _dwc2_hcd_hub_status_data,
5011197ba5f4SPaul Zimmerman 	.hub_control = _dwc2_hcd_hub_control,
5012197ba5f4SPaul Zimmerman 	.clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
501399a65798SGregory Herrero 
501499a65798SGregory Herrero 	.bus_suspend = _dwc2_hcd_suspend,
501599a65798SGregory Herrero 	.bus_resume = _dwc2_hcd_resume,
50163bc04e28SDouglas Anderson 
50173bc04e28SDouglas Anderson 	.map_urb_for_dma	= dwc2_map_urb_for_dma,
50183bc04e28SDouglas Anderson 	.unmap_urb_for_dma	= dwc2_unmap_urb_for_dma,
5019197ba5f4SPaul Zimmerman };
5020197ba5f4SPaul Zimmerman 
5021197ba5f4SPaul Zimmerman /*
5022197ba5f4SPaul Zimmerman  * Frees secondary storage associated with the dwc2_hsotg structure contained
5023197ba5f4SPaul Zimmerman  * in the struct usb_hcd field
5024197ba5f4SPaul Zimmerman  */
5025197ba5f4SPaul Zimmerman static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
5026197ba5f4SPaul Zimmerman {
5027197ba5f4SPaul Zimmerman 	u32 ahbcfg;
5028197ba5f4SPaul Zimmerman 	u32 dctl;
5029197ba5f4SPaul Zimmerman 	int i;
5030197ba5f4SPaul Zimmerman 
5031197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
5032197ba5f4SPaul Zimmerman 
5033197ba5f4SPaul Zimmerman 	/* Free memory for QH/QTD lists */
5034197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
503538d2b5fbSDouglas Anderson 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
5036197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
5037197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
5038197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
5039197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
5040197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
5041197ba5f4SPaul Zimmerman 
5042197ba5f4SPaul Zimmerman 	/* Free memory for the host channels */
5043197ba5f4SPaul Zimmerman 	for (i = 0; i < MAX_EPS_CHANNELS; i++) {
5044197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
5045197ba5f4SPaul Zimmerman 
50469da51974SJohn Youn 		if (chan) {
5047197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
5048197ba5f4SPaul Zimmerman 				i, chan);
5049197ba5f4SPaul Zimmerman 			hsotg->hc_ptr_array[i] = NULL;
5050197ba5f4SPaul Zimmerman 			kfree(chan);
5051197ba5f4SPaul Zimmerman 		}
5052197ba5f4SPaul Zimmerman 	}
5053197ba5f4SPaul Zimmerman 
505495832c00SJohn Youn 	if (hsotg->params.host_dma) {
5055197ba5f4SPaul Zimmerman 		if (hsotg->status_buf) {
5056197ba5f4SPaul Zimmerman 			dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
5057197ba5f4SPaul Zimmerman 					  hsotg->status_buf,
5058197ba5f4SPaul Zimmerman 					  hsotg->status_buf_dma);
5059197ba5f4SPaul Zimmerman 			hsotg->status_buf = NULL;
5060197ba5f4SPaul Zimmerman 		}
5061197ba5f4SPaul Zimmerman 	} else {
5062197ba5f4SPaul Zimmerman 		kfree(hsotg->status_buf);
5063197ba5f4SPaul Zimmerman 		hsotg->status_buf = NULL;
5064197ba5f4SPaul Zimmerman 	}
5065197ba5f4SPaul Zimmerman 
5066f25c42b8SGevorg Sahakyan 	ahbcfg = dwc2_readl(hsotg, GAHBCFG);
5067197ba5f4SPaul Zimmerman 
5068197ba5f4SPaul Zimmerman 	/* Disable all interrupts */
5069197ba5f4SPaul Zimmerman 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
5070f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
5071f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
5072197ba5f4SPaul Zimmerman 
5073197ba5f4SPaul Zimmerman 	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
5074f25c42b8SGevorg Sahakyan 		dctl = dwc2_readl(hsotg, DCTL);
5075197ba5f4SPaul Zimmerman 		dctl |= DCTL_SFTDISCON;
5076f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dctl, DCTL);
5077197ba5f4SPaul Zimmerman 	}
5078197ba5f4SPaul Zimmerman 
5079197ba5f4SPaul Zimmerman 	if (hsotg->wq_otg) {
5080197ba5f4SPaul Zimmerman 		if (!cancel_work_sync(&hsotg->wf_otg))
5081197ba5f4SPaul Zimmerman 			flush_workqueue(hsotg->wq_otg);
5082197ba5f4SPaul Zimmerman 		destroy_workqueue(hsotg->wq_otg);
5083197ba5f4SPaul Zimmerman 	}
5084197ba5f4SPaul Zimmerman 
5085c40cf770SDouglas Anderson 	cancel_work_sync(&hsotg->phy_reset_work);
5086c40cf770SDouglas Anderson 
5087197ba5f4SPaul Zimmerman 	del_timer(&hsotg->wkp_timer);
5088197ba5f4SPaul Zimmerman }
5089197ba5f4SPaul Zimmerman 
5090197ba5f4SPaul Zimmerman static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5091197ba5f4SPaul Zimmerman {
5092197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
5093197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
5094197ba5f4SPaul Zimmerman 
5095197ba5f4SPaul Zimmerman 	dwc2_hcd_free(hsotg);
5096197ba5f4SPaul Zimmerman }
5097197ba5f4SPaul Zimmerman 
5098197ba5f4SPaul Zimmerman /*
5099197ba5f4SPaul Zimmerman  * Initializes the HCD. This function allocates memory for and initializes the
5100197ba5f4SPaul Zimmerman  * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5101197ba5f4SPaul Zimmerman  * USB bus with the core and calls the hc_driver->start() function. It returns
5102197ba5f4SPaul Zimmerman  * a negative error on failure.
5103197ba5f4SPaul Zimmerman  */
51044fe160d5SHeiner Kallweit int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5105197ba5f4SPaul Zimmerman {
5106348becdcSHeiner Kallweit 	struct platform_device *pdev = to_platform_device(hsotg->dev);
5107348becdcSHeiner Kallweit 	struct resource *res;
5108197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5109197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
5110197ba5f4SPaul Zimmerman 	u32 hcfg;
5111197ba5f4SPaul Zimmerman 	int i, num_channels;
5112197ba5f4SPaul Zimmerman 	int retval;
5113197ba5f4SPaul Zimmerman 
5114f5500eccSDinh Nguyen 	if (usb_disabled())
5115f5500eccSDinh Nguyen 		return -ENODEV;
5116f5500eccSDinh Nguyen 
5117197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
5118197ba5f4SPaul Zimmerman 
5119197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5120197ba5f4SPaul Zimmerman 
5121f25c42b8SGevorg Sahakyan 	hcfg = dwc2_readl(hsotg, HCFG);
5122197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5123197ba5f4SPaul Zimmerman 
5124197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
51256396bb22SKees Cook 	hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
51266396bb22SKees Cook 					 sizeof(*hsotg->frame_num_array),
51276396bb22SKees Cook 					 GFP_KERNEL);
5128197ba5f4SPaul Zimmerman 	if (!hsotg->frame_num_array)
5129197ba5f4SPaul Zimmerman 		goto error1;
51306396bb22SKees Cook 	hsotg->last_frame_num_array =
51316396bb22SKees Cook 		kcalloc(FRAME_NUM_ARRAY_SIZE,
51326396bb22SKees Cook 			sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
5133197ba5f4SPaul Zimmerman 	if (!hsotg->last_frame_num_array)
5134197ba5f4SPaul Zimmerman 		goto error1;
5135197ba5f4SPaul Zimmerman #endif
5136483bb254SDouglas Anderson 	hsotg->last_frame_num = HFNUM_MAX_FRNUM;
5137197ba5f4SPaul Zimmerman 
5138197ba5f4SPaul Zimmerman 	/* Check if the bus driver or platform code has setup a dma_mask */
513995832c00SJohn Youn 	if (hsotg->params.host_dma &&
51409da51974SJohn Youn 	    !hsotg->dev->dma_mask) {
5141197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
5142197ba5f4SPaul Zimmerman 			 "dma_mask not set, disabling DMA\n");
5143fdb09b3eSNicholas Mc Guire 		hsotg->params.host_dma = false;
514495832c00SJohn Youn 		hsotg->params.dma_desc_enable = false;
5145197ba5f4SPaul Zimmerman 	}
5146197ba5f4SPaul Zimmerman 
5147197ba5f4SPaul Zimmerman 	/* Set device flags indicating whether the HCD supports DMA */
514895832c00SJohn Youn 	if (hsotg->params.host_dma) {
5149197ba5f4SPaul Zimmerman 		if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5150197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set DMA mask\n");
5151197ba5f4SPaul Zimmerman 		if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5152197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
5153197ba5f4SPaul Zimmerman 	}
5154197ba5f4SPaul Zimmerman 
5155ca8b0332SChen Yu 	if (hsotg->params.change_speed_quirk) {
5156ca8b0332SChen Yu 		dwc2_hc_driver.free_dev = dwc2_free_dev;
5157ca8b0332SChen Yu 		dwc2_hc_driver.reset_device = dwc2_reset_device;
5158ca8b0332SChen Yu 	}
5159ca8b0332SChen Yu 
51607b81cb6bSChristoph Hellwig 	if (hsotg->params.host_dma)
51617b81cb6bSChristoph Hellwig 		dwc2_hc_driver.flags |= HCD_DMA;
51627b81cb6bSChristoph Hellwig 
5163197ba5f4SPaul Zimmerman 	hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5164197ba5f4SPaul Zimmerman 	if (!hcd)
5165197ba5f4SPaul Zimmerman 		goto error1;
5166197ba5f4SPaul Zimmerman 
5167197ba5f4SPaul Zimmerman 	hcd->has_tt = 1;
5168197ba5f4SPaul Zimmerman 
5169348becdcSHeiner Kallweit 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5170856e6e8eSYang Yingliang 	if (!res) {
5171856e6e8eSYang Yingliang 		retval = -EINVAL;
51723755278fSMiaoqian Lin 		goto error2;
5173856e6e8eSYang Yingliang 	}
5174348becdcSHeiner Kallweit 	hcd->rsrc_start = res->start;
5175348becdcSHeiner Kallweit 	hcd->rsrc_len = resource_size(res);
5176348becdcSHeiner Kallweit 
5177197ba5f4SPaul Zimmerman 	((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
5178197ba5f4SPaul Zimmerman 	hsotg->priv = hcd;
5179197ba5f4SPaul Zimmerman 
5180197ba5f4SPaul Zimmerman 	/*
5181197ba5f4SPaul Zimmerman 	 * Disable the global interrupt until all the interrupt handlers are
5182197ba5f4SPaul Zimmerman 	 * installed
5183197ba5f4SPaul Zimmerman 	 */
5184197ba5f4SPaul Zimmerman 	dwc2_disable_global_interrupts(hsotg);
5185197ba5f4SPaul Zimmerman 
5186197ba5f4SPaul Zimmerman 	/* Initialize the DWC_otg core, and select the Phy type */
51870fe239bcSDouglas Anderson 	retval = dwc2_core_init(hsotg, true);
5188197ba5f4SPaul Zimmerman 	if (retval)
5189197ba5f4SPaul Zimmerman 		goto error2;
5190197ba5f4SPaul Zimmerman 
5191197ba5f4SPaul Zimmerman 	/* Create new workqueue and init work */
5192197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5193ec7b1268SBhaktipriya Shridhar 	hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
5194197ba5f4SPaul Zimmerman 	if (!hsotg->wq_otg) {
5195197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Failed to create workqueue\n");
5196197ba5f4SPaul Zimmerman 		goto error2;
5197197ba5f4SPaul Zimmerman 	}
5198197ba5f4SPaul Zimmerman 	INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5199197ba5f4SPaul Zimmerman 
5200e99e88a9SKees Cook 	timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
5201197ba5f4SPaul Zimmerman 
5202197ba5f4SPaul Zimmerman 	/* Initialize the non-periodic schedule */
5203197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
520438d2b5fbSDouglas Anderson 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
5205197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5206197ba5f4SPaul Zimmerman 
5207197ba5f4SPaul Zimmerman 	/* Initialize the periodic schedule */
5208197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5209197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5210197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5211197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5212197ba5f4SPaul Zimmerman 
5213c9c8ac01SDouglas Anderson 	INIT_LIST_HEAD(&hsotg->split_order);
5214c9c8ac01SDouglas Anderson 
5215197ba5f4SPaul Zimmerman 	/*
5216197ba5f4SPaul Zimmerman 	 * Create a host channel descriptor for each host channel implemented
5217197ba5f4SPaul Zimmerman 	 * in the controller. Initialize the channel descriptor array.
5218197ba5f4SPaul Zimmerman 	 */
5219197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->free_hc_list);
5220bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
5221197ba5f4SPaul Zimmerman 	memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5222197ba5f4SPaul Zimmerman 
5223197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
5224197ba5f4SPaul Zimmerman 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
52259da51974SJohn Youn 		if (!channel)
5226197ba5f4SPaul Zimmerman 			goto error3;
5227197ba5f4SPaul Zimmerman 		channel->hc_num = i;
5228c9c8ac01SDouglas Anderson 		INIT_LIST_HEAD(&channel->split_order_list_entry);
5229197ba5f4SPaul Zimmerman 		hsotg->hc_ptr_array[i] = channel;
5230197ba5f4SPaul Zimmerman 	}
5231197ba5f4SPaul Zimmerman 
5232c40cf770SDouglas Anderson 	/* Initialize work */
5233197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5234197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5235c40cf770SDouglas Anderson 	INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
5236197ba5f4SPaul Zimmerman 
5237197ba5f4SPaul Zimmerman 	/*
5238197ba5f4SPaul Zimmerman 	 * Allocate space for storing data on status transactions. Normally no
5239197ba5f4SPaul Zimmerman 	 * data is sent, but this space acts as a bit bucket. This must be
5240197ba5f4SPaul Zimmerman 	 * done after usb_add_hcd since that function allocates the DMA buffer
5241197ba5f4SPaul Zimmerman 	 * pool.
5242197ba5f4SPaul Zimmerman 	 */
524395832c00SJohn Youn 	if (hsotg->params.host_dma)
5244197ba5f4SPaul Zimmerman 		hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5245197ba5f4SPaul Zimmerman 					DWC2_HCD_STATUS_BUF_SIZE,
5246197ba5f4SPaul Zimmerman 					&hsotg->status_buf_dma, GFP_KERNEL);
5247197ba5f4SPaul Zimmerman 	else
5248197ba5f4SPaul Zimmerman 		hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5249197ba5f4SPaul Zimmerman 					  GFP_KERNEL);
5250197ba5f4SPaul Zimmerman 
5251197ba5f4SPaul Zimmerman 	if (!hsotg->status_buf)
5252197ba5f4SPaul Zimmerman 		goto error3;
5253197ba5f4SPaul Zimmerman 
52543b5fcc9aSGregory Herrero 	/*
52553b5fcc9aSGregory Herrero 	 * Create kmem caches to handle descriptor buffers in descriptor
52563b5fcc9aSGregory Herrero 	 * DMA mode.
52573b5fcc9aSGregory Herrero 	 * Alignment must be set to 512 bytes.
52583b5fcc9aSGregory Herrero 	 */
5259bea8e86cSJohn Youn 	if (hsotg->params.dma_desc_enable ||
5260bea8e86cSJohn Youn 	    hsotg->params.dma_desc_fs_enable) {
52613b5fcc9aSGregory Herrero 		hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
5262ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52633b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
52643b5fcc9aSGregory Herrero 				NULL);
52653b5fcc9aSGregory Herrero 		if (!hsotg->desc_gen_cache) {
52663b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52673b5fcc9aSGregory Herrero 				"unable to create dwc2 generic desc cache\n");
52683b5fcc9aSGregory Herrero 
52693b5fcc9aSGregory Herrero 			/*
52703b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52713b5fcc9aSGregory Herrero 			 * usable.
52723b5fcc9aSGregory Herrero 			 */
527395832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
527495832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
52753b5fcc9aSGregory Herrero 		}
52763b5fcc9aSGregory Herrero 
52773b5fcc9aSGregory Herrero 		hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
5278ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52793b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
52803b5fcc9aSGregory Herrero 		if (!hsotg->desc_hsisoc_cache) {
52813b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52823b5fcc9aSGregory Herrero 				"unable to create dwc2 hs isoc desc cache\n");
52833b5fcc9aSGregory Herrero 
52843b5fcc9aSGregory Herrero 			kmem_cache_destroy(hsotg->desc_gen_cache);
52853b5fcc9aSGregory Herrero 
52863b5fcc9aSGregory Herrero 			/*
52873b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52883b5fcc9aSGregory Herrero 			 * usable.
52893b5fcc9aSGregory Herrero 			 */
529095832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
529195832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
52923b5fcc9aSGregory Herrero 		}
52933b5fcc9aSGregory Herrero 	}
52943b5fcc9aSGregory Herrero 
5295af424a41SWilliam Wu 	if (hsotg->params.host_dma) {
5296af424a41SWilliam Wu 		/*
5297af424a41SWilliam Wu 		 * Create kmem caches to handle non-aligned buffer
5298af424a41SWilliam Wu 		 * in Buffer DMA mode.
5299af424a41SWilliam Wu 		 */
5300af424a41SWilliam Wu 		hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
5301af424a41SWilliam Wu 						DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
5302af424a41SWilliam Wu 						SLAB_CACHE_DMA, NULL);
5303af424a41SWilliam Wu 		if (!hsotg->unaligned_cache)
5304af424a41SWilliam Wu 			dev_err(hsotg->dev,
5305af424a41SWilliam Wu 				"unable to create dwc2 unaligned cache\n");
5306af424a41SWilliam Wu 	}
5307af424a41SWilliam Wu 
5308197ba5f4SPaul Zimmerman 	hsotg->otg_port = 1;
5309197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
5310197ba5f4SPaul Zimmerman 	hsotg->frame_list_dma = 0;
5311197ba5f4SPaul Zimmerman 	hsotg->periodic_qh_count = 0;
5312197ba5f4SPaul Zimmerman 
5313197ba5f4SPaul Zimmerman 	/* Initiate lx_state to L3 disconnected state */
5314197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L3;
5315197ba5f4SPaul Zimmerman 
5316197ba5f4SPaul Zimmerman 	hcd->self.otg_port = hsotg->otg_port;
5317197ba5f4SPaul Zimmerman 
5318197ba5f4SPaul Zimmerman 	/* Don't support SG list at this point */
5319197ba5f4SPaul Zimmerman 	hcd->self.sg_tablesize = 0;
5320197ba5f4SPaul Zimmerman 
53212c8845feSAmelie Delaunay 	hcd->tpl_support = of_usb_host_tpl_support(hsotg->dev->of_node);
53222c8845feSAmelie Delaunay 
53239df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
53249df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, &hcd->self);
53259df4ceacSMian Yousaf Kaukab 
5326197ba5f4SPaul Zimmerman 	/*
5327197ba5f4SPaul Zimmerman 	 * Finish generic HCD initialization and start the HCD. This function
5328197ba5f4SPaul Zimmerman 	 * allocates the DMA buffer pool, registers the USB bus, requests the
5329197ba5f4SPaul Zimmerman 	 * IRQ line, and calls hcd_start method.
5330197ba5f4SPaul Zimmerman 	 */
53314fe160d5SHeiner Kallweit 	retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
5332197ba5f4SPaul Zimmerman 	if (retval < 0)
53333b5fcc9aSGregory Herrero 		goto error4;
5334197ba5f4SPaul Zimmerman 
5335ec513b16SLinus Torvalds 	device_wakeup_enable(hcd->self.controller);
5336ec513b16SLinus Torvalds 
5337197ba5f4SPaul Zimmerman 	dwc2_hcd_dump_state(hsotg);
5338197ba5f4SPaul Zimmerman 
5339197ba5f4SPaul Zimmerman 	dwc2_enable_global_interrupts(hsotg);
5340197ba5f4SPaul Zimmerman 
5341197ba5f4SPaul Zimmerman 	return 0;
5342197ba5f4SPaul Zimmerman 
53433b5fcc9aSGregory Herrero error4:
5344af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53453b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5346af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
5347197ba5f4SPaul Zimmerman error3:
5348197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5349197ba5f4SPaul Zimmerman error2:
5350197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5351197ba5f4SPaul Zimmerman error1:
5352197ba5f4SPaul Zimmerman 
5353197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5354197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5355197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5356197ba5f4SPaul Zimmerman #endif
5357197ba5f4SPaul Zimmerman 
5358197ba5f4SPaul Zimmerman 	dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
5359197ba5f4SPaul Zimmerman 	return retval;
5360197ba5f4SPaul Zimmerman }
5361197ba5f4SPaul Zimmerman 
5362197ba5f4SPaul Zimmerman /*
5363197ba5f4SPaul Zimmerman  * Removes the HCD.
5364197ba5f4SPaul Zimmerman  * Frees memory and resources associated with the HCD and deregisters the bus.
5365197ba5f4SPaul Zimmerman  */
5366197ba5f4SPaul Zimmerman void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
5367197ba5f4SPaul Zimmerman {
5368197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5369197ba5f4SPaul Zimmerman 
5370197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
5371197ba5f4SPaul Zimmerman 
5372197ba5f4SPaul Zimmerman 	hcd = dwc2_hsotg_to_hcd(hsotg);
5373197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
5374197ba5f4SPaul Zimmerman 
5375197ba5f4SPaul Zimmerman 	if (!hcd) {
5376197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
5377197ba5f4SPaul Zimmerman 			__func__);
5378197ba5f4SPaul Zimmerman 		return;
5379197ba5f4SPaul Zimmerman 	}
5380197ba5f4SPaul Zimmerman 
53819df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
53829df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, NULL);
53839df4ceacSMian Yousaf Kaukab 
5384197ba5f4SPaul Zimmerman 	usb_remove_hcd(hcd);
5385197ba5f4SPaul Zimmerman 	hsotg->priv = NULL;
53863b5fcc9aSGregory Herrero 
5387af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53883b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5389af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
53903b5fcc9aSGregory Herrero 
5391197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5392197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5393197ba5f4SPaul Zimmerman 
5394197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5395197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5396197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5397197ba5f4SPaul Zimmerman #endif
5398197ba5f4SPaul Zimmerman }
539958e52ff6SJohn Youn 
540058e52ff6SJohn Youn /**
540158e52ff6SJohn Youn  * dwc2_backup_host_registers() - Backup controller host registers.
540258e52ff6SJohn Youn  * When suspending usb bus, registers needs to be backuped
540358e52ff6SJohn Youn  * if controller power is disabled once suspended.
540458e52ff6SJohn Youn  *
540558e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
540658e52ff6SJohn Youn  */
540758e52ff6SJohn Youn int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
540858e52ff6SJohn Youn {
540958e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
541058e52ff6SJohn Youn 	int i;
541158e52ff6SJohn Youn 
541258e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
541358e52ff6SJohn Youn 
541458e52ff6SJohn Youn 	/* Backup Host regs */
541558e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
5416f25c42b8SGevorg Sahakyan 	hr->hcfg = dwc2_readl(hsotg, HCFG);
54173c7b9856SMinas Harutyunyan 	hr->hflbaddr = dwc2_readl(hsotg, HFLBADDR);
5418f25c42b8SGevorg Sahakyan 	hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
54193c7b9856SMinas Harutyunyan 	for (i = 0; i < hsotg->params.host_channels; ++i) {
54203c7b9856SMinas Harutyunyan 		hr->hcchar[i] = dwc2_readl(hsotg, HCCHAR(i));
54213c7b9856SMinas Harutyunyan 		hr->hcsplt[i] = dwc2_readl(hsotg, HCSPLT(i));
5422f25c42b8SGevorg Sahakyan 		hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
54233c7b9856SMinas Harutyunyan 		hr->hctsiz[i] = dwc2_readl(hsotg, HCTSIZ(i));
54243c7b9856SMinas Harutyunyan 		hr->hcidma[i] = dwc2_readl(hsotg, HCDMA(i));
54253c7b9856SMinas Harutyunyan 		hr->hcidmab[i] = dwc2_readl(hsotg, HCDMAB(i));
54263c7b9856SMinas Harutyunyan 	}
542758e52ff6SJohn Youn 
542858e52ff6SJohn Youn 	hr->hprt0 = dwc2_read_hprt0(hsotg);
5429f25c42b8SGevorg Sahakyan 	hr->hfir = dwc2_readl(hsotg, HFIR);
5430f25c42b8SGevorg Sahakyan 	hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
543158e52ff6SJohn Youn 	hr->valid = true;
543258e52ff6SJohn Youn 
543358e52ff6SJohn Youn 	return 0;
543458e52ff6SJohn Youn }
543558e52ff6SJohn Youn 
543658e52ff6SJohn Youn /**
543758e52ff6SJohn Youn  * dwc2_restore_host_registers() - Restore controller host registers.
543858e52ff6SJohn Youn  * When resuming usb bus, device registers needs to be restored
543958e52ff6SJohn Youn  * if controller power were disabled.
544058e52ff6SJohn Youn  *
544158e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
544258e52ff6SJohn Youn  */
544358e52ff6SJohn Youn int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
544458e52ff6SJohn Youn {
544558e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
544658e52ff6SJohn Youn 	int i;
544758e52ff6SJohn Youn 
544858e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
544958e52ff6SJohn Youn 
545058e52ff6SJohn Youn 	/* Restore host regs */
545158e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
545258e52ff6SJohn Youn 	if (!hr->valid) {
545358e52ff6SJohn Youn 		dev_err(hsotg->dev, "%s: no host registers to restore\n",
545458e52ff6SJohn Youn 			__func__);
545558e52ff6SJohn Youn 		return -EINVAL;
545658e52ff6SJohn Youn 	}
545758e52ff6SJohn Youn 	hr->valid = false;
545858e52ff6SJohn Youn 
5459f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
54603c7b9856SMinas Harutyunyan 	dwc2_writel(hsotg, hr->hflbaddr, HFLBADDR);
5461f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
546258e52ff6SJohn Youn 
54633c7b9856SMinas Harutyunyan 	for (i = 0; i < hsotg->params.host_channels; ++i) {
54643c7b9856SMinas Harutyunyan 		dwc2_writel(hsotg, hr->hcchar[i], HCCHAR(i));
54653c7b9856SMinas Harutyunyan 		dwc2_writel(hsotg, hr->hcsplt[i], HCSPLT(i));
5466f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
54673c7b9856SMinas Harutyunyan 		dwc2_writel(hsotg, hr->hctsiz[i], HCTSIZ(i));
54683c7b9856SMinas Harutyunyan 		dwc2_writel(hsotg, hr->hcidma[i], HCDMA(i));
54693c7b9856SMinas Harutyunyan 		dwc2_writel(hsotg, hr->hcidmab[i], HCDMAB(i));
54703c7b9856SMinas Harutyunyan 	}
547158e52ff6SJohn Youn 
5472f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hprt0, HPRT0);
5473f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hfir, HFIR);
5474f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
547558e52ff6SJohn Youn 	hsotg->frame_number = 0;
547658e52ff6SJohn Youn 
547758e52ff6SJohn Youn 	return 0;
547858e52ff6SJohn Youn }
5479c5c403dcSVardan Mikayelyan 
5480c5c403dcSVardan Mikayelyan /**
5481c5c403dcSVardan Mikayelyan  * dwc2_host_enter_hibernation() - Put controller in Hibernation.
5482c5c403dcSVardan Mikayelyan  *
5483c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5484c5c403dcSVardan Mikayelyan  */
5485c5c403dcSVardan Mikayelyan int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
5486c5c403dcSVardan Mikayelyan {
5487c5c403dcSVardan Mikayelyan 	unsigned long flags;
5488c5c403dcSVardan Mikayelyan 	int ret = 0;
5489c5c403dcSVardan Mikayelyan 	u32 hprt0;
5490c5c403dcSVardan Mikayelyan 	u32 pcgcctl;
5491c5c403dcSVardan Mikayelyan 	u32 gusbcfg;
5492c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5493c5c403dcSVardan Mikayelyan 
5494c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
5495c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_global_registers(hsotg);
5496c5c403dcSVardan Mikayelyan 	if (ret) {
5497c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5498c5c403dcSVardan Mikayelyan 			__func__);
5499c5c403dcSVardan Mikayelyan 		return ret;
5500c5c403dcSVardan Mikayelyan 	}
5501c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_host_registers(hsotg);
5502c5c403dcSVardan Mikayelyan 	if (ret) {
5503c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5504c5c403dcSVardan Mikayelyan 			__func__);
5505c5c403dcSVardan Mikayelyan 		return ret;
5506c5c403dcSVardan Mikayelyan 	}
5507c5c403dcSVardan Mikayelyan 
5508c5c403dcSVardan Mikayelyan 	/* Enter USB Suspend Mode */
5509f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5510c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_SUSP;
5511c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5512f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5513c5c403dcSVardan Mikayelyan 
5514c5c403dcSVardan Mikayelyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
55155e3bbae8SArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
551607b8dc55SColin Ian King 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
5517c5c403dcSVardan Mikayelyan 
5518c5c403dcSVardan Mikayelyan 	/*
5519c5c403dcSVardan Mikayelyan 	 * We need to disable interrupts to prevent servicing of any IRQ
5520c5c403dcSVardan Mikayelyan 	 * during going to hibernation
5521c5c403dcSVardan Mikayelyan 	 */
5522c5c403dcSVardan Mikayelyan 	spin_lock_irqsave(&hsotg->lock, flags);
5523c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L2;
5524c5c403dcSVardan Mikayelyan 
5525f25c42b8SGevorg Sahakyan 	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
5526c5c403dcSVardan Mikayelyan 	if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
5527c5c403dcSVardan Mikayelyan 		/* ULPI interface */
5528c5c403dcSVardan Mikayelyan 		/* Suspend the Phy Clock */
5529f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5530c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5531f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5532c5c403dcSVardan Mikayelyan 		udelay(10);
5533c5c403dcSVardan Mikayelyan 
5534f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5535c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5536f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5537c5c403dcSVardan Mikayelyan 		udelay(10);
5538c5c403dcSVardan Mikayelyan 	} else {
5539c5c403dcSVardan Mikayelyan 		/* UTMI+ Interface */
5540f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5541c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5542f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5543c5c403dcSVardan Mikayelyan 		udelay(10);
5544c5c403dcSVardan Mikayelyan 
5545f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5546c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5547f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5548c5c403dcSVardan Mikayelyan 		udelay(10);
5549c5c403dcSVardan Mikayelyan 	}
5550c5c403dcSVardan Mikayelyan 
5551c5c403dcSVardan Mikayelyan 	/* Enable interrupts from wake up logic */
5552f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5553c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PMUINTSEL;
5554f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5555c5c403dcSVardan Mikayelyan 	udelay(10);
5556c5c403dcSVardan Mikayelyan 
5557c5c403dcSVardan Mikayelyan 	/* Unmask host mode interrupts in GPWRDN */
5558f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5559c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_DISCONN_DET_MSK;
5560c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5561c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5562f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5563c5c403dcSVardan Mikayelyan 	udelay(10);
5564c5c403dcSVardan Mikayelyan 
5565c5c403dcSVardan Mikayelyan 	/* Enable Power Down Clamp */
5566f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5567c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNCLMP;
5568f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5569c5c403dcSVardan Mikayelyan 	udelay(10);
5570c5c403dcSVardan Mikayelyan 
5571c5c403dcSVardan Mikayelyan 	/* Switch off VDD */
5572f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5573c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNSWTCH;
5574f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5575c5c403dcSVardan Mikayelyan 
5576c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 1;
5577c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 1;
5578c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation completed\n");
5579c5c403dcSVardan Mikayelyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
5580c5c403dcSVardan Mikayelyan 	return ret;
5581c5c403dcSVardan Mikayelyan }
5582c5c403dcSVardan Mikayelyan 
5583c5c403dcSVardan Mikayelyan /*
5584c5c403dcSVardan Mikayelyan  * dwc2_host_exit_hibernation()
5585c5c403dcSVardan Mikayelyan  *
5586c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5587c5c403dcSVardan Mikayelyan  * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5588c5c403dcSVardan Mikayelyan  * @param reset: indicates whether resume is initiated by Reset.
5589c5c403dcSVardan Mikayelyan  *
5590c5c403dcSVardan Mikayelyan  * Return: non-zero if failed to enter to hibernation.
5591c5c403dcSVardan Mikayelyan  *
5592c5c403dcSVardan Mikayelyan  * This function is for exiting from Host mode hibernation by
5593c5c403dcSVardan Mikayelyan  * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
5594c5c403dcSVardan Mikayelyan  */
5595c5c403dcSVardan Mikayelyan int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
5596c5c403dcSVardan Mikayelyan 			       int reset)
5597c5c403dcSVardan Mikayelyan {
5598c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5599c5c403dcSVardan Mikayelyan 	u32 hprt0;
5600c5c403dcSVardan Mikayelyan 	int ret = 0;
5601c5c403dcSVardan Mikayelyan 	struct dwc2_gregs_backup *gr;
5602c5c403dcSVardan Mikayelyan 	struct dwc2_hregs_backup *hr;
5603c5c403dcSVardan Mikayelyan 
5604c5c403dcSVardan Mikayelyan 	gr = &hsotg->gr_backup;
5605c5c403dcSVardan Mikayelyan 	hr = &hsotg->hr_backup;
5606c5c403dcSVardan Mikayelyan 
5607c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev,
5608c5c403dcSVardan Mikayelyan 		"%s: called with rem_wakeup = %d reset = %d\n",
5609c5c403dcSVardan Mikayelyan 		__func__, rem_wakeup, reset);
5610c5c403dcSVardan Mikayelyan 
5611c5c403dcSVardan Mikayelyan 	dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
5612c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5613c5c403dcSVardan Mikayelyan 
5614c5c403dcSVardan Mikayelyan 	/*
5615c5c403dcSVardan Mikayelyan 	 * This step is not described in functional spec but if not wait for
5616c5c403dcSVardan Mikayelyan 	 * this delay, mismatch interrupts occurred because just after restore
5617c5c403dcSVardan Mikayelyan 	 * core is in Device mode(gintsts.curmode == 0)
5618c5c403dcSVardan Mikayelyan 	 */
5619c5c403dcSVardan Mikayelyan 	mdelay(100);
5620c5c403dcSVardan Mikayelyan 
5621c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5622f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5623c5c403dcSVardan Mikayelyan 
5624c5c403dcSVardan Mikayelyan 	/* De-assert Restore */
5625f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5626c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_RESTORE;
5627f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5628c5c403dcSVardan Mikayelyan 	udelay(10);
5629c5c403dcSVardan Mikayelyan 
5630c5c403dcSVardan Mikayelyan 	/* Restore GUSBCFG, HCFG */
5631f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5632f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5633c5c403dcSVardan Mikayelyan 
5634c5c403dcSVardan Mikayelyan 	/* De-assert Wakeup Logic */
5635bae2bc73SMinas Harutyunyan 	if (!(rem_wakeup && hsotg->hw_params.snpsid >= DWC2_CORE_REV_4_30a)) {
5636f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5637c5c403dcSVardan Mikayelyan 		gpwrdn &= ~GPWRDN_PMUACTV;
5638f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5639c5c403dcSVardan Mikayelyan 		udelay(10);
5640bae2bc73SMinas Harutyunyan 	}
5641c5c403dcSVardan Mikayelyan 
5642c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5643c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5644c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5645c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5646f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5647c5c403dcSVardan Mikayelyan 
5648c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5649c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5650c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5651c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5652c5c403dcSVardan Mikayelyan 
5653c5c403dcSVardan Mikayelyan 	if (reset) {
5654c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RST;
5655f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5656c5c403dcSVardan Mikayelyan 
5657c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5658c5c403dcSVardan Mikayelyan 		mdelay(60);
5659c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RST;
5660f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5661c5c403dcSVardan Mikayelyan 	} else {
5662c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RES;
5663f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5664c5c403dcSVardan Mikayelyan 
5665bae2bc73SMinas Harutyunyan 		/* De-assert Wakeup Logic */
5666bae2bc73SMinas Harutyunyan 		if ((rem_wakeup && hsotg->hw_params.snpsid >= DWC2_CORE_REV_4_30a)) {
5667bae2bc73SMinas Harutyunyan 			gpwrdn = dwc2_readl(hsotg, GPWRDN);
5668bae2bc73SMinas Harutyunyan 			gpwrdn &= ~GPWRDN_PMUACTV;
5669bae2bc73SMinas Harutyunyan 			dwc2_writel(hsotg, gpwrdn, GPWRDN);
5670bae2bc73SMinas Harutyunyan 			udelay(10);
5671bae2bc73SMinas Harutyunyan 		}
5672c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5673c5c403dcSVardan Mikayelyan 		mdelay(100);
5674c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RES;
5675f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5676c5c403dcSVardan Mikayelyan 	}
5677c5c403dcSVardan Mikayelyan 	/* Clear all interrupt status */
5678f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5679c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_CONNDET;
5680c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_ENACHG;
5681c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5682f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5683c5c403dcSVardan Mikayelyan 
5684f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5685c5c403dcSVardan Mikayelyan 
5686c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5687f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5688c5c403dcSVardan Mikayelyan 
5689c5c403dcSVardan Mikayelyan 	/* Restore global registers */
5690c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_global_registers(hsotg);
5691c5c403dcSVardan Mikayelyan 	if (ret) {
5692c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore registers\n",
5693c5c403dcSVardan Mikayelyan 			__func__);
5694c5c403dcSVardan Mikayelyan 		return ret;
5695c5c403dcSVardan Mikayelyan 	}
5696c5c403dcSVardan Mikayelyan 
5697c5c403dcSVardan Mikayelyan 	/* Restore host registers */
5698c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_host_registers(hsotg);
5699c5c403dcSVardan Mikayelyan 	if (ret) {
5700c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5701c5c403dcSVardan Mikayelyan 			__func__);
5702c5c403dcSVardan Mikayelyan 		return ret;
5703c5c403dcSVardan Mikayelyan 	}
5704c5c403dcSVardan Mikayelyan 
5705c2db8d7bSArtur Petrosyan 	if (rem_wakeup) {
570622bb5cfdSArtur Petrosyan 		dwc2_hcd_rem_wakeup(hsotg);
5707c2db8d7bSArtur Petrosyan 		/*
5708c2db8d7bSArtur Petrosyan 		 * Change "port_connect_status_change" flag to re-enumerate,
5709c2db8d7bSArtur Petrosyan 		 * because after exit from hibernation port connection status
5710c2db8d7bSArtur Petrosyan 		 * is not detected.
5711c2db8d7bSArtur Petrosyan 		 */
5712c2db8d7bSArtur Petrosyan 		hsotg->flags.b.port_connect_status_change = 1;
5713c2db8d7bSArtur Petrosyan 	}
571422bb5cfdSArtur Petrosyan 
5715c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5716c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 0;
5717c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L0;
5718c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
5719c5c403dcSVardan Mikayelyan 	return ret;
5720c5c403dcSVardan Mikayelyan }
5721c846b03fSDouglas Anderson 
5722c846b03fSDouglas Anderson bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
5723c846b03fSDouglas Anderson {
5724c846b03fSDouglas Anderson 	struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
5725c846b03fSDouglas Anderson 
5726c846b03fSDouglas Anderson 	/* If the controller isn't allowed to wakeup then we can power off. */
5727c846b03fSDouglas Anderson 	if (!device_may_wakeup(dwc2->dev))
5728c846b03fSDouglas Anderson 		return true;
5729c846b03fSDouglas Anderson 
5730c846b03fSDouglas Anderson 	/*
5731c846b03fSDouglas Anderson 	 * We don't want to power off the PHY if something under the
5732c846b03fSDouglas Anderson 	 * root hub has wakeup enabled.
5733c846b03fSDouglas Anderson 	 */
5734c846b03fSDouglas Anderson 	if (usb_wakeup_enabled_descendants(root_hub))
5735c846b03fSDouglas Anderson 		return false;
5736c846b03fSDouglas Anderson 
5737c846b03fSDouglas Anderson 	/* No reason to keep the PHY powered, so allow poweroff */
5738c846b03fSDouglas Anderson 	return true;
5739c846b03fSDouglas Anderson }
57409ce9e5adSArtur Petrosyan 
57419ce9e5adSArtur Petrosyan /**
57429ce9e5adSArtur Petrosyan  * dwc2_host_enter_partial_power_down() - Put controller in partial
57439ce9e5adSArtur Petrosyan  * power down.
57449ce9e5adSArtur Petrosyan  *
57459ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
57469ce9e5adSArtur Petrosyan  *
57479ce9e5adSArtur Petrosyan  * Return: non-zero if failed to enter host partial power down.
57489ce9e5adSArtur Petrosyan  *
57499ce9e5adSArtur Petrosyan  * This function is for entering Host mode partial power down.
57509ce9e5adSArtur Petrosyan  */
57519ce9e5adSArtur Petrosyan int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg)
57529ce9e5adSArtur Petrosyan {
57539ce9e5adSArtur Petrosyan 	u32 pcgcctl;
57549ce9e5adSArtur Petrosyan 	u32 hprt0;
57559ce9e5adSArtur Petrosyan 	int ret = 0;
57569ce9e5adSArtur Petrosyan 
57579ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down started.\n");
57589ce9e5adSArtur Petrosyan 
57599ce9e5adSArtur Petrosyan 	/* Put this port in suspend mode. */
57609ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
57619ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
57629ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
57639ce9e5adSArtur Petrosyan 	udelay(5);
57649ce9e5adSArtur Petrosyan 
57659ce9e5adSArtur Petrosyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
57669ce9e5adSArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
57679ce9e5adSArtur Petrosyan 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
57689ce9e5adSArtur Petrosyan 
57699ce9e5adSArtur Petrosyan 	/* Backup all registers */
57709ce9e5adSArtur Petrosyan 	ret = dwc2_backup_global_registers(hsotg);
57719ce9e5adSArtur Petrosyan 	if (ret) {
57729ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
57739ce9e5adSArtur Petrosyan 			__func__);
57749ce9e5adSArtur Petrosyan 		return ret;
57759ce9e5adSArtur Petrosyan 	}
57769ce9e5adSArtur Petrosyan 
57779ce9e5adSArtur Petrosyan 	ret = dwc2_backup_host_registers(hsotg);
57789ce9e5adSArtur Petrosyan 	if (ret) {
57799ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
57809ce9e5adSArtur Petrosyan 			__func__);
57819ce9e5adSArtur Petrosyan 		return ret;
57829ce9e5adSArtur Petrosyan 	}
57839ce9e5adSArtur Petrosyan 
57849ce9e5adSArtur Petrosyan 	/*
57859ce9e5adSArtur Petrosyan 	 * Clear any pending interrupts since dwc2 will not be able to
57869ce9e5adSArtur Petrosyan 	 * clear them after entering partial_power_down.
57879ce9e5adSArtur Petrosyan 	 */
57889ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
57899ce9e5adSArtur Petrosyan 
57909ce9e5adSArtur Petrosyan 	/* Put the controller in low power state */
57919ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
57929ce9e5adSArtur Petrosyan 
57939ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_PWRCLMP;
57949ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57959ce9e5adSArtur Petrosyan 	udelay(5);
57969ce9e5adSArtur Petrosyan 
57979ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
57989ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57999ce9e5adSArtur Petrosyan 	udelay(5);
58009ce9e5adSArtur Petrosyan 
58019ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_STOPPCLK;
58029ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58039ce9e5adSArtur Petrosyan 
58049ce9e5adSArtur Petrosyan 	/* Set in_ppd flag to 1 as here core enters suspend. */
58059ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 1;
58069ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
58079ce9e5adSArtur Petrosyan 	hsotg->bus_suspended = true;
58089ce9e5adSArtur Petrosyan 
58099ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down completed.\n");
58109ce9e5adSArtur Petrosyan 
58119ce9e5adSArtur Petrosyan 	return ret;
58129ce9e5adSArtur Petrosyan }
58139ce9e5adSArtur Petrosyan 
58149ce9e5adSArtur Petrosyan /*
58159ce9e5adSArtur Petrosyan  * dwc2_host_exit_partial_power_down() - Exit controller from host partial
58169ce9e5adSArtur Petrosyan  * power down.
58179ce9e5adSArtur Petrosyan  *
58189ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
58199ce9e5adSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by Reset.
58209ce9e5adSArtur Petrosyan  * @restore: indicates whether need to restore the registers or not.
58219ce9e5adSArtur Petrosyan  *
58229ce9e5adSArtur Petrosyan  * Return: non-zero if failed to exit host partial power down.
58239ce9e5adSArtur Petrosyan  *
58249ce9e5adSArtur Petrosyan  * This function is for exiting from Host mode partial power down.
58259ce9e5adSArtur Petrosyan  */
58269ce9e5adSArtur Petrosyan int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg,
58279ce9e5adSArtur Petrosyan 				      int rem_wakeup, bool restore)
58289ce9e5adSArtur Petrosyan {
58299ce9e5adSArtur Petrosyan 	u32 pcgcctl;
58309ce9e5adSArtur Petrosyan 	int ret = 0;
58319ce9e5adSArtur Petrosyan 	u32 hprt0;
58329ce9e5adSArtur Petrosyan 
58339ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down started.\n");
58349ce9e5adSArtur Petrosyan 
58359ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58369ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_STOPPCLK;
58379ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58389ce9e5adSArtur Petrosyan 	udelay(5);
58399ce9e5adSArtur Petrosyan 
58409ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58419ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_PWRCLMP;
58429ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58439ce9e5adSArtur Petrosyan 	udelay(5);
58449ce9e5adSArtur Petrosyan 
58459ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58469ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
58479ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58489ce9e5adSArtur Petrosyan 
58499ce9e5adSArtur Petrosyan 	udelay(100);
58509ce9e5adSArtur Petrosyan 	if (restore) {
58519ce9e5adSArtur Petrosyan 		ret = dwc2_restore_global_registers(hsotg);
58529ce9e5adSArtur Petrosyan 		if (ret) {
58539ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore registers\n",
58549ce9e5adSArtur Petrosyan 				__func__);
58559ce9e5adSArtur Petrosyan 			return ret;
58569ce9e5adSArtur Petrosyan 		}
58579ce9e5adSArtur Petrosyan 
58589ce9e5adSArtur Petrosyan 		ret = dwc2_restore_host_registers(hsotg);
58599ce9e5adSArtur Petrosyan 		if (ret) {
58609ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore host registers\n",
58619ce9e5adSArtur Petrosyan 				__func__);
58629ce9e5adSArtur Petrosyan 			return ret;
58639ce9e5adSArtur Petrosyan 		}
58649ce9e5adSArtur Petrosyan 	}
58659ce9e5adSArtur Petrosyan 
58669ce9e5adSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
58679ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
58689ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_RES;
58699ce9e5adSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
58709ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
58719ce9e5adSArtur Petrosyan 	udelay(5);
58729ce9e5adSArtur Petrosyan 
58739ce9e5adSArtur Petrosyan 	if (!rem_wakeup) {
58749ce9e5adSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
58759ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58769ce9e5adSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
58779ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58789ce9e5adSArtur Petrosyan 
58799ce9e5adSArtur Petrosyan 		hsotg->bus_suspended = false;
58809ce9e5adSArtur Petrosyan 	} else {
58819ce9e5adSArtur Petrosyan 		/* Turn on the port power bit. */
58829ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58839ce9e5adSArtur Petrosyan 		hprt0 |= HPRT0_PWR;
58849ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58859ce9e5adSArtur Petrosyan 
58869ce9e5adSArtur Petrosyan 		/* Connect hcd. */
58879ce9e5adSArtur Petrosyan 		dwc2_hcd_connect(hsotg);
58889ce9e5adSArtur Petrosyan 
58899ce9e5adSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
58909ce9e5adSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
58919ce9e5adSArtur Petrosyan 	}
58929ce9e5adSArtur Petrosyan 
58939ce9e5adSArtur Petrosyan 	/* Set lx_state to and in_ppd to 0 as here core exits from suspend. */
58949ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 0;
58959ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L0;
58969ce9e5adSArtur Petrosyan 
58979ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n");
58989ce9e5adSArtur Petrosyan 	return ret;
58999ce9e5adSArtur Petrosyan }
590079c87c3cSArtur Petrosyan 
590179c87c3cSArtur Petrosyan /**
590279c87c3cSArtur Petrosyan  * dwc2_host_enter_clock_gating() - Put controller in clock gating.
590379c87c3cSArtur Petrosyan  *
590479c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
590579c87c3cSArtur Petrosyan  *
590679c87c3cSArtur Petrosyan  * This function is for entering Host mode clock gating.
590779c87c3cSArtur Petrosyan  */
590879c87c3cSArtur Petrosyan void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
590979c87c3cSArtur Petrosyan {
591079c87c3cSArtur Petrosyan 	u32 hprt0;
591179c87c3cSArtur Petrosyan 	u32 pcgctl;
591279c87c3cSArtur Petrosyan 
591379c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host clock gating.\n");
591479c87c3cSArtur Petrosyan 
591579c87c3cSArtur Petrosyan 	/* Put this port in suspend mode. */
591679c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
591779c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
591879c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
591979c87c3cSArtur Petrosyan 
592079c87c3cSArtur Petrosyan 	/* Set the Phy Clock bit as suspend is received. */
592179c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
592279c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_STOPPCLK;
592379c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
592479c87c3cSArtur Petrosyan 	udelay(5);
592579c87c3cSArtur Petrosyan 
592679c87c3cSArtur Petrosyan 	/* Set the Gate hclk as suspend is received. */
592779c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
592879c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_GATEHCLK;
592979c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
593079c87c3cSArtur Petrosyan 	udelay(5);
593179c87c3cSArtur Petrosyan 
593279c87c3cSArtur Petrosyan 	hsotg->bus_suspended = true;
593379c87c3cSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
593479c87c3cSArtur Petrosyan }
593579c87c3cSArtur Petrosyan 
593679c87c3cSArtur Petrosyan /**
593779c87c3cSArtur Petrosyan  * dwc2_host_exit_clock_gating() - Exit controller from clock gating.
593879c87c3cSArtur Petrosyan  *
593979c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
594079c87c3cSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by remote wakeup
594179c87c3cSArtur Petrosyan  *
594279c87c3cSArtur Petrosyan  * This function is for exiting Host mode clock gating.
594379c87c3cSArtur Petrosyan  */
594479c87c3cSArtur Petrosyan void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
594579c87c3cSArtur Petrosyan {
594679c87c3cSArtur Petrosyan 	u32 hprt0;
594779c87c3cSArtur Petrosyan 	u32 pcgctl;
594879c87c3cSArtur Petrosyan 
594979c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host clock gating.\n");
595079c87c3cSArtur Petrosyan 
595179c87c3cSArtur Petrosyan 	/* Clear the Gate hclk. */
595279c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
595379c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_GATEHCLK;
595479c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
595579c87c3cSArtur Petrosyan 	udelay(5);
595679c87c3cSArtur Petrosyan 
595779c87c3cSArtur Petrosyan 	/* Phy Clock bit. */
595879c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
595979c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_STOPPCLK;
596079c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
596179c87c3cSArtur Petrosyan 	udelay(5);
596279c87c3cSArtur Petrosyan 
596379c87c3cSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
596479c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
596579c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_RES;
596679c87c3cSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
596779c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
596879c87c3cSArtur Petrosyan 	udelay(5);
596979c87c3cSArtur Petrosyan 
597079c87c3cSArtur Petrosyan 	if (!rem_wakeup) {
597179c87c3cSArtur Petrosyan 		/* In case of port resume need to wait for 40 ms */
597279c87c3cSArtur Petrosyan 		msleep(USB_RESUME_TIMEOUT);
597379c87c3cSArtur Petrosyan 
597479c87c3cSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
597579c87c3cSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
597679c87c3cSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
597779c87c3cSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
597879c87c3cSArtur Petrosyan 
597979c87c3cSArtur Petrosyan 		hsotg->bus_suspended = false;
598079c87c3cSArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
598179c87c3cSArtur Petrosyan 	} else {
598279c87c3cSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
598379c87c3cSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
598479c87c3cSArtur Petrosyan 	}
598579c87c3cSArtur Petrosyan }
5986