xref: /openbmc/linux/drivers/net/phy/at803x.c (revision d57019d1)
10ca7111aSMatus Ujhelyi /*
20ca7111aSMatus Ujhelyi  * drivers/net/phy/at803x.c
30ca7111aSMatus Ujhelyi  *
40ca7111aSMatus Ujhelyi  * Driver for Atheros 803x PHY
50ca7111aSMatus Ujhelyi  *
60ca7111aSMatus Ujhelyi  * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
70ca7111aSMatus Ujhelyi  *
80ca7111aSMatus Ujhelyi  * This program is free software; you can redistribute  it and/or modify it
90ca7111aSMatus Ujhelyi  * under  the terms of  the GNU General  Public License as published by the
100ca7111aSMatus Ujhelyi  * Free Software Foundation;  either version 2 of the  License, or (at your
110ca7111aSMatus Ujhelyi  * option) any later version.
120ca7111aSMatus Ujhelyi  */
130ca7111aSMatus Ujhelyi 
140ca7111aSMatus Ujhelyi #include <linux/phy.h>
150ca7111aSMatus Ujhelyi #include <linux/module.h>
160ca7111aSMatus Ujhelyi #include <linux/string.h>
170ca7111aSMatus Ujhelyi #include <linux/netdevice.h>
180ca7111aSMatus Ujhelyi #include <linux/etherdevice.h>
1913a56b44SDaniel Mack #include <linux/of_gpio.h>
2013a56b44SDaniel Mack #include <linux/gpio/consumer.h>
210ca7111aSMatus Ujhelyi 
220ca7111aSMatus Ujhelyi #define AT803X_INTR_ENABLE			0x12
23e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_AUTONEG_ERR		BIT(15)
24e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_SPEED_CHANGED	BIT(14)
25e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_DUPLEX_CHANGED	BIT(13)
26e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_PAGE_RECEIVED	BIT(12)
27e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_FAIL		BIT(11)
28e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_LINK_SUCCESS		BIT(10)
29e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE	BIT(5)
30e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_POLARITY_CHANGED	BIT(1)
31e6e4a556SMartin Blumenstingl #define AT803X_INTR_ENABLE_WOL			BIT(0)
32e6e4a556SMartin Blumenstingl 
330ca7111aSMatus Ujhelyi #define AT803X_INTR_STATUS			0x13
34a46bd63bSMartin Blumenstingl 
3513a56b44SDaniel Mack #define AT803X_SMART_SPEED			0x14
3613a56b44SDaniel Mack #define AT803X_LED_CONTROL			0x18
37a46bd63bSMartin Blumenstingl 
380ca7111aSMatus Ujhelyi #define AT803X_DEVICE_ADDR			0x03
390ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_0_15_OFFSET		0x804C
400ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_16_31_OFFSET	0x804B
410ca7111aSMatus Ujhelyi #define AT803X_LOC_MAC_ADDR_32_47_OFFSET	0x804A
420ca7111aSMatus Ujhelyi #define AT803X_MMD_ACCESS_CONTROL		0x0D
430ca7111aSMatus Ujhelyi #define AT803X_MMD_ACCESS_CONTROL_DATA		0x0E
440ca7111aSMatus Ujhelyi #define AT803X_FUNC_DATA			0x4003
45a46bd63bSMartin Blumenstingl 
461ca6d1b1SMugunthan V N #define AT803X_DEBUG_ADDR			0x1D
471ca6d1b1SMugunthan V N #define AT803X_DEBUG_DATA			0x1E
48a46bd63bSMartin Blumenstingl 
492e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_REG_0			0x00
502e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_RX_CLK_DLY_EN		BIT(15)
51a46bd63bSMartin Blumenstingl 
522e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_REG_5			0x05
532e5f9f28SMartin Blumenstingl #define AT803X_DEBUG_TX_CLK_DLY_EN		BIT(8)
540ca7111aSMatus Ujhelyi 
5598267311SZefir Kurtisi #define AT803X_REG_CHIP_CONFIG			0x1f
5698267311SZefir Kurtisi #define AT803X_BT_BX_REG_SEL			0x8000
5798267311SZefir Kurtisi 
58bd8ca17fSDaniel Mack #define ATH8030_PHY_ID 0x004dd076
59bd8ca17fSDaniel Mack #define ATH8031_PHY_ID 0x004dd074
60bd8ca17fSDaniel Mack #define ATH8035_PHY_ID 0x004dd072
61bd8ca17fSDaniel Mack 
620ca7111aSMatus Ujhelyi MODULE_DESCRIPTION("Atheros 803x PHY driver");
630ca7111aSMatus Ujhelyi MODULE_AUTHOR("Matus Ujhelyi");
640ca7111aSMatus Ujhelyi MODULE_LICENSE("GPL");
650ca7111aSMatus Ujhelyi 
6613a56b44SDaniel Mack struct at803x_priv {
6713a56b44SDaniel Mack 	bool phy_reset:1;
6813a56b44SDaniel Mack 	struct gpio_desc *gpiod_reset;
6913a56b44SDaniel Mack };
7013a56b44SDaniel Mack 
7113a56b44SDaniel Mack struct at803x_context {
7213a56b44SDaniel Mack 	u16 bmcr;
7313a56b44SDaniel Mack 	u16 advertise;
7413a56b44SDaniel Mack 	u16 control1000;
7513a56b44SDaniel Mack 	u16 int_enable;
7613a56b44SDaniel Mack 	u16 smart_speed;
7713a56b44SDaniel Mack 	u16 led_control;
7813a56b44SDaniel Mack };
7913a56b44SDaniel Mack 
802e5f9f28SMartin Blumenstingl static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
812e5f9f28SMartin Blumenstingl {
822e5f9f28SMartin Blumenstingl 	int ret;
832e5f9f28SMartin Blumenstingl 
842e5f9f28SMartin Blumenstingl 	ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
852e5f9f28SMartin Blumenstingl 	if (ret < 0)
862e5f9f28SMartin Blumenstingl 		return ret;
872e5f9f28SMartin Blumenstingl 
882e5f9f28SMartin Blumenstingl 	return phy_read(phydev, AT803X_DEBUG_DATA);
892e5f9f28SMartin Blumenstingl }
902e5f9f28SMartin Blumenstingl 
912e5f9f28SMartin Blumenstingl static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
922e5f9f28SMartin Blumenstingl 				 u16 clear, u16 set)
932e5f9f28SMartin Blumenstingl {
942e5f9f28SMartin Blumenstingl 	u16 val;
952e5f9f28SMartin Blumenstingl 	int ret;
962e5f9f28SMartin Blumenstingl 
972e5f9f28SMartin Blumenstingl 	ret = at803x_debug_reg_read(phydev, reg);
982e5f9f28SMartin Blumenstingl 	if (ret < 0)
992e5f9f28SMartin Blumenstingl 		return ret;
1002e5f9f28SMartin Blumenstingl 
1012e5f9f28SMartin Blumenstingl 	val = ret & 0xffff;
1022e5f9f28SMartin Blumenstingl 	val &= ~clear;
1032e5f9f28SMartin Blumenstingl 	val |= set;
1042e5f9f28SMartin Blumenstingl 
1052e5f9f28SMartin Blumenstingl 	return phy_write(phydev, AT803X_DEBUG_DATA, val);
1062e5f9f28SMartin Blumenstingl }
1072e5f9f28SMartin Blumenstingl 
1082e5f9f28SMartin Blumenstingl static inline int at803x_enable_rx_delay(struct phy_device *phydev)
1092e5f9f28SMartin Blumenstingl {
1102e5f9f28SMartin Blumenstingl 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
1112e5f9f28SMartin Blumenstingl 					AT803X_DEBUG_RX_CLK_DLY_EN);
1122e5f9f28SMartin Blumenstingl }
1132e5f9f28SMartin Blumenstingl 
1142e5f9f28SMartin Blumenstingl static inline int at803x_enable_tx_delay(struct phy_device *phydev)
1152e5f9f28SMartin Blumenstingl {
1162e5f9f28SMartin Blumenstingl 	return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
1172e5f9f28SMartin Blumenstingl 					AT803X_DEBUG_TX_CLK_DLY_EN);
1182e5f9f28SMartin Blumenstingl }
1192e5f9f28SMartin Blumenstingl 
12013a56b44SDaniel Mack /* save relevant PHY registers to private copy */
12113a56b44SDaniel Mack static void at803x_context_save(struct phy_device *phydev,
12213a56b44SDaniel Mack 				struct at803x_context *context)
12313a56b44SDaniel Mack {
12413a56b44SDaniel Mack 	context->bmcr = phy_read(phydev, MII_BMCR);
12513a56b44SDaniel Mack 	context->advertise = phy_read(phydev, MII_ADVERTISE);
12613a56b44SDaniel Mack 	context->control1000 = phy_read(phydev, MII_CTRL1000);
12713a56b44SDaniel Mack 	context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
12813a56b44SDaniel Mack 	context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
12913a56b44SDaniel Mack 	context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
13013a56b44SDaniel Mack }
13113a56b44SDaniel Mack 
13213a56b44SDaniel Mack /* restore relevant PHY registers from private copy */
13313a56b44SDaniel Mack static void at803x_context_restore(struct phy_device *phydev,
13413a56b44SDaniel Mack 				   const struct at803x_context *context)
13513a56b44SDaniel Mack {
13613a56b44SDaniel Mack 	phy_write(phydev, MII_BMCR, context->bmcr);
13713a56b44SDaniel Mack 	phy_write(phydev, MII_ADVERTISE, context->advertise);
13813a56b44SDaniel Mack 	phy_write(phydev, MII_CTRL1000, context->control1000);
13913a56b44SDaniel Mack 	phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
14013a56b44SDaniel Mack 	phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
14113a56b44SDaniel Mack 	phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
14213a56b44SDaniel Mack }
14313a56b44SDaniel Mack 
144ea13c9eeSMugunthan V N static int at803x_set_wol(struct phy_device *phydev,
145ea13c9eeSMugunthan V N 			  struct ethtool_wolinfo *wol)
1460ca7111aSMatus Ujhelyi {
1470ca7111aSMatus Ujhelyi 	struct net_device *ndev = phydev->attached_dev;
1480ca7111aSMatus Ujhelyi 	const u8 *mac;
149ea13c9eeSMugunthan V N 	int ret;
150ea13c9eeSMugunthan V N 	u32 value;
1510ca7111aSMatus Ujhelyi 	unsigned int i, offsets[] = {
1520ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_32_47_OFFSET,
1530ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_16_31_OFFSET,
1540ca7111aSMatus Ujhelyi 		AT803X_LOC_MAC_ADDR_0_15_OFFSET,
1550ca7111aSMatus Ujhelyi 	};
1560ca7111aSMatus Ujhelyi 
1570ca7111aSMatus Ujhelyi 	if (!ndev)
158ea13c9eeSMugunthan V N 		return -ENODEV;
1590ca7111aSMatus Ujhelyi 
160ea13c9eeSMugunthan V N 	if (wol->wolopts & WAKE_MAGIC) {
1610ca7111aSMatus Ujhelyi 		mac = (const u8 *) ndev->dev_addr;
1620ca7111aSMatus Ujhelyi 
1630ca7111aSMatus Ujhelyi 		if (!is_valid_ether_addr(mac))
164ea13c9eeSMugunthan V N 			return -EFAULT;
1650ca7111aSMatus Ujhelyi 
1660ca7111aSMatus Ujhelyi 		for (i = 0; i < 3; i++) {
1670ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
1680ca7111aSMatus Ujhelyi 				  AT803X_DEVICE_ADDR);
1690ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
1700ca7111aSMatus Ujhelyi 				  offsets[i]);
1710ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL,
1720ca7111aSMatus Ujhelyi 				  AT803X_FUNC_DATA);
1730ca7111aSMatus Ujhelyi 			phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA,
1740ca7111aSMatus Ujhelyi 				  mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
1750ca7111aSMatus Ujhelyi 		}
176ea13c9eeSMugunthan V N 
177ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
178e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_WOL;
179ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
180ea13c9eeSMugunthan V N 		if (ret)
181ea13c9eeSMugunthan V N 			return ret;
182ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
183ea13c9eeSMugunthan V N 	} else {
184ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_ENABLE);
185e6e4a556SMartin Blumenstingl 		value &= (~AT803X_INTR_ENABLE_WOL);
186ea13c9eeSMugunthan V N 		ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
187ea13c9eeSMugunthan V N 		if (ret)
188ea13c9eeSMugunthan V N 			return ret;
189ea13c9eeSMugunthan V N 		value = phy_read(phydev, AT803X_INTR_STATUS);
190ea13c9eeSMugunthan V N 	}
191ea13c9eeSMugunthan V N 
192ea13c9eeSMugunthan V N 	return ret;
193ea13c9eeSMugunthan V N }
194ea13c9eeSMugunthan V N 
195ea13c9eeSMugunthan V N static void at803x_get_wol(struct phy_device *phydev,
196ea13c9eeSMugunthan V N 			   struct ethtool_wolinfo *wol)
197ea13c9eeSMugunthan V N {
198ea13c9eeSMugunthan V N 	u32 value;
199ea13c9eeSMugunthan V N 
200ea13c9eeSMugunthan V N 	wol->supported = WAKE_MAGIC;
201ea13c9eeSMugunthan V N 	wol->wolopts = 0;
202ea13c9eeSMugunthan V N 
203ea13c9eeSMugunthan V N 	value = phy_read(phydev, AT803X_INTR_ENABLE);
204e6e4a556SMartin Blumenstingl 	if (value & AT803X_INTR_ENABLE_WOL)
205ea13c9eeSMugunthan V N 		wol->wolopts |= WAKE_MAGIC;
2060ca7111aSMatus Ujhelyi }
2070ca7111aSMatus Ujhelyi 
2086229ed1fSDaniel Mack static int at803x_suspend(struct phy_device *phydev)
2096229ed1fSDaniel Mack {
2106229ed1fSDaniel Mack 	int value;
2116229ed1fSDaniel Mack 	int wol_enabled;
21298267311SZefir Kurtisi 	int ccr;
2136229ed1fSDaniel Mack 
2146229ed1fSDaniel Mack 	mutex_lock(&phydev->lock);
2156229ed1fSDaniel Mack 
2166229ed1fSDaniel Mack 	value = phy_read(phydev, AT803X_INTR_ENABLE);
217e6e4a556SMartin Blumenstingl 	wol_enabled = value & AT803X_INTR_ENABLE_WOL;
2186229ed1fSDaniel Mack 
2196229ed1fSDaniel Mack 	value = phy_read(phydev, MII_BMCR);
2206229ed1fSDaniel Mack 
2216229ed1fSDaniel Mack 	if (wol_enabled)
2226229ed1fSDaniel Mack 		value |= BMCR_ISOLATE;
2236229ed1fSDaniel Mack 	else
2246229ed1fSDaniel Mack 		value |= BMCR_PDOWN;
2256229ed1fSDaniel Mack 
2266229ed1fSDaniel Mack 	phy_write(phydev, MII_BMCR, value);
2276229ed1fSDaniel Mack 
22898267311SZefir Kurtisi 	if (phydev->interface != PHY_INTERFACE_MODE_SGMII)
22998267311SZefir Kurtisi 		goto done;
23098267311SZefir Kurtisi 
23198267311SZefir Kurtisi 	/* also power-down SGMII interface */
23298267311SZefir Kurtisi 	ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
23398267311SZefir Kurtisi 	phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL);
23498267311SZefir Kurtisi 	phy_write(phydev, MII_BMCR, phy_read(phydev, MII_BMCR) | BMCR_PDOWN);
23598267311SZefir Kurtisi 	phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL);
23698267311SZefir Kurtisi 
23798267311SZefir Kurtisi done:
2386229ed1fSDaniel Mack 	mutex_unlock(&phydev->lock);
2396229ed1fSDaniel Mack 
2406229ed1fSDaniel Mack 	return 0;
2416229ed1fSDaniel Mack }
2426229ed1fSDaniel Mack 
2436229ed1fSDaniel Mack static int at803x_resume(struct phy_device *phydev)
2446229ed1fSDaniel Mack {
2456229ed1fSDaniel Mack 	int value;
24698267311SZefir Kurtisi 	int ccr;
2476229ed1fSDaniel Mack 
2486229ed1fSDaniel Mack 	mutex_lock(&phydev->lock);
2496229ed1fSDaniel Mack 
2506229ed1fSDaniel Mack 	value = phy_read(phydev, MII_BMCR);
2516229ed1fSDaniel Mack 	value &= ~(BMCR_PDOWN | BMCR_ISOLATE);
2526229ed1fSDaniel Mack 	phy_write(phydev, MII_BMCR, value);
2536229ed1fSDaniel Mack 
25498267311SZefir Kurtisi 	if (phydev->interface != PHY_INTERFACE_MODE_SGMII)
25598267311SZefir Kurtisi 		goto done;
25698267311SZefir Kurtisi 
25798267311SZefir Kurtisi 	/* also power-up SGMII interface */
25898267311SZefir Kurtisi 	ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
25998267311SZefir Kurtisi 	phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL);
26098267311SZefir Kurtisi 	value = phy_read(phydev, MII_BMCR) & ~(BMCR_PDOWN | BMCR_ISOLATE);
26198267311SZefir Kurtisi 	phy_write(phydev, MII_BMCR, value);
26298267311SZefir Kurtisi 	phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL);
26398267311SZefir Kurtisi 
26498267311SZefir Kurtisi done:
2656229ed1fSDaniel Mack 	mutex_unlock(&phydev->lock);
2666229ed1fSDaniel Mack 
2676229ed1fSDaniel Mack 	return 0;
2686229ed1fSDaniel Mack }
2696229ed1fSDaniel Mack 
27013a56b44SDaniel Mack static int at803x_probe(struct phy_device *phydev)
27113a56b44SDaniel Mack {
272e5a03bfdSAndrew Lunn 	struct device *dev = &phydev->mdio.dev;
27313a56b44SDaniel Mack 	struct at803x_priv *priv;
274687908c2SUwe Kleine-König 	struct gpio_desc *gpiod_reset;
27513a56b44SDaniel Mack 
2768f2877caSFengguang Wu 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
27713a56b44SDaniel Mack 	if (!priv)
27813a56b44SDaniel Mack 		return -ENOMEM;
27913a56b44SDaniel Mack 
280d57019d1SSergei Shtylyov 	gpiod_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
281687908c2SUwe Kleine-König 	if (IS_ERR(gpiod_reset))
282687908c2SUwe Kleine-König 		return PTR_ERR(gpiod_reset);
283687908c2SUwe Kleine-König 
284687908c2SUwe Kleine-König 	priv->gpiod_reset = gpiod_reset;
28513a56b44SDaniel Mack 
28613a56b44SDaniel Mack 	phydev->priv = priv;
28713a56b44SDaniel Mack 
28813a56b44SDaniel Mack 	return 0;
28913a56b44SDaniel Mack }
29013a56b44SDaniel Mack 
2910ca7111aSMatus Ujhelyi static int at803x_config_init(struct phy_device *phydev)
2920ca7111aSMatus Ujhelyi {
2931ca6d1b1SMugunthan V N 	int ret;
2940ca7111aSMatus Ujhelyi 
2956ff01dbbSDaniel Mack 	ret = genphy_config_init(phydev);
2966ff01dbbSDaniel Mack 	if (ret < 0)
2976ff01dbbSDaniel Mack 		return ret;
2980ca7111aSMatus Ujhelyi 
2992e5f9f28SMartin Blumenstingl 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID ||
3002e5f9f28SMartin Blumenstingl 			phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
3012e5f9f28SMartin Blumenstingl 		ret = at803x_enable_rx_delay(phydev);
3022e5f9f28SMartin Blumenstingl 		if (ret < 0)
3031ca6d1b1SMugunthan V N 			return ret;
3042e5f9f28SMartin Blumenstingl 	}
3052e5f9f28SMartin Blumenstingl 
3062e5f9f28SMartin Blumenstingl 	if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID ||
3072e5f9f28SMartin Blumenstingl 			phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
3082e5f9f28SMartin Blumenstingl 		ret = at803x_enable_tx_delay(phydev);
3092e5f9f28SMartin Blumenstingl 		if (ret < 0)
3101ca6d1b1SMugunthan V N 			return ret;
3111ca6d1b1SMugunthan V N 	}
3121ca6d1b1SMugunthan V N 
3130ca7111aSMatus Ujhelyi 	return 0;
3140ca7111aSMatus Ujhelyi }
3150ca7111aSMatus Ujhelyi 
31677a99394SZhao Qiang static int at803x_ack_interrupt(struct phy_device *phydev)
31777a99394SZhao Qiang {
31877a99394SZhao Qiang 	int err;
31977a99394SZhao Qiang 
320a46bd63bSMartin Blumenstingl 	err = phy_read(phydev, AT803X_INTR_STATUS);
32177a99394SZhao Qiang 
32277a99394SZhao Qiang 	return (err < 0) ? err : 0;
32377a99394SZhao Qiang }
32477a99394SZhao Qiang 
32577a99394SZhao Qiang static int at803x_config_intr(struct phy_device *phydev)
32677a99394SZhao Qiang {
32777a99394SZhao Qiang 	int err;
32877a99394SZhao Qiang 	int value;
32977a99394SZhao Qiang 
330a46bd63bSMartin Blumenstingl 	value = phy_read(phydev, AT803X_INTR_ENABLE);
33177a99394SZhao Qiang 
332e6e4a556SMartin Blumenstingl 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
333e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
334e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
335e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
336e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_FAIL;
337e6e4a556SMartin Blumenstingl 		value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
338e6e4a556SMartin Blumenstingl 
339e6e4a556SMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, value);
340e6e4a556SMartin Blumenstingl 	}
34177a99394SZhao Qiang 	else
342a46bd63bSMartin Blumenstingl 		err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
34377a99394SZhao Qiang 
34477a99394SZhao Qiang 	return err;
34577a99394SZhao Qiang }
34677a99394SZhao Qiang 
34713a56b44SDaniel Mack static void at803x_link_change_notify(struct phy_device *phydev)
34813a56b44SDaniel Mack {
34913a56b44SDaniel Mack 	struct at803x_priv *priv = phydev->priv;
35013a56b44SDaniel Mack 
35113a56b44SDaniel Mack 	/*
35213a56b44SDaniel Mack 	 * Conduct a hardware reset for AT8030 every time a link loss is
35313a56b44SDaniel Mack 	 * signalled. This is necessary to circumvent a hardware bug that
35413a56b44SDaniel Mack 	 * occurs when the cable is unplugged while TX packets are pending
35513a56b44SDaniel Mack 	 * in the FIFO. In such cases, the FIFO enters an error mode it
35613a56b44SDaniel Mack 	 * cannot recover from by software.
35713a56b44SDaniel Mack 	 */
35813a56b44SDaniel Mack 	if (phydev->drv->phy_id == ATH8030_PHY_ID) {
35913a56b44SDaniel Mack 		if (phydev->state == PHY_NOLINK) {
36013a56b44SDaniel Mack 			if (priv->gpiod_reset && !priv->phy_reset) {
36113a56b44SDaniel Mack 				struct at803x_context context;
36213a56b44SDaniel Mack 
36313a56b44SDaniel Mack 				at803x_context_save(phydev, &context);
36413a56b44SDaniel Mack 
36513a56b44SDaniel Mack 				gpiod_set_value(priv->gpiod_reset, 1);
36613a56b44SDaniel Mack 				msleep(1);
367d57019d1SSergei Shtylyov 				gpiod_set_value(priv->gpiod_reset, 0);
368d57019d1SSergei Shtylyov 				msleep(1);
36913a56b44SDaniel Mack 
37013a56b44SDaniel Mack 				at803x_context_restore(phydev, &context);
37113a56b44SDaniel Mack 
37272ba48beSAndrew Lunn 				phydev_dbg(phydev, "%s(): phy was reset\n",
37313a56b44SDaniel Mack 					   __func__);
37413a56b44SDaniel Mack 				priv->phy_reset = true;
37513a56b44SDaniel Mack 			}
37613a56b44SDaniel Mack 		} else {
37713a56b44SDaniel Mack 			priv->phy_reset = false;
37813a56b44SDaniel Mack 		}
37913a56b44SDaniel Mack 	}
38013a56b44SDaniel Mack }
38113a56b44SDaniel Mack 
382317420abSMugunthan V N static struct phy_driver at803x_driver[] = {
383317420abSMugunthan V N {
3840ca7111aSMatus Ujhelyi 	/* ATHEROS 8035 */
385bd8ca17fSDaniel Mack 	.phy_id			= ATH8035_PHY_ID,
3860ca7111aSMatus Ujhelyi 	.name			= "Atheros 8035 ethernet",
3870ca7111aSMatus Ujhelyi 	.phy_id_mask		= 0xffffffef,
38813a56b44SDaniel Mack 	.probe			= at803x_probe,
3890ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
39013a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
391ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
392ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
3936229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
3946229ed1fSDaniel Mack 	.resume			= at803x_resume,
3950ca7111aSMatus Ujhelyi 	.features		= PHY_GBIT_FEATURES,
3960ca7111aSMatus Ujhelyi 	.flags			= PHY_HAS_INTERRUPT,
3970197ffedSDaniel Mack 	.config_aneg		= genphy_config_aneg,
3980197ffedSDaniel Mack 	.read_status		= genphy_read_status,
3990eae5982SMåns Rullgård 	.ack_interrupt		= at803x_ack_interrupt,
4000eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
401317420abSMugunthan V N }, {
4020ca7111aSMatus Ujhelyi 	/* ATHEROS 8030 */
403bd8ca17fSDaniel Mack 	.phy_id			= ATH8030_PHY_ID,
4040ca7111aSMatus Ujhelyi 	.name			= "Atheros 8030 ethernet",
4050ca7111aSMatus Ujhelyi 	.phy_id_mask		= 0xffffffef,
40613a56b44SDaniel Mack 	.probe			= at803x_probe,
4070ca7111aSMatus Ujhelyi 	.config_init		= at803x_config_init,
40813a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
409ea13c9eeSMugunthan V N 	.set_wol		= at803x_set_wol,
410ea13c9eeSMugunthan V N 	.get_wol		= at803x_get_wol,
4116229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
4126229ed1fSDaniel Mack 	.resume			= at803x_resume,
413e15bb4c6SMartin Blumenstingl 	.features		= PHY_BASIC_FEATURES,
4140ca7111aSMatus Ujhelyi 	.flags			= PHY_HAS_INTERRUPT,
4150197ffedSDaniel Mack 	.config_aneg		= genphy_config_aneg,
4160197ffedSDaniel Mack 	.read_status		= genphy_read_status,
4170eae5982SMåns Rullgård 	.ack_interrupt		= at803x_ack_interrupt,
4180eae5982SMåns Rullgård 	.config_intr		= at803x_config_intr,
41905d7cce8SMugunthan V N }, {
42005d7cce8SMugunthan V N 	/* ATHEROS 8031 */
421bd8ca17fSDaniel Mack 	.phy_id			= ATH8031_PHY_ID,
42205d7cce8SMugunthan V N 	.name			= "Atheros 8031 ethernet",
42305d7cce8SMugunthan V N 	.phy_id_mask		= 0xffffffef,
42413a56b44SDaniel Mack 	.probe			= at803x_probe,
42505d7cce8SMugunthan V N 	.config_init		= at803x_config_init,
42613a56b44SDaniel Mack 	.link_change_notify	= at803x_link_change_notify,
42705d7cce8SMugunthan V N 	.set_wol		= at803x_set_wol,
42805d7cce8SMugunthan V N 	.get_wol		= at803x_get_wol,
4296229ed1fSDaniel Mack 	.suspend		= at803x_suspend,
4306229ed1fSDaniel Mack 	.resume			= at803x_resume,
43105d7cce8SMugunthan V N 	.features		= PHY_GBIT_FEATURES,
43205d7cce8SMugunthan V N 	.flags			= PHY_HAS_INTERRUPT,
4330197ffedSDaniel Mack 	.config_aneg		= genphy_config_aneg,
4340197ffedSDaniel Mack 	.read_status		= genphy_read_status,
43577a99394SZhao Qiang 	.ack_interrupt		= &at803x_ack_interrupt,
43677a99394SZhao Qiang 	.config_intr		= &at803x_config_intr,
437317420abSMugunthan V N } };
4380ca7111aSMatus Ujhelyi 
43950fd7150SJohan Hovold module_phy_driver(at803x_driver);
4400ca7111aSMatus Ujhelyi 
4410ca7111aSMatus Ujhelyi static struct mdio_device_id __maybe_unused atheros_tbl[] = {
442bd8ca17fSDaniel Mack 	{ ATH8030_PHY_ID, 0xffffffef },
443bd8ca17fSDaniel Mack 	{ ATH8031_PHY_ID, 0xffffffef },
444bd8ca17fSDaniel Mack 	{ ATH8035_PHY_ID, 0xffffffef },
4450ca7111aSMatus Ujhelyi 	{ }
4460ca7111aSMatus Ujhelyi };
4470ca7111aSMatus Ujhelyi 
4480ca7111aSMatus Ujhelyi MODULE_DEVICE_TABLE(mdio, atheros_tbl);
449