[U-Boot] [PATCH v5 0/3] Add helper function for linking a DM Eth device to a PHY

The patch set introduces dm_eth_phy_connect which takes in an ethernet device and uses DT information to find the associated PHY and connect the Ethernet interface to it. This should simplify similar code in ethernet drivers.
I've dropped the new binding for scanning an MDIO bus, I'll look into reworking that to match the behavior in Linux. The changes are more extensive though and I plan to do that in a separate submission.
Finally the patch set updates fsl_enetc driver to use the new helper function reducing the code by about 30 lines.
This patch set supersedes v3 series: https://patchwork.ozlabs.org/project/uboot/list/?series=140114 and v4 series: https://patchwork.ozlabs.org/project/uboot/list/?series=142858
Changes in v5: - drop mdio-handle binding - support alternative/obsolete bindings (like 'phy', 'phy-device')
Changes in v4: - rebased on current head
Changes in v3: - added cover letter - check for null PHY pointer before using it in dm_eth_connect_phy_handle - moved the code dealing with MDIO scanning into a separate patch - renames several arguments and variables for a bit more clarity and consistency
Changes in v2: - Moved MDIO scan code into dm_mdio_phy_scan which is also exported - Use interface instead of if_type for consistency - don't use phy pointer if NULL in fsl_enetc code
Alex Marginean (3): net: mdio-uclass: rename arguments of dm_mdio_phy_connect for clarity net: mdio-uclass: add dm_eth_phy_connect helper function drivers: net: fsl_enetc: use the new MDIO DM helper functions
drivers/net/fsl_enetc.c | 53 ++++----------------- drivers/net/fsl_enetc.h | 1 + include/miiphy.h | 18 +++++-- net/mdio-uclass.c | 102 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 122 insertions(+), 52 deletions(-)

