phy-hisi-inno-usb2.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * HiSilicon INNO USB2 PHY Driver.
  3. *
  4. * Copyright (c) 2016-2017 HiSilicon Technologies Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/phy/phy.h>
  25. #include <linux/reset.h>
  26. #define INNO_PHY_PORT_NUM 2
  27. #define REF_CLK_STABLE_TIME 100 /* unit:us */
  28. #define UTMI_CLK_STABLE_TIME 200 /* unit:us */
  29. #define TEST_CLK_STABLE_TIME 2 /* unit:ms */
  30. #define PHY_CLK_STABLE_TIME 2 /* unit:ms */
  31. #define UTMI_RST_COMPLETE_TIME 2 /* unit:ms */
  32. #define POR_RST_COMPLETE_TIME 300 /* unit:us */
  33. #define PHY_TEST_DATA GENMASK(7, 0)
  34. #define PHY_TEST_ADDR GENMASK(15, 8)
  35. #define PHY_TEST_PORT GENMASK(18, 16)
  36. #define PHY_TEST_WREN BIT(21)
  37. #define PHY_TEST_CLK BIT(22) /* rising edge active */
  38. #define PHY_TEST_RST BIT(23) /* low active */
  39. #define PHY_CLK_ENABLE BIT(2)
  40. struct hisi_inno_phy_port {
  41. struct reset_control *utmi_rst;
  42. struct hisi_inno_phy_priv *priv;
  43. };
  44. struct hisi_inno_phy_priv {
  45. void __iomem *mmio;
  46. struct clk *ref_clk;
  47. struct reset_control *por_rst;
  48. struct hisi_inno_phy_port ports[INNO_PHY_PORT_NUM];
  49. };
  50. static void hisi_inno_phy_write_reg(struct hisi_inno_phy_priv *priv,
  51. u8 port, u32 addr, u32 data)
  52. {
  53. void __iomem *reg = priv->mmio;
  54. u32 val;
  55. val = (data & PHY_TEST_DATA) |
  56. ((addr << 8) & PHY_TEST_ADDR) |
  57. ((port << 16) & PHY_TEST_PORT) |
  58. PHY_TEST_WREN | PHY_TEST_RST;
  59. writel(val, reg);
  60. val |= PHY_TEST_CLK;
  61. writel(val, reg);
  62. val &= ~PHY_TEST_CLK;
  63. writel(val, reg);
  64. }
  65. static void hisi_inno_phy_setup(struct hisi_inno_phy_priv *priv)
  66. {
  67. /* The phy clk is controlled by the port0 register 0x06. */
  68. hisi_inno_phy_write_reg(priv, 0, 0x06, PHY_CLK_ENABLE);
  69. msleep(PHY_CLK_STABLE_TIME);
  70. }
  71. static int hisi_inno_phy_init(struct phy *phy)
  72. {
  73. struct hisi_inno_phy_port *port = phy_get_drvdata(phy);
  74. struct hisi_inno_phy_priv *priv = port->priv;
  75. int ret;
  76. ret = clk_prepare_enable(priv->ref_clk);
  77. if (ret)
  78. return ret;
  79. udelay(REF_CLK_STABLE_TIME);
  80. reset_control_deassert(priv->por_rst);
  81. udelay(POR_RST_COMPLETE_TIME);
  82. /* Set up phy registers */
  83. hisi_inno_phy_setup(priv);
  84. reset_control_deassert(port->utmi_rst);
  85. udelay(UTMI_RST_COMPLETE_TIME);
  86. return 0;
  87. }
  88. static int hisi_inno_phy_exit(struct phy *phy)
  89. {
  90. struct hisi_inno_phy_port *port = phy_get_drvdata(phy);
  91. struct hisi_inno_phy_priv *priv = port->priv;
  92. reset_control_assert(port->utmi_rst);
  93. reset_control_assert(priv->por_rst);
  94. clk_disable_unprepare(priv->ref_clk);
  95. return 0;
  96. }
  97. static const struct phy_ops hisi_inno_phy_ops = {
  98. .init = hisi_inno_phy_init,
  99. .exit = hisi_inno_phy_exit,
  100. .owner = THIS_MODULE,
  101. };
  102. static int hisi_inno_phy_probe(struct platform_device *pdev)
  103. {
  104. struct device *dev = &pdev->dev;
  105. struct device_node *np = dev->of_node;
  106. struct hisi_inno_phy_priv *priv;
  107. struct phy_provider *provider;
  108. struct device_node *child;
  109. struct resource *res;
  110. int i = 0;
  111. int ret;
  112. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  113. if (!priv)
  114. return -ENOMEM;
  115. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  116. priv->mmio = devm_ioremap_resource(dev, res);
  117. if (IS_ERR(priv->mmio)) {
  118. ret = PTR_ERR(priv->mmio);
  119. return ret;
  120. }
  121. priv->ref_clk = devm_clk_get(dev, NULL);
  122. if (IS_ERR(priv->ref_clk))
  123. return PTR_ERR(priv->ref_clk);
  124. priv->por_rst = devm_reset_control_get_exclusive(dev, NULL);
  125. if (IS_ERR(priv->por_rst))
  126. return PTR_ERR(priv->por_rst);
  127. for_each_child_of_node(np, child) {
  128. struct reset_control *rst;
  129. struct phy *phy;
  130. rst = of_reset_control_get_exclusive(child, NULL);
  131. if (IS_ERR(rst))
  132. return PTR_ERR(rst);
  133. priv->ports[i].utmi_rst = rst;
  134. priv->ports[i].priv = priv;
  135. phy = devm_phy_create(dev, child, &hisi_inno_phy_ops);
  136. if (IS_ERR(phy))
  137. return PTR_ERR(phy);
  138. phy_set_bus_width(phy, 8);
  139. phy_set_drvdata(phy, &priv->ports[i]);
  140. i++;
  141. if (i > INNO_PHY_PORT_NUM) {
  142. dev_warn(dev, "Support %d ports in maximum\n", i);
  143. break;
  144. }
  145. }
  146. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  147. return PTR_ERR_OR_ZERO(provider);
  148. }
  149. static const struct of_device_id hisi_inno_phy_of_match[] = {
  150. { .compatible = "hisilicon,inno-usb2-phy", },
  151. { .compatible = "hisilicon,hi3798cv200-usb2-phy", },
  152. { },
  153. };
  154. MODULE_DEVICE_TABLE(of, hisi_inno_phy_of_match);
  155. static struct platform_driver hisi_inno_phy_driver = {
  156. .probe = hisi_inno_phy_probe,
  157. .driver = {
  158. .name = "hisi-inno-phy",
  159. .of_match_table = hisi_inno_phy_of_match,
  160. }
  161. };
  162. module_platform_driver(hisi_inno_phy_driver);
  163. MODULE_DESCRIPTION("HiSilicon INNO USB2 PHY Driver");
  164. MODULE_LICENSE("GPL v2");