xref: /linux/drivers/usb/dwc2/hcd.c (revision ef307bc6ef04e8c1ea843231db58e3afaafa9fa6)
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);
2704197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2705197ba5f4SPaul Zimmerman 			break;
2706197ba5f4SPaul Zimmerman 
2707197ba5f4SPaul Zimmerman 		/*
2708197ba5f4SPaul Zimmerman 		 * Move the QH from the periodic ready schedule to the
2709197ba5f4SPaul Zimmerman 		 * periodic assigned schedule
2710197ba5f4SPaul Zimmerman 		 */
2711197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
271294ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
271394ef7aeeSDouglas Anderson 			       &hsotg->periodic_sched_assigned);
2714197ba5f4SPaul Zimmerman 		ret_val = DWC2_TRANSACTION_PERIODIC;
2715197ba5f4SPaul Zimmerman 	}
2716197ba5f4SPaul Zimmerman 
2717197ba5f4SPaul Zimmerman 	/*
2718197ba5f4SPaul Zimmerman 	 * Process entries in the inactive portion of the non-periodic
2719197ba5f4SPaul Zimmerman 	 * schedule. Some free host channels may not be used if they are
2720197ba5f4SPaul Zimmerman 	 * reserved for periodic transfers.
2721197ba5f4SPaul Zimmerman 	 */
2722bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
2723197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->non_periodic_sched_inactive.next;
2724197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
272595832c00SJohn Youn 		if (!hsotg->params.uframe_sched &&
2726197ba5f4SPaul Zimmerman 		    hsotg->non_periodic_channels >= num_channels -
2727197ba5f4SPaul Zimmerman 						hsotg->periodic_channels)
2728197ba5f4SPaul Zimmerman 			break;
2729197ba5f4SPaul Zimmerman 		if (list_empty(&hsotg->free_hc_list))
2730197ba5f4SPaul Zimmerman 			break;
2731197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
273295832c00SJohn Youn 		if (hsotg->params.uframe_sched) {
2733197ba5f4SPaul Zimmerman 			if (hsotg->available_host_channels < 1)
2734197ba5f4SPaul Zimmerman 				break;
2735197ba5f4SPaul Zimmerman 			hsotg->available_host_channels--;
2736197ba5f4SPaul Zimmerman 		}
2737197ba5f4SPaul Zimmerman 
2738197ba5f4SPaul Zimmerman 		if (dwc2_assign_and_init_hc(hsotg, qh))
2739197ba5f4SPaul Zimmerman 			break;
2740197ba5f4SPaul Zimmerman 
2741197ba5f4SPaul Zimmerman 		/*
2742197ba5f4SPaul Zimmerman 		 * Move the QH from the non-periodic inactive schedule to the
2743197ba5f4SPaul Zimmerman 		 * non-periodic active schedule
2744197ba5f4SPaul Zimmerman 		 */
2745197ba5f4SPaul Zimmerman 		qh_ptr = qh_ptr->next;
274694ef7aeeSDouglas Anderson 		list_move_tail(&qh->qh_list_entry,
2747197ba5f4SPaul Zimmerman 			       &hsotg->non_periodic_sched_active);
2748197ba5f4SPaul Zimmerman 
2749197ba5f4SPaul Zimmerman 		if (ret_val == DWC2_TRANSACTION_NONE)
2750197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2751197ba5f4SPaul Zimmerman 		else
2752197ba5f4SPaul Zimmerman 			ret_val = DWC2_TRANSACTION_ALL;
2753197ba5f4SPaul Zimmerman 
275495832c00SJohn Youn 		if (!hsotg->params.uframe_sched)
2755197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels++;
2756197ba5f4SPaul Zimmerman 	}
2757197ba5f4SPaul Zimmerman 
2758197ba5f4SPaul Zimmerman 	return ret_val;
2759197ba5f4SPaul Zimmerman }
2760197ba5f4SPaul Zimmerman 
2761197ba5f4SPaul Zimmerman /**
2762197ba5f4SPaul Zimmerman  * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2763197ba5f4SPaul Zimmerman  * a host channel associated with either a periodic or non-periodic transfer
2764197ba5f4SPaul Zimmerman  *
2765197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure
2766197ba5f4SPaul Zimmerman  * @chan:  Host channel descriptor associated with either a periodic or
2767197ba5f4SPaul Zimmerman  *         non-periodic transfer
2768197ba5f4SPaul Zimmerman  * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2769197ba5f4SPaul Zimmerman  *                     for periodic transfers or the non-periodic Tx FIFO
2770197ba5f4SPaul Zimmerman  *                     for non-periodic transfers
2771197ba5f4SPaul Zimmerman  *
2772197ba5f4SPaul Zimmerman  * Return: 1 if a request is queued and more requests may be needed to
2773197ba5f4SPaul Zimmerman  * complete the transfer, 0 if no more requests are required for this
2774197ba5f4SPaul Zimmerman  * transfer, -1 if there is insufficient space in the Tx FIFO
2775197ba5f4SPaul Zimmerman  *
2776197ba5f4SPaul Zimmerman  * This function assumes that there is space available in the appropriate
2777197ba5f4SPaul Zimmerman  * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2778197ba5f4SPaul Zimmerman  * it checks whether space is available in the appropriate Tx FIFO.
2779197ba5f4SPaul Zimmerman  *
2780197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2781197ba5f4SPaul Zimmerman  */
2782197ba5f4SPaul Zimmerman static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2783197ba5f4SPaul Zimmerman 				  struct dwc2_host_chan *chan,
2784197ba5f4SPaul Zimmerman 				  u16 fifo_dwords_avail)
2785197ba5f4SPaul Zimmerman {
2786197ba5f4SPaul Zimmerman 	int retval = 0;
2787197ba5f4SPaul Zimmerman 
2788c9c8ac01SDouglas Anderson 	if (chan->do_split)
2789c9c8ac01SDouglas Anderson 		/* Put ourselves on the list to keep order straight */
2790c9c8ac01SDouglas Anderson 		list_move_tail(&chan->split_order_list_entry,
2791c9c8ac01SDouglas Anderson 			       &hsotg->split_order);
2792c9c8ac01SDouglas Anderson 
27937b813767SAlexandru M Stan 	if (hsotg->params.host_dma && chan->qh) {
279495832c00SJohn Youn 		if (hsotg->params.dma_desc_enable) {
2795197ba5f4SPaul Zimmerman 			if (!chan->xfer_started ||
2796197ba5f4SPaul Zimmerman 			    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2797197ba5f4SPaul Zimmerman 				dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2798197ba5f4SPaul Zimmerman 				chan->qh->ping_state = 0;
2799197ba5f4SPaul Zimmerman 			}
2800197ba5f4SPaul Zimmerman 		} else if (!chan->xfer_started) {
2801197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2802197ba5f4SPaul Zimmerman 			chan->qh->ping_state = 0;
2803197ba5f4SPaul Zimmerman 		}
2804197ba5f4SPaul Zimmerman 	} else if (chan->halt_pending) {
2805197ba5f4SPaul Zimmerman 		/* Don't queue a request if the channel has been halted */
2806197ba5f4SPaul Zimmerman 	} else if (chan->halt_on_queue) {
2807197ba5f4SPaul Zimmerman 		dwc2_hc_halt(hsotg, chan, chan->halt_status);
2808197ba5f4SPaul Zimmerman 	} else if (chan->do_ping) {
2809197ba5f4SPaul Zimmerman 		if (!chan->xfer_started)
2810197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2811197ba5f4SPaul Zimmerman 	} else if (!chan->ep_is_in ||
2812197ba5f4SPaul Zimmerman 		   chan->data_pid_start == DWC2_HC_PID_SETUP) {
2813197ba5f4SPaul Zimmerman 		if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2814197ba5f4SPaul Zimmerman 			if (!chan->xfer_started) {
2815197ba5f4SPaul Zimmerman 				dwc2_hc_start_transfer(hsotg, chan);
2816197ba5f4SPaul Zimmerman 				retval = 1;
2817197ba5f4SPaul Zimmerman 			} else {
2818197ba5f4SPaul Zimmerman 				retval = dwc2_hc_continue_transfer(hsotg, chan);
2819197ba5f4SPaul Zimmerman 			}
2820197ba5f4SPaul Zimmerman 		} else {
2821197ba5f4SPaul Zimmerman 			retval = -1;
2822197ba5f4SPaul Zimmerman 		}
2823197ba5f4SPaul Zimmerman 	} else {
2824197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
2825197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer(hsotg, chan);
2826197ba5f4SPaul Zimmerman 			retval = 1;
2827197ba5f4SPaul Zimmerman 		} else {
2828197ba5f4SPaul Zimmerman 			retval = dwc2_hc_continue_transfer(hsotg, chan);
2829197ba5f4SPaul Zimmerman 		}
2830197ba5f4SPaul Zimmerman 	}
2831197ba5f4SPaul Zimmerman 
2832197ba5f4SPaul Zimmerman 	return retval;
2833197ba5f4SPaul Zimmerman }
2834197ba5f4SPaul Zimmerman 
2835197ba5f4SPaul Zimmerman /*
2836197ba5f4SPaul Zimmerman  * Processes periodic channels for the next frame and queues transactions for
2837197ba5f4SPaul Zimmerman  * these channels to the DWC_otg controller. After queueing transactions, the
2838197ba5f4SPaul Zimmerman  * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2839197ba5f4SPaul Zimmerman  * to queue as Periodic Tx FIFO or request queue space becomes available.
2840197ba5f4SPaul Zimmerman  * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2841197ba5f4SPaul Zimmerman  *
2842197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2843197ba5f4SPaul Zimmerman  */
2844197ba5f4SPaul Zimmerman static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2845197ba5f4SPaul Zimmerman {
2846197ba5f4SPaul Zimmerman 	struct list_head *qh_ptr;
2847197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2848197ba5f4SPaul Zimmerman 	u32 tx_status;
2849197ba5f4SPaul Zimmerman 	u32 fspcavail;
2850197ba5f4SPaul Zimmerman 	u32 gintmsk;
2851197ba5f4SPaul Zimmerman 	int status;
28524e50e011SDouglas Anderson 	bool no_queue_space = false;
28534e50e011SDouglas Anderson 	bool no_fifo_space = false;
2854197ba5f4SPaul Zimmerman 	u32 qspcavail;
2855197ba5f4SPaul Zimmerman 
28564e50e011SDouglas Anderson 	/* If empty list then just adjust interrupt enables */
28574e50e011SDouglas Anderson 	if (list_empty(&hsotg->periodic_sched_assigned))
28584e50e011SDouglas Anderson 		goto exit;
28594e50e011SDouglas Anderson 
2860197ba5f4SPaul Zimmerman 	if (dbg_perio())
2861197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
2862197ba5f4SPaul Zimmerman 
2863f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, HPTXSTS);
2864197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2865197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
2866197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2867197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
2868197ba5f4SPaul Zimmerman 
2869197ba5f4SPaul Zimmerman 	if (dbg_perio()) {
2870197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx Req Queue Space Avail (before queue): %d\n",
2871197ba5f4SPaul Zimmerman 			 qspcavail);
2872197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  P Tx FIFO Space Avail (before queue): %d\n",
2873197ba5f4SPaul Zimmerman 			 fspcavail);
2874197ba5f4SPaul Zimmerman 	}
2875197ba5f4SPaul Zimmerman 
2876197ba5f4SPaul Zimmerman 	qh_ptr = hsotg->periodic_sched_assigned.next;
2877197ba5f4SPaul Zimmerman 	while (qh_ptr != &hsotg->periodic_sched_assigned) {
2878f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, HPTXSTS);
2879197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2880197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
2881197ba5f4SPaul Zimmerman 		if (qspcavail == 0) {
2882fdb09b3eSNicholas Mc Guire 			no_queue_space = true;
2883197ba5f4SPaul Zimmerman 			break;
2884197ba5f4SPaul Zimmerman 		}
2885197ba5f4SPaul Zimmerman 
2886197ba5f4SPaul Zimmerman 		qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2887197ba5f4SPaul Zimmerman 		if (!qh->channel) {
2888197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2889197ba5f4SPaul Zimmerman 			continue;
2890197ba5f4SPaul Zimmerman 		}
2891197ba5f4SPaul Zimmerman 
2892197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
2893197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty) {
2894197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2895197ba5f4SPaul Zimmerman 			continue;
2896197ba5f4SPaul Zimmerman 		}
2897197ba5f4SPaul Zimmerman 
2898197ba5f4SPaul Zimmerman 		/*
2899197ba5f4SPaul Zimmerman 		 * Set a flag if we're queuing high-bandwidth in slave mode.
2900197ba5f4SPaul Zimmerman 		 * The flag prevents any halts to get into the request queue in
2901197ba5f4SPaul Zimmerman 		 * the middle of multiple high-bandwidth packets getting queued.
2902197ba5f4SPaul Zimmerman 		 */
290395832c00SJohn Youn 		if (!hsotg->params.host_dma &&
2904197ba5f4SPaul Zimmerman 		    qh->channel->multi_count > 1)
2905197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 1;
2906197ba5f4SPaul Zimmerman 
2907197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2908197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
2909197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
2910197ba5f4SPaul Zimmerman 		if (status < 0) {
2911fdb09b3eSNicholas Mc Guire 			no_fifo_space = true;
2912197ba5f4SPaul Zimmerman 			break;
2913197ba5f4SPaul Zimmerman 		}
2914197ba5f4SPaul Zimmerman 
2915197ba5f4SPaul Zimmerman 		/*
2916197ba5f4SPaul Zimmerman 		 * In Slave mode, stay on the current transfer until there is
2917197ba5f4SPaul Zimmerman 		 * nothing more to do or the high-bandwidth request count is
2918197ba5f4SPaul Zimmerman 		 * reached. In DMA mode, only need to queue one request. The
2919197ba5f4SPaul Zimmerman 		 * controller automatically handles multiple packets for
2920197ba5f4SPaul Zimmerman 		 * high-bandwidth transfers.
2921197ba5f4SPaul Zimmerman 		 */
292295832c00SJohn Youn 		if (hsotg->params.host_dma || status == 0 ||
2923197ba5f4SPaul Zimmerman 		    qh->channel->requests == qh->channel->multi_count) {
2924197ba5f4SPaul Zimmerman 			qh_ptr = qh_ptr->next;
2925197ba5f4SPaul Zimmerman 			/*
2926197ba5f4SPaul Zimmerman 			 * Move the QH from the periodic assigned schedule to
2927197ba5f4SPaul Zimmerman 			 * the periodic queued schedule
2928197ba5f4SPaul Zimmerman 			 */
292994ef7aeeSDouglas Anderson 			list_move_tail(&qh->qh_list_entry,
2930197ba5f4SPaul Zimmerman 				       &hsotg->periodic_sched_queued);
2931197ba5f4SPaul Zimmerman 
2932197ba5f4SPaul Zimmerman 			/* done queuing high bandwidth */
2933197ba5f4SPaul Zimmerman 			hsotg->queuing_high_bandwidth = 0;
2934197ba5f4SPaul Zimmerman 		}
2935197ba5f4SPaul Zimmerman 	}
2936197ba5f4SPaul Zimmerman 
29374e50e011SDouglas Anderson exit:
29384e50e011SDouglas Anderson 	if (no_queue_space || no_fifo_space ||
293995832c00SJohn Youn 	    (!hsotg->params.host_dma &&
29404e50e011SDouglas Anderson 	     !list_empty(&hsotg->periodic_sched_assigned))) {
2941197ba5f4SPaul Zimmerman 		/*
2942197ba5f4SPaul Zimmerman 		 * May need to queue more transactions as the request
2943197ba5f4SPaul Zimmerman 		 * queue or Tx FIFO empties. Enable the periodic Tx
2944197ba5f4SPaul Zimmerman 		 * FIFO empty interrupt. (Always use the half-empty
2945197ba5f4SPaul Zimmerman 		 * level to ensure that new requests are loaded as
2946197ba5f4SPaul Zimmerman 		 * soon as possible.)
2947197ba5f4SPaul Zimmerman 		 */
2948f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29494e50e011SDouglas Anderson 		if (!(gintmsk & GINTSTS_PTXFEMP)) {
2950197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_PTXFEMP;
2951f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
29524e50e011SDouglas Anderson 		}
2953197ba5f4SPaul Zimmerman 	} else {
2954197ba5f4SPaul Zimmerman 		/*
2955197ba5f4SPaul Zimmerman 		 * Disable the Tx FIFO empty interrupt since there are
2956197ba5f4SPaul Zimmerman 		 * no more transactions that need to be queued right
2957197ba5f4SPaul Zimmerman 		 * now. This function is called from interrupt
2958197ba5f4SPaul Zimmerman 		 * handlers to queue more transactions as transfer
2959197ba5f4SPaul Zimmerman 		 * states change.
2960197ba5f4SPaul Zimmerman 		 */
2961f25c42b8SGevorg Sahakyan 		gintmsk = dwc2_readl(hsotg, GINTMSK);
29624e50e011SDouglas Anderson 		if (gintmsk & GINTSTS_PTXFEMP) {
2963197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_PTXFEMP;
2964f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
2965197ba5f4SPaul Zimmerman 		}
2966197ba5f4SPaul Zimmerman 	}
2967197ba5f4SPaul Zimmerman }
2968197ba5f4SPaul Zimmerman 
2969197ba5f4SPaul Zimmerman /*
2970197ba5f4SPaul Zimmerman  * Processes active non-periodic channels and queues transactions for these
2971197ba5f4SPaul Zimmerman  * channels to the DWC_otg controller. After queueing transactions, the NP Tx
2972197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is enabled if there are more transactions to queue as
2973197ba5f4SPaul Zimmerman  * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
2974197ba5f4SPaul Zimmerman  * FIFO Empty interrupt is disabled.
2975197ba5f4SPaul Zimmerman  *
2976197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
2977197ba5f4SPaul Zimmerman  */
2978197ba5f4SPaul Zimmerman static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
2979197ba5f4SPaul Zimmerman {
2980197ba5f4SPaul Zimmerman 	struct list_head *orig_qh_ptr;
2981197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
2982197ba5f4SPaul Zimmerman 	u32 tx_status;
2983197ba5f4SPaul Zimmerman 	u32 qspcavail;
2984197ba5f4SPaul Zimmerman 	u32 fspcavail;
2985197ba5f4SPaul Zimmerman 	u32 gintmsk;
2986197ba5f4SPaul Zimmerman 	int status;
2987197ba5f4SPaul Zimmerman 	int no_queue_space = 0;
2988197ba5f4SPaul Zimmerman 	int no_fifo_space = 0;
2989197ba5f4SPaul Zimmerman 	int more_to_do = 0;
2990197ba5f4SPaul Zimmerman 
2991197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
2992197ba5f4SPaul Zimmerman 
2993f25c42b8SGevorg Sahakyan 	tx_status = dwc2_readl(hsotg, GNPTXSTS);
2994197ba5f4SPaul Zimmerman 	qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2995197ba5f4SPaul Zimmerman 		    TXSTS_QSPCAVAIL_SHIFT;
2996197ba5f4SPaul Zimmerman 	fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2997197ba5f4SPaul Zimmerman 		    TXSTS_FSPCAVAIL_SHIFT;
2998197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx Req Queue Space Avail (before queue): %d\n",
2999197ba5f4SPaul Zimmerman 		 qspcavail);
3000197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  NP Tx FIFO Space Avail (before queue): %d\n",
3001197ba5f4SPaul Zimmerman 		 fspcavail);
3002197ba5f4SPaul Zimmerman 
3003197ba5f4SPaul Zimmerman 	/*
3004197ba5f4SPaul Zimmerman 	 * Keep track of the starting point. Skip over the start-of-list
3005197ba5f4SPaul Zimmerman 	 * entry.
3006197ba5f4SPaul Zimmerman 	 */
3007197ba5f4SPaul Zimmerman 	if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3008197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3009197ba5f4SPaul Zimmerman 	orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3010197ba5f4SPaul Zimmerman 
3011197ba5f4SPaul Zimmerman 	/*
3012197ba5f4SPaul Zimmerman 	 * Process once through the active list or until no more space is
3013197ba5f4SPaul Zimmerman 	 * available in the request queue or the Tx FIFO
3014197ba5f4SPaul Zimmerman 	 */
3015197ba5f4SPaul Zimmerman 	do {
3016f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3017197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3018197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
301995832c00SJohn Youn 		if (!hsotg->params.host_dma && qspcavail == 0) {
3020197ba5f4SPaul Zimmerman 			no_queue_space = 1;
3021197ba5f4SPaul Zimmerman 			break;
3022197ba5f4SPaul Zimmerman 		}
3023197ba5f4SPaul Zimmerman 
3024197ba5f4SPaul Zimmerman 		qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3025197ba5f4SPaul Zimmerman 				qh_list_entry);
3026197ba5f4SPaul Zimmerman 		if (!qh->channel)
3027197ba5f4SPaul Zimmerman 			goto next;
3028197ba5f4SPaul Zimmerman 
3029197ba5f4SPaul Zimmerman 		/* Make sure EP's TT buffer is clean before queueing qtds */
3030197ba5f4SPaul Zimmerman 		if (qh->tt_buffer_dirty)
3031197ba5f4SPaul Zimmerman 			goto next;
3032197ba5f4SPaul Zimmerman 
3033197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3034197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3035197ba5f4SPaul Zimmerman 		status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3036197ba5f4SPaul Zimmerman 
3037197ba5f4SPaul Zimmerman 		if (status > 0) {
3038197ba5f4SPaul Zimmerman 			more_to_do = 1;
3039197ba5f4SPaul Zimmerman 		} else if (status < 0) {
3040197ba5f4SPaul Zimmerman 			no_fifo_space = 1;
3041197ba5f4SPaul Zimmerman 			break;
3042197ba5f4SPaul Zimmerman 		}
3043197ba5f4SPaul Zimmerman next:
3044197ba5f4SPaul Zimmerman 		/* Advance to next QH, skipping start-of-list entry */
3045197ba5f4SPaul Zimmerman 		hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3046197ba5f4SPaul Zimmerman 		if (hsotg->non_periodic_qh_ptr ==
3047197ba5f4SPaul Zimmerman 				&hsotg->non_periodic_sched_active)
3048197ba5f4SPaul Zimmerman 			hsotg->non_periodic_qh_ptr =
3049197ba5f4SPaul Zimmerman 					hsotg->non_periodic_qh_ptr->next;
3050197ba5f4SPaul Zimmerman 	} while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3051197ba5f4SPaul Zimmerman 
305295832c00SJohn Youn 	if (!hsotg->params.host_dma) {
3053f25c42b8SGevorg Sahakyan 		tx_status = dwc2_readl(hsotg, GNPTXSTS);
3054197ba5f4SPaul Zimmerman 		qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3055197ba5f4SPaul Zimmerman 			    TXSTS_QSPCAVAIL_SHIFT;
3056197ba5f4SPaul Zimmerman 		fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3057197ba5f4SPaul Zimmerman 			    TXSTS_FSPCAVAIL_SHIFT;
3058197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3059197ba5f4SPaul Zimmerman 			 "  NP Tx Req Queue Space Avail (after queue): %d\n",
3060197ba5f4SPaul Zimmerman 			 qspcavail);
3061197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3062197ba5f4SPaul Zimmerman 			 "  NP Tx FIFO Space Avail (after queue): %d\n",
3063197ba5f4SPaul Zimmerman 			 fspcavail);
3064197ba5f4SPaul Zimmerman 
3065197ba5f4SPaul Zimmerman 		if (more_to_do || no_queue_space || no_fifo_space) {
3066197ba5f4SPaul Zimmerman 			/*
3067197ba5f4SPaul Zimmerman 			 * May need to queue more transactions as the request
3068197ba5f4SPaul Zimmerman 			 * queue or Tx FIFO empties. Enable the non-periodic
3069197ba5f4SPaul Zimmerman 			 * Tx FIFO empty interrupt. (Always use the half-empty
3070197ba5f4SPaul Zimmerman 			 * level to ensure that new requests are loaded as
3071197ba5f4SPaul Zimmerman 			 * soon as possible.)
3072197ba5f4SPaul Zimmerman 			 */
3073f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3074197ba5f4SPaul Zimmerman 			gintmsk |= GINTSTS_NPTXFEMP;
3075f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3076197ba5f4SPaul Zimmerman 		} else {
3077197ba5f4SPaul Zimmerman 			/*
3078197ba5f4SPaul Zimmerman 			 * Disable the Tx FIFO empty interrupt since there are
3079197ba5f4SPaul Zimmerman 			 * no more transactions that need to be queued right
3080197ba5f4SPaul Zimmerman 			 * now. This function is called from interrupt
3081197ba5f4SPaul Zimmerman 			 * handlers to queue more transactions as transfer
3082197ba5f4SPaul Zimmerman 			 * states change.
3083197ba5f4SPaul Zimmerman 			 */
3084f25c42b8SGevorg Sahakyan 			gintmsk = dwc2_readl(hsotg, GINTMSK);
3085197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3086f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3087197ba5f4SPaul Zimmerman 		}
3088197ba5f4SPaul Zimmerman 	}
3089197ba5f4SPaul Zimmerman }
3090197ba5f4SPaul Zimmerman 
3091197ba5f4SPaul Zimmerman /**
3092197ba5f4SPaul Zimmerman  * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3093197ba5f4SPaul Zimmerman  * and queues transactions for these channels to the DWC_otg controller. Called
3094197ba5f4SPaul Zimmerman  * from the HCD interrupt handler functions.
3095197ba5f4SPaul Zimmerman  *
3096197ba5f4SPaul Zimmerman  * @hsotg:   The HCD state structure
3097197ba5f4SPaul Zimmerman  * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3098197ba5f4SPaul Zimmerman  *           or both)
3099197ba5f4SPaul Zimmerman  *
3100197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
3101197ba5f4SPaul Zimmerman  */
3102197ba5f4SPaul Zimmerman void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3103197ba5f4SPaul Zimmerman 				 enum dwc2_transaction_type tr_type)
3104197ba5f4SPaul Zimmerman {
3105197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3106197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Queue Transactions\n");
3107197ba5f4SPaul Zimmerman #endif
3108197ba5f4SPaul Zimmerman 	/* Process host channels associated with periodic transfers */
31094e50e011SDouglas Anderson 	if (tr_type == DWC2_TRANSACTION_PERIODIC ||
31104e50e011SDouglas Anderson 	    tr_type == DWC2_TRANSACTION_ALL)
3111197ba5f4SPaul Zimmerman 		dwc2_process_periodic_channels(hsotg);
3112197ba5f4SPaul Zimmerman 
3113197ba5f4SPaul Zimmerman 	/* Process host channels associated with non-periodic transfers */
3114197ba5f4SPaul Zimmerman 	if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3115197ba5f4SPaul Zimmerman 	    tr_type == DWC2_TRANSACTION_ALL) {
3116197ba5f4SPaul Zimmerman 		if (!list_empty(&hsotg->non_periodic_sched_active)) {
3117197ba5f4SPaul Zimmerman 			dwc2_process_non_periodic_channels(hsotg);
3118197ba5f4SPaul Zimmerman 		} else {
3119197ba5f4SPaul Zimmerman 			/*
3120197ba5f4SPaul Zimmerman 			 * Ensure NP Tx FIFO empty interrupt is disabled when
3121197ba5f4SPaul Zimmerman 			 * there are no non-periodic transfers to process
3122197ba5f4SPaul Zimmerman 			 */
3123f25c42b8SGevorg Sahakyan 			u32 gintmsk = dwc2_readl(hsotg, GINTMSK);
3124197ba5f4SPaul Zimmerman 
3125197ba5f4SPaul Zimmerman 			gintmsk &= ~GINTSTS_NPTXFEMP;
3126f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, gintmsk, GINTMSK);
3127197ba5f4SPaul Zimmerman 		}
3128197ba5f4SPaul Zimmerman 	}
3129197ba5f4SPaul Zimmerman }
3130197ba5f4SPaul Zimmerman 
3131197ba5f4SPaul Zimmerman static void dwc2_conn_id_status_change(struct work_struct *work)
3132197ba5f4SPaul Zimmerman {
3133197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3134197ba5f4SPaul Zimmerman 						wf_otg);
3135197ba5f4SPaul Zimmerman 	u32 count = 0;
3136197ba5f4SPaul Zimmerman 	u32 gotgctl;
31375390d438SMian Yousaf Kaukab 	unsigned long flags;
3138197ba5f4SPaul Zimmerman 
3139197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3140197ba5f4SPaul Zimmerman 
3141f25c42b8SGevorg Sahakyan 	gotgctl = dwc2_readl(hsotg, GOTGCTL);
3142197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3143197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3144197ba5f4SPaul Zimmerman 		!!(gotgctl & GOTGCTL_CONID_B));
3145197ba5f4SPaul Zimmerman 
3146197ba5f4SPaul Zimmerman 	/* B-Device connector (Device Mode) */
3147197ba5f4SPaul Zimmerman 	if (gotgctl & GOTGCTL_CONID_B) {
3148531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
3149197ba5f4SPaul Zimmerman 		/* Wait for switch to device mode */
3150197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId B\n");
31519156a7efSChen Yu 		if (hsotg->bus_suspended) {
31529156a7efSChen Yu 			dev_info(hsotg->dev,
31539156a7efSChen Yu 				 "Do port resume before switching to device mode\n");
31549156a7efSChen Yu 			dwc2_port_resume(hsotg);
31559156a7efSChen Yu 		}
3156197ba5f4SPaul Zimmerman 		while (!dwc2_is_device_mode(hsotg)) {
3157197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev,
3158197ba5f4SPaul Zimmerman 				 "Waiting for Peripheral Mode, Mode=%s\n",
3159197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ? "Host" :
3160197ba5f4SPaul Zimmerman 				 "Peripheral");
316104a9db79SNicholas Mc Guire 			msleep(20);
3162fc30c4bbSJohn Stultz 			/*
3163fc30c4bbSJohn Stultz 			 * Sometimes the initial GOTGCTRL read is wrong, so
3164fc30c4bbSJohn Stultz 			 * check it again and jump to host mode if that was
3165fc30c4bbSJohn Stultz 			 * the case.
3166fc30c4bbSJohn Stultz 			 */
3167f25c42b8SGevorg Sahakyan 			gotgctl = dwc2_readl(hsotg, GOTGCTL);
3168fc30c4bbSJohn Stultz 			if (!(gotgctl & GOTGCTL_CONID_B))
3169fc30c4bbSJohn Stultz 				goto host;
3170197ba5f4SPaul Zimmerman 			if (++count > 250)
3171197ba5f4SPaul Zimmerman 				break;
3172197ba5f4SPaul Zimmerman 		}
3173197ba5f4SPaul Zimmerman 		if (count > 250)
3174197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3175197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
31764d4d99afSArtur Petrosyan 
31774d4d99afSArtur Petrosyan 		/*
31784d4d99afSArtur Petrosyan 		 * Exit Partial Power Down without restoring registers.
31794d4d99afSArtur Petrosyan 		 * No need to check the return value as registers
31804d4d99afSArtur Petrosyan 		 * are not being restored.
31814d4d99afSArtur Petrosyan 		 */
31824d4d99afSArtur Petrosyan 		if (hsotg->in_ppd && hsotg->lx_state == DWC2_L2)
31834d4d99afSArtur Petrosyan 			dwc2_exit_partial_power_down(hsotg, 0, false);
31844d4d99afSArtur Petrosyan 
3185197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
31860fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3187197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
31885390d438SMian Yousaf Kaukab 		spin_lock_irqsave(&hsotg->lock, flags);
31891f91b4ccSFelipe Balbi 		dwc2_hsotg_core_init_disconnected(hsotg, false);
31905390d438SMian Yousaf Kaukab 		spin_unlock_irqrestore(&hsotg->lock, flags);
319166e77a24SRazmik Karapetyan 		/* Enable ACG feature in device mode,if supported */
319266e77a24SRazmik Karapetyan 		dwc2_enable_acg(hsotg);
31931f91b4ccSFelipe Balbi 		dwc2_hsotg_core_connect(hsotg);
3194197ba5f4SPaul Zimmerman 	} else {
3195fc30c4bbSJohn Stultz host:
3196197ba5f4SPaul Zimmerman 		/* A-Device connector (Host Mode) */
3197197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "connId A\n");
3198197ba5f4SPaul Zimmerman 		while (!dwc2_is_host_mode(hsotg)) {
3199197ba5f4SPaul Zimmerman 			dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3200197ba5f4SPaul Zimmerman 				 dwc2_is_host_mode(hsotg) ?
3201197ba5f4SPaul Zimmerman 				 "Host" : "Peripheral");
320204a9db79SNicholas Mc Guire 			msleep(20);
3203197ba5f4SPaul Zimmerman 			if (++count > 250)
3204197ba5f4SPaul Zimmerman 				break;
3205197ba5f4SPaul Zimmerman 		}
3206197ba5f4SPaul Zimmerman 		if (count > 250)
3207197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3208197ba5f4SPaul Zimmerman 				"Connection id status change timed out\n");
3209197ba5f4SPaul Zimmerman 
3210d2471d4aSJohn Stultz 		spin_lock_irqsave(&hsotg->lock, flags);
3211d2471d4aSJohn Stultz 		dwc2_hsotg_disconnect(hsotg);
3212d2471d4aSJohn Stultz 		spin_unlock_irqrestore(&hsotg->lock, flags);
3213d2471d4aSJohn Stultz 
3214d2471d4aSJohn Stultz 		hsotg->op_state = OTG_STATE_A_HOST;
3215197ba5f4SPaul Zimmerman 		/* Initialize the Core for Host mode */
32160fe239bcSDouglas Anderson 		dwc2_core_init(hsotg, false);
3217197ba5f4SPaul Zimmerman 		dwc2_enable_global_interrupts(hsotg);
3218197ba5f4SPaul Zimmerman 		dwc2_hcd_start(hsotg);
3219197ba5f4SPaul Zimmerman 	}
3220197ba5f4SPaul Zimmerman }
3221197ba5f4SPaul Zimmerman 
3222e99e88a9SKees Cook static void dwc2_wakeup_detected(struct timer_list *t)
3223197ba5f4SPaul Zimmerman {
3224e99e88a9SKees Cook 	struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
3225197ba5f4SPaul Zimmerman 	u32 hprt0;
3226197ba5f4SPaul Zimmerman 
3227197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3228197ba5f4SPaul Zimmerman 
3229197ba5f4SPaul Zimmerman 	/*
3230197ba5f4SPaul Zimmerman 	 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3231197ba5f4SPaul Zimmerman 	 * so that OPT tests pass with all PHYs.)
3232197ba5f4SPaul Zimmerman 	 */
3233197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
3234197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3235197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RES;
3236f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
3237197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
3238f25c42b8SGevorg Sahakyan 		dwc2_readl(hsotg, HPRT0));
3239197ba5f4SPaul Zimmerman 
3240197ba5f4SPaul Zimmerman 	dwc2_hcd_rem_wakeup(hsotg);
3241fdb09b3eSNicholas Mc Guire 	hsotg->bus_suspended = false;
3242197ba5f4SPaul Zimmerman 
3243197ba5f4SPaul Zimmerman 	/* Change to L0 state */
3244197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L0;
3245197ba5f4SPaul Zimmerman }
3246197ba5f4SPaul Zimmerman 
3247197ba5f4SPaul Zimmerman static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3248197ba5f4SPaul Zimmerman {
3249197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3250197ba5f4SPaul Zimmerman 
3251197ba5f4SPaul Zimmerman 	return hcd->self.b_hnp_enable;
3252197ba5f4SPaul Zimmerman }
3253197ba5f4SPaul Zimmerman 
3254139fae7aSArtur Petrosyan /**
3255139fae7aSArtur Petrosyan  * dwc2_port_suspend() - Put controller in suspend mode for host.
3256139fae7aSArtur Petrosyan  *
3257139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3258139fae7aSArtur Petrosyan  * @windex: The control request wIndex field
3259139fae7aSArtur Petrosyan  *
326022ff0c8eSArtur Petrosyan  * Return: non-zero if failed to enter suspend mode for host.
326122ff0c8eSArtur Petrosyan  *
3262139fae7aSArtur Petrosyan  * This function is for entering Host mode suspend.
3263139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3264139fae7aSArtur Petrosyan  */
326522ff0c8eSArtur Petrosyan int dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3266197ba5f4SPaul Zimmerman {
3267197ba5f4SPaul Zimmerman 	unsigned long flags;
3268197ba5f4SPaul Zimmerman 	u32 pcgctl;
3269197ba5f4SPaul Zimmerman 	u32 gotgctl;
327022ff0c8eSArtur Petrosyan 	int ret = 0;
3271197ba5f4SPaul Zimmerman 
3272197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3273197ba5f4SPaul Zimmerman 
3274197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
3275197ba5f4SPaul Zimmerman 
3276197ba5f4SPaul Zimmerman 	if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
3277f25c42b8SGevorg Sahakyan 		gotgctl = dwc2_readl(hsotg, GOTGCTL);
3278197ba5f4SPaul Zimmerman 		gotgctl |= GOTGCTL_HSTSETHNPEN;
3279f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gotgctl, GOTGCTL);
3280197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_A_SUSPEND;
3281197ba5f4SPaul Zimmerman 	}
3282197ba5f4SPaul Zimmerman 
328322ff0c8eSArtur Petrosyan 	switch (hsotg->params.power_down) {
328422ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
328522ff0c8eSArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
328622ff0c8eSArtur Petrosyan 		if (ret)
328722ff0c8eSArtur Petrosyan 			dev_err(hsotg->dev,
328822ff0c8eSArtur Petrosyan 				"enter partial_power_down failed.\n");
328922ff0c8eSArtur Petrosyan 		break;
329022ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
32918f7f8689SArtur Petrosyan 		/*
32928f7f8689SArtur Petrosyan 		 * Perform spin unlock and lock because in
32938f7f8689SArtur Petrosyan 		 * "dwc2_host_enter_hibernation()" function there is a spinlock
32948f7f8689SArtur Petrosyan 		 * logic which prevents servicing of any IRQ during entering
32958f7f8689SArtur Petrosyan 		 * hibernation.
32968f7f8689SArtur Petrosyan 		 */
32978f7f8689SArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
32988f7f8689SArtur Petrosyan 		ret = dwc2_enter_hibernation(hsotg, 1);
32998f7f8689SArtur Petrosyan 		if (ret)
33008f7f8689SArtur Petrosyan 			dev_err(hsotg->dev, "enter hibernation failed.\n");
33018f7f8689SArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
33028f7f8689SArtur Petrosyan 		break;
330322ff0c8eSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3304a2a23d3fSGregory Herrero 		/*
3305d37b939cSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
3306d37b939cSArtur Petrosyan 		 * clock gating is used to save power.
3307a2a23d3fSGregory Herrero 		 */
3308c4a0f7a6SMarek Szyprowski 		if (!hsotg->params.no_clock_gating)
3309d37b939cSArtur Petrosyan 			dwc2_host_enter_clock_gating(hsotg);
3310d37b939cSArtur Petrosyan 		break;
331122ff0c8eSArtur Petrosyan 	}
3312197ba5f4SPaul Zimmerman 
3313197ba5f4SPaul Zimmerman 	/* For HNP the bus must be suspended for at least 200ms */
3314197ba5f4SPaul Zimmerman 	if (dwc2_host_is_b_hnp_enabled(hsotg)) {
3315f25c42b8SGevorg Sahakyan 		pcgctl = dwc2_readl(hsotg, PCGCTL);
3316197ba5f4SPaul Zimmerman 		pcgctl &= ~PCGCTL_STOPPCLK;
3317f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgctl, PCGCTL);
3318197ba5f4SPaul Zimmerman 
3319197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3320197ba5f4SPaul Zimmerman 
332104a9db79SNicholas Mc Guire 		msleep(200);
3322197ba5f4SPaul Zimmerman 	} else {
3323197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
3324197ba5f4SPaul Zimmerman 	}
332522ff0c8eSArtur Petrosyan 
332622ff0c8eSArtur Petrosyan 	return ret;
3327197ba5f4SPaul Zimmerman }
3328197ba5f4SPaul Zimmerman 
3329139fae7aSArtur Petrosyan /**
3330139fae7aSArtur Petrosyan  * dwc2_port_resume() - Exit controller from suspend mode for host.
3331139fae7aSArtur Petrosyan  *
3332139fae7aSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
3333139fae7aSArtur Petrosyan  *
33341e0890ebSArtur Petrosyan  * Return: non-zero if failed to exit suspend mode for host.
33351e0890ebSArtur Petrosyan  *
3336139fae7aSArtur Petrosyan  * This function is for exiting Host mode suspend.
3337139fae7aSArtur Petrosyan  * Must NOT be called with interrupt disabled or spinlock held.
3338139fae7aSArtur Petrosyan  */
33391e0890ebSArtur Petrosyan int dwc2_port_resume(struct dwc2_hsotg *hsotg)
334030db103cSGregory Herrero {
334130db103cSGregory Herrero 	unsigned long flags;
33421e0890ebSArtur Petrosyan 	int ret = 0;
334330db103cSGregory Herrero 
33444d273c2aSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
33454d273c2aSDouglas Anderson 
33461e0890ebSArtur Petrosyan 	switch (hsotg->params.power_down) {
33471e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
33481e0890ebSArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
33491e0890ebSArtur Petrosyan 		if (ret)
33501e0890ebSArtur Petrosyan 			dev_err(hsotg->dev,
33511e0890ebSArtur Petrosyan 				"exit partial_power_down failed.\n");
33521e0890ebSArtur Petrosyan 		break;
33531e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
3354e358c215SArtur Petrosyan 		/* Exit host hibernation. */
3355e358c215SArtur Petrosyan 		ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
3356e358c215SArtur Petrosyan 		if (ret)
3357e358c215SArtur Petrosyan 			dev_err(hsotg->dev, "exit hibernation failed.\n");
3358e358c215SArtur Petrosyan 		break;
33591e0890ebSArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
3360a2a23d3fSGregory Herrero 		/*
33613cf8143eSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
33623cf8143eSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
3363a2a23d3fSGregory Herrero 		 */
33644d273c2aSDouglas Anderson 		spin_unlock_irqrestore(&hsotg->lock, flags);
33653cf8143eSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
33664d273c2aSDouglas Anderson 		spin_lock_irqsave(&hsotg->lock, flags);
33673cf8143eSArtur Petrosyan 		break;
33681e0890ebSArtur Petrosyan 	}
33691e0890ebSArtur Petrosyan 
337030db103cSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
33711e0890ebSArtur Petrosyan 
33721e0890ebSArtur Petrosyan 	return ret;
337330db103cSGregory Herrero }
337430db103cSGregory Herrero 
3375197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
3376197ba5f4SPaul Zimmerman static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3377197ba5f4SPaul Zimmerman 				u16 wvalue, u16 windex, char *buf, u16 wlength)
3378197ba5f4SPaul Zimmerman {
3379197ba5f4SPaul Zimmerman 	struct usb_hub_descriptor *hub_desc;
3380197ba5f4SPaul Zimmerman 	int retval = 0;
3381197ba5f4SPaul Zimmerman 	u32 hprt0;
3382197ba5f4SPaul Zimmerman 	u32 port_status;
3383197ba5f4SPaul Zimmerman 	u32 speed;
3384197ba5f4SPaul Zimmerman 	u32 pcgctl;
3385cd7cd0e6SFabrice Gasnier 	u32 pwr;
3386197ba5f4SPaul Zimmerman 
3387197ba5f4SPaul Zimmerman 	switch (typereq) {
3388197ba5f4SPaul Zimmerman 	case ClearHubFeature:
3389197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3390197ba5f4SPaul Zimmerman 
3391197ba5f4SPaul Zimmerman 		switch (wvalue) {
3392197ba5f4SPaul Zimmerman 		case C_HUB_LOCAL_POWER:
3393197ba5f4SPaul Zimmerman 		case C_HUB_OVER_CURRENT:
3394197ba5f4SPaul Zimmerman 			/* Nothing required here */
3395197ba5f4SPaul Zimmerman 			break;
3396197ba5f4SPaul Zimmerman 
3397197ba5f4SPaul Zimmerman 		default:
3398197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3399197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3400197ba5f4SPaul Zimmerman 				"ClearHubFeature request %1xh unknown\n",
3401197ba5f4SPaul Zimmerman 				wvalue);
3402197ba5f4SPaul Zimmerman 		}
3403197ba5f4SPaul Zimmerman 		break;
3404197ba5f4SPaul Zimmerman 
3405197ba5f4SPaul Zimmerman 	case ClearPortFeature:
3406197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_L1)
3407197ba5f4SPaul Zimmerman 			if (!windex || windex > 1)
3408197ba5f4SPaul Zimmerman 				goto error;
3409197ba5f4SPaul Zimmerman 		switch (wvalue) {
3410197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_ENABLE:
3411197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3412197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3413197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3414197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_ENA;
3415f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3416197ba5f4SPaul Zimmerman 			break;
3417197ba5f4SPaul Zimmerman 
3418197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3419197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3420197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
3421b0bb9bb6SPaul Zimmerman 
3422e358c215SArtur Petrosyan 			if (hsotg->bus_suspended)
3423e358c215SArtur Petrosyan 				retval = dwc2_port_resume(hsotg);
3424197ba5f4SPaul Zimmerman 			break;
3425197ba5f4SPaul Zimmerman 
3426197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3427197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3428197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_POWER\n");
3429197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3430cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3431197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_PWR;
3432f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3433cd7cd0e6SFabrice Gasnier 			if (pwr)
3434cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_exit(hsotg);
3435197ba5f4SPaul Zimmerman 			break;
3436197ba5f4SPaul Zimmerman 
3437197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3438197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3439197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3440197ba5f4SPaul Zimmerman 			/* Port indicator not supported */
3441197ba5f4SPaul Zimmerman 			break;
3442197ba5f4SPaul Zimmerman 
3443197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_CONNECTION:
3444197ba5f4SPaul Zimmerman 			/*
3445197ba5f4SPaul Zimmerman 			 * Clears driver's internal Connect Status Change flag
3446197ba5f4SPaul Zimmerman 			 */
3447197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3448197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3449197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change = 0;
3450197ba5f4SPaul Zimmerman 			break;
3451197ba5f4SPaul Zimmerman 
3452197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_RESET:
3453197ba5f4SPaul Zimmerman 			/* Clears driver's internal Port Reset Change flag */
3454197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3455197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3456197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change = 0;
3457197ba5f4SPaul Zimmerman 			break;
3458197ba5f4SPaul Zimmerman 
3459197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_ENABLE:
3460197ba5f4SPaul Zimmerman 			/*
3461197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Enable/Disable
3462197ba5f4SPaul Zimmerman 			 * Change flag
3463197ba5f4SPaul Zimmerman 			 */
3464197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3465197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3466197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change = 0;
3467197ba5f4SPaul Zimmerman 			break;
3468197ba5f4SPaul Zimmerman 
3469197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_SUSPEND:
3470197ba5f4SPaul Zimmerman 			/*
3471197ba5f4SPaul Zimmerman 			 * Clears the driver's internal Port Suspend Change
3472197ba5f4SPaul Zimmerman 			 * flag, which is set when resume signaling on the host
3473197ba5f4SPaul Zimmerman 			 * port is complete
3474197ba5f4SPaul Zimmerman 			 */
3475197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3476197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3477197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change = 0;
3478197ba5f4SPaul Zimmerman 			break;
3479197ba5f4SPaul Zimmerman 
3480197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_PORT_L1:
3481197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3482197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3483197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_l1_change = 0;
3484197ba5f4SPaul Zimmerman 			break;
3485197ba5f4SPaul Zimmerman 
3486197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_C_OVER_CURRENT:
3487197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3488197ba5f4SPaul Zimmerman 				"ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3489197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change = 0;
3490197ba5f4SPaul Zimmerman 			break;
3491197ba5f4SPaul Zimmerman 
3492197ba5f4SPaul Zimmerman 		default:
3493197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3494197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3495197ba5f4SPaul Zimmerman 				"ClearPortFeature request %1xh unknown or unsupported\n",
3496197ba5f4SPaul Zimmerman 				wvalue);
3497197ba5f4SPaul Zimmerman 		}
3498197ba5f4SPaul Zimmerman 		break;
3499197ba5f4SPaul Zimmerman 
3500197ba5f4SPaul Zimmerman 	case GetHubDescriptor:
3501197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3502197ba5f4SPaul Zimmerman 		hub_desc = (struct usb_hub_descriptor *)buf;
3503197ba5f4SPaul Zimmerman 		hub_desc->bDescLength = 9;
3504a5dd0395SSergei Shtylyov 		hub_desc->bDescriptorType = USB_DT_HUB;
3505197ba5f4SPaul Zimmerman 		hub_desc->bNbrPorts = 1;
35063d040de8SSergei Shtylyov 		hub_desc->wHubCharacteristics =
35073d040de8SSergei Shtylyov 			cpu_to_le16(HUB_CHAR_COMMON_LPSM |
35083d040de8SSergei Shtylyov 				    HUB_CHAR_INDV_PORT_OCPM);
3509197ba5f4SPaul Zimmerman 		hub_desc->bPwrOn2PwrGood = 1;
3510197ba5f4SPaul Zimmerman 		hub_desc->bHubContrCurrent = 0;
3511197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[0] = 0;
3512197ba5f4SPaul Zimmerman 		hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3513197ba5f4SPaul Zimmerman 		break;
3514197ba5f4SPaul Zimmerman 
3515197ba5f4SPaul Zimmerman 	case GetHubStatus:
3516197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "GetHubStatus\n");
3517197ba5f4SPaul Zimmerman 		memset(buf, 0, 4);
3518197ba5f4SPaul Zimmerman 		break;
3519197ba5f4SPaul Zimmerman 
3520197ba5f4SPaul Zimmerman 	case GetPortStatus:
3521197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3522197ba5f4SPaul Zimmerman 			 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3523197ba5f4SPaul Zimmerman 			 hsotg->flags.d32);
3524197ba5f4SPaul Zimmerman 		if (!windex || windex > 1)
3525197ba5f4SPaul Zimmerman 			goto error;
3526197ba5f4SPaul Zimmerman 
3527197ba5f4SPaul Zimmerman 		port_status = 0;
3528197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_connect_status_change)
3529197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3530197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_enable_change)
3531197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_ENABLE << 16;
3532197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_suspend_change)
3533197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3534197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_l1_change)
3535197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_L1 << 16;
3536197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_reset_change)
3537197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_RESET << 16;
3538197ba5f4SPaul Zimmerman 		if (hsotg->flags.b.port_over_current_change) {
3539197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "Overcurrent change detected\n");
3540197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3541197ba5f4SPaul Zimmerman 		}
3542197ba5f4SPaul Zimmerman 
3543197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3544197ba5f4SPaul Zimmerman 			/*
3545197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3546197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3547197ba5f4SPaul Zimmerman 			 * return 0's for the remainder of the port status
3548197ba5f4SPaul Zimmerman 			 * since the port register can't be read if the core
3549197ba5f4SPaul Zimmerman 			 * is in device mode.
3550197ba5f4SPaul Zimmerman 			 */
3551197ba5f4SPaul Zimmerman 			*(__le32 *)buf = cpu_to_le32(port_status);
3552197ba5f4SPaul Zimmerman 			break;
3553197ba5f4SPaul Zimmerman 		}
3554197ba5f4SPaul Zimmerman 
3555f25c42b8SGevorg Sahakyan 		hprt0 = dwc2_readl(hsotg, HPRT0);
3556197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "  HPRT0: 0x%08x\n", hprt0);
3557197ba5f4SPaul Zimmerman 
3558197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_CONNSTS)
3559197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_CONNECTION;
3560197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_ENA)
3561197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_ENABLE;
3562197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_SUSP)
3563197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_SUSPEND;
3564197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_OVRCURRACT)
3565197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_OVERCURRENT;
3566197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_RST)
3567197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_RESET;
3568197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_PWR)
3569197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_POWER;
3570197ba5f4SPaul Zimmerman 
3571197ba5f4SPaul Zimmerman 		speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
3572197ba5f4SPaul Zimmerman 		if (speed == HPRT0_SPD_HIGH_SPEED)
3573197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_HIGH_SPEED;
3574197ba5f4SPaul Zimmerman 		else if (speed == HPRT0_SPD_LOW_SPEED)
3575197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_LOW_SPEED;
3576197ba5f4SPaul Zimmerman 
3577197ba5f4SPaul Zimmerman 		if (hprt0 & HPRT0_TSTCTL_MASK)
3578197ba5f4SPaul Zimmerman 			port_status |= USB_PORT_STAT_TEST;
3579197ba5f4SPaul Zimmerman 		/* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3580197ba5f4SPaul Zimmerman 
3581bea8e86cSJohn Youn 		if (hsotg->params.dma_desc_fs_enable) {
3582fbb9e22bSMian Yousaf Kaukab 			/*
3583fbb9e22bSMian Yousaf Kaukab 			 * Enable descriptor DMA only if a full speed
3584fbb9e22bSMian Yousaf Kaukab 			 * device is connected.
3585fbb9e22bSMian Yousaf Kaukab 			 */
3586fbb9e22bSMian Yousaf Kaukab 			if (hsotg->new_connection &&
3587fbb9e22bSMian Yousaf Kaukab 			    ((port_status &
3588fbb9e22bSMian Yousaf Kaukab 			      (USB_PORT_STAT_CONNECTION |
3589fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_HIGH_SPEED |
3590fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_LOW_SPEED)) ==
3591fbb9e22bSMian Yousaf Kaukab 			       USB_PORT_STAT_CONNECTION)) {
3592fbb9e22bSMian Yousaf Kaukab 				u32 hcfg;
3593fbb9e22bSMian Yousaf Kaukab 
3594fbb9e22bSMian Yousaf Kaukab 				dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
359595832c00SJohn Youn 				hsotg->params.dma_desc_enable = true;
3596f25c42b8SGevorg Sahakyan 				hcfg = dwc2_readl(hsotg, HCFG);
3597fbb9e22bSMian Yousaf Kaukab 				hcfg |= HCFG_DESCDMA;
3598f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hcfg, HCFG);
3599fbb9e22bSMian Yousaf Kaukab 				hsotg->new_connection = false;
3600fbb9e22bSMian Yousaf Kaukab 			}
3601fbb9e22bSMian Yousaf Kaukab 		}
3602fbb9e22bSMian Yousaf Kaukab 
3603197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
3604197ba5f4SPaul Zimmerman 		*(__le32 *)buf = cpu_to_le32(port_status);
3605197ba5f4SPaul Zimmerman 		break;
3606197ba5f4SPaul Zimmerman 
3607197ba5f4SPaul Zimmerman 	case SetHubFeature:
3608197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetHubFeature\n");
3609197ba5f4SPaul Zimmerman 		/* No HUB features supported */
3610197ba5f4SPaul Zimmerman 		break;
3611197ba5f4SPaul Zimmerman 
3612197ba5f4SPaul Zimmerman 	case SetPortFeature:
3613197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "SetPortFeature\n");
3614197ba5f4SPaul Zimmerman 		if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3615197ba5f4SPaul Zimmerman 			goto error;
3616197ba5f4SPaul Zimmerman 
3617197ba5f4SPaul Zimmerman 		if (!hsotg->flags.b.port_connect_status) {
3618197ba5f4SPaul Zimmerman 			/*
3619197ba5f4SPaul Zimmerman 			 * The port is disconnected, which means the core is
3620197ba5f4SPaul Zimmerman 			 * either in device mode or it soon will be. Just
3621197ba5f4SPaul Zimmerman 			 * return without doing anything since the port
3622197ba5f4SPaul Zimmerman 			 * register can't be written if the core is in device
3623197ba5f4SPaul Zimmerman 			 * mode.
3624197ba5f4SPaul Zimmerman 			 */
3625197ba5f4SPaul Zimmerman 			break;
3626197ba5f4SPaul Zimmerman 		}
3627197ba5f4SPaul Zimmerman 
3628197ba5f4SPaul Zimmerman 		switch (wvalue) {
3629197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_SUSPEND:
3630197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3631197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3632197ba5f4SPaul Zimmerman 			if (windex != hsotg->otg_port)
3633197ba5f4SPaul Zimmerman 				goto error;
36348f7f8689SArtur Petrosyan 			if (!hsotg->bus_suspended)
36358f7f8689SArtur Petrosyan 				retval = dwc2_port_suspend(hsotg, windex);
3636197ba5f4SPaul Zimmerman 			break;
3637197ba5f4SPaul Zimmerman 
3638197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_POWER:
3639197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3640197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_POWER\n");
3641197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3642cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3643197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_PWR;
3644f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3645cd7cd0e6SFabrice Gasnier 			if (!pwr)
3646cd7cd0e6SFabrice Gasnier 				dwc2_vbus_supply_init(hsotg);
3647197ba5f4SPaul Zimmerman 			break;
3648197ba5f4SPaul Zimmerman 
3649197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_RESET:
3650c363af9cSArtur Petrosyan 			dev_dbg(hsotg->dev,
3651c363af9cSArtur Petrosyan 				"SetPortFeature - USB_PORT_FEAT_RESET\n");
3652c363af9cSArtur Petrosyan 
3653c363af9cSArtur Petrosyan 			hprt0 = dwc2_read_hprt0(hsotg);
3654c363af9cSArtur Petrosyan 
3655c363af9cSArtur Petrosyan 			if (hsotg->hibernated) {
3656c363af9cSArtur Petrosyan 				retval = dwc2_exit_hibernation(hsotg, 0, 1, 1);
3657c363af9cSArtur Petrosyan 				if (retval)
3658c363af9cSArtur Petrosyan 					dev_err(hsotg->dev,
3659c363af9cSArtur Petrosyan 						"exit hibernation failed\n");
3660c363af9cSArtur Petrosyan 			}
3661e97570f7SArtur Petrosyan 
3662e97570f7SArtur Petrosyan 			if (hsotg->in_ppd) {
3663e97570f7SArtur Petrosyan 				retval = dwc2_exit_partial_power_down(hsotg, 1,
3664e97570f7SArtur Petrosyan 								      true);
3665e97570f7SArtur Petrosyan 				if (retval)
3666e97570f7SArtur Petrosyan 					dev_err(hsotg->dev,
3667e97570f7SArtur Petrosyan 						"exit partial_power_down failed\n");
3668e97570f7SArtur Petrosyan 			}
3669e97570f7SArtur Petrosyan 
36705f9e60c0SArtur Petrosyan 			if (hsotg->params.power_down ==
36715f9e60c0SArtur Petrosyan 			    DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended)
36725f9e60c0SArtur Petrosyan 				dwc2_host_exit_clock_gating(hsotg, 0);
36735f9e60c0SArtur Petrosyan 
3674f25c42b8SGevorg Sahakyan 			pcgctl = dwc2_readl(hsotg, PCGCTL);
3675197ba5f4SPaul Zimmerman 			pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
3676f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, pcgctl, PCGCTL);
3677197ba5f4SPaul Zimmerman 			/* ??? Original driver does this */
3678f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, 0, PCGCTL);
3679197ba5f4SPaul Zimmerman 
3680197ba5f4SPaul Zimmerman 			hprt0 = dwc2_read_hprt0(hsotg);
3681cd7cd0e6SFabrice Gasnier 			pwr = hprt0 & HPRT0_PWR;
3682197ba5f4SPaul Zimmerman 			/* Clear suspend bit if resetting from suspend state */
3683197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_SUSP;
3684197ba5f4SPaul Zimmerman 
3685197ba5f4SPaul Zimmerman 			/*
3686197ba5f4SPaul Zimmerman 			 * When B-Host the Port reset bit is set in the Start
3687197ba5f4SPaul Zimmerman 			 * HCD Callback function, so that the reset is started
3688197ba5f4SPaul Zimmerman 			 * within 1ms of the HNP success interrupt
3689197ba5f4SPaul Zimmerman 			 */
3690197ba5f4SPaul Zimmerman 			if (!dwc2_hcd_is_b_host(hsotg)) {
3691197ba5f4SPaul Zimmerman 				hprt0 |= HPRT0_PWR | HPRT0_RST;
3692197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3693197ba5f4SPaul Zimmerman 					"In host mode, hprt0=%08x\n", hprt0);
3694f25c42b8SGevorg Sahakyan 				dwc2_writel(hsotg, hprt0, HPRT0);
3695cd7cd0e6SFabrice Gasnier 				if (!pwr)
3696cd7cd0e6SFabrice Gasnier 					dwc2_vbus_supply_init(hsotg);
3697197ba5f4SPaul Zimmerman 			}
3698197ba5f4SPaul Zimmerman 
3699197ba5f4SPaul Zimmerman 			/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
370004a9db79SNicholas Mc Guire 			msleep(50);
3701197ba5f4SPaul Zimmerman 			hprt0 &= ~HPRT0_RST;
3702f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
3703197ba5f4SPaul Zimmerman 			hsotg->lx_state = DWC2_L0; /* Now back to On state */
3704197ba5f4SPaul Zimmerman 			break;
3705197ba5f4SPaul Zimmerman 
3706197ba5f4SPaul Zimmerman 		case USB_PORT_FEAT_INDICATOR:
3707197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev,
3708197ba5f4SPaul Zimmerman 				"SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3709197ba5f4SPaul Zimmerman 			/* Not supported */
3710197ba5f4SPaul Zimmerman 			break;
3711197ba5f4SPaul Zimmerman 
371296d480e6SJingwu Lin 		case USB_PORT_FEAT_TEST:
371396d480e6SJingwu Lin 			hprt0 = dwc2_read_hprt0(hsotg);
371496d480e6SJingwu Lin 			dev_dbg(hsotg->dev,
371596d480e6SJingwu Lin 				"SetPortFeature - USB_PORT_FEAT_TEST\n");
371696d480e6SJingwu Lin 			hprt0 &= ~HPRT0_TSTCTL_MASK;
371796d480e6SJingwu Lin 			hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
3718f25c42b8SGevorg Sahakyan 			dwc2_writel(hsotg, hprt0, HPRT0);
371996d480e6SJingwu Lin 			break;
372096d480e6SJingwu Lin 
3721197ba5f4SPaul Zimmerman 		default:
3722197ba5f4SPaul Zimmerman 			retval = -EINVAL;
3723197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3724197ba5f4SPaul Zimmerman 				"SetPortFeature %1xh unknown or unsupported\n",
3725197ba5f4SPaul Zimmerman 				wvalue);
3726197ba5f4SPaul Zimmerman 			break;
3727197ba5f4SPaul Zimmerman 		}
3728197ba5f4SPaul Zimmerman 		break;
3729197ba5f4SPaul Zimmerman 
3730197ba5f4SPaul Zimmerman 	default:
3731197ba5f4SPaul Zimmerman error:
3732197ba5f4SPaul Zimmerman 		retval = -EINVAL;
3733197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3734197ba5f4SPaul Zimmerman 			"Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3735197ba5f4SPaul Zimmerman 			typereq, windex, wvalue);
3736197ba5f4SPaul Zimmerman 		break;
3737197ba5f4SPaul Zimmerman 	}
3738197ba5f4SPaul Zimmerman 
3739197ba5f4SPaul Zimmerman 	return retval;
3740197ba5f4SPaul Zimmerman }
3741197ba5f4SPaul Zimmerman 
3742197ba5f4SPaul Zimmerman static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3743197ba5f4SPaul Zimmerman {
3744197ba5f4SPaul Zimmerman 	int retval;
3745197ba5f4SPaul Zimmerman 
3746197ba5f4SPaul Zimmerman 	if (port != 1)
3747197ba5f4SPaul Zimmerman 		return -EINVAL;
3748197ba5f4SPaul Zimmerman 
3749197ba5f4SPaul Zimmerman 	retval = (hsotg->flags.b.port_connect_status_change ||
3750197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_reset_change ||
3751197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_enable_change ||
3752197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_suspend_change ||
3753197ba5f4SPaul Zimmerman 		  hsotg->flags.b.port_over_current_change);
3754197ba5f4SPaul Zimmerman 
3755197ba5f4SPaul Zimmerman 	if (retval) {
3756197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3757197ba5f4SPaul Zimmerman 			"DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3758197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_connect_status_change: %d\n",
3759197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_connect_status_change);
3760197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_reset_change: %d\n",
3761197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_reset_change);
3762197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_enable_change: %d\n",
3763197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_enable_change);
3764197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_suspend_change: %d\n",
3765197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_suspend_change);
3766197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  port_over_current_change: %d\n",
3767197ba5f4SPaul Zimmerman 			hsotg->flags.b.port_over_current_change);
3768197ba5f4SPaul Zimmerman 	}
3769197ba5f4SPaul Zimmerman 
3770197ba5f4SPaul Zimmerman 	return retval;
3771197ba5f4SPaul Zimmerman }
3772197ba5f4SPaul Zimmerman 
3773197ba5f4SPaul Zimmerman int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3774197ba5f4SPaul Zimmerman {
3775f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3776197ba5f4SPaul Zimmerman 
3777197ba5f4SPaul Zimmerman #ifdef DWC2_DEBUG_SOF
3778197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
3779197ba5f4SPaul Zimmerman 		 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
3780197ba5f4SPaul Zimmerman #endif
3781197ba5f4SPaul Zimmerman 	return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3782197ba5f4SPaul Zimmerman }
3783197ba5f4SPaul Zimmerman 
3784fae4e826SDouglas Anderson int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3785fae4e826SDouglas Anderson {
3786f25c42b8SGevorg Sahakyan 	u32 hprt = dwc2_readl(hsotg, HPRT0);
3787f25c42b8SGevorg Sahakyan 	u32 hfir = dwc2_readl(hsotg, HFIR);
3788f25c42b8SGevorg Sahakyan 	u32 hfnum = dwc2_readl(hsotg, HFNUM);
3789fae4e826SDouglas Anderson 	unsigned int us_per_frame;
3790fae4e826SDouglas Anderson 	unsigned int frame_number;
3791fae4e826SDouglas Anderson 	unsigned int remaining;
3792fae4e826SDouglas Anderson 	unsigned int interval;
3793fae4e826SDouglas Anderson 	unsigned int phy_clks;
3794fae4e826SDouglas Anderson 
3795fae4e826SDouglas Anderson 	/* High speed has 125 us per (micro) frame; others are 1 ms per */
3796fae4e826SDouglas Anderson 	us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3797fae4e826SDouglas Anderson 
3798fae4e826SDouglas Anderson 	/* Extract fields */
3799fae4e826SDouglas Anderson 	frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3800fae4e826SDouglas Anderson 	remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3801fae4e826SDouglas Anderson 	interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3802fae4e826SDouglas Anderson 
3803fae4e826SDouglas Anderson 	/*
3804fae4e826SDouglas Anderson 	 * Number of phy clocks since the last tick of the frame number after
3805fae4e826SDouglas Anderson 	 * "us" has passed.
3806fae4e826SDouglas Anderson 	 */
3807fae4e826SDouglas Anderson 	phy_clks = (interval - remaining) +
3808fae4e826SDouglas Anderson 		   DIV_ROUND_UP(interval * us, us_per_frame);
3809fae4e826SDouglas Anderson 
3810fae4e826SDouglas Anderson 	return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3811fae4e826SDouglas Anderson }
3812fae4e826SDouglas Anderson 
3813197ba5f4SPaul Zimmerman int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3814197ba5f4SPaul Zimmerman {
3815197ba5f4SPaul Zimmerman 	return hsotg->op_state == OTG_STATE_B_HOST;
3816197ba5f4SPaul Zimmerman }
3817197ba5f4SPaul Zimmerman 
3818197ba5f4SPaul Zimmerman static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3819197ba5f4SPaul Zimmerman 					       int iso_desc_count,
3820197ba5f4SPaul Zimmerman 					       gfp_t mem_flags)
3821197ba5f4SPaul Zimmerman {
3822197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3823197ba5f4SPaul Zimmerman 
3824eeca7606SGustavo A. R. Silva 	urb = kzalloc(struct_size(urb, iso_descs, iso_desc_count), mem_flags);
3825197ba5f4SPaul Zimmerman 	if (urb)
3826197ba5f4SPaul Zimmerman 		urb->packet_count = iso_desc_count;
3827197ba5f4SPaul Zimmerman 	return urb;
3828197ba5f4SPaul Zimmerman }
3829197ba5f4SPaul Zimmerman 
3830197ba5f4SPaul Zimmerman static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3831197ba5f4SPaul Zimmerman 				      struct dwc2_hcd_urb *urb, u8 dev_addr,
3832babd1839SDouglas Anderson 				      u8 ep_num, u8 ep_type, u8 ep_dir,
3833babd1839SDouglas Anderson 				      u16 maxp, u16 maxp_mult)
3834197ba5f4SPaul Zimmerman {
3835197ba5f4SPaul Zimmerman 	if (dbg_perio() ||
3836197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_BULK ||
3837197ba5f4SPaul Zimmerman 	    ep_type == USB_ENDPOINT_XFER_CONTROL)
3838197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
3839babd1839SDouglas Anderson 			 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, maxp=%d (%d mult)\n",
3840babd1839SDouglas Anderson 			 dev_addr, ep_num, ep_dir, ep_type, maxp, maxp_mult);
3841197ba5f4SPaul Zimmerman 	urb->pipe_info.dev_addr = dev_addr;
3842197ba5f4SPaul Zimmerman 	urb->pipe_info.ep_num = ep_num;
3843197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_type = ep_type;
3844197ba5f4SPaul Zimmerman 	urb->pipe_info.pipe_dir = ep_dir;
3845babd1839SDouglas Anderson 	urb->pipe_info.maxp = maxp;
3846babd1839SDouglas Anderson 	urb->pipe_info.maxp_mult = maxp_mult;
3847197ba5f4SPaul Zimmerman }
3848197ba5f4SPaul Zimmerman 
3849197ba5f4SPaul Zimmerman /*
3850197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
3851197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
3852197ba5f4SPaul Zimmerman  */
3853197ba5f4SPaul Zimmerman void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3854197ba5f4SPaul Zimmerman {
3855197ba5f4SPaul Zimmerman #ifdef DEBUG
3856197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
3857197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb;
3858197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
3859197ba5f4SPaul Zimmerman 	int num_channels;
3860197ba5f4SPaul Zimmerman 	u32 np_tx_status;
3861197ba5f4SPaul Zimmerman 	u32 p_tx_status;
3862197ba5f4SPaul Zimmerman 	int i;
3863197ba5f4SPaul Zimmerman 
3864bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
3865197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3866197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3867197ba5f4SPaul Zimmerman 		"************************************************************\n");
3868197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HCD State:\n");
3869197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  Num channels: %d\n", num_channels);
3870197ba5f4SPaul Zimmerman 
3871197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
3872197ba5f4SPaul Zimmerman 		chan = hsotg->hc_ptr_array[i];
3873197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "  Channel %d:\n", i);
3874197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
3875197ba5f4SPaul Zimmerman 			"    dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3876197ba5f4SPaul Zimmerman 			chan->dev_addr, chan->ep_num, chan->ep_is_in);
3877197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    speed: %d\n", chan->speed);
3878197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    ep_type: %d\n", chan->ep_type);
3879197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    max_packet: %d\n", chan->max_packet);
3880197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    data_pid_start: %d\n",
3881197ba5f4SPaul Zimmerman 			chan->data_pid_start);
3882197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    multi_count: %d\n", chan->multi_count);
3883197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_started: %d\n",
3884197ba5f4SPaul Zimmerman 			chan->xfer_started);
3885197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_buf: %p\n", chan->xfer_buf);
3886197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_dma: %08lx\n",
3887197ba5f4SPaul Zimmerman 			(unsigned long)chan->xfer_dma);
3888197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_len: %d\n", chan->xfer_len);
3889197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xfer_count: %d\n", chan->xfer_count);
3890197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_on_queue: %d\n",
3891197ba5f4SPaul Zimmerman 			chan->halt_on_queue);
3892197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_pending: %d\n",
3893197ba5f4SPaul Zimmerman 			chan->halt_pending);
3894197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    halt_status: %d\n", chan->halt_status);
3895197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    do_split: %d\n", chan->do_split);
3896197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    complete_split: %d\n",
3897197ba5f4SPaul Zimmerman 			chan->complete_split);
3898197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_addr: %d\n", chan->hub_addr);
3899197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    hub_port: %d\n", chan->hub_port);
3900197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    xact_pos: %d\n", chan->xact_pos);
3901197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    requests: %d\n", chan->requests);
3902197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "    qh: %p\n", chan->qh);
3903197ba5f4SPaul Zimmerman 
3904197ba5f4SPaul Zimmerman 		if (chan->xfer_started) {
3905197ba5f4SPaul Zimmerman 			u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3906197ba5f4SPaul Zimmerman 
3907f25c42b8SGevorg Sahakyan 			hfnum = dwc2_readl(hsotg, HFNUM);
3908f25c42b8SGevorg Sahakyan 			hcchar = dwc2_readl(hsotg, HCCHAR(i));
3909f25c42b8SGevorg Sahakyan 			hctsiz = dwc2_readl(hsotg, HCTSIZ(i));
3910f25c42b8SGevorg Sahakyan 			hcint = dwc2_readl(hsotg, HCINT(i));
3911f25c42b8SGevorg Sahakyan 			hcintmsk = dwc2_readl(hsotg, HCINTMSK(i));
3912197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hfnum: 0x%08x\n", hfnum);
3913197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcchar: 0x%08x\n", hcchar);
3914197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hctsiz: 0x%08x\n", hctsiz);
3915197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcint: 0x%08x\n", hcint);
3916197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    hcintmsk: 0x%08x\n", hcintmsk);
3917197ba5f4SPaul Zimmerman 		}
3918197ba5f4SPaul Zimmerman 
3919197ba5f4SPaul Zimmerman 		if (!(chan->xfer_started && chan->qh))
3920197ba5f4SPaul Zimmerman 			continue;
3921197ba5f4SPaul Zimmerman 
3922197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3923197ba5f4SPaul Zimmerman 			if (!qtd->in_process)
3924197ba5f4SPaul Zimmerman 				break;
3925197ba5f4SPaul Zimmerman 			urb = qtd->urb;
3926197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "    URB Info:\n");
3927197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "      qtd: %p, urb: %p\n",
3928197ba5f4SPaul Zimmerman 				qtd, urb);
3929197ba5f4SPaul Zimmerman 			if (urb) {
3930197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3931197ba5f4SPaul Zimmerman 					"      Dev: %d, EP: %d %s\n",
3932197ba5f4SPaul Zimmerman 					dwc2_hcd_get_dev_addr(&urb->pipe_info),
3933197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_num(&urb->pipe_info),
3934197ba5f4SPaul Zimmerman 					dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3935197ba5f4SPaul Zimmerman 					"IN" : "OUT");
3936197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3937babd1839SDouglas Anderson 					"      Max packet size: %d (%d mult)\n",
3938babd1839SDouglas Anderson 					dwc2_hcd_get_maxp(&urb->pipe_info),
3939babd1839SDouglas Anderson 					dwc2_hcd_get_maxp_mult(&urb->pipe_info));
3940197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3941197ba5f4SPaul Zimmerman 					"      transfer_buffer: %p\n",
3942197ba5f4SPaul Zimmerman 					urb->buf);
3943197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3944197ba5f4SPaul Zimmerman 					"      transfer_dma: %08lx\n",
3945197ba5f4SPaul Zimmerman 					(unsigned long)urb->dma);
3946197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev,
3947197ba5f4SPaul Zimmerman 					"      transfer_buffer_length: %d\n",
3948197ba5f4SPaul Zimmerman 					urb->length);
3949197ba5f4SPaul Zimmerman 				dev_dbg(hsotg->dev, "      actual_length: %d\n",
3950197ba5f4SPaul Zimmerman 					urb->actual_length);
3951197ba5f4SPaul Zimmerman 			}
3952197ba5f4SPaul Zimmerman 		}
3953197ba5f4SPaul Zimmerman 	}
3954197ba5f4SPaul Zimmerman 
3955197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  non_periodic_channels: %d\n",
3956197ba5f4SPaul Zimmerman 		hsotg->non_periodic_channels);
3957197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_channels: %d\n",
3958197ba5f4SPaul Zimmerman 		hsotg->periodic_channels);
3959197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  periodic_usecs: %d\n", hsotg->periodic_usecs);
3960f25c42b8SGevorg Sahakyan 	np_tx_status = dwc2_readl(hsotg, GNPTXSTS);
3961197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx Req Queue Space Avail: %d\n",
3962197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3963197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  NP Tx FIFO Space Avail: %d\n",
3964197ba5f4SPaul Zimmerman 		(np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3965f25c42b8SGevorg Sahakyan 	p_tx_status = dwc2_readl(hsotg, HPTXSTS);
3966197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx Req Queue Space Avail: %d\n",
3967197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3968197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  P Tx FIFO Space Avail: %d\n",
3969197ba5f4SPaul Zimmerman 		(p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3970197ba5f4SPaul Zimmerman 	dwc2_dump_global_registers(hsotg);
3971197ba5f4SPaul Zimmerman 	dwc2_dump_host_registers(hsotg);
3972197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
3973197ba5f4SPaul Zimmerman 		"************************************************************\n");
3974197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3975197ba5f4SPaul Zimmerman #endif
3976197ba5f4SPaul Zimmerman }
3977197ba5f4SPaul Zimmerman 
3978197ba5f4SPaul Zimmerman struct wrapper_priv_data {
3979197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg;
3980197ba5f4SPaul Zimmerman };
3981197ba5f4SPaul Zimmerman 
3982197ba5f4SPaul Zimmerman /* Gets the dwc2_hsotg from a usb_hcd */
3983197ba5f4SPaul Zimmerman static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
3984197ba5f4SPaul Zimmerman {
3985197ba5f4SPaul Zimmerman 	struct wrapper_priv_data *p;
3986197ba5f4SPaul Zimmerman 
3987197ba5f4SPaul Zimmerman 	p = (struct wrapper_priv_data *)&hcd->hcd_priv;
3988197ba5f4SPaul Zimmerman 	return p->hsotg;
3989197ba5f4SPaul Zimmerman }
3990197ba5f4SPaul Zimmerman 
39919f9f09b0SDouglas Anderson /**
39929f9f09b0SDouglas Anderson  * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
39939f9f09b0SDouglas Anderson  *
39949f9f09b0SDouglas Anderson  * This will get the dwc2_tt structure (and ttport) associated with the given
39959f9f09b0SDouglas Anderson  * context (which is really just a struct urb pointer).
39969f9f09b0SDouglas Anderson  *
39979f9f09b0SDouglas Anderson  * The first time this is called for a given TT we allocate memory for our
39989f9f09b0SDouglas Anderson  * structure.  When everyone is done and has called dwc2_host_put_tt_info()
39999f9f09b0SDouglas Anderson  * then the refcount for the structure will go to 0 and we'll free it.
40009f9f09b0SDouglas Anderson  *
40019f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40029f9f09b0SDouglas Anderson  * @context:   The priv pointer from a struct dwc2_hcd_urb.
40039f9f09b0SDouglas Anderson  * @mem_flags: Flags for allocating memory.
40049f9f09b0SDouglas Anderson  * @ttport:    We'll return this device's port number here.  That's used to
40059f9f09b0SDouglas Anderson  *             reference into the bitmap if we're on a multi_tt hub.
40069f9f09b0SDouglas Anderson  *
40079f9f09b0SDouglas Anderson  * Return: a pointer to a struct dwc2_tt.  Don't forget to call
40089f9f09b0SDouglas Anderson  *         dwc2_host_put_tt_info()!  Returns NULL upon memory alloc failure.
40099f9f09b0SDouglas Anderson  */
40109f9f09b0SDouglas Anderson 
40119f9f09b0SDouglas Anderson struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
40129f9f09b0SDouglas Anderson 				      gfp_t mem_flags, int *ttport)
40139f9f09b0SDouglas Anderson {
40149f9f09b0SDouglas Anderson 	struct urb *urb = context;
40159f9f09b0SDouglas Anderson 	struct dwc2_tt *dwc_tt = NULL;
40169f9f09b0SDouglas Anderson 
40179f9f09b0SDouglas Anderson 	if (urb->dev->tt) {
40189f9f09b0SDouglas Anderson 		*ttport = urb->dev->ttport;
40199f9f09b0SDouglas Anderson 
40209f9f09b0SDouglas Anderson 		dwc_tt = urb->dev->tt->hcpriv;
40219da51974SJohn Youn 		if (!dwc_tt) {
40229f9f09b0SDouglas Anderson 			size_t bitmap_size;
40239f9f09b0SDouglas Anderson 
40249f9f09b0SDouglas Anderson 			/*
40259f9f09b0SDouglas Anderson 			 * For single_tt we need one schedule.  For multi_tt
40269f9f09b0SDouglas Anderson 			 * we need one per port.
40279f9f09b0SDouglas Anderson 			 */
40289f9f09b0SDouglas Anderson 			bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
40299f9f09b0SDouglas Anderson 				      sizeof(dwc_tt->periodic_bitmaps[0]);
40309f9f09b0SDouglas Anderson 			if (urb->dev->tt->multi)
40319f9f09b0SDouglas Anderson 				bitmap_size *= urb->dev->tt->hub->maxchild;
40329f9f09b0SDouglas Anderson 
40339f9f09b0SDouglas Anderson 			dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
40349f9f09b0SDouglas Anderson 					 mem_flags);
40359da51974SJohn Youn 			if (!dwc_tt)
40369f9f09b0SDouglas Anderson 				return NULL;
40379f9f09b0SDouglas Anderson 
40389f9f09b0SDouglas Anderson 			dwc_tt->usb_tt = urb->dev->tt;
40399f9f09b0SDouglas Anderson 			dwc_tt->usb_tt->hcpriv = dwc_tt;
40409f9f09b0SDouglas Anderson 		}
40419f9f09b0SDouglas Anderson 
40429f9f09b0SDouglas Anderson 		dwc_tt->refcount++;
40439f9f09b0SDouglas Anderson 	}
40449f9f09b0SDouglas Anderson 
40459f9f09b0SDouglas Anderson 	return dwc_tt;
40469f9f09b0SDouglas Anderson }
40479f9f09b0SDouglas Anderson 
40489f9f09b0SDouglas Anderson /**
40499f9f09b0SDouglas Anderson  * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
40509f9f09b0SDouglas Anderson  *
40519f9f09b0SDouglas Anderson  * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
40529f9f09b0SDouglas Anderson  * of the structure are done.
40539f9f09b0SDouglas Anderson  *
40549f9f09b0SDouglas Anderson  * It's OK to call this with NULL.
40559f9f09b0SDouglas Anderson  *
40569f9f09b0SDouglas Anderson  * @hsotg:     The HCD state structure for the DWC OTG controller.
40579f9f09b0SDouglas Anderson  * @dwc_tt:    The pointer returned by dwc2_host_get_tt_info.
40589f9f09b0SDouglas Anderson  */
40599f9f09b0SDouglas Anderson void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
40609f9f09b0SDouglas Anderson {
40619f9f09b0SDouglas Anderson 	/* Model kfree and make put of NULL a no-op */
40629da51974SJohn Youn 	if (!dwc_tt)
40639f9f09b0SDouglas Anderson 		return;
40649f9f09b0SDouglas Anderson 
40659f9f09b0SDouglas Anderson 	WARN_ON(dwc_tt->refcount < 1);
40669f9f09b0SDouglas Anderson 
40679f9f09b0SDouglas Anderson 	dwc_tt->refcount--;
40689f9f09b0SDouglas Anderson 	if (!dwc_tt->refcount) {
40699f9f09b0SDouglas Anderson 		dwc_tt->usb_tt->hcpriv = NULL;
40709f9f09b0SDouglas Anderson 		kfree(dwc_tt);
40719f9f09b0SDouglas Anderson 	}
40729f9f09b0SDouglas Anderson }
40739f9f09b0SDouglas Anderson 
4074197ba5f4SPaul Zimmerman int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4075197ba5f4SPaul Zimmerman {
4076197ba5f4SPaul Zimmerman 	struct urb *urb = context;
4077197ba5f4SPaul Zimmerman 
4078197ba5f4SPaul Zimmerman 	return urb->dev->speed;
4079197ba5f4SPaul Zimmerman }
4080197ba5f4SPaul Zimmerman 
4081197ba5f4SPaul Zimmerman static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4082197ba5f4SPaul Zimmerman 					struct urb *urb)
4083197ba5f4SPaul Zimmerman {
4084197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4085197ba5f4SPaul Zimmerman 
4086197ba5f4SPaul Zimmerman 	if (urb->interval)
4087197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated += bw / urb->interval;
4088197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4089197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs++;
4090197ba5f4SPaul Zimmerman 	else
4091197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs++;
4092197ba5f4SPaul Zimmerman }
4093197ba5f4SPaul Zimmerman 
4094197ba5f4SPaul Zimmerman static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4095197ba5f4SPaul Zimmerman 				    struct urb *urb)
4096197ba5f4SPaul Zimmerman {
4097197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4098197ba5f4SPaul Zimmerman 
4099197ba5f4SPaul Zimmerman 	if (urb->interval)
4100197ba5f4SPaul Zimmerman 		bus->bandwidth_allocated -= bw / urb->interval;
4101197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4102197ba5f4SPaul Zimmerman 		bus->bandwidth_isoc_reqs--;
4103197ba5f4SPaul Zimmerman 	else
4104197ba5f4SPaul Zimmerman 		bus->bandwidth_int_reqs--;
4105197ba5f4SPaul Zimmerman }
4106197ba5f4SPaul Zimmerman 
4107197ba5f4SPaul Zimmerman /*
4108197ba5f4SPaul Zimmerman  * Sets the final status of an URB and returns it to the upper layer. Any
4109197ba5f4SPaul Zimmerman  * required cleanup of the URB is performed.
4110197ba5f4SPaul Zimmerman  *
4111197ba5f4SPaul Zimmerman  * Must be called with interrupt disabled and spinlock held
4112197ba5f4SPaul Zimmerman  */
4113197ba5f4SPaul Zimmerman void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4114197ba5f4SPaul Zimmerman 			int status)
4115197ba5f4SPaul Zimmerman {
4116197ba5f4SPaul Zimmerman 	struct urb *urb;
4117197ba5f4SPaul Zimmerman 	int i;
4118197ba5f4SPaul Zimmerman 
4119197ba5f4SPaul Zimmerman 	if (!qtd) {
4120197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4121197ba5f4SPaul Zimmerman 		return;
4122197ba5f4SPaul Zimmerman 	}
4123197ba5f4SPaul Zimmerman 
4124197ba5f4SPaul Zimmerman 	if (!qtd->urb) {
4125197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4126197ba5f4SPaul Zimmerman 		return;
4127197ba5f4SPaul Zimmerman 	}
4128197ba5f4SPaul Zimmerman 
4129197ba5f4SPaul Zimmerman 	urb = qtd->urb->priv;
4130197ba5f4SPaul Zimmerman 	if (!urb) {
4131197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4132197ba5f4SPaul Zimmerman 		return;
4133197ba5f4SPaul Zimmerman 	}
4134197ba5f4SPaul Zimmerman 
4135197ba5f4SPaul Zimmerman 	urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
4136197ba5f4SPaul Zimmerman 
4137197ba5f4SPaul Zimmerman 	if (dbg_urb(urb))
4138197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
4139197ba5f4SPaul Zimmerman 			 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4140197ba5f4SPaul Zimmerman 			 __func__, urb, usb_pipedevice(urb->pipe),
4141197ba5f4SPaul Zimmerman 			 usb_pipeendpoint(urb->pipe),
4142197ba5f4SPaul Zimmerman 			 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4143197ba5f4SPaul Zimmerman 			 urb->actual_length);
4144197ba5f4SPaul Zimmerman 
4145197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4146197ba5f4SPaul Zimmerman 		urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
4147197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; ++i) {
4148197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].actual_length =
4149197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_actual_length(
4150197ba5f4SPaul Zimmerman 						qtd->urb, i);
4151197ba5f4SPaul Zimmerman 			urb->iso_frame_desc[i].status =
4152197ba5f4SPaul Zimmerman 				dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
4153197ba5f4SPaul Zimmerman 		}
4154197ba5f4SPaul Zimmerman 	}
4155197ba5f4SPaul Zimmerman 
4156fe9b1773SGregory Herrero 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4157fe9b1773SGregory Herrero 		for (i = 0; i < urb->number_of_packets; i++)
4158fe9b1773SGregory Herrero 			dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4159fe9b1773SGregory Herrero 				 i, urb->iso_frame_desc[i].status);
4160fe9b1773SGregory Herrero 	}
4161fe9b1773SGregory Herrero 
4162197ba5f4SPaul Zimmerman 	urb->status = status;
4163197ba5f4SPaul Zimmerman 	if (!status) {
4164197ba5f4SPaul Zimmerman 		if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4165197ba5f4SPaul Zimmerman 		    urb->actual_length < urb->transfer_buffer_length)
4166197ba5f4SPaul Zimmerman 			urb->status = -EREMOTEIO;
4167197ba5f4SPaul Zimmerman 	}
4168197ba5f4SPaul Zimmerman 
4169197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4170197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4171197ba5f4SPaul Zimmerman 		struct usb_host_endpoint *ep = urb->ep;
4172197ba5f4SPaul Zimmerman 
4173197ba5f4SPaul Zimmerman 		if (ep)
4174197ba5f4SPaul Zimmerman 			dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4175197ba5f4SPaul Zimmerman 					dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4176197ba5f4SPaul Zimmerman 					urb);
4177197ba5f4SPaul Zimmerman 	}
4178197ba5f4SPaul Zimmerman 
4179197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
4180197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4181197ba5f4SPaul Zimmerman 	kfree(qtd->urb);
4182197ba5f4SPaul Zimmerman 	qtd->urb = NULL;
4183197ba5f4SPaul Zimmerman 
4184197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
4185197ba5f4SPaul Zimmerman }
4186197ba5f4SPaul Zimmerman 
4187197ba5f4SPaul Zimmerman /*
4188197ba5f4SPaul Zimmerman  * Work queue function for starting the HCD when A-Cable is connected
4189197ba5f4SPaul Zimmerman  */
4190197ba5f4SPaul Zimmerman static void dwc2_hcd_start_func(struct work_struct *work)
4191197ba5f4SPaul Zimmerman {
4192197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4193197ba5f4SPaul Zimmerman 						start_work.work);
4194197ba5f4SPaul Zimmerman 
4195197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4196197ba5f4SPaul Zimmerman 	dwc2_host_start(hsotg);
4197197ba5f4SPaul Zimmerman }
4198197ba5f4SPaul Zimmerman 
4199197ba5f4SPaul Zimmerman /*
4200197ba5f4SPaul Zimmerman  * Reset work queue function
4201197ba5f4SPaul Zimmerman  */
4202197ba5f4SPaul Zimmerman static void dwc2_hcd_reset_func(struct work_struct *work)
4203197ba5f4SPaul Zimmerman {
4204197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4205197ba5f4SPaul Zimmerman 						reset_work.work);
42064a065c7bSDouglas Anderson 	unsigned long flags;
4207197ba5f4SPaul Zimmerman 	u32 hprt0;
4208197ba5f4SPaul Zimmerman 
4209197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "USB RESET function called\n");
42104a065c7bSDouglas Anderson 
42114a065c7bSDouglas Anderson 	spin_lock_irqsave(&hsotg->lock, flags);
42124a065c7bSDouglas Anderson 
4213197ba5f4SPaul Zimmerman 	hprt0 = dwc2_read_hprt0(hsotg);
4214197ba5f4SPaul Zimmerman 	hprt0 &= ~HPRT0_RST;
4215f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
4216197ba5f4SPaul Zimmerman 	hsotg->flags.b.port_reset_change = 1;
42174a065c7bSDouglas Anderson 
42184a065c7bSDouglas Anderson 	spin_unlock_irqrestore(&hsotg->lock, flags);
4219197ba5f4SPaul Zimmerman }
4220197ba5f4SPaul Zimmerman 
4221c40cf770SDouglas Anderson static void dwc2_hcd_phy_reset_func(struct work_struct *work)
4222c40cf770SDouglas Anderson {
4223c40cf770SDouglas Anderson 	struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4224c40cf770SDouglas Anderson 						phy_reset_work);
4225c40cf770SDouglas Anderson 	int ret;
4226c40cf770SDouglas Anderson 
4227c40cf770SDouglas Anderson 	ret = phy_reset(hsotg->phy);
4228c40cf770SDouglas Anderson 	if (ret)
4229c40cf770SDouglas Anderson 		dev_warn(hsotg->dev, "PHY reset failed\n");
4230c40cf770SDouglas Anderson }
4231c40cf770SDouglas Anderson 
4232197ba5f4SPaul Zimmerman /*
4233197ba5f4SPaul Zimmerman  * =========================================================================
4234197ba5f4SPaul Zimmerman  *  Linux HC Driver Functions
4235197ba5f4SPaul Zimmerman  * =========================================================================
4236197ba5f4SPaul Zimmerman  */
4237197ba5f4SPaul Zimmerman 
4238197ba5f4SPaul Zimmerman /*
4239197ba5f4SPaul Zimmerman  * Initializes the DWC_otg controller and its root hub and prepares it for host
4240197ba5f4SPaul Zimmerman  * mode operation. Activates the root port. Returns 0 on success and a negative
4241197ba5f4SPaul Zimmerman  * error code on failure.
4242197ba5f4SPaul Zimmerman  */
4243197ba5f4SPaul Zimmerman static int _dwc2_hcd_start(struct usb_hcd *hcd)
4244197ba5f4SPaul Zimmerman {
4245197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4246197ba5f4SPaul Zimmerman 	struct usb_bus *bus = hcd_to_bus(hcd);
4247197ba5f4SPaul Zimmerman 	unsigned long flags;
4248cd7cd0e6SFabrice Gasnier 	u32 hprt0;
424941ee1ea2SFabrice Gasnier 	int ret;
4250197ba5f4SPaul Zimmerman 
4251197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4252197ba5f4SPaul Zimmerman 
4253197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
425431927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L0;
4255197ba5f4SPaul Zimmerman 	hcd->state = HC_STATE_RUNNING;
425631927b6bSGregory Herrero 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4257197ba5f4SPaul Zimmerman 
4258197ba5f4SPaul Zimmerman 	if (dwc2_is_device_mode(hsotg)) {
4259197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4260197ba5f4SPaul Zimmerman 		return 0;	/* why 0 ?? */
4261197ba5f4SPaul Zimmerman 	}
4262197ba5f4SPaul Zimmerman 
4263197ba5f4SPaul Zimmerman 	dwc2_hcd_reinit(hsotg);
4264197ba5f4SPaul Zimmerman 
4265cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4266cd7cd0e6SFabrice Gasnier 	/* Has vbus power been turned on in dwc2_core_host_init ? */
4267cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR) {
4268cd7cd0e6SFabrice Gasnier 		/* Enable external vbus supply before resuming root hub */
426941ee1ea2SFabrice Gasnier 		spin_unlock_irqrestore(&hsotg->lock, flags);
427041ee1ea2SFabrice Gasnier 		ret = dwc2_vbus_supply_init(hsotg);
427141ee1ea2SFabrice Gasnier 		if (ret)
427241ee1ea2SFabrice Gasnier 			return ret;
427341ee1ea2SFabrice Gasnier 		spin_lock_irqsave(&hsotg->lock, flags);
4274cd7cd0e6SFabrice Gasnier 	}
427541ee1ea2SFabrice Gasnier 
4276197ba5f4SPaul Zimmerman 	/* Initialize and connect root hub if one is not already attached */
4277197ba5f4SPaul Zimmerman 	if (bus->root_hub) {
4278197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4279197ba5f4SPaul Zimmerman 		/* Inform the HUB driver to resume */
4280197ba5f4SPaul Zimmerman 		usb_hcd_resume_root_hub(hcd);
4281197ba5f4SPaul Zimmerman 	}
4282197ba5f4SPaul Zimmerman 
4283197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4284531ef5ebSAmelie Delaunay 
428541ee1ea2SFabrice Gasnier 	return 0;
4286197ba5f4SPaul Zimmerman }
4287197ba5f4SPaul Zimmerman 
4288197ba5f4SPaul Zimmerman /*
4289197ba5f4SPaul Zimmerman  * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4290197ba5f4SPaul Zimmerman  * stopped.
4291197ba5f4SPaul Zimmerman  */
4292197ba5f4SPaul Zimmerman static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4293197ba5f4SPaul Zimmerman {
4294197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4295197ba5f4SPaul Zimmerman 	unsigned long flags;
4296cd7cd0e6SFabrice Gasnier 	u32 hprt0;
4297197ba5f4SPaul Zimmerman 
42985bbf6ce0SGregory Herrero 	/* Turn off all host-specific interrupts */
42995bbf6ce0SGregory Herrero 	dwc2_disable_host_interrupts(hsotg);
43005bbf6ce0SGregory Herrero 
4301091473adSGregory Herrero 	/* Wait for interrupt processing to finish */
4302091473adSGregory Herrero 	synchronize_irq(hcd->irq);
4303091473adSGregory Herrero 
4304197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4305cd7cd0e6SFabrice Gasnier 	hprt0 = dwc2_read_hprt0(hsotg);
4306091473adSGregory Herrero 	/* Ensure hcd is disconnected */
43076a659531SDouglas Anderson 	dwc2_hcd_disconnect(hsotg, true);
4308197ba5f4SPaul Zimmerman 	dwc2_hcd_stop(hsotg);
430931927b6bSGregory Herrero 	hsotg->lx_state = DWC2_L3;
431031927b6bSGregory Herrero 	hcd->state = HC_STATE_HALT;
431131927b6bSGregory Herrero 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4312197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4313197ba5f4SPaul Zimmerman 
4314cd7cd0e6SFabrice Gasnier 	/* keep balanced supply init/exit by checking HPRT0_PWR */
4315cd7cd0e6SFabrice Gasnier 	if (hprt0 & HPRT0_PWR)
4316531ef5ebSAmelie Delaunay 		dwc2_vbus_supply_exit(hsotg);
4317531ef5ebSAmelie Delaunay 
4318197ba5f4SPaul Zimmerman 	usleep_range(1000, 3000);
4319197ba5f4SPaul Zimmerman }
4320197ba5f4SPaul Zimmerman 
432199a65798SGregory Herrero static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
432299a65798SGregory Herrero {
432399a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4324a2a23d3fSGregory Herrero 	unsigned long flags;
4325a2a23d3fSGregory Herrero 	int ret = 0;
432699a65798SGregory Herrero 
4327a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4328a2a23d3fSGregory Herrero 
4329f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4330f367b72cSMeng Dongyang 		goto unlock;
4331f367b72cSMeng Dongyang 
4332a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L0)
4333a2a23d3fSGregory Herrero 		goto unlock;
4334a2a23d3fSGregory Herrero 
4335a2a23d3fSGregory Herrero 	if (!HCD_HW_ACCESSIBLE(hcd))
4336a2a23d3fSGregory Herrero 		goto unlock;
4337a2a23d3fSGregory Herrero 
4338866932e2SJohn Stultz 	if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4339866932e2SJohn Stultz 		goto unlock;
4340866932e2SJohn Stultz 
4341113f86d0SArtur Petrosyan 	if (hsotg->bus_suspended)
4342a2a23d3fSGregory Herrero 		goto skip_power_saving;
4343a2a23d3fSGregory Herrero 
4344113f86d0SArtur Petrosyan 	if (hsotg->flags.b.port_connect_status == 0)
4345113f86d0SArtur Petrosyan 		goto skip_power_saving;
4346113f86d0SArtur Petrosyan 
4347113f86d0SArtur Petrosyan 	switch (hsotg->params.power_down) {
4348113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4349113f86d0SArtur Petrosyan 		/* Enter partial_power_down */
4350113f86d0SArtur Petrosyan 		ret = dwc2_enter_partial_power_down(hsotg);
4351113f86d0SArtur Petrosyan 		if (ret)
4352113f86d0SArtur Petrosyan 			dev_err(hsotg->dev,
4353113f86d0SArtur Petrosyan 				"enter partial_power_down failed\n");
4354113f86d0SArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
4355113f86d0SArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4356113f86d0SArtur Petrosyan 		break;
4357113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4358755d0effSArtur Petrosyan 		/* Enter hibernation */
4359755d0effSArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
4360755d0effSArtur Petrosyan 		ret = dwc2_enter_hibernation(hsotg, 1);
4361755d0effSArtur Petrosyan 		if (ret)
4362755d0effSArtur Petrosyan 			dev_err(hsotg->dev, "enter hibernation failed\n");
4363755d0effSArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
4364755d0effSArtur Petrosyan 
4365755d0effSArtur Petrosyan 		/* After entering suspend, hardware is not accessible */
4366755d0effSArtur Petrosyan 		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4367755d0effSArtur Petrosyan 		break;
4368113f86d0SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
436950fb0c12SArtur Petrosyan 		/*
437050fb0c12SArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
437150fb0c12SArtur Petrosyan 		 * clock gating is used to save power.
437250fb0c12SArtur Petrosyan 		 */
437334146c68SDinh Nguyen 		if (!hsotg->params.no_clock_gating) {
437450fb0c12SArtur Petrosyan 			dwc2_host_enter_clock_gating(hsotg);
437550fb0c12SArtur Petrosyan 
437650fb0c12SArtur Petrosyan 			/* After entering suspend, hardware is not accessible */
437750fb0c12SArtur Petrosyan 			clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
437834146c68SDinh Nguyen 		}
437950fb0c12SArtur Petrosyan 		break;
4380113f86d0SArtur Petrosyan 	default:
4381113f86d0SArtur Petrosyan 		goto skip_power_saving;
43826f6d7059SDouglas Anderson 	}
4383113f86d0SArtur Petrosyan 
43845aa678c7SFabrice Gasnier 	spin_unlock_irqrestore(&hsotg->lock, flags);
4385531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_exit(hsotg);
43865aa678c7SFabrice Gasnier 	spin_lock_irqsave(&hsotg->lock, flags);
43876f6d7059SDouglas Anderson 
4388a2a23d3fSGregory Herrero 	/* Ask phy to be suspended */
4389a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4390a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4391a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, true);
4392a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4393a2a23d3fSGregory Herrero 	}
4394a2a23d3fSGregory Herrero 
4395a2a23d3fSGregory Herrero skip_power_saving:
439699a65798SGregory Herrero 	hsotg->lx_state = DWC2_L2;
4397a2a23d3fSGregory Herrero unlock:
4398a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4399a2a23d3fSGregory Herrero 
4400a2a23d3fSGregory Herrero 	return ret;
440199a65798SGregory Herrero }
440299a65798SGregory Herrero 
440399a65798SGregory Herrero static int _dwc2_hcd_resume(struct usb_hcd *hcd)
440499a65798SGregory Herrero {
440599a65798SGregory Herrero 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4406a2a23d3fSGregory Herrero 	unsigned long flags;
4407c74c26f6SArtur Petrosyan 	u32 hprt0;
4408a2a23d3fSGregory Herrero 	int ret = 0;
4409a2a23d3fSGregory Herrero 
4410a2a23d3fSGregory Herrero 	spin_lock_irqsave(&hsotg->lock, flags);
4411a2a23d3fSGregory Herrero 
4412f367b72cSMeng Dongyang 	if (dwc2_is_device_mode(hsotg))
4413f367b72cSMeng Dongyang 		goto unlock;
4414f367b72cSMeng Dongyang 
4415a2a23d3fSGregory Herrero 	if (hsotg->lx_state != DWC2_L2)
4416a2a23d3fSGregory Herrero 		goto unlock;
4417a2a23d3fSGregory Herrero 
4418c74c26f6SArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
4419c74c26f6SArtur Petrosyan 
4420c74c26f6SArtur Petrosyan 	/*
4421c74c26f6SArtur Petrosyan 	 * Added port connection status checking which prevents exiting from
4422c74c26f6SArtur Petrosyan 	 * Partial Power Down mode from _dwc2_hcd_resume() if not in Partial
4423c74c26f6SArtur Petrosyan 	 * Power Down mode.
4424c74c26f6SArtur Petrosyan 	 */
4425c74c26f6SArtur Petrosyan 	if (hprt0 & HPRT0_CONNSTS) {
4426a2a23d3fSGregory Herrero 		hsotg->lx_state = DWC2_L0;
4427a2a23d3fSGregory Herrero 		goto unlock;
4428a2a23d3fSGregory Herrero 	}
4429a2a23d3fSGregory Herrero 
4430c74c26f6SArtur Petrosyan 	switch (hsotg->params.power_down) {
4431c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
4432c74c26f6SArtur Petrosyan 		ret = dwc2_exit_partial_power_down(hsotg, 0, true);
4433c74c26f6SArtur Petrosyan 		if (ret)
4434c74c26f6SArtur Petrosyan 			dev_err(hsotg->dev,
4435c74c26f6SArtur Petrosyan 				"exit partial_power_down failed\n");
4436c74c26f6SArtur Petrosyan 		/*
4437c74c26f6SArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4438c74c26f6SArtur Petrosyan 		 * since an interrupt may rise.
4439c74c26f6SArtur Petrosyan 		 */
4440c74c26f6SArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4441c74c26f6SArtur Petrosyan 		break;
4442c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
4443ae0da4fdSArtur Petrosyan 		ret = dwc2_exit_hibernation(hsotg, 0, 0, 1);
4444ae0da4fdSArtur Petrosyan 		if (ret)
4445ae0da4fdSArtur Petrosyan 			dev_err(hsotg->dev, "exit hibernation failed.\n");
4446ae0da4fdSArtur Petrosyan 
4447ae0da4fdSArtur Petrosyan 		/*
4448ae0da4fdSArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4449ae0da4fdSArtur Petrosyan 		 * since an interrupt may rise.
4450ae0da4fdSArtur Petrosyan 		 */
4451ae0da4fdSArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4452ae0da4fdSArtur Petrosyan 		break;
4453c74c26f6SArtur Petrosyan 	case DWC2_POWER_DOWN_PARAM_NONE:
4454ef5e0eecSArtur Petrosyan 		/*
4455ef5e0eecSArtur Petrosyan 		 * If not hibernation nor partial power down are supported,
4456ef5e0eecSArtur Petrosyan 		 * port resume is done using the clock gating programming flow.
4457ef5e0eecSArtur Petrosyan 		 */
4458ef5e0eecSArtur Petrosyan 		spin_unlock_irqrestore(&hsotg->lock, flags);
4459ef5e0eecSArtur Petrosyan 		dwc2_host_exit_clock_gating(hsotg, 0);
4460ef5e0eecSArtur Petrosyan 
4461ef5e0eecSArtur Petrosyan 		/*
4462ef5e0eecSArtur Petrosyan 		 * Initialize the Core for Host mode, as after system resume
4463ef5e0eecSArtur Petrosyan 		 * the global interrupts are disabled.
4464ef5e0eecSArtur Petrosyan 		 */
4465ef5e0eecSArtur Petrosyan 		dwc2_core_init(hsotg, false);
4466ef5e0eecSArtur Petrosyan 		dwc2_enable_global_interrupts(hsotg);
4467ef5e0eecSArtur Petrosyan 		dwc2_hcd_reinit(hsotg);
4468ef5e0eecSArtur Petrosyan 		spin_lock_irqsave(&hsotg->lock, flags);
4469ef5e0eecSArtur Petrosyan 
4470ef5e0eecSArtur Petrosyan 		/*
4471ef5e0eecSArtur Petrosyan 		 * Set HW accessible bit before powering on the controller
4472ef5e0eecSArtur Petrosyan 		 * since an interrupt may rise.
4473ef5e0eecSArtur Petrosyan 		 */
4474ef5e0eecSArtur Petrosyan 		set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4475ef5e0eecSArtur Petrosyan 		break;
4476c74c26f6SArtur Petrosyan 	default:
4477c74c26f6SArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
4478c74c26f6SArtur Petrosyan 		goto unlock;
4479c74c26f6SArtur Petrosyan 	}
4480c74c26f6SArtur Petrosyan 
4481c74c26f6SArtur Petrosyan 	/* Change Root port status, as port status change occurred after resume.*/
4482c74c26f6SArtur Petrosyan 	hsotg->flags.b.port_suspend_change = 1;
4483c74c26f6SArtur Petrosyan 
4484a2a23d3fSGregory Herrero 	/*
4485a2a23d3fSGregory Herrero 	 * Enable power if not already done.
4486a2a23d3fSGregory Herrero 	 * This must not be spinlocked since duration
4487a2a23d3fSGregory Herrero 	 * of this call is unknown.
4488a2a23d3fSGregory Herrero 	 */
4489a2a23d3fSGregory Herrero 	if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4490a2a23d3fSGregory Herrero 		spin_unlock_irqrestore(&hsotg->lock, flags);
4491a2a23d3fSGregory Herrero 		usb_phy_set_suspend(hsotg->uphy, false);
4492a2a23d3fSGregory Herrero 		spin_lock_irqsave(&hsotg->lock, flags);
4493a2a23d3fSGregory Herrero 	}
4494a2a23d3fSGregory Herrero 
4495c74c26f6SArtur Petrosyan 	/* Enable external vbus supply after resuming the port. */
4496a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4497531ef5ebSAmelie Delaunay 	dwc2_vbus_supply_init(hsotg);
4498531ef5ebSAmelie Delaunay 
44995634e016SGregory Herrero 	/* Wait for controller to correctly update D+/D- level */
45005634e016SGregory Herrero 	usleep_range(3000, 5000);
4501c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
45025634e016SGregory Herrero 
4503a2a23d3fSGregory Herrero 	/*
4504a2a23d3fSGregory Herrero 	 * Clear Port Enable and Port Status changes.
4505a2a23d3fSGregory Herrero 	 * Enable Port Power.
4506a2a23d3fSGregory Herrero 	 */
4507f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, HPRT0_PWR | HPRT0_CONNDET |
4508f25c42b8SGevorg Sahakyan 			HPRT0_ENACHG, HPRT0);
4509a2a23d3fSGregory Herrero 
4510c74c26f6SArtur Petrosyan 	/* Wait for controller to detect Port Connect */
4511c74c26f6SArtur Petrosyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
4512c74c26f6SArtur Petrosyan 	usleep_range(5000, 7000);
4513c74c26f6SArtur Petrosyan 	spin_lock_irqsave(&hsotg->lock, flags);
4514a2a23d3fSGregory Herrero unlock:
4515a2a23d3fSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
4516a2a23d3fSGregory Herrero 
4517a2a23d3fSGregory Herrero 	return ret;
451899a65798SGregory Herrero }
451999a65798SGregory Herrero 
4520197ba5f4SPaul Zimmerman /* Returns the current frame number */
4521197ba5f4SPaul Zimmerman static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4522197ba5f4SPaul Zimmerman {
4523197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4524197ba5f4SPaul Zimmerman 
4525197ba5f4SPaul Zimmerman 	return dwc2_hcd_get_frame_number(hsotg);
4526197ba5f4SPaul Zimmerman }
4527197ba5f4SPaul Zimmerman 
4528197ba5f4SPaul Zimmerman static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4529197ba5f4SPaul Zimmerman 			       char *fn_name)
4530197ba5f4SPaul Zimmerman {
4531197ba5f4SPaul Zimmerman #ifdef VERBOSE_DEBUG
4532197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4533efe357f4SNicholas Mc Guire 	char *pipetype = NULL;
4534efe357f4SNicholas Mc Guire 	char *speed = NULL;
4535197ba5f4SPaul Zimmerman 
4536197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4537197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Device address: %d\n",
4538197ba5f4SPaul Zimmerman 		 usb_pipedevice(urb->pipe));
4539197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint: %d, %s\n",
4540197ba5f4SPaul Zimmerman 		 usb_pipeendpoint(urb->pipe),
4541197ba5f4SPaul Zimmerman 		 usb_pipein(urb->pipe) ? "IN" : "OUT");
4542197ba5f4SPaul Zimmerman 
4543197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4544197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4545197ba5f4SPaul Zimmerman 		pipetype = "CONTROL";
4546197ba5f4SPaul Zimmerman 		break;
4547197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4548197ba5f4SPaul Zimmerman 		pipetype = "BULK";
4549197ba5f4SPaul Zimmerman 		break;
4550197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4551197ba5f4SPaul Zimmerman 		pipetype = "INTERRUPT";
4552197ba5f4SPaul Zimmerman 		break;
4553197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4554197ba5f4SPaul Zimmerman 		pipetype = "ISOCHRONOUS";
4555197ba5f4SPaul Zimmerman 		break;
4556197ba5f4SPaul Zimmerman 	}
4557197ba5f4SPaul Zimmerman 
4558197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Endpoint type: %s %s (%s)\n", pipetype,
4559197ba5f4SPaul Zimmerman 		 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4560197ba5f4SPaul Zimmerman 		 "IN" : "OUT");
4561197ba5f4SPaul Zimmerman 
4562197ba5f4SPaul Zimmerman 	switch (urb->dev->speed) {
4563197ba5f4SPaul Zimmerman 	case USB_SPEED_HIGH:
4564197ba5f4SPaul Zimmerman 		speed = "HIGH";
4565197ba5f4SPaul Zimmerman 		break;
4566197ba5f4SPaul Zimmerman 	case USB_SPEED_FULL:
4567197ba5f4SPaul Zimmerman 		speed = "FULL";
4568197ba5f4SPaul Zimmerman 		break;
4569197ba5f4SPaul Zimmerman 	case USB_SPEED_LOW:
4570197ba5f4SPaul Zimmerman 		speed = "LOW";
4571197ba5f4SPaul Zimmerman 		break;
4572197ba5f4SPaul Zimmerman 	default:
4573197ba5f4SPaul Zimmerman 		speed = "UNKNOWN";
4574197ba5f4SPaul Zimmerman 		break;
4575197ba5f4SPaul Zimmerman 	}
4576197ba5f4SPaul Zimmerman 
4577197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Speed: %s\n", speed);
4578babd1839SDouglas Anderson 	dev_vdbg(hsotg->dev, "  Max packet size: %d (%d mult)\n",
4579babd1839SDouglas Anderson 		 usb_endpoint_maxp(&urb->ep->desc),
4580babd1839SDouglas Anderson 		 usb_endpoint_maxp_mult(&urb->ep->desc));
4581babd1839SDouglas Anderson 
4582197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Data buffer length: %d\n",
4583197ba5f4SPaul Zimmerman 		 urb->transfer_buffer_length);
4584197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Transfer buffer: %p, Transfer DMA: %08lx\n",
4585197ba5f4SPaul Zimmerman 		 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4586197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Setup buffer: %p, Setup DMA: %08lx\n",
4587197ba5f4SPaul Zimmerman 		 urb->setup_packet, (unsigned long)urb->setup_dma);
4588197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "  Interval: %d\n", urb->interval);
4589197ba5f4SPaul Zimmerman 
4590197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4591197ba5f4SPaul Zimmerman 		int i;
4592197ba5f4SPaul Zimmerman 
4593197ba5f4SPaul Zimmerman 		for (i = 0; i < urb->number_of_packets; i++) {
4594197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "  ISO Desc %d:\n", i);
4595197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "    offset: %d, length %d\n",
4596197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].offset,
4597197ba5f4SPaul Zimmerman 				 urb->iso_frame_desc[i].length);
4598197ba5f4SPaul Zimmerman 		}
4599197ba5f4SPaul Zimmerman 	}
4600197ba5f4SPaul Zimmerman #endif
4601197ba5f4SPaul Zimmerman }
4602197ba5f4SPaul Zimmerman 
4603197ba5f4SPaul Zimmerman /*
4604197ba5f4SPaul Zimmerman  * Starts processing a USB transfer request specified by a USB Request Block
4605197ba5f4SPaul Zimmerman  * (URB). mem_flags indicates the type of memory allocation to use while
4606197ba5f4SPaul Zimmerman  * processing this URB.
4607197ba5f4SPaul Zimmerman  */
4608197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4609197ba5f4SPaul Zimmerman 				 gfp_t mem_flags)
4610197ba5f4SPaul Zimmerman {
4611197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4612197ba5f4SPaul Zimmerman 	struct usb_host_endpoint *ep = urb->ep;
4613197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *dwc2_urb;
4614197ba5f4SPaul Zimmerman 	int i;
4615197ba5f4SPaul Zimmerman 	int retval;
4616197ba5f4SPaul Zimmerman 	int alloc_bandwidth = 0;
4617197ba5f4SPaul Zimmerman 	u8 ep_type = 0;
4618197ba5f4SPaul Zimmerman 	u32 tflags = 0;
4619197ba5f4SPaul Zimmerman 	void *buf;
4620197ba5f4SPaul Zimmerman 	unsigned long flags;
4621b58e6ceeSMian Yousaf Kaukab 	struct dwc2_qh *qh;
4622b58e6ceeSMian Yousaf Kaukab 	bool qh_allocated = false;
4623b5a468a6SMian Yousaf Kaukab 	struct dwc2_qtd *qtd;
4624c3595df7SArtur Petrosyan 	struct dwc2_gregs_backup *gr;
4625c3595df7SArtur Petrosyan 
4626c3595df7SArtur Petrosyan 	gr = &hsotg->gr_backup;
4627197ba5f4SPaul Zimmerman 
4628197ba5f4SPaul Zimmerman 	if (dbg_urb(urb)) {
4629197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4630197ba5f4SPaul Zimmerman 		dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4631197ba5f4SPaul Zimmerman 	}
4632197ba5f4SPaul Zimmerman 
4633c3595df7SArtur Petrosyan 	if (hsotg->hibernated) {
4634c3595df7SArtur Petrosyan 		if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
4635c3595df7SArtur Petrosyan 			retval = dwc2_exit_hibernation(hsotg, 0, 0, 1);
4636c3595df7SArtur Petrosyan 		else
4637c3595df7SArtur Petrosyan 			retval = dwc2_exit_hibernation(hsotg, 0, 0, 0);
4638c3595df7SArtur Petrosyan 
4639c3595df7SArtur Petrosyan 		if (retval)
4640c3595df7SArtur Petrosyan 			dev_err(hsotg->dev,
4641c3595df7SArtur Petrosyan 				"exit hibernation failed.\n");
4642c3595df7SArtur Petrosyan 	}
4643c3595df7SArtur Petrosyan 
464475f43ac3SArtur Petrosyan 	if (hsotg->in_ppd) {
464575f43ac3SArtur Petrosyan 		retval = dwc2_exit_partial_power_down(hsotg, 0, true);
464675f43ac3SArtur Petrosyan 		if (retval)
464775f43ac3SArtur Petrosyan 			dev_err(hsotg->dev,
464875f43ac3SArtur Petrosyan 				"exit partial_power_down failed\n");
464975f43ac3SArtur Petrosyan 	}
465075f43ac3SArtur Petrosyan 
465116c729f9SArtur Petrosyan 	if (hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
465216c729f9SArtur Petrosyan 	    hsotg->bus_suspended) {
465316c729f9SArtur Petrosyan 		if (dwc2_is_device_mode(hsotg))
465416c729f9SArtur Petrosyan 			dwc2_gadget_exit_clock_gating(hsotg, 0);
465516c729f9SArtur Petrosyan 		else
465616c729f9SArtur Petrosyan 			dwc2_host_exit_clock_gating(hsotg, 0);
465716c729f9SArtur Petrosyan 	}
465816c729f9SArtur Petrosyan 
46599da51974SJohn Youn 	if (!ep)
4660197ba5f4SPaul Zimmerman 		return -EINVAL;
4661197ba5f4SPaul Zimmerman 
4662197ba5f4SPaul Zimmerman 	if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4663197ba5f4SPaul Zimmerman 	    usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4664197ba5f4SPaul Zimmerman 		spin_lock_irqsave(&hsotg->lock, flags);
4665197ba5f4SPaul Zimmerman 		if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4666197ba5f4SPaul Zimmerman 			alloc_bandwidth = 1;
4667197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
4668197ba5f4SPaul Zimmerman 	}
4669197ba5f4SPaul Zimmerman 
4670197ba5f4SPaul Zimmerman 	switch (usb_pipetype(urb->pipe)) {
4671197ba5f4SPaul Zimmerman 	case PIPE_CONTROL:
4672197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_CONTROL;
4673197ba5f4SPaul Zimmerman 		break;
4674197ba5f4SPaul Zimmerman 	case PIPE_ISOCHRONOUS:
4675197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_ISOC;
4676197ba5f4SPaul Zimmerman 		break;
4677197ba5f4SPaul Zimmerman 	case PIPE_BULK:
4678197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_BULK;
4679197ba5f4SPaul Zimmerman 		break;
4680197ba5f4SPaul Zimmerman 	case PIPE_INTERRUPT:
4681197ba5f4SPaul Zimmerman 		ep_type = USB_ENDPOINT_XFER_INT;
4682197ba5f4SPaul Zimmerman 		break;
4683197ba5f4SPaul Zimmerman 	}
4684197ba5f4SPaul Zimmerman 
4685197ba5f4SPaul Zimmerman 	dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4686197ba5f4SPaul Zimmerman 				      mem_flags);
4687197ba5f4SPaul Zimmerman 	if (!dwc2_urb)
4688197ba5f4SPaul Zimmerman 		return -ENOMEM;
4689197ba5f4SPaul Zimmerman 
4690197ba5f4SPaul Zimmerman 	dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4691197ba5f4SPaul Zimmerman 				  usb_pipeendpoint(urb->pipe), ep_type,
4692197ba5f4SPaul Zimmerman 				  usb_pipein(urb->pipe),
4693babd1839SDouglas Anderson 				  usb_endpoint_maxp(&ep->desc),
4694babd1839SDouglas Anderson 				  usb_endpoint_maxp_mult(&ep->desc));
4695197ba5f4SPaul Zimmerman 
4696197ba5f4SPaul Zimmerman 	buf = urb->transfer_buffer;
4697197ba5f4SPaul Zimmerman 
4698edfbcb32SChristoph Hellwig 	if (hcd_uses_dma(hcd)) {
4699197ba5f4SPaul Zimmerman 		if (!buf && (urb->transfer_dma & 3)) {
4700197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
4701197ba5f4SPaul Zimmerman 				"%s: unaligned transfer with no transfer_buffer",
4702197ba5f4SPaul Zimmerman 				__func__);
4703197ba5f4SPaul Zimmerman 			retval = -EINVAL;
470433ad261aSGregory Herrero 			goto fail0;
4705197ba5f4SPaul Zimmerman 		}
4706197ba5f4SPaul Zimmerman 	}
4707197ba5f4SPaul Zimmerman 
4708197ba5f4SPaul Zimmerman 	if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4709197ba5f4SPaul Zimmerman 		tflags |= URB_GIVEBACK_ASAP;
4710197ba5f4SPaul Zimmerman 	if (urb->transfer_flags & URB_ZERO_PACKET)
4711197ba5f4SPaul Zimmerman 		tflags |= URB_SEND_ZERO_PACKET;
4712197ba5f4SPaul Zimmerman 
4713197ba5f4SPaul Zimmerman 	dwc2_urb->priv = urb;
4714197ba5f4SPaul Zimmerman 	dwc2_urb->buf = buf;
4715197ba5f4SPaul Zimmerman 	dwc2_urb->dma = urb->transfer_dma;
4716197ba5f4SPaul Zimmerman 	dwc2_urb->length = urb->transfer_buffer_length;
4717197ba5f4SPaul Zimmerman 	dwc2_urb->setup_packet = urb->setup_packet;
4718197ba5f4SPaul Zimmerman 	dwc2_urb->setup_dma = urb->setup_dma;
4719197ba5f4SPaul Zimmerman 	dwc2_urb->flags = tflags;
4720197ba5f4SPaul Zimmerman 	dwc2_urb->interval = urb->interval;
4721197ba5f4SPaul Zimmerman 	dwc2_urb->status = -EINPROGRESS;
4722197ba5f4SPaul Zimmerman 
4723197ba5f4SPaul Zimmerman 	for (i = 0; i < urb->number_of_packets; ++i)
4724197ba5f4SPaul Zimmerman 		dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4725197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].offset,
4726197ba5f4SPaul Zimmerman 						 urb->iso_frame_desc[i].length);
4727197ba5f4SPaul Zimmerman 
4728197ba5f4SPaul Zimmerman 	urb->hcpriv = dwc2_urb;
4729b58e6ceeSMian Yousaf Kaukab 	qh = (struct dwc2_qh *)ep->hcpriv;
4730b58e6ceeSMian Yousaf Kaukab 	/* Create QH for the endpoint if it doesn't exist */
4731b58e6ceeSMian Yousaf Kaukab 	if (!qh) {
4732b58e6ceeSMian Yousaf Kaukab 		qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4733b58e6ceeSMian Yousaf Kaukab 		if (!qh) {
4734b58e6ceeSMian Yousaf Kaukab 			retval = -ENOMEM;
4735b58e6ceeSMian Yousaf Kaukab 			goto fail0;
4736b58e6ceeSMian Yousaf Kaukab 		}
4737b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = qh;
4738b58e6ceeSMian Yousaf Kaukab 		qh_allocated = true;
4739b58e6ceeSMian Yousaf Kaukab 	}
4740197ba5f4SPaul Zimmerman 
4741b5a468a6SMian Yousaf Kaukab 	qtd = kzalloc(sizeof(*qtd), mem_flags);
4742b5a468a6SMian Yousaf Kaukab 	if (!qtd) {
4743b5a468a6SMian Yousaf Kaukab 		retval = -ENOMEM;
4744b5a468a6SMian Yousaf Kaukab 		goto fail1;
4745b5a468a6SMian Yousaf Kaukab 	}
4746b5a468a6SMian Yousaf Kaukab 
4747197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4748197ba5f4SPaul Zimmerman 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
4749197ba5f4SPaul Zimmerman 	if (retval)
4750197ba5f4SPaul Zimmerman 		goto fail2;
4751197ba5f4SPaul Zimmerman 
4752b5a468a6SMian Yousaf Kaukab 	retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4753b5a468a6SMian Yousaf Kaukab 	if (retval)
4754b5a468a6SMian Yousaf Kaukab 		goto fail3;
4755b5a468a6SMian Yousaf Kaukab 
4756197ba5f4SPaul Zimmerman 	if (alloc_bandwidth) {
4757197ba5f4SPaul Zimmerman 		dwc2_allocate_bus_bandwidth(hcd,
4758197ba5f4SPaul Zimmerman 				dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4759197ba5f4SPaul Zimmerman 				urb);
4760197ba5f4SPaul Zimmerman 	}
4761197ba5f4SPaul Zimmerman 
476233ad261aSGregory Herrero 	spin_unlock_irqrestore(&hsotg->lock, flags);
476333ad261aSGregory Herrero 
4764197ba5f4SPaul Zimmerman 	return 0;
4765197ba5f4SPaul Zimmerman 
4766b5a468a6SMian Yousaf Kaukab fail3:
4767197ba5f4SPaul Zimmerman 	dwc2_urb->priv = NULL;
4768197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
476916e80218SDouglas Anderson 	if (qh_allocated && qh->channel && qh->channel->qh == qh)
477016e80218SDouglas Anderson 		qh->channel->qh = NULL;
4771b5a468a6SMian Yousaf Kaukab fail2:
4772197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4773*ef307bc6SJia-Ju Bai 	spin_unlock_irqrestore(&hsotg->lock, flags);
4774b5a468a6SMian Yousaf Kaukab 	kfree(qtd);
4775b5a468a6SMian Yousaf Kaukab fail1:
4776b58e6ceeSMian Yousaf Kaukab 	if (qh_allocated) {
4777b58e6ceeSMian Yousaf Kaukab 		struct dwc2_qtd *qtd2, *qtd2_tmp;
4778b58e6ceeSMian Yousaf Kaukab 
4779b58e6ceeSMian Yousaf Kaukab 		ep->hcpriv = NULL;
4780b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_unlink(hsotg, qh);
4781b58e6ceeSMian Yousaf Kaukab 		/* Free each QTD in the QH's QTD list */
4782b58e6ceeSMian Yousaf Kaukab 		list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
4783b58e6ceeSMian Yousaf Kaukab 					 qtd_list_entry)
4784b58e6ceeSMian Yousaf Kaukab 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4785b58e6ceeSMian Yousaf Kaukab 		dwc2_hcd_qh_free(hsotg, qh);
4786b58e6ceeSMian Yousaf Kaukab 	}
478733ad261aSGregory Herrero fail0:
4788197ba5f4SPaul Zimmerman 	kfree(dwc2_urb);
4789197ba5f4SPaul Zimmerman 
4790197ba5f4SPaul Zimmerman 	return retval;
4791197ba5f4SPaul Zimmerman }
4792197ba5f4SPaul Zimmerman 
4793197ba5f4SPaul Zimmerman /*
4794197ba5f4SPaul Zimmerman  * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4795197ba5f4SPaul Zimmerman  */
4796197ba5f4SPaul Zimmerman static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4797197ba5f4SPaul Zimmerman 				 int status)
4798197ba5f4SPaul Zimmerman {
4799197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4800197ba5f4SPaul Zimmerman 	int rc;
4801197ba5f4SPaul Zimmerman 	unsigned long flags;
4802197ba5f4SPaul Zimmerman 
4803197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4804197ba5f4SPaul Zimmerman 	dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4805197ba5f4SPaul Zimmerman 
4806197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4807197ba5f4SPaul Zimmerman 
4808197ba5f4SPaul Zimmerman 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4809197ba5f4SPaul Zimmerman 	if (rc)
4810197ba5f4SPaul Zimmerman 		goto out;
4811197ba5f4SPaul Zimmerman 
4812197ba5f4SPaul Zimmerman 	if (!urb->hcpriv) {
4813197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4814197ba5f4SPaul Zimmerman 		goto out;
4815197ba5f4SPaul Zimmerman 	}
4816197ba5f4SPaul Zimmerman 
4817197ba5f4SPaul Zimmerman 	rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4818197ba5f4SPaul Zimmerman 
4819197ba5f4SPaul Zimmerman 	usb_hcd_unlink_urb_from_ep(hcd, urb);
4820197ba5f4SPaul Zimmerman 
4821197ba5f4SPaul Zimmerman 	kfree(urb->hcpriv);
4822197ba5f4SPaul Zimmerman 	urb->hcpriv = NULL;
4823197ba5f4SPaul Zimmerman 
4824197ba5f4SPaul Zimmerman 	/* Higher layer software sets URB status */
4825197ba5f4SPaul Zimmerman 	spin_unlock(&hsotg->lock);
4826197ba5f4SPaul Zimmerman 	usb_hcd_giveback_urb(hcd, urb, status);
4827197ba5f4SPaul Zimmerman 	spin_lock(&hsotg->lock);
4828197ba5f4SPaul Zimmerman 
4829197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4830197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  urb->status = %d\n", urb->status);
4831197ba5f4SPaul Zimmerman out:
4832197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4833197ba5f4SPaul Zimmerman 
4834197ba5f4SPaul Zimmerman 	return rc;
4835197ba5f4SPaul Zimmerman }
4836197ba5f4SPaul Zimmerman 
4837197ba5f4SPaul Zimmerman /*
4838197ba5f4SPaul Zimmerman  * Frees resources in the DWC_otg controller related to a given endpoint. Also
4839197ba5f4SPaul Zimmerman  * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4840197ba5f4SPaul Zimmerman  * must already be dequeued.
4841197ba5f4SPaul Zimmerman  */
4842197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4843197ba5f4SPaul Zimmerman 				       struct usb_host_endpoint *ep)
4844197ba5f4SPaul Zimmerman {
4845197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4846197ba5f4SPaul Zimmerman 
4847197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4848197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4849197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress, ep->hcpriv);
4850197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4851197ba5f4SPaul Zimmerman }
4852197ba5f4SPaul Zimmerman 
4853197ba5f4SPaul Zimmerman /*
4854197ba5f4SPaul Zimmerman  * Resets endpoint specific parameter values, in current version used to reset
4855197ba5f4SPaul Zimmerman  * the data toggle (as a WA). This function can be called from usb_clear_halt
4856197ba5f4SPaul Zimmerman  * routine.
4857197ba5f4SPaul Zimmerman  */
4858197ba5f4SPaul Zimmerman static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4859197ba5f4SPaul Zimmerman 				     struct usb_host_endpoint *ep)
4860197ba5f4SPaul Zimmerman {
4861197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4862197ba5f4SPaul Zimmerman 	unsigned long flags;
4863197ba5f4SPaul Zimmerman 
4864197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev,
4865197ba5f4SPaul Zimmerman 		"DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4866197ba5f4SPaul Zimmerman 		ep->desc.bEndpointAddress);
4867197ba5f4SPaul Zimmerman 
4868197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4869197ba5f4SPaul Zimmerman 	dwc2_hcd_endpoint_reset(hsotg, ep);
4870197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4871197ba5f4SPaul Zimmerman }
4872197ba5f4SPaul Zimmerman 
4873197ba5f4SPaul Zimmerman /*
4874197ba5f4SPaul Zimmerman  * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4875197ba5f4SPaul Zimmerman  * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4876197ba5f4SPaul Zimmerman  * interrupt.
4877197ba5f4SPaul Zimmerman  *
4878197ba5f4SPaul Zimmerman  * This function is called by the USB core when an interrupt occurs
4879197ba5f4SPaul Zimmerman  */
4880197ba5f4SPaul Zimmerman static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4881197ba5f4SPaul Zimmerman {
4882197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4883197ba5f4SPaul Zimmerman 
4884197ba5f4SPaul Zimmerman 	return dwc2_handle_hcd_intr(hsotg);
4885197ba5f4SPaul Zimmerman }
4886197ba5f4SPaul Zimmerman 
4887197ba5f4SPaul Zimmerman /*
4888197ba5f4SPaul Zimmerman  * Creates Status Change bitmap for the root hub and root port. The bitmap is
4889197ba5f4SPaul Zimmerman  * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4890197ba5f4SPaul Zimmerman  * is the status change indicator for the single root port. Returns 1 if either
4891197ba5f4SPaul Zimmerman  * change indicator is 1, otherwise returns 0.
4892197ba5f4SPaul Zimmerman  */
4893197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4894197ba5f4SPaul Zimmerman {
4895197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4896197ba5f4SPaul Zimmerman 
4897197ba5f4SPaul Zimmerman 	buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4898197ba5f4SPaul Zimmerman 	return buf[0] != 0;
4899197ba5f4SPaul Zimmerman }
4900197ba5f4SPaul Zimmerman 
4901197ba5f4SPaul Zimmerman /* Handles hub class-specific requests */
4902197ba5f4SPaul Zimmerman static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4903197ba5f4SPaul Zimmerman 				 u16 windex, char *buf, u16 wlength)
4904197ba5f4SPaul Zimmerman {
4905197ba5f4SPaul Zimmerman 	int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4906197ba5f4SPaul Zimmerman 					  wvalue, windex, buf, wlength);
4907197ba5f4SPaul Zimmerman 	return retval;
4908197ba5f4SPaul Zimmerman }
4909197ba5f4SPaul Zimmerman 
4910197ba5f4SPaul Zimmerman /* Handles hub TT buffer clear completions */
4911197ba5f4SPaul Zimmerman static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4912197ba5f4SPaul Zimmerman 					       struct usb_host_endpoint *ep)
4913197ba5f4SPaul Zimmerman {
4914197ba5f4SPaul Zimmerman 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4915197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
4916197ba5f4SPaul Zimmerman 	unsigned long flags;
4917197ba5f4SPaul Zimmerman 
4918197ba5f4SPaul Zimmerman 	qh = ep->hcpriv;
4919197ba5f4SPaul Zimmerman 	if (!qh)
4920197ba5f4SPaul Zimmerman 		return;
4921197ba5f4SPaul Zimmerman 
4922197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
4923197ba5f4SPaul Zimmerman 	qh->tt_buffer_dirty = 0;
4924197ba5f4SPaul Zimmerman 
4925197ba5f4SPaul Zimmerman 	if (hsotg->flags.b.port_connect_status)
4926197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4927197ba5f4SPaul Zimmerman 
4928197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
4929197ba5f4SPaul Zimmerman }
4930197ba5f4SPaul Zimmerman 
4931ca8b0332SChen Yu /*
4932ca8b0332SChen Yu  * HPRT0_SPD_HIGH_SPEED: high speed
4933ca8b0332SChen Yu  * HPRT0_SPD_FULL_SPEED: full speed
4934ca8b0332SChen Yu  */
4935ca8b0332SChen Yu static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4936ca8b0332SChen Yu {
4937ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4938ca8b0332SChen Yu 
4939ca8b0332SChen Yu 	if (hsotg->params.speed == speed)
4940ca8b0332SChen Yu 		return;
4941ca8b0332SChen Yu 
4942ca8b0332SChen Yu 	hsotg->params.speed = speed;
4943ca8b0332SChen Yu 	queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4944ca8b0332SChen Yu }
4945ca8b0332SChen Yu 
4946ca8b0332SChen Yu static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4947ca8b0332SChen Yu {
4948ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4949ca8b0332SChen Yu 
4950ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4951ca8b0332SChen Yu 		return;
4952ca8b0332SChen Yu 
4953ca8b0332SChen Yu 	/*
4954ca8b0332SChen Yu 	 * On removal, set speed to default high-speed.
4955ca8b0332SChen Yu 	 */
4956ca8b0332SChen Yu 	if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4957ca8b0332SChen Yu 	    udev->parent->speed < USB_SPEED_HIGH) {
4958ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to default high-speed\n");
4959ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4960ca8b0332SChen Yu 	}
4961ca8b0332SChen Yu }
4962ca8b0332SChen Yu 
4963ca8b0332SChen Yu static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4964ca8b0332SChen Yu {
4965ca8b0332SChen Yu 	struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4966ca8b0332SChen Yu 
4967ca8b0332SChen Yu 	if (!hsotg->params.change_speed_quirk)
4968ca8b0332SChen Yu 		return 0;
4969ca8b0332SChen Yu 
4970ca8b0332SChen Yu 	if (udev->speed == USB_SPEED_HIGH) {
4971ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to high-speed\n");
4972ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4973ca8b0332SChen Yu 	} else if ((udev->speed == USB_SPEED_FULL ||
4974ca8b0332SChen Yu 				udev->speed == USB_SPEED_LOW)) {
4975ca8b0332SChen Yu 		/*
4976ca8b0332SChen Yu 		 * Change speed setting to full-speed if there's
4977ca8b0332SChen Yu 		 * a full-speed or low-speed device plugged in.
4978ca8b0332SChen Yu 		 */
4979ca8b0332SChen Yu 		dev_info(hsotg->dev, "Set speed to full-speed\n");
4980ca8b0332SChen Yu 		dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4981ca8b0332SChen Yu 	}
4982ca8b0332SChen Yu 
4983ca8b0332SChen Yu 	return 0;
4984ca8b0332SChen Yu }
4985ca8b0332SChen Yu 
4986197ba5f4SPaul Zimmerman static struct hc_driver dwc2_hc_driver = {
4987197ba5f4SPaul Zimmerman 	.description = "dwc2_hsotg",
4988197ba5f4SPaul Zimmerman 	.product_desc = "DWC OTG Controller",
4989197ba5f4SPaul Zimmerman 	.hcd_priv_size = sizeof(struct wrapper_priv_data),
4990197ba5f4SPaul Zimmerman 
4991197ba5f4SPaul Zimmerman 	.irq = _dwc2_hcd_irq,
49928add17cfSDouglas Anderson 	.flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
4993197ba5f4SPaul Zimmerman 
4994197ba5f4SPaul Zimmerman 	.start = _dwc2_hcd_start,
4995197ba5f4SPaul Zimmerman 	.stop = _dwc2_hcd_stop,
4996197ba5f4SPaul Zimmerman 	.urb_enqueue = _dwc2_hcd_urb_enqueue,
4997197ba5f4SPaul Zimmerman 	.urb_dequeue = _dwc2_hcd_urb_dequeue,
4998197ba5f4SPaul Zimmerman 	.endpoint_disable = _dwc2_hcd_endpoint_disable,
4999197ba5f4SPaul Zimmerman 	.endpoint_reset = _dwc2_hcd_endpoint_reset,
5000197ba5f4SPaul Zimmerman 	.get_frame_number = _dwc2_hcd_get_frame_number,
5001197ba5f4SPaul Zimmerman 
5002197ba5f4SPaul Zimmerman 	.hub_status_data = _dwc2_hcd_hub_status_data,
5003197ba5f4SPaul Zimmerman 	.hub_control = _dwc2_hcd_hub_control,
5004197ba5f4SPaul Zimmerman 	.clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
500599a65798SGregory Herrero 
500699a65798SGregory Herrero 	.bus_suspend = _dwc2_hcd_suspend,
500799a65798SGregory Herrero 	.bus_resume = _dwc2_hcd_resume,
50083bc04e28SDouglas Anderson 
50093bc04e28SDouglas Anderson 	.map_urb_for_dma	= dwc2_map_urb_for_dma,
50103bc04e28SDouglas Anderson 	.unmap_urb_for_dma	= dwc2_unmap_urb_for_dma,
5011197ba5f4SPaul Zimmerman };
5012197ba5f4SPaul Zimmerman 
5013197ba5f4SPaul Zimmerman /*
5014197ba5f4SPaul Zimmerman  * Frees secondary storage associated with the dwc2_hsotg structure contained
5015197ba5f4SPaul Zimmerman  * in the struct usb_hcd field
5016197ba5f4SPaul Zimmerman  */
5017197ba5f4SPaul Zimmerman static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
5018197ba5f4SPaul Zimmerman {
5019197ba5f4SPaul Zimmerman 	u32 ahbcfg;
5020197ba5f4SPaul Zimmerman 	u32 dctl;
5021197ba5f4SPaul Zimmerman 	int i;
5022197ba5f4SPaul Zimmerman 
5023197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
5024197ba5f4SPaul Zimmerman 
5025197ba5f4SPaul Zimmerman 	/* Free memory for QH/QTD lists */
5026197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
502738d2b5fbSDouglas Anderson 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
5028197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
5029197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
5030197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
5031197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
5032197ba5f4SPaul Zimmerman 	dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
5033197ba5f4SPaul Zimmerman 
5034197ba5f4SPaul Zimmerman 	/* Free memory for the host channels */
5035197ba5f4SPaul Zimmerman 	for (i = 0; i < MAX_EPS_CHANNELS; i++) {
5036197ba5f4SPaul Zimmerman 		struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
5037197ba5f4SPaul Zimmerman 
50389da51974SJohn Youn 		if (chan) {
5039197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
5040197ba5f4SPaul Zimmerman 				i, chan);
5041197ba5f4SPaul Zimmerman 			hsotg->hc_ptr_array[i] = NULL;
5042197ba5f4SPaul Zimmerman 			kfree(chan);
5043197ba5f4SPaul Zimmerman 		}
5044197ba5f4SPaul Zimmerman 	}
5045197ba5f4SPaul Zimmerman 
504695832c00SJohn Youn 	if (hsotg->params.host_dma) {
5047197ba5f4SPaul Zimmerman 		if (hsotg->status_buf) {
5048197ba5f4SPaul Zimmerman 			dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
5049197ba5f4SPaul Zimmerman 					  hsotg->status_buf,
5050197ba5f4SPaul Zimmerman 					  hsotg->status_buf_dma);
5051197ba5f4SPaul Zimmerman 			hsotg->status_buf = NULL;
5052197ba5f4SPaul Zimmerman 		}
5053197ba5f4SPaul Zimmerman 	} else {
5054197ba5f4SPaul Zimmerman 		kfree(hsotg->status_buf);
5055197ba5f4SPaul Zimmerman 		hsotg->status_buf = NULL;
5056197ba5f4SPaul Zimmerman 	}
5057197ba5f4SPaul Zimmerman 
5058f25c42b8SGevorg Sahakyan 	ahbcfg = dwc2_readl(hsotg, GAHBCFG);
5059197ba5f4SPaul Zimmerman 
5060197ba5f4SPaul Zimmerman 	/* Disable all interrupts */
5061197ba5f4SPaul Zimmerman 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
5062f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, ahbcfg, GAHBCFG);
5063f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0, GINTMSK);
5064197ba5f4SPaul Zimmerman 
5065197ba5f4SPaul Zimmerman 	if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
5066f25c42b8SGevorg Sahakyan 		dctl = dwc2_readl(hsotg, DCTL);
5067197ba5f4SPaul Zimmerman 		dctl |= DCTL_SFTDISCON;
5068f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, dctl, DCTL);
5069197ba5f4SPaul Zimmerman 	}
5070197ba5f4SPaul Zimmerman 
5071197ba5f4SPaul Zimmerman 	if (hsotg->wq_otg) {
5072197ba5f4SPaul Zimmerman 		if (!cancel_work_sync(&hsotg->wf_otg))
5073197ba5f4SPaul Zimmerman 			flush_workqueue(hsotg->wq_otg);
5074197ba5f4SPaul Zimmerman 		destroy_workqueue(hsotg->wq_otg);
5075197ba5f4SPaul Zimmerman 	}
5076197ba5f4SPaul Zimmerman 
5077c40cf770SDouglas Anderson 	cancel_work_sync(&hsotg->phy_reset_work);
5078c40cf770SDouglas Anderson 
5079197ba5f4SPaul Zimmerman 	del_timer(&hsotg->wkp_timer);
5080197ba5f4SPaul Zimmerman }
5081197ba5f4SPaul Zimmerman 
5082197ba5f4SPaul Zimmerman static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5083197ba5f4SPaul Zimmerman {
5084197ba5f4SPaul Zimmerman 	/* Turn off all host-specific interrupts */
5085197ba5f4SPaul Zimmerman 	dwc2_disable_host_interrupts(hsotg);
5086197ba5f4SPaul Zimmerman 
5087197ba5f4SPaul Zimmerman 	dwc2_hcd_free(hsotg);
5088197ba5f4SPaul Zimmerman }
5089197ba5f4SPaul Zimmerman 
5090197ba5f4SPaul Zimmerman /*
5091197ba5f4SPaul Zimmerman  * Initializes the HCD. This function allocates memory for and initializes the
5092197ba5f4SPaul Zimmerman  * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5093197ba5f4SPaul Zimmerman  * USB bus with the core and calls the hc_driver->start() function. It returns
5094197ba5f4SPaul Zimmerman  * a negative error on failure.
5095197ba5f4SPaul Zimmerman  */
50964fe160d5SHeiner Kallweit int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5097197ba5f4SPaul Zimmerman {
5098348becdcSHeiner Kallweit 	struct platform_device *pdev = to_platform_device(hsotg->dev);
5099348becdcSHeiner Kallweit 	struct resource *res;
5100197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5101197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *channel;
5102197ba5f4SPaul Zimmerman 	u32 hcfg;
5103197ba5f4SPaul Zimmerman 	int i, num_channels;
5104197ba5f4SPaul Zimmerman 	int retval;
5105197ba5f4SPaul Zimmerman 
5106f5500eccSDinh Nguyen 	if (usb_disabled())
5107f5500eccSDinh Nguyen 		return -ENODEV;
5108f5500eccSDinh Nguyen 
5109197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
5110197ba5f4SPaul Zimmerman 
5111197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5112197ba5f4SPaul Zimmerman 
5113f25c42b8SGevorg Sahakyan 	hcfg = dwc2_readl(hsotg, HCFG);
5114197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5115197ba5f4SPaul Zimmerman 
5116197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
51176396bb22SKees Cook 	hsotg->frame_num_array = kcalloc(FRAME_NUM_ARRAY_SIZE,
51186396bb22SKees Cook 					 sizeof(*hsotg->frame_num_array),
51196396bb22SKees Cook 					 GFP_KERNEL);
5120197ba5f4SPaul Zimmerman 	if (!hsotg->frame_num_array)
5121197ba5f4SPaul Zimmerman 		goto error1;
51226396bb22SKees Cook 	hsotg->last_frame_num_array =
51236396bb22SKees Cook 		kcalloc(FRAME_NUM_ARRAY_SIZE,
51246396bb22SKees Cook 			sizeof(*hsotg->last_frame_num_array), GFP_KERNEL);
5125197ba5f4SPaul Zimmerman 	if (!hsotg->last_frame_num_array)
5126197ba5f4SPaul Zimmerman 		goto error1;
5127197ba5f4SPaul Zimmerman #endif
5128483bb254SDouglas Anderson 	hsotg->last_frame_num = HFNUM_MAX_FRNUM;
5129197ba5f4SPaul Zimmerman 
5130197ba5f4SPaul Zimmerman 	/* Check if the bus driver or platform code has setup a dma_mask */
513195832c00SJohn Youn 	if (hsotg->params.host_dma &&
51329da51974SJohn Youn 	    !hsotg->dev->dma_mask) {
5133197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
5134197ba5f4SPaul Zimmerman 			 "dma_mask not set, disabling DMA\n");
5135fdb09b3eSNicholas Mc Guire 		hsotg->params.host_dma = false;
513695832c00SJohn Youn 		hsotg->params.dma_desc_enable = false;
5137197ba5f4SPaul Zimmerman 	}
5138197ba5f4SPaul Zimmerman 
5139197ba5f4SPaul Zimmerman 	/* Set device flags indicating whether the HCD supports DMA */
514095832c00SJohn Youn 	if (hsotg->params.host_dma) {
5141197ba5f4SPaul Zimmerman 		if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5142197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set DMA mask\n");
5143197ba5f4SPaul Zimmerman 		if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5144197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
5145197ba5f4SPaul Zimmerman 	}
5146197ba5f4SPaul Zimmerman 
5147ca8b0332SChen Yu 	if (hsotg->params.change_speed_quirk) {
5148ca8b0332SChen Yu 		dwc2_hc_driver.free_dev = dwc2_free_dev;
5149ca8b0332SChen Yu 		dwc2_hc_driver.reset_device = dwc2_reset_device;
5150ca8b0332SChen Yu 	}
5151ca8b0332SChen Yu 
51527b81cb6bSChristoph Hellwig 	if (hsotg->params.host_dma)
51537b81cb6bSChristoph Hellwig 		dwc2_hc_driver.flags |= HCD_DMA;
51547b81cb6bSChristoph Hellwig 
5155197ba5f4SPaul Zimmerman 	hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5156197ba5f4SPaul Zimmerman 	if (!hcd)
5157197ba5f4SPaul Zimmerman 		goto error1;
5158197ba5f4SPaul Zimmerman 
5159197ba5f4SPaul Zimmerman 	hcd->has_tt = 1;
5160197ba5f4SPaul Zimmerman 
5161348becdcSHeiner Kallweit 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5162856e6e8eSYang Yingliang 	if (!res) {
5163856e6e8eSYang Yingliang 		retval = -EINVAL;
51643755278fSMiaoqian Lin 		goto error2;
5165856e6e8eSYang Yingliang 	}
5166348becdcSHeiner Kallweit 	hcd->rsrc_start = res->start;
5167348becdcSHeiner Kallweit 	hcd->rsrc_len = resource_size(res);
5168348becdcSHeiner Kallweit 
5169197ba5f4SPaul Zimmerman 	((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
5170197ba5f4SPaul Zimmerman 	hsotg->priv = hcd;
5171197ba5f4SPaul Zimmerman 
5172197ba5f4SPaul Zimmerman 	/*
5173197ba5f4SPaul Zimmerman 	 * Disable the global interrupt until all the interrupt handlers are
5174197ba5f4SPaul Zimmerman 	 * installed
5175197ba5f4SPaul Zimmerman 	 */
5176197ba5f4SPaul Zimmerman 	dwc2_disable_global_interrupts(hsotg);
5177197ba5f4SPaul Zimmerman 
5178197ba5f4SPaul Zimmerman 	/* Initialize the DWC_otg core, and select the Phy type */
51790fe239bcSDouglas Anderson 	retval = dwc2_core_init(hsotg, true);
5180197ba5f4SPaul Zimmerman 	if (retval)
5181197ba5f4SPaul Zimmerman 		goto error2;
5182197ba5f4SPaul Zimmerman 
5183197ba5f4SPaul Zimmerman 	/* Create new workqueue and init work */
5184197ba5f4SPaul Zimmerman 	retval = -ENOMEM;
5185ec7b1268SBhaktipriya Shridhar 	hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
5186197ba5f4SPaul Zimmerman 	if (!hsotg->wq_otg) {
5187197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Failed to create workqueue\n");
5188197ba5f4SPaul Zimmerman 		goto error2;
5189197ba5f4SPaul Zimmerman 	}
5190197ba5f4SPaul Zimmerman 	INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5191197ba5f4SPaul Zimmerman 
5192e99e88a9SKees Cook 	timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
5193197ba5f4SPaul Zimmerman 
5194197ba5f4SPaul Zimmerman 	/* Initialize the non-periodic schedule */
5195197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
519638d2b5fbSDouglas Anderson 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
5197197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5198197ba5f4SPaul Zimmerman 
5199197ba5f4SPaul Zimmerman 	/* Initialize the periodic schedule */
5200197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5201197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5202197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5203197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5204197ba5f4SPaul Zimmerman 
5205c9c8ac01SDouglas Anderson 	INIT_LIST_HEAD(&hsotg->split_order);
5206c9c8ac01SDouglas Anderson 
5207197ba5f4SPaul Zimmerman 	/*
5208197ba5f4SPaul Zimmerman 	 * Create a host channel descriptor for each host channel implemented
5209197ba5f4SPaul Zimmerman 	 * in the controller. Initialize the channel descriptor array.
5210197ba5f4SPaul Zimmerman 	 */
5211197ba5f4SPaul Zimmerman 	INIT_LIST_HEAD(&hsotg->free_hc_list);
5212bea8e86cSJohn Youn 	num_channels = hsotg->params.host_channels;
5213197ba5f4SPaul Zimmerman 	memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5214197ba5f4SPaul Zimmerman 
5215197ba5f4SPaul Zimmerman 	for (i = 0; i < num_channels; i++) {
5216197ba5f4SPaul Zimmerman 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
52179da51974SJohn Youn 		if (!channel)
5218197ba5f4SPaul Zimmerman 			goto error3;
5219197ba5f4SPaul Zimmerman 		channel->hc_num = i;
5220c9c8ac01SDouglas Anderson 		INIT_LIST_HEAD(&channel->split_order_list_entry);
5221197ba5f4SPaul Zimmerman 		hsotg->hc_ptr_array[i] = channel;
5222197ba5f4SPaul Zimmerman 	}
5223197ba5f4SPaul Zimmerman 
5224c40cf770SDouglas Anderson 	/* Initialize work */
5225197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5226197ba5f4SPaul Zimmerman 	INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5227c40cf770SDouglas Anderson 	INIT_WORK(&hsotg->phy_reset_work, dwc2_hcd_phy_reset_func);
5228197ba5f4SPaul Zimmerman 
5229197ba5f4SPaul Zimmerman 	/*
5230197ba5f4SPaul Zimmerman 	 * Allocate space for storing data on status transactions. Normally no
5231197ba5f4SPaul Zimmerman 	 * data is sent, but this space acts as a bit bucket. This must be
5232197ba5f4SPaul Zimmerman 	 * done after usb_add_hcd since that function allocates the DMA buffer
5233197ba5f4SPaul Zimmerman 	 * pool.
5234197ba5f4SPaul Zimmerman 	 */
523595832c00SJohn Youn 	if (hsotg->params.host_dma)
5236197ba5f4SPaul Zimmerman 		hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5237197ba5f4SPaul Zimmerman 					DWC2_HCD_STATUS_BUF_SIZE,
5238197ba5f4SPaul Zimmerman 					&hsotg->status_buf_dma, GFP_KERNEL);
5239197ba5f4SPaul Zimmerman 	else
5240197ba5f4SPaul Zimmerman 		hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5241197ba5f4SPaul Zimmerman 					  GFP_KERNEL);
5242197ba5f4SPaul Zimmerman 
5243197ba5f4SPaul Zimmerman 	if (!hsotg->status_buf)
5244197ba5f4SPaul Zimmerman 		goto error3;
5245197ba5f4SPaul Zimmerman 
52463b5fcc9aSGregory Herrero 	/*
52473b5fcc9aSGregory Herrero 	 * Create kmem caches to handle descriptor buffers in descriptor
52483b5fcc9aSGregory Herrero 	 * DMA mode.
52493b5fcc9aSGregory Herrero 	 * Alignment must be set to 512 bytes.
52503b5fcc9aSGregory Herrero 	 */
5251bea8e86cSJohn Youn 	if (hsotg->params.dma_desc_enable ||
5252bea8e86cSJohn Youn 	    hsotg->params.dma_desc_fs_enable) {
52533b5fcc9aSGregory Herrero 		hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
5254ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52553b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
52563b5fcc9aSGregory Herrero 				NULL);
52573b5fcc9aSGregory Herrero 		if (!hsotg->desc_gen_cache) {
52583b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52593b5fcc9aSGregory Herrero 				"unable to create dwc2 generic desc cache\n");
52603b5fcc9aSGregory Herrero 
52613b5fcc9aSGregory Herrero 			/*
52623b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52633b5fcc9aSGregory Herrero 			 * usable.
52643b5fcc9aSGregory Herrero 			 */
526595832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
526695832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
52673b5fcc9aSGregory Herrero 		}
52683b5fcc9aSGregory Herrero 
52693b5fcc9aSGregory Herrero 		hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
5270ec703251SVahram Aharonyan 				sizeof(struct dwc2_dma_desc) *
52713b5fcc9aSGregory Herrero 				MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
52723b5fcc9aSGregory Herrero 		if (!hsotg->desc_hsisoc_cache) {
52733b5fcc9aSGregory Herrero 			dev_err(hsotg->dev,
52743b5fcc9aSGregory Herrero 				"unable to create dwc2 hs isoc desc cache\n");
52753b5fcc9aSGregory Herrero 
52763b5fcc9aSGregory Herrero 			kmem_cache_destroy(hsotg->desc_gen_cache);
52773b5fcc9aSGregory Herrero 
52783b5fcc9aSGregory Herrero 			/*
52793b5fcc9aSGregory Herrero 			 * Disable descriptor dma mode since it will not be
52803b5fcc9aSGregory Herrero 			 * usable.
52813b5fcc9aSGregory Herrero 			 */
528295832c00SJohn Youn 			hsotg->params.dma_desc_enable = false;
528395832c00SJohn Youn 			hsotg->params.dma_desc_fs_enable = false;
52843b5fcc9aSGregory Herrero 		}
52853b5fcc9aSGregory Herrero 	}
52863b5fcc9aSGregory Herrero 
5287af424a41SWilliam Wu 	if (hsotg->params.host_dma) {
5288af424a41SWilliam Wu 		/*
5289af424a41SWilliam Wu 		 * Create kmem caches to handle non-aligned buffer
5290af424a41SWilliam Wu 		 * in Buffer DMA mode.
5291af424a41SWilliam Wu 		 */
5292af424a41SWilliam Wu 		hsotg->unaligned_cache = kmem_cache_create("dwc2-unaligned-dma",
5293af424a41SWilliam Wu 						DWC2_KMEM_UNALIGNED_BUF_SIZE, 4,
5294af424a41SWilliam Wu 						SLAB_CACHE_DMA, NULL);
5295af424a41SWilliam Wu 		if (!hsotg->unaligned_cache)
5296af424a41SWilliam Wu 			dev_err(hsotg->dev,
5297af424a41SWilliam Wu 				"unable to create dwc2 unaligned cache\n");
5298af424a41SWilliam Wu 	}
5299af424a41SWilliam Wu 
5300197ba5f4SPaul Zimmerman 	hsotg->otg_port = 1;
5301197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
5302197ba5f4SPaul Zimmerman 	hsotg->frame_list_dma = 0;
5303197ba5f4SPaul Zimmerman 	hsotg->periodic_qh_count = 0;
5304197ba5f4SPaul Zimmerman 
5305197ba5f4SPaul Zimmerman 	/* Initiate lx_state to L3 disconnected state */
5306197ba5f4SPaul Zimmerman 	hsotg->lx_state = DWC2_L3;
5307197ba5f4SPaul Zimmerman 
5308197ba5f4SPaul Zimmerman 	hcd->self.otg_port = hsotg->otg_port;
5309197ba5f4SPaul Zimmerman 
5310197ba5f4SPaul Zimmerman 	/* Don't support SG list at this point */
5311197ba5f4SPaul Zimmerman 	hcd->self.sg_tablesize = 0;
5312197ba5f4SPaul Zimmerman 
53132c8845feSAmelie Delaunay 	hcd->tpl_support = of_usb_host_tpl_support(hsotg->dev->of_node);
53142c8845feSAmelie Delaunay 
53159df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
53169df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, &hcd->self);
53179df4ceacSMian Yousaf Kaukab 
5318197ba5f4SPaul Zimmerman 	/*
5319197ba5f4SPaul Zimmerman 	 * Finish generic HCD initialization and start the HCD. This function
5320197ba5f4SPaul Zimmerman 	 * allocates the DMA buffer pool, registers the USB bus, requests the
5321197ba5f4SPaul Zimmerman 	 * IRQ line, and calls hcd_start method.
5322197ba5f4SPaul Zimmerman 	 */
53234fe160d5SHeiner Kallweit 	retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
5324197ba5f4SPaul Zimmerman 	if (retval < 0)
53253b5fcc9aSGregory Herrero 		goto error4;
5326197ba5f4SPaul Zimmerman 
5327ec513b16SLinus Torvalds 	device_wakeup_enable(hcd->self.controller);
5328ec513b16SLinus Torvalds 
5329197ba5f4SPaul Zimmerman 	dwc2_hcd_dump_state(hsotg);
5330197ba5f4SPaul Zimmerman 
5331197ba5f4SPaul Zimmerman 	dwc2_enable_global_interrupts(hsotg);
5332197ba5f4SPaul Zimmerman 
5333197ba5f4SPaul Zimmerman 	return 0;
5334197ba5f4SPaul Zimmerman 
53353b5fcc9aSGregory Herrero error4:
5336af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53373b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5338af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
5339197ba5f4SPaul Zimmerman error3:
5340197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5341197ba5f4SPaul Zimmerman error2:
5342197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5343197ba5f4SPaul Zimmerman error1:
5344197ba5f4SPaul Zimmerman 
5345197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5346197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5347197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5348197ba5f4SPaul Zimmerman #endif
5349197ba5f4SPaul Zimmerman 
5350197ba5f4SPaul Zimmerman 	dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
5351197ba5f4SPaul Zimmerman 	return retval;
5352197ba5f4SPaul Zimmerman }
5353197ba5f4SPaul Zimmerman 
5354197ba5f4SPaul Zimmerman /*
5355197ba5f4SPaul Zimmerman  * Removes the HCD.
5356197ba5f4SPaul Zimmerman  * Frees memory and resources associated with the HCD and deregisters the bus.
5357197ba5f4SPaul Zimmerman  */
5358197ba5f4SPaul Zimmerman void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
5359197ba5f4SPaul Zimmerman {
5360197ba5f4SPaul Zimmerman 	struct usb_hcd *hcd;
5361197ba5f4SPaul Zimmerman 
5362197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
5363197ba5f4SPaul Zimmerman 
5364197ba5f4SPaul Zimmerman 	hcd = dwc2_hsotg_to_hcd(hsotg);
5365197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
5366197ba5f4SPaul Zimmerman 
5367197ba5f4SPaul Zimmerman 	if (!hcd) {
5368197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
5369197ba5f4SPaul Zimmerman 			__func__);
5370197ba5f4SPaul Zimmerman 		return;
5371197ba5f4SPaul Zimmerman 	}
5372197ba5f4SPaul Zimmerman 
53739df4ceacSMian Yousaf Kaukab 	if (!IS_ERR_OR_NULL(hsotg->uphy))
53749df4ceacSMian Yousaf Kaukab 		otg_set_host(hsotg->uphy->otg, NULL);
53759df4ceacSMian Yousaf Kaukab 
5376197ba5f4SPaul Zimmerman 	usb_remove_hcd(hcd);
5377197ba5f4SPaul Zimmerman 	hsotg->priv = NULL;
53783b5fcc9aSGregory Herrero 
5379af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->unaligned_cache);
53803b5fcc9aSGregory Herrero 	kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5381af424a41SWilliam Wu 	kmem_cache_destroy(hsotg->desc_gen_cache);
53823b5fcc9aSGregory Herrero 
5383197ba5f4SPaul Zimmerman 	dwc2_hcd_release(hsotg);
5384197ba5f4SPaul Zimmerman 	usb_put_hcd(hcd);
5385197ba5f4SPaul Zimmerman 
5386197ba5f4SPaul Zimmerman #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5387197ba5f4SPaul Zimmerman 	kfree(hsotg->last_frame_num_array);
5388197ba5f4SPaul Zimmerman 	kfree(hsotg->frame_num_array);
5389197ba5f4SPaul Zimmerman #endif
5390197ba5f4SPaul Zimmerman }
539158e52ff6SJohn Youn 
539258e52ff6SJohn Youn /**
539358e52ff6SJohn Youn  * dwc2_backup_host_registers() - Backup controller host registers.
539458e52ff6SJohn Youn  * When suspending usb bus, registers needs to be backuped
539558e52ff6SJohn Youn  * if controller power is disabled once suspended.
539658e52ff6SJohn Youn  *
539758e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
539858e52ff6SJohn Youn  */
539958e52ff6SJohn Youn int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
540058e52ff6SJohn Youn {
540158e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
540258e52ff6SJohn Youn 	int i;
540358e52ff6SJohn Youn 
540458e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
540558e52ff6SJohn Youn 
540658e52ff6SJohn Youn 	/* Backup Host regs */
540758e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
5408f25c42b8SGevorg Sahakyan 	hr->hcfg = dwc2_readl(hsotg, HCFG);
5409f25c42b8SGevorg Sahakyan 	hr->haintmsk = dwc2_readl(hsotg, HAINTMSK);
5410bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5411f25c42b8SGevorg Sahakyan 		hr->hcintmsk[i] = dwc2_readl(hsotg, HCINTMSK(i));
541258e52ff6SJohn Youn 
541358e52ff6SJohn Youn 	hr->hprt0 = dwc2_read_hprt0(hsotg);
5414f25c42b8SGevorg Sahakyan 	hr->hfir = dwc2_readl(hsotg, HFIR);
5415f25c42b8SGevorg Sahakyan 	hr->hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
541658e52ff6SJohn Youn 	hr->valid = true;
541758e52ff6SJohn Youn 
541858e52ff6SJohn Youn 	return 0;
541958e52ff6SJohn Youn }
542058e52ff6SJohn Youn 
542158e52ff6SJohn Youn /**
542258e52ff6SJohn Youn  * dwc2_restore_host_registers() - Restore controller host registers.
542358e52ff6SJohn Youn  * When resuming usb bus, device registers needs to be restored
542458e52ff6SJohn Youn  * if controller power were disabled.
542558e52ff6SJohn Youn  *
542658e52ff6SJohn Youn  * @hsotg: Programming view of the DWC_otg controller
542758e52ff6SJohn Youn  */
542858e52ff6SJohn Youn int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
542958e52ff6SJohn Youn {
543058e52ff6SJohn Youn 	struct dwc2_hregs_backup *hr;
543158e52ff6SJohn Youn 	int i;
543258e52ff6SJohn Youn 
543358e52ff6SJohn Youn 	dev_dbg(hsotg->dev, "%s\n", __func__);
543458e52ff6SJohn Youn 
543558e52ff6SJohn Youn 	/* Restore host regs */
543658e52ff6SJohn Youn 	hr = &hsotg->hr_backup;
543758e52ff6SJohn Youn 	if (!hr->valid) {
543858e52ff6SJohn Youn 		dev_err(hsotg->dev, "%s: no host registers to restore\n",
543958e52ff6SJohn Youn 			__func__);
544058e52ff6SJohn Youn 		return -EINVAL;
544158e52ff6SJohn Youn 	}
544258e52ff6SJohn Youn 	hr->valid = false;
544358e52ff6SJohn Youn 
5444f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5445f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->haintmsk, HAINTMSK);
544658e52ff6SJohn Youn 
5447bea8e86cSJohn Youn 	for (i = 0; i < hsotg->params.host_channels; ++i)
5448f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hr->hcintmsk[i], HCINTMSK(i));
544958e52ff6SJohn Youn 
5450f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hprt0, HPRT0);
5451f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hfir, HFIR);
5452f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hptxfsiz, HPTXFSIZ);
545358e52ff6SJohn Youn 	hsotg->frame_number = 0;
545458e52ff6SJohn Youn 
545558e52ff6SJohn Youn 	return 0;
545658e52ff6SJohn Youn }
5457c5c403dcSVardan Mikayelyan 
5458c5c403dcSVardan Mikayelyan /**
5459c5c403dcSVardan Mikayelyan  * dwc2_host_enter_hibernation() - Put controller in Hibernation.
5460c5c403dcSVardan Mikayelyan  *
5461c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5462c5c403dcSVardan Mikayelyan  */
5463c5c403dcSVardan Mikayelyan int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
5464c5c403dcSVardan Mikayelyan {
5465c5c403dcSVardan Mikayelyan 	unsigned long flags;
5466c5c403dcSVardan Mikayelyan 	int ret = 0;
5467c5c403dcSVardan Mikayelyan 	u32 hprt0;
5468c5c403dcSVardan Mikayelyan 	u32 pcgcctl;
5469c5c403dcSVardan Mikayelyan 	u32 gusbcfg;
5470c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5471c5c403dcSVardan Mikayelyan 
5472c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
5473c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_global_registers(hsotg);
5474c5c403dcSVardan Mikayelyan 	if (ret) {
5475c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5476c5c403dcSVardan Mikayelyan 			__func__);
5477c5c403dcSVardan Mikayelyan 		return ret;
5478c5c403dcSVardan Mikayelyan 	}
5479c5c403dcSVardan Mikayelyan 	ret = dwc2_backup_host_registers(hsotg);
5480c5c403dcSVardan Mikayelyan 	if (ret) {
5481c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5482c5c403dcSVardan Mikayelyan 			__func__);
5483c5c403dcSVardan Mikayelyan 		return ret;
5484c5c403dcSVardan Mikayelyan 	}
5485c5c403dcSVardan Mikayelyan 
5486c5c403dcSVardan Mikayelyan 	/* Enter USB Suspend Mode */
5487f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5488c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_SUSP;
5489c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5490f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5491c5c403dcSVardan Mikayelyan 
5492c5c403dcSVardan Mikayelyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
54935e3bbae8SArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 5000))
549407b8dc55SColin Ian King 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
5495c5c403dcSVardan Mikayelyan 
5496c5c403dcSVardan Mikayelyan 	/*
5497c5c403dcSVardan Mikayelyan 	 * We need to disable interrupts to prevent servicing of any IRQ
5498c5c403dcSVardan Mikayelyan 	 * during going to hibernation
5499c5c403dcSVardan Mikayelyan 	 */
5500c5c403dcSVardan Mikayelyan 	spin_lock_irqsave(&hsotg->lock, flags);
5501c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L2;
5502c5c403dcSVardan Mikayelyan 
5503f25c42b8SGevorg Sahakyan 	gusbcfg = dwc2_readl(hsotg, GUSBCFG);
5504c5c403dcSVardan Mikayelyan 	if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
5505c5c403dcSVardan Mikayelyan 		/* ULPI interface */
5506c5c403dcSVardan Mikayelyan 		/* Suspend the Phy Clock */
5507f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5508c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5509f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5510c5c403dcSVardan Mikayelyan 		udelay(10);
5511c5c403dcSVardan Mikayelyan 
5512f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5513c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5514f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5515c5c403dcSVardan Mikayelyan 		udelay(10);
5516c5c403dcSVardan Mikayelyan 	} else {
5517c5c403dcSVardan Mikayelyan 		/* UTMI+ Interface */
5518f25c42b8SGevorg Sahakyan 		gpwrdn = dwc2_readl(hsotg, GPWRDN);
5519c5c403dcSVardan Mikayelyan 		gpwrdn |= GPWRDN_PMUACTV;
5520f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, gpwrdn, GPWRDN);
5521c5c403dcSVardan Mikayelyan 		udelay(10);
5522c5c403dcSVardan Mikayelyan 
5523f25c42b8SGevorg Sahakyan 		pcgcctl = dwc2_readl(hsotg, PCGCTL);
5524c5c403dcSVardan Mikayelyan 		pcgcctl |= PCGCTL_STOPPCLK;
5525f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, pcgcctl, PCGCTL);
5526c5c403dcSVardan Mikayelyan 		udelay(10);
5527c5c403dcSVardan Mikayelyan 	}
5528c5c403dcSVardan Mikayelyan 
5529c5c403dcSVardan Mikayelyan 	/* Enable interrupts from wake up logic */
5530f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5531c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PMUINTSEL;
5532f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5533c5c403dcSVardan Mikayelyan 	udelay(10);
5534c5c403dcSVardan Mikayelyan 
5535c5c403dcSVardan Mikayelyan 	/* Unmask host mode interrupts in GPWRDN */
5536f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5537c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_DISCONN_DET_MSK;
5538c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5539c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5540f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5541c5c403dcSVardan Mikayelyan 	udelay(10);
5542c5c403dcSVardan Mikayelyan 
5543c5c403dcSVardan Mikayelyan 	/* Enable Power Down Clamp */
5544f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5545c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNCLMP;
5546f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5547c5c403dcSVardan Mikayelyan 	udelay(10);
5548c5c403dcSVardan Mikayelyan 
5549c5c403dcSVardan Mikayelyan 	/* Switch off VDD */
5550f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5551c5c403dcSVardan Mikayelyan 	gpwrdn |= GPWRDN_PWRDNSWTCH;
5552f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5553c5c403dcSVardan Mikayelyan 
5554c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 1;
5555c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 1;
5556c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation completed\n");
5557c5c403dcSVardan Mikayelyan 	spin_unlock_irqrestore(&hsotg->lock, flags);
5558c5c403dcSVardan Mikayelyan 	return ret;
5559c5c403dcSVardan Mikayelyan }
5560c5c403dcSVardan Mikayelyan 
5561c5c403dcSVardan Mikayelyan /*
5562c5c403dcSVardan Mikayelyan  * dwc2_host_exit_hibernation()
5563c5c403dcSVardan Mikayelyan  *
5564c5c403dcSVardan Mikayelyan  * @hsotg: Programming view of the DWC_otg controller
5565c5c403dcSVardan Mikayelyan  * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5566c5c403dcSVardan Mikayelyan  * @param reset: indicates whether resume is initiated by Reset.
5567c5c403dcSVardan Mikayelyan  *
5568c5c403dcSVardan Mikayelyan  * Return: non-zero if failed to enter to hibernation.
5569c5c403dcSVardan Mikayelyan  *
5570c5c403dcSVardan Mikayelyan  * This function is for exiting from Host mode hibernation by
5571c5c403dcSVardan Mikayelyan  * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
5572c5c403dcSVardan Mikayelyan  */
5573c5c403dcSVardan Mikayelyan int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
5574c5c403dcSVardan Mikayelyan 			       int reset)
5575c5c403dcSVardan Mikayelyan {
5576c5c403dcSVardan Mikayelyan 	u32 gpwrdn;
5577c5c403dcSVardan Mikayelyan 	u32 hprt0;
5578c5c403dcSVardan Mikayelyan 	int ret = 0;
5579c5c403dcSVardan Mikayelyan 	struct dwc2_gregs_backup *gr;
5580c5c403dcSVardan Mikayelyan 	struct dwc2_hregs_backup *hr;
5581c5c403dcSVardan Mikayelyan 
5582c5c403dcSVardan Mikayelyan 	gr = &hsotg->gr_backup;
5583c5c403dcSVardan Mikayelyan 	hr = &hsotg->hr_backup;
5584c5c403dcSVardan Mikayelyan 
5585c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev,
5586c5c403dcSVardan Mikayelyan 		"%s: called with rem_wakeup = %d reset = %d\n",
5587c5c403dcSVardan Mikayelyan 		__func__, rem_wakeup, reset);
5588c5c403dcSVardan Mikayelyan 
5589c5c403dcSVardan Mikayelyan 	dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
5590c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5591c5c403dcSVardan Mikayelyan 
5592c5c403dcSVardan Mikayelyan 	/*
5593c5c403dcSVardan Mikayelyan 	 * This step is not described in functional spec but if not wait for
5594c5c403dcSVardan Mikayelyan 	 * this delay, mismatch interrupts occurred because just after restore
5595c5c403dcSVardan Mikayelyan 	 * core is in Device mode(gintsts.curmode == 0)
5596c5c403dcSVardan Mikayelyan 	 */
5597c5c403dcSVardan Mikayelyan 	mdelay(100);
5598c5c403dcSVardan Mikayelyan 
5599c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5600f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5601c5c403dcSVardan Mikayelyan 
5602c5c403dcSVardan Mikayelyan 	/* De-assert Restore */
5603f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5604c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_RESTORE;
5605f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5606c5c403dcSVardan Mikayelyan 	udelay(10);
5607c5c403dcSVardan Mikayelyan 
5608c5c403dcSVardan Mikayelyan 	/* Restore GUSBCFG, HCFG */
5609f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5610f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hr->hcfg, HCFG);
5611c5c403dcSVardan Mikayelyan 
5612c5c403dcSVardan Mikayelyan 	/* De-assert Wakeup Logic */
5613f25c42b8SGevorg Sahakyan 	gpwrdn = dwc2_readl(hsotg, GPWRDN);
5614c5c403dcSVardan Mikayelyan 	gpwrdn &= ~GPWRDN_PMUACTV;
5615f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, gpwrdn, GPWRDN);
5616c5c403dcSVardan Mikayelyan 	udelay(10);
5617c5c403dcSVardan Mikayelyan 
5618c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5619c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5620c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5621c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5622f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5623c5c403dcSVardan Mikayelyan 
5624c5c403dcSVardan Mikayelyan 	hprt0 = hr->hprt0;
5625c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_PWR;
5626c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5627c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_SUSP;
5628c5c403dcSVardan Mikayelyan 
5629c5c403dcSVardan Mikayelyan 	if (reset) {
5630c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RST;
5631f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5632c5c403dcSVardan Mikayelyan 
5633c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5634c5c403dcSVardan Mikayelyan 		mdelay(60);
5635c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RST;
5636f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5637c5c403dcSVardan Mikayelyan 	} else {
5638c5c403dcSVardan Mikayelyan 		hprt0 |= HPRT0_RES;
5639f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5640c5c403dcSVardan Mikayelyan 
5641c5c403dcSVardan Mikayelyan 		/* Wait for Resume time and then program HPRT again */
5642c5c403dcSVardan Mikayelyan 		mdelay(100);
5643c5c403dcSVardan Mikayelyan 		hprt0 &= ~HPRT0_RES;
5644f25c42b8SGevorg Sahakyan 		dwc2_writel(hsotg, hprt0, HPRT0);
5645c5c403dcSVardan Mikayelyan 	}
5646c5c403dcSVardan Mikayelyan 	/* Clear all interrupt status */
5647f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5648c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_CONNDET;
5649c5c403dcSVardan Mikayelyan 	hprt0 |= HPRT0_ENACHG;
5650c5c403dcSVardan Mikayelyan 	hprt0 &= ~HPRT0_ENA;
5651f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, hprt0, HPRT0);
5652c5c403dcSVardan Mikayelyan 
5653f25c42b8SGevorg Sahakyan 	hprt0 = dwc2_readl(hsotg, HPRT0);
5654c5c403dcSVardan Mikayelyan 
5655c5c403dcSVardan Mikayelyan 	/* Clear all pending interupts */
5656f25c42b8SGevorg Sahakyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
5657c5c403dcSVardan Mikayelyan 
5658c5c403dcSVardan Mikayelyan 	/* Restore global registers */
5659c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_global_registers(hsotg);
5660c5c403dcSVardan Mikayelyan 	if (ret) {
5661c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore registers\n",
5662c5c403dcSVardan Mikayelyan 			__func__);
5663c5c403dcSVardan Mikayelyan 		return ret;
5664c5c403dcSVardan Mikayelyan 	}
5665c5c403dcSVardan Mikayelyan 
5666c5c403dcSVardan Mikayelyan 	/* Restore host registers */
5667c5c403dcSVardan Mikayelyan 	ret = dwc2_restore_host_registers(hsotg);
5668c5c403dcSVardan Mikayelyan 	if (ret) {
5669c5c403dcSVardan Mikayelyan 		dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5670c5c403dcSVardan Mikayelyan 			__func__);
5671c5c403dcSVardan Mikayelyan 		return ret;
5672c5c403dcSVardan Mikayelyan 	}
5673c5c403dcSVardan Mikayelyan 
5674c2db8d7bSArtur Petrosyan 	if (rem_wakeup) {
567522bb5cfdSArtur Petrosyan 		dwc2_hcd_rem_wakeup(hsotg);
5676c2db8d7bSArtur Petrosyan 		/*
5677c2db8d7bSArtur Petrosyan 		 * Change "port_connect_status_change" flag to re-enumerate,
5678c2db8d7bSArtur Petrosyan 		 * because after exit from hibernation port connection status
5679c2db8d7bSArtur Petrosyan 		 * is not detected.
5680c2db8d7bSArtur Petrosyan 		 */
5681c2db8d7bSArtur Petrosyan 		hsotg->flags.b.port_connect_status_change = 1;
5682c2db8d7bSArtur Petrosyan 	}
568322bb5cfdSArtur Petrosyan 
5684c5c403dcSVardan Mikayelyan 	hsotg->hibernated = 0;
5685c5c403dcSVardan Mikayelyan 	hsotg->bus_suspended = 0;
5686c5c403dcSVardan Mikayelyan 	hsotg->lx_state = DWC2_L0;
5687c5c403dcSVardan Mikayelyan 	dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
5688c5c403dcSVardan Mikayelyan 	return ret;
5689c5c403dcSVardan Mikayelyan }
5690c846b03fSDouglas Anderson 
5691c846b03fSDouglas Anderson bool dwc2_host_can_poweroff_phy(struct dwc2_hsotg *dwc2)
5692c846b03fSDouglas Anderson {
5693c846b03fSDouglas Anderson 	struct usb_device *root_hub = dwc2_hsotg_to_hcd(dwc2)->self.root_hub;
5694c846b03fSDouglas Anderson 
5695c846b03fSDouglas Anderson 	/* If the controller isn't allowed to wakeup then we can power off. */
5696c846b03fSDouglas Anderson 	if (!device_may_wakeup(dwc2->dev))
5697c846b03fSDouglas Anderson 		return true;
5698c846b03fSDouglas Anderson 
5699c846b03fSDouglas Anderson 	/*
5700c846b03fSDouglas Anderson 	 * We don't want to power off the PHY if something under the
5701c846b03fSDouglas Anderson 	 * root hub has wakeup enabled.
5702c846b03fSDouglas Anderson 	 */
5703c846b03fSDouglas Anderson 	if (usb_wakeup_enabled_descendants(root_hub))
5704c846b03fSDouglas Anderson 		return false;
5705c846b03fSDouglas Anderson 
5706c846b03fSDouglas Anderson 	/* No reason to keep the PHY powered, so allow poweroff */
5707c846b03fSDouglas Anderson 	return true;
5708c846b03fSDouglas Anderson }
57099ce9e5adSArtur Petrosyan 
57109ce9e5adSArtur Petrosyan /**
57119ce9e5adSArtur Petrosyan  * dwc2_host_enter_partial_power_down() - Put controller in partial
57129ce9e5adSArtur Petrosyan  * power down.
57139ce9e5adSArtur Petrosyan  *
57149ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
57159ce9e5adSArtur Petrosyan  *
57169ce9e5adSArtur Petrosyan  * Return: non-zero if failed to enter host partial power down.
57179ce9e5adSArtur Petrosyan  *
57189ce9e5adSArtur Petrosyan  * This function is for entering Host mode partial power down.
57199ce9e5adSArtur Petrosyan  */
57209ce9e5adSArtur Petrosyan int dwc2_host_enter_partial_power_down(struct dwc2_hsotg *hsotg)
57219ce9e5adSArtur Petrosyan {
57229ce9e5adSArtur Petrosyan 	u32 pcgcctl;
57239ce9e5adSArtur Petrosyan 	u32 hprt0;
57249ce9e5adSArtur Petrosyan 	int ret = 0;
57259ce9e5adSArtur Petrosyan 
57269ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down started.\n");
57279ce9e5adSArtur Petrosyan 
57289ce9e5adSArtur Petrosyan 	/* Put this port in suspend mode. */
57299ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
57309ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
57319ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
57329ce9e5adSArtur Petrosyan 	udelay(5);
57339ce9e5adSArtur Petrosyan 
57349ce9e5adSArtur Petrosyan 	/* Wait for the HPRT0.PrtSusp register field to be set */
57359ce9e5adSArtur Petrosyan 	if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 3000))
57369ce9e5adSArtur Petrosyan 		dev_warn(hsotg->dev, "Suspend wasn't generated\n");
57379ce9e5adSArtur Petrosyan 
57389ce9e5adSArtur Petrosyan 	/* Backup all registers */
57399ce9e5adSArtur Petrosyan 	ret = dwc2_backup_global_registers(hsotg);
57409ce9e5adSArtur Petrosyan 	if (ret) {
57419ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
57429ce9e5adSArtur Petrosyan 			__func__);
57439ce9e5adSArtur Petrosyan 		return ret;
57449ce9e5adSArtur Petrosyan 	}
57459ce9e5adSArtur Petrosyan 
57469ce9e5adSArtur Petrosyan 	ret = dwc2_backup_host_registers(hsotg);
57479ce9e5adSArtur Petrosyan 	if (ret) {
57489ce9e5adSArtur Petrosyan 		dev_err(hsotg->dev, "%s: failed to backup host registers\n",
57499ce9e5adSArtur Petrosyan 			__func__);
57509ce9e5adSArtur Petrosyan 		return ret;
57519ce9e5adSArtur Petrosyan 	}
57529ce9e5adSArtur Petrosyan 
57539ce9e5adSArtur Petrosyan 	/*
57549ce9e5adSArtur Petrosyan 	 * Clear any pending interrupts since dwc2 will not be able to
57559ce9e5adSArtur Petrosyan 	 * clear them after entering partial_power_down.
57569ce9e5adSArtur Petrosyan 	 */
57579ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, 0xffffffff, GINTSTS);
57589ce9e5adSArtur Petrosyan 
57599ce9e5adSArtur Petrosyan 	/* Put the controller in low power state */
57609ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
57619ce9e5adSArtur Petrosyan 
57629ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_PWRCLMP;
57639ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57649ce9e5adSArtur Petrosyan 	udelay(5);
57659ce9e5adSArtur Petrosyan 
57669ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
57679ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57689ce9e5adSArtur Petrosyan 	udelay(5);
57699ce9e5adSArtur Petrosyan 
57709ce9e5adSArtur Petrosyan 	pcgcctl |= PCGCTL_STOPPCLK;
57719ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
57729ce9e5adSArtur Petrosyan 
57739ce9e5adSArtur Petrosyan 	/* Set in_ppd flag to 1 as here core enters suspend. */
57749ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 1;
57759ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
57769ce9e5adSArtur Petrosyan 	hsotg->bus_suspended = true;
57779ce9e5adSArtur Petrosyan 
57789ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host partial power down completed.\n");
57799ce9e5adSArtur Petrosyan 
57809ce9e5adSArtur Petrosyan 	return ret;
57819ce9e5adSArtur Petrosyan }
57829ce9e5adSArtur Petrosyan 
57839ce9e5adSArtur Petrosyan /*
57849ce9e5adSArtur Petrosyan  * dwc2_host_exit_partial_power_down() - Exit controller from host partial
57859ce9e5adSArtur Petrosyan  * power down.
57869ce9e5adSArtur Petrosyan  *
57879ce9e5adSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
57889ce9e5adSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by Reset.
57899ce9e5adSArtur Petrosyan  * @restore: indicates whether need to restore the registers or not.
57909ce9e5adSArtur Petrosyan  *
57919ce9e5adSArtur Petrosyan  * Return: non-zero if failed to exit host partial power down.
57929ce9e5adSArtur Petrosyan  *
57939ce9e5adSArtur Petrosyan  * This function is for exiting from Host mode partial power down.
57949ce9e5adSArtur Petrosyan  */
57959ce9e5adSArtur Petrosyan int dwc2_host_exit_partial_power_down(struct dwc2_hsotg *hsotg,
57969ce9e5adSArtur Petrosyan 				      int rem_wakeup, bool restore)
57979ce9e5adSArtur Petrosyan {
57989ce9e5adSArtur Petrosyan 	u32 pcgcctl;
57999ce9e5adSArtur Petrosyan 	int ret = 0;
58009ce9e5adSArtur Petrosyan 	u32 hprt0;
58019ce9e5adSArtur Petrosyan 
58029ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down started.\n");
58039ce9e5adSArtur Petrosyan 
58049ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58059ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_STOPPCLK;
58069ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58079ce9e5adSArtur Petrosyan 	udelay(5);
58089ce9e5adSArtur Petrosyan 
58099ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58109ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_PWRCLMP;
58119ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58129ce9e5adSArtur Petrosyan 	udelay(5);
58139ce9e5adSArtur Petrosyan 
58149ce9e5adSArtur Petrosyan 	pcgcctl = dwc2_readl(hsotg, PCGCTL);
58159ce9e5adSArtur Petrosyan 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
58169ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, pcgcctl, PCGCTL);
58179ce9e5adSArtur Petrosyan 
58189ce9e5adSArtur Petrosyan 	udelay(100);
58199ce9e5adSArtur Petrosyan 	if (restore) {
58209ce9e5adSArtur Petrosyan 		ret = dwc2_restore_global_registers(hsotg);
58219ce9e5adSArtur Petrosyan 		if (ret) {
58229ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore registers\n",
58239ce9e5adSArtur Petrosyan 				__func__);
58249ce9e5adSArtur Petrosyan 			return ret;
58259ce9e5adSArtur Petrosyan 		}
58269ce9e5adSArtur Petrosyan 
58279ce9e5adSArtur Petrosyan 		ret = dwc2_restore_host_registers(hsotg);
58289ce9e5adSArtur Petrosyan 		if (ret) {
58299ce9e5adSArtur Petrosyan 			dev_err(hsotg->dev, "%s: failed to restore host registers\n",
58309ce9e5adSArtur Petrosyan 				__func__);
58319ce9e5adSArtur Petrosyan 			return ret;
58329ce9e5adSArtur Petrosyan 		}
58339ce9e5adSArtur Petrosyan 	}
58349ce9e5adSArtur Petrosyan 
58359ce9e5adSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
58369ce9e5adSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
58379ce9e5adSArtur Petrosyan 	hprt0 |= HPRT0_RES;
58389ce9e5adSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
58399ce9e5adSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
58409ce9e5adSArtur Petrosyan 	udelay(5);
58419ce9e5adSArtur Petrosyan 
58429ce9e5adSArtur Petrosyan 	if (!rem_wakeup) {
58439ce9e5adSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
58449ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58459ce9e5adSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
58469ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58479ce9e5adSArtur Petrosyan 
58489ce9e5adSArtur Petrosyan 		hsotg->bus_suspended = false;
58499ce9e5adSArtur Petrosyan 	} else {
58509ce9e5adSArtur Petrosyan 		/* Turn on the port power bit. */
58519ce9e5adSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
58529ce9e5adSArtur Petrosyan 		hprt0 |= HPRT0_PWR;
58539ce9e5adSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
58549ce9e5adSArtur Petrosyan 
58559ce9e5adSArtur Petrosyan 		/* Connect hcd. */
58569ce9e5adSArtur Petrosyan 		dwc2_hcd_connect(hsotg);
58579ce9e5adSArtur Petrosyan 
58589ce9e5adSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
58599ce9e5adSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
58609ce9e5adSArtur Petrosyan 	}
58619ce9e5adSArtur Petrosyan 
58629ce9e5adSArtur Petrosyan 	/* Set lx_state to and in_ppd to 0 as here core exits from suspend. */
58639ce9e5adSArtur Petrosyan 	hsotg->in_ppd = 0;
58649ce9e5adSArtur Petrosyan 	hsotg->lx_state = DWC2_L0;
58659ce9e5adSArtur Petrosyan 
58669ce9e5adSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host partial power down completed.\n");
58679ce9e5adSArtur Petrosyan 	return ret;
58689ce9e5adSArtur Petrosyan }
586979c87c3cSArtur Petrosyan 
587079c87c3cSArtur Petrosyan /**
587179c87c3cSArtur Petrosyan  * dwc2_host_enter_clock_gating() - Put controller in clock gating.
587279c87c3cSArtur Petrosyan  *
587379c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
587479c87c3cSArtur Petrosyan  *
587579c87c3cSArtur Petrosyan  * This function is for entering Host mode clock gating.
587679c87c3cSArtur Petrosyan  */
587779c87c3cSArtur Petrosyan void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
587879c87c3cSArtur Petrosyan {
587979c87c3cSArtur Petrosyan 	u32 hprt0;
588079c87c3cSArtur Petrosyan 	u32 pcgctl;
588179c87c3cSArtur Petrosyan 
588279c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Entering host clock gating.\n");
588379c87c3cSArtur Petrosyan 
588479c87c3cSArtur Petrosyan 	/* Put this port in suspend mode. */
588579c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
588679c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_SUSP;
588779c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
588879c87c3cSArtur Petrosyan 
588979c87c3cSArtur Petrosyan 	/* Set the Phy Clock bit as suspend is received. */
589079c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
589179c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_STOPPCLK;
589279c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
589379c87c3cSArtur Petrosyan 	udelay(5);
589479c87c3cSArtur Petrosyan 
589579c87c3cSArtur Petrosyan 	/* Set the Gate hclk as suspend is received. */
589679c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
589779c87c3cSArtur Petrosyan 	pcgctl |= PCGCTL_GATEHCLK;
589879c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
589979c87c3cSArtur Petrosyan 	udelay(5);
590079c87c3cSArtur Petrosyan 
590179c87c3cSArtur Petrosyan 	hsotg->bus_suspended = true;
590279c87c3cSArtur Petrosyan 	hsotg->lx_state = DWC2_L2;
590379c87c3cSArtur Petrosyan }
590479c87c3cSArtur Petrosyan 
590579c87c3cSArtur Petrosyan /**
590679c87c3cSArtur Petrosyan  * dwc2_host_exit_clock_gating() - Exit controller from clock gating.
590779c87c3cSArtur Petrosyan  *
590879c87c3cSArtur Petrosyan  * @hsotg: Programming view of the DWC_otg controller
590979c87c3cSArtur Petrosyan  * @rem_wakeup: indicates whether resume is initiated by remote wakeup
591079c87c3cSArtur Petrosyan  *
591179c87c3cSArtur Petrosyan  * This function is for exiting Host mode clock gating.
591279c87c3cSArtur Petrosyan  */
591379c87c3cSArtur Petrosyan void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
591479c87c3cSArtur Petrosyan {
591579c87c3cSArtur Petrosyan 	u32 hprt0;
591679c87c3cSArtur Petrosyan 	u32 pcgctl;
591779c87c3cSArtur Petrosyan 
591879c87c3cSArtur Petrosyan 	dev_dbg(hsotg->dev, "Exiting host clock gating.\n");
591979c87c3cSArtur Petrosyan 
592079c87c3cSArtur Petrosyan 	/* Clear the Gate hclk. */
592179c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
592279c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_GATEHCLK;
592379c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
592479c87c3cSArtur Petrosyan 	udelay(5);
592579c87c3cSArtur Petrosyan 
592679c87c3cSArtur Petrosyan 	/* Phy Clock bit. */
592779c87c3cSArtur Petrosyan 	pcgctl = dwc2_readl(hsotg, PCGCTL);
592879c87c3cSArtur Petrosyan 	pcgctl &= ~PCGCTL_STOPPCLK;
592979c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, pcgctl, PCGCTL);
593079c87c3cSArtur Petrosyan 	udelay(5);
593179c87c3cSArtur Petrosyan 
593279c87c3cSArtur Petrosyan 	/* Drive resume signaling and exit suspend mode on the port. */
593379c87c3cSArtur Petrosyan 	hprt0 = dwc2_read_hprt0(hsotg);
593479c87c3cSArtur Petrosyan 	hprt0 |= HPRT0_RES;
593579c87c3cSArtur Petrosyan 	hprt0 &= ~HPRT0_SUSP;
593679c87c3cSArtur Petrosyan 	dwc2_writel(hsotg, hprt0, HPRT0);
593779c87c3cSArtur Petrosyan 	udelay(5);
593879c87c3cSArtur Petrosyan 
593979c87c3cSArtur Petrosyan 	if (!rem_wakeup) {
594079c87c3cSArtur Petrosyan 		/* In case of port resume need to wait for 40 ms */
594179c87c3cSArtur Petrosyan 		msleep(USB_RESUME_TIMEOUT);
594279c87c3cSArtur Petrosyan 
594379c87c3cSArtur Petrosyan 		/* Stop driveing resume signaling on the port. */
594479c87c3cSArtur Petrosyan 		hprt0 = dwc2_read_hprt0(hsotg);
594579c87c3cSArtur Petrosyan 		hprt0 &= ~HPRT0_RES;
594679c87c3cSArtur Petrosyan 		dwc2_writel(hsotg, hprt0, HPRT0);
594779c87c3cSArtur Petrosyan 
594879c87c3cSArtur Petrosyan 		hsotg->bus_suspended = false;
594979c87c3cSArtur Petrosyan 		hsotg->lx_state = DWC2_L0;
595079c87c3cSArtur Petrosyan 	} else {
595179c87c3cSArtur Petrosyan 		mod_timer(&hsotg->wkp_timer,
595279c87c3cSArtur Petrosyan 			  jiffies + msecs_to_jiffies(71));
595379c87c3cSArtur Petrosyan 	}
595479c87c3cSArtur Petrosyan }
5955