micrel.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /* Operation Mode Strap Override */
  23. #define MII_KSZPHY_OMSO 0x16
  24. #define KSZPHY_OMSO_B_CAST_OFF (1 << 9)
  25. #define KSZPHY_OMSO_RMII_OVERRIDE (1 << 1)
  26. #define KSZPHY_OMSO_MII_OVERRIDE (1 << 0)
  27. /* general Interrupt control/status reg in vendor specific block. */
  28. #define MII_KSZPHY_INTCS 0x1B
  29. #define KSZPHY_INTCS_JABBER (1 << 15)
  30. #define KSZPHY_INTCS_RECEIVE_ERR (1 << 14)
  31. #define KSZPHY_INTCS_PAGE_RECEIVE (1 << 13)
  32. #define KSZPHY_INTCS_PARELLEL (1 << 12)
  33. #define KSZPHY_INTCS_LINK_PARTNER_ACK (1 << 11)
  34. #define KSZPHY_INTCS_LINK_DOWN (1 << 10)
  35. #define KSZPHY_INTCS_REMOTE_FAULT (1 << 9)
  36. #define KSZPHY_INTCS_LINK_UP (1 << 8)
  37. #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
  38. KSZPHY_INTCS_LINK_DOWN)
  39. /* general PHY control reg in vendor specific block. */
  40. #define MII_KSZPHY_CTRL 0x1F
  41. /* bitmap of PHY register to set interrupt mode */
  42. #define KSZPHY_CTRL_INT_ACTIVE_HIGH (1 << 9)
  43. #define KSZ9021_CTRL_INT_ACTIVE_HIGH (1 << 14)
  44. #define KS8737_CTRL_INT_ACTIVE_HIGH (1 << 14)
  45. #define KSZ8051_RMII_50MHZ_CLK (1 << 7)
  46. static int kszphy_ack_interrupt(struct phy_device *phydev)
  47. {
  48. /* bit[7..0] int status, which is a read and clear register. */
  49. int rc;
  50. rc = phy_read(phydev, MII_KSZPHY_INTCS);
  51. return (rc < 0) ? rc : 0;
  52. }
  53. static int kszphy_set_interrupt(struct phy_device *phydev)
  54. {
  55. int temp;
  56. temp = (PHY_INTERRUPT_ENABLED == phydev->interrupts) ?
  57. KSZPHY_INTCS_ALL : 0;
  58. return phy_write(phydev, MII_KSZPHY_INTCS, temp);
  59. }
  60. static int kszphy_config_intr(struct phy_device *phydev)
  61. {
  62. int temp, rc;
  63. /* set the interrupt pin active low */
  64. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  65. temp &= ~KSZPHY_CTRL_INT_ACTIVE_HIGH;
  66. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  67. rc = kszphy_set_interrupt(phydev);
  68. return rc < 0 ? rc : 0;
  69. }
  70. static int ksz9021_config_intr(struct phy_device *phydev)
  71. {
  72. int temp, rc;
  73. /* set the interrupt pin active low */
  74. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  75. temp &= ~KSZ9021_CTRL_INT_ACTIVE_HIGH;
  76. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  77. rc = kszphy_set_interrupt(phydev);
  78. return rc < 0 ? rc : 0;
  79. }
  80. static int ks8737_config_intr(struct phy_device *phydev)
  81. {
  82. int temp, rc;
  83. /* set the interrupt pin active low */
  84. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  85. temp &= ~KS8737_CTRL_INT_ACTIVE_HIGH;
  86. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  87. rc = kszphy_set_interrupt(phydev);
  88. return rc < 0 ? rc : 0;
  89. }
  90. static int kszphy_config_init(struct phy_device *phydev)
  91. {
  92. return 0;
  93. }
  94. static int ksz8021_config_init(struct phy_device *phydev)
  95. {
  96. const u16 val = KSZPHY_OMSO_B_CAST_OFF | KSZPHY_OMSO_RMII_OVERRIDE;
  97. phy_write(phydev, MII_KSZPHY_OMSO, val);
  98. return 0;
  99. }
  100. static int ks8051_config_init(struct phy_device *phydev)
  101. {
  102. int regval;
  103. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  104. regval = phy_read(phydev, MII_KSZPHY_CTRL);
  105. regval |= KSZ8051_RMII_50MHZ_CLK;
  106. phy_write(phydev, MII_KSZPHY_CTRL, regval);
  107. }
  108. return 0;
  109. }
  110. #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
  111. #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX (1 << 6)
  112. #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED (1 << 4)
  113. int ksz8873mll_read_status(struct phy_device *phydev)
  114. {
  115. int regval;
  116. /* dummy read */
  117. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  118. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  119. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
  120. phydev->duplex = DUPLEX_HALF;
  121. else
  122. phydev->duplex = DUPLEX_FULL;
  123. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
  124. phydev->speed = SPEED_10;
  125. else
  126. phydev->speed = SPEED_100;
  127. phydev->link = 1;
  128. phydev->pause = phydev->asym_pause = 0;
  129. return 0;
  130. }
  131. static int ksz8873mll_config_aneg(struct phy_device *phydev)
  132. {
  133. return 0;
  134. }
  135. static struct phy_driver ksphy_driver[] = {
  136. {
  137. .phy_id = PHY_ID_KS8737,
  138. .phy_id_mask = 0x00fffff0,
  139. .name = "Micrel KS8737",
  140. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  141. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  142. .config_init = kszphy_config_init,
  143. .config_aneg = genphy_config_aneg,
  144. .read_status = genphy_read_status,
  145. .ack_interrupt = kszphy_ack_interrupt,
  146. .config_intr = ks8737_config_intr,
  147. .driver = { .owner = THIS_MODULE,},
  148. }, {
  149. .phy_id = PHY_ID_KSZ8021,
  150. .phy_id_mask = 0x00ffffff,
  151. .name = "Micrel KSZ8021",
  152. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  153. SUPPORTED_Asym_Pause),
  154. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  155. .config_init = ksz8021_config_init,
  156. .config_aneg = genphy_config_aneg,
  157. .read_status = genphy_read_status,
  158. .ack_interrupt = kszphy_ack_interrupt,
  159. .config_intr = kszphy_config_intr,
  160. .driver = { .owner = THIS_MODULE,},
  161. }, {
  162. .phy_id = PHY_ID_KSZ8041,
  163. .phy_id_mask = 0x00fffff0,
  164. .name = "Micrel KSZ8041",
  165. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  166. | SUPPORTED_Asym_Pause),
  167. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  168. .config_init = kszphy_config_init,
  169. .config_aneg = genphy_config_aneg,
  170. .read_status = genphy_read_status,
  171. .ack_interrupt = kszphy_ack_interrupt,
  172. .config_intr = kszphy_config_intr,
  173. .driver = { .owner = THIS_MODULE,},
  174. }, {
  175. .phy_id = PHY_ID_KSZ8051,
  176. .phy_id_mask = 0x00fffff0,
  177. .name = "Micrel KSZ8051",
  178. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  179. | SUPPORTED_Asym_Pause),
  180. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  181. .config_init = ks8051_config_init,
  182. .config_aneg = genphy_config_aneg,
  183. .read_status = genphy_read_status,
  184. .ack_interrupt = kszphy_ack_interrupt,
  185. .config_intr = kszphy_config_intr,
  186. .driver = { .owner = THIS_MODULE,},
  187. }, {
  188. .phy_id = PHY_ID_KSZ8001,
  189. .name = "Micrel KSZ8001 or KS8721",
  190. .phy_id_mask = 0x00ffffff,
  191. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  192. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  193. .config_init = kszphy_config_init,
  194. .config_aneg = genphy_config_aneg,
  195. .read_status = genphy_read_status,
  196. .ack_interrupt = kszphy_ack_interrupt,
  197. .config_intr = kszphy_config_intr,
  198. .driver = { .owner = THIS_MODULE,},
  199. }, {
  200. .phy_id = PHY_ID_KSZ9021,
  201. .phy_id_mask = 0x000ffffe,
  202. .name = "Micrel KSZ9021 Gigabit PHY",
  203. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause
  204. | SUPPORTED_Asym_Pause),
  205. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  206. .config_init = kszphy_config_init,
  207. .config_aneg = genphy_config_aneg,
  208. .read_status = genphy_read_status,
  209. .ack_interrupt = kszphy_ack_interrupt,
  210. .config_intr = ksz9021_config_intr,
  211. .driver = { .owner = THIS_MODULE, },
  212. }, {
  213. .phy_id = PHY_ID_KSZ8873MLL,
  214. .phy_id_mask = 0x00fffff0,
  215. .name = "Micrel KSZ8873MLL Switch",
  216. .features = (SUPPORTED_Pause | SUPPORTED_Asym_Pause),
  217. .flags = PHY_HAS_MAGICANEG,
  218. .config_init = kszphy_config_init,
  219. .config_aneg = ksz8873mll_config_aneg,
  220. .read_status = ksz8873mll_read_status,
  221. .driver = { .owner = THIS_MODULE, },
  222. } };
  223. static int __init ksphy_init(void)
  224. {
  225. return phy_drivers_register(ksphy_driver,
  226. ARRAY_SIZE(ksphy_driver));
  227. }
  228. static void __exit ksphy_exit(void)
  229. {
  230. phy_drivers_unregister(ksphy_driver,
  231. ARRAY_SIZE(ksphy_driver));
  232. }
  233. module_init(ksphy_init);
  234. module_exit(ksphy_exit);
  235. MODULE_DESCRIPTION("Micrel PHY driver");
  236. MODULE_AUTHOR("David J. Choi");
  237. MODULE_LICENSE("GPL");
  238. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  239. { PHY_ID_KSZ9021, 0x000ffffe },
  240. { PHY_ID_KSZ8001, 0x00ffffff },
  241. { PHY_ID_KS8737, 0x00fffff0 },
  242. { PHY_ID_KSZ8021, 0x00ffffff },
  243. { PHY_ID_KSZ8041, 0x00fffff0 },
  244. { PHY_ID_KSZ8051, 0x00fffff0 },
  245. { PHY_ID_KSZ8873MLL, 0x00fffff0 },
  246. { }
  247. };
  248. MODULE_DEVICE_TABLE(mdio, micrel_tbl);