meson-gxl.c 6.8 KB

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