diff mbox series

[usb-next,v10,3/8] usb: core: add a wrapper for the USB PHYs on the HCD

Message ID 20180218184504.3331-4-martin.blumenstingl@googlemail.com
State Superseded
Headers show
Series initialize (multiple) PHYs for a HCD | expand

Commit Message

Martin Blumenstingl Feb. 18, 2018, 6:44 p.m. UTC
Many SoC platforms have separate devices for the USB PHY which are
registered through the generic PHY framework. These PHYs have to be
enabled to make the USB controller actually work. They also have to be
disabled again on shutdown/suspend.

Currently (at least) the following HCI platform drivers are using custom
code to obtain all PHYs via devicetree for the roothub/controller and
disable/enable them when required:
- ehci-platform.c has ehci_platform_power_{on,off}
- xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
- ohci-platform.c has ohci_platform_power_{on,off}

With this new wrapper the USB PHYs can be specified directly in the
USB controller's devicetree node (just like on the drivers listed
above). This allows SoCs like the Amlogic Meson GXL family to operate
correctly once this is wired up correctly. These SoCs use a dwc3
controller and require all USB PHYs to be initialized (if one of the USB
PHYs it not initialized then none of USB port works at all).

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Tested-by: Yixun Lan <yixun.lan@amlogic.com>
Cc: Neil Armstrong <narmstrong@baylibre.com>
Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/usb/core/Makefile |   2 +-
 drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/usb/core/phy.h    |   7 ++
 3 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 drivers/usb/core/phy.c
 create mode 100644 drivers/usb/core/phy.h

Comments

Roger Quadros March 16, 2018, 2:32 p.m. UTC | #1
+some TI folks

Hi Martin,

On 18/02/18 20:44, Martin Blumenstingl wrote:
> Many SoC platforms have separate devices for the USB PHY which are
> registered through the generic PHY framework. These PHYs have to be
> enabled to make the USB controller actually work. They also have to be
> disabled again on shutdown/suspend.
> 
> Currently (at least) the following HCI platform drivers are using custom
> code to obtain all PHYs via devicetree for the roothub/controller and
> disable/enable them when required:
> - ehci-platform.c has ehci_platform_power_{on,off}
> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> - ohci-platform.c has ohci_platform_power_{on,off}
> 
> With this new wrapper the USB PHYs can be specified directly in the
> USB controller's devicetree node (just like on the drivers listed
> above). This allows SoCs like the Amlogic Meson GXL family to operate
> correctly once this is wired up correctly. These SoCs use a dwc3
> controller and require all USB PHYs to be initialized (if one of the USB
> PHYs it not initialized then none of USB port works at all).
> 
> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> Cc: Neil Armstrong <narmstrong@baylibre.com>
> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>

This patch is breaking low power cases on TI SoCs when USB is in host mode.
I'll explain why below.

> ---
>  drivers/usb/core/Makefile |   2 +-
>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>  drivers/usb/core/phy.h    |   7 ++
>  3 files changed, 166 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/usb/core/phy.c
>  create mode 100644 drivers/usb/core/phy.h
> 
> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> index 92c9cefb4317..18e874b0441e 100644
> --- a/drivers/usb/core/Makefile
> +++ b/drivers/usb/core/Makefile
> @@ -6,7 +6,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
> +usbcore-y += phy.o port.o
>  
>  usbcore-$(CONFIG_OF)		+= of.o
>  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> new file mode 100644
> index 000000000000..09b7c43c0ea4
> --- /dev/null
> +++ b/drivers/usb/core/phy.c
> @@ -0,0 +1,158 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> + * multiple (actual) PHY devices. This is comes handy when initializing
> + * all PHYs on a HCD and to keep them all in the same state.
> + *
> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> + */
> +
> +#include <linux/device.h>
> +#include <linux/list.h>
> +#include <linux/phy/phy.h>
> +#include <linux/of.h>
> +
> +#include "phy.h"
> +
> +struct usb_phy_roothub {
> +	struct phy		*phy;
> +	struct list_head	list;
> +};
> +
> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return ERR_PTR(-ENOMEM);
> +
> +	INIT_LIST_HEAD(&roothub_entry->list);
> +
> +	return roothub_entry;
> +}
> +
> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> +				   struct list_head *list)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> +
> +	if (IS_ERR_OR_NULL(phy)) {
> +		if (!phy || PTR_ERR(phy) == -ENODEV)
> +			return 0;
> +		else
> +			return PTR_ERR(phy);
> +	}
> +
> +	roothub_entry = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(roothub_entry))
> +		return PTR_ERR(roothub_entry);
> +
> +	roothub_entry->phy = phy;
> +
> +	list_add_tail(&roothub_entry->list, list);
> +
> +	return 0;
> +}
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> +{
> +	struct usb_phy_roothub *phy_roothub;
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int i, num_phys, err;
> +
> +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> +					      "#phy-cells");
> +	if (num_phys <= 0)
> +		return NULL;
> +
> +	phy_roothub = usb_phy_roothub_alloc(dev);
> +	if (IS_ERR(phy_roothub))
> +		return phy_roothub;
> +
> +	for (i = 0; i < num_phys; i++) {
> +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_init(roothub_entry->phy);

The phy_init() function actually enables the PHY clocks.
It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().

> +		if (err)
> +			goto err_exit_phys;
> +	}
> +
> +	return phy_roothub;
> +
> +err_exit_phys:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_exit(roothub_entry->phy);
> +
> +err_out:
> +	return ERR_PTR(err);
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> +
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err, ret = 0;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_exit(roothub_entry->phy);
> +		if (err)
> +			ret = ret;
> +	}

phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().

With that there is nothing else being done here. Shouldn't we be doing the equivalent of
usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?

> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +	struct list_head *head;
> +	int err;
> +
> +	if (!phy_roothub)
> +		return 0;
> +
> +	head = &phy_roothub->list;
> +
> +	list_for_each_entry(roothub_entry, head, list) {
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err)
> +			goto err_out;
> +	}
> +
> +	return 0;
> +
> +err_out:
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +		phy_power_off(roothub_entry->phy);
> +
> +	return err;
> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> +
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> +{
> +	struct usb_phy_roothub *roothub_entry;
> +
> +	if (!phy_roothub)
> +		return;
> +
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +		phy_power_off(roothub_entry->phy);

Not doing the phy_exit() here leaves the clocks enabled on our SoC and
we're no longer able to reach low power states on system suspend.

> +}
> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> new file mode 100644
> index 000000000000..6fde59bfbff8
> --- /dev/null
> +++ b/drivers/usb/core/phy.h
> @@ -0,0 +1,7 @@
> +struct usb_phy_roothub;
> +
> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> +
> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> 

The following patch fixes the issue for me. Let me know what you think and I can post it officially.

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 09b7c43..23232d3 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 {
 	struct usb_phy_roothub *phy_roothub;
-	struct usb_phy_roothub *roothub_entry;
-	struct list_head *head;
 	int i, num_phys, err;
 
 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
@@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 	for (i = 0; i < num_phys; i++) {
 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
 		if (err)
-			goto err_out;
-	}
-
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_init(roothub_entry->phy);
-		if (err)
-			goto err_exit_phys;
+			return ERR_PTR(err);
 	}
 
 	return phy_roothub;
-
-err_exit_phys:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
-		phy_exit(roothub_entry->phy);
-
-err_out:
-	return ERR_PTR(err);
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
 
@@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return 0;
 
-	head = &phy_roothub->list;
-
-	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_exit(roothub_entry->phy);
-		if (err)
-			ret = ret;
-	}
+	/* TODO: usb_phy_roothub_del_phy */
+	/* TODO: usb_phy_roothub_free */
 
 	return ret;
 }
@@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
 	head = &phy_roothub->list;
 
 	list_for_each_entry(roothub_entry, head, list) {
-		err = phy_power_on(roothub_entry->phy);
+		err = phy_init(roothub_entry->phy);
 		if (err)
 			goto err_out;
+		err = phy_power_on(roothub_entry->phy);
+		if (err) {
+			phy_exit(roothub_entry->phy);
+			goto err_out;
+		}
 	}
 
 	return 0;
 
 err_out:
-	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 
 	return err;
 }
@@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
 	if (!phy_roothub)
 		return;
 
-	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
 		phy_power_off(roothub_entry->phy);
+		phy_exit(roothub_entry->phy);
+	}
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
Martin Blumenstingl March 18, 2018, 10:29 p.m. UTC | #2
Hi Roger,

On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> +some TI folks
>
> Hi Martin,
>
> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> Many SoC platforms have separate devices for the USB PHY which are
>> registered through the generic PHY framework. These PHYs have to be
>> enabled to make the USB controller actually work. They also have to be
>> disabled again on shutdown/suspend.
>>
>> Currently (at least) the following HCI platform drivers are using custom
>> code to obtain all PHYs via devicetree for the roothub/controller and
>> disable/enable them when required:
>> - ehci-platform.c has ehci_platform_power_{on,off}
>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> - ohci-platform.c has ohci_platform_power_{on,off}
>>
>> With this new wrapper the USB PHYs can be specified directly in the
>> USB controller's devicetree node (just like on the drivers listed
>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> correctly once this is wired up correctly. These SoCs use a dwc3
>> controller and require all USB PHYs to be initialized (if one of the USB
>> PHYs it not initialized then none of USB port works at all).
>>
>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
based on your explanation and reading the TI PHY drivers I am assuming
that the affected SoCs are using the "phy-omap-usb2" driver

>> ---
>>  drivers/usb/core/Makefile |   2 +-
>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>  drivers/usb/core/phy.h    |   7 ++
>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/usb/core/phy.c
>>  create mode 100644 drivers/usb/core/phy.h
>>
>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> index 92c9cefb4317..18e874b0441e 100644
>> --- a/drivers/usb/core/Makefile
>> +++ b/drivers/usb/core/Makefile
>> @@ -6,7 +6,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
>> +usbcore-y += phy.o port.o
>>
>>  usbcore-$(CONFIG_OF)         += of.o
>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> new file mode 100644
>> index 000000000000..09b7c43c0ea4
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.c
>> @@ -0,0 +1,158 @@
>> +// SPDX-License-Identifier: GPL-2.0+
>> +/*
>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> + * all PHYs on a HCD and to keep them all in the same state.
>> + *
>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> + */
>> +
>> +#include <linux/device.h>
>> +#include <linux/list.h>
>> +#include <linux/phy/phy.h>
>> +#include <linux/of.h>
>> +
>> +#include "phy.h"
>> +
>> +struct usb_phy_roothub {
>> +     struct phy              *phy;
>> +     struct list_head        list;
>> +};
>> +
>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +     if (!roothub_entry)
>> +             return ERR_PTR(-ENOMEM);
>> +
>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> +
>> +     return roothub_entry;
>> +}
>> +
>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> +                                struct list_head *list)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> +
>> +     if (IS_ERR_OR_NULL(phy)) {
>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> +                     return 0;
>> +             else
>> +                     return PTR_ERR(phy);
>> +     }
>> +
>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(roothub_entry))
>> +             return PTR_ERR(roothub_entry);
>> +
>> +     roothub_entry->phy = phy;
>> +
>> +     list_add_tail(&roothub_entry->list, list);
>> +
>> +     return 0;
>> +}
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> +{
>> +     struct usb_phy_roothub *phy_roothub;
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int i, num_phys, err;
>> +
>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> +                                           "#phy-cells");
>> +     if (num_phys <= 0)
>> +             return NULL;
>> +
>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> +     if (IS_ERR(phy_roothub))
>> +             return phy_roothub;
>> +
>> +     for (i = 0; i < num_phys; i++) {
>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_init(roothub_entry->phy);
>
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
do you mean that phy_init should be moved to usb_phy_roothub_power_on
(just before phy_power_on is called within usb_phy_roothub_power_on)?

an earlier version of my patch did exactly this, but it caused
problems during a suspend/resume cycle on Mediatek devices
Chunfeng Yun reported that issue here [0], quote from that mail for
easier reading:
"In order to keep link state on mt8173, we just power off all phys(not
exit) when system enter suspend, then power on them again (needn't
init, otherwise device will be disconnected) when system resume, this
can avoid re-enumerating device."

>> +             if (err)
>> +                     goto err_exit_phys;
>> +     }
>> +
>> +     return phy_roothub;
>> +
>> +err_exit_phys:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_exit(roothub_entry->phy);
>> +
>> +err_out:
>> +     return ERR_PTR(err);
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> +
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err, ret = 0;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_exit(roothub_entry->phy);
>> +             if (err)
>> +                     ret = ret;
>> +     }
>
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
if I understood Chunfeng Yun correctly this will require
re-enumeration of the USB devices after a suspend/resume cycle on
Mediatek SoCs

> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>
>> +
>> +     return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +     struct list_head *head;
>> +     int err;
>> +
>> +     if (!phy_roothub)
>> +             return 0;
>> +
>> +     head = &phy_roothub->list;
>> +
>> +     list_for_each_entry(roothub_entry, head, list) {
>> +             err = phy_power_on(roothub_entry->phy);
>> +             if (err)
>> +                     goto err_out;
>> +     }
>> +
>> +     return 0;
>> +
>> +err_out:
>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> +             phy_power_off(roothub_entry->phy);
>> +
>> +     return err;
>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> +
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> +{
>> +     struct usb_phy_roothub *roothub_entry;
>> +
>> +     if (!phy_roothub)
>> +             return;
>> +
>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> +             phy_power_off(roothub_entry->phy);
>
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
I'm not sure where this problem should be solved:
- set skip_phy_initialization in struct usb_hcd to 1 for the affected
TI platforms
- fix this in the usb_phy_roothub code
- fix this in the PHY driver
- somewhere else

>> +}
>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> new file mode 100644
>> index 000000000000..6fde59bfbff8
>> --- /dev/null
>> +++ b/drivers/usb/core/phy.h
>> @@ -0,0 +1,7 @@
>> +struct usb_phy_roothub;
>> +
>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> +
>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>
>
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
Chunfeng: could you please test whether this patch breaks the
suspend/resume cycle on your Mediatek platforms? see [0] where I used
a similar approach with a much older version of the "initialize
multiple PHYs per HCD" patch

> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>         struct usb_phy_roothub *phy_roothub;
> -       struct usb_phy_roothub *roothub_entry;
> -       struct list_head *head;
>         int i, num_phys, err;
>
>         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>         for (i = 0; i < num_phys; i++) {
>                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>                 if (err)
> -                       goto err_out;
> -       }
> -
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_init(roothub_entry->phy);
> -               if (err)
> -                       goto err_exit_phys;
> +                       return ERR_PTR(err);
>         }
>
>         return phy_roothub;
> -
> -err_exit_phys:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -               phy_exit(roothub_entry->phy);
> -
> -err_out:
> -       return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return 0;
>
> -       head = &phy_roothub->list;
> -
> -       list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_exit(roothub_entry->phy);
> -               if (err)
> -                       ret = ret;
> -       }
> +       /* TODO: usb_phy_roothub_del_phy */
> +       /* TODO: usb_phy_roothub_free */
>
>         return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>         head = &phy_roothub->list;
>
>         list_for_each_entry(roothub_entry, head, list) {
> -               err = phy_power_on(roothub_entry->phy);
> +               err = phy_init(roothub_entry->phy);
>                 if (err)
>                         goto err_out;
> +               err = phy_power_on(roothub_entry->phy);
> +               if (err) {
> +                       phy_exit(roothub_entry->phy);
> +                       goto err_out;
> +               }
>         }
>
>         return 0;
>
>  err_out:
> -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>
>         return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>         if (!phy_roothub)
>                 return;
>
> -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>                 phy_power_off(roothub_entry->phy);
> +               phy_exit(roothub_entry->phy);
> +       }
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin


