xref: /openbmc/linux/drivers/net/phy/at803x.c (revision 9d4dae29)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
20ca7111aSMatus Ujhelyi /*
30ca7111aSMatus Ujhelyi  * drivers/net/phy/at803x.c
40ca7111aSMatus Ujhelyi  *
596c36712SMichael Walle  * Driver for Qualcomm Atheros AR803x PHY
60ca7111aSMatus Ujhelyi  *
70ca7111aSMatus Ujhelyi  * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
80ca7111aSMatus Ujhelyi  */
90ca7111aSMatus Ujhelyi 
100ca7111aSMatus Ujhelyi #include <linux/phy.h>
110ca7111aSMatus Ujhelyi #include <linux/module.h>
120ca7111aSMatus Ujhelyi #include <linux/string.h>
130ca7111aSMatus Ujhelyi #include <linux/netdevice.h>
140ca7111aSMatus Ujhelyi #include <linux/etherdevice.h>
156cb75767SMichael Walle #include <linux/ethtool_netlink.h>
1613a56b44SDaniel Mack #include <linux/of_gpio.h>
172f664823SMichael Walle #include <linux/bitfield.h>
1813a56b44SDaniel Mack #include <linux/gpio/consumer.h>
192f664823SMichael Walle #include <linux/regulator/of_regulator.h>
202f664823SMichael Walle #include <linux/regulator/driver.h>
212f664823SMichael Walle #include <linux/regulator/consumer.h>
222f664823SMichael Walle #include <dt-bindings/net/qca-ar803x.h>
230ca7111aSMatus Ujhelyi 
247dce80c2SOleksij Rempel #define AT803X_SPECIFIC_FUNCTION_CONTROL	0x10
257dce80c2SOleksij Rempel #define AT803X_SFC_ASSERT_CRS			BIT(11)
267dce80c2SOleksij Rempel #define AT803X_SFC_FORCE_LINK			BIT(10)
277dce80c2SOleksij Rempel #define AT803X_SFC_MDI_CROSSOVER_MODE_M		GENMASK(6, 5)
287dce80c2SOleksij Rempel #define AT803X_SFC_AUTOMATIC_CROSSOVER		0x3
297dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDIX			0x1
307dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDI			0x0
317dce80c2SOleksij Rempel #define AT803X_SFC_SQE_TEST			BIT(2)
327dce80c2SOleksij Rempel #define AT803X_SFC_POLARITY_REVERSAL		BIT(1)
337dce80c2SOleksij Rempel #define AT803X_SFC_DISABLE_JABBER		BIT(0)
347dce80c2SOleksij Rempel 
3506d5f344SRussell King #define AT803X_SPECIFIC_STATUS			0x11
369540cddaSLuo Jie #define AT803X_SS_SPEED_MASK			GENMASK(15, 14)
379540cddaSLuo Jie #define AT803X_SS_SPEED_1000			2
389540cddaSLuo Jie #define AT803X_SS_SPEED_100			1
399540cddaSLuo Jie #define AT803X_SS_SPEED_10			0
4006d5f344SRussell King #define AT803X_SS_DUPLEX			BIT(13)
4106d5f344SRussell King #define AT803X_SS_SPEED_DUPLEX_RESOLVED		BIT(11)
4206d5f344SRussell King #define AT803X_SS_MDIX				BIT(6)
4306d5f344SRussell King 
4479c7bc05SLuo Jie #define QCA808X_SS_SPEED_MASK			GENMASK(9, 7)
4579c7bc05SLuo Jie #define QCA808X_SS_SPEED_2500			4
4679c7bc05SLuo Jie 
470ca7111aSMatus Ujhelyi #define AT803X_INTR_ENABLE			0x12
48e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_AUTONEG_ERR		BIT(15)
49e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_SPEED_CHANGED	BIT(14)
50e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_DUPLEX_CHANGED	BIT(13)
51e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_PAGE_RECEIVED	BIT(12)
52e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_FAIL		BIT(11)
53e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_SUCCESS		BIT(10)
54e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE	BIT(5)
55e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_POLARITY_CHANGED	BIT(1)
56e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WOL			BIT(0)
57e6e4a556SMartin Blumenstingl 
580ca7111aSMatus Ujhelyi #define AT803X_INTR_STATUS			0x13
59a46bd63bSMartin Blumenstingl 
6013a56b44SDaniel Mack #define AT803X_SMART_SPEED			0x14
61cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_ENABLE		BIT(5)
62cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_RETRY_LIMIT_MASK	GENMASK(4, 2)
63cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_BYPASS_TIMER		BIT(1)
646cb75767SMichael Walle #define AT803X_CDT				0x16
656cb75767SMichael Walle #define AT803X_CDT_MDI_PAIR_MASK		GENMASK(9, 8)
666cb75767SMichael Walle #define AT803X_CDT_ENABLE_TEST			BIT(0)
676cb75767SMichael Walle #define AT803X_CDT_STATUS			0x1c
686cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_NORMAL		0
696cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_SHORT		1
706cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_OPEN		2
716cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_FAIL		3
726cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_MASK		GENMASK(9, 8)
736cb75767SMichael Walle #define AT803X_CDT_STATUS_DELTA_TIME_MASK	GENMASK(7, 0)
7413a56b44SDaniel Mack #define AT803X_LED_CONTROL			0x18
75a46bd63bSMartin Blumenstingl 
767beecaf7SLuo Jie #define AT803X_PHY_MMD3_WOL_CTRL		0x8012
777beecaf7SLuo Jie #define AT803X_WOL_EN				BIT(5)
780ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_0_15_OFFSET		0x804C
790ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_16_31_OFFSET	0x804B
800ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_32_47_OFFSET	0x804A
81f62265b5SZefir Kurtisi #define AT803X_REG_CHIP_CONFIG			0x1f
82f62265b5SZefir Kurtisi #define AT803X_BT_BX_REG_SEL			0x8000
83a46bd63bSMartin Blumenstingl 
841ca6d1b1SMugunthan V N #define AT803X_DEBUG_ADDR			0x1D
851ca6d1b1SMugunthan V N #define AT803X_DEBUG_DATA			0x1E
86a46bd63bSMartin Blumenstingl 
87f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_MASK			0x0F
88f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_SGMII			0x01
89f62265b5SZefir Kurtisi 
90f62265b5SZefir Kurtisi #define AT803X_PSSR				0x11	/*PHY-Specific Status Register*/
91f62265b5SZefir Kurtisi #define AT803X_PSSR_MR_AN_COMPLETE		0x0200
92f62265b5SZefir Kurtisi 
9367999555SAnsuel Smith #define AT803X_DEBUG_ANALOG_TEST_CTRL		0x00
941ca83119SAnsuel Smith #define QCA8327_DEBUG_MANU_CTRL_EN		BIT(2)
951ca83119SAnsuel Smith #define QCA8337_DEBUG_MANU_CTRL_EN		GENMASK(3, 2)
962e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_RX_CLK_DLY_EN		BIT(15)
97a46bd63bSMartin Blumenstingl 
9867999555SAnsuel Smith #define AT803X_DEBUG_SYSTEM_CTRL_MODE		0x05
992e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_TX_CLK_DLY_EN		BIT(8)
1000ca7111aSMatus Ujhelyi 
101ba3c01eeSAnsuel Smith #define AT803X_DEBUG_REG_HIB_CTRL		0x0b
102ba3c01eeSAnsuel Smith #define   AT803X_DEBUG_HIB_CTRL_SEL_RST_80U	BIT(10)
103ba3c01eeSAnsuel Smith #define   AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE	BIT(13)
104ba3c01eeSAnsuel Smith 
105272833b9SAnsuel Smith #define AT803X_DEBUG_REG_3C			0x3C
106272833b9SAnsuel Smith 
10767999555SAnsuel Smith #define AT803X_DEBUG_REG_GREEN			0x3D
108ba3c01eeSAnsuel Smith #define   AT803X_DEBUG_GATE_CLK_IN1000		BIT(6)
109272833b9SAnsuel Smith 
1102f664823SMichael Walle #define AT803X_DEBUG_REG_1F			0x1F
1112f664823SMichael Walle #define AT803X_DEBUG_PLL_ON			BIT(2)
1122f664823SMichael Walle #define AT803X_DEBUG_RGMII_1V8			BIT(3)
1132f664823SMichael Walle 
114272833b9SAnsuel Smith #define MDIO_AZ_DEBUG				0x800D
115272833b9SAnsuel Smith 
1162f664823SMichael Walle /* AT803x supports either the XTAL input pad, an internal PLL or the
1172f664823SMichael Walle  * DSP as clock reference for the clock output pad. The XTAL reference
1182f664823SMichael Walle  * is only used for 25 MHz output, all other frequencies need the PLL.
1192f664823SMichael Walle  * The DSP as a clock reference is used in synchronous ethernet
1202f664823SMichael Walle  * applications.
1212f664823SMichael Walle  *
1222f664823SMichael Walle  * By default the PLL is only enabled if there is a link. Otherwise
1232f664823SMichael Walle  * the PHY will go into low power state and disabled the PLL. You can
1242f664823SMichael Walle  * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
1252f664823SMichael Walle  * enabled.
1262f664823SMichael Walle  */
1272f664823SMichael Walle #define AT803X_MMD7_CLK25M			0x8016
1282f664823SMichael Walle #define AT803X_CLK_OUT_MASK			GENMASK(4, 2)
1292f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_XTAL		0
1302f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_DSP		1
1312f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_PLL		2
1322f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_DSP		3
1332f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_PLL		4
1342f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_DSP		5
1352f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_PLL		6
1362f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_DSP		7
1372f664823SMichael Walle 
138428061f7SMichael Walle /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
139428061f7SMichael Walle  * but doesn't support choosing between XTAL/PLL and DSP.
1402f664823SMichael Walle  */
1412f664823SMichael Walle #define AT8035_CLK_OUT_MASK			GENMASK(4, 3)
1422f664823SMichael Walle 
1432f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_MASK		GENMASK(8, 7)
1442f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_FULL		0
1452f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_HALF		1
1462f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_QUARTER		2
1472f664823SMichael Walle 
148cde0f4f8SMichael Walle #define AT803X_DEFAULT_DOWNSHIFT		5
149cde0f4f8SMichael Walle #define AT803X_MIN_DOWNSHIFT			2
150cde0f4f8SMichael Walle #define AT803X_MAX_DOWNSHIFT			9
151cde0f4f8SMichael Walle 
152390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL1		0x805b
153390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL2		0x805c
154390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3		0x805d
155390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN	BIT(8)
156390b4cadSRussell King 
1577908d2ceSOleksij Rempel #define ATH9331_PHY_ID				0x004dd041
158bd8ca17fSDaniel Mack #define ATH8030_PHY_ID				0x004dd076
159bd8ca17fSDaniel Mack #define ATH8031_PHY_ID				0x004dd074
1605800091aSDavid Bauer #define ATH8032_PHY_ID				0x004dd023
161bd8ca17fSDaniel Mack #define ATH8035_PHY_ID				0x004dd072
1620465d8f8SMichael Walle #define AT8030_PHY_ID_MASK			0xffffffef
163bd8ca17fSDaniel Mack 
164daf61732SLuo Jie #define QCA8081_PHY_ID				0x004dd101
165daf61732SLuo Jie 
166b4df02b5SAnsuel Smith #define QCA8327_A_PHY_ID			0x004dd033
167b4df02b5SAnsuel Smith #define QCA8327_B_PHY_ID			0x004dd034
168272833b9SAnsuel Smith #define QCA8337_PHY_ID				0x004dd036
169fada2ce0SDavid Bauer #define QCA9561_PHY_ID				0x004dd042
170272833b9SAnsuel Smith #define QCA8K_PHY_ID_MASK			0xffffffff
171272833b9SAnsuel Smith 
172272833b9SAnsuel Smith #define QCA8K_DEVFLAGS_REVISION_MASK		GENMASK(2, 0)
173272833b9SAnsuel Smith 
174c329e5afSDavid Bauer #define AT803X_PAGE_FIBER			0
175c329e5afSDavid Bauer #define AT803X_PAGE_COPPER			1
176c329e5afSDavid Bauer 
177d0e13fd5SAnsuel Smith /* don't turn off internal PLL */
178d0e13fd5SAnsuel Smith #define AT803X_KEEP_PLL_ENABLED			BIT(0)
179d0e13fd5SAnsuel Smith #define AT803X_DISABLE_SMARTEEE			BIT(1)
180d0e13fd5SAnsuel Smith 
1812acdd43fSLuo Jie /* ADC threshold */
1822acdd43fSLuo Jie #define QCA808X_PHY_DEBUG_ADC_THRESHOLD		0x2c80
1832acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_MASK		GENMASK(7, 0)
1842acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_80MV		0
1852acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_100MV		0xf0
1862acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_200MV		0x0f
1872acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_300MV		0xff
1882acdd43fSLuo Jie 
1892acdd43fSLuo Jie /* CLD control */
1902acdd43fSLuo Jie #define QCA808X_PHY_MMD3_ADDR_CLD_CTRL7		0x8007
1912acdd43fSLuo Jie #define QCA808X_8023AZ_AFE_CTRL_MASK		GENMASK(8, 4)
1922acdd43fSLuo Jie #define QCA808X_8023AZ_AFE_EN			0x90
1932acdd43fSLuo Jie 
1942acdd43fSLuo Jie /* AZ control */
1952acdd43fSLuo Jie #define QCA808X_PHY_MMD3_AZ_TRAINING_CTRL	0x8008
1962acdd43fSLuo Jie #define QCA808X_MMD3_AZ_TRAINING_VAL		0x1c32
1972acdd43fSLuo Jie 
1982acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB	0x8014
1992acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_20DB_VALUE	0x529
2002acdd43fSLuo Jie 
2012acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB	0x800E
2022acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_17DB_VALUE	0x341
2032acdd43fSLuo Jie 
2042acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB	0x801E
2052acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_27DB_VALUE	0x419
2062acdd43fSLuo Jie 
2072acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB	0x8020
2082acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_28DB_VALUE	0x341
2092acdd43fSLuo Jie 
2102acdd43fSLuo Jie #define QCA808X_PHY_MMD7_TOP_OPTION1		0x901c
2112acdd43fSLuo Jie #define QCA808X_TOP_OPTION1_DATA		0x0
2122acdd43fSLuo Jie 
2132acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_1		0xa100
2142acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_1_VALUE		0x9203
2152acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_2		0xa101
2162acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_2_VALUE		0x48ad
2172acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_3		0xa103
2182acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_3_VALUE		0x1698
2192acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_4		0xa105
2202acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_4_VALUE		0x8001
2212acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_5		0xa106
2222acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_5_VALUE		0x1111
2232acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_6		0xa011
2242acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_6_VALUE		0x5f85
2252acdd43fSLuo Jie 
226*9d4dae29SLuo Jie /* master/slave seed config */
227*9d4dae29SLuo Jie #define QCA808X_PHY_DEBUG_LOCAL_SEED		9
228*9d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_ENABLE	BIT(1)
229*9d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_CFG		GENMASK(12, 2)
230*9d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_RANGE		0x32
231*9d4dae29SLuo Jie 
232daf61732SLuo Jie MODULE_DESCRIPTION("Qualcomm Atheros AR803x and QCA808X PHY driver");
2330ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi");
2340ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL");
2350ca7111aSMatus Ujhelyi 
236272833b9SAnsuel Smith enum stat_access_type {
237272833b9SAnsuel Smith 	PHY,
238272833b9SAnsuel Smith 	MMD
239272833b9SAnsuel Smith };
240272833b9SAnsuel Smith 
241272833b9SAnsuel Smith struct at803x_hw_stat {
242272833b9SAnsuel Smith 	const char *string;
243272833b9SAnsuel Smith 	u8 reg;
244272833b9SAnsuel Smith 	u32 mask;
245272833b9SAnsuel Smith 	enum stat_access_type access_type;
246272833b9SAnsuel Smith };
247272833b9SAnsuel Smith 
248272833b9SAnsuel Smith static struct at803x_hw_stat at803x_hw_stats[] = {
249272833b9SAnsuel Smith 	{ "phy_idle_errors", 0xa, GENMASK(7, 0), PHY},
250272833b9SAnsuel Smith 	{ "phy_receive_errors", 0x15, GENMASK(15, 0), PHY},
251272833b9SAnsuel Smith 	{ "eee_wake_errors", 0x16, GENMASK(15, 0), MMD},
252272833b9SAnsuel Smith };
253272833b9SAnsuel Smith 
2542f664823SMichael Walle struct at803x_priv {
2552f664823SMichael Walle 	int flags;
2562f664823SMichael Walle 	u16 clk_25m_reg;
2572f664823SMichael Walle 	u16 clk_25m_mask;
258390b4cadSRussell King 	u8 smarteee_lpi_tw_1g;
259390b4cadSRussell King 	u8 smarteee_lpi_tw_100m;
2602f664823SMichael Walle 	struct regulator_dev *vddio_rdev;
2612f664823SMichael Walle 	struct regulator_dev *vddh_rdev;
2622f664823SMichael Walle 	struct regulator *vddio;
263272833b9SAnsuel Smith 	u64 stats[ARRAY_SIZE(at803x_hw_stats)];
2642f664823SMichael Walle };
2652f664823SMichael Walle 
26613a56b44SDaniel Mack struct at803x_context {
26713a56b44SDaniel Mack 	u16 bmcr;
26813a56b44SDaniel Mack 	u16 advertise;
26913a56b44SDaniel Mack 	u16 control1000;
27013a56b44SDaniel Mack 	u16 int_enable;
27113a56b44SDaniel Mack 	u16 smart_speed;
27213a56b44SDaniel Mack 	u16 led_control;
27313a56b44SDaniel Mack };
27413a56b44SDaniel Mack 
275272833b9SAnsuel Smith static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data)
276272833b9SAnsuel Smith {
277272833b9SAnsuel Smith 	int ret;
278272833b9SAnsuel Smith 
279272833b9SAnsuel Smith 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
280272833b9SAnsuel Smith 	if (ret < 0)
281272833b9SAnsuel Smith 		return ret;
282272833b9SAnsuel Smith 
283272833b9SAnsuel Smith 	return phy_write(phydev, AT803X_DEBUG_DATA, data);
284272833b9SAnsuel Smith }
285272833b9SAnsuel Smith 
2862e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
2872e5f9f28SMartin Blumenstingl {
2882e5f9f28SMartin Blumenstingl 	int ret;
2892e5f9f28SMartin Blumenstingl 
2902e5f9f28SMartin Blumenstingl 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
2912e5f9f28SMartin Blumenstingl 	if (ret < 0)
2922e5f9f28SMartin Blumenstingl 		return ret;
2932e5f9f28SMartin Blumenstingl 
2942e5f9f28SMartin Blumenstingl 	return phy_read(phydev, AT803X_DEBUG_DATA);
2952e5f9f28SMartin Blumenstingl }
2962e5f9f28SMartin Blumenstingl 
2972e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
2982e5f9f28SMartin Blumenstingl 				 u16 clear, u16 set)
2992e5f9f28SMartin Blumenstingl {
3002e5f9f28SMartin Blumenstingl 	u16 val;
3012e5f9f28SMartin Blumenstingl 	int ret;
3022e5f9f28SMartin Blumenstingl 
3032e5f9f28SMartin Blumenstingl 	ret = at803x_debug_reg_read(phydev, reg);
3042e5f9f28SMartin Blumenstingl 	if (ret < 0)
3052e5f9f28SMartin Blumenstingl 		return ret;
3062e5f9f28SMartin Blumenstingl 
3072e5f9f28SMartin Blumenstingl 	val = ret & 0xffff;
3082e5f9f28SMartin Blumenstingl 	val &= ~clear;
3092e5f9f28SMartin Blumenstingl 	val |= set;
3102e5f9f28SMartin Blumenstingl 
3112e5f9f28SMartin Blumenstingl 	return phy_write(phydev, AT803X_DEBUG_DATA, val);
3122e5f9f28SMartin Blumenstingl }
3132e5f9f28SMartin Blumenstingl 
314c329e5afSDavid Bauer static int at803x_write_page(struct phy_device *phydev, int page)
315c329e5afSDavid Bauer {
316c329e5afSDavid Bauer 	int mask;
317c329e5afSDavid Bauer 	int set;
318c329e5afSDavid Bauer 
319c329e5afSDavid Bauer 	if (page == AT803X_PAGE_COPPER) {
320c329e5afSDavid Bauer 		set = AT803X_BT_BX_REG_SEL;
321c329e5afSDavid Bauer 		mask = 0;
322c329e5afSDavid Bauer 	} else {
323c329e5afSDavid Bauer 		set = 0;
324c329e5afSDavid Bauer 		mask = AT803X_BT_BX_REG_SEL;
325c329e5afSDavid Bauer 	}
326c329e5afSDavid Bauer 
327c329e5afSDavid Bauer 	return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
328c329e5afSDavid Bauer }
329c329e5afSDavid Bauer 
330c329e5afSDavid Bauer static int at803x_read_page(struct phy_device *phydev)
331c329e5afSDavid Bauer {
332c329e5afSDavid Bauer 	int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
333c329e5afSDavid Bauer 
334c329e5afSDavid Bauer 	if (ccr < 0)
335c329e5afSDavid Bauer 		return ccr;
336c329e5afSDavid Bauer 
337c329e5afSDavid Bauer 	if (ccr & AT803X_BT_BX_REG_SEL)
338c329e5afSDavid Bauer 		return AT803X_PAGE_COPPER;
339c329e5afSDavid Bauer 
340c329e5afSDavid Bauer 	return AT803X_PAGE_FIBER;
341c329e5afSDavid Bauer }
342c329e5afSDavid Bauer 
3436d4cd041SVinod Koul static int at803x_enable_rx_delay(struct phy_device *phydev)
3446d4cd041SVinod Koul {
34567999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0,
3466d4cd041SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN);
3476d4cd041SVinod Koul }
3486d4cd041SVinod Koul 
3496d4cd041SVinod Koul static int at803x_enable_tx_delay(struct phy_device *phydev)
3506d4cd041SVinod Koul {
35167999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0,
3526d4cd041SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN);
3536d4cd041SVinod Koul }
3546d4cd041SVinod Koul 
35543f2ebd5SVinod Koul static int at803x_disable_rx_delay(struct phy_device *phydev)
3562e5f9f28SMartin Blumenstingl {
35767999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
358cd28d1d6SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN, 0);
3592e5f9f28SMartin Blumenstingl }
3602e5f9f28SMartin Blumenstingl 
36143f2ebd5SVinod Koul static int at803x_disable_tx_delay(struct phy_device *phydev)
3622e5f9f28SMartin Blumenstingl {
36367999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE,
364cd28d1d6SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN, 0);
3652e5f9f28SMartin Blumenstingl }
3662e5f9f28SMartin Blumenstingl 
36713a56b44SDaniel Mack /* save relevant PHY registers to private copy */
36813a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev,
36913a56b44SDaniel Mack 				struct at803x_context *context)
37013a56b44SDaniel Mack {
37113a56b44SDaniel Mack 	context->bmcr = phy_read(phydev, MII_BMCR);
37213a56b44SDaniel Mack 	context->advertise = phy_read(phydev, MII_ADVERTISE);
37313a56b44SDaniel Mack 	context->control1000 = phy_read(phydev, MII_CTRL1000);
37413a56b44SDaniel Mack 	context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
37513a56b44SDaniel Mack 	context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
37613a56b44SDaniel Mack 	context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
37713a56b44SDaniel Mack }
37813a56b44SDaniel Mack 
37913a56b44SDaniel Mack /* restore relevant PHY registers from private copy */
38013a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev,
38113a56b44SDaniel Mack 				   const struct at803x_context *context)
38213a56b44SDaniel Mack {
38313a56b44SDaniel Mack 	phy_write(phydev, MII_BMCR, context->bmcr);
38413a56b44SDaniel Mack 	phy_write(phydev, MII_ADVERTISE, context->advertise);
38513a56b44SDaniel Mack 	phy_write(phydev, MII_CTRL1000, context->control1000);
38613a56b44SDaniel Mack 	phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
38713a56b44SDaniel Mack 	phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
38813a56b44SDaniel Mack 	phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
38913a56b44SDaniel Mack }
39013a56b44SDaniel Mack 
391ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev,
392ea13c9eeSMugunthan V N 			  struct ethtool_wolinfo *wol)
3930ca7111aSMatus Ujhelyi {
3940ca7111aSMatus Ujhelyi 	struct net_device *ndev = phydev->attached_dev;
3950ca7111aSMatus Ujhelyi 	const u8 *mac;
3967beecaf7SLuo Jie 	int ret, irq_enabled;
397c0f0b563SLuo Jie 	unsigned int i;
398c0f0b563SLuo Jie 	const unsigned int offsets[] = {
3990ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_32_47_OFFSET,
4000ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_16_31_OFFSET,
4010ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_0_15_OFFSET,
4020ca7111aSMatus Ujhelyi 	};
4030ca7111aSMatus Ujhelyi 
4040ca7111aSMatus Ujhelyi 	if (!ndev)
405ea13c9eeSMugunthan V N 		return -ENODEV;
4060ca7111aSMatus Ujhelyi 
407ea13c9eeSMugunthan V N 	if (wol->wolopts & WAKE_MAGIC) {
4080ca7111aSMatus Ujhelyi 		mac = (const u8 *) ndev->dev_addr;
4090ca7111aSMatus Ujhelyi 
4100ca7111aSMatus Ujhelyi 		if (!is_valid_ether_addr(mac))
411fc755687SDan Murphy 			return -EINVAL;
4120ca7111aSMatus Ujhelyi 
4130e021396SCarlo Caione 		for (i = 0; i < 3; i++)
414c0f0b563SLuo Jie 			phy_write_mmd(phydev, MDIO_MMD_PCS, offsets[i],
4150ca7111aSMatus Ujhelyi 				      mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
416ea13c9eeSMugunthan V N 
4177beecaf7SLuo Jie 		/* Enable WOL function */
4187beecaf7SLuo Jie 		ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL,
4197beecaf7SLuo Jie 				0, AT803X_WOL_EN);
4207beecaf7SLuo Jie 		if (ret)
4217beecaf7SLuo Jie 			return ret;
4227beecaf7SLuo Jie 		/* Enable WOL interrupt */
4232d4284e8SLuo Jie 		ret = phy_modify(phydev, AT803X_INTR_ENABLE, 0, AT803X_INTR_ENABLE_WOL);
424ea13c9eeSMugunthan V N 		if (ret)
425ea13c9eeSMugunthan V N 			return ret;
426ea13c9eeSMugunthan V N 	} else {
4277beecaf7SLuo Jie 		/* Disable WoL function */
4287beecaf7SLuo Jie 		ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL,
4297beecaf7SLuo Jie 				AT803X_WOL_EN, 0);
4307beecaf7SLuo Jie 		if (ret)
4317beecaf7SLuo Jie 			return ret;
4327beecaf7SLuo Jie 		/* Disable WOL interrupt */
4332d4284e8SLuo Jie 		ret = phy_modify(phydev, AT803X_INTR_ENABLE, AT803X_INTR_ENABLE_WOL, 0);
434ea13c9eeSMugunthan V N 		if (ret)
435ea13c9eeSMugunthan V N 			return ret;
436ea13c9eeSMugunthan V N 	}
437ea13c9eeSMugunthan V N 
4387beecaf7SLuo Jie 	/* Clear WOL status */
4397beecaf7SLuo Jie 	ret = phy_read(phydev, AT803X_INTR_STATUS);
4407beecaf7SLuo Jie 	if (ret < 0)
441ea13c9eeSMugunthan V N 		return ret;
4427beecaf7SLuo Jie 
4437beecaf7SLuo Jie 	/* Check if there are other interrupts except for WOL triggered when PHY is
4447beecaf7SLuo Jie 	 * in interrupt mode, only the interrupts enabled by AT803X_INTR_ENABLE can
4457beecaf7SLuo Jie 	 * be passed up to the interrupt PIN.
4467beecaf7SLuo Jie 	 */
4477beecaf7SLuo Jie 	irq_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
4487beecaf7SLuo Jie 	if (irq_enabled < 0)
4497beecaf7SLuo Jie 		return irq_enabled;
4507beecaf7SLuo Jie 
4517beecaf7SLuo Jie 	irq_enabled &= ~AT803X_INTR_ENABLE_WOL;
4527beecaf7SLuo Jie 	if (ret & irq_enabled && !phy_polling_mode(phydev))
4537beecaf7SLuo Jie 		phy_trigger_machine(phydev);
4547beecaf7SLuo Jie 
4557beecaf7SLuo Jie 	return 0;
456ea13c9eeSMugunthan V N }
457ea13c9eeSMugunthan V N 
458ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev,
459ea13c9eeSMugunthan V N 			   struct ethtool_wolinfo *wol)
460ea13c9eeSMugunthan V N {
461ea13c9eeSMugunthan V N 	u32 value;
462ea13c9eeSMugunthan V N 
463ea13c9eeSMugunthan V N 	wol->supported = WAKE_MAGIC;
464ea13c9eeSMugunthan V N 	wol->wolopts = 0;
465ea13c9eeSMugunthan V N 
4667beecaf7SLuo Jie 	value = phy_read_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL);
4677beecaf7SLuo Jie 	if (value < 0)
4687beecaf7SLuo Jie 		return;
4697beecaf7SLuo Jie 
4707beecaf7SLuo Jie 	if (value & AT803X_WOL_EN)
471ea13c9eeSMugunthan V N 		wol->wolopts |= WAKE_MAGIC;
4720ca7111aSMatus Ujhelyi }
4730ca7111aSMatus Ujhelyi 
474272833b9SAnsuel Smith static int at803x_get_sset_count(struct phy_device *phydev)
475272833b9SAnsuel Smith {
476272833b9SAnsuel Smith 	return ARRAY_SIZE(at803x_hw_stats);
477272833b9SAnsuel Smith }
478272833b9SAnsuel Smith 
479272833b9SAnsuel Smith static void at803x_get_strings(struct phy_device *phydev, u8 *data)
480272833b9SAnsuel Smith {
481272833b9SAnsuel Smith 	int i;
482272833b9SAnsuel Smith 
483272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) {
484272833b9SAnsuel Smith 		strscpy(data + i * ETH_GSTRING_LEN,
485272833b9SAnsuel Smith 			at803x_hw_stats[i].string, ETH_GSTRING_LEN);
486272833b9SAnsuel Smith 	}
487272833b9SAnsuel Smith }
488272833b9SAnsuel Smith 
489272833b9SAnsuel Smith static u64 at803x_get_stat(struct phy_device *phydev, int i)
490272833b9SAnsuel Smith {
491272833b9SAnsuel Smith 	struct at803x_hw_stat stat = at803x_hw_stats[i];
492272833b9SAnsuel Smith 	struct at803x_priv *priv = phydev->priv;
493272833b9SAnsuel Smith 	int val;
494272833b9SAnsuel Smith 	u64 ret;
495272833b9SAnsuel Smith 
496272833b9SAnsuel Smith 	if (stat.access_type == MMD)
497272833b9SAnsuel Smith 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg);
498272833b9SAnsuel Smith 	else
499272833b9SAnsuel Smith 		val = phy_read(phydev, stat.reg);
500272833b9SAnsuel Smith 
501272833b9SAnsuel Smith 	if (val < 0) {
502272833b9SAnsuel Smith 		ret = U64_MAX;
503272833b9SAnsuel Smith 	} else {
504272833b9SAnsuel Smith 		val = val & stat.mask;
505272833b9SAnsuel Smith 		priv->stats[i] += val;
506272833b9SAnsuel Smith 		ret = priv->stats[i];
507272833b9SAnsuel Smith 	}
508272833b9SAnsuel Smith 
509272833b9SAnsuel Smith 	return ret;
510272833b9SAnsuel Smith }
511272833b9SAnsuel Smith 
512272833b9SAnsuel Smith static void at803x_get_stats(struct phy_device *phydev,
513272833b9SAnsuel Smith 			     struct ethtool_stats *stats, u64 *data)
514272833b9SAnsuel Smith {
515272833b9SAnsuel Smith 	int i;
516272833b9SAnsuel Smith 
517272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++)
518272833b9SAnsuel Smith 		data[i] = at803x_get_stat(phydev, i);
519272833b9SAnsuel Smith }
520272833b9SAnsuel Smith 
5216229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev)
5226229ed1fSDaniel Mack {
5236229ed1fSDaniel Mack 	int value;
5246229ed1fSDaniel Mack 	int wol_enabled;
5256229ed1fSDaniel Mack 
5266229ed1fSDaniel Mack 	value = phy_read(phydev, AT803X_INTR_ENABLE);
527e6e4a556SMartin Blumenstingl 	wol_enabled = value & AT803X_INTR_ENABLE_WOL;
5286229ed1fSDaniel Mack 
5296229ed1fSDaniel Mack 	if (wol_enabled)
530fea23fb5SRussell King 		value = BMCR_ISOLATE;
5316229ed1fSDaniel Mack 	else
532fea23fb5SRussell King 		value = BMCR_PDOWN;
5336229ed1fSDaniel Mack 
534fea23fb5SRussell King 	phy_modify(phydev, MII_BMCR, 0, value);
5356229ed1fSDaniel Mack 
5366229ed1fSDaniel Mack 	return 0;
5376229ed1fSDaniel Mack }
5386229ed1fSDaniel Mack 
5396229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev)
5406229ed1fSDaniel Mack {
541f102852fSRussell King 	return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
5426229ed1fSDaniel Mack }
5436229ed1fSDaniel Mack 
5442f664823SMichael Walle static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
5452f664823SMichael Walle 					    unsigned int selector)
5462f664823SMichael Walle {
5472f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
5482f664823SMichael Walle 
5492f664823SMichael Walle 	if (selector)
5502f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
5512f664823SMichael Walle 					     0, AT803X_DEBUG_RGMII_1V8);
5522f664823SMichael Walle 	else
5532f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
5542f664823SMichael Walle 					     AT803X_DEBUG_RGMII_1V8, 0);
5552f664823SMichael Walle }
5562f664823SMichael Walle 
5572f664823SMichael Walle static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
5582f664823SMichael Walle {
5592f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
5602f664823SMichael Walle 	int val;
5612f664823SMichael Walle 
5622f664823SMichael Walle 	val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
5632f664823SMichael Walle 	if (val < 0)
5642f664823SMichael Walle 		return val;
5652f664823SMichael Walle 
5662f664823SMichael Walle 	return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
5672f664823SMichael Walle }
5682f664823SMichael Walle 
5693faaf539SRikard Falkeborn static const struct regulator_ops vddio_regulator_ops = {
5702f664823SMichael Walle 	.list_voltage = regulator_list_voltage_table,
5712f664823SMichael Walle 	.set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
5722f664823SMichael Walle 	.get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
5732f664823SMichael Walle };
5742f664823SMichael Walle 
5752f664823SMichael Walle static const unsigned int vddio_voltage_table[] = {
5762f664823SMichael Walle 	1500000,
5772f664823SMichael Walle 	1800000,
5782f664823SMichael Walle };
5792f664823SMichael Walle 
5802f664823SMichael Walle static const struct regulator_desc vddio_desc = {
5812f664823SMichael Walle 	.name = "vddio",
5822f664823SMichael Walle 	.of_match = of_match_ptr("vddio-regulator"),
5832f664823SMichael Walle 	.n_voltages = ARRAY_SIZE(vddio_voltage_table),
5842f664823SMichael Walle 	.volt_table = vddio_voltage_table,
5852f664823SMichael Walle 	.ops = &vddio_regulator_ops,
5862f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
5872f664823SMichael Walle 	.owner = THIS_MODULE,
5882f664823SMichael Walle };
5892f664823SMichael Walle 
5903faaf539SRikard Falkeborn static const struct regulator_ops vddh_regulator_ops = {
5912f664823SMichael Walle };
5922f664823SMichael Walle 
5932f664823SMichael Walle static const struct regulator_desc vddh_desc = {
5942f664823SMichael Walle 	.name = "vddh",
5952f664823SMichael Walle 	.of_match = of_match_ptr("vddh-regulator"),
5962f664823SMichael Walle 	.n_voltages = 1,
5972f664823SMichael Walle 	.fixed_uV = 2500000,
5982f664823SMichael Walle 	.ops = &vddh_regulator_ops,
5992f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
6002f664823SMichael Walle 	.owner = THIS_MODULE,
6012f664823SMichael Walle };
6022f664823SMichael Walle 
6032f664823SMichael Walle static int at8031_register_regulators(struct phy_device *phydev)
6042f664823SMichael Walle {
6052f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
6062f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
6072f664823SMichael Walle 	struct regulator_config config = { };
6082f664823SMichael Walle 
6092f664823SMichael Walle 	config.dev = dev;
6102f664823SMichael Walle 	config.driver_data = phydev;
6112f664823SMichael Walle 
6122f664823SMichael Walle 	priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
6132f664823SMichael Walle 	if (IS_ERR(priv->vddio_rdev)) {
6142f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDIO regulator\n");
6152f664823SMichael Walle 		return PTR_ERR(priv->vddio_rdev);
6162f664823SMichael Walle 	}
6172f664823SMichael Walle 
6182f664823SMichael Walle 	priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
6192f664823SMichael Walle 	if (IS_ERR(priv->vddh_rdev)) {
6202f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDH regulator\n");
6212f664823SMichael Walle 		return PTR_ERR(priv->vddh_rdev);
6222f664823SMichael Walle 	}
6232f664823SMichael Walle 
6242f664823SMichael Walle 	return 0;
6252f664823SMichael Walle }
6262f664823SMichael Walle 
6272f664823SMichael Walle static int at803x_parse_dt(struct phy_device *phydev)
6282f664823SMichael Walle {
6292f664823SMichael Walle 	struct device_node *node = phydev->mdio.dev.of_node;
6302f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
631390b4cadSRussell King 	u32 freq, strength, tw;
6323f2edd30SAndrew Lunn 	unsigned int sel;
6332f664823SMichael Walle 	int ret;
6342f664823SMichael Walle 
6352f664823SMichael Walle 	if (!IS_ENABLED(CONFIG_OF_MDIO))
6362f664823SMichael Walle 		return 0;
6372f664823SMichael Walle 
638390b4cadSRussell King 	if (of_property_read_bool(node, "qca,disable-smarteee"))
639390b4cadSRussell King 		priv->flags |= AT803X_DISABLE_SMARTEEE;
640390b4cadSRussell King 
641390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
642390b4cadSRussell King 		if (!tw || tw > 255) {
643390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
644390b4cadSRussell King 			return -EINVAL;
645390b4cadSRussell King 		}
646390b4cadSRussell King 		priv->smarteee_lpi_tw_1g = tw;
647390b4cadSRussell King 	}
648390b4cadSRussell King 
649390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
650390b4cadSRussell King 		if (!tw || tw > 255) {
651390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
652390b4cadSRussell King 			return -EINVAL;
653390b4cadSRussell King 		}
654390b4cadSRussell King 		priv->smarteee_lpi_tw_100m = tw;
655390b4cadSRussell King 	}
656390b4cadSRussell King 
6572f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
6582f664823SMichael Walle 	if (!ret) {
6592f664823SMichael Walle 		switch (freq) {
6602f664823SMichael Walle 		case 25000000:
6612f664823SMichael Walle 			sel = AT803X_CLK_OUT_25MHZ_XTAL;
6622f664823SMichael Walle 			break;
6632f664823SMichael Walle 		case 50000000:
6642f664823SMichael Walle 			sel = AT803X_CLK_OUT_50MHZ_PLL;
6652f664823SMichael Walle 			break;
6662f664823SMichael Walle 		case 62500000:
6672f664823SMichael Walle 			sel = AT803X_CLK_OUT_62_5MHZ_PLL;
6682f664823SMichael Walle 			break;
6692f664823SMichael Walle 		case 125000000:
6702f664823SMichael Walle 			sel = AT803X_CLK_OUT_125MHZ_PLL;
6712f664823SMichael Walle 			break;
6722f664823SMichael Walle 		default:
6732f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-frequency\n");
6742f664823SMichael Walle 			return -EINVAL;
6752f664823SMichael Walle 		}
6762f664823SMichael Walle 
6773f2edd30SAndrew Lunn 		priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
6783f2edd30SAndrew Lunn 		priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
6792f664823SMichael Walle 
6802f664823SMichael Walle 		/* Fixup for the AR8030/AR8035. This chip has another mask and
6812f664823SMichael Walle 		 * doesn't support the DSP reference. Eg. the lowest bit of the
6822f664823SMichael Walle 		 * mask. The upper two bits select the same frequencies. Mask
6832f664823SMichael Walle 		 * the lowest bit here.
6842f664823SMichael Walle 		 *
6852f664823SMichael Walle 		 * Warning:
6862f664823SMichael Walle 		 *   There was no datasheet for the AR8030 available so this is
6872f664823SMichael Walle 		 *   just a guess. But the AR8035 is listed as pin compatible
6882f664823SMichael Walle 		 *   to the AR8030 so there might be a good chance it works on
6892f664823SMichael Walle 		 *   the AR8030 too.
6902f664823SMichael Walle 		 */
6918887ca54SRussell King 		if (phydev->drv->phy_id == ATH8030_PHY_ID ||
6928887ca54SRussell King 		    phydev->drv->phy_id == ATH8035_PHY_ID) {
693b1f4c209SOleksij Rempel 			priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
694b1f4c209SOleksij Rempel 			priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
6952f664823SMichael Walle 		}
6962f664823SMichael Walle 	}
6972f664823SMichael Walle 
6982f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
6992f664823SMichael Walle 	if (!ret) {
7002f664823SMichael Walle 		priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
7012f664823SMichael Walle 		switch (strength) {
7022f664823SMichael Walle 		case AR803X_STRENGTH_FULL:
7032f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
7042f664823SMichael Walle 			break;
7052f664823SMichael Walle 		case AR803X_STRENGTH_HALF:
7062f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
7072f664823SMichael Walle 			break;
7082f664823SMichael Walle 		case AR803X_STRENGTH_QUARTER:
7092f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
7102f664823SMichael Walle 			break;
7112f664823SMichael Walle 		default:
7122f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-strength\n");
7132f664823SMichael Walle 			return -EINVAL;
7142f664823SMichael Walle 		}
7152f664823SMichael Walle 	}
7162f664823SMichael Walle 
717428061f7SMichael Walle 	/* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
718428061f7SMichael Walle 	 * options.
719428061f7SMichael Walle 	 */
7208887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
7212f664823SMichael Walle 		if (of_property_read_bool(node, "qca,keep-pll-enabled"))
7222f664823SMichael Walle 			priv->flags |= AT803X_KEEP_PLL_ENABLED;
7232f664823SMichael Walle 
7242f664823SMichael Walle 		ret = at8031_register_regulators(phydev);
7252f664823SMichael Walle 		if (ret < 0)
7262f664823SMichael Walle 			return ret;
7272f664823SMichael Walle 
7282f664823SMichael Walle 		priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev,
7292f664823SMichael Walle 							  "vddio");
7302f664823SMichael Walle 		if (IS_ERR(priv->vddio)) {
7312f664823SMichael Walle 			phydev_err(phydev, "failed to get VDDIO regulator\n");
7322f664823SMichael Walle 			return PTR_ERR(priv->vddio);
7332f664823SMichael Walle 		}
7342f664823SMichael Walle 	}
7352f664823SMichael Walle 
7362f664823SMichael Walle 	return 0;
7372f664823SMichael Walle }
7382f664823SMichael Walle 
7392f664823SMichael Walle static int at803x_probe(struct phy_device *phydev)
7402f664823SMichael Walle {
7412f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
7422f664823SMichael Walle 	struct at803x_priv *priv;
743c329e5afSDavid Bauer 	int ret;
7442f664823SMichael Walle 
7452f664823SMichael Walle 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
7462f664823SMichael Walle 	if (!priv)
7472f664823SMichael Walle 		return -ENOMEM;
7482f664823SMichael Walle 
7492f664823SMichael Walle 	phydev->priv = priv;
7502f664823SMichael Walle 
751c329e5afSDavid Bauer 	ret = at803x_parse_dt(phydev);
752c329e5afSDavid Bauer 	if (ret)
753c329e5afSDavid Bauer 		return ret;
754c329e5afSDavid Bauer 
7558f7e8762SMichael Walle 	if (priv->vddio) {
7568f7e8762SMichael Walle 		ret = regulator_enable(priv->vddio);
7578f7e8762SMichael Walle 		if (ret < 0)
7588f7e8762SMichael Walle 			return ret;
7598f7e8762SMichael Walle 	}
7608f7e8762SMichael Walle 
761c329e5afSDavid Bauer 	/* Some bootloaders leave the fiber page selected.
762c329e5afSDavid Bauer 	 * Switch to the copper page, as otherwise we read
763c329e5afSDavid Bauer 	 * the PHY capabilities from the fiber side.
764c329e5afSDavid Bauer 	 */
7658887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
7668f7e8762SMichael Walle 		phy_lock_mdio_bus(phydev);
7678f7e8762SMichael Walle 		ret = at803x_write_page(phydev, AT803X_PAGE_COPPER);
7688f7e8762SMichael Walle 		phy_unlock_mdio_bus(phydev);
7698f7e8762SMichael Walle 		if (ret)
7708f7e8762SMichael Walle 			goto err;
771c329e5afSDavid Bauer 	}
772c329e5afSDavid Bauer 
7738f7e8762SMichael Walle 	return 0;
7748f7e8762SMichael Walle 
7758f7e8762SMichael Walle err:
7768f7e8762SMichael Walle 	if (priv->vddio)
7778f7e8762SMichael Walle 		regulator_disable(priv->vddio);
7788f7e8762SMichael Walle 
779c329e5afSDavid Bauer 	return ret;
7802f664823SMichael Walle }
7812f664823SMichael Walle 
7822318ca8aSMichael Walle static void at803x_remove(struct phy_device *phydev)
7832318ca8aSMichael Walle {
7842318ca8aSMichael Walle 	struct at803x_priv *priv = phydev->priv;
7852318ca8aSMichael Walle 
7862318ca8aSMichael Walle 	if (priv->vddio)
7872318ca8aSMichael Walle 		regulator_disable(priv->vddio);
7882318ca8aSMichael Walle }
7892318ca8aSMichael Walle 
790b856150cSDavid Bauer static int at803x_get_features(struct phy_device *phydev)
791b856150cSDavid Bauer {
792b856150cSDavid Bauer 	int err;
793b856150cSDavid Bauer 
794b856150cSDavid Bauer 	err = genphy_read_abilities(phydev);
795b856150cSDavid Bauer 	if (err)
796b856150cSDavid Bauer 		return err;
797b856150cSDavid Bauer 
798765c22aaSLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID) {
799765c22aaSLuo Jie 		err = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_NG_EXTABLE);
800765c22aaSLuo Jie 		if (err < 0)
801765c22aaSLuo Jie 			return err;
802765c22aaSLuo Jie 
803765c22aaSLuo Jie 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->supported,
804765c22aaSLuo Jie 				err & MDIO_PMA_NG_EXTABLE_2_5GBT);
805765c22aaSLuo Jie 	}
806765c22aaSLuo Jie 
807f5621a01SVladimir Oltean 	if (phydev->drv->phy_id != ATH8031_PHY_ID)
808b856150cSDavid Bauer 		return 0;
809b856150cSDavid Bauer 
810b856150cSDavid Bauer 	/* AR8031/AR8033 have different status registers
811b856150cSDavid Bauer 	 * for copper and fiber operation. However, the
812b856150cSDavid Bauer 	 * extended status register is the same for both
813b856150cSDavid Bauer 	 * operation modes.
814b856150cSDavid Bauer 	 *
815b856150cSDavid Bauer 	 * As a result of that, ESTATUS_1000_XFULL is set
816b856150cSDavid Bauer 	 * to 1 even when operating in copper TP mode.
817b856150cSDavid Bauer 	 *
818b856150cSDavid Bauer 	 * Remove this mode from the supported link modes,
819b856150cSDavid Bauer 	 * as this driver currently only supports copper
820b856150cSDavid Bauer 	 * operation.
821b856150cSDavid Bauer 	 */
822b856150cSDavid Bauer 	linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
823b856150cSDavid Bauer 			   phydev->supported);
824b856150cSDavid Bauer 	return 0;
825b856150cSDavid Bauer }
826b856150cSDavid Bauer 
827390b4cadSRussell King static int at803x_smarteee_config(struct phy_device *phydev)
828390b4cadSRussell King {
829390b4cadSRussell King 	struct at803x_priv *priv = phydev->priv;
830390b4cadSRussell King 	u16 mask = 0, val = 0;
831390b4cadSRussell King 	int ret;
832390b4cadSRussell King 
833390b4cadSRussell King 	if (priv->flags & AT803X_DISABLE_SMARTEEE)
834390b4cadSRussell King 		return phy_modify_mmd(phydev, MDIO_MMD_PCS,
835390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3,
836390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
837390b4cadSRussell King 
838390b4cadSRussell King 	if (priv->smarteee_lpi_tw_1g) {
839390b4cadSRussell King 		mask |= 0xff00;
840390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_1g << 8;
841390b4cadSRussell King 	}
842390b4cadSRussell King 	if (priv->smarteee_lpi_tw_100m) {
843390b4cadSRussell King 		mask |= 0x00ff;
844390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_100m;
845390b4cadSRussell King 	}
846390b4cadSRussell King 	if (!mask)
847390b4cadSRussell King 		return 0;
848390b4cadSRussell King 
849390b4cadSRussell King 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
850390b4cadSRussell King 			     mask, val);
851390b4cadSRussell King 	if (ret)
852390b4cadSRussell King 		return ret;
853390b4cadSRussell King 
854390b4cadSRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
855390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
856390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
857390b4cadSRussell King }
858390b4cadSRussell King 
8592f664823SMichael Walle static int at803x_clk_out_config(struct phy_device *phydev)
8602f664823SMichael Walle {
8612f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
8622f664823SMichael Walle 
8632f664823SMichael Walle 	if (!priv->clk_25m_mask)
8642f664823SMichael Walle 		return 0;
8652f664823SMichael Walle 
866a45c1c10SRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
867a45c1c10SRussell King 			      priv->clk_25m_mask, priv->clk_25m_reg);
8682f664823SMichael Walle }
8692f664823SMichael Walle 
8702f664823SMichael Walle static int at8031_pll_config(struct phy_device *phydev)
8712f664823SMichael Walle {
8722f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
8732f664823SMichael Walle 
8742f664823SMichael Walle 	/* The default after hardware reset is PLL OFF. After a soft reset, the
8752f664823SMichael Walle 	 * values are retained.
8762f664823SMichael Walle 	 */
8772f664823SMichael Walle 	if (priv->flags & AT803X_KEEP_PLL_ENABLED)
8782f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
8792f664823SMichael Walle 					     0, AT803X_DEBUG_PLL_ON);
8802f664823SMichael Walle 	else
8812f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
8822f664823SMichael Walle 					     AT803X_DEBUG_PLL_ON, 0);
8832f664823SMichael Walle }
8842f664823SMichael Walle 
8850ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev)
8860ca7111aSMatus Ujhelyi {
8871ca6d1b1SMugunthan V N 	int ret;
8880ca7111aSMatus Ujhelyi 
8896d4cd041SVinod Koul 	/* The RX and TX delay default is:
8906d4cd041SVinod Koul 	 *   after HW reset: RX delay enabled and TX delay disabled
8916d4cd041SVinod Koul 	 *   after SW reset: RX delay enabled, while TX delay retains the
8926d4cd041SVinod Koul 	 *   value before reset.
8936d4cd041SVinod Koul 	 */
894bb0ce4c1SAndré Draszik 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
895bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
896bb0ce4c1SAndré Draszik 		ret = at803x_enable_rx_delay(phydev);
897bb0ce4c1SAndré Draszik 	else
898cd28d1d6SVinod Koul 		ret = at803x_disable_rx_delay(phydev);
8992e5f9f28SMartin Blumenstingl 	if (ret < 0)
9001ca6d1b1SMugunthan V N 		return ret;
9016d4cd041SVinod Koul 
9026d4cd041SVinod Koul 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
903bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
9046d4cd041SVinod Koul 		ret = at803x_enable_tx_delay(phydev);
905bb0ce4c1SAndré Draszik 	else
906bb0ce4c1SAndré Draszik 		ret = at803x_disable_tx_delay(phydev);
9072f664823SMichael Walle 	if (ret < 0)
9086d4cd041SVinod Koul 		return ret;
9092f664823SMichael Walle 
910390b4cadSRussell King 	ret = at803x_smarteee_config(phydev);
911390b4cadSRussell King 	if (ret < 0)
912390b4cadSRussell King 		return ret;
913390b4cadSRussell King 
9142f664823SMichael Walle 	ret = at803x_clk_out_config(phydev);
9152f664823SMichael Walle 	if (ret < 0)
9162f664823SMichael Walle 		return ret;
9172f664823SMichael Walle 
9188887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
9192f664823SMichael Walle 		ret = at8031_pll_config(phydev);
9202f664823SMichael Walle 		if (ret < 0)
9212f664823SMichael Walle 			return ret;
9222f664823SMichael Walle 	}
9232f664823SMichael Walle 
9243c51fa5dSRussell King 	/* Ar803x extended next page bit is enabled by default. Cisco
9253c51fa5dSRussell King 	 * multigig switches read this bit and attempt to negotiate 10Gbps
9263c51fa5dSRussell King 	 * rates even if the next page bit is disabled. This is incorrect
9273c51fa5dSRussell King 	 * behaviour but we still need to accommodate it. XNP is only needed
9283c51fa5dSRussell King 	 * for 10Gbps support, so disable XNP.
9293c51fa5dSRussell King 	 */
9303c51fa5dSRussell King 	return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
9310ca7111aSMatus Ujhelyi }
9320ca7111aSMatus Ujhelyi 
93377a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev)
93477a99394SZhao Qiang {
93577a99394SZhao Qiang 	int err;
93677a99394SZhao Qiang 
937a46bd63bSMartin Blumenstingl 	err = phy_read(phydev, AT803X_INTR_STATUS);
93877a99394SZhao Qiang 
93977a99394SZhao Qiang 	return (err < 0) ? err : 0;
94077a99394SZhao Qiang }
94177a99394SZhao Qiang 
94277a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev)
94377a99394SZhao Qiang {
94477a99394SZhao Qiang 	int err;
94577a99394SZhao Qiang 	int value;
94677a99394SZhao Qiang 
947a46bd63bSMartin Blumenstingl 	value = phy_read(phydev, AT803X_INTR_ENABLE);
94877a99394SZhao Qiang 
949e6e4a556SMartin Blumenstingl 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
950a3417885SIoana Ciornei 		/* Clear any pending interrupts */
951a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
952a3417885SIoana Ciornei 		if (err)
953a3417885SIoana Ciornei 			return err;
954a3417885SIoana Ciornei 
955e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
956e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
957e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
958e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_FAIL;
959e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
960e6e4a556SMartin Blumenstingl 
961e6e4a556SMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, value);
962a3417885SIoana Ciornei 	} else {
963a46bd63bSMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
964a3417885SIoana Ciornei 		if (err)
965a3417885SIoana Ciornei 			return err;
966a3417885SIoana Ciornei 
967a3417885SIoana Ciornei 		/* Clear any pending interrupts */
968a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
969a3417885SIoana Ciornei 	}
97077a99394SZhao Qiang 
97177a99394SZhao Qiang 	return err;
97277a99394SZhao Qiang }
97377a99394SZhao Qiang 
97429773097SIoana Ciornei static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev)
97529773097SIoana Ciornei {
97629773097SIoana Ciornei 	int irq_status, int_enabled;
97729773097SIoana Ciornei 
97829773097SIoana Ciornei 	irq_status = phy_read(phydev, AT803X_INTR_STATUS);
97929773097SIoana Ciornei 	if (irq_status < 0) {
98029773097SIoana Ciornei 		phy_error(phydev);
98129773097SIoana Ciornei 		return IRQ_NONE;
98229773097SIoana Ciornei 	}
98329773097SIoana Ciornei 
98429773097SIoana Ciornei 	/* Read the current enabled interrupts */
98529773097SIoana Ciornei 	int_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
98629773097SIoana Ciornei 	if (int_enabled < 0) {
98729773097SIoana Ciornei 		phy_error(phydev);
98829773097SIoana Ciornei 		return IRQ_NONE;
98929773097SIoana Ciornei 	}
99029773097SIoana Ciornei 
99129773097SIoana Ciornei 	/* See if this was one of our enabled interrupts */
99229773097SIoana Ciornei 	if (!(irq_status & int_enabled))
99329773097SIoana Ciornei 		return IRQ_NONE;
99429773097SIoana Ciornei 
99529773097SIoana Ciornei 	phy_trigger_machine(phydev);
99629773097SIoana Ciornei 
99729773097SIoana Ciornei 	return IRQ_HANDLED;
99829773097SIoana Ciornei }
99929773097SIoana Ciornei 
100013a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev)
100113a56b44SDaniel Mack {
100213a56b44SDaniel Mack 	/*
100313a56b44SDaniel Mack 	 * Conduct a hardware reset for AT8030 every time a link loss is
100413a56b44SDaniel Mack 	 * signalled. This is necessary to circumvent a hardware bug that
100513a56b44SDaniel Mack 	 * occurs when the cable is unplugged while TX packets are pending
100613a56b44SDaniel Mack 	 * in the FIFO. In such cases, the FIFO enters an error mode it
100713a56b44SDaniel Mack 	 * cannot recover from by software.
100813a56b44SDaniel Mack 	 */
10096110ed2dSDavid Bauer 	if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
101013a56b44SDaniel Mack 		struct at803x_context context;
101113a56b44SDaniel Mack 
101213a56b44SDaniel Mack 		at803x_context_save(phydev, &context);
101313a56b44SDaniel Mack 
1014bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 1);
101513a56b44SDaniel Mack 		msleep(1);
1016bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 0);
1017d57019d1SSergei Shtylyov 		msleep(1);
101813a56b44SDaniel Mack 
101913a56b44SDaniel Mack 		at803x_context_restore(phydev, &context);
102013a56b44SDaniel Mack 
10215c5f626bSHeiner Kallweit 		phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
102213a56b44SDaniel Mack 	}
102313a56b44SDaniel Mack }
102413a56b44SDaniel Mack 
102579c7bc05SLuo Jie static int at803x_read_specific_status(struct phy_device *phydev)
102606d5f344SRussell King {
102779c7bc05SLuo Jie 	int ss;
102806d5f344SRussell King 
102906d5f344SRussell King 	/* Read the AT8035 PHY-Specific Status register, which indicates the
103006d5f344SRussell King 	 * speed and duplex that the PHY is actually using, irrespective of
103106d5f344SRussell King 	 * whether we are in autoneg mode or not.
103206d5f344SRussell King 	 */
103306d5f344SRussell King 	ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
103406d5f344SRussell King 	if (ss < 0)
103506d5f344SRussell King 		return ss;
103606d5f344SRussell King 
103706d5f344SRussell King 	if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
103879c7bc05SLuo Jie 		int sfc, speed;
10397dce80c2SOleksij Rempel 
10407dce80c2SOleksij Rempel 		sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL);
10417dce80c2SOleksij Rempel 		if (sfc < 0)
10427dce80c2SOleksij Rempel 			return sfc;
10437dce80c2SOleksij Rempel 
104479c7bc05SLuo Jie 		/* qca8081 takes the different bits for speed value from at803x */
104579c7bc05SLuo Jie 		if (phydev->drv->phy_id == QCA8081_PHY_ID)
104679c7bc05SLuo Jie 			speed = FIELD_GET(QCA808X_SS_SPEED_MASK, ss);
104779c7bc05SLuo Jie 		else
104879c7bc05SLuo Jie 			speed = FIELD_GET(AT803X_SS_SPEED_MASK, ss);
104979c7bc05SLuo Jie 
105079c7bc05SLuo Jie 		switch (speed) {
105106d5f344SRussell King 		case AT803X_SS_SPEED_10:
105206d5f344SRussell King 			phydev->speed = SPEED_10;
105306d5f344SRussell King 			break;
105406d5f344SRussell King 		case AT803X_SS_SPEED_100:
105506d5f344SRussell King 			phydev->speed = SPEED_100;
105606d5f344SRussell King 			break;
105706d5f344SRussell King 		case AT803X_SS_SPEED_1000:
105806d5f344SRussell King 			phydev->speed = SPEED_1000;
105906d5f344SRussell King 			break;
106079c7bc05SLuo Jie 		case QCA808X_SS_SPEED_2500:
106179c7bc05SLuo Jie 			phydev->speed = SPEED_2500;
106279c7bc05SLuo Jie 			break;
106306d5f344SRussell King 		}
106406d5f344SRussell King 		if (ss & AT803X_SS_DUPLEX)
106506d5f344SRussell King 			phydev->duplex = DUPLEX_FULL;
106606d5f344SRussell King 		else
106706d5f344SRussell King 			phydev->duplex = DUPLEX_HALF;
10687dce80c2SOleksij Rempel 
106906d5f344SRussell King 		if (ss & AT803X_SS_MDIX)
107006d5f344SRussell King 			phydev->mdix = ETH_TP_MDI_X;
107106d5f344SRussell King 		else
107206d5f344SRussell King 			phydev->mdix = ETH_TP_MDI;
10737dce80c2SOleksij Rempel 
10747dce80c2SOleksij Rempel 		switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) {
10757dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDI:
10767dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
10777dce80c2SOleksij Rempel 			break;
10787dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDIX:
10797dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
10807dce80c2SOleksij Rempel 			break;
10817dce80c2SOleksij Rempel 		case AT803X_SFC_AUTOMATIC_CROSSOVER:
10827dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
10837dce80c2SOleksij Rempel 			break;
10847dce80c2SOleksij Rempel 		}
108506d5f344SRussell King 	}
108606d5f344SRussell King 
108779c7bc05SLuo Jie 	return 0;
108879c7bc05SLuo Jie }
108979c7bc05SLuo Jie 
109079c7bc05SLuo Jie static int at803x_read_status(struct phy_device *phydev)
109179c7bc05SLuo Jie {
109279c7bc05SLuo Jie 	int err, old_link = phydev->link;
109379c7bc05SLuo Jie 
109479c7bc05SLuo Jie 	/* Update the link, but return if there was an error */
109579c7bc05SLuo Jie 	err = genphy_update_link(phydev);
109679c7bc05SLuo Jie 	if (err)
109779c7bc05SLuo Jie 		return err;
109879c7bc05SLuo Jie 
109979c7bc05SLuo Jie 	/* why bother the PHY if nothing can have changed */
110079c7bc05SLuo Jie 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
110179c7bc05SLuo Jie 		return 0;
110279c7bc05SLuo Jie 
110379c7bc05SLuo Jie 	phydev->speed = SPEED_UNKNOWN;
110479c7bc05SLuo Jie 	phydev->duplex = DUPLEX_UNKNOWN;
110579c7bc05SLuo Jie 	phydev->pause = 0;
110679c7bc05SLuo Jie 	phydev->asym_pause = 0;
110779c7bc05SLuo Jie 
110879c7bc05SLuo Jie 	err = genphy_read_lpa(phydev);
110979c7bc05SLuo Jie 	if (err < 0)
111079c7bc05SLuo Jie 		return err;
111179c7bc05SLuo Jie 
111279c7bc05SLuo Jie 	err = at803x_read_specific_status(phydev);
111379c7bc05SLuo Jie 	if (err < 0)
111479c7bc05SLuo Jie 		return err;
111579c7bc05SLuo Jie 
111606d5f344SRussell King 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
111706d5f344SRussell King 		phy_resolve_aneg_pause(phydev);
111806d5f344SRussell King 
111906d5f344SRussell King 	return 0;
112006d5f344SRussell King }
112106d5f344SRussell King 
11227dce80c2SOleksij Rempel static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl)
11237dce80c2SOleksij Rempel {
11247dce80c2SOleksij Rempel 	u16 val;
11257dce80c2SOleksij Rempel 
11267dce80c2SOleksij Rempel 	switch (ctrl) {
11277dce80c2SOleksij Rempel 	case ETH_TP_MDI:
11287dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDI;
11297dce80c2SOleksij Rempel 		break;
11307dce80c2SOleksij Rempel 	case ETH_TP_MDI_X:
11317dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDIX;
11327dce80c2SOleksij Rempel 		break;
11337dce80c2SOleksij Rempel 	case ETH_TP_MDI_AUTO:
11347dce80c2SOleksij Rempel 		val = AT803X_SFC_AUTOMATIC_CROSSOVER;
11357dce80c2SOleksij Rempel 		break;
11367dce80c2SOleksij Rempel 	default:
11377dce80c2SOleksij Rempel 		return 0;
11387dce80c2SOleksij Rempel 	}
11397dce80c2SOleksij Rempel 
11407dce80c2SOleksij Rempel 	return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL,
11417dce80c2SOleksij Rempel 			  AT803X_SFC_MDI_CROSSOVER_MODE_M,
11427dce80c2SOleksij Rempel 			  FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val));
11437dce80c2SOleksij Rempel }
11447dce80c2SOleksij Rempel 
11457dce80c2SOleksij Rempel static int at803x_config_aneg(struct phy_device *phydev)
11467dce80c2SOleksij Rempel {
11477dce80c2SOleksij Rempel 	int ret;
11487dce80c2SOleksij Rempel 
11497dce80c2SOleksij Rempel 	ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
11507dce80c2SOleksij Rempel 	if (ret < 0)
11517dce80c2SOleksij Rempel 		return ret;
11527dce80c2SOleksij Rempel 
11537dce80c2SOleksij Rempel 	/* Changes of the midx bits are disruptive to the normal operation;
11547dce80c2SOleksij Rempel 	 * therefore any changes to these registers must be followed by a
11557dce80c2SOleksij Rempel 	 * software reset to take effect.
11567dce80c2SOleksij Rempel 	 */
11577dce80c2SOleksij Rempel 	if (ret == 1) {
11587dce80c2SOleksij Rempel 		ret = genphy_soft_reset(phydev);
11597dce80c2SOleksij Rempel 		if (ret < 0)
11607dce80c2SOleksij Rempel 			return ret;
11617dce80c2SOleksij Rempel 	}
11627dce80c2SOleksij Rempel 
1163f884d449SLuo Jie 	/* Do not restart auto-negotiation by setting ret to 0 defautly,
1164f884d449SLuo Jie 	 * when calling __genphy_config_aneg later.
1165f884d449SLuo Jie 	 */
1166f884d449SLuo Jie 	ret = 0;
1167f884d449SLuo Jie 
1168f884d449SLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID) {
1169f884d449SLuo Jie 		int phy_ctrl = 0;
1170f884d449SLuo Jie 
1171f884d449SLuo Jie 		/* The reg MII_BMCR also needs to be configured for force mode, the
1172f884d449SLuo Jie 		 * genphy_config_aneg is also needed.
1173f884d449SLuo Jie 		 */
1174f884d449SLuo Jie 		if (phydev->autoneg == AUTONEG_DISABLE)
1175f884d449SLuo Jie 			genphy_c45_pma_setup_forced(phydev);
1176f884d449SLuo Jie 
1177f884d449SLuo Jie 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->advertising))
1178f884d449SLuo Jie 			phy_ctrl = MDIO_AN_10GBT_CTRL_ADV2_5G;
1179f884d449SLuo Jie 
1180f884d449SLuo Jie 		ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
1181f884d449SLuo Jie 				MDIO_AN_10GBT_CTRL_ADV2_5G, phy_ctrl);
1182f884d449SLuo Jie 		if (ret < 0)
1183f884d449SLuo Jie 			return ret;
1184f884d449SLuo Jie 	}
1185f884d449SLuo Jie 
1186f884d449SLuo Jie 	return __genphy_config_aneg(phydev, ret);
11877dce80c2SOleksij Rempel }
11887dce80c2SOleksij Rempel 
1189cde0f4f8SMichael Walle static int at803x_get_downshift(struct phy_device *phydev, u8 *d)
1190cde0f4f8SMichael Walle {
1191cde0f4f8SMichael Walle 	int val;
1192cde0f4f8SMichael Walle 
1193cde0f4f8SMichael Walle 	val = phy_read(phydev, AT803X_SMART_SPEED);
1194cde0f4f8SMichael Walle 	if (val < 0)
1195cde0f4f8SMichael Walle 		return val;
1196cde0f4f8SMichael Walle 
1197cde0f4f8SMichael Walle 	if (val & AT803X_SMART_SPEED_ENABLE)
1198cde0f4f8SMichael Walle 		*d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2;
1199cde0f4f8SMichael Walle 	else
1200cde0f4f8SMichael Walle 		*d = DOWNSHIFT_DEV_DISABLE;
1201cde0f4f8SMichael Walle 
1202cde0f4f8SMichael Walle 	return 0;
1203cde0f4f8SMichael Walle }
1204cde0f4f8SMichael Walle 
1205cde0f4f8SMichael Walle static int at803x_set_downshift(struct phy_device *phydev, u8 cnt)
1206cde0f4f8SMichael Walle {
1207cde0f4f8SMichael Walle 	u16 mask, set;
1208cde0f4f8SMichael Walle 	int ret;
1209cde0f4f8SMichael Walle 
1210cde0f4f8SMichael Walle 	switch (cnt) {
1211cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DEFAULT_COUNT:
1212cde0f4f8SMichael Walle 		cnt = AT803X_DEFAULT_DOWNSHIFT;
1213cde0f4f8SMichael Walle 		fallthrough;
1214cde0f4f8SMichael Walle 	case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT:
1215cde0f4f8SMichael Walle 		set = AT803X_SMART_SPEED_ENABLE |
1216cde0f4f8SMichael Walle 		      AT803X_SMART_SPEED_BYPASS_TIMER |
1217cde0f4f8SMichael Walle 		      FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2);
1218cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK;
1219cde0f4f8SMichael Walle 		break;
1220cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DISABLE:
1221cde0f4f8SMichael Walle 		set = 0;
1222cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_ENABLE |
1223cde0f4f8SMichael Walle 		       AT803X_SMART_SPEED_BYPASS_TIMER;
1224cde0f4f8SMichael Walle 		break;
1225cde0f4f8SMichael Walle 	default:
1226cde0f4f8SMichael Walle 		return -EINVAL;
1227cde0f4f8SMichael Walle 	}
1228cde0f4f8SMichael Walle 
1229cde0f4f8SMichael Walle 	ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set);
1230cde0f4f8SMichael Walle 
1231cde0f4f8SMichael Walle 	/* After changing the smart speed settings, we need to perform a
1232cde0f4f8SMichael Walle 	 * software reset, use phy_init_hw() to make sure we set the
1233cde0f4f8SMichael Walle 	 * reapply any values which might got lost during software reset.
1234cde0f4f8SMichael Walle 	 */
1235cde0f4f8SMichael Walle 	if (ret == 1)
1236cde0f4f8SMichael Walle 		ret = phy_init_hw(phydev);
1237cde0f4f8SMichael Walle 
1238cde0f4f8SMichael Walle 	return ret;
1239cde0f4f8SMichael Walle }
1240cde0f4f8SMichael Walle 
1241cde0f4f8SMichael Walle static int at803x_get_tunable(struct phy_device *phydev,
1242cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, void *data)
1243cde0f4f8SMichael Walle {
1244cde0f4f8SMichael Walle 	switch (tuna->id) {
1245cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1246cde0f4f8SMichael Walle 		return at803x_get_downshift(phydev, data);
1247cde0f4f8SMichael Walle 	default:
1248cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1249cde0f4f8SMichael Walle 	}
1250cde0f4f8SMichael Walle }
1251cde0f4f8SMichael Walle 
1252cde0f4f8SMichael Walle static int at803x_set_tunable(struct phy_device *phydev,
1253cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, const void *data)
1254cde0f4f8SMichael Walle {
1255cde0f4f8SMichael Walle 	switch (tuna->id) {
1256cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1257cde0f4f8SMichael Walle 		return at803x_set_downshift(phydev, *(const u8 *)data);
1258cde0f4f8SMichael Walle 	default:
1259cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1260cde0f4f8SMichael Walle 	}
1261cde0f4f8SMichael Walle }
1262cde0f4f8SMichael Walle 
12636cb75767SMichael Walle static int at803x_cable_test_result_trans(u16 status)
12646cb75767SMichael Walle {
12656cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
12666cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_NORMAL:
12676cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
12686cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
12696cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
12706cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
12716cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
12726cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_FAIL:
12736cb75767SMichael Walle 	default:
12746cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
12756cb75767SMichael Walle 	}
12766cb75767SMichael Walle }
12776cb75767SMichael Walle 
12786cb75767SMichael Walle static bool at803x_cdt_test_failed(u16 status)
12796cb75767SMichael Walle {
12806cb75767SMichael Walle 	return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
12816cb75767SMichael Walle 		AT803X_CDT_STATUS_STAT_FAIL;
12826cb75767SMichael Walle }
12836cb75767SMichael Walle 
12846cb75767SMichael Walle static bool at803x_cdt_fault_length_valid(u16 status)
12856cb75767SMichael Walle {
12866cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
12876cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
12886cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
12896cb75767SMichael Walle 		return true;
12906cb75767SMichael Walle 	}
12916cb75767SMichael Walle 	return false;
12926cb75767SMichael Walle }
12936cb75767SMichael Walle 
12946cb75767SMichael Walle static int at803x_cdt_fault_length(u16 status)
12956cb75767SMichael Walle {
12966cb75767SMichael Walle 	int dt;
12976cb75767SMichael Walle 
12986cb75767SMichael Walle 	/* According to the datasheet the distance to the fault is
12996cb75767SMichael Walle 	 * DELTA_TIME * 0.824 meters.
13006cb75767SMichael Walle 	 *
13016cb75767SMichael Walle 	 * The author suspect the correct formula is:
13026cb75767SMichael Walle 	 *
13036cb75767SMichael Walle 	 *   fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2
13046cb75767SMichael Walle 	 *
13056cb75767SMichael Walle 	 * where c is the speed of light, VF is the velocity factor of
13066cb75767SMichael Walle 	 * the twisted pair cable, 125MHz the counter frequency and
13076cb75767SMichael Walle 	 * we need to divide by 2 because the hardware will measure the
13086cb75767SMichael Walle 	 * round trip time to the fault and back to the PHY.
13096cb75767SMichael Walle 	 *
13106cb75767SMichael Walle 	 * With a VF of 0.69 we get the factor 0.824 mentioned in the
13116cb75767SMichael Walle 	 * datasheet.
13126cb75767SMichael Walle 	 */
13136cb75767SMichael Walle 	dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status);
13146cb75767SMichael Walle 
13156cb75767SMichael Walle 	return (dt * 824) / 10;
13166cb75767SMichael Walle }
13176cb75767SMichael Walle 
13186cb75767SMichael Walle static int at803x_cdt_start(struct phy_device *phydev, int pair)
13196cb75767SMichael Walle {
13206cb75767SMichael Walle 	u16 cdt;
13216cb75767SMichael Walle 
13226cb75767SMichael Walle 	cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
13236cb75767SMichael Walle 	      AT803X_CDT_ENABLE_TEST;
13246cb75767SMichael Walle 
13256cb75767SMichael Walle 	return phy_write(phydev, AT803X_CDT, cdt);
13266cb75767SMichael Walle }
13276cb75767SMichael Walle 
13286cb75767SMichael Walle static int at803x_cdt_wait_for_completion(struct phy_device *phydev)
13296cb75767SMichael Walle {
13306cb75767SMichael Walle 	int val, ret;
13316cb75767SMichael Walle 
13326cb75767SMichael Walle 	/* One test run takes about 25ms */
13336cb75767SMichael Walle 	ret = phy_read_poll_timeout(phydev, AT803X_CDT, val,
13346cb75767SMichael Walle 				    !(val & AT803X_CDT_ENABLE_TEST),
13356cb75767SMichael Walle 				    30000, 100000, true);
13366cb75767SMichael Walle 
13376cb75767SMichael Walle 	return ret < 0 ? ret : 0;
13386cb75767SMichael Walle }
13396cb75767SMichael Walle 
13406cb75767SMichael Walle static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
13416cb75767SMichael Walle {
13426cb75767SMichael Walle 	static const int ethtool_pair[] = {
13436cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_A,
13446cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_B,
13456cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_C,
13466cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_D,
13476cb75767SMichael Walle 	};
13486cb75767SMichael Walle 	int ret, val;
13496cb75767SMichael Walle 
13506cb75767SMichael Walle 	ret = at803x_cdt_start(phydev, pair);
13516cb75767SMichael Walle 	if (ret)
13526cb75767SMichael Walle 		return ret;
13536cb75767SMichael Walle 
13546cb75767SMichael Walle 	ret = at803x_cdt_wait_for_completion(phydev);
13556cb75767SMichael Walle 	if (ret)
13566cb75767SMichael Walle 		return ret;
13576cb75767SMichael Walle 
13586cb75767SMichael Walle 	val = phy_read(phydev, AT803X_CDT_STATUS);
13596cb75767SMichael Walle 	if (val < 0)
13606cb75767SMichael Walle 		return val;
13616cb75767SMichael Walle 
13626cb75767SMichael Walle 	if (at803x_cdt_test_failed(val))
13636cb75767SMichael Walle 		return 0;
13646cb75767SMichael Walle 
13656cb75767SMichael Walle 	ethnl_cable_test_result(phydev, ethtool_pair[pair],
13666cb75767SMichael Walle 				at803x_cable_test_result_trans(val));
13676cb75767SMichael Walle 
13686cb75767SMichael Walle 	if (at803x_cdt_fault_length_valid(val))
13696cb75767SMichael Walle 		ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
13706cb75767SMichael Walle 					      at803x_cdt_fault_length(val));
13716cb75767SMichael Walle 
13726cb75767SMichael Walle 	return 1;
13736cb75767SMichael Walle }
13746cb75767SMichael Walle 
13756cb75767SMichael Walle static int at803x_cable_test_get_status(struct phy_device *phydev,
13766cb75767SMichael Walle 					bool *finished)
13776cb75767SMichael Walle {
1378dc0f3ed1SOleksij Rempel 	unsigned long pair_mask;
13796cb75767SMichael Walle 	int retries = 20;
13806cb75767SMichael Walle 	int pair, ret;
13816cb75767SMichael Walle 
1382dc0f3ed1SOleksij Rempel 	if (phydev->phy_id == ATH9331_PHY_ID ||
1383fada2ce0SDavid Bauer 	    phydev->phy_id == ATH8032_PHY_ID ||
1384fada2ce0SDavid Bauer 	    phydev->phy_id == QCA9561_PHY_ID)
1385dc0f3ed1SOleksij Rempel 		pair_mask = 0x3;
1386dc0f3ed1SOleksij Rempel 	else
1387dc0f3ed1SOleksij Rempel 		pair_mask = 0xf;
1388dc0f3ed1SOleksij Rempel 
13896cb75767SMichael Walle 	*finished = false;
13906cb75767SMichael Walle 
13916cb75767SMichael Walle 	/* According to the datasheet the CDT can be performed when
13926cb75767SMichael Walle 	 * there is no link partner or when the link partner is
13936cb75767SMichael Walle 	 * auto-negotiating. Starting the test will restart the AN
13946cb75767SMichael Walle 	 * automatically. It seems that doing this repeatedly we will
13956cb75767SMichael Walle 	 * get a slot where our link partner won't disturb our
13966cb75767SMichael Walle 	 * measurement.
13976cb75767SMichael Walle 	 */
13986cb75767SMichael Walle 	while (pair_mask && retries--) {
13996cb75767SMichael Walle 		for_each_set_bit(pair, &pair_mask, 4) {
14006cb75767SMichael Walle 			ret = at803x_cable_test_one_pair(phydev, pair);
14016cb75767SMichael Walle 			if (ret < 0)
14026cb75767SMichael Walle 				return ret;
14036cb75767SMichael Walle 			if (ret)
14046cb75767SMichael Walle 				clear_bit(pair, &pair_mask);
14056cb75767SMichael Walle 		}
14066cb75767SMichael Walle 		if (pair_mask)
14076cb75767SMichael Walle 			msleep(250);
14086cb75767SMichael Walle 	}
14096cb75767SMichael Walle 
14106cb75767SMichael Walle 	*finished = true;
14116cb75767SMichael Walle 
14126cb75767SMichael Walle 	return 0;
14136cb75767SMichael Walle }
14146cb75767SMichael Walle 
14156cb75767SMichael Walle static int at803x_cable_test_start(struct phy_device *phydev)
14166cb75767SMichael Walle {
14176cb75767SMichael Walle 	/* Enable auto-negotiation, but advertise no capabilities, no link
14186cb75767SMichael Walle 	 * will be established. A restart of the auto-negotiation is not
14196cb75767SMichael Walle 	 * required, because the cable test will automatically break the link.
14206cb75767SMichael Walle 	 */
14216cb75767SMichael Walle 	phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
14226cb75767SMichael Walle 	phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
1423dc0f3ed1SOleksij Rempel 	if (phydev->phy_id != ATH9331_PHY_ID &&
1424fada2ce0SDavid Bauer 	    phydev->phy_id != ATH8032_PHY_ID &&
1425fada2ce0SDavid Bauer 	    phydev->phy_id != QCA9561_PHY_ID)
14266cb75767SMichael Walle 		phy_write(phydev, MII_CTRL1000, 0);
14276cb75767SMichael Walle 
14286cb75767SMichael Walle 	/* we do all the (time consuming) work later */
14296cb75767SMichael Walle 	return 0;
14306cb75767SMichael Walle }
14316cb75767SMichael Walle 
1432272833b9SAnsuel Smith static int qca83xx_config_init(struct phy_device *phydev)
1433272833b9SAnsuel Smith {
1434272833b9SAnsuel Smith 	u8 switch_revision;
1435272833b9SAnsuel Smith 
1436272833b9SAnsuel Smith 	switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK;
1437272833b9SAnsuel Smith 
1438272833b9SAnsuel Smith 	switch (switch_revision) {
1439272833b9SAnsuel Smith 	case 1:
1440272833b9SAnsuel Smith 		/* For 100M waveform */
144167999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0x02ea);
1442272833b9SAnsuel Smith 		/* Turn on Gigabit clock */
144367999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x68a0);
1444272833b9SAnsuel Smith 		break;
1445272833b9SAnsuel Smith 
1446272833b9SAnsuel Smith 	case 2:
1447272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0);
1448272833b9SAnsuel Smith 		fallthrough;
1449272833b9SAnsuel Smith 	case 4:
1450272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f);
145167999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x6860);
145267999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0x2c46);
1453272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000);
1454272833b9SAnsuel Smith 		break;
1455272833b9SAnsuel Smith 	}
1456272833b9SAnsuel Smith 
14571ca83119SAnsuel Smith 	/* QCA8327 require DAC amplitude adjustment for 100m set to +6%.
14581ca83119SAnsuel Smith 	 * Disable on init and enable only with 100m speed following
14591ca83119SAnsuel Smith 	 * qca original source code.
14601ca83119SAnsuel Smith 	 */
14611ca83119SAnsuel Smith 	if (phydev->drv->phy_id == QCA8327_A_PHY_ID ||
14621ca83119SAnsuel Smith 	    phydev->drv->phy_id == QCA8327_B_PHY_ID)
146367999555SAnsuel Smith 		at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
14641ca83119SAnsuel Smith 				      QCA8327_DEBUG_MANU_CTRL_EN, 0);
14651ca83119SAnsuel Smith 
14669d1c29b4SAnsuel Smith 	/* Following original QCA sourcecode set port to prefer master */
14679d1c29b4SAnsuel Smith 	phy_set_bits(phydev, MII_CTRL1000, CTL1000_PREFER_MASTER);
14689d1c29b4SAnsuel Smith 
1469272833b9SAnsuel Smith 	return 0;
1470272833b9SAnsuel Smith }
1471272833b9SAnsuel Smith 
14721ca83119SAnsuel Smith static void qca83xx_link_change_notify(struct phy_device *phydev)
14731ca83119SAnsuel Smith {
14741ca83119SAnsuel Smith 	/* QCA8337 doesn't require DAC Amplitude adjustement */
14751ca83119SAnsuel Smith 	if (phydev->drv->phy_id == QCA8337_PHY_ID)
14761ca83119SAnsuel Smith 		return;
14771ca83119SAnsuel Smith 
14781ca83119SAnsuel Smith 	/* Set DAC Amplitude adjustment to +6% for 100m on link running */
14791ca83119SAnsuel Smith 	if (phydev->state == PHY_RUNNING) {
14801ca83119SAnsuel Smith 		if (phydev->speed == SPEED_100)
148167999555SAnsuel Smith 			at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
14821ca83119SAnsuel Smith 					      QCA8327_DEBUG_MANU_CTRL_EN,
14831ca83119SAnsuel Smith 					      QCA8327_DEBUG_MANU_CTRL_EN);
14841ca83119SAnsuel Smith 	} else {
14851ca83119SAnsuel Smith 		/* Reset DAC Amplitude adjustment */
148667999555SAnsuel Smith 		at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
14871ca83119SAnsuel Smith 				      QCA8327_DEBUG_MANU_CTRL_EN, 0);
14881ca83119SAnsuel Smith 	}
14891ca83119SAnsuel Smith }
14901ca83119SAnsuel Smith 
1491ba3c01eeSAnsuel Smith static int qca83xx_resume(struct phy_device *phydev)
1492ba3c01eeSAnsuel Smith {
1493ba3c01eeSAnsuel Smith 	int ret, val;
1494ba3c01eeSAnsuel Smith 
1495ba3c01eeSAnsuel Smith 	/* Skip reset if not suspended */
1496ba3c01eeSAnsuel Smith 	if (!phydev->suspended)
1497ba3c01eeSAnsuel Smith 		return 0;
1498ba3c01eeSAnsuel Smith 
1499ba3c01eeSAnsuel Smith 	/* Reinit the port, reset values set by suspend */
1500ba3c01eeSAnsuel Smith 	qca83xx_config_init(phydev);
1501ba3c01eeSAnsuel Smith 
1502ba3c01eeSAnsuel Smith 	/* Reset the port on port resume */
1503ba3c01eeSAnsuel Smith 	phy_set_bits(phydev, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
1504ba3c01eeSAnsuel Smith 
1505ba3c01eeSAnsuel Smith 	/* On resume from suspend the switch execute a reset and
1506ba3c01eeSAnsuel Smith 	 * restart auto-negotiation. Wait for reset to complete.
1507ba3c01eeSAnsuel Smith 	 */
1508ba3c01eeSAnsuel Smith 	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1509ba3c01eeSAnsuel Smith 				    50000, 600000, true);
1510ba3c01eeSAnsuel Smith 	if (ret)
1511ba3c01eeSAnsuel Smith 		return ret;
1512ba3c01eeSAnsuel Smith 
1513ba3c01eeSAnsuel Smith 	msleep(1);
1514ba3c01eeSAnsuel Smith 
1515ba3c01eeSAnsuel Smith 	return 0;
1516ba3c01eeSAnsuel Smith }
1517ba3c01eeSAnsuel Smith 
1518ba3c01eeSAnsuel Smith static int qca83xx_suspend(struct phy_device *phydev)
1519ba3c01eeSAnsuel Smith {
1520ba3c01eeSAnsuel Smith 	u16 mask = 0;
1521ba3c01eeSAnsuel Smith 
1522ba3c01eeSAnsuel Smith 	/* Only QCA8337 support actual suspend.
1523ba3c01eeSAnsuel Smith 	 * QCA8327 cause port unreliability when phy suspend
1524ba3c01eeSAnsuel Smith 	 * is set.
1525ba3c01eeSAnsuel Smith 	 */
1526ba3c01eeSAnsuel Smith 	if (phydev->drv->phy_id == QCA8337_PHY_ID) {
1527ba3c01eeSAnsuel Smith 		genphy_suspend(phydev);
1528ba3c01eeSAnsuel Smith 	} else {
1529ba3c01eeSAnsuel Smith 		mask |= ~(BMCR_SPEED1000 | BMCR_FULLDPLX);
1530ba3c01eeSAnsuel Smith 		phy_modify(phydev, MII_BMCR, mask, 0);
1531ba3c01eeSAnsuel Smith 	}
1532ba3c01eeSAnsuel Smith 
153367999555SAnsuel Smith 	at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_GREEN,
1534ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_GATE_CLK_IN1000, 0);
1535ba3c01eeSAnsuel Smith 
1536ba3c01eeSAnsuel Smith 	at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
1537ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE |
1538ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_HIB_CTRL_SEL_RST_80U, 0);
1539ba3c01eeSAnsuel Smith 
1540ba3c01eeSAnsuel Smith 	return 0;
1541ba3c01eeSAnsuel Smith }
1542ba3c01eeSAnsuel Smith 
15432acdd43fSLuo Jie static int qca808x_phy_fast_retrain_config(struct phy_device *phydev)
15442acdd43fSLuo Jie {
15452acdd43fSLuo Jie 	int ret;
15462acdd43fSLuo Jie 
15472acdd43fSLuo Jie 	/* Enable fast retrain */
15482acdd43fSLuo Jie 	ret = genphy_c45_fast_retrain(phydev, true);
15492acdd43fSLuo Jie 	if (ret)
15502acdd43fSLuo Jie 		return ret;
15512acdd43fSLuo Jie 
15522acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_AN, QCA808X_PHY_MMD7_TOP_OPTION1,
15532acdd43fSLuo Jie 			QCA808X_TOP_OPTION1_DATA);
15542acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB,
15552acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_20DB_VALUE);
15562acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB,
15572acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_17DB_VALUE);
15582acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB,
15592acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_27DB_VALUE);
15602acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB,
15612acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_28DB_VALUE);
15622acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_1,
15632acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_1_VALUE);
15642acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_4,
15652acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_4_VALUE);
15662acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_5,
15672acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_5_VALUE);
15682acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_3,
15692acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_3_VALUE);
15702acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_6,
15712acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_6_VALUE);
15722acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_2,
15732acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_2_VALUE);
15742acdd43fSLuo Jie 
15752acdd43fSLuo Jie 	return 0;
15762acdd43fSLuo Jie }
15772acdd43fSLuo Jie 
1578*9d4dae29SLuo Jie static int qca808x_phy_ms_random_seed_set(struct phy_device *phydev)
1579*9d4dae29SLuo Jie {
1580*9d4dae29SLuo Jie 	u16 seed_value = (prandom_u32() % QCA808X_MASTER_SLAVE_SEED_RANGE);
1581*9d4dae29SLuo Jie 
1582*9d4dae29SLuo Jie 	return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED,
1583*9d4dae29SLuo Jie 			QCA808X_MASTER_SLAVE_SEED_CFG,
1584*9d4dae29SLuo Jie 			FIELD_PREP(QCA808X_MASTER_SLAVE_SEED_CFG, seed_value));
1585*9d4dae29SLuo Jie }
1586*9d4dae29SLuo Jie 
1587*9d4dae29SLuo Jie static int qca808x_phy_ms_seed_enable(struct phy_device *phydev, bool enable)
1588*9d4dae29SLuo Jie {
1589*9d4dae29SLuo Jie 	u16 seed_enable = 0;
1590*9d4dae29SLuo Jie 
1591*9d4dae29SLuo Jie 	if (enable)
1592*9d4dae29SLuo Jie 		seed_enable = QCA808X_MASTER_SLAVE_SEED_ENABLE;
1593*9d4dae29SLuo Jie 
1594*9d4dae29SLuo Jie 	return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED,
1595*9d4dae29SLuo Jie 			QCA808X_MASTER_SLAVE_SEED_ENABLE, seed_enable);
1596*9d4dae29SLuo Jie }
1597*9d4dae29SLuo Jie 
15982acdd43fSLuo Jie static int qca808x_config_init(struct phy_device *phydev)
15992acdd43fSLuo Jie {
16002acdd43fSLuo Jie 	int ret;
16012acdd43fSLuo Jie 
16022acdd43fSLuo Jie 	/* Active adc&vga on 802.3az for the link 1000M and 100M */
16032acdd43fSLuo Jie 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_ADDR_CLD_CTRL7,
16042acdd43fSLuo Jie 			QCA808X_8023AZ_AFE_CTRL_MASK, QCA808X_8023AZ_AFE_EN);
16052acdd43fSLuo Jie 	if (ret)
16062acdd43fSLuo Jie 		return ret;
16072acdd43fSLuo Jie 
16082acdd43fSLuo Jie 	/* Adjust the threshold on 802.3az for the link 1000M */
16092acdd43fSLuo Jie 	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
16102acdd43fSLuo Jie 			QCA808X_PHY_MMD3_AZ_TRAINING_CTRL, QCA808X_MMD3_AZ_TRAINING_VAL);
16112acdd43fSLuo Jie 	if (ret)
16122acdd43fSLuo Jie 		return ret;
16132acdd43fSLuo Jie 
16142acdd43fSLuo Jie 	/* Config the fast retrain for the link 2500M */
16152acdd43fSLuo Jie 	ret = qca808x_phy_fast_retrain_config(phydev);
16162acdd43fSLuo Jie 	if (ret)
16172acdd43fSLuo Jie 		return ret;
16182acdd43fSLuo Jie 
1619*9d4dae29SLuo Jie 	/* Configure lower ramdom seed to make phy linked as slave mode */
1620*9d4dae29SLuo Jie 	ret = qca808x_phy_ms_random_seed_set(phydev);
1621*9d4dae29SLuo Jie 	if (ret)
1622*9d4dae29SLuo Jie 		return ret;
1623*9d4dae29SLuo Jie 
1624*9d4dae29SLuo Jie 	/* Enable seed */
1625*9d4dae29SLuo Jie 	ret = qca808x_phy_ms_seed_enable(phydev, true);
1626*9d4dae29SLuo Jie 	if (ret)
1627*9d4dae29SLuo Jie 		return ret;
1628*9d4dae29SLuo Jie 
16292acdd43fSLuo Jie 	/* Configure adc threshold as 100mv for the link 10M */
16302acdd43fSLuo Jie 	return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_ADC_THRESHOLD,
16312acdd43fSLuo Jie 			QCA808X_ADC_THRESHOLD_MASK, QCA808X_ADC_THRESHOLD_100MV);
16322acdd43fSLuo Jie }
16332acdd43fSLuo Jie 
163479c7bc05SLuo Jie static int qca808x_read_status(struct phy_device *phydev)
163579c7bc05SLuo Jie {
163679c7bc05SLuo Jie 	int ret;
163779c7bc05SLuo Jie 
163879c7bc05SLuo Jie 	ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_STAT);
163979c7bc05SLuo Jie 	if (ret < 0)
164079c7bc05SLuo Jie 		return ret;
164179c7bc05SLuo Jie 
164279c7bc05SLuo Jie 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->lp_advertising,
164379c7bc05SLuo Jie 			ret & MDIO_AN_10GBT_STAT_LP2_5G);
164479c7bc05SLuo Jie 
164579c7bc05SLuo Jie 	ret = genphy_read_status(phydev);
164679c7bc05SLuo Jie 	if (ret)
164779c7bc05SLuo Jie 		return ret;
164879c7bc05SLuo Jie 
164979c7bc05SLuo Jie 	ret = at803x_read_specific_status(phydev);
165079c7bc05SLuo Jie 	if (ret < 0)
165179c7bc05SLuo Jie 		return ret;
165279c7bc05SLuo Jie 
165379c7bc05SLuo Jie 	if (phydev->link && phydev->speed == SPEED_2500)
165479c7bc05SLuo Jie 		phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
165579c7bc05SLuo Jie 	else
165679c7bc05SLuo Jie 		phydev->interface = PHY_INTERFACE_MODE_SMII;
165779c7bc05SLuo Jie 
165879c7bc05SLuo Jie 	return 0;
165979c7bc05SLuo Jie }
166079c7bc05SLuo Jie 
1661*9d4dae29SLuo Jie static int qca808x_soft_reset(struct phy_device *phydev)
1662*9d4dae29SLuo Jie {
1663*9d4dae29SLuo Jie 	int ret;
1664*9d4dae29SLuo Jie 
1665*9d4dae29SLuo Jie 	ret = genphy_soft_reset(phydev);
1666*9d4dae29SLuo Jie 	if (ret < 0)
1667*9d4dae29SLuo Jie 		return ret;
1668*9d4dae29SLuo Jie 
1669*9d4dae29SLuo Jie 	return qca808x_phy_ms_seed_enable(phydev, true);
1670*9d4dae29SLuo Jie }
1671*9d4dae29SLuo Jie 
1672317420abSMugunthan V N static struct phy_driver at803x_driver[] = {
1673317420abSMugunthan V N {
167496c36712SMichael Walle 	/* Qualcomm Atheros AR8035 */
16750465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
167696c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8035",
16776cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
16782f664823SMichael Walle 	.probe			= at803x_probe,
16792318ca8aSMichael Walle 	.remove			= at803x_remove,
16807dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
16810ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
1682cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
1683ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
1684ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
16856229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
16866229ed1fSDaniel Mack 	.resume			= at803x_resume,
1687dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
168806d5f344SRussell King 	.read_status		= at803x_read_status,
16890eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
169029773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1691cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
1692cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
16936cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
16946cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
1695317420abSMugunthan V N }, {
169696c36712SMichael Walle 	/* Qualcomm Atheros AR8030 */
1697bd8ca17fSDaniel Mack 	.phy_id			= ATH8030_PHY_ID,
169896c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8030",
16990465d8f8SMichael Walle 	.phy_id_mask		= AT8030_PHY_ID_MASK,
17002f664823SMichael Walle 	.probe			= at803x_probe,
17012318ca8aSMichael Walle 	.remove			= at803x_remove,
17020ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
170313a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
1704ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
1705ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
17066229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
17076229ed1fSDaniel Mack 	.resume			= at803x_resume,
1708dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
17090eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
171029773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
171105d7cce8SMugunthan V N }, {
171296c36712SMichael Walle 	/* Qualcomm Atheros AR8031/AR8033 */
17130465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
171496c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8031/AR8033",
17156cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
17162f664823SMichael Walle 	.probe			= at803x_probe,
17172318ca8aSMichael Walle 	.remove			= at803x_remove,
171805d7cce8SMugunthan V N 	.config_init		= at803x_config_init,
171963477a5dSMichael Walle 	.config_aneg		= at803x_config_aneg,
1720cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
172105d7cce8SMugunthan V N 	.set_wol		= at803x_set_wol,
172205d7cce8SMugunthan V N 	.get_wol		= at803x_get_wol,
17236229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
17246229ed1fSDaniel Mack 	.resume			= at803x_resume,
1725c329e5afSDavid Bauer 	.read_page		= at803x_read_page,
1726c329e5afSDavid Bauer 	.write_page		= at803x_write_page,
1727b856150cSDavid Bauer 	.get_features		= at803x_get_features,
172806d5f344SRussell King 	.read_status		= at803x_read_status,
172977a99394SZhao Qiang 	.config_intr		= &at803x_config_intr,
173029773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1731cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
1732cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
17336cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
17346cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
17357908d2ceSOleksij Rempel }, {
17365800091aSDavid Bauer 	/* Qualcomm Atheros AR8032 */
17375800091aSDavid Bauer 	PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
17385800091aSDavid Bauer 	.name			= "Qualcomm Atheros AR8032",
17395800091aSDavid Bauer 	.probe			= at803x_probe,
17405800091aSDavid Bauer 	.remove			= at803x_remove,
1741dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
17425800091aSDavid Bauer 	.config_init		= at803x_config_init,
17435800091aSDavid Bauer 	.link_change_notify	= at803x_link_change_notify,
17445800091aSDavid Bauer 	.set_wol		= at803x_set_wol,
17455800091aSDavid Bauer 	.get_wol		= at803x_get_wol,
17465800091aSDavid Bauer 	.suspend		= at803x_suspend,
17475800091aSDavid Bauer 	.resume			= at803x_resume,
17485800091aSDavid Bauer 	/* PHY_BASIC_FEATURES */
17495800091aSDavid Bauer 	.config_intr		= at803x_config_intr,
175029773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1751dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
1752dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
17535800091aSDavid Bauer }, {
17547908d2ceSOleksij Rempel 	/* ATHEROS AR9331 */
17557908d2ceSOleksij Rempel 	PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
175696c36712SMichael Walle 	.name			= "Qualcomm Atheros AR9331 built-in PHY",
17577908d2ceSOleksij Rempel 	.suspend		= at803x_suspend,
17587908d2ceSOleksij Rempel 	.resume			= at803x_resume,
1759dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
17607908d2ceSOleksij Rempel 	/* PHY_BASIC_FEATURES */
17617908d2ceSOleksij Rempel 	.config_intr		= &at803x_config_intr,
176229773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1763dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
1764dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
17657dce80c2SOleksij Rempel 	.read_status		= at803x_read_status,
17667dce80c2SOleksij Rempel 	.soft_reset		= genphy_soft_reset,
17677dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
1768272833b9SAnsuel Smith }, {
1769fada2ce0SDavid Bauer 	/* Qualcomm Atheros QCA9561 */
1770fada2ce0SDavid Bauer 	PHY_ID_MATCH_EXACT(QCA9561_PHY_ID),
1771fada2ce0SDavid Bauer 	.name			= "Qualcomm Atheros QCA9561 built-in PHY",
1772fada2ce0SDavid Bauer 	.suspend		= at803x_suspend,
1773fada2ce0SDavid Bauer 	.resume			= at803x_resume,
1774fada2ce0SDavid Bauer 	.flags			= PHY_POLL_CABLE_TEST,
1775fada2ce0SDavid Bauer 	/* PHY_BASIC_FEATURES */
1776fada2ce0SDavid Bauer 	.config_intr		= &at803x_config_intr,
1777fada2ce0SDavid Bauer 	.handle_interrupt	= at803x_handle_interrupt,
1778fada2ce0SDavid Bauer 	.cable_test_start	= at803x_cable_test_start,
1779fada2ce0SDavid Bauer 	.cable_test_get_status	= at803x_cable_test_get_status,
1780fada2ce0SDavid Bauer 	.read_status		= at803x_read_status,
1781fada2ce0SDavid Bauer 	.soft_reset		= genphy_soft_reset,
1782fada2ce0SDavid Bauer 	.config_aneg		= at803x_config_aneg,
1783fada2ce0SDavid Bauer }, {
1784272833b9SAnsuel Smith 	/* QCA8337 */
1785272833b9SAnsuel Smith 	.phy_id			= QCA8337_PHY_ID,
1786272833b9SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
1787d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8337 internal PHY",
1788272833b9SAnsuel Smith 	/* PHY_GBIT_FEATURES */
17891ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
1790272833b9SAnsuel Smith 	.probe			= at803x_probe,
1791272833b9SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
1792272833b9SAnsuel Smith 	.config_init		= qca83xx_config_init,
1793272833b9SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
1794272833b9SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
1795272833b9SAnsuel Smith 	.get_strings		= at803x_get_strings,
1796272833b9SAnsuel Smith 	.get_stats		= at803x_get_stats,
1797ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
1798ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
17990ccf8511SAnsuel Smith }, {
1800b4df02b5SAnsuel Smith 	/* QCA8327-A from switch QCA8327-AL1A */
1801b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_A_PHY_ID,
18020ccf8511SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
1803d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-A internal PHY",
1804b4df02b5SAnsuel Smith 	/* PHY_GBIT_FEATURES */
18051ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
1806b4df02b5SAnsuel Smith 	.probe			= at803x_probe,
1807b4df02b5SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
1808b4df02b5SAnsuel Smith 	.config_init		= qca83xx_config_init,
1809b4df02b5SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
1810b4df02b5SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
1811b4df02b5SAnsuel Smith 	.get_strings		= at803x_get_strings,
1812b4df02b5SAnsuel Smith 	.get_stats		= at803x_get_stats,
1813ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
1814ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
1815b4df02b5SAnsuel Smith }, {
1816b4df02b5SAnsuel Smith 	/* QCA8327-B from switch QCA8327-BL1A */
1817b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_B_PHY_ID,
1818b4df02b5SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
1819d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-B internal PHY",
18200ccf8511SAnsuel Smith 	/* PHY_GBIT_FEATURES */
18211ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
18220ccf8511SAnsuel Smith 	.probe			= at803x_probe,
18230ccf8511SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
18240ccf8511SAnsuel Smith 	.config_init		= qca83xx_config_init,
18250ccf8511SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
18260ccf8511SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
18270ccf8511SAnsuel Smith 	.get_strings		= at803x_get_strings,
18280ccf8511SAnsuel Smith 	.get_stats		= at803x_get_stats,
1829ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
1830ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
1831daf61732SLuo Jie }, {
1832daf61732SLuo Jie 	/* Qualcomm QCA8081 */
1833daf61732SLuo Jie 	PHY_ID_MATCH_EXACT(QCA8081_PHY_ID),
1834daf61732SLuo Jie 	.name			= "Qualcomm QCA8081",
1835daf61732SLuo Jie 	.config_intr		= at803x_config_intr,
1836daf61732SLuo Jie 	.handle_interrupt	= at803x_handle_interrupt,
1837daf61732SLuo Jie 	.get_tunable		= at803x_get_tunable,
1838daf61732SLuo Jie 	.set_tunable		= at803x_set_tunable,
1839daf61732SLuo Jie 	.set_wol		= at803x_set_wol,
1840daf61732SLuo Jie 	.get_wol		= at803x_get_wol,
1841765c22aaSLuo Jie 	.get_features		= at803x_get_features,
1842f884d449SLuo Jie 	.config_aneg		= at803x_config_aneg,
1843daf61732SLuo Jie 	.suspend		= genphy_suspend,
1844daf61732SLuo Jie 	.resume			= genphy_resume,
184579c7bc05SLuo Jie 	.read_status		= qca808x_read_status,
18462acdd43fSLuo Jie 	.config_init		= qca808x_config_init,
1847*9d4dae29SLuo Jie 	.soft_reset		= qca808x_soft_reset,
1848272833b9SAnsuel Smith }, };
18490ca7111aSMatus Ujhelyi 
185050fd7150SJohan Hovold module_phy_driver(at803x_driver);
18510ca7111aSMatus Ujhelyi 
18520ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = {
18530465d8f8SMichael Walle 	{ ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
18540465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
18555800091aSDavid Bauer 	{ PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
18560465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
18577908d2ceSOleksij Rempel 	{ PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
18580ccf8511SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8337_PHY_ID) },
1859b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_A_PHY_ID) },
1860b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_B_PHY_ID) },
1861fada2ce0SDavid Bauer 	{ PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) },
1862daf61732SLuo Jie 	{ PHY_ID_MATCH_EXACT(QCA8081_PHY_ID) },
18630ca7111aSMatus Ujhelyi 	{ }
18640ca7111aSMatus Ujhelyi };
18650ca7111aSMatus Ujhelyi 
18660ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl);
1867