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_config_intr(struct phy_device *phydev)
53 struct smsc_phy_priv *priv = phydev->priv;
57 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
58 intmask = MII_LAN83C185_ISF_INT4 | MII_LAN83C185_ISF_INT6;
59 if (priv->energy_enable)
60 intmask |= MII_LAN83C185_ISF_INT7;
63 rc = phy_write(phydev, MII_LAN83C185_IM, intmask);
65 return rc < 0 ? rc : 0;
68 static int smsc_phy_ack_interrupt(struct phy_device *phydev)
70 int rc = phy_read (phydev, MII_LAN83C185_ISF);
72 return rc < 0 ? rc : 0;
75 static int smsc_phy_config_init(struct phy_device *phydev)
77 struct smsc_phy_priv *priv = phydev->priv;
80 if (!priv->energy_enable)
83 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
88 /* Enable energy detect mode for this SMSC Transceivers */
89 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
90 rc | MII_LAN83C185_EDPWRDOWN);
94 return smsc_phy_ack_interrupt(phydev);
97 static int smsc_phy_reset(struct phy_device *phydev)
99 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
103 /* If the SMSC PHY is in power down mode, then set it
104 * in all capable mode before using it.
106 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
107 /* set "all capable" mode */
108 rc |= MII_LAN83C185_MODE_ALL;
109 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
113 return genphy_soft_reset(phydev);
116 static int lan911x_config_init(struct phy_device *phydev)
118 return smsc_phy_ack_interrupt(phydev);
121 static int lan87xx_config_aneg(struct phy_device *phydev)
126 switch (phydev->mdix_ctrl) {
128 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
131 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
132 SPECIAL_CTRL_STS_AMDIX_STATE_;
134 case ETH_TP_MDI_AUTO:
135 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
138 return genphy_config_aneg(phydev);
141 rc = phy_read(phydev, SPECIAL_CTRL_STS);
145 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
146 SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
147 SPECIAL_CTRL_STS_AMDIX_STATE_);
149 phy_write(phydev, SPECIAL_CTRL_STS, rc);
151 phydev->mdix = phydev->mdix_ctrl;
152 return genphy_config_aneg(phydev);
155 static int lan95xx_config_aneg_ext(struct phy_device *phydev)
159 if (phydev->phy_id != 0x0007c0f0) /* not (LAN9500A or LAN9505A) */
160 return lan87xx_config_aneg(phydev);
162 /* Extend Manual AutoMDIX timer */
163 rc = phy_read(phydev, PHY_EDPD_CONFIG);
167 rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
168 phy_write(phydev, PHY_EDPD_CONFIG, rc);
169 return lan87xx_config_aneg(phydev);
173 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
174 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
175 * unstable detection of plugging in Ethernet cable.
176 * This workaround disables Energy Detect Power-Down mode and waiting for
177 * response on link pulses to detect presence of plugged Ethernet cable.
178 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
179 * save approximately 220 mW of power if cable is unplugged.
181 static int lan87xx_read_status(struct phy_device *phydev)
183 struct smsc_phy_priv *priv = phydev->priv;
186 err = genphy_read_status(phydev);
190 if (!phydev->link && priv->energy_enable) {
191 /* Disable EDPD to wake up PHY */
192 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
196 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
197 rc & ~MII_LAN83C185_EDPWRDOWN);
201 /* Wait max 640 ms to detect energy and the timeout is not
204 read_poll_timeout(phy_read, rc,
205 rc & MII_LAN83C185_ENERGYON || rc < 0,
206 10000, 640000, true, phydev,
207 MII_LAN83C185_CTRL_STATUS);
212 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
216 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
217 rc | MII_LAN83C185_EDPWRDOWN);
225 static int smsc_get_sset_count(struct phy_device *phydev)
227 return ARRAY_SIZE(smsc_hw_stats);
230 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
234 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
235 strncpy(data + i * ETH_GSTRING_LEN,
236 smsc_hw_stats[i].string, ETH_GSTRING_LEN);
240 static u64 smsc_get_stat(struct phy_device *phydev, int i)
242 struct smsc_hw_stat stat = smsc_hw_stats[i];
246 val = phy_read(phydev, stat.reg);
255 static void smsc_get_stats(struct phy_device *phydev,
256 struct ethtool_stats *stats, u64 *data)
260 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
261 data[i] = smsc_get_stat(phydev, i);
264 static void smsc_phy_remove(struct phy_device *phydev)
266 struct smsc_phy_priv *priv = phydev->priv;
268 clk_disable_unprepare(priv->refclk);
269 clk_put(priv->refclk);
272 static int smsc_phy_probe(struct phy_device *phydev)
274 struct device *dev = &phydev->mdio.dev;
275 struct device_node *of_node = dev->of_node;
276 struct smsc_phy_priv *priv;
279 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
283 priv->energy_enable = true;
285 if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
286 priv->energy_enable = false;
290 /* Make clk optional to keep DTB backward compatibility. */
291 priv->refclk = clk_get_optional(dev, NULL);
292 if (IS_ERR(priv->refclk))
293 return dev_err_probe(dev, PTR_ERR(priv->refclk),
294 "Failed to request clock\n");
296 ret = clk_prepare_enable(priv->refclk);
300 ret = clk_set_rate(priv->refclk, 50 * 1000 * 1000);
302 clk_disable_unprepare(priv->refclk);
309 static struct phy_driver smsc_phy_driver[] = {
311 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
312 .phy_id_mask = 0xfffffff0,
313 .name = "SMSC LAN83C185",
315 /* PHY_BASIC_FEATURES */
317 .probe = smsc_phy_probe,
319 /* basic functions */
320 .config_init = smsc_phy_config_init,
321 .soft_reset = smsc_phy_reset,
324 .ack_interrupt = smsc_phy_ack_interrupt,
325 .config_intr = smsc_phy_config_intr,
327 .suspend = genphy_suspend,
328 .resume = genphy_resume,
330 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
331 .phy_id_mask = 0xfffffff0,
332 .name = "SMSC LAN8187",
334 /* PHY_BASIC_FEATURES */
336 .probe = smsc_phy_probe,
338 /* basic functions */
339 .config_init = smsc_phy_config_init,
340 .soft_reset = smsc_phy_reset,
343 .ack_interrupt = smsc_phy_ack_interrupt,
344 .config_intr = smsc_phy_config_intr,
347 .get_sset_count = smsc_get_sset_count,
348 .get_strings = smsc_get_strings,
349 .get_stats = smsc_get_stats,
351 .suspend = genphy_suspend,
352 .resume = genphy_resume,
354 /* This covers internal PHY (phy_id: 0x0007C0C3) for
355 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
357 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
358 .phy_id_mask = 0xfffffff0,
359 .name = "SMSC LAN8700",
361 /* PHY_BASIC_FEATURES */
363 .probe = smsc_phy_probe,
365 /* basic functions */
366 .read_status = lan87xx_read_status,
367 .config_init = smsc_phy_config_init,
368 .soft_reset = smsc_phy_reset,
369 .config_aneg = lan87xx_config_aneg,
372 .ack_interrupt = smsc_phy_ack_interrupt,
373 .config_intr = smsc_phy_config_intr,
376 .get_sset_count = smsc_get_sset_count,
377 .get_strings = smsc_get_strings,
378 .get_stats = smsc_get_stats,
380 .suspend = genphy_suspend,
381 .resume = genphy_resume,
383 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
384 .phy_id_mask = 0xfffffff0,
385 .name = "SMSC LAN911x Internal PHY",
387 /* PHY_BASIC_FEATURES */
389 .probe = smsc_phy_probe,
391 /* basic functions */
392 .config_init = lan911x_config_init,
395 .ack_interrupt = smsc_phy_ack_interrupt,
396 .config_intr = smsc_phy_config_intr,
398 .suspend = genphy_suspend,
399 .resume = genphy_resume,
401 /* This covers internal PHY (phy_id: 0x0007C0F0) for
402 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
404 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
405 .phy_id_mask = 0xfffffff0,
406 .name = "SMSC LAN8710/LAN8720",
408 /* PHY_BASIC_FEATURES */
410 .probe = smsc_phy_probe,
411 .remove = smsc_phy_remove,
413 /* basic functions */
414 .read_status = lan87xx_read_status,
415 .config_init = smsc_phy_config_init,
416 .soft_reset = smsc_phy_reset,
417 .config_aneg = lan95xx_config_aneg_ext,
420 .ack_interrupt = smsc_phy_ack_interrupt,
421 .config_intr = smsc_phy_config_intr,
424 .get_sset_count = smsc_get_sset_count,
425 .get_strings = smsc_get_strings,
426 .get_stats = smsc_get_stats,
428 .suspend = genphy_suspend,
429 .resume = genphy_resume,
431 .phy_id = 0x0007c110,
432 .phy_id_mask = 0xfffffff0,
433 .name = "SMSC LAN8740",
435 /* PHY_BASIC_FEATURES */
436 .flags = PHY_RST_AFTER_CLK_EN,
438 .probe = smsc_phy_probe,
440 /* basic functions */
441 .read_status = lan87xx_read_status,
442 .config_init = smsc_phy_config_init,
443 .soft_reset = smsc_phy_reset,
446 .ack_interrupt = smsc_phy_ack_interrupt,
447 .config_intr = smsc_phy_config_intr,
450 .get_sset_count = smsc_get_sset_count,
451 .get_strings = smsc_get_strings,
452 .get_stats = smsc_get_stats,
454 .suspend = genphy_suspend,
455 .resume = genphy_resume,
458 module_phy_driver(smsc_phy_driver);
460 MODULE_DESCRIPTION("SMSC PHY driver");
461 MODULE_AUTHOR("Herbert Valerio Riedel");
462 MODULE_LICENSE("GPL");
464 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
465 { 0x0007c0a0, 0xfffffff0 },
466 { 0x0007c0b0, 0xfffffff0 },
467 { 0x0007c0c0, 0xfffffff0 },
468 { 0x0007c0d0, 0xfffffff0 },
469 { 0x0007c0f0, 0xfffffff0 },
470 { 0x0007c110, 0xfffffff0 },
474 MODULE_DEVICE_TABLE(mdio, smsc_tbl);