xref: /openbmc/linux/drivers/net/phy/smsc.c (revision e5acf9c8)
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 		clk_disable_unprepare(priv->refclk);
296 		return ret;
297 	}
298 
299 	return 0;
300 }
301 
302 static struct phy_driver smsc_phy_driver[] = {
303 {
304 	.phy_id		= 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
305 	.phy_id_mask	= 0xfffffff0,
306 	.name		= "SMSC LAN83C185",
307 
308 	/* PHY_BASIC_FEATURES */
309 
310 	.probe		= smsc_phy_probe,
311 
312 	/* basic functions */
313 	.config_init	= smsc_phy_config_init,
314 	.soft_reset	= smsc_phy_reset,
315 
316 	/* IRQ related */
317 	.ack_interrupt	= smsc_phy_ack_interrupt,
318 	.config_intr	= smsc_phy_config_intr,
319 
320 	.suspend	= genphy_suspend,
321 	.resume		= genphy_resume,
322 }, {
323 	.phy_id		= 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
324 	.phy_id_mask	= 0xfffffff0,
325 	.name		= "SMSC LAN8187",
326 
327 	/* PHY_BASIC_FEATURES */
328 
329 	.probe		= smsc_phy_probe,
330 
331 	/* basic functions */
332 	.config_init	= smsc_phy_config_init,
333 	.soft_reset	= smsc_phy_reset,
334 
335 	/* IRQ related */
336 	.ack_interrupt	= smsc_phy_ack_interrupt,
337 	.config_intr	= smsc_phy_config_intr,
338 
339 	/* Statistics */
340 	.get_sset_count = smsc_get_sset_count,
341 	.get_strings	= smsc_get_strings,
342 	.get_stats	= smsc_get_stats,
343 
344 	.suspend	= genphy_suspend,
345 	.resume		= genphy_resume,
346 }, {
347 	/* This covers internal PHY (phy_id: 0x0007C0C3) for
348 	 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
349 	 */
350 	.phy_id		= 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
351 	.phy_id_mask	= 0xfffffff0,
352 	.name		= "SMSC LAN8700",
353 
354 	/* PHY_BASIC_FEATURES */
355 
356 	.probe		= smsc_phy_probe,
357 
358 	/* basic functions */
359 	.read_status	= lan87xx_read_status,
360 	.config_init	= smsc_phy_config_init,
361 	.soft_reset	= smsc_phy_reset,
362 	.config_aneg	= lan87xx_config_aneg,
363 
364 	/* IRQ related */
365 	.ack_interrupt	= smsc_phy_ack_interrupt,
366 	.config_intr	= smsc_phy_config_intr,
367 
368 	/* Statistics */
369 	.get_sset_count = smsc_get_sset_count,
370 	.get_strings	= smsc_get_strings,
371 	.get_stats	= smsc_get_stats,
372 
373 	.suspend	= genphy_suspend,
374 	.resume		= genphy_resume,
375 }, {
376 	.phy_id		= 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
377 	.phy_id_mask	= 0xfffffff0,
378 	.name		= "SMSC LAN911x Internal PHY",
379 
380 	/* PHY_BASIC_FEATURES */
381 
382 	.probe		= smsc_phy_probe,
383 
384 	/* basic functions */
385 	.config_init	= lan911x_config_init,
386 
387 	/* IRQ related */
388 	.ack_interrupt	= smsc_phy_ack_interrupt,
389 	.config_intr	= smsc_phy_config_intr,
390 
391 	.suspend	= genphy_suspend,
392 	.resume		= genphy_resume,
393 }, {
394 	/* This covers internal PHY (phy_id: 0x0007C0F0) for
395 	 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
396 	 */
397 	.phy_id		= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
398 	.phy_id_mask	= 0xfffffff0,
399 	.name		= "SMSC LAN8710/LAN8720",
400 
401 	/* PHY_BASIC_FEATURES */
402 
403 	.probe		= smsc_phy_probe,
404 	.remove		= smsc_phy_remove,
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	= lan87xx_config_aneg_ext,
411 
412 	/* IRQ related */
413 	.ack_interrupt	= smsc_phy_ack_interrupt,
414 	.config_intr	= smsc_phy_config_intr,
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 	.ack_interrupt	= smsc_phy_ack_interrupt,
440 	.config_intr	= smsc_phy_config_intr,
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 
451 module_phy_driver(smsc_phy_driver);
452 
453 MODULE_DESCRIPTION("SMSC PHY driver");
454 MODULE_AUTHOR("Herbert Valerio Riedel");
455 MODULE_LICENSE("GPL");
456 
457 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
458 	{ 0x0007c0a0, 0xfffffff0 },
459 	{ 0x0007c0b0, 0xfffffff0 },
460 	{ 0x0007c0c0, 0xfffffff0 },
461 	{ 0x0007c0d0, 0xfffffff0 },
462 	{ 0x0007c0f0, 0xfffffff0 },
463 	{ 0x0007c110, 0xfffffff0 },
464 	{ }
465 };
466 
467 MODULE_DEVICE_TABLE(mdio, smsc_tbl);
468