[0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html
--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Chunfeng Yun (云春峰) March 19, 2018, 5:43 a.m. UTC | #3
On Fri, 2018-03-16 at 16:32 +0200, Roger Quadros wrote:
> +some TI folks
> 
> Hi Martin,
> 
> On 18/02/18 20:44, Martin Blumenstingl wrote:
> > Many SoC platforms have separate devices for the USB PHY which are
> > registered through the generic PHY framework. These PHYs have to be
> > enabled to make the USB controller actually work. They also have to be
> > disabled again on shutdown/suspend.
> > 
> > Currently (at least) the following HCI platform drivers are using custom
> > code to obtain all PHYs via devicetree for the roothub/controller and
> > disable/enable them when required:
> > - ehci-platform.c has ehci_platform_power_{on,off}
> > - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> > - ohci-platform.c has ohci_platform_power_{on,off}
> > 
> > With this new wrapper the USB PHYs can be specified directly in the
> > USB controller's devicetree node (just like on the drivers listed
> > above). This allows SoCs like the Amlogic Meson GXL family to operate
> > correctly once this is wired up correctly. These SoCs use a dwc3
> > controller and require all USB PHYs to be initialized (if one of the USB
> > PHYs it not initialized then none of USB port works at all).
> > 
> > Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> > Cc: Neil Armstrong <narmstrong@baylibre.com>
> > Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> 
> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> I'll explain why below.
> 
> > ---
> >  drivers/usb/core/Makefile |   2 +-
> >  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >  drivers/usb/core/phy.h    |   7 ++
> >  3 files changed, 166 insertions(+), 1 deletion(-)
> >  create mode 100644 drivers/usb/core/phy.c
> >  create mode 100644 drivers/usb/core/phy.h
> > 
> > diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> > index 92c9cefb4317..18e874b0441e 100644
> > --- a/drivers/usb/core/Makefile
> > +++ b/drivers/usb/core/Makefile
> > @@ -6,7 +6,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
> > +usbcore-y += phy.o port.o
> >  
> >  usbcore-$(CONFIG_OF)		+= of.o
> >  usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > new file mode 100644
> > index 000000000000..09b7c43c0ea4
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.c
> > @@ -0,0 +1,158 @@
> > +// SPDX-License-Identifier: GPL-2.0+
> > +/*
> > + * A wrapper for multiple PHYs which passes all phy_* function calls to
> > + * multiple (actual) PHY devices. This is comes handy when initializing
> > + * all PHYs on a HCD and to keep them all in the same state.
> > + *
> > + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> > + */
> > +
> > +#include <linux/device.h>
> > +#include <linux/list.h>
> > +#include <linux/phy/phy.h>
> > +#include <linux/of.h>
> > +
> > +#include "phy.h"
> > +
> > +struct usb_phy_roothub {
> > +	struct phy		*phy;
> > +	struct list_head	list;
> > +};
> > +
> > +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> > +	if (!roothub_entry)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	INIT_LIST_HEAD(&roothub_entry->list);
> > +
> > +	return roothub_entry;
> > +}
> > +
> > +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> > +				   struct list_head *list)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> > +
> > +	if (IS_ERR_OR_NULL(phy)) {
> > +		if (!phy || PTR_ERR(phy) == -ENODEV)
> > +			return 0;
> > +		else
> > +			return PTR_ERR(phy);
> > +	}
> > +
> > +	roothub_entry = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(roothub_entry))
> > +		return PTR_ERR(roothub_entry);
> > +
> > +	roothub_entry->phy = phy;
> > +
> > +	list_add_tail(&roothub_entry->list, list);
> > +
> > +	return 0;
> > +}
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> > +{
> > +	struct usb_phy_roothub *phy_roothub;
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int i, num_phys, err;
> > +
> > +	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > +					      "#phy-cells");
> > +	if (num_phys <= 0)
> > +		return NULL;
> > +
> > +	phy_roothub = usb_phy_roothub_alloc(dev);
> > +	if (IS_ERR(phy_roothub))
> > +		return phy_roothub;
> > +
> > +	for (i = 0; i < num_phys; i++) {
> > +		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_init(roothub_entry->phy);
> 
> The phy_init() function actually enables the PHY clocks.
> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> 
> > +		if (err)
> > +			goto err_exit_phys;
> > +	}
> > +
> > +	return phy_roothub;
> > +
> > +err_exit_phys:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_exit(roothub_entry->phy);
> > +
> > +err_out:
> > +	return ERR_PTR(err);
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> > +
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err, ret = 0;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_exit(roothub_entry->phy);
> > +		if (err)
> > +			ret = ret;
> > +	}
> 
> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> 
> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> 
> > +
> > +	return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +	struct list_head *head;
> > +	int err;
> > +
> > +	if (!phy_roothub)
> > +		return 0;
> > +
> > +	head = &phy_roothub->list;
> > +
> > +	list_for_each_entry(roothub_entry, head, list) {
> > +		err = phy_power_on(roothub_entry->phy);
> > +		if (err)
> > +			goto err_out;
> > +	}
> > +
> > +	return 0;
> > +
> > +err_out:
> > +	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +		phy_power_off(roothub_entry->phy);
> > +
> > +	return err;
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> > +
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> > +{
> > +	struct usb_phy_roothub *roothub_entry;
> > +
> > +	if (!phy_roothub)
> > +		return;
> > +
> > +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +		phy_power_off(roothub_entry->phy);
> 
> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> we're no longer able to reach low power states on system suspend.
Maybe you can modify phy driver if low power state is only blocked by
enabled clocks, try to disable clocks in phy_power_off, and enable them
again in phy_power_on()
> 
> > +}
> > +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> > diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> > new file mode 100644
> > index 000000000000..6fde59bfbff8
> > --- /dev/null
> > +++ b/drivers/usb/core/phy.h
> > @@ -0,0 +1,7 @@
> > +struct usb_phy_roothub;
> > +
> > +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> > +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> > +
> > +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> > +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> > 
> 
> The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 09b7c43..23232d3 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  {
>  	struct usb_phy_roothub *phy_roothub;
> -	struct usb_phy_roothub *roothub_entry;
> -	struct list_head *head;
>  	int i, num_phys, err;
>  
>  	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>  	for (i = 0; i < num_phys; i++) {
>  		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>  		if (err)
> -			goto err_out;
> -	}
> -
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_init(roothub_entry->phy);
> -		if (err)
> -			goto err_exit_phys;
> +			return ERR_PTR(err);
>  	}
>  
>  	return phy_roothub;
> -
> -err_exit_phys:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> -		phy_exit(roothub_entry->phy);
> -
> -err_out:
> -	return ERR_PTR(err);
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>  
> @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return 0;
>  
> -	head = &phy_roothub->list;
> -
> -	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_exit(roothub_entry->phy);
> -		if (err)
> -			ret = ret;
> -	}
> +	/* TODO: usb_phy_roothub_del_phy */
> +	/* TODO: usb_phy_roothub_free */
>  
>  	return ret;
>  }
> @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>  	head = &phy_roothub->list;
>  
>  	list_for_each_entry(roothub_entry, head, list) {
> -		err = phy_power_on(roothub_entry->phy);
> +		err = phy_init(roothub_entry->phy);
>  		if (err)
>  			goto err_out;
> +		err = phy_power_on(roothub_entry->phy);
> +		if (err) {
> +			phy_exit(roothub_entry->phy);
> +			goto err_out;
> +		}
>  	}
>  
>  	return 0;
>  
>  err_out:
> -	list_for_each_entry_continue_reverse(roothub_entry, head, list)
> +	list_for_each_entry_continue_reverse(roothub_entry, head, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  
>  	return err;
>  }
> @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>  	if (!phy_roothub)
>  		return;
>  
> -	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> +	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
>  		phy_power_off(roothub_entry->phy);
> +		phy_exit(roothub_entry->phy);
> +	}
>  }
>  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> 
> 


--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Chunfeng Yun (云春峰) March 19, 2018, 6:02 a.m. UTC | #4
Hi,
On Sun, 2018-03-18 at 23:29 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> > +some TI folks
> >
> > Hi Martin,
> >
> > On 18/02/18 20:44, Martin Blumenstingl wrote:
> >> Many SoC platforms have separate devices for the USB PHY which are
> >> registered through the generic PHY framework. These PHYs have to be
> >> enabled to make the USB controller actually work. They also have to be
> >> disabled again on shutdown/suspend.
> >>
> >> Currently (at least) the following HCI platform drivers are using custom
> >> code to obtain all PHYs via devicetree for the roothub/controller and
> >> disable/enable them when required:
> >> - ehci-platform.c has ehci_platform_power_{on,off}
> >> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >> - ohci-platform.c has ohci_platform_power_{on,off}
> >>
> >> With this new wrapper the USB PHYs can be specified directly in the
> >> USB controller's devicetree node (just like on the drivers listed
> >> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >> correctly once this is wired up correctly. These SoCs use a dwc3
> >> controller and require all USB PHYs to be initialized (if one of the USB
> >> PHYs it not initialized then none of USB port works at all).
> >>
> >> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >
> > This patch is breaking low power cases on TI SoCs when USB is in host mode.
> > I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
[...]
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >> +{
> >> +     struct usb_phy_roothub *phy_roothub;
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int i, num_phys, err;
> >> +
> >> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >> +                                           "#phy-cells");
> >> +     if (num_phys <= 0)
> >> +             return NULL;
> >> +
> >> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >> +     if (IS_ERR(phy_roothub))
> >> +             return phy_roothub;
> >> +
> >> +     for (i = 0; i < num_phys; i++) {
> >> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_init(roothub_entry->phy);
> >
> > The phy_init() function actually enables the PHY clocks.
> > It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 
> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
> >> +             if (err)
> >> +                     goto err_exit_phys;
> >> +     }
> >> +
> >> +     return phy_roothub;
> >> +
> >> +err_exit_phys:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_exit(roothub_entry->phy);
> >> +
> >> +err_out:
> >> +     return ERR_PTR(err);
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >> +
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err, ret = 0;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_exit(roothub_entry->phy);
> >> +             if (err)
> >> +                     ret = ret;
> >> +     }
> >
> > phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
You are right
> 
> > With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> > usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >
> >> +
> >> +     return ret;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +     struct list_head *head;
> >> +     int err;
> >> +
> >> +     if (!phy_roothub)
> >> +             return 0;
> >> +
> >> +     head = &phy_roothub->list;
> >> +
> >> +     list_for_each_entry(roothub_entry, head, list) {
> >> +             err = phy_power_on(roothub_entry->phy);
> >> +             if (err)
> >> +                     goto err_out;
> >> +     }
> >> +
> >> +     return 0;
> >> +
> >> +err_out:
> >> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >> +             phy_power_off(roothub_entry->phy);
> >> +
> >> +     return err;
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >> +
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >> +{
> >> +     struct usb_phy_roothub *roothub_entry;
> >> +
> >> +     if (!phy_roothub)
> >> +             return;
> >> +
> >> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >> +             phy_power_off(roothub_entry->phy);
> >
> > Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> > we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms
> - fix this in the usb_phy_roothub code
> - fix this in the PHY driver
> - somewhere else
> 
> >> +}
> >> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >> new file mode 100644
> >> index 000000000000..6fde59bfbff8
> >> --- /dev/null
> >> +++ b/drivers/usb/core/phy.h
> >> @@ -0,0 +1,7 @@
> >> +struct usb_phy_roothub;
> >> +
> >> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >> +
> >> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>
> >
> > The following patch fixes the issue for me. Let me know what you think and I can post it officially.
> Chunfeng: could you please test whether this patch breaks the
> suspend/resume cycle on your Mediatek platforms? see [0] where I used
> a similar approach with a much older version of the "initialize
> multiple PHYs per HCD" patch
If add Roger's one, it do disconnect plugged devices on MTK platforms,
due to re-initialize u2 phys when resume,as you mentioned above
> 
> > diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> > index 09b7c43..23232d3 100644
> > --- a/drivers/usb/core/phy.c
> > +++ b/drivers/usb/core/phy.c
> > @@ -59,8 +59,6 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >  struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >  {
> >         struct usb_phy_roothub *phy_roothub;
> > -       struct usb_phy_roothub *roothub_entry;
> > -       struct list_head *head;
> >         int i, num_phys, err;
> >
> >         num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> > @@ -75,25 +73,10 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >         for (i = 0; i < num_phys; i++) {
> >                 err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >                 if (err)
> > -                       goto err_out;
> > -       }
> > -
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_init(roothub_entry->phy);
> > -               if (err)
> > -                       goto err_exit_phys;
> > +                       return ERR_PTR(err);
> >         }
> >
> >         return phy_roothub;
> > -
> > -err_exit_phys:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > -               phy_exit(roothub_entry->phy);
> > -
> > -err_out:
> > -       return ERR_PTR(err);
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >
> > @@ -106,13 +89,8 @@ int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return 0;
> >
> > -       head = &phy_roothub->list;
> > -
> > -       list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_exit(roothub_entry->phy);
> > -               if (err)
> > -                       ret = ret;
> > -       }
> > +       /* TODO: usb_phy_roothub_del_phy */
> > +       /* TODO: usb_phy_roothub_free */
> >
> >         return ret;
> >  }
> > @@ -130,16 +108,23 @@ int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >         head = &phy_roothub->list;
> >
> >         list_for_each_entry(roothub_entry, head, list) {
> > -               err = phy_power_on(roothub_entry->phy);
> > +               err = phy_init(roothub_entry->phy);
> >                 if (err)
> >                         goto err_out;
> > +               err = phy_power_on(roothub_entry->phy);
> > +               if (err) {
> > +                       phy_exit(roothub_entry->phy);
> > +                       goto err_out;
> > +               }
> >         }
> >
> >         return 0;
> >
> >  err_out:
> > -       list_for_each_entry_continue_reverse(roothub_entry, head, list)
> > +       list_for_each_entry_continue_reverse(roothub_entry, head, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >
> >         return err;
> >  }
> > @@ -152,7 +137,9 @@ void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >         if (!phy_roothub)
> >                 return;
> >
> > -       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> > +       list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list) {
> >                 phy_power_off(roothub_entry->phy);
> > +               phy_exit(roothub_entry->phy);
> > +       }
> >  }
> >  EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin
> 
> 
> [0] http://lists.infradead.org/pipermail/linux-amlogic/2017-July/004374.html


--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Roger Quadros March 19, 2018, 8:49 a.m. UTC | #5
Hi,

On 19/03/18 00:29, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> +some TI folks
>>
>> Hi Martin,
>>
>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>> Many SoC platforms have separate devices for the USB PHY which are
>>> registered through the generic PHY framework. These PHYs have to be
>>> enabled to make the USB controller actually work. They also have to be
>>> disabled again on shutdown/suspend.
>>>
>>> Currently (at least) the following HCI platform drivers are using custom
>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>> disable/enable them when required:
>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>
>>> With this new wrapper the USB PHYs can be specified directly in the
>>> USB controller's devicetree node (just like on the drivers listed
>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>> controller and require all USB PHYs to be initialized (if one of the USB
>>> PHYs it not initialized then none of USB port works at all).
>>>
>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>
>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> I'll explain why below.
> based on your explanation and reading the TI PHY drivers I am assuming
> that the affected SoCs are using the "phy-omap-usb2" driver
> 
yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"

>>> ---
>>>  drivers/usb/core/Makefile |   2 +-
>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>  drivers/usb/core/phy.h    |   7 ++
>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>  create mode 100644 drivers/usb/core/phy.c
>>>  create mode 100644 drivers/usb/core/phy.h
>>>
>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>> index 92c9cefb4317..18e874b0441e 100644
>>> --- a/drivers/usb/core/Makefile
>>> +++ b/drivers/usb/core/Makefile
>>> @@ -6,7 +6,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
>>> +usbcore-y += phy.o port.o
>>>
>>>  usbcore-$(CONFIG_OF)         += of.o
>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>> new file mode 100644
>>> index 000000000000..09b7c43c0ea4
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.c
>>> @@ -0,0 +1,158 @@
>>> +// SPDX-License-Identifier: GPL-2.0+
>>> +/*
>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>> + * all PHYs on a HCD and to keep them all in the same state.
>>> + *
>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>> + */
>>> +
>>> +#include <linux/device.h>
>>> +#include <linux/list.h>
>>> +#include <linux/phy/phy.h>
>>> +#include <linux/of.h>
>>> +
>>> +#include "phy.h"
>>> +
>>> +struct usb_phy_roothub {
>>> +     struct phy              *phy;
>>> +     struct list_head        list;
>>> +};
>>> +
>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>> +     if (!roothub_entry)
>>> +             return ERR_PTR(-ENOMEM);
>>> +
>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>> +
>>> +     return roothub_entry;
>>> +}
>>> +
>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>> +                                struct list_head *list)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>> +
>>> +     if (IS_ERR_OR_NULL(phy)) {
>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>> +                     return 0;
>>> +             else
>>> +                     return PTR_ERR(phy);
>>> +     }
>>> +
>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(roothub_entry))
>>> +             return PTR_ERR(roothub_entry);
>>> +
>>> +     roothub_entry->phy = phy;
>>> +
>>> +     list_add_tail(&roothub_entry->list, list);
>>> +
>>> +     return 0;
>>> +}
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>> +{
>>> +     struct usb_phy_roothub *phy_roothub;
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int i, num_phys, err;
>>> +
>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>> +                                           "#phy-cells");
>>> +     if (num_phys <= 0)
>>> +             return NULL;
>>> +
>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>> +     if (IS_ERR(phy_roothub))
>>> +             return phy_roothub;
>>> +
>>> +     for (i = 0; i < num_phys; i++) {
>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_init(roothub_entry->phy);
>>
>> The phy_init() function actually enables the PHY clocks.
>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> 

Yes.

> an earlier version of my patch did exactly this, but it caused
> problems during a suspend/resume cycle on Mediatek devices
> Chunfeng Yun reported that issue here [0], quote from that mail for
> easier reading:
> "In order to keep link state on mt8173, we just power off all phys(not
> exit) when system enter suspend, then power on them again (needn't
> init, otherwise device will be disconnected) when system resume, this
> can avoid re-enumerating device."
> 
>>> +             if (err)
>>> +                     goto err_exit_phys;
>>> +     }
>>> +
>>> +     return phy_roothub;
>>> +
>>> +err_exit_phys:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_exit(roothub_entry->phy);
>>> +
>>> +err_out:
>>> +     return ERR_PTR(err);
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>> +
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err, ret = 0;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_exit(roothub_entry->phy);
>>> +             if (err)
>>> +                     ret = ret;
>>> +     }
>>
>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> if I understood Chunfeng Yun correctly this will require
> re-enumeration of the USB devices after a suspend/resume cycle on
> Mediatek SoCs
> 

OK. I suppose that there are 2 cases
1) Mediatek's case: USB controller context retained across suspend/resume.
Remote wakeup probably required.
No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
during suspend/resume to keep PHY link active.

2) TI's case: low power important: USB context is lost, OK to re-enumerate.
phy_exit()/phy_init() must be called during suspend/resume.


