xref: /linux/drivers/net/phy/marvell10g.c (revision 3d3ced2ec5d71b99d72ae6910fbdf890bc2eccf0)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
220b2af32SRussell King /*
320b2af32SRussell King  * Marvell 10G 88x3310 PHY driver
420b2af32SRussell King  *
520b2af32SRussell King  * Based upon the ID registers, this PHY appears to be a mixture of IPs
620b2af32SRussell King  * from two different companies.
720b2af32SRussell King  *
820b2af32SRussell King  * There appears to be several different data paths through the PHY which
920b2af32SRussell King  * are automatically managed by the PHY.  The following has been determined
1005ca1b32SRussell King  * via observation and experimentation for a setup using single-lane Serdes:
1120b2af32SRussell King  *
1220b2af32SRussell King  *       SGMII PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for <= 1G)
1320b2af32SRussell King  *  10GBASE-KR PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for 10G)
1420b2af32SRussell King  *  10GBASE-KR PHYXS -- BASE-R PCS -- Fiber
1520b2af32SRussell King  *
1605ca1b32SRussell King  * With XAUI, observation shows:
1705ca1b32SRussell King  *
1805ca1b32SRussell King  *        XAUI PHYXS -- <appropriate PCS as above>
1905ca1b32SRussell King  *
2005ca1b32SRussell King  * and no switching of the host interface mode occurs.
2105ca1b32SRussell King  *
2220b2af32SRussell King  * If both the fiber and copper ports are connected, the first to gain
2320b2af32SRussell King  * link takes priority and the other port is completely locked out.
2420b2af32SRussell King  */
250d3ad854SRussell King #include <linux/ctype.h>
260d3ad854SRussell King #include <linux/hwmon.h>
27952b6b3bSAntoine Tenart #include <linux/marvell_phy.h>
280d3ad854SRussell King #include <linux/phy.h>
2920b2af32SRussell King 
30c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_MASK	0xfffffffe
31c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_REV	(MARVELL_PHY_ID_88X3310 | 0xa)
32c47455f9SMaxime Chevallier 
3320b2af32SRussell King enum {
34*3d3ced2eSRussell King 	MV_PMA_BOOT		= 0xc050,
35*3d3ced2eSRussell King 	MV_PMA_BOOT_FATAL	= BIT(0),
36*3d3ced2eSRussell King 
3720b2af32SRussell King 	MV_PCS_BASE_T		= 0x0000,
3820b2af32SRussell King 	MV_PCS_BASE_R		= 0x1000,
3920b2af32SRussell King 	MV_PCS_1000BASEX	= 0x2000,
4020b2af32SRussell King 
41ea4efe25SRussell King 	MV_PCS_PAIRSWAP		= 0x8182,
42ea4efe25SRussell King 	MV_PCS_PAIRSWAP_MASK	= 0x0003,
43ea4efe25SRussell King 	MV_PCS_PAIRSWAP_AB	= 0x0002,
44ea4efe25SRussell King 	MV_PCS_PAIRSWAP_NONE	= 0x0003,
45ea4efe25SRussell King 
4620b2af32SRussell King 	/* These registers appear at 0x800X and 0xa00X - the 0xa00X control
4720b2af32SRussell King 	 * registers appear to set themselves to the 0x800X when AN is
4820b2af32SRussell King 	 * restarted, but status registers appear readable from either.
4920b2af32SRussell King 	 */
5020b2af32SRussell King 	MV_AN_CTRL1000		= 0x8000, /* 1000base-T control register */
5120b2af32SRussell King 	MV_AN_STAT1000		= 0x8001, /* 1000base-T status register */
520d3ad854SRussell King 
530d3ad854SRussell King 	/* Vendor2 MMD registers */
54af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL		= 0xf001,
55af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL_PWRDOWN = 0x0800,
560d3ad854SRussell King 	MV_V2_TEMP_CTRL		= 0xf08a,
570d3ad854SRussell King 	MV_V2_TEMP_CTRL_MASK	= 0xc000,
580d3ad854SRussell King 	MV_V2_TEMP_CTRL_SAMPLE	= 0x0000,
590d3ad854SRussell King 	MV_V2_TEMP_CTRL_DISABLE	= 0xc000,
600d3ad854SRussell King 	MV_V2_TEMP		= 0xf08c,
610d3ad854SRussell King 	MV_V2_TEMP_UNKNOWN	= 0x9600, /* unknown function */
620d3ad854SRussell King };
630d3ad854SRussell King 
640d3ad854SRussell King struct mv3310_priv {
650d3ad854SRussell King 	struct device *hwmon_dev;
660d3ad854SRussell King 	char *hwmon_name;
6720b2af32SRussell King };
6820b2af32SRussell King 
690d3ad854SRussell King #ifdef CONFIG_HWMON
700d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data,
710d3ad854SRussell King 				       enum hwmon_sensor_types type,
720d3ad854SRussell King 				       u32 attr, int channel)
730d3ad854SRussell King {
740d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
750d3ad854SRussell King 		return 0444;
760d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input)
770d3ad854SRussell King 		return 0444;
780d3ad854SRussell King 	return 0;
790d3ad854SRussell King }
800d3ad854SRussell King 
810d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
820d3ad854SRussell King 			     u32 attr, int channel, long *value)
830d3ad854SRussell King {
840d3ad854SRussell King 	struct phy_device *phydev = dev_get_drvdata(dev);
850d3ad854SRussell King 	int temp;
860d3ad854SRussell King 
870d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
880d3ad854SRussell King 		*value = MSEC_PER_SEC;
890d3ad854SRussell King 		return 0;
900d3ad854SRussell King 	}
910d3ad854SRussell King 
920d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input) {
930d3ad854SRussell King 		temp = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP);
940d3ad854SRussell King 		if (temp < 0)
950d3ad854SRussell King 			return temp;
960d3ad854SRussell King 
970d3ad854SRussell King 		*value = ((temp & 0xff) - 75) * 1000;
980d3ad854SRussell King 
990d3ad854SRussell King 		return 0;
1000d3ad854SRussell King 	}
1010d3ad854SRussell King 
1020d3ad854SRussell King 	return -EOPNOTSUPP;
1030d3ad854SRussell King }
1040d3ad854SRussell King 
1050d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = {
1060d3ad854SRussell King 	.is_visible = mv3310_hwmon_is_visible,
1070d3ad854SRussell King 	.read = mv3310_hwmon_read,
1080d3ad854SRussell King };
1090d3ad854SRussell King 
1100d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = {
1110d3ad854SRussell King 	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
1120d3ad854SRussell King 	0,
1130d3ad854SRussell King };
1140d3ad854SRussell King 
1150d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = {
1160d3ad854SRussell King 	.type = hwmon_chip,
1170d3ad854SRussell King 	.config = mv3310_hwmon_chip_config,
1180d3ad854SRussell King };
1190d3ad854SRussell King 
1200d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = {
1210d3ad854SRussell King 	HWMON_T_INPUT,
1220d3ad854SRussell King 	0,
1230d3ad854SRussell King };
1240d3ad854SRussell King 
1250d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = {
1260d3ad854SRussell King 	.type = hwmon_temp,
1270d3ad854SRussell King 	.config = mv3310_hwmon_temp_config,
1280d3ad854SRussell King };
1290d3ad854SRussell King 
1300d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = {
1310d3ad854SRussell King 	&mv3310_hwmon_chip,
1320d3ad854SRussell King 	&mv3310_hwmon_temp,
1330d3ad854SRussell King 	NULL,
1340d3ad854SRussell King };
1350d3ad854SRussell King 
1360d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = {
1370d3ad854SRussell King 	.ops = &mv3310_hwmon_ops,
1380d3ad854SRussell King 	.info = mv3310_hwmon_info,
1390d3ad854SRussell King };
1400d3ad854SRussell King 
1410d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
1420d3ad854SRussell King {
1430d3ad854SRussell King 	u16 val;
1440d3ad854SRussell King 	int ret;
1450d3ad854SRussell King 
1460d3ad854SRussell King 	ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP,
1470d3ad854SRussell King 			    MV_V2_TEMP_UNKNOWN);
1480d3ad854SRussell King 	if (ret < 0)
1490d3ad854SRussell King 		return ret;
1500d3ad854SRussell King 
1510d3ad854SRussell King 	val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE;
1520d3ad854SRussell King 
153b06d8e5aSHeiner Kallweit 	return phy_modify_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL,
154b06d8e5aSHeiner Kallweit 			      MV_V2_TEMP_CTRL_MASK, val);
1550d3ad854SRussell King }
1560d3ad854SRussell King 
1570d3ad854SRussell King static void mv3310_hwmon_disable(void *data)
1580d3ad854SRussell King {
1590d3ad854SRussell King 	struct phy_device *phydev = data;
1600d3ad854SRussell King 
1610d3ad854SRussell King 	mv3310_hwmon_config(phydev, false);
1620d3ad854SRussell King }
1630d3ad854SRussell King 
1640d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
1650d3ad854SRussell King {
1660d3ad854SRussell King 	struct device *dev = &phydev->mdio.dev;
1670d3ad854SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1680d3ad854SRussell King 	int i, j, ret;
1690d3ad854SRussell King 
1700d3ad854SRussell King 	priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
1710d3ad854SRussell King 	if (!priv->hwmon_name)
1720d3ad854SRussell King 		return -ENODEV;
1730d3ad854SRussell King 
1740d3ad854SRussell King 	for (i = j = 0; priv->hwmon_name[i]; i++) {
1750d3ad854SRussell King 		if (isalnum(priv->hwmon_name[i])) {
1760d3ad854SRussell King 			if (i != j)
1770d3ad854SRussell King 				priv->hwmon_name[j] = priv->hwmon_name[i];
1780d3ad854SRussell King 			j++;
1790d3ad854SRussell King 		}
1800d3ad854SRussell King 	}
1810d3ad854SRussell King 	priv->hwmon_name[j] = '\0';
1820d3ad854SRussell King 
1830d3ad854SRussell King 	ret = mv3310_hwmon_config(phydev, true);
1840d3ad854SRussell King 	if (ret)
1850d3ad854SRussell King 		return ret;
1860d3ad854SRussell King 
1870d3ad854SRussell King 	ret = devm_add_action_or_reset(dev, mv3310_hwmon_disable, phydev);
1880d3ad854SRussell King 	if (ret)
1890d3ad854SRussell King 		return ret;
1900d3ad854SRussell King 
1910d3ad854SRussell King 	priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
1920d3ad854SRussell King 				priv->hwmon_name, phydev,
1930d3ad854SRussell King 				&mv3310_hwmon_chip_info, NULL);
1940d3ad854SRussell King 
1950d3ad854SRussell King 	return PTR_ERR_OR_ZERO(priv->hwmon_dev);
1960d3ad854SRussell King }
1970d3ad854SRussell King #else
1980d3ad854SRussell King static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
1990d3ad854SRussell King {
2000d3ad854SRussell King 	return 0;
2010d3ad854SRussell King }
2020d3ad854SRussell King 
2030d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
2040d3ad854SRussell King {
2050d3ad854SRussell King 	return 0;
2060d3ad854SRussell King }
2070d3ad854SRussell King #endif
2080d3ad854SRussell King 
20920b2af32SRussell King static int mv3310_probe(struct phy_device *phydev)
21020b2af32SRussell King {
2110d3ad854SRussell King 	struct mv3310_priv *priv;
21220b2af32SRussell King 	u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
2130d3ad854SRussell King 	int ret;
21420b2af32SRussell King 
21520b2af32SRussell King 	if (!phydev->is_c45 ||
21620b2af32SRussell King 	    (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
21720b2af32SRussell King 		return -ENODEV;
21820b2af32SRussell King 
219*3d3ced2eSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT);
220*3d3ced2eSRussell King 	if (ret < 0)
221*3d3ced2eSRussell King 		return ret;
222*3d3ced2eSRussell King 
223*3d3ced2eSRussell King 	if (ret & MV_PMA_BOOT_FATAL) {
224*3d3ced2eSRussell King 		dev_warn(&phydev->mdio.dev,
225*3d3ced2eSRussell King 			 "PHY failed to boot firmware, status=%04x\n", ret);
226*3d3ced2eSRussell King 		return -ENODEV;
227*3d3ced2eSRussell King 	}
228*3d3ced2eSRussell King 
2290d3ad854SRussell King 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
2300d3ad854SRussell King 	if (!priv)
2310d3ad854SRussell King 		return -ENOMEM;
2320d3ad854SRussell King 
2330d3ad854SRussell King 	dev_set_drvdata(&phydev->mdio.dev, priv);
2340d3ad854SRussell King 
2350d3ad854SRussell King 	ret = mv3310_hwmon_probe(phydev);
2360d3ad854SRussell King 	if (ret)
2370d3ad854SRussell King 		return ret;
2380d3ad854SRussell King 
23920b2af32SRussell King 	return 0;
24020b2af32SRussell King }
24120b2af32SRussell King 
2420d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev)
2430d3ad854SRussell King {
244af3e28cbSAntoine Tenart 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
245af3e28cbSAntoine Tenart 				MV_V2_PORT_CTRL_PWRDOWN);
2460d3ad854SRussell King }
2470d3ad854SRussell King 
2480d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev)
2490d3ad854SRussell King {
250af3e28cbSAntoine Tenart 	int ret;
251af3e28cbSAntoine Tenart 
252af3e28cbSAntoine Tenart 	ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
253af3e28cbSAntoine Tenart 				 MV_V2_PORT_CTRL_PWRDOWN);
254af3e28cbSAntoine Tenart 	if (ret)
255af3e28cbSAntoine Tenart 		return ret;
256af3e28cbSAntoine Tenart 
2570d3ad854SRussell King 	return mv3310_hwmon_config(phydev, true);
2580d3ad854SRussell King }
2590d3ad854SRussell King 
260c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010
261c47455f9SMaxime Chevallier  * don't set bit 14 in PMA Extended Abilities (1.11), although they do
262c47455f9SMaxime Chevallier  * support 2.5GBASET and 5GBASET. For these models, we can still read their
263c47455f9SMaxime Chevallier  * 2.5G/5G extended abilities register (1.21). We detect these models based on
264c47455f9SMaxime Chevallier  * the PMA device identifier, with a mask matching models known to have this
265c47455f9SMaxime Chevallier  * issue
266c47455f9SMaxime Chevallier  */
267c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev)
268c47455f9SMaxime Chevallier {
269c47455f9SMaxime Chevallier 	if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD))
270c47455f9SMaxime Chevallier 		return false;
271c47455f9SMaxime Chevallier 
272c47455f9SMaxime Chevallier 	/* Only some revisions of the 88X3310 family PMA seem to be impacted */
273c47455f9SMaxime Chevallier 	return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
274c47455f9SMaxime Chevallier 		MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
275c47455f9SMaxime Chevallier }
276c47455f9SMaxime Chevallier 
27720b2af32SRussell King static int mv3310_config_init(struct phy_device *phydev)
27820b2af32SRussell King {
27920b2af32SRussell King 	/* Check that the PHY interface type is compatible */
28020b2af32SRussell King 	if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
281e555e5b1SMaxime Chevallier 	    phydev->interface != PHY_INTERFACE_MODE_2500BASEX &&
28220b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_XAUI &&
28320b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
28420b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_10GKR)
28520b2af32SRussell King 		return -ENODEV;
28620b2af32SRussell King 
28774145424SMaxime Chevallier 	return 0;
28874145424SMaxime Chevallier }
28974145424SMaxime Chevallier 
29074145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev)
29174145424SMaxime Chevallier {
29274145424SMaxime Chevallier 	int ret, val;
29374145424SMaxime Chevallier 
294ac3f5533SMaxime Chevallier 	ret = genphy_c45_pma_read_abilities(phydev);
295ac3f5533SMaxime Chevallier 	if (ret)
296ac3f5533SMaxime Chevallier 		return ret;
29720b2af32SRussell King 
298c47455f9SMaxime Chevallier 	if (mv3310_has_pma_ngbaset_quirk(phydev)) {
299c47455f9SMaxime Chevallier 		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
300c47455f9SMaxime Chevallier 				   MDIO_PMA_NG_EXTABLE);
301c47455f9SMaxime Chevallier 		if (val < 0)
302c47455f9SMaxime Chevallier 			return val;
303c47455f9SMaxime Chevallier 
304c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
305c47455f9SMaxime Chevallier 				 phydev->supported,
306c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_2_5GBT);
307c47455f9SMaxime Chevallier 
308c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
309c47455f9SMaxime Chevallier 				 phydev->supported,
310c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_5GBT);
311c47455f9SMaxime Chevallier 	}
312c47455f9SMaxime Chevallier 
31320b2af32SRussell King 	return 0;
31420b2af32SRussell King }
31520b2af32SRussell King 
31620b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev)
31720b2af32SRussell King {
31820b2af32SRussell King 	bool changed = false;
3193c1bcc86SAndrew Lunn 	u16 reg;
32020b2af32SRussell King 	int ret;
32120b2af32SRussell King 
322ea4efe25SRussell King 	/* We don't support manual MDI control */
323ea4efe25SRussell King 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
324ea4efe25SRussell King 
32530de65c3SHeiner Kallweit 	if (phydev->autoneg == AUTONEG_DISABLE)
32630de65c3SHeiner Kallweit 		return genphy_c45_pma_setup_forced(phydev);
32720b2af32SRussell King 
3283de97f3cSAndrew Lunn 	ret = genphy_c45_an_config_aneg(phydev);
32920b2af32SRussell King 	if (ret < 0)
33020b2af32SRussell King 		return ret;
33120b2af32SRussell King 	if (ret > 0)
33220b2af32SRussell King 		changed = true;
33320b2af32SRussell King 
3343de97f3cSAndrew Lunn 	/* Clause 45 has no standardized support for 1000BaseT, therefore
3353de97f3cSAndrew Lunn 	 * use vendor registers for this mode.
3363de97f3cSAndrew Lunn 	 */
3373c1bcc86SAndrew Lunn 	reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
338b06d8e5aSHeiner Kallweit 	ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
3393c1bcc86SAndrew Lunn 			     ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg);
34020b2af32SRussell King 	if (ret < 0)
34120b2af32SRussell King 		return ret;
34220b2af32SRussell King 	if (ret > 0)
34320b2af32SRussell King 		changed = true;
34420b2af32SRussell King 
3456b4cb6cbSHeiner Kallweit 	return genphy_c45_check_and_restart_aneg(phydev, changed);
34620b2af32SRussell King }
34720b2af32SRussell King 
34820b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev)
34920b2af32SRussell King {
35020b2af32SRussell King 	int val;
35120b2af32SRussell King 
35220b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
35320b2af32SRussell King 	if (val < 0)
35420b2af32SRussell King 		return val;
35520b2af32SRussell King 
35620b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
35720b2af32SRussell King 		return 1;
35820b2af32SRussell King 
35920b2af32SRussell King 	return genphy_c45_aneg_done(phydev);
36020b2af32SRussell King }
36120b2af32SRussell King 
36236c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev)
36336c4449aSRussell King {
36436c4449aSRussell King 	if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
365e555e5b1SMaxime Chevallier 	     phydev->interface == PHY_INTERFACE_MODE_2500BASEX ||
36636c4449aSRussell King 	     phydev->interface == PHY_INTERFACE_MODE_10GKR) && phydev->link) {
36736c4449aSRussell King 		/* The PHY automatically switches its serdes interface (and
368e555e5b1SMaxime Chevallier 		 * active PHYXS instance) between Cisco SGMII, 10GBase-KR and
369e555e5b1SMaxime Chevallier 		 * 2500BaseX modes according to the speed.  Florian suggests
370e555e5b1SMaxime Chevallier 		 * setting phydev->interface to communicate this to the MAC.
371e555e5b1SMaxime Chevallier 		 * Only do this if we are already in one of the above modes.
37236c4449aSRussell King 		 */
373e555e5b1SMaxime Chevallier 		switch (phydev->speed) {
374e555e5b1SMaxime Chevallier 		case SPEED_10000:
37536c4449aSRussell King 			phydev->interface = PHY_INTERFACE_MODE_10GKR;
376e555e5b1SMaxime Chevallier 			break;
377e555e5b1SMaxime Chevallier 		case SPEED_2500:
378e555e5b1SMaxime Chevallier 			phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
379e555e5b1SMaxime Chevallier 			break;
380e555e5b1SMaxime Chevallier 		case SPEED_1000:
381e555e5b1SMaxime Chevallier 		case SPEED_100:
382e555e5b1SMaxime Chevallier 		case SPEED_10:
38336c4449aSRussell King 			phydev->interface = PHY_INTERFACE_MODE_SGMII;
384e555e5b1SMaxime Chevallier 			break;
385e555e5b1SMaxime Chevallier 		default:
386e555e5b1SMaxime Chevallier 			break;
387e555e5b1SMaxime Chevallier 		}
38836c4449aSRussell King 	}
38936c4449aSRussell King }
39036c4449aSRussell King 
39120b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
39220b2af32SRussell King static int mv3310_read_10gbr_status(struct phy_device *phydev)
39320b2af32SRussell King {
39420b2af32SRussell King 	phydev->link = 1;
39520b2af32SRussell King 	phydev->speed = SPEED_10000;
39620b2af32SRussell King 	phydev->duplex = DUPLEX_FULL;
39720b2af32SRussell King 
39836c4449aSRussell King 	mv3310_update_interface(phydev);
39920b2af32SRussell King 
40020b2af32SRussell King 	return 0;
40120b2af32SRussell King }
40220b2af32SRussell King 
40320b2af32SRussell King static int mv3310_read_status(struct phy_device *phydev)
40420b2af32SRussell King {
40520b2af32SRussell King 	int val;
40620b2af32SRussell King 
40720b2af32SRussell King 	phydev->speed = SPEED_UNKNOWN;
40820b2af32SRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
409c0ec3c27SAndrew Lunn 	linkmode_zero(phydev->lp_advertising);
41020b2af32SRussell King 	phydev->link = 0;
41120b2af32SRussell King 	phydev->pause = 0;
41220b2af32SRussell King 	phydev->asym_pause = 0;
413ea4efe25SRussell King 	phydev->mdix = 0;
41420b2af32SRussell King 
41520b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
41620b2af32SRussell King 	if (val < 0)
41720b2af32SRussell King 		return val;
41820b2af32SRussell King 
41920b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
42020b2af32SRussell King 		return mv3310_read_10gbr_status(phydev);
42120b2af32SRussell King 
422998a8a83SHeiner Kallweit 	val = genphy_c45_read_link(phydev);
42320b2af32SRussell King 	if (val < 0)
42420b2af32SRussell King 		return val;
42520b2af32SRussell King 
42620b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
42720b2af32SRussell King 	if (val < 0)
42820b2af32SRussell King 		return val;
42920b2af32SRussell King 
43020b2af32SRussell King 	if (val & MDIO_AN_STAT1_COMPLETE) {
43120b2af32SRussell King 		val = genphy_c45_read_lpa(phydev);
43220b2af32SRussell King 		if (val < 0)
43320b2af32SRussell King 			return val;
43420b2af32SRussell King 
435cc1122b0SColin Ian King 		/* Read the link partner's 1G advertisement */
43620b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
43720b2af32SRussell King 		if (val < 0)
43820b2af32SRussell King 			return val;
43920b2af32SRussell King 
44078a24df3SAndrew Lunn 		mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
44120b2af32SRussell King 
4426798d03cSRussell King 		if (phydev->autoneg == AUTONEG_ENABLE)
4436798d03cSRussell King 			phy_resolve_aneg_linkmode(phydev);
44420b2af32SRussell King 	}
44520b2af32SRussell King 
44620b2af32SRussell King 	if (phydev->autoneg != AUTONEG_ENABLE) {
44720b2af32SRussell King 		val = genphy_c45_read_pma(phydev);
44820b2af32SRussell King 		if (val < 0)
44920b2af32SRussell King 			return val;
45020b2af32SRussell King 	}
45120b2af32SRussell King 
452ea4efe25SRussell King 	if (phydev->speed == SPEED_10000) {
453ea4efe25SRussell King 		val = genphy_c45_read_mdix(phydev);
454ea4efe25SRussell King 		if (val < 0)
455ea4efe25SRussell King 			return val;
456ea4efe25SRussell King 	} else {
457ea4efe25SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_PAIRSWAP);
458ea4efe25SRussell King 		if (val < 0)
459ea4efe25SRussell King 			return val;
460ea4efe25SRussell King 
461ea4efe25SRussell King 		switch (val & MV_PCS_PAIRSWAP_MASK) {
462ea4efe25SRussell King 		case MV_PCS_PAIRSWAP_AB:
463ea4efe25SRussell King 			phydev->mdix = ETH_TP_MDI_X;
464ea4efe25SRussell King 			break;
465ea4efe25SRussell King 		case MV_PCS_PAIRSWAP_NONE:
466ea4efe25SRussell King 			phydev->mdix = ETH_TP_MDI;
467ea4efe25SRussell King 			break;
468ea4efe25SRussell King 		default:
469ea4efe25SRussell King 			phydev->mdix = ETH_TP_MDI_INVALID;
470ea4efe25SRussell King 			break;
471ea4efe25SRussell King 		}
472ea4efe25SRussell King 	}
473ea4efe25SRussell King 
47436c4449aSRussell King 	mv3310_update_interface(phydev);
47520b2af32SRussell King 
47620b2af32SRussell King 	return 0;
47720b2af32SRussell King }
47820b2af32SRussell King 
47920b2af32SRussell King static struct phy_driver mv3310_drivers[] = {
48020b2af32SRussell King 	{
481631ba906SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88X3310,
482952b6b3bSAntoine Tenart 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
48320b2af32SRussell King 		.name		= "mv88x3310",
48474145424SMaxime Chevallier 		.get_features	= mv3310_get_features,
4857be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
48620b2af32SRussell King 		.config_init	= mv3310_config_init,
4870d3ad854SRussell King 		.probe		= mv3310_probe,
4880d3ad854SRussell King 		.suspend	= mv3310_suspend,
4890d3ad854SRussell King 		.resume		= mv3310_resume,
49020b2af32SRussell King 		.config_aneg	= mv3310_config_aneg,
49120b2af32SRussell King 		.aneg_done	= mv3310_aneg_done,
49220b2af32SRussell King 		.read_status	= mv3310_read_status,
49320b2af32SRussell King 	},
49462d01535SMaxime Chevallier 	{
49562d01535SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88E2110,
49662d01535SMaxime Chevallier 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
49762d01535SMaxime Chevallier 		.name		= "mv88x2110",
49862d01535SMaxime Chevallier 		.probe		= mv3310_probe,
499e02c4a9dSAntoine Tenart 		.suspend	= mv3310_suspend,
500e02c4a9dSAntoine Tenart 		.resume		= mv3310_resume,
5017be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
50262d01535SMaxime Chevallier 		.config_init	= mv3310_config_init,
50362d01535SMaxime Chevallier 		.config_aneg	= mv3310_config_aneg,
50462d01535SMaxime Chevallier 		.aneg_done	= mv3310_aneg_done,
50562d01535SMaxime Chevallier 		.read_status	= mv3310_read_status,
50662d01535SMaxime Chevallier 	},
50720b2af32SRussell King };
50820b2af32SRussell King 
50920b2af32SRussell King module_phy_driver(mv3310_drivers);
51020b2af32SRussell King 
51120b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
512631ba906SMaxime Chevallier 	{ MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_MASK },
51362d01535SMaxime Chevallier 	{ MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK },
51420b2af32SRussell King 	{ },
51520b2af32SRussell King };
51620b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
51720b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
51820b2af32SRussell King MODULE_LICENSE("GPL");
519