micrel.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * drivers/net/phy/micrel.c
  3. *
  4. * Driver for Micrel PHYs
  5. *
  6. * Author: David J. Choi
  7. *
  8. * Copyright (c) 2010-2013 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 : Micrel Phys:
  16. * Giga phys: ksz9021, ksz9031
  17. * 100/10 Phys : ksz8001, ksz8721, ksz8737, ksz8041
  18. * ksz8021, ksz8031, ksz8051,
  19. * ksz8081, ksz8091,
  20. * ksz8061,
  21. * Switch : ksz8873, ksz886x
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/phy.h>
  26. #include <linux/micrel_phy.h>
  27. /* Operation Mode Strap Override */
  28. #define MII_KSZPHY_OMSO 0x16
  29. #define KSZPHY_OMSO_B_CAST_OFF (1 << 9)
  30. #define KSZPHY_OMSO_RMII_OVERRIDE (1 << 1)
  31. #define KSZPHY_OMSO_MII_OVERRIDE (1 << 0)
  32. /* general Interrupt control/status reg in vendor specific block. */
  33. #define MII_KSZPHY_INTCS 0x1B
  34. #define KSZPHY_INTCS_JABBER (1 << 15)
  35. #define KSZPHY_INTCS_RECEIVE_ERR (1 << 14)
  36. #define KSZPHY_INTCS_PAGE_RECEIVE (1 << 13)
  37. #define KSZPHY_INTCS_PARELLEL (1 << 12)
  38. #define KSZPHY_INTCS_LINK_PARTNER_ACK (1 << 11)
  39. #define KSZPHY_INTCS_LINK_DOWN (1 << 10)
  40. #define KSZPHY_INTCS_REMOTE_FAULT (1 << 9)
  41. #define KSZPHY_INTCS_LINK_UP (1 << 8)
  42. #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
  43. KSZPHY_INTCS_LINK_DOWN)
  44. /* general PHY control reg in vendor specific block. */
  45. #define MII_KSZPHY_CTRL 0x1F
  46. /* bitmap of PHY register to set interrupt mode */
  47. #define KSZPHY_CTRL_INT_ACTIVE_HIGH (1 << 9)
  48. #define KSZ9021_CTRL_INT_ACTIVE_HIGH (1 << 14)
  49. #define KS8737_CTRL_INT_ACTIVE_HIGH (1 << 14)
  50. #define KSZ8051_RMII_50MHZ_CLK (1 << 7)
  51. static int kszphy_ack_interrupt(struct phy_device *phydev)
  52. {
  53. /* bit[7..0] int status, which is a read and clear register. */
  54. int rc;
  55. rc = phy_read(phydev, MII_KSZPHY_INTCS);
  56. return (rc < 0) ? rc : 0;
  57. }
  58. static int kszphy_set_interrupt(struct phy_device *phydev)
  59. {
  60. int temp;
  61. temp = (PHY_INTERRUPT_ENABLED == phydev->interrupts) ?
  62. KSZPHY_INTCS_ALL : 0;
  63. return phy_write(phydev, MII_KSZPHY_INTCS, temp);
  64. }
  65. static int kszphy_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 &= ~KSZPHY_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 ksz9021_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 &= ~KSZ9021_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 ks8737_config_intr(struct phy_device *phydev)
  86. {
  87. int temp, rc;
  88. /* set the interrupt pin active low */
  89. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  90. temp &= ~KS8737_CTRL_INT_ACTIVE_HIGH;
  91. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  92. rc = kszphy_set_interrupt(phydev);
  93. return rc < 0 ? rc : 0;
  94. }
  95. static int kszphy_config_init(struct phy_device *phydev)
  96. {
  97. return 0;
  98. }
  99. static int ksz8021_config_init(struct phy_device *phydev)
  100. {
  101. const u16 val = KSZPHY_OMSO_B_CAST_OFF | KSZPHY_OMSO_RMII_OVERRIDE;
  102. phy_write(phydev, MII_KSZPHY_OMSO, val);
  103. return 0;
  104. }
  105. static int ks8051_config_init(struct phy_device *phydev)
  106. {
  107. int regval;
  108. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  109. regval = phy_read(phydev, MII_KSZPHY_CTRL);
  110. regval |= KSZ8051_RMII_50MHZ_CLK;
  111. phy_write(phydev, MII_KSZPHY_CTRL, regval);
  112. }
  113. return 0;
  114. }
  115. #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
  116. #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX (1 << 6)
  117. #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED (1 << 4)
  118. int ksz8873mll_read_status(struct phy_device *phydev)
  119. {
  120. int regval;
  121. /* dummy read */
  122. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  123. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  124. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
  125. phydev->duplex = DUPLEX_HALF;
  126. else
  127. phydev->duplex = DUPLEX_FULL;
  128. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
  129. phydev->speed = SPEED_10;
  130. else
  131. phydev->speed = SPEED_100;
  132. phydev->link = 1;
  133. phydev->pause = phydev->asym_pause = 0;
  134. return 0;
  135. }
  136. static int ksz8873mll_config_aneg(struct phy_device *phydev)
  137. {
  138. return 0;
  139. }
  140. static struct phy_driver ksphy_driver[] = {
  141. {
  142. .phy_id = PHY_ID_KS8737,
  143. .phy_id_mask = 0x00fffff0,
  144. .name = "Micrel KS8737",
  145. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  146. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  147. .config_init = kszphy_config_init,
  148. .config_aneg = genphy_config_aneg,
  149. .read_status = genphy_read_status,
  150. .ack_interrupt = kszphy_ack_interrupt,
  151. .config_intr = ks8737_config_intr,
  152. .driver = { .owner = THIS_MODULE,},
  153. }, {
  154. .phy_id = PHY_ID_KSZ8021,
  155. .phy_id_mask = 0x00ffffff,
  156. .name = "Micrel KSZ8021 or KSZ8031",
  157. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  158. SUPPORTED_Asym_Pause),
  159. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  160. .config_init = ksz8021_config_init,
  161. .config_aneg = genphy_config_aneg,
  162. .read_status = genphy_read_status,
  163. .ack_interrupt = kszphy_ack_interrupt,
  164. .config_intr = kszphy_config_intr,
  165. .driver = { .owner = THIS_MODULE,},
  166. }, {
  167. .phy_id = PHY_ID_KSZ8041,
  168. .phy_id_mask = 0x00fffff0,
  169. .name = "Micrel KSZ8041",
  170. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  171. | SUPPORTED_Asym_Pause),
  172. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  173. .config_init = kszphy_config_init,
  174. .config_aneg = genphy_config_aneg,
  175. .read_status = genphy_read_status,
  176. .ack_interrupt = kszphy_ack_interrupt,
  177. .config_intr = kszphy_config_intr,
  178. .driver = { .owner = THIS_MODULE,},
  179. }, {
  180. .phy_id = PHY_ID_KSZ8051,
  181. .phy_id_mask = 0x00fffff0,
  182. .name = "Micrel KSZ8051",
  183. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  184. | SUPPORTED_Asym_Pause),
  185. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  186. .config_init = ks8051_config_init,
  187. .config_aneg = genphy_config_aneg,
  188. .read_status = genphy_read_status,
  189. .ack_interrupt = kszphy_ack_interrupt,
  190. .config_intr = kszphy_config_intr,
  191. .driver = { .owner = THIS_MODULE,},
  192. }, {
  193. .phy_id = PHY_ID_KSZ8001,
  194. .name = "Micrel KSZ8001 or KS8721",
  195. .phy_id_mask = 0x00ffffff,
  196. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  197. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  198. .config_init = kszphy_config_init,
  199. .config_aneg = genphy_config_aneg,
  200. .read_status = genphy_read_status,
  201. .ack_interrupt = kszphy_ack_interrupt,
  202. .config_intr = kszphy_config_intr,
  203. .driver = { .owner = THIS_MODULE,},
  204. }, {
  205. .phy_id = PHY_ID_KSZ8081,
  206. .name = "Micrel KSZ8081 or KSZ8091",
  207. .phy_id_mask = 0x00fffff0,
  208. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  209. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  210. .config_init = kszphy_config_init,
  211. .config_aneg = genphy_config_aneg,
  212. .read_status = genphy_read_status,
  213. .ack_interrupt = kszphy_ack_interrupt,
  214. .config_intr = kszphy_config_intr,
  215. .driver = { .owner = THIS_MODULE,},
  216. }, {
  217. .phy_id = PHY_ID_KSZ8061,
  218. .name = "Micrel KSZ8061",
  219. .phy_id_mask = 0x00fffff0,
  220. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  221. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  222. .config_init = kszphy_config_init,
  223. .config_aneg = genphy_config_aneg,
  224. .read_status = genphy_read_status,
  225. .ack_interrupt = kszphy_ack_interrupt,
  226. .config_intr = kszphy_config_intr,
  227. .driver = { .owner = THIS_MODULE,},
  228. }, {
  229. .phy_id = PHY_ID_KSZ9021,
  230. .phy_id_mask = 0x000ffffe,
  231. .name = "Micrel KSZ9021 Gigabit PHY",
  232. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause),
  233. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  234. .config_init = kszphy_config_init,
  235. .config_aneg = genphy_config_aneg,
  236. .read_status = genphy_read_status,
  237. .ack_interrupt = kszphy_ack_interrupt,
  238. .config_intr = ksz9021_config_intr,
  239. .driver = { .owner = THIS_MODULE, },
  240. }, {
  241. .phy_id = PHY_ID_KSZ9031,
  242. .phy_id_mask = 0x00fffff0,
  243. .name = "Micrel KSZ9031 Gigabit PHY",
  244. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause
  245. | SUPPORTED_Asym_Pause),
  246. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  247. .config_init = kszphy_config_init,
  248. .config_aneg = genphy_config_aneg,
  249. .read_status = genphy_read_status,
  250. .ack_interrupt = kszphy_ack_interrupt,
  251. .config_intr = ksz9021_config_intr,
  252. .driver = { .owner = THIS_MODULE, },
  253. }, {
  254. .phy_id = PHY_ID_KSZ8873MLL,
  255. .phy_id_mask = 0x00fffff0,
  256. .name = "Micrel KSZ8873MLL Switch",
  257. .features = (SUPPORTED_Pause | SUPPORTED_Asym_Pause),
  258. .flags = PHY_HAS_MAGICANEG,
  259. .config_init = kszphy_config_init,
  260. .config_aneg = ksz8873mll_config_aneg,
  261. .read_status = ksz8873mll_read_status,
  262. .driver = { .owner = THIS_MODULE, },
  263. }, {
  264. .phy_id = PHY_ID_KSZ886X,
  265. .phy_id_mask = 0x00fffff0,
  266. .name = "Micrel KSZ886X Switch",
  267. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  268. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  269. .config_init = kszphy_config_init,
  270. .config_aneg = genphy_config_aneg,
  271. .read_status = genphy_read_status,
  272. .driver = { .owner = THIS_MODULE, },
  273. } };
  274. static int __init ksphy_init(void)
  275. {
  276. return phy_drivers_register(ksphy_driver,
  277. ARRAY_SIZE(ksphy_driver));
  278. }
  279. static void __exit ksphy_exit(void)
  280. {
  281. phy_drivers_unregister(ksphy_driver,
  282. ARRAY_SIZE(ksphy_driver));
  283. }
  284. module_init(ksphy_init);
  285. module_exit(ksphy_exit);
  286. MODULE_DESCRIPTION("Micrel PHY driver");
  287. MODULE_AUTHOR("David J. Choi");
  288. MODULE_LICENSE("GPL");
  289. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  290. { PHY_ID_KSZ9021, 0x000ffffe },
  291. { PHY_ID_KSZ9031, 0x00fffff0 },
  292. { PHY_ID_KSZ8001, 0x00ffffff },
  293. { PHY_ID_KS8737, 0x00fffff0 },
  294. { PHY_ID_KSZ8021, 0x00ffffff },
  295. { PHY_ID_KSZ8041, 0x00fffff0 },
  296. { PHY_ID_KSZ8051, 0x00fffff0 },
  297. { PHY_ID_KSZ8061, 0x00fffff0 },
  298. { PHY_ID_KSZ8081, 0x00fffff0 },
  299. { PHY_ID_KSZ8873MLL, 0x00fffff0 },
  300. { PHY_ID_KSZ886X, 0x00fffff0 },
  301. { }
  302. };
  303. MODULE_DEVICE_TABLE(mdio, micrel_tbl);