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/kernel.h> 16 #include <linux/module.h> 17 #include <linux/mii.h> 18 #include <linux/ethtool.h> 19 #include <linux/of.h> 20 #include <linux/phy.h> 21 #include <linux/netdevice.h> 22 #include <linux/smscphy.h> 23 24 /* Vendor-specific PHY Definitions */ 25 /* EDPD NLP / crossover time configuration */ 26 #define PHY_EDPD_CONFIG 16 27 #define PHY_EDPD_CONFIG_EXT_CROSSOVER_ 0x0001 28 29 /* Control/Status Indication Register */ 30 #define SPECIAL_CTRL_STS 27 31 #define SPECIAL_CTRL_STS_OVRRD_AMDIX_ 0x8000 32 #define SPECIAL_CTRL_STS_AMDIX_ENABLE_ 0x4000 33 #define SPECIAL_CTRL_STS_AMDIX_STATE_ 0x2000 34 35 struct smsc_hw_stat { 36 const char *string; 37 u8 reg; 38 u8 bits; 39 }; 40 41 static struct smsc_hw_stat smsc_hw_stats[] = { 42 { "phy_symbol_errors", 26, 16}, 43 }; 44 45 struct smsc_phy_priv { 46 bool energy_enable; 47 }; 48 49 static int smsc_phy_config_intr(struct phy_device *phydev) 50 { 51 int rc = phy_write (phydev, MII_LAN83C185_IM, 52 ((PHY_INTERRUPT_ENABLED == phydev->interrupts) 53 ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS 54 : 0)); 55 56 return rc < 0 ? rc : 0; 57 } 58 59 static int smsc_phy_ack_interrupt(struct phy_device *phydev) 60 { 61 int rc = phy_read (phydev, MII_LAN83C185_ISF); 62 63 return rc < 0 ? rc : 0; 64 } 65 66 static int smsc_phy_config_init(struct phy_device *phydev) 67 { 68 struct smsc_phy_priv *priv = phydev->priv; 69 70 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); 71 72 if (rc < 0) 73 return rc; 74 75 if (priv->energy_enable) { 76 /* Enable energy detect mode for this SMSC Transceivers */ 77 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS, 78 rc | MII_LAN83C185_EDPWRDOWN); 79 if (rc < 0) 80 return rc; 81 } 82 83 return smsc_phy_ack_interrupt(phydev); 84 } 85 86 static int smsc_phy_reset(struct phy_device *phydev) 87 { 88 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES); 89 if (rc < 0) 90 return rc; 91 92 /* If the SMSC PHY is in power down mode, then set it 93 * in all capable mode before using it. 94 */ 95 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) { 96 /* set "all capable" mode */ 97 rc |= MII_LAN83C185_MODE_ALL; 98 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc); 99 } 100 101 /* reset the phy */ 102 return genphy_soft_reset(phydev); 103 } 104 105 static int lan911x_config_init(struct phy_device *phydev) 106 { 107 return smsc_phy_ack_interrupt(phydev); 108 } 109 110 static int lan87xx_config_aneg(struct phy_device *phydev) 111 { 112 int rc; 113 int val; 114 115 switch (phydev->mdix_ctrl) { 116 case ETH_TP_MDI: 117 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_; 118 break; 119 case ETH_TP_MDI_X: 120 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ | 121 SPECIAL_CTRL_STS_AMDIX_STATE_; 122 break; 123 case ETH_TP_MDI_AUTO: 124 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_; 125 break; 126 default: 127 return genphy_config_aneg(phydev); 128 } 129 130 rc = phy_read(phydev, SPECIAL_CTRL_STS); 131 if (rc < 0) 132 return rc; 133 134 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ | 135 SPECIAL_CTRL_STS_AMDIX_ENABLE_ | 136 SPECIAL_CTRL_STS_AMDIX_STATE_); 137 rc |= val; 138 phy_write(phydev, SPECIAL_CTRL_STS, rc); 139 140 phydev->mdix = phydev->mdix_ctrl; 141 return genphy_config_aneg(phydev); 142 } 143 144 static int lan87xx_config_aneg_ext(struct phy_device *phydev) 145 { 146 int rc; 147 148 /* Extend Manual AutoMDIX timer */ 149 rc = phy_read(phydev, PHY_EDPD_CONFIG); 150 if (rc < 0) 151 return rc; 152 153 rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_; 154 phy_write(phydev, PHY_EDPD_CONFIG, rc); 155 return lan87xx_config_aneg(phydev); 156 } 157 158 /* 159 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable 160 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to 161 * unstable detection of plugging in Ethernet cable. 162 * This workaround disables Energy Detect Power-Down mode and waiting for 163 * response on link pulses to detect presence of plugged Ethernet cable. 164 * The Energy Detect Power-Down mode is enabled again in the end of procedure to 165 * save approximately 220 mW of power if cable is unplugged. 166 */ 167 static int lan87xx_read_status(struct phy_device *phydev) 168 { 169 struct smsc_phy_priv *priv = phydev->priv; 170 171 int err = genphy_read_status(phydev); 172 173 if (!phydev->link && priv->energy_enable) { 174 /* Disable EDPD to wake up PHY */ 175 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); 176 if (rc < 0) 177 return rc; 178 179 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS, 180 rc & ~MII_LAN83C185_EDPWRDOWN); 181 if (rc < 0) 182 return rc; 183 184 /* Wait max 640 ms to detect energy and the timeout is not 185 * an actual error. 186 */ 187 read_poll_timeout(phy_read, rc, 188 rc & MII_LAN83C185_ENERGYON || rc < 0, 189 10000, 640000, true, phydev, 190 MII_LAN83C185_CTRL_STATUS); 191 if (rc < 0) 192 return rc; 193 194 /* Re-enable EDPD */ 195 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS); 196 if (rc < 0) 197 return rc; 198 199 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS, 200 rc | MII_LAN83C185_EDPWRDOWN); 201 if (rc < 0) 202 return rc; 203 } 204 205 return err; 206 } 207 208 static int smsc_get_sset_count(struct phy_device *phydev) 209 { 210 return ARRAY_SIZE(smsc_hw_stats); 211 } 212 213 static void smsc_get_strings(struct phy_device *phydev, u8 *data) 214 { 215 int i; 216 217 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) { 218 strncpy(data + i * ETH_GSTRING_LEN, 219 smsc_hw_stats[i].string, ETH_GSTRING_LEN); 220 } 221 } 222 223 static u64 smsc_get_stat(struct phy_device *phydev, int i) 224 { 225 struct smsc_hw_stat stat = smsc_hw_stats[i]; 226 int val; 227 u64 ret; 228 229 val = phy_read(phydev, stat.reg); 230 if (val < 0) 231 ret = U64_MAX; 232 else 233 ret = val; 234 235 return ret; 236 } 237 238 static void smsc_get_stats(struct phy_device *phydev, 239 struct ethtool_stats *stats, u64 *data) 240 { 241 int i; 242 243 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) 244 data[i] = smsc_get_stat(phydev, i); 245 } 246 247 static int smsc_phy_probe(struct phy_device *phydev) 248 { 249 struct device *dev = &phydev->mdio.dev; 250 struct device_node *of_node = dev->of_node; 251 struct smsc_phy_priv *priv; 252 253 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 254 if (!priv) 255 return -ENOMEM; 256 257 priv->energy_enable = true; 258 259 if (of_property_read_bool(of_node, "smsc,disable-energy-detect")) 260 priv->energy_enable = false; 261 262 phydev->priv = priv; 263 264 return 0; 265 } 266 267 static struct phy_driver smsc_phy_driver[] = { 268 { 269 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */ 270 .phy_id_mask = 0xfffffff0, 271 .name = "SMSC LAN83C185", 272 273 /* PHY_BASIC_FEATURES */ 274 275 .probe = smsc_phy_probe, 276 277 /* basic functions */ 278 .config_init = smsc_phy_config_init, 279 .soft_reset = smsc_phy_reset, 280 281 /* IRQ related */ 282 .ack_interrupt = smsc_phy_ack_interrupt, 283 .config_intr = smsc_phy_config_intr, 284 285 .suspend = genphy_suspend, 286 .resume = genphy_resume, 287 }, { 288 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */ 289 .phy_id_mask = 0xfffffff0, 290 .name = "SMSC LAN8187", 291 292 /* PHY_BASIC_FEATURES */ 293 294 .probe = smsc_phy_probe, 295 296 /* basic functions */ 297 .config_init = smsc_phy_config_init, 298 .soft_reset = smsc_phy_reset, 299 300 /* IRQ related */ 301 .ack_interrupt = smsc_phy_ack_interrupt, 302 .config_intr = smsc_phy_config_intr, 303 304 /* Statistics */ 305 .get_sset_count = smsc_get_sset_count, 306 .get_strings = smsc_get_strings, 307 .get_stats = smsc_get_stats, 308 309 .suspend = genphy_suspend, 310 .resume = genphy_resume, 311 }, { 312 /* This covers internal PHY (phy_id: 0x0007C0C3) for 313 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505) 314 */ 315 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */ 316 .phy_id_mask = 0xfffffff0, 317 .name = "SMSC LAN8700", 318 319 /* PHY_BASIC_FEATURES */ 320 321 .probe = smsc_phy_probe, 322 323 /* basic functions */ 324 .read_status = lan87xx_read_status, 325 .config_init = smsc_phy_config_init, 326 .soft_reset = smsc_phy_reset, 327 .config_aneg = lan87xx_config_aneg, 328 329 /* IRQ related */ 330 .ack_interrupt = smsc_phy_ack_interrupt, 331 .config_intr = smsc_phy_config_intr, 332 333 /* Statistics */ 334 .get_sset_count = smsc_get_sset_count, 335 .get_strings = smsc_get_strings, 336 .get_stats = smsc_get_stats, 337 338 .suspend = genphy_suspend, 339 .resume = genphy_resume, 340 }, { 341 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */ 342 .phy_id_mask = 0xfffffff0, 343 .name = "SMSC LAN911x Internal PHY", 344 345 /* PHY_BASIC_FEATURES */ 346 347 .probe = smsc_phy_probe, 348 349 /* basic functions */ 350 .config_init = lan911x_config_init, 351 352 /* IRQ related */ 353 .ack_interrupt = smsc_phy_ack_interrupt, 354 .config_intr = smsc_phy_config_intr, 355 356 .suspend = genphy_suspend, 357 .resume = genphy_resume, 358 }, { 359 /* This covers internal PHY (phy_id: 0x0007C0F0) for 360 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01) 361 */ 362 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */ 363 .phy_id_mask = 0xfffffff0, 364 .name = "SMSC LAN8710/LAN8720", 365 366 /* PHY_BASIC_FEATURES */ 367 .flags = PHY_RST_AFTER_CLK_EN, 368 369 .probe = smsc_phy_probe, 370 371 /* basic functions */ 372 .read_status = lan87xx_read_status, 373 .config_init = smsc_phy_config_init, 374 .soft_reset = smsc_phy_reset, 375 .config_aneg = lan87xx_config_aneg_ext, 376 377 /* IRQ related */ 378 .ack_interrupt = smsc_phy_ack_interrupt, 379 .config_intr = smsc_phy_config_intr, 380 381 /* Statistics */ 382 .get_sset_count = smsc_get_sset_count, 383 .get_strings = smsc_get_strings, 384 .get_stats = smsc_get_stats, 385 386 .suspend = genphy_suspend, 387 .resume = genphy_resume, 388 }, { 389 .phy_id = 0x0007c110, 390 .phy_id_mask = 0xfffffff0, 391 .name = "SMSC LAN8740", 392 393 /* PHY_BASIC_FEATURES */ 394 .flags = PHY_RST_AFTER_CLK_EN, 395 396 .probe = smsc_phy_probe, 397 398 /* basic functions */ 399 .read_status = lan87xx_read_status, 400 .config_init = smsc_phy_config_init, 401 .soft_reset = smsc_phy_reset, 402 403 /* IRQ related */ 404 .ack_interrupt = smsc_phy_ack_interrupt, 405 .config_intr = smsc_phy_config_intr, 406 407 /* Statistics */ 408 .get_sset_count = smsc_get_sset_count, 409 .get_strings = smsc_get_strings, 410 .get_stats = smsc_get_stats, 411 412 .suspend = genphy_suspend, 413 .resume = genphy_resume, 414 } }; 415 416 module_phy_driver(smsc_phy_driver); 417 418 MODULE_DESCRIPTION("SMSC PHY driver"); 419 MODULE_AUTHOR("Herbert Valerio Riedel"); 420 MODULE_LICENSE("GPL"); 421 422 static struct mdio_device_id __maybe_unused smsc_tbl[] = { 423 { 0x0007c0a0, 0xfffffff0 }, 424 { 0x0007c0b0, 0xfffffff0 }, 425 { 0x0007c0c0, 0xfffffff0 }, 426 { 0x0007c0d0, 0xfffffff0 }, 427 { 0x0007c0f0, 0xfffffff0 }, 428 { 0x0007c110, 0xfffffff0 }, 429 { } 430 }; 431 432 MODULE_DEVICE_TABLE(mdio, smsc_tbl); 433