vitesse.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Driver for Vitesse PHYs
  3. *
  4. * Author: Kriston Carson
  5. *
  6. * Copyright (c) 2005, 2009, 2011 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mii.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/phy.h>
  19. /* Vitesse Extended Page Magic Register(s) */
  20. #define MII_VSC82X4_EXT_PAGE_16E 0x10
  21. #define MII_VSC82X4_EXT_PAGE_17E 0x11
  22. #define MII_VSC82X4_EXT_PAGE_18E 0x12
  23. /* Vitesse Extended Control Register 1 */
  24. #define MII_VSC8244_EXT_CON1 0x17
  25. #define MII_VSC8244_EXTCON1_INIT 0x0000
  26. #define MII_VSC8244_EXTCON1_TX_SKEW_MASK 0x0c00
  27. #define MII_VSC8244_EXTCON1_RX_SKEW_MASK 0x0300
  28. #define MII_VSC8244_EXTCON1_TX_SKEW 0x0800
  29. #define MII_VSC8244_EXTCON1_RX_SKEW 0x0200
  30. /* Vitesse Interrupt Mask Register */
  31. #define MII_VSC8244_IMASK 0x19
  32. #define MII_VSC8244_IMASK_IEN 0x8000
  33. #define MII_VSC8244_IMASK_SPEED 0x4000
  34. #define MII_VSC8244_IMASK_LINK 0x2000
  35. #define MII_VSC8244_IMASK_DUPLEX 0x1000
  36. #define MII_VSC8244_IMASK_MASK 0xf000
  37. #define MII_VSC8221_IMASK_MASK 0xa000
  38. /* Vitesse Interrupt Status Register */
  39. #define MII_VSC8244_ISTAT 0x1a
  40. #define MII_VSC8244_ISTAT_STATUS 0x8000
  41. #define MII_VSC8244_ISTAT_SPEED 0x4000
  42. #define MII_VSC8244_ISTAT_LINK 0x2000
  43. #define MII_VSC8244_ISTAT_DUPLEX 0x1000
  44. /* Vitesse Auxiliary Control/Status Register */
  45. #define MII_VSC8244_AUX_CONSTAT 0x1c
  46. #define MII_VSC8244_AUXCONSTAT_INIT 0x0000
  47. #define MII_VSC8244_AUXCONSTAT_DUPLEX 0x0020
  48. #define MII_VSC8244_AUXCONSTAT_SPEED 0x0018
  49. #define MII_VSC8244_AUXCONSTAT_GBIT 0x0010
  50. #define MII_VSC8244_AUXCONSTAT_100 0x0008
  51. #define MII_VSC8221_AUXCONSTAT_INIT 0x0004 /* need to set this bit? */
  52. #define MII_VSC8221_AUXCONSTAT_RESERVED 0x0004
  53. /* Vitesse Extended Page Access Register */
  54. #define MII_VSC82X4_EXT_PAGE_ACCESS 0x1f
  55. #define PHY_ID_VSC8234 0x000fc620
  56. #define PHY_ID_VSC8244 0x000fc6c0
  57. #define PHY_ID_VSC8574 0x000704a0
  58. #define PHY_ID_VSC8662 0x00070660
  59. #define PHY_ID_VSC8221 0x000fc550
  60. #define PHY_ID_VSC8211 0x000fc4b0
  61. MODULE_DESCRIPTION("Vitesse PHY driver");
  62. MODULE_AUTHOR("Kriston Carson");
  63. MODULE_LICENSE("GPL");
  64. static int vsc824x_add_skew(struct phy_device *phydev)
  65. {
  66. int err;
  67. int extcon;
  68. extcon = phy_read(phydev, MII_VSC8244_EXT_CON1);
  69. if (extcon < 0)
  70. return extcon;
  71. extcon &= ~(MII_VSC8244_EXTCON1_TX_SKEW_MASK |
  72. MII_VSC8244_EXTCON1_RX_SKEW_MASK);
  73. extcon |= (MII_VSC8244_EXTCON1_TX_SKEW |
  74. MII_VSC8244_EXTCON1_RX_SKEW);
  75. err = phy_write(phydev, MII_VSC8244_EXT_CON1, extcon);
  76. return err;
  77. }
  78. static int vsc824x_config_init(struct phy_device *phydev)
  79. {
  80. int err;
  81. err = phy_write(phydev, MII_VSC8244_AUX_CONSTAT,
  82. MII_VSC8244_AUXCONSTAT_INIT);
  83. if (err < 0)
  84. return err;
  85. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  86. err = vsc824x_add_skew(phydev);
  87. return err;
  88. }
  89. static int vsc824x_ack_interrupt(struct phy_device *phydev)
  90. {
  91. int err = 0;
  92. /* Don't bother to ACK the interrupts if interrupts
  93. * are disabled. The 824x cannot clear the interrupts
  94. * if they are disabled.
  95. */
  96. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  97. err = phy_read(phydev, MII_VSC8244_ISTAT);
  98. return (err < 0) ? err : 0;
  99. }
  100. static int vsc82xx_config_intr(struct phy_device *phydev)
  101. {
  102. int err;
  103. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  104. err = phy_write(phydev, MII_VSC8244_IMASK,
  105. (phydev->drv->phy_id == PHY_ID_VSC8234 ||
  106. phydev->drv->phy_id == PHY_ID_VSC8244 ||
  107. phydev->drv->phy_id == PHY_ID_VSC8574) ?
  108. MII_VSC8244_IMASK_MASK :
  109. MII_VSC8221_IMASK_MASK);
  110. else {
  111. /* The Vitesse PHY cannot clear the interrupt
  112. * once it has disabled them, so we clear them first
  113. */
  114. err = phy_read(phydev, MII_VSC8244_ISTAT);
  115. if (err < 0)
  116. return err;
  117. err = phy_write(phydev, MII_VSC8244_IMASK, 0);
  118. }
  119. return err;
  120. }
  121. static int vsc8221_config_init(struct phy_device *phydev)
  122. {
  123. int err;
  124. err = phy_write(phydev, MII_VSC8244_AUX_CONSTAT,
  125. MII_VSC8221_AUXCONSTAT_INIT);
  126. return err;
  127. /* Perhaps we should set EXT_CON1 based on the interface?
  128. * Options are 802.3Z SerDes or SGMII
  129. */
  130. }
  131. /* vsc82x4_config_autocross_enable - Enable auto MDI/MDI-X for forced links
  132. * @phydev: target phy_device struct
  133. *
  134. * Enable auto MDI/MDI-X when in 10/100 forced link speeds by writing
  135. * special values in the VSC8234/VSC8244 extended reserved registers
  136. */
  137. static int vsc82x4_config_autocross_enable(struct phy_device *phydev)
  138. {
  139. int ret;
  140. if (phydev->autoneg == AUTONEG_ENABLE || phydev->speed > SPEED_100)
  141. return 0;
  142. /* map extended registers set 0x10 - 0x1e */
  143. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x52b5);
  144. if (ret >= 0)
  145. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_18E, 0x0012);
  146. if (ret >= 0)
  147. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_17E, 0x2803);
  148. if (ret >= 0)
  149. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_16E, 0x87fa);
  150. /* map standard registers set 0x10 - 0x1e */
  151. if (ret >= 0)
  152. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x0000);
  153. else
  154. phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x0000);
  155. return ret;
  156. }
  157. /* vsc82x4_config_aneg - restart auto-negotiation or write BMCR
  158. * @phydev: target phy_device struct
  159. *
  160. * Description: If auto-negotiation is enabled, we configure the
  161. * advertising, and then restart auto-negotiation. If it is not
  162. * enabled, then we write the BMCR and also start the auto
  163. * MDI/MDI-X feature
  164. */
  165. static int vsc82x4_config_aneg(struct phy_device *phydev)
  166. {
  167. int ret;
  168. /* Enable auto MDI/MDI-X when in 10/100 forced link speeds by
  169. * writing special values in the VSC8234 extended reserved registers
  170. */
  171. if (phydev->autoneg != AUTONEG_ENABLE && phydev->speed <= SPEED_100) {
  172. ret = genphy_setup_forced(phydev);
  173. if (ret < 0) /* error */
  174. return ret;
  175. return vsc82x4_config_autocross_enable(phydev);
  176. }
  177. return genphy_config_aneg(phydev);
  178. }
  179. /* Vitesse 82xx */
  180. static struct phy_driver vsc82xx_driver[] = {
  181. {
  182. .phy_id = PHY_ID_VSC8234,
  183. .name = "Vitesse VSC8234",
  184. .phy_id_mask = 0x000ffff0,
  185. .features = PHY_GBIT_FEATURES,
  186. .flags = PHY_HAS_INTERRUPT,
  187. .config_init = &vsc824x_config_init,
  188. .config_aneg = &vsc82x4_config_aneg,
  189. .read_status = &genphy_read_status,
  190. .ack_interrupt = &vsc824x_ack_interrupt,
  191. .config_intr = &vsc82xx_config_intr,
  192. .driver = { .owner = THIS_MODULE,},
  193. }, {
  194. .phy_id = PHY_ID_VSC8244,
  195. .name = "Vitesse VSC8244",
  196. .phy_id_mask = 0x000fffc0,
  197. .features = PHY_GBIT_FEATURES,
  198. .flags = PHY_HAS_INTERRUPT,
  199. .config_init = &vsc824x_config_init,
  200. .config_aneg = &vsc82x4_config_aneg,
  201. .read_status = &genphy_read_status,
  202. .ack_interrupt = &vsc824x_ack_interrupt,
  203. .config_intr = &vsc82xx_config_intr,
  204. .driver = { .owner = THIS_MODULE,},
  205. }, {
  206. .phy_id = PHY_ID_VSC8574,
  207. .name = "Vitesse VSC8574",
  208. .phy_id_mask = 0x000ffff0,
  209. .features = PHY_GBIT_FEATURES,
  210. .flags = PHY_HAS_INTERRUPT,
  211. .config_init = &vsc824x_config_init,
  212. .config_aneg = &vsc82x4_config_aneg,
  213. .read_status = &genphy_read_status,
  214. .ack_interrupt = &vsc824x_ack_interrupt,
  215. .config_intr = &vsc82xx_config_intr,
  216. .driver = { .owner = THIS_MODULE,},
  217. }, {
  218. .phy_id = PHY_ID_VSC8662,
  219. .name = "Vitesse VSC8662",
  220. .phy_id_mask = 0x000ffff0,
  221. .features = PHY_GBIT_FEATURES,
  222. .flags = PHY_HAS_INTERRUPT,
  223. .config_init = &vsc824x_config_init,
  224. .config_aneg = &vsc82x4_config_aneg,
  225. .read_status = &genphy_read_status,
  226. .ack_interrupt = &vsc824x_ack_interrupt,
  227. .config_intr = &vsc82xx_config_intr,
  228. .driver = { .owner = THIS_MODULE,},
  229. }, {
  230. /* Vitesse 8221 */
  231. .phy_id = PHY_ID_VSC8221,
  232. .phy_id_mask = 0x000ffff0,
  233. .name = "Vitesse VSC8221",
  234. .features = PHY_GBIT_FEATURES,
  235. .flags = PHY_HAS_INTERRUPT,
  236. .config_init = &vsc8221_config_init,
  237. .config_aneg = &genphy_config_aneg,
  238. .read_status = &genphy_read_status,
  239. .ack_interrupt = &vsc824x_ack_interrupt,
  240. .config_intr = &vsc82xx_config_intr,
  241. .driver = { .owner = THIS_MODULE,},
  242. }, {
  243. /* Vitesse 8211 */
  244. .phy_id = PHY_ID_VSC8211,
  245. .phy_id_mask = 0x000ffff0,
  246. .name = "Vitesse VSC8211",
  247. .features = PHY_GBIT_FEATURES,
  248. .flags = PHY_HAS_INTERRUPT,
  249. .config_init = &vsc8221_config_init,
  250. .config_aneg = &genphy_config_aneg,
  251. .read_status = &genphy_read_status,
  252. .ack_interrupt = &vsc824x_ack_interrupt,
  253. .config_intr = &vsc82xx_config_intr,
  254. .driver = { .owner = THIS_MODULE,},
  255. } };
  256. static int __init vsc82xx_init(void)
  257. {
  258. return phy_drivers_register(vsc82xx_driver,
  259. ARRAY_SIZE(vsc82xx_driver));
  260. }
  261. static void __exit vsc82xx_exit(void)
  262. {
  263. return phy_drivers_unregister(vsc82xx_driver,
  264. ARRAY_SIZE(vsc82xx_driver));
  265. }
  266. module_init(vsc82xx_init);
  267. module_exit(vsc82xx_exit);
  268. static struct mdio_device_id __maybe_unused vitesse_tbl[] = {
  269. { PHY_ID_VSC8234, 0x000ffff0 },
  270. { PHY_ID_VSC8244, 0x000fffc0 },
  271. { PHY_ID_VSC8574, 0x000ffff0 },
  272. { PHY_ID_VSC8662, 0x000ffff0 },
  273. { PHY_ID_VSC8221, 0x000ffff0 },
  274. { PHY_ID_VSC8211, 0x000ffff0 },
  275. { }
  276. };
  277. MODULE_DEVICE_TABLE(mdio, vitesse_tbl);