[PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off

From: Fabrice Gasnier

Date: Wed Nov 25 2026 - 11:33:13 EST


When the power domain containing the DWC2 controller is disabled during
suspend, its registers are lost. Reading registers to back them up from
dwc2_suspend() after clock gating can hang STM32MP1x in both host and
device mode. Host suspend enters clock gating directly; in device mode,
this can happen if the host suspended the USB bus before system suspend.

Instead of unconditional backup of gadget/host registers, check if these
registers have already been back'ed up (e.g. gr->valid == true). This is
the case when the controller has already been put into one of the low
power modes. This avoid unclocked access.

When entering clock gating, add backup of the gadget/host registers. This
will address the case when the bus has entered suspend state, prior the
PM suspend sequence. Discard the valid flag upon exit from clock gating.
There's no need to restore registers in clock-gating exit routines.

Protect the backup/restore of the registers against interrupt that may
arise during these phases (with a spin_lock).

Move the registers restore after the STM32 vbus/id detection glue has
been restored, as a wrong mode may be temporarily observed, triggering
ID status change IRQs.

Fixes: ba6e518d136b ("usb: dwc2: Implement recovery after PM domain off")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@xxxxxxxxxxx>
---
Changes in v6:
- Basically keep original approach instead of reworking it as discussed
with Stefan.
- Re-visit the way to restore registers: add registers backup in clock
gating entry routines. When controller is suspended (bus suspend) and
platform PM suspend is requested, the registers have already been
back'ed up so the problematic backup phase becomes redundant and can
be skipped.
- Address sashiko remarks on stale variables in subsequent separate
patches to ease review.

Changes in v5:
- Sashiko comments: stale variables in device mode, possible race caused
by early call to dwc2_enable_global_interrupts during dwc2_resume(),
drop outdated note from the commit message.

Changes in v4:
- Disconnect the gadget and clear stale endpoint and low-power state.
Sashiko comment "usb: dwc2: incomplete gadget state cleanup in
dwc2_hsotg_resume()"
- Reinitialize host state and clear stale backups after register loss.
Sashiko comment "usb: dwc2: incomplete host mode recovery in
dwc2_resume() after PM domain off"
- Revisit commit message

Changes in v3:
- Add a note in the commit message since Sashiko comment on
_dwc2_hcd_resume() check on HPRT0_CONNSTS bit.
- Add need_restore flag and pass it to dwc2_hsotg_resume() to avoid
possible interrupt race with lx_state update as raised by Sashiko,
instead of updating lx_state to DWC2_L0.

Changes in v2:
- In case registers have been lost, ensure the device mode resume takes
place by updating the lx_state to DWC2_L0, as reported by Sashiko. Note
in case of error in dwc2_core_init() choice is made to leave the low
level HW init, as it is the expected state out of the resume routine.
---
drivers/usb/dwc2/gadget.c | 13 +++++++++++++
drivers/usb/dwc2/hcd.c | 12 ++++++++++++
drivers/usb/dwc2/platform.c | 42 ++++++++++++++++++++++++++----------------
3 files changed, 51 insertions(+), 16 deletions(-)

diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index adc3df0ca7dd..02c93a41043a 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -5766,6 +5766,15 @@ void dwc2_gadget_enter_clock_gating(struct dwc2_hsotg *hsotg)

dev_dbg(hsotg->dev, "Entering device clock gating.\n");

+ /*
+ * Clock gating doesn't need a register backup. Do it here anyway, in
+ * anticipation of a platform PM suspend: once the USB bus is suspended
+ * and the clock gated, registers can't be read safely (access may hang
+ * on some platforms). dwc2_resume() uses this backup to restore the
+ * registers if the PM domain was turned off.
+ */
+ dwc2_gadget_backup_critical_registers(hsotg);
+
/* Set the Phy Clock bit as suspend is received. */
pcgctl = dwc2_readl(hsotg, PCGCTL);
pcgctl |= PCGCTL_STOPPCLK;
@@ -5792,6 +5801,7 @@ void dwc2_gadget_enter_clock_gating(struct dwc2_hsotg *hsotg)
*/
void dwc2_gadget_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
{
+ struct dwc2_gregs_backup *gr = &hsotg->gr_backup;
u32 pcgctl;
u32 dctl;

@@ -5820,4 +5830,7 @@ void dwc2_gadget_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
call_gadget(hsotg, resume);
hsotg->lx_state = DWC2_L0;
hsotg->bus_suspended = false;
+
+ /* Discard previously stored registers */
+ gr->valid = false;
}
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 40ab50538334..db6126cd72c8 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -5935,6 +5935,14 @@ void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
u32 pcgctl;

dev_dbg(hsotg->dev, "Entering host clock gating.\n");
+ /*
+ * Clock gating doesn't need a register backup. Do it here anyway, in
+ * anticipation of a platform PM suspend: once the USB bus is suspended
+ * and the clock gated, registers can't be read safely (access may hang
+ * on some platforms). dwc2_resume() uses this backup to restore the
+ * registers if the PM domain was turned off.
+ */
+ dwc2_host_backup_critical_registers(hsotg);

/* Put this port in suspend mode. */
hprt0 = dwc2_read_hprt0(hsotg);
@@ -5967,6 +5975,7 @@ void dwc2_host_enter_clock_gating(struct dwc2_hsotg *hsotg)
*/
void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
{
+ struct dwc2_gregs_backup *gr = &hsotg->gr_backup;
u32 hprt0;
u32 pcgctl;

@@ -6006,4 +6015,7 @@ void dwc2_host_exit_clock_gating(struct dwc2_hsotg *hsotg, int rem_wakeup)
mod_timer(&hsotg->wkp_timer,
jiffies + msecs_to_jiffies(71));
}
+
+ /* Discard previously stored registers */
+ gr->valid = false;
}
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index 3df9b198d1b2..7f3436e6a153 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -649,6 +649,8 @@ static int dwc2_driver_probe(struct platform_device *dev)
static int __maybe_unused dwc2_suspend(struct device *dev)
{
struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
+ struct dwc2_gregs_backup *gr;
+ unsigned long flags;
bool is_device_mode;
int ret = 0;

@@ -692,10 +694,15 @@ static int __maybe_unused dwc2_suspend(struct device *dev)
regulator_disable(dwc2->usb33d);
}

