Skip to contentSkip to conversation

DT MDIO bus of fixed phys

RFC PATCH In review Andrew Lunn 2 participants 12 messages Mar 11, 2016 – Mar 14, 2016

Subsystems (5)
lore
Share & tools
lore
author maintainer
#2 lore raw

RFC PATCH 2/2 phy: fixed-phy: Allow DT description of an MDIO bus and PHYs.

In reply to Andrew Lunn

Not all MACs are connected to PHYs. They can for example be connected to a switch. Using the fixed PHY properties with the MAC is possible, but requires support in the MAC. It is however simpler to make use of the phy-handle property to point to a PHY. To achieve this, the PHY must be in the device tree.

Allow virtual MDIO busses to be represented in device tree, which contains virtual fixed-phys.

---
 .../devicetree/bindings/net/fixed-link.txt         |  39 +++++++
 drivers/net/phy/fixed_phy.c                        | 122 ++++++++++++++++++++-
 2 files changed, 157 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/net/fixed-link.txt +39 -0
index ec5d889fe3d8..2a22b92007fa 100644
@@ -52,3 +52,42 @@ ethernet@1 {
 	};
 	...
 };
+
+Fixed link PHYs on an MDIO bus
+------------------------------
+
+An alternative to using the fixed link properties in the MAC is to
+define an MDIO bus with a number of fixed link phys on it.
+
+Required properties:
+- compatible = ""linux,mdio-fixed-phy";
+- #address-cells = <1>;
+- #size-cells = <0>;
+
+Child nodes represent PHYs on this mdio bus. Standard properties for
+fixed links, 'speed', 'full-duplex', 'pause', 'asym-pause',
+'link-gpios', as defined above are used. Additionally a 'reg' property
+is required for the address of the PHY on the bus. This should be of
+value 0 to 31.
+
+Example
+-------
+
+mdio {
+	compatible = "linux,mdio-fixed-phy";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	phy0: phy@0 {
+		reg = <0>;
+		speed = <1000>;
+		pause;
+		link-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
+	};
+
+	phy1: phy@8 {
+		reg = <8>;
+		speed = <100>;
+		full-duplex;
+	};
+};
drivers/net/phy/fixed_phy.c +118 -4

162 lines not on this page · raw message

Signature · 1 line
2.7.0
author maintainer
#1 lore raw

RFC PATCH 1/2 of: of_mdio: Factor out fixed-link parsing

In reply to Andrew Lunn

Turn the parsing of the fixed link properties into a helper function, and export it for others to use.

---
 drivers/of/of_mdio.c    | 34 +++++++++++++++++++++-------------
 include/linux/of_mdio.h | 11 ++++++++++-
 2 files changed, 31 insertions(+), 14 deletions(-)
drivers/of/of_mdio.c +21 -13
index 5e7838290998..e9ad328174ff 100644
@@ -397,6 +397,23 @@ bool of_phy_is_fixed_link(struct device_node *np)
 }
 EXPORT_SYMBOL(of_phy_is_fixed_link);
 
