micrel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 ksz_config_flags(struct phy_device *phydev)
  52. {
  53. int regval;
  54. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  55. regval = phy_read(phydev, MII_KSZPHY_CTRL);
  56. regval |= KSZ8051_RMII_50MHZ_CLK;
  57. return phy_write(phydev, MII_KSZPHY_CTRL, regval);
  58. }
  59. return 0;
  60. }
  61. static int kszphy_ack_interrupt(struct phy_device *phydev)
  62. {
  63. /* bit[7..0] int status, which is a read and clear register. */
  64. int rc;
  65. rc = phy_read(phydev, MII_KSZPHY_INTCS);
  66. return (rc < 0) ? rc : 0;
  67. }
  68. static int kszphy_set_interrupt(struct phy_device *phydev)
  69. {
  70. int temp;
  71. temp = (PHY_INTERRUPT_ENABLED == phydev->interrupts) ?
  72. KSZPHY_INTCS_ALL : 0;
  73. return phy_write(phydev, MII_KSZPHY_INTCS, temp);
  74. }
  75. static int kszphy_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 &= ~KSZPHY_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 ksz9021_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 &= ~KSZ9021_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 ks8737_config_intr(struct phy_device *phydev)
  96. {
  97. int temp, rc;
  98. /* set the interrupt pin active low */
  99. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  100. temp &= ~KS8737_CTRL_INT_ACTIVE_HIGH;
  101. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  102. rc = kszphy_set_interrupt(phydev);
  103. return rc < 0 ? rc : 0;
  104. }
  105. static int kszphy_config_init(struct phy_device *phydev)
  106. {
  107. return 0;
  108. }
  109. static int ksz8021_config_init(struct phy_device *phydev)
  110. {
  111. int rc;
  112. const u16 val = KSZPHY_OMSO_B_CAST_OFF | KSZPHY_OMSO_RMII_OVERRIDE;
  113. phy_write(phydev, MII_KSZPHY_OMSO, val);
  114. rc = ksz_config_flags(phydev);
  115. return rc < 0 ? rc : 0;
  116. }
  117. static int ks8051_config_init(struct phy_device *phydev)
  118. {
  119. int rc;
  120. rc = ksz_config_flags(phydev);
  121. return rc < 0 ? rc : 0;
  122. }
  123. #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
  124. #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX (1 << 6)
  125. #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED (1 << 4)
  126. int ksz8873mll_read_status(struct phy_device *phydev)
  127. {
  128. int regval;
  129. /* dummy read */
  130. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  131. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  132. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
  133. phydev->duplex = DUPLEX_HALF;
  134. else
  135. phydev->duplex = DUPLEX_FULL;
  136. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
  137. phydev->speed = SPEED_10;
  138. else
  139. phydev->speed = SPEED_100;
  140. phydev->link = 1;
  141. phydev->pause = phydev->asym_pause = 0;
  142. return 0;
  143. }
  144. static int ksz8873mll_config_aneg(struct phy_device *phydev)
  145. {
  146. return 0;
  147. }
  148. static struct phy_driver ksphy_driver[] = {
  149. {
  150. .phy_id = PHY_ID_KS8737,
  151. .phy_id_mask = 0x00fffff0,
  152. .name = "Micrel KS8737",
  153. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  154. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  155. .config_init = kszphy_config_init,
  156. .config_aneg = genphy_config_aneg,
  157. .read_status = genphy_read_status,
  158. .ack_interrupt = kszphy_ack_interrupt,
  159. .config_intr = ks8737_config_intr,
  160. .driver = { .owner = THIS_MODULE,},
  161. }, {
  162. .phy_id = PHY_ID_KSZ8021,
  163. .phy_id_mask = 0x00ffffff,
  164. .name = "Micrel KSZ8021 or KSZ8031",
  165. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  166. SUPPORTED_Asym_Pause),
  167. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  168. .config_init = ksz8021_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_KSZ8031,
  176. .phy_id_mask = 0x00ffffff,
  177. .name = "Micrel KSZ8031",
  178. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  179. SUPPORTED_Asym_Pause),
  180. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  181. .config_init = ksz8021_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_KSZ8041,
  189. .phy_id_mask = 0x00fffff0,
  190. .name = "Micrel KSZ8041",
  191. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  192. | SUPPORTED_Asym_Pause),
  193. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  194. .config_init = kszphy_config_init,
  195. .config_aneg = genphy_config_aneg,
  196. .read_status = genphy_read_status,
  197. .ack_interrupt = kszphy_ack_interrupt,
  198. .config_intr = kszphy_config_intr,
  199. .driver = { .owner = THIS_MODULE,},
  200. }, {
  201. .phy_id = PHY_ID_KSZ8051,
  202. .phy_id_mask = 0x00fffff0,
  203. .name = "Micrel KSZ8051",
  204. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  205. | SUPPORTED_Asym_Pause),
  206. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  207. .config_init = ks8051_config_init,
  208. .config_aneg = genphy_config_aneg,
  209. .read_status = genphy_read_status,
  210. .ack_interrupt = kszphy_ack_interrupt,
  211. .config_intr = kszphy_config_intr,
  212. .driver = { .owner = THIS_MODULE,},
  213. }, {
  214. .phy_id = PHY_ID_KSZ8001,
  215. .name = "Micrel KSZ8001 or KS8721",
  216. .phy_id_mask = 0x00ffffff,
  217. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  218. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  219. .config_init = kszphy_config_init,
  220. .config_aneg = genphy_config_aneg,
  221. .read_status = genphy_read_status,
  222. .ack_interrupt = kszphy_ack_interrupt,
  223. .config_intr = kszphy_config_intr,
  224. .driver = { .owner = THIS_MODULE,},
  225. }, {
  226. .phy_id = PHY_ID_KSZ8081,
  227. .name = "Micrel KSZ8081 or KSZ8091",
  228. .phy_id_mask = 0x00fffff0,
  229. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  230. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  231. .config_init = kszphy_config_init,
  232. .config_aneg = genphy_config_aneg,
  233. .read_status = genphy_read_status,
  234. .ack_interrupt = kszphy_ack_interrupt,
  235. .config_intr = kszphy_config_intr,
  236. .driver = { .owner = THIS_MODULE,},
  237. }, {
  238. .phy_id = PHY_ID_KSZ8061,
  239. .name = "Micrel KSZ8061",
  240. .phy_id_mask = 0x00fffff0,
  241. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  242. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  243. .config_init = kszphy_config_init,
  244. .config_aneg = genphy_config_aneg,
  245. .read_status = genphy_read_status,
  246. .ack_interrupt = kszphy_ack_interrupt,
  247. .config_intr = kszphy_config_intr,
  248. .driver = { .owner = THIS_MODULE,},
  249. }, {
  250. .phy_id = PHY_ID_KSZ9021,
  251. .phy_id_mask = 0x000ffffe,
  252. .name = "Micrel KSZ9021 Gigabit PHY",
  253. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause),
  254. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  255. .config_init = kszphy_config_init,
  256. .config_aneg = genphy_config_aneg,
  257. .read_status = genphy_read_status,
  258. .ack_interrupt = kszphy_ack_interrupt,
  259. .config_intr = ksz9021_config_intr,
  260. .driver = { .owner = THIS_MODULE, },
  261. }, {
  262. .phy_id = PHY_ID_KSZ9031,
  263. .phy_id_mask = 0x00fffff0,
  264. .name = "Micrel KSZ9031 Gigabit PHY",
  265. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause
  266. | SUPPORTED_Asym_Pause),
  267. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  268. .config_init = kszphy_config_init,
  269. .config_aneg = genphy_config_aneg,
  270. .read_status = genphy_read_status,
  271. .ack_interrupt = kszphy_ack_interrupt,
  272. .config_intr = ksz9021_config_intr,
  273. .driver = { .owner = THIS_MODULE, },
  274. }, {
  275. .phy_id = PHY_ID_KSZ8873MLL,
  276. .phy_id_mask = 0x00fffff0,
  277. .name = "Micrel KSZ8873MLL Switch",
  278. .features = (SUPPORTED_Pause | SUPPORTED_Asym_Pause),
  279. .flags = PHY_HAS_MAGICANEG,
  280. .config_init = kszphy_config_init,
  281. .config_aneg = ksz8873mll_config_aneg,
  282. .read_status = ksz8873mll_read_status,
  283. .driver = { .owner = THIS_MODULE, },
  284. }, {
  285. .phy_id = PHY_ID_KSZ886X,
  286. .phy_id_mask = 0x00fffff0,
  287. .name = "Micrel KSZ886X Switch",
  288. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  289. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  290. .config_init = kszphy_config_init,
  291. .config_aneg = genphy_config_aneg,
  292. .read_status = genphy_read_status,
  293. .driver = { .owner = THIS_MODULE, },
  294. } };
  295. static int __init ksphy_init(void)
  296. {
  297. return phy_drivers_register(ksphy_driver,
  298. ARRAY_SIZE(ksphy_driver));
  299. }
  300. static void __exit ksphy_exit(void)
  301. {
  302. phy_drivers_unregister(ksphy_driver,
  303. ARRAY_SIZE(ksphy_driver));
  304. }
  305. module_init(ksphy_init);
  306. module_exit(ksphy_exit);
  307. MODULE_DESCRIPTION("Micrel PHY driver");
  308. MODULE_AUTHOR("David J. Choi");
  309. MODULE_LICENSE("GPL");
  310. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  311. { PHY_ID_KSZ9021, 0x000ffffe },
  312. { PHY_ID_KSZ9031, 0x00fffff0 },
  313. { PHY_ID_KSZ8001, 0x00ffffff },
  314. { PHY_ID_KS8737, 0x00fffff0 },
  315. { PHY_ID_KSZ8021, 0x00ffffff },
  316. { PHY_ID_KSZ8031, 0x00ffffff },
  317. { PHY_ID_KSZ8041, 0x00fffff0 },
  318. { PHY_ID_KSZ8051, 0x00fffff0 },
  319. { PHY_ID_KSZ8061, 0x00fffff0 },
  320. { PHY_ID_KSZ8081, 0x00fffff0 },
  321. { PHY_ID_KSZ8873MLL, 0x00fffff0 },
  322. { PHY_ID_KSZ886X, 0x00fffff0 },
  323. { }
  324. };
  325. MODULE_DEVICE_TABLE(mdio, micrel_tbl);