>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>
>>> +
>>> +     return ret;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +     struct list_head *head;
>>> +     int err;
>>> +
>>> +     if (!phy_roothub)
>>> +             return 0;
>>> +
>>> +     head = &phy_roothub->list;
>>> +
>>> +     list_for_each_entry(roothub_entry, head, list) {
>>> +             err = phy_power_on(roothub_entry->phy);
>>> +             if (err)
>>> +                     goto err_out;
>>> +     }
>>> +
>>> +     return 0;
>>> +
>>> +err_out:
>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>> +             phy_power_off(roothub_entry->phy);
>>> +
>>> +     return err;
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>> +
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>> +{
>>> +     struct usb_phy_roothub *roothub_entry;
>>> +
>>> +     if (!phy_roothub)
>>> +             return;
>>> +
>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>> +             phy_power_off(roothub_entry->phy);
>>
>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> we're no longer able to reach low power states on system suspend.
> I'm not sure where this problem should be solved:
> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> TI platforms

Many TI platforms are affected, omap5*, dra7*, am43*

> - fix this in the usb_phy_roothub code

I'd vote for fixing it in the usb_phy_roothub code. How?
How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?

> - fix this in the PHY driver

There is nothing to fix in the PHY driver. It is doing what it is supposed to do.

> - somewhere else
> 
>>> +}
>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>> new file mode 100644
>>> index 000000000000..6fde59bfbff8
>>> --- /dev/null
>>> +++ b/drivers/usb/core/phy.h
>>> @@ -0,0 +1,7 @@
>>> +struct usb_phy_roothub;
>>> +
>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>> +
>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>
>>

<snip>
Martin Blumenstingl March 19, 2018, 4:12 p.m. UTC | #6
Hi Roger,

On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> Hi,
>
> On 19/03/18 00:29, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>> +some TI folks
>>>
>>> Hi Martin,
>>>
>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>> registered through the generic PHY framework. These PHYs have to be
>>>> enabled to make the USB controller actually work. They also have to be
>>>> disabled again on shutdown/suspend.
>>>>
>>>> Currently (at least) the following HCI platform drivers are using custom
>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>> disable/enable them when required:
>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>
>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>> USB controller's devicetree node (just like on the drivers listed
>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>> PHYs it not initialized then none of USB port works at all).
>>>>
>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>
>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>> I'll explain why below.
>> based on your explanation and reading the TI PHY drivers I am assuming
>> that the affected SoCs are using the "phy-omap-usb2" driver
>>
> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
I missed that, thanks

>>>> ---
>>>>  drivers/usb/core/Makefile |   2 +-
>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>
>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>> index 92c9cefb4317..18e874b0441e 100644
>>>> --- a/drivers/usb/core/Makefile
>>>> +++ b/drivers/usb/core/Makefile
>>>> @@ -6,7 +6,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
>>>> +usbcore-y += phy.o port.o
>>>>
>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>> new file mode 100644
>>>> index 000000000000..09b7c43c0ea4
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.c
>>>> @@ -0,0 +1,158 @@
>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>> +/*
>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>> + *
>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>> + */
>>>> +
>>>> +#include <linux/device.h>
>>>> +#include <linux/list.h>
>>>> +#include <linux/phy/phy.h>
>>>> +#include <linux/of.h>
>>>> +
>>>> +#include "phy.h"
>>>> +
>>>> +struct usb_phy_roothub {
>>>> +     struct phy              *phy;
>>>> +     struct list_head        list;
>>>> +};
>>>> +
>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>> +     if (!roothub_entry)
>>>> +             return ERR_PTR(-ENOMEM);
>>>> +
>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>> +
>>>> +     return roothub_entry;
>>>> +}
>>>> +
>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>> +                                struct list_head *list)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>> +
>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>> +                     return 0;
>>>> +             else
>>>> +                     return PTR_ERR(phy);
>>>> +     }
>>>> +
>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(roothub_entry))
>>>> +             return PTR_ERR(roothub_entry);
>>>> +
>>>> +     roothub_entry->phy = phy;
>>>> +
>>>> +     list_add_tail(&roothub_entry->list, list);
>>>> +
>>>> +     return 0;
>>>> +}
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>> +{
>>>> +     struct usb_phy_roothub *phy_roothub;
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int i, num_phys, err;
>>>> +
>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>> +                                           "#phy-cells");
>>>> +     if (num_phys <= 0)
>>>> +             return NULL;
>>>> +
>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>> +     if (IS_ERR(phy_roothub))
>>>> +             return phy_roothub;
>>>> +
>>>> +     for (i = 0; i < num_phys; i++) {
>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_init(roothub_entry->phy);
>>>
>>> The phy_init() function actually enables the PHY clocks.
>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>
>
> Yes.
>
>> an earlier version of my patch did exactly this, but it caused
>> problems during a suspend/resume cycle on Mediatek devices
>> Chunfeng Yun reported that issue here [0], quote from that mail for
>> easier reading:
>> "In order to keep link state on mt8173, we just power off all phys(not
>> exit) when system enter suspend, then power on them again (needn't
>> init, otherwise device will be disconnected) when system resume, this
>> can avoid re-enumerating device."
>>
>>>> +             if (err)
>>>> +                     goto err_exit_phys;
>>>> +     }
>>>> +
>>>> +     return phy_roothub;
>>>> +
>>>> +err_exit_phys:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_exit(roothub_entry->phy);
>>>> +
>>>> +err_out:
>>>> +     return ERR_PTR(err);
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>> +
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err, ret = 0;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_exit(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     ret = ret;
>>>> +     }
>>>
>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> if I understood Chunfeng Yun correctly this will require
>> re-enumeration of the USB devices after a suspend/resume cycle on
>> Mediatek SoCs
>>
>
> OK. I suppose that there are 2 cases
> 1) Mediatek's case: USB controller context retained across suspend/resume.
> Remote wakeup probably required.
> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> during suspend/resume to keep PHY link active.
Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
that the parent of the USB controller can be marked as "wakeup-source"

> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> phy_exit()/phy_init() must be called during suspend/resume.
ACK

>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>
>>>> +
>>>> +     return ret;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +     struct list_head *head;
>>>> +     int err;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return 0;
>>>> +
>>>> +     head = &phy_roothub->list;
>>>> +
>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>> +             err = phy_power_on(roothub_entry->phy);
>>>> +             if (err)
>>>> +                     goto err_out;
>>>> +     }
>>>> +
>>>> +     return 0;
>>>> +
>>>> +err_out:
>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>> +
>>>> +     return err;
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>> +
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>> +{
>>>> +     struct usb_phy_roothub *roothub_entry;
>>>> +
>>>> +     if (!phy_roothub)
>>>> +             return;
>>>> +
>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>> +             phy_power_off(roothub_entry->phy);
>>>
>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>> we're no longer able to reach low power states on system suspend.
>> I'm not sure where this problem should be solved:
>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> TI platforms
>
> Many TI platforms are affected, omap5*, dra7*, am43*
>
>> - fix this in the usb_phy_roothub code
>
> I'd vote for fixing it in the usb_phy_roothub code. How?
> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
@Chunfeng: can you confirm Roger's idea that we could call phy_exit if
the controller is *NOT* marked as "wakeup-source"?
I am also not sure if it would work, since the "wakeup-source"
property is defined on the USB controller's parent node in case of the
Mediatek MTU3 (Mediatek USB3.0 DRD) controller

>> - fix this in the PHY driver
>
> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
I actually wonder if phy_ops should have explicit suspend/resume support:
- assuming we define two new callbacks: .suspend and .resume
- the PHY framework could call .power_off by default if .suspend is not defined
- the PHY framework could call .power_on by default if .resume is not defined
- drivers could set .suspend and .resume on their own, allowing more
fine-grained control by for example *only* stopping the clock (but not
re-initializing the registers, etc.)

@Roger: what do you think about this?
Kishon (the PHY framework maintainer) is also CC'ed - I would like to
hear his opinion too

>> - somewhere else
>>
>>>> +}
>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>> new file mode 100644
>>>> index 000000000000..6fde59bfbff8
>>>> --- /dev/null
>>>> +++ b/drivers/usb/core/phy.h
>>>> @@ -0,0 +1,7 @@
>>>> +struct usb_phy_roothub;
>>>> +
>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>> +
>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>
>>>
>
> <snip>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin
--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Chunfeng Yun (云春峰) March 20, 2018, 7:54 a.m. UTC | #7
Hi Martin,

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,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
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
In fact, xhci-mtk driver always keeps link state across suspend/resume,
no matter "wakeup-source" is marked or not, it's just used to
enable/disable to send wakeup signal to SPM.

> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
I'm afraid it doesn't work.
If re-initialize phys when system resume, it will cause usb controller
to be re-initialized too, but the driver doesn't support it currently.

> 
> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin


--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Roger Quadros March 20, 2018, 10:55 a.m. UTC | #8
On 19/03/18 18:12, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,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
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller

That is about wakeup-source and not about whether remote wakeup should be enabled or not.
i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.

> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?

Looks fine to me. However one thing is still not clear. How does the PHY driver
decide if it needs to do a full power off or partial power off?
It doesn't know about the roothub's device_may_wakeup() status.

> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
>>> - somewhere else
>>>
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>> new file mode 100644
>>>>> index 000000000000..6fde59bfbff8
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.h
>>>>> @@ -0,0 +1,7 @@
>>>>> +struct usb_phy_roothub;
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>
>>>>
>>
Roger Quadros March 20, 2018, 11:02 a.m. UTC | #9
On 20/03/18 12:55, Roger Quadros wrote:
> On 19/03/18 18:12, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,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
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
> That is about wakeup-source and not about whether remote wakeup should be enabled or not.

correction. It indeed means enable/disable wakeup :).

> i.e. wakeup-source can be present but wakeup can still be disabled by the user via sysfs.
> 
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
> 
> Looks fine to me. However one thing is still not clear. How does the PHY driver
> decide if it needs to do a full power off or partial power off?
> It doesn't know about the roothub's device_may_wakeup() status.
> 
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>>>> - somewhere else
>>>>
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>> new file mode 100644
>>>>>> index 000000000000..6fde59bfbff8
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>> @@ -0,0 +1,7 @@
>>>>>> +struct usb_phy_roothub;
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>
>>>>>
>>>
>
Kishon Vijay Abraham I March 20, 2018, 11:27 a.m. UTC | #10
Hi,

On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> Hi,
>>
>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>> +some TI folks
>>>>
>>>> Hi Martin,
>>>>
>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>> enabled to make the USB controller actually work. They also have to be
>>>>> disabled again on shutdown/suspend.
>>>>>
>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>> disable/enable them when required:
>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>
>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>
>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>
>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>> I'll explain why below.
>>> based on your explanation and reading the TI PHY drivers I am assuming
>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>
>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
>>>>> ---
>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>
>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>> --- a/drivers/usb/core/Makefile
>>>>> +++ b/drivers/usb/core/Makefile
>>>>> @@ -6,7 +6,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
>>>>> +usbcore-y += phy.o port.o
>>>>>
>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>> new file mode 100644
>>>>> index 000000000000..09b7c43c0ea4
>>>>> --- /dev/null
>>>>> +++ b/drivers/usb/core/phy.c
>>>>> @@ -0,0 +1,158 @@
>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>> +/*
>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>> + *
>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>> + */
>>>>> +
>>>>> +#include <linux/device.h>
>>>>> +#include <linux/list.h>
>>>>> +#include <linux/phy/phy.h>
>>>>> +#include <linux/of.h>
>>>>> +
>>>>> +#include "phy.h"
>>>>> +
>>>>> +struct usb_phy_roothub {
>>>>> +     struct phy              *phy;
>>>>> +     struct list_head        list;
>>>>> +};
>>>>> +
>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>> +     if (!roothub_entry)
>>>>> +             return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>> +
>>>>> +     return roothub_entry;
>>>>> +}
>>>>> +
>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>> +                                struct list_head *list)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>> +
>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>> +                     return 0;
>>>>> +             else
>>>>> +                     return PTR_ERR(phy);
>>>>> +     }
>>>>> +
>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(roothub_entry))
>>>>> +             return PTR_ERR(roothub_entry);
>>>>> +
>>>>> +     roothub_entry->phy = phy;
>>>>> +
>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>> +
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>> +{
>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int i, num_phys, err;
>>>>> +
>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>> +                                           "#phy-cells");
>>>>> +     if (num_phys <= 0)
>>>>> +             return NULL;
>>>>> +
>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>> +     if (IS_ERR(phy_roothub))
>>>>> +             return phy_roothub;
>>>>> +
>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_init(roothub_entry->phy);
>>>>
>>>> The phy_init() function actually enables the PHY clocks.
>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>
>>
>> Yes.
>>
>>> an earlier version of my patch did exactly this, but it caused
>>> problems during a suspend/resume cycle on Mediatek devices
>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>> easier reading:
>>> "In order to keep link state on mt8173, we just power off all phys(not
>>> exit) when system enter suspend, then power on them again (needn't
>>> init, otherwise device will be disconnected) when system resume, this
>>> can avoid re-enumerating device."
>>>
>>>>> +             if (err)
>>>>> +                     goto err_exit_phys;
>>>>> +     }
>>>>> +
>>>>> +     return phy_roothub;
>>>>> +
>>>>> +err_exit_phys:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_exit(roothub_entry->phy);
>>>>> +
>>>>> +err_out:
>>>>> +     return ERR_PTR(err);
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>> +
>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err, ret = 0;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     ret = ret;
>>>>> +     }
>>>>
>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>> if I understood Chunfeng Yun correctly this will require
>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>> Mediatek SoCs
>>>
>>
>> OK. I suppose that there are 2 cases
>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>> Remote wakeup probably required.
>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>
>>>>> +
>>>>> +     return ret;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>> +
>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +     struct list_head *head;
>>>>> +     int err;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return 0;
>>>>> +
>>>>> +     head = &phy_roothub->list;
>>>>> +
>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>> +             if (err)
>>>>> +                     goto err_out;
>>>>> +     }
>>>>> +
>>>>> +     return 0;
>>>>> +
>>>>> +err_out:
>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>> +
>>>>> +     return err;
>>>>> +}
>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>> +
>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>> +{
>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>> +
>>>>> +     if (!phy_roothub)
>>>>> +             return;
>>>>> +
>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>> +             phy_power_off(roothub_entry->phy);
>>>>
>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>> we're no longer able to reach low power states on system suspend.
>>> I'm not sure where this problem should be solved:
>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>> TI platforms
>>
>> Many TI platforms are affected, omap5*, dra7*, am43*
>>
>>> - fix this in the usb_phy_roothub code
>>
>> I'd vote for fixing it in the usb_phy_roothub code. How?
>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
>>> - fix this in the PHY driver
>>
>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:

Not in favor of adding explicit suspend/resume ops since PM framework already
has those. I think we should let PHY drivers manage suspend/resume on its own
(after creating the dependency between the controller device and PHY using
device_link_add).

Thanks
Kishon
--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Chunfeng Yun (云春峰) March 20, 2018, 12:04 p.m. UTC | #11
Hi Martin & Roger:

On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> Hi Roger,
> 
> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> > Hi,
> >
> > On 19/03/18 00:29, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>> +some TI folks
> >>>
> >>> Hi Martin,
> >>>
> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>> registered through the generic PHY framework. These PHYs have to be
> >>>> enabled to make the USB controller actually work. They also have to be
> >>>> disabled again on shutdown/suspend.
> >>>>
> >>>> Currently (at least) the following HCI platform drivers are using custom
> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>> disable/enable them when required:
> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>
> >>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>> USB controller's devicetree node (just like on the drivers listed
> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>> PHYs it not initialized then none of USB port works at all).
> >>>>
> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>
> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>> I'll explain why below.
> >> based on your explanation and reading the TI PHY drivers I am assuming
> >> that the affected SoCs are using the "phy-omap-usb2" driver
> >>
> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> I missed that, thanks
> 
> >>>> ---
> >>>>  drivers/usb/core/Makefile |   2 +-
> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>
> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>> index 92c9cefb4317..18e874b0441e 100644
> >>>> --- a/drivers/usb/core/Makefile
> >>>> +++ b/drivers/usb/core/Makefile
> >>>> @@ -6,7 +6,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
> >>>> +usbcore-y += phy.o port.o
> >>>>
> >>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>> new file mode 100644
> >>>> index 000000000000..09b7c43c0ea4
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.c
> >>>> @@ -0,0 +1,158 @@
> >>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>> +/*
> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>> + *
> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>> + */
> >>>> +
> >>>> +#include <linux/device.h>
> >>>> +#include <linux/list.h>
> >>>> +#include <linux/phy/phy.h>
> >>>> +#include <linux/of.h>
> >>>> +
> >>>> +#include "phy.h"
> >>>> +
> >>>> +struct usb_phy_roothub {
> >>>> +     struct phy              *phy;
> >>>> +     struct list_head        list;
> >>>> +};
> >>>> +
> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>> +     if (!roothub_entry)
> >>>> +             return ERR_PTR(-ENOMEM);
> >>>> +
> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>> +
> >>>> +     return roothub_entry;
> >>>> +}
> >>>> +
> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>> +                                struct list_head *list)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>> +
> >>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>> +                     return 0;
> >>>> +             else
> >>>> +                     return PTR_ERR(phy);
> >>>> +     }
> >>>> +
> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(roothub_entry))
> >>>> +             return PTR_ERR(roothub_entry);
> >>>> +
> >>>> +     roothub_entry->phy = phy;
> >>>> +
> >>>> +     list_add_tail(&roothub_entry->list, list);
> >>>> +
> >>>> +     return 0;
> >>>> +}
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>> +{
> >>>> +     struct usb_phy_roothub *phy_roothub;
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int i, num_phys, err;
> >>>> +
> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>> +                                           "#phy-cells");
> >>>> +     if (num_phys <= 0)
> >>>> +             return NULL;
> >>>> +
> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>> +     if (IS_ERR(phy_roothub))
> >>>> +             return phy_roothub;
> >>>> +
> >>>> +     for (i = 0; i < num_phys; i++) {
> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_init(roothub_entry->phy);
> >>>
> >>> The phy_init() function actually enables the PHY clocks.
> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>
> >
> > Yes.
> >
> >> an earlier version of my patch did exactly this, but it caused
> >> problems during a suspend/resume cycle on Mediatek devices
> >> Chunfeng Yun reported that issue here [0], quote from that mail for
> >> easier reading:
> >> "In order to keep link state on mt8173, we just power off all phys(not
> >> exit) when system enter suspend, then power on them again (needn't
> >> init, otherwise device will be disconnected) when system resume, this
> >> can avoid re-enumerating device."
> >>
> >>>> +             if (err)
> >>>> +                     goto err_exit_phys;
> >>>> +     }
> >>>> +
> >>>> +     return phy_roothub;
> >>>> +
> >>>> +err_exit_phys:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_exit(roothub_entry->phy);
> >>>> +
> >>>> +err_out:
> >>>> +     return ERR_PTR(err);
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>> +
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err, ret = 0;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_exit(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     ret = ret;
> >>>> +     }
> >>>
> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >> if I understood Chunfeng Yun correctly this will require
> >> re-enumeration of the USB devices after a suspend/resume cycle on
> >> Mediatek SoCs
> >>
> >
> > OK. I suppose that there are 2 cases
> > 1) Mediatek's case: USB controller context retained across suspend/resume.
> > Remote wakeup probably required.
> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> > during suspend/resume to keep PHY link active.
> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> that the parent of the USB controller can be marked as "wakeup-source"
> 
> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> > phy_exit()/phy_init() must be called during suspend/resume.
> ACK
> 
> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>
> >>>> +
> >>>> +     return ret;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +     struct list_head *head;
> >>>> +     int err;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return 0;
> >>>> +
> >>>> +     head = &phy_roothub->list;
> >>>> +
> >>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>> +             err = phy_power_on(roothub_entry->phy);
> >>>> +             if (err)
> >>>> +                     goto err_out;
> >>>> +     }
> >>>> +
> >>>> +     return 0;
> >>>> +
> >>>> +err_out:
> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>> +
> >>>> +     return err;
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>> +
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>> +{
> >>>> +     struct usb_phy_roothub *roothub_entry;
> >>>> +
> >>>> +     if (!phy_roothub)
> >>>> +             return;
> >>>> +
> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>> +             phy_power_off(roothub_entry->phy);
> >>>
> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>> we're no longer able to reach low power states on system suspend.
> >> I'm not sure where this problem should be solved:
> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >> TI platforms
> >
> > Many TI platforms are affected, omap5*, dra7*, am43*
> >
> >> - fix this in the usb_phy_roothub code
> >
> > I'd vote for fixing it in the usb_phy_roothub code. How?
> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> the controller is *NOT* marked as "wakeup-source"?
> I am also not sure if it would work, since the "wakeup-source"
> property is defined on the USB controller's parent node in case of the
> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> 
Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
devices by device_init_wakeup(dev, true),but not dependent on
"wakeup-source" property, so maybe we can use device_can_wakeup() to
decide whether call phy_exit()/init() or not.

> >> - fix this in the PHY driver
> >
> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> I actually wonder if phy_ops should have explicit suspend/resume support:
> - assuming we define two new callbacks: .suspend and .resume
> - the PHY framework could call .power_off by default if .suspend is not defined
> - the PHY framework could call .power_on by default if .resume is not defined
> - drivers could set .suspend and .resume on their own, allowing more
> fine-grained control by for example *only* stopping the clock (but not
> re-initializing the registers, etc.)
> 
> @Roger: what do you think about this?
> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> hear his opinion too
> 
> >> - somewhere else
> >>
> >>>> +}
> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>> new file mode 100644
> >>>> index 000000000000..6fde59bfbff8
> >>>> --- /dev/null
> >>>> +++ b/drivers/usb/core/phy.h
> >>>> @@ -0,0 +1,7 @@
> >>>> +struct usb_phy_roothub;
> >>>> +
> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>> +
> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>
> >>>
> >
> > <snip>
> >
> > --
> > cheers,
> > -roger
> >
> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
> 
> 
> Regards
> Martin


