blob: 9659dbd33c8c527831a1e93301160cc0d254f5fb [file] [log] [blame]
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001/*
2 * core.c - DesignWare HS OTG Controller common routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * The Core code provides basic services for accessing and managing the
39 * DWC_otg hardware. These services are used by both the Host Controller
40 * Driver and the Peripheral Controller Driver.
41 */
42#include <linux/kernel.h>
43#include <linux/module.h>
44#include <linux/moduleparam.h>
45#include <linux/spinlock.h>
46#include <linux/interrupt.h>
47#include <linux/dma-mapping.h>
48#include <linux/delay.h>
49#include <linux/io.h>
50#include <linux/slab.h>
51#include <linux/usb.h>
52
53#include <linux/usb/hcd.h>
54#include <linux/usb/ch11.h>
55
56#include "core.h"
57#include "hcd.h"
58
Gregory Herrerod17ee772015-04-29 22:09:01 +020059#if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
60/**
61 * dwc2_backup_host_registers() - Backup controller host registers.
62 * When suspending usb bus, registers needs to be backuped
63 * if controller power is disabled once suspended.
64 *
65 * @hsotg: Programming view of the DWC_otg controller
66 */
67static int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
68{
69 struct dwc2_hregs_backup *hr;
70 int i;
71
72 dev_dbg(hsotg->dev, "%s\n", __func__);
73
74 /* Backup Host regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +020075 hr = &hsotg->hr_backup;
Antti Seppälä95c8bc32015-08-20 21:41:07 +030076 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
77 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +020078 for (i = 0; i < hsotg->core_params->host_channels; ++i)
Antti Seppälä95c8bc32015-08-20 21:41:07 +030079 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +020080
Gregory Herrerocc047ce2015-09-22 15:16:37 +020081 hr->hprt0 = dwc2_read_hprt0(hsotg);
Antti Seppälä95c8bc32015-08-20 21:41:07 +030082 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +020083 hr->valid = true;
Gregory Herrerod17ee772015-04-29 22:09:01 +020084
85 return 0;
86}
87
88/**
89 * dwc2_restore_host_registers() - Restore controller host registers.
90 * When resuming usb bus, device registers needs to be restored
91 * if controller power were disabled.
92 *
93 * @hsotg: Programming view of the DWC_otg controller
94 */
95static int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
96{
97 struct dwc2_hregs_backup *hr;
98 int i;
99
100 dev_dbg(hsotg->dev, "%s\n", __func__);
101
102 /* Restore host regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200103 hr = &hsotg->hr_backup;
104 if (!hr->valid) {
Gregory Herrerod17ee772015-04-29 22:09:01 +0200105 dev_err(hsotg->dev, "%s: no host registers to restore\n",
106 __func__);
107 return -EINVAL;
108 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200109 hr->valid = false;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200110
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300111 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
112 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200113
114 for (i = 0; i < hsotg->core_params->host_channels; ++i)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300115 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200116
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300117 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
118 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
Gregory Herrero08c4ffc2015-09-22 15:16:45 +0200119 hsotg->frame_number = 0;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200120
121 return 0;
122}
123#else
124static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
125{ return 0; }
126
127static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
128{ return 0; }
129#endif
130
131#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
132 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
133/**
134 * dwc2_backup_device_registers() - Backup controller device registers.
135 * When suspending usb bus, registers needs to be backuped
136 * if controller power is disabled once suspended.
137 *
138 * @hsotg: Programming view of the DWC_otg controller
139 */
140static int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
141{
142 struct dwc2_dregs_backup *dr;
143 int i;
144
145 dev_dbg(hsotg->dev, "%s\n", __func__);
146
147 /* Backup dev regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200148 dr = &hsotg->dr_backup;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200149
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300150 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
151 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
152 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
153 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
154 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200155
156 for (i = 0; i < hsotg->num_of_eps; i++) {
157 /* Backup IN EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300158 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200159
160 /* Ensure DATA PID is correctly configured */
161 if (dr->diepctl[i] & DXEPCTL_DPID)
162 dr->diepctl[i] |= DXEPCTL_SETD1PID;
163 else
164 dr->diepctl[i] |= DXEPCTL_SETD0PID;
165
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300166 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
167 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200168
169 /* Backup OUT EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300170 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200171
172 /* Ensure DATA PID is correctly configured */
173 if (dr->doepctl[i] & DXEPCTL_DPID)
174 dr->doepctl[i] |= DXEPCTL_SETD1PID;
175 else
176 dr->doepctl[i] |= DXEPCTL_SETD0PID;
177
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300178 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
179 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200180 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200181 dr->valid = true;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200182 return 0;
183}
184
185/**
186 * dwc2_restore_device_registers() - Restore controller device registers.
187 * When resuming usb bus, device registers needs to be restored
188 * if controller power were disabled.
189 *
190 * @hsotg: Programming view of the DWC_otg controller
191 */
192static int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
193{
194 struct dwc2_dregs_backup *dr;
195 u32 dctl;
196 int i;
197
198 dev_dbg(hsotg->dev, "%s\n", __func__);
199
200 /* Restore dev regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200201 dr = &hsotg->dr_backup;
202 if (!dr->valid) {
Gregory Herrerod17ee772015-04-29 22:09:01 +0200203 dev_err(hsotg->dev, "%s: no device registers to restore\n",
204 __func__);
205 return -EINVAL;
206 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200207 dr->valid = false;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200208
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300209 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
210 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
211 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
212 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
213 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200214
215 for (i = 0; i < hsotg->num_of_eps; i++) {
216 /* Restore IN EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300217 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
218 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
219 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200220
221 /* Restore OUT EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300222 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
223 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
224 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200225 }
226
227 /* Set the Power-On Programming done bit */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300228 dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200229 dctl |= DCTL_PWRONPRGDONE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300230 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200231
232 return 0;
233}
234#else
235static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
236{ return 0; }
237
238static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
239{ return 0; }
240#endif
241
242/**
243 * dwc2_backup_global_registers() - Backup global controller registers.
244 * When suspending usb bus, registers needs to be backuped
245 * if controller power is disabled once suspended.
246 *
247 * @hsotg: Programming view of the DWC_otg controller
248 */
249static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
250{
251 struct dwc2_gregs_backup *gr;
252 int i;
253
254 /* Backup global regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200255 gr = &hsotg->gr_backup;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200256
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300257 gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
258 gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
259 gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
260 gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
261 gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
262 gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
263 gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
264 gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200265 for (i = 0; i < MAX_EPS_CHANNELS; i++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300266 gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200267
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200268 gr->valid = true;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200269 return 0;
270}
271
272/**
273 * dwc2_restore_global_registers() - Restore controller global registers.
274 * When resuming usb bus, device registers needs to be restored
275 * if controller power were disabled.
276 *
277 * @hsotg: Programming view of the DWC_otg controller
278 */
279static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
280{
281 struct dwc2_gregs_backup *gr;
282 int i;
283
284 dev_dbg(hsotg->dev, "%s\n", __func__);
285
286 /* Restore global regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200287 gr = &hsotg->gr_backup;
288 if (!gr->valid) {
Gregory Herrerod17ee772015-04-29 22:09:01 +0200289 dev_err(hsotg->dev, "%s: no global registers to restore\n",
290 __func__);
291 return -EINVAL;
292 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200293 gr->valid = false;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200294
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300295 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
296 dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
297 dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
298 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
299 dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
300 dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
301 dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
302 dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
303 dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200304 for (i = 0; i < MAX_EPS_CHANNELS; i++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300305 dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200306
307 return 0;
308}
309
310/**
311 * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
312 *
313 * @hsotg: Programming view of the DWC_otg controller
314 * @restore: Controller registers need to be restored
315 */
316int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
317{
318 u32 pcgcctl;
319 int ret = 0;
320
Gregory Herrero285046a2015-04-29 22:09:19 +0200321 if (!hsotg->core_params->hibernation)
322 return -ENOTSUPP;
323
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300324 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200325 pcgcctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300326 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200327
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300328 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200329 pcgcctl &= ~PCGCTL_PWRCLMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300330 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200331
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300332 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200333 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300334 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200335
336 udelay(100);
337 if (restore) {
338 ret = dwc2_restore_global_registers(hsotg);
339 if (ret) {
340 dev_err(hsotg->dev, "%s: failed to restore registers\n",
341 __func__);
342 return ret;
343 }
344 if (dwc2_is_host_mode(hsotg)) {
345 ret = dwc2_restore_host_registers(hsotg);
346 if (ret) {
347 dev_err(hsotg->dev, "%s: failed to restore host registers\n",
348 __func__);
349 return ret;
350 }
351 } else {
352 ret = dwc2_restore_device_registers(hsotg);
353 if (ret) {
354 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
355 __func__);
356 return ret;
357 }
358 }
359 }
360
361 return ret;
362}
363
364/**
365 * dwc2_enter_hibernation() - Put controller in Partial Power Down.
366 *
367 * @hsotg: Programming view of the DWC_otg controller
368 */
369int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
370{
371 u32 pcgcctl;
372 int ret = 0;
373
Gregory Herrero285046a2015-04-29 22:09:19 +0200374 if (!hsotg->core_params->hibernation)
375 return -ENOTSUPP;
376
Gregory Herrerod17ee772015-04-29 22:09:01 +0200377 /* Backup all registers */
378 ret = dwc2_backup_global_registers(hsotg);
379 if (ret) {
380 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
381 __func__);
382 return ret;
383 }
384
385 if (dwc2_is_host_mode(hsotg)) {
386 ret = dwc2_backup_host_registers(hsotg);
387 if (ret) {
388 dev_err(hsotg->dev, "%s: failed to backup host registers\n",
389 __func__);
390 return ret;
391 }
392 } else {
393 ret = dwc2_backup_device_registers(hsotg);
394 if (ret) {
395 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
396 __func__);
397 return ret;
398 }
399 }
400
Gregory Herrerocad73da2015-09-22 15:16:49 +0200401 /*
402 * Clear any pending interrupts since dwc2 will not be able to
403 * clear them after entering hibernation.
404 */
405 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
406
Gregory Herrerod17ee772015-04-29 22:09:01 +0200407 /* Put the controller in low power state */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300408 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200409
410 pcgcctl |= PCGCTL_PWRCLMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300411 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200412 ndelay(20);
413
414 pcgcctl |= PCGCTL_RSTPDWNMODULE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300415 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200416 ndelay(20);
417
418 pcgcctl |= PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300419 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200420
421 return ret;
422}
423
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700424/**
425 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
426 * used in both device and host modes
427 *
428 * @hsotg: Programming view of the DWC_otg controller
429 */
430static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
431{
432 u32 intmsk;
433
434 /* Clear any pending OTG Interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300435 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700436
437 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300438 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700439
440 /* Enable the interrupts in the GINTMSK */
441 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
442
443 if (hsotg->core_params->dma_enable <= 0)
444 intmsk |= GINTSTS_RXFLVL;
Gregory Herreroa6d249d2015-04-29 22:09:04 +0200445 if (hsotg->core_params->external_id_pin_ctl <= 0)
446 intmsk |= GINTSTS_CONIDSTSCHNG;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700447
Gregory Herreroa6d249d2015-04-29 22:09:04 +0200448 intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700449 GINTSTS_SESSREQINT;
450
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300451 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700452}
453
454/*
455 * Initializes the FSLSPClkSel field of the HCFG register depending on the
456 * PHY type
457 */
458static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
459{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700460 u32 hcfg, val;
461
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200462 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
463 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700464 hsotg->core_params->ulpi_fs_ls > 0) ||
465 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
466 /* Full speed PHY */
467 val = HCFG_FSLSPCLKSEL_48_MHZ;
468 } else {
469 /* High speed PHY running at full speed or high speed */
470 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
471 }
472
473 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300474 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700475 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200476 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300477 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700478}
479
480/*
481 * Do core a soft reset of the core. Be careful with this because it
482 * resets all the internal state machines of the core.
483 */
Yunzhi Licebfdbf2015-12-17 11:14:26 -0800484int dwc2_core_reset(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700485{
486 u32 greset;
487 int count = 0;
Kever Yangc0155b92014-08-06 09:01:50 +0800488 u32 gusbcfg;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700489
490 dev_vdbg(hsotg->dev, "%s()\n", __func__);
491
492 /* Wait for AHB master IDLE state */
493 do {
Yunzhi Li20bde642015-12-17 11:15:08 -0800494 udelay(1);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300495 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700496 if (++count > 50) {
497 dev_warn(hsotg->dev,
498 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
499 __func__, greset);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100500 return -EBUSY;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700501 }
502 } while (!(greset & GRSTCTL_AHBIDLE));
503
504 /* Core Soft Reset */
505 count = 0;
506 greset |= GRSTCTL_CSFTRST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300507 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700508 do {
Yunzhi Li20bde642015-12-17 11:15:08 -0800509 udelay(1);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300510 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700511 if (++count > 50) {
512 dev_warn(hsotg->dev,
513 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
514 __func__, greset);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100515 return -EBUSY;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700516 }
517 } while (greset & GRSTCTL_CSFTRST);
518
Kever Yangc0155b92014-08-06 09:01:50 +0800519 if (hsotg->dr_mode == USB_DR_MODE_HOST) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300520 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800521 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
522 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300523 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800524 } else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300525 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800526 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
527 gusbcfg |= GUSBCFG_FORCEDEVMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300528 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800529 } else if (hsotg->dr_mode == USB_DR_MODE_OTG) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300530 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800531 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
532 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300533 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800534 }
535
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700536 /*
537 * NOTE: This long sleep is _very_ important, otherwise the core will
538 * not stay in host mode after a connector ID change!
539 */
Yunzhi Li20bde642015-12-17 11:15:08 -0800540 usleep_range(150000, 160000);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100541
542 return 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700543}
544
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100545static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700546{
547 u32 usbcfg, i2cctl;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100548 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700549
550 /*
551 * core_init() is now called on every switch so only call the
552 * following for the first time through
553 */
554 if (select_phy) {
555 dev_dbg(hsotg->dev, "FS PHY selected\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300556 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700557 usbcfg |= GUSBCFG_PHYSEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300558 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700559
560 /* Reset after a PHY select */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100561 retval = dwc2_core_reset(hsotg);
562 if (retval) {
563 dev_err(hsotg->dev, "%s() Reset failed, aborting",
564 __func__);
565 return retval;
566 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700567 }
568
569 /*
570 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
571 * do this on HNP Dev/Host mode switches (done in dev_init and
572 * host_init).
573 */
574 if (dwc2_is_host_mode(hsotg))
575 dwc2_init_fs_ls_pclk_sel(hsotg);
576
577 if (hsotg->core_params->i2c_enable > 0) {
578 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
579
580 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300581 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700582 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300583 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700584
585 /* Program GI2CCTL.I2CEn */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300586 i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700587 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
588 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
589 i2cctl &= ~GI2CCTL_I2CEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300590 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700591 i2cctl |= GI2CCTL_I2CEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300592 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700593 }
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100594
595 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700596}
597
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100598static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700599{
600 u32 usbcfg;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100601 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700602
603 if (!select_phy)
Paul Zimmermana23666c2014-02-04 11:42:15 -0800604 return 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700605
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300606 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700607
608 /*
609 * HS PHY parameters. These parameters are preserved during soft reset
610 * so only program the first time. Do a soft reset immediately after
611 * setting phyif.
612 */
613 switch (hsotg->core_params->phy_type) {
614 case DWC2_PHY_TYPE_PARAM_ULPI:
615 /* ULPI interface */
616 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
617 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
618 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
619 if (hsotg->core_params->phy_ulpi_ddr > 0)
620 usbcfg |= GUSBCFG_DDRSEL;
621 break;
622 case DWC2_PHY_TYPE_PARAM_UTMI:
623 /* UTMI+ interface */
624 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
625 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
626 if (hsotg->core_params->phy_utmi_width == 16)
627 usbcfg |= GUSBCFG_PHYIF16;
628 break;
629 default:
630 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
631 break;
632 }
633
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300634 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700635
636 /* Reset after setting the PHY parameters */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100637 retval = dwc2_core_reset(hsotg);
638 if (retval) {
639 dev_err(hsotg->dev, "%s() Reset failed, aborting",
640 __func__);
641 return retval;
642 }
643
644 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700645}
646
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100647static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700648{
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200649 u32 usbcfg;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100650 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700651
652 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
653 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
654 /* If FS mode with FS PHY */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100655 retval = dwc2_fs_phy_init(hsotg, select_phy);
656 if (retval)
657 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700658 } else {
659 /* High speed PHY */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100660 retval = dwc2_hs_phy_init(hsotg, select_phy);
661 if (retval)
662 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700663 }
664
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200665 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
666 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700667 hsotg->core_params->ulpi_fs_ls > 0) {
668 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300669 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700670 usbcfg |= GUSBCFG_ULPI_FS_LS;
671 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300672 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700673 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300674 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700675 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
676 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300677 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700678 }
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100679
680 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700681}
682
683static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
684{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300685 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700686
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200687 switch (hsotg->hw_params.arch) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700688 case GHWCFG2_EXT_DMA_ARCH:
689 dev_err(hsotg->dev, "External DMA Mode not supported\n");
690 return -EINVAL;
691
692 case GHWCFG2_INT_DMA_ARCH:
693 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
Paul Zimmerman4d3190e2013-07-16 12:22:12 -0700694 if (hsotg->core_params->ahbcfg != -1) {
695 ahbcfg &= GAHBCFG_CTRL_MASK;
696 ahbcfg |= hsotg->core_params->ahbcfg &
697 ~GAHBCFG_CTRL_MASK;
698 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700699 break;
700
701 case GHWCFG2_SLAVE_ONLY_ARCH:
702 default:
703 dev_dbg(hsotg->dev, "Slave Only Mode\n");
704 break;
705 }
706
707 dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
708 hsotg->core_params->dma_enable,
709 hsotg->core_params->dma_desc_enable);
710
711 if (hsotg->core_params->dma_enable > 0) {
712 if (hsotg->core_params->dma_desc_enable > 0)
713 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
714 else
715 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
716 } else {
717 dev_dbg(hsotg->dev, "Using Slave mode\n");
718 hsotg->core_params->dma_desc_enable = 0;
719 }
720
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700721 if (hsotg->core_params->dma_enable > 0)
722 ahbcfg |= GAHBCFG_DMA_EN;
723
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300724 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700725
726 return 0;
727}
728
729static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
730{
731 u32 usbcfg;
732
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300733 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700734 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
735
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200736 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700737 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
738 if (hsotg->core_params->otg_cap ==
739 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
740 usbcfg |= GUSBCFG_HNPCAP;
741 if (hsotg->core_params->otg_cap !=
742 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
743 usbcfg |= GUSBCFG_SRPCAP;
744 break;
745
746 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
747 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
748 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
749 if (hsotg->core_params->otg_cap !=
750 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
751 usbcfg |= GUSBCFG_SRPCAP;
752 break;
753
754 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
755 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
756 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
757 default:
758 break;
759 }
760
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300761 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700762}
763
764/**
765 * dwc2_core_init() - Initializes the DWC_otg controller registers and
766 * prepares the core for device mode or host mode operation
767 *
Douglas Anderson0fe239b2015-12-17 11:14:40 -0800768 * @hsotg: Programming view of the DWC_otg controller
769 * @initial_setup: If true then this is the first init for this instance.
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700770 */
Douglas Anderson0fe239b2015-12-17 11:14:40 -0800771int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700772{
773 u32 usbcfg, otgctl;
774 int retval;
775
776 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
777
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300778 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700779
780 /* Set ULPI External VBUS bit if needed */
781 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
782 if (hsotg->core_params->phy_ulpi_ext_vbus ==
783 DWC2_PHY_ULPI_EXTERNAL_VBUS)
784 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
785
786 /* Set external TS Dline pulsing bit if needed */
787 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
788 if (hsotg->core_params->ts_dline > 0)
789 usbcfg |= GUSBCFG_TERMSELDLPULSE;
790
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300791 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700792
Douglas Anderson0fe239b2015-12-17 11:14:40 -0800793 /*
794 * Reset the Controller
795 *
796 * We only need to reset the controller if this is a re-init.
797 * For the first init we know for sure that earlier code reset us (it
798 * needed to in order to properly detect various parameters).
799 */
800 if (!initial_setup) {
801 retval = dwc2_core_reset(hsotg);
802 if (retval) {
803 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
804 __func__);
805 return retval;
806 }
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100807 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700808
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700809 /*
810 * This needs to happen in FS mode before any other programming occurs
811 */
Douglas Anderson0fe239b2015-12-17 11:14:40 -0800812 retval = dwc2_phy_init(hsotg, initial_setup);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100813 if (retval)
814 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700815
816 /* Program the GAHBCFG Register */
817 retval = dwc2_gahbcfg_init(hsotg);
818 if (retval)
819 return retval;
820
821 /* Program the GUSBCFG register */
822 dwc2_gusbcfg_init(hsotg);
823
824 /* Program the GOTGCTL register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300825 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700826 otgctl &= ~GOTGCTL_OTGVER;
827 if (hsotg->core_params->otg_ver > 0)
828 otgctl |= GOTGCTL_OTGVER;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300829 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700830 dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
831
832 /* Clear the SRP success bit for FS-I2c */
833 hsotg->srp_success = 0;
834
835 /* Enable common interrupts */
836 dwc2_enable_common_interrupts(hsotg);
837
838 /*
Mickael Maison997f4f82014-12-23 17:39:45 +0100839 * Do device or host initialization based on mode during PCD and
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700840 * HCD initialization
841 */
842 if (dwc2_is_host_mode(hsotg)) {
843 dev_dbg(hsotg->dev, "Host Mode\n");
844 hsotg->op_state = OTG_STATE_A_HOST;
845 } else {
846 dev_dbg(hsotg->dev, "Device Mode\n");
847 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
848 }
849
850 return 0;
851}
852
853/**
854 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
855 *
856 * @hsotg: Programming view of DWC_otg controller
857 */
858void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
859{
860 u32 intmsk;
861
862 dev_dbg(hsotg->dev, "%s()\n", __func__);
863
864 /* Disable all interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300865 dwc2_writel(0, hsotg->regs + GINTMSK);
866 dwc2_writel(0, hsotg->regs + HAINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700867
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700868 /* Enable the common interrupts */
869 dwc2_enable_common_interrupts(hsotg);
870
871 /* Enable host mode interrupts without disturbing common interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300872 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Mian Yousaf Kaukab44e4a602015-10-12 11:23:27 +0200873 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300874 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700875}
876
877/**
878 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
879 *
880 * @hsotg: Programming view of DWC_otg controller
881 */
882void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
883{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300884 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700885
886 /* Disable host mode interrupts without disturbing common interrupts */
887 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
Mian Yousaf Kaukab77dbf712015-09-22 15:16:47 +0200888 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300889 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700890}
891
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500892/*
893 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
894 * For system that have a total fifo depth that is smaller than the default
895 * RX + TX fifo size.
896 *
897 * @hsotg: Programming view of DWC_otg controller
898 */
899static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
900{
901 struct dwc2_core_params *params = hsotg->core_params;
902 struct dwc2_hw_params *hw = &hsotg->hw_params;
903 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
904
905 total_fifo_size = hw->total_fifo_size;
906 rxfsiz = params->host_rx_fifo_size;
907 nptxfsiz = params->host_nperio_tx_fifo_size;
908 ptxfsiz = params->host_perio_tx_fifo_size;
909
910 /*
911 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
912 * allocation with support for high bandwidth endpoints. Synopsys
913 * defines MPS(Max Packet size) for a periodic EP=1024, and for
914 * non-periodic as 512.
915 */
916 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
917 /*
918 * For Buffer DMA mode/Scatter Gather DMA mode
919 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
920 * with n = number of host channel.
921 * 2 * ((1024/4) + 2) = 516
922 */
923 rxfsiz = 516 + hw->host_channels;
924
925 /*
926 * min non-periodic tx fifo depth
927 * 2 * (largest non-periodic USB packet used / 4)
928 * 2 * (512/4) = 256
929 */
930 nptxfsiz = 256;
931
932 /*
933 * min periodic tx fifo depth
934 * (largest packet size*MC)/4
935 * (1024 * 3)/4 = 768
936 */
937 ptxfsiz = 768;
938
939 params->host_rx_fifo_size = rxfsiz;
940 params->host_nperio_tx_fifo_size = nptxfsiz;
941 params->host_perio_tx_fifo_size = ptxfsiz;
942 }
943
944 /*
945 * If the summation of RX, NPTX and PTX fifo sizes is still
946 * bigger than the total_fifo_size, then we have a problem.
947 *
948 * We won't be able to allocate as many endpoints. Right now,
949 * we're just printing an error message, but ideally this FIFO
950 * allocation algorithm would be improved in the future.
951 *
952 * FIXME improve this FIFO allocation algorithm.
953 */
954 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
955 dev_err(hsotg->dev, "invalid fifo sizes\n");
956}
957
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700958static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
959{
960 struct dwc2_core_params *params = hsotg->core_params;
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200961 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700962
Matthijs Kooijman12086052013-04-29 19:46:35 +0000963 if (!params->enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700964 return;
965
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500966 dwc2_calculate_dynamic_fifo(hsotg);
967
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700968 /* Rx FIFO */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300969 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200970 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
971 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
972 grxfsiz |= params->host_rx_fifo_size <<
973 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300974 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
975 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
976 dwc2_readl(hsotg->regs + GRXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700977
978 /* Non-periodic Tx FIFO */
979 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300980 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700981 nptxfsiz = params->host_nperio_tx_fifo_size <<
982 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
983 nptxfsiz |= params->host_rx_fifo_size <<
984 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300985 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700986 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300987 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700988
989 /* Periodic Tx FIFO */
990 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300991 dwc2_readl(hsotg->regs + HPTXFSIZ));
Matthijs Kooijmanc35205a2013-08-30 18:45:18 +0200992 hptxfsiz = params->host_perio_tx_fifo_size <<
993 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
994 hptxfsiz |= (params->host_rx_fifo_size +
995 params->host_nperio_tx_fifo_size) <<
996 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300997 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700998 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300999 dwc2_readl(hsotg->regs + HPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001000
1001 if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001002 hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001003 /*
1004 * Global DFIFOCFG calculation for Host mode -
1005 * include RxFIFO, NPTXFIFO and HPTXFIFO
1006 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001007 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001008 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
Matthijs Kooijman08b9f9d2013-08-30 18:45:19 +02001009 dfifocfg |= (params->host_rx_fifo_size +
1010 params->host_nperio_tx_fifo_size +
1011 params->host_perio_tx_fifo_size) <<
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001012 GDFIFOCFG_EPINFOBASE_SHIFT &
1013 GDFIFOCFG_EPINFOBASE_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001014 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001015 }
1016}
1017
1018/**
1019 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
1020 * Host mode
1021 *
1022 * @hsotg: Programming view of DWC_otg controller
1023 *
1024 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
1025 * request queues. Host channels are reset to ensure that they are ready for
1026 * performing transfers.
1027 */
1028void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
1029{
1030 u32 hcfg, hfir, otgctl;
1031
1032 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
1033
1034 /* Restart the Phy Clock */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001035 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001036
1037 /* Initialize Host Configuration Register */
1038 dwc2_init_fs_ls_pclk_sel(hsotg);
1039 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001040 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001041 hcfg |= HCFG_FSLSSUPP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001042 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001043 }
1044
1045 /*
1046 * This bit allows dynamic reloading of the HFIR register during
Masanari Iida0dcde5082013-09-13 23:34:36 +09001047 * runtime. This bit needs to be programmed during initial configuration
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001048 * and its value must not be changed during runtime.
1049 */
1050 if (hsotg->core_params->reload_ctl > 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001051 hfir = dwc2_readl(hsotg->regs + HFIR);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001052 hfir |= HFIR_RLDCTRL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001053 dwc2_writel(hfir, hsotg->regs + HFIR);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001054 }
1055
1056 if (hsotg->core_params->dma_desc_enable > 0) {
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001057 u32 op_mode = hsotg->hw_params.op_mode;
1058 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
1059 !hsotg->hw_params.dma_desc_enable ||
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001060 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
1061 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
1062 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
1063 dev_err(hsotg->dev,
1064 "Hardware does not support descriptor DMA mode -\n");
1065 dev_err(hsotg->dev,
1066 "falling back to buffer DMA mode.\n");
1067 hsotg->core_params->dma_desc_enable = 0;
1068 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001069 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001070 hcfg |= HCFG_DESCDMA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001071 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001072 }
1073 }
1074
1075 /* Configure data FIFO sizes */
1076 dwc2_config_fifos(hsotg);
1077
1078 /* TODO - check this */
1079 /* Clear Host Set HNP Enable in the OTG Control Register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001080 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001081 otgctl &= ~GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001082 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001083
1084 /* Make sure the FIFOs are flushed */
1085 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
1086 dwc2_flush_rx_fifo(hsotg);
1087
1088 /* Clear Host Set HNP Enable in the OTG Control Register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001089 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001090 otgctl &= ~GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001091 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001092
1093 if (hsotg->core_params->dma_desc_enable <= 0) {
1094 int num_channels, i;
1095 u32 hcchar;
1096
1097 /* Flush out any leftover queued requests */
1098 num_channels = hsotg->core_params->host_channels;
1099 for (i = 0; i < num_channels; i++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001100 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001101 hcchar &= ~HCCHAR_CHENA;
1102 hcchar |= HCCHAR_CHDIS;
1103 hcchar &= ~HCCHAR_EPDIR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001104 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001105 }
1106
1107 /* Halt all channels to put them into a known state */
1108 for (i = 0; i < num_channels; i++) {
1109 int count = 0;
1110
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001111 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001112 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
1113 hcchar &= ~HCCHAR_EPDIR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001114 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001115 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
1116 __func__, i);
1117 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001118 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001119 if (++count > 1000) {
1120 dev_err(hsotg->dev,
1121 "Unable to clear enable on channel %d\n",
1122 i);
1123 break;
1124 }
1125 udelay(1);
1126 } while (hcchar & HCCHAR_CHENA);
1127 }
1128 }
1129
1130 /* Turn on the vbus power */
1131 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
1132 if (hsotg->op_state == OTG_STATE_A_HOST) {
1133 u32 hprt0 = dwc2_read_hprt0(hsotg);
1134
1135 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
1136 !!(hprt0 & HPRT0_PWR));
1137 if (!(hprt0 & HPRT0_PWR)) {
1138 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001139 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001140 }
1141 }
1142
1143 dwc2_enable_host_interrupts(hsotg);
1144}
1145
1146static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
1147 struct dwc2_host_chan *chan)
1148{
1149 u32 hcintmsk = HCINTMSK_CHHLTD;
1150
1151 switch (chan->ep_type) {
1152 case USB_ENDPOINT_XFER_CONTROL:
1153 case USB_ENDPOINT_XFER_BULK:
1154 dev_vdbg(hsotg->dev, "control/bulk\n");
1155 hcintmsk |= HCINTMSK_XFERCOMPL;
1156 hcintmsk |= HCINTMSK_STALL;
1157 hcintmsk |= HCINTMSK_XACTERR;
1158 hcintmsk |= HCINTMSK_DATATGLERR;
1159 if (chan->ep_is_in) {
1160 hcintmsk |= HCINTMSK_BBLERR;
1161 } else {
1162 hcintmsk |= HCINTMSK_NAK;
1163 hcintmsk |= HCINTMSK_NYET;
1164 if (chan->do_ping)
1165 hcintmsk |= HCINTMSK_ACK;
1166 }
1167
1168 if (chan->do_split) {
1169 hcintmsk |= HCINTMSK_NAK;
1170 if (chan->complete_split)
1171 hcintmsk |= HCINTMSK_NYET;
1172 else
1173 hcintmsk |= HCINTMSK_ACK;
1174 }
1175
1176 if (chan->error_state)
1177 hcintmsk |= HCINTMSK_ACK;
1178 break;
1179
1180 case USB_ENDPOINT_XFER_INT:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001181 if (dbg_perio())
1182 dev_vdbg(hsotg->dev, "intr\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001183 hcintmsk |= HCINTMSK_XFERCOMPL;
1184 hcintmsk |= HCINTMSK_NAK;
1185 hcintmsk |= HCINTMSK_STALL;
1186 hcintmsk |= HCINTMSK_XACTERR;
1187 hcintmsk |= HCINTMSK_DATATGLERR;
1188 hcintmsk |= HCINTMSK_FRMOVRUN;
1189
1190 if (chan->ep_is_in)
1191 hcintmsk |= HCINTMSK_BBLERR;
1192 if (chan->error_state)
1193 hcintmsk |= HCINTMSK_ACK;
1194 if (chan->do_split) {
1195 if (chan->complete_split)
1196 hcintmsk |= HCINTMSK_NYET;
1197 else
1198 hcintmsk |= HCINTMSK_ACK;
1199 }
1200 break;
1201
1202 case USB_ENDPOINT_XFER_ISOC:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001203 if (dbg_perio())
1204 dev_vdbg(hsotg->dev, "isoc\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001205 hcintmsk |= HCINTMSK_XFERCOMPL;
1206 hcintmsk |= HCINTMSK_FRMOVRUN;
1207 hcintmsk |= HCINTMSK_ACK;
1208
1209 if (chan->ep_is_in) {
1210 hcintmsk |= HCINTMSK_XACTERR;
1211 hcintmsk |= HCINTMSK_BBLERR;
1212 }
1213 break;
1214 default:
1215 dev_err(hsotg->dev, "## Unknown EP type ##\n");
1216 break;
1217 }
1218
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001219 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001220 if (dbg_hc(chan))
1221 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001222}
1223
1224static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
1225 struct dwc2_host_chan *chan)
1226{
1227 u32 hcintmsk = HCINTMSK_CHHLTD;
1228
1229 /*
1230 * For Descriptor DMA mode core halts the channel on AHB error.
1231 * Interrupt is not required.
1232 */
1233 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001234 if (dbg_hc(chan))
1235 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001236 hcintmsk |= HCINTMSK_AHBERR;
1237 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001238 if (dbg_hc(chan))
1239 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001240 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1241 hcintmsk |= HCINTMSK_XFERCOMPL;
1242 }
1243
1244 if (chan->error_state && !chan->do_split &&
1245 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001246 if (dbg_hc(chan))
1247 dev_vdbg(hsotg->dev, "setting ACK\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001248 hcintmsk |= HCINTMSK_ACK;
1249 if (chan->ep_is_in) {
1250 hcintmsk |= HCINTMSK_DATATGLERR;
1251 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
1252 hcintmsk |= HCINTMSK_NAK;
1253 }
1254 }
1255
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001256 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001257 if (dbg_hc(chan))
1258 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001259}
1260
1261static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
1262 struct dwc2_host_chan *chan)
1263{
1264 u32 intmsk;
1265
1266 if (hsotg->core_params->dma_enable > 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001267 if (dbg_hc(chan))
1268 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001269 dwc2_hc_enable_dma_ints(hsotg, chan);
1270 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001271 if (dbg_hc(chan))
1272 dev_vdbg(hsotg->dev, "DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001273 dwc2_hc_enable_slave_ints(hsotg, chan);
1274 }
1275
1276 /* Enable the top level host channel interrupt */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001277 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001278 intmsk |= 1 << chan->hc_num;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001279 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001280 if (dbg_hc(chan))
1281 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001282
1283 /* Make sure host channel interrupts are enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001284 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001285 intmsk |= GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001286 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001287 if (dbg_hc(chan))
1288 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001289}
1290
1291/**
1292 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
1293 * a specific endpoint
1294 *
1295 * @hsotg: Programming view of DWC_otg controller
1296 * @chan: Information needed to initialize the host channel
1297 *
1298 * The HCCHARn register is set up with the characteristics specified in chan.
1299 * Host channel interrupts that may need to be serviced while this transfer is
1300 * in progress are enabled.
1301 */
1302void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1303{
1304 u8 hc_num = chan->hc_num;
1305 u32 hcintmsk;
1306 u32 hcchar;
1307 u32 hcsplt = 0;
1308
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001309 if (dbg_hc(chan))
1310 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001311
1312 /* Clear old interrupt conditions for this host channel */
1313 hcintmsk = 0xffffffff;
1314 hcintmsk &= ~HCINTMSK_RESERVED14_31;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001315 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001316
1317 /* Enable channel interrupts required for this transfer */
1318 dwc2_hc_enable_ints(hsotg, chan);
1319
1320 /*
1321 * Program the HCCHARn register with the endpoint characteristics for
1322 * the current transfer
1323 */
1324 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
1325 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
1326 if (chan->ep_is_in)
1327 hcchar |= HCCHAR_EPDIR;
1328 if (chan->speed == USB_SPEED_LOW)
1329 hcchar |= HCCHAR_LSPDDEV;
1330 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
1331 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001332 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001333 if (dbg_hc(chan)) {
1334 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
1335 hc_num, hcchar);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001336
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001337 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
1338 __func__, hc_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001339 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001340 chan->dev_addr);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001341 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001342 chan->ep_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001343 dev_vdbg(hsotg->dev, " Is In: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001344 chan->ep_is_in);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001345 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001346 chan->speed == USB_SPEED_LOW);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001347 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001348 chan->ep_type);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001349 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001350 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001351 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001352
1353 /* Program the HCSPLT register for SPLITs */
1354 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001355 if (dbg_hc(chan))
1356 dev_vdbg(hsotg->dev,
1357 "Programming HC %d with split --> %s\n",
1358 hc_num,
1359 chan->complete_split ? "CSPLIT" : "SSPLIT");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001360 if (chan->complete_split)
1361 hcsplt |= HCSPLT_COMPSPLT;
1362 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
1363 HCSPLT_XACTPOS_MASK;
1364 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
1365 HCSPLT_HUBADDR_MASK;
1366 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
1367 HCSPLT_PRTADDR_MASK;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001368 if (dbg_hc(chan)) {
1369 dev_vdbg(hsotg->dev, " comp split %d\n",
1370 chan->complete_split);
1371 dev_vdbg(hsotg->dev, " xact pos %d\n",
1372 chan->xact_pos);
1373 dev_vdbg(hsotg->dev, " hub addr %d\n",
1374 chan->hub_addr);
1375 dev_vdbg(hsotg->dev, " hub port %d\n",
1376 chan->hub_port);
1377 dev_vdbg(hsotg->dev, " is_in %d\n",
1378 chan->ep_is_in);
1379 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001380 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001381 dev_vdbg(hsotg->dev, " xferlen %d\n",
1382 chan->xfer_len);
1383 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001384 }
1385
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001386 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001387}
1388
1389/**
1390 * dwc2_hc_halt() - Attempts to halt a host channel
1391 *
1392 * @hsotg: Controller register interface
1393 * @chan: Host channel to halt
1394 * @halt_status: Reason for halting the channel
1395 *
1396 * This function should only be called in Slave mode or to abort a transfer in
1397 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
1398 * controller halts the channel when the transfer is complete or a condition
1399 * occurs that requires application intervention.
1400 *
1401 * In slave mode, checks for a free request queue entry, then sets the Channel
1402 * Enable and Channel Disable bits of the Host Channel Characteristics
1403 * register of the specified channel to intiate the halt. If there is no free
1404 * request queue entry, sets only the Channel Disable bit of the HCCHARn
1405 * register to flush requests for this channel. In the latter case, sets a
1406 * flag to indicate that the host channel needs to be halted when a request
1407 * queue slot is open.
1408 *
1409 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
1410 * HCCHARn register. The controller ensures there is space in the request
1411 * queue before submitting the halt request.
1412 *
1413 * Some time may elapse before the core flushes any posted requests for this
1414 * host channel and halts. The Channel Halted interrupt handler completes the
1415 * deactivation of the host channel.
1416 */
1417void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
1418 enum dwc2_halt_status halt_status)
1419{
1420 u32 nptxsts, hptxsts, hcchar;
1421
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001422 if (dbg_hc(chan))
1423 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001424 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1425 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1426
1427 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1428 halt_status == DWC2_HC_XFER_AHB_ERR) {
1429 /*
1430 * Disable all channel interrupts except Ch Halted. The QTD
1431 * and QH state associated with this transfer has been cleared
1432 * (in the case of URB_DEQUEUE), so the channel needs to be
1433 * shut down carefully to prevent crashes.
1434 */
1435 u32 hcintmsk = HCINTMSK_CHHLTD;
1436
1437 dev_vdbg(hsotg->dev, "dequeue/error\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001438 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001439
1440 /*
1441 * Make sure no other interrupts besides halt are currently
1442 * pending. Handling another interrupt could cause a crash due
1443 * to the QTD and QH state.
1444 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001445 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001446
1447 /*
1448 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1449 * even if the channel was already halted for some other
1450 * reason
1451 */
1452 chan->halt_status = halt_status;
1453
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001454 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001455 if (!(hcchar & HCCHAR_CHENA)) {
1456 /*
1457 * The channel is either already halted or it hasn't
1458 * started yet. In DMA mode, the transfer may halt if
1459 * it finishes normally or a condition occurs that
1460 * requires driver intervention. Don't want to halt
1461 * the channel again. In either Slave or DMA mode,
1462 * it's possible that the transfer has been assigned
1463 * to a channel, but not started yet when an URB is
1464 * dequeued. Don't want to halt a channel that hasn't
1465 * started yet.
1466 */
1467 return;
1468 }
1469 }
1470 if (chan->halt_pending) {
1471 /*
1472 * A halt has already been issued for this channel. This might
1473 * happen when a transfer is aborted by a higher level in
1474 * the stack.
1475 */
1476 dev_vdbg(hsotg->dev,
1477 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1478 __func__, chan->hc_num);
1479 return;
1480 }
1481
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001482 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001483
1484 /* No need to set the bit in DDMA for disabling the channel */
1485 /* TODO check it everywhere channel is disabled */
1486 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001487 if (dbg_hc(chan))
1488 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001489 hcchar |= HCCHAR_CHENA;
1490 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001491 if (dbg_hc(chan))
1492 dev_dbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001493 }
1494 hcchar |= HCCHAR_CHDIS;
1495
1496 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001497 if (dbg_hc(chan))
1498 dev_vdbg(hsotg->dev, "DMA not enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001499 hcchar |= HCCHAR_CHENA;
1500
1501 /* Check for space in the request queue to issue the halt */
1502 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1503 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1504 dev_vdbg(hsotg->dev, "control/bulk\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001505 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001506 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1507 dev_vdbg(hsotg->dev, "Disabling channel\n");
1508 hcchar &= ~HCCHAR_CHENA;
1509 }
1510 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001511 if (dbg_perio())
1512 dev_vdbg(hsotg->dev, "isoc/intr\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001513 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001514 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1515 hsotg->queuing_high_bandwidth) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001516 if (dbg_perio())
1517 dev_vdbg(hsotg->dev, "Disabling channel\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001518 hcchar &= ~HCCHAR_CHENA;
1519 }
1520 }
1521 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001522 if (dbg_hc(chan))
1523 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001524 }
1525
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001526 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001527 chan->halt_status = halt_status;
1528
1529 if (hcchar & HCCHAR_CHENA) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001530 if (dbg_hc(chan))
1531 dev_vdbg(hsotg->dev, "Channel enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001532 chan->halt_pending = 1;
1533 chan->halt_on_queue = 0;
1534 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001535 if (dbg_hc(chan))
1536 dev_vdbg(hsotg->dev, "Channel disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001537 chan->halt_on_queue = 1;
1538 }
1539
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001540 if (dbg_hc(chan)) {
1541 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1542 chan->hc_num);
1543 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1544 hcchar);
1545 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1546 chan->halt_pending);
1547 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1548 chan->halt_on_queue);
1549 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1550 chan->halt_status);
1551 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001552}
1553
1554/**
1555 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1556 *
1557 * @hsotg: Programming view of DWC_otg controller
1558 * @chan: Identifies the host channel to clean up
1559 *
1560 * This function is normally called after a transfer is done and the host
1561 * channel is being released
1562 */
1563void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1564{
1565 u32 hcintmsk;
1566
1567 chan->xfer_started = 0;
1568
1569 /*
1570 * Clear channel interrupt enables and any unhandled channel interrupt
1571 * conditions
1572 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001573 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001574 hcintmsk = 0xffffffff;
1575 hcintmsk &= ~HCINTMSK_RESERVED14_31;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001576 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001577}
1578
1579/**
1580 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1581 * which frame a periodic transfer should occur
1582 *
1583 * @hsotg: Programming view of DWC_otg controller
1584 * @chan: Identifies the host channel to set up and its properties
1585 * @hcchar: Current value of the HCCHAR register for the specified host channel
1586 *
1587 * This function has no effect on non-periodic transfers
1588 */
1589static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1590 struct dwc2_host_chan *chan, u32 *hcchar)
1591{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001592 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1593 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001594 /* 1 if _next_ frame is odd, 0 if it's even */
Paul Zimmerman81a58952013-06-24 11:34:23 -07001595 if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001596 *hcchar |= HCCHAR_ODDFRM;
1597 }
1598}
1599
1600static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1601{
1602 /* Set up the initial PID for the transfer */
1603 if (chan->speed == USB_SPEED_HIGH) {
1604 if (chan->ep_is_in) {
1605 if (chan->multi_count == 1)
1606 chan->data_pid_start = DWC2_HC_PID_DATA0;
1607 else if (chan->multi_count == 2)
1608 chan->data_pid_start = DWC2_HC_PID_DATA1;
1609 else
1610 chan->data_pid_start = DWC2_HC_PID_DATA2;
1611 } else {
1612 if (chan->multi_count == 1)
1613 chan->data_pid_start = DWC2_HC_PID_DATA0;
1614 else
1615 chan->data_pid_start = DWC2_HC_PID_MDATA;
1616 }
1617 } else {
1618 chan->data_pid_start = DWC2_HC_PID_DATA0;
1619 }
1620}
1621
1622/**
1623 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1624 * the Host Channel
1625 *
1626 * @hsotg: Programming view of DWC_otg controller
1627 * @chan: Information needed to initialize the host channel
1628 *
1629 * This function should only be called in Slave mode. For a channel associated
1630 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1631 * associated with a periodic EP, the periodic Tx FIFO is written.
1632 *
1633 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1634 * the number of bytes written to the Tx FIFO.
1635 */
1636static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1637 struct dwc2_host_chan *chan)
1638{
1639 u32 i;
1640 u32 remaining_count;
1641 u32 byte_count;
1642 u32 dword_count;
1643 u32 __iomem *data_fifo;
1644 u32 *data_buf = (u32 *)chan->xfer_buf;
1645
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001646 if (dbg_hc(chan))
1647 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001648
1649 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1650
1651 remaining_count = chan->xfer_len - chan->xfer_count;
1652 if (remaining_count > chan->max_packet)
1653 byte_count = chan->max_packet;
1654 else
1655 byte_count = remaining_count;
1656
1657 dword_count = (byte_count + 3) / 4;
1658
1659 if (((unsigned long)data_buf & 0x3) == 0) {
1660 /* xfer_buf is DWORD aligned */
1661 for (i = 0; i < dword_count; i++, data_buf++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001662 dwc2_writel(*data_buf, data_fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001663 } else {
1664 /* xfer_buf is not DWORD aligned */
1665 for (i = 0; i < dword_count; i++, data_buf++) {
1666 u32 data = data_buf[0] | data_buf[1] << 8 |
1667 data_buf[2] << 16 | data_buf[3] << 24;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001668 dwc2_writel(data, data_fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001669 }
1670 }
1671
1672 chan->xfer_count += byte_count;
1673 chan->xfer_buf += byte_count;
1674}
1675
1676/**
1677 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1678 * channel and starts the transfer
1679 *
1680 * @hsotg: Programming view of DWC_otg controller
1681 * @chan: Information needed to initialize the host channel. The xfer_len value
1682 * may be reduced to accommodate the max widths of the XferSize and
1683 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1684 * changed to reflect the final xfer_len value.
1685 *
1686 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1687 * the caller must ensure that there is sufficient space in the request queue
1688 * and Tx Data FIFO.
1689 *
1690 * For an OUT transfer in Slave mode, it loads a data packet into the
1691 * appropriate FIFO. If necessary, additional data packets are loaded in the
1692 * Host ISR.
1693 *
1694 * For an IN transfer in Slave mode, a data packet is requested. The data
1695 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1696 * additional data packets are requested in the Host ISR.
1697 *
1698 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1699 * register along with a packet count of 1 and the channel is enabled. This
1700 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1701 * simply set to 0 since no data transfer occurs in this case.
1702 *
1703 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1704 * all the information required to perform the subsequent data transfer. In
1705 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1706 * controller performs the entire PING protocol, then starts the data
1707 * transfer.
1708 */
1709void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1710 struct dwc2_host_chan *chan)
1711{
1712 u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1713 u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1714 u32 hcchar;
1715 u32 hctsiz = 0;
1716 u16 num_packets;
Douglas Anderson69b76cd2015-11-11 10:33:52 -08001717 u32 ec_mc;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001718
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001719 if (dbg_hc(chan))
1720 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001721
1722 if (chan->do_ping) {
1723 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001724 if (dbg_hc(chan))
1725 dev_vdbg(hsotg->dev, "ping, no DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001726 dwc2_hc_do_ping(hsotg, chan);
1727 chan->xfer_started = 1;
1728 return;
1729 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001730 if (dbg_hc(chan))
1731 dev_vdbg(hsotg->dev, "ping, DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001732 hctsiz |= TSIZ_DOPNG;
1733 }
1734 }
1735
1736 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001737 if (dbg_hc(chan))
1738 dev_vdbg(hsotg->dev, "split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001739 num_packets = 1;
1740
1741 if (chan->complete_split && !chan->ep_is_in)
1742 /*
1743 * For CSPLIT OUT Transfer, set the size to 0 so the
1744 * core doesn't expect any data written to the FIFO
1745 */
1746 chan->xfer_len = 0;
1747 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1748 chan->xfer_len = chan->max_packet;
1749 else if (!chan->ep_is_in && chan->xfer_len > 188)
1750 chan->xfer_len = 188;
1751
1752 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1753 TSIZ_XFERSIZE_MASK;
Douglas Anderson69b76cd2015-11-11 10:33:52 -08001754
1755 /* For split set ec_mc for immediate retries */
1756 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1757 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1758 ec_mc = 3;
1759 else
1760 ec_mc = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001761 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001762 if (dbg_hc(chan))
1763 dev_vdbg(hsotg->dev, "no split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001764 /*
1765 * Ensure that the transfer length and packet count will fit
1766 * in the widths allocated for them in the HCTSIZn register
1767 */
1768 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1769 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1770 /*
1771 * Make sure the transfer size is no larger than one
1772 * (micro)frame's worth of data. (A check was done
1773 * when the periodic transfer was accepted to ensure
1774 * that a (micro)frame's worth of data can be
1775 * programmed into a channel.)
1776 */
1777 u32 max_periodic_len =
1778 chan->multi_count * chan->max_packet;
1779
1780 if (chan->xfer_len > max_periodic_len)
1781 chan->xfer_len = max_periodic_len;
1782 } else if (chan->xfer_len > max_hc_xfer_size) {
1783 /*
1784 * Make sure that xfer_len is a multiple of max packet
1785 * size
1786 */
1787 chan->xfer_len =
1788 max_hc_xfer_size - chan->max_packet + 1;
1789 }
1790
1791 if (chan->xfer_len > 0) {
1792 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1793 chan->max_packet;
1794 if (num_packets > max_hc_pkt_count) {
1795 num_packets = max_hc_pkt_count;
1796 chan->xfer_len = num_packets * chan->max_packet;
1797 }
1798 } else {
1799 /* Need 1 packet for transfer length of 0 */
1800 num_packets = 1;
1801 }
1802
1803 if (chan->ep_is_in)
1804 /*
1805 * Always program an integral # of max packets for IN
1806 * transfers
1807 */
1808 chan->xfer_len = num_packets * chan->max_packet;
1809
1810 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1811 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1812 /*
1813 * Make sure that the multi_count field matches the
1814 * actual transfer length
1815 */
1816 chan->multi_count = num_packets;
1817
1818 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1819 dwc2_set_pid_isoc(chan);
1820
1821 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1822 TSIZ_XFERSIZE_MASK;
Douglas Anderson69b76cd2015-11-11 10:33:52 -08001823
1824 /* The ec_mc gets the multi_count for non-split */
1825 ec_mc = chan->multi_count;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001826 }
1827
1828 chan->start_pkt_count = num_packets;
1829 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1830 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1831 TSIZ_SC_MC_PID_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001832 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001833 if (dbg_hc(chan)) {
1834 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1835 hctsiz, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001836
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001837 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1838 chan->hc_num);
1839 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001840 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1841 TSIZ_XFERSIZE_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001842 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001843 (hctsiz & TSIZ_PKTCNT_MASK) >>
1844 TSIZ_PKTCNT_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001845 dev_vdbg(hsotg->dev, " Start PID: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001846 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1847 TSIZ_SC_MC_PID_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001848 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001849
1850 if (hsotg->core_params->dma_enable > 0) {
1851 dma_addr_t dma_addr;
1852
1853 if (chan->align_buf) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001854 if (dbg_hc(chan))
1855 dev_vdbg(hsotg->dev, "align_buf\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001856 dma_addr = chan->align_buf;
1857 } else {
1858 dma_addr = chan->xfer_dma;
1859 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001860 dwc2_writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001861 if (dbg_hc(chan))
1862 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1863 (unsigned long)dma_addr, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001864 }
1865
1866 /* Start the split */
1867 if (chan->do_split) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001868 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001869
1870 hcsplt |= HCSPLT_SPLTENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001871 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001872 }
1873
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001874 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001875 hcchar &= ~HCCHAR_MULTICNT_MASK;
Douglas Anderson69b76cd2015-11-11 10:33:52 -08001876 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001877 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1878
1879 if (hcchar & HCCHAR_CHDIS)
1880 dev_warn(hsotg->dev,
1881 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1882 __func__, chan->hc_num, hcchar);
1883
1884 /* Set host channel enable after all other setup is complete */
1885 hcchar |= HCCHAR_CHENA;
1886 hcchar &= ~HCCHAR_CHDIS;
1887
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001888 if (dbg_hc(chan))
1889 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001890 (hcchar & HCCHAR_MULTICNT_MASK) >>
1891 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001892
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001893 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001894 if (dbg_hc(chan))
1895 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1896 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001897
1898 chan->xfer_started = 1;
1899 chan->requests++;
1900
1901 if (hsotg->core_params->dma_enable <= 0 &&
1902 !chan->ep_is_in && chan->xfer_len > 0)
1903 /* Load OUT packet into the appropriate Tx FIFO */
1904 dwc2_hc_write_packet(hsotg, chan);
1905}
1906
1907/**
1908 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1909 * host channel and starts the transfer in Descriptor DMA mode
1910 *
1911 * @hsotg: Programming view of DWC_otg controller
1912 * @chan: Information needed to initialize the host channel
1913 *
1914 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1915 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1916 * with micro-frame bitmap.
1917 *
1918 * Initializes HCDMA register with descriptor list address and CTD value then
1919 * starts the transfer via enabling the channel.
1920 */
1921void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1922 struct dwc2_host_chan *chan)
1923{
1924 u32 hcchar;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001925 u32 hctsiz = 0;
1926
1927 if (chan->do_ping)
1928 hctsiz |= TSIZ_DOPNG;
1929
1930 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1931 dwc2_set_pid_isoc(chan);
1932
1933 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1934 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1935 TSIZ_SC_MC_PID_MASK;
1936
1937 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1938 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1939
1940 /* Non-zero only for high-speed interrupt endpoints */
1941 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1942
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001943 if (dbg_hc(chan)) {
1944 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1945 chan->hc_num);
1946 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1947 chan->data_pid_start);
1948 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1949 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001950
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001951 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001952
Gregory Herrero95105a92015-11-20 11:49:29 +01001953 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1954 chan->desc_list_sz, DMA_TO_DEVICE);
1955
Mian Yousaf Kaukabe23b8a52015-11-20 11:49:30 +01001956 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001957
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001958 if (dbg_hc(chan))
Mian Yousaf Kaukabe23b8a52015-11-20 11:49:30 +01001959 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1960 &chan->desc_list_addr, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001961
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001962 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001963 hcchar &= ~HCCHAR_MULTICNT_MASK;
1964 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1965 HCCHAR_MULTICNT_MASK;
1966
1967 if (hcchar & HCCHAR_CHDIS)
1968 dev_warn(hsotg->dev,
1969 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1970 __func__, chan->hc_num, hcchar);
1971
1972 /* Set host channel enable after all other setup is complete */
1973 hcchar |= HCCHAR_CHENA;
1974 hcchar &= ~HCCHAR_CHDIS;
1975
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001976 if (dbg_hc(chan))
1977 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001978 (hcchar & HCCHAR_MULTICNT_MASK) >>
1979 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001980
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001981 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001982 if (dbg_hc(chan))
1983 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1984 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001985
1986 chan->xfer_started = 1;
1987 chan->requests++;
1988}
1989
1990/**
1991 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1992 * a previous call to dwc2_hc_start_transfer()
1993 *
1994 * @hsotg: Programming view of DWC_otg controller
1995 * @chan: Information needed to initialize the host channel
1996 *
1997 * The caller must ensure there is sufficient space in the request queue and Tx
1998 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1999 * the controller acts autonomously to complete transfers programmed to a host
2000 * channel.
2001 *
2002 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
2003 * if there is any data remaining to be queued. For an IN transfer, another
2004 * data packet is always requested. For the SETUP phase of a control transfer,
2005 * this function does nothing.
2006 *
2007 * Return: 1 if a new request is queued, 0 if no more requests are required
2008 * for this transfer
2009 */
2010int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
2011 struct dwc2_host_chan *chan)
2012{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002013 if (dbg_hc(chan))
2014 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2015 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002016
2017 if (chan->do_split)
2018 /* SPLITs always queue just once per channel */
2019 return 0;
2020
2021 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
2022 /* SETUPs are queued only once since they can't be NAK'd */
2023 return 0;
2024
2025 if (chan->ep_is_in) {
2026 /*
2027 * Always queue another request for other IN transfers. If
2028 * back-to-back INs are issued and NAKs are received for both,
2029 * the driver may still be processing the first NAK when the
2030 * second NAK is received. When the interrupt handler clears
2031 * the NAK interrupt for the first NAK, the second NAK will
2032 * not be seen. So we can't depend on the NAK interrupt
2033 * handler to requeue a NAK'd request. Instead, IN requests
2034 * are issued each time this function is called. When the
2035 * transfer completes, the extra requests for the channel will
2036 * be flushed.
2037 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002038 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002039
2040 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
2041 hcchar |= HCCHAR_CHENA;
2042 hcchar &= ~HCCHAR_CHDIS;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002043 if (dbg_hc(chan))
2044 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
2045 hcchar);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002046 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002047 chan->requests++;
2048 return 1;
2049 }
2050
2051 /* OUT transfers */
2052
2053 if (chan->xfer_count < chan->xfer_len) {
2054 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2055 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002056 u32 hcchar = dwc2_readl(hsotg->regs +
2057 HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002058
2059 dwc2_hc_set_even_odd_frame(hsotg, chan,
2060 &hcchar);
2061 }
2062
2063 /* Load OUT packet into the appropriate Tx FIFO */
2064 dwc2_hc_write_packet(hsotg, chan);
2065 chan->requests++;
2066 return 1;
2067 }
2068
2069 return 0;
2070}
2071
2072/**
2073 * dwc2_hc_do_ping() - Starts a PING transfer
2074 *
2075 * @hsotg: Programming view of DWC_otg controller
2076 * @chan: Information needed to initialize the host channel
2077 *
2078 * This function should only be called in Slave mode. The Do Ping bit is set in
2079 * the HCTSIZ register, then the channel is enabled.
2080 */
2081void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
2082{
2083 u32 hcchar;
2084 u32 hctsiz;
2085
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002086 if (dbg_hc(chan))
2087 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2088 chan->hc_num);
2089
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002090
2091 hctsiz = TSIZ_DOPNG;
2092 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002093 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002094
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002095 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002096 hcchar |= HCCHAR_CHENA;
2097 hcchar &= ~HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002098 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002099}
2100
2101/**
2102 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
2103 * the HFIR register according to PHY type and speed
2104 *
2105 * @hsotg: Programming view of DWC_otg controller
2106 *
2107 * NOTE: The caller can modify the value of the HFIR register only after the
2108 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
2109 * has been set
2110 */
2111u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
2112{
2113 u32 usbcfg;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002114 u32 hprt0;
2115 int clock = 60; /* default value */
2116
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002117 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2118 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002119
2120 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
2121 !(usbcfg & GUSBCFG_PHYIF16))
2122 clock = 60;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002123 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002124 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
2125 clock = 48;
2126 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2127 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2128 clock = 30;
2129 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2130 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
2131 clock = 60;
2132 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2133 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2134 clock = 48;
2135 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002136 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002137 clock = 48;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002138 if ((usbcfg & GUSBCFG_PHYSEL) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002139 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002140 clock = 48;
2141
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002142 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002143 /* High speed case */
2144 return 125 * clock;
2145 else
2146 /* FS/LS case */
2147 return 1000 * clock;
2148}
2149
2150/**
2151 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
2152 * buffer
2153 *
2154 * @core_if: Programming view of DWC_otg controller
2155 * @dest: Destination buffer for the packet
2156 * @bytes: Number of bytes to copy to the destination
2157 */
2158void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
2159{
2160 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
2161 u32 *data_buf = (u32 *)dest;
2162 int word_count = (bytes + 3) / 4;
2163 int i;
2164
2165 /*
2166 * Todo: Account for the case where dest is not dword aligned. This
2167 * requires reading data from the FIFO into a u32 temp buffer, then
2168 * moving it into the data buffer.
2169 */
2170
2171 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
2172
2173 for (i = 0; i < word_count; i++, data_buf++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002174 *data_buf = dwc2_readl(fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002175}
2176
2177/**
2178 * dwc2_dump_host_registers() - Prints the host registers
2179 *
2180 * @hsotg: Programming view of DWC_otg controller
2181 *
2182 * NOTE: This function will be removed once the peripheral controller code
2183 * is integrated and the driver is stable
2184 */
2185void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
2186{
2187#ifdef DEBUG
2188 u32 __iomem *addr;
2189 int i;
2190
2191 dev_dbg(hsotg->dev, "Host Global Registers\n");
2192 addr = hsotg->regs + HCFG;
2193 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002194 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002195 addr = hsotg->regs + HFIR;
2196 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002197 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002198 addr = hsotg->regs + HFNUM;
2199 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002200 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002201 addr = hsotg->regs + HPTXSTS;
2202 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002203 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002204 addr = hsotg->regs + HAINT;
2205 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002206 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002207 addr = hsotg->regs + HAINTMSK;
2208 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002209 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002210 if (hsotg->core_params->dma_desc_enable > 0) {
2211 addr = hsotg->regs + HFLBADDR;
2212 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002213 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002214 }
2215
2216 addr = hsotg->regs + HPRT0;
2217 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002218 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002219
2220 for (i = 0; i < hsotg->core_params->host_channels; i++) {
2221 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
2222 addr = hsotg->regs + HCCHAR(i);
2223 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002224 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002225 addr = hsotg->regs + HCSPLT(i);
2226 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002227 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002228 addr = hsotg->regs + HCINT(i);
2229 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002230 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002231 addr = hsotg->regs + HCINTMSK(i);
2232 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002233 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002234 addr = hsotg->regs + HCTSIZ(i);
2235 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002236 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002237 addr = hsotg->regs + HCDMA(i);
2238 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002239 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002240 if (hsotg->core_params->dma_desc_enable > 0) {
2241 addr = hsotg->regs + HCDMAB(i);
2242 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002243 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002244 }
2245 }
2246#endif
2247}
2248
2249/**
2250 * dwc2_dump_global_registers() - Prints the core global registers
2251 *
2252 * @hsotg: Programming view of DWC_otg controller
2253 *
2254 * NOTE: This function will be removed once the peripheral controller code
2255 * is integrated and the driver is stable
2256 */
2257void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
2258{
2259#ifdef DEBUG
2260 u32 __iomem *addr;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002261
2262 dev_dbg(hsotg->dev, "Core Global Registers\n");
2263 addr = hsotg->regs + GOTGCTL;
2264 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002265 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002266 addr = hsotg->regs + GOTGINT;
2267 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002268 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002269 addr = hsotg->regs + GAHBCFG;
2270 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002271 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002272 addr = hsotg->regs + GUSBCFG;
2273 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002274 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002275 addr = hsotg->regs + GRSTCTL;
2276 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002277 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002278 addr = hsotg->regs + GINTSTS;
2279 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002280 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002281 addr = hsotg->regs + GINTMSK;
2282 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002283 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002284 addr = hsotg->regs + GRXSTSR;
2285 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002286 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002287 addr = hsotg->regs + GRXFSIZ;
2288 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002289 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002290 addr = hsotg->regs + GNPTXFSIZ;
2291 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002292 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002293 addr = hsotg->regs + GNPTXSTS;
2294 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002295 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002296 addr = hsotg->regs + GI2CCTL;
2297 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002298 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002299 addr = hsotg->regs + GPVNDCTL;
2300 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002301 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002302 addr = hsotg->regs + GGPIO;
2303 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002304 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002305 addr = hsotg->regs + GUID;
2306 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002307 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002308 addr = hsotg->regs + GSNPSID;
2309 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002310 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002311 addr = hsotg->regs + GHWCFG1;
2312 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002313 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002314 addr = hsotg->regs + GHWCFG2;
2315 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002316 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002317 addr = hsotg->regs + GHWCFG3;
2318 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002319 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002320 addr = hsotg->regs + GHWCFG4;
2321 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002322 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002323 addr = hsotg->regs + GLPMCFG;
2324 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002325 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002326 addr = hsotg->regs + GPWRDN;
2327 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002328 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002329 addr = hsotg->regs + GDFIFOCFG;
2330 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002331 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002332 addr = hsotg->regs + HPTXFSIZ;
2333 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002334 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002335
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002336 addr = hsotg->regs + PCGCTL;
2337 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002338 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002339#endif
2340}
2341
2342/**
2343 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
2344 *
2345 * @hsotg: Programming view of DWC_otg controller
2346 * @num: Tx FIFO to flush
2347 */
2348void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
2349{
2350 u32 greset;
2351 int count = 0;
2352
2353 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
2354
2355 greset = GRSTCTL_TXFFLSH;
2356 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002357 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002358
2359 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002360 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002361 if (++count > 10000) {
2362 dev_warn(hsotg->dev,
2363 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
2364 __func__, greset,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002365 dwc2_readl(hsotg->regs + GNPTXSTS));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002366 break;
2367 }
2368 udelay(1);
2369 } while (greset & GRSTCTL_TXFFLSH);
2370
2371 /* Wait for at least 3 PHY Clocks */
2372 udelay(1);
2373}
2374
2375/**
2376 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
2377 *
2378 * @hsotg: Programming view of DWC_otg controller
2379 */
2380void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
2381{
2382 u32 greset;
2383 int count = 0;
2384
2385 dev_vdbg(hsotg->dev, "%s()\n", __func__);
2386
2387 greset = GRSTCTL_RXFFLSH;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002388 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002389
2390 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002391 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002392 if (++count > 10000) {
2393 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
2394 __func__, greset);
2395 break;
2396 }
2397 udelay(1);
2398 } while (greset & GRSTCTL_RXFFLSH);
2399
2400 /* Wait for at least 3 PHY Clocks */
2401 udelay(1);
2402}
2403
Paul Zimmerman498f0662013-11-22 16:43:47 -08002404#define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002405
2406/* Parameter access functions */
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002407void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002408{
2409 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002410
2411 switch (val) {
2412 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002413 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002414 valid = 0;
2415 break;
2416 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002417 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002418 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2419 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2420 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2421 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2422 break;
2423 default:
2424 valid = 0;
2425 break;
2426 }
2427 break;
2428 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
2429 /* always valid */
2430 break;
2431 default:
2432 valid = 0;
2433 break;
2434 }
2435
2436 if (!valid) {
2437 if (val >= 0)
2438 dev_err(hsotg->dev,
2439 "%d invalid for otg_cap parameter. Check HW configuration.\n",
2440 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002441 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002442 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2443 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2444 break;
2445 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2446 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2447 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2448 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2449 break;
2450 default:
2451 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2452 break;
2453 }
2454 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002455 }
2456
2457 hsotg->core_params->otg_cap = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002458}
2459
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002460void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002461{
2462 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002463
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002464 if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002465 valid = 0;
2466 if (val < 0)
2467 valid = 0;
2468
2469 if (!valid) {
2470 if (val >= 0)
2471 dev_err(hsotg->dev,
2472 "%d invalid for dma_enable parameter. Check HW configuration.\n",
2473 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002474 val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002475 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002476 }
2477
2478 hsotg->core_params->dma_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002479}
2480
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002481void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002482{
2483 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002484
2485 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002486 !hsotg->hw_params.dma_desc_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002487 valid = 0;
2488 if (val < 0)
2489 valid = 0;
2490
2491 if (!valid) {
2492 if (val >= 0)
2493 dev_err(hsotg->dev,
2494 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2495 val);
2496 val = (hsotg->core_params->dma_enable > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002497 hsotg->hw_params.dma_desc_enable);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002498 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002499 }
2500
2501 hsotg->core_params->dma_desc_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002502}
2503
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01002504void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg, int val)
2505{
2506 int valid = 1;
2507
2508 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2509 !hsotg->hw_params.dma_desc_enable))
2510 valid = 0;
2511 if (val < 0)
2512 valid = 0;
2513
2514 if (!valid) {
2515 if (val >= 0)
2516 dev_err(hsotg->dev,
2517 "%d invalid for dma_desc_fs_enable parameter. Check HW configuration.\n",
2518 val);
2519 val = (hsotg->core_params->dma_enable > 0 &&
2520 hsotg->hw_params.dma_desc_enable);
2521 }
2522
2523 hsotg->core_params->dma_desc_fs_enable = val;
2524 dev_dbg(hsotg->dev, "Setting dma_desc_fs_enable to %d\n", val);
2525}
2526
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002527void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2528 int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002529{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002530 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002531 if (val >= 0) {
2532 dev_err(hsotg->dev,
2533 "Wrong value for host_support_fs_low_power\n");
2534 dev_err(hsotg->dev,
2535 "host_support_fs_low_power must be 0 or 1\n");
2536 }
2537 val = 0;
2538 dev_dbg(hsotg->dev,
2539 "Setting host_support_fs_low_power to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002540 }
2541
2542 hsotg->core_params->host_support_fs_ls_low_power = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002543}
2544
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002545void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002546{
2547 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002548
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002549 if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002550 valid = 0;
2551 if (val < 0)
2552 valid = 0;
2553
2554 if (!valid) {
2555 if (val >= 0)
2556 dev_err(hsotg->dev,
2557 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2558 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002559 val = hsotg->hw_params.enable_dynamic_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002560 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002561 }
2562
2563 hsotg->core_params->enable_dynamic_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002564}
2565
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002566void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002567{
2568 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002569
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002570 if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002571 valid = 0;
2572
2573 if (!valid) {
2574 if (val >= 0)
2575 dev_err(hsotg->dev,
2576 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2577 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002578 val = hsotg->hw_params.host_rx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002579 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002580 }
2581
2582 hsotg->core_params->host_rx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002583}
2584
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002585void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002586{
2587 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002588
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002589 if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002590 valid = 0;
2591
2592 if (!valid) {
2593 if (val >= 0)
2594 dev_err(hsotg->dev,
2595 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2596 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002597 val = hsotg->hw_params.host_nperio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002598 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2599 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002600 }
2601
2602 hsotg->core_params->host_nperio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002603}
2604
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002605void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002606{
2607 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002608
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002609 if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002610 valid = 0;
2611
2612 if (!valid) {
2613 if (val >= 0)
2614 dev_err(hsotg->dev,
2615 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2616 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002617 val = hsotg->hw_params.host_perio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002618 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2619 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002620 }
2621
2622 hsotg->core_params->host_perio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002623}
2624
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002625void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002626{
2627 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002628
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002629 if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002630 valid = 0;
2631
2632 if (!valid) {
2633 if (val >= 0)
2634 dev_err(hsotg->dev,
2635 "%d invalid for max_transfer_size. Check HW configuration.\n",
2636 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002637 val = hsotg->hw_params.max_transfer_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002638 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002639 }
2640
2641 hsotg->core_params->max_transfer_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002642}
2643
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002644void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002645{
2646 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002647
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002648 if (val < 15 || val > hsotg->hw_params.max_packet_count)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002649 valid = 0;
2650
2651 if (!valid) {
2652 if (val >= 0)
2653 dev_err(hsotg->dev,
2654 "%d invalid for max_packet_count. Check HW configuration.\n",
2655 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002656 val = hsotg->hw_params.max_packet_count;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002657 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002658 }
2659
2660 hsotg->core_params->max_packet_count = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002661}
2662
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002663void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002664{
2665 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002666
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002667 if (val < 1 || val > hsotg->hw_params.host_channels)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002668 valid = 0;
2669
2670 if (!valid) {
2671 if (val >= 0)
2672 dev_err(hsotg->dev,
2673 "%d invalid for host_channels. Check HW configuration.\n",
2674 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002675 val = hsotg->hw_params.host_channels;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002676 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002677 }
2678
2679 hsotg->core_params->host_channels = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002680}
2681
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002682void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002683{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002684 int valid = 0;
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002685 u32 hs_phy_type, fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002686
Paul Zimmerman498f0662013-11-22 16:43:47 -08002687 if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
2688 DWC2_PHY_TYPE_PARAM_ULPI)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002689 if (val >= 0) {
2690 dev_err(hsotg->dev, "Wrong value for phy_type\n");
2691 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2692 }
2693
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002694 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002695 }
2696
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002697 hs_phy_type = hsotg->hw_params.hs_phy_type;
2698 fs_phy_type = hsotg->hw_params.fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002699 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2700 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2701 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2702 valid = 1;
2703 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2704 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2705 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2706 valid = 1;
2707 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2708 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2709 valid = 1;
2710
2711 if (!valid) {
2712 if (val >= 0)
2713 dev_err(hsotg->dev,
2714 "%d invalid for phy_type. Check HW configuration.\n",
2715 val);
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002716 val = DWC2_PHY_TYPE_PARAM_FS;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002717 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2718 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2719 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2720 val = DWC2_PHY_TYPE_PARAM_UTMI;
2721 else
2722 val = DWC2_PHY_TYPE_PARAM_ULPI;
2723 }
2724 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002725 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002726
2727 hsotg->core_params->phy_type = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002728}
2729
2730static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2731{
2732 return hsotg->core_params->phy_type;
2733}
2734
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002735void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002736{
2737 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002738
Paul Zimmerman498f0662013-11-22 16:43:47 -08002739 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002740 if (val >= 0) {
2741 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2742 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2743 }
2744 valid = 0;
2745 }
2746
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002747 if (val == DWC2_SPEED_PARAM_HIGH &&
2748 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002749 valid = 0;
2750
2751 if (!valid) {
2752 if (val >= 0)
2753 dev_err(hsotg->dev,
2754 "%d invalid for speed parameter. Check HW configuration.\n",
2755 val);
2756 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002757 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002758 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002759 }
2760
2761 hsotg->core_params->speed = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002762}
2763
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002764void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002765{
2766 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002767
Paul Zimmerman498f0662013-11-22 16:43:47 -08002768 if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2769 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002770 if (val >= 0) {
2771 dev_err(hsotg->dev,
2772 "Wrong value for host_ls_low_power_phy_clk parameter\n");
2773 dev_err(hsotg->dev,
2774 "host_ls_low_power_phy_clk must be 0 or 1\n");
2775 }
2776 valid = 0;
2777 }
2778
2779 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2780 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2781 valid = 0;
2782
2783 if (!valid) {
2784 if (val >= 0)
2785 dev_err(hsotg->dev,
2786 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2787 val);
2788 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2789 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2790 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2791 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2792 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002793 }
2794
2795 hsotg->core_params->host_ls_low_power_phy_clk = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002796}
2797
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002798void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002799{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002800 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002801 if (val >= 0) {
2802 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2803 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2804 }
2805 val = 0;
2806 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002807 }
2808
2809 hsotg->core_params->phy_ulpi_ddr = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002810}
2811
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002812void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002813{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002814 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002815 if (val >= 0) {
2816 dev_err(hsotg->dev,
2817 "Wrong value for phy_ulpi_ext_vbus\n");
2818 dev_err(hsotg->dev,
2819 "phy_ulpi_ext_vbus must be 0 or 1\n");
2820 }
2821 val = 0;
2822 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002823 }
2824
2825 hsotg->core_params->phy_ulpi_ext_vbus = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002826}
2827
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002828void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002829{
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002830 int valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002831
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002832 switch (hsotg->hw_params.utmi_phy_data_width) {
2833 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
2834 valid = (val == 8);
2835 break;
2836 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
2837 valid = (val == 16);
2838 break;
2839 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
2840 valid = (val == 8 || val == 16);
2841 break;
2842 }
2843
2844 if (!valid) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002845 if (val >= 0) {
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002846 dev_err(hsotg->dev,
2847 "%d invalid for phy_utmi_width. Check HW configuration.\n",
2848 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002849 }
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002850 val = (hsotg->hw_params.utmi_phy_data_width ==
2851 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002852 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002853 }
2854
2855 hsotg->core_params->phy_utmi_width = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002856}
2857
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002858void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002859{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002860 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002861 if (val >= 0) {
2862 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2863 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2864 }
2865 val = 0;
2866 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002867 }
2868
2869 hsotg->core_params->ulpi_fs_ls = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002870}
2871
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002872void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002873{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002874 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002875 if (val >= 0) {
2876 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2877 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2878 }
2879 val = 0;
2880 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002881 }
2882
2883 hsotg->core_params->ts_dline = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002884}
2885
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002886void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002887{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002888 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002889
Paul Zimmerman498f0662013-11-22 16:43:47 -08002890 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002891 if (val >= 0) {
2892 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
2893 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
2894 }
2895
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002896 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002897 }
2898
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002899 if (val == 1 && !(hsotg->hw_params.i2c_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002900 valid = 0;
2901
2902 if (!valid) {
2903 if (val >= 0)
2904 dev_err(hsotg->dev,
2905 "%d invalid for i2c_enable. Check HW configuration.\n",
2906 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002907 val = hsotg->hw_params.i2c_enable;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002908 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002909 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002910
2911 hsotg->core_params->i2c_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002912}
2913
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002914void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002915{
2916 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002917
Paul Zimmerman498f0662013-11-22 16:43:47 -08002918 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002919 if (val >= 0) {
2920 dev_err(hsotg->dev,
2921 "Wrong value for en_multiple_tx_fifo,\n");
2922 dev_err(hsotg->dev,
2923 "en_multiple_tx_fifo must be 0 or 1\n");
2924 }
2925 valid = 0;
2926 }
2927
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002928 if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002929 valid = 0;
2930
2931 if (!valid) {
2932 if (val >= 0)
2933 dev_err(hsotg->dev,
2934 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
2935 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002936 val = hsotg->hw_params.en_multiple_tx_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002937 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002938 }
2939
2940 hsotg->core_params->en_multiple_tx_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002941}
2942
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002943void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002944{
2945 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002946
Paul Zimmerman498f0662013-11-22 16:43:47 -08002947 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002948 if (val >= 0) {
2949 dev_err(hsotg->dev,
2950 "'%d' invalid for parameter reload_ctl\n", val);
2951 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
2952 }
2953 valid = 0;
2954 }
2955
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002956 if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002957 valid = 0;
2958
2959 if (!valid) {
2960 if (val >= 0)
2961 dev_err(hsotg->dev,
2962 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
2963 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002964 val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002965 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002966 }
2967
2968 hsotg->core_params->reload_ctl = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002969}
2970
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002971void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002972{
Paul Zimmerman4d3190e2013-07-16 12:22:12 -07002973 if (val != -1)
2974 hsotg->core_params->ahbcfg = val;
2975 else
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002976 hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002977 GAHBCFG_HBSTLEN_SHIFT;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002978}
2979
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002980void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002981{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002982 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002983 if (val >= 0) {
2984 dev_err(hsotg->dev,
2985 "'%d' invalid for parameter otg_ver\n", val);
2986 dev_err(hsotg->dev,
2987 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
2988 }
2989 val = 0;
2990 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002991 }
2992
2993 hsotg->core_params->otg_ver = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002994}
2995
Wei Yongjun49cf10c2013-11-28 10:27:59 +08002996static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
Paul Zimmermane8576e62013-11-25 13:42:47 -08002997{
2998 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2999 if (val >= 0) {
3000 dev_err(hsotg->dev,
3001 "'%d' invalid for parameter uframe_sched\n",
3002 val);
3003 dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
3004 }
3005 val = 1;
3006 dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
3007 }
3008
3009 hsotg->core_params->uframe_sched = val;
3010}
3011
Gregory Herreroa6d249d2015-04-29 22:09:04 +02003012static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
3013 int val)
3014{
3015 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3016 if (val >= 0) {
3017 dev_err(hsotg->dev,
3018 "'%d' invalid for parameter external_id_pin_ctl\n",
3019 val);
3020 dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
3021 }
3022 val = 0;
3023 dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
3024 }
3025
3026 hsotg->core_params->external_id_pin_ctl = val;
3027}
3028
Gregory Herrero285046a2015-04-29 22:09:19 +02003029static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
3030 int val)
3031{
3032 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3033 if (val >= 0) {
3034 dev_err(hsotg->dev,
3035 "'%d' invalid for parameter hibernation\n",
3036 val);
3037 dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
3038 }
3039 val = 0;
3040 dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
3041 }
3042
3043 hsotg->core_params->hibernation = val;
3044}
3045
Paul Zimmermane8576e62013-11-25 13:42:47 -08003046/*
3047 * This function is called during module intialization to pass module parameters
3048 * for the DWC_otg core.
3049 */
3050void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
3051 const struct dwc2_core_params *params)
3052{
3053 dev_dbg(hsotg->dev, "%s()\n", __func__);
3054
3055 dwc2_set_param_otg_cap(hsotg, params->otg_cap);
3056 dwc2_set_param_dma_enable(hsotg, params->dma_enable);
3057 dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003058 dwc2_set_param_dma_desc_fs_enable(hsotg, params->dma_desc_fs_enable);
Paul Zimmermane8576e62013-11-25 13:42:47 -08003059 dwc2_set_param_host_support_fs_ls_low_power(hsotg,
3060 params->host_support_fs_ls_low_power);
3061 dwc2_set_param_enable_dynamic_fifo(hsotg,
3062 params->enable_dynamic_fifo);
3063 dwc2_set_param_host_rx_fifo_size(hsotg,
3064 params->host_rx_fifo_size);
3065 dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
3066 params->host_nperio_tx_fifo_size);
3067 dwc2_set_param_host_perio_tx_fifo_size(hsotg,
3068 params->host_perio_tx_fifo_size);
3069 dwc2_set_param_max_transfer_size(hsotg,
3070 params->max_transfer_size);
3071 dwc2_set_param_max_packet_count(hsotg,
3072 params->max_packet_count);
3073 dwc2_set_param_host_channels(hsotg, params->host_channels);
3074 dwc2_set_param_phy_type(hsotg, params->phy_type);
3075 dwc2_set_param_speed(hsotg, params->speed);
3076 dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
3077 params->host_ls_low_power_phy_clk);
3078 dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
3079 dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
3080 params->phy_ulpi_ext_vbus);
3081 dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
3082 dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
3083 dwc2_set_param_ts_dline(hsotg, params->ts_dline);
3084 dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
3085 dwc2_set_param_en_multiple_tx_fifo(hsotg,
3086 params->en_multiple_tx_fifo);
3087 dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
3088 dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
3089 dwc2_set_param_otg_ver(hsotg, params->otg_ver);
3090 dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
Gregory Herreroa6d249d2015-04-29 22:09:04 +02003091 dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
Gregory Herrero285046a2015-04-29 22:09:19 +02003092 dwc2_set_param_hibernation(hsotg, params->hibernation);
Paul Zimmermane8576e62013-11-25 13:42:47 -08003093}
3094
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003095/**
3096 * During device initialization, read various hardware configuration
3097 * registers and interpret the contents.
3098 */
3099int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
3100{
3101 struct dwc2_hw_params *hw = &hsotg->hw_params;
3102 unsigned width;
3103 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
3104 u32 hptxfsiz, grxfsiz, gnptxfsiz;
Douglas Andersonf6194732015-12-17 11:14:54 -08003105 u32 gusbcfg = 0;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003106
3107 /*
3108 * Attempt to ensure this device is really a DWC_otg Controller.
3109 * Read and verify the GSNPSID register contents. The value should be
3110 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
3111 * as in "OTG version 2.xx" or "OTG version 3.xx".
3112 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003113 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003114 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
3115 (hw->snpsid & 0xfffff000) != 0x4f543000) {
3116 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
3117 hw->snpsid);
3118 return -ENODEV;
3119 }
3120
3121 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
3122 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
3123 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
3124
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003125 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
3126 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
3127 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
3128 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
3129 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003130
3131 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
3132 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
3133 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
3134 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003135 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
3136
Doug Anderson2867c052014-08-07 12:48:11 -07003137 /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
Douglas Andersonf6194732015-12-17 11:14:54 -08003138 if (hsotg->dr_mode != USB_DR_MODE_HOST) {
3139 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Douglas Anderson99182462015-12-17 11:14:12 -08003140 dwc2_writel(gusbcfg | GUSBCFG_FORCEHOSTMODE,
3141 hsotg->regs + GUSBCFG);
Yunzhi Li20bde642015-12-17 11:15:08 -08003142 usleep_range(25000, 50000);
Douglas Anderson99182462015-12-17 11:14:12 -08003143 }
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003144
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003145 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
3146 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
Doug Anderson2867c052014-08-07 12:48:11 -07003147 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003148 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
Douglas Andersonf6194732015-12-17 11:14:54 -08003149 if (hsotg->dr_mode != USB_DR_MODE_HOST) {
Douglas Anderson99182462015-12-17 11:14:12 -08003150 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Yunzhi Li20bde642015-12-17 11:15:08 -08003151 usleep_range(25000, 50000);
Douglas Anderson99182462015-12-17 11:14:12 -08003152 }
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003153
3154 /* hwcfg2 */
3155 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
3156 GHWCFG2_OP_MODE_SHIFT;
3157 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
3158 GHWCFG2_ARCHITECTURE_SHIFT;
3159 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
3160 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
3161 GHWCFG2_NUM_HOST_CHAN_SHIFT);
3162 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
3163 GHWCFG2_HS_PHY_TYPE_SHIFT;
3164 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
3165 GHWCFG2_FS_PHY_TYPE_SHIFT;
3166 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
3167 GHWCFG2_NUM_DEV_EP_SHIFT;
3168 hw->nperio_tx_q_depth =
3169 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
3170 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
3171 hw->host_perio_tx_q_depth =
3172 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
3173 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
3174 hw->dev_token_q_depth =
3175 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
3176 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
3177
3178 /* hwcfg3 */
3179 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
3180 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
3181 hw->max_transfer_size = (1 << (width + 11)) - 1;
Paul Zimmermane8f8c142014-09-16 13:47:26 -07003182 /*
3183 * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
3184 * coherent buffers with this size, and if it's too large we can
3185 * exhaust the coherent DMA pool.
3186 */
3187 if (hw->max_transfer_size > 65535)
3188 hw->max_transfer_size = 65535;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003189 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
3190 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
3191 hw->max_packet_count = (1 << (width + 4)) - 1;
3192 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
3193 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
3194 GHWCFG3_DFIFO_DEPTH_SHIFT;
3195
3196 /* hwcfg4 */
3197 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
3198 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
3199 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
3200 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
3201 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003202 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
3203 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003204
3205 /* fifo sizes */
3206 hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
3207 GRXFSIZ_DEPTH_SHIFT;
3208 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3209 FIFOSIZE_DEPTH_SHIFT;
3210 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3211 FIFOSIZE_DEPTH_SHIFT;
3212
3213 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
3214 dev_dbg(hsotg->dev, " op_mode=%d\n",
3215 hw->op_mode);
3216 dev_dbg(hsotg->dev, " arch=%d\n",
3217 hw->arch);
3218 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
3219 hw->dma_desc_enable);
3220 dev_dbg(hsotg->dev, " power_optimized=%d\n",
3221 hw->power_optimized);
3222 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
3223 hw->i2c_enable);
3224 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
3225 hw->hs_phy_type);
3226 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
3227 hw->fs_phy_type);
Masanari Iida971bd8f2015-05-20 23:54:02 +09003228 dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003229 hw->utmi_phy_data_width);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003230 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
3231 hw->num_dev_ep);
3232 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
3233 hw->num_dev_perio_in_ep);
3234 dev_dbg(hsotg->dev, " host_channels=%d\n",
3235 hw->host_channels);
3236 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
3237 hw->max_transfer_size);
3238 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
3239 hw->max_packet_count);
3240 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
3241 hw->nperio_tx_q_depth);
3242 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
3243 hw->host_perio_tx_q_depth);
3244 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
3245 hw->dev_token_q_depth);
3246 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
3247 hw->enable_dynamic_fifo);
3248 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
3249 hw->en_multiple_tx_fifo);
3250 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
3251 hw->total_fifo_size);
3252 dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
3253 hw->host_rx_fifo_size);
3254 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
3255 hw->host_nperio_tx_fifo_size);
3256 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
3257 hw->host_perio_tx_fifo_size);
3258 dev_dbg(hsotg->dev, "\n");
3259
3260 return 0;
3261}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02003262
3263/*
3264 * Sets all parameters to the given value.
3265 *
3266 * Assumes that the dwc2_core_params struct contains only integers.
3267 */
3268void dwc2_set_all_params(struct dwc2_core_params *params, int value)
3269{
3270 int *p = (int *)params;
3271 size_t size = sizeof(*params) / sizeof(*p);
3272 int i;
3273
3274 for (i = 0; i < size; i++)
3275 p[i] = value;
3276}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02003277
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003278
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003279u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
3280{
Paul Zimmermanb66a3f02013-11-22 16:43:50 -08003281 return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003282}
3283
Paul Zimmerman057715f2013-11-22 16:43:51 -08003284bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003285{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003286 if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
Paul Zimmerman057715f2013-11-22 16:43:51 -08003287 return false;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003288 else
Paul Zimmerman057715f2013-11-22 16:43:51 -08003289 return true;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003290}
3291
3292/**
3293 * dwc2_enable_global_interrupts() - Enables the controller's Global
3294 * Interrupt in the AHB Config register
3295 *
3296 * @hsotg: Programming view of DWC_otg controller
3297 */
3298void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
3299{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003300 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003301
3302 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003303 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003304}
3305
3306/**
3307 * dwc2_disable_global_interrupts() - Disables the controller's Global
3308 * Interrupt in the AHB Config register
3309 *
3310 * @hsotg: Programming view of DWC_otg controller
3311 */
3312void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
3313{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003314 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003315
3316 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003317 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003318}
3319
3320MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
3321MODULE_AUTHOR("Synopsys, Inc.");
3322MODULE_LICENSE("Dual BSD/GPL");