xref: /linux/drivers/usb/dwc2/core.c (revision 09c96980dc723462ed2eeacc945fed5bcb278f85)
1197ba5f4SPaul Zimmerman /*
2197ba5f4SPaul Zimmerman  * core.c - DesignWare HS OTG Controller common routines
3197ba5f4SPaul Zimmerman  *
4197ba5f4SPaul Zimmerman  * Copyright (C) 2004-2013 Synopsys, Inc.
5197ba5f4SPaul Zimmerman  *
6197ba5f4SPaul Zimmerman  * Redistribution and use in source and binary forms, with or without
7197ba5f4SPaul Zimmerman  * modification, are permitted provided that the following conditions
8197ba5f4SPaul Zimmerman  * are met:
9197ba5f4SPaul Zimmerman  * 1. Redistributions of source code must retain the above copyright
10197ba5f4SPaul Zimmerman  *    notice, this list of conditions, and the following disclaimer,
11197ba5f4SPaul Zimmerman  *    without modification.
12197ba5f4SPaul Zimmerman  * 2. Redistributions in binary form must reproduce the above copyright
13197ba5f4SPaul Zimmerman  *    notice, this list of conditions and the following disclaimer in the
14197ba5f4SPaul Zimmerman  *    documentation and/or other materials provided with the distribution.
15197ba5f4SPaul Zimmerman  * 3. The names of the above-listed copyright holders may not be used
16197ba5f4SPaul Zimmerman  *    to endorse or promote products derived from this software without
17197ba5f4SPaul Zimmerman  *    specific prior written permission.
18197ba5f4SPaul Zimmerman  *
19197ba5f4SPaul Zimmerman  * ALTERNATIVELY, this software may be distributed under the terms of the
20197ba5f4SPaul Zimmerman  * GNU General Public License ("GPL") as published by the Free Software
21197ba5f4SPaul Zimmerman  * Foundation; either version 2 of the License, or (at your option) any
22197ba5f4SPaul Zimmerman  * later version.
23197ba5f4SPaul Zimmerman  *
24197ba5f4SPaul Zimmerman  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25197ba5f4SPaul Zimmerman  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26197ba5f4SPaul Zimmerman  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27197ba5f4SPaul Zimmerman  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28197ba5f4SPaul Zimmerman  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29197ba5f4SPaul Zimmerman  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30197ba5f4SPaul Zimmerman  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31197ba5f4SPaul Zimmerman  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32197ba5f4SPaul Zimmerman  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33197ba5f4SPaul Zimmerman  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34197ba5f4SPaul Zimmerman  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35197ba5f4SPaul Zimmerman  */
36197ba5f4SPaul Zimmerman 
37197ba5f4SPaul Zimmerman /*
38197ba5f4SPaul Zimmerman  * The Core code provides basic services for accessing and managing the
39197ba5f4SPaul Zimmerman  * DWC_otg hardware. These services are used by both the Host Controller
40197ba5f4SPaul Zimmerman  * Driver and the Peripheral Controller Driver.
41197ba5f4SPaul Zimmerman  */
42197ba5f4SPaul Zimmerman #include <linux/kernel.h>
43197ba5f4SPaul Zimmerman #include <linux/module.h>
44197ba5f4SPaul Zimmerman #include <linux/moduleparam.h>
45197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
46197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
47197ba5f4SPaul Zimmerman #include <linux/dma-mapping.h>
48197ba5f4SPaul Zimmerman #include <linux/delay.h>
49197ba5f4SPaul Zimmerman #include <linux/io.h>
50197ba5f4SPaul Zimmerman #include <linux/slab.h>
51197ba5f4SPaul Zimmerman #include <linux/usb.h>
52197ba5f4SPaul Zimmerman 
53197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
54197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
55197ba5f4SPaul Zimmerman 
56197ba5f4SPaul Zimmerman #include "core.h"
57197ba5f4SPaul Zimmerman #include "hcd.h"
58197ba5f4SPaul Zimmerman 
59d17ee77bSGregory Herrero #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
60d17ee77bSGregory Herrero /**
61d17ee77bSGregory Herrero  * dwc2_backup_host_registers() - Backup controller host registers.
62d17ee77bSGregory Herrero  * When suspending usb bus, registers needs to be backuped
63d17ee77bSGregory Herrero  * if controller power is disabled once suspended.
64d17ee77bSGregory Herrero  *
65d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
66d17ee77bSGregory Herrero  */
67d17ee77bSGregory Herrero static int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
68d17ee77bSGregory Herrero {
69d17ee77bSGregory Herrero 	struct dwc2_hregs_backup *hr;
70d17ee77bSGregory Herrero 	int i;
71d17ee77bSGregory Herrero 
72d17ee77bSGregory Herrero 	dev_dbg(hsotg->dev, "%s\n", __func__);
73d17ee77bSGregory Herrero 
74d17ee77bSGregory Herrero 	/* Backup Host regs */
75cc1e204cSMian Yousaf Kaukab 	hr = &hsotg->hr_backup;
7695c8bc36SAntti Seppälä 	hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
7795c8bc36SAntti Seppälä 	hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
78d17ee77bSGregory Herrero 	for (i = 0; i < hsotg->core_params->host_channels; ++i)
7995c8bc36SAntti Seppälä 		hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
80d17ee77bSGregory Herrero 
81cc047ce4SGregory Herrero 	hr->hprt0 = dwc2_read_hprt0(hsotg);
8295c8bc36SAntti Seppälä 	hr->hfir = dwc2_readl(hsotg->regs + HFIR);
83cc1e204cSMian Yousaf Kaukab 	hr->valid = true;
84d17ee77bSGregory Herrero 
85d17ee77bSGregory Herrero 	return 0;
86d17ee77bSGregory Herrero }
87d17ee77bSGregory Herrero 
88d17ee77bSGregory Herrero /**
89d17ee77bSGregory Herrero  * dwc2_restore_host_registers() - Restore controller host registers.
90d17ee77bSGregory Herrero  * When resuming usb bus, device registers needs to be restored
91d17ee77bSGregory Herrero  * if controller power were disabled.
92d17ee77bSGregory Herrero  *
93d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
94d17ee77bSGregory Herrero  */
95d17ee77bSGregory Herrero static int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
96d17ee77bSGregory Herrero {
97d17ee77bSGregory Herrero 	struct dwc2_hregs_backup *hr;
98d17ee77bSGregory Herrero 	int i;
99d17ee77bSGregory Herrero 
100d17ee77bSGregory Herrero 	dev_dbg(hsotg->dev, "%s\n", __func__);
101d17ee77bSGregory Herrero 
102d17ee77bSGregory Herrero 	/* Restore host regs */
103cc1e204cSMian Yousaf Kaukab 	hr = &hsotg->hr_backup;
104cc1e204cSMian Yousaf Kaukab 	if (!hr->valid) {
105d17ee77bSGregory Herrero 		dev_err(hsotg->dev, "%s: no host registers to restore\n",
106d17ee77bSGregory Herrero 				__func__);
107d17ee77bSGregory Herrero 		return -EINVAL;
108d17ee77bSGregory Herrero 	}
109cc1e204cSMian Yousaf Kaukab 	hr->valid = false;
110d17ee77bSGregory Herrero 
11195c8bc36SAntti Seppälä 	dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
11295c8bc36SAntti Seppälä 	dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
113d17ee77bSGregory Herrero 
114d17ee77bSGregory Herrero 	for (i = 0; i < hsotg->core_params->host_channels; ++i)
11595c8bc36SAntti Seppälä 		dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
116d17ee77bSGregory Herrero 
11795c8bc36SAntti Seppälä 	dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
11895c8bc36SAntti Seppälä 	dwc2_writel(hr->hfir, hsotg->regs + HFIR);
11908c4ffc2SGregory Herrero 	hsotg->frame_number = 0;
120d17ee77bSGregory Herrero 
121d17ee77bSGregory Herrero 	return 0;
122d17ee77bSGregory Herrero }
123d17ee77bSGregory Herrero #else
124d17ee77bSGregory Herrero static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
125d17ee77bSGregory Herrero { return 0; }
126d17ee77bSGregory Herrero 
127d17ee77bSGregory Herrero static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
128d17ee77bSGregory Herrero { return 0; }
129d17ee77bSGregory Herrero #endif
130d17ee77bSGregory Herrero 
131d17ee77bSGregory Herrero #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
132d17ee77bSGregory Herrero 	IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
133d17ee77bSGregory Herrero /**
134d17ee77bSGregory Herrero  * dwc2_backup_device_registers() - Backup controller device registers.
135d17ee77bSGregory Herrero  * When suspending usb bus, registers needs to be backuped
136d17ee77bSGregory Herrero  * if controller power is disabled once suspended.
137d17ee77bSGregory Herrero  *
138d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
139d17ee77bSGregory Herrero  */
140d17ee77bSGregory Herrero static int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
141d17ee77bSGregory Herrero {
142d17ee77bSGregory Herrero 	struct dwc2_dregs_backup *dr;
143d17ee77bSGregory Herrero 	int i;
144d17ee77bSGregory Herrero 
145d17ee77bSGregory Herrero 	dev_dbg(hsotg->dev, "%s\n", __func__);
146d17ee77bSGregory Herrero 
147d17ee77bSGregory Herrero 	/* Backup dev regs */
148cc1e204cSMian Yousaf Kaukab 	dr = &hsotg->dr_backup;
149d17ee77bSGregory Herrero 
15095c8bc36SAntti Seppälä 	dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
15195c8bc36SAntti Seppälä 	dr->dctl = dwc2_readl(hsotg->regs + DCTL);
15295c8bc36SAntti Seppälä 	dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
15395c8bc36SAntti Seppälä 	dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
15495c8bc36SAntti Seppälä 	dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
155d17ee77bSGregory Herrero 
156d17ee77bSGregory Herrero 	for (i = 0; i < hsotg->num_of_eps; i++) {
157d17ee77bSGregory Herrero 		/* Backup IN EPs */
15895c8bc36SAntti Seppälä 		dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
159d17ee77bSGregory Herrero 
160d17ee77bSGregory Herrero 		/* Ensure DATA PID is correctly configured */
161d17ee77bSGregory Herrero 		if (dr->diepctl[i] & DXEPCTL_DPID)
162d17ee77bSGregory Herrero 			dr->diepctl[i] |= DXEPCTL_SETD1PID;
163d17ee77bSGregory Herrero 		else
164d17ee77bSGregory Herrero 			dr->diepctl[i] |= DXEPCTL_SETD0PID;
165d17ee77bSGregory Herrero 
16695c8bc36SAntti Seppälä 		dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
16795c8bc36SAntti Seppälä 		dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
168d17ee77bSGregory Herrero 
169d17ee77bSGregory Herrero 		/* Backup OUT EPs */
17095c8bc36SAntti Seppälä 		dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
171d17ee77bSGregory Herrero 
172d17ee77bSGregory Herrero 		/* Ensure DATA PID is correctly configured */
173d17ee77bSGregory Herrero 		if (dr->doepctl[i] & DXEPCTL_DPID)
174d17ee77bSGregory Herrero 			dr->doepctl[i] |= DXEPCTL_SETD1PID;
175d17ee77bSGregory Herrero 		else
176d17ee77bSGregory Herrero 			dr->doepctl[i] |= DXEPCTL_SETD0PID;
177d17ee77bSGregory Herrero 
17895c8bc36SAntti Seppälä 		dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
17995c8bc36SAntti Seppälä 		dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
180d17ee77bSGregory Herrero 	}
181cc1e204cSMian Yousaf Kaukab 	dr->valid = true;
182d17ee77bSGregory Herrero 	return 0;
183d17ee77bSGregory Herrero }
184d17ee77bSGregory Herrero 
185d17ee77bSGregory Herrero /**
186d17ee77bSGregory Herrero  * dwc2_restore_device_registers() - Restore controller device registers.
187d17ee77bSGregory Herrero  * When resuming usb bus, device registers needs to be restored
188d17ee77bSGregory Herrero  * if controller power were disabled.
189d17ee77bSGregory Herrero  *
190d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
191d17ee77bSGregory Herrero  */
192d17ee77bSGregory Herrero static int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
193d17ee77bSGregory Herrero {
194d17ee77bSGregory Herrero 	struct dwc2_dregs_backup *dr;
195d17ee77bSGregory Herrero 	u32 dctl;
196d17ee77bSGregory Herrero 	int i;
197d17ee77bSGregory Herrero 
198d17ee77bSGregory Herrero 	dev_dbg(hsotg->dev, "%s\n", __func__);
199d17ee77bSGregory Herrero 
200d17ee77bSGregory Herrero 	/* Restore dev regs */
201cc1e204cSMian Yousaf Kaukab 	dr = &hsotg->dr_backup;
202cc1e204cSMian Yousaf Kaukab 	if (!dr->valid) {
203d17ee77bSGregory Herrero 		dev_err(hsotg->dev, "%s: no device registers to restore\n",
204d17ee77bSGregory Herrero 				__func__);
205d17ee77bSGregory Herrero 		return -EINVAL;
206d17ee77bSGregory Herrero 	}
207cc1e204cSMian Yousaf Kaukab 	dr->valid = false;
208d17ee77bSGregory Herrero 
20995c8bc36SAntti Seppälä 	dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
21095c8bc36SAntti Seppälä 	dwc2_writel(dr->dctl, hsotg->regs + DCTL);
21195c8bc36SAntti Seppälä 	dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
21295c8bc36SAntti Seppälä 	dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
21395c8bc36SAntti Seppälä 	dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
214d17ee77bSGregory Herrero 
215d17ee77bSGregory Herrero 	for (i = 0; i < hsotg->num_of_eps; i++) {
216d17ee77bSGregory Herrero 		/* Restore IN EPs */
21795c8bc36SAntti Seppälä 		dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
21895c8bc36SAntti Seppälä 		dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
21995c8bc36SAntti Seppälä 		dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
220d17ee77bSGregory Herrero 
221d17ee77bSGregory Herrero 		/* Restore OUT EPs */
22295c8bc36SAntti Seppälä 		dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
22395c8bc36SAntti Seppälä 		dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
22495c8bc36SAntti Seppälä 		dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
225d17ee77bSGregory Herrero 	}
226d17ee77bSGregory Herrero 
227d17ee77bSGregory Herrero 	/* Set the Power-On Programming done bit */
22895c8bc36SAntti Seppälä 	dctl = dwc2_readl(hsotg->regs + DCTL);
229d17ee77bSGregory Herrero 	dctl |= DCTL_PWRONPRGDONE;
23095c8bc36SAntti Seppälä 	dwc2_writel(dctl, hsotg->regs + DCTL);
231d17ee77bSGregory Herrero 
232d17ee77bSGregory Herrero 	return 0;
233d17ee77bSGregory Herrero }
234d17ee77bSGregory Herrero #else
235d17ee77bSGregory Herrero static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
236d17ee77bSGregory Herrero { return 0; }
237d17ee77bSGregory Herrero 
238d17ee77bSGregory Herrero static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
239d17ee77bSGregory Herrero { return 0; }
240d17ee77bSGregory Herrero #endif
241d17ee77bSGregory Herrero 
242d17ee77bSGregory Herrero /**
243d17ee77bSGregory Herrero  * dwc2_backup_global_registers() - Backup global controller registers.
244d17ee77bSGregory Herrero  * When suspending usb bus, registers needs to be backuped
245d17ee77bSGregory Herrero  * if controller power is disabled once suspended.
246d17ee77bSGregory Herrero  *
247d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
248d17ee77bSGregory Herrero  */
249d17ee77bSGregory Herrero static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
250d17ee77bSGregory Herrero {
251d17ee77bSGregory Herrero 	struct dwc2_gregs_backup *gr;
252d17ee77bSGregory Herrero 	int i;
253d17ee77bSGregory Herrero 
254d17ee77bSGregory Herrero 	/* Backup global regs */
255cc1e204cSMian Yousaf Kaukab 	gr = &hsotg->gr_backup;
256d17ee77bSGregory Herrero 
25795c8bc36SAntti Seppälä 	gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
25895c8bc36SAntti Seppälä 	gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
25995c8bc36SAntti Seppälä 	gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
26095c8bc36SAntti Seppälä 	gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
26195c8bc36SAntti Seppälä 	gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
26295c8bc36SAntti Seppälä 	gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
26395c8bc36SAntti Seppälä 	gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
26495c8bc36SAntti Seppälä 	gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
265d17ee77bSGregory Herrero 	for (i = 0; i < MAX_EPS_CHANNELS; i++)
26695c8bc36SAntti Seppälä 		gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
267d17ee77bSGregory Herrero 
268cc1e204cSMian Yousaf Kaukab 	gr->valid = true;
269d17ee77bSGregory Herrero 	return 0;
270d17ee77bSGregory Herrero }
271d17ee77bSGregory Herrero 
272d17ee77bSGregory Herrero /**
273d17ee77bSGregory Herrero  * dwc2_restore_global_registers() - Restore controller global registers.
274d17ee77bSGregory Herrero  * When resuming usb bus, device registers needs to be restored
275d17ee77bSGregory Herrero  * if controller power were disabled.
276d17ee77bSGregory Herrero  *
277d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
278d17ee77bSGregory Herrero  */
279d17ee77bSGregory Herrero static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
280d17ee77bSGregory Herrero {
281d17ee77bSGregory Herrero 	struct dwc2_gregs_backup *gr;
282d17ee77bSGregory Herrero 	int i;
283d17ee77bSGregory Herrero 
284d17ee77bSGregory Herrero 	dev_dbg(hsotg->dev, "%s\n", __func__);
285d17ee77bSGregory Herrero 
286d17ee77bSGregory Herrero 	/* Restore global regs */
287cc1e204cSMian Yousaf Kaukab 	gr = &hsotg->gr_backup;
288cc1e204cSMian Yousaf Kaukab 	if (!gr->valid) {
289d17ee77bSGregory Herrero 		dev_err(hsotg->dev, "%s: no global registers to restore\n",
290d17ee77bSGregory Herrero 				__func__);
291d17ee77bSGregory Herrero 		return -EINVAL;
292d17ee77bSGregory Herrero 	}
293cc1e204cSMian Yousaf Kaukab 	gr->valid = false;
294d17ee77bSGregory Herrero 
29595c8bc36SAntti Seppälä 	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
29695c8bc36SAntti Seppälä 	dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
29795c8bc36SAntti Seppälä 	dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
29895c8bc36SAntti Seppälä 	dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
29995c8bc36SAntti Seppälä 	dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
30095c8bc36SAntti Seppälä 	dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
30195c8bc36SAntti Seppälä 	dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
30295c8bc36SAntti Seppälä 	dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
30395c8bc36SAntti Seppälä 	dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
304d17ee77bSGregory Herrero 	for (i = 0; i < MAX_EPS_CHANNELS; i++)
30595c8bc36SAntti Seppälä 		dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
306d17ee77bSGregory Herrero 
307d17ee77bSGregory Herrero 	return 0;
308d17ee77bSGregory Herrero }
309d17ee77bSGregory Herrero 
310d17ee77bSGregory Herrero /**
311d17ee77bSGregory Herrero  * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
312d17ee77bSGregory Herrero  *
313d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
314d17ee77bSGregory Herrero  * @restore: Controller registers need to be restored
315d17ee77bSGregory Herrero  */
316d17ee77bSGregory Herrero int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
317d17ee77bSGregory Herrero {
318d17ee77bSGregory Herrero 	u32 pcgcctl;
319d17ee77bSGregory Herrero 	int ret = 0;
320d17ee77bSGregory Herrero 
321285046aaSGregory Herrero 	if (!hsotg->core_params->hibernation)
322285046aaSGregory Herrero 		return -ENOTSUPP;
323285046aaSGregory Herrero 
32495c8bc36SAntti Seppälä 	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
325d17ee77bSGregory Herrero 	pcgcctl &= ~PCGCTL_STOPPCLK;
32695c8bc36SAntti Seppälä 	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
327d17ee77bSGregory Herrero 
32895c8bc36SAntti Seppälä 	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
329d17ee77bSGregory Herrero 	pcgcctl &= ~PCGCTL_PWRCLMP;
33095c8bc36SAntti Seppälä 	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
331d17ee77bSGregory Herrero 
33295c8bc36SAntti Seppälä 	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
333d17ee77bSGregory Herrero 	pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
33495c8bc36SAntti Seppälä 	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
335d17ee77bSGregory Herrero 
336d17ee77bSGregory Herrero 	udelay(100);
337d17ee77bSGregory Herrero 	if (restore) {
338d17ee77bSGregory Herrero 		ret = dwc2_restore_global_registers(hsotg);
339d17ee77bSGregory Herrero 		if (ret) {
340d17ee77bSGregory Herrero 			dev_err(hsotg->dev, "%s: failed to restore registers\n",
341d17ee77bSGregory Herrero 					__func__);
342d17ee77bSGregory Herrero 			return ret;
343d17ee77bSGregory Herrero 		}
344d17ee77bSGregory Herrero 		if (dwc2_is_host_mode(hsotg)) {
345d17ee77bSGregory Herrero 			ret = dwc2_restore_host_registers(hsotg);
346d17ee77bSGregory Herrero 			if (ret) {
347d17ee77bSGregory Herrero 				dev_err(hsotg->dev, "%s: failed to restore host registers\n",
348d17ee77bSGregory Herrero 						__func__);
349d17ee77bSGregory Herrero 				return ret;
350d17ee77bSGregory Herrero 			}
351d17ee77bSGregory Herrero 		} else {
352d17ee77bSGregory Herrero 			ret = dwc2_restore_device_registers(hsotg);
353d17ee77bSGregory Herrero 			if (ret) {
354d17ee77bSGregory Herrero 				dev_err(hsotg->dev, "%s: failed to restore device registers\n",
355d17ee77bSGregory Herrero 						__func__);
356d17ee77bSGregory Herrero 				return ret;
357d17ee77bSGregory Herrero 			}
358d17ee77bSGregory Herrero 		}
359d17ee77bSGregory Herrero 	}
360d17ee77bSGregory Herrero 
361d17ee77bSGregory Herrero 	return ret;
362d17ee77bSGregory Herrero }
363d17ee77bSGregory Herrero 
364d17ee77bSGregory Herrero /**
365d17ee77bSGregory Herrero  * dwc2_enter_hibernation() - Put controller in Partial Power Down.
366d17ee77bSGregory Herrero  *
367d17ee77bSGregory Herrero  * @hsotg: Programming view of the DWC_otg controller
368d17ee77bSGregory Herrero  */
369d17ee77bSGregory Herrero int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
370d17ee77bSGregory Herrero {
371d17ee77bSGregory Herrero 	u32 pcgcctl;
372d17ee77bSGregory Herrero 	int ret = 0;
373d17ee77bSGregory Herrero 
374285046aaSGregory Herrero 	if (!hsotg->core_params->hibernation)
375285046aaSGregory Herrero 		return -ENOTSUPP;
376285046aaSGregory Herrero 
377d17ee77bSGregory Herrero 	/* Backup all registers */
378d17ee77bSGregory Herrero 	ret = dwc2_backup_global_registers(hsotg);
379d17ee77bSGregory Herrero 	if (ret) {
380d17ee77bSGregory Herrero 		dev_err(hsotg->dev, "%s: failed to backup global registers\n",
381d17ee77bSGregory Herrero 				__func__);
382d17ee77bSGregory Herrero 		return ret;
383d17ee77bSGregory Herrero 	}
384d17ee77bSGregory Herrero 
385d17ee77bSGregory Herrero 	if (dwc2_is_host_mode(hsotg)) {
386d17ee77bSGregory Herrero 		ret = dwc2_backup_host_registers(hsotg);
387d17ee77bSGregory Herrero 		if (ret) {
388d17ee77bSGregory Herrero 			dev_err(hsotg->dev, "%s: failed to backup host registers\n",
389d17ee77bSGregory Herrero 					__func__);
390d17ee77bSGregory Herrero 			return ret;
391d17ee77bSGregory Herrero 		}
392d17ee77bSGregory Herrero 	} else {
393d17ee77bSGregory Herrero 		ret = dwc2_backup_device_registers(hsotg);
394d17ee77bSGregory Herrero 		if (ret) {
395d17ee77bSGregory Herrero 			dev_err(hsotg->dev, "%s: failed to backup device registers\n",
396d17ee77bSGregory Herrero 					__func__);
397d17ee77bSGregory Herrero 			return ret;
398d17ee77bSGregory Herrero 		}
399d17ee77bSGregory Herrero 	}
400d17ee77bSGregory Herrero 
401cad73da2SGregory Herrero 	/*
402cad73da2SGregory Herrero 	 * Clear any pending interrupts since dwc2 will not be able to
403cad73da2SGregory Herrero 	 * clear them after entering hibernation.
404cad73da2SGregory Herrero 	 */
405cad73da2SGregory Herrero 	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
406cad73da2SGregory Herrero 
407d17ee77bSGregory Herrero 	/* Put the controller in low power state */
40895c8bc36SAntti Seppälä 	pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
409d17ee77bSGregory Herrero 
410d17ee77bSGregory Herrero 	pcgcctl |= PCGCTL_PWRCLMP;
41195c8bc36SAntti Seppälä 	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
412d17ee77bSGregory Herrero 	ndelay(20);
413d17ee77bSGregory Herrero 
414d17ee77bSGregory Herrero 	pcgcctl |= PCGCTL_RSTPDWNMODULE;
41595c8bc36SAntti Seppälä 	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
416d17ee77bSGregory Herrero 	ndelay(20);
417d17ee77bSGregory Herrero 
418d17ee77bSGregory Herrero 	pcgcctl |= PCGCTL_STOPPCLK;
41995c8bc36SAntti Seppälä 	dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
420d17ee77bSGregory Herrero 
421d17ee77bSGregory Herrero 	return ret;
422d17ee77bSGregory Herrero }
423d17ee77bSGregory Herrero 
424197ba5f4SPaul Zimmerman /**
425197ba5f4SPaul Zimmerman  * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
426197ba5f4SPaul Zimmerman  * used in both device and host modes
427197ba5f4SPaul Zimmerman  *
428197ba5f4SPaul Zimmerman  * @hsotg: Programming view of the DWC_otg controller
429197ba5f4SPaul Zimmerman  */
430197ba5f4SPaul Zimmerman static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
431197ba5f4SPaul Zimmerman {
432197ba5f4SPaul Zimmerman 	u32 intmsk;
433197ba5f4SPaul Zimmerman 
434197ba5f4SPaul Zimmerman 	/* Clear any pending OTG Interrupts */
43595c8bc36SAntti Seppälä 	dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
436197ba5f4SPaul Zimmerman 
437197ba5f4SPaul Zimmerman 	/* Clear any pending interrupts */
43895c8bc36SAntti Seppälä 	dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
439197ba5f4SPaul Zimmerman 
440197ba5f4SPaul Zimmerman 	/* Enable the interrupts in the GINTMSK */
441197ba5f4SPaul Zimmerman 	intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
442197ba5f4SPaul Zimmerman 
443197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_enable <= 0)
444197ba5f4SPaul Zimmerman 		intmsk |= GINTSTS_RXFLVL;
445a6d249d8SGregory Herrero 	if (hsotg->core_params->external_id_pin_ctl <= 0)
446a6d249d8SGregory Herrero 		intmsk |= GINTSTS_CONIDSTSCHNG;
447197ba5f4SPaul Zimmerman 
448a6d249d8SGregory Herrero 	intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
449197ba5f4SPaul Zimmerman 		  GINTSTS_SESSREQINT;
450197ba5f4SPaul Zimmerman 
45195c8bc36SAntti Seppälä 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
452197ba5f4SPaul Zimmerman }
453197ba5f4SPaul Zimmerman 
454197ba5f4SPaul Zimmerman /*
455197ba5f4SPaul Zimmerman  * Initializes the FSLSPClkSel field of the HCFG register depending on the
456197ba5f4SPaul Zimmerman  * PHY type
457197ba5f4SPaul Zimmerman  */
458197ba5f4SPaul Zimmerman static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
459197ba5f4SPaul Zimmerman {
460197ba5f4SPaul Zimmerman 	u32 hcfg, val;
461197ba5f4SPaul Zimmerman 
462197ba5f4SPaul Zimmerman 	if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
463197ba5f4SPaul Zimmerman 	     hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
464197ba5f4SPaul Zimmerman 	     hsotg->core_params->ulpi_fs_ls > 0) ||
465197ba5f4SPaul Zimmerman 	    hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
466197ba5f4SPaul Zimmerman 		/* Full speed PHY */
467197ba5f4SPaul Zimmerman 		val = HCFG_FSLSPCLKSEL_48_MHZ;
468197ba5f4SPaul Zimmerman 	} else {
469197ba5f4SPaul Zimmerman 		/* High speed PHY running at full speed or high speed */
470197ba5f4SPaul Zimmerman 		val = HCFG_FSLSPCLKSEL_30_60_MHZ;
471197ba5f4SPaul Zimmerman 	}
472197ba5f4SPaul Zimmerman 
473197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
47495c8bc36SAntti Seppälä 	hcfg = dwc2_readl(hsotg->regs + HCFG);
475197ba5f4SPaul Zimmerman 	hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
476197ba5f4SPaul Zimmerman 	hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
47795c8bc36SAntti Seppälä 	dwc2_writel(hcfg, hsotg->regs + HCFG);
478197ba5f4SPaul Zimmerman }
479197ba5f4SPaul Zimmerman 
480197ba5f4SPaul Zimmerman /*
481197ba5f4SPaul Zimmerman  * Do core a soft reset of the core.  Be careful with this because it
482197ba5f4SPaul Zimmerman  * resets all the internal state machines of the core.
483197ba5f4SPaul Zimmerman  */
484b5d308abSJohn Youn int dwc2_core_reset(struct dwc2_hsotg *hsotg)
485197ba5f4SPaul Zimmerman {
486197ba5f4SPaul Zimmerman 	u32 greset;
487197ba5f4SPaul Zimmerman 	int count = 0;
488197ba5f4SPaul Zimmerman 
489197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
490197ba5f4SPaul Zimmerman 
491197ba5f4SPaul Zimmerman 	/* Core Soft Reset */
492b8ccc593SJohn Youn 	greset = dwc2_readl(hsotg->regs + GRSTCTL);
493197ba5f4SPaul Zimmerman 	greset |= GRSTCTL_CSFTRST;
49495c8bc36SAntti Seppälä 	dwc2_writel(greset, hsotg->regs + GRSTCTL);
495197ba5f4SPaul Zimmerman 	do {
49620bde643SYunzhi Li 		udelay(1);
49795c8bc36SAntti Seppälä 		greset = dwc2_readl(hsotg->regs + GRSTCTL);
498197ba5f4SPaul Zimmerman 		if (++count > 50) {
499197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev,
500197ba5f4SPaul Zimmerman 				 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
501197ba5f4SPaul Zimmerman 				 __func__, greset);
502197ba5f4SPaul Zimmerman 			return -EBUSY;
503197ba5f4SPaul Zimmerman 		}
504197ba5f4SPaul Zimmerman 	} while (greset & GRSTCTL_CSFTRST);
505197ba5f4SPaul Zimmerman 
506b8ccc593SJohn Youn 	/* Wait for AHB master IDLE state */
507b8ccc593SJohn Youn 	count = 0;
508b8ccc593SJohn Youn 	do {
509b8ccc593SJohn Youn 		udelay(1);
510b8ccc593SJohn Youn 		greset = dwc2_readl(hsotg->regs + GRSTCTL);
511b8ccc593SJohn Youn 		if (++count > 50) {
512b8ccc593SJohn Youn 			dev_warn(hsotg->dev,
513b8ccc593SJohn Youn 				 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
514b8ccc593SJohn Youn 				 __func__, greset);
515b8ccc593SJohn Youn 			return -EBUSY;
516b8ccc593SJohn Youn 		}
517b8ccc593SJohn Youn 	} while (!(greset & GRSTCTL_AHBIDLE));
518b8ccc593SJohn Youn 
519b5d308abSJohn Youn 	return 0;
520b5d308abSJohn Youn }
521b5d308abSJohn Youn 
522b5d308abSJohn Youn /*
523*09c96980SJohn Youn  * Force the mode of the controller.
524*09c96980SJohn Youn  *
525*09c96980SJohn Youn  * Forcing the mode is needed for two cases:
526*09c96980SJohn Youn  *
527*09c96980SJohn Youn  * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
528*09c96980SJohn Youn  * controller to stay in a particular mode regardless of ID pin
529*09c96980SJohn Youn  * changes. We do this usually after a core reset.
530*09c96980SJohn Youn  *
531*09c96980SJohn Youn  * 2) During probe we want to read reset values of the hw
532*09c96980SJohn Youn  * configuration registers that are only available in either host or
533*09c96980SJohn Youn  * device mode. We may need to force the mode if the current mode does
534*09c96980SJohn Youn  * not allow us to access the register in the mode that we want.
535*09c96980SJohn Youn  *
536*09c96980SJohn Youn  * In either case it only makes sense to force the mode if the
537*09c96980SJohn Youn  * controller hardware is OTG capable.
538*09c96980SJohn Youn  *
539*09c96980SJohn Youn  * Checks are done in this function to determine whether doing a force
540*09c96980SJohn Youn  * would be valid or not.
541*09c96980SJohn Youn  *
542*09c96980SJohn Youn  * If a force is done, it requires a 25ms delay to take effect.
543*09c96980SJohn Youn  *
544*09c96980SJohn Youn  * Returns true if the mode was forced.
545*09c96980SJohn Youn  */
546*09c96980SJohn Youn static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
547*09c96980SJohn Youn {
548*09c96980SJohn Youn 	u32 gusbcfg;
549*09c96980SJohn Youn 	u32 set;
550*09c96980SJohn Youn 	u32 clear;
551*09c96980SJohn Youn 
552*09c96980SJohn Youn 	dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
553*09c96980SJohn Youn 
554*09c96980SJohn Youn 	/*
555*09c96980SJohn Youn 	 * Force mode has no effect if the hardware is not OTG.
556*09c96980SJohn Youn 	 */
557*09c96980SJohn Youn 	if (!dwc2_hw_is_otg(hsotg))
558*09c96980SJohn Youn 		return false;
559*09c96980SJohn Youn 
560*09c96980SJohn Youn 	/*
561*09c96980SJohn Youn 	 * If dr_mode is either peripheral or host only, there is no
562*09c96980SJohn Youn 	 * need to ever force the mode to the opposite mode.
563*09c96980SJohn Youn 	 */
564*09c96980SJohn Youn 	if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
565*09c96980SJohn Youn 		return false;
566*09c96980SJohn Youn 
567*09c96980SJohn Youn 	if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
568*09c96980SJohn Youn 		return false;
569*09c96980SJohn Youn 
570*09c96980SJohn Youn 	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
571*09c96980SJohn Youn 
572*09c96980SJohn Youn 	set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
573*09c96980SJohn Youn 	clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
574*09c96980SJohn Youn 
575*09c96980SJohn Youn 	/*
576*09c96980SJohn Youn 	 * If the force mode bit is already set, don't set it.
577*09c96980SJohn Youn 	 */
578*09c96980SJohn Youn 	if ((gusbcfg & set) && !(gusbcfg & clear))
579*09c96980SJohn Youn 		return false;
580*09c96980SJohn Youn 
581*09c96980SJohn Youn 	gusbcfg &= ~clear;
582*09c96980SJohn Youn 	gusbcfg |= set;
583*09c96980SJohn Youn 	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
584*09c96980SJohn Youn 
585*09c96980SJohn Youn 	msleep(25);
586*09c96980SJohn Youn 	return true;
587*09c96980SJohn Youn }
588*09c96980SJohn Youn 
589*09c96980SJohn Youn /*
590*09c96980SJohn Youn  * Clears the force mode bits.
591*09c96980SJohn Youn  */
592*09c96980SJohn Youn static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
593*09c96980SJohn Youn {
594*09c96980SJohn Youn 	u32 gusbcfg;
595*09c96980SJohn Youn 
596*09c96980SJohn Youn 	gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
597*09c96980SJohn Youn 	gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
598*09c96980SJohn Youn 	gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
599*09c96980SJohn Youn 	dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
600*09c96980SJohn Youn 
601*09c96980SJohn Youn 	/*
602*09c96980SJohn Youn 	 * NOTE: This long sleep is _very_ important, otherwise the core will
603*09c96980SJohn Youn 	 * not stay in host mode after a connector ID change!
604*09c96980SJohn Youn 	 */
605*09c96980SJohn Youn 	usleep_range(150000, 160000);
606*09c96980SJohn Youn }
607*09c96980SJohn Youn 
608*09c96980SJohn Youn /*
609*09c96980SJohn Youn  * Sets or clears force mode based on the dr_mode parameter.
610*09c96980SJohn Youn  */
611*09c96980SJohn Youn void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
612*09c96980SJohn Youn {
613*09c96980SJohn Youn 	switch (hsotg->dr_mode) {
614*09c96980SJohn Youn 	case USB_DR_MODE_HOST:
615*09c96980SJohn Youn 		dwc2_force_mode(hsotg, true);
616*09c96980SJohn Youn 		break;
617*09c96980SJohn Youn 	case USB_DR_MODE_PERIPHERAL:
618*09c96980SJohn Youn 		dwc2_force_mode(hsotg, false);
619*09c96980SJohn Youn 		break;
620*09c96980SJohn Youn 	case USB_DR_MODE_OTG:
621*09c96980SJohn Youn 		dwc2_clear_force_mode(hsotg);
622*09c96980SJohn Youn 		break;
623*09c96980SJohn Youn 	default:
624*09c96980SJohn Youn 		dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
625*09c96980SJohn Youn 			 __func__, hsotg->dr_mode);
626*09c96980SJohn Youn 		break;
627*09c96980SJohn Youn 	}
628*09c96980SJohn Youn }
629*09c96980SJohn Youn 
630*09c96980SJohn Youn /*
631b5d308abSJohn Youn  * Do core a soft reset of the core.  Be careful with this because it
632b5d308abSJohn Youn  * resets all the internal state machines of the core.
633b5d308abSJohn Youn  *
634b5d308abSJohn Youn  * Additionally this will apply force mode as per the hsotg->dr_mode
635b5d308abSJohn Youn  * parameter.
636b5d308abSJohn Youn  */
637b5d308abSJohn Youn int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg)
638b5d308abSJohn Youn {
639b5d308abSJohn Youn 	int retval;
640b5d308abSJohn Youn 
641b5d308abSJohn Youn 	retval = dwc2_core_reset(hsotg);
642b5d308abSJohn Youn 	if (retval)
643b5d308abSJohn Youn 		return retval;
644b5d308abSJohn Youn 
645*09c96980SJohn Youn 	dwc2_force_dr_mode(hsotg);
646197ba5f4SPaul Zimmerman 	return 0;
647197ba5f4SPaul Zimmerman }
648197ba5f4SPaul Zimmerman 
649197ba5f4SPaul Zimmerman static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
650197ba5f4SPaul Zimmerman {
651197ba5f4SPaul Zimmerman 	u32 usbcfg, i2cctl;
652197ba5f4SPaul Zimmerman 	int retval = 0;
653197ba5f4SPaul Zimmerman 
654197ba5f4SPaul Zimmerman 	/*
655197ba5f4SPaul Zimmerman 	 * core_init() is now called on every switch so only call the
656197ba5f4SPaul Zimmerman 	 * following for the first time through
657197ba5f4SPaul Zimmerman 	 */
658197ba5f4SPaul Zimmerman 	if (select_phy) {
659197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "FS PHY selected\n");
6607d56cc26SDouglas Anderson 
66195c8bc36SAntti Seppälä 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
6627d56cc26SDouglas Anderson 		if (!(usbcfg & GUSBCFG_PHYSEL)) {
663197ba5f4SPaul Zimmerman 			usbcfg |= GUSBCFG_PHYSEL;
66495c8bc36SAntti Seppälä 			dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
665197ba5f4SPaul Zimmerman 
666197ba5f4SPaul Zimmerman 			/* Reset after a PHY select */
6676d58f346SJohn Youn 			retval = dwc2_core_reset_and_force_dr_mode(hsotg);
6687d56cc26SDouglas Anderson 
669197ba5f4SPaul Zimmerman 			if (retval) {
6707d56cc26SDouglas Anderson 				dev_err(hsotg->dev,
6717d56cc26SDouglas Anderson 					"%s: Reset failed, aborting", __func__);
672197ba5f4SPaul Zimmerman 				return retval;
673197ba5f4SPaul Zimmerman 			}
674197ba5f4SPaul Zimmerman 		}
6757d56cc26SDouglas Anderson 	}
676197ba5f4SPaul Zimmerman 
677197ba5f4SPaul Zimmerman 	/*
678197ba5f4SPaul Zimmerman 	 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
679197ba5f4SPaul Zimmerman 	 * do this on HNP Dev/Host mode switches (done in dev_init and
680197ba5f4SPaul Zimmerman 	 * host_init).
681197ba5f4SPaul Zimmerman 	 */
682197ba5f4SPaul Zimmerman 	if (dwc2_is_host_mode(hsotg))
683197ba5f4SPaul Zimmerman 		dwc2_init_fs_ls_pclk_sel(hsotg);
684197ba5f4SPaul Zimmerman 
685197ba5f4SPaul Zimmerman 	if (hsotg->core_params->i2c_enable > 0) {
686197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
687197ba5f4SPaul Zimmerman 
688197ba5f4SPaul Zimmerman 		/* Program GUSBCFG.OtgUtmiFsSel to I2C */
68995c8bc36SAntti Seppälä 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
690197ba5f4SPaul Zimmerman 		usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
69195c8bc36SAntti Seppälä 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
692197ba5f4SPaul Zimmerman 
693197ba5f4SPaul Zimmerman 		/* Program GI2CCTL.I2CEn */
69495c8bc36SAntti Seppälä 		i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
695197ba5f4SPaul Zimmerman 		i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
696197ba5f4SPaul Zimmerman 		i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
697197ba5f4SPaul Zimmerman 		i2cctl &= ~GI2CCTL_I2CEN;
69895c8bc36SAntti Seppälä 		dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
699197ba5f4SPaul Zimmerman 		i2cctl |= GI2CCTL_I2CEN;
70095c8bc36SAntti Seppälä 		dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
701197ba5f4SPaul Zimmerman 	}
702197ba5f4SPaul Zimmerman 
703197ba5f4SPaul Zimmerman 	return retval;
704197ba5f4SPaul Zimmerman }
705197ba5f4SPaul Zimmerman 
706197ba5f4SPaul Zimmerman static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
707197ba5f4SPaul Zimmerman {
7087d56cc26SDouglas Anderson 	u32 usbcfg, usbcfg_old;
709197ba5f4SPaul Zimmerman 	int retval = 0;
710197ba5f4SPaul Zimmerman 
711197ba5f4SPaul Zimmerman 	if (!select_phy)
712a23666c4SPaul Zimmerman 		return 0;
713197ba5f4SPaul Zimmerman 
7147d56cc26SDouglas Anderson 	usbcfg = usbcfg_old = dwc2_readl(hsotg->regs + GUSBCFG);
715197ba5f4SPaul Zimmerman 
716197ba5f4SPaul Zimmerman 	/*
717197ba5f4SPaul Zimmerman 	 * HS PHY parameters. These parameters are preserved during soft reset
718197ba5f4SPaul Zimmerman 	 * so only program the first time. Do a soft reset immediately after
719197ba5f4SPaul Zimmerman 	 * setting phyif.
720197ba5f4SPaul Zimmerman 	 */
721197ba5f4SPaul Zimmerman 	switch (hsotg->core_params->phy_type) {
722197ba5f4SPaul Zimmerman 	case DWC2_PHY_TYPE_PARAM_ULPI:
723197ba5f4SPaul Zimmerman 		/* ULPI interface */
724197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
725197ba5f4SPaul Zimmerman 		usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
726197ba5f4SPaul Zimmerman 		usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
727197ba5f4SPaul Zimmerman 		if (hsotg->core_params->phy_ulpi_ddr > 0)
728197ba5f4SPaul Zimmerman 			usbcfg |= GUSBCFG_DDRSEL;
729197ba5f4SPaul Zimmerman 		break;
730197ba5f4SPaul Zimmerman 	case DWC2_PHY_TYPE_PARAM_UTMI:
731197ba5f4SPaul Zimmerman 		/* UTMI+ interface */
732197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
733197ba5f4SPaul Zimmerman 		usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
734197ba5f4SPaul Zimmerman 		if (hsotg->core_params->phy_utmi_width == 16)
735197ba5f4SPaul Zimmerman 			usbcfg |= GUSBCFG_PHYIF16;
736197ba5f4SPaul Zimmerman 		break;
737197ba5f4SPaul Zimmerman 	default:
738197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "FS PHY selected at HS!\n");
739197ba5f4SPaul Zimmerman 		break;
740197ba5f4SPaul Zimmerman 	}
741197ba5f4SPaul Zimmerman 
7427d56cc26SDouglas Anderson 	if (usbcfg != usbcfg_old) {
74395c8bc36SAntti Seppälä 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
744197ba5f4SPaul Zimmerman 
745197ba5f4SPaul Zimmerman 		/* Reset after setting the PHY parameters */
7466d58f346SJohn Youn 		retval = dwc2_core_reset_and_force_dr_mode(hsotg);
747197ba5f4SPaul Zimmerman 		if (retval) {
7487d56cc26SDouglas Anderson 			dev_err(hsotg->dev,
7497d56cc26SDouglas Anderson 				"%s: Reset failed, aborting", __func__);
750197ba5f4SPaul Zimmerman 			return retval;
751197ba5f4SPaul Zimmerman 		}
7527d56cc26SDouglas Anderson 	}
753197ba5f4SPaul Zimmerman 
754197ba5f4SPaul Zimmerman 	return retval;
755197ba5f4SPaul Zimmerman }
756197ba5f4SPaul Zimmerman 
757197ba5f4SPaul Zimmerman static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
758197ba5f4SPaul Zimmerman {
759197ba5f4SPaul Zimmerman 	u32 usbcfg;
760197ba5f4SPaul Zimmerman 	int retval = 0;
761197ba5f4SPaul Zimmerman 
762197ba5f4SPaul Zimmerman 	if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
763197ba5f4SPaul Zimmerman 	    hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
764197ba5f4SPaul Zimmerman 		/* If FS mode with FS PHY */
765197ba5f4SPaul Zimmerman 		retval = dwc2_fs_phy_init(hsotg, select_phy);
766197ba5f4SPaul Zimmerman 		if (retval)
767197ba5f4SPaul Zimmerman 			return retval;
768197ba5f4SPaul Zimmerman 	} else {
769197ba5f4SPaul Zimmerman 		/* High speed PHY */
770197ba5f4SPaul Zimmerman 		retval = dwc2_hs_phy_init(hsotg, select_phy);
771197ba5f4SPaul Zimmerman 		if (retval)
772197ba5f4SPaul Zimmerman 			return retval;
773197ba5f4SPaul Zimmerman 	}
774197ba5f4SPaul Zimmerman 
775197ba5f4SPaul Zimmerman 	if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
776197ba5f4SPaul Zimmerman 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
777197ba5f4SPaul Zimmerman 	    hsotg->core_params->ulpi_fs_ls > 0) {
778197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
77995c8bc36SAntti Seppälä 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
780197ba5f4SPaul Zimmerman 		usbcfg |= GUSBCFG_ULPI_FS_LS;
781197ba5f4SPaul Zimmerman 		usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
78295c8bc36SAntti Seppälä 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
783197ba5f4SPaul Zimmerman 	} else {
78495c8bc36SAntti Seppälä 		usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
785197ba5f4SPaul Zimmerman 		usbcfg &= ~GUSBCFG_ULPI_FS_LS;
786197ba5f4SPaul Zimmerman 		usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
78795c8bc36SAntti Seppälä 		dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
788197ba5f4SPaul Zimmerman 	}
789197ba5f4SPaul Zimmerman 
790197ba5f4SPaul Zimmerman 	return retval;
791197ba5f4SPaul Zimmerman }
792197ba5f4SPaul Zimmerman 
793197ba5f4SPaul Zimmerman static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
794197ba5f4SPaul Zimmerman {
79595c8bc36SAntti Seppälä 	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
796197ba5f4SPaul Zimmerman 
797197ba5f4SPaul Zimmerman 	switch (hsotg->hw_params.arch) {
798197ba5f4SPaul Zimmerman 	case GHWCFG2_EXT_DMA_ARCH:
799197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "External DMA Mode not supported\n");
800197ba5f4SPaul Zimmerman 		return -EINVAL;
801197ba5f4SPaul Zimmerman 
802197ba5f4SPaul Zimmerman 	case GHWCFG2_INT_DMA_ARCH:
803197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Internal DMA Mode\n");
804197ba5f4SPaul Zimmerman 		if (hsotg->core_params->ahbcfg != -1) {
805197ba5f4SPaul Zimmerman 			ahbcfg &= GAHBCFG_CTRL_MASK;
806197ba5f4SPaul Zimmerman 			ahbcfg |= hsotg->core_params->ahbcfg &
807197ba5f4SPaul Zimmerman 				  ~GAHBCFG_CTRL_MASK;
808197ba5f4SPaul Zimmerman 		}
809197ba5f4SPaul Zimmerman 		break;
810197ba5f4SPaul Zimmerman 
811197ba5f4SPaul Zimmerman 	case GHWCFG2_SLAVE_ONLY_ARCH:
812197ba5f4SPaul Zimmerman 	default:
813197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Slave Only Mode\n");
814197ba5f4SPaul Zimmerman 		break;
815197ba5f4SPaul Zimmerman 	}
816197ba5f4SPaul Zimmerman 
817197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
818197ba5f4SPaul Zimmerman 		hsotg->core_params->dma_enable,
819197ba5f4SPaul Zimmerman 		hsotg->core_params->dma_desc_enable);
820197ba5f4SPaul Zimmerman 
821197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_enable > 0) {
822197ba5f4SPaul Zimmerman 		if (hsotg->core_params->dma_desc_enable > 0)
823197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
824197ba5f4SPaul Zimmerman 		else
825197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
826197ba5f4SPaul Zimmerman 	} else {
827197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Using Slave mode\n");
828197ba5f4SPaul Zimmerman 		hsotg->core_params->dma_desc_enable = 0;
829197ba5f4SPaul Zimmerman 	}
830197ba5f4SPaul Zimmerman 
831197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_enable > 0)
832197ba5f4SPaul Zimmerman 		ahbcfg |= GAHBCFG_DMA_EN;
833197ba5f4SPaul Zimmerman 
83495c8bc36SAntti Seppälä 	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
835197ba5f4SPaul Zimmerman 
836197ba5f4SPaul Zimmerman 	return 0;
837197ba5f4SPaul Zimmerman }
838197ba5f4SPaul Zimmerman 
839197ba5f4SPaul Zimmerman static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
840197ba5f4SPaul Zimmerman {
841197ba5f4SPaul Zimmerman 	u32 usbcfg;
842197ba5f4SPaul Zimmerman 
84395c8bc36SAntti Seppälä 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
844197ba5f4SPaul Zimmerman 	usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
845197ba5f4SPaul Zimmerman 
846197ba5f4SPaul Zimmerman 	switch (hsotg->hw_params.op_mode) {
847197ba5f4SPaul Zimmerman 	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
848197ba5f4SPaul Zimmerman 		if (hsotg->core_params->otg_cap ==
849197ba5f4SPaul Zimmerman 				DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
850197ba5f4SPaul Zimmerman 			usbcfg |= GUSBCFG_HNPCAP;
851197ba5f4SPaul Zimmerman 		if (hsotg->core_params->otg_cap !=
852197ba5f4SPaul Zimmerman 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
853197ba5f4SPaul Zimmerman 			usbcfg |= GUSBCFG_SRPCAP;
854197ba5f4SPaul Zimmerman 		break;
855197ba5f4SPaul Zimmerman 
856197ba5f4SPaul Zimmerman 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
857197ba5f4SPaul Zimmerman 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
858197ba5f4SPaul Zimmerman 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
859197ba5f4SPaul Zimmerman 		if (hsotg->core_params->otg_cap !=
860197ba5f4SPaul Zimmerman 				DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
861197ba5f4SPaul Zimmerman 			usbcfg |= GUSBCFG_SRPCAP;
862197ba5f4SPaul Zimmerman 		break;
863197ba5f4SPaul Zimmerman 
864197ba5f4SPaul Zimmerman 	case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
865197ba5f4SPaul Zimmerman 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
866197ba5f4SPaul Zimmerman 	case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
867197ba5f4SPaul Zimmerman 	default:
868197ba5f4SPaul Zimmerman 		break;
869197ba5f4SPaul Zimmerman 	}
870197ba5f4SPaul Zimmerman 
87195c8bc36SAntti Seppälä 	dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
872197ba5f4SPaul Zimmerman }
873197ba5f4SPaul Zimmerman 
874197ba5f4SPaul Zimmerman /**
875197ba5f4SPaul Zimmerman  * dwc2_core_init() - Initializes the DWC_otg controller registers and
876197ba5f4SPaul Zimmerman  * prepares the core for device mode or host mode operation
877197ba5f4SPaul Zimmerman  *
878197ba5f4SPaul Zimmerman  * @hsotg:         Programming view of the DWC_otg controller
8790fe239bcSDouglas Anderson  * @initial_setup: If true then this is the first init for this instance.
880197ba5f4SPaul Zimmerman  */
8810fe239bcSDouglas Anderson int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
882197ba5f4SPaul Zimmerman {
883197ba5f4SPaul Zimmerman 	u32 usbcfg, otgctl;
884197ba5f4SPaul Zimmerman 	int retval;
885197ba5f4SPaul Zimmerman 
886197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
887197ba5f4SPaul Zimmerman 
88895c8bc36SAntti Seppälä 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
889197ba5f4SPaul Zimmerman 
890197ba5f4SPaul Zimmerman 	/* Set ULPI External VBUS bit if needed */
891197ba5f4SPaul Zimmerman 	usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
892197ba5f4SPaul Zimmerman 	if (hsotg->core_params->phy_ulpi_ext_vbus ==
893197ba5f4SPaul Zimmerman 				DWC2_PHY_ULPI_EXTERNAL_VBUS)
894197ba5f4SPaul Zimmerman 		usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
895197ba5f4SPaul Zimmerman 
896197ba5f4SPaul Zimmerman 	/* Set external TS Dline pulsing bit if needed */
897197ba5f4SPaul Zimmerman 	usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
898197ba5f4SPaul Zimmerman 	if (hsotg->core_params->ts_dline > 0)
899197ba5f4SPaul Zimmerman 		usbcfg |= GUSBCFG_TERMSELDLPULSE;
900197ba5f4SPaul Zimmerman 
90195c8bc36SAntti Seppälä 	dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
902197ba5f4SPaul Zimmerman 
9030fe239bcSDouglas Anderson 	/*
9040fe239bcSDouglas Anderson 	 * Reset the Controller
9050fe239bcSDouglas Anderson 	 *
9060fe239bcSDouglas Anderson 	 * We only need to reset the controller if this is a re-init.
9070fe239bcSDouglas Anderson 	 * For the first init we know for sure that earlier code reset us (it
9080fe239bcSDouglas Anderson 	 * needed to in order to properly detect various parameters).
9090fe239bcSDouglas Anderson 	 */
9100fe239bcSDouglas Anderson 	if (!initial_setup) {
9116d58f346SJohn Youn 		retval = dwc2_core_reset_and_force_dr_mode(hsotg);
912197ba5f4SPaul Zimmerman 		if (retval) {
913197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
914197ba5f4SPaul Zimmerman 					__func__);
915197ba5f4SPaul Zimmerman 			return retval;
916197ba5f4SPaul Zimmerman 		}
9170fe239bcSDouglas Anderson 	}
918197ba5f4SPaul Zimmerman 
919197ba5f4SPaul Zimmerman 	/*
920197ba5f4SPaul Zimmerman 	 * This needs to happen in FS mode before any other programming occurs
921197ba5f4SPaul Zimmerman 	 */
9220fe239bcSDouglas Anderson 	retval = dwc2_phy_init(hsotg, initial_setup);
923197ba5f4SPaul Zimmerman 	if (retval)
924197ba5f4SPaul Zimmerman 		return retval;
925197ba5f4SPaul Zimmerman 
926197ba5f4SPaul Zimmerman 	/* Program the GAHBCFG Register */
927197ba5f4SPaul Zimmerman 	retval = dwc2_gahbcfg_init(hsotg);
928197ba5f4SPaul Zimmerman 	if (retval)
929197ba5f4SPaul Zimmerman 		return retval;
930197ba5f4SPaul Zimmerman 
931197ba5f4SPaul Zimmerman 	/* Program the GUSBCFG register */
932197ba5f4SPaul Zimmerman 	dwc2_gusbcfg_init(hsotg);
933197ba5f4SPaul Zimmerman 
934197ba5f4SPaul Zimmerman 	/* Program the GOTGCTL register */
93595c8bc36SAntti Seppälä 	otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
936197ba5f4SPaul Zimmerman 	otgctl &= ~GOTGCTL_OTGVER;
937197ba5f4SPaul Zimmerman 	if (hsotg->core_params->otg_ver > 0)
938197ba5f4SPaul Zimmerman 		otgctl |= GOTGCTL_OTGVER;
93995c8bc36SAntti Seppälä 	dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
940197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
941197ba5f4SPaul Zimmerman 
942197ba5f4SPaul Zimmerman 	/* Clear the SRP success bit for FS-I2c */
943197ba5f4SPaul Zimmerman 	hsotg->srp_success = 0;
944197ba5f4SPaul Zimmerman 
945197ba5f4SPaul Zimmerman 	/* Enable common interrupts */
946197ba5f4SPaul Zimmerman 	dwc2_enable_common_interrupts(hsotg);
947197ba5f4SPaul Zimmerman 
948197ba5f4SPaul Zimmerman 	/*
949997f4f81SMickael Maison 	 * Do device or host initialization based on mode during PCD and
950197ba5f4SPaul Zimmerman 	 * HCD initialization
951197ba5f4SPaul Zimmerman 	 */
952197ba5f4SPaul Zimmerman 	if (dwc2_is_host_mode(hsotg)) {
953197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Host Mode\n");
954197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_A_HOST;
955197ba5f4SPaul Zimmerman 	} else {
956197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Device Mode\n");
957197ba5f4SPaul Zimmerman 		hsotg->op_state = OTG_STATE_B_PERIPHERAL;
958197ba5f4SPaul Zimmerman 	}
959197ba5f4SPaul Zimmerman 
960197ba5f4SPaul Zimmerman 	return 0;
961197ba5f4SPaul Zimmerman }
962197ba5f4SPaul Zimmerman 
963197ba5f4SPaul Zimmerman /**
964197ba5f4SPaul Zimmerman  * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
965197ba5f4SPaul Zimmerman  *
966197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
967197ba5f4SPaul Zimmerman  */
968197ba5f4SPaul Zimmerman void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
969197ba5f4SPaul Zimmerman {
970197ba5f4SPaul Zimmerman 	u32 intmsk;
971197ba5f4SPaul Zimmerman 
972197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
973197ba5f4SPaul Zimmerman 
974197ba5f4SPaul Zimmerman 	/* Disable all interrupts */
97595c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + GINTMSK);
97695c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + HAINTMSK);
977197ba5f4SPaul Zimmerman 
978197ba5f4SPaul Zimmerman 	/* Enable the common interrupts */
979197ba5f4SPaul Zimmerman 	dwc2_enable_common_interrupts(hsotg);
980197ba5f4SPaul Zimmerman 
981197ba5f4SPaul Zimmerman 	/* Enable host mode interrupts without disturbing common interrupts */
98295c8bc36SAntti Seppälä 	intmsk = dwc2_readl(hsotg->regs + GINTMSK);
98344e4a60dSMian Yousaf Kaukab 	intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
98495c8bc36SAntti Seppälä 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
985197ba5f4SPaul Zimmerman }
986197ba5f4SPaul Zimmerman 
987197ba5f4SPaul Zimmerman /**
988197ba5f4SPaul Zimmerman  * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
989197ba5f4SPaul Zimmerman  *
990197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
991197ba5f4SPaul Zimmerman  */
992197ba5f4SPaul Zimmerman void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
993197ba5f4SPaul Zimmerman {
99495c8bc36SAntti Seppälä 	u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
995197ba5f4SPaul Zimmerman 
996197ba5f4SPaul Zimmerman 	/* Disable host mode interrupts without disturbing common interrupts */
997197ba5f4SPaul Zimmerman 	intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
99877dbf713SMian Yousaf Kaukab 		    GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
99995c8bc36SAntti Seppälä 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
1000197ba5f4SPaul Zimmerman }
1001197ba5f4SPaul Zimmerman 
1002112fe8e2SDinh Nguyen /*
1003112fe8e2SDinh Nguyen  * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
1004112fe8e2SDinh Nguyen  * For system that have a total fifo depth that is smaller than the default
1005112fe8e2SDinh Nguyen  * RX + TX fifo size.
1006112fe8e2SDinh Nguyen  *
1007112fe8e2SDinh Nguyen  * @hsotg: Programming view of DWC_otg controller
1008112fe8e2SDinh Nguyen  */
1009112fe8e2SDinh Nguyen static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
1010112fe8e2SDinh Nguyen {
1011112fe8e2SDinh Nguyen 	struct dwc2_core_params *params = hsotg->core_params;
1012112fe8e2SDinh Nguyen 	struct dwc2_hw_params *hw = &hsotg->hw_params;
1013112fe8e2SDinh Nguyen 	u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
1014112fe8e2SDinh Nguyen 
1015112fe8e2SDinh Nguyen 	total_fifo_size = hw->total_fifo_size;
1016112fe8e2SDinh Nguyen 	rxfsiz = params->host_rx_fifo_size;
1017112fe8e2SDinh Nguyen 	nptxfsiz = params->host_nperio_tx_fifo_size;
1018112fe8e2SDinh Nguyen 	ptxfsiz = params->host_perio_tx_fifo_size;
1019112fe8e2SDinh Nguyen 
1020112fe8e2SDinh Nguyen 	/*
1021112fe8e2SDinh Nguyen 	 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
1022112fe8e2SDinh Nguyen 	 * allocation with support for high bandwidth endpoints. Synopsys
1023112fe8e2SDinh Nguyen 	 * defines MPS(Max Packet size) for a periodic EP=1024, and for
1024112fe8e2SDinh Nguyen 	 * non-periodic as 512.
1025112fe8e2SDinh Nguyen 	 */
1026112fe8e2SDinh Nguyen 	if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
1027112fe8e2SDinh Nguyen 		/*
1028112fe8e2SDinh Nguyen 		 * For Buffer DMA mode/Scatter Gather DMA mode
1029112fe8e2SDinh Nguyen 		 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
1030112fe8e2SDinh Nguyen 		 * with n = number of host channel.
1031112fe8e2SDinh Nguyen 		 * 2 * ((1024/4) + 2) = 516
1032112fe8e2SDinh Nguyen 		 */
1033112fe8e2SDinh Nguyen 		rxfsiz = 516 + hw->host_channels;
1034112fe8e2SDinh Nguyen 
1035112fe8e2SDinh Nguyen 		/*
1036112fe8e2SDinh Nguyen 		 * min non-periodic tx fifo depth
1037112fe8e2SDinh Nguyen 		 * 2 * (largest non-periodic USB packet used / 4)
1038112fe8e2SDinh Nguyen 		 * 2 * (512/4) = 256
1039112fe8e2SDinh Nguyen 		 */
1040112fe8e2SDinh Nguyen 		nptxfsiz = 256;
1041112fe8e2SDinh Nguyen 
1042112fe8e2SDinh Nguyen 		/*
1043112fe8e2SDinh Nguyen 		 * min periodic tx fifo depth
1044112fe8e2SDinh Nguyen 		 * (largest packet size*MC)/4
1045112fe8e2SDinh Nguyen 		 * (1024 * 3)/4 = 768
1046112fe8e2SDinh Nguyen 		 */
1047112fe8e2SDinh Nguyen 		ptxfsiz = 768;
1048112fe8e2SDinh Nguyen 
1049112fe8e2SDinh Nguyen 		params->host_rx_fifo_size = rxfsiz;
1050112fe8e2SDinh Nguyen 		params->host_nperio_tx_fifo_size = nptxfsiz;
1051112fe8e2SDinh Nguyen 		params->host_perio_tx_fifo_size = ptxfsiz;
1052112fe8e2SDinh Nguyen 	}
1053112fe8e2SDinh Nguyen 
1054112fe8e2SDinh Nguyen 	/*
1055112fe8e2SDinh Nguyen 	 * If the summation of RX, NPTX and PTX fifo sizes is still
1056112fe8e2SDinh Nguyen 	 * bigger than the total_fifo_size, then we have a problem.
1057112fe8e2SDinh Nguyen 	 *
1058112fe8e2SDinh Nguyen 	 * We won't be able to allocate as many endpoints. Right now,
1059112fe8e2SDinh Nguyen 	 * we're just printing an error message, but ideally this FIFO
1060112fe8e2SDinh Nguyen 	 * allocation algorithm would be improved in the future.
1061112fe8e2SDinh Nguyen 	 *
1062112fe8e2SDinh Nguyen 	 * FIXME improve this FIFO allocation algorithm.
1063112fe8e2SDinh Nguyen 	 */
1064112fe8e2SDinh Nguyen 	if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
1065112fe8e2SDinh Nguyen 		dev_err(hsotg->dev, "invalid fifo sizes\n");
1066112fe8e2SDinh Nguyen }
1067112fe8e2SDinh Nguyen 
1068197ba5f4SPaul Zimmerman static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
1069197ba5f4SPaul Zimmerman {
1070197ba5f4SPaul Zimmerman 	struct dwc2_core_params *params = hsotg->core_params;
1071197ba5f4SPaul Zimmerman 	u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
1072197ba5f4SPaul Zimmerman 
1073197ba5f4SPaul Zimmerman 	if (!params->enable_dynamic_fifo)
1074197ba5f4SPaul Zimmerman 		return;
1075197ba5f4SPaul Zimmerman 
1076112fe8e2SDinh Nguyen 	dwc2_calculate_dynamic_fifo(hsotg);
1077112fe8e2SDinh Nguyen 
1078197ba5f4SPaul Zimmerman 	/* Rx FIFO */
107995c8bc36SAntti Seppälä 	grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
1080197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
1081197ba5f4SPaul Zimmerman 	grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
1082197ba5f4SPaul Zimmerman 	grxfsiz |= params->host_rx_fifo_size <<
1083197ba5f4SPaul Zimmerman 		   GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
108495c8bc36SAntti Seppälä 	dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
108595c8bc36SAntti Seppälä 	dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
108695c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + GRXFSIZ));
1087197ba5f4SPaul Zimmerman 
1088197ba5f4SPaul Zimmerman 	/* Non-periodic Tx FIFO */
1089197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
109095c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + GNPTXFSIZ));
1091197ba5f4SPaul Zimmerman 	nptxfsiz = params->host_nperio_tx_fifo_size <<
1092197ba5f4SPaul Zimmerman 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
1093197ba5f4SPaul Zimmerman 	nptxfsiz |= params->host_rx_fifo_size <<
1094197ba5f4SPaul Zimmerman 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
109595c8bc36SAntti Seppälä 	dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
1096197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
109795c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + GNPTXFSIZ));
1098197ba5f4SPaul Zimmerman 
1099197ba5f4SPaul Zimmerman 	/* Periodic Tx FIFO */
1100197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
110195c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + HPTXFSIZ));
1102197ba5f4SPaul Zimmerman 	hptxfsiz = params->host_perio_tx_fifo_size <<
1103197ba5f4SPaul Zimmerman 		   FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
1104197ba5f4SPaul Zimmerman 	hptxfsiz |= (params->host_rx_fifo_size +
1105197ba5f4SPaul Zimmerman 		     params->host_nperio_tx_fifo_size) <<
1106197ba5f4SPaul Zimmerman 		    FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
110795c8bc36SAntti Seppälä 	dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
1108197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
110995c8bc36SAntti Seppälä 		dwc2_readl(hsotg->regs + HPTXFSIZ));
1110197ba5f4SPaul Zimmerman 
1111197ba5f4SPaul Zimmerman 	if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
1112197ba5f4SPaul Zimmerman 	    hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
1113197ba5f4SPaul Zimmerman 		/*
1114197ba5f4SPaul Zimmerman 		 * Global DFIFOCFG calculation for Host mode -
1115197ba5f4SPaul Zimmerman 		 * include RxFIFO, NPTXFIFO and HPTXFIFO
1116197ba5f4SPaul Zimmerman 		 */
111795c8bc36SAntti Seppälä 		dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
1118197ba5f4SPaul Zimmerman 		dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
1119197ba5f4SPaul Zimmerman 		dfifocfg |= (params->host_rx_fifo_size +
1120197ba5f4SPaul Zimmerman 			     params->host_nperio_tx_fifo_size +
1121197ba5f4SPaul Zimmerman 			     params->host_perio_tx_fifo_size) <<
1122197ba5f4SPaul Zimmerman 			    GDFIFOCFG_EPINFOBASE_SHIFT &
1123197ba5f4SPaul Zimmerman 			    GDFIFOCFG_EPINFOBASE_MASK;
112495c8bc36SAntti Seppälä 		dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
1125197ba5f4SPaul Zimmerman 	}
1126197ba5f4SPaul Zimmerman }
1127197ba5f4SPaul Zimmerman 
1128197ba5f4SPaul Zimmerman /**
1129197ba5f4SPaul Zimmerman  * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
1130197ba5f4SPaul Zimmerman  * Host mode
1131197ba5f4SPaul Zimmerman  *
1132197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
1133197ba5f4SPaul Zimmerman  *
1134197ba5f4SPaul Zimmerman  * This function flushes the Tx and Rx FIFOs and flushes any entries in the
1135197ba5f4SPaul Zimmerman  * request queues. Host channels are reset to ensure that they are ready for
1136197ba5f4SPaul Zimmerman  * performing transfers.
1137197ba5f4SPaul Zimmerman  */
1138197ba5f4SPaul Zimmerman void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
1139197ba5f4SPaul Zimmerman {
1140197ba5f4SPaul Zimmerman 	u32 hcfg, hfir, otgctl;
1141197ba5f4SPaul Zimmerman 
1142197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
1143197ba5f4SPaul Zimmerman 
1144197ba5f4SPaul Zimmerman 	/* Restart the Phy Clock */
114595c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + PCGCTL);
1146197ba5f4SPaul Zimmerman 
1147197ba5f4SPaul Zimmerman 	/* Initialize Host Configuration Register */
1148197ba5f4SPaul Zimmerman 	dwc2_init_fs_ls_pclk_sel(hsotg);
1149197ba5f4SPaul Zimmerman 	if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
115095c8bc36SAntti Seppälä 		hcfg = dwc2_readl(hsotg->regs + HCFG);
1151197ba5f4SPaul Zimmerman 		hcfg |= HCFG_FSLSSUPP;
115295c8bc36SAntti Seppälä 		dwc2_writel(hcfg, hsotg->regs + HCFG);
1153197ba5f4SPaul Zimmerman 	}
1154197ba5f4SPaul Zimmerman 
1155197ba5f4SPaul Zimmerman 	/*
1156197ba5f4SPaul Zimmerman 	 * This bit allows dynamic reloading of the HFIR register during
1157197ba5f4SPaul Zimmerman 	 * runtime. This bit needs to be programmed during initial configuration
1158197ba5f4SPaul Zimmerman 	 * and its value must not be changed during runtime.
1159197ba5f4SPaul Zimmerman 	 */
1160197ba5f4SPaul Zimmerman 	if (hsotg->core_params->reload_ctl > 0) {
116195c8bc36SAntti Seppälä 		hfir = dwc2_readl(hsotg->regs + HFIR);
1162197ba5f4SPaul Zimmerman 		hfir |= HFIR_RLDCTRL;
116395c8bc36SAntti Seppälä 		dwc2_writel(hfir, hsotg->regs + HFIR);
1164197ba5f4SPaul Zimmerman 	}
1165197ba5f4SPaul Zimmerman 
1166197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_desc_enable > 0) {
1167197ba5f4SPaul Zimmerman 		u32 op_mode = hsotg->hw_params.op_mode;
1168197ba5f4SPaul Zimmerman 		if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
1169197ba5f4SPaul Zimmerman 		    !hsotg->hw_params.dma_desc_enable ||
1170197ba5f4SPaul Zimmerman 		    op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
1171197ba5f4SPaul Zimmerman 		    op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
1172197ba5f4SPaul Zimmerman 		    op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
1173197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
1174197ba5f4SPaul Zimmerman 				"Hardware does not support descriptor DMA mode -\n");
1175197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
1176197ba5f4SPaul Zimmerman 				"falling back to buffer DMA mode.\n");
1177197ba5f4SPaul Zimmerman 			hsotg->core_params->dma_desc_enable = 0;
1178197ba5f4SPaul Zimmerman 		} else {
117995c8bc36SAntti Seppälä 			hcfg = dwc2_readl(hsotg->regs + HCFG);
1180197ba5f4SPaul Zimmerman 			hcfg |= HCFG_DESCDMA;
118195c8bc36SAntti Seppälä 			dwc2_writel(hcfg, hsotg->regs + HCFG);
1182197ba5f4SPaul Zimmerman 		}
1183197ba5f4SPaul Zimmerman 	}
1184197ba5f4SPaul Zimmerman 
1185197ba5f4SPaul Zimmerman 	/* Configure data FIFO sizes */
1186197ba5f4SPaul Zimmerman 	dwc2_config_fifos(hsotg);
1187197ba5f4SPaul Zimmerman 
1188197ba5f4SPaul Zimmerman 	/* TODO - check this */
1189197ba5f4SPaul Zimmerman 	/* Clear Host Set HNP Enable in the OTG Control Register */
119095c8bc36SAntti Seppälä 	otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
1191197ba5f4SPaul Zimmerman 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
119295c8bc36SAntti Seppälä 	dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
1193197ba5f4SPaul Zimmerman 
1194197ba5f4SPaul Zimmerman 	/* Make sure the FIFOs are flushed */
1195197ba5f4SPaul Zimmerman 	dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
1196197ba5f4SPaul Zimmerman 	dwc2_flush_rx_fifo(hsotg);
1197197ba5f4SPaul Zimmerman 
1198197ba5f4SPaul Zimmerman 	/* Clear Host Set HNP Enable in the OTG Control Register */
119995c8bc36SAntti Seppälä 	otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
1200197ba5f4SPaul Zimmerman 	otgctl &= ~GOTGCTL_HSTSETHNPEN;
120195c8bc36SAntti Seppälä 	dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
1202197ba5f4SPaul Zimmerman 
1203197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_desc_enable <= 0) {
1204197ba5f4SPaul Zimmerman 		int num_channels, i;
1205197ba5f4SPaul Zimmerman 		u32 hcchar;
1206197ba5f4SPaul Zimmerman 
1207197ba5f4SPaul Zimmerman 		/* Flush out any leftover queued requests */
1208197ba5f4SPaul Zimmerman 		num_channels = hsotg->core_params->host_channels;
1209197ba5f4SPaul Zimmerman 		for (i = 0; i < num_channels; i++) {
121095c8bc36SAntti Seppälä 			hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1211197ba5f4SPaul Zimmerman 			hcchar &= ~HCCHAR_CHENA;
1212197ba5f4SPaul Zimmerman 			hcchar |= HCCHAR_CHDIS;
1213197ba5f4SPaul Zimmerman 			hcchar &= ~HCCHAR_EPDIR;
121495c8bc36SAntti Seppälä 			dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1215197ba5f4SPaul Zimmerman 		}
1216197ba5f4SPaul Zimmerman 
1217197ba5f4SPaul Zimmerman 		/* Halt all channels to put them into a known state */
1218197ba5f4SPaul Zimmerman 		for (i = 0; i < num_channels; i++) {
1219197ba5f4SPaul Zimmerman 			int count = 0;
1220197ba5f4SPaul Zimmerman 
122195c8bc36SAntti Seppälä 			hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1222197ba5f4SPaul Zimmerman 			hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
1223197ba5f4SPaul Zimmerman 			hcchar &= ~HCCHAR_EPDIR;
122495c8bc36SAntti Seppälä 			dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1225197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
1226197ba5f4SPaul Zimmerman 				__func__, i);
1227197ba5f4SPaul Zimmerman 			do {
122895c8bc36SAntti Seppälä 				hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1229197ba5f4SPaul Zimmerman 				if (++count > 1000) {
1230197ba5f4SPaul Zimmerman 					dev_err(hsotg->dev,
1231197ba5f4SPaul Zimmerman 						"Unable to clear enable on channel %d\n",
1232197ba5f4SPaul Zimmerman 						i);
1233197ba5f4SPaul Zimmerman 					break;
1234197ba5f4SPaul Zimmerman 				}
1235197ba5f4SPaul Zimmerman 				udelay(1);
1236197ba5f4SPaul Zimmerman 			} while (hcchar & HCCHAR_CHENA);
1237197ba5f4SPaul Zimmerman 		}
1238197ba5f4SPaul Zimmerman 	}
1239197ba5f4SPaul Zimmerman 
1240197ba5f4SPaul Zimmerman 	/* Turn on the vbus power */
1241197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
1242197ba5f4SPaul Zimmerman 	if (hsotg->op_state == OTG_STATE_A_HOST) {
1243197ba5f4SPaul Zimmerman 		u32 hprt0 = dwc2_read_hprt0(hsotg);
1244197ba5f4SPaul Zimmerman 
1245197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
1246197ba5f4SPaul Zimmerman 			!!(hprt0 & HPRT0_PWR));
1247197ba5f4SPaul Zimmerman 		if (!(hprt0 & HPRT0_PWR)) {
1248197ba5f4SPaul Zimmerman 			hprt0 |= HPRT0_PWR;
124995c8bc36SAntti Seppälä 			dwc2_writel(hprt0, hsotg->regs + HPRT0);
1250197ba5f4SPaul Zimmerman 		}
1251197ba5f4SPaul Zimmerman 	}
1252197ba5f4SPaul Zimmerman 
1253197ba5f4SPaul Zimmerman 	dwc2_enable_host_interrupts(hsotg);
1254197ba5f4SPaul Zimmerman }
1255197ba5f4SPaul Zimmerman 
1256197ba5f4SPaul Zimmerman static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
1257197ba5f4SPaul Zimmerman 				      struct dwc2_host_chan *chan)
1258197ba5f4SPaul Zimmerman {
1259197ba5f4SPaul Zimmerman 	u32 hcintmsk = HCINTMSK_CHHLTD;
1260197ba5f4SPaul Zimmerman 
1261197ba5f4SPaul Zimmerman 	switch (chan->ep_type) {
1262197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
1263197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
1264197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "control/bulk\n");
1265197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_XFERCOMPL;
1266197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_STALL;
1267197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_XACTERR;
1268197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_DATATGLERR;
1269197ba5f4SPaul Zimmerman 		if (chan->ep_is_in) {
1270197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_BBLERR;
1271197ba5f4SPaul Zimmerman 		} else {
1272197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_NAK;
1273197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_NYET;
1274197ba5f4SPaul Zimmerman 			if (chan->do_ping)
1275197ba5f4SPaul Zimmerman 				hcintmsk |= HCINTMSK_ACK;
1276197ba5f4SPaul Zimmerman 		}
1277197ba5f4SPaul Zimmerman 
1278197ba5f4SPaul Zimmerman 		if (chan->do_split) {
1279197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_NAK;
1280197ba5f4SPaul Zimmerman 			if (chan->complete_split)
1281197ba5f4SPaul Zimmerman 				hcintmsk |= HCINTMSK_NYET;
1282197ba5f4SPaul Zimmerman 			else
1283197ba5f4SPaul Zimmerman 				hcintmsk |= HCINTMSK_ACK;
1284197ba5f4SPaul Zimmerman 		}
1285197ba5f4SPaul Zimmerman 
1286197ba5f4SPaul Zimmerman 		if (chan->error_state)
1287197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_ACK;
1288197ba5f4SPaul Zimmerman 		break;
1289197ba5f4SPaul Zimmerman 
1290197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
1291197ba5f4SPaul Zimmerman 		if (dbg_perio())
1292197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "intr\n");
1293197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_XFERCOMPL;
1294197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_NAK;
1295197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_STALL;
1296197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_XACTERR;
1297197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_DATATGLERR;
1298197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_FRMOVRUN;
1299197ba5f4SPaul Zimmerman 
1300197ba5f4SPaul Zimmerman 		if (chan->ep_is_in)
1301197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_BBLERR;
1302197ba5f4SPaul Zimmerman 		if (chan->error_state)
1303197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_ACK;
1304197ba5f4SPaul Zimmerman 		if (chan->do_split) {
1305197ba5f4SPaul Zimmerman 			if (chan->complete_split)
1306197ba5f4SPaul Zimmerman 				hcintmsk |= HCINTMSK_NYET;
1307197ba5f4SPaul Zimmerman 			else
1308197ba5f4SPaul Zimmerman 				hcintmsk |= HCINTMSK_ACK;
1309197ba5f4SPaul Zimmerman 		}
1310197ba5f4SPaul Zimmerman 		break;
1311197ba5f4SPaul Zimmerman 
1312197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
1313197ba5f4SPaul Zimmerman 		if (dbg_perio())
1314197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "isoc\n");
1315197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_XFERCOMPL;
1316197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_FRMOVRUN;
1317197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_ACK;
1318197ba5f4SPaul Zimmerman 
1319197ba5f4SPaul Zimmerman 		if (chan->ep_is_in) {
1320197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_XACTERR;
1321197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_BBLERR;
1322197ba5f4SPaul Zimmerman 		}
1323197ba5f4SPaul Zimmerman 		break;
1324197ba5f4SPaul Zimmerman 	default:
1325197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "## Unknown EP type ##\n");
1326197ba5f4SPaul Zimmerman 		break;
1327197ba5f4SPaul Zimmerman 	}
1328197ba5f4SPaul Zimmerman 
132995c8bc36SAntti Seppälä 	dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1330197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1331197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
1332197ba5f4SPaul Zimmerman }
1333197ba5f4SPaul Zimmerman 
1334197ba5f4SPaul Zimmerman static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
1335197ba5f4SPaul Zimmerman 				    struct dwc2_host_chan *chan)
1336197ba5f4SPaul Zimmerman {
1337197ba5f4SPaul Zimmerman 	u32 hcintmsk = HCINTMSK_CHHLTD;
1338197ba5f4SPaul Zimmerman 
1339197ba5f4SPaul Zimmerman 	/*
1340197ba5f4SPaul Zimmerman 	 * For Descriptor DMA mode core halts the channel on AHB error.
1341197ba5f4SPaul Zimmerman 	 * Interrupt is not required.
1342197ba5f4SPaul Zimmerman 	 */
1343197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_desc_enable <= 0) {
1344197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1345197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1346197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_AHBERR;
1347197ba5f4SPaul Zimmerman 	} else {
1348197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1349197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "desc DMA enabled\n");
1350197ba5f4SPaul Zimmerman 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1351197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_XFERCOMPL;
1352197ba5f4SPaul Zimmerman 	}
1353197ba5f4SPaul Zimmerman 
1354197ba5f4SPaul Zimmerman 	if (chan->error_state && !chan->do_split &&
1355197ba5f4SPaul Zimmerman 	    chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
1356197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1357197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "setting ACK\n");
1358197ba5f4SPaul Zimmerman 		hcintmsk |= HCINTMSK_ACK;
1359197ba5f4SPaul Zimmerman 		if (chan->ep_is_in) {
1360197ba5f4SPaul Zimmerman 			hcintmsk |= HCINTMSK_DATATGLERR;
1361197ba5f4SPaul Zimmerman 			if (chan->ep_type != USB_ENDPOINT_XFER_INT)
1362197ba5f4SPaul Zimmerman 				hcintmsk |= HCINTMSK_NAK;
1363197ba5f4SPaul Zimmerman 		}
1364197ba5f4SPaul Zimmerman 	}
1365197ba5f4SPaul Zimmerman 
136695c8bc36SAntti Seppälä 	dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1367197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1368197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
1369197ba5f4SPaul Zimmerman }
1370197ba5f4SPaul Zimmerman 
1371197ba5f4SPaul Zimmerman static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
1372197ba5f4SPaul Zimmerman 				struct dwc2_host_chan *chan)
1373197ba5f4SPaul Zimmerman {
1374197ba5f4SPaul Zimmerman 	u32 intmsk;
1375197ba5f4SPaul Zimmerman 
1376197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_enable > 0) {
1377197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1378197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "DMA enabled\n");
1379197ba5f4SPaul Zimmerman 		dwc2_hc_enable_dma_ints(hsotg, chan);
1380197ba5f4SPaul Zimmerman 	} else {
1381197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1382197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "DMA disabled\n");
1383197ba5f4SPaul Zimmerman 		dwc2_hc_enable_slave_ints(hsotg, chan);
1384197ba5f4SPaul Zimmerman 	}
1385197ba5f4SPaul Zimmerman 
1386197ba5f4SPaul Zimmerman 	/* Enable the top level host channel interrupt */
138795c8bc36SAntti Seppälä 	intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
1388197ba5f4SPaul Zimmerman 	intmsk |= 1 << chan->hc_num;
138995c8bc36SAntti Seppälä 	dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
1390197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1391197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
1392197ba5f4SPaul Zimmerman 
1393197ba5f4SPaul Zimmerman 	/* Make sure host channel interrupts are enabled */
139495c8bc36SAntti Seppälä 	intmsk = dwc2_readl(hsotg->regs + GINTMSK);
1395197ba5f4SPaul Zimmerman 	intmsk |= GINTSTS_HCHINT;
139695c8bc36SAntti Seppälä 	dwc2_writel(intmsk, hsotg->regs + GINTMSK);
1397197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1398197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
1399197ba5f4SPaul Zimmerman }
1400197ba5f4SPaul Zimmerman 
1401197ba5f4SPaul Zimmerman /**
1402197ba5f4SPaul Zimmerman  * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
1403197ba5f4SPaul Zimmerman  * a specific endpoint
1404197ba5f4SPaul Zimmerman  *
1405197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
1406197ba5f4SPaul Zimmerman  * @chan:  Information needed to initialize the host channel
1407197ba5f4SPaul Zimmerman  *
1408197ba5f4SPaul Zimmerman  * The HCCHARn register is set up with the characteristics specified in chan.
1409197ba5f4SPaul Zimmerman  * Host channel interrupts that may need to be serviced while this transfer is
1410197ba5f4SPaul Zimmerman  * in progress are enabled.
1411197ba5f4SPaul Zimmerman  */
1412197ba5f4SPaul Zimmerman void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1413197ba5f4SPaul Zimmerman {
1414197ba5f4SPaul Zimmerman 	u8 hc_num = chan->hc_num;
1415197ba5f4SPaul Zimmerman 	u32 hcintmsk;
1416197ba5f4SPaul Zimmerman 	u32 hcchar;
1417197ba5f4SPaul Zimmerman 	u32 hcsplt = 0;
1418197ba5f4SPaul Zimmerman 
1419197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1420197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1421197ba5f4SPaul Zimmerman 
1422197ba5f4SPaul Zimmerman 	/* Clear old interrupt conditions for this host channel */
1423197ba5f4SPaul Zimmerman 	hcintmsk = 0xffffffff;
1424197ba5f4SPaul Zimmerman 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
142595c8bc36SAntti Seppälä 	dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
1426197ba5f4SPaul Zimmerman 
1427197ba5f4SPaul Zimmerman 	/* Enable channel interrupts required for this transfer */
1428197ba5f4SPaul Zimmerman 	dwc2_hc_enable_ints(hsotg, chan);
1429197ba5f4SPaul Zimmerman 
1430197ba5f4SPaul Zimmerman 	/*
1431197ba5f4SPaul Zimmerman 	 * Program the HCCHARn register with the endpoint characteristics for
1432197ba5f4SPaul Zimmerman 	 * the current transfer
1433197ba5f4SPaul Zimmerman 	 */
1434197ba5f4SPaul Zimmerman 	hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
1435197ba5f4SPaul Zimmerman 	hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
1436197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
1437197ba5f4SPaul Zimmerman 		hcchar |= HCCHAR_EPDIR;
1438197ba5f4SPaul Zimmerman 	if (chan->speed == USB_SPEED_LOW)
1439197ba5f4SPaul Zimmerman 		hcchar |= HCCHAR_LSPDDEV;
1440197ba5f4SPaul Zimmerman 	hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
1441197ba5f4SPaul Zimmerman 	hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
144295c8bc36SAntti Seppälä 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
1443197ba5f4SPaul Zimmerman 	if (dbg_hc(chan)) {
1444197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
1445197ba5f4SPaul Zimmerman 			 hc_num, hcchar);
1446197ba5f4SPaul Zimmerman 
1447197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s: Channel %d\n",
1448197ba5f4SPaul Zimmerman 			 __func__, hc_num);
1449197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Dev Addr: %d\n",
1450197ba5f4SPaul Zimmerman 			 chan->dev_addr);
1451197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Ep Num: %d\n",
1452197ba5f4SPaul Zimmerman 			 chan->ep_num);
1453197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Is In: %d\n",
1454197ba5f4SPaul Zimmerman 			 chan->ep_is_in);
1455197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Is Low Speed: %d\n",
1456197ba5f4SPaul Zimmerman 			 chan->speed == USB_SPEED_LOW);
1457197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Ep Type: %d\n",
1458197ba5f4SPaul Zimmerman 			 chan->ep_type);
1459197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Max Pkt: %d\n",
1460197ba5f4SPaul Zimmerman 			 chan->max_packet);
1461197ba5f4SPaul Zimmerman 	}
1462197ba5f4SPaul Zimmerman 
1463197ba5f4SPaul Zimmerman 	/* Program the HCSPLT register for SPLITs */
1464197ba5f4SPaul Zimmerman 	if (chan->do_split) {
1465197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1466197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev,
1467197ba5f4SPaul Zimmerman 				 "Programming HC %d with split --> %s\n",
1468197ba5f4SPaul Zimmerman 				 hc_num,
1469197ba5f4SPaul Zimmerman 				 chan->complete_split ? "CSPLIT" : "SSPLIT");
1470197ba5f4SPaul Zimmerman 		if (chan->complete_split)
1471197ba5f4SPaul Zimmerman 			hcsplt |= HCSPLT_COMPSPLT;
1472197ba5f4SPaul Zimmerman 		hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
1473197ba5f4SPaul Zimmerman 			  HCSPLT_XACTPOS_MASK;
1474197ba5f4SPaul Zimmerman 		hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
1475197ba5f4SPaul Zimmerman 			  HCSPLT_HUBADDR_MASK;
1476197ba5f4SPaul Zimmerman 		hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
1477197ba5f4SPaul Zimmerman 			  HCSPLT_PRTADDR_MASK;
1478197ba5f4SPaul Zimmerman 		if (dbg_hc(chan)) {
1479197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	  comp split %d\n",
1480197ba5f4SPaul Zimmerman 				 chan->complete_split);
1481197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	  xact pos %d\n",
1482197ba5f4SPaul Zimmerman 				 chan->xact_pos);
1483197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	  hub addr %d\n",
1484197ba5f4SPaul Zimmerman 				 chan->hub_addr);
1485197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	  hub port %d\n",
1486197ba5f4SPaul Zimmerman 				 chan->hub_port);
1487197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	  is_in %d\n",
1488197ba5f4SPaul Zimmerman 				 chan->ep_is_in);
1489197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	  Max Pkt %d\n",
1490197ba5f4SPaul Zimmerman 				 chan->max_packet);
1491197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	  xferlen %d\n",
1492197ba5f4SPaul Zimmerman 				 chan->xfer_len);
1493197ba5f4SPaul Zimmerman 		}
1494197ba5f4SPaul Zimmerman 	}
1495197ba5f4SPaul Zimmerman 
149695c8bc36SAntti Seppälä 	dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
1497197ba5f4SPaul Zimmerman }
1498197ba5f4SPaul Zimmerman 
1499197ba5f4SPaul Zimmerman /**
1500197ba5f4SPaul Zimmerman  * dwc2_hc_halt() - Attempts to halt a host channel
1501197ba5f4SPaul Zimmerman  *
1502197ba5f4SPaul Zimmerman  * @hsotg:       Controller register interface
1503197ba5f4SPaul Zimmerman  * @chan:        Host channel to halt
1504197ba5f4SPaul Zimmerman  * @halt_status: Reason for halting the channel
1505197ba5f4SPaul Zimmerman  *
1506197ba5f4SPaul Zimmerman  * This function should only be called in Slave mode or to abort a transfer in
1507197ba5f4SPaul Zimmerman  * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
1508197ba5f4SPaul Zimmerman  * controller halts the channel when the transfer is complete or a condition
1509197ba5f4SPaul Zimmerman  * occurs that requires application intervention.
1510197ba5f4SPaul Zimmerman  *
1511197ba5f4SPaul Zimmerman  * In slave mode, checks for a free request queue entry, then sets the Channel
1512197ba5f4SPaul Zimmerman  * Enable and Channel Disable bits of the Host Channel Characteristics
1513197ba5f4SPaul Zimmerman  * register of the specified channel to intiate the halt. If there is no free
1514197ba5f4SPaul Zimmerman  * request queue entry, sets only the Channel Disable bit of the HCCHARn
1515197ba5f4SPaul Zimmerman  * register to flush requests for this channel. In the latter case, sets a
1516197ba5f4SPaul Zimmerman  * flag to indicate that the host channel needs to be halted when a request
1517197ba5f4SPaul Zimmerman  * queue slot is open.
1518197ba5f4SPaul Zimmerman  *
1519197ba5f4SPaul Zimmerman  * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
1520197ba5f4SPaul Zimmerman  * HCCHARn register. The controller ensures there is space in the request
1521197ba5f4SPaul Zimmerman  * queue before submitting the halt request.
1522197ba5f4SPaul Zimmerman  *
1523197ba5f4SPaul Zimmerman  * Some time may elapse before the core flushes any posted requests for this
1524197ba5f4SPaul Zimmerman  * host channel and halts. The Channel Halted interrupt handler completes the
1525197ba5f4SPaul Zimmerman  * deactivation of the host channel.
1526197ba5f4SPaul Zimmerman  */
1527197ba5f4SPaul Zimmerman void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
1528197ba5f4SPaul Zimmerman 		  enum dwc2_halt_status halt_status)
1529197ba5f4SPaul Zimmerman {
1530197ba5f4SPaul Zimmerman 	u32 nptxsts, hptxsts, hcchar;
1531197ba5f4SPaul Zimmerman 
1532197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1533197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1534197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1535197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1536197ba5f4SPaul Zimmerman 
1537197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1538197ba5f4SPaul Zimmerman 	    halt_status == DWC2_HC_XFER_AHB_ERR) {
1539197ba5f4SPaul Zimmerman 		/*
1540197ba5f4SPaul Zimmerman 		 * Disable all channel interrupts except Ch Halted. The QTD
1541197ba5f4SPaul Zimmerman 		 * and QH state associated with this transfer has been cleared
1542197ba5f4SPaul Zimmerman 		 * (in the case of URB_DEQUEUE), so the channel needs to be
1543197ba5f4SPaul Zimmerman 		 * shut down carefully to prevent crashes.
1544197ba5f4SPaul Zimmerman 		 */
1545197ba5f4SPaul Zimmerman 		u32 hcintmsk = HCINTMSK_CHHLTD;
1546197ba5f4SPaul Zimmerman 
1547197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "dequeue/error\n");
154895c8bc36SAntti Seppälä 		dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1549197ba5f4SPaul Zimmerman 
1550197ba5f4SPaul Zimmerman 		/*
1551197ba5f4SPaul Zimmerman 		 * Make sure no other interrupts besides halt are currently
1552197ba5f4SPaul Zimmerman 		 * pending. Handling another interrupt could cause a crash due
1553197ba5f4SPaul Zimmerman 		 * to the QTD and QH state.
1554197ba5f4SPaul Zimmerman 		 */
155595c8bc36SAntti Seppälä 		dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1556197ba5f4SPaul Zimmerman 
1557197ba5f4SPaul Zimmerman 		/*
1558197ba5f4SPaul Zimmerman 		 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1559197ba5f4SPaul Zimmerman 		 * even if the channel was already halted for some other
1560197ba5f4SPaul Zimmerman 		 * reason
1561197ba5f4SPaul Zimmerman 		 */
1562197ba5f4SPaul Zimmerman 		chan->halt_status = halt_status;
1563197ba5f4SPaul Zimmerman 
156495c8bc36SAntti Seppälä 		hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1565197ba5f4SPaul Zimmerman 		if (!(hcchar & HCCHAR_CHENA)) {
1566197ba5f4SPaul Zimmerman 			/*
1567197ba5f4SPaul Zimmerman 			 * The channel is either already halted or it hasn't
1568197ba5f4SPaul Zimmerman 			 * started yet. In DMA mode, the transfer may halt if
1569197ba5f4SPaul Zimmerman 			 * it finishes normally or a condition occurs that
1570197ba5f4SPaul Zimmerman 			 * requires driver intervention. Don't want to halt
1571197ba5f4SPaul Zimmerman 			 * the channel again. In either Slave or DMA mode,
1572197ba5f4SPaul Zimmerman 			 * it's possible that the transfer has been assigned
1573197ba5f4SPaul Zimmerman 			 * to a channel, but not started yet when an URB is
1574197ba5f4SPaul Zimmerman 			 * dequeued. Don't want to halt a channel that hasn't
1575197ba5f4SPaul Zimmerman 			 * started yet.
1576197ba5f4SPaul Zimmerman 			 */
1577197ba5f4SPaul Zimmerman 			return;
1578197ba5f4SPaul Zimmerman 		}
1579197ba5f4SPaul Zimmerman 	}
1580197ba5f4SPaul Zimmerman 	if (chan->halt_pending) {
1581197ba5f4SPaul Zimmerman 		/*
1582197ba5f4SPaul Zimmerman 		 * A halt has already been issued for this channel. This might
1583197ba5f4SPaul Zimmerman 		 * happen when a transfer is aborted by a higher level in
1584197ba5f4SPaul Zimmerman 		 * the stack.
1585197ba5f4SPaul Zimmerman 		 */
1586197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
1587197ba5f4SPaul Zimmerman 			 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1588197ba5f4SPaul Zimmerman 			 __func__, chan->hc_num);
1589197ba5f4SPaul Zimmerman 		return;
1590197ba5f4SPaul Zimmerman 	}
1591197ba5f4SPaul Zimmerman 
159295c8bc36SAntti Seppälä 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1593197ba5f4SPaul Zimmerman 
1594197ba5f4SPaul Zimmerman 	/* No need to set the bit in DDMA for disabling the channel */
1595197ba5f4SPaul Zimmerman 	/* TODO check it everywhere channel is disabled */
1596197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_desc_enable <= 0) {
1597197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1598197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1599197ba5f4SPaul Zimmerman 		hcchar |= HCCHAR_CHENA;
1600197ba5f4SPaul Zimmerman 	} else {
1601197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1602197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "desc DMA enabled\n");
1603197ba5f4SPaul Zimmerman 	}
1604197ba5f4SPaul Zimmerman 	hcchar |= HCCHAR_CHDIS;
1605197ba5f4SPaul Zimmerman 
1606197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_enable <= 0) {
1607197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1608197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "DMA not enabled\n");
1609197ba5f4SPaul Zimmerman 		hcchar |= HCCHAR_CHENA;
1610197ba5f4SPaul Zimmerman 
1611197ba5f4SPaul Zimmerman 		/* Check for space in the request queue to issue the halt */
1612197ba5f4SPaul Zimmerman 		if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1613197ba5f4SPaul Zimmerman 		    chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1614197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "control/bulk\n");
161595c8bc36SAntti Seppälä 			nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1616197ba5f4SPaul Zimmerman 			if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1617197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev, "Disabling channel\n");
1618197ba5f4SPaul Zimmerman 				hcchar &= ~HCCHAR_CHENA;
1619197ba5f4SPaul Zimmerman 			}
1620197ba5f4SPaul Zimmerman 		} else {
1621197ba5f4SPaul Zimmerman 			if (dbg_perio())
1622197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev, "isoc/intr\n");
162395c8bc36SAntti Seppälä 			hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1624197ba5f4SPaul Zimmerman 			if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1625197ba5f4SPaul Zimmerman 			    hsotg->queuing_high_bandwidth) {
1626197ba5f4SPaul Zimmerman 				if (dbg_perio())
1627197ba5f4SPaul Zimmerman 					dev_vdbg(hsotg->dev, "Disabling channel\n");
1628197ba5f4SPaul Zimmerman 				hcchar &= ~HCCHAR_CHENA;
1629197ba5f4SPaul Zimmerman 			}
1630197ba5f4SPaul Zimmerman 		}
1631197ba5f4SPaul Zimmerman 	} else {
1632197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1633197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "DMA enabled\n");
1634197ba5f4SPaul Zimmerman 	}
1635197ba5f4SPaul Zimmerman 
163695c8bc36SAntti Seppälä 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1637197ba5f4SPaul Zimmerman 	chan->halt_status = halt_status;
1638197ba5f4SPaul Zimmerman 
1639197ba5f4SPaul Zimmerman 	if (hcchar & HCCHAR_CHENA) {
1640197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1641197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "Channel enabled\n");
1642197ba5f4SPaul Zimmerman 		chan->halt_pending = 1;
1643197ba5f4SPaul Zimmerman 		chan->halt_on_queue = 0;
1644197ba5f4SPaul Zimmerman 	} else {
1645197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1646197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "Channel disabled\n");
1647197ba5f4SPaul Zimmerman 		chan->halt_on_queue = 1;
1648197ba5f4SPaul Zimmerman 	}
1649197ba5f4SPaul Zimmerman 
1650197ba5f4SPaul Zimmerman 	if (dbg_hc(chan)) {
1651197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1652197ba5f4SPaul Zimmerman 			 chan->hc_num);
1653197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 hcchar: 0x%08x\n",
1654197ba5f4SPaul Zimmerman 			 hcchar);
1655197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 halt_pending: %d\n",
1656197ba5f4SPaul Zimmerman 			 chan->halt_pending);
1657197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 halt_on_queue: %d\n",
1658197ba5f4SPaul Zimmerman 			 chan->halt_on_queue);
1659197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 halt_status: %d\n",
1660197ba5f4SPaul Zimmerman 			 chan->halt_status);
1661197ba5f4SPaul Zimmerman 	}
1662197ba5f4SPaul Zimmerman }
1663197ba5f4SPaul Zimmerman 
1664197ba5f4SPaul Zimmerman /**
1665197ba5f4SPaul Zimmerman  * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1666197ba5f4SPaul Zimmerman  *
1667197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
1668197ba5f4SPaul Zimmerman  * @chan:  Identifies the host channel to clean up
1669197ba5f4SPaul Zimmerman  *
1670197ba5f4SPaul Zimmerman  * This function is normally called after a transfer is done and the host
1671197ba5f4SPaul Zimmerman  * channel is being released
1672197ba5f4SPaul Zimmerman  */
1673197ba5f4SPaul Zimmerman void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1674197ba5f4SPaul Zimmerman {
1675197ba5f4SPaul Zimmerman 	u32 hcintmsk;
1676197ba5f4SPaul Zimmerman 
1677197ba5f4SPaul Zimmerman 	chan->xfer_started = 0;
1678197ba5f4SPaul Zimmerman 
1679197ba5f4SPaul Zimmerman 	/*
1680197ba5f4SPaul Zimmerman 	 * Clear channel interrupt enables and any unhandled channel interrupt
1681197ba5f4SPaul Zimmerman 	 * conditions
1682197ba5f4SPaul Zimmerman 	 */
168395c8bc36SAntti Seppälä 	dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1684197ba5f4SPaul Zimmerman 	hcintmsk = 0xffffffff;
1685197ba5f4SPaul Zimmerman 	hcintmsk &= ~HCINTMSK_RESERVED14_31;
168695c8bc36SAntti Seppälä 	dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1687197ba5f4SPaul Zimmerman }
1688197ba5f4SPaul Zimmerman 
1689197ba5f4SPaul Zimmerman /**
1690197ba5f4SPaul Zimmerman  * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1691197ba5f4SPaul Zimmerman  * which frame a periodic transfer should occur
1692197ba5f4SPaul Zimmerman  *
1693197ba5f4SPaul Zimmerman  * @hsotg:  Programming view of DWC_otg controller
1694197ba5f4SPaul Zimmerman  * @chan:   Identifies the host channel to set up and its properties
1695197ba5f4SPaul Zimmerman  * @hcchar: Current value of the HCCHAR register for the specified host channel
1696197ba5f4SPaul Zimmerman  *
1697197ba5f4SPaul Zimmerman  * This function has no effect on non-periodic transfers
1698197ba5f4SPaul Zimmerman  */
1699197ba5f4SPaul Zimmerman static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1700197ba5f4SPaul Zimmerman 				       struct dwc2_host_chan *chan, u32 *hcchar)
1701197ba5f4SPaul Zimmerman {
1702197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1703197ba5f4SPaul Zimmerman 	    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1704197ba5f4SPaul Zimmerman 		/* 1 if _next_ frame is odd, 0 if it's even */
1705197ba5f4SPaul Zimmerman 		if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
1706197ba5f4SPaul Zimmerman 			*hcchar |= HCCHAR_ODDFRM;
1707197ba5f4SPaul Zimmerman 	}
1708197ba5f4SPaul Zimmerman }
1709197ba5f4SPaul Zimmerman 
1710197ba5f4SPaul Zimmerman static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1711197ba5f4SPaul Zimmerman {
1712197ba5f4SPaul Zimmerman 	/* Set up the initial PID for the transfer */
1713197ba5f4SPaul Zimmerman 	if (chan->speed == USB_SPEED_HIGH) {
1714197ba5f4SPaul Zimmerman 		if (chan->ep_is_in) {
1715197ba5f4SPaul Zimmerman 			if (chan->multi_count == 1)
1716197ba5f4SPaul Zimmerman 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1717197ba5f4SPaul Zimmerman 			else if (chan->multi_count == 2)
1718197ba5f4SPaul Zimmerman 				chan->data_pid_start = DWC2_HC_PID_DATA1;
1719197ba5f4SPaul Zimmerman 			else
1720197ba5f4SPaul Zimmerman 				chan->data_pid_start = DWC2_HC_PID_DATA2;
1721197ba5f4SPaul Zimmerman 		} else {
1722197ba5f4SPaul Zimmerman 			if (chan->multi_count == 1)
1723197ba5f4SPaul Zimmerman 				chan->data_pid_start = DWC2_HC_PID_DATA0;
1724197ba5f4SPaul Zimmerman 			else
1725197ba5f4SPaul Zimmerman 				chan->data_pid_start = DWC2_HC_PID_MDATA;
1726197ba5f4SPaul Zimmerman 		}
1727197ba5f4SPaul Zimmerman 	} else {
1728197ba5f4SPaul Zimmerman 		chan->data_pid_start = DWC2_HC_PID_DATA0;
1729197ba5f4SPaul Zimmerman 	}
1730197ba5f4SPaul Zimmerman }
1731197ba5f4SPaul Zimmerman 
1732197ba5f4SPaul Zimmerman /**
1733197ba5f4SPaul Zimmerman  * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1734197ba5f4SPaul Zimmerman  * the Host Channel
1735197ba5f4SPaul Zimmerman  *
1736197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
1737197ba5f4SPaul Zimmerman  * @chan:  Information needed to initialize the host channel
1738197ba5f4SPaul Zimmerman  *
1739197ba5f4SPaul Zimmerman  * This function should only be called in Slave mode. For a channel associated
1740197ba5f4SPaul Zimmerman  * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1741197ba5f4SPaul Zimmerman  * associated with a periodic EP, the periodic Tx FIFO is written.
1742197ba5f4SPaul Zimmerman  *
1743197ba5f4SPaul Zimmerman  * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1744197ba5f4SPaul Zimmerman  * the number of bytes written to the Tx FIFO.
1745197ba5f4SPaul Zimmerman  */
1746197ba5f4SPaul Zimmerman static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1747197ba5f4SPaul Zimmerman 				 struct dwc2_host_chan *chan)
1748197ba5f4SPaul Zimmerman {
1749197ba5f4SPaul Zimmerman 	u32 i;
1750197ba5f4SPaul Zimmerman 	u32 remaining_count;
1751197ba5f4SPaul Zimmerman 	u32 byte_count;
1752197ba5f4SPaul Zimmerman 	u32 dword_count;
1753197ba5f4SPaul Zimmerman 	u32 __iomem *data_fifo;
1754197ba5f4SPaul Zimmerman 	u32 *data_buf = (u32 *)chan->xfer_buf;
1755197ba5f4SPaul Zimmerman 
1756197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1757197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1758197ba5f4SPaul Zimmerman 
1759197ba5f4SPaul Zimmerman 	data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1760197ba5f4SPaul Zimmerman 
1761197ba5f4SPaul Zimmerman 	remaining_count = chan->xfer_len - chan->xfer_count;
1762197ba5f4SPaul Zimmerman 	if (remaining_count > chan->max_packet)
1763197ba5f4SPaul Zimmerman 		byte_count = chan->max_packet;
1764197ba5f4SPaul Zimmerman 	else
1765197ba5f4SPaul Zimmerman 		byte_count = remaining_count;
1766197ba5f4SPaul Zimmerman 
1767197ba5f4SPaul Zimmerman 	dword_count = (byte_count + 3) / 4;
1768197ba5f4SPaul Zimmerman 
1769197ba5f4SPaul Zimmerman 	if (((unsigned long)data_buf & 0x3) == 0) {
1770197ba5f4SPaul Zimmerman 		/* xfer_buf is DWORD aligned */
1771197ba5f4SPaul Zimmerman 		for (i = 0; i < dword_count; i++, data_buf++)
177295c8bc36SAntti Seppälä 			dwc2_writel(*data_buf, data_fifo);
1773197ba5f4SPaul Zimmerman 	} else {
1774197ba5f4SPaul Zimmerman 		/* xfer_buf is not DWORD aligned */
1775197ba5f4SPaul Zimmerman 		for (i = 0; i < dword_count; i++, data_buf++) {
1776197ba5f4SPaul Zimmerman 			u32 data = data_buf[0] | data_buf[1] << 8 |
1777197ba5f4SPaul Zimmerman 				   data_buf[2] << 16 | data_buf[3] << 24;
177895c8bc36SAntti Seppälä 			dwc2_writel(data, data_fifo);
1779197ba5f4SPaul Zimmerman 		}
1780197ba5f4SPaul Zimmerman 	}
1781197ba5f4SPaul Zimmerman 
1782197ba5f4SPaul Zimmerman 	chan->xfer_count += byte_count;
1783197ba5f4SPaul Zimmerman 	chan->xfer_buf += byte_count;
1784197ba5f4SPaul Zimmerman }
1785197ba5f4SPaul Zimmerman 
1786197ba5f4SPaul Zimmerman /**
1787197ba5f4SPaul Zimmerman  * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1788197ba5f4SPaul Zimmerman  * channel and starts the transfer
1789197ba5f4SPaul Zimmerman  *
1790197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
1791197ba5f4SPaul Zimmerman  * @chan:  Information needed to initialize the host channel. The xfer_len value
1792197ba5f4SPaul Zimmerman  *         may be reduced to accommodate the max widths of the XferSize and
1793197ba5f4SPaul Zimmerman  *         PktCnt fields in the HCTSIZn register. The multi_count value may be
1794197ba5f4SPaul Zimmerman  *         changed to reflect the final xfer_len value.
1795197ba5f4SPaul Zimmerman  *
1796197ba5f4SPaul Zimmerman  * This function may be called in either Slave mode or DMA mode. In Slave mode,
1797197ba5f4SPaul Zimmerman  * the caller must ensure that there is sufficient space in the request queue
1798197ba5f4SPaul Zimmerman  * and Tx Data FIFO.
1799197ba5f4SPaul Zimmerman  *
1800197ba5f4SPaul Zimmerman  * For an OUT transfer in Slave mode, it loads a data packet into the
1801197ba5f4SPaul Zimmerman  * appropriate FIFO. If necessary, additional data packets are loaded in the
1802197ba5f4SPaul Zimmerman  * Host ISR.
1803197ba5f4SPaul Zimmerman  *
1804197ba5f4SPaul Zimmerman  * For an IN transfer in Slave mode, a data packet is requested. The data
1805197ba5f4SPaul Zimmerman  * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1806197ba5f4SPaul Zimmerman  * additional data packets are requested in the Host ISR.
1807197ba5f4SPaul Zimmerman  *
1808197ba5f4SPaul Zimmerman  * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1809197ba5f4SPaul Zimmerman  * register along with a packet count of 1 and the channel is enabled. This
1810197ba5f4SPaul Zimmerman  * causes a single PING transaction to occur. Other fields in HCTSIZ are
1811197ba5f4SPaul Zimmerman  * simply set to 0 since no data transfer occurs in this case.
1812197ba5f4SPaul Zimmerman  *
1813197ba5f4SPaul Zimmerman  * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1814197ba5f4SPaul Zimmerman  * all the information required to perform the subsequent data transfer. In
1815197ba5f4SPaul Zimmerman  * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1816197ba5f4SPaul Zimmerman  * controller performs the entire PING protocol, then starts the data
1817197ba5f4SPaul Zimmerman  * transfer.
1818197ba5f4SPaul Zimmerman  */
1819197ba5f4SPaul Zimmerman void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1820197ba5f4SPaul Zimmerman 			    struct dwc2_host_chan *chan)
1821197ba5f4SPaul Zimmerman {
1822197ba5f4SPaul Zimmerman 	u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1823197ba5f4SPaul Zimmerman 	u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1824197ba5f4SPaul Zimmerman 	u32 hcchar;
1825197ba5f4SPaul Zimmerman 	u32 hctsiz = 0;
1826197ba5f4SPaul Zimmerman 	u16 num_packets;
182769b76cdfSDouglas Anderson 	u32 ec_mc;
1828197ba5f4SPaul Zimmerman 
1829197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1830197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s()\n", __func__);
1831197ba5f4SPaul Zimmerman 
1832197ba5f4SPaul Zimmerman 	if (chan->do_ping) {
1833197ba5f4SPaul Zimmerman 		if (hsotg->core_params->dma_enable <= 0) {
1834197ba5f4SPaul Zimmerman 			if (dbg_hc(chan))
1835197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev, "ping, no DMA\n");
1836197ba5f4SPaul Zimmerman 			dwc2_hc_do_ping(hsotg, chan);
1837197ba5f4SPaul Zimmerman 			chan->xfer_started = 1;
1838197ba5f4SPaul Zimmerman 			return;
1839197ba5f4SPaul Zimmerman 		} else {
1840197ba5f4SPaul Zimmerman 			if (dbg_hc(chan))
1841197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev, "ping, DMA\n");
1842197ba5f4SPaul Zimmerman 			hctsiz |= TSIZ_DOPNG;
1843197ba5f4SPaul Zimmerman 		}
1844197ba5f4SPaul Zimmerman 	}
1845197ba5f4SPaul Zimmerman 
1846197ba5f4SPaul Zimmerman 	if (chan->do_split) {
1847197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1848197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "split\n");
1849197ba5f4SPaul Zimmerman 		num_packets = 1;
1850197ba5f4SPaul Zimmerman 
1851197ba5f4SPaul Zimmerman 		if (chan->complete_split && !chan->ep_is_in)
1852197ba5f4SPaul Zimmerman 			/*
1853197ba5f4SPaul Zimmerman 			 * For CSPLIT OUT Transfer, set the size to 0 so the
1854197ba5f4SPaul Zimmerman 			 * core doesn't expect any data written to the FIFO
1855197ba5f4SPaul Zimmerman 			 */
1856197ba5f4SPaul Zimmerman 			chan->xfer_len = 0;
1857197ba5f4SPaul Zimmerman 		else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1858197ba5f4SPaul Zimmerman 			chan->xfer_len = chan->max_packet;
1859197ba5f4SPaul Zimmerman 		else if (!chan->ep_is_in && chan->xfer_len > 188)
1860197ba5f4SPaul Zimmerman 			chan->xfer_len = 188;
1861197ba5f4SPaul Zimmerman 
1862197ba5f4SPaul Zimmerman 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1863197ba5f4SPaul Zimmerman 			  TSIZ_XFERSIZE_MASK;
186469b76cdfSDouglas Anderson 
186569b76cdfSDouglas Anderson 		/* For split set ec_mc for immediate retries */
186669b76cdfSDouglas Anderson 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
186769b76cdfSDouglas Anderson 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
186869b76cdfSDouglas Anderson 			ec_mc = 3;
186969b76cdfSDouglas Anderson 		else
187069b76cdfSDouglas Anderson 			ec_mc = 1;
1871197ba5f4SPaul Zimmerman 	} else {
1872197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1873197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "no split\n");
1874197ba5f4SPaul Zimmerman 		/*
1875197ba5f4SPaul Zimmerman 		 * Ensure that the transfer length and packet count will fit
1876197ba5f4SPaul Zimmerman 		 * in the widths allocated for them in the HCTSIZn register
1877197ba5f4SPaul Zimmerman 		 */
1878197ba5f4SPaul Zimmerman 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1879197ba5f4SPaul Zimmerman 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1880197ba5f4SPaul Zimmerman 			/*
1881197ba5f4SPaul Zimmerman 			 * Make sure the transfer size is no larger than one
1882197ba5f4SPaul Zimmerman 			 * (micro)frame's worth of data. (A check was done
1883197ba5f4SPaul Zimmerman 			 * when the periodic transfer was accepted to ensure
1884197ba5f4SPaul Zimmerman 			 * that a (micro)frame's worth of data can be
1885197ba5f4SPaul Zimmerman 			 * programmed into a channel.)
1886197ba5f4SPaul Zimmerman 			 */
1887197ba5f4SPaul Zimmerman 			u32 max_periodic_len =
1888197ba5f4SPaul Zimmerman 				chan->multi_count * chan->max_packet;
1889197ba5f4SPaul Zimmerman 
1890197ba5f4SPaul Zimmerman 			if (chan->xfer_len > max_periodic_len)
1891197ba5f4SPaul Zimmerman 				chan->xfer_len = max_periodic_len;
1892197ba5f4SPaul Zimmerman 		} else if (chan->xfer_len > max_hc_xfer_size) {
1893197ba5f4SPaul Zimmerman 			/*
1894197ba5f4SPaul Zimmerman 			 * Make sure that xfer_len is a multiple of max packet
1895197ba5f4SPaul Zimmerman 			 * size
1896197ba5f4SPaul Zimmerman 			 */
1897197ba5f4SPaul Zimmerman 			chan->xfer_len =
1898197ba5f4SPaul Zimmerman 				max_hc_xfer_size - chan->max_packet + 1;
1899197ba5f4SPaul Zimmerman 		}
1900197ba5f4SPaul Zimmerman 
1901197ba5f4SPaul Zimmerman 		if (chan->xfer_len > 0) {
1902197ba5f4SPaul Zimmerman 			num_packets = (chan->xfer_len + chan->max_packet - 1) /
1903197ba5f4SPaul Zimmerman 					chan->max_packet;
1904197ba5f4SPaul Zimmerman 			if (num_packets > max_hc_pkt_count) {
1905197ba5f4SPaul Zimmerman 				num_packets = max_hc_pkt_count;
1906197ba5f4SPaul Zimmerman 				chan->xfer_len = num_packets * chan->max_packet;
1907197ba5f4SPaul Zimmerman 			}
1908197ba5f4SPaul Zimmerman 		} else {
1909197ba5f4SPaul Zimmerman 			/* Need 1 packet for transfer length of 0 */
1910197ba5f4SPaul Zimmerman 			num_packets = 1;
1911197ba5f4SPaul Zimmerman 		}
1912197ba5f4SPaul Zimmerman 
1913197ba5f4SPaul Zimmerman 		if (chan->ep_is_in)
1914197ba5f4SPaul Zimmerman 			/*
1915197ba5f4SPaul Zimmerman 			 * Always program an integral # of max packets for IN
1916197ba5f4SPaul Zimmerman 			 * transfers
1917197ba5f4SPaul Zimmerman 			 */
1918197ba5f4SPaul Zimmerman 			chan->xfer_len = num_packets * chan->max_packet;
1919197ba5f4SPaul Zimmerman 
1920197ba5f4SPaul Zimmerman 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1921197ba5f4SPaul Zimmerman 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1922197ba5f4SPaul Zimmerman 			/*
1923197ba5f4SPaul Zimmerman 			 * Make sure that the multi_count field matches the
1924197ba5f4SPaul Zimmerman 			 * actual transfer length
1925197ba5f4SPaul Zimmerman 			 */
1926197ba5f4SPaul Zimmerman 			chan->multi_count = num_packets;
1927197ba5f4SPaul Zimmerman 
1928197ba5f4SPaul Zimmerman 		if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1929197ba5f4SPaul Zimmerman 			dwc2_set_pid_isoc(chan);
1930197ba5f4SPaul Zimmerman 
1931197ba5f4SPaul Zimmerman 		hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1932197ba5f4SPaul Zimmerman 			  TSIZ_XFERSIZE_MASK;
193369b76cdfSDouglas Anderson 
193469b76cdfSDouglas Anderson 		/* The ec_mc gets the multi_count for non-split */
193569b76cdfSDouglas Anderson 		ec_mc = chan->multi_count;
1936197ba5f4SPaul Zimmerman 	}
1937197ba5f4SPaul Zimmerman 
1938197ba5f4SPaul Zimmerman 	chan->start_pkt_count = num_packets;
1939197ba5f4SPaul Zimmerman 	hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1940197ba5f4SPaul Zimmerman 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1941197ba5f4SPaul Zimmerman 		  TSIZ_SC_MC_PID_MASK;
194295c8bc36SAntti Seppälä 	dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1943197ba5f4SPaul Zimmerman 	if (dbg_hc(chan)) {
1944197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1945197ba5f4SPaul Zimmerman 			 hctsiz, chan->hc_num);
1946197ba5f4SPaul Zimmerman 
1947197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1948197ba5f4SPaul Zimmerman 			 chan->hc_num);
1949197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Xfer Size: %d\n",
1950197ba5f4SPaul Zimmerman 			 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1951197ba5f4SPaul Zimmerman 			 TSIZ_XFERSIZE_SHIFT);
1952197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Num Pkts: %d\n",
1953197ba5f4SPaul Zimmerman 			 (hctsiz & TSIZ_PKTCNT_MASK) >>
1954197ba5f4SPaul Zimmerman 			 TSIZ_PKTCNT_SHIFT);
1955197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
1956197ba5f4SPaul Zimmerman 			 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1957197ba5f4SPaul Zimmerman 			 TSIZ_SC_MC_PID_SHIFT);
1958197ba5f4SPaul Zimmerman 	}
1959197ba5f4SPaul Zimmerman 
1960197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_enable > 0) {
1961197ba5f4SPaul Zimmerman 		dma_addr_t dma_addr;
1962197ba5f4SPaul Zimmerman 
1963197ba5f4SPaul Zimmerman 		if (chan->align_buf) {
1964197ba5f4SPaul Zimmerman 			if (dbg_hc(chan))
1965197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev, "align_buf\n");
1966197ba5f4SPaul Zimmerman 			dma_addr = chan->align_buf;
1967197ba5f4SPaul Zimmerman 		} else {
1968197ba5f4SPaul Zimmerman 			dma_addr = chan->xfer_dma;
1969197ba5f4SPaul Zimmerman 		}
197095c8bc36SAntti Seppälä 		dwc2_writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
1971197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
1972197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1973197ba5f4SPaul Zimmerman 				 (unsigned long)dma_addr, chan->hc_num);
1974197ba5f4SPaul Zimmerman 	}
1975197ba5f4SPaul Zimmerman 
1976197ba5f4SPaul Zimmerman 	/* Start the split */
1977197ba5f4SPaul Zimmerman 	if (chan->do_split) {
197895c8bc36SAntti Seppälä 		u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1979197ba5f4SPaul Zimmerman 
1980197ba5f4SPaul Zimmerman 		hcsplt |= HCSPLT_SPLTENA;
198195c8bc36SAntti Seppälä 		dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1982197ba5f4SPaul Zimmerman 	}
1983197ba5f4SPaul Zimmerman 
198495c8bc36SAntti Seppälä 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1985197ba5f4SPaul Zimmerman 	hcchar &= ~HCCHAR_MULTICNT_MASK;
198669b76cdfSDouglas Anderson 	hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1987197ba5f4SPaul Zimmerman 	dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1988197ba5f4SPaul Zimmerman 
1989197ba5f4SPaul Zimmerman 	if (hcchar & HCCHAR_CHDIS)
1990197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
1991197ba5f4SPaul Zimmerman 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1992197ba5f4SPaul Zimmerman 			 __func__, chan->hc_num, hcchar);
1993197ba5f4SPaul Zimmerman 
1994197ba5f4SPaul Zimmerman 	/* Set host channel enable after all other setup is complete */
1995197ba5f4SPaul Zimmerman 	hcchar |= HCCHAR_CHENA;
1996197ba5f4SPaul Zimmerman 	hcchar &= ~HCCHAR_CHDIS;
1997197ba5f4SPaul Zimmerman 
1998197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
1999197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
2000197ba5f4SPaul Zimmerman 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
2001197ba5f4SPaul Zimmerman 			 HCCHAR_MULTICNT_SHIFT);
2002197ba5f4SPaul Zimmerman 
200395c8bc36SAntti Seppälä 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2004197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
2005197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
2006197ba5f4SPaul Zimmerman 			 chan->hc_num);
2007197ba5f4SPaul Zimmerman 
2008197ba5f4SPaul Zimmerman 	chan->xfer_started = 1;
2009197ba5f4SPaul Zimmerman 	chan->requests++;
2010197ba5f4SPaul Zimmerman 
2011197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_enable <= 0 &&
2012197ba5f4SPaul Zimmerman 	    !chan->ep_is_in && chan->xfer_len > 0)
2013197ba5f4SPaul Zimmerman 		/* Load OUT packet into the appropriate Tx FIFO */
2014197ba5f4SPaul Zimmerman 		dwc2_hc_write_packet(hsotg, chan);
2015197ba5f4SPaul Zimmerman }
2016197ba5f4SPaul Zimmerman 
2017197ba5f4SPaul Zimmerman /**
2018197ba5f4SPaul Zimmerman  * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
2019197ba5f4SPaul Zimmerman  * host channel and starts the transfer in Descriptor DMA mode
2020197ba5f4SPaul Zimmerman  *
2021197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2022197ba5f4SPaul Zimmerman  * @chan:  Information needed to initialize the host channel
2023197ba5f4SPaul Zimmerman  *
2024197ba5f4SPaul Zimmerman  * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
2025197ba5f4SPaul Zimmerman  * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
2026197ba5f4SPaul Zimmerman  * with micro-frame bitmap.
2027197ba5f4SPaul Zimmerman  *
2028197ba5f4SPaul Zimmerman  * Initializes HCDMA register with descriptor list address and CTD value then
2029197ba5f4SPaul Zimmerman  * starts the transfer via enabling the channel.
2030197ba5f4SPaul Zimmerman  */
2031197ba5f4SPaul Zimmerman void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
2032197ba5f4SPaul Zimmerman 				 struct dwc2_host_chan *chan)
2033197ba5f4SPaul Zimmerman {
2034197ba5f4SPaul Zimmerman 	u32 hcchar;
2035197ba5f4SPaul Zimmerman 	u32 hctsiz = 0;
2036197ba5f4SPaul Zimmerman 
2037197ba5f4SPaul Zimmerman 	if (chan->do_ping)
2038197ba5f4SPaul Zimmerman 		hctsiz |= TSIZ_DOPNG;
2039197ba5f4SPaul Zimmerman 
2040197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2041197ba5f4SPaul Zimmerman 		dwc2_set_pid_isoc(chan);
2042197ba5f4SPaul Zimmerman 
2043197ba5f4SPaul Zimmerman 	/* Packet Count and Xfer Size are not used in Descriptor DMA mode */
2044197ba5f4SPaul Zimmerman 	hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
2045197ba5f4SPaul Zimmerman 		  TSIZ_SC_MC_PID_MASK;
2046197ba5f4SPaul Zimmerman 
2047197ba5f4SPaul Zimmerman 	/* 0 - 1 descriptor, 1 - 2 descriptors, etc */
2048197ba5f4SPaul Zimmerman 	hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
2049197ba5f4SPaul Zimmerman 
2050197ba5f4SPaul Zimmerman 	/* Non-zero only for high-speed interrupt endpoints */
2051197ba5f4SPaul Zimmerman 	hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
2052197ba5f4SPaul Zimmerman 
2053197ba5f4SPaul Zimmerman 	if (dbg_hc(chan)) {
2054197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2055197ba5f4SPaul Zimmerman 			 chan->hc_num);
2056197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Start PID: %d\n",
2057197ba5f4SPaul Zimmerman 			 chan->data_pid_start);
2058197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 NTD: %d\n", chan->ntd - 1);
2059197ba5f4SPaul Zimmerman 	}
2060197ba5f4SPaul Zimmerman 
206195c8bc36SAntti Seppälä 	dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
2062197ba5f4SPaul Zimmerman 
206395105a99SGregory Herrero 	dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
206495105a99SGregory Herrero 				   chan->desc_list_sz, DMA_TO_DEVICE);
206595105a99SGregory Herrero 
2066e23b8a54SMian Yousaf Kaukab 	dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
2067197ba5f4SPaul Zimmerman 
2068197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
2069e23b8a54SMian Yousaf Kaukab 		dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
2070e23b8a54SMian Yousaf Kaukab 			 &chan->desc_list_addr, chan->hc_num);
2071197ba5f4SPaul Zimmerman 
207295c8bc36SAntti Seppälä 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
2073197ba5f4SPaul Zimmerman 	hcchar &= ~HCCHAR_MULTICNT_MASK;
2074197ba5f4SPaul Zimmerman 	hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
2075197ba5f4SPaul Zimmerman 		  HCCHAR_MULTICNT_MASK;
2076197ba5f4SPaul Zimmerman 
2077197ba5f4SPaul Zimmerman 	if (hcchar & HCCHAR_CHDIS)
2078197ba5f4SPaul Zimmerman 		dev_warn(hsotg->dev,
2079197ba5f4SPaul Zimmerman 			 "%s: chdis set, channel %d, hcchar 0x%08x\n",
2080197ba5f4SPaul Zimmerman 			 __func__, chan->hc_num, hcchar);
2081197ba5f4SPaul Zimmerman 
2082197ba5f4SPaul Zimmerman 	/* Set host channel enable after all other setup is complete */
2083197ba5f4SPaul Zimmerman 	hcchar |= HCCHAR_CHENA;
2084197ba5f4SPaul Zimmerman 	hcchar &= ~HCCHAR_CHDIS;
2085197ba5f4SPaul Zimmerman 
2086197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
2087197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "	 Multi Cnt: %d\n",
2088197ba5f4SPaul Zimmerman 			 (hcchar & HCCHAR_MULTICNT_MASK) >>
2089197ba5f4SPaul Zimmerman 			 HCCHAR_MULTICNT_SHIFT);
2090197ba5f4SPaul Zimmerman 
209195c8bc36SAntti Seppälä 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2092197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
2093197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
2094197ba5f4SPaul Zimmerman 			 chan->hc_num);
2095197ba5f4SPaul Zimmerman 
2096197ba5f4SPaul Zimmerman 	chan->xfer_started = 1;
2097197ba5f4SPaul Zimmerman 	chan->requests++;
2098197ba5f4SPaul Zimmerman }
2099197ba5f4SPaul Zimmerman 
2100197ba5f4SPaul Zimmerman /**
2101197ba5f4SPaul Zimmerman  * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
2102197ba5f4SPaul Zimmerman  * a previous call to dwc2_hc_start_transfer()
2103197ba5f4SPaul Zimmerman  *
2104197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2105197ba5f4SPaul Zimmerman  * @chan:  Information needed to initialize the host channel
2106197ba5f4SPaul Zimmerman  *
2107197ba5f4SPaul Zimmerman  * The caller must ensure there is sufficient space in the request queue and Tx
2108197ba5f4SPaul Zimmerman  * Data FIFO. This function should only be called in Slave mode. In DMA mode,
2109197ba5f4SPaul Zimmerman  * the controller acts autonomously to complete transfers programmed to a host
2110197ba5f4SPaul Zimmerman  * channel.
2111197ba5f4SPaul Zimmerman  *
2112197ba5f4SPaul Zimmerman  * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
2113197ba5f4SPaul Zimmerman  * if there is any data remaining to be queued. For an IN transfer, another
2114197ba5f4SPaul Zimmerman  * data packet is always requested. For the SETUP phase of a control transfer,
2115197ba5f4SPaul Zimmerman  * this function does nothing.
2116197ba5f4SPaul Zimmerman  *
2117197ba5f4SPaul Zimmerman  * Return: 1 if a new request is queued, 0 if no more requests are required
2118197ba5f4SPaul Zimmerman  * for this transfer
2119197ba5f4SPaul Zimmerman  */
2120197ba5f4SPaul Zimmerman int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
2121197ba5f4SPaul Zimmerman 			      struct dwc2_host_chan *chan)
2122197ba5f4SPaul Zimmerman {
2123197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
2124197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2125197ba5f4SPaul Zimmerman 			 chan->hc_num);
2126197ba5f4SPaul Zimmerman 
2127197ba5f4SPaul Zimmerman 	if (chan->do_split)
2128197ba5f4SPaul Zimmerman 		/* SPLITs always queue just once per channel */
2129197ba5f4SPaul Zimmerman 		return 0;
2130197ba5f4SPaul Zimmerman 
2131197ba5f4SPaul Zimmerman 	if (chan->data_pid_start == DWC2_HC_PID_SETUP)
2132197ba5f4SPaul Zimmerman 		/* SETUPs are queued only once since they can't be NAK'd */
2133197ba5f4SPaul Zimmerman 		return 0;
2134197ba5f4SPaul Zimmerman 
2135197ba5f4SPaul Zimmerman 	if (chan->ep_is_in) {
2136197ba5f4SPaul Zimmerman 		/*
2137197ba5f4SPaul Zimmerman 		 * Always queue another request for other IN transfers. If
2138197ba5f4SPaul Zimmerman 		 * back-to-back INs are issued and NAKs are received for both,
2139197ba5f4SPaul Zimmerman 		 * the driver may still be processing the first NAK when the
2140197ba5f4SPaul Zimmerman 		 * second NAK is received. When the interrupt handler clears
2141197ba5f4SPaul Zimmerman 		 * the NAK interrupt for the first NAK, the second NAK will
2142197ba5f4SPaul Zimmerman 		 * not be seen. So we can't depend on the NAK interrupt
2143197ba5f4SPaul Zimmerman 		 * handler to requeue a NAK'd request. Instead, IN requests
2144197ba5f4SPaul Zimmerman 		 * are issued each time this function is called. When the
2145197ba5f4SPaul Zimmerman 		 * transfer completes, the extra requests for the channel will
2146197ba5f4SPaul Zimmerman 		 * be flushed.
2147197ba5f4SPaul Zimmerman 		 */
214895c8bc36SAntti Seppälä 		u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
2149197ba5f4SPaul Zimmerman 
2150197ba5f4SPaul Zimmerman 		dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
2151197ba5f4SPaul Zimmerman 		hcchar |= HCCHAR_CHENA;
2152197ba5f4SPaul Zimmerman 		hcchar &= ~HCCHAR_CHDIS;
2153197ba5f4SPaul Zimmerman 		if (dbg_hc(chan))
2154197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "	 IN xfer: hcchar = 0x%08x\n",
2155197ba5f4SPaul Zimmerman 				 hcchar);
215695c8bc36SAntti Seppälä 		dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2157197ba5f4SPaul Zimmerman 		chan->requests++;
2158197ba5f4SPaul Zimmerman 		return 1;
2159197ba5f4SPaul Zimmerman 	}
2160197ba5f4SPaul Zimmerman 
2161197ba5f4SPaul Zimmerman 	/* OUT transfers */
2162197ba5f4SPaul Zimmerman 
2163197ba5f4SPaul Zimmerman 	if (chan->xfer_count < chan->xfer_len) {
2164197ba5f4SPaul Zimmerman 		if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2165197ba5f4SPaul Zimmerman 		    chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
216695c8bc36SAntti Seppälä 			u32 hcchar = dwc2_readl(hsotg->regs +
2167197ba5f4SPaul Zimmerman 						HCCHAR(chan->hc_num));
2168197ba5f4SPaul Zimmerman 
2169197ba5f4SPaul Zimmerman 			dwc2_hc_set_even_odd_frame(hsotg, chan,
2170197ba5f4SPaul Zimmerman 						   &hcchar);
2171197ba5f4SPaul Zimmerman 		}
2172197ba5f4SPaul Zimmerman 
2173197ba5f4SPaul Zimmerman 		/* Load OUT packet into the appropriate Tx FIFO */
2174197ba5f4SPaul Zimmerman 		dwc2_hc_write_packet(hsotg, chan);
2175197ba5f4SPaul Zimmerman 		chan->requests++;
2176197ba5f4SPaul Zimmerman 		return 1;
2177197ba5f4SPaul Zimmerman 	}
2178197ba5f4SPaul Zimmerman 
2179197ba5f4SPaul Zimmerman 	return 0;
2180197ba5f4SPaul Zimmerman }
2181197ba5f4SPaul Zimmerman 
2182197ba5f4SPaul Zimmerman /**
2183197ba5f4SPaul Zimmerman  * dwc2_hc_do_ping() - Starts a PING transfer
2184197ba5f4SPaul Zimmerman  *
2185197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2186197ba5f4SPaul Zimmerman  * @chan:  Information needed to initialize the host channel
2187197ba5f4SPaul Zimmerman  *
2188197ba5f4SPaul Zimmerman  * This function should only be called in Slave mode. The Do Ping bit is set in
2189197ba5f4SPaul Zimmerman  * the HCTSIZ register, then the channel is enabled.
2190197ba5f4SPaul Zimmerman  */
2191197ba5f4SPaul Zimmerman void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
2192197ba5f4SPaul Zimmerman {
2193197ba5f4SPaul Zimmerman 	u32 hcchar;
2194197ba5f4SPaul Zimmerman 	u32 hctsiz;
2195197ba5f4SPaul Zimmerman 
2196197ba5f4SPaul Zimmerman 	if (dbg_hc(chan))
2197197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2198197ba5f4SPaul Zimmerman 			 chan->hc_num);
2199197ba5f4SPaul Zimmerman 
2200197ba5f4SPaul Zimmerman 
2201197ba5f4SPaul Zimmerman 	hctsiz = TSIZ_DOPNG;
2202197ba5f4SPaul Zimmerman 	hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
220395c8bc36SAntti Seppälä 	dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
2204197ba5f4SPaul Zimmerman 
220595c8bc36SAntti Seppälä 	hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
2206197ba5f4SPaul Zimmerman 	hcchar |= HCCHAR_CHENA;
2207197ba5f4SPaul Zimmerman 	hcchar &= ~HCCHAR_CHDIS;
220895c8bc36SAntti Seppälä 	dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2209197ba5f4SPaul Zimmerman }
2210197ba5f4SPaul Zimmerman 
2211197ba5f4SPaul Zimmerman /**
2212197ba5f4SPaul Zimmerman  * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
2213197ba5f4SPaul Zimmerman  * the HFIR register according to PHY type and speed
2214197ba5f4SPaul Zimmerman  *
2215197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2216197ba5f4SPaul Zimmerman  *
2217197ba5f4SPaul Zimmerman  * NOTE: The caller can modify the value of the HFIR register only after the
2218197ba5f4SPaul Zimmerman  * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
2219197ba5f4SPaul Zimmerman  * has been set
2220197ba5f4SPaul Zimmerman  */
2221197ba5f4SPaul Zimmerman u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
2222197ba5f4SPaul Zimmerman {
2223197ba5f4SPaul Zimmerman 	u32 usbcfg;
2224197ba5f4SPaul Zimmerman 	u32 hprt0;
2225197ba5f4SPaul Zimmerman 	int clock = 60;	/* default value */
2226197ba5f4SPaul Zimmerman 
222795c8bc36SAntti Seppälä 	usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
222895c8bc36SAntti Seppälä 	hprt0 = dwc2_readl(hsotg->regs + HPRT0);
2229197ba5f4SPaul Zimmerman 
2230197ba5f4SPaul Zimmerman 	if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
2231197ba5f4SPaul Zimmerman 	    !(usbcfg & GUSBCFG_PHYIF16))
2232197ba5f4SPaul Zimmerman 		clock = 60;
2233197ba5f4SPaul Zimmerman 	if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
2234197ba5f4SPaul Zimmerman 	    GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
2235197ba5f4SPaul Zimmerman 		clock = 48;
2236197ba5f4SPaul Zimmerman 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2237197ba5f4SPaul Zimmerman 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2238197ba5f4SPaul Zimmerman 		clock = 30;
2239197ba5f4SPaul Zimmerman 	if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2240197ba5f4SPaul Zimmerman 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
2241197ba5f4SPaul Zimmerman 		clock = 60;
2242197ba5f4SPaul Zimmerman 	if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2243197ba5f4SPaul Zimmerman 	    !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2244197ba5f4SPaul Zimmerman 		clock = 48;
2245197ba5f4SPaul Zimmerman 	if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
2246197ba5f4SPaul Zimmerman 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
2247197ba5f4SPaul Zimmerman 		clock = 48;
2248197ba5f4SPaul Zimmerman 	if ((usbcfg & GUSBCFG_PHYSEL) &&
2249197ba5f4SPaul Zimmerman 	    hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2250197ba5f4SPaul Zimmerman 		clock = 48;
2251197ba5f4SPaul Zimmerman 
2252197ba5f4SPaul Zimmerman 	if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
2253197ba5f4SPaul Zimmerman 		/* High speed case */
2254197ba5f4SPaul Zimmerman 		return 125 * clock;
2255197ba5f4SPaul Zimmerman 	else
2256197ba5f4SPaul Zimmerman 		/* FS/LS case */
2257197ba5f4SPaul Zimmerman 		return 1000 * clock;
2258197ba5f4SPaul Zimmerman }
2259197ba5f4SPaul Zimmerman 
2260197ba5f4SPaul Zimmerman /**
2261197ba5f4SPaul Zimmerman  * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
2262197ba5f4SPaul Zimmerman  * buffer
2263197ba5f4SPaul Zimmerman  *
2264197ba5f4SPaul Zimmerman  * @core_if: Programming view of DWC_otg controller
2265197ba5f4SPaul Zimmerman  * @dest:    Destination buffer for the packet
2266197ba5f4SPaul Zimmerman  * @bytes:   Number of bytes to copy to the destination
2267197ba5f4SPaul Zimmerman  */
2268197ba5f4SPaul Zimmerman void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
2269197ba5f4SPaul Zimmerman {
2270197ba5f4SPaul Zimmerman 	u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
2271197ba5f4SPaul Zimmerman 	u32 *data_buf = (u32 *)dest;
2272197ba5f4SPaul Zimmerman 	int word_count = (bytes + 3) / 4;
2273197ba5f4SPaul Zimmerman 	int i;
2274197ba5f4SPaul Zimmerman 
2275197ba5f4SPaul Zimmerman 	/*
2276197ba5f4SPaul Zimmerman 	 * Todo: Account for the case where dest is not dword aligned. This
2277197ba5f4SPaul Zimmerman 	 * requires reading data from the FIFO into a u32 temp buffer, then
2278197ba5f4SPaul Zimmerman 	 * moving it into the data buffer.
2279197ba5f4SPaul Zimmerman 	 */
2280197ba5f4SPaul Zimmerman 
2281197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
2282197ba5f4SPaul Zimmerman 
2283197ba5f4SPaul Zimmerman 	for (i = 0; i < word_count; i++, data_buf++)
228495c8bc36SAntti Seppälä 		*data_buf = dwc2_readl(fifo);
2285197ba5f4SPaul Zimmerman }
2286197ba5f4SPaul Zimmerman 
2287197ba5f4SPaul Zimmerman /**
2288197ba5f4SPaul Zimmerman  * dwc2_dump_host_registers() - Prints the host registers
2289197ba5f4SPaul Zimmerman  *
2290197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2291197ba5f4SPaul Zimmerman  *
2292197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
2293197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
2294197ba5f4SPaul Zimmerman  */
2295197ba5f4SPaul Zimmerman void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
2296197ba5f4SPaul Zimmerman {
2297197ba5f4SPaul Zimmerman #ifdef DEBUG
2298197ba5f4SPaul Zimmerman 	u32 __iomem *addr;
2299197ba5f4SPaul Zimmerman 	int i;
2300197ba5f4SPaul Zimmerman 
2301197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Host Global Registers\n");
2302197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HCFG;
2303197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HCFG	 @0x%08lX : 0x%08X\n",
230495c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2305197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HFIR;
2306197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HFIR	 @0x%08lX : 0x%08X\n",
230795c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2308197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HFNUM;
2309197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HFNUM	 @0x%08lX : 0x%08X\n",
231095c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2311197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HPTXSTS;
2312197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HPTXSTS	 @0x%08lX : 0x%08X\n",
231395c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2314197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HAINT;
2315197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HAINT	 @0x%08lX : 0x%08X\n",
231695c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2317197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HAINTMSK;
2318197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HAINTMSK	 @0x%08lX : 0x%08X\n",
231995c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2320197ba5f4SPaul Zimmerman 	if (hsotg->core_params->dma_desc_enable > 0) {
2321197ba5f4SPaul Zimmerman 		addr = hsotg->regs + HFLBADDR;
2322197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
232395c8bc36SAntti Seppälä 			(unsigned long)addr, dwc2_readl(addr));
2324197ba5f4SPaul Zimmerman 	}
2325197ba5f4SPaul Zimmerman 
2326197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HPRT0;
2327197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HPRT0	 @0x%08lX : 0x%08X\n",
232895c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2329197ba5f4SPaul Zimmerman 
2330197ba5f4SPaul Zimmerman 	for (i = 0; i < hsotg->core_params->host_channels; i++) {
2331197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
2332197ba5f4SPaul Zimmerman 		addr = hsotg->regs + HCCHAR(i);
2333197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HCCHAR	 @0x%08lX : 0x%08X\n",
233495c8bc36SAntti Seppälä 			(unsigned long)addr, dwc2_readl(addr));
2335197ba5f4SPaul Zimmerman 		addr = hsotg->regs + HCSPLT(i);
2336197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HCSPLT	 @0x%08lX : 0x%08X\n",
233795c8bc36SAntti Seppälä 			(unsigned long)addr, dwc2_readl(addr));
2338197ba5f4SPaul Zimmerman 		addr = hsotg->regs + HCINT(i);
2339197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HCINT	 @0x%08lX : 0x%08X\n",
234095c8bc36SAntti Seppälä 			(unsigned long)addr, dwc2_readl(addr));
2341197ba5f4SPaul Zimmerman 		addr = hsotg->regs + HCINTMSK(i);
2342197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HCINTMSK	 @0x%08lX : 0x%08X\n",
234395c8bc36SAntti Seppälä 			(unsigned long)addr, dwc2_readl(addr));
2344197ba5f4SPaul Zimmerman 		addr = hsotg->regs + HCTSIZ(i);
2345197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HCTSIZ	 @0x%08lX : 0x%08X\n",
234695c8bc36SAntti Seppälä 			(unsigned long)addr, dwc2_readl(addr));
2347197ba5f4SPaul Zimmerman 		addr = hsotg->regs + HCDMA(i);
2348197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "HCDMA	 @0x%08lX : 0x%08X\n",
234995c8bc36SAntti Seppälä 			(unsigned long)addr, dwc2_readl(addr));
2350197ba5f4SPaul Zimmerman 		if (hsotg->core_params->dma_desc_enable > 0) {
2351197ba5f4SPaul Zimmerman 			addr = hsotg->regs + HCDMAB(i);
2352197ba5f4SPaul Zimmerman 			dev_dbg(hsotg->dev, "HCDMAB	 @0x%08lX : 0x%08X\n",
235395c8bc36SAntti Seppälä 				(unsigned long)addr, dwc2_readl(addr));
2354197ba5f4SPaul Zimmerman 		}
2355197ba5f4SPaul Zimmerman 	}
2356197ba5f4SPaul Zimmerman #endif
2357197ba5f4SPaul Zimmerman }
2358197ba5f4SPaul Zimmerman 
2359197ba5f4SPaul Zimmerman /**
2360197ba5f4SPaul Zimmerman  * dwc2_dump_global_registers() - Prints the core global registers
2361197ba5f4SPaul Zimmerman  *
2362197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2363197ba5f4SPaul Zimmerman  *
2364197ba5f4SPaul Zimmerman  * NOTE: This function will be removed once the peripheral controller code
2365197ba5f4SPaul Zimmerman  * is integrated and the driver is stable
2366197ba5f4SPaul Zimmerman  */
2367197ba5f4SPaul Zimmerman void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
2368197ba5f4SPaul Zimmerman {
2369197ba5f4SPaul Zimmerman #ifdef DEBUG
2370197ba5f4SPaul Zimmerman 	u32 __iomem *addr;
2371197ba5f4SPaul Zimmerman 
2372197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Core Global Registers\n");
2373197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GOTGCTL;
2374197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GOTGCTL	 @0x%08lX : 0x%08X\n",
237595c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2376197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GOTGINT;
2377197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GOTGINT	 @0x%08lX : 0x%08X\n",
237895c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2379197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GAHBCFG;
2380197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GAHBCFG	 @0x%08lX : 0x%08X\n",
238195c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2382197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GUSBCFG;
2383197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GUSBCFG	 @0x%08lX : 0x%08X\n",
238495c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2385197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GRSTCTL;
2386197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GRSTCTL	 @0x%08lX : 0x%08X\n",
238795c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2388197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GINTSTS;
2389197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GINTSTS	 @0x%08lX : 0x%08X\n",
239095c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2391197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GINTMSK;
2392197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GINTMSK	 @0x%08lX : 0x%08X\n",
239395c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2394197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GRXSTSR;
2395197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GRXSTSR	 @0x%08lX : 0x%08X\n",
239695c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2397197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GRXFSIZ;
2398197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GRXFSIZ	 @0x%08lX : 0x%08X\n",
239995c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2400197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GNPTXFSIZ;
2401197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GNPTXFSIZ	 @0x%08lX : 0x%08X\n",
240295c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2403197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GNPTXSTS;
2404197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GNPTXSTS	 @0x%08lX : 0x%08X\n",
240595c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2406197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GI2CCTL;
2407197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GI2CCTL	 @0x%08lX : 0x%08X\n",
240895c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2409197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GPVNDCTL;
2410197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GPVNDCTL	 @0x%08lX : 0x%08X\n",
241195c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2412197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GGPIO;
2413197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GGPIO	 @0x%08lX : 0x%08X\n",
241495c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2415197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GUID;
2416197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GUID	 @0x%08lX : 0x%08X\n",
241795c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2418197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GSNPSID;
2419197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GSNPSID	 @0x%08lX : 0x%08X\n",
242095c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2421197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GHWCFG1;
2422197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GHWCFG1	 @0x%08lX : 0x%08X\n",
242395c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2424197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GHWCFG2;
2425197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GHWCFG2	 @0x%08lX : 0x%08X\n",
242695c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2427197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GHWCFG3;
2428197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GHWCFG3	 @0x%08lX : 0x%08X\n",
242995c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2430197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GHWCFG4;
2431197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GHWCFG4	 @0x%08lX : 0x%08X\n",
243295c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2433197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GLPMCFG;
2434197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GLPMCFG	 @0x%08lX : 0x%08X\n",
243595c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2436197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GPWRDN;
2437197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GPWRDN	 @0x%08lX : 0x%08X\n",
243895c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2439197ba5f4SPaul Zimmerman 	addr = hsotg->regs + GDFIFOCFG;
2440197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "GDFIFOCFG	 @0x%08lX : 0x%08X\n",
244195c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2442197ba5f4SPaul Zimmerman 	addr = hsotg->regs + HPTXFSIZ;
2443197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "HPTXFSIZ	 @0x%08lX : 0x%08X\n",
244495c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2445197ba5f4SPaul Zimmerman 
2446197ba5f4SPaul Zimmerman 	addr = hsotg->regs + PCGCTL;
2447197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "PCGCTL	 @0x%08lX : 0x%08X\n",
244895c8bc36SAntti Seppälä 		(unsigned long)addr, dwc2_readl(addr));
2449197ba5f4SPaul Zimmerman #endif
2450197ba5f4SPaul Zimmerman }
2451197ba5f4SPaul Zimmerman 
2452197ba5f4SPaul Zimmerman /**
2453197ba5f4SPaul Zimmerman  * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
2454197ba5f4SPaul Zimmerman  *
2455197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2456197ba5f4SPaul Zimmerman  * @num:   Tx FIFO to flush
2457197ba5f4SPaul Zimmerman  */
2458197ba5f4SPaul Zimmerman void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
2459197ba5f4SPaul Zimmerman {
2460197ba5f4SPaul Zimmerman 	u32 greset;
2461197ba5f4SPaul Zimmerman 	int count = 0;
2462197ba5f4SPaul Zimmerman 
2463197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
2464197ba5f4SPaul Zimmerman 
2465197ba5f4SPaul Zimmerman 	greset = GRSTCTL_TXFFLSH;
2466197ba5f4SPaul Zimmerman 	greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
246795c8bc36SAntti Seppälä 	dwc2_writel(greset, hsotg->regs + GRSTCTL);
2468197ba5f4SPaul Zimmerman 
2469197ba5f4SPaul Zimmerman 	do {
247095c8bc36SAntti Seppälä 		greset = dwc2_readl(hsotg->regs + GRSTCTL);
2471197ba5f4SPaul Zimmerman 		if (++count > 10000) {
2472197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev,
2473197ba5f4SPaul Zimmerman 				 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
2474197ba5f4SPaul Zimmerman 				 __func__, greset,
247595c8bc36SAntti Seppälä 				 dwc2_readl(hsotg->regs + GNPTXSTS));
2476197ba5f4SPaul Zimmerman 			break;
2477197ba5f4SPaul Zimmerman 		}
2478197ba5f4SPaul Zimmerman 		udelay(1);
2479197ba5f4SPaul Zimmerman 	} while (greset & GRSTCTL_TXFFLSH);
2480197ba5f4SPaul Zimmerman 
2481197ba5f4SPaul Zimmerman 	/* Wait for at least 3 PHY Clocks */
2482197ba5f4SPaul Zimmerman 	udelay(1);
2483197ba5f4SPaul Zimmerman }
2484197ba5f4SPaul Zimmerman 
2485197ba5f4SPaul Zimmerman /**
2486197ba5f4SPaul Zimmerman  * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
2487197ba5f4SPaul Zimmerman  *
2488197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
2489197ba5f4SPaul Zimmerman  */
2490197ba5f4SPaul Zimmerman void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
2491197ba5f4SPaul Zimmerman {
2492197ba5f4SPaul Zimmerman 	u32 greset;
2493197ba5f4SPaul Zimmerman 	int count = 0;
2494197ba5f4SPaul Zimmerman 
2495197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
2496197ba5f4SPaul Zimmerman 
2497197ba5f4SPaul Zimmerman 	greset = GRSTCTL_RXFFLSH;
249895c8bc36SAntti Seppälä 	dwc2_writel(greset, hsotg->regs + GRSTCTL);
2499197ba5f4SPaul Zimmerman 
2500197ba5f4SPaul Zimmerman 	do {
250195c8bc36SAntti Seppälä 		greset = dwc2_readl(hsotg->regs + GRSTCTL);
2502197ba5f4SPaul Zimmerman 		if (++count > 10000) {
2503197ba5f4SPaul Zimmerman 			dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
2504197ba5f4SPaul Zimmerman 				 __func__, greset);
2505197ba5f4SPaul Zimmerman 			break;
2506197ba5f4SPaul Zimmerman 		}
2507197ba5f4SPaul Zimmerman 		udelay(1);
2508197ba5f4SPaul Zimmerman 	} while (greset & GRSTCTL_RXFFLSH);
2509197ba5f4SPaul Zimmerman 
2510197ba5f4SPaul Zimmerman 	/* Wait for at least 3 PHY Clocks */
2511197ba5f4SPaul Zimmerman 	udelay(1);
2512197ba5f4SPaul Zimmerman }
2513197ba5f4SPaul Zimmerman 
2514197ba5f4SPaul Zimmerman #define DWC2_OUT_OF_BOUNDS(a, b, c)	((a) < (b) || (a) > (c))
2515197ba5f4SPaul Zimmerman 
2516197ba5f4SPaul Zimmerman /* Parameter access functions */
2517197ba5f4SPaul Zimmerman void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
2518197ba5f4SPaul Zimmerman {
2519197ba5f4SPaul Zimmerman 	int valid = 1;
2520197ba5f4SPaul Zimmerman 
2521197ba5f4SPaul Zimmerman 	switch (val) {
2522197ba5f4SPaul Zimmerman 	case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
2523197ba5f4SPaul Zimmerman 		if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
2524197ba5f4SPaul Zimmerman 			valid = 0;
2525197ba5f4SPaul Zimmerman 		break;
2526197ba5f4SPaul Zimmerman 	case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
2527197ba5f4SPaul Zimmerman 		switch (hsotg->hw_params.op_mode) {
2528197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2529197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2530197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2531197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2532197ba5f4SPaul Zimmerman 			break;
2533197ba5f4SPaul Zimmerman 		default:
2534197ba5f4SPaul Zimmerman 			valid = 0;
2535197ba5f4SPaul Zimmerman 			break;
2536197ba5f4SPaul Zimmerman 		}
2537197ba5f4SPaul Zimmerman 		break;
2538197ba5f4SPaul Zimmerman 	case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
2539197ba5f4SPaul Zimmerman 		/* always valid */
2540197ba5f4SPaul Zimmerman 		break;
2541197ba5f4SPaul Zimmerman 	default:
2542197ba5f4SPaul Zimmerman 		valid = 0;
2543197ba5f4SPaul Zimmerman 		break;
2544197ba5f4SPaul Zimmerman 	}
2545197ba5f4SPaul Zimmerman 
2546197ba5f4SPaul Zimmerman 	if (!valid) {
2547197ba5f4SPaul Zimmerman 		if (val >= 0)
2548197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2549197ba5f4SPaul Zimmerman 				"%d invalid for otg_cap parameter. Check HW configuration.\n",
2550197ba5f4SPaul Zimmerman 				val);
2551197ba5f4SPaul Zimmerman 		switch (hsotg->hw_params.op_mode) {
2552197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2553197ba5f4SPaul Zimmerman 			val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2554197ba5f4SPaul Zimmerman 			break;
2555197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2556197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2557197ba5f4SPaul Zimmerman 		case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2558197ba5f4SPaul Zimmerman 			val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2559197ba5f4SPaul Zimmerman 			break;
2560197ba5f4SPaul Zimmerman 		default:
2561197ba5f4SPaul Zimmerman 			val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2562197ba5f4SPaul Zimmerman 			break;
2563197ba5f4SPaul Zimmerman 		}
2564197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
2565197ba5f4SPaul Zimmerman 	}
2566197ba5f4SPaul Zimmerman 
2567197ba5f4SPaul Zimmerman 	hsotg->core_params->otg_cap = val;
2568197ba5f4SPaul Zimmerman }
2569197ba5f4SPaul Zimmerman 
2570197ba5f4SPaul Zimmerman void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
2571197ba5f4SPaul Zimmerman {
2572197ba5f4SPaul Zimmerman 	int valid = 1;
2573197ba5f4SPaul Zimmerman 
2574197ba5f4SPaul Zimmerman 	if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
2575197ba5f4SPaul Zimmerman 		valid = 0;
2576197ba5f4SPaul Zimmerman 	if (val < 0)
2577197ba5f4SPaul Zimmerman 		valid = 0;
2578197ba5f4SPaul Zimmerman 
2579197ba5f4SPaul Zimmerman 	if (!valid) {
2580197ba5f4SPaul Zimmerman 		if (val >= 0)
2581197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2582197ba5f4SPaul Zimmerman 				"%d invalid for dma_enable parameter. Check HW configuration.\n",
2583197ba5f4SPaul Zimmerman 				val);
2584197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
2585197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
2586197ba5f4SPaul Zimmerman 	}
2587197ba5f4SPaul Zimmerman 
2588197ba5f4SPaul Zimmerman 	hsotg->core_params->dma_enable = val;
2589197ba5f4SPaul Zimmerman }
2590197ba5f4SPaul Zimmerman 
2591197ba5f4SPaul Zimmerman void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
2592197ba5f4SPaul Zimmerman {
2593197ba5f4SPaul Zimmerman 	int valid = 1;
2594197ba5f4SPaul Zimmerman 
2595197ba5f4SPaul Zimmerman 	if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2596197ba5f4SPaul Zimmerman 			!hsotg->hw_params.dma_desc_enable))
2597197ba5f4SPaul Zimmerman 		valid = 0;
2598197ba5f4SPaul Zimmerman 	if (val < 0)
2599197ba5f4SPaul Zimmerman 		valid = 0;
2600197ba5f4SPaul Zimmerman 
2601197ba5f4SPaul Zimmerman 	if (!valid) {
2602197ba5f4SPaul Zimmerman 		if (val >= 0)
2603197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2604197ba5f4SPaul Zimmerman 				"%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2605197ba5f4SPaul Zimmerman 				val);
2606197ba5f4SPaul Zimmerman 		val = (hsotg->core_params->dma_enable > 0 &&
2607197ba5f4SPaul Zimmerman 			hsotg->hw_params.dma_desc_enable);
2608197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
2609197ba5f4SPaul Zimmerman 	}
2610197ba5f4SPaul Zimmerman 
2611197ba5f4SPaul Zimmerman 	hsotg->core_params->dma_desc_enable = val;
2612197ba5f4SPaul Zimmerman }
2613197ba5f4SPaul Zimmerman 
2614fbb9e22bSMian Yousaf Kaukab void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg, int val)
2615fbb9e22bSMian Yousaf Kaukab {
2616fbb9e22bSMian Yousaf Kaukab 	int valid = 1;
2617fbb9e22bSMian Yousaf Kaukab 
2618fbb9e22bSMian Yousaf Kaukab 	if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2619fbb9e22bSMian Yousaf Kaukab 			!hsotg->hw_params.dma_desc_enable))
2620fbb9e22bSMian Yousaf Kaukab 		valid = 0;
2621fbb9e22bSMian Yousaf Kaukab 	if (val < 0)
2622fbb9e22bSMian Yousaf Kaukab 		valid = 0;
2623fbb9e22bSMian Yousaf Kaukab 
2624fbb9e22bSMian Yousaf Kaukab 	if (!valid) {
2625fbb9e22bSMian Yousaf Kaukab 		if (val >= 0)
2626fbb9e22bSMian Yousaf Kaukab 			dev_err(hsotg->dev,
2627fbb9e22bSMian Yousaf Kaukab 				"%d invalid for dma_desc_fs_enable parameter. Check HW configuration.\n",
2628fbb9e22bSMian Yousaf Kaukab 				val);
2629fbb9e22bSMian Yousaf Kaukab 		val = (hsotg->core_params->dma_enable > 0 &&
2630fbb9e22bSMian Yousaf Kaukab 			hsotg->hw_params.dma_desc_enable);
2631fbb9e22bSMian Yousaf Kaukab 	}
2632fbb9e22bSMian Yousaf Kaukab 
2633fbb9e22bSMian Yousaf Kaukab 	hsotg->core_params->dma_desc_fs_enable = val;
2634fbb9e22bSMian Yousaf Kaukab 	dev_dbg(hsotg->dev, "Setting dma_desc_fs_enable to %d\n", val);
2635fbb9e22bSMian Yousaf Kaukab }
2636fbb9e22bSMian Yousaf Kaukab 
2637197ba5f4SPaul Zimmerman void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2638197ba5f4SPaul Zimmerman 						 int val)
2639197ba5f4SPaul Zimmerman {
2640197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2641197ba5f4SPaul Zimmerman 		if (val >= 0) {
2642197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2643197ba5f4SPaul Zimmerman 				"Wrong value for host_support_fs_low_power\n");
2644197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2645197ba5f4SPaul Zimmerman 				"host_support_fs_low_power must be 0 or 1\n");
2646197ba5f4SPaul Zimmerman 		}
2647197ba5f4SPaul Zimmerman 		val = 0;
2648197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev,
2649197ba5f4SPaul Zimmerman 			"Setting host_support_fs_low_power to %d\n", val);
2650197ba5f4SPaul Zimmerman 	}
2651197ba5f4SPaul Zimmerman 
2652197ba5f4SPaul Zimmerman 	hsotg->core_params->host_support_fs_ls_low_power = val;
2653197ba5f4SPaul Zimmerman }
2654197ba5f4SPaul Zimmerman 
2655197ba5f4SPaul Zimmerman void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
2656197ba5f4SPaul Zimmerman {
2657197ba5f4SPaul Zimmerman 	int valid = 1;
2658197ba5f4SPaul Zimmerman 
2659197ba5f4SPaul Zimmerman 	if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
2660197ba5f4SPaul Zimmerman 		valid = 0;
2661197ba5f4SPaul Zimmerman 	if (val < 0)
2662197ba5f4SPaul Zimmerman 		valid = 0;
2663197ba5f4SPaul Zimmerman 
2664197ba5f4SPaul Zimmerman 	if (!valid) {
2665197ba5f4SPaul Zimmerman 		if (val >= 0)
2666197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2667197ba5f4SPaul Zimmerman 				"%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2668197ba5f4SPaul Zimmerman 				val);
2669197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.enable_dynamic_fifo;
2670197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
2671197ba5f4SPaul Zimmerman 	}
2672197ba5f4SPaul Zimmerman 
2673197ba5f4SPaul Zimmerman 	hsotg->core_params->enable_dynamic_fifo = val;
2674197ba5f4SPaul Zimmerman }
2675197ba5f4SPaul Zimmerman 
2676197ba5f4SPaul Zimmerman void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2677197ba5f4SPaul Zimmerman {
2678197ba5f4SPaul Zimmerman 	int valid = 1;
2679197ba5f4SPaul Zimmerman 
2680197ba5f4SPaul Zimmerman 	if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
2681197ba5f4SPaul Zimmerman 		valid = 0;
2682197ba5f4SPaul Zimmerman 
2683197ba5f4SPaul Zimmerman 	if (!valid) {
2684197ba5f4SPaul Zimmerman 		if (val >= 0)
2685197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2686197ba5f4SPaul Zimmerman 				"%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2687197ba5f4SPaul Zimmerman 				val);
2688197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.host_rx_fifo_size;
2689197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
2690197ba5f4SPaul Zimmerman 	}
2691197ba5f4SPaul Zimmerman 
2692197ba5f4SPaul Zimmerman 	hsotg->core_params->host_rx_fifo_size = val;
2693197ba5f4SPaul Zimmerman }
2694197ba5f4SPaul Zimmerman 
2695197ba5f4SPaul Zimmerman void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2696197ba5f4SPaul Zimmerman {
2697197ba5f4SPaul Zimmerman 	int valid = 1;
2698197ba5f4SPaul Zimmerman 
2699197ba5f4SPaul Zimmerman 	if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
2700197ba5f4SPaul Zimmerman 		valid = 0;
2701197ba5f4SPaul Zimmerman 
2702197ba5f4SPaul Zimmerman 	if (!valid) {
2703197ba5f4SPaul Zimmerman 		if (val >= 0)
2704197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2705197ba5f4SPaul Zimmerman 				"%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2706197ba5f4SPaul Zimmerman 				val);
2707197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.host_nperio_tx_fifo_size;
2708197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2709197ba5f4SPaul Zimmerman 			val);
2710197ba5f4SPaul Zimmerman 	}
2711197ba5f4SPaul Zimmerman 
2712197ba5f4SPaul Zimmerman 	hsotg->core_params->host_nperio_tx_fifo_size = val;
2713197ba5f4SPaul Zimmerman }
2714197ba5f4SPaul Zimmerman 
2715197ba5f4SPaul Zimmerman void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2716197ba5f4SPaul Zimmerman {
2717197ba5f4SPaul Zimmerman 	int valid = 1;
2718197ba5f4SPaul Zimmerman 
2719197ba5f4SPaul Zimmerman 	if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
2720197ba5f4SPaul Zimmerman 		valid = 0;
2721197ba5f4SPaul Zimmerman 
2722197ba5f4SPaul Zimmerman 	if (!valid) {
2723197ba5f4SPaul Zimmerman 		if (val >= 0)
2724197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2725197ba5f4SPaul Zimmerman 				"%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2726197ba5f4SPaul Zimmerman 				val);
2727197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.host_perio_tx_fifo_size;
2728197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2729197ba5f4SPaul Zimmerman 			val);
2730197ba5f4SPaul Zimmerman 	}
2731197ba5f4SPaul Zimmerman 
2732197ba5f4SPaul Zimmerman 	hsotg->core_params->host_perio_tx_fifo_size = val;
2733197ba5f4SPaul Zimmerman }
2734197ba5f4SPaul Zimmerman 
2735197ba5f4SPaul Zimmerman void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
2736197ba5f4SPaul Zimmerman {
2737197ba5f4SPaul Zimmerman 	int valid = 1;
2738197ba5f4SPaul Zimmerman 
2739197ba5f4SPaul Zimmerman 	if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
2740197ba5f4SPaul Zimmerman 		valid = 0;
2741197ba5f4SPaul Zimmerman 
2742197ba5f4SPaul Zimmerman 	if (!valid) {
2743197ba5f4SPaul Zimmerman 		if (val >= 0)
2744197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2745197ba5f4SPaul Zimmerman 				"%d invalid for max_transfer_size. Check HW configuration.\n",
2746197ba5f4SPaul Zimmerman 				val);
2747197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.max_transfer_size;
2748197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
2749197ba5f4SPaul Zimmerman 	}
2750197ba5f4SPaul Zimmerman 
2751197ba5f4SPaul Zimmerman 	hsotg->core_params->max_transfer_size = val;
2752197ba5f4SPaul Zimmerman }
2753197ba5f4SPaul Zimmerman 
2754197ba5f4SPaul Zimmerman void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
2755197ba5f4SPaul Zimmerman {
2756197ba5f4SPaul Zimmerman 	int valid = 1;
2757197ba5f4SPaul Zimmerman 
2758197ba5f4SPaul Zimmerman 	if (val < 15 || val > hsotg->hw_params.max_packet_count)
2759197ba5f4SPaul Zimmerman 		valid = 0;
2760197ba5f4SPaul Zimmerman 
2761197ba5f4SPaul Zimmerman 	if (!valid) {
2762197ba5f4SPaul Zimmerman 		if (val >= 0)
2763197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2764197ba5f4SPaul Zimmerman 				"%d invalid for max_packet_count. Check HW configuration.\n",
2765197ba5f4SPaul Zimmerman 				val);
2766197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.max_packet_count;
2767197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
2768197ba5f4SPaul Zimmerman 	}
2769197ba5f4SPaul Zimmerman 
2770197ba5f4SPaul Zimmerman 	hsotg->core_params->max_packet_count = val;
2771197ba5f4SPaul Zimmerman }
2772197ba5f4SPaul Zimmerman 
2773197ba5f4SPaul Zimmerman void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
2774197ba5f4SPaul Zimmerman {
2775197ba5f4SPaul Zimmerman 	int valid = 1;
2776197ba5f4SPaul Zimmerman 
2777197ba5f4SPaul Zimmerman 	if (val < 1 || val > hsotg->hw_params.host_channels)
2778197ba5f4SPaul Zimmerman 		valid = 0;
2779197ba5f4SPaul Zimmerman 
2780197ba5f4SPaul Zimmerman 	if (!valid) {
2781197ba5f4SPaul Zimmerman 		if (val >= 0)
2782197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2783197ba5f4SPaul Zimmerman 				"%d invalid for host_channels. Check HW configuration.\n",
2784197ba5f4SPaul Zimmerman 				val);
2785197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.host_channels;
2786197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
2787197ba5f4SPaul Zimmerman 	}
2788197ba5f4SPaul Zimmerman 
2789197ba5f4SPaul Zimmerman 	hsotg->core_params->host_channels = val;
2790197ba5f4SPaul Zimmerman }
2791197ba5f4SPaul Zimmerman 
2792197ba5f4SPaul Zimmerman void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
2793197ba5f4SPaul Zimmerman {
2794197ba5f4SPaul Zimmerman 	int valid = 0;
2795197ba5f4SPaul Zimmerman 	u32 hs_phy_type, fs_phy_type;
2796197ba5f4SPaul Zimmerman 
2797197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
2798197ba5f4SPaul Zimmerman 			       DWC2_PHY_TYPE_PARAM_ULPI)) {
2799197ba5f4SPaul Zimmerman 		if (val >= 0) {
2800197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Wrong value for phy_type\n");
2801197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2802197ba5f4SPaul Zimmerman 		}
2803197ba5f4SPaul Zimmerman 
2804197ba5f4SPaul Zimmerman 		valid = 0;
2805197ba5f4SPaul Zimmerman 	}
2806197ba5f4SPaul Zimmerman 
2807197ba5f4SPaul Zimmerman 	hs_phy_type = hsotg->hw_params.hs_phy_type;
2808197ba5f4SPaul Zimmerman 	fs_phy_type = hsotg->hw_params.fs_phy_type;
2809197ba5f4SPaul Zimmerman 	if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2810197ba5f4SPaul Zimmerman 	    (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2811197ba5f4SPaul Zimmerman 	     hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2812197ba5f4SPaul Zimmerman 		valid = 1;
2813197ba5f4SPaul Zimmerman 	else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2814197ba5f4SPaul Zimmerman 		 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2815197ba5f4SPaul Zimmerman 		  hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2816197ba5f4SPaul Zimmerman 		valid = 1;
2817197ba5f4SPaul Zimmerman 	else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2818197ba5f4SPaul Zimmerman 		 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2819197ba5f4SPaul Zimmerman 		valid = 1;
2820197ba5f4SPaul Zimmerman 
2821197ba5f4SPaul Zimmerman 	if (!valid) {
2822197ba5f4SPaul Zimmerman 		if (val >= 0)
2823197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2824197ba5f4SPaul Zimmerman 				"%d invalid for phy_type. Check HW configuration.\n",
2825197ba5f4SPaul Zimmerman 				val);
2826197ba5f4SPaul Zimmerman 		val = DWC2_PHY_TYPE_PARAM_FS;
2827197ba5f4SPaul Zimmerman 		if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2828197ba5f4SPaul Zimmerman 			if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2829197ba5f4SPaul Zimmerman 			    hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2830197ba5f4SPaul Zimmerman 				val = DWC2_PHY_TYPE_PARAM_UTMI;
2831197ba5f4SPaul Zimmerman 			else
2832197ba5f4SPaul Zimmerman 				val = DWC2_PHY_TYPE_PARAM_ULPI;
2833197ba5f4SPaul Zimmerman 		}
2834197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
2835197ba5f4SPaul Zimmerman 	}
2836197ba5f4SPaul Zimmerman 
2837197ba5f4SPaul Zimmerman 	hsotg->core_params->phy_type = val;
2838197ba5f4SPaul Zimmerman }
2839197ba5f4SPaul Zimmerman 
2840197ba5f4SPaul Zimmerman static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2841197ba5f4SPaul Zimmerman {
2842197ba5f4SPaul Zimmerman 	return hsotg->core_params->phy_type;
2843197ba5f4SPaul Zimmerman }
2844197ba5f4SPaul Zimmerman 
2845197ba5f4SPaul Zimmerman void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
2846197ba5f4SPaul Zimmerman {
2847197ba5f4SPaul Zimmerman 	int valid = 1;
2848197ba5f4SPaul Zimmerman 
2849197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2850197ba5f4SPaul Zimmerman 		if (val >= 0) {
2851197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2852197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2853197ba5f4SPaul Zimmerman 		}
2854197ba5f4SPaul Zimmerman 		valid = 0;
2855197ba5f4SPaul Zimmerman 	}
2856197ba5f4SPaul Zimmerman 
2857197ba5f4SPaul Zimmerman 	if (val == DWC2_SPEED_PARAM_HIGH &&
2858197ba5f4SPaul Zimmerman 	    dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2859197ba5f4SPaul Zimmerman 		valid = 0;
2860197ba5f4SPaul Zimmerman 
2861197ba5f4SPaul Zimmerman 	if (!valid) {
2862197ba5f4SPaul Zimmerman 		if (val >= 0)
2863197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2864197ba5f4SPaul Zimmerman 				"%d invalid for speed parameter. Check HW configuration.\n",
2865197ba5f4SPaul Zimmerman 				val);
2866197ba5f4SPaul Zimmerman 		val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
2867197ba5f4SPaul Zimmerman 				DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
2868197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
2869197ba5f4SPaul Zimmerman 	}
2870197ba5f4SPaul Zimmerman 
2871197ba5f4SPaul Zimmerman 	hsotg->core_params->speed = val;
2872197ba5f4SPaul Zimmerman }
2873197ba5f4SPaul Zimmerman 
2874197ba5f4SPaul Zimmerman void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
2875197ba5f4SPaul Zimmerman {
2876197ba5f4SPaul Zimmerman 	int valid = 1;
2877197ba5f4SPaul Zimmerman 
2878197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2879197ba5f4SPaul Zimmerman 			       DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
2880197ba5f4SPaul Zimmerman 		if (val >= 0) {
2881197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2882197ba5f4SPaul Zimmerman 				"Wrong value for host_ls_low_power_phy_clk parameter\n");
2883197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2884197ba5f4SPaul Zimmerman 				"host_ls_low_power_phy_clk must be 0 or 1\n");
2885197ba5f4SPaul Zimmerman 		}
2886197ba5f4SPaul Zimmerman 		valid = 0;
2887197ba5f4SPaul Zimmerman 	}
2888197ba5f4SPaul Zimmerman 
2889197ba5f4SPaul Zimmerman 	if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2890197ba5f4SPaul Zimmerman 	    dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2891197ba5f4SPaul Zimmerman 		valid = 0;
2892197ba5f4SPaul Zimmerman 
2893197ba5f4SPaul Zimmerman 	if (!valid) {
2894197ba5f4SPaul Zimmerman 		if (val >= 0)
2895197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2896197ba5f4SPaul Zimmerman 				"%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2897197ba5f4SPaul Zimmerman 				val);
2898197ba5f4SPaul Zimmerman 		val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2899197ba5f4SPaul Zimmerman 			? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2900197ba5f4SPaul Zimmerman 			: DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2901197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2902197ba5f4SPaul Zimmerman 			val);
2903197ba5f4SPaul Zimmerman 	}
2904197ba5f4SPaul Zimmerman 
2905197ba5f4SPaul Zimmerman 	hsotg->core_params->host_ls_low_power_phy_clk = val;
2906197ba5f4SPaul Zimmerman }
2907197ba5f4SPaul Zimmerman 
2908197ba5f4SPaul Zimmerman void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
2909197ba5f4SPaul Zimmerman {
2910197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2911197ba5f4SPaul Zimmerman 		if (val >= 0) {
2912197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2913197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2914197ba5f4SPaul Zimmerman 		}
2915197ba5f4SPaul Zimmerman 		val = 0;
2916197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
2917197ba5f4SPaul Zimmerman 	}
2918197ba5f4SPaul Zimmerman 
2919197ba5f4SPaul Zimmerman 	hsotg->core_params->phy_ulpi_ddr = val;
2920197ba5f4SPaul Zimmerman }
2921197ba5f4SPaul Zimmerman 
2922197ba5f4SPaul Zimmerman void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
2923197ba5f4SPaul Zimmerman {
2924197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2925197ba5f4SPaul Zimmerman 		if (val >= 0) {
2926197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2927197ba5f4SPaul Zimmerman 				"Wrong value for phy_ulpi_ext_vbus\n");
2928197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2929197ba5f4SPaul Zimmerman 				"phy_ulpi_ext_vbus must be 0 or 1\n");
2930197ba5f4SPaul Zimmerman 		}
2931197ba5f4SPaul Zimmerman 		val = 0;
2932197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
2933197ba5f4SPaul Zimmerman 	}
2934197ba5f4SPaul Zimmerman 
2935197ba5f4SPaul Zimmerman 	hsotg->core_params->phy_ulpi_ext_vbus = val;
2936197ba5f4SPaul Zimmerman }
2937197ba5f4SPaul Zimmerman 
2938197ba5f4SPaul Zimmerman void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
2939197ba5f4SPaul Zimmerman {
2940197ba5f4SPaul Zimmerman 	int valid = 0;
2941197ba5f4SPaul Zimmerman 
2942197ba5f4SPaul Zimmerman 	switch (hsotg->hw_params.utmi_phy_data_width) {
2943197ba5f4SPaul Zimmerman 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
2944197ba5f4SPaul Zimmerman 		valid = (val == 8);
2945197ba5f4SPaul Zimmerman 		break;
2946197ba5f4SPaul Zimmerman 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
2947197ba5f4SPaul Zimmerman 		valid = (val == 16);
2948197ba5f4SPaul Zimmerman 		break;
2949197ba5f4SPaul Zimmerman 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
2950197ba5f4SPaul Zimmerman 		valid = (val == 8 || val == 16);
2951197ba5f4SPaul Zimmerman 		break;
2952197ba5f4SPaul Zimmerman 	}
2953197ba5f4SPaul Zimmerman 
2954197ba5f4SPaul Zimmerman 	if (!valid) {
2955197ba5f4SPaul Zimmerman 		if (val >= 0) {
2956197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
2957197ba5f4SPaul Zimmerman 				"%d invalid for phy_utmi_width. Check HW configuration.\n",
2958197ba5f4SPaul Zimmerman 				val);
2959197ba5f4SPaul Zimmerman 		}
2960197ba5f4SPaul Zimmerman 		val = (hsotg->hw_params.utmi_phy_data_width ==
2961197ba5f4SPaul Zimmerman 		       GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
2962197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
2963197ba5f4SPaul Zimmerman 	}
2964197ba5f4SPaul Zimmerman 
2965197ba5f4SPaul Zimmerman 	hsotg->core_params->phy_utmi_width = val;
2966197ba5f4SPaul Zimmerman }
2967197ba5f4SPaul Zimmerman 
2968197ba5f4SPaul Zimmerman void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
2969197ba5f4SPaul Zimmerman {
2970197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2971197ba5f4SPaul Zimmerman 		if (val >= 0) {
2972197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2973197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2974197ba5f4SPaul Zimmerman 		}
2975197ba5f4SPaul Zimmerman 		val = 0;
2976197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
2977197ba5f4SPaul Zimmerman 	}
2978197ba5f4SPaul Zimmerman 
2979197ba5f4SPaul Zimmerman 	hsotg->core_params->ulpi_fs_ls = val;
2980197ba5f4SPaul Zimmerman }
2981197ba5f4SPaul Zimmerman 
2982197ba5f4SPaul Zimmerman void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
2983197ba5f4SPaul Zimmerman {
2984197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2985197ba5f4SPaul Zimmerman 		if (val >= 0) {
2986197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2987197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2988197ba5f4SPaul Zimmerman 		}
2989197ba5f4SPaul Zimmerman 		val = 0;
2990197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
2991197ba5f4SPaul Zimmerman 	}
2992197ba5f4SPaul Zimmerman 
2993197ba5f4SPaul Zimmerman 	hsotg->core_params->ts_dline = val;
2994197ba5f4SPaul Zimmerman }
2995197ba5f4SPaul Zimmerman 
2996197ba5f4SPaul Zimmerman void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
2997197ba5f4SPaul Zimmerman {
2998197ba5f4SPaul Zimmerman 	int valid = 1;
2999197ba5f4SPaul Zimmerman 
3000197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3001197ba5f4SPaul Zimmerman 		if (val >= 0) {
3002197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
3003197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
3004197ba5f4SPaul Zimmerman 		}
3005197ba5f4SPaul Zimmerman 
3006197ba5f4SPaul Zimmerman 		valid = 0;
3007197ba5f4SPaul Zimmerman 	}
3008197ba5f4SPaul Zimmerman 
3009197ba5f4SPaul Zimmerman 	if (val == 1 && !(hsotg->hw_params.i2c_enable))
3010197ba5f4SPaul Zimmerman 		valid = 0;
3011197ba5f4SPaul Zimmerman 
3012197ba5f4SPaul Zimmerman 	if (!valid) {
3013197ba5f4SPaul Zimmerman 		if (val >= 0)
3014197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3015197ba5f4SPaul Zimmerman 				"%d invalid for i2c_enable. Check HW configuration.\n",
3016197ba5f4SPaul Zimmerman 				val);
3017197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.i2c_enable;
3018197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
3019197ba5f4SPaul Zimmerman 	}
3020197ba5f4SPaul Zimmerman 
3021197ba5f4SPaul Zimmerman 	hsotg->core_params->i2c_enable = val;
3022197ba5f4SPaul Zimmerman }
3023197ba5f4SPaul Zimmerman 
3024197ba5f4SPaul Zimmerman void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
3025197ba5f4SPaul Zimmerman {
3026197ba5f4SPaul Zimmerman 	int valid = 1;
3027197ba5f4SPaul Zimmerman 
3028197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3029197ba5f4SPaul Zimmerman 		if (val >= 0) {
3030197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3031197ba5f4SPaul Zimmerman 				"Wrong value for en_multiple_tx_fifo,\n");
3032197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3033197ba5f4SPaul Zimmerman 				"en_multiple_tx_fifo must be 0 or 1\n");
3034197ba5f4SPaul Zimmerman 		}
3035197ba5f4SPaul Zimmerman 		valid = 0;
3036197ba5f4SPaul Zimmerman 	}
3037197ba5f4SPaul Zimmerman 
3038197ba5f4SPaul Zimmerman 	if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
3039197ba5f4SPaul Zimmerman 		valid = 0;
3040197ba5f4SPaul Zimmerman 
3041197ba5f4SPaul Zimmerman 	if (!valid) {
3042197ba5f4SPaul Zimmerman 		if (val >= 0)
3043197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3044197ba5f4SPaul Zimmerman 				"%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
3045197ba5f4SPaul Zimmerman 				val);
3046197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.en_multiple_tx_fifo;
3047197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
3048197ba5f4SPaul Zimmerman 	}
3049197ba5f4SPaul Zimmerman 
3050197ba5f4SPaul Zimmerman 	hsotg->core_params->en_multiple_tx_fifo = val;
3051197ba5f4SPaul Zimmerman }
3052197ba5f4SPaul Zimmerman 
3053197ba5f4SPaul Zimmerman void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
3054197ba5f4SPaul Zimmerman {
3055197ba5f4SPaul Zimmerman 	int valid = 1;
3056197ba5f4SPaul Zimmerman 
3057197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3058197ba5f4SPaul Zimmerman 		if (val >= 0) {
3059197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3060197ba5f4SPaul Zimmerman 				"'%d' invalid for parameter reload_ctl\n", val);
3061197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
3062197ba5f4SPaul Zimmerman 		}
3063197ba5f4SPaul Zimmerman 		valid = 0;
3064197ba5f4SPaul Zimmerman 	}
3065197ba5f4SPaul Zimmerman 
3066197ba5f4SPaul Zimmerman 	if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
3067197ba5f4SPaul Zimmerman 		valid = 0;
3068197ba5f4SPaul Zimmerman 
3069197ba5f4SPaul Zimmerman 	if (!valid) {
3070197ba5f4SPaul Zimmerman 		if (val >= 0)
3071197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3072197ba5f4SPaul Zimmerman 				"%d invalid for parameter reload_ctl. Check HW configuration.\n",
3073197ba5f4SPaul Zimmerman 				val);
3074197ba5f4SPaul Zimmerman 		val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
3075197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
3076197ba5f4SPaul Zimmerman 	}
3077197ba5f4SPaul Zimmerman 
3078197ba5f4SPaul Zimmerman 	hsotg->core_params->reload_ctl = val;
3079197ba5f4SPaul Zimmerman }
3080197ba5f4SPaul Zimmerman 
3081197ba5f4SPaul Zimmerman void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
3082197ba5f4SPaul Zimmerman {
3083197ba5f4SPaul Zimmerman 	if (val != -1)
3084197ba5f4SPaul Zimmerman 		hsotg->core_params->ahbcfg = val;
3085197ba5f4SPaul Zimmerman 	else
3086197ba5f4SPaul Zimmerman 		hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
3087197ba5f4SPaul Zimmerman 						GAHBCFG_HBSTLEN_SHIFT;
3088197ba5f4SPaul Zimmerman }
3089197ba5f4SPaul Zimmerman 
3090197ba5f4SPaul Zimmerman void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
3091197ba5f4SPaul Zimmerman {
3092197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3093197ba5f4SPaul Zimmerman 		if (val >= 0) {
3094197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3095197ba5f4SPaul Zimmerman 				"'%d' invalid for parameter otg_ver\n", val);
3096197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3097197ba5f4SPaul Zimmerman 				"otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
3098197ba5f4SPaul Zimmerman 		}
3099197ba5f4SPaul Zimmerman 		val = 0;
3100197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
3101197ba5f4SPaul Zimmerman 	}
3102197ba5f4SPaul Zimmerman 
3103197ba5f4SPaul Zimmerman 	hsotg->core_params->otg_ver = val;
3104197ba5f4SPaul Zimmerman }
3105197ba5f4SPaul Zimmerman 
3106197ba5f4SPaul Zimmerman static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
3107197ba5f4SPaul Zimmerman {
3108197ba5f4SPaul Zimmerman 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3109197ba5f4SPaul Zimmerman 		if (val >= 0) {
3110197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
3111197ba5f4SPaul Zimmerman 				"'%d' invalid for parameter uframe_sched\n",
3112197ba5f4SPaul Zimmerman 				val);
3113197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
3114197ba5f4SPaul Zimmerman 		}
3115197ba5f4SPaul Zimmerman 		val = 1;
3116197ba5f4SPaul Zimmerman 		dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
3117197ba5f4SPaul Zimmerman 	}
3118197ba5f4SPaul Zimmerman 
3119197ba5f4SPaul Zimmerman 	hsotg->core_params->uframe_sched = val;
3120197ba5f4SPaul Zimmerman }
3121197ba5f4SPaul Zimmerman 
3122a6d249d8SGregory Herrero static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
3123a6d249d8SGregory Herrero 		int val)
3124a6d249d8SGregory Herrero {
3125a6d249d8SGregory Herrero 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3126a6d249d8SGregory Herrero 		if (val >= 0) {
3127a6d249d8SGregory Herrero 			dev_err(hsotg->dev,
3128a6d249d8SGregory Herrero 				"'%d' invalid for parameter external_id_pin_ctl\n",
3129a6d249d8SGregory Herrero 				val);
3130a6d249d8SGregory Herrero 			dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
3131a6d249d8SGregory Herrero 		}
3132a6d249d8SGregory Herrero 		val = 0;
3133a6d249d8SGregory Herrero 		dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
3134a6d249d8SGregory Herrero 	}
3135a6d249d8SGregory Herrero 
3136a6d249d8SGregory Herrero 	hsotg->core_params->external_id_pin_ctl = val;
3137a6d249d8SGregory Herrero }
3138a6d249d8SGregory Herrero 
3139285046aaSGregory Herrero static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
3140285046aaSGregory Herrero 		int val)
3141285046aaSGregory Herrero {
3142285046aaSGregory Herrero 	if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3143285046aaSGregory Herrero 		if (val >= 0) {
3144285046aaSGregory Herrero 			dev_err(hsotg->dev,
3145285046aaSGregory Herrero 				"'%d' invalid for parameter hibernation\n",
3146285046aaSGregory Herrero 				val);
3147285046aaSGregory Herrero 			dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
3148285046aaSGregory Herrero 		}
3149285046aaSGregory Herrero 		val = 0;
3150285046aaSGregory Herrero 		dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
3151285046aaSGregory Herrero 	}
3152285046aaSGregory Herrero 
3153285046aaSGregory Herrero 	hsotg->core_params->hibernation = val;
3154285046aaSGregory Herrero }
3155285046aaSGregory Herrero 
3156197ba5f4SPaul Zimmerman /*
3157197ba5f4SPaul Zimmerman  * This function is called during module intialization to pass module parameters
3158197ba5f4SPaul Zimmerman  * for the DWC_otg core.
3159197ba5f4SPaul Zimmerman  */
3160197ba5f4SPaul Zimmerman void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
3161197ba5f4SPaul Zimmerman 			 const struct dwc2_core_params *params)
3162197ba5f4SPaul Zimmerman {
3163197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "%s()\n", __func__);
3164197ba5f4SPaul Zimmerman 
3165197ba5f4SPaul Zimmerman 	dwc2_set_param_otg_cap(hsotg, params->otg_cap);
3166197ba5f4SPaul Zimmerman 	dwc2_set_param_dma_enable(hsotg, params->dma_enable);
3167197ba5f4SPaul Zimmerman 	dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
3168fbb9e22bSMian Yousaf Kaukab 	dwc2_set_param_dma_desc_fs_enable(hsotg, params->dma_desc_fs_enable);
3169197ba5f4SPaul Zimmerman 	dwc2_set_param_host_support_fs_ls_low_power(hsotg,
3170197ba5f4SPaul Zimmerman 			params->host_support_fs_ls_low_power);
3171197ba5f4SPaul Zimmerman 	dwc2_set_param_enable_dynamic_fifo(hsotg,
3172197ba5f4SPaul Zimmerman 			params->enable_dynamic_fifo);
3173197ba5f4SPaul Zimmerman 	dwc2_set_param_host_rx_fifo_size(hsotg,
3174197ba5f4SPaul Zimmerman 			params->host_rx_fifo_size);
3175197ba5f4SPaul Zimmerman 	dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
3176197ba5f4SPaul Zimmerman 			params->host_nperio_tx_fifo_size);
3177197ba5f4SPaul Zimmerman 	dwc2_set_param_host_perio_tx_fifo_size(hsotg,
3178197ba5f4SPaul Zimmerman 			params->host_perio_tx_fifo_size);
3179197ba5f4SPaul Zimmerman 	dwc2_set_param_max_transfer_size(hsotg,
3180197ba5f4SPaul Zimmerman 			params->max_transfer_size);
3181197ba5f4SPaul Zimmerman 	dwc2_set_param_max_packet_count(hsotg,
3182197ba5f4SPaul Zimmerman 			params->max_packet_count);
3183197ba5f4SPaul Zimmerman 	dwc2_set_param_host_channels(hsotg, params->host_channels);
3184197ba5f4SPaul Zimmerman 	dwc2_set_param_phy_type(hsotg, params->phy_type);
3185197ba5f4SPaul Zimmerman 	dwc2_set_param_speed(hsotg, params->speed);
3186197ba5f4SPaul Zimmerman 	dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
3187197ba5f4SPaul Zimmerman 			params->host_ls_low_power_phy_clk);
3188197ba5f4SPaul Zimmerman 	dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
3189197ba5f4SPaul Zimmerman 	dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
3190197ba5f4SPaul Zimmerman 			params->phy_ulpi_ext_vbus);
3191197ba5f4SPaul Zimmerman 	dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
3192197ba5f4SPaul Zimmerman 	dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
3193197ba5f4SPaul Zimmerman 	dwc2_set_param_ts_dline(hsotg, params->ts_dline);
3194197ba5f4SPaul Zimmerman 	dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
3195197ba5f4SPaul Zimmerman 	dwc2_set_param_en_multiple_tx_fifo(hsotg,
3196197ba5f4SPaul Zimmerman 			params->en_multiple_tx_fifo);
3197197ba5f4SPaul Zimmerman 	dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
3198197ba5f4SPaul Zimmerman 	dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
3199197ba5f4SPaul Zimmerman 	dwc2_set_param_otg_ver(hsotg, params->otg_ver);
3200197ba5f4SPaul Zimmerman 	dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
3201a6d249d8SGregory Herrero 	dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
3202285046aaSGregory Herrero 	dwc2_set_param_hibernation(hsotg, params->hibernation);
3203197ba5f4SPaul Zimmerman }
3204197ba5f4SPaul Zimmerman 
3205*09c96980SJohn Youn /*
3206*09c96980SJohn Youn  * Forces either host or device mode if the controller is not
3207*09c96980SJohn Youn  * currently in that mode.
3208*09c96980SJohn Youn  *
3209*09c96980SJohn Youn  * Returns true if the mode was forced.
3210*09c96980SJohn Youn  */
3211*09c96980SJohn Youn static bool dwc2_force_mode_if_needed(struct dwc2_hsotg *hsotg, bool host)
3212*09c96980SJohn Youn {
3213*09c96980SJohn Youn 	if (host && dwc2_is_host_mode(hsotg))
3214*09c96980SJohn Youn 		return false;
3215*09c96980SJohn Youn 	else if (!host && dwc2_is_device_mode(hsotg))
3216*09c96980SJohn Youn 		return false;
3217*09c96980SJohn Youn 
3218*09c96980SJohn Youn 	return dwc2_force_mode(hsotg, host);
3219*09c96980SJohn Youn }
3220*09c96980SJohn Youn 
3221197ba5f4SPaul Zimmerman /**
3222197ba5f4SPaul Zimmerman  * During device initialization, read various hardware configuration
3223197ba5f4SPaul Zimmerman  * registers and interpret the contents.
3224263b7fb5SJohn Youn  *
3225263b7fb5SJohn Youn  * This should be called during driver probe. It will perform a core
3226263b7fb5SJohn Youn  * soft reset in order to get the reset values of the parameters.
3227197ba5f4SPaul Zimmerman  */
3228197ba5f4SPaul Zimmerman int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
3229197ba5f4SPaul Zimmerman {
3230197ba5f4SPaul Zimmerman 	struct dwc2_hw_params *hw = &hsotg->hw_params;
3231197ba5f4SPaul Zimmerman 	unsigned width;
3232197ba5f4SPaul Zimmerman 	u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
3233197ba5f4SPaul Zimmerman 	u32 hptxfsiz, grxfsiz, gnptxfsiz;
3234f6194731SDouglas Anderson 	u32 gusbcfg = 0;
3235263b7fb5SJohn Youn 	int retval;
3236197ba5f4SPaul Zimmerman 
3237197ba5f4SPaul Zimmerman 	/*
3238197ba5f4SPaul Zimmerman 	 * Attempt to ensure this device is really a DWC_otg Controller.
3239197ba5f4SPaul Zimmerman 	 * Read and verify the GSNPSID register contents. The value should be
3240197ba5f4SPaul Zimmerman 	 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
3241197ba5f4SPaul Zimmerman 	 * as in "OTG version 2.xx" or "OTG version 3.xx".
3242197ba5f4SPaul Zimmerman 	 */
324395c8bc36SAntti Seppälä 	hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
3244197ba5f4SPaul Zimmerman 	if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
3245197ba5f4SPaul Zimmerman 	    (hw->snpsid & 0xfffff000) != 0x4f543000) {
3246197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
3247197ba5f4SPaul Zimmerman 			hw->snpsid);
3248197ba5f4SPaul Zimmerman 		return -ENODEV;
3249197ba5f4SPaul Zimmerman 	}
3250197ba5f4SPaul Zimmerman 
3251197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
3252197ba5f4SPaul Zimmerman 		hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
3253197ba5f4SPaul Zimmerman 		hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
3254197ba5f4SPaul Zimmerman 
3255263b7fb5SJohn Youn 	retval = dwc2_core_reset(hsotg);
3256263b7fb5SJohn Youn 	if (retval)
3257263b7fb5SJohn Youn 		return retval;
3258263b7fb5SJohn Youn 
325995c8bc36SAntti Seppälä 	hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
326095c8bc36SAntti Seppälä 	hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
326195c8bc36SAntti Seppälä 	hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
326295c8bc36SAntti Seppälä 	hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
326395c8bc36SAntti Seppälä 	grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
3264197ba5f4SPaul Zimmerman 
3265197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
3266197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
3267197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
3268197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
3269197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
3270197ba5f4SPaul Zimmerman 
32712867c05dSDoug Anderson 	/* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
3272f6194731SDouglas Anderson 	if (hsotg->dr_mode != USB_DR_MODE_HOST) {
327395c8bc36SAntti Seppälä 		gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
327499182467SDouglas Anderson 		dwc2_writel(gusbcfg | GUSBCFG_FORCEHOSTMODE,
327599182467SDouglas Anderson 			    hsotg->regs + GUSBCFG);
327620bde643SYunzhi Li 		usleep_range(25000, 50000);
327799182467SDouglas Anderson 	}
3278197ba5f4SPaul Zimmerman 
327995c8bc36SAntti Seppälä 	gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
328095c8bc36SAntti Seppälä 	hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
32812867c05dSDoug Anderson 	dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
3282197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
3283f6194731SDouglas Anderson 	if (hsotg->dr_mode != USB_DR_MODE_HOST) {
328495c8bc36SAntti Seppälä 		dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
328520bde643SYunzhi Li 		usleep_range(25000, 50000);
328699182467SDouglas Anderson 	}
3287197ba5f4SPaul Zimmerman 
3288197ba5f4SPaul Zimmerman 	/* hwcfg2 */
3289197ba5f4SPaul Zimmerman 	hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
3290197ba5f4SPaul Zimmerman 		      GHWCFG2_OP_MODE_SHIFT;
3291197ba5f4SPaul Zimmerman 	hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
3292197ba5f4SPaul Zimmerman 		   GHWCFG2_ARCHITECTURE_SHIFT;
3293197ba5f4SPaul Zimmerman 	hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
3294197ba5f4SPaul Zimmerman 	hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
3295197ba5f4SPaul Zimmerman 				GHWCFG2_NUM_HOST_CHAN_SHIFT);
3296197ba5f4SPaul Zimmerman 	hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
3297197ba5f4SPaul Zimmerman 			  GHWCFG2_HS_PHY_TYPE_SHIFT;
3298197ba5f4SPaul Zimmerman 	hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
3299197ba5f4SPaul Zimmerman 			  GHWCFG2_FS_PHY_TYPE_SHIFT;
3300197ba5f4SPaul Zimmerman 	hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
3301197ba5f4SPaul Zimmerman 			 GHWCFG2_NUM_DEV_EP_SHIFT;
3302197ba5f4SPaul Zimmerman 	hw->nperio_tx_q_depth =
3303197ba5f4SPaul Zimmerman 		(hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
3304197ba5f4SPaul Zimmerman 		GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
3305197ba5f4SPaul Zimmerman 	hw->host_perio_tx_q_depth =
3306197ba5f4SPaul Zimmerman 		(hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
3307197ba5f4SPaul Zimmerman 		GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
3308197ba5f4SPaul Zimmerman 	hw->dev_token_q_depth =
3309197ba5f4SPaul Zimmerman 		(hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
3310197ba5f4SPaul Zimmerman 		GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
3311197ba5f4SPaul Zimmerman 
3312197ba5f4SPaul Zimmerman 	/* hwcfg3 */
3313197ba5f4SPaul Zimmerman 	width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
3314197ba5f4SPaul Zimmerman 		GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
3315197ba5f4SPaul Zimmerman 	hw->max_transfer_size = (1 << (width + 11)) - 1;
3316e8f8c14dSPaul Zimmerman 	/*
3317e8f8c14dSPaul Zimmerman 	 * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
3318e8f8c14dSPaul Zimmerman 	 * coherent buffers with this size, and if it's too large we can
3319e8f8c14dSPaul Zimmerman 	 * exhaust the coherent DMA pool.
3320e8f8c14dSPaul Zimmerman 	 */
3321e8f8c14dSPaul Zimmerman 	if (hw->max_transfer_size > 65535)
3322e8f8c14dSPaul Zimmerman 		hw->max_transfer_size = 65535;
3323197ba5f4SPaul Zimmerman 	width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
3324197ba5f4SPaul Zimmerman 		GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
3325197ba5f4SPaul Zimmerman 	hw->max_packet_count = (1 << (width + 4)) - 1;
3326197ba5f4SPaul Zimmerman 	hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
3327197ba5f4SPaul Zimmerman 	hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
3328197ba5f4SPaul Zimmerman 			      GHWCFG3_DFIFO_DEPTH_SHIFT;
3329197ba5f4SPaul Zimmerman 
3330197ba5f4SPaul Zimmerman 	/* hwcfg4 */
3331197ba5f4SPaul Zimmerman 	hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
3332197ba5f4SPaul Zimmerman 	hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
3333197ba5f4SPaul Zimmerman 				  GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
3334197ba5f4SPaul Zimmerman 	hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
3335197ba5f4SPaul Zimmerman 	hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
3336197ba5f4SPaul Zimmerman 	hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
3337197ba5f4SPaul Zimmerman 				  GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
3338197ba5f4SPaul Zimmerman 
3339197ba5f4SPaul Zimmerman 	/* fifo sizes */
3340197ba5f4SPaul Zimmerman 	hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
3341197ba5f4SPaul Zimmerman 				GRXFSIZ_DEPTH_SHIFT;
3342197ba5f4SPaul Zimmerman 	hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3343197ba5f4SPaul Zimmerman 				       FIFOSIZE_DEPTH_SHIFT;
3344197ba5f4SPaul Zimmerman 	hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3345197ba5f4SPaul Zimmerman 				      FIFOSIZE_DEPTH_SHIFT;
3346197ba5f4SPaul Zimmerman 
3347197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "Detected values from hardware:\n");
3348197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  op_mode=%d\n",
3349197ba5f4SPaul Zimmerman 		hw->op_mode);
3350197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  arch=%d\n",
3351197ba5f4SPaul Zimmerman 		hw->arch);
3352197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  dma_desc_enable=%d\n",
3353197ba5f4SPaul Zimmerman 		hw->dma_desc_enable);
3354197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  power_optimized=%d\n",
3355197ba5f4SPaul Zimmerman 		hw->power_optimized);
3356197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  i2c_enable=%d\n",
3357197ba5f4SPaul Zimmerman 		hw->i2c_enable);
3358197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  hs_phy_type=%d\n",
3359197ba5f4SPaul Zimmerman 		hw->hs_phy_type);
3360197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  fs_phy_type=%d\n",
3361197ba5f4SPaul Zimmerman 		hw->fs_phy_type);
3362971bd8faSMasanari Iida 	dev_dbg(hsotg->dev, "  utmi_phy_data_width=%d\n",
3363197ba5f4SPaul Zimmerman 		hw->utmi_phy_data_width);
3364197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  num_dev_ep=%d\n",
3365197ba5f4SPaul Zimmerman 		hw->num_dev_ep);
3366197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  num_dev_perio_in_ep=%d\n",
3367197ba5f4SPaul Zimmerman 		hw->num_dev_perio_in_ep);
3368197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  host_channels=%d\n",
3369197ba5f4SPaul Zimmerman 		hw->host_channels);
3370197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  max_transfer_size=%d\n",
3371197ba5f4SPaul Zimmerman 		hw->max_transfer_size);
3372197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  max_packet_count=%d\n",
3373197ba5f4SPaul Zimmerman 		hw->max_packet_count);
3374197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  nperio_tx_q_depth=0x%0x\n",
3375197ba5f4SPaul Zimmerman 		hw->nperio_tx_q_depth);
3376197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  host_perio_tx_q_depth=0x%0x\n",
3377197ba5f4SPaul Zimmerman 		hw->host_perio_tx_q_depth);
3378197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  dev_token_q_depth=0x%0x\n",
3379197ba5f4SPaul Zimmerman 		hw->dev_token_q_depth);
3380197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  enable_dynamic_fifo=%d\n",
3381197ba5f4SPaul Zimmerman 		hw->enable_dynamic_fifo);
3382197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  en_multiple_tx_fifo=%d\n",
3383197ba5f4SPaul Zimmerman 		hw->en_multiple_tx_fifo);
3384197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  total_fifo_size=%d\n",
3385197ba5f4SPaul Zimmerman 		hw->total_fifo_size);
3386197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  host_rx_fifo_size=%d\n",
3387197ba5f4SPaul Zimmerman 		hw->host_rx_fifo_size);
3388197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  host_nperio_tx_fifo_size=%d\n",
3389197ba5f4SPaul Zimmerman 		hw->host_nperio_tx_fifo_size);
3390197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "  host_perio_tx_fifo_size=%d\n",
3391197ba5f4SPaul Zimmerman 		hw->host_perio_tx_fifo_size);
3392197ba5f4SPaul Zimmerman 	dev_dbg(hsotg->dev, "\n");
3393197ba5f4SPaul Zimmerman 
3394197ba5f4SPaul Zimmerman 	return 0;
3395197ba5f4SPaul Zimmerman }
3396ecb176c6SMian Yousaf Kaukab 
3397ecb176c6SMian Yousaf Kaukab /*
3398ecb176c6SMian Yousaf Kaukab  * Sets all parameters to the given value.
3399ecb176c6SMian Yousaf Kaukab  *
3400ecb176c6SMian Yousaf Kaukab  * Assumes that the dwc2_core_params struct contains only integers.
3401ecb176c6SMian Yousaf Kaukab  */
3402ecb176c6SMian Yousaf Kaukab void dwc2_set_all_params(struct dwc2_core_params *params, int value)
3403ecb176c6SMian Yousaf Kaukab {
3404ecb176c6SMian Yousaf Kaukab 	int *p = (int *)params;
3405ecb176c6SMian Yousaf Kaukab 	size_t size = sizeof(*params) / sizeof(*p);
3406ecb176c6SMian Yousaf Kaukab 	int i;
3407ecb176c6SMian Yousaf Kaukab 
3408ecb176c6SMian Yousaf Kaukab 	for (i = 0; i < size; i++)
3409ecb176c6SMian Yousaf Kaukab 		p[i] = value;
3410ecb176c6SMian Yousaf Kaukab }
3411ecb176c6SMian Yousaf Kaukab 
3412197ba5f4SPaul Zimmerman 
3413197ba5f4SPaul Zimmerman u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
3414197ba5f4SPaul Zimmerman {
3415197ba5f4SPaul Zimmerman 	return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
3416197ba5f4SPaul Zimmerman }
3417197ba5f4SPaul Zimmerman 
3418197ba5f4SPaul Zimmerman bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
3419197ba5f4SPaul Zimmerman {
342095c8bc36SAntti Seppälä 	if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
3421197ba5f4SPaul Zimmerman 		return false;
3422197ba5f4SPaul Zimmerman 	else
3423197ba5f4SPaul Zimmerman 		return true;
3424197ba5f4SPaul Zimmerman }
3425197ba5f4SPaul Zimmerman 
3426197ba5f4SPaul Zimmerman /**
3427197ba5f4SPaul Zimmerman  * dwc2_enable_global_interrupts() - Enables the controller's Global
3428197ba5f4SPaul Zimmerman  * Interrupt in the AHB Config register
3429197ba5f4SPaul Zimmerman  *
3430197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
3431197ba5f4SPaul Zimmerman  */
3432197ba5f4SPaul Zimmerman void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
3433197ba5f4SPaul Zimmerman {
343495c8bc36SAntti Seppälä 	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
3435197ba5f4SPaul Zimmerman 
3436197ba5f4SPaul Zimmerman 	ahbcfg |= GAHBCFG_GLBL_INTR_EN;
343795c8bc36SAntti Seppälä 	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
3438197ba5f4SPaul Zimmerman }
3439197ba5f4SPaul Zimmerman 
3440197ba5f4SPaul Zimmerman /**
3441197ba5f4SPaul Zimmerman  * dwc2_disable_global_interrupts() - Disables the controller's Global
3442197ba5f4SPaul Zimmerman  * Interrupt in the AHB Config register
3443197ba5f4SPaul Zimmerman  *
3444197ba5f4SPaul Zimmerman  * @hsotg: Programming view of DWC_otg controller
3445197ba5f4SPaul Zimmerman  */
3446197ba5f4SPaul Zimmerman void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
3447197ba5f4SPaul Zimmerman {
344895c8bc36SAntti Seppälä 	u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
3449197ba5f4SPaul Zimmerman 
3450197ba5f4SPaul Zimmerman 	ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
345195c8bc36SAntti Seppälä 	dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
3452197ba5f4SPaul Zimmerman }
3453197ba5f4SPaul Zimmerman 
34546bea9620SJohn Youn /* Returns the controller's GHWCFG2.OTG_MODE. */
34556bea9620SJohn Youn unsigned dwc2_op_mode(struct dwc2_hsotg *hsotg)
34566bea9620SJohn Youn {
34576bea9620SJohn Youn 	u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
34586bea9620SJohn Youn 
34596bea9620SJohn Youn 	return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
34606bea9620SJohn Youn 		GHWCFG2_OP_MODE_SHIFT;
34616bea9620SJohn Youn }
34626bea9620SJohn Youn 
34636bea9620SJohn Youn /* Returns true if the controller is capable of DRD. */
34646bea9620SJohn Youn bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
34656bea9620SJohn Youn {
34666bea9620SJohn Youn 	unsigned op_mode = dwc2_op_mode(hsotg);
34676bea9620SJohn Youn 
34686bea9620SJohn Youn 	return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
34696bea9620SJohn Youn 		(op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
34706bea9620SJohn Youn 		(op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
34716bea9620SJohn Youn }
34726bea9620SJohn Youn 
34736bea9620SJohn Youn /* Returns true if the controller is host-only. */
34746bea9620SJohn Youn bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
34756bea9620SJohn Youn {
34766bea9620SJohn Youn 	unsigned op_mode = dwc2_op_mode(hsotg);
34776bea9620SJohn Youn 
34786bea9620SJohn Youn 	return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
34796bea9620SJohn Youn 		(op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
34806bea9620SJohn Youn }
34816bea9620SJohn Youn 
34826bea9620SJohn Youn /* Returns true if the controller is device-only. */
34836bea9620SJohn Youn bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
34846bea9620SJohn Youn {
34856bea9620SJohn Youn 	unsigned op_mode = dwc2_op_mode(hsotg);
34866bea9620SJohn Youn 
34876bea9620SJohn Youn 	return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
34886bea9620SJohn Youn 		(op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
34896bea9620SJohn Youn }
34906bea9620SJohn Youn 
3491197ba5f4SPaul Zimmerman MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
3492197ba5f4SPaul Zimmerman MODULE_AUTHOR("Synopsys, Inc.");
3493197ba5f4SPaul Zimmerman MODULE_LICENSE("Dual BSD/GPL");
3494