--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Martin Blumenstingl March 20, 2018, 9:57 p.m. UTC | #12
Hello Kishon,

On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> Hi,
>
> On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>> Hi,
>>>
>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> +some TI folks
>>>>>
>>>>> Hi Martin,
>>>>>
>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>> disabled again on shutdown/suspend.
>>>>>>
>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>> disable/enable them when required:
>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>
>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>
>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>
>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>> I'll explain why below.
>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>
>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>>>>>> ---
>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>
>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>> --- a/drivers/usb/core/Makefile
>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>> @@ -6,7 +6,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
>>>>>> +usbcore-y += phy.o port.o
>>>>>>
>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>> new file mode 100644
>>>>>> index 000000000000..09b7c43c0ea4
>>>>>> --- /dev/null
>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>> @@ -0,0 +1,158 @@
>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>> +/*
>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>> + *
>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>> + */
>>>>>> +
>>>>>> +#include <linux/device.h>
>>>>>> +#include <linux/list.h>
>>>>>> +#include <linux/phy/phy.h>
>>>>>> +#include <linux/of.h>
>>>>>> +
>>>>>> +#include "phy.h"
>>>>>> +
>>>>>> +struct usb_phy_roothub {
>>>>>> +     struct phy              *phy;
>>>>>> +     struct list_head        list;
>>>>>> +};
>>>>>> +
>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>> +     if (!roothub_entry)
>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>> +
>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>> +
>>>>>> +     return roothub_entry;
>>>>>> +}
>>>>>> +
>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>> +                                struct list_head *list)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>> +
>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>> +                     return 0;
>>>>>> +             else
>>>>>> +                     return PTR_ERR(phy);
>>>>>> +     }
>>>>>> +
>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>> +
>>>>>> +     roothub_entry->phy = phy;
>>>>>> +
>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>> +
>>>>>> +     return 0;
>>>>>> +}
>>>>>> +
>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int i, num_phys, err;
>>>>>> +
>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>> +                                           "#phy-cells");
>>>>>> +     if (num_phys <= 0)
>>>>>> +             return NULL;
>>>>>> +
>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>> +             return phy_roothub;
>>>>>> +
>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>
>>>>> The phy_init() function actually enables the PHY clocks.
>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>
>>>
>>> Yes.
>>>
>>>> an earlier version of my patch did exactly this, but it caused
>>>> problems during a suspend/resume cycle on Mediatek devices
>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>> easier reading:
>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>> exit) when system enter suspend, then power on them again (needn't
>>>> init, otherwise device will be disconnected) when system resume, this
>>>> can avoid re-enumerating device."
>>>>
>>>>>> +             if (err)
>>>>>> +                     goto err_exit_phys;
>>>>>> +     }
>>>>>> +
>>>>>> +     return phy_roothub;
>>>>>> +
>>>>>> +err_exit_phys:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>> +
>>>>>> +err_out:
>>>>>> +     return ERR_PTR(err);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>> +
>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err, ret = 0;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     ret = ret;
>>>>>> +     }
>>>>>
>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>> if I understood Chunfeng Yun correctly this will require
>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>> Mediatek SoCs
>>>>
>>>
>>> OK. I suppose that there are 2 cases
>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>> Remote wakeup probably required.
>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>> during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>> phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>
>>>>>> +
>>>>>> +     return ret;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>> +
>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +     struct list_head *head;
>>>>>> +     int err;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return 0;
>>>>>> +
>>>>>> +     head = &phy_roothub->list;
>>>>>> +
>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>> +             if (err)
>>>>>> +                     goto err_out;
>>>>>> +     }
>>>>>> +
>>>>>> +     return 0;
>>>>>> +
>>>>>> +err_out:
>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>> +
>>>>>> +     return err;
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>> +
>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>> +{
>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>> +
>>>>>> +     if (!phy_roothub)
>>>>>> +             return;
>>>>>> +
>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>
>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>> we're no longer able to reach low power states on system suspend.
>>>> I'm not sure where this problem should be solved:
>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>> TI platforms
>>>
>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>
>>>> - fix this in the usb_phy_roothub code
>>>
>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
>>>> - fix this in the PHY driver
>>>
>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>
> Not in favor of adding explicit suspend/resume ops since PM framework already
> has those. I think we should let PHY drivers manage suspend/resume on its own
> (after creating the dependency between the controller device and PHY using
> device_link_add).
even better if we can re-use some existing code!

the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
support suspend/resume yet, so unfortunately I cannot test this.
besides that I have zero experience with suspend/resume logic.

I'll try to read more about that topic, but I definitely need someone
who could help testing if I have a patch ready
in any case: I think implementing this should not be done for v4.17
anymore - maybe we can find a small solution for v4.17 and switch to
your proposed solution later (assuming nobody has arguments against
that)


Regards
Martin
--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Martin Blumenstingl March 20, 2018, 10:01 p.m. UTC | #13
Hi Roger, Hi Chunfeng,

On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> Hi Martin & Roger:
>
> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>> Hi Roger,
>>
>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>> > Hi,
>> >
>> > On 19/03/18 00:29, Martin Blumenstingl wrote:
>> >> Hi Roger,
>> >>
>> >> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>> >>> +some TI folks
>> >>>
>> >>> Hi Martin,
>> >>>
>> >>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>> >>>> Many SoC platforms have separate devices for the USB PHY which are
>> >>>> registered through the generic PHY framework. These PHYs have to be
>> >>>> enabled to make the USB controller actually work. They also have to be
>> >>>> disabled again on shutdown/suspend.
>> >>>>
>> >>>> Currently (at least) the following HCI platform drivers are using custom
>> >>>> code to obtain all PHYs via devicetree for the roothub/controller and
>> >>>> disable/enable them when required:
>> >>>> - ehci-platform.c has ehci_platform_power_{on,off}
>> >>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>> >>>> - ohci-platform.c has ohci_platform_power_{on,off}
>> >>>>
>> >>>> With this new wrapper the USB PHYs can be specified directly in the
>> >>>> USB controller's devicetree node (just like on the drivers listed
>> >>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>> >>>> correctly once this is wired up correctly. These SoCs use a dwc3
>> >>>> controller and require all USB PHYs to be initialized (if one of the USB
>> >>>> PHYs it not initialized then none of USB port works at all).
>> >>>>
>> >>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>> >>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>> >>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>> >>>
>> >>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>> >>> I'll explain why below.
>> >> based on your explanation and reading the TI PHY drivers I am assuming
>> >> that the affected SoCs are using the "phy-omap-usb2" driver
>> >>
>> > yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>> I missed that, thanks
>>
>> >>>> ---
>> >>>>  drivers/usb/core/Makefile |   2 +-
>> >>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>> >>>>  drivers/usb/core/phy.h    |   7 ++
>> >>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>> >>>>  create mode 100644 drivers/usb/core/phy.c
>> >>>>  create mode 100644 drivers/usb/core/phy.h
>> >>>>
>> >>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>> >>>> index 92c9cefb4317..18e874b0441e 100644
>> >>>> --- a/drivers/usb/core/Makefile
>> >>>> +++ b/drivers/usb/core/Makefile
>> >>>> @@ -6,7 +6,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
>> >>>> +usbcore-y += phy.o port.o
>> >>>>
>> >>>>  usbcore-$(CONFIG_OF)         += of.o
>> >>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>> >>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> >>>> new file mode 100644
>> >>>> index 000000000000..09b7c43c0ea4
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.c
>> >>>> @@ -0,0 +1,158 @@
>> >>>> +// SPDX-License-Identifier: GPL-2.0+
>> >>>> +/*
>> >>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>> >>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>> >>>> + * all PHYs on a HCD and to keep them all in the same state.
>> >>>> + *
>> >>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>> >>>> + */
>> >>>> +
>> >>>> +#include <linux/device.h>
>> >>>> +#include <linux/list.h>
>> >>>> +#include <linux/phy/phy.h>
>> >>>> +#include <linux/of.h>
>> >>>> +
>> >>>> +#include "phy.h"
>> >>>> +
>> >>>> +struct usb_phy_roothub {
>> >>>> +     struct phy              *phy;
>> >>>> +     struct list_head        list;
>> >>>> +};
>> >>>> +
>> >>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> >>>> +     if (!roothub_entry)
>> >>>> +             return ERR_PTR(-ENOMEM);
>> >>>> +
>> >>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>> >>>> +
>> >>>> +     return roothub_entry;
>> >>>> +}
>> >>>> +
>> >>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>> >>>> +                                struct list_head *list)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>> >>>> +
>> >>>> +     if (IS_ERR_OR_NULL(phy)) {
>> >>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>> >>>> +                     return 0;
>> >>>> +             else
>> >>>> +                     return PTR_ERR(phy);
>> >>>> +     }
>> >>>> +
>> >>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(roothub_entry))
>> >>>> +             return PTR_ERR(roothub_entry);
>> >>>> +
>> >>>> +     roothub_entry->phy = phy;
>> >>>> +
>> >>>> +     list_add_tail(&roothub_entry->list, list);
>> >>>> +
>> >>>> +     return 0;
>> >>>> +}
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *phy_roothub;
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int i, num_phys, err;
>> >>>> +
>> >>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>> >>>> +                                           "#phy-cells");
>> >>>> +     if (num_phys <= 0)
>> >>>> +             return NULL;
>> >>>> +
>> >>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>> >>>> +     if (IS_ERR(phy_roothub))
>> >>>> +             return phy_roothub;
>> >>>> +
>> >>>> +     for (i = 0; i < num_phys; i++) {
>> >>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_init(roothub_entry->phy);
>> >>>
>> >>> The phy_init() function actually enables the PHY clocks.
>> >>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>> >> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>> >> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>> >>
>> >
>> > Yes.
>> >
>> >> an earlier version of my patch did exactly this, but it caused
>> >> problems during a suspend/resume cycle on Mediatek devices
>> >> Chunfeng Yun reported that issue here [0], quote from that mail for
>> >> easier reading:
>> >> "In order to keep link state on mt8173, we just power off all phys(not
>> >> exit) when system enter suspend, then power on them again (needn't
>> >> init, otherwise device will be disconnected) when system resume, this
>> >> can avoid re-enumerating device."
>> >>
>> >>>> +             if (err)
>> >>>> +                     goto err_exit_phys;
>> >>>> +     }
>> >>>> +
>> >>>> +     return phy_roothub;
>> >>>> +
>> >>>> +err_exit_phys:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_exit(roothub_entry->phy);
>> >>>> +
>> >>>> +err_out:
>> >>>> +     return ERR_PTR(err);
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>> >>>> +
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err, ret = 0;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_exit(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     ret = ret;
>> >>>> +     }
>> >>>
>> >>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>> >> if I understood Chunfeng Yun correctly this will require
>> >> re-enumeration of the USB devices after a suspend/resume cycle on
>> >> Mediatek SoCs
>> >>
>> >
>> > OK. I suppose that there are 2 cases
>> > 1) Mediatek's case: USB controller context retained across suspend/resume.
>> > Remote wakeup probably required.
>> > No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>> > during suspend/resume to keep PHY link active.
>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>> that the parent of the USB controller can be marked as "wakeup-source"
>>
>> > 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>> > phy_exit()/phy_init() must be called during suspend/resume.
>> ACK
>>
>> >>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>> >>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>> >>>
>> >>>> +
>> >>>> +     return ret;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +     struct list_head *head;
>> >>>> +     int err;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return 0;
>> >>>> +
>> >>>> +     head = &phy_roothub->list;
>> >>>> +
>> >>>> +     list_for_each_entry(roothub_entry, head, list) {
>> >>>> +             err = phy_power_on(roothub_entry->phy);
>> >>>> +             if (err)
>> >>>> +                     goto err_out;
>> >>>> +     }
>> >>>> +
>> >>>> +     return 0;
>> >>>> +
>> >>>> +err_out:
>> >>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>> +
>> >>>> +     return err;
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>> >>>> +
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>> >>>> +{
>> >>>> +     struct usb_phy_roothub *roothub_entry;
>> >>>> +
>> >>>> +     if (!phy_roothub)
>> >>>> +             return;
>> >>>> +
>> >>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>> >>>> +             phy_power_off(roothub_entry->phy);
>> >>>
>> >>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>> >>> we're no longer able to reach low power states on system suspend.
>> >> I'm not sure where this problem should be solved:
>> >> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>> >> TI platforms
>> >
>> > Many TI platforms are affected, omap5*, dra7*, am43*
>> >
>> >> - fix this in the usb_phy_roothub code
>> >
>> > I'd vote for fixing it in the usb_phy_roothub code. How?
>> > How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>> > If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>> the controller is *NOT* marked as "wakeup-source"?
>> I am also not sure if it would work, since the "wakeup-source"
>> property is defined on the USB controller's parent node in case of the
>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>
> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> devices by device_init_wakeup(dev, true),but not dependent on
> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> decide whether call phy_exit()/init() or not.
the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
yet, so I cannot test this
based on this suggestion I threw up two patches which are *compile
tested only* based on Greg's usb-next branch
you can find them here: [0] (as well as attached to this mail)

@Chunfeng: can you please test this on one of your Mediatek SoCs?
@Roger: can you please test this on a TI SoC?

(apologies in advance if these patches don't work)

please note that I won't have access to my computer until Saturday.
if these patches need to be rewritten/replaced/etc. then feel free to
send your own version to the list

>> >> - fix this in the PHY driver
>> >
>> > There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>> I actually wonder if phy_ops should have explicit suspend/resume support:
>> - assuming we define two new callbacks: .suspend and .resume
>> - the PHY framework could call .power_off by default if .suspend is not defined
>> - the PHY framework could call .power_on by default if .resume is not defined
>> - drivers could set .suspend and .resume on their own, allowing more
>> fine-grained control by for example *only* stopping the clock (but not
>> re-initializing the registers, etc.)
>>
>> @Roger: what do you think about this?
>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>> hear his opinion too
>>
>> >> - somewhere else
>> >>
>> >>>> +}
>> >>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>> >>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>> >>>> new file mode 100644
>> >>>> index 000000000000..6fde59bfbff8
>> >>>> --- /dev/null
>> >>>> +++ b/drivers/usb/core/phy.h
>> >>>> @@ -0,0 +1,7 @@
>> >>>> +struct usb_phy_roothub;
>> >>>> +
>> >>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>> >>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>> >>>> +
>> >>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>> >>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>> >>>>
>> >>>
>> >
>> > <snip>
>> >
>> > --
>> > cheers,
>> > -roger
>> >
>> > Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
>>
>>
>> Regards
>> Martin
>
>


Regards
Martin


[0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
From 78afe3dfbf21dd521076d1fd625096fa440f5195 Mon Sep 17 00:00:00 2001
From: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
Date: Tue, 20 Mar 2018 22:36:39 +0100
Subject: [PATCH 1/2] usb: core: split usb_phy_roothub_{init,alloc}

Before this patch usb_phy_roothub_init served two purposes (from a
caller's point of view - like hcd.c):
- parsing the PHYs and allocating the list entries
- calling phy_init on each list entry

While this worked so far it has one disadvantage: if we need to call
phy_init for each PHY instance then the existing code cannot be re-used.
Solve this by splitting off usb_phy_roothub_alloc which only parses the
PHYs and allocates the list entries.
usb_phy_roothub_init then gets a struct usb_phy_roothub and only calls
phy_init on each PHY instance (along with the corresponding cleanup if
that failed somewhere).

This is a preparation step for adding proper suspend support for some
hardware that requires phy_exit to be called during suspend and phy_init
to be called during resume.

Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
---
 drivers/usb/core/hcd.c | 10 +++++++---
 drivers/usb/core/phy.c | 45 ++++++++++++++++++++++-----------------------
 drivers/usb/core/phy.h |  4 +++-
 3 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 5a92d8f7c484..89b10d859047 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2758,12 +2758,16 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	}
 
 	if (!hcd->skip_phy_initialization) {
-		hcd->phy_roothub = usb_phy_roothub_init(hcd->self.sysdev);
+		hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev);
 		if (IS_ERR(hcd->phy_roothub)) {
 			retval = PTR_ERR(hcd->phy_roothub);
-			goto err_phy_roothub_init;
+			goto err_phy_roothub_alloc;
 		}
 
+		retval = usb_phy_roothub_init(hcd->phy_roothub);
+		if (retval)
+			goto err_phy_roothub_alloc;
+
 		retval = usb_phy_roothub_power_on(hcd->phy_roothub);
 		if (retval)
 			goto err_usb_phy_roothub_power_on;
@@ -2936,7 +2940,7 @@ int usb_add_hcd(struct usb_hcd *hcd,
 	usb_phy_roothub_power_off(hcd->phy_roothub);
 err_usb_phy_roothub_power_on:
 	usb_phy_roothub_exit(hcd->phy_roothub);
-err_phy_roothub_init:
+err_phy_roothub_alloc:
 	if (hcd->remove_phy && hcd->usb_phy) {
 		usb_phy_shutdown(hcd->usb_phy);
 		usb_put_phy(hcd->usb_phy);
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 09b7c43c0ea4..50a80dbe776c 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -19,19 +19,6 @@ struct usb_phy_roothub {
 	struct list_head	list;
 };
 
-static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
-{
-	struct usb_phy_roothub *roothub_entry;
-
-	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
-	if (!roothub_entry)
-		return ERR_PTR(-ENOMEM);
-
-	INIT_LIST_HEAD(&roothub_entry->list);
-
-	return roothub_entry;
-}
-
 static int usb_phy_roothub_add_phy(struct device *dev, int index,
 				   struct list_head *list)
 {
@@ -56,11 +43,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 	return 0;
 }
 
-struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
+struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
 {
 	struct usb_phy_roothub *phy_roothub;
-	struct usb_phy_roothub *roothub_entry;
-	struct list_head *head;
 	int i, num_phys, err;
 
 	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
@@ -68,16 +53,31 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 	if (num_phys <= 0)
 		return NULL;
 
-	phy_roothub = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(phy_roothub))
-		return phy_roothub;
+	phy_roothub = devm_kzalloc(dev, sizeof(*phy_roothub), GFP_KERNEL);
+	if (!phy_roothub)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&phy_roothub->list);
 
 	for (i = 0; i < num_phys; i++) {
 		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
 		if (err)
-			goto err_out;
+			return ERR_PTR(err);
 	}
 
+	return phy_roothub;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_alloc);
+
+int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err;
+
+	if (!phy_roothub)
+		return 0;
+
 	head = &phy_roothub->list;
 
 	list_for_each_entry(roothub_entry, head, list) {
@@ -86,14 +86,13 @@ struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
 			goto err_exit_phys;
 	}
 
-	return phy_roothub;
+	return 0;
 
 err_exit_phys:
 	list_for_each_entry_continue_reverse(roothub_entry, head, list)
 		phy_exit(roothub_entry->phy);
 
-err_out:
-	return ERR_PTR(err);
+	return err;
 }
 EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
 
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
index 6fde59bfbff8..eb31253201ad 100644
--- a/drivers/usb/core/phy.h
+++ b/drivers/usb/core/phy.h
@@ -1,6 +1,8 @@
 struct usb_phy_roothub;
 
-struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
+struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev);
+
+int usb_phy_roothub_init(struct usb_phy_roothub *phy_roothub);
 int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
 
 int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
