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 |
| 23 | #define AT803X_INTR_STATUS 0x13 |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 24 | #define AT803X_SMART_SPEED 0x14 |
| 25 | #define AT803X_LED_CONTROL 0x18 |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 26 | #define AT803X_WOL_ENABLE 0x01 |
| 27 | #define AT803X_DEVICE_ADDR 0x03 |
| 28 | #define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C |
| 29 | #define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B |
| 30 | #define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A |
| 31 | #define AT803X_MMD_ACCESS_CONTROL 0x0D |
| 32 | #define AT803X_MMD_ACCESS_CONTROL_DATA 0x0E |
| 33 | #define AT803X_FUNC_DATA 0x4003 |
Zhao Qiang | 77a9939 | 2014-03-28 15:39:41 +0800 | [diff] [blame] | 34 | #define AT803X_INER 0x0012 |
| 35 | #define AT803X_INER_INIT 0xec00 |
| 36 | #define AT803X_INSR 0x0013 |
Mugunthan V N | 1ca6d1b | 2013-06-03 20:10:06 +0000 | [diff] [blame] | 37 | #define AT803X_DEBUG_ADDR 0x1D |
| 38 | #define AT803X_DEBUG_DATA 0x1E |
| 39 | #define AT803X_DEBUG_SYSTEM_MODE_CTRL 0x05 |
| 40 | #define AT803X_DEBUG_RGMII_TX_CLK_DLY BIT(8) |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 41 | |
Daniel Mack | bd8ca17 | 2014-06-18 11:01:42 +0200 | [diff] [blame] | 42 | #define ATH8030_PHY_ID 0x004dd076 |
| 43 | #define ATH8031_PHY_ID 0x004dd074 |
| 44 | #define ATH8035_PHY_ID 0x004dd072 |
| 45 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 46 | MODULE_DESCRIPTION("Atheros 803x PHY driver"); |
| 47 | MODULE_AUTHOR("Matus Ujhelyi"); |
| 48 | MODULE_LICENSE("GPL"); |
| 49 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 50 | struct at803x_priv { |
| 51 | bool phy_reset:1; |
| 52 | struct gpio_desc *gpiod_reset; |
| 53 | }; |
| 54 | |
| 55 | struct at803x_context { |
| 56 | u16 bmcr; |
| 57 | u16 advertise; |
| 58 | u16 control1000; |
| 59 | u16 int_enable; |
| 60 | u16 smart_speed; |
| 61 | u16 led_control; |
| 62 | }; |
| 63 | |
| 64 | /* save relevant PHY registers to private copy */ |
| 65 | static void at803x_context_save(struct phy_device *phydev, |
| 66 | struct at803x_context *context) |
| 67 | { |
| 68 | context->bmcr = phy_read(phydev, MII_BMCR); |
| 69 | context->advertise = phy_read(phydev, MII_ADVERTISE); |
| 70 | context->control1000 = phy_read(phydev, MII_CTRL1000); |
| 71 | context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE); |
| 72 | context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED); |
| 73 | context->led_control = phy_read(phydev, AT803X_LED_CONTROL); |
| 74 | } |
| 75 | |
| 76 | /* restore relevant PHY registers from private copy */ |
| 77 | static void at803x_context_restore(struct phy_device *phydev, |
| 78 | const struct at803x_context *context) |
| 79 | { |
| 80 | phy_write(phydev, MII_BMCR, context->bmcr); |
| 81 | phy_write(phydev, MII_ADVERTISE, context->advertise); |
| 82 | phy_write(phydev, MII_CTRL1000, context->control1000); |
| 83 | phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable); |
| 84 | phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed); |
| 85 | phy_write(phydev, AT803X_LED_CONTROL, context->led_control); |
| 86 | } |
| 87 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 88 | static int at803x_set_wol(struct phy_device *phydev, |
| 89 | struct ethtool_wolinfo *wol) |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 90 | { |
| 91 | struct net_device *ndev = phydev->attached_dev; |
| 92 | const u8 *mac; |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 93 | int ret; |
| 94 | u32 value; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 95 | unsigned int i, offsets[] = { |
| 96 | AT803X_LOC_MAC_ADDR_32_47_OFFSET, |
| 97 | AT803X_LOC_MAC_ADDR_16_31_OFFSET, |
| 98 | AT803X_LOC_MAC_ADDR_0_15_OFFSET, |
| 99 | }; |
| 100 | |
| 101 | if (!ndev) |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 102 | return -ENODEV; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 103 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 104 | if (wol->wolopts & WAKE_MAGIC) { |
| 105 | mac = (const u8 *) ndev->dev_addr; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 106 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 107 | if (!is_valid_ether_addr(mac)) |
| 108 | return -EFAULT; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 109 | |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 110 | for (i = 0; i < 3; i++) { |
| 111 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 112 | AT803X_DEVICE_ADDR); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 113 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 114 | offsets[i]); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 115 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 116 | AT803X_FUNC_DATA); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 117 | phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 118 | mac[(i * 2) + 1] | (mac[(i * 2)] << 8)); |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
| 122 | value |= AT803X_WOL_ENABLE; |
| 123 | ret = phy_write(phydev, AT803X_INTR_ENABLE, value); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | value = phy_read(phydev, AT803X_INTR_STATUS); |
| 127 | } else { |
| 128 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
| 129 | value &= (~AT803X_WOL_ENABLE); |
| 130 | ret = phy_write(phydev, AT803X_INTR_ENABLE, value); |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | value = phy_read(phydev, AT803X_INTR_STATUS); |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 134 | } |
Mugunthan V N | ea13c9e | 2013-06-03 20:10:05 +0000 | [diff] [blame] | 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static void at803x_get_wol(struct phy_device *phydev, |
| 140 | struct ethtool_wolinfo *wol) |
| 141 | { |
| 142 | u32 value; |
| 143 | |
| 144 | wol->supported = WAKE_MAGIC; |
| 145 | wol->wolopts = 0; |
| 146 | |
| 147 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
| 148 | if (value & AT803X_WOL_ENABLE) |
| 149 | wol->wolopts |= WAKE_MAGIC; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Daniel Mack | 6229ed1 | 2013-09-21 16:53:02 +0200 | [diff] [blame] | 152 | static int at803x_suspend(struct phy_device *phydev) |
| 153 | { |
| 154 | int value; |
| 155 | int wol_enabled; |
| 156 | |
| 157 | mutex_lock(&phydev->lock); |
| 158 | |
| 159 | value = phy_read(phydev, AT803X_INTR_ENABLE); |
| 160 | wol_enabled = value & AT803X_WOL_ENABLE; |
| 161 | |
| 162 | value = phy_read(phydev, MII_BMCR); |
| 163 | |
| 164 | if (wol_enabled) |
| 165 | value |= BMCR_ISOLATE; |
| 166 | else |
| 167 | value |= BMCR_PDOWN; |
| 168 | |
| 169 | phy_write(phydev, MII_BMCR, value); |
| 170 | |
| 171 | mutex_unlock(&phydev->lock); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int at803x_resume(struct phy_device *phydev) |
| 177 | { |
| 178 | int value; |
| 179 | |
| 180 | mutex_lock(&phydev->lock); |
| 181 | |
| 182 | value = phy_read(phydev, MII_BMCR); |
| 183 | value &= ~(BMCR_PDOWN | BMCR_ISOLATE); |
| 184 | phy_write(phydev, MII_BMCR, value); |
| 185 | |
| 186 | mutex_unlock(&phydev->lock); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 191 | static int at803x_probe(struct phy_device *phydev) |
| 192 | { |
| 193 | struct device *dev = &phydev->dev; |
| 194 | struct at803x_priv *priv; |
Uwe Kleine-König | 687908c | 2015-03-31 22:08:45 +0200 | [diff] [blame] | 195 | struct gpio_desc *gpiod_reset; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 196 | |
Fengguang Wu | 8f2877c | 2014-06-22 12:32:51 +0200 | [diff] [blame] | 197 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 198 | if (!priv) |
| 199 | return -ENOMEM; |
| 200 | |
Uwe Kleine-König | 687908c | 2015-03-31 22:08:45 +0200 | [diff] [blame] | 201 | gpiod_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
| 202 | if (IS_ERR(gpiod_reset)) |
| 203 | return PTR_ERR(gpiod_reset); |
| 204 | |
| 205 | priv->gpiod_reset = gpiod_reset; |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 206 | |
| 207 | phydev->priv = priv; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 212 | static int at803x_config_init(struct phy_device *phydev) |
| 213 | { |
Mugunthan V N | 1ca6d1b | 2013-06-03 20:10:06 +0000 | [diff] [blame] | 214 | int ret; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 215 | |
Daniel Mack | 6ff01db | 2014-04-16 17:19:13 +0200 | [diff] [blame] | 216 | ret = genphy_config_init(phydev); |
| 217 | if (ret < 0) |
| 218 | return ret; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 219 | |
Mugunthan V N | 1ca6d1b | 2013-06-03 20:10:06 +0000 | [diff] [blame] | 220 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) { |
| 221 | ret = phy_write(phydev, AT803X_DEBUG_ADDR, |
| 222 | AT803X_DEBUG_SYSTEM_MODE_CTRL); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | ret = phy_write(phydev, AT803X_DEBUG_DATA, |
| 226 | AT803X_DEBUG_RGMII_TX_CLK_DLY); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | } |
| 230 | |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
Zhao Qiang | 77a9939 | 2014-03-28 15:39:41 +0800 | [diff] [blame] | 234 | static int at803x_ack_interrupt(struct phy_device *phydev) |
| 235 | { |
| 236 | int err; |
| 237 | |
| 238 | err = phy_read(phydev, AT803X_INSR); |
| 239 | |
| 240 | return (err < 0) ? err : 0; |
| 241 | } |
| 242 | |
| 243 | static int at803x_config_intr(struct phy_device *phydev) |
| 244 | { |
| 245 | int err; |
| 246 | int value; |
| 247 | |
| 248 | value = phy_read(phydev, AT803X_INER); |
| 249 | |
| 250 | if (phydev->interrupts == PHY_INTERRUPT_ENABLED) |
| 251 | err = phy_write(phydev, AT803X_INER, |
| 252 | value | AT803X_INER_INIT); |
| 253 | else |
| 254 | err = phy_write(phydev, AT803X_INER, 0); |
| 255 | |
| 256 | return err; |
| 257 | } |
| 258 | |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 259 | static void at803x_link_change_notify(struct phy_device *phydev) |
| 260 | { |
| 261 | struct at803x_priv *priv = phydev->priv; |
| 262 | |
| 263 | /* |
| 264 | * Conduct a hardware reset for AT8030 every time a link loss is |
| 265 | * signalled. This is necessary to circumvent a hardware bug that |
| 266 | * occurs when the cable is unplugged while TX packets are pending |
| 267 | * in the FIFO. In such cases, the FIFO enters an error mode it |
| 268 | * cannot recover from by software. |
| 269 | */ |
| 270 | if (phydev->drv->phy_id == ATH8030_PHY_ID) { |
| 271 | if (phydev->state == PHY_NOLINK) { |
| 272 | if (priv->gpiod_reset && !priv->phy_reset) { |
| 273 | struct at803x_context context; |
| 274 | |
| 275 | at803x_context_save(phydev, &context); |
| 276 | |
| 277 | gpiod_set_value(priv->gpiod_reset, 0); |
| 278 | msleep(1); |
| 279 | gpiod_set_value(priv->gpiod_reset, 1); |
| 280 | msleep(1); |
| 281 | |
| 282 | at803x_context_restore(phydev, &context); |
| 283 | |
| 284 | dev_dbg(&phydev->dev, "%s(): phy was reset\n", |
| 285 | __func__); |
| 286 | priv->phy_reset = true; |
| 287 | } |
| 288 | } else { |
| 289 | priv->phy_reset = false; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Mugunthan V N | 317420a | 2013-06-03 20:10:04 +0000 | [diff] [blame] | 294 | static struct phy_driver at803x_driver[] = { |
| 295 | { |
| 296 | /* ATHEROS 8035 */ |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 297 | .phy_id = ATH8035_PHY_ID, |
| 298 | .name = "Atheros 8035 ethernet", |
| 299 | .phy_id_mask = 0xffffffef, |
| 300 | .probe = at803x_probe, |
| 301 | .config_init = at803x_config_init, |
| 302 | .link_change_notify = at803x_link_change_notify, |
| 303 | .set_wol = at803x_set_wol, |
| 304 | .get_wol = at803x_get_wol, |
| 305 | .suspend = at803x_suspend, |
| 306 | .resume = at803x_resume, |
| 307 | .features = PHY_GBIT_FEATURES, |
| 308 | .flags = PHY_HAS_INTERRUPT, |
| 309 | .config_aneg = genphy_config_aneg, |
| 310 | .read_status = genphy_read_status, |
| 311 | .driver = { |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 312 | .owner = THIS_MODULE, |
| 313 | }, |
Mugunthan V N | 317420a | 2013-06-03 20:10:04 +0000 | [diff] [blame] | 314 | }, { |
| 315 | /* ATHEROS 8030 */ |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 316 | .phy_id = ATH8030_PHY_ID, |
| 317 | .name = "Atheros 8030 ethernet", |
| 318 | .phy_id_mask = 0xffffffef, |
| 319 | .probe = at803x_probe, |
| 320 | .config_init = at803x_config_init, |
| 321 | .link_change_notify = at803x_link_change_notify, |
| 322 | .set_wol = at803x_set_wol, |
| 323 | .get_wol = at803x_get_wol, |
| 324 | .suspend = at803x_suspend, |
| 325 | .resume = at803x_resume, |
| 326 | .features = PHY_GBIT_FEATURES, |
| 327 | .flags = PHY_HAS_INTERRUPT, |
| 328 | .config_aneg = genphy_config_aneg, |
| 329 | .read_status = genphy_read_status, |
| 330 | .driver = { |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 331 | .owner = THIS_MODULE, |
| 332 | }, |
Mugunthan V N | 05d7cce | 2013-06-03 20:10:07 +0000 | [diff] [blame] | 333 | }, { |
| 334 | /* ATHEROS 8031 */ |
Daniel Mack | 13a56b4 | 2014-06-18 11:01:43 +0200 | [diff] [blame] | 335 | .phy_id = ATH8031_PHY_ID, |
| 336 | .name = "Atheros 8031 ethernet", |
| 337 | .phy_id_mask = 0xffffffef, |
| 338 | .probe = at803x_probe, |
| 339 | .config_init = at803x_config_init, |
| 340 | .link_change_notify = at803x_link_change_notify, |
| 341 | .set_wol = at803x_set_wol, |
| 342 | .get_wol = at803x_get_wol, |
| 343 | .suspend = at803x_suspend, |
| 344 | .resume = at803x_resume, |
| 345 | .features = PHY_GBIT_FEATURES, |
| 346 | .flags = PHY_HAS_INTERRUPT, |
| 347 | .config_aneg = genphy_config_aneg, |
| 348 | .read_status = genphy_read_status, |
| 349 | .ack_interrupt = &at803x_ack_interrupt, |
| 350 | .config_intr = &at803x_config_intr, |
| 351 | .driver = { |
Mugunthan V N | 05d7cce | 2013-06-03 20:10:07 +0000 | [diff] [blame] | 352 | .owner = THIS_MODULE, |
| 353 | }, |
Mugunthan V N | 317420a | 2013-06-03 20:10:04 +0000 | [diff] [blame] | 354 | } }; |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 355 | |
Johan Hovold | 50fd715 | 2014-11-11 19:45:59 +0100 | [diff] [blame] | 356 | module_phy_driver(at803x_driver); |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 357 | |
| 358 | static struct mdio_device_id __maybe_unused atheros_tbl[] = { |
Daniel Mack | bd8ca17 | 2014-06-18 11:01:42 +0200 | [diff] [blame] | 359 | { ATH8030_PHY_ID, 0xffffffef }, |
| 360 | { ATH8031_PHY_ID, 0xffffffef }, |
| 361 | { ATH8035_PHY_ID, 0xffffffef }, |
Matus Ujhelyi | 0ca7111 | 2012-10-14 19:07:16 +0000 | [diff] [blame] | 362 | { } |
| 363 | }; |
| 364 | |
| 365 | MODULE_DEVICE_TABLE(mdio, atheros_tbl); |