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 static int dwc2_backup_global_registers(struct dwc2_hsotg
*hsotg
)
69 struct dwc2_gregs_backup
*gr
;
72 /* Backup global regs */
73 gr
= &hsotg
->gr_backup
;
75 gr
->gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
76 gr
->gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
77 gr
->gahbcfg
= dwc2_readl(hsotg
->regs
+ GAHBCFG
);
78 gr
->gusbcfg
= dwc2_readl(hsotg
->regs
+ GUSBCFG
);
79 gr
->grxfsiz
= dwc2_readl(hsotg
->regs
+ GRXFSIZ
);
80 gr
->gnptxfsiz
= dwc2_readl(hsotg
->regs
+ GNPTXFSIZ
);
81 gr
->hptxfsiz
= dwc2_readl(hsotg
->regs
+ HPTXFSIZ
);
82 gr
->gdfifocfg
= dwc2_readl(hsotg
->regs
+ GDFIFOCFG
);
83 for (i
= 0; i
< MAX_EPS_CHANNELS
; i
++)
84 gr
->dtxfsiz
[i
] = dwc2_readl(hsotg
->regs
+ DPTXFSIZN(i
));
91 * dwc2_restore_global_registers() - Restore controller global registers.
92 * When resuming usb bus, device registers needs to be restored
93 * if controller power were disabled.
95 * @hsotg: Programming view of the DWC_otg controller
97 static int dwc2_restore_global_registers(struct dwc2_hsotg
*hsotg
)
99 struct dwc2_gregs_backup
*gr
;
102 dev_dbg(hsotg
->dev
, "%s\n", __func__
);
104 /* Restore global regs */
105 gr
= &hsotg
->gr_backup
;
107 dev_err(hsotg
->dev
, "%s: no global registers to restore\n",
113 dwc2_writel(0xffffffff, hsotg
->regs
+ GINTSTS
);
114 dwc2_writel(gr
->gotgctl
, hsotg
->regs
+ GOTGCTL
);
115 dwc2_writel(gr
->gintmsk
, hsotg
->regs
+ GINTMSK
);
116 dwc2_writel(gr
->gusbcfg
, hsotg
->regs
+ GUSBCFG
);
117 dwc2_writel(gr
->gahbcfg
, hsotg
->regs
+ GAHBCFG
);
118 dwc2_writel(gr
->grxfsiz
, hsotg
->regs
+ GRXFSIZ
);
119 dwc2_writel(gr
->gnptxfsiz
, hsotg
->regs
+ GNPTXFSIZ
);
120 dwc2_writel(gr
->hptxfsiz
, hsotg
->regs
+ HPTXFSIZ
);
121 dwc2_writel(gr
->gdfifocfg
, hsotg
->regs
+ GDFIFOCFG
);
122 for (i
= 0; i
< MAX_EPS_CHANNELS
; i
++)
123 dwc2_writel(gr
->dtxfsiz
[i
], hsotg
->regs
+ DPTXFSIZN(i
));
129 * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
131 * @hsotg: Programming view of the DWC_otg controller
132 * @restore: Controller registers need to be restored
134 int dwc2_exit_hibernation(struct dwc2_hsotg
*hsotg
, bool restore
)
139 if (!hsotg
->params
.hibernation
)
142 pcgcctl
= dwc2_readl(hsotg
->regs
+ PCGCTL
);
143 pcgcctl
&= ~PCGCTL_STOPPCLK
;
144 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
146 pcgcctl
= dwc2_readl(hsotg
->regs
+ PCGCTL
);
147 pcgcctl
&= ~PCGCTL_PWRCLMP
;
148 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
150 pcgcctl
= dwc2_readl(hsotg
->regs
+ PCGCTL
);
151 pcgcctl
&= ~PCGCTL_RSTPDWNMODULE
;
152 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
156 ret
= dwc2_restore_global_registers(hsotg
);
158 dev_err(hsotg
->dev
, "%s: failed to restore registers\n",
162 if (dwc2_is_host_mode(hsotg
)) {
163 ret
= dwc2_restore_host_registers(hsotg
);
165 dev_err(hsotg
->dev
, "%s: failed to restore host registers\n",
170 ret
= dwc2_restore_device_registers(hsotg
);
172 dev_err(hsotg
->dev
, "%s: failed to restore device registers\n",
183 * dwc2_enter_hibernation() - Put controller in Partial Power Down.
185 * @hsotg: Programming view of the DWC_otg controller
187 int dwc2_enter_hibernation(struct dwc2_hsotg
*hsotg
)
192 if (!hsotg
->params
.hibernation
)
195 /* Backup all registers */
196 ret
= dwc2_backup_global_registers(hsotg
);
198 dev_err(hsotg
->dev
, "%s: failed to backup global registers\n",
203 if (dwc2_is_host_mode(hsotg
)) {
204 ret
= dwc2_backup_host_registers(hsotg
);
206 dev_err(hsotg
->dev
, "%s: failed to backup host registers\n",
211 ret
= dwc2_backup_device_registers(hsotg
);
213 dev_err(hsotg
->dev
, "%s: failed to backup device registers\n",
220 * Clear any pending interrupts since dwc2 will not be able to
221 * clear them after entering hibernation.
223 dwc2_writel(0xffffffff, hsotg
->regs
+ GINTSTS
);
225 /* Put the controller in low power state */
226 pcgcctl
= dwc2_readl(hsotg
->regs
+ PCGCTL
);
228 pcgcctl
|= PCGCTL_PWRCLMP
;
229 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
232 pcgcctl
|= PCGCTL_RSTPDWNMODULE
;
233 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
236 pcgcctl
|= PCGCTL_STOPPCLK
;
237 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
243 * dwc2_wait_for_mode() - Waits for the controller mode.
244 * @hsotg: Programming view of the DWC_otg controller.
245 * @host_mode: If true, waits for host mode, otherwise device mode.
247 static void dwc2_wait_for_mode(struct dwc2_hsotg
*hsotg
,
252 unsigned int timeout
= 110;
254 dev_vdbg(hsotg
->dev
, "Waiting for %s mode\n",
255 host_mode
? "host" : "device");
262 if (dwc2_is_host_mode(hsotg
) == host_mode
) {
263 dev_vdbg(hsotg
->dev
, "%s mode set\n",
264 host_mode
? "Host" : "Device");
269 ms
= ktime_to_ms(ktime_sub(end
, start
));
271 if (ms
>= (s64
)timeout
) {
272 dev_warn(hsotg
->dev
, "%s: Couldn't set %s mode\n",
273 __func__
, host_mode
? "host" : "device");
277 usleep_range(1000, 2000);
282 * dwc2_iddig_filter_enabled() - Returns true if the IDDIG debounce
285 static bool dwc2_iddig_filter_enabled(struct dwc2_hsotg
*hsotg
)
290 if (!dwc2_hw_is_otg(hsotg
))
293 /* Check if core configuration includes the IDDIG filter. */
294 ghwcfg4
= dwc2_readl(hsotg
->regs
+ GHWCFG4
);
295 if (!(ghwcfg4
& GHWCFG4_IDDIG_FILT_EN
))
299 * Check if the IDDIG debounce filter is bypassed. Available
300 * in core version >= 3.10a.
302 gsnpsid
= dwc2_readl(hsotg
->regs
+ GSNPSID
);
303 if (gsnpsid
>= DWC2_CORE_REV_3_10a
) {
304 u32 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
306 if (gotgctl
& GOTGCTL_DBNCE_FLTR_BYPASS
)
314 * Do core a soft reset of the core. Be careful with this because it
315 * resets all the internal state machines of the core.
317 int dwc2_core_reset(struct dwc2_hsotg
*hsotg
, bool skip_wait
)
321 bool wait_for_host_mode
= false;
323 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
326 * If the current mode is host, either due to the force mode
327 * bit being set (which persists after core reset) or the
328 * connector id pin, a core soft reset will temporarily reset
329 * the mode to device. A delay from the IDDIG debounce filter
330 * will occur before going back to host mode.
332 * Determine whether we will go back into host mode after a
333 * reset and account for this delay after the reset.
335 if (dwc2_iddig_filter_enabled(hsotg
)) {
336 u32 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
337 u32 gusbcfg
= dwc2_readl(hsotg
->regs
+ GUSBCFG
);
339 if (!(gotgctl
& GOTGCTL_CONID_B
) ||
340 (gusbcfg
& GUSBCFG_FORCEHOSTMODE
)) {
341 wait_for_host_mode
= true;
345 /* Core Soft Reset */
346 greset
= dwc2_readl(hsotg
->regs
+ GRSTCTL
);
347 greset
|= GRSTCTL_CSFTRST
;
348 dwc2_writel(greset
, hsotg
->regs
+ GRSTCTL
);
351 greset
= dwc2_readl(hsotg
->regs
+ GRSTCTL
);
354 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
358 } while (greset
& GRSTCTL_CSFTRST
);
360 /* Wait for AHB master IDLE state */
364 greset
= dwc2_readl(hsotg
->regs
+ GRSTCTL
);
367 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
371 } while (!(greset
& GRSTCTL_AHBIDLE
));
373 if (wait_for_host_mode
&& !skip_wait
)
374 dwc2_wait_for_mode(hsotg
, true);
380 * Force the mode of the controller.
382 * Forcing the mode is needed for two cases:
384 * 1) If the dr_mode is set to either HOST or PERIPHERAL we force the
385 * controller to stay in a particular mode regardless of ID pin
386 * changes. We do this usually after a core reset.
388 * 2) During probe we want to read reset values of the hw
389 * configuration registers that are only available in either host or
390 * device mode. We may need to force the mode if the current mode does
391 * not allow us to access the register in the mode that we want.
393 * In either case it only makes sense to force the mode if the
394 * controller hardware is OTG capable.
396 * Checks are done in this function to determine whether doing a force
397 * would be valid or not.
399 * If a force is done, it requires a IDDIG debounce filter delay if
400 * the filter is configured and enabled. We poll the current mode of
401 * the controller to account for this delay.
403 static bool dwc2_force_mode(struct dwc2_hsotg
*hsotg
, bool host
)
409 dev_dbg(hsotg
->dev
, "Forcing mode to %s\n", host
? "host" : "device");
412 * Force mode has no effect if the hardware is not OTG.
414 if (!dwc2_hw_is_otg(hsotg
))
418 * If dr_mode is either peripheral or host only, there is no
419 * need to ever force the mode to the opposite mode.
421 if (WARN_ON(host
&& hsotg
->dr_mode
== USB_DR_MODE_PERIPHERAL
))
424 if (WARN_ON(!host
&& hsotg
->dr_mode
== USB_DR_MODE_HOST
))
427 gusbcfg
= dwc2_readl(hsotg
->regs
+ GUSBCFG
);
429 set
= host
? GUSBCFG_FORCEHOSTMODE
: GUSBCFG_FORCEDEVMODE
;
430 clear
= host
? GUSBCFG_FORCEDEVMODE
: GUSBCFG_FORCEHOSTMODE
;
434 dwc2_writel(gusbcfg
, hsotg
->regs
+ GUSBCFG
);
436 dwc2_wait_for_mode(hsotg
, host
);
441 * dwc2_clear_force_mode() - Clears the force mode bits.
443 * After clearing the bits, wait up to 100 ms to account for any
444 * potential IDDIG filter delay. We can't know if we expect this delay
445 * or not because the value of the connector ID status is affected by
446 * the force mode. We only need to call this once during probe if
449 void dwc2_clear_force_mode(struct dwc2_hsotg
*hsotg
)
453 gusbcfg
= dwc2_readl(hsotg
->regs
+ GUSBCFG
);
454 gusbcfg
&= ~GUSBCFG_FORCEHOSTMODE
;
455 gusbcfg
&= ~GUSBCFG_FORCEDEVMODE
;
456 dwc2_writel(gusbcfg
, hsotg
->regs
+ GUSBCFG
);
458 if (dwc2_iddig_filter_enabled(hsotg
))
463 * Sets or clears force mode based on the dr_mode parameter.
465 void dwc2_force_dr_mode(struct dwc2_hsotg
*hsotg
)
469 switch (hsotg
->dr_mode
) {
470 case USB_DR_MODE_HOST
:
471 ret
= dwc2_force_mode(hsotg
, true);
473 * NOTE: This is required for some rockchip soc based
474 * platforms on their host-only dwc2.
480 case USB_DR_MODE_PERIPHERAL
:
481 dwc2_force_mode(hsotg
, false);
483 case USB_DR_MODE_OTG
:
484 dwc2_clear_force_mode(hsotg
);
487 dev_warn(hsotg
->dev
, "%s() Invalid dr_mode=%d\n",
488 __func__
, hsotg
->dr_mode
);
494 * Do core a soft reset of the core. Be careful with this because it
495 * resets all the internal state machines of the core.
497 * Additionally this will apply force mode as per the hsotg->dr_mode
500 int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg
*hsotg
)
504 retval
= dwc2_core_reset(hsotg
, false);
508 dwc2_force_dr_mode(hsotg
);
513 * dwc2_dump_host_registers() - Prints the host registers
515 * @hsotg: Programming view of DWC_otg controller
517 * NOTE: This function will be removed once the peripheral controller code
518 * is integrated and the driver is stable
520 void dwc2_dump_host_registers(struct dwc2_hsotg
*hsotg
)
526 dev_dbg(hsotg
->dev
, "Host Global Registers\n");
527 addr
= hsotg
->regs
+ HCFG
;
528 dev_dbg(hsotg
->dev
, "HCFG @0x%08lX : 0x%08X\n",
529 (unsigned long)addr
, dwc2_readl(addr
));
530 addr
= hsotg
->regs
+ HFIR
;
531 dev_dbg(hsotg
->dev
, "HFIR @0x%08lX : 0x%08X\n",
532 (unsigned long)addr
, dwc2_readl(addr
));
533 addr
= hsotg
->regs
+ HFNUM
;
534 dev_dbg(hsotg
->dev
, "HFNUM @0x%08lX : 0x%08X\n",
535 (unsigned long)addr
, dwc2_readl(addr
));
536 addr
= hsotg
->regs
+ HPTXSTS
;
537 dev_dbg(hsotg
->dev
, "HPTXSTS @0x%08lX : 0x%08X\n",
538 (unsigned long)addr
, dwc2_readl(addr
));
539 addr
= hsotg
->regs
+ HAINT
;
540 dev_dbg(hsotg
->dev
, "HAINT @0x%08lX : 0x%08X\n",
541 (unsigned long)addr
, dwc2_readl(addr
));
542 addr
= hsotg
->regs
+ HAINTMSK
;
543 dev_dbg(hsotg
->dev
, "HAINTMSK @0x%08lX : 0x%08X\n",
544 (unsigned long)addr
, dwc2_readl(addr
));
545 if (hsotg
->params
.dma_desc_enable
) {
546 addr
= hsotg
->regs
+ HFLBADDR
;
547 dev_dbg(hsotg
->dev
, "HFLBADDR @0x%08lX : 0x%08X\n",
548 (unsigned long)addr
, dwc2_readl(addr
));
551 addr
= hsotg
->regs
+ HPRT0
;
552 dev_dbg(hsotg
->dev
, "HPRT0 @0x%08lX : 0x%08X\n",
553 (unsigned long)addr
, dwc2_readl(addr
));
555 for (i
= 0; i
< hsotg
->params
.host_channels
; i
++) {
556 dev_dbg(hsotg
->dev
, "Host Channel %d Specific Registers\n", i
);
557 addr
= hsotg
->regs
+ HCCHAR(i
);
558 dev_dbg(hsotg
->dev
, "HCCHAR @0x%08lX : 0x%08X\n",
559 (unsigned long)addr
, dwc2_readl(addr
));
560 addr
= hsotg
->regs
+ HCSPLT(i
);
561 dev_dbg(hsotg
->dev
, "HCSPLT @0x%08lX : 0x%08X\n",
562 (unsigned long)addr
, dwc2_readl(addr
));
563 addr
= hsotg
->regs
+ HCINT(i
);
564 dev_dbg(hsotg
->dev
, "HCINT @0x%08lX : 0x%08X\n",
565 (unsigned long)addr
, dwc2_readl(addr
));
566 addr
= hsotg
->regs
+ HCINTMSK(i
);
567 dev_dbg(hsotg
->dev
, "HCINTMSK @0x%08lX : 0x%08X\n",
568 (unsigned long)addr
, dwc2_readl(addr
));
569 addr
= hsotg
->regs
+ HCTSIZ(i
);
570 dev_dbg(hsotg
->dev
, "HCTSIZ @0x%08lX : 0x%08X\n",
571 (unsigned long)addr
, dwc2_readl(addr
));
572 addr
= hsotg
->regs
+ HCDMA(i
);
573 dev_dbg(hsotg
->dev
, "HCDMA @0x%08lX : 0x%08X\n",
574 (unsigned long)addr
, dwc2_readl(addr
));
575 if (hsotg
->params
.dma_desc_enable
) {
576 addr
= hsotg
->regs
+ HCDMAB(i
);
577 dev_dbg(hsotg
->dev
, "HCDMAB @0x%08lX : 0x%08X\n",
578 (unsigned long)addr
, dwc2_readl(addr
));
585 * dwc2_dump_global_registers() - Prints the core global registers
587 * @hsotg: Programming view of DWC_otg controller
589 * NOTE: This function will be removed once the peripheral controller code
590 * is integrated and the driver is stable
592 void dwc2_dump_global_registers(struct dwc2_hsotg
*hsotg
)
597 dev_dbg(hsotg
->dev
, "Core Global Registers\n");
598 addr
= hsotg
->regs
+ GOTGCTL
;
599 dev_dbg(hsotg
->dev
, "GOTGCTL @0x%08lX : 0x%08X\n",
600 (unsigned long)addr
, dwc2_readl(addr
));
601 addr
= hsotg
->regs
+ GOTGINT
;
602 dev_dbg(hsotg
->dev
, "GOTGINT @0x%08lX : 0x%08X\n",
603 (unsigned long)addr
, dwc2_readl(addr
));
604 addr
= hsotg
->regs
+ GAHBCFG
;
605 dev_dbg(hsotg
->dev
, "GAHBCFG @0x%08lX : 0x%08X\n",
606 (unsigned long)addr
, dwc2_readl(addr
));
607 addr
= hsotg
->regs
+ GUSBCFG
;
608 dev_dbg(hsotg
->dev
, "GUSBCFG @0x%08lX : 0x%08X\n",
609 (unsigned long)addr
, dwc2_readl(addr
));
610 addr
= hsotg
->regs
+ GRSTCTL
;
611 dev_dbg(hsotg
->dev
, "GRSTCTL @0x%08lX : 0x%08X\n",
612 (unsigned long)addr
, dwc2_readl(addr
));
613 addr
= hsotg
->regs
+ GINTSTS
;
614 dev_dbg(hsotg
->dev
, "GINTSTS @0x%08lX : 0x%08X\n",
615 (unsigned long)addr
, dwc2_readl(addr
));
616 addr
= hsotg
->regs
+ GINTMSK
;
617 dev_dbg(hsotg
->dev
, "GINTMSK @0x%08lX : 0x%08X\n",
618 (unsigned long)addr
, dwc2_readl(addr
));
619 addr
= hsotg
->regs
+ GRXSTSR
;
620 dev_dbg(hsotg
->dev
, "GRXSTSR @0x%08lX : 0x%08X\n",
621 (unsigned long)addr
, dwc2_readl(addr
));
622 addr
= hsotg
->regs
+ GRXFSIZ
;
623 dev_dbg(hsotg
->dev
, "GRXFSIZ @0x%08lX : 0x%08X\n",
624 (unsigned long)addr
, dwc2_readl(addr
));
625 addr
= hsotg
->regs
+ GNPTXFSIZ
;
626 dev_dbg(hsotg
->dev
, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
627 (unsigned long)addr
, dwc2_readl(addr
));
628 addr
= hsotg
->regs
+ GNPTXSTS
;
629 dev_dbg(hsotg
->dev
, "GNPTXSTS @0x%08lX : 0x%08X\n",
630 (unsigned long)addr
, dwc2_readl(addr
));
631 addr
= hsotg
->regs
+ GI2CCTL
;
632 dev_dbg(hsotg
->dev
, "GI2CCTL @0x%08lX : 0x%08X\n",
633 (unsigned long)addr
, dwc2_readl(addr
));
634 addr
= hsotg
->regs
+ GPVNDCTL
;
635 dev_dbg(hsotg
->dev
, "GPVNDCTL @0x%08lX : 0x%08X\n",
636 (unsigned long)addr
, dwc2_readl(addr
));
637 addr
= hsotg
->regs
+ GGPIO
;
638 dev_dbg(hsotg
->dev
, "GGPIO @0x%08lX : 0x%08X\n",
639 (unsigned long)addr
, dwc2_readl(addr
));
640 addr
= hsotg
->regs
+ GUID
;
641 dev_dbg(hsotg
->dev
, "GUID @0x%08lX : 0x%08X\n",
642 (unsigned long)addr
, dwc2_readl(addr
));
643 addr
= hsotg
->regs
+ GSNPSID
;
644 dev_dbg(hsotg
->dev
, "GSNPSID @0x%08lX : 0x%08X\n",
645 (unsigned long)addr
, dwc2_readl(addr
));
646 addr
= hsotg
->regs
+ GHWCFG1
;
647 dev_dbg(hsotg
->dev
, "GHWCFG1 @0x%08lX : 0x%08X\n",
648 (unsigned long)addr
, dwc2_readl(addr
));
649 addr
= hsotg
->regs
+ GHWCFG2
;
650 dev_dbg(hsotg
->dev
, "GHWCFG2 @0x%08lX : 0x%08X\n",
651 (unsigned long)addr
, dwc2_readl(addr
));
652 addr
= hsotg
->regs
+ GHWCFG3
;
653 dev_dbg(hsotg
->dev
, "GHWCFG3 @0x%08lX : 0x%08X\n",
654 (unsigned long)addr
, dwc2_readl(addr
));
655 addr
= hsotg
->regs
+ GHWCFG4
;
656 dev_dbg(hsotg
->dev
, "GHWCFG4 @0x%08lX : 0x%08X\n",
657 (unsigned long)addr
, dwc2_readl(addr
));
658 addr
= hsotg
->regs
+ GLPMCFG
;
659 dev_dbg(hsotg
->dev
, "GLPMCFG @0x%08lX : 0x%08X\n",
660 (unsigned long)addr
, dwc2_readl(addr
));
661 addr
= hsotg
->regs
+ GPWRDN
;
662 dev_dbg(hsotg
->dev
, "GPWRDN @0x%08lX : 0x%08X\n",
663 (unsigned long)addr
, dwc2_readl(addr
));
664 addr
= hsotg
->regs
+ GDFIFOCFG
;
665 dev_dbg(hsotg
->dev
, "GDFIFOCFG @0x%08lX : 0x%08X\n",
666 (unsigned long)addr
, dwc2_readl(addr
));
667 addr
= hsotg
->regs
+ HPTXFSIZ
;
668 dev_dbg(hsotg
->dev
, "HPTXFSIZ @0x%08lX : 0x%08X\n",
669 (unsigned long)addr
, dwc2_readl(addr
));
671 addr
= hsotg
->regs
+ PCGCTL
;
672 dev_dbg(hsotg
->dev
, "PCGCTL @0x%08lX : 0x%08X\n",
673 (unsigned long)addr
, dwc2_readl(addr
));
678 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
680 * @hsotg: Programming view of DWC_otg controller
681 * @num: Tx FIFO to flush
683 void dwc2_flush_tx_fifo(struct dwc2_hsotg
*hsotg
, const int num
)
688 dev_vdbg(hsotg
->dev
, "Flush Tx FIFO %d\n", num
);
690 greset
= GRSTCTL_TXFFLSH
;
691 greset
|= num
<< GRSTCTL_TXFNUM_SHIFT
& GRSTCTL_TXFNUM_MASK
;
692 dwc2_writel(greset
, hsotg
->regs
+ GRSTCTL
);
695 greset
= dwc2_readl(hsotg
->regs
+ GRSTCTL
);
696 if (++count
> 10000) {
698 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
700 dwc2_readl(hsotg
->regs
+ GNPTXSTS
));
704 } while (greset
& GRSTCTL_TXFFLSH
);
706 /* Wait for at least 3 PHY Clocks */
711 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
713 * @hsotg: Programming view of DWC_otg controller
715 void dwc2_flush_rx_fifo(struct dwc2_hsotg
*hsotg
)
720 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
722 greset
= GRSTCTL_RXFFLSH
;
723 dwc2_writel(greset
, hsotg
->regs
+ GRSTCTL
);
726 greset
= dwc2_readl(hsotg
->regs
+ GRSTCTL
);
727 if (++count
> 10000) {
728 dev_warn(hsotg
->dev
, "%s() HANG! GRSTCTL=%0x\n",
733 } while (greset
& GRSTCTL_RXFFLSH
);
735 /* Wait for at least 3 PHY Clocks */
740 * Forces either host or device mode if the controller is not
741 * currently in that mode.
743 * Returns true if the mode was forced.
745 bool dwc2_force_mode_if_needed(struct dwc2_hsotg
*hsotg
, bool host
)
747 if (host
&& dwc2_is_host_mode(hsotg
))
749 else if (!host
&& dwc2_is_device_mode(hsotg
))
752 return dwc2_force_mode(hsotg
, host
);
755 bool dwc2_is_controller_alive(struct dwc2_hsotg
*hsotg
)
757 if (dwc2_readl(hsotg
->regs
+ GSNPSID
) == 0xffffffff)
764 * dwc2_enable_global_interrupts() - Enables the controller's Global
765 * Interrupt in the AHB Config register
767 * @hsotg: Programming view of DWC_otg controller
769 void dwc2_enable_global_interrupts(struct dwc2_hsotg
*hsotg
)
771 u32 ahbcfg
= dwc2_readl(hsotg
->regs
+ GAHBCFG
);
773 ahbcfg
|= GAHBCFG_GLBL_INTR_EN
;
774 dwc2_writel(ahbcfg
, hsotg
->regs
+ GAHBCFG
);
778 * dwc2_disable_global_interrupts() - Disables the controller's Global
779 * Interrupt in the AHB Config register
781 * @hsotg: Programming view of DWC_otg controller
783 void dwc2_disable_global_interrupts(struct dwc2_hsotg
*hsotg
)
785 u32 ahbcfg
= dwc2_readl(hsotg
->regs
+ GAHBCFG
);
787 ahbcfg
&= ~GAHBCFG_GLBL_INTR_EN
;
788 dwc2_writel(ahbcfg
, hsotg
->regs
+ GAHBCFG
);
791 /* Returns the controller's GHWCFG2.OTG_MODE. */
792 unsigned int dwc2_op_mode(struct dwc2_hsotg
*hsotg
)
794 u32 ghwcfg2
= dwc2_readl(hsotg
->regs
+ GHWCFG2
);
796 return (ghwcfg2
& GHWCFG2_OP_MODE_MASK
) >>
797 GHWCFG2_OP_MODE_SHIFT
;
800 /* Returns true if the controller is capable of DRD. */
801 bool dwc2_hw_is_otg(struct dwc2_hsotg
*hsotg
)
803 unsigned int op_mode
= dwc2_op_mode(hsotg
);
805 return (op_mode
== GHWCFG2_OP_MODE_HNP_SRP_CAPABLE
) ||
806 (op_mode
== GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE
) ||
807 (op_mode
== GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE
);
810 /* Returns true if the controller is host-only. */
811 bool dwc2_hw_is_host(struct dwc2_hsotg
*hsotg
)
813 unsigned int op_mode
= dwc2_op_mode(hsotg
);
815 return (op_mode
== GHWCFG2_OP_MODE_SRP_CAPABLE_HOST
) ||
816 (op_mode
== GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST
);
819 /* Returns true if the controller is device-only. */
820 bool dwc2_hw_is_device(struct dwc2_hsotg
*hsotg
)
822 unsigned int op_mode
= dwc2_op_mode(hsotg
);
824 return (op_mode
== GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE
) ||
825 (op_mode
== GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE
);
828 MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
829 MODULE_AUTHOR("Synopsys, Inc.");
830 MODULE_LICENSE("Dual BSD/GPL");