From: Johannes Schneider <johannes.schneider@leica-geosystems.com>
To: barebox@lists.infradead.org
Cc: Johannes Schneider <johannes.schneider@leica-geosystems.com>
Subject: [PATCH] usb: dwc3: gadget: ack pending events while waiting for the halt
Date: Sat, 26 Sep 2026 14:11:37 +0000 [thread overview]
Message-ID: <20260926141137.244764-1-johannes.schneider@leica-geosystems.com> (raw)
The controller sets DSTS.DEVCTRLHLT only once every event in the event
buffer has been acknowledged. Linux relies on its interrupt handler
doing that while dwc3_gadget_run_stop() polls; barebox has no
interrupts, and gadget_unbind_driver() unregisters the UDC poller
before it disconnects. Any event left pending at that point keeps the
controller running, and the poll ends in its 2 s timeout with
-ETIMEDOUT.
On an i.MX8MP board with the gadget enumerated by a host, 1 to 15
events (GEVNTCOUNT 4 to 60) were pending on most boots, and each such
boot lost 2.0 s between the start of shutdown_barebox() and the jump
to Linux; with the events acknowledged, the controller halts within
1 ms.
The gadget is being stopped, so acknowledge the events from the poll
loop without processing them, keeping lpos in step with the hardware.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Johannes Schneider <johannes.schneider@leica-geosystems.com>
---
drivers/usb/dwc3/gadget.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 8b26a0c102..46a70231a7 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -2322,6 +2322,19 @@ static void __dwc3_gadget_set_speed(struct dwc3 *dwc)
dwc3_writel(dwc->regs, DWC3_DCFG, reg);
}
+static void dwc3_gadget_discard_events(struct dwc3 *dwc)
+{
+ struct dwc3_event_buffer *evt = dwc->ev_buf;
+ u32 count;
+
+ count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)) & DWC3_GEVNTCOUNT_MASK;
+ if (!count)
+ return;
+
+ evt->lpos = (evt->lpos + count) % evt->length;
+ dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
+}
+
static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
{
u32 reg;
@@ -2356,6 +2369,9 @@ static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
do {
udelay(1000);
+ /* the controller only halts once pending events are acked */
+ if (!is_on)
+ dwc3_gadget_discard_events(dwc);
reg = dwc3_readl(dwc->regs, DWC3_DSTS);
reg &= DWC3_DSTS_DEVCTRLHLT;
} while (--timeout && !(!is_on ^ !reg));
base-commit: 75819f9e196e2da048e96c8589898fdc38ef8e7a
--
2.53.0
reply other threads:[~2026-09-26 14:13 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260926141137.244764-1-johannes.schneider@leica-geosystems.com \
--to=johannes.schneider@leica-geosystems.com \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox