1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Core PHY library, taken from phy.c 4 */ 5 #include <linux/export.h> 6 #include <linux/phy.h> 7 #include <linux/of.h> 8 9 #include "phylib.h" 10 #include "phylib-internal.h" 11 #include "phy-caps.h" 12 13 /** 14 * phy_speed_to_str - Return a string representing the PHY link speed 15 * 16 * @speed: Speed of the link 17 */ 18 const char *phy_speed_to_str(int speed) 19 { 20 BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 121, 21 "Enum ethtool_link_mode_bit_indices and phylib are out of sync. " 22 "If a speed or mode has been added please update phy_speed_to_str " 23 "and the PHY settings array.\n"); 24 25 switch (speed) { 26 case SPEED_10: 27 return "10Mbps"; 28 case SPEED_100: 29 return "100Mbps"; 30 case SPEED_1000: 31 return "1Gbps"; 32 case SPEED_2500: 33 return "2.5Gbps"; 34 case SPEED_5000: 35 return "5Gbps"; 36 case SPEED_10000: 37 return "10Gbps"; 38 case SPEED_14000: 39 return "14Gbps"; 40 case SPEED_20000: 41 return "20Gbps"; 42 case SPEED_25000: 43 return "25Gbps"; 44 case SPEED_40000: 45 return "40Gbps"; 46 case SPEED_50000: 47 return "50Gbps"; 48 case SPEED_56000: 49 return "56Gbps"; 50 case SPEED_100000: 51 return "100Gbps"; 52 case SPEED_200000: 53 return "200Gbps"; 54 case SPEED_400000: 55 return "400Gbps"; 56 case SPEED_800000: 57 return "800Gbps"; 58 case SPEED_UNKNOWN: 59 return "Unknown"; 60 default: 61 return "Unsupported (update phy-core.c)"; 62 } 63 } 64 EXPORT_SYMBOL_GPL(phy_speed_to_str); 65 66 /** 67 * phy_duplex_to_str - Return string describing the duplex 68 * 69 * @duplex: Duplex setting to describe 70 */ 71 const char *phy_duplex_to_str(unsigned int duplex) 72 { 73 if (duplex == DUPLEX_HALF) 74 return "Half"; 75 if (duplex == DUPLEX_FULL) 76 return "Full"; 77 if (duplex == DUPLEX_UNKNOWN) 78 return "Unknown"; 79 return "Unsupported (update phy-core.c)"; 80 } 81 EXPORT_SYMBOL_GPL(phy_duplex_to_str); 82 83 /** 84 * phy_rate_matching_to_str - Return a string describing the rate matching 85 * 86 * @rate_matching: Type of rate matching to describe 87 */ 88 const char *phy_rate_matching_to_str(int rate_matching) 89 { 90 switch (rate_matching) { 91 case RATE_MATCH_NONE: 92 return "none"; 93 case RATE_MATCH_PAUSE: 94 return "pause"; 95 case RATE_MATCH_CRS: 96 return "crs"; 97 case RATE_MATCH_OPEN_LOOP: 98 return "open-loop"; 99 } 100 return "Unsupported (update phy-core.c)"; 101 } 102 EXPORT_SYMBOL_GPL(phy_rate_matching_to_str); 103 104 /** 105 * phy_interface_num_ports - Return the number of links that can be carried by 106 * a given MAC-PHY physical link. Returns 0 if this is 107 * unknown, the number of links else. 108 * 109 * @interface: The interface mode we want to get the number of ports 110 */ 111 int phy_interface_num_ports(phy_interface_t interface) 112 { 113 switch (interface) { 114 case PHY_INTERFACE_MODE_NA: 115 return 0; 116 case PHY_INTERFACE_MODE_INTERNAL: 117 case PHY_INTERFACE_MODE_MII: 118 case PHY_INTERFACE_MODE_GMII: 119 case PHY_INTERFACE_MODE_TBI: 120 case PHY_INTERFACE_MODE_REVMII: 121 case PHY_INTERFACE_MODE_RMII: 122 case PHY_INTERFACE_MODE_REVRMII: 123 case PHY_INTERFACE_MODE_RGMII: 124 case PHY_INTERFACE_MODE_RGMII_ID: 125 case PHY_INTERFACE_MODE_RGMII_RXID: 126 case PHY_INTERFACE_MODE_RGMII_TXID: 127 case PHY_INTERFACE_MODE_RTBI: 128 case PHY_INTERFACE_MODE_XGMII: 129 case PHY_INTERFACE_MODE_XLGMII: 130 case PHY_INTERFACE_MODE_MOCA: 131 case PHY_INTERFACE_MODE_TRGMII: 132 case PHY_INTERFACE_MODE_USXGMII: 133 case PHY_INTERFACE_MODE_SGMII: 134 case PHY_INTERFACE_MODE_SMII: 135 case PHY_INTERFACE_MODE_1000BASEX: 136 case PHY_INTERFACE_MODE_2500BASEX: 137 case PHY_INTERFACE_MODE_5GBASER: 138 case PHY_INTERFACE_MODE_10GBASER: 139 case PHY_INTERFACE_MODE_25GBASER: 140 case PHY_INTERFACE_MODE_10GKR: 141 case PHY_INTERFACE_MODE_100BASEX: 142 case PHY_INTERFACE_MODE_RXAUI: 143 case PHY_INTERFACE_MODE_XAUI: 144 case PHY_INTERFACE_MODE_1000BASEKX: 145 return 1; 146 case PHY_INTERFACE_MODE_QSGMII: 147 case PHY_INTERFACE_MODE_QUSGMII: 148 case PHY_INTERFACE_MODE_10G_QXGMII: 149 return 4; 150 case PHY_INTERFACE_MODE_PSGMII: 151 return 5; 152 case PHY_INTERFACE_MODE_MAX: 153 WARN_ONCE(1, "PHY_INTERFACE_MODE_MAX isn't a valid interface mode"); 154 return 0; 155 } 156 return 0; 157 } 158 EXPORT_SYMBOL_GPL(phy_interface_num_ports); 159 160 static void __set_phy_supported(struct phy_device *phydev, u32 max_speed) 161 { 162 phy_caps_linkmode_max_speed(max_speed, phydev->supported); 163 } 164 165 /** 166 * phy_set_max_speed - Set the maximum speed the PHY should support 167 * 168 * @phydev: The phy_device struct 169 * @max_speed: Maximum speed 170 * 171 * The PHY might be more capable than the MAC. For example a Fast Ethernet 172 * is connected to a 1G PHY. This function allows the MAC to indicate its 173 * maximum speed, and so limit what the PHY will advertise. 174 */ 175 void phy_set_max_speed(struct phy_device *phydev, u32 max_speed) 176 { 177 __set_phy_supported(phydev, max_speed); 178 179 phy_advertise_supported(phydev); 180 } 181 EXPORT_SYMBOL(phy_set_max_speed); 182 183 void of_set_phy_supported(struct phy_device *phydev) 184 { 185 struct device_node *node = phydev->mdio.dev.of_node; 186 u32 max_speed; 187 188 if (!IS_ENABLED(CONFIG_OF_MDIO)) 189 return; 190 191 if (!node) 192 return; 193 194 if (!of_property_read_u32(node, "max-speed", &max_speed)) 195 __set_phy_supported(phydev, max_speed); 196 } 197 198 void of_set_phy_eee_broken(struct phy_device *phydev) 199 { 200 struct device_node *node = phydev->mdio.dev.of_node; 201 unsigned long *modes = phydev->eee_disabled_modes; 202 203 if (!IS_ENABLED(CONFIG_OF_MDIO) || !node) 204 return; 205 206 linkmode_zero(modes); 207 208 if (of_property_read_bool(node, "eee-broken-100tx")) 209 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, modes); 210 if (of_property_read_bool(node, "eee-broken-1000t")) 211 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, modes); 212 if (of_property_read_bool(node, "eee-broken-10gt")) 213 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, modes); 214 if (of_property_read_bool(node, "eee-broken-1000kx")) 215 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, modes); 216 if (of_property_read_bool(node, "eee-broken-10gkx4")) 217 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, modes); 218 if (of_property_read_bool(node, "eee-broken-10gkr")) 219 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, modes); 220 } 221 222 /** 223 * of_set_phy_timing_role - Set the master/slave mode of the PHY 224 * 225 * @phydev: The phy_device struct 226 * 227 * Set master/slave configuration of the PHY based on the device tree. 228 */ 229 void of_set_phy_timing_role(struct phy_device *phydev) 230 { 231 struct device_node *node = phydev->mdio.dev.of_node; 232 const char *master; 233 234 if (!IS_ENABLED(CONFIG_OF_MDIO)) 235 return; 236 237 if (!node) 238 return; 239 240 if (of_property_read_string(node, "timing-role", &master)) 241 return; 242 243 if (strcmp(master, "forced-master") == 0) 244 phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_FORCE; 245 else if (strcmp(master, "forced-slave") == 0) 246 phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_FORCE; 247 else if (strcmp(master, "preferred-master") == 0) 248 phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_PREFERRED; 249 else if (strcmp(master, "preferred-slave") == 0) 250 phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_PREFERRED; 251 else 252 phydev_warn(phydev, "Unknown master-slave mode %s\n", master); 253 } 254 255 /** 256 * phy_resolve_aneg_pause - Determine pause autoneg results 257 * 258 * @phydev: The phy_device struct 259 * 260 * Once autoneg has completed the local pause settings can be 261 * resolved. Determine if pause and asymmetric pause should be used 262 * by the MAC. 263 */ 264 265 void phy_resolve_aneg_pause(struct phy_device *phydev) 266 { 267 if (phydev->duplex == DUPLEX_FULL) { 268 phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 269 phydev->lp_advertising); 270 phydev->asym_pause = linkmode_test_bit( 271 ETHTOOL_LINK_MODE_Asym_Pause_BIT, 272 phydev->lp_advertising); 273 } 274 } 275 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause); 276 277 /** 278 * phy_resolve_aneg_linkmode - resolve the advertisements into PHY settings 279 * @phydev: The phy_device struct 280 * 281 * Resolve our and the link partner advertisements into their corresponding 282 * speed and duplex. If full duplex was negotiated, extract the pause mode 283 * from the link partner mask. 284 */ 285 void phy_resolve_aneg_linkmode(struct phy_device *phydev) 286 { 287 __ETHTOOL_DECLARE_LINK_MODE_MASK(common); 288 const struct link_capabilities *c; 289 290 linkmode_and(common, phydev->lp_advertising, phydev->advertising); 291 292 c = phy_caps_lookup_by_linkmode(common); 293 if (c) { 294 phydev->speed = c->speed; 295 phydev->duplex = c->duplex; 296 } 297 298 phy_resolve_aneg_pause(phydev); 299 } 300 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode); 301 302 /** 303 * phy_check_downshift - check whether downshift occurred 304 * @phydev: The phy_device struct 305 * 306 * Check whether a downshift to a lower speed occurred. If this should be the 307 * case warn the user. 308 * Prerequisite for detecting downshift is that PHY driver implements the 309 * read_status callback and sets phydev->speed to the actual link speed. 310 */ 311 void phy_check_downshift(struct phy_device *phydev) 312 { 313 __ETHTOOL_DECLARE_LINK_MODE_MASK(common); 314 const struct link_capabilities *c; 315 int speed = SPEED_UNKNOWN; 316 317 phydev->downshifted_rate = 0; 318 319 if (phydev->autoneg == AUTONEG_DISABLE || 320 phydev->speed == SPEED_UNKNOWN) 321 return; 322 323 linkmode_and(common, phydev->lp_advertising, phydev->advertising); 324 325 c = phy_caps_lookup_by_linkmode(common); 326 if (c) 327 speed = c->speed; 328 329 if (speed == SPEED_UNKNOWN || phydev->speed >= speed) 330 return; 331 332 phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n", 333 phy_speed_to_str(speed), phy_speed_to_str(phydev->speed)); 334 335 phydev->downshifted_rate = 1; 336 } 337 338 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only) 339 { 340 __ETHTOOL_DECLARE_LINK_MODE_MASK(common); 341 const struct link_capabilities *c; 342 343 linkmode_and(common, phydev->lp_advertising, phydev->advertising); 344 345 c = phy_caps_lookup_by_linkmode_rev(common, fdx_only); 346 if (c) 347 return c->speed; 348 349 return SPEED_UNKNOWN; 350 } 351 352 int phy_speed_down_core(struct phy_device *phydev) 353 { 354 int min_common_speed = phy_resolve_min_speed(phydev, true); 355 356 if (min_common_speed == SPEED_UNKNOWN) 357 return -EINVAL; 358 359 phy_caps_linkmode_max_speed(min_common_speed, phydev->advertising); 360 361 return 0; 362 } 363 364 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad, 365 u16 regnum) 366 { 367 /* Write the desired MMD Devad */ 368 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad); 369 370 /* Write the desired MMD register address */ 371 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum); 372 373 /* Select the Function : DATA with no post increment */ 374 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, 375 devad | MII_MMD_CTRL_NOINCR); 376 } 377 378 int mmd_phy_read(struct mii_bus *bus, int phy_addr, bool is_c45, 379 int devad, u32 regnum) 380 { 381 if (is_c45) 382 return __mdiobus_c45_read(bus, phy_addr, devad, regnum); 383 384 mmd_phy_indirect(bus, phy_addr, devad, regnum); 385 /* Read the content of the MMD's selected register */ 386 return __mdiobus_read(bus, phy_addr, MII_MMD_DATA); 387 } 388 EXPORT_SYMBOL_GPL(mmd_phy_read); 389 390 int mmd_phy_write(struct mii_bus *bus, int phy_addr, bool is_c45, 391 int devad, u32 regnum, u16 val) 392 { 393 if (is_c45) 394 return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val); 395 396 mmd_phy_indirect(bus, phy_addr, devad, regnum); 397 /* Write the data into MMD's selected register */ 398 return __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val); 399 } 400 EXPORT_SYMBOL_GPL(mmd_phy_write); 401 402 /** 403 * __phy_read_mmd - Convenience function for reading a register 404 * from an MMD on a given PHY. 405 * @phydev: The phy_device struct 406 * @devad: The MMD to read from (0..31) 407 * @regnum: The register on the MMD to read (0..65535) 408 * 409 * Same rules as for __phy_read(); 410 */ 411 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) 412 { 413 if (regnum > (u16)~0 || devad > 32) 414 return -EINVAL; 415 416 if (phydev->drv && phydev->drv->read_mmd) 417 return phydev->drv->read_mmd(phydev, devad, regnum); 418 419 return mmd_phy_read(phydev->mdio.bus, phydev->mdio.addr, 420 phydev->is_c45, devad, regnum); 421 } 422 EXPORT_SYMBOL(__phy_read_mmd); 423 424 /** 425 * phy_read_mmd - Convenience function for reading a register 426 * from an MMD on a given PHY. 427 * @phydev: The phy_device struct 428 * @devad: The MMD to read from 429 * @regnum: The register on the MMD to read 430 * 431 * Same rules as for phy_read(); 432 */ 433 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) 434 { 435 int ret; 436 437 phy_lock_mdio_bus(phydev); 438 ret = __phy_read_mmd(phydev, devad, regnum); 439 phy_unlock_mdio_bus(phydev); 440 441 return ret; 442 } 443 EXPORT_SYMBOL(phy_read_mmd); 444 445 /** 446 * __phy_write_mmd - Convenience function for writing a register 447 * on an MMD on a given PHY. 448 * @phydev: The phy_device struct 449 * @devad: The MMD to read from 450 * @regnum: The register on the MMD to read 451 * @val: value to write to @regnum 452 * 453 * Same rules as for __phy_write(); 454 */ 455 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) 456 { 457 if (regnum > (u16)~0 || devad > 32) 458 return -EINVAL; 459 460 if (phydev->drv && phydev->drv->write_mmd) 461 return phydev->drv->write_mmd(phydev, devad, regnum, val); 462 463 return mmd_phy_write(phydev->mdio.bus, phydev->mdio.addr, 464 phydev->is_c45, devad, regnum, val); 465 } 466 EXPORT_SYMBOL(__phy_write_mmd); 467 468 /** 469 * phy_write_mmd - Convenience function for writing a register 470 * on an MMD on a given PHY. 471 * @phydev: The phy_device struct 472 * @devad: The MMD to read from 473 * @regnum: The register on the MMD to read 474 * @val: value to write to @regnum 475 * 476 * Same rules as for phy_write(); 477 */ 478 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) 479 { 480 int ret; 481 482 phy_lock_mdio_bus(phydev); 483 ret = __phy_write_mmd(phydev, devad, regnum, val); 484 phy_unlock_mdio_bus(phydev); 485 486 return ret; 487 } 488 EXPORT_SYMBOL(phy_write_mmd); 489 490 /** 491 * phy_modify_changed - Function for modifying a PHY register 492 * @phydev: the phy_device struct 493 * @regnum: register number to modify 494 * @mask: bit mask of bits to clear 495 * @set: new value of bits set in mask to write to @regnum 496 * 497 * NOTE: MUST NOT be called from interrupt context, 498 * because the bus read/write functions may wait for an interrupt 499 * to conclude the operation. 500 * 501 * Returns negative errno, 0 if there was no change, and 1 in case of change 502 */ 503 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) 504 { 505 int ret; 506 507 phy_lock_mdio_bus(phydev); 508 ret = __phy_modify_changed(phydev, regnum, mask, set); 509 phy_unlock_mdio_bus(phydev); 510 511 return ret; 512 } 513 EXPORT_SYMBOL_GPL(phy_modify_changed); 514 515 /** 516 * __phy_modify - Convenience function for modifying a PHY register 517 * @phydev: the phy_device struct 518 * @regnum: register number to modify 519 * @mask: bit mask of bits to clear 520 * @set: new value of bits set in mask to write to @regnum 521 * 522 * NOTE: MUST NOT be called from interrupt context, 523 * because the bus read/write functions may wait for an interrupt 524 * to conclude the operation. 525 */ 526 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) 527 { 528 int ret; 529 530 ret = __phy_modify_changed(phydev, regnum, mask, set); 531 532 return ret < 0 ? ret : 0; 533 } 534 EXPORT_SYMBOL_GPL(__phy_modify); 535 536 /** 537 * phy_modify - Convenience function for modifying a given PHY register 538 * @phydev: the phy_device struct 539 * @regnum: register number to write 540 * @mask: bit mask of bits to clear 541 * @set: new value of bits set in mask to write to @regnum 542 * 543 * NOTE: MUST NOT be called from interrupt context, 544 * because the bus read/write functions may wait for an interrupt 545 * to conclude the operation. 546 */ 547 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) 548 { 549 int ret; 550 551 phy_lock_mdio_bus(phydev); 552 ret = __phy_modify(phydev, regnum, mask, set); 553 phy_unlock_mdio_bus(phydev); 554 555 return ret; 556 } 557 EXPORT_SYMBOL_GPL(phy_modify); 558 559 /** 560 * __phy_modify_mmd_changed - Function for modifying a register on MMD 561 * @phydev: the phy_device struct 562 * @devad: the MMD containing register to modify 563 * @regnum: register number to modify 564 * @mask: bit mask of bits to clear 565 * @set: new value of bits set in mask to write to @regnum 566 * 567 * Unlocked helper function which allows a MMD register to be modified as 568 * new register value = (old register value & ~mask) | set 569 * 570 * Returns negative errno, 0 if there was no change, and 1 in case of change 571 */ 572 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum, 573 u16 mask, u16 set) 574 { 575 int new, ret; 576 577 ret = __phy_read_mmd(phydev, devad, regnum); 578 if (ret < 0) 579 return ret; 580 581 new = (ret & ~mask) | set; 582 if (new == ret) 583 return 0; 584 585 ret = __phy_write_mmd(phydev, devad, regnum, new); 586 587 return ret < 0 ? ret : 1; 588 } 589 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed); 590 591 /** 592 * phy_modify_mmd_changed - Function for modifying a register on MMD 593 * @phydev: the phy_device struct 594 * @devad: the MMD containing register to modify 595 * @regnum: register number to modify 596 * @mask: bit mask of bits to clear 597 * @set: new value of bits set in mask to write to @regnum 598 * 599 * NOTE: MUST NOT be called from interrupt context, 600 * because the bus read/write functions may wait for an interrupt 601 * to conclude the operation. 602 * 603 * Returns negative errno, 0 if there was no change, and 1 in case of change 604 */ 605 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum, 606 u16 mask, u16 set) 607 { 608 int ret; 609 610 phy_lock_mdio_bus(phydev); 611 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set); 612 phy_unlock_mdio_bus(phydev); 613 614 return ret; 615 } 616 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed); 617 618 /** 619 * __phy_modify_mmd - Convenience function for modifying a register on MMD 620 * @phydev: the phy_device struct 621 * @devad: the MMD containing register to modify 622 * @regnum: register number to modify 623 * @mask: bit mask of bits to clear 624 * @set: new value of bits set in mask to write to @regnum 625 * 626 * NOTE: MUST NOT be called from interrupt context, 627 * because the bus read/write functions may wait for an interrupt 628 * to conclude the operation. 629 */ 630 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum, 631 u16 mask, u16 set) 632 { 633 int ret; 634 635 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set); 636 637 return ret < 0 ? ret : 0; 638 } 639 EXPORT_SYMBOL_GPL(__phy_modify_mmd); 640 641 /** 642 * phy_modify_mmd - Convenience function for modifying a register on MMD 643 * @phydev: the phy_device struct 644 * @devad: the MMD containing register to modify 645 * @regnum: register number to modify 646 * @mask: bit mask of bits to clear 647 * @set: new value of bits set in mask to write to @regnum 648 * 649 * NOTE: MUST NOT be called from interrupt context, 650 * because the bus read/write functions may wait for an interrupt 651 * to conclude the operation. 652 */ 653 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum, 654 u16 mask, u16 set) 655 { 656 int ret; 657 658 phy_lock_mdio_bus(phydev); 659 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set); 660 phy_unlock_mdio_bus(phydev); 661 662 return ret; 663 } 664 EXPORT_SYMBOL_GPL(phy_modify_mmd); 665 666 static int __phy_read_page(struct phy_device *phydev) 667 { 668 if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n")) 669 return -EOPNOTSUPP; 670 671 return phydev->drv->read_page(phydev); 672 } 673 674 static int __phy_write_page(struct phy_device *phydev, int page) 675 { 676 if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n")) 677 return -EOPNOTSUPP; 678 679 return phydev->drv->write_page(phydev, page); 680 } 681 682 /** 683 * phy_save_page() - take the bus lock and save the current page 684 * @phydev: a pointer to a &struct phy_device 685 * 686 * Take the MDIO bus lock, and return the current page number. On error, 687 * returns a negative errno. phy_restore_page() must always be called 688 * after this, irrespective of success or failure of this call. 689 */ 690 int phy_save_page(struct phy_device *phydev) 691 { 692 phy_lock_mdio_bus(phydev); 693 return __phy_read_page(phydev); 694 } 695 EXPORT_SYMBOL_GPL(phy_save_page); 696 697 /** 698 * phy_select_page() - take the bus lock, save the current page, and set a page 699 * @phydev: a pointer to a &struct phy_device 700 * @page: desired page 701 * 702 * Take the MDIO bus lock to protect against concurrent access, save the 703 * current PHY page, and set the current page. On error, returns a 704 * negative errno, otherwise returns the previous page number. 705 * phy_restore_page() must always be called after this, irrespective 706 * of success or failure of this call. 707 */ 708 int phy_select_page(struct phy_device *phydev, int page) 709 { 710 int ret, oldpage; 711 712 oldpage = ret = phy_save_page(phydev); 713 if (ret < 0) 714 return ret; 715 716 if (oldpage != page) { 717 ret = __phy_write_page(phydev, page); 718 if (ret < 0) 719 return ret; 720 } 721 722 return oldpage; 723 } 724 EXPORT_SYMBOL_GPL(phy_select_page); 725 726 /** 727 * phy_restore_page() - restore the page register and release the bus lock 728 * @phydev: a pointer to a &struct phy_device 729 * @oldpage: the old page, return value from phy_save_page() or phy_select_page() 730 * @ret: operation's return code 731 * 732 * Release the MDIO bus lock, restoring @oldpage if it is a valid page. 733 * This function propagates the earliest error code from the group of 734 * operations. 735 * 736 * Returns: 737 * @oldpage if it was a negative value, otherwise 738 * @ret if it was a negative errno value, otherwise 739 * phy_write_page()'s negative value if it were in error, otherwise 740 * @ret. 741 */ 742 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret) 743 { 744 int r; 745 746 if (oldpage >= 0) { 747 r = __phy_write_page(phydev, oldpage); 748 749 /* Propagate the operation return code if the page write 750 * was successful. 751 */ 752 if (ret >= 0 && r < 0) 753 ret = r; 754 } else { 755 /* Propagate the phy page selection error code */ 756 ret = oldpage; 757 } 758 759 phy_unlock_mdio_bus(phydev); 760 761 return ret; 762 } 763 EXPORT_SYMBOL_GPL(phy_restore_page); 764 765 /** 766 * phy_read_paged() - Convenience function for reading a paged register 767 * @phydev: a pointer to a &struct phy_device 768 * @page: the page for the phy 769 * @regnum: register number 770 * 771 * Same rules as for phy_read(). 772 */ 773 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum) 774 { 775 int ret = 0, oldpage; 776 777 oldpage = phy_select_page(phydev, page); 778 if (oldpage >= 0) 779 ret = __phy_read(phydev, regnum); 780 781 return phy_restore_page(phydev, oldpage, ret); 782 } 783 EXPORT_SYMBOL(phy_read_paged); 784 785 /** 786 * phy_write_paged() - Convenience function for writing a paged register 787 * @phydev: a pointer to a &struct phy_device 788 * @page: the page for the phy 789 * @regnum: register number 790 * @val: value to write 791 * 792 * Same rules as for phy_write(). 793 */ 794 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val) 795 { 796 int ret = 0, oldpage; 797 798 oldpage = phy_select_page(phydev, page); 799 if (oldpage >= 0) 800 ret = __phy_write(phydev, regnum, val); 801 802 return phy_restore_page(phydev, oldpage, ret); 803 } 804 EXPORT_SYMBOL(phy_write_paged); 805 806 /** 807 * phy_modify_paged_changed() - Function for modifying a paged register 808 * @phydev: a pointer to a &struct phy_device 809 * @page: the page for the phy 810 * @regnum: register number 811 * @mask: bit mask of bits to clear 812 * @set: bit mask of bits to set 813 * 814 * Returns negative errno, 0 if there was no change, and 1 in case of change 815 */ 816 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum, 817 u16 mask, u16 set) 818 { 819 int ret = 0, oldpage; 820 821 oldpage = phy_select_page(phydev, page); 822 if (oldpage >= 0) 823 ret = __phy_modify_changed(phydev, regnum, mask, set); 824 825 return phy_restore_page(phydev, oldpage, ret); 826 } 827 EXPORT_SYMBOL(phy_modify_paged_changed); 828 829 /** 830 * phy_modify_paged() - Convenience function for modifying a paged register 831 * @phydev: a pointer to a &struct phy_device 832 * @page: the page for the phy 833 * @regnum: register number 834 * @mask: bit mask of bits to clear 835 * @set: bit mask of bits to set 836 * 837 * Same rules as for phy_read() and phy_write(). 838 */ 839 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum, 840 u16 mask, u16 set) 841 { 842 int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set); 843 844 return ret < 0 ? ret : 0; 845 } 846 EXPORT_SYMBOL(phy_modify_paged); 847