meson-gxl-usb2.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Meson GXL and GXM USB2 PHY driver
  4. *
  5. * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  6. * Copyright (C) 2018 BayLibre, SAS
  7. * Author: Neil Armstrong <narmstron@baylibre.com>
  8. */
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <bitfield.h>
  12. #include <dm.h>
  13. #include <errno.h>
  14. #include <generic-phy.h>
  15. #include <regmap.h>
  16. #include <power/regulator.h>
  17. #include <clk.h>
  18. #include <linux/bitops.h>
  19. #include <linux/compat.h>
  20. /* bits [31:27] are read-only */
  21. #define U2P_R0 0x0
  22. #define U2P_R0_BYPASS_SEL BIT(0)
  23. #define U2P_R0_BYPASS_DM_EN BIT(1)
  24. #define U2P_R0_BYPASS_DP_EN BIT(2)
  25. #define U2P_R0_TXBITSTUFF_ENH BIT(3)
  26. #define U2P_R0_TXBITSTUFF_EN BIT(4)
  27. #define U2P_R0_DM_PULLDOWN BIT(5)
  28. #define U2P_R0_DP_PULLDOWN BIT(6)
  29. #define U2P_R0_DP_VBUS_VLD_EXT_SEL BIT(7)
  30. #define U2P_R0_DP_VBUS_VLD_EXT BIT(8)
  31. #define U2P_R0_ADP_PRB_EN BIT(9)
  32. #define U2P_R0_ADP_DISCHARGE BIT(10)
  33. #define U2P_R0_ADP_CHARGE BIT(11)
  34. #define U2P_R0_DRV_VBUS BIT(12)
  35. #define U2P_R0_ID_PULLUP BIT(13)
  36. #define U2P_R0_LOOPBACK_EN_B BIT(14)
  37. #define U2P_R0_OTG_DISABLE BIT(15)
  38. #define U2P_R0_COMMON_ONN BIT(16)
  39. #define U2P_R0_FSEL_MASK GENMASK(19, 17)
  40. #define U2P_R0_REF_CLK_SEL_MASK GENMASK(21, 20)
  41. #define U2P_R0_POWER_ON_RESET BIT(22)
  42. #define U2P_R0_V_ATE_TEST_EN_B_MASK GENMASK(24, 23)
  43. #define U2P_R0_ID_SET_ID_DQ BIT(25)
  44. #define U2P_R0_ATE_RESET BIT(26)
  45. #define U2P_R0_FSV_MINUS BIT(27)
  46. #define U2P_R0_FSV_PLUS BIT(28)
  47. #define U2P_R0_BYPASS_DM_DATA BIT(29)
  48. #define U2P_R0_BYPASS_DP_DATA BIT(30)
  49. #define U2P_R1 0x4
  50. #define U2P_R1_BURN_IN_TEST BIT(0)
  51. #define U2P_R1_ACA_ENABLE BIT(1)
  52. #define U2P_R1_DCD_ENABLE BIT(2)
  53. #define U2P_R1_VDAT_SRC_EN_B BIT(3)
  54. #define U2P_R1_VDAT_DET_EN_B BIT(4)
  55. #define U2P_R1_CHARGES_SEL BIT(5)
  56. #define U2P_R1_TX_PREEMP_PULSE_TUNE BIT(6)
  57. #define U2P_R1_TX_PREEMP_AMP_TUNE_MASK GENMASK(8, 7)
  58. #define U2P_R1_TX_RES_TUNE_MASK GENMASK(10, 9)
  59. #define U2P_R1_TX_RISE_TUNE_MASK GENMASK(12, 11)
  60. #define U2P_R1_TX_VREF_TUNE_MASK GENMASK(16, 13)
  61. #define U2P_R1_TX_FSLS_TUNE_MASK GENMASK(20, 17)
  62. #define U2P_R1_TX_HSXV_TUNE_MASK GENMASK(22, 21)
  63. #define U2P_R1_OTG_TUNE_MASK GENMASK(25, 23)
  64. #define U2P_R1_SQRX_TUNE_MASK GENMASK(28, 26)
  65. #define U2P_R1_COMP_DIS_TUNE_MASK GENMASK(31, 29)
  66. /* bits [31:14] are read-only */
  67. #define U2P_R2 0x8
  68. #define U2P_R2_TESTDATA_IN_MASK GENMASK(7, 0)
  69. #define U2P_R2_TESTADDR_MASK GENMASK(11, 8)
  70. #define U2P_R2_TESTDATA_OUT_SEL BIT(12)
  71. #define U2P_R2_TESTCLK BIT(13)
  72. #define U2P_R2_TESTDATA_OUT_MASK GENMASK(17, 14)
  73. #define U2P_R2_ACA_PIN_RANGE_C BIT(18)
  74. #define U2P_R2_ACA_PIN_RANGE_B BIT(19)
  75. #define U2P_R2_ACA_PIN_RANGE_A BIT(20)
  76. #define U2P_R2_ACA_PIN_GND BIT(21)
  77. #define U2P_R2_ACA_PIN_FLOAT BIT(22)
  78. #define U2P_R2_CHARGE_DETECT BIT(23)
  79. #define U2P_R2_DEVICE_SESSION_VALID BIT(24)
  80. #define U2P_R2_ADP_PROBE BIT(25)
  81. #define U2P_R2_ADP_SENSE BIT(26)
  82. #define U2P_R2_SESSION_END BIT(27)
  83. #define U2P_R2_VBUS_VALID BIT(28)
  84. #define U2P_R2_B_VALID BIT(29)
  85. #define U2P_R2_A_VALID BIT(30)
  86. #define U2P_R2_ID_DIG BIT(31)
  87. #define U2P_R3 0xc
  88. #define RESET_COMPLETE_TIME 500
  89. struct phy_meson_gxl_usb2_priv {
  90. struct regmap *regmap;
  91. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  92. struct udevice *phy_supply;
  93. #endif
  94. #if CONFIG_IS_ENABLED(CLK)
  95. struct clk clk;
  96. #endif
  97. };
  98. static void phy_meson_gxl_usb2_reset(struct phy_meson_gxl_usb2_priv *priv)
  99. {
  100. uint val;
  101. regmap_read(priv->regmap, U2P_R0, &val);
  102. /* reset the PHY and wait until settings are stabilized */
  103. val |= U2P_R0_POWER_ON_RESET;
  104. regmap_write(priv->regmap, U2P_R0, val);
  105. udelay(RESET_COMPLETE_TIME);
  106. val &= ~U2P_R0_POWER_ON_RESET;
  107. regmap_write(priv->regmap, U2P_R0, val);
  108. udelay(RESET_COMPLETE_TIME);
  109. }
  110. static void
  111. phy_meson_gxl_usb2_set_host_mode(struct phy_meson_gxl_usb2_priv *priv)
  112. {
  113. uint val;
  114. regmap_read(priv->regmap, U2P_R0, &val);
  115. val |= U2P_R0_DM_PULLDOWN;
  116. val |= U2P_R0_DP_PULLDOWN;
  117. val &= ~U2P_R0_ID_PULLUP;
  118. regmap_write(priv->regmap, U2P_R0, val);
  119. phy_meson_gxl_usb2_reset(priv);
  120. }
  121. static int phy_meson_gxl_usb2_power_on(struct phy *phy)
  122. {
  123. struct udevice *dev = phy->dev;
  124. struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
  125. uint val;
  126. regmap_read(priv->regmap, U2P_R0, &val);
  127. /* power on the PHY by taking it out of reset mode */
  128. val &= ~U2P_R0_POWER_ON_RESET;
  129. regmap_write(priv->regmap, U2P_R0, val);
  130. phy_meson_gxl_usb2_set_host_mode(priv);
  131. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  132. if (priv->phy_supply) {
  133. int ret = regulator_set_enable(priv->phy_supply, true);
  134. if (ret)
  135. return ret;
  136. }
  137. #endif
  138. return 0;
  139. }
  140. static int phy_meson_gxl_usb2_power_off(struct phy *phy)
  141. {
  142. struct udevice *dev = phy->dev;
  143. struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
  144. uint val;
  145. regmap_read(priv->regmap, U2P_R0, &val);
  146. /* power off the PHY by putting it into reset mode */
  147. val |= U2P_R0_POWER_ON_RESET;
  148. regmap_write(priv->regmap, U2P_R0, val);
  149. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  150. if (priv->phy_supply) {
  151. int ret = regulator_set_enable(priv->phy_supply, false);
  152. if (ret) {
  153. pr_err("Error disabling PHY supply\n");
  154. return ret;
  155. }
  156. }
  157. #endif
  158. return 0;
  159. }
  160. struct phy_ops meson_gxl_usb2_phy_ops = {
  161. .power_on = phy_meson_gxl_usb2_power_on,
  162. .power_off = phy_meson_gxl_usb2_power_off,
  163. };
  164. int meson_gxl_usb2_phy_probe(struct udevice *dev)
  165. {
  166. struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
  167. int ret;
  168. ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
  169. if (ret)
  170. return ret;
  171. #if CONFIG_IS_ENABLED(CLK)
  172. ret = clk_get_by_index(dev, 0, &priv->clk);
  173. if (ret < 0)
  174. return ret;
  175. ret = clk_enable(&priv->clk);
  176. if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
  177. pr_err("failed to enable PHY clock\n");
  178. clk_free(&priv->clk);
  179. return ret;
  180. }
  181. #endif
  182. #if CONFIG_IS_ENABLED(DM_REGULATOR)
  183. ret = device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
  184. if (ret && ret != -ENOENT) {
  185. pr_err("Failed to get PHY regulator\n");
  186. return ret;
  187. }
  188. #endif
  189. return 0;
  190. }
  191. static const struct udevice_id meson_gxl_usb2_phy_ids[] = {
  192. { .compatible = "amlogic,meson-gxl-usb2-phy" },
  193. { }
  194. };
  195. U_BOOT_DRIVER(meson_gxl_usb2_phy) = {
  196. .name = "meson_gxl_usb2_phy",
  197. .id = UCLASS_PHY,
  198. .of_match = meson_gxl_usb2_phy_ids,
  199. .probe = meson_gxl_usb2_phy_probe,
  200. .ops = &meson_gxl_usb2_phy_ops,
  201. .priv_auto_alloc_size = sizeof(struct phy_meson_gxl_usb2_priv),
  202. };