phy-pistachio-usb.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * IMG Pistachio USB PHY driver
  3. *
  4. * Copyright (C) 2015 Google, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <dt-bindings/phy/phy-pistachio-usb.h>
  21. #define USB_PHY_CONTROL1 0x04
  22. #define USB_PHY_CONTROL1_FSEL_SHIFT 2
  23. #define USB_PHY_CONTROL1_FSEL_MASK 0x7
  24. #define USB_PHY_STRAP_CONTROL 0x10
  25. #define USB_PHY_STRAP_CONTROL_REFCLK_SHIFT 4
  26. #define USB_PHY_STRAP_CONTROL_REFCLK_MASK 0x3
  27. #define USB_PHY_STATUS 0x14
  28. #define USB_PHY_STATUS_RX_PHY_CLK BIT(9)
  29. #define USB_PHY_STATUS_RX_UTMI_CLK BIT(8)
  30. #define USB_PHY_STATUS_VBUS_FAULT BIT(7)
  31. struct pistachio_usb_phy {
  32. struct device *dev;
  33. struct regmap *cr_top;
  34. struct clk *phy_clk;
  35. unsigned int refclk;
  36. };
  37. static const unsigned long fsel_rate_map[] = {
  38. 9600000,
  39. 10000000,
  40. 12000000,
  41. 19200000,
  42. 20000000,
  43. 24000000,
  44. 0,
  45. 50000000,
  46. };
  47. static int pistachio_usb_phy_power_on(struct phy *phy)
  48. {
  49. struct pistachio_usb_phy *p_phy = phy_get_drvdata(phy);
  50. unsigned long timeout, rate;
  51. unsigned int i;
  52. int ret;
  53. ret = clk_prepare_enable(p_phy->phy_clk);
  54. if (ret < 0) {
  55. dev_err(p_phy->dev, "Failed to enable PHY clock: %d\n", ret);
  56. return ret;
  57. }
  58. regmap_update_bits(p_phy->cr_top, USB_PHY_STRAP_CONTROL,
  59. USB_PHY_STRAP_CONTROL_REFCLK_MASK <<
  60. USB_PHY_STRAP_CONTROL_REFCLK_SHIFT,
  61. p_phy->refclk << USB_PHY_STRAP_CONTROL_REFCLK_SHIFT);
  62. rate = clk_get_rate(p_phy->phy_clk);
  63. if (p_phy->refclk == REFCLK_XO_CRYSTAL && rate != 12000000) {
  64. dev_err(p_phy->dev, "Unsupported rate for XO crystal: %ld\n",
  65. rate);
  66. ret = -EINVAL;
  67. goto disable_clk;
  68. }
  69. for (i = 0; i < ARRAY_SIZE(fsel_rate_map); i++) {
  70. if (rate == fsel_rate_map[i])
  71. break;
  72. }
  73. if (i == ARRAY_SIZE(fsel_rate_map)) {
  74. dev_err(p_phy->dev, "Unsupported clock rate: %lu\n", rate);
  75. ret = -EINVAL;
  76. goto disable_clk;
  77. }
  78. regmap_update_bits(p_phy->cr_top, USB_PHY_CONTROL1,
  79. USB_PHY_CONTROL1_FSEL_MASK <<
  80. USB_PHY_CONTROL1_FSEL_SHIFT,
  81. i << USB_PHY_CONTROL1_FSEL_SHIFT);
  82. timeout = jiffies + msecs_to_jiffies(200);
  83. while (time_before(jiffies, timeout)) {
  84. unsigned int val;
  85. regmap_read(p_phy->cr_top, USB_PHY_STATUS, &val);
  86. if (val & USB_PHY_STATUS_VBUS_FAULT) {
  87. dev_err(p_phy->dev, "VBUS fault detected\n");
  88. ret = -EIO;
  89. goto disable_clk;
  90. }
  91. if ((val & USB_PHY_STATUS_RX_PHY_CLK) &&
  92. (val & USB_PHY_STATUS_RX_UTMI_CLK))
  93. return 0;
  94. usleep_range(1000, 1500);
  95. }
  96. dev_err(p_phy->dev, "Timed out waiting for PHY to power on\n");
  97. ret = -ETIMEDOUT;
  98. disable_clk:
  99. clk_disable_unprepare(p_phy->phy_clk);
  100. return ret;
  101. }
  102. static int pistachio_usb_phy_power_off(struct phy *phy)
  103. {
  104. struct pistachio_usb_phy *p_phy = phy_get_drvdata(phy);
  105. clk_disable_unprepare(p_phy->phy_clk);
  106. return 0;
  107. }
  108. static const struct phy_ops pistachio_usb_phy_ops = {
  109. .power_on = pistachio_usb_phy_power_on,
  110. .power_off = pistachio_usb_phy_power_off,
  111. .owner = THIS_MODULE,
  112. };
  113. static int pistachio_usb_phy_probe(struct platform_device *pdev)
  114. {
  115. struct pistachio_usb_phy *p_phy;
  116. struct phy_provider *provider;
  117. struct phy *phy;
  118. int ret;
  119. p_phy = devm_kzalloc(&pdev->dev, sizeof(*p_phy), GFP_KERNEL);
  120. if (!p_phy)
  121. return -ENOMEM;
  122. p_phy->dev = &pdev->dev;
  123. platform_set_drvdata(pdev, p_phy);
  124. p_phy->cr_top = syscon_regmap_lookup_by_phandle(p_phy->dev->of_node,
  125. "img,cr-top");
  126. if (IS_ERR(p_phy->cr_top)) {
  127. dev_err(p_phy->dev, "Failed to get CR_TOP registers: %ld\n",
  128. PTR_ERR(p_phy->cr_top));
  129. return PTR_ERR(p_phy->cr_top);
  130. }
  131. p_phy->phy_clk = devm_clk_get(p_phy->dev, "usb_phy");
  132. if (IS_ERR(p_phy->phy_clk)) {
  133. dev_err(p_phy->dev, "Failed to get usb_phy clock: %ld\n",
  134. PTR_ERR(p_phy->phy_clk));
  135. return PTR_ERR(p_phy->phy_clk);
  136. }
  137. ret = of_property_read_u32(p_phy->dev->of_node, "img,refclk",
  138. &p_phy->refclk);
  139. if (ret < 0) {
  140. dev_err(p_phy->dev, "No reference clock selector specified\n");
  141. return ret;
  142. }
  143. phy = devm_phy_create(p_phy->dev, NULL, &pistachio_usb_phy_ops);
  144. if (IS_ERR(phy)) {
  145. dev_err(p_phy->dev, "Failed to create PHY: %ld\n",
  146. PTR_ERR(phy));
  147. return PTR_ERR(phy);
  148. }
  149. phy_set_drvdata(phy, p_phy);
  150. provider = devm_of_phy_provider_register(p_phy->dev,
  151. of_phy_simple_xlate);
  152. if (IS_ERR(provider)) {
  153. dev_err(p_phy->dev, "Failed to register PHY provider: %ld\n",
  154. PTR_ERR(provider));
  155. return PTR_ERR(provider);
  156. }
  157. return 0;
  158. }
  159. static const struct of_device_id pistachio_usb_phy_of_match[] = {
  160. { .compatible = "img,pistachio-usb-phy", },
  161. { },
  162. };
  163. MODULE_DEVICE_TABLE(of, pistachio_usb_phy_of_match);
  164. static struct platform_driver pistachio_usb_phy_driver = {
  165. .probe = pistachio_usb_phy_probe,
  166. .driver = {
  167. .name = "pistachio-usb-phy",
  168. .of_match_table = pistachio_usb_phy_of_match,
  169. },
  170. };
  171. module_platform_driver(pistachio_usb_phy_driver);
  172. MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
  173. MODULE_DESCRIPTION("IMG Pistachio USB2.0 PHY driver");
  174. MODULE_LICENSE("GPL v2");