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