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