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