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