Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/phy/at803x.c |
| 3 | * |
| 4 | * Driver for Atheros 803x PHY |
| 5 | * |
| 6 | * Author: Matus Ujhelyi <ujhelyi.m@gmail.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/phy.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | #include <linux/etherdevice.h> |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 19 | #include <linux/of_gpio.h> |
| 20 | #include <linux/gpio/consumer.h> |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 21 | |
| 22 | #define AT803X_INTR_ENABLE 0x12 |
Martin Blumenstingl | e6e4a55 | 2016-01-15 01:55:24 +0100 | [diff] [blame] | 23 | #define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15) |
| 24 | #define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14) |
| 25 | #define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13) |
| 26 | #define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12) |
| 27 | #define AT803X_INTR_ENABLE_LINK_FAIL BIT(11) |
| 28 | #define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10) |
| 29 | #define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5) |
| 30 | #define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1) |
| 31 | #define AT803X_INTR_ENABLE_WOL BIT(0) |
| 32 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 33 | #define AT803X_INTR_STATUS 0x13 |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 34 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 35 | #define AT803X_SMART_SPEED 0x14 |
| 36 | #define AT803X_LED_CONTROL 0x18 |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 37 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 38 | #define AT803X_DEVICE_ADDR 0x03 |
| 39 | #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C |
| 40 | #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B |
| 41 | #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A |
| 42 | #define AT803X_MMD_ACCESS_CONTROL 0x0D |
| 43 | #define AT803X_MMD_ACCESS_CONTROL_DATA 0x0E |
| 44 | #define AT803X_FUNC_DATA 0x4003 |
Zefir Kurtisi | f62265b | 2016-10-24 12:40:54 +0200 | [diff] [blame] | 45 | #define AT803X_REG_CHIP_CONFIG 0x1f |
| 46 | #define AT803X_BT_BX_REG_SEL 0x8000 |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 47 | |
Mugunthan V N | 1ca6d1b | 2013-06-03 20:10:06 +0000 | [diff] [blame] | 48 | #define AT803X_DEBUG_ADDR 0x1D |
| 49 | #define AT803X_DEBUG_DATA 0x1E |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 50 | |
Zefir Kurtisi | f62265b | 2016-10-24 12:40:54 +0200 | [diff] [blame] | 51 | #define AT803X_MODE_CFG_MASK 0x0F |
| 52 | #define AT803X_MODE_CFG_SGMII 0x01 |
| 53 | |
| 54 | #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/ |
| 55 | #define AT803X_PSSR_MR_AN_COMPLETE 0x0200 |
| 56 | |
Martin Blumenstingl | 2e5f9f2 | 2016-01-15 01:55:22 +0100 | [diff] [blame] | 57 | #define AT803X_DEBUG_REG_0 0x00 |
| 58 | #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15) |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 59 | |
Martin Blumenstingl | 2e5f9f2 | 2016-01-15 01:55:22 +0100 | [diff] [blame] | 60 | #define AT803X_DEBUG_REG_5 0x05 |
| 61 | #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8) |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 62 | |
Daniel Mack | bd8ca17 | 2014-06-18 11:01:42 +0200 | [diff] [blame] | 63 | #define ATH8030_PHY_ID 0x004dd076 |
| 64 | #define ATH8031_PHY_ID 0x004dd074 |
| 65 | #define ATH8035_PHY_ID 0x004dd072 |
Fabio Estevam | 58effd7 | 2016-10-26 14:03:54 -0200 | [diff] [blame] | 66 | #define AT803X_PHY_ID_MASK 0xffffffef |
Daniel Mack | bd8ca17 | 2014-06-18 11:01:42 +0200 | [diff] [blame] | 67 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 68 | MODULE_DESCRIPTION("Atheros 803x PHY driver"); |
| 69 | MODULE_AUTHOR("Matus Ujhelyi"); |
| 70 | MODULE_LICENSE("GPL"); |
| 71 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 72 | struct at803x_priv { |
| 73 | bool phy_reset:1; |
| 74 | struct gpio_desc *gpiod_reset; |
| 75 | }; |
| 76 | |
| 77 | struct at803x_context { |
| 78 | u16 bmcr; |
| 79 | u16 advertise; |
| 80 | u16 control1000; |
| 81 | u16 int_enable; |
| 82 | u16 smart_speed; |
| 83 | u16 led_control; |
| 84 | }; |
| 85 | |
Martin Blumenstingl | 2e5f9f2 | 2016-01-15 01:55:22 +0100 | [diff] [blame] | 86 | static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg) |
| 87 | { |
| 88 | int ret; |
| 89 | |
| 90 | ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg); |
| 91 | if (ret < 0) |
| 92 | return ret; |
| 93 | |
| 94 | return phy_read(phydev, AT803X_DEBUG_DATA); |
| 95 | } |
| 96 | |
| 97 | static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg, |
| 98 | u16 clear, u16 set) |
| 99 | { |
| 100 | u16 val; |
| 101 | int ret; |
| 102 | |
| 103 | ret = at803x_debug_reg_read(phydev, reg); |
| 104 | if (ret < 0) |
| 105 | return ret; |
| 106 | |
| 107 | val = ret & 0xffff; |
| 108 | val &= ~clear; |
| 109 | val |= set; |
| 110 | |
| 111 | return phy_write(phydev, AT803X_DEBUG_DATA, val); |
| 112 | } |
| 113 | |
| 114 | static inline int at803x_enable_rx_delay(struct phy_device *phydev) |
| 115 | { |
| 116 | return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0, |
| 117 | AT803X_DEBUG_RX_CLK_DLY_EN); |
| 118 | } |
| 119 | |
| 120 | static inline int at803x_enable_tx_delay(struct phy_device *phydev) |
| 121 | { |
| 122 | return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0, |
| 123 | AT803X_DEBUG_TX_CLK_DLY_EN); |
| 124 | } |
| 125 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 126 | /* save relevant PHY registers to private copy */ |
| 127 | static void at803x_context_save(struct phy_device *phydev, |
| 128 | struct at803x_context *context) |
| 129 | { |
| 130 | context->bmcr = phy_read(phydev, MII_BMCR); |
| 131 | context->advertise = phy_read(phydev, MII_ADVERTISE); |
| 132 | context->control1000 = phy_read(phydev, MII_CTRL1000); |
| 133 | context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE); |
| 134 | context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED); |
| 135 | context->led_control = phy_read(phydev, AT803X_LED_CONTROL); |
| 136 | } |
| 137 | |
| 138 | /* restore relevant PHY registers from private copy */ |
| 139 | static void at803x_context_restore(struct phy_device *phydev, |
| 140 | const struct at803x_context *context) |
| 141 | { |
| 142 | phy_write(phydev, MII_BMCR, context->bmcr); |
| 143 | phy_write(phydev, MII_ADVERTISE, context->advertise); |
| 144 | phy_write(phydev, MII_CTRL1000, context->control1000); |
| 145 | phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable); |
| 146 | phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed); |
| 147 | phy_write(phydev, AT803X_LED_CONTROL, context->led_control); |
| 148 | } |
| 149 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 150 | static int at803x_set_wol(struct phy_device *phydev, |
| 151 | struct ethtool_wolinfo *wol) |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 152 | { |
| 153 | struct net_device *ndev = phydev->attached_dev; |
| 154 | const u8 *mac; |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 155 | int ret; |
| 156 | u32 value; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 157 | unsigned int i, offsets[] = { |
| 158 | AT803X_LOC_MAC_ADDR_32_47_OFFSET, |
| 159 | AT803X_LOC_MAC_ADDR_16_31_OFFSET, |
| 160 | AT803X_LOC_MAC_ADDR_0_15_OFFSET, |
| 161 | }; |
| 162 | |
| 163 | if (!ndev) |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 164 | return -ENODEV; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 165 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 166 | if (wol->wolopts & WAKE_MAGIC) { |
| 167 | mac = (const u8 *) ndev->dev_addr; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 168 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 169 | if (!is_valid_ether_addr(mac)) |
Dan Murphy | fc75568 | 2017-10-10 12:42:56 -0500 | [diff] [blame^] | 170 | return -EINVAL; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 171 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 172 | for (i = 0; i < 3; i++) { |
| 173 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 174 | AT803X_DEVICE_ADDR); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 175 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 176 | offsets[i]); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 177 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 178 | AT803X_FUNC_DATA); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 179 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 180 | mac[(i * 2) + 1] | (mac[(i * 2)] << 8)); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
Martin Blumenstingl | e6e4a55 | 2016-01-15 01:55:24 +0100 | [diff] [blame] | 184 | value |= AT803X_INTR_ENABLE_WOL; |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 185 | ret = phy_write(phydev, AT803X_INTR_ENABLE, value); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | value = phy_read(phydev, AT803X_INTR_STATUS); |
| 189 | } else { |
| 190 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
Martin Blumenstingl | e6e4a55 | 2016-01-15 01:55:24 +0100 | [diff] [blame] | 191 | value &= (~AT803X_INTR_ENABLE_WOL); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 192 | ret = phy_write(phydev, AT803X_INTR_ENABLE, value); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | value = phy_read(phydev, AT803X_INTR_STATUS); |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 196 | } |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static void at803x_get_wol(struct phy_device *phydev, |
| 202 | struct ethtool_wolinfo *wol) |
| 203 | { |
| 204 | u32 value; |
| 205 | |
| 206 | wol->supported = WAKE_MAGIC; |
| 207 | wol->wolopts = 0; |
| 208 | |
| 209 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
Martin Blumenstingl | e6e4a55 | 2016-01-15 01:55:24 +0100 | [diff] [blame] | 210 | if (value & AT803X_INTR_ENABLE_WOL) |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 211 | wol->wolopts |= WAKE_MAGIC; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Daniel Mack | 6229ed1 | 2013-09-21 16:53:02 +0200 | [diff] [blame] | 214 | static int at803x_suspend(struct phy_device *phydev) |
| 215 | { |
| 216 | int value; |
| 217 | int wol_enabled; |
| 218 | |
| 219 | mutex_lock(&phydev->lock); |
| 220 | |
| 221 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
Martin Blumenstingl | e6e4a55 | 2016-01-15 01:55:24 +0100 | [diff] [blame] | 222 | wol_enabled = value & AT803X_INTR_ENABLE_WOL; |
Daniel Mack | 6229ed1 | 2013-09-21 16:53:02 +0200 | [diff] [blame] | 223 | |
| 224 | value = phy_read(phydev, MII_BMCR); |
| 225 | |
| 226 | if (wol_enabled) |
| 227 | value |= BMCR_ISOLATE; |
| 228 | else |
| 229 | value |= BMCR_PDOWN; |
| 230 | |
| 231 | phy_write(phydev, MII_BMCR, value); |
| 232 | |
| 233 | mutex_unlock(&phydev->lock); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int at803x_resume(struct phy_device *phydev) |
| 239 | { |
| 240 | int value; |
| 241 | |
| 242 | mutex_lock(&phydev->lock); |
| 243 | |
| 244 | value = phy_read(phydev, MII_BMCR); |
| 245 | value &= ~(BMCR_PDOWN | BMCR_ISOLATE); |
| 246 | phy_write(phydev, MII_BMCR, value); |
| 247 | |
| 248 | mutex_unlock(&phydev->lock); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 253 | static int at803x_probe(struct phy_device *phydev) |
| 254 | { |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 255 | struct device *dev = &phydev->mdio.dev; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 256 | struct at803x_priv *priv; |
Uwe Kleine-König | 687908c | 2015-03-31 22:08:45 +0200 | [diff] [blame] | 257 | struct gpio_desc *gpiod_reset; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 258 | |
Fengguang Wu | 8f2877c | 2014-06-22 12:32:51 +0200 | [diff] [blame] | 259 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 260 | if (!priv) |
| 261 | return -ENOMEM; |
| 262 | |
Sebastian Frias | 9eb13f6 | 2016-03-23 11:49:09 +0100 | [diff] [blame] | 263 | if (phydev->drv->phy_id != ATH8030_PHY_ID) |
| 264 | goto does_not_require_reset_workaround; |
| 265 | |
Sergei Shtylyov | d57019d | 2016-03-23 00:44:40 +0300 | [diff] [blame] | 266 | gpiod_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); |
Uwe Kleine-König | 687908c | 2015-03-31 22:08:45 +0200 | [diff] [blame] | 267 | if (IS_ERR(gpiod_reset)) |
| 268 | return PTR_ERR(gpiod_reset); |
| 269 | |
| 270 | priv->gpiod_reset = gpiod_reset; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 271 | |
Sebastian Frias | 9eb13f6 | 2016-03-23 11:49:09 +0100 | [diff] [blame] | 272 | does_not_require_reset_workaround: |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 273 | phydev->priv = priv; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 278 | static int at803x_config_init(struct phy_device *phydev) |
| 279 | { |
Mugunthan V N | 1ca6d1b | 2013-06-03 20:10:06 +0000 | [diff] [blame] | 280 | int ret; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 281 | |
Daniel Mack | 6ff01db | 2014-04-16 17:19:13 +0200 | [diff] [blame] | 282 | ret = genphy_config_init(phydev); |
| 283 | if (ret < 0) |
| 284 | return ret; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 285 | |
Martin Blumenstingl | 2e5f9f2 | 2016-01-15 01:55:22 +0100 | [diff] [blame] | 286 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID || |
| 287 | phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) { |
| 288 | ret = at803x_enable_rx_delay(phydev); |
| 289 | if (ret < 0) |
Mugunthan V N | 1ca6d1b | 2013-06-03 20:10:06 +0000 | [diff] [blame] | 290 | return ret; |
Martin Blumenstingl | 2e5f9f2 | 2016-01-15 01:55:22 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID || |
| 294 | phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) { |
| 295 | ret = at803x_enable_tx_delay(phydev); |
| 296 | if (ret < 0) |
Mugunthan V N | 1ca6d1b | 2013-06-03 20:10:06 +0000 | [diff] [blame] | 297 | return ret; |
| 298 | } |
| 299 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
Zhao Qiang | 77a9939 | 2014-03-28 15:39:41 +0800 | [diff] [blame] | 303 | static int at803x_ack_interrupt(struct phy_device *phydev) |
| 304 | { |
| 305 | int err; |
| 306 | |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 307 | err = phy_read(phydev, AT803X_INTR_STATUS); |
Zhao Qiang | 77a9939 | 2014-03-28 15:39:41 +0800 | [diff] [blame] | 308 | |
| 309 | return (err < 0) ? err : 0; |
| 310 | } |
| 311 | |
| 312 | static int at803x_config_intr(struct phy_device *phydev) |
| 313 | { |
| 314 | int err; |
| 315 | int value; |
| 316 | |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 317 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
Zhao Qiang | 77a9939 | 2014-03-28 15:39:41 +0800 | [diff] [blame] | 318 | |
Martin Blumenstingl | e6e4a55 | 2016-01-15 01:55:24 +0100 | [diff] [blame] | 319 | if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { |
| 320 | value |= AT803X_INTR_ENABLE_AUTONEG_ERR; |
| 321 | value |= AT803X_INTR_ENABLE_SPEED_CHANGED; |
| 322 | value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED; |
| 323 | value |= AT803X_INTR_ENABLE_LINK_FAIL; |
| 324 | value |= AT803X_INTR_ENABLE_LINK_SUCCESS; |
| 325 | |
| 326 | err = phy_write(phydev, AT803X_INTR_ENABLE, value); |
| 327 | } |
Zhao Qiang | 77a9939 | 2014-03-28 15:39:41 +0800 | [diff] [blame] | 328 | else |
Martin Blumenstingl | a46bd63 | 2016-01-15 01:55:23 +0100 | [diff] [blame] | 329 | err = phy_write(phydev, AT803X_INTR_ENABLE, 0); |
Zhao Qiang | 77a9939 | 2014-03-28 15:39:41 +0800 | [diff] [blame] | 330 | |
| 331 | return err; |
| 332 | } |
| 333 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 334 | static void at803x_link_change_notify(struct phy_device *phydev) |
| 335 | { |
| 336 | struct at803x_priv *priv = phydev->priv; |
| 337 | |
| 338 | /* |
| 339 | * Conduct a hardware reset for AT8030 every time a link loss is |
| 340 | * signalled. This is necessary to circumvent a hardware bug that |
| 341 | * occurs when the cable is unplugged while TX packets are pending |
| 342 | * in the FIFO. In such cases, the FIFO enters an error mode it |
| 343 | * cannot recover from by software. |
| 344 | */ |
Timur Tabi | a05d7df | 2016-04-26 12:44:18 -0500 | [diff] [blame] | 345 | if (phydev->state == PHY_NOLINK) { |
| 346 | if (priv->gpiod_reset && !priv->phy_reset) { |
| 347 | struct at803x_context context; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 348 | |
Timur Tabi | a05d7df | 2016-04-26 12:44:18 -0500 | [diff] [blame] | 349 | at803x_context_save(phydev, &context); |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 350 | |
Timur Tabi | a05d7df | 2016-04-26 12:44:18 -0500 | [diff] [blame] | 351 | gpiod_set_value(priv->gpiod_reset, 1); |
| 352 | msleep(1); |
| 353 | gpiod_set_value(priv->gpiod_reset, 0); |
| 354 | msleep(1); |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 355 | |
Timur Tabi | a05d7df | 2016-04-26 12:44:18 -0500 | [diff] [blame] | 356 | at803x_context_restore(phydev, &context); |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 357 | |
Timur Tabi | a05d7df | 2016-04-26 12:44:18 -0500 | [diff] [blame] | 358 | phydev_dbg(phydev, "%s(): phy was reset\n", |
| 359 | __func__); |
| 360 | priv->phy_reset = true; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 361 | } |
Timur Tabi | a05d7df | 2016-04-26 12:44:18 -0500 | [diff] [blame] | 362 | } else { |
| 363 | priv->phy_reset = false; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Zefir Kurtisi | f62265b | 2016-10-24 12:40:54 +0200 | [diff] [blame] | 367 | static int at803x_aneg_done(struct phy_device *phydev) |
| 368 | { |
| 369 | int ccr; |
| 370 | |
| 371 | int aneg_done = genphy_aneg_done(phydev); |
| 372 | if (aneg_done != BMSR_ANEGCOMPLETE) |
| 373 | return aneg_done; |
| 374 | |
| 375 | /* |
| 376 | * in SGMII mode, if copper side autoneg is successful, |
| 377 | * also check SGMII side autoneg result |
| 378 | */ |
| 379 | ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG); |
| 380 | if ((ccr & AT803X_MODE_CFG_MASK) != AT803X_MODE_CFG_SGMII) |
| 381 | return aneg_done; |
| 382 | |
| 383 | /* switch to SGMII/fiber page */ |
| 384 | phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL); |
| 385 | |
| 386 | /* check if the SGMII link is OK. */ |
| 387 | if (!(phy_read(phydev, AT803X_PSSR) & AT803X_PSSR_MR_AN_COMPLETE)) { |
| 388 | pr_warn("803x_aneg_done: SGMII link is not ok\n"); |
| 389 | aneg_done = 0; |
| 390 | } |
| 391 | /* switch back to copper page */ |
| 392 | phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL); |
| 393 | |
| 394 | return aneg_done; |
| 395 | } |
| 396 | |
Mugunthan V N | 317420a | 2013-06-03 20:10:04 +0000 | [diff] [blame] | 397 | static struct phy_driver at803x_driver[] = { |
| 398 | { |
| 399 | /* ATHEROS 8035 */ |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 400 | .phy_id = ATH8035_PHY_ID, |
| 401 | .name = "Atheros 8035 ethernet", |
Fabio Estevam | 58effd7 | 2016-10-26 14:03:54 -0200 | [diff] [blame] | 402 | .phy_id_mask = AT803X_PHY_ID_MASK, |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 403 | .probe = at803x_probe, |
| 404 | .config_init = at803x_config_init, |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 405 | .set_wol = at803x_set_wol, |
| 406 | .get_wol = at803x_get_wol, |
| 407 | .suspend = at803x_suspend, |
| 408 | .resume = at803x_resume, |
| 409 | .features = PHY_GBIT_FEATURES, |
| 410 | .flags = PHY_HAS_INTERRUPT, |
| 411 | .config_aneg = genphy_config_aneg, |
| 412 | .read_status = genphy_read_status, |
Måns Rullgård | 0eae598 | 2015-11-12 17:40:20 +0000 | [diff] [blame] | 413 | .ack_interrupt = at803x_ack_interrupt, |
| 414 | .config_intr = at803x_config_intr, |
Mugunthan V N | 317420a | 2013-06-03 20:10:04 +0000 | [diff] [blame] | 415 | }, { |
| 416 | /* ATHEROS 8030 */ |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 417 | .phy_id = ATH8030_PHY_ID, |
| 418 | .name = "Atheros 8030 ethernet", |
Fabio Estevam | 58effd7 | 2016-10-26 14:03:54 -0200 | [diff] [blame] | 419 | .phy_id_mask = AT803X_PHY_ID_MASK, |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 420 | .probe = at803x_probe, |
| 421 | .config_init = at803x_config_init, |
| 422 | .link_change_notify = at803x_link_change_notify, |
| 423 | .set_wol = at803x_set_wol, |
| 424 | .get_wol = at803x_get_wol, |
| 425 | .suspend = at803x_suspend, |
| 426 | .resume = at803x_resume, |
Martin Blumenstingl | e15bb4c | 2016-01-15 01:55:21 +0100 | [diff] [blame] | 427 | .features = PHY_BASIC_FEATURES, |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 428 | .flags = PHY_HAS_INTERRUPT, |
| 429 | .config_aneg = genphy_config_aneg, |
| 430 | .read_status = genphy_read_status, |
Måns Rullgård | 0eae598 | 2015-11-12 17:40:20 +0000 | [diff] [blame] | 431 | .ack_interrupt = at803x_ack_interrupt, |
| 432 | .config_intr = at803x_config_intr, |
Mugunthan V N | 05d7cce | 2013-06-03 20:10:07 +0000 | [diff] [blame] | 433 | }, { |
| 434 | /* ATHEROS 8031 */ |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 435 | .phy_id = ATH8031_PHY_ID, |
| 436 | .name = "Atheros 8031 ethernet", |
Fabio Estevam | 58effd7 | 2016-10-26 14:03:54 -0200 | [diff] [blame] | 437 | .phy_id_mask = AT803X_PHY_ID_MASK, |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 438 | .probe = at803x_probe, |
| 439 | .config_init = at803x_config_init, |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 440 | .set_wol = at803x_set_wol, |
| 441 | .get_wol = at803x_get_wol, |
| 442 | .suspend = at803x_suspend, |
| 443 | .resume = at803x_resume, |
| 444 | .features = PHY_GBIT_FEATURES, |
| 445 | .flags = PHY_HAS_INTERRUPT, |
| 446 | .config_aneg = genphy_config_aneg, |
| 447 | .read_status = genphy_read_status, |
Zefir Kurtisi | f62265b | 2016-10-24 12:40:54 +0200 | [diff] [blame] | 448 | .aneg_done = at803x_aneg_done, |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 449 | .ack_interrupt = &at803x_ack_interrupt, |
| 450 | .config_intr = &at803x_config_intr, |
Mugunthan V N | 317420a | 2013-06-03 20:10:04 +0000 | [diff] [blame] | 451 | } }; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 452 | |
Johan Hovold | 50fd715 | 2014-11-11 19:45:59 +0100 | [diff] [blame] | 453 | module_phy_driver(at803x_driver); |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 454 | |
| 455 | static struct mdio_device_id __maybe_unused atheros_tbl[] = { |
Fabio Estevam | 58effd7 | 2016-10-26 14:03:54 -0200 | [diff] [blame] | 456 | { ATH8030_PHY_ID, AT803X_PHY_ID_MASK }, |
| 457 | { ATH8031_PHY_ID, AT803X_PHY_ID_MASK }, |
| 458 | { ATH8035_PHY_ID, AT803X_PHY_ID_MASK }, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 459 | { } |
| 460 | }; |
| 461 | |
| 462 | MODULE_DEVICE_TABLE(mdio, atheros_tbl); |