xref: /linux/drivers/net/phy/smsc.c (revision fec53f44945877c8627da4d3ad70e3ac7e204f38)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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  *
11  * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
12  *
13  */
14 
15 #include <linux/clk.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mii.h>
19 #include <linux/ethtool.h>
20 #include <linux/of.h>
21 #include <linux/phy.h>
22 #include <linux/netdevice.h>
23 #include <linux/smscphy.h>
24 
25 /* Vendor-specific PHY Definitions */
26 /* EDPD NLP / crossover time configuration */
27 #define PHY_EDPD_CONFIG			16
28 #define PHY_EDPD_CONFIG_EXT_CROSSOVER_	0x0001
29 
30 /* Control/Status Indication Register */
31 #define SPECIAL_CTRL_STS		27
32 #define SPECIAL_CTRL_STS_OVRRD_AMDIX_	0x8000
33 #define SPECIAL_CTRL_STS_AMDIX_ENABLE_	0x4000
34 #define SPECIAL_CTRL_STS_AMDIX_STATE_	0x2000
35 
36 struct smsc_hw_stat {
37 	const char *string;
38 	u8 reg;
39 	u8 bits;
40 };
41 
42 static struct smsc_hw_stat smsc_hw_stats[] = {
43 	{ "phy_symbol_errors", 26, 16},
44 };
45 
46 struct smsc_phy_priv {
47 	bool energy_enable;
48 };
49 
50 static int smsc_phy_ack_interrupt(struct phy_device *phydev)
51 {
52 	int rc = phy_read(phydev, MII_LAN83C185_ISF);
53 
54 	return rc < 0 ? rc : 0;
55 }
56 
57 static int smsc_phy_config_intr(struct phy_device *phydev)
58 {
59 	int rc;
60 
61 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
62 		rc = smsc_phy_ack_interrupt(phydev);
63 		if (rc)
64 			return rc;
65 
66 		rc = phy_write(phydev, MII_LAN83C185_IM,
67 			       MII_LAN83C185_ISF_INT_PHYLIB_EVENTS);
68 	} else {
69 		rc = phy_write(phydev, MII_LAN83C185_IM, 0);
70 		if (rc)
71 			return rc;
72 
73 		rc = smsc_phy_ack_interrupt(phydev);
74 	}
75 
76 	return rc < 0 ? rc : 0;
77 }
78 
79 static irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev)
80 {
81 	int irq_status;
82 
83 	irq_status = phy_read(phydev, MII_LAN83C185_ISF);
84 	if (irq_status < 0) {
85 		if (irq_status != -ENODEV)
86 			phy_error(phydev);
87 
88 		return IRQ_NONE;
89 	}
90 
91 	if (!(irq_status & MII_LAN83C185_ISF_INT_PHYLIB_EVENTS))
92 		return IRQ_NONE;
93 
94 	phy_trigger_machine(phydev);
95 
96 	return IRQ_HANDLED;
97 }
98 
99 static int smsc_phy_config_init(struct phy_device *phydev)
100 {
101 	struct smsc_phy_priv *priv = phydev->priv;
102 
103 	if (!priv->energy_enable || phydev->irq != PHY_POLL)
104 		return 0;
105 
106 	/* Enable energy detect power down mode */
107 	return phy_set_bits(phydev, MII_LAN83C185_CTRL_STATUS,
108 			    MII_LAN83C185_EDPWRDOWN);
109 }
110 
111 static int smsc_phy_reset(struct phy_device *phydev)
112 {
113 	int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
114 	if (rc < 0)
115 		return rc;
116 
117 	/* If the SMSC PHY is in power down mode, then set it
118 	 * in all capable mode before using it.
119 	 */
120 	if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
121 		/* set "all capable" mode */
122 		rc |= MII_LAN83C185_MODE_ALL;
123 		phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
124 	}
125 
126 	/* reset the phy */
127 	return genphy_soft_reset(phydev);
128 }
129 
130 static int lan87xx_config_aneg(struct phy_device *phydev)
131 {
132 	int rc;
133 	int val;
134 
135 	switch (phydev->mdix_ctrl) {
136 	case ETH_TP_MDI:
137 		val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
138 		break;
139 	case ETH_TP_MDI_X:
140 		val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
141 			SPECIAL_CTRL_STS_AMDIX_STATE_;
142 		break;
143 	case ETH_TP_MDI_AUTO:
144 		val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
145 		break;
146 	default:
147 		return genphy_config_aneg(phydev);
148 	}
149 
150 	rc = phy_read(phydev, SPECIAL_CTRL_STS);
151 	if (rc < 0)
152 		return rc;
153 
154 	rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
155 		SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
156 		SPECIAL_CTRL_STS_AMDIX_STATE_);
157 	rc |= val;
158 	phy_write(phydev, SPECIAL_CTRL_STS, rc);
159 
160 	phydev->mdix = phydev->mdix_ctrl;
161 	return genphy_config_aneg(phydev);
162 }
163 
164 static int lan95xx_config_aneg_ext(struct phy_device *phydev)
165 {
166 	if (phydev->phy_id == 0x0007c0f0) { /* LAN9500A or LAN9505A */
167 		/* Extend Manual AutoMDIX timer */
168 		int rc = phy_set_bits(phydev, PHY_EDPD_CONFIG,
169 				      PHY_EDPD_CONFIG_EXT_CROSSOVER_);
170 
171 		if (rc < 0)
172 			return rc;
173 	}
174 
175 	return lan87xx_config_aneg(phydev);
176 }
177 
178 /*
179  * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
180  * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
181  * unstable detection of plugging in Ethernet cable.
182  * This workaround disables Energy Detect Power-Down mode and waiting for
183  * response on link pulses to detect presence of plugged Ethernet cable.
184  * The Energy Detect Power-Down mode is enabled again in the end of procedure to
185  * save approximately 220 mW of power if cable is unplugged.
186  * The workaround is only applicable to poll mode. Energy Detect Power-Down may
187  * not be used in interrupt mode lest link change detection becomes unreliable.
188  */
189 static int lan87xx_read_status(struct phy_device *phydev)
190 {
191 	struct smsc_phy_priv *priv = phydev->priv;
192 	int err;
193 
194 	err = genphy_read_status(phydev);
195 	if (err)
196 		return err;
197 
198 	if (!phydev->link && priv->energy_enable && phydev->irq == PHY_POLL) {
199 		/* Disable EDPD to wake up PHY */
200 		int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
201 		if (rc < 0)
202 			return rc;
203 
204 		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
205 			       rc & ~MII_LAN83C185_EDPWRDOWN);
206 		if (rc < 0)
207 			return rc;
208 
209 		/* Wait max 640 ms to detect energy and the timeout is not
210 		 * an actual error.
211 		 */
212 		read_poll_timeout(phy_read, rc,
213 				  rc & MII_LAN83C185_ENERGYON || rc < 0,
214 				  10000, 640000, true, phydev,
215 				  MII_LAN83C185_CTRL_STATUS);
216 		if (rc < 0)
217 			return rc;
218 
219 		/* Re-enable EDPD */
220 		rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
221 		if (rc < 0)
222 			return rc;
223 
224 		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
225 			       rc | MII_LAN83C185_EDPWRDOWN);
226 		if (rc < 0)
227 			return rc;
228 	}
229 
230 	return err;
231 }
232 
233 static int smsc_get_sset_count(struct phy_device *phydev)
234 {
235 	return ARRAY_SIZE(smsc_hw_stats);
236 }
237 
238 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
239 {
240 	int i;
241 
242 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
243 		strncpy(data + i * ETH_GSTRING_LEN,
244 		       smsc_hw_stats[i].string, ETH_GSTRING_LEN);
245 	}
246 }
247 
248 static u64 smsc_get_stat(struct phy_device *phydev, int i)
249 {
250 	struct smsc_hw_stat stat = smsc_hw_stats[i];
251 	int val;
252 	u64 ret;
253 
254 	val = phy_read(phydev, stat.reg);
255 	if (val < 0)
256 		ret = U64_MAX;
257 	else
258 		ret = val;
259 
260 	return ret;
261 }
262 
263 static void smsc_get_stats(struct phy_device *phydev,
264 			   struct ethtool_stats *stats, u64 *data)
265 {
266 	int i;
267 
268 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
269 		data[i] = smsc_get_stat(phydev, i);
270 }
271 
272 static int smsc_phy_probe(struct phy_device *phydev)
273 {
274 	struct device *dev = &phydev->mdio.dev;
275 	struct smsc_phy_priv *priv;
276 	struct clk *refclk;
277 
278 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
279 	if (!priv)
280 		return -ENOMEM;
281 
282 	priv->energy_enable = true;
283 
284 	if (device_property_present(dev, "smsc,disable-energy-detect"))
285 		priv->energy_enable = false;
286 
287 	phydev->priv = priv;
288 
289 	/* Make clk optional to keep DTB backward compatibility. */
290 	refclk = devm_clk_get_optional_enabled(dev, NULL);
291 	if (IS_ERR(refclk))
292 		return dev_err_probe(dev, PTR_ERR(refclk),
293 				     "Failed to request clock\n");
294 
295 	return clk_set_rate(refclk, 50 * 1000 * 1000);
296 }
297 
298 static struct phy_driver smsc_phy_driver[] = {
299 {
300 	.phy_id		= 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
301 	.phy_id_mask	= 0xfffffff0,
302 	.name		= "SMSC LAN83C185",
303 
304 	/* PHY_BASIC_FEATURES */
305 
306 	.probe		= smsc_phy_probe,
307 
308 	/* basic functions */
309 	.config_init	= smsc_phy_config_init,
310 	.soft_reset	= smsc_phy_reset,
311 
312 	/* IRQ related */
313 	.config_intr	= smsc_phy_config_intr,
314 	.handle_interrupt = smsc_phy_handle_interrupt,
315 
316 	.suspend	= genphy_suspend,
317 	.resume		= genphy_resume,
318 }, {
319 	.phy_id		= 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
320 	.phy_id_mask	= 0xfffffff0,
321 	.name		= "SMSC LAN8187",
322 
323 	/* PHY_BASIC_FEATURES */
324 
325 	.probe		= smsc_phy_probe,
326 
327 	/* basic functions */
328 	.config_init	= smsc_phy_config_init,
329 	.soft_reset	= smsc_phy_reset,
330 
331 	/* IRQ related */
332 	.config_intr	= smsc_phy_config_intr,
333 	.handle_interrupt = smsc_phy_handle_interrupt,
334 
335 	/* Statistics */
336 	.get_sset_count = smsc_get_sset_count,
337 	.get_strings	= smsc_get_strings,
338 	.get_stats	= smsc_get_stats,
339 
340 	.suspend	= genphy_suspend,
341 	.resume		= genphy_resume,
342 }, {
343 	/* This covers internal PHY (phy_id: 0x0007C0C3) for
344 	 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
345 	 */
346 	.phy_id		= 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
347 	.phy_id_mask	= 0xfffffff0,
348 	.name		= "SMSC LAN8700",
349 
350 	/* PHY_BASIC_FEATURES */
351 
352 	.probe		= smsc_phy_probe,
353 
354 	/* basic functions */
355 	.read_status	= lan87xx_read_status,
356 	.config_init	= smsc_phy_config_init,
357 	.soft_reset	= smsc_phy_reset,
358 	.config_aneg	= lan87xx_config_aneg,
359 
360 	/* IRQ related */
361 	.config_intr	= smsc_phy_config_intr,
362 	.handle_interrupt = smsc_phy_handle_interrupt,
363 
364 	/* Statistics */
365 	.get_sset_count = smsc_get_sset_count,
366 	.get_strings	= smsc_get_strings,
367 	.get_stats	= smsc_get_stats,
368 
369 	.suspend	= genphy_suspend,
370 	.resume		= genphy_resume,
371 }, {
372 	.phy_id		= 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
373 	.phy_id_mask	= 0xfffffff0,
374 	.name		= "SMSC LAN911x Internal PHY",
375 
376 	/* PHY_BASIC_FEATURES */
377 
378 	.probe		= smsc_phy_probe,
379 
380 	/* IRQ related */
381 	.config_intr	= smsc_phy_config_intr,
382 	.handle_interrupt = smsc_phy_handle_interrupt,
383 
384 	.suspend	= genphy_suspend,
385 	.resume		= genphy_resume,
386 }, {
387 	/* This covers internal PHY (phy_id: 0x0007C0F0) for
388 	 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
389 	 */
390 	.phy_id		= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
391 	.phy_id_mask	= 0xfffffff0,
392 	.name		= "SMSC LAN8710/LAN8720",
393 
394 	/* PHY_BASIC_FEATURES */
395 
396 	.probe		= smsc_phy_probe,
397 
398 	/* basic functions */
399 	.read_status	= lan87xx_read_status,
400 	.config_init	= smsc_phy_config_init,
401 	.soft_reset	= smsc_phy_reset,
402 	.config_aneg	= lan95xx_config_aneg_ext,
403 
404 	/* IRQ related */
405 	.config_intr	= smsc_phy_config_intr,
406 	.handle_interrupt = smsc_phy_handle_interrupt,
407 
408 	/* Statistics */
409 	.get_sset_count = smsc_get_sset_count,
410 	.get_strings	= smsc_get_strings,
411 	.get_stats	= smsc_get_stats,
412 
413 	.suspend	= genphy_suspend,
414 	.resume		= genphy_resume,
415 }, {
416 	.phy_id		= 0x0007c110,
417 	.phy_id_mask	= 0xfffffff0,
418 	.name		= "SMSC LAN8740",
419 
420 	/* PHY_BASIC_FEATURES */
421 	.flags		= PHY_RST_AFTER_CLK_EN,
422 
423 	.probe		= smsc_phy_probe,
424 
425 	/* basic functions */
426 	.read_status	= lan87xx_read_status,
427 	.config_init	= smsc_phy_config_init,
428 	.soft_reset	= smsc_phy_reset,
429 
430 	/* IRQ related */
431 	.config_intr	= smsc_phy_config_intr,
432 	.handle_interrupt = smsc_phy_handle_interrupt,
433 
434 	/* Statistics */
435 	.get_sset_count = smsc_get_sset_count,
436 	.get_strings	= smsc_get_strings,
437 	.get_stats	= smsc_get_stats,
438 
439 	.suspend	= genphy_suspend,
440 	.resume		= genphy_resume,
441 }, {
442 	.phy_id		= 0x0007c130,	/* 0x0007c130 and 0x0007c131 */
443 	/* This mask (0xfffffff2) is to differentiate from
444 	 * LAN88xx (phy_id 0x0007c132)
445 	 * and allows future phy_id revisions.
446 	 */
447 	.phy_id_mask	= 0xfffffff2,
448 	.name		= "Microchip LAN8742",
449 
450 	/* PHY_BASIC_FEATURES */
451 	.flags		= PHY_RST_AFTER_CLK_EN,
452 
453 	.probe		= smsc_phy_probe,
454 
455 	/* basic functions */
456 	.read_status	= lan87xx_read_status,
457 	.config_init	= smsc_phy_config_init,
458 	.soft_reset	= smsc_phy_reset,
459 
460 	/* IRQ related */
461 	.config_intr	= smsc_phy_config_intr,
462 	.handle_interrupt = smsc_phy_handle_interrupt,
463 
464 	/* Statistics */
465 	.get_sset_count = smsc_get_sset_count,
466 	.get_strings	= smsc_get_strings,
467 	.get_stats	= smsc_get_stats,
468 
469 	.suspend	= genphy_suspend,
470 	.resume		= genphy_resume,
471 } };
472 
473 module_phy_driver(smsc_phy_driver);
474 
475 MODULE_DESCRIPTION("SMSC PHY driver");
476 MODULE_AUTHOR("Herbert Valerio Riedel");
477 MODULE_LICENSE("GPL");
478 
479 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
480 	{ 0x0007c0a0, 0xfffffff0 },
481 	{ 0x0007c0b0, 0xfffffff0 },
482 	{ 0x0007c0c0, 0xfffffff0 },
483 	{ 0x0007c0d0, 0xfffffff0 },
484 	{ 0x0007c0f0, 0xfffffff0 },
485 	{ 0x0007c110, 0xfffffff0 },
486 	{ 0x0007c130, 0xfffffff2 },
487 	{ }
488 };
489 
490 MODULE_DEVICE_TABLE(mdio, smsc_tbl);
491