vitesse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. /* Vitesse VSC8601 Extended PHY Control Register 1 */
  56. #define MII_VSC8601_EPHY_CTL 0x17
  57. #define MII_VSC8601_EPHY_CTL_RGMII_SKEW (1 << 8)
  58. #define PHY_ID_VSC8234 0x000fc620
  59. #define PHY_ID_VSC8244 0x000fc6c0
  60. #define PHY_ID_VSC8514 0x00070670
  61. #define PHY_ID_VSC8574 0x000704a0
  62. #define PHY_ID_VSC8601 0x00070420
  63. #define PHY_ID_VSC8662 0x00070660
  64. #define PHY_ID_VSC8221 0x000fc550
  65. #define PHY_ID_VSC8211 0x000fc4b0
  66. MODULE_DESCRIPTION("Vitesse PHY driver");
  67. MODULE_AUTHOR("Kriston Carson");
  68. MODULE_LICENSE("GPL");
  69. static int vsc824x_add_skew(struct phy_device *phydev)
  70. {
  71. int err;
  72. int extcon;
  73. extcon = phy_read(phydev, MII_VSC8244_EXT_CON1);
  74. if (extcon < 0)
  75. return extcon;
  76. extcon &= ~(MII_VSC8244_EXTCON1_TX_SKEW_MASK |
  77. MII_VSC8244_EXTCON1_RX_SKEW_MASK);
  78. extcon |= (MII_VSC8244_EXTCON1_TX_SKEW |
  79. MII_VSC8244_EXTCON1_RX_SKEW);
  80. err = phy_write(phydev, MII_VSC8244_EXT_CON1, extcon);
  81. return err;
  82. }
  83. static int vsc824x_config_init(struct phy_device *phydev)
  84. {
  85. int err;
  86. err = phy_write(phydev, MII_VSC8244_AUX_CONSTAT,
  87. MII_VSC8244_AUXCONSTAT_INIT);
  88. if (err < 0)
  89. return err;
  90. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  91. err = vsc824x_add_skew(phydev);
  92. return err;
  93. }
  94. /* This adds a skew for both TX and RX clocks, so the skew should only be
  95. * applied to "rgmii-id" interfaces. It may not work as expected
  96. * on "rgmii-txid", "rgmii-rxid" or "rgmii" interfaces. */
  97. static int vsc8601_add_skew(struct phy_device *phydev)
  98. {
  99. int ret;
  100. ret = phy_read(phydev, MII_VSC8601_EPHY_CTL);
  101. if (ret < 0)
  102. return ret;
  103. ret |= MII_VSC8601_EPHY_CTL_RGMII_SKEW;
  104. return phy_write(phydev, MII_VSC8601_EPHY_CTL, ret);
  105. }
  106. static int vsc8601_config_init(struct phy_device *phydev)
  107. {
  108. int ret = 0;
  109. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  110. ret = vsc8601_add_skew(phydev);
  111. if (ret < 0)
  112. return ret;
  113. return genphy_config_init(phydev);
  114. }
  115. static int vsc824x_ack_interrupt(struct phy_device *phydev)
  116. {
  117. int err = 0;
  118. /* Don't bother to ACK the interrupts if interrupts
  119. * are disabled. The 824x cannot clear the interrupts
  120. * if they are disabled.
  121. */
  122. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  123. err = phy_read(phydev, MII_VSC8244_ISTAT);
  124. return (err < 0) ? err : 0;
  125. }
  126. static int vsc82xx_config_intr(struct phy_device *phydev)
  127. {
  128. int err;
  129. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  130. err = phy_write(phydev, MII_VSC8244_IMASK,
  131. (phydev->drv->phy_id == PHY_ID_VSC8234 ||
  132. phydev->drv->phy_id == PHY_ID_VSC8244 ||
  133. phydev->drv->phy_id == PHY_ID_VSC8514 ||
  134. phydev->drv->phy_id == PHY_ID_VSC8574 ||
  135. phydev->drv->phy_id == PHY_ID_VSC8601) ?
  136. MII_VSC8244_IMASK_MASK :
  137. MII_VSC8221_IMASK_MASK);
  138. else {
  139. /* The Vitesse PHY cannot clear the interrupt
  140. * once it has disabled them, so we clear them first
  141. */
  142. err = phy_read(phydev, MII_VSC8244_ISTAT);
  143. if (err < 0)
  144. return err;
  145. err = phy_write(phydev, MII_VSC8244_IMASK, 0);
  146. }
  147. return err;
  148. }
  149. static int vsc8221_config_init(struct phy_device *phydev)
  150. {
  151. int err;
  152. err = phy_write(phydev, MII_VSC8244_AUX_CONSTAT,
  153. MII_VSC8221_AUXCONSTAT_INIT);
  154. return err;
  155. /* Perhaps we should set EXT_CON1 based on the interface?
  156. * Options are 802.3Z SerDes or SGMII
  157. */
  158. }
  159. /* vsc82x4_config_autocross_enable - Enable auto MDI/MDI-X for forced links
  160. * @phydev: target phy_device struct
  161. *
  162. * Enable auto MDI/MDI-X when in 10/100 forced link speeds by writing
  163. * special values in the VSC8234/VSC8244 extended reserved registers
  164. */
  165. static int vsc82x4_config_autocross_enable(struct phy_device *phydev)
  166. {
  167. int ret;
  168. if (phydev->autoneg == AUTONEG_ENABLE || phydev->speed > SPEED_100)
  169. return 0;
  170. /* map extended registers set 0x10 - 0x1e */
  171. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x52b5);
  172. if (ret >= 0)
  173. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_18E, 0x0012);
  174. if (ret >= 0)
  175. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_17E, 0x2803);
  176. if (ret >= 0)
  177. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_16E, 0x87fa);
  178. /* map standard registers set 0x10 - 0x1e */
  179. if (ret >= 0)
  180. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x0000);
  181. else
  182. phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x0000);
  183. return ret;
  184. }
  185. /* vsc82x4_config_aneg - restart auto-negotiation or write BMCR
  186. * @phydev: target phy_device struct
  187. *
  188. * Description: If auto-negotiation is enabled, we configure the
  189. * advertising, and then restart auto-negotiation. If it is not
  190. * enabled, then we write the BMCR and also start the auto
  191. * MDI/MDI-X feature
  192. */
  193. static int vsc82x4_config_aneg(struct phy_device *phydev)
  194. {
  195. int ret;
  196. /* Enable auto MDI/MDI-X when in 10/100 forced link speeds by
  197. * writing special values in the VSC8234 extended reserved registers
  198. */
  199. if (phydev->autoneg != AUTONEG_ENABLE && phydev->speed <= SPEED_100) {
  200. ret = genphy_setup_forced(phydev);
  201. if (ret < 0) /* error */
  202. return ret;
  203. return vsc82x4_config_autocross_enable(phydev);
  204. }
  205. return genphy_config_aneg(phydev);
  206. }
  207. /* Vitesse 82xx */
  208. static struct phy_driver vsc82xx_driver[] = {
  209. {
  210. .phy_id = PHY_ID_VSC8234,
  211. .name = "Vitesse VSC8234",
  212. .phy_id_mask = 0x000ffff0,
  213. .features = PHY_GBIT_FEATURES,
  214. .flags = PHY_HAS_INTERRUPT,
  215. .config_init = &vsc824x_config_init,
  216. .config_aneg = &vsc82x4_config_aneg,
  217. .read_status = &genphy_read_status,
  218. .ack_interrupt = &vsc824x_ack_interrupt,
  219. .config_intr = &vsc82xx_config_intr,
  220. }, {
  221. .phy_id = PHY_ID_VSC8244,
  222. .name = "Vitesse VSC8244",
  223. .phy_id_mask = 0x000fffc0,
  224. .features = PHY_GBIT_FEATURES,
  225. .flags = PHY_HAS_INTERRUPT,
  226. .config_init = &vsc824x_config_init,
  227. .config_aneg = &vsc82x4_config_aneg,
  228. .read_status = &genphy_read_status,
  229. .ack_interrupt = &vsc824x_ack_interrupt,
  230. .config_intr = &vsc82xx_config_intr,
  231. }, {
  232. .phy_id = PHY_ID_VSC8514,
  233. .name = "Vitesse VSC8514",
  234. .phy_id_mask = 0x000ffff0,
  235. .features = PHY_GBIT_FEATURES,
  236. .flags = PHY_HAS_INTERRUPT,
  237. .config_init = &vsc824x_config_init,
  238. .config_aneg = &vsc82x4_config_aneg,
  239. .read_status = &genphy_read_status,
  240. .ack_interrupt = &vsc824x_ack_interrupt,
  241. .config_intr = &vsc82xx_config_intr,
  242. }, {
  243. .phy_id = PHY_ID_VSC8574,
  244. .name = "Vitesse VSC8574",
  245. .phy_id_mask = 0x000ffff0,
  246. .features = PHY_GBIT_FEATURES,
  247. .flags = PHY_HAS_INTERRUPT,
  248. .config_init = &vsc824x_config_init,
  249. .config_aneg = &vsc82x4_config_aneg,
  250. .read_status = &genphy_read_status,
  251. .ack_interrupt = &vsc824x_ack_interrupt,
  252. .config_intr = &vsc82xx_config_intr,
  253. }, {
  254. .phy_id = PHY_ID_VSC8601,
  255. .name = "Vitesse VSC8601",
  256. .phy_id_mask = 0x000ffff0,
  257. .features = PHY_GBIT_FEATURES,
  258. .flags = PHY_HAS_INTERRUPT,
  259. .config_init = &vsc8601_config_init,
  260. .config_aneg = &genphy_config_aneg,
  261. .read_status = &genphy_read_status,
  262. .ack_interrupt = &vsc824x_ack_interrupt,
  263. .config_intr = &vsc82xx_config_intr,
  264. }, {
  265. .phy_id = PHY_ID_VSC8662,
  266. .name = "Vitesse VSC8662",
  267. .phy_id_mask = 0x000ffff0,
  268. .features = PHY_GBIT_FEATURES,
  269. .flags = PHY_HAS_INTERRUPT,
  270. .config_init = &vsc824x_config_init,
  271. .config_aneg = &vsc82x4_config_aneg,
  272. .read_status = &genphy_read_status,
  273. .ack_interrupt = &vsc824x_ack_interrupt,
  274. .config_intr = &vsc82xx_config_intr,
  275. }, {
  276. /* Vitesse 8221 */
  277. .phy_id = PHY_ID_VSC8221,
  278. .phy_id_mask = 0x000ffff0,
  279. .name = "Vitesse VSC8221",
  280. .features = PHY_GBIT_FEATURES,
  281. .flags = PHY_HAS_INTERRUPT,
  282. .config_init = &vsc8221_config_init,
  283. .config_aneg = &genphy_config_aneg,
  284. .read_status = &genphy_read_status,
  285. .ack_interrupt = &vsc824x_ack_interrupt,
  286. .config_intr = &vsc82xx_config_intr,
  287. }, {
  288. /* Vitesse 8211 */
  289. .phy_id = PHY_ID_VSC8211,
  290. .phy_id_mask = 0x000ffff0,
  291. .name = "Vitesse VSC8211",
  292. .features = PHY_GBIT_FEATURES,
  293. .flags = PHY_HAS_INTERRUPT,
  294. .config_init = &vsc8221_config_init,
  295. .config_aneg = &genphy_config_aneg,
  296. .read_status = &genphy_read_status,
  297. .ack_interrupt = &vsc824x_ack_interrupt,
  298. .config_intr = &vsc82xx_config_intr,
  299. } };
  300. module_phy_driver(vsc82xx_driver);
  301. static struct mdio_device_id __maybe_unused vitesse_tbl[] = {
  302. { PHY_ID_VSC8234, 0x000ffff0 },
  303. { PHY_ID_VSC8244, 0x000fffc0 },
  304. { PHY_ID_VSC8514, 0x000ffff0 },
  305. { PHY_ID_VSC8574, 0x000ffff0 },
  306. { PHY_ID_VSC8662, 0x000ffff0 },
  307. { PHY_ID_VSC8221, 0x000ffff0 },
  308. { PHY_ID_VSC8211, 0x000ffff0 },
  309. { }
  310. };
  311. MODULE_DEVICE_TABLE(mdio, vitesse_tbl);