Renamed dm_mdio_phy_connect arguments dev to mdiodev and addr to phyaddr for a bit more clarity and consistency with the following patches. Also use NULL instead of 0 on error return path.
Signed-off-by: Alex Marginean alexandru.marginean@nxp.com --- include/miiphy.h | 6 +++--- net/mdio-uclass.c | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/include/miiphy.h b/include/miiphy.h index 9b97d09f18..94bf0da24a 100644 --- a/include/miiphy.h +++ b/include/miiphy.h @@ -154,14 +154,14 @@ void dm_mdio_probe_devices(void); /** * dm_mdio_phy_connect - Wrapper over phy_connect for DM MDIO * - * @dev: mdio dev - * @addr: PHY address on MDIO bus + * @mdiodev: mdio device the PHY is accesible on + * @phyaddr: PHY address on MDIO bus * @ethdev: ethernet device to connect to the PHY * @interface: MAC-PHY protocol * * @return pointer to phy_device, or 0 on error */ -struct phy_device *dm_mdio_phy_connect(struct udevice *dev, int addr, +struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, struct udevice *ethdev, phy_interface_t interface);
diff --git a/net/mdio-uclass.c b/net/mdio-uclass.c index 6f922e80b6..7a5f1d6dcc 100644 --- a/net/mdio-uclass.c +++ b/net/mdio-uclass.c @@ -104,16 +104,16 @@ static int dm_mdio_pre_remove(struct udevice *dev) return 0; }
-struct phy_device *dm_mdio_phy_connect(struct udevice *dev, int addr, +struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, struct udevice *ethdev, phy_interface_t interface) { - struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); + struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev);
- if (device_probe(dev)) - return 0; + if (device_probe(mdiodev)) + return NULL;
- return phy_connect(pdata->mii_bus, addr, ethdev, interface); + return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface); }
UCLASS_DRIVER(mdio) = {

On Mon, Nov 25, 2019 at 9:17 AM Alex Marginean alexandru.marginean@nxp.com wrote:
Renamed dm_mdio_phy_connect arguments dev to mdiodev and addr to phyaddr for a bit more clarity and consistency with the following patches. Also use NULL instead of 0 on error return path.
Signed-off-by: Alex Marginean alexandru.marginean@nxp.com
Acked-by: Joe Hershberger joe.hershberger@ni.com

The function connects an ethernet device to a PHY using DT information. This API is only available for eth devices with an associated device tree node.
Signed-off-by: Alex Marginean alexandru.marginean@nxp.com --- include/miiphy.h | 12 +++++++ net/mdio-uclass.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+)
diff --git a/include/miiphy.h b/include/miiphy.h index 94bf0da24a..61c136b114 100644 --- a/include/miiphy.h +++ b/include/miiphy.h @@ -165,6 +165,18 @@ struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, struct udevice *ethdev, phy_interface_t interface);
+/** + * dm_eth_phy_connect - Connect an Eth device to a PHY based on device tree + * + * Picks up the DT phy-handle and phy-mode from ethernet device node and + * connects the ethernet device to the linked PHY. + * + * @ethdev: ethernet device + * + * @return pointer to phy_device, or 0 on error + */ +struct phy_device *dm_eth_phy_connect(struct udevice *ethdev); + #endif
#ifdef CONFIG_DM_MDIO_MUX diff --git a/net/mdio-uclass.c b/net/mdio-uclass.c index 7a5f1d6dcc..b15d15470d 100644 --- a/net/mdio-uclass.c +++ b/net/mdio-uclass.c @@ -10,6 +10,17 @@ #include <dm/device-internal.h> #include <dm/uclass-internal.h>
+/* DT node properties for MAC-PHY interface */ +#define PHY_MODE_STR_CNT 2 +static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode", + "phy-connection-type" }; +/* DT node properties that reference a PHY node */ +#define PHY_HANDLE_STR_CNT 3 +const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle", + "phy", + "phy-device" }; + + void dm_mdio_probe_devices(void) { struct udevice *it; @@ -116,6 +127,86 @@ struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface); }
+static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev, + phy_interface_t interface) +{ + u32 phy_addr; + struct udevice *mdiodev; + struct phy_device *phy; + struct ofnode_phandle_args phandle = {.node = ofnode_null()}; + int i; + + for (i = 0; i < PHY_HANDLE_STR_CNT; i++) + if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL, + 0, 0, &phandle)) + break; + + if (!ofnode_valid(phandle.node)) { + dev_dbg(dev, "can't find PHY node\n"); + return NULL; + } + + /* + * reading 'reg' directly should be fine. This is a PHY node, the + * address is always size 1 and requires no translation + */ + if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) { + dev_dbg(ethdev, "missing reg property in phy node\n"); + return NULL; + } + + if (uclass_get_device_by_ofnode(UCLASS_MDIO, + ofnode_get_parent(phandle.node), + &mdiodev)) { + dev_dbg(dev, "can't find MDIO bus for node %s\n", + ofnode_get_name(ofnode_get_parent(phandle.node))); + return NULL; + } + + phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface); + + if (phy) + phy->node = phandle.node; + + return phy; +} + +/* Connect to a PHY linked in eth DT node */ +struct phy_device *dm_eth_phy_connect(struct udevice *ethdev) +{ + const char *if_str; + phy_interface_t interface; + struct phy_device *phy; + int i; + + if (!ofnode_valid(ethdev->node)) { + debug("%s: supplied eth dev has no DT node!\n", ethdev->name); + return NULL; + } + + interface = PHY_INTERFACE_MODE_NONE; + for (i = 0; i < PHY_MODE_STR_CNT; i++) { + if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]); + if (if_str) { + interface = phy_get_interface_by_name(if_str); + break; + } + } + if (interface < 0) + interface = PHY_INTERFACE_MODE_NONE; + if (interface == PHY_INTERFACE_MODE_NONE) + dev_dbg(ethdev, "can't find interface mode, default to NONE\n"); + + phy = dm_eth_connect_phy_handle(ethdev, interface); + + if (!phy) + return NULL; + + phy->interface = interface; + + return phy; +} + UCLASS_DRIVER(mdio) = { .id = UCLASS_MDIO, .name = "mdio",

On Mon, Nov 25, 2019 at 9:17 AM Alex Marginean alexandru.marginean@nxp.com wrote:
The function connects an ethernet device to a PHY using DT information. This API is only available for eth devices with an associated device tree node.
Signed-off-by: Alex Marginean alexandru.marginean@nxp.com
Acked-by: Joe Hershberger joe.hershberger@ni.com

Uses the new dm_eth_phy_connect helper to connect to the PHY to simplify the code.
Signed-off-by: Alex Marginean alexandru.marginean@nxp.com --- drivers/net/fsl_enetc.c | 53 +++++++---------------------------------- drivers/net/fsl_enetc.h | 1 + 2 files changed, 10 insertions(+), 44 deletions(-)
diff --git a/drivers/net/fsl_enetc.c b/drivers/net/fsl_enetc.c index 0ca7e838a8..e1713a3337 100644 --- a/drivers/net/fsl_enetc.c +++ b/drivers/net/fsl_enetc.c @@ -203,57 +203,20 @@ static void enetc_start_pcs(struct udevice *dev) }
/* Configure the actual/external ethernet PHY, if one is found */ -static void enetc_start_phy(struct udevice *dev) +static void enetc_config_phy(struct udevice *dev) { struct enetc_priv *priv = dev_get_priv(dev); - struct udevice *miidev; - struct phy_device *phy; - u32 phandle, phy_id; - ofnode phy_node; int supported;
- if (!ofnode_valid(dev->node)) { - enetc_dbg(dev, "no enetc ofnode found, skipping PHY set-up\n"); - return; - } - - if (ofnode_read_u32(dev->node, "phy-handle", &phandle)) { - enetc_dbg(dev, "phy-handle not found, skipping PHY set-up\n"); - return; - } - - phy_node = ofnode_get_by_phandle(phandle); - if (!ofnode_valid(phy_node)) { - enetc_dbg(dev, "invalid phy node, skipping PHY set-up\n"); - return; - } - enetc_dbg(dev, "phy node: %s\n", ofnode_get_name(phy_node)); + priv->phy = dm_eth_phy_connect(dev);
- if (ofnode_read_u32(phy_node, "reg", &phy_id)) { - enetc_dbg(dev, - "missing reg in PHY node, skipping PHY set-up\n"); + if (!priv->phy) return; - } - - if (uclass_get_device_by_ofnode(UCLASS_MDIO, - ofnode_get_parent(phy_node), - &miidev)) { - enetc_dbg(dev, "can't find MDIO bus for node %s\n", - ofnode_get_name(ofnode_get_parent(phy_node))); - return; - } - - phy = dm_mdio_phy_connect(miidev, phy_id, dev, priv->if_type); - if (!phy) { - enetc_dbg(dev, "dm_mdio_phy_connect returned null\n"); - return; - }
supported = GENMASK(6, 0); /* speeds up to 1G & AN */ - phy->advertising = phy->supported & supported; - phy->node = phy_node; - phy_config(phy); - phy_startup(phy); + priv->phy->advertising = priv->phy->supported & supported; + + phy_config(priv->phy); }
/* @@ -468,7 +431,9 @@ static int enetc_start(struct udevice *dev) enetc_setup_rx_bdr(dev);
enetc_start_pcs(dev); - enetc_start_phy(dev); + enetc_config_phy(dev); + if (priv->phy) + phy_startup(priv->phy);
return 0; } diff --git a/drivers/net/fsl_enetc.h b/drivers/net/fsl_enetc.h index 0bb4cdff47..9a36cdad80 100644 --- a/drivers/net/fsl_enetc.h +++ b/drivers/net/fsl_enetc.h @@ -154,6 +154,7 @@ struct enetc_priv {
int if_type; struct mii_dev imdio; + struct phy_device *phy; };
/* register accessors */

On Mon, Nov 25, 2019 at 9:17 AM Alex Marginean alexandru.marginean@nxp.com wrote:
Uses the new dm_eth_phy_connect helper to connect to the PHY to simplify the code.
Signed-off-by: Alex Marginean alexandru.marginean@nxp.com
Acked-by: Joe Hershberger joe.hershberger@ni.com
participants (2)
-
Alex Marginean
-
Joe Hershberger