blob: 74568ae1612535948b750015740d051691422d09 [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0+
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +02002/*
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 *
Steve Glendinning90b24cf2012-04-16 12:13:29 +010011 * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
Steve Glendinning4d9b1a02008-04-28 18:37:29 +010012 *
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020013 */
14
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020015#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mii.h>
18#include <linux/ethtool.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020019#include <linux/of.h>
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020020#include <linux/phy.h>
21#include <linux/netdevice.h>
Javier Martinez Canillas43c67592012-01-03 20:23:18 -050022#include <linux/smscphy.h>
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020023
Andrew Lunn030a8902017-06-03 20:00:36 +020024struct smsc_hw_stat {
25 const char *string;
26 u8 reg;
27 u8 bits;
28};
29
30static struct smsc_hw_stat smsc_hw_stats[] = {
31 { "phy_symbol_errors", 26, 16},
32};
33
Teresa Remmet0a9c4532016-01-20 13:40:35 +010034struct smsc_phy_priv {
35 bool energy_enable;
36};
37
Steve Glendinning48c41b92008-04-28 18:36:46 +010038static int smsc_phy_config_intr(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020039{
40 int rc = phy_write (phydev, MII_LAN83C185_IM,
41 ((PHY_INTERRUPT_ENABLED == phydev->interrupts)
42 ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
43 : 0));
44
45 return rc < 0 ? rc : 0;
46}
47
Steve Glendinning48c41b92008-04-28 18:36:46 +010048static int smsc_phy_ack_interrupt(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020049{
50 int rc = phy_read (phydev, MII_LAN83C185_ISF);
51
52 return rc < 0 ? rc : 0;
53}
54
Steve Glendinning48c41b92008-04-28 18:36:46 +010055static int smsc_phy_config_init(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020056{
Teresa Remmet0a9c4532016-01-20 13:40:35 +010057 struct smsc_phy_priv *priv = phydev->priv;
58
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020059 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
60
61 if (rc < 0)
62 return rc;
63
Teresa Remmet0a9c4532016-01-20 13:40:35 +010064 if (priv->energy_enable) {
Heiko Schocherd88ecb32015-10-17 06:04:36 +020065 /* Enable energy detect mode for this SMSC Transceivers */
66 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
67 rc | MII_LAN83C185_EDPWRDOWN);
68 if (rc < 0)
69 return rc;
70 }
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020071
72 return smsc_phy_ack_interrupt(phydev);
73}
74
75static int smsc_phy_reset(struct phy_device *phydev)
76{
trem6ded7cd2012-12-04 08:52:10 +000077 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
78 if (rc < 0)
79 return rc;
80
81 /* If the SMSC PHY is in power down mode, then set it
82 * in all capable mode before using it.
83 */
84 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +020085 /* set "all capable" mode */
trem6ded7cd2012-12-04 08:52:10 +000086 rc |= MII_LAN83C185_MODE_ALL;
87 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
trem6ded7cd2012-12-04 08:52:10 +000088 }
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +020089
90 /* reset the phy */
91 return genphy_soft_reset(phydev);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020092}
93
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -080094static int lan911x_config_init(struct phy_device *phydev)
95{
96 return smsc_phy_ack_interrupt(phydev);
97}
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020098
Patrick Trantham4223dbf2012-11-15 09:00:57 +000099/*
Igor Plyatov776829d2015-08-14 20:11:02 +0300100 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
101 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
102 * unstable detection of plugging in Ethernet cable.
103 * This workaround disables Energy Detect Power-Down mode and waiting for
104 * response on link pulses to detect presence of plugged Ethernet cable.
105 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
106 * save approximately 220 mW of power if cable is unplugged.
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000107 */
108static int lan87xx_read_status(struct phy_device *phydev)
109{
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100110 struct smsc_phy_priv *priv = phydev->priv;
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000111
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100112 int err = genphy_read_status(phydev);
113
114 if (!phydev->link && priv->energy_enable) {
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000115 /* Disable EDPD to wake up PHY */
116 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
117 if (rc < 0)
118 return rc;
119
120 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
121 rc & ~MII_LAN83C185_EDPWRDOWN);
122 if (rc < 0)
123 return rc;
124
Dejin Zheng6d61f482020-06-20 22:55:34 +0800125 /* Wait max 640 ms to detect energy and the timeout is not
126 * an actual error.
127 */
128 read_poll_timeout(phy_read, rc,
129 rc & MII_LAN83C185_ENERGYON || rc < 0,
130 10000, 640000, true, phydev,
131 MII_LAN83C185_CTRL_STATUS);
Dejin Zheng7ae7ad22020-03-23 23:05:59 +0800132 if (rc < 0)
133 return rc;
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000134
135 /* Re-enable EDPD */
136 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
137 if (rc < 0)
138 return rc;
139
140 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
141 rc | MII_LAN83C185_EDPWRDOWN);
142 if (rc < 0)
143 return rc;
144 }
145
146 return err;
147}
148
Andrew Lunn030a8902017-06-03 20:00:36 +0200149static int smsc_get_sset_count(struct phy_device *phydev)
150{
151 return ARRAY_SIZE(smsc_hw_stats);
152}
153
154static void smsc_get_strings(struct phy_device *phydev, u8 *data)
155{
156 int i;
157
158 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
Arnd Bergmann2da55392017-06-20 22:40:46 +0200159 strncpy(data + i * ETH_GSTRING_LEN,
Andrew Lunn030a8902017-06-03 20:00:36 +0200160 smsc_hw_stats[i].string, ETH_GSTRING_LEN);
161 }
162}
163
Andrew Lunn030a8902017-06-03 20:00:36 +0200164static u64 smsc_get_stat(struct phy_device *phydev, int i)
165{
166 struct smsc_hw_stat stat = smsc_hw_stats[i];
167 int val;
168 u64 ret;
169
170 val = phy_read(phydev, stat.reg);
171 if (val < 0)
Jisheng Zhang6c3442f2018-04-27 16:18:58 +0800172 ret = U64_MAX;
Andrew Lunn030a8902017-06-03 20:00:36 +0200173 else
174 ret = val;
175
176 return ret;
177}
178
179static void smsc_get_stats(struct phy_device *phydev,
180 struct ethtool_stats *stats, u64 *data)
181{
182 int i;
183
184 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
185 data[i] = smsc_get_stat(phydev, i);
186}
187
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100188static int smsc_phy_probe(struct phy_device *phydev)
189{
190 struct device *dev = &phydev->mdio.dev;
191 struct device_node *of_node = dev->of_node;
192 struct smsc_phy_priv *priv;
193
194 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
195 if (!priv)
196 return -ENOMEM;
197
198 priv->energy_enable = true;
199
200 if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
201 priv->energy_enable = false;
202
203 phydev->priv = priv;
204
205 return 0;
206}
207
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000208static struct phy_driver smsc_phy_driver[] = {
209{
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200210 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
211 .phy_id_mask = 0xfffffff0,
212 .name = "SMSC LAN83C185",
213
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200214 /* PHY_BASIC_FEATURES */
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200215
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100216 .probe = smsc_phy_probe,
217
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200218 /* basic functions */
Steve Glendinning48c41b92008-04-28 18:36:46 +0100219 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200220 .soft_reset = smsc_phy_reset,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200221
222 /* IRQ related */
Steve Glendinning48c41b92008-04-28 18:36:46 +0100223 .ack_interrupt = smsc_phy_ack_interrupt,
224 .config_intr = smsc_phy_config_intr,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200225
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800226 .suspend = genphy_suspend,
227 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000228}, {
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100229 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
230 .phy_id_mask = 0xfffffff0,
231 .name = "SMSC LAN8187",
232
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200233 /* PHY_BASIC_FEATURES */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100234
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100235 .probe = smsc_phy_probe,
236
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100237 /* basic functions */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100238 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200239 .soft_reset = smsc_phy_reset,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100240
241 /* IRQ related */
242 .ack_interrupt = smsc_phy_ack_interrupt,
243 .config_intr = smsc_phy_config_intr,
244
Andrew Lunn030a8902017-06-03 20:00:36 +0200245 /* Statistics */
246 .get_sset_count = smsc_get_sset_count,
247 .get_strings = smsc_get_strings,
248 .get_stats = smsc_get_stats,
249
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800250 .suspend = genphy_suspend,
251 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000252}, {
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100253 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
254 .phy_id_mask = 0xfffffff0,
255 .name = "SMSC LAN8700",
256
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200257 /* PHY_BASIC_FEATURES */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100258
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100259 .probe = smsc_phy_probe,
260
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100261 /* basic functions */
Igor Plyatov776829d2015-08-14 20:11:02 +0300262 .read_status = lan87xx_read_status,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100263 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200264 .soft_reset = smsc_phy_reset,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100265
266 /* IRQ related */
267 .ack_interrupt = smsc_phy_ack_interrupt,
268 .config_intr = smsc_phy_config_intr,
269
Andrew Lunn030a8902017-06-03 20:00:36 +0200270 /* Statistics */
271 .get_sset_count = smsc_get_sset_count,
272 .get_strings = smsc_get_strings,
273 .get_stats = smsc_get_stats,
274
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800275 .suspend = genphy_suspend,
276 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000277}, {
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000278 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
279 .phy_id_mask = 0xfffffff0,
280 .name = "SMSC LAN911x Internal PHY",
281
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200282 /* PHY_BASIC_FEATURES */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000283
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100284 .probe = smsc_phy_probe,
285
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000286 /* basic functions */
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -0800287 .config_init = lan911x_config_init,
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000288
289 /* IRQ related */
290 .ack_interrupt = smsc_phy_ack_interrupt,
291 .config_intr = smsc_phy_config_intr,
292
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800293 .suspend = genphy_suspend,
294 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000295}, {
Steve Glendinninge072b632009-03-23 15:17:31 -0700296 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
297 .phy_id_mask = 0xfffffff0,
298 .name = "SMSC LAN8710/LAN8720",
299
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200300 /* PHY_BASIC_FEATURES */
Heiner Kallweita4307c02018-11-09 18:17:22 +0100301 .flags = PHY_RST_AFTER_CLK_EN,
Steve Glendinninge072b632009-03-23 15:17:31 -0700302
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100303 .probe = smsc_phy_probe,
304
Steve Glendinninge072b632009-03-23 15:17:31 -0700305 /* basic functions */
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000306 .read_status = lan87xx_read_status,
Patrick Trantham4257d582012-12-06 15:16:02 +0000307 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200308 .soft_reset = smsc_phy_reset,
Steve Glendinninge072b632009-03-23 15:17:31 -0700309
310 /* IRQ related */
311 .ack_interrupt = smsc_phy_ack_interrupt,
312 .config_intr = smsc_phy_config_intr,
313
Andrew Lunn030a8902017-06-03 20:00:36 +0200314 /* Statistics */
315 .get_sset_count = smsc_get_sset_count,
316 .get_strings = smsc_get_strings,
317 .get_stats = smsc_get_stats,
318
Steve Glendinninge072b632009-03-23 15:17:31 -0700319 .suspend = genphy_suspend,
320 .resume = genphy_resume,
Joshua Henderson26706d42016-01-09 04:54:21 -0700321}, {
322 .phy_id = 0x0007c110,
323 .phy_id_mask = 0xfffffff0,
324 .name = "SMSC LAN8740",
325
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200326 /* PHY_BASIC_FEATURES */
Martin Fuzzey76db2d42019-10-23 11:44:24 +0200327 .flags = PHY_RST_AFTER_CLK_EN,
Joshua Henderson26706d42016-01-09 04:54:21 -0700328
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100329 .probe = smsc_phy_probe,
330
Joshua Henderson26706d42016-01-09 04:54:21 -0700331 /* basic functions */
Joshua Henderson26706d42016-01-09 04:54:21 -0700332 .read_status = lan87xx_read_status,
333 .config_init = smsc_phy_config_init,
334 .soft_reset = smsc_phy_reset,
335
336 /* IRQ related */
337 .ack_interrupt = smsc_phy_ack_interrupt,
338 .config_intr = smsc_phy_config_intr,
339
Andrew Lunn030a8902017-06-03 20:00:36 +0200340 /* Statistics */
341 .get_sset_count = smsc_get_sset_count,
342 .get_strings = smsc_get_strings,
343 .get_stats = smsc_get_stats,
344
Joshua Henderson26706d42016-01-09 04:54:21 -0700345 .suspend = genphy_suspend,
346 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000347} };
Steve Glendinninge072b632009-03-23 15:17:31 -0700348
Johan Hovold50fd7152014-11-11 19:45:59 +0100349module_phy_driver(smsc_phy_driver);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200350
351MODULE_DESCRIPTION("SMSC PHY driver");
352MODULE_AUTHOR("Herbert Valerio Riedel");
353MODULE_LICENSE("GPL");
354
Uwe Kleine-Königcf93c942010-10-03 23:43:32 +0000355static struct mdio_device_id __maybe_unused smsc_tbl[] = {
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000356 { 0x0007c0a0, 0xfffffff0 },
357 { 0x0007c0b0, 0xfffffff0 },
358 { 0x0007c0c0, 0xfffffff0 },
359 { 0x0007c0d0, 0xfffffff0 },
360 { 0x0007c0f0, 0xfffffff0 },
Joshua Henderson26706d42016-01-09 04:54:21 -0700361 { 0x0007c110, 0xfffffff0 },
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000362 { }
363};
364
365MODULE_DEVICE_TABLE(mdio, smsc_tbl);