phy-meson-gxl-usb3.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Meson GXL USB3 PHY and OTG mode detection driver
  4. *
  5. * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/phy/phy.h>
  13. #include <linux/regmap.h>
  14. #include <linux/reset.h>
  15. #include <linux/platform_device.h>
  16. #define USB_R0 0x00
  17. #define USB_R0_P30_FSEL_MASK GENMASK(5, 0)
  18. #define USB_R0_P30_PHY_RESET BIT(6)
  19. #define USB_R0_P30_TEST_POWERDOWN_HSP BIT(7)
  20. #define USB_R0_P30_TEST_POWERDOWN_SSP BIT(8)
  21. #define USB_R0_P30_ACJT_LEVEL_MASK GENMASK(13, 9)
  22. #define USB_R0_P30_TX_BOOST_LEVEL_MASK GENMASK(16, 14)
  23. #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
  24. #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
  25. #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
  26. #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
  27. #define USB_R0_U2D_ACT BIT(31)
  28. #define USB_R1 0x04
  29. #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
  30. #define USB_R1_U3H_PME_ENABLE BIT(1)
  31. #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(6, 2)
  32. #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(11, 7)
  33. #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(15, 12)
  34. #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
  35. #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
  36. #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
  37. #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
  38. #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
  39. #define USB_R2 0x08
  40. #define USB_R2_P30_CR_DATA_IN_MASK GENMASK(15, 0)
  41. #define USB_R2_P30_CR_READ BIT(16)
  42. #define USB_R2_P30_CR_WRITE BIT(17)
  43. #define USB_R2_P30_CR_CAP_ADDR BIT(18)
  44. #define USB_R2_P30_CR_CAP_DATA BIT(19)
  45. #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
  46. #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
  47. #define USB_R3 0x0c
  48. #define USB_R3_P30_SSC_ENABLE BIT(0)
  49. #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
  50. #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
  51. #define USB_R3_P30_REF_SSP_EN BIT(13)
  52. #define USB_R3_P30_LOS_BIAS_MASK GENMASK(18, 16)
  53. #define USB_R3_P30_LOS_LEVEL_MASK GENMASK(23, 19)
  54. #define USB_R3_P30_MPLL_MULTIPLIER_MASK GENMASK(30, 24)
  55. #define USB_R4 0x10
  56. #define USB_R4_P21_PORT_RESET_0 BIT(0)
  57. #define USB_R4_P21_SLEEP_M0 BIT(1)
  58. #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
  59. #define USB_R4_P21_ONLY BIT(4)
  60. #define USB_R5 0x14
  61. #define USB_R5_ID_DIG_SYNC BIT(0)
  62. #define USB_R5_ID_DIG_REG BIT(1)
  63. #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
  64. #define USB_R5_ID_DIG_EN_0 BIT(4)
  65. #define USB_R5_ID_DIG_EN_1 BIT(5)
  66. #define USB_R5_ID_DIG_CURR BIT(6)
  67. #define USB_R5_ID_DIG_IRQ BIT(7)
  68. #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
  69. #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
  70. /* read-only register */
  71. #define USB_R6 0x18
  72. #define USB_R6_P30_CR_DATA_OUT_MASK GENMASK(15, 0)
  73. #define USB_R6_P30_CR_ACK BIT(16)
  74. struct phy_meson_gxl_usb3_priv {
  75. struct regmap *regmap;
  76. enum phy_mode mode;
  77. struct clk *clk_phy;
  78. struct clk *clk_peripheral;
  79. struct reset_control *reset;
  80. };
  81. static const struct regmap_config phy_meson_gxl_usb3_regmap_conf = {
  82. .reg_bits = 8,
  83. .val_bits = 32,
  84. .reg_stride = 4,
  85. .max_register = USB_R6,
  86. };
  87. static int phy_meson_gxl_usb3_power_on(struct phy *phy)
  88. {
  89. struct phy_meson_gxl_usb3_priv *priv = phy_get_drvdata(phy);
  90. regmap_update_bits(priv->regmap, USB_R5, USB_R5_ID_DIG_EN_0,
  91. USB_R5_ID_DIG_EN_0);
  92. regmap_update_bits(priv->regmap, USB_R5, USB_R5_ID_DIG_EN_1,
  93. USB_R5_ID_DIG_EN_1);
  94. regmap_update_bits(priv->regmap, USB_R5, USB_R5_ID_DIG_TH_MASK,
  95. FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff));
  96. return 0;
  97. }
  98. static int phy_meson_gxl_usb3_power_off(struct phy *phy)
  99. {
  100. struct phy_meson_gxl_usb3_priv *priv = phy_get_drvdata(phy);
  101. regmap_update_bits(priv->regmap, USB_R5, USB_R5_ID_DIG_EN_0, 0);
  102. regmap_update_bits(priv->regmap, USB_R5, USB_R5_ID_DIG_EN_1, 0);
  103. return 0;
  104. }
  105. static int phy_meson_gxl_usb3_set_mode(struct phy *phy, enum phy_mode mode)
  106. {
  107. struct phy_meson_gxl_usb3_priv *priv = phy_get_drvdata(phy);
  108. switch (mode) {
  109. case PHY_MODE_USB_HOST:
  110. regmap_update_bits(priv->regmap, USB_R0, USB_R0_U2D_ACT, 0);
  111. regmap_update_bits(priv->regmap, USB_R4, USB_R4_P21_SLEEP_M0,
  112. 0);
  113. break;
  114. case PHY_MODE_USB_DEVICE:
  115. regmap_update_bits(priv->regmap, USB_R0, USB_R0_U2D_ACT,
  116. USB_R0_U2D_ACT);
  117. regmap_update_bits(priv->regmap, USB_R4, USB_R4_P21_SLEEP_M0,
  118. USB_R4_P21_SLEEP_M0);
  119. break;
  120. default:
  121. dev_err(&phy->dev, "unsupported PHY mode %d\n", mode);
  122. return -EINVAL;
  123. }
  124. priv->mode = mode;
  125. return 0;
  126. }
  127. static int phy_meson_gxl_usb3_init(struct phy *phy)
  128. {
  129. struct phy_meson_gxl_usb3_priv *priv = phy_get_drvdata(phy);
  130. int ret;
  131. ret = reset_control_reset(priv->reset);
  132. if (ret)
  133. goto err;
  134. ret = clk_prepare_enable(priv->clk_phy);
  135. if (ret)
  136. goto err;
  137. ret = clk_prepare_enable(priv->clk_peripheral);
  138. if (ret)
  139. goto err_disable_clk_phy;
  140. ret = phy_meson_gxl_usb3_set_mode(phy, priv->mode);
  141. if (ret)
  142. goto err_disable_clk_peripheral;
  143. regmap_update_bits(priv->regmap, USB_R1,
  144. USB_R1_U3H_FLADJ_30MHZ_REG_MASK,
  145. FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20));
  146. return 0;
  147. err_disable_clk_peripheral:
  148. clk_disable_unprepare(priv->clk_peripheral);
  149. err_disable_clk_phy:
  150. clk_disable_unprepare(priv->clk_phy);
  151. err:
  152. return ret;
  153. }
  154. static int phy_meson_gxl_usb3_exit(struct phy *phy)
  155. {
  156. struct phy_meson_gxl_usb3_priv *priv = phy_get_drvdata(phy);
  157. clk_disable_unprepare(priv->clk_peripheral);
  158. clk_disable_unprepare(priv->clk_phy);
  159. return 0;
  160. }
  161. static const struct phy_ops phy_meson_gxl_usb3_ops = {
  162. .power_on = phy_meson_gxl_usb3_power_on,
  163. .power_off = phy_meson_gxl_usb3_power_off,
  164. .set_mode = phy_meson_gxl_usb3_set_mode,
  165. .init = phy_meson_gxl_usb3_init,
  166. .exit = phy_meson_gxl_usb3_exit,
  167. .owner = THIS_MODULE,
  168. };
  169. static int phy_meson_gxl_usb3_probe(struct platform_device *pdev)
  170. {
  171. struct device *dev = &pdev->dev;
  172. struct device_node *np = dev->of_node;
  173. struct phy_meson_gxl_usb3_priv *priv;
  174. struct resource *res;
  175. struct phy *phy;
  176. struct phy_provider *phy_provider;
  177. void __iomem *base;
  178. int ret;
  179. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  180. if (!priv)
  181. return -ENOMEM;
  182. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  183. base = devm_ioremap_resource(dev, res);
  184. if (IS_ERR(base))
  185. return PTR_ERR(base);
  186. priv->regmap = devm_regmap_init_mmio(dev, base,
  187. &phy_meson_gxl_usb3_regmap_conf);
  188. if (IS_ERR(priv->regmap))
  189. return PTR_ERR(priv->regmap);
  190. priv->clk_phy = devm_clk_get(dev, "phy");
  191. if (IS_ERR(priv->clk_phy))
  192. return PTR_ERR(priv->clk_phy);
  193. priv->clk_peripheral = devm_clk_get(dev, "peripheral");
  194. if (IS_ERR(priv->clk_peripheral))
  195. return PTR_ERR(priv->clk_peripheral);
  196. priv->reset = devm_reset_control_array_get_shared(dev);
  197. if (IS_ERR(priv->reset))
  198. return PTR_ERR(priv->reset);
  199. /*
  200. * default to host mode as hardware defaults and/or boot-loader
  201. * behavior can result in this PHY starting up in device mode. this
  202. * default and the initialization in phy_meson_gxl_usb3_init ensure
  203. * that we reproducibly start in a known mode on all devices.
  204. */
  205. priv->mode = PHY_MODE_USB_HOST;
  206. phy = devm_phy_create(dev, np, &phy_meson_gxl_usb3_ops);
  207. if (IS_ERR(phy)) {
  208. ret = PTR_ERR(phy);
  209. if (ret != -EPROBE_DEFER)
  210. dev_err(dev, "failed to create PHY\n");
  211. return ret;
  212. }
  213. phy_set_drvdata(phy, priv);
  214. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  215. return PTR_ERR_OR_ZERO(phy_provider);
  216. }
  217. static const struct of_device_id phy_meson_gxl_usb3_of_match[] = {
  218. { .compatible = "amlogic,meson-gxl-usb3-phy", },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(of, phy_meson_gxl_usb3_of_match);
  222. static struct platform_driver phy_meson_gxl_usb3_driver = {
  223. .probe = phy_meson_gxl_usb3_probe,
  224. .driver = {
  225. .name = "phy-meson-gxl-usb3",
  226. .of_match_table = phy_meson_gxl_usb3_of_match,
  227. },
  228. };
  229. module_platform_driver(phy_meson_gxl_usb3_driver);
  230. MODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>");
  231. MODULE_DESCRIPTION("Meson GXL USB3 PHY and OTG detection driver");
  232. MODULE_LICENSE("GPL v2");