meson-gxl.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Amlogic Meson GXL Internal PHY Driver
  4. *
  5. * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
  6. * Copyright (C) 2016 BayLibre, SAS. All rights reserved.
  7. * Author: Neil Armstrong <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/mii.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/phy.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/bitfield.h>
  16. #define TSTCNTL 20
  17. #define TSTCNTL_READ BIT(15)
  18. #define TSTCNTL_WRITE BIT(14)
  19. #define TSTCNTL_REG_BANK_SEL GENMASK(12, 11)
  20. #define TSTCNTL_TEST_MODE BIT(10)
  21. #define TSTCNTL_READ_ADDRESS GENMASK(9, 5)
  22. #define TSTCNTL_WRITE_ADDRESS GENMASK(4, 0)
  23. #define TSTREAD1 21
  24. #define TSTWRITE 23
  25. #define INTSRC_FLAG 29
  26. #define INTSRC_ANEG_PR BIT(1)
  27. #define INTSRC_PARALLEL_FAULT BIT(2)
  28. #define INTSRC_ANEG_LP_ACK BIT(3)
  29. #define INTSRC_LINK_DOWN BIT(4)
  30. #define INTSRC_REMOTE_FAULT BIT(5)
  31. #define INTSRC_ANEG_COMPLETE BIT(6)
  32. #define INTSRC_ENERGY_DETECT BIT(7)
  33. #define INTSRC_MASK 30
  34. #define INT_SOURCES (INTSRC_LINK_DOWN | INTSRC_ANEG_COMPLETE | \
  35. INTSRC_ENERGY_DETECT)
  36. #define BANK_ANALOG_DSP 0
  37. #define BANK_WOL 1
  38. #define BANK_BIST 3
  39. /* WOL Registers */
  40. #define LPI_STATUS 0xc
  41. #define LPI_STATUS_RSV12 BIT(12)
  42. /* BIST Registers */
  43. #define FR_PLL_CONTROL 0x1b
  44. #define FR_PLL_DIV0 0x1c
  45. #define FR_PLL_DIV1 0x1d
  46. static int meson_gxl_open_banks(struct phy_device *phydev)
  47. {
  48. int ret;
  49. /* Enable Analog and DSP register Bank access by
  50. * toggling TSTCNTL_TEST_MODE bit in the TSTCNTL register
  51. */
  52. ret = phy_write(phydev, TSTCNTL, 0);
  53. if (ret)
  54. return ret;
  55. ret = phy_write(phydev, TSTCNTL, TSTCNTL_TEST_MODE);
  56. if (ret)
  57. return ret;
  58. ret = phy_write(phydev, TSTCNTL, 0);
  59. if (ret)
  60. return ret;
  61. return phy_write(phydev, TSTCNTL, TSTCNTL_TEST_MODE);
  62. }
  63. static void meson_gxl_close_banks(struct phy_device *phydev)
  64. {
  65. phy_write(phydev, TSTCNTL, 0);
  66. }
  67. static int meson_gxl_read_reg(struct phy_device *phydev,
  68. unsigned int bank, unsigned int reg)
  69. {
  70. int ret;
  71. ret = meson_gxl_open_banks(phydev);
  72. if (ret)
  73. goto out;
  74. ret = phy_write(phydev, TSTCNTL, TSTCNTL_READ |
  75. FIELD_PREP(TSTCNTL_REG_BANK_SEL, bank) |
  76. TSTCNTL_TEST_MODE |
  77. FIELD_PREP(TSTCNTL_READ_ADDRESS, reg));
  78. if (ret)
  79. goto out;
  80. ret = phy_read(phydev, TSTREAD1);
  81. out:
  82. /* Close the bank access on our way out */
  83. meson_gxl_close_banks(phydev);
  84. return ret;
  85. }
  86. static int meson_gxl_write_reg(struct phy_device *phydev,
  87. unsigned int bank, unsigned int reg,
  88. uint16_t value)
  89. {
  90. int ret;
  91. ret = meson_gxl_open_banks(phydev);
  92. if (ret)
  93. goto out;
  94. ret = phy_write(phydev, TSTWRITE, value);
  95. if (ret)
  96. goto out;
  97. ret = phy_write(phydev, TSTCNTL, TSTCNTL_WRITE |
  98. FIELD_PREP(TSTCNTL_REG_BANK_SEL, bank) |
  99. TSTCNTL_TEST_MODE |
  100. FIELD_PREP(TSTCNTL_WRITE_ADDRESS, reg));
  101. out:
  102. /* Close the bank access on our way out */
  103. meson_gxl_close_banks(phydev);
  104. return ret;
  105. }
  106. static int meson_gxl_config_init(struct phy_device *phydev)
  107. {
  108. int ret;
  109. /* Enable fractional PLL */
  110. ret = meson_gxl_write_reg(phydev, BANK_BIST, FR_PLL_CONTROL, 0x5);
  111. if (ret)
  112. return ret;
  113. /* Program fraction FR_PLL_DIV1 */
  114. ret = meson_gxl_write_reg(phydev, BANK_BIST, FR_PLL_DIV1, 0x029a);
  115. if (ret)
  116. return ret;
  117. /* Program fraction FR_PLL_DIV1 */
  118. ret = meson_gxl_write_reg(phydev, BANK_BIST, FR_PLL_DIV0, 0xaaaa);
  119. if (ret)
  120. return ret;
  121. return 0;
  122. }
  123. /* This function is provided to cope with the possible failures of this phy
  124. * during aneg process. When aneg fails, the PHY reports that aneg is done
  125. * but the value found in MII_LPA is wrong:
  126. * - Early failures: MII_LPA is just 0x0001. if MII_EXPANSION reports that
  127. * the link partner (LP) supports aneg but the LP never acked our base
  128. * code word, it is likely that we never sent it to begin with.
  129. * - Late failures: MII_LPA is filled with a value which seems to make sense
  130. * but it actually is not what the LP is advertising. It seems that we
  131. * can detect this using a magic bit in the WOL bank (reg 12 - bit 12).
  132. * If this particular bit is not set when aneg is reported being done,
  133. * it means MII_LPA is likely to be wrong.
  134. *
  135. * In both case, forcing a restart of the aneg process solve the problem.
  136. * When this failure happens, the first retry is usually successful but,
  137. * in some cases, it may take up to 6 retries to get a decent result
  138. */
  139. static int meson_gxl_read_status(struct phy_device *phydev)
  140. {
  141. int ret, wol, lpa, exp;
  142. if (phydev->autoneg == AUTONEG_ENABLE) {
  143. ret = genphy_aneg_done(phydev);
  144. if (ret < 0)
  145. return ret;
  146. else if (!ret)
  147. goto read_status_continue;
  148. /* Aneg is done, let's check everything is fine */
  149. wol = meson_gxl_read_reg(phydev, BANK_WOL, LPI_STATUS);
  150. if (wol < 0)
  151. return wol;
  152. lpa = phy_read(phydev, MII_LPA);
  153. if (lpa < 0)
  154. return lpa;
  155. exp = phy_read(phydev, MII_EXPANSION);
  156. if (exp < 0)
  157. return exp;
  158. if (!(wol & LPI_STATUS_RSV12) ||
  159. ((exp & EXPANSION_NWAY) && !(lpa & LPA_LPACK))) {
  160. /* Looks like aneg failed after all */
  161. phydev_dbg(phydev, "LPA corruption - aneg restart\n");
  162. return genphy_restart_aneg(phydev);
  163. }
  164. }
  165. read_status_continue:
  166. return genphy_read_status(phydev);
  167. }
  168. static int meson_gxl_ack_interrupt(struct phy_device *phydev)
  169. {
  170. int ret = phy_read(phydev, INTSRC_FLAG);
  171. return ret < 0 ? ret : 0;
  172. }
  173. static int meson_gxl_config_intr(struct phy_device *phydev)
  174. {
  175. int ret;
  176. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  177. /* Ack any pending IRQ */
  178. ret = meson_gxl_ack_interrupt(phydev);
  179. if (ret)
  180. return ret;
  181. ret = phy_write(phydev, INTSRC_MASK, INT_SOURCES);
  182. } else {
  183. ret = phy_write(phydev, INTSRC_MASK, 0);
  184. /* Ack any pending IRQ */
  185. ret = meson_gxl_ack_interrupt(phydev);
  186. }
  187. return ret;
  188. }
  189. static irqreturn_t meson_gxl_handle_interrupt(struct phy_device *phydev)
  190. {
  191. int irq_status;
  192. irq_status = phy_read(phydev, INTSRC_FLAG);
  193. if (irq_status < 0) {
  194. phy_error(phydev);
  195. return IRQ_NONE;
  196. }
  197. irq_status &= INT_SOURCES;
  198. if (irq_status == 0)
  199. return IRQ_NONE;
  200. /* Aneg-complete interrupt is used for link-up detection */
  201. if (phydev->autoneg == AUTONEG_ENABLE &&
  202. irq_status == INTSRC_ENERGY_DETECT)
  203. return IRQ_HANDLED;
  204. phy_trigger_machine(phydev);
  205. return IRQ_HANDLED;
  206. }
  207. static struct phy_driver meson_gxl_phy[] = {
  208. {
  209. PHY_ID_MATCH_EXACT(0x01814400),
  210. .name = "Meson GXL Internal PHY",
  211. /* PHY_BASIC_FEATURES */
  212. .flags = PHY_IS_INTERNAL,
  213. .soft_reset = genphy_soft_reset,
  214. .config_init = meson_gxl_config_init,
  215. .read_status = meson_gxl_read_status,
  216. .config_intr = meson_gxl_config_intr,
  217. .handle_interrupt = meson_gxl_handle_interrupt,
  218. .suspend = genphy_suspend,
  219. .resume = genphy_resume,
  220. .read_mmd = genphy_read_mmd_unsupported,
  221. .write_mmd = genphy_write_mmd_unsupported,
  222. }, {
  223. PHY_ID_MATCH_EXACT(0x01803301),
  224. .name = "Meson G12A Internal PHY",
  225. /* PHY_BASIC_FEATURES */
  226. .flags = PHY_IS_INTERNAL,
  227. .soft_reset = genphy_soft_reset,
  228. .config_intr = meson_gxl_config_intr,
  229. .handle_interrupt = meson_gxl_handle_interrupt,
  230. .suspend = genphy_suspend,
  231. .resume = genphy_resume,
  232. .read_mmd = genphy_read_mmd_unsupported,
  233. .write_mmd = genphy_write_mmd_unsupported,
  234. },
  235. };
  236. static struct mdio_device_id __maybe_unused meson_gxl_tbl[] = {
  237. { PHY_ID_MATCH_VENDOR(0x01814400) },
  238. { PHY_ID_MATCH_VENDOR(0x01803301) },
  239. { }
  240. };
  241. module_phy_driver(meson_gxl_phy);
  242. MODULE_DEVICE_TABLE(mdio, meson_gxl_tbl);
  243. MODULE_DESCRIPTION("Amlogic Meson GXL Internal PHY driver");
  244. MODULE_AUTHOR("Baoqi wang");
  245. MODULE_AUTHOR("Neil Armstrong <[email protected]>");
  246. MODULE_AUTHOR("Jerome Brunet <[email protected]>");
  247. MODULE_LICENSE("GPL");