xref: /openbmc/linux/drivers/net/phy/at803x.c (revision fea7cfb8)
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 
275*fea7cfb8SLuo Jie /* QCA808X 1G chip type */
276*fea7cfb8SLuo Jie #define QCA808X_PHY_MMD7_CHIP_TYPE		0x901d
277*fea7cfb8SLuo Jie #define QCA808X_PHY_CHIP_TYPE_1G		BIT(0)
278*fea7cfb8SLuo Jie 
279daf61732SLuo Jie MODULE_DESCRIPTION("Qualcomm Atheros AR803x and QCA808X PHY driver");
2800ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi");
2810ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL");
2820ca7111aSMatus Ujhelyi 
283272833b9SAnsuel Smith enum stat_access_type {
284272833b9SAnsuel Smith 	PHY,
285272833b9SAnsuel Smith 	MMD
286272833b9SAnsuel Smith };
287272833b9SAnsuel Smith 
288272833b9SAnsuel Smith struct at803x_hw_stat {
289272833b9SAnsuel Smith 	const char *string;
290272833b9SAnsuel Smith 	u8 reg;
291272833b9SAnsuel Smith 	u32 mask;
292272833b9SAnsuel Smith 	enum stat_access_type access_type;
293272833b9SAnsuel Smith };
294272833b9SAnsuel Smith 
295272833b9SAnsuel Smith static struct at803x_hw_stat at803x_hw_stats[] = {
296272833b9SAnsuel Smith 	{ "phy_idle_errors", 0xa, GENMASK(7, 0), PHY},
297272833b9SAnsuel Smith 	{ "phy_receive_errors", 0x15, GENMASK(15, 0), PHY},
298272833b9SAnsuel Smith 	{ "eee_wake_errors", 0x16, GENMASK(15, 0), MMD},
299272833b9SAnsuel Smith };
300272833b9SAnsuel Smith 
3012f664823SMichael Walle struct at803x_priv {
3022f664823SMichael Walle 	int flags;
3032f664823SMichael Walle 	u16 clk_25m_reg;
3042f664823SMichael Walle 	u16 clk_25m_mask;
305390b4cadSRussell King 	u8 smarteee_lpi_tw_1g;
306390b4cadSRussell King 	u8 smarteee_lpi_tw_100m;
3073265f421SRobert Hancock 	bool is_fiber;
3083265f421SRobert Hancock 	bool is_1000basex;
3092f664823SMichael Walle 	struct regulator_dev *vddio_rdev;
3102f664823SMichael Walle 	struct regulator_dev *vddh_rdev;
311272833b9SAnsuel Smith 	u64 stats[ARRAY_SIZE(at803x_hw_stats)];
3122f664823SMichael Walle };
3132f664823SMichael Walle 
31413a56b44SDaniel Mack struct at803x_context {
31513a56b44SDaniel Mack 	u16 bmcr;
31613a56b44SDaniel Mack 	u16 advertise;
31713a56b44SDaniel Mack 	u16 control1000;
31813a56b44SDaniel Mack 	u16 int_enable;
31913a56b44SDaniel Mack 	u16 smart_speed;
32013a56b44SDaniel Mack 	u16 led_control;
32113a56b44SDaniel Mack };
32213a56b44SDaniel Mack 
323272833b9SAnsuel Smith static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data)
324272833b9SAnsuel Smith {
325272833b9SAnsuel Smith 	int ret;
326272833b9SAnsuel Smith 
327272833b9SAnsuel Smith 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
328272833b9SAnsuel Smith 	if (ret < 0)
329272833b9SAnsuel Smith 		return ret;
330272833b9SAnsuel Smith 
331272833b9SAnsuel Smith 	return phy_write(phydev, AT803X_DEBUG_DATA, data);
332272833b9SAnsuel Smith }
333272833b9SAnsuel Smith 
3342e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
3352e5f9f28SMartin Blumenstingl {
3362e5f9f28SMartin Blumenstingl 	int ret;
3372e5f9f28SMartin Blumenstingl 
3382e5f9f28SMartin Blumenstingl 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
3392e5f9f28SMartin Blumenstingl 	if (ret < 0)
3402e5f9f28SMartin Blumenstingl 		return ret;
3412e5f9f28SMartin Blumenstingl 
3422e5f9f28SMartin Blumenstingl 	return phy_read(phydev, AT803X_DEBUG_DATA);
3432e5f9f28SMartin Blumenstingl }
3442e5f9f28SMartin Blumenstingl 
3452e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
3462e5f9f28SMartin Blumenstingl 				 u16 clear, u16 set)
3472e5f9f28SMartin Blumenstingl {
3482e5f9f28SMartin Blumenstingl 	u16 val;
3492e5f9f28SMartin Blumenstingl 	int ret;
3502e5f9f28SMartin Blumenstingl 
3512e5f9f28SMartin Blumenstingl 	ret = at803x_debug_reg_read(phydev, reg);
3522e5f9f28SMartin Blumenstingl 	if (ret < 0)
3532e5f9f28SMartin Blumenstingl 		return ret;
3542e5f9f28SMartin Blumenstingl 
3552e5f9f28SMartin Blumenstingl 	val = ret & 0xffff;
3562e5f9f28SMartin Blumenstingl 	val &= ~clear;
3572e5f9f28SMartin Blumenstingl 	val |= set;
3582e5f9f28SMartin Blumenstingl 
3592e5f9f28SMartin Blumenstingl 	return phy_write(phydev, AT803X_DEBUG_DATA, val);
3602e5f9f28SMartin Blumenstingl }
3612e5f9f28SMartin Blumenstingl 
362c329e5afSDavid Bauer static int at803x_write_page(struct phy_device *phydev, int page)
363c329e5afSDavid Bauer {
364c329e5afSDavid Bauer 	int mask;
365c329e5afSDavid Bauer 	int set;
366c329e5afSDavid Bauer 
367c329e5afSDavid Bauer 	if (page == AT803X_PAGE_COPPER) {
368c329e5afSDavid Bauer 		set = AT803X_BT_BX_REG_SEL;
369c329e5afSDavid Bauer 		mask = 0;
370c329e5afSDavid Bauer 	} else {
371c329e5afSDavid Bauer 		set = 0;
372c329e5afSDavid Bauer 		mask = AT803X_BT_BX_REG_SEL;
373c329e5afSDavid Bauer 	}
374c329e5afSDavid Bauer 
375c329e5afSDavid Bauer 	return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
376c329e5afSDavid Bauer }
377c329e5afSDavid Bauer 
378c329e5afSDavid Bauer static int at803x_read_page(struct phy_device *phydev)
379c329e5afSDavid Bauer {
380c329e5afSDavid Bauer 	int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
381c329e5afSDavid Bauer 
382c329e5afSDavid Bauer 	if (ccr < 0)
383c329e5afSDavid Bauer 		return ccr;
384c329e5afSDavid Bauer 
385c329e5afSDavid Bauer 	if (ccr & AT803X_BT_BX_REG_SEL)
386c329e5afSDavid Bauer 		return AT803X_PAGE_COPPER;
387c329e5afSDavid Bauer 
388c329e5afSDavid Bauer 	return AT803X_PAGE_FIBER;
389c329e5afSDavid Bauer }
390c329e5afSDavid Bauer 
3916d4cd041SVinod Koul static int at803x_enable_rx_delay(struct phy_device *phydev)
3926d4cd041SVinod Koul {
39367999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0,
3946d4cd041SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN);
3956d4cd041SVinod Koul }
3966d4cd041SVinod Koul 
3976d4cd041SVinod Koul static int at803x_enable_tx_delay(struct phy_device *phydev)
3986d4cd041SVinod Koul {
39967999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0,
4006d4cd041SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN);
4016d4cd041SVinod Koul }
4026d4cd041SVinod Koul 
40343f2ebd5SVinod Koul static int at803x_disable_rx_delay(struct phy_device *phydev)
4042e5f9f28SMartin Blumenstingl {
40567999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
406cd28d1d6SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN, 0);
4072e5f9f28SMartin Blumenstingl }
4082e5f9f28SMartin Blumenstingl 
40943f2ebd5SVinod Koul static int at803x_disable_tx_delay(struct phy_device *phydev)
4102e5f9f28SMartin Blumenstingl {
41167999555SAnsuel Smith 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE,
412cd28d1d6SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN, 0);
4132e5f9f28SMartin Blumenstingl }
4142e5f9f28SMartin Blumenstingl 
41513a56b44SDaniel Mack /* save relevant PHY registers to private copy */
41613a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev,
41713a56b44SDaniel Mack 				struct at803x_context *context)
41813a56b44SDaniel Mack {
41913a56b44SDaniel Mack 	context->bmcr = phy_read(phydev, MII_BMCR);
42013a56b44SDaniel Mack 	context->advertise = phy_read(phydev, MII_ADVERTISE);
42113a56b44SDaniel Mack 	context->control1000 = phy_read(phydev, MII_CTRL1000);
42213a56b44SDaniel Mack 	context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
42313a56b44SDaniel Mack 	context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
42413a56b44SDaniel Mack 	context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
42513a56b44SDaniel Mack }
42613a56b44SDaniel Mack 
42713a56b44SDaniel Mack /* restore relevant PHY registers from private copy */
42813a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev,
42913a56b44SDaniel Mack 				   const struct at803x_context *context)
43013a56b44SDaniel Mack {
43113a56b44SDaniel Mack 	phy_write(phydev, MII_BMCR, context->bmcr);
43213a56b44SDaniel Mack 	phy_write(phydev, MII_ADVERTISE, context->advertise);
43313a56b44SDaniel Mack 	phy_write(phydev, MII_CTRL1000, context->control1000);
43413a56b44SDaniel Mack 	phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
43513a56b44SDaniel Mack 	phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
43613a56b44SDaniel Mack 	phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
43713a56b44SDaniel Mack }
43813a56b44SDaniel Mack 
439ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev,
440ea13c9eeSMugunthan V N 			  struct ethtool_wolinfo *wol)
4410ca7111aSMatus Ujhelyi {
442d7cd5e06SViorel Suman 	int ret, irq_enabled;
443d7cd5e06SViorel Suman 
444d7cd5e06SViorel Suman 	if (wol->wolopts & WAKE_MAGIC) {
4450ca7111aSMatus Ujhelyi 		struct net_device *ndev = phydev->attached_dev;
4460ca7111aSMatus Ujhelyi 		const u8 *mac;
447c0f0b563SLuo Jie 		unsigned int i;
448edcb501eSColin Ian King 		static const unsigned int offsets[] = {
4490ca7111aSMatus Ujhelyi 			AT803X_LOC_MAC_ADDR_32_47_OFFSET,
4500ca7111aSMatus Ujhelyi 			AT803X_LOC_MAC_ADDR_16_31_OFFSET,
4510ca7111aSMatus Ujhelyi 			AT803X_LOC_MAC_ADDR_0_15_OFFSET,
4520ca7111aSMatus Ujhelyi 		};
4530ca7111aSMatus Ujhelyi 
4540ca7111aSMatus Ujhelyi 		if (!ndev)
455ea13c9eeSMugunthan V N 			return -ENODEV;
4560ca7111aSMatus Ujhelyi 
4570ca7111aSMatus Ujhelyi 		mac = (const u8 *) ndev->dev_addr;
4580ca7111aSMatus Ujhelyi 
4590ca7111aSMatus Ujhelyi 		if (!is_valid_ether_addr(mac))
460fc755687SDan Murphy 			return -EINVAL;
4610ca7111aSMatus Ujhelyi 
4620e021396SCarlo Caione 		for (i = 0; i < 3; i++)
463c0f0b563SLuo Jie 			phy_write_mmd(phydev, MDIO_MMD_PCS, offsets[i],
4640ca7111aSMatus Ujhelyi 				      mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
465ea13c9eeSMugunthan V N 
4667beecaf7SLuo Jie 		/* Enable WOL function */
4677beecaf7SLuo Jie 		ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL,
4687beecaf7SLuo Jie 				0, AT803X_WOL_EN);
4697beecaf7SLuo Jie 		if (ret)
4707beecaf7SLuo Jie 			return ret;
4717beecaf7SLuo Jie 		/* Enable WOL interrupt */
4722d4284e8SLuo Jie 		ret = phy_modify(phydev, AT803X_INTR_ENABLE, 0, AT803X_INTR_ENABLE_WOL);
473ea13c9eeSMugunthan V N 		if (ret)
474ea13c9eeSMugunthan V N 			return ret;
475ea13c9eeSMugunthan V N 	} else {
4767beecaf7SLuo Jie 		/* Disable WoL function */
4777beecaf7SLuo Jie 		ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL,
4787beecaf7SLuo Jie 				AT803X_WOL_EN, 0);
4797beecaf7SLuo Jie 		if (ret)
4807beecaf7SLuo Jie 			return ret;
4817beecaf7SLuo Jie 		/* Disable WOL interrupt */
4822d4284e8SLuo Jie 		ret = phy_modify(phydev, AT803X_INTR_ENABLE, AT803X_INTR_ENABLE_WOL, 0);
483ea13c9eeSMugunthan V N 		if (ret)
484ea13c9eeSMugunthan V N 			return ret;
485ea13c9eeSMugunthan V N 	}
486ea13c9eeSMugunthan V N 
4877beecaf7SLuo Jie 	/* Clear WOL status */
4887beecaf7SLuo Jie 	ret = phy_read(phydev, AT803X_INTR_STATUS);
4897beecaf7SLuo Jie 	if (ret < 0)
490ea13c9eeSMugunthan V N 		return ret;
4917beecaf7SLuo Jie 
4927beecaf7SLuo Jie 	/* Check if there are other interrupts except for WOL triggered when PHY is
4937beecaf7SLuo Jie 	 * in interrupt mode, only the interrupts enabled by AT803X_INTR_ENABLE can
4947beecaf7SLuo Jie 	 * be passed up to the interrupt PIN.
4957beecaf7SLuo Jie 	 */
4967beecaf7SLuo Jie 	irq_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
4977beecaf7SLuo Jie 	if (irq_enabled < 0)
4987beecaf7SLuo Jie 		return irq_enabled;
4997beecaf7SLuo Jie 
5007beecaf7SLuo Jie 	irq_enabled &= ~AT803X_INTR_ENABLE_WOL;
5017beecaf7SLuo Jie 	if (ret & irq_enabled && !phy_polling_mode(phydev))
5027beecaf7SLuo Jie 		phy_trigger_machine(phydev);
5037beecaf7SLuo Jie 
5047beecaf7SLuo Jie 	return 0;
505ea13c9eeSMugunthan V N }
506ea13c9eeSMugunthan V N 
507ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev,
508ea13c9eeSMugunthan V N 			   struct ethtool_wolinfo *wol)
509ea13c9eeSMugunthan V N {
510911e3a46SJiapeng Chong 	int value;
511ea13c9eeSMugunthan V N 
512ea13c9eeSMugunthan V N 	wol->supported = WAKE_MAGIC;
513ea13c9eeSMugunthan V N 	wol->wolopts = 0;
514ea13c9eeSMugunthan V N 
5157beecaf7SLuo Jie 	value = phy_read_mmd(phydev, MDIO_MMD_PCS, AT803X_PHY_MMD3_WOL_CTRL);
5167beecaf7SLuo Jie 	if (value < 0)
5177beecaf7SLuo Jie 		return;
5187beecaf7SLuo Jie 
5197beecaf7SLuo Jie 	if (value & AT803X_WOL_EN)
520ea13c9eeSMugunthan V N 		wol->wolopts |= WAKE_MAGIC;
5210ca7111aSMatus Ujhelyi }
5220ca7111aSMatus Ujhelyi 
523272833b9SAnsuel Smith static int at803x_get_sset_count(struct phy_device *phydev)
524272833b9SAnsuel Smith {
525272833b9SAnsuel Smith 	return ARRAY_SIZE(at803x_hw_stats);
526272833b9SAnsuel Smith }
527272833b9SAnsuel Smith 
528272833b9SAnsuel Smith static void at803x_get_strings(struct phy_device *phydev, u8 *data)
529272833b9SAnsuel Smith {
530272833b9SAnsuel Smith 	int i;
531272833b9SAnsuel Smith 
532272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) {
533272833b9SAnsuel Smith 		strscpy(data + i * ETH_GSTRING_LEN,
534272833b9SAnsuel Smith 			at803x_hw_stats[i].string, ETH_GSTRING_LEN);
535272833b9SAnsuel Smith 	}
536272833b9SAnsuel Smith }
537272833b9SAnsuel Smith 
538272833b9SAnsuel Smith static u64 at803x_get_stat(struct phy_device *phydev, int i)
539272833b9SAnsuel Smith {
540272833b9SAnsuel Smith 	struct at803x_hw_stat stat = at803x_hw_stats[i];
541272833b9SAnsuel Smith 	struct at803x_priv *priv = phydev->priv;
542272833b9SAnsuel Smith 	int val;
543272833b9SAnsuel Smith 	u64 ret;
544272833b9SAnsuel Smith 
545272833b9SAnsuel Smith 	if (stat.access_type == MMD)
546272833b9SAnsuel Smith 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg);
547272833b9SAnsuel Smith 	else
548272833b9SAnsuel Smith 		val = phy_read(phydev, stat.reg);
549272833b9SAnsuel Smith 
550272833b9SAnsuel Smith 	if (val < 0) {
551272833b9SAnsuel Smith 		ret = U64_MAX;
552272833b9SAnsuel Smith 	} else {
553272833b9SAnsuel Smith 		val = val & stat.mask;
554272833b9SAnsuel Smith 		priv->stats[i] += val;
555272833b9SAnsuel Smith 		ret = priv->stats[i];
556272833b9SAnsuel Smith 	}
557272833b9SAnsuel Smith 
558272833b9SAnsuel Smith 	return ret;
559272833b9SAnsuel Smith }
560272833b9SAnsuel Smith 
561272833b9SAnsuel Smith static void at803x_get_stats(struct phy_device *phydev,
562272833b9SAnsuel Smith 			     struct ethtool_stats *stats, u64 *data)
563272833b9SAnsuel Smith {
564272833b9SAnsuel Smith 	int i;
565272833b9SAnsuel Smith 
566272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++)
567272833b9SAnsuel Smith 		data[i] = at803x_get_stat(phydev, i);
568272833b9SAnsuel Smith }
569272833b9SAnsuel Smith 
5706229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev)
5716229ed1fSDaniel Mack {
5726229ed1fSDaniel Mack 	int value;
5736229ed1fSDaniel Mack 	int wol_enabled;
5746229ed1fSDaniel Mack 
5756229ed1fSDaniel Mack 	value = phy_read(phydev, AT803X_INTR_ENABLE);
576e6e4a556SMartin Blumenstingl 	wol_enabled = value & AT803X_INTR_ENABLE_WOL;
5776229ed1fSDaniel Mack 
5786229ed1fSDaniel Mack 	if (wol_enabled)
579fea23fb5SRussell King 		value = BMCR_ISOLATE;
5806229ed1fSDaniel Mack 	else
581fea23fb5SRussell King 		value = BMCR_PDOWN;
5826229ed1fSDaniel Mack 
583fea23fb5SRussell King 	phy_modify(phydev, MII_BMCR, 0, value);
5846229ed1fSDaniel Mack 
5856229ed1fSDaniel Mack 	return 0;
5866229ed1fSDaniel Mack }
5876229ed1fSDaniel Mack 
5886229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev)
5896229ed1fSDaniel Mack {
590f102852fSRussell King 	return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
5916229ed1fSDaniel Mack }
5926229ed1fSDaniel Mack 
5932f664823SMichael Walle static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
5942f664823SMichael Walle 					    unsigned int selector)
5952f664823SMichael Walle {
5962f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
5972f664823SMichael Walle 
5982f664823SMichael Walle 	if (selector)
5992f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
6002f664823SMichael Walle 					     0, AT803X_DEBUG_RGMII_1V8);
6012f664823SMichael Walle 	else
6022f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
6032f664823SMichael Walle 					     AT803X_DEBUG_RGMII_1V8, 0);
6042f664823SMichael Walle }
6052f664823SMichael Walle 
6062f664823SMichael Walle static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
6072f664823SMichael Walle {
6082f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
6092f664823SMichael Walle 	int val;
6102f664823SMichael Walle 
6112f664823SMichael Walle 	val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
6122f664823SMichael Walle 	if (val < 0)
6132f664823SMichael Walle 		return val;
6142f664823SMichael Walle 
6152f664823SMichael Walle 	return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
6162f664823SMichael Walle }
6172f664823SMichael Walle 
6183faaf539SRikard Falkeborn static const struct regulator_ops vddio_regulator_ops = {
6192f664823SMichael Walle 	.list_voltage = regulator_list_voltage_table,
6202f664823SMichael Walle 	.set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
6212f664823SMichael Walle 	.get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
6222f664823SMichael Walle };
6232f664823SMichael Walle 
6242f664823SMichael Walle static const unsigned int vddio_voltage_table[] = {
6252f664823SMichael Walle 	1500000,
6262f664823SMichael Walle 	1800000,
6272f664823SMichael Walle };
6282f664823SMichael Walle 
6292f664823SMichael Walle static const struct regulator_desc vddio_desc = {
6302f664823SMichael Walle 	.name = "vddio",
6312f664823SMichael Walle 	.of_match = of_match_ptr("vddio-regulator"),
6322f664823SMichael Walle 	.n_voltages = ARRAY_SIZE(vddio_voltage_table),
6332f664823SMichael Walle 	.volt_table = vddio_voltage_table,
6342f664823SMichael Walle 	.ops = &vddio_regulator_ops,
6352f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
6362f664823SMichael Walle 	.owner = THIS_MODULE,
6372f664823SMichael Walle };
6382f664823SMichael Walle 
6393faaf539SRikard Falkeborn static const struct regulator_ops vddh_regulator_ops = {
6402f664823SMichael Walle };
6412f664823SMichael Walle 
6422f664823SMichael Walle static const struct regulator_desc vddh_desc = {
6432f664823SMichael Walle 	.name = "vddh",
6442f664823SMichael Walle 	.of_match = of_match_ptr("vddh-regulator"),
6452f664823SMichael Walle 	.n_voltages = 1,
6462f664823SMichael Walle 	.fixed_uV = 2500000,
6472f664823SMichael Walle 	.ops = &vddh_regulator_ops,
6482f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
6492f664823SMichael Walle 	.owner = THIS_MODULE,
6502f664823SMichael Walle };
6512f664823SMichael Walle 
6522f664823SMichael Walle static int at8031_register_regulators(struct phy_device *phydev)
6532f664823SMichael Walle {
6542f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
6552f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
6562f664823SMichael Walle 	struct regulator_config config = { };
6572f664823SMichael Walle 
6582f664823SMichael Walle 	config.dev = dev;
6592f664823SMichael Walle 	config.driver_data = phydev;
6602f664823SMichael Walle 
6612f664823SMichael Walle 	priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
6622f664823SMichael Walle 	if (IS_ERR(priv->vddio_rdev)) {
6632f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDIO regulator\n");
6642f664823SMichael Walle 		return PTR_ERR(priv->vddio_rdev);
6652f664823SMichael Walle 	}
6662f664823SMichael Walle 
6672f664823SMichael Walle 	priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
6682f664823SMichael Walle 	if (IS_ERR(priv->vddh_rdev)) {
6692f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDH regulator\n");
6702f664823SMichael Walle 		return PTR_ERR(priv->vddh_rdev);
6712f664823SMichael Walle 	}
6722f664823SMichael Walle 
6732f664823SMichael Walle 	return 0;
6742f664823SMichael Walle }
6752f664823SMichael Walle 
676dc4d5fccSRobert Hancock static int at803x_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
677dc4d5fccSRobert Hancock {
678dc4d5fccSRobert Hancock 	struct phy_device *phydev = upstream;
679dc4d5fccSRobert Hancock 	__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_support);
680dc4d5fccSRobert Hancock 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
681fd580c98SRussell King 	DECLARE_PHY_INTERFACE_MASK(interfaces);
682dc4d5fccSRobert Hancock 	phy_interface_t iface;
683dc4d5fccSRobert Hancock 
684dc4d5fccSRobert Hancock 	linkmode_zero(phy_support);
685dc4d5fccSRobert Hancock 	phylink_set(phy_support, 1000baseX_Full);
686dc4d5fccSRobert Hancock 	phylink_set(phy_support, 1000baseT_Full);
687dc4d5fccSRobert Hancock 	phylink_set(phy_support, Autoneg);
688dc4d5fccSRobert Hancock 	phylink_set(phy_support, Pause);
689dc4d5fccSRobert Hancock 	phylink_set(phy_support, Asym_Pause);
690dc4d5fccSRobert Hancock 
691dc4d5fccSRobert Hancock 	linkmode_zero(sfp_support);
692fd580c98SRussell King 	sfp_parse_support(phydev->sfp_bus, id, sfp_support, interfaces);
693dc4d5fccSRobert Hancock 	/* Some modules support 10G modes as well as others we support.
694dc4d5fccSRobert Hancock 	 * Mask out non-supported modes so the correct interface is picked.
695dc4d5fccSRobert Hancock 	 */
696dc4d5fccSRobert Hancock 	linkmode_and(sfp_support, phy_support, sfp_support);
697dc4d5fccSRobert Hancock 
698dc4d5fccSRobert Hancock 	if (linkmode_empty(sfp_support)) {
699dc4d5fccSRobert Hancock 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted\n");
700dc4d5fccSRobert Hancock 		return -EINVAL;
701dc4d5fccSRobert Hancock 	}
702dc4d5fccSRobert Hancock 
703dc4d5fccSRobert Hancock 	iface = sfp_select_interface(phydev->sfp_bus, sfp_support);
704dc4d5fccSRobert Hancock 
705dc4d5fccSRobert Hancock 	/* Only 1000Base-X is supported by AR8031/8033 as the downstream SerDes
706dc4d5fccSRobert Hancock 	 * interface for use with SFP modules.
707dc4d5fccSRobert Hancock 	 * However, some copper modules detected as having a preferred SGMII
708dc4d5fccSRobert Hancock 	 * interface do default to and function in 1000Base-X mode, so just
709dc4d5fccSRobert Hancock 	 * print a warning and allow such modules, as they may have some chance
710dc4d5fccSRobert Hancock 	 * of working.
711dc4d5fccSRobert Hancock 	 */
712dc4d5fccSRobert Hancock 	if (iface == PHY_INTERFACE_MODE_SGMII)
713dc4d5fccSRobert Hancock 		dev_warn(&phydev->mdio.dev, "module may not function if 1000Base-X not supported\n");
714dc4d5fccSRobert Hancock 	else if (iface != PHY_INTERFACE_MODE_1000BASEX)
715dc4d5fccSRobert Hancock 		return -EINVAL;
716dc4d5fccSRobert Hancock 
717dc4d5fccSRobert Hancock 	return 0;
718dc4d5fccSRobert Hancock }
719dc4d5fccSRobert Hancock 
720dc4d5fccSRobert Hancock static const struct sfp_upstream_ops at803x_sfp_ops = {
721dc4d5fccSRobert Hancock 	.attach = phy_sfp_attach,
722dc4d5fccSRobert Hancock 	.detach = phy_sfp_detach,
723dc4d5fccSRobert Hancock 	.module_insert = at803x_sfp_insert,
724dc4d5fccSRobert Hancock };
725dc4d5fccSRobert Hancock 
7262f664823SMichael Walle static int at803x_parse_dt(struct phy_device *phydev)
7272f664823SMichael Walle {
7282f664823SMichael Walle 	struct device_node *node = phydev->mdio.dev.of_node;
7292f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
730390b4cadSRussell King 	u32 freq, strength, tw;
7313f2edd30SAndrew Lunn 	unsigned int sel;
7322f664823SMichael Walle 	int ret;
7332f664823SMichael Walle 
7342f664823SMichael Walle 	if (!IS_ENABLED(CONFIG_OF_MDIO))
7352f664823SMichael Walle 		return 0;
7362f664823SMichael Walle 
737390b4cadSRussell King 	if (of_property_read_bool(node, "qca,disable-smarteee"))
738390b4cadSRussell King 		priv->flags |= AT803X_DISABLE_SMARTEEE;
739390b4cadSRussell King 
7409ecf0401SWei Fang 	if (of_property_read_bool(node, "qca,disable-hibernation-mode"))
7419ecf0401SWei Fang 		priv->flags |= AT803X_DISABLE_HIBERNATION_MODE;
7429ecf0401SWei Fang 
743390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
744390b4cadSRussell King 		if (!tw || tw > 255) {
745390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
746390b4cadSRussell King 			return -EINVAL;
747390b4cadSRussell King 		}
748390b4cadSRussell King 		priv->smarteee_lpi_tw_1g = tw;
749390b4cadSRussell King 	}
750390b4cadSRussell King 
751390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
752390b4cadSRussell King 		if (!tw || tw > 255) {
753390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
754390b4cadSRussell King 			return -EINVAL;
755390b4cadSRussell King 		}
756390b4cadSRussell King 		priv->smarteee_lpi_tw_100m = tw;
757390b4cadSRussell King 	}
758390b4cadSRussell King 
7592f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
7602f664823SMichael Walle 	if (!ret) {
7612f664823SMichael Walle 		switch (freq) {
7622f664823SMichael Walle 		case 25000000:
7632f664823SMichael Walle 			sel = AT803X_CLK_OUT_25MHZ_XTAL;
7642f664823SMichael Walle 			break;
7652f664823SMichael Walle 		case 50000000:
7662f664823SMichael Walle 			sel = AT803X_CLK_OUT_50MHZ_PLL;
7672f664823SMichael Walle 			break;
7682f664823SMichael Walle 		case 62500000:
7692f664823SMichael Walle 			sel = AT803X_CLK_OUT_62_5MHZ_PLL;
7702f664823SMichael Walle 			break;
7712f664823SMichael Walle 		case 125000000:
7722f664823SMichael Walle 			sel = AT803X_CLK_OUT_125MHZ_PLL;
7732f664823SMichael Walle 			break;
7742f664823SMichael Walle 		default:
7752f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-frequency\n");
7762f664823SMichael Walle 			return -EINVAL;
7772f664823SMichael Walle 		}
7782f664823SMichael Walle 
7793f2edd30SAndrew Lunn 		priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
7803f2edd30SAndrew Lunn 		priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
7812f664823SMichael Walle 
7822f664823SMichael Walle 		/* Fixup for the AR8030/AR8035. This chip has another mask and
7832f664823SMichael Walle 		 * doesn't support the DSP reference. Eg. the lowest bit of the
7842f664823SMichael Walle 		 * mask. The upper two bits select the same frequencies. Mask
7852f664823SMichael Walle 		 * the lowest bit here.
7862f664823SMichael Walle 		 *
7872f664823SMichael Walle 		 * Warning:
7882f664823SMichael Walle 		 *   There was no datasheet for the AR8030 available so this is
7892f664823SMichael Walle 		 *   just a guess. But the AR8035 is listed as pin compatible
7902f664823SMichael Walle 		 *   to the AR8030 so there might be a good chance it works on
7912f664823SMichael Walle 		 *   the AR8030 too.
7922f664823SMichael Walle 		 */
7938887ca54SRussell King 		if (phydev->drv->phy_id == ATH8030_PHY_ID ||
7948887ca54SRussell King 		    phydev->drv->phy_id == ATH8035_PHY_ID) {
795b1f4c209SOleksij Rempel 			priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
796b1f4c209SOleksij Rempel 			priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
7972f664823SMichael Walle 		}
7982f664823SMichael Walle 	}
7992f664823SMichael Walle 
8002f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
8012f664823SMichael Walle 	if (!ret) {
8022f664823SMichael Walle 		priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
8032f664823SMichael Walle 		switch (strength) {
8042f664823SMichael Walle 		case AR803X_STRENGTH_FULL:
8052f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
8062f664823SMichael Walle 			break;
8072f664823SMichael Walle 		case AR803X_STRENGTH_HALF:
8082f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
8092f664823SMichael Walle 			break;
8102f664823SMichael Walle 		case AR803X_STRENGTH_QUARTER:
8112f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
8122f664823SMichael Walle 			break;
8132f664823SMichael Walle 		default:
8142f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-strength\n");
8152f664823SMichael Walle 			return -EINVAL;
8162f664823SMichael Walle 		}
8172f664823SMichael Walle 	}
8182f664823SMichael Walle 
819428061f7SMichael Walle 	/* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
820428061f7SMichael Walle 	 * options.
821428061f7SMichael Walle 	 */
8228887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
8232f664823SMichael Walle 		if (of_property_read_bool(node, "qca,keep-pll-enabled"))
8242f664823SMichael Walle 			priv->flags |= AT803X_KEEP_PLL_ENABLED;
8252f664823SMichael Walle 
8262f664823SMichael Walle 		ret = at8031_register_regulators(phydev);
8272f664823SMichael Walle 		if (ret < 0)
8282f664823SMichael Walle 			return ret;
8292f664823SMichael Walle 
830988e8d90SChristophe JAILLET 		ret = devm_regulator_get_enable_optional(&phydev->mdio.dev,
8312f664823SMichael Walle 							 "vddio");
832988e8d90SChristophe JAILLET 		if (ret) {
8332f664823SMichael Walle 			phydev_err(phydev, "failed to get VDDIO regulator\n");
834988e8d90SChristophe JAILLET 			return ret;
8352f664823SMichael Walle 		}
836dc4d5fccSRobert Hancock 
837dc4d5fccSRobert Hancock 		/* Only AR8031/8033 support 1000Base-X for SFP modules */
838dc4d5fccSRobert Hancock 		ret = phy_sfp_probe(phydev, &at803x_sfp_ops);
839dc4d5fccSRobert Hancock 		if (ret < 0)
840dc4d5fccSRobert Hancock 			return ret;
8412f664823SMichael Walle 	}
8422f664823SMichael Walle 
8432f664823SMichael Walle 	return 0;
8442f664823SMichael Walle }
8452f664823SMichael Walle 
8462f664823SMichael Walle static int at803x_probe(struct phy_device *phydev)
8472f664823SMichael Walle {
8482f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
8492f664823SMichael Walle 	struct at803x_priv *priv;
850c329e5afSDavid Bauer 	int ret;
8512f664823SMichael Walle 
8522f664823SMichael Walle 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
8532f664823SMichael Walle 	if (!priv)
8542f664823SMichael Walle 		return -ENOMEM;
8552f664823SMichael Walle 
8562f664823SMichael Walle 	phydev->priv = priv;
8572f664823SMichael Walle 
858c329e5afSDavid Bauer 	ret = at803x_parse_dt(phydev);
859c329e5afSDavid Bauer 	if (ret)
860c329e5afSDavid Bauer 		return ret;
861c329e5afSDavid Bauer 
8623265f421SRobert Hancock 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
8633265f421SRobert Hancock 		int ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
8643265f421SRobert Hancock 		int mode_cfg;
865d7cd5e06SViorel Suman 		struct ethtool_wolinfo wol = {
866d7cd5e06SViorel Suman 			.wolopts = 0,
867d7cd5e06SViorel Suman 		};
8683265f421SRobert Hancock 
869988e8d90SChristophe JAILLET 		if (ccr < 0)
870988e8d90SChristophe JAILLET 			return ccr;
8713265f421SRobert Hancock 		mode_cfg = ccr & AT803X_MODE_CFG_MASK;
8723265f421SRobert Hancock 
8733265f421SRobert Hancock 		switch (mode_cfg) {
8743265f421SRobert Hancock 		case AT803X_MODE_CFG_BX1000_RGMII_50OHM:
8753265f421SRobert Hancock 		case AT803X_MODE_CFG_BX1000_RGMII_75OHM:
8763265f421SRobert Hancock 			priv->is_1000basex = true;
8773265f421SRobert Hancock 			fallthrough;
8783265f421SRobert Hancock 		case AT803X_MODE_CFG_FX100_RGMII_50OHM:
8793265f421SRobert Hancock 		case AT803X_MODE_CFG_FX100_RGMII_75OHM:
8803265f421SRobert Hancock 			priv->is_fiber = true;
8813265f421SRobert Hancock 			break;
8823265f421SRobert Hancock 		}
883d7cd5e06SViorel Suman 
884d7cd5e06SViorel Suman 		/* Disable WOL by default */
885d7cd5e06SViorel Suman 		ret = at803x_set_wol(phydev, &wol);
886d7cd5e06SViorel Suman 		if (ret < 0) {
887d7cd5e06SViorel Suman 			phydev_err(phydev, "failed to disable WOL on probe: %d\n", ret);
888988e8d90SChristophe JAILLET 			return ret;
889d7cd5e06SViorel Suman 		}
8903265f421SRobert Hancock 	}
8913265f421SRobert Hancock 
8928f7e8762SMichael Walle 	return 0;
8932318ca8aSMichael Walle }
8942318ca8aSMichael Walle 
895b856150cSDavid Bauer static int at803x_get_features(struct phy_device *phydev)
896b856150cSDavid Bauer {
8973265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
898b856150cSDavid Bauer 	int err;
899b856150cSDavid Bauer 
900b856150cSDavid Bauer 	err = genphy_read_abilities(phydev);
901b856150cSDavid Bauer 	if (err)
902b856150cSDavid Bauer 		return err;
903b856150cSDavid Bauer 
904f5621a01SVladimir Oltean 	if (phydev->drv->phy_id != ATH8031_PHY_ID)
905b856150cSDavid Bauer 		return 0;
906b856150cSDavid Bauer 
907b856150cSDavid Bauer 	/* AR8031/AR8033 have different status registers
908b856150cSDavid Bauer 	 * for copper and fiber operation. However, the
909b856150cSDavid Bauer 	 * extended status register is the same for both
910b856150cSDavid Bauer 	 * operation modes.
911b856150cSDavid Bauer 	 *
912b856150cSDavid Bauer 	 * As a result of that, ESTATUS_1000_XFULL is set
913b856150cSDavid Bauer 	 * to 1 even when operating in copper TP mode.
914b856150cSDavid Bauer 	 *
9153265f421SRobert Hancock 	 * Remove this mode from the supported link modes
9163265f421SRobert Hancock 	 * when not operating in 1000BaseX mode.
917b856150cSDavid Bauer 	 */
9183265f421SRobert Hancock 	if (!priv->is_1000basex)
919b856150cSDavid Bauer 		linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
920b856150cSDavid Bauer 				   phydev->supported);
9213265f421SRobert Hancock 
922b856150cSDavid Bauer 	return 0;
923b856150cSDavid Bauer }
924b856150cSDavid Bauer 
925390b4cadSRussell King static int at803x_smarteee_config(struct phy_device *phydev)
926390b4cadSRussell King {
927390b4cadSRussell King 	struct at803x_priv *priv = phydev->priv;
928390b4cadSRussell King 	u16 mask = 0, val = 0;
929390b4cadSRussell King 	int ret;
930390b4cadSRussell King 
931390b4cadSRussell King 	if (priv->flags & AT803X_DISABLE_SMARTEEE)
932390b4cadSRussell King 		return phy_modify_mmd(phydev, MDIO_MMD_PCS,
933390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3,
934390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
935390b4cadSRussell King 
936390b4cadSRussell King 	if (priv->smarteee_lpi_tw_1g) {
937390b4cadSRussell King 		mask |= 0xff00;
938390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_1g << 8;
939390b4cadSRussell King 	}
940390b4cadSRussell King 	if (priv->smarteee_lpi_tw_100m) {
941390b4cadSRussell King 		mask |= 0x00ff;
942390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_100m;
943390b4cadSRussell King 	}
944390b4cadSRussell King 	if (!mask)
945390b4cadSRussell King 		return 0;
946390b4cadSRussell King 
947390b4cadSRussell King 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
948390b4cadSRussell King 			     mask, val);
949390b4cadSRussell King 	if (ret)
950390b4cadSRussell King 		return ret;
951390b4cadSRussell King 
952390b4cadSRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
953390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
954390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
955390b4cadSRussell King }
956390b4cadSRussell King 
9572f664823SMichael Walle static int at803x_clk_out_config(struct phy_device *phydev)
9582f664823SMichael Walle {
9592f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
9602f664823SMichael Walle 
9612f664823SMichael Walle 	if (!priv->clk_25m_mask)
9622f664823SMichael Walle 		return 0;
9632f664823SMichael Walle 
964a45c1c10SRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
965a45c1c10SRussell King 			      priv->clk_25m_mask, priv->clk_25m_reg);
9662f664823SMichael Walle }
9672f664823SMichael Walle 
9682f664823SMichael Walle static int at8031_pll_config(struct phy_device *phydev)
9692f664823SMichael Walle {
9702f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
9712f664823SMichael Walle 
9722f664823SMichael Walle 	/* The default after hardware reset is PLL OFF. After a soft reset, the
9732f664823SMichael Walle 	 * values are retained.
9742f664823SMichael Walle 	 */
9752f664823SMichael Walle 	if (priv->flags & AT803X_KEEP_PLL_ENABLED)
9762f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
9772f664823SMichael Walle 					     0, AT803X_DEBUG_PLL_ON);
9782f664823SMichael Walle 	else
9792f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
9802f664823SMichael Walle 					     AT803X_DEBUG_PLL_ON, 0);
9812f664823SMichael Walle }
9822f664823SMichael Walle 
9839ecf0401SWei Fang static int at803x_hibernation_mode_config(struct phy_device *phydev)
9849ecf0401SWei Fang {
9859ecf0401SWei Fang 	struct at803x_priv *priv = phydev->priv;
9869ecf0401SWei Fang 
9879ecf0401SWei Fang 	/* The default after hardware reset is hibernation mode enabled. After
9889ecf0401SWei Fang 	 * software reset, the value is retained.
9899ecf0401SWei Fang 	 */
9909ecf0401SWei Fang 	if (!(priv->flags & AT803X_DISABLE_HIBERNATION_MODE))
9919ecf0401SWei Fang 		return 0;
9929ecf0401SWei Fang 
9939ecf0401SWei Fang 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
9949ecf0401SWei Fang 					 AT803X_DEBUG_HIB_CTRL_PS_HIB_EN, 0);
9959ecf0401SWei Fang }
9969ecf0401SWei Fang 
9970ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev)
9980ca7111aSMatus Ujhelyi {
9993265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
10001ca6d1b1SMugunthan V N 	int ret;
10010ca7111aSMatus Ujhelyi 
10024f3a00c7SRobert Hancock 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
10034f3a00c7SRobert Hancock 		/* Some bootloaders leave the fiber page selected.
10043265f421SRobert Hancock 		 * Switch to the appropriate page (fiber or copper), as otherwise we
10053265f421SRobert Hancock 		 * read the PHY capabilities from the wrong page.
10064f3a00c7SRobert Hancock 		 */
10074f3a00c7SRobert Hancock 		phy_lock_mdio_bus(phydev);
10083265f421SRobert Hancock 		ret = at803x_write_page(phydev,
10093265f421SRobert Hancock 					priv->is_fiber ? AT803X_PAGE_FIBER :
10103265f421SRobert Hancock 							 AT803X_PAGE_COPPER);
10114f3a00c7SRobert Hancock 		phy_unlock_mdio_bus(phydev);
10124f3a00c7SRobert Hancock 		if (ret)
10134f3a00c7SRobert Hancock 			return ret;
10144f3a00c7SRobert Hancock 
10154f3a00c7SRobert Hancock 		ret = at8031_pll_config(phydev);
10164f3a00c7SRobert Hancock 		if (ret < 0)
10174f3a00c7SRobert Hancock 			return ret;
10184f3a00c7SRobert Hancock 	}
10194f3a00c7SRobert Hancock 
10206d4cd041SVinod Koul 	/* The RX and TX delay default is:
10216d4cd041SVinod Koul 	 *   after HW reset: RX delay enabled and TX delay disabled
10226d4cd041SVinod Koul 	 *   after SW reset: RX delay enabled, while TX delay retains the
10236d4cd041SVinod Koul 	 *   value before reset.
10246d4cd041SVinod Koul 	 */
1025bb0ce4c1SAndré Draszik 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1026bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1027bb0ce4c1SAndré Draszik 		ret = at803x_enable_rx_delay(phydev);
1028bb0ce4c1SAndré Draszik 	else
1029cd28d1d6SVinod Koul 		ret = at803x_disable_rx_delay(phydev);
10302e5f9f28SMartin Blumenstingl 	if (ret < 0)
10311ca6d1b1SMugunthan V N 		return ret;
10326d4cd041SVinod Koul 
10336d4cd041SVinod Koul 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1034bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
10356d4cd041SVinod Koul 		ret = at803x_enable_tx_delay(phydev);
1036bb0ce4c1SAndré Draszik 	else
1037bb0ce4c1SAndré Draszik 		ret = at803x_disable_tx_delay(phydev);
10382f664823SMichael Walle 	if (ret < 0)
10396d4cd041SVinod Koul 		return ret;
10402f664823SMichael Walle 
1041390b4cadSRussell King 	ret = at803x_smarteee_config(phydev);
1042390b4cadSRussell King 	if (ret < 0)
1043390b4cadSRussell King 		return ret;
1044390b4cadSRussell King 
10452f664823SMichael Walle 	ret = at803x_clk_out_config(phydev);
10462f664823SMichael Walle 	if (ret < 0)
10472f664823SMichael Walle 		return ret;
10482f664823SMichael Walle 
10499ecf0401SWei Fang 	ret = at803x_hibernation_mode_config(phydev);
10509ecf0401SWei Fang 	if (ret < 0)
10519ecf0401SWei Fang 		return ret;
10529ecf0401SWei Fang 
10533c51fa5dSRussell King 	/* Ar803x extended next page bit is enabled by default. Cisco
10543c51fa5dSRussell King 	 * multigig switches read this bit and attempt to negotiate 10Gbps
10553c51fa5dSRussell King 	 * rates even if the next page bit is disabled. This is incorrect
10563c51fa5dSRussell King 	 * behaviour but we still need to accommodate it. XNP is only needed
10573c51fa5dSRussell King 	 * for 10Gbps support, so disable XNP.
10583c51fa5dSRussell King 	 */
10593c51fa5dSRussell King 	return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
10600ca7111aSMatus Ujhelyi }
10610ca7111aSMatus Ujhelyi 
106277a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev)
106377a99394SZhao Qiang {
106477a99394SZhao Qiang 	int err;
106577a99394SZhao Qiang 
1066a46bd63bSMartin Blumenstingl 	err = phy_read(phydev, AT803X_INTR_STATUS);
106777a99394SZhao Qiang 
106877a99394SZhao Qiang 	return (err < 0) ? err : 0;
106977a99394SZhao Qiang }
107077a99394SZhao Qiang 
107177a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev)
107277a99394SZhao Qiang {
10733265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
107477a99394SZhao Qiang 	int err;
107577a99394SZhao Qiang 	int value;
107677a99394SZhao Qiang 
1077a46bd63bSMartin Blumenstingl 	value = phy_read(phydev, AT803X_INTR_ENABLE);
107877a99394SZhao Qiang 
1079e6e4a556SMartin Blumenstingl 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1080a3417885SIoana Ciornei 		/* Clear any pending interrupts */
1081a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
1082a3417885SIoana Ciornei 		if (err)
1083a3417885SIoana Ciornei 			return err;
1084a3417885SIoana Ciornei 
1085e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
1086e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
1087e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
1088e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_FAIL;
1089e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
10903265f421SRobert Hancock 		if (priv->is_fiber) {
10913265f421SRobert Hancock 			value |= AT803X_INTR_ENABLE_LINK_FAIL_BX;
10923265f421SRobert Hancock 			value |= AT803X_INTR_ENABLE_LINK_SUCCESS_BX;
10933265f421SRobert Hancock 		}
1094e6e4a556SMartin Blumenstingl 
1095e6e4a556SMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, value);
1096a3417885SIoana Ciornei 	} else {
1097a46bd63bSMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
1098a3417885SIoana Ciornei 		if (err)
1099a3417885SIoana Ciornei 			return err;
1100a3417885SIoana Ciornei 
1101a3417885SIoana Ciornei 		/* Clear any pending interrupts */
1102a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
1103a3417885SIoana Ciornei 	}
110477a99394SZhao Qiang 
110577a99394SZhao Qiang 	return err;
110677a99394SZhao Qiang }
110777a99394SZhao Qiang 
110829773097SIoana Ciornei static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev)
110929773097SIoana Ciornei {
111029773097SIoana Ciornei 	int irq_status, int_enabled;
111129773097SIoana Ciornei 
111229773097SIoana Ciornei 	irq_status = phy_read(phydev, AT803X_INTR_STATUS);
111329773097SIoana Ciornei 	if (irq_status < 0) {
111429773097SIoana Ciornei 		phy_error(phydev);
111529773097SIoana Ciornei 		return IRQ_NONE;
111629773097SIoana Ciornei 	}
111729773097SIoana Ciornei 
111829773097SIoana Ciornei 	/* Read the current enabled interrupts */
111929773097SIoana Ciornei 	int_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
112029773097SIoana Ciornei 	if (int_enabled < 0) {
112129773097SIoana Ciornei 		phy_error(phydev);
112229773097SIoana Ciornei 		return IRQ_NONE;
112329773097SIoana Ciornei 	}
112429773097SIoana Ciornei 
112529773097SIoana Ciornei 	/* See if this was one of our enabled interrupts */
112629773097SIoana Ciornei 	if (!(irq_status & int_enabled))
112729773097SIoana Ciornei 		return IRQ_NONE;
112829773097SIoana Ciornei 
112929773097SIoana Ciornei 	phy_trigger_machine(phydev);
113029773097SIoana Ciornei 
113129773097SIoana Ciornei 	return IRQ_HANDLED;
113229773097SIoana Ciornei }
113329773097SIoana Ciornei 
113413a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev)
113513a56b44SDaniel Mack {
113613a56b44SDaniel Mack 	/*
113713a56b44SDaniel Mack 	 * Conduct a hardware reset for AT8030 every time a link loss is
113813a56b44SDaniel Mack 	 * signalled. This is necessary to circumvent a hardware bug that
113913a56b44SDaniel Mack 	 * occurs when the cable is unplugged while TX packets are pending
114013a56b44SDaniel Mack 	 * in the FIFO. In such cases, the FIFO enters an error mode it
114113a56b44SDaniel Mack 	 * cannot recover from by software.
114213a56b44SDaniel Mack 	 */
11436110ed2dSDavid Bauer 	if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
114413a56b44SDaniel Mack 		struct at803x_context context;
114513a56b44SDaniel Mack 
114613a56b44SDaniel Mack 		at803x_context_save(phydev, &context);
114713a56b44SDaniel Mack 
1148bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 1);
114913a56b44SDaniel Mack 		msleep(1);
1150bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 0);
1151d57019d1SSergei Shtylyov 		msleep(1);
115213a56b44SDaniel Mack 
115313a56b44SDaniel Mack 		at803x_context_restore(phydev, &context);
115413a56b44SDaniel Mack 
11555c5f626bSHeiner Kallweit 		phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
115613a56b44SDaniel Mack 	}
115713a56b44SDaniel Mack }
115813a56b44SDaniel Mack 
115979c7bc05SLuo Jie static int at803x_read_specific_status(struct phy_device *phydev)
116006d5f344SRussell King {
116179c7bc05SLuo Jie 	int ss;
116206d5f344SRussell King 
116306d5f344SRussell King 	/* Read the AT8035 PHY-Specific Status register, which indicates the
116406d5f344SRussell King 	 * speed and duplex that the PHY is actually using, irrespective of
116506d5f344SRussell King 	 * whether we are in autoneg mode or not.
116606d5f344SRussell King 	 */
116706d5f344SRussell King 	ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
116806d5f344SRussell King 	if (ss < 0)
116906d5f344SRussell King 		return ss;
117006d5f344SRussell King 
117106d5f344SRussell King 	if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
117279c7bc05SLuo Jie 		int sfc, speed;
11737dce80c2SOleksij Rempel 
11747dce80c2SOleksij Rempel 		sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL);
11757dce80c2SOleksij Rempel 		if (sfc < 0)
11767dce80c2SOleksij Rempel 			return sfc;
11777dce80c2SOleksij Rempel 
117879c7bc05SLuo Jie 		/* qca8081 takes the different bits for speed value from at803x */
117979c7bc05SLuo Jie 		if (phydev->drv->phy_id == QCA8081_PHY_ID)
118079c7bc05SLuo Jie 			speed = FIELD_GET(QCA808X_SS_SPEED_MASK, ss);
118179c7bc05SLuo Jie 		else
118279c7bc05SLuo Jie 			speed = FIELD_GET(AT803X_SS_SPEED_MASK, ss);
118379c7bc05SLuo Jie 
118479c7bc05SLuo Jie 		switch (speed) {
118506d5f344SRussell King 		case AT803X_SS_SPEED_10:
118606d5f344SRussell King 			phydev->speed = SPEED_10;
118706d5f344SRussell King 			break;
118806d5f344SRussell King 		case AT803X_SS_SPEED_100:
118906d5f344SRussell King 			phydev->speed = SPEED_100;
119006d5f344SRussell King 			break;
119106d5f344SRussell King 		case AT803X_SS_SPEED_1000:
119206d5f344SRussell King 			phydev->speed = SPEED_1000;
119306d5f344SRussell King 			break;
119479c7bc05SLuo Jie 		case QCA808X_SS_SPEED_2500:
119579c7bc05SLuo Jie 			phydev->speed = SPEED_2500;
119679c7bc05SLuo Jie 			break;
119706d5f344SRussell King 		}
119806d5f344SRussell King 		if (ss & AT803X_SS_DUPLEX)
119906d5f344SRussell King 			phydev->duplex = DUPLEX_FULL;
120006d5f344SRussell King 		else
120106d5f344SRussell King 			phydev->duplex = DUPLEX_HALF;
12027dce80c2SOleksij Rempel 
120306d5f344SRussell King 		if (ss & AT803X_SS_MDIX)
120406d5f344SRussell King 			phydev->mdix = ETH_TP_MDI_X;
120506d5f344SRussell King 		else
120606d5f344SRussell King 			phydev->mdix = ETH_TP_MDI;
12077dce80c2SOleksij Rempel 
12087dce80c2SOleksij Rempel 		switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) {
12097dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDI:
12107dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
12117dce80c2SOleksij Rempel 			break;
12127dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDIX:
12137dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
12147dce80c2SOleksij Rempel 			break;
12157dce80c2SOleksij Rempel 		case AT803X_SFC_AUTOMATIC_CROSSOVER:
12167dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
12177dce80c2SOleksij Rempel 			break;
12187dce80c2SOleksij Rempel 		}
121906d5f344SRussell King 	}
122006d5f344SRussell King 
122179c7bc05SLuo Jie 	return 0;
122279c7bc05SLuo Jie }
122379c7bc05SLuo Jie 
122479c7bc05SLuo Jie static int at803x_read_status(struct phy_device *phydev)
122579c7bc05SLuo Jie {
12263265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
122779c7bc05SLuo Jie 	int err, old_link = phydev->link;
122879c7bc05SLuo Jie 
12293265f421SRobert Hancock 	if (priv->is_1000basex)
12303265f421SRobert Hancock 		return genphy_c37_read_status(phydev);
12313265f421SRobert Hancock 
123279c7bc05SLuo Jie 	/* Update the link, but return if there was an error */
123379c7bc05SLuo Jie 	err = genphy_update_link(phydev);
123479c7bc05SLuo Jie 	if (err)
123579c7bc05SLuo Jie 		return err;
123679c7bc05SLuo Jie 
123779c7bc05SLuo Jie 	/* why bother the PHY if nothing can have changed */
123879c7bc05SLuo Jie 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
123979c7bc05SLuo Jie 		return 0;
124079c7bc05SLuo Jie 
124179c7bc05SLuo Jie 	phydev->speed = SPEED_UNKNOWN;
124279c7bc05SLuo Jie 	phydev->duplex = DUPLEX_UNKNOWN;
124379c7bc05SLuo Jie 	phydev->pause = 0;
124479c7bc05SLuo Jie 	phydev->asym_pause = 0;
124579c7bc05SLuo Jie 
124679c7bc05SLuo Jie 	err = genphy_read_lpa(phydev);
124779c7bc05SLuo Jie 	if (err < 0)
124879c7bc05SLuo Jie 		return err;
124979c7bc05SLuo Jie 
125079c7bc05SLuo Jie 	err = at803x_read_specific_status(phydev);
125179c7bc05SLuo Jie 	if (err < 0)
125279c7bc05SLuo Jie 		return err;
125379c7bc05SLuo Jie 
125406d5f344SRussell King 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
125506d5f344SRussell King 		phy_resolve_aneg_pause(phydev);
125606d5f344SRussell King 
125706d5f344SRussell King 	return 0;
125806d5f344SRussell King }
125906d5f344SRussell King 
12607dce80c2SOleksij Rempel static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl)
12617dce80c2SOleksij Rempel {
12627dce80c2SOleksij Rempel 	u16 val;
12637dce80c2SOleksij Rempel 
12647dce80c2SOleksij Rempel 	switch (ctrl) {
12657dce80c2SOleksij Rempel 	case ETH_TP_MDI:
12667dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDI;
12677dce80c2SOleksij Rempel 		break;
12687dce80c2SOleksij Rempel 	case ETH_TP_MDI_X:
12697dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDIX;
12707dce80c2SOleksij Rempel 		break;
12717dce80c2SOleksij Rempel 	case ETH_TP_MDI_AUTO:
12727dce80c2SOleksij Rempel 		val = AT803X_SFC_AUTOMATIC_CROSSOVER;
12737dce80c2SOleksij Rempel 		break;
12747dce80c2SOleksij Rempel 	default:
12757dce80c2SOleksij Rempel 		return 0;
12767dce80c2SOleksij Rempel 	}
12777dce80c2SOleksij Rempel 
12787dce80c2SOleksij Rempel 	return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL,
12797dce80c2SOleksij Rempel 			  AT803X_SFC_MDI_CROSSOVER_MODE_M,
12807dce80c2SOleksij Rempel 			  FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val));
12817dce80c2SOleksij Rempel }
12827dce80c2SOleksij Rempel 
12837dce80c2SOleksij Rempel static int at803x_config_aneg(struct phy_device *phydev)
12847dce80c2SOleksij Rempel {
12853265f421SRobert Hancock 	struct at803x_priv *priv = phydev->priv;
12867dce80c2SOleksij Rempel 	int ret;
12877dce80c2SOleksij Rempel 
12887dce80c2SOleksij Rempel 	ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
12897dce80c2SOleksij Rempel 	if (ret < 0)
12907dce80c2SOleksij Rempel 		return ret;
12917dce80c2SOleksij Rempel 
12927dce80c2SOleksij Rempel 	/* Changes of the midx bits are disruptive to the normal operation;
12937dce80c2SOleksij Rempel 	 * therefore any changes to these registers must be followed by a
12947dce80c2SOleksij Rempel 	 * software reset to take effect.
12957dce80c2SOleksij Rempel 	 */
12967dce80c2SOleksij Rempel 	if (ret == 1) {
12977dce80c2SOleksij Rempel 		ret = genphy_soft_reset(phydev);
12987dce80c2SOleksij Rempel 		if (ret < 0)
12997dce80c2SOleksij Rempel 			return ret;
13007dce80c2SOleksij Rempel 	}
13017dce80c2SOleksij Rempel 
13023265f421SRobert Hancock 	if (priv->is_1000basex)
13033265f421SRobert Hancock 		return genphy_c37_config_aneg(phydev);
13043265f421SRobert Hancock 
1305f884d449SLuo Jie 	/* Do not restart auto-negotiation by setting ret to 0 defautly,
1306f884d449SLuo Jie 	 * when calling __genphy_config_aneg later.
1307f884d449SLuo Jie 	 */
1308f884d449SLuo Jie 	ret = 0;
1309f884d449SLuo Jie 
1310f884d449SLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID) {
1311f884d449SLuo Jie 		int phy_ctrl = 0;
1312f884d449SLuo Jie 
1313f884d449SLuo Jie 		/* The reg MII_BMCR also needs to be configured for force mode, the
1314f884d449SLuo Jie 		 * genphy_config_aneg is also needed.
1315f884d449SLuo Jie 		 */
1316f884d449SLuo Jie 		if (phydev->autoneg == AUTONEG_DISABLE)
1317f884d449SLuo Jie 			genphy_c45_pma_setup_forced(phydev);
1318f884d449SLuo Jie 
1319f884d449SLuo Jie 		if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->advertising))
1320f884d449SLuo Jie 			phy_ctrl = MDIO_AN_10GBT_CTRL_ADV2_5G;
1321f884d449SLuo Jie 
1322f884d449SLuo Jie 		ret = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
1323f884d449SLuo Jie 				MDIO_AN_10GBT_CTRL_ADV2_5G, phy_ctrl);
1324f884d449SLuo Jie 		if (ret < 0)
1325f884d449SLuo Jie 			return ret;
1326f884d449SLuo Jie 	}
1327f884d449SLuo Jie 
1328f884d449SLuo Jie 	return __genphy_config_aneg(phydev, ret);
13297dce80c2SOleksij Rempel }
13307dce80c2SOleksij Rempel 
1331cde0f4f8SMichael Walle static int at803x_get_downshift(struct phy_device *phydev, u8 *d)
1332cde0f4f8SMichael Walle {
1333cde0f4f8SMichael Walle 	int val;
1334cde0f4f8SMichael Walle 
1335cde0f4f8SMichael Walle 	val = phy_read(phydev, AT803X_SMART_SPEED);
1336cde0f4f8SMichael Walle 	if (val < 0)
1337cde0f4f8SMichael Walle 		return val;
1338cde0f4f8SMichael Walle 
1339cde0f4f8SMichael Walle 	if (val & AT803X_SMART_SPEED_ENABLE)
1340cde0f4f8SMichael Walle 		*d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2;
1341cde0f4f8SMichael Walle 	else
1342cde0f4f8SMichael Walle 		*d = DOWNSHIFT_DEV_DISABLE;
1343cde0f4f8SMichael Walle 
1344cde0f4f8SMichael Walle 	return 0;
1345cde0f4f8SMichael Walle }
1346cde0f4f8SMichael Walle 
1347cde0f4f8SMichael Walle static int at803x_set_downshift(struct phy_device *phydev, u8 cnt)
1348cde0f4f8SMichael Walle {
1349cde0f4f8SMichael Walle 	u16 mask, set;
1350cde0f4f8SMichael Walle 	int ret;
1351cde0f4f8SMichael Walle 
1352cde0f4f8SMichael Walle 	switch (cnt) {
1353cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DEFAULT_COUNT:
1354cde0f4f8SMichael Walle 		cnt = AT803X_DEFAULT_DOWNSHIFT;
1355cde0f4f8SMichael Walle 		fallthrough;
1356cde0f4f8SMichael Walle 	case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT:
1357cde0f4f8SMichael Walle 		set = AT803X_SMART_SPEED_ENABLE |
1358cde0f4f8SMichael Walle 		      AT803X_SMART_SPEED_BYPASS_TIMER |
1359cde0f4f8SMichael Walle 		      FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2);
1360cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK;
1361cde0f4f8SMichael Walle 		break;
1362cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DISABLE:
1363cde0f4f8SMichael Walle 		set = 0;
1364cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_ENABLE |
1365cde0f4f8SMichael Walle 		       AT803X_SMART_SPEED_BYPASS_TIMER;
1366cde0f4f8SMichael Walle 		break;
1367cde0f4f8SMichael Walle 	default:
1368cde0f4f8SMichael Walle 		return -EINVAL;
1369cde0f4f8SMichael Walle 	}
1370cde0f4f8SMichael Walle 
1371cde0f4f8SMichael Walle 	ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set);
1372cde0f4f8SMichael Walle 
1373cde0f4f8SMichael Walle 	/* After changing the smart speed settings, we need to perform a
1374cde0f4f8SMichael Walle 	 * software reset, use phy_init_hw() to make sure we set the
1375cde0f4f8SMichael Walle 	 * reapply any values which might got lost during software reset.
1376cde0f4f8SMichael Walle 	 */
1377cde0f4f8SMichael Walle 	if (ret == 1)
1378cde0f4f8SMichael Walle 		ret = phy_init_hw(phydev);
1379cde0f4f8SMichael Walle 
1380cde0f4f8SMichael Walle 	return ret;
1381cde0f4f8SMichael Walle }
1382cde0f4f8SMichael Walle 
1383cde0f4f8SMichael Walle static int at803x_get_tunable(struct phy_device *phydev,
1384cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, void *data)
1385cde0f4f8SMichael Walle {
1386cde0f4f8SMichael Walle 	switch (tuna->id) {
1387cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1388cde0f4f8SMichael Walle 		return at803x_get_downshift(phydev, data);
1389cde0f4f8SMichael Walle 	default:
1390cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1391cde0f4f8SMichael Walle 	}
1392cde0f4f8SMichael Walle }
1393cde0f4f8SMichael Walle 
1394cde0f4f8SMichael Walle static int at803x_set_tunable(struct phy_device *phydev,
1395cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, const void *data)
1396cde0f4f8SMichael Walle {
1397cde0f4f8SMichael Walle 	switch (tuna->id) {
1398cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1399cde0f4f8SMichael Walle 		return at803x_set_downshift(phydev, *(const u8 *)data);
1400cde0f4f8SMichael Walle 	default:
1401cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1402cde0f4f8SMichael Walle 	}
1403cde0f4f8SMichael Walle }
1404cde0f4f8SMichael Walle 
14056cb75767SMichael Walle static int at803x_cable_test_result_trans(u16 status)
14066cb75767SMichael Walle {
14076cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
14086cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_NORMAL:
14096cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
14106cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
14116cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
14126cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
14136cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
14146cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_FAIL:
14156cb75767SMichael Walle 	default:
14166cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
14176cb75767SMichael Walle 	}
14186cb75767SMichael Walle }
14196cb75767SMichael Walle 
14206cb75767SMichael Walle static bool at803x_cdt_test_failed(u16 status)
14216cb75767SMichael Walle {
14226cb75767SMichael Walle 	return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
14236cb75767SMichael Walle 		AT803X_CDT_STATUS_STAT_FAIL;
14246cb75767SMichael Walle }
14256cb75767SMichael Walle 
14266cb75767SMichael Walle static bool at803x_cdt_fault_length_valid(u16 status)
14276cb75767SMichael Walle {
14286cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
14296cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
14306cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
14316cb75767SMichael Walle 		return true;
14326cb75767SMichael Walle 	}
14336cb75767SMichael Walle 	return false;
14346cb75767SMichael Walle }
14356cb75767SMichael Walle 
14366cb75767SMichael Walle static int at803x_cdt_fault_length(u16 status)
14376cb75767SMichael Walle {
14386cb75767SMichael Walle 	int dt;
14396cb75767SMichael Walle 
14406cb75767SMichael Walle 	/* According to the datasheet the distance to the fault is
14416cb75767SMichael Walle 	 * DELTA_TIME * 0.824 meters.
14426cb75767SMichael Walle 	 *
14436cb75767SMichael Walle 	 * The author suspect the correct formula is:
14446cb75767SMichael Walle 	 *
14456cb75767SMichael Walle 	 *   fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2
14466cb75767SMichael Walle 	 *
14476cb75767SMichael Walle 	 * where c is the speed of light, VF is the velocity factor of
14486cb75767SMichael Walle 	 * the twisted pair cable, 125MHz the counter frequency and
14496cb75767SMichael Walle 	 * we need to divide by 2 because the hardware will measure the
14506cb75767SMichael Walle 	 * round trip time to the fault and back to the PHY.
14516cb75767SMichael Walle 	 *
14526cb75767SMichael Walle 	 * With a VF of 0.69 we get the factor 0.824 mentioned in the
14536cb75767SMichael Walle 	 * datasheet.
14546cb75767SMichael Walle 	 */
14556cb75767SMichael Walle 	dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status);
14566cb75767SMichael Walle 
14576cb75767SMichael Walle 	return (dt * 824) / 10;
14586cb75767SMichael Walle }
14596cb75767SMichael Walle 
14606cb75767SMichael Walle static int at803x_cdt_start(struct phy_device *phydev, int pair)
14616cb75767SMichael Walle {
14626cb75767SMichael Walle 	u16 cdt;
14636cb75767SMichael Walle 
14648c84d752SLuo Jie 	/* qca8081 takes the different bit 15 to enable CDT test */
14658c84d752SLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID)
14668c84d752SLuo Jie 		cdt = QCA808X_CDT_ENABLE_TEST |
14678c84d752SLuo Jie 			QCA808X_CDT_LENGTH_UNIT |
14688c84d752SLuo Jie 			QCA808X_CDT_INTER_CHECK_DIS;
14698c84d752SLuo Jie 	else
14706cb75767SMichael Walle 		cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
14716cb75767SMichael Walle 			AT803X_CDT_ENABLE_TEST;
14726cb75767SMichael Walle 
14736cb75767SMichael Walle 	return phy_write(phydev, AT803X_CDT, cdt);
14746cb75767SMichael Walle }
14756cb75767SMichael Walle 
14766cb75767SMichael Walle static int at803x_cdt_wait_for_completion(struct phy_device *phydev)
14776cb75767SMichael Walle {
14786cb75767SMichael Walle 	int val, ret;
14798c84d752SLuo Jie 	u16 cdt_en;
14808c84d752SLuo Jie 
14818c84d752SLuo Jie 	if (phydev->drv->phy_id == QCA8081_PHY_ID)
14828c84d752SLuo Jie 		cdt_en = QCA808X_CDT_ENABLE_TEST;
14838c84d752SLuo Jie 	else
14848c84d752SLuo Jie 		cdt_en = AT803X_CDT_ENABLE_TEST;
14856cb75767SMichael Walle 
14866cb75767SMichael Walle 	/* One test run takes about 25ms */
14876cb75767SMichael Walle 	ret = phy_read_poll_timeout(phydev, AT803X_CDT, val,
14888c84d752SLuo Jie 				    !(val & cdt_en),
14896cb75767SMichael Walle 				    30000, 100000, true);
14906cb75767SMichael Walle 
14916cb75767SMichael Walle 	return ret < 0 ? ret : 0;
14926cb75767SMichael Walle }
14936cb75767SMichael Walle 
14946cb75767SMichael Walle static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
14956cb75767SMichael Walle {
14966cb75767SMichael Walle 	static const int ethtool_pair[] = {
14976cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_A,
14986cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_B,
14996cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_C,
15006cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_D,
15016cb75767SMichael Walle 	};
15026cb75767SMichael Walle 	int ret, val;
15036cb75767SMichael Walle 
15046cb75767SMichael Walle 	ret = at803x_cdt_start(phydev, pair);
15056cb75767SMichael Walle 	if (ret)
15066cb75767SMichael Walle 		return ret;
15076cb75767SMichael Walle 
15086cb75767SMichael Walle 	ret = at803x_cdt_wait_for_completion(phydev);
15096cb75767SMichael Walle 	if (ret)
15106cb75767SMichael Walle 		return ret;
15116cb75767SMichael Walle 
15126cb75767SMichael Walle 	val = phy_read(phydev, AT803X_CDT_STATUS);
15136cb75767SMichael Walle 	if (val < 0)
15146cb75767SMichael Walle 		return val;
15156cb75767SMichael Walle 
15166cb75767SMichael Walle 	if (at803x_cdt_test_failed(val))
15176cb75767SMichael Walle 		return 0;
15186cb75767SMichael Walle 
15196cb75767SMichael Walle 	ethnl_cable_test_result(phydev, ethtool_pair[pair],
15206cb75767SMichael Walle 				at803x_cable_test_result_trans(val));
15216cb75767SMichael Walle 
15226cb75767SMichael Walle 	if (at803x_cdt_fault_length_valid(val))
15236cb75767SMichael Walle 		ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
15246cb75767SMichael Walle 					      at803x_cdt_fault_length(val));
15256cb75767SMichael Walle 
15266cb75767SMichael Walle 	return 1;
15276cb75767SMichael Walle }
15286cb75767SMichael Walle 
15296cb75767SMichael Walle static int at803x_cable_test_get_status(struct phy_device *phydev,
15306cb75767SMichael Walle 					bool *finished)
15316cb75767SMichael Walle {
1532dc0f3ed1SOleksij Rempel 	unsigned long pair_mask;
15336cb75767SMichael Walle 	int retries = 20;
15346cb75767SMichael Walle 	int pair, ret;
15356cb75767SMichael Walle 
1536dc0f3ed1SOleksij Rempel 	if (phydev->phy_id == ATH9331_PHY_ID ||
1537fada2ce0SDavid Bauer 	    phydev->phy_id == ATH8032_PHY_ID ||
1538fada2ce0SDavid Bauer 	    phydev->phy_id == QCA9561_PHY_ID)
1539dc0f3ed1SOleksij Rempel 		pair_mask = 0x3;
1540dc0f3ed1SOleksij Rempel 	else
1541dc0f3ed1SOleksij Rempel 		pair_mask = 0xf;
1542dc0f3ed1SOleksij Rempel 
15436cb75767SMichael Walle 	*finished = false;
15446cb75767SMichael Walle 
15456cb75767SMichael Walle 	/* According to the datasheet the CDT can be performed when
15466cb75767SMichael Walle 	 * there is no link partner or when the link partner is
15476cb75767SMichael Walle 	 * auto-negotiating. Starting the test will restart the AN
15486cb75767SMichael Walle 	 * automatically. It seems that doing this repeatedly we will
15496cb75767SMichael Walle 	 * get a slot where our link partner won't disturb our
15506cb75767SMichael Walle 	 * measurement.
15516cb75767SMichael Walle 	 */
15526cb75767SMichael Walle 	while (pair_mask && retries--) {
15536cb75767SMichael Walle 		for_each_set_bit(pair, &pair_mask, 4) {
15546cb75767SMichael Walle 			ret = at803x_cable_test_one_pair(phydev, pair);
15556cb75767SMichael Walle 			if (ret < 0)
15566cb75767SMichael Walle 				return ret;
15576cb75767SMichael Walle 			if (ret)
15586cb75767SMichael Walle 				clear_bit(pair, &pair_mask);
15596cb75767SMichael Walle 		}
15606cb75767SMichael Walle 		if (pair_mask)
15616cb75767SMichael Walle 			msleep(250);
15626cb75767SMichael Walle 	}
15636cb75767SMichael Walle 
15646cb75767SMichael Walle 	*finished = true;
15656cb75767SMichael Walle 
15666cb75767SMichael Walle 	return 0;
15676cb75767SMichael Walle }
15686cb75767SMichael Walle 
15696cb75767SMichael Walle static int at803x_cable_test_start(struct phy_device *phydev)
15706cb75767SMichael Walle {
15716cb75767SMichael Walle 	/* Enable auto-negotiation, but advertise no capabilities, no link
15726cb75767SMichael Walle 	 * will be established. A restart of the auto-negotiation is not
15736cb75767SMichael Walle 	 * required, because the cable test will automatically break the link.
15746cb75767SMichael Walle 	 */
15756cb75767SMichael Walle 	phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
15766cb75767SMichael Walle 	phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
1577dc0f3ed1SOleksij Rempel 	if (phydev->phy_id != ATH9331_PHY_ID &&
1578fada2ce0SDavid Bauer 	    phydev->phy_id != ATH8032_PHY_ID &&
1579fada2ce0SDavid Bauer 	    phydev->phy_id != QCA9561_PHY_ID)
15806cb75767SMichael Walle 		phy_write(phydev, MII_CTRL1000, 0);
15816cb75767SMichael Walle 
15826cb75767SMichael Walle 	/* we do all the (time consuming) work later */
15836cb75767SMichael Walle 	return 0;
15846cb75767SMichael Walle }
15856cb75767SMichael Walle 
1586272833b9SAnsuel Smith static int qca83xx_config_init(struct phy_device *phydev)
1587272833b9SAnsuel Smith {
1588272833b9SAnsuel Smith 	u8 switch_revision;
1589272833b9SAnsuel Smith 
1590272833b9SAnsuel Smith 	switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK;
1591272833b9SAnsuel Smith 
1592272833b9SAnsuel Smith 	switch (switch_revision) {
1593272833b9SAnsuel Smith 	case 1:
1594272833b9SAnsuel Smith 		/* For 100M waveform */
159567999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL, 0x02ea);
1596272833b9SAnsuel Smith 		/* Turn on Gigabit clock */
159767999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x68a0);
1598272833b9SAnsuel Smith 		break;
1599272833b9SAnsuel Smith 
1600272833b9SAnsuel Smith 	case 2:
1601272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0);
1602272833b9SAnsuel Smith 		fallthrough;
1603272833b9SAnsuel Smith 	case 4:
1604272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f);
160567999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_GREEN, 0x6860);
160667999555SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_SYSTEM_CTRL_MODE, 0x2c46);
1607272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000);
1608272833b9SAnsuel Smith 		break;
1609272833b9SAnsuel Smith 	}
1610272833b9SAnsuel Smith 
16111ca83119SAnsuel Smith 	/* QCA8327 require DAC amplitude adjustment for 100m set to +6%.
16121ca83119SAnsuel Smith 	 * Disable on init and enable only with 100m speed following
16131ca83119SAnsuel Smith 	 * qca original source code.
16141ca83119SAnsuel Smith 	 */
16151ca83119SAnsuel Smith 	if (phydev->drv->phy_id == QCA8327_A_PHY_ID ||
16161ca83119SAnsuel Smith 	    phydev->drv->phy_id == QCA8327_B_PHY_ID)
161767999555SAnsuel Smith 		at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
16181ca83119SAnsuel Smith 				      QCA8327_DEBUG_MANU_CTRL_EN, 0);
16191ca83119SAnsuel Smith 
16209d1c29b4SAnsuel Smith 	/* Following original QCA sourcecode set port to prefer master */
16219d1c29b4SAnsuel Smith 	phy_set_bits(phydev, MII_CTRL1000, CTL1000_PREFER_MASTER);
16229d1c29b4SAnsuel Smith 
1623272833b9SAnsuel Smith 	return 0;
1624272833b9SAnsuel Smith }
1625272833b9SAnsuel Smith 
16261ca83119SAnsuel Smith static void qca83xx_link_change_notify(struct phy_device *phydev)
16271ca83119SAnsuel Smith {
16281ca83119SAnsuel Smith 	/* QCA8337 doesn't require DAC Amplitude adjustement */
16291ca83119SAnsuel Smith 	if (phydev->drv->phy_id == QCA8337_PHY_ID)
16301ca83119SAnsuel Smith 		return;
16311ca83119SAnsuel Smith 
16321ca83119SAnsuel Smith 	/* Set DAC Amplitude adjustment to +6% for 100m on link running */
16331ca83119SAnsuel Smith 	if (phydev->state == PHY_RUNNING) {
16341ca83119SAnsuel Smith 		if (phydev->speed == SPEED_100)
163567999555SAnsuel Smith 			at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
16361ca83119SAnsuel Smith 					      QCA8327_DEBUG_MANU_CTRL_EN,
16371ca83119SAnsuel Smith 					      QCA8327_DEBUG_MANU_CTRL_EN);
16381ca83119SAnsuel Smith 	} else {
16391ca83119SAnsuel Smith 		/* Reset DAC Amplitude adjustment */
164067999555SAnsuel Smith 		at803x_debug_reg_mask(phydev, AT803X_DEBUG_ANALOG_TEST_CTRL,
16411ca83119SAnsuel Smith 				      QCA8327_DEBUG_MANU_CTRL_EN, 0);
16421ca83119SAnsuel Smith 	}
16431ca83119SAnsuel Smith }
16441ca83119SAnsuel Smith 
1645ba3c01eeSAnsuel Smith static int qca83xx_resume(struct phy_device *phydev)
1646ba3c01eeSAnsuel Smith {
1647ba3c01eeSAnsuel Smith 	int ret, val;
1648ba3c01eeSAnsuel Smith 
1649ba3c01eeSAnsuel Smith 	/* Skip reset if not suspended */
1650ba3c01eeSAnsuel Smith 	if (!phydev->suspended)
1651ba3c01eeSAnsuel Smith 		return 0;
1652ba3c01eeSAnsuel Smith 
1653ba3c01eeSAnsuel Smith 	/* Reinit the port, reset values set by suspend */
1654ba3c01eeSAnsuel Smith 	qca83xx_config_init(phydev);
1655ba3c01eeSAnsuel Smith 
1656ba3c01eeSAnsuel Smith 	/* Reset the port on port resume */
1657ba3c01eeSAnsuel Smith 	phy_set_bits(phydev, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
1658ba3c01eeSAnsuel Smith 
1659ba3c01eeSAnsuel Smith 	/* On resume from suspend the switch execute a reset and
1660ba3c01eeSAnsuel Smith 	 * restart auto-negotiation. Wait for reset to complete.
1661ba3c01eeSAnsuel Smith 	 */
1662ba3c01eeSAnsuel Smith 	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1663ba3c01eeSAnsuel Smith 				    50000, 600000, true);
1664ba3c01eeSAnsuel Smith 	if (ret)
1665ba3c01eeSAnsuel Smith 		return ret;
1666ba3c01eeSAnsuel Smith 
1667ba3c01eeSAnsuel Smith 	msleep(1);
1668ba3c01eeSAnsuel Smith 
1669ba3c01eeSAnsuel Smith 	return 0;
1670ba3c01eeSAnsuel Smith }
1671ba3c01eeSAnsuel Smith 
1672ba3c01eeSAnsuel Smith static int qca83xx_suspend(struct phy_device *phydev)
1673ba3c01eeSAnsuel Smith {
1674ba3c01eeSAnsuel Smith 	u16 mask = 0;
1675ba3c01eeSAnsuel Smith 
1676ba3c01eeSAnsuel Smith 	/* Only QCA8337 support actual suspend.
1677ba3c01eeSAnsuel Smith 	 * QCA8327 cause port unreliability when phy suspend
1678ba3c01eeSAnsuel Smith 	 * is set.
1679ba3c01eeSAnsuel Smith 	 */
1680ba3c01eeSAnsuel Smith 	if (phydev->drv->phy_id == QCA8337_PHY_ID) {
1681ba3c01eeSAnsuel Smith 		genphy_suspend(phydev);
1682ba3c01eeSAnsuel Smith 	} else {
1683ba3c01eeSAnsuel Smith 		mask |= ~(BMCR_SPEED1000 | BMCR_FULLDPLX);
1684ba3c01eeSAnsuel Smith 		phy_modify(phydev, MII_BMCR, mask, 0);
1685ba3c01eeSAnsuel Smith 	}
1686ba3c01eeSAnsuel Smith 
168767999555SAnsuel Smith 	at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_GREEN,
1688ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_GATE_CLK_IN1000, 0);
1689ba3c01eeSAnsuel Smith 
1690ba3c01eeSAnsuel Smith 	at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
1691ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_HIB_CTRL_EN_ANY_CHANGE |
1692ba3c01eeSAnsuel Smith 			      AT803X_DEBUG_HIB_CTRL_SEL_RST_80U, 0);
1693ba3c01eeSAnsuel Smith 
1694ba3c01eeSAnsuel Smith 	return 0;
1695ba3c01eeSAnsuel Smith }
1696ba3c01eeSAnsuel Smith 
16972acdd43fSLuo Jie static int qca808x_phy_fast_retrain_config(struct phy_device *phydev)
16982acdd43fSLuo Jie {
16992acdd43fSLuo Jie 	int ret;
17002acdd43fSLuo Jie 
17012acdd43fSLuo Jie 	/* Enable fast retrain */
17022acdd43fSLuo Jie 	ret = genphy_c45_fast_retrain(phydev, true);
17032acdd43fSLuo Jie 	if (ret)
17042acdd43fSLuo Jie 		return ret;
17052acdd43fSLuo Jie 
17062acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_AN, QCA808X_PHY_MMD7_TOP_OPTION1,
17072acdd43fSLuo Jie 			QCA808X_TOP_OPTION1_DATA);
17082acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_20DB,
17092acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_20DB_VALUE);
17102acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_17DB,
17112acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_17DB_VALUE);
17122acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_27DB,
17132acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_27DB_VALUE);
17142acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PMAPMD, QCA808X_PHY_MMD1_MSE_THRESHOLD_28DB,
17152acdd43fSLuo Jie 			QCA808X_MSE_THRESHOLD_28DB_VALUE);
17162acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_1,
17172acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_1_VALUE);
17182acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_4,
17192acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_4_VALUE);
17202acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_5,
17212acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_5_VALUE);
17222acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_3,
17232acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_3_VALUE);
17242acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_6,
17252acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_6_VALUE);
17262acdd43fSLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_DEBUG_2,
17272acdd43fSLuo Jie 			QCA808X_MMD3_DEBUG_2_VALUE);
17282acdd43fSLuo Jie 
17292acdd43fSLuo Jie 	return 0;
17302acdd43fSLuo Jie }
17312acdd43fSLuo Jie 
17329d4dae29SLuo Jie static int qca808x_phy_ms_seed_enable(struct phy_device *phydev, bool enable)
17339d4dae29SLuo Jie {
1734f3db55aeSLuo Jie 	u16 seed_value;
17359d4dae29SLuo Jie 
1736f3db55aeSLuo Jie 	if (!enable)
17379d4dae29SLuo Jie 		return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED,
1738f3db55aeSLuo Jie 				QCA808X_MASTER_SLAVE_SEED_ENABLE, 0);
1739f3db55aeSLuo Jie 
1740f3db55aeSLuo Jie 	seed_value = get_random_u32_below(QCA808X_MASTER_SLAVE_SEED_RANGE);
1741f3db55aeSLuo Jie 	return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_LOCAL_SEED,
1742f3db55aeSLuo Jie 			QCA808X_MASTER_SLAVE_SEED_CFG | QCA808X_MASTER_SLAVE_SEED_ENABLE,
1743f3db55aeSLuo Jie 			FIELD_PREP(QCA808X_MASTER_SLAVE_SEED_CFG, seed_value) |
1744f3db55aeSLuo Jie 			QCA808X_MASTER_SLAVE_SEED_ENABLE);
17459d4dae29SLuo Jie }
17469d4dae29SLuo Jie 
17477cc32095SLuo Jie static bool qca808x_is_prefer_master(struct phy_device *phydev)
17487cc32095SLuo Jie {
17497cc32095SLuo Jie 	return (phydev->master_slave_get == MASTER_SLAVE_CFG_MASTER_FORCE) ||
17507cc32095SLuo Jie 		(phydev->master_slave_get == MASTER_SLAVE_CFG_MASTER_PREFERRED);
17517cc32095SLuo Jie }
17527cc32095SLuo Jie 
17532acdd43fSLuo Jie static int qca808x_config_init(struct phy_device *phydev)
17542acdd43fSLuo Jie {
17552acdd43fSLuo Jie 	int ret;
17562acdd43fSLuo Jie 
17572acdd43fSLuo Jie 	/* Active adc&vga on 802.3az for the link 1000M and 100M */
17582acdd43fSLuo Jie 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, QCA808X_PHY_MMD3_ADDR_CLD_CTRL7,
17592acdd43fSLuo Jie 			QCA808X_8023AZ_AFE_CTRL_MASK, QCA808X_8023AZ_AFE_EN);
17602acdd43fSLuo Jie 	if (ret)
17612acdd43fSLuo Jie 		return ret;
17622acdd43fSLuo Jie 
17632acdd43fSLuo Jie 	/* Adjust the threshold on 802.3az for the link 1000M */
17642acdd43fSLuo Jie 	ret = phy_write_mmd(phydev, MDIO_MMD_PCS,
17652acdd43fSLuo Jie 			QCA808X_PHY_MMD3_AZ_TRAINING_CTRL, QCA808X_MMD3_AZ_TRAINING_VAL);
17662acdd43fSLuo Jie 	if (ret)
17672acdd43fSLuo Jie 		return ret;
17682acdd43fSLuo Jie 
17692acdd43fSLuo Jie 	/* Config the fast retrain for the link 2500M */
17702acdd43fSLuo Jie 	ret = qca808x_phy_fast_retrain_config(phydev);
17712acdd43fSLuo Jie 	if (ret)
17722acdd43fSLuo Jie 		return ret;
17732acdd43fSLuo Jie 
17747cc32095SLuo Jie 	ret = genphy_read_master_slave(phydev);
17757cc32095SLuo Jie 	if (ret < 0)
17767cc32095SLuo Jie 		return ret;
17777cc32095SLuo Jie 
17787cc32095SLuo Jie 	if (!qca808x_is_prefer_master(phydev)) {
1779f3db55aeSLuo Jie 		/* Enable seed and configure lower ramdom seed to make phy linked as slave mode */
17809d4dae29SLuo Jie 		ret = qca808x_phy_ms_seed_enable(phydev, true);
17819d4dae29SLuo Jie 		if (ret)
17829d4dae29SLuo Jie 			return ret;
17837cc32095SLuo Jie 	}
17849d4dae29SLuo Jie 
17852acdd43fSLuo Jie 	/* Configure adc threshold as 100mv for the link 10M */
17862acdd43fSLuo Jie 	return at803x_debug_reg_mask(phydev, QCA808X_PHY_DEBUG_ADC_THRESHOLD,
17872acdd43fSLuo Jie 			QCA808X_ADC_THRESHOLD_MASK, QCA808X_ADC_THRESHOLD_100MV);
17882acdd43fSLuo Jie }
17892acdd43fSLuo Jie 
179079c7bc05SLuo Jie static int qca808x_read_status(struct phy_device *phydev)
179179c7bc05SLuo Jie {
179279c7bc05SLuo Jie 	int ret;
179379c7bc05SLuo Jie 
179479c7bc05SLuo Jie 	ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_10GBT_STAT);
179579c7bc05SLuo Jie 	if (ret < 0)
179679c7bc05SLuo Jie 		return ret;
179779c7bc05SLuo Jie 
179879c7bc05SLuo Jie 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->lp_advertising,
179979c7bc05SLuo Jie 			ret & MDIO_AN_10GBT_STAT_LP2_5G);
180079c7bc05SLuo Jie 
180179c7bc05SLuo Jie 	ret = genphy_read_status(phydev);
180279c7bc05SLuo Jie 	if (ret)
180379c7bc05SLuo Jie 		return ret;
180479c7bc05SLuo Jie 
180579c7bc05SLuo Jie 	ret = at803x_read_specific_status(phydev);
180679c7bc05SLuo Jie 	if (ret < 0)
180779c7bc05SLuo Jie 		return ret;
180879c7bc05SLuo Jie 
1809881cc731SJonathan McDowell 	if (phydev->link) {
1810881cc731SJonathan McDowell 		if (phydev->speed == SPEED_2500)
181179c7bc05SLuo Jie 			phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
181279c7bc05SLuo Jie 		else
1813881cc731SJonathan McDowell 			phydev->interface = PHY_INTERFACE_MODE_SGMII;
1814881cc731SJonathan McDowell 	} else {
18158bc1c543SLuo Jie 		/* generate seed as a lower random value to make PHY linked as SLAVE easily,
18167cc32095SLuo Jie 		 * except for master/slave configuration fault detected or the master mode
18177cc32095SLuo Jie 		 * preferred.
18187cc32095SLuo Jie 		 *
18198bc1c543SLuo Jie 		 * the reason for not putting this code into the function link_change_notify is
18208bc1c543SLuo Jie 		 * the corner case where the link partner is also the qca8081 PHY and the seed
18218bc1c543SLuo Jie 		 * value is configured as the same value, the link can't be up and no link change
18228bc1c543SLuo Jie 		 * occurs.
18238bc1c543SLuo Jie 		 */
18247cc32095SLuo Jie 		if (phydev->master_slave_state == MASTER_SLAVE_STATE_ERR ||
18257cc32095SLuo Jie 				qca808x_is_prefer_master(phydev)) {
18268bc1c543SLuo Jie 			qca808x_phy_ms_seed_enable(phydev, false);
18278bc1c543SLuo Jie 		} else {
18288bc1c543SLuo Jie 			qca808x_phy_ms_seed_enable(phydev, true);
18298bc1c543SLuo Jie 		}
18308bc1c543SLuo Jie 	}
18318bc1c543SLuo Jie 
183279c7bc05SLuo Jie 	return 0;
183379c7bc05SLuo Jie }
183479c7bc05SLuo Jie 
18359d4dae29SLuo Jie static int qca808x_soft_reset(struct phy_device *phydev)
18369d4dae29SLuo Jie {
18379d4dae29SLuo Jie 	int ret;
18389d4dae29SLuo Jie 
18399d4dae29SLuo Jie 	ret = genphy_soft_reset(phydev);
18409d4dae29SLuo Jie 	if (ret < 0)
18419d4dae29SLuo Jie 		return ret;
18429d4dae29SLuo Jie 
18439d4dae29SLuo Jie 	return qca808x_phy_ms_seed_enable(phydev, true);
18449d4dae29SLuo Jie }
18459d4dae29SLuo Jie 
18468c84d752SLuo Jie static bool qca808x_cdt_fault_length_valid(int cdt_code)
18478c84d752SLuo Jie {
18488c84d752SLuo Jie 	switch (cdt_code) {
18498c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_SHORT:
18508c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_OPEN:
18518c84d752SLuo Jie 		return true;
18528c84d752SLuo Jie 	default:
18538c84d752SLuo Jie 		return false;
18548c84d752SLuo Jie 	}
18558c84d752SLuo Jie }
18568c84d752SLuo Jie 
18578c84d752SLuo Jie static int qca808x_cable_test_result_trans(int cdt_code)
18588c84d752SLuo Jie {
18598c84d752SLuo Jie 	switch (cdt_code) {
18608c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_NORMAL:
18618c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
18628c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_SHORT:
18638c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
18648c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_OPEN:
18658c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
18668c84d752SLuo Jie 	case QCA808X_CDT_STATUS_STAT_FAIL:
18678c84d752SLuo Jie 	default:
18688c84d752SLuo Jie 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
18698c84d752SLuo Jie 	}
18708c84d752SLuo Jie }
18718c84d752SLuo Jie 
18728c84d752SLuo Jie static int qca808x_cdt_fault_length(struct phy_device *phydev, int pair)
18738c84d752SLuo Jie {
18748c84d752SLuo Jie 	int val;
18758c84d752SLuo Jie 	u32 cdt_length_reg = 0;
18768c84d752SLuo Jie 
18778c84d752SLuo Jie 	switch (pair) {
18788c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_A:
18798c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_A;
18808c84d752SLuo Jie 		break;
18818c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_B:
18828c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_B;
18838c84d752SLuo Jie 		break;
18848c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_C:
18858c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_C;
18868c84d752SLuo Jie 		break;
18878c84d752SLuo Jie 	case ETHTOOL_A_CABLE_PAIR_D:
18888c84d752SLuo Jie 		cdt_length_reg = QCA808X_MMD3_CDT_DIAG_PAIR_D;
18898c84d752SLuo Jie 		break;
18908c84d752SLuo Jie 	default:
18918c84d752SLuo Jie 		return -EINVAL;
18928c84d752SLuo Jie 	}
18938c84d752SLuo Jie 
18948c84d752SLuo Jie 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, cdt_length_reg);
18958c84d752SLuo Jie 	if (val < 0)
18968c84d752SLuo Jie 		return val;
18978c84d752SLuo Jie 
18988c84d752SLuo Jie 	return (FIELD_GET(QCA808X_CDT_DIAG_LENGTH, val) * 824) / 10;
18998c84d752SLuo Jie }
19008c84d752SLuo Jie 
19018c84d752SLuo Jie static int qca808x_cable_test_start(struct phy_device *phydev)
19028c84d752SLuo Jie {
19038c84d752SLuo Jie 	int ret;
19048c84d752SLuo Jie 
19058c84d752SLuo Jie 	/* perform CDT with the following configs:
19068c84d752SLuo Jie 	 * 1. disable hibernation.
19078c84d752SLuo Jie 	 * 2. force PHY working in MDI mode.
19088c84d752SLuo Jie 	 * 3. for PHY working in 1000BaseT.
19098c84d752SLuo Jie 	 * 4. configure the threshold.
19108c84d752SLuo Jie 	 */
19118c84d752SLuo Jie 
19128c84d752SLuo Jie 	ret = at803x_debug_reg_mask(phydev, QCA808X_DBG_AN_TEST, QCA808X_HIBERNATION_EN, 0);
19138c84d752SLuo Jie 	if (ret < 0)
19148c84d752SLuo Jie 		return ret;
19158c84d752SLuo Jie 
19168c84d752SLuo Jie 	ret = at803x_config_mdix(phydev, ETH_TP_MDI);
19178c84d752SLuo Jie 	if (ret < 0)
19188c84d752SLuo Jie 		return ret;
19198c84d752SLuo Jie 
19208c84d752SLuo Jie 	/* Force 1000base-T needs to configure PMA/PMD and MII_BMCR */
19218c84d752SLuo Jie 	phydev->duplex = DUPLEX_FULL;
19228c84d752SLuo Jie 	phydev->speed = SPEED_1000;
19238c84d752SLuo Jie 	ret = genphy_c45_pma_setup_forced(phydev);
19248c84d752SLuo Jie 	if (ret < 0)
19258c84d752SLuo Jie 		return ret;
19268c84d752SLuo Jie 
19278c84d752SLuo Jie 	ret = genphy_setup_forced(phydev);
19288c84d752SLuo Jie 	if (ret < 0)
19298c84d752SLuo Jie 		return ret;
19308c84d752SLuo Jie 
19318c84d752SLuo Jie 	/* configure the thresholds for open, short, pair ok test */
19328c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8074, 0xc040);
19338c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8076, 0xc040);
19348c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8077, 0xa060);
19358c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8078, 0xc050);
19368c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807a, 0xc060);
19378c84d752SLuo Jie 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x807e, 0xb060);
19388c84d752SLuo Jie 
19398c84d752SLuo Jie 	return 0;
19408c84d752SLuo Jie }
19418c84d752SLuo Jie 
19428c84d752SLuo Jie static int qca808x_cable_test_get_status(struct phy_device *phydev, bool *finished)
19438c84d752SLuo Jie {
19448c84d752SLuo Jie 	int ret, val;
19458c84d752SLuo Jie 	int pair_a, pair_b, pair_c, pair_d;
19468c84d752SLuo Jie 
19478c84d752SLuo Jie 	*finished = false;
19488c84d752SLuo Jie 
19498c84d752SLuo Jie 	ret = at803x_cdt_start(phydev, 0);
19508c84d752SLuo Jie 	if (ret)
19518c84d752SLuo Jie 		return ret;
19528c84d752SLuo Jie 
19538c84d752SLuo Jie 	ret = at803x_cdt_wait_for_completion(phydev);
19548c84d752SLuo Jie 	if (ret)
19558c84d752SLuo Jie 		return ret;
19568c84d752SLuo Jie 
19578c84d752SLuo Jie 	val = phy_read_mmd(phydev, MDIO_MMD_PCS, QCA808X_MMD3_CDT_STATUS);
19588c84d752SLuo Jie 	if (val < 0)
19598c84d752SLuo Jie 		return val;
19608c84d752SLuo Jie 
19618c84d752SLuo Jie 	pair_a = FIELD_GET(QCA808X_CDT_CODE_PAIR_A, val);
19628c84d752SLuo Jie 	pair_b = FIELD_GET(QCA808X_CDT_CODE_PAIR_B, val);
19638c84d752SLuo Jie 	pair_c = FIELD_GET(QCA808X_CDT_CODE_PAIR_C, val);
19648c84d752SLuo Jie 	pair_d = FIELD_GET(QCA808X_CDT_CODE_PAIR_D, val);
19658c84d752SLuo Jie 
19668c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
19678c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_a));
19688c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_B,
19698c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_b));
19708c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_C,
19718c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_c));
19728c84d752SLuo Jie 	ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_D,
19738c84d752SLuo Jie 				qca808x_cable_test_result_trans(pair_d));
19748c84d752SLuo Jie 
19758c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_a))
19768c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A,
19778c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A));
19788c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_b))
19798c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B,
19808c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B));
19818c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_c))
19828c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C,
19838c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C));
19848c84d752SLuo Jie 	if (qca808x_cdt_fault_length_valid(pair_d))
19858c84d752SLuo Jie 		ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D,
19868c84d752SLuo Jie 				qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D));
19878c84d752SLuo Jie 
19888c84d752SLuo Jie 	*finished = true;
19898c84d752SLuo Jie 
19908c84d752SLuo Jie 	return 0;
19918c84d752SLuo Jie }
19928c84d752SLuo Jie 
19938b8bc13dSLuo Jie static int qca808x_get_features(struct phy_device *phydev)
19948b8bc13dSLuo Jie {
19958b8bc13dSLuo Jie 	int ret;
19968b8bc13dSLuo Jie 
19978b8bc13dSLuo Jie 	ret = genphy_c45_pma_read_abilities(phydev);
19988b8bc13dSLuo Jie 	if (ret)
19998b8bc13dSLuo Jie 		return ret;
20008b8bc13dSLuo Jie 
20018b8bc13dSLuo Jie 	/* The autoneg ability is not existed in bit3 of MMD7.1,
20028b8bc13dSLuo Jie 	 * but it is supported by qca808x PHY, so we add it here
20038b8bc13dSLuo Jie 	 * manually.
20048b8bc13dSLuo Jie 	 */
20058b8bc13dSLuo Jie 	linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported);
20068b8bc13dSLuo Jie 
2007*fea7cfb8SLuo Jie 	/* As for the qca8081 1G version chip, the 2500baseT ability is also
2008*fea7cfb8SLuo Jie 	 * existed in the bit0 of MMD1.21, we need to remove it manually if
2009*fea7cfb8SLuo Jie 	 * it is the qca8081 1G chip according to the bit0 of MMD7.0x901d.
2010*fea7cfb8SLuo Jie 	 */
2011*fea7cfb8SLuo Jie 	ret = phy_read_mmd(phydev, MDIO_MMD_AN, QCA808X_PHY_MMD7_CHIP_TYPE);
2012*fea7cfb8SLuo Jie 	if (ret < 0)
2013*fea7cfb8SLuo Jie 		return ret;
2014*fea7cfb8SLuo Jie 
2015*fea7cfb8SLuo Jie 	if (QCA808X_PHY_CHIP_TYPE_1G & ret)
2016*fea7cfb8SLuo Jie 		linkmode_clear_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, phydev->supported);
2017*fea7cfb8SLuo Jie 
20188b8bc13dSLuo Jie 	return 0;
20198b8bc13dSLuo Jie }
20208b8bc13dSLuo Jie 
2021317420abSMugunthan V N static struct phy_driver at803x_driver[] = {
2022317420abSMugunthan V N {
202396c36712SMichael Walle 	/* Qualcomm Atheros AR8035 */
20240465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
202596c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8035",
20266cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
20272f664823SMichael Walle 	.probe			= at803x_probe,
20287dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
20290ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
2030cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
2031ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
2032ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
20336229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
20346229ed1fSDaniel Mack 	.resume			= at803x_resume,
2035dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
203606d5f344SRussell King 	.read_status		= at803x_read_status,
20370eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
203829773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2039cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
2040cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
20416cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
20426cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
2043317420abSMugunthan V N }, {
204496c36712SMichael Walle 	/* Qualcomm Atheros AR8030 */
2045bd8ca17fSDaniel Mack 	.phy_id			= ATH8030_PHY_ID,
204696c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8030",
20470465d8f8SMichael Walle 	.phy_id_mask		= AT8030_PHY_ID_MASK,
20482f664823SMichael Walle 	.probe			= at803x_probe,
20490ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
205013a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
2051ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
2052ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
20536229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
20546229ed1fSDaniel Mack 	.resume			= at803x_resume,
2055dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
20560eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
205729773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
205805d7cce8SMugunthan V N }, {
205996c36712SMichael Walle 	/* Qualcomm Atheros AR8031/AR8033 */
20600465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
206196c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8031/AR8033",
20626cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
20632f664823SMichael Walle 	.probe			= at803x_probe,
206405d7cce8SMugunthan V N 	.config_init		= at803x_config_init,
206563477a5dSMichael Walle 	.config_aneg		= at803x_config_aneg,
2066cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
206705d7cce8SMugunthan V N 	.set_wol		= at803x_set_wol,
206805d7cce8SMugunthan V N 	.get_wol		= at803x_get_wol,
20696229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
20706229ed1fSDaniel Mack 	.resume			= at803x_resume,
2071c329e5afSDavid Bauer 	.read_page		= at803x_read_page,
2072c329e5afSDavid Bauer 	.write_page		= at803x_write_page,
2073b856150cSDavid Bauer 	.get_features		= at803x_get_features,
207406d5f344SRussell King 	.read_status		= at803x_read_status,
207577a99394SZhao Qiang 	.config_intr		= &at803x_config_intr,
207629773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2077cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
2078cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
20796cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
20806cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
20817908d2ceSOleksij Rempel }, {
20825800091aSDavid Bauer 	/* Qualcomm Atheros AR8032 */
20835800091aSDavid Bauer 	PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
20845800091aSDavid Bauer 	.name			= "Qualcomm Atheros AR8032",
20855800091aSDavid Bauer 	.probe			= at803x_probe,
2086dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
20875800091aSDavid Bauer 	.config_init		= at803x_config_init,
20885800091aSDavid Bauer 	.link_change_notify	= at803x_link_change_notify,
20895800091aSDavid Bauer 	.set_wol		= at803x_set_wol,
20905800091aSDavid Bauer 	.get_wol		= at803x_get_wol,
20915800091aSDavid Bauer 	.suspend		= at803x_suspend,
20925800091aSDavid Bauer 	.resume			= at803x_resume,
20935800091aSDavid Bauer 	/* PHY_BASIC_FEATURES */
20945800091aSDavid Bauer 	.config_intr		= at803x_config_intr,
209529773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2096dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
2097dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
20985800091aSDavid Bauer }, {
20997908d2ceSOleksij Rempel 	/* ATHEROS AR9331 */
21007908d2ceSOleksij Rempel 	PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
210196c36712SMichael Walle 	.name			= "Qualcomm Atheros AR9331 built-in PHY",
21029926de73SOleksij Rempel 	.probe			= at803x_probe,
21037908d2ceSOleksij Rempel 	.suspend		= at803x_suspend,
21047908d2ceSOleksij Rempel 	.resume			= at803x_resume,
2105dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
21067908d2ceSOleksij Rempel 	/* PHY_BASIC_FEATURES */
21077908d2ceSOleksij Rempel 	.config_intr		= &at803x_config_intr,
210829773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
2109dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
2110dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
21117dce80c2SOleksij Rempel 	.read_status		= at803x_read_status,
21127dce80c2SOleksij Rempel 	.soft_reset		= genphy_soft_reset,
21137dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
2114272833b9SAnsuel Smith }, {
2115fada2ce0SDavid Bauer 	/* Qualcomm Atheros QCA9561 */
2116fada2ce0SDavid Bauer 	PHY_ID_MATCH_EXACT(QCA9561_PHY_ID),
2117fada2ce0SDavid Bauer 	.name			= "Qualcomm Atheros QCA9561 built-in PHY",
21189926de73SOleksij Rempel 	.probe			= at803x_probe,
2119fada2ce0SDavid Bauer 	.suspend		= at803x_suspend,
2120fada2ce0SDavid Bauer 	.resume			= at803x_resume,
2121fada2ce0SDavid Bauer 	.flags			= PHY_POLL_CABLE_TEST,
2122fada2ce0SDavid Bauer 	/* PHY_BASIC_FEATURES */
2123fada2ce0SDavid Bauer 	.config_intr		= &at803x_config_intr,
2124fada2ce0SDavid Bauer 	.handle_interrupt	= at803x_handle_interrupt,
2125fada2ce0SDavid Bauer 	.cable_test_start	= at803x_cable_test_start,
2126fada2ce0SDavid Bauer 	.cable_test_get_status	= at803x_cable_test_get_status,
2127fada2ce0SDavid Bauer 	.read_status		= at803x_read_status,
2128fada2ce0SDavid Bauer 	.soft_reset		= genphy_soft_reset,
2129fada2ce0SDavid Bauer 	.config_aneg		= at803x_config_aneg,
2130fada2ce0SDavid Bauer }, {
2131272833b9SAnsuel Smith 	/* QCA8337 */
2132272833b9SAnsuel Smith 	.phy_id			= QCA8337_PHY_ID,
2133272833b9SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
2134d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8337 internal PHY",
2135272833b9SAnsuel Smith 	/* PHY_GBIT_FEATURES */
21361ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
2137272833b9SAnsuel Smith 	.probe			= at803x_probe,
2138272833b9SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
2139272833b9SAnsuel Smith 	.config_init		= qca83xx_config_init,
2140272833b9SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
2141272833b9SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
2142272833b9SAnsuel Smith 	.get_strings		= at803x_get_strings,
2143272833b9SAnsuel Smith 	.get_stats		= at803x_get_stats,
2144ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
2145ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
21460ccf8511SAnsuel Smith }, {
2147b4df02b5SAnsuel Smith 	/* QCA8327-A from switch QCA8327-AL1A */
2148b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_A_PHY_ID,
21490ccf8511SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
2150d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-A internal PHY",
2151b4df02b5SAnsuel Smith 	/* PHY_GBIT_FEATURES */
21521ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
2153b4df02b5SAnsuel Smith 	.probe			= at803x_probe,
2154b4df02b5SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
2155b4df02b5SAnsuel Smith 	.config_init		= qca83xx_config_init,
2156b4df02b5SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
2157b4df02b5SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
2158b4df02b5SAnsuel Smith 	.get_strings		= at803x_get_strings,
2159b4df02b5SAnsuel Smith 	.get_stats		= at803x_get_stats,
2160ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
2161ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
2162b4df02b5SAnsuel Smith }, {
2163b4df02b5SAnsuel Smith 	/* QCA8327-B from switch QCA8327-BL1A */
2164b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_B_PHY_ID,
2165b4df02b5SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
2166d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-B internal PHY",
21670ccf8511SAnsuel Smith 	/* PHY_GBIT_FEATURES */
21681ca83119SAnsuel Smith 	.link_change_notify	= qca83xx_link_change_notify,
21690ccf8511SAnsuel Smith 	.probe			= at803x_probe,
21700ccf8511SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
21710ccf8511SAnsuel Smith 	.config_init		= qca83xx_config_init,
21720ccf8511SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
21730ccf8511SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
21740ccf8511SAnsuel Smith 	.get_strings		= at803x_get_strings,
21750ccf8511SAnsuel Smith 	.get_stats		= at803x_get_stats,
2176ba3c01eeSAnsuel Smith 	.suspend		= qca83xx_suspend,
2177ba3c01eeSAnsuel Smith 	.resume			= qca83xx_resume,
2178daf61732SLuo Jie }, {
2179daf61732SLuo Jie 	/* Qualcomm QCA8081 */
2180daf61732SLuo Jie 	PHY_ID_MATCH_EXACT(QCA8081_PHY_ID),
2181daf61732SLuo Jie 	.name			= "Qualcomm QCA8081",
21828c84d752SLuo Jie 	.flags			= PHY_POLL_CABLE_TEST,
21839926de73SOleksij Rempel 	.probe			= at803x_probe,
2184daf61732SLuo Jie 	.config_intr		= at803x_config_intr,
2185daf61732SLuo Jie 	.handle_interrupt	= at803x_handle_interrupt,
2186daf61732SLuo Jie 	.get_tunable		= at803x_get_tunable,
2187daf61732SLuo Jie 	.set_tunable		= at803x_set_tunable,
2188daf61732SLuo Jie 	.set_wol		= at803x_set_wol,
2189daf61732SLuo Jie 	.get_wol		= at803x_get_wol,
21908b8bc13dSLuo Jie 	.get_features		= qca808x_get_features,
2191f884d449SLuo Jie 	.config_aneg		= at803x_config_aneg,
2192daf61732SLuo Jie 	.suspend		= genphy_suspend,
2193daf61732SLuo Jie 	.resume			= genphy_resume,
219479c7bc05SLuo Jie 	.read_status		= qca808x_read_status,
21952acdd43fSLuo Jie 	.config_init		= qca808x_config_init,
21969d4dae29SLuo Jie 	.soft_reset		= qca808x_soft_reset,
21978c84d752SLuo Jie 	.cable_test_start	= qca808x_cable_test_start,
21988c84d752SLuo Jie 	.cable_test_get_status	= qca808x_cable_test_get_status,
2199272833b9SAnsuel Smith }, };
22000ca7111aSMatus Ujhelyi 
220150fd7150SJohan Hovold module_phy_driver(at803x_driver);
22020ca7111aSMatus Ujhelyi 
22030ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = {
22040465d8f8SMichael Walle 	{ ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
22050465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
22065800091aSDavid Bauer 	{ PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
22070465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
22087908d2ceSOleksij Rempel 	{ PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
22090ccf8511SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8337_PHY_ID) },
2210b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_A_PHY_ID) },
2211b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_B_PHY_ID) },
2212fada2ce0SDavid Bauer 	{ PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) },
2213daf61732SLuo Jie 	{ PHY_ID_MATCH_EXACT(QCA8081_PHY_ID) },
22140ca7111aSMatus Ujhelyi 	{ }
22150ca7111aSMatus Ujhelyi };
22160ca7111aSMatus Ujhelyi 
22170ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl);
2218