aquantia.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Driver for Aquantia PHY
  3. *
  4. * Author: Shaohui Xie <Shaohui.Xie@freescale.com>
  5. *
  6. * Copyright 2015 Freescale Semiconductor, Inc.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/delay.h>
  15. #include <linux/mii.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/phy.h>
  18. #include <linux/mdio.h>
  19. #define PHY_ID_AQ1202 0x03a1b445
  20. #define PHY_ID_AQ2104 0x03a1b460
  21. #define PHY_ID_AQR105 0x03a1b4a2
  22. #define PHY_ID_AQR405 0x03a1b4b0
  23. #define PHY_AQUANTIA_FEATURES (SUPPORTED_10000baseT_Full | \
  24. SUPPORTED_1000baseT_Full | \
  25. SUPPORTED_100baseT_Full | \
  26. PHY_DEFAULT_FEATURES)
  27. static int aquantia_config_aneg(struct phy_device *phydev)
  28. {
  29. phydev->supported = PHY_AQUANTIA_FEATURES;
  30. phydev->advertising = phydev->supported;
  31. return 0;
  32. }
  33. static int aquantia_aneg_done(struct phy_device *phydev)
  34. {
  35. int reg;
  36. reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
  37. return (reg < 0) ? reg : (reg & BMSR_ANEGCOMPLETE);
  38. }
  39. static int aquantia_config_intr(struct phy_device *phydev)
  40. {
  41. int err;
  42. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  43. err = phy_write_mmd(phydev, MDIO_MMD_AN, 0xd401, 1);
  44. if (err < 0)
  45. return err;
  46. err = phy_write_mmd(phydev, MDIO_MMD_VEND1, 0xff00, 1);
  47. if (err < 0)
  48. return err;
  49. err = phy_write_mmd(phydev, MDIO_MMD_VEND1, 0xff01, 0x1001);
  50. } else {
  51. err = phy_write_mmd(phydev, MDIO_MMD_AN, 0xd401, 0);
  52. if (err < 0)
  53. return err;
  54. err = phy_write_mmd(phydev, MDIO_MMD_VEND1, 0xff00, 0);
  55. if (err < 0)
  56. return err;
  57. err = phy_write_mmd(phydev, MDIO_MMD_VEND1, 0xff01, 0);
  58. }
  59. return err;
  60. }
  61. static int aquantia_ack_interrupt(struct phy_device *phydev)
  62. {
  63. int reg;
  64. reg = phy_read_mmd(phydev, MDIO_MMD_AN, 0xcc01);
  65. return (reg < 0) ? reg : 0;
  66. }
  67. static int aquantia_read_status(struct phy_device *phydev)
  68. {
  69. int reg;
  70. reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
  71. reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
  72. if (reg & MDIO_STAT1_LSTATUS)
  73. phydev->link = 1;
  74. else
  75. phydev->link = 0;
  76. reg = phy_read_mmd(phydev, MDIO_MMD_AN, 0xc800);
  77. mdelay(10);
  78. reg = phy_read_mmd(phydev, MDIO_MMD_AN, 0xc800);
  79. switch (reg) {
  80. case 0x9:
  81. phydev->speed = SPEED_2500;
  82. break;
  83. case 0x5:
  84. phydev->speed = SPEED_1000;
  85. break;
  86. case 0x3:
  87. phydev->speed = SPEED_100;
  88. break;
  89. case 0x7:
  90. default:
  91. phydev->speed = SPEED_10000;
  92. break;
  93. }
  94. phydev->duplex = DUPLEX_FULL;
  95. return 0;
  96. }
  97. static struct phy_driver aquantia_driver[] = {
  98. {
  99. .phy_id = PHY_ID_AQ1202,
  100. .phy_id_mask = 0xfffffff0,
  101. .name = "Aquantia AQ1202",
  102. .features = PHY_AQUANTIA_FEATURES,
  103. .flags = PHY_HAS_INTERRUPT,
  104. .aneg_done = aquantia_aneg_done,
  105. .config_aneg = aquantia_config_aneg,
  106. .config_intr = aquantia_config_intr,
  107. .ack_interrupt = aquantia_ack_interrupt,
  108. .read_status = aquantia_read_status,
  109. .driver = { .owner = THIS_MODULE,},
  110. },
  111. {
  112. .phy_id = PHY_ID_AQ2104,
  113. .phy_id_mask = 0xfffffff0,
  114. .name = "Aquantia AQ2104",
  115. .features = PHY_AQUANTIA_FEATURES,
  116. .flags = PHY_HAS_INTERRUPT,
  117. .aneg_done = aquantia_aneg_done,
  118. .config_aneg = aquantia_config_aneg,
  119. .config_intr = aquantia_config_intr,
  120. .ack_interrupt = aquantia_ack_interrupt,
  121. .read_status = aquantia_read_status,
  122. .driver = { .owner = THIS_MODULE,},
  123. },
  124. {
  125. .phy_id = PHY_ID_AQR105,
  126. .phy_id_mask = 0xfffffff0,
  127. .name = "Aquantia AQR105",
  128. .features = PHY_AQUANTIA_FEATURES,
  129. .flags = PHY_HAS_INTERRUPT,
  130. .aneg_done = aquantia_aneg_done,
  131. .config_aneg = aquantia_config_aneg,
  132. .config_intr = aquantia_config_intr,
  133. .ack_interrupt = aquantia_ack_interrupt,
  134. .read_status = aquantia_read_status,
  135. .driver = { .owner = THIS_MODULE,},
  136. },
  137. {
  138. .phy_id = PHY_ID_AQR405,
  139. .phy_id_mask = 0xfffffff0,
  140. .name = "Aquantia AQR405",
  141. .features = PHY_AQUANTIA_FEATURES,
  142. .flags = PHY_HAS_INTERRUPT,
  143. .aneg_done = aquantia_aneg_done,
  144. .config_aneg = aquantia_config_aneg,
  145. .config_intr = aquantia_config_intr,
  146. .ack_interrupt = aquantia_ack_interrupt,
  147. .read_status = aquantia_read_status,
  148. .driver = { .owner = THIS_MODULE,},
  149. },
  150. };
  151. module_phy_driver(aquantia_driver);
  152. static struct mdio_device_id __maybe_unused aquantia_tbl[] = {
  153. { PHY_ID_AQ1202, 0xfffffff0 },
  154. { PHY_ID_AQ2104, 0xfffffff0 },
  155. { PHY_ID_AQR105, 0xfffffff0 },
  156. { PHY_ID_AQR405, 0xfffffff0 },
  157. { }
  158. };
  159. MODULE_DEVICE_TABLE(mdio, aquantia_tbl);
  160. MODULE_DESCRIPTION("Aquantia PHY driver");
  161. MODULE_AUTHOR("Shaohui Xie <Shaohui.Xie@freescale.com>");
  162. MODULE_LICENSE("GPL v2");