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