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