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
) {
117 /* Disable EDPD to wake up PHY */
118 int rc
= phy_read(phydev
, MII_LAN83C185_CTRL_STATUS
);
122 rc
= phy_write(phydev
, MII_LAN83C185_CTRL_STATUS
,
123 rc
& ~MII_LAN83C185_EDPWRDOWN
);
127 /* Wait max 640 ms to detect energy */
128 for (i
= 0; i
< 64; i
++) {
129 /* Sleep to allow link test pulses to be sent */
131 rc
= phy_read(phydev
, MII_LAN83C185_CTRL_STATUS
);
134 if (rc
& MII_LAN83C185_ENERGYON
)
139 rc
= phy_read(phydev
, MII_LAN83C185_CTRL_STATUS
);
143 rc
= phy_write(phydev
, MII_LAN83C185_CTRL_STATUS
,
144 rc
| MII_LAN83C185_EDPWRDOWN
);
152 static int smsc_get_sset_count(struct phy_device
*phydev
)
154 return ARRAY_SIZE(smsc_hw_stats
);
157 static void smsc_get_strings(struct phy_device
*phydev
, u8
*data
)
161 for (i
= 0; i
< ARRAY_SIZE(smsc_hw_stats
); i
++) {
162 strncpy(data
+ i
* ETH_GSTRING_LEN
,
163 smsc_hw_stats
[i
].string
, ETH_GSTRING_LEN
);
167 static u64
smsc_get_stat(struct phy_device
*phydev
, int i
)
169 struct smsc_hw_stat stat
= smsc_hw_stats
[i
];
173 val
= phy_read(phydev
, stat
.reg
);
182 static void smsc_get_stats(struct phy_device
*phydev
,
183 struct ethtool_stats
*stats
, u64
*data
)
187 for (i
= 0; i
< ARRAY_SIZE(smsc_hw_stats
); i
++)
188 data
[i
] = smsc_get_stat(phydev
, i
);
191 static int smsc_phy_probe(struct phy_device
*phydev
)
193 struct device
*dev
= &phydev
->mdio
.dev
;
194 struct device_node
*of_node
= dev
->of_node
;
195 struct smsc_phy_priv
*priv
;
197 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
201 priv
->energy_enable
= true;
203 if (of_property_read_bool(of_node
, "smsc,disable-energy-detect"))
204 priv
->energy_enable
= false;
211 static struct phy_driver smsc_phy_driver
[] = {
213 .phy_id
= 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
214 .phy_id_mask
= 0xfffffff0,
215 .name
= "SMSC LAN83C185",
217 /* PHY_BASIC_FEATURES */
219 .probe
= smsc_phy_probe
,
221 /* basic functions */
222 .config_init
= smsc_phy_config_init
,
223 .soft_reset
= smsc_phy_reset
,
226 .ack_interrupt
= smsc_phy_ack_interrupt
,
227 .config_intr
= smsc_phy_config_intr
,
229 .suspend
= genphy_suspend
,
230 .resume
= genphy_resume
,
232 .phy_id
= 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
233 .phy_id_mask
= 0xfffffff0,
234 .name
= "SMSC LAN8187",
236 /* PHY_BASIC_FEATURES */
238 .probe
= smsc_phy_probe
,
240 /* basic functions */
241 .config_init
= smsc_phy_config_init
,
242 .soft_reset
= smsc_phy_reset
,
245 .ack_interrupt
= smsc_phy_ack_interrupt
,
246 .config_intr
= smsc_phy_config_intr
,
249 .get_sset_count
= smsc_get_sset_count
,
250 .get_strings
= smsc_get_strings
,
251 .get_stats
= smsc_get_stats
,
253 .suspend
= genphy_suspend
,
254 .resume
= genphy_resume
,
256 .phy_id
= 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
257 .phy_id_mask
= 0xfffffff0,
258 .name
= "SMSC LAN8700",
260 /* PHY_BASIC_FEATURES */
262 .probe
= smsc_phy_probe
,
264 /* basic functions */
265 .read_status
= lan87xx_read_status
,
266 .config_init
= smsc_phy_config_init
,
267 .soft_reset
= smsc_phy_reset
,
270 .ack_interrupt
= smsc_phy_ack_interrupt
,
271 .config_intr
= smsc_phy_config_intr
,
274 .get_sset_count
= smsc_get_sset_count
,
275 .get_strings
= smsc_get_strings
,
276 .get_stats
= smsc_get_stats
,
278 .suspend
= genphy_suspend
,
279 .resume
= genphy_resume
,
281 .phy_id
= 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
282 .phy_id_mask
= 0xfffffff0,
283 .name
= "SMSC LAN911x Internal PHY",
285 /* PHY_BASIC_FEATURES */
287 .probe
= smsc_phy_probe
,
289 /* basic functions */
290 .config_init
= lan911x_config_init
,
293 .ack_interrupt
= smsc_phy_ack_interrupt
,
294 .config_intr
= smsc_phy_config_intr
,
296 .suspend
= genphy_suspend
,
297 .resume
= genphy_resume
,
299 .phy_id
= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
300 .phy_id_mask
= 0xfffffff0,
301 .name
= "SMSC LAN8710/LAN8720",
303 /* PHY_BASIC_FEATURES */
304 .flags
= PHY_RST_AFTER_CLK_EN
,
306 .probe
= smsc_phy_probe
,
308 /* basic functions */
309 .read_status
= lan87xx_read_status
,
310 .config_init
= smsc_phy_config_init
,
311 .soft_reset
= smsc_phy_reset
,
314 .ack_interrupt
= smsc_phy_ack_interrupt
,
315 .config_intr
= smsc_phy_config_intr
,
318 .get_sset_count
= smsc_get_sset_count
,
319 .get_strings
= smsc_get_strings
,
320 .get_stats
= smsc_get_stats
,
322 .suspend
= genphy_suspend
,
323 .resume
= genphy_resume
,
325 .phy_id
= 0x0007c110,
326 .phy_id_mask
= 0xfffffff0,
327 .name
= "SMSC LAN8740",
329 /* PHY_BASIC_FEATURES */
330 .flags
= PHY_RST_AFTER_CLK_EN
,
332 .probe
= smsc_phy_probe
,
334 /* basic functions */
335 .read_status
= lan87xx_read_status
,
336 .config_init
= smsc_phy_config_init
,
337 .soft_reset
= smsc_phy_reset
,
340 .ack_interrupt
= smsc_phy_ack_interrupt
,
341 .config_intr
= smsc_phy_config_intr
,
344 .get_sset_count
= smsc_get_sset_count
,
345 .get_strings
= smsc_get_strings
,
346 .get_stats
= smsc_get_stats
,
348 .suspend
= genphy_suspend
,
349 .resume
= genphy_resume
,
352 module_phy_driver(smsc_phy_driver
);
354 MODULE_DESCRIPTION("SMSC PHY driver");
355 MODULE_AUTHOR("Herbert Valerio Riedel");
356 MODULE_LICENSE("GPL");
358 static struct mdio_device_id __maybe_unused smsc_tbl
[] = {
359 { 0x0007c0a0, 0xfffffff0 },
360 { 0x0007c0b0, 0xfffffff0 },
361 { 0x0007c0c0, 0xfffffff0 },
362 { 0x0007c0d0, 0xfffffff0 },
363 { 0x0007c0f0, 0xfffffff0 },
364 { 0x0007c110, 0xfffffff0 },
368 MODULE_DEVICE_TABLE(mdio
, smsc_tbl
);