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> |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 30 | #include <linux/mdio.h> |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 31 | #include <linux/io.h> |
| 32 | #include <linux/uaccess.h> |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 33 | #include <linux/of.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 | |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 60 | static const int phy_basic_ports_array[] = { |
| 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 | |
| 67 | static const int phy_fibre_port_array[] = { |
| 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 | |
| 72 | static const int phy_all_ports_features_array[] = { |
| 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, |
| 93 | ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 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; |
| 229 | extern struct phy_driver genphy_10g_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++) { |
| 492 | if (!(phydev->c45_ids.devices_in_package & (1 << i))) |
| 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 | |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 556 | static int phy_request_driver_module(struct phy_device *dev, int phy_id) |
| 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) { |
Heiner Kallweit | 13d0ab6 | 2019-01-16 08:07:38 +0100 | [diff] [blame] | 568 | phydev_err(dev, "error %d loading PHY driver module for ID 0x%08x\n", |
| 569 | ret, phy_id); |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 576 | struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 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 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 600 | dev->speed = 0; |
| 601 | dev->duplex = -1; |
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++) { |
| 636 | if (!(c45_ids->devices_in_package & (1 << i))) |
| 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 | { |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 816 | struct phy_c45_device_ids c45_ids = {0}; |
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 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 820 | r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 821 | if (r) |
| 822 | return ERR_PTR(r); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 823 | |
Giuseppe Cavallaro | 6436cbc | 2008-11-20 20:43:18 -0800 | [diff] [blame] | 824 | /* If the phy_id is mostly Fs, there is no device there */ |
| 825 | if ((phy_id & 0x1fffffff) == 0x1fffffff) |
Sergei Shtylyov | b74766a | 2016-04-24 20:25:23 +0300 | [diff] [blame] | 826 | return ERR_PTR(-ENODEV); |
Giuseppe Cavallaro | 6436cbc | 2008-11-20 20:43:18 -0800 | [diff] [blame] | 827 | |
Sergei Shtylyov | e62a768 | 2014-01-05 03:21:52 +0300 | [diff] [blame] | 828 | return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 829 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 830 | EXPORT_SYMBOL(get_phy_device); |
| 831 | |
| 832 | /** |
| 833 | * phy_device_register - Register the phy device on the MDIO bus |
Randy Dunlap | 1d4ac5d | 2009-06-16 16:56:33 +0000 | [diff] [blame] | 834 | * @phydev: phy_device structure to be added to the MDIO bus |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 835 | */ |
| 836 | int phy_device_register(struct phy_device *phydev) |
| 837 | { |
| 838 | int err; |
| 839 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 840 | err = mdiobus_register_device(&phydev->mdio); |
| 841 | if (err) |
| 842 | return err; |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 843 | |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 844 | /* Deassert the reset signal */ |
| 845 | phy_device_reset(phydev, 0); |
| 846 | |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 847 | /* Run all of the fixups for this PHY */ |
Florian Fainelli | d92f5de | 2014-07-28 16:28:07 -0700 | [diff] [blame] | 848 | err = phy_scan_fixups(phydev); |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 849 | if (err) { |
Heiner Kallweit | c3a6a17 | 2019-01-15 21:50:25 +0100 | [diff] [blame] | 850 | phydev_err(phydev, "failed to initialize\n"); |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 851 | goto out; |
| 852 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 853 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 854 | err = device_add(&phydev->mdio.dev); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 855 | if (err) { |
Heiner Kallweit | c3a6a17 | 2019-01-15 21:50:25 +0100 | [diff] [blame] | 856 | phydev_err(phydev, "failed to add\n"); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 857 | goto out; |
| 858 | } |
| 859 | |
| 860 | return 0; |
| 861 | |
| 862 | out: |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 863 | /* Assert the reset signal */ |
| 864 | phy_device_reset(phydev, 1); |
| 865 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 866 | mdiobus_unregister_device(&phydev->mdio); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 867 | return err; |
| 868 | } |
| 869 | EXPORT_SYMBOL(phy_device_register); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 870 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 871 | /** |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 872 | * phy_device_remove - Remove a previously registered phy device from the MDIO bus |
| 873 | * @phydev: phy_device structure to remove |
| 874 | * |
| 875 | * This doesn't free the phy_device itself, it merely reverses the effects |
| 876 | * of phy_device_register(). Use phy_device_free() to free the device |
| 877 | * after calling this function. |
| 878 | */ |
| 879 | void phy_device_remove(struct phy_device *phydev) |
| 880 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 881 | device_del(&phydev->mdio.dev); |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 882 | |
| 883 | /* Assert the reset signal */ |
| 884 | phy_device_reset(phydev, 1); |
| 885 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 886 | mdiobus_unregister_device(&phydev->mdio); |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 887 | } |
| 888 | EXPORT_SYMBOL(phy_device_remove); |
| 889 | |
| 890 | /** |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 891 | * phy_find_first - finds the first PHY device on the bus |
| 892 | * @bus: the target MII bus |
| 893 | */ |
| 894 | struct phy_device *phy_find_first(struct mii_bus *bus) |
| 895 | { |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 896 | struct phy_device *phydev; |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 897 | int addr; |
| 898 | |
| 899 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 900 | phydev = mdiobus_get_phy(bus, addr); |
| 901 | if (phydev) |
| 902 | return phydev; |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 903 | } |
| 904 | return NULL; |
| 905 | } |
| 906 | EXPORT_SYMBOL(phy_find_first); |
| 907 | |
Russell King | a81497b | 2017-07-25 15:02:58 +0100 | [diff] [blame] | 908 | static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier) |
| 909 | { |
| 910 | struct net_device *netdev = phydev->attached_dev; |
| 911 | |
| 912 | if (do_carrier) { |
| 913 | if (up) |
| 914 | netif_carrier_on(netdev); |
| 915 | else |
| 916 | netif_carrier_off(netdev); |
| 917 | } |
| 918 | phydev->adjust_link(netdev); |
| 919 | } |
| 920 | |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 921 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 922 | * phy_prepare_link - prepares the PHY layer to monitor link status |
| 923 | * @phydev: target phy_device struct |
| 924 | * @handler: callback function for link status change notifications |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 925 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 926 | * Description: Tells the PHY infrastructure to handle the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 927 | * gory details on monitoring link status (whether through |
| 928 | * polling or an interrupt), and to call back to the |
| 929 | * connected device driver when the link status changes. |
| 930 | * If you want to monitor your own link state, don't call |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 931 | * this function. |
| 932 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 933 | static void phy_prepare_link(struct phy_device *phydev, |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 934 | void (*handler)(struct net_device *)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 935 | { |
| 936 | phydev->adjust_link = handler; |
| 937 | } |
| 938 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 939 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 940 | * phy_connect_direct - connect an ethernet device to a specific phy_device |
| 941 | * @dev: the network device to connect |
| 942 | * @phydev: the pointer to the phy device |
| 943 | * @handler: callback function for state change notifications |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 944 | * @interface: PHY device's interface |
| 945 | */ |
| 946 | int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 947 | void (*handler)(struct net_device *), |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 948 | phy_interface_t interface) |
| 949 | { |
| 950 | int rc; |
| 951 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 952 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 953 | if (rc) |
| 954 | return rc; |
| 955 | |
| 956 | phy_prepare_link(phydev, handler); |
Heiner Kallweit | 434a431 | 2019-01-23 07:31:58 +0100 | [diff] [blame] | 957 | if (phy_interrupt_is_valid(phydev)) |
| 958 | phy_request_interrupt(phydev); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | EXPORT_SYMBOL(phy_connect_direct); |
| 963 | |
| 964 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 965 | * phy_connect - connect an ethernet device to a PHY device |
| 966 | * @dev: the network device to connect |
Randy Dunlap | 5d12b13 | 2008-04-28 10:58:22 -0700 | [diff] [blame] | 967 | * @bus_id: the id string of the PHY device to connect |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 968 | * @handler: callback function for state change notifications |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 969 | * @interface: PHY device's interface |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 970 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 971 | * Description: Convenience function for connecting ethernet |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 972 | * devices to PHY devices. The default behavior is for |
| 973 | * the PHY infrastructure to handle everything, and only notify |
| 974 | * the connected driver when the link status changes. If you |
| 975 | * don't want, or can't use the provided functionality, you may |
| 976 | * choose to call only the subset of functions which provide |
| 977 | * the desired functionality. |
| 978 | */ |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 979 | 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] | 980 | void (*handler)(struct net_device *), |
| 981 | phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 982 | { |
| 983 | struct phy_device *phydev; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 984 | struct device *d; |
| 985 | int rc; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 986 | |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 987 | /* Search the list of PHY devices on the mdio bus for the |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 988 | * PHY with the requested name |
| 989 | */ |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 990 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
| 991 | if (!d) { |
| 992 | pr_err("PHY %s not found\n", bus_id); |
| 993 | return ERR_PTR(-ENODEV); |
| 994 | } |
| 995 | phydev = to_phy_device(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 996 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 997 | rc = phy_connect_direct(dev, phydev, handler, interface); |
Johan Hovold | 17ae1c6 | 2016-11-03 18:40:19 +0100 | [diff] [blame] | 998 | put_device(d); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 999 | if (rc) |
| 1000 | return ERR_PTR(rc); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1001 | |
| 1002 | return phydev; |
| 1003 | } |
| 1004 | EXPORT_SYMBOL(phy_connect); |
| 1005 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1006 | /** |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1007 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY |
| 1008 | * device |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1009 | * @phydev: target phy_device struct |
| 1010 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1011 | void phy_disconnect(struct phy_device *phydev) |
| 1012 | { |
Heiner Kallweit | 472115d | 2019-01-17 20:07:54 +0100 | [diff] [blame] | 1013 | if (phy_is_started(phydev)) |
| 1014 | phy_stop(phydev); |
| 1015 | |
Heiner Kallweit | bb658ab | 2019-01-17 20:09:21 +0100 | [diff] [blame] | 1016 | if (phy_interrupt_is_valid(phydev)) |
| 1017 | free_irq(phydev->irq, phydev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1018 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1019 | phydev->adjust_link = NULL; |
| 1020 | |
| 1021 | phy_detach(phydev); |
| 1022 | } |
| 1023 | EXPORT_SYMBOL(phy_disconnect); |
| 1024 | |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1025 | /** |
| 1026 | * phy_poll_reset - Safely wait until a PHY reset has properly completed |
| 1027 | * @phydev: The PHY device to poll |
| 1028 | * |
| 1029 | * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as |
| 1030 | * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR |
| 1031 | * register must be polled until the BMCR_RESET bit clears. |
| 1032 | * |
| 1033 | * Furthermore, any attempts to write to PHY registers may have no effect |
| 1034 | * or even generate MDIO bus errors until this is complete. |
| 1035 | * |
| 1036 | * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the |
| 1037 | * standard and do not fully reset after the BMCR_RESET bit is set, and may |
| 1038 | * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an |
| 1039 | * effort to support such broken PHYs, this function is separate from the |
| 1040 | * standard phy_init_hw() which will zero all the other bits in the BMCR |
| 1041 | * and reapply all driver-specific and board-specific fixups. |
| 1042 | */ |
| 1043 | static int phy_poll_reset(struct phy_device *phydev) |
| 1044 | { |
| 1045 | /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ |
| 1046 | unsigned int retries = 12; |
| 1047 | int ret; |
| 1048 | |
| 1049 | do { |
| 1050 | msleep(50); |
| 1051 | ret = phy_read(phydev, MII_BMCR); |
| 1052 | if (ret < 0) |
| 1053 | return ret; |
| 1054 | } while (ret & BMCR_RESET && --retries); |
| 1055 | if (ret & BMCR_RESET) |
| 1056 | return -ETIMEDOUT; |
| 1057 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1058 | /* Some chips (smsc911x) may still need up to another 1ms after the |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1059 | * BMCR_RESET bit is cleared before they are usable. |
| 1060 | */ |
| 1061 | msleep(1); |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1065 | int phy_init_hw(struct phy_device *phydev) |
| 1066 | { |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1067 | int ret = 0; |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1068 | |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 1069 | /* Deassert the reset signal */ |
| 1070 | phy_device_reset(phydev, 0); |
| 1071 | |
Heiner Kallweit | a599698 | 2019-01-19 10:43:07 +0100 | [diff] [blame] | 1072 | if (!phydev->drv) |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1073 | return 0; |
| 1074 | |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1075 | if (phydev->drv->soft_reset) |
| 1076 | ret = phydev->drv->soft_reset(phydev); |
Florian Fainelli | 9df81dd | 2014-02-17 13:34:03 -0800 | [diff] [blame] | 1077 | |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1078 | if (ret < 0) |
| 1079 | return ret; |
| 1080 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1081 | ret = phy_scan_fixups(phydev); |
| 1082 | if (ret < 0) |
| 1083 | return ret; |
| 1084 | |
Heiner Kallweit | a599698 | 2019-01-19 10:43:07 +0100 | [diff] [blame] | 1085 | if (phydev->drv->config_init) |
| 1086 | ret = phydev->drv->config_init(phydev); |
| 1087 | |
| 1088 | return ret; |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1089 | } |
Florian Fainelli | 87aa9f9 | 2013-12-06 13:01:34 -0800 | [diff] [blame] | 1090 | EXPORT_SYMBOL(phy_init_hw); |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 1091 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1092 | void phy_attached_info(struct phy_device *phydev) |
| 1093 | { |
| 1094 | phy_attached_print(phydev, NULL); |
| 1095 | } |
| 1096 | EXPORT_SYMBOL(phy_attached_info); |
| 1097 | |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1098 | #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] | 1099 | void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) |
| 1100 | { |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1101 | const char *drv_name = phydev->drv ? phydev->drv->name : "unbound"; |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1102 | char *irq_str; |
Geert Uytterhoeven | 059fbe8 | 2017-09-21 13:27:02 +0200 | [diff] [blame] | 1103 | char irq_num[8]; |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1104 | |
| 1105 | switch(phydev->irq) { |
| 1106 | case PHY_POLL: |
| 1107 | irq_str = "POLL"; |
| 1108 | break; |
| 1109 | case PHY_IGNORE_INTERRUPT: |
| 1110 | irq_str = "IGNORE"; |
| 1111 | break; |
| 1112 | default: |
| 1113 | snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq); |
| 1114 | irq_str = irq_num; |
| 1115 | break; |
| 1116 | } |
| 1117 | |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1118 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1119 | if (!fmt) { |
Andrew Lunn | c4fabb8 | 2018-09-29 23:04:11 +0200 | [diff] [blame] | 1120 | phydev_info(phydev, ATTACHED_FMT "\n", |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1121 | drv_name, phydev_name(phydev), |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1122 | irq_str); |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1123 | } else { |
| 1124 | va_list ap; |
| 1125 | |
Andrew Lunn | c4fabb8 | 2018-09-29 23:04:11 +0200 | [diff] [blame] | 1126 | phydev_info(phydev, ATTACHED_FMT, |
Florian Fainelli | fcd03e3 | 2017-08-22 14:26:47 -0700 | [diff] [blame] | 1127 | drv_name, phydev_name(phydev), |
Romain Perier | 5e369ae | 2017-09-04 10:41:36 +0200 | [diff] [blame] | 1128 | irq_str); |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1129 | |
| 1130 | va_start(ap, fmt); |
| 1131 | vprintk(fmt, ap); |
| 1132 | va_end(ap); |
| 1133 | } |
| 1134 | } |
| 1135 | EXPORT_SYMBOL(phy_attached_print); |
| 1136 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1137 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1138 | * 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] | 1139 | * @dev: network device to attach |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1140 | * @phydev: Pointer to phy_device to attach |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1141 | * @flags: PHY device's dev_flags |
| 1142 | * @interface: PHY device's interface |
| 1143 | * |
| 1144 | * Description: Called by drivers to attach to a particular PHY |
| 1145 | * device. The phy_device is found, and properly hooked up |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1146 | * to the phy_driver. If no driver is attached, then a |
| 1147 | * generic driver is used. The phy_device is given a ptr to |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1148 | * the attaching device, and given a callback for link status |
| 1149 | * change. The phy_device is returned to the attaching driver. |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1150 | * This function takes a reference on the phy device. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1151 | */ |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1152 | int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
| 1153 | u32 flags, phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1154 | { |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1155 | struct module *ndev_owner = dev->dev.parent->driver->owner; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1156 | struct mii_bus *bus = phydev->mdio.bus; |
| 1157 | struct device *d = &phydev->mdio.dev; |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1158 | bool using_genphy = false; |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 1159 | int err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1160 | |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1161 | /* For Ethernet device drivers that register their own MDIO bus, we |
| 1162 | * will have bus->owner match ndev_mod, so we do not want to increment |
| 1163 | * our own module->refcnt here, otherwise we would not be able to |
| 1164 | * unload later on. |
| 1165 | */ |
| 1166 | if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1167 | dev_err(&dev->dev, "failed to get the bus module\n"); |
| 1168 | return -EIO; |
| 1169 | } |
| 1170 | |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1171 | get_device(d); |
| 1172 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1173 | /* Assume that if there is no driver, that it doesn't |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1174 | * exist, and we should use the genphy driver. |
| 1175 | */ |
Sergei Shtylyov | ef899c0 | 2015-08-28 21:35:14 +0300 | [diff] [blame] | 1176 | if (!d->driver) { |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1177 | if (phydev->is_c45) |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 1178 | d->driver = &genphy_10g_driver.mdiodrv.driver; |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1179 | else |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 1180 | d->driver = &genphy_driver.mdiodrv.driver; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1181 | |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1182 | using_genphy = true; |
| 1183 | } |
| 1184 | |
| 1185 | if (!try_module_get(d->driver->owner)) { |
| 1186 | dev_err(&dev->dev, "failed to get the device driver module\n"); |
| 1187 | err = -EIO; |
| 1188 | goto error_put_device; |
| 1189 | } |
| 1190 | |
| 1191 | if (using_genphy) { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1192 | err = d->driver->probe(d); |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 1193 | if (err >= 0) |
| 1194 | err = device_bind_driver(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1195 | |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 1196 | if (err) |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1197 | goto error_module_put; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | if (phydev->attached_dev) { |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1201 | dev_err(&dev->dev, "PHY already attached\n"); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1202 | err = -EBUSY; |
| 1203 | goto error; |
Ezequiel Garcia | b3565f2 | 2014-07-23 16:47:32 -0300 | [diff] [blame] | 1204 | } |
| 1205 | |
Russell King | a81497b | 2017-07-25 15:02:58 +0100 | [diff] [blame] | 1206 | phydev->phy_link_change = phy_link_change; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1207 | phydev->attached_dev = dev; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 1208 | dev->phydev = phydev; |
Florian Fainelli | a399546 | 2017-05-27 10:42:25 -0700 | [diff] [blame] | 1209 | |
| 1210 | /* Some Ethernet drivers try to connect to a PHY device before |
| 1211 | * calling register_netdevice() -> netdev_register_kobject() and |
| 1212 | * does the dev->dev.kobj initialization. Here we only check for |
| 1213 | * success which indicates that the network device kobject is |
| 1214 | * ready. Once we do that we still need to keep track of whether |
| 1215 | * links were successfully set up or not for phy_detach() to |
| 1216 | * remove them accordingly. |
| 1217 | */ |
| 1218 | phydev->sysfs_links = false; |
| 1219 | |
Florian Fainelli | 5568363 | 2017-05-25 09:21:41 -0700 | [diff] [blame] | 1220 | err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, |
| 1221 | "attached_dev"); |
Florian Fainelli | a399546 | 2017-05-27 10:42:25 -0700 | [diff] [blame] | 1222 | if (!err) { |
Grygorii Strashko | 4414b3e | 2018-03-16 17:08:35 -0500 | [diff] [blame] | 1223 | err = sysfs_create_link_nowarn(&dev->dev.kobj, |
| 1224 | &phydev->mdio.dev.kobj, |
| 1225 | "phydev"); |
| 1226 | if (err) { |
| 1227 | dev_err(&dev->dev, "could not add device link to %s err %d\n", |
| 1228 | kobject_name(&phydev->mdio.dev.kobj), |
| 1229 | err); |
| 1230 | /* non-fatal - some net drivers can use one netdevice |
| 1231 | * with more then one phy |
| 1232 | */ |
| 1233 | } |
Florian Fainelli | 5568363 | 2017-05-25 09:21:41 -0700 | [diff] [blame] | 1234 | |
Florian Fainelli | a399546 | 2017-05-27 10:42:25 -0700 | [diff] [blame] | 1235 | phydev->sysfs_links = true; |
| 1236 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1237 | |
| 1238 | phydev->dev_flags = flags; |
| 1239 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 1240 | phydev->interface = interface; |
| 1241 | |
Anton Vorontsov | ef24b16 | 2010-08-24 14:46:12 -0700 | [diff] [blame] | 1242 | phydev->state = PHY_READY; |
| 1243 | |
Sjoerd Simons | 113c74d | 2016-01-14 21:57:18 +0100 | [diff] [blame] | 1244 | /* Initial carrier state is off as the phy is about to be |
| 1245 | * (re)initialized. |
| 1246 | */ |
| 1247 | netif_carrier_off(phydev->attached_dev); |
| 1248 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 1249 | /* Do initial configuration here, now that |
| 1250 | * we have certain key parameters |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1251 | * (dev_flags and interface) |
| 1252 | */ |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 1253 | err = phy_init_hw(phydev); |
| 1254 | if (err) |
Woojung Huh | a7dac9f | 2016-11-23 23:10:33 +0000 | [diff] [blame] | 1255 | goto error; |
Sebastian Hesselbarth | 1211ce5 | 2013-12-13 10:20:28 +0100 | [diff] [blame] | 1256 | |
Woojung Huh | a7dac9f | 2016-11-23 23:10:33 +0000 | [diff] [blame] | 1257 | phy_resume(phydev); |
Zach Brown | 2e0bc45 | 2016-10-17 10:49:55 -0500 | [diff] [blame] | 1258 | phy_led_triggers_register(phydev); |
| 1259 | |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 1260 | return err; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1261 | |
| 1262 | error: |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1263 | /* phy_detach() does all of the cleanup below */ |
Woojung Huh | a7dac9f | 2016-11-23 23:10:33 +0000 | [diff] [blame] | 1264 | phy_detach(phydev); |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1265 | return err; |
| 1266 | |
| 1267 | error_module_put: |
Mao Wenan | cafe8df | 2017-01-31 18:46:43 -0800 | [diff] [blame] | 1268 | module_put(d->driver->owner); |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1269 | error_put_device: |
| 1270 | put_device(d); |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1271 | if (ndev_owner != bus->owner) |
| 1272 | module_put(bus->owner); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1273 | return err; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1274 | } |
Andy Fleming | 257184d | 2014-01-10 14:27:54 +0800 | [diff] [blame] | 1275 | EXPORT_SYMBOL(phy_attach_direct); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1276 | |
| 1277 | /** |
| 1278 | * phy_attach - attach a network device to a particular PHY device |
| 1279 | * @dev: network device to attach |
| 1280 | * @bus_id: Bus ID of PHY device to attach |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1281 | * @interface: PHY device's interface |
| 1282 | * |
| 1283 | * Description: Same as phy_attach_direct() except that a PHY bus_id |
| 1284 | * string is passed instead of a pointer to a struct phy_device. |
| 1285 | */ |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1286 | struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, |
| 1287 | phy_interface_t interface) |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1288 | { |
| 1289 | struct bus_type *bus = &mdio_bus_type; |
| 1290 | struct phy_device *phydev; |
| 1291 | struct device *d; |
| 1292 | int rc; |
| 1293 | |
| 1294 | /* Search the list of PHY devices on the mdio bus for the |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1295 | * PHY with the requested name |
| 1296 | */ |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1297 | d = bus_find_device_by_name(bus, NULL, bus_id); |
| 1298 | if (!d) { |
| 1299 | pr_err("PHY %s not found\n", bus_id); |
| 1300 | return ERR_PTR(-ENODEV); |
| 1301 | } |
| 1302 | phydev = to_phy_device(d); |
| 1303 | |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 1304 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
Johan Hovold | 17ae1c6 | 2016-11-03 18:40:19 +0100 | [diff] [blame] | 1305 | put_device(d); |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 1306 | if (rc) |
| 1307 | return ERR_PTR(rc); |
| 1308 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1309 | return phydev; |
| 1310 | } |
| 1311 | EXPORT_SYMBOL(phy_attach); |
| 1312 | |
Florian Fainelli | 5db5ea9 | 2019-01-15 15:09:35 -0800 | [diff] [blame] | 1313 | static bool phy_driver_is_genphy_kind(struct phy_device *phydev, |
| 1314 | struct device_driver *driver) |
| 1315 | { |
| 1316 | struct device *d = &phydev->mdio.dev; |
| 1317 | bool ret = false; |
| 1318 | |
| 1319 | if (!phydev->drv) |
| 1320 | return ret; |
| 1321 | |
| 1322 | get_device(d); |
| 1323 | ret = d->driver == driver; |
| 1324 | put_device(d); |
| 1325 | |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | bool phy_driver_is_genphy(struct phy_device *phydev) |
| 1330 | { |
| 1331 | return phy_driver_is_genphy_kind(phydev, |
| 1332 | &genphy_driver.mdiodrv.driver); |
| 1333 | } |
| 1334 | EXPORT_SYMBOL_GPL(phy_driver_is_genphy); |
| 1335 | |
| 1336 | bool phy_driver_is_genphy_10g(struct phy_device *phydev) |
| 1337 | { |
| 1338 | return phy_driver_is_genphy_kind(phydev, |
| 1339 | &genphy_10g_driver.mdiodrv.driver); |
| 1340 | } |
| 1341 | EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g); |
| 1342 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1343 | /** |
| 1344 | * phy_detach - detach a PHY device from its network device |
| 1345 | * @phydev: target phy_device struct |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1346 | * |
| 1347 | * This detaches the phy device from its network device and the phy |
| 1348 | * driver, and drops the reference count taken in phy_attach_direct(). |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1349 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1350 | void phy_detach(struct phy_device *phydev) |
| 1351 | { |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1352 | struct net_device *dev = phydev->attached_dev; |
| 1353 | struct module *ndev_owner = dev->dev.parent->driver->owner; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1354 | struct mii_bus *bus; |
Ezequiel Garcia | b3565f2 | 2014-07-23 16:47:32 -0300 | [diff] [blame] | 1355 | |
Florian Fainelli | a399546 | 2017-05-27 10:42:25 -0700 | [diff] [blame] | 1356 | if (phydev->sysfs_links) { |
| 1357 | sysfs_remove_link(&dev->dev.kobj, "phydev"); |
| 1358 | sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); |
| 1359 | } |
Heiner Kallweit | 93f41e6 | 2018-09-24 22:01:32 +0200 | [diff] [blame] | 1360 | phy_suspend(phydev); |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 1361 | phydev->attached_dev->phydev = NULL; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1362 | phydev->attached_dev = NULL; |
Russell King | 9525ae8 | 2017-07-25 15:03:13 +0100 | [diff] [blame] | 1363 | phydev->phylink = NULL; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1364 | |
Geert Uytterhoeven | 0075bd6 | 2016-11-28 15:18:31 +0100 | [diff] [blame] | 1365 | phy_led_triggers_unregister(phydev); |
| 1366 | |
Florian Fainelli | 6d9f66a | 2017-02-08 19:05:26 -0800 | [diff] [blame] | 1367 | module_put(phydev->mdio.dev.driver->owner); |
| 1368 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1369 | /* If the device had no specific driver before (i.e. - it |
| 1370 | * was using the generic driver), we unbind the device |
| 1371 | * from the generic driver so that there's a chance a |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1372 | * real driver could be loaded |
| 1373 | */ |
Florian Fainelli | 5db5ea9 | 2019-01-15 15:09:35 -0800 | [diff] [blame] | 1374 | if (phy_driver_is_genphy(phydev) || |
| 1375 | phy_driver_is_genphy_10g(phydev)) |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 1376 | device_release_driver(&phydev->mdio.dev); |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1377 | |
Russell King | 7322967 | 2015-09-24 20:36:08 +0100 | [diff] [blame] | 1378 | /* |
| 1379 | * The phydev might go away on the put_device() below, so avoid |
| 1380 | * a use-after-free bug by reading the underlying bus first. |
| 1381 | */ |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1382 | bus = phydev->mdio.bus; |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 1383 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1384 | put_device(&phydev->mdio.dev); |
Florian Fainelli | ec988ad | 2016-12-06 20:54:43 -0800 | [diff] [blame] | 1385 | if (ndev_owner != bus->owner) |
| 1386 | module_put(bus->owner); |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* Assert the reset signal */ |
| 1389 | phy_device_reset(phydev, 1); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1390 | } |
| 1391 | EXPORT_SYMBOL(phy_detach); |
| 1392 | |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1393 | int phy_suspend(struct phy_device *phydev) |
| 1394 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1395 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
Heiner Kallweit | 93f41e6 | 2018-09-24 22:01:32 +0200 | [diff] [blame] | 1396 | struct net_device *netdev = phydev->attached_dev; |
Sebastian Hesselbarth | 32fc3fd | 2014-03-14 10:07:44 +0100 | [diff] [blame] | 1397 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1398 | int ret = 0; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1399 | |
| 1400 | /* If the device has WOL enabled, we cannot suspend the PHY */ |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1401 | phy_ethtool_get_wol(phydev, &wol); |
Heiner Kallweit | 93f41e6 | 2018-09-24 22:01:32 +0200 | [diff] [blame] | 1402 | if (wol.wolopts || (netdev && netdev->wol_enabled)) |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1403 | return -EBUSY; |
| 1404 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1405 | if (phydev->drv && phydrv->suspend) |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1406 | ret = phydrv->suspend(phydev); |
| 1407 | |
| 1408 | if (ret) |
| 1409 | return ret; |
| 1410 | |
| 1411 | phydev->suspended = true; |
| 1412 | |
| 1413 | return ret; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1414 | } |
Florian Fainelli | ca12769 | 2014-07-08 10:38:36 -0700 | [diff] [blame] | 1415 | EXPORT_SYMBOL(phy_suspend); |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1416 | |
Andrew Lunn | 9c2c2e6 | 2018-02-27 01:56:06 +0100 | [diff] [blame] | 1417 | int __phy_resume(struct phy_device *phydev) |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1418 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 1419 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1420 | int ret = 0; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1421 | |
Russell King | f5e6403 | 2017-12-12 10:45:36 +0000 | [diff] [blame] | 1422 | WARN_ON(!mutex_is_locked(&phydev->lock)); |
| 1423 | |
Florian Fainelli | 25149ef | 2017-02-17 16:07:34 -0800 | [diff] [blame] | 1424 | if (phydev->drv && phydrv->resume) |
Florian Fainelli | 8a477a6 | 2015-01-26 22:05:39 -0800 | [diff] [blame] | 1425 | ret = phydrv->resume(phydev); |
| 1426 | |
| 1427 | if (ret) |
| 1428 | return ret; |
| 1429 | |
| 1430 | phydev->suspended = false; |
| 1431 | |
| 1432 | return ret; |
Sebastian Hesselbarth | 481b5d9 | 2013-12-13 10:20:27 +0100 | [diff] [blame] | 1433 | } |
Andrew Lunn | 9c2c2e6 | 2018-02-27 01:56:06 +0100 | [diff] [blame] | 1434 | EXPORT_SYMBOL(__phy_resume); |
| 1435 | |
| 1436 | int phy_resume(struct phy_device *phydev) |
| 1437 | { |
| 1438 | int ret; |
| 1439 | |
| 1440 | mutex_lock(&phydev->lock); |
| 1441 | ret = __phy_resume(phydev); |
| 1442 | mutex_unlock(&phydev->lock); |
| 1443 | |
| 1444 | return ret; |
| 1445 | } |
Florian Fainelli | ca12769 | 2014-07-08 10:38:36 -0700 | [diff] [blame] | 1446 | EXPORT_SYMBOL(phy_resume); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1447 | |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 1448 | int phy_loopback(struct phy_device *phydev, bool enable) |
| 1449 | { |
| 1450 | struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver); |
| 1451 | int ret = 0; |
| 1452 | |
| 1453 | mutex_lock(&phydev->lock); |
| 1454 | |
| 1455 | if (enable && phydev->loopback_enabled) { |
| 1456 | ret = -EBUSY; |
| 1457 | goto out; |
| 1458 | } |
| 1459 | |
| 1460 | if (!enable && !phydev->loopback_enabled) { |
| 1461 | ret = -EINVAL; |
| 1462 | goto out; |
| 1463 | } |
| 1464 | |
| 1465 | if (phydev->drv && phydrv->set_loopback) |
| 1466 | ret = phydrv->set_loopback(phydev, enable); |
| 1467 | else |
| 1468 | ret = -EOPNOTSUPP; |
| 1469 | |
| 1470 | if (ret) |
| 1471 | goto out; |
| 1472 | |
| 1473 | phydev->loopback_enabled = enable; |
| 1474 | |
| 1475 | out: |
| 1476 | mutex_unlock(&phydev->lock); |
| 1477 | return ret; |
| 1478 | } |
| 1479 | EXPORT_SYMBOL(phy_loopback); |
| 1480 | |
Richard Leitner | a966849 | 2017-12-11 13:16:58 +0100 | [diff] [blame] | 1481 | /** |
| 1482 | * phy_reset_after_clk_enable - perform a PHY reset if needed |
| 1483 | * @phydev: target phy_device struct |
| 1484 | * |
| 1485 | * Description: Some PHYs are known to need a reset after their refclk was |
| 1486 | * enabled. This function evaluates the flags and perform the reset if it's |
| 1487 | * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy |
| 1488 | * was reset. |
| 1489 | */ |
| 1490 | int phy_reset_after_clk_enable(struct phy_device *phydev) |
| 1491 | { |
| 1492 | if (!phydev || !phydev->drv) |
| 1493 | return -ENODEV; |
| 1494 | |
| 1495 | if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { |
| 1496 | phy_device_reset(phydev, 1); |
| 1497 | phy_device_reset(phydev, 0); |
| 1498 | return 1; |
| 1499 | } |
| 1500 | |
| 1501 | return 0; |
| 1502 | } |
| 1503 | EXPORT_SYMBOL(phy_reset_after_clk_enable); |
| 1504 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1505 | /* Generic PHY support and helper functions */ |
| 1506 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1507 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1508 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1509 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1510 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1511 | * Description: Writes MII_ADVERTISE with the appropriate values, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1512 | * after sanitizing the values to make sure we only advertise |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1513 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 1514 | * hasn't changed, and > 0 if it has changed. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1515 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 1516 | static int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1517 | { |
| 1518 | u32 advertise; |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame^] | 1519 | int bmsr, adv; |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1520 | int err, changed = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1521 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1522 | /* Only allow advertising what this PHY supports */ |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1523 | linkmode_and(phydev->advertising, phydev->advertising, |
| 1524 | phydev->supported); |
| 1525 | if (!ethtool_convert_link_mode_to_legacy_u32(&advertise, |
| 1526 | phydev->advertising)) |
| 1527 | phydev_warn(phydev, "PHY advertising (%*pb) more modes than genphy supports, some modes not advertised.\n", |
| 1528 | __ETHTOOL_LINK_MODE_MASK_NBITS, |
| 1529 | phydev->advertising); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1530 | |
| 1531 | /* Setup standard advertisement */ |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame^] | 1532 | err = phy_modify_changed(phydev, MII_ADVERTISE, |
| 1533 | ADVERTISE_ALL | ADVERTISE_100BASE4 | |
| 1534 | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, |
| 1535 | ethtool_adv_to_mii_adv_t(advertise)); |
| 1536 | if (err < 0) |
| 1537 | return err; |
| 1538 | if (err > 0) |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1539 | changed = 1; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1540 | |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1541 | bmsr = phy_read(phydev, MII_BMSR); |
| 1542 | if (bmsr < 0) |
| 1543 | return bmsr; |
| 1544 | |
| 1545 | /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all |
| 1546 | * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a |
| 1547 | * logical 1. |
| 1548 | */ |
| 1549 | if (!(bmsr & BMSR_ESTATEN)) |
| 1550 | return changed; |
| 1551 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1552 | /* Configure gigabit if it's supported */ |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame^] | 1553 | adv = 0; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1554 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 1555 | phydev->supported) || |
| 1556 | linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 1557 | phydev->supported)) |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame^] | 1558 | adv = ethtool_adv_to_mii_ctrl1000_t(advertise); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1559 | |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame^] | 1560 | err = phy_modify_changed(phydev, MII_CTRL1000, |
| 1561 | ADVERTISE_1000FULL | ADVERTISE_1000HALF, |
| 1562 | adv); |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1563 | if (err < 0) |
| 1564 | return err; |
Heiner Kallweit | 4f9744e | 2019-02-10 19:59:57 +0100 | [diff] [blame^] | 1565 | if (err > 0) |
| 1566 | changed = 1; |
Florian Fainelli | 5273e3a | 2014-02-03 12:35:46 -0800 | [diff] [blame] | 1567 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 1568 | return changed; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1569 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1570 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1571 | /** |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1572 | * genphy_config_eee_advert - disable unwanted eee mode advertisement |
| 1573 | * @phydev: target phy_device struct |
| 1574 | * |
| 1575 | * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy |
| 1576 | * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't |
| 1577 | * changed, and 1 if it has changed. |
| 1578 | */ |
| 1579 | static int genphy_config_eee_advert(struct phy_device *phydev) |
| 1580 | { |
jbrunet | 3bb9ab6 | 2016-12-19 16:05:36 +0100 | [diff] [blame] | 1581 | int broken = phydev->eee_broken_modes; |
| 1582 | int old_adv, adv; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1583 | |
| 1584 | /* Nothing to disable */ |
| 1585 | if (!broken) |
| 1586 | return 0; |
| 1587 | |
| 1588 | /* If the following call fails, we assume that EEE is not |
| 1589 | * supported by the phy. If we read 0, EEE is not advertised |
| 1590 | * In both case, we don't need to continue |
| 1591 | */ |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1592 | adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1593 | if (adv <= 0) |
| 1594 | return 0; |
| 1595 | |
| 1596 | old_adv = adv; |
| 1597 | adv &= ~broken; |
| 1598 | |
| 1599 | /* Advertising remains unchanged with the broken mask */ |
| 1600 | if (old_adv == adv) |
| 1601 | return 0; |
| 1602 | |
Russell King | a6d99fc | 2017-03-21 16:36:53 +0000 | [diff] [blame] | 1603 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv); |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1604 | |
| 1605 | return 1; |
| 1606 | } |
| 1607 | |
| 1608 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1609 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 1610 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1611 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1612 | * Description: Configures MII_BMCR to force speed/duplex |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1613 | * to the values in phydev. Assumes that the values are valid. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1614 | * Please see phy_sanitize_settings(). |
| 1615 | */ |
Madalin Bucur | 3fb69bc | 2013-11-20 16:38:19 -0600 | [diff] [blame] | 1616 | int genphy_setup_forced(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1617 | { |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 1618 | u16 ctl = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1619 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1620 | phydev->pause = 0; |
| 1621 | phydev->asym_pause = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1622 | |
| 1623 | if (SPEED_1000 == phydev->speed) |
| 1624 | ctl |= BMCR_SPEED1000; |
| 1625 | else if (SPEED_100 == phydev->speed) |
| 1626 | ctl |= BMCR_SPEED100; |
| 1627 | |
| 1628 | if (DUPLEX_FULL == phydev->duplex) |
| 1629 | ctl |= BMCR_FULLDPLX; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1630 | |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 1631 | return phy_modify(phydev, MII_BMCR, |
Ingo van Lil | 18a5b05 | 2018-02-12 12:02:52 +0100 | [diff] [blame] | 1632 | ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1633 | } |
Madalin Bucur | 3fb69bc | 2013-11-20 16:38:19 -0600 | [diff] [blame] | 1634 | EXPORT_SYMBOL(genphy_setup_forced); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1635 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1636 | /** |
| 1637 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 1638 | * @phydev: target phy_device struct |
| 1639 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1640 | int genphy_restart_aneg(struct phy_device *phydev) |
| 1641 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1642 | /* Don't isolate the PHY if we're negotiating */ |
Russell King | f102852 | 2018-01-05 16:07:10 +0000 | [diff] [blame] | 1643 | return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 1644 | BMCR_ANENABLE | BMCR_ANRESTART); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1645 | } |
Adrian Bunk | 892871d | 2008-10-13 18:48:09 -0700 | [diff] [blame] | 1646 | EXPORT_SYMBOL(genphy_restart_aneg); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1647 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1648 | /** |
| 1649 | * genphy_config_aneg - restart auto-negotiation or write BMCR |
| 1650 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1651 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1652 | * Description: If auto-negotiation is enabled, we configure the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1653 | * advertising, and then restart auto-negotiation. If it is not |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1654 | * enabled, then we write the BMCR. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1655 | */ |
| 1656 | int genphy_config_aneg(struct phy_device *phydev) |
| 1657 | { |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1658 | int err, changed; |
| 1659 | |
| 1660 | changed = genphy_config_eee_advert(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1661 | |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1662 | if (AUTONEG_ENABLE != phydev->autoneg) |
| 1663 | return genphy_setup_forced(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1664 | |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1665 | err = genphy_config_advert(phydev); |
| 1666 | if (err < 0) /* error */ |
| 1667 | return err; |
| 1668 | |
| 1669 | changed |= err; |
| 1670 | |
| 1671 | if (changed == 0) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1672 | /* Advertisement hasn't changed, but maybe aneg was never on to |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1673 | * begin with? Or maybe phy was isolated? |
| 1674 | */ |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1675 | int ctl = phy_read(phydev, MII_BMCR); |
| 1676 | |
| 1677 | if (ctl < 0) |
| 1678 | return ctl; |
| 1679 | |
| 1680 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1681 | changed = 1; /* do restart aneg */ |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /* Only restart aneg if we are advertising something different |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1685 | * than we were before. |
| 1686 | */ |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1687 | if (changed > 0) |
| 1688 | return genphy_restart_aneg(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1689 | |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 1690 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1691 | } |
| 1692 | EXPORT_SYMBOL(genphy_config_aneg); |
| 1693 | |
Florian Fainelli | a9fa6e6 | 2014-02-11 17:27:36 -0800 | [diff] [blame] | 1694 | /** |
| 1695 | * genphy_aneg_done - return auto-negotiation status |
| 1696 | * @phydev: target phy_device struct |
| 1697 | * |
| 1698 | * Description: Reads the status register and returns 0 either if |
| 1699 | * auto-negotiation is incomplete, or if there was an error. |
| 1700 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. |
| 1701 | */ |
| 1702 | int genphy_aneg_done(struct phy_device *phydev) |
| 1703 | { |
| 1704 | int retval = phy_read(phydev, MII_BMSR); |
| 1705 | |
| 1706 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); |
| 1707 | } |
| 1708 | EXPORT_SYMBOL(genphy_aneg_done); |
| 1709 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1710 | /** |
| 1711 | * genphy_update_link - update link status in @phydev |
| 1712 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1713 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1714 | * Description: Update the value in phydev->link to reflect the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1715 | * current link value. In order to do this, we need to read |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1716 | * the status register twice, keeping the second value. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1717 | */ |
| 1718 | int genphy_update_link(struct phy_device *phydev) |
| 1719 | { |
| 1720 | int status; |
| 1721 | |
Heiner Kallweit | 93c0970 | 2019-02-06 19:39:52 +0100 | [diff] [blame] | 1722 | /* The link state is latched low so that momentary link |
| 1723 | * drops can be detected. Do not double-read the status |
| 1724 | * in polling mode to detect such short link drops. |
| 1725 | */ |
| 1726 | if (!phy_polling_mode(phydev)) { |
| 1727 | status = phy_read(phydev, MII_BMSR); |
Heiner Kallweit | c397ab2 | 2019-02-07 20:22:20 +0100 | [diff] [blame] | 1728 | if (status < 0) { |
Heiner Kallweit | 93c0970 | 2019-02-06 19:39:52 +0100 | [diff] [blame] | 1729 | return status; |
Heiner Kallweit | c397ab2 | 2019-02-07 20:22:20 +0100 | [diff] [blame] | 1730 | } else if (status & BMSR_LSTATUS) { |
| 1731 | phydev->link = 1; |
| 1732 | return 0; |
| 1733 | } |
Heiner Kallweit | 93c0970 | 2019-02-06 19:39:52 +0100 | [diff] [blame] | 1734 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1735 | |
| 1736 | /* Read link and autonegotiation status */ |
| 1737 | status = phy_read(phydev, MII_BMSR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1738 | if (status < 0) |
| 1739 | return status; |
| 1740 | |
| 1741 | if ((status & BMSR_LSTATUS) == 0) |
| 1742 | phydev->link = 0; |
| 1743 | else |
| 1744 | phydev->link = 1; |
| 1745 | |
| 1746 | return 0; |
| 1747 | } |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 1748 | EXPORT_SYMBOL(genphy_update_link); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1749 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1750 | /** |
| 1751 | * genphy_read_status - check the link status and update current link state |
| 1752 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1753 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 1754 | * Description: Check the link, then figure out the current state |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1755 | * by comparing what we advertise with what the link partner |
| 1756 | * advertises. Start by checking the gigabit possibilities, |
| 1757 | * then move on to 10/100. |
| 1758 | */ |
| 1759 | int genphy_read_status(struct phy_device *phydev) |
| 1760 | { |
| 1761 | int adv; |
| 1762 | int err; |
| 1763 | int lpa; |
| 1764 | int lpagb = 0; |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1765 | int common_adv; |
| 1766 | int common_adv_gb = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1767 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1768 | /* Update the link, but return if there was an error */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1769 | err = genphy_update_link(phydev); |
| 1770 | if (err) |
| 1771 | return err; |
| 1772 | |
Andrew Lunn | c0ec3c2 | 2018-11-10 23:43:34 +0100 | [diff] [blame] | 1773 | linkmode_zero(phydev->lp_advertising); |
Florian Fainelli | 114002b | 2013-12-06 13:01:30 -0800 | [diff] [blame] | 1774 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1775 | if (AUTONEG_ENABLE == phydev->autoneg) { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1776 | if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 1777 | phydev->supported) || |
| 1778 | linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 1779 | phydev->supported)) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1780 | lpagb = phy_read(phydev, MII_STAT1000); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1781 | if (lpagb < 0) |
| 1782 | return lpagb; |
| 1783 | |
| 1784 | adv = phy_read(phydev, MII_CTRL1000); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1785 | if (adv < 0) |
| 1786 | return adv; |
| 1787 | |
Heiner Kallweit | b8f8c8e | 2018-07-21 15:48:47 +0200 | [diff] [blame] | 1788 | if (lpagb & LPA_1000MSFAIL) { |
| 1789 | if (adv & CTL1000_ENABLE_MASTER) |
| 1790 | phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); |
| 1791 | else |
| 1792 | phydev_err(phydev, "Master/Slave resolution failed\n"); |
| 1793 | return -ENOLINK; |
| 1794 | } |
| 1795 | |
Andrew Lunn | 78a24df | 2018-12-05 21:49:41 +0100 | [diff] [blame] | 1796 | mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, |
| 1797 | lpagb); |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1798 | common_adv_gb = lpagb & adv << 2; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | lpa = phy_read(phydev, MII_LPA); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1802 | if (lpa < 0) |
| 1803 | return lpa; |
| 1804 | |
Andrew Lunn | d335193 | 2018-12-05 21:49:43 +0100 | [diff] [blame] | 1805 | mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); |
Florian Fainelli | 114002b | 2013-12-06 13:01:30 -0800 | [diff] [blame] | 1806 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1807 | adv = phy_read(phydev, MII_ADVERTISE); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1808 | if (adv < 0) |
| 1809 | return adv; |
| 1810 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1811 | common_adv = lpa & adv; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1812 | |
| 1813 | phydev->speed = SPEED_10; |
| 1814 | phydev->duplex = DUPLEX_HALF; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1815 | phydev->pause = 0; |
| 1816 | phydev->asym_pause = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1817 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1818 | if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1819 | phydev->speed = SPEED_1000; |
| 1820 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1821 | if (common_adv_gb & LPA_1000FULL) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1822 | phydev->duplex = DUPLEX_FULL; |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1823 | } else if (common_adv & (LPA_100FULL | LPA_100HALF)) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1824 | phydev->speed = SPEED_100; |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1825 | |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1826 | if (common_adv & LPA_100FULL) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1827 | phydev->duplex = DUPLEX_FULL; |
| 1828 | } else |
Cristian Bercaru | a4572e0 | 2014-02-25 10:22:48 +0200 | [diff] [blame] | 1829 | if (common_adv & LPA_10FULL) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1830 | phydev->duplex = DUPLEX_FULL; |
| 1831 | |
Florian Fainelli | e109374 | 2013-12-17 21:38:12 -0800 | [diff] [blame] | 1832 | if (phydev->duplex == DUPLEX_FULL) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1833 | phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; |
| 1834 | phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; |
| 1835 | } |
| 1836 | } else { |
| 1837 | int bmcr = phy_read(phydev, MII_BMCR); |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1838 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1839 | if (bmcr < 0) |
| 1840 | return bmcr; |
| 1841 | |
| 1842 | if (bmcr & BMCR_FULLDPLX) |
| 1843 | phydev->duplex = DUPLEX_FULL; |
| 1844 | else |
| 1845 | phydev->duplex = DUPLEX_HALF; |
| 1846 | |
| 1847 | if (bmcr & BMCR_SPEED1000) |
| 1848 | phydev->speed = SPEED_1000; |
| 1849 | else if (bmcr & BMCR_SPEED100) |
| 1850 | phydev->speed = SPEED_100; |
| 1851 | else |
| 1852 | phydev->speed = SPEED_10; |
| 1853 | |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 1854 | phydev->pause = 0; |
| 1855 | phydev->asym_pause = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | return 0; |
| 1859 | } |
| 1860 | EXPORT_SYMBOL(genphy_read_status); |
| 1861 | |
Florian Fainelli | 797ac07 | 2014-02-17 13:34:02 -0800 | [diff] [blame] | 1862 | /** |
| 1863 | * genphy_soft_reset - software reset the PHY via BMCR_RESET bit |
| 1864 | * @phydev: target phy_device struct |
| 1865 | * |
| 1866 | * Description: Perform a software PHY reset using the standard |
| 1867 | * BMCR_RESET bit and poll for the reset bit to be cleared. |
| 1868 | * |
| 1869 | * Returns: 0 on success, < 0 on failure |
| 1870 | */ |
| 1871 | int genphy_soft_reset(struct phy_device *phydev) |
| 1872 | { |
| 1873 | int ret; |
| 1874 | |
| 1875 | ret = phy_write(phydev, MII_BMCR, BMCR_RESET); |
| 1876 | if (ret < 0) |
| 1877 | return ret; |
| 1878 | |
| 1879 | return phy_poll_reset(phydev); |
| 1880 | } |
| 1881 | EXPORT_SYMBOL(genphy_soft_reset); |
| 1882 | |
Daniel Mack | af6b696 | 2014-04-16 17:19:12 +0200 | [diff] [blame] | 1883 | int genphy_config_init(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1884 | { |
Eric Sesterhenn | 84c22d7 | 2006-09-25 16:39:22 -0700 | [diff] [blame] | 1885 | int val; |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1886 | __ETHTOOL_DECLARE_LINK_MODE_MASK(features) = { 0, }; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1887 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1888 | linkmode_set_bit_array(phy_basic_ports_array, |
| 1889 | ARRAY_SIZE(phy_basic_ports_array), |
| 1890 | features); |
| 1891 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, features); |
| 1892 | linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1893 | |
| 1894 | /* Do we support autonegotiation? */ |
| 1895 | val = phy_read(phydev, MII_BMSR); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1896 | if (val < 0) |
| 1897 | return val; |
| 1898 | |
| 1899 | if (val & BMSR_ANEGCAPABLE) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1900 | linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1901 | |
| 1902 | if (val & BMSR_100FULL) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1903 | linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1904 | if (val & BMSR_100HALF) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1905 | linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1906 | if (val & BMSR_10FULL) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1907 | linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1908 | if (val & BMSR_10HALF) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1909 | linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1910 | |
| 1911 | if (val & BMSR_ESTATEN) { |
| 1912 | val = phy_read(phydev, MII_ESTATUS); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1913 | if (val < 0) |
| 1914 | return val; |
| 1915 | |
| 1916 | if (val & ESTATUS_1000_TFULL) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1917 | linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 1918 | features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1919 | if (val & ESTATUS_1000_THALF) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1920 | linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 1921 | features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1922 | } |
| 1923 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1924 | linkmode_and(phydev->supported, phydev->supported, features); |
| 1925 | linkmode_and(phydev->advertising, phydev->advertising, features); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1926 | |
| 1927 | return 0; |
| 1928 | } |
Uwe Kleine-König | a0a32d3 | 2017-05-24 00:26:07 +0200 | [diff] [blame] | 1929 | EXPORT_SYMBOL(genphy_config_init); |
Andy Fleming | 124059f | 2014-01-10 14:27:37 +0800 | [diff] [blame] | 1930 | |
Kevin Hao | 5df7af8 | 2018-03-20 09:44:52 +0800 | [diff] [blame] | 1931 | /* This is used for the phy device which doesn't support the MMD extended |
| 1932 | * register access, but it does have side effect when we are trying to access |
| 1933 | * the MMD register via indirect method. |
| 1934 | */ |
| 1935 | int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) |
| 1936 | { |
| 1937 | return -EOPNOTSUPP; |
| 1938 | } |
| 1939 | EXPORT_SYMBOL(genphy_read_mmd_unsupported); |
| 1940 | |
| 1941 | int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, |
| 1942 | u16 regnum, u16 val) |
| 1943 | { |
| 1944 | return -EOPNOTSUPP; |
| 1945 | } |
| 1946 | EXPORT_SYMBOL(genphy_write_mmd_unsupported); |
| 1947 | |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1948 | int genphy_suspend(struct phy_device *phydev) |
| 1949 | { |
Heiner Kallweit | 032f470 | 2018-01-12 21:20:36 +0100 | [diff] [blame] | 1950 | return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1951 | } |
| 1952 | EXPORT_SYMBOL(genphy_suspend); |
| 1953 | |
| 1954 | int genphy_resume(struct phy_device *phydev) |
| 1955 | { |
Heiner Kallweit | 032f470 | 2018-01-12 21:20:36 +0100 | [diff] [blame] | 1956 | return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1957 | } |
| 1958 | EXPORT_SYMBOL(genphy_resume); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1959 | |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 1960 | int genphy_loopback(struct phy_device *phydev, bool enable) |
| 1961 | { |
Russell King | f102852 | 2018-01-05 16:07:10 +0000 | [diff] [blame] | 1962 | return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, |
Russell King | fea23fb | 2018-01-02 10:58:58 +0000 | [diff] [blame] | 1963 | enable ? BMCR_LOOPBACK : 0); |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 1964 | } |
| 1965 | EXPORT_SYMBOL(genphy_loopback); |
| 1966 | |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 1967 | static int __set_phy_supported(struct phy_device *phydev, u32 max_speed) |
| 1968 | { |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 1969 | switch (max_speed) { |
Heiner Kallweit | 6915bf3 | 2018-12-03 08:04:57 +0100 | [diff] [blame] | 1970 | case SPEED_10: |
| 1971 | linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, |
| 1972 | phydev->supported); |
| 1973 | linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 1974 | phydev->supported); |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 1975 | /* fall through */ |
| 1976 | case SPEED_100: |
Heiner Kallweit | 6915bf3 | 2018-12-03 08:04:57 +0100 | [diff] [blame] | 1977 | linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, |
| 1978 | phydev->supported); |
| 1979 | linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, |
| 1980 | phydev->supported); |
| 1981 | break; |
| 1982 | case SPEED_1000: |
| 1983 | break; |
| 1984 | default: |
| 1985 | return -ENOTSUPP; |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
| 1991 | int phy_set_max_speed(struct phy_device *phydev, u32 max_speed) |
| 1992 | { |
| 1993 | int err; |
| 1994 | |
| 1995 | err = __set_phy_supported(phydev, max_speed); |
| 1996 | if (err) |
| 1997 | return err; |
| 1998 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 1999 | linkmode_copy(phydev->advertising, phydev->supported); |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 2000 | |
| 2001 | return 0; |
| 2002 | } |
| 2003 | EXPORT_SYMBOL(phy_set_max_speed); |
| 2004 | |
Andrew Lunn | 41124fa | 2018-09-12 01:53:14 +0200 | [diff] [blame] | 2005 | /** |
| 2006 | * phy_remove_link_mode - Remove a supported link mode |
| 2007 | * @phydev: phy_device structure to remove link mode from |
| 2008 | * @link_mode: Link mode to be removed |
| 2009 | * |
| 2010 | * Description: Some MACs don't support all link modes which the PHY |
| 2011 | * does. e.g. a 1G MAC often does not support 1000Half. Add a helper |
| 2012 | * to remove a link mode. |
| 2013 | */ |
| 2014 | void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) |
| 2015 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2016 | linkmode_clear_bit(link_mode, phydev->supported); |
| 2017 | linkmode_copy(phydev->advertising, phydev->supported); |
Andrew Lunn | 41124fa | 2018-09-12 01:53:14 +0200 | [diff] [blame] | 2018 | } |
| 2019 | EXPORT_SYMBOL(phy_remove_link_mode); |
| 2020 | |
Andrew Lunn | af8d9bb | 2018-09-12 01:53:15 +0200 | [diff] [blame] | 2021 | /** |
Andrew Lunn | c306ad3 | 2018-09-12 01:53:16 +0200 | [diff] [blame] | 2022 | * phy_support_sym_pause - Enable support of symmetrical pause |
| 2023 | * @phydev: target phy_device struct |
| 2024 | * |
| 2025 | * Description: Called by the MAC to indicate is supports symmetrical |
| 2026 | * Pause, but not asym pause. |
| 2027 | */ |
| 2028 | void phy_support_sym_pause(struct phy_device *phydev) |
| 2029 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2030 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); |
| 2031 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); |
| 2032 | linkmode_copy(phydev->advertising, phydev->supported); |
Andrew Lunn | c306ad3 | 2018-09-12 01:53:16 +0200 | [diff] [blame] | 2033 | } |
| 2034 | EXPORT_SYMBOL(phy_support_sym_pause); |
| 2035 | |
| 2036 | /** |
Andrew Lunn | af8d9bb | 2018-09-12 01:53:15 +0200 | [diff] [blame] | 2037 | * phy_support_asym_pause - Enable support of asym pause |
| 2038 | * @phydev: target phy_device struct |
| 2039 | * |
| 2040 | * Description: Called by the MAC to indicate is supports Asym Pause. |
| 2041 | */ |
| 2042 | void phy_support_asym_pause(struct phy_device *phydev) |
| 2043 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2044 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); |
| 2045 | linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); |
| 2046 | linkmode_copy(phydev->advertising, phydev->supported); |
Andrew Lunn | af8d9bb | 2018-09-12 01:53:15 +0200 | [diff] [blame] | 2047 | } |
| 2048 | EXPORT_SYMBOL(phy_support_asym_pause); |
| 2049 | |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2050 | /** |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2051 | * phy_set_sym_pause - Configure symmetric Pause |
| 2052 | * @phydev: target phy_device struct |
| 2053 | * @rx: Receiver Pause is supported |
| 2054 | * @tx: Transmit Pause is supported |
| 2055 | * @autoneg: Auto neg should be used |
| 2056 | * |
| 2057 | * Description: Configure advertised Pause support depending on if |
| 2058 | * receiver pause and pause auto neg is supported. Generally called |
| 2059 | * from the set_pauseparam .ndo. |
| 2060 | */ |
| 2061 | void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, |
| 2062 | bool autoneg) |
| 2063 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2064 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2065 | |
| 2066 | if (rx && tx && autoneg) |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2067 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2068 | phydev->supported); |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2069 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2070 | linkmode_copy(phydev->advertising, phydev->supported); |
Andrew Lunn | 0c12240 | 2018-09-12 01:53:18 +0200 | [diff] [blame] | 2071 | } |
| 2072 | EXPORT_SYMBOL(phy_set_sym_pause); |
| 2073 | |
| 2074 | /** |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2075 | * phy_set_asym_pause - Configure Pause and Asym Pause |
| 2076 | * @phydev: target phy_device struct |
| 2077 | * @rx: Receiver Pause is supported |
| 2078 | * @tx: Transmit Pause is supported |
| 2079 | * |
| 2080 | * Description: Configure advertised Pause support depending on if |
| 2081 | * transmit and receiver pause is supported. If there has been a |
| 2082 | * change in adverting, trigger a new autoneg. Generally called from |
| 2083 | * the set_pauseparam .ndo. |
| 2084 | */ |
| 2085 | void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) |
| 2086 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2087 | __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2088 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2089 | linkmode_copy(oldadv, phydev->advertising); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2090 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2091 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2092 | phydev->advertising); |
| 2093 | linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2094 | phydev->advertising); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2095 | |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2096 | if (rx) { |
| 2097 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2098 | phydev->advertising); |
| 2099 | linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2100 | phydev->advertising); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2101 | } |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2102 | |
| 2103 | if (tx) |
| 2104 | linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2105 | phydev->advertising); |
| 2106 | |
| 2107 | if (!linkmode_equal(oldadv, phydev->advertising) && |
| 2108 | phydev->autoneg) |
| 2109 | phy_start_aneg(phydev); |
Andrew Lunn | 70814e8 | 2018-09-12 01:53:17 +0200 | [diff] [blame] | 2110 | } |
| 2111 | EXPORT_SYMBOL(phy_set_asym_pause); |
| 2112 | |
Andrew Lunn | 22b7d29 | 2018-09-12 01:53:19 +0200 | [diff] [blame] | 2113 | /** |
| 2114 | * phy_validate_pause - Test if the PHY/MAC support the pause configuration |
| 2115 | * @phydev: phy_device struct |
| 2116 | * @pp: requested pause configuration |
| 2117 | * |
| 2118 | * Description: Test if the PHY/MAC combination supports the Pause |
| 2119 | * configuration the user is requesting. Returns True if it is |
| 2120 | * supported, false otherwise. |
| 2121 | */ |
| 2122 | bool phy_validate_pause(struct phy_device *phydev, |
| 2123 | struct ethtool_pauseparam *pp) |
| 2124 | { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2125 | if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2126 | phydev->supported) || |
| 2127 | (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2128 | phydev->supported) && |
Andrew Lunn | 22b7d29 | 2018-09-12 01:53:19 +0200 | [diff] [blame] | 2129 | pp->rx_pause != pp->tx_pause)) |
| 2130 | return false; |
| 2131 | return true; |
| 2132 | } |
| 2133 | EXPORT_SYMBOL(phy_validate_pause); |
| 2134 | |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 2135 | static void of_set_phy_supported(struct phy_device *phydev) |
| 2136 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 2137 | struct device_node *node = phydev->mdio.dev.of_node; |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 2138 | u32 max_speed; |
| 2139 | |
| 2140 | if (!IS_ENABLED(CONFIG_OF_MDIO)) |
| 2141 | return; |
| 2142 | |
| 2143 | if (!node) |
| 2144 | return; |
| 2145 | |
Simon Horman | f3a6bd3 | 2015-09-30 15:15:52 +0900 | [diff] [blame] | 2146 | if (!of_property_read_u32(node, "max-speed", &max_speed)) |
| 2147 | __set_phy_supported(phydev, max_speed); |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 2148 | } |
| 2149 | |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 2150 | static void of_set_phy_eee_broken(struct phy_device *phydev) |
| 2151 | { |
| 2152 | struct device_node *node = phydev->mdio.dev.of_node; |
jbrunet | 57f3986 | 2016-12-19 16:05:37 +0100 | [diff] [blame] | 2153 | u32 broken = 0; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 2154 | |
| 2155 | if (!IS_ENABLED(CONFIG_OF_MDIO)) |
| 2156 | return; |
| 2157 | |
| 2158 | if (!node) |
| 2159 | return; |
| 2160 | |
jbrunet | 57f3986 | 2016-12-19 16:05:37 +0100 | [diff] [blame] | 2161 | if (of_property_read_bool(node, "eee-broken-100tx")) |
| 2162 | broken |= MDIO_EEE_100TX; |
| 2163 | if (of_property_read_bool(node, "eee-broken-1000t")) |
| 2164 | broken |= MDIO_EEE_1000T; |
| 2165 | if (of_property_read_bool(node, "eee-broken-10gt")) |
| 2166 | broken |= MDIO_EEE_10GT; |
| 2167 | if (of_property_read_bool(node, "eee-broken-1000kx")) |
| 2168 | broken |= MDIO_EEE_1000KX; |
| 2169 | if (of_property_read_bool(node, "eee-broken-10gkx4")) |
| 2170 | broken |= MDIO_EEE_10GKX4; |
| 2171 | if (of_property_read_bool(node, "eee-broken-10gkr")) |
| 2172 | broken |= MDIO_EEE_10GKR; |
| 2173 | |
| 2174 | phydev->eee_broken_modes = broken; |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 2175 | } |
| 2176 | |
Heiner Kallweit | 0d2e778 | 2018-11-09 18:16:28 +0100 | [diff] [blame] | 2177 | static bool phy_drv_supports_irq(struct phy_driver *phydrv) |
| 2178 | { |
Heiner Kallweit | a21ff3c | 2018-11-12 21:16:06 +0100 | [diff] [blame] | 2179 | return phydrv->config_intr && phydrv->ack_interrupt; |
Heiner Kallweit | 0d2e778 | 2018-11-09 18:16:28 +0100 | [diff] [blame] | 2180 | } |
| 2181 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2182 | /** |
| 2183 | * phy_probe - probe and init a PHY device |
| 2184 | * @dev: device to probe and init |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2185 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2186 | * Description: Take care of setting up the phy_device structure, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2187 | * set the state to READY (the driver's init function should |
| 2188 | * set it to STARTING if needed). |
| 2189 | */ |
| 2190 | static int phy_probe(struct device *dev) |
| 2191 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 2192 | struct phy_device *phydev = to_phy_device(dev); |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 2193 | struct device_driver *drv = phydev->mdio.dev.driver; |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 2194 | struct phy_driver *phydrv = to_phy_driver(drv); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2195 | int err = 0; |
| 2196 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2197 | phydev->drv = phydrv; |
| 2198 | |
Florian Fainelli | 2c7b492 | 2013-05-19 22:53:42 +0000 | [diff] [blame] | 2199 | /* Disable the interrupt if the PHY doesn't support it |
| 2200 | * but the interrupt is still a valid one |
| 2201 | */ |
Heiner Kallweit | 0d2e778 | 2018-11-09 18:16:28 +0100 | [diff] [blame] | 2202 | if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2203 | phydev->irq = PHY_POLL; |
| 2204 | |
Florian Fainelli | 4284b6a | 2013-05-23 01:11:12 +0000 | [diff] [blame] | 2205 | if (phydrv->flags & PHY_IS_INTERNAL) |
| 2206 | phydev->is_internal = true; |
| 2207 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2208 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2209 | |
Andrew Lunn | 92ed2eb | 2019-02-09 14:46:30 +0100 | [diff] [blame] | 2210 | if (phydev->drv->probe) { |
| 2211 | /* Deassert the reset signal */ |
| 2212 | phy_device_reset(phydev, 0); |
| 2213 | |
| 2214 | err = phydev->drv->probe(phydev); |
| 2215 | if (err) { |
| 2216 | /* Assert the reset signal */ |
| 2217 | phy_device_reset(phydev, 1); |
| 2218 | goto out; |
| 2219 | } |
| 2220 | } |
| 2221 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2222 | /* Start out supporting everything. Eventually, |
| 2223 | * a controller will attach, and may modify one |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 2224 | * or both of these values |
| 2225 | */ |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2226 | if (phydrv->features) { |
| 2227 | linkmode_copy(phydev->supported, phydrv->features); |
| 2228 | } else { |
| 2229 | err = phydrv->get_features(phydev); |
| 2230 | if (err) |
| 2231 | goto out; |
| 2232 | } |
| 2233 | |
Sascha Hauer | de906af | 2014-05-21 15:29:45 +0200 | [diff] [blame] | 2234 | of_set_phy_supported(phydev); |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2235 | linkmode_copy(phydev->advertising, phydev->supported); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2236 | |
jbrunet | d853d14 | 2016-11-28 10:46:46 +0100 | [diff] [blame] | 2237 | /* Get the EEE modes we want to prohibit. We will ask |
| 2238 | * the PHY stop advertising these mode later on |
| 2239 | */ |
| 2240 | of_set_phy_eee_broken(phydev); |
| 2241 | |
Timur Tabi | 529ed12 | 2016-12-07 13:20:51 -0600 | [diff] [blame] | 2242 | /* The Pause Frame bits indicate that the PHY can support passing |
| 2243 | * pause frames. During autonegotiation, the PHYs will determine if |
| 2244 | * they should allow pause frames to pass. The MAC driver should then |
| 2245 | * use that result to determine whether to enable flow control via |
| 2246 | * pause frames. |
| 2247 | * |
| 2248 | * Normally, PHY drivers should not set the Pause bits, and instead |
| 2249 | * allow phylib to do that. However, there may be some situations |
| 2250 | * (e.g. hardware erratum) where the driver wants to set only one |
| 2251 | * of these bits. |
| 2252 | */ |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2253 | if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && |
| 2254 | !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { |
Andrew Lunn | 3c1bcc8 | 2018-11-10 23:43:33 +0100 | [diff] [blame] | 2255 | linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, |
| 2256 | phydev->supported); |
| 2257 | linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, |
| 2258 | phydev->supported); |
Timur Tabi | 529ed12 | 2016-12-07 13:20:51 -0600 | [diff] [blame] | 2259 | } |
| 2260 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2261 | /* Set the state to READY by default */ |
| 2262 | phydev->state = PHY_READY; |
| 2263 | |
Andrew Lunn | 92ed2eb | 2019-02-09 14:46:30 +0100 | [diff] [blame] | 2264 | out: |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2265 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2266 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2267 | return err; |
| 2268 | } |
| 2269 | |
| 2270 | static int phy_remove(struct device *dev) |
| 2271 | { |
Sergei Shtylyov | 553fe92 | 2014-01-05 03:23:19 +0300 | [diff] [blame] | 2272 | struct phy_device *phydev = to_phy_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2273 | |
Florian Fainelli | 7b9a88a | 2017-02-17 16:07:33 -0800 | [diff] [blame] | 2274 | cancel_delayed_work_sync(&phydev->state_queue); |
| 2275 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2276 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2277 | phydev->state = PHY_DOWN; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 2278 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2279 | |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 2280 | if (phydev->drv && phydev->drv->remove) { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2281 | phydev->drv->remove(phydev); |
Sergei Shtylyov | bafbdd5 | 2017-12-04 13:35:05 +0100 | [diff] [blame] | 2282 | |
| 2283 | /* Assert the reset signal */ |
| 2284 | phy_device_reset(phydev, 1); |
| 2285 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2286 | phydev->drv = NULL; |
| 2287 | |
| 2288 | return 0; |
| 2289 | } |
| 2290 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2291 | /** |
| 2292 | * phy_driver_register - register a phy_driver with the PHY layer |
| 2293 | * @new_driver: new phy_driver to register |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2294 | * @owner: module owning this PHY |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 2295 | */ |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2296 | int phy_driver_register(struct phy_driver *new_driver, struct module *owner) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2297 | { |
| 2298 | int retval; |
| 2299 | |
Andrew Lunn | efbdfdc | 2019-02-09 15:24:47 +0100 | [diff] [blame] | 2300 | /* Either the features are hard coded, or dynamically |
| 2301 | * determine. It cannot be both or neither |
| 2302 | */ |
| 2303 | if (WARN_ON((!new_driver->features && !new_driver->get_features) || |
| 2304 | (new_driver->features && new_driver->get_features))) { |
Camelia Groza | 3e64cf7 | 2019-01-17 14:22:36 +0200 | [diff] [blame] | 2305 | pr_err("%s: Driver features are missing\n", new_driver->name); |
| 2306 | return -EINVAL; |
| 2307 | } |
| 2308 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 2309 | new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; |
| 2310 | new_driver->mdiodrv.driver.name = new_driver->name; |
| 2311 | new_driver->mdiodrv.driver.bus = &mdio_bus_type; |
| 2312 | new_driver->mdiodrv.driver.probe = phy_probe; |
| 2313 | new_driver->mdiodrv.driver.remove = phy_remove; |
| 2314 | new_driver->mdiodrv.driver.owner = owner; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2315 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 2316 | retval = driver_register(&new_driver->mdiodrv.driver); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2317 | if (retval) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 2318 | pr_err("%s: Error %d in registering driver\n", |
| 2319 | new_driver->name, retval); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2320 | |
| 2321 | return retval; |
| 2322 | } |
| 2323 | |
Olof Johansson | f2511f1 | 2007-11-04 16:09:23 -0600 | [diff] [blame] | 2324 | pr_debug("%s: Registered new driver\n", new_driver->name); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2325 | |
| 2326 | return 0; |
| 2327 | } |
| 2328 | EXPORT_SYMBOL(phy_driver_register); |
| 2329 | |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2330 | int phy_drivers_register(struct phy_driver *new_driver, int n, |
| 2331 | struct module *owner) |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2332 | { |
| 2333 | int i, ret = 0; |
| 2334 | |
| 2335 | for (i = 0; i < n; i++) { |
Andrew Lunn | be01da7 | 2016-01-06 20:11:22 +0100 | [diff] [blame] | 2336 | ret = phy_driver_register(new_driver + i, owner); |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2337 | if (ret) { |
| 2338 | while (i-- > 0) |
| 2339 | phy_driver_unregister(new_driver + i); |
| 2340 | break; |
| 2341 | } |
| 2342 | } |
| 2343 | return ret; |
| 2344 | } |
| 2345 | EXPORT_SYMBOL(phy_drivers_register); |
| 2346 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2347 | void phy_driver_unregister(struct phy_driver *drv) |
| 2348 | { |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame] | 2349 | driver_unregister(&drv->mdiodrv.driver); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2350 | } |
| 2351 | EXPORT_SYMBOL(phy_driver_unregister); |
| 2352 | |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2353 | void phy_drivers_unregister(struct phy_driver *drv, int n) |
| 2354 | { |
| 2355 | int i; |
Sergei Shtylyov | 2f53e90 | 2014-01-05 03:17:06 +0300 | [diff] [blame] | 2356 | |
| 2357 | for (i = 0; i < n; i++) |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2358 | phy_driver_unregister(drv + i); |
Christian Hohnstaedt | d5bf907 | 2012-07-04 05:44:34 +0000 | [diff] [blame] | 2359 | } |
| 2360 | EXPORT_SYMBOL(phy_drivers_unregister); |
| 2361 | |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2362 | static struct phy_driver genphy_driver = { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2363 | .phy_id = 0xffffffff, |
| 2364 | .phy_id_mask = 0xffffffff, |
| 2365 | .name = "Generic PHY", |
Florian Fainelli | 0878fff | 2017-03-05 12:34:49 -0800 | [diff] [blame] | 2366 | .soft_reset = genphy_no_soft_reset, |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2367 | .config_init = genphy_config_init, |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 2368 | .features = PHY_GBIT_ALL_PORTS_FEATURES, |
Florian Fainelli | 76a423a | 2014-02-11 17:27:37 -0800 | [diff] [blame] | 2369 | .aneg_done = genphy_aneg_done, |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 2370 | .suspend = genphy_suspend, |
| 2371 | .resume = genphy_resume, |
Lin Yun Sheng | f0f9b4e | 2017-06-30 17:44:15 +0800 | [diff] [blame] | 2372 | .set_loopback = genphy_loopback, |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2373 | }; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2374 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2375 | static int __init phy_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2376 | { |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2377 | int rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2378 | |
| 2379 | rc = mdio_bus_init(); |
| 2380 | if (rc) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2381 | return rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2382 | |
Andrew Lunn | 719655a | 2018-09-29 23:04:16 +0200 | [diff] [blame] | 2383 | features_init(); |
| 2384 | |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2385 | rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2386 | if (rc) |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2387 | goto err_10g; |
| 2388 | |
| 2389 | rc = phy_driver_register(&genphy_driver, THIS_MODULE); |
| 2390 | if (rc) { |
| 2391 | phy_driver_unregister(&genphy_10g_driver); |
| 2392 | err_10g: |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2393 | mdio_bus_exit(); |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2394 | } |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2395 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2396 | return rc; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2397 | } |
| 2398 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2399 | static void __exit phy_exit(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2400 | { |
Russell King | 921690f | 2017-06-05 12:23:05 +0100 | [diff] [blame] | 2401 | phy_driver_unregister(&genphy_10g_driver); |
| 2402 | phy_driver_unregister(&genphy_driver); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2403 | mdio_bus_exit(); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 2404 | } |
| 2405 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 2406 | subsys_initcall(phy_init); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 2407 | module_exit(phy_exit); |