Skip to contentSkip to conversation

usb: dwc2: Skip HCD PHY setup when a role switch is used

PATCH In review Chen-Yu Yeh 3 participants 4 messages Nov 30 – Dec 1

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: dwc2: Skip HCD PHY setup when a role switch is used

When a USB role switch is registered, dwc2_drd_init() sets the PHY mode according to the default role. Later in probe, dwc2_hcd_init() calls usb_add_hcd(), which initializes the PHY again and unconditionally sets it to host mode. This overrides the role chosen at boot.

With role-switch-default-mode = "peripheral", the controller is forced into device mode while the PHY is left in host mode, so the gadget is never enumerated by the remote host:

  phy phy-3000000.syscon:[email protected]: set mode 6    <- dwc2_drd_init()
  phy phy-3000000.syscon:[email protected]: set mode 5    <- usb_add_hcd()
  phy phy-3000000.syscon:[email protected]: set mode 1    <- usb_add_hcd()

Set skip_phy_initialization when a role switch is present, so the DRD code is the only place that sets the PHY mode. This is safe because dwc2_lowlevel_hw_enable() already initializes and powers on the PHY earlier in probe. Without a role switch, the behaviour is unchanged.

Tested on Milk-V Duo 256M (SG2002). Besides the "set mode" messages, the PHY state was read from the USB PHY control register 0x03000048:
0x3c0 means the ID is overridden to device, 0x142 means the ID is overridden to host with VBUS enabled.

1) role-switch-default-mode = "peripheral", Duo connected to a PC

Before this patch:

     set mode 6, 5, 1
     0x03000048 = 0x142
     the PC does not see any gadget

After this patch:
set mode 6
0x03000048 = 0x3c0
the PC sees the g_ether gadget:
ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget

2) role-switch-default-mode = "host", Duo on the IO board, USB stick plugged in before boot

After this patch:
set mode 1
0x03000048 = 0x142
usb 1-1: new high-speed USB device number 2 using dwc2 hub 1-1:1.0: 4 ports detected
usb 1-1.1: new high-speed USB device number 3 using dwc2
sda: sda1 sda2 sda3 sda4
Unplugging and replugging the stick enumerates it again at high speed.

3) Role changes through sysfs, same setup as 1), three rounds

     echo host:   set mode 1, 0x03000048 = 0x142
     echo device: set mode 6, 0x03000048 = 0x3c0, UDC "configured"
     echo device while already in device: set mode 6, 0x3c0
     echo none:   0x03000048 = 0x3c0 (default role)

4) dr_mode = "host" without usb-role-switch, Duo on the IO board (regression test, role_sw is NULL so nothing is skipped)

set mode 5, 1 (still set by usb_add_hcd()) 0x03000048 = 0x142
/sys/class/usb_role/ is empty
hub and USB stick enumerated at high speed, sda1-sda4 present

---
This is the fix for the boot role problem you mentioned in the cover letter of "[PATCH 0/4] usb: dwc2: Add USB DRD support on CV1800 Series SoC" [1] and asked me to send [2]. It applies on top of that series.

It also needs the fix for the inverted is_host check in patch 2/4 ("usb: dwc2: drd: Sync the generic PHY mode with role requests"), which sashiko-bot reported and you agreed to fix in the next version [3]. In dwc2_drd_init(), v1 has:

	if (hsotg->role_sw_default_mode != USB_DR_MODE_UNKNOWN)
		is_host = hsotg->dr_mode == USB_DR_MODE_HOST;
	else
		is_host = hsotg->role_sw_default_mode == USB_DR_MODE_HOST;

All the tests above were run with the two branches swapped:

	if (hsotg->role_sw_default_mode != USB_DR_MODE_UNKNOWN)
		is_host = hsotg->role_sw_default_mode == USB_DR_MODE_HOST;
	else
		is_host = hsotg->dr_mode == USB_DR_MODE_HOST;

Until now the inverted check was hidden, because usb_add_hcd() set the PHY to host mode right after. With this patch but without that fix, role-switch-default-mode = "host" on the IO board gives:

set mode 6
0x03000048 = 0x3c0
usb 1-1: new full-speed USB device number 2 using dwc2 usb 1-1: not running at top speed; connect to a high speed hub usb 1-1.1: new full-speed USB device number 3 using dwc2 usb 1-1.1: not running at top speed; connect to a high speed hub

The controller is forced to host by dwc2_ovr_init(), but the PHY is in device mode, so devices only come up at full speed. With the fix, the same setup runs at high speed (test 2 above).

