Skip to contentSkip to conversation

Add power sequence for hard-wired USB devices

PATCH New Peter Chen 7 participants 21 messages Mar 3, 2016 – Apr 5, 2016

Subsystems (4)
lore
Share & tools
lore

Peter Chen

author
#0 lore raw

cover Add power sequence for hard-wired USB devices

Hi all,

I have a patch for adding device tree support for USB device [1] some days ago, this is a follow-up to support the USB devices which need to do some power sequence like enabling USB PHY clock, toggling reset gpio, etc, before this device can work normal. I have seen some use cases for it.

In the 1st patch, the USB HUB driver will iterate all its sub devices described at dts, and do power on sequence if there is a phandle "usb-pwrseq" for it. The user can add its specific clock and reset pin in its dts to support this feature.

In the 2nd patch, there is a workaround for chipidea driver to let the hcd know its device node.

In the 3rd patch, it is a user case for this problem. @Maciej @Fabio, would you kind to test it at udoo boards, and supply the related vid/pid for this HUB?
@troy, would you kind add your dts for it at nitrogen6x board?

[1] http://www.spinics.net/lists/linux-usb/msg136698.html

Peter Chen (3):
usb: core: add power sequence for USB devices
usb: chipidea: host: let the hcd know's parent device node
ARM: dts: imx6qdl-udoo.dtsi: fix onboard USB HUB property

 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 arch/arm/boot/dts/imx6qdl-udoo.dtsi                |  35 ++---
 drivers/usb/chipidea/host.c                        |  18 ++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 7 files changed, 260 insertions(+), 27 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

---
 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 5 files changed, 232 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Documentation/devicetree/bindings/usb/usb-device.txt +40 -1
index 1c35e7b..c7a298c 100644
@@ -13,8 +13,37 @@ Required properties:
 - reg: the port number which this device is connecting to, the range
   is 1-31.
 
+Optional properties:
+- usb-pwrseq: the power sequence handler which need to do before this USB
+  device can work.
+- clocks: the input clock for USB device.
+- clock-frequency: the frequency for device's clock.
+- reset-gpios: Should specify the GPIO for reset.
+- reset-duration-us: the duration in microsecond for assert reset signal.
+
 Example:
 
+/ {
+	...
+
+	hub_genesys_1_pwrseq: hub_genesys_1_pwrseq {
+		compatible = "usb-pwrseq";
+		reset-gpios = <&gpio4 5 GPIO_ACTIVE_LOW>; /* hub reset pin */
+		reset-duration-us = <10>;
+		clocks = <&clks IMX6QDL_CLK_CKO>;
+	};
+
+	ethernet_asix_2_pwrseq: ethernet_asix_2_pwrseq {
+		compatible = "usb-pwrseq";
+		reset-gpios = <&gpio4 6 GPIO_ACTIVE_HIGH>; /* ethernet_rst */
+		reset-duration-us = <20>;
+		clock-frequency = <24000000>;
+		clocks = <&clks IMX6QDL_CLK_CKO1>;
+	};
+
+	...
+};
+
 &usb1 {
 	status = "okay";
 
@@ -24,5 +53,15 @@ Example:
 	hub: genesys at 1 {
 		compatible = "usb5e3,608";
 		reg = <1>;
+		usb-pwrseq = <&hub_genesys_1_pwrseq>;
+
+		#address-cells = <1>;
+		#size-cells = <0>;
+
+		ethernet: asix at 1 {
+			compatible = "usbb95,1708";
+			reg = <1>;
+			usb-pwrseq = <&ethernet_asix_1_pwrseq>;
+		};
 	};
-}
+};
drivers/usb/core/Makefile +1 -1
index 9780877..9cdc548 100644
@@ -5,7 +5,7 @@
 usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o
 usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o
 usbcore-y += devio.o notify.o generic.o quirks.o devices.o
-usbcore-y += port.o of.o
+usbcore-y += port.o of.o pwrseq.o
 
 usbcore-$(CONFIG_PCI)		+= hcd-pci.o
 usbcore-$(CONFIG_ACPI)		+= usb-acpi.o
drivers/usb/core/hub.c +32 -0
index 0f82db4..0091428 100644
@@ -26,6 +26,8 @@
 #include <linux/mutex.h>
 #include <linux/random.h>
 #include <linux/pm_qos.h>
+#include <linux/of.h>
+#include <linux/usb/of.h>
 
 #include <asm/uaccess.h>
 #include <asm/byteorder.h>
@@ -1680,6 +1682,27 @@ static void hub_release(struct kref *kref)
 	kfree(hub);
 }
 
