1/* 2 * OF helpers for the MDIO (Ethernet PHY) API 3 * 4 * Copyright (c) 2009 Secret Lab Technologies, Ltd. 5 * 6 * This file is released under the GPLv2 7 * 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. 10 */ 11 12#include <linux/kernel.h> 13#include <linux/device.h> 14#include <linux/netdevice.h> 15#include <linux/err.h> 16#include <linux/phy.h> 17#include <linux/of.h> 18#include <linux/of_irq.h> 19#include <linux/of_mdio.h> 20#include <linux/module.h> 21 22MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); 23MODULE_LICENSE("GPL"); 24 25/** 26 * of_mdiobus_register - Register mii_bus and create PHYs from the device tree 27 * @mdio: pointer to mii_bus structure 28 * @np: pointer to device_node of MDIO bus. 29 * 30 * This function registers the mii_bus structure and registers a phy_device 31 * for each child node of @np. 32 */ 33int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np) 34{ 35 struct phy_device *phy; 36 struct device_node *child; 37 int rc, i; 38 39 /* Mask out all PHYs from auto probing. Instead the PHYs listed in 40 * the device tree are populated after the bus has been registered */ 41 mdio->phy_mask = ~0; 42 43 /* Clear all the IRQ properties */ 44 if (mdio->irq) 45 for (i=0; i<PHY_MAX_ADDR; i++) 46 mdio->irq[i] = PHY_POLL; 47 48 /* Register the MDIO bus */ 49 rc = mdiobus_register(mdio); 50 if (rc) 51 return rc; 52 53 /* Loop over the child nodes and register a phy_device for each one */ 54 for_each_child_of_node(np, child) { 55 const __be32 *addr; 56 int len; 57 58 /* A PHY must have a reg property in the range [0-31] */ 59 addr = of_get_property(child, "reg", &len); 60 if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) { 61 dev_err(&mdio->dev, "%s has invalid PHY address\n", 62 child->full_name); 63 continue; 64 } 65 66 if (mdio->irq) { 67 mdio->irq[*addr] = irq_of_parse_and_map(child, 0); 68 if (!mdio->irq[*addr]) 69 mdio->irq[*addr] = PHY_POLL; 70 } 71 72 phy = get_phy_device(mdio, be32_to_cpup(addr)); 73 if (!phy || IS_ERR(phy)) { 74 dev_err(&mdio->dev, "error probing PHY at address %i\n", 75 *addr); 76 continue; 77 } 78 phy_scan_fixups(phy); 79 80 /* Associate the OF node with the device structure so it 81 * can be looked up later */ 82 of_node_get(child); 83 phy->dev.of_node = child; 84 85 /* All data is now stored in the phy struct; register it */ 86 rc = phy_device_register(phy); 87 if (rc) { 88 phy_device_free(phy); 89 of_node_put(child); 90 continue; 91 } 92 93 dev_dbg(&mdio->dev, "registered phy %s at address %i\n", 94 child->name, *addr); 95 } 96 97 return 0; 98} 99EXPORT_SYMBOL(of_mdiobus_register); 100 101/* Helper function for of_phy_find_device */ 102static int of_phy_match(struct device *dev, void *phy_np) 103{ 104 return dev->of_node == phy_np; 105} 106 107/** 108 * of_phy_find_device - Give a PHY node, find the phy_device 109 * @phy_np: Pointer to the phy's device tree node 110 * 111 * Returns a pointer to the phy_device. 112 */ 113struct phy_device *of_phy_find_device(struct device_node *phy_np) 114{ 115 struct device *d; 116 if (!phy_np) 117 return NULL; 118 119 d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match); 120 return d ? to_phy_device(d) : NULL; 121} 122EXPORT_SYMBOL(of_phy_find_device); 123 124/** 125 * of_phy_connect - Connect to the phy described in the device tree 126 * @dev: pointer to net_device claiming the phy 127 * @phy_np: Pointer to device tree node for the PHY 128 * @hndlr: Link state callback for the network device 129 * @iface: PHY data interface type 130 * 131 * Returns a pointer to the phy_device if successfull. NULL otherwise 132 */ 133struct phy_device *of_phy_connect(struct net_device *dev, 134 struct device_node *phy_np, 135 void (*hndlr)(struct net_device *), u32 flags, 136 phy_interface_t iface) 137{ 138 struct phy_device *phy = of_phy_find_device(phy_np); 139 140 if (!phy) 141 return NULL; 142 143 return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy; 144} 145EXPORT_SYMBOL(of_phy_connect); 146 147/** 148 * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy 149 * @dev: pointer to net_device claiming the phy 150 * @hndlr: Link state callback for the network device 151 * @iface: PHY data interface type 152 * 153 * This function is a temporary stop-gap and will be removed soon. It is 154 * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do 155 * not call this function from new drivers. 156 */ 157struct phy_device *of_phy_connect_fixed_link(struct net_device *dev, 158 void (*hndlr)(struct net_device *), 159 phy_interface_t iface) 160{ 161 struct device_node *net_np; 162 char bus_id[MII_BUS_ID_SIZE + 3]; 163 struct phy_device *phy; 164 const __be32 *phy_id; 165 int sz; 166 167 if (!dev->dev.parent) 168 return NULL; 169 170 net_np = dev->dev.parent->of_node; 171 if (!net_np) 172 return NULL; 173 174 phy_id = of_get_property(net_np, "fixed-link", &sz); 175 if (!phy_id || sz < sizeof(*phy_id)) 176 return NULL; 177 178 sprintf(bus_id, PHY_ID_FMT, "0", be32_to_cpu(phy_id[0])); 179 180 phy = phy_connect(dev, bus_id, hndlr, 0, iface); 181 return IS_ERR(phy) ? NULL : phy; 182} 183EXPORT_SYMBOL(of_phy_connect_fixed_link); 184