xref: /linux/drivers/net/phy/marvell10g.c (revision c84786fa8f912e1450727f49375b3f0fe2b13afc)
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>
2936023da1SRussell King #include <linux/sfp.h>
3020b2af32SRussell King 
31c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_MASK	0xfffffffe
32c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_REV	(MARVELL_PHY_ID_88X3310 | 0xa)
33c47455f9SMaxime Chevallier 
3420b2af32SRussell King enum {
353d3ced2eSRussell King 	MV_PMA_BOOT		= 0xc050,
363d3ced2eSRussell King 	MV_PMA_BOOT_FATAL	= BIT(0),
373d3ced2eSRussell King 
3820b2af32SRussell King 	MV_PCS_BASE_T		= 0x0000,
3920b2af32SRussell King 	MV_PCS_BASE_R		= 0x1000,
4020b2af32SRussell King 	MV_PCS_1000BASEX	= 0x2000,
4120b2af32SRussell King 
42*c84786faSRussell King 	MV_PCS_CSSR1		= 0x8008,
43*c84786faSRussell King 	MV_PCS_CSSR1_SPD1_MASK	= 0xc000,
44*c84786faSRussell King 	MV_PCS_CSSR1_SPD1_SPD2	= 0xc000,
45*c84786faSRussell King 	MV_PCS_CSSR1_SPD1_1000	= 0x8000,
46*c84786faSRussell King 	MV_PCS_CSSR1_SPD1_100	= 0x4000,
47*c84786faSRussell King 	MV_PCS_CSSR1_SPD1_10	= 0x0000,
48*c84786faSRussell King 	MV_PCS_CSSR1_DUPLEX_FULL= BIT(13),
49*c84786faSRussell King 	MV_PCS_CSSR1_RESOLVED	= BIT(11),
50*c84786faSRussell King 	MV_PCS_CSSR1_MDIX	= BIT(6),
51*c84786faSRussell King 	MV_PCS_CSSR1_SPD2_MASK	= 0x000c,
52*c84786faSRussell King 	MV_PCS_CSSR1_SPD2_5000	= 0x0008,
53*c84786faSRussell King 	MV_PCS_CSSR1_SPD2_2500	= 0x0004,
54*c84786faSRussell King 	MV_PCS_CSSR1_SPD2_10000	= 0x0000,
55ea4efe25SRussell King 
5620b2af32SRussell King 	/* These registers appear at 0x800X and 0xa00X - the 0xa00X control
5720b2af32SRussell King 	 * registers appear to set themselves to the 0x800X when AN is
5820b2af32SRussell King 	 * restarted, but status registers appear readable from either.
5920b2af32SRussell King 	 */
6020b2af32SRussell King 	MV_AN_CTRL1000		= 0x8000, /* 1000base-T control register */
6120b2af32SRussell King 	MV_AN_STAT1000		= 0x8001, /* 1000base-T status register */
620d3ad854SRussell King 
630d3ad854SRussell King 	/* Vendor2 MMD registers */
64af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL		= 0xf001,
65af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL_PWRDOWN = 0x0800,
660d3ad854SRussell King 	MV_V2_TEMP_CTRL		= 0xf08a,
670d3ad854SRussell King 	MV_V2_TEMP_CTRL_MASK	= 0xc000,
680d3ad854SRussell King 	MV_V2_TEMP_CTRL_SAMPLE	= 0x0000,
690d3ad854SRussell King 	MV_V2_TEMP_CTRL_DISABLE	= 0xc000,
700d3ad854SRussell King 	MV_V2_TEMP		= 0xf08c,
710d3ad854SRussell King 	MV_V2_TEMP_UNKNOWN	= 0x9600, /* unknown function */
720d3ad854SRussell King };
730d3ad854SRussell King 
740d3ad854SRussell King struct mv3310_priv {
750d3ad854SRussell King 	struct device *hwmon_dev;
760d3ad854SRussell King 	char *hwmon_name;
7720b2af32SRussell King };
7820b2af32SRussell King 
790d3ad854SRussell King #ifdef CONFIG_HWMON
800d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data,
810d3ad854SRussell King 				       enum hwmon_sensor_types type,
820d3ad854SRussell King 				       u32 attr, int channel)
830d3ad854SRussell King {
840d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
850d3ad854SRussell King 		return 0444;
860d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input)
870d3ad854SRussell King 		return 0444;
880d3ad854SRussell King 	return 0;
890d3ad854SRussell King }
900d3ad854SRussell King 
910d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
920d3ad854SRussell King 			     u32 attr, int channel, long *value)
930d3ad854SRussell King {
940d3ad854SRussell King 	struct phy_device *phydev = dev_get_drvdata(dev);
950d3ad854SRussell King 	int temp;
960d3ad854SRussell King 
970d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
980d3ad854SRussell King 		*value = MSEC_PER_SEC;
990d3ad854SRussell King 		return 0;
1000d3ad854SRussell King 	}
1010d3ad854SRussell King 
1020d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input) {
1030d3ad854SRussell King 		temp = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP);
1040d3ad854SRussell King 		if (temp < 0)
1050d3ad854SRussell King 			return temp;
1060d3ad854SRussell King 
1070d3ad854SRussell King 		*value = ((temp & 0xff) - 75) * 1000;
1080d3ad854SRussell King 
1090d3ad854SRussell King 		return 0;
1100d3ad854SRussell King 	}
1110d3ad854SRussell King 
1120d3ad854SRussell King 	return -EOPNOTSUPP;
1130d3ad854SRussell King }
1140d3ad854SRussell King 
1150d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = {
1160d3ad854SRussell King 	.is_visible = mv3310_hwmon_is_visible,
1170d3ad854SRussell King 	.read = mv3310_hwmon_read,
1180d3ad854SRussell King };
1190d3ad854SRussell King 
1200d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = {
1210d3ad854SRussell King 	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
1220d3ad854SRussell King 	0,
1230d3ad854SRussell King };
1240d3ad854SRussell King 
1250d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = {
1260d3ad854SRussell King 	.type = hwmon_chip,
1270d3ad854SRussell King 	.config = mv3310_hwmon_chip_config,
1280d3ad854SRussell King };
1290d3ad854SRussell King 
1300d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = {
1310d3ad854SRussell King 	HWMON_T_INPUT,
1320d3ad854SRussell King 	0,
1330d3ad854SRussell King };
1340d3ad854SRussell King 
1350d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = {
1360d3ad854SRussell King 	.type = hwmon_temp,
1370d3ad854SRussell King 	.config = mv3310_hwmon_temp_config,
1380d3ad854SRussell King };
1390d3ad854SRussell King 
1400d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = {
1410d3ad854SRussell King 	&mv3310_hwmon_chip,
1420d3ad854SRussell King 	&mv3310_hwmon_temp,
1430d3ad854SRussell King 	NULL,
1440d3ad854SRussell King };
1450d3ad854SRussell King 
1460d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = {
1470d3ad854SRussell King 	.ops = &mv3310_hwmon_ops,
1480d3ad854SRussell King 	.info = mv3310_hwmon_info,
1490d3ad854SRussell King };
1500d3ad854SRussell King 
1510d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
1520d3ad854SRussell King {
1530d3ad854SRussell King 	u16 val;
1540d3ad854SRussell King 	int ret;
1550d3ad854SRussell King 
1560d3ad854SRussell King 	ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP,
1570d3ad854SRussell King 			    MV_V2_TEMP_UNKNOWN);
1580d3ad854SRussell King 	if (ret < 0)
1590d3ad854SRussell King 		return ret;
1600d3ad854SRussell King 
1610d3ad854SRussell King 	val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE;
1620d3ad854SRussell King 
163b06d8e5aSHeiner Kallweit 	return phy_modify_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL,
164b06d8e5aSHeiner Kallweit 			      MV_V2_TEMP_CTRL_MASK, val);
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 
21936023da1SRussell King static int mv3310_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
22036023da1SRussell King {
22136023da1SRussell King 	struct phy_device *phydev = upstream;
22236023da1SRussell King 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
22336023da1SRussell King 	phy_interface_t iface;
22436023da1SRussell King 
22536023da1SRussell King 	sfp_parse_support(phydev->sfp_bus, id, support);
226a4516c70SRussell King 	iface = sfp_select_interface(phydev->sfp_bus, support);
22736023da1SRussell King 
228e0f909bcSRussell King 	if (iface != PHY_INTERFACE_MODE_10GBASER) {
22936023da1SRussell King 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
23036023da1SRussell King 		return -EINVAL;
23136023da1SRussell King 	}
23236023da1SRussell King 	return 0;
23336023da1SRussell King }
23436023da1SRussell King 
23536023da1SRussell King static const struct sfp_upstream_ops mv3310_sfp_ops = {
23636023da1SRussell King 	.attach = phy_sfp_attach,
23736023da1SRussell King 	.detach = phy_sfp_detach,
23836023da1SRussell King 	.module_insert = mv3310_sfp_insert,
23936023da1SRussell King };
24036023da1SRussell King 
24120b2af32SRussell King static int mv3310_probe(struct phy_device *phydev)
24220b2af32SRussell King {
2430d3ad854SRussell King 	struct mv3310_priv *priv;
24420b2af32SRussell King 	u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
2450d3ad854SRussell King 	int ret;
24620b2af32SRussell King 
24720b2af32SRussell King 	if (!phydev->is_c45 ||
24820b2af32SRussell King 	    (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
24920b2af32SRussell King 		return -ENODEV;
25020b2af32SRussell King 
2513d3ced2eSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT);
2523d3ced2eSRussell King 	if (ret < 0)
2533d3ced2eSRussell King 		return ret;
2543d3ced2eSRussell King 
2553d3ced2eSRussell King 	if (ret & MV_PMA_BOOT_FATAL) {
2563d3ced2eSRussell King 		dev_warn(&phydev->mdio.dev,
2573d3ced2eSRussell King 			 "PHY failed to boot firmware, status=%04x\n", ret);
2583d3ced2eSRussell King 		return -ENODEV;
2593d3ced2eSRussell King 	}
2603d3ced2eSRussell King 
2610d3ad854SRussell King 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
2620d3ad854SRussell King 	if (!priv)
2630d3ad854SRussell King 		return -ENOMEM;
2640d3ad854SRussell King 
2650d3ad854SRussell King 	dev_set_drvdata(&phydev->mdio.dev, priv);
2660d3ad854SRussell King 
2670d3ad854SRussell King 	ret = mv3310_hwmon_probe(phydev);
2680d3ad854SRussell King 	if (ret)
2690d3ad854SRussell King 		return ret;
2700d3ad854SRussell King 
27136023da1SRussell King 	return phy_sfp_probe(phydev, &mv3310_sfp_ops);
27220b2af32SRussell King }
27320b2af32SRussell King 
2740d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev)
2750d3ad854SRussell King {
276af3e28cbSAntoine Tenart 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
277af3e28cbSAntoine Tenart 				MV_V2_PORT_CTRL_PWRDOWN);
2780d3ad854SRussell King }
2790d3ad854SRussell King 
2800d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev)
2810d3ad854SRussell King {
282af3e28cbSAntoine Tenart 	int ret;
283af3e28cbSAntoine Tenart 
284af3e28cbSAntoine Tenart 	ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
285af3e28cbSAntoine Tenart 				 MV_V2_PORT_CTRL_PWRDOWN);
286af3e28cbSAntoine Tenart 	if (ret)
287af3e28cbSAntoine Tenart 		return ret;
288af3e28cbSAntoine Tenart 
2890d3ad854SRussell King 	return mv3310_hwmon_config(phydev, true);
2900d3ad854SRussell King }
2910d3ad854SRussell King 
292c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010
293c47455f9SMaxime Chevallier  * don't set bit 14 in PMA Extended Abilities (1.11), although they do
294c47455f9SMaxime Chevallier  * support 2.5GBASET and 5GBASET. For these models, we can still read their
295c47455f9SMaxime Chevallier  * 2.5G/5G extended abilities register (1.21). We detect these models based on
296c47455f9SMaxime Chevallier  * the PMA device identifier, with a mask matching models known to have this
297c47455f9SMaxime Chevallier  * issue
298c47455f9SMaxime Chevallier  */
299c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev)
300c47455f9SMaxime Chevallier {
301c47455f9SMaxime Chevallier 	if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD))
302c47455f9SMaxime Chevallier 		return false;
303c47455f9SMaxime Chevallier 
304c47455f9SMaxime Chevallier 	/* Only some revisions of the 88X3310 family PMA seem to be impacted */
305c47455f9SMaxime Chevallier 	return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
306c47455f9SMaxime Chevallier 		MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
307c47455f9SMaxime Chevallier }
308c47455f9SMaxime Chevallier 
30920b2af32SRussell King static int mv3310_config_init(struct phy_device *phydev)
31020b2af32SRussell King {
31120b2af32SRussell King 	/* Check that the PHY interface type is compatible */
31220b2af32SRussell King 	if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
313e555e5b1SMaxime Chevallier 	    phydev->interface != PHY_INTERFACE_MODE_2500BASEX &&
31420b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_XAUI &&
31520b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
316e0f909bcSRussell King 	    phydev->interface != PHY_INTERFACE_MODE_10GBASER)
31720b2af32SRussell King 		return -ENODEV;
31820b2af32SRussell King 
31974145424SMaxime Chevallier 	return 0;
32074145424SMaxime Chevallier }
32174145424SMaxime Chevallier 
32274145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev)
32374145424SMaxime Chevallier {
32474145424SMaxime Chevallier 	int ret, val;
32574145424SMaxime Chevallier 
326ac3f5533SMaxime Chevallier 	ret = genphy_c45_pma_read_abilities(phydev);
327ac3f5533SMaxime Chevallier 	if (ret)
328ac3f5533SMaxime Chevallier 		return ret;
32920b2af32SRussell King 
330c47455f9SMaxime Chevallier 	if (mv3310_has_pma_ngbaset_quirk(phydev)) {
331c47455f9SMaxime Chevallier 		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
332c47455f9SMaxime Chevallier 				   MDIO_PMA_NG_EXTABLE);
333c47455f9SMaxime Chevallier 		if (val < 0)
334c47455f9SMaxime Chevallier 			return val;
335c47455f9SMaxime Chevallier 
336c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
337c47455f9SMaxime Chevallier 				 phydev->supported,
338c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_2_5GBT);
339c47455f9SMaxime Chevallier 
340c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
341c47455f9SMaxime Chevallier 				 phydev->supported,
342c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_5GBT);
343c47455f9SMaxime Chevallier 	}
344c47455f9SMaxime Chevallier 
34520b2af32SRussell King 	return 0;
34620b2af32SRussell King }
34720b2af32SRussell King 
34820b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev)
34920b2af32SRussell King {
35020b2af32SRussell King 	bool changed = false;
3513c1bcc86SAndrew Lunn 	u16 reg;
35220b2af32SRussell King 	int ret;
35320b2af32SRussell King 
354ea4efe25SRussell King 	/* We don't support manual MDI control */
355ea4efe25SRussell King 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
356ea4efe25SRussell King 
35730de65c3SHeiner Kallweit 	if (phydev->autoneg == AUTONEG_DISABLE)
35830de65c3SHeiner Kallweit 		return genphy_c45_pma_setup_forced(phydev);
35920b2af32SRussell King 
3603de97f3cSAndrew Lunn 	ret = genphy_c45_an_config_aneg(phydev);
36120b2af32SRussell King 	if (ret < 0)
36220b2af32SRussell King 		return ret;
36320b2af32SRussell King 	if (ret > 0)
36420b2af32SRussell King 		changed = true;
36520b2af32SRussell King 
3663de97f3cSAndrew Lunn 	/* Clause 45 has no standardized support for 1000BaseT, therefore
3673de97f3cSAndrew Lunn 	 * use vendor registers for this mode.
3683de97f3cSAndrew Lunn 	 */
3693c1bcc86SAndrew Lunn 	reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
370b06d8e5aSHeiner Kallweit 	ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
3713c1bcc86SAndrew Lunn 			     ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg);
37220b2af32SRussell King 	if (ret < 0)
37320b2af32SRussell King 		return ret;
37420b2af32SRussell King 	if (ret > 0)
37520b2af32SRussell King 		changed = true;
37620b2af32SRussell King 
3776b4cb6cbSHeiner Kallweit 	return genphy_c45_check_and_restart_aneg(phydev, changed);
37820b2af32SRussell King }
37920b2af32SRussell King 
38020b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev)
38120b2af32SRussell King {
38220b2af32SRussell King 	int val;
38320b2af32SRussell King 
38420b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
38520b2af32SRussell King 	if (val < 0)
38620b2af32SRussell King 		return val;
38720b2af32SRussell King 
38820b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
38920b2af32SRussell King 		return 1;
39020b2af32SRussell King 
39120b2af32SRussell King 	return genphy_c45_aneg_done(phydev);
39220b2af32SRussell King }
39320b2af32SRussell King 
39436c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev)
39536c4449aSRussell King {
39636c4449aSRussell King 	if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
397e555e5b1SMaxime Chevallier 	     phydev->interface == PHY_INTERFACE_MODE_2500BASEX ||
398e0f909bcSRussell King 	     phydev->interface == PHY_INTERFACE_MODE_10GBASER) &&
399e0f909bcSRussell King 	    phydev->link) {
40036c4449aSRussell King 		/* The PHY automatically switches its serdes interface (and
401e0f909bcSRussell King 		 * active PHYXS instance) between Cisco SGMII, 10GBase-R and
402e555e5b1SMaxime Chevallier 		 * 2500BaseX modes according to the speed.  Florian suggests
403e555e5b1SMaxime Chevallier 		 * setting phydev->interface to communicate this to the MAC.
404e555e5b1SMaxime Chevallier 		 * Only do this if we are already in one of the above modes.
40536c4449aSRussell King 		 */
406e555e5b1SMaxime Chevallier 		switch (phydev->speed) {
407e555e5b1SMaxime Chevallier 		case SPEED_10000:
408e0f909bcSRussell King 			phydev->interface = PHY_INTERFACE_MODE_10GBASER;
409e555e5b1SMaxime Chevallier 			break;
410e555e5b1SMaxime Chevallier 		case SPEED_2500:
411e555e5b1SMaxime Chevallier 			phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
412e555e5b1SMaxime Chevallier 			break;
413e555e5b1SMaxime Chevallier 		case SPEED_1000:
414e555e5b1SMaxime Chevallier 		case SPEED_100:
415e555e5b1SMaxime Chevallier 		case SPEED_10:
41636c4449aSRussell King 			phydev->interface = PHY_INTERFACE_MODE_SGMII;
417e555e5b1SMaxime Chevallier 			break;
418e555e5b1SMaxime Chevallier 		default:
419e555e5b1SMaxime Chevallier 			break;
420e555e5b1SMaxime Chevallier 		}
42136c4449aSRussell King 	}
42236c4449aSRussell King }
42336c4449aSRussell King 
42420b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
425*c84786faSRussell King static int mv3310_read_status_10gbaser(struct phy_device *phydev)
42620b2af32SRussell King {
42720b2af32SRussell King 	phydev->link = 1;
42820b2af32SRussell King 	phydev->speed = SPEED_10000;
42920b2af32SRussell King 	phydev->duplex = DUPLEX_FULL;
43020b2af32SRussell King 
43120b2af32SRussell King 	return 0;
43220b2af32SRussell King }
43320b2af32SRussell King 
434*c84786faSRussell King static int mv3310_read_status_copper(struct phy_device *phydev)
43520b2af32SRussell King {
436*c84786faSRussell King 	int cssr1, speed, val;
43720b2af32SRussell King 
438998a8a83SHeiner Kallweit 	val = genphy_c45_read_link(phydev);
43920b2af32SRussell King 	if (val < 0)
44020b2af32SRussell King 		return val;
44120b2af32SRussell King 
44220b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
44320b2af32SRussell King 	if (val < 0)
44420b2af32SRussell King 		return val;
44520b2af32SRussell King 
446*c84786faSRussell King 	cssr1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSSR1);
447*c84786faSRussell King 	if (cssr1 < 0)
448*c84786faSRussell King 		return val;
449*c84786faSRussell King 
450*c84786faSRussell King 	/* If the link settings are not resolved, mark the link down */
451*c84786faSRussell King 	if (!(cssr1 & MV_PCS_CSSR1_RESOLVED)) {
452*c84786faSRussell King 		phydev->link = 0;
453*c84786faSRussell King 		return 0;
454*c84786faSRussell King 	}
455*c84786faSRussell King 
456*c84786faSRussell King 	/* Read the copper link settings */
457*c84786faSRussell King 	speed = cssr1 & MV_PCS_CSSR1_SPD1_MASK;
458*c84786faSRussell King 	if (speed == MV_PCS_CSSR1_SPD1_SPD2)
459*c84786faSRussell King 		speed |= cssr1 & MV_PCS_CSSR1_SPD2_MASK;
460*c84786faSRussell King 
461*c84786faSRussell King 	switch (speed) {
462*c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_10000:
463*c84786faSRussell King 		phydev->speed = SPEED_10000;
464*c84786faSRussell King 		break;
465*c84786faSRussell King 
466*c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_5000:
467*c84786faSRussell King 		phydev->speed = SPEED_5000;
468*c84786faSRussell King 		break;
469*c84786faSRussell King 
470*c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_2500:
471*c84786faSRussell King 		phydev->speed = SPEED_2500;
472*c84786faSRussell King 		break;
473*c84786faSRussell King 
474*c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_1000:
475*c84786faSRussell King 		phydev->speed = SPEED_1000;
476*c84786faSRussell King 		break;
477*c84786faSRussell King 
478*c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_100:
479*c84786faSRussell King 		phydev->speed = SPEED_100;
480*c84786faSRussell King 		break;
481*c84786faSRussell King 
482*c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_10:
483*c84786faSRussell King 		phydev->speed = SPEED_10;
484*c84786faSRussell King 		break;
485*c84786faSRussell King 	}
486*c84786faSRussell King 
487*c84786faSRussell King 	phydev->duplex = cssr1 & MV_PCS_CSSR1_DUPLEX_FULL ?
488*c84786faSRussell King 			 DUPLEX_FULL : DUPLEX_HALF;
489*c84786faSRussell King 	phydev->mdix = cssr1 & MV_PCS_CSSR1_MDIX ?
490*c84786faSRussell King 		       ETH_TP_MDI_X : ETH_TP_MDI;
491*c84786faSRussell King 
49220b2af32SRussell King 	if (val & MDIO_AN_STAT1_COMPLETE) {
49320b2af32SRussell King 		val = genphy_c45_read_lpa(phydev);
49420b2af32SRussell King 		if (val < 0)
49520b2af32SRussell King 			return val;
49620b2af32SRussell King 
497cc1122b0SColin Ian King 		/* Read the link partner's 1G advertisement */
49820b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
49920b2af32SRussell King 		if (val < 0)
50020b2af32SRussell King 			return val;
50120b2af32SRussell King 
50278a24df3SAndrew Lunn 		mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
50320b2af32SRussell King 
504*c84786faSRussell King 		/* Update the pause status */
505*c84786faSRussell King 		phy_resolve_aneg_pause(phydev);
50620b2af32SRussell King 	}
50720b2af32SRussell King 
508*c84786faSRussell King 	return 0;
50920b2af32SRussell King }
51020b2af32SRussell King 
511*c84786faSRussell King static int mv3310_read_status(struct phy_device *phydev)
512*c84786faSRussell King {
513*c84786faSRussell King 	int err, val;
514ea4efe25SRussell King 
515*c84786faSRussell King 	phydev->speed = SPEED_UNKNOWN;
516*c84786faSRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
517*c84786faSRussell King 	linkmode_zero(phydev->lp_advertising);
518*c84786faSRussell King 	phydev->link = 0;
519*c84786faSRussell King 	phydev->pause = 0;
520*c84786faSRussell King 	phydev->asym_pause = 0;
521ea4efe25SRussell King 	phydev->mdix = ETH_TP_MDI_INVALID;
522ea4efe25SRussell King 
523*c84786faSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
524*c84786faSRussell King 	if (val < 0)
525*c84786faSRussell King 		return val;
526*c84786faSRussell King 
527*c84786faSRussell King 	if (val & MDIO_STAT1_LSTATUS)
528*c84786faSRussell King 		err = mv3310_read_status_10gbaser(phydev);
529*c84786faSRussell King 	else
530*c84786faSRussell King 		err = mv3310_read_status_copper(phydev);
531*c84786faSRussell King 	if (err < 0)
532*c84786faSRussell King 		return err;
533*c84786faSRussell King 
534*c84786faSRussell King 	if (phydev->link)
53536c4449aSRussell King 		mv3310_update_interface(phydev);
53620b2af32SRussell King 
53720b2af32SRussell King 	return 0;
53820b2af32SRussell King }
53920b2af32SRussell King 
54020b2af32SRussell King static struct phy_driver mv3310_drivers[] = {
54120b2af32SRussell King 	{
542631ba906SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88X3310,
543952b6b3bSAntoine Tenart 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
54420b2af32SRussell King 		.name		= "mv88x3310",
54574145424SMaxime Chevallier 		.get_features	= mv3310_get_features,
5467be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
54720b2af32SRussell King 		.config_init	= mv3310_config_init,
5480d3ad854SRussell King 		.probe		= mv3310_probe,
5490d3ad854SRussell King 		.suspend	= mv3310_suspend,
5500d3ad854SRussell King 		.resume		= mv3310_resume,
55120b2af32SRussell King 		.config_aneg	= mv3310_config_aneg,
55220b2af32SRussell King 		.aneg_done	= mv3310_aneg_done,
55320b2af32SRussell King 		.read_status	= mv3310_read_status,
55420b2af32SRussell King 	},
55562d01535SMaxime Chevallier 	{
55662d01535SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88E2110,
55762d01535SMaxime Chevallier 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
55862d01535SMaxime Chevallier 		.name		= "mv88x2110",
55962d01535SMaxime Chevallier 		.probe		= mv3310_probe,
560e02c4a9dSAntoine Tenart 		.suspend	= mv3310_suspend,
561e02c4a9dSAntoine Tenart 		.resume		= mv3310_resume,
5627be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
56362d01535SMaxime Chevallier 		.config_init	= mv3310_config_init,
56462d01535SMaxime Chevallier 		.config_aneg	= mv3310_config_aneg,
56562d01535SMaxime Chevallier 		.aneg_done	= mv3310_aneg_done,
56662d01535SMaxime Chevallier 		.read_status	= mv3310_read_status,
56762d01535SMaxime Chevallier 	},
56820b2af32SRussell King };
56920b2af32SRussell King 
57020b2af32SRussell King module_phy_driver(mv3310_drivers);
57120b2af32SRussell King 
57220b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
573631ba906SMaxime Chevallier 	{ MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_MASK },
57462d01535SMaxime Chevallier 	{ MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK },
57520b2af32SRussell King 	{ },
57620b2af32SRussell King };
57720b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
57820b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
57920b2af32SRussell King MODULE_LICENSE("GPL");
580