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 * @rem_wakeup: indicates whether resume is initiated by Reset.
135 * @restore: Controller registers need to be restored
137 int dwc2_exit_partial_power_down(struct dwc2_hsotg *hsotg, int rem_wakeup,
140 struct dwc2_gregs_backup *gr;
142 gr = &hsotg->gr_backup;
145 * Restore host or device regisers with the same mode core enterted
146 * to partial power down by checking "GOTGCTL_CURMODE_HOST" backup
147 * value of the "gotgctl" register.
149 if (gr->gotgctl & GOTGCTL_CURMODE_HOST)
150 return dwc2_host_exit_partial_power_down(hsotg, rem_wakeup,
153 return dwc2_gadget_exit_partial_power_down(hsotg, restore);
157 * dwc2_enter_partial_power_down() - Put controller in Partial Power Down.
159 * @hsotg: Programming view of the DWC_otg controller
161 int dwc2_enter_partial_power_down(struct dwc2_hsotg *hsotg)
163 if (dwc2_is_host_mode(hsotg))
164 return dwc2_host_enter_partial_power_down(hsotg);
166 return dwc2_gadget_enter_partial_power_down(hsotg);
170 * dwc2_restore_essential_regs() - Restore essiential regs of core.
172 * @hsotg: Programming view of the DWC_otg controller
173 * @rmode: Restore mode, enabled in case of remote-wakeup.
174 * @is_host: Host or device mode.
176 static void dwc2_restore_essential_regs(struct dwc2_hsotg *hsotg, int rmode,
180 struct dwc2_gregs_backup *gr;
181 struct dwc2_dregs_backup *dr;
182 struct dwc2_hregs_backup *hr;
184 gr = &hsotg->gr_backup;
185 dr = &hsotg->dr_backup;
186 hr = &hsotg->hr_backup;
188 dev_dbg(hsotg->dev, "%s: restoring essential regs\n", __func__);
190 /* Load restore values for [31:14] bits */
191 pcgcctl = (gr->pcgcctl & 0xffffc000);
194 if (!(pcgcctl & PCGCTL_P2HD_PRT_SPD_MASK))
197 if (!(pcgcctl & PCGCTL_P2HD_DEV_ENUM_SPD_MASK))
200 dwc2_writel(hsotg, pcgcctl, PCGCTL);
202 /* Umnask global Interrupt in GAHBCFG and restore it */
203 dwc2_writel(hsotg, gr->gahbcfg | GAHBCFG_GLBL_INTR_EN, GAHBCFG);
205 /* Clear all pending interupts */
206 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
208 /* Unmask restore done interrupt */
209 dwc2_writel(hsotg, GINTSTS_RESTOREDONE, GINTMSK);
211 /* Restore GUSBCFG and HCFG/DCFG */
212 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
215 dwc2_writel(hsotg, hr->hcfg, HCFG);
217 pcgcctl |= PCGCTL_RESTOREMODE;
218 dwc2_writel(hsotg, pcgcctl, PCGCTL);
221 pcgcctl |= PCGCTL_ESS_REG_RESTORED;
222 dwc2_writel(hsotg, pcgcctl, PCGCTL);
225 dwc2_writel(hsotg, dr->dcfg, DCFG);
227 pcgcctl |= PCGCTL_RESTOREMODE | PCGCTL_RSTPDWNMODULE;
228 dwc2_writel(hsotg, pcgcctl, PCGCTL);
231 pcgcctl |= PCGCTL_ESS_REG_RESTORED;
232 dwc2_writel(hsotg, pcgcctl, PCGCTL);
238 * dwc2_hib_restore_common() - Common part of restore routine.
240 * @hsotg: Programming view of the DWC_otg controller
241 * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
242 * @is_host: Host or device mode.
244 void dwc2_hib_restore_common(struct dwc2_hsotg *hsotg, int rem_wakeup,
249 /* Switch-on voltage to the core */
250 gpwrdn = dwc2_readl(hsotg, GPWRDN);
251 gpwrdn &= ~GPWRDN_PWRDNSWTCH;
252 dwc2_writel(hsotg, gpwrdn, GPWRDN);
256 gpwrdn = dwc2_readl(hsotg, GPWRDN);
257 gpwrdn &= ~GPWRDN_PWRDNRSTN;
258 dwc2_writel(hsotg, gpwrdn, GPWRDN);
261 /* Enable restore from PMU */
262 gpwrdn = dwc2_readl(hsotg, GPWRDN);
263 gpwrdn |= GPWRDN_RESTORE;
264 dwc2_writel(hsotg, gpwrdn, GPWRDN);
267 /* Disable Power Down Clamp */
268 gpwrdn = dwc2_readl(hsotg, GPWRDN);
269 gpwrdn &= ~GPWRDN_PWRDNCLMP;
270 dwc2_writel(hsotg, gpwrdn, GPWRDN);
273 if (!is_host && rem_wakeup)
276 /* Deassert reset core */
277 gpwrdn = dwc2_readl(hsotg, GPWRDN);
278 gpwrdn |= GPWRDN_PWRDNRSTN;
279 dwc2_writel(hsotg, gpwrdn, GPWRDN);
282 /* Disable PMU interrupt */
283 gpwrdn = dwc2_readl(hsotg, GPWRDN);
284 gpwrdn &= ~GPWRDN_PMUINTSEL;
285 dwc2_writel(hsotg, gpwrdn, GPWRDN);
288 /* Set Restore Essential Regs bit in PCGCCTL register */
289 dwc2_restore_essential_regs(hsotg, rem_wakeup, is_host);
292 * Wait For Restore_done Interrupt. This mechanism of polling the
293 * interrupt is introduced to avoid any possible race conditions
295 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS, GINTSTS_RESTOREDONE,
298 "%s: Restore Done wan't generated here\n",
301 dev_dbg(hsotg->dev, "restore done generated here\n");
304 * To avoid restore done interrupt storm after restore is
305 * generated clear GINTSTS_RESTOREDONE bit.
307 dwc2_writel(hsotg, GINTSTS_RESTOREDONE, GINTSTS);
312 * dwc2_wait_for_mode() - Waits for the controller mode.
313 * @hsotg: Programming view of the DWC_otg controller.
314 * @host_mode: If true, waits for host mode, otherwise device mode.
316 static void dwc2_wait_for_mode(struct dwc2_hsotg *hsotg,
321 unsigned int timeout = 110;
323 dev_vdbg(hsotg->dev, "Waiting for %s mode\n",
324 host_mode ? "host" : "device");
331 if (dwc2_is_host_mode(hsotg) == host_mode) {
332 dev_vdbg(hsotg->dev, "%s mode set\n",
333 host_mode ? "Host" : "Device");
338 ms = ktime_to_ms(ktime_sub(end, start));
340 if (ms >= (s64)timeout) {
341 dev_warn(hsotg->dev, "%s: Couldn't set %s mode\n",
342 __func__, host_mode ? "host" : "device");
346 usleep_range(1000, 2000);
351 * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
354 * @hsotg: Programming view of DWC_otg controller
356 static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg *hsotg)
361 if (!dwc2_hw_is_otg(hsotg))
364 /* Check if core configuration includes the IDDIG filter. */
365 ghwcfg4 = dwc2_readl(hsotg, GHWCFG4);
366 if (!(ghwcfg4 & GHWCFG4_IDDIG_FILT_EN))
370 * Check if the IDDIG debounce filter is bypassed. Available
371 * in core version >= 3.10a.
373 gsnpsid = dwc2_readl(hsotg, GSNPSID);
374 if (gsnpsid >= DWC2_CORE_REV_3_10a) {
375 u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
377 if (gotgctl & GOTGCTL_DBNCE_FLTR_BYPASS)
385 * dwc2_enter_hibernation() - Common function to enter hibernation.
387 * @hsotg: Programming view of the DWC_otg controller
388 * @is_host: True if core is in host mode.
390 * Return: 0 if successful, negative error code otherwise
392 int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg, int is_host)
395 return dwc2_host_enter_hibernation(hsotg);
397 return dwc2_gadget_enter_hibernation(hsotg);
401 * dwc2_exit_hibernation() - Common function to exit from hibernation.
403 * @hsotg: Programming view of the DWC_otg controller
404 * @rem_wakeup: Remote-wakeup, enabled in case of remote-wakeup.
405 * @reset: Enabled in case of restore with reset.
406 * @is_host: True if core is in host mode.
408 * Return: 0 if successful, negative error code otherwise
410 int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
411 int reset, int is_host)
414 return dwc2_host_exit_hibernation(hsotg, rem_wakeup, reset);
416 return dwc2_gadget_exit_hibernation(hsotg, rem_wakeup, reset);
420 * Do core a soft reset of the core. Be careful with this because it
421 * resets all the internal state machines of the core.
423 int dwc2_core_reset(struct dwc2_hsotg *hsotg, bool skip_wait)
426 bool wait_for_host_mode = false;
428 dev_vdbg(hsotg->dev, "%s()\n", __func__);
431 * If the current mode is host, either due to the force mode
432 * bit being set (which persists after core reset) or the
433 * connector id pin, a core soft reset will temporarily reset
434 * the mode to device. A delay from the IDDIG debounce filter
435 * will occur before going back to host mode.
437 * Determine whether we will go back into host mode after a
438 * reset and account for this delay after the reset.
440 if (dwc2_iddig_filter_enabled(hsotg)) {
441 u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
442 u32 gusbcfg = dwc2_readl(hsotg, GUSBCFG);
444 if (!(gotgctl & GOTGCTL_CONID_B) ||
445 (gusbcfg & GUSBCFG_FORCEHOSTMODE)) {
446 wait_for_host_mode = true;
450 /* Core Soft Reset */
451 greset = dwc2_readl(hsotg, GRSTCTL);
452 greset |= GRSTCTL_CSFTRST;
453 dwc2_writel(hsotg, greset, GRSTCTL);
455 if ((hsotg->hw_params.snpsid & DWC2_CORE_REV_MASK) <
456 (DWC2_CORE_REV_4_20a & DWC2_CORE_REV_MASK)) {
457 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL,
458 GRSTCTL_CSFTRST, 10000)) {
459 dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL_CSFTRST\n",
464 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
465 GRSTCTL_CSFTRST_DONE, 10000)) {
466 dev_warn(hsotg->dev, "%s: HANG! Soft Reset timeout GRSTCTL_CSFTRST_DONE\n",
470 greset = dwc2_readl(hsotg, GRSTCTL);
471 greset &= ~GRSTCTL_CSFTRST;
472 greset |= GRSTCTL_CSFTRST_DONE;
473 dwc2_writel(hsotg, greset, GRSTCTL);
477 * Switching from device mode to host mode by disconnecting
478 * device cable core enters and exits form hibernation.
479 * However, the fifo map remains not cleared. It results
480 * to a WARNING (WARNING: CPU: 5 PID: 0 at drivers/usb/dwc2/
481 * gadget.c:307 dwc2_hsotg_init_fifo+0x12/0x152 [dwc2])
482 * if in host mode we disconnect the micro a to b host
483 * cable. Because core reset occurs.
484 * To avoid the WARNING, fifo_map should be cleared
485 * in dwc2_core_reset() function by taking into account configs.
486 * fifo_map must be cleared only if driver is configured in
487 * "CONFIG_USB_DWC2_PERIPHERAL" or "CONFIG_USB_DWC2_DUAL_ROLE"
490 dwc2_clear_fifo_map(hsotg);
492 /* Wait for AHB master IDLE state */
493 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000)) {
494 dev_warn(hsotg->dev, "%s: HANG! AHB Idle timeout GRSTCTL GRSTCTL_AHBIDLE\n",
499 if (wait_for_host_mode && !skip_wait)
500 dwc2_wait_for_mode(hsotg, true);
506 * dwc2_force_mode() - Force the mode of the controller.
508 * Forcing the mode is needed for two cases:
510 * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
511 * controller to stay in a particular mode regardless of ID pin
512 * changes. We do this once during probe.
514 * 2) During probe we want to read reset values of the hw
515 * configuration registers that are only available in either host or
516 * device mode. We may need to force the mode if the current mode does
517 * not allow us to access the register in the mode that we want.
519 * In either case it only makes sense to force the mode if the
520 * controller hardware is OTG capable.
522 * Checks are done in this function to determine whether doing a force
523 * would be valid or not.
525 * If a force is done, it requires a IDDIG debounce filter delay if
526 * the filter is configured and enabled. We poll the current mode of
527 * the controller to account for this delay.
529 * @hsotg: Programming view of DWC_otg controller
530 * @host: Host mode flag
532 void dwc2_force_mode(struct dwc2_hsotg *hsotg, bool host)
538 dev_dbg(hsotg->dev, "Forcing mode to %s\n", host ? "host" : "device");
541 * Force mode has no effect if the hardware is not OTG.
543 if (!dwc2_hw_is_otg(hsotg))
547 * If dr_mode is either peripheral or host only, there is no
548 * need to ever force the mode to the opposite mode.
550 if (WARN_ON(host && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
553 if (WARN_ON(!host && hsotg->dr_mode == USB_DR_MODE_HOST))
556 gusbcfg = dwc2_readl(hsotg, GUSBCFG);
558 set = host ? GUSBCFG_FORCEHOSTMODE : GUSBCFG_FORCEDEVMODE;
559 clear = host ? GUSBCFG_FORCEDEVMODE : GUSBCFG_FORCEHOSTMODE;
563 dwc2_writel(hsotg, gusbcfg, GUSBCFG);
565 dwc2_wait_for_mode(hsotg, host);
570 * dwc2_clear_force_mode() - Clears the force mode bits.
572 * After clearing the bits, wait up to 100 ms to account for any
573 * potential IDDIG filter delay. We can't know if we expect this delay
574 * or not because the value of the connector ID status is affected by
575 * the force mode. We only need to call this once during probe if
578 * @hsotg: Programming view of DWC_otg controller
580 static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
584 if (!dwc2_hw_is_otg(hsotg))
587 dev_dbg(hsotg->dev, "Clearing force mode bits\n");
589 gusbcfg = dwc2_readl(hsotg, GUSBCFG);
590 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
591 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
592 dwc2_writel(hsotg, gusbcfg, GUSBCFG);
594 if (dwc2_iddig_filter_enabled(hsotg))
599 * Sets or clears force mode based on the dr_mode parameter.
601 void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
603 switch (hsotg->dr_mode) {
604 case USB_DR_MODE_HOST:
606 * NOTE: This is required for some rockchip soc based
607 * platforms on their host-only dwc2.
609 if (!dwc2_hw_is_otg(hsotg))
613 case USB_DR_MODE_PERIPHERAL:
614 dwc2_force_mode(hsotg, false);
616 case USB_DR_MODE_OTG:
617 dwc2_clear_force_mode(hsotg);
620 dev_warn(hsotg->dev, "%s() Invalid dr_mode=%d\n",
621 __func__, hsotg->dr_mode);
627 * dwc2_enable_acg - enable active clock gating feature
629 void dwc2_enable_acg(struct dwc2_hsotg *hsotg)
631 if (hsotg->params.acg_enable) {
632 u32 pcgcctl1 = dwc2_readl(hsotg, PCGCCTL1);
634 dev_dbg(hsotg->dev, "Enabling Active Clock Gating\n");
635 pcgcctl1 |= PCGCCTL1_GATEEN;
636 dwc2_writel(hsotg, pcgcctl1, PCGCCTL1);
641 * dwc2_dump_host_registers() - Prints the host registers
643 * @hsotg: Programming view of DWC_otg controller
645 * NOTE: This function will be removed once the peripheral controller code
646 * is integrated and the driver is stable
648 void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
654 dev_dbg(hsotg->dev, "Host Global Registers\n");
655 addr = hsotg->regs + HCFG;
656 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
657 (unsigned long)addr, dwc2_readl(hsotg, HCFG));
658 addr = hsotg->regs + HFIR;
659 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
660 (unsigned long)addr, dwc2_readl(hsotg, HFIR));
661 addr = hsotg->regs + HFNUM;
662 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
663 (unsigned long)addr, dwc2_readl(hsotg, HFNUM));
664 addr = hsotg->regs + HPTXSTS;
665 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
666 (unsigned long)addr, dwc2_readl(hsotg, HPTXSTS));
667 addr = hsotg->regs + HAINT;
668 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
669 (unsigned long)addr, dwc2_readl(hsotg, HAINT));
670 addr = hsotg->regs + HAINTMSK;
671 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
672 (unsigned long)addr, dwc2_readl(hsotg, HAINTMSK));
673 if (hsotg->params.dma_desc_enable) {
674 addr = hsotg->regs + HFLBADDR;
675 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
676 (unsigned long)addr, dwc2_readl(hsotg, HFLBADDR));
679 addr = hsotg->regs + HPRT0;
680 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
681 (unsigned long)addr, dwc2_readl(hsotg, HPRT0));
683 for (i = 0; i < hsotg->params.host_channels; i++) {
684 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
685 addr = hsotg->regs + HCCHAR(i);
686 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
687 (unsigned long)addr, dwc2_readl(hsotg, HCCHAR(i)));
688 addr = hsotg->regs + HCSPLT(i);
689 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
690 (unsigned long)addr, dwc2_readl(hsotg, HCSPLT(i)));
691 addr = hsotg->regs + HCINT(i);
692 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
693 (unsigned long)addr, dwc2_readl(hsotg, HCINT(i)));
694 addr = hsotg->regs + HCINTMSK(i);
695 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
696 (unsigned long)addr, dwc2_readl(hsotg, HCINTMSK(i)));
697 addr = hsotg->regs + HCTSIZ(i);
698 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
699 (unsigned long)addr, dwc2_readl(hsotg, HCTSIZ(i)));
700 addr = hsotg->regs + HCDMA(i);
701 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
702 (unsigned long)addr, dwc2_readl(hsotg, HCDMA(i)));
703 if (hsotg->params.dma_desc_enable) {
704 addr = hsotg->regs + HCDMAB(i);
705 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
706 (unsigned long)addr, dwc2_readl(hsotg,
714 * dwc2_dump_global_registers() - Prints the core global registers
716 * @hsotg: Programming view of DWC_otg controller
718 * NOTE: This function will be removed once the peripheral controller code
719 * is integrated and the driver is stable
721 void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
726 dev_dbg(hsotg->dev, "Core Global Registers\n");
727 addr = hsotg->regs + GOTGCTL;
728 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
729 (unsigned long)addr, dwc2_readl(hsotg, GOTGCTL));
730 addr = hsotg->regs + GOTGINT;
731 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
732 (unsigned long)addr, dwc2_readl(hsotg, GOTGINT));
733 addr = hsotg->regs + GAHBCFG;
734 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
735 (unsigned long)addr, dwc2_readl(hsotg, GAHBCFG));
736 addr = hsotg->regs + GUSBCFG;
737 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
738 (unsigned long)addr, dwc2_readl(hsotg, GUSBCFG));
739 addr = hsotg->regs + GRSTCTL;
740 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
741 (unsigned long)addr, dwc2_readl(hsotg, GRSTCTL));
742 addr = hsotg->regs + GINTSTS;
743 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
744 (unsigned long)addr, dwc2_readl(hsotg, GINTSTS));
745 addr = hsotg->regs + GINTMSK;
746 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
747 (unsigned long)addr, dwc2_readl(hsotg, GINTMSK));
748 addr = hsotg->regs + GRXSTSR;
749 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
750 (unsigned long)addr, dwc2_readl(hsotg, GRXSTSR));
751 addr = hsotg->regs + GRXFSIZ;
752 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
753 (unsigned long)addr, dwc2_readl(hsotg, GRXFSIZ));
754 addr = hsotg->regs + GNPTXFSIZ;
755 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
756 (unsigned long)addr, dwc2_readl(hsotg, GNPTXFSIZ));
757 addr = hsotg->regs + GNPTXSTS;
758 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
759 (unsigned long)addr, dwc2_readl(hsotg, GNPTXSTS));
760 addr = hsotg->regs + GI2CCTL;
761 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
762 (unsigned long)addr, dwc2_readl(hsotg, GI2CCTL));
763 addr = hsotg->regs + GPVNDCTL;
764 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
765 (unsigned long)addr, dwc2_readl(hsotg, GPVNDCTL));
766 addr = hsotg->regs + GGPIO;
767 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
768 (unsigned long)addr, dwc2_readl(hsotg, GGPIO));
769 addr = hsotg->regs + GUID;
770 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
771 (unsigned long)addr, dwc2_readl(hsotg, GUID));
772 addr = hsotg->regs + GSNPSID;
773 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
774 (unsigned long)addr, dwc2_readl(hsotg, GSNPSID));
775 addr = hsotg->regs + GHWCFG1;
776 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
777 (unsigned long)addr, dwc2_readl(hsotg, GHWCFG1));
778 addr = hsotg->regs + GHWCFG2;
779 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
780 (unsigned long)addr, dwc2_readl(hsotg, GHWCFG2));
781 addr = hsotg->regs + GHWCFG3;
782 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
783 (unsigned long)addr, dwc2_readl(hsotg, GHWCFG3));
784 addr = hsotg->regs + GHWCFG4;
785 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
786 (unsigned long)addr, dwc2_readl(hsotg, GHWCFG4));
787 addr = hsotg->regs + GLPMCFG;
788 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
789 (unsigned long)addr, dwc2_readl(hsotg, GLPMCFG));
790 addr = hsotg->regs + GPWRDN;
791 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
792 (unsigned long)addr, dwc2_readl(hsotg, GPWRDN));
793 addr = hsotg->regs + GDFIFOCFG;
794 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
795 (unsigned long)addr, dwc2_readl(hsotg, GDFIFOCFG));
796 addr = hsotg->regs + HPTXFSIZ;
797 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
798 (unsigned long)addr, dwc2_readl(hsotg, HPTXFSIZ));
800 addr = hsotg->regs + PCGCTL;
801 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
802 (unsigned long)addr, dwc2_readl(hsotg, PCGCTL));
807 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
809 * @hsotg: Programming view of DWC_otg controller
810 * @num: Tx FIFO to flush
812 void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
816 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
818 /* Wait for AHB master IDLE state */
819 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
820 dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
823 greset = GRSTCTL_TXFFLSH;
824 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
825 dwc2_writel(hsotg, greset, GRSTCTL);
827 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 10000))
828 dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_TXFFLSH\n",
831 /* Wait for at least 3 PHY Clocks */
836 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
838 * @hsotg: Programming view of DWC_otg controller
840 void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
844 dev_vdbg(hsotg->dev, "%s()\n", __func__);
846 /* Wait for AHB master IDLE state */
847 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL, GRSTCTL_AHBIDLE, 10000))
848 dev_warn(hsotg->dev, "%s: HANG! AHB Idle GRSCTL\n",
851 greset = GRSTCTL_RXFFLSH;
852 dwc2_writel(hsotg, greset, GRSTCTL);
854 /* Wait for RxFIFO flush done */
855 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_RXFFLSH, 10000))
856 dev_warn(hsotg->dev, "%s: HANG! timeout GRSTCTL GRSTCTL_RXFFLSH\n",
859 /* Wait for at least 3 PHY Clocks */
863 bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
865 if (dwc2_readl(hsotg, GSNPSID) == 0xffffffff)
872 * dwc2_enable_global_interrupts() - Enables the controller's Global
873 * Interrupt in the AHB Config register
875 * @hsotg: Programming view of DWC_otg controller
877 void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
879 u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
881 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
882 dwc2_writel(hsotg, ahbcfg, GAHBCFG);
886 * dwc2_disable_global_interrupts() - Disables the controller's Global
887 * Interrupt in the AHB Config register
889 * @hsotg: Programming view of DWC_otg controller
891 void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
893 u32 ahbcfg = dwc2_readl(hsotg, GAHBCFG);
895 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
896 dwc2_writel(hsotg, ahbcfg, GAHBCFG);
899 /* Returns the controller's GHWCFG2.OTG_MODE. */
900 unsigned int dwc2_op_mode(struct dwc2_hsotg *hsotg)
902 u32 ghwcfg2 = dwc2_readl(hsotg, GHWCFG2);
904 return (ghwcfg2 & GHWCFG2_OP_MODE_MASK) >>
905 GHWCFG2_OP_MODE_SHIFT;
908 /* Returns true if the controller is capable of DRD. */
909 bool dwc2_hw_is_otg(struct dwc2_hsotg *hsotg)
911 unsigned int op_mode = dwc2_op_mode(hsotg);
913 return (op_mode == GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) ||
914 (op_mode == GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE) ||
915 (op_mode == GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE);
918 /* Returns true if the controller is host-only. */
919 bool dwc2_hw_is_host(struct dwc2_hsotg *hsotg)
921 unsigned int op_mode = dwc2_op_mode(hsotg);
923 return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_HOST) ||
924 (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST);
927 /* Returns true if the controller is device-only. */
928 bool dwc2_hw_is_device(struct dwc2_hsotg *hsotg)
930 unsigned int op_mode = dwc2_op_mode(hsotg);
932 return (op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE) ||
933 (op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE);
937 * dwc2_hsotg_wait_bit_set - Waits for bit to be set.
938 * @hsotg: Programming view of DWC_otg controller.
939 * @offset: Register's offset where bit/bits must be set.
940 * @mask: Mask of the bit/bits which must be set.
941 * @timeout: Timeout to wait.
943 * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
945 int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
950 for (i = 0; i < timeout; i++) {
951 if (dwc2_readl(hsotg, offset) & mask)
960 * dwc2_hsotg_wait_bit_clear - Waits for bit to be clear.
961 * @hsotg: Programming view of DWC_otg controller.
962 * @offset: Register's offset where bit/bits must be set.
963 * @mask: Mask of the bit/bits which must be set.
964 * @timeout: Timeout to wait.
966 * Return: 0 if bit/bits are set or -ETIMEDOUT in case of timeout.
968 int dwc2_hsotg_wait_bit_clear(struct dwc2_hsotg *hsotg, u32 offset, u32 mask,
973 for (i = 0; i < timeout; i++) {
974 if (!(dwc2_readl(hsotg, offset) & mask))
983 * Initializes the FSLSPClkSel field of the HCFG register depending on the
986 void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
990 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
991 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
992 hsotg->params.ulpi_fs_ls) ||
993 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
995 val = HCFG_FSLSPCLKSEL_48_MHZ;
997 /* High speed PHY running at full speed or high speed */
998 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
1001 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
1002 hcfg = dwc2_readl(hsotg, HCFG);
1003 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
1004 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
1005 dwc2_writel(hsotg, hcfg, HCFG);
1008 static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
1010 u32 usbcfg, ggpio, i2cctl;
1014 * core_init() is now called on every switch so only call the
1015 * following for the first time through
1018 dev_dbg(hsotg->dev, "FS PHY selected\n");
1020 usbcfg = dwc2_readl(hsotg, GUSBCFG);
1021 if (!(usbcfg & GUSBCFG_PHYSEL)) {
1022 usbcfg |= GUSBCFG_PHYSEL;
1023 dwc2_writel(hsotg, usbcfg, GUSBCFG);
1025 /* Reset after a PHY select */
1026 retval = dwc2_core_reset(hsotg, false);
1030 "%s: Reset failed, aborting", __func__);
1035 if (hsotg->params.activate_stm_fs_transceiver) {
1036 ggpio = dwc2_readl(hsotg, GGPIO);
1037 if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
1038 dev_dbg(hsotg->dev, "Activating transceiver\n");
1040 * STM32F4x9 uses the GGPIO register as general
1041 * core configuration register.
1043 ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
1044 dwc2_writel(hsotg, ggpio, GGPIO);
1050 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
1051 * do this on HNP Dev/Host mode switches (done in dev_init and
1054 if (dwc2_is_host_mode(hsotg))
1055 dwc2_init_fs_ls_pclk_sel(hsotg);
1057 if (hsotg->params.i2c_enable) {
1058 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
1060 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
1061 usbcfg = dwc2_readl(hsotg, GUSBCFG);
1062 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
1063 dwc2_writel(hsotg, usbcfg, GUSBCFG);
1065 /* Program GI2CCTL.I2CEn */
1066 i2cctl = dwc2_readl(hsotg, GI2CCTL);
1067 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
1068 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
1069 i2cctl &= ~GI2CCTL_I2CEN;
1070 dwc2_writel(hsotg, i2cctl, GI2CCTL);
1071 i2cctl |= GI2CCTL_I2CEN;
1072 dwc2_writel(hsotg, i2cctl, GI2CCTL);
1078 static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
1080 u32 usbcfg, usbcfg_old;
1086 usbcfg = dwc2_readl(hsotg, GUSBCFG);
1087 usbcfg_old = usbcfg;
1090 * HS PHY parameters. These parameters are preserved during soft reset
1091 * so only program the first time. Do a soft reset immediately after
1094 switch (hsotg->params.phy_type) {
1095 case DWC2_PHY_TYPE_PARAM_ULPI:
1096 /* ULPI interface */
1097 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
1098 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
1099 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
1100 if (hsotg->params.phy_ulpi_ddr)
1101 usbcfg |= GUSBCFG_DDRSEL;
1103 /* Set external VBUS indicator as needed. */
1104 if (hsotg->params.oc_disable)
1105 usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
1106 GUSBCFG_INDICATORPASSTHROUGH);
1108 case DWC2_PHY_TYPE_PARAM_UTMI:
1109 /* UTMI+ interface */
1110 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
1111 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
1112 if (hsotg->params.phy_utmi_width == 16)
1113 usbcfg |= GUSBCFG_PHYIF16;
1115 /* Set turnaround time */
1116 if (dwc2_is_device_mode(hsotg)) {
1117 usbcfg &= ~GUSBCFG_USBTRDTIM_MASK;
1118 if (hsotg->params.phy_utmi_width == 16)
1119 usbcfg |= 5 << GUSBCFG_USBTRDTIM_SHIFT;
1121 usbcfg |= 9 << GUSBCFG_USBTRDTIM_SHIFT;
1125 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
1129 if (usbcfg != usbcfg_old) {
1130 dwc2_writel(hsotg, usbcfg, GUSBCFG);
1132 /* Reset after setting the PHY parameters */
1133 retval = dwc2_core_reset(hsotg, false);
1136 "%s: Reset failed, aborting", __func__);
1144 int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
1149 if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
1150 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
1151 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
1152 /* If FS/LS mode with FS/LS PHY */
1153 retval = dwc2_fs_phy_init(hsotg, select_phy);
1157 /* High speed PHY */
1158 retval = dwc2_hs_phy_init(hsotg, select_phy);
1163 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
1164 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
1165 hsotg->params.ulpi_fs_ls) {
1166 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
1167 usbcfg = dwc2_readl(hsotg, GUSBCFG);
1168 usbcfg |= GUSBCFG_ULPI_FS_LS;
1169 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
1170 dwc2_writel(hsotg, usbcfg, GUSBCFG);
1172 usbcfg = dwc2_readl(hsotg, GUSBCFG);
1173 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
1174 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
1175 dwc2_writel(hsotg, usbcfg, GUSBCFG);
1181 MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
1182 MODULE_AUTHOR("Synopsys, Inc.");
1183 MODULE_LICENSE("Dual BSD/GPL");