+int of_phy_parse_fixed_link(struct device_node *np,
+			    struct fixed_phy_status *status,
+			    int *link_gpio)
+{
+	status->link = 1;
+	status->duplex = of_property_read_bool(np, "full-duplex");
+	if (of_property_read_u32(np, "speed", &status->speed))
+		return -EINVAL;
+	status->pause = of_property_read_bool(np, "pause");
+	status->asym_pause = of_property_read_bool(np, "asym-pause");
+	*link_gpio = of_get_named_gpio_flags(np, "link-gpios", 0, NULL);
+	if (*link_gpio == -EPROBE_DEFER)
+		return -EPROBE_DEFER;
+	return 0;
+}
+EXPORT_SYMBOL(of_phy_parse_fixed_link);
+
 int of_phy_register_fixed_link(struct device_node *np)
 {
 	struct fixed_phy_status status = {};
@@ -419,20 +436,11 @@ int of_phy_register_fixed_link(struct device_node *np)
 	/* New binding */
 	fixed_link_node = of_get_child_by_name(np, "fixed-link");
 	if (fixed_link_node) {
-		status.link = 1;
-		status.duplex = of_property_read_bool(fixed_link_node,
-						      "full-duplex");
-		if (of_property_read_u32(fixed_link_node, "speed", &status.speed))
-			return -EINVAL;
-		status.pause = of_property_read_bool(fixed_link_node, "pause");
-		status.asym_pause = of_property_read_bool(fixed_link_node,
-							  "asym-pause");
-		link_gpio = of_get_named_gpio_flags(fixed_link_node,
-						    "link-gpios", 0, NULL);
+		err = of_phy_parse_fixed_link(fixed_link_node, &status,
+					      &link_gpio);
 		of_node_put(fixed_link_node);
-		if (link_gpio == -EPROBE_DEFER)
-			return -EPROBE_DEFER;
-
+		if (err)
+			return err;
 		phy = fixed_phy_register(PHY_POLL, &status, link_gpio, np);
 		return IS_ERR(phy) ? PTR_ERR(phy) : 0;
 	}
include/linux/of_mdio.h +10 -1
index 8f2237eb3485..1286a76dbcf0 100644
@@ -10,6 +10,7 @@
 #define __LINUX_OF_MDIO_H
 
 #include <linux/phy.h>
+#include <linux/phy_fixed.h>
 #include <linux/of.h>
 
 #ifdef CONFIG_OF
@@ -25,7 +26,6 @@ struct phy_device *of_phy_attach(struct net_device *dev,
 
 extern struct mii_bus *of_mdio_find_bus(struct device_node *mdio_np);
 extern int of_mdio_parse_addr(struct device *dev, const struct device_node *np);
-
 #else /* CONFIG_OF */
 static inline int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
 {
@@ -72,6 +72,9 @@ static inline int of_mdio_parse_addr(struct device *dev,
 #if defined(CONFIG_OF) && defined(CONFIG_FIXED_PHY)
 extern int of_phy_register_fixed_link(struct device_node *np);
 extern bool of_phy_is_fixed_link(struct device_node *np);
+extern int of_phy_parse_fixed_link(struct device_node *np,
+				   struct fixed_phy_status *status,
+				   int *link_gpio);
 #else
 static inline int of_phy_register_fixed_link(struct device_node *np)
 {
@@ -81,6 +84,12 @@ static inline bool of_phy_is_fixed_link(struct device_node *np)
 {
 	return false;
 }
+static inline int of_phy_parse_fixed_link(struct device_node *np,
+				   struct fixed_phy_status *status,
+				   int *link_gpio)
+{
+	return -ENOSYS;
+}
 #endif
 
 
Signature · 1 line
2.7.0

Andrew Lunn

author maintainer
#0 lore raw

cover DT MDIO bus of fixed phys

Currently, supporting a fixed-phy in a MAC driver is a bit messy. It needs to be explicit supported, since a fixed phy is somewhat different from a normal phy.

These two patches solve this by making fixed-phys appear as normal PHYs within device tree, allowing them to be referenced by a phandle. Any MAC driver that supports phy-handle can then automatically support a fixed-phy without any code change.

Andrew Lunn (2):
of: of_mdio: Factor out fixed-link parsing
phy: fixed-phy: Allow DT description of an MDIO bus and PHYs.

 .../devicetree/bindings/net/fixed-link.txt         |  39 +++++++
 drivers/net/phy/fixed_phy.c                        | 122 ++++++++++++++++++++-
 drivers/of/of_mdio.c                               |  34 +++---
 include/linux/of_mdio.h                            |  11 +-
 4 files changed, 188 insertions(+), 18 deletions(-)
Signature · 1 line
2.7.0
#6 lore raw
Quoted 8 lines · Andrew Lunn

Currently, supporting a fixed-phy in a MAC driver is a bit messy. It needs to be explicit supported, since a fixed phy is somewhat different from a normal phy.

These two patches solve this by making fixed-phys appear as normal PHYs within device tree, allowing them to be referenced by a phandle. Any MAC driver that supports phy-handle can then automatically support a fixed-phy without any code change.

Humm, if that's the problem we want to solve, we could introduce a helper function which tries to locate the phy using a 'phy-handle' property, and if it fails falls back to looking for a fixed-link property and use that if desired. That behavior could either be automatic, or controlled via boolean flag for instance which indicates whether fallback is appropriate?

I am not sure the virtual MDIO bus is looking much better in that regard, it does provide a good model for how the fixed PHYs are
implemented: as a virtual MDIO bus, but that seems quite a bit of a stretch here...

Signature · 1 line
Florian
#3 lore raw

Re: [RFC PATCH net-next 2/2] phy: fixed-phy: Allow DT description of an MDIO bus and PHYs.

In reply to Andrew Lunn
Quoted 22 lines · Andrew Lunn

Not all MACs are connected to PHYs. They can for example be connected to a switch. Using the fixed PHY properties with the MAC is possible, but requires support in the MAC. It is however simpler to make use of the phy-handle property to point to a PHY. To achieve this, the PHY must be in the device tree.

Allow virtual MDIO busses to be represented in device tree, which contains virtual fixed-phys.

---
 .../devicetree/bindings/net/fixed-link.txt         |  39 +++++++
 drivers/net/phy/fixed_phy.c                        | 122 ++++++++++++++++++++-
 2 files changed, 157 insertions(+), 4 deletions(-)
Documentation/devicetree/bindings/net/fixed-link.txt +0 -0
index ec5d889fe3d8..2a22b92007fa 100644
@@ -52,3 +52,42 @@ ethernet@1 {
 	};
...

Documentation/devicetree/bindings/net/fixed-link.txt ethernet@1 { · line 63

Quoted 10 lines
Documentation/devicetree/bindings/net/fixed-link.txt +9 -0
 };
+
+Fixed link PHYs on an MDIO bus
+------------------------------
+
+An alternative to using the fixed link properties in the MAC is to
+define an MDIO bus with a number of fixed link phys on it.
+
+Required properties:
+- compatible = ""linux,mdio-fixed-phy";

One too many " here.

Documentation/devicetree/bindings/net/fixed-link.txt ethernet@1 { · line 71

Quoted 8 lines
+- #address-cells = <1>;
+- #size-cells = <0>;
Documentation/devicetree/bindings/net/fixed-link.txt +6 -0
+
+Child nodes represent PHYs on this mdio bus. Standard properties for
+fixed links, 'speed', 'full-duplex', 'pause', 'asym-pause',
+'link-gpios', as defined above are used. Additionally a 'reg' property
+is required for the address of the PHY on the bus. This should be of
+value 0 to 31.

This is a virtual bus, the only limitation is because we re-use to the maximum permission extent the real MDIO bus code, and this is putting a SW constraint on something that does not have one here.

Quoted 11 lines
Documentation/devicetree/bindings/net/fixed-link.txt +11 -0
+
+Example
+-------
+
+mdio {
+	compatible = "linux,mdio-fixed-phy";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	phy0: phy@0 {
+		reg = <0>;

Documentation/devicetree/bindings/net/fixed-link.txt ethernet@1 { · line 92

Quoted 10 lines
Documentation/devicetree/bindings/net/fixed-link.txt +10 -0
+		speed = <1000>;
+		pause;
+		link-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
+	};
+
+	phy1: phy@8 {
+		reg = <8>;
+		speed = <100>;
+		full-duplex;
+	};

The 'fixed-link' property, although it suffers from one basic issue which is that it is placed on the consumer of it (e.g: an Ethernet MAC Device Tree node) while being a two-headed snake (you actually describe a data-pipe with a fixed-link) is not quite perfect, but this does not seem to look any better.

One thing that Thomas solved nicely was avoid the need for allocating an address on the virtual MDIO bus, but this is coming back here, which does not seem needed except for correctness wrt. how real MDIO buses.

Signature · 1 line
Florian
author maintainer
#7 lore raw
In reply to Florian Fainelli
Quoted 5 lines · Florian Fainelli
Quoted 4 lines · Andrew Lunn

Currently, supporting a fixed-phy in a MAC driver is a bit messy. It needs to be explicit supported, since a fixed phy is somewhat different from a normal phy.

Quoted 8 lines
Quoted 4 lines

These two patches solve this by making fixed-phys appear as normal PHYs within device tree, allowing them to be referenced by a phandle. Any MAC driver that supports phy-handle can then automatically support a fixed-phy without any code change.

Humm, if that's the problem we want to solve, we could introduce a helper function which tries to locate the phy using a 'phy-handle' property

I don't follow you. Where do you get a phandle from to use with phy-handle?

Andrew

#8 lore raw
Quoted 11 lines · Andrew Lunn
Quoted 10 lines · Florian Fainelli
Quoted 8 lines · Andrew Lunn

Currently, supporting a fixed-phy in a MAC driver is a bit messy. It needs to be explicit supported, since a fixed phy is somewhat different from a normal phy.

These two patches solve this by making fixed-phys appear as normal PHYs within device tree, allowing them to be referenced by a phandle. Any MAC driver that supports phy-handle can then automatically support a fixed-phy without any code change.

Quoted 8 lines
Quoted 3 lines

Humm, if that's the problem we want to solve, we could introduce a helper function which tries to locate the phy using a 'phy-handle' property

I don't follow you. Where do you get a phandle from to use with phy-handle?

From the caller of the function: the consumer of that phy-handle and/or

fixed-link property which is either an Ethernet MAC driver or a DSA's switch port node.

Signature · 1 line
Florian
author maintainer
#4 lore raw

Re: [RFC PATCH net-next 2/2] phy: fixed-phy: Allow DT description of an MDIO bus and PHYs.

In reply to Florian Fainelli
Quoted 9 lines

Documentation/devicetree/bindings/net/fixed-link.txt ethernet@1 { · line 71

Quoted 5 lines
Documentation/devicetree/bindings/net/fixed-link.txt +5 -0
+Child nodes represent PHYs on this mdio bus. Standard properties for
+fixed links, 'speed', 'full-duplex', 'pause', 'asym-pause',
+'link-gpios', as defined above are used. Additionally a 'reg' property
+is required for the address of the PHY on the bus. This should be of
+value 0 to 31.

This is a virtual bus, the only limitation is because we re-use to the maximum permission extent the real MDIO bus code, and this is putting a SW constraint on something that does not have one here.

True. I was too lazy to audit the phy and mdio code to see if it actually allows phy_addr >= 32. And it is easy to instantiate another bus if you need it.

Quoted 19 lines
Quoted 19 lines
Documentation/devicetree/bindings/net/fixed-link.txt +19 -0
+
+Example
+-------
+
+mdio {
+	compatible = "linux,mdio-fixed-phy";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	phy0: phy@0 {
+		reg = <0>;
+		speed = <1000>;
+		pause;
+		link-gpios = <&gpio0 12 GPIO_ACTIVE_HIGH>;
+	};
+
+	phy1: phy@8 {
+		reg = <8>;
+		speed = <100>;
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		full-duplex;
+	};

The 'fixed-link' property, although it suffers from one basic issue which is that it is placed on the consumer of it (e.g: an Ethernet MAC Device Tree node) while being a two-headed snake (you actually describe a data-pipe with a fixed-link) is not quite perfect, but this does not seem to look any better.

It is actually just the same. Not worse, not better.

I see the real benefit in the drivers. They get fixed-phy for free. Cleanup on release just works, which most drivers don't even do now, so leak a fixed phy every time they are unloaded. DSA has some messy code, which has had a number of bugs, handling fixed phys. By adding this extra code, which i hope is reasonably simple to understand and review, we make the MAC drivers a lot simpler and less error prone.

Quoted 3 lines

One thing that Thomas solved nicely was avoid the need for allocating an address on the virtual MDIO bus, but this is coming back here, which does not seem needed except for correctness wrt. how real MDIO buses.

Actually Thomas's code is broken. Fixed phy addresses are allocated incrementally. When a fixed phy is released, its address is not re-used. So as soon as you have gone through 32 alloc/free cycles, all further allocs fail. This is however fixable.

Andrew

author maintainer
#9 lore raw
In reply to Florian Fainelli
Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

Humm, if that's the problem we want to solve, we could introduce a helper function which tries to locate the phy using a 'phy-handle' property

I don't follow you. Where do you get a phandle from to use with phy-handle?

From the caller of the function: the consumer of that phy-handle and/or

fixed-link property which is either an Ethernet MAC driver or a DSA's switch port node.

I still don't get it. Lets take a real example. I currently have this in one of my dts files:

&fec1 {
       phy-mode = "rmii";
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_fec1>;
       status = "okay";

       fixed-link {
                  speed = <100>;
                  full-duplex;
       };
};

and we now want to use a phy-handle:

&fec1 {
       phy-mode = "rmii";
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_fec1>;
       status = "okay";

       phy-handle = <XZY>;
};

What do i use as XZY, so that it somehow resolves to a fixed-phy?

Thanks
Andrew

author maintainer
#5 lore raw

Re: [RFC PATCH net-next 2/2] phy: fixed-phy: Allow DT description of an MDIO bus and PHYs.

In reply to Florian Fainelli
Quoted 14 lines

One too many " here.

Documentation/devicetree/bindings/net/fixed-link.txt ethernet@1 { · line 71

Quoted 8 lines
+- #address-cells = <1>;
+- #size-cells = <0>;
Documentation/devicetree/bindings/net/fixed-link.txt +6 -0
+
+Child nodes represent PHYs on this mdio bus. Standard properties for
+fixed links, 'speed', 'full-duplex', 'pause', 'asym-pause',
+'link-gpios', as defined above are used. Additionally a 'reg' property
+is required for the address of the PHY on the bus. This should be of
+value 0 to 31.

This is a virtual bus, the only limitation is because we re-use to the maximum permission extent the real MDIO bus code, and this is putting a SW constraint on something that does not have one here.

Hi Florian

I now took a look at the core code. PHY_MAX_ADDR is used in quite a few places, e.g. phy_find_first(), of_mdiobus_link_phydev(), __mdiobus_register(). In order to be able to use this virtual MDIO bus just like any other MDIO bus, we need to enforce PHY_MAX_ADDR. Otherwise it is not going to work.

Andrew

#10 lore raw
In reply to Andrew Lunn
Quoted 14 lines · Andrew Lunn
Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

Humm, if that's the problem we want to solve, we could introduce a helper function which tries to locate the phy using a 'phy-handle' property

I don't follow you. Where do you get a phandle from to use with phy-handle?

From the caller of the function: the consumer of that phy-handle and/or

fixed-link property which is either an Ethernet MAC driver or a DSA's switch port node.

I still don't get it. Lets take a real example. I currently have this in one of my dts files:

Quoted 11 lines
&fec1 {
       phy-mode = "rmii";
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_fec1>;
       status = "okay";

       fixed-link {
                  speed = <100>;
                  full-duplex;
       };
};

All drivers have this exact same structure:

&fec1 {
	phy-handle = <XYZ>;
	or
	fixed-link {
		speed = <100>;
		full-duplex;
	};
};

In both cases, the argument that this proposed helper function would take is a struct device_node pointing to &fec1 here. You could therefore imagine having something along these lines:

struct device_node *of_get_phy_by_phandle(struct device_node *dn, bool try_fixed_link)

{
	struct device_node *phy_dn;
	int ret;

	phy_dn = of_parse_phandle(dn, "phy-handle", 0);
	if (!phy_dn && !try_fixed_link)
		return -ENODEV;

	if (of_phy_is_fixed_link(dn)) {
		ret  = of_phy_register_fixed_link(dn);
		if (ret)
			return PTR_ERR(-ret);

		phy_dn = of_node_get(dn);
	}

	return phy_dn;		
}

In fact, we could even remove the "try_fixed_link" argument and just see if of_phy_is_fixed_link() returns true. Yes, this is not a proper device_node pointing to the emulated PHY, but without introducing binding changes, that is probably the best we can do.

I mistakenly used the term 'phandle' when I actually meant 'struct device_node' reference.

Signature · 1 line
Florian
author maintainer
#11 lore raw
Quoted 38 lines · Florian Fainelli
Quoted 14 lines · Andrew Lunn
Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

Humm, if that's the problem we want to solve, we could introduce a helper function which tries to locate the phy using a 'phy-handle' property

I don't follow you. Where do you get a phandle from to use with phy-handle?

From the caller of the function: the consumer of that phy-handle and/or

fixed-link property which is either an Ethernet MAC driver or a DSA's switch port node.

I still don't get it. Lets take a real example. I currently have this in one of my dts files:

Quoted 11 lines
&fec1 {
       phy-mode = "rmii";
       pinctrl-names = "default";
       pinctrl-0 = <&pinctrl_fec1>;
       status = "okay";

       fixed-link {
                  speed = <100>;
                  full-duplex;
       };
};

All drivers have this exact same structure:

&fec1 {
	phy-handle = <XYZ>;
	or
	fixed-link {
		speed = <100>;
		full-duplex;
	};
};
Quoted 6 lines

In both cases, the argument that this proposed helper function would take is a struct device_node pointing to &fec1 here. You could therefore imagine having something along these lines:

struct device_node *of_get_phy_by_phandle(struct device_node *dn, bool try_fixed_link)

I don't particularly like this name. It suggests it is using the phandle, when it might not.

Quoted 10 lines
{
	struct device_node *phy_dn;
	int ret;

	phy_dn = of_parse_phandle(dn, "phy-handle", 0);
	if (!phy_dn && !try_fixed_link)
		return -ENODEV;

	if (of_phy_is_fixed_link(dn)) {
		ret  = of_phy_register_fixed_link(dn);
Quoted 8 lines
		if (ret)
			return PTR_ERR(-ret);

		phy_dn = of_node_get(dn);
	}

	return phy_dn;		
}

To make release work, i think you need to hack something into phy_disconnect() or phy_detach() so that the fixed_phy registered above gets freed.

I don't particularly like these special cases. What i suggested does not require any special cases, because they act just like phys on an mdio bus.

Andrew