phy-meson-gxl-usb2.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Meson GXL and GXM USB2 PHY driver
  4. *
  5. * Copyright (C) 2017 Martin Blumenstingl <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/reset.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. /* bits [31:27] are read-only */
  17. #define U2P_R0 0x0
  18. #define U2P_R0_BYPASS_SEL BIT(0)
  19. #define U2P_R0_BYPASS_DM_EN BIT(1)
  20. #define U2P_R0_BYPASS_DP_EN BIT(2)
  21. #define U2P_R0_TXBITSTUFF_ENH BIT(3)
  22. #define U2P_R0_TXBITSTUFF_EN BIT(4)
  23. #define U2P_R0_DM_PULLDOWN BIT(5)
  24. #define U2P_R0_DP_PULLDOWN BIT(6)
  25. #define U2P_R0_DP_VBUS_VLD_EXT_SEL BIT(7)
  26. #define U2P_R0_DP_VBUS_VLD_EXT BIT(8)
  27. #define U2P_R0_ADP_PRB_EN BIT(9)
  28. #define U2P_R0_ADP_DISCHARGE BIT(10)
  29. #define U2P_R0_ADP_CHARGE BIT(11)
  30. #define U2P_R0_DRV_VBUS BIT(12)
  31. #define U2P_R0_ID_PULLUP BIT(13)
  32. #define U2P_R0_LOOPBACK_EN_B BIT(14)
  33. #define U2P_R0_OTG_DISABLE BIT(15)
  34. #define U2P_R0_COMMON_ONN BIT(16)
  35. #define U2P_R0_FSEL_MASK GENMASK(19, 17)
  36. #define U2P_R0_REF_CLK_SEL_MASK GENMASK(21, 20)
  37. #define U2P_R0_POWER_ON_RESET BIT(22)
  38. #define U2P_R0_V_ATE_TEST_EN_B_MASK GENMASK(24, 23)
  39. #define U2P_R0_ID_SET_ID_DQ BIT(25)
  40. #define U2P_R0_ATE_RESET BIT(26)
  41. #define U2P_R0_FSV_MINUS BIT(27)
  42. #define U2P_R0_FSV_PLUS BIT(28)
  43. #define U2P_R0_BYPASS_DM_DATA BIT(29)
  44. #define U2P_R0_BYPASS_DP_DATA BIT(30)
  45. #define U2P_R1 0x4
  46. #define U2P_R1_BURN_IN_TEST BIT(0)
  47. #define U2P_R1_ACA_ENABLE BIT(1)
  48. #define U2P_R1_DCD_ENABLE BIT(2)
  49. #define U2P_R1_VDAT_SRC_EN_B BIT(3)
  50. #define U2P_R1_VDAT_DET_EN_B BIT(4)
  51. #define U2P_R1_CHARGES_SEL BIT(5)
  52. #define U2P_R1_TX_PREEMP_PULSE_TUNE BIT(6)
  53. #define U2P_R1_TX_PREEMP_AMP_TUNE_MASK GENMASK(8, 7)
  54. #define U2P_R1_TX_RES_TUNE_MASK GENMASK(10, 9)
  55. #define U2P_R1_TX_RISE_TUNE_MASK GENMASK(12, 11)
  56. #define U2P_R1_TX_VREF_TUNE_MASK GENMASK(16, 13)
  57. #define U2P_R1_TX_FSLS_TUNE_MASK GENMASK(20, 17)
  58. #define U2P_R1_TX_HSXV_TUNE_MASK GENMASK(22, 21)
  59. #define U2P_R1_OTG_TUNE_MASK GENMASK(25, 23)
  60. #define U2P_R1_SQRX_TUNE_MASK GENMASK(28, 26)
  61. #define U2P_R1_COMP_DIS_TUNE_MASK GENMASK(31, 29)
  62. /* bits [31:14] are read-only */
  63. #define U2P_R2 0x8
  64. #define U2P_R2_TESTDATA_IN_MASK GENMASK(7, 0)
  65. #define U2P_R2_TESTADDR_MASK GENMASK(11, 8)
  66. #define U2P_R2_TESTDATA_OUT_SEL BIT(12)
  67. #define U2P_R2_TESTCLK BIT(13)
  68. #define U2P_R2_TESTDATA_OUT_MASK GENMASK(17, 14)
  69. #define U2P_R2_ACA_PIN_RANGE_C BIT(18)
  70. #define U2P_R2_ACA_PIN_RANGE_B BIT(19)
  71. #define U2P_R2_ACA_PIN_RANGE_A BIT(20)
  72. #define U2P_R2_ACA_PIN_GND BIT(21)
  73. #define U2P_R2_ACA_PIN_FLOAT BIT(22)
  74. #define U2P_R2_CHARGE_DETECT BIT(23)
  75. #define U2P_R2_DEVICE_SESSION_VALID BIT(24)
  76. #define U2P_R2_ADP_PROBE BIT(25)
  77. #define U2P_R2_ADP_SENSE BIT(26)
  78. #define U2P_R2_SESSION_END BIT(27)
  79. #define U2P_R2_VBUS_VALID BIT(28)
  80. #define U2P_R2_B_VALID BIT(29)
  81. #define U2P_R2_A_VALID BIT(30)
  82. #define U2P_R2_ID_DIG BIT(31)
  83. #define U2P_R3 0xc
  84. #define RESET_COMPLETE_TIME 500
  85. struct phy_meson_gxl_usb2_priv {
  86. struct regmap *regmap;
  87. enum phy_mode mode;
  88. int is_enabled;
  89. struct clk *clk;
  90. struct reset_control *reset;
  91. };
  92. static const struct regmap_config phy_meson_gxl_usb2_regmap_conf = {
  93. .reg_bits = 8,
  94. .val_bits = 32,
  95. .reg_stride = 4,
  96. .max_register = U2P_R3,
  97. };
  98. static int phy_meson_gxl_usb2_init(struct phy *phy)
  99. {
  100. struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy);
  101. int ret;
  102. ret = reset_control_reset(priv->reset);
  103. if (ret)
  104. return ret;
  105. ret = clk_prepare_enable(priv->clk);
  106. if (ret) {
  107. reset_control_rearm(priv->reset);
  108. return ret;
  109. }
  110. return 0;
  111. }
  112. static int phy_meson_gxl_usb2_exit(struct phy *phy)
  113. {
  114. struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy);
  115. clk_disable_unprepare(priv->clk);
  116. reset_control_rearm(priv->reset);
  117. return 0;
  118. }
  119. static int phy_meson_gxl_usb2_reset(struct phy *phy)
  120. {
  121. struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy);
  122. if (priv->is_enabled) {
  123. /* reset the PHY and wait until settings are stabilized */
  124. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET,
  125. U2P_R0_POWER_ON_RESET);
  126. udelay(RESET_COMPLETE_TIME);
  127. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET,
  128. 0);
  129. udelay(RESET_COMPLETE_TIME);
  130. }
  131. return 0;
  132. }
  133. static int phy_meson_gxl_usb2_set_mode(struct phy *phy,
  134. enum phy_mode mode, int submode)
  135. {
  136. struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy);
  137. switch (mode) {
  138. case PHY_MODE_USB_HOST:
  139. case PHY_MODE_USB_OTG:
  140. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DM_PULLDOWN,
  141. U2P_R0_DM_PULLDOWN);
  142. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DP_PULLDOWN,
  143. U2P_R0_DP_PULLDOWN);
  144. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_ID_PULLUP,
  145. U2P_R0_ID_PULLUP);
  146. break;
  147. case PHY_MODE_USB_DEVICE:
  148. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DM_PULLDOWN,
  149. 0);
  150. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DP_PULLDOWN,
  151. 0);
  152. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_ID_PULLUP,
  153. U2P_R0_ID_PULLUP);
  154. break;
  155. default:
  156. return -EINVAL;
  157. }
  158. phy_meson_gxl_usb2_reset(phy);
  159. priv->mode = mode;
  160. return 0;
  161. }
  162. static int phy_meson_gxl_usb2_power_off(struct phy *phy)
  163. {
  164. struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy);
  165. priv->is_enabled = 0;
  166. /* power off the PHY by putting it into reset mode */
  167. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET,
  168. U2P_R0_POWER_ON_RESET);
  169. return 0;
  170. }
  171. static int phy_meson_gxl_usb2_power_on(struct phy *phy)
  172. {
  173. struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy);
  174. int ret;
  175. priv->is_enabled = 1;
  176. /* power on the PHY by taking it out of reset mode */
  177. regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET, 0);
  178. ret = phy_meson_gxl_usb2_set_mode(phy, priv->mode, 0);
  179. if (ret) {
  180. phy_meson_gxl_usb2_power_off(phy);
  181. dev_err(&phy->dev, "Failed to initialize PHY with mode %d\n",
  182. priv->mode);
  183. return ret;
  184. }
  185. return 0;
  186. }
  187. static const struct phy_ops phy_meson_gxl_usb2_ops = {
  188. .init = phy_meson_gxl_usb2_init,
  189. .exit = phy_meson_gxl_usb2_exit,
  190. .power_on = phy_meson_gxl_usb2_power_on,
  191. .power_off = phy_meson_gxl_usb2_power_off,
  192. .set_mode = phy_meson_gxl_usb2_set_mode,
  193. .reset = phy_meson_gxl_usb2_reset,
  194. .owner = THIS_MODULE,
  195. };
  196. static int phy_meson_gxl_usb2_probe(struct platform_device *pdev)
  197. {
  198. struct device *dev = &pdev->dev;
  199. struct phy_provider *phy_provider;
  200. struct phy_meson_gxl_usb2_priv *priv;
  201. struct phy *phy;
  202. void __iomem *base;
  203. int ret;
  204. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  205. if (!priv)
  206. return -ENOMEM;
  207. platform_set_drvdata(pdev, priv);
  208. base = devm_platform_ioremap_resource(pdev, 0);
  209. if (IS_ERR(base))
  210. return PTR_ERR(base);
  211. /* start in host mode */
  212. priv->mode = PHY_MODE_USB_HOST;
  213. priv->regmap = devm_regmap_init_mmio(dev, base,
  214. &phy_meson_gxl_usb2_regmap_conf);
  215. if (IS_ERR(priv->regmap))
  216. return PTR_ERR(priv->regmap);
  217. priv->clk = devm_clk_get_optional(dev, "phy");
  218. if (IS_ERR(priv->clk))
  219. return PTR_ERR(priv->clk);
  220. priv->reset = devm_reset_control_get_optional_shared(dev, "phy");
  221. if (IS_ERR(priv->reset))
  222. return PTR_ERR(priv->reset);
  223. phy = devm_phy_create(dev, NULL, &phy_meson_gxl_usb2_ops);
  224. if (IS_ERR(phy)) {
  225. ret = PTR_ERR(phy);
  226. if (ret != -EPROBE_DEFER)
  227. dev_err(dev, "failed to create PHY\n");
  228. return ret;
  229. }
  230. phy_set_drvdata(phy, priv);
  231. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  232. return PTR_ERR_OR_ZERO(phy_provider);
  233. }
  234. static const struct of_device_id phy_meson_gxl_usb2_of_match[] = {
  235. { .compatible = "amlogic,meson-gxl-usb2-phy", },
  236. { },
  237. };
  238. MODULE_DEVICE_TABLE(of, phy_meson_gxl_usb2_of_match);
  239. static struct platform_driver phy_meson_gxl_usb2_driver = {
  240. .probe = phy_meson_gxl_usb2_probe,
  241. .driver = {
  242. .name = "phy-meson-gxl-usb2",
  243. .of_match_table = phy_meson_gxl_usb2_of_match,
  244. },
  245. };
  246. module_platform_driver(phy_meson_gxl_usb2_driver);
  247. MODULE_AUTHOR("Martin Blumenstingl <[email protected]>");
  248. MODULE_DESCRIPTION("Meson GXL and GXM USB2 PHY driver");
  249. MODULE_LICENSE("GPL v2");