- if (is_device_mode)
- ret = dwc2_gadget_backup_critical_registers(dwc2);
- else
- ret = dwc2_host_backup_critical_registers(dwc2);
+ spin_lock_irqsave(&dwc2->lock, flags);
+ gr = &dwc2->gr_backup;
+ if (!gr->valid) {
+ if (is_device_mode)
+ ret = dwc2_gadget_backup_critical_registers(dwc2);
+ else
+ ret = dwc2_host_backup_critical_registers(dwc2);
+ }
+ spin_unlock_irqrestore(&dwc2->lock, flags);

if (ret)
return ret;
@@ -730,6 +737,7 @@ static int dwc2_restore_critical_registers(struct dwc2_hsotg *hsotg)
static int __maybe_unused dwc2_resume(struct device *dev)
{
struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
+ unsigned long flags;
int ret = 0;

if (!dwc2->ll_hw_enabled)
@@ -742,18 +750,6 @@ static int __maybe_unused dwc2_resume(struct device *dev)
}
dwc2->phy_off_for_suspend = false;

- /*
- * During suspend it's possible that the power domain for the
- * DWC2 controller is disabled and all register values get lost.
- * In case the GUSBCFG register is not initialized, it's clear the
- * registers must be restored.
- */
- if (!(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK)) {
- ret = dwc2_restore_critical_registers(dwc2);
- if (ret)
- return ret;
- }
-
if (dwc2->params.activate_stm_id_vb_detection) {
unsigned long flags;
u32 ggpio, gotgctl;
@@ -786,6 +782,20 @@ static int __maybe_unused dwc2_resume(struct device *dev)
dwc2_drd_resume(dwc2);
}

+ /*
+ * During suspend it's possible that the power domain for the
+ * DWC2 controller is disabled and all register values get lost.
+ * In case the GUSBCFG register is not initialized, it's clear the
+ * registers must be restored.
+ */
+ if (!(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK)) {
+ spin_lock_irqsave(&dwc2->lock, flags);
+ ret = dwc2_restore_critical_registers(dwc2);
+ spin_unlock_irqrestore(&dwc2->lock, flags);
+ if (ret)
+ return ret;
+ }
+
if (dwc2_is_device_mode(dwc2))
ret = dwc2_hsotg_resume(dwc2);


--
2.43.0