Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Generic PHY Management code |
| 4 | * |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 5 | * Copyright 2011 Freescale Semiconductor, Inc. |
| 6 | * author Andy Fleming |
| 7 | * |
| 8 | * Based loosely off of Linux's PHY Lib |
| 9 | */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Simon Glass | c74c8e6 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | #include <net.h> |
| 16 | #include <command.h> |
| 17 | #include <miiphy.h> |
| 18 | #include <phy.h> |
| 19 | #include <errno.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 20 | #include <asm/global_data.h> |
Marek Vasut | e99a6ef | 2024-03-18 15:57:01 +0100 | [diff] [blame] | 21 | #include <asm-generic/gpio.h> |
| 22 | #include <dm/device_compat.h> |
Bin Meng | 676fbd3 | 2021-03-14 20:14:52 +0800 | [diff] [blame] | 23 | #include <dm/of_extra.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 25 | #include <linux/delay.h> |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 26 | #include <linux/err.h> |
Shengzhou Liu | 597fe04 | 2014-04-11 16:14:17 +0800 | [diff] [blame] | 27 | #include <linux/compiler.h> |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 28 | |
Michal Simek | abbfcbe | 2015-05-13 13:40:40 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 31 | /* Generic PHY support and helper functions */ |
| 32 | |
| 33 | /** |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 34 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 35 | * @phydev: target phy_device struct |
| 36 | * |
| 37 | * Description: Writes MII_ADVERTISE with the appropriate values, |
| 38 | * after sanitizing the values to make sure we only advertise |
| 39 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 40 | * hasn't changed, and > 0 if it has changed. |
| 41 | */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 42 | static int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 43 | { |
| 44 | u32 advertise; |
Florian Fainelli | bbdcaff | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 45 | int oldadv, adv, bmsr; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 46 | int err, changed = 0; |
| 47 | |
Florian Fainelli | bbdcaff | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 48 | /* Only allow advertising what this PHY supports */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 49 | phydev->advertising &= phydev->supported; |
| 50 | advertise = phydev->advertising; |
| 51 | |
| 52 | /* Setup standard advertisement */ |
Florian Fainelli | bbdcaff | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 53 | adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); |
| 54 | oldadv = adv; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 55 | |
| 56 | if (adv < 0) |
| 57 | return adv; |
| 58 | |
| 59 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
| 60 | ADVERTISE_PAUSE_ASYM); |
| 61 | if (advertise & ADVERTISED_10baseT_Half) |
| 62 | adv |= ADVERTISE_10HALF; |
| 63 | if (advertise & ADVERTISED_10baseT_Full) |
| 64 | adv |= ADVERTISE_10FULL; |
| 65 | if (advertise & ADVERTISED_100baseT_Half) |
| 66 | adv |= ADVERTISE_100HALF; |
| 67 | if (advertise & ADVERTISED_100baseT_Full) |
| 68 | adv |= ADVERTISE_100FULL; |
| 69 | if (advertise & ADVERTISED_Pause) |
| 70 | adv |= ADVERTISE_PAUSE_CAP; |
| 71 | if (advertise & ADVERTISED_Asym_Pause) |
| 72 | adv |= ADVERTISE_PAUSE_ASYM; |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 73 | if (advertise & ADVERTISED_1000baseX_Half) |
| 74 | adv |= ADVERTISE_1000XHALF; |
| 75 | if (advertise & ADVERTISED_1000baseX_Full) |
| 76 | adv |= ADVERTISE_1000XFULL; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 77 | |
| 78 | if (adv != oldadv) { |
| 79 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv); |
| 80 | |
| 81 | if (err < 0) |
| 82 | return err; |
| 83 | changed = 1; |
| 84 | } |
| 85 | |
Florian Fainelli | bbdcaff | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 86 | bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 87 | if (bmsr < 0) |
| 88 | return bmsr; |
| 89 | |
| 90 | /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all |
| 91 | * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a |
| 92 | * logical 1. |
| 93 | */ |
| 94 | if (!(bmsr & BMSR_ESTATEN)) |
| 95 | return changed; |
| 96 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 97 | /* Configure gigabit if it's supported */ |
Florian Fainelli | bbdcaff | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 98 | adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000); |
| 99 | oldadv = adv; |
| 100 | |
| 101 | if (adv < 0) |
| 102 | return adv; |
| 103 | |
| 104 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 105 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 106 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
| 107 | SUPPORTED_1000baseT_Full)) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 108 | if (advertise & SUPPORTED_1000baseT_Half) |
| 109 | adv |= ADVERTISE_1000HALF; |
| 110 | if (advertise & SUPPORTED_1000baseT_Full) |
| 111 | adv |= ADVERTISE_1000FULL; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Florian Fainelli | bbdcaff | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 114 | if (adv != oldadv) |
| 115 | changed = 1; |
| 116 | |
| 117 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv); |
| 118 | if (err < 0) |
| 119 | return err; |
| 120 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 121 | return changed; |
| 122 | } |
| 123 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 124 | /** |
| 125 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 126 | * @phydev: target phy_device struct |
| 127 | * |
| 128 | * Description: Configures MII_BMCR to force speed/duplex |
| 129 | * to the values in phydev. Assumes that the values are valid. |
| 130 | */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 131 | static int genphy_setup_forced(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 132 | { |
| 133 | int err; |
Alexandre Messier | 53b0c38 | 2016-01-22 14:16:15 -0500 | [diff] [blame] | 134 | int ctl = BMCR_ANRESTART; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 135 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 136 | phydev->pause = 0; |
| 137 | phydev->asym_pause = 0; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 138 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 139 | if (phydev->speed == SPEED_1000) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 140 | ctl |= BMCR_SPEED1000; |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 141 | else if (phydev->speed == SPEED_100) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 142 | ctl |= BMCR_SPEED100; |
| 143 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 144 | if (phydev->duplex == DUPLEX_FULL) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 145 | ctl |= BMCR_FULLDPLX; |
| 146 | |
| 147 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); |
| 148 | |
| 149 | return err; |
| 150 | } |
| 151 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 152 | /** |
| 153 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 154 | * @phydev: target phy_device struct |
| 155 | */ |
| 156 | int genphy_restart_aneg(struct phy_device *phydev) |
| 157 | { |
| 158 | int ctl; |
| 159 | |
| 160 | ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 161 | |
| 162 | if (ctl < 0) |
| 163 | return ctl; |
| 164 | |
| 165 | ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); |
| 166 | |
| 167 | /* Don't isolate the PHY if we're negotiating */ |
| 168 | ctl &= ~(BMCR_ISOLATE); |
| 169 | |
| 170 | ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); |
| 171 | |
| 172 | return ctl; |
| 173 | } |
| 174 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 175 | /** |
| 176 | * genphy_config_aneg - restart auto-negotiation or write BMCR |
| 177 | * @phydev: target phy_device struct |
| 178 | * |
| 179 | * Description: If auto-negotiation is enabled, we configure the |
| 180 | * advertising, and then restart auto-negotiation. If it is not |
| 181 | * enabled, then we write the BMCR. |
| 182 | */ |
| 183 | int genphy_config_aneg(struct phy_device *phydev) |
| 184 | { |
| 185 | int result; |
| 186 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 187 | if (phydev->autoneg != AUTONEG_ENABLE) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 188 | return genphy_setup_forced(phydev); |
| 189 | |
| 190 | result = genphy_config_advert(phydev); |
| 191 | |
| 192 | if (result < 0) /* error */ |
| 193 | return result; |
| 194 | |
| 195 | if (result == 0) { |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 196 | /* |
| 197 | * Advertisment hasn't changed, but maybe aneg was never on to |
| 198 | * begin with? Or maybe phy was isolated? |
| 199 | */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 200 | int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 201 | |
| 202 | if (ctl < 0) |
| 203 | return ctl; |
| 204 | |
| 205 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) |
| 206 | result = 1; /* do restart aneg */ |
| 207 | } |
| 208 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 209 | /* |
| 210 | * Only restart aneg if we are advertising something different |
| 211 | * than we were before. |
| 212 | */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 213 | if (result > 0) |
| 214 | result = genphy_restart_aneg(phydev); |
| 215 | |
| 216 | return result; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * genphy_update_link - update link status in @phydev |
| 221 | * @phydev: target phy_device struct |
| 222 | * |
| 223 | * Description: Update the value in phydev->link to reflect the |
| 224 | * current link value. In order to do this, we need to read |
| 225 | * the status register twice, keeping the second value. |
| 226 | */ |
| 227 | int genphy_update_link(struct phy_device *phydev) |
| 228 | { |
| 229 | unsigned int mii_reg; |
| 230 | |
| 231 | /* |
| 232 | * Wait if the link is up, and autonegotiation is in progress |
| 233 | * (ie - we're capable and it's not done) |
| 234 | */ |
| 235 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 236 | |
| 237 | /* |
| 238 | * If we already saw the link up, and it hasn't gone down, then |
| 239 | * we don't need to wait for autoneg again |
| 240 | */ |
| 241 | if (phydev->link && mii_reg & BMSR_LSTATUS) |
| 242 | return 0; |
| 243 | |
Alexandre Messier | 1f9e672 | 2016-01-22 14:16:56 -0500 | [diff] [blame] | 244 | if ((phydev->autoneg == AUTONEG_ENABLE) && |
| 245 | !(mii_reg & BMSR_ANEGCOMPLETE)) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 246 | int i = 0; |
| 247 | |
| 248 | printf("%s Waiting for PHY auto negotiation to complete", |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 249 | phydev->dev->name); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 250 | while (!(mii_reg & BMSR_ANEGCOMPLETE)) { |
| 251 | /* |
| 252 | * Timeout reached ? |
| 253 | */ |
Andre Przywara | a44ee24 | 2020-01-03 22:08:47 +0000 | [diff] [blame] | 254 | if (i > (PHY_ANEG_TIMEOUT / 50)) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 255 | printf(" TIMEOUT !\n"); |
| 256 | phydev->link = 0; |
Michal Simek | ef5e821 | 2016-05-18 12:48:57 +0200 | [diff] [blame] | 257 | return -ETIMEDOUT; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (ctrlc()) { |
| 261 | puts("user interrupt!\n"); |
| 262 | phydev->link = 0; |
| 263 | return -EINTR; |
| 264 | } |
| 265 | |
Stefan Roese | 27c3f70 | 2019-09-30 10:26:42 +0200 | [diff] [blame] | 266 | if ((i++ % 10) == 0) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 267 | printf("."); |
| 268 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 269 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
Stefan Roese | 27c3f70 | 2019-09-30 10:26:42 +0200 | [diff] [blame] | 270 | mdelay(50); /* 50 ms */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 271 | } |
| 272 | printf(" done\n"); |
| 273 | phydev->link = 1; |
| 274 | } else { |
| 275 | /* Read the link a second time to clear the latched state */ |
| 276 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 277 | |
| 278 | if (mii_reg & BMSR_LSTATUS) |
| 279 | phydev->link = 1; |
| 280 | else |
| 281 | phydev->link = 0; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Generic function which updates the speed and duplex. If |
| 289 | * autonegotiation is enabled, it uses the AND of the link |
| 290 | * partner's advertised capabilities and our advertised |
| 291 | * capabilities. If autonegotiation is disabled, we use the |
| 292 | * appropriate bits in the control register. |
| 293 | * |
| 294 | * Stolen from Linux's mii.c and phy_device.c |
| 295 | */ |
Yegor Yefremov | e2043f5 | 2012-11-28 11:15:17 +0100 | [diff] [blame] | 296 | int genphy_parse_link(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 297 | { |
| 298 | int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 299 | |
| 300 | /* We're using autonegotiation */ |
Alexandre Messier | 1f9e672 | 2016-01-22 14:16:56 -0500 | [diff] [blame] | 301 | if (phydev->autoneg == AUTONEG_ENABLE) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 302 | u32 lpa = 0; |
Heiko Schocher | f6d1f6e | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 303 | int gblpa = 0; |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 304 | u32 estatus = 0; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 305 | |
| 306 | /* Check for gigabit capability */ |
David Dueck | 3a530d1 | 2013-11-05 17:23:02 +0100 | [diff] [blame] | 307 | if (phydev->supported & (SUPPORTED_1000baseT_Full | |
| 308 | SUPPORTED_1000baseT_Half)) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 309 | /* We want a list of states supported by |
| 310 | * both PHYs in the link |
| 311 | */ |
| 312 | gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000); |
Heiko Schocher | f6d1f6e | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 313 | if (gblpa < 0) { |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 314 | debug("Could not read MII_STAT1000. "); |
| 315 | debug("Ignoring gigabit capability\n"); |
Heiko Schocher | f6d1f6e | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 316 | gblpa = 0; |
| 317 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 318 | gblpa &= phy_read(phydev, |
| 319 | MDIO_DEVAD_NONE, MII_CTRL1000) << 2; |
| 320 | } |
| 321 | |
| 322 | /* Set the baseline so we only have to set them |
| 323 | * if they're different |
| 324 | */ |
| 325 | phydev->speed = SPEED_10; |
| 326 | phydev->duplex = DUPLEX_HALF; |
| 327 | |
| 328 | /* Check the gigabit fields */ |
| 329 | if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) { |
| 330 | phydev->speed = SPEED_1000; |
| 331 | |
| 332 | if (gblpa & PHY_1000BTSR_1000FD) |
| 333 | phydev->duplex = DUPLEX_FULL; |
| 334 | |
| 335 | /* We're done! */ |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); |
| 340 | lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA); |
| 341 | |
Wolfgang Denk | 0dcfb0f | 2011-09-28 21:02:43 +0200 | [diff] [blame] | 342 | if (lpa & (LPA_100FULL | LPA_100HALF)) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 343 | phydev->speed = SPEED_100; |
| 344 | |
Wolfgang Denk | 0dcfb0f | 2011-09-28 21:02:43 +0200 | [diff] [blame] | 345 | if (lpa & LPA_100FULL) |
| 346 | phydev->duplex = DUPLEX_FULL; |
| 347 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 348 | } else if (lpa & LPA_10FULL) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 349 | phydev->duplex = DUPLEX_FULL; |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 350 | } |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 351 | |
Sascha Silbe | 9ba30f6 | 2013-07-19 12:25:10 +0200 | [diff] [blame] | 352 | /* |
| 353 | * Extended status may indicate that the PHY supports |
| 354 | * 1000BASE-T/X even though the 1000BASE-T registers |
| 355 | * are missing. In this case we can't tell whether the |
| 356 | * peer also supports it, so we only check extended |
| 357 | * status if the 1000BASE-T registers are actually |
| 358 | * missing. |
| 359 | */ |
| 360 | if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP)) |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 361 | estatus = phy_read(phydev, MDIO_DEVAD_NONE, |
| 362 | MII_ESTATUS); |
| 363 | |
| 364 | if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF | |
| 365 | ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) { |
| 366 | phydev->speed = SPEED_1000; |
| 367 | if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL)) |
| 368 | phydev->duplex = DUPLEX_FULL; |
| 369 | } |
| 370 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 371 | } else { |
| 372 | u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 373 | |
| 374 | phydev->speed = SPEED_10; |
| 375 | phydev->duplex = DUPLEX_HALF; |
| 376 | |
| 377 | if (bmcr & BMCR_FULLDPLX) |
| 378 | phydev->duplex = DUPLEX_FULL; |
| 379 | |
| 380 | if (bmcr & BMCR_SPEED1000) |
| 381 | phydev->speed = SPEED_1000; |
| 382 | else if (bmcr & BMCR_SPEED100) |
| 383 | phydev->speed = SPEED_100; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | int genphy_config(struct phy_device *phydev) |
| 390 | { |
| 391 | int val; |
| 392 | u32 features; |
| 393 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 394 | features = (SUPPORTED_TP | SUPPORTED_MII |
| 395 | | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 396 | SUPPORTED_BNC); |
| 397 | |
| 398 | /* Do we support autonegotiation? */ |
| 399 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 400 | |
| 401 | if (val < 0) |
| 402 | return val; |
| 403 | |
| 404 | if (val & BMSR_ANEGCAPABLE) |
| 405 | features |= SUPPORTED_Autoneg; |
| 406 | |
| 407 | if (val & BMSR_100FULL) |
| 408 | features |= SUPPORTED_100baseT_Full; |
| 409 | if (val & BMSR_100HALF) |
| 410 | features |= SUPPORTED_100baseT_Half; |
| 411 | if (val & BMSR_10FULL) |
| 412 | features |= SUPPORTED_10baseT_Full; |
| 413 | if (val & BMSR_10HALF) |
| 414 | features |= SUPPORTED_10baseT_Half; |
| 415 | |
| 416 | if (val & BMSR_ESTATEN) { |
| 417 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS); |
| 418 | |
| 419 | if (val < 0) |
| 420 | return val; |
| 421 | |
| 422 | if (val & ESTATUS_1000_TFULL) |
| 423 | features |= SUPPORTED_1000baseT_Full; |
| 424 | if (val & ESTATUS_1000_THALF) |
| 425 | features |= SUPPORTED_1000baseT_Half; |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 426 | if (val & ESTATUS_1000_XFULL) |
| 427 | features |= SUPPORTED_1000baseX_Full; |
| 428 | if (val & ESTATUS_1000_XHALF) |
Fabio Estevam | 9a5dad2 | 2013-07-19 10:01:34 -0300 | [diff] [blame] | 429 | features |= SUPPORTED_1000baseX_Half; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 430 | } |
| 431 | |
Sascha Hauer | 44bc317 | 2016-01-13 16:59:32 +0300 | [diff] [blame] | 432 | phydev->supported &= features; |
| 433 | phydev->advertising &= features; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 434 | |
| 435 | genphy_config_aneg(phydev); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | int genphy_startup(struct phy_device *phydev) |
| 441 | { |
Michal Simek | b733c27 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 442 | int ret; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 443 | |
Michal Simek | b733c27 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 444 | ret = genphy_update_link(phydev); |
| 445 | if (ret) |
| 446 | return ret; |
| 447 | |
| 448 | return genphy_parse_link(phydev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | int genphy_shutdown(struct phy_device *phydev) |
| 452 | { |
| 453 | return 0; |
| 454 | } |
| 455 | |
Marek Vasut | f705329 | 2023-03-19 18:03:12 +0100 | [diff] [blame] | 456 | U_BOOT_PHY_DRIVER(genphy) = { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 457 | .uid = 0xffffffff, |
| 458 | .mask = 0xffffffff, |
| 459 | .name = "Generic PHY", |
Sascha Hauer | 44bc317 | 2016-01-13 16:59:32 +0300 | [diff] [blame] | 460 | .features = PHY_GBIT_FEATURES | SUPPORTED_MII | |
| 461 | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 462 | SUPPORTED_BNC, |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 463 | .config = genphy_config, |
| 464 | .startup = genphy_startup, |
| 465 | .shutdown = genphy_shutdown, |
| 466 | }; |
| 467 | |
Alexey Brodkin | b18acb0 | 2016-01-13 16:59:34 +0300 | [diff] [blame] | 468 | int phy_set_supported(struct phy_device *phydev, u32 max_speed) |
| 469 | { |
| 470 | /* The default values for phydev->supported are provided by the PHY |
| 471 | * driver "features" member, we want to reset to sane defaults first |
| 472 | * before supporting higher speeds. |
| 473 | */ |
| 474 | phydev->supported &= PHY_DEFAULT_FEATURES; |
| 475 | |
| 476 | switch (max_speed) { |
| 477 | default: |
| 478 | return -ENOTSUPP; |
| 479 | case SPEED_1000: |
| 480 | phydev->supported |= PHY_1000BT_FEATURES; |
| 481 | /* fall through */ |
| 482 | case SPEED_100: |
| 483 | phydev->supported |= PHY_100BT_FEATURES; |
| 484 | /* fall through */ |
| 485 | case SPEED_10: |
| 486 | phydev->supported |= PHY_10BT_FEATURES; |
| 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 492 | static int phy_probe(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 493 | { |
| 494 | int err = 0; |
| 495 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 496 | phydev->advertising = phydev->drv->features; |
| 497 | phydev->supported = phydev->drv->features; |
| 498 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 499 | phydev->mmds = phydev->drv->mmds; |
| 500 | |
| 501 | if (phydev->drv->probe) |
| 502 | err = phydev->drv->probe(phydev); |
| 503 | |
| 504 | return err; |
| 505 | } |
| 506 | |
Marek Behún | 79bef5f | 2022-04-07 00:33:06 +0200 | [diff] [blame] | 507 | static struct phy_driver *generic_for_phy(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 508 | { |
| 509 | #ifdef CONFIG_PHYLIB_10G |
Marek Behún | 79bef5f | 2022-04-07 00:33:06 +0200 | [diff] [blame] | 510 | if (phydev->is_c45) |
Marek Vasut | 20bd8e4 | 2023-03-19 18:03:13 +0100 | [diff] [blame] | 511 | return ll_entry_get(struct phy_driver, gen10g, phy_driver); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 512 | #endif |
| 513 | |
Marek Vasut | f705329 | 2023-03-19 18:03:12 +0100 | [diff] [blame] | 514 | return ll_entry_get(struct phy_driver, genphy, phy_driver); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 515 | } |
| 516 | |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 517 | static struct phy_driver *get_phy_driver(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 518 | { |
Marek Vasut | 7940a93 | 2023-03-19 18:02:42 +0100 | [diff] [blame] | 519 | const int ll_n_ents = ll_entry_count(struct phy_driver, phy_driver); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 520 | int phy_id = phydev->phy_id; |
Marek Vasut | 8728d4c | 2023-03-19 18:03:14 +0100 | [diff] [blame] | 521 | struct phy_driver *ll_entry; |
| 522 | struct phy_driver *drv; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 523 | |
Marek Vasut | 7940a93 | 2023-03-19 18:02:42 +0100 | [diff] [blame] | 524 | ll_entry = ll_entry_start(struct phy_driver, phy_driver); |
| 525 | for (drv = ll_entry; drv != ll_entry + ll_n_ents; drv++) |
| 526 | if ((drv->uid & drv->mask) == (phy_id & drv->mask)) |
| 527 | return drv; |
| 528 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 529 | /* If we made it here, there's no driver for this PHY */ |
Marek Behún | 79bef5f | 2022-04-07 00:33:06 +0200 | [diff] [blame] | 530 | return generic_for_phy(phydev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 531 | } |
| 532 | |
Michal Simek | 3249116 | 2022-02-23 15:45:41 +0100 | [diff] [blame] | 533 | struct phy_device *phy_device_create(struct mii_dev *bus, int addr, |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 534 | u32 phy_id, bool is_c45) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 535 | { |
| 536 | struct phy_device *dev; |
| 537 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 538 | /* |
| 539 | * We allocate the device, and initialize the |
| 540 | * default values |
| 541 | */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 542 | dev = malloc(sizeof(*dev)); |
| 543 | if (!dev) { |
| 544 | printf("Failed to allocate PHY device for %s:%d\n", |
Vladimir Oltean | 15c49df | 2020-07-16 18:09:08 +0800 | [diff] [blame] | 545 | bus ? bus->name : "(null bus)", addr); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | memset(dev, 0, sizeof(*dev)); |
| 550 | |
| 551 | dev->duplex = -1; |
Mugunthan V N | 26d3acd | 2015-09-03 15:50:21 +0530 | [diff] [blame] | 552 | dev->link = 0; |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 553 | dev->interface = PHY_INTERFACE_MODE_NA; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 554 | |
Grygorii Strashko | eef0b8a | 2018-07-05 12:02:48 -0500 | [diff] [blame] | 555 | dev->node = ofnode_null(); |
Grygorii Strashko | eef0b8a | 2018-07-05 12:02:48 -0500 | [diff] [blame] | 556 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 557 | dev->autoneg = AUTONEG_ENABLE; |
| 558 | |
| 559 | dev->addr = addr; |
| 560 | dev->phy_id = phy_id; |
Pankaj Bansal | b3eabd8 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 561 | dev->is_c45 = is_c45; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 562 | dev->bus = bus; |
| 563 | |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 564 | dev->drv = get_phy_driver(dev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 565 | |
Siva Durga Prasad Paladugu | 05eb6a6 | 2019-03-04 16:02:11 +0100 | [diff] [blame] | 566 | if (phy_probe(dev)) { |
| 567 | printf("%s, PHY probe failed\n", __func__); |
| 568 | return NULL; |
| 569 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 570 | |
Jacky Chou | f146c44 | 2024-01-15 18:34:47 +0800 | [diff] [blame] | 571 | if (addr >= 0 && addr < PHY_MAX_ADDR && phy_id != PHY_FIXED_ID && |
| 572 | phy_id != PHY_NCSI_ID) |
Michal Simek | 7b4ea2d | 2018-12-19 16:57:38 +0100 | [diff] [blame] | 573 | bus->phymap[addr] = dev; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 574 | |
| 575 | return dev; |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * get_phy_id - reads the specified addr for its ID. |
| 580 | * @bus: the target MII bus |
| 581 | * @addr: PHY address on the MII bus |
| 582 | * @phy_id: where to store the ID retrieved. |
| 583 | * |
| 584 | * Description: Reads the ID registers of the PHY at @addr on the |
| 585 | * @bus, stores it in @phy_id and returns zero on success. |
| 586 | */ |
Shengzhou Liu | 5707d5f | 2015-04-07 18:46:32 +0800 | [diff] [blame] | 587 | int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 588 | { |
| 589 | int phy_reg; |
| 590 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 591 | /* |
| 592 | * Grab the bits from PHYIR1, and put them |
| 593 | * in the upper half |
| 594 | */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 595 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID1); |
| 596 | |
| 597 | if (phy_reg < 0) |
| 598 | return -EIO; |
| 599 | |
| 600 | *phy_id = (phy_reg & 0xffff) << 16; |
| 601 | |
| 602 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
| 603 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID2); |
| 604 | |
| 605 | if (phy_reg < 0) |
| 606 | return -EIO; |
| 607 | |
| 608 | *phy_id |= (phy_reg & 0xffff); |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 613 | static struct phy_device *create_phy_by_mask(struct mii_dev *bus, |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 614 | uint phy_mask, int devad) |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 615 | { |
| 616 | u32 phy_id = 0xffffffff; |
Pankaj Bansal | b3eabd8 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 617 | bool is_c45; |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 618 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 619 | while (phy_mask) { |
| 620 | int addr = ffs(phy_mask) - 1; |
| 621 | int r = get_phy_id(bus, addr, devad, &phy_id); |
Alex Marginean | 3bf135b | 2019-07-05 12:28:55 +0300 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * If the PHY ID is flat 0 we ignore it. There are C45 PHYs |
| 625 | * that return all 0s for C22 reads (like Aquantia AQR112) and |
| 626 | * there are C22 PHYs that return all 0s for C45 reads (like |
| 627 | * Atheros AR8035). |
| 628 | */ |
| 629 | if (r == 0 && phy_id == 0) |
| 630 | goto next; |
| 631 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 632 | /* If the PHY ID is mostly f's, we didn't find anything */ |
Pankaj Bansal | b3eabd8 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 633 | if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff) { |
| 634 | is_c45 = (devad == MDIO_DEVAD_NONE) ? false : true; |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 635 | return phy_device_create(bus, addr, phy_id, is_c45); |
Pankaj Bansal | b3eabd8 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 636 | } |
Alex Marginean | 3bf135b | 2019-07-05 12:28:55 +0300 | [diff] [blame] | 637 | next: |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 638 | phy_mask &= ~(1 << addr); |
| 639 | } |
| 640 | return NULL; |
| 641 | } |
| 642 | |
| 643 | static struct phy_device *search_for_existing_phy(struct mii_dev *bus, |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 644 | uint phy_mask) |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 645 | { |
| 646 | /* If we have one, return the existing device, with new interface */ |
| 647 | while (phy_mask) { |
Eugeniu Rosca | f5dbfc8 | 2024-01-04 05:26:23 +0100 | [diff] [blame] | 648 | unsigned int addr = ffs(phy_mask) - 1; |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 649 | |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 650 | if (bus->phymap[addr]) |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 651 | return bus->phymap[addr]; |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 652 | |
Eugeniu Rosca | f5dbfc8 | 2024-01-04 05:26:23 +0100 | [diff] [blame] | 653 | phy_mask &= ~(1U << addr); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 654 | } |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus, |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 659 | uint phy_mask) |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 660 | { |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 661 | struct phy_device *phydev; |
Florin Chiculita | 9c6de50 | 2020-04-29 14:25:48 +0300 | [diff] [blame] | 662 | int devad[] = { |
| 663 | /* Clause-22 */ |
| 664 | MDIO_DEVAD_NONE, |
| 665 | /* Clause-45 */ |
| 666 | MDIO_MMD_PMAPMD, |
| 667 | MDIO_MMD_WIS, |
| 668 | MDIO_MMD_PCS, |
| 669 | MDIO_MMD_PHYXS, |
| 670 | MDIO_MMD_VEND1, |
| 671 | }; |
| 672 | int i, devad_cnt; |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 673 | |
Florin Chiculita | 9c6de50 | 2020-04-29 14:25:48 +0300 | [diff] [blame] | 674 | devad_cnt = sizeof(devad)/sizeof(int); |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 675 | phydev = search_for_existing_phy(bus, phy_mask); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 676 | if (phydev) |
| 677 | return phydev; |
Florin Chiculita | 9c6de50 | 2020-04-29 14:25:48 +0300 | [diff] [blame] | 678 | /* try different access clauses */ |
| 679 | for (i = 0; i < devad_cnt; i++) { |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 680 | phydev = create_phy_by_mask(bus, phy_mask, devad[i]); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 681 | if (IS_ERR(phydev)) |
| 682 | return NULL; |
| 683 | if (phydev) |
| 684 | return phydev; |
| 685 | } |
Bin Meng | 3e1949d | 2015-10-07 21:19:30 -0700 | [diff] [blame] | 686 | |
| 687 | debug("\n%s PHY: ", bus->name); |
| 688 | while (phy_mask) { |
| 689 | int addr = ffs(phy_mask) - 1; |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 690 | |
Bin Meng | 3e1949d | 2015-10-07 21:19:30 -0700 | [diff] [blame] | 691 | debug("%d ", addr); |
| 692 | phy_mask &= ~(1 << addr); |
| 693 | } |
| 694 | debug("not found\n"); |
Bin Meng | 0132b9a | 2015-10-07 21:19:29 -0700 | [diff] [blame] | 695 | |
| 696 | return NULL; |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 699 | /** |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 700 | * get_phy_device - reads the specified PHY device and returns its |
| 701 | * @phy_device struct |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 702 | * @bus: the target MII bus |
| 703 | * @addr: PHY address on the MII bus |
| 704 | * |
| 705 | * Description: Reads the ID registers of the PHY at @addr on the |
| 706 | * @bus, then allocates and returns the phy_device to represent it. |
| 707 | */ |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 708 | static struct phy_device *get_phy_device(struct mii_dev *bus, int addr) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 709 | { |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 710 | return get_phy_device_by_mask(bus, 1 << addr); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | int phy_reset(struct phy_device *phydev) |
| 714 | { |
| 715 | int reg; |
| 716 | int timeout = 500; |
| 717 | int devad = MDIO_DEVAD_NONE; |
| 718 | |
Shaohui Xie | ddcd1f3 | 2016-01-28 15:55:46 +0800 | [diff] [blame] | 719 | if (phydev->flags & PHY_FLAG_BROKEN_RESET) |
| 720 | return 0; |
| 721 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 722 | #ifdef CONFIG_PHYLIB_10G |
| 723 | /* If it's 10G, we need to issue reset through one of the MMDs */ |
Marek Behún | 79bef5f | 2022-04-07 00:33:06 +0200 | [diff] [blame] | 724 | if (phydev->is_c45) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 725 | if (!phydev->mmds) |
| 726 | gen10g_discover_mmds(phydev); |
| 727 | |
| 728 | devad = ffs(phydev->mmds) - 1; |
| 729 | } |
| 730 | #endif |
| 731 | |
Stefan Agner | a058052 | 2015-12-09 11:21:25 -0800 | [diff] [blame] | 732 | if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 733 | debug("PHY reset failed\n"); |
| 734 | return -1; |
| 735 | } |
| 736 | |
Tom Rini | 16199a8 | 2022-03-18 08:38:26 -0400 | [diff] [blame] | 737 | #if CONFIG_PHY_RESET_DELAY > 0 |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 738 | udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */ |
| 739 | #endif |
| 740 | /* |
| 741 | * Poll the control register for the reset bit to go to 0 (it is |
| 742 | * auto-clearing). This should happen within 0.5 seconds per the |
| 743 | * IEEE spec. |
| 744 | */ |
Stefan Agner | a058052 | 2015-12-09 11:21:25 -0800 | [diff] [blame] | 745 | reg = phy_read(phydev, devad, MII_BMCR); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 746 | while ((reg & BMCR_RESET) && timeout--) { |
| 747 | reg = phy_read(phydev, devad, MII_BMCR); |
| 748 | |
| 749 | if (reg < 0) { |
| 750 | debug("PHY status read failed\n"); |
| 751 | return -1; |
| 752 | } |
| 753 | udelay(1000); |
| 754 | } |
| 755 | |
| 756 | if (reg & BMCR_RESET) { |
| 757 | puts("PHY reset timed out\n"); |
| 758 | return -1; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | int miiphy_reset(const char *devname, unsigned char addr) |
| 765 | { |
| 766 | struct mii_dev *bus = miiphy_get_dev_by_name(devname); |
| 767 | struct phy_device *phydev; |
| 768 | |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 769 | phydev = get_phy_device(bus, addr); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 770 | |
| 771 | return phy_reset(phydev); |
| 772 | } |
| 773 | |
Marek Vasut | e99a6ef | 2024-03-18 15:57:01 +0100 | [diff] [blame] | 774 | #if CONFIG_IS_ENABLED(DM_GPIO) && CONFIG_IS_ENABLED(OF_REAL) && \ |
| 775 | !IS_ENABLED(CONFIG_DM_ETH_PHY) |
| 776 | int phy_gpio_reset(struct udevice *dev) |
| 777 | { |
| 778 | struct ofnode_phandle_args phandle_args; |
| 779 | struct gpio_desc gpio; |
| 780 | u32 assert, deassert; |
| 781 | ofnode node; |
| 782 | int ret; |
| 783 | |
| 784 | ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0, |
| 785 | &phandle_args); |
| 786 | /* No PHY handle is OK */ |
| 787 | if (ret) |
| 788 | return 0; |
| 789 | |
| 790 | node = phandle_args.node; |
| 791 | if (!ofnode_valid(node)) |
| 792 | return -EINVAL; |
| 793 | |
| 794 | ret = gpio_request_by_name_nodev(node, "reset-gpios", 0, &gpio, |
| 795 | GPIOD_IS_OUT | GPIOD_ACTIVE_LOW); |
| 796 | /* No PHY reset GPIO is OK */ |
| 797 | if (ret) |
| 798 | return 0; |
| 799 | |
| 800 | assert = ofnode_read_u32_default(node, "reset-assert-us", 20000); |
| 801 | deassert = ofnode_read_u32_default(node, "reset-deassert-us", 1000); |
| 802 | ret = dm_gpio_set_value(&gpio, 1); |
| 803 | if (ret) { |
| 804 | dev_err(dev, "Failed assert gpio, err: %d\n", ret); |
| 805 | return ret; |
| 806 | } |
| 807 | |
| 808 | udelay(assert); |
| 809 | |
| 810 | ret = dm_gpio_set_value(&gpio, 0); |
| 811 | if (ret) { |
| 812 | dev_err(dev, "Failed deassert gpio, err: %d\n", ret); |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | udelay(deassert); |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | #else |
| 821 | int phy_gpio_reset(struct udevice *dev) |
| 822 | { |
| 823 | return 0; |
| 824 | } |
| 825 | #endif |
| 826 | |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 827 | struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 828 | { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 829 | /* Reset the bus */ |
Jörg Krause | 59370f3 | 2015-07-15 15:18:22 +0200 | [diff] [blame] | 830 | if (bus->reset) { |
Vladimir Zapolskiy | e3a7721 | 2011-09-05 07:24:07 +0000 | [diff] [blame] | 831 | bus->reset(bus); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 832 | |
Jörg Krause | 59370f3 | 2015-07-15 15:18:22 +0200 | [diff] [blame] | 833 | /* Wait 15ms to make sure the PHY has come out of hard reset */ |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 834 | mdelay(15); |
Jörg Krause | 59370f3 | 2015-07-15 15:18:22 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 837 | return get_phy_device_by_mask(bus, phy_mask); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 838 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 839 | |
Marek Vasut | 495fc04 | 2023-05-31 00:51:25 +0200 | [diff] [blame] | 840 | static void phy_connect_dev(struct phy_device *phydev, struct udevice *dev, |
| 841 | phy_interface_t interface) |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 842 | { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 843 | /* Soft Reset the PHY */ |
| 844 | phy_reset(phydev); |
Bin Meng | 17ecfa9 | 2015-10-07 21:19:31 -0700 | [diff] [blame] | 845 | if (phydev->dev && phydev->dev != dev) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 846 | printf("%s:%d is connected to %s. Reconnecting to %s\n", |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 847 | phydev->bus->name, phydev->addr, |
| 848 | phydev->dev->name, dev->name); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 849 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 850 | phydev->dev = dev; |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 851 | phydev->interface = interface; |
| 852 | debug("%s connected to %s mode %s\n", dev->name, phydev->drv->name, |
| 853 | phy_string_for_interface(interface)); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 854 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 855 | |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 856 | #ifdef CONFIG_PHY_XILINX_GMII2RGMII |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 857 | static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus, |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 858 | struct udevice *dev) |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 859 | { |
| 860 | struct phy_device *phydev = NULL; |
Michal Simek | 0a9f0e0 | 2021-04-26 14:26:48 +0200 | [diff] [blame] | 861 | ofnode node; |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 862 | |
Michal Simek | 0a9f0e0 | 2021-04-26 14:26:48 +0200 | [diff] [blame] | 863 | ofnode_for_each_subnode(node, dev_ofnode(dev)) { |
Bin Meng | 6c99381 | 2021-03-14 20:14:50 +0800 | [diff] [blame] | 864 | node = ofnode_by_compatible(node, "xlnx,gmii-to-rgmii-1.0"); |
| 865 | if (ofnode_valid(node)) { |
Tejas Bhumkar | e31d707 | 2023-09-15 10:20:43 +0530 | [diff] [blame] | 866 | int gmiirgmii_phyaddr; |
| 867 | |
| 868 | gmiirgmii_phyaddr = ofnode_read_u32_default(node, "reg", 0); |
| 869 | phydev = phy_device_create(bus, gmiirgmii_phyaddr, |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 870 | PHY_GMII2RGMII_ID, false); |
Bin Meng | 6c99381 | 2021-03-14 20:14:50 +0800 | [diff] [blame] | 871 | if (phydev) |
| 872 | phydev->node = node; |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 873 | break; |
| 874 | } |
Bin Meng | 6c99381 | 2021-03-14 20:14:50 +0800 | [diff] [blame] | 875 | |
| 876 | node = ofnode_first_subnode(node); |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | return phydev; |
| 880 | } |
| 881 | #endif |
| 882 | |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 883 | #ifdef CONFIG_PHY_FIXED |
Vladimir Oltean | d0781c9 | 2021-01-25 14:23:52 +0200 | [diff] [blame] | 884 | /** |
| 885 | * fixed_phy_create() - create an unconnected fixed-link pseudo-PHY device |
| 886 | * @node: OF node for the container of the fixed-link node |
| 887 | * |
| 888 | * Description: Creates a struct phy_device based on a fixed-link of_node |
| 889 | * description. Can be used without phy_connect by drivers which do not expose |
| 890 | * a UCLASS_ETH udevice. |
| 891 | */ |
| 892 | struct phy_device *fixed_phy_create(ofnode node) |
| 893 | { |
Vladimir Oltean | f27bc8a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 894 | struct phy_device *phydev; |
Vladimir Oltean | d0781c9 | 2021-01-25 14:23:52 +0200 | [diff] [blame] | 895 | ofnode subnode; |
| 896 | |
Vladimir Oltean | d0781c9 | 2021-01-25 14:23:52 +0200 | [diff] [blame] | 897 | subnode = ofnode_find_subnode(node, "fixed-link"); |
| 898 | if (!ofnode_valid(subnode)) { |
| 899 | return NULL; |
| 900 | } |
| 901 | |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 902 | phydev = phy_device_create(NULL, 0, PHY_FIXED_ID, false); |
Heinrich Schuchardt | ebb8ff6 | 2022-07-11 19:40:13 +0200 | [diff] [blame] | 903 | if (phydev) { |
Vladimir Oltean | f27bc8a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 904 | phydev->node = subnode; |
Heinrich Schuchardt | ebb8ff6 | 2022-07-11 19:40:13 +0200 | [diff] [blame] | 905 | phydev->interface = ofnode_read_phy_mode(node); |
| 906 | } |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 907 | |
Vladimir Oltean | f27bc8a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 908 | return phydev; |
Vladimir Oltean | d0781c9 | 2021-01-25 14:23:52 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 911 | static struct phy_device *phy_connect_fixed(struct mii_dev *bus, |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 912 | struct udevice *dev) |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 913 | { |
Vladimir Oltean | f27bc8a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 914 | ofnode node = dev_ofnode(dev), subnode; |
Bin Meng | 676fbd3 | 2021-03-14 20:14:52 +0800 | [diff] [blame] | 915 | struct phy_device *phydev = NULL; |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 916 | |
Bin Meng | 676fbd3 | 2021-03-14 20:14:52 +0800 | [diff] [blame] | 917 | if (ofnode_phy_is_fixed_link(node, &subnode)) { |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 918 | phydev = phy_device_create(bus, 0, PHY_FIXED_ID, false); |
Bin Meng | 676fbd3 | 2021-03-14 20:14:52 +0800 | [diff] [blame] | 919 | if (phydev) |
| 920 | phydev->node = subnode; |
| 921 | } |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 922 | |
| 923 | return phydev; |
| 924 | } |
Hannes Schmelzer | db40c1a | 2017-03-23 15:11:43 +0100 | [diff] [blame] | 925 | #endif |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 926 | |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 927 | struct phy_device *phy_connect(struct mii_dev *bus, int addr, |
| 928 | struct udevice *dev, |
| 929 | phy_interface_t interface) |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 930 | { |
| 931 | struct phy_device *phydev = NULL; |
Priyanka Jain | 1f60789 | 2019-11-05 04:05:11 +0000 | [diff] [blame] | 932 | uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff; |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 933 | |
| 934 | #ifdef CONFIG_PHY_FIXED |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 935 | phydev = phy_connect_fixed(bus, dev); |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 936 | #endif |
Samuel Mendoza-Jonas | e2ffeaa | 2019-06-18 11:37:18 +1000 | [diff] [blame] | 937 | |
| 938 | #ifdef CONFIG_PHY_NCSI |
Samuel Mendoza-Jonas | 09bd3d0 | 2022-08-08 21:46:03 +0930 | [diff] [blame] | 939 | if (!phydev && interface == PHY_INTERFACE_MODE_NCSI) |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 940 | phydev = phy_device_create(bus, 0, PHY_NCSI_ID, false); |
Samuel Mendoza-Jonas | e2ffeaa | 2019-06-18 11:37:18 +1000 | [diff] [blame] | 941 | #endif |
| 942 | |
Michal Simek | a744a28 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 943 | #ifdef CONFIG_PHY_ETHERNET_ID |
| 944 | if (!phydev) |
Tom Rini | 7f418ea | 2022-04-15 08:09:52 -0400 | [diff] [blame] | 945 | phydev = phy_connect_phy_id(bus, dev, addr); |
Michal Simek | a744a28 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 946 | #endif |
| 947 | |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 948 | #ifdef CONFIG_PHY_XILINX_GMII2RGMII |
| 949 | if (!phydev) |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 950 | phydev = phy_connect_gmii2rgmii(bus, dev); |
Siva Durga Prasad Paladugu | f41e588 | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 951 | #endif |
Siva Durga Prasad Paladugu | c256d3f | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 952 | |
Mario Six | 8d63120 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 953 | if (!phydev) |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 954 | phydev = phy_find_by_mask(bus, mask); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 955 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 956 | if (phydev) |
Marek Behún | e24b58f | 2022-04-07 00:33:08 +0200 | [diff] [blame] | 957 | phy_connect_dev(phydev, dev, interface); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 958 | else |
| 959 | printf("Could not get PHY for %s: addr %d\n", bus->name, addr); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 960 | return phydev; |
| 961 | } |
| 962 | |
Timur Tabi | 6e5b9ac | 2012-07-05 10:33:18 +0000 | [diff] [blame] | 963 | /* |
| 964 | * Start the PHY. Returns 0 on success, or a negative error code. |
| 965 | */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 966 | int phy_startup(struct phy_device *phydev) |
| 967 | { |
| 968 | if (phydev->drv->startup) |
Timur Tabi | 6e5b9ac | 2012-07-05 10:33:18 +0000 | [diff] [blame] | 969 | return phydev->drv->startup(phydev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
Jeroen Hofstee | 3c6928f | 2014-10-08 22:57:26 +0200 | [diff] [blame] | 974 | __weak int board_phy_config(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 975 | { |
Troy Kisky | 9fafe7d | 2012-02-07 14:08:49 +0000 | [diff] [blame] | 976 | if (phydev->drv->config) |
| 977 | return phydev->drv->config(phydev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 978 | return 0; |
| 979 | } |
| 980 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 981 | int phy_config(struct phy_device *phydev) |
| 982 | { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 983 | /* Invoke an optional board-specific helper */ |
Michal Simek | 7a673f0 | 2016-05-18 14:37:23 +0200 | [diff] [blame] | 984 | return board_phy_config(phydev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | int phy_shutdown(struct phy_device *phydev) |
| 988 | { |
| 989 | if (phydev->drv->shutdown) |
| 990 | phydev->drv->shutdown(phydev); |
| 991 | |
| 992 | return 0; |
| 993 | } |
Simon Glass | c74c8e6 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 994 | |
Ariel D'Alessandro | 087baf8 | 2022-04-12 10:31:36 -0300 | [diff] [blame] | 995 | /** |
| 996 | * phy_modify - Convenience function for modifying a given PHY register |
| 997 | * @phydev: the phy_device struct |
| 998 | * @devad: The MMD to read from |
| 999 | * @regnum: register number to write |
| 1000 | * @mask: bit mask of bits to clear |
| 1001 | * @set: new value of bits set in mask to write to @regnum |
| 1002 | */ |
| 1003 | int phy_modify(struct phy_device *phydev, int devad, int regnum, u16 mask, |
| 1004 | u16 set) |
| 1005 | { |
| 1006 | int ret; |
| 1007 | |
| 1008 | ret = phy_read(phydev, devad, regnum); |
| 1009 | if (ret < 0) |
| 1010 | return ret; |
| 1011 | |
| 1012 | return phy_write(phydev, devad, regnum, (ret & ~mask) | set); |
| 1013 | } |
Ramon Fried | 65f2266 | 2022-06-05 03:44:15 +0300 | [diff] [blame] | 1014 | |
| 1015 | /** |
| 1016 | * phy_read - Convenience function for reading a given PHY register |
| 1017 | * @phydev: the phy_device struct |
| 1018 | * @devad: The MMD to read from |
| 1019 | * @regnum: register number to read |
| 1020 | * @return: value for success or negative errno for failure |
| 1021 | */ |
| 1022 | int phy_read(struct phy_device *phydev, int devad, int regnum) |
| 1023 | { |
| 1024 | struct mii_dev *bus = phydev->bus; |
| 1025 | |
| 1026 | if (!bus || !bus->read) { |
| 1027 | debug("%s: No bus configured\n", __func__); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | |
| 1031 | return bus->read(bus, phydev->addr, devad, regnum); |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * phy_write - Convenience function for writing a given PHY register |
| 1036 | * @phydev: the phy_device struct |
| 1037 | * @devad: The MMD to read from |
| 1038 | * @regnum: register number to write |
| 1039 | * @val: value to write to @regnum |
| 1040 | * @return: 0 for success or negative errno for failure |
| 1041 | */ |
| 1042 | int phy_write(struct phy_device *phydev, int devad, int regnum, u16 val) |
| 1043 | { |
| 1044 | struct mii_dev *bus = phydev->bus; |
| 1045 | |
| 1046 | if (!bus || !bus->write) { |
| 1047 | debug("%s: No bus configured\n", __func__); |
| 1048 | return -1; |
| 1049 | } |
| 1050 | |
| 1051 | return bus->write(bus, phydev->addr, devad, regnum, val); |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * phy_mmd_start_indirect - Convenience function for writing MMD registers |
| 1056 | * @phydev: the phy_device struct |
| 1057 | * @devad: The MMD to read from |
| 1058 | * @regnum: register number to write |
| 1059 | * @return: None |
| 1060 | */ |
| 1061 | void phy_mmd_start_indirect(struct phy_device *phydev, int devad, int regnum) |
| 1062 | { |
| 1063 | /* Write the desired MMD Devad */ |
| 1064 | phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL, devad); |
| 1065 | |
| 1066 | /* Write the desired MMD register address */ |
| 1067 | phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, regnum); |
| 1068 | |
| 1069 | /* Select the Function : DATA with no post increment */ |
| 1070 | phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL, |
| 1071 | (devad | MII_MMD_CTRL_NOINCR)); |
| 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * phy_read_mmd - Convenience function for reading a register |
| 1076 | * from an MMD on a given PHY. |
| 1077 | * @phydev: The phy_device struct |
| 1078 | * @devad: The MMD to read from |
| 1079 | * @regnum: The register on the MMD to read |
| 1080 | * @return: Value for success or negative errno for failure |
| 1081 | */ |
| 1082 | int phy_read_mmd(struct phy_device *phydev, int devad, int regnum) |
| 1083 | { |
| 1084 | struct phy_driver *drv = phydev->drv; |
| 1085 | |
| 1086 | if (regnum > (u16)~0 || devad > 32) |
| 1087 | return -EINVAL; |
| 1088 | |
| 1089 | /* driver-specific access */ |
| 1090 | if (drv->read_mmd) |
| 1091 | return drv->read_mmd(phydev, devad, regnum); |
| 1092 | |
| 1093 | /* direct C45 / C22 access */ |
| 1094 | if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES || |
| 1095 | devad == MDIO_DEVAD_NONE || !devad) |
| 1096 | return phy_read(phydev, devad, regnum); |
| 1097 | |
| 1098 | /* indirect C22 access */ |
| 1099 | phy_mmd_start_indirect(phydev, devad, regnum); |
| 1100 | |
| 1101 | /* Read the content of the MMD's selected register */ |
| 1102 | return phy_read(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA); |
| 1103 | } |
| 1104 | |
| 1105 | /** |
| 1106 | * phy_write_mmd - Convenience function for writing a register |
| 1107 | * on an MMD on a given PHY. |
| 1108 | * @phydev: The phy_device struct |
| 1109 | * @devad: The MMD to read from |
| 1110 | * @regnum: The register on the MMD to read |
| 1111 | * @val: value to write to @regnum |
| 1112 | * @return: 0 for success or negative errno for failure |
| 1113 | */ |
| 1114 | int phy_write_mmd(struct phy_device *phydev, int devad, int regnum, u16 val) |
| 1115 | { |
| 1116 | struct phy_driver *drv = phydev->drv; |
| 1117 | |
| 1118 | if (regnum > (u16)~0 || devad > 32) |
| 1119 | return -EINVAL; |
| 1120 | |
| 1121 | /* driver-specific access */ |
| 1122 | if (drv->write_mmd) |
| 1123 | return drv->write_mmd(phydev, devad, regnum, val); |
| 1124 | |
| 1125 | /* direct C45 / C22 access */ |
| 1126 | if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES || |
| 1127 | devad == MDIO_DEVAD_NONE || !devad) |
| 1128 | return phy_write(phydev, devad, regnum, val); |
| 1129 | |
| 1130 | /* indirect C22 access */ |
| 1131 | phy_mmd_start_indirect(phydev, devad, regnum); |
| 1132 | |
| 1133 | /* Write the data into MMD's selected register */ |
| 1134 | return phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, val); |
| 1135 | } |
| 1136 | |
| 1137 | /** |
| 1138 | * phy_set_bits_mmd - Convenience function for setting bits in a register |
| 1139 | * on MMD |
| 1140 | * @phydev: the phy_device struct |
| 1141 | * @devad: the MMD containing register to modify |
| 1142 | * @regnum: register number to modify |
| 1143 | * @val: bits to set |
| 1144 | * @return: 0 for success or negative errno for failure |
| 1145 | */ |
| 1146 | int phy_set_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) |
| 1147 | { |
| 1148 | int value, ret; |
| 1149 | |
| 1150 | value = phy_read_mmd(phydev, devad, regnum); |
| 1151 | if (value < 0) |
| 1152 | return value; |
| 1153 | |
| 1154 | value |= val; |
| 1155 | |
| 1156 | ret = phy_write_mmd(phydev, devad, regnum, value); |
| 1157 | if (ret < 0) |
| 1158 | return ret; |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | /** |
| 1164 | * phy_clear_bits_mmd - Convenience function for clearing bits in a register |
| 1165 | * on MMD |
| 1166 | * @phydev: the phy_device struct |
| 1167 | * @devad: the MMD containing register to modify |
| 1168 | * @regnum: register number to modify |
| 1169 | * @val: bits to clear |
| 1170 | * @return: 0 for success or negative errno for failure |
| 1171 | */ |
| 1172 | int phy_clear_bits_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) |
| 1173 | { |
| 1174 | int value, ret; |
| 1175 | |
| 1176 | value = phy_read_mmd(phydev, devad, regnum); |
| 1177 | if (value < 0) |
| 1178 | return value; |
| 1179 | |
| 1180 | value &= ~val; |
| 1181 | |
| 1182 | ret = phy_write_mmd(phydev, devad, regnum, value); |
| 1183 | if (ret < 0) |
| 1184 | return ret; |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
Samuel Mendoza-Jonas | 09bd3d0 | 2022-08-08 21:46:03 +0930 | [diff] [blame] | 1188 | |
Marek Vasut | 87b7502 | 2023-03-19 18:08:07 +0100 | [diff] [blame] | 1189 | /** |
| 1190 | * phy_modify_mmd_changed - Function for modifying a register on MMD |
| 1191 | * @phydev: the phy_device struct |
| 1192 | * @devad: the MMD containing register to modify |
| 1193 | * @regnum: register number to modify |
| 1194 | * @mask: bit mask of bits to clear |
| 1195 | * @set: new value of bits set in mask to write to @regnum |
| 1196 | * |
| 1197 | * NOTE: MUST NOT be called from interrupt context, |
| 1198 | * because the bus read/write functions may wait for an interrupt |
| 1199 | * to conclude the operation. |
| 1200 | * |
| 1201 | * Returns negative errno, 0 if there was no change, and 1 in case of change |
| 1202 | */ |
| 1203 | int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum, |
| 1204 | u16 mask, u16 set) |
| 1205 | { |
| 1206 | int new, ret; |
| 1207 | |
| 1208 | ret = phy_read_mmd(phydev, devad, regnum); |
| 1209 | if (ret < 0) |
| 1210 | return ret; |
| 1211 | |
| 1212 | new = (ret & ~mask) | set; |
| 1213 | if (new == ret) |
| 1214 | return 0; |
| 1215 | |
| 1216 | ret = phy_write_mmd(phydev, devad, regnum, new); |
| 1217 | |
| 1218 | return ret < 0 ? ret : 1; |
| 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * phy_modify_mmd - Convenience function for modifying a register on MMD |
| 1223 | * @phydev: the phy_device struct |
| 1224 | * @devad: the MMD containing register to modify |
| 1225 | * @regnum: register number to modify |
| 1226 | * @mask: bit mask of bits to clear |
| 1227 | * @set: new value of bits set in mask to write to @regnum |
| 1228 | * |
| 1229 | * NOTE: MUST NOT be called from interrupt context, |
| 1230 | * because the bus read/write functions may wait for an interrupt |
| 1231 | * to conclude the operation. |
| 1232 | */ |
| 1233 | int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum, |
| 1234 | u16 mask, u16 set) |
| 1235 | { |
| 1236 | int ret; |
| 1237 | |
| 1238 | ret = phy_modify_mmd_changed(phydev, devad, regnum, mask, set); |
| 1239 | |
| 1240 | return ret < 0 ? ret : 0; |
| 1241 | } |
| 1242 | |
Samuel Mendoza-Jonas | 09bd3d0 | 2022-08-08 21:46:03 +0930 | [diff] [blame] | 1243 | bool phy_interface_is_ncsi(void) |
| 1244 | { |
Marek Vasut | 75d2889 | 2023-03-21 18:25:54 +0100 | [diff] [blame] | 1245 | #ifdef CONFIG_PHY_NCSI |
Samuel Mendoza-Jonas | 09bd3d0 | 2022-08-08 21:46:03 +0930 | [diff] [blame] | 1246 | struct eth_pdata *pdata = dev_get_plat(eth_get_dev()); |
| 1247 | |
| 1248 | return pdata->phy_interface == PHY_INTERFACE_MODE_NCSI; |
Marek Vasut | 75d2889 | 2023-03-21 18:25:54 +0100 | [diff] [blame] | 1249 | #else |
| 1250 | return 0; |
| 1251 | #endif |
Samuel Mendoza-Jonas | 09bd3d0 | 2022-08-08 21:46:03 +0930 | [diff] [blame] | 1252 | } |