phy-sun50i-usb3.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Allwinner sun50i(H6) USB 3.0 phy driver
  4. *
  5. * Copyright (C) 2017 Icenowy Zheng <icenowy@aosc.io>
  6. *
  7. * Based on phy-sun9i-usb.c, which is:
  8. *
  9. * Copyright (C) 2014-2015 Chen-Yu Tsai <wens@csie.org>
  10. *
  11. * Based on code from Allwinner BSP, which is:
  12. *
  13. * Copyright (c) 2010-2015 Allwinner Technology Co., Ltd.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/reset.h>
  23. /* Interface Status and Control Registers */
  24. #define SUNXI_ISCR 0x00
  25. #define SUNXI_PIPE_CLOCK_CONTROL 0x14
  26. #define SUNXI_PHY_TUNE_LOW 0x18
  27. #define SUNXI_PHY_TUNE_HIGH 0x1c
  28. #define SUNXI_PHY_EXTERNAL_CONTROL 0x20
  29. /* USB2.0 Interface Status and Control Register */
  30. #define SUNXI_ISCR_FORCE_VBUS (3 << 12)
  31. /* PIPE Clock Control Register */
  32. #define SUNXI_PCC_PIPE_CLK_OPEN (1 << 6)
  33. /* PHY External Control Register */
  34. #define SUNXI_PEC_EXTERN_VBUS (3 << 1)
  35. #define SUNXI_PEC_SSC_EN (1 << 24)
  36. #define SUNXI_PEC_REF_SSP_EN (1 << 26)
  37. /* PHY Tune High Register */
  38. #define SUNXI_TX_DEEMPH_3P5DB(n) ((n) << 19)
  39. #define SUNXI_TX_DEEMPH_3P5DB_MASK GENMASK(24, 19)
  40. #define SUNXI_TX_DEEMPH_6DB(n) ((n) << 13)
  41. #define SUNXI_TX_DEEMPH_6GB_MASK GENMASK(18, 13)
  42. #define SUNXI_TX_SWING_FULL(n) ((n) << 6)
  43. #define SUNXI_TX_SWING_FULL_MASK GENMASK(12, 6)
  44. #define SUNXI_LOS_BIAS(n) ((n) << 3)
  45. #define SUNXI_LOS_BIAS_MASK GENMASK(5, 3)
  46. #define SUNXI_TXVBOOSTLVL(n) ((n) << 0)
  47. #define SUNXI_TXVBOOSTLVL_MASK GENMASK(2, 0)
  48. struct sun50i_usb3_phy {
  49. struct phy *phy;
  50. void __iomem *regs;
  51. struct reset_control *reset;
  52. struct clk *clk;
  53. };
  54. static void sun50i_usb3_phy_open(struct sun50i_usb3_phy *phy)
  55. {
  56. u32 val;
  57. val = readl(phy->regs + SUNXI_PHY_EXTERNAL_CONTROL);
  58. val |= SUNXI_PEC_EXTERN_VBUS;
  59. val |= SUNXI_PEC_SSC_EN | SUNXI_PEC_REF_SSP_EN;
  60. writel(val, phy->regs + SUNXI_PHY_EXTERNAL_CONTROL);
  61. val = readl(phy->regs + SUNXI_PIPE_CLOCK_CONTROL);
  62. val |= SUNXI_PCC_PIPE_CLK_OPEN;
  63. writel(val, phy->regs + SUNXI_PIPE_CLOCK_CONTROL);
  64. val = readl(phy->regs + SUNXI_ISCR);
  65. val |= SUNXI_ISCR_FORCE_VBUS;
  66. writel(val, phy->regs + SUNXI_ISCR);
  67. /*
  68. * All the magic numbers written to the PHY_TUNE_{LOW_HIGH}
  69. * registers are directly taken from the BSP USB3 driver from
  70. * Allwiner.
  71. */
  72. writel(0x0047fc87, phy->regs + SUNXI_PHY_TUNE_LOW);
  73. val = readl(phy->regs + SUNXI_PHY_TUNE_HIGH);
  74. val &= ~(SUNXI_TXVBOOSTLVL_MASK | SUNXI_LOS_BIAS_MASK |
  75. SUNXI_TX_SWING_FULL_MASK | SUNXI_TX_DEEMPH_6GB_MASK |
  76. SUNXI_TX_DEEMPH_3P5DB_MASK);
  77. val |= SUNXI_TXVBOOSTLVL(0x7);
  78. val |= SUNXI_LOS_BIAS(0x7);
  79. val |= SUNXI_TX_SWING_FULL(0x55);
  80. val |= SUNXI_TX_DEEMPH_6DB(0x20);
  81. val |= SUNXI_TX_DEEMPH_3P5DB(0x15);
  82. writel(val, phy->regs + SUNXI_PHY_TUNE_HIGH);
  83. }
  84. static int sun50i_usb3_phy_init(struct phy *_phy)
  85. {
  86. struct sun50i_usb3_phy *phy = phy_get_drvdata(_phy);
  87. int ret;
  88. ret = clk_prepare_enable(phy->clk);
  89. if (ret)
  90. return ret;
  91. ret = reset_control_deassert(phy->reset);
  92. if (ret) {
  93. clk_disable_unprepare(phy->clk);
  94. return ret;
  95. }
  96. sun50i_usb3_phy_open(phy);
  97. return 0;
  98. }
  99. static int sun50i_usb3_phy_exit(struct phy *_phy)
  100. {
  101. struct sun50i_usb3_phy *phy = phy_get_drvdata(_phy);
  102. reset_control_assert(phy->reset);
  103. clk_disable_unprepare(phy->clk);
  104. return 0;
  105. }
  106. static const struct phy_ops sun50i_usb3_phy_ops = {
  107. .init = sun50i_usb3_phy_init,
  108. .exit = sun50i_usb3_phy_exit,
  109. .owner = THIS_MODULE,
  110. };
  111. static int sun50i_usb3_phy_probe(struct platform_device *pdev)
  112. {
  113. struct sun50i_usb3_phy *phy;
  114. struct device *dev = &pdev->dev;
  115. struct phy_provider *phy_provider;
  116. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  117. if (!phy)
  118. return -ENOMEM;
  119. phy->clk = devm_clk_get(dev, NULL);
  120. if (IS_ERR(phy->clk)) {
  121. if (PTR_ERR(phy->clk) != -EPROBE_DEFER)
  122. dev_err(dev, "failed to get phy clock\n");
  123. return PTR_ERR(phy->clk);
  124. }
  125. phy->reset = devm_reset_control_get(dev, NULL);
  126. if (IS_ERR(phy->reset)) {
  127. dev_err(dev, "failed to get reset control\n");
  128. return PTR_ERR(phy->reset);
  129. }
  130. phy->regs = devm_platform_ioremap_resource(pdev, 0);
  131. if (IS_ERR(phy->regs))
  132. return PTR_ERR(phy->regs);
  133. phy->phy = devm_phy_create(dev, NULL, &sun50i_usb3_phy_ops);
  134. if (IS_ERR(phy->phy)) {
  135. dev_err(dev, "failed to create PHY\n");
  136. return PTR_ERR(phy->phy);
  137. }
  138. phy_set_drvdata(phy->phy, phy);
  139. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  140. return PTR_ERR_OR_ZERO(phy_provider);
  141. }
  142. static const struct of_device_id sun50i_usb3_phy_of_match[] = {
  143. { .compatible = "allwinner,sun50i-h6-usb3-phy" },
  144. { },
  145. };
  146. MODULE_DEVICE_TABLE(of, sun50i_usb3_phy_of_match);
  147. static struct platform_driver sun50i_usb3_phy_driver = {
  148. .probe = sun50i_usb3_phy_probe,
  149. .driver = {
  150. .of_match_table = sun50i_usb3_phy_of_match,
  151. .name = "sun50i-usb3-phy",
  152. }
  153. };
  154. module_platform_driver(sun50i_usb3_phy_driver);
  155. MODULE_DESCRIPTION("Allwinner H6 USB 3.0 phy driver");
  156. MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
  157. MODULE_LICENSE("GPL");