blob: fa9b26b915071ada49ee1b068ff098c09a962ff1 [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/**
60 * dwc2_backup_global_registers() - Backup global controller registers.
61 * When suspending usb bus, registers needs to be backuped
62 * if controller power is disabled once suspended.
63 *
64 * @hsotg: Programming view of the DWC_otg controller
65 */
66static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
67{
68 struct dwc2_gregs_backup *gr;
69 int i;
70
71 /* Backup global regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +020072 gr = &hsotg->gr_backup;
Gregory Herrerod17ee772015-04-29 22:09:01 +020073
Antti Seppälä95c8bc32015-08-20 21:41:07 +030074 gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
75 gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
76 gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
77 gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
78 gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
79 gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
80 gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
81 gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
Gregory Herrerod17ee772015-04-29 22:09:01 +020082 for (i = 0; i < MAX_EPS_CHANNELS; i++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +030083 gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +020084
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +020085 gr->valid = true;
Gregory Herrerod17ee772015-04-29 22:09:01 +020086 return 0;
87}
88
89/**
90 * dwc2_restore_global_registers() - Restore controller global registers.
91 * When resuming usb bus, device registers needs to be restored
92 * if controller power were disabled.
93 *
94 * @hsotg: Programming view of the DWC_otg controller
95 */
96static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
97{
98 struct dwc2_gregs_backup *gr;
99 int i;
100
101 dev_dbg(hsotg->dev, "%s\n", __func__);
102
103 /* Restore global regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200104 gr = &hsotg->gr_backup;
105 if (!gr->valid) {
Gregory Herrerod17ee772015-04-29 22:09:01 +0200106 dev_err(hsotg->dev, "%s: no global registers to restore\n",
107 __func__);
108 return -EINVAL;
109 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200110 gr->valid = false;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200111
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300112 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
113 dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
114 dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
115 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
116 dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
117 dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
118 dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
119 dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
120 dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200121 for (i = 0; i < MAX_EPS_CHANNELS; i++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300122 dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200123
124 return 0;
125}
126
127/**
128 * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
129 *
130 * @hsotg: Programming view of the DWC_otg controller
131 * @restore: Controller registers need to be restored
132 */
133int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
134{
135 u32 pcgcctl;
136 int ret = 0;
137
Gregory Herrero285046a2015-04-29 22:09:19 +0200138 if (!hsotg->core_params->hibernation)
139 return -ENOTSUPP;
140
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300141 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200142 pcgcctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300143 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200144
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300145 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200146 pcgcctl &= ~PCGCTL_PWRCLMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300147 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200148
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300149 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200150 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300151 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200152
153 udelay(100);
154 if (restore) {
155 ret = dwc2_restore_global_registers(hsotg);
156 if (ret) {
157 dev_err(hsotg->dev, "%s: failed to restore registers\n",
158 __func__);
159 return ret;
160 }
161 if (dwc2_is_host_mode(hsotg)) {
162 ret = dwc2_restore_host_registers(hsotg);
163 if (ret) {
164 dev_err(hsotg->dev, "%s: failed to restore host registers\n",
165 __func__);
166 return ret;
167 }
168 } else {
169 ret = dwc2_restore_device_registers(hsotg);
170 if (ret) {
171 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
172 __func__);
173 return ret;
174 }
175 }
176 }
177
178 return ret;
179}
180
181/**
182 * dwc2_enter_hibernation() - Put controller in Partial Power Down.
183 *
184 * @hsotg: Programming view of the DWC_otg controller
185 */
186int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
187{
188 u32 pcgcctl;
189 int ret = 0;
190
Gregory Herrero285046a2015-04-29 22:09:19 +0200191 if (!hsotg->core_params->hibernation)
192 return -ENOTSUPP;
193
Gregory Herrerod17ee772015-04-29 22:09:01 +0200194 /* Backup all registers */
195 ret = dwc2_backup_global_registers(hsotg);
196 if (ret) {
197 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
198 __func__);
199 return ret;
200 }
201
202 if (dwc2_is_host_mode(hsotg)) {
203 ret = dwc2_backup_host_registers(hsotg);
204 if (ret) {
205 dev_err(hsotg->dev, "%s: failed to backup host registers\n",
206 __func__);
207 return ret;
208 }
209 } else {
210 ret = dwc2_backup_device_registers(hsotg);
211 if (ret) {
212 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
213 __func__);
214 return ret;
215 }
216 }
217
Gregory Herrerocad73da2015-09-22 15:16:49 +0200218 /*
219 * Clear any pending interrupts since dwc2 will not be able to
220 * clear them after entering hibernation.
221 */
222 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
223
Gregory Herrerod17ee772015-04-29 22:09:01 +0200224 /* Put the controller in low power state */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300225 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200226
227 pcgcctl |= PCGCTL_PWRCLMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300228 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200229 ndelay(20);
230
231 pcgcctl |= PCGCTL_RSTPDWNMODULE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300232 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200233 ndelay(20);
234
235 pcgcctl |= PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300236 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200237
238 return ret;
239}
240
John Younfef6bc32016-09-07 19:39:40 -0700241/**
242 * dwc2_wait_for_mode() - Waits for the controller mode.
243 * @hsotg: Programming view of the DWC_otg controller.
244 * @host_mode: If true, waits for host mode, otherwise device mode.
245 */
246static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
247 bool host_mode)
248{
249 ktime_t start;
250 ktime_t end;
251 unsigned int timeout = 110;
252
253 dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
254 host_mode ? "host" : "device");
255
256 start = ktime_get();
257
258 while (1) {
259 s64 ms;
260
261 if (dwc2_is_host_mode(hsotg) == host_mode) {
262 dev_vdbg(hsotg->dev, "%s mode set\n",
263 host_mode ? "Host" : "Device");
264 break;
265 }
266
267 end = ktime_get();
268 ms = ktime_to_ms(ktime_sub(end, start));
269
270 if (ms >= (s64)timeout) {
271 dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
272 __func__, host_mode ? "host" : "device");
273 break;
274 }
275
276 usleep_range(1000, 2000);
277 }
278}
279
280/**
281 * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
282 * filter is enabled.
283 */
284static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
285{
286 u32 gsnpsid;
287 u32 ghwcfg4;
288
289 if (!dwc2_hw_is_otg(hsotg))
290 return false;
291
292 /* Check if core configuration includes the IDDIG filter. */
293 ghwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
294 if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
295 return false;
296
297 /*
298 * Check if the IDDIG debounce filter is bypassed. Available
299 * in core version >= 3.10a.
300 */
301 gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
302 if (gsnpsid >= DWC2_CORE_REV_3_10a) {
303 u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
304
305 if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
306 return false;
307 }
308
309 return true;
310}
311
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700312/*
313 * Do core a soft reset of the core. Be careful with this because it
314 * resets all the internal state machines of the core.
315 */
John Younb5d308a2015-12-17 11:16:03 -0800316int dwc2_core_reset(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700317{
318 u32 greset;
319 int count = 0;
John Younfef6bc32016-09-07 19:39:40 -0700320 bool wait_for_host_mode = false;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700321
322 dev_vdbg(hsotg->dev, "%s()\n", __func__);
323
John Younfef6bc32016-09-07 19:39:40 -0700324 /*
325 * If the current mode is host, either due to the force mode
326 * bit being set (which persists after core reset) or the
327 * connector id pin, a core soft reset will temporarily reset
328 * the mode to device. A delay from the IDDIG debounce filter
329 * will occur before going back to host mode.
330 *
331 * Determine whether we will go back into host mode after a
332 * reset and account for this delay after the reset.
333 */
334 if (dwc2_iddig_filter_enabled(hsotg)) {
335 u32 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
336 u32 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
337
338 if (!(gotgctl & GOTGCTL_CONID_B) ||
339 (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
340 wait_for_host_mode = true;
341 }
342 }
343
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700344 /* Core Soft Reset */
John Younb8ccc592015-12-17 11:15:35 -0800345 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700346 greset |= GRSTCTL_CSFTRST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300347 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700348 do {
Yunzhi Li20bde642015-12-17 11:15:08 -0800349 udelay(1);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300350 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700351 if (++count > 50) {
352 dev_warn(hsotg->dev,
353 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
354 __func__, greset);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100355 return -EBUSY;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700356 }
357 } while (greset & GRSTCTL_CSFTRST);
358
John Younb8ccc592015-12-17 11:15:35 -0800359 /* Wait for AHB master IDLE state */
360 count = 0;
361 do {
362 udelay(1);
363 greset = dwc2_readl(hsotg->regs + GRSTCTL);
364 if (++count > 50) {
365 dev_warn(hsotg->dev,
366 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
367 __func__, greset);
368 return -EBUSY;
369 }
370 } while (!(greset & GRSTCTL_AHBIDLE));
371
John Younfef6bc32016-09-07 19:39:40 -0700372 if (wait_for_host_mode)
373 dwc2_wait_for_mode(hsotg, true);
374
John Younb5d308a2015-12-17 11:16:03 -0800375 return 0;
376}
377
378/*
John Youn09c96982015-12-17 11:17:12 -0800379 * Force the mode of the controller.
380 *
381 * Forcing the mode is needed for two cases:
382 *
383 * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
384 * controller to stay in a particular mode regardless of ID pin
385 * changes. We do this usually after a core reset.
386 *
387 * 2) During probe we want to read reset values of the hw
388 * configuration registers that are only available in either host or
389 * device mode. We may need to force the mode if the current mode does
390 * not allow us to access the register in the mode that we want.
391 *
392 * In either case it only makes sense to force the mode if the
393 * controller hardware is OTG capable.
394 *
395 * Checks are done in this function to determine whether doing a force
396 * would be valid or not.
397 *
John Youn2938fc62016-09-07 19:39:43 -0700398 * If a force is done, it requires a IDDIG debounce filter delay if
399 * the filter is configured and enabled. We poll the current mode of
400 * the controller to account for this delay.
John Youn09c96982015-12-17 11:17:12 -0800401 */
402static bool dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
403{
404 u32 gusbcfg;
405 u32 set;
406 u32 clear;
407
408 dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
409
410 /*
411 * Force mode has no effect if the hardware is not OTG.
412 */
413 if (!dwc2_hw_is_otg(hsotg))
414 return false;
415
416 /*
417 * If dr_mode is either peripheral or host only, there is no
418 * need to ever force the mode to the opposite mode.
419 */
420 if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
421 return false;
422
423 if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
424 return false;
425
426 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
427
428 set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
429 clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
430
John Youn09c96982015-12-17 11:17:12 -0800431 gusbcfg &= ~clear;
432 gusbcfg |= set;
433 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
434
John Youn2938fc62016-09-07 19:39:43 -0700435 dwc2_wait_for_mode(hsotg, host);
John Youn09c96982015-12-17 11:17:12 -0800436 return true;
437}
438
John Youn2938fc62016-09-07 19:39:43 -0700439/**
440 * dwc2_clear_force_mode() - Clears the force mode bits.
441 *
442 * After clearing the bits, wait up to 100 ms to account for any
443 * potential IDDIG filter delay. We can't know if we expect this delay
444 * or not because the value of the connector ID status is affected by
445 * the force mode. We only need to call this once during probe if
446 * dr_mode == OTG.
John Youn09c96982015-12-17 11:17:12 -0800447 */
448static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
449{
450 u32 gusbcfg;
451
452 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
453 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
454 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
455 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
456
John Youn2938fc62016-09-07 19:39:43 -0700457 if (dwc2_iddig_filter_enabled(hsotg))
458 usleep_range(100000, 110000);
John Youn09c96982015-12-17 11:17:12 -0800459}
460
461/*
462 * Sets or clears force mode based on the dr_mode parameter.
463 */
464void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
465{
466 switch (hsotg->dr_mode) {
467 case USB_DR_MODE_HOST:
468 dwc2_force_mode(hsotg, true);
469 break;
470 case USB_DR_MODE_PERIPHERAL:
471 dwc2_force_mode(hsotg, false);
472 break;
473 case USB_DR_MODE_OTG:
474 dwc2_clear_force_mode(hsotg);
475 break;
476 default:
477 dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
478 __func__, hsotg->dr_mode);
479 break;
480 }
481}
482
483/*
John Younb5d308a2015-12-17 11:16:03 -0800484 * Do core a soft reset of the core. Be careful with this because it
485 * resets all the internal state machines of the core.
486 *
487 * Additionally this will apply force mode as per the hsotg->dr_mode
488 * parameter.
489 */
490int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg)
491{
492 int retval;
John Younb5d308a2015-12-17 11:16:03 -0800493
494 retval = dwc2_core_reset(hsotg);
495 if (retval)
496 return retval;
497
John Youn09c96982015-12-17 11:17:12 -0800498 dwc2_force_dr_mode(hsotg);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100499 return 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700500}
501
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700502/**
503 * dwc2_dump_host_registers() - Prints the host registers
504 *
505 * @hsotg: Programming view of DWC_otg controller
506 *
507 * NOTE: This function will be removed once the peripheral controller code
508 * is integrated and the driver is stable
509 */
510void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
511{
512#ifdef DEBUG
513 u32 __iomem *addr;
514 int i;
515
516 dev_dbg(hsotg->dev, "Host Global Registers\n");
517 addr = hsotg->regs + HCFG;
518 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300519 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700520 addr = hsotg->regs + HFIR;
521 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300522 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700523 addr = hsotg->regs + HFNUM;
524 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300525 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700526 addr = hsotg->regs + HPTXSTS;
527 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300528 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700529 addr = hsotg->regs + HAINT;
530 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300531 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700532 addr = hsotg->regs + HAINTMSK;
533 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300534 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700535 if (hsotg->core_params->dma_desc_enable > 0) {
536 addr = hsotg->regs + HFLBADDR;
537 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300538 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700539 }
540
541 addr = hsotg->regs + HPRT0;
542 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300543 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700544
545 for (i = 0; i < hsotg->core_params->host_channels; i++) {
546 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
547 addr = hsotg->regs + HCCHAR(i);
548 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300549 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700550 addr = hsotg->regs + HCSPLT(i);
551 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300552 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700553 addr = hsotg->regs + HCINT(i);
554 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300555 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700556 addr = hsotg->regs + HCINTMSK(i);
557 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300558 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700559 addr = hsotg->regs + HCTSIZ(i);
560 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300561 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700562 addr = hsotg->regs + HCDMA(i);
563 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300564 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700565 if (hsotg->core_params->dma_desc_enable > 0) {
566 addr = hsotg->regs + HCDMAB(i);
567 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300568 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700569 }
570 }
571#endif
572}
573
574/**
575 * dwc2_dump_global_registers() - Prints the core global registers
576 *
577 * @hsotg: Programming view of DWC_otg controller
578 *
579 * NOTE: This function will be removed once the peripheral controller code
580 * is integrated and the driver is stable
581 */
582void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
583{
584#ifdef DEBUG
585 u32 __iomem *addr;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700586
587 dev_dbg(hsotg->dev, "Core Global Registers\n");
588 addr = hsotg->regs + GOTGCTL;
589 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300590 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700591 addr = hsotg->regs + GOTGINT;
592 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300593 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700594 addr = hsotg->regs + GAHBCFG;
595 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300596 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700597 addr = hsotg->regs + GUSBCFG;
598 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300599 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700600 addr = hsotg->regs + GRSTCTL;
601 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300602 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700603 addr = hsotg->regs + GINTSTS;
604 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300605 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700606 addr = hsotg->regs + GINTMSK;
607 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300608 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700609 addr = hsotg->regs + GRXSTSR;
610 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300611 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700612 addr = hsotg->regs + GRXFSIZ;
613 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300614 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700615 addr = hsotg->regs + GNPTXFSIZ;
616 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300617 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700618 addr = hsotg->regs + GNPTXSTS;
619 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300620 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700621 addr = hsotg->regs + GI2CCTL;
622 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300623 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700624 addr = hsotg->regs + GPVNDCTL;
625 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300626 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700627 addr = hsotg->regs + GGPIO;
628 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300629 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700630 addr = hsotg->regs + GUID;
631 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300632 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700633 addr = hsotg->regs + GSNPSID;
634 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300635 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700636 addr = hsotg->regs + GHWCFG1;
637 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300638 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700639 addr = hsotg->regs + GHWCFG2;
640 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300641 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700642 addr = hsotg->regs + GHWCFG3;
643 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300644 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700645 addr = hsotg->regs + GHWCFG4;
646 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300647 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700648 addr = hsotg->regs + GLPMCFG;
649 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300650 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700651 addr = hsotg->regs + GPWRDN;
652 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300653 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700654 addr = hsotg->regs + GDFIFOCFG;
655 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300656 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700657 addr = hsotg->regs + HPTXFSIZ;
658 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300659 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700660
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700661 addr = hsotg->regs + PCGCTL;
662 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300663 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700664#endif
665}
666
667/**
668 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
669 *
670 * @hsotg: Programming view of DWC_otg controller
671 * @num: Tx FIFO to flush
672 */
673void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
674{
675 u32 greset;
676 int count = 0;
677
678 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
679
680 greset = GRSTCTL_TXFFLSH;
681 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300682 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700683
684 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300685 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700686 if (++count > 10000) {
687 dev_warn(hsotg->dev,
688 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
689 __func__, greset,
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300690 dwc2_readl(hsotg->regs + GNPTXSTS));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700691 break;
692 }
693 udelay(1);
694 } while (greset & GRSTCTL_TXFFLSH);
695
696 /* Wait for at least 3 PHY Clocks */
697 udelay(1);
698}
699
700/**
701 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
702 *
703 * @hsotg: Programming view of DWC_otg controller
704 */
705void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
706{
707 u32 greset;
708 int count = 0;
709
710 dev_vdbg(hsotg->dev, "%s()\n", __func__);
711
712 greset = GRSTCTL_RXFFLSH;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300713 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700714
715 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300716 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700717 if (++count > 10000) {
718 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
719 __func__, greset);
720 break;
721 }
722 udelay(1);
723 } while (greset & GRSTCTL_RXFFLSH);
724
725 /* Wait for at least 3 PHY Clocks */
726 udelay(1);
727}
728
Paul Zimmerman498f0662013-11-22 16:43:47 -0800729#define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700730
731/* Parameter access functions */
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800732void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700733{
734 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700735
736 switch (val) {
737 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200738 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700739 valid = 0;
740 break;
741 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200742 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700743 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
744 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
745 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
746 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
747 break;
748 default:
749 valid = 0;
750 break;
751 }
752 break;
753 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
754 /* always valid */
755 break;
756 default:
757 valid = 0;
758 break;
759 }
760
761 if (!valid) {
762 if (val >= 0)
763 dev_err(hsotg->dev,
764 "%d invalid for otg_cap parameter. Check HW configuration.\n",
765 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200766 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700767 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
768 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
769 break;
770 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
771 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
772 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
773 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
774 break;
775 default:
776 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
777 break;
778 }
779 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700780 }
781
782 hsotg->core_params->otg_cap = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700783}
784
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800785void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700786{
787 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700788
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200789 if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700790 valid = 0;
791 if (val < 0)
792 valid = 0;
793
794 if (!valid) {
795 if (val >= 0)
796 dev_err(hsotg->dev,
797 "%d invalid for dma_enable parameter. Check HW configuration.\n",
798 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200799 val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700800 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700801 }
802
803 hsotg->core_params->dma_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700804}
805
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800806void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700807{
808 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700809
810 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200811 !hsotg->hw_params.dma_desc_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700812 valid = 0;
813 if (val < 0)
814 valid = 0;
815
816 if (!valid) {
817 if (val >= 0)
818 dev_err(hsotg->dev,
819 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
820 val);
821 val = (hsotg->core_params->dma_enable > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200822 hsotg->hw_params.dma_desc_enable);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700823 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700824 }
825
826 hsotg->core_params->dma_desc_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700827}
828
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +0100829void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg, int val)
830{
831 int valid = 1;
832
833 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
834 !hsotg->hw_params.dma_desc_enable))
835 valid = 0;
836 if (val < 0)
837 valid = 0;
838
839 if (!valid) {
840 if (val >= 0)
841 dev_err(hsotg->dev,
842 "%d invalid for dma_desc_fs_enable parameter. Check HW configuration.\n",
843 val);
844 val = (hsotg->core_params->dma_enable > 0 &&
845 hsotg->hw_params.dma_desc_enable);
846 }
847
848 hsotg->core_params->dma_desc_fs_enable = val;
849 dev_dbg(hsotg->dev, "Setting dma_desc_fs_enable to %d\n", val);
850}
851
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800852void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
853 int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700854{
Paul Zimmerman498f0662013-11-22 16:43:47 -0800855 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700856 if (val >= 0) {
857 dev_err(hsotg->dev,
858 "Wrong value for host_support_fs_low_power\n");
859 dev_err(hsotg->dev,
860 "host_support_fs_low_power must be 0 or 1\n");
861 }
862 val = 0;
863 dev_dbg(hsotg->dev,
864 "Setting host_support_fs_low_power to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700865 }
866
867 hsotg->core_params->host_support_fs_ls_low_power = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700868}
869
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800870void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700871{
872 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700873
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200874 if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700875 valid = 0;
876 if (val < 0)
877 valid = 0;
878
879 if (!valid) {
880 if (val >= 0)
881 dev_err(hsotg->dev,
882 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
883 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200884 val = hsotg->hw_params.enable_dynamic_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700885 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700886 }
887
888 hsotg->core_params->enable_dynamic_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700889}
890
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800891void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700892{
893 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700894
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200895 if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700896 valid = 0;
897
898 if (!valid) {
899 if (val >= 0)
900 dev_err(hsotg->dev,
901 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
902 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200903 val = hsotg->hw_params.host_rx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700904 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700905 }
906
907 hsotg->core_params->host_rx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700908}
909
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800910void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700911{
912 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700913
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200914 if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700915 valid = 0;
916
917 if (!valid) {
918 if (val >= 0)
919 dev_err(hsotg->dev,
920 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
921 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200922 val = hsotg->hw_params.host_nperio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700923 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
924 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700925 }
926
927 hsotg->core_params->host_nperio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700928}
929
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800930void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700931{
932 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700933
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200934 if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700935 valid = 0;
936
937 if (!valid) {
938 if (val >= 0)
939 dev_err(hsotg->dev,
940 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
941 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200942 val = hsotg->hw_params.host_perio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700943 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
944 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700945 }
946
947 hsotg->core_params->host_perio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700948}
949
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800950void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700951{
952 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700953
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200954 if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700955 valid = 0;
956
957 if (!valid) {
958 if (val >= 0)
959 dev_err(hsotg->dev,
960 "%d invalid for max_transfer_size. Check HW configuration.\n",
961 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200962 val = hsotg->hw_params.max_transfer_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700963 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700964 }
965
966 hsotg->core_params->max_transfer_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700967}
968
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800969void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700970{
971 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700972
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200973 if (val < 15 || val > hsotg->hw_params.max_packet_count)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700974 valid = 0;
975
976 if (!valid) {
977 if (val >= 0)
978 dev_err(hsotg->dev,
979 "%d invalid for max_packet_count. Check HW configuration.\n",
980 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200981 val = hsotg->hw_params.max_packet_count;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700982 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700983 }
984
985 hsotg->core_params->max_packet_count = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700986}
987
Paul Zimmerman7218dae2013-11-22 16:43:48 -0800988void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700989{
990 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700991
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200992 if (val < 1 || val > hsotg->hw_params.host_channels)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700993 valid = 0;
994
995 if (!valid) {
996 if (val >= 0)
997 dev_err(hsotg->dev,
998 "%d invalid for host_channels. Check HW configuration.\n",
999 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001000 val = hsotg->hw_params.host_channels;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001001 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001002 }
1003
1004 hsotg->core_params->host_channels = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001005}
1006
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001007void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001008{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001009 int valid = 0;
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02001010 u32 hs_phy_type, fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001011
Paul Zimmerman498f0662013-11-22 16:43:47 -08001012 if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
1013 DWC2_PHY_TYPE_PARAM_ULPI)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001014 if (val >= 0) {
1015 dev_err(hsotg->dev, "Wrong value for phy_type\n");
1016 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
1017 }
1018
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001019 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001020 }
1021
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001022 hs_phy_type = hsotg->hw_params.hs_phy_type;
1023 fs_phy_type = hsotg->hw_params.fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001024 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
1025 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
1026 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
1027 valid = 1;
1028 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
1029 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
1030 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
1031 valid = 1;
1032 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
1033 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
1034 valid = 1;
1035
1036 if (!valid) {
1037 if (val >= 0)
1038 dev_err(hsotg->dev,
1039 "%d invalid for phy_type. Check HW configuration.\n",
1040 val);
Matthijs Kooijman929aea02013-04-29 19:36:48 +00001041 val = DWC2_PHY_TYPE_PARAM_FS;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001042 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
1043 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
1044 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
1045 val = DWC2_PHY_TYPE_PARAM_UTMI;
1046 else
1047 val = DWC2_PHY_TYPE_PARAM_ULPI;
1048 }
1049 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001050 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001051
1052 hsotg->core_params->phy_type = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001053}
1054
1055static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
1056{
1057 return hsotg->core_params->phy_type;
1058}
1059
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001060void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001061{
1062 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001063
Paul Zimmerman498f0662013-11-22 16:43:47 -08001064 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001065 if (val >= 0) {
1066 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
1067 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
1068 }
1069 valid = 0;
1070 }
1071
Matthijs Kooijman929aea02013-04-29 19:36:48 +00001072 if (val == DWC2_SPEED_PARAM_HIGH &&
1073 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001074 valid = 0;
1075
1076 if (!valid) {
1077 if (val >= 0)
1078 dev_err(hsotg->dev,
1079 "%d invalid for speed parameter. Check HW configuration.\n",
1080 val);
1081 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
Matthijs Kooijman929aea02013-04-29 19:36:48 +00001082 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001083 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001084 }
1085
1086 hsotg->core_params->speed = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001087}
1088
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001089void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001090{
1091 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001092
Paul Zimmerman498f0662013-11-22 16:43:47 -08001093 if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
1094 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001095 if (val >= 0) {
1096 dev_err(hsotg->dev,
1097 "Wrong value for host_ls_low_power_phy_clk parameter\n");
1098 dev_err(hsotg->dev,
1099 "host_ls_low_power_phy_clk must be 0 or 1\n");
1100 }
1101 valid = 0;
1102 }
1103
1104 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
1105 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
1106 valid = 0;
1107
1108 if (!valid) {
1109 if (val >= 0)
1110 dev_err(hsotg->dev,
1111 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
1112 val);
1113 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
1114 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
1115 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
1116 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
1117 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001118 }
1119
1120 hsotg->core_params->host_ls_low_power_phy_clk = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001121}
1122
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001123void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001124{
Paul Zimmerman498f0662013-11-22 16:43:47 -08001125 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001126 if (val >= 0) {
1127 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
1128 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
1129 }
1130 val = 0;
1131 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001132 }
1133
1134 hsotg->core_params->phy_ulpi_ddr = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001135}
1136
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001137void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001138{
Paul Zimmerman498f0662013-11-22 16:43:47 -08001139 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001140 if (val >= 0) {
1141 dev_err(hsotg->dev,
1142 "Wrong value for phy_ulpi_ext_vbus\n");
1143 dev_err(hsotg->dev,
1144 "phy_ulpi_ext_vbus must be 0 or 1\n");
1145 }
1146 val = 0;
1147 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001148 }
1149
1150 hsotg->core_params->phy_ulpi_ext_vbus = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001151}
1152
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001153void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001154{
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02001155 int valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001156
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02001157 switch (hsotg->hw_params.utmi_phy_data_width) {
1158 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
1159 valid = (val == 8);
1160 break;
1161 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
1162 valid = (val == 16);
1163 break;
1164 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
1165 valid = (val == 8 || val == 16);
1166 break;
1167 }
1168
1169 if (!valid) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001170 if (val >= 0) {
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02001171 dev_err(hsotg->dev,
1172 "%d invalid for phy_utmi_width. Check HW configuration.\n",
1173 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001174 }
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02001175 val = (hsotg->hw_params.utmi_phy_data_width ==
1176 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001177 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001178 }
1179
1180 hsotg->core_params->phy_utmi_width = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001181}
1182
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001183void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001184{
Paul Zimmerman498f0662013-11-22 16:43:47 -08001185 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001186 if (val >= 0) {
1187 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
1188 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
1189 }
1190 val = 0;
1191 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001192 }
1193
1194 hsotg->core_params->ulpi_fs_ls = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001195}
1196
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001197void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001198{
Paul Zimmerman498f0662013-11-22 16:43:47 -08001199 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001200 if (val >= 0) {
1201 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
1202 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
1203 }
1204 val = 0;
1205 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001206 }
1207
1208 hsotg->core_params->ts_dline = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001209}
1210
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001211void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001212{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001213 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001214
Paul Zimmerman498f0662013-11-22 16:43:47 -08001215 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001216 if (val >= 0) {
1217 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
1218 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
1219 }
1220
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001221 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001222 }
1223
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001224 if (val == 1 && !(hsotg->hw_params.i2c_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001225 valid = 0;
1226
1227 if (!valid) {
1228 if (val >= 0)
1229 dev_err(hsotg->dev,
1230 "%d invalid for i2c_enable. Check HW configuration.\n",
1231 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001232 val = hsotg->hw_params.i2c_enable;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001233 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001234 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001235
1236 hsotg->core_params->i2c_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001237}
1238
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001239void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001240{
1241 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001242
Paul Zimmerman498f0662013-11-22 16:43:47 -08001243 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001244 if (val >= 0) {
1245 dev_err(hsotg->dev,
1246 "Wrong value for en_multiple_tx_fifo,\n");
1247 dev_err(hsotg->dev,
1248 "en_multiple_tx_fifo must be 0 or 1\n");
1249 }
1250 valid = 0;
1251 }
1252
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001253 if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001254 valid = 0;
1255
1256 if (!valid) {
1257 if (val >= 0)
1258 dev_err(hsotg->dev,
1259 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
1260 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001261 val = hsotg->hw_params.en_multiple_tx_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001262 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001263 }
1264
1265 hsotg->core_params->en_multiple_tx_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001266}
1267
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001268void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001269{
1270 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001271
Paul Zimmerman498f0662013-11-22 16:43:47 -08001272 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001273 if (val >= 0) {
1274 dev_err(hsotg->dev,
1275 "'%d' invalid for parameter reload_ctl\n", val);
1276 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
1277 }
1278 valid = 0;
1279 }
1280
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001281 if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001282 valid = 0;
1283
1284 if (!valid) {
1285 if (val >= 0)
1286 dev_err(hsotg->dev,
1287 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
1288 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001289 val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001290 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001291 }
1292
1293 hsotg->core_params->reload_ctl = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001294}
1295
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001296void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001297{
Paul Zimmerman4d3190e2013-07-16 12:22:12 -07001298 if (val != -1)
1299 hsotg->core_params->ahbcfg = val;
1300 else
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02001301 hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02001302 GAHBCFG_HBSTLEN_SHIFT;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001303}
1304
Paul Zimmerman7218dae2013-11-22 16:43:48 -08001305void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001306{
Paul Zimmerman498f0662013-11-22 16:43:47 -08001307 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001308 if (val >= 0) {
1309 dev_err(hsotg->dev,
1310 "'%d' invalid for parameter otg_ver\n", val);
1311 dev_err(hsotg->dev,
1312 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
1313 }
1314 val = 0;
1315 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001316 }
1317
1318 hsotg->core_params->otg_ver = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001319}
1320
Wei Yongjun49cf10c2013-11-28 10:27:59 +08001321static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
Paul Zimmermane8576e62013-11-25 13:42:47 -08001322{
1323 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1324 if (val >= 0) {
1325 dev_err(hsotg->dev,
1326 "'%d' invalid for parameter uframe_sched\n",
1327 val);
1328 dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
1329 }
1330 val = 1;
1331 dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
1332 }
1333
1334 hsotg->core_params->uframe_sched = val;
1335}
1336
Gregory Herreroa6d249d2015-04-29 22:09:04 +02001337static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
1338 int val)
1339{
1340 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1341 if (val >= 0) {
1342 dev_err(hsotg->dev,
1343 "'%d' invalid for parameter external_id_pin_ctl\n",
1344 val);
1345 dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
1346 }
1347 val = 0;
1348 dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
1349 }
1350
1351 hsotg->core_params->external_id_pin_ctl = val;
1352}
1353
Gregory Herrero285046a2015-04-29 22:09:19 +02001354static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
1355 int val)
1356{
1357 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
1358 if (val >= 0) {
1359 dev_err(hsotg->dev,
1360 "'%d' invalid for parameter hibernation\n",
1361 val);
1362 dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
1363 }
1364 val = 0;
1365 dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
1366 }
1367
1368 hsotg->core_params->hibernation = val;
1369}
1370
Paul Zimmermane8576e62013-11-25 13:42:47 -08001371/*
1372 * This function is called during module intialization to pass module parameters
1373 * for the DWC_otg core.
1374 */
1375void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
1376 const struct dwc2_core_params *params)
1377{
1378 dev_dbg(hsotg->dev, "%s()\n", __func__);
1379
1380 dwc2_set_param_otg_cap(hsotg, params->otg_cap);
1381 dwc2_set_param_dma_enable(hsotg, params->dma_enable);
1382 dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01001383 dwc2_set_param_dma_desc_fs_enable(hsotg, params->dma_desc_fs_enable);
Paul Zimmermane8576e62013-11-25 13:42:47 -08001384 dwc2_set_param_host_support_fs_ls_low_power(hsotg,
1385 params->host_support_fs_ls_low_power);
1386 dwc2_set_param_enable_dynamic_fifo(hsotg,
1387 params->enable_dynamic_fifo);
1388 dwc2_set_param_host_rx_fifo_size(hsotg,
1389 params->host_rx_fifo_size);
1390 dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
1391 params->host_nperio_tx_fifo_size);
1392 dwc2_set_param_host_perio_tx_fifo_size(hsotg,
1393 params->host_perio_tx_fifo_size);
1394 dwc2_set_param_max_transfer_size(hsotg,
1395 params->max_transfer_size);
1396 dwc2_set_param_max_packet_count(hsotg,
1397 params->max_packet_count);
1398 dwc2_set_param_host_channels(hsotg, params->host_channels);
1399 dwc2_set_param_phy_type(hsotg, params->phy_type);
1400 dwc2_set_param_speed(hsotg, params->speed);
1401 dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
1402 params->host_ls_low_power_phy_clk);
1403 dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
1404 dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
1405 params->phy_ulpi_ext_vbus);
1406 dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
1407 dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
1408 dwc2_set_param_ts_dline(hsotg, params->ts_dline);
1409 dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
1410 dwc2_set_param_en_multiple_tx_fifo(hsotg,
1411 params->en_multiple_tx_fifo);
1412 dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
1413 dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
1414 dwc2_set_param_otg_ver(hsotg, params->otg_ver);
1415 dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
Gregory Herreroa6d249d2015-04-29 22:09:04 +02001416 dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
Gregory Herrero285046a2015-04-29 22:09:19 +02001417 dwc2_set_param_hibernation(hsotg, params->hibernation);
Paul Zimmermane8576e62013-11-25 13:42:47 -08001418}
1419
John Youn09c96982015-12-17 11:17:12 -08001420/*
1421 * Forces either host or device mode if the controller is not
1422 * currently in that mode.
1423 *
1424 * Returns true if the mode was forced.
1425 */
1426static bool dwc2_force_mode_if_needed(struct dwc2_hsotg *hsotg, bool host)
1427{
1428 if (host && dwc2_is_host_mode(hsotg))
1429 return false;
1430 else if (!host && dwc2_is_device_mode(hsotg))
1431 return false;
1432
1433 return dwc2_force_mode(hsotg, host);
1434}
1435
John Youn55e10402015-12-17 11:17:31 -08001436/*
1437 * Gets host hardware parameters. Forces host mode if not currently in
1438 * host mode. Should be called immediately after a core soft reset in
1439 * order to get the reset values.
1440 */
1441static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
1442{
1443 struct dwc2_hw_params *hw = &hsotg->hw_params;
1444 u32 gnptxfsiz;
1445 u32 hptxfsiz;
1446 bool forced;
1447
1448 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
1449 return;
1450
1451 forced = dwc2_force_mode_if_needed(hsotg, true);
1452
1453 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
1454 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
1455 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
1456 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
1457
1458 if (forced)
1459 dwc2_clear_force_mode(hsotg);
1460
1461 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
1462 FIFOSIZE_DEPTH_SHIFT;
1463 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
1464 FIFOSIZE_DEPTH_SHIFT;
1465}
1466
1467/*
1468 * Gets device hardware parameters. Forces device mode if not
1469 * currently in device mode. Should be called immediately after a core
1470 * soft reset in order to get the reset values.
1471 */
1472static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
1473{
1474 struct dwc2_hw_params *hw = &hsotg->hw_params;
1475 bool forced;
1476 u32 gnptxfsiz;
1477
1478 if (hsotg->dr_mode == USB_DR_MODE_HOST)
1479 return;
1480
1481 forced = dwc2_force_mode_if_needed(hsotg, false);
1482
1483 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
1484 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
1485
1486 if (forced)
1487 dwc2_clear_force_mode(hsotg);
1488
1489 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
1490 FIFOSIZE_DEPTH_SHIFT;
1491}
1492
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001493/**
1494 * During device initialization, read various hardware configuration
1495 * registers and interpret the contents.
1496 */
1497int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
1498{
1499 struct dwc2_hw_params *hw = &hsotg->hw_params;
1500 unsigned width;
1501 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
John Youn55e10402015-12-17 11:17:31 -08001502 u32 grxfsiz;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001503
1504 /*
1505 * Attempt to ensure this device is really a DWC_otg Controller.
1506 * Read and verify the GSNPSID register contents. The value should be
1507 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
1508 * as in "OTG version 2.xx" or "OTG version 3.xx".
1509 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001510 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001511 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
1512 (hw->snpsid & 0xfffff000) != 0x4f543000) {
1513 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
1514 hw->snpsid);
1515 return -ENODEV;
1516 }
1517
1518 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
1519 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
1520 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
1521
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001522 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
1523 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
1524 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
1525 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
1526 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001527
1528 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
1529 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
1530 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
1531 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001532 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
1533
John Youn55e10402015-12-17 11:17:31 -08001534 /*
1535 * Host specific hardware parameters. Reading these parameters
1536 * requires the controller to be in host mode. The mode will
1537 * be forced, if necessary, to read these values.
1538 */
1539 dwc2_get_host_hwparams(hsotg);
1540 dwc2_get_dev_hwparams(hsotg);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001541
John Youn55e10402015-12-17 11:17:31 -08001542 /* hwcfg1 */
1543 hw->dev_ep_dirs = hwcfg1;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001544
1545 /* hwcfg2 */
1546 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
1547 GHWCFG2_OP_MODE_SHIFT;
1548 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
1549 GHWCFG2_ARCHITECTURE_SHIFT;
1550 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
1551 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
1552 GHWCFG2_NUM_HOST_CHAN_SHIFT);
1553 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
1554 GHWCFG2_HS_PHY_TYPE_SHIFT;
1555 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
1556 GHWCFG2_FS_PHY_TYPE_SHIFT;
1557 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
1558 GHWCFG2_NUM_DEV_EP_SHIFT;
1559 hw->nperio_tx_q_depth =
1560 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
1561 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
1562 hw->host_perio_tx_q_depth =
1563 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
1564 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
1565 hw->dev_token_q_depth =
1566 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
1567 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
1568
1569 /* hwcfg3 */
1570 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
1571 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
1572 hw->max_transfer_size = (1 << (width + 11)) - 1;
1573 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
1574 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
1575 hw->max_packet_count = (1 << (width + 4)) - 1;
1576 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
1577 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
1578 GHWCFG3_DFIFO_DEPTH_SHIFT;
1579
1580 /* hwcfg4 */
1581 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
1582 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
1583 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
1584 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
1585 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02001586 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
1587 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001588
1589 /* fifo sizes */
1590 hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
1591 GRXFSIZ_DEPTH_SHIFT;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001592
1593 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
1594 dev_dbg(hsotg->dev, " op_mode=%d\n",
1595 hw->op_mode);
1596 dev_dbg(hsotg->dev, " arch=%d\n",
1597 hw->arch);
1598 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
1599 hw->dma_desc_enable);
1600 dev_dbg(hsotg->dev, " power_optimized=%d\n",
1601 hw->power_optimized);
1602 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
1603 hw->i2c_enable);
1604 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
1605 hw->hs_phy_type);
1606 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
1607 hw->fs_phy_type);
Masanari Iida971bd8f2015-05-20 23:54:02 +09001608 dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02001609 hw->utmi_phy_data_width);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001610 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
1611 hw->num_dev_ep);
1612 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
1613 hw->num_dev_perio_in_ep);
1614 dev_dbg(hsotg->dev, " host_channels=%d\n",
1615 hw->host_channels);
1616 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
1617 hw->max_transfer_size);
1618 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
1619 hw->max_packet_count);
1620 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
1621 hw->nperio_tx_q_depth);
1622 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
1623 hw->host_perio_tx_q_depth);
1624 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
1625 hw->dev_token_q_depth);
1626 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
1627 hw->enable_dynamic_fifo);
1628 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
1629 hw->en_multiple_tx_fifo);
1630 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
1631 hw->total_fifo_size);
1632 dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
1633 hw->host_rx_fifo_size);
1634 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
1635 hw->host_nperio_tx_fifo_size);
1636 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
1637 hw->host_perio_tx_fifo_size);
1638 dev_dbg(hsotg->dev, "\n");
1639
1640 return 0;
1641}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02001642
1643/*
1644 * Sets all parameters to the given value.
1645 *
1646 * Assumes that the dwc2_core_params struct contains only integers.
1647 */
1648void dwc2_set_all_params(struct dwc2_core_params *params, int value)
1649{
1650 int *p = (int *)params;
1651 size_t size = sizeof(*params) / sizeof(*p);
1652 int i;
1653
1654 for (i = 0; i < size; i++)
1655 p[i] = value;
1656}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02001657
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001658
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001659u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
1660{
Paul Zimmermanb66a3f02013-11-22 16:43:50 -08001661 return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001662}
1663
Paul Zimmerman057715f2013-11-22 16:43:51 -08001664bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001665{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001666 if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
Paul Zimmerman057715f2013-11-22 16:43:51 -08001667 return false;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001668 else
Paul Zimmerman057715f2013-11-22 16:43:51 -08001669 return true;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001670}
1671
1672/**
1673 * dwc2_enable_global_interrupts() - Enables the controller's Global
1674 * Interrupt in the AHB Config register
1675 *
1676 * @hsotg: Programming view of DWC_otg controller
1677 */
1678void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
1679{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001680 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001681
1682 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001683 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001684}
1685
1686/**
1687 * dwc2_disable_global_interrupts() - Disables the controller's Global
1688 * Interrupt in the AHB Config register
1689 *
1690 * @hsotg: Programming view of DWC_otg controller
1691 */
1692void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
1693{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001694 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001695
1696 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001697 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001698}
1699
John Youn6bea9622015-12-17 11:16:17 -08001700/* Returns the controller's GHWCFG2.OTG_MODE. */
1701unsigned dwc2_op_mode(struct dwc2_hsotg *hsotg)
1702{
1703 u32 ghwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
1704
1705 return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
1706 GHWCFG2_OP_MODE_SHIFT;
1707}
1708
1709/* Returns true if the controller is capable of DRD. */
1710bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
1711{
1712 unsigned op_mode = dwc2_op_mode(hsotg);
1713
1714 return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
1715 (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
1716 (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
1717}
1718
1719/* Returns true if the controller is host-only. */
1720bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
1721{
1722 unsigned op_mode = dwc2_op_mode(hsotg);
1723
1724 return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
1725 (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
1726}
1727
1728/* Returns true if the controller is device-only. */
1729bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
1730{
1731 unsigned op_mode = dwc2_op_mode(hsotg);
1732
1733 return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
1734 (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
1735}
1736
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001737MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
1738MODULE_AUTHOR("Synopsys, Inc.");
1739MODULE_LICENSE("Dual BSD/GPL");