meson-gxl.c 6.1 KB

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