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 {
50 static int smsc_phy_ack_interrupt(struct phy_device *phydev)
52 int rc = phy_read(phydev, MII_LAN83C185_ISF);
54 return rc < 0 ? rc : 0;
57 static int smsc_phy_config_intr(struct phy_device *phydev)
61 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
62 rc = smsc_phy_ack_interrupt(phydev);
66 rc = phy_write(phydev, MII_LAN83C185_IM,
67 MII_LAN83C185_ISF_INT_PHYLIB_EVENTS);
69 rc = phy_write(phydev, MII_LAN83C185_IM, 0);
73 rc = smsc_phy_ack_interrupt(phydev);
76 return rc < 0 ? rc : 0;
79 static irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev)
83 irq_status = phy_read(phydev, MII_LAN83C185_ISF);
85 if (irq_status != -ENODEV)
91 if (!(irq_status & MII_LAN83C185_ISF_INT_PHYLIB_EVENTS))
94 phy_trigger_machine(phydev);
99 static int smsc_phy_config_init(struct phy_device *phydev)
101 struct smsc_phy_priv *priv = phydev->priv;
103 if (!priv->energy_enable || phydev->irq != PHY_POLL)
106 /* Enable energy detect power down mode */
107 return phy_set_bits(phydev, MII_LAN83C185_CTRL_STATUS,
108 MII_LAN83C185_EDPWRDOWN);
111 static int smsc_phy_reset(struct phy_device *phydev)
113 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
117 /* If the SMSC PHY is in power down mode, then set it
118 * in all capable mode before using it.
120 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
121 /* set "all capable" mode */
122 rc |= MII_LAN83C185_MODE_ALL;
123 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
127 return genphy_soft_reset(phydev);
130 static int lan87xx_config_aneg(struct phy_device *phydev)
135 switch (phydev->mdix_ctrl) {
137 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
140 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
141 SPECIAL_CTRL_STS_AMDIX_STATE_;
143 case ETH_TP_MDI_AUTO:
144 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
147 return genphy_config_aneg(phydev);
150 rc = phy_read(phydev, SPECIAL_CTRL_STS);
154 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
155 SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
156 SPECIAL_CTRL_STS_AMDIX_STATE_);
158 phy_write(phydev, SPECIAL_CTRL_STS, rc);
160 phydev->mdix = phydev->mdix_ctrl;
161 return genphy_config_aneg(phydev);
164 static int lan95xx_config_aneg_ext(struct phy_device *phydev)
166 if (phydev->phy_id == 0x0007c0f0) { /* LAN9500A or LAN9505A */
167 /* Extend Manual AutoMDIX timer */
168 int rc = phy_set_bits(phydev, PHY_EDPD_CONFIG,
169 PHY_EDPD_CONFIG_EXT_CROSSOVER_);
175 return lan87xx_config_aneg(phydev);
179 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
180 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
181 * unstable detection of plugging in Ethernet cable.
182 * This workaround disables Energy Detect Power-Down mode and waiting for
183 * response on link pulses to detect presence of plugged Ethernet cable.
184 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
185 * save approximately 220 mW of power if cable is unplugged.
186 * The workaround is only applicable to poll mode. Energy Detect Power-Down may
187 * not be used in interrupt mode lest link change detection becomes unreliable.
189 static int lan87xx_read_status(struct phy_device *phydev)
191 struct smsc_phy_priv *priv = phydev->priv;
193 int err = genphy_read_status(phydev);
195 if (!phydev->link && priv->energy_enable && phydev->irq == PHY_POLL) {
196 /* Disable EDPD to wake up PHY */
197 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
201 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
202 rc & ~MII_LAN83C185_EDPWRDOWN);
206 /* Wait max 640 ms to detect energy and the timeout is not
209 read_poll_timeout(phy_read, rc,
210 rc & MII_LAN83C185_ENERGYON || rc < 0,
211 10000, 640000, true, phydev,
212 MII_LAN83C185_CTRL_STATUS);
217 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
221 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
222 rc | MII_LAN83C185_EDPWRDOWN);
230 static int smsc_get_sset_count(struct phy_device *phydev)
232 return ARRAY_SIZE(smsc_hw_stats);
235 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
239 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
240 strncpy(data + i * ETH_GSTRING_LEN,
241 smsc_hw_stats[i].string, ETH_GSTRING_LEN);
245 static u64 smsc_get_stat(struct phy_device *phydev, int i)
247 struct smsc_hw_stat stat = smsc_hw_stats[i];
251 val = phy_read(phydev, stat.reg);
260 static void smsc_get_stats(struct phy_device *phydev,
261 struct ethtool_stats *stats, u64 *data)
265 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
266 data[i] = smsc_get_stat(phydev, i);
269 static int smsc_phy_probe(struct phy_device *phydev)
271 struct device *dev = &phydev->mdio.dev;
272 struct smsc_phy_priv *priv;
275 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
279 priv->energy_enable = true;
281 if (device_property_present(dev, "smsc,disable-energy-detect"))
282 priv->energy_enable = false;
286 /* Make clk optional to keep DTB backward compatibility. */
287 refclk = devm_clk_get_optional_enabled(dev, NULL);
289 return dev_err_probe(dev, PTR_ERR(refclk),
290 "Failed to request clock\n");
292 return clk_set_rate(refclk, 50 * 1000 * 1000);
295 static struct phy_driver smsc_phy_driver[] = {
297 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
298 .phy_id_mask = 0xfffffff0,
299 .name = "SMSC LAN83C185",
301 /* PHY_BASIC_FEATURES */
303 .probe = smsc_phy_probe,
305 /* basic functions */
306 .config_init = smsc_phy_config_init,
307 .soft_reset = smsc_phy_reset,
310 .config_intr = smsc_phy_config_intr,
311 .handle_interrupt = smsc_phy_handle_interrupt,
313 .suspend = genphy_suspend,
314 .resume = genphy_resume,
316 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
317 .phy_id_mask = 0xfffffff0,
318 .name = "SMSC LAN8187",
320 /* PHY_BASIC_FEATURES */
322 .probe = smsc_phy_probe,
324 /* basic functions */
325 .config_init = smsc_phy_config_init,
326 .soft_reset = smsc_phy_reset,
329 .config_intr = smsc_phy_config_intr,
330 .handle_interrupt = smsc_phy_handle_interrupt,
333 .get_sset_count = smsc_get_sset_count,
334 .get_strings = smsc_get_strings,
335 .get_stats = smsc_get_stats,
337 .suspend = genphy_suspend,
338 .resume = genphy_resume,
340 /* This covers internal PHY (phy_id: 0x0007C0C3) for
341 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
343 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
344 .phy_id_mask = 0xfffffff0,
345 .name = "SMSC LAN8700",
347 /* PHY_BASIC_FEATURES */
349 .probe = smsc_phy_probe,
351 /* basic functions */
352 .read_status = lan87xx_read_status,
353 .config_init = smsc_phy_config_init,
354 .soft_reset = smsc_phy_reset,
355 .config_aneg = lan87xx_config_aneg,
358 .config_intr = smsc_phy_config_intr,
359 .handle_interrupt = smsc_phy_handle_interrupt,
362 .get_sset_count = smsc_get_sset_count,
363 .get_strings = smsc_get_strings,
364 .get_stats = smsc_get_stats,
366 .suspend = genphy_suspend,
367 .resume = genphy_resume,
369 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
370 .phy_id_mask = 0xfffffff0,
371 .name = "SMSC LAN911x Internal PHY",
373 /* PHY_BASIC_FEATURES */
375 .probe = smsc_phy_probe,
378 .config_intr = smsc_phy_config_intr,
379 .handle_interrupt = smsc_phy_handle_interrupt,
381 .suspend = genphy_suspend,
382 .resume = genphy_resume,
384 /* This covers internal PHY (phy_id: 0x0007C0F0) for
385 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
387 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
388 .phy_id_mask = 0xfffffff0,
389 .name = "SMSC LAN8710/LAN8720",
391 /* PHY_BASIC_FEATURES */
393 .probe = smsc_phy_probe,
395 /* basic functions */
396 .read_status = lan87xx_read_status,
397 .config_init = smsc_phy_config_init,
398 .soft_reset = smsc_phy_reset,
399 .config_aneg = lan95xx_config_aneg_ext,
402 .config_intr = smsc_phy_config_intr,
403 .handle_interrupt = smsc_phy_handle_interrupt,
406 .get_sset_count = smsc_get_sset_count,
407 .get_strings = smsc_get_strings,
408 .get_stats = smsc_get_stats,
410 .suspend = genphy_suspend,
411 .resume = genphy_resume,
413 .phy_id = 0x0007c110,
414 .phy_id_mask = 0xfffffff0,
415 .name = "SMSC LAN8740",
417 /* PHY_BASIC_FEATURES */
418 .flags = PHY_RST_AFTER_CLK_EN,
420 .probe = smsc_phy_probe,
422 /* basic functions */
423 .read_status = lan87xx_read_status,
424 .config_init = smsc_phy_config_init,
425 .soft_reset = smsc_phy_reset,
428 .config_intr = smsc_phy_config_intr,
429 .handle_interrupt = smsc_phy_handle_interrupt,
432 .get_sset_count = smsc_get_sset_count,
433 .get_strings = smsc_get_strings,
434 .get_stats = smsc_get_stats,
436 .suspend = genphy_suspend,
437 .resume = genphy_resume,
439 .phy_id = 0x0007c130, /* 0x0007c130 and 0x0007c131 */
440 /* This mask (0xfffffff2) is to differentiate from
441 * LAN88xx (phy_id 0x0007c132)
442 * and allows future phy_id revisions.
444 .phy_id_mask = 0xfffffff2,
445 .name = "Microchip LAN8742",
447 /* PHY_BASIC_FEATURES */
448 .flags = PHY_RST_AFTER_CLK_EN,
450 .probe = smsc_phy_probe,
452 /* basic functions */
453 .read_status = lan87xx_read_status,
454 .config_init = smsc_phy_config_init,
455 .soft_reset = smsc_phy_reset,
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,
470 module_phy_driver(smsc_phy_driver);
472 MODULE_DESCRIPTION("SMSC PHY driver");
473 MODULE_AUTHOR("Herbert Valerio Riedel");
474 MODULE_LICENSE("GPL");
476 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
477 { 0x0007c0a0, 0xfffffff0 },
478 { 0x0007c0b0, 0xfffffff0 },
479 { 0x0007c0c0, 0xfffffff0 },
480 { 0x0007c0d0, 0xfffffff0 },
481 { 0x0007c0f0, 0xfffffff0 },
482 { 0x0007c110, 0xfffffff0 },
483 { 0x0007c130, 0xfffffff2 },
487 MODULE_DEVICE_TABLE(mdio, smsc_tbl);