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