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