Chunfeng Yun (云春峰) March 20, 2018, 11:47 p.m. UTC | #14
Hi Martin,

On Tue, 2018-03-20 at 22:57 +0100, Martin Blumenstingl wrote:
> Hello Kishon,
> 
> On Tue, Mar 20, 2018 at 12:27 PM, Kishon Vijay Abraham I <kishon@ti.com> wrote:
> > Hi,
> >
> > On Monday 19 March 2018 09:42 PM, Martin Blumenstingl wrote:
> >> Hi Roger,
> >>
> >> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>> Hi,
> >>>
> >>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>> Hi Roger,
> >>>>
> >>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>> +some TI folks
> >>>>>
> >>>>> Hi Martin,
> >>>>>
> >>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>> disabled again on shutdown/suspend.
> >>>>>>
> >>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>> disable/enable them when required:
> >>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>
> >>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>
> >>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>
> >>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>> I'll explain why below.
> >>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>
> >>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >> I missed that, thanks
> >>
> >>>>>> ---
> >>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>
> >>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>> --- a/drivers/usb/core/Makefile
> >>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>> @@ -6,7 +6,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
> >>>>>> +usbcore-y += phy.o port.o
> >>>>>>
> >>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>> new file mode 100644
> >>>>>> index 000000000000..09b7c43c0ea4
> >>>>>> --- /dev/null
> >>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>> @@ -0,0 +1,158 @@
> >>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>> +/*
> >>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>> + *
> >>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>> + */
> >>>>>> +
> >>>>>> +#include <linux/device.h>
> >>>>>> +#include <linux/list.h>
> >>>>>> +#include <linux/phy/phy.h>
> >>>>>> +#include <linux/of.h>
> >>>>>> +
> >>>>>> +#include "phy.h"
> >>>>>> +
> >>>>>> +struct usb_phy_roothub {
> >>>>>> +     struct phy              *phy;
> >>>>>> +     struct list_head        list;
> >>>>>> +};
> >>>>>> +
> >>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>> +     if (!roothub_entry)
> >>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>> +
> >>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>> +
> >>>>>> +     return roothub_entry;
> >>>>>> +}
> >>>>>> +
> >>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>> +                                struct list_head *list)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>> +
> >>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>> +                     return 0;
> >>>>>> +             else
> >>>>>> +                     return PTR_ERR(phy);
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>> +
> >>>>>> +     roothub_entry->phy = phy;
> >>>>>> +
> >>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +}
> >>>>>> +
> >>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int i, num_phys, err;
> >>>>>> +
> >>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>> +                                           "#phy-cells");
> >>>>>> +     if (num_phys <= 0)
> >>>>>> +             return NULL;
> >>>>>> +
> >>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>> +             return phy_roothub;
> >>>>>> +
> >>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>
> >>>>> The phy_init() function actually enables the PHY clocks.
> >>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>
> >>>
> >>> Yes.
> >>>
> >>>> an earlier version of my patch did exactly this, but it caused
> >>>> problems during a suspend/resume cycle on Mediatek devices
> >>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>> easier reading:
> >>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>> exit) when system enter suspend, then power on them again (needn't
> >>>> init, otherwise device will be disconnected) when system resume, this
> >>>> can avoid re-enumerating device."
> >>>>
> >>>>>> +             if (err)
> >>>>>> +                     goto err_exit_phys;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return phy_roothub;
> >>>>>> +
> >>>>>> +err_exit_phys:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     return ERR_PTR(err);
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err, ret = 0;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     ret = ret;
> >>>>>> +     }
> >>>>>
> >>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>> if I understood Chunfeng Yun correctly this will require
> >>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>> Mediatek SoCs
> >>>>
> >>>
> >>> OK. I suppose that there are 2 cases
> >>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>> Remote wakeup probably required.
> >>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>> during suspend/resume to keep PHY link active.
> >> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >> that the parent of the USB controller can be marked as "wakeup-source"
> >>
> >>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>> phy_exit()/phy_init() must be called during suspend/resume.
> >> ACK
> >>
> >>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>
> >>>>>> +
> >>>>>> +     return ret;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>> +
> >>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +     struct list_head *head;
> >>>>>> +     int err;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return 0;
> >>>>>> +
> >>>>>> +     head = &phy_roothub->list;
> >>>>>> +
> >>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>> +             if (err)
> >>>>>> +                     goto err_out;
> >>>>>> +     }
> >>>>>> +
> >>>>>> +     return 0;
> >>>>>> +
> >>>>>> +err_out:
> >>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>> +
> >>>>>> +     return err;
> >>>>>> +}
> >>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>> +
> >>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>> +{
> >>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>> +
> >>>>>> +     if (!phy_roothub)
> >>>>>> +             return;
> >>>>>> +
> >>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>
> >>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>> we're no longer able to reach low power states on system suspend.
> >>>> I'm not sure where this problem should be solved:
> >>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>> TI platforms
> >>>
> >>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>
> >>>> - fix this in the usb_phy_roothub code
> >>>
> >>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >> the controller is *NOT* marked as "wakeup-source"?
> >> I am also not sure if it would work, since the "wakeup-source"
> >> property is defined on the USB controller's parent node in case of the
> >> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>
> >>>> - fix this in the PHY driver
> >>>
> >>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >> I actually wonder if phy_ops should have explicit suspend/resume support:
> >
> > Not in favor of adding explicit suspend/resume ops since PM framework already
> > has those. I think we should let PHY drivers manage suspend/resume on its own
> > (after creating the dependency between the controller device and PHY using
> > device_link_add).
> even better if we can re-use some existing code!
> 
> the platform I am working on (64-bit Amlogic Meson GXL/GXM) does not
> support suspend/resume yet, so unfortunately I cannot test this.
> besides that I have zero experience with suspend/resume logic.
> 
> I'll try to read more about that topic, but I definitely need someone
> who could help testing if I have a patch ready
> in any case: I think implementing this should not be done for v4.17
> anymore - maybe we can find a small solution for v4.17 and switch to
> your proposed solution later (assuming nobody has arguments against
> that)
> 
I'll test it.

Thanks
> 
> Regards
> Martin


--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Roger Quadros March 21, 2018, 11:30 a.m. UTC | #15
Martin,

On 21/03/18 00:01, Martin Blumenstingl wrote:
> Hi Roger, Hi Chunfeng,
> 
> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>> Hi Martin & Roger:
>>
>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>> Hi Roger,
>>>
>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>> Hi,
>>>>
>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> +some TI folks
>>>>>>
>>>>>> Hi Martin,
>>>>>>
>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>> disabled again on shutdown/suspend.
>>>>>>>
>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>> disable/enable them when required:
>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>
>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>
>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>
>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>> I'll explain why below.
>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>
>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>> I missed that, thanks
>>>
>>>>>>> ---
>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>
>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>> @@ -6,7 +6,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
>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>
>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>> @@ -0,0 +1,158 @@
>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>> +/*
>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>> + *
>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>> + */
>>>>>>> +
>>>>>>> +#include <linux/device.h>
>>>>>>> +#include <linux/list.h>
>>>>>>> +#include <linux/phy/phy.h>
>>>>>>> +#include <linux/of.h>
>>>>>>> +
>>>>>>> +#include "phy.h"
>>>>>>> +
>>>>>>> +struct usb_phy_roothub {
>>>>>>> +     struct phy              *phy;
>>>>>>> +     struct list_head        list;
>>>>>>> +};
>>>>>>> +
>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>> +     if (!roothub_entry)
>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>> +
>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>> +
>>>>>>> +     return roothub_entry;
>>>>>>> +}
>>>>>>> +
>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>> +                                struct list_head *list)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>> +
>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>> +                     return 0;
>>>>>>> +             else
>>>>>>> +                     return PTR_ERR(phy);
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>> +
>>>>>>> +     roothub_entry->phy = phy;
>>>>>>> +
>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +}
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int i, num_phys, err;
>>>>>>> +
>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>> +                                           "#phy-cells");
>>>>>>> +     if (num_phys <= 0)
>>>>>>> +             return NULL;
>>>>>>> +
>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>> +             return phy_roothub;
>>>>>>> +
>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>
>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>
>>>>
>>>> Yes.
>>>>
>>>>> an earlier version of my patch did exactly this, but it caused
>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>> easier reading:
>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>> can avoid re-enumerating device."
>>>>>
>>>>>>> +             if (err)
>>>>>>> +                     goto err_exit_phys;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return phy_roothub;
>>>>>>> +
>>>>>>> +err_exit_phys:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     return ERR_PTR(err);
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err, ret = 0;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     ret = ret;
>>>>>>> +     }
>>>>>>
>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>> if I understood Chunfeng Yun correctly this will require
>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>> Mediatek SoCs
>>>>>
>>>>
>>>> OK. I suppose that there are 2 cases
>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>> Remote wakeup probably required.
>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>> during suspend/resume to keep PHY link active.
>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>
>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>> ACK
>>>
>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>
>>>>>>> +
>>>>>>> +     return ret;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +     struct list_head *head;
>>>>>>> +     int err;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return 0;
>>>>>>> +
>>>>>>> +     head = &phy_roothub->list;
>>>>>>> +
>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>> +             if (err)
>>>>>>> +                     goto err_out;
>>>>>>> +     }
>>>>>>> +
>>>>>>> +     return 0;
>>>>>>> +
>>>>>>> +err_out:
>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>> +
>>>>>>> +     return err;
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>> +
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>> +{
>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>> +
>>>>>>> +     if (!phy_roothub)
>>>>>>> +             return;
>>>>>>> +
>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>
>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>> we're no longer able to reach low power states on system suspend.
>>>>> I'm not sure where this problem should be solved:
>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>> TI platforms
>>>>
>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>
>>>>> - fix this in the usb_phy_roothub code
>>>>
>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>> the controller is *NOT* marked as "wakeup-source"?
>>> I am also not sure if it would work, since the "wakeup-source"
>>> property is defined on the USB controller's parent node in case of the
>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>
>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>> devices by device_init_wakeup(dev, true),but not dependent on
>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>> decide whether call phy_exit()/init() or not.
> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> yet, so I cannot test this
> based on this suggestion I threw up two patches which are *compile
> tested only* based on Greg's usb-next branch
> you can find them here: [0] (as well as attached to this mail)
> 
> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> @Roger: can you please test this on a TI SoC?
> 
> (apologies in advance if these patches don't work)
> 
> please note that I won't have access to my computer until Saturday.
> if these patches need to be rewritten/replaced/etc. then feel free to
> send your own version to the list

Had to do the following to build

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..2884607 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)


And the following to fix runtime issues

diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..8598906 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);


Here are my obervations
- if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
low power states.

- if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
and so phy_power_off won't be called either.

This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.

This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
What does /sys/bus/usb/device/usb<?>/power/wakeup say?

Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
Chunfeng, why did you need this patch?

[1] - https://patchwork.kernel.org/patch/10298641/

What options do we have now to fix the original issue?

Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
of doing phy_power_off() as well as phy_exit() during suspend.

Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
flag in hcd to use it?

> 
>>>>> - fix this in the PHY driver
>>>>
>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>> - assuming we define two new callbacks: .suspend and .resume
>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>> - the PHY framework could call .power_on by default if .resume is not defined
>>> - drivers could set .suspend and .resume on their own, allowing more
>>> fine-grained control by for example *only* stopping the clock (but not
>>> re-initializing the registers, etc.)
>>>
>>> @Roger: what do you think about this?
>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>> hear his opinion too
>>>
>>>>> - somewhere else
>>>>>
>>>>>>> +}
>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>> new file mode 100644
>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>> --- /dev/null
>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>> @@ -0,0 +1,7 @@
>>>>>>> +struct usb_phy_roothub;
>>>>>>> +
>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>> +
>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>
>>>>>>
>>>>

> 
> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
>
Chunfeng Yun (云春峰) March 22, 2018, 8:10 a.m. UTC | #16
Hi,
On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> Martin,
> 
> On 21/03/18 00:01, Martin Blumenstingl wrote:
> > Hi Roger, Hi Chunfeng,
> > 
> > On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >> Hi Martin & Roger:
> >>
> >> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>> Hi Roger,
> >>>
> >>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>> Hi,
> >>>>
> >>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> +some TI folks
> >>>>>>
> >>>>>> Hi Martin,
> >>>>>>
> >>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>> disabled again on shutdown/suspend.
> >>>>>>>
> >>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>> disable/enable them when required:
> >>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>
> >>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>
> >>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>
> >>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>> I'll explain why below.
> >>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>
> >>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>> I missed that, thanks
> >>>
> >>>>>>> ---
> >>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>
> >>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>> @@ -6,7 +6,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
> >>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>
> >>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>> @@ -0,0 +1,158 @@
> >>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>> +/*
> >>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>> + *
> >>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>> + */
> >>>>>>> +
> >>>>>>> +#include <linux/device.h>
> >>>>>>> +#include <linux/list.h>
> >>>>>>> +#include <linux/phy/phy.h>
> >>>>>>> +#include <linux/of.h>
> >>>>>>> +
> >>>>>>> +#include "phy.h"
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub {
> >>>>>>> +     struct phy              *phy;
> >>>>>>> +     struct list_head        list;
> >>>>>>> +};
> >>>>>>> +
> >>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>> +     if (!roothub_entry)
> >>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>> +
> >>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>> +
> >>>>>>> +     return roothub_entry;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>> +                                struct list_head *list)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>> +
> >>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>> +                     return 0;
> >>>>>>> +             else
> >>>>>>> +                     return PTR_ERR(phy);
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>> +
> >>>>>>> +     roothub_entry->phy = phy;
> >>>>>>> +
> >>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int i, num_phys, err;
> >>>>>>> +
> >>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>> +                                           "#phy-cells");
> >>>>>>> +     if (num_phys <= 0)
> >>>>>>> +             return NULL;
> >>>>>>> +
> >>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>> +             return phy_roothub;
> >>>>>>> +
> >>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>
> >>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>
> >>>>
> >>>> Yes.
> >>>>
> >>>>> an earlier version of my patch did exactly this, but it caused
> >>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>> easier reading:
> >>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>> can avoid re-enumerating device."
> >>>>>
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_exit_phys;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return phy_roothub;
> >>>>>>> +
> >>>>>>> +err_exit_phys:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     return ERR_PTR(err);
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err, ret = 0;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     ret = ret;
> >>>>>>> +     }
> >>>>>>
> >>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>> if I understood Chunfeng Yun correctly this will require
> >>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>> Mediatek SoCs
> >>>>>
> >>>>
> >>>> OK. I suppose that there are 2 cases
> >>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>> Remote wakeup probably required.
> >>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>> during suspend/resume to keep PHY link active.
> >>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>
> >>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>> ACK
> >>>
> >>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>
> >>>>>>> +
> >>>>>>> +     return ret;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +     struct list_head *head;
> >>>>>>> +     int err;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return 0;
> >>>>>>> +
> >>>>>>> +     head = &phy_roothub->list;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>> +             if (err)
> >>>>>>> +                     goto err_out;
> >>>>>>> +     }
> >>>>>>> +
> >>>>>>> +     return 0;
> >>>>>>> +
> >>>>>>> +err_out:
> >>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>> +
> >>>>>>> +     return err;
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>> +
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>> +{
> >>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>> +
> >>>>>>> +     if (!phy_roothub)
> >>>>>>> +             return;
> >>>>>>> +
> >>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>
> >>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>> we're no longer able to reach low power states on system suspend.
> >>>>> I'm not sure where this problem should be solved:
> >>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>> TI platforms
> >>>>
> >>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>
> >>>>> - fix this in the usb_phy_roothub code
> >>>>
> >>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>> the controller is *NOT* marked as "wakeup-source"?
> >>> I am also not sure if it would work, since the "wakeup-source"
> >>> property is defined on the USB controller's parent node in case of the
> >>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>
> >> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >> devices by device_init_wakeup(dev, true),but not dependent on
> >> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >> decide whether call phy_exit()/init() or not.
> > the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> > yet, so I cannot test this
> > based on this suggestion I threw up two patches which are *compile
> > tested only* based on Greg's usb-next branch
> > you can find them here: [0] (as well as attached to this mail)
> > 
> > @Chunfeng: can you please test this on one of your Mediatek SoCs?
> > @Roger: can you please test this on a TI SoC?
> > 
> > (apologies in advance if these patches don't work)
> > 
> > please note that I won't have access to my computer until Saturday.
> > if these patches need to be rewritten/replaced/etc. then feel free to
> > send your own version to the list
> 
> Had to do the following to build
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);

Try to use hcd->self.controller instead of &rhdev->dev;

>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
ditto
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
ditto
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> 
> 
> And the following to fix runtime issues
> 
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
It's ok
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> 
> 
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
> 
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
> 
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
You maybe use a wrong device.
> 
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
They are also "disabled" state;
I think we should use wakeup state of hcd controller, but not
usb_device's, please test it, thanks.

> 
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
> 
> [1] - https://patchwork.kernel.org/patch/10298641/
> 
> What options do we have now to fix the original issue?
> 
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
> 
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?

> 
> > 
> >>>>> - fix this in the PHY driver
> >>>>
> >>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
> >>> I actually wonder if phy_ops should have explicit suspend/resume support:
> >>> - assuming we define two new callbacks: .suspend and .resume
> >>> - the PHY framework could call .power_off by default if .suspend is not defined
> >>> - the PHY framework could call .power_on by default if .resume is not defined
> >>> - drivers could set .suspend and .resume on their own, allowing more
> >>> fine-grained control by for example *only* stopping the clock (but not
> >>> re-initializing the registers, etc.)
> >>>
> >>> @Roger: what do you think about this?
> >>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
> >>> hear his opinion too
> >>>
> >>>>> - somewhere else
> >>>>>
> >>>>>>> +}
> >>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
> >>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
> >>>>>>> new file mode 100644
> >>>>>>> index 000000000000..6fde59bfbff8
> >>>>>>> --- /dev/null
> >>>>>>> +++ b/drivers/usb/core/phy.h
> >>>>>>> @@ -0,0 +1,7 @@
> >>>>>>> +struct usb_phy_roothub;
> >>>>>>> +
> >>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
> >>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +
> >>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
> >>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
> >>>>>>>
> >>>>>>
> >>>>
> 
> > 
> > [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
> > 
> 


--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Roger Quadros March 22, 2018, 12:41 p.m. UTC | #17
On 22/03/18 10:10, Chunfeng Yun wrote:
> Hi,
> On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
>> Martin,
>>
>> On 21/03/18 00:01, Martin Blumenstingl wrote:
>>> Hi Roger, Hi Chunfeng,
>>>
>>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>>> Hi Martin & Roger:
>>>>
>>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>>> Hi Roger,
>>>>>
>>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>> Hi,
>>>>>>
>>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>>> Hi Roger,
>>>>>>>
>>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>>> +some TI folks
>>>>>>>>
>>>>>>>> Hi Martin,
>>>>>>>>
>>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>>
>>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>>> disable/enable them when required:
>>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>>
>>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>>
>>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>>
>>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>>> I'll explain why below.
>>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>>
>>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>>> I missed that, thanks
>>>>>
>>>>>>>>> ---
>>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>>
>>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>>> @@ -6,7 +6,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
>>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>>
>>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>>> new file mode 100644
>>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>>> --- /dev/null
>>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>>> +/*
>>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>>> + *
>>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>>> + */
>>>>>>>>> +
>>>>>>>>> +#include <linux/device.h>
>>>>>>>>> +#include <linux/list.h>
>>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>>> +#include <linux/of.h>
>>>>>>>>> +
>>>>>>>>> +#include "phy.h"
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub {
>>>>>>>>> +     struct phy              *phy;
>>>>>>>>> +     struct list_head        list;
>>>>>>>>> +};
>>>>>>>>> +
>>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>>> +     if (!roothub_entry)
>>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>>> +
>>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>>> +
>>>>>>>>> +     return roothub_entry;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>>> +                                struct list_head *list)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>>> +
>>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>>> +                     return 0;
>>>>>>>>> +             else
>>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>>> +
>>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>>> +
>>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int i, num_phys, err;
>>>>>>>>> +
>>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>>> +                                           "#phy-cells");
>>>>>>>>> +     if (num_phys <= 0)
>>>>>>>>> +             return NULL;
>>>>>>>>> +
>>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>>> +             return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>>
>>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>>
>>>>>>
>>>>>> Yes.
>>>>>>
>>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>>> easier reading:
>>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>>> can avoid re-enumerating device."
>>>>>>>
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_exit_phys;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return phy_roothub;
>>>>>>>>> +
>>>>>>>>> +err_exit_phys:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     return ERR_PTR(err);
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err, ret = 0;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     ret = ret;
>>>>>>>>> +     }
>>>>>>>>
>>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>>> Mediatek SoCs
>>>>>>>
>>>>>>
>>>>>> OK. I suppose that there are 2 cases
>>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>>> Remote wakeup probably required.
>>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>>> during suspend/resume to keep PHY link active.
>>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>>
>>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>>> ACK
>>>>>
>>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>>
>>>>>>>>> +
>>>>>>>>> +     return ret;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>>> +
>>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +     struct list_head *head;
>>>>>>>>> +     int err;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return 0;
>>>>>>>>> +
>>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>>> +             if (err)
>>>>>>>>> +                     goto err_out;
>>>>>>>>> +     }
>>>>>>>>> +
>>>>>>>>> +     return 0;
>>>>>>>>> +
>>>>>>>>> +err_out:
>>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>> +
>>>>>>>>> +     return err;
>>>>>>>>> +}
>>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>>> +
>>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>>> +{
>>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>>> +
>>>>>>>>> +     if (!phy_roothub)
>>>>>>>>> +             return;
>>>>>>>>> +
>>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>>
>>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>>> I'm not sure where this problem should be solved:
>>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>>> TI platforms
>>>>>>
>>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>>
>>>>>>> - fix this in the usb_phy_roothub code
>>>>>>
>>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>>> the controller is *NOT* marked as "wakeup-source"?
>>>>> I am also not sure if it would work, since the "wakeup-source"
>>>>> property is defined on the USB controller's parent node in case of the
>>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>>
>>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>>> devices by device_init_wakeup(dev, true),but not dependent on
>>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>>> decide whether call phy_exit()/init() or not.
>>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>>> yet, so I cannot test this
>>> based on this suggestion I threw up two patches which are *compile
>>> tested only* based on Greg's usb-next branch
>>> you can find them here: [0] (as well as attached to this mail)
>>>
>>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>>> @Roger: can you please test this on a TI SoC?
>>>
>>> (apologies in advance if these patches don't work)
>>>
>>> please note that I won't have access to my computer until Saturday.
>>> if these patches need to be rewritten/replaced/etc. then feel free to
>>> send your own version to the list
>>
>> Had to do the following to build
>>
>> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
>> index 6d4a419..2884607 100644
>> --- a/drivers/usb/core/hcd.c
>> +++ b/drivers/usb/core/hcd.c
>> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>>  		hcd->state = HC_STATE_SUSPENDED;
>>  
>>  		if (!PMSG_IS_AUTO(msg))
>> -			usb_phy_roothub_suspend(hcd->phy_roothub);
>> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> 
> Try to use hcd->self.controller instead of &rhdev->dev;

Actually it should be hcd->self.sysdev.
> 
>>  
>>  		/* Did we race with a root-hub wakeup event? */
>>  		if (rhdev->do_remote_wakeup) {
>> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  	}
>>  
>>  	if (!PMSG_IS_AUTO(msg)) {
>> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
>> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		if (status)
>>  			return status;
>>  	}
>> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>>  		}
>>  	} else {
>>  		hcd->state = old_state;
>> -		usb_phy_roothub_suspend(hcd->phy_roothub);
>> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> ditto
>>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>>  				"resume", status);
>>  		if (status != -ESHUTDOWN)
>>
>>
>> And the following to fix runtime issues
>>
>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>> index 2eca371..8598906 100644
>> --- a/drivers/usb/core/phy.c
>> +++ b/drivers/usb/core/phy.c
>> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>  			return PTR_ERR(phy);
>>  	}
>>  
>> -	roothub_entry = usb_phy_roothub_alloc(dev);
>> -	if (IS_ERR(roothub_entry))
>> -		return PTR_ERR(roothub_entry);
>> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>> +	if (!roothub_entry)
>> +		return -ENOMEM;
>>  
>>  	roothub_entry->phy = phy;
>>  
>> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>>  	usb_phy_roothub_power_off(phy_roothub);
>>  
>>  	/* keep the PHYs initialized so the device can wake up the system */
>> -	if (device_can_wakeup(dev))
>> +	if (device_may_wakeup(dev))
> It's ok

I had to additionally fix usb_phy_roothub_resume() from
	if (device_can_wakeup(dev))
to
	if (!device_may_wakeup(dev))

>>  		return 0;
>>  
>>  	return usb_phy_roothub_exit(phy_roothub);
>>
>>
>> Here are my obervations
>> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
>> low power states.
>>
>> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
>> and so phy_power_off won't be called either.
>>
>> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> You maybe use a wrong device.

Yes, after using the correct device I don't see the problem.

Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
Once you confirm it works for you I can send the 2 patches officially.

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 6d4a419..04cc453 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
 		hcd->state = HC_STATE_SUSPENDED;
 
 		if (!PMSG_IS_AUTO(msg))
-			usb_phy_roothub_suspend(hcd->phy_roothub);
+			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 
 		/* Did we race with a root-hub wakeup event? */
 		if (rhdev->do_remote_wakeup) {
@@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 	}
 
 	if (!PMSG_IS_AUTO(msg)) {
-		status = usb_phy_roothub_resume(hcd->phy_roothub);
+		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
 		if (status)
 			return status;
 	}
@@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
 		}
 	} else {
 		hcd->state = old_state;
-		usb_phy_roothub_suspend(hcd->phy_roothub);
+		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
 		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
 				"resume", status);
 		if (status != -ESHUTDOWN)
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
index 2eca371..25fe729 100644
--- a/drivers/usb/core/phy.c
+++ b/drivers/usb/core/phy.c
@@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
 			return PTR_ERR(phy);
 	}
 
-	roothub_entry = usb_phy_roothub_alloc(dev);
-	if (IS_ERR(roothub_entry))
-		return PTR_ERR(roothub_entry);
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return -ENOMEM;
 
 	roothub_entry->phy = phy;
 
@@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
 	usb_phy_roothub_power_off(phy_roothub);
 
 	/* keep the PHYs initialized so the device can wake up the system */
-	if (device_can_wakeup(dev))
+	if (device_may_wakeup(dev))
 		return 0;
 
 	return usb_phy_roothub_exit(phy_roothub);
@@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
 	int err;
 
 	/* if the device can't wake up the system _exit was called */
-	if (device_can_wakeup(dev)) {
+	if (!device_may_wakeup(dev)) {
 		err = usb_phy_roothub_init(phy_roothub);
 		if (err)
 			return err;
Chunfeng Yun (云春峰) March 23, 2018, 3:19 a.m. UTC | #18
On Thu, 2018-03-22 at 14:41 +0200, Roger Quadros wrote:
> On 22/03/18 10:10, Chunfeng Yun wrote:
> > Hi,
> > On Wed, 2018-03-21 at 13:30 +0200, Roger Quadros wrote:
> >> Martin,
> >>
> >> On 21/03/18 00:01, Martin Blumenstingl wrote:
> >>> Hi Roger, Hi Chunfeng,
> >>>
> >>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >>>> Hi Martin & Roger:
> >>>>
> >>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
> >>>>> Hi Roger,
> >>>>>
> >>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>> Hi,
> >>>>>>
> >>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
> >>>>>>> Hi Roger,
> >>>>>>>
> >>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
> >>>>>>>> +some TI folks
> >>>>>>>>
> >>>>>>>> Hi Martin,
> >>>>>>>>
> >>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
> >>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
> >>>>>>>>> registered through the generic PHY framework. These PHYs have to be
> >>>>>>>>> enabled to make the USB controller actually work. They also have to be
> >>>>>>>>> disabled again on shutdown/suspend.
> >>>>>>>>>
> >>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
> >>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
> >>>>>>>>> disable/enable them when required:
> >>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
> >>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
> >>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
> >>>>>>>>>
> >>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
> >>>>>>>>> USB controller's devicetree node (just like on the drivers listed
> >>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
> >>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
> >>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
> >>>>>>>>> PHYs it not initialized then none of USB port works at all).
> >>>>>>>>>
> >>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
> >>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
> >>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
> >>>>>>>>
> >>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
> >>>>>>>> I'll explain why below.
> >>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
> >>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
> >>>>>>>
> >>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
> >>>>> I missed that, thanks
> >>>>>
> >>>>>>>>> ---
> >>>>>>>>>  drivers/usb/core/Makefile |   2 +-
> >>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
> >>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
> >>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
> >>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
> >>>>>>>>>
> >>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
> >>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
> >>>>>>>>> --- a/drivers/usb/core/Makefile
> >>>>>>>>> +++ b/drivers/usb/core/Makefile
> >>>>>>>>> @@ -6,7 +6,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
> >>>>>>>>> +usbcore-y += phy.o port.o
> >>>>>>>>>
> >>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
> >>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
> >>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >>>>>>>>> new file mode 100644
> >>>>>>>>> index 000000000000..09b7c43c0ea4
> >>>>>>>>> --- /dev/null
> >>>>>>>>> +++ b/drivers/usb/core/phy.c
> >>>>>>>>> @@ -0,0 +1,158 @@
> >>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
> >>>>>>>>> +/*
> >>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
> >>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
> >>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
> >>>>>>>>> + *
> >>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
> >>>>>>>>> + */
> >>>>>>>>> +
> >>>>>>>>> +#include <linux/device.h>
> >>>>>>>>> +#include <linux/list.h>
> >>>>>>>>> +#include <linux/phy/phy.h>
> >>>>>>>>> +#include <linux/of.h>
> >>>>>>>>> +
> >>>>>>>>> +#include "phy.h"
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub {
> >>>>>>>>> +     struct phy              *phy;
> >>>>>>>>> +     struct list_head        list;
> >>>>>>>>> +};
> >>>>>>>>> +
> >>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >>>>>>>>> +     if (!roothub_entry)
> >>>>>>>>> +             return ERR_PTR(-ENOMEM);
> >>>>>>>>> +
> >>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
> >>>>>>>>> +
> >>>>>>>>> +     return roothub_entry;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>>>>>>>> +                                struct list_head *list)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
> >>>>>>>>> +
> >>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
> >>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
> >>>>>>>>> +                     return 0;
> >>>>>>>>> +             else
> >>>>>>>>> +                     return PTR_ERR(phy);
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(roothub_entry))
> >>>>>>>>> +             return PTR_ERR(roothub_entry);
> >>>>>>>>> +
> >>>>>>>>> +     roothub_entry->phy = phy;
> >>>>>>>>> +
> >>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int i, num_phys, err;
> >>>>>>>>> +
> >>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
> >>>>>>>>> +                                           "#phy-cells");
> >>>>>>>>> +     if (num_phys <= 0)
> >>>>>>>>> +             return NULL;
> >>>>>>>>> +
> >>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
> >>>>>>>>> +     if (IS_ERR(phy_roothub))
> >>>>>>>>> +             return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +     for (i = 0; i < num_phys; i++) {
> >>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_init(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> The phy_init() function actually enables the PHY clocks.
> >>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
> >>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
> >>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
> >>>>>>>
> >>>>>>
> >>>>>> Yes.
> >>>>>>
> >>>>>>> an earlier version of my patch did exactly this, but it caused
> >>>>>>> problems during a suspend/resume cycle on Mediatek devices
> >>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
> >>>>>>> easier reading:
> >>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
> >>>>>>> exit) when system enter suspend, then power on them again (needn't
> >>>>>>> init, otherwise device will be disconnected) when system resume, this
> >>>>>>> can avoid re-enumerating device."
> >>>>>>>
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_exit_phys;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return phy_roothub;
> >>>>>>>>> +
> >>>>>>>>> +err_exit_phys:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_exit(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     return ERR_PTR(err);
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err, ret = 0;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_exit(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     ret = ret;
> >>>>>>>>> +     }
> >>>>>>>>
> >>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
> >>>>>>> if I understood Chunfeng Yun correctly this will require
> >>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
> >>>>>>> Mediatek SoCs
> >>>>>>>
> >>>>>>
> >>>>>> OK. I suppose that there are 2 cases
> >>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
> >>>>>> Remote wakeup probably required.
> >>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
> >>>>>> during suspend/resume to keep PHY link active.
> >>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
> >>>>> that the parent of the USB controller can be marked as "wakeup-source"
> >>>>>
> >>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
> >>>>>> phy_exit()/phy_init() must be called during suspend/resume.
> >>>>> ACK
> >>>>>
> >>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
> >>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
> >>>>>>>>
> >>>>>>>>> +
> >>>>>>>>> +     return ret;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
> >>>>>>>>> +
> >>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +     struct list_head *head;
> >>>>>>>>> +     int err;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return 0;
> >>>>>>>>> +
> >>>>>>>>> +     head = &phy_roothub->list;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
> >>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
> >>>>>>>>> +             if (err)
> >>>>>>>>> +                     goto err_out;
> >>>>>>>>> +     }
> >>>>>>>>> +
> >>>>>>>>> +     return 0;
> >>>>>>>>> +
> >>>>>>>>> +err_out:
> >>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>> +
> >>>>>>>>> +     return err;
> >>>>>>>>> +}
> >>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
> >>>>>>>>> +
> >>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
> >>>>>>>>> +{
> >>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
> >>>>>>>>> +
> >>>>>>>>> +     if (!phy_roothub)
> >>>>>>>>> +             return;
> >>>>>>>>> +
> >>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
> >>>>>>>>> +             phy_power_off(roothub_entry->phy);
> >>>>>>>>
> >>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
> >>>>>>>> we're no longer able to reach low power states on system suspend.
> >>>>>>> I'm not sure where this problem should be solved:
> >>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
> >>>>>>> TI platforms
> >>>>>>
> >>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
> >>>>>>
> >>>>>>> - fix this in the usb_phy_roothub code
> >>>>>>
> >>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
> >>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
> >>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
> >>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
> >>>>> the controller is *NOT* marked as "wakeup-source"?
> >>>>> I am also not sure if it would work, since the "wakeup-source"
> >>>>> property is defined on the USB controller's parent node in case of the
> >>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
> >>>>>
> >>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
> >>>> devices by device_init_wakeup(dev, true),but not dependent on
> >>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
> >>>> decide whether call phy_exit()/init() or not.
> >>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
> >>> yet, so I cannot test this
> >>> based on this suggestion I threw up two patches which are *compile
> >>> tested only* based on Greg's usb-next branch
> >>> you can find them here: [0] (as well as attached to this mail)
> >>>
> >>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
> >>> @Roger: can you please test this on a TI SoC?
> >>>
> >>> (apologies in advance if these patches don't work)
> >>>
> >>> please note that I won't have access to my computer until Saturday.
> >>> if these patches need to be rewritten/replaced/etc. then feel free to
> >>> send your own version to the list
> >>
> >> Had to do the following to build
> >>
> >> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> >> index 6d4a419..2884607 100644
> >> --- a/drivers/usb/core/hcd.c
> >> +++ b/drivers/usb/core/hcd.c
> >> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
> >>  		hcd->state = HC_STATE_SUSPENDED;
> >>  
> >>  		if (!PMSG_IS_AUTO(msg))
> >> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +			usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > 
> > Try to use hcd->self.controller instead of &rhdev->dev;
> 
> Actually it should be hcd->self.sysdev.
I find that xhci-plat.c enables wakeup by hcd->self.controller which is
different from hcd->self.sysdev sometimes.

And hcd->self.controller is the same as hcd->self.sysdev on MTK's
platform.

> > 
> >>  
> >>  		/* Did we race with a root-hub wakeup event? */
> >>  		if (rhdev->do_remote_wakeup) {
> >> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  	}
> >>  
> >>  	if (!PMSG_IS_AUTO(msg)) {
> >> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> >> +		status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		if (status)
> >>  			return status;
> >>  	}
> >> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
> >>  		}
> >>  	} else {
> >>  		hcd->state = old_state;
> >> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> >> +		usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
> > ditto
> >>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
> >>  				"resume", status);
> >>  		if (status != -ESHUTDOWN)
> >>
> >>
> >> And the following to fix runtime issues
> >>
> >> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> >> index 2eca371..8598906 100644
> >> --- a/drivers/usb/core/phy.c
> >> +++ b/drivers/usb/core/phy.c
> >> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
> >>  			return PTR_ERR(phy);
> >>  	}
> >>  
> >> -	roothub_entry = usb_phy_roothub_alloc(dev);
> >> -	if (IS_ERR(roothub_entry))
> >> -		return PTR_ERR(roothub_entry);
> >> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> >> +	if (!roothub_entry)
> >> +		return -ENOMEM;
> >>  
> >>  	roothub_entry->phy = phy;
> >>  
> >> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
> >>  	usb_phy_roothub_power_off(phy_roothub);
> >>  
> >>  	/* keep the PHYs initialized so the device can wake up the system */
> >> -	if (device_can_wakeup(dev))
> >> +	if (device_may_wakeup(dev))
> > It's ok
> 
> I had to additionally fix usb_phy_roothub_resume() from
> 	if (device_can_wakeup(dev))
> to
> 	if (!device_may_wakeup(dev))
> 
> >>  		return 0;
> >>  
> >>  	return usb_phy_roothub_exit(phy_roothub);
> >>
> >>
> >> Here are my obervations
> >> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> >> low power states.
> >>
> >> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> >> and so phy_power_off won't be called either.
> >>
> >> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
> > You maybe use a wrong device.
> 
> Yes, after using the correct device I don't see the problem.
> 
> Can you please try the below patch on top of the 2 Patches that Martin sent and the new patch you sent?
> Once you confirm it works for you I can send the 2 patches officially.
It works on MTK's platform, I'll test it again when official patches are
sent out.

Thanks a lot
> 
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..04cc453 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>  		hcd->state = HC_STATE_SUSPENDED;
>  
>  		if (!PMSG_IS_AUTO(msg))
> -			usb_phy_roothub_suspend(hcd->phy_roothub);
> +			usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  
>  		/* Did we race with a root-hub wakeup event? */
>  		if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  	}
>  
>  	if (!PMSG_IS_AUTO(msg)) {
> -		status = usb_phy_roothub_resume(hcd->phy_roothub);
> +		status = usb_phy_roothub_resume(hcd->self.sysdev, hcd->phy_roothub);
>  		if (status)
>  			return status;
>  	}
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>  		}
>  	} else {
>  		hcd->state = old_state;
> -		usb_phy_roothub_suspend(hcd->phy_roothub);
> +		usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub);
>  		dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>  				"resume", status);
>  		if (status != -ESHUTDOWN)
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..25fe729 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>  			return PTR_ERR(phy);
>  	}
>  
> -	roothub_entry = usb_phy_roothub_alloc(dev);
> -	if (IS_ERR(roothub_entry))
> -		return PTR_ERR(roothub_entry);
> +	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +	if (!roothub_entry)
> +		return -ENOMEM;
>  
>  	roothub_entry->phy = phy;
>  
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>  	usb_phy_roothub_power_off(phy_roothub);
>  
>  	/* keep the PHYs initialized so the device can wake up the system */
> -	if (device_can_wakeup(dev))
> +	if (device_may_wakeup(dev))
>  		return 0;
>  
>  	return usb_phy_roothub_exit(phy_roothub);
> @@ -175,7 +175,7 @@ int usb_phy_roothub_resume(struct device *dev,
>  	int err;
>  
>  	/* if the device can't wake up the system _exit was called */
> -	if (device_can_wakeup(dev)) {
> +	if (!device_may_wakeup(dev)) {
>  		err = usb_phy_roothub_init(phy_roothub);
>  		if (err)
>  			return err;


--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Martin Blumenstingl March 24, 2018, 11:23 a.m. UTC | #19
Hi Roger,

On Wed, Mar 21, 2018 at 12:30 PM, Roger Quadros <rogerq@ti.com> wrote:
> Martin,
>
> On 21/03/18 00:01, Martin Blumenstingl wrote:
>> Hi Roger, Hi Chunfeng,
>>
>> On Tue, Mar 20, 2018 at 1:04 PM, Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>>> Hi Martin & Roger:
>>>
>>> On Mon, 2018-03-19 at 17:12 +0100, Martin Blumenstingl wrote:
>>>> Hi Roger,
>>>>
>>>> On Mon, Mar 19, 2018 at 9:49 AM, Roger Quadros <rogerq@ti.com> wrote:
>>>>> Hi,
>>>>>
>>>>> On 19/03/18 00:29, Martin Blumenstingl wrote:
>>>>>> Hi Roger,
>>>>>>
>>>>>> On Fri, Mar 16, 2018 at 3:32 PM, Roger Quadros <rogerq@ti.com> wrote:
>>>>>>> +some TI folks
>>>>>>>
>>>>>>> Hi Martin,
>>>>>>>
>>>>>>> On 18/02/18 20:44, Martin Blumenstingl wrote:
>>>>>>>> Many SoC platforms have separate devices for the USB PHY which are
>>>>>>>> registered through the generic PHY framework. These PHYs have to be
>>>>>>>> enabled to make the USB controller actually work. They also have to be
>>>>>>>> disabled again on shutdown/suspend.
>>>>>>>>
>>>>>>>> Currently (at least) the following HCI platform drivers are using custom
>>>>>>>> code to obtain all PHYs via devicetree for the roothub/controller and
>>>>>>>> disable/enable them when required:
>>>>>>>> - ehci-platform.c has ehci_platform_power_{on,off}
>>>>>>>> - xhci-mtk.c has xhci_mtk_phy_{init,exit,power_on,power_off}
>>>>>>>> - ohci-platform.c has ohci_platform_power_{on,off}
>>>>>>>>
>>>>>>>> With this new wrapper the USB PHYs can be specified directly in the
>>>>>>>> USB controller's devicetree node (just like on the drivers listed
>>>>>>>> above). This allows SoCs like the Amlogic Meson GXL family to operate
>>>>>>>> correctly once this is wired up correctly. These SoCs use a dwc3
>>>>>>>> controller and require all USB PHYs to be initialized (if one of the USB
>>>>>>>> PHYs it not initialized then none of USB port works at all).
>>>>>>>>
>>>>>>>> Signed-off-by: Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> Tested-by: Yixun Lan <yixun.lan@amlogic.com>
>>>>>>>> Cc: Neil Armstrong <narmstrong@baylibre.com>
>>>>>>>> Cc: Chunfeng Yun <chunfeng.yun@mediatek.com>
>>>>>>>
>>>>>>> This patch is breaking low power cases on TI SoCs when USB is in host mode.
>>>>>>> I'll explain why below.
>>>>>> based on your explanation and reading the TI PHY drivers I am assuming
>>>>>> that the affected SoCs are using the "phy-omap-usb2" driver
>>>>>>
>>>>> yes and phy-ti-pipe3 as well i.e. "ti,phy-usb3" and "ti,omap-usb3"
>>>> I missed that, thanks
>>>>
>>>>>>>> ---
>>>>>>>>  drivers/usb/core/Makefile |   2 +-
>>>>>>>>  drivers/usb/core/phy.c    | 158 ++++++++++++++++++++++++++++++++++++++++++++++
>>>>>>>>  drivers/usb/core/phy.h    |   7 ++
>>>>>>>>  3 files changed, 166 insertions(+), 1 deletion(-)
>>>>>>>>  create mode 100644 drivers/usb/core/phy.c
>>>>>>>>  create mode 100644 drivers/usb/core/phy.h
>>>>>>>>
>>>>>>>> diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
>>>>>>>> index 92c9cefb4317..18e874b0441e 100644
>>>>>>>> --- a/drivers/usb/core/Makefile
>>>>>>>> +++ b/drivers/usb/core/Makefile
>>>>>>>> @@ -6,7 +6,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
>>>>>>>> +usbcore-y += phy.o port.o
>>>>>>>>
>>>>>>>>  usbcore-$(CONFIG_OF)         += of.o
>>>>>>>>  usbcore-$(CONFIG_USB_PCI)            += hcd-pci.o
>>>>>>>> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..09b7c43c0ea4
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.c
>>>>>>>> @@ -0,0 +1,158 @@
>>>>>>>> +// SPDX-License-Identifier: GPL-2.0+
>>>>>>>> +/*
>>>>>>>> + * A wrapper for multiple PHYs which passes all phy_* function calls to
>>>>>>>> + * multiple (actual) PHY devices. This is comes handy when initializing
>>>>>>>> + * all PHYs on a HCD and to keep them all in the same state.
>>>>>>>> + *
>>>>>>>> + * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
>>>>>>>> + */
>>>>>>>> +
>>>>>>>> +#include <linux/device.h>
>>>>>>>> +#include <linux/list.h>
>>>>>>>> +#include <linux/phy/phy.h>
>>>>>>>> +#include <linux/of.h>
>>>>>>>> +
>>>>>>>> +#include "phy.h"
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub {
>>>>>>>> +     struct phy              *phy;
>>>>>>>> +     struct list_head        list;
>>>>>>>> +};
>>>>>>>> +
>>>>>>>> +static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
>>>>>>>> +     if (!roothub_entry)
>>>>>>>> +             return ERR_PTR(-ENOMEM);
>>>>>>>> +
>>>>>>>> +     INIT_LIST_HEAD(&roothub_entry->list);
>>>>>>>> +
>>>>>>>> +     return roothub_entry;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +static int usb_phy_roothub_add_phy(struct device *dev, int index,
>>>>>>>> +                                struct list_head *list)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
>>>>>>>> +
>>>>>>>> +     if (IS_ERR_OR_NULL(phy)) {
>>>>>>>> +             if (!phy || PTR_ERR(phy) == -ENODEV)
>>>>>>>> +                     return 0;
>>>>>>>> +             else
>>>>>>>> +                     return PTR_ERR(phy);
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     roothub_entry = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(roothub_entry))
>>>>>>>> +             return PTR_ERR(roothub_entry);
>>>>>>>> +
>>>>>>>> +     roothub_entry->phy = phy;
>>>>>>>> +
>>>>>>>> +     list_add_tail(&roothub_entry->list, list);
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +}
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *phy_roothub;
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int i, num_phys, err;
>>>>>>>> +
>>>>>>>> +     num_phys = of_count_phandle_with_args(dev->of_node, "phys",
>>>>>>>> +                                           "#phy-cells");
>>>>>>>> +     if (num_phys <= 0)
>>>>>>>> +             return NULL;
>>>>>>>> +
>>>>>>>> +     phy_roothub = usb_phy_roothub_alloc(dev);
>>>>>>>> +     if (IS_ERR(phy_roothub))
>>>>>>>> +             return phy_roothub;
>>>>>>>> +
>>>>>>>> +     for (i = 0; i < num_phys; i++) {
>>>>>>>> +             err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_init(roothub_entry->phy);
>>>>>>>
>>>>>>> The phy_init() function actually enables the PHY clocks.
>>>>>>> It should be moved to the usb_phy_roothub_exit() routine just before calling phy_power_on().
>>>>>> do you mean that phy_init should be moved to usb_phy_roothub_power_on
>>>>>> (just before phy_power_on is called within usb_phy_roothub_power_on)?
>>>>>>
>>>>>
>>>>> Yes.
>>>>>
>>>>>> an earlier version of my patch did exactly this, but it caused
>>>>>> problems during a suspend/resume cycle on Mediatek devices
>>>>>> Chunfeng Yun reported that issue here [0], quote from that mail for
>>>>>> easier reading:
>>>>>> "In order to keep link state on mt8173, we just power off all phys(not
>>>>>> exit) when system enter suspend, then power on them again (needn't
>>>>>> init, otherwise device will be disconnected) when system resume, this
>>>>>> can avoid re-enumerating device."
>>>>>>
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_exit_phys;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return phy_roothub;
>>>>>>>> +
>>>>>>>> +err_exit_phys:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_exit(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     return ERR_PTR(err);
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err, ret = 0;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_exit(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     ret = ret;
>>>>>>>> +     }
>>>>>>>
>>>>>>> phy_exit() should be moved to usb_phy_roothub_poweroff() just after calling phy_power_off().
>>>>>> if I understood Chunfeng Yun correctly this will require
>>>>>> re-enumeration of the USB devices after a suspend/resume cycle on
>>>>>> Mediatek SoCs
>>>>>>
>>>>>
>>>>> OK. I suppose that there are 2 cases
>>>>> 1) Mediatek's case: USB controller context retained across suspend/resume.
>>>>> Remote wakeup probably required.
>>>>> No re-enumeration preferred after resume. phy_exit()/phy_init() must not be called
>>>>> during suspend/resume to keep PHY link active.
>>>> Documentation/devicetree/bindings/usb/mediatek,mtu3.txt indeed shows
>>>> that the parent of the USB controller can be marked as "wakeup-source"
>>>>
>>>>> 2) TI's case: low power important: USB context is lost, OK to re-enumerate.
>>>>> phy_exit()/phy_init() must be called during suspend/resume.
>>>> ACK
>>>>
>>>>>>> With that there is nothing else being done here. Shouldn't we be doing the equivalent of
>>>>>>> usb_phy_roothub_del_phy() and usb_phy_roothub_free() here?
>>>>>>>
>>>>>>>> +
>>>>>>>> +     return ret;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +     struct list_head *head;
>>>>>>>> +     int err;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return 0;
>>>>>>>> +
>>>>>>>> +     head = &phy_roothub->list;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry(roothub_entry, head, list) {
>>>>>>>> +             err = phy_power_on(roothub_entry->phy);
>>>>>>>> +             if (err)
>>>>>>>> +                     goto err_out;
>>>>>>>> +     }
>>>>>>>> +
>>>>>>>> +     return 0;
>>>>>>>> +
>>>>>>>> +err_out:
>>>>>>>> +     list_for_each_entry_continue_reverse(roothub_entry, head, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>> +
>>>>>>>> +     return err;
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
>>>>>>>> +
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
>>>>>>>> +{
>>>>>>>> +     struct usb_phy_roothub *roothub_entry;
>>>>>>>> +
>>>>>>>> +     if (!phy_roothub)
>>>>>>>> +             return;
>>>>>>>> +
>>>>>>>> +     list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
>>>>>>>> +             phy_power_off(roothub_entry->phy);
>>>>>>>
>>>>>>> Not doing the phy_exit() here leaves the clocks enabled on our SoC and
>>>>>>> we're no longer able to reach low power states on system suspend.
>>>>>> I'm not sure where this problem should be solved:
>>>>>> - set skip_phy_initialization in struct usb_hcd to 1 for the affected
>>>>>> TI platforms
>>>>>
>>>>> Many TI platforms are affected, omap5*, dra7*, am43*
>>>>>
>>>>>> - fix this in the usb_phy_roothub code
>>>>>
>>>>> I'd vote for fixing it in the usb_phy_roothub code. How?
>>>>> How about using the device_can_wakeup() to decide if we should call phy_exit()/init() or not?
>>>>> If the USB device can't wakeup the system there is no point in keeping it powered/clocked right?
>>>> @Chunfeng: can you confirm Roger's idea that we could call phy_exit if
>>>> the controller is *NOT* marked as "wakeup-source"?
>>>> I am also not sure if it would work, since the "wakeup-source"
>>>> property is defined on the USB controller's parent node in case of the
>>>> Mediatek MTU3 (Mediatek USB3.0 DRD) controller
>>>>
>>> Very sorry, I forgot that MTU3 & xHCI drivers always set them as wakeup
>>> devices by device_init_wakeup(dev, true),but not dependent on
>>> "wakeup-source" property, so maybe we can use device_can_wakeup() to
>>> decide whether call phy_exit()/init() or not.
>> the 64-bit Amlogic Meson GXL/GXM SoCs don't support suspend/resume
>> yet, so I cannot test this
>> based on this suggestion I threw up two patches which are *compile
>> tested only* based on Greg's usb-next branch
>> you can find them here: [0] (as well as attached to this mail)
>>
>> @Chunfeng: can you please test this on one of your Mediatek SoCs?
>> @Roger: can you please test this on a TI SoC?
>>
>> (apologies in advance if these patches don't work)
>>
>> please note that I won't have access to my computer until Saturday.
>> if these patches need to be rewritten/replaced/etc. then feel free to
>> send your own version to the list
>
> Had to do the following to build
>
> diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
> index 6d4a419..2884607 100644
> --- a/drivers/usb/core/hcd.c
> +++ b/drivers/usb/core/hcd.c
> @@ -2262,7 +2262,7 @@ int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
>                 hcd->state = HC_STATE_SUSPENDED;
>
>                 if (!PMSG_IS_AUTO(msg))
> -                       usb_phy_roothub_suspend(hcd->phy_roothub);
> +                       usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>
>                 /* Did we race with a root-hub wakeup event? */
>                 if (rhdev->do_remote_wakeup) {
> @@ -2302,7 +2302,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>         }
>
>         if (!PMSG_IS_AUTO(msg)) {
> -               status = usb_phy_roothub_resume(hcd->phy_roothub);
> +               status = usb_phy_roothub_resume(&rhdev->dev, hcd->phy_roothub);
>                 if (status)
>                         return status;
>         }
> @@ -2344,7 +2344,7 @@ int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
>                 }
>         } else {
>                 hcd->state = old_state;
> -               usb_phy_roothub_suspend(hcd->phy_roothub);
> +               usb_phy_roothub_suspend(&rhdev->dev, hcd->phy_roothub);
>                 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
>                                 "resume", status);
>                 if (status != -ESHUTDOWN)
>
>
> And the following to fix runtime issues
>
> diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
> index 2eca371..8598906 100644
> --- a/drivers/usb/core/phy.c
> +++ b/drivers/usb/core/phy.c
> @@ -32,9 +32,9 @@ static int usb_phy_roothub_add_phy(struct device *dev, int index,
>                         return PTR_ERR(phy);
>         }
>
> -       roothub_entry = usb_phy_roothub_alloc(dev);
> -       if (IS_ERR(roothub_entry))
> -               return PTR_ERR(roothub_entry);
> +       roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
> +       if (!roothub_entry)
> +               return -ENOMEM;
>
>         roothub_entry->phy = phy;
>
> @@ -162,7 +162,7 @@ int usb_phy_roothub_suspend(struct device *dev,
>         usb_phy_roothub_power_off(phy_roothub);
>
>         /* keep the PHYs initialized so the device can wake up the system */
> -       if (device_can_wakeup(dev))
> +       if (device_may_wakeup(dev))
>                 return 0;
>
>         return usb_phy_roothub_exit(phy_roothub);
>
>
> Here are my obervations
> - if wakeup is disabled it works fine as expected, phy_exit() is called and I'm able to reach
> low power states.
>
> - if wakeup is enabled (/sys/bus/usb/device/usb2/power/wakeup), then hcd_bus_suspend() is never called
> and so phy_power_off won't be called either.
>
> This means that the device_may_wakeup() check is redundant. Sorry for suggesting this.
>
> This also means that wakeup is not enabled on Chunfeng's platform. Chunfeng, can you confirm this?
> What does /sys/bus/usb/device/usb<?>/power/wakeup say?
>
> Chunfeng sent a patch [1] to set shared_hcd->skip_phy_initialization for the mtk platform.
> Chunfeng, why did you need this patch?
>
> [1] - https://patchwork.kernel.org/patch/10298641/
>
> What options do we have now to fix the original issue?
>
> Chungfeng, can you set skip_phy_initialization for the hcd that has PHY's linked to it?
> Then usb_phy_roothub_*() driver will be a no-op for you. And we could revert to the original approach
> of doing phy_power_off() as well as phy_exit() during suspend.
>
> Alternatively, Martin, how about not relying on skip_phy_initialization flag but having default behaviour
> of no-op for the usb_phy_roothub_*() driver. As platforms migrate to it they can set a new
> flag in hcd to use it?
I added this code to add support for the USB controller on the Amlogic
Meson GXL and GXM SoCs:
these use a dwc3 controller for host mode

so while this approach would reduce the risk of breaking other
controllers your TI SoCs would still be "affected" since I would like
to enable the new logic for the dwc3 controller (which the TI SoCs use
as well).

I will come up with an RFC patch series based on your and Chunfeng's suggestions
thank you very much for working it out while I was away!

>>
>>>>>> - fix this in the PHY driver
>>>>>
>>>>> There is nothing to fix in the PHY driver. It is doing what it is supposed to do.
>>>> I actually wonder if phy_ops should have explicit suspend/resume support:
>>>> - assuming we define two new callbacks: .suspend and .resume
>>>> - the PHY framework could call .power_off by default if .suspend is not defined
>>>> - the PHY framework could call .power_on by default if .resume is not defined
>>>> - drivers could set .suspend and .resume on their own, allowing more
>>>> fine-grained control by for example *only* stopping the clock (but not
>>>> re-initializing the registers, etc.)
>>>>
>>>> @Roger: what do you think about this?
>>>> Kishon (the PHY framework maintainer) is also CC'ed - I would like to
>>>> hear his opinion too
>>>>
>>>>>> - somewhere else
>>>>>>
>>>>>>>> +}
>>>>>>>> +EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
>>>>>>>> diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
>>>>>>>> new file mode 100644
>>>>>>>> index 000000000000..6fde59bfbff8
>>>>>>>> --- /dev/null
>>>>>>>> +++ b/drivers/usb/core/phy.h
>>>>>>>> @@ -0,0 +1,7 @@
>>>>>>>> +struct usb_phy_roothub;
>>>>>>>> +
>>>>>>>> +struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
>>>>>>>> +int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +
>>>>>>>> +int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
>>>>>>>> +void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);
>>>>>>>>
>>>>>>>
>>>>>
>
>>
>> [0] https://github.com/xdarklight/linux/commits/usb-phy-roothub-suspend-rfc-v1
>>
>
> --
> cheers,
> -roger
>
> Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki


Regards
Martin
--
To unsubscribe from this list: send the line "unsubscribe linux-tegra" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox series

Patch

diff --git a/drivers/usb/core/Makefile b/drivers/usb/core/Makefile
index 92c9cefb4317..18e874b0441e 100644
--- a/drivers/usb/core/Makefile
+++ b/drivers/usb/core/Makefile
@@ -6,7 +6,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
+usbcore-y += phy.o port.o
 
 usbcore-$(CONFIG_OF)		+= of.o
 usbcore-$(CONFIG_USB_PCI)		+= hcd-pci.o
diff --git a/drivers/usb/core/phy.c b/drivers/usb/core/phy.c
new file mode 100644
index 000000000000..09b7c43c0ea4
--- /dev/null
+++ b/drivers/usb/core/phy.c
@@ -0,0 +1,158 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * A wrapper for multiple PHYs which passes all phy_* function calls to
+ * multiple (actual) PHY devices. This is comes handy when initializing
+ * all PHYs on a HCD and to keep them all in the same state.
+ *
+ * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
+ */
+
+#include <linux/device.h>
+#include <linux/list.h>
+#include <linux/phy/phy.h>
+#include <linux/of.h>
+
+#include "phy.h"
+
+struct usb_phy_roothub {
+	struct phy		*phy;
+	struct list_head	list;
+};
+
+static struct usb_phy_roothub *usb_phy_roothub_alloc(struct device *dev)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	roothub_entry = devm_kzalloc(dev, sizeof(*roothub_entry), GFP_KERNEL);
+	if (!roothub_entry)
+		return ERR_PTR(-ENOMEM);
+
+	INIT_LIST_HEAD(&roothub_entry->list);
+
+	return roothub_entry;
+}
+
+static int usb_phy_roothub_add_phy(struct device *dev, int index,
+				   struct list_head *list)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct phy *phy = devm_of_phy_get_by_index(dev, dev->of_node, index);
+
+	if (IS_ERR_OR_NULL(phy)) {
+		if (!phy || PTR_ERR(phy) == -ENODEV)
+			return 0;
+		else
+			return PTR_ERR(phy);
+	}
+
+	roothub_entry = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(roothub_entry))
+		return PTR_ERR(roothub_entry);
+
+	roothub_entry->phy = phy;
+
+	list_add_tail(&roothub_entry->list, list);
+
+	return 0;
+}
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev)
+{
+	struct usb_phy_roothub *phy_roothub;
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int i, num_phys, err;
+
+	num_phys = of_count_phandle_with_args(dev->of_node, "phys",
+					      "#phy-cells");
+	if (num_phys <= 0)
+		return NULL;
+
+	phy_roothub = usb_phy_roothub_alloc(dev);
+	if (IS_ERR(phy_roothub))
+		return phy_roothub;
+
+	for (i = 0; i < num_phys; i++) {
+		err = usb_phy_roothub_add_phy(dev, i, &phy_roothub->list);
+		if (err)
+			goto err_out;
+	}
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_init(roothub_entry->phy);
+		if (err)
+			goto err_exit_phys;
+	}
+
+	return phy_roothub;
+
+err_exit_phys:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_exit(roothub_entry->phy);
+
+err_out:
+	return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_init);
+
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err, ret = 0;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_exit(roothub_entry->phy);
+		if (err)
+			ret = ret;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_exit);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+	struct list_head *head;
+	int err;
+
+	if (!phy_roothub)
+		return 0;
+
+	head = &phy_roothub->list;
+
+	list_for_each_entry(roothub_entry, head, list) {
+		err = phy_power_on(roothub_entry->phy);
+		if (err)
+			goto err_out;
+	}
+
+	return 0;
+
+err_out:
+	list_for_each_entry_continue_reverse(roothub_entry, head, list)
+		phy_power_off(roothub_entry->phy);
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_on);
+
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub)
+{
+	struct usb_phy_roothub *roothub_entry;
+
+	if (!phy_roothub)
+		return;
+
+	list_for_each_entry_reverse(roothub_entry, &phy_roothub->list, list)
+		phy_power_off(roothub_entry->phy);
+}
+EXPORT_SYMBOL_GPL(usb_phy_roothub_power_off);
diff --git a/drivers/usb/core/phy.h b/drivers/usb/core/phy.h
new file mode 100644
index 000000000000..6fde59bfbff8
--- /dev/null
+++ b/drivers/usb/core/phy.h
@@ -0,0 +1,7 @@ 
+struct usb_phy_roothub;
+
+struct usb_phy_roothub *usb_phy_roothub_init(struct device *dev);
+int usb_phy_roothub_exit(struct usb_phy_roothub *phy_roothub);
+
+int usb_phy_roothub_power_on(struct usb_phy_roothub *phy_roothub);
+void usb_phy_roothub_power_off(struct usb_phy_roothub *phy_roothub);