xref: /openbmc/linux/drivers/net/phy/smsc.c (revision 83c4a4ee)
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 	struct clk *refclk;
49 };
50 
51 static int smsc_phy_ack_interrupt(struct phy_device *phydev)
52 {
53 	int rc = phy_read(phydev, MII_LAN83C185_ISF);
54 
55 	return rc < 0 ? rc : 0;
56 }
57 
58 static int smsc_phy_config_intr(struct phy_device *phydev)
59 {
60 	struct smsc_phy_priv *priv = phydev->priv;
61 	u16 intmask = 0;
62 	int rc;
63 
64 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
65 		rc = smsc_phy_ack_interrupt(phydev);
66 		if (rc)
67 			return rc;
68 
69 		intmask = MII_LAN83C185_ISF_INT4 | MII_LAN83C185_ISF_INT6;
70 		if (priv->energy_enable)
71 			intmask |= MII_LAN83C185_ISF_INT7;
72 		rc = phy_write(phydev, MII_LAN83C185_IM, intmask);
73 	} else {
74 		rc = phy_write(phydev, MII_LAN83C185_IM, intmask);
75 		if (rc)
76 			return rc;
77 
78 		rc = smsc_phy_ack_interrupt(phydev);
79 	}
80 
81 	return rc < 0 ? rc : 0;
82 }
83 
84 static irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev)
85 {
86 	int irq_status, irq_enabled;
87 
88 	irq_enabled = phy_read(phydev, MII_LAN83C185_IM);
89 	if (irq_enabled < 0) {
90 		phy_error(phydev);
91 		return IRQ_NONE;
92 	}
93 
94 	irq_status = phy_read(phydev, MII_LAN83C185_ISF);
95 	if (irq_status < 0) {
96 		phy_error(phydev);
97 		return IRQ_NONE;
98 	}
99 
100 	if (!(irq_status & irq_enabled))
101 		return IRQ_NONE;
102 
103 	phy_trigger_machine(phydev);
104 
105 	return IRQ_HANDLED;
106 }
107 
108 static int smsc_phy_config_init(struct phy_device *phydev)
109 {
110 	struct smsc_phy_priv *priv = phydev->priv;
111 	int rc;
112 
113 	if (!priv->energy_enable)
114 		return 0;
115 
116 	rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
117 
118 	if (rc < 0)
119 		return rc;
120 
121 	/* Enable energy detect mode for this SMSC Transceivers */
122 	rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
123 		       rc | MII_LAN83C185_EDPWRDOWN);
124 	if (rc < 0)
125 		return rc;
126 
127 	return smsc_phy_ack_interrupt(phydev);
128 }
129 
130 static int smsc_phy_reset(struct phy_device *phydev)
131 {
132 	int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
133 	if (rc < 0)
134 		return rc;
135 
136 	/* If the SMSC PHY is in power down mode, then set it
137 	 * in all capable mode before using it.
138 	 */
139 	if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
140 		/* set "all capable" mode */
141 		rc |= MII_LAN83C185_MODE_ALL;
142 		phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
143 	}
144 
145 	/* reset the phy */
146 	return genphy_soft_reset(phydev);
147 }
148 
149 static int lan911x_config_init(struct phy_device *phydev)
150 {
151 	return smsc_phy_ack_interrupt(phydev);
152 }
153 
154 static int lan87xx_config_aneg(struct phy_device *phydev)
155 {
156 	int rc;
157 	int val;
158 
159 	switch (phydev->mdix_ctrl) {
160 	case ETH_TP_MDI:
161 		val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
162 		break;
163 	case ETH_TP_MDI_X:
164 		val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
165 			SPECIAL_CTRL_STS_AMDIX_STATE_;
166 		break;
167 	case ETH_TP_MDI_AUTO:
168 		val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
169 		break;
170 	default:
171 		return genphy_config_aneg(phydev);
172 	}
173 
174 	rc = phy_read(phydev, SPECIAL_CTRL_STS);
175 	if (rc < 0)
176 		return rc;
177 
178 	rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
179 		SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
180 		SPECIAL_CTRL_STS_AMDIX_STATE_);
181 	rc |= val;
182 	phy_write(phydev, SPECIAL_CTRL_STS, rc);
183 
184 	phydev->mdix = phydev->mdix_ctrl;
185 	return genphy_config_aneg(phydev);
186 }
187 
188 static int lan87xx_config_aneg_ext(struct phy_device *phydev)
189 {
190 	int rc;
191 
192 	/* Extend Manual AutoMDIX timer */
193 	rc = phy_read(phydev, PHY_EDPD_CONFIG);
194 	if (rc < 0)
195 		return rc;
196 
197 	rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
198 	phy_write(phydev, PHY_EDPD_CONFIG, rc);
199 	return lan87xx_config_aneg(phydev);
200 }
201 
202 /*
203  * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
204  * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
205  * unstable detection of plugging in Ethernet cable.
206  * This workaround disables Energy Detect Power-Down mode and waiting for
207  * response on link pulses to detect presence of plugged Ethernet cable.
208  * The Energy Detect Power-Down mode is enabled again in the end of procedure to
209  * save approximately 220 mW of power if cable is unplugged.
210  */
211 static int lan87xx_read_status(struct phy_device *phydev)
212 {
213 	struct smsc_phy_priv *priv = phydev->priv;
214 
215 	int err = genphy_read_status(phydev);
216 
217 	if (!phydev->link && priv->energy_enable) {
218 		/* Disable EDPD to wake up PHY */
219 		int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
220 		if (rc < 0)
221 			return rc;
222 
223 		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
224 			       rc & ~MII_LAN83C185_EDPWRDOWN);
225 		if (rc < 0)
226 			return rc;
227 
228 		/* Wait max 640 ms to detect energy and the timeout is not
229 		 * an actual error.
230 		 */
231 		read_poll_timeout(phy_read, rc,
232 				  rc & MII_LAN83C185_ENERGYON || rc < 0,
233 				  10000, 640000, true, phydev,
234 				  MII_LAN83C185_CTRL_STATUS);
235 		if (rc < 0)
236 			return rc;
237 
238 		/* Re-enable EDPD */
239 		rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
240 		if (rc < 0)
241 			return rc;
242 
243 		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
244 			       rc | MII_LAN83C185_EDPWRDOWN);
245 		if (rc < 0)
246 			return rc;
247 	}
248 
249 	return err;
250 }
251 
252 static int smsc_get_sset_count(struct phy_device *phydev)
253 {
254 	return ARRAY_SIZE(smsc_hw_stats);
255 }
256 
257 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
258 {
259 	int i;
260 
261 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
262 		strncpy(data + i * ETH_GSTRING_LEN,
263 		       smsc_hw_stats[i].string, ETH_GSTRING_LEN);
264 	}
265 }
266 
267 static u64 smsc_get_stat(struct phy_device *phydev, int i)
268 {
269 	struct smsc_hw_stat stat = smsc_hw_stats[i];
270 	int val;
271 	u64 ret;
272 
273 	val = phy_read(phydev, stat.reg);
274 	if (val < 0)
275 		ret = U64_MAX;
276 	else
277 		ret = val;
278 
279 	return ret;
280 }
281 
282 static void smsc_get_stats(struct phy_device *phydev,
283 			   struct ethtool_stats *stats, u64 *data)
284 {
285 	int i;
286 
287 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
288 		data[i] = smsc_get_stat(phydev, i);
289 }
290 
291 static void smsc_phy_remove(struct phy_device *phydev)
292 {
293 	struct smsc_phy_priv *priv = phydev->priv;
294 
295 	clk_disable_unprepare(priv->refclk);
296 	clk_put(priv->refclk);
297 }
298 
299 static int smsc_phy_probe(struct phy_device *phydev)
300 {
301 	struct device *dev = &phydev->mdio.dev;
302 	struct device_node *of_node = dev->of_node;
303 	struct smsc_phy_priv *priv;
304 	int ret;
305 
306 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
307 	if (!priv)
308 		return -ENOMEM;
309 
310 	priv->energy_enable = true;
311 
312 	if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
313 		priv->energy_enable = false;
314 
315 	phydev->priv = priv;
316 
317 	/* Make clk optional to keep DTB backward compatibility. */
318 	priv->refclk = clk_get_optional(dev, NULL);
319 	if (IS_ERR(priv->refclk))
320 		dev_err_probe(dev, PTR_ERR(priv->refclk), "Failed to request clock\n");
321 
322 	ret = clk_prepare_enable(priv->refclk);
323 	if (ret)
324 		return ret;
325 
326 	ret = clk_set_rate(priv->refclk, 50 * 1000 * 1000);
327 	if (ret) {
328 		clk_disable_unprepare(priv->refclk);
329 		return ret;
330 	}
331 
332 	return 0;
333 }
334 
335 static struct phy_driver smsc_phy_driver[] = {
336 {
337 	.phy_id		= 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
338 	.phy_id_mask	= 0xfffffff0,
339 	.name		= "SMSC LAN83C185",
340 
341 	/* PHY_BASIC_FEATURES */
342 
343 	.probe		= smsc_phy_probe,
344 
345 	/* basic functions */
346 	.config_init	= smsc_phy_config_init,
347 	.soft_reset	= smsc_phy_reset,
348 
349 	/* IRQ related */
350 	.config_intr	= smsc_phy_config_intr,
351 	.handle_interrupt = smsc_phy_handle_interrupt,
352 
353 	.suspend	= genphy_suspend,
354 	.resume		= genphy_resume,
355 }, {
356 	.phy_id		= 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
357 	.phy_id_mask	= 0xfffffff0,
358 	.name		= "SMSC LAN8187",
359 
360 	/* PHY_BASIC_FEATURES */
361 
362 	.probe		= smsc_phy_probe,
363 
364 	/* basic functions */
365 	.config_init	= smsc_phy_config_init,
366 	.soft_reset	= smsc_phy_reset,
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 	/* This covers internal PHY (phy_id: 0x0007C0C3) for
381 	 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
382 	 */
383 	.phy_id		= 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
384 	.phy_id_mask	= 0xfffffff0,
385 	.name		= "SMSC LAN8700",
386 
387 	/* PHY_BASIC_FEATURES */
388 
389 	.probe		= smsc_phy_probe,
390 
391 	/* basic functions */
392 	.read_status	= lan87xx_read_status,
393 	.config_init	= smsc_phy_config_init,
394 	.soft_reset	= smsc_phy_reset,
395 	.config_aneg	= lan87xx_config_aneg,
396 
397 	/* IRQ related */
398 	.config_intr	= smsc_phy_config_intr,
399 	.handle_interrupt = smsc_phy_handle_interrupt,
400 
401 	/* Statistics */
402 	.get_sset_count = smsc_get_sset_count,
403 	.get_strings	= smsc_get_strings,
404 	.get_stats	= smsc_get_stats,
405 
406 	.suspend	= genphy_suspend,
407 	.resume		= genphy_resume,
408 }, {
409 	.phy_id		= 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
410 	.phy_id_mask	= 0xfffffff0,
411 	.name		= "SMSC LAN911x Internal PHY",
412 
413 	/* PHY_BASIC_FEATURES */
414 
415 	.probe		= smsc_phy_probe,
416 
417 	/* basic functions */
418 	.config_init	= lan911x_config_init,
419 
420 	/* IRQ related */
421 	.config_intr	= smsc_phy_config_intr,
422 	.handle_interrupt = smsc_phy_handle_interrupt,
423 
424 	.suspend	= genphy_suspend,
425 	.resume		= genphy_resume,
426 }, {
427 	/* This covers internal PHY (phy_id: 0x0007C0F0) for
428 	 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
429 	 */
430 	.phy_id		= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
431 	.phy_id_mask	= 0xfffffff0,
432 	.name		= "SMSC LAN8710/LAN8720",
433 
434 	/* PHY_BASIC_FEATURES */
435 
436 	.probe		= smsc_phy_probe,
437 	.remove		= smsc_phy_remove,
438 
439 	/* basic functions */
440 	.read_status	= lan87xx_read_status,
441 	.config_init	= smsc_phy_config_init,
442 	.soft_reset	= smsc_phy_reset,
443 	.config_aneg	= lan87xx_config_aneg_ext,
444 
445 	/* IRQ related */
446 	.config_intr	= smsc_phy_config_intr,
447 	.handle_interrupt = smsc_phy_handle_interrupt,
448 
449 	/* Statistics */
450 	.get_sset_count = smsc_get_sset_count,
451 	.get_strings	= smsc_get_strings,
452 	.get_stats	= smsc_get_stats,
453 
454 	.suspend	= genphy_suspend,
455 	.resume		= genphy_resume,
456 }, {
457 	.phy_id		= 0x0007c110,
458 	.phy_id_mask	= 0xfffffff0,
459 	.name		= "SMSC LAN8740",
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 	{ }
498 };
499 
500 MODULE_DEVICE_TABLE(mdio, smsc_tbl);
501