+static int hub_of_pwrseq(struct usb_device *hdev, bool on)
+{
+	struct device *parent;
+	struct device_node *node;
+	int ret = 0;
+
+	if (hdev->parent)
+		parent = &hdev->dev;
+	else
+		parent = bus_to_hcd(hdev->bus)->self.controller;
+
+	for_each_child_of_node(parent->of_node, node) {
+		ret = on ? usb_child_pwrseq_on(node)
+			: usb_child_pwrseq_off(node);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static unsigned highspeed_hubs;
 
 static void hub_disconnect(struct usb_interface *intf)
@@ -1722,6 +1745,10 @@ static void hub_disconnect(struct usb_interface *intf)
 	kfree(hub->buffer);
 
 	pm_suspend_ignore_children(&intf->dev, false);
+
+	if (hub_of_pwrseq(hdev, false))
+		dev_err(&hdev->dev, "failed to do power operations off\n");
+
 	kref_put(&hub->kref, hub_release);
 }
 
@@ -1731,6 +1758,7 @@ static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
 	struct usb_endpoint_descriptor *endpoint;
 	struct usb_device *hdev;
 	struct usb_hub *hub;
+	int ret;
 
 	desc = intf->cur_altsetting;
 	hdev = interface_to_usbdev(intf);
@@ -1850,6 +1878,10 @@ descriptor_error:
 	if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
 		hub->quirk_check_port_auto_suspend = 1;
 
+	ret = hub_of_pwrseq(hdev, true);
+	if (ret)
+		dev_err(&hdev->dev, "failed to do power operations on\n");
+
 	if (hub_configure(hub, endpoint) >= 0)
 		return 0;
 
drivers/usb/core/pwrseq.c new file +149 -0

149 lines not on this page · raw message

include/linux/usb/of.h +10 -0
index de3237f..1adf065 100644
@@ -18,6 +18,8 @@ int of_usb_update_otg_caps(struct device_node *np,
 			struct usb_otg_caps *otg_caps);
 struct device_node *usb_of_get_child_node(struct device_node *parent,
 			int portnum);
+int usb_child_pwrseq_on(struct device_node *node);
+int usb_child_pwrseq_off(struct device_node *node);
 #else
 static inline enum usb_dr_mode
 of_usb_get_dr_mode_by_phy(struct device_node *phy_np)
@@ -38,6 +40,14 @@ static inline struct device_node *usb_of_get_child_node
 {
 	return NULL;
 }
+static inline int usb_child_pwrseq_on(struct device_node *node)
+{
+	return 0;
+}
+static inline int usb_child_pwrseq_off(struct device_node *node)
+{
+	return 0;
+}
 #endif
 
 #if IS_ENABLED(CONFIG_OF) && IS_ENABLED(CONFIG_USB_SUPPORT)
Signature · 1 line
1.9.1
author
#13 lore raw

PATCH 2/3 usb: chipidea: host: let the hcd know's parent device node

In reply to Peter Chen

From: Peter Chen <[email protected]>

Since the hcd (chipidea core device) has no device node, so if we want to describe the child node under the hcd, we had to put it under its parent's node (glue layer device), and in the code, we need to let the hcd knows glue layer's code, then the USB core can handle this node.

---
 drivers/usb/chipidea/host.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/usb/chipidea/host.c +14 -4
index 053bac9..55120ef 100644
@@ -109,15 +109,25 @@ static int host_start(struct ci_hdrc *ci)
 	struct ehci_hcd *ehci;
 	struct ehci_ci_priv *priv;
 	int ret;
+	struct device *dev = ci->dev;
 
-	if (usb_disabled())
+	if (usb_disabled() || !dev)
 		return -ENODEV;
 
-	hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
+	/*
+	 * USB Core will try to get child node under roothub,
+	 * but chipidea core has no of_node, and the child node
+	 * for controller is located at glue layer's node which
+	 * is chipidea core's parent.
+	 */
+	if (dev->parent && dev->parent->of_node)
+		dev->of_node = dev->parent->of_node;
+
+	hcd = usb_create_hcd(&ci_ehci_hc_driver, dev, dev_name(dev));
 	if (!hcd)
 		return -ENOMEM;
 
-	dev_set_drvdata(ci->dev, ci);
+	dev_set_drvdata(dev, ci);
 	hcd->rsrc_start = ci->hw_bank.phys;
 	hcd->rsrc_len = ci->hw_bank.size;
 	hcd->regs = ci->hw_bank.abs;
@@ -143,7 +153,7 @@ static int host_start(struct ci_hdrc *ci)
 		if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
 			ret = regulator_enable(ci->platdata->reg_vbus);
 			if (ret) {
-				dev_err(ci->dev,
+				dev_err(dev,
 				"Failed to enable vbus regulator, ret=%d\n",
 									ret);
 				goto put_hcd;
Signature · 1 line
1.9.1
author
#18 lore raw

PATCH 3/3 ARM: dts: imx6qdl-udoo.dtsi: fix onboard USB HUB property

In reply to Peter Chen

From: Peter Chen <[email protected]>

The current dts describes USB HUB's property at USB controller's entry, it is improper. The USB HUB should be the child node under USB controller.

---
 arch/arm/boot/dts/imx6qdl-udoo.dtsi | 35 ++++++++++++++---------------------
 1 file changed, 14 insertions(+), 21 deletions(-)
arch/arm/boot/dts/imx6qdl-udoo.dtsi +14 -21
index 1211da8..d8a4274 100644
@@ -9,6 +9,8 @@
  *
  */
 
+#include <dt-bindings/gpio/gpio.h>
+
 / {
 	chosen {
 		stdout-path = &uart2;
@@ -17,23 +19,6 @@
 	memory {
 		reg = <0x10000000 0x40000000>;
 	};
-
-	regulators {
-		compatible = "simple-bus";
-		#address-cells = <1>;
-		#size-cells = <0>;
-
-		reg_usb_h1_vbus: regulator at 0 {
-			compatible = "regulator-fixed";
-			reg = <0>;
-			regulator-name = "usb_h1_vbus";
-			regulator-min-microvolt = <5000000>;
-			regulator-max-microvolt = <5000000>;
-			enable-active-high;
-			startup-delay-us = <2>; /* USB2415 requires a POR of 1 us minimum */
-			gpio = <&gpio7 12 0>;
-		};
-	};
 };
 
 &fec {
@@ -119,11 +104,19 @@
 };
 
 &usbh1 {
-	pinctrl-names = "default";
-	pinctrl-0 = <&pinctrl_usbh>;
-	vbus-supply = <&reg_usb_h1_vbus>;
-	clocks = <&clks 201>;
 	status = "okay";
+
+	#address-cells = <1>;
+	#size-cells = <0>;
+	hub: usb2415 at 01 {
+		compatible = "usbxxxx,xxxx";
+		reg = <1>;
+		pinctrl-names = "default";
+		pinctrl-0 = <&pinctrl_usbh>;
+		clocks = <&clks IMX6QDL_CLK_CKO>;
+		reset-gpios = <&gpio7 12 GPIO_ACTIVE_LOW>;
+		reset-duration-us = <3000>;
+	};
 };
 
 &usdhc3 {
Signature · 1 line
1.9.1
#14 lore raw

PATCH 2/3 usb: chipidea: host: let the hcd know's parent device node

In reply to Peter Chen
Quoted 26 lines · Peter Chen

From: Peter Chen <[email protected]>

Since the hcd (chipidea core device) has no device node, so if we want to describe the child node under the hcd, we had to put it under its parent's node (glue layer device), and in the code, we need to let the hcd knows glue layer's code, then the USB core can handle this node.

---
 drivers/usb/chipidea/host.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/usb/chipidea/host.c +2 -1
index 053bac9..55120ef 100644
@@ -109,15 +109,25 @@ static int host_start(struct ci_hdrc *ci)
 	struct ehci_hcd *ehci;
 	struct ehci_ci_priv *priv;
 	int ret;
+	struct device *dev = ci->dev;
 
-	if (usb_disabled())
+	if (usb_disabled() || !dev)
 		return -ENODEV;

drivers/usb/chipidea/host.c static int host_start(struct ci_hdrc *ci) · line 124

Quoted 10 lines
drivers/usb/chipidea/host.c +8 -1
 
-	hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
+	/*
+	 * USB Core will try to get child node under roothub,
+	 * but chipidea core has no of_node, and the child node
+	 * for controller is located at glue layer's node which
+	 * is chipidea core's parent.
+	 */
+	if (dev->parent && dev->parent->of_node)
+		dev->of_node = dev->parent->of_node;

Is this a good idea? Two devices with the same of_node?

I know the networking code assumes of_node values are unique, and uses it to find a device. Are you 100% sure the USB code does not make this assumption.

Andrew

#2 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

In reply to Peter Chen
Quoted 24 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

drivers/usb/core/hub.c +10 -0
+static int hub_of_pwrseq(struct usb_device *hdev, bool on)
+{
+	struct device *parent;
+	struct device_node *node;
+	int ret = 0;
+
+	if (hdev->parent)
+		parent = &hdev->dev;
+	else
+		parent = bus_to_hcd(hdev->bus)->self.controller;

drivers/usb/core/hub.c static void hub_release(struct kref *kref) · line 1704

Quoted 10 lines
drivers/usb/core/hub.c +10 -0
+
+	for_each_child_of_node(parent->of_node, node) {
+		ret = on ? usb_child_pwrseq_on(node)
+			: usb_child_pwrseq_off(node);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}

If you get a failure, do you want to leave the power to all the preceding devices turned on? It seems to me you should either turn all of them back off, or else continue trying to turn on power for the remaining children.

Alan Stern

#4 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

In reply to Peter Chen
Quoted 26 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

---
 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 5 files changed, 232 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Documentation/devicetree/bindings/usb/usb-device.txt +0 -0
index 1c35e7b..c7a298c 100644

Documentation/devicetree/bindings/usb/usb-device.txt Required properties: · line 22

Quoted 10 lines
Documentation/devicetree/bindings/usb/usb-device.txt +7 -0
@@ -13,8 +13,37 @@ Required properties:
 - reg: the port number which this device is connecting to, the range
   is 1-31.
 
+Optional properties:
+- usb-pwrseq: the power sequence handler which need to do before this USB
+  device can work.
+- clocks: the input clock for USB device.
+- clock-frequency: the frequency for device's clock.
+- reset-gpios: Should specify the GPIO for reset.
+- reset-duration-us: the duration in microsecond for assert reset signal.

So we sorted out how to describe USB devices in DT, but now we don't use it? I know this is similar to what was done for MMC, but I really don't like it.

Just put all these properties into the device nodes. The parent can then scan for children and handle the power sequencing with generic code. I think it is safe to assume that if these properties are present, they need to be handled for device detection. We may still need something device specific to handle some devices at some point, but that is true either way.

Rob

#19 lore raw

PATCH 3/3 ARM: dts: imx6qdl-udoo.dtsi: fix onboard USB HUB property

In reply to Peter Chen

Hi Peter,

Quoted 7 lines · Peter Chen

From: Peter Chen <[email protected]>

The current dts describes USB HUB's property at USB controller's entry, it is improper. The USB HUB should be the child node under USB controller.

Thanks for patches!

VID:PID for hub on UDOO board is 0424:2514.

I've tested these patches on this board and was able to make USB work again with following DT changes:

arch/arm/boot/dts/imx6qdl-udoo.dtsi +16 -17
index d3e54e4..4956de7 100644
@@ -9,6 +9,8 @@
  *
  */
 
+#include <dt-bindings/gpio/gpio.h>
+
 / {
 	chosen {
 		stdout-path = &uart2;
@@ -18,21 +27,11 @@
 		reg = <0x10000000 0x40000000>;
 	};
 
-	regulators {
-		compatible = "simple-bus";
-		#address-cells = <1>;
-		#size-cells = <0>;
-
-		reg_usb_h1_vbus: regulator at 0 {
-			compatible = "regulator-fixed";
-			reg = <0>;
-			regulator-name = "usb_h1_vbus";
-			regulator-min-microvolt = <5000000>;
-			regulator-max-microvolt = <5000000>;
-			enable-active-high;
-			startup-delay-us = <2>; /* USB2415 requires a POR of 1 us minimum */
-			gpio = <&gpio7 12 0>;
-		};
+	usb2415_pwrseq: usb2415_pwrseq {
+		compatible = "usb-pwrseq";
+		clocks = <&clks IMX6QDL_CLK_CKO>;
+		reset-gpios = <&gpio7 12 GPIO_ACTIVE_LOW>;
+		reset-duration-us = <3000>;
 	};
 
 	sound {
@@ -163,9 +218,16 @@
 &usbh1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&pinctrl_usbh>;
-	vbus-supply = <&reg_usb_h1_vbus>;
-	clocks = <&clks 201>;
 	status = "okay";
+
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	hub: usb2415 at 01 {
+		compatible = "usb424,2514";
+		reg = <1>;
+		usb-pwrseq = <&usb2415_pwrseq>;
+	};
 };
 
 &usdhc3 {

Maciej

#15 lore raw

PATCH 2/3 usb: chipidea: host: let the hcd know's parent device node

In reply to Andrew Lunn
Quoted 28 lines · Andrew Lunn
Quoted 27 lines · Peter Chen

From: Peter Chen <[email protected]>

Since the hcd (chipidea core device) has no device node, so if we want to describe the child node under the hcd, we had to put it under its parent's node (glue layer device), and in the code, we need to let the hcd knows glue layer's code, then the USB core can handle this node.

---
 drivers/usb/chipidea/host.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/usb/chipidea/host.c +2 -1
index 053bac9..55120ef 100644
@@ -109,15 +109,25 @@ static int host_start(struct ci_hdrc *ci)
 	struct ehci_hcd *ehci;
 	struct ehci_ci_priv *priv;
 	int ret;
+	struct device *dev = ci->dev;
 
-	if (usb_disabled())
+	if (usb_disabled() || !dev)
 		return -ENODEV;
 
Quoted 12 lines

drivers/usb/chipidea/host.c static int host_start(struct ci_hdrc *ci) · line 124

Quoted 9 lines
drivers/usb/chipidea/host.c +8 -1
-	hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
+	/*
+	 * USB Core will try to get child node under roothub,
+	 * but chipidea core has no of_node, and the child node
+	 * for controller is located at glue layer's node which
+	 * is chipidea core's parent.
+	 */
+	if (dev->parent && dev->parent->of_node)
+		dev->of_node = dev->parent->of_node;

Is this a good idea? Two devices with the same of_node?

This is only for chipidea driver whose host controller device doesn't have entry at dts, but other host controller driver which supports device tree should have its entry at dts.

Quoted 4 lines

I know the networking code assumes of_node values are unique, and uses it to find a device. Are you 100% sure the USB code does not make this assumption.

The controller device is the root for USB device, the common USB code will not touch its glue layer device (controller's parent).

Signature · 2 lines
Best Regards,
Peter Chen
#5 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

In reply to Rob Herring
Quoted 30 lines · Rob Herring
Quoted 29 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

---
 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 5 files changed, 232 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Documentation/devicetree/bindings/usb/usb-device.txt +0 -0
index 1c35e7b..c7a298c 100644
@@ -13,8 +13,37 @@ Required properties:
 - reg: the port number which this device is connecting to, the range
   is 1-31.
 
Quoted 10 lines

Documentation/devicetree/bindings/usb/usb-device.txt Required properties: · line 22

Quoted 7 lines
Documentation/devicetree/bindings/usb/usb-device.txt +7 -0
+Optional properties:
+- usb-pwrseq: the power sequence handler which need to do before this USB
+  device can work.
+- clocks: the input clock for USB device.
+- clock-frequency: the frequency for device's clock.
+- reset-gpios: Should specify the GPIO for reset.
+- reset-duration-us: the duration in microsecond for assert reset signal.

So we sorted out how to describe USB devices in DT, but now we don't use it?

We only know USB device after USB bus finds it, but without power on sequence, the USB bus can't find it.

Quoted 10 lines

I know this is similar to what was done for MMC, but I really don't like it.

Just put all these properties into the device nodes. The parent can then scan for children and handle the power sequencing with generic code. I think it is safe to assume that if these properties are present, they need to be handled for device detection. We may still need something device specific to handle some devices at some point, but that is true either way.

It is two different things, one is power sequence which needs to be done before the device can be detected. Another is the features for this USB devices, it can be handled after this device has been recognized.

Signature · 2 lines
Best Regards,
Peter Chen
#20 lore raw

PATCH 3/3 ARM: dts: imx6qdl-udoo.dtsi: fix onboard USB HUB property

In reply to Maciej S. Szmigiero
Quoted 68 lines · Maciej S. Szmigiero

Hi Peter,

Quoted 7 lines · Peter Chen

From: Peter Chen <[email protected]>

The current dts describes USB HUB's property at USB controller's entry, it is improper. The USB HUB should be the child node under USB controller.

Thanks for patches!

VID:PID for hub on UDOO board is 0424:2514.

I've tested these patches on this board and was able to make USB work again with following DT changes:

arch/arm/boot/dts/imx6qdl-udoo.dtsi +9 -17
index d3e54e4..4956de7 100644
@@ -9,6 +9,8 @@
  *
  */
 
+#include <dt-bindings/gpio/gpio.h>
+
 / {
 	chosen {
 		stdout-path = &uart2;
@@ -18,21 +27,11 @@
 		reg = <0x10000000 0x40000000>;
 	};
 
-	regulators {
-		compatible = "simple-bus";
-		#address-cells = <1>;
-		#size-cells = <0>;
-
-		reg_usb_h1_vbus: regulator at 0 {
-			compatible = "regulator-fixed";
-			reg = <0>;
-			regulator-name = "usb_h1_vbus";
-			regulator-min-microvolt = <5000000>;
-			regulator-max-microvolt = <5000000>;
-			enable-active-high;
-			startup-delay-us = <2>; /* USB2415 requires a POR of 1 us minimum */
-			gpio = <&gpio7 12 0>;
-		};
+	usb2415_pwrseq: usb2415_pwrseq {
+		compatible = "usb-pwrseq";
+		clocks = <&clks IMX6QDL_CLK_CKO>;
+		reset-gpios = <&gpio7 12 GPIO_ACTIVE_LOW>;
+		reset-duration-us = <3000>;
 	};
 
 	sound {
@@ -163,9 +218,16 @@
 &usbh1 {
 	pinctrl-names = "default";
 	pinctrl-0 = <&pinctrl_usbh>;
-	vbus-supply = <&reg_usb_h1_vbus>;
-	clocks = <&clks 201>;
 	status = "okay";
+
+	#address-cells = <1>;

arch/arm/boot/dts/imx6qdl-udoo.dtsi line 233

Quoted 10 lines
arch/arm/boot/dts/imx6qdl-udoo.dtsi +7 -0
+	#size-cells = <0>;
+
+	hub: usb2415 at 01 {
+		compatible = "usb424,2514";
+		reg = <1>;
+		usb-pwrseq = <&usb2415_pwrseq>;
+	};
 };
 
 &usdhc3 {

Thank you very much.

Signature · 2 lines
Best Regards,
Peter Chen
#3 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

In reply to Alan Stern
Quoted 33 lines · Alan Stern
Quoted 31 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

drivers/usb/core/hub.c +17 -0
+static int hub_of_pwrseq(struct usb_device *hdev, bool on)
+{
+	struct device *parent;
+	struct device_node *node;
+	int ret = 0;
+
+	if (hdev->parent)
+		parent = &hdev->dev;
+	else
+		parent = bus_to_hcd(hdev->bus)->self.controller;
+
+	for_each_child_of_node(parent->of_node, node) {
+		ret = on ? usb_child_pwrseq_on(node)
+			: usb_child_pwrseq_off(node);
+		if (ret)
+			return ret;
+	}
Quoted 9 lines

drivers/usb/core/hub.c static void hub_release(struct kref *kref) · line 1704

Quoted 3 lines
drivers/usb/core/hub.c +3 -0
+
+	return 0;
+}

If you get a failure, do you want to leave the power to all the preceding devices turned on? It seems to me you should either turn all of them back off, or else continue trying to turn on power for the remaining children.

Thanks, I will show error message for that device, and continue to turn on power for the remaining children.

Signature · 2 lines
Best Regards,
Peter Chen
#16 lore raw

PATCH 2/3 usb: chipidea: host: let the hcd know's parent device node

In reply to Peter Chen
Quoted 31 lines
Quoted 14 lines
Quoted 14 lines
drivers/usb/chipidea/host.c +2 -1
index 053bac9..55120ef 100644
@@ -109,15 +109,25 @@ static int host_start(struct ci_hdrc *ci)
 	struct ehci_hcd *ehci;
 	struct ehci_ci_priv *priv;
 	int ret;
+	struct device *dev = ci->dev;
 
-	if (usb_disabled())
+	if (usb_disabled() || !dev)
 		return -ENODEV;
 
Quoted 12 lines

drivers/usb/chipidea/host.c static int host_start(struct ci_hdrc *ci) · line 124

Quoted 9 lines
drivers/usb/chipidea/host.c +8 -1
-	hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
+	/*
+	 * USB Core will try to get child node under roothub,
+	 * but chipidea core has no of_node, and the child node
+	 * for controller is located at glue layer's node which
+	 * is chipidea core's parent.
+	 */
+	if (dev->parent && dev->parent->of_node)
+		dev->of_node = dev->parent->of_node;

Is this a good idea? Two devices with the same of_node?

This is only for chipidea driver whose host controller device doesn't have entry at dts, but other host controller driver which supports device tree should have its entry at dts.

Quoted 7 lines
Quoted 4 lines

I know the networking code assumes of_node values are unique, and uses it to find a device. Are you 100% sure the USB code does not make this assumption.

The controller device is the root for USB device, the common USB code will not touch its glue layer device (controller's parent).

I'm just thinking about code like:

of_find_spi_master_by_node(), of_find_net_device_by_node(), of_find_backlight_by_node(), etc.

If somebody was to implement an of_find_usb_host_by_node() are you 100% sure the right node will be found? This seems like a bug waiting to happen.

Andrew

#6 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

In reply to Peter Chen
Quoted 42 lines · Peter Chen
Quoted 30 lines · Rob Herring
Quoted 29 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

---
 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 5 files changed, 232 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Documentation/devicetree/bindings/usb/usb-device.txt +0 -0
index 1c35e7b..c7a298c 100644
@@ -13,8 +13,37 @@ Required properties:
 - reg: the port number which this device is connecting to, the range
   is 1-31.
 
Quoted 10 lines

Documentation/devicetree/bindings/usb/usb-device.txt Required properties: · line 22

Quoted 7 lines
Documentation/devicetree/bindings/usb/usb-device.txt +7 -0
+Optional properties:
+- usb-pwrseq: the power sequence handler which need to do before this USB
+  device can work.
+- clocks: the input clock for USB device.
+- clock-frequency: the frequency for device's clock.
+- reset-gpios: Should specify the GPIO for reset.
+- reset-duration-us: the duration in microsecond for assert reset signal.

So we sorted out how to describe USB devices in DT, but now we don't use it?

Quoted 2 lines

We only know USB device after USB bus finds it, but without power on sequence, the USB bus can't find it.

Not really true. Device tree says it exists, you just cannot see it yet. The fact it is in device tree means it is soldered on the board and really is there. So when the host controller enumerates the bus, it should run the power sequence of all child nodes to make them appear.

Andrew

#17 lore raw

PATCH 2/3 usb: chipidea: host: let the hcd know's parent device node

In reply to Andrew Lunn
Quoted 39 lines · Andrew Lunn
Quoted 31 lines
Quoted 14 lines
Quoted 14 lines
drivers/usb/chipidea/host.c +2 -1
index 053bac9..55120ef 100644
@@ -109,15 +109,25 @@ static int host_start(struct ci_hdrc *ci)
 	struct ehci_hcd *ehci;
 	struct ehci_ci_priv *priv;
 	int ret;
+	struct device *dev = ci->dev;
 
-	if (usb_disabled())
+	if (usb_disabled() || !dev)
 		return -ENODEV;
 
Quoted 12 lines

drivers/usb/chipidea/host.c static int host_start(struct ci_hdrc *ci) · line 124

Quoted 9 lines
drivers/usb/chipidea/host.c +8 -1
-	hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
+	/*
+	 * USB Core will try to get child node under roothub,
+	 * but chipidea core has no of_node, and the child node
+	 * for controller is located at glue layer's node which
+	 * is chipidea core's parent.
+	 */
+	if (dev->parent && dev->parent->of_node)
+		dev->of_node = dev->parent->of_node;

Is this a good idea? Two devices with the same of_node?

This is only for chipidea driver whose host controller device doesn't have entry at dts, but other host controller driver which supports device tree should have its entry at dts.

Quoted 7 lines
Quoted 4 lines

I know the networking code assumes of_node values are unique, and uses it to find a device. Are you 100% sure the USB code does not make this assumption.

The controller device is the root for USB device, the common USB code will not touch its glue layer device (controller's parent).

Quoted 8 lines

I'm just thinking about code like:

of_find_spi_master_by_node(), of_find_net_device_by_node(), of_find_backlight_by_node(), etc.

If somebody was to implement an of_find_usb_host_by_node() are you 100% sure the right node will be found? This seems like a bug waiting to happen.

Yes, almost 100% sure it is ok since the common USB code will never use its root's parent node to match or get something.

Signature · 2 lines
Best Regards,
Peter Chen
#7 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

In reply to Andrew Lunn
Quoted 43 lines · Andrew Lunn
Quoted 42 lines · Peter Chen
Quoted 40 lines · Rob Herring
Quoted 26 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

---
 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 5 files changed, 232 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Documentation/devicetree/bindings/usb/usb-device.txt +0 -0
index 1c35e7b..c7a298c 100644

Documentation/devicetree/bindings/usb/usb-device.txt Required properties: · line 22

Quoted 10 lines
Documentation/devicetree/bindings/usb/usb-device.txt +7 -0
@@ -13,8 +13,37 @@ Required properties:
 - reg: the port number which this device is connecting to, the range
   is 1-31.
 
+Optional properties:
+- usb-pwrseq: the power sequence handler which need to do before this USB
+  device can work.
+- clocks: the input clock for USB device.
+- clock-frequency: the frequency for device's clock.
+- reset-gpios: Should specify the GPIO for reset.
+- reset-duration-us: the duration in microsecond for assert reset signal.

So we sorted out how to describe USB devices in DT, but now we don't use it?

Quoted 9 lines
Quoted 2 lines

We only know USB device after USB bus finds it, but without power on sequence, the USB bus can't find it.

Not really true. Device tree says it exists, you just cannot see it yet. The fact it is in device tree means it is soldered on the board and really is there. So when the host controller enumerates the bus, it should run the power sequence of all child nodes to make them appear.

Yes, it is my patch doing. My words "We only know USB device after USB bus finds it" means the USB common code only create the USB device, and assign its .of_node after USB bus finds it.

Signature · 2 lines
Best Regards,
Peter Chen
maintainer
#8 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

Quoted 47 lines · Peter Chen
Quoted 46 lines · Andrew Lunn
Quoted 44 lines · Peter Chen
Quoted 30 lines · Rob Herring
Quoted 29 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

---
 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 5 files changed, 232 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Documentation/devicetree/bindings/usb/usb-device.txt +0 -0
index 1c35e7b..c7a298c 100644
@@ -13,8 +13,37 @@ Required properties:
 - reg: the port number which this device is connecting to, the range
   is 1-31.
 
Quoted 10 lines

Documentation/devicetree/bindings/usb/usb-device.txt Required properties: · line 22

Quoted 7 lines
Documentation/devicetree/bindings/usb/usb-device.txt +7 -0
+Optional properties:
+- usb-pwrseq: the power sequence handler which need to do before this USB
+  device can work.
+- clocks: the input clock for USB device.
+- clock-frequency: the frequency for device's clock.
+- reset-gpios: Should specify the GPIO for reset.
+- reset-duration-us: the duration in microsecond for assert reset signal.

So we sorted out how to describe USB devices in DT, but now we don't use it?

We only know USB device after USB bus finds it, but without power on sequence, the USB bus can't find it.

Quoted 6 lines
Quoted 6 lines

Not really true. Device tree says it exists, you just cannot see it yet. The fact it is in device tree means it is soldered on the board and really is there. So when the host controller enumerates the bus, it should run the power sequence of all child nodes to make them appear.

Exactly.

Quoted 3 lines

Yes, it is my patch doing. My words "We only know USB device after USB bus finds it" means the USB common code only create the USB device, and assign its .of_node after USB bus finds it.

I understand the problem. It is just as easy for you to search power sequence child nodes as searching the *actual* child nodes for devices. The main difference is that a power sequence node indicates that power sequencing can be handled generically. With the device nodes, you have to assume that the presence of standard properties like reset-gpios means you can handle it generically.

Either solution suffers from not handling cases that can't be handled generically. No doubt, we'll just see continued extensions to keep things generic when they really shouldn't be.

Rob

#9 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

Quoted 68 lines · Rob Herring
Quoted 47 lines · Peter Chen
Quoted 46 lines · Andrew Lunn
Quoted 44 lines · Peter Chen
Quoted 30 lines · Rob Herring
Quoted 29 lines · Peter Chen

Some hard-wired USB devices need to do power sequence to let the device work normally, the typical power sequence like: enable USB PHY clock, toggle reset pin, etc. But current Linux USB driver lacks of such code to do it, it may cause some hard-wired USB devices works abnormal or can't be recognized by controller at all.

In this patch, it will do power on sequence at hub's probe for all devices under this hub (includes root hub) if this device is described at dts and there is a phandle "usb-pwrseq" for it. At hub_disconnect, it will do power off sequence.

---
 .../devicetree/bindings/usb/usb-device.txt         |  41 +++++-
 drivers/usb/core/Makefile                          |   2 +-
 drivers/usb/core/hub.c                             |  32 +++++
 drivers/usb/core/pwrseq.c                          | 149 +++++++++++++++++++++
 include/linux/usb/of.h                             |  10 ++
 5 files changed, 232 insertions(+), 2 deletions(-)
 create mode 100644 drivers/usb/core/pwrseq.c
Documentation/devicetree/bindings/usb/usb-device.txt +0 -0
index 1c35e7b..c7a298c 100644
@@ -13,8 +13,37 @@ Required properties:
 - reg: the port number which this device is connecting to, the range
   is 1-31.
 
Quoted 10 lines

Documentation/devicetree/bindings/usb/usb-device.txt Required properties: · line 22

Quoted 7 lines
Documentation/devicetree/bindings/usb/usb-device.txt +7 -0
+Optional properties:
+- usb-pwrseq: the power sequence handler which need to do before this USB
+  device can work.
+- clocks: the input clock for USB device.
+- clock-frequency: the frequency for device's clock.
+- reset-gpios: Should specify the GPIO for reset.
+- reset-duration-us: the duration in microsecond for assert reset signal.

So we sorted out how to describe USB devices in DT, but now we don't use it?

We only know USB device after USB bus finds it, but without power on sequence, the USB bus can't find it.

Quoted 6 lines
Quoted 6 lines

Not really true. Device tree says it exists, you just cannot see it yet. The fact it is in device tree means it is soldered on the board and really is there. So when the host controller enumerates the bus, it should run the power sequence of all child nodes to make them appear.

Exactly.

Quoted 3 lines

Yes, it is my patch doing. My words "We only know USB device after USB bus finds it" means the USB common code only create the USB device, and assign its .of_node after USB bus finds it.

I understand the problem. It is just as easy for you to search power sequence child nodes as searching the *actual* child nodes for devices. The main difference is that a power sequence node indicates that power sequencing can be handled generically. With the device nodes, you have to assume that the presence of standard properties like reset-gpios means you can handle it generically.

Quoted 4 lines

Either solution suffers from not handling cases that can't be handled generically. No doubt, we'll just see continued extensions to keep things generic when they really shouldn't be.

So, would you like to accept the generic solution like below:

- Create a generic power sequence driver, and it will be probed according to compatible string at device tree. At its probe, we can create a power sequence structure, and let this structure as the private data for this power sequence device.

struct pwrseq

{
	(*on) (struct pwrseq *);
	(*off) (struct pwrseq *);
	...
};

At power sequence driver ->probe

{
	pwrseq = devm_kzalloc(dev, sizeof(*pwrseq), GFP_KERNEL);
	pwrseq->on = generic_pwrseq_on(pwrseq);
	pwrseq->on = generic_pwrseq_off(pwrseq);
	dev_set_drvdata(dev, pwrseq);
}

- At device driver which need to use power sequence operation
- Get the node according to phandle, and get its private data accordingly. The driver can assign its pwrseq to its structure or not.
- The driver can call its pwrseq->on and pwrseq->off accordingly.

Signature · 2 lines
Best Regards,
Peter Chen
#10 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

Quoted 6 lines

So, would you like to accept the generic solution like below:

- Create a generic power sequence driver, and it will be probed according to compatible string at device tree. At its probe, we can create a power sequence structure, and let this structure as the private data for this power sequence device.

I'm not sure a separate driver is required. Why not consider it more like pinctrl properties? They are listed in the devices node. Have the bus enumerate code first walk all children and run their on sequence. Bus shutdown would again walk the children and run the off sequence.

Andrew

#11 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

Quoted 7 lines · Andrew Lunn
Quoted 6 lines

So, would you like to accept the generic solution like below:

- Create a generic power sequence driver, and it will be probed according to compatible string at device tree. At its probe, we can create a power sequence structure, and let this structure as the private data for this power sequence device.

Quoted 5 lines

I'm not sure a separate driver is required. Why not consider it more like pinctrl properties? They are listed in the devices node. Have the bus enumerate code first walk all children and run their on sequence. Bus shutdown would again walk the children and run the off sequence.

The device which needs power sequence may not at platform bus, it may be at USB bus, MMC bus, etc.

Quoted 1 line

From what I see, A generic pwrseq driver can cover gpio-en, gpio-rst, clock,

clock-freq, etc, but I find the pwrseq_emmc does something special, like request a restart handler.

Add Ulf, who is the power sequence author for MMC.

Hi Ulf, Rob suggested that if we can create some generic things (driver or library) for power sequence for all devices, what do you think?

Signature · 2 lines
Best Regards,
Peter Chen
#12 lore raw

PATCH 1/3 usb: core: add power sequence for USB devices

Quoted 23 lines · Peter Chen
Quoted 7 lines · Andrew Lunn
Quoted 6 lines

So, would you like to accept the generic solution like below:

- Create a generic power sequence driver, and it will be probed according to compatible string at device tree. At its probe, we can create a power sequence structure, and let this structure as the private data for this power sequence device.

Quoted 5 lines

I'm not sure a separate driver is required. Why not consider it more like pinctrl properties? They are listed in the devices node. Have the bus enumerate code first walk all children and run their on sequence. Bus shutdown would again walk the children and run the off sequence.

The device which needs power sequence may not at platform bus, it may be at USB bus, MMC bus, etc.

From what I see, A generic pwrseq driver can cover gpio-en, gpio-rst, clock, clock-freq, etc, but I find the pwrseq_emmc does something special, like request a restart handler.

Add Ulf, who is the power sequence author for MMC.

Quoted 7 lines

Hi Ulf, Rob suggested that if we can create some generic things (driver or library) for power sequence for all devices, what do you think?

--

Best Regards,
Peter Chen

ping....

Any more comments? If no, I will try to create some generic stuffs for power sequence.

Signature · 2 lines
BR,
Peter Chen