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 | */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
Simon Glass | dbad346 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Andy Fleming | aecf6fc | 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 | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 20 | #include <asm/global_data.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 23 | #include <linux/err.h> |
Shengzhou Liu | fcfc786 | 2014-04-11 16:14:17 +0800 | [diff] [blame] | 24 | #include <linux/compiler.h> |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 25 | |
Michal Simek | 5f67631 | 2015-05-13 13:40:40 +0200 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 28 | /* Generic PHY support and helper functions */ |
| 29 | |
| 30 | /** |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 31 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 32 | * @phydev: target phy_device struct |
| 33 | * |
| 34 | * Description: Writes MII_ADVERTISE with the appropriate values, |
| 35 | * after sanitizing the values to make sure we only advertise |
| 36 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 37 | * hasn't changed, and > 0 if it has changed. |
| 38 | */ |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 39 | static int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 40 | { |
| 41 | u32 advertise; |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 42 | int oldadv, adv, bmsr; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 43 | int err, changed = 0; |
| 44 | |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 45 | /* Only allow advertising what this PHY supports */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 46 | phydev->advertising &= phydev->supported; |
| 47 | advertise = phydev->advertising; |
| 48 | |
| 49 | /* Setup standard advertisement */ |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 50 | adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); |
| 51 | oldadv = adv; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 52 | |
| 53 | if (adv < 0) |
| 54 | return adv; |
| 55 | |
| 56 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
| 57 | ADVERTISE_PAUSE_ASYM); |
| 58 | if (advertise & ADVERTISED_10baseT_Half) |
| 59 | adv |= ADVERTISE_10HALF; |
| 60 | if (advertise & ADVERTISED_10baseT_Full) |
| 61 | adv |= ADVERTISE_10FULL; |
| 62 | if (advertise & ADVERTISED_100baseT_Half) |
| 63 | adv |= ADVERTISE_100HALF; |
| 64 | if (advertise & ADVERTISED_100baseT_Full) |
| 65 | adv |= ADVERTISE_100FULL; |
| 66 | if (advertise & ADVERTISED_Pause) |
| 67 | adv |= ADVERTISE_PAUSE_CAP; |
| 68 | if (advertise & ADVERTISED_Asym_Pause) |
| 69 | adv |= ADVERTISE_PAUSE_ASYM; |
Charles Coldwell | 2332941 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 70 | if (advertise & ADVERTISED_1000baseX_Half) |
| 71 | adv |= ADVERTISE_1000XHALF; |
| 72 | if (advertise & ADVERTISED_1000baseX_Full) |
| 73 | adv |= ADVERTISE_1000XFULL; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 74 | |
| 75 | if (adv != oldadv) { |
| 76 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv); |
| 77 | |
| 78 | if (err < 0) |
| 79 | return err; |
| 80 | changed = 1; |
| 81 | } |
| 82 | |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 83 | bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 84 | if (bmsr < 0) |
| 85 | return bmsr; |
| 86 | |
| 87 | /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all |
| 88 | * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a |
| 89 | * logical 1. |
| 90 | */ |
| 91 | if (!(bmsr & BMSR_ESTATEN)) |
| 92 | return changed; |
| 93 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 94 | /* Configure gigabit if it's supported */ |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 95 | adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000); |
| 96 | oldadv = adv; |
| 97 | |
| 98 | if (adv < 0) |
| 99 | return adv; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 100 | |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 101 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
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 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
| 104 | SUPPORTED_1000baseT_Full)) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 105 | if (advertise & SUPPORTED_1000baseT_Half) |
| 106 | adv |= ADVERTISE_1000HALF; |
| 107 | if (advertise & SUPPORTED_1000baseT_Full) |
| 108 | adv |= ADVERTISE_1000FULL; |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 109 | } |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 110 | |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 111 | if (adv != oldadv) |
| 112 | changed = 1; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 113 | |
Florian Fainelli | 6c8be84 | 2016-01-13 16:59:31 +0300 | [diff] [blame] | 114 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv); |
| 115 | if (err < 0) |
| 116 | return err; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 117 | |
| 118 | return changed; |
| 119 | } |
| 120 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 121 | /** |
| 122 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 123 | * @phydev: target phy_device struct |
| 124 | * |
| 125 | * Description: Configures MII_BMCR to force speed/duplex |
| 126 | * to the values in phydev. Assumes that the values are valid. |
| 127 | */ |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 128 | static int genphy_setup_forced(struct phy_device *phydev) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 129 | { |
| 130 | int err; |
Alexandre Messier | 103a8c4 | 2016-01-22 14:16:15 -0500 | [diff] [blame] | 131 | int ctl = BMCR_ANRESTART; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 132 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 133 | phydev->pause = 0; |
| 134 | phydev->asym_pause = 0; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 135 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 136 | if (phydev->speed == SPEED_1000) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 137 | ctl |= BMCR_SPEED1000; |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 138 | else if (phydev->speed == SPEED_100) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 139 | ctl |= BMCR_SPEED100; |
| 140 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 141 | if (phydev->duplex == DUPLEX_FULL) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 142 | ctl |= BMCR_FULLDPLX; |
| 143 | |
| 144 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); |
| 145 | |
| 146 | return err; |
| 147 | } |
| 148 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 149 | /** |
| 150 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 151 | * @phydev: target phy_device struct |
| 152 | */ |
| 153 | int genphy_restart_aneg(struct phy_device *phydev) |
| 154 | { |
| 155 | int ctl; |
| 156 | |
| 157 | ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 158 | |
| 159 | if (ctl < 0) |
| 160 | return ctl; |
| 161 | |
| 162 | ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); |
| 163 | |
| 164 | /* Don't isolate the PHY if we're negotiating */ |
| 165 | ctl &= ~(BMCR_ISOLATE); |
| 166 | |
| 167 | ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); |
| 168 | |
| 169 | return ctl; |
| 170 | } |
| 171 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 172 | /** |
| 173 | * genphy_config_aneg - restart auto-negotiation or write BMCR |
| 174 | * @phydev: target phy_device struct |
| 175 | * |
| 176 | * Description: If auto-negotiation is enabled, we configure the |
| 177 | * advertising, and then restart auto-negotiation. If it is not |
| 178 | * enabled, then we write the BMCR. |
| 179 | */ |
| 180 | int genphy_config_aneg(struct phy_device *phydev) |
| 181 | { |
| 182 | int result; |
| 183 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 184 | if (phydev->autoneg != AUTONEG_ENABLE) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 185 | return genphy_setup_forced(phydev); |
| 186 | |
| 187 | result = genphy_config_advert(phydev); |
| 188 | |
| 189 | if (result < 0) /* error */ |
| 190 | return result; |
| 191 | |
| 192 | if (result == 0) { |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 193 | /* |
| 194 | * Advertisment hasn't changed, but maybe aneg was never on to |
| 195 | * begin with? Or maybe phy was isolated? |
| 196 | */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 197 | int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 198 | |
| 199 | if (ctl < 0) |
| 200 | return ctl; |
| 201 | |
| 202 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) |
| 203 | result = 1; /* do restart aneg */ |
| 204 | } |
| 205 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 206 | /* |
| 207 | * Only restart aneg if we are advertising something different |
| 208 | * than we were before. |
| 209 | */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 210 | if (result > 0) |
| 211 | result = genphy_restart_aneg(phydev); |
| 212 | |
| 213 | return result; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * genphy_update_link - update link status in @phydev |
| 218 | * @phydev: target phy_device struct |
| 219 | * |
| 220 | * Description: Update the value in phydev->link to reflect the |
| 221 | * current link value. In order to do this, we need to read |
| 222 | * the status register twice, keeping the second value. |
| 223 | */ |
| 224 | int genphy_update_link(struct phy_device *phydev) |
| 225 | { |
| 226 | unsigned int mii_reg; |
| 227 | |
| 228 | /* |
| 229 | * Wait if the link is up, and autonegotiation is in progress |
| 230 | * (ie - we're capable and it's not done) |
| 231 | */ |
| 232 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 233 | |
| 234 | /* |
| 235 | * If we already saw the link up, and it hasn't gone down, then |
| 236 | * we don't need to wait for autoneg again |
| 237 | */ |
| 238 | if (phydev->link && mii_reg & BMSR_LSTATUS) |
| 239 | return 0; |
| 240 | |
Alexandre Messier | 010c5ec | 2016-01-22 14:16:56 -0500 | [diff] [blame] | 241 | if ((phydev->autoneg == AUTONEG_ENABLE) && |
| 242 | !(mii_reg & BMSR_ANEGCOMPLETE)) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 243 | int i = 0; |
| 244 | |
| 245 | printf("%s Waiting for PHY auto negotiation to complete", |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 246 | phydev->dev->name); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 247 | while (!(mii_reg & BMSR_ANEGCOMPLETE)) { |
| 248 | /* |
| 249 | * Timeout reached ? |
| 250 | */ |
Andre Przywara | 71a6a60 | 2020-01-03 22:08:47 +0000 | [diff] [blame] | 251 | if (i > (PHY_ANEG_TIMEOUT / 50)) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 252 | printf(" TIMEOUT !\n"); |
| 253 | phydev->link = 0; |
Michal Simek | cf6677b | 2016-05-18 12:48:57 +0200 | [diff] [blame] | 254 | return -ETIMEDOUT; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | if (ctrlc()) { |
| 258 | puts("user interrupt!\n"); |
| 259 | phydev->link = 0; |
| 260 | return -EINTR; |
| 261 | } |
| 262 | |
Stefan Roese | 5cf96d1 | 2019-09-30 10:26:42 +0200 | [diff] [blame] | 263 | if ((i++ % 10) == 0) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 264 | printf("."); |
| 265 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 266 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
Stefan Roese | 5cf96d1 | 2019-09-30 10:26:42 +0200 | [diff] [blame] | 267 | mdelay(50); /* 50 ms */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 268 | } |
| 269 | printf(" done\n"); |
| 270 | phydev->link = 1; |
| 271 | } else { |
| 272 | /* Read the link a second time to clear the latched state */ |
| 273 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 274 | |
| 275 | if (mii_reg & BMSR_LSTATUS) |
| 276 | phydev->link = 1; |
| 277 | else |
| 278 | phydev->link = 0; |
| 279 | } |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Generic function which updates the speed and duplex. If |
| 286 | * autonegotiation is enabled, it uses the AND of the link |
| 287 | * partner's advertised capabilities and our advertised |
| 288 | * capabilities. If autonegotiation is disabled, we use the |
| 289 | * appropriate bits in the control register. |
| 290 | * |
| 291 | * Stolen from Linux's mii.c and phy_device.c |
| 292 | */ |
Yegor Yefremov | c40f5d3 | 2012-11-28 11:15:17 +0100 | [diff] [blame] | 293 | int genphy_parse_link(struct phy_device *phydev) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 294 | { |
| 295 | int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 296 | |
| 297 | /* We're using autonegotiation */ |
Alexandre Messier | 010c5ec | 2016-01-22 14:16:56 -0500 | [diff] [blame] | 298 | if (phydev->autoneg == AUTONEG_ENABLE) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 299 | u32 lpa = 0; |
Heiko Schocher | 94c3502 | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 300 | int gblpa = 0; |
Charles Coldwell | 2332941 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 301 | u32 estatus = 0; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 302 | |
| 303 | /* Check for gigabit capability */ |
David Dueck | ed45423 | 2013-11-05 17:23:02 +0100 | [diff] [blame] | 304 | if (phydev->supported & (SUPPORTED_1000baseT_Full | |
| 305 | SUPPORTED_1000baseT_Half)) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 306 | /* We want a list of states supported by |
| 307 | * both PHYs in the link |
| 308 | */ |
| 309 | gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000); |
Heiko Schocher | 94c3502 | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 310 | if (gblpa < 0) { |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 311 | debug("Could not read MII_STAT1000. "); |
| 312 | debug("Ignoring gigabit capability\n"); |
Heiko Schocher | 94c3502 | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 313 | gblpa = 0; |
| 314 | } |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 315 | gblpa &= phy_read(phydev, |
| 316 | MDIO_DEVAD_NONE, MII_CTRL1000) << 2; |
| 317 | } |
| 318 | |
| 319 | /* Set the baseline so we only have to set them |
| 320 | * if they're different |
| 321 | */ |
| 322 | phydev->speed = SPEED_10; |
| 323 | phydev->duplex = DUPLEX_HALF; |
| 324 | |
| 325 | /* Check the gigabit fields */ |
| 326 | if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) { |
| 327 | phydev->speed = SPEED_1000; |
| 328 | |
| 329 | if (gblpa & PHY_1000BTSR_1000FD) |
| 330 | phydev->duplex = DUPLEX_FULL; |
| 331 | |
| 332 | /* We're done! */ |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); |
| 337 | lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA); |
| 338 | |
Wolfgang Denk | a7b06ce | 2011-09-28 21:02:43 +0200 | [diff] [blame] | 339 | if (lpa & (LPA_100FULL | LPA_100HALF)) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 340 | phydev->speed = SPEED_100; |
| 341 | |
Wolfgang Denk | a7b06ce | 2011-09-28 21:02:43 +0200 | [diff] [blame] | 342 | if (lpa & LPA_100FULL) |
| 343 | phydev->duplex = DUPLEX_FULL; |
| 344 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 345 | } else if (lpa & LPA_10FULL) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 346 | phydev->duplex = DUPLEX_FULL; |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 347 | } |
Charles Coldwell | 2332941 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 348 | |
Sascha Silbe | 2ac8d30 | 2013-07-19 12:25:10 +0200 | [diff] [blame] | 349 | /* |
| 350 | * Extended status may indicate that the PHY supports |
| 351 | * 1000BASE-T/X even though the 1000BASE-T registers |
| 352 | * are missing. In this case we can't tell whether the |
| 353 | * peer also supports it, so we only check extended |
| 354 | * status if the 1000BASE-T registers are actually |
| 355 | * missing. |
| 356 | */ |
| 357 | if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP)) |
Charles Coldwell | 2332941 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 358 | estatus = phy_read(phydev, MDIO_DEVAD_NONE, |
| 359 | MII_ESTATUS); |
| 360 | |
| 361 | if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF | |
| 362 | ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) { |
| 363 | phydev->speed = SPEED_1000; |
| 364 | if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL)) |
| 365 | phydev->duplex = DUPLEX_FULL; |
| 366 | } |
| 367 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 368 | } else { |
| 369 | u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 370 | |
| 371 | phydev->speed = SPEED_10; |
| 372 | phydev->duplex = DUPLEX_HALF; |
| 373 | |
| 374 | if (bmcr & BMCR_FULLDPLX) |
| 375 | phydev->duplex = DUPLEX_FULL; |
| 376 | |
| 377 | if (bmcr & BMCR_SPEED1000) |
| 378 | phydev->speed = SPEED_1000; |
| 379 | else if (bmcr & BMCR_SPEED100) |
| 380 | phydev->speed = SPEED_100; |
| 381 | } |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | int genphy_config(struct phy_device *phydev) |
| 387 | { |
| 388 | int val; |
| 389 | u32 features; |
| 390 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 391 | features = (SUPPORTED_TP | SUPPORTED_MII |
| 392 | | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 393 | SUPPORTED_BNC); |
| 394 | |
| 395 | /* Do we support autonegotiation? */ |
| 396 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 397 | |
| 398 | if (val < 0) |
| 399 | return val; |
| 400 | |
| 401 | if (val & BMSR_ANEGCAPABLE) |
| 402 | features |= SUPPORTED_Autoneg; |
| 403 | |
| 404 | if (val & BMSR_100FULL) |
| 405 | features |= SUPPORTED_100baseT_Full; |
| 406 | if (val & BMSR_100HALF) |
| 407 | features |= SUPPORTED_100baseT_Half; |
| 408 | if (val & BMSR_10FULL) |
| 409 | features |= SUPPORTED_10baseT_Full; |
| 410 | if (val & BMSR_10HALF) |
| 411 | features |= SUPPORTED_10baseT_Half; |
| 412 | |
| 413 | if (val & BMSR_ESTATEN) { |
| 414 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS); |
| 415 | |
| 416 | if (val < 0) |
| 417 | return val; |
| 418 | |
| 419 | if (val & ESTATUS_1000_TFULL) |
| 420 | features |= SUPPORTED_1000baseT_Full; |
| 421 | if (val & ESTATUS_1000_THALF) |
| 422 | features |= SUPPORTED_1000baseT_Half; |
Charles Coldwell | 2332941 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 423 | if (val & ESTATUS_1000_XFULL) |
| 424 | features |= SUPPORTED_1000baseX_Full; |
| 425 | if (val & ESTATUS_1000_XHALF) |
Fabio Estevam | 45d601e | 2013-07-19 10:01:34 -0300 | [diff] [blame] | 426 | features |= SUPPORTED_1000baseX_Half; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Sascha Hauer | 6cc1e7d | 2016-01-13 16:59:32 +0300 | [diff] [blame] | 429 | phydev->supported &= features; |
| 430 | phydev->advertising &= features; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 431 | |
| 432 | genphy_config_aneg(phydev); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | int genphy_startup(struct phy_device *phydev) |
| 438 | { |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 439 | int ret; |
| 440 | |
| 441 | ret = genphy_update_link(phydev); |
| 442 | if (ret) |
| 443 | return ret; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 444 | |
Michal Simek | 5ff8966 | 2016-05-18 12:46:12 +0200 | [diff] [blame] | 445 | return genphy_parse_link(phydev); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | int genphy_shutdown(struct phy_device *phydev) |
| 449 | { |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static struct phy_driver genphy_driver = { |
| 454 | .uid = 0xffffffff, |
| 455 | .mask = 0xffffffff, |
| 456 | .name = "Generic PHY", |
Sascha Hauer | 6cc1e7d | 2016-01-13 16:59:32 +0300 | [diff] [blame] | 457 | .features = PHY_GBIT_FEATURES | SUPPORTED_MII | |
| 458 | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 459 | SUPPORTED_BNC, |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 460 | .config = genphy_config, |
| 461 | .startup = genphy_startup, |
| 462 | .shutdown = genphy_shutdown, |
| 463 | }; |
| 464 | |
Siva Durga Prasad Paladugu | 1daad9e | 2019-03-15 17:46:47 +0530 | [diff] [blame] | 465 | int genphy_init(void) |
| 466 | { |
| 467 | return phy_register(&genphy_driver); |
| 468 | } |
| 469 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 470 | static LIST_HEAD(phy_drivers); |
| 471 | |
| 472 | int phy_init(void) |
| 473 | { |
Siva Durga Prasad Paladugu | f113754 | 2019-03-04 16:01:30 +0100 | [diff] [blame] | 474 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 475 | /* |
| 476 | * The pointers inside phy_drivers also needs to be updated incase of |
| 477 | * manual reloc, without which these points to some invalid |
| 478 | * pre reloc address and leads to invalid accesses, hangs. |
| 479 | */ |
| 480 | struct list_head *head = &phy_drivers; |
| 481 | |
| 482 | head->next = (void *)head->next + gd->reloc_off; |
| 483 | head->prev = (void *)head->prev + gd->reloc_off; |
| 484 | #endif |
| 485 | |
Florian Fainelli | 01b4ade | 2017-12-09 14:59:54 -0800 | [diff] [blame] | 486 | #ifdef CONFIG_B53_SWITCH |
| 487 | phy_b53_init(); |
| 488 | #endif |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 489 | #ifdef CONFIG_MV88E61XX_SWITCH |
| 490 | phy_mv88e61xx_init(); |
| 491 | #endif |
Shaohui Xie | 0e548d7 | 2014-12-30 18:32:04 +0800 | [diff] [blame] | 492 | #ifdef CONFIG_PHY_AQUANTIA |
| 493 | phy_aquantia_init(); |
| 494 | #endif |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 495 | #ifdef CONFIG_PHY_ATHEROS |
| 496 | phy_atheros_init(); |
| 497 | #endif |
| 498 | #ifdef CONFIG_PHY_BROADCOM |
| 499 | phy_broadcom_init(); |
| 500 | #endif |
Shengzhou Liu | c878bdb | 2014-11-10 18:32:29 +0800 | [diff] [blame] | 501 | #ifdef CONFIG_PHY_CORTINA |
| 502 | phy_cortina_init(); |
| 503 | #endif |
Abbie Chang | 556872f | 2021-01-14 13:34:12 -0800 | [diff] [blame] | 504 | #ifdef CONFIG_PHY_CORTINA_ACCESS |
| 505 | phy_cortina_access_init(); |
| 506 | #endif |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 507 | #ifdef CONFIG_PHY_DAVICOM |
| 508 | phy_davicom_init(); |
| 509 | #endif |
Matt Porter | 3bbeb79 | 2013-03-20 05:38:13 +0000 | [diff] [blame] | 510 | #ifdef CONFIG_PHY_ET1011C |
| 511 | phy_et1011c_init(); |
| 512 | #endif |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 513 | #ifdef CONFIG_PHY_LXT |
| 514 | phy_lxt_init(); |
| 515 | #endif |
| 516 | #ifdef CONFIG_PHY_MARVELL |
| 517 | phy_marvell_init(); |
| 518 | #endif |
Alexandru Gagniuc | 757bb67 | 2017-07-07 11:36:57 -0700 | [diff] [blame] | 519 | #ifdef CONFIG_PHY_MICREL_KSZ8XXX |
| 520 | phy_micrel_ksz8xxx_init(); |
| 521 | #endif |
| 522 | #ifdef CONFIG_PHY_MICREL_KSZ90X1 |
| 523 | phy_micrel_ksz90x1_init(); |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 524 | #endif |
Neil Armstrong | 7a4c90d | 2017-10-18 10:02:10 +0200 | [diff] [blame] | 525 | #ifdef CONFIG_PHY_MESON_GXL |
| 526 | phy_meson_gxl_init(); |
| 527 | #endif |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 528 | #ifdef CONFIG_PHY_NATSEMI |
| 529 | phy_natsemi_init(); |
| 530 | #endif |
| 531 | #ifdef CONFIG_PHY_REALTEK |
| 532 | phy_realtek_init(); |
| 533 | #endif |
Nobuhiro Iwamatsu | 61134dc | 2011-11-23 21:24:15 +0000 | [diff] [blame] | 534 | #ifdef CONFIG_PHY_SMSC |
| 535 | phy_smsc_init(); |
| 536 | #endif |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 537 | #ifdef CONFIG_PHY_TERANETICS |
| 538 | phy_teranetics_init(); |
| 539 | #endif |
Edgar E. Iglesias | 8d3ce68 | 2015-09-25 23:46:08 -0700 | [diff] [blame] | 540 | #ifdef CONFIG_PHY_TI |
| 541 | phy_ti_init(); |
| 542 | #endif |
Andy Fleming | 60ca78b | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 543 | #ifdef CONFIG_PHY_VITESSE |
| 544 | phy_vitesse_init(); |
| 545 | #endif |
Siva Durga Prasad Paladugu | dd6cbd3 | 2016-02-05 13:22:10 +0530 | [diff] [blame] | 546 | #ifdef CONFIG_PHY_XILINX |
| 547 | phy_xilinx_init(); |
| 548 | #endif |
John Haechten | ee253f9 | 2016-12-09 22:15:17 +0000 | [diff] [blame] | 549 | #ifdef CONFIG_PHY_MSCC |
| 550 | phy_mscc_init(); |
| 551 | #endif |
Hannes Schmelzer | da49460 | 2017-03-23 15:11:43 +0100 | [diff] [blame] | 552 | #ifdef CONFIG_PHY_FIXED |
| 553 | phy_fixed_init(); |
| 554 | #endif |
Samuel Mendoza-Jonas | b069c4a | 2019-06-18 11:37:18 +1000 | [diff] [blame] | 555 | #ifdef CONFIG_PHY_NCSI |
| 556 | phy_ncsi_init(); |
| 557 | #endif |
Siva Durga Prasad Paladugu | d5c4e1e | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 558 | #ifdef CONFIG_PHY_XILINX_GMII2RGMII |
| 559 | phy_xilinx_gmii2rgmii_init(); |
| 560 | #endif |
Siva Durga Prasad Paladugu | 1daad9e | 2019-03-15 17:46:47 +0530 | [diff] [blame] | 561 | genphy_init(); |
| 562 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | int phy_register(struct phy_driver *drv) |
| 567 | { |
| 568 | INIT_LIST_HEAD(&drv->list); |
| 569 | list_add_tail(&drv->list, &phy_drivers); |
| 570 | |
Michal Simek | 5f67631 | 2015-05-13 13:40:40 +0200 | [diff] [blame] | 571 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 572 | if (drv->probe) |
| 573 | drv->probe += gd->reloc_off; |
| 574 | if (drv->config) |
| 575 | drv->config += gd->reloc_off; |
| 576 | if (drv->startup) |
| 577 | drv->startup += gd->reloc_off; |
| 578 | if (drv->shutdown) |
| 579 | drv->shutdown += gd->reloc_off; |
| 580 | if (drv->readext) |
| 581 | drv->readext += gd->reloc_off; |
| 582 | if (drv->writeext) |
| 583 | drv->writeext += gd->reloc_off; |
Carlo Caione | 4de87e2 | 2019-02-08 17:25:06 +0000 | [diff] [blame] | 584 | if (drv->read_mmd) |
| 585 | drv->read_mmd += gd->reloc_off; |
| 586 | if (drv->write_mmd) |
| 587 | drv->write_mmd += gd->reloc_off; |
Michal Simek | 5f67631 | 2015-05-13 13:40:40 +0200 | [diff] [blame] | 588 | #endif |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Alexey Brodkin | e476bb2 | 2016-01-13 16:59:34 +0300 | [diff] [blame] | 592 | int phy_set_supported(struct phy_device *phydev, u32 max_speed) |
| 593 | { |
| 594 | /* The default values for phydev->supported are provided by the PHY |
| 595 | * driver "features" member, we want to reset to sane defaults first |
| 596 | * before supporting higher speeds. |
| 597 | */ |
| 598 | phydev->supported &= PHY_DEFAULT_FEATURES; |
| 599 | |
| 600 | switch (max_speed) { |
| 601 | default: |
| 602 | return -ENOTSUPP; |
| 603 | case SPEED_1000: |
| 604 | phydev->supported |= PHY_1000BT_FEATURES; |
| 605 | /* fall through */ |
| 606 | case SPEED_100: |
| 607 | phydev->supported |= PHY_100BT_FEATURES; |
| 608 | /* fall through */ |
| 609 | case SPEED_10: |
| 610 | phydev->supported |= PHY_10BT_FEATURES; |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 616 | static int phy_probe(struct phy_device *phydev) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 617 | { |
| 618 | int err = 0; |
| 619 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 620 | phydev->advertising = phydev->drv->features; |
| 621 | phydev->supported = phydev->drv->features; |
| 622 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 623 | phydev->mmds = phydev->drv->mmds; |
| 624 | |
| 625 | if (phydev->drv->probe) |
| 626 | err = phydev->drv->probe(phydev); |
| 627 | |
| 628 | return err; |
| 629 | } |
| 630 | |
| 631 | static struct phy_driver *generic_for_interface(phy_interface_t interface) |
| 632 | { |
| 633 | #ifdef CONFIG_PHYLIB_10G |
| 634 | if (is_10g_interface(interface)) |
| 635 | return &gen10g_driver; |
| 636 | #endif |
| 637 | |
| 638 | return &genphy_driver; |
| 639 | } |
| 640 | |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 641 | static struct phy_driver *get_phy_driver(struct phy_device *phydev, |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 642 | phy_interface_t interface) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 643 | { |
| 644 | struct list_head *entry; |
| 645 | int phy_id = phydev->phy_id; |
| 646 | struct phy_driver *drv = NULL; |
| 647 | |
| 648 | list_for_each(entry, &phy_drivers) { |
| 649 | drv = list_entry(entry, struct phy_driver, list); |
| 650 | if ((drv->uid & drv->mask) == (phy_id & drv->mask)) |
| 651 | return drv; |
| 652 | } |
| 653 | |
| 654 | /* If we made it here, there's no driver for this PHY */ |
| 655 | return generic_for_interface(interface); |
| 656 | } |
| 657 | |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 658 | static struct phy_device *phy_device_create(struct mii_dev *bus, int addr, |
Pankaj Bansal | 3c43a48 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 659 | u32 phy_id, bool is_c45, |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 660 | phy_interface_t interface) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 661 | { |
| 662 | struct phy_device *dev; |
| 663 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 664 | /* |
| 665 | * We allocate the device, and initialize the |
| 666 | * default values |
| 667 | */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 668 | dev = malloc(sizeof(*dev)); |
| 669 | if (!dev) { |
| 670 | printf("Failed to allocate PHY device for %s:%d\n", |
Vladimir Oltean | fb73b12 | 2020-07-16 18:09:08 +0800 | [diff] [blame] | 671 | bus ? bus->name : "(null bus)", addr); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 672 | return NULL; |
| 673 | } |
| 674 | |
| 675 | memset(dev, 0, sizeof(*dev)); |
| 676 | |
| 677 | dev->duplex = -1; |
Mugunthan V N | bbc97ba | 2015-09-03 15:50:21 +0530 | [diff] [blame] | 678 | dev->link = 0; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 679 | dev->interface = interface; |
| 680 | |
Grygorii Strashko | 6189c06 | 2018-07-05 12:02:48 -0500 | [diff] [blame] | 681 | #ifdef CONFIG_DM_ETH |
| 682 | dev->node = ofnode_null(); |
| 683 | #endif |
| 684 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 685 | dev->autoneg = AUTONEG_ENABLE; |
| 686 | |
| 687 | dev->addr = addr; |
| 688 | dev->phy_id = phy_id; |
Pankaj Bansal | 3c43a48 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 689 | dev->is_c45 = is_c45; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 690 | dev->bus = bus; |
| 691 | |
| 692 | dev->drv = get_phy_driver(dev, interface); |
| 693 | |
Siva Durga Prasad Paladugu | a722848 | 2019-03-04 16:02:11 +0100 | [diff] [blame] | 694 | if (phy_probe(dev)) { |
| 695 | printf("%s, PHY probe failed\n", __func__); |
| 696 | return NULL; |
| 697 | } |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 698 | |
Vladimir Oltean | fb73b12 | 2020-07-16 18:09:08 +0800 | [diff] [blame] | 699 | if (addr >= 0 && addr < PHY_MAX_ADDR && phy_id != PHY_FIXED_ID) |
Michal Simek | 02a99d7 | 2018-12-19 16:57:38 +0100 | [diff] [blame] | 700 | bus->phymap[addr] = dev; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 701 | |
| 702 | return dev; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * get_phy_id - reads the specified addr for its ID. |
| 707 | * @bus: the target MII bus |
| 708 | * @addr: PHY address on the MII bus |
| 709 | * @phy_id: where to store the ID retrieved. |
| 710 | * |
| 711 | * Description: Reads the ID registers of the PHY at @addr on the |
| 712 | * @bus, stores it in @phy_id and returns zero on success. |
| 713 | */ |
Shengzhou Liu | 072b0fa | 2015-04-07 18:46:32 +0800 | [diff] [blame] | 714 | 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] | 715 | { |
| 716 | int phy_reg; |
| 717 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 718 | /* |
| 719 | * Grab the bits from PHYIR1, and put them |
| 720 | * in the upper half |
| 721 | */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 722 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID1); |
| 723 | |
| 724 | if (phy_reg < 0) |
| 725 | return -EIO; |
| 726 | |
| 727 | *phy_id = (phy_reg & 0xffff) << 16; |
| 728 | |
| 729 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
| 730 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID2); |
| 731 | |
| 732 | if (phy_reg < 0) |
| 733 | return -EIO; |
| 734 | |
| 735 | *phy_id |= (phy_reg & 0xffff); |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 740 | static struct phy_device *create_phy_by_mask(struct mii_dev *bus, |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 741 | uint phy_mask, int devad, |
| 742 | phy_interface_t interface) |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 743 | { |
| 744 | u32 phy_id = 0xffffffff; |
Pankaj Bansal | 3c43a48 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 745 | bool is_c45; |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 746 | |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 747 | while (phy_mask) { |
| 748 | int addr = ffs(phy_mask) - 1; |
| 749 | int r = get_phy_id(bus, addr, devad, &phy_id); |
Alex Marginean | 920fe67 | 2019-07-05 12:28:55 +0300 | [diff] [blame] | 750 | |
| 751 | /* |
| 752 | * If the PHY ID is flat 0 we ignore it. There are C45 PHYs |
| 753 | * that return all 0s for C22 reads (like Aquantia AQR112) and |
| 754 | * there are C22 PHYs that return all 0s for C45 reads (like |
| 755 | * Atheros AR8035). |
| 756 | */ |
| 757 | if (r == 0 && phy_id == 0) |
| 758 | goto next; |
| 759 | |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 760 | /* 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] | 761 | if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff) { |
| 762 | is_c45 = (devad == MDIO_DEVAD_NONE) ? false : true; |
| 763 | return phy_device_create(bus, addr, phy_id, is_c45, |
| 764 | interface); |
| 765 | } |
Alex Marginean | 920fe67 | 2019-07-05 12:28:55 +0300 | [diff] [blame] | 766 | next: |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 767 | phy_mask &= ~(1 << addr); |
| 768 | } |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | static struct phy_device *search_for_existing_phy(struct mii_dev *bus, |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 773 | uint phy_mask, |
| 774 | phy_interface_t interface) |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 775 | { |
| 776 | /* If we have one, return the existing device, with new interface */ |
| 777 | while (phy_mask) { |
| 778 | int addr = ffs(phy_mask) - 1; |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 779 | |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 780 | if (bus->phymap[addr]) { |
| 781 | bus->phymap[addr]->interface = interface; |
| 782 | return bus->phymap[addr]; |
| 783 | } |
| 784 | phy_mask &= ~(1 << addr); |
| 785 | } |
| 786 | return NULL; |
| 787 | } |
| 788 | |
| 789 | static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus, |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 790 | uint phy_mask, |
| 791 | phy_interface_t interface) |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 792 | { |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 793 | struct phy_device *phydev; |
Florin Chiculita | 0439bee | 2020-04-29 14:25:48 +0300 | [diff] [blame] | 794 | int devad[] = { |
| 795 | /* Clause-22 */ |
| 796 | MDIO_DEVAD_NONE, |
| 797 | /* Clause-45 */ |
| 798 | MDIO_MMD_PMAPMD, |
| 799 | MDIO_MMD_WIS, |
| 800 | MDIO_MMD_PCS, |
| 801 | MDIO_MMD_PHYXS, |
| 802 | MDIO_MMD_VEND1, |
| 803 | }; |
| 804 | int i, devad_cnt; |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 805 | |
Florin Chiculita | 0439bee | 2020-04-29 14:25:48 +0300 | [diff] [blame] | 806 | devad_cnt = sizeof(devad)/sizeof(int); |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 807 | phydev = search_for_existing_phy(bus, phy_mask, interface); |
| 808 | if (phydev) |
| 809 | return phydev; |
Florin Chiculita | 0439bee | 2020-04-29 14:25:48 +0300 | [diff] [blame] | 810 | /* try different access clauses */ |
| 811 | for (i = 0; i < devad_cnt; i++) { |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 812 | phydev = create_phy_by_mask(bus, phy_mask, |
Florin Chiculita | 0439bee | 2020-04-29 14:25:48 +0300 | [diff] [blame] | 813 | devad[i], interface); |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 814 | if (IS_ERR(phydev)) |
| 815 | return NULL; |
| 816 | if (phydev) |
| 817 | return phydev; |
| 818 | } |
Bin Meng | 59c6d89 | 2015-10-07 21:19:30 -0700 | [diff] [blame] | 819 | |
| 820 | debug("\n%s PHY: ", bus->name); |
| 821 | while (phy_mask) { |
| 822 | int addr = ffs(phy_mask) - 1; |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 823 | |
Bin Meng | 59c6d89 | 2015-10-07 21:19:30 -0700 | [diff] [blame] | 824 | debug("%d ", addr); |
| 825 | phy_mask &= ~(1 << addr); |
| 826 | } |
| 827 | debug("not found\n"); |
Bin Meng | 0776c57 | 2015-10-07 21:19:29 -0700 | [diff] [blame] | 828 | |
| 829 | return NULL; |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 832 | /** |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 833 | * get_phy_device - reads the specified PHY device and returns its |
| 834 | * @phy_device struct |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 835 | * @bus: the target MII bus |
| 836 | * @addr: PHY address on the MII bus |
| 837 | * |
| 838 | * Description: Reads the ID registers of the PHY at @addr on the |
| 839 | * @bus, then allocates and returns the phy_device to represent it. |
| 840 | */ |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 841 | static struct phy_device *get_phy_device(struct mii_dev *bus, int addr, |
| 842 | phy_interface_t interface) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 843 | { |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 844 | return get_phy_device_by_mask(bus, 1 << addr, interface); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | int phy_reset(struct phy_device *phydev) |
| 848 | { |
| 849 | int reg; |
| 850 | int timeout = 500; |
| 851 | int devad = MDIO_DEVAD_NONE; |
| 852 | |
Shaohui Xie | 62a7b92 | 2016-01-28 15:55:46 +0800 | [diff] [blame] | 853 | if (phydev->flags & PHY_FLAG_BROKEN_RESET) |
| 854 | return 0; |
| 855 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 856 | #ifdef CONFIG_PHYLIB_10G |
| 857 | /* If it's 10G, we need to issue reset through one of the MMDs */ |
| 858 | if (is_10g_interface(phydev->interface)) { |
| 859 | if (!phydev->mmds) |
| 860 | gen10g_discover_mmds(phydev); |
| 861 | |
| 862 | devad = ffs(phydev->mmds) - 1; |
| 863 | } |
| 864 | #endif |
| 865 | |
Stefan Agner | e64013d | 2015-12-09 11:21:25 -0800 | [diff] [blame] | 866 | if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 867 | debug("PHY reset failed\n"); |
| 868 | return -1; |
| 869 | } |
| 870 | |
| 871 | #ifdef CONFIG_PHY_RESET_DELAY |
| 872 | udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */ |
| 873 | #endif |
| 874 | /* |
| 875 | * Poll the control register for the reset bit to go to 0 (it is |
| 876 | * auto-clearing). This should happen within 0.5 seconds per the |
| 877 | * IEEE spec. |
| 878 | */ |
Stefan Agner | e64013d | 2015-12-09 11:21:25 -0800 | [diff] [blame] | 879 | reg = phy_read(phydev, devad, MII_BMCR); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 880 | while ((reg & BMCR_RESET) && timeout--) { |
| 881 | reg = phy_read(phydev, devad, MII_BMCR); |
| 882 | |
| 883 | if (reg < 0) { |
| 884 | debug("PHY status read failed\n"); |
| 885 | return -1; |
| 886 | } |
| 887 | udelay(1000); |
| 888 | } |
| 889 | |
| 890 | if (reg & BMCR_RESET) { |
| 891 | puts("PHY reset timed out\n"); |
| 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | int miiphy_reset(const char *devname, unsigned char addr) |
| 899 | { |
| 900 | struct mii_dev *bus = miiphy_get_dev_by_name(devname); |
| 901 | struct phy_device *phydev; |
| 902 | |
| 903 | /* |
| 904 | * miiphy_reset was only used on standard PHYs, so we'll fake it here. |
| 905 | * If later code tries to connect with the right interface, this will |
| 906 | * be corrected by get_phy_device in phy_connect() |
| 907 | */ |
| 908 | phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII); |
| 909 | |
| 910 | return phy_reset(phydev); |
| 911 | } |
| 912 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 913 | struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask, |
| 914 | phy_interface_t interface) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 915 | { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 916 | /* Reset the bus */ |
Jörg Krause | 71b1d86 | 2015-07-15 15:18:22 +0200 | [diff] [blame] | 917 | if (bus->reset) { |
Vladimir Zapolskiy | 46f10bb | 2011-09-05 07:24:07 +0000 | [diff] [blame] | 918 | bus->reset(bus); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 919 | |
Jörg Krause | 71b1d86 | 2015-07-15 15:18:22 +0200 | [diff] [blame] | 920 | /* 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] | 921 | mdelay(15); |
Jörg Krause | 71b1d86 | 2015-07-15 15:18:22 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 924 | return get_phy_device_by_mask(bus, phy_mask, interface); |
| 925 | } |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 926 | |
Simon Glass | dbad346 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 927 | #ifdef CONFIG_DM_ETH |
| 928 | void phy_connect_dev(struct phy_device *phydev, struct udevice *dev) |
| 929 | #else |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 930 | void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev) |
Simon Glass | dbad346 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 931 | #endif |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 932 | { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 933 | /* Soft Reset the PHY */ |
| 934 | phy_reset(phydev); |
Bin Meng | f87a15c | 2015-10-07 21:19:31 -0700 | [diff] [blame] | 935 | if (phydev->dev && phydev->dev != dev) { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 936 | printf("%s:%d is connected to %s. Reconnecting to %s\n", |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 937 | phydev->bus->name, phydev->addr, |
| 938 | phydev->dev->name, dev->name); |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 939 | } |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 940 | phydev->dev = dev; |
Wolfgang Denk | a71ebc4 | 2011-07-24 21:39:10 +0000 | [diff] [blame] | 941 | debug("%s connected to %s\n", dev->name, phydev->drv->name); |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 942 | } |
Siva Durga Prasad Paladugu | d5c4e1e | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 943 | |
| 944 | #ifdef CONFIG_PHY_XILINX_GMII2RGMII |
| 945 | #ifdef CONFIG_DM_ETH |
| 946 | static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus, |
| 947 | struct udevice *dev, |
| 948 | phy_interface_t interface) |
| 949 | #else |
| 950 | static struct phy_device *phy_connect_gmii2rgmii(struct mii_dev *bus, |
| 951 | struct eth_device *dev, |
| 952 | phy_interface_t interface) |
| 953 | #endif |
| 954 | { |
| 955 | struct phy_device *phydev = NULL; |
| 956 | int sn = dev_of_offset(dev); |
| 957 | int off; |
| 958 | |
| 959 | while (sn > 0) { |
| 960 | off = fdt_node_offset_by_compatible(gd->fdt_blob, sn, |
| 961 | "xlnx,gmii-to-rgmii-1.0"); |
| 962 | if (off > 0) { |
| 963 | phydev = phy_device_create(bus, off, |
| 964 | PHY_GMII2RGMII_ID, false, |
| 965 | interface); |
| 966 | break; |
| 967 | } |
| 968 | if (off == -FDT_ERR_NOTFOUND) |
| 969 | sn = fdt_first_subnode(gd->fdt_blob, sn); |
| 970 | else |
| 971 | printf("%s: Error finding compat string:%d\n", |
| 972 | __func__, off); |
| 973 | } |
| 974 | |
| 975 | return phydev; |
| 976 | } |
| 977 | #endif |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 978 | |
Siva Durga Prasad Paladugu | 03f937a | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 979 | #ifdef CONFIG_PHY_FIXED |
Vladimir Oltean | 8c089f7 | 2021-01-25 14:23:52 +0200 | [diff] [blame^] | 980 | /** |
| 981 | * fixed_phy_create() - create an unconnected fixed-link pseudo-PHY device |
| 982 | * @node: OF node for the container of the fixed-link node |
| 983 | * |
| 984 | * Description: Creates a struct phy_device based on a fixed-link of_node |
| 985 | * description. Can be used without phy_connect by drivers which do not expose |
| 986 | * a UCLASS_ETH udevice. |
| 987 | */ |
| 988 | struct phy_device *fixed_phy_create(ofnode node) |
| 989 | { |
| 990 | phy_interface_t interface = PHY_INTERFACE_MODE_NONE; |
| 991 | const char *if_str; |
| 992 | ofnode subnode; |
| 993 | |
| 994 | if_str = ofnode_read_string(node, "phy-mode"); |
| 995 | if (!if_str) { |
| 996 | if_str = ofnode_read_string(node, "phy-interface-type"); |
| 997 | } |
| 998 | if (if_str) { |
| 999 | interface = phy_get_interface_by_name(if_str); |
| 1000 | } |
| 1001 | |
| 1002 | subnode = ofnode_find_subnode(node, "fixed-link"); |
| 1003 | if (!ofnode_valid(subnode)) { |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | |
| 1007 | return phy_device_create(NULL, ofnode_to_offset(subnode), PHY_FIXED_ID, |
| 1008 | false, interface); |
| 1009 | } |
| 1010 | |
Simon Glass | dbad346 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 1011 | #ifdef CONFIG_DM_ETH |
Siva Durga Prasad Paladugu | 03f937a | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 1012 | static struct phy_device *phy_connect_fixed(struct mii_dev *bus, |
| 1013 | struct udevice *dev, |
| 1014 | phy_interface_t interface) |
Simon Glass | dbad346 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 1015 | #else |
Siva Durga Prasad Paladugu | 03f937a | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 1016 | static struct phy_device *phy_connect_fixed(struct mii_dev *bus, |
| 1017 | struct eth_device *dev, |
| 1018 | phy_interface_t interface) |
Simon Glass | dbad346 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 1019 | #endif |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 1020 | { |
Hannes Schmelzer | da49460 | 2017-03-23 15:11:43 +0100 | [diff] [blame] | 1021 | struct phy_device *phydev = NULL; |
Hannes Schmelzer | da49460 | 2017-03-23 15:11:43 +0100 | [diff] [blame] | 1022 | int sn; |
| 1023 | const char *name; |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 1024 | |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 1025 | sn = fdt_first_subnode(gd->fdt_blob, dev_of_offset(dev)); |
Hannes Schmelzer | da49460 | 2017-03-23 15:11:43 +0100 | [diff] [blame] | 1026 | while (sn > 0) { |
| 1027 | name = fdt_get_name(gd->fdt_blob, sn, NULL); |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 1028 | if (name && strcmp(name, "fixed-link") == 0) { |
Pankaj Bansal | 3c43a48 | 2018-11-16 06:26:18 +0000 | [diff] [blame] | 1029 | phydev = phy_device_create(bus, sn, PHY_FIXED_ID, false, |
| 1030 | interface); |
Hannes Schmelzer | da49460 | 2017-03-23 15:11:43 +0100 | [diff] [blame] | 1031 | break; |
| 1032 | } |
| 1033 | sn = fdt_next_subnode(gd->fdt_blob, sn); |
| 1034 | } |
Siva Durga Prasad Paladugu | 03f937a | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 1035 | |
| 1036 | return phydev; |
| 1037 | } |
Hannes Schmelzer | da49460 | 2017-03-23 15:11:43 +0100 | [diff] [blame] | 1038 | #endif |
Siva Durga Prasad Paladugu | 03f937a | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 1039 | |
| 1040 | #ifdef CONFIG_DM_ETH |
| 1041 | struct phy_device *phy_connect(struct mii_dev *bus, int addr, |
| 1042 | struct udevice *dev, |
| 1043 | phy_interface_t interface) |
| 1044 | #else |
| 1045 | struct phy_device *phy_connect(struct mii_dev *bus, int addr, |
| 1046 | struct eth_device *dev, |
| 1047 | phy_interface_t interface) |
| 1048 | #endif |
| 1049 | { |
| 1050 | struct phy_device *phydev = NULL; |
Priyanka Jain | 11691fd | 2019-11-05 04:05:11 +0000 | [diff] [blame] | 1051 | uint mask = (addr >= 0) ? (1 << addr) : 0xffffffff; |
Siva Durga Prasad Paladugu | 03f937a | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 1052 | |
| 1053 | #ifdef CONFIG_PHY_FIXED |
| 1054 | phydev = phy_connect_fixed(bus, dev, interface); |
| 1055 | #endif |
Samuel Mendoza-Jonas | b069c4a | 2019-06-18 11:37:18 +1000 | [diff] [blame] | 1056 | |
| 1057 | #ifdef CONFIG_PHY_NCSI |
| 1058 | if (!phydev) |
| 1059 | phydev = phy_device_create(bus, 0, PHY_NCSI_ID, false, interface); |
| 1060 | #endif |
| 1061 | |
Siva Durga Prasad Paladugu | d5c4e1e | 2018-11-27 11:49:11 +0530 | [diff] [blame] | 1062 | #ifdef CONFIG_PHY_XILINX_GMII2RGMII |
| 1063 | if (!phydev) |
| 1064 | phydev = phy_connect_gmii2rgmii(bus, dev, interface); |
| 1065 | #endif |
Siva Durga Prasad Paladugu | 03f937a | 2018-11-27 11:49:10 +0530 | [diff] [blame] | 1066 | |
Mario Six | 7757743 | 2018-01-15 11:08:27 +0100 | [diff] [blame] | 1067 | if (!phydev) |
Hannes Schmelzer | 02505e9 | 2019-03-29 09:54:05 +0100 | [diff] [blame] | 1068 | phydev = phy_find_by_mask(bus, mask, interface); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1069 | |
Troy Kisky | 9519bc5 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 1070 | if (phydev) |
| 1071 | phy_connect_dev(phydev, dev); |
| 1072 | else |
| 1073 | 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] | 1074 | return phydev; |
| 1075 | } |
| 1076 | |
Timur Tabi | 251180b | 2012-07-05 10:33:18 +0000 | [diff] [blame] | 1077 | /* |
| 1078 | * Start the PHY. Returns 0 on success, or a negative error code. |
| 1079 | */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1080 | int phy_startup(struct phy_device *phydev) |
| 1081 | { |
| 1082 | if (phydev->drv->startup) |
Timur Tabi | 251180b | 2012-07-05 10:33:18 +0000 | [diff] [blame] | 1083 | return phydev->drv->startup(phydev); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1084 | |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
Jeroen Hofstee | e4f8947 | 2014-10-08 22:57:26 +0200 | [diff] [blame] | 1088 | __weak int board_phy_config(struct phy_device *phydev) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1089 | { |
Troy Kisky | 8584641 | 2012-02-07 14:08:49 +0000 | [diff] [blame] | 1090 | if (phydev->drv->config) |
| 1091 | return phydev->drv->config(phydev); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1092 | return 0; |
| 1093 | } |
| 1094 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1095 | int phy_config(struct phy_device *phydev) |
| 1096 | { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1097 | /* Invoke an optional board-specific helper */ |
Michal Simek | 24ce232 | 2016-05-18 14:37:23 +0200 | [diff] [blame] | 1098 | return board_phy_config(phydev); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | int phy_shutdown(struct phy_device *phydev) |
| 1102 | { |
| 1103 | if (phydev->drv->shutdown) |
| 1104 | phydev->drv->shutdown(phydev); |
| 1105 | |
| 1106 | return 0; |
| 1107 | } |
Simon Glass | dbad346 | 2015-04-05 16:07:39 -0600 | [diff] [blame] | 1108 | |
| 1109 | int phy_get_interface_by_name(const char *str) |
| 1110 | { |
| 1111 | int i; |
| 1112 | |
| 1113 | for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) { |
| 1114 | if (!strcmp(str, phy_interface_strings[i])) |
| 1115 | return i; |
| 1116 | } |
| 1117 | |
| 1118 | return -1; |
| 1119 | } |