micrel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. #include <linux/of.h>
  28. /* Operation Mode Strap Override */
  29. #define MII_KSZPHY_OMSO 0x16
  30. #define KSZPHY_OMSO_B_CAST_OFF (1 << 9)
  31. #define KSZPHY_OMSO_RMII_OVERRIDE (1 << 1)
  32. #define KSZPHY_OMSO_MII_OVERRIDE (1 << 0)
  33. /* general Interrupt control/status reg in vendor specific block. */
  34. #define MII_KSZPHY_INTCS 0x1B
  35. #define KSZPHY_INTCS_JABBER (1 << 15)
  36. #define KSZPHY_INTCS_RECEIVE_ERR (1 << 14)
  37. #define KSZPHY_INTCS_PAGE_RECEIVE (1 << 13)
  38. #define KSZPHY_INTCS_PARELLEL (1 << 12)
  39. #define KSZPHY_INTCS_LINK_PARTNER_ACK (1 << 11)
  40. #define KSZPHY_INTCS_LINK_DOWN (1 << 10)
  41. #define KSZPHY_INTCS_REMOTE_FAULT (1 << 9)
  42. #define KSZPHY_INTCS_LINK_UP (1 << 8)
  43. #define KSZPHY_INTCS_ALL (KSZPHY_INTCS_LINK_UP |\
  44. KSZPHY_INTCS_LINK_DOWN)
  45. /* general PHY control reg in vendor specific block. */
  46. #define MII_KSZPHY_CTRL 0x1F
  47. /* bitmap of PHY register to set interrupt mode */
  48. #define KSZPHY_CTRL_INT_ACTIVE_HIGH (1 << 9)
  49. #define KSZ9021_CTRL_INT_ACTIVE_HIGH (1 << 14)
  50. #define KS8737_CTRL_INT_ACTIVE_HIGH (1 << 14)
  51. #define KSZ8051_RMII_50MHZ_CLK (1 << 7)
  52. /* Write/read to/from extended registers */
  53. #define MII_KSZPHY_EXTREG 0x0b
  54. #define KSZPHY_EXTREG_WRITE 0x8000
  55. #define MII_KSZPHY_EXTREG_WRITE 0x0c
  56. #define MII_KSZPHY_EXTREG_READ 0x0d
  57. /* Extended registers */
  58. #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104
  59. #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105
  60. #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106
  61. #define PS_TO_REG 200
  62. static int ksz_config_flags(struct phy_device *phydev)
  63. {
  64. int regval;
  65. if (phydev->dev_flags & MICREL_PHY_50MHZ_CLK) {
  66. regval = phy_read(phydev, MII_KSZPHY_CTRL);
  67. regval |= KSZ8051_RMII_50MHZ_CLK;
  68. return phy_write(phydev, MII_KSZPHY_CTRL, regval);
  69. }
  70. return 0;
  71. }
  72. static int kszphy_extended_write(struct phy_device *phydev,
  73. u32 regnum, u16 val)
  74. {
  75. phy_write(phydev, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | regnum);
  76. return phy_write(phydev, MII_KSZPHY_EXTREG_WRITE, val);
  77. }
  78. static int kszphy_extended_read(struct phy_device *phydev,
  79. u32 regnum)
  80. {
  81. phy_write(phydev, MII_KSZPHY_EXTREG, regnum);
  82. return phy_read(phydev, MII_KSZPHY_EXTREG_READ);
  83. }
  84. static int kszphy_ack_interrupt(struct phy_device *phydev)
  85. {
  86. /* bit[7..0] int status, which is a read and clear register. */
  87. int rc;
  88. rc = phy_read(phydev, MII_KSZPHY_INTCS);
  89. return (rc < 0) ? rc : 0;
  90. }
  91. static int kszphy_set_interrupt(struct phy_device *phydev)
  92. {
  93. int temp;
  94. temp = (PHY_INTERRUPT_ENABLED == phydev->interrupts) ?
  95. KSZPHY_INTCS_ALL : 0;
  96. return phy_write(phydev, MII_KSZPHY_INTCS, temp);
  97. }
  98. static int kszphy_config_intr(struct phy_device *phydev)
  99. {
  100. int temp, rc;
  101. /* set the interrupt pin active low */
  102. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  103. temp &= ~KSZPHY_CTRL_INT_ACTIVE_HIGH;
  104. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  105. rc = kszphy_set_interrupt(phydev);
  106. return rc < 0 ? rc : 0;
  107. }
  108. static int ksz9021_config_intr(struct phy_device *phydev)
  109. {
  110. int temp, rc;
  111. /* set the interrupt pin active low */
  112. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  113. temp &= ~KSZ9021_CTRL_INT_ACTIVE_HIGH;
  114. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  115. rc = kszphy_set_interrupt(phydev);
  116. return rc < 0 ? rc : 0;
  117. }
  118. static int ks8737_config_intr(struct phy_device *phydev)
  119. {
  120. int temp, rc;
  121. /* set the interrupt pin active low */
  122. temp = phy_read(phydev, MII_KSZPHY_CTRL);
  123. temp &= ~KS8737_CTRL_INT_ACTIVE_HIGH;
  124. phy_write(phydev, MII_KSZPHY_CTRL, temp);
  125. rc = kszphy_set_interrupt(phydev);
  126. return rc < 0 ? rc : 0;
  127. }
  128. static int kszphy_config_init(struct phy_device *phydev)
  129. {
  130. return 0;
  131. }
  132. static int ksz8021_config_init(struct phy_device *phydev)
  133. {
  134. int rc;
  135. const u16 val = KSZPHY_OMSO_B_CAST_OFF | KSZPHY_OMSO_RMII_OVERRIDE;
  136. phy_write(phydev, MII_KSZPHY_OMSO, val);
  137. rc = ksz_config_flags(phydev);
  138. return rc < 0 ? rc : 0;
  139. }
  140. static int ks8051_config_init(struct phy_device *phydev)
  141. {
  142. int rc;
  143. rc = ksz_config_flags(phydev);
  144. return rc < 0 ? rc : 0;
  145. }
  146. static int ksz9021_load_values_from_of(struct phy_device *phydev,
  147. struct device_node *of_node, u16 reg,
  148. char *field1, char *field2,
  149. char *field3, char *field4)
  150. {
  151. int val1 = -1;
  152. int val2 = -2;
  153. int val3 = -3;
  154. int val4 = -4;
  155. int newval;
  156. int matches = 0;
  157. if (!of_property_read_u32(of_node, field1, &val1))
  158. matches++;
  159. if (!of_property_read_u32(of_node, field2, &val2))
  160. matches++;
  161. if (!of_property_read_u32(of_node, field3, &val3))
  162. matches++;
  163. if (!of_property_read_u32(of_node, field4, &val4))
  164. matches++;
  165. if (!matches)
  166. return 0;
  167. if (matches < 4)
  168. newval = kszphy_extended_read(phydev, reg);
  169. else
  170. newval = 0;
  171. if (val1 != -1)
  172. newval = ((newval & 0xfff0) | ((val1 / PS_TO_REG) & 0xf) << 0);
  173. if (val2 != -1)
  174. newval = ((newval & 0xff0f) | ((val2 / PS_TO_REG) & 0xf) << 4);
  175. if (val3 != -1)
  176. newval = ((newval & 0xf0ff) | ((val3 / PS_TO_REG) & 0xf) << 8);
  177. if (val4 != -1)
  178. newval = ((newval & 0x0fff) | ((val4 / PS_TO_REG) & 0xf) << 12);
  179. return kszphy_extended_write(phydev, reg, newval);
  180. }
  181. static int ksz9021_config_init(struct phy_device *phydev)
  182. {
  183. struct device *dev = &phydev->dev;
  184. struct device_node *of_node = dev->of_node;
  185. if (!of_node && dev->parent->of_node)
  186. of_node = dev->parent->of_node;
  187. if (of_node) {
  188. ksz9021_load_values_from_of(phydev, of_node,
  189. MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
  190. "txen-skew-ps", "txc-skew-ps",
  191. "rxdv-skew-ps", "rxc-skew-ps");
  192. ksz9021_load_values_from_of(phydev, of_node,
  193. MII_KSZPHY_RX_DATA_PAD_SKEW,
  194. "rxd0-skew-ps", "rxd1-skew-ps",
  195. "rxd2-skew-ps", "rxd3-skew-ps");
  196. ksz9021_load_values_from_of(phydev, of_node,
  197. MII_KSZPHY_TX_DATA_PAD_SKEW,
  198. "txd0-skew-ps", "txd1-skew-ps",
  199. "txd2-skew-ps", "txd3-skew-ps");
  200. }
  201. return 0;
  202. }
  203. #define KSZ8873MLL_GLOBAL_CONTROL_4 0x06
  204. #define KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX (1 << 6)
  205. #define KSZ8873MLL_GLOBAL_CONTROL_4_SPEED (1 << 4)
  206. static int ksz8873mll_read_status(struct phy_device *phydev)
  207. {
  208. int regval;
  209. /* dummy read */
  210. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  211. regval = phy_read(phydev, KSZ8873MLL_GLOBAL_CONTROL_4);
  212. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_DUPLEX)
  213. phydev->duplex = DUPLEX_HALF;
  214. else
  215. phydev->duplex = DUPLEX_FULL;
  216. if (regval & KSZ8873MLL_GLOBAL_CONTROL_4_SPEED)
  217. phydev->speed = SPEED_10;
  218. else
  219. phydev->speed = SPEED_100;
  220. phydev->link = 1;
  221. phydev->pause = phydev->asym_pause = 0;
  222. return 0;
  223. }
  224. static int ksz8873mll_config_aneg(struct phy_device *phydev)
  225. {
  226. return 0;
  227. }
  228. static struct phy_driver ksphy_driver[] = {
  229. {
  230. .phy_id = PHY_ID_KS8737,
  231. .phy_id_mask = 0x00fffff0,
  232. .name = "Micrel KS8737",
  233. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  234. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  235. .config_init = kszphy_config_init,
  236. .config_aneg = genphy_config_aneg,
  237. .read_status = genphy_read_status,
  238. .ack_interrupt = kszphy_ack_interrupt,
  239. .config_intr = ks8737_config_intr,
  240. .driver = { .owner = THIS_MODULE,},
  241. }, {
  242. .phy_id = PHY_ID_KSZ8021,
  243. .phy_id_mask = 0x00ffffff,
  244. .name = "Micrel KSZ8021 or KSZ8031",
  245. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  246. SUPPORTED_Asym_Pause),
  247. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  248. .config_init = ksz8021_config_init,
  249. .config_aneg = genphy_config_aneg,
  250. .read_status = genphy_read_status,
  251. .ack_interrupt = kszphy_ack_interrupt,
  252. .config_intr = kszphy_config_intr,
  253. .driver = { .owner = THIS_MODULE,},
  254. }, {
  255. .phy_id = PHY_ID_KSZ8031,
  256. .phy_id_mask = 0x00ffffff,
  257. .name = "Micrel KSZ8031",
  258. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  259. SUPPORTED_Asym_Pause),
  260. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  261. .config_init = ksz8021_config_init,
  262. .config_aneg = genphy_config_aneg,
  263. .read_status = genphy_read_status,
  264. .ack_interrupt = kszphy_ack_interrupt,
  265. .config_intr = kszphy_config_intr,
  266. .driver = { .owner = THIS_MODULE,},
  267. }, {
  268. .phy_id = PHY_ID_KSZ8041,
  269. .phy_id_mask = 0x00fffff0,
  270. .name = "Micrel KSZ8041",
  271. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  272. | SUPPORTED_Asym_Pause),
  273. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  274. .config_init = kszphy_config_init,
  275. .config_aneg = genphy_config_aneg,
  276. .read_status = genphy_read_status,
  277. .ack_interrupt = kszphy_ack_interrupt,
  278. .config_intr = kszphy_config_intr,
  279. .driver = { .owner = THIS_MODULE,},
  280. }, {
  281. .phy_id = PHY_ID_KSZ8051,
  282. .phy_id_mask = 0x00fffff0,
  283. .name = "Micrel KSZ8051",
  284. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  285. | SUPPORTED_Asym_Pause),
  286. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  287. .config_init = ks8051_config_init,
  288. .config_aneg = genphy_config_aneg,
  289. .read_status = genphy_read_status,
  290. .ack_interrupt = kszphy_ack_interrupt,
  291. .config_intr = kszphy_config_intr,
  292. .driver = { .owner = THIS_MODULE,},
  293. }, {
  294. .phy_id = PHY_ID_KSZ8001,
  295. .name = "Micrel KSZ8001 or KS8721",
  296. .phy_id_mask = 0x00ffffff,
  297. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  298. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  299. .config_init = kszphy_config_init,
  300. .config_aneg = genphy_config_aneg,
  301. .read_status = genphy_read_status,
  302. .ack_interrupt = kszphy_ack_interrupt,
  303. .config_intr = kszphy_config_intr,
  304. .driver = { .owner = THIS_MODULE,},
  305. }, {
  306. .phy_id = PHY_ID_KSZ8081,
  307. .name = "Micrel KSZ8081 or KSZ8091",
  308. .phy_id_mask = 0x00fffff0,
  309. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  310. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  311. .config_init = kszphy_config_init,
  312. .config_aneg = genphy_config_aneg,
  313. .read_status = genphy_read_status,
  314. .ack_interrupt = kszphy_ack_interrupt,
  315. .config_intr = kszphy_config_intr,
  316. .driver = { .owner = THIS_MODULE,},
  317. }, {
  318. .phy_id = PHY_ID_KSZ8061,
  319. .name = "Micrel KSZ8061",
  320. .phy_id_mask = 0x00fffff0,
  321. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  322. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  323. .config_init = kszphy_config_init,
  324. .config_aneg = genphy_config_aneg,
  325. .read_status = genphy_read_status,
  326. .ack_interrupt = kszphy_ack_interrupt,
  327. .config_intr = kszphy_config_intr,
  328. .driver = { .owner = THIS_MODULE,},
  329. }, {
  330. .phy_id = PHY_ID_KSZ9021,
  331. .phy_id_mask = 0x000ffffe,
  332. .name = "Micrel KSZ9021 Gigabit PHY",
  333. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause),
  334. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  335. .config_init = ksz9021_config_init,
  336. .config_aneg = genphy_config_aneg,
  337. .read_status = genphy_read_status,
  338. .ack_interrupt = kszphy_ack_interrupt,
  339. .config_intr = ksz9021_config_intr,
  340. .driver = { .owner = THIS_MODULE, },
  341. }, {
  342. .phy_id = PHY_ID_KSZ9031,
  343. .phy_id_mask = 0x00fffff0,
  344. .name = "Micrel KSZ9031 Gigabit PHY",
  345. .features = (PHY_GBIT_FEATURES | SUPPORTED_Pause
  346. | SUPPORTED_Asym_Pause),
  347. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  348. .config_init = kszphy_config_init,
  349. .config_aneg = genphy_config_aneg,
  350. .read_status = genphy_read_status,
  351. .ack_interrupt = kszphy_ack_interrupt,
  352. .config_intr = ksz9021_config_intr,
  353. .driver = { .owner = THIS_MODULE, },
  354. }, {
  355. .phy_id = PHY_ID_KSZ8873MLL,
  356. .phy_id_mask = 0x00fffff0,
  357. .name = "Micrel KSZ8873MLL Switch",
  358. .features = (SUPPORTED_Pause | SUPPORTED_Asym_Pause),
  359. .flags = PHY_HAS_MAGICANEG,
  360. .config_init = kszphy_config_init,
  361. .config_aneg = ksz8873mll_config_aneg,
  362. .read_status = ksz8873mll_read_status,
  363. .driver = { .owner = THIS_MODULE, },
  364. }, {
  365. .phy_id = PHY_ID_KSZ886X,
  366. .phy_id_mask = 0x00fffff0,
  367. .name = "Micrel KSZ886X Switch",
  368. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause),
  369. .flags = PHY_HAS_MAGICANEG | PHY_HAS_INTERRUPT,
  370. .config_init = kszphy_config_init,
  371. .config_aneg = genphy_config_aneg,
  372. .read_status = genphy_read_status,
  373. .driver = { .owner = THIS_MODULE, },
  374. } };
  375. static int __init ksphy_init(void)
  376. {
  377. return phy_drivers_register(ksphy_driver,
  378. ARRAY_SIZE(ksphy_driver));
  379. }
  380. static void __exit ksphy_exit(void)
  381. {
  382. phy_drivers_unregister(ksphy_driver,
  383. ARRAY_SIZE(ksphy_driver));
  384. }
  385. module_init(ksphy_init);
  386. module_exit(ksphy_exit);
  387. MODULE_DESCRIPTION("Micrel PHY driver");
  388. MODULE_AUTHOR("David J. Choi");
  389. MODULE_LICENSE("GPL");
  390. static struct mdio_device_id __maybe_unused micrel_tbl[] = {
  391. { PHY_ID_KSZ9021, 0x000ffffe },
  392. { PHY_ID_KSZ9031, 0x00fffff0 },
  393. { PHY_ID_KSZ8001, 0x00ffffff },
  394. { PHY_ID_KS8737, 0x00fffff0 },
  395. { PHY_ID_KSZ8021, 0x00ffffff },
  396. { PHY_ID_KSZ8031, 0x00ffffff },
  397. { PHY_ID_KSZ8041, 0x00fffff0 },
  398. { PHY_ID_KSZ8051, 0x00fffff0 },
  399. { PHY_ID_KSZ8061, 0x00fffff0 },
  400. { PHY_ID_KSZ8081, 0x00fffff0 },
  401. { PHY_ID_KSZ8873MLL, 0x00fffff0 },
  402. { PHY_ID_KSZ886X, 0x00fffff0 },
  403. { }
  404. };
  405. MODULE_DEVICE_TABLE(mdio, micrel_tbl);