meson-gxl.c 6.7 KB

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