1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/net/phy/smsc.c
7 * Author: Herbert Valerio Riedel
9 * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org>
11 * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
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>
21 #include <linux/phy.h>
22 #include <linux/netdevice.h>
23 #include <linux/smscphy.h>
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
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
42 static struct smsc_hw_stat smsc_hw_stats[] = {
43 { "phy_symbol_errors", 26, 16},
46 struct smsc_phy_priv {
51 static int smsc_phy_ack_interrupt(struct phy_device *phydev)
53 int rc = phy_read(phydev, MII_LAN83C185_ISF);
55 return rc < 0 ? rc : 0;
58 static int smsc_phy_config_intr(struct phy_device *phydev)
60 struct smsc_phy_priv *priv = phydev->priv;
64 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
65 rc = smsc_phy_ack_interrupt(phydev);
69 intmask = MII_LAN83C185_ISF_INT4 | MII_LAN83C185_ISF_INT6;
70 if (priv->energy_enable)
71 intmask |= MII_LAN83C185_ISF_INT7;
72 rc = phy_write(phydev, MII_LAN83C185_IM, intmask);
74 rc = phy_write(phydev, MII_LAN83C185_IM, intmask);
78 rc = smsc_phy_ack_interrupt(phydev);
81 return rc < 0 ? rc : 0;
84 static irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev)
86 int irq_status, irq_enabled;
88 irq_enabled = phy_read(phydev, MII_LAN83C185_IM);
89 if (irq_enabled < 0) {
94 irq_status = phy_read(phydev, MII_LAN83C185_ISF);
100 if (!(irq_status & irq_enabled))
103 phy_trigger_machine(phydev);
108 static int smsc_phy_config_init(struct phy_device *phydev)
110 struct smsc_phy_priv *priv = phydev->priv;
113 if (!priv->energy_enable)
116 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
121 /* Enable energy detect mode for this SMSC Transceivers */
122 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
123 rc | MII_LAN83C185_EDPWRDOWN);
127 return smsc_phy_ack_interrupt(phydev);
130 static int smsc_phy_reset(struct phy_device *phydev)
132 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
136 /* If the SMSC PHY is in power down mode, then set it
137 * in all capable mode before using it.
139 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
140 /* set "all capable" mode */
141 rc |= MII_LAN83C185_MODE_ALL;
142 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
146 return genphy_soft_reset(phydev);
149 static int lan911x_config_init(struct phy_device *phydev)
151 return smsc_phy_ack_interrupt(phydev);
154 static int lan87xx_config_aneg(struct phy_device *phydev)
159 switch (phydev->mdix_ctrl) {
161 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
164 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
165 SPECIAL_CTRL_STS_AMDIX_STATE_;
167 case ETH_TP_MDI_AUTO:
168 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
171 return genphy_config_aneg(phydev);
174 rc = phy_read(phydev, SPECIAL_CTRL_STS);
178 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
179 SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
180 SPECIAL_CTRL_STS_AMDIX_STATE_);
182 phy_write(phydev, SPECIAL_CTRL_STS, rc);
184 phydev->mdix = phydev->mdix_ctrl;
185 return genphy_config_aneg(phydev);
188 static int lan95xx_config_aneg_ext(struct phy_device *phydev)
192 if (phydev->phy_id != 0x0007c0f0) /* not (LAN9500A or LAN9505A) */
193 return lan87xx_config_aneg(phydev);
195 /* Extend Manual AutoMDIX timer */
196 rc = phy_read(phydev, PHY_EDPD_CONFIG);
200 rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
201 phy_write(phydev, PHY_EDPD_CONFIG, rc);
202 return lan87xx_config_aneg(phydev);
206 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
207 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
208 * unstable detection of plugging in Ethernet cable.
209 * This workaround disables Energy Detect Power-Down mode and waiting for
210 * response on link pulses to detect presence of plugged Ethernet cable.
211 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
212 * save approximately 220 mW of power if cable is unplugged.
214 static int lan87xx_read_status(struct phy_device *phydev)
216 struct smsc_phy_priv *priv = phydev->priv;
218 int err = genphy_read_status(phydev);
220 if (!phydev->link && priv->energy_enable) {
223 /* Disable EDPD to wake up PHY */
224 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
228 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
229 rc & ~MII_LAN83C185_EDPWRDOWN);
233 /* Wait max 1500 ms to detect energy and the timeout is not
236 read_poll_timeout(phy_read, rc,
237 rc & MII_LAN83C185_ENERGYON || rc < 0,
238 150000, 1500000, true, phydev,
239 MII_LAN83C185_CTRL_STATUS);
244 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
248 energy_detected = !!(rc & MII_LAN83C185_ENERGYON);
250 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
251 rc | MII_LAN83C185_EDPWRDOWN);
255 /* Save CPU and power by deferring the next poll */
256 if (!energy_detected)
263 static int smsc_get_sset_count(struct phy_device *phydev)
265 return ARRAY_SIZE(smsc_hw_stats);
268 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
272 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
273 strncpy(data + i * ETH_GSTRING_LEN,
274 smsc_hw_stats[i].string, ETH_GSTRING_LEN);
278 static u64 smsc_get_stat(struct phy_device *phydev, int i)
280 struct smsc_hw_stat stat = smsc_hw_stats[i];
284 val = phy_read(phydev, stat.reg);
293 static void smsc_get_stats(struct phy_device *phydev,
294 struct ethtool_stats *stats, u64 *data)
298 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
299 data[i] = smsc_get_stat(phydev, i);
302 static void smsc_phy_remove(struct phy_device *phydev)
304 struct smsc_phy_priv *priv = phydev->priv;
306 clk_disable_unprepare(priv->refclk);
307 clk_put(priv->refclk);
310 static int smsc_phy_probe(struct phy_device *phydev)
312 struct device *dev = &phydev->mdio.dev;
313 struct device_node *of_node = dev->of_node;
314 struct smsc_phy_priv *priv;
317 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
321 priv->energy_enable = true;
323 if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
324 priv->energy_enable = false;
328 /* Make clk optional to keep DTB backward compatibility. */
329 priv->refclk = clk_get_optional(dev, NULL);
330 if (IS_ERR(priv->refclk))
331 return dev_err_probe(dev, PTR_ERR(priv->refclk),
332 "Failed to request clock\n");
334 ret = clk_prepare_enable(priv->refclk);
338 ret = clk_set_rate(priv->refclk, 50 * 1000 * 1000);
340 clk_disable_unprepare(priv->refclk);
347 static struct phy_driver smsc_phy_driver[] = {
349 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
350 .phy_id_mask = 0xfffffff0,
351 .name = "SMSC LAN83C185",
353 /* PHY_BASIC_FEATURES */
355 .probe = smsc_phy_probe,
357 /* basic functions */
358 .config_init = smsc_phy_config_init,
359 .soft_reset = smsc_phy_reset,
362 .config_intr = smsc_phy_config_intr,
363 .handle_interrupt = smsc_phy_handle_interrupt,
365 .suspend = genphy_suspend,
366 .resume = genphy_resume,
368 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
369 .phy_id_mask = 0xfffffff0,
370 .name = "SMSC LAN8187",
372 /* PHY_BASIC_FEATURES */
374 .probe = smsc_phy_probe,
376 /* basic functions */
377 .config_init = smsc_phy_config_init,
378 .soft_reset = smsc_phy_reset,
381 .config_intr = smsc_phy_config_intr,
382 .handle_interrupt = smsc_phy_handle_interrupt,
385 .get_sset_count = smsc_get_sset_count,
386 .get_strings = smsc_get_strings,
387 .get_stats = smsc_get_stats,
389 .suspend = genphy_suspend,
390 .resume = genphy_resume,
392 /* This covers internal PHY (phy_id: 0x0007C0C3) for
393 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
395 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
396 .phy_id_mask = 0xfffffff0,
397 .name = "SMSC LAN8700",
399 /* PHY_BASIC_FEATURES */
401 .probe = smsc_phy_probe,
403 /* basic functions */
404 .read_status = lan87xx_read_status,
405 .config_init = smsc_phy_config_init,
406 .soft_reset = smsc_phy_reset,
407 .config_aneg = lan87xx_config_aneg,
410 .config_intr = smsc_phy_config_intr,
411 .handle_interrupt = smsc_phy_handle_interrupt,
414 .get_sset_count = smsc_get_sset_count,
415 .get_strings = smsc_get_strings,
416 .get_stats = smsc_get_stats,
418 .suspend = genphy_suspend,
419 .resume = genphy_resume,
421 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
422 .phy_id_mask = 0xfffffff0,
423 .name = "SMSC LAN911x Internal PHY",
425 /* PHY_BASIC_FEATURES */
427 .probe = smsc_phy_probe,
429 /* basic functions */
430 .config_init = lan911x_config_init,
433 .config_intr = smsc_phy_config_intr,
434 .handle_interrupt = smsc_phy_handle_interrupt,
436 .suspend = genphy_suspend,
437 .resume = genphy_resume,
439 /* This covers internal PHY (phy_id: 0x0007C0F0) for
440 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
442 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
443 .phy_id_mask = 0xfffffff0,
444 .name = "SMSC LAN8710/LAN8720",
446 /* PHY_BASIC_FEATURES */
448 .probe = smsc_phy_probe,
449 .remove = smsc_phy_remove,
451 /* basic functions */
452 .read_status = lan87xx_read_status,
453 .config_init = smsc_phy_config_init,
454 .soft_reset = smsc_phy_reset,
455 .config_aneg = lan95xx_config_aneg_ext,
458 .config_intr = smsc_phy_config_intr,
459 .handle_interrupt = smsc_phy_handle_interrupt,
462 .get_sset_count = smsc_get_sset_count,
463 .get_strings = smsc_get_strings,
464 .get_stats = smsc_get_stats,
466 .suspend = genphy_suspend,
467 .resume = genphy_resume,
469 .phy_id = 0x0007c110,
470 .phy_id_mask = 0xfffffff0,
471 .name = "SMSC LAN8740",
473 /* PHY_BASIC_FEATURES */
474 .flags = PHY_RST_AFTER_CLK_EN,
476 .probe = smsc_phy_probe,
478 /* basic functions */
479 .read_status = lan87xx_read_status,
480 .config_init = smsc_phy_config_init,
481 .soft_reset = smsc_phy_reset,
484 .config_intr = smsc_phy_config_intr,
485 .handle_interrupt = smsc_phy_handle_interrupt,
488 .get_sset_count = smsc_get_sset_count,
489 .get_strings = smsc_get_strings,
490 .get_stats = smsc_get_stats,
492 .suspend = genphy_suspend,
493 .resume = genphy_resume,
496 module_phy_driver(smsc_phy_driver);
498 MODULE_DESCRIPTION("SMSC PHY driver");
499 MODULE_AUTHOR("Herbert Valerio Riedel");
500 MODULE_LICENSE("GPL");
502 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
503 { 0x0007c0a0, 0xfffffff0 },
504 { 0x0007c0b0, 0xfffffff0 },
505 { 0x0007c0c0, 0xfffffff0 },
506 { 0x0007c0d0, 0xfffffff0 },
507 { 0x0007c0f0, 0xfffffff0 },
508 { 0x0007c110, 0xfffffff0 },
512 MODULE_DEVICE_TABLE(mdio, smsc_tbl);