Andrew Lunn | a2443fd | 2019-01-21 19:05:50 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 2 | /* Framework for finding and configuring PHYs. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 3 | * Also contains generic PHY driver |
| 4 | * |
| 5 | * Author: Andy Fleming |
| 6 | * |
| 7 | * Copyright (c) 2004 Freescale Semiconductor, Inc. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 8 | */ |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 13 | #include <linux/string.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/unistd.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/etherdevice.h> |
| 22 | #include <linux/skbuff.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 23 | #include <linux/mm.h> |
| 24 | #include <linux/module.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 25 | #include <linux/mii.h> |
| 26 | #include <linux/ethtool.h> |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 27 | #include <linux/bitmap.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 28 | #include <linux/phy.h> |
Zach Brown | 2e0bc45 | 2016-10-17 10:49:55 -0500 | [diff] [blame] | 29 | #include <linux/phy_led_triggers.h> |
Russell King | 298e54f | 2019-11-15 19:56:51 +0000 | [diff] [blame] | 30 | #include <linux/sfp.h> |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 31 | #include <linux/mdio.h> |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 32 | #include <linux/io.h> |
| 33 | #include <linux/uaccess.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 34 | |
Olaf Hering | afcceaa | 2005-12-14 00:33:49 +0100 | [diff] [blame] | 35 | MODULE_DESCRIPTION("PHY library"); |
| 36 | MODULE_AUTHOR("Andy Fleming"); |
| 37 | MODULE_LICENSE("GPL"); |
| 38 | |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 39 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init; |
| 40 | EXPORT_SYMBOL_GPL(phy_basic_features); |
| 41 | |
| 42 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init; |
| 43 | EXPORT_SYMBOL_GPL(phy_basic_t1_features); |
| 44 | |
| 45 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init; |
| 46 | EXPORT_SYMBOL_GPL(phy_gbit_features); |
| 47 | |
| 48 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init; |
| 49 | EXPORT_SYMBOL_GPL(phy_gbit_fibre_features); |
| 50 | |
| 51 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init; |
| 52 | EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features); |
| 53 | |
| 54 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init; |
| 55 | EXPORT_SYMBOL_GPL(phy_10gbit_features); |
| 56 | |
Andrew Lunn | 9e857a4 | 2019-01-15 16:55:30 +0100 | [diff] [blame] | 57 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init; |
| 58 | EXPORT_SYMBOL_GPL(phy_10gbit_fec_features); |
| 59 | |
Denis Efremov | 54638c6 | 2019-07-10 21:03:24 +0300 | [diff] [blame] | 60 | const int phy_basic_ports_array[3] = { |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 61 | ETHTOOL_LINK_MODE_Autoneg_BIT, |
| 62 | ETHTOOL_LINK_MODE_TP_BIT, |
| 63 | ETHTOOL_LINK_MODE_MII_BIT, |
| 64 | }; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 65 | EXPORT_SYMBOL_GPL(phy_basic_ports_array); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 66 | |
Denis Efremov | 54638c6 | 2019-07-10 21:03:24 +0300 | [diff] [blame] | 67 | const int phy_fibre_port_array[1] = { |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 68 | ETHTOOL_LINK_MODE_FIBRE_BIT, |
| 69 | }; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 70 | EXPORT_SYMBOL_GPL(phy_fibre_port_array); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 71 | |
Denis Efremov | 54638c6 | 2019-07-10 21:03:24 +0300 | [diff] [blame] | 72 | const int phy_all_ports_features_array[7] = { |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 73 | ETHTOOL_LINK_MODE_Autoneg_BIT, |
| 74 | ETHTOOL_LINK_MODE_TP_BIT, |
| 75 | ETHTOOL_LINK_MODE_MII_BIT, |
| 76 | ETHTOOL_LINK_MODE_FIBRE_BIT, |
| 77 | ETHTOOL_LINK_MODE_AUI_BIT, |
| 78 | ETHTOOL_LINK_MODE_BNC_BIT, |
| 79 | ETHTOOL_LINK_MODE_Backplane_BIT, |
| 80 | }; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 81 | EXPORT_SYMBOL_GPL(phy_all_ports_features_array); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 82 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 83 | const int phy_10_100_features_array[4] = { |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 84 | ETHTOOL_LINK_MODE_10baseT_Half_BIT, |
| 85 | ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 86 | ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 87 | ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 88 | }; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(phy_10_100_features_array); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 90 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 91 | const int phy_basic_t1_features_array[2] = { |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 92 | ETHTOOL_LINK_MODE_TP_BIT, |
Andrew Lunn | e5fb32c | 2019-05-22 20:47:04 +0200 | [diff] [blame] | 93 | ETHTOOL_LINK_MODE_100baseT1_Full_BIT, |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 94 | }; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 95 | EXPORT_SYMBOL_GPL(phy_basic_t1_features_array); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 96 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 97 | const int phy_gbit_features_array[2] = { |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 98 | ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 99 | ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 100 | }; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(phy_gbit_features_array); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 102 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 103 | const int phy_10gbit_features_array[1] = { |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 104 | ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
| 105 | }; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(phy_10gbit_features_array); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 107 | |
Andrew Lunn | 9e857a4 | 2019-01-15 16:55:30 +0100 | [diff] [blame] | 108 | const int phy_10gbit_fec_features_array[1] = { |
| 109 | ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, |
| 110 | }; |
| 111 | EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array); |
| 112 | |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 113 | __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init; |
| 114 | EXPORT_SYMBOL_GPL(phy_10gbit_full_features); |
| 115 | |
| 116 | static const int phy_10gbit_full_features_array[] = { |
| 117 | ETHTOOL_LINK_MODE_10baseT_Full_BIT, |
| 118 | ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 119 | ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 120 | ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
| 121 | }; |
| 122 | |
| 123 | static void features_init(void) |
| 124 | { |
| 125 | /* 10/100 half/full*/ |
| 126 | linkmode_set_bit_array(phy_basic_ports_array, |
| 127 | ARRAY_SIZE(phy_basic_ports_array), |
| 128 | phy_basic_features); |
| 129 | linkmode_set_bit_array(phy_10_100_features_array, |
| 130 | ARRAY_SIZE(phy_10_100_features_array), |
| 131 | phy_basic_features); |
| 132 | |
| 133 | /* 100 full, TP */ |
| 134 | linkmode_set_bit_array(phy_basic_t1_features_array, |
| 135 | ARRAY_SIZE(phy_basic_t1_features_array), |
| 136 | phy_basic_t1_features); |
| 137 | |
| 138 | /* 10/100 half/full + 1000 half/full */ |
| 139 | linkmode_set_bit_array(phy_basic_ports_array, |
| 140 | ARRAY_SIZE(phy_basic_ports_array), |
| 141 | phy_gbit_features); |
| 142 | linkmode_set_bit_array(phy_10_100_features_array, |
| 143 | ARRAY_SIZE(phy_10_100_features_array), |
| 144 | phy_gbit_features); |
| 145 | linkmode_set_bit_array(phy_gbit_features_array, |
| 146 | ARRAY_SIZE(phy_gbit_features_array), |
| 147 | phy_gbit_features); |
| 148 | |
| 149 | /* 10/100 half/full + 1000 half/full + fibre*/ |
| 150 | linkmode_set_bit_array(phy_basic_ports_array, |
| 151 | ARRAY_SIZE(phy_basic_ports_array), |
| 152 | phy_gbit_fibre_features); |
| 153 | linkmode_set_bit_array(phy_10_100_features_array, |
| 154 | ARRAY_SIZE(phy_10_100_features_array), |
| 155 | phy_gbit_fibre_features); |
| 156 | linkmode_set_bit_array(phy_gbit_features_array, |
| 157 | ARRAY_SIZE(phy_gbit_features_array), |
| 158 | phy_gbit_fibre_features); |
| 159 | linkmode_set_bit_array(phy_fibre_port_array, |
| 160 | ARRAY_SIZE(phy_fibre_port_array), |
| 161 | phy_gbit_fibre_features); |
| 162 | |
| 163 | /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/ |
| 164 | linkmode_set_bit_array(phy_all_ports_features_array, |
| 165 | ARRAY_SIZE(phy_all_ports_features_array), |
| 166 | phy_gbit_all_ports_features); |
| 167 | linkmode_set_bit_array(phy_10_100_features_array, |
| 168 | ARRAY_SIZE(phy_10_100_features_array), |
| 169 | phy_gbit_all_ports_features); |
| 170 | linkmode_set_bit_array(phy_gbit_features_array, |
| 171 | ARRAY_SIZE(phy_gbit_features_array), |
| 172 | phy_gbit_all_ports_features); |
| 173 | |
| 174 | /* 10/100 half/full + 1000 half/full + 10G full*/ |
| 175 | linkmode_set_bit_array(phy_all_ports_features_array, |
| 176 | ARRAY_SIZE(phy_all_ports_features_array), |
| 177 | phy_10gbit_features); |
| 178 | linkmode_set_bit_array(phy_10_100_features_array, |
| 179 | ARRAY_SIZE(phy_10_100_features_array), |
| 180 | phy_10gbit_features); |
| 181 | linkmode_set_bit_array(phy_gbit_features_array, |
| 182 | ARRAY_SIZE(phy_gbit_features_array), |
| 183 | phy_10gbit_features); |
| 184 | linkmode_set_bit_array(phy_10gbit_features_array, |
| 185 | ARRAY_SIZE(phy_10gbit_features_array), |
| 186 | phy_10gbit_features); |
| 187 | |
| 188 | /* 10/100/1000/10G full */ |
| 189 | linkmode_set_bit_array(phy_all_ports_features_array, |
| 190 | ARRAY_SIZE(phy_all_ports_features_array), |
| 191 | phy_10gbit_full_features); |
| 192 | linkmode_set_bit_array(phy_10gbit_full_features_array, |
| 193 | ARRAY_SIZE(phy_10gbit_full_features_array), |
| 194 | phy_10gbit_full_features); |
Andrew Lunn | 9e857a4 | 2019-01-15 16:55:30 +0100 | [diff] [blame] | 195 | /* 10G FEC only */ |
| 196 | linkmode_set_bit_array(phy_10gbit_fec_features_array, |
| 197 | ARRAY_SIZE(phy_10gbit_fec_features_array), |
| 198 | phy_10gbit_fec_features); |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 201 | void phy_device_free(struct phy_device *phydev) |
| 202 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 203 | put_device(&phydev->mdio.dev); |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 204 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 205 | EXPORT_SYMBOL(phy_device_free); |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 206 | |
Andrew Lunn | 711fdba | 2016-01-06 20:11:27 +0100 | [diff] [blame] | 207 | static void phy_mdio_device_free(struct mdio_device *mdiodev) |
| 208 | { |
| 209 | struct phy_device *phydev; |
| 210 | |
| 211 | phydev = container_of(mdiodev, struct phy_device, mdio); |
| 212 | phy_device_free(phydev); |
| 213 | } |
| 214 | |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 215 | static void phy_device_release(struct device *dev) |
| 216 | { |
Petr Malat | b2a4319 | 2013-02-28 01:01:52 +0000 | [diff] [blame] | 217 | kfree(to_phy_device(dev)); |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 218 | } |
| 219 | |
Andrew Lunn | 711fdba | 2016-01-06 20:11:27 +0100 | [diff] [blame] | 220 | static void phy_mdio_device_remove(struct mdio_device *mdiodev) |
| 221 | { |
| 222 | struct phy_device *phydev; |
| 223 | |
| 224 | phydev = container_of(mdiodev, struct phy_device, mdio); |
| 225 | phy_device_remove(phydev); |
| 226 | } |
| 227 | |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 228 | static struct phy_driver genphy_driver; |
Heiner Kallweit | 22b56e8 | 2019-04-07 13:55:01 +0200 | [diff] [blame] | 229 | extern struct phy_driver genphy_c45_driver; |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 230 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 231 | static LIST_HEAD(phy_fixup_list); |
| 232 | static DEFINE_MUTEX(phy_fixup_lock); |
| 233 | |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 234 | #ifdef CONFIG_PM |
| 235 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) |
| 236 | { |
| 237 | struct device_driver *drv = phydev->mdio.dev.driver; |
| 238 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 239 | struct net_device *netdev = phydev->attached_dev; |
| 240 | |
| 241 | if (!drv || !phydrv->suspend) |
| 242 | return false; |
| 243 | |
| 244 | /* PHY not attached? May suspend if the PHY has not already been |
| 245 | * suspended as part of a prior call to phy_disconnect() -> |
| 246 | * phy_detach() -> phy_suspend() because the parent netdev might be the |
| 247 | * MDIO bus driver and clock gated at this point. |
| 248 | */ |
| 249 | if (!netdev) |
| 250 | return !phydev->suspended; |
| 251 | |
Heiner Kallweit | 93f41e6 | 2018-09-24 22:01:32 +0200 | [diff] [blame] | 252 | if (netdev->wol_enabled) |
| 253 | return false; |
| 254 | |
| 255 | /* As long as not all affected network drivers support the |
| 256 | * wol_enabled flag, let's check for hints that WoL is enabled. |
| 257 | * Don't suspend PHY if the attached netdev parent may wake up. |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 258 | * The parent may point to a PCI device, as in tg3 driver. |
| 259 | */ |
| 260 | if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) |
| 261 | return false; |
| 262 | |
| 263 | /* Also don't suspend PHY if the netdev itself may wakeup. This |
| 264 | * is the case for devices w/o underlaying pwr. mgmt. aware bus, |
| 265 | * e.g. SoC devices. |
| 266 | */ |
| 267 | if (device_may_wakeup(&netdev->dev)) |
| 268 | return false; |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | static int mdio_bus_phy_suspend(struct device *dev) |
| 274 | { |
| 275 | struct phy_device *phydev = to_phy_device(dev); |
| 276 | |
| 277 | /* We must stop the state machine manually, otherwise it stops out of |
| 278 | * control, possibly with the phydev->lock held. Upon resume, netdev |
| 279 | * may call phy routines that try to grab the same lock, and that may |
| 280 | * lead to a deadlock. |
| 281 | */ |
| 282 | if (phydev->attached_dev && phydev->adjust_link) |
| 283 | phy_stop_machine(phydev); |
| 284 | |
| 285 | if (!mdio_bus_phy_may_suspend(phydev)) |
| 286 | return 0; |
| 287 | |
| 288 | return phy_suspend(phydev); |
| 289 | } |
| 290 | |
| 291 | static int mdio_bus_phy_resume(struct device *dev) |
| 292 | { |
| 293 | struct phy_device *phydev = to_phy_device(dev); |
| 294 | int ret; |
| 295 | |
| 296 | if (!mdio_bus_phy_may_suspend(phydev)) |
| 297 | goto no_resume; |
| 298 | |
| 299 | ret = phy_resume(phydev); |
| 300 | if (ret < 0) |
| 301 | return ret; |
| 302 | |
| 303 | no_resume: |
| 304 | if (phydev->attached_dev && phydev->adjust_link) |
| 305 | phy_start_machine(phydev); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int mdio_bus_phy_restore(struct device *dev) |
| 311 | { |
| 312 | struct phy_device *phydev = to_phy_device(dev); |
| 313 | struct net_device *netdev = phydev->attached_dev; |
| 314 | int ret; |
| 315 | |
| 316 | if (!netdev) |
| 317 | return 0; |
| 318 | |
| 319 | ret = phy_init_hw(phydev); |
| 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | |
Kunihiko Hayashi | 8742beb | 2018-12-18 16:57:04 +0900 | [diff] [blame] | 323 | if (phydev->attached_dev && phydev->adjust_link) |
| 324 | phy_start_machine(phydev); |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static const struct dev_pm_ops mdio_bus_phy_pm_ops = { |
| 330 | .suspend = mdio_bus_phy_suspend, |
| 331 | .resume = mdio_bus_phy_resume, |
| 332 | .freeze = mdio_bus_phy_suspend, |
| 333 | .thaw = mdio_bus_phy_resume, |
| 334 | .restore = mdio_bus_phy_restore, |
| 335 | }; |
| 336 | |
| 337 | #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops) |
| 338 | |
| 339 | #else |
| 340 | |
| 341 | #define MDIO_BUS_PHY_PM_OPS NULL |
| 342 | |
| 343 | #endif /* CONFIG_PM */ |
| 344 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 345 | /** |
| 346 | * 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] | 347 | * @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] | 348 | * @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] | 349 | * It can also be PHY_ANY_UID |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 350 | * @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] | 351 | * comparison |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 352 | * @run: The actual code to be run when a matching PHY is found |
| 353 | */ |
| 354 | 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] | 355 | int (*run)(struct phy_device *)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 356 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 357 | struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 358 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 359 | if (!fixup) |
| 360 | return -ENOMEM; |
| 361 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 362 | strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 363 | fixup->phy_uid = phy_uid; |
| 364 | fixup->phy_uid_mask = phy_uid_mask; |
| 365 | fixup->run = run; |
| 366 | |
| 367 | mutex_lock(&phy_fixup_lock); |
| 368 | list_add_tail(&fixup->list, &phy_fixup_list); |
| 369 | mutex_unlock(&phy_fixup_lock); |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | EXPORT_SYMBOL(phy_register_fixup); |
| 374 | |
| 375 | /* Registers a fixup to be run on any PHY with the UID in phy_uid */ |
| 376 | 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] | 377 | int (*run)(struct phy_device *)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 378 | { |
| 379 | return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); |
| 380 | } |
| 381 | EXPORT_SYMBOL(phy_register_fixup_for_uid); |
| 382 | |
| 383 | /* Registers a fixup to be run on the PHY with id string bus_id */ |
| 384 | int phy_register_fixup_for_id(const char *bus_id, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 385 | int (*run)(struct phy_device *)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 386 | { |
| 387 | return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); |
| 388 | } |
| 389 | EXPORT_SYMBOL(phy_register_fixup_for_id); |
| 390 | |
Woojung.Huh@microchip.com | f38e7a3 | 2016-12-07 20:26:07 +0000 | [diff] [blame] | 391 | /** |
| 392 | * phy_unregister_fixup - remove a phy_fixup from the list |
| 393 | * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list |
| 394 | * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list |
| 395 | * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison |
| 396 | */ |
| 397 | int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask) |
| 398 | { |
| 399 | struct list_head *pos, *n; |
| 400 | struct phy_fixup *fixup; |
| 401 | int ret; |
| 402 | |
| 403 | ret = -ENODEV; |
| 404 | |
| 405 | mutex_lock(&phy_fixup_lock); |
| 406 | list_for_each_safe(pos, n, &phy_fixup_list) { |
| 407 | fixup = list_entry(pos, struct phy_fixup, list); |
| 408 | |
| 409 | if ((!strcmp(fixup->bus_id, bus_id)) && |
| 410 | ((fixup->phy_uid & phy_uid_mask) == |
| 411 | (phy_uid & phy_uid_mask))) { |
| 412 | list_del(&fixup->list); |
| 413 | kfree(fixup); |
| 414 | ret = 0; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | mutex_unlock(&phy_fixup_lock); |
| 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | EXPORT_SYMBOL(phy_unregister_fixup); |
| 423 | |
| 424 | /* Unregisters a fixup of any PHY with the UID in phy_uid */ |
| 425 | int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask) |
| 426 | { |
| 427 | return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask); |
| 428 | } |
| 429 | EXPORT_SYMBOL(phy_unregister_fixup_for_uid); |
| 430 | |
| 431 | /* Unregisters a fixup of the PHY with id string bus_id */ |
| 432 | int phy_unregister_fixup_for_id(const char *bus_id) |
| 433 | { |
| 434 | return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff); |
| 435 | } |
| 436 | EXPORT_SYMBOL(phy_unregister_fixup_for_id); |
| 437 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 438 | /* Returns 1 if fixup matches phydev in bus_id and phy_uid. |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 439 | * Fixups can be set to match any in one or more fields. |
| 440 | */ |
| 441 | static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) |
| 442 | { |
Andrew Lunn | 84eff6d | 2016-01-06 20:11:10 +0100 | [diff] [blame] | 443 | if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 444 | if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) |
| 445 | return 0; |
| 446 | |
| 447 | if ((fixup->phy_uid & fixup->phy_uid_mask) != |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 448 | (phydev->phy_id & fixup->phy_uid_mask)) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 449 | if (fixup->phy_uid != PHY_ANY_UID) |
| 450 | return 0; |
| 451 | |
| 452 | return 1; |
| 453 | } |
| 454 | |
| 455 | /* Runs any matching fixups for this phydev */ |
Sergei Shtylyov | fbfcec6 | 2014-01-05 03:28:27 +0300 | [diff] [blame] | 456 | static int phy_scan_fixups(struct phy_device *phydev) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 457 | { |
| 458 | struct phy_fixup *fixup; |
| 459 | |
| 460 | mutex_lock(&phy_fixup_lock); |
| 461 | list_for_each_entry(fixup, &phy_fixup_list, list) { |
| 462 | if (phy_needs_fixup(phydev, fixup)) { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 463 | int err = fixup->run(phydev); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 464 | |
Jiri Slaby | bc23283c | 2009-07-13 11:23:39 +0000 | [diff] [blame] | 465 | if (err < 0) { |
| 466 | mutex_unlock(&phy_fixup_lock); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 467 | return err; |
Jiri Slaby | bc23283c | 2009-07-13 11:23:39 +0000 | [diff] [blame] | 468 | } |
Florian Fainelli | b0ae009 | 2014-02-11 17:27:41 -0800 | [diff] [blame] | 469 | phydev->has_fixups = true; |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | mutex_unlock(&phy_fixup_lock); |
| 473 | |
| 474 | return 0; |
| 475 | } |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 476 | |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 477 | static int phy_bus_match(struct device *dev, struct device_driver *drv) |
| 478 | { |
| 479 | struct phy_device *phydev = to_phy_device(dev); |
| 480 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 481 | const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); |
| 482 | int i; |
| 483 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 484 | if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)) |
| 485 | return 0; |
| 486 | |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 487 | if (phydrv->match_phy_device) |
| 488 | return phydrv->match_phy_device(phydev); |
| 489 | |
| 490 | if (phydev->is_c45) { |
| 491 | for (i = 1; i < num_ids; i++) { |
Russell King | b95e86d | 2019-11-15 20:08:37 +0000 | [diff] [blame] | 492 | if (phydev->c45_ids.device_ids[i] == 0xffffffff) |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 493 | continue; |
| 494 | |
| 495 | if ((phydrv->phy_id & phydrv->phy_id_mask) == |
| 496 | (phydev->c45_ids.device_ids[i] & |
| 497 | phydrv->phy_id_mask)) |
| 498 | return 1; |
| 499 | } |
| 500 | return 0; |
| 501 | } else { |
| 502 | return (phydrv->phy_id & phydrv->phy_id_mask) == |
| 503 | (phydev->phy_id & phydrv->phy_id_mask); |
| 504 | } |
| 505 | } |
| 506 | |
Heiner Kallweit | 7f4828f | 2018-06-02 22:36:06 +0200 | [diff] [blame] | 507 | static ssize_t |
| 508 | phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 509 | { |
| 510 | struct phy_device *phydev = to_phy_device(dev); |
| 511 | |
| 512 | return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); |
| 513 | } |
| 514 | static DEVICE_ATTR_RO(phy_id); |
| 515 | |
| 516 | static ssize_t |
| 517 | phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 518 | { |
| 519 | struct phy_device *phydev = to_phy_device(dev); |
| 520 | const char *mode = NULL; |
| 521 | |
| 522 | if (phy_is_internal(phydev)) |
| 523 | mode = "internal"; |
| 524 | else |
| 525 | mode = phy_modes(phydev->interface); |
| 526 | |
| 527 | return sprintf(buf, "%s\n", mode); |
| 528 | } |
| 529 | static DEVICE_ATTR_RO(phy_interface); |
| 530 | |
| 531 | static ssize_t |
| 532 | phy_has_fixups_show(struct device *dev, struct device_attribute *attr, |
| 533 | char *buf) |
| 534 | { |
| 535 | struct phy_device *phydev = to_phy_device(dev); |
| 536 | |
| 537 | return sprintf(buf, "%d\n", phydev->has_fixups); |
| 538 | } |
| 539 | static DEVICE_ATTR_RO(phy_has_fixups); |
| 540 | |
| 541 | static struct attribute *phy_dev_attrs[] = { |
| 542 | &dev_attr_phy_id.attr, |
| 543 | &dev_attr_phy_interface.attr, |
| 544 | &dev_attr_phy_has_fixups.attr, |
| 545 | NULL, |
| 546 | }; |
| 547 | ATTRIBUTE_GROUPS(phy_dev); |
| 548 | |
| 549 | static const struct device_type mdio_bus_phy_type = { |
| 550 | .name = "PHY", |
| 551 | .groups = phy_dev_groups, |
| 552 | .release = phy_device_release, |
| 553 | .pm = MDIO_BUS_PHY_PM_OPS, |
| 554 | }; |
| 555 | |
Russell King | 7d49a32 | 2019-12-19 23:24:52 +0000 | [diff] [blame] | 556 | static int phy_request_driver_module(struct phy_device *dev, u32 phy_id) |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 557 | { |
| 558 | int ret; |
| 559 | |
| 560 | ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, |
| 561 | MDIO_ID_ARGS(phy_id)); |
Heiner Kallweit | 21e1944 | 2019-01-19 10:30:21 +0100 | [diff] [blame] | 562 | /* We only check for failures in executing the usermode binary, |
| 563 | * not whether a PHY driver module exists for the PHY ID. |
| 564 | * Accept -ENOENT because this may occur in case no initramfs exists, |
| 565 | * then modprobe isn't available. |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 566 | */ |
Heiner Kallweit | 21e1944 | 2019-01-19 10:30:21 +0100 | [diff] [blame] | 567 | if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) { |
Russell King | 7d49a32 | 2019-12-19 23:24:52 +0000 | [diff] [blame] | 568 | phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n", |
| 569 | ret, (unsigned long)phy_id); |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
Russell King | 7d49a32 | 2019-12-19 23:24:52 +0000 | [diff] [blame] | 576 | struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 577 | bool is_c45, |
| 578 | struct phy_c45_device_ids *c45_ids) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 579 | { |
| 580 | struct phy_device *dev; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 581 | struct mdio_device *mdiodev; |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 582 | int ret = 0; |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 583 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 584 | /* We allocate the device, and initialize the default values */ |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 585 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Sergei Shtylyov | ef899c0 | 2015-08-28 21:35:14 +0300 | [diff] [blame] | 586 | if (!dev) |
Sergei Shtylyov | d3765f0 | 2015-08-28 21:34:34 +0300 | [diff] [blame] | 587 | return ERR_PTR(-ENOMEM); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 588 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 589 | mdiodev = &dev->mdio; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 590 | mdiodev->dev.parent = &bus->dev; |
| 591 | mdiodev->dev.bus = &mdio_bus_type; |
Heiner Kallweit | 7f4828f | 2018-06-02 22:36:06 +0200 | [diff] [blame] | 592 | mdiodev->dev.type = &mdio_bus_phy_type; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 593 | mdiodev->bus = bus; |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 594 | mdiodev->bus_match = phy_bus_match; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 595 | mdiodev->addr = addr; |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 596 | mdiodev->flags = MDIO_DEVICE_FLAG_PHY; |
Andrew Lunn | 711fdba | 2016-01-06 20:11:27 +0100 | [diff] [blame] | 597 | mdiodev->device_free = phy_mdio_device_free; |
| 598 | mdiodev->device_remove = phy_mdio_device_remove; |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 599 | |
Russell King | a5d66f8 | 2019-11-22 15:23:23 +0000 | [diff] [blame] | 600 | dev->speed = SPEED_UNKNOWN; |
| 601 | dev->duplex = DUPLEX_UNKNOWN; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 602 | dev->pause = 0; |
| 603 | dev->asym_pause = 0; |
Heiner Kallweit | c69851e | 2018-03-11 15:00:37 +0100 | [diff] [blame] | 604 | dev->link = 0; |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 605 | dev->interface = PHY_INTERFACE_MODE_GMII; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 606 | |
| 607 | dev->autoneg = AUTONEG_ENABLE; |
| 608 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 609 | dev->is_c45 = is_c45; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 610 | dev->phy_id = phy_id; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 611 | if (c45_ids) |
| 612 | dev->c45_ids = *c45_ids; |
Dan Carpenter | 47b356e | 2016-01-12 12:36:21 +0300 | [diff] [blame] | 613 | dev->irq = bus->irq[addr]; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 614 | dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 615 | |
| 616 | dev->state = PHY_DOWN; |
| 617 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 618 | mutex_init(&dev->lock); |
Anton Vorontsov | 4f9c85a | 2010-01-18 05:37:16 +0000 | [diff] [blame] | 619 | INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 620 | |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 621 | /* Request the appropriate module unconditionally; don't |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 622 | * bother trying to do so only if it isn't already loaded, |
| 623 | * because that gets complicated. A hotplug event would have |
| 624 | * done an unconditional modprobe anyway. |
| 625 | * We don't do normal hotplug because it won't work for MDIO |
| 626 | * -- because it relies on the device staying around for long |
| 627 | * enough for the driver to get loaded. With MDIO, the NIC |
| 628 | * driver will get bored and give up as soon as it finds that |
| 629 | * there's no driver _already_ loaded. |
| 630 | */ |
Jose Abreu | 30fcd6a | 2018-12-02 16:33:14 +0100 | [diff] [blame] | 631 | if (is_c45 && c45_ids) { |
| 632 | const int num_ids = ARRAY_SIZE(c45_ids->device_ids); |
| 633 | int i; |
| 634 | |
| 635 | for (i = 1; i < num_ids; i++) { |
Russell King | b95e86d | 2019-11-15 20:08:37 +0000 | [diff] [blame] | 636 | if (c45_ids->device_ids[i] == 0xffffffff) |
Jose Abreu | 30fcd6a | 2018-12-02 16:33:14 +0100 | [diff] [blame] | 637 | continue; |
| 638 | |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 639 | ret = phy_request_driver_module(dev, |
| 640 | c45_ids->device_ids[i]); |
| 641 | if (ret) |
| 642 | break; |
Jose Abreu | 30fcd6a | 2018-12-02 16:33:14 +0100 | [diff] [blame] | 643 | } |
| 644 | } else { |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 645 | ret = phy_request_driver_module(dev, phy_id); |
Jose Abreu | 30fcd6a | 2018-12-02 16:33:14 +0100 | [diff] [blame] | 646 | } |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 647 | |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 648 | if (!ret) { |
| 649 | device_initialize(&mdiodev->dev); |
| 650 | } else { |
| 651 | kfree(dev); |
| 652 | dev = ERR_PTR(ret); |
| 653 | } |
Petr Malat | b2a4319 | 2013-02-28 01:01:52 +0000 | [diff] [blame] | 654 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 655 | return dev; |
| 656 | } |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 657 | EXPORT_SYMBOL(phy_device_create); |
| 658 | |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 659 | /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. |
| 660 | * @bus: the target MII bus |
| 661 | * @addr: PHY address on the MII bus |
| 662 | * @dev_addr: MMD address in the PHY. |
| 663 | * @devices_in_package: where to store the devices in package information. |
| 664 | * |
| 665 | * Description: reads devices in package registers of a MMD at @dev_addr |
| 666 | * from PHY at @addr on @bus. |
| 667 | * |
| 668 | * Returns: 0 on success, -EIO on failure. |
| 669 | */ |
| 670 | static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr, |
| 671 | u32 *devices_in_package) |
| 672 | { |
| 673 | int phy_reg, reg_addr; |
| 674 | |
| 675 | reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2; |
| 676 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 677 | if (phy_reg < 0) |
| 678 | return -EIO; |
Heiner Kallweit | 50684da | 2019-02-09 09:46:53 +0100 | [diff] [blame] | 679 | *devices_in_package = phy_reg << 16; |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 680 | |
| 681 | reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1; |
| 682 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 683 | if (phy_reg < 0) |
| 684 | return -EIO; |
Heiner Kallweit | 50684da | 2019-02-09 09:46:53 +0100 | [diff] [blame] | 685 | *devices_in_package |= phy_reg; |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 686 | |
Heiner Kallweit | 3b5e74e | 2019-02-08 19:25:22 +0100 | [diff] [blame] | 687 | /* Bit 0 doesn't represent a device, it indicates c22 regs presence */ |
| 688 | *devices_in_package &= ~BIT(0); |
| 689 | |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 690 | return 0; |
| 691 | } |
| 692 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 693 | /** |
| 694 | * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. |
| 695 | * @bus: the target MII bus |
| 696 | * @addr: PHY address on the MII bus |
| 697 | * @phy_id: where to store the ID retrieved. |
| 698 | * @c45_ids: where to store the c45 ID information. |
| 699 | * |
| 700 | * If the PHY devices-in-package appears to be valid, it and the |
| 701 | * corresponding identifiers are stored in @c45_ids, zero is stored |
| 702 | * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns |
| 703 | * zero on success. |
| 704 | * |
| 705 | */ |
| 706 | static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id, |
| 707 | struct phy_c45_device_ids *c45_ids) { |
| 708 | int phy_reg; |
| 709 | int i, reg_addr; |
| 710 | const int num_ids = ARRAY_SIZE(c45_ids->device_ids); |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 711 | u32 *devs = &c45_ids->devices_in_package; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 712 | |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 713 | /* Find first non-zero Devices In package. Device zero is reserved |
| 714 | * 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] | 715 | */ |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 716 | for (i = 1; i < num_ids && *devs == 0; i++) { |
| 717 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs); |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 718 | if (phy_reg < 0) |
| 719 | return -EIO; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 720 | |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 721 | if ((*devs & 0x1fffffff) == 0x1fffffff) { |
| 722 | /* If mostly Fs, there is no device there, |
| 723 | * then let's continue to probe more, as some |
| 724 | * 10G PHYs have zero Devices In package, |
| 725 | * e.g. Cortina CS4315/CS4340 PHY. |
| 726 | */ |
| 727 | phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs); |
| 728 | if (phy_reg < 0) |
| 729 | return -EIO; |
| 730 | /* no device there, let's get out of here */ |
| 731 | if ((*devs & 0x1fffffff) == 0x1fffffff) { |
Shengzhou Liu | da1da28 | 2015-06-26 17:58:52 +0800 | [diff] [blame] | 732 | *phy_id = 0xffffffff; |
| 733 | return 0; |
Shaohui Xie | 5f6c99e | 2015-11-03 12:27:33 +0800 | [diff] [blame] | 734 | } else { |
| 735 | break; |
Shengzhou Liu | da1da28 | 2015-06-26 17:58:52 +0800 | [diff] [blame] | 736 | } |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
| 740 | /* Now probe Device Identifiers for each device present. */ |
| 741 | for (i = 1; i < num_ids; i++) { |
| 742 | if (!(c45_ids->devices_in_package & (1 << i))) |
| 743 | continue; |
| 744 | |
| 745 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1; |
| 746 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 747 | if (phy_reg < 0) |
| 748 | return -EIO; |
Heiner Kallweit | 50684da | 2019-02-09 09:46:53 +0100 | [diff] [blame] | 749 | c45_ids->device_ids[i] = phy_reg << 16; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 750 | |
| 751 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2; |
| 752 | phy_reg = mdiobus_read(bus, addr, reg_addr); |
| 753 | if (phy_reg < 0) |
| 754 | return -EIO; |
Heiner Kallweit | 50684da | 2019-02-09 09:46:53 +0100 | [diff] [blame] | 755 | c45_ids->device_ids[i] |= phy_reg; |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 756 | } |
| 757 | *phy_id = 0; |
| 758 | return 0; |
| 759 | } |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 760 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 761 | /** |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 762 | * get_phy_id - reads the specified addr for its ID. |
| 763 | * @bus: the target MII bus |
| 764 | * @addr: PHY address on the MII bus |
| 765 | * @phy_id: where to store the ID retrieved. |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 766 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
| 767 | * @c45_ids: where to store the c45 ID information. |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 768 | * |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 769 | * Description: In the case of a 802.3-c22 PHY, reads the ID registers |
| 770 | * of the PHY at @addr on the @bus, stores it in @phy_id and returns |
| 771 | * zero on success. |
| 772 | * |
| 773 | * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and |
| 774 | * its return value is in turn returned. |
| 775 | * |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 776 | */ |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 777 | static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id, |
| 778 | bool is_c45, struct phy_c45_device_ids *c45_ids) |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 779 | { |
| 780 | int phy_reg; |
| 781 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 782 | if (is_c45) |
| 783 | return get_phy_c45_ids(bus, addr, phy_id, c45_ids); |
| 784 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 785 | /* Grab the bits from PHYIR1, and put them in the upper half */ |
David Daney | 6fe3264 | 2011-09-30 11:51:22 +0000 | [diff] [blame] | 786 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); |
Alexandre Belloni | 02a6efc | 2018-04-24 18:09:04 +0200 | [diff] [blame] | 787 | if (phy_reg < 0) { |
Heiner Kallweit | d8cce3a | 2019-01-16 19:52:51 +0100 | [diff] [blame] | 788 | /* returning -ENODEV doesn't stop bus scanning */ |
| 789 | return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO; |
Alexandre Belloni | 02a6efc | 2018-04-24 18:09:04 +0200 | [diff] [blame] | 790 | } |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 791 | |
Heiner Kallweit | 50684da | 2019-02-09 09:46:53 +0100 | [diff] [blame] | 792 | *phy_id = phy_reg << 16; |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 793 | |
| 794 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
David Daney | 6fe3264 | 2011-09-30 11:51:22 +0000 | [diff] [blame] | 795 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 796 | if (phy_reg < 0) |
| 797 | return -EIO; |
| 798 | |
Heiner Kallweit | 50684da | 2019-02-09 09:46:53 +0100 | [diff] [blame] | 799 | *phy_id |= phy_reg; |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /** |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 805 | * get_phy_device - reads the specified PHY device and returns its @phy_device |
| 806 | * struct |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 807 | * @bus: the target MII bus |
| 808 | * @addr: PHY address on the MII bus |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 809 | * @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] | 810 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 811 | * Description: Reads the ID registers of the PHY at @addr on the |
| 812 | * @bus, then allocates and returns the phy_device to represent it. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 813 | */ |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 814 | 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] | 815 | { |
Russell King | b95e86d | 2019-11-15 20:08:37 +0000 | [diff] [blame] | 816 | struct phy_c45_device_ids c45_ids; |
David S. Miller | 160c85f | 2012-06-27 21:28:14 -0700 | [diff] [blame] | 817 | u32 phy_id = 0; |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 818 | int r; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 819 | |
Russell King | b95e86d | 2019-11-15 20:08:37 +0000 | [diff] [blame] | 820 | c45_ids.devices_in_package = 0; |
| 821 | memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids)); |
| 822 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 823 | r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 824 | if (r) |
| 825 | return ERR_PTR(r); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 826 | |
Giuseppe Cavallaro | 6436cbc | 2008-11-20 20:43:18 -0800 | [diff] [blame] | 827 | /* If the phy_id is mostly Fs, there is no device there */ |
| 828 | if ((phy_id & 0x1fffffff) == 0x1fffffff) |
Sergei Shtylyov | b74766a | 2016-04-24 20:25:23 +0300 | [diff] [blame] | 829 | return ERR_PTR(-ENODEV); |
Giuseppe Cavallaro | 6436cbc | 2008-11-20 20:43:18 -0800 | [diff] [blame] | 830 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 831 | return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 832 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 833 | EXPORT_SYMBOL(get_phy_device); |
| 834 | |
| 835 | /** |
| 836 | * phy_device_register - Register the phy device on the MDIO bus |
Randy Dunlap | 1d4ac5d | 2009-06-16 16:56:33 +0000 | [diff] [blame] | 837 | * @phydev: phy_device structure to be added to the MDIO bus |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 838 | */ |
| 839 | int phy_device_register(struct phy_device *phydev) |
| 840 | { |
| 841 | int err; |
| 842 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 843 | err = mdiobus_register_device(&phydev->mdio); |
| 844 | if (err) |
| 845 | return err; |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 846 | |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 847 | /* Deassert the reset signal */ |
| 848 | phy_device_reset(phydev, 0); |
| 849 | |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 850 | /* Run all of the fixups for this PHY */ |
Florian Fainelli | d92f5de | 2014-07-28 16:28:07 -0700 | [diff] [blame] | 851 | err = phy_scan_fixups(phydev); |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 852 | if (err) { |
Heiner Kallweit | c3a6a17 | 2019-01-15 21:50:25 +0100 | [diff] [blame] | 853 | phydev_err(phydev, "failed to initialize\n"); |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 854 | goto out; |
| 855 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 856 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 857 | err = device_add(&phydev->mdio.dev); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 858 | if (err) { |
Heiner Kallweit | c3a6a17 | 2019-01-15 21:50:25 +0100 | [diff] [blame] | 859 | phydev_err(phydev, "failed to add\n"); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 860 | goto out; |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | |
| 865 | out: |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 866 | /* Assert the reset signal */ |
| 867 | phy_device_reset(phydev, 1); |
| 868 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 869 | mdiobus_unregister_device(&phydev->mdio); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 870 | return err; |
| 871 | } |
| 872 | EXPORT_SYMBOL(phy_device_register); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 873 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 874 | /** |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 875 | * phy_device_remove - Remove a previously registered phy device from the MDIO bus |
| 876 | * @phydev: phy_device structure to remove |
| 877 | * |
| 878 | * This doesn't free the phy_device itself, it merely reverses the effects |
| 879 | * of phy_device_register(). Use phy_device_free() to free the device |
| 880 | * after calling this function. |
| 881 | */ |
| 882 | void phy_device_remove(struct phy_device *phydev) |
| 883 | { |
Richard Cochran | 1dca22b | 2019-12-25 18:16:18 -0800 | [diff] [blame^] | 884 | if (phydev->mii_ts) |
| 885 | unregister_mii_timestamper(phydev->mii_ts); |
| 886 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 887 | device_del(&phydev->mdio.dev); |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 888 | |
| 889 | /* Assert the reset signal */ |
| 890 | phy_device_reset(phydev, 1); |
| 891 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 892 | mdiobus_unregister_device(&phydev->mdio); |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 893 | } |
| 894 | EXPORT_SYMBOL(phy_device_remove); |
| 895 | |
| 896 | /** |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 897 | * phy_find_first - finds the first PHY device on the bus |
| 898 | * @bus: the target MII bus |
| 899 | */ |
| 900 | struct phy_device *phy_find_first(struct mii_bus *bus) |
| 901 | { |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 902 | struct phy_device *phydev; |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 903 | int addr; |
| 904 | |
| 905 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 906 | phydev = mdiobus_get_phy(bus, addr); |
| 907 | if (phydev) |
| 908 | return phydev; |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 909 | } |
| 910 | return NULL; |
| 911 | } |
| 912 | EXPORT_SYMBOL(phy_find_first); |
| 913 | |
Russell King | a81497b | 2017-07-25 15:02:58 +0100 | [diff] [blame] | 914 | static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier) |
| 915 | { |
| 916 | struct net_device *netdev = phydev->attached_dev; |
| 917 | |
| 918 | if (do_carrier) { |
| 919 | if (up) |
| 920 | netif_carrier_on(netdev); |
| 921 | else |
| 922 | netif_carrier_off(netdev); |
| 923 | } |
| 924 | phydev->adjust_link(netdev); |
Richard Cochran | 4715f65 | 2019-12-25 18:16:15 -0800 | [diff] [blame] | 925 | if (phydev->mii_ts && phydev->mii_ts->link_state) |
| 926 | phydev->mii_ts->link_state(phydev->mii_ts, phydev); |
Russell King | a81497b | 2017-07-25 15:02:58 +0100 | [diff] [blame] | 927 | } |
| 928 | |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 929 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 930 | * phy_prepare_link - prepares the PHY layer to monitor link status |
| 931 | * @phydev: target phy_device struct |
| 932 | * @handler: callback function for link status change notifications |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 933 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 934 | * Description: Tells the PHY infrastructure to handle the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 935 | * gory details on monitoring link status (whether through |
| 936 | * polling or an interrupt), and to call back to the |
| 937 | * connected device driver when the link status changes. |
| 938 | * If you want to monitor your own link state, don't call |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 939 | * this function. |
| 940 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 941 | static void phy_prepare_link(struct phy_device *phydev, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 942 | void (*handler)(struct net_device *)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 943 | { |
| 944 | phydev->adjust_link = handler; |
| 945 | } |
| 946 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 947 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 948 | * phy_connect_direct - connect an ethernet device to a specific phy_device |
| 949 | * @dev: the network device to connect |
| 950 | * @phydev: the pointer to the phy device |
| 951 | * @handler: callback function for state change notifications |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 952 | * @interface: PHY device's interface |
| 953 | */ |
| 954 | int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 955 | void (*handler)(struct net_device *), |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 956 | phy_interface_t interface) |
| 957 | { |
| 958 | int rc; |
| 959 | |
Ioana Ciornei | 82c76ac | 2019-05-28 20:38:09 +0300 | [diff] [blame] | 960 | if (!dev) |
| 961 | return -EINVAL; |
| 962 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 963 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 964 | if (rc) |
| 965 | return rc; |
| 966 | |
| 967 | phy_prepare_link(phydev, handler); |
Heiner Kallweit | 434a431 | 2019-01-23 07:31:58 +0100 | [diff] [blame] | 968 | if (phy_interrupt_is_valid(phydev)) |
| 969 | phy_request_interrupt(phydev); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | EXPORT_SYMBOL(phy_connect_direct); |
| 974 | |
| 975 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 976 | * phy_connect - connect an ethernet device to a PHY device |
| 977 | * @dev: the network device to connect |
Randy Dunlap | 5d12b13 | 2008-04-28 10:58:22 -0700 | [diff] [blame] | 978 | * @bus_id: the id string of the PHY device to connect |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 979 | * @handler: callback function for state change notifications |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 980 | * @interface: PHY device's interface |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 981 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 982 | * Description: Convenience function for connecting ethernet |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 983 | * devices to PHY devices. The default behavior is for |
| 984 | * the PHY infrastructure to handle everything, and only notify |
| 985 | * the connected driver when the link status changes. If you |
| 986 | * don't want, or can't use the provided functionality, you may |
| 987 | * choose to call only the subset of functions which provide |
| 988 | * the desired functionality. |
| 989 | */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 990 | 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] | 991 | void (*handler)(struct net_device *), |
| 992 | phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 993 | { |
| 994 | struct phy_device *phydev; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 995 | struct device *d; |
| 996 | int rc; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 997 | |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 998 | /* Search the list of PHY devices on the mdio bus for the |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 999 | * PHY with the requested name |
| 1000 | */ |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1001 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
| 1002 | if (!d) { |
| 1003 | pr_err("PHY %s not found\n", bus_id); |
| 1004 | return ERR_PTR(-ENODEV); |
| 1005 | } |
| 1006 | phydev = to_phy_device(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1007 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 1008 | rc = phy_connect_direct(dev, phydev, handler, interface); |
Johan Hovold | 17ae1c6 | 2016-11-03 18:40:19 +0100 | [diff] [blame] | 1009 | put_device(d); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1010 | if (rc) |
| 1011 | return ERR_PTR(rc); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1012 | |
| 1013 | return phydev; |
| 1014 | } |
| 1015 | EXPORT_SYMBOL(phy_connect); |
| 1016 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1017 | /** |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1018 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY |
| 1019 | * device |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1020 | * @phydev: target phy_device struct |
| 1021 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1022 | void phy_disconnect(struct phy_device *phydev) |
| 1023 | { |
Heiner Kallweit | 472115d | 2019-01-17 20:07:54 +0100 | [diff] [blame] | 1024 | if (phy_is_started(phydev)) |
| 1025 | phy_stop(phydev); |
| 1026 | |
Heiner Kallweit | bb658ab | 2019-01-17 20:09:21 +0100 | [diff] [blame] | 1027 | if (phy_interrupt_is_valid(phydev)) |
Heiner Kallweit | 07b0928 | 2019-05-30 15:09:15 +0200 | [diff] [blame] | 1028 | phy_free_interrupt(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1029 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1030 | phydev->adjust_link = NULL; |
| 1031 | |
| 1032 | phy_detach(phydev); |
| 1033 | } |
| 1034 | EXPORT_SYMBOL(phy_disconnect); |
| 1035 | |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1036 | /** |
| 1037 | * phy_poll_reset - Safely wait until a PHY reset has properly completed |
| 1038 | * @phydev: The PHY device to poll |
| 1039 | * |
| 1040 | * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as |
| 1041 | * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR |
| 1042 | * register must be polled until the BMCR_RESET bit clears. |
| 1043 | * |
| 1044 | * Furthermore, any attempts to write to PHY registers may have no effect |
| 1045 | * or even generate MDIO bus errors until this is complete. |
| 1046 | * |
| 1047 | * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the |
| 1048 | * standard and do not fully reset after the BMCR_RESET bit is set, and may |
| 1049 | * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an |
| 1050 | * effort to support such broken PHYs, this function is separate from the |
| 1051 | * standard phy_init_hw() which will zero all the other bits in the BMCR |
| 1052 | * and reapply all driver-specific and board-specific fixups. |
| 1053 | */ |
| 1054 | static int phy_poll_reset(struct phy_device *phydev) |
| 1055 | { |
| 1056 | /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ |
| 1057 | unsigned int retries = 12; |
| 1058 | int ret; |
| 1059 | |
| 1060 | do { |
| 1061 | msleep(50); |
| 1062 | ret = phy_read(phydev, MII_BMCR); |
| 1063 | if (ret < 0) |
| 1064 | return ret; |
| 1065 | } while (ret & BMCR_RESET && --retries); |
| 1066 | if (ret & BMCR_RESET) |
| 1067 | return -ETIMEDOUT; |
| 1068 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1069 | /* Some chips (smsc911x) may still need up to another 1ms after the |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1070 | * BMCR_RESET bit is cleared before they are usable. |
| 1071 | */ |
| 1072 | msleep(1); |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1076 | int phy_init_hw(struct phy_device *phydev) |
| 1077 | { |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1078 | int ret = 0; |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1079 | |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 1080 | /* Deassert the reset signal */ |
| 1081 | phy_device_reset(phydev, 0); |
| 1082 | |
Heiner Kallweit | a599698 | 2019-01-19 10:43:07 +0100 | [diff] [blame] | 1083 | if (!phydev->drv) |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1084 | return 0; |
| 1085 | |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1086 | if (phydev->drv->soft_reset) |
| 1087 | ret = phydev->drv->soft_reset(phydev); |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1088 | |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1089 | if (ret < 0) |
| 1090 | return ret; |
| 1091 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1092 | ret = phy_scan_fixups(phydev); |
| 1093 | if (ret < 0) |
| 1094 | return ret; |
| 1095 | |
Heiner Kallweit | a599698 | 2019-01-19 10:43:07 +0100 | [diff] [blame] | 1096 | if (phydev->drv->config_init) |
| 1097 | ret = phydev->drv->config_init(phydev); |
| 1098 | |
| 1099 | return ret; |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1100 | } |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1101 | EXPORT_SYMBOL(phy_init_hw); |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1102 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1103 | void phy_attached_info(struct phy_device *phydev) |
| 1104 | { |
| 1105 | phy_attached_print(phydev, NULL); |
| 1106 | } |
| 1107 | EXPORT_SYMBOL(phy_attached_info); |
| 1108 | |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1109 | #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)" |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1110 | void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) |
| 1111 | { |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1112 | const char *drv_name = phydev->drv ? phydev->drv->name : "unbound"; |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1113 | char *irq_str; |
Geert Uytterhoeven | 059fbe8 | 2017-09-21 13:27:02 +0200 | [diff] [blame] | 1114 | char irq_num[8]; |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1115 | |
| 1116 | switch(phydev->irq) { |
| 1117 | case PHY_POLL: |
| 1118 | irq_str = "POLL"; |
| 1119 | break; |
| 1120 | case PHY_IGNORE_INTERRUPT: |
| 1121 | irq_str = "IGNORE"; |
| 1122 | break; |
| 1123 | default: |
| 1124 | snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq); |
| 1125 | irq_str = irq_num; |
| 1126 | break; |
| 1127 | } |
| 1128 | |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1129 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1130 | if (!fmt) { |
Andrew Lunn | c4fabb8 | 2018-09-29 23:04:11 +0200 | [diff] [blame] | 1131 | phydev_info(phydev, ATTACHED_FMT "\n", |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1132 | drv_name, phydev_name(phydev), |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1133 | irq_str); |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1134 | } else { |
| 1135 | va_list ap; |
| 1136 | |
Andrew Lunn | c4fabb8 | 2018-09-29 23:04:11 +0200 | [diff] [blame] | 1137 | phydev_info(phydev, ATTACHED_FMT, |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1138 | drv_name, phydev_name(phydev), |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1139 | irq_str); |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1140 | |
| 1141 | va_start(ap, fmt); |
| 1142 | vprintk(fmt, ap); |
| 1143 | va_end(ap); |
| 1144 | } |
| 1145 | } |
| 1146 | EXPORT_SYMBOL(phy_attached_print); |
| 1147 | |
Vladimir Oltean | 53cfca2 | 2019-05-28 20:38:07 +0300 | [diff] [blame] | 1148 | static void phy_sysfs_create_links(struct phy_device *phydev) |
| 1149 | { |
| 1150 | struct net_device *dev = phydev->attached_dev; |
| 1151 | int err; |
| 1152 | |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1153 | if (!dev) |
| 1154 | return; |
| 1155 | |
Vladimir Oltean | 53cfca2 | 2019-05-28 20:38:07 +0300 | [diff] [blame] | 1156 | err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, |
| 1157 | "attached_dev"); |
| 1158 | if (err) |
| 1159 | return; |
| 1160 | |
| 1161 | err = sysfs_create_link_nowarn(&dev->dev.kobj, |
| 1162 | &phydev->mdio.dev.kobj, |
| 1163 | "phydev"); |
| 1164 | if (err) { |
| 1165 | dev_err(&dev->dev, "could not add device link to %s err %d\n", |
| 1166 | kobject_name(&phydev->mdio.dev.kobj), |
| 1167 | err); |
| 1168 | /* non-fatal - some net drivers can use one netdevice |
| 1169 | * with more then one phy |
| 1170 | */ |
| 1171 | } |
| 1172 | |
| 1173 | phydev->sysfs_links = true; |
| 1174 | } |
| 1175 | |
Ioana Ciornei | c920f74 | 2019-05-28 20:38:10 +0300 | [diff] [blame] | 1176 | static ssize_t |
| 1177 | phy_standalone_show(struct device *dev, struct device_attribute *attr, |
| 1178 | char *buf) |
| 1179 | { |
| 1180 | struct phy_device *phydev = to_phy_device(dev); |
| 1181 | |
| 1182 | return sprintf(buf, "%d\n", !phydev->attached_dev); |
| 1183 | } |
| 1184 | static DEVICE_ATTR_RO(phy_standalone); |
| 1185 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1186 | /** |
Russell King | 298e54f | 2019-11-15 19:56:51 +0000 | [diff] [blame] | 1187 | * phy_sfp_attach - attach the SFP bus to the PHY upstream network device |
| 1188 | * @upstream: pointer to the phy device |
| 1189 | * @bus: sfp bus representing cage being attached |
| 1190 | * |
| 1191 | * This is used to fill in the sfp_upstream_ops .attach member. |
| 1192 | */ |
| 1193 | void phy_sfp_attach(void *upstream, struct sfp_bus *bus) |
| 1194 | { |
| 1195 | struct phy_device *phydev = upstream; |
| 1196 | |
| 1197 | if (phydev->attached_dev) |
| 1198 | phydev->attached_dev->sfp_bus = bus; |
| 1199 | phydev->sfp_bus_attached = true; |
| 1200 | } |
| 1201 | EXPORT_SYMBOL(phy_sfp_attach); |
| 1202 | |
| 1203 | /** |
| 1204 | * phy_sfp_detach - detach the SFP bus from the PHY upstream network device |
| 1205 | * @upstream: pointer to the phy device |
| 1206 | * @bus: sfp bus representing cage being attached |
| 1207 | * |
| 1208 | * This is used to fill in the sfp_upstream_ops .detach member. |
| 1209 | */ |
| 1210 | void phy_sfp_detach(void *upstream, struct sfp_bus *bus) |
| 1211 | { |
| 1212 | struct phy_device *phydev = upstream; |
| 1213 | |
| 1214 | if (phydev->attached_dev) |
| 1215 | phydev->attached_dev->sfp_bus = NULL; |
| 1216 | phydev->sfp_bus_attached = false; |
| 1217 | } |
| 1218 | EXPORT_SYMBOL(phy_sfp_detach); |
| 1219 | |
| 1220 | /** |
| 1221 | * phy_sfp_probe - probe for a SFP cage attached to this PHY device |
| 1222 | * @phydev: Pointer to phy_device |
| 1223 | * @ops: SFP's upstream operations |
| 1224 | */ |
| 1225 | int phy_sfp_probe(struct phy_device *phydev, |
| 1226 | const struct sfp_upstream_ops *ops) |
| 1227 | { |
| 1228 | struct sfp_bus *bus; |
| 1229 | int ret; |
| 1230 | |
| 1231 | if (phydev->mdio.dev.fwnode) { |
| 1232 | bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); |
| 1233 | if (IS_ERR(bus)) |
| 1234 | return PTR_ERR(bus); |
| 1235 | |
| 1236 | phydev->sfp_bus = bus; |
| 1237 | |
| 1238 | ret = sfp_bus_add_upstream(bus, phydev, ops); |
| 1239 | sfp_bus_put(bus); |
| 1240 | } |
| 1241 | return 0; |
| 1242 | } |
| 1243 | EXPORT_SYMBOL(phy_sfp_probe); |
| 1244 | |
| 1245 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1246 | * 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] | 1247 | * @dev: network device to attach |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1248 | * @phydev: Pointer to phy_device to attach |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1249 | * @flags: PHY device's dev_flags |
| 1250 | * @interface: PHY device's interface |
| 1251 | * |
| 1252 | * Description: Called by drivers to attach to a particular PHY |
| 1253 | * device. The phy_device is found, and properly hooked up |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1254 | * to the phy_driver. If no driver is attached, then a |
| 1255 | * generic driver is used. The phy_device is given a ptr to |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1256 | * the attaching device, and given a callback for link status |
| 1257 | * change. The phy_device is returned to the attaching driver. |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1258 | * This function takes a reference on the phy device. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1259 | */ |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1260 | int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
| 1261 | u32 flags, phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1262 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1263 | struct mii_bus *bus = phydev->mdio.bus; |
| 1264 | struct device *d = &phydev->mdio.dev; |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1265 | struct module *ndev_owner = NULL; |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1266 | bool using_genphy = false; |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 1267 | int err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1268 | |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1269 | /* For Ethernet device drivers that register their own MDIO bus, we |
| 1270 | * will have bus->owner match ndev_mod, so we do not want to increment |
| 1271 | * our own module->refcnt here, otherwise we would not be able to |
| 1272 | * unload later on. |
| 1273 | */ |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1274 | if (dev) |
| 1275 | ndev_owner = dev->dev.parent->driver->owner; |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1276 | if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1277 | phydev_err(phydev, "failed to get the bus module\n"); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1278 | return -EIO; |
| 1279 | } |
| 1280 | |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1281 | get_device(d); |
| 1282 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1283 | /* Assume that if there is no driver, that it doesn't |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1284 | * exist, and we should use the genphy driver. |
| 1285 | */ |
Sergei Shtylyov | ef899c0 | 2015-08-28 21:35:14 +0300 | [diff] [blame] | 1286 | if (!d->driver) { |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1287 | if (phydev->is_c45) |
Heiner Kallweit | 22b56e8 | 2019-04-07 13:55:01 +0200 | [diff] [blame] | 1288 | d->driver = &genphy_c45_driver.mdiodrv.driver; |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1289 | else |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 1290 | d->driver = &genphy_driver.mdiodrv.driver; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1291 | |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1292 | using_genphy = true; |
| 1293 | } |
| 1294 | |
| 1295 | if (!try_module_get(d->driver->owner)) { |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1296 | phydev_err(phydev, "failed to get the device driver module\n"); |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1297 | err = -EIO; |
| 1298 | goto error_put_device; |
| 1299 | } |
| 1300 | |
| 1301 | if (using_genphy) { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1302 | err = d->driver->probe(d); |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 1303 | if (err >= 0) |
| 1304 | err = device_bind_driver(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1305 | |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 1306 | if (err) |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1307 | goto error_module_put; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | if (phydev->attached_dev) { |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1311 | dev_err(&dev->dev, "PHY already attached\n"); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1312 | err = -EBUSY; |
| 1313 | goto error; |
Ezequiel Garcia | b3565f2 | 2014-07-23 16:47:32 -0300 | [diff] [blame] | 1314 | } |
| 1315 | |
Russell King | a81497b | 2017-07-25 15:02:58 +0100 | [diff] [blame] | 1316 | phydev->phy_link_change = phy_link_change; |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1317 | if (dev) { |
| 1318 | phydev->attached_dev = dev; |
| 1319 | dev->phydev = phydev; |
Russell King | 298e54f | 2019-11-15 19:56:51 +0000 | [diff] [blame] | 1320 | |
| 1321 | if (phydev->sfp_bus_attached) |
| 1322 | dev->sfp_bus = phydev->sfp_bus; |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1323 | } |
Florian Fainelli | a399546 | 2017-05-27 10:42:25 -0700 | [diff] [blame] | 1324 | |
| 1325 | /* Some Ethernet drivers try to connect to a PHY device before |
| 1326 | * calling register_netdevice() -> netdev_register_kobject() and |
| 1327 | * does the dev->dev.kobj initialization. Here we only check for |
| 1328 | * success which indicates that the network device kobject is |
| 1329 | * ready. Once we do that we still need to keep track of whether |
| 1330 | * links were successfully set up or not for phy_detach() to |
| 1331 | * remove them accordingly. |
| 1332 | */ |
| 1333 | phydev->sysfs_links = false; |
| 1334 | |
Vladimir Oltean | 53cfca2 | 2019-05-28 20:38:07 +0300 | [diff] [blame] | 1335 | phy_sysfs_create_links(phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1336 | |
Ioana Ciornei | c920f74 | 2019-05-28 20:38:10 +0300 | [diff] [blame] | 1337 | if (!phydev->attached_dev) { |
| 1338 | err = sysfs_create_file(&phydev->mdio.dev.kobj, |
| 1339 | &dev_attr_phy_standalone.attr); |
| 1340 | if (err) |
| 1341 | phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); |
| 1342 | } |
| 1343 | |
Tao Ren | e7312ef | 2019-10-22 11:31:06 -0700 | [diff] [blame] | 1344 | phydev->dev_flags |= flags; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1345 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 1346 | phydev->interface = interface; |
| 1347 | |
Anton Vorontsov | ef24b16 | 2010-08-24 14:46:12 -0700 | [diff] [blame] | 1348 | phydev->state = PHY_READY; |
| 1349 | |
Sjoerd Simons | 113c74d | 2016-01-14 21:57:18 +0100 | [diff] [blame] | 1350 | /* Initial carrier state is off as the phy is about to be |
| 1351 | * (re)initialized. |
| 1352 | */ |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1353 | if (dev) |
| 1354 | netif_carrier_off(phydev->attached_dev); |
Sjoerd Simons | 113c74d | 2016-01-14 21:57:18 +0100 | [diff] [blame] | 1355 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 1356 | /* Do initial configuration here, now that |
| 1357 | * we have certain key parameters |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1358 | * (dev_flags and interface) |
| 1359 | */ |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 1360 | err = phy_init_hw(phydev); |
| 1361 | if (err) |
Woojung Huh | a7dac9f | 2016-11-23 23:10:33 +0000 | [diff] [blame] | 1362 | goto error; |
Sebastian Hesselbarth | 1211ce5 | 2013-12-13 10:20:28 +0100 | [diff] [blame] | 1363 | |
Woojung Huh | a7dac9f | 2016-11-23 23:10:33 +0000 | [diff] [blame] | 1364 | phy_resume(phydev); |
Zach Brown | 2e0bc45 | 2016-10-17 10:49:55 -0500 | [diff] [blame] | 1365 | phy_led_triggers_register(phydev); |
| 1366 | |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 1367 | return err; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1368 | |
| 1369 | error: |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1370 | /* phy_detach() does all of the cleanup below */ |
Woojung Huh | a7dac9f | 2016-11-23 23:10:33 +0000 | [diff] [blame] | 1371 | phy_detach(phydev); |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1372 | return err; |
| 1373 | |
| 1374 | error_module_put: |
Mao Wenan | cafe8df | 2017-01-31 18:46:43 -0800 | [diff] [blame] | 1375 | module_put(d->driver->owner); |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1376 | error_put_device: |
| 1377 | put_device(d); |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1378 | if (ndev_owner != bus->owner) |
| 1379 | module_put(bus->owner); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1380 | return err; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1381 | } |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1382 | EXPORT_SYMBOL(phy_attach_direct); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1383 | |
| 1384 | /** |
| 1385 | * phy_attach - attach a network device to a particular PHY device |
| 1386 | * @dev: network device to attach |
| 1387 | * @bus_id: Bus ID of PHY device to attach |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1388 | * @interface: PHY device's interface |
| 1389 | * |
| 1390 | * Description: Same as phy_attach_direct() except that a PHY bus_id |
| 1391 | * string is passed instead of a pointer to a struct phy_device. |
| 1392 | */ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1393 | struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, |
| 1394 | phy_interface_t interface) |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1395 | { |
| 1396 | struct bus_type *bus = &mdio_bus_type; |
| 1397 | struct phy_device *phydev; |
| 1398 | struct device *d; |
| 1399 | int rc; |
| 1400 | |
Ioana Ciornei | 82c76ac | 2019-05-28 20:38:09 +0300 | [diff] [blame] | 1401 | if (!dev) |
| 1402 | return ERR_PTR(-EINVAL); |
| 1403 | |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1404 | /* Search the list of PHY devices on the mdio bus for the |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1405 | * PHY with the requested name |
| 1406 | */ |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1407 | d = bus_find_device_by_name(bus, NULL, bus_id); |
| 1408 | if (!d) { |
| 1409 | pr_err("PHY %s not found\n", bus_id); |
| 1410 | return ERR_PTR(-ENODEV); |
| 1411 | } |
| 1412 | phydev = to_phy_device(d); |
| 1413 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 1414 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
Johan Hovold | 17ae1c6 | 2016-11-03 18:40:19 +0100 | [diff] [blame] | 1415 | put_device(d); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1416 | if (rc) |
| 1417 | return ERR_PTR(rc); |
| 1418 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1419 | return phydev; |
| 1420 | } |
| 1421 | EXPORT_SYMBOL(phy_attach); |
| 1422 | |
Florian Fainelli | 5db5ea9 | 2019-01-15 15:09:35 -0800 | [diff] [blame] | 1423 | static bool phy_driver_is_genphy_kind(struct phy_device *phydev, |
| 1424 | struct device_driver *driver) |
| 1425 | { |
| 1426 | struct device *d = &phydev->mdio.dev; |
| 1427 | bool ret = false; |
| 1428 | |
| 1429 | if (!phydev->drv) |
| 1430 | return ret; |
| 1431 | |
| 1432 | get_device(d); |
| 1433 | ret = d->driver == driver; |
| 1434 | put_device(d); |
| 1435 | |
| 1436 | return ret; |
| 1437 | } |
| 1438 | |
| 1439 | bool phy_driver_is_genphy(struct phy_device *phydev) |
| 1440 | { |
| 1441 | return phy_driver_is_genphy_kind(phydev, |
| 1442 | &genphy_driver.mdiodrv.driver); |
| 1443 | } |
| 1444 | EXPORT_SYMBOL_GPL(phy_driver_is_genphy); |
| 1445 | |
| 1446 | bool phy_driver_is_genphy_10g(struct phy_device *phydev) |
| 1447 | { |
| 1448 | return phy_driver_is_genphy_kind(phydev, |
Heiner Kallweit | 22b56e8 | 2019-04-07 13:55:01 +0200 | [diff] [blame] | 1449 | &genphy_c45_driver.mdiodrv.driver); |
Florian Fainelli | 5db5ea9 | 2019-01-15 15:09:35 -0800 | [diff] [blame] | 1450 | } |
| 1451 | EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); |
| 1452 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1453 | /** |
| 1454 | * phy_detach - detach a PHY device from its network device |
| 1455 | * @phydev: target phy_device struct |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1456 | * |
| 1457 | * This detaches the phy device from its network device and the phy |
| 1458 | * driver, and drops the reference count taken in phy_attach_direct(). |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1459 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1460 | void phy_detach(struct phy_device *phydev) |
| 1461 | { |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1462 | struct net_device *dev = phydev->attached_dev; |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1463 | struct module *ndev_owner = NULL; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1464 | struct mii_bus *bus; |
Ezequiel Garcia | b3565f2 | 2014-07-23 16:47:32 -0300 | [diff] [blame] | 1465 | |
Florian Fainelli | a399546 | 2017-05-27 10:42:25 -0700 | [diff] [blame] | 1466 | if (phydev->sysfs_links) { |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1467 | if (dev) |
| 1468 | sysfs_remove_link(&dev->dev.kobj, "phydev"); |
Florian Fainelli | a399546 | 2017-05-27 10:42:25 -0700 | [diff] [blame] | 1469 | sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); |
| 1470 | } |
Ioana Ciornei | c920f74 | 2019-05-28 20:38:10 +0300 | [diff] [blame] | 1471 | |
| 1472 | if (!phydev->attached_dev) |
| 1473 | sysfs_remove_file(&phydev->mdio.dev.kobj, |
| 1474 | &dev_attr_phy_standalone.attr); |
| 1475 | |
Heiner Kallweit | 93f41e6 | 2018-09-24 22:01:32 +0200 | [diff] [blame] | 1476 | phy_suspend(phydev); |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1477 | if (dev) { |
| 1478 | phydev->attached_dev->phydev = NULL; |
| 1479 | phydev->attached_dev = NULL; |
| 1480 | } |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1481 | phydev->phylink = NULL; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1482 | |
Geert Uytterhoeven | 0075bd6 | 2016-11-28 15:18:31 +0100 | [diff] [blame] | 1483 | phy_led_triggers_unregister(phydev); |
| 1484 | |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1485 | module_put(phydev->mdio.dev.driver->owner); |
| 1486 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1487 | /* If the device had no specific driver before (i.e. - it |
| 1488 | * was using the generic driver), we unbind the device |
| 1489 | * from the generic driver so that there's a chance a |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1490 | * real driver could be loaded |
| 1491 | */ |
Florian Fainelli | 5db5ea9 | 2019-01-15 15:09:35 -0800 | [diff] [blame] | 1492 | if (phy_driver_is_genphy(phydev) || |
| 1493 | phy_driver_is_genphy_10g(phydev)) |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 1494 | device_release_driver(&phydev->mdio.dev); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1495 | |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1496 | /* |
| 1497 | * The phydev might go away on the put_device() below, so avoid |
| 1498 | * a use-after-free bug by reading the underlying bus first. |
| 1499 | */ |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1500 | bus = phydev->mdio.bus; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1501 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1502 | put_device(&phydev->mdio.dev); |
Ioana Ciornei | 2db2d9d1 | 2019-05-28 20:38:08 +0300 | [diff] [blame] | 1503 | if (dev) |
| 1504 | ndev_owner = dev->dev.parent->driver->owner; |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1505 | if (ndev_owner != bus->owner) |
| 1506 | module_put(bus->owner); |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 1507 | |
| 1508 | /* Assert the reset signal */ |
| 1509 | phy_device_reset(phydev, 1); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1510 | } |
| 1511 | EXPORT_SYMBOL(phy_detach); |
| 1512 | |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1513 | int phy_suspend(struct phy_device *phydev) |
| 1514 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1515 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
Heiner Kallweit | 93f41e6 | 2018-09-24 22:01:32 +0200 | [diff] [blame] | 1516 | struct net_device *netdev = phydev->attached_dev; |
Sebastian Hesselbarth | 32fc3fd | 2014-03-14 10:07:44 +0100 | [diff] [blame] | 1517 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1518 | int ret = 0; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1519 | |
| 1520 | /* If the device has WOL enabled, we cannot suspend the PHY */ |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1521 | phy_ethtool_get_wol(phydev, &wol); |
Heiner Kallweit | 93f41e6 | 2018-09-24 22:01:32 +0200 | [diff] [blame] | 1522 | if (wol.wolopts || (netdev && netdev->wol_enabled)) |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1523 | return -EBUSY; |
| 1524 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1525 | if (phydev->drv && phydrv->suspend) |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1526 | ret = phydrv->suspend(phydev); |
| 1527 | |
| 1528 | if (ret) |
| 1529 | return ret; |
| 1530 | |
| 1531 | phydev->suspended = true; |
| 1532 | |
| 1533 | return ret; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1534 | } |
Florian Fainelli | ca12769 | 2014-07-08 10:38:36 -0700 | [diff] [blame] | 1535 | EXPORT_SYMBOL(phy_suspend); |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1536 | |
Andrew Lunn | 9c2c2e6 | 2018-02-27 01:56:06 +0100 | [diff] [blame] | 1537 | int __phy_resume(struct phy_device *phydev) |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1538 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1539 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1540 | int ret = 0; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1541 | |
Russell King | f5e6403 | 2017-12-12 10:45:36 +0000 | [diff] [blame] | 1542 | WARN_ON(!mutex_is_locked(&phydev->lock)); |
| 1543 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1544 | if (phydev->drv && phydrv->resume) |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1545 | ret = phydrv->resume(phydev); |
| 1546 | |
| 1547 | if (ret) |
| 1548 | return ret; |
| 1549 | |
| 1550 | phydev->suspended = false; |
| 1551 | |
| 1552 | return ret; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1553 | } |
Andrew Lunn | 9c2c2e6 | 2018-02-27 01:56:06 +0100 | [diff] [blame] | 1554 | EXPORT_SYMBOL(__phy_resume); |
| 1555 | |
| 1556 | int phy_resume(struct phy_device *phydev) |
| 1557 | { |
| 1558 | int ret; |
| 1559 | |
| 1560 | mutex_lock(&phydev->lock); |
| 1561 | ret = __phy_resume(phydev); |
| 1562 | mutex_unlock(&phydev->lock); |
| 1563 | |
| 1564 | return ret; |
| 1565 | } |
Florian Fainelli | ca12769 | 2014-07-08 10:38:36 -0700 | [diff] [blame] | 1566 | EXPORT_SYMBOL(phy_resume); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1567 | |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 1568 | int phy_loopback(struct phy_device *phydev, bool enable) |
| 1569 | { |
| 1570 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
| 1571 | int ret = 0; |
| 1572 | |
| 1573 | mutex_lock(&phydev->lock); |
| 1574 | |
| 1575 | if (enable && phydev->loopback_enabled) { |
| 1576 | ret = -EBUSY; |
| 1577 | goto out; |
| 1578 | } |
| 1579 | |
| 1580 | if (!enable && !phydev->loopback_enabled) { |
| 1581 | ret = -EINVAL; |
| 1582 | goto out; |
| 1583 | } |
| 1584 | |
| 1585 | if (phydev->drv && phydrv->set_loopback) |
| 1586 | ret = phydrv->set_loopback(phydev, enable); |
| 1587 | else |
| 1588 | ret = -EOPNOTSUPP; |
| 1589 | |
| 1590 | if (ret) |
| 1591 | goto out; |
| 1592 | |
| 1593 | phydev->loopback_enabled = enable; |
| 1594 | |
| 1595 | out: |
| 1596 | mutex_unlock(&phydev->lock); |
| 1597 | return ret; |
| 1598 | } |
| 1599 | EXPORT_SYMBOL(phy_loopback); |
| 1600 | |
Richard Leitner | a966849 | 2017-12-11 13:16:58 +0100 | [diff] [blame] | 1601 | /** |
| 1602 | * phy_reset_after_clk_enable - perform a PHY reset if needed |
| 1603 | * @phydev: target phy_device struct |
| 1604 | * |
| 1605 | * Description: Some PHYs are known to need a reset after their refclk was |
| 1606 | * enabled. This function evaluates the flags and perform the reset if it's |
| 1607 | * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy |
| 1608 | * was reset. |
| 1609 | */ |
| 1610 | int phy_reset_after_clk_enable(struct phy_device *phydev) |
| 1611 | { |
| 1612 | if (!phydev || !phydev->drv) |
| 1613 | return -ENODEV; |
| 1614 | |
| 1615 | if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { |
| 1616 | phy_device_reset(phydev, 1); |
| 1617 | phy_device_reset(phydev, 0); |
| 1618 | return 1; |
| 1619 | } |
| 1620 | |
| 1621 | return 0; |
| 1622 | } |
| 1623 | EXPORT_SYMBOL(phy_reset_after_clk_enable); |
| 1624 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1625 | /* Generic PHY support and helper functions */ |
| 1626 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1627 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1628 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1629 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1630 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1631 | * Description: Writes MII_ADVERTISE with the appropriate values, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1632 | * after sanitizing the values to make sure we only advertise |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1633 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 1634 | * hasn't changed, and > 0 if it has changed. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1635 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 1636 | static int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1637 | { |
Heiner Kallweit | 3eef868 | 2019-08-09 20:43:04 +0200 | [diff] [blame] | 1638 | int err, bmsr, changed = 0; |
| 1639 | u32 adv; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1640 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1641 | /* Only allow advertising what this PHY supports */ |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1642 | linkmode_and(phydev->advertising, phydev->advertising, |
| 1643 | phydev->supported); |
Heiner Kallweit | 3eef868 | 2019-08-09 20:43:04 +0200 | [diff] [blame] | 1644 | |
| 1645 | adv = linkmode_adv_to_mii_adv_t(phydev->advertising); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1646 | |
| 1647 | /* Setup standard advertisement */ |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame] | 1648 | err = phy_modify_changed(phydev, MII_ADVERTISE, |
| 1649 | ADVERTISE_ALL | ADVERTISE_100BASE4 | |
| 1650 | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, |
Heiner Kallweit | 3eef868 | 2019-08-09 20:43:04 +0200 | [diff] [blame] | 1651 | adv); |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame] | 1652 | if (err < 0) |
| 1653 | return err; |
| 1654 | if (err > 0) |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1655 | changed = 1; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1656 | |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1657 | bmsr = phy_read(phydev, MII_BMSR); |
| 1658 | if (bmsr < 0) |
| 1659 | return bmsr; |
| 1660 | |
| 1661 | /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all |
| 1662 | * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a |
| 1663 | * logical 1. |
| 1664 | */ |
| 1665 | if (!(bmsr & BMSR_ESTATEN)) |
| 1666 | return changed; |
| 1667 | |
Heiner Kallweit | 3eef868 | 2019-08-09 20:43:04 +0200 | [diff] [blame] | 1668 | adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1669 | |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame] | 1670 | err = phy_modify_changed(phydev, MII_CTRL1000, |
| 1671 | ADVERTISE_1000FULL | ADVERTISE_1000HALF, |
| 1672 | adv); |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1673 | if (err < 0) |
| 1674 | return err; |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame] | 1675 | if (err > 0) |
| 1676 | changed = 1; |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1677 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1678 | return changed; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1679 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1680 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1681 | /** |
Heiner Kallweit | fa6e98c | 2019-10-22 11:31:07 -0700 | [diff] [blame] | 1682 | * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters |
| 1683 | * @phydev: target phy_device struct |
| 1684 | * |
| 1685 | * Description: Writes MII_ADVERTISE with the appropriate values, |
| 1686 | * after sanitizing the values to make sure we only advertise |
| 1687 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 1688 | * hasn't changed, and > 0 if it has changed. This function is intended |
| 1689 | * for Clause 37 1000Base-X mode. |
| 1690 | */ |
| 1691 | static int genphy_c37_config_advert(struct phy_device *phydev) |
| 1692 | { |
| 1693 | u16 adv = 0; |
| 1694 | |
| 1695 | /* Only allow advertising what this PHY supports */ |
| 1696 | linkmode_and(phydev->advertising, phydev->advertising, |
| 1697 | phydev->supported); |
| 1698 | |
| 1699 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, |
| 1700 | phydev->advertising)) |
| 1701 | adv |= ADVERTISE_1000XFULL; |
| 1702 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 1703 | phydev->advertising)) |
| 1704 | adv |= ADVERTISE_1000XPAUSE; |
| 1705 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 1706 | phydev->advertising)) |
| 1707 | adv |= ADVERTISE_1000XPSE_ASYM; |
| 1708 | |
| 1709 | return phy_modify_changed(phydev, MII_ADVERTISE, |
| 1710 | ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | |
| 1711 | ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, |
| 1712 | adv); |
| 1713 | } |
| 1714 | |
| 1715 | /** |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1716 | * genphy_config_eee_advert - disable unwanted eee mode advertisement |
| 1717 | * @phydev: target phy_device struct |
| 1718 | * |
| 1719 | * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy |
| 1720 | * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't |
| 1721 | * changed, and 1 if it has changed. |
| 1722 | */ |
Heiner Kallweit | cd34499 | 2019-02-18 21:26:58 +0100 | [diff] [blame] | 1723 | int genphy_config_eee_advert(struct phy_device *phydev) |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1724 | { |
Heiner Kallweit | 9f771f1 | 2019-02-11 22:16:13 +0100 | [diff] [blame] | 1725 | int err; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1726 | |
| 1727 | /* Nothing to disable */ |
Heiner Kallweit | 9f771f1 | 2019-02-11 22:16:13 +0100 | [diff] [blame] | 1728 | if (!phydev->eee_broken_modes) |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1729 | return 0; |
| 1730 | |
Heiner Kallweit | 9f771f1 | 2019-02-11 22:16:13 +0100 | [diff] [blame] | 1731 | err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, |
| 1732 | phydev->eee_broken_modes, 0); |
| 1733 | /* If the call failed, we assume that EEE is not supported */ |
| 1734 | return err < 0 ? 0 : err; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1735 | } |
Heiner Kallweit | cd34499 | 2019-02-18 21:26:58 +0100 | [diff] [blame] | 1736 | EXPORT_SYMBOL(genphy_config_eee_advert); |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1737 | |
| 1738 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1739 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 1740 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1741 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1742 | * Description: Configures MII_BMCR to force speed/duplex |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1743 | * to the values in phydev. Assumes that the values are valid. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1744 | * Please see phy_sanitize_settings(). |
| 1745 | */ |
Madalin Bucur | 3fb69bc | 2013-11-20 16:38:19 -0600 | [diff] [blame] | 1746 | int genphy_setup_forced(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1747 | { |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 1748 | u16 ctl = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1749 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1750 | phydev->pause = 0; |
| 1751 | phydev->asym_pause = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1752 | |
| 1753 | if (SPEED_1000 == phydev->speed) |
| 1754 | ctl |= BMCR_SPEED1000; |
| 1755 | else if (SPEED_100 == phydev->speed) |
| 1756 | ctl |= BMCR_SPEED100; |
| 1757 | |
| 1758 | if (DUPLEX_FULL == phydev->duplex) |
| 1759 | ctl |= BMCR_FULLDPLX; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1760 | |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 1761 | return phy_modify(phydev, MII_BMCR, |
Ingo van Lil | 18a5b05 | 2018-02-12 12:02:52 +0100 | [diff] [blame] | 1762 | ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1763 | } |
Madalin Bucur | 3fb69bc | 2013-11-20 16:38:19 -0600 | [diff] [blame] | 1764 | EXPORT_SYMBOL(genphy_setup_forced); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1765 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1766 | /** |
| 1767 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 1768 | * @phydev: target phy_device struct |
| 1769 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1770 | int genphy_restart_aneg(struct phy_device *phydev) |
| 1771 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1772 | /* Don't isolate the PHY if we're negotiating */ |
Russell King | f102852 | 2018-01-05 16:07:10 +0000 | [diff] [blame] | 1773 | return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 1774 | BMCR_ANENABLE | BMCR_ANRESTART); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1775 | } |
Adrian Bunk | 892871d | 2008-10-13 18:48:09 -0700 | [diff] [blame] | 1776 | EXPORT_SYMBOL(genphy_restart_aneg); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1777 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1778 | /** |
Russell King | 2a10ab0 | 2019-12-17 13:39:11 +0000 | [diff] [blame] | 1779 | * genphy_check_and_restart_aneg - Enable and restart auto-negotiation |
| 1780 | * @phydev: target phy_device struct |
| 1781 | * @restart: whether aneg restart is requested |
| 1782 | * |
| 1783 | * Check, and restart auto-negotiation if needed. |
| 1784 | */ |
| 1785 | int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) |
| 1786 | { |
| 1787 | int ret = 0; |
| 1788 | |
| 1789 | if (!restart) { |
| 1790 | /* Advertisement hasn't changed, but maybe aneg was never on to |
| 1791 | * begin with? Or maybe phy was isolated? |
| 1792 | */ |
| 1793 | ret = phy_read(phydev, MII_BMCR); |
| 1794 | if (ret < 0) |
| 1795 | return ret; |
| 1796 | |
| 1797 | if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) |
| 1798 | restart = true; |
| 1799 | } |
| 1800 | |
| 1801 | if (restart) |
| 1802 | ret = genphy_restart_aneg(phydev); |
| 1803 | |
| 1804 | return ret; |
| 1805 | } |
| 1806 | EXPORT_SYMBOL(genphy_check_and_restart_aneg); |
| 1807 | |
| 1808 | /** |
Heiner Kallweit | f4069cd | 2019-08-09 20:43:50 +0200 | [diff] [blame] | 1809 | * __genphy_config_aneg - restart auto-negotiation or write BMCR |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1810 | * @phydev: target phy_device struct |
Heiner Kallweit | f4069cd | 2019-08-09 20:43:50 +0200 | [diff] [blame] | 1811 | * @changed: whether autoneg is requested |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1812 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1813 | * Description: If auto-negotiation is enabled, we configure the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1814 | * advertising, and then restart auto-negotiation. If it is not |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1815 | * enabled, then we write the BMCR. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1816 | */ |
Heiner Kallweit | f4069cd | 2019-08-09 20:43:50 +0200 | [diff] [blame] | 1817 | int __genphy_config_aneg(struct phy_device *phydev, bool changed) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1818 | { |
Heiner Kallweit | f4069cd | 2019-08-09 20:43:50 +0200 | [diff] [blame] | 1819 | int err; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1820 | |
Heiner Kallweit | f4069cd | 2019-08-09 20:43:50 +0200 | [diff] [blame] | 1821 | if (genphy_config_eee_advert(phydev)) |
| 1822 | changed = true; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1823 | |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1824 | if (AUTONEG_ENABLE != phydev->autoneg) |
| 1825 | return genphy_setup_forced(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1826 | |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1827 | err = genphy_config_advert(phydev); |
| 1828 | if (err < 0) /* error */ |
| 1829 | return err; |
Heiner Kallweit | f4069cd | 2019-08-09 20:43:50 +0200 | [diff] [blame] | 1830 | else if (err) |
| 1831 | changed = true; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1832 | |
Russell King | 2a10ab0 | 2019-12-17 13:39:11 +0000 | [diff] [blame] | 1833 | return genphy_check_and_restart_aneg(phydev, changed); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1834 | } |
Heiner Kallweit | f4069cd | 2019-08-09 20:43:50 +0200 | [diff] [blame] | 1835 | EXPORT_SYMBOL(__genphy_config_aneg); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1836 | |
Florian Fainelli | a9fa6e6 | 2014-02-11 17:27:36 -0800 | [diff] [blame] | 1837 | /** |
Heiner Kallweit | fa6e98c | 2019-10-22 11:31:07 -0700 | [diff] [blame] | 1838 | * genphy_c37_config_aneg - restart auto-negotiation or write BMCR |
| 1839 | * @phydev: target phy_device struct |
| 1840 | * |
| 1841 | * Description: If auto-negotiation is enabled, we configure the |
| 1842 | * advertising, and then restart auto-negotiation. If it is not |
| 1843 | * enabled, then we write the BMCR. This function is intended |
| 1844 | * for use with Clause 37 1000Base-X mode. |
| 1845 | */ |
| 1846 | int genphy_c37_config_aneg(struct phy_device *phydev) |
| 1847 | { |
| 1848 | int err, changed; |
| 1849 | |
| 1850 | if (phydev->autoneg != AUTONEG_ENABLE) |
| 1851 | return genphy_setup_forced(phydev); |
| 1852 | |
| 1853 | err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, |
| 1854 | BMCR_SPEED1000); |
| 1855 | if (err) |
| 1856 | return err; |
| 1857 | |
| 1858 | changed = genphy_c37_config_advert(phydev); |
| 1859 | if (changed < 0) /* error */ |
| 1860 | return changed; |
| 1861 | |
| 1862 | if (!changed) { |
| 1863 | /* Advertisement hasn't changed, but maybe aneg was never on to |
| 1864 | * begin with? Or maybe phy was isolated? |
| 1865 | */ |
| 1866 | int ctl = phy_read(phydev, MII_BMCR); |
| 1867 | |
| 1868 | if (ctl < 0) |
| 1869 | return ctl; |
| 1870 | |
| 1871 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) |
| 1872 | changed = 1; /* do restart aneg */ |
| 1873 | } |
| 1874 | |
| 1875 | /* Only restart aneg if we are advertising something different |
| 1876 | * than we were before. |
| 1877 | */ |
| 1878 | if (changed > 0) |
| 1879 | return genphy_restart_aneg(phydev); |
| 1880 | |
| 1881 | return 0; |
| 1882 | } |
| 1883 | EXPORT_SYMBOL(genphy_c37_config_aneg); |
| 1884 | |
| 1885 | /** |
Florian Fainelli | a9fa6e6 | 2014-02-11 17:27:36 -0800 | [diff] [blame] | 1886 | * genphy_aneg_done - return auto-negotiation status |
| 1887 | * @phydev: target phy_device struct |
| 1888 | * |
| 1889 | * Description: Reads the status register and returns 0 either if |
| 1890 | * auto-negotiation is incomplete, or if there was an error. |
| 1891 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. |
| 1892 | */ |
| 1893 | int genphy_aneg_done(struct phy_device *phydev) |
| 1894 | { |
| 1895 | int retval = phy_read(phydev, MII_BMSR); |
| 1896 | |
| 1897 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); |
| 1898 | } |
| 1899 | EXPORT_SYMBOL(genphy_aneg_done); |
| 1900 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1901 | /** |
| 1902 | * genphy_update_link - update link status in @phydev |
| 1903 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1904 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1905 | * Description: Update the value in phydev->link to reflect the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1906 | * current link value. In order to do this, we need to read |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1907 | * the status register twice, keeping the second value. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1908 | */ |
| 1909 | int genphy_update_link(struct phy_device *phydev) |
| 1910 | { |
Heiner Kallweit | c36757eb | 2019-08-12 21:20:02 +0200 | [diff] [blame] | 1911 | int status = 0, bmcr; |
| 1912 | |
| 1913 | bmcr = phy_read(phydev, MII_BMCR); |
| 1914 | if (bmcr < 0) |
| 1915 | return bmcr; |
| 1916 | |
| 1917 | /* Autoneg is being started, therefore disregard BMSR value and |
| 1918 | * report link as down. |
| 1919 | */ |
| 1920 | if (bmcr & BMCR_ANRESTART) |
| 1921 | goto done; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1922 | |
Heiner Kallweit | 93c0970 | 2019-02-06 19:39:52 +0100 | [diff] [blame] | 1923 | /* The link state is latched low so that momentary link |
| 1924 | * drops can be detected. Do not double-read the status |
| 1925 | * in polling mode to detect such short link drops. |
| 1926 | */ |
| 1927 | if (!phy_polling_mode(phydev)) { |
| 1928 | status = phy_read(phydev, MII_BMSR); |
Heiner Kallweit | b7f29f8 | 2019-04-08 19:28:28 +0200 | [diff] [blame] | 1929 | if (status < 0) |
Heiner Kallweit | 93c0970 | 2019-02-06 19:39:52 +0100 | [diff] [blame] | 1930 | return status; |
Heiner Kallweit | b7f29f8 | 2019-04-08 19:28:28 +0200 | [diff] [blame] | 1931 | else if (status & BMSR_LSTATUS) |
| 1932 | goto done; |
Heiner Kallweit | 93c0970 | 2019-02-06 19:39:52 +0100 | [diff] [blame] | 1933 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1934 | |
| 1935 | /* Read link and autonegotiation status */ |
| 1936 | status = phy_read(phydev, MII_BMSR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1937 | if (status < 0) |
| 1938 | return status; |
Heiner Kallweit | b7f29f8 | 2019-04-08 19:28:28 +0200 | [diff] [blame] | 1939 | done: |
Heiner Kallweit | 4950c2b | 2019-04-02 20:43:30 +0200 | [diff] [blame] | 1940 | phydev->link = status & BMSR_LSTATUS ? 1 : 0; |
| 1941 | phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1942 | |
Heiner Kallweit | aa6b195 | 2019-07-31 23:05:10 +0200 | [diff] [blame] | 1943 | /* Consider the case that autoneg was started and "aneg complete" |
| 1944 | * bit has been reset, but "link up" bit not yet. |
| 1945 | */ |
| 1946 | if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete) |
| 1947 | phydev->link = 0; |
| 1948 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1949 | return 0; |
| 1950 | } |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 1951 | EXPORT_SYMBOL(genphy_update_link); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1952 | |
Russell King | 8d3dc3ac | 2019-10-04 17:06:04 +0100 | [diff] [blame] | 1953 | int genphy_read_lpa(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1954 | { |
Russell King | 8d3dc3ac | 2019-10-04 17:06:04 +0100 | [diff] [blame] | 1955 | int lpa, lpagb; |
Heiner Kallweit | b6163f1 | 2019-03-30 10:22:45 +0100 | [diff] [blame] | 1956 | |
Yonglong Liu | 3de5ae5 | 2019-10-16 10:30:39 +0800 | [diff] [blame] | 1957 | if (phydev->autoneg == AUTONEG_ENABLE) { |
| 1958 | if (!phydev->autoneg_complete) { |
| 1959 | mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, |
| 1960 | 0); |
| 1961 | mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0); |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
Heiner Kallweit | 3b8b11f | 2019-04-05 21:23:13 +0200 | [diff] [blame] | 1965 | if (phydev->is_gigabit_capable) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1966 | lpagb = phy_read(phydev, MII_STAT1000); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1967 | if (lpagb < 0) |
| 1968 | return lpagb; |
| 1969 | |
Heiner Kallweit | b8f8c8e | 2018-07-21 15:48:47 +0200 | [diff] [blame] | 1970 | if (lpagb & LPA_1000MSFAIL) { |
Heiner Kallweit | 916e571 | 2019-08-15 13:15:19 +0200 | [diff] [blame] | 1971 | int adv = phy_read(phydev, MII_CTRL1000); |
| 1972 | |
| 1973 | if (adv < 0) |
| 1974 | return adv; |
| 1975 | |
Heiner Kallweit | b8f8c8e | 2018-07-21 15:48:47 +0200 | [diff] [blame] | 1976 | if (adv & CTL1000_ENABLE_MASTER) |
| 1977 | phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); |
| 1978 | else |
| 1979 | phydev_err(phydev, "Master/Slave resolution failed\n"); |
| 1980 | return -ENOLINK; |
| 1981 | } |
| 1982 | |
Andrew Lunn | 78a24df | 2018-12-05 21:49:41 +0100 | [diff] [blame] | 1983 | mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, |
| 1984 | lpagb); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | lpa = phy_read(phydev, MII_LPA); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1988 | if (lpa < 0) |
| 1989 | return lpa; |
| 1990 | |
Andrew Lunn | d335193 | 2018-12-05 21:49:43 +0100 | [diff] [blame] | 1991 | mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); |
Yonglong Liu | 3de5ae5 | 2019-10-16 10:30:39 +0800 | [diff] [blame] | 1992 | } else { |
| 1993 | linkmode_zero(phydev->lp_advertising); |
Russell King | 8d3dc3ac | 2019-10-04 17:06:04 +0100 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | return 0; |
| 1997 | } |
| 1998 | EXPORT_SYMBOL(genphy_read_lpa); |
| 1999 | |
| 2000 | /** |
Russell King | 0efc286 | 2019-12-17 13:39:16 +0000 | [diff] [blame] | 2001 | * genphy_read_status_fixed - read the link parameters for !aneg mode |
| 2002 | * @phydev: target phy_device struct |
| 2003 | * |
| 2004 | * Read the current duplex and speed state for a PHY operating with |
| 2005 | * autonegotiation disabled. |
| 2006 | */ |
| 2007 | int genphy_read_status_fixed(struct phy_device *phydev) |
| 2008 | { |
| 2009 | int bmcr = phy_read(phydev, MII_BMCR); |
| 2010 | |
| 2011 | if (bmcr < 0) |
| 2012 | return bmcr; |
| 2013 | |
| 2014 | if (bmcr & BMCR_FULLDPLX) |
| 2015 | phydev->duplex = DUPLEX_FULL; |
| 2016 | else |
| 2017 | phydev->duplex = DUPLEX_HALF; |
| 2018 | |
| 2019 | if (bmcr & BMCR_SPEED1000) |
| 2020 | phydev->speed = SPEED_1000; |
| 2021 | else if (bmcr & BMCR_SPEED100) |
| 2022 | phydev->speed = SPEED_100; |
| 2023 | else |
| 2024 | phydev->speed = SPEED_10; |
| 2025 | |
| 2026 | return 0; |
| 2027 | } |
| 2028 | EXPORT_SYMBOL(genphy_read_status_fixed); |
| 2029 | |
| 2030 | /** |
Russell King | 8d3dc3ac | 2019-10-04 17:06:04 +0100 | [diff] [blame] | 2031 | * genphy_read_status - check the link status and update current link state |
| 2032 | * @phydev: target phy_device struct |
| 2033 | * |
| 2034 | * Description: Check the link, then figure out the current state |
| 2035 | * by comparing what we advertise with what the link partner |
| 2036 | * advertises. Start by checking the gigabit possibilities, |
| 2037 | * then move on to 10/100. |
| 2038 | */ |
| 2039 | int genphy_read_status(struct phy_device *phydev) |
| 2040 | { |
| 2041 | int err, old_link = phydev->link; |
| 2042 | |
| 2043 | /* Update the link, but return if there was an error */ |
| 2044 | err = genphy_update_link(phydev); |
| 2045 | if (err) |
| 2046 | return err; |
| 2047 | |
| 2048 | /* why bother the PHY if nothing can have changed */ |
| 2049 | if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) |
| 2050 | return 0; |
| 2051 | |
| 2052 | phydev->speed = SPEED_UNKNOWN; |
| 2053 | phydev->duplex = DUPLEX_UNKNOWN; |
| 2054 | phydev->pause = 0; |
| 2055 | phydev->asym_pause = 0; |
| 2056 | |
| 2057 | err = genphy_read_lpa(phydev); |
| 2058 | if (err < 0) |
| 2059 | return err; |
| 2060 | |
| 2061 | if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { |
Heiner Kallweit | 5502b21 | 2019-02-14 22:16:27 +0100 | [diff] [blame] | 2062 | phy_resolve_aneg_linkmode(phydev); |
Heiner Kallweit | b6163f1 | 2019-03-30 10:22:45 +0100 | [diff] [blame] | 2063 | } else if (phydev->autoneg == AUTONEG_DISABLE) { |
Russell King | 0efc286 | 2019-12-17 13:39:16 +0000 | [diff] [blame] | 2064 | err = genphy_read_status_fixed(phydev); |
| 2065 | if (err < 0) |
| 2066 | return err; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | return 0; |
| 2070 | } |
| 2071 | EXPORT_SYMBOL(genphy_read_status); |
| 2072 | |
Florian Fainelli | 797ac07 | 2014-02-17 13:34:02 -0800 | [diff] [blame] | 2073 | /** |
Heiner Kallweit | fa6e98c | 2019-10-22 11:31:07 -0700 | [diff] [blame] | 2074 | * genphy_c37_read_status - check the link status and update current link state |
| 2075 | * @phydev: target phy_device struct |
| 2076 | * |
| 2077 | * Description: Check the link, then figure out the current state |
| 2078 | * by comparing what we advertise with what the link partner |
| 2079 | * advertises. This function is for Clause 37 1000Base-X mode. |
| 2080 | */ |
| 2081 | int genphy_c37_read_status(struct phy_device *phydev) |
| 2082 | { |
| 2083 | int lpa, err, old_link = phydev->link; |
| 2084 | |
| 2085 | /* Update the link, but return if there was an error */ |
| 2086 | err = genphy_update_link(phydev); |
| 2087 | if (err) |
| 2088 | return err; |
| 2089 | |
| 2090 | /* why bother the PHY if nothing can have changed */ |
| 2091 | if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) |
| 2092 | return 0; |
| 2093 | |
| 2094 | phydev->duplex = DUPLEX_UNKNOWN; |
| 2095 | phydev->pause = 0; |
| 2096 | phydev->asym_pause = 0; |
| 2097 | |
| 2098 | if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { |
| 2099 | lpa = phy_read(phydev, MII_LPA); |
| 2100 | if (lpa < 0) |
| 2101 | return lpa; |
| 2102 | |
| 2103 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, |
| 2104 | phydev->lp_advertising, lpa & LPA_LPACK); |
| 2105 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, |
| 2106 | phydev->lp_advertising, lpa & LPA_1000XFULL); |
| 2107 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2108 | phydev->lp_advertising, lpa & LPA_1000XPAUSE); |
| 2109 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2110 | phydev->lp_advertising, |
| 2111 | lpa & LPA_1000XPAUSE_ASYM); |
| 2112 | |
| 2113 | phy_resolve_aneg_linkmode(phydev); |
| 2114 | } else if (phydev->autoneg == AUTONEG_DISABLE) { |
| 2115 | int bmcr = phy_read(phydev, MII_BMCR); |
| 2116 | |
| 2117 | if (bmcr < 0) |
| 2118 | return bmcr; |
| 2119 | |
| 2120 | if (bmcr & BMCR_FULLDPLX) |
| 2121 | phydev->duplex = DUPLEX_FULL; |
| 2122 | else |
| 2123 | phydev->duplex = DUPLEX_HALF; |
| 2124 | } |
| 2125 | |
| 2126 | return 0; |
| 2127 | } |
| 2128 | EXPORT_SYMBOL(genphy_c37_read_status); |
| 2129 | |
| 2130 | /** |
Florian Fainelli | 797ac07 | 2014-02-17 13:34:02 -0800 | [diff] [blame] | 2131 | * genphy_soft_reset - software reset the PHY via BMCR_RESET bit |
| 2132 | * @phydev: target phy_device struct |
| 2133 | * |
| 2134 | * Description: Perform a software PHY reset using the standard |
| 2135 | * BMCR_RESET bit and poll for the reset bit to be cleared. |
| 2136 | * |
| 2137 | * Returns: 0 on success, < 0 on failure |
| 2138 | */ |
| 2139 | int genphy_soft_reset(struct phy_device *phydev) |
| 2140 | { |
Heiner Kallweit | 8c90b79 | 2019-04-24 21:41:06 +0200 | [diff] [blame] | 2141 | u16 res = BMCR_RESET; |
Florian Fainelli | 797ac07 | 2014-02-17 13:34:02 -0800 | [diff] [blame] | 2142 | int ret; |
| 2143 | |
Heiner Kallweit | 8c90b79 | 2019-04-24 21:41:06 +0200 | [diff] [blame] | 2144 | if (phydev->autoneg == AUTONEG_ENABLE) |
| 2145 | res |= BMCR_ANRESTART; |
| 2146 | |
| 2147 | ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); |
Florian Fainelli | 797ac07 | 2014-02-17 13:34:02 -0800 | [diff] [blame] | 2148 | if (ret < 0) |
| 2149 | return ret; |
| 2150 | |
Heiner Kallweit | 8c90b79 | 2019-04-24 21:41:06 +0200 | [diff] [blame] | 2151 | ret = phy_poll_reset(phydev); |
| 2152 | if (ret) |
| 2153 | return ret; |
| 2154 | |
| 2155 | /* BMCR may be reset to defaults */ |
| 2156 | if (phydev->autoneg == AUTONEG_DISABLE) |
| 2157 | ret = genphy_setup_forced(phydev); |
| 2158 | |
| 2159 | return ret; |
Florian Fainelli | 797ac07 | 2014-02-17 13:34:02 -0800 | [diff] [blame] | 2160 | } |
| 2161 | EXPORT_SYMBOL(genphy_soft_reset); |
| 2162 | |
Heiner Kallweit | 045925e | 2019-03-27 21:58:44 +0100 | [diff] [blame] | 2163 | /** |
| 2164 | * genphy_read_abilities - read PHY abilities from Clause 22 registers |
| 2165 | * @phydev: target phy_device struct |
| 2166 | * |
| 2167 | * Description: Reads the PHY's abilities and populates |
| 2168 | * phydev->supported accordingly. |
| 2169 | * |
| 2170 | * Returns: 0 on success, < 0 on failure |
| 2171 | */ |
| 2172 | int genphy_read_abilities(struct phy_device *phydev) |
| 2173 | { |
| 2174 | int val; |
| 2175 | |
| 2176 | linkmode_set_bit_array(phy_basic_ports_array, |
| 2177 | ARRAY_SIZE(phy_basic_ports_array), |
| 2178 | phydev->supported); |
| 2179 | |
| 2180 | val = phy_read(phydev, MII_BMSR); |
| 2181 | if (val < 0) |
| 2182 | return val; |
| 2183 | |
| 2184 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported, |
| 2185 | val & BMSR_ANEGCAPABLE); |
| 2186 | |
| 2187 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported, |
| 2188 | val & BMSR_100FULL); |
| 2189 | linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported, |
| 2190 | val & BMSR_100HALF); |
| 2191 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported, |
| 2192 | val & BMSR_10FULL); |
| 2193 | linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported, |
| 2194 | val & BMSR_10HALF); |
| 2195 | |
| 2196 | if (val & BMSR_ESTATEN) { |
| 2197 | val = phy_read(phydev, MII_ESTATUS); |
| 2198 | if (val < 0) |
| 2199 | return val; |
| 2200 | |
| 2201 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 2202 | phydev->supported, val & ESTATUS_1000_TFULL); |
| 2203 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 2204 | phydev->supported, val & ESTATUS_1000_THALF); |
Robert Hancock | f30e33b | 2019-06-11 16:06:09 -0600 | [diff] [blame] | 2205 | linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, |
| 2206 | phydev->supported, val & ESTATUS_1000_XFULL); |
Heiner Kallweit | 045925e | 2019-03-27 21:58:44 +0100 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | return 0; |
| 2210 | } |
| 2211 | EXPORT_SYMBOL(genphy_read_abilities); |
| 2212 | |
Kevin Hao | 5df7af8 | 2018-03-20 09:44:52 +0800 | [diff] [blame] | 2213 | /* This is used for the phy device which doesn't support the MMD extended |
| 2214 | * register access, but it does have side effect when we are trying to access |
| 2215 | * the MMD register via indirect method. |
| 2216 | */ |
| 2217 | int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) |
| 2218 | { |
| 2219 | return -EOPNOTSUPP; |
| 2220 | } |
| 2221 | EXPORT_SYMBOL(genphy_read_mmd_unsupported); |
| 2222 | |
| 2223 | int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, |
| 2224 | u16 regnum, u16 val) |
| 2225 | { |
| 2226 | return -EOPNOTSUPP; |
| 2227 | } |
| 2228 | EXPORT_SYMBOL(genphy_write_mmd_unsupported); |
| 2229 | |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 2230 | int genphy_suspend(struct phy_device *phydev) |
| 2231 | { |
Heiner Kallweit | 032f470 | 2018-01-12 21:20:36 +0100 | [diff] [blame] | 2232 | return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 2233 | } |
| 2234 | EXPORT_SYMBOL(genphy_suspend); |
| 2235 | |
| 2236 | int genphy_resume(struct phy_device *phydev) |
| 2237 | { |
Heiner Kallweit | 032f470 | 2018-01-12 21:20:36 +0100 | [diff] [blame] | 2238 | return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 2239 | } |
| 2240 | EXPORT_SYMBOL(genphy_resume); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2241 | |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 2242 | int genphy_loopback(struct phy_device *phydev, bool enable) |
| 2243 | { |
Russell King | f102852 | 2018-01-05 16:07:10 +0000 | [diff] [blame] | 2244 | return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 2245 | enable ? BMCR_LOOPBACK : 0); |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 2246 | } |
| 2247 | EXPORT_SYMBOL(genphy_loopback); |
| 2248 | |
Andrew Lunn | 41124fa | 2018-09-12 01:53:14 +0200 | [diff] [blame] | 2249 | /** |
| 2250 | * phy_remove_link_mode - Remove a supported link mode |
| 2251 | * @phydev: phy_device structure to remove link mode from |
| 2252 | * @link_mode: Link mode to be removed |
| 2253 | * |
| 2254 | * Description: Some MACs don't support all link modes which the PHY |
| 2255 | * does. e.g. a 1G MAC often does not support 1000Half. Add a helper |
| 2256 | * to remove a link mode. |
| 2257 | */ |
| 2258 | void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) |
| 2259 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2260 | linkmode_clear_bit(link_mode, phydev->supported); |
Heiner Kallweit | 22c0ef6 | 2019-05-01 21:34:43 +0200 | [diff] [blame] | 2261 | phy_advertise_supported(phydev); |
Andrew Lunn | 41124fa | 2018-09-12 01:53:14 +0200 | [diff] [blame] | 2262 | } |
| 2263 | EXPORT_SYMBOL(phy_remove_link_mode); |
| 2264 | |
Heiner Kallweit | 22c0ef6 | 2019-05-01 21:34:43 +0200 | [diff] [blame] | 2265 | static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src) |
| 2266 | { |
| 2267 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst, |
| 2268 | linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src)); |
| 2269 | linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst, |
| 2270 | linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src)); |
| 2271 | } |
| 2272 | |
| 2273 | /** |
| 2274 | * phy_advertise_supported - Advertise all supported modes |
| 2275 | * @phydev: target phy_device struct |
| 2276 | * |
| 2277 | * Description: Called to advertise all supported modes, doesn't touch |
| 2278 | * pause mode advertising. |
| 2279 | */ |
| 2280 | void phy_advertise_supported(struct phy_device *phydev) |
| 2281 | { |
| 2282 | __ETHTOOL_DECLARE_LINK_MODE_MASK(new); |
| 2283 | |
| 2284 | linkmode_copy(new, phydev->supported); |
| 2285 | phy_copy_pause_bits(new, phydev->advertising); |
| 2286 | linkmode_copy(phydev->advertising, new); |
| 2287 | } |
| 2288 | EXPORT_SYMBOL(phy_advertise_supported); |
| 2289 | |
Andrew Lunn | af8d9bb | 2018-09-12 01:53:15 +0200 | [diff] [blame] | 2290 | /** |
Andrew Lunn | c306ad3 | 2018-09-12 01:53:16 +0200 | [diff] [blame] | 2291 | * phy_support_sym_pause - Enable support of symmetrical pause |
| 2292 | * @phydev: target phy_device struct |
| 2293 | * |
| 2294 | * Description: Called by the MAC to indicate is supports symmetrical |
| 2295 | * Pause, but not asym pause. |
| 2296 | */ |
| 2297 | void phy_support_sym_pause(struct phy_device *phydev) |
| 2298 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2299 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); |
Heiner Kallweit | 22c0ef6 | 2019-05-01 21:34:43 +0200 | [diff] [blame] | 2300 | phy_copy_pause_bits(phydev->advertising, phydev->supported); |
Andrew Lunn | c306ad3 | 2018-09-12 01:53:16 +0200 | [diff] [blame] | 2301 | } |
| 2302 | EXPORT_SYMBOL(phy_support_sym_pause); |
| 2303 | |
| 2304 | /** |
Andrew Lunn | af8d9bb | 2018-09-12 01:53:15 +0200 | [diff] [blame] | 2305 | * phy_support_asym_pause - Enable support of asym pause |
| 2306 | * @phydev: target phy_device struct |
| 2307 | * |
| 2308 | * Description: Called by the MAC to indicate is supports Asym Pause. |
| 2309 | */ |
| 2310 | void phy_support_asym_pause(struct phy_device *phydev) |
| 2311 | { |
Heiner Kallweit | 22c0ef6 | 2019-05-01 21:34:43 +0200 | [diff] [blame] | 2312 | phy_copy_pause_bits(phydev->advertising, phydev->supported); |
Andrew Lunn | af8d9bb | 2018-09-12 01:53:15 +0200 | [diff] [blame] | 2313 | } |
| 2314 | EXPORT_SYMBOL(phy_support_asym_pause); |
| 2315 | |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2316 | /** |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2317 | * phy_set_sym_pause - Configure symmetric Pause |
| 2318 | * @phydev: target phy_device struct |
| 2319 | * @rx: Receiver Pause is supported |
| 2320 | * @tx: Transmit Pause is supported |
| 2321 | * @autoneg: Auto neg should be used |
| 2322 | * |
| 2323 | * Description: Configure advertised Pause support depending on if |
| 2324 | * receiver pause and pause auto neg is supported. Generally called |
| 2325 | * from the set_pauseparam .ndo. |
| 2326 | */ |
| 2327 | void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, |
| 2328 | bool autoneg) |
| 2329 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2330 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2331 | |
| 2332 | if (rx && tx && autoneg) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2333 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2334 | phydev->supported); |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2335 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2336 | linkmode_copy(phydev->advertising, phydev->supported); |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2337 | } |
| 2338 | EXPORT_SYMBOL(phy_set_sym_pause); |
| 2339 | |
| 2340 | /** |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2341 | * phy_set_asym_pause - Configure Pause and Asym Pause |
| 2342 | * @phydev: target phy_device struct |
| 2343 | * @rx: Receiver Pause is supported |
| 2344 | * @tx: Transmit Pause is supported |
| 2345 | * |
| 2346 | * Description: Configure advertised Pause support depending on if |
| 2347 | * transmit and receiver pause is supported. If there has been a |
| 2348 | * change in adverting, trigger a new autoneg. Generally called from |
| 2349 | * the set_pauseparam .ndo. |
| 2350 | */ |
| 2351 | void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) |
| 2352 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2353 | __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2354 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2355 | linkmode_copy(oldadv, phydev->advertising); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2356 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2357 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2358 | phydev->advertising); |
| 2359 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2360 | phydev->advertising); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2361 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2362 | if (rx) { |
| 2363 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2364 | phydev->advertising); |
| 2365 | linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2366 | phydev->advertising); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2367 | } |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2368 | |
| 2369 | if (tx) |
| 2370 | linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2371 | phydev->advertising); |
| 2372 | |
| 2373 | if (!linkmode_equal(oldadv, phydev->advertising) && |
| 2374 | phydev->autoneg) |
| 2375 | phy_start_aneg(phydev); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2376 | } |
| 2377 | EXPORT_SYMBOL(phy_set_asym_pause); |
| 2378 | |
Andrew Lunn | 22b7d29 | 2018-09-12 01:53:19 +0200 | [diff] [blame] | 2379 | /** |
| 2380 | * phy_validate_pause - Test if the PHY/MAC support the pause configuration |
| 2381 | * @phydev: phy_device struct |
| 2382 | * @pp: requested pause configuration |
| 2383 | * |
| 2384 | * Description: Test if the PHY/MAC combination supports the Pause |
| 2385 | * configuration the user is requesting. Returns True if it is |
| 2386 | * supported, false otherwise. |
| 2387 | */ |
| 2388 | bool phy_validate_pause(struct phy_device *phydev, |
| 2389 | struct ethtool_pauseparam *pp) |
| 2390 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2391 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
Heiner Kallweit | b4010af | 2019-05-01 21:54:28 +0200 | [diff] [blame] | 2392 | phydev->supported) && pp->rx_pause) |
Andrew Lunn | 22b7d29 | 2018-09-12 01:53:19 +0200 | [diff] [blame] | 2393 | return false; |
Heiner Kallweit | b4010af | 2019-05-01 21:54:28 +0200 | [diff] [blame] | 2394 | |
| 2395 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2396 | phydev->supported) && |
| 2397 | pp->rx_pause != pp->tx_pause) |
| 2398 | return false; |
| 2399 | |
Andrew Lunn | 22b7d29 | 2018-09-12 01:53:19 +0200 | [diff] [blame] | 2400 | return true; |
| 2401 | } |
| 2402 | EXPORT_SYMBOL(phy_validate_pause); |
| 2403 | |
Heiner Kallweit | 0d2e778 | 2018-11-09 18:16:28 +0100 | [diff] [blame] | 2404 | static bool phy_drv_supports_irq(struct phy_driver *phydrv) |
| 2405 | { |
Heiner Kallweit | a21ff3c | 2018-11-12 21:16:06 +0100 | [diff] [blame] | 2406 | return phydrv->config_intr && phydrv->ack_interrupt; |
Heiner Kallweit | 0d2e778 | 2018-11-09 18:16:28 +0100 | [diff] [blame] | 2407 | } |
| 2408 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2409 | /** |
| 2410 | * phy_probe - probe and init a PHY device |
| 2411 | * @dev: device to probe and init |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2412 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2413 | * Description: Take care of setting up the phy_device structure, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2414 | * set the state to READY (the driver's init function should |
| 2415 | * set it to STARTING if needed). |
| 2416 | */ |
| 2417 | static int phy_probe(struct device *dev) |
| 2418 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 2419 | struct phy_device *phydev = to_phy_device(dev); |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 2420 | struct device_driver *drv = phydev->mdio.dev.driver; |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 2421 | struct phy_driver *phydrv = to_phy_driver(drv); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2422 | int err = 0; |
| 2423 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2424 | phydev->drv = phydrv; |
| 2425 | |
Florian Fainelli | 2c7b492 | 2013-05-19 22:53:42 +0000 | [diff] [blame] | 2426 | /* Disable the interrupt if the PHY doesn't support it |
| 2427 | * but the interrupt is still a valid one |
| 2428 | */ |
Heiner Kallweit | 0d2e778 | 2018-11-09 18:16:28 +0100 | [diff] [blame] | 2429 | if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2430 | phydev->irq = PHY_POLL; |
| 2431 | |
Florian Fainelli | 4284b6a | 2013-05-23 01:11:12 +0000 | [diff] [blame] | 2432 | if (phydrv->flags & PHY_IS_INTERNAL) |
| 2433 | phydev->is_internal = true; |
| 2434 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2435 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2436 | |
Andrew Lunn | 92ed2eb | 2019-02-09 14:46:30 +0100 | [diff] [blame] | 2437 | if (phydev->drv->probe) { |
| 2438 | /* Deassert the reset signal */ |
| 2439 | phy_device_reset(phydev, 0); |
| 2440 | |
| 2441 | err = phydev->drv->probe(phydev); |
| 2442 | if (err) { |
| 2443 | /* Assert the reset signal */ |
| 2444 | phy_device_reset(phydev, 1); |
| 2445 | goto out; |
| 2446 | } |
| 2447 | } |
| 2448 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2449 | /* Start out supporting everything. Eventually, |
| 2450 | * a controller will attach, and may modify one |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 2451 | * or both of these values |
| 2452 | */ |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2453 | if (phydrv->features) { |
| 2454 | linkmode_copy(phydev->supported, phydrv->features); |
Heiner Kallweit | a1deab1 | 2019-04-03 23:14:33 +0200 | [diff] [blame] | 2455 | } else if (phydrv->get_features) { |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2456 | err = phydrv->get_features(phydev); |
Heiner Kallweit | a1deab1 | 2019-04-03 23:14:33 +0200 | [diff] [blame] | 2457 | } else if (phydev->is_c45) { |
| 2458 | err = genphy_c45_pma_read_abilities(phydev); |
| 2459 | } else { |
| 2460 | err = genphy_read_abilities(phydev); |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2461 | } |
| 2462 | |
Heiner Kallweit | a1deab1 | 2019-04-03 23:14:33 +0200 | [diff] [blame] | 2463 | if (err) |
| 2464 | goto out; |
| 2465 | |
Heiner Kallweit | 5e42574 | 2019-04-16 23:54:00 +0200 | [diff] [blame] | 2466 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, |
| 2467 | phydev->supported)) |
| 2468 | phydev->autoneg = 0; |
| 2469 | |
Heiner Kallweit | 3b8b11f | 2019-04-05 21:23:13 +0200 | [diff] [blame] | 2470 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 2471 | phydev->supported)) |
| 2472 | phydev->is_gigabit_capable = 1; |
| 2473 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 2474 | phydev->supported)) |
| 2475 | phydev->is_gigabit_capable = 1; |
| 2476 | |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 2477 | of_set_phy_supported(phydev); |
Heiner Kallweit | 22c0ef6 | 2019-05-01 21:34:43 +0200 | [diff] [blame] | 2478 | phy_advertise_supported(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2479 | |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 2480 | /* Get the EEE modes we want to prohibit. We will ask |
| 2481 | * the PHY stop advertising these mode later on |
| 2482 | */ |
| 2483 | of_set_phy_eee_broken(phydev); |
| 2484 | |
Timur Tabi | 529ed12 | 2016-12-07 13:20:51 -0600 | [diff] [blame] | 2485 | /* The Pause Frame bits indicate that the PHY can support passing |
| 2486 | * pause frames. During autonegotiation, the PHYs will determine if |
| 2487 | * they should allow pause frames to pass. The MAC driver should then |
| 2488 | * use that result to determine whether to enable flow control via |
| 2489 | * pause frames. |
| 2490 | * |
| 2491 | * Normally, PHY drivers should not set the Pause bits, and instead |
| 2492 | * allow phylib to do that. However, there may be some situations |
| 2493 | * (e.g. hardware erratum) where the driver wants to set only one |
| 2494 | * of these bits. |
| 2495 | */ |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2496 | if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && |
| 2497 | !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2498 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2499 | phydev->supported); |
| 2500 | linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2501 | phydev->supported); |
Timur Tabi | 529ed12 | 2016-12-07 13:20:51 -0600 | [diff] [blame] | 2502 | } |
| 2503 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2504 | /* Set the state to READY by default */ |
| 2505 | phydev->state = PHY_READY; |
| 2506 | |
Andrew Lunn | 92ed2eb | 2019-02-09 14:46:30 +0100 | [diff] [blame] | 2507 | out: |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2508 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2509 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2510 | return err; |
| 2511 | } |
| 2512 | |
| 2513 | static int phy_remove(struct device *dev) |
| 2514 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 2515 | struct phy_device *phydev = to_phy_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2516 | |
Florian Fainelli | 7b9a88a | 2017-02-17 16:07:33 -0800 | [diff] [blame] | 2517 | cancel_delayed_work_sync(&phydev->state_queue); |
| 2518 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2519 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2520 | phydev->state = PHY_DOWN; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2521 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2522 | |
Russell King | 298e54f | 2019-11-15 19:56:51 +0000 | [diff] [blame] | 2523 | sfp_bus_del_upstream(phydev->sfp_bus); |
| 2524 | phydev->sfp_bus = NULL; |
| 2525 | |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 2526 | if (phydev->drv && phydev->drv->remove) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2527 | phydev->drv->remove(phydev); |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 2528 | |
| 2529 | /* Assert the reset signal */ |
| 2530 | phy_device_reset(phydev, 1); |
| 2531 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2532 | phydev->drv = NULL; |
| 2533 | |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2537 | /** |
| 2538 | * phy_driver_register - register a phy_driver with the PHY layer |
| 2539 | * @new_driver: new phy_driver to register |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2540 | * @owner: module owning this PHY |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2541 | */ |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2542 | int phy_driver_register(struct phy_driver *new_driver, struct module *owner) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2543 | { |
| 2544 | int retval; |
| 2545 | |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2546 | /* Either the features are hard coded, or dynamically |
Heiner Kallweit | a1deab1 | 2019-04-03 23:14:33 +0200 | [diff] [blame] | 2547 | * determined. It cannot be both. |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2548 | */ |
Heiner Kallweit | a1deab1 | 2019-04-03 23:14:33 +0200 | [diff] [blame] | 2549 | if (WARN_ON(new_driver->features && new_driver->get_features)) { |
| 2550 | pr_err("%s: features and get_features must not both be set\n", |
| 2551 | new_driver->name); |
Camelia Groza | 3e64cf7 | 2019-01-17 14:22:36 +0200 | [diff] [blame] | 2552 | return -EINVAL; |
| 2553 | } |
| 2554 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 2555 | new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; |
| 2556 | new_driver->mdiodrv.driver.name = new_driver->name; |
| 2557 | new_driver->mdiodrv.driver.bus = &mdio_bus_type; |
| 2558 | new_driver->mdiodrv.driver.probe = phy_probe; |
| 2559 | new_driver->mdiodrv.driver.remove = phy_remove; |
| 2560 | new_driver->mdiodrv.driver.owner = owner; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2561 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 2562 | retval = driver_register(&new_driver->mdiodrv.driver); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2563 | if (retval) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 2564 | pr_err("%s: Error %d in registering driver\n", |
| 2565 | new_driver->name, retval); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2566 | |
| 2567 | return retval; |
| 2568 | } |
| 2569 | |
Olof Johansson | f2511f1 | 2007-11-04 16:09:23 -0600 | [diff] [blame] | 2570 | pr_debug("%s: Registered new driver\n", new_driver->name); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2571 | |
| 2572 | return 0; |
| 2573 | } |
| 2574 | EXPORT_SYMBOL(phy_driver_register); |
| 2575 | |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2576 | int phy_drivers_register(struct phy_driver *new_driver, int n, |
| 2577 | struct module *owner) |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2578 | { |
| 2579 | int i, ret = 0; |
| 2580 | |
| 2581 | for (i = 0; i < n; i++) { |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2582 | ret = phy_driver_register(new_driver + i, owner); |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2583 | if (ret) { |
| 2584 | while (i-- > 0) |
| 2585 | phy_driver_unregister(new_driver + i); |
| 2586 | break; |
| 2587 | } |
| 2588 | } |
| 2589 | return ret; |
| 2590 | } |
| 2591 | EXPORT_SYMBOL(phy_drivers_register); |
| 2592 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2593 | void phy_driver_unregister(struct phy_driver *drv) |
| 2594 | { |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 2595 | driver_unregister(&drv->mdiodrv.driver); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2596 | } |
| 2597 | EXPORT_SYMBOL(phy_driver_unregister); |
| 2598 | |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2599 | void phy_drivers_unregister(struct phy_driver *drv, int n) |
| 2600 | { |
| 2601 | int i; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 2602 | |
| 2603 | for (i = 0; i < n; i++) |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2604 | phy_driver_unregister(drv + i); |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2605 | } |
| 2606 | EXPORT_SYMBOL(phy_drivers_unregister); |
| 2607 | |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2608 | static struct phy_driver genphy_driver = { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2609 | .phy_id = 0xffffffff, |
| 2610 | .phy_id_mask = 0xffffffff, |
| 2611 | .name = "Generic PHY", |
Florian Fainelli | 0878fff | 2017-03-05 12:34:49 -0800 | [diff] [blame] | 2612 | .soft_reset = genphy_no_soft_reset, |
Heiner Kallweit | 2a4d867 | 2019-03-27 21:59:33 +0100 | [diff] [blame] | 2613 | .get_features = genphy_read_abilities, |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 2614 | .suspend = genphy_suspend, |
| 2615 | .resume = genphy_resume, |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 2616 | .set_loopback = genphy_loopback, |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2617 | }; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2618 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2619 | static int __init phy_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2620 | { |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2621 | int rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2622 | |
| 2623 | rc = mdio_bus_init(); |
| 2624 | if (rc) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2625 | return rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2626 | |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 2627 | features_init(); |
| 2628 | |
Heiner Kallweit | 22b56e8 | 2019-04-07 13:55:01 +0200 | [diff] [blame] | 2629 | rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2630 | if (rc) |
Heiner Kallweit | 22b56e8 | 2019-04-07 13:55:01 +0200 | [diff] [blame] | 2631 | goto err_c45; |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2632 | |
| 2633 | rc = phy_driver_register(&genphy_driver, THIS_MODULE); |
| 2634 | if (rc) { |
Heiner Kallweit | 22b56e8 | 2019-04-07 13:55:01 +0200 | [diff] [blame] | 2635 | phy_driver_unregister(&genphy_c45_driver); |
| 2636 | err_c45: |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2637 | mdio_bus_exit(); |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2638 | } |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2639 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2640 | return rc; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2641 | } |
| 2642 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2643 | static void __exit phy_exit(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2644 | { |
Heiner Kallweit | 22b56e8 | 2019-04-07 13:55:01 +0200 | [diff] [blame] | 2645 | phy_driver_unregister(&genphy_c45_driver); |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2646 | phy_driver_unregister(&genphy_driver); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2647 | mdio_bus_exit(); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2648 | } |
| 2649 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2650 | subsys_initcall(phy_init); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2651 | module_exit(phy_exit); |