phy-hisi-inno-usb2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HiSilicon INNO USB2 PHY Driver.
  4. *
  5. * Copyright (c) 2016-2017 HiSilicon Technologies Co., Ltd.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/phy/phy.h>
  13. #include <linux/reset.h>
  14. #define INNO_PHY_PORT_NUM 2
  15. #define REF_CLK_STABLE_TIME 100 /* unit:us */
  16. #define UTMI_CLK_STABLE_TIME 200 /* unit:us */
  17. #define TEST_CLK_STABLE_TIME 2 /* unit:ms */
  18. #define PHY_CLK_STABLE_TIME 2 /* unit:ms */
  19. #define UTMI_RST_COMPLETE_TIME 2 /* unit:ms */
  20. #define POR_RST_COMPLETE_TIME 300 /* unit:us */
  21. #define PHY_TEST_DATA GENMASK(7, 0)
  22. #define PHY_TEST_ADDR GENMASK(15, 8)
  23. #define PHY_TEST_PORT GENMASK(18, 16)
  24. #define PHY_TEST_WREN BIT(21)
  25. #define PHY_TEST_CLK BIT(22) /* rising edge active */
  26. #define PHY_TEST_RST BIT(23) /* low active */
  27. #define PHY_CLK_ENABLE BIT(2)
  28. struct hisi_inno_phy_port {
  29. struct reset_control *utmi_rst;
  30. struct hisi_inno_phy_priv *priv;
  31. };
  32. struct hisi_inno_phy_priv {
  33. void __iomem *mmio;
  34. struct clk *ref_clk;
  35. struct reset_control *por_rst;
  36. struct hisi_inno_phy_port ports[INNO_PHY_PORT_NUM];
  37. };
  38. static void hisi_inno_phy_write_reg(struct hisi_inno_phy_priv *priv,
  39. u8 port, u32 addr, u32 data)
  40. {
  41. void __iomem *reg = priv->mmio;
  42. u32 val;
  43. val = (data & PHY_TEST_DATA) |
  44. ((addr << 8) & PHY_TEST_ADDR) |
  45. ((port << 16) & PHY_TEST_PORT) |
  46. PHY_TEST_WREN | PHY_TEST_RST;
  47. writel(val, reg);
  48. val |= PHY_TEST_CLK;
  49. writel(val, reg);
  50. val &= ~PHY_TEST_CLK;
  51. writel(val, reg);
  52. }
  53. static void hisi_inno_phy_setup(struct hisi_inno_phy_priv *priv)
  54. {
  55. /* The phy clk is controlled by the port0 register 0x06. */
  56. hisi_inno_phy_write_reg(priv, 0, 0x06, PHY_CLK_ENABLE);
  57. msleep(PHY_CLK_STABLE_TIME);
  58. }
  59. static int hisi_inno_phy_init(struct phy *phy)
  60. {
  61. struct hisi_inno_phy_port *port = phy_get_drvdata(phy);
  62. struct hisi_inno_phy_priv *priv = port->priv;
  63. int ret;
  64. ret = clk_prepare_enable(priv->ref_clk);
  65. if (ret)
  66. return ret;
  67. udelay(REF_CLK_STABLE_TIME);
  68. reset_control_deassert(priv->por_rst);
  69. udelay(POR_RST_COMPLETE_TIME);
  70. /* Set up phy registers */
  71. hisi_inno_phy_setup(priv);
  72. reset_control_deassert(port->utmi_rst);
  73. udelay(UTMI_RST_COMPLETE_TIME);
  74. return 0;
  75. }
  76. static int hisi_inno_phy_exit(struct phy *phy)
  77. {
  78. struct hisi_inno_phy_port *port = phy_get_drvdata(phy);
  79. struct hisi_inno_phy_priv *priv = port->priv;
  80. reset_control_assert(port->utmi_rst);
  81. reset_control_assert(priv->por_rst);
  82. clk_disable_unprepare(priv->ref_clk);
  83. return 0;
  84. }
  85. static const struct phy_ops hisi_inno_phy_ops = {
  86. .init = hisi_inno_phy_init,
  87. .exit = hisi_inno_phy_exit,
  88. .owner = THIS_MODULE,
  89. };
  90. static int hisi_inno_phy_probe(struct platform_device *pdev)
  91. {
  92. struct device *dev = &pdev->dev;
  93. struct device_node *np = dev->of_node;
  94. struct hisi_inno_phy_priv *priv;
  95. struct phy_provider *provider;
  96. struct device_node *child;
  97. int i = 0;
  98. int ret;
  99. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  100. if (!priv)
  101. return -ENOMEM;
  102. priv->mmio = devm_platform_ioremap_resource(pdev, 0);
  103. if (IS_ERR(priv->mmio)) {
  104. ret = PTR_ERR(priv->mmio);
  105. return ret;
  106. }
  107. priv->ref_clk = devm_clk_get(dev, NULL);
  108. if (IS_ERR(priv->ref_clk))
  109. return PTR_ERR(priv->ref_clk);
  110. priv->por_rst = devm_reset_control_get_exclusive(dev, NULL);
  111. if (IS_ERR(priv->por_rst))
  112. return PTR_ERR(priv->por_rst);
  113. for_each_child_of_node(np, child) {
  114. struct reset_control *rst;
  115. struct phy *phy;
  116. rst = of_reset_control_get_exclusive(child, NULL);
  117. if (IS_ERR(rst)) {
  118. of_node_put(child);
  119. return PTR_ERR(rst);
  120. }
  121. priv->ports[i].utmi_rst = rst;
  122. priv->ports[i].priv = priv;
  123. phy = devm_phy_create(dev, child, &hisi_inno_phy_ops);
  124. if (IS_ERR(phy)) {
  125. of_node_put(child);
  126. return PTR_ERR(phy);
  127. }
  128. phy_set_bus_width(phy, 8);
  129. phy_set_drvdata(phy, &priv->ports[i]);
  130. i++;
  131. if (i >= INNO_PHY_PORT_NUM) {
  132. dev_warn(dev, "Support %d ports in maximum\n", i);
  133. of_node_put(child);
  134. break;
  135. }
  136. }
  137. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  138. return PTR_ERR_OR_ZERO(provider);
  139. }
  140. static const struct of_device_id hisi_inno_phy_of_match[] = {
  141. { .compatible = "hisilicon,inno-usb2-phy", },
  142. { .compatible = "hisilicon,hi3798cv200-usb2-phy", },
  143. { },
  144. };
  145. MODULE_DEVICE_TABLE(of, hisi_inno_phy_of_match);
  146. static struct platform_driver hisi_inno_phy_driver = {
  147. .probe = hisi_inno_phy_probe,
  148. .driver = {
  149. .name = "hisi-inno-phy",
  150. .of_match_table = hisi_inno_phy_of_match,
  151. }
  152. };
  153. module_platform_driver(hisi_inno_phy_driver);
  154. MODULE_DESCRIPTION("HiSilicon INNO USB2 PHY Driver");
  155. MODULE_LICENSE("GPL v2");