xref: /openbmc/linux/drivers/net/phy/at803x.c (revision 7cc32095)
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>
162f664823SMichael Walle #include <linux/bitfield.h>
172f664823SMichael Walle #include <linux/regulator/of_regulator.h>
182f664823SMichael Walle #include <linux/regulator/driver.h>
192f664823SMichael Walle #include <linux/regulator/consumer.h>
20a593a2fcSAndy Shevchenko #include <linux/of.h>
21dc4d5fccSRobert Hancock #include <linux/phylink.h>
22dc4d5fccSRobert Hancock #include <linux/sfp.h>
232f664823SMichael Walle #include <dt-bindings/net/qca-ar803x.h>
240ca7111aSMatus Ujhelyi 
257dce80c2SOleksij Rempel #define AT803X_SPECIFIC_FUNCTION_CONTROL	0x10
267dce80c2SOleksij Rempel #define AT803X_SFC_ASSERT_CRS			BIT(11)
277dce80c2SOleksij Rempel #define AT803X_SFC_FORCE_LINK			BIT(10)
287dce80c2SOleksij Rempel #define AT803X_SFC_MDI_CROSSOVER_MODE_M		GENMASK(6, 5)
297dce80c2SOleksij Rempel #define AT803X_SFC_AUTOMATIC_CROSSOVER		0x3
307dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDIX			0x1
317dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDI			0x0
327dce80c2SOleksij Rempel #define AT803X_SFC_SQE_TEST			BIT(2)
337dce80c2SOleksij Rempel #define AT803X_SFC_POLARITY_REVERSAL		BIT(1)
347dce80c2SOleksij Rempel #define AT803X_SFC_DISABLE_JABBER		BIT(0)
357dce80c2SOleksij Rempel 
3606d5f344SRussell King #define AT803X_SPECIFIC_STATUS			0x11
379540cddaSLuo Jie #define AT803X_SS_SPEED_MASK			GENMASK(15, 14)
389540cddaSLuo Jie #define AT803X_SS_SPEED_1000			2
399540cddaSLuo Jie #define AT803X_SS_SPEED_100			1
409540cddaSLuo Jie #define AT803X_SS_SPEED_10			0
4106d5f344SRussell King #define AT803X_SS_DUPLEX			BIT(13)
4206d5f344SRussell King #define AT803X_SS_SPEED_DUPLEX_RESOLVED		BIT(11)
4306d5f344SRussell King #define AT803X_SS_MDIX				BIT(6)
4406d5f344SRussell King 
4579c7bc05SLuo Jie #define QCA808X_SS_SPEED_MASK			GENMASK(9, 7)
4679c7bc05SLuo Jie #define QCA808X_SS_SPEED_2500			4
4779c7bc05SLuo Jie 
480ca7111aSMatus Ujhelyi #define AT803X_INTR_ENABLE			0x12
49e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_AUTONEG_ERR		BIT(15)
50e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_SPEED_CHANGED	BIT(14)
51e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_DUPLEX_CHANGED	BIT(13)
52e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_PAGE_RECEIVED	BIT(12)
53e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_FAIL		BIT(11)
54e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_SUCCESS		BIT(10)
553265f421SRobert Hancock #define AT803X_INTR_ENABLE_LINK_FAIL_BX		BIT(8)
563265f421SRobert Hancock #define AT803X_INTR_ENABLE_LINK_SUCCESS_BX	BIT(7)
57e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE	BIT(5)
58e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_POLARITY_CHANGED	BIT(1)
59e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WOL			BIT(0)
60e6e4a556SMartin Blumenstingl 
610ca7111aSMatus Ujhelyi #define AT803X_INTR_STATUS			0x13
62a46bd63bSMartin Blumenstingl 
6313a56b44SDaniel Mack #define AT803X_SMART_SPEED			0x14
64cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_ENABLE		BIT(5)
65cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_RETRY_LIMIT_MASK	GENMASK(4, 2)
66cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_BYPASS_TIMER		BIT(1)
676cb75767SMichael Walle #define AT803X_CDT				0x16
686cb75767SMichael Walle #define AT803X_CDT_MDI_PAIR_MASK		GENMASK(9, 8)
696cb75767SMichael Walle #define AT803X_CDT_ENABLE_TEST			BIT(0)
706cb75767SMichael Walle #define AT803X_CDT_STATUS			0x1c
716cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_NORMAL		0
726cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_SHORT		1
736cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_OPEN		2
746cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_FAIL		3
756cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_MASK		GENMASK(9, 8)
766cb75767SMichael Walle #define AT803X_CDT_STATUS_DELTA_TIME_MASK	GENMASK(7, 0)
7713a56b44SDaniel Mack #define AT803X_LED_CONTROL			0x18
78a46bd63bSMartin Blumenstingl 
797beecaf7SLuo Jie #define AT803X_PHY_MMD3_WOL_CTRL		0x8012
807beecaf7SLuo Jie #define AT803X_WOL_EN				BIT(5)
810ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_0_15_OFFSET		0x804C
820ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_16_31_OFFSET	0x804B
830ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_32_47_OFFSET	0x804A
84f62265b5SZefir Kurtisi #define AT803X_REG_CHIP_CONFIG			0x1f
85f62265b5SZefir Kurtisi #define AT803X_BT_BX_REG_SEL			0x8000
86a46bd63bSMartin Blumenstingl 
871ca6d1b1SMugunthan V N #define AT803X_DEBUG_ADDR			0x1D
881ca6d1b1SMugunthan V N #define AT803X_DEBUG_DATA			0x1E
89a46bd63bSMartin Blumenstingl 
90f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_MASK			0x0F
913265f421SRobert Hancock #define AT803X_MODE_CFG_BASET_RGMII		0x00
923265f421SRobert Hancock #define AT803X_MODE_CFG_BASET_SGMII		0x01
933265f421SRobert Hancock #define AT803X_MODE_CFG_BX1000_RGMII_50OHM	0x02
943265f421SRobert Hancock #define AT803X_MODE_CFG_BX1000_RGMII_75OHM	0x03
953265f421SRobert Hancock #define AT803X_MODE_CFG_BX1000_CONV_50OHM	0x04
963265f421SRobert Hancock #define AT803X_MODE_CFG_BX1000_CONV_75OHM	0x05
973265f421SRobert Hancock #define AT803X_MODE_CFG_FX100_RGMII_50OHM	0x06
983265f421SRobert Hancock #define AT803X_MODE_CFG_FX100_CONV_50OHM	0x07
993265f421SRobert Hancock #define AT803X_MODE_CFG_RGMII_AUTO_MDET		0x0B
1003265f421SRobert Hancock #define AT803X_MODE_CFG_FX100_RGMII_75OHM	0x0E
1013265f421SRobert Hancock #define AT803X_MODE_CFG_FX100_CONV_75OHM	0x0F
102f62265b5SZefir Kurtisi 
103f62265b5SZefir Kurtisi #define AT803X_PSSR				0x11	/*PHY-Specific Status Register*/
104f62265b5SZefir Kurtisi #define AT803X_PSSR_MR_AN_COMPLETE		0x0200
105f62265b5SZefir Kurtisi 
10667999555SAnsuel Smith #define AT803X_DEBUG_ANALOG_TEST_CTRL		0x00
1071ca83119SAnsuel Smith #define QCA8327_DEBUG_MANU_CTRL_EN		BIT(2)
1081ca83119SAnsuel Smith #define QCA8337_DEBUG_MANU_CTRL_EN		GENMASK(3, 2)
1092e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_RX_CLK_DLY_EN		BIT(15)
110a46bd63bSMartin Blumenstingl 
11167999555SAnsuel Smith #define AT803X_DEBUG_SYSTEM_CTRL_MODE		0x05
1122e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_TX_CLK_DLY_EN		BIT(8)
1130ca7111aSMatus Ujhelyi 
114ba3c01eeSAnsuel Smith #define AT803X_DEBUG_REG_HIB_CTRL		0x0b
115ba3c01eeSAnsuel Smith #define   AT803X_DEBUG_HIB_CTRL_SEL_RST_80U	BIT(10)
116ba3c01eeSAnsuel Smith #define   AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE	BIT(13)
1179ecf0401SWei Fang #define   AT803X_DEBUG_HIB_CTRL_PS_HIB_EN	BIT(15)
118ba3c01eeSAnsuel Smith 
119272833b9SAnsuel Smith #define AT803X_DEBUG_REG_3C			0x3C
120272833b9SAnsuel Smith 
12167999555SAnsuel Smith #define AT803X_DEBUG_REG_GREEN			0x3D
122ba3c01eeSAnsuel Smith #define   AT803X_DEBUG_GATE_CLK_IN1000		BIT(6)
123272833b9SAnsuel Smith 
1242f664823SMichael Walle #define AT803X_DEBUG_REG_1F			0x1F
1252f664823SMichael Walle #define AT803X_DEBUG_PLL_ON			BIT(2)
1262f664823SMichael Walle #define AT803X_DEBUG_RGMII_1V8			BIT(3)
1272f664823SMichael Walle 
128272833b9SAnsuel Smith #define MDIO_AZ_DEBUG				0x800D
129272833b9SAnsuel Smith 
1302f664823SMichael Walle /* AT803x supports either the XTAL input pad, an internal PLL or the
1312f664823SMichael Walle  * DSP as clock reference for the clock output pad. The XTAL reference
1322f664823SMichael Walle  * is only used for 25 MHz output, all other frequencies need the PLL.
1332f664823SMichael Walle  * The DSP as a clock reference is used in synchronous ethernet
1342f664823SMichael Walle  * applications.
1352f664823SMichael Walle  *
1362f664823SMichael Walle  * By default the PLL is only enabled if there is a link. Otherwise
1372f664823SMichael Walle  * the PHY will go into low power state and disabled the PLL. You can
1382f664823SMichael Walle  * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
1392f664823SMichael Walle  * enabled.
1402f664823SMichael Walle  */
1412f664823SMichael Walle #define AT803X_MMD7_CLK25M			0x8016
1422f664823SMichael Walle #define AT803X_CLK_OUT_MASK			GENMASK(4, 2)
1432f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_XTAL		0
1442f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_DSP		1
1452f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_PLL		2
1462f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_DSP		3
1472f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_PLL		4
1482f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_DSP		5
1492f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_PLL		6
1502f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_DSP		7
1512f664823SMichael Walle 
152428061f7SMichael Walle /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
153428061f7SMichael Walle  * but doesn't support choosing between XTAL/PLL and DSP.
1542f664823SMichael Walle  */
1552f664823SMichael Walle #define AT8035_CLK_OUT_MASK			GENMASK(4, 3)
1562f664823SMichael Walle 
1572f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_MASK		GENMASK(8, 7)
1582f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_FULL		0
1592f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_HALF		1
1602f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_QUARTER		2
1612f664823SMichael Walle 
162cde0f4f8SMichael Walle #define AT803X_DEFAULT_DOWNSHIFT		5
163cde0f4f8SMichael Walle #define AT803X_MIN_DOWNSHIFT			2
164cde0f4f8SMichael Walle #define AT803X_MAX_DOWNSHIFT			9
165cde0f4f8SMichael Walle 
166390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL1		0x805b
167390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL2		0x805c
168390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3		0x805d
169390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN	BIT(8)
170390b4cadSRussell King 
1717908d2ceSOleksij Rempel #define ATH9331_PHY_ID				0x004dd041
172bd8ca17fSDaniel Mack #define ATH8030_PHY_ID				0x004dd076
173bd8ca17fSDaniel Mack #define ATH8031_PHY_ID				0x004dd074
1745800091aSDavid Bauer #define ATH8032_PHY_ID				0x004dd023
175bd8ca17fSDaniel Mack #define ATH8035_PHY_ID				0x004dd072
1760465d8f8SMichael Walle #define AT8030_PHY_ID_MASK			0xffffffef
177bd8ca17fSDaniel Mack 
178daf61732SLuo Jie #define QCA8081_PHY_ID				0x004dd101
179daf61732SLuo Jie 
180b4df02b5SAnsuel Smith #define QCA8327_A_PHY_ID			0x004dd033
181b4df02b5SAnsuel Smith #define QCA8327_B_PHY_ID			0x004dd034
182272833b9SAnsuel Smith #define QCA8337_PHY_ID				0x004dd036
183fada2ce0SDavid Bauer #define QCA9561_PHY_ID				0x004dd042
184272833b9SAnsuel Smith #define QCA8K_PHY_ID_MASK			0xffffffff
185272833b9SAnsuel Smith 
186272833b9SAnsuel Smith #define QCA8K_DEVFLAGS_REVISION_MASK		GENMASK(2, 0)
187272833b9SAnsuel Smith 
188c329e5afSDavid Bauer #define AT803X_PAGE_FIBER			0
189c329e5afSDavid Bauer #define AT803X_PAGE_COPPER			1
190c329e5afSDavid Bauer 
191d0e13fd5SAnsuel Smith /* don't turn off internal PLL */
192d0e13fd5SAnsuel Smith #define AT803X_KEEP_PLL_ENABLED			BIT(0)
193d0e13fd5SAnsuel Smith #define AT803X_DISABLE_SMARTEEE			BIT(1)
194d0e13fd5SAnsuel Smith 
1959ecf0401SWei Fang /* disable hibernation mode */
1969ecf0401SWei Fang #define AT803X_DISABLE_HIBERNATION_MODE		BIT(2)
1979ecf0401SWei Fang 
1982acdd43fSLuo Jie /* ADC threshold */
1992acdd43fSLuo Jie #define QCA808X_PHY_DEBUG_ADC_THRESHOLD		0x2c80
2002acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_MASK		GENMASK(7, 0)
2012acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_80MV		0
2022acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_100MV		0xf0
2032acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_200MV		0x0f
2042acdd43fSLuo Jie #define QCA808X_ADC_THRESHOLD_300MV		0xff
2052acdd43fSLuo Jie 
2062acdd43fSLuo Jie /* CLD control */
2072acdd43fSLuo Jie #define QCA808X_PHY_MMD3_ADDR_CLD_CTRL7		0x8007
2082acdd43fSLuo Jie #define QCA808X_8023AZ_AFE_CTRL_MASK		GENMASK(8, 4)
2092acdd43fSLuo Jie #define QCA808X_8023AZ_AFE_EN			0x90
2102acdd43fSLuo Jie 
2112acdd43fSLuo Jie /* AZ control */
2122acdd43fSLuo Jie #define QCA808X_PHY_MMD3_AZ_TRAINING_CTRL	0x8008
2132acdd43fSLuo Jie #define QCA808X_MMD3_AZ_TRAINING_VAL		0x1c32
2142acdd43fSLuo Jie 
2152acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB	0x8014
2162acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_20DB_VALUE	0x529
2172acdd43fSLuo Jie 
2182acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB	0x800E
2192acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_17DB_VALUE	0x341
2202acdd43fSLuo Jie 
2212acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB	0x801E
2222acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_27DB_VALUE	0x419
2232acdd43fSLuo Jie 
2242acdd43fSLuo Jie #define QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB	0x8020
2252acdd43fSLuo Jie #define QCA808X_MSE_THRESHOLD_28DB_VALUE	0x341
2262acdd43fSLuo Jie 
2272acdd43fSLuo Jie #define QCA808X_PHY_MMD7_TOP_OPTION1		0x901c
2282acdd43fSLuo Jie #define QCA808X_TOP_OPTION1_DATA		0x0
2292acdd43fSLuo Jie 
2302acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_1		0xa100
2312acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_1_VALUE		0x9203
2322acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_2		0xa101
2332acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_2_VALUE		0x48ad
2342acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_3		0xa103
2352acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_3_VALUE		0x1698
2362acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_4		0xa105
2372acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_4_VALUE		0x8001
2382acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_5		0xa106
2392acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_5_VALUE		0x1111
2402acdd43fSLuo Jie #define QCA808X_PHY_MMD3_DEBUG_6		0xa011
2412acdd43fSLuo Jie #define QCA808X_MMD3_DEBUG_6_VALUE		0x5f85
2422acdd43fSLuo Jie 
2439d4dae29SLuo Jie /* master/slave seed config */
2449d4dae29SLuo Jie #define QCA808X_PHY_DEBUG_LOCAL_SEED		9
2459d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_ENABLE	BIT(1)
2469d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_CFG		GENMASK(12, 2)
2479d4dae29SLuo Jie #define QCA808X_MASTER_SLAVE_SEED_RANGE		0x32
2489d4dae29SLuo Jie 
2498c84d752SLuo Jie /* Hibernation yields lower power consumpiton in contrast with normal operation mode.
2508c84d752SLuo Jie  * when the copper cable is unplugged, the PHY enters into hibernation mode in about 10s.
2518c84d752SLuo Jie  */
2528c84d752SLuo Jie #define QCA808X_DBG_AN_TEST			0xb
2538c84d752SLuo Jie #define QCA808X_HIBERNATION_EN			BIT(15)
2548c84d752SLuo Jie 
2558c84d752SLuo Jie #define QCA808X_CDT_ENABLE_TEST			BIT(15)
2568c84d752SLuo Jie #define QCA808X_CDT_INTER_CHECK_DIS		BIT(13)
2578c84d752SLuo Jie #define QCA808X_CDT_LENGTH_UNIT			BIT(10)
2588c84d752SLuo Jie 
2598c84d752SLuo Jie #define QCA808X_MMD3_CDT_STATUS			0x8064
2608c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_A		0x8065
2618c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_B		0x8066
2628c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_C		0x8067
2638c84d752SLuo Jie #define QCA808X_MMD3_CDT_DIAG_PAIR_D		0x8068
2648c84d752SLuo Jie #define QCA808X_CDT_DIAG_LENGTH			GENMASK(7, 0)
2658c84d752SLuo Jie 
2668c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_A			GENMASK(15, 12)
2678c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_B			GENMASK(11, 8)
2688c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_C			GENMASK(7, 4)
2698c84d752SLuo Jie #define QCA808X_CDT_CODE_PAIR_D			GENMASK(3, 0)
2708c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_FAIL		0
2718c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_NORMAL		1
2728c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_OPEN		2
2738c84d752SLuo Jie #define QCA808X_CDT_STATUS_STAT_SHORT		3
2748c84d752SLuo Jie 
275daf61732SLuo Jie MODULE_DESCRIPTION("Qualcomm Atheros AR803x and QCA808X PHY driver");
2760ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi");
2770ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL");
2780ca7111aSMatus Ujhelyi 
279272833b9SAnsuel Smith enum stat_access_type {
280272833b9SAnsuel Smith 	PHY,
281272833b9SAnsuel Smith 	MMD
282272833b9SAnsuel Smith };
283272833b9SAnsuel Smith 
284272833b9SAnsuel Smith struct at803x_hw_stat {
285272833b9SAnsuel Smith 	const char *string;
286272833b9SAnsuel Smith 	u8 reg;
287272833b9SAnsuel Smith 	u32 mask;
288272833b9SAnsuel Smith 	enum stat_access_type access_type;
289272833b9SAnsuel Smith };
290272833b9SAnsuel Smith 
291272833b9SAnsuel Smith static struct at803x_hw_stat at803x_hw_stats[] = {
292272833b9SAnsuel Smith 	{ "phy_idle_errors", 0xa, GENMASK(7, 0), PHY},
293272833b9SAnsuel Smith 	{ "phy_receive_errors", 0x15, GENMASK(15, 0), PHY},
294272833b9SAnsuel Smith 	{ "eee_wake_errors", 0x16, GENMASK(15, 0), MMD},
295272833b9SAnsuel Smith };
296272833b9SAnsuel Smith 
2972f664823SMichael Walle struct at803x_priv {
2982f664823SMichael Walle 	int flags;
2992f664823SMichael Walle 	u16 clk_25m_reg;
3002f664823SMichael Walle 	u16 clk_25m_mask;
301390b4cadSRussell King 	u8 smarteee_lpi_tw_1g;
302390b4cadSRussell King 	u8 smarteee_lpi_tw_100m;
3033265f421SRobert Hancock 	bool is_fiber;
3043265f421SRobert Hancock 	bool is_1000basex;
3052f664823SMichael Walle 	struct regulator_dev *vddio_rdev;
3062f664823SMichael Walle 	struct regulator_dev *vddh_rdev;
307272833b9SAnsuel Smith 	u64 stats[ARRAY_SIZE(at803x_hw_stats)];
3082f664823SMichael Walle };
3092f664823SMichael Walle 
31013a56b44SDaniel Mack struct at803x_context {
31113a56b44SDaniel Mack 	u16 bmcr;
31213a56b44SDaniel Mack 	u16 advertise;
31313a56b44SDaniel Mack 	u16 control1000;
31413a56b44SDaniel Mack 	u16 int_enable;
31513a56b44SDaniel Mack 	u16 smart_speed;
31613a56b44SDaniel Mack 	u16 led_control;
31713a56b44SDaniel Mack };
31813a56b44SDaniel Mack 
319272833b9SAnsuel Smith static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data)
320272833b9SAnsuel Smith {
321272833b9SAnsuel Smith 	int ret;
322272833b9SAnsuel Smith 
323272833b9SAnsuel Smith 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
324272833b9SAnsuel Smith 	if (ret < 0)
325272833b9SAnsuel Smith 		return ret;
326272833b9SAnsuel Smith 
327272833b9SAnsuel Smith 	return phy_write(phydev, AT803X_DEBUG_DATA, data);
328272833b9SAnsuel Smith }
329272833b9SAnsuel Smith 
3302e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
3312e5f9f28SMartin Blumenstingl {
3322e5f9f28SMartin Blumenstingl 	int ret;
3332e5f9f28SMartin Blumenstingl 
3342e5f9f28SMartin Blumenstingl 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
3352e5f9f28SMartin Blumenstingl 	if (ret < 0)
3362e5f9f28SMartin Blumenstingl 		return ret;
3372e5f9f28SMartin Blumenstingl 
3382e5f9f28SMartin Blumenstingl 	return phy_read(phydev, AT803X_DEBUG_DATA);
3392e5f9f28SMartin Blumenstingl }
3402e5f9f28SMartin Blumenstingl 
3412e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
3422e5f9f28SMartin Blumenstingl 				 u16 clear, u16 set)
3432e5f9f28SMartin Blumenstingl {
3442e5f9f28SMartin Blumenstingl 	u16 val;
3452e5f9f28SMartin Blumenstingl 	int ret;
3462e5f9f28SMartin Blumenstingl 
3472e5f9f28SMartin Blumenstingl 	ret = at803x_debug_reg_read(phydev, reg);
3482e5f9f28SMartin Blumenstingl 	if (ret < 0)
3492e5f9f28SMartin Blumenstingl 		return ret;
3502e5f9f28SMartin Blumenstingl 
3512e5f9f28SMartin Blumenstingl 	val = ret & 0xffff;
3522e5f9f28SMartin Blumenstingl 	val &= ~clear;
3532e5f9f28SMartin Blumenstingl 	val |= set;
3542e5f9f28SMartin Blumenstingl 
3552e5f9f28SMartin Blumenstingl 	return phy_write(phydev, AT803X_DEBUG_DATA, val);
3562e5f9f28SMartin Blumenstingl }
3572e5f9f28SMartin Blumenstingl 
358c329e5afSDavid Bauer static int at803x_write_page(struct phy_device *phydev, int page)
359c329e5afSDavid Bauer {
360c329e5afSDavid Bauer 	int mask;
361c329e5afSDavid Bauer 	int set;
362c329e5afSDavid Bauer 
363c329e5afSDavid Bauer 	if (page == AT803X_PAGE_COPPER) {
364c329e5afSDavid Bauer 		set = AT803X_BT_BX_REG_SEL;
365c329e5afSDavid Bauer 		mask = 0;
366c329e5afSDavid Bauer 	} else {
367c329e5afSDavid Bauer 		set = 0;
368c329e5afSDavid Bauer 		mask = AT803X_BT_BX_REG_SEL;
369c329e5afSDavid Bauer 	}
370c329e5afSDavid Bauer 
371c329e5afSDavid Bauer 	return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
372c329e5afSDavid Bauer }
373c329e5afSDavid Bauer 
374c329e5afSDavid Bauer static int at803x_read_page(struct phy_device *phydev)
375c329e5afSDavid Bauer {
376c329e5afSDavid Bauer 	int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
377c329e5afSDavid Bauer 
378c329e5afSDavid Bauer 	if (ccr < 0)
379c329e5afSDavid Bauer 		return ccr;
380c329e5afSDavid Bauer 
381c329e5afSDavid Bauer 	if (ccr & AT803X_BT_BX_REG_SEL)
382c329e5afSDavid Bauer 		return AT803X_PAGE_COPPER;
383c329e5afSDavid Bauer 
384c329e5afSDavid Bauer 	return AT803X_PAGE_FIBER;
385c329e5afSDavid Bauer }
386c329e5afSDavid Bauer 
3876d4cd041SVinod Koul static int at803x_enable_rx_delay(struct phy_device *phydev)
3886d4cd041SVinod Koul {
38967999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0,
3906d4cd041SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN);
3916d4cd041SVinod Koul }
3926d4cd041SVinod Koul 
3936d4cd041SVinod Koul static int at803x_enable_tx_delay(struct phy_device *phydev)
3946d4cd041SVinod Koul {
39567999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0,
3966d4cd041SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN);
3976d4cd041SVinod Koul }
3986d4cd041SVinod Koul 
39943f2ebd5SVinod Koul static int at803x_disable_rx_delay(struct phy_device *phydev)
4002e5f9f28SMartin Blumenstingl {
40167999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
402cd28d1d6SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN, 0);
4032e5f9f28SMartin Blumenstingl }
4042e5f9f28SMartin Blumenstingl 
40543f2ebd5SVinod Koul static int at803x_disable_tx_delay(struct phy_device *phydev)
4062e5f9f28SMartin Blumenstingl {
40767999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE,
408cd28d1d6SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN, 0);
4092e5f9f28SMartin Blumenstingl }
4102e5f9f28SMartin Blumenstingl 
41113a56b44SDaniel Mack /* save relevant PHY registers to private copy */
41213a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev,
41313a56b44SDaniel Mack 				struct at803x_context *context)
41413a56b44SDaniel Mack {
41513a56b44SDaniel Mack 	context->bmcr = phy_read(phydev, MII_BMCR);
41613a56b44SDaniel Mack 	context->advertise = phy_read(phydev, MII_ADVERTISE);
41713a56b44SDaniel Mack 	context->control1000 = phy_read(phydev, MII_CTRL1000);
41813a56b44SDaniel Mack 	context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
41913a56b44SDaniel Mack 	context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
42013a56b44SDaniel Mack 	context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
42113a56b44SDaniel Mack }
42213a56b44SDaniel Mack 
42313a56b44SDaniel Mack /* restore relevant PHY registers from private copy */
42413a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev,
42513a56b44SDaniel Mack 				   const struct at803x_context *context)
42613a56b44SDaniel Mack {
42713a56b44SDaniel Mack 	phy_write(phydev, MII_BMCR, context->bmcr);
42813a56b44SDaniel Mack 	phy_write(phydev, MII_ADVERTISE, context->advertise);
42913a56b44SDaniel Mack 	phy_write(phydev, MII_CTRL1000, context->control1000);
43013a56b44SDaniel Mack 	phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
43113a56b44SDaniel Mack 	phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
43213a56b44SDaniel Mack 	phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
43313a56b44SDaniel Mack }
43413a56b44SDaniel Mack 
435ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev,
436ea13c9eeSMugunthan V N 			  struct ethtool_wolinfo *wol)
4370ca7111aSMatus Ujhelyi {
438d7cd5e06SViorel Suman 	int ret, irq_enabled;
439d7cd5e06SViorel Suman 
440d7cd5e06SViorel Suman 	if (wol->wolopts & WAKE_MAGIC) {
4410ca7111aSMatus Ujhelyi 		struct net_device *ndev = phydev->attached_dev;
4420ca7111aSMatus Ujhelyi 		const u8 *mac;
443c0f0b563SLuo Jie 		unsigned int i;
444edcb501eSColin Ian King 		static const unsigned int offsets[] = {
4450ca7111aSMatus Ujhelyi 			AT803X_LOC_MAC_ADDR_32_47_OFFSET,
4460ca7111aSMatus Ujhelyi 			AT803X_LOC_MAC_ADDR_16_31_OFFSET,
4470ca7111aSMatus Ujhelyi 			AT803X_LOC_MAC_ADDR_0_15_OFFSET,
4480ca7111aSMatus Ujhelyi 		};
4490ca7111aSMatus Ujhelyi 
4500ca7111aSMatus Ujhelyi 		if (!ndev)
451ea13c9eeSMugunthan V N 			return -ENODEV;
4520ca7111aSMatus Ujhelyi 
4530ca7111aSMatus Ujhelyi 		mac = (const u8 *) ndev->dev_addr;
4540ca7111aSMatus Ujhelyi 
4550ca7111aSMatus Ujhelyi 		if (!is_valid_ether_addr(mac))
456fc755687SDan Murphy 			return -EINVAL;
4570ca7111aSMatus Ujhelyi 
4580e021396SCarlo Caione 		for (i = 0; i < 3; i++)
459c0f0b563SLuo Jie 			phy_write_mmd(phydev, MDIO_MMD_PCS, offsets[i],
4600ca7111aSMatus Ujhelyi 				      mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
461ea13c9eeSMugunthan V N 
4627beecaf7SLuo Jie 		/* Enable WOL function */
4637beecaf7SLuo Jie 		ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL,
4647beecaf7SLuo Jie 				0, AT803X_WOL_EN);
4657beecaf7SLuo Jie 		if (ret)
4667beecaf7SLuo Jie 			return ret;
4677beecaf7SLuo Jie 		/* Enable WOL interrupt */
4682d4284e8SLuo Jie 		ret = phy_modify(phydev, AT803X_INTR_ENABLE, 0, AT803X_INTR_ENABLE_WOL);
469ea13c9eeSMugunthan V N 		if (ret)
470ea13c9eeSMugunthan V N 			return ret;
471ea13c9eeSMugunthan V N 	} else {
4727beecaf7SLuo Jie 		/* Disable WoL function */
4737beecaf7SLuo Jie 		ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL,
4747beecaf7SLuo Jie 				AT803X_WOL_EN, 0);
4757beecaf7SLuo Jie 		if (ret)
4767beecaf7SLuo Jie 			return ret;
4777beecaf7SLuo Jie 		/* Disable WOL interrupt */
4782d4284e8SLuo Jie 		ret = phy_modify(phydev, AT803X_INTR_ENABLE, AT803X_INTR_ENABLE_WOL, 0);
479ea13c9eeSMugunthan V N 		if (ret)
480ea13c9eeSMugunthan V N 			return ret;
481ea13c9eeSMugunthan V N 	}
482ea13c9eeSMugunthan V N 
4837beecaf7SLuo Jie 	/* Clear WOL status */
4847beecaf7SLuo Jie 	ret = phy_read(phydev, AT803X_INTR_STATUS);
4857beecaf7SLuo Jie 	if (ret < 0)
486ea13c9eeSMugunthan V N 		return ret;
4877beecaf7SLuo Jie 
4887beecaf7SLuo Jie 	/* Check if there are other interrupts except for WOL triggered when PHY is
4897beecaf7SLuo Jie 	 * in interrupt mode, only the interrupts enabled by AT803X_INTR_ENABLE can
4907beecaf7SLuo Jie 	 * be passed up to the interrupt PIN.
4917beecaf7SLuo Jie 	 */
4927beecaf7SLuo Jie 	irq_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
4937beecaf7SLuo Jie 	if (irq_enabled < 0)
4947beecaf7SLuo Jie 		return irq_enabled;
4957beecaf7SLuo Jie 
4967beecaf7SLuo Jie 	irq_enabled &= ~AT803X_INTR_ENABLE_WOL;
4977beecaf7SLuo Jie 	if (ret & irq_enabled && !phy_polling_mode(phydev))
4987beecaf7SLuo Jie 		phy_trigger_machine(phydev);
4997beecaf7SLuo Jie 
5007beecaf7SLuo Jie 	return 0;
501ea13c9eeSMugunthan V N }
502ea13c9eeSMugunthan V N 
503ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev,
504ea13c9eeSMugunthan V N 			   struct ethtool_wolinfo *wol)
505ea13c9eeSMugunthan V N {
506911e3a46SJiapeng Chong 	int value;
507ea13c9eeSMugunthan V N 
508ea13c9eeSMugunthan V N 	wol->supported = WAKE_MAGIC;
509ea13c9eeSMugunthan V N 	wol->wolopts = 0;
510ea13c9eeSMugunthan V N 
5117beecaf7SLuo Jie 	value = phy_read_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL);
5127beecaf7SLuo Jie 	if (value < 0)
5137beecaf7SLuo Jie 		return;
5147beecaf7SLuo Jie 
5157beecaf7SLuo Jie 	if (value & AT803X_WOL_EN)
516ea13c9eeSMugunthan V N 		wol->wolopts |= WAKE_MAGIC;
5170ca7111aSMatus Ujhelyi }
5180ca7111aSMatus Ujhelyi 
519272833b9SAnsuel Smith static int at803x_get_sset_count(struct phy_device *phydev)
520272833b9SAnsuel Smith {
521272833b9SAnsuel Smith 	return ARRAY_SIZE(at803x_hw_stats);
522272833b9SAnsuel Smith }
523272833b9SAnsuel Smith 
524272833b9SAnsuel Smith static void at803x_get_strings(struct phy_device *phydev, u8 *data)
525272833b9SAnsuel Smith {
526272833b9SAnsuel Smith 	int i;
527272833b9SAnsuel Smith 
528272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) {
529272833b9SAnsuel Smith 		strscpy(data + i * ETH_GSTRING_LEN,
530272833b9SAnsuel Smith 			at803x_hw_stats[i].string, ETH_GSTRING_LEN);
531272833b9SAnsuel Smith 	}
532272833b9SAnsuel Smith }
533272833b9SAnsuel Smith 
534272833b9SAnsuel Smith static u64 at803x_get_stat(struct phy_device *phydev, int i)
535272833b9SAnsuel Smith {
536272833b9SAnsuel Smith 	struct at803x_hw_stat stat = at803x_hw_stats[i];
537272833b9SAnsuel Smith 	struct at803x_priv *priv = phydev->priv;
538272833b9SAnsuel Smith 	int val;
539272833b9SAnsuel Smith 	u64 ret;
540272833b9SAnsuel Smith 
541272833b9SAnsuel Smith 	if (stat.access_type == MMD)
542272833b9SAnsuel Smith 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg);
543272833b9SAnsuel Smith 	else
544272833b9SAnsuel Smith 		val = phy_read(phydev, stat.reg);
545272833b9SAnsuel Smith 
546272833b9SAnsuel Smith 	if (val < 0) {
547272833b9SAnsuel Smith 		ret = U64_MAX;
548272833b9SAnsuel Smith 	} else {
549272833b9SAnsuel Smith 		val = val & stat.mask;
550272833b9SAnsuel Smith 		priv->stats[i] += val;
551272833b9SAnsuel Smith 		ret = priv->stats[i];
552272833b9SAnsuel Smith 	}
553272833b9SAnsuel Smith 
554272833b9SAnsuel Smith 	return ret;
555272833b9SAnsuel Smith }
556272833b9SAnsuel Smith 
557272833b9SAnsuel Smith static void at803x_get_stats(struct phy_device *phydev,
558272833b9SAnsuel Smith 			     struct ethtool_stats *stats, u64 *data)
559272833b9SAnsuel Smith {
560272833b9SAnsuel Smith 	int i;
561272833b9SAnsuel Smith 
562272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++)
563272833b9SAnsuel Smith 		data[i] = at803x_get_stat(phydev, i);
564272833b9SAnsuel Smith }
565272833b9SAnsuel Smith 
5666229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev)
5676229ed1fSDaniel Mack {
5686229ed1fSDaniel Mack 	int value;
5696229ed1fSDaniel Mack 	int wol_enabled;
5706229ed1fSDaniel Mack 
5716229ed1fSDaniel Mack 	value = phy_read(phydev, AT803X_INTR_ENABLE);
572e6e4a556SMartin Blumenstingl 	wol_enabled = value & AT803X_INTR_ENABLE_WOL;
5736229ed1fSDaniel Mack 
5746229ed1fSDaniel Mack 	if (wol_enabled)
575fea23fb5SRussell King 		value = BMCR_ISOLATE;
5766229ed1fSDaniel Mack 	else
577fea23fb5SRussell King 		value = BMCR_PDOWN;
5786229ed1fSDaniel Mack 
579fea23fb5SRussell King 	phy_modify(phydev, MII_BMCR, 0, value);
5806229ed1fSDaniel Mack 
5816229ed1fSDaniel Mack 	return 0;
5826229ed1fSDaniel Mack }
5836229ed1fSDaniel Mack 
5846229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev)
5856229ed1fSDaniel Mack {
586f102852fSRussell King 	return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
5876229ed1fSDaniel Mack }
5886229ed1fSDaniel Mack 
5892f664823SMichael Walle static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
5902f664823SMichael Walle 					    unsigned int selector)
5912f664823SMichael Walle {
5922f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
5932f664823SMichael Walle 
5942f664823SMichael Walle 	if (selector)
5952f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
5962f664823SMichael Walle 					     0, AT803X_DEBUG_RGMII_1V8);
5972f664823SMichael Walle 	else
5982f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
5992f664823SMichael Walle 					     AT803X_DEBUG_RGMII_1V8, 0);
6002f664823SMichael Walle }
6012f664823SMichael Walle 
6022f664823SMichael Walle static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
6032f664823SMichael Walle {
6042f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
6052f664823SMichael Walle 	int val;
6062f664823SMichael Walle 
6072f664823SMichael Walle 	val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
6082f664823SMichael Walle 	if (val < 0)
6092f664823SMichael Walle 		return val;
6102f664823SMichael Walle 
6112f664823SMichael Walle 	return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
6122f664823SMichael Walle }
6132f664823SMichael Walle 
6143faaf539SRikard Falkeborn static const struct regulator_ops vddio_regulator_ops = {
6152f664823SMichael Walle 	.list_voltage = regulator_list_voltage_table,
6162f664823SMichael Walle 	.set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
6172f664823SMichael Walle 	.get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
6182f664823SMichael Walle };
6192f664823SMichael Walle 
6202f664823SMichael Walle static const unsigned int vddio_voltage_table[] = {
6212f664823SMichael Walle 	1500000,
6222f664823SMichael Walle 	1800000,
6232f664823SMichael Walle };
6242f664823SMichael Walle 
6252f664823SMichael Walle static const struct regulator_desc vddio_desc = {
6262f664823SMichael Walle 	.name = "vddio",
6272f664823SMichael Walle 	.of_match = of_match_ptr("vddio-regulator"),
6282f664823SMichael Walle 	.n_voltages = ARRAY_SIZE(vddio_voltage_table),
6292f664823SMichael Walle 	.volt_table = vddio_voltage_table,
6302f664823SMichael Walle 	.ops = &vddio_regulator_ops,
6312f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
6322f664823SMichael Walle 	.owner = THIS_MODULE,
6332f664823SMichael Walle };
6342f664823SMichael Walle 
6353faaf539SRikard Falkeborn static const struct regulator_ops vddh_regulator_ops = {
6362f664823SMichael Walle };
6372f664823SMichael Walle 
6382f664823SMichael Walle static const struct regulator_desc vddh_desc = {
6392f664823SMichael Walle 	.name = "vddh",
6402f664823SMichael Walle 	.of_match = of_match_ptr("vddh-regulator"),
6412f664823SMichael Walle 	.n_voltages = 1,
6422f664823SMichael Walle 	.fixed_uV = 2500000,
6432f664823SMichael Walle 	.ops = &vddh_regulator_ops,
6442f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
6452f664823SMichael Walle 	.owner = THIS_MODULE,
6462f664823SMichael Walle };
6472f664823SMichael Walle 
6482f664823SMichael Walle static int at8031_register_regulators(struct phy_device *phydev)
6492f664823SMichael Walle {
6502f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
6512f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
6522f664823SMichael Walle 	struct regulator_config config = { };
6532f664823SMichael Walle 
6542f664823SMichael Walle 	config.dev = dev;
6552f664823SMichael Walle 	config.driver_data = phydev;
6562f664823SMichael Walle 
6572f664823SMichael Walle 	priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
6582f664823SMichael Walle 	if (IS_ERR(priv->vddio_rdev)) {
6592f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDIO regulator\n");
6602f664823SMichael Walle 		return PTR_ERR(priv->vddio_rdev);
6612f664823SMichael Walle 	}
6622f664823SMichael Walle 
6632f664823SMichael Walle 	priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
6642f664823SMichael Walle 	if (IS_ERR(priv->vddh_rdev)) {
6652f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDH regulator\n");
6662f664823SMichael Walle 		return PTR_ERR(priv->vddh_rdev);
6672f664823SMichael Walle 	}
6682f664823SMichael Walle 
6692f664823SMichael Walle 	return 0;
6702f664823SMichael Walle }
6712f664823SMichael Walle 
672dc4d5fccSRobert Hancock static int at803x_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
673dc4d5fccSRobert Hancock {
674dc4d5fccSRobert Hancock 	struct phy_device *phydev = upstream;
675dc4d5fccSRobert Hancock 	__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_support);
676dc4d5fccSRobert Hancock 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
677fd580c98SRussell King 	DECLARE_PHY_INTERFACE_MASK(interfaces);
678dc4d5fccSRobert Hancock 	phy_interface_t iface;
679dc4d5fccSRobert Hancock 
680dc4d5fccSRobert Hancock 	linkmode_zero(phy_support);
681dc4d5fccSRobert Hancock 	phylink_set(phy_support, 1000baseX_Full);
682dc4d5fccSRobert Hancock 	phylink_set(phy_support, 1000baseT_Full);
683dc4d5fccSRobert Hancock 	phylink_set(phy_support, Autoneg);
684dc4d5fccSRobert Hancock 	phylink_set(phy_support, Pause);
685dc4d5fccSRobert Hancock 	phylink_set(phy_support, Asym_Pause);
686dc4d5fccSRobert Hancock 
687dc4d5fccSRobert Hancock 	linkmode_zero(sfp_support);
688fd580c98SRussell King 	sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces);
689dc4d5fccSRobert Hancock 	/* Some modules support 10G modes as well as others we support.
690dc4d5fccSRobert Hancock 	 * Mask out non-supported modes so the correct interface is picked.
691dc4d5fccSRobert Hancock 	 */
692dc4d5fccSRobert Hancock 	linkmode_and(sfp_support, phy_support, sfp_support);
693dc4d5fccSRobert Hancock 
694dc4d5fccSRobert Hancock 	if (linkmode_empty(sfp_support)) {
695dc4d5fccSRobert Hancock 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
696dc4d5fccSRobert Hancock 		return -EINVAL;
697dc4d5fccSRobert Hancock 	}
698dc4d5fccSRobert Hancock 
699dc4d5fccSRobert Hancock 	iface = sfp_select_interface(phydev->sfp_bus, sfp_support);
700dc4d5fccSRobert Hancock 
701dc4d5fccSRobert Hancock 	/* Only 1000Base-X is supported by AR8031/8033 as the downstream SerDes
702dc4d5fccSRobert Hancock 	 * interface for use with SFP modules.
703dc4d5fccSRobert Hancock 	 * However, some copper modules detected as having a preferred SGMII
704dc4d5fccSRobert Hancock 	 * interface do default to and function in 1000Base-X mode, so just
705dc4d5fccSRobert Hancock 	 * print a warning and allow such modules, as they may have some chance
706dc4d5fccSRobert Hancock 	 * of working.
707dc4d5fccSRobert Hancock 	 */
708dc4d5fccSRobert Hancock 	if (iface == PHY_INTERFACE_MODE_SGMII)
709dc4d5fccSRobert Hancock 		dev_warn(&phydev->mdio.dev, "module may not function if 1000Base-X not supported\n");
710dc4d5fccSRobert Hancock 	else if (iface != PHY_INTERFACE_MODE_1000BASEX)
711dc4d5fccSRobert Hancock 		return -EINVAL;
712dc4d5fccSRobert Hancock 
713dc4d5fccSRobert Hancock 	return 0;
714dc4d5fccSRobert Hancock }
715dc4d5fccSRobert Hancock 
716dc4d5fccSRobert Hancock static const struct sfp_upstream_ops at803x_sfp_ops = {
717dc4d5fccSRobert Hancock 	.attach = phy_sfp_attach,
718dc4d5fccSRobert Hancock 	.detach = phy_sfp_detach,
719dc4d5fccSRobert Hancock 	.module_insert = at803x_sfp_insert,
720dc4d5fccSRobert Hancock };
721dc4d5fccSRobert Hancock 
7222f664823SMichael Walle static int at803x_parse_dt(struct phy_device *phydev)
7232f664823SMichael Walle {
7242f664823SMichael Walle 	struct device_node *node = phydev->mdio.dev.of_node;
7252f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
726390b4cadSRussell King 	u32 freq, strength, tw;
7273f2edd30SAndrew Lunn 	unsigned int sel;
7282f664823SMichael Walle 	int ret;
7292f664823SMichael Walle 
7302f664823SMichael Walle 	if (!IS_ENABLED(CONFIG_OF_MDIO))
7312f664823SMichael Walle 		return 0;
7322f664823SMichael Walle 
733390b4cadSRussell King 	if (of_property_read_bool(node, "qca,disable-smarteee"))
734390b4cadSRussell King 		priv->flags |= AT803X_DISABLE_SMARTEEE;
735390b4cadSRussell King 
7369ecf0401SWei Fang 	if (of_property_read_bool(node, "qca,disable-hibernation-mode"))
7379ecf0401SWei Fang 		priv->flags |= AT803X_DISABLE_HIBERNATION_MODE;
7389ecf0401SWei Fang 
739390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
740390b4cadSRussell King 		if (!tw || tw > 255) {
741390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
742390b4cadSRussell King 			return -EINVAL;
743390b4cadSRussell King 		}
744390b4cadSRussell King 		priv->smarteee_lpi_tw_1g = tw;
745390b4cadSRussell King 	}
746390b4cadSRussell King 
747390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
748390b4cadSRussell King 		if (!tw || tw > 255) {
749390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
750390b4cadSRussell King 			return -EINVAL;
751390b4cadSRussell King 		}
752390b4cadSRussell King 		priv->smarteee_lpi_tw_100m = tw;
753390b4cadSRussell King 	}
754390b4cadSRussell King 
7552f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
7562f664823SMichael Walle 	if (!ret) {
7572f664823SMichael Walle 		switch (freq) {
7582f664823SMichael Walle 		case 25000000:
7592f664823SMichael Walle 			sel = AT803X_CLK_OUT_25MHZ_XTAL;
7602f664823SMichael Walle 			break;
7612f664823SMichael Walle 		case 50000000:
7622f664823SMichael Walle 			sel = AT803X_CLK_OUT_50MHZ_PLL;
7632f664823SMichael Walle 			break;
7642f664823SMichael Walle 		case 62500000:
7652f664823SMichael Walle 			sel = AT803X_CLK_OUT_62_5MHZ_PLL;
7662f664823SMichael Walle 			break;
7672f664823SMichael Walle 		case 125000000:
7682f664823SMichael Walle 			sel = AT803X_CLK_OUT_125MHZ_PLL;
7692f664823SMichael Walle 			break;
7702f664823SMichael Walle 		default:
7712f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-frequency\n");
7722f664823SMichael Walle 			return -EINVAL;
7732f664823SMichael Walle 		}
7742f664823SMichael Walle 
7753f2edd30SAndrew Lunn 		priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
7763f2edd30SAndrew Lunn 		priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
7772f664823SMichael Walle 
7782f664823SMichael Walle 		/* Fixup for the AR8030/AR8035. This chip has another mask and
7792f664823SMichael Walle 		 * doesn't support the DSP reference. Eg. the lowest bit of the
7802f664823SMichael Walle 		 * mask. The upper two bits select the same frequencies. Mask
7812f664823SMichael Walle 		 * the lowest bit here.
7822f664823SMichael Walle 		 *
7832f664823SMichael Walle 		 * Warning:
7842f664823SMichael Walle 		 *   There was no datasheet for the AR8030 available so this is
7852f664823SMichael Walle 		 *   just a guess. But the AR8035 is listed as pin compatible
7862f664823SMichael Walle 		 *   to the AR8030 so there might be a good chance it works on
7872f664823SMichael Walle 		 *   the AR8030 too.
7882f664823SMichael Walle 		 */
7898887ca54SRussell King 		if (phydev->drv->phy_id == ATH8030_PHY_ID ||
7908887ca54SRussell King 		    phydev->drv->phy_id == ATH8035_PHY_ID) {
791b1f4c209SOleksij Rempel 			priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
792b1f4c209SOleksij Rempel 			priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
7932f664823SMichael Walle 		}
7942f664823SMichael Walle 	}
7952f664823SMichael Walle 
7962f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
7972f664823SMichael Walle 	if (!ret) {
7982f664823SMichael Walle 		priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
7992f664823SMichael Walle 		switch (strength) {
8002f664823SMichael Walle 		case AR803X_STRENGTH_FULL:
8012f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
8022f664823SMichael Walle 			break;
8032f664823SMichael Walle 		case AR803X_STRENGTH_HALF:
8042f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
8052f664823SMichael Walle 			break;
8062f664823SMichael Walle 		case AR803X_STRENGTH_QUARTER:
8072f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
8082f664823SMichael Walle 			break;
8092f664823SMichael Walle 		default:
8102f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-strength\n");
8112f664823SMichael Walle 			return -EINVAL;
8122f664823SMichael Walle 		}
8132f664823SMichael Walle 	}
8142f664823SMichael Walle 
815428061f7SMichael Walle 	/* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
816428061f7SMichael Walle 	 * options.
817428061f7SMichael Walle 	 */
8188887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
8192f664823SMichael Walle 		if (of_property_read_bool(node, "qca,keep-pll-enabled"))
8202f664823SMichael Walle 			priv->flags |= AT803X_KEEP_PLL_ENABLED;
8212f664823SMichael Walle 
8222f664823SMichael Walle 		ret = at8031_register_regulators(phydev);
8232f664823SMichael Walle 		if (ret < 0)
8242f664823SMichael Walle 			return ret;
8252f664823SMichael Walle 
826988e8d90SChristophe JAILLET 		ret = devm_regulator_get_enable_optional(&phydev->mdio.dev,
8272f664823SMichael Walle 							 "vddio");
828988e8d90SChristophe JAILLET 		if (ret) {
8292f664823SMichael Walle 			phydev_err(phydev, "failed to get VDDIO regulator\n");
830988e8d90SChristophe JAILLET 			return ret;
8312f664823SMichael Walle 		}
832dc4d5fccSRobert Hancock 
833dc4d5fccSRobert Hancock 		/* Only AR8031/8033 support 1000Base-X for SFP modules */
834dc4d5fccSRobert Hancock 		ret = phy_sfp_probe(phydev, &at803x_sfp_ops);
835dc4d5fccSRobert Hancock 		if (ret < 0)
836dc4d5fccSRobert Hancock 			return ret;
8372f664823SMichael Walle 	}
8382f664823SMichael Walle 
8392f664823SMichael Walle 	return 0;
8402f664823SMichael Walle }
8412f664823SMichael Walle 
8422f664823SMichael Walle static int at803x_probe(struct phy_device *phydev)
8432f664823SMichael Walle {
8442f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
8452f664823SMichael Walle 	struct at803x_priv *priv;
846c329e5afSDavid Bauer 	int ret;
8472f664823SMichael Walle 
8482f664823SMichael Walle 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
8492f664823SMichael Walle 	if (!priv)
8502f664823SMichael Walle 		return -ENOMEM;
8512f664823SMichael Walle 
8522f664823SMichael Walle 	phydev->priv = priv;
8532f664823SMichael Walle 
854c329e5afSDavid Bauer 	ret = at803x_parse_dt(phydev);
855c329e5afSDavid Bauer 	if (ret)
856c329e5afSDavid Bauer 		return ret;
857c329e5afSDavid Bauer 
8583265f421SRobert Hancock 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
8593265f421SRobert Hancock 		int ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
8603265f421SRobert Hancock 		int mode_cfg;
861d7cd5e06SViorel Suman 		struct ethtool_wolinfo wol = {
862d7cd5e06SViorel Suman 			.wolopts = 0,
863d7cd5e06SViorel Suman 		};
8643265f421SRobert Hancock 
865988e8d90SChristophe JAILLET 		if (ccr < 0)
866988e8d90SChristophe JAILLET 			return ccr;
8673265f421SRobert Hancock 		mode_cfg = ccr & AT803X_MODE_CFG_MASK;
8683265f421SRobert Hancock 
8693265f421SRobert Hancock 		switch (mode_cfg) {
8703265f421SRobert Hancock 		case AT803X_MODE_CFG_BX1000_RGMII_50OHM:
8713265f421SRobert Hancock 		case AT803X_MODE_CFG_BX1000_RGMII_75OHM:
8723265f421SRobert Hancock 			priv->is_1000basex = true;
8733265f421SRobert Hancock 			fallthrough;
8743265f421SRobert Hancock 		case AT803X_MODE_CFG_FX100_RGMII_50OHM:
8753265f421SRobert Hancock 		case AT803X_MODE_CFG_FX100_RGMII_75OHM:
8763265f421SRobert Hancock 			priv->is_fiber = true;
8773265f421SRobert Hancock 			break;
8783265f421SRobert Hancock 		}
879d7cd5e06SViorel Suman 
880d7cd5e06SViorel Suman 		/* Disable WOL by default */
881d7cd5e06SViorel Suman 		ret = at803x_set_wol(phydev, &wol);
882d7cd5e06SViorel Suman 		if (ret < 0) {
883d7cd5e06SViorel Suman 			phydev_err(phydev, "failed to disable WOL on probe: %d\n", ret);
884988e8d90SChristophe JAILLET 			return ret;
885d7cd5e06SViorel Suman 		}
8863265f421SRobert Hancock 	}
8873265f421SRobert Hancock 
8888f7e8762SMichael Walle 	return 0;
8892318ca8aSMichael Walle }
8902318ca8aSMichael Walle 
891b856150cSDavid Bauer static int at803x_get_features(struct phy_device *phydev)
892b856150cSDavid Bauer {
8933265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
894b856150cSDavid Bauer 	int err;
895b856150cSDavid Bauer 
896b856150cSDavid Bauer 	err = genphy_read_abilities(phydev);
897b856150cSDavid Bauer 	if (err)
898b856150cSDavid Bauer 		return err;
899b856150cSDavid Bauer 
900f5621a01SVladimir Oltean 	if (phydev->drv->phy_id != ATH8031_PHY_ID)
901b856150cSDavid Bauer 		return 0;
902b856150cSDavid Bauer 
903b856150cSDavid Bauer 	/* AR8031/AR8033 have different status registers
904b856150cSDavid Bauer 	 * for copper and fiber operation. However, the
905b856150cSDavid Bauer 	 * extended status register is the same for both
906b856150cSDavid Bauer 	 * operation modes.
907b856150cSDavid Bauer 	 *
908b856150cSDavid Bauer 	 * As a result of that, ESTATUS_1000_XFULL is set
909b856150cSDavid Bauer 	 * to 1 even when operating in copper TP mode.
910b856150cSDavid Bauer 	 *
9113265f421SRobert Hancock 	 * Remove this mode from the supported link modes
9123265f421SRobert Hancock 	 * when not operating in 1000BaseX mode.
913b856150cSDavid Bauer 	 */
9143265f421SRobert Hancock 	if (!priv->is_1000basex)
915b856150cSDavid Bauer 		linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
916b856150cSDavid Bauer 				   phydev->supported);
9173265f421SRobert Hancock 
918b856150cSDavid Bauer 	return 0;
919b856150cSDavid Bauer }
920b856150cSDavid Bauer 
921390b4cadSRussell King static int at803x_smarteee_config(struct phy_device *phydev)
922390b4cadSRussell King {
923390b4cadSRussell King 	struct at803x_priv *priv = phydev->priv;
924390b4cadSRussell King 	u16 mask = 0, val = 0;
925390b4cadSRussell King 	int ret;
926390b4cadSRussell King 
927390b4cadSRussell King 	if (priv->flags & AT803X_DISABLE_SMARTEEE)
928390b4cadSRussell King 		return phy_modify_mmd(phydev, MDIO_MMD_PCS,
929390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3,
930390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
931390b4cadSRussell King 
932390b4cadSRussell King 	if (priv->smarteee_lpi_tw_1g) {
933390b4cadSRussell King 		mask |= 0xff00;
934390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_1g << 8;
935390b4cadSRussell King 	}
936390b4cadSRussell King 	if (priv->smarteee_lpi_tw_100m) {
937390b4cadSRussell King 		mask |= 0x00ff;
938390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_100m;
939390b4cadSRussell King 	}
940390b4cadSRussell King 	if (!mask)
941390b4cadSRussell King 		return 0;
942390b4cadSRussell King 
943390b4cadSRussell King 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
944390b4cadSRussell King 			     mask, val);
945390b4cadSRussell King 	if (ret)
946390b4cadSRussell King 		return ret;
947390b4cadSRussell King 
948390b4cadSRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
949390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
950390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
951390b4cadSRussell King }
952390b4cadSRussell King 
9532f664823SMichael Walle static int at803x_clk_out_config(struct phy_device *phydev)
9542f664823SMichael Walle {
9552f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
9562f664823SMichael Walle 
9572f664823SMichael Walle 	if (!priv->clk_25m_mask)
9582f664823SMichael Walle 		return 0;
9592f664823SMichael Walle 
960a45c1c10SRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
961a45c1c10SRussell King 			      priv->clk_25m_mask, priv->clk_25m_reg);
9622f664823SMichael Walle }
9632f664823SMichael Walle 
9642f664823SMichael Walle static int at8031_pll_config(struct phy_device *phydev)
9652f664823SMichael Walle {
9662f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
9672f664823SMichael Walle 
9682f664823SMichael Walle 	/* The default after hardware reset is PLL OFF. After a soft reset, the
9692f664823SMichael Walle 	 * values are retained.
9702f664823SMichael Walle 	 */
9712f664823SMichael Walle 	if (priv->flags & AT803X_KEEP_PLL_ENABLED)
9722f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
9732f664823SMichael Walle 					     0, AT803X_DEBUG_PLL_ON);
9742f664823SMichael Walle 	else
9752f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
9762f664823SMichael Walle 					     AT803X_DEBUG_PLL_ON, 0);
9772f664823SMichael Walle }
9782f664823SMichael Walle 
9799ecf0401SWei Fang static int at803x_hibernation_mode_config(struct phy_device *phydev)
9809ecf0401SWei Fang {
9819ecf0401SWei Fang 	struct at803x_priv *priv = phydev->priv;
9829ecf0401SWei Fang 
9839ecf0401SWei Fang 	/* The default after hardware reset is hibernation mode enabled. After
9849ecf0401SWei Fang 	 * software reset, the value is retained.
9859ecf0401SWei Fang 	 */
9869ecf0401SWei Fang 	if (!(priv->flags & AT803X_DISABLE_HIBERNATION_MODE))
9879ecf0401SWei Fang 		return 0;
9889ecf0401SWei Fang 
9899ecf0401SWei Fang 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
9909ecf0401SWei Fang 					 AT803X_DEBUG_HIB_CTRL_PS_HIB_EN, 0);
9919ecf0401SWei Fang }
9929ecf0401SWei Fang 
9930ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev)
9940ca7111aSMatus Ujhelyi {
9953265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
9961ca6d1b1SMugunthan V N 	int ret;
9970ca7111aSMatus Ujhelyi 
9984f3a00c7SRobert Hancock 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
9994f3a00c7SRobert Hancock 		/* Some bootloaders leave the fiber page selected.
10003265f421SRobert Hancock 		 * Switch to the appropriate page (fiber or copper), as otherwise we
10013265f421SRobert Hancock 		 * read the PHY capabilities from the wrong page.
10024f3a00c7SRobert Hancock 		 */
10034f3a00c7SRobert Hancock 		phy_lock_mdio_bus(phydev);
10043265f421SRobert Hancock 		ret = at803x_write_page(phydev,
10053265f421SRobert Hancock 					priv->is_fiber ? AT803X_PAGE_FIBER :
10063265f421SRobert Hancock 							 AT803X_PAGE_COPPER);
10074f3a00c7SRobert Hancock 		phy_unlock_mdio_bus(phydev);
10084f3a00c7SRobert Hancock 		if (ret)
10094f3a00c7SRobert Hancock 			return ret;
10104f3a00c7SRobert Hancock 
10114f3a00c7SRobert Hancock 		ret = at8031_pll_config(phydev);
10124f3a00c7SRobert Hancock 		if (ret < 0)
10134f3a00c7SRobert Hancock 			return ret;
10144f3a00c7SRobert Hancock 	}
10154f3a00c7SRobert Hancock 
10166d4cd041SVinod Koul 	/* The RX and TX delay default is:
10176d4cd041SVinod Koul 	 *   after HW reset: RX delay enabled and TX delay disabled
10186d4cd041SVinod Koul 	 *   after SW reset: RX delay enabled, while TX delay retains the
10196d4cd041SVinod Koul 	 *   value before reset.
10206d4cd041SVinod Koul 	 */
1021bb0ce4c1SAndré Draszik 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1022bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1023bb0ce4c1SAndré Draszik 		ret = at803x_enable_rx_delay(phydev);
1024bb0ce4c1SAndré Draszik 	else
1025cd28d1d6SVinod Koul 		ret = at803x_disable_rx_delay(phydev);
10262e5f9f28SMartin Blumenstingl 	if (ret < 0)
10271ca6d1b1SMugunthan V N 		return ret;
10286d4cd041SVinod Koul 
10296d4cd041SVinod Koul 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1030bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
10316d4cd041SVinod Koul 		ret = at803x_enable_tx_delay(phydev);
1032bb0ce4c1SAndré Draszik 	else
1033bb0ce4c1SAndré Draszik 		ret = at803x_disable_tx_delay(phydev);
10342f664823SMichael Walle 	if (ret < 0)
10356d4cd041SVinod Koul 		return ret;
10362f664823SMichael Walle 
1037390b4cadSRussell King 	ret = at803x_smarteee_config(phydev);
1038390b4cadSRussell King 	if (ret < 0)
1039390b4cadSRussell King 		return ret;
1040390b4cadSRussell King 
10412f664823SMichael Walle 	ret = at803x_clk_out_config(phydev);
10422f664823SMichael Walle 	if (ret < 0)
10432f664823SMichael Walle 		return ret;
10442f664823SMichael Walle 
10459ecf0401SWei Fang 	ret = at803x_hibernation_mode_config(phydev);
10469ecf0401SWei Fang 	if (ret < 0)
10479ecf0401SWei Fang 		return ret;
10489ecf0401SWei Fang 
10493c51fa5dSRussell King 	/* Ar803x extended next page bit is enabled by default. Cisco
10503c51fa5dSRussell King 	 * multigig switches read this bit and attempt to negotiate 10Gbps
10513c51fa5dSRussell King 	 * rates even if the next page bit is disabled. This is incorrect
10523c51fa5dSRussell King 	 * behaviour but we still need to accommodate it. XNP is only needed
10533c51fa5dSRussell King 	 * for 10Gbps support, so disable XNP.
10543c51fa5dSRussell King 	 */
10553c51fa5dSRussell King 	return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
10560ca7111aSMatus Ujhelyi }
10570ca7111aSMatus Ujhelyi 
105877a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev)
105977a99394SZhao Qiang {
106077a99394SZhao Qiang 	int err;
106177a99394SZhao Qiang 
1062a46bd63bSMartin Blumenstingl 	err = phy_read(phydev, AT803X_INTR_STATUS);
106377a99394SZhao Qiang 
106477a99394SZhao Qiang 	return (err < 0) ? err : 0;
106577a99394SZhao Qiang }
106677a99394SZhao Qiang 
106777a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev)
106877a99394SZhao Qiang {
10693265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
107077a99394SZhao Qiang 	int err;
107177a99394SZhao Qiang 	int value;
107277a99394SZhao Qiang 
1073a46bd63bSMartin Blumenstingl 	value = phy_read(phydev, AT803X_INTR_ENABLE);
107477a99394SZhao Qiang 
1075e6e4a556SMartin Blumenstingl 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1076a3417885SIoana Ciornei 		/* Clear any pending interrupts */
1077a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
1078a3417885SIoana Ciornei 		if (err)
1079a3417885SIoana Ciornei 			return err;
1080a3417885SIoana Ciornei 
1081e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
1082e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
1083e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
1084e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_FAIL;
1085e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
10863265f421SRobert Hancock 		if (priv->is_fiber) {
10873265f421SRobert Hancock 			value |= AT803X_INTR_ENABLE_LINK_FAIL_BX;
10883265f421SRobert Hancock 			value |= AT803X_INTR_ENABLE_LINK_SUCCESS_BX;
10893265f421SRobert Hancock 		}
1090e6e4a556SMartin Blumenstingl 
1091e6e4a556SMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, value);
1092a3417885SIoana Ciornei 	} else {
1093a46bd63bSMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
1094a3417885SIoana Ciornei 		if (err)
1095a3417885SIoana Ciornei 			return err;
1096a3417885SIoana Ciornei 
1097a3417885SIoana Ciornei 		/* Clear any pending interrupts */
1098a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
1099a3417885SIoana Ciornei 	}
110077a99394SZhao Qiang 
110177a99394SZhao Qiang 	return err;
110277a99394SZhao Qiang }
110377a99394SZhao Qiang 
110429773097SIoana Ciornei static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev)
110529773097SIoana Ciornei {
110629773097SIoana Ciornei 	int irq_status, int_enabled;
110729773097SIoana Ciornei 
110829773097SIoana Ciornei 	irq_status = phy_read(phydev, AT803X_INTR_STATUS);
110929773097SIoana Ciornei 	if (irq_status < 0) {
111029773097SIoana Ciornei 		phy_error(phydev);
111129773097SIoana Ciornei 		return IRQ_NONE;
111229773097SIoana Ciornei 	}
111329773097SIoana Ciornei 
111429773097SIoana Ciornei 	/* Read the current enabled interrupts */
111529773097SIoana Ciornei 	int_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
111629773097SIoana Ciornei 	if (int_enabled < 0) {
111729773097SIoana Ciornei 		phy_error(phydev);
111829773097SIoana Ciornei 		return IRQ_NONE;
111929773097SIoana Ciornei 	}
112029773097SIoana Ciornei 
112129773097SIoana Ciornei 	/* See if this was one of our enabled interrupts */
112229773097SIoana Ciornei 	if (!(irq_status & int_enabled))
112329773097SIoana Ciornei 		return IRQ_NONE;
112429773097SIoana Ciornei 
112529773097SIoana Ciornei 	phy_trigger_machine(phydev);
112629773097SIoana Ciornei 
112729773097SIoana Ciornei 	return IRQ_HANDLED;
112829773097SIoana Ciornei }
112929773097SIoana Ciornei 
113013a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev)
113113a56b44SDaniel Mack {
113213a56b44SDaniel Mack 	/*
113313a56b44SDaniel Mack 	 * Conduct a hardware reset for AT8030 every time a link loss is
113413a56b44SDaniel Mack 	 * signalled. This is necessary to circumvent a hardware bug that
113513a56b44SDaniel Mack 	 * occurs when the cable is unplugged while TX packets are pending
113613a56b44SDaniel Mack 	 * in the FIFO. In such cases, the FIFO enters an error mode it
113713a56b44SDaniel Mack 	 * cannot recover from by software.
113813a56b44SDaniel Mack 	 */
11396110ed2dSDavid Bauer 	if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
114013a56b44SDaniel Mack 		struct at803x_context context;
114113a56b44SDaniel Mack 
114213a56b44SDaniel Mack 		at803x_context_save(phydev, &context);
114313a56b44SDaniel Mack 
1144bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 1);
114513a56b44SDaniel Mack 		msleep(1);
1146bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 0);
1147d57019d1SSergei Shtylyov 		msleep(1);
114813a56b44SDaniel Mack 
114913a56b44SDaniel Mack 		at803x_context_restore(phydev, &context);
115013a56b44SDaniel Mack 
11515c5f626bSHeiner Kallweit 		phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
115213a56b44SDaniel Mack 	}
115313a56b44SDaniel Mack }
115413a56b44SDaniel Mack 
115579c7bc05SLuo Jie static int at803x_read_specific_status(struct phy_device *phydev)
115606d5f344SRussell King {
115779c7bc05SLuo Jie 	int ss;
115806d5f344SRussell King 
115906d5f344SRussell King 	/* Read the AT8035 PHY-Specific Status register, which indicates the
116006d5f344SRussell King 	 * speed and duplex that the PHY is actually using, irrespective of
116106d5f344SRussell King 	 * whether we are in autoneg mode or not.
116206d5f344SRussell King 	 */
116306d5f344SRussell King 	ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
116406d5f344SRussell King 	if (ss < 0)
116506d5f344SRussell King 		return ss;
116606d5f344SRussell King 
116706d5f344SRussell King 	if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
116879c7bc05SLuo Jie 		int sfc, speed;
11697dce80c2SOleksij Rempel 
11707dce80c2SOleksij Rempel 		sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL);
11717dce80c2SOleksij Rempel 		if (sfc < 0)
11727dce80c2SOleksij Rempel 			return sfc;
11737dce80c2SOleksij Rempel 
117479c7bc05SLuo Jie 		/* qca8081 takes the different bits for speed value from at803x */
117579c7bc05SLuo Jie 		if (phydev->drv->phy_id == QCA8081_PHY_ID)
117679c7bc05SLuo Jie 			speed = FIELD_GET(QCA808X_SS_SPEED_MASK, ss);
117779c7bc05SLuo Jie 		else
117879c7bc05SLuo Jie 			speed = FIELD_GET(AT803X_SS_SPEED_MASK, ss);
117979c7bc05SLuo Jie 
118079c7bc05SLuo Jie 		switch (speed) {
118106d5f344SRussell King 		case AT803X_SS_SPEED_10:
118206d5f344SRussell King 			phydev->speed = SPEED_10;
118306d5f344SRussell King 			break;
118406d5f344SRussell King 		case AT803X_SS_SPEED_100:
118506d5f344SRussell King 			phydev->speed = SPEED_100;
118606d5f344SRussell King 			break;
118706d5f344SRussell King 		case AT803X_SS_SPEED_1000:
118806d5f344SRussell King 			phydev->speed = SPEED_1000;
118906d5f344SRussell King 			break;
119079c7bc05SLuo Jie 		case QCA808X_SS_SPEED_2500:
119179c7bc05SLuo Jie 			phydev->speed = SPEED_2500;
119279c7bc05SLuo Jie 			break;
119306d5f344SRussell King 		}
119406d5f344SRussell King 		if (ss & AT803X_SS_DUPLEX)
119506d5f344SRussell King 			phydev->duplex = DUPLEX_FULL;
119606d5f344SRussell King 		else
119706d5f344SRussell King 			phydev->duplex = DUPLEX_HALF;
11987dce80c2SOleksij Rempel 
119906d5f344SRussell King 		if (ss & AT803X_SS_MDIX)
120006d5f344SRussell King 			phydev->mdix = ETH_TP_MDI_X;
120106d5f344SRussell King 		else
120206d5f344SRussell King 			phydev->mdix = ETH_TP_MDI;
12037dce80c2SOleksij Rempel 
12047dce80c2SOleksij Rempel 		switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) {
12057dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDI:
12067dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
12077dce80c2SOleksij Rempel 			break;
12087dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDIX:
12097dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
12107dce80c2SOleksij Rempel 			break;
12117dce80c2SOleksij Rempel 		case AT803X_SFC_AUTOMATIC_CROSSOVER:
12127dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
12137dce80c2SOleksij Rempel 			break;
12147dce80c2SOleksij Rempel 		}
121506d5f344SRussell King 	}
121606d5f344SRussell King 
121779c7bc05SLuo Jie 	return 0;
121879c7bc05SLuo Jie }
121979c7bc05SLuo Jie 
122079c7bc05SLuo Jie static int at803x_read_status(struct phy_device *phydev)
122179c7bc05SLuo Jie {
12223265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
122379c7bc05SLuo Jie 	int err, old_link = phydev->link;
122479c7bc05SLuo Jie 
12253265f421SRobert Hancock 	if (priv->is_1000basex)
12263265f421SRobert Hancock 		return genphy_c37_read_status(phydev);
12273265f421SRobert Hancock 
122879c7bc05SLuo Jie 	/* Update the link, but return if there was an error */
122979c7bc05SLuo Jie 	err = genphy_update_link(phydev);
123079c7bc05SLuo Jie 	if (err)
123179c7bc05SLuo Jie 		return err;
123279c7bc05SLuo Jie 
123379c7bc05SLuo Jie 	/* why bother the PHY if nothing can have changed */
123479c7bc05SLuo Jie 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
123579c7bc05SLuo Jie 		return 0;
123679c7bc05SLuo Jie 
123779c7bc05SLuo Jie 	phydev->speed = SPEED_UNKNOWN;
123879c7bc05SLuo Jie 	phydev->duplex = DUPLEX_UNKNOWN;
123979c7bc05SLuo Jie 	phydev->pause = 0;
124079c7bc05SLuo Jie 	phydev->asym_pause = 0;
124179c7bc05SLuo Jie 
124279c7bc05SLuo Jie 	err = genphy_read_lpa(phydev);
124379c7bc05SLuo Jie 	if (err < 0)
124479c7bc05SLuo Jie 		return err;
124579c7bc05SLuo Jie 
124679c7bc05SLuo Jie 	err = at803x_read_specific_status(phydev);
124779c7bc05SLuo Jie 	if (err < 0)
124879c7bc05SLuo Jie 		return err;
124979c7bc05SLuo Jie 
125006d5f344SRussell King 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
125106d5f344SRussell King 		phy_resolve_aneg_pause(phydev);
125206d5f344SRussell King 
125306d5f344SRussell King 	return 0;
125406d5f344SRussell King }
125506d5f344SRussell King 
12567dce80c2SOleksij Rempel static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl)
12577dce80c2SOleksij Rempel {
12587dce80c2SOleksij Rempel 	u16 val;
12597dce80c2SOleksij Rempel 
12607dce80c2SOleksij Rempel 	switch (ctrl) {
12617dce80c2SOleksij Rempel 	case ETH_TP_MDI:
12627dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDI;
12637dce80c2SOleksij Rempel 		break;
12647dce80c2SOleksij Rempel 	case ETH_TP_MDI_X:
12657dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDIX;
12667dce80c2SOleksij Rempel 		break;
12677dce80c2SOleksij Rempel 	case ETH_TP_MDI_AUTO:
12687dce80c2SOleksij Rempel 		val = AT803X_SFC_AUTOMATIC_CROSSOVER;
12697dce80c2SOleksij Rempel 		break;
12707dce80c2SOleksij Rempel 	default:
12717dce80c2SOleksij Rempel 		return 0;
12727dce80c2SOleksij Rempel 	}
12737dce80c2SOleksij Rempel 
12747dce80c2SOleksij Rempel 	return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL,
12757dce80c2SOleksij Rempel 			  AT803X_SFC_MDI_CROSSOVER_MODE_M,
12767dce80c2SOleksij Rempel 			  FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val));
12777dce80c2SOleksij Rempel }
12787dce80c2SOleksij Rempel 
12797dce80c2SOleksij Rempel static int at803x_config_aneg(struct phy_device *phydev)
12807dce80c2SOleksij Rempel {
12813265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
12827dce80c2SOleksij Rempel 	int ret;
12837dce80c2SOleksij Rempel 
12847dce80c2SOleksij Rempel 	ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
12857dce80c2SOleksij Rempel 	if (ret < 0)
12867dce80c2SOleksij Rempel 		return ret;
12877dce80c2SOleksij Rempel 
12887dce80c2SOleksij Rempel 	/* Changes of the midx bits are disruptive to the normal operation;
12897dce80c2SOleksij Rempel 	 * therefore any changes to these registers must be followed by a
12907dce80c2SOleksij Rempel 	 * software reset to take effect.
12917dce80c2SOleksij Rempel 	 */
12927dce80c2SOleksij Rempel 	if (ret == 1) {
12937dce80c2SOleksij Rempel 		ret = genphy_soft_reset(phydev);
12947dce80c2SOleksij Rempel 		if (ret < 0)
12957dce80c2SOleksij Rempel 			return ret;
12967dce80c2SOleksij Rempel 	}
12977dce80c2SOleksij Rempel 
12983265f421SRobert Hancock 	if (priv->is_1000basex)
12993265f421SRobert Hancock 		return genphy_c37_config_aneg(phydev);
13003265f421SRobert Hancock 
1301f884d449SLuo Jie 	/* Do not restart auto-negotiation by setting ret to 0 defautly,
1302f884d449SLuo Jie 	 * when calling __genphy_config_aneg later.
1303f884d449SLuo Jie 	 */
1304f884d449SLuo Jie 	ret = 0;
1305f884d449SLuo Jie 
1306f884d449SLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID) {
1307f884d449SLuo Jie 		int phy_ctrl = 0;
1308f884d449SLuo Jie 
1309f884d449SLuo Jie 		/* The reg MII_BMCR also needs to be configured for force mode, the
1310f884d449SLuo Jie 		 * genphy_config_aneg is also needed.
1311f884d449SLuo Jie 		 */
1312f884d449SLuo Jie 		if (phydev->autoneg == AUTONEG_DISABLE)
1313f884d449SLuo Jie 			genphy_c45_pma_setup_forced(phydev);
1314f884d449SLuo Jie 
1315f884d449SLuo Jie 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->advertising))
1316f884d449SLuo Jie 			phy_ctrl = MDIO_AN_10GBT_CTRL_ADV2_5G;
1317f884d449SLuo Jie 
1318f884d449SLuo Jie 		ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
1319f884d449SLuo Jie 				MDIO_AN_10GBT_CTRL_ADV2_5G, phy_ctrl);
1320f884d449SLuo Jie 		if (ret < 0)
1321f884d449SLuo Jie 			return ret;
1322f884d449SLuo Jie 	}
1323f884d449SLuo Jie 
1324f884d449SLuo Jie 	return __genphy_config_aneg(phydev, ret);
13257dce80c2SOleksij Rempel }
13267dce80c2SOleksij Rempel 
1327cde0f4f8SMichael Walle static int at803x_get_downshift(struct phy_device *phydev, u8 *d)
1328cde0f4f8SMichael Walle {
1329cde0f4f8SMichael Walle 	int val;
1330cde0f4f8SMichael Walle 
1331cde0f4f8SMichael Walle 	val = phy_read(phydev, AT803X_SMART_SPEED);
1332cde0f4f8SMichael Walle 	if (val < 0)
1333cde0f4f8SMichael Walle 		return val;
1334cde0f4f8SMichael Walle 
1335cde0f4f8SMichael Walle 	if (val & AT803X_SMART_SPEED_ENABLE)
1336cde0f4f8SMichael Walle 		*d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2;
1337cde0f4f8SMichael Walle 	else
1338cde0f4f8SMichael Walle 		*d = DOWNSHIFT_DEV_DISABLE;
1339cde0f4f8SMichael Walle 
1340cde0f4f8SMichael Walle 	return 0;
1341cde0f4f8SMichael Walle }
1342cde0f4f8SMichael Walle 
1343cde0f4f8SMichael Walle static int at803x_set_downshift(struct phy_device *phydev, u8 cnt)
1344cde0f4f8SMichael Walle {
1345cde0f4f8SMichael Walle 	u16 mask, set;
1346cde0f4f8SMichael Walle 	int ret;
1347cde0f4f8SMichael Walle 
1348cde0f4f8SMichael Walle 	switch (cnt) {
1349cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DEFAULT_COUNT:
1350cde0f4f8SMichael Walle 		cnt = AT803X_DEFAULT_DOWNSHIFT;
1351cde0f4f8SMichael Walle 		fallthrough;
1352cde0f4f8SMichael Walle 	case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT:
1353cde0f4f8SMichael Walle 		set = AT803X_SMART_SPEED_ENABLE |
1354cde0f4f8SMichael Walle 		      AT803X_SMART_SPEED_BYPASS_TIMER |
1355cde0f4f8SMichael Walle 		      FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2);
1356cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK;
1357cde0f4f8SMichael Walle 		break;
1358cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DISABLE:
1359cde0f4f8SMichael Walle 		set = 0;
1360cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_ENABLE |
1361cde0f4f8SMichael Walle 		       AT803X_SMART_SPEED_BYPASS_TIMER;
1362cde0f4f8SMichael Walle 		break;
1363cde0f4f8SMichael Walle 	default:
1364cde0f4f8SMichael Walle 		return -EINVAL;
1365cde0f4f8SMichael Walle 	}
1366cde0f4f8SMichael Walle 
1367cde0f4f8SMichael Walle 	ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set);
1368cde0f4f8SMichael Walle 
1369cde0f4f8SMichael Walle 	/* After changing the smart speed settings, we need to perform a
1370cde0f4f8SMichael Walle 	 * software reset, use phy_init_hw() to make sure we set the
1371cde0f4f8SMichael Walle 	 * reapply any values which might got lost during software reset.
1372cde0f4f8SMichael Walle 	 */
1373cde0f4f8SMichael Walle 	if (ret == 1)
1374cde0f4f8SMichael Walle 		ret = phy_init_hw(phydev);
1375cde0f4f8SMichael Walle 
1376cde0f4f8SMichael Walle 	return ret;
1377cde0f4f8SMichael Walle }
1378cde0f4f8SMichael Walle 
1379cde0f4f8SMichael Walle static int at803x_get_tunable(struct phy_device *phydev,
1380cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, void *data)
1381cde0f4f8SMichael Walle {
1382cde0f4f8SMichael Walle 	switch (tuna->id) {
1383cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1384cde0f4f8SMichael Walle 		return at803x_get_downshift(phydev, data);
1385cde0f4f8SMichael Walle 	default:
1386cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1387cde0f4f8SMichael Walle 	}
1388cde0f4f8SMichael Walle }
1389cde0f4f8SMichael Walle 
1390cde0f4f8SMichael Walle static int at803x_set_tunable(struct phy_device *phydev,
1391cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, const void *data)
1392cde0f4f8SMichael Walle {
1393cde0f4f8SMichael Walle 	switch (tuna->id) {
1394cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1395cde0f4f8SMichael Walle 		return at803x_set_downshift(phydev, *(const u8 *)data);
1396cde0f4f8SMichael Walle 	default:
1397cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1398cde0f4f8SMichael Walle 	}
1399cde0f4f8SMichael Walle }
1400cde0f4f8SMichael Walle 
14016cb75767SMichael Walle static int at803x_cable_test_result_trans(u16 status)
14026cb75767SMichael Walle {
14036cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
14046cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_NORMAL:
14056cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
14066cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
14076cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
14086cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
14096cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
14106cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_FAIL:
14116cb75767SMichael Walle 	default:
14126cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
14136cb75767SMichael Walle 	}
14146cb75767SMichael Walle }
14156cb75767SMichael Walle 
14166cb75767SMichael Walle static bool at803x_cdt_test_failed(u16 status)
14176cb75767SMichael Walle {
14186cb75767SMichael Walle 	return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
14196cb75767SMichael Walle 		AT803X_CDT_STATUS_STAT_FAIL;
14206cb75767SMichael Walle }
14216cb75767SMichael Walle 
14226cb75767SMichael Walle static bool at803x_cdt_fault_length_valid(u16 status)
14236cb75767SMichael Walle {
14246cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
14256cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
14266cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
14276cb75767SMichael Walle 		return true;
14286cb75767SMichael Walle 	}
14296cb75767SMichael Walle 	return false;
14306cb75767SMichael Walle }
14316cb75767SMichael Walle 
14326cb75767SMichael Walle static int at803x_cdt_fault_length(u16 status)
14336cb75767SMichael Walle {
14346cb75767SMichael Walle 	int dt;
14356cb75767SMichael Walle 
14366cb75767SMichael Walle 	/* According to the datasheet the distance to the fault is
14376cb75767SMichael Walle 	 * DELTA_TIME * 0.824 meters.
14386cb75767SMichael Walle 	 *
14396cb75767SMichael Walle 	 * The author suspect the correct formula is:
14406cb75767SMichael Walle 	 *
14416cb75767SMichael Walle 	 *   fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2
14426cb75767SMichael Walle 	 *
14436cb75767SMichael Walle 	 * where c is the speed of light, VF is the velocity factor of
14446cb75767SMichael Walle 	 * the twisted pair cable, 125MHz the counter frequency and
14456cb75767SMichael Walle 	 * we need to divide by 2 because the hardware will measure the
14466cb75767SMichael Walle 	 * round trip time to the fault and back to the PHY.
14476cb75767SMichael Walle 	 *
14486cb75767SMichael Walle 	 * With a VF of 0.69 we get the factor 0.824 mentioned in the
14496cb75767SMichael Walle 	 * datasheet.
14506cb75767SMichael Walle 	 */
14516cb75767SMichael Walle 	dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status);
14526cb75767SMichael Walle 
14536cb75767SMichael Walle 	return (dt * 824) / 10;
14546cb75767SMichael Walle }
14556cb75767SMichael Walle 
14566cb75767SMichael Walle static int at803x_cdt_start(struct phy_device *phydev, int pair)
14576cb75767SMichael Walle {
14586cb75767SMichael Walle 	u16 cdt;
14596cb75767SMichael Walle 
14608c84d752SLuo Jie 	/* qca8081 takes the different bit 15 to enable CDT test */
14618c84d752SLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID)
14628c84d752SLuo Jie 		cdt = QCA808X_CDT_ENABLE_TEST |
14638c84d752SLuo Jie 			QCA808X_CDT_LENGTH_UNIT |
14648c84d752SLuo Jie 			QCA808X_CDT_INTER_CHECK_DIS;
14658c84d752SLuo Jie 	else
14666cb75767SMichael Walle 		cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
14676cb75767SMichael Walle 			AT803X_CDT_ENABLE_TEST;
14686cb75767SMichael Walle 
14696cb75767SMichael Walle 	return phy_write(phydev, AT803X_CDT, cdt);
14706cb75767SMichael Walle }
14716cb75767SMichael Walle 
14726cb75767SMichael Walle static int at803x_cdt_wait_for_completion(struct phy_device *phydev)
14736cb75767SMichael Walle {
14746cb75767SMichael Walle 	int val, ret;
14758c84d752SLuo Jie 	u16 cdt_en;
14768c84d752SLuo Jie 
14778c84d752SLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID)
14788c84d752SLuo Jie 		cdt_en = QCA808X_CDT_ENABLE_TEST;
14798c84d752SLuo Jie 	else
14808c84d752SLuo Jie 		cdt_en = AT803X_CDT_ENABLE_TEST;
14816cb75767SMichael Walle 
14826cb75767SMichael Walle 	/* One test run takes about 25ms */
14836cb75767SMichael Walle 	ret = phy_read_poll_timeout(phydev, AT803X_CDT, val,
14848c84d752SLuo Jie 				    !(val & cdt_en),
14856cb75767SMichael Walle 				    30000, 100000, true);
14866cb75767SMichael Walle 
14876cb75767SMichael Walle 	return ret < 0 ? ret : 0;
14886cb75767SMichael Walle }
14896cb75767SMichael Walle 
14906cb75767SMichael Walle static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
14916cb75767SMichael Walle {
14926cb75767SMichael Walle 	static const int ethtool_pair[] = {
14936cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_A,
14946cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_B,
14956cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_C,
14966cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_D,
14976cb75767SMichael Walle 	};
14986cb75767SMichael Walle 	int ret, val;
14996cb75767SMichael Walle 
15006cb75767SMichael Walle 	ret = at803x_cdt_start(phydev, pair);
15016cb75767SMichael Walle 	if (ret)
15026cb75767SMichael Walle 		return ret;
15036cb75767SMichael Walle 
15046cb75767SMichael Walle 	ret = at803x_cdt_wait_for_completion(phydev);
15056cb75767SMichael Walle 	if (ret)
15066cb75767SMichael Walle 		return ret;
15076cb75767SMichael Walle 
15086cb75767SMichael Walle 	val = phy_read(phydev, AT803X_CDT_STATUS);
15096cb75767SMichael Walle 	if (val < 0)
15106cb75767SMichael Walle 		return val;
15116cb75767SMichael Walle 
15126cb75767SMichael Walle 	if (at803x_cdt_test_failed(val))
15136cb75767SMichael Walle 		return 0;
15146cb75767SMichael Walle 
15156cb75767SMichael Walle 	ethnl_cable_test_result(phydev, ethtool_pair[pair],
15166cb75767SMichael Walle 				at803x_cable_test_result_trans(val));
15176cb75767SMichael Walle 
15186cb75767SMichael Walle 	if (at803x_cdt_fault_length_valid(val))
15196cb75767SMichael Walle 		ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
15206cb75767SMichael Walle 					      at803x_cdt_fault_length(val));
15216cb75767SMichael Walle 
15226cb75767SMichael Walle 	return 1;
15236cb75767SMichael Walle }
15246cb75767SMichael Walle 
15256cb75767SMichael Walle static int at803x_cable_test_get_status(struct phy_device *phydev,
15266cb75767SMichael Walle 					bool *finished)
15276cb75767SMichael Walle {
1528dc0f3ed1SOleksij Rempel 	unsigned long pair_mask;
15296cb75767SMichael Walle 	int retries = 20;
15306cb75767SMichael Walle 	int pair, ret;
15316cb75767SMichael Walle 
1532dc0f3ed1SOleksij Rempel 	if (phydev->phy_id == ATH9331_PHY_ID ||
1533fada2ce0SDavid Bauer 	    phydev->phy_id == ATH8032_PHY_ID ||
1534fada2ce0SDavid Bauer 	    phydev->phy_id == QCA9561_PHY_ID)
1535dc0f3ed1SOleksij Rempel 		pair_mask = 0x3;
1536dc0f3ed1SOleksij Rempel 	else
1537dc0f3ed1SOleksij Rempel 		pair_mask = 0xf;
1538dc0f3ed1SOleksij Rempel 
15396cb75767SMichael Walle 	*finished = false;
15406cb75767SMichael Walle 
15416cb75767SMichael Walle 	/* According to the datasheet the CDT can be performed when
15426cb75767SMichael Walle 	 * there is no link partner or when the link partner is
15436cb75767SMichael Walle 	 * auto-negotiating. Starting the test will restart the AN
15446cb75767SMichael Walle 	 * automatically. It seems that doing this repeatedly we will
15456cb75767SMichael Walle 	 * get a slot where our link partner won't disturb our
15466cb75767SMichael Walle 	 * measurement.
15476cb75767SMichael Walle 	 */
15486cb75767SMichael Walle 	while (pair_mask && retries--) {
15496cb75767SMichael Walle 		for_each_set_bit(pair, &pair_mask, 4) {
15506cb75767SMichael Walle 			ret = at803x_cable_test_one_pair(phydev, pair);
15516cb75767SMichael Walle 			if (ret < 0)
15526cb75767SMichael Walle 				return ret;
15536cb75767SMichael Walle 			if (ret)
15546cb75767SMichael Walle 				clear_bit(pair, &pair_mask);
15556cb75767SMichael Walle 		}
15566cb75767SMichael Walle 		if (pair_mask)
15576cb75767SMichael Walle 			msleep(250);
15586cb75767SMichael Walle 	}
15596cb75767SMichael Walle 
15606cb75767SMichael Walle 	*finished = true;
15616cb75767SMichael Walle 
15626cb75767SMichael Walle 	return 0;
15636cb75767SMichael Walle }
15646cb75767SMichael Walle 
15656cb75767SMichael Walle static int at803x_cable_test_start(struct phy_device *phydev)
15666cb75767SMichael Walle {
15676cb75767SMichael Walle 	/* Enable auto-negotiation, but advertise no capabilities, no link
15686cb75767SMichael Walle 	 * will be established. A restart of the auto-negotiation is not
15696cb75767SMichael Walle 	 * required, because the cable test will automatically break the link.
15706cb75767SMichael Walle 	 */
15716cb75767SMichael Walle 	phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
15726cb75767SMichael Walle 	phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
1573dc0f3ed1SOleksij Rempel 	if (phydev->phy_id != ATH9331_PHY_ID &&
1574fada2ce0SDavid Bauer 	    phydev->phy_id != ATH8032_PHY_ID &&
1575fada2ce0SDavid Bauer 	    phydev->phy_id != QCA9561_PHY_ID)
15766cb75767SMichael Walle 		phy_write(phydev, MII_CTRL1000, 0);
15776cb75767SMichael Walle 
15786cb75767SMichael Walle 	/* we do all the (time consuming) work later */
15796cb75767SMichael Walle 	return 0;
15806cb75767SMichael Walle }
15816cb75767SMichael Walle 
1582272833b9SAnsuel Smith static int qca83xx_config_init(struct phy_device *phydev)
1583272833b9SAnsuel Smith {
1584272833b9SAnsuel Smith 	u8 switch_revision;
1585272833b9SAnsuel Smith 
1586272833b9SAnsuel Smith 	switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK;
1587272833b9SAnsuel Smith 
1588272833b9SAnsuel Smith 	switch (switch_revision) {
1589272833b9SAnsuel Smith 	case 1:
1590272833b9SAnsuel Smith 		/* For 100M waveform */
159167999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0x02ea);
1592272833b9SAnsuel Smith 		/* Turn on Gigabit clock */
159367999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x68a0);
1594272833b9SAnsuel Smith 		break;
1595272833b9SAnsuel Smith 
1596272833b9SAnsuel Smith 	case 2:
1597272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0);
1598272833b9SAnsuel Smith 		fallthrough;
1599272833b9SAnsuel Smith 	case 4:
1600272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f);
160167999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x6860);
160267999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0x2c46);
1603272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000);
1604272833b9SAnsuel Smith 		break;
1605272833b9SAnsuel Smith 	}
1606272833b9SAnsuel Smith 
16071ca83119SAnsuel Smith 	/* QCA8327 require DAC amplitude adjustment for 100m set to +6%.
16081ca83119SAnsuel Smith 	 * Disable on init and enable only with 100m speed following
16091ca83119SAnsuel Smith 	 * qca original source code.
16101ca83119SAnsuel Smith 	 */
16111ca83119SAnsuel Smith 	if (phydev->drv->phy_id == QCA8327_A_PHY_ID ||
16121ca83119SAnsuel Smith 	    phydev->drv->phy_id == QCA8327_B_PHY_ID)
161367999555SAnsuel Smith 		at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
16141ca83119SAnsuel Smith 				      QCA8327_DEBUG_MANU_CTRL_EN, 0);
16151ca83119SAnsuel Smith 
16169d1c29b4SAnsuel Smith 	/* Following original QCA sourcecode set port to prefer master */
16179d1c29b4SAnsuel Smith 	phy_set_bits(phydev, MII_CTRL1000, CTL1000_PREFER_MASTER);
16189d1c29b4SAnsuel Smith 
1619272833b9SAnsuel Smith 	return 0;
1620272833b9SAnsuel Smith }
1621272833b9SAnsuel Smith 
16221ca83119SAnsuel Smith static void qca83xx_link_change_notify(struct phy_device *phydev)
16231ca83119SAnsuel Smith {
16241ca83119SAnsuel Smith 	/* QCA8337 doesn't require DAC Amplitude adjustement */
16251ca83119SAnsuel Smith 	if (phydev->drv->phy_id == QCA8337_PHY_ID)
16261ca83119SAnsuel Smith 		return;
16271ca83119SAnsuel Smith 
16281ca83119SAnsuel Smith 	/* Set DAC Amplitude adjustment to +6% for 100m on link running */
16291ca83119SAnsuel Smith 	if (phydev->state == PHY_RUNNING) {
16301ca83119SAnsuel Smith 		if (phydev->speed == SPEED_100)
163167999555SAnsuel Smith 			at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
16321ca83119SAnsuel Smith 					      QCA8327_DEBUG_MANU_CTRL_EN,
16331ca83119SAnsuel Smith 					      QCA8327_DEBUG_MANU_CTRL_EN);
16341ca83119SAnsuel Smith 	} else {
16351ca83119SAnsuel Smith 		/* Reset DAC Amplitude adjustment */
163667999555SAnsuel Smith 		at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
16371ca83119SAnsuel Smith 				      QCA8327_DEBUG_MANU_CTRL_EN, 0);
16381ca83119SAnsuel Smith 	}
16391ca83119SAnsuel Smith }
16401ca83119SAnsuel Smith 
1641ba3c01eeSAnsuel Smith static int qca83xx_resume(struct phy_device *phydev)
1642ba3c01eeSAnsuel Smith {
1643ba3c01eeSAnsuel Smith 	int ret, val;
1644ba3c01eeSAnsuel Smith 
1645ba3c01eeSAnsuel Smith 	/* Skip reset if not suspended */
1646ba3c01eeSAnsuel Smith 	if (!phydev->suspended)
1647ba3c01eeSAnsuel Smith 		return 0;
1648ba3c01eeSAnsuel Smith 
1649ba3c01eeSAnsuel Smith 	/* Reinit the port, reset values set by suspend */
1650ba3c01eeSAnsuel Smith 	qca83xx_config_init(phydev);
1651ba3c01eeSAnsuel Smith 
1652ba3c01eeSAnsuel Smith 	/* Reset the port on port resume */
1653ba3c01eeSAnsuel Smith 	phy_set_bits(phydev, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
1654ba3c01eeSAnsuel Smith 
1655ba3c01eeSAnsuel Smith 	/* On resume from suspend the switch execute a reset and
1656ba3c01eeSAnsuel Smith 	 * restart auto-negotiation. Wait for reset to complete.
1657ba3c01eeSAnsuel Smith 	 */
1658ba3c01eeSAnsuel Smith 	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1659ba3c01eeSAnsuel Smith 				    50000, 600000, true);
1660ba3c01eeSAnsuel Smith 	if (ret)
1661ba3c01eeSAnsuel Smith 		return ret;
1662ba3c01eeSAnsuel Smith 
1663ba3c01eeSAnsuel Smith 	msleep(1);
1664ba3c01eeSAnsuel Smith 
1665ba3c01eeSAnsuel Smith 	return 0;
1666ba3c01eeSAnsuel Smith }
1667ba3c01eeSAnsuel Smith 
1668ba3c01eeSAnsuel Smith static int qca83xx_suspend(struct phy_device *phydev)
1669ba3c01eeSAnsuel Smith {
1670ba3c01eeSAnsuel Smith 	u16 mask = 0;
1671ba3c01eeSAnsuel Smith 
1672ba3c01eeSAnsuel Smith 	/* Only QCA8337 support actual suspend.
1673ba3c01eeSAnsuel Smith 	 * QCA8327 cause port unreliability when phy suspend
1674ba3c01eeSAnsuel Smith 	 * is set.
1675ba3c01eeSAnsuel Smith 	 */
1676ba3c01eeSAnsuel Smith 	if (phydev->drv->phy_id == QCA8337_PHY_ID) {
1677ba3c01eeSAnsuel Smith 		genphy_suspend(phydev);
1678ba3c01eeSAnsuel Smith 	} else {
1679ba3c01eeSAnsuel Smith 		mask |= ~(BMCR_SPEED1000 | BMCR_FULLDPLX);
1680ba3c01eeSAnsuel Smith 		phy_modify(phydev, MII_BMCR, mask, 0);
1681ba3c01eeSAnsuel Smith 	}
1682ba3c01eeSAnsuel Smith 
168367999555SAnsuel Smith 	at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_GREEN,
1684ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_GATE_CLK_IN1000, 0);
1685ba3c01eeSAnsuel Smith 
1686ba3c01eeSAnsuel Smith 	at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
1687ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE |
1688ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_HIB_CTRL_SEL_RST_80U, 0);
1689ba3c01eeSAnsuel Smith 
1690ba3c01eeSAnsuel Smith 	return 0;
1691ba3c01eeSAnsuel Smith }
1692ba3c01eeSAnsuel Smith 
16932acdd43fSLuo Jie static int qca808x_phy_fast_retrain_config(struct phy_device *phydev)
16942acdd43fSLuo Jie {
16952acdd43fSLuo Jie 	int ret;
16962acdd43fSLuo Jie 
16972acdd43fSLuo Jie 	/* Enable fast retrain */
16982acdd43fSLuo Jie 	ret = genphy_c45_fast_retrain(phydev, true);
16992acdd43fSLuo Jie 	if (ret)
17002acdd43fSLuo Jie 		return ret;
17012acdd43fSLuo Jie 
17022acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_AN, QCA808X_PHY_MMD7_TOP_OPTION1,
17032acdd43fSLuo Jie 			QCA808X_TOP_OPTION1_DATA);
17042acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB,
17052acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_20DB_VALUE);
17062acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB,
17072acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_17DB_VALUE);
17082acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB,
17092acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_27DB_VALUE);
17102acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB,
17112acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_28DB_VALUE);
17122acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_1,
17132acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_1_VALUE);
17142acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_4,
17152acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_4_VALUE);
17162acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_5,
17172acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_5_VALUE);
17182acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_3,
17192acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_3_VALUE);
17202acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_6,
17212acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_6_VALUE);
17222acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_2,
17232acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_2_VALUE);
17242acdd43fSLuo Jie 
17252acdd43fSLuo Jie 	return 0;
17262acdd43fSLuo Jie }
17272acdd43fSLuo Jie 
17289d4dae29SLuo Jie static int qca808x_phy_ms_seed_enable(struct phy_device *phydev, bool enable)
17299d4dae29SLuo Jie {
1730f3db55aeSLuo Jie 	u16 seed_value;
17319d4dae29SLuo Jie 
1732f3db55aeSLuo Jie 	if (!enable)
17339d4dae29SLuo Jie 		return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED,
1734f3db55aeSLuo Jie 				QCA808X_MASTER_SLAVE_SEED_ENABLE, 0);
1735f3db55aeSLuo Jie 
1736f3db55aeSLuo Jie 	seed_value = get_random_u32_below(QCA808X_MASTER_SLAVE_SEED_RANGE);
1737f3db55aeSLuo Jie 	return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED,
1738f3db55aeSLuo Jie 			QCA808X_MASTER_SLAVE_SEED_CFG | QCA808X_MASTER_SLAVE_SEED_ENABLE,
1739f3db55aeSLuo Jie 			FIELD_PREP(QCA808X_MASTER_SLAVE_SEED_CFG, seed_value) |
1740f3db55aeSLuo Jie 			QCA808X_MASTER_SLAVE_SEED_ENABLE);
17419d4dae29SLuo Jie }
17429d4dae29SLuo Jie 
1743*7cc32095SLuo Jie static bool qca808x_is_prefer_master(struct phy_device *phydev)
1744*7cc32095SLuo Jie {
1745*7cc32095SLuo Jie 	return (phydev->master_slave_get == MASTER_SLAVE_CFG_MASTER_FORCE) ||
1746*7cc32095SLuo Jie 		(phydev->master_slave_get == MASTER_SLAVE_CFG_MASTER_PREFERRED);
1747*7cc32095SLuo Jie }
1748*7cc32095SLuo Jie 
17492acdd43fSLuo Jie static int qca808x_config_init(struct phy_device *phydev)
17502acdd43fSLuo Jie {
17512acdd43fSLuo Jie 	int ret;
17522acdd43fSLuo Jie 
17532acdd43fSLuo Jie 	/* Active adc&vga on 802.3az for the link 1000M and 100M */
17542acdd43fSLuo Jie 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_ADDR_CLD_CTRL7,
17552acdd43fSLuo Jie 			QCA808X_8023AZ_AFE_CTRL_MASK, QCA808X_8023AZ_AFE_EN);
17562acdd43fSLuo Jie 	if (ret)
17572acdd43fSLuo Jie 		return ret;
17582acdd43fSLuo Jie 
17592acdd43fSLuo Jie 	/* Adjust the threshold on 802.3az for the link 1000M */
17602acdd43fSLuo Jie 	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
17612acdd43fSLuo Jie 			QCA808X_PHY_MMD3_AZ_TRAINING_CTRL, QCA808X_MMD3_AZ_TRAINING_VAL);
17622acdd43fSLuo Jie 	if (ret)
17632acdd43fSLuo Jie 		return ret;
17642acdd43fSLuo Jie 
17652acdd43fSLuo Jie 	/* Config the fast retrain for the link 2500M */
17662acdd43fSLuo Jie 	ret = qca808x_phy_fast_retrain_config(phydev);
17672acdd43fSLuo Jie 	if (ret)
17682acdd43fSLuo Jie 		return ret;
17692acdd43fSLuo Jie 
1770*7cc32095SLuo Jie 	ret = genphy_read_master_slave(phydev);
1771*7cc32095SLuo Jie 	if (ret < 0)
1772*7cc32095SLuo Jie 		return ret;
1773*7cc32095SLuo Jie 
1774*7cc32095SLuo Jie 	if (!qca808x_is_prefer_master(phydev)) {
1775f3db55aeSLuo Jie 		/* Enable seed and configure lower ramdom seed to make phy linked as slave mode */
17769d4dae29SLuo Jie 		ret = qca808x_phy_ms_seed_enable(phydev, true);
17779d4dae29SLuo Jie 		if (ret)
17789d4dae29SLuo Jie 			return ret;
1779*7cc32095SLuo Jie 	}
17809d4dae29SLuo Jie 
17812acdd43fSLuo Jie 	/* Configure adc threshold as 100mv for the link 10M */
17822acdd43fSLuo Jie 	return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_ADC_THRESHOLD,
17832acdd43fSLuo Jie 			QCA808X_ADC_THRESHOLD_MASK, QCA808X_ADC_THRESHOLD_100MV);
17842acdd43fSLuo Jie }
17852acdd43fSLuo Jie 
178679c7bc05SLuo Jie static int qca808x_read_status(struct phy_device *phydev)
178779c7bc05SLuo Jie {
178879c7bc05SLuo Jie 	int ret;
178979c7bc05SLuo Jie 
179079c7bc05SLuo Jie 	ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_STAT);
179179c7bc05SLuo Jie 	if (ret < 0)
179279c7bc05SLuo Jie 		return ret;
179379c7bc05SLuo Jie 
179479c7bc05SLuo Jie 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->lp_advertising,
179579c7bc05SLuo Jie 			ret & MDIO_AN_10GBT_STAT_LP2_5G);
179679c7bc05SLuo Jie 
179779c7bc05SLuo Jie 	ret = genphy_read_status(phydev);
179879c7bc05SLuo Jie 	if (ret)
179979c7bc05SLuo Jie 		return ret;
180079c7bc05SLuo Jie 
180179c7bc05SLuo Jie 	ret = at803x_read_specific_status(phydev);
180279c7bc05SLuo Jie 	if (ret < 0)
180379c7bc05SLuo Jie 		return ret;
180479c7bc05SLuo Jie 
1805881cc731SJonathan McDowell 	if (phydev->link) {
1806881cc731SJonathan McDowell 		if (phydev->speed == SPEED_2500)
180779c7bc05SLuo Jie 			phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
180879c7bc05SLuo Jie 		else
1809881cc731SJonathan McDowell 			phydev->interface = PHY_INTERFACE_MODE_SGMII;
1810881cc731SJonathan McDowell 	} else {
18118bc1c543SLuo Jie 		/* generate seed as a lower random value to make PHY linked as SLAVE easily,
1812*7cc32095SLuo Jie 		 * except for master/slave configuration fault detected or the master mode
1813*7cc32095SLuo Jie 		 * preferred.
1814*7cc32095SLuo Jie 		 *
18158bc1c543SLuo Jie 		 * the reason for not putting this code into the function link_change_notify is
18168bc1c543SLuo Jie 		 * the corner case where the link partner is also the qca8081 PHY and the seed
18178bc1c543SLuo Jie 		 * value is configured as the same value, the link can't be up and no link change
18188bc1c543SLuo Jie 		 * occurs.
18198bc1c543SLuo Jie 		 */
1820*7cc32095SLuo Jie 		if (phydev->master_slave_state == MASTER_SLAVE_STATE_ERR ||
1821*7cc32095SLuo Jie 				qca808x_is_prefer_master(phydev)) {
18228bc1c543SLuo Jie 			qca808x_phy_ms_seed_enable(phydev, false);
18238bc1c543SLuo Jie 		} else {
18248bc1c543SLuo Jie 			qca808x_phy_ms_seed_enable(phydev, true);
18258bc1c543SLuo Jie 		}
18268bc1c543SLuo Jie 	}
18278bc1c543SLuo Jie 
182879c7bc05SLuo Jie 	return 0;
182979c7bc05SLuo Jie }
183079c7bc05SLuo Jie 
18319d4dae29SLuo Jie static int qca808x_soft_reset(struct phy_device *phydev)
18329d4dae29SLuo Jie {
18339d4dae29SLuo Jie 	int ret;
18349d4dae29SLuo Jie 
18359d4dae29SLuo Jie 	ret = genphy_soft_reset(phydev);
18369d4dae29SLuo Jie 	if (ret < 0)
18379d4dae29SLuo Jie 		return ret;
18389d4dae29SLuo Jie 
18399d4dae29SLuo Jie 	return qca808x_phy_ms_seed_enable(phydev, true);
18409d4dae29SLuo Jie }
18419d4dae29SLuo Jie 
18428c84d752SLuo Jie static bool qca808x_cdt_fault_length_valid(int cdt_code)
18438c84d752SLuo Jie {
18448c84d752SLuo Jie 	switch (cdt_code) {
18458c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_SHORT:
18468c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_OPEN:
18478c84d752SLuo Jie 		return true;
18488c84d752SLuo Jie 	default:
18498c84d752SLuo Jie 		return false;
18508c84d752SLuo Jie 	}
18518c84d752SLuo Jie }
18528c84d752SLuo Jie 
18538c84d752SLuo Jie static int qca808x_cable_test_result_trans(int cdt_code)
18548c84d752SLuo Jie {
18558c84d752SLuo Jie 	switch (cdt_code) {
18568c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_NORMAL:
18578c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
18588c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_SHORT:
18598c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
18608c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_OPEN:
18618c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
18628c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_FAIL:
18638c84d752SLuo Jie 	default:
18648c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
18658c84d752SLuo Jie 	}
18668c84d752SLuo Jie }
18678c84d752SLuo Jie 
18688c84d752SLuo Jie static int qca808x_cdt_fault_length(struct phy_device *phydev, int pair)
18698c84d752SLuo Jie {
18708c84d752SLuo Jie 	int val;
18718c84d752SLuo Jie 	u32 cdt_length_reg = 0;
18728c84d752SLuo Jie 
18738c84d752SLuo Jie 	switch (pair) {
18748c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_A:
18758c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_A;
18768c84d752SLuo Jie 		break;
18778c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_B:
18788c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_B;
18798c84d752SLuo Jie 		break;
18808c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_C:
18818c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_C;
18828c84d752SLuo Jie 		break;
18838c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_D:
18848c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_D;
18858c84d752SLuo Jie 		break;
18868c84d752SLuo Jie 	default:
18878c84d752SLuo Jie 		return -EINVAL;
18888c84d752SLuo Jie 	}
18898c84d752SLuo Jie 
18908c84d752SLuo Jie 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, cdt_length_reg);
18918c84d752SLuo Jie 	if (val < 0)
18928c84d752SLuo Jie 		return val;
18938c84d752SLuo Jie 
18948c84d752SLuo Jie 	return (FIELD_GET(QCA808X_CDT_DIAG_LENGTH, val) * 824) / 10;
18958c84d752SLuo Jie }
18968c84d752SLuo Jie 
18978c84d752SLuo Jie static int qca808x_cable_test_start(struct phy_device *phydev)
18988c84d752SLuo Jie {
18998c84d752SLuo Jie 	int ret;
19008c84d752SLuo Jie 
19018c84d752SLuo Jie 	/* perform CDT with the following configs:
19028c84d752SLuo Jie 	 * 1. disable hibernation.
19038c84d752SLuo Jie 	 * 2. force PHY working in MDI mode.
19048c84d752SLuo Jie 	 * 3. for PHY working in 1000BaseT.
19058c84d752SLuo Jie 	 * 4. configure the threshold.
19068c84d752SLuo Jie 	 */
19078c84d752SLuo Jie 
19088c84d752SLuo Jie 	ret = at803x_debug_reg_mask(phydev, QCA808X_DBG_AN_TEST, QCA808X_HIBERNATION_EN, 0);
19098c84d752SLuo Jie 	if (ret < 0)
19108c84d752SLuo Jie 		return ret;
19118c84d752SLuo Jie 
19128c84d752SLuo Jie 	ret = at803x_config_mdix(phydev, ETH_TP_MDI);
19138c84d752SLuo Jie 	if (ret < 0)
19148c84d752SLuo Jie 		return ret;
19158c84d752SLuo Jie 
19168c84d752SLuo Jie 	/* Force 1000base-T needs to configure PMA/PMD and MII_BMCR */
19178c84d752SLuo Jie 	phydev->duplex = DUPLEX_FULL;
19188c84d752SLuo Jie 	phydev->speed = SPEED_1000;
19198c84d752SLuo Jie 	ret = genphy_c45_pma_setup_forced(phydev);
19208c84d752SLuo Jie 	if (ret < 0)
19218c84d752SLuo Jie 		return ret;
19228c84d752SLuo Jie 
19238c84d752SLuo Jie 	ret = genphy_setup_forced(phydev);
19248c84d752SLuo Jie 	if (ret < 0)
19258c84d752SLuo Jie 		return ret;
19268c84d752SLuo Jie 
19278c84d752SLuo Jie 	/* configure the thresholds for open, short, pair ok test */
19288c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8074, 0xc040);
19298c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8076, 0xc040);
19308c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8077, 0xa060);
19318c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8078, 0xc050);
19328c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807a, 0xc060);
19338c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807e, 0xb060);
19348c84d752SLuo Jie 
19358c84d752SLuo Jie 	return 0;
19368c84d752SLuo Jie }
19378c84d752SLuo Jie 
19388c84d752SLuo Jie static int qca808x_cable_test_get_status(struct phy_device *phydev, bool *finished)
19398c84d752SLuo Jie {
19408c84d752SLuo Jie 	int ret, val;
19418c84d752SLuo Jie 	int pair_a, pair_b, pair_c, pair_d;
19428c84d752SLuo Jie 
19438c84d752SLuo Jie 	*finished = false;
19448c84d752SLuo Jie 
19458c84d752SLuo Jie 	ret = at803x_cdt_start(phydev, 0);
19468c84d752SLuo Jie 	if (ret)
19478c84d752SLuo Jie 		return ret;
19488c84d752SLuo Jie 
19498c84d752SLuo Jie 	ret = at803x_cdt_wait_for_completion(phydev);
19508c84d752SLuo Jie 	if (ret)
19518c84d752SLuo Jie 		return ret;
19528c84d752SLuo Jie 
19538c84d752SLuo Jie 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, QCA808X_MMD3_CDT_STATUS);
19548c84d752SLuo Jie 	if (val < 0)
19558c84d752SLuo Jie 		return val;
19568c84d752SLuo Jie 
19578c84d752SLuo Jie 	pair_a = FIELD_GET(QCA808X_CDT_CODE_PAIR_A, val);
19588c84d752SLuo Jie 	pair_b = FIELD_GET(QCA808X_CDT_CODE_PAIR_B, val);
19598c84d752SLuo Jie 	pair_c = FIELD_GET(QCA808X_CDT_CODE_PAIR_C, val);
19608c84d752SLuo Jie 	pair_d = FIELD_GET(QCA808X_CDT_CODE_PAIR_D, val);
19618c84d752SLuo Jie 
19628c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
19638c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_a));
19648c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_B,
19658c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_b));
19668c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_C,
19678c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_c));
19688c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_D,
19698c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_d));
19708c84d752SLuo Jie 
19718c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_a))
19728c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A,
19738c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A));
19748c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_b))
19758c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B,
19768c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B));
19778c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_c))
19788c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C,
19798c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C));
19808c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_d))
19818c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D,
19828c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D));
19838c84d752SLuo Jie 
19848c84d752SLuo Jie 	*finished = true;
19858c84d752SLuo Jie 
19868c84d752SLuo Jie 	return 0;
19878c84d752SLuo Jie }
19888c84d752SLuo Jie 
19898b8bc13dSLuo Jie static int qca808x_get_features(struct phy_device *phydev)
19908b8bc13dSLuo Jie {
19918b8bc13dSLuo Jie 	int ret;
19928b8bc13dSLuo Jie 
19938b8bc13dSLuo Jie 	ret = genphy_c45_pma_read_abilities(phydev);
19948b8bc13dSLuo Jie 	if (ret)
19958b8bc13dSLuo Jie 		return ret;
19968b8bc13dSLuo Jie 
19978b8bc13dSLuo Jie 	/* The autoneg ability is not existed in bit3 of MMD7.1,
19988b8bc13dSLuo Jie 	 * but it is supported by qca808x PHY, so we add it here
19998b8bc13dSLuo Jie 	 * manually.
20008b8bc13dSLuo Jie 	 */
20018b8bc13dSLuo Jie 	linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported);
20028b8bc13dSLuo Jie 
20038b8bc13dSLuo Jie 	return 0;
20048b8bc13dSLuo Jie }
20058b8bc13dSLuo Jie 
2006317420abSMugunthan V N static struct phy_driver at803x_driver[] = {
2007317420abSMugunthan V N {
200896c36712SMichael Walle 	/* Qualcomm Atheros AR8035 */
20090465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
201096c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8035",
20116cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
20122f664823SMichael Walle 	.probe			= at803x_probe,
20137dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
20140ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
2015cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
2016ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
2017ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
20186229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
20196229ed1fSDaniel Mack 	.resume			= at803x_resume,
2020dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
202106d5f344SRussell King 	.read_status		= at803x_read_status,
20220eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
202329773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2024cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
2025cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
20266cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
20276cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
2028317420abSMugunthan V N }, {
202996c36712SMichael Walle 	/* Qualcomm Atheros AR8030 */
2030bd8ca17fSDaniel Mack 	.phy_id			= ATH8030_PHY_ID,
203196c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8030",
20320465d8f8SMichael Walle 	.phy_id_mask		= AT8030_PHY_ID_MASK,
20332f664823SMichael Walle 	.probe			= at803x_probe,
20340ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
203513a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
2036ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
2037ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
20386229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
20396229ed1fSDaniel Mack 	.resume			= at803x_resume,
2040dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
20410eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
204229773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
204305d7cce8SMugunthan V N }, {
204496c36712SMichael Walle 	/* Qualcomm Atheros AR8031/AR8033 */
20450465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
204696c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8031/AR8033",
20476cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
20482f664823SMichael Walle 	.probe			= at803x_probe,
204905d7cce8SMugunthan V N 	.config_init		= at803x_config_init,
205063477a5dSMichael Walle 	.config_aneg		= at803x_config_aneg,
2051cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
205205d7cce8SMugunthan V N 	.set_wol		= at803x_set_wol,
205305d7cce8SMugunthan V N 	.get_wol		= at803x_get_wol,
20546229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
20556229ed1fSDaniel Mack 	.resume			= at803x_resume,
2056c329e5afSDavid Bauer 	.read_page		= at803x_read_page,
2057c329e5afSDavid Bauer 	.write_page		= at803x_write_page,
2058b856150cSDavid Bauer 	.get_features		= at803x_get_features,
205906d5f344SRussell King 	.read_status		= at803x_read_status,
206077a99394SZhao Qiang 	.config_intr		= &at803x_config_intr,
206129773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2062cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
2063cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
20646cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
20656cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
20667908d2ceSOleksij Rempel }, {
20675800091aSDavid Bauer 	/* Qualcomm Atheros AR8032 */
20685800091aSDavid Bauer 	PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
20695800091aSDavid Bauer 	.name			= "Qualcomm Atheros AR8032",
20705800091aSDavid Bauer 	.probe			= at803x_probe,
2071dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
20725800091aSDavid Bauer 	.config_init		= at803x_config_init,
20735800091aSDavid Bauer 	.link_change_notify	= at803x_link_change_notify,
20745800091aSDavid Bauer 	.set_wol		= at803x_set_wol,
20755800091aSDavid Bauer 	.get_wol		= at803x_get_wol,
20765800091aSDavid Bauer 	.suspend		= at803x_suspend,
20775800091aSDavid Bauer 	.resume			= at803x_resume,
20785800091aSDavid Bauer 	/* PHY_BASIC_FEATURES */
20795800091aSDavid Bauer 	.config_intr		= at803x_config_intr,
208029773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2081dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
2082dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
20835800091aSDavid Bauer }, {
20847908d2ceSOleksij Rempel 	/* ATHEROS AR9331 */
20857908d2ceSOleksij Rempel 	PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
208696c36712SMichael Walle 	.name			= "Qualcomm Atheros AR9331 built-in PHY",
20879926de73SOleksij Rempel 	.probe			= at803x_probe,
20887908d2ceSOleksij Rempel 	.suspend		= at803x_suspend,
20897908d2ceSOleksij Rempel 	.resume			= at803x_resume,
2090dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
20917908d2ceSOleksij Rempel 	/* PHY_BASIC_FEATURES */
20927908d2ceSOleksij Rempel 	.config_intr		= &at803x_config_intr,
209329773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2094dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
2095dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
20967dce80c2SOleksij Rempel 	.read_status		= at803x_read_status,
20977dce80c2SOleksij Rempel 	.soft_reset		= genphy_soft_reset,
20987dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
2099272833b9SAnsuel Smith }, {
2100fada2ce0SDavid Bauer 	/* Qualcomm Atheros QCA9561 */
2101fada2ce0SDavid Bauer 	PHY_ID_MATCH_EXACT(QCA9561_PHY_ID),
2102fada2ce0SDavid Bauer 	.name			= "Qualcomm Atheros QCA9561 built-in PHY",
21039926de73SOleksij Rempel 	.probe			= at803x_probe,
2104fada2ce0SDavid Bauer 	.suspend		= at803x_suspend,
2105fada2ce0SDavid Bauer 	.resume			= at803x_resume,
2106fada2ce0SDavid Bauer 	.flags			= PHY_POLL_CABLE_TEST,
2107fada2ce0SDavid Bauer 	/* PHY_BASIC_FEATURES */
2108fada2ce0SDavid Bauer 	.config_intr		= &at803x_config_intr,
2109fada2ce0SDavid Bauer 	.handle_interrupt	= at803x_handle_interrupt,
2110fada2ce0SDavid Bauer 	.cable_test_start	= at803x_cable_test_start,
2111fada2ce0SDavid Bauer 	.cable_test_get_status	= at803x_cable_test_get_status,
2112fada2ce0SDavid Bauer 	.read_status		= at803x_read_status,
2113fada2ce0SDavid Bauer 	.soft_reset		= genphy_soft_reset,
2114fada2ce0SDavid Bauer 	.config_aneg		= at803x_config_aneg,
2115fada2ce0SDavid Bauer }, {
2116272833b9SAnsuel Smith 	/* QCA8337 */
2117272833b9SAnsuel Smith 	.phy_id			= QCA8337_PHY_ID,
2118272833b9SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
2119d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8337 internal PHY",
2120272833b9SAnsuel Smith 	/* PHY_GBIT_FEATURES */
21211ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
2122272833b9SAnsuel Smith 	.probe			= at803x_probe,
2123272833b9SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
2124272833b9SAnsuel Smith 	.config_init		= qca83xx_config_init,
2125272833b9SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
2126272833b9SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
2127272833b9SAnsuel Smith 	.get_strings		= at803x_get_strings,
2128272833b9SAnsuel Smith 	.get_stats		= at803x_get_stats,
2129ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
2130ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
21310ccf8511SAnsuel Smith }, {
2132b4df02b5SAnsuel Smith 	/* QCA8327-A from switch QCA8327-AL1A */
2133b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_A_PHY_ID,
21340ccf8511SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
2135d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-A internal PHY",
2136b4df02b5SAnsuel Smith 	/* PHY_GBIT_FEATURES */
21371ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
2138b4df02b5SAnsuel Smith 	.probe			= at803x_probe,
2139b4df02b5SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
2140b4df02b5SAnsuel Smith 	.config_init		= qca83xx_config_init,
2141b4df02b5SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
2142b4df02b5SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
2143b4df02b5SAnsuel Smith 	.get_strings		= at803x_get_strings,
2144b4df02b5SAnsuel Smith 	.get_stats		= at803x_get_stats,
2145ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
2146ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
2147b4df02b5SAnsuel Smith }, {
2148b4df02b5SAnsuel Smith 	/* QCA8327-B from switch QCA8327-BL1A */
2149b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_B_PHY_ID,
2150b4df02b5SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
2151d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-B internal PHY",
21520ccf8511SAnsuel Smith 	/* PHY_GBIT_FEATURES */
21531ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
21540ccf8511SAnsuel Smith 	.probe			= at803x_probe,
21550ccf8511SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
21560ccf8511SAnsuel Smith 	.config_init		= qca83xx_config_init,
21570ccf8511SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
21580ccf8511SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
21590ccf8511SAnsuel Smith 	.get_strings		= at803x_get_strings,
21600ccf8511SAnsuel Smith 	.get_stats		= at803x_get_stats,
2161ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
2162ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
2163daf61732SLuo Jie }, {
2164daf61732SLuo Jie 	/* Qualcomm QCA8081 */
2165daf61732SLuo Jie 	PHY_ID_MATCH_EXACT(QCA8081_PHY_ID),
2166daf61732SLuo Jie 	.name			= "Qualcomm QCA8081",
21678c84d752SLuo Jie 	.flags			= PHY_POLL_CABLE_TEST,
21689926de73SOleksij Rempel 	.probe			= at803x_probe,
2169daf61732SLuo Jie 	.config_intr		= at803x_config_intr,
2170daf61732SLuo Jie 	.handle_interrupt	= at803x_handle_interrupt,
2171daf61732SLuo Jie 	.get_tunable		= at803x_get_tunable,
2172daf61732SLuo Jie 	.set_tunable		= at803x_set_tunable,
2173daf61732SLuo Jie 	.set_wol		= at803x_set_wol,
2174daf61732SLuo Jie 	.get_wol		= at803x_get_wol,
21758b8bc13dSLuo Jie 	.get_features		= qca808x_get_features,
2176f884d449SLuo Jie 	.config_aneg		= at803x_config_aneg,
2177daf61732SLuo Jie 	.suspend		= genphy_suspend,
2178daf61732SLuo Jie 	.resume			= genphy_resume,
217979c7bc05SLuo Jie 	.read_status		= qca808x_read_status,
21802acdd43fSLuo Jie 	.config_init		= qca808x_config_init,
21819d4dae29SLuo Jie 	.soft_reset		= qca808x_soft_reset,
21828c84d752SLuo Jie 	.cable_test_start	= qca808x_cable_test_start,
21838c84d752SLuo Jie 	.cable_test_get_status	= qca808x_cable_test_get_status,
2184272833b9SAnsuel Smith }, };
21850ca7111aSMatus Ujhelyi 
218650fd7150SJohan Hovold module_phy_driver(at803x_driver);
21870ca7111aSMatus Ujhelyi 
21880ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = {
21890465d8f8SMichael Walle 	{ ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
21900465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
21915800091aSDavid Bauer 	{ PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
21920465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
21937908d2ceSOleksij Rempel 	{ PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
21940ccf8511SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8337_PHY_ID) },
2195b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_A_PHY_ID) },
2196b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_B_PHY_ID) },
2197fada2ce0SDavid Bauer 	{ PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) },
2198daf61732SLuo Jie 	{ PHY_ID_MATCH_EXACT(QCA8081_PHY_ID) },
21990ca7111aSMatus Ujhelyi 	{ }
22000ca7111aSMatus Ujhelyi };
22010ca7111aSMatus Ujhelyi 
22020ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl);
2203