usb: dwc3: respect role-switch-default-mode
Subsystems (2)
PATCH 1/1 usb: dwc3: respect role-switch-default-mode
* Factor out the common pattern of checking the dr_mode property on the usb node and it's parent
* Respect the usb-role-switch property, rather than requiring dr_mode be set
* Override OTG mode with the value in role-switch-default-mode
The devicetree bindings don't dictate that dr_mode is a required property, but the dwc3 driver would refuse to probe if it was unset (this breaks the Qualcomm RB2 board which doesn't set it).
Here, we update the driver to respect the other properties that can be used to describe the controller, and more gracefully handle some of the edge cases.
---
Cc: Ilias Apalodimas <[email protected]>
Cc: Neil Armstrong <[email protected]>
--- drivers/usb/dwc3/dwc3-generic.c | 56 +++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 13 deletions(-)
drivers/usb/dwc3/dwc3-generic.c +43 -13
@@ -49,8 +49,46 @@ struct dwc3_generic_host_priv {struct dwc3_generic_priv gen_priv;struct udevice *vbus_supply; }; +/** +* dwc3_get_preferred_dr_mode() - Get the preferred DR mode for the USB node +* +* Since we don't support dynamic role switching yet in dwc3, this is a slightly +* opinionated wrapper around usb_get_dr_mode() which will respect the +* role-switch-default-mode property if it is present and the dr_mode is OTG. +* +* @child: Node to get the DR mode for +*/ +static enum usb_dr_mode dwc3_get_preferred_dr_mode (ofnode node) +{ +enum usb_dr_mode mode; +bool is_role_switch =ofnode_has_property (node,"usb-role-switch" ); + + mode =usb_get_dr_mode (node); +/* Attempt to search the parent node too */ +if (mode ==USB_DR_MODE_UNKNOWN ) { + node =ofnode_get_parent (node); + mode =usb_get_dr_mode (node); + is_role_switch |=ofnode_has_property (node,"usb-role-switch" ); + } + +/* If the usb-role-switch property is present, but dr_mode isn't, then the +* default is OTG. +*/ +if (is_role_switch && mode ==USB_DR_MODE_UNKNOWN ) + mode =USB_DR_MODE_OTG ; + +/* Respect the role-switch-default-mode property */ +if (mode ==USB_DR_MODE_OTG && is_role_switch) { + mode =usb_get_role_switch_default_mode (node); +if (mode ==USB_DR_MODE_UNKNOWN ) + mode =USB_DR_MODE_OTG ; + } + +return mode; +} +static int dwc3_generic_probe (struct udevice *dev,struct dwc3_generic_priv *priv) {int rc;
@@ -178,17 +216,12 @@ static int dwc3_generic_of_to_plat(struct udevice *dev)pr_info ("No USB maximum speed specified. Using super speed\n" ); plat->maximum_speed =USB_SPEED_SUPER ; } - plat->dr_mode =usb_get_dr_mode (node); + plat->dr_mode =dwc3_get_preferred_dr_mode (node);if (plat->dr_mode ==USB_DR_MODE_UNKNOWN ) { -/* might be a leaf so check the parent for mode */ - node =dev_ofnode (dev->parent); - plat->dr_mode =usb_get_dr_mode (node); -if (plat->dr_mode ==USB_DR_MODE_UNKNOWN ) { -pr_err ("Invalid usb mode setup\n" ); -return -ENODEV ; - } +pr_err ("Invalid usb mode setup\n" ); +return -ENODEV ; }return 0 ; }
@@ -541,12 +574,9 @@ static int dwc3_glue_bind_common(struct udevice *parent, ofnode node)int ret;debug ("%s: subnode name: %s\n" , __func__, name); -/* if the parent node doesn't have a mode check the leaf */ - dr_mode =usb_get_dr_mode (dev_ofnode (parent)); -if (!dr_mode) - dr_mode =usb_get_dr_mode (node); + dr_mode =dwc3_get_preferred_dr_mode (node);if (CONFIG_IS_ENABLED (DM_USB_GADGET ) && (dr_mode ==USB_DR_MODE_PERIPHERAL || dr_mode ==USB_DR_MODE_OTG )) {debug ("%s: dr_mode: OTG or Peripheral\n" , __func__);
@@ -698,9 +728,9 @@ int dwc3_glue_probe(struct udevice *dev)while (child) {enum usb_dr_mode dr_mode; - dr_mode =usb_get_dr_mode (dev_ofnode (child)); + dr_mode =dwc3_get_preferred_dr_mode (dev_ofnode (child));device_find_next_child (&child);if (ops && ops->glue_configure) ops->glue_configure (dev, index, dr_mode); index++;
Signature · 1 line
2.45.0
Quoted 34 lines · Caleb Connolly
* Factor out the common pattern of checking the
dr_modeproperty on the usb node and it's parent
* Respect the usb-role-switch property, rather than requiringdr_modebe set
* Override OTG mode with the value in role-switch-default-modeThe devicetree bindings don't dictate that
dr_modeis a required property, but the dwc3 driver would refuse to probe if it was unset (this breaks the Qualcomm RB2 board which doesn't set it).Here, we update the driver to respect the other properties that can be used to describe the controller, and more gracefully handle some of the edge cases.
Signed-off-by Caleb Connolly <[email protected]>---
Cc: Ilias Apalodimas <[email protected]>
Cc: Neil Armstrong <[email protected]>--- drivers/usb/dwc3/dwc3-generic.c | 56 +++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 13 deletions(-)drivers/usb/dwc3/dwc3-generic.c +3
-0@@ -49,8 +49,46 @@ struct dwc3_generic_host_priv {struct dwc3_generic_priv gen_priv;struct udevice *vbus_supply; }; +/** +* dwc3_get_preferred_dr_mode() - Get the preferred DR mode for the USB node +*
drivers/usb/dwc3/dwc3-generic.c struct dwc3_generic_host_priv { · line 65
Quoted 10 lines
drivers/usb/dwc3/dwc3-generic.c +10
-0+* Since we don't support dynamic role switching yet in dwc3, this is a slightly +* opinionated wrapper around usb_get_dr_mode() which will respect the +* role-switch-default-mode property if it is present and the dr_mode is OTG. +* +* @child: Node to get the DR mode for +*/ +static enum usb_dr_mode dwc3_get_preferred_dr_mode (ofnode node) +{ +enum usb_dr_mode mode; +bool is_role_switch =ofnode_has_property (node,"usb-role-switch" );
The usb-role-switch DT property is not DWC3 specific, even in U-Boot it is used by e.g. STM32MP15xx which is DWC2. This should be added to core code.
Also, it would be really good to sync the DWC3 driver with more up to date Linux state, but that's out of the scope of this patch obviously.
Hi Marek,
Quoted 41 lines · Marek Vasut
Quoted 40 lines · Caleb Connolly
* Factor out the common pattern of checking the
dr_modeproperty on the usb node and it's parent
* Respect the usb-role-switch property, rather than requiringdr_modebe set
* Override OTG mode with the value in role-switch-default-modeThe devicetree bindings don't dictate that
dr_modeis a required property, but the dwc3 driver would refuse to probe if it was unset (this breaks the Qualcomm RB2 board which doesn't set it).Here, we update the driver to respect the other properties that can be used to describe the controller, and more gracefully handle some of the edge cases.
Signed-off-by Caleb Connolly <[email protected]>---
Cc: Ilias Apalodimas <[email protected]>
Cc: Neil Armstrong <[email protected]>
---
drivers/usb/dwc3/dwc3-generic.c| 56 +++++++++++++++++++++++++--------1 file changed, 43 insertions(+), 13 deletions(-)drivers/usb/dwc3/dwc3-generic.c +2
-0@@ -49,8 +49,46 @@ struct dwc3_generic_host_priv { struct dwc3_generic_priv gen_priv; struct udevice *vbus_supply; }; +/** +* dwc3_get_preferred_dr_mode() - Get the preferred DR mode for the USB node
+ *
+ * Since we don't support dynamic role switching yet in dwc3, this is a slightly
+ * opinionated wrapper aroundusb_get_dr_mode()which will respect the
+ * role-switch-default-mode property if it is present and thedr_modeis OTG.
+ *
Quoted 8 lines
drivers/usb/dwc3/dwc3-generic.c struct dwc3_generic_host_priv { · line 65
Quoted 6 lines
+ * @child: Node to get the DR mode for
+ */drivers/usb/dwc3/dwc3-generic.c +4
-0+static enum usb_dr_mode dwc3_get_preferred_dr_mode (ofnode node) +{ + enum usb_dr_mode mode; + bool is_role_switch = ofnode_has_property(node, "usb-role-switch");The usb-role-switch DT property is not DWC3 specific, even in U-Boot it is used by e.g. STM32MP15xx which is DWC2. This should be added to core code.
I couldn't see an obvious way to add this desired behaviour without risking breaking some existing boards. Do you mean keep this as a new function but just move it to core code? Or update the existing get_dr_mode() function?
Quoted 2 lines
Also, it would be really good to sync the DWC3 driver with more up to date Linux state, but that's out of the scope of this patch obviously.
Yes, I'm definitely feeling this. Last time I tried updating dwc3 as you suggested (reverting all patches since the Linux import, updating, re-applying)... But there have just been too many changes on the Linux side for this to work anymore, and too many arch specific dwc3 glue drivers which call into the core that might break.
I would love it if we could figure out some path forward here, even if that's just someone spending more than half a day on the porting effort :D
Signature · 1 line
// Caleb (they/them)
Quoted 53 lines · Caleb Connolly
Hi Marek,
Quoted 41 lines · Marek Vasut
Quoted 40 lines · Caleb Connolly
* Factor out the common pattern of checking the
dr_modeproperty on the usb node and it's parent
* Respect the usb-role-switch property, rather than requiringdr_modebe set
* Override OTG mode with the value in role-switch-default-modeThe devicetree bindings don't dictate that
dr_modeis a required property, but the dwc3 driver would refuse to probe if it was unset (this breaks the Qualcomm RB2 board which doesn't set it).Here, we update the driver to respect the other properties that can be used to describe the controller, and more gracefully handle some of the edge cases.
Signed-off-by Caleb Connolly <[email protected]>---
Cc: Ilias Apalodimas <[email protected]>
Cc: Neil Armstrong <[email protected]>
---
drivers/usb/dwc3/dwc3-generic.c| 56 +++++++++++++++++++++++++--------1 file changed, 43 insertions(+), 13 deletions(-)drivers/usb/dwc3/dwc3-generic.c +2
-0@@ -49,8 +49,46 @@ struct dwc3_generic_host_priv { struct dwc3_generic_priv gen_priv; struct udevice *vbus_supply; }; +/** +* dwc3_get_preferred_dr_mode() - Get the preferred DR mode for the USB node
+ *
+ * Since we don't support dynamic role switching yet in dwc3, this is a slightly
+ * opinionated wrapper aroundusb_get_dr_mode()which will respect the
+ * role-switch-default-mode property if it is present and thedr_modeis OTG.
+ *Quoted 8 lines
drivers/usb/dwc3/dwc3-generic.c struct dwc3_generic_host_priv { · line 65
Quoted 6 lines
+ * @child: Node to get the DR mode for
+ */drivers/usb/dwc3/dwc3-generic.c +4
-0+static enum usb_dr_mode dwc3_get_preferred_dr_mode (ofnode node) +{ + enum usb_dr_mode mode; + bool is_role_switch = ofnode_has_property(node, "usb-role-switch");The usb-role-switch DT property is not DWC3 specific, even in U-Boot it is used by e.g. STM32MP15xx which is DWC2. This should be added to core code.
Quoted 1 line
I couldn't see an obvious way to add this desired behaviour without risking breaking some existing boards. Do you mean keep this as a new function but just move it to core code?
Yes, make this available to other drivers by placing it into core code.
Quoted 1 line
Or update the existing
get_dr_mode()function?
Better keep the get_dr_mode() as-is , I think the get_preferred_dr_mode() is a superset, right ?
You could prepare a conversion series and switch other drivers over to it, people can then test and selectively revert if something breaks.
Quoted 3 lines
Quoted 1 line
Also, it would be really good to sync the DWC3 driver with more up to date Linux state, but that's out of the scope of this patch obviously.
Yes, I'm definitely feeling this. Last time I tried updating dwc3 as you suggested (reverting all patches since the Linux import, updating, re-applying)... But there have just been too many changes on the Linux side for this to work anymore, and too many arch specific dwc3 glue drivers which call into the core that might break.
I am hoping these arch/ drivers should have been cleaned up by now, what is there that is still open ?
Quoted 1 line
I would love it if we could figure out some path forward here, even if that's just someone spending more than half a day on the porting effort :D
I'd love to find a way to update too.