xref: /openbmc/linux/drivers/net/phy/at803x.c (revision a2443fd1)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
20ca7111aSMatus Ujhelyi /*
30ca7111aSMatus Ujhelyi  * drivers/net/phy/at803x.c
40ca7111aSMatus Ujhelyi  *
50ca7111aSMatus Ujhelyi  * Driver for Atheros 803x 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>
1513a56b44SDaniel Mack #include <linux/of_gpio.h>
1613a56b44SDaniel Mack #include <linux/gpio/consumer.h>
170ca7111aSMatus Ujhelyi 
180ca7111aSMatus Ujhelyi #define AT803X_INTR_ENABLE			0x12
19e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_AUTONEG_ERR		BIT(15)
20e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_SPEED_CHANGED	BIT(14)
21e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_DUPLEX_CHANGED	BIT(13)
22e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_PAGE_RECEIVED	BIT(12)
23e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_FAIL		BIT(11)
24e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_SUCCESS		BIT(10)
25e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE	BIT(5)
26e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_POLARITY_CHANGED	BIT(1)
27e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WOL			BIT(0)
28e6e4a556SMartin Blumenstingl 
290ca7111aSMatus Ujhelyi #define AT803X_INTR_STATUS			0x13
30a46bd63bSMartin Blumenstingl 
3113a56b44SDaniel Mack #define AT803X_SMART_SPEED			0x14
3213a56b44SDaniel Mack #define AT803X_LED_CONTROL			0x18
33a46bd63bSMartin Blumenstingl 
340ca7111aSMatus Ujhelyi #define AT803X_DEVICE_ADDR			0x03
350ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_0_15_OFFSET		0x804C
360ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_16_31_OFFSET	0x804B
370ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_32_47_OFFSET	0x804A
380ca7111aSMatus Ujhelyi #define AT803X_MMD_ACCESS_CONTROL		0x0D
390ca7111aSMatus Ujhelyi #define AT803X_MMD_ACCESS_CONTROL_DATA		0x0E
400ca7111aSMatus Ujhelyi #define AT803X_FUNC_DATA			0x4003
41f62265b5SZefir Kurtisi #define AT803X_REG_CHIP_CONFIG			0x1f
42f62265b5SZefir Kurtisi #define AT803X_BT_BX_REG_SEL			0x8000
43a46bd63bSMartin Blumenstingl 
441ca6d1b1SMugunthan V N #define AT803X_DEBUG_ADDR			0x1D
451ca6d1b1SMugunthan V N #define AT803X_DEBUG_DATA			0x1E
46a46bd63bSMartin Blumenstingl 
47f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_MASK			0x0F
48f62265b5SZefir Kurtisi #define AT803X_MODE_CFG_SGMII			0x01
49f62265b5SZefir Kurtisi 
50f62265b5SZefir Kurtisi #define AT803X_PSSR			0x11	/*PHY-Specific Status Register*/
51f62265b5SZefir Kurtisi #define AT803X_PSSR_MR_AN_COMPLETE	0x0200
52f62265b5SZefir Kurtisi 
532e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_REG_0			0x00
542e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_RX_CLK_DLY_EN		BIT(15)
55a46bd63bSMartin Blumenstingl 
562e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_REG_5			0x05
572e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_TX_CLK_DLY_EN		BIT(8)
580ca7111aSMatus Ujhelyi 
59bd8ca17fSDaniel Mack #define ATH8030_PHY_ID 0x004dd076
60bd8ca17fSDaniel Mack #define ATH8031_PHY_ID 0x004dd074
61bd8ca17fSDaniel Mack #define ATH8035_PHY_ID 0x004dd072
6258effd71SFabio Estevam #define AT803X_PHY_ID_MASK			0xffffffef
63bd8ca17fSDaniel Mack 
640ca7111aSMatus Ujhelyi MODULE_DESCRIPTION("Atheros 803x PHY driver");
650ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi");
660ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL");
670ca7111aSMatus Ujhelyi 
6813a56b44SDaniel Mack struct at803x_priv {
6913a56b44SDaniel Mack 	bool phy_reset:1;
7013a56b44SDaniel Mack };
7113a56b44SDaniel Mack 
7213a56b44SDaniel Mack struct at803x_context {
7313a56b44SDaniel Mack 	u16 bmcr;
7413a56b44SDaniel Mack 	u16 advertise;
7513a56b44SDaniel Mack 	u16 control1000;
7613a56b44SDaniel Mack 	u16 int_enable;
7713a56b44SDaniel Mack 	u16 smart_speed;
7813a56b44SDaniel Mack 	u16 led_control;
7913a56b44SDaniel Mack };
8013a56b44SDaniel Mack 
812e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
822e5f9f28SMartin Blumenstingl {
832e5f9f28SMartin Blumenstingl 	int ret;
842e5f9f28SMartin Blumenstingl 
852e5f9f28SMartin Blumenstingl 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
862e5f9f28SMartin Blumenstingl 	if (ret < 0)
872e5f9f28SMartin Blumenstingl 		return ret;
882e5f9f28SMartin Blumenstingl 
892e5f9f28SMartin Blumenstingl 	return phy_read(phydev, AT803X_DEBUG_DATA);
902e5f9f28SMartin Blumenstingl }
912e5f9f28SMartin Blumenstingl 
922e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
932e5f9f28SMartin Blumenstingl 				 u16 clear, u16 set)
942e5f9f28SMartin Blumenstingl {
952e5f9f28SMartin Blumenstingl 	u16 val;
962e5f9f28SMartin Blumenstingl 	int ret;
972e5f9f28SMartin Blumenstingl 
982e5f9f28SMartin Blumenstingl 	ret = at803x_debug_reg_read(phydev, reg);
992e5f9f28SMartin Blumenstingl 	if (ret < 0)
1002e5f9f28SMartin Blumenstingl 		return ret;
1012e5f9f28SMartin Blumenstingl 
1022e5f9f28SMartin Blumenstingl 	val = ret & 0xffff;
1032e5f9f28SMartin Blumenstingl 	val &= ~clear;
1042e5f9f28SMartin Blumenstingl 	val |= set;
1052e5f9f28SMartin Blumenstingl 
1062e5f9f28SMartin Blumenstingl 	return phy_write(phydev, AT803X_DEBUG_DATA, val);
1072e5f9f28SMartin Blumenstingl }
1082e5f9f28SMartin Blumenstingl 
109cd28d1d6SVinod Koul static inline int at803x_disable_rx_delay(struct phy_device *phydev)
1102e5f9f28SMartin Blumenstingl {
111cd28d1d6SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0,
112cd28d1d6SVinod Koul 				     AT803X_DEBUG_RX_CLK_DLY_EN, 0);
1132e5f9f28SMartin Blumenstingl }
1142e5f9f28SMartin Blumenstingl 
115cd28d1d6SVinod Koul static inline int at803x_disable_tx_delay(struct phy_device *phydev)
1162e5f9f28SMartin Blumenstingl {
117cd28d1d6SVinod Koul 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5,
118cd28d1d6SVinod Koul 				     AT803X_DEBUG_TX_CLK_DLY_EN, 0);
1192e5f9f28SMartin Blumenstingl }
1202e5f9f28SMartin Blumenstingl 
12113a56b44SDaniel Mack /* save relevant PHY registers to private copy */
12213a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev,
12313a56b44SDaniel Mack 				struct at803x_context *context)
12413a56b44SDaniel Mack {
12513a56b44SDaniel Mack 	context->bmcr = phy_read(phydev, MII_BMCR);
12613a56b44SDaniel Mack 	context->advertise = phy_read(phydev, MII_ADVERTISE);
12713a56b44SDaniel Mack 	context->control1000 = phy_read(phydev, MII_CTRL1000);
12813a56b44SDaniel Mack 	context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
12913a56b44SDaniel Mack 	context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
13013a56b44SDaniel Mack 	context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
13113a56b44SDaniel Mack }
13213a56b44SDaniel Mack 
13313a56b44SDaniel Mack /* restore relevant PHY registers from private copy */
13413a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev,
13513a56b44SDaniel Mack 				   const struct at803x_context *context)
13613a56b44SDaniel Mack {
13713a56b44SDaniel Mack 	phy_write(phydev, MII_BMCR, context->bmcr);
13813a56b44SDaniel Mack 	phy_write(phydev, MII_ADVERTISE, context->advertise);
13913a56b44SDaniel Mack 	phy_write(phydev, MII_CTRL1000, context->control1000);
14013a56b44SDaniel Mack 	phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
14113a56b44SDaniel Mack 	phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
14213a56b44SDaniel Mack 	phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
14313a56b44SDaniel Mack }
14413a56b44SDaniel Mack 
145ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev,
146ea13c9eeSMugunthan V N 			  struct ethtool_wolinfo *wol)
1470ca7111aSMatus Ujhelyi {
1480ca7111aSMatus Ujhelyi 	struct net_device *ndev = phydev->attached_dev;
1490ca7111aSMatus Ujhelyi 	const u8 *mac;
150ea13c9eeSMugunthan V N 	int ret;
151ea13c9eeSMugunthan V N 	u32 value;
1520ca7111aSMatus Ujhelyi 	unsigned int i, offsets[] = {
1530ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_32_47_OFFSET,
1540ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_16_31_OFFSET,
1550ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_0_15_OFFSET,
1560ca7111aSMatus Ujhelyi 	};
1570ca7111aSMatus Ujhelyi 
1580ca7111aSMatus Ujhelyi 	if (!ndev)
159ea13c9eeSMugunthan V N 		return -ENODEV;
1600ca7111aSMatus Ujhelyi 
161ea13c9eeSMugunthan V N 	if (wol->wolopts & WAKE_MAGIC) {
1620ca7111aSMatus Ujhelyi 		mac = (const u8 *) ndev->dev_addr;
1630ca7111aSMatus Ujhelyi 
1640ca7111aSMatus Ujhelyi 		if (!is_valid_ether_addr(mac))
165fc755687SDan Murphy 			return -EINVAL;
1660ca7111aSMatus Ujhelyi 
1670ca7111aSMatus Ujhelyi 		for (i = 0; i < 3; i++) {
1680ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
1690ca7111aSMatus Ujhelyi 				  AT803X_DEVICE_ADDR);
1700ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
1710ca7111aSMatus Ujhelyi 				  offsets[i]);
1720ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
1730ca7111aSMatus Ujhelyi 				  AT803X_FUNC_DATA);
1740ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
1750ca7111aSMatus Ujhelyi 				  mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
1760ca7111aSMatus Ujhelyi 		}
177ea13c9eeSMugunthan V N 
178ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
179e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_WOL;
180ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
181ea13c9eeSMugunthan V N 		if (ret)
182ea13c9eeSMugunthan V N 			return ret;
183ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
184ea13c9eeSMugunthan V N 	} else {
185ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
186e6e4a556SMartin Blumenstingl 		value &= (~AT803X_INTR_ENABLE_WOL);
187ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
188ea13c9eeSMugunthan V N 		if (ret)
189ea13c9eeSMugunthan V N 			return ret;
190ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
191ea13c9eeSMugunthan V N 	}
192ea13c9eeSMugunthan V N 
193ea13c9eeSMugunthan V N 	return ret;
194ea13c9eeSMugunthan V N }
195ea13c9eeSMugunthan V N 
196ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev,
197ea13c9eeSMugunthan V N 			   struct ethtool_wolinfo *wol)
198ea13c9eeSMugunthan V N {
199ea13c9eeSMugunthan V N 	u32 value;
200ea13c9eeSMugunthan V N 
201ea13c9eeSMugunthan V N 	wol->supported = WAKE_MAGIC;
202ea13c9eeSMugunthan V N 	wol->wolopts = 0;
203ea13c9eeSMugunthan V N 
204ea13c9eeSMugunthan V N 	value = phy_read(phydev, AT803X_INTR_ENABLE);
205e6e4a556SMartin Blumenstingl 	if (value & AT803X_INTR_ENABLE_WOL)
206ea13c9eeSMugunthan V N 		wol->wolopts |= WAKE_MAGIC;
2070ca7111aSMatus Ujhelyi }
2080ca7111aSMatus Ujhelyi 
2096229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev)
2106229ed1fSDaniel Mack {
2116229ed1fSDaniel Mack 	int value;
2126229ed1fSDaniel Mack 	int wol_enabled;
2136229ed1fSDaniel Mack 
2146229ed1fSDaniel Mack 	value = phy_read(phydev, AT803X_INTR_ENABLE);
215e6e4a556SMartin Blumenstingl 	wol_enabled = value & AT803X_INTR_ENABLE_WOL;
2166229ed1fSDaniel Mack 
2176229ed1fSDaniel Mack 	if (wol_enabled)
218fea23fb5SRussell King 		value = BMCR_ISOLATE;
2196229ed1fSDaniel Mack 	else
220fea23fb5SRussell King 		value = BMCR_PDOWN;
2216229ed1fSDaniel Mack 
222fea23fb5SRussell King 	phy_modify(phydev, MII_BMCR, 0, value);
2236229ed1fSDaniel Mack 
2246229ed1fSDaniel Mack 	return 0;
2256229ed1fSDaniel Mack }
2266229ed1fSDaniel Mack 
2276229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev)
2286229ed1fSDaniel Mack {
229f102852fSRussell King 	return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
2306229ed1fSDaniel Mack }
2316229ed1fSDaniel Mack 
23213a56b44SDaniel Mack static int at803x_probe(struct phy_device *phydev)
23313a56b44SDaniel Mack {
234e5a03bfdSAndrew Lunn 	struct device *dev = &phydev->mdio.dev;
23513a56b44SDaniel Mack 	struct at803x_priv *priv;
23613a56b44SDaniel Mack 
2378f2877caSFengguang Wu 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
23813a56b44SDaniel Mack 	if (!priv)
23913a56b44SDaniel Mack 		return -ENOMEM;
24013a56b44SDaniel Mack 
24113a56b44SDaniel Mack 	phydev->priv = priv;
24213a56b44SDaniel Mack 
24313a56b44SDaniel Mack 	return 0;
24413a56b44SDaniel Mack }
24513a56b44SDaniel Mack 
2460ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev)
2470ca7111aSMatus Ujhelyi {
2481ca6d1b1SMugunthan V N 	int ret;
2490ca7111aSMatus Ujhelyi 
2506ff01dbbSDaniel Mack 	ret = genphy_config_init(phydev);
2516ff01dbbSDaniel Mack 	if (ret < 0)
2526ff01dbbSDaniel Mack 		return ret;
2530ca7111aSMatus Ujhelyi 
2542e5f9f28SMartin Blumenstingl 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID ||
255cd28d1d6SVinod Koul 			phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
256cd28d1d6SVinod Koul 			phydev->interface == PHY_INTERFACE_MODE_RGMII) {
257cd28d1d6SVinod Koul 		ret = at803x_disable_rx_delay(phydev);
2582e5f9f28SMartin Blumenstingl 		if (ret < 0)
2591ca6d1b1SMugunthan V N 			return ret;
2602e5f9f28SMartin Blumenstingl 	}
2612e5f9f28SMartin Blumenstingl 
2622e5f9f28SMartin Blumenstingl 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID ||
263cd28d1d6SVinod Koul 			phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
264cd28d1d6SVinod Koul 			phydev->interface == PHY_INTERFACE_MODE_RGMII) {
265cd28d1d6SVinod Koul 		ret = at803x_disable_tx_delay(phydev);
2662e5f9f28SMartin Blumenstingl 		if (ret < 0)
2671ca6d1b1SMugunthan V N 			return ret;
2681ca6d1b1SMugunthan V N 	}
2691ca6d1b1SMugunthan V N 
2700ca7111aSMatus Ujhelyi 	return 0;
2710ca7111aSMatus Ujhelyi }
2720ca7111aSMatus Ujhelyi 
27377a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev)
27477a99394SZhao Qiang {
27577a99394SZhao Qiang 	int err;
27677a99394SZhao Qiang 
277a46bd63bSMartin Blumenstingl 	err = phy_read(phydev, AT803X_INTR_STATUS);
27877a99394SZhao Qiang 
27977a99394SZhao Qiang 	return (err < 0) ? err : 0;
28077a99394SZhao Qiang }
28177a99394SZhao Qiang 
28277a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev)
28377a99394SZhao Qiang {
28477a99394SZhao Qiang 	int err;
28577a99394SZhao Qiang 	int value;
28677a99394SZhao Qiang 
287a46bd63bSMartin Blumenstingl 	value = phy_read(phydev, AT803X_INTR_ENABLE);
28877a99394SZhao Qiang 
289e6e4a556SMartin Blumenstingl 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
290e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
291e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
292e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
293e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_FAIL;
294e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
295e6e4a556SMartin Blumenstingl 
296e6e4a556SMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, value);
297e6e4a556SMartin Blumenstingl 	}
29877a99394SZhao Qiang 	else
299a46bd63bSMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
30077a99394SZhao Qiang 
30177a99394SZhao Qiang 	return err;
30277a99394SZhao Qiang }
30377a99394SZhao Qiang 
30413a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev)
30513a56b44SDaniel Mack {
30613a56b44SDaniel Mack 	struct at803x_priv *priv = phydev->priv;
30713a56b44SDaniel Mack 
30813a56b44SDaniel Mack 	/*
30913a56b44SDaniel Mack 	 * Conduct a hardware reset for AT8030 every time a link loss is
31013a56b44SDaniel Mack 	 * signalled. This is necessary to circumvent a hardware bug that
31113a56b44SDaniel Mack 	 * occurs when the cable is unplugged while TX packets are pending
31213a56b44SDaniel Mack 	 * in the FIFO. In such cases, the FIFO enters an error mode it
31313a56b44SDaniel Mack 	 * cannot recover from by software.
31413a56b44SDaniel Mack 	 */
31513a56b44SDaniel Mack 	if (phydev->state == PHY_NOLINK) {
316bafbdd52SSergei Shtylyov 		if (phydev->mdio.reset && !priv->phy_reset) {
31713a56b44SDaniel Mack 			struct at803x_context context;
31813a56b44SDaniel Mack 
31913a56b44SDaniel Mack 			at803x_context_save(phydev, &context);
32013a56b44SDaniel Mack 
321bafbdd52SSergei Shtylyov 			phy_device_reset(phydev, 1);
32213a56b44SDaniel Mack 			msleep(1);
323bafbdd52SSergei Shtylyov 			phy_device_reset(phydev, 0);
324d57019d1SSergei Shtylyov 			msleep(1);
32513a56b44SDaniel Mack 
32613a56b44SDaniel Mack 			at803x_context_restore(phydev, &context);
32713a56b44SDaniel Mack 
32872ba48beSAndrew Lunn 			phydev_dbg(phydev, "%s(): phy was reset\n",
32913a56b44SDaniel Mack 				   __func__);
33013a56b44SDaniel Mack 			priv->phy_reset = true;
33113a56b44SDaniel Mack 		}
33213a56b44SDaniel Mack 	} else {
33313a56b44SDaniel Mack 		priv->phy_reset = false;
33413a56b44SDaniel Mack 	}
33513a56b44SDaniel Mack }
33613a56b44SDaniel Mack 
337f62265b5SZefir Kurtisi static int at803x_aneg_done(struct phy_device *phydev)
338f62265b5SZefir Kurtisi {
339f62265b5SZefir Kurtisi 	int ccr;
340f62265b5SZefir Kurtisi 
341f62265b5SZefir Kurtisi 	int aneg_done = genphy_aneg_done(phydev);
342f62265b5SZefir Kurtisi 	if (aneg_done != BMSR_ANEGCOMPLETE)
343f62265b5SZefir Kurtisi 		return aneg_done;
344f62265b5SZefir Kurtisi 
345f62265b5SZefir Kurtisi 	/*
346f62265b5SZefir Kurtisi 	 * in SGMII mode, if copper side autoneg is successful,
347f62265b5SZefir Kurtisi 	 * also check SGMII side autoneg result
348f62265b5SZefir Kurtisi 	 */
349f62265b5SZefir Kurtisi 	ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
350f62265b5SZefir Kurtisi 	if ((ccr & AT803X_MODE_CFG_MASK) != AT803X_MODE_CFG_SGMII)
351f62265b5SZefir Kurtisi 		return aneg_done;
352f62265b5SZefir Kurtisi 
353f62265b5SZefir Kurtisi 	/* switch to SGMII/fiber page */
354f62265b5SZefir Kurtisi 	phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL);
355f62265b5SZefir Kurtisi 
356f62265b5SZefir Kurtisi 	/* check if the SGMII link is OK. */
357f62265b5SZefir Kurtisi 	if (!(phy_read(phydev, AT803X_PSSR) & AT803X_PSSR_MR_AN_COMPLETE)) {
358ab2a605fSAndrew Lunn 		phydev_warn(phydev, "803x_aneg_done: SGMII link is not ok\n");
359f62265b5SZefir Kurtisi 		aneg_done = 0;
360f62265b5SZefir Kurtisi 	}
361f62265b5SZefir Kurtisi 	/* switch back to copper page */
362f62265b5SZefir Kurtisi 	phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL);
363f62265b5SZefir Kurtisi 
364f62265b5SZefir Kurtisi 	return aneg_done;
365f62265b5SZefir Kurtisi }
366f62265b5SZefir Kurtisi 
367317420abSMugunthan V N static struct phy_driver at803x_driver[] = {
368317420abSMugunthan V N {
3690ca7111aSMatus Ujhelyi 	/* ATHEROS 8035 */
370bd8ca17fSDaniel Mack 	.phy_id			= ATH8035_PHY_ID,
3710ca7111aSMatus Ujhelyi 	.name			= "Atheros 8035 ethernet",
37258effd71SFabio Estevam 	.phy_id_mask		= AT803X_PHY_ID_MASK,
37313a56b44SDaniel Mack 	.probe			= at803x_probe,
3740ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
375ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
376ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
3776229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
3786229ed1fSDaniel Mack 	.resume			= at803x_resume,
3790ca7111aSMatus Ujhelyi 	.features		= PHY_GBIT_FEATURES,
3800eae5982SMåns Rullgård 	.ack_interrupt		= at803x_ack_interrupt,
3810eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
382317420abSMugunthan V N }, {
3830ca7111aSMatus Ujhelyi 	/* ATHEROS 8030 */
384bd8ca17fSDaniel Mack 	.phy_id			= ATH8030_PHY_ID,
3850ca7111aSMatus Ujhelyi 	.name			= "Atheros 8030 ethernet",
38658effd71SFabio Estevam 	.phy_id_mask		= AT803X_PHY_ID_MASK,
38713a56b44SDaniel Mack 	.probe			= at803x_probe,
3880ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
38913a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
390ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
391ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
3926229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
3936229ed1fSDaniel Mack 	.resume			= at803x_resume,
394e15bb4c6SMartin Blumenstingl 	.features		= PHY_BASIC_FEATURES,
3950eae5982SMåns Rullgård 	.ack_interrupt		= at803x_ack_interrupt,
3960eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
39705d7cce8SMugunthan V N }, {
39805d7cce8SMugunthan V N 	/* ATHEROS 8031 */
399bd8ca17fSDaniel Mack 	.phy_id			= ATH8031_PHY_ID,
40005d7cce8SMugunthan V N 	.name			= "Atheros 8031 ethernet",
40158effd71SFabio Estevam 	.phy_id_mask		= AT803X_PHY_ID_MASK,
40213a56b44SDaniel Mack 	.probe			= at803x_probe,
40305d7cce8SMugunthan V N 	.config_init		= at803x_config_init,
40405d7cce8SMugunthan V N 	.set_wol		= at803x_set_wol,
40505d7cce8SMugunthan V N 	.get_wol		= at803x_get_wol,
4066229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
4076229ed1fSDaniel Mack 	.resume			= at803x_resume,
40805d7cce8SMugunthan V N 	.features		= PHY_GBIT_FEATURES,
409f62265b5SZefir Kurtisi 	.aneg_done		= at803x_aneg_done,
41077a99394SZhao Qiang 	.ack_interrupt		= &at803x_ack_interrupt,
41177a99394SZhao Qiang 	.config_intr		= &at803x_config_intr,
412317420abSMugunthan V N } };
4130ca7111aSMatus Ujhelyi 
41450fd7150SJohan Hovold module_phy_driver(at803x_driver);
4150ca7111aSMatus Ujhelyi 
4160ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = {
41758effd71SFabio Estevam 	{ ATH8030_PHY_ID, AT803X_PHY_ID_MASK },
41858effd71SFabio Estevam 	{ ATH8031_PHY_ID, AT803X_PHY_ID_MASK },
41958effd71SFabio Estevam 	{ ATH8035_PHY_ID, AT803X_PHY_ID_MASK },
4200ca7111aSMatus Ujhelyi 	{ }
4210ca7111aSMatus Ujhelyi };
4220ca7111aSMatus Ujhelyi 
4230ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl);
424