blob: 19ab2821cff028c1819ae06d4a79a6c884a94baa [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
Andre Edich05b35e72020-08-26 13:17:17 +020024/* Vendor-specific PHY Definitions */
25/* EDPD NLP / crossover time configuration */
26#define PHY_EDPD_CONFIG 16
27#define PHY_EDPD_CONFIG_EXT_CROSSOVER_ 0x0001
28
29/* Control/Status Indication Register */
30#define SPECIAL_CTRL_STS 27
31#define SPECIAL_CTRL_STS_OVRRD_AMDIX_ 0x8000
32#define SPECIAL_CTRL_STS_AMDIX_ENABLE_ 0x4000
33#define SPECIAL_CTRL_STS_AMDIX_STATE_ 0x2000
34
Andrew Lunn030a8902017-06-03 20:00:36 +020035struct smsc_hw_stat {
36 const char *string;
37 u8 reg;
38 u8 bits;
39};
40
41static struct smsc_hw_stat smsc_hw_stats[] = {
42 { "phy_symbol_errors", 26, 16},
43};
44
Teresa Remmet0a9c4532016-01-20 13:40:35 +010045struct smsc_phy_priv {
46 bool energy_enable;
47};
48
Steve Glendinning48c41b92008-04-28 18:36:46 +010049static int smsc_phy_config_intr(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020050{
Marco Felsch73654942020-09-09 15:44:57 +020051 struct smsc_phy_priv *priv = phydev->priv;
52 u16 intmask = 0;
53 int rc;
54
55 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
56 intmask = MII_LAN83C185_ISF_INT4 | MII_LAN83C185_ISF_INT6;
57 if (priv->energy_enable)
58 intmask |= MII_LAN83C185_ISF_INT7;
59 }
60
61 rc = phy_write(phydev, MII_LAN83C185_IM, intmask);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020062
63 return rc < 0 ? rc : 0;
64}
65
Steve Glendinning48c41b92008-04-28 18:36:46 +010066static int smsc_phy_ack_interrupt(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020067{
68 int rc = phy_read (phydev, MII_LAN83C185_ISF);
69
70 return rc < 0 ? rc : 0;
71}
72
Steve Glendinning48c41b92008-04-28 18:36:46 +010073static int smsc_phy_config_init(struct phy_device *phydev)
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +020074{
Teresa Remmet0a9c4532016-01-20 13:40:35 +010075 struct smsc_phy_priv *priv = phydev->priv;
76
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020077 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
78
79 if (rc < 0)
80 return rc;
81
Teresa Remmet0a9c4532016-01-20 13:40:35 +010082 if (priv->energy_enable) {
Heiko Schocherd88ecb32015-10-17 06:04:36 +020083 /* Enable energy detect mode for this SMSC Transceivers */
84 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
85 rc | MII_LAN83C185_EDPWRDOWN);
86 if (rc < 0)
87 return rc;
88 }
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020089
90 return smsc_phy_ack_interrupt(phydev);
91}
92
93static int smsc_phy_reset(struct phy_device *phydev)
94{
trem6ded7cd2012-12-04 08:52:10 +000095 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
96 if (rc < 0)
97 return rc;
98
99 /* If the SMSC PHY is in power down mode, then set it
100 * in all capable mode before using it.
101 */
102 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +0200103 /* set "all capable" mode */
trem6ded7cd2012-12-04 08:52:10 +0000104 rc |= MII_LAN83C185_MODE_ALL;
105 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
trem6ded7cd2012-12-04 08:52:10 +0000106 }
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +0200107
108 /* reset the phy */
109 return genphy_soft_reset(phydev);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200110}
111
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -0800112static int lan911x_config_init(struct phy_device *phydev)
113{
114 return smsc_phy_ack_interrupt(phydev);
115}
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200116
Andre Edich05b35e72020-08-26 13:17:17 +0200117static int lan87xx_config_aneg(struct phy_device *phydev)
118{
119 int rc;
120 int val;
121
122 switch (phydev->mdix_ctrl) {
123 case ETH_TP_MDI:
124 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
125 break;
126 case ETH_TP_MDI_X:
127 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
128 SPECIAL_CTRL_STS_AMDIX_STATE_;
129 break;
130 case ETH_TP_MDI_AUTO:
131 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
132 break;
133 default:
134 return genphy_config_aneg(phydev);
135 }
136
137 rc = phy_read(phydev, SPECIAL_CTRL_STS);
138 if (rc < 0)
139 return rc;
140
141 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
142 SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
143 SPECIAL_CTRL_STS_AMDIX_STATE_);
144 rc |= val;
145 phy_write(phydev, SPECIAL_CTRL_STS, rc);
146
147 phydev->mdix = phydev->mdix_ctrl;
148 return genphy_config_aneg(phydev);
149}
150
151static int lan87xx_config_aneg_ext(struct phy_device *phydev)
152{
153 int rc;
154
155 /* Extend Manual AutoMDIX timer */
156 rc = phy_read(phydev, PHY_EDPD_CONFIG);
157 if (rc < 0)
158 return rc;
159
160 rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
161 phy_write(phydev, PHY_EDPD_CONFIG, rc);
162 return lan87xx_config_aneg(phydev);
163}
164
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000165/*
Igor Plyatov776829d2015-08-14 20:11:02 +0300166 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
167 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
168 * unstable detection of plugging in Ethernet cable.
169 * This workaround disables Energy Detect Power-Down mode and waiting for
170 * response on link pulses to detect presence of plugged Ethernet cable.
171 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
172 * save approximately 220 mW of power if cable is unplugged.
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000173 */
174static int lan87xx_read_status(struct phy_device *phydev)
175{
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100176 struct smsc_phy_priv *priv = phydev->priv;
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000177
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100178 int err = genphy_read_status(phydev);
179
180 if (!phydev->link && priv->energy_enable) {
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000181 /* Disable EDPD to wake up PHY */
182 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
183 if (rc < 0)
184 return rc;
185
186 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
187 rc & ~MII_LAN83C185_EDPWRDOWN);
188 if (rc < 0)
189 return rc;
190
Dejin Zheng6d61f482020-06-20 22:55:34 +0800191 /* Wait max 640 ms to detect energy and the timeout is not
192 * an actual error.
193 */
194 read_poll_timeout(phy_read, rc,
195 rc & MII_LAN83C185_ENERGYON || rc < 0,
196 10000, 640000, true, phydev,
197 MII_LAN83C185_CTRL_STATUS);
Dejin Zheng7ae7ad22020-03-23 23:05:59 +0800198 if (rc < 0)
199 return rc;
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000200
201 /* Re-enable EDPD */
202 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
203 if (rc < 0)
204 return rc;
205
206 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
207 rc | MII_LAN83C185_EDPWRDOWN);
208 if (rc < 0)
209 return rc;
210 }
211
212 return err;
213}
214
Andrew Lunn030a8902017-06-03 20:00:36 +0200215static int smsc_get_sset_count(struct phy_device *phydev)
216{
217 return ARRAY_SIZE(smsc_hw_stats);
218}
219
220static void smsc_get_strings(struct phy_device *phydev, u8 *data)
221{
222 int i;
223
224 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
Arnd Bergmann2da55392017-06-20 22:40:46 +0200225 strncpy(data + i * ETH_GSTRING_LEN,
Andrew Lunn030a8902017-06-03 20:00:36 +0200226 smsc_hw_stats[i].string, ETH_GSTRING_LEN);
227 }
228}
229
Andrew Lunn030a8902017-06-03 20:00:36 +0200230static u64 smsc_get_stat(struct phy_device *phydev, int i)
231{
232 struct smsc_hw_stat stat = smsc_hw_stats[i];
233 int val;
234 u64 ret;
235
236 val = phy_read(phydev, stat.reg);
237 if (val < 0)
Jisheng Zhang6c3442f2018-04-27 16:18:58 +0800238 ret = U64_MAX;
Andrew Lunn030a8902017-06-03 20:00:36 +0200239 else
240 ret = val;
241
242 return ret;
243}
244
245static void smsc_get_stats(struct phy_device *phydev,
246 struct ethtool_stats *stats, u64 *data)
247{
248 int i;
249
250 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
251 data[i] = smsc_get_stat(phydev, i);
252}
253
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100254static int smsc_phy_probe(struct phy_device *phydev)
255{
256 struct device *dev = &phydev->mdio.dev;
257 struct device_node *of_node = dev->of_node;
258 struct smsc_phy_priv *priv;
259
260 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
261 if (!priv)
262 return -ENOMEM;
263
264 priv->energy_enable = true;
265
266 if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
267 priv->energy_enable = false;
268
269 phydev->priv = priv;
270
271 return 0;
272}
273
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000274static struct phy_driver smsc_phy_driver[] = {
275{
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200276 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
277 .phy_id_mask = 0xfffffff0,
278 .name = "SMSC LAN83C185",
279
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200280 /* PHY_BASIC_FEATURES */
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200281
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100282 .probe = smsc_phy_probe,
283
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200284 /* basic functions */
Steve Glendinning48c41b92008-04-28 18:36:46 +0100285 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200286 .soft_reset = smsc_phy_reset,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200287
288 /* IRQ related */
Steve Glendinning48c41b92008-04-28 18:36:46 +0100289 .ack_interrupt = smsc_phy_ack_interrupt,
290 .config_intr = smsc_phy_config_intr,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200291
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800292 .suspend = genphy_suspend,
293 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000294}, {
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100295 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
296 .phy_id_mask = 0xfffffff0,
297 .name = "SMSC LAN8187",
298
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200299 /* PHY_BASIC_FEATURES */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100300
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100301 .probe = smsc_phy_probe,
302
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100303 /* basic functions */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100304 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200305 .soft_reset = smsc_phy_reset,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100306
307 /* IRQ related */
308 .ack_interrupt = smsc_phy_ack_interrupt,
309 .config_intr = smsc_phy_config_intr,
310
Andrew Lunn030a8902017-06-03 20:00:36 +0200311 /* Statistics */
312 .get_sset_count = smsc_get_sset_count,
313 .get_strings = smsc_get_strings,
314 .get_stats = smsc_get_stats,
315
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800316 .suspend = genphy_suspend,
317 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000318}, {
Andre Edich05b35e72020-08-26 13:17:17 +0200319 /* This covers internal PHY (phy_id: 0x0007C0C3) for
320 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
321 */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100322 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
323 .phy_id_mask = 0xfffffff0,
324 .name = "SMSC LAN8700",
325
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200326 /* PHY_BASIC_FEATURES */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100327
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100328 .probe = smsc_phy_probe,
329
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100330 /* basic functions */
Igor Plyatov776829d2015-08-14 20:11:02 +0300331 .read_status = lan87xx_read_status,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100332 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200333 .soft_reset = smsc_phy_reset,
Andre Edich05b35e72020-08-26 13:17:17 +0200334 .config_aneg = lan87xx_config_aneg,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100335
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
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800345 .suspend = genphy_suspend,
346 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000347}, {
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000348 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
349 .phy_id_mask = 0xfffffff0,
350 .name = "SMSC LAN911x Internal PHY",
351
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200352 /* PHY_BASIC_FEATURES */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000353
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100354 .probe = smsc_phy_probe,
355
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000356 /* basic functions */
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -0800357 .config_init = lan911x_config_init,
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000358
359 /* IRQ related */
360 .ack_interrupt = smsc_phy_ack_interrupt,
361 .config_intr = smsc_phy_config_intr,
362
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800363 .suspend = genphy_suspend,
364 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000365}, {
Andre Edich05b35e72020-08-26 13:17:17 +0200366 /* This covers internal PHY (phy_id: 0x0007C0F0) for
367 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
368 */
Steve Glendinninge072b632009-03-23 15:17:31 -0700369 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
370 .phy_id_mask = 0xfffffff0,
371 .name = "SMSC LAN8710/LAN8720",
372
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200373 /* PHY_BASIC_FEATURES */
Heiner Kallweita4307c02018-11-09 18:17:22 +0100374 .flags = PHY_RST_AFTER_CLK_EN,
Steve Glendinninge072b632009-03-23 15:17:31 -0700375
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100376 .probe = smsc_phy_probe,
377
Steve Glendinninge072b632009-03-23 15:17:31 -0700378 /* basic functions */
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000379 .read_status = lan87xx_read_status,
Patrick Trantham4257d582012-12-06 15:16:02 +0000380 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200381 .soft_reset = smsc_phy_reset,
Andre Edich05b35e72020-08-26 13:17:17 +0200382 .config_aneg = lan87xx_config_aneg_ext,
Steve Glendinninge072b632009-03-23 15:17:31 -0700383
384 /* IRQ related */
385 .ack_interrupt = smsc_phy_ack_interrupt,
386 .config_intr = smsc_phy_config_intr,
387
Andrew Lunn030a8902017-06-03 20:00:36 +0200388 /* Statistics */
389 .get_sset_count = smsc_get_sset_count,
390 .get_strings = smsc_get_strings,
391 .get_stats = smsc_get_stats,
392
Steve Glendinninge072b632009-03-23 15:17:31 -0700393 .suspend = genphy_suspend,
394 .resume = genphy_resume,
Joshua Henderson26706d42016-01-09 04:54:21 -0700395}, {
396 .phy_id = 0x0007c110,
397 .phy_id_mask = 0xfffffff0,
398 .name = "SMSC LAN8740",
399
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200400 /* PHY_BASIC_FEATURES */
Martin Fuzzey76db2d42019-10-23 11:44:24 +0200401 .flags = PHY_RST_AFTER_CLK_EN,
Joshua Henderson26706d42016-01-09 04:54:21 -0700402
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100403 .probe = smsc_phy_probe,
404
Joshua Henderson26706d42016-01-09 04:54:21 -0700405 /* basic functions */
Joshua Henderson26706d42016-01-09 04:54:21 -0700406 .read_status = lan87xx_read_status,
407 .config_init = smsc_phy_config_init,
408 .soft_reset = smsc_phy_reset,
409
410 /* IRQ related */
411 .ack_interrupt = smsc_phy_ack_interrupt,
412 .config_intr = smsc_phy_config_intr,
413
Andrew Lunn030a8902017-06-03 20:00:36 +0200414 /* Statistics */
415 .get_sset_count = smsc_get_sset_count,
416 .get_strings = smsc_get_strings,
417 .get_stats = smsc_get_stats,
418
Joshua Henderson26706d42016-01-09 04:54:21 -0700419 .suspend = genphy_suspend,
420 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000421} };
Steve Glendinninge072b632009-03-23 15:17:31 -0700422
Johan Hovold50fd7152014-11-11 19:45:59 +0100423module_phy_driver(smsc_phy_driver);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200424
425MODULE_DESCRIPTION("SMSC PHY driver");
426MODULE_AUTHOR("Herbert Valerio Riedel");
427MODULE_LICENSE("GPL");
428
Uwe Kleine-Königcf93c942010-10-03 23:43:32 +0000429static struct mdio_device_id __maybe_unused smsc_tbl[] = {
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000430 { 0x0007c0a0, 0xfffffff0 },
431 { 0x0007c0b0, 0xfffffff0 },
432 { 0x0007c0c0, 0xfffffff0 },
433 { 0x0007c0d0, 0xfffffff0 },
434 { 0x0007c0f0, 0xfffffff0 },
Joshua Henderson26706d42016-01-09 04:54:21 -0700435 { 0x0007c110, 0xfffffff0 },
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000436 { }
437};
438
439MODULE_DEVICE_TABLE(mdio, smsc_tbl);