* [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1
@ 2026-11-25 16:32 Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off Fabrice Gasnier
` (11 more replies)
0 siblings, 12 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
This series addresses several hang, bad_irq or re-initialisation issues
found on stm32mp1 when going to system low power (stop mode):
- First fixes can be reproduced on stm32mp13 DK board, stm32mp15 DK
and EVAL boards.
- Subsequent fixes can be reproduced on stm32mp15 EVAL board that uses
an external Vbus regulator and stm32 dedicated detection logic.
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v6:
- Instead of reworking the PM off recovery, rather add registers backup
when in clock gating mode, and rely on already bakc'ed up registers
upon recovery, as discussed with Stefan.
- Split PM off recovery fix into several patchs to ease the review.
- Address Sashiko findings seen in v5.
- Link to v5: https://patch.msgid.link/20261117-dwc2-low-power-fixes-stm32-v5-0-0f197e0e6362@foss.st.com
Changes in v5:
- Address Sashiko comments on first and last patch
- Added one separate patch as last issue raised by Sashiko appears in
current patchset but pre-exists.
- Link to v4: https://patch.msgid.link/20261113-dwc2-low-power-fixes-stm32-v4-0-08a8c962a09e@foss.st.com
Changes in v4:
- Address Sashiko feedback on register-loss recovery: add
dwc2_hcd_restore() to reinitialize the host and clear stale state;
disconnect the gadget and clear stale endpoint and low-power state.
- Fix the HCD resume race by holding hsotg->lock with interrupts disabled
across global interrupt enable and dwc2_hcd_reinit().
- Rework VBUS handling: track the regulator enable reference with
vbus_supply_enabled and a mutex, keep external VBUS enabled across
system suspend, and drop dwc2_vbus_supply_restore().
- Track STM32 ID/VBUS detection enablement with
stm32_id_vb_detection_enabled.
- Link to v3: https://patch.msgid.link/20261106-dwc2-low-power-fixes-stm32-v3-0-68d2a0e9bab6@foss.st.com
Changes in v3:
- Update since Sashiko comments
- Add a paramter (need_restore) to gadget resume function based on
canary detection to avoir possible race on lx_state as raised by
Sashiko
- Add a dwc2_vbus_supply_restore() to check regulator enable state
during hcd resume, to enforce it gets restored along with PWR bit
if it has been turned off earlier.
- Introduced a new patch for a possible race in hcd resume, with
HCD_FLAG_HW_ACCESSIBLE not being set, but host interrupts are active.
- Refactor stm32 vb/id detection, to rool back in case of regulator
disable fails.
- Link to v2: https://patch.msgid.link/20261013-dwc2-low-power-fixes-stm32-v2-0-79865a4f1b63@foss.st.com
Changes in v2:
- Several fixes or commit messages updates since Sashiko review (see
changelog in each patch).
- Add Thinh instead of Minas to the recipients
- Link to v1: https://patch.msgid.link/20261008-dwc2-low-power-fixes-stm32-v1-0-5b303df762e6@foss.st.com
---
Fabrice Gasnier (11):
usb: dwc2: fix recovery after PM domain off
usb: dwc2: keep TOUTCAL canary valid after PHY init
usb: dwc2: drop stale state after register restore on resume
usb: dwc2: clean up old role on mode change during suspend
usb: dwc2: fix a possible interrupt storm in hcd resume
usb: dwc2: fix a possible interrupt storm on stm32mp1
usb: dwc2: hcd: fix possible successive vbus supply enables
usb: dwc2: hcd: don't disable external vbus in system suspend
usb: dwc2: restore peripheral mode on low-level enable
usb: dwc2: platform: fix stm32mp15 detection logic power management
usb: dwc2: drop redundant ll_hw_enabled checks in suspend/resume
drivers/usb/dwc2/core.c | 17 +++
drivers/usb/dwc2/core.h | 25 +++-
drivers/usb/dwc2/core_intr.c | 9 +-
drivers/usb/dwc2/gadget.c | 61 ++++++++--
drivers/usb/dwc2/hcd.c | 150 +++++++++++++++++++-----
drivers/usb/dwc2/platform.c | 269 +++++++++++++++++++++++++++++--------------
6 files changed, 405 insertions(+), 126 deletions(-)
---
base-commit: b0baaf6cbb16bc773d0c76916aac17c44a6cd557
change-id: 20261007-dwc2-low-power-fixes-stm32-0c1e5e68b252
Best regards,
--
Fabrice Gasnier <fabrice.gasnier@foss.st.com>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:58 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 02/11] usb: dwc2: keep TOUTCAL canary valid after PHY init Fabrice Gasnier
` (10 subsequent siblings)
11 siblings, 1 reply; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
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@vger.kernel.org
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
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
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 02/11] usb: dwc2: keep TOUTCAL canary valid after PHY init
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume Fabrice Gasnier
` (9 subsequent siblings)
11 siblings, 0 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
dwc2_resume() uses GUSBCFG.TOUTCAL as a canary for register loss during
system resume. PHY initialization can reset the core and clear TOUTCAL.
In dual-role device mode without a gadget driver, TOUTCAL can also remain
at its power-on value of zero. Either case can make resume falsely detect
register loss.
Run PHY initialization before setting TOUTCAL in
dwc2_hsotg_core_init_disconnected(), and initialize TOUTCAL in
dwc2_gusbcfg_init() after PHY setup. This keeps the canary nonzero after
PHY initialization and in driverless dual-role device mode.
Fixes: ba6e518d136b ("usb: dwc2: Implement recovery after PM domain off")
Cc: stable@vger.kernel.org
Closes: https://lore.kernel.org/linux-usb/sashiko-outbox-219256@kernel.org/
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v6:
- New patch to address 1st finding of the Sashiko review of "usb: dwc2:
fix recovery after PM domain off".
---
drivers/usb/dwc2/gadget.c | 4 ++--
drivers/usb/dwc2/hcd.c | 7 +++++++
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 02c93a41043a..5e75d6975baa 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -3461,6 +3461,8 @@ void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
}
}
+ dwc2_phy_init(hsotg, true);
+
/*
* we must now enable ep0 ready for host detection and then
* set configuration.
@@ -3484,8 +3486,6 @@ void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
usbcfg |= GUSBCFG_SRPCAP;
dwc2_writel(hsotg, usbcfg, GUSBCFG);
- dwc2_phy_init(hsotg, true);
-
if (!is_usb_reset) {
dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
if (hsotg->params.eusb2_disc)
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index db6126cd72c8..0d20b9de857a 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -128,6 +128,13 @@ static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
break;
}
+ /*
+ * dwc2_resume() uses TOUTCAL to detect register loss. Set it here
+ * for dual-role device mode before a gadget driver binds.
+ */
+ usbcfg &= ~GUSBCFG_TOUTCAL_MASK;
+ usbcfg |= GUSBCFG_TOUTCAL(7);
+
dwc2_writel(hsotg, usbcfg, GUSBCFG);
}
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 02/11] usb: dwc2: keep TOUTCAL canary valid after PHY init Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 17:09 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend Fabrice Gasnier
` (8 subsequent siblings)
11 siblings, 1 reply; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
After a power domain loss, dwc2_resume() restores the registers that were
backed up when entering a low power mode (clock gating, partial power
down), or in dwc2_suspend() when no backup exists yet. The restore only
brings the hardware back to the state it had when the backup was taken.
The software state is still the one of the suspended controller, and events
that occurred while the power domain was off have been missed:
- lx_state is DWC2_L2 and bus_suspended is set, so dwc2_hsotg_resume()
returns early and _dwc2_hcd_resume() tries to resume the port, even
though the controller has been fully restored.
- in_ppd, hibernated and the backup valid flags may be left over.
- In device mode, the gadget speed and the enabled endpoints are stale,
although the host most likely saw a disconnect while the platform was
unpowered.
- In host mode, a connect or disconnect that happened during suspend is not
reported, as GINTSTS is cleared by the restore.
Add dwc2_clear_lost_state() to reset the common software state, and use it
after a register loss is detected (GUSBCFG.TOUTCAL cleared):
- device mode: dwc2_hsotg_resume() gets a "lost" parameter. When set, it
disconnects the gadget, disables the non-EP0 endpoints, sets the speed to
unknown and clears the stale state, then reinitializes the core and
reconnects as for a regular resume.
- host mode: dwc2_hcd_lost_state() clears the stale state and flags a
connect and suspend status change, so the hub re-evaluates the port.
Global interrupts are disabled by the restore under the lock, and only
re-enabled once this cleanup is done, so an interrupt cannot run on stale
state in between.
Fixes: ba6e518d136b ("usb: dwc2: Implement recovery after PM domain off")
Cc: stable@vger.kernel.org
Closes: https://lore.kernel.org/all/sashiko-outbox-202003@kernel.org/
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v6:
- patch split from "usb: dwc2: fix recovery after PM domain off" to ease
the review
---
drivers/usb/dwc2/core.c | 17 +++++++++++++++++
drivers/usb/dwc2/core.h | 8 ++++++--
drivers/usb/dwc2/gadget.c | 24 +++++++++++++++++++-----
drivers/usb/dwc2/hcd.c | 20 ++++++++++++++++++++
drivers/usb/dwc2/platform.c | 18 ++++++++++++++++--
5 files changed, 78 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/dwc2/core.c b/drivers/usb/dwc2/core.c
index 6a0135e06b00..5da76f0fbcf6 100644
--- a/drivers/usb/dwc2/core.c
+++ b/drivers/usb/dwc2/core.c
@@ -600,6 +600,23 @@ void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
}
}
+/**
+ * dwc2_clear_lost_state() - Clear software state made stale by register loss
+ * @hsotg: Programming view of DWC_otg controller
+ *
+ * Must be called with the spinlock held.
+ */
+void dwc2_clear_lost_state(struct dwc2_hsotg *hsotg)
+{
+ hsotg->hibernated = 0;
+ hsotg->in_ppd = 0;
+ hsotg->bus_suspended = false;
+ hsotg->gr_backup.valid = false;
+ hsotg->dr_backup.valid = false;
+ hsotg->hr_backup.valid = false;
+ hsotg->lx_state = DWC2_L0;
+}
+
/*
* dwc2_enable_acg - enable active clock gating feature
*/
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index 54c040da0cae..9873ae7b52c2 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -1411,12 +1411,14 @@ void dwc2_dump_dev_registers(struct dwc2_hsotg *hsotg);
void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg);
void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg);
+void dwc2_clear_lost_state(struct dwc2_hsotg *hsotg);
+
/* Gadget defines */
#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg);
int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2);
-int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2);
+int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost);
int dwc2_gadget_init(struct dwc2_hsotg *hsotg);
void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
bool reset);
@@ -1452,7 +1454,7 @@ static inline int dwc2_hsotg_remove(struct dwc2_hsotg *dwc2)
{ return 0; }
static inline int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2)
{ return 0; }
-static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2)
+static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost)
{ return 0; }
static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
{ return 0; }
@@ -1503,6 +1505,7 @@ static inline void dwc2_clear_fifo_map(struct dwc2_hsotg *hsotg) {}
#if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg);
int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us);
+void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg);
void dwc2_hcd_connect(struct dwc2_hsotg *hsotg);
void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force);
void dwc2_hcd_start(struct dwc2_hsotg *hsotg);
@@ -1530,6 +1533,7 @@ static inline int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
static inline int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg,
int us)
{ return 0; }
+static inline void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg) {}
static inline void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) {}
static inline void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) {}
static inline void dwc2_hcd_start(struct dwc2_hsotg *hsotg) {}
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 5e75d6975baa..295091a87b12 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -5213,26 +5213,40 @@ int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
return 0;
}
-int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
+int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg, bool lost)
{
unsigned long flags;
+ int ep;
- if (hsotg->lx_state == DWC2_L2)
+ if (!lost && hsotg->lx_state == DWC2_L2)
return 0;
- if (hsotg->driver) {
+ if (hsotg->driver)
dev_info(hsotg->dev, "resuming usb gadget %s\n",
hsotg->driver->driver.name);
- spin_lock_irqsave(&hsotg->lock, flags);
+ spin_lock_irqsave(&hsotg->lock, flags);
+ if (lost) {
+ /* Host likely saw a disconnect: drop stale gadget state */
+ dwc2_hsotg_disconnect(hsotg);
+ for (ep = 1; ep < hsotg->num_of_eps; ep++) {
+ if (hsotg->eps_in[ep])
+ dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
+ if (hsotg->eps_out[ep])
+ dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
+ }
+ hsotg->gadget.speed = USB_SPEED_UNKNOWN;
+ dwc2_clear_lost_state(hsotg);
+ }
+ if (hsotg->driver) {
dwc2_hsotg_core_init_disconnected(hsotg, false);
if (hsotg->enabled) {
/* Enable ACG feature in device mode,if supported */
dwc2_enable_acg(hsotg);
dwc2_hsotg_core_connect(hsotg);
}
- spin_unlock_irqrestore(&hsotg->lock, flags);
}
+ spin_unlock_irqrestore(&hsotg->lock, flags);
return 0;
}
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 0d20b9de857a..6f2b192f68f8 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -2324,6 +2324,26 @@ static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
dwc2_core_host_init(hsotg);
}
+/**
+ * dwc2_hcd_lost_state - drop stale host state after registers were restored
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * Make the hub re-evaluate the port, as a connect change may have been lost
+ * while the controller was powered off.
+ */
+void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg)
+{
+ struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
+ unsigned long flags;
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_clear_lost_state(hsotg);
+ hsotg->flags.b.port_connect_status_change = 1;
+ hsotg->flags.b.port_suspend_change = 1;
+ set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+}
+
static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
struct dwc2_host_chan *chan,
struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index 7f3436e6a153..53c34a42f760 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -738,6 +738,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)
{
struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
unsigned long flags;
+ bool lost;
int ret = 0;
if (!dwc2->ll_hw_enabled)
@@ -788,16 +789,29 @@ static int __maybe_unused dwc2_resume(struct device *dev)
* In case the GUSBCFG register is not initialized, it's clear the
* registers must be restored.
*/
- if (!(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK)) {
+ lost = !(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK);
+ if (lost) {
spin_lock_irqsave(&dwc2->lock, flags);
ret = dwc2_restore_critical_registers(dwc2);
+ /*
+ * The restore re-enables global interrupts: keep them off
+ * until the stale state is cleaned up below.
+ */
+ if (!ret)
+ dwc2_disable_global_interrupts(dwc2);
spin_unlock_irqrestore(&dwc2->lock, flags);
if (ret)
return ret;
}
if (dwc2_is_device_mode(dwc2))
- ret = dwc2_hsotg_resume(dwc2);
+ ret = dwc2_hsotg_resume(dwc2, lost);
+ else if (lost)
+ dwc2_hcd_lost_state(dwc2);
+
+ /* Global interrupts are off after a restore, enable them back */
+ if (lost)
+ dwc2_enable_global_interrupts(dwc2);
return ret;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (2 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:57 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 05/11] usb: dwc2: fix a possible interrupt storm in hcd resume Fabrice Gasnier
` (7 subsequent siblings)
11 siblings, 1 reply; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
If the power domain is off during system suspend and the OTG ID pin
changes meanwhile, dwc2_resume() sees a register loss but the role that
was active at suspend is never cleaned up. No connector ID interrupt
reports the change, as GINTSTS is cleared by the register restore, and
the register backup belongs to the old role: host registers could be
restored in device mode, or the other way around.
When the core was in device mode at suspend and resumes in host mode, the
gadget driver is never told about the disconnect and the gadget state
stays stale. When it was in host mode and resumes in device mode, URBs and
host flags stay as they were and the external VBUS supply is left enabled.
Record the mode in dwc2_suspend(). On register loss with dr_mode otg,
compare it with the mode after resume. If it differs, skip the register
restore and clean up the old role before initializing the new one:
- device to host: disconnect the gadget, clear stale state, the FIFO map
and set the speed to unknown, without accessing endpoint registers that
are unusable in host mode. Then reinitialize the host with the new
dwc2_hcd_restore(). If it fails, the HCD was not reinitialized, so it is
left inaccessible.
- host to device: fail pending URBs, clear stale state, make the HCD
accessible and release VBUS, then reinitialize the core and resume the
gadget.
This addresses the second finding of the Sashiko review of "usb: dwc2:
fix recovery after PM domain off".
Fixes: ba6e518d136b ("usb: dwc2: Implement recovery after PM domain off")
Cc: stable@vger.kernel.org
Closes: https://lore.kernel.org/linux-usb/sashiko-outbox-219256@kernel.org/
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v6:
- New patch added since Sashiko finding
---
drivers/usb/dwc2/core.h | 9 +++++++
drivers/usb/dwc2/gadget.c | 20 ++++++++++++++++
drivers/usb/dwc2/hcd.c | 58 +++++++++++++++++++++++++++++++++++++++++++++
drivers/usb/dwc2/platform.c | 25 ++++++++++++++++---
4 files changed, 109 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index 9873ae7b52c2..89a34956bf22 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -876,6 +876,7 @@ struct dwc2_hregs_backup {
* @reset_phy_on_wake: Quirk saying that we should assert PHY reset on a
* remote wakeup.
* @phy_off_for_suspend: Status of whether we turned the PHY off at suspend.
+ * @suspended_in_device_mode: True if the core was in device mode at suspend.
* @need_phy_for_wake: Quirk saying that we should keep the PHY on at
* suspend if we need USB to wake us up.
* @frame_number: Frame number read from the core. For both device
@@ -1074,6 +1075,7 @@ struct dwc2_hsotg {
unsigned int reset_phy_on_wake:1;
unsigned int need_phy_for_wake:1;
unsigned int phy_off_for_suspend:1;
+ unsigned int suspended_in_device_mode:1;
u16 frame_number;
struct phy *phy;
@@ -1419,6 +1421,7 @@ void dwc2_clear_lost_state(struct dwc2_hsotg *hsotg);
int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg);
int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2);
int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost);
+void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg);
int dwc2_gadget_init(struct dwc2_hsotg *hsotg);
void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
bool reset);
@@ -1456,6 +1459,7 @@ static inline int dwc2_hsotg_suspend(struct dwc2_hsotg *dwc2)
{ return 0; }
static inline int dwc2_hsotg_resume(struct dwc2_hsotg *dwc2, bool lost)
{ return 0; }
+static inline void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg) {}
static inline int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
{ return 0; }
static inline void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *dwc2,
@@ -1505,7 +1509,9 @@ static inline void dwc2_clear_fifo_map(struct dwc2_hsotg *hsotg) {}
#if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg);
int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us);
+int dwc2_hcd_restore(struct dwc2_hsotg *hsotg);
void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg);
+void dwc2_hcd_role_lost(struct dwc2_hsotg *hsotg);
void dwc2_hcd_connect(struct dwc2_hsotg *hsotg);
void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force);
void dwc2_hcd_start(struct dwc2_hsotg *hsotg);
@@ -1533,7 +1539,10 @@ static inline int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
static inline int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg,
int us)
{ return 0; }
+static inline int dwc2_hcd_restore(struct dwc2_hsotg *hsotg)
+{ return 0; }
static inline void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg) {}
+static inline void dwc2_hcd_role_lost(struct dwc2_hsotg *hsotg) {}
static inline void dwc2_hcd_connect(struct dwc2_hsotg *hsotg) {}
static inline void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force) {}
static inline void dwc2_hcd_start(struct dwc2_hsotg *hsotg) {}
diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 295091a87b12..9b12c336e4aa 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -5251,6 +5251,26 @@ int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg, bool lost)
return 0;
}
+/**
+ * dwc2_hsotg_role_lost - drop gadget state after a role change in suspend
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * The core came back from register loss in host mode while the gadget was
+ * active at suspend. Disconnect the gadget and clear stale software state
+ * without touching the endpoint registers, which are not usable in host mode.
+ */
+void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_hsotg_disconnect(hsotg);
+ dwc2_clear_fifo_map(hsotg);
+ hsotg->gadget.speed = USB_SPEED_UNKNOWN;
+ dwc2_clear_lost_state(hsotg);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+}
+
/**
* dwc2_backup_device_registers() - Backup controller device registers.
* When suspending usb bus, registers needs to be backuped
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 6f2b192f68f8..60b39ccd0cad 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -2324,6 +2324,42 @@ static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
dwc2_core_host_init(hsotg);
}
+/**
+ * dwc2_hcd_restore - reinitialize host state after a role change in suspend
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * Return: 0 on success, or a negative error code on failure.
+ */
+int dwc2_hcd_restore(struct dwc2_hsotg *hsotg)
+{
+ struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
+ unsigned long flags;
+ int ret;
+
+ clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ dwc2_disable_global_interrupts(hsotg);
+
+ ret = dwc2_vbus_supply_init(hsotg);
+ if (ret)
+ return ret;
+
+ ret = dwc2_core_init(hsotg, true);
+ if (ret) {
+ dwc2_vbus_supply_exit(hsotg);
+ return ret;
+ }
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_hcd_reinit(hsotg);
+ dwc2_clear_lost_state(hsotg);
+ hsotg->flags.b.port_connect_status_change = 1;
+ hsotg->flags.b.port_suspend_change = 1;
+ set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+
+ return 0;
+}
+
/**
* dwc2_hcd_lost_state - drop stale host state after registers were restored
* @hsotg: Programming view of the DWC_otg controller
@@ -2344,6 +2380,28 @@ void dwc2_hcd_lost_state(struct dwc2_hsotg *hsotg)
spin_unlock_irqrestore(&hsotg->lock, flags);
}
+/**
+ * dwc2_hcd_role_lost - drop host state after a role change in suspend
+ * @hsotg: Programming view of the DWC_otg controller
+ *
+ * The core came back from register loss in device mode while the host was
+ * active at suspend. Fail pending URBs, release VBUS and clear stale software
+ * state so the host can be started again on the next role switch.
+ */
+void dwc2_hcd_role_lost(struct dwc2_hsotg *hsotg)
+{
+ struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
+ unsigned long flags;
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ dwc2_hcd_disconnect(hsotg, true);
+ dwc2_clear_lost_state(hsotg);
+ set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+
+ dwc2_vbus_supply_exit(hsotg);
+}
+
static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
struct dwc2_host_chan *chan,
struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index 53c34a42f760..d89a30308f86 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -658,6 +658,7 @@ static int __maybe_unused dwc2_suspend(struct device *dev)
return 0;
is_device_mode = dwc2_is_device_mode(dwc2);
+ dwc2->suspended_in_device_mode = is_device_mode;
if (is_device_mode)
dwc2_hsotg_suspend(dwc2);
@@ -738,7 +739,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)
{
struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
unsigned long flags;
- bool lost;
+ bool lost, role_changed;
int ret = 0;
if (!dwc2->ll_hw_enabled)
@@ -790,7 +791,25 @@ static int __maybe_unused dwc2_resume(struct device *dev)
* registers must be restored.
*/
lost = !(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK);
- if (lost) {
+ role_changed = lost && dwc2->dr_mode == USB_DR_MODE_OTG &&
+ dwc2->suspended_in_device_mode != dwc2_is_device_mode(dwc2);
+ if (role_changed) {
+ /*
+ * The ID pin changed while the registers were lost: no
+ * connector ID interrupt reports it and the register backup
+ * belongs to the old role. Clean up the old role and
+ * initialize the new one instead of restoring.
+ */
+ if (dwc2->suspended_in_device_mode) {
+ dwc2_hsotg_role_lost(dwc2);
+ ret = dwc2_hcd_restore(dwc2);
+ } else {
+ dwc2_hcd_role_lost(dwc2);
+ ret = dwc2_core_init(dwc2, true);
+ }
+ if (ret)
+ return ret;
+ } else if (lost) {
spin_lock_irqsave(&dwc2->lock, flags);
ret = dwc2_restore_critical_registers(dwc2);
/*
@@ -806,7 +825,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)
if (dwc2_is_device_mode(dwc2))
ret = dwc2_hsotg_resume(dwc2, lost);
- else if (lost)
+ else if (lost && !role_changed)
dwc2_hcd_lost_state(dwc2);
/* Global interrupts are off after a restore, enable them back */
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 05/11] usb: dwc2: fix a possible interrupt storm in hcd resume
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (3 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 06/11] usb: dwc2: fix a possible interrupt storm on stm32mp1 Fabrice Gasnier
` (6 subsequent siblings)
11 siblings, 0 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
The host interrupts are re-enabled in dwc2_hcd_reinit(). So there is a
race during resume between a possible hcd IRQ, and the HW accessible bit
to be set. This can result in a bad_irq to be reported (nobody cared).
Note dwc2_hcd_reinit() "Must be called with interrupt disabled and
spinlock held." So, move dwc2_hcd_reinit() and the glocal irq enabling
inside the critical section, after HCD_FLAG_HW_ACCESSIBLE has been set.
Fixes: ef5e0eec4764 ("usb: dwc2: Add clock gating exiting flow by system resume")
Cc: stable@vger.kernel.org
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Note it's been found while debugging with additional mdelay after
dwc2_hcd_reinit() call.
---
Changes in v4:
- Address Sashiko comment "usb: dwc2: unlocked dwc2_hcd_reinit() call in
_dwc2_hcd_resume()": rather move the dwc2_hcd_reinit() and irq enabling
under the critical section, after HCD_FLAG_HW_ACCESSIBLE has been set.
- Update commit msg.
Changes in v3:
- Introduce this patch since debug session (since sashiko comment on
patch 1)
---
drivers/usb/dwc2/hcd.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 60b39ccd0cad..7729581d1102 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -4568,8 +4568,6 @@ static int _dwc2_hcd_resume(struct usb_hcd *hcd)
* the global interrupts are disabled.
*/
dwc2_core_init(hsotg, false);
- dwc2_enable_global_interrupts(hsotg);
- dwc2_hcd_reinit(hsotg);
spin_lock_irqsave(&hsotg->lock, flags);
/*
@@ -4577,6 +4575,8 @@ static int _dwc2_hcd_resume(struct usb_hcd *hcd)
* since an interrupt may rise.
*/
set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ dwc2_enable_global_interrupts(hsotg);
+ dwc2_hcd_reinit(hsotg);
break;
default:
hsotg->lx_state = DWC2_L0;
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 06/11] usb: dwc2: fix a possible interrupt storm on stm32mp1
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (4 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 05/11] usb: dwc2: fix a possible interrupt storm in hcd resume Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 07/11] usb: dwc2: hcd: fix possible successive vbus supply enables Fabrice Gasnier
` (5 subsequent siblings)
11 siblings, 0 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
On stm32mp1, upon resume from low power (stop) mode, in host mode, a
port interrupt storm may be seen before the hardware is accessible.
The core is able to generate interrupts during system resume in
platform.c when calling dwc2_resume()->__dwc2_lowlevel_hw_enable(). As
the PHY and clocks become available, a resume IRQ can be seen with a
Host Port Interrupt (PrtInt).
The wakeup interrupt handler make the controller to exit power down mode,
but the HCD handler returns IRQ_NONE, due to the HW accessible flag has
been cleared in _dwc2_hcd_suspend() earlier. It ends-up with the bad_irq
handler.
So set the HCD_FLAG_HW_ACCESSIBLE flag when the resume IRQ is handled to
exit from power down mode.
Fixes: ef5e0eec4764 ("usb: dwc2: Add clock gating exiting flow by system resume")
Cc: stable@vger.kernel.org
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v3:
- Set HCD_FLAG_HW_ACCESSIBLE only when dwc2_exit_partial_power_down()
succeeds.
Changes in v2:
- Add check on hcd before setting HCD_FLAG_HW_ACCESSIBLE flag, to avoid a
NULL pointer dereference on hcd in peripheral-only mode as raised by
Sashiko.
- Add same handling in PPD mode as raised by Sashiko. Only handle clock
gating and partial power down mode (PPD) as pre-existing wakeup handler
manages these two modes. I don't have Hibernation capable hardware to
cross-check this mode.
---
drivers/usb/dwc2/core_intr.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/dwc2/core_intr.c b/drivers/usb/dwc2/core_intr.c
index 9565e6a52725..df9f6b963fb8 100644
--- a/drivers/usb/dwc2/core_intr.c
+++ b/drivers/usb/dwc2/core_intr.c
@@ -453,18 +453,25 @@ static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg *hsotg)
}
} else {
if (hsotg->lx_state == DWC2_L2) {
+ struct usb_hcd *hcd = hsotg->priv;
+
if (hsotg->in_ppd) {
ret = dwc2_exit_partial_power_down(hsotg, 1,
true);
if (ret)
dev_err(hsotg->dev,
"exit partial_power_down failed\n");
+ else if (hcd)
+ set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
}
if (hsotg->params.power_down ==
DWC2_POWER_DOWN_PARAM_NONE && hsotg->bus_suspended &&
- !hsotg->params.no_clock_gating)
+ !hsotg->params.no_clock_gating) {
dwc2_host_exit_clock_gating(hsotg, 1);
+ if (hcd)
+ set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
+ }
/*
* If we've got this quirk then the PHY is stuck upon
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 07/11] usb: dwc2: hcd: fix possible successive vbus supply enables
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (5 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 06/11] usb: dwc2: fix a possible interrupt storm on stm32mp1 Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 08/11] usb: dwc2: hcd: don't disable external vbus in system suspend Fabrice Gasnier
` (4 subsequent siblings)
11 siblings, 0 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
Relying on HPRT0_PWR value to call dwc2_vbus_supply_init() shows some
corner case resulting in vbus enable count to increase.
As consequence, dwc2_vbus_supply_exit call done when clearing
PWR bit won't disable vbus (for example upon suspend request).
Address two case that has been seen:
- In _dwc2_hcd_start(), external VBUS supply may have already been enabled,
before entering dwc2_hcd_reinit (e.g. the controller is in host mode,
without any device plugged).
- Upon resume from low power in case registers have been lost. In clock
gating mode PM suspend flow doesn't disable vbus while in host mode:
- dwc2_port_suspend() enters clock gating (it sets lx_state to DWC2_L2)
- _dwc2_hcd_suspend() nothing more to do as already in L2.
- _dwc2_hcd_resume() - checks HPRT0_CONNSTS. If it's not set (register
loss) it sets HPRT0_PWR and call dwc2_vbus_supply_init().
Fix this by adding a boolean flag to balance vbus enable/disable calls
instead of relying on HPRT0_PWR value. Clean the existing checks on pwr
bit. A mutex is added as dwc2_vbus_supply_init/_exit calls can run
concurrently from hub_control and dwc2_conn_id_status_change.
Fixes: cd7cd0e6cedf ("usb: dwc2: fix unbalanced use of external vbus-supply")
Cc: stable@vger.kernel.org
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v4:
- drop previous vbus_was_off boolean. Introduce vbus_supply_enabled flag
instead and vbus_supply_lock.
- remove checks on pwr when possible as now replaced by this flag to keep
balanced calls.
Changes in v2:
- Add a vbus_was_off boolean to check explicitly an off-on transition
on port power, to enable vbus supply, instead of relying an XOR check
that would be symmetric, e.g. wrongly enable it in case it gets switched
off (1 to 0) as reported by Sashiko.
---
drivers/usb/dwc2/core.h | 6 ++++++
drivers/usb/dwc2/hcd.c | 45 ++++++++++++++++++++++++---------------------
drivers/usb/dwc2/platform.c | 1 +
3 files changed, 31 insertions(+), 21 deletions(-)
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index 89a34956bf22..1bd652c1bb42 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -10,6 +10,7 @@
#include <linux/acpi.h>
#include <linux/device-id/pci.h>
+#include <linux/mutex.h>
#include <linux/phy/phy.h>
#include <linux/regulator/consumer.h>
#include <linux/usb/gadget.h>
@@ -889,6 +890,9 @@ struct dwc2_hregs_backup {
* removed once all SoCs support usb transceiver.
* @supplies: Definition of USB power supplies
* @vbus_supply: Regulator supplying vbus.
+ * @vbus_supply_lock: Protects vbus_supply state and regulator operations.
+ * @vbus_supply_enabled: True if this driver holds an enable reference to
+ * vbus_supply.
* @usb33d: Optional 3.3v regulator used on some stm32 devices to
* supply ID and VBUS detection hardware.
* @lock: Spinlock that protects all the driver data structures
@@ -1083,6 +1087,8 @@ struct dwc2_hsotg {
struct dwc2_hsotg_plat *plat;
struct regulator_bulk_data supplies[DWC2_NUM_SUPPLIES];
struct regulator *vbus_supply;
+ struct mutex vbus_supply_lock;
+ bool vbus_supply_enabled;
struct regulator *usb33d;
spinlock_t lock;
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index 7729581d1102..cfa5d2af60d1 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -140,18 +140,32 @@ static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
static int dwc2_vbus_supply_init(struct dwc2_hsotg *hsotg)
{
- if (hsotg->vbus_supply)
- return regulator_enable(hsotg->vbus_supply);
+ int ret = 0;
- return 0;
+ scoped_guard(mutex, &hsotg->vbus_supply_lock) {
+ if (hsotg->vbus_supply && !hsotg->vbus_supply_enabled) {
+ ret = regulator_enable(hsotg->vbus_supply);
+ if (!ret)
+ hsotg->vbus_supply_enabled = true;
+ }
+ }
+
+ return ret;
}
static int dwc2_vbus_supply_exit(struct dwc2_hsotg *hsotg)
{
- if (hsotg->vbus_supply)
- return regulator_disable(hsotg->vbus_supply);
+ int ret = 0;
- return 0;
+ scoped_guard(mutex, &hsotg->vbus_supply_lock) {
+ if (hsotg->vbus_supply && hsotg->vbus_supply_enabled) {
+ ret = regulator_disable(hsotg->vbus_supply);
+ if (!ret)
+ hsotg->vbus_supply_enabled = false;
+ }
+ }
+
+ return ret;
}
/**
@@ -3488,7 +3502,6 @@ static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
u32 port_status;
u32 speed;
u32 pcgctl;
- u32 pwr;
switch (typereq) {
case ClearHubFeature:
@@ -3533,11 +3546,9 @@ static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
dev_dbg(hsotg->dev,
"ClearPortFeature USB_PORT_FEAT_POWER\n");
hprt0 = dwc2_read_hprt0(hsotg);
- pwr = hprt0 & HPRT0_PWR;
hprt0 &= ~HPRT0_PWR;
dwc2_writel(hsotg, hprt0, HPRT0);
- if (pwr)
- dwc2_vbus_supply_exit(hsotg);
+ dwc2_vbus_supply_exit(hsotg);
break;
case USB_PORT_FEAT_INDICATOR:
@@ -3741,11 +3752,9 @@ static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
dev_dbg(hsotg->dev,
"SetPortFeature - USB_PORT_FEAT_POWER\n");
hprt0 = dwc2_read_hprt0(hsotg);
- pwr = hprt0 & HPRT0_PWR;
hprt0 |= HPRT0_PWR;
dwc2_writel(hsotg, hprt0, HPRT0);
- if (!pwr)
- dwc2_vbus_supply_init(hsotg);
+ dwc2_vbus_supply_init(hsotg);
break;
case USB_PORT_FEAT_RESET:
@@ -3780,7 +3789,6 @@ static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
dwc2_writel(hsotg, 0, PCGCTL);
hprt0 = dwc2_read_hprt0(hsotg);
- pwr = hprt0 & HPRT0_PWR;
/* Clear suspend bit if resetting from suspend state */
hprt0 &= ~HPRT0_SUSP;
@@ -3794,8 +3802,7 @@ static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
dev_dbg(hsotg->dev,
"In host mode, hprt0=%08x\n", hprt0);
dwc2_writel(hsotg, hprt0, HPRT0);
- if (!pwr)
- dwc2_vbus_supply_init(hsotg);
+ dwc2_vbus_supply_init(hsotg);
}
/* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
@@ -4397,7 +4404,6 @@ static void _dwc2_hcd_stop(struct usb_hcd *hcd)
{
struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
unsigned long flags;
- u32 hprt0;
/* Turn off all host-specific interrupts */
dwc2_disable_host_interrupts(hsotg);
@@ -4406,7 +4412,6 @@ static void _dwc2_hcd_stop(struct usb_hcd *hcd)
synchronize_irq(hcd->irq);
spin_lock_irqsave(&hsotg->lock, flags);
- hprt0 = dwc2_read_hprt0(hsotg);
/* Ensure hcd is disconnected */
dwc2_hcd_disconnect(hsotg, true);
dwc2_hcd_stop(hsotg);
@@ -4415,9 +4420,7 @@ static void _dwc2_hcd_stop(struct usb_hcd *hcd)
clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
spin_unlock_irqrestore(&hsotg->lock, flags);
- /* keep balanced supply init/exit by checking HPRT0_PWR */
- if (hprt0 & HPRT0_PWR)
- dwc2_vbus_supply_exit(hsotg);
+ dwc2_vbus_supply_exit(hsotg);
usleep_range(1000, 3000);
}
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index d89a30308f86..24ede0b6ab81 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -471,6 +471,7 @@ static int dwc2_driver_probe(struct platform_device *dev)
return retval;
spin_lock_init(&hsotg->lock);
+ mutex_init(&hsotg->vbus_supply_lock);
hsotg->vbus_supply = devm_regulator_get_optional(hsotg->dev, "vbus");
if (IS_ERR(hsotg->vbus_supply)) {
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 08/11] usb: dwc2: hcd: don't disable external vbus in system suspend
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (6 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 07/11] usb: dwc2: hcd: fix possible successive vbus supply enables Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable Fabrice Gasnier
` (3 subsequent siblings)
11 siblings, 0 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
It's not required to remove port power upon system suspend, as explained
in commit 113f86d0c302 ("usb: dwc2: Update partial power down entering by
system suspend"). It can be addressed with hub ctrl (USB_PORT_FEAT_POWER).
When PM suspend flow disables Vbus while in host mode by
calling dwc2_vbus_supply_exit()) in _dwc2_hcd_suspend() and the core has
been put into clock gating mode, it triggers a disconnect and a resume
IRQ (during suspend):
++Disconnect Detected Interrupt++ (Host) a_host
++Resume or Remote Wakeup Detected Interrupt++
That starts the wkp_timer in dwc2_host_exit_clock_gating() that will
finally resume the port, e.g. call dwc2_wakeup_detected() and set
lx_state = DWC2_L0 after ~70ms.
It races with PM suspend/resume flow. During _dwc2_hcd_resume() the port
may already be in DWC2_L0 state skipping the dwc2_vbus_supply_init(),
leaving the Vbus disabled.
So remove the disable of external Vbus in bus suspend to avoid this.
Fixes: 113f86d0c302 ("usb: dwc2: Update partial power down entering by system suspend")
Cc: stable@vger.kernel.org
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v4:
- Rename the commit title and re-visit the commit message.
- Drop dwc2_vbus_supply_restore() but keep dwc2_vbus_supply_init() to
enforce vbus is present when setting PWR bit.
Changes in v3:
- Sashiko comment (state desync leaves VBUS regulator permanently off)
Add a dwc2_vbus_supply_restore() to check regulator enable state
during hcd resume, if it has been turned off earlier, enable it,
so it is kept sync'ed with PWR bit that gets set.
Changes in v2:
- rephrase commit message since Sashiko review. Hopefully it better
explains the issue, and why HPRT0_PWR is kept synchronized with
external vbus supply.
---
drivers/usb/dwc2/hcd.c | 4 ----
1 file changed, 4 deletions(-)
diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c
index cfa5d2af60d1..a39dcbddac84 100644
--- a/drivers/usb/dwc2/hcd.c
+++ b/drivers/usb/dwc2/hcd.c
@@ -4488,10 +4488,6 @@ static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
goto skip_power_saving;
}
- spin_unlock_irqrestore(&hsotg->lock, flags);
- dwc2_vbus_supply_exit(hsotg);
- spin_lock_irqsave(&hsotg->lock, flags);
-
/* Ask phy to be suspended */
if (!IS_ERR_OR_NULL(hsotg->uphy)) {
spin_unlock_irqrestore(&hsotg->lock, flags);
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (7 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 08/11] usb: dwc2: hcd: don't disable external vbus in system suspend Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 17:08 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 10/11] usb: dwc2: platform: fix stm32mp15 detection logic power management Fabrice Gasnier
` (2 subsequent siblings)
11 siblings, 1 reply; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
In fixed peripheral mode, the gadget code manages the low-level hardware
on its own: it is disabled after probe, and enabled again when a gadget
driver binds. While it is disabled, dwc2_suspend() and dwc2_resume() return
early since ll_hw_enabled is false. If the DWC2 registers are lost during
system suspend, nothing restores the mode selection when the gadget driver
binds later:
- FORCEDEVMODE is not set, so the core may not come up in device mode.
- With a usb-role-switch, the session overrides set by dwc2_drd_resume()
are not restored either.
Restore them at the end of __dwc2_lowlevel_hw_enable() when dr_mode is
peripheral: call dwc2_drd_resume() if a role switch is registered, or
dwc2_force_dr_mode() otherwise. The dr_mode check leaves the probe-time
enable unchanged, as dr_mode is parsed later and the role switch isn't
registered yet.
As this is now done on low-level enable, skip the peripheral case in
dwc2_resume(). Other modes still restore the forced mode or the role switch
session there.
Fixes: 2b94b054ac49 ("usb: dwc2: fix hang during suspend if set as peripheral")
Cc: stable@vger.kernel.org
Closes: https://lore.kernel.org/linux-usb/sashiko-outbox-219235@kernel.org/
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v6:
- Also restore the role switch session overrides with dwc2_drd_resume()
when a usb-role-switch is registered in peripheral mode. Issue raised
by Sashiko.
Changes in v5:
- New separate patch added since issue raised by Sashiko on next patch
pre-exists, e.g. missing forced mode restore in peripheral-only mode.
---
drivers/usb/dwc2/platform.c | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index 24ede0b6ab81..38954dee6ad0 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -129,6 +129,18 @@ static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
if (ret)
goto err_dis_clk;
+ if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
+ /*
+ * Gadget code manages low-level hardware on its own. Restore
+ * the mode and role switch session after register loss while
+ * it was disabled.
+ */
+ if (hsotg->role_sw)
+ dwc2_drd_resume(hsotg);
+ else
+ dwc2_force_dr_mode(hsotg);
+ }
+
return 0;
err_dis_clk:
@@ -778,11 +790,12 @@ static int __maybe_unused dwc2_resume(struct device *dev)
spin_unlock_irqrestore(&dwc2->lock, flags);
}
- if (!dwc2->role_sw) {
- /* Need to restore FORCEDEVMODE/FORCEHOSTMODE */
- dwc2_force_dr_mode(dwc2);
- } else {
- dwc2_drd_resume(dwc2);
+ if (dwc2->dr_mode != USB_DR_MODE_PERIPHERAL) {
+ if (!dwc2->role_sw)
+ /* Need to restore FORCEDEVMODE/FORCEHOSTMODE */
+ dwc2_force_dr_mode(dwc2);
+ else
+ dwc2_drd_resume(dwc2);
}
/*
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 10/11] usb: dwc2: platform: fix stm32mp15 detection logic power management
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (8 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 11/11] usb: dwc2: drop redundant ll_hw_enabled checks in suspend/resume Fabrice Gasnier
2026-11-25 17:09 ` [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 sashiko-bot
11 siblings, 0 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32, stable
On stm32mp15, the STM32 detection logic can remain enabled even though
the other hardware resources are disabled during probe in peripheral
mode:
/* Gadget code manages lowlevel hw on its own */
if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
dwc2_lowlevel_hw_disable(hsotg);
When entering system low power, the detection logic resources would then
remain enabled because dwc2_suspend() checks:
if (!dwc2->ll_hw_enabled)
return 0;
If the stm32mp15 enters standby, the register contents can be lost and
not restored, leaving dwc2 unusable. This can happen when no gadget
driver has been bound.
Move the STM32 detection logic enable/disable procedures into dedicated
dwc2_stm32_hw_[enable/disable]() routines and call them from the
__dwc2_lowlevel_hw_[enable/disable]() routines.
Factorize the probe code by replacing regulator_enable() and GGPIO
register access with a call to dwc2_stm32_hw_enable(). The earlier
dwc2_lowlevel_hw_enable() call during probe cannot do this because
activate_stm_id_vb_detection is only known after dwc2_init_params() runs
and the usb33d supply is acquired.
Track VBUS/ID detection state in stm32_id_vb_detection_enabled so it
is disabled only after successful enablement.
Fixes: 2b94b054ac49 ("usb: dwc2: fix hang during suspend if set as peripheral")
Fixes: a415083a11cc ("usb: dwc2: add support for STM32MP15 SoCs USB OTG HS and FS")
Cc: stable@vger.kernel.org
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
Changes in v5:
- rebased on ("usb: dwc2: restore peripheral mode on low-level enable")
intended to address sashiko comment "usb: dwc2: missing forced mode
restore in dwc2_stm32_hw_enable()"
Changes in v4:
- Track successful STM32 ID/VBUS detection enablement with
stm32_id_vb_detection_enabled, avoiding an invalid regulator disable
during probe error cleanup, as reported by Sashiko.
Changes in v3:
- Roll back if regulator_disable() fails in dwc2_stm32_hw_disable(), as
reported by Sashiko.
- Add a note in response to Sashiko's v2 review.
Changes in v2:
- Fix the probe error path if devm_regulator_get() or
dwc2_stm32_hw_enable() fails, as reported by Sashiko.
- Describe the hardware-disable error handling in the commit message.
Notes:
- If dwc2_stm32_hw_disable() fails in __dwc2_lowlevel_hw_disable(),
there is nothing to roll back, so return immediately. Alternatively,
the error could be ignored to continue disabling other resources.
Similar behavior already exists for phy shutdown/exit/power_off errors;
this could be addressed separately.
- In host mode with remote wakeup enabled and a descendant enabled to
wake the system, phy_off_for_suspend is false. The STM32 power domain
is not expected to turn off, so registers should not be lost.
---
drivers/usb/dwc2/core.h | 2 +
drivers/usb/dwc2/platform.c | 185 ++++++++++++++++++++++++++------------------
2 files changed, 112 insertions(+), 75 deletions(-)
diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h
index 1bd652c1bb42..9a3bf6a5047c 100644
--- a/drivers/usb/dwc2/core.h
+++ b/drivers/usb/dwc2/core.h
@@ -878,6 +878,7 @@ struct dwc2_hregs_backup {
* remote wakeup.
* @phy_off_for_suspend: Status of whether we turned the PHY off at suspend.
* @suspended_in_device_mode: True if the core was in device mode at suspend.
+ * @stm32_id_vb_detection_enabled: True if STM32 ID/VBUS detection is enabled.
* @need_phy_for_wake: Quirk saying that we should keep the PHY on at
* suspend if we need USB to wake us up.
* @frame_number: Frame number read from the core. For both device
@@ -1080,6 +1081,7 @@ struct dwc2_hsotg {
unsigned int need_phy_for_wake:1;
unsigned int phy_off_for_suspend:1;
unsigned int suspended_in_device_mode:1;
+ bool stm32_id_vb_detection_enabled;
u16 frame_number;
struct phy *phy;
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index 38954dee6ad0..c1f5e03c0d03 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -91,6 +91,95 @@ static int dwc2_get_dr_mode(struct dwc2_hsotg *hsotg)
return 0;
}
+static void dwc2_stm32_hw_activate_id_vb_detection(struct dwc2_hsotg *hsotg)
+{
+ unsigned long flags;
+ u32 ggpio, gotgctl;
+
+ ggpio = dwc2_readl(hsotg, GGPIO);
+ ggpio |= GGPIO_STM32_OTG_GCCFG_IDEN;
+ ggpio |= GGPIO_STM32_OTG_GCCFG_VBDEN;
+ dwc2_writel(hsotg, ggpio, GGPIO);
+
+ /* ID/VBUS detection startup time */
+ usleep_range(5000, 7000);
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ /* Remove overrides when resuming from the disable */
+ gotgctl = dwc2_readl(hsotg, GOTGCTL);
+ gotgctl &= ~GOTGCTL_DBNCE_FLTR_BYPASS;
+ gotgctl &= ~(GOTGCTL_BVALOEN | GOTGCTL_AVALOEN |
+ GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL);
+ dwc2_writel(hsotg, gotgctl, GOTGCTL);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+}
+
+static int dwc2_stm32_hw_enable(struct dwc2_hsotg *hsotg)
+{
+ int ret;
+
+ /* params are only known, once dwc2_init_params() has been probed */
+ if (!hsotg->params.activate_stm_id_vb_detection)
+ return 0;
+
+ if (hsotg->stm32_id_vb_detection_enabled)
+ return 0;
+
+ ret = regulator_enable(hsotg->usb33d);
+ if (ret)
+ return ret;
+
+ dwc2_stm32_hw_activate_id_vb_detection(hsotg);
+ hsotg->stm32_id_vb_detection_enabled = true;
+
+ return 0;
+}
+
+static int dwc2_stm32_hw_disable(struct dwc2_hsotg *hsotg)
+{
+ unsigned long flags;
+ u32 ggpio, gotgctl;
+ int ret;
+
+ if (!hsotg->stm32_id_vb_detection_enabled)
+ return 0;
+
+ /*
+ * Need to force the mode to the current mode to avoid Mode
+ * Mismatch Interrupt when ID detection will be disabled.
+ */
+ dwc2_force_mode(hsotg, !dwc2_is_device_mode(hsotg));
+
+ spin_lock_irqsave(&hsotg->lock, flags);
+ gotgctl = dwc2_readl(hsotg, GOTGCTL);
+ /* bypass debounce filter, enable overrides */
+ gotgctl |= GOTGCTL_DBNCE_FLTR_BYPASS;
+ gotgctl |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN;
+ /* Force A / B session if needed */
+ if (gotgctl & GOTGCTL_ASESVLD)
+ gotgctl |= GOTGCTL_AVALOVAL;
+ if (gotgctl & GOTGCTL_BSESVLD)
+ gotgctl |= GOTGCTL_BVALOVAL;
+ dwc2_writel(hsotg, gotgctl, GOTGCTL);
+ spin_unlock_irqrestore(&hsotg->lock, flags);
+
+ ggpio = dwc2_readl(hsotg, GGPIO);
+ ggpio &= ~GGPIO_STM32_OTG_GCCFG_IDEN;
+ ggpio &= ~GGPIO_STM32_OTG_GCCFG_VBDEN;
+ dwc2_writel(hsotg, ggpio, GGPIO);
+
+ ret = regulator_disable(hsotg->usb33d);
+ if (ret) {
+ dwc2_stm32_hw_activate_id_vb_detection(hsotg);
+ dwc2_force_dr_mode(hsotg);
+ return ret;
+ }
+
+ hsotg->stm32_id_vb_detection_enabled = false;
+
+ return 0;
+}
+
static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
{
struct platform_device *pdev = to_platform_device(hsotg->dev);
@@ -141,8 +230,22 @@ static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
dwc2_force_dr_mode(hsotg);
}
+ ret = dwc2_stm32_hw_enable(hsotg);
+ if (ret)
+ goto err_dis_phy;
+
return 0;
+err_dis_phy:
+ if (hsotg->uphy) {
+ usb_phy_shutdown(hsotg->uphy);
+ } else if (hsotg->plat && hsotg->plat->phy_exit) {
+ hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
+ } else {
+ phy_power_off(hsotg->phy);
+ phy_exit(hsotg->phy);
+ }
+
err_dis_clk:
if (hsotg->clk)
clk_disable_unprepare(hsotg->clk);
@@ -178,6 +281,10 @@ static int __dwc2_lowlevel_hw_disable(struct dwc2_hsotg *hsotg)
struct platform_device *pdev = to_platform_device(hsotg->dev);
int ret = 0;
+ ret = dwc2_stm32_hw_disable(hsotg);
+ if (ret)
+ return ret;
+
if (hsotg->uphy) {
usb_phy_shutdown(hsotg->uphy);
} else if (hsotg->plat && hsotg->plat->phy_exit) {
@@ -358,9 +465,6 @@ static void dwc2_driver_remove(struct platform_device *dev)
dwc2_drd_exit(hsotg);
- if (hsotg->params.activate_stm_id_vb_detection)
- regulator_disable(hsotg->usb33d);
-
if (hsotg->ll_hw_enabled)
dwc2_lowlevel_hw_disable(hsotg);
}
@@ -554,33 +658,23 @@ static int dwc2_driver_probe(struct platform_device *dev)
goto error;
if (hsotg->params.activate_stm_id_vb_detection) {
- u32 ggpio;
-
hsotg->usb33d = devm_regulator_get(hsotg->dev, "usb33d");
if (IS_ERR(hsotg->usb33d)) {
retval = PTR_ERR(hsotg->usb33d);
dev_err_probe(hsotg->dev, retval, "failed to request usb33d supply\n");
goto error;
}
- retval = regulator_enable(hsotg->usb33d);
+ retval = dwc2_stm32_hw_enable(hsotg);
if (retval) {
- dev_err_probe(hsotg->dev, retval, "failed to enable usb33d supply\n");
+ dev_err_probe(hsotg->dev, retval, "dwc2_stm32_hw_enable failed\n");
goto error;
}
-
- ggpio = dwc2_readl(hsotg, GGPIO);
- ggpio |= GGPIO_STM32_OTG_GCCFG_IDEN;
- ggpio |= GGPIO_STM32_OTG_GCCFG_VBDEN;
- dwc2_writel(hsotg, ggpio, GGPIO);
-
- /* ID/VBUS detection startup time */
- usleep_range(5000, 7000);
}
retval = dwc2_drd_init(hsotg);
if (retval) {
dev_err_probe(hsotg->dev, retval, "failed to initialize dual-role\n");
- goto error_init;
+ goto error;
}
if (hsotg->dr_mode != USB_DR_MODE_HOST) {
@@ -650,9 +744,6 @@ static int dwc2_driver_probe(struct platform_device *dev)
error_drd:
dwc2_drd_exit(hsotg);
-error_init:
- if (hsotg->params.activate_stm_id_vb_detection)
- regulator_disable(hsotg->usb33d);
error:
if (hsotg->ll_hw_enabled)
dwc2_lowlevel_hw_disable(hsotg);
@@ -677,37 +768,6 @@ static int __maybe_unused dwc2_suspend(struct device *dev)
dwc2_drd_suspend(dwc2);
- if (dwc2->params.activate_stm_id_vb_detection) {
- unsigned long flags;
- u32 ggpio, gotgctl;
-
- /*
- * Need to force the mode to the current mode to avoid Mode
- * Mismatch Interrupt when ID detection will be disabled.
- */
- dwc2_force_mode(dwc2, !is_device_mode);
-
- spin_lock_irqsave(&dwc2->lock, flags);
- gotgctl = dwc2_readl(dwc2, GOTGCTL);
- /* bypass debounce filter, enable overrides */
- gotgctl |= GOTGCTL_DBNCE_FLTR_BYPASS;
- gotgctl |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN;
- /* Force A / B session if needed */
- if (gotgctl & GOTGCTL_ASESVLD)
- gotgctl |= GOTGCTL_AVALOVAL;
- if (gotgctl & GOTGCTL_BSESVLD)
- gotgctl |= GOTGCTL_BVALOVAL;
- dwc2_writel(dwc2, gotgctl, GOTGCTL);
- spin_unlock_irqrestore(&dwc2->lock, flags);
-
- ggpio = dwc2_readl(dwc2, GGPIO);
- ggpio &= ~GGPIO_STM32_OTG_GCCFG_IDEN;
- ggpio &= ~GGPIO_STM32_OTG_GCCFG_VBDEN;
- dwc2_writel(dwc2, ggpio, GGPIO);
-
- regulator_disable(dwc2->usb33d);
- }
-
spin_lock_irqsave(&dwc2->lock, flags);
gr = &dwc2->gr_backup;
if (!gr->valid) {
@@ -765,31 +825,6 @@ static int __maybe_unused dwc2_resume(struct device *dev)
}
dwc2->phy_off_for_suspend = false;
- if (dwc2->params.activate_stm_id_vb_detection) {
- unsigned long flags;
- u32 ggpio, gotgctl;
-
- ret = regulator_enable(dwc2->usb33d);
- if (ret)
- return ret;
-
- ggpio = dwc2_readl(dwc2, GGPIO);
- ggpio |= GGPIO_STM32_OTG_GCCFG_IDEN;
- ggpio |= GGPIO_STM32_OTG_GCCFG_VBDEN;
- dwc2_writel(dwc2, ggpio, GGPIO);
-
- /* ID/VBUS detection startup time */
- usleep_range(5000, 7000);
-
- spin_lock_irqsave(&dwc2->lock, flags);
- gotgctl = dwc2_readl(dwc2, GOTGCTL);
- gotgctl &= ~GOTGCTL_DBNCE_FLTR_BYPASS;
- gotgctl &= ~(GOTGCTL_BVALOEN | GOTGCTL_AVALOEN |
- GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL);
- dwc2_writel(dwc2, gotgctl, GOTGCTL);
- spin_unlock_irqrestore(&dwc2->lock, flags);
- }
-
if (dwc2->dr_mode != USB_DR_MODE_PERIPHERAL) {
if (!dwc2->role_sw)
/* Need to restore FORCEDEVMODE/FORCEHOSTMODE */
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH v6 11/11] usb: dwc2: drop redundant ll_hw_enabled checks in suspend/resume
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (9 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 10/11] usb: dwc2: platform: fix stm32mp15 detection logic power management Fabrice Gasnier
@ 2026-11-25 16:32 ` Fabrice Gasnier
2026-11-25 17:09 ` [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 sashiko-bot
11 siblings, 0 replies; 17+ messages in thread
From: Fabrice Gasnier @ 2026-11-25 16:32 UTC (permalink / raw)
To: Thinh Nguyen, Greg Kroah-Hartman, Douglas Anderson, Stefan Wahren,
Artur Petrosyan, Amelie Delaunay, Fabrice Gasnier, Jisheng Zhang,
Felipe Balbi
Cc: linux-usb, linux-kernel, linux-stm32
Since commit 2b94b054ac49 ("usb: dwc2: fix hang during suspend if set as
peripheral"), dwc2_suspend() and dwc2_resume() return early when
ll_hw_enabled is false. The later ll_hw_enabled checks in both functions
are therefore always true. Remove them.
No functional change.
Signed-off-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
---
drivers/usb/dwc2/platform.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
index c1f5e03c0d03..733b05191dd7 100644
--- a/drivers/usb/dwc2/platform.c
+++ b/drivers/usb/dwc2/platform.c
@@ -781,8 +781,7 @@ static int __maybe_unused dwc2_suspend(struct device *dev)
if (ret)
return ret;
- if (dwc2->ll_hw_enabled &&
- (is_device_mode || dwc2_host_can_poweroff_phy(dwc2))) {
+ if (is_device_mode || dwc2_host_can_poweroff_phy(dwc2)) {
ret = __dwc2_lowlevel_hw_disable(dwc2);
dwc2->phy_off_for_suspend = true;
}
@@ -818,7 +817,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)
if (!dwc2->ll_hw_enabled)
return 0;
- if (dwc2->phy_off_for_suspend && dwc2->ll_hw_enabled) {
+ if (dwc2->phy_off_for_suspend) {
ret = __dwc2_lowlevel_hw_enable(dwc2);
if (ret)
return ret;
--
2.43.0
^ permalink raw reply related [flat|nested] 17+ messages in thread
* Re: [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend
2026-11-25 16:32 ` [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend Fabrice Gasnier
@ 2026-11-25 16:57 ` sashiko-bot
0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-25 16:57 UTC (permalink / raw)
To: Fabrice Gasnier; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: dwc2: device endpoint register access in host mode in
dwc2_hsotg_role_lost()
--
commit 9eca46957e8d956eb9059255d4d71f159a5ec1a4
Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
usb: dwc2: clean up old role on mode change during suspend
This commit records the active role at suspend and, if register state is
lost during suspend while the OTG ID pin changes, cleans up the old role
and initializes the new role on resume instead of restoring the previous
role's register backup.
> diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
> index 295091a87b129..9b12c336e4aa9 100644
> --- a/drivers/usb/dwc2/gadget.c
> +++ b/drivers/usb/dwc2/gadget.c
> @@ -5251,6 +5251,26 @@ int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg, bool lost)
> return 0;
> }
>
> +/**
> + * dwc2_hsotg_role_lost - drop gadget state after a role change in suspend
> + * @hsotg: Programming view of the DWC_otg controller
> + *
> + * The core came back from register loss in host mode while the gadget was
> + * active at suspend. Disconnect the gadget and clear stale software state
> + * without touching the endpoint registers, which are not usable in host mode.
> + */
> +void dwc2_hsotg_role_lost(struct dwc2_hsotg *hsotg)
> +{
> + unsigned long flags;
> +
> + spin_lock_irqsave(&hsotg->lock, flags);
> + dwc2_hsotg_disconnect(hsotg);
> + dwc2_clear_fifo_map(hsotg);
> + hsotg->gadget.speed = USB_SPEED_UNKNOWN;
> + dwc2_clear_lost_state(hsotg);
> + spin_unlock_irqrestore(&hsotg->lock, flags);
> +}
[Severity: Medium]
Can dwc2_hsotg_role_lost() still access device endpoint registers while
the controller is in host mode, and leave endpoint state stale when
usb_ep_disable() is deferred?
When the USB bus is already suspended (hsotg->lx_state is DWC2_L2 or
DWC2_L1) at system suspend, dwc2_hsotg_suspend() returns early, leaving
hsotg->connected set, endpoints enabled with non-zero fifo_index and
fifo_size, and hsotg->op_state at OTG_STATE_B_PERIPHERAL. On resume into
host mode, dwc2_hsotg_role_lost() runs before dwc2_hcd_restore() updates
hsotg->op_state to OTG_STATE_A_HOST, and calls dwc2_hsotg_disconnect():
1. dwc2_hsotg_disconnect() calls kill_all_requests() on every endpoint,
which reads DTXFSTS(ep->fifo_index) and flushes the TX FIFO when
hsotg->dedicated_fifos is set:
drivers/usb/dwc2/gadget.c:kill_all_requests() {
...
if (!hsotg->dedicated_fifos)
return;
size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
if (size < ep->fifo_size)
dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
}
2. dwc2_hsotg_disconnect() then invokes call_gadget(hsotg, disconnect).
Gadget drivers that synchronously call usb_ep_disable() during
disconnect enter dwc2_hsotg_ep_disable(), where the host-mode guard
(hsotg->op_state != OTG_STATE_B_PERIPHERAL) does not trigger because
op_state has not been updated yet, so it reads and writes DIEPCTL,
DOEPCTL, DAINTMSK and DTXFSTS while the hardware is in host mode.
3. Conversely, if a gadget function driver defers usb_ep_disable() to a
workqueue or thread that runs after dwc2_hcd_restore() sets op_state to
OTG_STATE_A_HOST, dwc2_hsotg_ep_disable() returns -EINVAL without
clearing hs_ep->fifo_index or hs_ep->fifo_size, and usb_ep_disable()
leaves ep->enabled set so the endpoint cannot be re-enabled on the next
switch back to device mode.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-dwc2-low-power-fixes-stm32-v6-0-d5df33b93eaf@foss.st.com?part=4
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off
2026-11-25 16:32 ` [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off Fabrice Gasnier
@ 2026-11-25 16:58 ` sashiko-bot
0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-25 16:58 UTC (permalink / raw)
To: Fabrice Gasnier; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usb: dwc2: stale gr_backup.valid after resume without PM domain
power loss
- [High] usb: dwc2: dwc2_restore_critical_registers() overwrites resume
config
--
commit c2d1c8f60a77ed180c814b0a57e8097e8be203f6
Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
usb: dwc2: fix recovery after PM domain off
This commit backs up critical registers when entering clock gating, skips the
backup in dwc2_suspend() if a valid backup already exists, and moves register
restoration in dwc2_resume() after STM32 ID/VBUS detection and dual-role
restoration.
> diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
> index 3df9b198d1b22..7f3436e6a1538 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);
[Severity: High]
Can gr->valid remain true across normal operation after a suspend/resume
cycle where the power domain is not turned off?
When dwc2_suspend() runs while the controller is not in clock gating (for
example, in device mode in DWC2_L0, in host mode with no device connected on
the root port, or when params.no_clock_gating is set), the backup here sets
gr->valid = true. If the power domain stays powered during that suspend,
dwc2_resume() sees GUSBCFG_TOUTCAL_MASK still set and skips
dwc2_restore_critical_registers(), while neither
dwc2_gadget_exit_clock_gating() nor dwc2_host_exit_clock_gating() runs to
clear gr->valid.
Because gr->valid stays true in DWC2_L0, subsequent dwc2_suspend() calls skip
backing up current registers. If a later suspend powers off the PM domain,
dwc2_resume() restores stale register values from the earlier suspend, or
restores the wrong mode's registers if an OTG role switch occurred in between.
[ ... ]
> @@ -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;
> + }
[Severity: High]
Does calling dwc2_restore_critical_registers() after the STM32 ID/VBUS
detection block and dwc2_force_dr_mode() / dwc2_drd_resume() overwrite the
GOTGCTL, GINTMSK, and GUSBCFG values just configured for resume?
drivers/usb/dwc2/core.c:dwc2_restore_global_registers() {
...
dwc2_writel(hsotg, gr->gotgctl, GOTGCTL);
dwc2_writel(hsotg, gr->gintmsk, GINTMSK);
dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
...
}
In dwc2_suspend(), dwc2_drd_suspend() masks GINTSTS_CONIDSTSCHNG in GINTMSK,
and the activate_stm_id_vb_detection path forces the mode in GUSBCFG and sets
session and debounce overrides in GOTGCTL before the !gr->valid backup saves
those registers into gr_backup. When the power domain is lost, running
dwc2_restore_critical_registers() after the resume steps clobbers the restored
GOTGCTL, GINTMSK, and GUSBCFG settings with the pre-suspend or suspend-time
snapshot, breaking OTG and dual-role detection after resume.
Even with commit "usb: dwc2: platform: fix stm32mp15 detection logic power
management" and commit "usb: dwc2: restore peripheral mode on low-level
enable" later in the series, dwc2_drd_suspend() still masks
GINTSTS_CONIDSTSCHNG before the backup in dwc2_suspend(), and
dwc2_force_dr_mode() and dwc2_drd_resume() still have their GUSBCFG, GOTGCTL,
and GINTMSK updates overwritten by dwc2_restore_global_registers().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-dwc2-low-power-fixes-stm32-v6-0-d5df33b93eaf@foss.st.com?part=1
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable
2026-11-25 16:32 ` [PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable Fabrice Gasnier
@ 2026-11-25 17:08 ` sashiko-bot
0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-25 17:08 UTC (permalink / raw)
To: Fabrice Gasnier; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: dwc2: missing GOTGCTL override restore in
dwc2_drd_resume() for ROLE_NONE
--
commit d994a6181c5bc6aa556fe623825b039f6d0ac91e
Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
usb: dwc2: restore peripheral mode on low-level enable
This commit restores the forced mode or role switch session overrides in
__dwc2_lowlevel_hw_enable() when dr_mode is USB_DR_MODE_PERIPHERAL, and
skips doing so in dwc2_resume() for peripheral mode.
> diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
> index 24ede0b6ab815..38954dee6ad0d 100644
> --- a/drivers/usb/dwc2/platform.c
> +++ b/drivers/usb/dwc2/platform.c
> @@ -129,6 +129,18 @@ static int __dwc2_lowlevel_hw_enable(struct dwc2_hsotg *hsotg)
> if (ret)
> goto err_dis_clk;
>
> + if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
> + /*
> + * Gadget code manages low-level hardware on its own. Restore
> + * the mode and role switch session after register loss while
> + * it was disabled.
> + */
> + if (hsotg->role_sw)
> + dwc2_drd_resume(hsotg);
> + else
> + dwc2_force_dr_mode(hsotg);
> + }
> +
> return 0;
>
> err_dis_clk:
[Severity: Medium]
Does dwc2_drd_resume() leave the GOTGCTL override enable bits cleared when
the role is USB_ROLE_NONE?
At probe, dwc2_ovr_init() enables GOTGCTL_BVALOEN, GOTGCTL_AVALOEN,
GOTGCTL_VBVALOEN and GOTGCTL_DBNCE_FLTR_BYPASS with the session valid
values cleared even when there is no default role. If registers are lost
during suspend while ll_hw_enabled is false (so gr_backup is neither saved
nor restored) and a gadget driver binds while the role switch state is
USB_ROLE_NONE and role_sw_default_mode is USB_DR_MODE_UNKNOWN,
dwc2_drd_resume() skips writing GOTGCTL:
drivers/usb/dwc2/drd.c:dwc2_drd_resume() {
...
/* restore last role that may have been lost */
if (role == USB_ROLE_HOST)
dwc2_ovr_avalid(hsotg, true);
else if (role == USB_ROLE_DEVICE)
dwc2_ovr_bvalid(hsotg, true);
dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
...
}
Without those override enable bits, dwc2_force_mode() runs without
GOTGCTL_DBNCE_FLTR_BYPASS and dwc2_hsotg_core_connect() reads the raw PHY
GOTGCTL_BSESVLD signal instead of the role switch override.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-dwc2-low-power-fixes-stm32-v6-0-d5df33b93eaf@foss.st.com?part=9
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume
2026-11-25 16:32 ` [PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume Fabrice Gasnier
@ 2026-11-25 17:09 ` sashiko-bot
0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-25 17:09 UTC (permalink / raw)
To: Fabrice Gasnier; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: dwc2: stale backup valid flags left set in dwc2_resume()
when not lost
--
commit 4c1dd5f40e80c2ddc45f465c1df3b944bef53d8d
Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
usb: dwc2: drop stale state after register restore on resume
This commit adds dwc2_clear_lost_state() and uses it in device and host
mode resume paths to clear stale software state when register loss is
detected after a power domain off.
> diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c
> index 7f3436e6a1538..53c34a42f7600 100644
> --- a/drivers/usb/dwc2/platform.c
> +++ b/drivers/usb/dwc2/platform.c
> @@ -738,6 +738,7 @@ static int __maybe_unused dwc2_resume(struct device *dev)
> {
> struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
> unsigned long flags;
> + bool lost;
> int ret = 0;
>
> if (!dwc2->ll_hw_enabled)
> @@ -788,16 +789,29 @@ static int __maybe_unused dwc2_resume(struct device *dev)
> * In case the GUSBCFG register is not initialized, it's clear the
> * registers must be restored.
> */
> - if (!(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK)) {
> + lost = !(dwc2_readl(dwc2, GUSBCFG) & GUSBCFG_TOUTCAL_MASK);
> + if (lost) {
> spin_lock_irqsave(&dwc2->lock, flags);
> ret = dwc2_restore_critical_registers(dwc2);
> + /*
> + * The restore re-enables global interrupts: keep them off
> + * until the stale state is cleaned up below.
> + */
> + if (!ret)
> + dwc2_disable_global_interrupts(dwc2);
> spin_unlock_irqrestore(&dwc2->lock, flags);
> if (ret)
> return ret;
> }
>
> if (dwc2_is_device_mode(dwc2))
> - ret = dwc2_hsotg_resume(dwc2);
> + ret = dwc2_hsotg_resume(dwc2, lost);
> + else if (lost)
> + dwc2_hcd_lost_state(dwc2);
[Severity: Medium]
Does skipping dwc2_clear_lost_state() when lost is false leave
gr_backup.valid and dr_backup.valid (or hr_backup.valid) set after a
suspend/resume cycle where the power domain stayed on?
When the controller is not already in a low-power state at system suspend
(for instance in device mode at DWC2_L0, where dwc2_hsotg_suspend() puts
it into DWC2_L3), dwc2_suspend() backs up the registers and sets the
backup valid flags:
drivers/usb/dwc2/platform.c:dwc2_suspend() {
...
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);
}
...
}
If the power domain is not powered off during that suspend (such as s2idle
or standby), lost is false on resume. In device mode,
dwc2_hsotg_resume(dwc2, false) reinitializes the core to DWC2_L0 without
calling dwc2_restore_critical_registers() or dwc2_clear_lost_state(),
leaving gr_backup.valid and dr_backup.valid true. Any subsequent
dwc2_suspend() will then see gr->valid == true and skip backing up the
current registers, so a later suspend that does lose power will restore
stale state from the earlier cycle (or fail to restore if the OTG role
changed in between).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-dwc2-low-power-fixes-stm32-v6-0-d5df33b93eaf@foss.st.com?part=3
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
` (10 preceding siblings ...)
2026-11-25 16:32 ` [PATCH v6 11/11] usb: dwc2: drop redundant ll_hw_enabled checks in suspend/resume Fabrice Gasnier
@ 2026-11-25 17:09 ` sashiko-bot
11 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-25 17:09 UTC (permalink / raw)
To: Fabrice Gasnier; +Cc: linux-usb
Sashiko has finished reviewing this 11-patch series:
- Patches 2, 5-8, 10, and 11: no issues found.
- Patch 1 ("usb: dwc2: fix recovery after PM domain off"): 2 potential
issues (2 High)
- Patch 3 ("usb: dwc2: drop stale state after register restore o..."): 1
potential issue (1 Medium)
- Patch 4 ("usb: dwc2: clean up old role on mode change during s..."): 1
potential issue (1 Medium)
- Patch 9 ("usb: dwc2: restore peripheral mode on low-level enable"): 1
potential issue (1 Medium)
Detailed reviews have been sent as separate replies to patches 1, 3, 4,
and 9.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-dwc2-low-power-fixes-stm32-v6-0-d5df33b93eaf@foss.st.com
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2026-11-25 17:09 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 16:32 [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 01/11] usb: dwc2: fix recovery after PM domain off Fabrice Gasnier
2026-11-25 16:58 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 02/11] usb: dwc2: keep TOUTCAL canary valid after PHY init Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 03/11] usb: dwc2: drop stale state after register restore on resume Fabrice Gasnier
2026-11-25 17:09 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 04/11] usb: dwc2: clean up old role on mode change during suspend Fabrice Gasnier
2026-11-25 16:57 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 05/11] usb: dwc2: fix a possible interrupt storm in hcd resume Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 06/11] usb: dwc2: fix a possible interrupt storm on stm32mp1 Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 07/11] usb: dwc2: hcd: fix possible successive vbus supply enables Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 08/11] usb: dwc2: hcd: don't disable external vbus in system suspend Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 09/11] usb: dwc2: restore peripheral mode on low-level enable Fabrice Gasnier
2026-11-25 17:08 ` sashiko-bot
2026-11-25 16:32 ` [PATCH v6 10/11] usb: dwc2: platform: fix stm32mp15 detection logic power management Fabrice Gasnier
2026-11-25 16:32 ` [PATCH v6 11/11] usb: dwc2: drop redundant ll_hw_enabled checks in suspend/resume Fabrice Gasnier
2026-11-25 17:09 ` [PATCH v6 00/11] usb: dwc2: fix low power issues found on stm32mp1 sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox