Skip to contentSkip to conversation

phy: Track power-on and init counts in uclass

PATCH Superseded Alper Nebi Yasak 4 participants 8 messages Dec 24, 2021 – Dec 31, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 phy: Track power-on and init counts in uclass

On boards using the RK3399 SoC, the USB OHCI and EHCI controllers share the same PHY device instance. While these controllers are being stopped they both attempt to power-off and deinitialize it, but trying to power-off the deinitialized PHY device results in a hang. This usually happens just before booting an OS, and can be explicitly triggered by running "usb start; usb stop" in the U-Boot shell.

Implement a uclass-wide counting mechanism for PHY initialization and power state change requests, so that we don't power-off/deinitialize a PHY instance until all of its users want it done. The Allwinner A10 USB PHY driver does this counting in-driver, remove those parts in favour of this in-uclass implementation.

The sandbox PHY operations test needs some changes since the uclass will no longer call into the drivers for actions matching its tracked state (e.g. powering-off a powered-off PHY). Update that test, and add a new one which simulates multiple users of a single PHY.

The major complication here is that PHY handles aren't deduplicated per instance, so the obvious idea of putting the counts in the PHY handles don't immediately work. It seems possible to bind a child udevice per PHY instance to the PHY provider and deduplicate the handles in each child's uclass-private areas, like in the CLK framework. An alternative approach could be to use those bound child udevices themselves as the PHY handles. Instead, to avoid the architectural changes those would require, this patch solves things by dynamically allocating a list of structs (one per instance) in the provider's uclass-private area.

---

Changes in v2:
- Rename {phy_,}id_priv -> {phy_,}counts
- Split phy_get_uclass_priv -> phy_{alloc,get}_counts
- Allocate counts (or return error) in generic_phy_get_by_*()
- Remove now-unnecessary null checks for counts of valid phy handles

v1: https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

 drivers/phy/allwinner/phy-sun4i-usb.c |   9 --
 drivers/phy/phy-uclass.c              | 120 ++++++++++++++++++++++++++
 test/dm/phy.c                         |  83 ++++++++++++++++--
 3 files changed, 198 insertions(+), 14 deletions(-)
drivers/phy/allwinner/phy-sun4i-usb.c +0 -9
index ab2a5d17fcff..86c589a65fd3 100644
@@ -125,7 +125,6 @@ struct sun4i_usb_phy_info {
 
 struct sun4i_usb_phy_plat {
 	void __iomem *pmu;
-	int power_on_count;
 	int gpio_vbus;
 	int gpio_vbus_det;
 	int gpio_id_det;
@@ -225,10 +224,6 @@ static int sun4i_usb_phy_power_on(struct phy *phy)
 		initial_usb_scan_delay = 0;
 	}
 
-	usb_phy->power_on_count++;
-	if (usb_phy->power_on_count != 1)
-		return 0;
-
 	if (usb_phy->gpio_vbus >= 0)
 		gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_UP);
 
@@ -240,10 +235,6 @@ static int sun4i_usb_phy_power_off(struct phy *phy)
 	struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
 	struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
 
-	usb_phy->power_on_count--;
-	if (usb_phy->power_on_count != 0)
-		return 0;
-
 	if (usb_phy->gpio_vbus >= 0)
 		gpio_set_value(usb_phy->gpio_vbus, SUNXI_GPIO_PULL_DISABLE);
 
drivers/phy/phy-uclass.c +120 -0

206 lines not on this page · raw message

test/dm/phy.c +78 -5

107 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw

Hi Alper,

Quoted 38 lines · Alper Nebi Yasak

On boards using the RK3399 SoC, the USB OHCI and EHCI controllers share the same PHY device instance. While these controllers are being stopped they both attempt to power-off and deinitialize it, but trying to power-off the deinitialized PHY device results in a hang. This usually happens just before booting an OS, and can be explicitly triggered by running "usb start; usb stop" in the U-Boot shell.

Implement a uclass-wide counting mechanism for PHY initialization and power state change requests, so that we don't power-off/deinitialize a PHY instance until all of its users want it done. The Allwinner A10 USB PHY driver does this counting in-driver, remove those parts in favour of this in-uclass implementation.

The sandbox PHY operations test needs some changes since the uclass will no longer call into the drivers for actions matching its tracked state (e.g. powering-off a powered-off PHY). Update that test, and add a new one which simulates multiple users of a single PHY.

The major complication here is that PHY handles aren't deduplicated per instance, so the obvious idea of putting the counts in the PHY handles don't immediately work. It seems possible to bind a child udevice per PHY instance to the PHY provider and deduplicate the handles in each child's uclass-private areas, like in the CLK framework. An alternative approach could be to use those bound child udevices themselves as the PHY handles. Instead, to avoid the architectural changes those would require, this patch solves things by dynamically allocating a list of structs (one per instance) in the provider's uclass-private area.

---

Changes in v2:
- Rename {phy_,}id_priv -> {phy_,}counts
- Split phy_get_uclass_priv -> phy_{alloc,get}_counts
- Allocate counts (or return error) in generic_phy_get_by_*()
- Remove now-unnecessary null checks for counts of valid phy handles

Quoted 6 lines

v1: https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

 drivers/phy/allwinner/phy-sun4i-usb.c |   9 --
 drivers/phy/phy-uclass.c              | 120 ++++++++++++++++++++++++++
 test/dm/phy.c                         |  83 ++++++++++++++++--
 3 files changed, 198 insertions(+), 14 deletions(-)

Should add comments for the struct

Also I wonder if a simple fixed-length array might be possible instead of the linked list?

#2 lore raw
Quoted 48 lines · Simon Glass

Hi Alper,

Quoted 38 lines · Alper Nebi Yasak

On boards using the RK3399 SoC, the USB OHCI and EHCI controllers share the same PHY device instance. While these controllers are being stopped they both attempt to power-off and deinitialize it, but trying to power-off the deinitialized PHY device results in a hang. This usually happens just before booting an OS, and can be explicitly triggered by running "usb start; usb stop" in the U-Boot shell.

Implement a uclass-wide counting mechanism for PHY initialization and power state change requests, so that we don't power-off/deinitialize a PHY instance until all of its users want it done. The Allwinner A10 USB PHY driver does this counting in-driver, remove those parts in favour of this in-uclass implementation.

The sandbox PHY operations test needs some changes since the uclass will no longer call into the drivers for actions matching its tracked state (e.g. powering-off a powered-off PHY). Update that test, and add a new one which simulates multiple users of a single PHY.

The major complication here is that PHY handles aren't deduplicated per instance, so the obvious idea of putting the counts in the PHY handles don't immediately work. It seems possible to bind a child udevice per PHY instance to the PHY provider and deduplicate the handles in each child's uclass-private areas, like in the CLK framework. An alternative approach could be to use those bound child udevices themselves as the PHY handles. Instead, to avoid the architectural changes those would require, this patch solves things by dynamically allocating a list of structs (one per instance) in the provider's uclass-private area.

---

Changes in v2:
- Rename {phy_,}id_priv -> {phy_,}counts
- Split phy_get_uclass_priv -> phy_{alloc,get}_counts
- Allocate counts (or return error) in generic_phy_get_by_*()
- Remove now-unnecessary null checks for counts of valid phy handles

Quoted 6 lines

v1: https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

 drivers/phy/allwinner/phy-sun4i-usb.c |   9 --
 drivers/phy/phy-uclass.c              | 120 ++++++++++++++++++++++++++
 test/dm/phy.c                         |  83 ++++++++++++++++--
 3 files changed, 198 insertions(+), 14 deletions(-)
Quoted 6 lines

Should add comments for the struct

Also I wonder if a simple fixed-length array might be possible instead of the linked list?

Thanks for the review Simon. Since I think this should unblock some common hardware, does everyone think this should be safe enough to pull in now, or no, I shouldn't bend the rules, and take this for v2021.04 instead?

Signature · 1 line
Tom
#3 lore raw

Hi Tom,

Quoted 54 lines · Tom Rini
Quoted 52 lines · Simon Glass

Hi Alper,

Quoted 38 lines · Alper Nebi Yasak

On boards using the RK3399 SoC, the USB OHCI and EHCI controllers share the same PHY device instance. While these controllers are being stopped they both attempt to power-off and deinitialize it, but trying to power-off the deinitialized PHY device results in a hang. This usually happens just before booting an OS, and can be explicitly triggered by running "usb start; usb stop" in the U-Boot shell.

Implement a uclass-wide counting mechanism for PHY initialization and power state change requests, so that we don't power-off/deinitialize a PHY instance until all of its users want it done. The Allwinner A10 USB PHY driver does this counting in-driver, remove those parts in favour of this in-uclass implementation.

The sandbox PHY operations test needs some changes since the uclass will no longer call into the drivers for actions matching its tracked state (e.g. powering-off a powered-off PHY). Update that test, and add a new one which simulates multiple users of a single PHY.

The major complication here is that PHY handles aren't deduplicated per instance, so the obvious idea of putting the counts in the PHY handles don't immediately work. It seems possible to bind a child udevice per PHY instance to the PHY provider and deduplicate the handles in each child's uclass-private areas, like in the CLK framework. An alternative approach could be to use those bound child udevices themselves as the PHY handles. Instead, to avoid the architectural changes those would require, this patch solves things by dynamically allocating a list of structs (one per instance) in the provider's uclass-private area.

---

Changes in v2:
- Rename {phy_,}id_priv -> {phy_,}counts
- Split phy_get_uclass_priv -> phy_{alloc,get}_counts
- Allocate counts (or return error) in generic_phy_get_by_*()
- Remove now-unnecessary null checks for counts of valid phy handles

Quoted 6 lines

v1: https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

 drivers/phy/allwinner/phy-sun4i-usb.c |   9 --
 drivers/phy/phy-uclass.c              | 120 ++++++++++++++++++++++++++
 test/dm/phy.c                         |  83 ++++++++++++++++--
 3 files changed, 198 insertions(+), 14 deletions(-)

Should add comments for the struct

Quoted 7 lines
Quoted 2 lines

Also I wonder if a simple fixed-length array might be possible instead of the linked list?

Thanks for the review Simon. Since I think this should unblock some common hardware, does everyone think this should be safe enough to pull in now, or no, I shouldn't bend the rules, and take this for v2021.04 instead?

It looks safe to me and it has a test. If there are boards destined for this release that need it, then I have no objection.

Regards,
Simon

#4 lore raw
Quoted 8 lines · Simon Glass
Quoted 4 lines · Tom Rini

Thanks for the review Simon. Since I think this should unblock some common hardware, does everyone think this should be safe enough to pull in now, or no, I shouldn't bend the rules, and take this for v2021.04 instead?

It looks safe to me and it has a test. If there are boards destined for this release that need it, then I have no objection.

I'm fine with either option. I guess most people already know and use the rockchip-inno-usb2 patch to get around this, so I don't exactly consider this urgent, especially since I have bigger problems with my board.

#5 lore raw
In reply to Tom Rini
Quoted 54 lines · Tom Rini
Quoted 52 lines · Simon Glass

Hi Alper,

Quoted 38 lines · Alper Nebi Yasak

On boards using the RK3399 SoC, the USB OHCI and EHCI controllers share the same PHY device instance. While these controllers are being stopped they both attempt to power-off and deinitialize it, but trying to power-off the deinitialized PHY device results in a hang. This usually happens just before booting an OS, and can be explicitly triggered by running "usb start; usb stop" in the U-Boot shell.

Implement a uclass-wide counting mechanism for PHY initialization and power state change requests, so that we don't power-off/deinitialize a PHY instance until all of its users want it done. The Allwinner A10 USB PHY driver does this counting in-driver, remove those parts in favour of this in-uclass implementation.

The sandbox PHY operations test needs some changes since the uclass will no longer call into the drivers for actions matching its tracked state (e.g. powering-off a powered-off PHY). Update that test, and add a new one which simulates multiple users of a single PHY.

The major complication here is that PHY handles aren't deduplicated per instance, so the obvious idea of putting the counts in the PHY handles don't immediately work. It seems possible to bind a child udevice per PHY instance to the PHY provider and deduplicate the handles in each child's uclass-private areas, like in the CLK framework. An alternative approach could be to use those bound child udevices themselves as the PHY handles. Instead, to avoid the architectural changes those would require, this patch solves things by dynamically allocating a list of structs (one per instance) in the provider's uclass-private area.

---

Changes in v2:
- Rename {phy_,}id_priv -> {phy_,}counts
- Split phy_get_uclass_priv -> phy_{alloc,get}_counts
- Allocate counts (or return error) in generic_phy_get_by_*()
- Remove now-unnecessary null checks for counts of valid phy handles

Quoted 6 lines

v1: https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

 drivers/phy/allwinner/phy-sun4i-usb.c |   9 --
 drivers/phy/phy-uclass.c              | 120 ++++++++++++++++++++++++++
 test/dm/phy.c                         |  83 ++++++++++++++++--
 3 files changed, 198 insertions(+), 14 deletions(-)

Should add comments for the struct

Quoted 7 lines
Quoted 2 lines

Also I wonder if a simple fixed-length array might be possible instead of the linked list?

Thanks for the review Simon. Since I think this should unblock some common hardware, does everyone think this should be safe enough to pull in now, or no, I shouldn't bend the rules, and take this for v2021.04 instead?

We've had a number of queries each release around this problem so it maybe worthwhile to include, I think that most distros that pre-build things for users are aware of it enough that they've had local patches for some time so personally for me I'm OK either way.

Peter

#6 lore raw
In reply to Simon Glass
Quoted 1 line · Simon Glass

Should add comments for the struct

I can do that and send as a v3.

Quoted 2 lines

Also I wonder if a simple fixed-length array might be possible instead of the linked list?

I think it's possible, my first prototype was something like:

  #define MAX_PHYS 16

  struct phy_uc_priv {
      int power_on_count[MAX_PHYS];
      int init_count[MAX_PHYS];
  };

  uc_priv = dev_get_uclass_priv(phy->dev);
  uc_priv->power_on_count[phy->id];
  uc_priv->init_count[phy->id];

  UCLASS_DRIVER(phy) = {
      ...
      .per_device_auto = sizeof(struct phy_uc_priv),
  }

But I rewrote as a linked list because I couldn't decide on a reasonable value for that MAX_PHYS. Best guess I have is from drivers/phy/cadence/phy-cadence-sierra.c which defines SIERRA_MAX_LANES as 16.

As array-of-structs, I guess things would be roughly:

  #define MAX_PHYS 16

  struct phy_counts {
      int power_on_count;
      int init_count;
  };

  counts = dev_get_uclass_priv(phy->dev)[phy->id];
  counts->power_on_count;
  counts->init_count;

  UCLASS_DRIVER(phy) = {
      ...
      .per_device_auto = sizeof(struct phy_counts[MAX_PHYS]),
  }

Should I change to that?

#7 lore raw

Hi Alper,

Quoted 27 lines · Alper Nebi Yasak
Quoted 1 line · Simon Glass

Should add comments for the struct

I can do that and send as a v3.

Quoted 2 lines

Also I wonder if a simple fixed-length array might be possible instead of the linked list?

I think it's possible, my first prototype was something like:

  #define MAX_PHYS 16

  struct phy_uc_priv {
      int power_on_count[MAX_PHYS];
      int init_count[MAX_PHYS];
  };

  uc_priv = dev_get_uclass_priv(phy->dev);
  uc_priv->power_on_count[phy->id];
  uc_priv->init_count[phy->id];

  UCLASS_DRIVER(phy) = {
      ...
      .per_device_auto = sizeof(struct phy_uc_priv),
  }
Quoted 4 lines

But I rewrote as a linked list because I couldn't decide on a reasonable value for that MAX_PHYS. Best guess I have is from drivers/phy/cadence/phy-cadence-sierra.c which defines SIERRA_MAX_LANES as 16.

You could make it a CONFIG.

I was more thinking of calculating the size and then allocating it in one hit, for simplicity. But given that you have something more flexible and it seems to be needed, let's leave it as it is.

Quoted 20 lines

As array-of-structs, I guess things would be roughly:

  #define MAX_PHYS 16

  struct phy_counts {
      int power_on_count;
      int init_count;
  };

  counts = dev_get_uclass_priv(phy->dev)[phy->id];
  counts->power_on_count;
  counts->init_count;

  UCLASS_DRIVER(phy) = {
      ...
      .per_device_auto = sizeof(struct phy_counts[MAX_PHYS]),
  }
Quoted 1 line

Should I change to that?

I really like driver model doing the allocation. But I think if you did that, it would be better to have a struct that included the array, so we can have a simple sizeof(struct xxx) there.

Regards,
Simon