With this patch, role-switch-default-mode in the DT takes effect at boot. About the Duo 256M DTS: you offered to add it if it defaults to host [2], and I replied that I would prefer peripheral [5]. Now that both defaults work at boot, I will leave the choice between "peripheral" and "host" to you, since you know this platform better. Either way, the DTS change will go as a separate patch, not mixed with the USB patches [4]. I am happy to send it, or you can add it with your DTS patch, whichever you prefer.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/CAKspUh+LLonm90ywMGrRUcsLDE+Axw64qjs0OtDB_ySUsrPPQw@mail.gmail.com/

---
 drivers/usb/dwc2/hcd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/usb/dwc2/hcd.c +10 -0
index d5275cd658d4..4425596e6eb6 100644
@@ -5321,6 +5321,16 @@ int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
 	if (!IS_ERR_OR_NULL(hsotg->uphy))
 		otg_set_host(hsotg->uphy->otg, &hcd->self);
 
+	/*
+	 * With a role switch, the PHY mode is set by the dwc2 DRD code
+	 * according to the role. Without this flag, usb_add_hcd() would
+	 * set the PHY to host mode and override the role chosen at boot.
+	 * Skipping is safe because dwc2_lowlevel_hw_enable() already
+	 * initializes and powers on the PHY.
+	 */
+	if (hsotg->role_sw)
+		hcd->skip_phy_initialization = 1;
+
 	/*
 	 * Finish generic HCD initialization and start the HCD. This function
 	 * allocates the DMA buffer pool, registers the USB bus, requests the
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] usb: dwc2: missing Signed-off-by trailer in commit message above tearline - [High] usb: dwc2…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] usb: dwc2: missing Signed-off-by trailer in commit message above tearline
- [High] usb: dwc2: skipping HCD PHY setup in dwc2_hcd_init() without DRD phy_set_mode()
--

commit c46a379897b380450cfb9fdaa8a88e3e6943d4dc
Author: Chen-Yu Yeh <[email protected]>

Quoted 58 lines

usb: dwc2: Skip HCD PHY setup when a role switch is used

When a USB role switch is registered, dwc2_drd_init() sets the PHY mode according to the default role. Later in probe, dwc2_hcd_init() calls usb_add_hcd(), which initializes the PHY again and unconditionally sets it to host mode. This overrides the role chosen at boot.

With role-switch-default-mode = "peripheral", the controller is forced into device mode while the PHY is left in host mode, so the gadget is never enumerated by the remote host:

  phy phy-3000000.syscon:[email protected]: set mode 6    <- dwc2_drd_init()
  phy phy-3000000.syscon:[email protected]: set mode 5    <- usb_add_hcd()
  phy phy-3000000.syscon:[email protected]: set mode 1    <- usb_add_hcd()

Set skip_phy_initialization when a role switch is present, so the DRD code is the only place that sets the PHY mode. This is safe because dwc2_lowlevel_hw_enable() already initializes and powers on the PHY earlier in probe. Without a role switch, the behaviour is unchanged.

Tested on Milk-V Duo 256M (SG2002). Besides the "set mode" messages, the PHY state was read from the USB PHY control register 0x03000048:
0x3c0 means the ID is overridden to device, 0x142 means the ID is overridden to host with VBUS enabled.

1) role-switch-default-mode = "peripheral", Duo connected to a PC

Before this patch:

     set mode 6, 5, 1
     0x03000048 = 0x142
     the PC does not see any gadget

After this patch:
set mode 6
0x03000048 = 0x3c0
the PC sees the g_ether gadget:
ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget

2) role-switch-default-mode = "host", Duo on the IO board, USB stick plugged in before boot

After this patch:
set mode 1
0x03000048 = 0x142
usb 1-1: new high-speed USB device number 2 using dwc2 hub 1-1:1.0: 4 ports detected
usb 1-1.1: new high-speed USB device number 3 using dwc2
sda: sda1 sda2 sda3 sda4
Unplugging and replugging the stick enumerates it again at high speed.

3) Role changes through sysfs, same setup as 1), three rounds

     echo host:   set mode 1, 0x03000048 = 0x142
     echo device: set mode 6, 0x03000048 = 0x3c0, UDC "configured"
     echo device while already in device: set mode 6, 0x3c0
     echo none:   0x03000048 = 0x3c0 (default role)
Quoted 7 lines

4) dr_mode = "host" without usb-role-switch, Duo on the IO board (regression test, role_sw is NULL so nothing is skipped)

set mode 5, 1 (still set by usb_add_hcd()) 0x03000048 = 0x142
/sys/class/usb_role/ is empty
hub and USB stick enumerated at high speed, sda1-sda4 present

[Severity: High]
The Signed-off-by trailer was placed below the --- separator and was stripped when the patch was applied, leaving the commit message without a Signed-off-by line.

Quoted 11 lines
drivers/usb/dwc2/hcd.c +3 -0
index 40ab50538334a..98d3b110769e2 100644
@@ -5334,6 +5334,16 @@ int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
 	if (!IS_ERR_OR_NULL(hsotg->uphy))
 		otg_set_host(hsotg->uphy->otg, &hcd->self);
 
+	/*
+	 * With a role switch, the PHY mode is set by the dwc2 DRD code
+	 * according to the role. Without this flag, usb_add_hcd() would

drivers/usb/dwc2/hcd.c int dwc2_hcd_init(struct dwc2_hsotg *hsotg) · line 5349

Quoted 10 lines
drivers/usb/dwc2/hcd.c +7 -0
+	 * set the PHY to host mode and override the role chosen at boot.
+	 * Skipping is safe because dwc2_lowlevel_hw_enable() already
+	 * initializes and powers on the PHY.
+	 */
+	if (hsotg->role_sw)
+		hcd->skip_phy_initialization = 1;
+
 	/*
 	 * Finish generic HCD initialization and start the HCD. This function
 	 * allocates the DMA buffer pool, registers the USB bus, requests the

[Severity: High]
Does setting hcd->skip_phy_initialization leave hsotg->phy without any mode configured when hsotg->role_sw is used?

In this tree, the prerequisite patch adding phy_set_mode() calls to drivers/usb/dwc2/drd.c is not present: dwc2_ovr_init() and dwc2_drd_role_sw_set() only update GOTGCTL and call dwc2_force_mode(), and __dwc2_lowlevel_hw_enable() only calls phy_init() and phy_power_on(). Bypassing usb_phy_roothub_set_mode() in usb_add_hcd() causes a regression where phy_set_mode() is never called on hsotg->phy for either host or peripheral mode, leaving PHYs that rely on .set_mode unconfigured.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw
In reply to Chen-Yu Yeh
Quoted 2 lines · On Mon, Nov 30, 2026 at 02:59:03PM +0000, [email protected] wrote:

- [High] usb: dwc2: missing Signed-off-by trailer in commit message above tearline

Right. I wrote the notes for Inochi into the commit message itself, so my Signed-off-by ended up below the "---" line and is dropped by git am. I will fix this in v2.

Quoted 2 lines

- [High] usb: dwc2: skipping HCD PHY setup in dwc2_hcd_init() without DRD phy_set_mode()

This patch depends on Inochi's series [1]. Its patch 2/4 makes dwc2_drd_init() and dwc2_drd_role_sw_set() call phy_set_mode(). Without that series, skipping the HCD PHY setup leaves the PHY mode unset, so this patch must not be applied on its own. I only mentioned the dependency below the "---" line. In v2, I will state it in the commit message itself.

Greg, please do not apply this version.

Inochi, would you like to carry this patch in v2 of your series? That would keep the dependency in one place. Otherwise, I will send v2 of this patch on top of your v2 once it is out.

[1] https://lore.kernel.org/all/[email protected]/

Regards,
Chen-Yu

maintainer
#3 lore raw
Quoted 18 lines · 葉宸佑
Quoted 2 lines · On Mon, Nov 30, 2026 at 02:59:03PM +0000, [email protected] wrote:

- [High] usb: dwc2: missing Signed-off-by trailer in commit message above tearline

Right. I wrote the notes for Inochi into the commit message itself, so my Signed-off-by ended up below the "---" line and is dropped by git am. I will fix this in v2.

Quoted 2 lines

- [High] usb: dwc2: skipping HCD PHY setup in dwc2_hcd_init() without DRD phy_set_mode()

This patch depends on Inochi's series [1]. Its patch 2/4 makes dwc2_drd_init() and dwc2_drd_role_sw_set() call phy_set_mode(). Without that series, skipping the HCD PHY setup leaves the PHY mode unset, so this patch must not be applied on its own. I only mentioned the dependency below the "---" line. In v2, I will state it in the commit message itself.

Quoted 7 lines

Greg, please do not apply this version.

Inochi, would you like to carry this patch in v2 of your series? That would keep the dependency in one place. Otherwise, I will send v2 of this patch on top of your v2 once it is out.

[1] https://lore.kernel.org/all/[email protected]/

Please treat it as a separated fix. This is a dangerous thing that I have dropped in my patch. This semantic change needs some check from DWC2 expert. As you know, the DWC3 has similar change, but it is not merged.

Regards,
Inochi