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 | |
Florian Fainelli | 8fdade4 | 2013-12-05 14:52:14 -0800 | [diff] [blame] | 25 | static void of_set_phy_supported(struct phy_device *phydev, u32 max_speed) |
| 26 | { |
Florian Fainelli | ce11c43 | 2014-02-13 13:14:48 -0800 | [diff] [blame] | 27 | /* The default values for phydev->supported are provided by the PHY |
| 28 | * driver "features" member, we want to reset to sane defaults fist |
| 29 | * before supporting higher speeds. |
| 30 | */ |
| 31 | phydev->supported &= PHY_DEFAULT_FEATURES; |
Florian Fainelli | 8fdade4 | 2013-12-05 14:52:14 -0800 | [diff] [blame] | 32 | |
| 33 | switch (max_speed) { |
| 34 | default: |
| 35 | return; |
| 36 | |
| 37 | case SPEED_1000: |
| 38 | phydev->supported |= PHY_1000BT_FEATURES; |
| 39 | case SPEED_100: |
| 40 | phydev->supported |= PHY_100BT_FEATURES; |
| 41 | case SPEED_10: |
| 42 | phydev->supported |= PHY_10BT_FEATURES; |
| 43 | } |
| 44 | } |
| 45 | |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 46 | static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child, |
| 47 | u32 addr) |
| 48 | { |
| 49 | struct phy_device *phy; |
| 50 | bool is_c45; |
Florian Fainelli | 7d97637 | 2013-12-05 14:52:12 -0800 | [diff] [blame] | 51 | int rc, prev_irq; |
Florian Fainelli | 8fdade4 | 2013-12-05 14:52:14 -0800 | [diff] [blame] | 52 | u32 max_speed = 0; |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 53 | |
| 54 | is_c45 = of_device_is_compatible(child, |
| 55 | "ethernet-phy-ieee802.3-c45"); |
| 56 | |
| 57 | phy = get_phy_device(mdio, addr, is_c45); |
| 58 | if (!phy || IS_ERR(phy)) |
| 59 | return 1; |
| 60 | |
| 61 | if (mdio->irq) { |
Florian Fainelli | 7d97637 | 2013-12-05 14:52:12 -0800 | [diff] [blame] | 62 | prev_irq = mdio->irq[addr]; |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 63 | mdio->irq[addr] = |
| 64 | irq_of_parse_and_map(child, 0); |
| 65 | if (!mdio->irq[addr]) |
Florian Fainelli | 7d97637 | 2013-12-05 14:52:12 -0800 | [diff] [blame] | 66 | mdio->irq[addr] = prev_irq; |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* Associate the OF node with the device structure so it |
| 70 | * can be looked up later */ |
| 71 | of_node_get(child); |
| 72 | phy->dev.of_node = child; |
| 73 | |
| 74 | /* All data is now stored in the phy struct; |
| 75 | * register it */ |
| 76 | rc = phy_device_register(phy); |
| 77 | if (rc) { |
| 78 | phy_device_free(phy); |
| 79 | of_node_put(child); |
| 80 | return 1; |
| 81 | } |
| 82 | |
Florian Fainelli | 8fdade4 | 2013-12-05 14:52:14 -0800 | [diff] [blame] | 83 | /* Set phydev->supported based on the "max-speed" property |
| 84 | * if present */ |
| 85 | if (!of_property_read_u32(child, "max-speed", &max_speed)) |
| 86 | of_set_phy_supported(phy, max_speed); |
| 87 | |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 88 | dev_dbg(&mdio->dev, "registered phy %s at address %i\n", |
Florian Fainelli | 8fdade4 | 2013-12-05 14:52:14 -0800 | [diff] [blame] | 89 | child->name, addr); |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 94 | /** |
| 95 | * of_mdiobus_register - Register mii_bus and create PHYs from the device tree |
| 96 | * @mdio: pointer to mii_bus structure |
| 97 | * @np: pointer to device_node of MDIO bus. |
| 98 | * |
| 99 | * This function registers the mii_bus structure and registers a phy_device |
| 100 | * for each child node of @np. |
| 101 | */ |
| 102 | int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np) |
| 103 | { |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 104 | struct device_node *child; |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 105 | const __be32 *paddr; |
| 106 | u32 addr; |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 107 | bool scanphys = false; |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 108 | int rc, i, len; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 109 | |
| 110 | /* Mask out all PHYs from auto probing. Instead the PHYs listed in |
| 111 | * the device tree are populated after the bus has been registered */ |
| 112 | mdio->phy_mask = ~0; |
| 113 | |
| 114 | /* Clear all the IRQ properties */ |
| 115 | if (mdio->irq) |
| 116 | for (i=0; i<PHY_MAX_ADDR; i++) |
| 117 | mdio->irq[i] = PHY_POLL; |
| 118 | |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 119 | mdio->dev.of_node = np; |
| 120 | |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 121 | /* Register the MDIO bus */ |
| 122 | rc = mdiobus_register(mdio); |
| 123 | if (rc) |
| 124 | return rc; |
| 125 | |
| 126 | /* 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] | 127 | for_each_available_child_of_node(np, child) { |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 128 | /* A PHY must have a reg property in the range [0-31] */ |
David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 129 | paddr = of_get_property(child, "reg", &len); |
| 130 | if (!paddr || len < sizeof(*paddr)) { |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 131 | scanphys = true; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 132 | dev_err(&mdio->dev, "%s has invalid PHY address\n", |
| 133 | child->full_name); |
| 134 | continue; |
| 135 | } |
| 136 | |
David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 137 | addr = be32_to_cpup(paddr); |
Florian Fainelli | bed2f9e | 2013-12-05 14:52:11 -0800 | [diff] [blame] | 138 | if (addr >= PHY_MAX_ADDR) { |
David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 139 | dev_err(&mdio->dev, "%s PHY address %i is too large\n", |
| 140 | child->full_name, addr); |
| 141 | continue; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 144 | rc = of_mdiobus_register_phy(mdio, child, addr); |
| 145 | if (rc) |
Sebastian Hesselbarth | 058112c | 2013-05-06 23:49:31 +0000 | [diff] [blame] | 146 | continue; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 149 | if (!scanphys) |
| 150 | return 0; |
| 151 | |
| 152 | /* auto scan for PHYs with empty reg property */ |
| 153 | for_each_available_child_of_node(np, child) { |
| 154 | /* Skip PHYs with reg property set */ |
| 155 | paddr = of_get_property(child, "reg", &len); |
| 156 | if (paddr) |
| 157 | continue; |
| 158 | |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 159 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { |
| 160 | /* skip already registered PHYs */ |
| 161 | if (mdio->phy_map[addr]) |
| 162 | continue; |
| 163 | |
| 164 | /* be noisy to encourage people to set reg property */ |
| 165 | dev_info(&mdio->dev, "scan phy %s at address %i\n", |
| 166 | child->name, addr); |
| 167 | |
Florian Fainelli | 2e79cb3 | 2013-12-05 14:52:10 -0800 | [diff] [blame] | 168 | rc = of_mdiobus_register_phy(mdio, child, addr); |
| 169 | if (rc) |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 170 | continue; |
Sebastian Hesselbarth | 779d835 | 2013-04-07 01:09:48 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | EXPORT_SYMBOL(of_mdiobus_register); |
| 177 | |
Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 178 | /* Helper function for of_phy_find_device */ |
| 179 | static int of_phy_match(struct device *dev, void *phy_np) |
| 180 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 181 | return dev->of_node == phy_np; |
Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 182 | } |
| 183 | |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 184 | /** |
| 185 | * of_phy_find_device - Give a PHY node, find the phy_device |
| 186 | * @phy_np: Pointer to the phy's device tree node |
| 187 | * |
| 188 | * Returns a pointer to the phy_device. |
| 189 | */ |
| 190 | struct phy_device *of_phy_find_device(struct device_node *phy_np) |
| 191 | { |
| 192 | struct device *d; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 193 | if (!phy_np) |
| 194 | return NULL; |
| 195 | |
Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 196 | 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] | 197 | return d ? to_phy_device(d) : NULL; |
| 198 | } |
| 199 | EXPORT_SYMBOL(of_phy_find_device); |
| 200 | |
| 201 | /** |
| 202 | * of_phy_connect - Connect to the phy described in the device tree |
| 203 | * @dev: pointer to net_device claiming the phy |
| 204 | * @phy_np: Pointer to device tree node for the PHY |
| 205 | * @hndlr: Link state callback for the network device |
| 206 | * @iface: PHY data interface type |
| 207 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 208 | * Returns a pointer to the phy_device if successful. NULL otherwise |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 209 | */ |
| 210 | struct phy_device *of_phy_connect(struct net_device *dev, |
| 211 | struct device_node *phy_np, |
| 212 | void (*hndlr)(struct net_device *), u32 flags, |
| 213 | phy_interface_t iface) |
| 214 | { |
| 215 | struct phy_device *phy = of_phy_find_device(phy_np); |
| 216 | |
| 217 | if (!phy) |
| 218 | return NULL; |
| 219 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 220 | return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy; |
Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 221 | } |
| 222 | EXPORT_SYMBOL(of_phy_connect); |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy |
| 226 | * @dev: pointer to net_device claiming the phy |
| 227 | * @hndlr: Link state callback for the network device |
| 228 | * @iface: PHY data interface type |
| 229 | * |
| 230 | * This function is a temporary stop-gap and will be removed soon. It is |
| 231 | * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do |
| 232 | * not call this function from new drivers. |
| 233 | */ |
| 234 | struct phy_device *of_phy_connect_fixed_link(struct net_device *dev, |
| 235 | void (*hndlr)(struct net_device *), |
| 236 | phy_interface_t iface) |
| 237 | { |
| 238 | struct device_node *net_np; |
| 239 | char bus_id[MII_BUS_ID_SIZE + 3]; |
| 240 | struct phy_device *phy; |
Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 241 | const __be32 *phy_id; |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 242 | int sz; |
| 243 | |
| 244 | if (!dev->dev.parent) |
| 245 | return NULL; |
| 246 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 247 | net_np = dev->dev.parent->of_node; |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 248 | if (!net_np) |
| 249 | return NULL; |
| 250 | |
| 251 | phy_id = of_get_property(net_np, "fixed-link", &sz); |
| 252 | if (!phy_id || sz < sizeof(*phy_id)) |
| 253 | return NULL; |
| 254 | |
Baruch Siach | e5c7d1f | 2012-02-27 14:48:46 +0200 | [diff] [blame] | 255 | 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] | 256 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 257 | phy = phy_connect(dev, bus_id, hndlr, iface); |
Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 258 | return IS_ERR(phy) ? NULL : phy; |
| 259 | } |
| 260 | EXPORT_SYMBOL(of_phy_connect_fixed_link); |
Andy Fleming | 7614aba | 2014-01-10 14:28:11 +0800 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * of_phy_attach - Attach to a PHY without starting the state machine |
| 264 | * @dev: pointer to net_device claiming the phy |
| 265 | * @phy_np: Node pointer for the PHY |
| 266 | * @flags: flags to pass to the PHY |
| 267 | * @iface: PHY data interface type |
| 268 | */ |
| 269 | struct phy_device *of_phy_attach(struct net_device *dev, |
| 270 | struct device_node *phy_np, u32 flags, |
| 271 | phy_interface_t iface) |
| 272 | { |
| 273 | struct phy_device *phy = of_phy_find_device(phy_np); |
| 274 | |
| 275 | if (!phy) |
| 276 | return NULL; |
| 277 | |
| 278 | return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy; |
| 279 | } |
| 280 | EXPORT_SYMBOL(of_phy_attach); |