xref: /linux/drivers/net/phy/marvell10g.c (revision 78a24df370072ea3b7c0a466efd776fc8f87c73a)
120b2af32SRussell King /*
220b2af32SRussell King  * Marvell 10G 88x3310 PHY driver
320b2af32SRussell King  *
420b2af32SRussell King  * Based upon the ID registers, this PHY appears to be a mixture of IPs
520b2af32SRussell King  * from two different companies.
620b2af32SRussell King  *
720b2af32SRussell King  * There appears to be several different data paths through the PHY which
820b2af32SRussell King  * are automatically managed by the PHY.  The following has been determined
905ca1b32SRussell King  * via observation and experimentation for a setup using single-lane Serdes:
1020b2af32SRussell King  *
1120b2af32SRussell King  *       SGMII PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for <= 1G)
1220b2af32SRussell King  *  10GBASE-KR PHYXS -- BASE-T PCS -- 10G PMA -- AN -- Copper (for 10G)
1320b2af32SRussell King  *  10GBASE-KR PHYXS -- BASE-R PCS -- Fiber
1420b2af32SRussell King  *
1505ca1b32SRussell King  * With XAUI, observation shows:
1605ca1b32SRussell King  *
1705ca1b32SRussell King  *        XAUI PHYXS -- <appropriate PCS as above>
1805ca1b32SRussell King  *
1905ca1b32SRussell King  * and no switching of the host interface mode occurs.
2005ca1b32SRussell King  *
2120b2af32SRussell King  * If both the fiber and copper ports are connected, the first to gain
2220b2af32SRussell King  * link takes priority and the other port is completely locked out.
2320b2af32SRussell King  */
240d3ad854SRussell King #include <linux/ctype.h>
250d3ad854SRussell King #include <linux/hwmon.h>
26952b6b3bSAntoine Tenart #include <linux/marvell_phy.h>
270d3ad854SRussell King #include <linux/phy.h>
2820b2af32SRussell King 
2920b2af32SRussell King enum {
3020b2af32SRussell King 	MV_PCS_BASE_T		= 0x0000,
3120b2af32SRussell King 	MV_PCS_BASE_R		= 0x1000,
3220b2af32SRussell King 	MV_PCS_1000BASEX	= 0x2000,
3320b2af32SRussell King 
34ea4efe25SRussell King 	MV_PCS_PAIRSWAP		= 0x8182,
35ea4efe25SRussell King 	MV_PCS_PAIRSWAP_MASK	= 0x0003,
36ea4efe25SRussell King 	MV_PCS_PAIRSWAP_AB	= 0x0002,
37ea4efe25SRussell King 	MV_PCS_PAIRSWAP_NONE	= 0x0003,
38ea4efe25SRussell King 
3920b2af32SRussell King 	/* These registers appear at 0x800X and 0xa00X - the 0xa00X control
4020b2af32SRussell King 	 * registers appear to set themselves to the 0x800X when AN is
4120b2af32SRussell King 	 * restarted, but status registers appear readable from either.
4220b2af32SRussell King 	 */
4320b2af32SRussell King 	MV_AN_CTRL1000		= 0x8000, /* 1000base-T control register */
4420b2af32SRussell King 	MV_AN_STAT1000		= 0x8001, /* 1000base-T status register */
450d3ad854SRussell King 
460d3ad854SRussell King 	/* Vendor2 MMD registers */
470d3ad854SRussell King 	MV_V2_TEMP_CTRL		= 0xf08a,
480d3ad854SRussell King 	MV_V2_TEMP_CTRL_MASK	= 0xc000,
490d3ad854SRussell King 	MV_V2_TEMP_CTRL_SAMPLE	= 0x0000,
500d3ad854SRussell King 	MV_V2_TEMP_CTRL_DISABLE	= 0xc000,
510d3ad854SRussell King 	MV_V2_TEMP		= 0xf08c,
520d3ad854SRussell King 	MV_V2_TEMP_UNKNOWN	= 0x9600, /* unknown function */
530d3ad854SRussell King };
540d3ad854SRussell King 
550d3ad854SRussell King struct mv3310_priv {
560d3ad854SRussell King 	struct device *hwmon_dev;
570d3ad854SRussell King 	char *hwmon_name;
5820b2af32SRussell King };
5920b2af32SRussell King 
6020b2af32SRussell King static int mv3310_modify(struct phy_device *phydev, int devad, u16 reg,
6120b2af32SRussell King 			 u16 mask, u16 bits)
6220b2af32SRussell King {
6320b2af32SRussell King 	int old, val, ret;
6420b2af32SRussell King 
6520b2af32SRussell King 	old = phy_read_mmd(phydev, devad, reg);
6620b2af32SRussell King 	if (old < 0)
6720b2af32SRussell King 		return old;
6820b2af32SRussell King 
6920b2af32SRussell King 	val = (old & ~mask) | (bits & mask);
7020b2af32SRussell King 	if (val == old)
7120b2af32SRussell King 		return 0;
7220b2af32SRussell King 
7320b2af32SRussell King 	ret = phy_write_mmd(phydev, devad, reg, val);
7420b2af32SRussell King 
7520b2af32SRussell King 	return ret < 0 ? ret : 1;
7620b2af32SRussell King }
7720b2af32SRussell King 
780d3ad854SRussell King #ifdef CONFIG_HWMON
790d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data,
800d3ad854SRussell King 				       enum hwmon_sensor_types type,
810d3ad854SRussell King 				       u32 attr, int channel)
820d3ad854SRussell King {
830d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
840d3ad854SRussell King 		return 0444;
850d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input)
860d3ad854SRussell King 		return 0444;
870d3ad854SRussell King 	return 0;
880d3ad854SRussell King }
890d3ad854SRussell King 
900d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
910d3ad854SRussell King 			     u32 attr, int channel, long *value)
920d3ad854SRussell King {
930d3ad854SRussell King 	struct phy_device *phydev = dev_get_drvdata(dev);
940d3ad854SRussell King 	int temp;
950d3ad854SRussell King 
960d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
970d3ad854SRussell King 		*value = MSEC_PER_SEC;
980d3ad854SRussell King 		return 0;
990d3ad854SRussell King 	}
1000d3ad854SRussell King 
1010d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input) {
1020d3ad854SRussell King 		temp = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP);
1030d3ad854SRussell King 		if (temp < 0)
1040d3ad854SRussell King 			return temp;
1050d3ad854SRussell King 
1060d3ad854SRussell King 		*value = ((temp & 0xff) - 75) * 1000;
1070d3ad854SRussell King 
1080d3ad854SRussell King 		return 0;
1090d3ad854SRussell King 	}
1100d3ad854SRussell King 
1110d3ad854SRussell King 	return -EOPNOTSUPP;
1120d3ad854SRussell King }
1130d3ad854SRussell King 
1140d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = {
1150d3ad854SRussell King 	.is_visible = mv3310_hwmon_is_visible,
1160d3ad854SRussell King 	.read = mv3310_hwmon_read,
1170d3ad854SRussell King };
1180d3ad854SRussell King 
1190d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = {
1200d3ad854SRussell King 	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
1210d3ad854SRussell King 	0,
1220d3ad854SRussell King };
1230d3ad854SRussell King 
1240d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = {
1250d3ad854SRussell King 	.type = hwmon_chip,
1260d3ad854SRussell King 	.config = mv3310_hwmon_chip_config,
1270d3ad854SRussell King };
1280d3ad854SRussell King 
1290d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = {
1300d3ad854SRussell King 	HWMON_T_INPUT,
1310d3ad854SRussell King 	0,
1320d3ad854SRussell King };
1330d3ad854SRussell King 
1340d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = {
1350d3ad854SRussell King 	.type = hwmon_temp,
1360d3ad854SRussell King 	.config = mv3310_hwmon_temp_config,
1370d3ad854SRussell King };
1380d3ad854SRussell King 
1390d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = {
1400d3ad854SRussell King 	&mv3310_hwmon_chip,
1410d3ad854SRussell King 	&mv3310_hwmon_temp,
1420d3ad854SRussell King 	NULL,
1430d3ad854SRussell King };
1440d3ad854SRussell King 
1450d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = {
1460d3ad854SRussell King 	.ops = &mv3310_hwmon_ops,
1470d3ad854SRussell King 	.info = mv3310_hwmon_info,
1480d3ad854SRussell King };
1490d3ad854SRussell King 
1500d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
1510d3ad854SRussell King {
1520d3ad854SRussell King 	u16 val;
1530d3ad854SRussell King 	int ret;
1540d3ad854SRussell King 
1550d3ad854SRussell King 	ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP,
1560d3ad854SRussell King 			    MV_V2_TEMP_UNKNOWN);
1570d3ad854SRussell King 	if (ret < 0)
1580d3ad854SRussell King 		return ret;
1590d3ad854SRussell King 
1600d3ad854SRussell King 	val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE;
1610d3ad854SRussell King 	ret = mv3310_modify(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL,
1620d3ad854SRussell King 			    MV_V2_TEMP_CTRL_MASK, val);
1630d3ad854SRussell King 
1640d3ad854SRussell King 	return ret < 0 ? ret : 0;
1650d3ad854SRussell King }
1660d3ad854SRussell King 
1670d3ad854SRussell King static void mv3310_hwmon_disable(void *data)
1680d3ad854SRussell King {
1690d3ad854SRussell King 	struct phy_device *phydev = data;
1700d3ad854SRussell King 
1710d3ad854SRussell King 	mv3310_hwmon_config(phydev, false);
1720d3ad854SRussell King }
1730d3ad854SRussell King 
1740d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
1750d3ad854SRussell King {
1760d3ad854SRussell King 	struct device *dev = &phydev->mdio.dev;
1770d3ad854SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1780d3ad854SRussell King 	int i, j, ret;
1790d3ad854SRussell King 
1800d3ad854SRussell King 	priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
1810d3ad854SRussell King 	if (!priv->hwmon_name)
1820d3ad854SRussell King 		return -ENODEV;
1830d3ad854SRussell King 
1840d3ad854SRussell King 	for (i = j = 0; priv->hwmon_name[i]; i++) {
1850d3ad854SRussell King 		if (isalnum(priv->hwmon_name[i])) {
1860d3ad854SRussell King 			if (i != j)
1870d3ad854SRussell King 				priv->hwmon_name[j] = priv->hwmon_name[i];
1880d3ad854SRussell King 			j++;
1890d3ad854SRussell King 		}
1900d3ad854SRussell King 	}
1910d3ad854SRussell King 	priv->hwmon_name[j] = '\0';
1920d3ad854SRussell King 
1930d3ad854SRussell King 	ret = mv3310_hwmon_config(phydev, true);
1940d3ad854SRussell King 	if (ret)
1950d3ad854SRussell King 		return ret;
1960d3ad854SRussell King 
1970d3ad854SRussell King 	ret = devm_add_action_or_reset(dev, mv3310_hwmon_disable, phydev);
1980d3ad854SRussell King 	if (ret)
1990d3ad854SRussell King 		return ret;
2000d3ad854SRussell King 
2010d3ad854SRussell King 	priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
2020d3ad854SRussell King 				priv->hwmon_name, phydev,
2030d3ad854SRussell King 				&mv3310_hwmon_chip_info, NULL);
2040d3ad854SRussell King 
2050d3ad854SRussell King 	return PTR_ERR_OR_ZERO(priv->hwmon_dev);
2060d3ad854SRussell King }
2070d3ad854SRussell King #else
2080d3ad854SRussell King static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
2090d3ad854SRussell King {
2100d3ad854SRussell King 	return 0;
2110d3ad854SRussell King }
2120d3ad854SRussell King 
2130d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
2140d3ad854SRussell King {
2150d3ad854SRussell King 	return 0;
2160d3ad854SRussell King }
2170d3ad854SRussell King #endif
2180d3ad854SRussell King 
21920b2af32SRussell King static int mv3310_probe(struct phy_device *phydev)
22020b2af32SRussell King {
2210d3ad854SRussell King 	struct mv3310_priv *priv;
22220b2af32SRussell King 	u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
2230d3ad854SRussell King 	int ret;
22420b2af32SRussell King 
22520b2af32SRussell King 	if (!phydev->is_c45 ||
22620b2af32SRussell King 	    (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
22720b2af32SRussell King 		return -ENODEV;
22820b2af32SRussell 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 {
2440d3ad854SRussell King 	return 0;
2450d3ad854SRussell King }
2460d3ad854SRussell King 
2470d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev)
2480d3ad854SRussell King {
2490d3ad854SRussell King 	return mv3310_hwmon_config(phydev, true);
2500d3ad854SRussell King }
2510d3ad854SRussell King 
25220b2af32SRussell King static int mv3310_config_init(struct phy_device *phydev)
25320b2af32SRussell King {
25420b2af32SRussell King 	__ETHTOOL_DECLARE_LINK_MODE_MASK(supported) = { 0, };
25520b2af32SRussell King 	int val;
25620b2af32SRussell King 
25720b2af32SRussell King 	/* Check that the PHY interface type is compatible */
25820b2af32SRussell King 	if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
25920b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_XAUI &&
26020b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
26120b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_10GKR)
26220b2af32SRussell King 		return -ENODEV;
26320b2af32SRussell King 
26420b2af32SRussell King 	__set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
26520b2af32SRussell King 	__set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported);
26620b2af32SRussell King 
26720b2af32SRussell King 	if (phydev->c45_ids.devices_in_package & MDIO_DEVS_AN) {
26820b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
26920b2af32SRussell King 		if (val < 0)
27020b2af32SRussell King 			return val;
27120b2af32SRussell King 
27220b2af32SRussell King 		if (val & MDIO_AN_STAT1_ABLE)
27320b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported);
27420b2af32SRussell King 	}
27520b2af32SRussell King 
27620b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_STAT2);
27720b2af32SRussell King 	if (val < 0)
27820b2af32SRussell King 		return val;
27920b2af32SRussell King 
28020b2af32SRussell King 	/* Ethtool does not support the WAN mode bits */
28120b2af32SRussell King 	if (val & (MDIO_PMA_STAT2_10GBSR | MDIO_PMA_STAT2_10GBLR |
28220b2af32SRussell King 		   MDIO_PMA_STAT2_10GBER | MDIO_PMA_STAT2_10GBLX4 |
28320b2af32SRussell King 		   MDIO_PMA_STAT2_10GBSW | MDIO_PMA_STAT2_10GBLW |
28420b2af32SRussell King 		   MDIO_PMA_STAT2_10GBEW))
28520b2af32SRussell King 		__set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
28620b2af32SRussell King 	if (val & MDIO_PMA_STAT2_10GBSR)
28720b2af32SRussell King 		__set_bit(ETHTOOL_LINK_MODE_10000baseSR_Full_BIT, supported);
28820b2af32SRussell King 	if (val & MDIO_PMA_STAT2_10GBLR)
28920b2af32SRussell King 		__set_bit(ETHTOOL_LINK_MODE_10000baseLR_Full_BIT, supported);
29020b2af32SRussell King 	if (val & MDIO_PMA_STAT2_10GBER)
29120b2af32SRussell King 		__set_bit(ETHTOOL_LINK_MODE_10000baseER_Full_BIT, supported);
29220b2af32SRussell King 
29320b2af32SRussell King 	if (val & MDIO_PMA_STAT2_EXTABLE) {
29420b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_EXTABLE);
29520b2af32SRussell King 		if (val < 0)
29620b2af32SRussell King 			return val;
29720b2af32SRussell King 
29820b2af32SRussell King 		if (val & (MDIO_PMA_EXTABLE_10GBT | MDIO_PMA_EXTABLE_1000BT |
29920b2af32SRussell King 			   MDIO_PMA_EXTABLE_100BTX | MDIO_PMA_EXTABLE_10BT))
30020b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_TP_BIT, supported);
30120b2af32SRussell King 		if (val & MDIO_PMA_EXTABLE_10GBLRM)
30220b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, supported);
30320b2af32SRussell King 		if (val & (MDIO_PMA_EXTABLE_10GBKX4 | MDIO_PMA_EXTABLE_10GBKR |
30420b2af32SRussell King 			   MDIO_PMA_EXTABLE_1000BKX))
30520b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, supported);
30620b2af32SRussell King 		if (val & MDIO_PMA_EXTABLE_10GBLRM)
30720b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_10000baseLRM_Full_BIT,
30820b2af32SRussell King 				  supported);
30920b2af32SRussell King 		if (val & MDIO_PMA_EXTABLE_10GBT)
31020b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
31120b2af32SRussell King 				  supported);
31220b2af32SRussell King 		if (val & MDIO_PMA_EXTABLE_10GBKX4)
31320b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
31420b2af32SRussell King 				  supported);
31520b2af32SRussell King 		if (val & MDIO_PMA_EXTABLE_10GBKR)
31620b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
31720b2af32SRussell King 				  supported);
31820b2af32SRussell King 		if (val & MDIO_PMA_EXTABLE_1000BT)
31920b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
32020b2af32SRussell King 				  supported);
32120b2af32SRussell King 		if (val & MDIO_PMA_EXTABLE_1000BKX)
32220b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
32320b2af32SRussell King 				  supported);
3246798d03cSRussell King 		if (val & MDIO_PMA_EXTABLE_100BTX) {
32520b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
32620b2af32SRussell King 				  supported);
3276798d03cSRussell King 			__set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
3286798d03cSRussell King 				  supported);
3296798d03cSRussell King 		}
3306798d03cSRussell King 		if (val & MDIO_PMA_EXTABLE_10BT) {
33120b2af32SRussell King 			__set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
33220b2af32SRussell King 				  supported);
3336798d03cSRussell King 			__set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
3346798d03cSRussell King 				  supported);
3356798d03cSRussell King 		}
33620b2af32SRussell King 	}
33720b2af32SRussell King 
3383c1bcc86SAndrew Lunn 	linkmode_copy(phydev->supported, supported);
3393c1bcc86SAndrew Lunn 	linkmode_and(phydev->advertising, phydev->advertising,
3403c1bcc86SAndrew Lunn 		     phydev->supported);
34120b2af32SRussell King 
34220b2af32SRussell King 	return 0;
34320b2af32SRussell King }
34420b2af32SRussell King 
34520b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev)
34620b2af32SRussell King {
34720b2af32SRussell King 	bool changed = false;
3483c1bcc86SAndrew Lunn 	u16 reg;
34920b2af32SRussell King 	int ret;
35020b2af32SRussell King 
351ea4efe25SRussell King 	/* We don't support manual MDI control */
352ea4efe25SRussell King 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
353ea4efe25SRussell King 
35420b2af32SRussell King 	if (phydev->autoneg == AUTONEG_DISABLE) {
35520b2af32SRussell King 		ret = genphy_c45_pma_setup_forced(phydev);
35620b2af32SRussell King 		if (ret < 0)
35720b2af32SRussell King 			return ret;
35820b2af32SRussell King 
35920b2af32SRussell King 		return genphy_c45_an_disable_aneg(phydev);
36020b2af32SRussell King 	}
36120b2af32SRussell King 
3623c1bcc86SAndrew Lunn 	linkmode_and(phydev->advertising, phydev->advertising,
3633c1bcc86SAndrew Lunn 		     phydev->supported);
36420b2af32SRussell King 
36520b2af32SRussell King 	ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE,
36620b2af32SRussell King 			    ADVERTISE_ALL | ADVERTISE_100BASE4 |
36720b2af32SRussell King 			    ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
3683c1bcc86SAndrew Lunn 			    linkmode_adv_to_mii_adv_t(phydev->advertising));
36920b2af32SRussell King 	if (ret < 0)
37020b2af32SRussell King 		return ret;
37120b2af32SRussell King 	if (ret > 0)
37220b2af32SRussell King 		changed = true;
37320b2af32SRussell King 
3743c1bcc86SAndrew Lunn 	reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
37520b2af32SRussell King 	ret = mv3310_modify(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
3763c1bcc86SAndrew Lunn 			    ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg);
37720b2af32SRussell King 	if (ret < 0)
37820b2af32SRussell King 		return ret;
37920b2af32SRussell King 	if (ret > 0)
38020b2af32SRussell King 		changed = true;
38120b2af32SRussell King 
38220b2af32SRussell King 	/* 10G control register */
3833c1bcc86SAndrew Lunn 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
3843c1bcc86SAndrew Lunn 			      phydev->advertising))
3853c1bcc86SAndrew Lunn 		reg = MDIO_AN_10GBT_CTRL_ADV10G;
3863c1bcc86SAndrew Lunn 	else
3873c1bcc86SAndrew Lunn 		reg = 0;
3883c1bcc86SAndrew Lunn 
38920b2af32SRussell King 	ret = mv3310_modify(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
3903c1bcc86SAndrew Lunn 			    MDIO_AN_10GBT_CTRL_ADV10G, reg);
39120b2af32SRussell King 	if (ret < 0)
39220b2af32SRussell King 		return ret;
39320b2af32SRussell King 	if (ret > 0)
39420b2af32SRussell King 		changed = true;
39520b2af32SRussell King 
39620b2af32SRussell King 	if (changed)
39720b2af32SRussell King 		ret = genphy_c45_restart_aneg(phydev);
39820b2af32SRussell King 
39920b2af32SRussell King 	return ret;
40020b2af32SRussell King }
40120b2af32SRussell King 
40220b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev)
40320b2af32SRussell King {
40420b2af32SRussell King 	int val;
40520b2af32SRussell King 
40620b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
40720b2af32SRussell King 	if (val < 0)
40820b2af32SRussell King 		return val;
40920b2af32SRussell King 
41020b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
41120b2af32SRussell King 		return 1;
41220b2af32SRussell King 
41320b2af32SRussell King 	return genphy_c45_aneg_done(phydev);
41420b2af32SRussell King }
41520b2af32SRussell King 
41636c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev)
41736c4449aSRussell King {
41836c4449aSRussell King 	if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
41936c4449aSRussell King 	     phydev->interface == PHY_INTERFACE_MODE_10GKR) && phydev->link) {
42036c4449aSRussell King 		/* The PHY automatically switches its serdes interface (and
42136c4449aSRussell King 		 * active PHYXS instance) between Cisco SGMII and 10GBase-KR
42236c4449aSRussell King 		 * modes according to the speed.  Florian suggests setting
42336c4449aSRussell King 		 * phydev->interface to communicate this to the MAC. Only do
42436c4449aSRussell King 		 * this if we are already in either SGMII or 10GBase-KR mode.
42536c4449aSRussell King 		 */
42636c4449aSRussell King 		if (phydev->speed == SPEED_10000)
42736c4449aSRussell King 			phydev->interface = PHY_INTERFACE_MODE_10GKR;
42836c4449aSRussell King 		else if (phydev->speed >= SPEED_10 &&
42936c4449aSRussell King 			 phydev->speed < SPEED_10000)
43036c4449aSRussell King 			phydev->interface = PHY_INTERFACE_MODE_SGMII;
43136c4449aSRussell King 	}
43236c4449aSRussell King }
43336c4449aSRussell King 
43420b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
43520b2af32SRussell King static int mv3310_read_10gbr_status(struct phy_device *phydev)
43620b2af32SRussell King {
43720b2af32SRussell King 	phydev->link = 1;
43820b2af32SRussell King 	phydev->speed = SPEED_10000;
43920b2af32SRussell King 	phydev->duplex = DUPLEX_FULL;
44020b2af32SRussell King 
44136c4449aSRussell King 	mv3310_update_interface(phydev);
44220b2af32SRussell King 
44320b2af32SRussell King 	return 0;
44420b2af32SRussell King }
44520b2af32SRussell King 
44620b2af32SRussell King static int mv3310_read_status(struct phy_device *phydev)
44720b2af32SRussell King {
44820b2af32SRussell King 	u32 mmd_mask = phydev->c45_ids.devices_in_package;
44920b2af32SRussell King 	int val;
45020b2af32SRussell King 
45120b2af32SRussell King 	/* The vendor devads do not report link status.  Avoid the PHYXS
45220b2af32SRussell King 	 * instance as there are three, and its status depends on the MAC
45320b2af32SRussell King 	 * being appropriately configured for the negotiated speed.
45420b2af32SRussell King 	 */
45520b2af32SRussell King 	mmd_mask &= ~(BIT(MDIO_MMD_VEND1) | BIT(MDIO_MMD_VEND2) |
45620b2af32SRussell King 		      BIT(MDIO_MMD_PHYXS));
45720b2af32SRussell King 
45820b2af32SRussell King 	phydev->speed = SPEED_UNKNOWN;
45920b2af32SRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
460c0ec3c27SAndrew Lunn 	linkmode_zero(phydev->lp_advertising);
46120b2af32SRussell King 	phydev->link = 0;
46220b2af32SRussell King 	phydev->pause = 0;
46320b2af32SRussell King 	phydev->asym_pause = 0;
464ea4efe25SRussell King 	phydev->mdix = 0;
46520b2af32SRussell King 
46620b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
46720b2af32SRussell King 	if (val < 0)
46820b2af32SRussell King 		return val;
46920b2af32SRussell King 
47020b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
47120b2af32SRussell King 		return mv3310_read_10gbr_status(phydev);
47220b2af32SRussell King 
47320b2af32SRussell King 	val = genphy_c45_read_link(phydev, mmd_mask);
47420b2af32SRussell King 	if (val < 0)
47520b2af32SRussell King 		return val;
47620b2af32SRussell King 
47720b2af32SRussell King 	phydev->link = val > 0 ? 1 : 0;
47820b2af32SRussell King 
47920b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
48020b2af32SRussell King 	if (val < 0)
48120b2af32SRussell King 		return val;
48220b2af32SRussell King 
48320b2af32SRussell King 	if (val & MDIO_AN_STAT1_COMPLETE) {
48420b2af32SRussell King 		val = genphy_c45_read_lpa(phydev);
48520b2af32SRussell King 		if (val < 0)
48620b2af32SRussell King 			return val;
48720b2af32SRussell King 
488cc1122b0SColin Ian King 		/* Read the link partner's 1G advertisement */
48920b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
49020b2af32SRussell King 		if (val < 0)
49120b2af32SRussell King 			return val;
49220b2af32SRussell King 
493*78a24df3SAndrew Lunn 		mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
49420b2af32SRussell King 
4956798d03cSRussell King 		if (phydev->autoneg == AUTONEG_ENABLE)
4966798d03cSRussell King 			phy_resolve_aneg_linkmode(phydev);
49720b2af32SRussell King 	}
49820b2af32SRussell King 
49920b2af32SRussell King 	if (phydev->autoneg != AUTONEG_ENABLE) {
50020b2af32SRussell King 		val = genphy_c45_read_pma(phydev);
50120b2af32SRussell King 		if (val < 0)
50220b2af32SRussell King 			return val;
50320b2af32SRussell King 	}
50420b2af32SRussell King 
505ea4efe25SRussell King 	if (phydev->speed == SPEED_10000) {
506ea4efe25SRussell King 		val = genphy_c45_read_mdix(phydev);
507ea4efe25SRussell King 		if (val < 0)
508ea4efe25SRussell King 			return val;
509ea4efe25SRussell King 	} else {
510ea4efe25SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_PAIRSWAP);
511ea4efe25SRussell King 		if (val < 0)
512ea4efe25SRussell King 			return val;
513ea4efe25SRussell King 
514ea4efe25SRussell King 		switch (val & MV_PCS_PAIRSWAP_MASK) {
515ea4efe25SRussell King 		case MV_PCS_PAIRSWAP_AB:
516ea4efe25SRussell King 			phydev->mdix = ETH_TP_MDI_X;
517ea4efe25SRussell King 			break;
518ea4efe25SRussell King 		case MV_PCS_PAIRSWAP_NONE:
519ea4efe25SRussell King 			phydev->mdix = ETH_TP_MDI;
520ea4efe25SRussell King 			break;
521ea4efe25SRussell King 		default:
522ea4efe25SRussell King 			phydev->mdix = ETH_TP_MDI_INVALID;
523ea4efe25SRussell King 			break;
524ea4efe25SRussell King 		}
525ea4efe25SRussell King 	}
526ea4efe25SRussell King 
52736c4449aSRussell King 	mv3310_update_interface(phydev);
52820b2af32SRussell King 
52920b2af32SRussell King 	return 0;
53020b2af32SRussell King }
53120b2af32SRussell King 
53220b2af32SRussell King static struct phy_driver mv3310_drivers[] = {
53320b2af32SRussell King 	{
53420b2af32SRussell King 		.phy_id		= 0x002b09aa,
535952b6b3bSAntoine Tenart 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
53620b2af32SRussell King 		.name		= "mv88x3310",
537719655a1SAndrew Lunn 		.features	= PHY_10GBIT_FEATURES,
53856847704SFlorian Fainelli 		.soft_reset	= gen10g_no_soft_reset,
53920b2af32SRussell King 		.config_init	= mv3310_config_init,
5400d3ad854SRussell King 		.probe		= mv3310_probe,
5410d3ad854SRussell King 		.suspend	= mv3310_suspend,
5420d3ad854SRussell King 		.resume		= mv3310_resume,
54320b2af32SRussell King 		.config_aneg	= mv3310_config_aneg,
54420b2af32SRussell King 		.aneg_done	= mv3310_aneg_done,
54520b2af32SRussell King 		.read_status	= mv3310_read_status,
54620b2af32SRussell King 	},
54720b2af32SRussell King };
54820b2af32SRussell King 
54920b2af32SRussell King module_phy_driver(mv3310_drivers);
55020b2af32SRussell King 
55120b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
552952b6b3bSAntoine Tenart 	{ 0x002b09aa, MARVELL_PHY_ID_MASK },
55320b2af32SRussell King 	{ },
55420b2af32SRussell King };
55520b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
55620b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
55720b2af32SRussell King MODULE_LICENSE("GPL");
558