1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * core.c - DesignWare HS OTG Controller common routines
5 * Copyright (C) 2004-2013 Synopsys, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * ALTERNATIVELY, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * The Core code provides basic services for accessing and managing the
40 * DWC_otg hardware. These services are used by both the Host Controller
41 * Driver and the Peripheral Controller Driver.
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/moduleparam.h>
46 #include <linux/spinlock.h>
47 #include <linux/interrupt.h>
48 #include <linux/dma-mapping.h>
49 #include <linux/delay.h>
51 #include <linux/slab.h>
52 #include <linux/usb.h>
54 #include <linux/usb/hcd.h>
55 #include <linux/usb/ch11.h>
61 * dwc2_backup_global_registers() - Backup global controller registers.
62 * When suspending usb bus, registers needs to be backuped
63 * if controller power is disabled once suspended.
65 * @hsotg: Programming view of the DWC_otg controller
67 int dwc2_backup_global_registers(struct dwc2_hsotg
*hsotg
)
69 struct dwc2_gregs_backup
*gr
;
71 dev_dbg(hsotg
->dev
, "%s\n", __func__
);
73 /* Backup global regs */
74 gr
= &hsotg
->gr_backup
;
76 gr
->gotgctl
= dwc2_readl(hsotg
, GOTGCTL
);
77 gr
->gintmsk
= dwc2_readl(hsotg
, GINTMSK
);
78 gr
->gahbcfg
= dwc2_readl(hsotg
, GAHBCFG
);
79 gr
->gusbcfg
= dwc2_readl(hsotg
, GUSBCFG
);
80 gr
->grxfsiz
= dwc2_readl(hsotg
, GRXFSIZ
);
81 gr
->gnptxfsiz
= dwc2_readl(hsotg
, GNPTXFSIZ
);
82 gr
->gdfifocfg
= dwc2_readl(hsotg
, GDFIFOCFG
);
83 gr
->pcgcctl1
= dwc2_readl(hsotg
, PCGCCTL1
);
84 gr
->glpmcfg
= dwc2_readl(hsotg
, GLPMCFG
);
85 gr
->gi2cctl
= dwc2_readl(hsotg
, GI2CCTL
);
86 gr
->pcgcctl
= dwc2_readl(hsotg
, PCGCTL
);
93 * dwc2_restore_global_registers() - Restore controller global registers.
94 * When resuming usb bus, device registers needs to be restored
95 * if controller power were disabled.
97 * @hsotg: Programming view of the DWC_otg controller
99 int dwc2_restore_global_registers(struct dwc2_hsotg
*hsotg
)
101 struct dwc2_gregs_backup
*gr
;
103 dev_dbg(hsotg
->dev
, "%s\n", __func__
);
105 /* Restore global regs */
106 gr
= &hsotg
->gr_backup
;
108 dev_err(hsotg
->dev
, "%s: no global registers to restore\n",
114 dwc2_writel(hsotg
, 0xffffffff, GINTSTS
);
115 dwc2_writel(hsotg
, gr
->gotgctl
, GOTGCTL
);
116 dwc2_writel(hsotg
, gr
->gintmsk
, GINTMSK
);
117 dwc2_writel(hsotg
, gr
->gusbcfg
, GUSBCFG
);
118 dwc2_writel(hsotg
, gr
->gahbcfg
, GAHBCFG
);
119 dwc2_writel(hsotg
, gr
->grxfsiz
, GRXFSIZ
);
120 dwc2_writel(hsotg
, gr
->gnptxfsiz
, GNPTXFSIZ
);
121 dwc2_writel(hsotg
, gr
->gdfifocfg
, GDFIFOCFG
);
122 dwc2_writel(hsotg
, gr
->pcgcctl1
, PCGCCTL1
);
123 dwc2_writel(hsotg
, gr
->glpmcfg
, GLPMCFG
);
124 dwc2_writel(hsotg
, gr
->pcgcctl
, PCGCTL
);
125 dwc2_writel(hsotg
, gr
->gi2cctl
, GI2CCTL
);
131 * dwc2_exit_partial_power_down() - Exit controller from Partial Power Down.
133 * @hsotg: Programming view of the DWC_otg controller
134 * @restore: Controller registers need to be restored
136 int dwc2_exit_partial_power_down(struct dwc2_hsotg
*hsotg
, bool restore
)
141 if (hsotg
->params
.power_down
!= DWC2_POWER_DOWN_PARAM_PARTIAL
)
144 pcgcctl
= dwc2_readl(hsotg
, PCGCTL
);
145 pcgcctl
&= ~PCGCTL_STOPPCLK
;
146 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
148 pcgcctl
= dwc2_readl(hsotg
, PCGCTL
);
149 pcgcctl
&= ~PCGCTL_PWRCLMP
;
150 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
152 pcgcctl
= dwc2_readl(hsotg
, PCGCTL
);
153 pcgcctl
&= ~PCGCTL_RSTPDWNMODULE
;
154 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
158 ret
= dwc2_restore_global_registers(hsotg
);
160 dev_err(hsotg
->dev
, "%s: failed to restore registers\n",
164 if (dwc2_is_host_mode(hsotg
)) {
165 ret
= dwc2_restore_host_registers(hsotg
);
167 dev_err(hsotg
->dev
, "%s: failed to restore host registers\n",
172 ret
= dwc2_restore_device_registers(hsotg
, 0);
174 dev_err(hsotg
->dev
, "%s: failed to restore device registers\n",
185 * dwc2_enter_partial_power_down() - Put controller in Partial Power Down.
187 * @hsotg: Programming view of the DWC_otg controller
189 int dwc2_enter_partial_power_down(struct dwc2_hsotg
*hsotg
)
194 if (!hsotg
->params
.power_down
)
197 /* Backup all registers */
198 ret
= dwc2_backup_global_registers(hsotg
);
200 dev_err(hsotg
->dev
, "%s: failed to backup global registers\n",
205 if (dwc2_is_host_mode(hsotg
)) {
206 ret
= dwc2_backup_host_registers(hsotg
);
208 dev_err(hsotg
->dev
, "%s: failed to backup host registers\n",
213 ret
= dwc2_backup_device_registers(hsotg
);
215 dev_err(hsotg
->dev
, "%s: failed to backup device registers\n",
222 * Clear any pending interrupts since dwc2 will not be able to
223 * clear them after entering partial_power_down.
225 dwc2_writel(hsotg
, 0xffffffff, GINTSTS
);
227 /* Put the controller in low power state */
228 pcgcctl
= dwc2_readl(hsotg
, PCGCTL
);
230 pcgcctl
|= PCGCTL_PWRCLMP
;
231 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
234 pcgcctl
|= PCGCTL_RSTPDWNMODULE
;
235 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
238 pcgcctl
|= PCGCTL_STOPPCLK
;
239 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
245 * dwc2_restore_essential_regs() - Restore essiential regs of core.
247 * @hsotg: Programming view of the DWC_otg controller
248 * @rmode: Restore mode, enabled in case of remote-wakeup.
249 * @is_host: Host or device mode.
251 static void dwc2_restore_essential_regs(struct dwc2_hsotg
*hsotg
, int rmode
,
255 struct dwc2_gregs_backup
*gr
;
256 struct dwc2_dregs_backup
*dr
;
257 struct dwc2_hregs_backup
*hr
;
259 gr
= &hsotg
->gr_backup
;
260 dr
= &hsotg
->dr_backup
;
261 hr
= &hsotg
->hr_backup
;
263 dev_dbg(hsotg
->dev
, "%s: restoring essential regs\n", __func__
);
265 /* Load restore values for [31:14] bits */
266 pcgcctl
= (gr
->pcgcctl
& 0xffffc000);
269 if (!(pcgcctl
& PCGCTL_P2HD_PRT_SPD_MASK
))
272 if (!(pcgcctl
& PCGCTL_P2HD_DEV_ENUM_SPD_MASK
))
275 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
277 /* Umnask global Interrupt in GAHBCFG and restore it */
278 dwc2_writel(hsotg
, gr
->gahbcfg
| GAHBCFG_GLBL_INTR_EN
, GAHBCFG
);
280 /* Clear all pending interupts */
281 dwc2_writel(hsotg
, 0xffffffff, GINTSTS
);
283 /* Unmask restore done interrupt */
284 dwc2_writel(hsotg
, GINTSTS_RESTOREDONE
, GINTMSK
);
286 /* Restore GUSBCFG and HCFG/DCFG */
287 dwc2_writel(hsotg
, gr
->gusbcfg
, GUSBCFG
);
290 dwc2_writel(hsotg
, hr
->hcfg
, HCFG
);
292 pcgcctl
|= PCGCTL_RESTOREMODE
;
293 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
296 pcgcctl
|= PCGCTL_ESS_REG_RESTORED
;
297 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
300 dwc2_writel(hsotg
, dr
->dcfg
, DCFG
);
302 pcgcctl
|= PCGCTL_RESTOREMODE
| PCGCTL_RSTPDWNMODULE
;
303 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
306 pcgcctl
|= PCGCTL_ESS_REG_RESTORED
;
307 dwc2_writel(hsotg
, pcgcctl
, PCGCTL
);
313 * dwc2_hib_restore_common() - Common part of restore routine.
315 * @hsotg: Programming view of the DWC_otg controller
316 * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
317 * @is_host: Host or device mode.
319 void dwc2_hib_restore_common(struct dwc2_hsotg
*hsotg
, int rem_wakeup
,
324 /* Switch-on voltage to the core */
325 gpwrdn
= dwc2_readl(hsotg
, GPWRDN
);
326 gpwrdn
&= ~GPWRDN_PWRDNSWTCH
;
327 dwc2_writel(hsotg
, gpwrdn
, GPWRDN
);
331 gpwrdn
= dwc2_readl(hsotg
, GPWRDN
);
332 gpwrdn
&= ~GPWRDN_PWRDNRSTN
;
333 dwc2_writel(hsotg
, gpwrdn
, GPWRDN
);
336 /* Enable restore from PMU */
337 gpwrdn
= dwc2_readl(hsotg
, GPWRDN
);
338 gpwrdn
|= GPWRDN_RESTORE
;
339 dwc2_writel(hsotg
, gpwrdn
, GPWRDN
);
342 /* Disable Power Down Clamp */
343 gpwrdn
= dwc2_readl(hsotg
, GPWRDN
);
344 gpwrdn
&= ~GPWRDN_PWRDNCLMP
;
345 dwc2_writel(hsotg
, gpwrdn
, GPWRDN
);
348 if (!is_host
&& rem_wakeup
)
351 /* Deassert reset core */
352 gpwrdn
= dwc2_readl(hsotg
, GPWRDN
);
353 gpwrdn
|= GPWRDN_PWRDNRSTN
;
354 dwc2_writel(hsotg
, gpwrdn
, GPWRDN
);
357 /* Disable PMU interrupt */
358 gpwrdn
= dwc2_readl(hsotg
, GPWRDN
);
359 gpwrdn
&= ~GPWRDN_PMUINTSEL
;
360 dwc2_writel(hsotg
, gpwrdn
, GPWRDN
);
363 /* Set Restore Essential Regs bit in PCGCCTL register */
364 dwc2_restore_essential_regs(hsotg
, rem_wakeup
, is_host
);
367 * Wait For Restore_done Interrupt. This mechanism of polling the
368 * interrupt is introduced to avoid any possible race conditions
370 if (dwc2_hsotg_wait_bit_set(hsotg
, GINTSTS
, GINTSTS_RESTOREDONE
,
373 "%s: Restore Done wan't generated here\n",
376 dev_dbg(hsotg
->dev
, "restore done generated here\n");
381 * dwc2_wait_for_mode() - Waits for the controller mode.
382 * @hsotg: Programming view of the DWC_otg controller.
383 * @host_mode: If true, waits for host mode, otherwise device mode.
385 static void dwc2_wait_for_mode(struct dwc2_hsotg
*hsotg
,
390 unsigned int timeout
= 110;
392 dev_vdbg(hsotg
->dev
, "Waiting for %s mode\n",
393 host_mode
? "host" : "device");
400 if (dwc2_is_host_mode(hsotg
) == host_mode
) {
401 dev_vdbg(hsotg
->dev
, "%s mode set\n",
402 host_mode
? "Host" : "Device");
407 ms
= ktime_to_ms(ktime_sub(end
, start
));
409 if (ms
>= (s64
)timeout
) {
410 dev_warn(hsotg
->dev
, "%s: Couldn't set %s mode\n",
411 __func__
, host_mode
? "host" : "device");
415 usleep_range(1000, 2000);
420 * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
423 * @hsotg: Programming view of DWC_otg controller
425 static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg
*hsotg
)
430 if (!dwc2_hw_is_otg(hsotg
))
433 /* Check if core configuration includes the IDDIG filter. */
434 ghwcfg4
= dwc2_readl(hsotg
, GHWCFG4
);
435 if (!(ghwcfg4
& GHWCFG4_IDDIG_FILT_EN
))
439 * Check if the IDDIG debounce filter is bypassed. Available
440 * in core version >= 3.10a.
442 gsnpsid
= dwc2_readl(hsotg
, GSNPSID
);
443 if (gsnpsid
>= DWC2_CORE_REV_3_10a
) {
444 u32 gotgctl
= dwc2_readl(hsotg
, GOTGCTL
);
446 if (gotgctl
& GOTGCTL_DBNCE_FLTR_BYPASS
)
454 * dwc2_enter_hibernation() - Common function to enter hibernation.
456 * @hsotg: Programming view of the DWC_otg controller
457 * @is_host: True if core is in host mode.
459 * Return: 0 if successful, negative error code otherwise
461 int dwc2_enter_hibernation(struct dwc2_hsotg
*hsotg
, int is_host
)
463 if (hsotg
->params
.power_down
!= DWC2_POWER_DOWN_PARAM_HIBERNATION
)
467 return dwc2_host_enter_hibernation(hsotg
);
469 return dwc2_gadget_enter_hibernation(hsotg
);
473 * dwc2_exit_hibernation() - Common function to exit from hibernation.
475 * @hsotg: Programming view of the DWC_otg controller
476 * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
477 * @reset: Enabled in case of restore with reset.
478 * @is_host: True if core is in host mode.
480 * Return: 0 if successful, negative error code otherwise
482 int dwc2_exit_hibernation(struct dwc2_hsotg
*hsotg
, int rem_wakeup
,
483 int reset
, int is_host
)
486 return dwc2_host_exit_hibernation(hsotg
, rem_wakeup
, reset
);
488 return dwc2_gadget_exit_hibernation(hsotg
, rem_wakeup
, reset
);
492 * Do core a soft reset of the core. Be careful with this because it
493 * resets all the internal state machines of the core.
495 int dwc2_core_reset(struct dwc2_hsotg
*hsotg
, bool skip_wait
)
498 bool wait_for_host_mode
= false;
500 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
503 * If the current mode is host, either due to the force mode
504 * bit being set (which persists after core reset) or the
505 * connector id pin, a core soft reset will temporarily reset
506 * the mode to device. A delay from the IDDIG debounce filter
507 * will occur before going back to host mode.
509 * Determine whether we will go back into host mode after a
510 * reset and account for this delay after the reset.
512 if (dwc2_iddig_filter_enabled(hsotg
)) {
513 u32 gotgctl
= dwc2_readl(hsotg
, GOTGCTL
);
514 u32 gusbcfg
= dwc2_readl(hsotg
, GUSBCFG
);
516 if (!(gotgctl
& GOTGCTL_CONID_B
) ||
517 (gusbcfg
& GUSBCFG_FORCEHOSTMODE
)) {
518 wait_for_host_mode
= true;
522 /* Core Soft Reset */
523 greset
= dwc2_readl(hsotg
, GRSTCTL
);
524 greset
|= GRSTCTL_CSFTRST
;
525 dwc2_writel(hsotg
, greset
, GRSTCTL
);
527 if (dwc2_hsotg_wait_bit_clear(hsotg
, GRSTCTL
, GRSTCTL_CSFTRST
, 50)) {
528 dev_warn(hsotg
->dev
, "%s: HANG! Soft Reset timeout GRSTCTL GRSTCTL_CSFTRST\n",
533 /* Wait for AHB master IDLE state */
534 if (dwc2_hsotg_wait_bit_set(hsotg
, GRSTCTL
, GRSTCTL_AHBIDLE
, 50)) {
535 dev_warn(hsotg
->dev
, "%s: HANG! AHB Idle timeout GRSTCTL GRSTCTL_AHBIDLE\n",
540 if (wait_for_host_mode
&& !skip_wait
)
541 dwc2_wait_for_mode(hsotg
, true);
547 * dwc2_force_mode() - Force the mode of the controller.
549 * Forcing the mode is needed for two cases:
551 * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
552 * controller to stay in a particular mode regardless of ID pin
553 * changes. We do this once during probe.
555 * 2) During probe we want to read reset values of the hw
556 * configuration registers that are only available in either host or
557 * device mode. We may need to force the mode if the current mode does
558 * not allow us to access the register in the mode that we want.
560 * In either case it only makes sense to force the mode if the
561 * controller hardware is OTG capable.
563 * Checks are done in this function to determine whether doing a force
564 * would be valid or not.
566 * If a force is done, it requires a IDDIG debounce filter delay if
567 * the filter is configured and enabled. We poll the current mode of
568 * the controller to account for this delay.
570 * @hsotg: Programming view of DWC_otg controller
571 * @host: Host mode flag
573 void dwc2_force_mode(struct dwc2_hsotg
*hsotg
, bool host
)
579 dev_dbg(hsotg
->dev
, "Forcing mode to %s\n", host
? "host" : "device");
582 * Force mode has no effect if the hardware is not OTG.
584 if (!dwc2_hw_is_otg(hsotg
))
588 * If dr_mode is either peripheral or host only, there is no
589 * need to ever force the mode to the opposite mode.
591 if (WARN_ON(host
&& hsotg
->dr_mode
== USB_DR_MODE_PERIPHERAL
))
594 if (WARN_ON(!host
&& hsotg
->dr_mode
== USB_DR_MODE_HOST
))
597 gusbcfg
= dwc2_readl(hsotg
, GUSBCFG
);
599 set
= host
? GUSBCFG_FORCEHOSTMODE
: GUSBCFG_FORCEDEVMODE
;
600 clear
= host
? GUSBCFG_FORCEDEVMODE
: GUSBCFG_FORCEHOSTMODE
;
604 dwc2_writel(hsotg
, gusbcfg
, GUSBCFG
);
606 dwc2_wait_for_mode(hsotg
, host
);
611 * dwc2_clear_force_mode() - Clears the force mode bits.
613 * After clearing the bits, wait up to 100 ms to account for any
614 * potential IDDIG filter delay. We can't know if we expect this delay
615 * or not because the value of the connector ID status is affected by
616 * the force mode. We only need to call this once during probe if
619 * @hsotg: Programming view of DWC_otg controller
621 static void dwc2_clear_force_mode(struct dwc2_hsotg
*hsotg
)
625 if (!dwc2_hw_is_otg(hsotg
))
628 dev_dbg(hsotg
->dev
, "Clearing force mode bits\n");
630 gusbcfg
= dwc2_readl(hsotg
, GUSBCFG
);
631 gusbcfg
&= ~GUSBCFG_FORCEHOSTMODE
;
632 gusbcfg
&= ~GUSBCFG_FORCEDEVMODE
;
633 dwc2_writel(hsotg
, gusbcfg
, GUSBCFG
);
635 if (dwc2_iddig_filter_enabled(hsotg
))
640 * Sets or clears force mode based on the dr_mode parameter.
642 void dwc2_force_dr_mode(struct dwc2_hsotg
*hsotg
)
644 switch (hsotg
->dr_mode
) {
645 case USB_DR_MODE_HOST
:
647 * NOTE: This is required for some rockchip soc based
648 * platforms on their host-only dwc2.
650 if (!dwc2_hw_is_otg(hsotg
))
654 case USB_DR_MODE_PERIPHERAL
:
655 dwc2_force_mode(hsotg
, false);
657 case USB_DR_MODE_OTG
:
658 dwc2_clear_force_mode(hsotg
);
661 dev_warn(hsotg
->dev
, "%s() Invalid dr_mode=%d\n",
662 __func__
, hsotg
->dr_mode
);
668 * dwc2_enable_acg - enable active clock gating feature
670 void dwc2_enable_acg(struct dwc2_hsotg
*hsotg
)
672 if (hsotg
->params
.acg_enable
) {
673 u32 pcgcctl1
= dwc2_readl(hsotg
, PCGCCTL1
);
675 dev_dbg(hsotg
->dev
, "Enabling Active Clock Gating\n");
676 pcgcctl1
|= PCGCCTL1_GATEEN
;
677 dwc2_writel(hsotg
, pcgcctl1
, PCGCCTL1
);
682 * dwc2_dump_host_registers() - Prints the host registers
684 * @hsotg: Programming view of DWC_otg controller
686 * NOTE: This function will be removed once the peripheral controller code
687 * is integrated and the driver is stable
689 void dwc2_dump_host_registers(struct dwc2_hsotg
*hsotg
)
695 dev_dbg(hsotg
->dev
, "Host Global Registers\n");
696 addr
= hsotg
->regs
+ HCFG
;
697 dev_dbg(hsotg
->dev
, "HCFG @0x%08lX : 0x%08X\n",
698 (unsigned long)addr
, dwc2_readl(hsotg
, HCFG
));
699 addr
= hsotg
->regs
+ HFIR
;
700 dev_dbg(hsotg
->dev
, "HFIR @0x%08lX : 0x%08X\n",
701 (unsigned long)addr
, dwc2_readl(hsotg
, HFIR
));
702 addr
= hsotg
->regs
+ HFNUM
;
703 dev_dbg(hsotg
->dev
, "HFNUM @0x%08lX : 0x%08X\n",
704 (unsigned long)addr
, dwc2_readl(hsotg
, HFNUM
));
705 addr
= hsotg
->regs
+ HPTXSTS
;
706 dev_dbg(hsotg
->dev
, "HPTXSTS @0x%08lX : 0x%08X\n",
707 (unsigned long)addr
, dwc2_readl(hsotg
, HPTXSTS
));
708 addr
= hsotg
->regs
+ HAINT
;
709 dev_dbg(hsotg
->dev
, "HAINT @0x%08lX : 0x%08X\n",
710 (unsigned long)addr
, dwc2_readl(hsotg
, HAINT
));
711 addr
= hsotg
->regs
+ HAINTMSK
;
712 dev_dbg(hsotg
->dev
, "HAINTMSK @0x%08lX : 0x%08X\n",
713 (unsigned long)addr
, dwc2_readl(hsotg
, HAINTMSK
));
714 if (hsotg
->params
.dma_desc_enable
) {
715 addr
= hsotg
->regs
+ HFLBADDR
;
716 dev_dbg(hsotg
->dev
, "HFLBADDR @0x%08lX : 0x%08X\n",
717 (unsigned long)addr
, dwc2_readl(hsotg
, HFLBADDR
));
720 addr
= hsotg
->regs
+ HPRT0
;
721 dev_dbg(hsotg
->dev
, "HPRT0 @0x%08lX : 0x%08X\n",
722 (unsigned long)addr
, dwc2_readl(hsotg
, HPRT0
));
724 for (i
= 0; i
< hsotg
->params
.host_channels
; i
++) {
725 dev_dbg(hsotg
->dev
, "Host Channel %d Specific Registers\n", i
);
726 addr
= hsotg
->regs
+ HCCHAR(i
);
727 dev_dbg(hsotg
->dev
, "HCCHAR @0x%08lX : 0x%08X\n",
728 (unsigned long)addr
, dwc2_readl(hsotg
, HCCHAR(i
)));
729 addr
= hsotg
->regs
+ HCSPLT(i
);
730 dev_dbg(hsotg
->dev
, "HCSPLT @0x%08lX : 0x%08X\n",
731 (unsigned long)addr
, dwc2_readl(hsotg
, HCSPLT(i
)));
732 addr
= hsotg
->regs
+ HCINT(i
);
733 dev_dbg(hsotg
->dev
, "HCINT @0x%08lX : 0x%08X\n",
734 (unsigned long)addr
, dwc2_readl(hsotg
, HCINT(i
)));
735 addr
= hsotg
->regs
+ HCINTMSK(i
);
736 dev_dbg(hsotg
->dev
, "HCINTMSK @0x%08lX : 0x%08X\n",
737 (unsigned long)addr
, dwc2_readl(hsotg
, HCINTMSK(i
)));
738 addr
= hsotg
->regs
+ HCTSIZ(i
);
739 dev_dbg(hsotg
->dev
, "HCTSIZ @0x%08lX : 0x%08X\n",
740 (unsigned long)addr
, dwc2_readl(hsotg
, HCTSIZ(i
)));
741 addr
= hsotg
->regs
+ HCDMA(i
);
742 dev_dbg(hsotg
->dev
, "HCDMA @0x%08lX : 0x%08X\n",
743 (unsigned long)addr
, dwc2_readl(hsotg
, HCDMA(i
)));
744 if (hsotg
->params
.dma_desc_enable
) {
745 addr
= hsotg
->regs
+ HCDMAB(i
);
746 dev_dbg(hsotg
->dev
, "HCDMAB @0x%08lX : 0x%08X\n",
747 (unsigned long)addr
, dwc2_readl(hsotg
,
755 * dwc2_dump_global_registers() - Prints the core global registers
757 * @hsotg: Programming view of DWC_otg controller
759 * NOTE: This function will be removed once the peripheral controller code
760 * is integrated and the driver is stable
762 void dwc2_dump_global_registers(struct dwc2_hsotg
*hsotg
)
767 dev_dbg(hsotg
->dev
, "Core Global Registers\n");
768 addr
= hsotg
->regs
+ GOTGCTL
;
769 dev_dbg(hsotg
->dev
, "GOTGCTL @0x%08lX : 0x%08X\n",
770 (unsigned long)addr
, dwc2_readl(hsotg
, GOTGCTL
));
771 addr
= hsotg
->regs
+ GOTGINT
;
772 dev_dbg(hsotg
->dev
, "GOTGINT @0x%08lX : 0x%08X\n",
773 (unsigned long)addr
, dwc2_readl(hsotg
, GOTGINT
));
774 addr
= hsotg
->regs
+ GAHBCFG
;
775 dev_dbg(hsotg
->dev
, "GAHBCFG @0x%08lX : 0x%08X\n",
776 (unsigned long)addr
, dwc2_readl(hsotg
, GAHBCFG
));
777 addr
= hsotg
->regs
+ GUSBCFG
;
778 dev_dbg(hsotg
->dev
, "GUSBCFG @0x%08lX : 0x%08X\n",
779 (unsigned long)addr
, dwc2_readl(hsotg
, GUSBCFG
));
780 addr
= hsotg
->regs
+ GRSTCTL
;
781 dev_dbg(hsotg
->dev
, "GRSTCTL @0x%08lX : 0x%08X\n",
782 (unsigned long)addr
, dwc2_readl(hsotg
, GRSTCTL
));
783 addr
= hsotg
->regs
+ GINTSTS
;
784 dev_dbg(hsotg
->dev
, "GINTSTS @0x%08lX : 0x%08X\n",
785 (unsigned long)addr
, dwc2_readl(hsotg
, GINTSTS
));
786 addr
= hsotg
->regs
+ GINTMSK
;
787 dev_dbg(hsotg
->dev
, "GINTMSK @0x%08lX : 0x%08X\n",
788 (unsigned long)addr
, dwc2_readl(hsotg
, GINTMSK
));
789 addr
= hsotg
->regs
+ GRXSTSR
;
790 dev_dbg(hsotg
->dev
, "GRXSTSR @0x%08lX : 0x%08X\n",
791 (unsigned long)addr
, dwc2_readl(hsotg
, GRXSTSR
));
792 addr
= hsotg
->regs
+ GRXFSIZ
;
793 dev_dbg(hsotg
->dev
, "GRXFSIZ @0x%08lX : 0x%08X\n",
794 (unsigned long)addr
, dwc2_readl(hsotg
, GRXFSIZ
));
795 addr
= hsotg
->regs
+ GNPTXFSIZ
;
796 dev_dbg(hsotg
->dev
, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
797 (unsigned long)addr
, dwc2_readl(hsotg
, GNPTXFSIZ
));
798 addr
= hsotg
->regs
+ GNPTXSTS
;
799 dev_dbg(hsotg
->dev
, "GNPTXSTS @0x%08lX : 0x%08X\n",
800 (unsigned long)addr
, dwc2_readl(hsotg
, GNPTXSTS
));
801 addr
= hsotg
->regs
+ GI2CCTL
;
802 dev_dbg(hsotg
->dev
, "GI2CCTL @0x%08lX : 0x%08X\n",
803 (unsigned long)addr
, dwc2_readl(hsotg
, GI2CCTL
));
804 addr
= hsotg
->regs
+ GPVNDCTL
;
805 dev_dbg(hsotg
->dev
, "GPVNDCTL @0x%08lX : 0x%08X\n",
806 (unsigned long)addr
, dwc2_readl(hsotg
, GPVNDCTL
));
807 addr
= hsotg
->regs
+ GGPIO
;
808 dev_dbg(hsotg
->dev
, "GGPIO @0x%08lX : 0x%08X\n",
809 (unsigned long)addr
, dwc2_readl(hsotg
, GGPIO
));
810 addr
= hsotg
->regs
+ GUID
;
811 dev_dbg(hsotg
->dev
, "GUID @0x%08lX : 0x%08X\n",
812 (unsigned long)addr
, dwc2_readl(hsotg
, GUID
));
813 addr
= hsotg
->regs
+ GSNPSID
;
814 dev_dbg(hsotg
->dev
, "GSNPSID @0x%08lX : 0x%08X\n",
815 (unsigned long)addr
, dwc2_readl(hsotg
, GSNPSID
));
816 addr
= hsotg
->regs
+ GHWCFG1
;
817 dev_dbg(hsotg
->dev
, "GHWCFG1 @0x%08lX : 0x%08X\n",
818 (unsigned long)addr
, dwc2_readl(hsotg
, GHWCFG1
));
819 addr
= hsotg
->regs
+ GHWCFG2
;
820 dev_dbg(hsotg
->dev
, "GHWCFG2 @0x%08lX : 0x%08X\n",
821 (unsigned long)addr
, dwc2_readl(hsotg
, GHWCFG2
));
822 addr
= hsotg
->regs
+ GHWCFG3
;
823 dev_dbg(hsotg
->dev
, "GHWCFG3 @0x%08lX : 0x%08X\n",
824 (unsigned long)addr
, dwc2_readl(hsotg
, GHWCFG3
));
825 addr
= hsotg
->regs
+ GHWCFG4
;
826 dev_dbg(hsotg
->dev
, "GHWCFG4 @0x%08lX : 0x%08X\n",
827 (unsigned long)addr
, dwc2_readl(hsotg
, GHWCFG4
));
828 addr
= hsotg
->regs
+ GLPMCFG
;
829 dev_dbg(hsotg
->dev
, "GLPMCFG @0x%08lX : 0x%08X\n",
830 (unsigned long)addr
, dwc2_readl(hsotg
, GLPMCFG
));
831 addr
= hsotg
->regs
+ GPWRDN
;
832 dev_dbg(hsotg
->dev
, "GPWRDN @0x%08lX : 0x%08X\n",
833 (unsigned long)addr
, dwc2_readl(hsotg
, GPWRDN
));
834 addr
= hsotg
->regs
+ GDFIFOCFG
;
835 dev_dbg(hsotg
->dev
, "GDFIFOCFG @0x%08lX : 0x%08X\n",
836 (unsigned long)addr
, dwc2_readl(hsotg
, GDFIFOCFG
));
837 addr
= hsotg
->regs
+ HPTXFSIZ
;
838 dev_dbg(hsotg
->dev
, "HPTXFSIZ @0x%08lX : 0x%08X\n",
839 (unsigned long)addr
, dwc2_readl(hsotg
, HPTXFSIZ
));
841 addr
= hsotg
->regs
+ PCGCTL
;
842 dev_dbg(hsotg
->dev
, "PCGCTL @0x%08lX : 0x%08X\n",
843 (unsigned long)addr
, dwc2_readl(hsotg
, PCGCTL
));
848 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
850 * @hsotg: Programming view of DWC_otg controller
851 * @num: Tx FIFO to flush
853 void dwc2_flush_tx_fifo(struct dwc2_hsotg
*hsotg
, const int num
)
857 dev_vdbg(hsotg
->dev
, "Flush Tx FIFO %d\n", num
);
859 /* Wait for AHB master IDLE state */
860 if (dwc2_hsotg_wait_bit_set(hsotg
, GRSTCTL
, GRSTCTL_AHBIDLE
, 10000))
861 dev_warn(hsotg
->dev
, "%s: HANG! AHB Idle GRSCTL\n",
864 greset
= GRSTCTL_TXFFLSH
;
865 greset
|= num
<< GRSTCTL_TXFNUM_SHIFT
& GRSTCTL_TXFNUM_MASK
;
866 dwc2_writel(hsotg
, greset
, GRSTCTL
);
868 if (dwc2_hsotg_wait_bit_clear(hsotg
, GRSTCTL
, GRSTCTL_TXFFLSH
, 10000))
869 dev_warn(hsotg
->dev
, "%s: HANG! timeout GRSTCTL GRSTCTL_TXFFLSH\n",
872 /* Wait for at least 3 PHY Clocks */
877 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
879 * @hsotg: Programming view of DWC_otg controller
881 void dwc2_flush_rx_fifo(struct dwc2_hsotg
*hsotg
)
885 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
887 /* Wait for AHB master IDLE state */
888 if (dwc2_hsotg_wait_bit_set(hsotg
, GRSTCTL
, GRSTCTL_AHBIDLE
, 10000))
889 dev_warn(hsotg
->dev
, "%s: HANG! AHB Idle GRSCTL\n",
892 greset
= GRSTCTL_RXFFLSH
;
893 dwc2_writel(hsotg
, greset
, GRSTCTL
);
895 /* Wait for RxFIFO flush done */
896 if (dwc2_hsotg_wait_bit_clear(hsotg
, GRSTCTL
, GRSTCTL_RXFFLSH
, 10000))
897 dev_warn(hsotg
->dev
, "%s: HANG! timeout GRSTCTL GRSTCTL_RXFFLSH\n",
900 /* Wait for at least 3 PHY Clocks */
904 bool dwc2_is_controller_alive(struct dwc2_hsotg
*hsotg
)
906 if (dwc2_readl(hsotg
, GSNPSID
) == 0xffffffff)
913 * dwc2_enable_global_interrupts() - Enables the controller's Global
914 * Interrupt in the AHB Config register
916 * @hsotg: Programming view of DWC_otg controller
918 void dwc2_enable_global_interrupts(struct dwc2_hsotg
*hsotg
)
920 u32 ahbcfg
= dwc2_readl(hsotg
, GAHBCFG
);
922 ahbcfg
|= GAHBCFG_GLBL_INTR_EN
;
923 dwc2_writel(hsotg
, ahbcfg
, GAHBCFG
);
927 * dwc2_disable_global_interrupts() - Disables the controller's Global
928 * Interrupt in the AHB Config register
930 * @hsotg: Programming view of DWC_otg controller
932 void dwc2_disable_global_interrupts(struct dwc2_hsotg
*hsotg
)
934 u32 ahbcfg
= dwc2_readl(hsotg
, GAHBCFG
);
936 ahbcfg
&= ~GAHBCFG_GLBL_INTR_EN
;
937 dwc2_writel(hsotg
, ahbcfg
, GAHBCFG
);
940 /* Returns the controller's GHWCFG2.OTG_MODE. */
941 unsigned int dwc2_op_mode(struct dwc2_hsotg
*hsotg
)
943 u32 ghwcfg2
= dwc2_readl(hsotg
, GHWCFG2
);
945 return (ghwcfg2
& GHWCFG2_OP_MODE_MASK
) >>
946 GHWCFG2_OP_MODE_SHIFT
;
949 /* Returns true if the controller is capable of DRD. */
950 bool dwc2_hw_is_otg(struct dwc2_hsotg
*hsotg
)
952 unsigned int op_mode
= dwc2_op_mode(hsotg
);
954 return (op_mode
== GHWCFG2_OP_MODE_HNP_SRP_CAPABLE
) ||
955 (op_mode
== GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE
) ||
956 (op_mode
== GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE
);
959 /* Returns true if the controller is host-only. */
960 bool dwc2_hw_is_host(struct dwc2_hsotg
*hsotg
)
962 unsigned int op_mode
= dwc2_op_mode(hsotg
);
964 return (op_mode
== GHWCFG2_OP_MODE_SRP_CAPABLE_HOST
) ||
965 (op_mode
== GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST
);
968 /* Returns true if the controller is device-only. */
969 bool dwc2_hw_is_device(struct dwc2_hsotg
*hsotg
)
971 unsigned int op_mode
= dwc2_op_mode(hsotg
);
973 return (op_mode
== GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE
) ||
974 (op_mode
== GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE
);
978 * dwc2_hsotg_wait_bit_set - Waits for bit to be set.
979 * @hsotg: Programming view of DWC_otg controller.
980 * @offset: Register's offset where bit/bits must be set.
981 * @mask: Mask of the bit/bits which must be set.
982 * @timeout: Timeout to wait.
984 * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
986 int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg
*hsotg
, u32 offset
, u32 mask
,
991 for (i
= 0; i
< timeout
; i
++) {
992 if (dwc2_readl(hsotg
, offset
) & mask
)
1001 * dwc2_hsotg_wait_bit_clear - Waits for bit to be clear.
1002 * @hsotg: Programming view of DWC_otg controller.
1003 * @offset: Register's offset where bit/bits must be set.
1004 * @mask: Mask of the bit/bits which must be set.
1005 * @timeout: Timeout to wait.
1007 * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
1009 int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg
*hsotg
, u32 offset
, u32 mask
,
1014 for (i
= 0; i
< timeout
; i
++) {
1015 if (!(dwc2_readl(hsotg
, offset
) & mask
))
1023 MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
1024 MODULE_AUTHOR("Synopsys, Inc.");
1025 MODULE_LICENSE("Dual BSD/GPL");