xref: /openbmc/linux/drivers/net/phy/at803x.c (revision fada2ce0)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
20ca7111aSMatus Ujhelyi /*
30ca7111aSMatus Ujhelyi  * drivers/net/phy/at803x.c
40ca7111aSMatus Ujhelyi  *
596c36712SMichael Walle  * Driver for Qualcomm Atheros AR803x PHY
60ca7111aSMatus Ujhelyi  *
70ca7111aSMatus Ujhelyi  * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
80ca7111aSMatus Ujhelyi  */
90ca7111aSMatus Ujhelyi 
100ca7111aSMatus Ujhelyi #include <linux/phy.h>
110ca7111aSMatus Ujhelyi #include <linux/module.h>
120ca7111aSMatus Ujhelyi #include <linux/string.h>
130ca7111aSMatus Ujhelyi #include <linux/netdevice.h>
140ca7111aSMatus Ujhelyi #include <linux/etherdevice.h>
156cb75767SMichael Walle #include <linux/ethtool_netlink.h>
1613a56b44SDaniel Mack #include <linux/of_gpio.h>
172f664823SMichael Walle #include <linux/bitfield.h>
1813a56b44SDaniel Mack #include <linux/gpio/consumer.h>
192f664823SMichael Walle #include <linux/regulator/of_regulator.h>
202f664823SMichael Walle #include <linux/regulator/driver.h>
212f664823SMichael Walle #include <linux/regulator/consumer.h>
222f664823SMichael Walle #include <dt-bindings/net/qca-ar803x.h>
230ca7111aSMatus Ujhelyi 
247dce80c2SOleksij Rempel #define AT803X_SPECIFIC_FUNCTION_CONTROL	0x10
257dce80c2SOleksij Rempel #define AT803X_SFC_ASSERT_CRS			BIT(11)
267dce80c2SOleksij Rempel #define AT803X_SFC_FORCE_LINK			BIT(10)
277dce80c2SOleksij Rempel #define AT803X_SFC_MDI_CROSSOVER_MODE_M		GENMASK(6, 5)
287dce80c2SOleksij Rempel #define AT803X_SFC_AUTOMATIC_CROSSOVER		0x3
297dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDIX			0x1
307dce80c2SOleksij Rempel #define AT803X_SFC_MANUAL_MDI			0x0
317dce80c2SOleksij Rempel #define AT803X_SFC_SQE_TEST			BIT(2)
327dce80c2SOleksij Rempel #define AT803X_SFC_POLARITY_REVERSAL		BIT(1)
337dce80c2SOleksij Rempel #define AT803X_SFC_DISABLE_JABBER		BIT(0)
347dce80c2SOleksij Rempel 
3506d5f344SRussell King #define AT803X_SPECIFIC_STATUS			0x11
3606d5f344SRussell King #define AT803X_SS_SPEED_MASK			(3 << 14)
3706d5f344SRussell King #define AT803X_SS_SPEED_1000			(2 << 14)
3806d5f344SRussell King #define AT803X_SS_SPEED_100			(1 << 14)
3906d5f344SRussell King #define AT803X_SS_SPEED_10			(0 << 14)
4006d5f344SRussell King #define AT803X_SS_DUPLEX			BIT(13)
4106d5f344SRussell King #define AT803X_SS_SPEED_DUPLEX_RESOLVED		BIT(11)
4206d5f344SRussell King #define AT803X_SS_MDIX				BIT(6)
4306d5f344SRussell King 
440ca7111aSMatus Ujhelyi #define AT803X_INTR_ENABLE			0x12
45e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_AUTONEG_ERR		BIT(15)
46e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_SPEED_CHANGED	BIT(14)
47e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_DUPLEX_CHANGED	BIT(13)
48e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_PAGE_RECEIVED	BIT(12)
49e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_FAIL		BIT(11)
50e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_SUCCESS		BIT(10)
51e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE	BIT(5)
52e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_POLARITY_CHANGED	BIT(1)
53e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WOL			BIT(0)
54e6e4a556SMartin Blumenstingl 
550ca7111aSMatus Ujhelyi #define AT803X_INTR_STATUS			0x13
56a46bd63bSMartin Blumenstingl 
5713a56b44SDaniel Mack #define AT803X_SMART_SPEED			0x14
58cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_ENABLE		BIT(5)
59cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_RETRY_LIMIT_MASK	GENMASK(4, 2)
60cde0f4f8SMichael Walle #define AT803X_SMART_SPEED_BYPASS_TIMER		BIT(1)
616cb75767SMichael Walle #define AT803X_CDT				0x16
626cb75767SMichael Walle #define AT803X_CDT_MDI_PAIR_MASK		GENMASK(9, 8)
636cb75767SMichael Walle #define AT803X_CDT_ENABLE_TEST			BIT(0)
646cb75767SMichael Walle #define AT803X_CDT_STATUS			0x1c
656cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_NORMAL		0
666cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_SHORT		1
676cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_OPEN		2
686cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_FAIL		3
696cb75767SMichael Walle #define AT803X_CDT_STATUS_STAT_MASK		GENMASK(9, 8)
706cb75767SMichael Walle #define AT803X_CDT_STATUS_DELTA_TIME_MASK	GENMASK(7, 0)
7113a56b44SDaniel Mack #define AT803X_LED_CONTROL			0x18
72a46bd63bSMartin Blumenstingl 
730ca7111aSMatus Ujhelyi #define AT803X_DEVICE_ADDR			0x03
740ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_0_15_OFFSET		0x804C
750ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_16_31_OFFSET	0x804B
760ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_32_47_OFFSET	0x804A
77f62265b5SZefir Kurtisi #define AT803X_REG_CHIP_CONFIG			0x1f
78f62265b5SZefir Kurtisi #define AT803X_BT_BX_REG_SEL			0x8000
79a46bd63bSMartin Blumenstingl 
801ca6d1b1SMugunthan V N #define AT803X_DEBUG_ADDR			0x1D
811ca6d1b1SMugunthan V N #define AT803X_DEBUG_DATA			0x1E
82a46bd63bSMartin Blumenstingl 
83f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_MASK			0x0F
84f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_SGMII			0x01
85f62265b5SZefir Kurtisi 
86f62265b5SZefir Kurtisi #define AT803X_PSSR				0x11	/*PHY-Specific Status Register*/
87f62265b5SZefir Kurtisi #define AT803X_PSSR_MR_AN_COMPLETE		0x0200
88f62265b5SZefir Kurtisi 
892e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_REG_0			0x00
902e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_RX_CLK_DLY_EN		BIT(15)
91a46bd63bSMartin Blumenstingl 
922e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_REG_5			0x05
932e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_TX_CLK_DLY_EN		BIT(8)
940ca7111aSMatus Ujhelyi 
95272833b9SAnsuel Smith #define AT803X_DEBUG_REG_3C			0x3C
96272833b9SAnsuel Smith 
97272833b9SAnsuel Smith #define AT803X_DEBUG_REG_3D			0x3D
98272833b9SAnsuel Smith 
992f664823SMichael Walle #define AT803X_DEBUG_REG_1F			0x1F
1002f664823SMichael Walle #define AT803X_DEBUG_PLL_ON			BIT(2)
1012f664823SMichael Walle #define AT803X_DEBUG_RGMII_1V8			BIT(3)
1022f664823SMichael Walle 
103272833b9SAnsuel Smith #define MDIO_AZ_DEBUG				0x800D
104272833b9SAnsuel Smith 
1052f664823SMichael Walle /* AT803x supports either the XTAL input pad, an internal PLL or the
1062f664823SMichael Walle  * DSP as clock reference for the clock output pad. The XTAL reference
1072f664823SMichael Walle  * is only used for 25 MHz output, all other frequencies need the PLL.
1082f664823SMichael Walle  * The DSP as a clock reference is used in synchronous ethernet
1092f664823SMichael Walle  * applications.
1102f664823SMichael Walle  *
1112f664823SMichael Walle  * By default the PLL is only enabled if there is a link. Otherwise
1122f664823SMichael Walle  * the PHY will go into low power state and disabled the PLL. You can
1132f664823SMichael Walle  * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
1142f664823SMichael Walle  * enabled.
1152f664823SMichael Walle  */
1162f664823SMichael Walle #define AT803X_MMD7_CLK25M			0x8016
1172f664823SMichael Walle #define AT803X_CLK_OUT_MASK			GENMASK(4, 2)
1182f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_XTAL		0
1192f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_DSP		1
1202f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_PLL		2
1212f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_DSP		3
1222f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_PLL		4
1232f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_DSP		5
1242f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_PLL		6
1252f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_DSP		7
1262f664823SMichael Walle 
127428061f7SMichael Walle /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
128428061f7SMichael Walle  * but doesn't support choosing between XTAL/PLL and DSP.
1292f664823SMichael Walle  */
1302f664823SMichael Walle #define AT8035_CLK_OUT_MASK			GENMASK(4, 3)
1312f664823SMichael Walle 
1322f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_MASK		GENMASK(8, 7)
1332f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_FULL		0
1342f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_HALF		1
1352f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_QUARTER		2
1362f664823SMichael Walle 
137cde0f4f8SMichael Walle #define AT803X_DEFAULT_DOWNSHIFT		5
138cde0f4f8SMichael Walle #define AT803X_MIN_DOWNSHIFT			2
139cde0f4f8SMichael Walle #define AT803X_MAX_DOWNSHIFT			9
140cde0f4f8SMichael Walle 
141390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL1		0x805b
142390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL2		0x805c
143390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3		0x805d
144390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN	BIT(8)
145390b4cadSRussell King 
1467908d2ceSOleksij Rempel #define ATH9331_PHY_ID				0x004dd041
147bd8ca17fSDaniel Mack #define ATH8030_PHY_ID				0x004dd076
148bd8ca17fSDaniel Mack #define ATH8031_PHY_ID				0x004dd074
1495800091aSDavid Bauer #define ATH8032_PHY_ID				0x004dd023
150bd8ca17fSDaniel Mack #define ATH8035_PHY_ID				0x004dd072
1510465d8f8SMichael Walle #define AT8030_PHY_ID_MASK			0xffffffef
152bd8ca17fSDaniel Mack 
153b4df02b5SAnsuel Smith #define QCA8327_A_PHY_ID			0x004dd033
154b4df02b5SAnsuel Smith #define QCA8327_B_PHY_ID			0x004dd034
155272833b9SAnsuel Smith #define QCA8337_PHY_ID				0x004dd036
156*fada2ce0SDavid Bauer #define QCA9561_PHY_ID				0x004dd042
157272833b9SAnsuel Smith #define QCA8K_PHY_ID_MASK			0xffffffff
158272833b9SAnsuel Smith 
159272833b9SAnsuel Smith #define QCA8K_DEVFLAGS_REVISION_MASK		GENMASK(2, 0)
160272833b9SAnsuel Smith 
161c329e5afSDavid Bauer #define AT803X_PAGE_FIBER			0
162c329e5afSDavid Bauer #define AT803X_PAGE_COPPER			1
163c329e5afSDavid Bauer 
164d0e13fd5SAnsuel Smith /* don't turn off internal PLL */
165d0e13fd5SAnsuel Smith #define AT803X_KEEP_PLL_ENABLED			BIT(0)
166d0e13fd5SAnsuel Smith #define AT803X_DISABLE_SMARTEEE			BIT(1)
167d0e13fd5SAnsuel Smith 
16896c36712SMichael Walle MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
1690ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi");
1700ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL");
1710ca7111aSMatus Ujhelyi 
172272833b9SAnsuel Smith enum stat_access_type {
173272833b9SAnsuel Smith 	PHY,
174272833b9SAnsuel Smith 	MMD
175272833b9SAnsuel Smith };
176272833b9SAnsuel Smith 
177272833b9SAnsuel Smith struct at803x_hw_stat {
178272833b9SAnsuel Smith 	const char *string;
179272833b9SAnsuel Smith 	u8 reg;
180272833b9SAnsuel Smith 	u32 mask;
181272833b9SAnsuel Smith 	enum stat_access_type access_type;
182272833b9SAnsuel Smith };
183272833b9SAnsuel Smith 
184272833b9SAnsuel Smith static struct at803x_hw_stat at803x_hw_stats[] = {
185272833b9SAnsuel Smith 	{ "phy_idle_errors", 0xa, GENMASK(7, 0), PHY},
186272833b9SAnsuel Smith 	{ "phy_receive_errors", 0x15, GENMASK(15, 0), PHY},
187272833b9SAnsuel Smith 	{ "eee_wake_errors", 0x16, GENMASK(15, 0), MMD},
188272833b9SAnsuel Smith };
189272833b9SAnsuel Smith 
1902f664823SMichael Walle struct at803x_priv {
1912f664823SMichael Walle 	int flags;
1922f664823SMichael Walle 	u16 clk_25m_reg;
1932f664823SMichael Walle 	u16 clk_25m_mask;
194390b4cadSRussell King 	u8 smarteee_lpi_tw_1g;
195390b4cadSRussell King 	u8 smarteee_lpi_tw_100m;
1962f664823SMichael Walle 	struct regulator_dev *vddio_rdev;
1972f664823SMichael Walle 	struct regulator_dev *vddh_rdev;
1982f664823SMichael Walle 	struct regulator *vddio;
199272833b9SAnsuel Smith 	u64 stats[ARRAY_SIZE(at803x_hw_stats)];
2002f664823SMichael Walle };
2012f664823SMichael Walle 
20213a56b44SDaniel Mack struct at803x_context {
20313a56b44SDaniel Mack 	u16 bmcr;
20413a56b44SDaniel Mack 	u16 advertise;
20513a56b44SDaniel Mack 	u16 control1000;
20613a56b44SDaniel Mack 	u16 int_enable;
20713a56b44SDaniel Mack 	u16 smart_speed;
20813a56b44SDaniel Mack 	u16 led_control;
20913a56b44SDaniel Mack };
21013a56b44SDaniel Mack 
211272833b9SAnsuel Smith static int at803x_debug_reg_write(struct phy_device *phydev, u16 reg, u16 data)
212272833b9SAnsuel Smith {
213272833b9SAnsuel Smith 	int ret;
214272833b9SAnsuel Smith 
215272833b9SAnsuel Smith 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
216272833b9SAnsuel Smith 	if (ret < 0)
217272833b9SAnsuel Smith 		return ret;
218272833b9SAnsuel Smith 
219272833b9SAnsuel Smith 	return phy_write(phydev, AT803X_DEBUG_DATA, data);
220272833b9SAnsuel Smith }
221272833b9SAnsuel Smith 
2222e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
2232e5f9f28SMartin Blumenstingl {
2242e5f9f28SMartin Blumenstingl 	int ret;
2252e5f9f28SMartin Blumenstingl 
2262e5f9f28SMartin Blumenstingl 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
2272e5f9f28SMartin Blumenstingl 	if (ret < 0)
2282e5f9f28SMartin Blumenstingl 		return ret;
2292e5f9f28SMartin Blumenstingl 
2302e5f9f28SMartin Blumenstingl 	return phy_read(phydev, AT803X_DEBUG_DATA);
2312e5f9f28SMartin Blumenstingl }
2322e5f9f28SMartin Blumenstingl 
2332e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
2342e5f9f28SMartin Blumenstingl 				 u16 clear, u16 set)
2352e5f9f28SMartin Blumenstingl {
2362e5f9f28SMartin Blumenstingl 	u16 val;
2372e5f9f28SMartin Blumenstingl 	int ret;
2382e5f9f28SMartin Blumenstingl 
2392e5f9f28SMartin Blumenstingl 	ret = at803x_debug_reg_read(phydev, reg);
2402e5f9f28SMartin Blumenstingl 	if (ret < 0)
2412e5f9f28SMartin Blumenstingl 		return ret;
2422e5f9f28SMartin Blumenstingl 
2432e5f9f28SMartin Blumenstingl 	val = ret & 0xffff;
2442e5f9f28SMartin Blumenstingl 	val &= ~clear;
2452e5f9f28SMartin Blumenstingl 	val |= set;
2462e5f9f28SMartin Blumenstingl 
2472e5f9f28SMartin Blumenstingl 	return phy_write(phydev, AT803X_DEBUG_DATA, val);
2482e5f9f28SMartin Blumenstingl }
2492e5f9f28SMartin Blumenstingl 
250c329e5afSDavid Bauer static int at803x_write_page(struct phy_device *phydev, int page)
251c329e5afSDavid Bauer {
252c329e5afSDavid Bauer 	int mask;
253c329e5afSDavid Bauer 	int set;
254c329e5afSDavid Bauer 
255c329e5afSDavid Bauer 	if (page == AT803X_PAGE_COPPER) {
256c329e5afSDavid Bauer 		set = AT803X_BT_BX_REG_SEL;
257c329e5afSDavid Bauer 		mask = 0;
258c329e5afSDavid Bauer 	} else {
259c329e5afSDavid Bauer 		set = 0;
260c329e5afSDavid Bauer 		mask = AT803X_BT_BX_REG_SEL;
261c329e5afSDavid Bauer 	}
262c329e5afSDavid Bauer 
263c329e5afSDavid Bauer 	return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
264c329e5afSDavid Bauer }
265c329e5afSDavid Bauer 
266c329e5afSDavid Bauer static int at803x_read_page(struct phy_device *phydev)
267c329e5afSDavid Bauer {
268c329e5afSDavid Bauer 	int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
269c329e5afSDavid Bauer 
270c329e5afSDavid Bauer 	if (ccr < 0)
271c329e5afSDavid Bauer 		return ccr;
272c329e5afSDavid Bauer 
273c329e5afSDavid Bauer 	if (ccr & AT803X_BT_BX_REG_SEL)
274c329e5afSDavid Bauer 		return AT803X_PAGE_COPPER;
275c329e5afSDavid Bauer 
276c329e5afSDavid Bauer 	return AT803X_PAGE_FIBER;
277c329e5afSDavid Bauer }
278c329e5afSDavid Bauer 
2796d4cd041SVinod Koul static int at803x_enable_rx_delay(struct phy_device *phydev)
2806d4cd041SVinod Koul {
2816d4cd041SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
2826d4cd041SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN);
2836d4cd041SVinod Koul }
2846d4cd041SVinod Koul 
2856d4cd041SVinod Koul static int at803x_enable_tx_delay(struct phy_device *phydev)
2866d4cd041SVinod Koul {
2876d4cd041SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
2886d4cd041SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN);
2896d4cd041SVinod Koul }
2906d4cd041SVinod Koul 
29143f2ebd5SVinod Koul static int at803x_disable_rx_delay(struct phy_device *phydev)
2922e5f9f28SMartin Blumenstingl {
293cd28d1d6SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0,
294cd28d1d6SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN, 0);
2952e5f9f28SMartin Blumenstingl }
2962e5f9f28SMartin Blumenstingl 
29743f2ebd5SVinod Koul static int at803x_disable_tx_delay(struct phy_device *phydev)
2982e5f9f28SMartin Blumenstingl {
299cd28d1d6SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5,
300cd28d1d6SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN, 0);
3012e5f9f28SMartin Blumenstingl }
3022e5f9f28SMartin Blumenstingl 
30313a56b44SDaniel Mack /* save relevant PHY registers to private copy */
30413a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev,
30513a56b44SDaniel Mack 				struct at803x_context *context)
30613a56b44SDaniel Mack {
30713a56b44SDaniel Mack 	context->bmcr = phy_read(phydev, MII_BMCR);
30813a56b44SDaniel Mack 	context->advertise = phy_read(phydev, MII_ADVERTISE);
30913a56b44SDaniel Mack 	context->control1000 = phy_read(phydev, MII_CTRL1000);
31013a56b44SDaniel Mack 	context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
31113a56b44SDaniel Mack 	context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
31213a56b44SDaniel Mack 	context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
31313a56b44SDaniel Mack }
31413a56b44SDaniel Mack 
31513a56b44SDaniel Mack /* restore relevant PHY registers from private copy */
31613a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev,
31713a56b44SDaniel Mack 				   const struct at803x_context *context)
31813a56b44SDaniel Mack {
31913a56b44SDaniel Mack 	phy_write(phydev, MII_BMCR, context->bmcr);
32013a56b44SDaniel Mack 	phy_write(phydev, MII_ADVERTISE, context->advertise);
32113a56b44SDaniel Mack 	phy_write(phydev, MII_CTRL1000, context->control1000);
32213a56b44SDaniel Mack 	phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
32313a56b44SDaniel Mack 	phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
32413a56b44SDaniel Mack 	phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
32513a56b44SDaniel Mack }
32613a56b44SDaniel Mack 
327ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev,
328ea13c9eeSMugunthan V N 			  struct ethtool_wolinfo *wol)
3290ca7111aSMatus Ujhelyi {
3300ca7111aSMatus Ujhelyi 	struct net_device *ndev = phydev->attached_dev;
3310ca7111aSMatus Ujhelyi 	const u8 *mac;
332ea13c9eeSMugunthan V N 	int ret;
333ea13c9eeSMugunthan V N 	u32 value;
3340ca7111aSMatus Ujhelyi 	unsigned int i, offsets[] = {
3350ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_32_47_OFFSET,
3360ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_16_31_OFFSET,
3370ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_0_15_OFFSET,
3380ca7111aSMatus Ujhelyi 	};
3390ca7111aSMatus Ujhelyi 
3400ca7111aSMatus Ujhelyi 	if (!ndev)
341ea13c9eeSMugunthan V N 		return -ENODEV;
3420ca7111aSMatus Ujhelyi 
343ea13c9eeSMugunthan V N 	if (wol->wolopts & WAKE_MAGIC) {
3440ca7111aSMatus Ujhelyi 		mac = (const u8 *) ndev->dev_addr;
3450ca7111aSMatus Ujhelyi 
3460ca7111aSMatus Ujhelyi 		if (!is_valid_ether_addr(mac))
347fc755687SDan Murphy 			return -EINVAL;
3480ca7111aSMatus Ujhelyi 
3490e021396SCarlo Caione 		for (i = 0; i < 3; i++)
3500e021396SCarlo Caione 			phy_write_mmd(phydev, AT803X_DEVICE_ADDR, offsets[i],
3510ca7111aSMatus Ujhelyi 				      mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
352ea13c9eeSMugunthan V N 
353ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
354e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_WOL;
355ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
356ea13c9eeSMugunthan V N 		if (ret)
357ea13c9eeSMugunthan V N 			return ret;
358ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
359ea13c9eeSMugunthan V N 	} else {
360ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
361e6e4a556SMartin Blumenstingl 		value &= (~AT803X_INTR_ENABLE_WOL);
362ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
363ea13c9eeSMugunthan V N 		if (ret)
364ea13c9eeSMugunthan V N 			return ret;
365ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
366ea13c9eeSMugunthan V N 	}
367ea13c9eeSMugunthan V N 
368ea13c9eeSMugunthan V N 	return ret;
369ea13c9eeSMugunthan V N }
370ea13c9eeSMugunthan V N 
371ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev,
372ea13c9eeSMugunthan V N 			   struct ethtool_wolinfo *wol)
373ea13c9eeSMugunthan V N {
374ea13c9eeSMugunthan V N 	u32 value;
375ea13c9eeSMugunthan V N 
376ea13c9eeSMugunthan V N 	wol->supported = WAKE_MAGIC;
377ea13c9eeSMugunthan V N 	wol->wolopts = 0;
378ea13c9eeSMugunthan V N 
379ea13c9eeSMugunthan V N 	value = phy_read(phydev, AT803X_INTR_ENABLE);
380e6e4a556SMartin Blumenstingl 	if (value & AT803X_INTR_ENABLE_WOL)
381ea13c9eeSMugunthan V N 		wol->wolopts |= WAKE_MAGIC;
3820ca7111aSMatus Ujhelyi }
3830ca7111aSMatus Ujhelyi 
384272833b9SAnsuel Smith static int at803x_get_sset_count(struct phy_device *phydev)
385272833b9SAnsuel Smith {
386272833b9SAnsuel Smith 	return ARRAY_SIZE(at803x_hw_stats);
387272833b9SAnsuel Smith }
388272833b9SAnsuel Smith 
389272833b9SAnsuel Smith static void at803x_get_strings(struct phy_device *phydev, u8 *data)
390272833b9SAnsuel Smith {
391272833b9SAnsuel Smith 	int i;
392272833b9SAnsuel Smith 
393272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++) {
394272833b9SAnsuel Smith 		strscpy(data + i * ETH_GSTRING_LEN,
395272833b9SAnsuel Smith 			at803x_hw_stats[i].string, ETH_GSTRING_LEN);
396272833b9SAnsuel Smith 	}
397272833b9SAnsuel Smith }
398272833b9SAnsuel Smith 
399272833b9SAnsuel Smith static u64 at803x_get_stat(struct phy_device *phydev, int i)
400272833b9SAnsuel Smith {
401272833b9SAnsuel Smith 	struct at803x_hw_stat stat = at803x_hw_stats[i];
402272833b9SAnsuel Smith 	struct at803x_priv *priv = phydev->priv;
403272833b9SAnsuel Smith 	int val;
404272833b9SAnsuel Smith 	u64 ret;
405272833b9SAnsuel Smith 
406272833b9SAnsuel Smith 	if (stat.access_type == MMD)
407272833b9SAnsuel Smith 		val = phy_read_mmd(phydev, MDIO_MMD_PCS, stat.reg);
408272833b9SAnsuel Smith 	else
409272833b9SAnsuel Smith 		val = phy_read(phydev, stat.reg);
410272833b9SAnsuel Smith 
411272833b9SAnsuel Smith 	if (val < 0) {
412272833b9SAnsuel Smith 		ret = U64_MAX;
413272833b9SAnsuel Smith 	} else {
414272833b9SAnsuel Smith 		val = val & stat.mask;
415272833b9SAnsuel Smith 		priv->stats[i] += val;
416272833b9SAnsuel Smith 		ret = priv->stats[i];
417272833b9SAnsuel Smith 	}
418272833b9SAnsuel Smith 
419272833b9SAnsuel Smith 	return ret;
420272833b9SAnsuel Smith }
421272833b9SAnsuel Smith 
422272833b9SAnsuel Smith static void at803x_get_stats(struct phy_device *phydev,
423272833b9SAnsuel Smith 			     struct ethtool_stats *stats, u64 *data)
424272833b9SAnsuel Smith {
425272833b9SAnsuel Smith 	int i;
426272833b9SAnsuel Smith 
427272833b9SAnsuel Smith 	for (i = 0; i < ARRAY_SIZE(at803x_hw_stats); i++)
428272833b9SAnsuel Smith 		data[i] = at803x_get_stat(phydev, i);
429272833b9SAnsuel Smith }
430272833b9SAnsuel Smith 
4316229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev)
4326229ed1fSDaniel Mack {
4336229ed1fSDaniel Mack 	int value;
4346229ed1fSDaniel Mack 	int wol_enabled;
4356229ed1fSDaniel Mack 
4366229ed1fSDaniel Mack 	value = phy_read(phydev, AT803X_INTR_ENABLE);
437e6e4a556SMartin Blumenstingl 	wol_enabled = value & AT803X_INTR_ENABLE_WOL;
4386229ed1fSDaniel Mack 
4396229ed1fSDaniel Mack 	if (wol_enabled)
440fea23fb5SRussell King 		value = BMCR_ISOLATE;
4416229ed1fSDaniel Mack 	else
442fea23fb5SRussell King 		value = BMCR_PDOWN;
4436229ed1fSDaniel Mack 
444fea23fb5SRussell King 	phy_modify(phydev, MII_BMCR, 0, value);
4456229ed1fSDaniel Mack 
4466229ed1fSDaniel Mack 	return 0;
4476229ed1fSDaniel Mack }
4486229ed1fSDaniel Mack 
4496229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev)
4506229ed1fSDaniel Mack {
451f102852fSRussell King 	return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
4526229ed1fSDaniel Mack }
4536229ed1fSDaniel Mack 
4542f664823SMichael Walle static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
4552f664823SMichael Walle 					    unsigned int selector)
4562f664823SMichael Walle {
4572f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
4582f664823SMichael Walle 
4592f664823SMichael Walle 	if (selector)
4602f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
4612f664823SMichael Walle 					     0, AT803X_DEBUG_RGMII_1V8);
4622f664823SMichael Walle 	else
4632f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
4642f664823SMichael Walle 					     AT803X_DEBUG_RGMII_1V8, 0);
4652f664823SMichael Walle }
4662f664823SMichael Walle 
4672f664823SMichael Walle static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
4682f664823SMichael Walle {
4692f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
4702f664823SMichael Walle 	int val;
4712f664823SMichael Walle 
4722f664823SMichael Walle 	val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
4732f664823SMichael Walle 	if (val < 0)
4742f664823SMichael Walle 		return val;
4752f664823SMichael Walle 
4762f664823SMichael Walle 	return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
4772f664823SMichael Walle }
4782f664823SMichael Walle 
4793faaf539SRikard Falkeborn static const struct regulator_ops vddio_regulator_ops = {
4802f664823SMichael Walle 	.list_voltage = regulator_list_voltage_table,
4812f664823SMichael Walle 	.set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
4822f664823SMichael Walle 	.get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
4832f664823SMichael Walle };
4842f664823SMichael Walle 
4852f664823SMichael Walle static const unsigned int vddio_voltage_table[] = {
4862f664823SMichael Walle 	1500000,
4872f664823SMichael Walle 	1800000,
4882f664823SMichael Walle };
4892f664823SMichael Walle 
4902f664823SMichael Walle static const struct regulator_desc vddio_desc = {
4912f664823SMichael Walle 	.name = "vddio",
4922f664823SMichael Walle 	.of_match = of_match_ptr("vddio-regulator"),
4932f664823SMichael Walle 	.n_voltages = ARRAY_SIZE(vddio_voltage_table),
4942f664823SMichael Walle 	.volt_table = vddio_voltage_table,
4952f664823SMichael Walle 	.ops = &vddio_regulator_ops,
4962f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
4972f664823SMichael Walle 	.owner = THIS_MODULE,
4982f664823SMichael Walle };
4992f664823SMichael Walle 
5003faaf539SRikard Falkeborn static const struct regulator_ops vddh_regulator_ops = {
5012f664823SMichael Walle };
5022f664823SMichael Walle 
5032f664823SMichael Walle static const struct regulator_desc vddh_desc = {
5042f664823SMichael Walle 	.name = "vddh",
5052f664823SMichael Walle 	.of_match = of_match_ptr("vddh-regulator"),
5062f664823SMichael Walle 	.n_voltages = 1,
5072f664823SMichael Walle 	.fixed_uV = 2500000,
5082f664823SMichael Walle 	.ops = &vddh_regulator_ops,
5092f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
5102f664823SMichael Walle 	.owner = THIS_MODULE,
5112f664823SMichael Walle };
5122f664823SMichael Walle 
5132f664823SMichael Walle static int at8031_register_regulators(struct phy_device *phydev)
5142f664823SMichael Walle {
5152f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
5162f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
5172f664823SMichael Walle 	struct regulator_config config = { };
5182f664823SMichael Walle 
5192f664823SMichael Walle 	config.dev = dev;
5202f664823SMichael Walle 	config.driver_data = phydev;
5212f664823SMichael Walle 
5222f664823SMichael Walle 	priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
5232f664823SMichael Walle 	if (IS_ERR(priv->vddio_rdev)) {
5242f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDIO regulator\n");
5252f664823SMichael Walle 		return PTR_ERR(priv->vddio_rdev);
5262f664823SMichael Walle 	}
5272f664823SMichael Walle 
5282f664823SMichael Walle 	priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
5292f664823SMichael Walle 	if (IS_ERR(priv->vddh_rdev)) {
5302f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDH regulator\n");
5312f664823SMichael Walle 		return PTR_ERR(priv->vddh_rdev);
5322f664823SMichael Walle 	}
5332f664823SMichael Walle 
5342f664823SMichael Walle 	return 0;
5352f664823SMichael Walle }
5362f664823SMichael Walle 
5372f664823SMichael Walle static int at803x_parse_dt(struct phy_device *phydev)
5382f664823SMichael Walle {
5392f664823SMichael Walle 	struct device_node *node = phydev->mdio.dev.of_node;
5402f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
541390b4cadSRussell King 	u32 freq, strength, tw;
5423f2edd30SAndrew Lunn 	unsigned int sel;
5432f664823SMichael Walle 	int ret;
5442f664823SMichael Walle 
5452f664823SMichael Walle 	if (!IS_ENABLED(CONFIG_OF_MDIO))
5462f664823SMichael Walle 		return 0;
5472f664823SMichael Walle 
548390b4cadSRussell King 	if (of_property_read_bool(node, "qca,disable-smarteee"))
549390b4cadSRussell King 		priv->flags |= AT803X_DISABLE_SMARTEEE;
550390b4cadSRussell King 
551390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
552390b4cadSRussell King 		if (!tw || tw > 255) {
553390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
554390b4cadSRussell King 			return -EINVAL;
555390b4cadSRussell King 		}
556390b4cadSRussell King 		priv->smarteee_lpi_tw_1g = tw;
557390b4cadSRussell King 	}
558390b4cadSRussell King 
559390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
560390b4cadSRussell King 		if (!tw || tw > 255) {
561390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
562390b4cadSRussell King 			return -EINVAL;
563390b4cadSRussell King 		}
564390b4cadSRussell King 		priv->smarteee_lpi_tw_100m = tw;
565390b4cadSRussell King 	}
566390b4cadSRussell King 
5672f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
5682f664823SMichael Walle 	if (!ret) {
5692f664823SMichael Walle 		switch (freq) {
5702f664823SMichael Walle 		case 25000000:
5712f664823SMichael Walle 			sel = AT803X_CLK_OUT_25MHZ_XTAL;
5722f664823SMichael Walle 			break;
5732f664823SMichael Walle 		case 50000000:
5742f664823SMichael Walle 			sel = AT803X_CLK_OUT_50MHZ_PLL;
5752f664823SMichael Walle 			break;
5762f664823SMichael Walle 		case 62500000:
5772f664823SMichael Walle 			sel = AT803X_CLK_OUT_62_5MHZ_PLL;
5782f664823SMichael Walle 			break;
5792f664823SMichael Walle 		case 125000000:
5802f664823SMichael Walle 			sel = AT803X_CLK_OUT_125MHZ_PLL;
5812f664823SMichael Walle 			break;
5822f664823SMichael Walle 		default:
5832f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-frequency\n");
5842f664823SMichael Walle 			return -EINVAL;
5852f664823SMichael Walle 		}
5862f664823SMichael Walle 
5873f2edd30SAndrew Lunn 		priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
5883f2edd30SAndrew Lunn 		priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
5892f664823SMichael Walle 
5902f664823SMichael Walle 		/* Fixup for the AR8030/AR8035. This chip has another mask and
5912f664823SMichael Walle 		 * doesn't support the DSP reference. Eg. the lowest bit of the
5922f664823SMichael Walle 		 * mask. The upper two bits select the same frequencies. Mask
5932f664823SMichael Walle 		 * the lowest bit here.
5942f664823SMichael Walle 		 *
5952f664823SMichael Walle 		 * Warning:
5962f664823SMichael Walle 		 *   There was no datasheet for the AR8030 available so this is
5972f664823SMichael Walle 		 *   just a guess. But the AR8035 is listed as pin compatible
5982f664823SMichael Walle 		 *   to the AR8030 so there might be a good chance it works on
5992f664823SMichael Walle 		 *   the AR8030 too.
6002f664823SMichael Walle 		 */
6018887ca54SRussell King 		if (phydev->drv->phy_id == ATH8030_PHY_ID ||
6028887ca54SRussell King 		    phydev->drv->phy_id == ATH8035_PHY_ID) {
603b1f4c209SOleksij Rempel 			priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
604b1f4c209SOleksij Rempel 			priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
6052f664823SMichael Walle 		}
6062f664823SMichael Walle 	}
6072f664823SMichael Walle 
6082f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
6092f664823SMichael Walle 	if (!ret) {
6102f664823SMichael Walle 		priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
6112f664823SMichael Walle 		switch (strength) {
6122f664823SMichael Walle 		case AR803X_STRENGTH_FULL:
6132f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
6142f664823SMichael Walle 			break;
6152f664823SMichael Walle 		case AR803X_STRENGTH_HALF:
6162f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
6172f664823SMichael Walle 			break;
6182f664823SMichael Walle 		case AR803X_STRENGTH_QUARTER:
6192f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
6202f664823SMichael Walle 			break;
6212f664823SMichael Walle 		default:
6222f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-strength\n");
6232f664823SMichael Walle 			return -EINVAL;
6242f664823SMichael Walle 		}
6252f664823SMichael Walle 	}
6262f664823SMichael Walle 
627428061f7SMichael Walle 	/* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
628428061f7SMichael Walle 	 * options.
629428061f7SMichael Walle 	 */
6308887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
6312f664823SMichael Walle 		if (of_property_read_bool(node, "qca,keep-pll-enabled"))
6322f664823SMichael Walle 			priv->flags |= AT803X_KEEP_PLL_ENABLED;
6332f664823SMichael Walle 
6342f664823SMichael Walle 		ret = at8031_register_regulators(phydev);
6352f664823SMichael Walle 		if (ret < 0)
6362f664823SMichael Walle 			return ret;
6372f664823SMichael Walle 
6382f664823SMichael Walle 		priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev,
6392f664823SMichael Walle 							  "vddio");
6402f664823SMichael Walle 		if (IS_ERR(priv->vddio)) {
6412f664823SMichael Walle 			phydev_err(phydev, "failed to get VDDIO regulator\n");
6422f664823SMichael Walle 			return PTR_ERR(priv->vddio);
6432f664823SMichael Walle 		}
6442f664823SMichael Walle 	}
6452f664823SMichael Walle 
6462f664823SMichael Walle 	return 0;
6472f664823SMichael Walle }
6482f664823SMichael Walle 
6492f664823SMichael Walle static int at803x_probe(struct phy_device *phydev)
6502f664823SMichael Walle {
6512f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
6522f664823SMichael Walle 	struct at803x_priv *priv;
653c329e5afSDavid Bauer 	int ret;
6542f664823SMichael Walle 
6552f664823SMichael Walle 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
6562f664823SMichael Walle 	if (!priv)
6572f664823SMichael Walle 		return -ENOMEM;
6582f664823SMichael Walle 
6592f664823SMichael Walle 	phydev->priv = priv;
6602f664823SMichael Walle 
661c329e5afSDavid Bauer 	ret = at803x_parse_dt(phydev);
662c329e5afSDavid Bauer 	if (ret)
663c329e5afSDavid Bauer 		return ret;
664c329e5afSDavid Bauer 
6658f7e8762SMichael Walle 	if (priv->vddio) {
6668f7e8762SMichael Walle 		ret = regulator_enable(priv->vddio);
6678f7e8762SMichael Walle 		if (ret < 0)
6688f7e8762SMichael Walle 			return ret;
6698f7e8762SMichael Walle 	}
6708f7e8762SMichael Walle 
671c329e5afSDavid Bauer 	/* Some bootloaders leave the fiber page selected.
672c329e5afSDavid Bauer 	 * Switch to the copper page, as otherwise we read
673c329e5afSDavid Bauer 	 * the PHY capabilities from the fiber side.
674c329e5afSDavid Bauer 	 */
6758887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
6768f7e8762SMichael Walle 		phy_lock_mdio_bus(phydev);
6778f7e8762SMichael Walle 		ret = at803x_write_page(phydev, AT803X_PAGE_COPPER);
6788f7e8762SMichael Walle 		phy_unlock_mdio_bus(phydev);
6798f7e8762SMichael Walle 		if (ret)
6808f7e8762SMichael Walle 			goto err;
681c329e5afSDavid Bauer 	}
682c329e5afSDavid Bauer 
6838f7e8762SMichael Walle 	return 0;
6848f7e8762SMichael Walle 
6858f7e8762SMichael Walle err:
6868f7e8762SMichael Walle 	if (priv->vddio)
6878f7e8762SMichael Walle 		regulator_disable(priv->vddio);
6888f7e8762SMichael Walle 
689c329e5afSDavid Bauer 	return ret;
6902f664823SMichael Walle }
6912f664823SMichael Walle 
6922318ca8aSMichael Walle static void at803x_remove(struct phy_device *phydev)
6932318ca8aSMichael Walle {
6942318ca8aSMichael Walle 	struct at803x_priv *priv = phydev->priv;
6952318ca8aSMichael Walle 
6962318ca8aSMichael Walle 	if (priv->vddio)
6972318ca8aSMichael Walle 		regulator_disable(priv->vddio);
6982318ca8aSMichael Walle }
6992318ca8aSMichael Walle 
700b856150cSDavid Bauer static int at803x_get_features(struct phy_device *phydev)
701b856150cSDavid Bauer {
702b856150cSDavid Bauer 	int err;
703b856150cSDavid Bauer 
704b856150cSDavid Bauer 	err = genphy_read_abilities(phydev);
705b856150cSDavid Bauer 	if (err)
706b856150cSDavid Bauer 		return err;
707b856150cSDavid Bauer 
708f5621a01SVladimir Oltean 	if (phydev->drv->phy_id != ATH8031_PHY_ID)
709b856150cSDavid Bauer 		return 0;
710b856150cSDavid Bauer 
711b856150cSDavid Bauer 	/* AR8031/AR8033 have different status registers
712b856150cSDavid Bauer 	 * for copper and fiber operation. However, the
713b856150cSDavid Bauer 	 * extended status register is the same for both
714b856150cSDavid Bauer 	 * operation modes.
715b856150cSDavid Bauer 	 *
716b856150cSDavid Bauer 	 * As a result of that, ESTATUS_1000_XFULL is set
717b856150cSDavid Bauer 	 * to 1 even when operating in copper TP mode.
718b856150cSDavid Bauer 	 *
719b856150cSDavid Bauer 	 * Remove this mode from the supported link modes,
720b856150cSDavid Bauer 	 * as this driver currently only supports copper
721b856150cSDavid Bauer 	 * operation.
722b856150cSDavid Bauer 	 */
723b856150cSDavid Bauer 	linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
724b856150cSDavid Bauer 			   phydev->supported);
725b856150cSDavid Bauer 	return 0;
726b856150cSDavid Bauer }
727b856150cSDavid Bauer 
728390b4cadSRussell King static int at803x_smarteee_config(struct phy_device *phydev)
729390b4cadSRussell King {
730390b4cadSRussell King 	struct at803x_priv *priv = phydev->priv;
731390b4cadSRussell King 	u16 mask = 0, val = 0;
732390b4cadSRussell King 	int ret;
733390b4cadSRussell King 
734390b4cadSRussell King 	if (priv->flags & AT803X_DISABLE_SMARTEEE)
735390b4cadSRussell King 		return phy_modify_mmd(phydev, MDIO_MMD_PCS,
736390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3,
737390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
738390b4cadSRussell King 
739390b4cadSRussell King 	if (priv->smarteee_lpi_tw_1g) {
740390b4cadSRussell King 		mask |= 0xff00;
741390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_1g << 8;
742390b4cadSRussell King 	}
743390b4cadSRussell King 	if (priv->smarteee_lpi_tw_100m) {
744390b4cadSRussell King 		mask |= 0x00ff;
745390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_100m;
746390b4cadSRussell King 	}
747390b4cadSRussell King 	if (!mask)
748390b4cadSRussell King 		return 0;
749390b4cadSRussell King 
750390b4cadSRussell King 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
751390b4cadSRussell King 			     mask, val);
752390b4cadSRussell King 	if (ret)
753390b4cadSRussell King 		return ret;
754390b4cadSRussell King 
755390b4cadSRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
756390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
757390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
758390b4cadSRussell King }
759390b4cadSRussell King 
7602f664823SMichael Walle static int at803x_clk_out_config(struct phy_device *phydev)
7612f664823SMichael Walle {
7622f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
7632f664823SMichael Walle 
7642f664823SMichael Walle 	if (!priv->clk_25m_mask)
7652f664823SMichael Walle 		return 0;
7662f664823SMichael Walle 
767a45c1c10SRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
768a45c1c10SRussell King 			      priv->clk_25m_mask, priv->clk_25m_reg);
7692f664823SMichael Walle }
7702f664823SMichael Walle 
7712f664823SMichael Walle static int at8031_pll_config(struct phy_device *phydev)
7722f664823SMichael Walle {
7732f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
7742f664823SMichael Walle 
7752f664823SMichael Walle 	/* The default after hardware reset is PLL OFF. After a soft reset, the
7762f664823SMichael Walle 	 * values are retained.
7772f664823SMichael Walle 	 */
7782f664823SMichael Walle 	if (priv->flags & AT803X_KEEP_PLL_ENABLED)
7792f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
7802f664823SMichael Walle 					     0, AT803X_DEBUG_PLL_ON);
7812f664823SMichael Walle 	else
7822f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
7832f664823SMichael Walle 					     AT803X_DEBUG_PLL_ON, 0);
7842f664823SMichael Walle }
7852f664823SMichael Walle 
7860ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev)
7870ca7111aSMatus Ujhelyi {
7881ca6d1b1SMugunthan V N 	int ret;
7890ca7111aSMatus Ujhelyi 
7906d4cd041SVinod Koul 	/* The RX and TX delay default is:
7916d4cd041SVinod Koul 	 *   after HW reset: RX delay enabled and TX delay disabled
7926d4cd041SVinod Koul 	 *   after SW reset: RX delay enabled, while TX delay retains the
7936d4cd041SVinod Koul 	 *   value before reset.
7946d4cd041SVinod Koul 	 */
795bb0ce4c1SAndré Draszik 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
796bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
797bb0ce4c1SAndré Draszik 		ret = at803x_enable_rx_delay(phydev);
798bb0ce4c1SAndré Draszik 	else
799cd28d1d6SVinod Koul 		ret = at803x_disable_rx_delay(phydev);
8002e5f9f28SMartin Blumenstingl 	if (ret < 0)
8011ca6d1b1SMugunthan V N 		return ret;
8026d4cd041SVinod Koul 
8036d4cd041SVinod Koul 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
804bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
8056d4cd041SVinod Koul 		ret = at803x_enable_tx_delay(phydev);
806bb0ce4c1SAndré Draszik 	else
807bb0ce4c1SAndré Draszik 		ret = at803x_disable_tx_delay(phydev);
8082f664823SMichael Walle 	if (ret < 0)
8096d4cd041SVinod Koul 		return ret;
8102f664823SMichael Walle 
811390b4cadSRussell King 	ret = at803x_smarteee_config(phydev);
812390b4cadSRussell King 	if (ret < 0)
813390b4cadSRussell King 		return ret;
814390b4cadSRussell King 
8152f664823SMichael Walle 	ret = at803x_clk_out_config(phydev);
8162f664823SMichael Walle 	if (ret < 0)
8172f664823SMichael Walle 		return ret;
8182f664823SMichael Walle 
8198887ca54SRussell King 	if (phydev->drv->phy_id == ATH8031_PHY_ID) {
8202f664823SMichael Walle 		ret = at8031_pll_config(phydev);
8212f664823SMichael Walle 		if (ret < 0)
8222f664823SMichael Walle 			return ret;
8232f664823SMichael Walle 	}
8242f664823SMichael Walle 
8253c51fa5dSRussell King 	/* Ar803x extended next page bit is enabled by default. Cisco
8263c51fa5dSRussell King 	 * multigig switches read this bit and attempt to negotiate 10Gbps
8273c51fa5dSRussell King 	 * rates even if the next page bit is disabled. This is incorrect
8283c51fa5dSRussell King 	 * behaviour but we still need to accommodate it. XNP is only needed
8293c51fa5dSRussell King 	 * for 10Gbps support, so disable XNP.
8303c51fa5dSRussell King 	 */
8313c51fa5dSRussell King 	return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
8320ca7111aSMatus Ujhelyi }
8330ca7111aSMatus Ujhelyi 
83477a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev)
83577a99394SZhao Qiang {
83677a99394SZhao Qiang 	int err;
83777a99394SZhao Qiang 
838a46bd63bSMartin Blumenstingl 	err = phy_read(phydev, AT803X_INTR_STATUS);
83977a99394SZhao Qiang 
84077a99394SZhao Qiang 	return (err < 0) ? err : 0;
84177a99394SZhao Qiang }
84277a99394SZhao Qiang 
84377a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev)
84477a99394SZhao Qiang {
84577a99394SZhao Qiang 	int err;
84677a99394SZhao Qiang 	int value;
84777a99394SZhao Qiang 
848a46bd63bSMartin Blumenstingl 	value = phy_read(phydev, AT803X_INTR_ENABLE);
84977a99394SZhao Qiang 
850e6e4a556SMartin Blumenstingl 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
851a3417885SIoana Ciornei 		/* Clear any pending interrupts */
852a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
853a3417885SIoana Ciornei 		if (err)
854a3417885SIoana Ciornei 			return err;
855a3417885SIoana Ciornei 
856e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
857e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
858e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
859e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_FAIL;
860e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
861e6e4a556SMartin Blumenstingl 
862e6e4a556SMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, value);
863a3417885SIoana Ciornei 	} else {
864a46bd63bSMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
865a3417885SIoana Ciornei 		if (err)
866a3417885SIoana Ciornei 			return err;
867a3417885SIoana Ciornei 
868a3417885SIoana Ciornei 		/* Clear any pending interrupts */
869a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
870a3417885SIoana Ciornei 	}
87177a99394SZhao Qiang 
87277a99394SZhao Qiang 	return err;
87377a99394SZhao Qiang }
87477a99394SZhao Qiang 
87529773097SIoana Ciornei static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev)
87629773097SIoana Ciornei {
87729773097SIoana Ciornei 	int irq_status, int_enabled;
87829773097SIoana Ciornei 
87929773097SIoana Ciornei 	irq_status = phy_read(phydev, AT803X_INTR_STATUS);
88029773097SIoana Ciornei 	if (irq_status < 0) {
88129773097SIoana Ciornei 		phy_error(phydev);
88229773097SIoana Ciornei 		return IRQ_NONE;
88329773097SIoana Ciornei 	}
88429773097SIoana Ciornei 
88529773097SIoana Ciornei 	/* Read the current enabled interrupts */
88629773097SIoana Ciornei 	int_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
88729773097SIoana Ciornei 	if (int_enabled < 0) {
88829773097SIoana Ciornei 		phy_error(phydev);
88929773097SIoana Ciornei 		return IRQ_NONE;
89029773097SIoana Ciornei 	}
89129773097SIoana Ciornei 
89229773097SIoana Ciornei 	/* See if this was one of our enabled interrupts */
89329773097SIoana Ciornei 	if (!(irq_status & int_enabled))
89429773097SIoana Ciornei 		return IRQ_NONE;
89529773097SIoana Ciornei 
89629773097SIoana Ciornei 	phy_trigger_machine(phydev);
89729773097SIoana Ciornei 
89829773097SIoana Ciornei 	return IRQ_HANDLED;
89929773097SIoana Ciornei }
90029773097SIoana Ciornei 
90113a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev)
90213a56b44SDaniel Mack {
90313a56b44SDaniel Mack 	/*
90413a56b44SDaniel Mack 	 * Conduct a hardware reset for AT8030 every time a link loss is
90513a56b44SDaniel Mack 	 * signalled. This is necessary to circumvent a hardware bug that
90613a56b44SDaniel Mack 	 * occurs when the cable is unplugged while TX packets are pending
90713a56b44SDaniel Mack 	 * in the FIFO. In such cases, the FIFO enters an error mode it
90813a56b44SDaniel Mack 	 * cannot recover from by software.
90913a56b44SDaniel Mack 	 */
9106110ed2dSDavid Bauer 	if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
91113a56b44SDaniel Mack 		struct at803x_context context;
91213a56b44SDaniel Mack 
91313a56b44SDaniel Mack 		at803x_context_save(phydev, &context);
91413a56b44SDaniel Mack 
915bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 1);
91613a56b44SDaniel Mack 		msleep(1);
917bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 0);
918d57019d1SSergei Shtylyov 		msleep(1);
91913a56b44SDaniel Mack 
92013a56b44SDaniel Mack 		at803x_context_restore(phydev, &context);
92113a56b44SDaniel Mack 
9225c5f626bSHeiner Kallweit 		phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
92313a56b44SDaniel Mack 	}
92413a56b44SDaniel Mack }
92513a56b44SDaniel Mack 
92606d5f344SRussell King static int at803x_read_status(struct phy_device *phydev)
92706d5f344SRussell King {
92806d5f344SRussell King 	int ss, err, old_link = phydev->link;
92906d5f344SRussell King 
93006d5f344SRussell King 	/* Update the link, but return if there was an error */
93106d5f344SRussell King 	err = genphy_update_link(phydev);
93206d5f344SRussell King 	if (err)
93306d5f344SRussell King 		return err;
93406d5f344SRussell King 
93506d5f344SRussell King 	/* why bother the PHY if nothing can have changed */
93606d5f344SRussell King 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
93706d5f344SRussell King 		return 0;
93806d5f344SRussell King 
93906d5f344SRussell King 	phydev->speed = SPEED_UNKNOWN;
94006d5f344SRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
94106d5f344SRussell King 	phydev->pause = 0;
94206d5f344SRussell King 	phydev->asym_pause = 0;
94306d5f344SRussell King 
94406d5f344SRussell King 	err = genphy_read_lpa(phydev);
94506d5f344SRussell King 	if (err < 0)
94606d5f344SRussell King 		return err;
94706d5f344SRussell King 
94806d5f344SRussell King 	/* Read the AT8035 PHY-Specific Status register, which indicates the
94906d5f344SRussell King 	 * speed and duplex that the PHY is actually using, irrespective of
95006d5f344SRussell King 	 * whether we are in autoneg mode or not.
95106d5f344SRussell King 	 */
95206d5f344SRussell King 	ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
95306d5f344SRussell King 	if (ss < 0)
95406d5f344SRussell King 		return ss;
95506d5f344SRussell King 
95606d5f344SRussell King 	if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
9577dce80c2SOleksij Rempel 		int sfc;
9587dce80c2SOleksij Rempel 
9597dce80c2SOleksij Rempel 		sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL);
9607dce80c2SOleksij Rempel 		if (sfc < 0)
9617dce80c2SOleksij Rempel 			return sfc;
9627dce80c2SOleksij Rempel 
96306d5f344SRussell King 		switch (ss & AT803X_SS_SPEED_MASK) {
96406d5f344SRussell King 		case AT803X_SS_SPEED_10:
96506d5f344SRussell King 			phydev->speed = SPEED_10;
96606d5f344SRussell King 			break;
96706d5f344SRussell King 		case AT803X_SS_SPEED_100:
96806d5f344SRussell King 			phydev->speed = SPEED_100;
96906d5f344SRussell King 			break;
97006d5f344SRussell King 		case AT803X_SS_SPEED_1000:
97106d5f344SRussell King 			phydev->speed = SPEED_1000;
97206d5f344SRussell King 			break;
97306d5f344SRussell King 		}
97406d5f344SRussell King 		if (ss & AT803X_SS_DUPLEX)
97506d5f344SRussell King 			phydev->duplex = DUPLEX_FULL;
97606d5f344SRussell King 		else
97706d5f344SRussell King 			phydev->duplex = DUPLEX_HALF;
9787dce80c2SOleksij Rempel 
97906d5f344SRussell King 		if (ss & AT803X_SS_MDIX)
98006d5f344SRussell King 			phydev->mdix = ETH_TP_MDI_X;
98106d5f344SRussell King 		else
98206d5f344SRussell King 			phydev->mdix = ETH_TP_MDI;
9837dce80c2SOleksij Rempel 
9847dce80c2SOleksij Rempel 		switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) {
9857dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDI:
9867dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
9877dce80c2SOleksij Rempel 			break;
9887dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDIX:
9897dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
9907dce80c2SOleksij Rempel 			break;
9917dce80c2SOleksij Rempel 		case AT803X_SFC_AUTOMATIC_CROSSOVER:
9927dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
9937dce80c2SOleksij Rempel 			break;
9947dce80c2SOleksij Rempel 		}
99506d5f344SRussell King 	}
99606d5f344SRussell King 
99706d5f344SRussell King 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
99806d5f344SRussell King 		phy_resolve_aneg_pause(phydev);
99906d5f344SRussell King 
100006d5f344SRussell King 	return 0;
100106d5f344SRussell King }
100206d5f344SRussell King 
10037dce80c2SOleksij Rempel static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl)
10047dce80c2SOleksij Rempel {
10057dce80c2SOleksij Rempel 	u16 val;
10067dce80c2SOleksij Rempel 
10077dce80c2SOleksij Rempel 	switch (ctrl) {
10087dce80c2SOleksij Rempel 	case ETH_TP_MDI:
10097dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDI;
10107dce80c2SOleksij Rempel 		break;
10117dce80c2SOleksij Rempel 	case ETH_TP_MDI_X:
10127dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDIX;
10137dce80c2SOleksij Rempel 		break;
10147dce80c2SOleksij Rempel 	case ETH_TP_MDI_AUTO:
10157dce80c2SOleksij Rempel 		val = AT803X_SFC_AUTOMATIC_CROSSOVER;
10167dce80c2SOleksij Rempel 		break;
10177dce80c2SOleksij Rempel 	default:
10187dce80c2SOleksij Rempel 		return 0;
10197dce80c2SOleksij Rempel 	}
10207dce80c2SOleksij Rempel 
10217dce80c2SOleksij Rempel 	return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL,
10227dce80c2SOleksij Rempel 			  AT803X_SFC_MDI_CROSSOVER_MODE_M,
10237dce80c2SOleksij Rempel 			  FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val));
10247dce80c2SOleksij Rempel }
10257dce80c2SOleksij Rempel 
10267dce80c2SOleksij Rempel static int at803x_config_aneg(struct phy_device *phydev)
10277dce80c2SOleksij Rempel {
10287dce80c2SOleksij Rempel 	int ret;
10297dce80c2SOleksij Rempel 
10307dce80c2SOleksij Rempel 	ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
10317dce80c2SOleksij Rempel 	if (ret < 0)
10327dce80c2SOleksij Rempel 		return ret;
10337dce80c2SOleksij Rempel 
10347dce80c2SOleksij Rempel 	/* Changes of the midx bits are disruptive to the normal operation;
10357dce80c2SOleksij Rempel 	 * therefore any changes to these registers must be followed by a
10367dce80c2SOleksij Rempel 	 * software reset to take effect.
10377dce80c2SOleksij Rempel 	 */
10387dce80c2SOleksij Rempel 	if (ret == 1) {
10397dce80c2SOleksij Rempel 		ret = genphy_soft_reset(phydev);
10407dce80c2SOleksij Rempel 		if (ret < 0)
10417dce80c2SOleksij Rempel 			return ret;
10427dce80c2SOleksij Rempel 	}
10437dce80c2SOleksij Rempel 
10447dce80c2SOleksij Rempel 	return genphy_config_aneg(phydev);
10457dce80c2SOleksij Rempel }
10467dce80c2SOleksij Rempel 
1047cde0f4f8SMichael Walle static int at803x_get_downshift(struct phy_device *phydev, u8 *d)
1048cde0f4f8SMichael Walle {
1049cde0f4f8SMichael Walle 	int val;
1050cde0f4f8SMichael Walle 
1051cde0f4f8SMichael Walle 	val = phy_read(phydev, AT803X_SMART_SPEED);
1052cde0f4f8SMichael Walle 	if (val < 0)
1053cde0f4f8SMichael Walle 		return val;
1054cde0f4f8SMichael Walle 
1055cde0f4f8SMichael Walle 	if (val & AT803X_SMART_SPEED_ENABLE)
1056cde0f4f8SMichael Walle 		*d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2;
1057cde0f4f8SMichael Walle 	else
1058cde0f4f8SMichael Walle 		*d = DOWNSHIFT_DEV_DISABLE;
1059cde0f4f8SMichael Walle 
1060cde0f4f8SMichael Walle 	return 0;
1061cde0f4f8SMichael Walle }
1062cde0f4f8SMichael Walle 
1063cde0f4f8SMichael Walle static int at803x_set_downshift(struct phy_device *phydev, u8 cnt)
1064cde0f4f8SMichael Walle {
1065cde0f4f8SMichael Walle 	u16 mask, set;
1066cde0f4f8SMichael Walle 	int ret;
1067cde0f4f8SMichael Walle 
1068cde0f4f8SMichael Walle 	switch (cnt) {
1069cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DEFAULT_COUNT:
1070cde0f4f8SMichael Walle 		cnt = AT803X_DEFAULT_DOWNSHIFT;
1071cde0f4f8SMichael Walle 		fallthrough;
1072cde0f4f8SMichael Walle 	case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT:
1073cde0f4f8SMichael Walle 		set = AT803X_SMART_SPEED_ENABLE |
1074cde0f4f8SMichael Walle 		      AT803X_SMART_SPEED_BYPASS_TIMER |
1075cde0f4f8SMichael Walle 		      FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2);
1076cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK;
1077cde0f4f8SMichael Walle 		break;
1078cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DISABLE:
1079cde0f4f8SMichael Walle 		set = 0;
1080cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_ENABLE |
1081cde0f4f8SMichael Walle 		       AT803X_SMART_SPEED_BYPASS_TIMER;
1082cde0f4f8SMichael Walle 		break;
1083cde0f4f8SMichael Walle 	default:
1084cde0f4f8SMichael Walle 		return -EINVAL;
1085cde0f4f8SMichael Walle 	}
1086cde0f4f8SMichael Walle 
1087cde0f4f8SMichael Walle 	ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set);
1088cde0f4f8SMichael Walle 
1089cde0f4f8SMichael Walle 	/* After changing the smart speed settings, we need to perform a
1090cde0f4f8SMichael Walle 	 * software reset, use phy_init_hw() to make sure we set the
1091cde0f4f8SMichael Walle 	 * reapply any values which might got lost during software reset.
1092cde0f4f8SMichael Walle 	 */
1093cde0f4f8SMichael Walle 	if (ret == 1)
1094cde0f4f8SMichael Walle 		ret = phy_init_hw(phydev);
1095cde0f4f8SMichael Walle 
1096cde0f4f8SMichael Walle 	return ret;
1097cde0f4f8SMichael Walle }
1098cde0f4f8SMichael Walle 
1099cde0f4f8SMichael Walle static int at803x_get_tunable(struct phy_device *phydev,
1100cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, void *data)
1101cde0f4f8SMichael Walle {
1102cde0f4f8SMichael Walle 	switch (tuna->id) {
1103cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1104cde0f4f8SMichael Walle 		return at803x_get_downshift(phydev, data);
1105cde0f4f8SMichael Walle 	default:
1106cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1107cde0f4f8SMichael Walle 	}
1108cde0f4f8SMichael Walle }
1109cde0f4f8SMichael Walle 
1110cde0f4f8SMichael Walle static int at803x_set_tunable(struct phy_device *phydev,
1111cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, const void *data)
1112cde0f4f8SMichael Walle {
1113cde0f4f8SMichael Walle 	switch (tuna->id) {
1114cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1115cde0f4f8SMichael Walle 		return at803x_set_downshift(phydev, *(const u8 *)data);
1116cde0f4f8SMichael Walle 	default:
1117cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1118cde0f4f8SMichael Walle 	}
1119cde0f4f8SMichael Walle }
1120cde0f4f8SMichael Walle 
11216cb75767SMichael Walle static int at803x_cable_test_result_trans(u16 status)
11226cb75767SMichael Walle {
11236cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
11246cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_NORMAL:
11256cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
11266cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
11276cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
11286cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
11296cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
11306cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_FAIL:
11316cb75767SMichael Walle 	default:
11326cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
11336cb75767SMichael Walle 	}
11346cb75767SMichael Walle }
11356cb75767SMichael Walle 
11366cb75767SMichael Walle static bool at803x_cdt_test_failed(u16 status)
11376cb75767SMichael Walle {
11386cb75767SMichael Walle 	return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
11396cb75767SMichael Walle 		AT803X_CDT_STATUS_STAT_FAIL;
11406cb75767SMichael Walle }
11416cb75767SMichael Walle 
11426cb75767SMichael Walle static bool at803x_cdt_fault_length_valid(u16 status)
11436cb75767SMichael Walle {
11446cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
11456cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
11466cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
11476cb75767SMichael Walle 		return true;
11486cb75767SMichael Walle 	}
11496cb75767SMichael Walle 	return false;
11506cb75767SMichael Walle }
11516cb75767SMichael Walle 
11526cb75767SMichael Walle static int at803x_cdt_fault_length(u16 status)
11536cb75767SMichael Walle {
11546cb75767SMichael Walle 	int dt;
11556cb75767SMichael Walle 
11566cb75767SMichael Walle 	/* According to the datasheet the distance to the fault is
11576cb75767SMichael Walle 	 * DELTA_TIME * 0.824 meters.
11586cb75767SMichael Walle 	 *
11596cb75767SMichael Walle 	 * The author suspect the correct formula is:
11606cb75767SMichael Walle 	 *
11616cb75767SMichael Walle 	 *   fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2
11626cb75767SMichael Walle 	 *
11636cb75767SMichael Walle 	 * where c is the speed of light, VF is the velocity factor of
11646cb75767SMichael Walle 	 * the twisted pair cable, 125MHz the counter frequency and
11656cb75767SMichael Walle 	 * we need to divide by 2 because the hardware will measure the
11666cb75767SMichael Walle 	 * round trip time to the fault and back to the PHY.
11676cb75767SMichael Walle 	 *
11686cb75767SMichael Walle 	 * With a VF of 0.69 we get the factor 0.824 mentioned in the
11696cb75767SMichael Walle 	 * datasheet.
11706cb75767SMichael Walle 	 */
11716cb75767SMichael Walle 	dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status);
11726cb75767SMichael Walle 
11736cb75767SMichael Walle 	return (dt * 824) / 10;
11746cb75767SMichael Walle }
11756cb75767SMichael Walle 
11766cb75767SMichael Walle static int at803x_cdt_start(struct phy_device *phydev, int pair)
11776cb75767SMichael Walle {
11786cb75767SMichael Walle 	u16 cdt;
11796cb75767SMichael Walle 
11806cb75767SMichael Walle 	cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
11816cb75767SMichael Walle 	      AT803X_CDT_ENABLE_TEST;
11826cb75767SMichael Walle 
11836cb75767SMichael Walle 	return phy_write(phydev, AT803X_CDT, cdt);
11846cb75767SMichael Walle }
11856cb75767SMichael Walle 
11866cb75767SMichael Walle static int at803x_cdt_wait_for_completion(struct phy_device *phydev)
11876cb75767SMichael Walle {
11886cb75767SMichael Walle 	int val, ret;
11896cb75767SMichael Walle 
11906cb75767SMichael Walle 	/* One test run takes about 25ms */
11916cb75767SMichael Walle 	ret = phy_read_poll_timeout(phydev, AT803X_CDT, val,
11926cb75767SMichael Walle 				    !(val & AT803X_CDT_ENABLE_TEST),
11936cb75767SMichael Walle 				    30000, 100000, true);
11946cb75767SMichael Walle 
11956cb75767SMichael Walle 	return ret < 0 ? ret : 0;
11966cb75767SMichael Walle }
11976cb75767SMichael Walle 
11986cb75767SMichael Walle static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
11996cb75767SMichael Walle {
12006cb75767SMichael Walle 	static const int ethtool_pair[] = {
12016cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_A,
12026cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_B,
12036cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_C,
12046cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_D,
12056cb75767SMichael Walle 	};
12066cb75767SMichael Walle 	int ret, val;
12076cb75767SMichael Walle 
12086cb75767SMichael Walle 	ret = at803x_cdt_start(phydev, pair);
12096cb75767SMichael Walle 	if (ret)
12106cb75767SMichael Walle 		return ret;
12116cb75767SMichael Walle 
12126cb75767SMichael Walle 	ret = at803x_cdt_wait_for_completion(phydev);
12136cb75767SMichael Walle 	if (ret)
12146cb75767SMichael Walle 		return ret;
12156cb75767SMichael Walle 
12166cb75767SMichael Walle 	val = phy_read(phydev, AT803X_CDT_STATUS);
12176cb75767SMichael Walle 	if (val < 0)
12186cb75767SMichael Walle 		return val;
12196cb75767SMichael Walle 
12206cb75767SMichael Walle 	if (at803x_cdt_test_failed(val))
12216cb75767SMichael Walle 		return 0;
12226cb75767SMichael Walle 
12236cb75767SMichael Walle 	ethnl_cable_test_result(phydev, ethtool_pair[pair],
12246cb75767SMichael Walle 				at803x_cable_test_result_trans(val));
12256cb75767SMichael Walle 
12266cb75767SMichael Walle 	if (at803x_cdt_fault_length_valid(val))
12276cb75767SMichael Walle 		ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
12286cb75767SMichael Walle 					      at803x_cdt_fault_length(val));
12296cb75767SMichael Walle 
12306cb75767SMichael Walle 	return 1;
12316cb75767SMichael Walle }
12326cb75767SMichael Walle 
12336cb75767SMichael Walle static int at803x_cable_test_get_status(struct phy_device *phydev,
12346cb75767SMichael Walle 					bool *finished)
12356cb75767SMichael Walle {
1236dc0f3ed1SOleksij Rempel 	unsigned long pair_mask;
12376cb75767SMichael Walle 	int retries = 20;
12386cb75767SMichael Walle 	int pair, ret;
12396cb75767SMichael Walle 
1240dc0f3ed1SOleksij Rempel 	if (phydev->phy_id == ATH9331_PHY_ID ||
1241*fada2ce0SDavid Bauer 	    phydev->phy_id == ATH8032_PHY_ID ||
1242*fada2ce0SDavid Bauer 	    phydev->phy_id == QCA9561_PHY_ID)
1243dc0f3ed1SOleksij Rempel 		pair_mask = 0x3;
1244dc0f3ed1SOleksij Rempel 	else
1245dc0f3ed1SOleksij Rempel 		pair_mask = 0xf;
1246dc0f3ed1SOleksij Rempel 
12476cb75767SMichael Walle 	*finished = false;
12486cb75767SMichael Walle 
12496cb75767SMichael Walle 	/* According to the datasheet the CDT can be performed when
12506cb75767SMichael Walle 	 * there is no link partner or when the link partner is
12516cb75767SMichael Walle 	 * auto-negotiating. Starting the test will restart the AN
12526cb75767SMichael Walle 	 * automatically. It seems that doing this repeatedly we will
12536cb75767SMichael Walle 	 * get a slot where our link partner won't disturb our
12546cb75767SMichael Walle 	 * measurement.
12556cb75767SMichael Walle 	 */
12566cb75767SMichael Walle 	while (pair_mask && retries--) {
12576cb75767SMichael Walle 		for_each_set_bit(pair, &pair_mask, 4) {
12586cb75767SMichael Walle 			ret = at803x_cable_test_one_pair(phydev, pair);
12596cb75767SMichael Walle 			if (ret < 0)
12606cb75767SMichael Walle 				return ret;
12616cb75767SMichael Walle 			if (ret)
12626cb75767SMichael Walle 				clear_bit(pair, &pair_mask);
12636cb75767SMichael Walle 		}
12646cb75767SMichael Walle 		if (pair_mask)
12656cb75767SMichael Walle 			msleep(250);
12666cb75767SMichael Walle 	}
12676cb75767SMichael Walle 
12686cb75767SMichael Walle 	*finished = true;
12696cb75767SMichael Walle 
12706cb75767SMichael Walle 	return 0;
12716cb75767SMichael Walle }
12726cb75767SMichael Walle 
12736cb75767SMichael Walle static int at803x_cable_test_start(struct phy_device *phydev)
12746cb75767SMichael Walle {
12756cb75767SMichael Walle 	/* Enable auto-negotiation, but advertise no capabilities, no link
12766cb75767SMichael Walle 	 * will be established. A restart of the auto-negotiation is not
12776cb75767SMichael Walle 	 * required, because the cable test will automatically break the link.
12786cb75767SMichael Walle 	 */
12796cb75767SMichael Walle 	phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
12806cb75767SMichael Walle 	phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
1281dc0f3ed1SOleksij Rempel 	if (phydev->phy_id != ATH9331_PHY_ID &&
1282*fada2ce0SDavid Bauer 	    phydev->phy_id != ATH8032_PHY_ID &&
1283*fada2ce0SDavid Bauer 	    phydev->phy_id != QCA9561_PHY_ID)
12846cb75767SMichael Walle 		phy_write(phydev, MII_CTRL1000, 0);
12856cb75767SMichael Walle 
12866cb75767SMichael Walle 	/* we do all the (time consuming) work later */
12876cb75767SMichael Walle 	return 0;
12886cb75767SMichael Walle }
12896cb75767SMichael Walle 
1290272833b9SAnsuel Smith static int qca83xx_config_init(struct phy_device *phydev)
1291272833b9SAnsuel Smith {
1292272833b9SAnsuel Smith 	u8 switch_revision;
1293272833b9SAnsuel Smith 
1294272833b9SAnsuel Smith 	switch_revision = phydev->dev_flags & QCA8K_DEVFLAGS_REVISION_MASK;
1295272833b9SAnsuel Smith 
1296272833b9SAnsuel Smith 	switch (switch_revision) {
1297272833b9SAnsuel Smith 	case 1:
1298272833b9SAnsuel Smith 		/* For 100M waveform */
1299272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_0, 0x02ea);
1300272833b9SAnsuel Smith 		/* Turn on Gigabit clock */
1301272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3D, 0x68a0);
1302272833b9SAnsuel Smith 		break;
1303272833b9SAnsuel Smith 
1304272833b9SAnsuel Smith 	case 2:
1305272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0x0);
1306272833b9SAnsuel Smith 		fallthrough;
1307272833b9SAnsuel Smith 	case 4:
1308272833b9SAnsuel Smith 		phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_AZ_DEBUG, 0x803f);
1309272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3D, 0x6860);
1310272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_5, 0x2c46);
1311272833b9SAnsuel Smith 		at803x_debug_reg_write(phydev, AT803X_DEBUG_REG_3C, 0x6000);
1312272833b9SAnsuel Smith 		break;
1313272833b9SAnsuel Smith 	}
1314272833b9SAnsuel Smith 
1315272833b9SAnsuel Smith 	return 0;
1316272833b9SAnsuel Smith }
1317272833b9SAnsuel Smith 
1318317420abSMugunthan V N static struct phy_driver at803x_driver[] = {
1319317420abSMugunthan V N {
132096c36712SMichael Walle 	/* Qualcomm Atheros AR8035 */
13210465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
132296c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8035",
13236cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
13242f664823SMichael Walle 	.probe			= at803x_probe,
13252318ca8aSMichael Walle 	.remove			= at803x_remove,
13267dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
13270ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
1328cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
1329ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
1330ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
13316229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
13326229ed1fSDaniel Mack 	.resume			= at803x_resume,
1333dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
133406d5f344SRussell King 	.read_status		= at803x_read_status,
13350eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
133629773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1337cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
1338cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
13396cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
13406cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
1341317420abSMugunthan V N }, {
134296c36712SMichael Walle 	/* Qualcomm Atheros AR8030 */
1343bd8ca17fSDaniel Mack 	.phy_id			= ATH8030_PHY_ID,
134496c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8030",
13450465d8f8SMichael Walle 	.phy_id_mask		= AT8030_PHY_ID_MASK,
13462f664823SMichael Walle 	.probe			= at803x_probe,
13472318ca8aSMichael Walle 	.remove			= at803x_remove,
13480ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
134913a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
1350ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
1351ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
13526229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
13536229ed1fSDaniel Mack 	.resume			= at803x_resume,
1354dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
13550eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
135629773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
135705d7cce8SMugunthan V N }, {
135896c36712SMichael Walle 	/* Qualcomm Atheros AR8031/AR8033 */
13590465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
136096c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8031/AR8033",
13616cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
13622f664823SMichael Walle 	.probe			= at803x_probe,
13632318ca8aSMichael Walle 	.remove			= at803x_remove,
136405d7cce8SMugunthan V N 	.config_init		= at803x_config_init,
136563477a5dSMichael Walle 	.config_aneg		= at803x_config_aneg,
1366cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
136705d7cce8SMugunthan V N 	.set_wol		= at803x_set_wol,
136805d7cce8SMugunthan V N 	.get_wol		= at803x_get_wol,
13696229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
13706229ed1fSDaniel Mack 	.resume			= at803x_resume,
1371c329e5afSDavid Bauer 	.read_page		= at803x_read_page,
1372c329e5afSDavid Bauer 	.write_page		= at803x_write_page,
1373b856150cSDavid Bauer 	.get_features		= at803x_get_features,
137406d5f344SRussell King 	.read_status		= at803x_read_status,
137577a99394SZhao Qiang 	.config_intr		= &at803x_config_intr,
137629773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1377cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
1378cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
13796cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
13806cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
13817908d2ceSOleksij Rempel }, {
13825800091aSDavid Bauer 	/* Qualcomm Atheros AR8032 */
13835800091aSDavid Bauer 	PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
13845800091aSDavid Bauer 	.name			= "Qualcomm Atheros AR8032",
13855800091aSDavid Bauer 	.probe			= at803x_probe,
13865800091aSDavid Bauer 	.remove			= at803x_remove,
1387dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
13885800091aSDavid Bauer 	.config_init		= at803x_config_init,
13895800091aSDavid Bauer 	.link_change_notify	= at803x_link_change_notify,
13905800091aSDavid Bauer 	.set_wol		= at803x_set_wol,
13915800091aSDavid Bauer 	.get_wol		= at803x_get_wol,
13925800091aSDavid Bauer 	.suspend		= at803x_suspend,
13935800091aSDavid Bauer 	.resume			= at803x_resume,
13945800091aSDavid Bauer 	/* PHY_BASIC_FEATURES */
13955800091aSDavid Bauer 	.config_intr		= at803x_config_intr,
139629773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1397dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
1398dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
13995800091aSDavid Bauer }, {
14007908d2ceSOleksij Rempel 	/* ATHEROS AR9331 */
14017908d2ceSOleksij Rempel 	PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
140296c36712SMichael Walle 	.name			= "Qualcomm Atheros AR9331 built-in PHY",
14037908d2ceSOleksij Rempel 	.suspend		= at803x_suspend,
14047908d2ceSOleksij Rempel 	.resume			= at803x_resume,
1405dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
14067908d2ceSOleksij Rempel 	/* PHY_BASIC_FEATURES */
14077908d2ceSOleksij Rempel 	.config_intr		= &at803x_config_intr,
140829773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1409dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
1410dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
14117dce80c2SOleksij Rempel 	.read_status		= at803x_read_status,
14127dce80c2SOleksij Rempel 	.soft_reset		= genphy_soft_reset,
14137dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
1414272833b9SAnsuel Smith }, {
1415*fada2ce0SDavid Bauer 	/* Qualcomm Atheros QCA9561 */
1416*fada2ce0SDavid Bauer 	PHY_ID_MATCH_EXACT(QCA9561_PHY_ID),
1417*fada2ce0SDavid Bauer 	.name			= "Qualcomm Atheros QCA9561 built-in PHY",
1418*fada2ce0SDavid Bauer 	.suspend		= at803x_suspend,
1419*fada2ce0SDavid Bauer 	.resume			= at803x_resume,
1420*fada2ce0SDavid Bauer 	.flags			= PHY_POLL_CABLE_TEST,
1421*fada2ce0SDavid Bauer 	/* PHY_BASIC_FEATURES */
1422*fada2ce0SDavid Bauer 	.config_intr		= &at803x_config_intr,
1423*fada2ce0SDavid Bauer 	.handle_interrupt	= at803x_handle_interrupt,
1424*fada2ce0SDavid Bauer 	.cable_test_start	= at803x_cable_test_start,
1425*fada2ce0SDavid Bauer 	.cable_test_get_status	= at803x_cable_test_get_status,
1426*fada2ce0SDavid Bauer 	.read_status		= at803x_read_status,
1427*fada2ce0SDavid Bauer 	.soft_reset		= genphy_soft_reset,
1428*fada2ce0SDavid Bauer 	.config_aneg		= at803x_config_aneg,
1429*fada2ce0SDavid Bauer }, {
1430272833b9SAnsuel Smith 	/* QCA8337 */
1431272833b9SAnsuel Smith 	.phy_id			= QCA8337_PHY_ID,
1432272833b9SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
1433d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8337 internal PHY",
1434272833b9SAnsuel Smith 	/* PHY_GBIT_FEATURES */
1435272833b9SAnsuel Smith 	.probe			= at803x_probe,
1436272833b9SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
1437272833b9SAnsuel Smith 	.config_init		= qca83xx_config_init,
1438272833b9SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
1439272833b9SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
1440272833b9SAnsuel Smith 	.get_strings		= at803x_get_strings,
1441272833b9SAnsuel Smith 	.get_stats		= at803x_get_stats,
144215b9df4eSAnsuel Smith 	.suspend		= genphy_suspend,
144315b9df4eSAnsuel Smith 	.resume			= genphy_resume,
14440ccf8511SAnsuel Smith }, {
1445b4df02b5SAnsuel Smith 	/* QCA8327-A from switch QCA8327-AL1A */
1446b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_A_PHY_ID,
14470ccf8511SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
1448d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-A internal PHY",
1449b4df02b5SAnsuel Smith 	/* PHY_GBIT_FEATURES */
1450b4df02b5SAnsuel Smith 	.probe			= at803x_probe,
1451b4df02b5SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
1452b4df02b5SAnsuel Smith 	.config_init		= qca83xx_config_init,
1453b4df02b5SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
1454b4df02b5SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
1455b4df02b5SAnsuel Smith 	.get_strings		= at803x_get_strings,
1456b4df02b5SAnsuel Smith 	.get_stats		= at803x_get_stats,
145715b9df4eSAnsuel Smith 	.suspend		= genphy_suspend,
145815b9df4eSAnsuel Smith 	.resume			= genphy_resume,
1459b4df02b5SAnsuel Smith }, {
1460b4df02b5SAnsuel Smith 	/* QCA8327-B from switch QCA8327-BL1A */
1461b4df02b5SAnsuel Smith 	.phy_id			= QCA8327_B_PHY_ID,
1462b4df02b5SAnsuel Smith 	.phy_id_mask		= QCA8K_PHY_ID_MASK,
1463d44fd860SAnsuel Smith 	.name			= "Qualcomm Atheros 8327-B internal PHY",
14640ccf8511SAnsuel Smith 	/* PHY_GBIT_FEATURES */
14650ccf8511SAnsuel Smith 	.probe			= at803x_probe,
14660ccf8511SAnsuel Smith 	.flags			= PHY_IS_INTERNAL,
14670ccf8511SAnsuel Smith 	.config_init		= qca83xx_config_init,
14680ccf8511SAnsuel Smith 	.soft_reset		= genphy_soft_reset,
14690ccf8511SAnsuel Smith 	.get_sset_count		= at803x_get_sset_count,
14700ccf8511SAnsuel Smith 	.get_strings		= at803x_get_strings,
14710ccf8511SAnsuel Smith 	.get_stats		= at803x_get_stats,
147215b9df4eSAnsuel Smith 	.suspend		= genphy_suspend,
147315b9df4eSAnsuel Smith 	.resume			= genphy_resume,
1474272833b9SAnsuel Smith }, };
14750ca7111aSMatus Ujhelyi 
147650fd7150SJohan Hovold module_phy_driver(at803x_driver);
14770ca7111aSMatus Ujhelyi 
14780ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = {
14790465d8f8SMichael Walle 	{ ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
14800465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
14815800091aSDavid Bauer 	{ PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
14820465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
14837908d2ceSOleksij Rempel 	{ PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
14840ccf8511SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8337_PHY_ID) },
1485b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_A_PHY_ID) },
1486b4df02b5SAnsuel Smith 	{ PHY_ID_MATCH_EXACT(QCA8327_B_PHY_ID) },
1487*fada2ce0SDavid Bauer 	{ PHY_ID_MATCH_EXACT(QCA9561_PHY_ID) },
14880ca7111aSMatus Ujhelyi 	{ }
14890ca7111aSMatus Ujhelyi };
14900ca7111aSMatus Ujhelyi 
14910ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl);
1492