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