1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2020 Synopsys, Inc. and/or its affiliates. 4 * Synopsys DesignWare XPCS helpers 5 * 6 * Author: Jose Abreu <Jose.Abreu@synopsys.com> 7 */ 8 9 #include <linux/delay.h> 10 #include <linux/pcs/pcs-xpcs.h> 11 #include <linux/mdio.h> 12 #include <linux/phylink.h> 13 #include <linux/workqueue.h> 14 #include "pcs-xpcs.h" 15 16 #define phylink_pcs_to_xpcs(pl_pcs) \ 17 container_of((pl_pcs), struct dw_xpcs, pcs) 18 19 static const int xpcs_usxgmii_features[] = { 20 ETHTOOL_LINK_MODE_Pause_BIT, 21 ETHTOOL_LINK_MODE_Asym_Pause_BIT, 22 ETHTOOL_LINK_MODE_Autoneg_BIT, 23 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 24 ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 25 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 26 ETHTOOL_LINK_MODE_2500baseX_Full_BIT, 27 __ETHTOOL_LINK_MODE_MASK_NBITS, 28 }; 29 30 static const int xpcs_10gkr_features[] = { 31 ETHTOOL_LINK_MODE_Pause_BIT, 32 ETHTOOL_LINK_MODE_Asym_Pause_BIT, 33 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 34 __ETHTOOL_LINK_MODE_MASK_NBITS, 35 }; 36 37 static const int xpcs_xlgmii_features[] = { 38 ETHTOOL_LINK_MODE_Pause_BIT, 39 ETHTOOL_LINK_MODE_Asym_Pause_BIT, 40 ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, 41 ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, 42 ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, 43 ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, 44 ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, 45 ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, 46 ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, 47 ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, 48 ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, 49 ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, 50 ETHTOOL_LINK_MODE_50000baseKR_Full_BIT, 51 ETHTOOL_LINK_MODE_50000baseSR_Full_BIT, 52 ETHTOOL_LINK_MODE_50000baseCR_Full_BIT, 53 ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT, 54 ETHTOOL_LINK_MODE_50000baseDR_Full_BIT, 55 ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, 56 ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, 57 ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, 58 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT, 59 ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT, 60 ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT, 61 ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT, 62 ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT, 63 ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT, 64 __ETHTOOL_LINK_MODE_MASK_NBITS, 65 }; 66 67 static const int xpcs_sgmii_features[] = { 68 ETHTOOL_LINK_MODE_Pause_BIT, 69 ETHTOOL_LINK_MODE_Asym_Pause_BIT, 70 ETHTOOL_LINK_MODE_Autoneg_BIT, 71 ETHTOOL_LINK_MODE_10baseT_Half_BIT, 72 ETHTOOL_LINK_MODE_10baseT_Full_BIT, 73 ETHTOOL_LINK_MODE_100baseT_Half_BIT, 74 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 75 ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 76 ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 77 __ETHTOOL_LINK_MODE_MASK_NBITS, 78 }; 79 80 static const int xpcs_2500basex_features[] = { 81 ETHTOOL_LINK_MODE_Pause_BIT, 82 ETHTOOL_LINK_MODE_Asym_Pause_BIT, 83 ETHTOOL_LINK_MODE_Autoneg_BIT, 84 ETHTOOL_LINK_MODE_2500baseX_Full_BIT, 85 ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 86 __ETHTOOL_LINK_MODE_MASK_NBITS, 87 }; 88 89 static const phy_interface_t xpcs_usxgmii_interfaces[] = { 90 PHY_INTERFACE_MODE_USXGMII, 91 }; 92 93 static const phy_interface_t xpcs_10gkr_interfaces[] = { 94 PHY_INTERFACE_MODE_10GKR, 95 }; 96 97 static const phy_interface_t xpcs_xlgmii_interfaces[] = { 98 PHY_INTERFACE_MODE_XLGMII, 99 }; 100 101 static const phy_interface_t xpcs_sgmii_interfaces[] = { 102 PHY_INTERFACE_MODE_SGMII, 103 }; 104 105 static const phy_interface_t xpcs_2500basex_interfaces[] = { 106 PHY_INTERFACE_MODE_2500BASEX, 107 PHY_INTERFACE_MODE_MAX, 108 }; 109 110 enum { 111 DW_XPCS_USXGMII, 112 DW_XPCS_10GKR, 113 DW_XPCS_XLGMII, 114 DW_XPCS_SGMII, 115 DW_XPCS_2500BASEX, 116 DW_XPCS_INTERFACE_MAX, 117 }; 118 119 struct xpcs_compat { 120 const int *supported; 121 const phy_interface_t *interface; 122 int num_interfaces; 123 int an_mode; 124 int (*pma_config)(struct dw_xpcs *xpcs); 125 }; 126 127 struct xpcs_id { 128 u32 id; 129 u32 mask; 130 const struct xpcs_compat *compat; 131 }; 132 133 static const struct xpcs_compat *xpcs_find_compat(const struct xpcs_id *id, 134 phy_interface_t interface) 135 { 136 int i, j; 137 138 for (i = 0; i < DW_XPCS_INTERFACE_MAX; i++) { 139 const struct xpcs_compat *compat = &id->compat[i]; 140 141 for (j = 0; j < compat->num_interfaces; j++) 142 if (compat->interface[j] == interface) 143 return compat; 144 } 145 146 return NULL; 147 } 148 149 int xpcs_get_an_mode(struct dw_xpcs *xpcs, phy_interface_t interface) 150 { 151 const struct xpcs_compat *compat; 152 153 compat = xpcs_find_compat(xpcs->id, interface); 154 if (!compat) 155 return -ENODEV; 156 157 return compat->an_mode; 158 } 159 EXPORT_SYMBOL_GPL(xpcs_get_an_mode); 160 161 static bool __xpcs_linkmode_supported(const struct xpcs_compat *compat, 162 enum ethtool_link_mode_bit_indices linkmode) 163 { 164 int i; 165 166 for (i = 0; compat->supported[i] != __ETHTOOL_LINK_MODE_MASK_NBITS; i++) 167 if (compat->supported[i] == linkmode) 168 return true; 169 170 return false; 171 } 172 173 #define xpcs_linkmode_supported(compat, mode) \ 174 __xpcs_linkmode_supported(compat, ETHTOOL_LINK_MODE_ ## mode ## _BIT) 175 176 int xpcs_read(struct dw_xpcs *xpcs, int dev, u32 reg) 177 { 178 u32 reg_addr = mdiobus_c45_addr(dev, reg); 179 struct mii_bus *bus = xpcs->mdiodev->bus; 180 int addr = xpcs->mdiodev->addr; 181 182 return mdiobus_read(bus, addr, reg_addr); 183 } 184 185 int xpcs_write(struct dw_xpcs *xpcs, int dev, u32 reg, u16 val) 186 { 187 u32 reg_addr = mdiobus_c45_addr(dev, reg); 188 struct mii_bus *bus = xpcs->mdiodev->bus; 189 int addr = xpcs->mdiodev->addr; 190 191 return mdiobus_write(bus, addr, reg_addr, val); 192 } 193 194 static int xpcs_read_vendor(struct dw_xpcs *xpcs, int dev, u32 reg) 195 { 196 return xpcs_read(xpcs, dev, DW_VENDOR | reg); 197 } 198 199 static int xpcs_write_vendor(struct dw_xpcs *xpcs, int dev, int reg, 200 u16 val) 201 { 202 return xpcs_write(xpcs, dev, DW_VENDOR | reg, val); 203 } 204 205 static int xpcs_read_vpcs(struct dw_xpcs *xpcs, int reg) 206 { 207 return xpcs_read_vendor(xpcs, MDIO_MMD_PCS, reg); 208 } 209 210 static int xpcs_write_vpcs(struct dw_xpcs *xpcs, int reg, u16 val) 211 { 212 return xpcs_write_vendor(xpcs, MDIO_MMD_PCS, reg, val); 213 } 214 215 static int xpcs_poll_reset(struct dw_xpcs *xpcs, int dev) 216 { 217 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ 218 unsigned int retries = 12; 219 int ret; 220 221 do { 222 msleep(50); 223 ret = xpcs_read(xpcs, dev, MDIO_CTRL1); 224 if (ret < 0) 225 return ret; 226 } while (ret & MDIO_CTRL1_RESET && --retries); 227 228 return (ret & MDIO_CTRL1_RESET) ? -ETIMEDOUT : 0; 229 } 230 231 static int xpcs_soft_reset(struct dw_xpcs *xpcs, 232 const struct xpcs_compat *compat) 233 { 234 int ret, dev; 235 236 switch (compat->an_mode) { 237 case DW_AN_C73: 238 dev = MDIO_MMD_PCS; 239 break; 240 case DW_AN_C37_SGMII: 241 case DW_2500BASEX: 242 dev = MDIO_MMD_VEND2; 243 break; 244 default: 245 return -1; 246 } 247 248 ret = xpcs_write(xpcs, dev, MDIO_CTRL1, MDIO_CTRL1_RESET); 249 if (ret < 0) 250 return ret; 251 252 return xpcs_poll_reset(xpcs, dev); 253 } 254 255 #define xpcs_warn(__xpcs, __state, __args...) \ 256 ({ \ 257 if ((__state)->link) \ 258 dev_warn(&(__xpcs)->mdiodev->dev, ##__args); \ 259 }) 260 261 static int xpcs_read_fault_c73(struct dw_xpcs *xpcs, 262 struct phylink_link_state *state) 263 { 264 int ret; 265 266 ret = xpcs_read(xpcs, MDIO_MMD_PCS, MDIO_STAT1); 267 if (ret < 0) 268 return ret; 269 270 if (ret & MDIO_STAT1_FAULT) { 271 xpcs_warn(xpcs, state, "Link fault condition detected!\n"); 272 return -EFAULT; 273 } 274 275 ret = xpcs_read(xpcs, MDIO_MMD_PCS, MDIO_STAT2); 276 if (ret < 0) 277 return ret; 278 279 if (ret & MDIO_STAT2_RXFAULT) 280 xpcs_warn(xpcs, state, "Receiver fault detected!\n"); 281 if (ret & MDIO_STAT2_TXFAULT) 282 xpcs_warn(xpcs, state, "Transmitter fault detected!\n"); 283 284 ret = xpcs_read_vendor(xpcs, MDIO_MMD_PCS, DW_VR_XS_PCS_DIG_STS); 285 if (ret < 0) 286 return ret; 287 288 if (ret & DW_RXFIFO_ERR) { 289 xpcs_warn(xpcs, state, "FIFO fault condition detected!\n"); 290 return -EFAULT; 291 } 292 293 ret = xpcs_read(xpcs, MDIO_MMD_PCS, MDIO_PCS_10GBRT_STAT1); 294 if (ret < 0) 295 return ret; 296 297 if (!(ret & MDIO_PCS_10GBRT_STAT1_BLKLK)) 298 xpcs_warn(xpcs, state, "Link is not locked!\n"); 299 300 ret = xpcs_read(xpcs, MDIO_MMD_PCS, MDIO_PCS_10GBRT_STAT2); 301 if (ret < 0) 302 return ret; 303 304 if (ret & MDIO_PCS_10GBRT_STAT2_ERR) { 305 xpcs_warn(xpcs, state, "Link has errors!\n"); 306 return -EFAULT; 307 } 308 309 return 0; 310 } 311 312 static int xpcs_read_link_c73(struct dw_xpcs *xpcs, bool an) 313 { 314 bool link = true; 315 int ret; 316 317 ret = xpcs_read(xpcs, MDIO_MMD_PCS, MDIO_STAT1); 318 if (ret < 0) 319 return ret; 320 321 if (!(ret & MDIO_STAT1_LSTATUS)) 322 link = false; 323 324 if (an) { 325 ret = xpcs_read(xpcs, MDIO_MMD_AN, MDIO_STAT1); 326 if (ret < 0) 327 return ret; 328 329 if (!(ret & MDIO_STAT1_LSTATUS)) 330 link = false; 331 } 332 333 return link; 334 } 335 336 static int xpcs_get_max_usxgmii_speed(const unsigned long *supported) 337 { 338 int max = SPEED_UNKNOWN; 339 340 if (phylink_test(supported, 1000baseKX_Full)) 341 max = SPEED_1000; 342 if (phylink_test(supported, 2500baseX_Full)) 343 max = SPEED_2500; 344 if (phylink_test(supported, 10000baseKX4_Full)) 345 max = SPEED_10000; 346 if (phylink_test(supported, 10000baseKR_Full)) 347 max = SPEED_10000; 348 349 return max; 350 } 351 352 static void xpcs_config_usxgmii(struct dw_xpcs *xpcs, int speed) 353 { 354 int ret, speed_sel; 355 356 switch (speed) { 357 case SPEED_10: 358 speed_sel = DW_USXGMII_10; 359 break; 360 case SPEED_100: 361 speed_sel = DW_USXGMII_100; 362 break; 363 case SPEED_1000: 364 speed_sel = DW_USXGMII_1000; 365 break; 366 case SPEED_2500: 367 speed_sel = DW_USXGMII_2500; 368 break; 369 case SPEED_5000: 370 speed_sel = DW_USXGMII_5000; 371 break; 372 case SPEED_10000: 373 speed_sel = DW_USXGMII_10000; 374 break; 375 default: 376 /* Nothing to do here */ 377 return; 378 } 379 380 ret = xpcs_read_vpcs(xpcs, MDIO_CTRL1); 381 if (ret < 0) 382 goto out; 383 384 ret = xpcs_write_vpcs(xpcs, MDIO_CTRL1, ret | DW_USXGMII_EN); 385 if (ret < 0) 386 goto out; 387 388 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, MDIO_CTRL1); 389 if (ret < 0) 390 goto out; 391 392 ret &= ~DW_USXGMII_SS_MASK; 393 ret |= speed_sel | DW_USXGMII_FULL; 394 395 ret = xpcs_write(xpcs, MDIO_MMD_VEND2, MDIO_CTRL1, ret); 396 if (ret < 0) 397 goto out; 398 399 ret = xpcs_read_vpcs(xpcs, MDIO_CTRL1); 400 if (ret < 0) 401 goto out; 402 403 ret = xpcs_write_vpcs(xpcs, MDIO_CTRL1, ret | DW_USXGMII_RST); 404 if (ret < 0) 405 goto out; 406 407 return; 408 409 out: 410 pr_err("%s: XPCS access returned %pe\n", __func__, ERR_PTR(ret)); 411 } 412 413 static int _xpcs_config_aneg_c73(struct dw_xpcs *xpcs, 414 const struct xpcs_compat *compat) 415 { 416 int ret, adv; 417 418 /* By default, in USXGMII mode XPCS operates at 10G baud and 419 * replicates data to achieve lower speeds. Hereby, in this 420 * default configuration we need to advertise all supported 421 * modes and not only the ones we want to use. 422 */ 423 424 /* SR_AN_ADV3 */ 425 adv = 0; 426 if (xpcs_linkmode_supported(compat, 2500baseX_Full)) 427 adv |= DW_C73_2500KX; 428 429 /* TODO: 5000baseKR */ 430 431 ret = xpcs_write(xpcs, MDIO_MMD_AN, DW_SR_AN_ADV3, adv); 432 if (ret < 0) 433 return ret; 434 435 /* SR_AN_ADV2 */ 436 adv = 0; 437 if (xpcs_linkmode_supported(compat, 1000baseKX_Full)) 438 adv |= DW_C73_1000KX; 439 if (xpcs_linkmode_supported(compat, 10000baseKX4_Full)) 440 adv |= DW_C73_10000KX4; 441 if (xpcs_linkmode_supported(compat, 10000baseKR_Full)) 442 adv |= DW_C73_10000KR; 443 444 ret = xpcs_write(xpcs, MDIO_MMD_AN, DW_SR_AN_ADV2, adv); 445 if (ret < 0) 446 return ret; 447 448 /* SR_AN_ADV1 */ 449 adv = DW_C73_AN_ADV_SF; 450 if (xpcs_linkmode_supported(compat, Pause)) 451 adv |= DW_C73_PAUSE; 452 if (xpcs_linkmode_supported(compat, Asym_Pause)) 453 adv |= DW_C73_ASYM_PAUSE; 454 455 return xpcs_write(xpcs, MDIO_MMD_AN, DW_SR_AN_ADV1, adv); 456 } 457 458 static int xpcs_config_aneg_c73(struct dw_xpcs *xpcs, 459 const struct xpcs_compat *compat) 460 { 461 int ret; 462 463 ret = _xpcs_config_aneg_c73(xpcs, compat); 464 if (ret < 0) 465 return ret; 466 467 ret = xpcs_read(xpcs, MDIO_MMD_AN, MDIO_CTRL1); 468 if (ret < 0) 469 return ret; 470 471 ret |= MDIO_AN_CTRL1_ENABLE | MDIO_AN_CTRL1_RESTART; 472 473 return xpcs_write(xpcs, MDIO_MMD_AN, MDIO_CTRL1, ret); 474 } 475 476 static int xpcs_aneg_done_c73(struct dw_xpcs *xpcs, 477 struct phylink_link_state *state, 478 const struct xpcs_compat *compat) 479 { 480 int ret; 481 482 ret = xpcs_read(xpcs, MDIO_MMD_AN, MDIO_STAT1); 483 if (ret < 0) 484 return ret; 485 486 if (ret & MDIO_AN_STAT1_COMPLETE) { 487 ret = xpcs_read(xpcs, MDIO_MMD_AN, DW_SR_AN_LP_ABL1); 488 if (ret < 0) 489 return ret; 490 491 /* Check if Aneg outcome is valid */ 492 if (!(ret & DW_C73_AN_ADV_SF)) { 493 xpcs_config_aneg_c73(xpcs, compat); 494 return 0; 495 } 496 497 return 1; 498 } 499 500 return 0; 501 } 502 503 static int xpcs_read_lpa_c73(struct dw_xpcs *xpcs, 504 struct phylink_link_state *state) 505 { 506 int ret; 507 508 ret = xpcs_read(xpcs, MDIO_MMD_AN, MDIO_STAT1); 509 if (ret < 0) 510 return ret; 511 512 if (!(ret & MDIO_AN_STAT1_LPABLE)) { 513 phylink_clear(state->lp_advertising, Autoneg); 514 return 0; 515 } 516 517 phylink_set(state->lp_advertising, Autoneg); 518 519 /* Clause 73 outcome */ 520 ret = xpcs_read(xpcs, MDIO_MMD_AN, DW_SR_AN_LP_ABL3); 521 if (ret < 0) 522 return ret; 523 524 if (ret & DW_C73_2500KX) 525 phylink_set(state->lp_advertising, 2500baseX_Full); 526 527 ret = xpcs_read(xpcs, MDIO_MMD_AN, DW_SR_AN_LP_ABL2); 528 if (ret < 0) 529 return ret; 530 531 if (ret & DW_C73_1000KX) 532 phylink_set(state->lp_advertising, 1000baseKX_Full); 533 if (ret & DW_C73_10000KX4) 534 phylink_set(state->lp_advertising, 10000baseKX4_Full); 535 if (ret & DW_C73_10000KR) 536 phylink_set(state->lp_advertising, 10000baseKR_Full); 537 538 ret = xpcs_read(xpcs, MDIO_MMD_AN, DW_SR_AN_LP_ABL1); 539 if (ret < 0) 540 return ret; 541 542 if (ret & DW_C73_PAUSE) 543 phylink_set(state->lp_advertising, Pause); 544 if (ret & DW_C73_ASYM_PAUSE) 545 phylink_set(state->lp_advertising, Asym_Pause); 546 547 linkmode_and(state->lp_advertising, state->lp_advertising, 548 state->advertising); 549 return 0; 550 } 551 552 static void xpcs_resolve_lpa_c73(struct dw_xpcs *xpcs, 553 struct phylink_link_state *state) 554 { 555 int max_speed = xpcs_get_max_usxgmii_speed(state->lp_advertising); 556 557 state->pause = MLO_PAUSE_TX | MLO_PAUSE_RX; 558 state->speed = max_speed; 559 state->duplex = DUPLEX_FULL; 560 } 561 562 static int xpcs_get_max_xlgmii_speed(struct dw_xpcs *xpcs, 563 struct phylink_link_state *state) 564 { 565 unsigned long *adv = state->advertising; 566 int speed = SPEED_UNKNOWN; 567 int bit; 568 569 for_each_set_bit(bit, adv, __ETHTOOL_LINK_MODE_MASK_NBITS) { 570 int new_speed = SPEED_UNKNOWN; 571 572 switch (bit) { 573 case ETHTOOL_LINK_MODE_25000baseCR_Full_BIT: 574 case ETHTOOL_LINK_MODE_25000baseKR_Full_BIT: 575 case ETHTOOL_LINK_MODE_25000baseSR_Full_BIT: 576 new_speed = SPEED_25000; 577 break; 578 case ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT: 579 case ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT: 580 case ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT: 581 case ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT: 582 new_speed = SPEED_40000; 583 break; 584 case ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT: 585 case ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT: 586 case ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT: 587 case ETHTOOL_LINK_MODE_50000baseKR_Full_BIT: 588 case ETHTOOL_LINK_MODE_50000baseSR_Full_BIT: 589 case ETHTOOL_LINK_MODE_50000baseCR_Full_BIT: 590 case ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT: 591 case ETHTOOL_LINK_MODE_50000baseDR_Full_BIT: 592 new_speed = SPEED_50000; 593 break; 594 case ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT: 595 case ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT: 596 case ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT: 597 case ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT: 598 case ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT: 599 case ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT: 600 case ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT: 601 case ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT: 602 case ETHTOOL_LINK_MODE_100000baseDR2_Full_BIT: 603 new_speed = SPEED_100000; 604 break; 605 default: 606 continue; 607 } 608 609 if (new_speed > speed) 610 speed = new_speed; 611 } 612 613 return speed; 614 } 615 616 static void xpcs_resolve_pma(struct dw_xpcs *xpcs, 617 struct phylink_link_state *state) 618 { 619 state->pause = MLO_PAUSE_TX | MLO_PAUSE_RX; 620 state->duplex = DUPLEX_FULL; 621 622 switch (state->interface) { 623 case PHY_INTERFACE_MODE_10GKR: 624 state->speed = SPEED_10000; 625 break; 626 case PHY_INTERFACE_MODE_XLGMII: 627 state->speed = xpcs_get_max_xlgmii_speed(xpcs, state); 628 break; 629 default: 630 state->speed = SPEED_UNKNOWN; 631 break; 632 } 633 } 634 635 void xpcs_validate(struct dw_xpcs *xpcs, unsigned long *supported, 636 struct phylink_link_state *state) 637 { 638 __ETHTOOL_DECLARE_LINK_MODE_MASK(xpcs_supported); 639 const struct xpcs_compat *compat; 640 int i; 641 642 /* phylink expects us to report all supported modes with 643 * PHY_INTERFACE_MODE_NA, just don't limit the supported and 644 * advertising masks and exit. 645 */ 646 if (state->interface == PHY_INTERFACE_MODE_NA) 647 return; 648 649 bitmap_zero(xpcs_supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 650 651 compat = xpcs_find_compat(xpcs->id, state->interface); 652 653 /* Populate the supported link modes for this 654 * PHY interface type 655 */ 656 if (compat) 657 for (i = 0; compat->supported[i] != __ETHTOOL_LINK_MODE_MASK_NBITS; i++) 658 set_bit(compat->supported[i], xpcs_supported); 659 660 linkmode_and(supported, supported, xpcs_supported); 661 linkmode_and(state->advertising, state->advertising, xpcs_supported); 662 } 663 EXPORT_SYMBOL_GPL(xpcs_validate); 664 665 int xpcs_config_eee(struct dw_xpcs *xpcs, int mult_fact_100ns, int enable) 666 { 667 int ret; 668 669 if (enable) { 670 /* Enable EEE */ 671 ret = DW_VR_MII_EEE_LTX_EN | DW_VR_MII_EEE_LRX_EN | 672 DW_VR_MII_EEE_TX_QUIET_EN | DW_VR_MII_EEE_RX_QUIET_EN | 673 DW_VR_MII_EEE_TX_EN_CTRL | DW_VR_MII_EEE_RX_EN_CTRL | 674 mult_fact_100ns << DW_VR_MII_EEE_MULT_FACT_100NS_SHIFT; 675 } else { 676 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, DW_VR_MII_EEE_MCTRL0); 677 if (ret < 0) 678 return ret; 679 ret &= ~(DW_VR_MII_EEE_LTX_EN | DW_VR_MII_EEE_LRX_EN | 680 DW_VR_MII_EEE_TX_QUIET_EN | DW_VR_MII_EEE_RX_QUIET_EN | 681 DW_VR_MII_EEE_TX_EN_CTRL | DW_VR_MII_EEE_RX_EN_CTRL | 682 DW_VR_MII_EEE_MULT_FACT_100NS); 683 } 684 685 ret = xpcs_write(xpcs, MDIO_MMD_VEND2, DW_VR_MII_EEE_MCTRL0, ret); 686 if (ret < 0) 687 return ret; 688 689 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, DW_VR_MII_EEE_MCTRL1); 690 if (ret < 0) 691 return ret; 692 693 ret |= DW_VR_MII_EEE_TRN_LPI; 694 return xpcs_write(xpcs, MDIO_MMD_VEND2, DW_VR_MII_EEE_MCTRL1, ret); 695 } 696 EXPORT_SYMBOL_GPL(xpcs_config_eee); 697 698 static int xpcs_config_aneg_c37_sgmii(struct dw_xpcs *xpcs, unsigned int mode) 699 { 700 int ret; 701 702 /* For AN for C37 SGMII mode, the settings are :- 703 * 1) VR_MII_AN_CTRL Bit(2:1)[PCS_MODE] = 10b (SGMII AN) 704 * 2) VR_MII_AN_CTRL Bit(3) [TX_CONFIG] = 0b (MAC side SGMII) 705 * DW xPCS used with DW EQoS MAC is always MAC side SGMII. 706 * 3) VR_MII_DIG_CTRL1 Bit(9) [MAC_AUTO_SW] = 1b (Automatic 707 * speed/duplex mode change by HW after SGMII AN complete) 708 * 709 * Note: Since it is MAC side SGMII, there is no need to set 710 * SR_MII_AN_ADV. MAC side SGMII receives AN Tx Config from 711 * PHY about the link state change after C28 AN is completed 712 * between PHY and Link Partner. There is also no need to 713 * trigger AN restart for MAC-side SGMII. 714 */ 715 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, DW_VR_MII_AN_CTRL); 716 if (ret < 0) 717 return ret; 718 719 ret &= ~(DW_VR_MII_PCS_MODE_MASK | DW_VR_MII_TX_CONFIG_MASK); 720 ret |= (DW_VR_MII_PCS_MODE_C37_SGMII << 721 DW_VR_MII_AN_CTRL_PCS_MODE_SHIFT & 722 DW_VR_MII_PCS_MODE_MASK); 723 ret |= (DW_VR_MII_TX_CONFIG_MAC_SIDE_SGMII << 724 DW_VR_MII_AN_CTRL_TX_CONFIG_SHIFT & 725 DW_VR_MII_TX_CONFIG_MASK); 726 ret = xpcs_write(xpcs, MDIO_MMD_VEND2, DW_VR_MII_AN_CTRL, ret); 727 if (ret < 0) 728 return ret; 729 730 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, DW_VR_MII_DIG_CTRL1); 731 if (ret < 0) 732 return ret; 733 734 if (phylink_autoneg_inband(mode)) 735 ret |= DW_VR_MII_DIG_CTRL1_MAC_AUTO_SW; 736 else 737 ret &= ~DW_VR_MII_DIG_CTRL1_MAC_AUTO_SW; 738 739 return xpcs_write(xpcs, MDIO_MMD_VEND2, DW_VR_MII_DIG_CTRL1, ret); 740 } 741 742 static int xpcs_config_2500basex(struct dw_xpcs *xpcs) 743 { 744 int ret; 745 746 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, DW_VR_MII_DIG_CTRL1); 747 if (ret < 0) 748 return ret; 749 ret |= DW_VR_MII_DIG_CTRL1_2G5_EN; 750 ret &= ~DW_VR_MII_DIG_CTRL1_MAC_AUTO_SW; 751 ret = xpcs_write(xpcs, MDIO_MMD_VEND2, DW_VR_MII_DIG_CTRL1, ret); 752 if (ret < 0) 753 return ret; 754 755 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, DW_VR_MII_MMD_CTRL); 756 if (ret < 0) 757 return ret; 758 ret &= ~AN_CL37_EN; 759 ret |= SGMII_SPEED_SS6; 760 ret &= ~SGMII_SPEED_SS13; 761 return xpcs_write(xpcs, MDIO_MMD_VEND2, DW_VR_MII_MMD_CTRL, ret); 762 } 763 764 int xpcs_do_config(struct dw_xpcs *xpcs, phy_interface_t interface, 765 unsigned int mode) 766 { 767 const struct xpcs_compat *compat; 768 int ret; 769 770 compat = xpcs_find_compat(xpcs->id, interface); 771 if (!compat) 772 return -ENODEV; 773 774 switch (compat->an_mode) { 775 case DW_AN_C73: 776 if (phylink_autoneg_inband(mode)) { 777 ret = xpcs_config_aneg_c73(xpcs, compat); 778 if (ret) 779 return ret; 780 } 781 break; 782 case DW_AN_C37_SGMII: 783 ret = xpcs_config_aneg_c37_sgmii(xpcs, mode); 784 if (ret) 785 return ret; 786 break; 787 case DW_2500BASEX: 788 ret = xpcs_config_2500basex(xpcs); 789 if (ret) 790 return ret; 791 break; 792 default: 793 return -1; 794 } 795 796 if (compat->pma_config) { 797 ret = compat->pma_config(xpcs); 798 if (ret) 799 return ret; 800 } 801 802 return 0; 803 } 804 EXPORT_SYMBOL_GPL(xpcs_do_config); 805 806 static int xpcs_config(struct phylink_pcs *pcs, unsigned int mode, 807 phy_interface_t interface, 808 const unsigned long *advertising, 809 bool permit_pause_to_mac) 810 { 811 struct dw_xpcs *xpcs = phylink_pcs_to_xpcs(pcs); 812 813 return xpcs_do_config(xpcs, interface, mode); 814 } 815 816 static int xpcs_get_state_c73(struct dw_xpcs *xpcs, 817 struct phylink_link_state *state, 818 const struct xpcs_compat *compat) 819 { 820 int ret; 821 822 /* Link needs to be read first ... */ 823 state->link = xpcs_read_link_c73(xpcs, state->an_enabled) > 0 ? 1 : 0; 824 825 /* ... and then we check the faults. */ 826 ret = xpcs_read_fault_c73(xpcs, state); 827 if (ret) { 828 ret = xpcs_soft_reset(xpcs, compat); 829 if (ret) 830 return ret; 831 832 state->link = 0; 833 834 return xpcs_do_config(xpcs, state->interface, MLO_AN_INBAND); 835 } 836 837 if (state->an_enabled && xpcs_aneg_done_c73(xpcs, state, compat)) { 838 state->an_complete = true; 839 xpcs_read_lpa_c73(xpcs, state); 840 xpcs_resolve_lpa_c73(xpcs, state); 841 } else if (state->an_enabled) { 842 state->link = 0; 843 } else if (state->link) { 844 xpcs_resolve_pma(xpcs, state); 845 } 846 847 return 0; 848 } 849 850 static int xpcs_get_state_c37_sgmii(struct dw_xpcs *xpcs, 851 struct phylink_link_state *state) 852 { 853 int ret; 854 855 /* Reset link_state */ 856 state->link = false; 857 state->speed = SPEED_UNKNOWN; 858 state->duplex = DUPLEX_UNKNOWN; 859 state->pause = 0; 860 861 /* For C37 SGMII mode, we check DW_VR_MII_AN_INTR_STS for link 862 * status, speed and duplex. 863 */ 864 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, DW_VR_MII_AN_INTR_STS); 865 if (ret < 0) 866 return false; 867 868 if (ret & DW_VR_MII_C37_ANSGM_SP_LNKSTS) { 869 int speed_value; 870 871 state->link = true; 872 873 speed_value = (ret & DW_VR_MII_AN_STS_C37_ANSGM_SP) >> 874 DW_VR_MII_AN_STS_C37_ANSGM_SP_SHIFT; 875 if (speed_value == DW_VR_MII_C37_ANSGM_SP_1000) 876 state->speed = SPEED_1000; 877 else if (speed_value == DW_VR_MII_C37_ANSGM_SP_100) 878 state->speed = SPEED_100; 879 else 880 state->speed = SPEED_10; 881 882 if (ret & DW_VR_MII_AN_STS_C37_ANSGM_FD) 883 state->duplex = DUPLEX_FULL; 884 else 885 state->duplex = DUPLEX_HALF; 886 } 887 888 return 0; 889 } 890 891 static void xpcs_get_state(struct phylink_pcs *pcs, 892 struct phylink_link_state *state) 893 { 894 struct dw_xpcs *xpcs = phylink_pcs_to_xpcs(pcs); 895 const struct xpcs_compat *compat; 896 int ret; 897 898 compat = xpcs_find_compat(xpcs->id, state->interface); 899 if (!compat) 900 return; 901 902 switch (compat->an_mode) { 903 case DW_AN_C73: 904 ret = xpcs_get_state_c73(xpcs, state, compat); 905 if (ret) { 906 pr_err("xpcs_get_state_c73 returned %pe\n", 907 ERR_PTR(ret)); 908 return; 909 } 910 break; 911 case DW_AN_C37_SGMII: 912 ret = xpcs_get_state_c37_sgmii(xpcs, state); 913 if (ret) { 914 pr_err("xpcs_get_state_c37_sgmii returned %pe\n", 915 ERR_PTR(ret)); 916 } 917 break; 918 default: 919 return; 920 } 921 } 922 923 static void xpcs_link_up_sgmii(struct dw_xpcs *xpcs, unsigned int mode, 924 int speed, int duplex) 925 { 926 int val, ret; 927 928 if (phylink_autoneg_inband(mode)) 929 return; 930 931 switch (speed) { 932 case SPEED_1000: 933 val = BMCR_SPEED1000; 934 break; 935 case SPEED_100: 936 val = BMCR_SPEED100; 937 break; 938 case SPEED_10: 939 val = BMCR_SPEED10; 940 break; 941 default: 942 return; 943 } 944 945 if (duplex == DUPLEX_FULL) 946 val |= BMCR_FULLDPLX; 947 948 ret = xpcs_write(xpcs, MDIO_MMD_VEND2, MDIO_CTRL1, val); 949 if (ret) 950 pr_err("%s: xpcs_write returned %pe\n", __func__, ERR_PTR(ret)); 951 } 952 953 void xpcs_link_up(struct phylink_pcs *pcs, unsigned int mode, 954 phy_interface_t interface, int speed, int duplex) 955 { 956 struct dw_xpcs *xpcs = phylink_pcs_to_xpcs(pcs); 957 958 if (interface == PHY_INTERFACE_MODE_USXGMII) 959 return xpcs_config_usxgmii(xpcs, speed); 960 if (interface == PHY_INTERFACE_MODE_SGMII) 961 return xpcs_link_up_sgmii(xpcs, mode, speed, duplex); 962 } 963 EXPORT_SYMBOL_GPL(xpcs_link_up); 964 965 static u32 xpcs_get_id(struct dw_xpcs *xpcs) 966 { 967 int ret; 968 u32 id; 969 970 /* First, search C73 PCS using PCS MMD */ 971 ret = xpcs_read(xpcs, MDIO_MMD_PCS, MII_PHYSID1); 972 if (ret < 0) 973 return 0xffffffff; 974 975 id = ret << 16; 976 977 ret = xpcs_read(xpcs, MDIO_MMD_PCS, MII_PHYSID2); 978 if (ret < 0) 979 return 0xffffffff; 980 981 /* If Device IDs are not all zeros or all ones, 982 * we found C73 AN-type device 983 */ 984 if ((id | ret) && (id | ret) != 0xffffffff) 985 return id | ret; 986 987 /* Next, search C37 PCS using Vendor-Specific MII MMD */ 988 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, MII_PHYSID1); 989 if (ret < 0) 990 return 0xffffffff; 991 992 id = ret << 16; 993 994 ret = xpcs_read(xpcs, MDIO_MMD_VEND2, MII_PHYSID2); 995 if (ret < 0) 996 return 0xffffffff; 997 998 /* If Device IDs are not all zeros, we found C37 AN-type device */ 999 if (id | ret) 1000 return id | ret; 1001 1002 return 0xffffffff; 1003 } 1004 1005 static const struct xpcs_compat synopsys_xpcs_compat[DW_XPCS_INTERFACE_MAX] = { 1006 [DW_XPCS_USXGMII] = { 1007 .supported = xpcs_usxgmii_features, 1008 .interface = xpcs_usxgmii_interfaces, 1009 .num_interfaces = ARRAY_SIZE(xpcs_usxgmii_interfaces), 1010 .an_mode = DW_AN_C73, 1011 }, 1012 [DW_XPCS_10GKR] = { 1013 .supported = xpcs_10gkr_features, 1014 .interface = xpcs_10gkr_interfaces, 1015 .num_interfaces = ARRAY_SIZE(xpcs_10gkr_interfaces), 1016 .an_mode = DW_AN_C73, 1017 }, 1018 [DW_XPCS_XLGMII] = { 1019 .supported = xpcs_xlgmii_features, 1020 .interface = xpcs_xlgmii_interfaces, 1021 .num_interfaces = ARRAY_SIZE(xpcs_xlgmii_interfaces), 1022 .an_mode = DW_AN_C73, 1023 }, 1024 [DW_XPCS_SGMII] = { 1025 .supported = xpcs_sgmii_features, 1026 .interface = xpcs_sgmii_interfaces, 1027 .num_interfaces = ARRAY_SIZE(xpcs_sgmii_interfaces), 1028 .an_mode = DW_AN_C37_SGMII, 1029 }, 1030 [DW_XPCS_2500BASEX] = { 1031 .supported = xpcs_2500basex_features, 1032 .interface = xpcs_2500basex_interfaces, 1033 .num_interfaces = ARRAY_SIZE(xpcs_2500basex_features), 1034 .an_mode = DW_2500BASEX, 1035 }, 1036 }; 1037 1038 static const struct xpcs_compat nxp_sja1105_xpcs_compat[DW_XPCS_INTERFACE_MAX] = { 1039 [DW_XPCS_SGMII] = { 1040 .supported = xpcs_sgmii_features, 1041 .interface = xpcs_sgmii_interfaces, 1042 .num_interfaces = ARRAY_SIZE(xpcs_sgmii_interfaces), 1043 .an_mode = DW_AN_C37_SGMII, 1044 .pma_config = nxp_sja1105_sgmii_pma_config, 1045 }, 1046 }; 1047 1048 static const struct xpcs_compat nxp_sja1110_xpcs_compat[DW_XPCS_INTERFACE_MAX] = { 1049 [DW_XPCS_SGMII] = { 1050 .supported = xpcs_sgmii_features, 1051 .interface = xpcs_sgmii_interfaces, 1052 .num_interfaces = ARRAY_SIZE(xpcs_sgmii_interfaces), 1053 .an_mode = DW_AN_C37_SGMII, 1054 .pma_config = nxp_sja1110_sgmii_pma_config, 1055 }, 1056 [DW_XPCS_2500BASEX] = { 1057 .supported = xpcs_2500basex_features, 1058 .interface = xpcs_2500basex_interfaces, 1059 .num_interfaces = ARRAY_SIZE(xpcs_2500basex_interfaces), 1060 .an_mode = DW_2500BASEX, 1061 .pma_config = nxp_sja1110_2500basex_pma_config, 1062 }, 1063 }; 1064 1065 static const struct xpcs_id xpcs_id_list[] = { 1066 { 1067 .id = SYNOPSYS_XPCS_ID, 1068 .mask = SYNOPSYS_XPCS_MASK, 1069 .compat = synopsys_xpcs_compat, 1070 }, { 1071 .id = NXP_SJA1105_XPCS_ID, 1072 .mask = SYNOPSYS_XPCS_MASK, 1073 .compat = nxp_sja1105_xpcs_compat, 1074 }, { 1075 .id = NXP_SJA1110_XPCS_ID, 1076 .mask = SYNOPSYS_XPCS_MASK, 1077 .compat = nxp_sja1110_xpcs_compat, 1078 }, 1079 }; 1080 1081 static const struct phylink_pcs_ops xpcs_phylink_ops = { 1082 .pcs_config = xpcs_config, 1083 .pcs_get_state = xpcs_get_state, 1084 .pcs_link_up = xpcs_link_up, 1085 }; 1086 1087 struct dw_xpcs *xpcs_create(struct mdio_device *mdiodev, 1088 phy_interface_t interface) 1089 { 1090 struct dw_xpcs *xpcs; 1091 u32 xpcs_id; 1092 int i, ret; 1093 1094 xpcs = kzalloc(sizeof(*xpcs), GFP_KERNEL); 1095 if (!xpcs) 1096 return ERR_PTR(-ENOMEM); 1097 1098 xpcs->mdiodev = mdiodev; 1099 1100 xpcs_id = xpcs_get_id(xpcs); 1101 1102 for (i = 0; i < ARRAY_SIZE(xpcs_id_list); i++) { 1103 const struct xpcs_id *entry = &xpcs_id_list[i]; 1104 const struct xpcs_compat *compat; 1105 1106 if ((xpcs_id & entry->mask) != entry->id) 1107 continue; 1108 1109 xpcs->id = entry; 1110 1111 compat = xpcs_find_compat(entry, interface); 1112 if (!compat) { 1113 ret = -ENODEV; 1114 goto out; 1115 } 1116 1117 xpcs->pcs.ops = &xpcs_phylink_ops; 1118 xpcs->pcs.poll = true; 1119 1120 ret = xpcs_soft_reset(xpcs, compat); 1121 if (ret) 1122 goto out; 1123 1124 return xpcs; 1125 } 1126 1127 ret = -ENODEV; 1128 1129 out: 1130 kfree(xpcs); 1131 1132 return ERR_PTR(ret); 1133 } 1134 EXPORT_SYMBOL_GPL(xpcs_create); 1135 1136 void xpcs_destroy(struct dw_xpcs *xpcs) 1137 { 1138 kfree(xpcs); 1139 } 1140 EXPORT_SYMBOL_GPL(xpcs_destroy); 1141 1142 MODULE_LICENSE("GPL v2"); 1143