Skip to contentSkip to conversation

Milk-V Duo 256M: USB peripheral mode works on mainline, how should dr_mode be handled?

DISCUSSION Chen-Yu Yeh 2 participants 5 messages Oct 24 – Oct 27 Sophgo devicetrees and drivers

lore
Share & tools
lore
#0 lore raw

Hi Inochi, Chen,

I tested the USB port of the Milk-V Duo 256M (SG2002) in peripheral mode on mainline and it works. Before sending a patch I would like to ask which way you prefer, since it touches the board's default.

What I tested (v7.3-rc5, CONFIG_USB_DWC2_DUAL_ROLE=y, g_ether built in), with only this change on top of sg2002-milkv-duo256m.dtb:

  &usb {
  	dr_mode = "peripheral";
  };

the bare board, plugged into a PC through its USB-C port, enumerates at high speed:

usb 3-9: new high-speed USB device number 53 using xhci_hcd
usb 3-9: Product: RNDIS/Ethernet Gadget
cdc_ether 3-9:1.0 usb0: register 'cdc_ether' at usb-0000:00:14.0-9, CDC Ethernet Device, 12:ca:4b:aa:b9:31

ping and ssh work over it, and iperf3 gives about 111 Mbit/s from the PC to the board and 88 Mbit/s from the board to the PC. No PHY changes were needed; the ID override in the top syscon can stay as it is.

Today all CV18xx boards set dr_mode = "host". On the Duo 256M, though, the USB-C port is the board's only USB connector, and the vendor image uses it in device mode by default (USB networking, which is how most users first log in). Host mode is needed mainly with the USB & Ethernet IO board, whose USB hub hangs off the same controller.

I see two options:

A) Keep "host" as the default and add a separate overlay that switches the port to peripheral mode, like the ethernet one.

B) Make "peripheral" the default for the bare board, and have the existing IO board overlay (sg2002-milkv-duo256m-eth.dtso) set dr_mode = "host", since that board is what needs host mode.

B matches how the hardware is normally used, but it changes the default for anyone using the bare board as a host today, for example through an OTG adapter. A is the conservative choice.

Which one would you prefer? I'm happy to send either, and to do the same for the other Duo boards if you want them kept consistent (Hiago, I've added you since the Duo overlay would be affected the same way).

Thanks,
Chen-Yu

maintainer
#1 lore raw
Quoted 6 lines · Chen-Yu Yeh

Hi Inochi, Chen,

I tested the USB port of the Milk-V Duo 256M (SG2002) in peripheral mode on mainline and it works. Before sending a patch I would like to ask which way you prefer, since it touches the board's default.

In fact, neither is prefered, the right way to use dual-role switch, but it seems like broken. If you want to fix this, feel free to do that. If not, ship the dts with the behavior from vendor.

Regards,
Inochi

Quoted 44 lines

What I tested (v7.3-rc5, CONFIG_USB_DWC2_DUAL_ROLE=y, g_ether built in), with only this change on top of sg2002-milkv-duo256m.dtb:

  &usb {
  	dr_mode = "peripheral";
  };

the bare board, plugged into a PC through its USB-C port, enumerates at high speed:

usb 3-9: new high-speed USB device number 53 using xhci_hcd
usb 3-9: Product: RNDIS/Ethernet Gadget
cdc_ether 3-9:1.0 usb0: register 'cdc_ether' at usb-0000:00:14.0-9, CDC Ethernet Device, 12:ca:4b:aa:b9:31

ping and ssh work over it, and iperf3 gives about 111 Mbit/s from the PC to the board and 88 Mbit/s from the board to the PC. No PHY changes were needed; the ID override in the top syscon can stay as it is.

Today all CV18xx boards set dr_mode = "host". On the Duo 256M, though, the USB-C port is the board's only USB connector, and the vendor image uses it in device mode by default (USB networking, which is how most users first log in). Host mode is needed mainly with the USB & Ethernet IO board, whose USB hub hangs off the same controller.

I see two options:

A) Keep "host" as the default and add a separate overlay that switches the port to peripheral mode, like the ethernet one.

B) Make "peripheral" the default for the bare board, and have the existing IO board overlay (sg2002-milkv-duo256m-eth.dtso) set dr_mode = "host", since that board is what needs host mode.

B matches how the hardware is normally used, but it changes the default for anyone using the bare board as a host today, for example through an OTG adapter. A is the conservative choice.

