xref: /linux/drivers/net/phy/marvell10g.c (revision a585c03e63fd3537cdf94e52485699617424453c)
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,
44*a585c03eSRussell King 	MV_PCS_CSCR1_ED_MASK	= 0x0300,
45*a585c03eSRussell King 	MV_PCS_CSCR1_ED_OFF	= 0x0000,
46*a585c03eSRussell King 	MV_PCS_CSCR1_ED_RX	= 0x0200,
47*a585c03eSRussell 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 
2308d8963c3SRussell King static int mv3310_reset(struct phy_device *phydev, u32 unit)
2318d8963c3SRussell King {
2328d8963c3SRussell King 	int retries, val, err;
2338d8963c3SRussell King 
2348d8963c3SRussell King 	err = phy_modify_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1,
2358d8963c3SRussell King 			     MDIO_CTRL1_RESET, MDIO_CTRL1_RESET);
2368d8963c3SRussell King 	if (err < 0)
2378d8963c3SRussell King 		return err;
2388d8963c3SRussell King 
2398d8963c3SRussell King 	retries = 20;
2408d8963c3SRussell King 	do {
2418d8963c3SRussell King 		msleep(5);
2428d8963c3SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1);
2438d8963c3SRussell King 		if (val < 0)
2448d8963c3SRussell King 			return val;
2458d8963c3SRussell King 	} while (val & MDIO_CTRL1_RESET && --retries);
2468d8963c3SRussell King 
2478d8963c3SRussell King 	return val & MDIO_CTRL1_RESET ? -ETIMEDOUT : 0;
2488d8963c3SRussell King }
2498d8963c3SRussell King 
250*a585c03eSRussell King static int mv3310_get_edpd(struct phy_device *phydev, u16 *edpd)
251*a585c03eSRussell King {
252*a585c03eSRussell King 	int val;
253*a585c03eSRussell King 
254*a585c03eSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1);
255*a585c03eSRussell King 	if (val < 0)
256*a585c03eSRussell King 		return val;
257*a585c03eSRussell King 
258*a585c03eSRussell King 	switch (val & MV_PCS_CSCR1_ED_MASK) {
259*a585c03eSRussell King 	case MV_PCS_CSCR1_ED_NLP:
260*a585c03eSRussell King 		*edpd = 1000;
261*a585c03eSRussell King 		break;
262*a585c03eSRussell King 	case MV_PCS_CSCR1_ED_RX:
263*a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_NO_TX;
264*a585c03eSRussell King 		break;
265*a585c03eSRussell King 	default:
266*a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_DISABLE;
267*a585c03eSRussell King 		break;
268*a585c03eSRussell King 	}
269*a585c03eSRussell King 	return 0;
270*a585c03eSRussell King }
271*a585c03eSRussell King 
272*a585c03eSRussell King static int mv3310_set_edpd(struct phy_device *phydev, u16 edpd)
273*a585c03eSRussell King {
274*a585c03eSRussell King 	u16 val;
275*a585c03eSRussell King 	int err;
276*a585c03eSRussell King 
277*a585c03eSRussell King 	switch (edpd) {
278*a585c03eSRussell King 	case 1000:
279*a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS:
280*a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_NLP;
281*a585c03eSRussell King 		break;
282*a585c03eSRussell King 
283*a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_NO_TX:
284*a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_RX;
285*a585c03eSRussell King 		break;
286*a585c03eSRussell King 
287*a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DISABLE:
288*a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_OFF;
289*a585c03eSRussell King 		break;
290*a585c03eSRussell King 
291*a585c03eSRussell King 	default:
292*a585c03eSRussell King 		return -EINVAL;
293*a585c03eSRussell King 	}
294*a585c03eSRussell King 
295*a585c03eSRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
296*a585c03eSRussell King 				     MV_PCS_CSCR1_ED_MASK, val);
297*a585c03eSRussell King 	if (err > 0)
298*a585c03eSRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
299*a585c03eSRussell King 
300*a585c03eSRussell King 	return err;
301*a585c03eSRussell King }
302*a585c03eSRussell King 
30336023da1SRussell King static int mv3310_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
30436023da1SRussell King {
30536023da1SRussell King 	struct phy_device *phydev = upstream;
30636023da1SRussell King 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
30736023da1SRussell King 	phy_interface_t iface;
30836023da1SRussell King 
30936023da1SRussell King 	sfp_parse_support(phydev->sfp_bus, id, support);
310a4516c70SRussell King 	iface = sfp_select_interface(phydev->sfp_bus, support);
31136023da1SRussell King 
312e0f909bcSRussell King 	if (iface != PHY_INTERFACE_MODE_10GBASER) {
31336023da1SRussell King 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
31436023da1SRussell King 		return -EINVAL;
31536023da1SRussell King 	}
31636023da1SRussell King 	return 0;
31736023da1SRussell King }
31836023da1SRussell King 
31936023da1SRussell King static const struct sfp_upstream_ops mv3310_sfp_ops = {
32036023da1SRussell King 	.attach = phy_sfp_attach,
32136023da1SRussell King 	.detach = phy_sfp_detach,
32236023da1SRussell King 	.module_insert = mv3310_sfp_insert,
32336023da1SRussell King };
32436023da1SRussell King 
32520b2af32SRussell King static int mv3310_probe(struct phy_device *phydev)
32620b2af32SRussell King {
3270d3ad854SRussell King 	struct mv3310_priv *priv;
32820b2af32SRussell King 	u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
3290d3ad854SRussell King 	int ret;
33020b2af32SRussell King 
33120b2af32SRussell King 	if (!phydev->is_c45 ||
33220b2af32SRussell King 	    (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
33320b2af32SRussell King 		return -ENODEV;
33420b2af32SRussell King 
3353d3ced2eSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT);
3363d3ced2eSRussell King 	if (ret < 0)
3373d3ced2eSRussell King 		return ret;
3383d3ced2eSRussell King 
3393d3ced2eSRussell King 	if (ret & MV_PMA_BOOT_FATAL) {
3403d3ced2eSRussell King 		dev_warn(&phydev->mdio.dev,
3413d3ced2eSRussell King 			 "PHY failed to boot firmware, status=%04x\n", ret);
3423d3ced2eSRussell King 		return -ENODEV;
3433d3ced2eSRussell King 	}
3443d3ced2eSRussell King 
3450d3ad854SRussell King 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
3460d3ad854SRussell King 	if (!priv)
3470d3ad854SRussell King 		return -ENOMEM;
3480d3ad854SRussell King 
3490d3ad854SRussell King 	dev_set_drvdata(&phydev->mdio.dev, priv);
3500d3ad854SRussell King 
3510d3ad854SRussell King 	ret = mv3310_hwmon_probe(phydev);
3520d3ad854SRussell King 	if (ret)
3530d3ad854SRussell King 		return ret;
3540d3ad854SRussell King 
35536023da1SRussell King 	return phy_sfp_probe(phydev, &mv3310_sfp_ops);
35620b2af32SRussell King }
35720b2af32SRussell King 
3580d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev)
3590d3ad854SRussell King {
360af3e28cbSAntoine Tenart 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
361af3e28cbSAntoine Tenart 				MV_V2_PORT_CTRL_PWRDOWN);
3620d3ad854SRussell King }
3630d3ad854SRussell King 
3640d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev)
3650d3ad854SRussell King {
366af3e28cbSAntoine Tenart 	int ret;
367af3e28cbSAntoine Tenart 
368af3e28cbSAntoine Tenart 	ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
369af3e28cbSAntoine Tenart 				 MV_V2_PORT_CTRL_PWRDOWN);
370af3e28cbSAntoine Tenart 	if (ret)
371af3e28cbSAntoine Tenart 		return ret;
372af3e28cbSAntoine Tenart 
3730d3ad854SRussell King 	return mv3310_hwmon_config(phydev, true);
3740d3ad854SRussell King }
3750d3ad854SRussell King 
376c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010
377c47455f9SMaxime Chevallier  * don't set bit 14 in PMA Extended Abilities (1.11), although they do
378c47455f9SMaxime Chevallier  * support 2.5GBASET and 5GBASET. For these models, we can still read their
379c47455f9SMaxime Chevallier  * 2.5G/5G extended abilities register (1.21). We detect these models based on
380c47455f9SMaxime Chevallier  * the PMA device identifier, with a mask matching models known to have this
381c47455f9SMaxime Chevallier  * issue
382c47455f9SMaxime Chevallier  */
383c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev)
384c47455f9SMaxime Chevallier {
385c47455f9SMaxime Chevallier 	if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD))
386c47455f9SMaxime Chevallier 		return false;
387c47455f9SMaxime Chevallier 
388c47455f9SMaxime Chevallier 	/* Only some revisions of the 88X3310 family PMA seem to be impacted */
389c47455f9SMaxime Chevallier 	return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
390c47455f9SMaxime Chevallier 		MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
391c47455f9SMaxime Chevallier }
392c47455f9SMaxime Chevallier 
39320b2af32SRussell King static int mv3310_config_init(struct phy_device *phydev)
39420b2af32SRussell King {
39520b2af32SRussell King 	/* Check that the PHY interface type is compatible */
39620b2af32SRussell King 	if (phydev->interface != PHY_INTERFACE_MODE_SGMII &&
397e555e5b1SMaxime Chevallier 	    phydev->interface != PHY_INTERFACE_MODE_2500BASEX &&
39820b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_XAUI &&
39920b2af32SRussell King 	    phydev->interface != PHY_INTERFACE_MODE_RXAUI &&
400e0f909bcSRussell King 	    phydev->interface != PHY_INTERFACE_MODE_10GBASER)
40120b2af32SRussell King 		return -ENODEV;
40220b2af32SRussell King 
4038d8963c3SRussell King 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
4048d8963c3SRussell King 
405*a585c03eSRussell King 	/* Enable EDPD mode - saving 600mW */
406*a585c03eSRussell King 	return mv3310_set_edpd(phydev, ETHTOOL_PHY_EDPD_DFLT_TX_MSECS);
40774145424SMaxime Chevallier }
40874145424SMaxime Chevallier 
40974145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev)
41074145424SMaxime Chevallier {
41174145424SMaxime Chevallier 	int ret, val;
41274145424SMaxime Chevallier 
413ac3f5533SMaxime Chevallier 	ret = genphy_c45_pma_read_abilities(phydev);
414ac3f5533SMaxime Chevallier 	if (ret)
415ac3f5533SMaxime Chevallier 		return ret;
41620b2af32SRussell King 
417c47455f9SMaxime Chevallier 	if (mv3310_has_pma_ngbaset_quirk(phydev)) {
418c47455f9SMaxime Chevallier 		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
419c47455f9SMaxime Chevallier 				   MDIO_PMA_NG_EXTABLE);
420c47455f9SMaxime Chevallier 		if (val < 0)
421c47455f9SMaxime Chevallier 			return val;
422c47455f9SMaxime Chevallier 
423c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
424c47455f9SMaxime Chevallier 				 phydev->supported,
425c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_2_5GBT);
426c47455f9SMaxime Chevallier 
427c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
428c47455f9SMaxime Chevallier 				 phydev->supported,
429c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_5GBT);
430c47455f9SMaxime Chevallier 	}
431c47455f9SMaxime Chevallier 
43220b2af32SRussell King 	return 0;
43320b2af32SRussell King }
43420b2af32SRussell King 
4358d8963c3SRussell King static int mv3310_config_mdix(struct phy_device *phydev)
4368d8963c3SRussell King {
4378d8963c3SRussell King 	u16 val;
4388d8963c3SRussell King 	int err;
4398d8963c3SRussell King 
4408d8963c3SRussell King 	switch (phydev->mdix_ctrl) {
4418d8963c3SRussell King 	case ETH_TP_MDI_AUTO:
4428d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_AUTO;
4438d8963c3SRussell King 		break;
4448d8963c3SRussell King 	case ETH_TP_MDI_X:
4458d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDIX;
4468d8963c3SRussell King 		break;
4478d8963c3SRussell King 	case ETH_TP_MDI:
4488d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDI;
4498d8963c3SRussell King 		break;
4508d8963c3SRussell King 	default:
4518d8963c3SRussell King 		return -EINVAL;
4528d8963c3SRussell King 	}
4538d8963c3SRussell King 
4548d8963c3SRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
4558d8963c3SRussell King 				     MV_PCS_CSCR1_MDIX_MASK, val);
4568d8963c3SRussell King 	if (err > 0)
4578d8963c3SRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
4588d8963c3SRussell King 
4598d8963c3SRussell King 	return err;
4608d8963c3SRussell King }
4618d8963c3SRussell King 
46220b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev)
46320b2af32SRussell King {
46420b2af32SRussell King 	bool changed = false;
4653c1bcc86SAndrew Lunn 	u16 reg;
46620b2af32SRussell King 	int ret;
46720b2af32SRussell King 
4688d8963c3SRussell King 	ret = mv3310_config_mdix(phydev);
4698d8963c3SRussell King 	if (ret < 0)
4708d8963c3SRussell King 		return ret;
471ea4efe25SRussell King 
47230de65c3SHeiner Kallweit 	if (phydev->autoneg == AUTONEG_DISABLE)
47330de65c3SHeiner Kallweit 		return genphy_c45_pma_setup_forced(phydev);
47420b2af32SRussell King 
4753de97f3cSAndrew Lunn 	ret = genphy_c45_an_config_aneg(phydev);
47620b2af32SRussell King 	if (ret < 0)
47720b2af32SRussell King 		return ret;
47820b2af32SRussell King 	if (ret > 0)
47920b2af32SRussell King 		changed = true;
48020b2af32SRussell King 
4813de97f3cSAndrew Lunn 	/* Clause 45 has no standardized support for 1000BaseT, therefore
4823de97f3cSAndrew Lunn 	 * use vendor registers for this mode.
4833de97f3cSAndrew Lunn 	 */
4843c1bcc86SAndrew Lunn 	reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
485b06d8e5aSHeiner Kallweit 	ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
4863c1bcc86SAndrew Lunn 			     ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg);
48720b2af32SRussell King 	if (ret < 0)
48820b2af32SRussell King 		return ret;
48920b2af32SRussell King 	if (ret > 0)
49020b2af32SRussell King 		changed = true;
49120b2af32SRussell King 
4926b4cb6cbSHeiner Kallweit 	return genphy_c45_check_and_restart_aneg(phydev, changed);
49320b2af32SRussell King }
49420b2af32SRussell King 
49520b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev)
49620b2af32SRussell King {
49720b2af32SRussell King 	int val;
49820b2af32SRussell King 
49920b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
50020b2af32SRussell King 	if (val < 0)
50120b2af32SRussell King 		return val;
50220b2af32SRussell King 
50320b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
50420b2af32SRussell King 		return 1;
50520b2af32SRussell King 
50620b2af32SRussell King 	return genphy_c45_aneg_done(phydev);
50720b2af32SRussell King }
50820b2af32SRussell King 
50936c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev)
51036c4449aSRussell King {
51136c4449aSRussell King 	if ((phydev->interface == PHY_INTERFACE_MODE_SGMII ||
512e555e5b1SMaxime Chevallier 	     phydev->interface == PHY_INTERFACE_MODE_2500BASEX ||
513e0f909bcSRussell King 	     phydev->interface == PHY_INTERFACE_MODE_10GBASER) &&
514e0f909bcSRussell King 	    phydev->link) {
51536c4449aSRussell King 		/* The PHY automatically switches its serdes interface (and
516e0f909bcSRussell King 		 * active PHYXS instance) between Cisco SGMII, 10GBase-R and
517e555e5b1SMaxime Chevallier 		 * 2500BaseX modes according to the speed.  Florian suggests
518e555e5b1SMaxime Chevallier 		 * setting phydev->interface to communicate this to the MAC.
519e555e5b1SMaxime Chevallier 		 * Only do this if we are already in one of the above modes.
52036c4449aSRussell King 		 */
521e555e5b1SMaxime Chevallier 		switch (phydev->speed) {
522e555e5b1SMaxime Chevallier 		case SPEED_10000:
523e0f909bcSRussell King 			phydev->interface = PHY_INTERFACE_MODE_10GBASER;
524e555e5b1SMaxime Chevallier 			break;
525e555e5b1SMaxime Chevallier 		case SPEED_2500:
526e555e5b1SMaxime Chevallier 			phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
527e555e5b1SMaxime Chevallier 			break;
528e555e5b1SMaxime Chevallier 		case SPEED_1000:
529e555e5b1SMaxime Chevallier 		case SPEED_100:
530e555e5b1SMaxime Chevallier 		case SPEED_10:
53136c4449aSRussell King 			phydev->interface = PHY_INTERFACE_MODE_SGMII;
532e555e5b1SMaxime Chevallier 			break;
533e555e5b1SMaxime Chevallier 		default:
534e555e5b1SMaxime Chevallier 			break;
535e555e5b1SMaxime Chevallier 		}
53636c4449aSRussell King 	}
53736c4449aSRussell King }
53836c4449aSRussell King 
53920b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
540c84786faSRussell King static int mv3310_read_status_10gbaser(struct phy_device *phydev)
54120b2af32SRussell King {
54220b2af32SRussell King 	phydev->link = 1;
54320b2af32SRussell King 	phydev->speed = SPEED_10000;
54420b2af32SRussell King 	phydev->duplex = DUPLEX_FULL;
54520b2af32SRussell King 
54620b2af32SRussell King 	return 0;
54720b2af32SRussell King }
54820b2af32SRussell King 
549c84786faSRussell King static int mv3310_read_status_copper(struct phy_device *phydev)
55020b2af32SRussell King {
551c84786faSRussell King 	int cssr1, speed, val;
55220b2af32SRussell King 
553998a8a83SHeiner Kallweit 	val = genphy_c45_read_link(phydev);
55420b2af32SRussell King 	if (val < 0)
55520b2af32SRussell King 		return val;
55620b2af32SRussell King 
55720b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
55820b2af32SRussell King 	if (val < 0)
55920b2af32SRussell King 		return val;
56020b2af32SRussell King 
561c84786faSRussell King 	cssr1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSSR1);
562c84786faSRussell King 	if (cssr1 < 0)
563c84786faSRussell King 		return val;
564c84786faSRussell King 
565c84786faSRussell King 	/* If the link settings are not resolved, mark the link down */
566c84786faSRussell King 	if (!(cssr1 & MV_PCS_CSSR1_RESOLVED)) {
567c84786faSRussell King 		phydev->link = 0;
568c84786faSRussell King 		return 0;
569c84786faSRussell King 	}
570c84786faSRussell King 
571c84786faSRussell King 	/* Read the copper link settings */
572c84786faSRussell King 	speed = cssr1 & MV_PCS_CSSR1_SPD1_MASK;
573c84786faSRussell King 	if (speed == MV_PCS_CSSR1_SPD1_SPD2)
574c84786faSRussell King 		speed |= cssr1 & MV_PCS_CSSR1_SPD2_MASK;
575c84786faSRussell King 
576c84786faSRussell King 	switch (speed) {
577c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_10000:
578c84786faSRussell King 		phydev->speed = SPEED_10000;
579c84786faSRussell King 		break;
580c84786faSRussell King 
581c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_5000:
582c84786faSRussell King 		phydev->speed = SPEED_5000;
583c84786faSRussell King 		break;
584c84786faSRussell King 
585c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_2500:
586c84786faSRussell King 		phydev->speed = SPEED_2500;
587c84786faSRussell King 		break;
588c84786faSRussell King 
589c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_1000:
590c84786faSRussell King 		phydev->speed = SPEED_1000;
591c84786faSRussell King 		break;
592c84786faSRussell King 
593c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_100:
594c84786faSRussell King 		phydev->speed = SPEED_100;
595c84786faSRussell King 		break;
596c84786faSRussell King 
597c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_10:
598c84786faSRussell King 		phydev->speed = SPEED_10;
599c84786faSRussell King 		break;
600c84786faSRussell King 	}
601c84786faSRussell King 
602c84786faSRussell King 	phydev->duplex = cssr1 & MV_PCS_CSSR1_DUPLEX_FULL ?
603c84786faSRussell King 			 DUPLEX_FULL : DUPLEX_HALF;
604c84786faSRussell King 	phydev->mdix = cssr1 & MV_PCS_CSSR1_MDIX ?
605c84786faSRussell King 		       ETH_TP_MDI_X : ETH_TP_MDI;
606c84786faSRussell King 
60720b2af32SRussell King 	if (val & MDIO_AN_STAT1_COMPLETE) {
60820b2af32SRussell King 		val = genphy_c45_read_lpa(phydev);
60920b2af32SRussell King 		if (val < 0)
61020b2af32SRussell King 			return val;
61120b2af32SRussell King 
612cc1122b0SColin Ian King 		/* Read the link partner's 1G advertisement */
61320b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
61420b2af32SRussell King 		if (val < 0)
61520b2af32SRussell King 			return val;
61620b2af32SRussell King 
61778a24df3SAndrew Lunn 		mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
61820b2af32SRussell King 
619c84786faSRussell King 		/* Update the pause status */
620c84786faSRussell King 		phy_resolve_aneg_pause(phydev);
62120b2af32SRussell King 	}
62220b2af32SRussell King 
623c84786faSRussell King 	return 0;
62420b2af32SRussell King }
62520b2af32SRussell King 
626c84786faSRussell King static int mv3310_read_status(struct phy_device *phydev)
627c84786faSRussell King {
628c84786faSRussell King 	int err, val;
629ea4efe25SRussell King 
630c84786faSRussell King 	phydev->speed = SPEED_UNKNOWN;
631c84786faSRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
632c84786faSRussell King 	linkmode_zero(phydev->lp_advertising);
633c84786faSRussell King 	phydev->link = 0;
634c84786faSRussell King 	phydev->pause = 0;
635c84786faSRussell King 	phydev->asym_pause = 0;
636ea4efe25SRussell King 	phydev->mdix = ETH_TP_MDI_INVALID;
637ea4efe25SRussell King 
638c84786faSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
639c84786faSRussell King 	if (val < 0)
640c84786faSRussell King 		return val;
641c84786faSRussell King 
642c84786faSRussell King 	if (val & MDIO_STAT1_LSTATUS)
643c84786faSRussell King 		err = mv3310_read_status_10gbaser(phydev);
644c84786faSRussell King 	else
645c84786faSRussell King 		err = mv3310_read_status_copper(phydev);
646c84786faSRussell King 	if (err < 0)
647c84786faSRussell King 		return err;
648c84786faSRussell King 
649c84786faSRussell King 	if (phydev->link)
65036c4449aSRussell King 		mv3310_update_interface(phydev);
65120b2af32SRussell King 
65220b2af32SRussell King 	return 0;
65320b2af32SRussell King }
65420b2af32SRussell King 
655*a585c03eSRussell King static int mv3310_get_tunable(struct phy_device *phydev,
656*a585c03eSRussell King 			      struct ethtool_tunable *tuna, void *data)
657*a585c03eSRussell King {
658*a585c03eSRussell King 	switch (tuna->id) {
659*a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
660*a585c03eSRussell King 		return mv3310_get_edpd(phydev, data);
661*a585c03eSRussell King 	default:
662*a585c03eSRussell King 		return -EOPNOTSUPP;
663*a585c03eSRussell King 	}
664*a585c03eSRussell King }
665*a585c03eSRussell King 
666*a585c03eSRussell King static int mv3310_set_tunable(struct phy_device *phydev,
667*a585c03eSRussell King 			      struct ethtool_tunable *tuna, const void *data)
668*a585c03eSRussell King {
669*a585c03eSRussell King 	switch (tuna->id) {
670*a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
671*a585c03eSRussell King 		return mv3310_set_edpd(phydev, *(u16 *)data);
672*a585c03eSRussell King 	default:
673*a585c03eSRussell King 		return -EOPNOTSUPP;
674*a585c03eSRussell King 	}
675*a585c03eSRussell King }
676*a585c03eSRussell King 
67720b2af32SRussell King static struct phy_driver mv3310_drivers[] = {
67820b2af32SRussell King 	{
679631ba906SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88X3310,
680952b6b3bSAntoine Tenart 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
68120b2af32SRussell King 		.name		= "mv88x3310",
68274145424SMaxime Chevallier 		.get_features	= mv3310_get_features,
6837be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
68420b2af32SRussell King 		.config_init	= mv3310_config_init,
6850d3ad854SRussell King 		.probe		= mv3310_probe,
6860d3ad854SRussell King 		.suspend	= mv3310_suspend,
6870d3ad854SRussell King 		.resume		= mv3310_resume,
68820b2af32SRussell King 		.config_aneg	= mv3310_config_aneg,
68920b2af32SRussell King 		.aneg_done	= mv3310_aneg_done,
69020b2af32SRussell King 		.read_status	= mv3310_read_status,
691*a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
692*a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
69320b2af32SRussell King 	},
69462d01535SMaxime Chevallier 	{
69562d01535SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88E2110,
69662d01535SMaxime Chevallier 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
69762d01535SMaxime Chevallier 		.name		= "mv88x2110",
69862d01535SMaxime Chevallier 		.probe		= mv3310_probe,
699e02c4a9dSAntoine Tenart 		.suspend	= mv3310_suspend,
700e02c4a9dSAntoine Tenart 		.resume		= mv3310_resume,
7017be3ad84SHeiner Kallweit 		.soft_reset	= genphy_no_soft_reset,
70262d01535SMaxime Chevallier 		.config_init	= mv3310_config_init,
70362d01535SMaxime Chevallier 		.config_aneg	= mv3310_config_aneg,
70462d01535SMaxime Chevallier 		.aneg_done	= mv3310_aneg_done,
70562d01535SMaxime Chevallier 		.read_status	= mv3310_read_status,
706*a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
707*a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
70862d01535SMaxime Chevallier 	},
70920b2af32SRussell King };
71020b2af32SRussell King 
71120b2af32SRussell King module_phy_driver(mv3310_drivers);
71220b2af32SRussell King 
71320b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
714631ba906SMaxime Chevallier 	{ MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_MASK },
71562d01535SMaxime Chevallier 	{ MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK },
71620b2af32SRussell King 	{ },
71720b2af32SRussell King };
71820b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
71920b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
72020b2af32SRussell King MODULE_LICENSE("GPL");
721