2 * OF helpers for the MDIO (Ethernet PHY) API
4 * Copyright (c) 2009 Secret Lab Technologies, Ltd.
6 * This file is released under the GPLv2
8 * This file provides helper functions for extracting PHY device information
9 * out of the OpenFirmware device tree and using it to populate an mii_bus.
12 #include <linux/phy.h>
14 #include <linux/of_mdio.h>
15 #include <linux/module.h>
17 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
18 MODULE_LICENSE("GPL");
21 * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
22 * @mdio: pointer to mii_bus structure
23 * @np: pointer to device_node of MDIO bus.
25 * This function registers the mii_bus structure and registers a phy_device
26 * for each child node of @np.
28 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
30 struct phy_device *phy;
31 struct device_node *child;
34 /* Mask out all PHYs from auto probing. Instead the PHYs listed in
35 * the device tree are populated after the bus has been registered */
38 /* Clear all the IRQ properties */
40 for (i=0; i<PHY_MAX_ADDR; i++)
41 mdio->irq[i] = PHY_POLL;
43 /* Register the MDIO bus */
44 rc = mdiobus_register(mdio);
48 /* Loop over the child nodes and register a phy_device for each one */
49 for_each_child_of_node(np, child) {
53 /* A PHY must have a reg property in the range [0-31] */
54 addr = of_get_property(child, "reg", &len);
55 if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) {
56 dev_err(&mdio->dev, "%s has invalid PHY address\n",
62 mdio->irq[*addr] = irq_of_parse_and_map(child, 0);
63 if (!mdio->irq[*addr])
64 mdio->irq[*addr] = PHY_POLL;
67 phy = get_phy_device(mdio, *addr);
69 dev_err(&mdio->dev, "error probing PHY at address %i\n",
75 /* Associate the OF node with the device structure so it
76 * can be looked up later */
78 dev_archdata_set_node(&phy->dev.archdata, child);
80 /* All data is now stored in the phy struct; register it */
81 rc = phy_device_register(phy);
88 dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
94 EXPORT_SYMBOL(of_mdiobus_register);
97 * of_phy_find_device - Give a PHY node, find the phy_device
98 * @phy_np: Pointer to the phy's device tree node
100 * Returns a pointer to the phy_device.
102 struct phy_device *of_phy_find_device(struct device_node *phy_np)
105 int match(struct device *dev, void *phy_np)
107 return dev_archdata_get_node(&dev->archdata) == phy_np;
113 d = bus_find_device(&mdio_bus_type, NULL, phy_np, match);
114 return d ? to_phy_device(d) : NULL;
116 EXPORT_SYMBOL(of_phy_find_device);
119 * of_phy_connect - Connect to the phy described in the device tree
120 * @dev: pointer to net_device claiming the phy
121 * @phy_np: Pointer to device tree node for the PHY
122 * @hndlr: Link state callback for the network device
123 * @iface: PHY data interface type
125 * Returns a pointer to the phy_device if successfull. NULL otherwise
127 struct phy_device *of_phy_connect(struct net_device *dev,
128 struct device_node *phy_np,
129 void (*hndlr)(struct net_device *), u32 flags,
130 phy_interface_t iface)
132 struct phy_device *phy = of_phy_find_device(phy_np);
137 return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy;
139 EXPORT_SYMBOL(of_phy_connect);