xref: /openbmc/linux/drivers/net/phy/at803x.c (revision b856150c)
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 
952f664823SMichael Walle #define AT803X_DEBUG_REG_1F			0x1F
962f664823SMichael Walle #define AT803X_DEBUG_PLL_ON			BIT(2)
972f664823SMichael Walle #define AT803X_DEBUG_RGMII_1V8			BIT(3)
982f664823SMichael Walle 
992f664823SMichael Walle /* AT803x supports either the XTAL input pad, an internal PLL or the
1002f664823SMichael Walle  * DSP as clock reference for the clock output pad. The XTAL reference
1012f664823SMichael Walle  * is only used for 25 MHz output, all other frequencies need the PLL.
1022f664823SMichael Walle  * The DSP as a clock reference is used in synchronous ethernet
1032f664823SMichael Walle  * applications.
1042f664823SMichael Walle  *
1052f664823SMichael Walle  * By default the PLL is only enabled if there is a link. Otherwise
1062f664823SMichael Walle  * the PHY will go into low power state and disabled the PLL. You can
1072f664823SMichael Walle  * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
1082f664823SMichael Walle  * enabled.
1092f664823SMichael Walle  */
1102f664823SMichael Walle #define AT803X_MMD7_CLK25M			0x8016
1112f664823SMichael Walle #define AT803X_CLK_OUT_MASK			GENMASK(4, 2)
1122f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_XTAL		0
1132f664823SMichael Walle #define AT803X_CLK_OUT_25MHZ_DSP		1
1142f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_PLL		2
1152f664823SMichael Walle #define AT803X_CLK_OUT_50MHZ_DSP		3
1162f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_PLL		4
1172f664823SMichael Walle #define AT803X_CLK_OUT_62_5MHZ_DSP		5
1182f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_PLL		6
1192f664823SMichael Walle #define AT803X_CLK_OUT_125MHZ_DSP		7
1202f664823SMichael Walle 
121428061f7SMichael Walle /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
122428061f7SMichael Walle  * but doesn't support choosing between XTAL/PLL and DSP.
1232f664823SMichael Walle  */
1242f664823SMichael Walle #define AT8035_CLK_OUT_MASK			GENMASK(4, 3)
1252f664823SMichael Walle 
1262f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_MASK		GENMASK(8, 7)
1272f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_FULL		0
1282f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_HALF		1
1292f664823SMichael Walle #define AT803X_CLK_OUT_STRENGTH_QUARTER		2
1302f664823SMichael Walle 
131cde0f4f8SMichael Walle #define AT803X_DEFAULT_DOWNSHIFT 5
132cde0f4f8SMichael Walle #define AT803X_MIN_DOWNSHIFT 2
133cde0f4f8SMichael Walle #define AT803X_MAX_DOWNSHIFT 9
134cde0f4f8SMichael Walle 
135390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL1		0x805b
136390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL2		0x805c
137390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3		0x805d
138390b4cadSRussell King #define AT803X_MMD3_SMARTEEE_CTL3_LPI_EN	BIT(8)
139390b4cadSRussell King 
1407908d2ceSOleksij Rempel #define ATH9331_PHY_ID 0x004dd041
141bd8ca17fSDaniel Mack #define ATH8030_PHY_ID 0x004dd076
142bd8ca17fSDaniel Mack #define ATH8031_PHY_ID 0x004dd074
1435800091aSDavid Bauer #define ATH8032_PHY_ID 0x004dd023
144bd8ca17fSDaniel Mack #define ATH8035_PHY_ID 0x004dd072
1450465d8f8SMichael Walle #define AT8030_PHY_ID_MASK			0xffffffef
146bd8ca17fSDaniel Mack 
147c329e5afSDavid Bauer #define AT803X_PAGE_FIBER		0
148c329e5afSDavid Bauer #define AT803X_PAGE_COPPER		1
149c329e5afSDavid Bauer 
15096c36712SMichael Walle MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
1510ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi");
1520ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL");
1530ca7111aSMatus Ujhelyi 
1542f664823SMichael Walle struct at803x_priv {
1552f664823SMichael Walle 	int flags;
1562f664823SMichael Walle #define AT803X_KEEP_PLL_ENABLED	BIT(0)	/* don't turn off internal PLL */
157390b4cadSRussell King #define AT803X_DISABLE_SMARTEEE	BIT(1)
1582f664823SMichael Walle 	u16 clk_25m_reg;
1592f664823SMichael Walle 	u16 clk_25m_mask;
160390b4cadSRussell King 	u8 smarteee_lpi_tw_1g;
161390b4cadSRussell King 	u8 smarteee_lpi_tw_100m;
1622f664823SMichael Walle 	struct regulator_dev *vddio_rdev;
1632f664823SMichael Walle 	struct regulator_dev *vddh_rdev;
1642f664823SMichael Walle 	struct regulator *vddio;
1652f664823SMichael Walle };
1662f664823SMichael Walle 
16713a56b44SDaniel Mack struct at803x_context {
16813a56b44SDaniel Mack 	u16 bmcr;
16913a56b44SDaniel Mack 	u16 advertise;
17013a56b44SDaniel Mack 	u16 control1000;
17113a56b44SDaniel Mack 	u16 int_enable;
17213a56b44SDaniel Mack 	u16 smart_speed;
17313a56b44SDaniel Mack 	u16 led_control;
17413a56b44SDaniel Mack };
17513a56b44SDaniel Mack 
1762e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
1772e5f9f28SMartin Blumenstingl {
1782e5f9f28SMartin Blumenstingl 	int ret;
1792e5f9f28SMartin Blumenstingl 
1802e5f9f28SMartin Blumenstingl 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
1812e5f9f28SMartin Blumenstingl 	if (ret < 0)
1822e5f9f28SMartin Blumenstingl 		return ret;
1832e5f9f28SMartin Blumenstingl 
1842e5f9f28SMartin Blumenstingl 	return phy_read(phydev, AT803X_DEBUG_DATA);
1852e5f9f28SMartin Blumenstingl }
1862e5f9f28SMartin Blumenstingl 
1872e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
1882e5f9f28SMartin Blumenstingl 				 u16 clear, u16 set)
1892e5f9f28SMartin Blumenstingl {
1902e5f9f28SMartin Blumenstingl 	u16 val;
1912e5f9f28SMartin Blumenstingl 	int ret;
1922e5f9f28SMartin Blumenstingl 
1932e5f9f28SMartin Blumenstingl 	ret = at803x_debug_reg_read(phydev, reg);
1942e5f9f28SMartin Blumenstingl 	if (ret < 0)
1952e5f9f28SMartin Blumenstingl 		return ret;
1962e5f9f28SMartin Blumenstingl 
1972e5f9f28SMartin Blumenstingl 	val = ret & 0xffff;
1982e5f9f28SMartin Blumenstingl 	val &= ~clear;
1992e5f9f28SMartin Blumenstingl 	val |= set;
2002e5f9f28SMartin Blumenstingl 
2012e5f9f28SMartin Blumenstingl 	return phy_write(phydev, AT803X_DEBUG_DATA, val);
2022e5f9f28SMartin Blumenstingl }
2032e5f9f28SMartin Blumenstingl 
204c329e5afSDavid Bauer static int at803x_write_page(struct phy_device *phydev, int page)
205c329e5afSDavid Bauer {
206c329e5afSDavid Bauer 	int mask;
207c329e5afSDavid Bauer 	int set;
208c329e5afSDavid Bauer 
209c329e5afSDavid Bauer 	if (page == AT803X_PAGE_COPPER) {
210c329e5afSDavid Bauer 		set = AT803X_BT_BX_REG_SEL;
211c329e5afSDavid Bauer 		mask = 0;
212c329e5afSDavid Bauer 	} else {
213c329e5afSDavid Bauer 		set = 0;
214c329e5afSDavid Bauer 		mask = AT803X_BT_BX_REG_SEL;
215c329e5afSDavid Bauer 	}
216c329e5afSDavid Bauer 
217c329e5afSDavid Bauer 	return __phy_modify(phydev, AT803X_REG_CHIP_CONFIG, mask, set);
218c329e5afSDavid Bauer }
219c329e5afSDavid Bauer 
220c329e5afSDavid Bauer static int at803x_read_page(struct phy_device *phydev)
221c329e5afSDavid Bauer {
222c329e5afSDavid Bauer 	int ccr = __phy_read(phydev, AT803X_REG_CHIP_CONFIG);
223c329e5afSDavid Bauer 
224c329e5afSDavid Bauer 	if (ccr < 0)
225c329e5afSDavid Bauer 		return ccr;
226c329e5afSDavid Bauer 
227c329e5afSDavid Bauer 	if (ccr & AT803X_BT_BX_REG_SEL)
228c329e5afSDavid Bauer 		return AT803X_PAGE_COPPER;
229c329e5afSDavid Bauer 
230c329e5afSDavid Bauer 	return AT803X_PAGE_FIBER;
231c329e5afSDavid Bauer }
232c329e5afSDavid Bauer 
2336d4cd041SVinod Koul static int at803x_enable_rx_delay(struct phy_device *phydev)
2346d4cd041SVinod Koul {
2356d4cd041SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
2366d4cd041SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN);
2376d4cd041SVinod Koul }
2386d4cd041SVinod Koul 
2396d4cd041SVinod Koul static int at803x_enable_tx_delay(struct phy_device *phydev)
2406d4cd041SVinod Koul {
2416d4cd041SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
2426d4cd041SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN);
2436d4cd041SVinod Koul }
2446d4cd041SVinod Koul 
24543f2ebd5SVinod Koul static int at803x_disable_rx_delay(struct phy_device *phydev)
2462e5f9f28SMartin Blumenstingl {
247cd28d1d6SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0,
248cd28d1d6SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN, 0);
2492e5f9f28SMartin Blumenstingl }
2502e5f9f28SMartin Blumenstingl 
25143f2ebd5SVinod Koul static int at803x_disable_tx_delay(struct phy_device *phydev)
2522e5f9f28SMartin Blumenstingl {
253cd28d1d6SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5,
254cd28d1d6SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN, 0);
2552e5f9f28SMartin Blumenstingl }
2562e5f9f28SMartin Blumenstingl 
25713a56b44SDaniel Mack /* save relevant PHY registers to private copy */
25813a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev,
25913a56b44SDaniel Mack 				struct at803x_context *context)
26013a56b44SDaniel Mack {
26113a56b44SDaniel Mack 	context->bmcr = phy_read(phydev, MII_BMCR);
26213a56b44SDaniel Mack 	context->advertise = phy_read(phydev, MII_ADVERTISE);
26313a56b44SDaniel Mack 	context->control1000 = phy_read(phydev, MII_CTRL1000);
26413a56b44SDaniel Mack 	context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
26513a56b44SDaniel Mack 	context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
26613a56b44SDaniel Mack 	context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
26713a56b44SDaniel Mack }
26813a56b44SDaniel Mack 
26913a56b44SDaniel Mack /* restore relevant PHY registers from private copy */
27013a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev,
27113a56b44SDaniel Mack 				   const struct at803x_context *context)
27213a56b44SDaniel Mack {
27313a56b44SDaniel Mack 	phy_write(phydev, MII_BMCR, context->bmcr);
27413a56b44SDaniel Mack 	phy_write(phydev, MII_ADVERTISE, context->advertise);
27513a56b44SDaniel Mack 	phy_write(phydev, MII_CTRL1000, context->control1000);
27613a56b44SDaniel Mack 	phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
27713a56b44SDaniel Mack 	phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
27813a56b44SDaniel Mack 	phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
27913a56b44SDaniel Mack }
28013a56b44SDaniel Mack 
281ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev,
282ea13c9eeSMugunthan V N 			  struct ethtool_wolinfo *wol)
2830ca7111aSMatus Ujhelyi {
2840ca7111aSMatus Ujhelyi 	struct net_device *ndev = phydev->attached_dev;
2850ca7111aSMatus Ujhelyi 	const u8 *mac;
286ea13c9eeSMugunthan V N 	int ret;
287ea13c9eeSMugunthan V N 	u32 value;
2880ca7111aSMatus Ujhelyi 	unsigned int i, offsets[] = {
2890ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_32_47_OFFSET,
2900ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_16_31_OFFSET,
2910ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_0_15_OFFSET,
2920ca7111aSMatus Ujhelyi 	};
2930ca7111aSMatus Ujhelyi 
2940ca7111aSMatus Ujhelyi 	if (!ndev)
295ea13c9eeSMugunthan V N 		return -ENODEV;
2960ca7111aSMatus Ujhelyi 
297ea13c9eeSMugunthan V N 	if (wol->wolopts & WAKE_MAGIC) {
2980ca7111aSMatus Ujhelyi 		mac = (const u8 *) ndev->dev_addr;
2990ca7111aSMatus Ujhelyi 
3000ca7111aSMatus Ujhelyi 		if (!is_valid_ether_addr(mac))
301fc755687SDan Murphy 			return -EINVAL;
3020ca7111aSMatus Ujhelyi 
3030e021396SCarlo Caione 		for (i = 0; i < 3; i++)
3040e021396SCarlo Caione 			phy_write_mmd(phydev, AT803X_DEVICE_ADDR, offsets[i],
3050ca7111aSMatus Ujhelyi 				      mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
306ea13c9eeSMugunthan V N 
307ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
308e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_WOL;
309ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
310ea13c9eeSMugunthan V N 		if (ret)
311ea13c9eeSMugunthan V N 			return ret;
312ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
313ea13c9eeSMugunthan V N 	} else {
314ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
315e6e4a556SMartin Blumenstingl 		value &= (~AT803X_INTR_ENABLE_WOL);
316ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
317ea13c9eeSMugunthan V N 		if (ret)
318ea13c9eeSMugunthan V N 			return ret;
319ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
320ea13c9eeSMugunthan V N 	}
321ea13c9eeSMugunthan V N 
322ea13c9eeSMugunthan V N 	return ret;
323ea13c9eeSMugunthan V N }
324ea13c9eeSMugunthan V N 
325ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev,
326ea13c9eeSMugunthan V N 			   struct ethtool_wolinfo *wol)
327ea13c9eeSMugunthan V N {
328ea13c9eeSMugunthan V N 	u32 value;
329ea13c9eeSMugunthan V N 
330ea13c9eeSMugunthan V N 	wol->supported = WAKE_MAGIC;
331ea13c9eeSMugunthan V N 	wol->wolopts = 0;
332ea13c9eeSMugunthan V N 
333ea13c9eeSMugunthan V N 	value = phy_read(phydev, AT803X_INTR_ENABLE);
334e6e4a556SMartin Blumenstingl 	if (value & AT803X_INTR_ENABLE_WOL)
335ea13c9eeSMugunthan V N 		wol->wolopts |= WAKE_MAGIC;
3360ca7111aSMatus Ujhelyi }
3370ca7111aSMatus Ujhelyi 
3386229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev)
3396229ed1fSDaniel Mack {
3406229ed1fSDaniel Mack 	int value;
3416229ed1fSDaniel Mack 	int wol_enabled;
3426229ed1fSDaniel Mack 
3436229ed1fSDaniel Mack 	value = phy_read(phydev, AT803X_INTR_ENABLE);
344e6e4a556SMartin Blumenstingl 	wol_enabled = value & AT803X_INTR_ENABLE_WOL;
3456229ed1fSDaniel Mack 
3466229ed1fSDaniel Mack 	if (wol_enabled)
347fea23fb5SRussell King 		value = BMCR_ISOLATE;
3486229ed1fSDaniel Mack 	else
349fea23fb5SRussell King 		value = BMCR_PDOWN;
3506229ed1fSDaniel Mack 
351fea23fb5SRussell King 	phy_modify(phydev, MII_BMCR, 0, value);
3526229ed1fSDaniel Mack 
3536229ed1fSDaniel Mack 	return 0;
3546229ed1fSDaniel Mack }
3556229ed1fSDaniel Mack 
3566229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev)
3576229ed1fSDaniel Mack {
358f102852fSRussell King 	return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
3596229ed1fSDaniel Mack }
3606229ed1fSDaniel Mack 
3612f664823SMichael Walle static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
3622f664823SMichael Walle 					    unsigned int selector)
3632f664823SMichael Walle {
3642f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
3652f664823SMichael Walle 
3662f664823SMichael Walle 	if (selector)
3672f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
3682f664823SMichael Walle 					     0, AT803X_DEBUG_RGMII_1V8);
3692f664823SMichael Walle 	else
3702f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
3712f664823SMichael Walle 					     AT803X_DEBUG_RGMII_1V8, 0);
3722f664823SMichael Walle }
3732f664823SMichael Walle 
3742f664823SMichael Walle static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
3752f664823SMichael Walle {
3762f664823SMichael Walle 	struct phy_device *phydev = rdev_get_drvdata(rdev);
3772f664823SMichael Walle 	int val;
3782f664823SMichael Walle 
3792f664823SMichael Walle 	val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
3802f664823SMichael Walle 	if (val < 0)
3812f664823SMichael Walle 		return val;
3822f664823SMichael Walle 
3832f664823SMichael Walle 	return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
3842f664823SMichael Walle }
3852f664823SMichael Walle 
3863faaf539SRikard Falkeborn static const struct regulator_ops vddio_regulator_ops = {
3872f664823SMichael Walle 	.list_voltage = regulator_list_voltage_table,
3882f664823SMichael Walle 	.set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
3892f664823SMichael Walle 	.get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
3902f664823SMichael Walle };
3912f664823SMichael Walle 
3922f664823SMichael Walle static const unsigned int vddio_voltage_table[] = {
3932f664823SMichael Walle 	1500000,
3942f664823SMichael Walle 	1800000,
3952f664823SMichael Walle };
3962f664823SMichael Walle 
3972f664823SMichael Walle static const struct regulator_desc vddio_desc = {
3982f664823SMichael Walle 	.name = "vddio",
3992f664823SMichael Walle 	.of_match = of_match_ptr("vddio-regulator"),
4002f664823SMichael Walle 	.n_voltages = ARRAY_SIZE(vddio_voltage_table),
4012f664823SMichael Walle 	.volt_table = vddio_voltage_table,
4022f664823SMichael Walle 	.ops = &vddio_regulator_ops,
4032f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
4042f664823SMichael Walle 	.owner = THIS_MODULE,
4052f664823SMichael Walle };
4062f664823SMichael Walle 
4073faaf539SRikard Falkeborn static const struct regulator_ops vddh_regulator_ops = {
4082f664823SMichael Walle };
4092f664823SMichael Walle 
4102f664823SMichael Walle static const struct regulator_desc vddh_desc = {
4112f664823SMichael Walle 	.name = "vddh",
4122f664823SMichael Walle 	.of_match = of_match_ptr("vddh-regulator"),
4132f664823SMichael Walle 	.n_voltages = 1,
4142f664823SMichael Walle 	.fixed_uV = 2500000,
4152f664823SMichael Walle 	.ops = &vddh_regulator_ops,
4162f664823SMichael Walle 	.type = REGULATOR_VOLTAGE,
4172f664823SMichael Walle 	.owner = THIS_MODULE,
4182f664823SMichael Walle };
4192f664823SMichael Walle 
4202f664823SMichael Walle static int at8031_register_regulators(struct phy_device *phydev)
4212f664823SMichael Walle {
4222f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
4232f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
4242f664823SMichael Walle 	struct regulator_config config = { };
4252f664823SMichael Walle 
4262f664823SMichael Walle 	config.dev = dev;
4272f664823SMichael Walle 	config.driver_data = phydev;
4282f664823SMichael Walle 
4292f664823SMichael Walle 	priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
4302f664823SMichael Walle 	if (IS_ERR(priv->vddio_rdev)) {
4312f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDIO regulator\n");
4322f664823SMichael Walle 		return PTR_ERR(priv->vddio_rdev);
4332f664823SMichael Walle 	}
4342f664823SMichael Walle 
4352f664823SMichael Walle 	priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
4362f664823SMichael Walle 	if (IS_ERR(priv->vddh_rdev)) {
4372f664823SMichael Walle 		phydev_err(phydev, "failed to register VDDH regulator\n");
4382f664823SMichael Walle 		return PTR_ERR(priv->vddh_rdev);
4392f664823SMichael Walle 	}
4402f664823SMichael Walle 
4412f664823SMichael Walle 	return 0;
4422f664823SMichael Walle }
4432f664823SMichael Walle 
4442f664823SMichael Walle static bool at803x_match_phy_id(struct phy_device *phydev, u32 phy_id)
4452f664823SMichael Walle {
4462f664823SMichael Walle 	return (phydev->phy_id & phydev->drv->phy_id_mask)
4472f664823SMichael Walle 		== (phy_id & phydev->drv->phy_id_mask);
4482f664823SMichael Walle }
4492f664823SMichael Walle 
4502f664823SMichael Walle static int at803x_parse_dt(struct phy_device *phydev)
4512f664823SMichael Walle {
4522f664823SMichael Walle 	struct device_node *node = phydev->mdio.dev.of_node;
4532f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
454390b4cadSRussell King 	u32 freq, strength, tw;
4553f2edd30SAndrew Lunn 	unsigned int sel;
4562f664823SMichael Walle 	int ret;
4572f664823SMichael Walle 
4582f664823SMichael Walle 	if (!IS_ENABLED(CONFIG_OF_MDIO))
4592f664823SMichael Walle 		return 0;
4602f664823SMichael Walle 
461390b4cadSRussell King 	if (of_property_read_bool(node, "qca,disable-smarteee"))
462390b4cadSRussell King 		priv->flags |= AT803X_DISABLE_SMARTEEE;
463390b4cadSRussell King 
464390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-1g", &tw)) {
465390b4cadSRussell King 		if (!tw || tw > 255) {
466390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-1g\n");
467390b4cadSRussell King 			return -EINVAL;
468390b4cadSRussell King 		}
469390b4cadSRussell King 		priv->smarteee_lpi_tw_1g = tw;
470390b4cadSRussell King 	}
471390b4cadSRussell King 
472390b4cadSRussell King 	if (!of_property_read_u32(node, "qca,smarteee-tw-us-100m", &tw)) {
473390b4cadSRussell King 		if (!tw || tw > 255) {
474390b4cadSRussell King 			phydev_err(phydev, "invalid qca,smarteee-tw-us-100m\n");
475390b4cadSRussell King 			return -EINVAL;
476390b4cadSRussell King 		}
477390b4cadSRussell King 		priv->smarteee_lpi_tw_100m = tw;
478390b4cadSRussell King 	}
479390b4cadSRussell King 
4802f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
4812f664823SMichael Walle 	if (!ret) {
4822f664823SMichael Walle 		switch (freq) {
4832f664823SMichael Walle 		case 25000000:
4842f664823SMichael Walle 			sel = AT803X_CLK_OUT_25MHZ_XTAL;
4852f664823SMichael Walle 			break;
4862f664823SMichael Walle 		case 50000000:
4872f664823SMichael Walle 			sel = AT803X_CLK_OUT_50MHZ_PLL;
4882f664823SMichael Walle 			break;
4892f664823SMichael Walle 		case 62500000:
4902f664823SMichael Walle 			sel = AT803X_CLK_OUT_62_5MHZ_PLL;
4912f664823SMichael Walle 			break;
4922f664823SMichael Walle 		case 125000000:
4932f664823SMichael Walle 			sel = AT803X_CLK_OUT_125MHZ_PLL;
4942f664823SMichael Walle 			break;
4952f664823SMichael Walle 		default:
4962f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-frequency\n");
4972f664823SMichael Walle 			return -EINVAL;
4982f664823SMichael Walle 		}
4992f664823SMichael Walle 
5003f2edd30SAndrew Lunn 		priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
5013f2edd30SAndrew Lunn 		priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
5022f664823SMichael Walle 
5032f664823SMichael Walle 		/* Fixup for the AR8030/AR8035. This chip has another mask and
5042f664823SMichael Walle 		 * doesn't support the DSP reference. Eg. the lowest bit of the
5052f664823SMichael Walle 		 * mask. The upper two bits select the same frequencies. Mask
5062f664823SMichael Walle 		 * the lowest bit here.
5072f664823SMichael Walle 		 *
5082f664823SMichael Walle 		 * Warning:
5092f664823SMichael Walle 		 *   There was no datasheet for the AR8030 available so this is
5102f664823SMichael Walle 		 *   just a guess. But the AR8035 is listed as pin compatible
5112f664823SMichael Walle 		 *   to the AR8030 so there might be a good chance it works on
5122f664823SMichael Walle 		 *   the AR8030 too.
5132f664823SMichael Walle 		 */
5142f664823SMichael Walle 		if (at803x_match_phy_id(phydev, ATH8030_PHY_ID) ||
5152f664823SMichael Walle 		    at803x_match_phy_id(phydev, ATH8035_PHY_ID)) {
516b1f4c209SOleksij Rempel 			priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
517b1f4c209SOleksij Rempel 			priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
5182f664823SMichael Walle 		}
5192f664823SMichael Walle 	}
5202f664823SMichael Walle 
5212f664823SMichael Walle 	ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
5222f664823SMichael Walle 	if (!ret) {
5232f664823SMichael Walle 		priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
5242f664823SMichael Walle 		switch (strength) {
5252f664823SMichael Walle 		case AR803X_STRENGTH_FULL:
5262f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
5272f664823SMichael Walle 			break;
5282f664823SMichael Walle 		case AR803X_STRENGTH_HALF:
5292f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
5302f664823SMichael Walle 			break;
5312f664823SMichael Walle 		case AR803X_STRENGTH_QUARTER:
5322f664823SMichael Walle 			priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
5332f664823SMichael Walle 			break;
5342f664823SMichael Walle 		default:
5352f664823SMichael Walle 			phydev_err(phydev, "invalid qca,clk-out-strength\n");
5362f664823SMichael Walle 			return -EINVAL;
5372f664823SMichael Walle 		}
5382f664823SMichael Walle 	}
5392f664823SMichael Walle 
540428061f7SMichael Walle 	/* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
541428061f7SMichael Walle 	 * options.
542428061f7SMichael Walle 	 */
5432f664823SMichael Walle 	if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
5442f664823SMichael Walle 		if (of_property_read_bool(node, "qca,keep-pll-enabled"))
5452f664823SMichael Walle 			priv->flags |= AT803X_KEEP_PLL_ENABLED;
5462f664823SMichael Walle 
5472f664823SMichael Walle 		ret = at8031_register_regulators(phydev);
5482f664823SMichael Walle 		if (ret < 0)
5492f664823SMichael Walle 			return ret;
5502f664823SMichael Walle 
5512f664823SMichael Walle 		priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev,
5522f664823SMichael Walle 							  "vddio");
5532f664823SMichael Walle 		if (IS_ERR(priv->vddio)) {
5542f664823SMichael Walle 			phydev_err(phydev, "failed to get VDDIO regulator\n");
5552f664823SMichael Walle 			return PTR_ERR(priv->vddio);
5562f664823SMichael Walle 		}
5572f664823SMichael Walle 	}
5582f664823SMichael Walle 
5592f664823SMichael Walle 	return 0;
5602f664823SMichael Walle }
5612f664823SMichael Walle 
5622f664823SMichael Walle static int at803x_probe(struct phy_device *phydev)
5632f664823SMichael Walle {
5642f664823SMichael Walle 	struct device *dev = &phydev->mdio.dev;
5652f664823SMichael Walle 	struct at803x_priv *priv;
566c329e5afSDavid Bauer 	int ret;
5672f664823SMichael Walle 
5682f664823SMichael Walle 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
5692f664823SMichael Walle 	if (!priv)
5702f664823SMichael Walle 		return -ENOMEM;
5712f664823SMichael Walle 
5722f664823SMichael Walle 	phydev->priv = priv;
5732f664823SMichael Walle 
574c329e5afSDavid Bauer 	ret = at803x_parse_dt(phydev);
575c329e5afSDavid Bauer 	if (ret)
576c329e5afSDavid Bauer 		return ret;
577c329e5afSDavid Bauer 
5788f7e8762SMichael Walle 	if (priv->vddio) {
5798f7e8762SMichael Walle 		ret = regulator_enable(priv->vddio);
5808f7e8762SMichael Walle 		if (ret < 0)
5818f7e8762SMichael Walle 			return ret;
5828f7e8762SMichael Walle 	}
5838f7e8762SMichael Walle 
584c329e5afSDavid Bauer 	/* Some bootloaders leave the fiber page selected.
585c329e5afSDavid Bauer 	 * Switch to the copper page, as otherwise we read
586c329e5afSDavid Bauer 	 * the PHY capabilities from the fiber side.
587c329e5afSDavid Bauer 	 */
588c329e5afSDavid Bauer 	if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
5898f7e8762SMichael Walle 		phy_lock_mdio_bus(phydev);
5908f7e8762SMichael Walle 		ret = at803x_write_page(phydev, AT803X_PAGE_COPPER);
5918f7e8762SMichael Walle 		phy_unlock_mdio_bus(phydev);
5928f7e8762SMichael Walle 		if (ret)
5938f7e8762SMichael Walle 			goto err;
594c329e5afSDavid Bauer 	}
595c329e5afSDavid Bauer 
5968f7e8762SMichael Walle 	return 0;
5978f7e8762SMichael Walle 
5988f7e8762SMichael Walle err:
5998f7e8762SMichael Walle 	if (priv->vddio)
6008f7e8762SMichael Walle 		regulator_disable(priv->vddio);
6018f7e8762SMichael Walle 
602c329e5afSDavid Bauer 	return ret;
6032f664823SMichael Walle }
6042f664823SMichael Walle 
6052318ca8aSMichael Walle static void at803x_remove(struct phy_device *phydev)
6062318ca8aSMichael Walle {
6072318ca8aSMichael Walle 	struct at803x_priv *priv = phydev->priv;
6082318ca8aSMichael Walle 
6092318ca8aSMichael Walle 	if (priv->vddio)
6102318ca8aSMichael Walle 		regulator_disable(priv->vddio);
6112318ca8aSMichael Walle }
6122318ca8aSMichael Walle 
613*b856150cSDavid Bauer static int at803x_get_features(struct phy_device *phydev)
614*b856150cSDavid Bauer {
615*b856150cSDavid Bauer 	int err;
616*b856150cSDavid Bauer 
617*b856150cSDavid Bauer 	err = genphy_read_abilities(phydev);
618*b856150cSDavid Bauer 	if (err)
619*b856150cSDavid Bauer 		return err;
620*b856150cSDavid Bauer 
621*b856150cSDavid Bauer 	if (!at803x_match_phy_id(phydev, ATH8031_PHY_ID))
622*b856150cSDavid Bauer 		return 0;
623*b856150cSDavid Bauer 
624*b856150cSDavid Bauer 	/* AR8031/AR8033 have different status registers
625*b856150cSDavid Bauer 	 * for copper and fiber operation. However, the
626*b856150cSDavid Bauer 	 * extended status register is the same for both
627*b856150cSDavid Bauer 	 * operation modes.
628*b856150cSDavid Bauer 	 *
629*b856150cSDavid Bauer 	 * As a result of that, ESTATUS_1000_XFULL is set
630*b856150cSDavid Bauer 	 * to 1 even when operating in copper TP mode.
631*b856150cSDavid Bauer 	 *
632*b856150cSDavid Bauer 	 * Remove this mode from the supported link modes,
633*b856150cSDavid Bauer 	 * as this driver currently only supports copper
634*b856150cSDavid Bauer 	 * operation.
635*b856150cSDavid Bauer 	 */
636*b856150cSDavid Bauer 	linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
637*b856150cSDavid Bauer 			   phydev->supported);
638*b856150cSDavid Bauer 	return 0;
639*b856150cSDavid Bauer }
640*b856150cSDavid Bauer 
641390b4cadSRussell King static int at803x_smarteee_config(struct phy_device *phydev)
642390b4cadSRussell King {
643390b4cadSRussell King 	struct at803x_priv *priv = phydev->priv;
644390b4cadSRussell King 	u16 mask = 0, val = 0;
645390b4cadSRussell King 	int ret;
646390b4cadSRussell King 
647390b4cadSRussell King 	if (priv->flags & AT803X_DISABLE_SMARTEEE)
648390b4cadSRussell King 		return phy_modify_mmd(phydev, MDIO_MMD_PCS,
649390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3,
650390b4cadSRussell King 				      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN, 0);
651390b4cadSRussell King 
652390b4cadSRussell King 	if (priv->smarteee_lpi_tw_1g) {
653390b4cadSRussell King 		mask |= 0xff00;
654390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_1g << 8;
655390b4cadSRussell King 	}
656390b4cadSRussell King 	if (priv->smarteee_lpi_tw_100m) {
657390b4cadSRussell King 		mask |= 0x00ff;
658390b4cadSRussell King 		val |= priv->smarteee_lpi_tw_100m;
659390b4cadSRussell King 	}
660390b4cadSRussell King 	if (!mask)
661390b4cadSRussell King 		return 0;
662390b4cadSRussell King 
663390b4cadSRussell King 	ret = phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL1,
664390b4cadSRussell King 			     mask, val);
665390b4cadSRussell King 	if (ret)
666390b4cadSRussell King 		return ret;
667390b4cadSRussell King 
668390b4cadSRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_PCS, AT803X_MMD3_SMARTEEE_CTL3,
669390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN,
670390b4cadSRussell King 			      AT803X_MMD3_SMARTEEE_CTL3_LPI_EN);
671390b4cadSRussell King }
672390b4cadSRussell King 
6732f664823SMichael Walle static int at803x_clk_out_config(struct phy_device *phydev)
6742f664823SMichael Walle {
6752f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
6762f664823SMichael Walle 
6772f664823SMichael Walle 	if (!priv->clk_25m_mask)
6782f664823SMichael Walle 		return 0;
6792f664823SMichael Walle 
680a45c1c10SRussell King 	return phy_modify_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M,
681a45c1c10SRussell King 			      priv->clk_25m_mask, priv->clk_25m_reg);
6822f664823SMichael Walle }
6832f664823SMichael Walle 
6842f664823SMichael Walle static int at8031_pll_config(struct phy_device *phydev)
6852f664823SMichael Walle {
6862f664823SMichael Walle 	struct at803x_priv *priv = phydev->priv;
6872f664823SMichael Walle 
6882f664823SMichael Walle 	/* The default after hardware reset is PLL OFF. After a soft reset, the
6892f664823SMichael Walle 	 * values are retained.
6902f664823SMichael Walle 	 */
6912f664823SMichael Walle 	if (priv->flags & AT803X_KEEP_PLL_ENABLED)
6922f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
6932f664823SMichael Walle 					     0, AT803X_DEBUG_PLL_ON);
6942f664823SMichael Walle 	else
6952f664823SMichael Walle 		return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
6962f664823SMichael Walle 					     AT803X_DEBUG_PLL_ON, 0);
6972f664823SMichael Walle }
6982f664823SMichael Walle 
6990ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev)
7000ca7111aSMatus Ujhelyi {
7011ca6d1b1SMugunthan V N 	int ret;
7020ca7111aSMatus Ujhelyi 
7036d4cd041SVinod Koul 	/* The RX and TX delay default is:
7046d4cd041SVinod Koul 	 *   after HW reset: RX delay enabled and TX delay disabled
7056d4cd041SVinod Koul 	 *   after SW reset: RX delay enabled, while TX delay retains the
7066d4cd041SVinod Koul 	 *   value before reset.
7076d4cd041SVinod Koul 	 */
708bb0ce4c1SAndré Draszik 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
709bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
710bb0ce4c1SAndré Draszik 		ret = at803x_enable_rx_delay(phydev);
711bb0ce4c1SAndré Draszik 	else
712cd28d1d6SVinod Koul 		ret = at803x_disable_rx_delay(phydev);
7132e5f9f28SMartin Blumenstingl 	if (ret < 0)
7141ca6d1b1SMugunthan V N 		return ret;
7156d4cd041SVinod Koul 
7166d4cd041SVinod Koul 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
717bb0ce4c1SAndré Draszik 	    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
7186d4cd041SVinod Koul 		ret = at803x_enable_tx_delay(phydev);
719bb0ce4c1SAndré Draszik 	else
720bb0ce4c1SAndré Draszik 		ret = at803x_disable_tx_delay(phydev);
7212f664823SMichael Walle 	if (ret < 0)
7226d4cd041SVinod Koul 		return ret;
7232f664823SMichael Walle 
724390b4cadSRussell King 	ret = at803x_smarteee_config(phydev);
725390b4cadSRussell King 	if (ret < 0)
726390b4cadSRussell King 		return ret;
727390b4cadSRussell King 
7282f664823SMichael Walle 	ret = at803x_clk_out_config(phydev);
7292f664823SMichael Walle 	if (ret < 0)
7302f664823SMichael Walle 		return ret;
7312f664823SMichael Walle 
7322f664823SMichael Walle 	if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
7332f664823SMichael Walle 		ret = at8031_pll_config(phydev);
7342f664823SMichael Walle 		if (ret < 0)
7352f664823SMichael Walle 			return ret;
7362f664823SMichael Walle 	}
7372f664823SMichael Walle 
7383c51fa5dSRussell King 	/* Ar803x extended next page bit is enabled by default. Cisco
7393c51fa5dSRussell King 	 * multigig switches read this bit and attempt to negotiate 10Gbps
7403c51fa5dSRussell King 	 * rates even if the next page bit is disabled. This is incorrect
7413c51fa5dSRussell King 	 * behaviour but we still need to accommodate it. XNP is only needed
7423c51fa5dSRussell King 	 * for 10Gbps support, so disable XNP.
7433c51fa5dSRussell King 	 */
7443c51fa5dSRussell King 	return phy_modify(phydev, MII_ADVERTISE, MDIO_AN_CTRL1_XNP, 0);
7450ca7111aSMatus Ujhelyi }
7460ca7111aSMatus Ujhelyi 
74777a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev)
74877a99394SZhao Qiang {
74977a99394SZhao Qiang 	int err;
75077a99394SZhao Qiang 
751a46bd63bSMartin Blumenstingl 	err = phy_read(phydev, AT803X_INTR_STATUS);
75277a99394SZhao Qiang 
75377a99394SZhao Qiang 	return (err < 0) ? err : 0;
75477a99394SZhao Qiang }
75577a99394SZhao Qiang 
75677a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev)
75777a99394SZhao Qiang {
75877a99394SZhao Qiang 	int err;
75977a99394SZhao Qiang 	int value;
76077a99394SZhao Qiang 
761a46bd63bSMartin Blumenstingl 	value = phy_read(phydev, AT803X_INTR_ENABLE);
76277a99394SZhao Qiang 
763e6e4a556SMartin Blumenstingl 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
764a3417885SIoana Ciornei 		/* Clear any pending interrupts */
765a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
766a3417885SIoana Ciornei 		if (err)
767a3417885SIoana Ciornei 			return err;
768a3417885SIoana Ciornei 
769e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
770e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
771e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
772e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_FAIL;
773e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
774e6e4a556SMartin Blumenstingl 
775e6e4a556SMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, value);
776a3417885SIoana Ciornei 	} else {
777a46bd63bSMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
778a3417885SIoana Ciornei 		if (err)
779a3417885SIoana Ciornei 			return err;
780a3417885SIoana Ciornei 
781a3417885SIoana Ciornei 		/* Clear any pending interrupts */
782a3417885SIoana Ciornei 		err = at803x_ack_interrupt(phydev);
783a3417885SIoana Ciornei 	}
78477a99394SZhao Qiang 
78577a99394SZhao Qiang 	return err;
78677a99394SZhao Qiang }
78777a99394SZhao Qiang 
78829773097SIoana Ciornei static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev)
78929773097SIoana Ciornei {
79029773097SIoana Ciornei 	int irq_status, int_enabled;
79129773097SIoana Ciornei 
79229773097SIoana Ciornei 	irq_status = phy_read(phydev, AT803X_INTR_STATUS);
79329773097SIoana Ciornei 	if (irq_status < 0) {
79429773097SIoana Ciornei 		phy_error(phydev);
79529773097SIoana Ciornei 		return IRQ_NONE;
79629773097SIoana Ciornei 	}
79729773097SIoana Ciornei 
79829773097SIoana Ciornei 	/* Read the current enabled interrupts */
79929773097SIoana Ciornei 	int_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
80029773097SIoana Ciornei 	if (int_enabled < 0) {
80129773097SIoana Ciornei 		phy_error(phydev);
80229773097SIoana Ciornei 		return IRQ_NONE;
80329773097SIoana Ciornei 	}
80429773097SIoana Ciornei 
80529773097SIoana Ciornei 	/* See if this was one of our enabled interrupts */
80629773097SIoana Ciornei 	if (!(irq_status & int_enabled))
80729773097SIoana Ciornei 		return IRQ_NONE;
80829773097SIoana Ciornei 
80929773097SIoana Ciornei 	phy_trigger_machine(phydev);
81029773097SIoana Ciornei 
81129773097SIoana Ciornei 	return IRQ_HANDLED;
81229773097SIoana Ciornei }
81329773097SIoana Ciornei 
81413a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev)
81513a56b44SDaniel Mack {
81613a56b44SDaniel Mack 	/*
81713a56b44SDaniel Mack 	 * Conduct a hardware reset for AT8030 every time a link loss is
81813a56b44SDaniel Mack 	 * signalled. This is necessary to circumvent a hardware bug that
81913a56b44SDaniel Mack 	 * occurs when the cable is unplugged while TX packets are pending
82013a56b44SDaniel Mack 	 * in the FIFO. In such cases, the FIFO enters an error mode it
82113a56b44SDaniel Mack 	 * cannot recover from by software.
82213a56b44SDaniel Mack 	 */
8236110ed2dSDavid Bauer 	if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
82413a56b44SDaniel Mack 		struct at803x_context context;
82513a56b44SDaniel Mack 
82613a56b44SDaniel Mack 		at803x_context_save(phydev, &context);
82713a56b44SDaniel Mack 
828bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 1);
82913a56b44SDaniel Mack 		msleep(1);
830bafbdd52SSergei Shtylyov 		phy_device_reset(phydev, 0);
831d57019d1SSergei Shtylyov 		msleep(1);
83213a56b44SDaniel Mack 
83313a56b44SDaniel Mack 		at803x_context_restore(phydev, &context);
83413a56b44SDaniel Mack 
8355c5f626bSHeiner Kallweit 		phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
83613a56b44SDaniel Mack 	}
83713a56b44SDaniel Mack }
83813a56b44SDaniel Mack 
83906d5f344SRussell King static int at803x_read_status(struct phy_device *phydev)
84006d5f344SRussell King {
84106d5f344SRussell King 	int ss, err, old_link = phydev->link;
84206d5f344SRussell King 
84306d5f344SRussell King 	/* Update the link, but return if there was an error */
84406d5f344SRussell King 	err = genphy_update_link(phydev);
84506d5f344SRussell King 	if (err)
84606d5f344SRussell King 		return err;
84706d5f344SRussell King 
84806d5f344SRussell King 	/* why bother the PHY if nothing can have changed */
84906d5f344SRussell King 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
85006d5f344SRussell King 		return 0;
85106d5f344SRussell King 
85206d5f344SRussell King 	phydev->speed = SPEED_UNKNOWN;
85306d5f344SRussell King 	phydev->duplex = DUPLEX_UNKNOWN;
85406d5f344SRussell King 	phydev->pause = 0;
85506d5f344SRussell King 	phydev->asym_pause = 0;
85606d5f344SRussell King 
85706d5f344SRussell King 	err = genphy_read_lpa(phydev);
85806d5f344SRussell King 	if (err < 0)
85906d5f344SRussell King 		return err;
86006d5f344SRussell King 
86106d5f344SRussell King 	/* Read the AT8035 PHY-Specific Status register, which indicates the
86206d5f344SRussell King 	 * speed and duplex that the PHY is actually using, irrespective of
86306d5f344SRussell King 	 * whether we are in autoneg mode or not.
86406d5f344SRussell King 	 */
86506d5f344SRussell King 	ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
86606d5f344SRussell King 	if (ss < 0)
86706d5f344SRussell King 		return ss;
86806d5f344SRussell King 
86906d5f344SRussell King 	if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
8707dce80c2SOleksij Rempel 		int sfc;
8717dce80c2SOleksij Rempel 
8727dce80c2SOleksij Rempel 		sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL);
8737dce80c2SOleksij Rempel 		if (sfc < 0)
8747dce80c2SOleksij Rempel 			return sfc;
8757dce80c2SOleksij Rempel 
87606d5f344SRussell King 		switch (ss & AT803X_SS_SPEED_MASK) {
87706d5f344SRussell King 		case AT803X_SS_SPEED_10:
87806d5f344SRussell King 			phydev->speed = SPEED_10;
87906d5f344SRussell King 			break;
88006d5f344SRussell King 		case AT803X_SS_SPEED_100:
88106d5f344SRussell King 			phydev->speed = SPEED_100;
88206d5f344SRussell King 			break;
88306d5f344SRussell King 		case AT803X_SS_SPEED_1000:
88406d5f344SRussell King 			phydev->speed = SPEED_1000;
88506d5f344SRussell King 			break;
88606d5f344SRussell King 		}
88706d5f344SRussell King 		if (ss & AT803X_SS_DUPLEX)
88806d5f344SRussell King 			phydev->duplex = DUPLEX_FULL;
88906d5f344SRussell King 		else
89006d5f344SRussell King 			phydev->duplex = DUPLEX_HALF;
8917dce80c2SOleksij Rempel 
89206d5f344SRussell King 		if (ss & AT803X_SS_MDIX)
89306d5f344SRussell King 			phydev->mdix = ETH_TP_MDI_X;
89406d5f344SRussell King 		else
89506d5f344SRussell King 			phydev->mdix = ETH_TP_MDI;
8967dce80c2SOleksij Rempel 
8977dce80c2SOleksij Rempel 		switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) {
8987dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDI:
8997dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI;
9007dce80c2SOleksij Rempel 			break;
9017dce80c2SOleksij Rempel 		case AT803X_SFC_MANUAL_MDIX:
9027dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_X;
9037dce80c2SOleksij Rempel 			break;
9047dce80c2SOleksij Rempel 		case AT803X_SFC_AUTOMATIC_CROSSOVER:
9057dce80c2SOleksij Rempel 			phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
9067dce80c2SOleksij Rempel 			break;
9077dce80c2SOleksij Rempel 		}
90806d5f344SRussell King 	}
90906d5f344SRussell King 
91006d5f344SRussell King 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
91106d5f344SRussell King 		phy_resolve_aneg_pause(phydev);
91206d5f344SRussell King 
91306d5f344SRussell King 	return 0;
91406d5f344SRussell King }
91506d5f344SRussell King 
9167dce80c2SOleksij Rempel static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl)
9177dce80c2SOleksij Rempel {
9187dce80c2SOleksij Rempel 	u16 val;
9197dce80c2SOleksij Rempel 
9207dce80c2SOleksij Rempel 	switch (ctrl) {
9217dce80c2SOleksij Rempel 	case ETH_TP_MDI:
9227dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDI;
9237dce80c2SOleksij Rempel 		break;
9247dce80c2SOleksij Rempel 	case ETH_TP_MDI_X:
9257dce80c2SOleksij Rempel 		val = AT803X_SFC_MANUAL_MDIX;
9267dce80c2SOleksij Rempel 		break;
9277dce80c2SOleksij Rempel 	case ETH_TP_MDI_AUTO:
9287dce80c2SOleksij Rempel 		val = AT803X_SFC_AUTOMATIC_CROSSOVER;
9297dce80c2SOleksij Rempel 		break;
9307dce80c2SOleksij Rempel 	default:
9317dce80c2SOleksij Rempel 		return 0;
9327dce80c2SOleksij Rempel 	}
9337dce80c2SOleksij Rempel 
9347dce80c2SOleksij Rempel 	return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL,
9357dce80c2SOleksij Rempel 			  AT803X_SFC_MDI_CROSSOVER_MODE_M,
9367dce80c2SOleksij Rempel 			  FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val));
9377dce80c2SOleksij Rempel }
9387dce80c2SOleksij Rempel 
9397dce80c2SOleksij Rempel static int at803x_config_aneg(struct phy_device *phydev)
9407dce80c2SOleksij Rempel {
9417dce80c2SOleksij Rempel 	int ret;
9427dce80c2SOleksij Rempel 
9437dce80c2SOleksij Rempel 	ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
9447dce80c2SOleksij Rempel 	if (ret < 0)
9457dce80c2SOleksij Rempel 		return ret;
9467dce80c2SOleksij Rempel 
9477dce80c2SOleksij Rempel 	/* Changes of the midx bits are disruptive to the normal operation;
9487dce80c2SOleksij Rempel 	 * therefore any changes to these registers must be followed by a
9497dce80c2SOleksij Rempel 	 * software reset to take effect.
9507dce80c2SOleksij Rempel 	 */
9517dce80c2SOleksij Rempel 	if (ret == 1) {
9527dce80c2SOleksij Rempel 		ret = genphy_soft_reset(phydev);
9537dce80c2SOleksij Rempel 		if (ret < 0)
9547dce80c2SOleksij Rempel 			return ret;
9557dce80c2SOleksij Rempel 	}
9567dce80c2SOleksij Rempel 
9577dce80c2SOleksij Rempel 	return genphy_config_aneg(phydev);
9587dce80c2SOleksij Rempel }
9597dce80c2SOleksij Rempel 
960cde0f4f8SMichael Walle static int at803x_get_downshift(struct phy_device *phydev, u8 *d)
961cde0f4f8SMichael Walle {
962cde0f4f8SMichael Walle 	int val;
963cde0f4f8SMichael Walle 
964cde0f4f8SMichael Walle 	val = phy_read(phydev, AT803X_SMART_SPEED);
965cde0f4f8SMichael Walle 	if (val < 0)
966cde0f4f8SMichael Walle 		return val;
967cde0f4f8SMichael Walle 
968cde0f4f8SMichael Walle 	if (val & AT803X_SMART_SPEED_ENABLE)
969cde0f4f8SMichael Walle 		*d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2;
970cde0f4f8SMichael Walle 	else
971cde0f4f8SMichael Walle 		*d = DOWNSHIFT_DEV_DISABLE;
972cde0f4f8SMichael Walle 
973cde0f4f8SMichael Walle 	return 0;
974cde0f4f8SMichael Walle }
975cde0f4f8SMichael Walle 
976cde0f4f8SMichael Walle static int at803x_set_downshift(struct phy_device *phydev, u8 cnt)
977cde0f4f8SMichael Walle {
978cde0f4f8SMichael Walle 	u16 mask, set;
979cde0f4f8SMichael Walle 	int ret;
980cde0f4f8SMichael Walle 
981cde0f4f8SMichael Walle 	switch (cnt) {
982cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DEFAULT_COUNT:
983cde0f4f8SMichael Walle 		cnt = AT803X_DEFAULT_DOWNSHIFT;
984cde0f4f8SMichael Walle 		fallthrough;
985cde0f4f8SMichael Walle 	case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT:
986cde0f4f8SMichael Walle 		set = AT803X_SMART_SPEED_ENABLE |
987cde0f4f8SMichael Walle 		      AT803X_SMART_SPEED_BYPASS_TIMER |
988cde0f4f8SMichael Walle 		      FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2);
989cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK;
990cde0f4f8SMichael Walle 		break;
991cde0f4f8SMichael Walle 	case DOWNSHIFT_DEV_DISABLE:
992cde0f4f8SMichael Walle 		set = 0;
993cde0f4f8SMichael Walle 		mask = AT803X_SMART_SPEED_ENABLE |
994cde0f4f8SMichael Walle 		       AT803X_SMART_SPEED_BYPASS_TIMER;
995cde0f4f8SMichael Walle 		break;
996cde0f4f8SMichael Walle 	default:
997cde0f4f8SMichael Walle 		return -EINVAL;
998cde0f4f8SMichael Walle 	}
999cde0f4f8SMichael Walle 
1000cde0f4f8SMichael Walle 	ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set);
1001cde0f4f8SMichael Walle 
1002cde0f4f8SMichael Walle 	/* After changing the smart speed settings, we need to perform a
1003cde0f4f8SMichael Walle 	 * software reset, use phy_init_hw() to make sure we set the
1004cde0f4f8SMichael Walle 	 * reapply any values which might got lost during software reset.
1005cde0f4f8SMichael Walle 	 */
1006cde0f4f8SMichael Walle 	if (ret == 1)
1007cde0f4f8SMichael Walle 		ret = phy_init_hw(phydev);
1008cde0f4f8SMichael Walle 
1009cde0f4f8SMichael Walle 	return ret;
1010cde0f4f8SMichael Walle }
1011cde0f4f8SMichael Walle 
1012cde0f4f8SMichael Walle static int at803x_get_tunable(struct phy_device *phydev,
1013cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, void *data)
1014cde0f4f8SMichael Walle {
1015cde0f4f8SMichael Walle 	switch (tuna->id) {
1016cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1017cde0f4f8SMichael Walle 		return at803x_get_downshift(phydev, data);
1018cde0f4f8SMichael Walle 	default:
1019cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1020cde0f4f8SMichael Walle 	}
1021cde0f4f8SMichael Walle }
1022cde0f4f8SMichael Walle 
1023cde0f4f8SMichael Walle static int at803x_set_tunable(struct phy_device *phydev,
1024cde0f4f8SMichael Walle 			      struct ethtool_tunable *tuna, const void *data)
1025cde0f4f8SMichael Walle {
1026cde0f4f8SMichael Walle 	switch (tuna->id) {
1027cde0f4f8SMichael Walle 	case ETHTOOL_PHY_DOWNSHIFT:
1028cde0f4f8SMichael Walle 		return at803x_set_downshift(phydev, *(const u8 *)data);
1029cde0f4f8SMichael Walle 	default:
1030cde0f4f8SMichael Walle 		return -EOPNOTSUPP;
1031cde0f4f8SMichael Walle 	}
1032cde0f4f8SMichael Walle }
1033cde0f4f8SMichael Walle 
10346cb75767SMichael Walle static int at803x_cable_test_result_trans(u16 status)
10356cb75767SMichael Walle {
10366cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
10376cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_NORMAL:
10386cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OK;
10396cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
10406cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
10416cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
10426cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
10436cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_FAIL:
10446cb75767SMichael Walle 	default:
10456cb75767SMichael Walle 		return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
10466cb75767SMichael Walle 	}
10476cb75767SMichael Walle }
10486cb75767SMichael Walle 
10496cb75767SMichael Walle static bool at803x_cdt_test_failed(u16 status)
10506cb75767SMichael Walle {
10516cb75767SMichael Walle 	return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
10526cb75767SMichael Walle 		AT803X_CDT_STATUS_STAT_FAIL;
10536cb75767SMichael Walle }
10546cb75767SMichael Walle 
10556cb75767SMichael Walle static bool at803x_cdt_fault_length_valid(u16 status)
10566cb75767SMichael Walle {
10576cb75767SMichael Walle 	switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
10586cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_OPEN:
10596cb75767SMichael Walle 	case AT803X_CDT_STATUS_STAT_SHORT:
10606cb75767SMichael Walle 		return true;
10616cb75767SMichael Walle 	}
10626cb75767SMichael Walle 	return false;
10636cb75767SMichael Walle }
10646cb75767SMichael Walle 
10656cb75767SMichael Walle static int at803x_cdt_fault_length(u16 status)
10666cb75767SMichael Walle {
10676cb75767SMichael Walle 	int dt;
10686cb75767SMichael Walle 
10696cb75767SMichael Walle 	/* According to the datasheet the distance to the fault is
10706cb75767SMichael Walle 	 * DELTA_TIME * 0.824 meters.
10716cb75767SMichael Walle 	 *
10726cb75767SMichael Walle 	 * The author suspect the correct formula is:
10736cb75767SMichael Walle 	 *
10746cb75767SMichael Walle 	 *   fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2
10756cb75767SMichael Walle 	 *
10766cb75767SMichael Walle 	 * where c is the speed of light, VF is the velocity factor of
10776cb75767SMichael Walle 	 * the twisted pair cable, 125MHz the counter frequency and
10786cb75767SMichael Walle 	 * we need to divide by 2 because the hardware will measure the
10796cb75767SMichael Walle 	 * round trip time to the fault and back to the PHY.
10806cb75767SMichael Walle 	 *
10816cb75767SMichael Walle 	 * With a VF of 0.69 we get the factor 0.824 mentioned in the
10826cb75767SMichael Walle 	 * datasheet.
10836cb75767SMichael Walle 	 */
10846cb75767SMichael Walle 	dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status);
10856cb75767SMichael Walle 
10866cb75767SMichael Walle 	return (dt * 824) / 10;
10876cb75767SMichael Walle }
10886cb75767SMichael Walle 
10896cb75767SMichael Walle static int at803x_cdt_start(struct phy_device *phydev, int pair)
10906cb75767SMichael Walle {
10916cb75767SMichael Walle 	u16 cdt;
10926cb75767SMichael Walle 
10936cb75767SMichael Walle 	cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
10946cb75767SMichael Walle 	      AT803X_CDT_ENABLE_TEST;
10956cb75767SMichael Walle 
10966cb75767SMichael Walle 	return phy_write(phydev, AT803X_CDT, cdt);
10976cb75767SMichael Walle }
10986cb75767SMichael Walle 
10996cb75767SMichael Walle static int at803x_cdt_wait_for_completion(struct phy_device *phydev)
11006cb75767SMichael Walle {
11016cb75767SMichael Walle 	int val, ret;
11026cb75767SMichael Walle 
11036cb75767SMichael Walle 	/* One test run takes about 25ms */
11046cb75767SMichael Walle 	ret = phy_read_poll_timeout(phydev, AT803X_CDT, val,
11056cb75767SMichael Walle 				    !(val & AT803X_CDT_ENABLE_TEST),
11066cb75767SMichael Walle 				    30000, 100000, true);
11076cb75767SMichael Walle 
11086cb75767SMichael Walle 	return ret < 0 ? ret : 0;
11096cb75767SMichael Walle }
11106cb75767SMichael Walle 
11116cb75767SMichael Walle static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
11126cb75767SMichael Walle {
11136cb75767SMichael Walle 	static const int ethtool_pair[] = {
11146cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_A,
11156cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_B,
11166cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_C,
11176cb75767SMichael Walle 		ETHTOOL_A_CABLE_PAIR_D,
11186cb75767SMichael Walle 	};
11196cb75767SMichael Walle 	int ret, val;
11206cb75767SMichael Walle 
11216cb75767SMichael Walle 	ret = at803x_cdt_start(phydev, pair);
11226cb75767SMichael Walle 	if (ret)
11236cb75767SMichael Walle 		return ret;
11246cb75767SMichael Walle 
11256cb75767SMichael Walle 	ret = at803x_cdt_wait_for_completion(phydev);
11266cb75767SMichael Walle 	if (ret)
11276cb75767SMichael Walle 		return ret;
11286cb75767SMichael Walle 
11296cb75767SMichael Walle 	val = phy_read(phydev, AT803X_CDT_STATUS);
11306cb75767SMichael Walle 	if (val < 0)
11316cb75767SMichael Walle 		return val;
11326cb75767SMichael Walle 
11336cb75767SMichael Walle 	if (at803x_cdt_test_failed(val))
11346cb75767SMichael Walle 		return 0;
11356cb75767SMichael Walle 
11366cb75767SMichael Walle 	ethnl_cable_test_result(phydev, ethtool_pair[pair],
11376cb75767SMichael Walle 				at803x_cable_test_result_trans(val));
11386cb75767SMichael Walle 
11396cb75767SMichael Walle 	if (at803x_cdt_fault_length_valid(val))
11406cb75767SMichael Walle 		ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
11416cb75767SMichael Walle 					      at803x_cdt_fault_length(val));
11426cb75767SMichael Walle 
11436cb75767SMichael Walle 	return 1;
11446cb75767SMichael Walle }
11456cb75767SMichael Walle 
11466cb75767SMichael Walle static int at803x_cable_test_get_status(struct phy_device *phydev,
11476cb75767SMichael Walle 					bool *finished)
11486cb75767SMichael Walle {
1149dc0f3ed1SOleksij Rempel 	unsigned long pair_mask;
11506cb75767SMichael Walle 	int retries = 20;
11516cb75767SMichael Walle 	int pair, ret;
11526cb75767SMichael Walle 
1153dc0f3ed1SOleksij Rempel 	if (phydev->phy_id == ATH9331_PHY_ID ||
1154dc0f3ed1SOleksij Rempel 	    phydev->phy_id == ATH8032_PHY_ID)
1155dc0f3ed1SOleksij Rempel 		pair_mask = 0x3;
1156dc0f3ed1SOleksij Rempel 	else
1157dc0f3ed1SOleksij Rempel 		pair_mask = 0xf;
1158dc0f3ed1SOleksij Rempel 
11596cb75767SMichael Walle 	*finished = false;
11606cb75767SMichael Walle 
11616cb75767SMichael Walle 	/* According to the datasheet the CDT can be performed when
11626cb75767SMichael Walle 	 * there is no link partner or when the link partner is
11636cb75767SMichael Walle 	 * auto-negotiating. Starting the test will restart the AN
11646cb75767SMichael Walle 	 * automatically. It seems that doing this repeatedly we will
11656cb75767SMichael Walle 	 * get a slot where our link partner won't disturb our
11666cb75767SMichael Walle 	 * measurement.
11676cb75767SMichael Walle 	 */
11686cb75767SMichael Walle 	while (pair_mask && retries--) {
11696cb75767SMichael Walle 		for_each_set_bit(pair, &pair_mask, 4) {
11706cb75767SMichael Walle 			ret = at803x_cable_test_one_pair(phydev, pair);
11716cb75767SMichael Walle 			if (ret < 0)
11726cb75767SMichael Walle 				return ret;
11736cb75767SMichael Walle 			if (ret)
11746cb75767SMichael Walle 				clear_bit(pair, &pair_mask);
11756cb75767SMichael Walle 		}
11766cb75767SMichael Walle 		if (pair_mask)
11776cb75767SMichael Walle 			msleep(250);
11786cb75767SMichael Walle 	}
11796cb75767SMichael Walle 
11806cb75767SMichael Walle 	*finished = true;
11816cb75767SMichael Walle 
11826cb75767SMichael Walle 	return 0;
11836cb75767SMichael Walle }
11846cb75767SMichael Walle 
11856cb75767SMichael Walle static int at803x_cable_test_start(struct phy_device *phydev)
11866cb75767SMichael Walle {
11876cb75767SMichael Walle 	/* Enable auto-negotiation, but advertise no capabilities, no link
11886cb75767SMichael Walle 	 * will be established. A restart of the auto-negotiation is not
11896cb75767SMichael Walle 	 * required, because the cable test will automatically break the link.
11906cb75767SMichael Walle 	 */
11916cb75767SMichael Walle 	phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
11926cb75767SMichael Walle 	phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
1193dc0f3ed1SOleksij Rempel 	if (phydev->phy_id != ATH9331_PHY_ID &&
1194dc0f3ed1SOleksij Rempel 	    phydev->phy_id != ATH8032_PHY_ID)
11956cb75767SMichael Walle 		phy_write(phydev, MII_CTRL1000, 0);
11966cb75767SMichael Walle 
11976cb75767SMichael Walle 	/* we do all the (time consuming) work later */
11986cb75767SMichael Walle 	return 0;
11996cb75767SMichael Walle }
12006cb75767SMichael Walle 
1201317420abSMugunthan V N static struct phy_driver at803x_driver[] = {
1202317420abSMugunthan V N {
120396c36712SMichael Walle 	/* Qualcomm Atheros AR8035 */
12040465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
120596c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8035",
12066cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
12072f664823SMichael Walle 	.probe			= at803x_probe,
12082318ca8aSMichael Walle 	.remove			= at803x_remove,
12097dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
12100ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
1211cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
1212ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
1213ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
12146229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
12156229ed1fSDaniel Mack 	.resume			= at803x_resume,
1216dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
121706d5f344SRussell King 	.read_status		= at803x_read_status,
12180eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
121929773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1220cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
1221cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
12226cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
12236cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
1224317420abSMugunthan V N }, {
122596c36712SMichael Walle 	/* Qualcomm Atheros AR8030 */
1226bd8ca17fSDaniel Mack 	.phy_id			= ATH8030_PHY_ID,
122796c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8030",
12280465d8f8SMichael Walle 	.phy_id_mask		= AT8030_PHY_ID_MASK,
12292f664823SMichael Walle 	.probe			= at803x_probe,
12302318ca8aSMichael Walle 	.remove			= at803x_remove,
12310ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
123213a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
1233ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
1234ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
12356229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
12366229ed1fSDaniel Mack 	.resume			= at803x_resume,
1237dcdecdcfSHeiner Kallweit 	/* PHY_BASIC_FEATURES */
12380eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
123929773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
124005d7cce8SMugunthan V N }, {
124196c36712SMichael Walle 	/* Qualcomm Atheros AR8031/AR8033 */
12420465d8f8SMichael Walle 	PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
124396c36712SMichael Walle 	.name			= "Qualcomm Atheros AR8031/AR8033",
12446cb75767SMichael Walle 	.flags			= PHY_POLL_CABLE_TEST,
12452f664823SMichael Walle 	.probe			= at803x_probe,
12462318ca8aSMichael Walle 	.remove			= at803x_remove,
124705d7cce8SMugunthan V N 	.config_init		= at803x_config_init,
124863477a5dSMichael Walle 	.config_aneg		= at803x_config_aneg,
1249cde0f4f8SMichael Walle 	.soft_reset		= genphy_soft_reset,
125005d7cce8SMugunthan V N 	.set_wol		= at803x_set_wol,
125105d7cce8SMugunthan V N 	.get_wol		= at803x_get_wol,
12526229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
12536229ed1fSDaniel Mack 	.resume			= at803x_resume,
1254c329e5afSDavid Bauer 	.read_page		= at803x_read_page,
1255c329e5afSDavid Bauer 	.write_page		= at803x_write_page,
1256*b856150cSDavid Bauer 	.get_features		= at803x_get_features,
125706d5f344SRussell King 	.read_status		= at803x_read_status,
125877a99394SZhao Qiang 	.config_intr		= &at803x_config_intr,
125929773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1260cde0f4f8SMichael Walle 	.get_tunable		= at803x_get_tunable,
1261cde0f4f8SMichael Walle 	.set_tunable		= at803x_set_tunable,
12626cb75767SMichael Walle 	.cable_test_start	= at803x_cable_test_start,
12636cb75767SMichael Walle 	.cable_test_get_status	= at803x_cable_test_get_status,
12647908d2ceSOleksij Rempel }, {
12655800091aSDavid Bauer 	/* Qualcomm Atheros AR8032 */
12665800091aSDavid Bauer 	PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
12675800091aSDavid Bauer 	.name			= "Qualcomm Atheros AR8032",
12685800091aSDavid Bauer 	.probe			= at803x_probe,
12695800091aSDavid Bauer 	.remove			= at803x_remove,
1270dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
12715800091aSDavid Bauer 	.config_init		= at803x_config_init,
12725800091aSDavid Bauer 	.link_change_notify	= at803x_link_change_notify,
12735800091aSDavid Bauer 	.set_wol		= at803x_set_wol,
12745800091aSDavid Bauer 	.get_wol		= at803x_get_wol,
12755800091aSDavid Bauer 	.suspend		= at803x_suspend,
12765800091aSDavid Bauer 	.resume			= at803x_resume,
12775800091aSDavid Bauer 	/* PHY_BASIC_FEATURES */
12785800091aSDavid Bauer 	.config_intr		= at803x_config_intr,
127929773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1280dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
1281dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
12825800091aSDavid Bauer }, {
12837908d2ceSOleksij Rempel 	/* ATHEROS AR9331 */
12847908d2ceSOleksij Rempel 	PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
128596c36712SMichael Walle 	.name			= "Qualcomm Atheros AR9331 built-in PHY",
12867908d2ceSOleksij Rempel 	.suspend		= at803x_suspend,
12877908d2ceSOleksij Rempel 	.resume			= at803x_resume,
1288dc0f3ed1SOleksij Rempel 	.flags			= PHY_POLL_CABLE_TEST,
12897908d2ceSOleksij Rempel 	/* PHY_BASIC_FEATURES */
12907908d2ceSOleksij Rempel 	.config_intr		= &at803x_config_intr,
129129773097SIoana Ciornei 	.handle_interrupt	= at803x_handle_interrupt,
1292dc0f3ed1SOleksij Rempel 	.cable_test_start	= at803x_cable_test_start,
1293dc0f3ed1SOleksij Rempel 	.cable_test_get_status	= at803x_cable_test_get_status,
12947dce80c2SOleksij Rempel 	.read_status		= at803x_read_status,
12957dce80c2SOleksij Rempel 	.soft_reset		= genphy_soft_reset,
12967dce80c2SOleksij Rempel 	.config_aneg		= at803x_config_aneg,
1297317420abSMugunthan V N } };
12980ca7111aSMatus Ujhelyi 
129950fd7150SJohan Hovold module_phy_driver(at803x_driver);
13000ca7111aSMatus Ujhelyi 
13010ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = {
13020465d8f8SMichael Walle 	{ ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
13030465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
13045800091aSDavid Bauer 	{ PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
13050465d8f8SMichael Walle 	{ PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
13067908d2ceSOleksij Rempel 	{ PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
13070ca7111aSMatus Ujhelyi 	{ }
13080ca7111aSMatus Ujhelyi };
13090ca7111aSMatus Ujhelyi 
13100ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl);
1311