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/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mii.h>
18 #include <linux/ethtool.h>
20 #include <linux/phy.h>
21 #include <linux/netdevice.h>
22 #include <linux/smscphy.h>
30 static struct smsc_hw_stat smsc_hw_stats
[] = {
31 { "phy_symbol_errors", 26, 16},
34 struct smsc_phy_priv
{
38 static int smsc_phy_config_intr(struct phy_device
*phydev
)
40 int rc
= phy_write (phydev
, MII_LAN83C185_IM
,
41 ((PHY_INTERRUPT_ENABLED
== phydev
->interrupts
)
42 ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
45 return rc
< 0 ? rc
: 0;
48 static int smsc_phy_ack_interrupt(struct phy_device
*phydev
)
50 int rc
= phy_read (phydev
, MII_LAN83C185_ISF
);
52 return rc
< 0 ? rc
: 0;
55 static int smsc_phy_config_init(struct phy_device
*phydev
)
57 struct smsc_phy_priv
*priv
= phydev
->priv
;
59 int rc
= phy_read(phydev
, MII_LAN83C185_CTRL_STATUS
);
64 if (priv
->energy_enable
) {
65 /* Enable energy detect mode for this SMSC Transceivers */
66 rc
= phy_write(phydev
, MII_LAN83C185_CTRL_STATUS
,
67 rc
| MII_LAN83C185_EDPWRDOWN
);
72 return smsc_phy_ack_interrupt(phydev
);
75 static int smsc_phy_reset(struct phy_device
*phydev
)
77 int rc
= phy_read(phydev
, MII_LAN83C185_SPECIAL_MODES
);
81 /* If the SMSC PHY is in power down mode, then set it
82 * in all capable mode before using it.
84 if ((rc
& MII_LAN83C185_MODE_MASK
) == MII_LAN83C185_MODE_POWERDOWN
) {
85 /* set "all capable" mode */
86 rc
|= MII_LAN83C185_MODE_ALL
;
87 phy_write(phydev
, MII_LAN83C185_SPECIAL_MODES
, rc
);
91 return genphy_soft_reset(phydev
);
94 static int lan911x_config_init(struct phy_device
*phydev
)
96 return smsc_phy_ack_interrupt(phydev
);
100 * 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.
108 static int lan87xx_read_status(struct phy_device
*phydev
)
110 struct smsc_phy_priv
*priv
= phydev
->priv
;
112 int err
= genphy_read_status(phydev
);
114 if (!phydev
->link
&& priv
->energy_enable
) {
115 /* Disable EDPD to wake up PHY */
116 int rc
= phy_read(phydev
, MII_LAN83C185_CTRL_STATUS
);
120 rc
= phy_write(phydev
, MII_LAN83C185_CTRL_STATUS
,
121 rc
& ~MII_LAN83C185_EDPWRDOWN
);
125 /* Wait max 640 ms to detect energy and the timeout is not
128 read_poll_timeout(phy_read
, rc
,
129 rc
& MII_LAN83C185_ENERGYON
|| rc
< 0,
130 10000, 640000, true, phydev
,
131 MII_LAN83C185_CTRL_STATUS
);
136 rc
= phy_read(phydev
, MII_LAN83C185_CTRL_STATUS
);
140 rc
= phy_write(phydev
, MII_LAN83C185_CTRL_STATUS
,
141 rc
| MII_LAN83C185_EDPWRDOWN
);
149 static int smsc_get_sset_count(struct phy_device
*phydev
)
151 return ARRAY_SIZE(smsc_hw_stats
);
154 static void smsc_get_strings(struct phy_device
*phydev
, u8
*data
)
158 for (i
= 0; i
< ARRAY_SIZE(smsc_hw_stats
); i
++) {
159 strncpy(data
+ i
* ETH_GSTRING_LEN
,
160 smsc_hw_stats
[i
].string
, ETH_GSTRING_LEN
);
164 static u64
smsc_get_stat(struct phy_device
*phydev
, int i
)
166 struct smsc_hw_stat stat
= smsc_hw_stats
[i
];
170 val
= phy_read(phydev
, stat
.reg
);
179 static void smsc_get_stats(struct phy_device
*phydev
,
180 struct ethtool_stats
*stats
, u64
*data
)
184 for (i
= 0; i
< ARRAY_SIZE(smsc_hw_stats
); i
++)
185 data
[i
] = smsc_get_stat(phydev
, i
);
188 static int smsc_phy_probe(struct phy_device
*phydev
)
190 struct device
*dev
= &phydev
->mdio
.dev
;
191 struct device_node
*of_node
= dev
->of_node
;
192 struct smsc_phy_priv
*priv
;
194 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
198 priv
->energy_enable
= true;
200 if (of_property_read_bool(of_node
, "smsc,disable-energy-detect"))
201 priv
->energy_enable
= false;
208 static struct phy_driver smsc_phy_driver
[] = {
210 .phy_id
= 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
211 .phy_id_mask
= 0xfffffff0,
212 .name
= "SMSC LAN83C185",
214 /* PHY_BASIC_FEATURES */
216 .probe
= smsc_phy_probe
,
218 /* basic functions */
219 .config_init
= smsc_phy_config_init
,
220 .soft_reset
= smsc_phy_reset
,
223 .ack_interrupt
= smsc_phy_ack_interrupt
,
224 .config_intr
= smsc_phy_config_intr
,
226 .suspend
= genphy_suspend
,
227 .resume
= genphy_resume
,
229 .phy_id
= 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
230 .phy_id_mask
= 0xfffffff0,
231 .name
= "SMSC LAN8187",
233 /* PHY_BASIC_FEATURES */
235 .probe
= smsc_phy_probe
,
237 /* basic functions */
238 .config_init
= smsc_phy_config_init
,
239 .soft_reset
= smsc_phy_reset
,
242 .ack_interrupt
= smsc_phy_ack_interrupt
,
243 .config_intr
= smsc_phy_config_intr
,
246 .get_sset_count
= smsc_get_sset_count
,
247 .get_strings
= smsc_get_strings
,
248 .get_stats
= smsc_get_stats
,
250 .suspend
= genphy_suspend
,
251 .resume
= genphy_resume
,
253 .phy_id
= 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
254 .phy_id_mask
= 0xfffffff0,
255 .name
= "SMSC LAN8700",
257 /* PHY_BASIC_FEATURES */
259 .probe
= smsc_phy_probe
,
261 /* basic functions */
262 .read_status
= lan87xx_read_status
,
263 .config_init
= smsc_phy_config_init
,
264 .soft_reset
= smsc_phy_reset
,
267 .ack_interrupt
= smsc_phy_ack_interrupt
,
268 .config_intr
= smsc_phy_config_intr
,
271 .get_sset_count
= smsc_get_sset_count
,
272 .get_strings
= smsc_get_strings
,
273 .get_stats
= smsc_get_stats
,
275 .suspend
= genphy_suspend
,
276 .resume
= genphy_resume
,
278 .phy_id
= 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
279 .phy_id_mask
= 0xfffffff0,
280 .name
= "SMSC LAN911x Internal PHY",
282 /* PHY_BASIC_FEATURES */
284 .probe
= smsc_phy_probe
,
286 /* basic functions */
287 .config_init
= lan911x_config_init
,
290 .ack_interrupt
= smsc_phy_ack_interrupt
,
291 .config_intr
= smsc_phy_config_intr
,
293 .suspend
= genphy_suspend
,
294 .resume
= genphy_resume
,
296 .phy_id
= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
297 .phy_id_mask
= 0xfffffff0,
298 .name
= "SMSC LAN8710/LAN8720",
300 /* PHY_BASIC_FEATURES */
301 .flags
= PHY_RST_AFTER_CLK_EN
,
303 .probe
= smsc_phy_probe
,
305 /* basic functions */
306 .read_status
= lan87xx_read_status
,
307 .config_init
= smsc_phy_config_init
,
308 .soft_reset
= smsc_phy_reset
,
311 .ack_interrupt
= smsc_phy_ack_interrupt
,
312 .config_intr
= smsc_phy_config_intr
,
315 .get_sset_count
= smsc_get_sset_count
,
316 .get_strings
= smsc_get_strings
,
317 .get_stats
= smsc_get_stats
,
319 .suspend
= genphy_suspend
,
320 .resume
= genphy_resume
,
322 .phy_id
= 0x0007c110,
323 .phy_id_mask
= 0xfffffff0,
324 .name
= "SMSC LAN8740",
326 /* PHY_BASIC_FEATURES */
327 .flags
= PHY_RST_AFTER_CLK_EN
,
329 .probe
= smsc_phy_probe
,
331 /* basic functions */
332 .read_status
= lan87xx_read_status
,
333 .config_init
= smsc_phy_config_init
,
334 .soft_reset
= smsc_phy_reset
,
337 .ack_interrupt
= smsc_phy_ack_interrupt
,
338 .config_intr
= smsc_phy_config_intr
,
341 .get_sset_count
= smsc_get_sset_count
,
342 .get_strings
= smsc_get_strings
,
343 .get_stats
= smsc_get_stats
,
345 .suspend
= genphy_suspend
,
346 .resume
= genphy_resume
,
349 module_phy_driver(smsc_phy_driver
);
351 MODULE_DESCRIPTION("SMSC PHY driver");
352 MODULE_AUTHOR("Herbert Valerio Riedel");
353 MODULE_LICENSE("GPL");
355 static struct mdio_device_id __maybe_unused smsc_tbl
[] = {
356 { 0x0007c0a0, 0xfffffff0 },
357 { 0x0007c0b0, 0xfffffff0 },
358 { 0x0007c0c0, 0xfffffff0 },
359 { 0x0007c0d0, 0xfffffff0 },
360 { 0x0007c0f0, 0xfffffff0 },
361 { 0x0007c110, 0xfffffff0 },
365 MODULE_DEVICE_TABLE(mdio
, smsc_tbl
);