phy-sun9i-usb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Allwinner sun9i USB phy driver
  3. *
  4. * Copyright (C) 2014-2015 Chen-Yu Tsai <wens@csie.org>
  5. *
  6. * Based on phy-sun4i-usb.c from
  7. * Hans de Goede <hdegoede@redhat.com>
  8. *
  9. * and code from
  10. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/phy/phy.h>
  27. #include <linux/usb/of.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/reset.h>
  30. #define SUNXI_AHB_INCR16_BURST_EN BIT(11)
  31. #define SUNXI_AHB_INCR8_BURST_EN BIT(10)
  32. #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
  33. #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
  34. #define SUNXI_ULPI_BYPASS_EN BIT(0)
  35. /* usb1 HSIC specific bits */
  36. #define SUNXI_EHCI_HS_FORCE BIT(20)
  37. #define SUNXI_HSIC_CONNECT_DET BIT(17)
  38. #define SUNXI_HSIC_CONNECT_INT BIT(16)
  39. #define SUNXI_HSIC BIT(1)
  40. struct sun9i_usb_phy {
  41. struct phy *phy;
  42. void __iomem *pmu;
  43. struct reset_control *reset;
  44. struct clk *clk;
  45. struct clk *hsic_clk;
  46. enum usb_phy_interface type;
  47. };
  48. static void sun9i_usb_phy_passby(struct sun9i_usb_phy *phy, int enable)
  49. {
  50. u32 bits, reg_value;
  51. bits = SUNXI_AHB_INCR16_BURST_EN | SUNXI_AHB_INCR8_BURST_EN |
  52. SUNXI_AHB_INCR4_BURST_EN | SUNXI_AHB_INCRX_ALIGN_EN |
  53. SUNXI_ULPI_BYPASS_EN;
  54. if (phy->type == USBPHY_INTERFACE_MODE_HSIC)
  55. bits |= SUNXI_HSIC | SUNXI_EHCI_HS_FORCE |
  56. SUNXI_HSIC_CONNECT_DET | SUNXI_HSIC_CONNECT_INT;
  57. reg_value = readl(phy->pmu);
  58. if (enable)
  59. reg_value |= bits;
  60. else
  61. reg_value &= ~bits;
  62. writel(reg_value, phy->pmu);
  63. }
  64. static int sun9i_usb_phy_init(struct phy *_phy)
  65. {
  66. struct sun9i_usb_phy *phy = phy_get_drvdata(_phy);
  67. int ret;
  68. ret = clk_prepare_enable(phy->clk);
  69. if (ret)
  70. goto err_clk;
  71. ret = clk_prepare_enable(phy->hsic_clk);
  72. if (ret)
  73. goto err_hsic_clk;
  74. ret = reset_control_deassert(phy->reset);
  75. if (ret)
  76. goto err_reset;
  77. sun9i_usb_phy_passby(phy, 1);
  78. return 0;
  79. err_reset:
  80. clk_disable_unprepare(phy->hsic_clk);
  81. err_hsic_clk:
  82. clk_disable_unprepare(phy->clk);
  83. err_clk:
  84. return ret;
  85. }
  86. static int sun9i_usb_phy_exit(struct phy *_phy)
  87. {
  88. struct sun9i_usb_phy *phy = phy_get_drvdata(_phy);
  89. sun9i_usb_phy_passby(phy, 0);
  90. reset_control_assert(phy->reset);
  91. clk_disable_unprepare(phy->hsic_clk);
  92. clk_disable_unprepare(phy->clk);
  93. return 0;
  94. }
  95. static const struct phy_ops sun9i_usb_phy_ops = {
  96. .init = sun9i_usb_phy_init,
  97. .exit = sun9i_usb_phy_exit,
  98. .owner = THIS_MODULE,
  99. };
  100. static int sun9i_usb_phy_probe(struct platform_device *pdev)
  101. {
  102. struct sun9i_usb_phy *phy;
  103. struct device *dev = &pdev->dev;
  104. struct device_node *np = dev->of_node;
  105. struct phy_provider *phy_provider;
  106. struct resource *res;
  107. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  108. if (!phy)
  109. return -ENOMEM;
  110. phy->type = of_usb_get_phy_mode(np);
  111. if (phy->type == USBPHY_INTERFACE_MODE_HSIC) {
  112. phy->clk = devm_clk_get(dev, "hsic_480M");
  113. if (IS_ERR(phy->clk)) {
  114. dev_err(dev, "failed to get hsic_480M clock\n");
  115. return PTR_ERR(phy->clk);
  116. }
  117. phy->hsic_clk = devm_clk_get(dev, "hsic_12M");
  118. if (IS_ERR(phy->clk)) {
  119. dev_err(dev, "failed to get hsic_12M clock\n");
  120. return PTR_ERR(phy->clk);
  121. }
  122. phy->reset = devm_reset_control_get(dev, "hsic");
  123. if (IS_ERR(phy->reset)) {
  124. dev_err(dev, "failed to get reset control\n");
  125. return PTR_ERR(phy->reset);
  126. }
  127. } else {
  128. phy->clk = devm_clk_get(dev, "phy");
  129. if (IS_ERR(phy->clk)) {
  130. dev_err(dev, "failed to get phy clock\n");
  131. return PTR_ERR(phy->clk);
  132. }
  133. phy->reset = devm_reset_control_get(dev, "phy");
  134. if (IS_ERR(phy->reset)) {
  135. dev_err(dev, "failed to get reset control\n");
  136. return PTR_ERR(phy->reset);
  137. }
  138. }
  139. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. phy->pmu = devm_ioremap_resource(dev, res);
  141. if (IS_ERR(phy->pmu))
  142. return PTR_ERR(phy->pmu);
  143. phy->phy = devm_phy_create(dev, NULL, &sun9i_usb_phy_ops);
  144. if (IS_ERR(phy->phy)) {
  145. dev_err(dev, "failed to create PHY\n");
  146. return PTR_ERR(phy->phy);
  147. }
  148. phy_set_drvdata(phy->phy, phy);
  149. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  150. return PTR_ERR_OR_ZERO(phy_provider);
  151. }
  152. static const struct of_device_id sun9i_usb_phy_of_match[] = {
  153. { .compatible = "allwinner,sun9i-a80-usb-phy" },
  154. { },
  155. };
  156. MODULE_DEVICE_TABLE(of, sun9i_usb_phy_of_match);
  157. static struct platform_driver sun9i_usb_phy_driver = {
  158. .probe = sun9i_usb_phy_probe,
  159. .driver = {
  160. .of_match_table = sun9i_usb_phy_of_match,
  161. .name = "sun9i-usb-phy",
  162. }
  163. };
  164. module_platform_driver(sun9i_usb_phy_driver);
  165. MODULE_DESCRIPTION("Allwinner sun9i USB phy driver");
  166. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  167. MODULE_LICENSE("GPL");