git: 82abe70fe98a - main - dpaa: Don't assume the MDIO is on the same fman as the MAC

From: Justin Hibbits <jhibbits_at_FreeBSD.org>
Date: Mon, 28 Nov 2022 22:08:05 UTC
The branch main has been updated by jhibbits:

URL: https://cgit.FreeBSD.org/src/commit/?id=82abe70fe98aaece817e35ba99fcc4aea4181067

commit 82abe70fe98aaece817e35ba99fcc4aea4181067
Author:     Justin Hibbits <jhibbits@FreeBSD.org>
AuthorDate: 2022-11-28 22:03:15 +0000
Commit:     Justin Hibbits <jhibbits@FreeBSD.org>
CommitDate: 2022-11-28 22:03:15 +0000

    dpaa: Don't assume the MDIO is on the same fman as the MAC
    
    The P5040 has the MDIO for FMAN2 on FMAN1 for some reason.  Instead of
    trying to manually find the MDIO, use a real xref.
---
 sys/dev/dpaa/fman_mdio.c    |  2 ++
 sys/dev/dpaa/if_dtsec_fdt.c | 46 +++++++++++++++------------------------------
 2 files changed, 17 insertions(+), 31 deletions(-)

diff --git a/sys/dev/dpaa/fman_mdio.c b/sys/dev/dpaa/fman_mdio.c
index 28ade4e3b315..ee90ba0b9cf9 100644
--- a/sys/dev/dpaa/fman_mdio.c
+++ b/sys/dev/dpaa/fman_mdio.c
@@ -134,6 +134,8 @@ pqmdio_fdt_attach(device_t dev)
 	bus_get_resource(dev, SYS_RES_MEMORY, 0, &start, &count);
 	sc->sc_offset = start;
 
+	OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
+
 	mtx_init(&sc->sc_lock, device_get_nameunit(dev), "QorIQ MDIO lock",
 	    MTX_DEF);
 
diff --git a/sys/dev/dpaa/if_dtsec_fdt.c b/sys/dev/dpaa/if_dtsec_fdt.c
index 179d620fcc3b..4bb96fa74f09 100644
--- a/sys/dev/dpaa/if_dtsec_fdt.c
+++ b/sys/dev/dpaa/if_dtsec_fdt.c
@@ -111,33 +111,11 @@ dtsec_fdt_probe(device_t dev)
 	return (BUS_PROBE_DEFAULT);
 }
 
-static int
-find_mdio(phandle_t phy_node, device_t mac, device_t *mdio_dev)
-{
-	device_t bus;
-
-	while (phy_node > 0) {
-		if (ofw_bus_node_is_compatible(phy_node, "fsl,fman-mdio"))
-			break;
-		phy_node = OF_parent(phy_node);
-	}
-
-	if (phy_node <= 0)
-		return (ENOENT);
-
-	bus = device_get_parent(mac);
-	*mdio_dev = ofw_bus_find_child_device_by_phandle(bus, phy_node);
-
-	if (*mdio_dev == NULL)
-		return (ENOENT);
-
-	return (0);
-}
-
 static int
 dtsec_fdt_attach(device_t dev)
 {
 	struct dtsec_softc *sc;
+	device_t phy_dev;
 	phandle_t enet_node, phy_node;
 	phandle_t fman_rxtx_node[2];
 	char phy_type[6];
@@ -162,24 +140,30 @@ dtsec_fdt_attach(device_t dev)
 	else
 		return(ENXIO);
 
-	/* Get MAC memory offset in SoC */
-	rid = 0;
-	sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
-	if (sc->sc_mem == NULL)
-		return (ENXIO);
-
 	/* Get PHY address */
 	if (OF_getprop(enet_node, "phy-handle", (void *)&phy_node,
 	    sizeof(phy_node)) <= 0)
 		return (ENXIO);
 
-	phy_node = OF_instance_to_package(phy_node);
+	phy_node = OF_node_from_xref(phy_node);
 
 	if (OF_getprop(phy_node, "reg", (void *)&sc->sc_phy_addr,
 	    sizeof(sc->sc_phy_addr)) <= 0)
 		return (ENXIO);
 
-	if (find_mdio(phy_node, dev, &sc->sc_mdio) != 0)
+	phy_dev = OF_device_from_xref(OF_parent(phy_node));
+
+	if (phy_dev == NULL) {
+		device_printf(dev, "No PHY found.\n");
+		return (ENXIO);
+	}
+
+	sc->sc_mdio = phy_dev;
+
+	/* Get MAC memory offset in SoC */
+	rid = 0;
+	sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
+	if (sc->sc_mem == NULL)
 		return (ENXIO);
 
 	/* Get PHY connection type */