Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Generic PHY Management code |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 5 | * |
| 6 | * Copyright 2011 Freescale Semiconductor, Inc. |
| 7 | * author Andy Fleming |
| 8 | * |
| 9 | * Based loosely off of Linux's PHY Lib |
| 10 | */ |
| 11 | |
| 12 | #include <config.h> |
| 13 | #include <common.h> |
| 14 | #include <malloc.h> |
| 15 | #include <net.h> |
| 16 | #include <command.h> |
| 17 | #include <miiphy.h> |
| 18 | #include <phy.h> |
| 19 | #include <errno.h> |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 20 | #include <linux/err.h> |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 21 | |
| 22 | /* Generic PHY support and helper functions */ |
| 23 | |
| 24 | /** |
| 25 | * genphy_config_advert - sanitize and advertise auto-negotation parameters |
| 26 | * @phydev: target phy_device struct |
| 27 | * |
| 28 | * Description: Writes MII_ADVERTISE with the appropriate values, |
| 29 | * after sanitizing the values to make sure we only advertise |
| 30 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 31 | * hasn't changed, and > 0 if it has changed. |
| 32 | */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 33 | static int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 34 | { |
| 35 | u32 advertise; |
| 36 | int oldadv, adv; |
| 37 | int err, changed = 0; |
| 38 | |
| 39 | /* Only allow advertising what |
| 40 | * this PHY supports */ |
| 41 | phydev->advertising &= phydev->supported; |
| 42 | advertise = phydev->advertising; |
| 43 | |
| 44 | /* Setup standard advertisement */ |
| 45 | oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); |
| 46 | |
| 47 | if (adv < 0) |
| 48 | return adv; |
| 49 | |
| 50 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
| 51 | ADVERTISE_PAUSE_ASYM); |
| 52 | if (advertise & ADVERTISED_10baseT_Half) |
| 53 | adv |= ADVERTISE_10HALF; |
| 54 | if (advertise & ADVERTISED_10baseT_Full) |
| 55 | adv |= ADVERTISE_10FULL; |
| 56 | if (advertise & ADVERTISED_100baseT_Half) |
| 57 | adv |= ADVERTISE_100HALF; |
| 58 | if (advertise & ADVERTISED_100baseT_Full) |
| 59 | adv |= ADVERTISE_100FULL; |
| 60 | if (advertise & ADVERTISED_Pause) |
| 61 | adv |= ADVERTISE_PAUSE_CAP; |
| 62 | if (advertise & ADVERTISED_Asym_Pause) |
| 63 | adv |= ADVERTISE_PAUSE_ASYM; |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 64 | if (advertise & ADVERTISED_1000baseX_Half) |
| 65 | adv |= ADVERTISE_1000XHALF; |
| 66 | if (advertise & ADVERTISED_1000baseX_Full) |
| 67 | adv |= ADVERTISE_1000XFULL; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 68 | |
| 69 | if (adv != oldadv) { |
| 70 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv); |
| 71 | |
| 72 | if (err < 0) |
| 73 | return err; |
| 74 | changed = 1; |
| 75 | } |
| 76 | |
| 77 | /* Configure gigabit if it's supported */ |
| 78 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
| 79 | SUPPORTED_1000baseT_Full)) { |
| 80 | oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000); |
| 81 | |
| 82 | if (adv < 0) |
| 83 | return adv; |
| 84 | |
| 85 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 86 | if (advertise & SUPPORTED_1000baseT_Half) |
| 87 | adv |= ADVERTISE_1000HALF; |
| 88 | if (advertise & SUPPORTED_1000baseT_Full) |
| 89 | adv |= ADVERTISE_1000FULL; |
| 90 | |
| 91 | if (adv != oldadv) { |
| 92 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, |
| 93 | adv); |
| 94 | |
| 95 | if (err < 0) |
| 96 | return err; |
| 97 | changed = 1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return changed; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 107 | * @phydev: target phy_device struct |
| 108 | * |
| 109 | * Description: Configures MII_BMCR to force speed/duplex |
| 110 | * to the values in phydev. Assumes that the values are valid. |
| 111 | */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 112 | static int genphy_setup_forced(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 113 | { |
| 114 | int err; |
| 115 | int ctl = 0; |
| 116 | |
| 117 | phydev->pause = phydev->asym_pause = 0; |
| 118 | |
| 119 | if (SPEED_1000 == phydev->speed) |
| 120 | ctl |= BMCR_SPEED1000; |
| 121 | else if (SPEED_100 == phydev->speed) |
| 122 | ctl |= BMCR_SPEED100; |
| 123 | |
| 124 | if (DUPLEX_FULL == phydev->duplex) |
| 125 | ctl |= BMCR_FULLDPLX; |
| 126 | |
| 127 | err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); |
| 128 | |
| 129 | return err; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 135 | * @phydev: target phy_device struct |
| 136 | */ |
| 137 | int genphy_restart_aneg(struct phy_device *phydev) |
| 138 | { |
| 139 | int ctl; |
| 140 | |
| 141 | ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 142 | |
| 143 | if (ctl < 0) |
| 144 | return ctl; |
| 145 | |
| 146 | ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); |
| 147 | |
| 148 | /* Don't isolate the PHY if we're negotiating */ |
| 149 | ctl &= ~(BMCR_ISOLATE); |
| 150 | |
| 151 | ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); |
| 152 | |
| 153 | return ctl; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * genphy_config_aneg - restart auto-negotiation or write BMCR |
| 159 | * @phydev: target phy_device struct |
| 160 | * |
| 161 | * Description: If auto-negotiation is enabled, we configure the |
| 162 | * advertising, and then restart auto-negotiation. If it is not |
| 163 | * enabled, then we write the BMCR. |
| 164 | */ |
| 165 | int genphy_config_aneg(struct phy_device *phydev) |
| 166 | { |
| 167 | int result; |
| 168 | |
| 169 | if (AUTONEG_ENABLE != phydev->autoneg) |
| 170 | return genphy_setup_forced(phydev); |
| 171 | |
| 172 | result = genphy_config_advert(phydev); |
| 173 | |
| 174 | if (result < 0) /* error */ |
| 175 | return result; |
| 176 | |
| 177 | if (result == 0) { |
| 178 | /* Advertisment hasn't changed, but maybe aneg was never on to |
| 179 | * begin with? Or maybe phy was isolated? */ |
| 180 | int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 181 | |
| 182 | if (ctl < 0) |
| 183 | return ctl; |
| 184 | |
| 185 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) |
| 186 | result = 1; /* do restart aneg */ |
| 187 | } |
| 188 | |
| 189 | /* Only restart aneg if we are advertising something different |
| 190 | * than we were before. */ |
| 191 | if (result > 0) |
| 192 | result = genphy_restart_aneg(phydev); |
| 193 | |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * genphy_update_link - update link status in @phydev |
| 199 | * @phydev: target phy_device struct |
| 200 | * |
| 201 | * Description: Update the value in phydev->link to reflect the |
| 202 | * current link value. In order to do this, we need to read |
| 203 | * the status register twice, keeping the second value. |
| 204 | */ |
| 205 | int genphy_update_link(struct phy_device *phydev) |
| 206 | { |
| 207 | unsigned int mii_reg; |
| 208 | |
| 209 | /* |
| 210 | * Wait if the link is up, and autonegotiation is in progress |
| 211 | * (ie - we're capable and it's not done) |
| 212 | */ |
| 213 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 214 | |
| 215 | /* |
| 216 | * If we already saw the link up, and it hasn't gone down, then |
| 217 | * we don't need to wait for autoneg again |
| 218 | */ |
| 219 | if (phydev->link && mii_reg & BMSR_LSTATUS) |
| 220 | return 0; |
| 221 | |
| 222 | if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) { |
| 223 | int i = 0; |
| 224 | |
| 225 | printf("%s Waiting for PHY auto negotiation to complete", |
| 226 | phydev->dev->name); |
| 227 | while (!(mii_reg & BMSR_ANEGCOMPLETE)) { |
| 228 | /* |
| 229 | * Timeout reached ? |
| 230 | */ |
| 231 | if (i > PHY_ANEG_TIMEOUT) { |
| 232 | printf(" TIMEOUT !\n"); |
| 233 | phydev->link = 0; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | if (ctrlc()) { |
| 238 | puts("user interrupt!\n"); |
| 239 | phydev->link = 0; |
| 240 | return -EINTR; |
| 241 | } |
| 242 | |
| 243 | if ((i++ % 500) == 0) |
| 244 | printf("."); |
| 245 | |
| 246 | udelay(1000); /* 1 ms */ |
| 247 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 248 | } |
| 249 | printf(" done\n"); |
| 250 | phydev->link = 1; |
| 251 | } else { |
| 252 | /* Read the link a second time to clear the latched state */ |
| 253 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 254 | |
| 255 | if (mii_reg & BMSR_LSTATUS) |
| 256 | phydev->link = 1; |
| 257 | else |
| 258 | phydev->link = 0; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Generic function which updates the speed and duplex. If |
| 266 | * autonegotiation is enabled, it uses the AND of the link |
| 267 | * partner's advertised capabilities and our advertised |
| 268 | * capabilities. If autonegotiation is disabled, we use the |
| 269 | * appropriate bits in the control register. |
| 270 | * |
| 271 | * Stolen from Linux's mii.c and phy_device.c |
| 272 | */ |
Yegor Yefremov | e2043f5 | 2012-11-28 11:15:17 +0100 | [diff] [blame] | 273 | int genphy_parse_link(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 274 | { |
| 275 | int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 276 | |
| 277 | /* We're using autonegotiation */ |
David Dueck | 3a530d1 | 2013-11-05 17:23:02 +0100 | [diff] [blame] | 278 | if (phydev->supported & SUPPORTED_Autoneg) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 279 | u32 lpa = 0; |
Heiko Schocher | f6d1f6e | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 280 | int gblpa = 0; |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 281 | u32 estatus = 0; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 282 | |
| 283 | /* Check for gigabit capability */ |
David Dueck | 3a530d1 | 2013-11-05 17:23:02 +0100 | [diff] [blame] | 284 | if (phydev->supported & (SUPPORTED_1000baseT_Full | |
| 285 | SUPPORTED_1000baseT_Half)) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 286 | /* We want a list of states supported by |
| 287 | * both PHYs in the link |
| 288 | */ |
| 289 | gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000); |
Heiko Schocher | f6d1f6e | 2013-07-23 15:32:36 +0200 | [diff] [blame] | 290 | if (gblpa < 0) { |
| 291 | debug("Could not read MII_STAT1000. Ignoring gigabit capability\n"); |
| 292 | gblpa = 0; |
| 293 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 294 | gblpa &= phy_read(phydev, |
| 295 | MDIO_DEVAD_NONE, MII_CTRL1000) << 2; |
| 296 | } |
| 297 | |
| 298 | /* Set the baseline so we only have to set them |
| 299 | * if they're different |
| 300 | */ |
| 301 | phydev->speed = SPEED_10; |
| 302 | phydev->duplex = DUPLEX_HALF; |
| 303 | |
| 304 | /* Check the gigabit fields */ |
| 305 | if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) { |
| 306 | phydev->speed = SPEED_1000; |
| 307 | |
| 308 | if (gblpa & PHY_1000BTSR_1000FD) |
| 309 | phydev->duplex = DUPLEX_FULL; |
| 310 | |
| 311 | /* We're done! */ |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); |
| 316 | lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA); |
| 317 | |
Wolfgang Denk | 0dcfb0f | 2011-09-28 21:02:43 +0200 | [diff] [blame] | 318 | if (lpa & (LPA_100FULL | LPA_100HALF)) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 319 | phydev->speed = SPEED_100; |
| 320 | |
Wolfgang Denk | 0dcfb0f | 2011-09-28 21:02:43 +0200 | [diff] [blame] | 321 | if (lpa & LPA_100FULL) |
| 322 | phydev->duplex = DUPLEX_FULL; |
| 323 | |
| 324 | } else if (lpa & LPA_10FULL) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 325 | phydev->duplex = DUPLEX_FULL; |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 326 | |
Sascha Silbe | 9ba30f6 | 2013-07-19 12:25:10 +0200 | [diff] [blame] | 327 | /* |
| 328 | * Extended status may indicate that the PHY supports |
| 329 | * 1000BASE-T/X even though the 1000BASE-T registers |
| 330 | * are missing. In this case we can't tell whether the |
| 331 | * peer also supports it, so we only check extended |
| 332 | * status if the 1000BASE-T registers are actually |
| 333 | * missing. |
| 334 | */ |
| 335 | if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP)) |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 336 | estatus = phy_read(phydev, MDIO_DEVAD_NONE, |
| 337 | MII_ESTATUS); |
| 338 | |
| 339 | if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF | |
| 340 | ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) { |
| 341 | phydev->speed = SPEED_1000; |
| 342 | if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL)) |
| 343 | phydev->duplex = DUPLEX_FULL; |
| 344 | } |
| 345 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 346 | } else { |
| 347 | u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); |
| 348 | |
| 349 | phydev->speed = SPEED_10; |
| 350 | phydev->duplex = DUPLEX_HALF; |
| 351 | |
| 352 | if (bmcr & BMCR_FULLDPLX) |
| 353 | phydev->duplex = DUPLEX_FULL; |
| 354 | |
| 355 | if (bmcr & BMCR_SPEED1000) |
| 356 | phydev->speed = SPEED_1000; |
| 357 | else if (bmcr & BMCR_SPEED100) |
| 358 | phydev->speed = SPEED_100; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int genphy_config(struct phy_device *phydev) |
| 365 | { |
| 366 | int val; |
| 367 | u32 features; |
| 368 | |
| 369 | /* For now, I'll claim that the generic driver supports |
| 370 | * all possible port types */ |
| 371 | features = (SUPPORTED_TP | SUPPORTED_MII |
| 372 | | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 373 | SUPPORTED_BNC); |
| 374 | |
| 375 | /* Do we support autonegotiation? */ |
| 376 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); |
| 377 | |
| 378 | if (val < 0) |
| 379 | return val; |
| 380 | |
| 381 | if (val & BMSR_ANEGCAPABLE) |
| 382 | features |= SUPPORTED_Autoneg; |
| 383 | |
| 384 | if (val & BMSR_100FULL) |
| 385 | features |= SUPPORTED_100baseT_Full; |
| 386 | if (val & BMSR_100HALF) |
| 387 | features |= SUPPORTED_100baseT_Half; |
| 388 | if (val & BMSR_10FULL) |
| 389 | features |= SUPPORTED_10baseT_Full; |
| 390 | if (val & BMSR_10HALF) |
| 391 | features |= SUPPORTED_10baseT_Half; |
| 392 | |
| 393 | if (val & BMSR_ESTATEN) { |
| 394 | val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS); |
| 395 | |
| 396 | if (val < 0) |
| 397 | return val; |
| 398 | |
| 399 | if (val & ESTATUS_1000_TFULL) |
| 400 | features |= SUPPORTED_1000baseT_Full; |
| 401 | if (val & ESTATUS_1000_THALF) |
| 402 | features |= SUPPORTED_1000baseT_Half; |
Charles Coldwell | de1d786 | 2013-02-21 08:25:52 -0500 | [diff] [blame] | 403 | if (val & ESTATUS_1000_XFULL) |
| 404 | features |= SUPPORTED_1000baseX_Full; |
| 405 | if (val & ESTATUS_1000_XHALF) |
Fabio Estevam | 9a5dad2 | 2013-07-19 10:01:34 -0300 | [diff] [blame] | 406 | features |= SUPPORTED_1000baseX_Half; |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | phydev->supported = features; |
| 410 | phydev->advertising = features; |
| 411 | |
| 412 | genphy_config_aneg(phydev); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | int genphy_startup(struct phy_device *phydev) |
| 418 | { |
| 419 | genphy_update_link(phydev); |
| 420 | genphy_parse_link(phydev); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | int genphy_shutdown(struct phy_device *phydev) |
| 426 | { |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static struct phy_driver genphy_driver = { |
| 431 | .uid = 0xffffffff, |
| 432 | .mask = 0xffffffff, |
| 433 | .name = "Generic PHY", |
| 434 | .features = 0, |
| 435 | .config = genphy_config, |
| 436 | .startup = genphy_startup, |
| 437 | .shutdown = genphy_shutdown, |
| 438 | }; |
| 439 | |
| 440 | static LIST_HEAD(phy_drivers); |
| 441 | |
| 442 | int phy_init(void) |
| 443 | { |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 444 | #ifdef CONFIG_PHY_ATHEROS |
| 445 | phy_atheros_init(); |
| 446 | #endif |
| 447 | #ifdef CONFIG_PHY_BROADCOM |
| 448 | phy_broadcom_init(); |
| 449 | #endif |
| 450 | #ifdef CONFIG_PHY_DAVICOM |
| 451 | phy_davicom_init(); |
| 452 | #endif |
Matt Porter | f485c8a | 2013-03-20 05:38:13 +0000 | [diff] [blame] | 453 | #ifdef CONFIG_PHY_ET1011C |
| 454 | phy_et1011c_init(); |
| 455 | #endif |
Yegor Yefremov | 0fae250 | 2012-11-28 11:15:18 +0100 | [diff] [blame] | 456 | #ifdef CONFIG_PHY_ICPLUS |
| 457 | phy_icplus_init(); |
| 458 | #endif |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 459 | #ifdef CONFIG_PHY_LXT |
| 460 | phy_lxt_init(); |
| 461 | #endif |
| 462 | #ifdef CONFIG_PHY_MARVELL |
| 463 | phy_marvell_init(); |
| 464 | #endif |
| 465 | #ifdef CONFIG_PHY_MICREL |
| 466 | phy_micrel_init(); |
| 467 | #endif |
| 468 | #ifdef CONFIG_PHY_NATSEMI |
| 469 | phy_natsemi_init(); |
| 470 | #endif |
| 471 | #ifdef CONFIG_PHY_REALTEK |
| 472 | phy_realtek_init(); |
| 473 | #endif |
Nobuhiro Iwamatsu | 5751aa2 | 2011-11-23 21:24:15 +0000 | [diff] [blame] | 474 | #ifdef CONFIG_PHY_SMSC |
| 475 | phy_smsc_init(); |
| 476 | #endif |
Andy Fleming | 9082eea | 2011-04-07 21:56:05 -0500 | [diff] [blame] | 477 | #ifdef CONFIG_PHY_TERANETICS |
| 478 | phy_teranetics_init(); |
| 479 | #endif |
| 480 | #ifdef CONFIG_PHY_VITESSE |
| 481 | phy_vitesse_init(); |
| 482 | #endif |
| 483 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int phy_register(struct phy_driver *drv) |
| 488 | { |
| 489 | INIT_LIST_HEAD(&drv->list); |
| 490 | list_add_tail(&drv->list, &phy_drivers); |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 495 | static int phy_probe(struct phy_device *phydev) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 496 | { |
| 497 | int err = 0; |
| 498 | |
| 499 | phydev->advertising = phydev->supported = phydev->drv->features; |
| 500 | phydev->mmds = phydev->drv->mmds; |
| 501 | |
| 502 | if (phydev->drv->probe) |
| 503 | err = phydev->drv->probe(phydev); |
| 504 | |
| 505 | return err; |
| 506 | } |
| 507 | |
| 508 | static struct phy_driver *generic_for_interface(phy_interface_t interface) |
| 509 | { |
| 510 | #ifdef CONFIG_PHYLIB_10G |
| 511 | if (is_10g_interface(interface)) |
| 512 | return &gen10g_driver; |
| 513 | #endif |
| 514 | |
| 515 | return &genphy_driver; |
| 516 | } |
| 517 | |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 518 | static struct phy_driver *get_phy_driver(struct phy_device *phydev, |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 519 | phy_interface_t interface) |
| 520 | { |
| 521 | struct list_head *entry; |
| 522 | int phy_id = phydev->phy_id; |
| 523 | struct phy_driver *drv = NULL; |
| 524 | |
| 525 | list_for_each(entry, &phy_drivers) { |
| 526 | drv = list_entry(entry, struct phy_driver, list); |
| 527 | if ((drv->uid & drv->mask) == (phy_id & drv->mask)) |
| 528 | return drv; |
| 529 | } |
| 530 | |
| 531 | /* If we made it here, there's no driver for this PHY */ |
| 532 | return generic_for_interface(interface); |
| 533 | } |
| 534 | |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 535 | static struct phy_device *phy_device_create(struct mii_dev *bus, int addr, |
| 536 | int phy_id, |
| 537 | phy_interface_t interface) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 538 | { |
| 539 | struct phy_device *dev; |
| 540 | |
| 541 | /* We allocate the device, and initialize the |
| 542 | * default values */ |
| 543 | dev = malloc(sizeof(*dev)); |
| 544 | if (!dev) { |
| 545 | printf("Failed to allocate PHY device for %s:%d\n", |
| 546 | bus->name, addr); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | memset(dev, 0, sizeof(*dev)); |
| 551 | |
| 552 | dev->duplex = -1; |
| 553 | dev->link = 1; |
| 554 | dev->interface = interface; |
| 555 | |
| 556 | dev->autoneg = AUTONEG_ENABLE; |
| 557 | |
| 558 | dev->addr = addr; |
| 559 | dev->phy_id = phy_id; |
| 560 | dev->bus = bus; |
| 561 | |
| 562 | dev->drv = get_phy_driver(dev, interface); |
| 563 | |
| 564 | phy_probe(dev); |
| 565 | |
| 566 | bus->phymap[addr] = dev; |
| 567 | |
| 568 | return dev; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * get_phy_id - reads the specified addr for its ID. |
| 573 | * @bus: the target MII bus |
| 574 | * @addr: PHY address on the MII bus |
| 575 | * @phy_id: where to store the ID retrieved. |
| 576 | * |
| 577 | * Description: Reads the ID registers of the PHY at @addr on the |
| 578 | * @bus, stores it in @phy_id and returns zero on success. |
| 579 | */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 580 | static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 581 | { |
| 582 | int phy_reg; |
| 583 | |
| 584 | /* Grab the bits from PHYIR1, and put them |
| 585 | * in the upper half */ |
| 586 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID1); |
| 587 | |
| 588 | if (phy_reg < 0) |
| 589 | return -EIO; |
| 590 | |
| 591 | *phy_id = (phy_reg & 0xffff) << 16; |
| 592 | |
| 593 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
| 594 | phy_reg = bus->read(bus, addr, devad, MII_PHYSID2); |
| 595 | |
| 596 | if (phy_reg < 0) |
| 597 | return -EIO; |
| 598 | |
| 599 | *phy_id |= (phy_reg & 0xffff); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 604 | static struct phy_device *create_phy_by_mask(struct mii_dev *bus, |
| 605 | unsigned phy_mask, int devad, phy_interface_t interface) |
| 606 | { |
| 607 | u32 phy_id = 0xffffffff; |
| 608 | while (phy_mask) { |
| 609 | int addr = ffs(phy_mask) - 1; |
| 610 | int r = get_phy_id(bus, addr, devad, &phy_id); |
| 611 | if (r < 0) |
| 612 | return ERR_PTR(r); |
| 613 | /* If the PHY ID is mostly f's, we didn't find anything */ |
| 614 | if ((phy_id & 0x1fffffff) != 0x1fffffff) |
| 615 | return phy_device_create(bus, addr, phy_id, interface); |
| 616 | phy_mask &= ~(1 << addr); |
| 617 | } |
| 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | static struct phy_device *search_for_existing_phy(struct mii_dev *bus, |
| 622 | unsigned phy_mask, phy_interface_t interface) |
| 623 | { |
| 624 | /* If we have one, return the existing device, with new interface */ |
| 625 | while (phy_mask) { |
| 626 | int addr = ffs(phy_mask) - 1; |
| 627 | if (bus->phymap[addr]) { |
| 628 | bus->phymap[addr]->interface = interface; |
| 629 | return bus->phymap[addr]; |
| 630 | } |
| 631 | phy_mask &= ~(1 << addr); |
| 632 | } |
| 633 | return NULL; |
| 634 | } |
| 635 | |
| 636 | static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus, |
| 637 | unsigned phy_mask, phy_interface_t interface) |
| 638 | { |
| 639 | int i; |
| 640 | struct phy_device *phydev; |
| 641 | |
| 642 | phydev = search_for_existing_phy(bus, phy_mask, interface); |
| 643 | if (phydev) |
| 644 | return phydev; |
| 645 | /* Try Standard (ie Clause 22) access */ |
| 646 | /* Otherwise we have to try Clause 45 */ |
| 647 | for (i = 0; i < 5; i++) { |
| 648 | phydev = create_phy_by_mask(bus, phy_mask, |
| 649 | i ? i : MDIO_DEVAD_NONE, interface); |
| 650 | if (IS_ERR(phydev)) |
| 651 | return NULL; |
| 652 | if (phydev) |
| 653 | return phydev; |
| 654 | } |
| 655 | printf("Phy not found\n"); |
| 656 | return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface); |
| 657 | } |
| 658 | |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 659 | /** |
| 660 | * get_phy_device - reads the specified PHY device and returns its @phy_device struct |
| 661 | * @bus: the target MII bus |
| 662 | * @addr: PHY address on the MII bus |
| 663 | * |
| 664 | * Description: Reads the ID registers of the PHY at @addr on the |
| 665 | * @bus, then allocates and returns the phy_device to represent it. |
| 666 | */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 667 | static struct phy_device *get_phy_device(struct mii_dev *bus, int addr, |
| 668 | phy_interface_t interface) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 669 | { |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 670 | return get_phy_device_by_mask(bus, 1 << addr, interface); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | int phy_reset(struct phy_device *phydev) |
| 674 | { |
| 675 | int reg; |
| 676 | int timeout = 500; |
| 677 | int devad = MDIO_DEVAD_NONE; |
| 678 | |
| 679 | #ifdef CONFIG_PHYLIB_10G |
| 680 | /* If it's 10G, we need to issue reset through one of the MMDs */ |
| 681 | if (is_10g_interface(phydev->interface)) { |
| 682 | if (!phydev->mmds) |
| 683 | gen10g_discover_mmds(phydev); |
| 684 | |
| 685 | devad = ffs(phydev->mmds) - 1; |
| 686 | } |
| 687 | #endif |
| 688 | |
| 689 | reg = phy_read(phydev, devad, MII_BMCR); |
| 690 | if (reg < 0) { |
| 691 | debug("PHY status read failed\n"); |
| 692 | return -1; |
| 693 | } |
| 694 | |
| 695 | reg |= BMCR_RESET; |
| 696 | |
| 697 | if (phy_write(phydev, devad, MII_BMCR, reg) < 0) { |
| 698 | debug("PHY reset failed\n"); |
| 699 | return -1; |
| 700 | } |
| 701 | |
| 702 | #ifdef CONFIG_PHY_RESET_DELAY |
| 703 | udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */ |
| 704 | #endif |
| 705 | /* |
| 706 | * Poll the control register for the reset bit to go to 0 (it is |
| 707 | * auto-clearing). This should happen within 0.5 seconds per the |
| 708 | * IEEE spec. |
| 709 | */ |
| 710 | while ((reg & BMCR_RESET) && timeout--) { |
| 711 | reg = phy_read(phydev, devad, MII_BMCR); |
| 712 | |
| 713 | if (reg < 0) { |
| 714 | debug("PHY status read failed\n"); |
| 715 | return -1; |
| 716 | } |
| 717 | udelay(1000); |
| 718 | } |
| 719 | |
| 720 | if (reg & BMCR_RESET) { |
| 721 | puts("PHY reset timed out\n"); |
| 722 | return -1; |
| 723 | } |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | int miiphy_reset(const char *devname, unsigned char addr) |
| 729 | { |
| 730 | struct mii_dev *bus = miiphy_get_dev_by_name(devname); |
| 731 | struct phy_device *phydev; |
| 732 | |
| 733 | /* |
| 734 | * miiphy_reset was only used on standard PHYs, so we'll fake it here. |
| 735 | * If later code tries to connect with the right interface, this will |
| 736 | * be corrected by get_phy_device in phy_connect() |
| 737 | */ |
| 738 | phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII); |
| 739 | |
| 740 | return phy_reset(phydev); |
| 741 | } |
| 742 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 743 | struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask, |
| 744 | phy_interface_t interface) |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 745 | { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 746 | /* Reset the bus */ |
Vladimir Zapolskiy | e3a7721 | 2011-09-05 07:24:07 +0000 | [diff] [blame] | 747 | if (bus->reset) |
| 748 | bus->reset(bus); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 749 | |
| 750 | /* Wait 15ms to make sure the PHY has come out of hard reset */ |
| 751 | udelay(15000); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 752 | return get_phy_device_by_mask(bus, phy_mask, interface); |
| 753 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 754 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 755 | void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev) |
| 756 | { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 757 | /* Soft Reset the PHY */ |
| 758 | phy_reset(phydev); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 759 | if (phydev->dev) { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 760 | printf("%s:%d is connected to %s. Reconnecting to %s\n", |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 761 | phydev->bus->name, phydev->addr, |
| 762 | phydev->dev->name, dev->name); |
| 763 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 764 | phydev->dev = dev; |
Wolfgang Denk | b91a9d9 | 2011-07-24 21:39:10 +0000 | [diff] [blame] | 765 | debug("%s connected to %s\n", dev->name, phydev->drv->name); |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 766 | } |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 767 | |
Troy Kisky | 1adb406 | 2012-10-22 16:40:43 +0000 | [diff] [blame] | 768 | struct phy_device *phy_connect(struct mii_dev *bus, int addr, |
| 769 | struct eth_device *dev, phy_interface_t interface) |
| 770 | { |
| 771 | struct phy_device *phydev; |
| 772 | |
| 773 | phydev = phy_find_by_mask(bus, 1 << addr, interface); |
| 774 | if (phydev) |
| 775 | phy_connect_dev(phydev, dev); |
| 776 | else |
| 777 | printf("Could not get PHY for %s: addr %d\n", bus->name, addr); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 778 | return phydev; |
| 779 | } |
| 780 | |
Timur Tabi | 6e5b9ac | 2012-07-05 10:33:18 +0000 | [diff] [blame] | 781 | /* |
| 782 | * Start the PHY. Returns 0 on success, or a negative error code. |
| 783 | */ |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 784 | int phy_startup(struct phy_device *phydev) |
| 785 | { |
| 786 | if (phydev->drv->startup) |
Timur Tabi | 6e5b9ac | 2012-07-05 10:33:18 +0000 | [diff] [blame] | 787 | return phydev->drv->startup(phydev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 788 | |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | static int __board_phy_config(struct phy_device *phydev) |
| 793 | { |
Troy Kisky | 9fafe7d | 2012-02-07 14:08:49 +0000 | [diff] [blame] | 794 | if (phydev->drv->config) |
| 795 | return phydev->drv->config(phydev); |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | int board_phy_config(struct phy_device *phydev) |
| 800 | __attribute__((weak, alias("__board_phy_config"))); |
| 801 | |
| 802 | int phy_config(struct phy_device *phydev) |
| 803 | { |
Andy Fleming | 5f18471 | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 804 | /* Invoke an optional board-specific helper */ |
| 805 | board_phy_config(phydev); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | int phy_shutdown(struct phy_device *phydev) |
| 811 | { |
| 812 | if (phydev->drv->shutdown) |
| 813 | phydev->drv->shutdown(phydev); |
| 814 | |
| 815 | return 0; |
| 816 | } |