vitesse.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Vitesse PHYs
  4. *
  5. * Author: Kriston Carson
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/mii.h>
  10. #include <linux/ethtool.h>
  11. #include <linux/phy.h>
  12. /* Vitesse Extended Page Magic Register(s) */
  13. #define MII_VSC82X4_EXT_PAGE_16E 0x10
  14. #define MII_VSC82X4_EXT_PAGE_17E 0x11
  15. #define MII_VSC82X4_EXT_PAGE_18E 0x12
  16. /* Vitesse Extended Control Register 1 */
  17. #define MII_VSC8244_EXT_CON1 0x17
  18. #define MII_VSC8244_EXTCON1_INIT 0x0000
  19. #define MII_VSC8244_EXTCON1_TX_SKEW_MASK 0x0c00
  20. #define MII_VSC8244_EXTCON1_RX_SKEW_MASK 0x0300
  21. #define MII_VSC8244_EXTCON1_TX_SKEW 0x0800
  22. #define MII_VSC8244_EXTCON1_RX_SKEW 0x0200
  23. /* Vitesse Interrupt Mask Register */
  24. #define MII_VSC8244_IMASK 0x19
  25. #define MII_VSC8244_IMASK_IEN 0x8000
  26. #define MII_VSC8244_IMASK_SPEED 0x4000
  27. #define MII_VSC8244_IMASK_LINK 0x2000
  28. #define MII_VSC8244_IMASK_DUPLEX 0x1000
  29. #define MII_VSC8244_IMASK_MASK 0xf000
  30. #define MII_VSC8221_IMASK_MASK 0xa000
  31. /* Vitesse Interrupt Status Register */
  32. #define MII_VSC8244_ISTAT 0x1a
  33. #define MII_VSC8244_ISTAT_STATUS 0x8000
  34. #define MII_VSC8244_ISTAT_SPEED 0x4000
  35. #define MII_VSC8244_ISTAT_LINK 0x2000
  36. #define MII_VSC8244_ISTAT_DUPLEX 0x1000
  37. /* Vitesse Auxiliary Control/Status Register */
  38. #define MII_VSC8244_AUX_CONSTAT 0x1c
  39. #define MII_VSC8244_AUXCONSTAT_INIT 0x0000
  40. #define MII_VSC8244_AUXCONSTAT_DUPLEX 0x0020
  41. #define MII_VSC8244_AUXCONSTAT_SPEED 0x0018
  42. #define MII_VSC8244_AUXCONSTAT_GBIT 0x0010
  43. #define MII_VSC8244_AUXCONSTAT_100 0x0008
  44. #define MII_VSC8221_AUXCONSTAT_INIT 0x0004 /* need to set this bit? */
  45. #define MII_VSC8221_AUXCONSTAT_RESERVED 0x0004
  46. /* Vitesse Extended Page Access Register */
  47. #define MII_VSC82X4_EXT_PAGE_ACCESS 0x1f
  48. /* Vitesse VSC8601 Extended PHY Control Register 1 */
  49. #define MII_VSC8601_EPHY_CTL 0x17
  50. #define MII_VSC8601_EPHY_CTL_RGMII_SKEW (1 << 8)
  51. #define PHY_ID_VSC8234 0x000fc620
  52. #define PHY_ID_VSC8244 0x000fc6c0
  53. #define PHY_ID_VSC8572 0x000704d0
  54. #define PHY_ID_VSC8601 0x00070420
  55. #define PHY_ID_VSC7385 0x00070450
  56. #define PHY_ID_VSC7388 0x00070480
  57. #define PHY_ID_VSC7395 0x00070550
  58. #define PHY_ID_VSC7398 0x00070580
  59. #define PHY_ID_VSC8662 0x00070660
  60. #define PHY_ID_VSC8221 0x000fc550
  61. #define PHY_ID_VSC8211 0x000fc4b0
  62. MODULE_DESCRIPTION("Vitesse PHY driver");
  63. MODULE_AUTHOR("Kriston Carson");
  64. MODULE_LICENSE("GPL");
  65. static int vsc824x_add_skew(struct phy_device *phydev)
  66. {
  67. int err;
  68. int extcon;
  69. extcon = phy_read(phydev, MII_VSC8244_EXT_CON1);
  70. if (extcon < 0)
  71. return extcon;
  72. extcon &= ~(MII_VSC8244_EXTCON1_TX_SKEW_MASK |
  73. MII_VSC8244_EXTCON1_RX_SKEW_MASK);
  74. extcon |= (MII_VSC8244_EXTCON1_TX_SKEW |
  75. MII_VSC8244_EXTCON1_RX_SKEW);
  76. err = phy_write(phydev, MII_VSC8244_EXT_CON1, extcon);
  77. return err;
  78. }
  79. static int vsc824x_config_init(struct phy_device *phydev)
  80. {
  81. int err;
  82. err = phy_write(phydev, MII_VSC8244_AUX_CONSTAT,
  83. MII_VSC8244_AUXCONSTAT_INIT);
  84. if (err < 0)
  85. return err;
  86. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  87. err = vsc824x_add_skew(phydev);
  88. return err;
  89. }
  90. #define VSC73XX_EXT_PAGE_ACCESS 0x1f
  91. static int vsc73xx_read_page(struct phy_device *phydev)
  92. {
  93. return __phy_read(phydev, VSC73XX_EXT_PAGE_ACCESS);
  94. }
  95. static int vsc73xx_write_page(struct phy_device *phydev, int page)
  96. {
  97. return __phy_write(phydev, VSC73XX_EXT_PAGE_ACCESS, page);
  98. }
  99. static void vsc73xx_config_init(struct phy_device *phydev)
  100. {
  101. /* Receiver init */
  102. phy_write(phydev, 0x1f, 0x2a30);
  103. phy_modify(phydev, 0x0c, 0x0300, 0x0200);
  104. phy_write(phydev, 0x1f, 0x0000);
  105. /* Config LEDs 0x61 */
  106. phy_modify(phydev, MII_TPISTATUS, 0xff00, 0x0061);
  107. }
  108. static int vsc738x_config_init(struct phy_device *phydev)
  109. {
  110. u16 rev;
  111. /* This magic sequence appear in the application note
  112. * "VSC7385/7388 PHY Configuration".
  113. *
  114. * Maybe one day we will get to know what it all means.
  115. */
  116. phy_write(phydev, 0x1f, 0x2a30);
  117. phy_modify(phydev, 0x08, 0x0200, 0x0200);
  118. phy_write(phydev, 0x1f, 0x52b5);
  119. phy_write(phydev, 0x10, 0xb68a);
  120. phy_modify(phydev, 0x12, 0xff07, 0x0003);
  121. phy_modify(phydev, 0x11, 0x00ff, 0x00a2);
  122. phy_write(phydev, 0x10, 0x968a);
  123. phy_write(phydev, 0x1f, 0x2a30);
  124. phy_modify(phydev, 0x08, 0x0200, 0x0000);
  125. phy_write(phydev, 0x1f, 0x0000);
  126. /* Read revision */
  127. rev = phy_read(phydev, MII_PHYSID2);
  128. rev &= 0x0f;
  129. /* Special quirk for revision 0 */
  130. if (rev == 0) {
  131. phy_write(phydev, 0x1f, 0x2a30);
  132. phy_modify(phydev, 0x08, 0x0200, 0x0200);
  133. phy_write(phydev, 0x1f, 0x52b5);
  134. phy_write(phydev, 0x12, 0x0000);
  135. phy_write(phydev, 0x11, 0x0689);
  136. phy_write(phydev, 0x10, 0x8f92);
  137. phy_write(phydev, 0x1f, 0x52b5);
  138. phy_write(phydev, 0x12, 0x0000);
  139. phy_write(phydev, 0x11, 0x0e35);
  140. phy_write(phydev, 0x10, 0x9786);
  141. phy_write(phydev, 0x1f, 0x2a30);
  142. phy_modify(phydev, 0x08, 0x0200, 0x0000);
  143. phy_write(phydev, 0x17, 0xff80);
  144. phy_write(phydev, 0x17, 0x0000);
  145. }
  146. phy_write(phydev, 0x1f, 0x0000);
  147. phy_write(phydev, 0x12, 0x0048);
  148. if (rev == 0) {
  149. phy_write(phydev, 0x1f, 0x2a30);
  150. phy_write(phydev, 0x14, 0x6600);
  151. phy_write(phydev, 0x1f, 0x0000);
  152. phy_write(phydev, 0x18, 0xa24e);
  153. } else {
  154. phy_write(phydev, 0x1f, 0x2a30);
  155. phy_modify(phydev, 0x16, 0x0fc0, 0x0240);
  156. phy_modify(phydev, 0x14, 0x6000, 0x4000);
  157. /* bits 14-15 in extended register 0x14 controls DACG amplitude
  158. * 6 = -8%, 2 is hardware default
  159. */
  160. phy_write(phydev, 0x1f, 0x0001);
  161. phy_modify(phydev, 0x14, 0xe000, 0x6000);
  162. phy_write(phydev, 0x1f, 0x0000);
  163. }
  164. vsc73xx_config_init(phydev);
  165. return 0;
  166. }
  167. static int vsc739x_config_init(struct phy_device *phydev)
  168. {
  169. /* This magic sequence appears in the VSC7395 SparX-G5e application
  170. * note "VSC7395/VSC7398 PHY Configuration"
  171. *
  172. * Maybe one day we will get to know what it all means.
  173. */
  174. phy_write(phydev, 0x1f, 0x2a30);
  175. phy_modify(phydev, 0x08, 0x0200, 0x0200);
  176. phy_write(phydev, 0x1f, 0x52b5);
  177. phy_write(phydev, 0x10, 0xb68a);
  178. phy_modify(phydev, 0x12, 0xff07, 0x0003);
  179. phy_modify(phydev, 0x11, 0x00ff, 0x00a2);
  180. phy_write(phydev, 0x10, 0x968a);
  181. phy_write(phydev, 0x1f, 0x2a30);
  182. phy_modify(phydev, 0x08, 0x0200, 0x0000);
  183. phy_write(phydev, 0x1f, 0x0000);
  184. phy_write(phydev, 0x1f, 0x0000);
  185. phy_write(phydev, 0x12, 0x0048);
  186. phy_write(phydev, 0x1f, 0x2a30);
  187. phy_modify(phydev, 0x16, 0x0fc0, 0x0240);
  188. phy_modify(phydev, 0x14, 0x6000, 0x4000);
  189. phy_write(phydev, 0x1f, 0x0001);
  190. phy_modify(phydev, 0x14, 0xe000, 0x6000);
  191. phy_write(phydev, 0x1f, 0x0000);
  192. vsc73xx_config_init(phydev);
  193. return 0;
  194. }
  195. static int vsc73xx_config_aneg(struct phy_device *phydev)
  196. {
  197. /* The VSC73xx switches does not like to be instructed to
  198. * do autonegotiation in any way, it prefers that you just go
  199. * with the power-on/reset defaults. Writing some registers will
  200. * just make autonegotiation permanently fail.
  201. */
  202. return 0;
  203. }
  204. /* This adds a skew for both TX and RX clocks, so the skew should only be
  205. * applied to "rgmii-id" interfaces. It may not work as expected
  206. * on "rgmii-txid", "rgmii-rxid" or "rgmii" interfaces. */
  207. static int vsc8601_add_skew(struct phy_device *phydev)
  208. {
  209. int ret;
  210. ret = phy_read(phydev, MII_VSC8601_EPHY_CTL);
  211. if (ret < 0)
  212. return ret;
  213. ret |= MII_VSC8601_EPHY_CTL_RGMII_SKEW;
  214. return phy_write(phydev, MII_VSC8601_EPHY_CTL, ret);
  215. }
  216. static int vsc8601_config_init(struct phy_device *phydev)
  217. {
  218. int ret = 0;
  219. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  220. ret = vsc8601_add_skew(phydev);
  221. if (ret < 0)
  222. return ret;
  223. return 0;
  224. }
  225. static int vsc824x_ack_interrupt(struct phy_device *phydev)
  226. {
  227. int err = 0;
  228. /* Don't bother to ACK the interrupts if interrupts
  229. * are disabled. The 824x cannot clear the interrupts
  230. * if they are disabled.
  231. */
  232. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  233. err = phy_read(phydev, MII_VSC8244_ISTAT);
  234. return (err < 0) ? err : 0;
  235. }
  236. static int vsc82xx_config_intr(struct phy_device *phydev)
  237. {
  238. int err;
  239. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  240. err = phy_write(phydev, MII_VSC8244_IMASK,
  241. (phydev->drv->phy_id == PHY_ID_VSC8234 ||
  242. phydev->drv->phy_id == PHY_ID_VSC8244 ||
  243. phydev->drv->phy_id == PHY_ID_VSC8572 ||
  244. phydev->drv->phy_id == PHY_ID_VSC8601) ?
  245. MII_VSC8244_IMASK_MASK :
  246. MII_VSC8221_IMASK_MASK);
  247. else {
  248. /* The Vitesse PHY cannot clear the interrupt
  249. * once it has disabled them, so we clear them first
  250. */
  251. err = phy_read(phydev, MII_VSC8244_ISTAT);
  252. if (err < 0)
  253. return err;
  254. err = phy_write(phydev, MII_VSC8244_IMASK, 0);
  255. }
  256. return err;
  257. }
  258. static int vsc8221_config_init(struct phy_device *phydev)
  259. {
  260. int err;
  261. err = phy_write(phydev, MII_VSC8244_AUX_CONSTAT,
  262. MII_VSC8221_AUXCONSTAT_INIT);
  263. return err;
  264. /* Perhaps we should set EXT_CON1 based on the interface?
  265. * Options are 802.3Z SerDes or SGMII
  266. */
  267. }
  268. /* vsc82x4_config_autocross_enable - Enable auto MDI/MDI-X for forced links
  269. * @phydev: target phy_device struct
  270. *
  271. * Enable auto MDI/MDI-X when in 10/100 forced link speeds by writing
  272. * special values in the VSC8234/VSC8244 extended reserved registers
  273. */
  274. static int vsc82x4_config_autocross_enable(struct phy_device *phydev)
  275. {
  276. int ret;
  277. if (phydev->autoneg == AUTONEG_ENABLE || phydev->speed > SPEED_100)
  278. return 0;
  279. /* map extended registers set 0x10 - 0x1e */
  280. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x52b5);
  281. if (ret >= 0)
  282. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_18E, 0x0012);
  283. if (ret >= 0)
  284. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_17E, 0x2803);
  285. if (ret >= 0)
  286. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_16E, 0x87fa);
  287. /* map standard registers set 0x10 - 0x1e */
  288. if (ret >= 0)
  289. ret = phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x0000);
  290. else
  291. phy_write(phydev, MII_VSC82X4_EXT_PAGE_ACCESS, 0x0000);
  292. return ret;
  293. }
  294. /* vsc82x4_config_aneg - restart auto-negotiation or write BMCR
  295. * @phydev: target phy_device struct
  296. *
  297. * Description: If auto-negotiation is enabled, we configure the
  298. * advertising, and then restart auto-negotiation. If it is not
  299. * enabled, then we write the BMCR and also start the auto
  300. * MDI/MDI-X feature
  301. */
  302. static int vsc82x4_config_aneg(struct phy_device *phydev)
  303. {
  304. int ret;
  305. /* Enable auto MDI/MDI-X when in 10/100 forced link speeds by
  306. * writing special values in the VSC8234 extended reserved registers
  307. */
  308. if (phydev->autoneg != AUTONEG_ENABLE && phydev->speed <= SPEED_100) {
  309. ret = genphy_setup_forced(phydev);
  310. if (ret < 0) /* error */
  311. return ret;
  312. return vsc82x4_config_autocross_enable(phydev);
  313. }
  314. return genphy_config_aneg(phydev);
  315. }
  316. /* Vitesse 82xx */
  317. static struct phy_driver vsc82xx_driver[] = {
  318. {
  319. .phy_id = PHY_ID_VSC8234,
  320. .name = "Vitesse VSC8234",
  321. .phy_id_mask = 0x000ffff0,
  322. /* PHY_GBIT_FEATURES */
  323. .config_init = &vsc824x_config_init,
  324. .config_aneg = &vsc82x4_config_aneg,
  325. .ack_interrupt = &vsc824x_ack_interrupt,
  326. .config_intr = &vsc82xx_config_intr,
  327. }, {
  328. .phy_id = PHY_ID_VSC8244,
  329. .name = "Vitesse VSC8244",
  330. .phy_id_mask = 0x000fffc0,
  331. /* PHY_GBIT_FEATURES */
  332. .config_init = &vsc824x_config_init,
  333. .config_aneg = &vsc82x4_config_aneg,
  334. .ack_interrupt = &vsc824x_ack_interrupt,
  335. .config_intr = &vsc82xx_config_intr,
  336. }, {
  337. .phy_id = PHY_ID_VSC8572,
  338. .name = "Vitesse VSC8572",
  339. .phy_id_mask = 0x000ffff0,
  340. /* PHY_GBIT_FEATURES */
  341. .config_init = &vsc824x_config_init,
  342. .config_aneg = &vsc82x4_config_aneg,
  343. .ack_interrupt = &vsc824x_ack_interrupt,
  344. .config_intr = &vsc82xx_config_intr,
  345. }, {
  346. .phy_id = PHY_ID_VSC8601,
  347. .name = "Vitesse VSC8601",
  348. .phy_id_mask = 0x000ffff0,
  349. /* PHY_GBIT_FEATURES */
  350. .config_init = &vsc8601_config_init,
  351. .ack_interrupt = &vsc824x_ack_interrupt,
  352. .config_intr = &vsc82xx_config_intr,
  353. }, {
  354. .phy_id = PHY_ID_VSC7385,
  355. .name = "Vitesse VSC7385",
  356. .phy_id_mask = 0x000ffff0,
  357. /* PHY_GBIT_FEATURES */
  358. .config_init = vsc738x_config_init,
  359. .config_aneg = vsc73xx_config_aneg,
  360. .read_page = vsc73xx_read_page,
  361. .write_page = vsc73xx_write_page,
  362. }, {
  363. .phy_id = PHY_ID_VSC7388,
  364. .name = "Vitesse VSC7388",
  365. .phy_id_mask = 0x000ffff0,
  366. /* PHY_GBIT_FEATURES */
  367. .config_init = vsc738x_config_init,
  368. .config_aneg = vsc73xx_config_aneg,
  369. .read_page = vsc73xx_read_page,
  370. .write_page = vsc73xx_write_page,
  371. }, {
  372. .phy_id = PHY_ID_VSC7395,
  373. .name = "Vitesse VSC7395",
  374. .phy_id_mask = 0x000ffff0,
  375. /* PHY_GBIT_FEATURES */
  376. .config_init = vsc739x_config_init,
  377. .config_aneg = vsc73xx_config_aneg,
  378. .read_page = vsc73xx_read_page,
  379. .write_page = vsc73xx_write_page,
  380. }, {
  381. .phy_id = PHY_ID_VSC7398,
  382. .name = "Vitesse VSC7398",
  383. .phy_id_mask = 0x000ffff0,
  384. /* PHY_GBIT_FEATURES */
  385. .config_init = vsc739x_config_init,
  386. .config_aneg = vsc73xx_config_aneg,
  387. .read_page = vsc73xx_read_page,
  388. .write_page = vsc73xx_write_page,
  389. }, {
  390. .phy_id = PHY_ID_VSC8662,
  391. .name = "Vitesse VSC8662",
  392. .phy_id_mask = 0x000ffff0,
  393. /* PHY_GBIT_FEATURES */
  394. .config_init = &vsc824x_config_init,
  395. .config_aneg = &vsc82x4_config_aneg,
  396. .ack_interrupt = &vsc824x_ack_interrupt,
  397. .config_intr = &vsc82xx_config_intr,
  398. }, {
  399. /* Vitesse 8221 */
  400. .phy_id = PHY_ID_VSC8221,
  401. .phy_id_mask = 0x000ffff0,
  402. .name = "Vitesse VSC8221",
  403. /* PHY_GBIT_FEATURES */
  404. .config_init = &vsc8221_config_init,
  405. .ack_interrupt = &vsc824x_ack_interrupt,
  406. .config_intr = &vsc82xx_config_intr,
  407. }, {
  408. /* Vitesse 8211 */
  409. .phy_id = PHY_ID_VSC8211,
  410. .phy_id_mask = 0x000ffff0,
  411. .name = "Vitesse VSC8211",
  412. /* PHY_GBIT_FEATURES */
  413. .config_init = &vsc8221_config_init,
  414. .ack_interrupt = &vsc824x_ack_interrupt,
  415. .config_intr = &vsc82xx_config_intr,
  416. } };
  417. module_phy_driver(vsc82xx_driver);
  418. static struct mdio_device_id __maybe_unused vitesse_tbl[] = {
  419. { PHY_ID_VSC8234, 0x000ffff0 },
  420. { PHY_ID_VSC8244, 0x000fffc0 },
  421. { PHY_ID_VSC8572, 0x000ffff0 },
  422. { PHY_ID_VSC7385, 0x000ffff0 },
  423. { PHY_ID_VSC7388, 0x000ffff0 },
  424. { PHY_ID_VSC7395, 0x000ffff0 },
  425. { PHY_ID_VSC7398, 0x000ffff0 },
  426. { PHY_ID_VSC8662, 0x000ffff0 },
  427. { PHY_ID_VSC8221, 0x000ffff0 },
  428. { PHY_ID_VSC8211, 0x000ffff0 },
  429. { }
  430. };
  431. MODULE_DEVICE_TABLE(mdio, vitesse_tbl);