xref: /openbmc/linux/drivers/net/phy/smsc.c (revision 8b305ee2)
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/crc16.h>
24 #include <linux/etherdevice.h>
25 #include <linux/smscphy.h>
26 
27 /* Vendor-specific PHY Definitions */
28 /* EDPD NLP / crossover time configuration */
29 #define PHY_EDPD_CONFIG			16
30 #define PHY_EDPD_CONFIG_EXT_CROSSOVER_	0x0001
31 
32 /* Control/Status Indication Register */
33 #define SPECIAL_CTRL_STS		27
34 #define SPECIAL_CTRL_STS_OVRRD_AMDIX_	0x8000
35 #define SPECIAL_CTRL_STS_AMDIX_ENABLE_	0x4000
36 #define SPECIAL_CTRL_STS_AMDIX_STATE_	0x2000
37 
38 #define EDPD_MAX_WAIT_DFLT_MS		640
39 /* interval between phylib state machine runs in ms */
40 #define PHY_STATE_MACH_MS		1000
41 
42 struct smsc_hw_stat {
43 	const char *string;
44 	u8 reg;
45 	u8 bits;
46 };
47 
48 static struct smsc_hw_stat smsc_hw_stats[] = {
49 	{ "phy_symbol_errors", 26, 16},
50 };
51 
52 struct smsc_phy_priv {
53 	unsigned int edpd_enable:1;
54 	unsigned int edpd_mode_set_by_user:1;
55 	unsigned int edpd_max_wait_ms;
56 	bool wol_arp;
57 };
58 
smsc_phy_ack_interrupt(struct phy_device * phydev)59 static int smsc_phy_ack_interrupt(struct phy_device *phydev)
60 {
61 	int rc = phy_read(phydev, MII_LAN83C185_ISF);
62 
63 	return rc < 0 ? rc : 0;
64 }
65 
smsc_phy_config_intr(struct phy_device * phydev)66 int smsc_phy_config_intr(struct phy_device *phydev)
67 {
68 	int rc;
69 
70 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
71 		rc = smsc_phy_ack_interrupt(phydev);
72 		if (rc)
73 			return rc;
74 
75 		rc = phy_write(phydev, MII_LAN83C185_IM,
76 			       MII_LAN83C185_ISF_INT_PHYLIB_EVENTS);
77 	} else {
78 		rc = phy_write(phydev, MII_LAN83C185_IM, 0);
79 		if (rc)
80 			return rc;
81 
82 		rc = smsc_phy_ack_interrupt(phydev);
83 	}
84 
85 	return rc < 0 ? rc : 0;
86 }
87 EXPORT_SYMBOL_GPL(smsc_phy_config_intr);
88 
smsc_phy_config_edpd(struct phy_device * phydev)89 static int smsc_phy_config_edpd(struct phy_device *phydev)
90 {
91 	struct smsc_phy_priv *priv = phydev->priv;
92 
93 	if (priv->edpd_enable)
94 		return phy_set_bits(phydev, MII_LAN83C185_CTRL_STATUS,
95 				    MII_LAN83C185_EDPWRDOWN);
96 	else
97 		return phy_clear_bits(phydev, MII_LAN83C185_CTRL_STATUS,
98 				      MII_LAN83C185_EDPWRDOWN);
99 }
100 
smsc_phy_handle_interrupt(struct phy_device * phydev)101 irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev)
102 {
103 	int irq_status;
104 
105 	irq_status = phy_read(phydev, MII_LAN83C185_ISF);
106 	if (irq_status < 0) {
107 		if (irq_status != -ENODEV)
108 			phy_error(phydev);
109 
110 		return IRQ_NONE;
111 	}
112 
113 	if (!(irq_status & MII_LAN83C185_ISF_INT_PHYLIB_EVENTS))
114 		return IRQ_NONE;
115 
116 	phy_trigger_machine(phydev);
117 
118 	return IRQ_HANDLED;
119 }
120 EXPORT_SYMBOL_GPL(smsc_phy_handle_interrupt);
121 
smsc_phy_config_init(struct phy_device * phydev)122 int smsc_phy_config_init(struct phy_device *phydev)
123 {
124 	struct smsc_phy_priv *priv = phydev->priv;
125 
126 	if (!priv)
127 		return 0;
128 
129 	/* don't use EDPD in irq mode except overridden by user */
130 	if (!priv->edpd_mode_set_by_user && phydev->irq != PHY_POLL)
131 		priv->edpd_enable = false;
132 
133 	return smsc_phy_config_edpd(phydev);
134 }
135 EXPORT_SYMBOL_GPL(smsc_phy_config_init);
136 
smsc_phy_reset(struct phy_device * phydev)137 static int smsc_phy_reset(struct phy_device *phydev)
138 {
139 	int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
140 	if (rc < 0)
141 		return rc;
142 
143 	/* If the SMSC PHY is in power down mode, then set it
144 	 * in all capable mode before using it.
145 	 */
146 	if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
147 		/* set "all capable" mode */
148 		rc |= MII_LAN83C185_MODE_ALL;
149 		phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
150 	}
151 
152 	/* reset the phy */
153 	return genphy_soft_reset(phydev);
154 }
155 
lan87xx_config_aneg(struct phy_device * phydev)156 static int lan87xx_config_aneg(struct phy_device *phydev)
157 {
158 	int rc;
159 	int val;
160 
161 	switch (phydev->mdix_ctrl) {
162 	case ETH_TP_MDI:
163 		val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
164 		break;
165 	case ETH_TP_MDI_X:
166 		val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
167 			SPECIAL_CTRL_STS_AMDIX_STATE_;
168 		break;
169 	case ETH_TP_MDI_AUTO:
170 		val = SPECIAL_CTRL_STS_AMDIX_ENABLE_;
171 		break;
172 	default:
173 		return genphy_config_aneg(phydev);
174 	}
175 
176 	rc = phy_read(phydev, SPECIAL_CTRL_STS);
177 	if (rc < 0)
178 		return rc;
179 
180 	rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
181 		SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
182 		SPECIAL_CTRL_STS_AMDIX_STATE_);
183 	rc |= val;
184 	phy_write(phydev, SPECIAL_CTRL_STS, rc);
185 
186 	phydev->mdix = phydev->mdix_ctrl;
187 	return genphy_config_aneg(phydev);
188 }
189 
lan95xx_config_aneg_ext(struct phy_device * phydev)190 static int lan95xx_config_aneg_ext(struct phy_device *phydev)
191 {
192 	if (phydev->phy_id == 0x0007c0f0) { /* LAN9500A or LAN9505A */
193 		/* Extend Manual AutoMDIX timer */
194 		int rc = phy_set_bits(phydev, PHY_EDPD_CONFIG,
195 				      PHY_EDPD_CONFIG_EXT_CROSSOVER_);
196 
197 		if (rc < 0)
198 			return rc;
199 	}
200 
201 	return lan87xx_config_aneg(phydev);
202 }
203 
204 /*
205  * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
206  * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
207  * unstable detection of plugging in Ethernet cable.
208  * This workaround disables Energy Detect Power-Down mode and waiting for
209  * response on link pulses to detect presence of plugged Ethernet cable.
210  * The Energy Detect Power-Down mode is enabled again in the end of procedure to
211  * save approximately 220 mW of power if cable is unplugged.
212  * The workaround is only applicable to poll mode. Energy Detect Power-Down may
213  * not be used in interrupt mode lest link change detection becomes unreliable.
214  */
lan87xx_read_status(struct phy_device * phydev)215 int lan87xx_read_status(struct phy_device *phydev)
216 {
217 	struct smsc_phy_priv *priv = phydev->priv;
218 	int err;
219 
220 	err = genphy_read_status(phydev);
221 	if (err)
222 		return err;
223 
224 	if (!phydev->link && priv && priv->edpd_enable &&
225 	    priv->edpd_max_wait_ms) {
226 		unsigned int max_wait = priv->edpd_max_wait_ms * 1000;
227 		int rc;
228 
229 		/* Disable EDPD to wake up PHY */
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 		/* Wait max 640 ms to detect energy and the timeout is not
240 		 * an actual error.
241 		 */
242 		read_poll_timeout(phy_read, rc,
243 				  rc & MII_LAN83C185_ENERGYON || rc < 0,
244 				  10000, max_wait, true, phydev,
245 				  MII_LAN83C185_CTRL_STATUS);
246 		if (rc < 0)
247 			return rc;
248 
249 		/* Re-enable EDPD */
250 		rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
251 		if (rc < 0)
252 			return rc;
253 
254 		rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
255 			       rc | MII_LAN83C185_EDPWRDOWN);
256 		if (rc < 0)
257 			return rc;
258 	}
259 
260 	return err;
261 }
262 EXPORT_SYMBOL_GPL(lan87xx_read_status);
263 
lan874x_phy_config_init(struct phy_device * phydev)264 static int lan874x_phy_config_init(struct phy_device *phydev)
265 {
266 	u16 val;
267 	int rc;
268 
269 	/* Setup LED2/nINT/nPME pin to function as nPME.  May need user option
270 	 * to use LED1/nINT/nPME.
271 	 */
272 	val = MII_LAN874X_PHY_PME2_SET;
273 
274 	/* The bits MII_LAN874X_PHY_WOL_PFDA_FR, MII_LAN874X_PHY_WOL_WUFR,
275 	 * MII_LAN874X_PHY_WOL_MPR, and MII_LAN874X_PHY_WOL_BCAST_FR need to
276 	 * be cleared to de-assert PME signal after a WoL event happens, but
277 	 * using PME auto clear gets around that.
278 	 */
279 	val |= MII_LAN874X_PHY_PME_SELF_CLEAR;
280 	rc = phy_write_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR,
281 			   val);
282 	if (rc < 0)
283 		return rc;
284 
285 	/* set nPME self clear delay time */
286 	rc = phy_write_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_MCFGR,
287 			   MII_LAN874X_PHY_PME_SELF_CLEAR_DELAY);
288 	if (rc < 0)
289 		return rc;
290 
291 	return smsc_phy_config_init(phydev);
292 }
293 
lan874x_get_wol(struct phy_device * phydev,struct ethtool_wolinfo * wol)294 static void lan874x_get_wol(struct phy_device *phydev,
295 			    struct ethtool_wolinfo *wol)
296 {
297 	struct smsc_phy_priv *priv = phydev->priv;
298 	int rc;
299 
300 	wol->supported = (WAKE_UCAST | WAKE_BCAST | WAKE_MAGIC |
301 			  WAKE_ARP | WAKE_MCAST);
302 	wol->wolopts = 0;
303 
304 	rc = phy_read_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR);
305 	if (rc < 0)
306 		return;
307 
308 	if (rc & MII_LAN874X_PHY_WOL_PFDAEN)
309 		wol->wolopts |= WAKE_UCAST;
310 
311 	if (rc & MII_LAN874X_PHY_WOL_BCSTEN)
312 		wol->wolopts |= WAKE_BCAST;
313 
314 	if (rc & MII_LAN874X_PHY_WOL_MPEN)
315 		wol->wolopts |= WAKE_MAGIC;
316 
317 	if (rc & MII_LAN874X_PHY_WOL_WUEN) {
318 		if (priv->wol_arp)
319 			wol->wolopts |= WAKE_ARP;
320 		else
321 			wol->wolopts |= WAKE_MCAST;
322 	}
323 }
324 
smsc_crc16(const u8 * buffer,size_t len)325 static u16 smsc_crc16(const u8 *buffer, size_t len)
326 {
327 	return bitrev16(crc16(0xFFFF, buffer, len));
328 }
329 
lan874x_chk_wol_pattern(const u8 pattern[],const u16 * mask,u8 len,u8 * data,u8 * datalen)330 static int lan874x_chk_wol_pattern(const u8 pattern[], const u16 *mask,
331 				   u8 len, u8 *data, u8 *datalen)
332 {
333 	size_t i, j, k;
334 	int ret = 0;
335 	u16 bits;
336 
337 	/* Pattern filtering can match up to 128 bytes of frame data.  There
338 	 * are 8 registers to program the 16-bit masks, where each bit means
339 	 * the byte will be compared.  The frame data will then go through a
340 	 * CRC16 calculation for hardware comparison.  This helper function
341 	 * makes sure only relevant frame data are included in this
342 	 * calculation.  It provides a warning when the masks and expected
343 	 * data size do not match.
344 	 */
345 	i = 0;
346 	k = 0;
347 	while (len > 0) {
348 		bits = *mask;
349 		for (j = 0; j < 16; j++, i++, len--) {
350 			/* No more pattern. */
351 			if (!len) {
352 				/* The rest of bitmap is not empty. */
353 				if (bits)
354 					ret = i + 1;
355 				break;
356 			}
357 			if (bits & 1)
358 				data[k++] = pattern[i];
359 			bits >>= 1;
360 		}
361 		mask++;
362 	}
363 	*datalen = k;
364 	return ret;
365 }
366 
lan874x_set_wol_pattern(struct phy_device * phydev,u16 val,const u8 data[],u8 datalen,const u16 * mask,u8 masklen)367 static int lan874x_set_wol_pattern(struct phy_device *phydev, u16 val,
368 				   const u8 data[], u8 datalen,
369 				   const u16 *mask, u8 masklen)
370 {
371 	u16 crc, reg;
372 	int rc;
373 
374 	/* Starting pattern offset is set before calling this function. */
375 	val |= MII_LAN874X_PHY_WOL_FILTER_EN;
376 	rc = phy_write_mmd(phydev, MDIO_MMD_PCS,
377 			   MII_LAN874X_PHY_MMD_WOL_WUF_CFGA, val);
378 	if (rc < 0)
379 		return rc;
380 
381 	crc = smsc_crc16(data, datalen);
382 	rc = phy_write_mmd(phydev, MDIO_MMD_PCS,
383 			   MII_LAN874X_PHY_MMD_WOL_WUF_CFGB, crc);
384 	if (rc < 0)
385 		return rc;
386 
387 	masklen = (masklen + 15) & ~0xf;
388 	reg = MII_LAN874X_PHY_MMD_WOL_WUF_MASK7;
389 	while (masklen >= 16) {
390 		rc = phy_write_mmd(phydev, MDIO_MMD_PCS, reg, *mask);
391 		if (rc < 0)
392 			return rc;
393 		reg--;
394 		mask++;
395 		masklen -= 16;
396 	}
397 
398 	/* Clear out the rest of mask registers. */
399 	while (reg != MII_LAN874X_PHY_MMD_WOL_WUF_MASK0) {
400 		phy_write_mmd(phydev, MDIO_MMD_PCS, reg, 0);
401 		reg--;
402 	}
403 	return rc;
404 }
405 
lan874x_set_wol(struct phy_device * phydev,struct ethtool_wolinfo * wol)406 static int lan874x_set_wol(struct phy_device *phydev,
407 			   struct ethtool_wolinfo *wol)
408 {
409 	struct net_device *ndev = phydev->attached_dev;
410 	struct smsc_phy_priv *priv = phydev->priv;
411 	u16 val, val_wucsr;
412 	u8 data[128];
413 	u8 datalen;
414 	int rc;
415 
416 	/* lan874x has only one WoL filter pattern */
417 	if ((wol->wolopts & (WAKE_ARP | WAKE_MCAST)) ==
418 	    (WAKE_ARP | WAKE_MCAST)) {
419 		phydev_info(phydev,
420 			    "lan874x WoL supports one of ARP|MCAST at a time\n");
421 		return -EOPNOTSUPP;
422 	}
423 
424 	rc = phy_read_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR);
425 	if (rc < 0)
426 		return rc;
427 
428 	val_wucsr = rc;
429 
430 	if (wol->wolopts & WAKE_UCAST)
431 		val_wucsr |= MII_LAN874X_PHY_WOL_PFDAEN;
432 	else
433 		val_wucsr &= ~MII_LAN874X_PHY_WOL_PFDAEN;
434 
435 	if (wol->wolopts & WAKE_BCAST)
436 		val_wucsr |= MII_LAN874X_PHY_WOL_BCSTEN;
437 	else
438 		val_wucsr &= ~MII_LAN874X_PHY_WOL_BCSTEN;
439 
440 	if (wol->wolopts & WAKE_MAGIC)
441 		val_wucsr |= MII_LAN874X_PHY_WOL_MPEN;
442 	else
443 		val_wucsr &= ~MII_LAN874X_PHY_WOL_MPEN;
444 
445 	/* Need to use pattern matching */
446 	if (wol->wolopts & (WAKE_ARP | WAKE_MCAST))
447 		val_wucsr |= MII_LAN874X_PHY_WOL_WUEN;
448 	else
449 		val_wucsr &= ~MII_LAN874X_PHY_WOL_WUEN;
450 
451 	if (wol->wolopts & WAKE_ARP) {
452 		const u8 pattern[2] = { 0x08, 0x06 };
453 		const u16 mask[1] = { 0x0003 };
454 
455 		rc = lan874x_chk_wol_pattern(pattern, mask, 2, data,
456 					     &datalen);
457 		if (rc)
458 			phydev_dbg(phydev, "pattern not valid at %d\n", rc);
459 
460 		/* Need to match broadcast destination address and provided
461 		 * data pattern at offset 12.
462 		 */
463 		val = 12 | MII_LAN874X_PHY_WOL_FILTER_BCSTEN;
464 		rc = lan874x_set_wol_pattern(phydev, val, data, datalen, mask,
465 					     2);
466 		if (rc < 0)
467 			return rc;
468 		priv->wol_arp = true;
469 	}
470 
471 	if (wol->wolopts & WAKE_MCAST) {
472 		/* Need to match multicast destination address. */
473 		val = MII_LAN874X_PHY_WOL_FILTER_MCASTTEN;
474 		rc = lan874x_set_wol_pattern(phydev, val, data, 0, NULL, 0);
475 		if (rc < 0)
476 			return rc;
477 		priv->wol_arp = false;
478 	}
479 
480 	if (wol->wolopts & (WAKE_MAGIC | WAKE_UCAST)) {
481 		const u8 *mac = (const u8 *)ndev->dev_addr;
482 		int i, reg;
483 
484 		reg = MII_LAN874X_PHY_MMD_WOL_RX_ADDRC;
485 		for (i = 0; i < 6; i += 2, reg--) {
486 			rc = phy_write_mmd(phydev, MDIO_MMD_PCS, reg,
487 					   ((mac[i + 1] << 8) | mac[i]));
488 			if (rc < 0)
489 				return rc;
490 		}
491 	}
492 
493 	rc = phy_write_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR,
494 			   val_wucsr);
495 	if (rc < 0)
496 		return rc;
497 
498 	return 0;
499 }
500 
smsc_get_sset_count(struct phy_device * phydev)501 static int smsc_get_sset_count(struct phy_device *phydev)
502 {
503 	return ARRAY_SIZE(smsc_hw_stats);
504 }
505 
smsc_get_strings(struct phy_device * phydev,u8 * data)506 static void smsc_get_strings(struct phy_device *phydev, u8 *data)
507 {
508 	int i;
509 
510 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
511 		strncpy(data + i * ETH_GSTRING_LEN,
512 		       smsc_hw_stats[i].string, ETH_GSTRING_LEN);
513 	}
514 }
515 
smsc_get_stat(struct phy_device * phydev,int i)516 static u64 smsc_get_stat(struct phy_device *phydev, int i)
517 {
518 	struct smsc_hw_stat stat = smsc_hw_stats[i];
519 	int val;
520 	u64 ret;
521 
522 	val = phy_read(phydev, stat.reg);
523 	if (val < 0)
524 		ret = U64_MAX;
525 	else
526 		ret = val;
527 
528 	return ret;
529 }
530 
smsc_get_stats(struct phy_device * phydev,struct ethtool_stats * stats,u64 * data)531 static void smsc_get_stats(struct phy_device *phydev,
532 			   struct ethtool_stats *stats, u64 *data)
533 {
534 	int i;
535 
536 	for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
537 		data[i] = smsc_get_stat(phydev, i);
538 }
539 
smsc_phy_get_edpd(struct phy_device * phydev,u16 * edpd)540 static int smsc_phy_get_edpd(struct phy_device *phydev, u16 *edpd)
541 {
542 	struct smsc_phy_priv *priv = phydev->priv;
543 
544 	if (!priv)
545 		return -EOPNOTSUPP;
546 
547 	if (!priv->edpd_enable)
548 		*edpd = ETHTOOL_PHY_EDPD_DISABLE;
549 	else if (!priv->edpd_max_wait_ms)
550 		*edpd = ETHTOOL_PHY_EDPD_NO_TX;
551 	else
552 		*edpd = PHY_STATE_MACH_MS + priv->edpd_max_wait_ms;
553 
554 	return 0;
555 }
556 
smsc_phy_set_edpd(struct phy_device * phydev,u16 edpd)557 static int smsc_phy_set_edpd(struct phy_device *phydev, u16 edpd)
558 {
559 	struct smsc_phy_priv *priv = phydev->priv;
560 
561 	if (!priv)
562 		return -EOPNOTSUPP;
563 
564 	switch (edpd) {
565 	case ETHTOOL_PHY_EDPD_DISABLE:
566 		priv->edpd_enable = false;
567 		break;
568 	case ETHTOOL_PHY_EDPD_NO_TX:
569 		priv->edpd_enable = true;
570 		priv->edpd_max_wait_ms = 0;
571 		break;
572 	case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS:
573 		edpd = PHY_STATE_MACH_MS + EDPD_MAX_WAIT_DFLT_MS;
574 		fallthrough;
575 	default:
576 		if (phydev->irq != PHY_POLL)
577 			return -EOPNOTSUPP;
578 		if (edpd < PHY_STATE_MACH_MS || edpd > PHY_STATE_MACH_MS + 1000)
579 			return -EINVAL;
580 		priv->edpd_enable = true;
581 		priv->edpd_max_wait_ms = edpd - PHY_STATE_MACH_MS;
582 	}
583 
584 	priv->edpd_mode_set_by_user = true;
585 
586 	return smsc_phy_config_edpd(phydev);
587 }
588 
smsc_phy_get_tunable(struct phy_device * phydev,struct ethtool_tunable * tuna,void * data)589 int smsc_phy_get_tunable(struct phy_device *phydev,
590 			 struct ethtool_tunable *tuna, void *data)
591 {
592 	switch (tuna->id) {
593 	case ETHTOOL_PHY_EDPD:
594 		return smsc_phy_get_edpd(phydev, data);
595 	default:
596 		return -EOPNOTSUPP;
597 	}
598 }
599 EXPORT_SYMBOL_GPL(smsc_phy_get_tunable);
600 
smsc_phy_set_tunable(struct phy_device * phydev,struct ethtool_tunable * tuna,const void * data)601 int smsc_phy_set_tunable(struct phy_device *phydev,
602 			 struct ethtool_tunable *tuna, const void *data)
603 {
604 	switch (tuna->id) {
605 	case ETHTOOL_PHY_EDPD:
606 		return smsc_phy_set_edpd(phydev, *(u16 *)data);
607 	default:
608 		return -EOPNOTSUPP;
609 	}
610 }
611 EXPORT_SYMBOL_GPL(smsc_phy_set_tunable);
612 
smsc_phy_probe(struct phy_device * phydev)613 int smsc_phy_probe(struct phy_device *phydev)
614 {
615 	struct device *dev = &phydev->mdio.dev;
616 	struct smsc_phy_priv *priv;
617 	struct clk *refclk;
618 
619 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
620 	if (!priv)
621 		return -ENOMEM;
622 
623 	priv->edpd_enable = true;
624 	priv->edpd_max_wait_ms = EDPD_MAX_WAIT_DFLT_MS;
625 
626 	if (device_property_present(dev, "smsc,disable-energy-detect"))
627 		priv->edpd_enable = false;
628 
629 	phydev->priv = priv;
630 
631 	/* Make clk optional to keep DTB backward compatibility. */
632 	refclk = devm_clk_get_optional_enabled(dev, NULL);
633 	if (IS_ERR(refclk))
634 		return dev_err_probe(dev, PTR_ERR(refclk),
635 				     "Failed to request clock\n");
636 
637 	return clk_set_rate(refclk, 50 * 1000 * 1000);
638 }
639 EXPORT_SYMBOL_GPL(smsc_phy_probe);
640 
641 static struct phy_driver smsc_phy_driver[] = {
642 {
643 	.phy_id		= 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
644 	.phy_id_mask	= 0xfffffff0,
645 	.name		= "SMSC LAN83C185",
646 
647 	/* PHY_BASIC_FEATURES */
648 
649 	.probe		= smsc_phy_probe,
650 
651 	/* basic functions */
652 	.config_init	= smsc_phy_config_init,
653 	.soft_reset	= smsc_phy_reset,
654 
655 	/* IRQ related */
656 	.config_intr	= smsc_phy_config_intr,
657 	.handle_interrupt = smsc_phy_handle_interrupt,
658 
659 	.suspend	= genphy_suspend,
660 	.resume		= genphy_resume,
661 }, {
662 	.phy_id		= 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
663 	.phy_id_mask	= 0xfffffff0,
664 	.name		= "SMSC LAN8187",
665 
666 	/* PHY_BASIC_FEATURES */
667 
668 	.probe		= smsc_phy_probe,
669 
670 	/* basic functions */
671 	.config_init	= smsc_phy_config_init,
672 	.soft_reset	= smsc_phy_reset,
673 
674 	/* IRQ related */
675 	.config_intr	= smsc_phy_config_intr,
676 	.handle_interrupt = smsc_phy_handle_interrupt,
677 
678 	/* Statistics */
679 	.get_sset_count = smsc_get_sset_count,
680 	.get_strings	= smsc_get_strings,
681 	.get_stats	= smsc_get_stats,
682 
683 	.suspend	= genphy_suspend,
684 	.resume		= genphy_resume,
685 }, {
686 	/* This covers internal PHY (phy_id: 0x0007C0C3) for
687 	 * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
688 	 */
689 	.phy_id		= 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
690 	.phy_id_mask	= 0xfffffff0,
691 	.name		= "SMSC LAN8700",
692 
693 	/* PHY_BASIC_FEATURES */
694 
695 	.probe		= smsc_phy_probe,
696 
697 	/* basic functions */
698 	.read_status	= lan87xx_read_status,
699 	.config_init	= smsc_phy_config_init,
700 	.soft_reset	= smsc_phy_reset,
701 	.config_aneg	= lan87xx_config_aneg,
702 
703 	/* IRQ related */
704 	.config_intr	= smsc_phy_config_intr,
705 	.handle_interrupt = smsc_phy_handle_interrupt,
706 
707 	/* Statistics */
708 	.get_sset_count = smsc_get_sset_count,
709 	.get_strings	= smsc_get_strings,
710 	.get_stats	= smsc_get_stats,
711 
712 	.get_tunable	= smsc_phy_get_tunable,
713 	.set_tunable	= smsc_phy_set_tunable,
714 
715 	.suspend	= genphy_suspend,
716 	.resume		= genphy_resume,
717 }, {
718 	.phy_id		= 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
719 	.phy_id_mask	= 0xfffffff0,
720 	.name		= "SMSC LAN911x Internal PHY",
721 
722 	/* PHY_BASIC_FEATURES */
723 
724 	.probe		= smsc_phy_probe,
725 
726 	/* IRQ related */
727 	.config_intr	= smsc_phy_config_intr,
728 	.handle_interrupt = smsc_phy_handle_interrupt,
729 
730 	.suspend	= genphy_suspend,
731 	.resume		= genphy_resume,
732 }, {
733 	/* This covers internal PHY (phy_id: 0x0007C0F0) for
734 	 * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
735 	 */
736 	.phy_id		= 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
737 	.phy_id_mask	= 0xfffffff0,
738 	.name		= "SMSC LAN8710/LAN8720",
739 
740 	/* PHY_BASIC_FEATURES */
741 
742 	.probe		= smsc_phy_probe,
743 
744 	/* basic functions */
745 	.read_status	= lan87xx_read_status,
746 	.config_init	= smsc_phy_config_init,
747 	.soft_reset	= smsc_phy_reset,
748 	.config_aneg	= lan95xx_config_aneg_ext,
749 
750 	/* IRQ related */
751 	.config_intr	= smsc_phy_config_intr,
752 	.handle_interrupt = smsc_phy_handle_interrupt,
753 
754 	/* Statistics */
755 	.get_sset_count = smsc_get_sset_count,
756 	.get_strings	= smsc_get_strings,
757 	.get_stats	= smsc_get_stats,
758 
759 	.get_tunable	= smsc_phy_get_tunable,
760 	.set_tunable	= smsc_phy_set_tunable,
761 
762 	.suspend	= genphy_suspend,
763 	.resume		= genphy_resume,
764 }, {
765 	.phy_id		= 0x0007c110,
766 	.phy_id_mask	= 0xfffffff0,
767 	.name		= "SMSC LAN8740",
768 
769 	/* PHY_BASIC_FEATURES */
770 	.flags		= PHY_RST_AFTER_CLK_EN,
771 
772 	.probe		= smsc_phy_probe,
773 
774 	/* basic functions */
775 	.read_status	= lan87xx_read_status,
776 	.config_init	= lan874x_phy_config_init,
777 	.soft_reset	= smsc_phy_reset,
778 
779 	/* IRQ related */
780 	.config_intr	= smsc_phy_config_intr,
781 	.handle_interrupt = smsc_phy_handle_interrupt,
782 
783 	/* Statistics */
784 	.get_sset_count = smsc_get_sset_count,
785 	.get_strings	= smsc_get_strings,
786 	.get_stats	= smsc_get_stats,
787 
788 	.get_tunable	= smsc_phy_get_tunable,
789 	.set_tunable	= smsc_phy_set_tunable,
790 
791 	/* WoL */
792 	.set_wol	= lan874x_set_wol,
793 	.get_wol	= lan874x_get_wol,
794 
795 	.suspend	= genphy_suspend,
796 	.resume		= genphy_resume,
797 }, {
798 	.phy_id		= 0x0007c130,	/* 0x0007c130 and 0x0007c131 */
799 	/* This mask (0xfffffff2) is to differentiate from
800 	 * LAN88xx (phy_id 0x0007c132)
801 	 * and allows future phy_id revisions.
802 	 */
803 	.phy_id_mask	= 0xfffffff2,
804 	.name		= "Microchip LAN8742",
805 
806 	/* PHY_BASIC_FEATURES */
807 	.flags		= PHY_RST_AFTER_CLK_EN,
808 
809 	.probe		= smsc_phy_probe,
810 
811 	/* basic functions */
812 	.read_status	= lan87xx_read_status,
813 	.config_init	= lan874x_phy_config_init,
814 	.soft_reset	= smsc_phy_reset,
815 
816 	/* IRQ related */
817 	.config_intr	= smsc_phy_config_intr,
818 	.handle_interrupt = smsc_phy_handle_interrupt,
819 
820 	/* Statistics */
821 	.get_sset_count = smsc_get_sset_count,
822 	.get_strings	= smsc_get_strings,
823 	.get_stats	= smsc_get_stats,
824 
825 	.get_tunable	= smsc_phy_get_tunable,
826 	.set_tunable	= smsc_phy_set_tunable,
827 
828 	/* WoL */
829 	.set_wol	= lan874x_set_wol,
830 	.get_wol	= lan874x_get_wol,
831 
832 	.suspend	= genphy_suspend,
833 	.resume		= genphy_resume,
834 } };
835 
836 module_phy_driver(smsc_phy_driver);
837 
838 MODULE_DESCRIPTION("SMSC PHY driver");
839 MODULE_AUTHOR("Herbert Valerio Riedel");
840 MODULE_LICENSE("GPL");
841 
842 static struct mdio_device_id __maybe_unused smsc_tbl[] = {
843 	{ 0x0007c0a0, 0xfffffff0 },
844 	{ 0x0007c0b0, 0xfffffff0 },
845 	{ 0x0007c0c0, 0xfffffff0 },
846 	{ 0x0007c0d0, 0xfffffff0 },
847 	{ 0x0007c0f0, 0xfffffff0 },
848 	{ 0x0007c110, 0xfffffff0 },
849 	{ 0x0007c130, 0xfffffff2 },
850 	{ }
851 };
852 
853 MODULE_DEVICE_TABLE(mdio, smsc_tbl);
854