Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 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 | |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/err.h> |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 16 | #include <linux/phy.h> |
| 17 | #include <linux/of.h> |
Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 18 | #include <linux/of_irq.h> |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 19 | #include <linux/of_mdio.h> |
| 20 | #include <linux/module.h> |
| 21 | |
| 22 | MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); |
| 23 | MODULE_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 | */ |
| 33 | int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np) |
| 34 | { |
| 35 | struct phy_device *phy; |
| 36 | struct device_node *child; |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 37 | const __be32 *paddr; |
| 38 | u32 addr; |
| 39 | bool is_c45, scanphys = false; |
| 40 | int rc, i, len; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 41 | |
| 42 | /* Mask out all PHYs from auto probing. Instead the PHYs listed in |
| 43 | * the device tree are populated after the bus has been registered */ |
| 44 | mdio->phy_mask = ~0; |
| 45 | |
| 46 | /* Clear all the IRQ properties */ |
| 47 | if (mdio->irq) |
| 48 | for (i=0; i<PHY_MAX_ADDR; i++) |
| 49 | mdio->irq[i] = PHY_POLL; |
| 50 | |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 51 | mdio->dev.of_node = np; |
| 52 | |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 53 | /* Register the MDIO bus */ |
| 54 | rc = mdiobus_register(mdio); |
| 55 | if (rc) |
| 56 | return rc; |
| 57 | |
| 58 | /* Loop over the child nodes and register a phy_device for each one */ |
Alexander Sverdlin | e207e76 | 2012-11-29 08:45:20 +0100 | [diff] [blame] | 59 | for_each_available_child_of_node(np, child) { |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 60 | /* A PHY must have a reg property in the range [0-31] */ |
David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 61 | paddr = of_get_property(child, "reg", &len); |
| 62 | if (!paddr || len < sizeof(*paddr)) { |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 63 | scanphys = true; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 64 | dev_err(&mdio->dev, "%s has invalid PHY address\n", |
| 65 | child->full_name); |
| 66 | continue; |
| 67 | } |
| 68 | |
David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 69 | addr = be32_to_cpup(paddr); |
| 70 | if (addr >= 32) { |
| 71 | dev_err(&mdio->dev, "%s PHY address %i is too large\n", |
| 72 | child->full_name, addr); |
| 73 | continue; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 74 | } |
| 75 | |
David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 76 | if (mdio->irq) { |
| 77 | mdio->irq[addr] = irq_of_parse_and_map(child, 0); |
| 78 | if (!mdio->irq[addr]) |
| 79 | mdio->irq[addr] = PHY_POLL; |
| 80 | } |
| 81 | |
David Daney | 6bd47ac | 2012-06-27 07:33:36 +0000 | [diff] [blame] | 82 | is_c45 = of_device_is_compatible(child, |
| 83 | "ethernet-phy-ieee802.3-c45"); |
| 84 | phy = get_phy_device(mdio, addr, is_c45); |
| 85 | |
Dan Carpenter | 9bd7371 | 2010-04-28 01:07:29 -0600 | [diff] [blame] | 86 | if (!phy || IS_ERR(phy)) { |
David Daney | 6bd47ac | 2012-06-27 07:33:36 +0000 | [diff] [blame] | 87 | phy = phy_device_create(mdio, addr, 0, false, NULL); |
| 88 | if (!phy || IS_ERR(phy)) { |
| 89 | dev_err(&mdio->dev, |
| 90 | "error creating PHY at address %i\n", |
| 91 | addr); |
| 92 | continue; |
| 93 | } |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 94 | } |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 95 | |
| 96 | /* Associate the OF node with the device structure so it |
| 97 | * can be looked up later */ |
| 98 | of_node_get(child); |
Grant Likely | d706c1b | 2010-04-13 16:12:28 -0700 | [diff] [blame] | 99 | phy->dev.of_node = child; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 100 | |
| 101 | /* All data is now stored in the phy struct; register it */ |
| 102 | rc = phy_device_register(phy); |
| 103 | if (rc) { |
| 104 | phy_device_free(phy); |
| 105 | of_node_put(child); |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | dev_dbg(&mdio->dev, "registered phy %s at address %i\n", |
David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 110 | child->name, addr); |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 113 | if (!scanphys) |
| 114 | return 0; |
| 115 | |
| 116 | /* auto scan for PHYs with empty reg property */ |
| 117 | for_each_available_child_of_node(np, child) { |
| 118 | /* Skip PHYs with reg property set */ |
| 119 | paddr = of_get_property(child, "reg", &len); |
| 120 | if (paddr) |
| 121 | continue; |
| 122 | |
| 123 | is_c45 = of_device_is_compatible(child, |
| 124 | "ethernet-phy-ieee802.3-c45"); |
| 125 | |
| 126 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { |
| 127 | /* skip already registered PHYs */ |
| 128 | if (mdio->phy_map[addr]) |
| 129 | continue; |
| 130 | |
| 131 | /* be noisy to encourage people to set reg property */ |
| 132 | dev_info(&mdio->dev, "scan phy %s at address %i\n", |
| 133 | child->name, addr); |
| 134 | |
| 135 | phy = get_phy_device(mdio, addr, is_c45); |
| 136 | if (!phy || IS_ERR(phy)) |
| 137 | continue; |
| 138 | |
| 139 | if (mdio->irq) { |
| 140 | mdio->irq[addr] = |
| 141 | irq_of_parse_and_map(child, 0); |
| 142 | if (!mdio->irq[addr]) |
| 143 | mdio->irq[addr] = PHY_POLL; |
| 144 | } |
| 145 | |
| 146 | /* Associate the OF node with the device structure so it |
| 147 | * can be looked up later */ |
| 148 | of_node_get(child); |
| 149 | phy->dev.of_node = child; |
| 150 | |
| 151 | /* All data is now stored in the phy struct; |
| 152 | * register it */ |
| 153 | rc = phy_device_register(phy); |
| 154 | if (rc) { |
| 155 | phy_device_free(phy); |
| 156 | of_node_put(child); |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | dev_info(&mdio->dev, "registered phy %s at address %i\n", |
| 161 | child->name, addr); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | EXPORT_SYMBOL(of_mdiobus_register); |
| 169 | |
Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 170 | /* Helper function for of_phy_find_device */ |
| 171 | static int of_phy_match(struct device *dev, void *phy_np) |
| 172 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 173 | return dev->of_node == phy_np; |
Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 174 | } |
| 175 | |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 176 | /** |
| 177 | * of_phy_find_device - Give a PHY node, find the phy_device |
| 178 | * @phy_np: Pointer to the phy's device tree node |
| 179 | * |
| 180 | * Returns a pointer to the phy_device. |
| 181 | */ |
| 182 | struct phy_device *of_phy_find_device(struct device_node *phy_np) |
| 183 | { |
| 184 | struct device *d; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 185 | if (!phy_np) |
| 186 | return NULL; |
| 187 | |
Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 188 | d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match); |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 189 | return d ? to_phy_device(d) : NULL; |
| 190 | } |
| 191 | EXPORT_SYMBOL(of_phy_find_device); |
| 192 | |
| 193 | /** |
| 194 | * of_phy_connect - Connect to the phy described in the device tree |
| 195 | * @dev: pointer to net_device claiming the phy |
| 196 | * @phy_np: Pointer to device tree node for the PHY |
| 197 | * @hndlr: Link state callback for the network device |
| 198 | * @iface: PHY data interface type |
| 199 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 200 | * Returns a pointer to the phy_device if successful. NULL otherwise |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 201 | */ |
| 202 | struct phy_device *of_phy_connect(struct net_device *dev, |
| 203 | struct device_node *phy_np, |
| 204 | void (*hndlr)(struct net_device *), u32 flags, |
| 205 | phy_interface_t iface) |
| 206 | { |
| 207 | struct phy_device *phy = of_phy_find_device(phy_np); |
| 208 | |
| 209 | if (!phy) |
| 210 | return NULL; |
| 211 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 212 | return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 213 | } |
| 214 | EXPORT_SYMBOL(of_phy_connect); |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy |
| 218 | * @dev: pointer to net_device claiming the phy |
| 219 | * @hndlr: Link state callback for the network device |
| 220 | * @iface: PHY data interface type |
| 221 | * |
| 222 | * This function is a temporary stop-gap and will be removed soon. It is |
| 223 | * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do |
| 224 | * not call this function from new drivers. |
| 225 | */ |
| 226 | struct phy_device *of_phy_connect_fixed_link(struct net_device *dev, |
| 227 | void (*hndlr)(struct net_device *), |
| 228 | phy_interface_t iface) |
| 229 | { |
| 230 | struct device_node *net_np; |
| 231 | char bus_id[MII_BUS_ID_SIZE + 3]; |
| 232 | struct phy_device *phy; |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 233 | const __be32 *phy_id; |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 234 | int sz; |
| 235 | |
| 236 | if (!dev->dev.parent) |
| 237 | return NULL; |
| 238 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 239 | net_np = dev->dev.parent->of_node; |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 240 | if (!net_np) |
| 241 | return NULL; |
| 242 | |
| 243 | phy_id = of_get_property(net_np, "fixed-link", &sz); |
| 244 | if (!phy_id || sz < sizeof(*phy_id)) |
| 245 | return NULL; |
| 246 | |
Baruch Siach | e5c7d1f | 2012-02-27 14:48:46 +0200 | [diff] [blame] | 247 | sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0])); |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 248 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 249 | phy = phy_connect(dev, bus_id, hndlr, iface); |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 250 | return IS_ERR(phy) ? NULL : phy; |
| 251 | } |
| 252 | EXPORT_SYMBOL(of_phy_connect_fixed_link); |