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