meson-gxl-usb2.c 6.4 KB

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