xref: /linux/drivers/net/phy/marvell10g.c (revision 4075a6a047bbb4c67a0670f4ad981cfc5ffb5c76)
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  */
25*4075a6a0SRussell King #include <linux/bitfield.h>
260d3ad854SRussell King #include <linux/ctype.h>
278d8963c3SRussell King #include <linux/delay.h>
280d3ad854SRussell King #include <linux/hwmon.h>
29952b6b3bSAntoine Tenart #include <linux/marvell_phy.h>
300d3ad854SRussell King #include <linux/phy.h>
3136023da1SRussell King #include <linux/sfp.h>
3208041a9aSVoon Weifeng #include <linux/netdevice.h>
3320b2af32SRussell King 
34c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_MASK	0xfffffffe
35c47455f9SMaxime Chevallier #define MV_PHY_ALASKA_NBT_QUIRK_REV	(MARVELL_PHY_ID_88X3310 | 0xa)
36c47455f9SMaxime Chevallier 
37*4075a6a0SRussell King #define MV_VERSION(a,b,c,d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))
38*4075a6a0SRussell King 
3920b2af32SRussell King enum {
40dd649b4fSRussell King 	MV_PMA_FW_VER0		= 0xc011,
41dd649b4fSRussell King 	MV_PMA_FW_VER1		= 0xc012,
429ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL	= 0xc04a,
439ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_SWRST				= BIT(15),
449ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK			= 0x7,
459ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII			= 0x0,
469ab0fbd0SMarek Behún 	MV_PMA_2180_PORT_CTRL_MACTYPE_DXGMII			= 0x1,
479ab0fbd0SMarek Behún 	MV_PMA_2180_PORT_CTRL_MACTYPE_QXGMII			= 0x2,
489ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER			= 0x4,
499ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN	= 0x5,
509ab0fbd0SMarek Behún 	MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH	= 0x6,
513d3ced2eSRussell King 	MV_PMA_BOOT		= 0xc050,
523d3ced2eSRussell King 	MV_PMA_BOOT_FATAL	= BIT(0),
533d3ced2eSRussell King 
5420b2af32SRussell King 	MV_PCS_BASE_T		= 0x0000,
5520b2af32SRussell King 	MV_PCS_BASE_R		= 0x1000,
5620b2af32SRussell King 	MV_PCS_1000BASEX	= 0x2000,
5720b2af32SRussell King 
588d8963c3SRussell King 	MV_PCS_CSCR1		= 0x8000,
59a585c03eSRussell King 	MV_PCS_CSCR1_ED_MASK	= 0x0300,
60a585c03eSRussell King 	MV_PCS_CSCR1_ED_OFF	= 0x0000,
61a585c03eSRussell King 	MV_PCS_CSCR1_ED_RX	= 0x0200,
62a585c03eSRussell King 	MV_PCS_CSCR1_ED_NLP	= 0x0300,
638d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MASK	= 0x0060,
648d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MDI	= 0x0000,
658d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_MDIX	= 0x0020,
668d8963c3SRussell King 	MV_PCS_CSCR1_MDIX_AUTO	= 0x0060,
678d8963c3SRussell King 
68*4075a6a0SRussell King 	MV_PCS_DSC1		= 0x8003,
69*4075a6a0SRussell King 	MV_PCS_DSC1_ENABLE	= BIT(9),
70*4075a6a0SRussell King 	MV_PCS_DSC1_10GBT	= 0x01c0,
71*4075a6a0SRussell King 	MV_PCS_DSC1_1GBR	= 0x0038,
72*4075a6a0SRussell King 	MV_PCS_DSC1_100BTX	= 0x0007,
73*4075a6a0SRussell King 	MV_PCS_DSC2		= 0x8004,
74*4075a6a0SRussell King 	MV_PCS_DSC2_2P5G	= 0xf000,
75*4075a6a0SRussell King 	MV_PCS_DSC2_5G		= 0x0f00,
76*4075a6a0SRussell King 
77c84786faSRussell King 	MV_PCS_CSSR1		= 0x8008,
78c84786faSRussell King 	MV_PCS_CSSR1_SPD1_MASK	= 0xc000,
79c84786faSRussell King 	MV_PCS_CSSR1_SPD1_SPD2	= 0xc000,
80c84786faSRussell King 	MV_PCS_CSSR1_SPD1_1000	= 0x8000,
81c84786faSRussell King 	MV_PCS_CSSR1_SPD1_100	= 0x4000,
82c84786faSRussell King 	MV_PCS_CSSR1_SPD1_10	= 0x0000,
83c84786faSRussell King 	MV_PCS_CSSR1_DUPLEX_FULL= BIT(13),
84c84786faSRussell King 	MV_PCS_CSSR1_RESOLVED	= BIT(11),
85c84786faSRussell King 	MV_PCS_CSSR1_MDIX	= BIT(6),
86c84786faSRussell King 	MV_PCS_CSSR1_SPD2_MASK	= 0x000c,
87c84786faSRussell King 	MV_PCS_CSSR1_SPD2_5000	= 0x0008,
88c84786faSRussell King 	MV_PCS_CSSR1_SPD2_2500	= 0x0004,
89c84786faSRussell King 	MV_PCS_CSSR1_SPD2_10000	= 0x0000,
90ea4efe25SRussell King 
91c3e302edSBaruch Siach 	/* Temperature read register (88E2110 only) */
92c3e302edSBaruch Siach 	MV_PCS_TEMP		= 0x8042,
93c3e302edSBaruch Siach 
94a5de4be0SMarek Behún 	/* Number of ports on the device */
95a5de4be0SMarek Behún 	MV_PCS_PORT_INFO	= 0xd00d,
96a5de4be0SMarek Behún 	MV_PCS_PORT_INFO_NPORTS_MASK	= 0x0380,
97a5de4be0SMarek Behún 	MV_PCS_PORT_INFO_NPORTS_SHIFT	= 7,
98a5de4be0SMarek Behún 
9920b2af32SRussell King 	/* These registers appear at 0x800X and 0xa00X - the 0xa00X control
10020b2af32SRussell King 	 * registers appear to set themselves to the 0x800X when AN is
10120b2af32SRussell King 	 * restarted, but status registers appear readable from either.
10220b2af32SRussell King 	 */
10320b2af32SRussell King 	MV_AN_CTRL1000		= 0x8000, /* 1000base-T control register */
10420b2af32SRussell King 	MV_AN_STAT1000		= 0x8001, /* 1000base-T status register */
1050d3ad854SRussell King 
1060d3ad854SRussell King 	/* Vendor2 MMD registers */
107af3e28cbSAntoine Tenart 	MV_V2_PORT_CTRL		= 0xf001,
1088f48c2acSRussell King 	MV_V2_PORT_CTRL_PWRDOWN					= BIT(11),
1099893f316SMarek Behún 	MV_V2_33X0_PORT_CTRL_SWRST				= BIT(15),
1109893f316SMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_MASK			= 0x7,
111f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI			= 0x0,
112f8ee45fcSMarek Behún 	MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH		= 0x1,
113f8ee45fcSMarek Behún 	MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN		= 0x1,
114f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH		= 0x2,
115f8ee45fcSMarek Behún 	MV_V2_3310_PORT_CTRL_MACTYPE_XAUI			= 0x3,
116f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER			= 0x4,
117f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN	= 0x5,
118f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH	= 0x6,
119f8ee45fcSMarek Behún 	MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII			= 0x7,
12008041a9aSVoon Weifeng 	MV_V2_PORT_INTR_STS     = 0xf040,
12108041a9aSVoon Weifeng 	MV_V2_PORT_INTR_MASK    = 0xf043,
12208041a9aSVoon Weifeng 	MV_V2_PORT_INTR_STS_WOL_EN      = BIT(8),
12308041a9aSVoon Weifeng 	MV_V2_MAGIC_PKT_WORD0   = 0xf06b,
12408041a9aSVoon Weifeng 	MV_V2_MAGIC_PKT_WORD1   = 0xf06c,
12508041a9aSVoon Weifeng 	MV_V2_MAGIC_PKT_WORD2   = 0xf06d,
12608041a9aSVoon Weifeng 	/* Wake on LAN registers */
12708041a9aSVoon Weifeng 	MV_V2_WOL_CTRL          = 0xf06e,
12808041a9aSVoon Weifeng 	MV_V2_WOL_CTRL_CLEAR_STS        = BIT(15),
12908041a9aSVoon Weifeng 	MV_V2_WOL_CTRL_MAGIC_PKT_EN     = BIT(0),
130c3e302edSBaruch Siach 	/* Temperature control/read registers (88X3310 only) */
1310d3ad854SRussell King 	MV_V2_TEMP_CTRL		= 0xf08a,
1320d3ad854SRussell King 	MV_V2_TEMP_CTRL_MASK	= 0xc000,
1330d3ad854SRussell King 	MV_V2_TEMP_CTRL_SAMPLE	= 0x0000,
1340d3ad854SRussell King 	MV_V2_TEMP_CTRL_DISABLE	= 0xc000,
1350d3ad854SRussell King 	MV_V2_TEMP		= 0xf08c,
1360d3ad854SRussell King 	MV_V2_TEMP_UNKNOWN	= 0x9600, /* unknown function */
1370d3ad854SRussell King };
1380d3ad854SRussell King 
13997bbe3bdSMarek Behún struct mv3310_chip {
140*4075a6a0SRussell King 	bool (*has_downshift)(struct phy_device *phydev);
141261a74c6SMarek Behún 	void (*init_supported_interfaces)(unsigned long *mask);
14297bbe3bdSMarek Behún 	int (*get_mactype)(struct phy_device *phydev);
14397bbe3bdSMarek Behún 	int (*init_interface)(struct phy_device *phydev, int mactype);
144884d9a67SMarek Behún 
145884d9a67SMarek Behún #ifdef CONFIG_HWMON
146884d9a67SMarek Behún 	int (*hwmon_read_temp_reg)(struct phy_device *phydev);
147884d9a67SMarek Behún #endif
14897bbe3bdSMarek Behún };
14997bbe3bdSMarek Behún 
1500d3ad854SRussell King struct mv3310_priv {
151261a74c6SMarek Behún 	DECLARE_BITMAP(supported_interfaces, PHY_INTERFACE_MODE_MAX);
152261a74c6SMarek Behún 
153dd649b4fSRussell King 	u32 firmware_ver;
154*4075a6a0SRussell King 	bool has_downshift;
155e1170333SBaruch Siach 	bool rate_match;
15697bbe3bdSMarek Behún 	phy_interface_t const_interface;
157dd649b4fSRussell King 
1580d3ad854SRussell King 	struct device *hwmon_dev;
1590d3ad854SRussell King 	char *hwmon_name;
16020b2af32SRussell King };
16120b2af32SRussell King 
16297bbe3bdSMarek Behún static const struct mv3310_chip *to_mv3310_chip(struct phy_device *phydev)
16397bbe3bdSMarek Behún {
16497bbe3bdSMarek Behún 	return phydev->drv->driver_data;
16597bbe3bdSMarek Behún }
16697bbe3bdSMarek Behún 
1670d3ad854SRussell King #ifdef CONFIG_HWMON
1680d3ad854SRussell King static umode_t mv3310_hwmon_is_visible(const void *data,
1690d3ad854SRussell King 				       enum hwmon_sensor_types type,
1700d3ad854SRussell King 				       u32 attr, int channel)
1710d3ad854SRussell King {
1720d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
1730d3ad854SRussell King 		return 0444;
1740d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input)
1750d3ad854SRussell King 		return 0444;
1760d3ad854SRussell King 	return 0;
1770d3ad854SRussell King }
1780d3ad854SRussell King 
179c3e302edSBaruch Siach static int mv3310_hwmon_read_temp_reg(struct phy_device *phydev)
180c3e302edSBaruch Siach {
181c3e302edSBaruch Siach 	return phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP);
182c3e302edSBaruch Siach }
183c3e302edSBaruch Siach 
184c3e302edSBaruch Siach static int mv2110_hwmon_read_temp_reg(struct phy_device *phydev)
185c3e302edSBaruch Siach {
186c3e302edSBaruch Siach 	return phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_TEMP);
187c3e302edSBaruch Siach }
188c3e302edSBaruch Siach 
1890d3ad854SRussell King static int mv3310_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1900d3ad854SRussell King 			     u32 attr, int channel, long *value)
1910d3ad854SRussell King {
1920d3ad854SRussell King 	struct phy_device *phydev = dev_get_drvdata(dev);
193884d9a67SMarek Behún 	const struct mv3310_chip *chip = to_mv3310_chip(phydev);
1940d3ad854SRussell King 	int temp;
1950d3ad854SRussell King 
1960d3ad854SRussell King 	if (type == hwmon_chip && attr == hwmon_chip_update_interval) {
1970d3ad854SRussell King 		*value = MSEC_PER_SEC;
1980d3ad854SRussell King 		return 0;
1990d3ad854SRussell King 	}
2000d3ad854SRussell King 
2010d3ad854SRussell King 	if (type == hwmon_temp && attr == hwmon_temp_input) {
202884d9a67SMarek Behún 		temp = chip->hwmon_read_temp_reg(phydev);
2030d3ad854SRussell King 		if (temp < 0)
2040d3ad854SRussell King 			return temp;
2050d3ad854SRussell King 
2060d3ad854SRussell King 		*value = ((temp & 0xff) - 75) * 1000;
2070d3ad854SRussell King 
2080d3ad854SRussell King 		return 0;
2090d3ad854SRussell King 	}
2100d3ad854SRussell King 
2110d3ad854SRussell King 	return -EOPNOTSUPP;
2120d3ad854SRussell King }
2130d3ad854SRussell King 
2140d3ad854SRussell King static const struct hwmon_ops mv3310_hwmon_ops = {
2150d3ad854SRussell King 	.is_visible = mv3310_hwmon_is_visible,
2160d3ad854SRussell King 	.read = mv3310_hwmon_read,
2170d3ad854SRussell King };
2180d3ad854SRussell King 
2190d3ad854SRussell King static u32 mv3310_hwmon_chip_config[] = {
2200d3ad854SRussell King 	HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
2210d3ad854SRussell King 	0,
2220d3ad854SRussell King };
2230d3ad854SRussell King 
2240d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_chip = {
2250d3ad854SRussell King 	.type = hwmon_chip,
2260d3ad854SRussell King 	.config = mv3310_hwmon_chip_config,
2270d3ad854SRussell King };
2280d3ad854SRussell King 
2290d3ad854SRussell King static u32 mv3310_hwmon_temp_config[] = {
2300d3ad854SRussell King 	HWMON_T_INPUT,
2310d3ad854SRussell King 	0,
2320d3ad854SRussell King };
2330d3ad854SRussell King 
2340d3ad854SRussell King static const struct hwmon_channel_info mv3310_hwmon_temp = {
2350d3ad854SRussell King 	.type = hwmon_temp,
2360d3ad854SRussell King 	.config = mv3310_hwmon_temp_config,
2370d3ad854SRussell King };
2380d3ad854SRussell King 
2390d3ad854SRussell King static const struct hwmon_channel_info *mv3310_hwmon_info[] = {
2400d3ad854SRussell King 	&mv3310_hwmon_chip,
2410d3ad854SRussell King 	&mv3310_hwmon_temp,
2420d3ad854SRussell King 	NULL,
2430d3ad854SRussell King };
2440d3ad854SRussell King 
2450d3ad854SRussell King static const struct hwmon_chip_info mv3310_hwmon_chip_info = {
2460d3ad854SRussell King 	.ops = &mv3310_hwmon_ops,
2470d3ad854SRussell King 	.info = mv3310_hwmon_info,
2480d3ad854SRussell King };
2490d3ad854SRussell King 
2500d3ad854SRussell King static int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
2510d3ad854SRussell King {
2520d3ad854SRussell King 	u16 val;
2530d3ad854SRussell King 	int ret;
2540d3ad854SRussell King 
255c3e302edSBaruch Siach 	if (phydev->drv->phy_id != MARVELL_PHY_ID_88X3310)
256c3e302edSBaruch Siach 		return 0;
257c3e302edSBaruch Siach 
2580d3ad854SRussell King 	ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP,
2590d3ad854SRussell King 			    MV_V2_TEMP_UNKNOWN);
2600d3ad854SRussell King 	if (ret < 0)
2610d3ad854SRussell King 		return ret;
2620d3ad854SRussell King 
2630d3ad854SRussell King 	val = enable ? MV_V2_TEMP_CTRL_SAMPLE : MV_V2_TEMP_CTRL_DISABLE;
2640d3ad854SRussell King 
265b06d8e5aSHeiner Kallweit 	return phy_modify_mmd(phydev, MDIO_MMD_VEND2, MV_V2_TEMP_CTRL,
266b06d8e5aSHeiner Kallweit 			      MV_V2_TEMP_CTRL_MASK, val);
2670d3ad854SRussell King }
2680d3ad854SRussell King 
2690d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
2700d3ad854SRussell King {
2710d3ad854SRussell King 	struct device *dev = &phydev->mdio.dev;
2720d3ad854SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
2730d3ad854SRussell King 	int i, j, ret;
2740d3ad854SRussell King 
2750d3ad854SRussell King 	priv->hwmon_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL);
2760d3ad854SRussell King 	if (!priv->hwmon_name)
2770d3ad854SRussell King 		return -ENODEV;
2780d3ad854SRussell King 
2790d3ad854SRussell King 	for (i = j = 0; priv->hwmon_name[i]; i++) {
2800d3ad854SRussell King 		if (isalnum(priv->hwmon_name[i])) {
2810d3ad854SRussell King 			if (i != j)
2820d3ad854SRussell King 				priv->hwmon_name[j] = priv->hwmon_name[i];
2830d3ad854SRussell King 			j++;
2840d3ad854SRussell King 		}
2850d3ad854SRussell King 	}
2860d3ad854SRussell King 	priv->hwmon_name[j] = '\0';
2870d3ad854SRussell King 
2880d3ad854SRussell King 	ret = mv3310_hwmon_config(phydev, true);
2890d3ad854SRussell King 	if (ret)
2900d3ad854SRussell King 		return ret;
2910d3ad854SRussell King 
2920d3ad854SRussell King 	priv->hwmon_dev = devm_hwmon_device_register_with_info(dev,
2930d3ad854SRussell King 				priv->hwmon_name, phydev,
2940d3ad854SRussell King 				&mv3310_hwmon_chip_info, NULL);
2950d3ad854SRussell King 
2960d3ad854SRussell King 	return PTR_ERR_OR_ZERO(priv->hwmon_dev);
2970d3ad854SRussell King }
2980d3ad854SRussell King #else
2990d3ad854SRussell King static inline int mv3310_hwmon_config(struct phy_device *phydev, bool enable)
3000d3ad854SRussell King {
3010d3ad854SRussell King 	return 0;
3020d3ad854SRussell King }
3030d3ad854SRussell King 
3040d3ad854SRussell King static int mv3310_hwmon_probe(struct phy_device *phydev)
3050d3ad854SRussell King {
3060d3ad854SRussell King 	return 0;
3070d3ad854SRussell King }
3080d3ad854SRussell King #endif
3090d3ad854SRussell King 
310c9cc1c81SRussell King static int mv3310_power_down(struct phy_device *phydev)
311c9cc1c81SRussell King {
312c9cc1c81SRussell King 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
313c9cc1c81SRussell King 				MV_V2_PORT_CTRL_PWRDOWN);
314c9cc1c81SRussell King }
315c9cc1c81SRussell King 
316c9cc1c81SRussell King static int mv3310_power_up(struct phy_device *phydev)
317c9cc1c81SRussell King {
3188f48c2acSRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
3198f48c2acSRussell King 	int ret;
3208f48c2acSRussell King 
3218f48c2acSRussell King 	ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
322c9cc1c81SRussell King 				 MV_V2_PORT_CTRL_PWRDOWN);
3238f48c2acSRussell King 
324829e7573SBaruch Siach 	if (phydev->drv->phy_id != MARVELL_PHY_ID_88X3310 ||
325829e7573SBaruch Siach 	    priv->firmware_ver < 0x00030000)
3268f48c2acSRussell King 		return ret;
3278f48c2acSRussell King 
3288f48c2acSRussell King 	return phy_set_bits_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL,
3299893f316SMarek Behún 				MV_V2_33X0_PORT_CTRL_SWRST);
330c9cc1c81SRussell King }
331c9cc1c81SRussell King 
3328d8963c3SRussell King static int mv3310_reset(struct phy_device *phydev, u32 unit)
3338d8963c3SRussell King {
3348964a217SDejin Zheng 	int val, err;
3358d8963c3SRussell King 
3368d8963c3SRussell King 	err = phy_modify_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1,
3378d8963c3SRussell King 			     MDIO_CTRL1_RESET, MDIO_CTRL1_RESET);
3388d8963c3SRussell King 	if (err < 0)
3398d8963c3SRussell King 		return err;
3408d8963c3SRussell King 
3418964a217SDejin Zheng 	return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_PCS,
3428964a217SDejin Zheng 					 unit + MDIO_CTRL1, val,
3438964a217SDejin Zheng 					 !(val & MDIO_CTRL1_RESET),
3448964a217SDejin Zheng 					 5000, 100000, true);
3458d8963c3SRussell King }
3468d8963c3SRussell King 
347*4075a6a0SRussell King static int mv3310_get_downshift(struct phy_device *phydev, u8 *ds)
348*4075a6a0SRussell King {
349*4075a6a0SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
350*4075a6a0SRussell King 	int val;
351*4075a6a0SRussell King 
352*4075a6a0SRussell King 	if (!priv->has_downshift)
353*4075a6a0SRussell King 		return -EOPNOTSUPP;
354*4075a6a0SRussell King 
355*4075a6a0SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC1);
356*4075a6a0SRussell King 	if (val < 0)
357*4075a6a0SRussell King 		return val;
358*4075a6a0SRussell King 
359*4075a6a0SRussell King 	if (val & MV_PCS_DSC1_ENABLE)
360*4075a6a0SRussell King 		/* assume that all fields are the same */
361*4075a6a0SRussell King 		*ds = 1 + FIELD_GET(MV_PCS_DSC1_10GBT, (u16)val);
362*4075a6a0SRussell King 	else
363*4075a6a0SRussell King 		*ds = DOWNSHIFT_DEV_DISABLE;
364*4075a6a0SRussell King 
365*4075a6a0SRussell King 	return 0;
366*4075a6a0SRussell King }
367*4075a6a0SRussell King 
368*4075a6a0SRussell King static int mv3310_set_downshift(struct phy_device *phydev, u8 ds)
369*4075a6a0SRussell King {
370*4075a6a0SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
371*4075a6a0SRussell King 	u16 val;
372*4075a6a0SRussell King 	int err;
373*4075a6a0SRussell King 
374*4075a6a0SRussell King 	if (!priv->has_downshift)
375*4075a6a0SRussell King 		return -EOPNOTSUPP;
376*4075a6a0SRussell King 
377*4075a6a0SRussell King 	if (ds == DOWNSHIFT_DEV_DISABLE)
378*4075a6a0SRussell King 		return phy_clear_bits_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC1,
379*4075a6a0SRussell King 					  MV_PCS_DSC1_ENABLE);
380*4075a6a0SRussell King 
381*4075a6a0SRussell King 	/* DOWNSHIFT_DEV_DEFAULT_COUNT is confusing. It looks like it should
382*4075a6a0SRussell King 	 * set the default settings for the PHY. However, it is used for
383*4075a6a0SRussell King 	 * "ethtool --set-phy-tunable ethN downshift on". The intention is
384*4075a6a0SRussell King 	 * to enable downshift at a default number of retries. The default
385*4075a6a0SRussell King 	 * settings for 88x3310 are for two retries with downshift disabled.
386*4075a6a0SRussell King 	 * So let's use two retries with downshift enabled.
387*4075a6a0SRussell King 	 */
388*4075a6a0SRussell King 	if (ds == DOWNSHIFT_DEV_DEFAULT_COUNT)
389*4075a6a0SRussell King 		ds = 2;
390*4075a6a0SRussell King 
391*4075a6a0SRussell King 	if (ds > 8)
392*4075a6a0SRussell King 		return -E2BIG;
393*4075a6a0SRussell King 
394*4075a6a0SRussell King 	ds -= 1;
395*4075a6a0SRussell King 	val = FIELD_PREP(MV_PCS_DSC2_2P5G, ds);
396*4075a6a0SRussell King 	val |= FIELD_PREP(MV_PCS_DSC2_5G, ds);
397*4075a6a0SRussell King 	err = phy_modify_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC2,
398*4075a6a0SRussell King 			     MV_PCS_DSC2_2P5G | MV_PCS_DSC2_5G, val);
399*4075a6a0SRussell King 	if (err < 0)
400*4075a6a0SRussell King 		return err;
401*4075a6a0SRussell King 
402*4075a6a0SRussell King 	val = MV_PCS_DSC1_ENABLE;
403*4075a6a0SRussell King 	val |= FIELD_PREP(MV_PCS_DSC1_10GBT, ds);
404*4075a6a0SRussell King 	val |= FIELD_PREP(MV_PCS_DSC1_1GBR, ds);
405*4075a6a0SRussell King 	val |= FIELD_PREP(MV_PCS_DSC1_100BTX, ds);
406*4075a6a0SRussell King 
407*4075a6a0SRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_PCS, MV_PCS_DSC1,
408*4075a6a0SRussell King 			      MV_PCS_DSC1_ENABLE | MV_PCS_DSC1_10GBT |
409*4075a6a0SRussell King 			      MV_PCS_DSC1_1GBR | MV_PCS_DSC1_100BTX, val);
410*4075a6a0SRussell King }
411*4075a6a0SRussell King 
412a585c03eSRussell King static int mv3310_get_edpd(struct phy_device *phydev, u16 *edpd)
413a585c03eSRussell King {
414a585c03eSRussell King 	int val;
415a585c03eSRussell King 
416a585c03eSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1);
417a585c03eSRussell King 	if (val < 0)
418a585c03eSRussell King 		return val;
419a585c03eSRussell King 
420a585c03eSRussell King 	switch (val & MV_PCS_CSCR1_ED_MASK) {
421a585c03eSRussell King 	case MV_PCS_CSCR1_ED_NLP:
422a585c03eSRussell King 		*edpd = 1000;
423a585c03eSRussell King 		break;
424a585c03eSRussell King 	case MV_PCS_CSCR1_ED_RX:
425a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_NO_TX;
426a585c03eSRussell King 		break;
427a585c03eSRussell King 	default:
428a585c03eSRussell King 		*edpd = ETHTOOL_PHY_EDPD_DISABLE;
429a585c03eSRussell King 		break;
430a585c03eSRussell King 	}
431a585c03eSRussell King 	return 0;
432a585c03eSRussell King }
433a585c03eSRussell King 
434a585c03eSRussell King static int mv3310_set_edpd(struct phy_device *phydev, u16 edpd)
435a585c03eSRussell King {
436a585c03eSRussell King 	u16 val;
437a585c03eSRussell King 	int err;
438a585c03eSRussell King 
439a585c03eSRussell King 	switch (edpd) {
440a585c03eSRussell King 	case 1000:
441a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS:
442a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_NLP;
443a585c03eSRussell King 		break;
444a585c03eSRussell King 
445a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_NO_TX:
446a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_RX;
447a585c03eSRussell King 		break;
448a585c03eSRussell King 
449a585c03eSRussell King 	case ETHTOOL_PHY_EDPD_DISABLE:
450a585c03eSRussell King 		val = MV_PCS_CSCR1_ED_OFF;
451a585c03eSRussell King 		break;
452a585c03eSRussell King 
453a585c03eSRussell King 	default:
454a585c03eSRussell King 		return -EINVAL;
455a585c03eSRussell King 	}
456a585c03eSRussell King 
457a585c03eSRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
458a585c03eSRussell King 				     MV_PCS_CSCR1_ED_MASK, val);
459a585c03eSRussell King 	if (err > 0)
460a585c03eSRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
461a585c03eSRussell King 
462a585c03eSRussell King 	return err;
463a585c03eSRussell King }
464a585c03eSRussell King 
46536023da1SRussell King static int mv3310_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
46636023da1SRussell King {
46736023da1SRussell King 	struct phy_device *phydev = upstream;
46836023da1SRussell King 	__ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
46936023da1SRussell King 	phy_interface_t iface;
47036023da1SRussell King 
47136023da1SRussell King 	sfp_parse_support(phydev->sfp_bus, id, support);
472a4516c70SRussell King 	iface = sfp_select_interface(phydev->sfp_bus, support);
47336023da1SRussell King 
474e0f909bcSRussell King 	if (iface != PHY_INTERFACE_MODE_10GBASER) {
47536023da1SRussell King 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
47636023da1SRussell King 		return -EINVAL;
47736023da1SRussell King 	}
47836023da1SRussell King 	return 0;
47936023da1SRussell King }
48036023da1SRussell King 
48136023da1SRussell King static const struct sfp_upstream_ops mv3310_sfp_ops = {
48236023da1SRussell King 	.attach = phy_sfp_attach,
48336023da1SRussell King 	.detach = phy_sfp_detach,
48436023da1SRussell King 	.module_insert = mv3310_sfp_insert,
48536023da1SRussell King };
48636023da1SRussell King 
48720b2af32SRussell King static int mv3310_probe(struct phy_device *phydev)
48820b2af32SRussell King {
489261a74c6SMarek Behún 	const struct mv3310_chip *chip = to_mv3310_chip(phydev);
4900d3ad854SRussell King 	struct mv3310_priv *priv;
49120b2af32SRussell King 	u32 mmd_mask = MDIO_DEVS_PMAPMD | MDIO_DEVS_AN;
4920d3ad854SRussell King 	int ret;
49320b2af32SRussell King 
49420b2af32SRussell King 	if (!phydev->is_c45 ||
49520b2af32SRussell King 	    (phydev->c45_ids.devices_in_package & mmd_mask) != mmd_mask)
49620b2af32SRussell King 		return -ENODEV;
49720b2af32SRussell King 
4983d3ced2eSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_BOOT);
4993d3ced2eSRussell King 	if (ret < 0)
5003d3ced2eSRussell King 		return ret;
5013d3ced2eSRussell King 
5023d3ced2eSRussell King 	if (ret & MV_PMA_BOOT_FATAL) {
5033d3ced2eSRussell King 		dev_warn(&phydev->mdio.dev,
5043d3ced2eSRussell King 			 "PHY failed to boot firmware, status=%04x\n", ret);
5053d3ced2eSRussell King 		return -ENODEV;
5063d3ced2eSRussell King 	}
5073d3ced2eSRussell King 
5080d3ad854SRussell King 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
5090d3ad854SRussell King 	if (!priv)
5100d3ad854SRussell King 		return -ENOMEM;
5110d3ad854SRussell King 
5120d3ad854SRussell King 	dev_set_drvdata(&phydev->mdio.dev, priv);
5130d3ad854SRussell King 
514dd649b4fSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER0);
515dd649b4fSRussell King 	if (ret < 0)
516dd649b4fSRussell King 		return ret;
517dd649b4fSRussell King 
518dd649b4fSRussell King 	priv->firmware_ver = ret << 16;
519dd649b4fSRussell King 
520dd649b4fSRussell King 	ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_FW_VER1);
521dd649b4fSRussell King 	if (ret < 0)
522dd649b4fSRussell King 		return ret;
523dd649b4fSRussell King 
524dd649b4fSRussell King 	priv->firmware_ver |= ret;
525dd649b4fSRussell King 
526dd649b4fSRussell King 	phydev_info(phydev, "Firmware version %u.%u.%u.%u\n",
527dd649b4fSRussell King 		    priv->firmware_ver >> 24, (priv->firmware_ver >> 16) & 255,
528dd649b4fSRussell King 		    (priv->firmware_ver >> 8) & 255, priv->firmware_ver & 255);
529dd649b4fSRussell King 
530*4075a6a0SRussell King 	if (chip->has_downshift)
531*4075a6a0SRussell King 		priv->has_downshift = chip->has_downshift(phydev);
532*4075a6a0SRussell King 
533c9cc1c81SRussell King 	/* Powering down the port when not in use saves about 600mW */
534c9cc1c81SRussell King 	ret = mv3310_power_down(phydev);
535c9cc1c81SRussell King 	if (ret)
536c9cc1c81SRussell King 		return ret;
537c9cc1c81SRussell King 
5380d3ad854SRussell King 	ret = mv3310_hwmon_probe(phydev);
5390d3ad854SRussell King 	if (ret)
5400d3ad854SRussell King 		return ret;
5410d3ad854SRussell King 
542261a74c6SMarek Behún 	chip->init_supported_interfaces(priv->supported_interfaces);
543261a74c6SMarek Behún 
54436023da1SRussell King 	return phy_sfp_probe(phydev, &mv3310_sfp_ops);
54520b2af32SRussell King }
54620b2af32SRussell King 
5471b8ef142SMarek Behún static void mv3310_remove(struct phy_device *phydev)
5481b8ef142SMarek Behún {
5491b8ef142SMarek Behún 	mv3310_hwmon_config(phydev, false);
5501b8ef142SMarek Behún }
5511b8ef142SMarek Behún 
5520d3ad854SRussell King static int mv3310_suspend(struct phy_device *phydev)
5530d3ad854SRussell King {
554c9cc1c81SRussell King 	return mv3310_power_down(phydev);
5550d3ad854SRussell King }
5560d3ad854SRussell King 
5570d3ad854SRussell King static int mv3310_resume(struct phy_device *phydev)
5580d3ad854SRussell King {
559af3e28cbSAntoine Tenart 	int ret;
560af3e28cbSAntoine Tenart 
561c9cc1c81SRussell King 	ret = mv3310_power_up(phydev);
562af3e28cbSAntoine Tenart 	if (ret)
563af3e28cbSAntoine Tenart 		return ret;
564af3e28cbSAntoine Tenart 
5650d3ad854SRussell King 	return mv3310_hwmon_config(phydev, true);
5660d3ad854SRussell King }
5670d3ad854SRussell King 
568c47455f9SMaxime Chevallier /* Some PHYs in the Alaska family such as the 88X3310 and the 88E2010
569c47455f9SMaxime Chevallier  * don't set bit 14 in PMA Extended Abilities (1.11), although they do
570c47455f9SMaxime Chevallier  * support 2.5GBASET and 5GBASET. For these models, we can still read their
571c47455f9SMaxime Chevallier  * 2.5G/5G extended abilities register (1.21). We detect these models based on
572c47455f9SMaxime Chevallier  * the PMA device identifier, with a mask matching models known to have this
573c47455f9SMaxime Chevallier  * issue
574c47455f9SMaxime Chevallier  */
575c47455f9SMaxime Chevallier static bool mv3310_has_pma_ngbaset_quirk(struct phy_device *phydev)
576c47455f9SMaxime Chevallier {
577c47455f9SMaxime Chevallier 	if (!(phydev->c45_ids.devices_in_package & MDIO_DEVS_PMAPMD))
578c47455f9SMaxime Chevallier 		return false;
579c47455f9SMaxime Chevallier 
580c47455f9SMaxime Chevallier 	/* Only some revisions of the 88X3310 family PMA seem to be impacted */
581c47455f9SMaxime Chevallier 	return (phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
582c47455f9SMaxime Chevallier 		MV_PHY_ALASKA_NBT_QUIRK_MASK) == MV_PHY_ALASKA_NBT_QUIRK_REV;
583c47455f9SMaxime Chevallier }
584c47455f9SMaxime Chevallier 
58597bbe3bdSMarek Behún static int mv2110_get_mactype(struct phy_device *phydev)
58697bbe3bdSMarek Behún {
58797bbe3bdSMarek Behún 	int mactype;
58897bbe3bdSMarek Behún 
58997bbe3bdSMarek Behún 	mactype = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MV_PMA_21X0_PORT_CTRL);
59097bbe3bdSMarek Behún 	if (mactype < 0)
59197bbe3bdSMarek Behún 		return mactype;
59297bbe3bdSMarek Behún 
59397bbe3bdSMarek Behún 	return mactype & MV_PMA_21X0_PORT_CTRL_MACTYPE_MASK;
59497bbe3bdSMarek Behún }
59597bbe3bdSMarek Behún 
59697bbe3bdSMarek Behún static int mv3310_get_mactype(struct phy_device *phydev)
59797bbe3bdSMarek Behún {
59897bbe3bdSMarek Behún 	int mactype;
59997bbe3bdSMarek Behún 
60097bbe3bdSMarek Behún 	mactype = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_PORT_CTRL);
60197bbe3bdSMarek Behún 	if (mactype < 0)
60297bbe3bdSMarek Behún 		return mactype;
60397bbe3bdSMarek Behún 
60497bbe3bdSMarek Behún 	return mactype & MV_V2_33X0_PORT_CTRL_MACTYPE_MASK;
60597bbe3bdSMarek Behún }
60697bbe3bdSMarek Behún 
60797bbe3bdSMarek Behún static int mv2110_init_interface(struct phy_device *phydev, int mactype)
60820b2af32SRussell King {
609e1170333SBaruch Siach 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
61097bbe3bdSMarek Behún 
61197bbe3bdSMarek Behún 	priv->rate_match = false;
61297bbe3bdSMarek Behún 
613ccbf2891SMarek Behún 	if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
61497bbe3bdSMarek Behún 		priv->rate_match = true;
615ccbf2891SMarek Behún 
616ccbf2891SMarek Behún 	if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_USXGMII)
617ccbf2891SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
618ccbf2891SMarek Behún 	else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH)
61997bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_10GBASER;
620ccbf2891SMarek Behún 	else if (mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER ||
621ccbf2891SMarek Behún 		 mactype == MV_PMA_21X0_PORT_CTRL_MACTYPE_5GBASER_NO_SGMII_AN)
622ccbf2891SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_NA;
623ccbf2891SMarek Behún 	else
624ccbf2891SMarek Behún 		return -EINVAL;
62597bbe3bdSMarek Behún 
62697bbe3bdSMarek Behún 	return 0;
62797bbe3bdSMarek Behún }
62897bbe3bdSMarek Behún 
62997bbe3bdSMarek Behún static int mv3310_init_interface(struct phy_device *phydev, int mactype)
63097bbe3bdSMarek Behún {
63197bbe3bdSMarek Behún 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
63297bbe3bdSMarek Behún 
63397bbe3bdSMarek Behún 	priv->rate_match = false;
63497bbe3bdSMarek Behún 
63597bbe3bdSMarek Behún 	if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
63697bbe3bdSMarek Behún 	    mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
63797bbe3bdSMarek Behún 	    mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH)
63897bbe3bdSMarek Behún 		priv->rate_match = true;
63997bbe3bdSMarek Behún 
640ccbf2891SMarek Behún 	if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_USXGMII)
641ccbf2891SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_USXGMII;
642ccbf2891SMarek Behún 	else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_RATE_MATCH ||
643ccbf2891SMarek Behún 		 mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER_NO_SGMII_AN ||
644ccbf2891SMarek Behún 		 mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_10GBASER)
64597bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_10GBASER;
646ccbf2891SMarek Behún 	else if (mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI_RATE_MATCH ||
647ccbf2891SMarek Behún 		 mactype == MV_V2_33X0_PORT_CTRL_MACTYPE_RXAUI)
64897bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_RXAUI;
649ccbf2891SMarek Behún 	else if (mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI_RATE_MATCH ||
650ccbf2891SMarek Behún 		 mactype == MV_V2_3310_PORT_CTRL_MACTYPE_XAUI)
65197bbe3bdSMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_XAUI;
652ccbf2891SMarek Behún 	else
653ccbf2891SMarek Behún 		return -EINVAL;
65497bbe3bdSMarek Behún 
65597bbe3bdSMarek Behún 	return 0;
65697bbe3bdSMarek Behún }
65797bbe3bdSMarek Behún 
6589885d016SMarek Behún static int mv3340_init_interface(struct phy_device *phydev, int mactype)
6599885d016SMarek Behún {
6609885d016SMarek Behún 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
6619885d016SMarek Behún 	int err = 0;
6629885d016SMarek Behún 
6639885d016SMarek Behún 	priv->rate_match = false;
6649885d016SMarek Behún 
6659885d016SMarek Behún 	if (mactype == MV_V2_3340_PORT_CTRL_MACTYPE_RXAUI_NO_SGMII_AN)
6669885d016SMarek Behún 		priv->const_interface = PHY_INTERFACE_MODE_RXAUI;
6679885d016SMarek Behún 	else
6689885d016SMarek Behún 		err = mv3310_init_interface(phydev, mactype);
6699885d016SMarek Behún 
6709885d016SMarek Behún 	return err;
6719885d016SMarek Behún }
6729885d016SMarek Behún 
67397bbe3bdSMarek Behún static int mv3310_config_init(struct phy_device *phydev)
67497bbe3bdSMarek Behún {
675261a74c6SMarek Behún 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
67697bbe3bdSMarek Behún 	const struct mv3310_chip *chip = to_mv3310_chip(phydev);
67797bbe3bdSMarek Behún 	int err, mactype;
678c9cc1c81SRussell King 
67920b2af32SRussell King 	/* Check that the PHY interface type is compatible */
680261a74c6SMarek Behún 	if (!test_bit(phydev->interface, priv->supported_interfaces))
68120b2af32SRussell King 		return -ENODEV;
68220b2af32SRussell King 
6838d8963c3SRussell King 	phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
6848d8963c3SRussell King 
685c9cc1c81SRussell King 	/* Power up so reset works */
686c9cc1c81SRussell King 	err = mv3310_power_up(phydev);
687c9cc1c81SRussell King 	if (err)
688c9cc1c81SRussell King 		return err;
689c9cc1c81SRussell King 
69097bbe3bdSMarek Behún 	mactype = chip->get_mactype(phydev);
69197bbe3bdSMarek Behún 	if (mactype < 0)
69297bbe3bdSMarek Behún 		return mactype;
69397bbe3bdSMarek Behún 
69497bbe3bdSMarek Behún 	err = chip->init_interface(phydev, mactype);
695ccbf2891SMarek Behún 	if (err) {
696ccbf2891SMarek Behún 		phydev_err(phydev, "MACTYPE configuration invalid\n");
69797bbe3bdSMarek Behún 		return err;
698ccbf2891SMarek Behún 	}
699e1170333SBaruch Siach 
700a585c03eSRussell King 	/* Enable EDPD mode - saving 600mW */
701*4075a6a0SRussell King 	err = mv3310_set_edpd(phydev, ETHTOOL_PHY_EDPD_DFLT_TX_MSECS);
702*4075a6a0SRussell King 	if (err)
703*4075a6a0SRussell King 		return err;
704*4075a6a0SRussell King 
705*4075a6a0SRussell King 	/* Allow downshift */
706*4075a6a0SRussell King 	err = mv3310_set_downshift(phydev, DOWNSHIFT_DEV_DEFAULT_COUNT);
707*4075a6a0SRussell King 	if (err && err != -EOPNOTSUPP)
708*4075a6a0SRussell King 		return err;
709*4075a6a0SRussell King 
710*4075a6a0SRussell King 	return 0;
71174145424SMaxime Chevallier }
71274145424SMaxime Chevallier 
71374145424SMaxime Chevallier static int mv3310_get_features(struct phy_device *phydev)
71474145424SMaxime Chevallier {
71574145424SMaxime Chevallier 	int ret, val;
71674145424SMaxime Chevallier 
717ac3f5533SMaxime Chevallier 	ret = genphy_c45_pma_read_abilities(phydev);
718ac3f5533SMaxime Chevallier 	if (ret)
719ac3f5533SMaxime Chevallier 		return ret;
72020b2af32SRussell King 
721c47455f9SMaxime Chevallier 	if (mv3310_has_pma_ngbaset_quirk(phydev)) {
722c47455f9SMaxime Chevallier 		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
723c47455f9SMaxime Chevallier 				   MDIO_PMA_NG_EXTABLE);
724c47455f9SMaxime Chevallier 		if (val < 0)
725c47455f9SMaxime Chevallier 			return val;
726c47455f9SMaxime Chevallier 
727c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
728c47455f9SMaxime Chevallier 				 phydev->supported,
729c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_2_5GBT);
730c47455f9SMaxime Chevallier 
731c47455f9SMaxime Chevallier 		linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
732c47455f9SMaxime Chevallier 				 phydev->supported,
733c47455f9SMaxime Chevallier 				 val & MDIO_PMA_NG_EXTABLE_5GBT);
734c47455f9SMaxime Chevallier 	}
735c47455f9SMaxime Chevallier 
73620b2af32SRussell King 	return 0;
73720b2af32SRussell King }
73820b2af32SRussell King 
7398d8963c3SRussell King static int mv3310_config_mdix(struct phy_device *phydev)
7408d8963c3SRussell King {
7418d8963c3SRussell King 	u16 val;
7428d8963c3SRussell King 	int err;
7438d8963c3SRussell King 
7448d8963c3SRussell King 	switch (phydev->mdix_ctrl) {
7458d8963c3SRussell King 	case ETH_TP_MDI_AUTO:
7468d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_AUTO;
7478d8963c3SRussell King 		break;
7488d8963c3SRussell King 	case ETH_TP_MDI_X:
7498d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDIX;
7508d8963c3SRussell King 		break;
7518d8963c3SRussell King 	case ETH_TP_MDI:
7528d8963c3SRussell King 		val = MV_PCS_CSCR1_MDIX_MDI;
7538d8963c3SRussell King 		break;
7548d8963c3SRussell King 	default:
7558d8963c3SRussell King 		return -EINVAL;
7568d8963c3SRussell King 	}
7578d8963c3SRussell King 
7588d8963c3SRussell King 	err = phy_modify_mmd_changed(phydev, MDIO_MMD_PCS, MV_PCS_CSCR1,
7598d8963c3SRussell King 				     MV_PCS_CSCR1_MDIX_MASK, val);
7608d8963c3SRussell King 	if (err > 0)
7618d8963c3SRussell King 		err = mv3310_reset(phydev, MV_PCS_BASE_T);
7628d8963c3SRussell King 
7638d8963c3SRussell King 	return err;
7648d8963c3SRussell King }
7658d8963c3SRussell King 
76620b2af32SRussell King static int mv3310_config_aneg(struct phy_device *phydev)
76720b2af32SRussell King {
76820b2af32SRussell King 	bool changed = false;
7693c1bcc86SAndrew Lunn 	u16 reg;
77020b2af32SRussell King 	int ret;
77120b2af32SRussell King 
7728d8963c3SRussell King 	ret = mv3310_config_mdix(phydev);
7738d8963c3SRussell King 	if (ret < 0)
7748d8963c3SRussell King 		return ret;
775ea4efe25SRussell King 
77630de65c3SHeiner Kallweit 	if (phydev->autoneg == AUTONEG_DISABLE)
77730de65c3SHeiner Kallweit 		return genphy_c45_pma_setup_forced(phydev);
77820b2af32SRussell King 
7793de97f3cSAndrew Lunn 	ret = genphy_c45_an_config_aneg(phydev);
78020b2af32SRussell King 	if (ret < 0)
78120b2af32SRussell King 		return ret;
78220b2af32SRussell King 	if (ret > 0)
78320b2af32SRussell King 		changed = true;
78420b2af32SRussell King 
7853de97f3cSAndrew Lunn 	/* Clause 45 has no standardized support for 1000BaseT, therefore
7863de97f3cSAndrew Lunn 	 * use vendor registers for this mode.
7873de97f3cSAndrew Lunn 	 */
7883c1bcc86SAndrew Lunn 	reg = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
789b06d8e5aSHeiner Kallweit 	ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MV_AN_CTRL1000,
7903c1bcc86SAndrew Lunn 			     ADVERTISE_1000FULL | ADVERTISE_1000HALF, reg);
79120b2af32SRussell King 	if (ret < 0)
79220b2af32SRussell King 		return ret;
79320b2af32SRussell King 	if (ret > 0)
79420b2af32SRussell King 		changed = true;
79520b2af32SRussell King 
7966b4cb6cbSHeiner Kallweit 	return genphy_c45_check_and_restart_aneg(phydev, changed);
79720b2af32SRussell King }
79820b2af32SRussell King 
79920b2af32SRussell King static int mv3310_aneg_done(struct phy_device *phydev)
80020b2af32SRussell King {
80120b2af32SRussell King 	int val;
80220b2af32SRussell King 
80320b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
80420b2af32SRussell King 	if (val < 0)
80520b2af32SRussell King 		return val;
80620b2af32SRussell King 
80720b2af32SRussell King 	if (val & MDIO_STAT1_LSTATUS)
80820b2af32SRussell King 		return 1;
80920b2af32SRussell King 
81020b2af32SRussell King 	return genphy_c45_aneg_done(phydev);
81120b2af32SRussell King }
81220b2af32SRussell King 
81336c4449aSRussell King static void mv3310_update_interface(struct phy_device *phydev)
81436c4449aSRussell King {
815e1170333SBaruch Siach 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
816e1170333SBaruch Siach 
817ccbf2891SMarek Behún 	if (!phydev->link)
818ccbf2891SMarek Behún 		return;
819ccbf2891SMarek Behún 
82097bbe3bdSMarek Behún 	/* In all of the "* with Rate Matching" modes the PHY interface is fixed
82197bbe3bdSMarek Behún 	 * at 10Gb. The PHY adapts the rate to actual wire speed with help of
822e1170333SBaruch Siach 	 * internal 16KB buffer.
823ccbf2891SMarek Behún 	 *
824ccbf2891SMarek Behún 	 * In USXGMII mode the PHY interface mode is also fixed.
825e1170333SBaruch Siach 	 */
826ccbf2891SMarek Behún 	if (priv->rate_match ||
827ccbf2891SMarek Behún 	    priv->const_interface == PHY_INTERFACE_MODE_USXGMII) {
82897bbe3bdSMarek Behún 		phydev->interface = priv->const_interface;
829e1170333SBaruch Siach 		return;
830e1170333SBaruch Siach 	}
831e1170333SBaruch Siach 
832ccbf2891SMarek Behún 	/* The PHY automatically switches its serdes interface (and active PHYXS
833ccbf2891SMarek Behún 	 * instance) between Cisco SGMII, 2500BaseX, 5GBase-R and 10GBase-R /
834ccbf2891SMarek Behún 	 * xaui / rxaui modes according to the speed.
835ccbf2891SMarek Behún 	 * Florian suggests setting phydev->interface to communicate this to the
836ccbf2891SMarek Behún 	 * MAC. Only do this if we are already in one of the above modes.
83736c4449aSRussell King 	 */
838e555e5b1SMaxime Chevallier 	switch (phydev->speed) {
839e555e5b1SMaxime Chevallier 	case SPEED_10000:
840ccbf2891SMarek Behún 		phydev->interface = priv->const_interface;
841e555e5b1SMaxime Chevallier 		break;
8420d375542SMarek Behún 	case SPEED_5000:
8430d375542SMarek Behún 		phydev->interface = PHY_INTERFACE_MODE_5GBASER;
8440d375542SMarek Behún 		break;
845e555e5b1SMaxime Chevallier 	case SPEED_2500:
846e555e5b1SMaxime Chevallier 		phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
847e555e5b1SMaxime Chevallier 		break;
848e555e5b1SMaxime Chevallier 	case SPEED_1000:
849e555e5b1SMaxime Chevallier 	case SPEED_100:
850e555e5b1SMaxime Chevallier 	case SPEED_10:
85136c4449aSRussell King 		phydev->interface = PHY_INTERFACE_MODE_SGMII;
852e555e5b1SMaxime Chevallier 		break;
853e555e5b1SMaxime Chevallier 	default:
854e555e5b1SMaxime Chevallier 		break;
855e555e5b1SMaxime Chevallier 	}
85636c4449aSRussell King }
85736c4449aSRussell King 
85820b2af32SRussell King /* 10GBASE-ER,LR,LRM,SR do not support autonegotiation. */
859c84786faSRussell King static int mv3310_read_status_10gbaser(struct phy_device *phydev)
86020b2af32SRussell King {
86120b2af32SRussell King 	phydev->link = 1;
86220b2af32SRussell King 	phydev->speed = SPEED_10000;
86320b2af32SRussell King 	phydev->duplex = DUPLEX_FULL;
8644217a64eSMichael Walle 	phydev->port = PORT_FIBRE;
86520b2af32SRussell King 
86620b2af32SRussell King 	return 0;
86720b2af32SRussell King }
86820b2af32SRussell King 
869c84786faSRussell King static int mv3310_read_status_copper(struct phy_device *phydev)
87020b2af32SRussell King {
871c84786faSRussell King 	int cssr1, speed, val;
87220b2af32SRussell King 
873998a8a83SHeiner Kallweit 	val = genphy_c45_read_link(phydev);
87420b2af32SRussell King 	if (val < 0)
87520b2af32SRussell King 		return val;
87620b2af32SRussell King 
87720b2af32SRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
87820b2af32SRussell King 	if (val < 0)
87920b2af32SRussell King 		return val;
88020b2af32SRussell King 
881c84786faSRussell King 	cssr1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_CSSR1);
882c84786faSRussell King 	if (cssr1 < 0)
883c84786faSRussell King 		return val;
884c84786faSRussell King 
885c84786faSRussell King 	/* If the link settings are not resolved, mark the link down */
886c84786faSRussell King 	if (!(cssr1 & MV_PCS_CSSR1_RESOLVED)) {
887c84786faSRussell King 		phydev->link = 0;
888c84786faSRussell King 		return 0;
889c84786faSRussell King 	}
890c84786faSRussell King 
891c84786faSRussell King 	/* Read the copper link settings */
892c84786faSRussell King 	speed = cssr1 & MV_PCS_CSSR1_SPD1_MASK;
893c84786faSRussell King 	if (speed == MV_PCS_CSSR1_SPD1_SPD2)
894c84786faSRussell King 		speed |= cssr1 & MV_PCS_CSSR1_SPD2_MASK;
895c84786faSRussell King 
896c84786faSRussell King 	switch (speed) {
897c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_10000:
898c84786faSRussell King 		phydev->speed = SPEED_10000;
899c84786faSRussell King 		break;
900c84786faSRussell King 
901c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_5000:
902c84786faSRussell King 		phydev->speed = SPEED_5000;
903c84786faSRussell King 		break;
904c84786faSRussell King 
905c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_SPD2 | MV_PCS_CSSR1_SPD2_2500:
906c84786faSRussell King 		phydev->speed = SPEED_2500;
907c84786faSRussell King 		break;
908c84786faSRussell King 
909c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_1000:
910c84786faSRussell King 		phydev->speed = SPEED_1000;
911c84786faSRussell King 		break;
912c84786faSRussell King 
913c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_100:
914c84786faSRussell King 		phydev->speed = SPEED_100;
915c84786faSRussell King 		break;
916c84786faSRussell King 
917c84786faSRussell King 	case MV_PCS_CSSR1_SPD1_10:
918c84786faSRussell King 		phydev->speed = SPEED_10;
919c84786faSRussell King 		break;
920c84786faSRussell King 	}
921c84786faSRussell King 
922c84786faSRussell King 	phydev->duplex = cssr1 & MV_PCS_CSSR1_DUPLEX_FULL ?
923c84786faSRussell King 			 DUPLEX_FULL : DUPLEX_HALF;
9244217a64eSMichael Walle 	phydev->port = PORT_TP;
925c84786faSRussell King 	phydev->mdix = cssr1 & MV_PCS_CSSR1_MDIX ?
926c84786faSRussell King 		       ETH_TP_MDI_X : ETH_TP_MDI;
927c84786faSRussell King 
92820b2af32SRussell King 	if (val & MDIO_AN_STAT1_COMPLETE) {
92920b2af32SRussell King 		val = genphy_c45_read_lpa(phydev);
93020b2af32SRussell King 		if (val < 0)
93120b2af32SRussell King 			return val;
93220b2af32SRussell King 
933cc1122b0SColin Ian King 		/* Read the link partner's 1G advertisement */
93420b2af32SRussell King 		val = phy_read_mmd(phydev, MDIO_MMD_AN, MV_AN_STAT1000);
93520b2af32SRussell King 		if (val < 0)
93620b2af32SRussell King 			return val;
93720b2af32SRussell King 
93878a24df3SAndrew Lunn 		mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, val);
93920b2af32SRussell King 
940c84786faSRussell King 		/* Update the pause status */
941c84786faSRussell King 		phy_resolve_aneg_pause(phydev);
94220b2af32SRussell King 	}
94320b2af32SRussell King 
944c84786faSRussell King 	return 0;
94520b2af32SRussell King }
94620b2af32SRussell King 
947c84786faSRussell King static int mv3310_read_status(struct phy_device *phydev)
948c84786faSRussell King {
949c84786faSRussell King 	int err, val;
950ea4efe25SRussell King 
951c84786faSRussell King 	phydev->speed = SPEED_UNKNOWN;
952c84786faSRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
953c84786faSRussell King 	linkmode_zero(phydev->lp_advertising);
954c84786faSRussell King 	phydev->link = 0;
955c84786faSRussell King 	phydev->pause = 0;
956c84786faSRussell King 	phydev->asym_pause = 0;
957ea4efe25SRussell King 	phydev->mdix = ETH_TP_MDI_INVALID;
958ea4efe25SRussell King 
959c84786faSRussell King 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_BASE_R + MDIO_STAT1);
960c84786faSRussell King 	if (val < 0)
961c84786faSRussell King 		return val;
962c84786faSRussell King 
963c84786faSRussell King 	if (val & MDIO_STAT1_LSTATUS)
964c84786faSRussell King 		err = mv3310_read_status_10gbaser(phydev);
965c84786faSRussell King 	else
966c84786faSRussell King 		err = mv3310_read_status_copper(phydev);
967c84786faSRussell King 	if (err < 0)
968c84786faSRussell King 		return err;
969c84786faSRussell King 
970c84786faSRussell King 	if (phydev->link)
97136c4449aSRussell King 		mv3310_update_interface(phydev);
97220b2af32SRussell King 
97320b2af32SRussell King 	return 0;
97420b2af32SRussell King }
97520b2af32SRussell King 
976a585c03eSRussell King static int mv3310_get_tunable(struct phy_device *phydev,
977a585c03eSRussell King 			      struct ethtool_tunable *tuna, void *data)
978a585c03eSRussell King {
979a585c03eSRussell King 	switch (tuna->id) {
980*4075a6a0SRussell King 	case ETHTOOL_PHY_DOWNSHIFT:
981*4075a6a0SRussell King 		return mv3310_get_downshift(phydev, data);
982a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
983a585c03eSRussell King 		return mv3310_get_edpd(phydev, data);
984a585c03eSRussell King 	default:
985a585c03eSRussell King 		return -EOPNOTSUPP;
986a585c03eSRussell King 	}
987a585c03eSRussell King }
988a585c03eSRussell King 
989a585c03eSRussell King static int mv3310_set_tunable(struct phy_device *phydev,
990a585c03eSRussell King 			      struct ethtool_tunable *tuna, const void *data)
991a585c03eSRussell King {
992a585c03eSRussell King 	switch (tuna->id) {
993*4075a6a0SRussell King 	case ETHTOOL_PHY_DOWNSHIFT:
994*4075a6a0SRussell King 		return mv3310_set_downshift(phydev, *(u8 *)data);
995a585c03eSRussell King 	case ETHTOOL_PHY_EDPD:
996a585c03eSRussell King 		return mv3310_set_edpd(phydev, *(u16 *)data);
997a585c03eSRussell King 	default:
998a585c03eSRussell King 		return -EOPNOTSUPP;
999a585c03eSRussell King 	}
1000a585c03eSRussell King }
1001a585c03eSRussell King 
1002*4075a6a0SRussell King static bool mv3310_has_downshift(struct phy_device *phydev)
1003*4075a6a0SRussell King {
1004*4075a6a0SRussell King 	struct mv3310_priv *priv = dev_get_drvdata(&phydev->mdio.dev);
1005*4075a6a0SRussell King 
1006*4075a6a0SRussell King 	/* Fails to downshift with firmware older than v0.3.5.0 */
1007*4075a6a0SRussell King 	return priv->firmware_ver >= MV_VERSION(0,3,5,0);
1008*4075a6a0SRussell King }
1009*4075a6a0SRussell King 
1010261a74c6SMarek Behún static void mv3310_init_supported_interfaces(unsigned long *mask)
1011261a74c6SMarek Behún {
1012261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_SGMII, mask);
1013261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_2500BASEX, mask);
1014261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_5GBASER, mask);
1015261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_XAUI, mask);
1016261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_RXAUI, mask);
1017261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_10GBASER, mask);
1018261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_USXGMII, mask);
1019261a74c6SMarek Behún }
1020261a74c6SMarek Behún 
10219885d016SMarek Behún static void mv3340_init_supported_interfaces(unsigned long *mask)
10229885d016SMarek Behún {
10239885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_SGMII, mask);
10249885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_2500BASEX, mask);
10259885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_5GBASER, mask);
10269885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_RXAUI, mask);
10279885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_10GBASER, mask);
10289885d016SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_USXGMII, mask);
10299885d016SMarek Behún }
10309885d016SMarek Behún 
1031261a74c6SMarek Behún static void mv2110_init_supported_interfaces(unsigned long *mask)
1032261a74c6SMarek Behún {
1033261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_SGMII, mask);
1034261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_2500BASEX, mask);
1035261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_5GBASER, mask);
1036261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_10GBASER, mask);
1037261a74c6SMarek Behún 	__set_bit(PHY_INTERFACE_MODE_USXGMII, mask);
1038261a74c6SMarek Behún }
1039261a74c6SMarek Behún 
10400fca947cSMarek Behún static void mv2111_init_supported_interfaces(unsigned long *mask)
10410fca947cSMarek Behún {
10420fca947cSMarek Behún 	__set_bit(PHY_INTERFACE_MODE_SGMII, mask);
10430fca947cSMarek Behún 	__set_bit(PHY_INTERFACE_MODE_2500BASEX, mask);
10440fca947cSMarek Behún 	__set_bit(PHY_INTERFACE_MODE_10GBASER, mask);
10450fca947cSMarek Behún 	__set_bit(PHY_INTERFACE_MODE_USXGMII, mask);
10460fca947cSMarek Behún }
10470fca947cSMarek Behún 
104897bbe3bdSMarek Behún static const struct mv3310_chip mv3310_type = {
1049*4075a6a0SRussell King 	.has_downshift = mv3310_has_downshift,
1050261a74c6SMarek Behún 	.init_supported_interfaces = mv3310_init_supported_interfaces,
105197bbe3bdSMarek Behún 	.get_mactype = mv3310_get_mactype,
105297bbe3bdSMarek Behún 	.init_interface = mv3310_init_interface,
1053884d9a67SMarek Behún 
1054884d9a67SMarek Behún #ifdef CONFIG_HWMON
1055884d9a67SMarek Behún 	.hwmon_read_temp_reg = mv3310_hwmon_read_temp_reg,
1056884d9a67SMarek Behún #endif
105797bbe3bdSMarek Behún };
105897bbe3bdSMarek Behún 
10599885d016SMarek Behún static const struct mv3310_chip mv3340_type = {
1060*4075a6a0SRussell King 	.has_downshift = mv3310_has_downshift,
10619885d016SMarek Behún 	.init_supported_interfaces = mv3340_init_supported_interfaces,
10629885d016SMarek Behún 	.get_mactype = mv3310_get_mactype,
10639885d016SMarek Behún 	.init_interface = mv3340_init_interface,
10649885d016SMarek Behún 
10659885d016SMarek Behún #ifdef CONFIG_HWMON
10669885d016SMarek Behún 	.hwmon_read_temp_reg = mv3310_hwmon_read_temp_reg,
10679885d016SMarek Behún #endif
10689885d016SMarek Behún };
10699885d016SMarek Behún 
107097bbe3bdSMarek Behún static const struct mv3310_chip mv2110_type = {
1071261a74c6SMarek Behún 	.init_supported_interfaces = mv2110_init_supported_interfaces,
107297bbe3bdSMarek Behún 	.get_mactype = mv2110_get_mactype,
107397bbe3bdSMarek Behún 	.init_interface = mv2110_init_interface,
1074884d9a67SMarek Behún 
1075884d9a67SMarek Behún #ifdef CONFIG_HWMON
1076884d9a67SMarek Behún 	.hwmon_read_temp_reg = mv2110_hwmon_read_temp_reg,
1077884d9a67SMarek Behún #endif
107897bbe3bdSMarek Behún };
107997bbe3bdSMarek Behún 
10800fca947cSMarek Behún static const struct mv3310_chip mv2111_type = {
10810fca947cSMarek Behún 	.init_supported_interfaces = mv2111_init_supported_interfaces,
10820fca947cSMarek Behún 	.get_mactype = mv2110_get_mactype,
10830fca947cSMarek Behún 	.init_interface = mv2110_init_interface,
10840fca947cSMarek Behún 
10850fca947cSMarek Behún #ifdef CONFIG_HWMON
10860fca947cSMarek Behún 	.hwmon_read_temp_reg = mv2110_hwmon_read_temp_reg,
10870fca947cSMarek Behún #endif
10880fca947cSMarek Behún };
10890fca947cSMarek Behún 
1090a5de4be0SMarek Behún static int mv3310_get_number_of_ports(struct phy_device *phydev)
1091a5de4be0SMarek Behún {
1092a5de4be0SMarek Behún 	int ret;
1093a5de4be0SMarek Behún 
1094a5de4be0SMarek Behún 	ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MV_PCS_PORT_INFO);
1095a5de4be0SMarek Behún 	if (ret < 0)
1096a5de4be0SMarek Behún 		return ret;
1097a5de4be0SMarek Behún 
1098a5de4be0SMarek Behún 	ret &= MV_PCS_PORT_INFO_NPORTS_MASK;
1099a5de4be0SMarek Behún 	ret >>= MV_PCS_PORT_INFO_NPORTS_SHIFT;
1100a5de4be0SMarek Behún 
1101a5de4be0SMarek Behún 	return ret + 1;
1102a5de4be0SMarek Behún }
1103a5de4be0SMarek Behún 
1104a5de4be0SMarek Behún static int mv3310_match_phy_device(struct phy_device *phydev)
1105a5de4be0SMarek Behún {
11060d55649dSVladimir Oltean 	if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
11070d55649dSVladimir Oltean 	     MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88X3310)
11080d55649dSVladimir Oltean 		return 0;
11090d55649dSVladimir Oltean 
1110a5de4be0SMarek Behún 	return mv3310_get_number_of_ports(phydev) == 1;
1111a5de4be0SMarek Behún }
1112a5de4be0SMarek Behún 
1113a5de4be0SMarek Behún static int mv3340_match_phy_device(struct phy_device *phydev)
1114a5de4be0SMarek Behún {
11150d55649dSVladimir Oltean 	if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
11160d55649dSVladimir Oltean 	     MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88X3310)
11170d55649dSVladimir Oltean 		return 0;
11180d55649dSVladimir Oltean 
1119a5de4be0SMarek Behún 	return mv3310_get_number_of_ports(phydev) == 4;
1120a5de4be0SMarek Behún }
1121a5de4be0SMarek Behún 
11220fca947cSMarek Behún static int mv211x_match_phy_device(struct phy_device *phydev, bool has_5g)
11230fca947cSMarek Behún {
11240fca947cSMarek Behún 	int val;
11250fca947cSMarek Behún 
11260fca947cSMarek Behún 	if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
11270fca947cSMarek Behún 	     MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88E2110)
11280fca947cSMarek Behún 		return 0;
11290fca947cSMarek Behún 
11300fca947cSMarek Behún 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_SPEED);
11310fca947cSMarek Behún 	if (val < 0)
11320fca947cSMarek Behún 		return val;
11330fca947cSMarek Behún 
11340fca947cSMarek Behún 	return !!(val & MDIO_PCS_SPEED_5G) == has_5g;
11350fca947cSMarek Behún }
11360fca947cSMarek Behún 
11370fca947cSMarek Behún static int mv2110_match_phy_device(struct phy_device *phydev)
11380fca947cSMarek Behún {
11390fca947cSMarek Behún 	return mv211x_match_phy_device(phydev, true);
11400fca947cSMarek Behún }
11410fca947cSMarek Behún 
11420fca947cSMarek Behún static int mv2111_match_phy_device(struct phy_device *phydev)
11430fca947cSMarek Behún {
11440fca947cSMarek Behún 	return mv211x_match_phy_device(phydev, false);
11450fca947cSMarek Behún }
11460fca947cSMarek Behún 
114708041a9aSVoon Weifeng static void mv3110_get_wol(struct phy_device *phydev,
114808041a9aSVoon Weifeng 			   struct ethtool_wolinfo *wol)
114908041a9aSVoon Weifeng {
115008041a9aSVoon Weifeng 	int ret;
115108041a9aSVoon Weifeng 
115208041a9aSVoon Weifeng 	wol->supported = WAKE_MAGIC;
115308041a9aSVoon Weifeng 	wol->wolopts = 0;
115408041a9aSVoon Weifeng 
115508041a9aSVoon Weifeng 	ret = phy_read_mmd(phydev, MDIO_MMD_VEND2, MV_V2_WOL_CTRL);
115608041a9aSVoon Weifeng 	if (ret < 0)
115708041a9aSVoon Weifeng 		return;
115808041a9aSVoon Weifeng 
115908041a9aSVoon Weifeng 	if (ret & MV_V2_WOL_CTRL_MAGIC_PKT_EN)
116008041a9aSVoon Weifeng 		wol->wolopts |= WAKE_MAGIC;
116108041a9aSVoon Weifeng }
116208041a9aSVoon Weifeng 
116308041a9aSVoon Weifeng static int mv3110_set_wol(struct phy_device *phydev,
116408041a9aSVoon Weifeng 			  struct ethtool_wolinfo *wol)
116508041a9aSVoon Weifeng {
116608041a9aSVoon Weifeng 	int ret;
116708041a9aSVoon Weifeng 
116808041a9aSVoon Weifeng 	if (wol->wolopts & WAKE_MAGIC) {
116908041a9aSVoon Weifeng 		/* Enable the WOL interrupt */
117008041a9aSVoon Weifeng 		ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2,
117108041a9aSVoon Weifeng 				       MV_V2_PORT_INTR_MASK,
117208041a9aSVoon Weifeng 				       MV_V2_PORT_INTR_STS_WOL_EN);
117308041a9aSVoon Weifeng 		if (ret < 0)
117408041a9aSVoon Weifeng 			return ret;
117508041a9aSVoon Weifeng 
117608041a9aSVoon Weifeng 		/* Store the device address for the magic packet */
117708041a9aSVoon Weifeng 		ret = phy_write_mmd(phydev, MDIO_MMD_VEND2,
117808041a9aSVoon Weifeng 				    MV_V2_MAGIC_PKT_WORD2,
117908041a9aSVoon Weifeng 				    ((phydev->attached_dev->dev_addr[5] << 8) |
118008041a9aSVoon Weifeng 				    phydev->attached_dev->dev_addr[4]));
118108041a9aSVoon Weifeng 		if (ret < 0)
118208041a9aSVoon Weifeng 			return ret;
118308041a9aSVoon Weifeng 
118408041a9aSVoon Weifeng 		ret = phy_write_mmd(phydev, MDIO_MMD_VEND2,
118508041a9aSVoon Weifeng 				    MV_V2_MAGIC_PKT_WORD1,
118608041a9aSVoon Weifeng 				    ((phydev->attached_dev->dev_addr[3] << 8) |
118708041a9aSVoon Weifeng 				    phydev->attached_dev->dev_addr[2]));
118808041a9aSVoon Weifeng 		if (ret < 0)
118908041a9aSVoon Weifeng 			return ret;
119008041a9aSVoon Weifeng 
119108041a9aSVoon Weifeng 		ret = phy_write_mmd(phydev, MDIO_MMD_VEND2,
119208041a9aSVoon Weifeng 				    MV_V2_MAGIC_PKT_WORD0,
119308041a9aSVoon Weifeng 				    ((phydev->attached_dev->dev_addr[1] << 8) |
119408041a9aSVoon Weifeng 				    phydev->attached_dev->dev_addr[0]));
119508041a9aSVoon Weifeng 		if (ret < 0)
119608041a9aSVoon Weifeng 			return ret;
119708041a9aSVoon Weifeng 
119808041a9aSVoon Weifeng 		/* Clear WOL status and enable magic packet matching */
119908041a9aSVoon Weifeng 		ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2,
120008041a9aSVoon Weifeng 				       MV_V2_WOL_CTRL,
120108041a9aSVoon Weifeng 				       MV_V2_WOL_CTRL_MAGIC_PKT_EN |
120208041a9aSVoon Weifeng 				       MV_V2_WOL_CTRL_CLEAR_STS);
120308041a9aSVoon Weifeng 		if (ret < 0)
120408041a9aSVoon Weifeng 			return ret;
120508041a9aSVoon Weifeng 	} else {
120608041a9aSVoon Weifeng 		/* Disable magic packet matching & reset WOL status bit */
120708041a9aSVoon Weifeng 		ret = phy_modify_mmd(phydev, MDIO_MMD_VEND2,
120808041a9aSVoon Weifeng 				     MV_V2_WOL_CTRL,
120908041a9aSVoon Weifeng 				     MV_V2_WOL_CTRL_MAGIC_PKT_EN,
121008041a9aSVoon Weifeng 				     MV_V2_WOL_CTRL_CLEAR_STS);
121108041a9aSVoon Weifeng 		if (ret < 0)
121208041a9aSVoon Weifeng 			return ret;
121308041a9aSVoon Weifeng 	}
121408041a9aSVoon Weifeng 
121508041a9aSVoon Weifeng 	/* Reset the clear WOL status bit as it does not self-clear */
121608041a9aSVoon Weifeng 	return phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2,
121708041a9aSVoon Weifeng 				  MV_V2_WOL_CTRL,
121808041a9aSVoon Weifeng 				  MV_V2_WOL_CTRL_CLEAR_STS);
121908041a9aSVoon Weifeng }
122008041a9aSVoon Weifeng 
122120b2af32SRussell King static struct phy_driver mv3310_drivers[] = {
122220b2af32SRussell King 	{
1223631ba906SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88X3310,
1224a5de4be0SMarek Behún 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
1225a5de4be0SMarek Behún 		.match_phy_device = mv3310_match_phy_device,
122620b2af32SRussell King 		.name		= "mv88x3310",
122797bbe3bdSMarek Behún 		.driver_data	= &mv3310_type,
122874145424SMaxime Chevallier 		.get_features	= mv3310_get_features,
122920b2af32SRussell King 		.config_init	= mv3310_config_init,
12300d3ad854SRussell King 		.probe		= mv3310_probe,
12310d3ad854SRussell King 		.suspend	= mv3310_suspend,
12320d3ad854SRussell King 		.resume		= mv3310_resume,
123320b2af32SRussell King 		.config_aneg	= mv3310_config_aneg,
123420b2af32SRussell King 		.aneg_done	= mv3310_aneg_done,
123520b2af32SRussell King 		.read_status	= mv3310_read_status,
1236a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
1237a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
12381b8ef142SMarek Behún 		.remove		= mv3310_remove,
1239d137c70dSWong Vee Khee 		.set_loopback	= genphy_c45_loopback,
124008041a9aSVoon Weifeng 		.get_wol	= mv3110_get_wol,
124108041a9aSVoon Weifeng 		.set_wol	= mv3110_set_wol,
124220b2af32SRussell King 	},
124362d01535SMaxime Chevallier 	{
1244a5de4be0SMarek Behún 		.phy_id		= MARVELL_PHY_ID_88X3310,
1245a5de4be0SMarek Behún 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
1246a5de4be0SMarek Behún 		.match_phy_device = mv3340_match_phy_device,
12479885d016SMarek Behún 		.name		= "mv88x3340",
12489885d016SMarek Behún 		.driver_data	= &mv3340_type,
12499885d016SMarek Behún 		.get_features	= mv3310_get_features,
12509885d016SMarek Behún 		.config_init	= mv3310_config_init,
12519885d016SMarek Behún 		.probe		= mv3310_probe,
12529885d016SMarek Behún 		.suspend	= mv3310_suspend,
12539885d016SMarek Behún 		.resume		= mv3310_resume,
12549885d016SMarek Behún 		.config_aneg	= mv3310_config_aneg,
12559885d016SMarek Behún 		.aneg_done	= mv3310_aneg_done,
12569885d016SMarek Behún 		.read_status	= mv3310_read_status,
12579885d016SMarek Behún 		.get_tunable	= mv3310_get_tunable,
12589885d016SMarek Behún 		.set_tunable	= mv3310_set_tunable,
12599885d016SMarek Behún 		.remove		= mv3310_remove,
12609885d016SMarek Behún 		.set_loopback	= genphy_c45_loopback,
12619885d016SMarek Behún 	},
12629885d016SMarek Behún 	{
126362d01535SMaxime Chevallier 		.phy_id		= MARVELL_PHY_ID_88E2110,
126462d01535SMaxime Chevallier 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
12650fca947cSMarek Behún 		.match_phy_device = mv2110_match_phy_device,
1266c89f27d4SMarek Behún 		.name		= "mv88e2110",
126797bbe3bdSMarek Behún 		.driver_data	= &mv2110_type,
126862d01535SMaxime Chevallier 		.probe		= mv3310_probe,
1269e02c4a9dSAntoine Tenart 		.suspend	= mv3310_suspend,
1270e02c4a9dSAntoine Tenart 		.resume		= mv3310_resume,
127162d01535SMaxime Chevallier 		.config_init	= mv3310_config_init,
127262d01535SMaxime Chevallier 		.config_aneg	= mv3310_config_aneg,
127362d01535SMaxime Chevallier 		.aneg_done	= mv3310_aneg_done,
127462d01535SMaxime Chevallier 		.read_status	= mv3310_read_status,
1275a585c03eSRussell King 		.get_tunable	= mv3310_get_tunable,
1276a585c03eSRussell King 		.set_tunable	= mv3310_set_tunable,
12771b8ef142SMarek Behún 		.remove		= mv3310_remove,
1278d137c70dSWong Vee Khee 		.set_loopback	= genphy_c45_loopback,
127908041a9aSVoon Weifeng 		.get_wol	= mv3110_get_wol,
128008041a9aSVoon Weifeng 		.set_wol	= mv3110_set_wol,
128162d01535SMaxime Chevallier 	},
12820fca947cSMarek Behún 	{
12830fca947cSMarek Behún 		.phy_id		= MARVELL_PHY_ID_88E2110,
12840fca947cSMarek Behún 		.phy_id_mask	= MARVELL_PHY_ID_MASK,
12850fca947cSMarek Behún 		.match_phy_device = mv2111_match_phy_device,
12860fca947cSMarek Behún 		.name		= "mv88e2111",
12870fca947cSMarek Behún 		.driver_data	= &mv2111_type,
12880fca947cSMarek Behún 		.probe		= mv3310_probe,
12890fca947cSMarek Behún 		.suspend	= mv3310_suspend,
12900fca947cSMarek Behún 		.resume		= mv3310_resume,
12910fca947cSMarek Behún 		.config_init	= mv3310_config_init,
12920fca947cSMarek Behún 		.config_aneg	= mv3310_config_aneg,
12930fca947cSMarek Behún 		.aneg_done	= mv3310_aneg_done,
12940fca947cSMarek Behún 		.read_status	= mv3310_read_status,
12950fca947cSMarek Behún 		.get_tunable	= mv3310_get_tunable,
12960fca947cSMarek Behún 		.set_tunable	= mv3310_set_tunable,
12970fca947cSMarek Behún 		.remove		= mv3310_remove,
12980fca947cSMarek Behún 		.set_loopback	= genphy_c45_loopback,
12990fca947cSMarek Behún 	},
130020b2af32SRussell King };
130120b2af32SRussell King 
130220b2af32SRussell King module_phy_driver(mv3310_drivers);
130320b2af32SRussell King 
130420b2af32SRussell King static struct mdio_device_id __maybe_unused mv3310_tbl[] = {
1305a5de4be0SMarek Behún 	{ MARVELL_PHY_ID_88X3310, MARVELL_PHY_ID_MASK },
130662d01535SMaxime Chevallier 	{ MARVELL_PHY_ID_88E2110, MARVELL_PHY_ID_MASK },
130720b2af32SRussell King 	{ },
130820b2af32SRussell King };
130920b2af32SRussell King MODULE_DEVICE_TABLE(mdio, mv3310_tbl);
1310c7dce05eSMarek Behún MODULE_DESCRIPTION("Marvell Alaska X/M multi-gigabit Ethernet PHY driver");
131120b2af32SRussell King MODULE_LICENSE("GPL");
1312