Which one would you prefer? I'm happy to send either, and to do the same for the other Duo boards if you want them kept consistent (Hiago, I've added you since the Duo overlay would be affected the same way).

Thanks,
Chen-Yu

#2 lore raw

(Resending as plain text, the list rejected the HTML version. Sorry for the duplicate.)

Quoted 3 lines · Inochi Amaoto

In fact, neither is prefered, the right way to use dual-role switch, but it seems like broken. If you want to fix this, feel free to do that. If not, ship the dts with the behavior from vendor.

Thanks. I'll look into getting the dual-role switch working on CV18xx, so that one DTB can do both host and peripheral.

Could you tell me what you saw broken when you tried it? From reading the code, my guess is the PHY: with dr_mode = "otg", usb_add_hcd() sets the PHY to host mode at probe (ID override in the top syscon), and nothing sets it back when the role switches to device. The PHY driver also treats OTG mode as a no-op. I'll start testing from there.

And to make sure I read you correctly: if this can't be fixed in reasonable time, do you mean I should send a dts patch that matches the vendor image's behavior (peripheral by default)?

Thanks,
Chen-Yu

maintainer
#3 lore raw
Quoted 8 lines · Chen-Yu Yeh

(Resending as plain text, the list rejected the HTML version. Sorry for the duplicate.)

Quoted 3 lines · Inochi Amaoto

In fact, neither is prefered, the right way to use dual-role switch, but it seems like broken. If you want to fix this, feel free to do that. If not, ship the dts with the behavior from vendor.

Quoted 9 lines

Thanks. I'll look into getting the dual-role switch working on CV18xx, so that one DTB can do both host and peripheral.

Could you tell me what you saw broken when you tried it? From reading the code, my guess is the PHY: with dr_mode = "otg", usb_add_hcd() sets the PHY to host mode at probe (ID override in the top syscon), and nothing sets it back when the role switches to device. The PHY driver also treats OTG mode as a no-op. I'll start testing from there.

In fact the dual role switch can not init normally, it will produce lots of USB error in startup if you enable debug information. The OTG mode for phy need a hardware signal and the document is kind of messy, I think it may need a line to trigger it.

Quoted 4 lines

And to make sure I read you correctly: if this can't be fixed in reasonable time, do you mean I should send a dts patch that matches the vendor image's behavior (peripheral by default)?

Yes, this is what I mean. You should match the vendor's behavior as everyone starts from the vendor image.

Regards,
Inochi

author
#4 lore raw
Quoted 4 lines · Inochi Amaoto

In fact the dual role switch can not init normally, it will produce lots of USB error in startup if you enable debug information. The OTG mode for phy need a hardware signal and the document is kind of messy, I think it may need a line to trigger it.

I see the same with usb-role-switch. dwc2's drd code switches roles through the GOTGCTL session overrides. Once B-valid is overridden off while the core is a B-device with VBUS present, GOTGINT.SesEndDet (0x4) fires back to back and the board hangs in the IRQ handler. This happens when switching to host, and also at probe without role-switch-default-mode.

I don't think an extra line is needed, though. The vendor kernel switches roles only by writing the ID override in the top syscon (0x48, bits 6/7, the same bits phy-cv1800-usb2 already uses) and lets dwc2's own OTG code follow the Connector ID change. With dr_mode = "otg" and no usb-role-switch, doing the same with devmem works on mainline:

- Duo 256M on a PC, switched between device and host three times:
every switch to device enumerates at high speed as the RNDIS gadget, every switch to host disconnects, and ping over usb0 works.

- Duo 256M on the IO board: after switching to host, the hub and a USB stick enumerate and sda shows up.

The remaining problem is at boot. usb_add_hcd() sets the PHY to host during probe, but that Connector ID change is not handled: the core is in host mode while GINTMSK still has the device mask, so the port interrupt is masked and the hub is never reset:

                  at boot      after toggling the ID once
  GINTSTS         0x05000023   0x04200021
  GINTMSK         0xd0bc3cd4   0xf300081e   (PrtInt: 0 -> 1)
  HPRT0           0x00021403   0x00001005   (ConnDet pending, Ena 0 -> 1)

So I'm working on a role switch in phy-cv1800-usb2 that drives the ID override, sets the default role (peripheral, as in the vendor image) when the PHY is initialized, and keeps the controller's phy_set_mode() from overriding it in OTG mode. I'll send it once it works on the board, unless you see a problem with this direction.

Thanks,
Chen-Yu