microchip.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2015 Microchip Technology
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mii.h>
  20. #include <linux/ethtool.h>
  21. #include <linux/phy.h>
  22. #include <linux/microchipphy.h>
  23. #define DRIVER_AUTHOR "WOOJUNG HUH <woojung.huh@microchip.com>"
  24. #define DRIVER_DESC "Microchip LAN88XX PHY driver"
  25. struct lan88xx_priv {
  26. int chip_id;
  27. int chip_rev;
  28. __u32 wolopts;
  29. };
  30. static int lan88xx_phy_config_intr(struct phy_device *phydev)
  31. {
  32. int rc;
  33. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  34. /* unmask all source and clear them before enable */
  35. rc = phy_write(phydev, LAN88XX_INT_MASK, 0x7FFF);
  36. rc = phy_read(phydev, LAN88XX_INT_STS);
  37. rc = phy_write(phydev, LAN88XX_INT_MASK,
  38. LAN88XX_INT_MASK_MDINTPIN_EN_ |
  39. LAN88XX_INT_MASK_LINK_CHANGE_);
  40. } else {
  41. rc = phy_write(phydev, LAN88XX_INT_MASK, 0);
  42. }
  43. return rc < 0 ? rc : 0;
  44. }
  45. static int lan88xx_phy_ack_interrupt(struct phy_device *phydev)
  46. {
  47. int rc = phy_read(phydev, LAN88XX_INT_STS);
  48. return rc < 0 ? rc : 0;
  49. }
  50. static int lan88xx_suspend(struct phy_device *phydev)
  51. {
  52. struct lan88xx_priv *priv = phydev->priv;
  53. /* do not power down PHY when WOL is enabled */
  54. if (!priv->wolopts)
  55. genphy_suspend(phydev);
  56. return 0;
  57. }
  58. static int lan88xx_probe(struct phy_device *phydev)
  59. {
  60. struct device *dev = &phydev->mdio.dev;
  61. struct lan88xx_priv *priv;
  62. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  63. if (!priv)
  64. return -ENOMEM;
  65. priv->wolopts = 0;
  66. /* these values can be used to identify internal PHY */
  67. priv->chip_id = phy_read_mmd(phydev, 3, LAN88XX_MMD3_CHIP_ID);
  68. priv->chip_rev = phy_read_mmd(phydev, 3, LAN88XX_MMD3_CHIP_REV);
  69. phydev->priv = priv;
  70. return 0;
  71. }
  72. static void lan88xx_remove(struct phy_device *phydev)
  73. {
  74. struct device *dev = &phydev->mdio.dev;
  75. struct lan88xx_priv *priv = phydev->priv;
  76. if (priv)
  77. devm_kfree(dev, priv);
  78. }
  79. static int lan88xx_set_wol(struct phy_device *phydev,
  80. struct ethtool_wolinfo *wol)
  81. {
  82. struct lan88xx_priv *priv = phydev->priv;
  83. priv->wolopts = wol->wolopts;
  84. return 0;
  85. }
  86. static void lan88xx_set_mdix(struct phy_device *phydev)
  87. {
  88. int buf;
  89. int val;
  90. switch (phydev->mdix_ctrl) {
  91. case ETH_TP_MDI:
  92. val = LAN88XX_EXT_MODE_CTRL_MDI_;
  93. break;
  94. case ETH_TP_MDI_X:
  95. val = LAN88XX_EXT_MODE_CTRL_MDI_X_;
  96. break;
  97. case ETH_TP_MDI_AUTO:
  98. val = LAN88XX_EXT_MODE_CTRL_AUTO_MDIX_;
  99. break;
  100. default:
  101. return;
  102. }
  103. phy_write(phydev, LAN88XX_EXT_PAGE_ACCESS, LAN88XX_EXT_PAGE_SPACE_1);
  104. buf = phy_read(phydev, LAN88XX_EXT_MODE_CTRL);
  105. buf &= ~LAN88XX_EXT_MODE_CTRL_MDIX_MASK_;
  106. buf |= val;
  107. phy_write(phydev, LAN88XX_EXT_MODE_CTRL, buf);
  108. phy_write(phydev, LAN88XX_EXT_PAGE_ACCESS, LAN88XX_EXT_PAGE_SPACE_0);
  109. }
  110. static int lan88xx_config_aneg(struct phy_device *phydev)
  111. {
  112. lan88xx_set_mdix(phydev);
  113. return genphy_config_aneg(phydev);
  114. }
  115. static struct phy_driver microchip_phy_driver[] = {
  116. {
  117. .phy_id = 0x0007c130,
  118. .phy_id_mask = 0xfffffff0,
  119. .name = "Microchip LAN88xx",
  120. .features = PHY_GBIT_FEATURES,
  121. .flags = PHY_HAS_INTERRUPT,
  122. .probe = lan88xx_probe,
  123. .remove = lan88xx_remove,
  124. .config_init = genphy_config_init,
  125. .config_aneg = lan88xx_config_aneg,
  126. .read_status = genphy_read_status,
  127. .ack_interrupt = lan88xx_phy_ack_interrupt,
  128. .config_intr = lan88xx_phy_config_intr,
  129. .suspend = lan88xx_suspend,
  130. .resume = genphy_resume,
  131. .set_wol = lan88xx_set_wol,
  132. } };
  133. module_phy_driver(microchip_phy_driver);
  134. static struct mdio_device_id __maybe_unused microchip_tbl[] = {
  135. { 0x0007c130, 0xfffffff0 },
  136. { }
  137. };
  138. MODULE_DEVICE_TABLE(mdio, microchip_tbl);
  139. MODULE_AUTHOR(DRIVER_AUTHOR);
  140. MODULE_DESCRIPTION(DRIVER_DESC);
  141. MODULE_LICENSE("GPL");