blob: 67c9f2b26c8eb777c5b492a48764698c63093b00 [file] [log] [blame]
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +02001/*
2 * drivers/net/phy/smsc.c
3 *
4 * Driver for SMSC PHYs
5 *
6 * Author: Herbert Valerio Riedel
7 *
8 * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
Steve Glendinning90b24cf2012-04-16 12:13:29 +010015 * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
Steve Glendinning4d9b1a02008-04-28 18:37:29 +010016 *
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020017 */
18
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020019#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/mii.h>
22#include <linux/ethtool.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020023#include <linux/of.h>
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020024#include <linux/phy.h>
25#include <linux/netdevice.h>
Javier Martinez Canillas43c67592012-01-03 20:23:18 -050026#include <linux/smscphy.h>
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020027
Teresa Remmet0a9c4532016-01-20 13:40:35 +010028struct smsc_phy_priv {
29 bool energy_enable;
30};
31
Steve Glendinning48c41b92008-04-28 18:36:46 +010032static int smsc_phy_config_intr(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020033{
34 int rc = phy_write (phydev, MII_LAN83C185_IM,
35 ((PHY_INTERRUPT_ENABLED == phydev->interrupts)
36 ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
37 : 0));
38
39 return rc < 0 ? rc : 0;
40}
41
Steve Glendinning48c41b92008-04-28 18:36:46 +010042static int smsc_phy_ack_interrupt(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020043{
44 int rc = phy_read (phydev, MII_LAN83C185_ISF);
45
46 return rc < 0 ? rc : 0;
47}
48
Steve Glendinning48c41b92008-04-28 18:36:46 +010049static int smsc_phy_config_init(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020050{
Teresa Remmet0a9c4532016-01-20 13:40:35 +010051 struct smsc_phy_priv *priv = phydev->priv;
52
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020053 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
54
55 if (rc < 0)
56 return rc;
57
Teresa Remmet0a9c4532016-01-20 13:40:35 +010058 if (priv->energy_enable) {
Heiko Schocherd88ecb32015-10-17 06:04:36 +020059 /* Enable energy detect mode for this SMSC Transceivers */
60 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
61 rc | MII_LAN83C185_EDPWRDOWN);
62 if (rc < 0)
63 return rc;
64 }
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020065
66 return smsc_phy_ack_interrupt(phydev);
67}
68
69static int smsc_phy_reset(struct phy_device *phydev)
70{
trem6ded7cd2012-12-04 08:52:10 +000071 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
72 if (rc < 0)
73 return rc;
74
75 /* If the SMSC PHY is in power down mode, then set it
76 * in all capable mode before using it.
77 */
78 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +020079 /* set "all capable" mode */
trem6ded7cd2012-12-04 08:52:10 +000080 rc |= MII_LAN83C185_MODE_ALL;
81 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
trem6ded7cd2012-12-04 08:52:10 +000082 }
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +020083
84 /* reset the phy */
85 return genphy_soft_reset(phydev);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020086}
87
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -080088static int lan911x_config_init(struct phy_device *phydev)
89{
90 return smsc_phy_ack_interrupt(phydev);
91}
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020092
Patrick Trantham4223dbf2012-11-15 09:00:57 +000093/*
Igor Plyatov776829d2015-08-14 20:11:02 +030094 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
95 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
96 * unstable detection of plugging in Ethernet cable.
97 * This workaround disables Energy Detect Power-Down mode and waiting for
98 * response on link pulses to detect presence of plugged Ethernet cable.
99 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
100 * save approximately 220 mW of power if cable is unplugged.
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000101 */
102static int lan87xx_read_status(struct phy_device *phydev)
103{
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100104 struct smsc_phy_priv *priv = phydev->priv;
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000105
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100106 int err = genphy_read_status(phydev);
107
108 if (!phydev->link && priv->energy_enable) {
109 int i;
110
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000111 /* Disable EDPD to wake up PHY */
112 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
113 if (rc < 0)
114 return rc;
115
116 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
117 rc & ~MII_LAN83C185_EDPWRDOWN);
118 if (rc < 0)
119 return rc;
120
Igor Plyatov776829d2015-08-14 20:11:02 +0300121 /* Wait max 640 ms to detect energy */
122 for (i = 0; i < 64; i++) {
123 /* Sleep to allow link test pulses to be sent */
124 msleep(10);
125 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
126 if (rc < 0)
127 return rc;
128 if (rc & MII_LAN83C185_ENERGYON)
129 break;
kbuild test robotff94c742015-08-18 06:31:42 +0800130 }
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000131
132 /* Re-enable EDPD */
133 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
134 if (rc < 0)
135 return rc;
136
137 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
138 rc | MII_LAN83C185_EDPWRDOWN);
139 if (rc < 0)
140 return rc;
141 }
142
143 return err;
144}
145
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100146static int smsc_phy_probe(struct phy_device *phydev)
147{
148 struct device *dev = &phydev->mdio.dev;
149 struct device_node *of_node = dev->of_node;
150 struct smsc_phy_priv *priv;
151
152 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
153 if (!priv)
154 return -ENOMEM;
155
156 priv->energy_enable = true;
157
158 if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
159 priv->energy_enable = false;
160
161 phydev->priv = priv;
162
163 return 0;
164}
165
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000166static struct phy_driver smsc_phy_driver[] = {
167{
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200168 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
169 .phy_id_mask = 0xfffffff0,
170 .name = "SMSC LAN83C185",
171
Timur Tabi529ed122016-12-07 13:20:51 -0600172 .features = PHY_BASIC_FEATURES,
Andrew Lunn1b86f702017-05-16 18:29:11 +0200173 .flags = PHY_HAS_INTERRUPT,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200174
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100175 .probe = smsc_phy_probe,
176
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200177 /* basic functions */
178 .config_aneg = genphy_config_aneg,
179 .read_status = genphy_read_status,
Steve Glendinning48c41b92008-04-28 18:36:46 +0100180 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200181 .soft_reset = smsc_phy_reset,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200182
183 /* IRQ related */
Steve Glendinning48c41b92008-04-28 18:36:46 +0100184 .ack_interrupt = smsc_phy_ack_interrupt,
185 .config_intr = smsc_phy_config_intr,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200186
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800187 .suspend = genphy_suspend,
188 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000189}, {
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100190 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
191 .phy_id_mask = 0xfffffff0,
192 .name = "SMSC LAN8187",
193
Timur Tabi529ed122016-12-07 13:20:51 -0600194 .features = PHY_BASIC_FEATURES,
Andrew Lunn1b86f702017-05-16 18:29:11 +0200195 .flags = PHY_HAS_INTERRUPT,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100196
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100197 .probe = smsc_phy_probe,
198
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100199 /* basic functions */
200 .config_aneg = genphy_config_aneg,
201 .read_status = genphy_read_status,
202 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200203 .soft_reset = smsc_phy_reset,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100204
205 /* IRQ related */
206 .ack_interrupt = smsc_phy_ack_interrupt,
207 .config_intr = smsc_phy_config_intr,
208
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800209 .suspend = genphy_suspend,
210 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000211}, {
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100212 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
213 .phy_id_mask = 0xfffffff0,
214 .name = "SMSC LAN8700",
215
Timur Tabi529ed122016-12-07 13:20:51 -0600216 .features = PHY_BASIC_FEATURES,
Andrew Lunn1b86f702017-05-16 18:29:11 +0200217 .flags = PHY_HAS_INTERRUPT,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100218
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100219 .probe = smsc_phy_probe,
220
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100221 /* basic functions */
222 .config_aneg = genphy_config_aneg,
Igor Plyatov776829d2015-08-14 20:11:02 +0300223 .read_status = lan87xx_read_status,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100224 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200225 .soft_reset = smsc_phy_reset,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100226
227 /* IRQ related */
228 .ack_interrupt = smsc_phy_ack_interrupt,
229 .config_intr = smsc_phy_config_intr,
230
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800231 .suspend = genphy_suspend,
232 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000233}, {
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000234 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
235 .phy_id_mask = 0xfffffff0,
236 .name = "SMSC LAN911x Internal PHY",
237
Timur Tabi529ed122016-12-07 13:20:51 -0600238 .features = PHY_BASIC_FEATURES,
Andrew Lunn1b86f702017-05-16 18:29:11 +0200239 .flags = PHY_HAS_INTERRUPT,
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000240
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100241 .probe = smsc_phy_probe,
242
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000243 /* basic functions */
244 .config_aneg = genphy_config_aneg,
245 .read_status = genphy_read_status,
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -0800246 .config_init = lan911x_config_init,
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000247
248 /* IRQ related */
249 .ack_interrupt = smsc_phy_ack_interrupt,
250 .config_intr = smsc_phy_config_intr,
251
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800252 .suspend = genphy_suspend,
253 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000254}, {
Steve Glendinninge072b632009-03-23 15:17:31 -0700255 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
256 .phy_id_mask = 0xfffffff0,
257 .name = "SMSC LAN8710/LAN8720",
258
Timur Tabi529ed122016-12-07 13:20:51 -0600259 .features = PHY_BASIC_FEATURES,
Andrew Lunn1b86f702017-05-16 18:29:11 +0200260 .flags = PHY_HAS_INTERRUPT,
Steve Glendinninge072b632009-03-23 15:17:31 -0700261
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100262 .probe = smsc_phy_probe,
263
Steve Glendinninge072b632009-03-23 15:17:31 -0700264 /* basic functions */
265 .config_aneg = genphy_config_aneg,
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000266 .read_status = lan87xx_read_status,
Patrick Trantham4257d582012-12-06 15:16:02 +0000267 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200268 .soft_reset = smsc_phy_reset,
Steve Glendinninge072b632009-03-23 15:17:31 -0700269
270 /* IRQ related */
271 .ack_interrupt = smsc_phy_ack_interrupt,
272 .config_intr = smsc_phy_config_intr,
273
274 .suspend = genphy_suspend,
275 .resume = genphy_resume,
Joshua Henderson26706d42016-01-09 04:54:21 -0700276}, {
277 .phy_id = 0x0007c110,
278 .phy_id_mask = 0xfffffff0,
279 .name = "SMSC LAN8740",
280
Timur Tabi529ed122016-12-07 13:20:51 -0600281 .features = PHY_BASIC_FEATURES,
Andrew Lunn1b86f702017-05-16 18:29:11 +0200282 .flags = PHY_HAS_INTERRUPT,
Joshua Henderson26706d42016-01-09 04:54:21 -0700283
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100284 .probe = smsc_phy_probe,
285
Joshua Henderson26706d42016-01-09 04:54:21 -0700286 /* basic functions */
287 .config_aneg = genphy_config_aneg,
288 .read_status = lan87xx_read_status,
289 .config_init = smsc_phy_config_init,
290 .soft_reset = smsc_phy_reset,
291
292 /* IRQ related */
293 .ack_interrupt = smsc_phy_ack_interrupt,
294 .config_intr = smsc_phy_config_intr,
295
296 .suspend = genphy_suspend,
297 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000298} };
Steve Glendinninge072b632009-03-23 15:17:31 -0700299
Johan Hovold50fd7152014-11-11 19:45:59 +0100300module_phy_driver(smsc_phy_driver);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200301
302MODULE_DESCRIPTION("SMSC PHY driver");
303MODULE_AUTHOR("Herbert Valerio Riedel");
304MODULE_LICENSE("GPL");
305
Uwe Kleine-Königcf93c942010-10-03 23:43:32 +0000306static struct mdio_device_id __maybe_unused smsc_tbl[] = {
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000307 { 0x0007c0a0, 0xfffffff0 },
308 { 0x0007c0b0, 0xfffffff0 },
309 { 0x0007c0c0, 0xfffffff0 },
310 { 0x0007c0d0, 0xfffffff0 },
311 { 0x0007c0f0, 0xfffffff0 },
Joshua Henderson26706d42016-01-09 04:54:21 -0700312 { 0x0007c110, 0xfffffff0 },
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000313 { }
314};
315
316MODULE_DEVICE_TABLE(mdio, smsc_tbl);