xref: /linux/drivers/net/phy/marvell10g.c (revision c9cc1c815d36f9d5723e369d662f238bc3b35d83)
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>
268d8963c3SRussell King #include <linux/delay.h>
270d3ad854SRussell King #include <linux/hwmon.h>
28952b6b3bSAntoine Tenart #include <linux/marvell_phy.h>
290d3ad854SRussell King #include <linux/phy.h>
3036023da1SRussell King #include <linux/sfp.h>
3120b2af32SRussell King 
32c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_MASK	0xfffffffe
33c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_REV	(MARVELL_PHY_ID_88X3310 | 0xa)
34c47455f9SMaxime Chevallier 
3520b2af32SRussell King enum {
363d3ced2eSRussell King 	MV_PMA_BOOT		= 0xc050,
373d3ced2eSRussell King 	MV_PMA_BOOT_FATAL	= BIT(0),
383d3ced2eSRussell King 
3920b2af32SRussell King 	MV_PCS_BASE_T		= 0x0000,
4020b2af32SRussell King 	MV_PCS_BASE_R		= 0x1000,
4120b2af32SRussell King 	MV_PCS_1000BASEX	= 0x2000,
4220b2af32SRussell King 
438d8963c3SRussell King 	MV_PCS_CSCR1		= 0x8000,
44a585c03eSRussell King 	MV_PCS_CSCR1_ED_MASK	= 0x0300,
45a585c03eSRussell King 	MV_PCS_CSCR1_ED_OFF	= 0x0000,
46a585c03eSRussell King 	MV_PCS_CSCR1_ED_RX	= 0x0200,
47a585c03eSRussell King 	MV_PCS_CSCR1_ED_NLP	= 0x0300,
488d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MASK	= 0x0060,
498d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MDI	= 0x0000,
508d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MDIX	= 0x0020,
518d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_AUTO	= 0x0060,
528d8963c3SRussell King 
53c84786faSRussell King 	MV_PCS_CSSR1		= 0x8008,
54c84786faSRussell King 	MV_PCS_CSSR1_SPD1_MASK	= 0xc000,
55c84786faSRussell King 	MV_PCS_CSSR1_SPD1_SPD2	= 0xc000,
56c84786faSRussell King 	MV_PCS_CSSR1_SPD1_1000	= 0x8000,
57c84786faSRussell King 	MV_PCS_CSSR1_SPD1_100	= 0x4000,
58c84786faSRussell King 	MV_PCS_CSSR1_SPD1_10	= 0x0000,
59c84786faSRussell King 	MV_PCS_CSSR1_DUPLEX_FULL= BIT(13),
60c84786faSRussell King 	MV_PCS_CSSR1_RESOLVED	= BIT(11),
61c84786faSRussell King 	MV_PCS_CSSR1_MDIX	= BIT(6),
62c84786faSRussell King 	MV_PCS_CSSR1_SPD2_MASK	= 0x000c,
63c84786faSRussell King 	MV_PCS_CSSR1_SPD2_5000	= 0x0008,
64c84786faSRussell King 	MV_PCS_CSSR1_SPD2_2500	= 0x0004,
65c84786faSRussell King 	MV_PCS_CSSR1_SPD2_10000	= 0x0000,
66ea4efe25SRussell King 
6720b2af32SRussell King 	/* These registers appear at 0x800X and 0xa00X - the 0xa00X control
6820b2af32SRussell King 	 * registers appear to set themselves to the 0x800X when AN is
6920b2af32SRussell King 	 * restarted, but status registers appear readable from either.
7020b2af32SRussell King 	 */
7120b2af32SRussell King 	MV_AN_CTRL1000		= 0x8000, /* 1000base-T control register */
7220b2af32SRussell King 	MV_AN_STAT1000		= 0x8001, /* 1000base-T status register */
730d3ad854SRussell King 
740d3ad854SRussell King 	/* Vendor2 MMD registers */
75af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL		= 0xf001,
76af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL_PWRDOWN = 0x0800,
770d3ad854SRussell King 	MV_V2_TEMP_CTRL		= 0xf08a,
780d3ad854SRussell King 	MV_V2_TEMP_CTRL_MASK	= 0xc000,
790d3ad854SRussell King 	MV_V2_TEMP_CTRL_SAMPLE	= 0x0000,
800d3ad854SRussell King 	MV_V2_TEMP_CTRL_DISABLE	= 0xc000,
810d3ad854SRussell King 	MV_V2_TEMP		= 0xf08c,
820d3ad854SRussell King 	MV_V2_TEMP_UNKNOWN	= 0x9600, /* unknown function */
830d3ad854SRussell King };
840d3ad854SRussell King 
850d3ad854SRussell King struct mv3310_priv {
860d3ad854SRussell King 	struct device *hwmon_dev;
870d3ad854SRussell King 	char *hwmon_name;
8820b2af32SRussell King };
8920b2af32SRussell King 
900d3ad854SRussell King #ifdef CONFIG_HWMON
910d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data,
920d3ad854SRussell King 				       enum hwmon_sensor_types type,
930d3ad854SRussell King 				       u32 attr, int channel)
940d3ad854SRussell King {
950d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
960d3ad854SRussell King 		return 0444;
970d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input)
980d3ad854SRussell King 		return 0444;
990d3ad854SRussell King 	return 0;
1000d3ad854SRussell King }
1010d3ad854SRussell King 
1020d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1030d3ad854SRussell King 			     u32 attr, int channel, long *value)
1040d3ad854SRussell King {
1050d3ad854SRussell King 	struct phy_device *phydev = dev_get_drvdata(dev);
1060d3ad854SRussell King 	int temp;
1070d3ad854SRussell King 
1080d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
1090d3ad854SRussell King 		*value = MSEC_PER_SEC;
1100d3ad854SRussell King 		return 0;
1110d3ad854SRussell King 	}
1120d3ad854SRussell King 
1130d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input) {
1140d3ad854SRussell King 		temp = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP);
1150d3ad854SRussell King 		if (temp < 0)
1160d3ad854SRussell King 			return temp;
1170d3ad854SRussell King 
1180d3ad854SRussell King 		*value = ((temp & 0xff) - 75) * 1000;
1190d3ad854SRussell King 
1200d3ad854SRussell King 		return 0;
1210d3ad854SRussell King 	}
1220d3ad854SRussell King 
1230d3ad854SRussell King 	return -EOPNOTSUPP;
1240d3ad854SRussell King }
1250d3ad854SRussell King 
1260d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = {
1270d3ad854SRussell King 	.is_visible = mv3310_hwmon_is_visible,
1280d3ad854SRussell King 	.read = mv3310_hwmon_read,
1290d3ad854SRussell King };
1300d3ad854SRussell King 
1310d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = {
1320d3ad854SRussell King 	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
1330d3ad854SRussell King 	0,
1340d3ad854SRussell King };
1350d3ad854SRussell King 
1360d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = {
1370d3ad854SRussell King 	.type = hwmon_chip,
1380d3ad854SRussell King 	.config = mv3310_hwmon_chip_config,
1390d3ad854SRussell King };
1400d3ad854SRussell King 
1410d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = {
1420d3ad854SRussell King 	HWMON_T_INPUT,
1430d3ad854SRussell King 	0,
1440d3ad854SRussell King };
1450d3ad854SRussell King 
1460d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = {
1470d3ad854SRussell King 	.type = hwmon_temp,
1480d3ad854SRussell King 	.config = mv3310_hwmon_temp_config,
1490d3ad854SRussell King };
1500d3ad854SRussell King 
1510d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = {
1520d3ad854SRussell King 	&mv3310_hwmon_chip,
1530d3ad854SRussell King 	&mv3310_hwmon_temp,
1540d3ad854SRussell King 	NULL,
1550d3ad854SRussell King };
1560d3ad854SRussell King 
1570d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = {
1580d3ad854SRussell King 	.ops = &mv3310_hwmon_ops,
1590d3ad854SRussell King 	.info = mv3310_hwmon_info,
1600d3ad854SRussell King };
1610d3ad854SRussell King 
1620d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
1630d3ad854SRussell King {
1640d3ad854SRussell King 	u16 val;
1650d3ad854SRussell King 	int ret;
1660d3ad854SRussell King 
1670d3ad854SRussell King 	ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP,
1680d3ad854SRussell King 			    MV_V2_TEMP_UNKNOWN);
1690d3ad854SRussell King 	if (ret < 0)
1700d3ad854SRussell King 		return ret;
1710d3ad854SRussell King 
1720d3ad854SRussell King 	val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE;
1730d3ad854SRussell King 
174b06d8e5aSHeiner Kallweit 	return phy_modify_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL,
175b06d8e5aSHeiner Kallweit 			      MV_V2_TEMP_CTRL_MASK, val);
1760d3ad854SRussell King }
1770d3ad854SRussell King 
1780d3ad854SRussell King static void mv3310_hwmon_disable(void *data)
1790d3ad854SRussell King {
1800d3ad854SRussell King 	struct phy_device *phydev = data;
1810d3ad854SRussell King 
1820d3ad854SRussell King 	mv3310_hwmon_config(phydev, false);
1830d3ad854SRussell King }
1840d3ad854SRussell King 
1850d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
1860d3ad854SRussell King {
1870d3ad854SRussell King 	struct device *dev = &phydev->mdio.dev;
1880d3ad854SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1890d3ad854SRussell King 	int i, j, ret;
1900d3ad854SRussell King 
1910d3ad854SRussell King 	priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
1920d3ad854SRussell King 	if (!priv->hwmon_name)
1930d3ad854SRussell King 		return -ENODEV;
1940d3ad854SRussell King 
1950d3ad854SRussell King 	for (i = j = 0; priv->hwmon_name[i]; i++) {
1960d3ad854SRussell King 		if (isalnum(priv->hwmon_name[i])) {
1970d3ad854SRussell King 			if (i != j)
1980d3ad854SRussell King 				priv->hwmon_name[j] = priv->hwmon_name[i];
1990d3ad854SRussell King 			j++;
2000d3ad854SRussell King 		}
2010d3ad854SRussell King 	}
2020d3ad854SRussell King 	priv->hwmon_name[j] = '\0';
2030d3ad854SRussell King 
2040d3ad854SRussell King 	ret = mv3310_hwmon_config(phydev, true);
2050d3ad854SRussell King 	if (ret)
2060d3ad854SRussell King 		return ret;
2070d3ad854SRussell King 
2080d3ad854SRussell King 	ret = devm_add_action_or_reset(dev, mv3310_hwmon_disable, phydev);
2090d3ad854SRussell King 	if (ret)
2100d3ad854SRussell King 		return ret;
2110d3ad854SRussell King 
2120d3ad854SRussell King 	priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
2130d3ad854SRussell King 				priv->hwmon_name, phydev,
2140d3ad854SRussell King 				&mv3310_hwmon_chip_info, NULL);
2150d3ad854SRussell King 
2160d3ad854SRussell King 	return PTR_ERR_OR_ZERO(priv->hwmon_dev);
2170d3ad854SRussell King }
2180d3ad854SRussell King #else
2190d3ad854SRussell King static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
2200d3ad854SRussell King {
2210d3ad854SRussell King 	return 0;
2220d3ad854SRussell King }
2230d3ad854SRussell King 
2240d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
2250d3ad854SRussell King {
2260d3ad854SRussell King 	return 0;
2270d3ad854SRussell King }
2280d3ad854SRussell King #endif
2290d3ad854SRussell King 
230*c9cc1c81SRussell King static int mv3310_power_down(struct phy_device *phydev)
231*c9cc1c81SRussell King {
232*c9cc1c81SRussell King 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
233*c9cc1c81SRussell King 				MV_V2_PORT_CTRL_PWRDOWN);
234*c9cc1c81SRussell King }
235*c9cc1c81SRussell King 
236*c9cc1c81SRussell King static int mv3310_power_up(struct phy_device *phydev)
237*c9cc1c81SRussell King {
238*c9cc1c81SRussell King 	return phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
239*c9cc1c81SRussell King 				  MV_V2_PORT_CTRL_PWRDOWN);
240*c9cc1c81SRussell King }
241*c9cc1c81SRussell King 
2428d8963c3SRussell King static int mv3310_reset(struct phy_device *phydev, u32 unit)
2438d8963c3SRussell King {
2448d8963c3SRussell King 	int retries, val, err;
2458d8963c3SRussell King 
2468d8963c3SRussell King 	err = phy_modify_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1,
2478d8963c3SRussell King 			     MDIO_CTRL1_RESET, MDIO_CTRL1_RESET);
2488d8963c3SRussell King 	if (err < 0)
2498d8963c3SRussell King 		return err;
2508d8963c3SRussell King 
2518d8963c3SRussell King 	retries = 20;
2528d8963c3SRussell King 	do {
2538d8963c3SRussell King 		msleep(5);
2548d8963c3SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1);
2558d8963c3SRussell King 		if (val < 0)
2568d8963c3SRussell King 			return val;
2578d8963c3SRussell King 	} while (val & MDIO_CTRL1_RESET && --retries);
2588d8963c3SRussell King 
2598d8963c3SRussell King 	return val & MDIO_CTRL1_RESET ? -ETIMEDOUT : 0;
2608d8963c3SRussell King }
2618d8963c3SRussell King 
262a585c03eSRussell King static int mv3310_get_edpd(struct phy_device *phydev, u16 *edpd)
263a585c03eSRussell King {
264a585c03eSRussell King 	int val;
265a585c03eSRussell King 
266a585c03eSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1);
267a585c03eSRussell King 	if (val < 0)
268a585c03eSRussell King 		return val;
269a585c03eSRussell King 
270a585c03eSRussell King 	switch (val & MV_PCS_CSCR1_ED_MASK) {
271a585c03eSRussell King 	case MV_PCS_CSCR1_ED_NLP:
272a585c03eSRussell King 		*edpd = 1000;
273a585c03eSRussell King 		break;
274a585c03eSRussell King 	case MV_PCS_CSCR1_ED_RX:
275a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_NO_TX;
276a585c03eSRussell King 		break;
277a585c03eSRussell King 	default:
278a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_DISABLE;
279a585c03eSRussell King 		break;
280a585c03eSRussell King 	}
281a585c03eSRussell King 	return 0;
282a585c03eSRussell King }
283a585c03eSRussell King 
284a585c03eSRussell King static int mv3310_set_edpd(struct phy_device *phydev, u16 edpd)
285a585c03eSRussell King {
286a585c03eSRussell King 	u16 val;
287a585c03eSRussell King 	int err;
288a585c03eSRussell King 
289a585c03eSRussell King 	switch (edpd) {
290a585c03eSRussell King 	case 1000:
291a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS:
292a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_NLP;
293a585c03eSRussell King 		break;
294a585c03eSRussell King 
295a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_NO_TX:
296a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_RX;
297a585c03eSRussell King 		break;
298a585c03eSRussell King 
299a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DISABLE:
300a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_OFF;
301a585c03eSRussell King 		break;
302a585c03eSRussell King 
303a585c03eSRussell King 	default:
304a585c03eSRussell King 		return -EINVAL;
305a585c03eSRussell King 	}
306a585c03eSRussell King 
307a585c03eSRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
308a585c03eSRussell King 				     MV_PCS_CSCR1_ED_MASK, val);
309a585c03eSRussell King 	if (err > 0)
310a585c03eSRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
311a585c03eSRussell King 
312a585c03eSRussell King 	return err;
313a585c03eSRussell King }
314a585c03eSRussell King 
31536023da1SRussell King static int mv3310_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
31636023da1SRussell King {
31736023da1SRussell King 	struct phy_device *phydev = upstream;
31836023da1SRussell King 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
31936023da1SRussell King 	phy_interface_t iface;
32036023da1SRussell King 
32136023da1SRussell King 	sfp_parse_support(phydev->sfp_bus, id, support);
322a4516c70SRussell King 	iface = sfp_select_interface(phydev->sfp_bus, support);
32336023da1SRussell King 
324e0f909bcSRussell King 	if (iface != PHY_INTERFACE_MODE_10GBASER) {
32536023da1SRussell King 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
32636023da1SRussell King 		return -EINVAL;
32736023da1SRussell King 	}
32836023da1SRussell King 	return 0;
32936023da1SRussell King }
33036023da1SRussell King 
33136023da1SRussell King static const struct sfp_upstream_ops mv3310_sfp_ops = {
33236023da1SRussell King 	.attach = phy_sfp_attach,
33336023da1SRussell King 	.detach = phy_sfp_detach,
33436023da1SRussell King 	.module_insert = mv3310_sfp_insert,
33536023da1SRussell King };
33636023da1SRussell King 
33720b2af32SRussell King static int mv3310_probe(struct phy_device *phydev)
33820b2af32SRussell King {
3390d3ad854SRussell King 	struct mv3310_priv *priv;
34020b2af32SRussell King 	u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
3410d3ad854SRussell King 	int ret;
34220b2af32SRussell King 
34320b2af32SRussell King 	if (!phydev->is_c45 ||
34420b2af32SRussell King 	    (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
34520b2af32SRussell King 		return -ENODEV;
34620b2af32SRussell King 
3473d3ced2eSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT);
3483d3ced2eSRussell King 	if (ret < 0)
3493d3ced2eSRussell King 		return ret;
3503d3ced2eSRussell King 
3513d3ced2eSRussell King 	if (ret & MV_PMA_BOOT_FATAL) {
3523d3ced2eSRussell King 		dev_warn(&phydev->mdio.dev,
3533d3ced2eSRussell King 			 "PHY failed to boot firmware, status=%04x\n", ret);
3543d3ced2eSRussell King 		return -ENODEV;
3553d3ced2eSRussell King 	}
3563d3ced2eSRussell King 
3570d3ad854SRussell King 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3580d3ad854SRussell King 	if (!priv)
3590d3ad854SRussell King 		return -ENOMEM;
3600d3ad854SRussell King 
3610d3ad854SRussell King 	dev_set_drvdata(&phydev->mdio.dev, priv);
3620d3ad854SRussell King 
363*c9cc1c81SRussell King 	/* Powering down the port when not in use saves about 600mW */
364*c9cc1c81SRussell King 	ret = mv3310_power_down(phydev);
365*c9cc1c81SRussell King 	if (ret)
366*c9cc1c81SRussell King 		return ret;
367*c9cc1c81SRussell King 
3680d3ad854SRussell King 	ret = mv3310_hwmon_probe(phydev);
3690d3ad854SRussell King 	if (ret)
3700d3ad854SRussell King 		return ret;
3710d3ad854SRussell King 
37236023da1SRussell King 	return phy_sfp_probe(phydev, &mv3310_sfp_ops);
37320b2af32SRussell King }
37420b2af32SRussell King 
3750d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev)
3760d3ad854SRussell King {
377*c9cc1c81SRussell King 	return mv3310_power_down(phydev);
3780d3ad854SRussell King }
3790d3ad854SRussell King 
3800d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev)
3810d3ad854SRussell King {
382af3e28cbSAntoine Tenart 	int ret;
383af3e28cbSAntoine Tenart 
384*c9cc1c81SRussell King 	ret = mv3310_power_up(phydev);
385af3e28cbSAntoine Tenart 	if (ret)
386af3e28cbSAntoine Tenart 		return ret;
387af3e28cbSAntoine Tenart 
3880d3ad854SRussell King 	return mv3310_hwmon_config(phydev, true);
3890d3ad854SRussell King }
3900d3ad854SRussell King 
391c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010
392c47455f9SMaxime Chevallier  * don't set bit 14 in PMA Extended Abilities (1.11), although they do
393c47455f9SMaxime Chevallier  * support 2.5GBASET and 5GBASET. For these models, we can still read their
394c47455f9SMaxime Chevallier  * 2.5G/5G extended abilities register (1.21). We detect these models based on
395c47455f9SMaxime Chevallier  * the PMA device identifier, with a mask matching models known to have this
396c47455f9SMaxime Chevallier  * issue
397c47455f9SMaxime Chevallier  */
398c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev)
399c47455f9SMaxime Chevallier {
400c47455f9SMaxime Chevallier 	if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD))
401c47455f9SMaxime Chevallier 		return false;
402c47455f9SMaxime Chevallier 
403c47455f9SMaxime Chevallier 	/* Only some revisions of the 88X3310 family PMA seem to be impacted */
404c47455f9SMaxime Chevallier 	return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
405c47455f9SMaxime Chevallier 		MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
406c47455f9SMaxime Chevallier }
407c47455f9SMaxime Chevallier 
40820b2af32SRussell King static int mv3310_config_init(struct phy_device *phydev)
40920b2af32SRussell King {
410*c9cc1c81SRussell King 	int err;
411*c9cc1c81SRussell King 
41220b2af32SRussell King 	/* Check that the PHY interface type is compatible */
41320b2af32SRussell King 	if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
414e555e5b1SMaxime Chevallier 	    phydev->interface != PHY_INTERFACE_MODE_2500BASEX &&
41520b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_XAUI &&
41620b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
417e0f909bcSRussell King 	    phydev->interface != PHY_INTERFACE_MODE_10GBASER)
41820b2af32SRussell King 		return -ENODEV;
41920b2af32SRussell King 
4208d8963c3SRussell King 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
4218d8963c3SRussell King 
422*c9cc1c81SRussell King 	/* Power up so reset works */
423*c9cc1c81SRussell King 	err = mv3310_power_up(phydev);
424*c9cc1c81SRussell King 	if (err)
425*c9cc1c81SRussell King 		return err;
426*c9cc1c81SRussell King 
427a585c03eSRussell King 	/* Enable EDPD mode - saving 600mW */
428a585c03eSRussell King 	return mv3310_set_edpd(phydev, ETHTOOL_PHY_EDPD_DFLT_TX_MSECS);
42974145424SMaxime Chevallier }
43074145424SMaxime Chevallier 
43174145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev)
43274145424SMaxime Chevallier {
43374145424SMaxime Chevallier 	int ret, val;
43474145424SMaxime Chevallier 
435ac3f5533SMaxime Chevallier 	ret = genphy_c45_pma_read_abilities(phydev);
436ac3f5533SMaxime Chevallier 	if (ret)
437ac3f5533SMaxime Chevallier 		return ret;
43820b2af32SRussell King 
439c47455f9SMaxime Chevallier 	if (mv3310_has_pma_ngbaset_quirk(phydev)) {
440c47455f9SMaxime Chevallier 		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
441c47455f9SMaxime Chevallier 				   MDIO_PMA_NG_EXTABLE);
442c47455f9SMaxime Chevallier 		if (val < 0)
443c47455f9SMaxime Chevallier 			return val;
444c47455f9SMaxime Chevallier 
445c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
446c47455f9SMaxime Chevallier 				 phydev->supported,
447c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_2_5GBT);
448c47455f9SMaxime Chevallier 
449c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
450c47455f9SMaxime Chevallier 				 phydev->supported,
451c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_5GBT);
452c47455f9SMaxime Chevallier 	}
453c47455f9SMaxime Chevallier 
45420b2af32SRussell King 	return 0;
45520b2af32SRussell King }
45620b2af32SRussell King 
4578d8963c3SRussell King static int mv3310_config_mdix(struct phy_device *phydev)
4588d8963c3SRussell King {
4598d8963c3SRussell King 	u16 val;
4608d8963c3SRussell King 	int err;
4618d8963c3SRussell King 
4628d8963c3SRussell King 	switch (phydev->mdix_ctrl) {
4638d8963c3SRussell King 	case ETH_TP_MDI_AUTO:
4648d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_AUTO;
4658d8963c3SRussell King 		break;
4668d8963c3SRussell King 	case ETH_TP_MDI_X:
4678d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDIX;
4688d8963c3SRussell King 		break;
4698d8963c3SRussell King 	case ETH_TP_MDI:
4708d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDI;
4718d8963c3SRussell King 		break;
4728d8963c3SRussell King 	default:
4738d8963c3SRussell King 		return -EINVAL;
4748d8963c3SRussell King 	}
4758d8963c3SRussell King 
4768d8963c3SRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
4778d8963c3SRussell King 				     MV_PCS_CSCR1_MDIX_MASK, val);
4788d8963c3SRussell King 	if (err > 0)
4798d8963c3SRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
4808d8963c3SRussell King 
4818d8963c3SRussell King 	return err;
4828d8963c3SRussell King }
4838d8963c3SRussell King 
48420b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev)
48520b2af32SRussell King {
48620b2af32SRussell King 	bool changed = false;
4873c1bcc86SAndrew Lunn 	u16 reg;
48820b2af32SRussell King 	int ret;
48920b2af32SRussell King 
4908d8963c3SRussell King 	ret = mv3310_config_mdix(phydev);
4918d8963c3SRussell King 	if (ret < 0)
4928d8963c3SRussell King 		return ret;
493ea4efe25SRussell King 
49430de65c3SHeiner Kallweit 	if (phydev->autoneg == AUTONEG_DISABLE)
49530de65c3SHeiner Kallweit 		return genphy_c45_pma_setup_forced(phydev);
49620b2af32SRussell King 
4973de97f3cSAndrew Lunn 	ret = genphy_c45_an_config_aneg(phydev);
49820b2af32SRussell King 	if (ret < 0)
49920b2af32SRussell King 		return ret;
50020b2af32SRussell King 	if (ret > 0)
50120b2af32SRussell King 		changed = true;
50220b2af32SRussell King 
5033de97f3cSAndrew Lunn 	/* Clause 45 has no standardized support for 1000BaseT, therefore
5043de97f3cSAndrew Lunn 	 * use vendor registers for this mode.
5053de97f3cSAndrew Lunn 	 */
5063c1bcc86SAndrew Lunn 	reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
507b06d8e5aSHeiner Kallweit 	ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
5083c1bcc86SAndrew Lunn 			     ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg);
50920b2af32SRussell King 	if (ret < 0)
51020b2af32SRussell King 		return ret;
51120b2af32SRussell King 	if (ret > 0)
51220b2af32SRussell King 		changed = true;
51320b2af32SRussell King 
5146b4cb6cbSHeiner Kallweit 	return genphy_c45_check_and_restart_aneg(phydev, changed);
51520b2af32SRussell King }
51620b2af32SRussell King 
51720b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev)
51820b2af32SRussell King {
51920b2af32SRussell King 	int val;
52020b2af32SRussell King 
52120b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
52220b2af32SRussell King 	if (val < 0)
52320b2af32SRussell King 		return val;
52420b2af32SRussell King 
52520b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
52620b2af32SRussell King 		return 1;
52720b2af32SRussell King 
52820b2af32SRussell King 	return genphy_c45_aneg_done(phydev);
52920b2af32SRussell King }
53020b2af32SRussell King 
53136c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev)
53236c4449aSRussell King {
53336c4449aSRussell King 	if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
534e555e5b1SMaxime Chevallier 	     phydev->interface == PHY_INTERFACE_MODE_2500BASEX ||
535e0f909bcSRussell King 	     phydev->interface == PHY_INTERFACE_MODE_10GBASER) &&
536e0f909bcSRussell King 	    phydev->link) {
53736c4449aSRussell King 		/* The PHY automatically switches its serdes interface (and
538e0f909bcSRussell King 		 * active PHYXS instance) between Cisco SGMII, 10GBase-R and
539e555e5b1SMaxime Chevallier 		 * 2500BaseX modes according to the speed.  Florian suggests
540e555e5b1SMaxime Chevallier 		 * setting phydev->interface to communicate this to the MAC.
541e555e5b1SMaxime Chevallier 		 * Only do this if we are already in one of the above modes.
54236c4449aSRussell King 		 */
543e555e5b1SMaxime Chevallier 		switch (phydev->speed) {
544e555e5b1SMaxime Chevallier 		case SPEED_10000:
545e0f909bcSRussell King 			phydev->interface = PHY_INTERFACE_MODE_10GBASER;
546e555e5b1SMaxime Chevallier 			break;
547e555e5b1SMaxime Chevallier 		case SPEED_2500:
548e555e5b1SMaxime Chevallier 			phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
549e555e5b1SMaxime Chevallier 			break;
550e555e5b1SMaxime Chevallier 		case SPEED_1000:
551e555e5b1SMaxime Chevallier 		case SPEED_100:
552e555e5b1SMaxime Chevallier 		case SPEED_10:
55336c4449aSRussell King 			phydev->interface = PHY_INTERFACE_MODE_SGMII;
554e555e5b1SMaxime Chevallier 			break;
555e555e5b1SMaxime Chevallier 		default:
556e555e5b1SMaxime Chevallier 			break;
557e555e5b1SMaxime Chevallier 		}
55836c4449aSRussell King 	}
55936c4449aSRussell King }
56036c4449aSRussell King 
56120b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
562c84786faSRussell King static int mv3310_read_status_10gbaser(struct phy_device *phydev)
56320b2af32SRussell King {
56420b2af32SRussell King 	phydev->link = 1;
56520b2af32SRussell King 	phydev->speed = SPEED_10000;
56620b2af32SRussell King 	phydev->duplex = DUPLEX_FULL;
56720b2af32SRussell King 
56820b2af32SRussell King 	return 0;
56920b2af32SRussell King }
57020b2af32SRussell King 
571c84786faSRussell King static int mv3310_read_status_copper(struct phy_device *phydev)
57220b2af32SRussell King {
573c84786faSRussell King 	int cssr1, speed, val;
57420b2af32SRussell King 
575998a8a83SHeiner Kallweit 	val = genphy_c45_read_link(phydev);
57620b2af32SRussell King 	if (val < 0)
57720b2af32SRussell King 		return val;
57820b2af32SRussell King 
57920b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
58020b2af32SRussell King 	if (val < 0)
58120b2af32SRussell King 		return val;
58220b2af32SRussell King 
583c84786faSRussell King 	cssr1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSSR1);
584c84786faSRussell King 	if (cssr1 < 0)
585c84786faSRussell King 		return val;
586c84786faSRussell King 
587c84786faSRussell King 	/* If the link settings are not resolved, mark the link down */
588c84786faSRussell King 	if (!(cssr1 & MV_PCS_CSSR1_RESOLVED)) {
589c84786faSRussell King 		phydev->link = 0;
590c84786faSRussell King 		return 0;
591c84786faSRussell King 	}
592c84786faSRussell King 
593c84786faSRussell King 	/* Read the copper link settings */
594c84786faSRussell King 	speed = cssr1 & MV_PCS_CSSR1_SPD1_MASK;
595c84786faSRussell King 	if (speed == MV_PCS_CSSR1_SPD1_SPD2)
596c84786faSRussell King 		speed |= cssr1 & MV_PCS_CSSR1_SPD2_MASK;
597c84786faSRussell King 
598c84786faSRussell King 	switch (speed) {
599c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_10000:
600c84786faSRussell King 		phydev->speed = SPEED_10000;
601c84786faSRussell King 		break;
602c84786faSRussell King 
603c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_5000:
604c84786faSRussell King 		phydev->speed = SPEED_5000;
605c84786faSRussell King 		break;
606c84786faSRussell King 
607c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_2500:
608c84786faSRussell King 		phydev->speed = SPEED_2500;
609c84786faSRussell King 		break;
610c84786faSRussell King 
611c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_1000:
612c84786faSRussell King 		phydev->speed = SPEED_1000;
613c84786faSRussell King 		break;
614c84786faSRussell King 
615c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_100:
616c84786faSRussell King 		phydev->speed = SPEED_100;
617c84786faSRussell King 		break;
618c84786faSRussell King 
619c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_10:
620c84786faSRussell King 		phydev->speed = SPEED_10;
621c84786faSRussell King 		break;
622c84786faSRussell King 	}
623c84786faSRussell King 
624c84786faSRussell King 	phydev->duplex = cssr1 & MV_PCS_CSSR1_DUPLEX_FULL ?
625c84786faSRussell King 			 DUPLEX_FULL : DUPLEX_HALF;
626c84786faSRussell King 	phydev->mdix = cssr1 & MV_PCS_CSSR1_MDIX ?
627c84786faSRussell King 		       ETH_TP_MDI_X : ETH_TP_MDI;
628c84786faSRussell King 
62920b2af32SRussell King 	if (val & MDIO_AN_STAT1_COMPLETE) {
63020b2af32SRussell King 		val = genphy_c45_read_lpa(phydev);
63120b2af32SRussell King 		if (val < 0)
63220b2af32SRussell King 			return val;
63320b2af32SRussell King 
634cc1122b0SColin Ian King 		/* Read the link partner's 1G advertisement */
63520b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
63620b2af32SRussell King 		if (val < 0)
63720b2af32SRussell King 			return val;
63820b2af32SRussell King 
63978a24df3SAndrew Lunn 		mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
64020b2af32SRussell King 
641c84786faSRussell King 		/* Update the pause status */
642c84786faSRussell King 		phy_resolve_aneg_pause(phydev);
64320b2af32SRussell King 	}
64420b2af32SRussell King 
645c84786faSRussell King 	return 0;
64620b2af32SRussell King }
64720b2af32SRussell King 
648c84786faSRussell King static int mv3310_read_status(struct phy_device *phydev)
649c84786faSRussell King {
650c84786faSRussell King 	int err, val;
651ea4efe25SRussell King 
652c84786faSRussell King 	phydev->speed = SPEED_UNKNOWN;
653c84786faSRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
654c84786faSRussell King 	linkmode_zero(phydev->lp_advertising);
655c84786faSRussell King 	phydev->link = 0;
656c84786faSRussell King 	phydev->pause = 0;
657c84786faSRussell King 	phydev->asym_pause = 0;
658ea4efe25SRussell King 	phydev->mdix = ETH_TP_MDI_INVALID;
659ea4efe25SRussell King 
660c84786faSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
661c84786faSRussell King 	if (val < 0)
662c84786faSRussell King 		return val;
663c84786faSRussell King 
664c84786faSRussell King 	if (val & MDIO_STAT1_LSTATUS)
665c84786faSRussell King 		err = mv3310_read_status_10gbaser(phydev);
666c84786faSRussell King 	else
667c84786faSRussell King 		err = mv3310_read_status_copper(phydev);
668c84786faSRussell King 	if (err < 0)
669c84786faSRussell King 		return err;
670c84786faSRussell King 
671c84786faSRussell King 	if (phydev->link)
67236c4449aSRussell King 		mv3310_update_interface(phydev);
67320b2af32SRussell King 
67420b2af32SRussell King 	return 0;
67520b2af32SRussell King }
67620b2af32SRussell King 
677a585c03eSRussell King static int mv3310_get_tunable(struct phy_device *phydev,
678a585c03eSRussell King 			      struct ethtool_tunable *tuna, void *data)
679a585c03eSRussell King {
680a585c03eSRussell King 	switch (tuna->id) {
681a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
682a585c03eSRussell King 		return mv3310_get_edpd(phydev, data);
683a585c03eSRussell King 	default:
684a585c03eSRussell King 		return -EOPNOTSUPP;
685a585c03eSRussell King 	}
686a585c03eSRussell King }
687a585c03eSRussell King 
688a585c03eSRussell King static int mv3310_set_tunable(struct phy_device *phydev,
689a585c03eSRussell King 			      struct ethtool_tunable *tuna, const void *data)
690a585c03eSRussell King {
691a585c03eSRussell King 	switch (tuna->id) {
692a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
693a585c03eSRussell King 		return mv3310_set_edpd(phydev, *(u16 *)data);
694a585c03eSRussell King 	default:
695a585c03eSRussell King 		return -EOPNOTSUPP;
696a585c03eSRussell King 	}
697a585c03eSRussell King }
698a585c03eSRussell King 
69920b2af32SRussell King static struct phy_driver mv3310_drivers[] = {
70020b2af32SRussell King 	{
701631ba906SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88X3310,
702952b6b3bSAntoine Tenart 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
70320b2af32SRussell King 		.name		= "mv88x3310",
70474145424SMaxime Chevallier 		.get_features	= mv3310_get_features,
7057be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
70620b2af32SRussell King 		.config_init	= mv3310_config_init,
7070d3ad854SRussell King 		.probe		= mv3310_probe,
7080d3ad854SRussell King 		.suspend	= mv3310_suspend,
7090d3ad854SRussell King 		.resume		= mv3310_resume,
71020b2af32SRussell King 		.config_aneg	= mv3310_config_aneg,
71120b2af32SRussell King 		.aneg_done	= mv3310_aneg_done,
71220b2af32SRussell King 		.read_status	= mv3310_read_status,
713a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
714a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
71520b2af32SRussell King 	},
71662d01535SMaxime Chevallier 	{
71762d01535SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88E2110,
71862d01535SMaxime Chevallier 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
71962d01535SMaxime Chevallier 		.name		= "mv88x2110",
72062d01535SMaxime Chevallier 		.probe		= mv3310_probe,
721e02c4a9dSAntoine Tenart 		.suspend	= mv3310_suspend,
722e02c4a9dSAntoine Tenart 		.resume		= mv3310_resume,
7237be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
72462d01535SMaxime Chevallier 		.config_init	= mv3310_config_init,
72562d01535SMaxime Chevallier 		.config_aneg	= mv3310_config_aneg,
72662d01535SMaxime Chevallier 		.aneg_done	= mv3310_aneg_done,
72762d01535SMaxime Chevallier 		.read_status	= mv3310_read_status,
728a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
729a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
73062d01535SMaxime Chevallier 	},
73120b2af32SRussell King };
73220b2af32SRussell King 
73320b2af32SRussell King module_phy_driver(mv3310_drivers);
73420b2af32SRussell King 
73520b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
736631ba906SMaxime Chevallier 	{ MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_MASK },
73762d01535SMaxime Chevallier 	{ MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK },
73820b2af32SRussell King 	{ },
73920b2af32SRussell King };
74020b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
74120b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
74220b2af32SRussell King MODULE_LICENSE("GPL");
743