xref: /linux/drivers/net/phy/marvell10g.c (revision 9885d016ffa9465f91498e7a70c413c30446ad49)
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 {
36dd649b4fSRussell King 	MV_PMA_FW_VER0		= 0xc011,
37dd649b4fSRussell King 	MV_PMA_FW_VER1		= 0xc012,
389ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL	= 0xc04a,
399ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_SWRST				= BIT(15),
409ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK			= 0x7,
419ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII			= 0x0,
429ab0fbd0SMarek Behún 	MV_PMA_2180_PORT_CTRL_MACTYPE_DXGMII			= 0x1,
439ab0fbd0SMarek Behún 	MV_PMA_2180_PORT_CTRL_MACTYPE_QXGMII			= 0x2,
449ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER			= 0x4,
459ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN	= 0x5,
469ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH	= 0x6,
473d3ced2eSRussell King 	MV_PMA_BOOT		= 0xc050,
483d3ced2eSRussell King 	MV_PMA_BOOT_FATAL	= BIT(0),
493d3ced2eSRussell King 
5020b2af32SRussell King 	MV_PCS_BASE_T		= 0x0000,
5120b2af32SRussell King 	MV_PCS_BASE_R		= 0x1000,
5220b2af32SRussell King 	MV_PCS_1000BASEX	= 0x2000,
5320b2af32SRussell King 
548d8963c3SRussell King 	MV_PCS_CSCR1		= 0x8000,
55a585c03eSRussell King 	MV_PCS_CSCR1_ED_MASK	= 0x0300,
56a585c03eSRussell King 	MV_PCS_CSCR1_ED_OFF	= 0x0000,
57a585c03eSRussell King 	MV_PCS_CSCR1_ED_RX	= 0x0200,
58a585c03eSRussell King 	MV_PCS_CSCR1_ED_NLP	= 0x0300,
598d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MASK	= 0x0060,
608d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MDI	= 0x0000,
618d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MDIX	= 0x0020,
628d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_AUTO	= 0x0060,
638d8963c3SRussell King 
64c84786faSRussell King 	MV_PCS_CSSR1		= 0x8008,
65c84786faSRussell King 	MV_PCS_CSSR1_SPD1_MASK	= 0xc000,
66c84786faSRussell King 	MV_PCS_CSSR1_SPD1_SPD2	= 0xc000,
67c84786faSRussell King 	MV_PCS_CSSR1_SPD1_1000	= 0x8000,
68c84786faSRussell King 	MV_PCS_CSSR1_SPD1_100	= 0x4000,
69c84786faSRussell King 	MV_PCS_CSSR1_SPD1_10	= 0x0000,
70c84786faSRussell King 	MV_PCS_CSSR1_DUPLEX_FULL= BIT(13),
71c84786faSRussell King 	MV_PCS_CSSR1_RESOLVED	= BIT(11),
72c84786faSRussell King 	MV_PCS_CSSR1_MDIX	= BIT(6),
73c84786faSRussell King 	MV_PCS_CSSR1_SPD2_MASK	= 0x000c,
74c84786faSRussell King 	MV_PCS_CSSR1_SPD2_5000	= 0x0008,
75c84786faSRussell King 	MV_PCS_CSSR1_SPD2_2500	= 0x0004,
76c84786faSRussell King 	MV_PCS_CSSR1_SPD2_10000	= 0x0000,
77ea4efe25SRussell King 
78c3e302edSBaruch Siach 	/* Temperature read register (88E2110 only) */
79c3e302edSBaruch Siach 	MV_PCS_TEMP		= 0x8042,
80c3e302edSBaruch Siach 
8120b2af32SRussell King 	/* These registers appear at 0x800X and 0xa00X - the 0xa00X control
8220b2af32SRussell King 	 * registers appear to set themselves to the 0x800X when AN is
8320b2af32SRussell King 	 * restarted, but status registers appear readable from either.
8420b2af32SRussell King 	 */
8520b2af32SRussell King 	MV_AN_CTRL1000		= 0x8000, /* 1000base-T control register */
8620b2af32SRussell King 	MV_AN_STAT1000		= 0x8001, /* 1000base-T status register */
870d3ad854SRussell King 
880d3ad854SRussell King 	/* Vendor2 MMD registers */
89af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL		= 0xf001,
908f48c2acSRussell King 	MV_V2_PORT_CTRL_PWRDOWN					= BIT(11),
919893f316SMarek Behún 	MV_V2_33X0_PORT_CTRL_SWRST				= BIT(15),
929893f316SMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_MASK			= 0x7,
93f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI			= 0x0,
94f8ee45fcSMarek Behún 	MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH		= 0x1,
95f8ee45fcSMarek Behún 	MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN		= 0x1,
96f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH		= 0x2,
97f8ee45fcSMarek Behún 	MV_V2_3310_PORT_CTRL_MACTYPE_XAUI			= 0x3,
98f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER			= 0x4,
99f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN	= 0x5,
100f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH	= 0x6,
101f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII			= 0x7,
102c3e302edSBaruch Siach 	/* Temperature control/read registers (88X3310 only) */
1030d3ad854SRussell King 	MV_V2_TEMP_CTRL		= 0xf08a,
1040d3ad854SRussell King 	MV_V2_TEMP_CTRL_MASK	= 0xc000,
1050d3ad854SRussell King 	MV_V2_TEMP_CTRL_SAMPLE	= 0x0000,
1060d3ad854SRussell King 	MV_V2_TEMP_CTRL_DISABLE	= 0xc000,
1070d3ad854SRussell King 	MV_V2_TEMP		= 0xf08c,
1080d3ad854SRussell King 	MV_V2_TEMP_UNKNOWN	= 0x9600, /* unknown function */
1090d3ad854SRussell King };
1100d3ad854SRussell King 
11197bbe3bdSMarek Behún struct mv3310_chip {
112261a74c6SMarek Behún 	void (*init_supported_interfaces)(unsigned long *mask);
11397bbe3bdSMarek Behún 	int (*get_mactype)(struct phy_device *phydev);
11497bbe3bdSMarek Behún 	int (*init_interface)(struct phy_device *phydev, int mactype);
115884d9a67SMarek Behún 
116884d9a67SMarek Behún #ifdef CONFIG_HWMON
117884d9a67SMarek Behún 	int (*hwmon_read_temp_reg)(struct phy_device *phydev);
118884d9a67SMarek Behún #endif
11997bbe3bdSMarek Behún };
12097bbe3bdSMarek Behún 
1210d3ad854SRussell King struct mv3310_priv {
122261a74c6SMarek Behún 	DECLARE_BITMAP(supported_interfaces, PHY_INTERFACE_MODE_MAX);
123261a74c6SMarek Behún 
124dd649b4fSRussell King 	u32 firmware_ver;
125e1170333SBaruch Siach 	bool rate_match;
12697bbe3bdSMarek Behún 	phy_interface_t const_interface;
127dd649b4fSRussell King 
1280d3ad854SRussell King 	struct device *hwmon_dev;
1290d3ad854SRussell King 	char *hwmon_name;
13020b2af32SRussell King };
13120b2af32SRussell King 
13297bbe3bdSMarek Behún static const struct mv3310_chip *to_mv3310_chip(struct phy_device *phydev)
13397bbe3bdSMarek Behún {
13497bbe3bdSMarek Behún 	return phydev->drv->driver_data;
13597bbe3bdSMarek Behún }
13697bbe3bdSMarek Behún 
1370d3ad854SRussell King #ifdef CONFIG_HWMON
1380d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data,
1390d3ad854SRussell King 				       enum hwmon_sensor_types type,
1400d3ad854SRussell King 				       u32 attr, int channel)
1410d3ad854SRussell King {
1420d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
1430d3ad854SRussell King 		return 0444;
1440d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input)
1450d3ad854SRussell King 		return 0444;
1460d3ad854SRussell King 	return 0;
1470d3ad854SRussell King }
1480d3ad854SRussell King 
149c3e302edSBaruch Siach static int mv3310_hwmon_read_temp_reg(struct phy_device *phydev)
150c3e302edSBaruch Siach {
151c3e302edSBaruch Siach 	return phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP);
152c3e302edSBaruch Siach }
153c3e302edSBaruch Siach 
154c3e302edSBaruch Siach static int mv2110_hwmon_read_temp_reg(struct phy_device *phydev)
155c3e302edSBaruch Siach {
156c3e302edSBaruch Siach 	return phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_TEMP);
157c3e302edSBaruch Siach }
158c3e302edSBaruch Siach 
1590d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1600d3ad854SRussell King 			     u32 attr, int channel, long *value)
1610d3ad854SRussell King {
1620d3ad854SRussell King 	struct phy_device *phydev = dev_get_drvdata(dev);
163884d9a67SMarek Behún 	const struct mv3310_chip *chip = to_mv3310_chip(phydev);
1640d3ad854SRussell King 	int temp;
1650d3ad854SRussell King 
1660d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
1670d3ad854SRussell King 		*value = MSEC_PER_SEC;
1680d3ad854SRussell King 		return 0;
1690d3ad854SRussell King 	}
1700d3ad854SRussell King 
1710d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input) {
172884d9a67SMarek Behún 		temp = chip->hwmon_read_temp_reg(phydev);
1730d3ad854SRussell King 		if (temp < 0)
1740d3ad854SRussell King 			return temp;
1750d3ad854SRussell King 
1760d3ad854SRussell King 		*value = ((temp & 0xff) - 75) * 1000;
1770d3ad854SRussell King 
1780d3ad854SRussell King 		return 0;
1790d3ad854SRussell King 	}
1800d3ad854SRussell King 
1810d3ad854SRussell King 	return -EOPNOTSUPP;
1820d3ad854SRussell King }
1830d3ad854SRussell King 
1840d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = {
1850d3ad854SRussell King 	.is_visible = mv3310_hwmon_is_visible,
1860d3ad854SRussell King 	.read = mv3310_hwmon_read,
1870d3ad854SRussell King };
1880d3ad854SRussell King 
1890d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = {
1900d3ad854SRussell King 	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
1910d3ad854SRussell King 	0,
1920d3ad854SRussell King };
1930d3ad854SRussell King 
1940d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = {
1950d3ad854SRussell King 	.type = hwmon_chip,
1960d3ad854SRussell King 	.config = mv3310_hwmon_chip_config,
1970d3ad854SRussell King };
1980d3ad854SRussell King 
1990d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = {
2000d3ad854SRussell King 	HWMON_T_INPUT,
2010d3ad854SRussell King 	0,
2020d3ad854SRussell King };
2030d3ad854SRussell King 
2040d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = {
2050d3ad854SRussell King 	.type = hwmon_temp,
2060d3ad854SRussell King 	.config = mv3310_hwmon_temp_config,
2070d3ad854SRussell King };
2080d3ad854SRussell King 
2090d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = {
2100d3ad854SRussell King 	&mv3310_hwmon_chip,
2110d3ad854SRussell King 	&mv3310_hwmon_temp,
2120d3ad854SRussell King 	NULL,
2130d3ad854SRussell King };
2140d3ad854SRussell King 
2150d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = {
2160d3ad854SRussell King 	.ops = &mv3310_hwmon_ops,
2170d3ad854SRussell King 	.info = mv3310_hwmon_info,
2180d3ad854SRussell King };
2190d3ad854SRussell King 
2200d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
2210d3ad854SRussell King {
2220d3ad854SRussell King 	u16 val;
2230d3ad854SRussell King 	int ret;
2240d3ad854SRussell King 
225c3e302edSBaruch Siach 	if (phydev->drv->phy_id != MARVELL_PHY_ID_88X3310)
226c3e302edSBaruch Siach 		return 0;
227c3e302edSBaruch Siach 
2280d3ad854SRussell King 	ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP,
2290d3ad854SRussell King 			    MV_V2_TEMP_UNKNOWN);
2300d3ad854SRussell King 	if (ret < 0)
2310d3ad854SRussell King 		return ret;
2320d3ad854SRussell King 
2330d3ad854SRussell King 	val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE;
2340d3ad854SRussell King 
235b06d8e5aSHeiner Kallweit 	return phy_modify_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL,
236b06d8e5aSHeiner Kallweit 			      MV_V2_TEMP_CTRL_MASK, val);
2370d3ad854SRussell King }
2380d3ad854SRussell King 
2390d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
2400d3ad854SRussell King {
2410d3ad854SRussell King 	struct device *dev = &phydev->mdio.dev;
2420d3ad854SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
2430d3ad854SRussell King 	int i, j, ret;
2440d3ad854SRussell King 
2450d3ad854SRussell King 	priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
2460d3ad854SRussell King 	if (!priv->hwmon_name)
2470d3ad854SRussell King 		return -ENODEV;
2480d3ad854SRussell King 
2490d3ad854SRussell King 	for (i = j = 0; priv->hwmon_name[i]; i++) {
2500d3ad854SRussell King 		if (isalnum(priv->hwmon_name[i])) {
2510d3ad854SRussell King 			if (i != j)
2520d3ad854SRussell King 				priv->hwmon_name[j] = priv->hwmon_name[i];
2530d3ad854SRussell King 			j++;
2540d3ad854SRussell King 		}
2550d3ad854SRussell King 	}
2560d3ad854SRussell King 	priv->hwmon_name[j] = '\0';
2570d3ad854SRussell King 
2580d3ad854SRussell King 	ret = mv3310_hwmon_config(phydev, true);
2590d3ad854SRussell King 	if (ret)
2600d3ad854SRussell King 		return ret;
2610d3ad854SRussell King 
2620d3ad854SRussell King 	priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
2630d3ad854SRussell King 				priv->hwmon_name, phydev,
2640d3ad854SRussell King 				&mv3310_hwmon_chip_info, NULL);
2650d3ad854SRussell King 
2660d3ad854SRussell King 	return PTR_ERR_OR_ZERO(priv->hwmon_dev);
2670d3ad854SRussell King }
2680d3ad854SRussell King #else
2690d3ad854SRussell King static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
2700d3ad854SRussell King {
2710d3ad854SRussell King 	return 0;
2720d3ad854SRussell King }
2730d3ad854SRussell King 
2740d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
2750d3ad854SRussell King {
2760d3ad854SRussell King 	return 0;
2770d3ad854SRussell King }
2780d3ad854SRussell King #endif
2790d3ad854SRussell King 
280c9cc1c81SRussell King static int mv3310_power_down(struct phy_device *phydev)
281c9cc1c81SRussell King {
282c9cc1c81SRussell King 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
283c9cc1c81SRussell King 				MV_V2_PORT_CTRL_PWRDOWN);
284c9cc1c81SRussell King }
285c9cc1c81SRussell King 
286c9cc1c81SRussell King static int mv3310_power_up(struct phy_device *phydev)
287c9cc1c81SRussell King {
2888f48c2acSRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
2898f48c2acSRussell King 	int ret;
2908f48c2acSRussell King 
2918f48c2acSRussell King 	ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
292c9cc1c81SRussell King 				 MV_V2_PORT_CTRL_PWRDOWN);
2938f48c2acSRussell King 
294829e7573SBaruch Siach 	if (phydev->drv->phy_id != MARVELL_PHY_ID_88X3310 ||
295829e7573SBaruch Siach 	    priv->firmware_ver < 0x00030000)
2968f48c2acSRussell King 		return ret;
2978f48c2acSRussell King 
2988f48c2acSRussell King 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
2999893f316SMarek Behún 				MV_V2_33X0_PORT_CTRL_SWRST);
300c9cc1c81SRussell King }
301c9cc1c81SRussell King 
3028d8963c3SRussell King static int mv3310_reset(struct phy_device *phydev, u32 unit)
3038d8963c3SRussell King {
3048964a217SDejin Zheng 	int val, err;
3058d8963c3SRussell King 
3068d8963c3SRussell King 	err = phy_modify_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1,
3078d8963c3SRussell King 			     MDIO_CTRL1_RESET, MDIO_CTRL1_RESET);
3088d8963c3SRussell King 	if (err < 0)
3098d8963c3SRussell King 		return err;
3108d8963c3SRussell King 
3118964a217SDejin Zheng 	return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_PCS,
3128964a217SDejin Zheng 					 unit + MDIO_CTRL1, val,
3138964a217SDejin Zheng 					 !(val & MDIO_CTRL1_RESET),
3148964a217SDejin Zheng 					 5000, 100000, true);
3158d8963c3SRussell King }
3168d8963c3SRussell King 
317a585c03eSRussell King static int mv3310_get_edpd(struct phy_device *phydev, u16 *edpd)
318a585c03eSRussell King {
319a585c03eSRussell King 	int val;
320a585c03eSRussell King 
321a585c03eSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1);
322a585c03eSRussell King 	if (val < 0)
323a585c03eSRussell King 		return val;
324a585c03eSRussell King 
325a585c03eSRussell King 	switch (val & MV_PCS_CSCR1_ED_MASK) {
326a585c03eSRussell King 	case MV_PCS_CSCR1_ED_NLP:
327a585c03eSRussell King 		*edpd = 1000;
328a585c03eSRussell King 		break;
329a585c03eSRussell King 	case MV_PCS_CSCR1_ED_RX:
330a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_NO_TX;
331a585c03eSRussell King 		break;
332a585c03eSRussell King 	default:
333a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_DISABLE;
334a585c03eSRussell King 		break;
335a585c03eSRussell King 	}
336a585c03eSRussell King 	return 0;
337a585c03eSRussell King }
338a585c03eSRussell King 
339a585c03eSRussell King static int mv3310_set_edpd(struct phy_device *phydev, u16 edpd)
340a585c03eSRussell King {
341a585c03eSRussell King 	u16 val;
342a585c03eSRussell King 	int err;
343a585c03eSRussell King 
344a585c03eSRussell King 	switch (edpd) {
345a585c03eSRussell King 	case 1000:
346a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS:
347a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_NLP;
348a585c03eSRussell King 		break;
349a585c03eSRussell King 
350a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_NO_TX:
351a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_RX;
352a585c03eSRussell King 		break;
353a585c03eSRussell King 
354a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DISABLE:
355a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_OFF;
356a585c03eSRussell King 		break;
357a585c03eSRussell King 
358a585c03eSRussell King 	default:
359a585c03eSRussell King 		return -EINVAL;
360a585c03eSRussell King 	}
361a585c03eSRussell King 
362a585c03eSRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
363a585c03eSRussell King 				     MV_PCS_CSCR1_ED_MASK, val);
364a585c03eSRussell King 	if (err > 0)
365a585c03eSRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
366a585c03eSRussell King 
367a585c03eSRussell King 	return err;
368a585c03eSRussell King }
369a585c03eSRussell King 
37036023da1SRussell King static int mv3310_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
37136023da1SRussell King {
37236023da1SRussell King 	struct phy_device *phydev = upstream;
37336023da1SRussell King 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
37436023da1SRussell King 	phy_interface_t iface;
37536023da1SRussell King 
37636023da1SRussell King 	sfp_parse_support(phydev->sfp_bus, id, support);
377a4516c70SRussell King 	iface = sfp_select_interface(phydev->sfp_bus, support);
37836023da1SRussell King 
379e0f909bcSRussell King 	if (iface != PHY_INTERFACE_MODE_10GBASER) {
38036023da1SRussell King 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
38136023da1SRussell King 		return -EINVAL;
38236023da1SRussell King 	}
38336023da1SRussell King 	return 0;
38436023da1SRussell King }
38536023da1SRussell King 
38636023da1SRussell King static const struct sfp_upstream_ops mv3310_sfp_ops = {
38736023da1SRussell King 	.attach = phy_sfp_attach,
38836023da1SRussell King 	.detach = phy_sfp_detach,
38936023da1SRussell King 	.module_insert = mv3310_sfp_insert,
39036023da1SRussell King };
39136023da1SRussell King 
39220b2af32SRussell King static int mv3310_probe(struct phy_device *phydev)
39320b2af32SRussell King {
394261a74c6SMarek Behún 	const struct mv3310_chip *chip = to_mv3310_chip(phydev);
3950d3ad854SRussell King 	struct mv3310_priv *priv;
39620b2af32SRussell King 	u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
3970d3ad854SRussell King 	int ret;
39820b2af32SRussell King 
39920b2af32SRussell King 	if (!phydev->is_c45 ||
40020b2af32SRussell King 	    (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
40120b2af32SRussell King 		return -ENODEV;
40220b2af32SRussell King 
4033d3ced2eSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT);
4043d3ced2eSRussell King 	if (ret < 0)
4053d3ced2eSRussell King 		return ret;
4063d3ced2eSRussell King 
4073d3ced2eSRussell King 	if (ret & MV_PMA_BOOT_FATAL) {
4083d3ced2eSRussell King 		dev_warn(&phydev->mdio.dev,
4093d3ced2eSRussell King 			 "PHY failed to boot firmware, status=%04x\n", ret);
4103d3ced2eSRussell King 		return -ENODEV;
4113d3ced2eSRussell King 	}
4123d3ced2eSRussell King 
4130d3ad854SRussell King 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
4140d3ad854SRussell King 	if (!priv)
4150d3ad854SRussell King 		return -ENOMEM;
4160d3ad854SRussell King 
4170d3ad854SRussell King 	dev_set_drvdata(&phydev->mdio.dev, priv);
4180d3ad854SRussell King 
419dd649b4fSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER0);
420dd649b4fSRussell King 	if (ret < 0)
421dd649b4fSRussell King 		return ret;
422dd649b4fSRussell King 
423dd649b4fSRussell King 	priv->firmware_ver = ret << 16;
424dd649b4fSRussell King 
425dd649b4fSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER1);
426dd649b4fSRussell King 	if (ret < 0)
427dd649b4fSRussell King 		return ret;
428dd649b4fSRussell King 
429dd649b4fSRussell King 	priv->firmware_ver |= ret;
430dd649b4fSRussell King 
431dd649b4fSRussell King 	phydev_info(phydev, "Firmware version %u.%u.%u.%u\n",
432dd649b4fSRussell King 		    priv->firmware_ver >> 24, (priv->firmware_ver >> 16) & 255,
433dd649b4fSRussell King 		    (priv->firmware_ver >> 8) & 255, priv->firmware_ver & 255);
434dd649b4fSRussell King 
435c9cc1c81SRussell King 	/* Powering down the port when not in use saves about 600mW */
436c9cc1c81SRussell King 	ret = mv3310_power_down(phydev);
437c9cc1c81SRussell King 	if (ret)
438c9cc1c81SRussell King 		return ret;
439c9cc1c81SRussell King 
4400d3ad854SRussell King 	ret = mv3310_hwmon_probe(phydev);
4410d3ad854SRussell King 	if (ret)
4420d3ad854SRussell King 		return ret;
4430d3ad854SRussell King 
444261a74c6SMarek Behún 	chip->init_supported_interfaces(priv->supported_interfaces);
445261a74c6SMarek Behún 
44636023da1SRussell King 	return phy_sfp_probe(phydev, &mv3310_sfp_ops);
44720b2af32SRussell King }
44820b2af32SRussell King 
4491b8ef142SMarek Behún static void mv3310_remove(struct phy_device *phydev)
4501b8ef142SMarek Behún {
4511b8ef142SMarek Behún 	mv3310_hwmon_config(phydev, false);
4521b8ef142SMarek Behún }
4531b8ef142SMarek Behún 
4540d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev)
4550d3ad854SRussell King {
456c9cc1c81SRussell King 	return mv3310_power_down(phydev);
4570d3ad854SRussell King }
4580d3ad854SRussell King 
4590d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev)
4600d3ad854SRussell King {
461af3e28cbSAntoine Tenart 	int ret;
462af3e28cbSAntoine Tenart 
463c9cc1c81SRussell King 	ret = mv3310_power_up(phydev);
464af3e28cbSAntoine Tenart 	if (ret)
465af3e28cbSAntoine Tenart 		return ret;
466af3e28cbSAntoine Tenart 
4670d3ad854SRussell King 	return mv3310_hwmon_config(phydev, true);
4680d3ad854SRussell King }
4690d3ad854SRussell King 
470c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010
471c47455f9SMaxime Chevallier  * don't set bit 14 in PMA Extended Abilities (1.11), although they do
472c47455f9SMaxime Chevallier  * support 2.5GBASET and 5GBASET. For these models, we can still read their
473c47455f9SMaxime Chevallier  * 2.5G/5G extended abilities register (1.21). We detect these models based on
474c47455f9SMaxime Chevallier  * the PMA device identifier, with a mask matching models known to have this
475c47455f9SMaxime Chevallier  * issue
476c47455f9SMaxime Chevallier  */
477c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev)
478c47455f9SMaxime Chevallier {
479c47455f9SMaxime Chevallier 	if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD))
480c47455f9SMaxime Chevallier 		return false;
481c47455f9SMaxime Chevallier 
482c47455f9SMaxime Chevallier 	/* Only some revisions of the 88X3310 family PMA seem to be impacted */
483c47455f9SMaxime Chevallier 	return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
484c47455f9SMaxime Chevallier 		MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
485c47455f9SMaxime Chevallier }
486c47455f9SMaxime Chevallier 
48797bbe3bdSMarek Behún static int mv2110_get_mactype(struct phy_device *phydev)
48897bbe3bdSMarek Behún {
48997bbe3bdSMarek Behún 	int mactype;
49097bbe3bdSMarek Behún 
49197bbe3bdSMarek Behún 	mactype = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_21X0_PORT_CTRL);
49297bbe3bdSMarek Behún 	if (mactype < 0)
49397bbe3bdSMarek Behún 		return mactype;
49497bbe3bdSMarek Behún 
49597bbe3bdSMarek Behún 	return mactype & MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK;
49697bbe3bdSMarek Behún }
49797bbe3bdSMarek Behún 
49897bbe3bdSMarek Behún static int mv3310_get_mactype(struct phy_device *phydev)
49997bbe3bdSMarek Behún {
50097bbe3bdSMarek Behún 	int mactype;
50197bbe3bdSMarek Behún 
50297bbe3bdSMarek Behún 	mactype = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL);
50397bbe3bdSMarek Behún 	if (mactype < 0)
50497bbe3bdSMarek Behún 		return mactype;
50597bbe3bdSMarek Behún 
50697bbe3bdSMarek Behún 	return mactype & MV_V2_33X0_PORT_CTRL_MACTYPE_MASK;
50797bbe3bdSMarek Behún }
50897bbe3bdSMarek Behún 
50997bbe3bdSMarek Behún static int mv2110_init_interface(struct phy_device *phydev, int mactype)
51020b2af32SRussell King {
511e1170333SBaruch Siach 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
51297bbe3bdSMarek Behún 
51397bbe3bdSMarek Behún 	priv->rate_match = false;
51497bbe3bdSMarek Behún 
515ccbf2891SMarek Behún 	if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
51697bbe3bdSMarek Behún 		priv->rate_match = true;
517ccbf2891SMarek Behún 
518ccbf2891SMarek Behún 	if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII)
519ccbf2891SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
520ccbf2891SMarek Behún 	else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
52197bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_10GBASER;
522ccbf2891SMarek Behún 	else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER ||
523ccbf2891SMarek Behún 		 mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN)
524ccbf2891SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_NA;
525ccbf2891SMarek Behún 	else
526ccbf2891SMarek Behún 		return -EINVAL;
52797bbe3bdSMarek Behún 
52897bbe3bdSMarek Behún 	return 0;
52997bbe3bdSMarek Behún }
53097bbe3bdSMarek Behún 
53197bbe3bdSMarek Behún static int mv3310_init_interface(struct phy_device *phydev, int mactype)
53297bbe3bdSMarek Behún {
53397bbe3bdSMarek Behún 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
53497bbe3bdSMarek Behún 
53597bbe3bdSMarek Behún 	priv->rate_match = false;
53697bbe3bdSMarek Behún 
53797bbe3bdSMarek Behún 	if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
53897bbe3bdSMarek Behún 	    mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
53997bbe3bdSMarek Behún 	    mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH)
54097bbe3bdSMarek Behún 		priv->rate_match = true;
54197bbe3bdSMarek Behún 
542ccbf2891SMarek Behún 	if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII)
543ccbf2891SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
544ccbf2891SMarek Behún 	else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
545ccbf2891SMarek Behún 		 mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN ||
546ccbf2891SMarek Behún 		 mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER)
54797bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_10GBASER;
548ccbf2891SMarek Behún 	else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
549ccbf2891SMarek Behún 		 mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI)
55097bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_RXAUI;
551ccbf2891SMarek Behún 	else if (mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH ||
552ccbf2891SMarek Behún 		 mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI)
55397bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_XAUI;
554ccbf2891SMarek Behún 	else
555ccbf2891SMarek Behún 		return -EINVAL;
55697bbe3bdSMarek Behún 
55797bbe3bdSMarek Behún 	return 0;
55897bbe3bdSMarek Behún }
55997bbe3bdSMarek Behún 
560*9885d016SMarek Behún static int mv3340_init_interface(struct phy_device *phydev, int mactype)
561*9885d016SMarek Behún {
562*9885d016SMarek Behún 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
563*9885d016SMarek Behún 	int err = 0;
564*9885d016SMarek Behún 
565*9885d016SMarek Behún 	priv->rate_match = false;
566*9885d016SMarek Behún 
567*9885d016SMarek Behún 	if (mactype == MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN)
568*9885d016SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_RXAUI;
569*9885d016SMarek Behún 	else
570*9885d016SMarek Behún 		err = mv3310_init_interface(phydev, mactype);
571*9885d016SMarek Behún 
572*9885d016SMarek Behún 	return err;
573*9885d016SMarek Behún }
574*9885d016SMarek Behún 
57597bbe3bdSMarek Behún static int mv3310_config_init(struct phy_device *phydev)
57697bbe3bdSMarek Behún {
577261a74c6SMarek Behún 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
57897bbe3bdSMarek Behún 	const struct mv3310_chip *chip = to_mv3310_chip(phydev);
57997bbe3bdSMarek Behún 	int err, mactype;
580c9cc1c81SRussell King 
58120b2af32SRussell King 	/* Check that the PHY interface type is compatible */
582261a74c6SMarek Behún 	if (!test_bit(phydev->interface, priv->supported_interfaces))
58320b2af32SRussell King 		return -ENODEV;
58420b2af32SRussell King 
5858d8963c3SRussell King 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
5868d8963c3SRussell King 
587c9cc1c81SRussell King 	/* Power up so reset works */
588c9cc1c81SRussell King 	err = mv3310_power_up(phydev);
589c9cc1c81SRussell King 	if (err)
590c9cc1c81SRussell King 		return err;
591c9cc1c81SRussell King 
59297bbe3bdSMarek Behún 	mactype = chip->get_mactype(phydev);
59397bbe3bdSMarek Behún 	if (mactype < 0)
59497bbe3bdSMarek Behún 		return mactype;
59597bbe3bdSMarek Behún 
59697bbe3bdSMarek Behún 	err = chip->init_interface(phydev, mactype);
597ccbf2891SMarek Behún 	if (err) {
598ccbf2891SMarek Behún 		phydev_err(phydev, "MACTYPE configuration invalid\n");
59997bbe3bdSMarek Behún 		return err;
600ccbf2891SMarek Behún 	}
601e1170333SBaruch Siach 
602a585c03eSRussell King 	/* Enable EDPD mode - saving 600mW */
603a585c03eSRussell King 	return mv3310_set_edpd(phydev, ETHTOOL_PHY_EDPD_DFLT_TX_MSECS);
60474145424SMaxime Chevallier }
60574145424SMaxime Chevallier 
60674145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev)
60774145424SMaxime Chevallier {
60874145424SMaxime Chevallier 	int ret, val;
60974145424SMaxime Chevallier 
610ac3f5533SMaxime Chevallier 	ret = genphy_c45_pma_read_abilities(phydev);
611ac3f5533SMaxime Chevallier 	if (ret)
612ac3f5533SMaxime Chevallier 		return ret;
61320b2af32SRussell King 
614c47455f9SMaxime Chevallier 	if (mv3310_has_pma_ngbaset_quirk(phydev)) {
615c47455f9SMaxime Chevallier 		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
616c47455f9SMaxime Chevallier 				   MDIO_PMA_NG_EXTABLE);
617c47455f9SMaxime Chevallier 		if (val < 0)
618c47455f9SMaxime Chevallier 			return val;
619c47455f9SMaxime Chevallier 
620c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
621c47455f9SMaxime Chevallier 				 phydev->supported,
622c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_2_5GBT);
623c47455f9SMaxime Chevallier 
624c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
625c47455f9SMaxime Chevallier 				 phydev->supported,
626c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_5GBT);
627c47455f9SMaxime Chevallier 	}
628c47455f9SMaxime Chevallier 
62920b2af32SRussell King 	return 0;
63020b2af32SRussell King }
63120b2af32SRussell King 
6328d8963c3SRussell King static int mv3310_config_mdix(struct phy_device *phydev)
6338d8963c3SRussell King {
6348d8963c3SRussell King 	u16 val;
6358d8963c3SRussell King 	int err;
6368d8963c3SRussell King 
6378d8963c3SRussell King 	switch (phydev->mdix_ctrl) {
6388d8963c3SRussell King 	case ETH_TP_MDI_AUTO:
6398d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_AUTO;
6408d8963c3SRussell King 		break;
6418d8963c3SRussell King 	case ETH_TP_MDI_X:
6428d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDIX;
6438d8963c3SRussell King 		break;
6448d8963c3SRussell King 	case ETH_TP_MDI:
6458d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDI;
6468d8963c3SRussell King 		break;
6478d8963c3SRussell King 	default:
6488d8963c3SRussell King 		return -EINVAL;
6498d8963c3SRussell King 	}
6508d8963c3SRussell King 
6518d8963c3SRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
6528d8963c3SRussell King 				     MV_PCS_CSCR1_MDIX_MASK, val);
6538d8963c3SRussell King 	if (err > 0)
6548d8963c3SRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
6558d8963c3SRussell King 
6568d8963c3SRussell King 	return err;
6578d8963c3SRussell King }
6588d8963c3SRussell King 
65920b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev)
66020b2af32SRussell King {
66120b2af32SRussell King 	bool changed = false;
6623c1bcc86SAndrew Lunn 	u16 reg;
66320b2af32SRussell King 	int ret;
66420b2af32SRussell King 
6658d8963c3SRussell King 	ret = mv3310_config_mdix(phydev);
6668d8963c3SRussell King 	if (ret < 0)
6678d8963c3SRussell King 		return ret;
668ea4efe25SRussell King 
66930de65c3SHeiner Kallweit 	if (phydev->autoneg == AUTONEG_DISABLE)
67030de65c3SHeiner Kallweit 		return genphy_c45_pma_setup_forced(phydev);
67120b2af32SRussell King 
6723de97f3cSAndrew Lunn 	ret = genphy_c45_an_config_aneg(phydev);
67320b2af32SRussell King 	if (ret < 0)
67420b2af32SRussell King 		return ret;
67520b2af32SRussell King 	if (ret > 0)
67620b2af32SRussell King 		changed = true;
67720b2af32SRussell King 
6783de97f3cSAndrew Lunn 	/* Clause 45 has no standardized support for 1000BaseT, therefore
6793de97f3cSAndrew Lunn 	 * use vendor registers for this mode.
6803de97f3cSAndrew Lunn 	 */
6813c1bcc86SAndrew Lunn 	reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
682b06d8e5aSHeiner Kallweit 	ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
6833c1bcc86SAndrew Lunn 			     ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg);
68420b2af32SRussell King 	if (ret < 0)
68520b2af32SRussell King 		return ret;
68620b2af32SRussell King 	if (ret > 0)
68720b2af32SRussell King 		changed = true;
68820b2af32SRussell King 
6896b4cb6cbSHeiner Kallweit 	return genphy_c45_check_and_restart_aneg(phydev, changed);
69020b2af32SRussell King }
69120b2af32SRussell King 
69220b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev)
69320b2af32SRussell King {
69420b2af32SRussell King 	int val;
69520b2af32SRussell King 
69620b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
69720b2af32SRussell King 	if (val < 0)
69820b2af32SRussell King 		return val;
69920b2af32SRussell King 
70020b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
70120b2af32SRussell King 		return 1;
70220b2af32SRussell King 
70320b2af32SRussell King 	return genphy_c45_aneg_done(phydev);
70420b2af32SRussell King }
70520b2af32SRussell King 
70636c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev)
70736c4449aSRussell King {
708e1170333SBaruch Siach 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
709e1170333SBaruch Siach 
710ccbf2891SMarek Behún 	if (!phydev->link)
711ccbf2891SMarek Behún 		return;
712ccbf2891SMarek Behún 
71397bbe3bdSMarek Behún 	/* In all of the "* with Rate Matching" modes the PHY interface is fixed
71497bbe3bdSMarek Behún 	 * at 10Gb. The PHY adapts the rate to actual wire speed with help of
715e1170333SBaruch Siach 	 * internal 16KB buffer.
716ccbf2891SMarek Behún 	 *
717ccbf2891SMarek Behún 	 * In USXGMII mode the PHY interface mode is also fixed.
718e1170333SBaruch Siach 	 */
719ccbf2891SMarek Behún 	if (priv->rate_match ||
720ccbf2891SMarek Behún 	    priv->const_interface == PHY_INTERFACE_MODE_USXGMII) {
72197bbe3bdSMarek Behún 		phydev->interface = priv->const_interface;
722e1170333SBaruch Siach 		return;
723e1170333SBaruch Siach 	}
724e1170333SBaruch Siach 
725ccbf2891SMarek Behún 	/* The PHY automatically switches its serdes interface (and active PHYXS
726ccbf2891SMarek Behún 	 * instance) between Cisco SGMII, 2500BaseX, 5GBase-R and 10GBase-R /
727ccbf2891SMarek Behún 	 * xaui / rxaui modes according to the speed.
728ccbf2891SMarek Behún 	 * Florian suggests setting phydev->interface to communicate this to the
729ccbf2891SMarek Behún 	 * MAC. Only do this if we are already in one of the above modes.
73036c4449aSRussell King 	 */
731e555e5b1SMaxime Chevallier 	switch (phydev->speed) {
732e555e5b1SMaxime Chevallier 	case SPEED_10000:
733ccbf2891SMarek Behún 		phydev->interface = priv->const_interface;
734e555e5b1SMaxime Chevallier 		break;
7350d375542SMarek Behún 	case SPEED_5000:
7360d375542SMarek Behún 		phydev->interface = PHY_INTERFACE_MODE_5GBASER;
7370d375542SMarek Behún 		break;
738e555e5b1SMaxime Chevallier 	case SPEED_2500:
739e555e5b1SMaxime Chevallier 		phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
740e555e5b1SMaxime Chevallier 		break;
741e555e5b1SMaxime Chevallier 	case SPEED_1000:
742e555e5b1SMaxime Chevallier 	case SPEED_100:
743e555e5b1SMaxime Chevallier 	case SPEED_10:
74436c4449aSRussell King 		phydev->interface = PHY_INTERFACE_MODE_SGMII;
745e555e5b1SMaxime Chevallier 		break;
746e555e5b1SMaxime Chevallier 	default:
747e555e5b1SMaxime Chevallier 		break;
748e555e5b1SMaxime Chevallier 	}
74936c4449aSRussell King }
75036c4449aSRussell King 
75120b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
752c84786faSRussell King static int mv3310_read_status_10gbaser(struct phy_device *phydev)
75320b2af32SRussell King {
75420b2af32SRussell King 	phydev->link = 1;
75520b2af32SRussell King 	phydev->speed = SPEED_10000;
75620b2af32SRussell King 	phydev->duplex = DUPLEX_FULL;
7574217a64eSMichael Walle 	phydev->port = PORT_FIBRE;
75820b2af32SRussell King 
75920b2af32SRussell King 	return 0;
76020b2af32SRussell King }
76120b2af32SRussell King 
762c84786faSRussell King static int mv3310_read_status_copper(struct phy_device *phydev)
76320b2af32SRussell King {
764c84786faSRussell King 	int cssr1, speed, val;
76520b2af32SRussell King 
766998a8a83SHeiner Kallweit 	val = genphy_c45_read_link(phydev);
76720b2af32SRussell King 	if (val < 0)
76820b2af32SRussell King 		return val;
76920b2af32SRussell King 
77020b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
77120b2af32SRussell King 	if (val < 0)
77220b2af32SRussell King 		return val;
77320b2af32SRussell King 
774c84786faSRussell King 	cssr1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSSR1);
775c84786faSRussell King 	if (cssr1 < 0)
776c84786faSRussell King 		return val;
777c84786faSRussell King 
778c84786faSRussell King 	/* If the link settings are not resolved, mark the link down */
779c84786faSRussell King 	if (!(cssr1 & MV_PCS_CSSR1_RESOLVED)) {
780c84786faSRussell King 		phydev->link = 0;
781c84786faSRussell King 		return 0;
782c84786faSRussell King 	}
783c84786faSRussell King 
784c84786faSRussell King 	/* Read the copper link settings */
785c84786faSRussell King 	speed = cssr1 & MV_PCS_CSSR1_SPD1_MASK;
786c84786faSRussell King 	if (speed == MV_PCS_CSSR1_SPD1_SPD2)
787c84786faSRussell King 		speed |= cssr1 & MV_PCS_CSSR1_SPD2_MASK;
788c84786faSRussell King 
789c84786faSRussell King 	switch (speed) {
790c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_10000:
791c84786faSRussell King 		phydev->speed = SPEED_10000;
792c84786faSRussell King 		break;
793c84786faSRussell King 
794c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_5000:
795c84786faSRussell King 		phydev->speed = SPEED_5000;
796c84786faSRussell King 		break;
797c84786faSRussell King 
798c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_2500:
799c84786faSRussell King 		phydev->speed = SPEED_2500;
800c84786faSRussell King 		break;
801c84786faSRussell King 
802c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_1000:
803c84786faSRussell King 		phydev->speed = SPEED_1000;
804c84786faSRussell King 		break;
805c84786faSRussell King 
806c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_100:
807c84786faSRussell King 		phydev->speed = SPEED_100;
808c84786faSRussell King 		break;
809c84786faSRussell King 
810c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_10:
811c84786faSRussell King 		phydev->speed = SPEED_10;
812c84786faSRussell King 		break;
813c84786faSRussell King 	}
814c84786faSRussell King 
815c84786faSRussell King 	phydev->duplex = cssr1 & MV_PCS_CSSR1_DUPLEX_FULL ?
816c84786faSRussell King 			 DUPLEX_FULL : DUPLEX_HALF;
8174217a64eSMichael Walle 	phydev->port = PORT_TP;
818c84786faSRussell King 	phydev->mdix = cssr1 & MV_PCS_CSSR1_MDIX ?
819c84786faSRussell King 		       ETH_TP_MDI_X : ETH_TP_MDI;
820c84786faSRussell King 
82120b2af32SRussell King 	if (val & MDIO_AN_STAT1_COMPLETE) {
82220b2af32SRussell King 		val = genphy_c45_read_lpa(phydev);
82320b2af32SRussell King 		if (val < 0)
82420b2af32SRussell King 			return val;
82520b2af32SRussell King 
826cc1122b0SColin Ian King 		/* Read the link partner's 1G advertisement */
82720b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
82820b2af32SRussell King 		if (val < 0)
82920b2af32SRussell King 			return val;
83020b2af32SRussell King 
83178a24df3SAndrew Lunn 		mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
83220b2af32SRussell King 
833c84786faSRussell King 		/* Update the pause status */
834c84786faSRussell King 		phy_resolve_aneg_pause(phydev);
83520b2af32SRussell King 	}
83620b2af32SRussell King 
837c84786faSRussell King 	return 0;
83820b2af32SRussell King }
83920b2af32SRussell King 
840c84786faSRussell King static int mv3310_read_status(struct phy_device *phydev)
841c84786faSRussell King {
842c84786faSRussell King 	int err, val;
843ea4efe25SRussell King 
844c84786faSRussell King 	phydev->speed = SPEED_UNKNOWN;
845c84786faSRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
846c84786faSRussell King 	linkmode_zero(phydev->lp_advertising);
847c84786faSRussell King 	phydev->link = 0;
848c84786faSRussell King 	phydev->pause = 0;
849c84786faSRussell King 	phydev->asym_pause = 0;
850ea4efe25SRussell King 	phydev->mdix = ETH_TP_MDI_INVALID;
851ea4efe25SRussell King 
852c84786faSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
853c84786faSRussell King 	if (val < 0)
854c84786faSRussell King 		return val;
855c84786faSRussell King 
856c84786faSRussell King 	if (val & MDIO_STAT1_LSTATUS)
857c84786faSRussell King 		err = mv3310_read_status_10gbaser(phydev);
858c84786faSRussell King 	else
859c84786faSRussell King 		err = mv3310_read_status_copper(phydev);
860c84786faSRussell King 	if (err < 0)
861c84786faSRussell King 		return err;
862c84786faSRussell King 
863c84786faSRussell King 	if (phydev->link)
86436c4449aSRussell King 		mv3310_update_interface(phydev);
86520b2af32SRussell King 
86620b2af32SRussell King 	return 0;
86720b2af32SRussell King }
86820b2af32SRussell King 
869a585c03eSRussell King static int mv3310_get_tunable(struct phy_device *phydev,
870a585c03eSRussell King 			      struct ethtool_tunable *tuna, void *data)
871a585c03eSRussell King {
872a585c03eSRussell King 	switch (tuna->id) {
873a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
874a585c03eSRussell King 		return mv3310_get_edpd(phydev, data);
875a585c03eSRussell King 	default:
876a585c03eSRussell King 		return -EOPNOTSUPP;
877a585c03eSRussell King 	}
878a585c03eSRussell King }
879a585c03eSRussell King 
880a585c03eSRussell King static int mv3310_set_tunable(struct phy_device *phydev,
881a585c03eSRussell King 			      struct ethtool_tunable *tuna, const void *data)
882a585c03eSRussell King {
883a585c03eSRussell King 	switch (tuna->id) {
884a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
885a585c03eSRussell King 		return mv3310_set_edpd(phydev, *(u16 *)data);
886a585c03eSRussell King 	default:
887a585c03eSRussell King 		return -EOPNOTSUPP;
888a585c03eSRussell King 	}
889a585c03eSRussell King }
890a585c03eSRussell King 
891261a74c6SMarek Behún static void mv3310_init_supported_interfaces(unsigned long *mask)
892261a74c6SMarek Behún {
893261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_SGMII, mask);
894261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_2500BASEX, mask);
895261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_5GBASER, mask);
896261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_XAUI, mask);
897261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_RXAUI, mask);
898261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_10GBASER, mask);
899261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_USXGMII, mask);
900261a74c6SMarek Behún }
901261a74c6SMarek Behún 
902*9885d016SMarek Behún static void mv3340_init_supported_interfaces(unsigned long *mask)
903*9885d016SMarek Behún {
904*9885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_SGMII, mask);
905*9885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_2500BASEX, mask);
906*9885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_5GBASER, mask);
907*9885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_RXAUI, mask);
908*9885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_10GBASER, mask);
909*9885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_USXGMII, mask);
910*9885d016SMarek Behún }
911*9885d016SMarek Behún 
912261a74c6SMarek Behún static void mv2110_init_supported_interfaces(unsigned long *mask)
913261a74c6SMarek Behún {
914261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_SGMII, mask);
915261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_2500BASEX, mask);
916261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_5GBASER, mask);
917261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_10GBASER, mask);
918261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_USXGMII, mask);
919261a74c6SMarek Behún }
920261a74c6SMarek Behún 
92197bbe3bdSMarek Behún static const struct mv3310_chip mv3310_type = {
922261a74c6SMarek Behún 	.init_supported_interfaces = mv3310_init_supported_interfaces,
92397bbe3bdSMarek Behún 	.get_mactype = mv3310_get_mactype,
92497bbe3bdSMarek Behún 	.init_interface = mv3310_init_interface,
925884d9a67SMarek Behún 
926884d9a67SMarek Behún #ifdef CONFIG_HWMON
927884d9a67SMarek Behún 	.hwmon_read_temp_reg = mv3310_hwmon_read_temp_reg,
928884d9a67SMarek Behún #endif
92997bbe3bdSMarek Behún };
93097bbe3bdSMarek Behún 
931*9885d016SMarek Behún static const struct mv3310_chip mv3340_type = {
932*9885d016SMarek Behún 	.init_supported_interfaces = mv3340_init_supported_interfaces,
933*9885d016SMarek Behún 	.get_mactype = mv3310_get_mactype,
934*9885d016SMarek Behún 	.init_interface = mv3340_init_interface,
935*9885d016SMarek Behún 
936*9885d016SMarek Behún #ifdef CONFIG_HWMON
937*9885d016SMarek Behún 	.hwmon_read_temp_reg = mv3310_hwmon_read_temp_reg,
938*9885d016SMarek Behún #endif
939*9885d016SMarek Behún };
940*9885d016SMarek Behún 
94197bbe3bdSMarek Behún static const struct mv3310_chip mv2110_type = {
942261a74c6SMarek Behún 	.init_supported_interfaces = mv2110_init_supported_interfaces,
94397bbe3bdSMarek Behún 	.get_mactype = mv2110_get_mactype,
94497bbe3bdSMarek Behún 	.init_interface = mv2110_init_interface,
945884d9a67SMarek Behún 
946884d9a67SMarek Behún #ifdef CONFIG_HWMON
947884d9a67SMarek Behún 	.hwmon_read_temp_reg = mv2110_hwmon_read_temp_reg,
948884d9a67SMarek Behún #endif
94997bbe3bdSMarek Behún };
95097bbe3bdSMarek Behún 
95120b2af32SRussell King static struct phy_driver mv3310_drivers[] = {
95220b2af32SRussell King 	{
953631ba906SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88X3310,
954*9885d016SMarek Behún 		.phy_id_mask	= MARVELL_PHY_ID_88X33X0_MASK,
95520b2af32SRussell King 		.name		= "mv88x3310",
95697bbe3bdSMarek Behún 		.driver_data	= &mv3310_type,
95774145424SMaxime Chevallier 		.get_features	= mv3310_get_features,
95820b2af32SRussell King 		.config_init	= mv3310_config_init,
9590d3ad854SRussell King 		.probe		= mv3310_probe,
9600d3ad854SRussell King 		.suspend	= mv3310_suspend,
9610d3ad854SRussell King 		.resume		= mv3310_resume,
96220b2af32SRussell King 		.config_aneg	= mv3310_config_aneg,
96320b2af32SRussell King 		.aneg_done	= mv3310_aneg_done,
96420b2af32SRussell King 		.read_status	= mv3310_read_status,
965a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
966a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
9671b8ef142SMarek Behún 		.remove		= mv3310_remove,
968d137c70dSWong Vee Khee 		.set_loopback	= genphy_c45_loopback,
96920b2af32SRussell King 	},
97062d01535SMaxime Chevallier 	{
971*9885d016SMarek Behún 		.phy_id		= MARVELL_PHY_ID_88X3340,
972*9885d016SMarek Behún 		.phy_id_mask	= MARVELL_PHY_ID_88X33X0_MASK,
973*9885d016SMarek Behún 		.name		= "mv88x3340",
974*9885d016SMarek Behún 		.driver_data	= &mv3340_type,
975*9885d016SMarek Behún 		.get_features	= mv3310_get_features,
976*9885d016SMarek Behún 		.config_init	= mv3310_config_init,
977*9885d016SMarek Behún 		.probe		= mv3310_probe,
978*9885d016SMarek Behún 		.suspend	= mv3310_suspend,
979*9885d016SMarek Behún 		.resume		= mv3310_resume,
980*9885d016SMarek Behún 		.config_aneg	= mv3310_config_aneg,
981*9885d016SMarek Behún 		.aneg_done	= mv3310_aneg_done,
982*9885d016SMarek Behún 		.read_status	= mv3310_read_status,
983*9885d016SMarek Behún 		.get_tunable	= mv3310_get_tunable,
984*9885d016SMarek Behún 		.set_tunable	= mv3310_set_tunable,
985*9885d016SMarek Behún 		.remove		= mv3310_remove,
986*9885d016SMarek Behún 		.set_loopback	= genphy_c45_loopback,
987*9885d016SMarek Behún 	},
988*9885d016SMarek Behún 	{
98962d01535SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88E2110,
99062d01535SMaxime Chevallier 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
99162d01535SMaxime Chevallier 		.name		= "mv88x2110",
99297bbe3bdSMarek Behún 		.driver_data	= &mv2110_type,
99362d01535SMaxime Chevallier 		.probe		= mv3310_probe,
994e02c4a9dSAntoine Tenart 		.suspend	= mv3310_suspend,
995e02c4a9dSAntoine Tenart 		.resume		= mv3310_resume,
99662d01535SMaxime Chevallier 		.config_init	= mv3310_config_init,
99762d01535SMaxime Chevallier 		.config_aneg	= mv3310_config_aneg,
99862d01535SMaxime Chevallier 		.aneg_done	= mv3310_aneg_done,
99962d01535SMaxime Chevallier 		.read_status	= mv3310_read_status,
1000a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
1001a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
10021b8ef142SMarek Behún 		.remove		= mv3310_remove,
1003d137c70dSWong Vee Khee 		.set_loopback	= genphy_c45_loopback,
100462d01535SMaxime Chevallier 	},
100520b2af32SRussell King };
100620b2af32SRussell King 
100720b2af32SRussell King module_phy_driver(mv3310_drivers);
100820b2af32SRussell King 
100920b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
1010*9885d016SMarek Behún 	{ MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_88X33X0_MASK },
1011*9885d016SMarek Behún 	{ MARVELL_PHY_ID_88X3340, MARVELL_PHY_ID_88X33X0_MASK },
101262d01535SMaxime Chevallier 	{ MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK },
101320b2af32SRussell King 	{ },
101420b2af32SRussell King };
101520b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
101620b2af32SRussell King MODULE_DESCRIPTION("Marvell Alaska X 10Gigabit Ethernet PHY driver (MV88X3310)");
101720b2af32SRussell King MODULE_LICENSE("GPL");
1018