blob: 0bcdf927a790e611e4fc649ca16aca9237264720 [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;
Marco Felsch436e3802020-09-09 15:44:58 +020076 int rc;
Teresa Remmet0a9c4532016-01-20 13:40:35 +010077
Marco Felsch436e3802020-09-09 15:44:58 +020078 if (!priv->energy_enable)
79 return 0;
80
81 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020082
83 if (rc < 0)
84 return rc;
85
Marco Felsch436e3802020-09-09 15:44:58 +020086 /* Enable energy detect mode for this SMSC Transceivers */
87 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
88 rc | MII_LAN83C185_EDPWRDOWN);
89 if (rc < 0)
90 return rc;
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +020091
92 return smsc_phy_ack_interrupt(phydev);
93}
94
95static int smsc_phy_reset(struct phy_device *phydev)
96{
trem6ded7cd2012-12-04 08:52:10 +000097 int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
98 if (rc < 0)
99 return rc;
100
101 /* If the SMSC PHY is in power down mode, then set it
102 * in all capable mode before using it.
103 */
104 if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +0200105 /* set "all capable" mode */
trem6ded7cd2012-12-04 08:52:10 +0000106 rc |= MII_LAN83C185_MODE_ALL;
107 phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
trem6ded7cd2012-12-04 08:52:10 +0000108 }
Manfred Schlaeglfc0f7e32016-06-06 10:47:47 +0200109
110 /* reset the phy */
111 return genphy_soft_reset(phydev);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200112}
113
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -0800114static int lan911x_config_init(struct phy_device *phydev)
115{
116 return smsc_phy_ack_interrupt(phydev);
117}
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200118
Andre Edich05b35e72020-08-26 13:17:17 +0200119static int lan87xx_config_aneg(struct phy_device *phydev)
120{
121 int rc;
122 int val;
123
124 switch (phydev->mdix_ctrl) {
125 case ETH_TP_MDI:
126 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
127 break;
128 case ETH_TP_MDI_X:
129 val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
130 SPECIAL_CTRL_STS_AMDIX_STATE_;
131 break;
132 case ETH_TP_MDI_AUTO:
133 val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
134 break;
135 default:
136 return genphy_config_aneg(phydev);
137 }
138
139 rc = phy_read(phydev, SPECIAL_CTRL_STS);
140 if (rc < 0)
141 return rc;
142
143 rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
144 SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
145 SPECIAL_CTRL_STS_AMDIX_STATE_);
146 rc |= val;
147 phy_write(phydev, SPECIAL_CTRL_STS, rc);
148
149 phydev->mdix = phydev->mdix_ctrl;
150 return genphy_config_aneg(phydev);
151}
152
153static int lan87xx_config_aneg_ext(struct phy_device *phydev)
154{
155 int rc;
156
157 /* Extend Manual AutoMDIX timer */
158 rc = phy_read(phydev, PHY_EDPD_CONFIG);
159 if (rc < 0)
160 return rc;
161
162 rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
163 phy_write(phydev, PHY_EDPD_CONFIG, rc);
164 return lan87xx_config_aneg(phydev);
165}
166
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000167/*
Igor Plyatov776829d2015-08-14 20:11:02 +0300168 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
169 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
170 * unstable detection of plugging in Ethernet cable.
171 * This workaround disables Energy Detect Power-Down mode and waiting for
172 * response on link pulses to detect presence of plugged Ethernet cable.
173 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
174 * save approximately 220 mW of power if cable is unplugged.
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000175 */
176static int lan87xx_read_status(struct phy_device *phydev)
177{
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100178 struct smsc_phy_priv *priv = phydev->priv;
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000179
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100180 int err = genphy_read_status(phydev);
181
182 if (!phydev->link && priv->energy_enable) {
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000183 /* Disable EDPD to wake up PHY */
184 int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
185 if (rc < 0)
186 return rc;
187
188 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
189 rc & ~MII_LAN83C185_EDPWRDOWN);
190 if (rc < 0)
191 return rc;
192
Dejin Zheng6d61f482020-06-20 22:55:34 +0800193 /* Wait max 640 ms to detect energy and the timeout is not
194 * an actual error.
195 */
196 read_poll_timeout(phy_read, rc,
197 rc & MII_LAN83C185_ENERGYON || rc < 0,
198 10000, 640000, true, phydev,
199 MII_LAN83C185_CTRL_STATUS);
Dejin Zheng7ae7ad22020-03-23 23:05:59 +0800200 if (rc < 0)
201 return rc;
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000202
203 /* Re-enable EDPD */
204 rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
205 if (rc < 0)
206 return rc;
207
208 rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
209 rc | MII_LAN83C185_EDPWRDOWN);
210 if (rc < 0)
211 return rc;
212 }
213
214 return err;
215}
216
Andrew Lunn030a8902017-06-03 20:00:36 +0200217static int smsc_get_sset_count(struct phy_device *phydev)
218{
219 return ARRAY_SIZE(smsc_hw_stats);
220}
221
222static void smsc_get_strings(struct phy_device *phydev, u8 *data)
223{
224 int i;
225
226 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
Arnd Bergmann2da55392017-06-20 22:40:46 +0200227 strncpy(data + i * ETH_GSTRING_LEN,
Andrew Lunn030a8902017-06-03 20:00:36 +0200228 smsc_hw_stats[i].string, ETH_GSTRING_LEN);
229 }
230}
231
Andrew Lunn030a8902017-06-03 20:00:36 +0200232static u64 smsc_get_stat(struct phy_device *phydev, int i)
233{
234 struct smsc_hw_stat stat = smsc_hw_stats[i];
235 int val;
236 u64 ret;
237
238 val = phy_read(phydev, stat.reg);
239 if (val < 0)
Jisheng Zhang6c3442f2018-04-27 16:18:58 +0800240 ret = U64_MAX;
Andrew Lunn030a8902017-06-03 20:00:36 +0200241 else
242 ret = val;
243
244 return ret;
245}
246
247static void smsc_get_stats(struct phy_device *phydev,
248 struct ethtool_stats *stats, u64 *data)
249{
250 int i;
251
252 for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
253 data[i] = smsc_get_stat(phydev, i);
254}
255
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100256static int smsc_phy_probe(struct phy_device *phydev)
257{
258 struct device *dev = &phydev->mdio.dev;
259 struct device_node *of_node = dev->of_node;
260 struct smsc_phy_priv *priv;
261
262 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
263 if (!priv)
264 return -ENOMEM;
265
266 priv->energy_enable = true;
267
268 if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
269 priv->energy_enable = false;
270
271 phydev->priv = priv;
272
273 return 0;
274}
275
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000276static struct phy_driver smsc_phy_driver[] = {
277{
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200278 .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
279 .phy_id_mask = 0xfffffff0,
280 .name = "SMSC LAN83C185",
281
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200282 /* PHY_BASIC_FEATURES */
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200283
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100284 .probe = smsc_phy_probe,
285
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200286 /* basic functions */
Steve Glendinning48c41b92008-04-28 18:36:46 +0100287 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200288 .soft_reset = smsc_phy_reset,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200289
290 /* IRQ related */
Steve Glendinning48c41b92008-04-28 18:36:46 +0100291 .ack_interrupt = smsc_phy_ack_interrupt,
292 .config_intr = smsc_phy_config_intr,
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200293
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800294 .suspend = genphy_suspend,
295 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000296}, {
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100297 .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
298 .phy_id_mask = 0xfffffff0,
299 .name = "SMSC LAN8187",
300
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200301 /* PHY_BASIC_FEATURES */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100302
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100303 .probe = smsc_phy_probe,
304
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100305 /* basic functions */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100306 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200307 .soft_reset = smsc_phy_reset,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100308
309 /* IRQ related */
310 .ack_interrupt = smsc_phy_ack_interrupt,
311 .config_intr = smsc_phy_config_intr,
312
Andrew Lunn030a8902017-06-03 20:00:36 +0200313 /* Statistics */
314 .get_sset_count = smsc_get_sset_count,
315 .get_strings = smsc_get_strings,
316 .get_stats = smsc_get_stats,
317
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800318 .suspend = genphy_suspend,
319 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000320}, {
Andre Edich05b35e72020-08-26 13:17:17 +0200321 /* This covers internal PHY (phy_id: 0x0007C0C3) for
322 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
323 */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100324 .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
325 .phy_id_mask = 0xfffffff0,
326 .name = "SMSC LAN8700",
327
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200328 /* PHY_BASIC_FEATURES */
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100329
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100330 .probe = smsc_phy_probe,
331
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100332 /* basic functions */
Igor Plyatov776829d2015-08-14 20:11:02 +0300333 .read_status = lan87xx_read_status,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100334 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200335 .soft_reset = smsc_phy_reset,
Andre Edich05b35e72020-08-26 13:17:17 +0200336 .config_aneg = lan87xx_config_aneg,
Steve Glendinning4d9b1a02008-04-28 18:37:29 +0100337
338 /* IRQ related */
339 .ack_interrupt = smsc_phy_ack_interrupt,
340 .config_intr = smsc_phy_config_intr,
341
Andrew Lunn030a8902017-06-03 20:00:36 +0200342 /* Statistics */
343 .get_sset_count = smsc_get_sset_count,
344 .get_strings = smsc_get_strings,
345 .get_stats = smsc_get_stats,
346
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800347 .suspend = genphy_suspend,
348 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000349}, {
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000350 .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
351 .phy_id_mask = 0xfffffff0,
352 .name = "SMSC LAN911x Internal PHY",
353
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200354 /* PHY_BASIC_FEATURES */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000355
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100356 .probe = smsc_phy_probe,
357
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000358 /* basic functions */
Giuseppe Cavallaro698244a2010-01-06 20:35:14 -0800359 .config_init = lan911x_config_init,
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000360
361 /* IRQ related */
362 .ack_interrupt = smsc_phy_ack_interrupt,
363 .config_intr = smsc_phy_config_intr,
364
Steve Glendinningc64d2a92009-01-22 14:07:43 -0800365 .suspend = genphy_suspend,
366 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000367}, {
Andre Edich05b35e72020-08-26 13:17:17 +0200368 /* This covers internal PHY (phy_id: 0x0007C0F0) for
369 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
370 */
Steve Glendinninge072b632009-03-23 15:17:31 -0700371 .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
372 .phy_id_mask = 0xfffffff0,
373 .name = "SMSC LAN8710/LAN8720",
374
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200375 /* PHY_BASIC_FEATURES */
Heiner Kallweita4307c02018-11-09 18:17:22 +0100376 .flags = PHY_RST_AFTER_CLK_EN,
Steve Glendinninge072b632009-03-23 15:17:31 -0700377
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100378 .probe = smsc_phy_probe,
379
Steve Glendinninge072b632009-03-23 15:17:31 -0700380 /* basic functions */
Patrick Trantham4223dbf2012-11-15 09:00:57 +0000381 .read_status = lan87xx_read_status,
Patrick Trantham4257d582012-12-06 15:16:02 +0000382 .config_init = smsc_phy_config_init,
Gwenhael Goavec-Merou21009682014-08-15 15:00:38 +0200383 .soft_reset = smsc_phy_reset,
Andre Edich05b35e72020-08-26 13:17:17 +0200384 .config_aneg = lan87xx_config_aneg_ext,
Steve Glendinninge072b632009-03-23 15:17:31 -0700385
386 /* IRQ related */
387 .ack_interrupt = smsc_phy_ack_interrupt,
388 .config_intr = smsc_phy_config_intr,
389
Andrew Lunn030a8902017-06-03 20:00:36 +0200390 /* Statistics */
391 .get_sset_count = smsc_get_sset_count,
392 .get_strings = smsc_get_strings,
393 .get_stats = smsc_get_stats,
394
Steve Glendinninge072b632009-03-23 15:17:31 -0700395 .suspend = genphy_suspend,
396 .resume = genphy_resume,
Joshua Henderson26706d42016-01-09 04:54:21 -0700397}, {
398 .phy_id = 0x0007c110,
399 .phy_id_mask = 0xfffffff0,
400 .name = "SMSC LAN8740",
401
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200402 /* PHY_BASIC_FEATURES */
Martin Fuzzey76db2d42019-10-23 11:44:24 +0200403 .flags = PHY_RST_AFTER_CLK_EN,
Joshua Henderson26706d42016-01-09 04:54:21 -0700404
Teresa Remmet0a9c4532016-01-20 13:40:35 +0100405 .probe = smsc_phy_probe,
406
Joshua Henderson26706d42016-01-09 04:54:21 -0700407 /* basic functions */
Joshua Henderson26706d42016-01-09 04:54:21 -0700408 .read_status = lan87xx_read_status,
409 .config_init = smsc_phy_config_init,
410 .soft_reset = smsc_phy_reset,
411
412 /* IRQ related */
413 .ack_interrupt = smsc_phy_ack_interrupt,
414 .config_intr = smsc_phy_config_intr,
415
Andrew Lunn030a8902017-06-03 20:00:36 +0200416 /* Statistics */
417 .get_sset_count = smsc_get_sset_count,
418 .get_strings = smsc_get_strings,
419 .get_stats = smsc_get_stats,
420
Joshua Henderson26706d42016-01-09 04:54:21 -0700421 .suspend = genphy_suspend,
422 .resume = genphy_resume,
Christian Hohnstaedtd5bf9072012-07-04 05:44:34 +0000423} };
Steve Glendinninge072b632009-03-23 15:17:31 -0700424
Johan Hovold50fd7152014-11-11 19:45:59 +0100425module_phy_driver(smsc_phy_driver);
Herbert Valerio Riedelc9e055a2006-05-07 23:22:53 +0200426
427MODULE_DESCRIPTION("SMSC PHY driver");
428MODULE_AUTHOR("Herbert Valerio Riedel");
429MODULE_LICENSE("GPL");
430
Uwe Kleine-Königcf93c942010-10-03 23:43:32 +0000431static struct mdio_device_id __maybe_unused smsc_tbl[] = {
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000432 { 0x0007c0a0, 0xfffffff0 },
433 { 0x0007c0b0, 0xfffffff0 },
434 { 0x0007c0c0, 0xfffffff0 },
435 { 0x0007c0d0, 0xfffffff0 },
436 { 0x0007c0f0, 0xfffffff0 },
Joshua Henderson26706d42016-01-09 04:54:21 -0700437 { 0x0007c110, 0xfffffff0 },
David Woodhouse4e4f10f2010-04-02 01:05:56 +0000438 { }
439};
440
441MODULE_DEVICE_TABLE(mdio, smsc_tbl);