xref: /linux/drivers/net/phy/smsc.c (revision d565263b7d83371e64ef315bdc3428909808b46f)
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 
193 	int err = genphy_read_status(phydev);
194 
195 	if (!phydev->link && priv->energy_enable && phydev->irq == PHY_POLL) {
196 		/* Disable EDPD to wake up PHY */
197 		int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
198 		if (rc < 0)
199 			return rc;
200 
201 		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
202 			       rc & ~MII_LAN83C185_EDPWRDOWN);
203 		if (rc < 0)
204 			return rc;
205 
206 		/* Wait max 640 ms to detect energy and the timeout is not
207 		 * an actual error.
208 		 */
209 		read_poll_timeout(phy_read, rc,
210 				  rc & MII_LAN83C185_ENERGYON || rc < 0,
211 				  10000, 640000, true, phydev,
212 				  MII_LAN83C185_CTRL_STATUS);
213 		if (rc < 0)
214 			return rc;
215 
216 		/* Re-enable EDPD */
217 		rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
218 		if (rc < 0)
219 			return rc;
220 
221 		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
222 			       rc | MII_LAN83C185_EDPWRDOWN);
223 		if (rc < 0)
224 			return rc;
225 	}
226 
227 	return err;
228 }
229 
230 static int smsc_get_sset_count(struct phy_device *phydev)
231 {
232 	return ARRAY_SIZE(smsc_hw_stats);
233 }
234 
235 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
236 {
237 	int i;
238 
239 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
240 		strncpy(data + i * ETH_GSTRING_LEN,
241 		       smsc_hw_stats[i].string, ETH_GSTRING_LEN);
242 	}
243 }
244 
245 static u64 smsc_get_stat(struct phy_device *phydev, int i)
246 {
247 	struct smsc_hw_stat stat = smsc_hw_stats[i];
248 	int val;
249 	u64 ret;
250 
251 	val = phy_read(phydev, stat.reg);
252 	if (val < 0)
253 		ret = U64_MAX;
254 	else
255 		ret = val;
256 
257 	return ret;
258 }
259 
260 static void smsc_get_stats(struct phy_device *phydev,
261 			   struct ethtool_stats *stats, u64 *data)
262 {
263 	int i;
264 
265 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
266 		data[i] = smsc_get_stat(phydev, i);
267 }
268 
269 static int smsc_phy_probe(struct phy_device *phydev)
270 {
271 	struct device *dev = &phydev->mdio.dev;
272 	struct smsc_phy_priv *priv;
273 	struct clk *refclk;
274 
275 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
276 	if (!priv)
277 		return -ENOMEM;
278 
279 	priv->energy_enable = true;
280 
281 	if (device_property_present(dev, "smsc,disable-energy-detect"))
282 		priv->energy_enable = false;
283 
284 	phydev->priv = priv;
285 
286 	/* Make clk optional to keep DTB backward compatibility. */
287 	refclk = devm_clk_get_optional_enabled(dev, NULL);
288 	if (IS_ERR(refclk))
289 		return dev_err_probe(dev, PTR_ERR(refclk),
290 				     "Failed to request clock\n");
291 
292 	return clk_set_rate(refclk, 50 * 1000 * 1000);
293 }
294 
295 static struct phy_driver smsc_phy_driver[] = {
296 {
297 	.phy_id		= 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
298 	.phy_id_mask	= 0xfffffff0,
299 	.name		= "SMSC LAN83C185",
300 
301 	/* PHY_BASIC_FEATURES */
302 
303 	.probe		= smsc_phy_probe,
304 
305 	/* basic functions */
306 	.config_init	= smsc_phy_config_init,
307 	.soft_reset	= smsc_phy_reset,
308 
309 	/* IRQ related */
310 	.config_intr	= smsc_phy_config_intr,
311 	.handle_interrupt = smsc_phy_handle_interrupt,
312 
313 	.suspend	= genphy_suspend,
314 	.resume		= genphy_resume,
315 }, {
316 	.phy_id		= 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
317 	.phy_id_mask	= 0xfffffff0,
318 	.name		= "SMSC LAN8187",
319 
320 	/* PHY_BASIC_FEATURES */
321 
322 	.probe		= smsc_phy_probe,
323 
324 	/* basic functions */
325 	.config_init	= smsc_phy_config_init,
326 	.soft_reset	= smsc_phy_reset,
327 
328 	/* IRQ related */
329 	.config_intr	= smsc_phy_config_intr,
330 	.handle_interrupt = smsc_phy_handle_interrupt,
331 
332 	/* Statistics */
333 	.get_sset_count = smsc_get_sset_count,
334 	.get_strings	= smsc_get_strings,
335 	.get_stats	= smsc_get_stats,
336 
337 	.suspend	= genphy_suspend,
338 	.resume		= genphy_resume,
339 }, {
340 	/* This covers internal PHY (phy_id: 0x0007C0C3) for
341 	 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
342 	 */
343 	.phy_id		= 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
344 	.phy_id_mask	= 0xfffffff0,
345 	.name		= "SMSC LAN8700",
346 
347 	/* PHY_BASIC_FEATURES */
348 
349 	.probe		= smsc_phy_probe,
350 
351 	/* basic functions */
352 	.read_status	= lan87xx_read_status,
353 	.config_init	= smsc_phy_config_init,
354 	.soft_reset	= smsc_phy_reset,
355 	.config_aneg	= lan87xx_config_aneg,
356 
357 	/* IRQ related */
358 	.config_intr	= smsc_phy_config_intr,
359 	.handle_interrupt = smsc_phy_handle_interrupt,
360 
361 	/* Statistics */
362 	.get_sset_count = smsc_get_sset_count,
363 	.get_strings	= smsc_get_strings,
364 	.get_stats	= smsc_get_stats,
365 
366 	.suspend	= genphy_suspend,
367 	.resume		= genphy_resume,
368 }, {
369 	.phy_id		= 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
370 	.phy_id_mask	= 0xfffffff0,
371 	.name		= "SMSC LAN911x Internal PHY",
372 
373 	/* PHY_BASIC_FEATURES */
374 
375 	.probe		= smsc_phy_probe,
376 
377 	/* IRQ related */
378 	.config_intr	= smsc_phy_config_intr,
379 	.handle_interrupt = smsc_phy_handle_interrupt,
380 
381 	.suspend	= genphy_suspend,
382 	.resume		= genphy_resume,
383 }, {
384 	/* This covers internal PHY (phy_id: 0x0007C0F0) for
385 	 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
386 	 */
387 	.phy_id		= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
388 	.phy_id_mask	= 0xfffffff0,
389 	.name		= "SMSC LAN8710/LAN8720",
390 
391 	/* PHY_BASIC_FEATURES */
392 
393 	.probe		= smsc_phy_probe,
394 
395 	/* basic functions */
396 	.read_status	= lan87xx_read_status,
397 	.config_init	= smsc_phy_config_init,
398 	.soft_reset	= smsc_phy_reset,
399 	.config_aneg	= lan95xx_config_aneg_ext,
400 
401 	/* IRQ related */
402 	.config_intr	= smsc_phy_config_intr,
403 	.handle_interrupt = smsc_phy_handle_interrupt,
404 
405 	/* Statistics */
406 	.get_sset_count = smsc_get_sset_count,
407 	.get_strings	= smsc_get_strings,
408 	.get_stats	= smsc_get_stats,
409 
410 	.suspend	= genphy_suspend,
411 	.resume		= genphy_resume,
412 }, {
413 	.phy_id		= 0x0007c110,
414 	.phy_id_mask	= 0xfffffff0,
415 	.name		= "SMSC LAN8740",
416 
417 	/* PHY_BASIC_FEATURES */
418 	.flags		= PHY_RST_AFTER_CLK_EN,
419 
420 	.probe		= smsc_phy_probe,
421 
422 	/* basic functions */
423 	.read_status	= lan87xx_read_status,
424 	.config_init	= smsc_phy_config_init,
425 	.soft_reset	= smsc_phy_reset,
426 
427 	/* IRQ related */
428 	.config_intr	= smsc_phy_config_intr,
429 	.handle_interrupt = smsc_phy_handle_interrupt,
430 
431 	/* Statistics */
432 	.get_sset_count = smsc_get_sset_count,
433 	.get_strings	= smsc_get_strings,
434 	.get_stats	= smsc_get_stats,
435 
436 	.suspend	= genphy_suspend,
437 	.resume		= genphy_resume,
438 }, {
439 	.phy_id		= 0x0007c130,	/* 0x0007c130 and 0x0007c131 */
440 	/* This mask (0xfffffff2) is to differentiate from
441 	 * LAN88xx (phy_id 0x0007c132)
442 	 * and allows future phy_id revisions.
443 	 */
444 	.phy_id_mask	= 0xfffffff2,
445 	.name		= "Microchip LAN8742",
446 
447 	/* PHY_BASIC_FEATURES */
448 	.flags		= PHY_RST_AFTER_CLK_EN,
449 
450 	.probe		= smsc_phy_probe,
451 
452 	/* basic functions */
453 	.read_status	= lan87xx_read_status,
454 	.config_init	= smsc_phy_config_init,
455 	.soft_reset	= smsc_phy_reset,
456 
457 	/* IRQ related */
458 	.config_intr	= smsc_phy_config_intr,
459 	.handle_interrupt = smsc_phy_handle_interrupt,
460 
461 	/* Statistics */
462 	.get_sset_count = smsc_get_sset_count,
463 	.get_strings	= smsc_get_strings,
464 	.get_stats	= smsc_get_stats,
465 
466 	.suspend	= genphy_suspend,
467 	.resume		= genphy_resume,
468 } };
469 
470 module_phy_driver(smsc_phy_driver);
471 
472 MODULE_DESCRIPTION("SMSC PHY driver");
473 MODULE_AUTHOR("Herbert Valerio Riedel");
474 MODULE_LICENSE("GPL");
475 
476 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
477 	{ 0x0007c0a0, 0xfffffff0 },
478 	{ 0x0007c0b0, 0xfffffff0 },
479 	{ 0x0007c0c0, 0xfffffff0 },
480 	{ 0x0007c0d0, 0xfffffff0 },
481 	{ 0x0007c0f0, 0xfffffff0 },
482 	{ 0x0007c110, 0xfffffff0 },
483 	{ 0x0007c130, 0xfffffff2 },
484 	{ }
485 };
486 
487 MODULE_DEVICE_TABLE(mdio, smsc_tbl);
488