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> 19 #include <linux/of_gpio.h> 20 #include <linux/gpio/consumer.h> 21 22 #define AT803X_INTR_ENABLE 0x12 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 33 #define AT803X_INTR_STATUS 0x13 34 35 #define AT803X_SMART_SPEED 0x14 36 #define AT803X_LED_CONTROL 0x18 37 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 45 #define AT803X_REG_CHIP_CONFIG 0x1f 46 #define AT803X_BT_BX_REG_SEL 0x8000 47 48 #define AT803X_DEBUG_ADDR 0x1D 49 #define AT803X_DEBUG_DATA 0x1E 50 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 57 #define AT803X_DEBUG_REG_0 0x00 58 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15) 59 60 #define AT803X_DEBUG_REG_5 0x05 61 #define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8) 62 63 #define ATH8030_PHY_ID 0x004dd076 64 #define ATH8031_PHY_ID 0x004dd074 65 #define ATH8035_PHY_ID 0x004dd072 66 #define AT803X_PHY_ID_MASK 0xffffffef 67 68 MODULE_DESCRIPTION("Atheros 803x PHY driver"); 69 MODULE_AUTHOR("Matus Ujhelyi"); 70 MODULE_LICENSE("GPL"); 71 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 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 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 150 static int at803x_set_wol(struct phy_device *phydev, 151 struct ethtool_wolinfo *wol) 152 { 153 struct net_device *ndev = phydev->attached_dev; 154 const u8 *mac; 155 int ret; 156 u32 value; 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) 164 return -ENODEV; 165 166 if (wol->wolopts & WAKE_MAGIC) { 167 mac = (const u8 *) ndev->dev_addr; 168 169 if (!is_valid_ether_addr(mac)) 170 return -EINVAL; 171 172 for (i = 0; i < 3; i++) { 173 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL, 174 AT803X_DEVICE_ADDR); 175 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA, 176 offsets[i]); 177 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL, 178 AT803X_FUNC_DATA); 179 phy_write(phydev, AT803X_MMD_ACCESS_CONTROL_DATA, 180 mac[(i * 2) + 1] | (mac[(i * 2)] << 8)); 181 } 182 183 value = phy_read(phydev, AT803X_INTR_ENABLE); 184 value |= AT803X_INTR_ENABLE_WOL; 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); 191 value &= (~AT803X_INTR_ENABLE_WOL); 192 ret = phy_write(phydev, AT803X_INTR_ENABLE, value); 193 if (ret) 194 return ret; 195 value = phy_read(phydev, AT803X_INTR_STATUS); 196 } 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); 210 if (value & AT803X_INTR_ENABLE_WOL) 211 wol->wolopts |= WAKE_MAGIC; 212 } 213 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); 222 wol_enabled = value & AT803X_INTR_ENABLE_WOL; 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 value = phy_read(phydev, MII_BMCR); 243 value &= ~(BMCR_PDOWN | BMCR_ISOLATE); 244 phy_write(phydev, MII_BMCR, value); 245 246 return 0; 247 } 248 249 static int at803x_probe(struct phy_device *phydev) 250 { 251 struct device *dev = &phydev->mdio.dev; 252 struct at803x_priv *priv; 253 struct gpio_desc *gpiod_reset; 254 255 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 256 if (!priv) 257 return -ENOMEM; 258 259 if (phydev->drv->phy_id != ATH8030_PHY_ID) 260 goto does_not_require_reset_workaround; 261 262 gpiod_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 263 if (IS_ERR(gpiod_reset)) 264 return PTR_ERR(gpiod_reset); 265 266 priv->gpiod_reset = gpiod_reset; 267 268 does_not_require_reset_workaround: 269 phydev->priv = priv; 270 271 return 0; 272 } 273 274 static int at803x_config_init(struct phy_device *phydev) 275 { 276 int ret; 277 278 ret = genphy_config_init(phydev); 279 if (ret < 0) 280 return ret; 281 282 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID || 283 phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) { 284 ret = at803x_enable_rx_delay(phydev); 285 if (ret < 0) 286 return ret; 287 } 288 289 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID || 290 phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) { 291 ret = at803x_enable_tx_delay(phydev); 292 if (ret < 0) 293 return ret; 294 } 295 296 return 0; 297 } 298 299 static int at803x_ack_interrupt(struct phy_device *phydev) 300 { 301 int err; 302 303 err = phy_read(phydev, AT803X_INTR_STATUS); 304 305 return (err < 0) ? err : 0; 306 } 307 308 static int at803x_config_intr(struct phy_device *phydev) 309 { 310 int err; 311 int value; 312 313 value = phy_read(phydev, AT803X_INTR_ENABLE); 314 315 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) { 316 value |= AT803X_INTR_ENABLE_AUTONEG_ERR; 317 value |= AT803X_INTR_ENABLE_SPEED_CHANGED; 318 value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED; 319 value |= AT803X_INTR_ENABLE_LINK_FAIL; 320 value |= AT803X_INTR_ENABLE_LINK_SUCCESS; 321 322 err = phy_write(phydev, AT803X_INTR_ENABLE, value); 323 } 324 else 325 err = phy_write(phydev, AT803X_INTR_ENABLE, 0); 326 327 return err; 328 } 329 330 static void at803x_link_change_notify(struct phy_device *phydev) 331 { 332 struct at803x_priv *priv = phydev->priv; 333 334 /* 335 * Conduct a hardware reset for AT8030 every time a link loss is 336 * signalled. This is necessary to circumvent a hardware bug that 337 * occurs when the cable is unplugged while TX packets are pending 338 * in the FIFO. In such cases, the FIFO enters an error mode it 339 * cannot recover from by software. 340 */ 341 if (phydev->state == PHY_NOLINK) { 342 if (priv->gpiod_reset && !priv->phy_reset) { 343 struct at803x_context context; 344 345 at803x_context_save(phydev, &context); 346 347 gpiod_set_value(priv->gpiod_reset, 1); 348 msleep(1); 349 gpiod_set_value(priv->gpiod_reset, 0); 350 msleep(1); 351 352 at803x_context_restore(phydev, &context); 353 354 phydev_dbg(phydev, "%s(): phy was reset\n", 355 __func__); 356 priv->phy_reset = true; 357 } 358 } else { 359 priv->phy_reset = false; 360 } 361 } 362 363 static int at803x_aneg_done(struct phy_device *phydev) 364 { 365 int ccr; 366 367 int aneg_done = genphy_aneg_done(phydev); 368 if (aneg_done != BMSR_ANEGCOMPLETE) 369 return aneg_done; 370 371 /* 372 * in SGMII mode, if copper side autoneg is successful, 373 * also check SGMII side autoneg result 374 */ 375 ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG); 376 if ((ccr & AT803X_MODE_CFG_MASK) != AT803X_MODE_CFG_SGMII) 377 return aneg_done; 378 379 /* switch to SGMII/fiber page */ 380 phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL); 381 382 /* check if the SGMII link is OK. */ 383 if (!(phy_read(phydev, AT803X_PSSR) & AT803X_PSSR_MR_AN_COMPLETE)) { 384 pr_warn("803x_aneg_done: SGMII link is not ok\n"); 385 aneg_done = 0; 386 } 387 /* switch back to copper page */ 388 phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL); 389 390 return aneg_done; 391 } 392 393 static struct phy_driver at803x_driver[] = { 394 { 395 /* ATHEROS 8035 */ 396 .phy_id = ATH8035_PHY_ID, 397 .name = "Atheros 8035 ethernet", 398 .phy_id_mask = AT803X_PHY_ID_MASK, 399 .probe = at803x_probe, 400 .config_init = at803x_config_init, 401 .set_wol = at803x_set_wol, 402 .get_wol = at803x_get_wol, 403 .suspend = at803x_suspend, 404 .resume = at803x_resume, 405 .features = PHY_GBIT_FEATURES, 406 .flags = PHY_HAS_INTERRUPT, 407 .config_aneg = genphy_config_aneg, 408 .read_status = genphy_read_status, 409 .ack_interrupt = at803x_ack_interrupt, 410 .config_intr = at803x_config_intr, 411 }, { 412 /* ATHEROS 8030 */ 413 .phy_id = ATH8030_PHY_ID, 414 .name = "Atheros 8030 ethernet", 415 .phy_id_mask = AT803X_PHY_ID_MASK, 416 .probe = at803x_probe, 417 .config_init = at803x_config_init, 418 .link_change_notify = at803x_link_change_notify, 419 .set_wol = at803x_set_wol, 420 .get_wol = at803x_get_wol, 421 .suspend = at803x_suspend, 422 .resume = at803x_resume, 423 .features = PHY_BASIC_FEATURES, 424 .flags = PHY_HAS_INTERRUPT, 425 .config_aneg = genphy_config_aneg, 426 .read_status = genphy_read_status, 427 .ack_interrupt = at803x_ack_interrupt, 428 .config_intr = at803x_config_intr, 429 }, { 430 /* ATHEROS 8031 */ 431 .phy_id = ATH8031_PHY_ID, 432 .name = "Atheros 8031 ethernet", 433 .phy_id_mask = AT803X_PHY_ID_MASK, 434 .probe = at803x_probe, 435 .config_init = at803x_config_init, 436 .set_wol = at803x_set_wol, 437 .get_wol = at803x_get_wol, 438 .suspend = at803x_suspend, 439 .resume = at803x_resume, 440 .features = PHY_GBIT_FEATURES, 441 .flags = PHY_HAS_INTERRUPT, 442 .config_aneg = genphy_config_aneg, 443 .read_status = genphy_read_status, 444 .aneg_done = at803x_aneg_done, 445 .ack_interrupt = &at803x_ack_interrupt, 446 .config_intr = &at803x_config_intr, 447 } }; 448 449 module_phy_driver(at803x_driver); 450 451 static struct mdio_device_id __maybe_unused atheros_tbl[] = { 452 { ATH8030_PHY_ID, AT803X_PHY_ID_MASK }, 453 { ATH8031_PHY_ID, AT803X_PHY_ID_MASK }, 454 { ATH8035_PHY_ID, AT803X_PHY_ID_MASK }, 455 { } 456 }; 457 458 MODULE_DEVICE_TABLE(mdio, atheros_tbl); 459