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 const char *phy_speed_to_str(int speed) 10 { 11 BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 92, 12 "Enum ethtool_link_mode_bit_indices and phylib are out of sync. " 13 "If a speed or mode has been added please update phy_speed_to_str " 14 "and the PHY settings array.\n"); 15 16 switch (speed) { 17 case SPEED_10: 18 return "10Mbps"; 19 case SPEED_100: 20 return "100Mbps"; 21 case SPEED_1000: 22 return "1Gbps"; 23 case SPEED_2500: 24 return "2.5Gbps"; 25 case SPEED_5000: 26 return "5Gbps"; 27 case SPEED_10000: 28 return "10Gbps"; 29 case SPEED_14000: 30 return "14Gbps"; 31 case SPEED_20000: 32 return "20Gbps"; 33 case SPEED_25000: 34 return "25Gbps"; 35 case SPEED_40000: 36 return "40Gbps"; 37 case SPEED_50000: 38 return "50Gbps"; 39 case SPEED_56000: 40 return "56Gbps"; 41 case SPEED_100000: 42 return "100Gbps"; 43 case SPEED_200000: 44 return "200Gbps"; 45 case SPEED_400000: 46 return "400Gbps"; 47 case SPEED_UNKNOWN: 48 return "Unknown"; 49 default: 50 return "Unsupported (update phy-core.c)"; 51 } 52 } 53 EXPORT_SYMBOL_GPL(phy_speed_to_str); 54 55 const char *phy_duplex_to_str(unsigned int duplex) 56 { 57 if (duplex == DUPLEX_HALF) 58 return "Half"; 59 if (duplex == DUPLEX_FULL) 60 return "Full"; 61 if (duplex == DUPLEX_UNKNOWN) 62 return "Unknown"; 63 return "Unsupported (update phy-core.c)"; 64 } 65 EXPORT_SYMBOL_GPL(phy_duplex_to_str); 66 67 /* A mapping of all SUPPORTED settings to speed/duplex. This table 68 * must be grouped by speed and sorted in descending match priority 69 * - iow, descending speed. */ 70 71 #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \ 72 .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT} 73 74 static const struct phy_setting settings[] = { 75 /* 400G */ 76 PHY_SETTING( 400000, FULL, 400000baseCR8_Full ), 77 PHY_SETTING( 400000, FULL, 400000baseKR8_Full ), 78 PHY_SETTING( 400000, FULL, 400000baseLR8_ER8_FR8_Full ), 79 PHY_SETTING( 400000, FULL, 400000baseDR8_Full ), 80 PHY_SETTING( 400000, FULL, 400000baseSR8_Full ), 81 PHY_SETTING( 400000, FULL, 400000baseCR4_Full ), 82 PHY_SETTING( 400000, FULL, 400000baseKR4_Full ), 83 PHY_SETTING( 400000, FULL, 400000baseLR4_ER4_FR4_Full ), 84 PHY_SETTING( 400000, FULL, 400000baseDR4_Full ), 85 PHY_SETTING( 400000, FULL, 400000baseSR4_Full ), 86 /* 200G */ 87 PHY_SETTING( 200000, FULL, 200000baseCR4_Full ), 88 PHY_SETTING( 200000, FULL, 200000baseKR4_Full ), 89 PHY_SETTING( 200000, FULL, 200000baseLR4_ER4_FR4_Full ), 90 PHY_SETTING( 200000, FULL, 200000baseDR4_Full ), 91 PHY_SETTING( 200000, FULL, 200000baseSR4_Full ), 92 PHY_SETTING( 200000, FULL, 200000baseCR2_Full ), 93 PHY_SETTING( 200000, FULL, 200000baseKR2_Full ), 94 PHY_SETTING( 200000, FULL, 200000baseLR2_ER2_FR2_Full ), 95 PHY_SETTING( 200000, FULL, 200000baseDR2_Full ), 96 PHY_SETTING( 200000, FULL, 200000baseSR2_Full ), 97 /* 100G */ 98 PHY_SETTING( 100000, FULL, 100000baseCR4_Full ), 99 PHY_SETTING( 100000, FULL, 100000baseKR4_Full ), 100 PHY_SETTING( 100000, FULL, 100000baseLR4_ER4_Full ), 101 PHY_SETTING( 100000, FULL, 100000baseSR4_Full ), 102 PHY_SETTING( 100000, FULL, 100000baseCR2_Full ), 103 PHY_SETTING( 100000, FULL, 100000baseKR2_Full ), 104 PHY_SETTING( 100000, FULL, 100000baseLR2_ER2_FR2_Full ), 105 PHY_SETTING( 100000, FULL, 100000baseDR2_Full ), 106 PHY_SETTING( 100000, FULL, 100000baseSR2_Full ), 107 PHY_SETTING( 100000, FULL, 100000baseCR_Full ), 108 PHY_SETTING( 100000, FULL, 100000baseKR_Full ), 109 PHY_SETTING( 100000, FULL, 100000baseLR_ER_FR_Full ), 110 PHY_SETTING( 100000, FULL, 100000baseDR_Full ), 111 PHY_SETTING( 100000, FULL, 100000baseSR_Full ), 112 /* 56G */ 113 PHY_SETTING( 56000, FULL, 56000baseCR4_Full ), 114 PHY_SETTING( 56000, FULL, 56000baseKR4_Full ), 115 PHY_SETTING( 56000, FULL, 56000baseLR4_Full ), 116 PHY_SETTING( 56000, FULL, 56000baseSR4_Full ), 117 /* 50G */ 118 PHY_SETTING( 50000, FULL, 50000baseCR2_Full ), 119 PHY_SETTING( 50000, FULL, 50000baseKR2_Full ), 120 PHY_SETTING( 50000, FULL, 50000baseSR2_Full ), 121 PHY_SETTING( 50000, FULL, 50000baseCR_Full ), 122 PHY_SETTING( 50000, FULL, 50000baseKR_Full ), 123 PHY_SETTING( 50000, FULL, 50000baseLR_ER_FR_Full ), 124 PHY_SETTING( 50000, FULL, 50000baseDR_Full ), 125 PHY_SETTING( 50000, FULL, 50000baseSR_Full ), 126 /* 40G */ 127 PHY_SETTING( 40000, FULL, 40000baseCR4_Full ), 128 PHY_SETTING( 40000, FULL, 40000baseKR4_Full ), 129 PHY_SETTING( 40000, FULL, 40000baseLR4_Full ), 130 PHY_SETTING( 40000, FULL, 40000baseSR4_Full ), 131 /* 25G */ 132 PHY_SETTING( 25000, FULL, 25000baseCR_Full ), 133 PHY_SETTING( 25000, FULL, 25000baseKR_Full ), 134 PHY_SETTING( 25000, FULL, 25000baseSR_Full ), 135 /* 20G */ 136 PHY_SETTING( 20000, FULL, 20000baseKR2_Full ), 137 PHY_SETTING( 20000, FULL, 20000baseMLD2_Full ), 138 /* 10G */ 139 PHY_SETTING( 10000, FULL, 10000baseCR_Full ), 140 PHY_SETTING( 10000, FULL, 10000baseER_Full ), 141 PHY_SETTING( 10000, FULL, 10000baseKR_Full ), 142 PHY_SETTING( 10000, FULL, 10000baseKX4_Full ), 143 PHY_SETTING( 10000, FULL, 10000baseLR_Full ), 144 PHY_SETTING( 10000, FULL, 10000baseLRM_Full ), 145 PHY_SETTING( 10000, FULL, 10000baseR_FEC ), 146 PHY_SETTING( 10000, FULL, 10000baseSR_Full ), 147 PHY_SETTING( 10000, FULL, 10000baseT_Full ), 148 /* 5G */ 149 PHY_SETTING( 5000, FULL, 5000baseT_Full ), 150 /* 2.5G */ 151 PHY_SETTING( 2500, FULL, 2500baseT_Full ), 152 PHY_SETTING( 2500, FULL, 2500baseX_Full ), 153 /* 1G */ 154 PHY_SETTING( 1000, FULL, 1000baseKX_Full ), 155 PHY_SETTING( 1000, FULL, 1000baseT_Full ), 156 PHY_SETTING( 1000, HALF, 1000baseT_Half ), 157 PHY_SETTING( 1000, FULL, 1000baseT1_Full ), 158 PHY_SETTING( 1000, FULL, 1000baseX_Full ), 159 /* 100M */ 160 PHY_SETTING( 100, FULL, 100baseT_Full ), 161 PHY_SETTING( 100, FULL, 100baseT1_Full ), 162 PHY_SETTING( 100, HALF, 100baseT_Half ), 163 PHY_SETTING( 100, HALF, 100baseFX_Half ), 164 PHY_SETTING( 100, FULL, 100baseFX_Full ), 165 /* 10M */ 166 PHY_SETTING( 10, FULL, 10baseT_Full ), 167 PHY_SETTING( 10, HALF, 10baseT_Half ), 168 }; 169 #undef PHY_SETTING 170 171 /** 172 * phy_lookup_setting - lookup a PHY setting 173 * @speed: speed to match 174 * @duplex: duplex to match 175 * @mask: allowed link modes 176 * @exact: an exact match is required 177 * 178 * Search the settings array for a setting that matches the speed and 179 * duplex, and which is supported. 180 * 181 * If @exact is unset, either an exact match or %NULL for no match will 182 * be returned. 183 * 184 * If @exact is set, an exact match, the fastest supported setting at 185 * or below the specified speed, the slowest supported setting, or if 186 * they all fail, %NULL will be returned. 187 */ 188 const struct phy_setting * 189 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact) 190 { 191 const struct phy_setting *p, *match = NULL, *last = NULL; 192 int i; 193 194 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) { 195 if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS && 196 test_bit(p->bit, mask)) { 197 last = p; 198 if (p->speed == speed && p->duplex == duplex) { 199 /* Exact match for speed and duplex */ 200 match = p; 201 break; 202 } else if (!exact) { 203 if (!match && p->speed <= speed) 204 /* Candidate */ 205 match = p; 206 207 if (p->speed < speed) 208 break; 209 } 210 } 211 } 212 213 if (!match && !exact) 214 match = last; 215 216 return match; 217 } 218 EXPORT_SYMBOL_GPL(phy_lookup_setting); 219 220 size_t phy_speeds(unsigned int *speeds, size_t size, 221 unsigned long *mask) 222 { 223 size_t count; 224 int i; 225 226 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++) 227 if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS && 228 test_bit(settings[i].bit, mask) && 229 (count == 0 || speeds[count - 1] != settings[i].speed)) 230 speeds[count++] = settings[i].speed; 231 232 return count; 233 } 234 235 static int __set_linkmode_max_speed(u32 max_speed, unsigned long *addr) 236 { 237 const struct phy_setting *p; 238 int i; 239 240 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) { 241 if (p->speed > max_speed) 242 linkmode_clear_bit(p->bit, addr); 243 else 244 break; 245 } 246 247 return 0; 248 } 249 250 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed) 251 { 252 return __set_linkmode_max_speed(max_speed, phydev->supported); 253 } 254 255 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed) 256 { 257 int err; 258 259 err = __set_phy_supported(phydev, max_speed); 260 if (err) 261 return err; 262 263 phy_advertise_supported(phydev); 264 265 return 0; 266 } 267 EXPORT_SYMBOL(phy_set_max_speed); 268 269 void of_set_phy_supported(struct phy_device *phydev) 270 { 271 struct device_node *node = phydev->mdio.dev.of_node; 272 u32 max_speed; 273 274 if (!IS_ENABLED(CONFIG_OF_MDIO)) 275 return; 276 277 if (!node) 278 return; 279 280 if (!of_property_read_u32(node, "max-speed", &max_speed)) 281 __set_phy_supported(phydev, max_speed); 282 } 283 284 void of_set_phy_eee_broken(struct phy_device *phydev) 285 { 286 struct device_node *node = phydev->mdio.dev.of_node; 287 u32 broken = 0; 288 289 if (!IS_ENABLED(CONFIG_OF_MDIO)) 290 return; 291 292 if (!node) 293 return; 294 295 if (of_property_read_bool(node, "eee-broken-100tx")) 296 broken |= MDIO_EEE_100TX; 297 if (of_property_read_bool(node, "eee-broken-1000t")) 298 broken |= MDIO_EEE_1000T; 299 if (of_property_read_bool(node, "eee-broken-10gt")) 300 broken |= MDIO_EEE_10GT; 301 if (of_property_read_bool(node, "eee-broken-1000kx")) 302 broken |= MDIO_EEE_1000KX; 303 if (of_property_read_bool(node, "eee-broken-10gkx4")) 304 broken |= MDIO_EEE_10GKX4; 305 if (of_property_read_bool(node, "eee-broken-10gkr")) 306 broken |= MDIO_EEE_10GKR; 307 308 phydev->eee_broken_modes = broken; 309 } 310 311 void phy_resolve_aneg_pause(struct phy_device *phydev) 312 { 313 if (phydev->duplex == DUPLEX_FULL) { 314 phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 315 phydev->lp_advertising); 316 phydev->asym_pause = linkmode_test_bit( 317 ETHTOOL_LINK_MODE_Asym_Pause_BIT, 318 phydev->lp_advertising); 319 } 320 } 321 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause); 322 323 /** 324 * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings 325 * @phydev: The phy_device struct 326 * 327 * Resolve our and the link partner advertisements into their corresponding 328 * speed and duplex. If full duplex was negotiated, extract the pause mode 329 * from the link partner mask. 330 */ 331 void phy_resolve_aneg_linkmode(struct phy_device *phydev) 332 { 333 __ETHTOOL_DECLARE_LINK_MODE_MASK(common); 334 int i; 335 336 linkmode_and(common, phydev->lp_advertising, phydev->advertising); 337 338 for (i = 0; i < ARRAY_SIZE(settings); i++) 339 if (test_bit(settings[i].bit, common)) { 340 phydev->speed = settings[i].speed; 341 phydev->duplex = settings[i].duplex; 342 break; 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 int i, speed = SPEED_UNKNOWN; 362 363 phydev->downshifted_rate = 0; 364 365 if (phydev->autoneg == AUTONEG_DISABLE || 366 phydev->speed == SPEED_UNKNOWN) 367 return; 368 369 linkmode_and(common, phydev->lp_advertising, phydev->advertising); 370 371 for (i = 0; i < ARRAY_SIZE(settings); i++) 372 if (test_bit(settings[i].bit, common)) { 373 speed = settings[i].speed; 374 break; 375 } 376 377 if (speed == SPEED_UNKNOWN || phydev->speed >= speed) 378 return; 379 380 phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n", 381 phy_speed_to_str(speed), phy_speed_to_str(phydev->speed)); 382 383 phydev->downshifted_rate = 1; 384 } 385 EXPORT_SYMBOL_GPL(phy_check_downshift); 386 387 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only) 388 { 389 __ETHTOOL_DECLARE_LINK_MODE_MASK(common); 390 int i = ARRAY_SIZE(settings); 391 392 linkmode_and(common, phydev->lp_advertising, phydev->advertising); 393 394 while (--i >= 0) { 395 if (test_bit(settings[i].bit, common)) { 396 if (fdx_only && settings[i].duplex != DUPLEX_FULL) 397 continue; 398 return settings[i].speed; 399 } 400 } 401 402 return SPEED_UNKNOWN; 403 } 404 405 int phy_speed_down_core(struct phy_device *phydev) 406 { 407 int min_common_speed = phy_resolve_min_speed(phydev, true); 408 409 if (min_common_speed == SPEED_UNKNOWN) 410 return -EINVAL; 411 412 return __set_linkmode_max_speed(min_common_speed, phydev->advertising); 413 } 414 415 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad, 416 u16 regnum) 417 { 418 /* Write the desired MMD Devad */ 419 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad); 420 421 /* Write the desired MMD register address */ 422 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum); 423 424 /* Select the Function : DATA with no post increment */ 425 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, 426 devad | MII_MMD_CTRL_NOINCR); 427 } 428 429 /** 430 * __phy_read_mmd - Convenience function for reading a register 431 * from an MMD on a given PHY. 432 * @phydev: The phy_device struct 433 * @devad: The MMD to read from (0..31) 434 * @regnum: The register on the MMD to read (0..65535) 435 * 436 * Same rules as for __phy_read(); 437 */ 438 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) 439 { 440 int val; 441 442 if (regnum > (u16)~0 || devad > 32) 443 return -EINVAL; 444 445 if (phydev->drv && phydev->drv->read_mmd) { 446 val = phydev->drv->read_mmd(phydev, devad, regnum); 447 } else if (phydev->is_c45) { 448 val = __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr, 449 devad, regnum); 450 } else { 451 struct mii_bus *bus = phydev->mdio.bus; 452 int phy_addr = phydev->mdio.addr; 453 454 mmd_phy_indirect(bus, phy_addr, devad, regnum); 455 456 /* Read the content of the MMD's selected register */ 457 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA); 458 } 459 return val; 460 } 461 EXPORT_SYMBOL(__phy_read_mmd); 462 463 /** 464 * phy_read_mmd - Convenience function for reading a register 465 * from an MMD on a given PHY. 466 * @phydev: The phy_device struct 467 * @devad: The MMD to read from 468 * @regnum: The register on the MMD to read 469 * 470 * Same rules as for phy_read(); 471 */ 472 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) 473 { 474 int ret; 475 476 phy_lock_mdio_bus(phydev); 477 ret = __phy_read_mmd(phydev, devad, regnum); 478 phy_unlock_mdio_bus(phydev); 479 480 return ret; 481 } 482 EXPORT_SYMBOL(phy_read_mmd); 483 484 /** 485 * __phy_write_mmd - Convenience function for writing a register 486 * on an MMD on a given PHY. 487 * @phydev: The phy_device struct 488 * @devad: The MMD to read from 489 * @regnum: The register on the MMD to read 490 * @val: value to write to @regnum 491 * 492 * Same rules as for __phy_write(); 493 */ 494 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) 495 { 496 int ret; 497 498 if (regnum > (u16)~0 || devad > 32) 499 return -EINVAL; 500 501 if (phydev->drv && phydev->drv->write_mmd) { 502 ret = phydev->drv->write_mmd(phydev, devad, regnum, val); 503 } else if (phydev->is_c45) { 504 ret = __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr, 505 devad, regnum, val); 506 } else { 507 struct mii_bus *bus = phydev->mdio.bus; 508 int phy_addr = phydev->mdio.addr; 509 510 mmd_phy_indirect(bus, phy_addr, devad, regnum); 511 512 /* Write the data into MMD's selected register */ 513 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val); 514 515 ret = 0; 516 } 517 return ret; 518 } 519 EXPORT_SYMBOL(__phy_write_mmd); 520 521 /** 522 * phy_write_mmd - Convenience function for writing a register 523 * on an MMD on a given PHY. 524 * @phydev: The phy_device struct 525 * @devad: The MMD to read from 526 * @regnum: The register on the MMD to read 527 * @val: value to write to @regnum 528 * 529 * Same rules as for phy_write(); 530 */ 531 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val) 532 { 533 int ret; 534 535 phy_lock_mdio_bus(phydev); 536 ret = __phy_write_mmd(phydev, devad, regnum, val); 537 phy_unlock_mdio_bus(phydev); 538 539 return ret; 540 } 541 EXPORT_SYMBOL(phy_write_mmd); 542 543 /** 544 * phy_modify_changed - Function for modifying a PHY register 545 * @phydev: the phy_device struct 546 * @regnum: register number to modify 547 * @mask: bit mask of bits to clear 548 * @set: new value of bits set in mask to write to @regnum 549 * 550 * NOTE: MUST NOT be called from interrupt context, 551 * because the bus read/write functions may wait for an interrupt 552 * to conclude the operation. 553 * 554 * Returns negative errno, 0 if there was no change, and 1 in case of change 555 */ 556 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) 557 { 558 int ret; 559 560 phy_lock_mdio_bus(phydev); 561 ret = __phy_modify_changed(phydev, regnum, mask, set); 562 phy_unlock_mdio_bus(phydev); 563 564 return ret; 565 } 566 EXPORT_SYMBOL_GPL(phy_modify_changed); 567 568 /** 569 * __phy_modify - Convenience function for modifying a PHY register 570 * @phydev: the phy_device struct 571 * @regnum: register number to modify 572 * @mask: bit mask of bits to clear 573 * @set: new value of bits set in mask to write to @regnum 574 * 575 * NOTE: MUST NOT be called from interrupt context, 576 * because the bus read/write functions may wait for an interrupt 577 * to conclude the operation. 578 */ 579 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) 580 { 581 int ret; 582 583 ret = __phy_modify_changed(phydev, regnum, mask, set); 584 585 return ret < 0 ? ret : 0; 586 } 587 EXPORT_SYMBOL_GPL(__phy_modify); 588 589 /** 590 * phy_modify - Convenience function for modifying a given PHY register 591 * @phydev: the phy_device struct 592 * @regnum: register number to write 593 * @mask: bit mask of bits to clear 594 * @set: new value of bits set in mask to write to @regnum 595 * 596 * NOTE: MUST NOT be called from interrupt context, 597 * because the bus read/write functions may wait for an interrupt 598 * to conclude the operation. 599 */ 600 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set) 601 { 602 int ret; 603 604 phy_lock_mdio_bus(phydev); 605 ret = __phy_modify(phydev, regnum, mask, set); 606 phy_unlock_mdio_bus(phydev); 607 608 return ret; 609 } 610 EXPORT_SYMBOL_GPL(phy_modify); 611 612 /** 613 * __phy_modify_mmd_changed - Function for modifying a register on MMD 614 * @phydev: the phy_device struct 615 * @devad: the MMD containing register to modify 616 * @regnum: register number to modify 617 * @mask: bit mask of bits to clear 618 * @set: new value of bits set in mask to write to @regnum 619 * 620 * Unlocked helper function which allows a MMD register to be modified as 621 * new register value = (old register value & ~mask) | set 622 * 623 * Returns negative errno, 0 if there was no change, and 1 in case of change 624 */ 625 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum, 626 u16 mask, u16 set) 627 { 628 int new, ret; 629 630 ret = __phy_read_mmd(phydev, devad, regnum); 631 if (ret < 0) 632 return ret; 633 634 new = (ret & ~mask) | set; 635 if (new == ret) 636 return 0; 637 638 ret = __phy_write_mmd(phydev, devad, regnum, new); 639 640 return ret < 0 ? ret : 1; 641 } 642 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed); 643 644 /** 645 * phy_modify_mmd_changed - Function for modifying a register on MMD 646 * @phydev: the phy_device struct 647 * @devad: the MMD containing register to modify 648 * @regnum: register number to modify 649 * @mask: bit mask of bits to clear 650 * @set: new value of bits set in mask to write to @regnum 651 * 652 * NOTE: MUST NOT be called from interrupt context, 653 * because the bus read/write functions may wait for an interrupt 654 * to conclude the operation. 655 * 656 * Returns negative errno, 0 if there was no change, and 1 in case of change 657 */ 658 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum, 659 u16 mask, u16 set) 660 { 661 int ret; 662 663 phy_lock_mdio_bus(phydev); 664 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set); 665 phy_unlock_mdio_bus(phydev); 666 667 return ret; 668 } 669 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed); 670 671 /** 672 * __phy_modify_mmd - Convenience function for modifying a register on MMD 673 * @phydev: the phy_device struct 674 * @devad: the MMD containing register to modify 675 * @regnum: register number to modify 676 * @mask: bit mask of bits to clear 677 * @set: new value of bits set in mask to write to @regnum 678 * 679 * NOTE: MUST NOT be called from interrupt context, 680 * because the bus read/write functions may wait for an interrupt 681 * to conclude the operation. 682 */ 683 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum, 684 u16 mask, u16 set) 685 { 686 int ret; 687 688 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set); 689 690 return ret < 0 ? ret : 0; 691 } 692 EXPORT_SYMBOL_GPL(__phy_modify_mmd); 693 694 /** 695 * phy_modify_mmd - Convenience function for modifying a register on MMD 696 * @phydev: the phy_device struct 697 * @devad: the MMD containing register to modify 698 * @regnum: register number to modify 699 * @mask: bit mask of bits to clear 700 * @set: new value of bits set in mask to write to @regnum 701 * 702 * NOTE: MUST NOT be called from interrupt context, 703 * because the bus read/write functions may wait for an interrupt 704 * to conclude the operation. 705 */ 706 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum, 707 u16 mask, u16 set) 708 { 709 int ret; 710 711 phy_lock_mdio_bus(phydev); 712 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set); 713 phy_unlock_mdio_bus(phydev); 714 715 return ret; 716 } 717 EXPORT_SYMBOL_GPL(phy_modify_mmd); 718 719 static int __phy_read_page(struct phy_device *phydev) 720 { 721 if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n")) 722 return -EOPNOTSUPP; 723 724 return phydev->drv->read_page(phydev); 725 } 726 727 static int __phy_write_page(struct phy_device *phydev, int page) 728 { 729 if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n")) 730 return -EOPNOTSUPP; 731 732 return phydev->drv->write_page(phydev, page); 733 } 734 735 /** 736 * phy_save_page() - take the bus lock and save the current page 737 * @phydev: a pointer to a &struct phy_device 738 * 739 * Take the MDIO bus lock, and return the current page number. On error, 740 * returns a negative errno. phy_restore_page() must always be called 741 * after this, irrespective of success or failure of this call. 742 */ 743 int phy_save_page(struct phy_device *phydev) 744 { 745 phy_lock_mdio_bus(phydev); 746 return __phy_read_page(phydev); 747 } 748 EXPORT_SYMBOL_GPL(phy_save_page); 749 750 /** 751 * phy_select_page() - take the bus lock, save the current page, and set a page 752 * @phydev: a pointer to a &struct phy_device 753 * @page: desired page 754 * 755 * Take the MDIO bus lock to protect against concurrent access, save the 756 * current PHY page, and set the current page. On error, returns a 757 * negative errno, otherwise returns the previous page number. 758 * phy_restore_page() must always be called after this, irrespective 759 * of success or failure of this call. 760 */ 761 int phy_select_page(struct phy_device *phydev, int page) 762 { 763 int ret, oldpage; 764 765 oldpage = ret = phy_save_page(phydev); 766 if (ret < 0) 767 return ret; 768 769 if (oldpage != page) { 770 ret = __phy_write_page(phydev, page); 771 if (ret < 0) 772 return ret; 773 } 774 775 return oldpage; 776 } 777 EXPORT_SYMBOL_GPL(phy_select_page); 778 779 /** 780 * phy_restore_page() - restore the page register and release the bus lock 781 * @phydev: a pointer to a &struct phy_device 782 * @oldpage: the old page, return value from phy_save_page() or phy_select_page() 783 * @ret: operation's return code 784 * 785 * Release the MDIO bus lock, restoring @oldpage if it is a valid page. 786 * This function propagates the earliest error code from the group of 787 * operations. 788 * 789 * Returns: 790 * @oldpage if it was a negative value, otherwise 791 * @ret if it was a negative errno value, otherwise 792 * phy_write_page()'s negative value if it were in error, otherwise 793 * @ret. 794 */ 795 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret) 796 { 797 int r; 798 799 if (oldpage >= 0) { 800 r = __phy_write_page(phydev, oldpage); 801 802 /* Propagate the operation return code if the page write 803 * was successful. 804 */ 805 if (ret >= 0 && r < 0) 806 ret = r; 807 } else { 808 /* Propagate the phy page selection error code */ 809 ret = oldpage; 810 } 811 812 phy_unlock_mdio_bus(phydev); 813 814 return ret; 815 } 816 EXPORT_SYMBOL_GPL(phy_restore_page); 817 818 /** 819 * phy_read_paged() - Convenience function for reading a paged register 820 * @phydev: a pointer to a &struct phy_device 821 * @page: the page for the phy 822 * @regnum: register number 823 * 824 * Same rules as for phy_read(). 825 */ 826 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum) 827 { 828 int ret = 0, oldpage; 829 830 oldpage = phy_select_page(phydev, page); 831 if (oldpage >= 0) 832 ret = __phy_read(phydev, regnum); 833 834 return phy_restore_page(phydev, oldpage, ret); 835 } 836 EXPORT_SYMBOL(phy_read_paged); 837 838 /** 839 * phy_write_paged() - Convenience function for writing a paged register 840 * @phydev: a pointer to a &struct phy_device 841 * @page: the page for the phy 842 * @regnum: register number 843 * @val: value to write 844 * 845 * Same rules as for phy_write(). 846 */ 847 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val) 848 { 849 int ret = 0, oldpage; 850 851 oldpage = phy_select_page(phydev, page); 852 if (oldpage >= 0) 853 ret = __phy_write(phydev, regnum, val); 854 855 return phy_restore_page(phydev, oldpage, ret); 856 } 857 EXPORT_SYMBOL(phy_write_paged); 858 859 /** 860 * phy_modify_paged_changed() - Function for modifying a paged register 861 * @phydev: a pointer to a &struct phy_device 862 * @page: the page for the phy 863 * @regnum: register number 864 * @mask: bit mask of bits to clear 865 * @set: bit mask of bits to set 866 * 867 * Returns negative errno, 0 if there was no change, and 1 in case of change 868 */ 869 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum, 870 u16 mask, u16 set) 871 { 872 int ret = 0, oldpage; 873 874 oldpage = phy_select_page(phydev, page); 875 if (oldpage >= 0) 876 ret = __phy_modify_changed(phydev, regnum, mask, set); 877 878 return phy_restore_page(phydev, oldpage, ret); 879 } 880 EXPORT_SYMBOL(phy_modify_paged_changed); 881 882 /** 883 * phy_modify_paged() - Convenience function for modifying a paged register 884 * @phydev: a pointer to a &struct phy_device 885 * @page: the page for the phy 886 * @regnum: register number 887 * @mask: bit mask of bits to clear 888 * @set: bit mask of bits to set 889 * 890 * Same rules as for phy_read() and phy_write(). 891 */ 892 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum, 893 u16 mask, u16 set) 894 { 895 int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set); 896 897 return ret < 0 ? ret : 0; 898 } 899 EXPORT_SYMBOL(phy_modify_paged); 900