xref: /kernel/linux/linux-5.10/drivers/net/phy/smsc.c (revision 8c2ecf20)
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
36struct smsc_hw_stat {
37	const char *string;
38	u8 reg;
39	u8 bits;
40};
41
42static struct smsc_hw_stat smsc_hw_stats[] = {
43	{ "phy_symbol_errors", 26, 16},
44};
45
46struct smsc_phy_priv {
47	bool energy_enable;
48	struct clk *refclk;
49};
50
51static 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
68static 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
75static 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
97static 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
116static int lan911x_config_init(struct phy_device *phydev)
117{
118	return smsc_phy_ack_interrupt(phydev);
119}
120
121static 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
155static int lan95xx_config_aneg_ext(struct phy_device *phydev)
156{
157	int rc;
158
159	if (phydev->phy_id != 0x0007c0f0) /* not (LAN9500A or LAN9505A) */
160		return lan87xx_config_aneg(phydev);
161
162	/* Extend Manual AutoMDIX timer */
163	rc = phy_read(phydev, PHY_EDPD_CONFIG);
164	if (rc < 0)
165		return rc;
166
167	rc |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
168	phy_write(phydev, PHY_EDPD_CONFIG, rc);
169	return lan87xx_config_aneg(phydev);
170}
171
172/*
173 * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
174 * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
175 * unstable detection of plugging in Ethernet cable.
176 * This workaround disables Energy Detect Power-Down mode and waiting for
177 * response on link pulses to detect presence of plugged Ethernet cable.
178 * The Energy Detect Power-Down mode is enabled again in the end of procedure to
179 * save approximately 220 mW of power if cable is unplugged.
180 */
181static int lan87xx_read_status(struct phy_device *phydev)
182{
183	struct smsc_phy_priv *priv = phydev->priv;
184	int err;
185
186	err = genphy_read_status(phydev);
187	if (err)
188		return err;
189
190	if (!phydev->link && priv->energy_enable) {
191		/* Disable EDPD to wake up PHY */
192		int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
193		if (rc < 0)
194			return rc;
195
196		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
197			       rc & ~MII_LAN83C185_EDPWRDOWN);
198		if (rc < 0)
199			return rc;
200
201		/* Wait max 640 ms to detect energy and the timeout is not
202		 * an actual error.
203		 */
204		read_poll_timeout(phy_read, rc,
205				  rc & MII_LAN83C185_ENERGYON || rc < 0,
206				  10000, 640000, true, phydev,
207				  MII_LAN83C185_CTRL_STATUS);
208		if (rc < 0)
209			return rc;
210
211		/* Re-enable EDPD */
212		rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
213		if (rc < 0)
214			return rc;
215
216		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
217			       rc | MII_LAN83C185_EDPWRDOWN);
218		if (rc < 0)
219			return rc;
220	}
221
222	return err;
223}
224
225static int smsc_get_sset_count(struct phy_device *phydev)
226{
227	return ARRAY_SIZE(smsc_hw_stats);
228}
229
230static void smsc_get_strings(struct phy_device *phydev, u8 *data)
231{
232	int i;
233
234	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
235		strncpy(data + i * ETH_GSTRING_LEN,
236		       smsc_hw_stats[i].string, ETH_GSTRING_LEN);
237	}
238}
239
240static u64 smsc_get_stat(struct phy_device *phydev, int i)
241{
242	struct smsc_hw_stat stat = smsc_hw_stats[i];
243	int val;
244	u64 ret;
245
246	val = phy_read(phydev, stat.reg);
247	if (val < 0)
248		ret = U64_MAX;
249	else
250		ret = val;
251
252	return ret;
253}
254
255static void smsc_get_stats(struct phy_device *phydev,
256			   struct ethtool_stats *stats, u64 *data)
257{
258	int i;
259
260	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
261		data[i] = smsc_get_stat(phydev, i);
262}
263
264static void smsc_phy_remove(struct phy_device *phydev)
265{
266	struct smsc_phy_priv *priv = phydev->priv;
267
268	clk_disable_unprepare(priv->refclk);
269	clk_put(priv->refclk);
270}
271
272static int smsc_phy_probe(struct phy_device *phydev)
273{
274	struct device *dev = &phydev->mdio.dev;
275	struct device_node *of_node = dev->of_node;
276	struct smsc_phy_priv *priv;
277	int ret;
278
279	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
280	if (!priv)
281		return -ENOMEM;
282
283	priv->energy_enable = true;
284
285	if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
286		priv->energy_enable = false;
287
288	phydev->priv = priv;
289
290	/* Make clk optional to keep DTB backward compatibility. */
291	priv->refclk = clk_get_optional(dev, NULL);
292	if (IS_ERR(priv->refclk))
293		return dev_err_probe(dev, PTR_ERR(priv->refclk),
294				     "Failed to request clock\n");
295
296	ret = clk_prepare_enable(priv->refclk);
297	if (ret)
298		return ret;
299
300	ret = clk_set_rate(priv->refclk, 50 * 1000 * 1000);
301	if (ret) {
302		clk_disable_unprepare(priv->refclk);
303		return ret;
304	}
305
306	return 0;
307}
308
309static 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	.ack_interrupt	= smsc_phy_ack_interrupt,
325	.config_intr	= smsc_phy_config_intr,
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	.ack_interrupt	= smsc_phy_ack_interrupt,
344	.config_intr	= smsc_phy_config_intr,
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	.ack_interrupt	= smsc_phy_ack_interrupt,
373	.config_intr	= smsc_phy_config_intr,
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	/* basic functions */
392	.config_init	= lan911x_config_init,
393
394	/* IRQ related */
395	.ack_interrupt	= smsc_phy_ack_interrupt,
396	.config_intr	= smsc_phy_config_intr,
397
398	.suspend	= genphy_suspend,
399	.resume		= genphy_resume,
400}, {
401	/* This covers internal PHY (phy_id: 0x0007C0F0) for
402	 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
403	 */
404	.phy_id		= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
405	.phy_id_mask	= 0xfffffff0,
406	.name		= "SMSC LAN8710/LAN8720",
407
408	/* PHY_BASIC_FEATURES */
409
410	.probe		= smsc_phy_probe,
411	.remove		= smsc_phy_remove,
412
413	/* basic functions */
414	.read_status	= lan87xx_read_status,
415	.config_init	= smsc_phy_config_init,
416	.soft_reset	= smsc_phy_reset,
417	.config_aneg	= lan95xx_config_aneg_ext,
418
419	/* IRQ related */
420	.ack_interrupt	= smsc_phy_ack_interrupt,
421	.config_intr	= smsc_phy_config_intr,
422
423	/* Statistics */
424	.get_sset_count = smsc_get_sset_count,
425	.get_strings	= smsc_get_strings,
426	.get_stats	= smsc_get_stats,
427
428	.suspend	= genphy_suspend,
429	.resume		= genphy_resume,
430}, {
431	.phy_id		= 0x0007c110,
432	.phy_id_mask	= 0xfffffff0,
433	.name		= "SMSC LAN8740",
434
435	/* PHY_BASIC_FEATURES */
436	.flags		= PHY_RST_AFTER_CLK_EN,
437
438	.probe		= smsc_phy_probe,
439
440	/* basic functions */
441	.read_status	= lan87xx_read_status,
442	.config_init	= smsc_phy_config_init,
443	.soft_reset	= smsc_phy_reset,
444
445	/* IRQ related */
446	.ack_interrupt	= smsc_phy_ack_interrupt,
447	.config_intr	= smsc_phy_config_intr,
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
458module_phy_driver(smsc_phy_driver);
459
460MODULE_DESCRIPTION("SMSC PHY driver");
461MODULE_AUTHOR("Herbert Valerio Riedel");
462MODULE_LICENSE("GPL");
463
464static struct mdio_device_id __maybe_unused smsc_tbl[] = {
465	{ 0x0007c0a0, 0xfffffff0 },
466	{ 0x0007c0b0, 0xfffffff0 },
467	{ 0x0007c0c0, 0xfffffff0 },
468	{ 0x0007c0d0, 0xfffffff0 },
469	{ 0x0007c0f0, 0xfffffff0 },
470	{ 0x0007c110, 0xfffffff0 },
471	{ }
472};
473
474MODULE_DEVICE_TABLE(mdio, smsc_tbl);
475