1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * drivers/net/phy/smsc.c 4 * 5 * Driver for SMSC PHYs 6 * 7 * Author: Herbert Valerio Riedel 8 * 9 * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org> 10 * 11 * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net 12 * 13 */ 14 15 #include <linux/clk.h> 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/mii.h> 19 #include <linux/ethtool.h> 20 #include <linux/of.h> 21 #include <linux/phy.h> 22 #include <linux/netdevice.h> 23 #include <linux/smscphy.h> 24 25 /* Vendor-specific PHY Definitions */ 26 /* EDPD NLP / crossover time configuration */ 27 #define PHY_EDPD_CONFIG 16 28 #define PHY_EDPD_CONFIG_EXT_CROSSOVER_ 0x0001 29 30 /* Control/Status Indication Register */ 31 #define SPECIAL_CTRL_STS 27 32 #define SPECIAL_CTRL_STS_OVRRD_AMDIX_ 0x8000 33 #define SPECIAL_CTRL_STS_AMDIX_ENABLE_ 0x4000 34 #define SPECIAL_CTRL_STS_AMDIX_STATE_ 0x2000 35 36 #define EDPD_MAX_WAIT_DFLT_MS 640 37 /* interval between phylib state machine runs in ms */ 38 #define PHY_STATE_MACH_MS 1000 39 40 struct smsc_hw_stat { 41 const char *string; 42 u8 reg; 43 u8 bits; 44 }; 45 46 static struct smsc_hw_stat smsc_hw_stats[] = { 47 { "phy_symbol_errors", 26, 16}, 48 }; 49 50 struct smsc_phy_priv { 51 unsigned int edpd_enable:1; 52 unsigned int edpd_mode_set_by_user:1; 53 unsigned int edpd_max_wait_ms; 54 }; 55 56 static int smsc_phy_ack_interrupt(struct phy_device *phydev) 57 { 58 int rc = phy_read(phydev, MII_LAN83C185_ISF); 59 60 return rc < 0 ? rc : 0; 61 } 62 63 int smsc_phy_config_intr(struct phy_device *phydev) 64 { 65 int rc; 66 67 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 68 rc = smsc_phy_ack_interrupt(phydev); 69 if (rc) 70 return rc; 71 72 rc = phy_write(phydev, MII_LAN83C185_IM, 73 MII_LAN83C185_ISF_INT_PHYLIB_EVENTS); 74 } else { 75 rc = phy_write(phydev, MII_LAN83C185_IM, 0); 76 if (rc) 77 return rc; 78 79 rc = smsc_phy_ack_interrupt(phydev); 80 } 81 82 return rc < 0 ? rc : 0; 83 } 84 EXPORT_SYMBOL_GPL(smsc_phy_config_intr); 85 86 static int smsc_phy_config_edpd(struct phy_device *phydev) 87 { 88 struct smsc_phy_priv *priv = phydev->priv; 89 90 if (priv->edpd_enable) 91 return phy_set_bits(phydev, MII_LAN83C185_CTRL_STATUS, 92 MII_LAN83C185_EDPWRDOWN); 93 else 94 return phy_clear_bits(phydev, MII_LAN83C185_CTRL_STATUS, 95 MII_LAN83C185_EDPWRDOWN); 96 } 97 98 irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev) 99 { 100 int irq_status; 101 102 irq_status = phy_read(phydev, MII_LAN83C185_ISF); 103 if (irq_status < 0) { 104 if (irq_status != -ENODEV) 105 phy_error(phydev); 106 107 return IRQ_NONE; 108 } 109 110 if (!(irq_status & MII_LAN83C185_ISF_INT_PHYLIB_EVENTS)) 111 return IRQ_NONE; 112 113 phy_trigger_machine(phydev); 114 115 return IRQ_HANDLED; 116 } 117 EXPORT_SYMBOL_GPL(smsc_phy_handle_interrupt); 118 119 int smsc_phy_config_init(struct phy_device *phydev) 120 { 121 struct smsc_phy_priv *priv = phydev->priv; 122 123 if (!priv) 124 return 0; 125 126 /* don't use EDPD in irq mode except overridden by user */ 127 if (!priv->edpd_mode_set_by_user && phydev->irq != PHY_POLL) 128 priv->edpd_enable = false; 129 130 return smsc_phy_config_edpd(phydev); 131 } 132 EXPORT_SYMBOL_GPL(smsc_phy_config_init); 133 134 static int smsc_phy_reset(struct phy_device *phydev) 135 { 136 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES); 137 if (rc < 0) 138 return rc; 139 140 /* If the SMSC PHY is in power down mode, then set it 141 * in all capable mode before using it. 142 */ 143 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) { 144 /* set "all capable" mode */ 145 rc |= MII_LAN83C185_MODE_ALL; 146 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc); 147 } 148 149 /* reset the phy */ 150 return genphy_soft_reset(phydev); 151 } 152 153 static int lan87xx_config_aneg(struct phy_device *phydev) 154 { 155 int rc; 156 int val; 157 158 switch (phydev->mdix_ctrl) { 159 case ETH_TP_MDI: 160 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_; 161 break; 162 case ETH_TP_MDI_X: 163 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ | 164 SPECIAL_CTRL_STS_AMDIX_STATE_; 165 break; 166 case ETH_TP_MDI_AUTO: 167 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_; 168 break; 169 default: 170 return genphy_config_aneg(phydev); 171 } 172 173 rc = phy_read(phydev, SPECIAL_CTRL_STS); 174 if (rc < 0) 175 return rc; 176 177 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ | 178 SPECIAL_CTRL_STS_AMDIX_ENABLE_ | 179 SPECIAL_CTRL_STS_AMDIX_STATE_); 180 rc |= val; 181 phy_write(phydev, SPECIAL_CTRL_STS, rc); 182 183 phydev->mdix = phydev->mdix_ctrl; 184 return genphy_config_aneg(phydev); 185 } 186 187 static int lan95xx_config_aneg_ext(struct phy_device *phydev) 188 { 189 if (phydev->phy_id == 0x0007c0f0) { /* LAN9500A or LAN9505A */ 190 /* Extend Manual AutoMDIX timer */ 191 int rc = phy_set_bits(phydev, PHY_EDPD_CONFIG, 192 PHY_EDPD_CONFIG_EXT_CROSSOVER_); 193 194 if (rc < 0) 195 return rc; 196 } 197 198 return lan87xx_config_aneg(phydev); 199 } 200 201 /* 202 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable 203 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to 204 * unstable detection of plugging in Ethernet cable. 205 * This workaround disables Energy Detect Power-Down mode and waiting for 206 * response on link pulses to detect presence of plugged Ethernet cable. 207 * The Energy Detect Power-Down mode is enabled again in the end of procedure to 208 * save approximately 220 mW of power if cable is unplugged. 209 * The workaround is only applicable to poll mode. Energy Detect Power-Down may 210 * not be used in interrupt mode lest link change detection becomes unreliable. 211 */ 212 int lan87xx_read_status(struct phy_device *phydev) 213 { 214 struct smsc_phy_priv *priv = phydev->priv; 215 int err; 216 217 err = genphy_read_status(phydev); 218 if (err) 219 return err; 220 221 if (!phydev->link && priv && priv->edpd_enable && 222 priv->edpd_max_wait_ms) { 223 unsigned int max_wait = priv->edpd_max_wait_ms * 1000; 224 int rc; 225 226 /* Disable EDPD to wake up PHY */ 227 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); 228 if (rc < 0) 229 return rc; 230 231 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS, 232 rc & ~MII_LAN83C185_EDPWRDOWN); 233 if (rc < 0) 234 return rc; 235 236 /* Wait max 640 ms to detect energy and the timeout is not 237 * an actual error. 238 */ 239 read_poll_timeout(phy_read, rc, 240 rc & MII_LAN83C185_ENERGYON || rc < 0, 241 10000, max_wait, true, phydev, 242 MII_LAN83C185_CTRL_STATUS); 243 if (rc < 0) 244 return rc; 245 246 /* Re-enable EDPD */ 247 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); 248 if (rc < 0) 249 return rc; 250 251 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS, 252 rc | MII_LAN83C185_EDPWRDOWN); 253 if (rc < 0) 254 return rc; 255 } 256 257 return err; 258 } 259 EXPORT_SYMBOL_GPL(lan87xx_read_status); 260 261 static int smsc_get_sset_count(struct phy_device *phydev) 262 { 263 return ARRAY_SIZE(smsc_hw_stats); 264 } 265 266 static void smsc_get_strings(struct phy_device *phydev, u8 *data) 267 { 268 int i; 269 270 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) { 271 strncpy(data + i * ETH_GSTRING_LEN, 272 smsc_hw_stats[i].string, ETH_GSTRING_LEN); 273 } 274 } 275 276 static u64 smsc_get_stat(struct phy_device *phydev, int i) 277 { 278 struct smsc_hw_stat stat = smsc_hw_stats[i]; 279 int val; 280 u64 ret; 281 282 val = phy_read(phydev, stat.reg); 283 if (val < 0) 284 ret = U64_MAX; 285 else 286 ret = val; 287 288 return ret; 289 } 290 291 static void smsc_get_stats(struct phy_device *phydev, 292 struct ethtool_stats *stats, u64 *data) 293 { 294 int i; 295 296 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) 297 data[i] = smsc_get_stat(phydev, i); 298 } 299 300 static int smsc_phy_get_edpd(struct phy_device *phydev, u16 *edpd) 301 { 302 struct smsc_phy_priv *priv = phydev->priv; 303 304 if (!priv) 305 return -EOPNOTSUPP; 306 307 if (!priv->edpd_enable) 308 *edpd = ETHTOOL_PHY_EDPD_DISABLE; 309 else if (!priv->edpd_max_wait_ms) 310 *edpd = ETHTOOL_PHY_EDPD_NO_TX; 311 else 312 *edpd = PHY_STATE_MACH_MS + priv->edpd_max_wait_ms; 313 314 return 0; 315 } 316 317 static int smsc_phy_set_edpd(struct phy_device *phydev, u16 edpd) 318 { 319 struct smsc_phy_priv *priv = phydev->priv; 320 321 if (!priv) 322 return -EOPNOTSUPP; 323 324 switch (edpd) { 325 case ETHTOOL_PHY_EDPD_DISABLE: 326 priv->edpd_enable = false; 327 break; 328 case ETHTOOL_PHY_EDPD_NO_TX: 329 priv->edpd_enable = true; 330 priv->edpd_max_wait_ms = 0; 331 break; 332 case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS: 333 edpd = PHY_STATE_MACH_MS + EDPD_MAX_WAIT_DFLT_MS; 334 fallthrough; 335 default: 336 if (phydev->irq != PHY_POLL) 337 return -EOPNOTSUPP; 338 if (edpd < PHY_STATE_MACH_MS || edpd > PHY_STATE_MACH_MS + 1000) 339 return -EINVAL; 340 priv->edpd_enable = true; 341 priv->edpd_max_wait_ms = edpd - PHY_STATE_MACH_MS; 342 } 343 344 priv->edpd_mode_set_by_user = true; 345 346 return smsc_phy_config_edpd(phydev); 347 } 348 349 int smsc_phy_get_tunable(struct phy_device *phydev, 350 struct ethtool_tunable *tuna, void *data) 351 { 352 switch (tuna->id) { 353 case ETHTOOL_PHY_EDPD: 354 return smsc_phy_get_edpd(phydev, data); 355 default: 356 return -EOPNOTSUPP; 357 } 358 } 359 EXPORT_SYMBOL_GPL(smsc_phy_get_tunable); 360 361 int smsc_phy_set_tunable(struct phy_device *phydev, 362 struct ethtool_tunable *tuna, const void *data) 363 { 364 switch (tuna->id) { 365 case ETHTOOL_PHY_EDPD: 366 return smsc_phy_set_edpd(phydev, *(u16 *)data); 367 default: 368 return -EOPNOTSUPP; 369 } 370 } 371 EXPORT_SYMBOL_GPL(smsc_phy_set_tunable); 372 373 int smsc_phy_probe(struct phy_device *phydev) 374 { 375 struct device *dev = &phydev->mdio.dev; 376 struct smsc_phy_priv *priv; 377 struct clk *refclk; 378 379 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 380 if (!priv) 381 return -ENOMEM; 382 383 priv->edpd_enable = true; 384 priv->edpd_max_wait_ms = EDPD_MAX_WAIT_DFLT_MS; 385 386 if (device_property_present(dev, "smsc,disable-energy-detect")) 387 priv->edpd_enable = false; 388 389 phydev->priv = priv; 390 391 /* Make clk optional to keep DTB backward compatibility. */ 392 refclk = devm_clk_get_optional_enabled(dev, NULL); 393 if (IS_ERR(refclk)) 394 return dev_err_probe(dev, PTR_ERR(refclk), 395 "Failed to request clock\n"); 396 397 return clk_set_rate(refclk, 50 * 1000 * 1000); 398 } 399 EXPORT_SYMBOL_GPL(smsc_phy_probe); 400 401 static struct phy_driver smsc_phy_driver[] = { 402 { 403 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */ 404 .phy_id_mask = 0xfffffff0, 405 .name = "SMSC LAN83C185", 406 407 /* PHY_BASIC_FEATURES */ 408 409 .probe = smsc_phy_probe, 410 411 /* basic functions */ 412 .config_init = smsc_phy_config_init, 413 .soft_reset = smsc_phy_reset, 414 415 /* IRQ related */ 416 .config_intr = smsc_phy_config_intr, 417 .handle_interrupt = smsc_phy_handle_interrupt, 418 419 .suspend = genphy_suspend, 420 .resume = genphy_resume, 421 }, { 422 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */ 423 .phy_id_mask = 0xfffffff0, 424 .name = "SMSC LAN8187", 425 426 /* PHY_BASIC_FEATURES */ 427 428 .probe = smsc_phy_probe, 429 430 /* basic functions */ 431 .config_init = smsc_phy_config_init, 432 .soft_reset = smsc_phy_reset, 433 434 /* IRQ related */ 435 .config_intr = smsc_phy_config_intr, 436 .handle_interrupt = smsc_phy_handle_interrupt, 437 438 /* Statistics */ 439 .get_sset_count = smsc_get_sset_count, 440 .get_strings = smsc_get_strings, 441 .get_stats = smsc_get_stats, 442 443 .suspend = genphy_suspend, 444 .resume = genphy_resume, 445 }, { 446 /* This covers internal PHY (phy_id: 0x0007C0C3) for 447 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505) 448 */ 449 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */ 450 .phy_id_mask = 0xfffffff0, 451 .name = "SMSC LAN8700", 452 453 /* PHY_BASIC_FEATURES */ 454 455 .probe = smsc_phy_probe, 456 457 /* basic functions */ 458 .read_status = lan87xx_read_status, 459 .config_init = smsc_phy_config_init, 460 .soft_reset = smsc_phy_reset, 461 .config_aneg = lan87xx_config_aneg, 462 463 /* IRQ related */ 464 .config_intr = smsc_phy_config_intr, 465 .handle_interrupt = smsc_phy_handle_interrupt, 466 467 /* Statistics */ 468 .get_sset_count = smsc_get_sset_count, 469 .get_strings = smsc_get_strings, 470 .get_stats = smsc_get_stats, 471 472 .get_tunable = smsc_phy_get_tunable, 473 .set_tunable = smsc_phy_set_tunable, 474 475 .suspend = genphy_suspend, 476 .resume = genphy_resume, 477 }, { 478 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */ 479 .phy_id_mask = 0xfffffff0, 480 .name = "SMSC LAN911x Internal PHY", 481 482 /* PHY_BASIC_FEATURES */ 483 484 .probe = smsc_phy_probe, 485 486 /* IRQ related */ 487 .config_intr = smsc_phy_config_intr, 488 .handle_interrupt = smsc_phy_handle_interrupt, 489 490 .suspend = genphy_suspend, 491 .resume = genphy_resume, 492 }, { 493 /* This covers internal PHY (phy_id: 0x0007C0F0) for 494 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01) 495 */ 496 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */ 497 .phy_id_mask = 0xfffffff0, 498 .name = "SMSC LAN8710/LAN8720", 499 500 /* PHY_BASIC_FEATURES */ 501 502 .probe = smsc_phy_probe, 503 504 /* basic functions */ 505 .read_status = lan87xx_read_status, 506 .config_init = smsc_phy_config_init, 507 .soft_reset = smsc_phy_reset, 508 .config_aneg = lan95xx_config_aneg_ext, 509 510 /* IRQ related */ 511 .config_intr = smsc_phy_config_intr, 512 .handle_interrupt = smsc_phy_handle_interrupt, 513 514 /* Statistics */ 515 .get_sset_count = smsc_get_sset_count, 516 .get_strings = smsc_get_strings, 517 .get_stats = smsc_get_stats, 518 519 .get_tunable = smsc_phy_get_tunable, 520 .set_tunable = smsc_phy_set_tunable, 521 522 .suspend = genphy_suspend, 523 .resume = genphy_resume, 524 }, { 525 .phy_id = 0x0007c110, 526 .phy_id_mask = 0xfffffff0, 527 .name = "SMSC LAN8740", 528 529 /* PHY_BASIC_FEATURES */ 530 .flags = PHY_RST_AFTER_CLK_EN, 531 532 .probe = smsc_phy_probe, 533 534 /* basic functions */ 535 .read_status = lan87xx_read_status, 536 .config_init = smsc_phy_config_init, 537 .soft_reset = smsc_phy_reset, 538 539 /* IRQ related */ 540 .config_intr = smsc_phy_config_intr, 541 .handle_interrupt = smsc_phy_handle_interrupt, 542 543 /* Statistics */ 544 .get_sset_count = smsc_get_sset_count, 545 .get_strings = smsc_get_strings, 546 .get_stats = smsc_get_stats, 547 548 .get_tunable = smsc_phy_get_tunable, 549 .set_tunable = smsc_phy_set_tunable, 550 551 .suspend = genphy_suspend, 552 .resume = genphy_resume, 553 }, { 554 .phy_id = 0x0007c130, /* 0x0007c130 and 0x0007c131 */ 555 /* This mask (0xfffffff2) is to differentiate from 556 * LAN88xx (phy_id 0x0007c132) 557 * and allows future phy_id revisions. 558 */ 559 .phy_id_mask = 0xfffffff2, 560 .name = "Microchip LAN8742", 561 562 /* PHY_BASIC_FEATURES */ 563 .flags = PHY_RST_AFTER_CLK_EN, 564 565 .probe = smsc_phy_probe, 566 567 /* basic functions */ 568 .read_status = lan87xx_read_status, 569 .config_init = smsc_phy_config_init, 570 .soft_reset = smsc_phy_reset, 571 572 /* IRQ related */ 573 .config_intr = smsc_phy_config_intr, 574 .handle_interrupt = smsc_phy_handle_interrupt, 575 576 /* Statistics */ 577 .get_sset_count = smsc_get_sset_count, 578 .get_strings = smsc_get_strings, 579 .get_stats = smsc_get_stats, 580 581 .get_tunable = smsc_phy_get_tunable, 582 .set_tunable = smsc_phy_set_tunable, 583 584 .suspend = genphy_suspend, 585 .resume = genphy_resume, 586 } }; 587 588 module_phy_driver(smsc_phy_driver); 589 590 MODULE_DESCRIPTION("SMSC PHY driver"); 591 MODULE_AUTHOR("Herbert Valerio Riedel"); 592 MODULE_LICENSE("GPL"); 593 594 static struct mdio_device_id __maybe_unused smsc_tbl[] = { 595 { 0x0007c0a0, 0xfffffff0 }, 596 { 0x0007c0b0, 0xfffffff0 }, 597 { 0x0007c0c0, 0xfffffff0 }, 598 { 0x0007c0d0, 0xfffffff0 }, 599 { 0x0007c0f0, 0xfffffff0 }, 600 { 0x0007c110, 0xfffffff0 }, 601 { 0x0007c130, 0xfffffff2 }, 602 { } 603 }; 604 605 MODULE_DEVICE_TABLE(mdio, smsc_tbl); 606