xref: /openbmc/linux/drivers/net/phy/mscc/mscc_main.c (revision 81464192839de0b5bc84c5739381101e04d94f62)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3  * Driver for Microsemi VSC85xx PHYs
4  *
5  * Author: Nagaraju Lakkaraju
6  * License: Dual MIT/GPL
7  * Copyright (c) 2016 Microsemi Corporation
8  */
9 
10 #include <linux/firmware.h>
11 #include <linux/jiffies.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mdio.h>
15 #include <linux/mii.h>
16 #include <linux/phy.h>
17 #include <linux/of.h>
18 #include <linux/netdevice.h>
19 #include <dt-bindings/net/mscc-phy-vsc8531.h>
20 
21 #include "mscc.h"
22 
23 static const struct vsc85xx_hw_stat vsc85xx_hw_stats[] = {
24 	{
25 		.string	= "phy_receive_errors",
26 		.reg	= MSCC_PHY_ERR_RX_CNT,
27 		.page	= MSCC_PHY_PAGE_STANDARD,
28 		.mask	= ERR_CNT_MASK,
29 	}, {
30 		.string	= "phy_false_carrier",
31 		.reg	= MSCC_PHY_ERR_FALSE_CARRIER_CNT,
32 		.page	= MSCC_PHY_PAGE_STANDARD,
33 		.mask	= ERR_CNT_MASK,
34 	}, {
35 		.string	= "phy_cu_media_link_disconnect",
36 		.reg	= MSCC_PHY_ERR_LINK_DISCONNECT_CNT,
37 		.page	= MSCC_PHY_PAGE_STANDARD,
38 		.mask	= ERR_CNT_MASK,
39 	}, {
40 		.string	= "phy_cu_media_crc_good_count",
41 		.reg	= MSCC_PHY_CU_MEDIA_CRC_VALID_CNT,
42 		.page	= MSCC_PHY_PAGE_EXTENDED,
43 		.mask	= VALID_CRC_CNT_CRC_MASK,
44 	}, {
45 		.string	= "phy_cu_media_crc_error_count",
46 		.reg	= MSCC_PHY_EXT_PHY_CNTL_4,
47 		.page	= MSCC_PHY_PAGE_EXTENDED,
48 		.mask	= ERR_CNT_MASK,
49 	},
50 };
51 
52 static const struct vsc85xx_hw_stat vsc8584_hw_stats[] = {
53 	{
54 		.string	= "phy_receive_errors",
55 		.reg	= MSCC_PHY_ERR_RX_CNT,
56 		.page	= MSCC_PHY_PAGE_STANDARD,
57 		.mask	= ERR_CNT_MASK,
58 	}, {
59 		.string	= "phy_false_carrier",
60 		.reg	= MSCC_PHY_ERR_FALSE_CARRIER_CNT,
61 		.page	= MSCC_PHY_PAGE_STANDARD,
62 		.mask	= ERR_CNT_MASK,
63 	}, {
64 		.string	= "phy_cu_media_link_disconnect",
65 		.reg	= MSCC_PHY_ERR_LINK_DISCONNECT_CNT,
66 		.page	= MSCC_PHY_PAGE_STANDARD,
67 		.mask	= ERR_CNT_MASK,
68 	}, {
69 		.string	= "phy_cu_media_crc_good_count",
70 		.reg	= MSCC_PHY_CU_MEDIA_CRC_VALID_CNT,
71 		.page	= MSCC_PHY_PAGE_EXTENDED,
72 		.mask	= VALID_CRC_CNT_CRC_MASK,
73 	}, {
74 		.string	= "phy_cu_media_crc_error_count",
75 		.reg	= MSCC_PHY_EXT_PHY_CNTL_4,
76 		.page	= MSCC_PHY_PAGE_EXTENDED,
77 		.mask	= ERR_CNT_MASK,
78 	}, {
79 		.string	= "phy_serdes_tx_good_pkt_count",
80 		.reg	= MSCC_PHY_SERDES_TX_VALID_CNT,
81 		.page	= MSCC_PHY_PAGE_EXTENDED_3,
82 		.mask	= VALID_CRC_CNT_CRC_MASK,
83 	}, {
84 		.string	= "phy_serdes_tx_bad_crc_count",
85 		.reg	= MSCC_PHY_SERDES_TX_CRC_ERR_CNT,
86 		.page	= MSCC_PHY_PAGE_EXTENDED_3,
87 		.mask	= ERR_CNT_MASK,
88 	}, {
89 		.string	= "phy_serdes_rx_good_pkt_count",
90 		.reg	= MSCC_PHY_SERDES_RX_VALID_CNT,
91 		.page	= MSCC_PHY_PAGE_EXTENDED_3,
92 		.mask	= VALID_CRC_CNT_CRC_MASK,
93 	}, {
94 		.string	= "phy_serdes_rx_bad_crc_count",
95 		.reg	= MSCC_PHY_SERDES_RX_CRC_ERR_CNT,
96 		.page	= MSCC_PHY_PAGE_EXTENDED_3,
97 		.mask	= ERR_CNT_MASK,
98 	},
99 };
100 
101 #if IS_ENABLED(CONFIG_OF_MDIO)
102 static const struct vsc8531_edge_rate_table edge_table[] = {
103 	{MSCC_VDDMAC_3300, { 0, 2,  4,  7, 10, 17, 29, 53} },
104 	{MSCC_VDDMAC_2500, { 0, 3,  6, 10, 14, 23, 37, 63} },
105 	{MSCC_VDDMAC_1800, { 0, 5,  9, 16, 23, 35, 52, 76} },
106 	{MSCC_VDDMAC_1500, { 0, 6, 14, 21, 29, 42, 58, 77} },
107 };
108 #endif
109 
110 static int vsc85xx_phy_read_page(struct phy_device *phydev)
111 {
112 	return __phy_read(phydev, MSCC_EXT_PAGE_ACCESS);
113 }
114 
115 static int vsc85xx_phy_write_page(struct phy_device *phydev, int page)
116 {
117 	return __phy_write(phydev, MSCC_EXT_PAGE_ACCESS, page);
118 }
119 
120 static int vsc85xx_get_sset_count(struct phy_device *phydev)
121 {
122 	struct vsc8531_private *priv = phydev->priv;
123 
124 	if (!priv)
125 		return 0;
126 
127 	return priv->nstats;
128 }
129 
130 static void vsc85xx_get_strings(struct phy_device *phydev, u8 *data)
131 {
132 	struct vsc8531_private *priv = phydev->priv;
133 	int i;
134 
135 	if (!priv)
136 		return;
137 
138 	for (i = 0; i < priv->nstats; i++)
139 		strlcpy(data + i * ETH_GSTRING_LEN, priv->hw_stats[i].string,
140 			ETH_GSTRING_LEN);
141 }
142 
143 static u64 vsc85xx_get_stat(struct phy_device *phydev, int i)
144 {
145 	struct vsc8531_private *priv = phydev->priv;
146 	int val;
147 
148 	val = phy_read_paged(phydev, priv->hw_stats[i].page,
149 			     priv->hw_stats[i].reg);
150 	if (val < 0)
151 		return U64_MAX;
152 
153 	val = val & priv->hw_stats[i].mask;
154 	priv->stats[i] += val;
155 
156 	return priv->stats[i];
157 }
158 
159 static void vsc85xx_get_stats(struct phy_device *phydev,
160 			      struct ethtool_stats *stats, u64 *data)
161 {
162 	struct vsc8531_private *priv = phydev->priv;
163 	int i;
164 
165 	if (!priv)
166 		return;
167 
168 	for (i = 0; i < priv->nstats; i++)
169 		data[i] = vsc85xx_get_stat(phydev, i);
170 }
171 
172 static int vsc85xx_led_cntl_set(struct phy_device *phydev,
173 				u8 led_num,
174 				u8 mode)
175 {
176 	int rc;
177 	u16 reg_val;
178 
179 	mutex_lock(&phydev->lock);
180 	reg_val = phy_read(phydev, MSCC_PHY_LED_MODE_SEL);
181 	reg_val &= ~LED_MODE_SEL_MASK(led_num);
182 	reg_val |= LED_MODE_SEL(led_num, (u16)mode);
183 	rc = phy_write(phydev, MSCC_PHY_LED_MODE_SEL, reg_val);
184 	mutex_unlock(&phydev->lock);
185 
186 	return rc;
187 }
188 
189 static int vsc85xx_mdix_get(struct phy_device *phydev, u8 *mdix)
190 {
191 	u16 reg_val;
192 
193 	reg_val = phy_read(phydev, MSCC_PHY_DEV_AUX_CNTL);
194 	if (reg_val & HP_AUTO_MDIX_X_OVER_IND_MASK)
195 		*mdix = ETH_TP_MDI_X;
196 	else
197 		*mdix = ETH_TP_MDI;
198 
199 	return 0;
200 }
201 
202 static int vsc85xx_mdix_set(struct phy_device *phydev, u8 mdix)
203 {
204 	int rc;
205 	u16 reg_val;
206 
207 	reg_val = phy_read(phydev, MSCC_PHY_BYPASS_CONTROL);
208 	if (mdix == ETH_TP_MDI || mdix == ETH_TP_MDI_X) {
209 		reg_val |= (DISABLE_PAIR_SWAP_CORR_MASK |
210 			    DISABLE_POLARITY_CORR_MASK  |
211 			    DISABLE_HP_AUTO_MDIX_MASK);
212 	} else {
213 		reg_val &= ~(DISABLE_PAIR_SWAP_CORR_MASK |
214 			     DISABLE_POLARITY_CORR_MASK  |
215 			     DISABLE_HP_AUTO_MDIX_MASK);
216 	}
217 	rc = phy_write(phydev, MSCC_PHY_BYPASS_CONTROL, reg_val);
218 	if (rc)
219 		return rc;
220 
221 	reg_val = 0;
222 
223 	if (mdix == ETH_TP_MDI)
224 		reg_val = FORCE_MDI_CROSSOVER_MDI;
225 	else if (mdix == ETH_TP_MDI_X)
226 		reg_val = FORCE_MDI_CROSSOVER_MDIX;
227 
228 	rc = phy_modify_paged(phydev, MSCC_PHY_PAGE_EXTENDED,
229 			      MSCC_PHY_EXT_MODE_CNTL, FORCE_MDI_CROSSOVER_MASK,
230 			      reg_val);
231 	if (rc < 0)
232 		return rc;
233 
234 	return genphy_restart_aneg(phydev);
235 }
236 
237 static int vsc85xx_downshift_get(struct phy_device *phydev, u8 *count)
238 {
239 	int reg_val;
240 
241 	reg_val = phy_read_paged(phydev, MSCC_PHY_PAGE_EXTENDED,
242 				 MSCC_PHY_ACTIPHY_CNTL);
243 	if (reg_val < 0)
244 		return reg_val;
245 
246 	reg_val &= DOWNSHIFT_CNTL_MASK;
247 	if (!(reg_val & DOWNSHIFT_EN))
248 		*count = DOWNSHIFT_DEV_DISABLE;
249 	else
250 		*count = ((reg_val & ~DOWNSHIFT_EN) >> DOWNSHIFT_CNTL_POS) + 2;
251 
252 	return 0;
253 }
254 
255 static int vsc85xx_downshift_set(struct phy_device *phydev, u8 count)
256 {
257 	if (count == DOWNSHIFT_DEV_DEFAULT_COUNT) {
258 		/* Default downshift count 3 (i.e. Bit3:2 = 0b01) */
259 		count = ((1 << DOWNSHIFT_CNTL_POS) | DOWNSHIFT_EN);
260 	} else if (count > DOWNSHIFT_COUNT_MAX || count == 1) {
261 		phydev_err(phydev, "Downshift count should be 2,3,4 or 5\n");
262 		return -ERANGE;
263 	} else if (count) {
264 		/* Downshift count is either 2,3,4 or 5 */
265 		count = (((count - 2) << DOWNSHIFT_CNTL_POS) | DOWNSHIFT_EN);
266 	}
267 
268 	return phy_modify_paged(phydev, MSCC_PHY_PAGE_EXTENDED,
269 				MSCC_PHY_ACTIPHY_CNTL, DOWNSHIFT_CNTL_MASK,
270 				count);
271 }
272 
273 static int vsc85xx_wol_set(struct phy_device *phydev,
274 			   struct ethtool_wolinfo *wol)
275 {
276 	int rc;
277 	u16 reg_val;
278 	u8  i;
279 	u16 pwd[3] = {0, 0, 0};
280 	struct ethtool_wolinfo *wol_conf = wol;
281 	u8 *mac_addr = phydev->attached_dev->dev_addr;
282 
283 	mutex_lock(&phydev->lock);
284 	rc = phy_select_page(phydev, MSCC_PHY_PAGE_EXTENDED_2);
285 	if (rc < 0) {
286 		rc = phy_restore_page(phydev, rc, rc);
287 		goto out_unlock;
288 	}
289 
290 	if (wol->wolopts & WAKE_MAGIC) {
291 		/* Store the device address for the magic packet */
292 		for (i = 0; i < ARRAY_SIZE(pwd); i++)
293 			pwd[i] = mac_addr[5 - (i * 2 + 1)] << 8 |
294 				 mac_addr[5 - i * 2];
295 		__phy_write(phydev, MSCC_PHY_WOL_LOWER_MAC_ADDR, pwd[0]);
296 		__phy_write(phydev, MSCC_PHY_WOL_MID_MAC_ADDR, pwd[1]);
297 		__phy_write(phydev, MSCC_PHY_WOL_UPPER_MAC_ADDR, pwd[2]);
298 	} else {
299 		__phy_write(phydev, MSCC_PHY_WOL_LOWER_MAC_ADDR, 0);
300 		__phy_write(phydev, MSCC_PHY_WOL_MID_MAC_ADDR, 0);
301 		__phy_write(phydev, MSCC_PHY_WOL_UPPER_MAC_ADDR, 0);
302 	}
303 
304 	if (wol_conf->wolopts & WAKE_MAGICSECURE) {
305 		for (i = 0; i < ARRAY_SIZE(pwd); i++)
306 			pwd[i] = wol_conf->sopass[5 - (i * 2 + 1)] << 8 |
307 				 wol_conf->sopass[5 - i * 2];
308 		__phy_write(phydev, MSCC_PHY_WOL_LOWER_PASSWD, pwd[0]);
309 		__phy_write(phydev, MSCC_PHY_WOL_MID_PASSWD, pwd[1]);
310 		__phy_write(phydev, MSCC_PHY_WOL_UPPER_PASSWD, pwd[2]);
311 	} else {
312 		__phy_write(phydev, MSCC_PHY_WOL_LOWER_PASSWD, 0);
313 		__phy_write(phydev, MSCC_PHY_WOL_MID_PASSWD, 0);
314 		__phy_write(phydev, MSCC_PHY_WOL_UPPER_PASSWD, 0);
315 	}
316 
317 	reg_val = __phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL);
318 	if (wol_conf->wolopts & WAKE_MAGICSECURE)
319 		reg_val |= SECURE_ON_ENABLE;
320 	else
321 		reg_val &= ~SECURE_ON_ENABLE;
322 	__phy_write(phydev, MSCC_PHY_WOL_MAC_CONTROL, reg_val);
323 
324 	rc = phy_restore_page(phydev, rc, rc > 0 ? 0 : rc);
325 	if (rc < 0)
326 		goto out_unlock;
327 
328 	if (wol->wolopts & WAKE_MAGIC) {
329 		/* Enable the WOL interrupt */
330 		reg_val = phy_read(phydev, MII_VSC85XX_INT_MASK);
331 		reg_val |= MII_VSC85XX_INT_MASK_WOL;
332 		rc = phy_write(phydev, MII_VSC85XX_INT_MASK, reg_val);
333 		if (rc)
334 			goto out_unlock;
335 	} else {
336 		/* Disable the WOL interrupt */
337 		reg_val = phy_read(phydev, MII_VSC85XX_INT_MASK);
338 		reg_val &= (~MII_VSC85XX_INT_MASK_WOL);
339 		rc = phy_write(phydev, MII_VSC85XX_INT_MASK, reg_val);
340 		if (rc)
341 			goto out_unlock;
342 	}
343 	/* Clear WOL iterrupt status */
344 	reg_val = phy_read(phydev, MII_VSC85XX_INT_STATUS);
345 
346 out_unlock:
347 	mutex_unlock(&phydev->lock);
348 
349 	return rc;
350 }
351 
352 static void vsc85xx_wol_get(struct phy_device *phydev,
353 			    struct ethtool_wolinfo *wol)
354 {
355 	int rc;
356 	u16 reg_val;
357 	u8  i;
358 	u16 pwd[3] = {0, 0, 0};
359 	struct ethtool_wolinfo *wol_conf = wol;
360 
361 	mutex_lock(&phydev->lock);
362 	rc = phy_select_page(phydev, MSCC_PHY_PAGE_EXTENDED_2);
363 	if (rc < 0)
364 		goto out_unlock;
365 
366 	reg_val = __phy_read(phydev, MSCC_PHY_WOL_MAC_CONTROL);
367 	if (reg_val & SECURE_ON_ENABLE)
368 		wol_conf->wolopts |= WAKE_MAGICSECURE;
369 	if (wol_conf->wolopts & WAKE_MAGICSECURE) {
370 		pwd[0] = __phy_read(phydev, MSCC_PHY_WOL_LOWER_PASSWD);
371 		pwd[1] = __phy_read(phydev, MSCC_PHY_WOL_MID_PASSWD);
372 		pwd[2] = __phy_read(phydev, MSCC_PHY_WOL_UPPER_PASSWD);
373 		for (i = 0; i < ARRAY_SIZE(pwd); i++) {
374 			wol_conf->sopass[5 - i * 2] = pwd[i] & 0x00ff;
375 			wol_conf->sopass[5 - (i * 2 + 1)] = (pwd[i] & 0xff00)
376 							    >> 8;
377 		}
378 	}
379 
380 out_unlock:
381 	phy_restore_page(phydev, rc, rc > 0 ? 0 : rc);
382 	mutex_unlock(&phydev->lock);
383 }
384 
385 #if IS_ENABLED(CONFIG_OF_MDIO)
386 static int vsc85xx_edge_rate_magic_get(struct phy_device *phydev)
387 {
388 	u32 vdd, sd;
389 	int i, j;
390 	struct device *dev = &phydev->mdio.dev;
391 	struct device_node *of_node = dev->of_node;
392 	u8 sd_array_size = ARRAY_SIZE(edge_table[0].slowdown);
393 
394 	if (!of_node)
395 		return -ENODEV;
396 
397 	if (of_property_read_u32(of_node, "vsc8531,vddmac", &vdd))
398 		vdd = MSCC_VDDMAC_3300;
399 
400 	if (of_property_read_u32(of_node, "vsc8531,edge-slowdown", &sd))
401 		sd = 0;
402 
403 	for (i = 0; i < ARRAY_SIZE(edge_table); i++)
404 		if (edge_table[i].vddmac == vdd)
405 			for (j = 0; j < sd_array_size; j++)
406 				if (edge_table[i].slowdown[j] == sd)
407 					return (sd_array_size - j - 1);
408 
409 	return -EINVAL;
410 }
411 
412 static int vsc85xx_dt_led_mode_get(struct phy_device *phydev,
413 				   char *led,
414 				   u32 default_mode)
415 {
416 	struct vsc8531_private *priv = phydev->priv;
417 	struct device *dev = &phydev->mdio.dev;
418 	struct device_node *of_node = dev->of_node;
419 	u32 led_mode;
420 	int err;
421 
422 	if (!of_node)
423 		return -ENODEV;
424 
425 	led_mode = default_mode;
426 	err = of_property_read_u32(of_node, led, &led_mode);
427 	if (!err && !(BIT(led_mode) & priv->supp_led_modes)) {
428 		phydev_err(phydev, "DT %s invalid\n", led);
429 		return -EINVAL;
430 	}
431 
432 	return led_mode;
433 }
434 
435 #else
436 static int vsc85xx_edge_rate_magic_get(struct phy_device *phydev)
437 {
438 	return 0;
439 }
440 
441 static int vsc85xx_dt_led_mode_get(struct phy_device *phydev,
442 				   char *led,
443 				   u8 default_mode)
444 {
445 	return default_mode;
446 }
447 #endif /* CONFIG_OF_MDIO */
448 
449 static int vsc85xx_dt_led_modes_get(struct phy_device *phydev,
450 				    u32 *default_mode)
451 {
452 	struct vsc8531_private *priv = phydev->priv;
453 	char led_dt_prop[28];
454 	int i, ret;
455 
456 	for (i = 0; i < priv->nleds; i++) {
457 		ret = sprintf(led_dt_prop, "vsc8531,led-%d-mode", i);
458 		if (ret < 0)
459 			return ret;
460 
461 		ret = vsc85xx_dt_led_mode_get(phydev, led_dt_prop,
462 					      default_mode[i]);
463 		if (ret < 0)
464 			return ret;
465 		priv->leds_mode[i] = ret;
466 	}
467 
468 	return 0;
469 }
470 
471 static int vsc85xx_edge_rate_cntl_set(struct phy_device *phydev, u8 edge_rate)
472 {
473 	int rc;
474 
475 	mutex_lock(&phydev->lock);
476 	rc = phy_modify_paged(phydev, MSCC_PHY_PAGE_EXTENDED_2,
477 			      MSCC_PHY_WOL_MAC_CONTROL, EDGE_RATE_CNTL_MASK,
478 			      edge_rate << EDGE_RATE_CNTL_POS);
479 	mutex_unlock(&phydev->lock);
480 
481 	return rc;
482 }
483 
484 static int vsc85xx_mac_if_set(struct phy_device *phydev,
485 			      phy_interface_t interface)
486 {
487 	int rc;
488 	u16 reg_val;
489 
490 	mutex_lock(&phydev->lock);
491 	reg_val = phy_read(phydev, MSCC_PHY_EXT_PHY_CNTL_1);
492 	reg_val &= ~(MAC_IF_SELECTION_MASK);
493 	switch (interface) {
494 	case PHY_INTERFACE_MODE_RGMII_TXID:
495 	case PHY_INTERFACE_MODE_RGMII_RXID:
496 	case PHY_INTERFACE_MODE_RGMII_ID:
497 	case PHY_INTERFACE_MODE_RGMII:
498 		reg_val |= (MAC_IF_SELECTION_RGMII << MAC_IF_SELECTION_POS);
499 		break;
500 	case PHY_INTERFACE_MODE_RMII:
501 		reg_val |= (MAC_IF_SELECTION_RMII << MAC_IF_SELECTION_POS);
502 		break;
503 	case PHY_INTERFACE_MODE_MII:
504 	case PHY_INTERFACE_MODE_GMII:
505 		reg_val |= (MAC_IF_SELECTION_GMII << MAC_IF_SELECTION_POS);
506 		break;
507 	default:
508 		rc = -EINVAL;
509 		goto out_unlock;
510 	}
511 	rc = phy_write(phydev, MSCC_PHY_EXT_PHY_CNTL_1, reg_val);
512 	if (rc)
513 		goto out_unlock;
514 
515 	rc = genphy_soft_reset(phydev);
516 
517 out_unlock:
518 	mutex_unlock(&phydev->lock);
519 
520 	return rc;
521 }
522 
523 /* Set the RGMII RX and TX clock skews individually, according to the PHY
524  * interface type, to:
525  *  * 0.2 ns (their default, and lowest, hardware value) if delays should
526  *    not be enabled
527  *  * 2.0 ns (which causes the data to be sampled at exactly half way between
528  *    clock transitions at 1000 Mbps) if delays should be enabled
529  */
530 static int vsc85xx_rgmii_set_skews(struct phy_device *phydev, u32 rgmii_cntl,
531 				   u16 rgmii_rx_delay_mask,
532 				   u16 rgmii_tx_delay_mask)
533 {
534 	u16 rgmii_rx_delay_pos = ffs(rgmii_rx_delay_mask) - 1;
535 	u16 rgmii_tx_delay_pos = ffs(rgmii_tx_delay_mask) - 1;
536 	u16 reg_val = 0;
537 	int rc;
538 
539 	mutex_lock(&phydev->lock);
540 
541 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID ||
542 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
543 		reg_val |= RGMII_CLK_DELAY_2_0_NS << rgmii_rx_delay_pos;
544 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID ||
545 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
546 		reg_val |= RGMII_CLK_DELAY_2_0_NS << rgmii_tx_delay_pos;
547 
548 	rc = phy_modify_paged(phydev, MSCC_PHY_PAGE_EXTENDED_2,
549 			      rgmii_cntl,
550 			      rgmii_rx_delay_mask | rgmii_tx_delay_mask,
551 			      reg_val);
552 
553 	mutex_unlock(&phydev->lock);
554 
555 	return rc;
556 }
557 
558 static int vsc85xx_default_config(struct phy_device *phydev)
559 {
560 	int rc;
561 
562 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
563 
564 	if (phy_interface_mode_is_rgmii(phydev->interface)) {
565 		rc = vsc85xx_rgmii_set_skews(phydev, VSC8502_RGMII_CNTL,
566 					     VSC8502_RGMII_RX_DELAY_MASK,
567 					     VSC8502_RGMII_TX_DELAY_MASK);
568 		if (rc)
569 			return rc;
570 	}
571 
572 	return 0;
573 }
574 
575 static int vsc85xx_get_tunable(struct phy_device *phydev,
576 			       struct ethtool_tunable *tuna, void *data)
577 {
578 	switch (tuna->id) {
579 	case ETHTOOL_PHY_DOWNSHIFT:
580 		return vsc85xx_downshift_get(phydev, (u8 *)data);
581 	default:
582 		return -EINVAL;
583 	}
584 }
585 
586 static int vsc85xx_set_tunable(struct phy_device *phydev,
587 			       struct ethtool_tunable *tuna,
588 			       const void *data)
589 {
590 	switch (tuna->id) {
591 	case ETHTOOL_PHY_DOWNSHIFT:
592 		return vsc85xx_downshift_set(phydev, *(u8 *)data);
593 	default:
594 		return -EINVAL;
595 	}
596 }
597 
598 /* mdiobus lock should be locked when using this function */
599 static void vsc85xx_tr_write(struct phy_device *phydev, u16 addr, u32 val)
600 {
601 	__phy_write(phydev, MSCC_PHY_TR_MSB, val >> 16);
602 	__phy_write(phydev, MSCC_PHY_TR_LSB, val & GENMASK(15, 0));
603 	__phy_write(phydev, MSCC_PHY_TR_CNTL, TR_WRITE | TR_ADDR(addr));
604 }
605 
606 static int vsc8531_pre_init_seq_set(struct phy_device *phydev)
607 {
608 	int rc;
609 	static const struct reg_val init_seq[] = {
610 		{0x0f90, 0x00688980},
611 		{0x0696, 0x00000003},
612 		{0x07fa, 0x0050100f},
613 		{0x1686, 0x00000004},
614 	};
615 	unsigned int i;
616 	int oldpage;
617 
618 	rc = phy_modify_paged(phydev, MSCC_PHY_PAGE_STANDARD,
619 			      MSCC_PHY_EXT_CNTL_STATUS, SMI_BROADCAST_WR_EN,
620 			      SMI_BROADCAST_WR_EN);
621 	if (rc < 0)
622 		return rc;
623 	rc = phy_modify_paged(phydev, MSCC_PHY_PAGE_TEST,
624 			      MSCC_PHY_TEST_PAGE_24, 0, 0x0400);
625 	if (rc < 0)
626 		return rc;
627 	rc = phy_modify_paged(phydev, MSCC_PHY_PAGE_TEST,
628 			      MSCC_PHY_TEST_PAGE_5, 0x0a00, 0x0e00);
629 	if (rc < 0)
630 		return rc;
631 	rc = phy_modify_paged(phydev, MSCC_PHY_PAGE_TEST,
632 			      MSCC_PHY_TEST_PAGE_8, 0x8000, 0x8000);
633 	if (rc < 0)
634 		return rc;
635 
636 	mutex_lock(&phydev->lock);
637 	oldpage = phy_select_page(phydev, MSCC_PHY_PAGE_TR);
638 	if (oldpage < 0)
639 		goto out_unlock;
640 
641 	for (i = 0; i < ARRAY_SIZE(init_seq); i++)
642 		vsc85xx_tr_write(phydev, init_seq[i].reg, init_seq[i].val);
643 
644 out_unlock:
645 	oldpage = phy_restore_page(phydev, oldpage, oldpage);
646 	mutex_unlock(&phydev->lock);
647 
648 	return oldpage;
649 }
650 
651 static int vsc85xx_eee_init_seq_set(struct phy_device *phydev)
652 {
653 	static const struct reg_val init_eee[] = {
654 		{0x0f82, 0x0012b00a},
655 		{0x1686, 0x00000004},
656 		{0x168c, 0x00d2c46f},
657 		{0x17a2, 0x00000620},
658 		{0x16a0, 0x00eeffdd},
659 		{0x16a6, 0x00071448},
660 		{0x16a4, 0x0013132f},
661 		{0x16a8, 0x00000000},
662 		{0x0ffc, 0x00c0a028},
663 		{0x0fe8, 0x0091b06c},
664 		{0x0fea, 0x00041600},
665 		{0x0f80, 0x00000af4},
666 		{0x0fec, 0x00901809},
667 		{0x0fee, 0x0000a6a1},
668 		{0x0ffe, 0x00b01007},
669 		{0x16b0, 0x00eeff00},
670 		{0x16b2, 0x00007000},
671 		{0x16b4, 0x00000814},
672 	};
673 	unsigned int i;
674 	int oldpage;
675 
676 	mutex_lock(&phydev->lock);
677 	oldpage = phy_select_page(phydev, MSCC_PHY_PAGE_TR);
678 	if (oldpage < 0)
679 		goto out_unlock;
680 
681 	for (i = 0; i < ARRAY_SIZE(init_eee); i++)
682 		vsc85xx_tr_write(phydev, init_eee[i].reg, init_eee[i].val);
683 
684 out_unlock:
685 	oldpage = phy_restore_page(phydev, oldpage, oldpage);
686 	mutex_unlock(&phydev->lock);
687 
688 	return oldpage;
689 }
690 
691 /* phydev->bus->mdio_lock should be locked when using this function */
692 static int phy_base_write(struct phy_device *phydev, u32 regnum, u16 val)
693 {
694 	if (unlikely(!mutex_is_locked(&phydev->mdio.bus->mdio_lock))) {
695 		dev_err(&phydev->mdio.dev, "MDIO bus lock not held!\n");
696 		dump_stack();
697 	}
698 
699 	return __phy_package_write(phydev, regnum, val);
700 }
701 
702 /* phydev->bus->mdio_lock should be locked when using this function */
703 static int phy_base_read(struct phy_device *phydev, u32 regnum)
704 {
705 	if (unlikely(!mutex_is_locked(&phydev->mdio.bus->mdio_lock))) {
706 		dev_err(&phydev->mdio.dev, "MDIO bus lock not held!\n");
707 		dump_stack();
708 	}
709 
710 	return __phy_package_read(phydev, regnum);
711 }
712 
713 /* bus->mdio_lock should be locked when using this function */
714 static void vsc8584_csr_write(struct phy_device *phydev, u16 addr, u32 val)
715 {
716 	phy_base_write(phydev, MSCC_PHY_TR_MSB, val >> 16);
717 	phy_base_write(phydev, MSCC_PHY_TR_LSB, val & GENMASK(15, 0));
718 	phy_base_write(phydev, MSCC_PHY_TR_CNTL, TR_WRITE | TR_ADDR(addr));
719 }
720 
721 /* bus->mdio_lock should be locked when using this function */
722 static int vsc8584_cmd(struct phy_device *phydev, u16 val)
723 {
724 	unsigned long deadline;
725 	u16 reg_val;
726 
727 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
728 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
729 
730 	phy_base_write(phydev, MSCC_PHY_PROC_CMD, PROC_CMD_NCOMPLETED | val);
731 
732 	deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
733 	do {
734 		reg_val = phy_base_read(phydev, MSCC_PHY_PROC_CMD);
735 	} while (time_before(jiffies, deadline) &&
736 		 (reg_val & PROC_CMD_NCOMPLETED) &&
737 		 !(reg_val & PROC_CMD_FAILED));
738 
739 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
740 
741 	if (reg_val & PROC_CMD_FAILED)
742 		return -EIO;
743 
744 	if (reg_val & PROC_CMD_NCOMPLETED)
745 		return -ETIMEDOUT;
746 
747 	return 0;
748 }
749 
750 /* bus->mdio_lock should be locked when using this function */
751 static int vsc8584_micro_deassert_reset(struct phy_device *phydev,
752 					bool patch_en)
753 {
754 	u32 enable, release;
755 
756 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
757 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
758 
759 	enable = RUN_FROM_INT_ROM | MICRO_CLK_EN | DW8051_CLK_EN;
760 	release = MICRO_NSOFT_RESET | RUN_FROM_INT_ROM | DW8051_CLK_EN |
761 		MICRO_CLK_EN;
762 
763 	if (patch_en) {
764 		enable |= MICRO_PATCH_EN;
765 		release |= MICRO_PATCH_EN;
766 
767 		/* Clear all patches */
768 		phy_base_write(phydev, MSCC_INT_MEM_CNTL, READ_RAM);
769 	}
770 
771 	/* Enable 8051 Micro clock; CLEAR/SET patch present; disable PRAM clock
772 	 * override and addr. auto-incr; operate at 125 MHz
773 	 */
774 	phy_base_write(phydev, MSCC_DW8051_CNTL_STATUS, enable);
775 	/* Release 8051 Micro SW reset */
776 	phy_base_write(phydev, MSCC_DW8051_CNTL_STATUS, release);
777 
778 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
779 
780 	return 0;
781 }
782 
783 /* bus->mdio_lock should be locked when using this function */
784 static int vsc8584_micro_assert_reset(struct phy_device *phydev)
785 {
786 	int ret;
787 	u16 reg;
788 
789 	ret = vsc8584_cmd(phydev, PROC_CMD_NOP);
790 	if (ret)
791 		return ret;
792 
793 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
794 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
795 
796 	reg = phy_base_read(phydev, MSCC_INT_MEM_CNTL);
797 	reg &= ~EN_PATCH_RAM_TRAP_ADDR(4);
798 	phy_base_write(phydev, MSCC_INT_MEM_CNTL, reg);
799 
800 	phy_base_write(phydev, MSCC_TRAP_ROM_ADDR(4), 0x005b);
801 	phy_base_write(phydev, MSCC_PATCH_RAM_ADDR(4), 0x005b);
802 
803 	reg = phy_base_read(phydev, MSCC_INT_MEM_CNTL);
804 	reg |= EN_PATCH_RAM_TRAP_ADDR(4);
805 	phy_base_write(phydev, MSCC_INT_MEM_CNTL, reg);
806 
807 	phy_base_write(phydev, MSCC_PHY_PROC_CMD, PROC_CMD_NOP);
808 
809 	reg = phy_base_read(phydev, MSCC_DW8051_CNTL_STATUS);
810 	reg &= ~MICRO_NSOFT_RESET;
811 	phy_base_write(phydev, MSCC_DW8051_CNTL_STATUS, reg);
812 
813 	phy_base_write(phydev, MSCC_PHY_PROC_CMD, PROC_CMD_MCB_ACCESS_MAC_CONF |
814 		       PROC_CMD_SGMII_PORT(0) | PROC_CMD_NO_MAC_CONF |
815 		       PROC_CMD_READ);
816 
817 	reg = phy_base_read(phydev, MSCC_INT_MEM_CNTL);
818 	reg &= ~EN_PATCH_RAM_TRAP_ADDR(4);
819 	phy_base_write(phydev, MSCC_INT_MEM_CNTL, reg);
820 
821 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
822 
823 	return 0;
824 }
825 
826 /* bus->mdio_lock should be locked when using this function */
827 static int vsc8584_get_fw_crc(struct phy_device *phydev, u16 start, u16 size,
828 			      u16 *crc)
829 {
830 	int ret;
831 
832 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_EXTENDED);
833 
834 	phy_base_write(phydev, MSCC_PHY_VERIPHY_CNTL_2, start);
835 	phy_base_write(phydev, MSCC_PHY_VERIPHY_CNTL_3, size);
836 
837 	/* Start Micro command */
838 	ret = vsc8584_cmd(phydev, PROC_CMD_CRC16);
839 	if (ret)
840 		goto out;
841 
842 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_EXTENDED);
843 
844 	*crc = phy_base_read(phydev, MSCC_PHY_VERIPHY_CNTL_2);
845 
846 out:
847 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
848 
849 	return ret;
850 }
851 
852 /* bus->mdio_lock should be locked when using this function */
853 static int vsc8584_patch_fw(struct phy_device *phydev,
854 			    const struct firmware *fw)
855 {
856 	int i, ret;
857 
858 	ret = vsc8584_micro_assert_reset(phydev);
859 	if (ret) {
860 		dev_err(&phydev->mdio.dev,
861 			"%s: failed to assert reset of micro\n", __func__);
862 		return ret;
863 	}
864 
865 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
866 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
867 
868 	/* Hold 8051 Micro in SW Reset, Enable auto incr address and patch clock
869 	 * Disable the 8051 Micro clock
870 	 */
871 	phy_base_write(phydev, MSCC_DW8051_CNTL_STATUS, RUN_FROM_INT_ROM |
872 		       AUTOINC_ADDR | PATCH_RAM_CLK | MICRO_CLK_EN |
873 		       MICRO_CLK_DIVIDE(2));
874 	phy_base_write(phydev, MSCC_INT_MEM_CNTL, READ_PRAM | INT_MEM_WRITE_EN |
875 		       INT_MEM_DATA(2));
876 	phy_base_write(phydev, MSCC_INT_MEM_ADDR, 0x0000);
877 
878 	for (i = 0; i < fw->size; i++)
879 		phy_base_write(phydev, MSCC_INT_MEM_CNTL, READ_PRAM |
880 			       INT_MEM_WRITE_EN | fw->data[i]);
881 
882 	/* Clear internal memory access */
883 	phy_base_write(phydev, MSCC_INT_MEM_CNTL, READ_RAM);
884 
885 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
886 
887 	return 0;
888 }
889 
890 /* bus->mdio_lock should be locked when using this function */
891 static bool vsc8574_is_serdes_init(struct phy_device *phydev)
892 {
893 	u16 reg;
894 	bool ret;
895 
896 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
897 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
898 
899 	reg = phy_base_read(phydev, MSCC_TRAP_ROM_ADDR(1));
900 	if (reg != 0x3eb7) {
901 		ret = false;
902 		goto out;
903 	}
904 
905 	reg = phy_base_read(phydev, MSCC_PATCH_RAM_ADDR(1));
906 	if (reg != 0x4012) {
907 		ret = false;
908 		goto out;
909 	}
910 
911 	reg = phy_base_read(phydev, MSCC_INT_MEM_CNTL);
912 	if (reg != EN_PATCH_RAM_TRAP_ADDR(1)) {
913 		ret = false;
914 		goto out;
915 	}
916 
917 	reg = phy_base_read(phydev, MSCC_DW8051_CNTL_STATUS);
918 	if ((MICRO_NSOFT_RESET | RUN_FROM_INT_ROM |  DW8051_CLK_EN |
919 	     MICRO_CLK_EN) != (reg & MSCC_DW8051_VLD_MASK)) {
920 		ret = false;
921 		goto out;
922 	}
923 
924 	ret = true;
925 out:
926 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
927 
928 	return ret;
929 }
930 
931 /* bus->mdio_lock should be locked when using this function */
932 static int vsc8574_config_pre_init(struct phy_device *phydev)
933 {
934 	static const struct reg_val pre_init1[] = {
935 		{0x0fae, 0x000401bd},
936 		{0x0fac, 0x000f000f},
937 		{0x17a0, 0x00a0f147},
938 		{0x0fe4, 0x00052f54},
939 		{0x1792, 0x0027303d},
940 		{0x07fe, 0x00000704},
941 		{0x0fe0, 0x00060150},
942 		{0x0f82, 0x0012b00a},
943 		{0x0f80, 0x00000d74},
944 		{0x02e0, 0x00000012},
945 		{0x03a2, 0x00050208},
946 		{0x03b2, 0x00009186},
947 		{0x0fb0, 0x000e3700},
948 		{0x1688, 0x00049f81},
949 		{0x0fd2, 0x0000ffff},
950 		{0x168a, 0x00039fa2},
951 		{0x1690, 0x0020640b},
952 		{0x0258, 0x00002220},
953 		{0x025a, 0x00002a20},
954 		{0x025c, 0x00003060},
955 		{0x025e, 0x00003fa0},
956 		{0x03a6, 0x0000e0f0},
957 		{0x0f92, 0x00001489},
958 		{0x16a2, 0x00007000},
959 		{0x16a6, 0x00071448},
960 		{0x16a0, 0x00eeffdd},
961 		{0x0fe8, 0x0091b06c},
962 		{0x0fea, 0x00041600},
963 		{0x16b0, 0x00eeff00},
964 		{0x16b2, 0x00007000},
965 		{0x16b4, 0x00000814},
966 		{0x0f90, 0x00688980},
967 		{0x03a4, 0x0000d8f0},
968 		{0x0fc0, 0x00000400},
969 		{0x07fa, 0x0050100f},
970 		{0x0796, 0x00000003},
971 		{0x07f8, 0x00c3ff98},
972 		{0x0fa4, 0x0018292a},
973 		{0x168c, 0x00d2c46f},
974 		{0x17a2, 0x00000620},
975 		{0x16a4, 0x0013132f},
976 		{0x16a8, 0x00000000},
977 		{0x0ffc, 0x00c0a028},
978 		{0x0fec, 0x00901c09},
979 		{0x0fee, 0x0004a6a1},
980 		{0x0ffe, 0x00b01807},
981 	};
982 	static const struct reg_val pre_init2[] = {
983 		{0x0486, 0x0008a518},
984 		{0x0488, 0x006dc696},
985 		{0x048a, 0x00000912},
986 		{0x048e, 0x00000db6},
987 		{0x049c, 0x00596596},
988 		{0x049e, 0x00000514},
989 		{0x04a2, 0x00410280},
990 		{0x04a4, 0x00000000},
991 		{0x04a6, 0x00000000},
992 		{0x04a8, 0x00000000},
993 		{0x04aa, 0x00000000},
994 		{0x04ae, 0x007df7dd},
995 		{0x04b0, 0x006d95d4},
996 		{0x04b2, 0x00492410},
997 	};
998 	struct device *dev = &phydev->mdio.dev;
999 	const struct firmware *fw;
1000 	unsigned int i;
1001 	u16 crc, reg;
1002 	bool serdes_init;
1003 	int ret;
1004 
1005 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1006 
1007 	/* all writes below are broadcasted to all PHYs in the same package */
1008 	reg = phy_base_read(phydev, MSCC_PHY_EXT_CNTL_STATUS);
1009 	reg |= SMI_BROADCAST_WR_EN;
1010 	phy_base_write(phydev, MSCC_PHY_EXT_CNTL_STATUS, reg);
1011 
1012 	phy_base_write(phydev, MII_VSC85XX_INT_MASK, 0);
1013 
1014 	/* The below register writes are tweaking analog and electrical
1015 	 * configuration that were determined through characterization by PHY
1016 	 * engineers. These don't mean anything more than "these are the best
1017 	 * values".
1018 	 */
1019 	phy_base_write(phydev, MSCC_PHY_EXT_PHY_CNTL_2, 0x0040);
1020 
1021 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TEST);
1022 
1023 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_20, 0x4320);
1024 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_24, 0x0c00);
1025 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_9, 0x18ca);
1026 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_5, 0x1b20);
1027 
1028 	reg = phy_base_read(phydev, MSCC_PHY_TEST_PAGE_8);
1029 	reg |= 0x8000;
1030 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_8, reg);
1031 
1032 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TR);
1033 
1034 	for (i = 0; i < ARRAY_SIZE(pre_init1); i++)
1035 		vsc8584_csr_write(phydev, pre_init1[i].reg, pre_init1[i].val);
1036 
1037 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_EXTENDED_2);
1038 
1039 	phy_base_write(phydev, MSCC_PHY_CU_PMD_TX_CNTL, 0x028e);
1040 
1041 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TR);
1042 
1043 	for (i = 0; i < ARRAY_SIZE(pre_init2); i++)
1044 		vsc8584_csr_write(phydev, pre_init2[i].reg, pre_init2[i].val);
1045 
1046 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TEST);
1047 
1048 	reg = phy_base_read(phydev, MSCC_PHY_TEST_PAGE_8);
1049 	reg &= ~0x8000;
1050 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_8, reg);
1051 
1052 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1053 
1054 	/* end of write broadcasting */
1055 	reg = phy_base_read(phydev, MSCC_PHY_EXT_CNTL_STATUS);
1056 	reg &= ~SMI_BROADCAST_WR_EN;
1057 	phy_base_write(phydev, MSCC_PHY_EXT_CNTL_STATUS, reg);
1058 
1059 	ret = request_firmware(&fw, MSCC_VSC8574_REVB_INT8051_FW, dev);
1060 	if (ret) {
1061 		dev_err(dev, "failed to load firmware %s, ret: %d\n",
1062 			MSCC_VSC8574_REVB_INT8051_FW, ret);
1063 		return ret;
1064 	}
1065 
1066 	/* Add one byte to size for the one added by the patch_fw function */
1067 	ret = vsc8584_get_fw_crc(phydev,
1068 				 MSCC_VSC8574_REVB_INT8051_FW_START_ADDR,
1069 				 fw->size + 1, &crc);
1070 	if (ret)
1071 		goto out;
1072 
1073 	if (crc == MSCC_VSC8574_REVB_INT8051_FW_CRC) {
1074 		serdes_init = vsc8574_is_serdes_init(phydev);
1075 
1076 		if (!serdes_init) {
1077 			ret = vsc8584_micro_assert_reset(phydev);
1078 			if (ret) {
1079 				dev_err(dev,
1080 					"%s: failed to assert reset of micro\n",
1081 					__func__);
1082 				goto out;
1083 			}
1084 		}
1085 	} else {
1086 		dev_dbg(dev, "FW CRC is not the expected one, patching FW\n");
1087 
1088 		serdes_init = false;
1089 
1090 		if (vsc8584_patch_fw(phydev, fw))
1091 			dev_warn(dev,
1092 				 "failed to patch FW, expect non-optimal device\n");
1093 	}
1094 
1095 	if (!serdes_init) {
1096 		phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
1097 			       MSCC_PHY_PAGE_EXTENDED_GPIO);
1098 
1099 		phy_base_write(phydev, MSCC_TRAP_ROM_ADDR(1), 0x3eb7);
1100 		phy_base_write(phydev, MSCC_PATCH_RAM_ADDR(1), 0x4012);
1101 		phy_base_write(phydev, MSCC_INT_MEM_CNTL,
1102 			       EN_PATCH_RAM_TRAP_ADDR(1));
1103 
1104 		vsc8584_micro_deassert_reset(phydev, false);
1105 
1106 		/* Add one byte to size for the one added by the patch_fw
1107 		 * function
1108 		 */
1109 		ret = vsc8584_get_fw_crc(phydev,
1110 					 MSCC_VSC8574_REVB_INT8051_FW_START_ADDR,
1111 					 fw->size + 1, &crc);
1112 		if (ret)
1113 			goto out;
1114 
1115 		if (crc != MSCC_VSC8574_REVB_INT8051_FW_CRC)
1116 			dev_warn(dev,
1117 				 "FW CRC after patching is not the expected one, expect non-optimal device\n");
1118 	}
1119 
1120 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
1121 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
1122 
1123 	ret = vsc8584_cmd(phydev, PROC_CMD_1588_DEFAULT_INIT |
1124 			  PROC_CMD_PHY_INIT);
1125 
1126 out:
1127 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1128 
1129 	release_firmware(fw);
1130 
1131 	return ret;
1132 }
1133 
1134 /* bus->mdio_lock should be locked when using this function */
1135 static int vsc8584_config_pre_init(struct phy_device *phydev)
1136 {
1137 	static const struct reg_val pre_init1[] = {
1138 		{0x07fa, 0x0050100f},
1139 		{0x1688, 0x00049f81},
1140 		{0x0f90, 0x00688980},
1141 		{0x03a4, 0x0000d8f0},
1142 		{0x0fc0, 0x00000400},
1143 		{0x0f82, 0x0012b002},
1144 		{0x1686, 0x00000004},
1145 		{0x168c, 0x00d2c46f},
1146 		{0x17a2, 0x00000620},
1147 		{0x16a0, 0x00eeffdd},
1148 		{0x16a6, 0x00071448},
1149 		{0x16a4, 0x0013132f},
1150 		{0x16a8, 0x00000000},
1151 		{0x0ffc, 0x00c0a028},
1152 		{0x0fe8, 0x0091b06c},
1153 		{0x0fea, 0x00041600},
1154 		{0x0f80, 0x00fffaff},
1155 		{0x0fec, 0x00901809},
1156 		{0x0ffe, 0x00b01007},
1157 		{0x16b0, 0x00eeff00},
1158 		{0x16b2, 0x00007000},
1159 		{0x16b4, 0x00000814},
1160 	};
1161 	static const struct reg_val pre_init2[] = {
1162 		{0x0486, 0x0008a518},
1163 		{0x0488, 0x006dc696},
1164 		{0x048a, 0x00000912},
1165 	};
1166 	const struct firmware *fw;
1167 	struct device *dev = &phydev->mdio.dev;
1168 	unsigned int i;
1169 	u16 crc, reg;
1170 	int ret;
1171 
1172 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1173 
1174 	/* all writes below are broadcasted to all PHYs in the same package */
1175 	reg = phy_base_read(phydev, MSCC_PHY_EXT_CNTL_STATUS);
1176 	reg |= SMI_BROADCAST_WR_EN;
1177 	phy_base_write(phydev, MSCC_PHY_EXT_CNTL_STATUS, reg);
1178 
1179 	phy_base_write(phydev, MII_VSC85XX_INT_MASK, 0);
1180 
1181 	reg = phy_base_read(phydev,  MSCC_PHY_BYPASS_CONTROL);
1182 	reg |= PARALLEL_DET_IGNORE_ADVERTISED;
1183 	phy_base_write(phydev, MSCC_PHY_BYPASS_CONTROL, reg);
1184 
1185 	/* The below register writes are tweaking analog and electrical
1186 	 * configuration that were determined through characterization by PHY
1187 	 * engineers. These don't mean anything more than "these are the best
1188 	 * values".
1189 	 */
1190 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_EXTENDED_3);
1191 
1192 	phy_base_write(phydev, MSCC_PHY_SERDES_TX_CRC_ERR_CNT, 0x2000);
1193 
1194 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TEST);
1195 
1196 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_5, 0x1f20);
1197 
1198 	reg = phy_base_read(phydev, MSCC_PHY_TEST_PAGE_8);
1199 	reg |= 0x8000;
1200 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_8, reg);
1201 
1202 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TR);
1203 
1204 	phy_base_write(phydev, MSCC_PHY_TR_CNTL, TR_WRITE | TR_ADDR(0x2fa4));
1205 
1206 	reg = phy_base_read(phydev, MSCC_PHY_TR_MSB);
1207 	reg &= ~0x007f;
1208 	reg |= 0x0019;
1209 	phy_base_write(phydev, MSCC_PHY_TR_MSB, reg);
1210 
1211 	phy_base_write(phydev, MSCC_PHY_TR_CNTL, TR_WRITE | TR_ADDR(0x0fa4));
1212 
1213 	for (i = 0; i < ARRAY_SIZE(pre_init1); i++)
1214 		vsc8584_csr_write(phydev, pre_init1[i].reg, pre_init1[i].val);
1215 
1216 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_EXTENDED_2);
1217 
1218 	phy_base_write(phydev, MSCC_PHY_CU_PMD_TX_CNTL, 0x028e);
1219 
1220 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TR);
1221 
1222 	for (i = 0; i < ARRAY_SIZE(pre_init2); i++)
1223 		vsc8584_csr_write(phydev, pre_init2[i].reg, pre_init2[i].val);
1224 
1225 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TEST);
1226 
1227 	reg = phy_base_read(phydev, MSCC_PHY_TEST_PAGE_8);
1228 	reg &= ~0x8000;
1229 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_8, reg);
1230 
1231 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1232 
1233 	/* end of write broadcasting */
1234 	reg = phy_base_read(phydev, MSCC_PHY_EXT_CNTL_STATUS);
1235 	reg &= ~SMI_BROADCAST_WR_EN;
1236 	phy_base_write(phydev, MSCC_PHY_EXT_CNTL_STATUS, reg);
1237 
1238 	ret = request_firmware(&fw, MSCC_VSC8584_REVB_INT8051_FW, dev);
1239 	if (ret) {
1240 		dev_err(dev, "failed to load firmware %s, ret: %d\n",
1241 			MSCC_VSC8584_REVB_INT8051_FW, ret);
1242 		return ret;
1243 	}
1244 
1245 	/* Add one byte to size for the one added by the patch_fw function */
1246 	ret = vsc8584_get_fw_crc(phydev,
1247 				 MSCC_VSC8584_REVB_INT8051_FW_START_ADDR,
1248 				 fw->size + 1, &crc);
1249 	if (ret)
1250 		goto out;
1251 
1252 	if (crc != MSCC_VSC8584_REVB_INT8051_FW_CRC) {
1253 		dev_dbg(dev, "FW CRC is not the expected one, patching FW\n");
1254 		if (vsc8584_patch_fw(phydev, fw))
1255 			dev_warn(dev,
1256 				 "failed to patch FW, expect non-optimal device\n");
1257 	}
1258 
1259 	vsc8584_micro_deassert_reset(phydev, false);
1260 
1261 	/* Add one byte to size for the one added by the patch_fw function */
1262 	ret = vsc8584_get_fw_crc(phydev,
1263 				 MSCC_VSC8584_REVB_INT8051_FW_START_ADDR,
1264 				 fw->size + 1, &crc);
1265 	if (ret)
1266 		goto out;
1267 
1268 	if (crc != MSCC_VSC8584_REVB_INT8051_FW_CRC)
1269 		dev_warn(dev,
1270 			 "FW CRC after patching is not the expected one, expect non-optimal device\n");
1271 
1272 	ret = vsc8584_micro_assert_reset(phydev);
1273 	if (ret)
1274 		goto out;
1275 
1276 	vsc8584_micro_deassert_reset(phydev, true);
1277 
1278 out:
1279 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1280 
1281 	release_firmware(fw);
1282 
1283 	return ret;
1284 }
1285 
1286 static void vsc8584_get_base_addr(struct phy_device *phydev)
1287 {
1288 	struct vsc8531_private *vsc8531 = phydev->priv;
1289 	u16 val, addr;
1290 
1291 	mutex_lock(&phydev->mdio.bus->mdio_lock);
1292 	__phy_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_EXTENDED);
1293 
1294 	addr = __phy_read(phydev, MSCC_PHY_EXT_PHY_CNTL_4);
1295 	addr >>= PHY_CNTL_4_ADDR_POS;
1296 
1297 	val = __phy_read(phydev, MSCC_PHY_ACTIPHY_CNTL);
1298 
1299 	__phy_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1300 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
1301 
1302 	if (val & PHY_ADDR_REVERSED)
1303 		vsc8531->base_addr = phydev->mdio.addr + addr;
1304 	else
1305 		vsc8531->base_addr = phydev->mdio.addr - addr;
1306 
1307 	vsc8531->addr = addr;
1308 }
1309 
1310 static int vsc8584_config_init(struct phy_device *phydev)
1311 {
1312 	struct vsc8531_private *vsc8531 = phydev->priv;
1313 	int ret, i;
1314 	u16 val;
1315 
1316 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1317 
1318 	mutex_lock(&phydev->mdio.bus->mdio_lock);
1319 
1320 	/* Some parts of the init sequence are identical for every PHY in the
1321 	 * package. Some parts are modifying the GPIO register bank which is a
1322 	 * set of registers that are affecting all PHYs, a few resetting the
1323 	 * microprocessor common to all PHYs. The CRC check responsible of the
1324 	 * checking the firmware within the 8051 microprocessor can only be
1325 	 * accessed via the PHY whose internal address in the package is 0.
1326 	 * All PHYs' interrupts mask register has to be zeroed before enabling
1327 	 * any PHY's interrupt in this register.
1328 	 * For all these reasons, we need to do the init sequence once and only
1329 	 * once whatever is the first PHY in the package that is initialized and
1330 	 * do the correct init sequence for all PHYs that are package-critical
1331 	 * in this pre-init function.
1332 	 */
1333 	if (phy_package_init_once(phydev)) {
1334 		/* The following switch statement assumes that the lowest
1335 		 * nibble of the phy_id_mask is always 0. This works because
1336 		 * the lowest nibble of the PHY_ID's below are also 0.
1337 		 */
1338 		WARN_ON(phydev->drv->phy_id_mask & 0xf);
1339 
1340 		switch (phydev->phy_id & phydev->drv->phy_id_mask) {
1341 		case PHY_ID_VSC8504:
1342 		case PHY_ID_VSC8552:
1343 		case PHY_ID_VSC8572:
1344 		case PHY_ID_VSC8574:
1345 			ret = vsc8574_config_pre_init(phydev);
1346 			break;
1347 		case PHY_ID_VSC856X:
1348 		case PHY_ID_VSC8575:
1349 		case PHY_ID_VSC8582:
1350 		case PHY_ID_VSC8584:
1351 			ret = vsc8584_config_pre_init(phydev);
1352 			break;
1353 		default:
1354 			ret = -EINVAL;
1355 			break;
1356 		}
1357 
1358 		if (ret)
1359 			goto err;
1360 	}
1361 
1362 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
1363 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
1364 
1365 	val = phy_base_read(phydev, MSCC_PHY_MAC_CFG_FASTLINK);
1366 	val &= ~MAC_CFG_MASK;
1367 	if (phydev->interface == PHY_INTERFACE_MODE_QSGMII) {
1368 		val |= MAC_CFG_QSGMII;
1369 	} else if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
1370 		val |= MAC_CFG_SGMII;
1371 	} else if (phy_interface_is_rgmii(phydev)) {
1372 		val |= MAC_CFG_RGMII;
1373 	} else {
1374 		ret = -EINVAL;
1375 		goto err;
1376 	}
1377 
1378 	ret = phy_base_write(phydev, MSCC_PHY_MAC_CFG_FASTLINK, val);
1379 	if (ret)
1380 		goto err;
1381 
1382 	if (!phy_interface_is_rgmii(phydev)) {
1383 		val = PROC_CMD_MCB_ACCESS_MAC_CONF | PROC_CMD_RST_CONF_PORT |
1384 			PROC_CMD_READ_MOD_WRITE_PORT;
1385 		if (phydev->interface == PHY_INTERFACE_MODE_QSGMII)
1386 			val |= PROC_CMD_QSGMII_MAC;
1387 		else
1388 			val |= PROC_CMD_SGMII_MAC;
1389 
1390 		ret = vsc8584_cmd(phydev, val);
1391 		if (ret)
1392 			goto err;
1393 
1394 		usleep_range(10000, 20000);
1395 	}
1396 
1397 	/* Disable SerDes for 100Base-FX */
1398 	ret = vsc8584_cmd(phydev, PROC_CMD_FIBER_MEDIA_CONF |
1399 			  PROC_CMD_FIBER_PORT(vsc8531->addr) |
1400 			  PROC_CMD_FIBER_DISABLE |
1401 			  PROC_CMD_READ_MOD_WRITE_PORT |
1402 			  PROC_CMD_RST_CONF_PORT | PROC_CMD_FIBER_100BASE_FX);
1403 	if (ret)
1404 		goto err;
1405 
1406 	/* Disable SerDes for 1000Base-X */
1407 	ret = vsc8584_cmd(phydev, PROC_CMD_FIBER_MEDIA_CONF |
1408 			  PROC_CMD_FIBER_PORT(vsc8531->addr) |
1409 			  PROC_CMD_FIBER_DISABLE |
1410 			  PROC_CMD_READ_MOD_WRITE_PORT |
1411 			  PROC_CMD_RST_CONF_PORT | PROC_CMD_FIBER_1000BASE_X);
1412 	if (ret)
1413 		goto err;
1414 
1415 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
1416 
1417 	ret = vsc8584_macsec_init(phydev);
1418 	if (ret)
1419 		return ret;
1420 
1421 	phy_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1422 
1423 	val = phy_read(phydev, MSCC_PHY_EXT_PHY_CNTL_1);
1424 	val &= ~(MEDIA_OP_MODE_MASK | VSC8584_MAC_IF_SELECTION_MASK);
1425 	val |= (MEDIA_OP_MODE_COPPER << MEDIA_OP_MODE_POS) |
1426 	       (VSC8584_MAC_IF_SELECTION_SGMII << VSC8584_MAC_IF_SELECTION_POS);
1427 	ret = phy_write(phydev, MSCC_PHY_EXT_PHY_CNTL_1, val);
1428 	if (ret)
1429 		return ret;
1430 
1431 	if (phy_interface_is_rgmii(phydev)) {
1432 		ret = vsc85xx_rgmii_set_skews(phydev, VSC8572_RGMII_CNTL,
1433 					      VSC8572_RGMII_RX_DELAY_MASK,
1434 					      VSC8572_RGMII_TX_DELAY_MASK);
1435 		if (ret)
1436 			return ret;
1437 	}
1438 
1439 	ret = genphy_soft_reset(phydev);
1440 	if (ret)
1441 		return ret;
1442 
1443 	for (i = 0; i < vsc8531->nleds; i++) {
1444 		ret = vsc85xx_led_cntl_set(phydev, i, vsc8531->leds_mode[i]);
1445 		if (ret)
1446 			return ret;
1447 	}
1448 
1449 	return 0;
1450 
1451 err:
1452 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
1453 	return ret;
1454 }
1455 
1456 static irqreturn_t vsc8584_handle_interrupt(struct phy_device *phydev)
1457 {
1458 	int irq_status;
1459 
1460 	irq_status = phy_read(phydev, MII_VSC85XX_INT_STATUS);
1461 	if (irq_status < 0 || !(irq_status & MII_VSC85XX_INT_MASK_MASK))
1462 		return IRQ_NONE;
1463 
1464 	if (irq_status & MII_VSC85XX_INT_MASK_EXT)
1465 		vsc8584_handle_macsec_interrupt(phydev);
1466 
1467 	if (irq_status & MII_VSC85XX_INT_MASK_LINK_CHG)
1468 		phy_mac_interrupt(phydev);
1469 
1470 	return IRQ_HANDLED;
1471 }
1472 
1473 static int vsc85xx_config_init(struct phy_device *phydev)
1474 {
1475 	int rc, i, phy_id;
1476 	struct vsc8531_private *vsc8531 = phydev->priv;
1477 
1478 	rc = vsc85xx_default_config(phydev);
1479 	if (rc)
1480 		return rc;
1481 
1482 	rc = vsc85xx_mac_if_set(phydev, phydev->interface);
1483 	if (rc)
1484 		return rc;
1485 
1486 	rc = vsc85xx_edge_rate_cntl_set(phydev, vsc8531->rate_magic);
1487 	if (rc)
1488 		return rc;
1489 
1490 	phy_id = phydev->drv->phy_id & phydev->drv->phy_id_mask;
1491 	if (PHY_ID_VSC8531 == phy_id || PHY_ID_VSC8541 == phy_id ||
1492 	    PHY_ID_VSC8530 == phy_id || PHY_ID_VSC8540 == phy_id) {
1493 		rc = vsc8531_pre_init_seq_set(phydev);
1494 		if (rc)
1495 			return rc;
1496 	}
1497 
1498 	rc = vsc85xx_eee_init_seq_set(phydev);
1499 	if (rc)
1500 		return rc;
1501 
1502 	for (i = 0; i < vsc8531->nleds; i++) {
1503 		rc = vsc85xx_led_cntl_set(phydev, i, vsc8531->leds_mode[i]);
1504 		if (rc)
1505 			return rc;
1506 	}
1507 
1508 	return 0;
1509 }
1510 
1511 static int vsc8584_did_interrupt(struct phy_device *phydev)
1512 {
1513 	int rc = 0;
1514 
1515 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
1516 		rc = phy_read(phydev, MII_VSC85XX_INT_STATUS);
1517 
1518 	return (rc < 0) ? 0 : rc & MII_VSC85XX_INT_MASK_MASK;
1519 }
1520 
1521 static int vsc8514_config_pre_init(struct phy_device *phydev)
1522 {
1523 	/* These are the settings to override the silicon default
1524 	 * values to handle hardware performance of PHY. They
1525 	 * are set at Power-On state and remain until PHY Reset.
1526 	 */
1527 	static const struct reg_val pre_init1[] = {
1528 		{0x0f90, 0x00688980},
1529 		{0x0786, 0x00000003},
1530 		{0x07fa, 0x0050100f},
1531 		{0x0f82, 0x0012b002},
1532 		{0x1686, 0x00000004},
1533 		{0x168c, 0x00d2c46f},
1534 		{0x17a2, 0x00000620},
1535 		{0x16a0, 0x00eeffdd},
1536 		{0x16a6, 0x00071448},
1537 		{0x16a4, 0x0013132f},
1538 		{0x16a8, 0x00000000},
1539 		{0x0ffc, 0x00c0a028},
1540 		{0x0fe8, 0x0091b06c},
1541 		{0x0fea, 0x00041600},
1542 		{0x0f80, 0x00fffaff},
1543 		{0x0fec, 0x00901809},
1544 		{0x0ffe, 0x00b01007},
1545 		{0x16b0, 0x00eeff00},
1546 		{0x16b2, 0x00007000},
1547 		{0x16b4, 0x00000814},
1548 	};
1549 	unsigned int i;
1550 	u16 reg;
1551 
1552 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1553 
1554 	/* all writes below are broadcasted to all PHYs in the same package */
1555 	reg = phy_base_read(phydev, MSCC_PHY_EXT_CNTL_STATUS);
1556 	reg |= SMI_BROADCAST_WR_EN;
1557 	phy_base_write(phydev, MSCC_PHY_EXT_CNTL_STATUS, reg);
1558 
1559 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TEST);
1560 
1561 	reg = phy_base_read(phydev, MSCC_PHY_TEST_PAGE_8);
1562 	reg |= BIT(15);
1563 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_8, reg);
1564 
1565 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TR);
1566 
1567 	for (i = 0; i < ARRAY_SIZE(pre_init1); i++)
1568 		vsc8584_csr_write(phydev, pre_init1[i].reg, pre_init1[i].val);
1569 
1570 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_TEST);
1571 
1572 	reg = phy_base_read(phydev, MSCC_PHY_TEST_PAGE_8);
1573 	reg &= ~BIT(15);
1574 	phy_base_write(phydev, MSCC_PHY_TEST_PAGE_8, reg);
1575 
1576 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1577 
1578 	reg = phy_base_read(phydev, MSCC_PHY_EXT_CNTL_STATUS);
1579 	reg &= ~SMI_BROADCAST_WR_EN;
1580 	phy_base_write(phydev, MSCC_PHY_EXT_CNTL_STATUS, reg);
1581 
1582 	return 0;
1583 }
1584 
1585 static u32 vsc85xx_csr_ctrl_phy_read(struct phy_device *phydev,
1586 				     u32 target, u32 reg)
1587 {
1588 	unsigned long deadline;
1589 	u32 val, val_l, val_h;
1590 
1591 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_CSR_CNTL);
1592 
1593 	/* CSR registers are grouped under different Target IDs.
1594 	 * 6-bit Target_ID is split between MSCC_EXT_PAGE_CSR_CNTL_20 and
1595 	 * MSCC_EXT_PAGE_CSR_CNTL_19 registers.
1596 	 * Target_ID[5:2] maps to bits[3:0] of MSCC_EXT_PAGE_CSR_CNTL_20
1597 	 * and Target_ID[1:0] maps to bits[13:12] of MSCC_EXT_PAGE_CSR_CNTL_19.
1598 	 */
1599 
1600 	/* Setup the Target ID */
1601 	phy_base_write(phydev, MSCC_EXT_PAGE_CSR_CNTL_20,
1602 		       MSCC_PHY_CSR_CNTL_20_TARGET(target >> 2));
1603 
1604 	/* Trigger CSR Action - Read into the CSR's */
1605 	phy_base_write(phydev, MSCC_EXT_PAGE_CSR_CNTL_19,
1606 		       MSCC_PHY_CSR_CNTL_19_CMD | MSCC_PHY_CSR_CNTL_19_READ |
1607 		       MSCC_PHY_CSR_CNTL_19_REG_ADDR(reg) |
1608 		       MSCC_PHY_CSR_CNTL_19_TARGET(target & 0x3));
1609 
1610 	/* Wait for register access*/
1611 	deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
1612 	do {
1613 		usleep_range(500, 1000);
1614 		val = phy_base_read(phydev, MSCC_EXT_PAGE_CSR_CNTL_19);
1615 	} while (time_before(jiffies, deadline) &&
1616 		!(val & MSCC_PHY_CSR_CNTL_19_CMD));
1617 
1618 	if (!(val & MSCC_PHY_CSR_CNTL_19_CMD))
1619 		return 0xffffffff;
1620 
1621 	/* Read the Least Significant Word (LSW) (17) */
1622 	val_l = phy_base_read(phydev, MSCC_EXT_PAGE_CSR_CNTL_17);
1623 
1624 	/* Read the Most Significant Word (MSW) (18) */
1625 	val_h = phy_base_read(phydev, MSCC_EXT_PAGE_CSR_CNTL_18);
1626 
1627 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
1628 		       MSCC_PHY_PAGE_STANDARD);
1629 
1630 	return (val_h << 16) | val_l;
1631 }
1632 
1633 static int vsc85xx_csr_ctrl_phy_write(struct phy_device *phydev,
1634 				      u32 target, u32 reg, u32 val)
1635 {
1636 	unsigned long deadline;
1637 
1638 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_CSR_CNTL);
1639 
1640 	/* CSR registers are grouped under different Target IDs.
1641 	 * 6-bit Target_ID is split between MSCC_EXT_PAGE_CSR_CNTL_20 and
1642 	 * MSCC_EXT_PAGE_CSR_CNTL_19 registers.
1643 	 * Target_ID[5:2] maps to bits[3:0] of MSCC_EXT_PAGE_CSR_CNTL_20
1644 	 * and Target_ID[1:0] maps to bits[13:12] of MSCC_EXT_PAGE_CSR_CNTL_19.
1645 	 */
1646 
1647 	/* Setup the Target ID */
1648 	phy_base_write(phydev, MSCC_EXT_PAGE_CSR_CNTL_20,
1649 		       MSCC_PHY_CSR_CNTL_20_TARGET(target >> 2));
1650 
1651 	/* Write the Least Significant Word (LSW) (17) */
1652 	phy_base_write(phydev, MSCC_EXT_PAGE_CSR_CNTL_17, (u16)val);
1653 
1654 	/* Write the Most Significant Word (MSW) (18) */
1655 	phy_base_write(phydev, MSCC_EXT_PAGE_CSR_CNTL_18, (u16)(val >> 16));
1656 
1657 	/* Trigger CSR Action - Write into the CSR's */
1658 	phy_base_write(phydev, MSCC_EXT_PAGE_CSR_CNTL_19,
1659 		       MSCC_PHY_CSR_CNTL_19_CMD |
1660 		       MSCC_PHY_CSR_CNTL_19_REG_ADDR(reg) |
1661 		       MSCC_PHY_CSR_CNTL_19_TARGET(target & 0x3));
1662 
1663 	/* Wait for register access */
1664 	deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
1665 	do {
1666 		usleep_range(500, 1000);
1667 		val = phy_base_read(phydev, MSCC_EXT_PAGE_CSR_CNTL_19);
1668 	} while (time_before(jiffies, deadline) &&
1669 		 !(val & MSCC_PHY_CSR_CNTL_19_CMD));
1670 
1671 	if (!(val & MSCC_PHY_CSR_CNTL_19_CMD))
1672 		return -ETIMEDOUT;
1673 
1674 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
1675 		       MSCC_PHY_PAGE_STANDARD);
1676 
1677 	return 0;
1678 }
1679 
1680 static int __phy_write_mcb_s6g(struct phy_device *phydev, u32 reg, u8 mcb,
1681 			       u32 op)
1682 {
1683 	unsigned long deadline;
1684 	u32 val;
1685 	int ret;
1686 
1687 	ret = vsc85xx_csr_ctrl_phy_write(phydev, PHY_MCB_TARGET, reg,
1688 					 op | (1 << mcb));
1689 	if (ret)
1690 		return -EINVAL;
1691 
1692 	deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
1693 	do {
1694 		usleep_range(500, 1000);
1695 		val = vsc85xx_csr_ctrl_phy_read(phydev, PHY_MCB_TARGET, reg);
1696 
1697 		if (val == 0xffffffff)
1698 			return -EIO;
1699 
1700 	} while (time_before(jiffies, deadline) && (val & op));
1701 
1702 	if (val & op)
1703 		return -ETIMEDOUT;
1704 
1705 	return 0;
1706 }
1707 
1708 /* Trigger a read to the spcified MCB */
1709 static int phy_update_mcb_s6g(struct phy_device *phydev, u32 reg, u8 mcb)
1710 {
1711 	return __phy_write_mcb_s6g(phydev, reg, mcb, PHY_MCB_S6G_READ);
1712 }
1713 
1714 /* Trigger a write to the spcified MCB */
1715 static int phy_commit_mcb_s6g(struct phy_device *phydev, u32 reg, u8 mcb)
1716 {
1717 	return __phy_write_mcb_s6g(phydev, reg, mcb, PHY_MCB_S6G_WRITE);
1718 }
1719 
1720 static int vsc8514_config_init(struct phy_device *phydev)
1721 {
1722 	struct vsc8531_private *vsc8531 = phydev->priv;
1723 	unsigned long deadline;
1724 	int ret, i;
1725 	u16 val;
1726 	u32 reg;
1727 
1728 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
1729 
1730 	mutex_lock(&phydev->mdio.bus->mdio_lock);
1731 
1732 	/* Some parts of the init sequence are identical for every PHY in the
1733 	 * package. Some parts are modifying the GPIO register bank which is a
1734 	 * set of registers that are affecting all PHYs, a few resetting the
1735 	 * microprocessor common to all PHYs.
1736 	 * All PHYs' interrupts mask register has to be zeroed before enabling
1737 	 * any PHY's interrupt in this register.
1738 	 * For all these reasons, we need to do the init sequence once and only
1739 	 * once whatever is the first PHY in the package that is initialized and
1740 	 * do the correct init sequence for all PHYs that are package-critical
1741 	 * in this pre-init function.
1742 	 */
1743 	if (phy_package_init_once(phydev))
1744 		vsc8514_config_pre_init(phydev);
1745 
1746 	phy_base_write(phydev, MSCC_EXT_PAGE_ACCESS,
1747 		       MSCC_PHY_PAGE_EXTENDED_GPIO);
1748 
1749 	val = phy_base_read(phydev, MSCC_PHY_MAC_CFG_FASTLINK);
1750 
1751 	val &= ~MAC_CFG_MASK;
1752 	val |= MAC_CFG_QSGMII;
1753 	ret = phy_base_write(phydev, MSCC_PHY_MAC_CFG_FASTLINK, val);
1754 
1755 	if (ret)
1756 		goto err;
1757 
1758 	ret = vsc8584_cmd(phydev,
1759 			  PROC_CMD_MCB_ACCESS_MAC_CONF |
1760 			  PROC_CMD_RST_CONF_PORT |
1761 			  PROC_CMD_READ_MOD_WRITE_PORT | PROC_CMD_QSGMII_MAC);
1762 	if (ret)
1763 		goto err;
1764 
1765 	/* 6g mcb */
1766 	phy_update_mcb_s6g(phydev, PHY_MCB_S6G_CFG, 0);
1767 	/* lcpll mcb */
1768 	phy_update_mcb_s6g(phydev, PHY_S6G_LCPLL_CFG, 0);
1769 	/* pll5gcfg0 */
1770 	ret = vsc85xx_csr_ctrl_phy_write(phydev, PHY_MCB_TARGET,
1771 					 PHY_S6G_PLL5G_CFG0, 0x7036f145);
1772 	if (ret)
1773 		goto err;
1774 
1775 	phy_commit_mcb_s6g(phydev, PHY_S6G_LCPLL_CFG, 0);
1776 	/* pllcfg */
1777 	ret = vsc85xx_csr_ctrl_phy_write(phydev, PHY_MCB_TARGET,
1778 					 PHY_S6G_PLL_CFG,
1779 					 (3 << PHY_S6G_PLL_ENA_OFFS_POS) |
1780 					 (120 << PHY_S6G_PLL_FSM_CTRL_DATA_POS)
1781 					 | (0 << PHY_S6G_PLL_FSM_ENA_POS));
1782 	if (ret)
1783 		goto err;
1784 
1785 	/* commoncfg */
1786 	ret = vsc85xx_csr_ctrl_phy_write(phydev, PHY_MCB_TARGET,
1787 					 PHY_S6G_COMMON_CFG,
1788 					 (0 << PHY_S6G_SYS_RST_POS) |
1789 					 (0 << PHY_S6G_ENA_LANE_POS) |
1790 					 (0 << PHY_S6G_ENA_LOOP_POS) |
1791 					 (0 << PHY_S6G_QRATE_POS) |
1792 					 (3 << PHY_S6G_IF_MODE_POS));
1793 	if (ret)
1794 		goto err;
1795 
1796 	/* misccfg */
1797 	ret = vsc85xx_csr_ctrl_phy_write(phydev, PHY_MCB_TARGET,
1798 					 PHY_S6G_MISC_CFG, 1);
1799 	if (ret)
1800 		goto err;
1801 
1802 	/* gpcfg */
1803 	ret = vsc85xx_csr_ctrl_phy_write(phydev, PHY_MCB_TARGET,
1804 					 PHY_S6G_GPC_CFG, 768);
1805 	if (ret)
1806 		goto err;
1807 
1808 	phy_commit_mcb_s6g(phydev, PHY_S6G_DFT_CFG2, 0);
1809 
1810 	deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
1811 	do {
1812 		usleep_range(500, 1000);
1813 		phy_update_mcb_s6g(phydev, PHY_MCB_S6G_CFG,
1814 				   0); /* read 6G MCB into CSRs */
1815 		reg = vsc85xx_csr_ctrl_phy_read(phydev, PHY_MCB_TARGET,
1816 						PHY_S6G_PLL_STATUS);
1817 		if (reg == 0xffffffff) {
1818 			mutex_unlock(&phydev->mdio.bus->mdio_lock);
1819 			return -EIO;
1820 		}
1821 
1822 	} while (time_before(jiffies, deadline) && (reg & BIT(12)));
1823 
1824 	if (reg & BIT(12)) {
1825 		mutex_unlock(&phydev->mdio.bus->mdio_lock);
1826 		return -ETIMEDOUT;
1827 	}
1828 
1829 	/* misccfg */
1830 	ret = vsc85xx_csr_ctrl_phy_write(phydev, PHY_MCB_TARGET,
1831 					 PHY_S6G_MISC_CFG, 0);
1832 	if (ret)
1833 		goto err;
1834 
1835 	phy_commit_mcb_s6g(phydev, PHY_MCB_S6G_CFG, 0);
1836 
1837 	deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
1838 	do {
1839 		usleep_range(500, 1000);
1840 		phy_update_mcb_s6g(phydev, PHY_MCB_S6G_CFG,
1841 				   0); /* read 6G MCB into CSRs */
1842 		reg = vsc85xx_csr_ctrl_phy_read(phydev, PHY_MCB_TARGET,
1843 						PHY_S6G_IB_STATUS0);
1844 		if (reg == 0xffffffff) {
1845 			mutex_unlock(&phydev->mdio.bus->mdio_lock);
1846 			return -EIO;
1847 		}
1848 
1849 	} while (time_before(jiffies, deadline) && !(reg & BIT(8)));
1850 
1851 	if (!(reg & BIT(8))) {
1852 		mutex_unlock(&phydev->mdio.bus->mdio_lock);
1853 		return -ETIMEDOUT;
1854 	}
1855 
1856 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
1857 
1858 	ret = phy_write(phydev, MSCC_EXT_PAGE_ACCESS, MSCC_PHY_PAGE_STANDARD);
1859 
1860 	if (ret)
1861 		return ret;
1862 
1863 	ret = phy_modify(phydev, MSCC_PHY_EXT_PHY_CNTL_1, MEDIA_OP_MODE_MASK,
1864 			 MEDIA_OP_MODE_COPPER << MEDIA_OP_MODE_POS);
1865 
1866 	if (ret)
1867 		return ret;
1868 
1869 	ret = genphy_soft_reset(phydev);
1870 
1871 	if (ret)
1872 		return ret;
1873 
1874 	for (i = 0; i < vsc8531->nleds; i++) {
1875 		ret = vsc85xx_led_cntl_set(phydev, i, vsc8531->leds_mode[i]);
1876 		if (ret)
1877 			return ret;
1878 	}
1879 
1880 	return ret;
1881 
1882 err:
1883 	mutex_unlock(&phydev->mdio.bus->mdio_lock);
1884 	return ret;
1885 }
1886 
1887 static int vsc85xx_ack_interrupt(struct phy_device *phydev)
1888 {
1889 	int rc = 0;
1890 
1891 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
1892 		rc = phy_read(phydev, MII_VSC85XX_INT_STATUS);
1893 
1894 	return (rc < 0) ? rc : 0;
1895 }
1896 
1897 static int vsc85xx_config_intr(struct phy_device *phydev)
1898 {
1899 	int rc;
1900 
1901 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1902 		vsc8584_config_macsec_intr(phydev);
1903 
1904 		rc = phy_write(phydev, MII_VSC85XX_INT_MASK,
1905 			       MII_VSC85XX_INT_MASK_MASK);
1906 	} else {
1907 		rc = phy_write(phydev, MII_VSC85XX_INT_MASK, 0);
1908 		if (rc < 0)
1909 			return rc;
1910 		rc = phy_read(phydev, MII_VSC85XX_INT_STATUS);
1911 	}
1912 
1913 	return rc;
1914 }
1915 
1916 static int vsc85xx_config_aneg(struct phy_device *phydev)
1917 {
1918 	int rc;
1919 
1920 	rc = vsc85xx_mdix_set(phydev, phydev->mdix_ctrl);
1921 	if (rc < 0)
1922 		return rc;
1923 
1924 	return genphy_config_aneg(phydev);
1925 }
1926 
1927 static int vsc85xx_read_status(struct phy_device *phydev)
1928 {
1929 	int rc;
1930 
1931 	rc = vsc85xx_mdix_get(phydev, &phydev->mdix);
1932 	if (rc < 0)
1933 		return rc;
1934 
1935 	return genphy_read_status(phydev);
1936 }
1937 
1938 static int vsc8514_probe(struct phy_device *phydev)
1939 {
1940 	struct vsc8531_private *vsc8531;
1941 	u32 default_mode[4] = {VSC8531_LINK_1000_ACTIVITY,
1942 	   VSC8531_LINK_100_ACTIVITY, VSC8531_LINK_ACTIVITY,
1943 	   VSC8531_DUPLEX_COLLISION};
1944 
1945 	vsc8531 = devm_kzalloc(&phydev->mdio.dev, sizeof(*vsc8531), GFP_KERNEL);
1946 	if (!vsc8531)
1947 		return -ENOMEM;
1948 
1949 	phydev->priv = vsc8531;
1950 
1951 	vsc8584_get_base_addr(phydev);
1952 	devm_phy_package_join(&phydev->mdio.dev, phydev,
1953 			      vsc8531->base_addr, 0);
1954 
1955 	vsc8531->nleds = 4;
1956 	vsc8531->supp_led_modes = VSC85XX_SUPP_LED_MODES;
1957 	vsc8531->hw_stats = vsc85xx_hw_stats;
1958 	vsc8531->nstats = ARRAY_SIZE(vsc85xx_hw_stats);
1959 	vsc8531->stats = devm_kcalloc(&phydev->mdio.dev, vsc8531->nstats,
1960 				      sizeof(u64), GFP_KERNEL);
1961 	if (!vsc8531->stats)
1962 		return -ENOMEM;
1963 
1964 	return vsc85xx_dt_led_modes_get(phydev, default_mode);
1965 }
1966 
1967 static int vsc8574_probe(struct phy_device *phydev)
1968 {
1969 	struct vsc8531_private *vsc8531;
1970 	u32 default_mode[4] = {VSC8531_LINK_1000_ACTIVITY,
1971 	   VSC8531_LINK_100_ACTIVITY, VSC8531_LINK_ACTIVITY,
1972 	   VSC8531_DUPLEX_COLLISION};
1973 
1974 	vsc8531 = devm_kzalloc(&phydev->mdio.dev, sizeof(*vsc8531), GFP_KERNEL);
1975 	if (!vsc8531)
1976 		return -ENOMEM;
1977 
1978 	phydev->priv = vsc8531;
1979 
1980 	vsc8584_get_base_addr(phydev);
1981 	devm_phy_package_join(&phydev->mdio.dev, phydev,
1982 			      vsc8531->base_addr, 0);
1983 
1984 	vsc8531->nleds = 4;
1985 	vsc8531->supp_led_modes = VSC8584_SUPP_LED_MODES;
1986 	vsc8531->hw_stats = vsc8584_hw_stats;
1987 	vsc8531->nstats = ARRAY_SIZE(vsc8584_hw_stats);
1988 	vsc8531->stats = devm_kcalloc(&phydev->mdio.dev, vsc8531->nstats,
1989 				      sizeof(u64), GFP_KERNEL);
1990 	if (!vsc8531->stats)
1991 		return -ENOMEM;
1992 
1993 	return vsc85xx_dt_led_modes_get(phydev, default_mode);
1994 }
1995 
1996 static int vsc8584_probe(struct phy_device *phydev)
1997 {
1998 	struct vsc8531_private *vsc8531;
1999 	u32 default_mode[4] = {VSC8531_LINK_1000_ACTIVITY,
2000 	   VSC8531_LINK_100_ACTIVITY, VSC8531_LINK_ACTIVITY,
2001 	   VSC8531_DUPLEX_COLLISION};
2002 
2003 	if ((phydev->phy_id & MSCC_DEV_REV_MASK) != VSC8584_REVB) {
2004 		dev_err(&phydev->mdio.dev, "Only VSC8584 revB is supported.\n");
2005 		return -ENOTSUPP;
2006 	}
2007 
2008 	vsc8531 = devm_kzalloc(&phydev->mdio.dev, sizeof(*vsc8531), GFP_KERNEL);
2009 	if (!vsc8531)
2010 		return -ENOMEM;
2011 
2012 	phydev->priv = vsc8531;
2013 
2014 	vsc8584_get_base_addr(phydev);
2015 	devm_phy_package_join(&phydev->mdio.dev, phydev,
2016 			      vsc8531->base_addr, 0);
2017 
2018 	vsc8531->nleds = 4;
2019 	vsc8531->supp_led_modes = VSC8584_SUPP_LED_MODES;
2020 	vsc8531->hw_stats = vsc8584_hw_stats;
2021 	vsc8531->nstats = ARRAY_SIZE(vsc8584_hw_stats);
2022 	vsc8531->stats = devm_kcalloc(&phydev->mdio.dev, vsc8531->nstats,
2023 				      sizeof(u64), GFP_KERNEL);
2024 	if (!vsc8531->stats)
2025 		return -ENOMEM;
2026 
2027 	return vsc85xx_dt_led_modes_get(phydev, default_mode);
2028 }
2029 
2030 static int vsc85xx_probe(struct phy_device *phydev)
2031 {
2032 	struct vsc8531_private *vsc8531;
2033 	int rate_magic;
2034 	u32 default_mode[2] = {VSC8531_LINK_1000_ACTIVITY,
2035 	   VSC8531_LINK_100_ACTIVITY};
2036 
2037 	rate_magic = vsc85xx_edge_rate_magic_get(phydev);
2038 	if (rate_magic < 0)
2039 		return rate_magic;
2040 
2041 	vsc8531 = devm_kzalloc(&phydev->mdio.dev, sizeof(*vsc8531), GFP_KERNEL);
2042 	if (!vsc8531)
2043 		return -ENOMEM;
2044 
2045 	phydev->priv = vsc8531;
2046 
2047 	vsc8531->rate_magic = rate_magic;
2048 	vsc8531->nleds = 2;
2049 	vsc8531->supp_led_modes = VSC85XX_SUPP_LED_MODES;
2050 	vsc8531->hw_stats = vsc85xx_hw_stats;
2051 	vsc8531->nstats = ARRAY_SIZE(vsc85xx_hw_stats);
2052 	vsc8531->stats = devm_kcalloc(&phydev->mdio.dev, vsc8531->nstats,
2053 				      sizeof(u64), GFP_KERNEL);
2054 	if (!vsc8531->stats)
2055 		return -ENOMEM;
2056 
2057 	return vsc85xx_dt_led_modes_get(phydev, default_mode);
2058 }
2059 
2060 /* Microsemi VSC85xx PHYs */
2061 static struct phy_driver vsc85xx_driver[] = {
2062 {
2063 	.phy_id		= PHY_ID_VSC8502,
2064 	.name		= "Microsemi GE VSC8502 SyncE",
2065 	.phy_id_mask	= 0xfffffff0,
2066 	/* PHY_BASIC_FEATURES */
2067 	.soft_reset	= &genphy_soft_reset,
2068 	.config_init	= &vsc85xx_config_init,
2069 	.config_aneg    = &vsc85xx_config_aneg,
2070 	.read_status	= &vsc85xx_read_status,
2071 	.ack_interrupt	= &vsc85xx_ack_interrupt,
2072 	.config_intr	= &vsc85xx_config_intr,
2073 	.suspend	= &genphy_suspend,
2074 	.resume		= &genphy_resume,
2075 	.probe		= &vsc85xx_probe,
2076 	.set_wol	= &vsc85xx_wol_set,
2077 	.get_wol	= &vsc85xx_wol_get,
2078 	.get_tunable	= &vsc85xx_get_tunable,
2079 	.set_tunable	= &vsc85xx_set_tunable,
2080 	.read_page	= &vsc85xx_phy_read_page,
2081 	.write_page	= &vsc85xx_phy_write_page,
2082 	.get_sset_count = &vsc85xx_get_sset_count,
2083 	.get_strings    = &vsc85xx_get_strings,
2084 	.get_stats      = &vsc85xx_get_stats,
2085 },
2086 {
2087 	.phy_id		= PHY_ID_VSC8504,
2088 	.name		= "Microsemi GE VSC8504 SyncE",
2089 	.phy_id_mask	= 0xfffffff0,
2090 	/* PHY_GBIT_FEATURES */
2091 	.soft_reset	= &genphy_soft_reset,
2092 	.config_init    = &vsc8584_config_init,
2093 	.config_aneg    = &vsc85xx_config_aneg,
2094 	.aneg_done	= &genphy_aneg_done,
2095 	.read_status	= &vsc85xx_read_status,
2096 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2097 	.config_intr    = &vsc85xx_config_intr,
2098 	.did_interrupt  = &vsc8584_did_interrupt,
2099 	.suspend	= &genphy_suspend,
2100 	.resume		= &genphy_resume,
2101 	.probe		= &vsc8574_probe,
2102 	.set_wol	= &vsc85xx_wol_set,
2103 	.get_wol	= &vsc85xx_wol_get,
2104 	.get_tunable	= &vsc85xx_get_tunable,
2105 	.set_tunable	= &vsc85xx_set_tunable,
2106 	.read_page	= &vsc85xx_phy_read_page,
2107 	.write_page	= &vsc85xx_phy_write_page,
2108 	.get_sset_count = &vsc85xx_get_sset_count,
2109 	.get_strings    = &vsc85xx_get_strings,
2110 	.get_stats      = &vsc85xx_get_stats,
2111 },
2112 {
2113 	.phy_id		= PHY_ID_VSC8514,
2114 	.name		= "Microsemi GE VSC8514 SyncE",
2115 	.phy_id_mask	= 0xfffffff0,
2116 	.soft_reset	= &genphy_soft_reset,
2117 	.config_init    = &vsc8514_config_init,
2118 	.config_aneg    = &vsc85xx_config_aneg,
2119 	.read_status	= &vsc85xx_read_status,
2120 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2121 	.config_intr    = &vsc85xx_config_intr,
2122 	.suspend	= &genphy_suspend,
2123 	.resume		= &genphy_resume,
2124 	.probe		= &vsc8514_probe,
2125 	.set_wol	= &vsc85xx_wol_set,
2126 	.get_wol	= &vsc85xx_wol_get,
2127 	.get_tunable	= &vsc85xx_get_tunable,
2128 	.set_tunable	= &vsc85xx_set_tunable,
2129 	.read_page      = &vsc85xx_phy_read_page,
2130 	.write_page     = &vsc85xx_phy_write_page,
2131 	.get_sset_count = &vsc85xx_get_sset_count,
2132 	.get_strings    = &vsc85xx_get_strings,
2133 	.get_stats      = &vsc85xx_get_stats,
2134 },
2135 {
2136 	.phy_id		= PHY_ID_VSC8530,
2137 	.name		= "Microsemi FE VSC8530",
2138 	.phy_id_mask	= 0xfffffff0,
2139 	/* PHY_BASIC_FEATURES */
2140 	.soft_reset	= &genphy_soft_reset,
2141 	.config_init	= &vsc85xx_config_init,
2142 	.config_aneg    = &vsc85xx_config_aneg,
2143 	.read_status	= &vsc85xx_read_status,
2144 	.ack_interrupt	= &vsc85xx_ack_interrupt,
2145 	.config_intr	= &vsc85xx_config_intr,
2146 	.suspend	= &genphy_suspend,
2147 	.resume		= &genphy_resume,
2148 	.probe		= &vsc85xx_probe,
2149 	.set_wol	= &vsc85xx_wol_set,
2150 	.get_wol	= &vsc85xx_wol_get,
2151 	.get_tunable	= &vsc85xx_get_tunable,
2152 	.set_tunable	= &vsc85xx_set_tunable,
2153 	.read_page	= &vsc85xx_phy_read_page,
2154 	.write_page	= &vsc85xx_phy_write_page,
2155 	.get_sset_count = &vsc85xx_get_sset_count,
2156 	.get_strings    = &vsc85xx_get_strings,
2157 	.get_stats      = &vsc85xx_get_stats,
2158 },
2159 {
2160 	.phy_id		= PHY_ID_VSC8531,
2161 	.name		= "Microsemi VSC8531",
2162 	.phy_id_mask    = 0xfffffff0,
2163 	/* PHY_GBIT_FEATURES */
2164 	.soft_reset	= &genphy_soft_reset,
2165 	.config_init    = &vsc85xx_config_init,
2166 	.config_aneg    = &vsc85xx_config_aneg,
2167 	.read_status	= &vsc85xx_read_status,
2168 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2169 	.config_intr    = &vsc85xx_config_intr,
2170 	.suspend	= &genphy_suspend,
2171 	.resume		= &genphy_resume,
2172 	.probe		= &vsc85xx_probe,
2173 	.set_wol	= &vsc85xx_wol_set,
2174 	.get_wol	= &vsc85xx_wol_get,
2175 	.get_tunable	= &vsc85xx_get_tunable,
2176 	.set_tunable	= &vsc85xx_set_tunable,
2177 	.read_page	= &vsc85xx_phy_read_page,
2178 	.write_page	= &vsc85xx_phy_write_page,
2179 	.get_sset_count = &vsc85xx_get_sset_count,
2180 	.get_strings    = &vsc85xx_get_strings,
2181 	.get_stats      = &vsc85xx_get_stats,
2182 },
2183 {
2184 	.phy_id		= PHY_ID_VSC8540,
2185 	.name		= "Microsemi FE VSC8540 SyncE",
2186 	.phy_id_mask	= 0xfffffff0,
2187 	/* PHY_BASIC_FEATURES */
2188 	.soft_reset	= &genphy_soft_reset,
2189 	.config_init	= &vsc85xx_config_init,
2190 	.config_aneg	= &vsc85xx_config_aneg,
2191 	.read_status	= &vsc85xx_read_status,
2192 	.ack_interrupt	= &vsc85xx_ack_interrupt,
2193 	.config_intr	= &vsc85xx_config_intr,
2194 	.suspend	= &genphy_suspend,
2195 	.resume		= &genphy_resume,
2196 	.probe		= &vsc85xx_probe,
2197 	.set_wol	= &vsc85xx_wol_set,
2198 	.get_wol	= &vsc85xx_wol_get,
2199 	.get_tunable	= &vsc85xx_get_tunable,
2200 	.set_tunable	= &vsc85xx_set_tunable,
2201 	.read_page	= &vsc85xx_phy_read_page,
2202 	.write_page	= &vsc85xx_phy_write_page,
2203 	.get_sset_count = &vsc85xx_get_sset_count,
2204 	.get_strings    = &vsc85xx_get_strings,
2205 	.get_stats      = &vsc85xx_get_stats,
2206 },
2207 {
2208 	.phy_id		= PHY_ID_VSC8541,
2209 	.name		= "Microsemi VSC8541 SyncE",
2210 	.phy_id_mask    = 0xfffffff0,
2211 	/* PHY_GBIT_FEATURES */
2212 	.soft_reset	= &genphy_soft_reset,
2213 	.config_init    = &vsc85xx_config_init,
2214 	.config_aneg    = &vsc85xx_config_aneg,
2215 	.read_status	= &vsc85xx_read_status,
2216 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2217 	.config_intr    = &vsc85xx_config_intr,
2218 	.suspend	= &genphy_suspend,
2219 	.resume		= &genphy_resume,
2220 	.probe		= &vsc85xx_probe,
2221 	.set_wol	= &vsc85xx_wol_set,
2222 	.get_wol	= &vsc85xx_wol_get,
2223 	.get_tunable	= &vsc85xx_get_tunable,
2224 	.set_tunable	= &vsc85xx_set_tunable,
2225 	.read_page	= &vsc85xx_phy_read_page,
2226 	.write_page	= &vsc85xx_phy_write_page,
2227 	.get_sset_count = &vsc85xx_get_sset_count,
2228 	.get_strings    = &vsc85xx_get_strings,
2229 	.get_stats      = &vsc85xx_get_stats,
2230 },
2231 {
2232 	.phy_id		= PHY_ID_VSC8552,
2233 	.name		= "Microsemi GE VSC8552 SyncE",
2234 	.phy_id_mask	= 0xfffffff0,
2235 	/* PHY_GBIT_FEATURES */
2236 	.soft_reset	= &genphy_soft_reset,
2237 	.config_init    = &vsc8584_config_init,
2238 	.config_aneg    = &vsc85xx_config_aneg,
2239 	.read_status	= &vsc85xx_read_status,
2240 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2241 	.config_intr    = &vsc85xx_config_intr,
2242 	.did_interrupt  = &vsc8584_did_interrupt,
2243 	.suspend	= &genphy_suspend,
2244 	.resume		= &genphy_resume,
2245 	.probe		= &vsc8574_probe,
2246 	.set_wol	= &vsc85xx_wol_set,
2247 	.get_wol	= &vsc85xx_wol_get,
2248 	.get_tunable	= &vsc85xx_get_tunable,
2249 	.set_tunable	= &vsc85xx_set_tunable,
2250 	.read_page	= &vsc85xx_phy_read_page,
2251 	.write_page	= &vsc85xx_phy_write_page,
2252 	.get_sset_count = &vsc85xx_get_sset_count,
2253 	.get_strings    = &vsc85xx_get_strings,
2254 	.get_stats      = &vsc85xx_get_stats,
2255 },
2256 {
2257 	.phy_id		= PHY_ID_VSC856X,
2258 	.name		= "Microsemi GE VSC856X SyncE",
2259 	.phy_id_mask	= 0xfffffff0,
2260 	/* PHY_GBIT_FEATURES */
2261 	.soft_reset	= &genphy_soft_reset,
2262 	.config_init    = &vsc8584_config_init,
2263 	.config_aneg    = &vsc85xx_config_aneg,
2264 	.read_status	= &vsc85xx_read_status,
2265 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2266 	.config_intr    = &vsc85xx_config_intr,
2267 	.did_interrupt  = &vsc8584_did_interrupt,
2268 	.suspend	= &genphy_suspend,
2269 	.resume		= &genphy_resume,
2270 	.probe		= &vsc8584_probe,
2271 	.get_tunable	= &vsc85xx_get_tunable,
2272 	.set_tunable	= &vsc85xx_set_tunable,
2273 	.read_page	= &vsc85xx_phy_read_page,
2274 	.write_page	= &vsc85xx_phy_write_page,
2275 	.get_sset_count = &vsc85xx_get_sset_count,
2276 	.get_strings    = &vsc85xx_get_strings,
2277 	.get_stats      = &vsc85xx_get_stats,
2278 },
2279 {
2280 	.phy_id		= PHY_ID_VSC8572,
2281 	.name		= "Microsemi GE VSC8572 SyncE",
2282 	.phy_id_mask	= 0xfffffff0,
2283 	/* PHY_GBIT_FEATURES */
2284 	.soft_reset	= &genphy_soft_reset,
2285 	.config_init    = &vsc8584_config_init,
2286 	.config_aneg    = &vsc85xx_config_aneg,
2287 	.aneg_done	= &genphy_aneg_done,
2288 	.read_status	= &vsc85xx_read_status,
2289 	.handle_interrupt = &vsc8584_handle_interrupt,
2290 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2291 	.config_intr    = &vsc85xx_config_intr,
2292 	.did_interrupt  = &vsc8584_did_interrupt,
2293 	.suspend	= &genphy_suspend,
2294 	.resume		= &genphy_resume,
2295 	.probe		= &vsc8574_probe,
2296 	.set_wol	= &vsc85xx_wol_set,
2297 	.get_wol	= &vsc85xx_wol_get,
2298 	.get_tunable	= &vsc85xx_get_tunable,
2299 	.set_tunable	= &vsc85xx_set_tunable,
2300 	.read_page	= &vsc85xx_phy_read_page,
2301 	.write_page	= &vsc85xx_phy_write_page,
2302 	.get_sset_count = &vsc85xx_get_sset_count,
2303 	.get_strings    = &vsc85xx_get_strings,
2304 	.get_stats      = &vsc85xx_get_stats,
2305 },
2306 {
2307 	.phy_id		= PHY_ID_VSC8574,
2308 	.name		= "Microsemi GE VSC8574 SyncE",
2309 	.phy_id_mask	= 0xfffffff0,
2310 	/* PHY_GBIT_FEATURES */
2311 	.soft_reset	= &genphy_soft_reset,
2312 	.config_init    = &vsc8584_config_init,
2313 	.config_aneg    = &vsc85xx_config_aneg,
2314 	.aneg_done	= &genphy_aneg_done,
2315 	.read_status	= &vsc85xx_read_status,
2316 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2317 	.config_intr    = &vsc85xx_config_intr,
2318 	.did_interrupt  = &vsc8584_did_interrupt,
2319 	.suspend	= &genphy_suspend,
2320 	.resume		= &genphy_resume,
2321 	.probe		= &vsc8574_probe,
2322 	.set_wol	= &vsc85xx_wol_set,
2323 	.get_wol	= &vsc85xx_wol_get,
2324 	.get_tunable	= &vsc85xx_get_tunable,
2325 	.set_tunable	= &vsc85xx_set_tunable,
2326 	.read_page	= &vsc85xx_phy_read_page,
2327 	.write_page	= &vsc85xx_phy_write_page,
2328 	.get_sset_count = &vsc85xx_get_sset_count,
2329 	.get_strings    = &vsc85xx_get_strings,
2330 	.get_stats      = &vsc85xx_get_stats,
2331 },
2332 {
2333 	.phy_id		= PHY_ID_VSC8575,
2334 	.name		= "Microsemi GE VSC8575 SyncE",
2335 	.phy_id_mask	= 0xfffffff0,
2336 	/* PHY_GBIT_FEATURES */
2337 	.soft_reset	= &genphy_soft_reset,
2338 	.config_init    = &vsc8584_config_init,
2339 	.config_aneg    = &vsc85xx_config_aneg,
2340 	.aneg_done	= &genphy_aneg_done,
2341 	.read_status	= &vsc85xx_read_status,
2342 	.handle_interrupt = &vsc8584_handle_interrupt,
2343 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2344 	.config_intr    = &vsc85xx_config_intr,
2345 	.did_interrupt  = &vsc8584_did_interrupt,
2346 	.suspend	= &genphy_suspend,
2347 	.resume		= &genphy_resume,
2348 	.probe		= &vsc8584_probe,
2349 	.get_tunable	= &vsc85xx_get_tunable,
2350 	.set_tunable	= &vsc85xx_set_tunable,
2351 	.read_page	= &vsc85xx_phy_read_page,
2352 	.write_page	= &vsc85xx_phy_write_page,
2353 	.get_sset_count = &vsc85xx_get_sset_count,
2354 	.get_strings    = &vsc85xx_get_strings,
2355 	.get_stats      = &vsc85xx_get_stats,
2356 },
2357 {
2358 	.phy_id		= PHY_ID_VSC8582,
2359 	.name		= "Microsemi GE VSC8582 SyncE",
2360 	.phy_id_mask	= 0xfffffff0,
2361 	/* PHY_GBIT_FEATURES */
2362 	.soft_reset	= &genphy_soft_reset,
2363 	.config_init    = &vsc8584_config_init,
2364 	.config_aneg    = &vsc85xx_config_aneg,
2365 	.aneg_done	= &genphy_aneg_done,
2366 	.read_status	= &vsc85xx_read_status,
2367 	.handle_interrupt = &vsc8584_handle_interrupt,
2368 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2369 	.config_intr    = &vsc85xx_config_intr,
2370 	.did_interrupt  = &vsc8584_did_interrupt,
2371 	.suspend	= &genphy_suspend,
2372 	.resume		= &genphy_resume,
2373 	.probe		= &vsc8584_probe,
2374 	.get_tunable	= &vsc85xx_get_tunable,
2375 	.set_tunable	= &vsc85xx_set_tunable,
2376 	.read_page	= &vsc85xx_phy_read_page,
2377 	.write_page	= &vsc85xx_phy_write_page,
2378 	.get_sset_count = &vsc85xx_get_sset_count,
2379 	.get_strings    = &vsc85xx_get_strings,
2380 	.get_stats      = &vsc85xx_get_stats,
2381 },
2382 {
2383 	.phy_id		= PHY_ID_VSC8584,
2384 	.name		= "Microsemi GE VSC8584 SyncE",
2385 	.phy_id_mask	= 0xfffffff0,
2386 	/* PHY_GBIT_FEATURES */
2387 	.soft_reset	= &genphy_soft_reset,
2388 	.config_init    = &vsc8584_config_init,
2389 	.config_aneg    = &vsc85xx_config_aneg,
2390 	.aneg_done	= &genphy_aneg_done,
2391 	.read_status	= &vsc85xx_read_status,
2392 	.handle_interrupt = &vsc8584_handle_interrupt,
2393 	.ack_interrupt  = &vsc85xx_ack_interrupt,
2394 	.config_intr    = &vsc85xx_config_intr,
2395 	.did_interrupt  = &vsc8584_did_interrupt,
2396 	.suspend	= &genphy_suspend,
2397 	.resume		= &genphy_resume,
2398 	.probe		= &vsc8584_probe,
2399 	.get_tunable	= &vsc85xx_get_tunable,
2400 	.set_tunable	= &vsc85xx_set_tunable,
2401 	.read_page	= &vsc85xx_phy_read_page,
2402 	.write_page	= &vsc85xx_phy_write_page,
2403 	.get_sset_count = &vsc85xx_get_sset_count,
2404 	.get_strings    = &vsc85xx_get_strings,
2405 	.get_stats      = &vsc85xx_get_stats,
2406 }
2407 
2408 };
2409 
2410 module_phy_driver(vsc85xx_driver);
2411 
2412 static struct mdio_device_id __maybe_unused vsc85xx_tbl[] = {
2413 	{ PHY_ID_VSC8504, 0xfffffff0, },
2414 	{ PHY_ID_VSC8514, 0xfffffff0, },
2415 	{ PHY_ID_VSC8530, 0xfffffff0, },
2416 	{ PHY_ID_VSC8531, 0xfffffff0, },
2417 	{ PHY_ID_VSC8540, 0xfffffff0, },
2418 	{ PHY_ID_VSC8541, 0xfffffff0, },
2419 	{ PHY_ID_VSC8552, 0xfffffff0, },
2420 	{ PHY_ID_VSC856X, 0xfffffff0, },
2421 	{ PHY_ID_VSC8572, 0xfffffff0, },
2422 	{ PHY_ID_VSC8574, 0xfffffff0, },
2423 	{ PHY_ID_VSC8575, 0xfffffff0, },
2424 	{ PHY_ID_VSC8582, 0xfffffff0, },
2425 	{ PHY_ID_VSC8584, 0xfffffff0, },
2426 	{ }
2427 };
2428 
2429 MODULE_DEVICE_TABLE(mdio, vsc85xx_tbl);
2430 
2431 MODULE_DESCRIPTION("Microsemi VSC85xx PHY driver");
2432 MODULE_AUTHOR("Nagaraju Lakkaraju");
2433 MODULE_LICENSE("Dual MIT/GPL");
2434