Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1 | /* Framework for finding and configuring PHYs. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2 | * Also contains generic PHY driver |
| 3 | * |
| 4 | * Author: Andy Fleming |
| 5 | * |
| 6 | * Copyright (c) 2004 Freescale Semiconductor, Inc. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | */ |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 14 | |
| 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 18 | #include <linux/string.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/unistd.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/netdevice.h> |
| 26 | #include <linux/etherdevice.h> |
| 27 | #include <linux/skbuff.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 28 | #include <linux/mm.h> |
| 29 | #include <linux/module.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 30 | #include <linux/mii.h> |
| 31 | #include <linux/ethtool.h> |
| 32 | #include <linux/phy.h> |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 33 | #include <linux/mdio.h> |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 34 | #include <linux/io.h> |
| 35 | #include <linux/uaccess.h> |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 36 | #include <linux/of.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 37 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 38 | #include <asm/irq.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 39 | |
Olaf Hering | afcceaa | 2005-12-14 00:33:49 +0100 | [diff] [blame] | 40 | MODULE_DESCRIPTION("PHY library"); |
| 41 | MODULE_AUTHOR("Andy Fleming"); |
| 42 | MODULE_LICENSE("GPL"); |
| 43 | |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 44 | void phy_device_free(struct phy_device *phydev) |
| 45 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 46 | put_device(&phydev->mdio.dev); |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 47 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 48 | EXPORT_SYMBOL(phy_device_free); |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 49 | |
| 50 | static void phy_device_release(struct device *dev) |
| 51 | { |
Petr Malat | b2a4319 | 2013-02-28 01:01:52 +0000 | [diff] [blame] | 52 | kfree(to_phy_device(dev)); |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 53 | } |
| 54 | |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 55 | enum genphy_driver { |
| 56 | GENPHY_DRV_1G, |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 57 | GENPHY_DRV_10G, |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 58 | GENPHY_DRV_MAX |
| 59 | }; |
| 60 | |
| 61 | static struct phy_driver genphy_driver[GENPHY_DRV_MAX]; |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 62 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 63 | static LIST_HEAD(phy_fixup_list); |
| 64 | static DEFINE_MUTEX(phy_fixup_lock); |
| 65 | |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 66 | #ifdef CONFIG_PM |
| 67 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) |
| 68 | { |
| 69 | struct device_driver *drv = phydev->mdio.dev.driver; |
| 70 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 71 | struct net_device *netdev = phydev->attached_dev; |
| 72 | |
| 73 | if (!drv || !phydrv->suspend) |
| 74 | return false; |
| 75 | |
| 76 | /* PHY not attached? May suspend if the PHY has not already been |
| 77 | * suspended as part of a prior call to phy_disconnect() -> |
| 78 | * phy_detach() -> phy_suspend() because the parent netdev might be the |
| 79 | * MDIO bus driver and clock gated at this point. |
| 80 | */ |
| 81 | if (!netdev) |
| 82 | return !phydev->suspended; |
| 83 | |
| 84 | /* Don't suspend PHY if the attached netdev parent may wakeup. |
| 85 | * The parent may point to a PCI device, as in tg3 driver. |
| 86 | */ |
| 87 | if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) |
| 88 | return false; |
| 89 | |
| 90 | /* Also don't suspend PHY if the netdev itself may wakeup. This |
| 91 | * is the case for devices w/o underlaying pwr. mgmt. aware bus, |
| 92 | * e.g. SoC devices. |
| 93 | */ |
| 94 | if (device_may_wakeup(&netdev->dev)) |
| 95 | return false; |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | static int mdio_bus_phy_suspend(struct device *dev) |
| 101 | { |
| 102 | struct phy_device *phydev = to_phy_device(dev); |
| 103 | |
| 104 | /* We must stop the state machine manually, otherwise it stops out of |
| 105 | * control, possibly with the phydev->lock held. Upon resume, netdev |
| 106 | * may call phy routines that try to grab the same lock, and that may |
| 107 | * lead to a deadlock. |
| 108 | */ |
| 109 | if (phydev->attached_dev && phydev->adjust_link) |
| 110 | phy_stop_machine(phydev); |
| 111 | |
| 112 | if (!mdio_bus_phy_may_suspend(phydev)) |
| 113 | return 0; |
| 114 | |
| 115 | return phy_suspend(phydev); |
| 116 | } |
| 117 | |
| 118 | static int mdio_bus_phy_resume(struct device *dev) |
| 119 | { |
| 120 | struct phy_device *phydev = to_phy_device(dev); |
| 121 | int ret; |
| 122 | |
| 123 | if (!mdio_bus_phy_may_suspend(phydev)) |
| 124 | goto no_resume; |
| 125 | |
| 126 | ret = phy_resume(phydev); |
| 127 | if (ret < 0) |
| 128 | return ret; |
| 129 | |
| 130 | no_resume: |
| 131 | if (phydev->attached_dev && phydev->adjust_link) |
| 132 | phy_start_machine(phydev); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int mdio_bus_phy_restore(struct device *dev) |
| 138 | { |
| 139 | struct phy_device *phydev = to_phy_device(dev); |
| 140 | struct net_device *netdev = phydev->attached_dev; |
| 141 | int ret; |
| 142 | |
| 143 | if (!netdev) |
| 144 | return 0; |
| 145 | |
| 146 | ret = phy_init_hw(phydev); |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
| 150 | /* The PHY needs to renegotiate. */ |
| 151 | phydev->link = 0; |
| 152 | phydev->state = PHY_UP; |
| 153 | |
| 154 | phy_start_machine(phydev); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static const struct dev_pm_ops mdio_bus_phy_pm_ops = { |
| 160 | .suspend = mdio_bus_phy_suspend, |
| 161 | .resume = mdio_bus_phy_resume, |
| 162 | .freeze = mdio_bus_phy_suspend, |
| 163 | .thaw = mdio_bus_phy_resume, |
| 164 | .restore = mdio_bus_phy_restore, |
| 165 | }; |
| 166 | |
| 167 | #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops) |
| 168 | |
| 169 | #else |
| 170 | |
| 171 | #define MDIO_BUS_PHY_PM_OPS NULL |
| 172 | |
| 173 | #endif /* CONFIG_PM */ |
| 174 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 175 | /** |
| 176 | * phy_register_fixup - creates a new phy_fixup and adds it to the list |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 177 | * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 178 | * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 179 | * It can also be PHY_ANY_UID |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 180 | * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 181 | * comparison |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 182 | * @run: The actual code to be run when a matching PHY is found |
| 183 | */ |
| 184 | int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 185 | int (*run)(struct phy_device *)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 186 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 187 | struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 188 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 189 | if (!fixup) |
| 190 | return -ENOMEM; |
| 191 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 192 | strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 193 | fixup->phy_uid = phy_uid; |
| 194 | fixup->phy_uid_mask = phy_uid_mask; |
| 195 | fixup->run = run; |
| 196 | |
| 197 | mutex_lock(&phy_fixup_lock); |
| 198 | list_add_tail(&fixup->list, &phy_fixup_list); |
| 199 | mutex_unlock(&phy_fixup_lock); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | EXPORT_SYMBOL(phy_register_fixup); |
| 204 | |
| 205 | /* Registers a fixup to be run on any PHY with the UID in phy_uid */ |
| 206 | int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 207 | int (*run)(struct phy_device *)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 208 | { |
| 209 | return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); |
| 210 | } |
| 211 | EXPORT_SYMBOL(phy_register_fixup_for_uid); |
| 212 | |
| 213 | /* Registers a fixup to be run on the PHY with id string bus_id */ |
| 214 | int phy_register_fixup_for_id(const char *bus_id, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 215 | int (*run)(struct phy_device *)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 216 | { |
| 217 | return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); |
| 218 | } |
| 219 | EXPORT_SYMBOL(phy_register_fixup_for_id); |
| 220 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 221 | /* Returns 1 if fixup matches phydev in bus_id and phy_uid. |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 222 | * Fixups can be set to match any in one or more fields. |
| 223 | */ |
| 224 | static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) |
| 225 | { |
Andrew Lunn | 84eff6d | 2016-01-06 20:11:10 +0100 | [diff] [blame] | 226 | if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 227 | if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) |
| 228 | return 0; |
| 229 | |
| 230 | if ((fixup->phy_uid & fixup->phy_uid_mask) != |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 231 | (phydev->phy_id & fixup->phy_uid_mask)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 232 | if (fixup->phy_uid != PHY_ANY_UID) |
| 233 | return 0; |
| 234 | |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | /* Runs any matching fixups for this phydev */ |
Sergei Shtylyov | fbfcec6 | 2014-01-05 03:28:27 +0300 | [diff] [blame] | 239 | static int phy_scan_fixups(struct phy_device *phydev) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 240 | { |
| 241 | struct phy_fixup *fixup; |
| 242 | |
| 243 | mutex_lock(&phy_fixup_lock); |
| 244 | list_for_each_entry(fixup, &phy_fixup_list, list) { |
| 245 | if (phy_needs_fixup(phydev, fixup)) { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 246 | int err = fixup->run(phydev); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 247 | |
Jiri Slaby | bc23283 | 2009-07-13 11:23:39 +0000 | [diff] [blame] | 248 | if (err < 0) { |
| 249 | mutex_unlock(&phy_fixup_lock); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 250 | return err; |
Jiri Slaby | bc23283 | 2009-07-13 11:23:39 +0000 | [diff] [blame] | 251 | } |
Florian Fainelli | b0ae009 | 2014-02-11 17:27:41 -0800 | [diff] [blame] | 252 | phydev->has_fixups = true; |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | mutex_unlock(&phy_fixup_lock); |
| 256 | |
| 257 | return 0; |
| 258 | } |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 259 | |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 260 | static int phy_bus_match(struct device *dev, struct device_driver *drv) |
| 261 | { |
| 262 | struct phy_device *phydev = to_phy_device(dev); |
| 263 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 264 | const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); |
| 265 | int i; |
| 266 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 267 | if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)) |
| 268 | return 0; |
| 269 | |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 270 | if (phydrv->match_phy_device) |
| 271 | return phydrv->match_phy_device(phydev); |
| 272 | |
| 273 | if (phydev->is_c45) { |
| 274 | for (i = 1; i < num_ids; i++) { |
| 275 | if (!(phydev->c45_ids.devices_in_package & (1 << i))) |
| 276 | continue; |
| 277 | |
| 278 | if ((phydrv->phy_id & phydrv->phy_id_mask) == |
| 279 | (phydev->c45_ids.device_ids[i] & |
| 280 | phydrv->phy_id_mask)) |
| 281 | return 1; |
| 282 | } |
| 283 | return 0; |
| 284 | } else { |
| 285 | return (phydrv->phy_id & phydrv->phy_id_mask) == |
| 286 | (phydev->phy_id & phydrv->phy_id_mask); |
| 287 | } |
| 288 | } |
| 289 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 290 | struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 291 | bool is_c45, |
| 292 | struct phy_c45_device_ids *c45_ids) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 293 | { |
| 294 | struct phy_device *dev; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 295 | struct mdio_device *mdiodev; |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 296 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 297 | /* We allocate the device, and initialize the default values */ |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 298 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Sergei Shtylyov | ef899c0 | 2015-08-28 21:35:14 +0300 | [diff] [blame] | 299 | if (!dev) |
Sergei Shtylyov | d3765f0 | 2015-08-28 21:34:34 +0300 | [diff] [blame] | 300 | return ERR_PTR(-ENOMEM); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 301 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 302 | mdiodev = &dev->mdio; |
| 303 | mdiodev->dev.release = phy_device_release; |
| 304 | mdiodev->dev.parent = &bus->dev; |
| 305 | mdiodev->dev.bus = &mdio_bus_type; |
| 306 | mdiodev->bus = bus; |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 307 | mdiodev->pm_ops = MDIO_BUS_PHY_PM_OPS; |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 308 | mdiodev->bus_match = phy_bus_match; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 309 | mdiodev->addr = addr; |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 310 | mdiodev->flags = MDIO_DEVICE_FLAG_PHY; |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 311 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 312 | dev->speed = 0; |
| 313 | dev->duplex = -1; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 314 | dev->pause = 0; |
| 315 | dev->asym_pause = 0; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 316 | dev->link = 1; |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 317 | dev->interface = PHY_INTERFACE_MODE_GMII; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 318 | |
| 319 | dev->autoneg = AUTONEG_ENABLE; |
| 320 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 321 | dev->is_c45 = is_c45; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 322 | dev->phy_id = phy_id; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 323 | if (c45_ids) |
| 324 | dev->c45_ids = *c45_ids; |
Sergei Shtylyov | ef899c0 | 2015-08-28 21:35:14 +0300 | [diff] [blame] | 325 | dev->irq = bus->irq ? bus->irq[addr] : PHY_POLL; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 326 | dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 327 | |
| 328 | dev->state = PHY_DOWN; |
| 329 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 330 | mutex_init(&dev->lock); |
Anton Vorontsov | 4f9c85a | 2010-01-18 05:37:16 +0000 | [diff] [blame] | 331 | INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); |
Florian Fainelli | 5ea94e7 | 2013-05-19 22:53:43 +0000 | [diff] [blame] | 332 | INIT_WORK(&dev->phy_queue, phy_change); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 333 | |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 334 | /* Request the appropriate module unconditionally; don't |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 335 | * bother trying to do so only if it isn't already loaded, |
| 336 | * because that gets complicated. A hotplug event would have |
| 337 | * done an unconditional modprobe anyway. |
| 338 | * We don't do normal hotplug because it won't work for MDIO |
| 339 | * -- because it relies on the device staying around for long |
| 340 | * enough for the driver to get loaded. With MDIO, the NIC |
| 341 | * driver will get bored and give up as soon as it finds that |
| 342 | * there's no driver _already_ loaded. |
| 343 | */ |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 344 | request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id)); |
| 345 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 346 | device_initialize(&mdiodev->dev); |
Petr Malat | b2a4319 | 2013-02-28 01:01:52 +0000 | [diff] [blame] | 347 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 348 | return dev; |
| 349 | } |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 350 | EXPORT_SYMBOL(phy_device_create); |
| 351 | |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 352 | /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. |
| 353 | * @bus: the target MII bus |
| 354 | * @addr: PHY address on the MII bus |
| 355 | * @dev_addr: MMD address in the PHY. |
| 356 | * @devices_in_package: where to store the devices in package information. |
| 357 | * |
| 358 | * Description: reads devices in package registers of a MMD at @dev_addr |
| 359 | * from PHY at @addr on @bus. |
| 360 | * |
| 361 | * Returns: 0 on success, -EIO on failure. |
| 362 | */ |
| 363 | static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr, |
| 364 | u32 *devices_in_package) |
| 365 | { |
| 366 | int phy_reg, reg_addr; |
| 367 | |
| 368 | reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2; |
| 369 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 370 | if (phy_reg < 0) |
| 371 | return -EIO; |
| 372 | *devices_in_package = (phy_reg & 0xffff) << 16; |
| 373 | |
| 374 | reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1; |
| 375 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 376 | if (phy_reg < 0) |
| 377 | return -EIO; |
| 378 | *devices_in_package |= (phy_reg & 0xffff); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 383 | /** |
| 384 | * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. |
| 385 | * @bus: the target MII bus |
| 386 | * @addr: PHY address on the MII bus |
| 387 | * @phy_id: where to store the ID retrieved. |
| 388 | * @c45_ids: where to store the c45 ID information. |
| 389 | * |
| 390 | * If the PHY devices-in-package appears to be valid, it and the |
| 391 | * corresponding identifiers are stored in @c45_ids, zero is stored |
| 392 | * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns |
| 393 | * zero on success. |
| 394 | * |
| 395 | */ |
| 396 | static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id, |
| 397 | struct phy_c45_device_ids *c45_ids) { |
| 398 | int phy_reg; |
| 399 | int i, reg_addr; |
| 400 | const int num_ids = ARRAY_SIZE(c45_ids->device_ids); |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 401 | u32 *devs = &c45_ids->devices_in_package; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 402 | |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 403 | /* Find first non-zero Devices In package. Device zero is reserved |
| 404 | * for 802.3 c45 complied PHYs, so don't probe it at first. |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 405 | */ |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 406 | for (i = 1; i < num_ids && *devs == 0; i++) { |
| 407 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs); |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 408 | if (phy_reg < 0) |
| 409 | return -EIO; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 410 | |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 411 | if ((*devs & 0x1fffffff) == 0x1fffffff) { |
| 412 | /* If mostly Fs, there is no device there, |
| 413 | * then let's continue to probe more, as some |
| 414 | * 10G PHYs have zero Devices In package, |
| 415 | * e.g. Cortina CS4315/CS4340 PHY. |
| 416 | */ |
| 417 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs); |
| 418 | if (phy_reg < 0) |
| 419 | return -EIO; |
| 420 | /* no device there, let's get out of here */ |
| 421 | if ((*devs & 0x1fffffff) == 0x1fffffff) { |
Shengzhou Liu | da1da28 | 2015-06-26 17:58:52 +0800 | [diff] [blame] | 422 | *phy_id = 0xffffffff; |
| 423 | return 0; |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 424 | } else { |
| 425 | break; |
Shengzhou Liu | da1da28 | 2015-06-26 17:58:52 +0800 | [diff] [blame] | 426 | } |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
| 430 | /* Now probe Device Identifiers for each device present. */ |
| 431 | for (i = 1; i < num_ids; i++) { |
| 432 | if (!(c45_ids->devices_in_package & (1 << i))) |
| 433 | continue; |
| 434 | |
| 435 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1; |
| 436 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 437 | if (phy_reg < 0) |
| 438 | return -EIO; |
| 439 | c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16; |
| 440 | |
| 441 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2; |
| 442 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 443 | if (phy_reg < 0) |
| 444 | return -EIO; |
| 445 | c45_ids->device_ids[i] |= (phy_reg & 0xffff); |
| 446 | } |
| 447 | *phy_id = 0; |
| 448 | return 0; |
| 449 | } |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 450 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 451 | /** |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 452 | * get_phy_id - reads the specified addr for its ID. |
| 453 | * @bus: the target MII bus |
| 454 | * @addr: PHY address on the MII bus |
| 455 | * @phy_id: where to store the ID retrieved. |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 456 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
| 457 | * @c45_ids: where to store the c45 ID information. |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 458 | * |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 459 | * Description: In the case of a 802.3-c22 PHY, reads the ID registers |
| 460 | * of the PHY at @addr on the @bus, stores it in @phy_id and returns |
| 461 | * zero on success. |
| 462 | * |
| 463 | * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and |
| 464 | * its return value is in turn returned. |
| 465 | * |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 466 | */ |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 467 | static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id, |
| 468 | bool is_c45, struct phy_c45_device_ids *c45_ids) |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 469 | { |
| 470 | int phy_reg; |
| 471 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 472 | if (is_c45) |
| 473 | return get_phy_c45_ids(bus, addr, phy_id, c45_ids); |
| 474 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 475 | /* Grab the bits from PHYIR1, and put them in the upper half */ |
David Daney | 6fe3264 | 2011-09-30 11:51:22 +0000 | [diff] [blame] | 476 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 477 | if (phy_reg < 0) |
| 478 | return -EIO; |
| 479 | |
| 480 | *phy_id = (phy_reg & 0xffff) << 16; |
| 481 | |
| 482 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
David Daney | 6fe3264 | 2011-09-30 11:51:22 +0000 | [diff] [blame] | 483 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 484 | if (phy_reg < 0) |
| 485 | return -EIO; |
| 486 | |
| 487 | *phy_id |= (phy_reg & 0xffff); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /** |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 493 | * get_phy_device - reads the specified PHY device and returns its @phy_device |
| 494 | * struct |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 495 | * @bus: the target MII bus |
| 496 | * @addr: PHY address on the MII bus |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 497 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 498 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 499 | * Description: Reads the ID registers of the PHY at @addr on the |
| 500 | * @bus, then allocates and returns the phy_device to represent it. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 501 | */ |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 502 | struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 503 | { |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 504 | struct phy_c45_device_ids c45_ids = {0}; |
David S. Miller | 160c85f | 2012-06-27 21:28:14 -0700 | [diff] [blame] | 505 | u32 phy_id = 0; |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 506 | int r; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 507 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 508 | r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 509 | if (r) |
| 510 | return ERR_PTR(r); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 511 | |
Giuseppe Cavallaro | 6436cbc | 2008-11-20 20:43:18 -0800 | [diff] [blame] | 512 | /* If the phy_id is mostly Fs, there is no device there */ |
| 513 | if ((phy_id & 0x1fffffff) == 0x1fffffff) |
| 514 | return NULL; |
| 515 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 516 | return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 517 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 518 | EXPORT_SYMBOL(get_phy_device); |
| 519 | |
Andrew Lunn | 5cf11be | 2016-01-06 20:11:19 +0100 | [diff] [blame] | 520 | static ssize_t |
| 521 | phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 522 | { |
| 523 | struct phy_device *phydev = to_phy_device(dev); |
| 524 | |
| 525 | return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); |
| 526 | } |
| 527 | static DEVICE_ATTR_RO(phy_id); |
| 528 | |
| 529 | static ssize_t |
| 530 | phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 531 | { |
| 532 | struct phy_device *phydev = to_phy_device(dev); |
| 533 | const char *mode = NULL; |
| 534 | |
| 535 | if (phy_is_internal(phydev)) |
| 536 | mode = "internal"; |
| 537 | else |
| 538 | mode = phy_modes(phydev->interface); |
| 539 | |
| 540 | return sprintf(buf, "%s\n", mode); |
| 541 | } |
| 542 | static DEVICE_ATTR_RO(phy_interface); |
| 543 | |
| 544 | static ssize_t |
| 545 | phy_has_fixups_show(struct device *dev, struct device_attribute *attr, |
| 546 | char *buf) |
| 547 | { |
| 548 | struct phy_device *phydev = to_phy_device(dev); |
| 549 | |
| 550 | return sprintf(buf, "%d\n", phydev->has_fixups); |
| 551 | } |
| 552 | static DEVICE_ATTR_RO(phy_has_fixups); |
| 553 | |
| 554 | static struct attribute *phy_dev_attrs[] = { |
| 555 | &dev_attr_phy_id.attr, |
| 556 | &dev_attr_phy_interface.attr, |
| 557 | &dev_attr_phy_has_fixups.attr, |
| 558 | NULL, |
| 559 | }; |
| 560 | ATTRIBUTE_GROUPS(phy_dev); |
| 561 | |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 562 | /** |
| 563 | * phy_device_register - Register the phy device on the MDIO bus |
Randy Dunlap | 1d4ac5d | 2009-06-16 16:56:33 +0000 | [diff] [blame] | 564 | * @phydev: phy_device structure to be added to the MDIO bus |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 565 | */ |
| 566 | int phy_device_register(struct phy_device *phydev) |
| 567 | { |
| 568 | int err; |
| 569 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 570 | err = mdiobus_register_device(&phydev->mdio); |
| 571 | if (err) |
| 572 | return err; |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 573 | |
| 574 | /* Run all of the fixups for this PHY */ |
Florian Fainelli | d92f5de | 2014-07-28 16:28:07 -0700 | [diff] [blame] | 575 | err = phy_scan_fixups(phydev); |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 576 | if (err) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 577 | pr_err("PHY %d failed to initialize\n", phydev->mdio.addr); |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 578 | goto out; |
| 579 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 580 | |
Andrew Lunn | 5cf11be | 2016-01-06 20:11:19 +0100 | [diff] [blame] | 581 | phydev->mdio.dev.groups = phy_dev_groups; |
| 582 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 583 | err = device_add(&phydev->mdio.dev); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 584 | if (err) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 585 | pr_err("PHY %d failed to add\n", phydev->mdio.addr); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 586 | goto out; |
| 587 | } |
| 588 | |
| 589 | return 0; |
| 590 | |
| 591 | out: |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 592 | mdiobus_unregister_device(&phydev->mdio); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 593 | return err; |
| 594 | } |
| 595 | EXPORT_SYMBOL(phy_device_register); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 596 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 597 | /** |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 598 | * phy_device_remove - Remove a previously registered phy device from the MDIO bus |
| 599 | * @phydev: phy_device structure to remove |
| 600 | * |
| 601 | * This doesn't free the phy_device itself, it merely reverses the effects |
| 602 | * of phy_device_register(). Use phy_device_free() to free the device |
| 603 | * after calling this function. |
| 604 | */ |
| 605 | void phy_device_remove(struct phy_device *phydev) |
| 606 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 607 | device_del(&phydev->mdio.dev); |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 608 | mdiobus_unregister_device(&phydev->mdio); |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 609 | } |
| 610 | EXPORT_SYMBOL(phy_device_remove); |
| 611 | |
| 612 | /** |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 613 | * phy_find_first - finds the first PHY device on the bus |
| 614 | * @bus: the target MII bus |
| 615 | */ |
| 616 | struct phy_device *phy_find_first(struct mii_bus *bus) |
| 617 | { |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 618 | struct phy_device *phydev; |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 619 | int addr; |
| 620 | |
| 621 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 622 | phydev = mdiobus_get_phy(bus, addr); |
| 623 | if (phydev) |
| 624 | return phydev; |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 625 | } |
| 626 | return NULL; |
| 627 | } |
| 628 | EXPORT_SYMBOL(phy_find_first); |
| 629 | |
| 630 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 631 | * phy_prepare_link - prepares the PHY layer to monitor link status |
| 632 | * @phydev: target phy_device struct |
| 633 | * @handler: callback function for link status change notifications |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 634 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 635 | * Description: Tells the PHY infrastructure to handle the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 636 | * gory details on monitoring link status (whether through |
| 637 | * polling or an interrupt), and to call back to the |
| 638 | * connected device driver when the link status changes. |
| 639 | * If you want to monitor your own link state, don't call |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 640 | * this function. |
| 641 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 642 | static void phy_prepare_link(struct phy_device *phydev, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 643 | void (*handler)(struct net_device *)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 644 | { |
| 645 | phydev->adjust_link = handler; |
| 646 | } |
| 647 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 648 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 649 | * phy_connect_direct - connect an ethernet device to a specific phy_device |
| 650 | * @dev: the network device to connect |
| 651 | * @phydev: the pointer to the phy device |
| 652 | * @handler: callback function for state change notifications |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 653 | * @interface: PHY device's interface |
| 654 | */ |
| 655 | int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 656 | void (*handler)(struct net_device *), |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 657 | phy_interface_t interface) |
| 658 | { |
| 659 | int rc; |
| 660 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 661 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 662 | if (rc) |
| 663 | return rc; |
| 664 | |
| 665 | phy_prepare_link(phydev, handler); |
Sergei Shtylyov | 29935ae | 2014-01-05 03:27:17 +0300 | [diff] [blame] | 666 | phy_start_machine(phydev); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 667 | if (phydev->irq > 0) |
| 668 | phy_start_interrupts(phydev); |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | EXPORT_SYMBOL(phy_connect_direct); |
| 673 | |
| 674 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 675 | * phy_connect - connect an ethernet device to a PHY device |
| 676 | * @dev: the network device to connect |
Randy Dunlap | 5d12b13 | 2008-04-28 10:58:22 -0700 | [diff] [blame] | 677 | * @bus_id: the id string of the PHY device to connect |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 678 | * @handler: callback function for state change notifications |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 679 | * @interface: PHY device's interface |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 680 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 681 | * Description: Convenience function for connecting ethernet |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 682 | * devices to PHY devices. The default behavior is for |
| 683 | * the PHY infrastructure to handle everything, and only notify |
| 684 | * the connected driver when the link status changes. If you |
| 685 | * don't want, or can't use the provided functionality, you may |
| 686 | * choose to call only the subset of functions which provide |
| 687 | * the desired functionality. |
| 688 | */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 689 | struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 690 | void (*handler)(struct net_device *), |
| 691 | phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 692 | { |
| 693 | struct phy_device *phydev; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 694 | struct device *d; |
| 695 | int rc; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 696 | |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 697 | /* Search the list of PHY devices on the mdio bus for the |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 698 | * PHY with the requested name |
| 699 | */ |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 700 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
| 701 | if (!d) { |
| 702 | pr_err("PHY %s not found\n", bus_id); |
| 703 | return ERR_PTR(-ENODEV); |
| 704 | } |
| 705 | phydev = to_phy_device(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 706 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 707 | rc = phy_connect_direct(dev, phydev, handler, interface); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 708 | if (rc) |
| 709 | return ERR_PTR(rc); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 710 | |
| 711 | return phydev; |
| 712 | } |
| 713 | EXPORT_SYMBOL(phy_connect); |
| 714 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 715 | /** |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 716 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY |
| 717 | * device |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 718 | * @phydev: target phy_device struct |
| 719 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 720 | void phy_disconnect(struct phy_device *phydev) |
| 721 | { |
| 722 | if (phydev->irq > 0) |
| 723 | phy_stop_interrupts(phydev); |
| 724 | |
| 725 | phy_stop_machine(phydev); |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 726 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 727 | phydev->adjust_link = NULL; |
| 728 | |
| 729 | phy_detach(phydev); |
| 730 | } |
| 731 | EXPORT_SYMBOL(phy_disconnect); |
| 732 | |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 733 | /** |
| 734 | * phy_poll_reset - Safely wait until a PHY reset has properly completed |
| 735 | * @phydev: The PHY device to poll |
| 736 | * |
| 737 | * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as |
| 738 | * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR |
| 739 | * register must be polled until the BMCR_RESET bit clears. |
| 740 | * |
| 741 | * Furthermore, any attempts to write to PHY registers may have no effect |
| 742 | * or even generate MDIO bus errors until this is complete. |
| 743 | * |
| 744 | * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the |
| 745 | * standard and do not fully reset after the BMCR_RESET bit is set, and may |
| 746 | * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an |
| 747 | * effort to support such broken PHYs, this function is separate from the |
| 748 | * standard phy_init_hw() which will zero all the other bits in the BMCR |
| 749 | * and reapply all driver-specific and board-specific fixups. |
| 750 | */ |
| 751 | static int phy_poll_reset(struct phy_device *phydev) |
| 752 | { |
| 753 | /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ |
| 754 | unsigned int retries = 12; |
| 755 | int ret; |
| 756 | |
| 757 | do { |
| 758 | msleep(50); |
| 759 | ret = phy_read(phydev, MII_BMCR); |
| 760 | if (ret < 0) |
| 761 | return ret; |
| 762 | } while (ret & BMCR_RESET && --retries); |
| 763 | if (ret & BMCR_RESET) |
| 764 | return -ETIMEDOUT; |
| 765 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 766 | /* Some chips (smsc911x) may still need up to another 1ms after the |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 767 | * BMCR_RESET bit is cleared before they are usable. |
| 768 | */ |
| 769 | msleep(1); |
| 770 | return 0; |
| 771 | } |
| 772 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 773 | int phy_init_hw(struct phy_device *phydev) |
| 774 | { |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 775 | int ret = 0; |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 776 | |
| 777 | if (!phydev->drv || !phydev->drv->config_init) |
| 778 | return 0; |
| 779 | |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 780 | if (phydev->drv->soft_reset) |
| 781 | ret = phydev->drv->soft_reset(phydev); |
| 782 | else |
| 783 | ret = genphy_soft_reset(phydev); |
| 784 | |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 785 | if (ret < 0) |
| 786 | return ret; |
| 787 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 788 | ret = phy_scan_fixups(phydev); |
| 789 | if (ret < 0) |
| 790 | return ret; |
| 791 | |
| 792 | return phydev->drv->config_init(phydev); |
| 793 | } |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 794 | EXPORT_SYMBOL(phy_init_hw); |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 795 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 796 | void phy_attached_info(struct phy_device *phydev) |
| 797 | { |
| 798 | phy_attached_print(phydev, NULL); |
| 799 | } |
| 800 | EXPORT_SYMBOL(phy_attached_info); |
| 801 | |
| 802 | #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)" |
| 803 | void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) |
| 804 | { |
| 805 | if (!fmt) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 806 | dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n", |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 807 | phydev->drv->name, phydev_name(phydev), |
| 808 | phydev->irq); |
| 809 | } else { |
| 810 | va_list ap; |
| 811 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 812 | dev_info(&phydev->mdio.dev, ATTACHED_FMT, |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 813 | phydev->drv->name, phydev_name(phydev), |
| 814 | phydev->irq); |
| 815 | |
| 816 | va_start(ap, fmt); |
| 817 | vprintk(fmt, ap); |
| 818 | va_end(ap); |
| 819 | } |
| 820 | } |
| 821 | EXPORT_SYMBOL(phy_attached_print); |
| 822 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 823 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 824 | * phy_attach_direct - attach a network device to a given PHY device pointer |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 825 | * @dev: network device to attach |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 826 | * @phydev: Pointer to phy_device to attach |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 827 | * @flags: PHY device's dev_flags |
| 828 | * @interface: PHY device's interface |
| 829 | * |
| 830 | * Description: Called by drivers to attach to a particular PHY |
| 831 | * device. The phy_device is found, and properly hooked up |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 832 | * to the phy_driver. If no driver is attached, then a |
| 833 | * generic driver is used. The phy_device is given a ptr to |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 834 | * the attaching device, and given a callback for link status |
| 835 | * change. The phy_device is returned to the attaching driver. |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 836 | * This function takes a reference on the phy device. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 837 | */ |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 838 | int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
| 839 | u32 flags, phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 840 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 841 | struct mii_bus *bus = phydev->mdio.bus; |
| 842 | struct device *d = &phydev->mdio.dev; |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 843 | int err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 844 | |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 845 | if (!try_module_get(bus->owner)) { |
| 846 | dev_err(&dev->dev, "failed to get the bus module\n"); |
| 847 | return -EIO; |
| 848 | } |
| 849 | |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 850 | get_device(d); |
| 851 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 852 | /* Assume that if there is no driver, that it doesn't |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 853 | * exist, and we should use the genphy driver. |
| 854 | */ |
Sergei Shtylyov | ef899c0 | 2015-08-28 21:35:14 +0300 | [diff] [blame] | 855 | if (!d->driver) { |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 856 | if (phydev->is_c45) |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 857 | d->driver = |
| 858 | &genphy_driver[GENPHY_DRV_10G].mdiodrv.driver; |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 859 | else |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 860 | d->driver = |
| 861 | &genphy_driver[GENPHY_DRV_1G].mdiodrv.driver; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 862 | |
| 863 | err = d->driver->probe(d); |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 864 | if (err >= 0) |
| 865 | err = device_bind_driver(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 866 | |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 867 | if (err) |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 868 | goto error; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | if (phydev->attached_dev) { |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 872 | dev_err(&dev->dev, "PHY already attached\n"); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 873 | err = -EBUSY; |
| 874 | goto error; |
Ezequiel Garcia | b3565f2 | 2014-07-23 16:47:32 -0300 | [diff] [blame] | 875 | } |
| 876 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 877 | phydev->attached_dev = dev; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 878 | dev->phydev = phydev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 879 | |
| 880 | phydev->dev_flags = flags; |
| 881 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 882 | phydev->interface = interface; |
| 883 | |
Anton Vorontsov | ef24b16 | 2010-08-24 14:46:12 -0700 | [diff] [blame] | 884 | phydev->state = PHY_READY; |
| 885 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 886 | /* Do initial configuration here, now that |
| 887 | * we have certain key parameters |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 888 | * (dev_flags and interface) |
| 889 | */ |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 890 | err = phy_init_hw(phydev); |
| 891 | if (err) |
| 892 | phy_detach(phydev); |
Guenter Roeck | b394745 | 2014-05-14 13:12:49 -0700 | [diff] [blame] | 893 | else |
| 894 | phy_resume(phydev); |
Sebastian Hesselbarth | 1211ce5 | 2013-12-13 10:20:28 +0100 | [diff] [blame] | 895 | |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 896 | return err; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 897 | |
| 898 | error: |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 899 | put_device(d); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 900 | module_put(bus->owner); |
| 901 | return err; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 902 | } |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 903 | EXPORT_SYMBOL(phy_attach_direct); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 904 | |
| 905 | /** |
| 906 | * phy_attach - attach a network device to a particular PHY device |
| 907 | * @dev: network device to attach |
| 908 | * @bus_id: Bus ID of PHY device to attach |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 909 | * @interface: PHY device's interface |
| 910 | * |
| 911 | * Description: Same as phy_attach_direct() except that a PHY bus_id |
| 912 | * string is passed instead of a pointer to a struct phy_device. |
| 913 | */ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 914 | struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, |
| 915 | phy_interface_t interface) |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 916 | { |
| 917 | struct bus_type *bus = &mdio_bus_type; |
| 918 | struct phy_device *phydev; |
| 919 | struct device *d; |
| 920 | int rc; |
| 921 | |
| 922 | /* Search the list of PHY devices on the mdio bus for the |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 923 | * PHY with the requested name |
| 924 | */ |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 925 | d = bus_find_device_by_name(bus, NULL, bus_id); |
| 926 | if (!d) { |
| 927 | pr_err("PHY %s not found\n", bus_id); |
| 928 | return ERR_PTR(-ENODEV); |
| 929 | } |
| 930 | phydev = to_phy_device(d); |
| 931 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 932 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 933 | if (rc) |
| 934 | return ERR_PTR(rc); |
| 935 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 936 | return phydev; |
| 937 | } |
| 938 | EXPORT_SYMBOL(phy_attach); |
| 939 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 940 | /** |
| 941 | * phy_detach - detach a PHY device from its network device |
| 942 | * @phydev: target phy_device struct |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 943 | * |
| 944 | * This detaches the phy device from its network device and the phy |
| 945 | * driver, and drops the reference count taken in phy_attach_direct(). |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 946 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 947 | void phy_detach(struct phy_device *phydev) |
| 948 | { |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 949 | struct mii_bus *bus; |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 950 | int i; |
Ezequiel Garcia | b3565f2 | 2014-07-23 16:47:32 -0300 | [diff] [blame] | 951 | |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 952 | phydev->attached_dev->phydev = NULL; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 953 | phydev->attached_dev = NULL; |
Sebastian Hesselbarth | 1211ce5 | 2013-12-13 10:20:28 +0100 | [diff] [blame] | 954 | phy_suspend(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 955 | |
| 956 | /* If the device had no specific driver before (i.e. - it |
| 957 | * was using the generic driver), we unbind the device |
| 958 | * from the generic driver so that there's a chance a |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 959 | * real driver could be loaded |
| 960 | */ |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 961 | for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) { |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 962 | if (phydev->mdio.dev.driver == |
| 963 | &genphy_driver[i].mdiodrv.driver) { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 964 | device_release_driver(&phydev->mdio.dev); |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | } |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 968 | |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 969 | /* |
| 970 | * The phydev might go away on the put_device() below, so avoid |
| 971 | * a use-after-free bug by reading the underlying bus first. |
| 972 | */ |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 973 | bus = phydev->mdio.bus; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 974 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 975 | put_device(&phydev->mdio.dev); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 976 | module_put(bus->owner); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 977 | } |
| 978 | EXPORT_SYMBOL(phy_detach); |
| 979 | |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 980 | int phy_suspend(struct phy_device *phydev) |
| 981 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 982 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
Sebastian Hesselbarth | 32fc3fd | 2014-03-14 10:07:44 +0100 | [diff] [blame] | 983 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 984 | int ret = 0; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 985 | |
| 986 | /* If the device has WOL enabled, we cannot suspend the PHY */ |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 987 | phy_ethtool_get_wol(phydev, &wol); |
| 988 | if (wol.wolopts) |
| 989 | return -EBUSY; |
| 990 | |
| 991 | if (phydrv->suspend) |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 992 | ret = phydrv->suspend(phydev); |
| 993 | |
| 994 | if (ret) |
| 995 | return ret; |
| 996 | |
| 997 | phydev->suspended = true; |
| 998 | |
| 999 | return ret; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1000 | } |
Florian Fainelli | ca12769 | 2014-07-08 10:38:36 -0700 | [diff] [blame] | 1001 | EXPORT_SYMBOL(phy_suspend); |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1002 | |
| 1003 | int phy_resume(struct phy_device *phydev) |
| 1004 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1005 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1006 | int ret = 0; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1007 | |
| 1008 | if (phydrv->resume) |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1009 | ret = phydrv->resume(phydev); |
| 1010 | |
| 1011 | if (ret) |
| 1012 | return ret; |
| 1013 | |
| 1014 | phydev->suspended = false; |
| 1015 | |
| 1016 | return ret; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1017 | } |
Florian Fainelli | ca12769 | 2014-07-08 10:38:36 -0700 | [diff] [blame] | 1018 | EXPORT_SYMBOL(phy_resume); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1019 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1020 | /* Generic PHY support and helper functions */ |
| 1021 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1022 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1023 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1024 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1025 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1026 | * Description: Writes MII_ADVERTISE with the appropriate values, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1027 | * after sanitizing the values to make sure we only advertise |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1028 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 1029 | * hasn't changed, and > 0 if it has changed. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1030 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 1031 | static int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1032 | { |
| 1033 | u32 advertise; |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1034 | int oldadv, adv, bmsr; |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1035 | int err, changed = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1036 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1037 | /* Only allow advertising what this PHY supports */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1038 | phydev->advertising &= phydev->supported; |
| 1039 | advertise = phydev->advertising; |
| 1040 | |
| 1041 | /* Setup standard advertisement */ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1042 | adv = phy_read(phydev, MII_ADVERTISE); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1043 | if (adv < 0) |
| 1044 | return adv; |
| 1045 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1046 | oldadv = adv; |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 1047 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1048 | ADVERTISE_PAUSE_ASYM); |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 1049 | adv |= ethtool_adv_to_mii_adv_t(advertise); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1050 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1051 | if (adv != oldadv) { |
| 1052 | err = phy_write(phydev, MII_ADVERTISE, adv); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1053 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1054 | if (err < 0) |
| 1055 | return err; |
| 1056 | changed = 1; |
| 1057 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1058 | |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1059 | bmsr = phy_read(phydev, MII_BMSR); |
| 1060 | if (bmsr < 0) |
| 1061 | return bmsr; |
| 1062 | |
| 1063 | /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all |
| 1064 | * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a |
| 1065 | * logical 1. |
| 1066 | */ |
| 1067 | if (!(bmsr & BMSR_ESTATEN)) |
| 1068 | return changed; |
| 1069 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1070 | /* Configure gigabit if it's supported */ |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1071 | adv = phy_read(phydev, MII_CTRL1000); |
| 1072 | if (adv < 0) |
| 1073 | return adv; |
| 1074 | |
| 1075 | oldadv = adv; |
| 1076 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 1077 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1078 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1079 | SUPPORTED_1000baseT_Full)) { |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 1080 | adv |= ethtool_adv_to_mii_ctrl1000_t(advertise); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1081 | } |
| 1082 | |
Mugunthan V N | eb68623 | 2015-06-25 22:21:02 +0530 | [diff] [blame] | 1083 | if (adv != oldadv) |
| 1084 | changed = 1; |
| 1085 | |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1086 | err = phy_write(phydev, MII_CTRL1000, adv); |
| 1087 | if (err < 0) |
| 1088 | return err; |
| 1089 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1090 | return changed; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1091 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1092 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1093 | /** |
| 1094 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 1095 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1096 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1097 | * Description: Configures MII_BMCR to force speed/duplex |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1098 | * to the values in phydev. Assumes that the values are valid. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1099 | * Please see phy_sanitize_settings(). |
| 1100 | */ |
Madalin Bucur | 3fb69bc | 2013-11-20 16:38:19 -0600 | [diff] [blame] | 1101 | int genphy_setup_forced(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1102 | { |
Domen Puncer | bc1e0a0 | 2007-08-17 08:54:45 +0200 | [diff] [blame] | 1103 | int ctl = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1104 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1105 | phydev->pause = 0; |
| 1106 | phydev->asym_pause = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1107 | |
| 1108 | if (SPEED_1000 == phydev->speed) |
| 1109 | ctl |= BMCR_SPEED1000; |
| 1110 | else if (SPEED_100 == phydev->speed) |
| 1111 | ctl |= BMCR_SPEED100; |
| 1112 | |
| 1113 | if (DUPLEX_FULL == phydev->duplex) |
| 1114 | ctl |= BMCR_FULLDPLX; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1115 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 1116 | return phy_write(phydev, MII_BMCR, ctl); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1117 | } |
Madalin Bucur | 3fb69bc | 2013-11-20 16:38:19 -0600 | [diff] [blame] | 1118 | EXPORT_SYMBOL(genphy_setup_forced); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1119 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1120 | /** |
| 1121 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 1122 | * @phydev: target phy_device struct |
| 1123 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1124 | int genphy_restart_aneg(struct phy_device *phydev) |
| 1125 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 1126 | int ctl = phy_read(phydev, MII_BMCR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1127 | |
| 1128 | if (ctl < 0) |
| 1129 | return ctl; |
| 1130 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1131 | ctl |= BMCR_ANENABLE | BMCR_ANRESTART; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1132 | |
| 1133 | /* Don't isolate the PHY if we're negotiating */ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1134 | ctl &= ~BMCR_ISOLATE; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1135 | |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 1136 | return phy_write(phydev, MII_BMCR, ctl); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1137 | } |
Adrian Bunk | 892871d | 2008-10-13 18:48:09 -0700 | [diff] [blame] | 1138 | EXPORT_SYMBOL(genphy_restart_aneg); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1139 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1140 | /** |
| 1141 | * genphy_config_aneg - restart auto-negotiation or write BMCR |
| 1142 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1143 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1144 | * Description: If auto-negotiation is enabled, we configure the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1145 | * advertising, and then restart auto-negotiation. If it is not |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1146 | * enabled, then we write the BMCR. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1147 | */ |
| 1148 | int genphy_config_aneg(struct phy_device *phydev) |
| 1149 | { |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1150 | int result; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1151 | |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1152 | if (AUTONEG_ENABLE != phydev->autoneg) |
| 1153 | return genphy_setup_forced(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1154 | |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1155 | result = genphy_config_advert(phydev); |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1156 | if (result < 0) /* error */ |
| 1157 | return result; |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1158 | if (result == 0) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1159 | /* Advertisement hasn't changed, but maybe aneg was never on to |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1160 | * begin with? Or maybe phy was isolated? |
| 1161 | */ |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1162 | int ctl = phy_read(phydev, MII_BMCR); |
| 1163 | |
| 1164 | if (ctl < 0) |
| 1165 | return ctl; |
| 1166 | |
| 1167 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) |
| 1168 | result = 1; /* do restart aneg */ |
| 1169 | } |
| 1170 | |
| 1171 | /* Only restart aneg if we are advertising something different |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1172 | * than we were before. |
| 1173 | */ |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1174 | if (result > 0) |
| 1175 | result = genphy_restart_aneg(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1176 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1177 | return result; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1178 | } |
| 1179 | EXPORT_SYMBOL(genphy_config_aneg); |
| 1180 | |
Florian Fainelli | a9fa6e6 | 2014-02-11 17:27:36 -0800 | [diff] [blame] | 1181 | /** |
| 1182 | * genphy_aneg_done - return auto-negotiation status |
| 1183 | * @phydev: target phy_device struct |
| 1184 | * |
| 1185 | * Description: Reads the status register and returns 0 either if |
| 1186 | * auto-negotiation is incomplete, or if there was an error. |
| 1187 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. |
| 1188 | */ |
| 1189 | int genphy_aneg_done(struct phy_device *phydev) |
| 1190 | { |
| 1191 | int retval = phy_read(phydev, MII_BMSR); |
| 1192 | |
| 1193 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); |
| 1194 | } |
| 1195 | EXPORT_SYMBOL(genphy_aneg_done); |
| 1196 | |
stephen hemminger | 395056e | 2014-01-19 11:48:20 -0800 | [diff] [blame] | 1197 | static int gen10g_config_aneg(struct phy_device *phydev) |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1198 | { |
| 1199 | return 0; |
| 1200 | } |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1201 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1202 | /** |
| 1203 | * genphy_update_link - update link status in @phydev |
| 1204 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1205 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1206 | * Description: Update the value in phydev->link to reflect the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1207 | * current link value. In order to do this, we need to read |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1208 | * the status register twice, keeping the second value. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1209 | */ |
| 1210 | int genphy_update_link(struct phy_device *phydev) |
| 1211 | { |
| 1212 | int status; |
| 1213 | |
| 1214 | /* Do a fake read */ |
| 1215 | status = phy_read(phydev, MII_BMSR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1216 | if (status < 0) |
| 1217 | return status; |
| 1218 | |
| 1219 | /* Read link and autonegotiation status */ |
| 1220 | status = phy_read(phydev, MII_BMSR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1221 | if (status < 0) |
| 1222 | return status; |
| 1223 | |
| 1224 | if ((status & BMSR_LSTATUS) == 0) |
| 1225 | phydev->link = 0; |
| 1226 | else |
| 1227 | phydev->link = 1; |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 1231 | EXPORT_SYMBOL(genphy_update_link); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1232 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1233 | /** |
| 1234 | * genphy_read_status - check the link status and update current link state |
| 1235 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1236 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1237 | * Description: Check the link, then figure out the current state |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1238 | * by comparing what we advertise with what the link partner |
| 1239 | * advertises. Start by checking the gigabit possibilities, |
| 1240 | * then move on to 10/100. |
| 1241 | */ |
| 1242 | int genphy_read_status(struct phy_device *phydev) |
| 1243 | { |
| 1244 | int adv; |
| 1245 | int err; |
| 1246 | int lpa; |
| 1247 | int lpagb = 0; |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1248 | int common_adv; |
| 1249 | int common_adv_gb = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1250 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1251 | /* Update the link, but return if there was an error */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1252 | err = genphy_update_link(phydev); |
| 1253 | if (err) |
| 1254 | return err; |
| 1255 | |
Florian Fainelli | 114002b | 2013-12-06 13:01:30 -0800 | [diff] [blame] | 1256 | phydev->lp_advertising = 0; |
| 1257 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1258 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 1259 | if (phydev->supported & (SUPPORTED_1000baseT_Half |
| 1260 | | SUPPORTED_1000baseT_Full)) { |
| 1261 | lpagb = phy_read(phydev, MII_STAT1000); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1262 | if (lpagb < 0) |
| 1263 | return lpagb; |
| 1264 | |
| 1265 | adv = phy_read(phydev, MII_CTRL1000); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1266 | if (adv < 0) |
| 1267 | return adv; |
| 1268 | |
Florian Fainelli | 114002b | 2013-12-06 13:01:30 -0800 | [diff] [blame] | 1269 | phydev->lp_advertising = |
| 1270 | mii_stat1000_to_ethtool_lpa_t(lpagb); |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1271 | common_adv_gb = lpagb & adv << 2; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | lpa = phy_read(phydev, MII_LPA); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1275 | if (lpa < 0) |
| 1276 | return lpa; |
| 1277 | |
Florian Fainelli | 114002b | 2013-12-06 13:01:30 -0800 | [diff] [blame] | 1278 | phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa); |
| 1279 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1280 | adv = phy_read(phydev, MII_ADVERTISE); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1281 | if (adv < 0) |
| 1282 | return adv; |
| 1283 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1284 | common_adv = lpa & adv; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1285 | |
| 1286 | phydev->speed = SPEED_10; |
| 1287 | phydev->duplex = DUPLEX_HALF; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1288 | phydev->pause = 0; |
| 1289 | phydev->asym_pause = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1290 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1291 | if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1292 | phydev->speed = SPEED_1000; |
| 1293 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1294 | if (common_adv_gb & LPA_1000FULL) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1295 | phydev->duplex = DUPLEX_FULL; |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1296 | } else if (common_adv & (LPA_100FULL | LPA_100HALF)) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1297 | phydev->speed = SPEED_100; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1298 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1299 | if (common_adv & LPA_100FULL) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1300 | phydev->duplex = DUPLEX_FULL; |
| 1301 | } else |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1302 | if (common_adv & LPA_10FULL) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1303 | phydev->duplex = DUPLEX_FULL; |
| 1304 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1305 | if (phydev->duplex == DUPLEX_FULL) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1306 | phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; |
| 1307 | phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; |
| 1308 | } |
| 1309 | } else { |
| 1310 | int bmcr = phy_read(phydev, MII_BMCR); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1311 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1312 | if (bmcr < 0) |
| 1313 | return bmcr; |
| 1314 | |
| 1315 | if (bmcr & BMCR_FULLDPLX) |
| 1316 | phydev->duplex = DUPLEX_FULL; |
| 1317 | else |
| 1318 | phydev->duplex = DUPLEX_HALF; |
| 1319 | |
| 1320 | if (bmcr & BMCR_SPEED1000) |
| 1321 | phydev->speed = SPEED_1000; |
| 1322 | else if (bmcr & BMCR_SPEED100) |
| 1323 | phydev->speed = SPEED_100; |
| 1324 | else |
| 1325 | phydev->speed = SPEED_10; |
| 1326 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1327 | phydev->pause = 0; |
| 1328 | phydev->asym_pause = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | EXPORT_SYMBOL(genphy_read_status); |
| 1334 | |
stephen hemminger | 395056e | 2014-01-19 11:48:20 -0800 | [diff] [blame] | 1335 | static int gen10g_read_status(struct phy_device *phydev) |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1336 | { |
| 1337 | int devad, reg; |
| 1338 | u32 mmd_mask = phydev->c45_ids.devices_in_package; |
| 1339 | |
| 1340 | phydev->link = 1; |
| 1341 | |
| 1342 | /* For now just lie and say it's 10G all the time */ |
| 1343 | phydev->speed = SPEED_10000; |
| 1344 | phydev->duplex = DUPLEX_FULL; |
| 1345 | |
| 1346 | for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) { |
| 1347 | if (!(mmd_mask & 1)) |
| 1348 | continue; |
| 1349 | |
| 1350 | /* Read twice because link state is latched and a |
| 1351 | * read moves the current state into the register |
| 1352 | */ |
| 1353 | phy_read_mmd(phydev, devad, MDIO_STAT1); |
| 1354 | reg = phy_read_mmd(phydev, devad, MDIO_STAT1); |
| 1355 | if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS)) |
| 1356 | phydev->link = 0; |
| 1357 | } |
| 1358 | |
| 1359 | return 0; |
| 1360 | } |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1361 | |
Florian Fainelli | 797ac07 | 2014-02-17 13:34:02 -0800 | [diff] [blame] | 1362 | /** |
| 1363 | * genphy_soft_reset - software reset the PHY via BMCR_RESET bit |
| 1364 | * @phydev: target phy_device struct |
| 1365 | * |
| 1366 | * Description: Perform a software PHY reset using the standard |
| 1367 | * BMCR_RESET bit and poll for the reset bit to be cleared. |
| 1368 | * |
| 1369 | * Returns: 0 on success, < 0 on failure |
| 1370 | */ |
| 1371 | int genphy_soft_reset(struct phy_device *phydev) |
| 1372 | { |
| 1373 | int ret; |
| 1374 | |
| 1375 | ret = phy_write(phydev, MII_BMCR, BMCR_RESET); |
| 1376 | if (ret < 0) |
| 1377 | return ret; |
| 1378 | |
| 1379 | return phy_poll_reset(phydev); |
| 1380 | } |
| 1381 | EXPORT_SYMBOL(genphy_soft_reset); |
| 1382 | |
Daniel Mack | af6b696 | 2014-04-16 17:19:12 +0200 | [diff] [blame] | 1383 | int genphy_config_init(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1384 | { |
Eric Sesterhenn | 84c22d7 | 2006-09-25 16:39:22 -0700 | [diff] [blame] | 1385 | int val; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1386 | u32 features; |
| 1387 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1388 | features = (SUPPORTED_TP | SUPPORTED_MII |
| 1389 | | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 1390 | SUPPORTED_BNC); |
| 1391 | |
| 1392 | /* Do we support autonegotiation? */ |
| 1393 | val = phy_read(phydev, MII_BMSR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1394 | if (val < 0) |
| 1395 | return val; |
| 1396 | |
| 1397 | if (val & BMSR_ANEGCAPABLE) |
| 1398 | features |= SUPPORTED_Autoneg; |
| 1399 | |
| 1400 | if (val & BMSR_100FULL) |
| 1401 | features |= SUPPORTED_100baseT_Full; |
| 1402 | if (val & BMSR_100HALF) |
| 1403 | features |= SUPPORTED_100baseT_Half; |
| 1404 | if (val & BMSR_10FULL) |
| 1405 | features |= SUPPORTED_10baseT_Full; |
| 1406 | if (val & BMSR_10HALF) |
| 1407 | features |= SUPPORTED_10baseT_Half; |
| 1408 | |
| 1409 | if (val & BMSR_ESTATEN) { |
| 1410 | val = phy_read(phydev, MII_ESTATUS); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1411 | if (val < 0) |
| 1412 | return val; |
| 1413 | |
| 1414 | if (val & ESTATUS_1000_TFULL) |
| 1415 | features |= SUPPORTED_1000baseT_Full; |
| 1416 | if (val & ESTATUS_1000_THALF) |
| 1417 | features |= SUPPORTED_1000baseT_Half; |
| 1418 | } |
| 1419 | |
Sascha Hauer | c242a47 | 2014-05-21 15:29:44 +0200 | [diff] [blame] | 1420 | phydev->supported &= features; |
| 1421 | phydev->advertising &= features; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1422 | |
| 1423 | return 0; |
| 1424 | } |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1425 | |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1426 | static int gen10g_soft_reset(struct phy_device *phydev) |
| 1427 | { |
| 1428 | /* Do nothing for now */ |
| 1429 | return 0; |
| 1430 | } |
Daniel Mack | af6b696 | 2014-04-16 17:19:12 +0200 | [diff] [blame] | 1431 | EXPORT_SYMBOL(genphy_config_init); |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1432 | |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1433 | static int gen10g_config_init(struct phy_device *phydev) |
| 1434 | { |
| 1435 | /* Temporarily just say we support everything */ |
| 1436 | phydev->supported = SUPPORTED_10000baseT_Full; |
| 1437 | phydev->advertising = SUPPORTED_10000baseT_Full; |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1442 | int genphy_suspend(struct phy_device *phydev) |
| 1443 | { |
| 1444 | int value; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1445 | |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1446 | mutex_lock(&phydev->lock); |
| 1447 | |
| 1448 | value = phy_read(phydev, MII_BMCR); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1449 | phy_write(phydev, MII_BMCR, value | BMCR_PDOWN); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1450 | |
| 1451 | mutex_unlock(&phydev->lock); |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | EXPORT_SYMBOL(genphy_suspend); |
| 1456 | |
stephen hemminger | 395056e | 2014-01-19 11:48:20 -0800 | [diff] [blame] | 1457 | static int gen10g_suspend(struct phy_device *phydev) |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1458 | { |
| 1459 | return 0; |
| 1460 | } |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1461 | |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1462 | int genphy_resume(struct phy_device *phydev) |
| 1463 | { |
| 1464 | int value; |
| 1465 | |
| 1466 | mutex_lock(&phydev->lock); |
| 1467 | |
| 1468 | value = phy_read(phydev, MII_BMCR); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1469 | phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1470 | |
| 1471 | mutex_unlock(&phydev->lock); |
| 1472 | |
| 1473 | return 0; |
| 1474 | } |
| 1475 | EXPORT_SYMBOL(genphy_resume); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1476 | |
stephen hemminger | 395056e | 2014-01-19 11:48:20 -0800 | [diff] [blame] | 1477 | static int gen10g_resume(struct phy_device *phydev) |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1478 | { |
| 1479 | return 0; |
| 1480 | } |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1481 | |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 1482 | static int __set_phy_supported(struct phy_device *phydev, u32 max_speed) |
| 1483 | { |
| 1484 | /* The default values for phydev->supported are provided by the PHY |
| 1485 | * driver "features" member, we want to reset to sane defaults first |
| 1486 | * before supporting higher speeds. |
| 1487 | */ |
| 1488 | phydev->supported &= PHY_DEFAULT_FEATURES; |
| 1489 | |
| 1490 | switch (max_speed) { |
| 1491 | default: |
| 1492 | return -ENOTSUPP; |
| 1493 | case SPEED_1000: |
| 1494 | phydev->supported |= PHY_1000BT_FEATURES; |
| 1495 | /* fall through */ |
| 1496 | case SPEED_100: |
| 1497 | phydev->supported |= PHY_100BT_FEATURES; |
| 1498 | /* fall through */ |
| 1499 | case SPEED_10: |
| 1500 | phydev->supported |= PHY_10BT_FEATURES; |
| 1501 | } |
| 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | int phy_set_max_speed(struct phy_device *phydev, u32 max_speed) |
| 1507 | { |
| 1508 | int err; |
| 1509 | |
| 1510 | err = __set_phy_supported(phydev, max_speed); |
| 1511 | if (err) |
| 1512 | return err; |
| 1513 | |
| 1514 | phydev->advertising = phydev->supported; |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | EXPORT_SYMBOL(phy_set_max_speed); |
| 1519 | |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 1520 | static void of_set_phy_supported(struct phy_device *phydev) |
| 1521 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1522 | struct device_node *node = phydev->mdio.dev.of_node; |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 1523 | u32 max_speed; |
| 1524 | |
| 1525 | if (!IS_ENABLED(CONFIG_OF_MDIO)) |
| 1526 | return; |
| 1527 | |
| 1528 | if (!node) |
| 1529 | return; |
| 1530 | |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 1531 | if (!of_property_read_u32(node, "max-speed", &max_speed)) |
| 1532 | __set_phy_supported(phydev, max_speed); |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 1533 | } |
| 1534 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1535 | /** |
| 1536 | * phy_probe - probe and init a PHY device |
| 1537 | * @dev: device to probe and init |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1538 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1539 | * Description: Take care of setting up the phy_device structure, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1540 | * set the state to READY (the driver's init function should |
| 1541 | * set it to STARTING if needed). |
| 1542 | */ |
| 1543 | static int phy_probe(struct device *dev) |
| 1544 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 1545 | struct phy_device *phydev = to_phy_device(dev); |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1546 | struct device_driver *drv = phydev->mdio.dev.driver; |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 1547 | struct phy_driver *phydrv = to_phy_driver(drv); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1548 | int err = 0; |
| 1549 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1550 | phydev->drv = phydrv; |
| 1551 | |
Florian Fainelli | 2c7b492 | 2013-05-19 22:53:42 +0000 | [diff] [blame] | 1552 | /* Disable the interrupt if the PHY doesn't support it |
| 1553 | * but the interrupt is still a valid one |
| 1554 | */ |
| 1555 | if (!(phydrv->flags & PHY_HAS_INTERRUPT) && |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1556 | phy_interrupt_is_valid(phydev)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1557 | phydev->irq = PHY_POLL; |
| 1558 | |
Florian Fainelli | 4284b6a | 2013-05-23 01:11:12 +0000 | [diff] [blame] | 1559 | if (phydrv->flags & PHY_IS_INTERNAL) |
| 1560 | phydev->is_internal = true; |
| 1561 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1562 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1563 | |
| 1564 | /* Start out supporting everything. Eventually, |
| 1565 | * a controller will attach, and may modify one |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1566 | * or both of these values |
| 1567 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1568 | phydev->supported = phydrv->features; |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 1569 | of_set_phy_supported(phydev); |
| 1570 | phydev->advertising = phydev->supported; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1571 | |
| 1572 | /* Set the state to READY by default */ |
| 1573 | phydev->state = PHY_READY; |
| 1574 | |
| 1575 | if (phydev->drv->probe) |
| 1576 | err = phydev->drv->probe(phydev); |
| 1577 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1578 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1579 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1580 | return err; |
| 1581 | } |
| 1582 | |
| 1583 | static int phy_remove(struct device *dev) |
| 1584 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 1585 | struct phy_device *phydev = to_phy_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1586 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1587 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1588 | phydev->state = PHY_DOWN; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 1589 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1590 | |
| 1591 | if (phydev->drv->remove) |
| 1592 | phydev->drv->remove(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1593 | phydev->drv = NULL; |
| 1594 | |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1598 | /** |
| 1599 | * phy_driver_register - register a phy_driver with the PHY layer |
| 1600 | * @new_driver: new phy_driver to register |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 1601 | * @owner: module owning this PHY |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1602 | */ |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 1603 | int phy_driver_register(struct phy_driver *new_driver, struct module *owner) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1604 | { |
| 1605 | int retval; |
| 1606 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 1607 | new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; |
| 1608 | new_driver->mdiodrv.driver.name = new_driver->name; |
| 1609 | new_driver->mdiodrv.driver.bus = &mdio_bus_type; |
| 1610 | new_driver->mdiodrv.driver.probe = phy_probe; |
| 1611 | new_driver->mdiodrv.driver.remove = phy_remove; |
| 1612 | new_driver->mdiodrv.driver.owner = owner; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1613 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 1614 | retval = driver_register(&new_driver->mdiodrv.driver); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1615 | if (retval) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 1616 | pr_err("%s: Error %d in registering driver\n", |
| 1617 | new_driver->name, retval); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1618 | |
| 1619 | return retval; |
| 1620 | } |
| 1621 | |
Olof Johansson | f2511f1 | 2007-11-04 16:09:23 -0600 | [diff] [blame] | 1622 | pr_debug("%s: Registered new driver\n", new_driver->name); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1623 | |
| 1624 | return 0; |
| 1625 | } |
| 1626 | EXPORT_SYMBOL(phy_driver_register); |
| 1627 | |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 1628 | int phy_drivers_register(struct phy_driver *new_driver, int n, |
| 1629 | struct module *owner) |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 1630 | { |
| 1631 | int i, ret = 0; |
| 1632 | |
| 1633 | for (i = 0; i < n; i++) { |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 1634 | ret = phy_driver_register(new_driver + i, owner); |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 1635 | if (ret) { |
| 1636 | while (i-- > 0) |
| 1637 | phy_driver_unregister(new_driver + i); |
| 1638 | break; |
| 1639 | } |
| 1640 | } |
| 1641 | return ret; |
| 1642 | } |
| 1643 | EXPORT_SYMBOL(phy_drivers_register); |
| 1644 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1645 | void phy_driver_unregister(struct phy_driver *drv) |
| 1646 | { |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 1647 | driver_unregister(&drv->mdiodrv.driver); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1648 | } |
| 1649 | EXPORT_SYMBOL(phy_driver_unregister); |
| 1650 | |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 1651 | void phy_drivers_unregister(struct phy_driver *drv, int n) |
| 1652 | { |
| 1653 | int i; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1654 | |
| 1655 | for (i = 0; i < n; i++) |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 1656 | phy_driver_unregister(drv + i); |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 1657 | } |
| 1658 | EXPORT_SYMBOL(phy_drivers_unregister); |
| 1659 | |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 1660 | static struct phy_driver genphy_driver[] = { |
| 1661 | { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1662 | .phy_id = 0xffffffff, |
| 1663 | .phy_id_mask = 0xffffffff, |
| 1664 | .name = "Generic PHY", |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1665 | .soft_reset = genphy_soft_reset, |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1666 | .config_init = genphy_config_init, |
Sascha Hauer | c242a47 | 2014-05-21 15:29:44 +0200 | [diff] [blame] | 1667 | .features = PHY_GBIT_FEATURES | SUPPORTED_MII | |
| 1668 | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 1669 | SUPPORTED_BNC, |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1670 | .config_aneg = genphy_config_aneg, |
Florian Fainelli | 76a423a | 2014-02-11 17:27:37 -0800 | [diff] [blame] | 1671 | .aneg_done = genphy_aneg_done, |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1672 | .read_status = genphy_read_status, |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1673 | .suspend = genphy_suspend, |
| 1674 | .resume = genphy_resume, |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1675 | }, { |
| 1676 | .phy_id = 0xffffffff, |
| 1677 | .phy_id_mask = 0xffffffff, |
| 1678 | .name = "Generic 10G PHY", |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1679 | .soft_reset = gen10g_soft_reset, |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1680 | .config_init = gen10g_config_init, |
| 1681 | .features = 0, |
| 1682 | .config_aneg = gen10g_config_aneg, |
| 1683 | .read_status = gen10g_read_status, |
| 1684 | .suspend = gen10g_suspend, |
| 1685 | .resume = gen10g_resume, |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 1686 | } }; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1687 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1688 | static int __init phy_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1689 | { |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1690 | int rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1691 | |
| 1692 | rc = mdio_bus_init(); |
| 1693 | if (rc) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1694 | return rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1695 | |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 1696 | rc = phy_drivers_register(genphy_driver, |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 1697 | ARRAY_SIZE(genphy_driver), THIS_MODULE); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1698 | if (rc) |
| 1699 | mdio_bus_exit(); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1700 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1701 | return rc; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1702 | } |
| 1703 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1704 | static void __exit phy_exit(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1705 | { |
Shaohui Xie | ab2145e | 2014-01-10 14:27:22 +0800 | [diff] [blame] | 1706 | phy_drivers_unregister(genphy_driver, |
| 1707 | ARRAY_SIZE(genphy_driver)); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1708 | mdio_bus_exit(); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1709 | } |
| 1710 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1711 | subsys_initcall(phy_init); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1712 | module_exit(phy_exit); |