micrel.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * drivers/net/phy/micrel.c
  3. *
  4. * Driver for Micrel PHYs
  5. *
  6. * Author: David J. Choi
  7. *
  8. * Copyright (c) 2010 Micrel, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * Support : ksz9021 1000/100/10 phy from Micrel
  16. * ks8001, ks8737, ks8721, ks8041, ks8051 100/10 phy
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/phy.h>
  21. #include <linux/micrel_phy.h>
  22. /* general Interrupt control/status reg in vendor specific block. */
  23. #define MII_KSZPHY_INTCS 0x1B
  24. #define KSZPHY_INTCS_JABBER (1 << 15)
  25. #define KSZPHY_INTCS_RECEIVE_ERR (1 << 14)
  26. #define KSZPHY_INTCS_PAGE_RECEIVE (1 << 13)
  27. #define KSZPHY_INTCS_PARELLEL (1 << 12)
  28. #define KSZPHY_INTCS_LINK_PARTNER_ACK (1 << 11)
  29. #define KSZPHY_INTCS_LINK_DOWN (1 << 10)
  30. #define KSZPHY_INTCS_REMOTE_FAULT (1 << 9)
  31. #define KSZPHY_INTCS_LINK_UP (1 << 8)
  32. #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
  33. KSZPHY_INTCS_LINK_DOWN)
  34. /* general PHY control reg in vendor specific block. */
  35. #define MII_KSZPHY_CTRL 0x1F
  36. /* bitmap of PHY register to set interrupt mode */
  37. #define KSZPHY_CTRL_INT_ACTIVE_HIGH (1 << 9)
  38. #define KSZ9021_CTRL_INT_ACTIVE_HIGH (1 << 14)
  39. #define KS8737_CTRL_INT_ACTIVE_HIGH (1 << 14)
  40. #define KSZ8051_RMII_50MHZ_CLK (1 << 7)
  41. static int kszphy_ack_interrupt(struct phy_device *phydev)
  42. {
  43. /* bit[7..0] int status, which is a read and clear register. */
  44. int rc;
  45. rc = phy_read(phydev, MII_KSZPHY_INTCS);
  46. return (rc < 0) ? rc : 0;
  47. }
  48. static int kszphy_set_interrupt(struct phy_device *phydev)
  49. {
  50. int temp;
  51. temp = (PHY_INTERRUPT_ENABLED == phydev->interrupts) ?
  52. KSZPHY_INTCS_ALL : 0;
  53. return phy_write(phydev, MII_KSZPHY_INTCS, temp);
  54. }
  55. static int kszphy_config_intr(struct phy_device *phydev)
  56. {
  57. int temp, rc;
  58. /* set the interrupt pin active low */
  59. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  60. temp &= ~KSZPHY_CTRL_INT_ACTIVE_HIGH;
  61. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  62. rc = kszphy_set_interrupt(phydev);
  63. return rc < 0 ? rc : 0;
  64. }
  65. static int ksz9021_config_intr(struct phy_device *phydev)
  66. {
  67. int temp, rc;
  68. /* set the interrupt pin active low */
  69. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  70. temp &= ~KSZ9021_CTRL_INT_ACTIVE_HIGH;
  71. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  72. rc = kszphy_set_interrupt(phydev);
  73. return rc < 0 ? rc : 0;
  74. }
  75. static int ks8737_config_intr(struct phy_device *phydev)
  76. {
  77. int temp, rc;
  78. /* set the interrupt pin active low */
  79. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  80. temp &= ~KS8737_CTRL_INT_ACTIVE_HIGH;
  81. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  82. rc = kszphy_set_interrupt(phydev);
  83. return rc < 0 ? rc : 0;
  84. }
  85. static int kszphy_config_init(struct phy_device *phydev)
  86. {
  87. return 0;
  88. }
  89. static int ks8051_config_init(struct phy_device *phydev)
  90. {
  91. int regval;
  92. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  93. regval = phy_read(phydev, MII_KSZPHY_CTRL);
  94. regval |= KSZ8051_RMII_50MHZ_CLK;
  95. phy_write(phydev, MII_KSZPHY_CTRL, regval);
  96. }
  97. return 0;
  98. }
  99. static struct phy_driver ks8737_driver = {
  100. .phy_id = PHY_ID_KS8737,
  101. .phy_id_mask = 0x00fffff0,
  102. .name = "Micrel KS8737",
  103. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  104. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  105. .config_init = kszphy_config_init,
  106. .config_aneg = genphy_config_aneg,
  107. .read_status = genphy_read_status,
  108. .ack_interrupt = kszphy_ack_interrupt,
  109. .config_intr = ks8737_config_intr,
  110. .driver = { .owner = THIS_MODULE,},
  111. };
  112. static struct phy_driver ks8041_driver = {
  113. .phy_id = PHY_ID_KS8041,
  114. .phy_id_mask = 0x00fffff0,
  115. .name = "Micrel KS8041",
  116. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  117. | SUPPORTED_Asym_Pause),
  118. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  119. .config_init = kszphy_config_init,
  120. .config_aneg = genphy_config_aneg,
  121. .read_status = genphy_read_status,
  122. .ack_interrupt = kszphy_ack_interrupt,
  123. .config_intr = kszphy_config_intr,
  124. .driver = { .owner = THIS_MODULE,},
  125. };
  126. static struct phy_driver ks8051_driver = {
  127. .phy_id = PHY_ID_KS8051,
  128. .phy_id_mask = 0x00fffff0,
  129. .name = "Micrel KS8051",
  130. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  131. | SUPPORTED_Asym_Pause),
  132. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  133. .config_init = ks8051_config_init,
  134. .config_aneg = genphy_config_aneg,
  135. .read_status = genphy_read_status,
  136. .ack_interrupt = kszphy_ack_interrupt,
  137. .config_intr = kszphy_config_intr,
  138. .driver = { .owner = THIS_MODULE,},
  139. };
  140. static struct phy_driver ks8001_driver = {
  141. .phy_id = PHY_ID_KS8001,
  142. .name = "Micrel KS8001 or KS8721",
  143. .phy_id_mask = 0x00fffff0,
  144. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  145. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  146. .config_init = kszphy_config_init,
  147. .config_aneg = genphy_config_aneg,
  148. .read_status = genphy_read_status,
  149. .ack_interrupt = kszphy_ack_interrupt,
  150. .config_intr = kszphy_config_intr,
  151. .driver = { .owner = THIS_MODULE,},
  152. };
  153. static struct phy_driver ksz9021_driver = {
  154. .phy_id = PHY_ID_KSZ9021,
  155. .phy_id_mask = 0x000fff10,
  156. .name = "Micrel KSZ9021 Gigabit PHY",
  157. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause
  158. | SUPPORTED_Asym_Pause),
  159. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  160. .config_init = kszphy_config_init,
  161. .config_aneg = genphy_config_aneg,
  162. .read_status = genphy_read_status,
  163. .ack_interrupt = kszphy_ack_interrupt,
  164. .config_intr = ksz9021_config_intr,
  165. .driver = { .owner = THIS_MODULE, },
  166. };
  167. static int __init ksphy_init(void)
  168. {
  169. int ret;
  170. ret = phy_driver_register(&ks8001_driver);
  171. if (ret)
  172. goto err1;
  173. ret = phy_driver_register(&ksz9021_driver);
  174. if (ret)
  175. goto err2;
  176. ret = phy_driver_register(&ks8737_driver);
  177. if (ret)
  178. goto err3;
  179. ret = phy_driver_register(&ks8041_driver);
  180. if (ret)
  181. goto err4;
  182. ret = phy_driver_register(&ks8051_driver);
  183. if (ret)
  184. goto err5;
  185. return 0;
  186. err5:
  187. phy_driver_unregister(&ks8041_driver);
  188. err4:
  189. phy_driver_unregister(&ks8737_driver);
  190. err3:
  191. phy_driver_unregister(&ksz9021_driver);
  192. err2:
  193. phy_driver_unregister(&ks8001_driver);
  194. err1:
  195. return ret;
  196. }
  197. static void __exit ksphy_exit(void)
  198. {
  199. phy_driver_unregister(&ks8001_driver);
  200. phy_driver_unregister(&ks8737_driver);
  201. phy_driver_unregister(&ksz9021_driver);
  202. phy_driver_unregister(&ks8041_driver);
  203. phy_driver_unregister(&ks8051_driver);
  204. }
  205. module_init(ksphy_init);
  206. module_exit(ksphy_exit);
  207. MODULE_DESCRIPTION("Micrel PHY driver");
  208. MODULE_AUTHOR("David J. Choi");
  209. MODULE_LICENSE("GPL");
  210. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  211. { PHY_ID_KSZ9021, 0x000fff10 },
  212. { PHY_ID_KS8001, 0x00fffff0 },
  213. { PHY_ID_KS8737, 0x00fffff0 },
  214. { PHY_ID_KS8041, 0x00fffff0 },
  215. { PHY_ID_KS8051, 0x00fffff0 },
  216. { }
  217. };
  218. MODULE_DEVICE_TABLE(mdio, micrel_tbl);