mxs-phy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Copyright (C) 2012 Marek Vasut <marex@denx.de>
  4. * on behalf of DENX Software Engineering GmbH
  5. *
  6. * The code contained herein is licensed under the GNU General Public
  7. * License. You may obtain a copy of the GNU General Public License
  8. * Version 2 or later at the following locations:
  9. *
  10. * http://www.opensource.org/licenses/gpl-license.html
  11. * http://www.gnu.org/copyleft/gpl.html
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/clk.h>
  17. #include <linux/usb/otg.h>
  18. #include <linux/stmp_device.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/workqueue.h>
  23. #define DRIVER_NAME "mxs_phy"
  24. #define HW_USBPHY_PWD 0x00
  25. #define HW_USBPHY_CTRL 0x30
  26. #define HW_USBPHY_CTRL_SET 0x34
  27. #define HW_USBPHY_CTRL_CLR 0x38
  28. #define BM_USBPHY_CTRL_SFTRST BIT(31)
  29. #define BM_USBPHY_CTRL_CLKGATE BIT(30)
  30. #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
  31. #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
  32. #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
  33. /*
  34. * Amount of delay in miliseconds to safely enable ENHOSTDISCONDETECT bit
  35. * so that connection and reset processing can be completed for the root hub.
  36. */
  37. #define MXY_PHY_ENHOSTDISCONDETECT_DELAY 250
  38. struct mxs_phy {
  39. struct usb_phy phy;
  40. struct clk *clk;
  41. struct delayed_work enhostdiscondetect_work;
  42. };
  43. #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
  44. static void mxs_phy_hw_init(struct mxs_phy *mxs_phy)
  45. {
  46. void __iomem *base = mxs_phy->phy.io_priv;
  47. stmp_reset_block(base + HW_USBPHY_CTRL);
  48. /* Power up the PHY */
  49. writel_relaxed(0, base + HW_USBPHY_PWD);
  50. /* enable FS/LS device */
  51. writel_relaxed(BM_USBPHY_CTRL_ENUTMILEVEL2 |
  52. BM_USBPHY_CTRL_ENUTMILEVEL3,
  53. base + HW_USBPHY_CTRL_SET);
  54. }
  55. static int mxs_phy_init(struct usb_phy *phy)
  56. {
  57. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  58. clk_prepare_enable(mxs_phy->clk);
  59. mxs_phy_hw_init(mxs_phy);
  60. INIT_DELAYED_WORK(&mxs_phy->enhostdiscondetect_work, NULL);
  61. return 0;
  62. }
  63. static void mxs_phy_shutdown(struct usb_phy *phy)
  64. {
  65. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  66. writel_relaxed(BM_USBPHY_CTRL_CLKGATE,
  67. phy->io_priv + HW_USBPHY_CTRL_SET);
  68. clk_disable_unprepare(mxs_phy->clk);
  69. }
  70. static void mxs_phy_enhostdiscondetect_delay(struct work_struct *ws)
  71. {
  72. struct mxs_phy *mxs_phy = container_of(ws, struct mxs_phy,
  73. enhostdiscondetect_work.work);
  74. /* Enable HOSTDISCONDETECT after delay. */
  75. dev_dbg(mxs_phy->phy.dev, "Setting ENHOSTDISCONDETECT\n");
  76. writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  77. mxs_phy->phy.io_priv + HW_USBPHY_CTRL_SET);
  78. }
  79. static int mxs_phy_on_connect(struct usb_phy *phy, int port)
  80. {
  81. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  82. dev_dbg(phy->dev, "Connect on port %d\n", port);
  83. mxs_phy_hw_init(mxs_phy);
  84. /*
  85. * Delay enabling ENHOSTDISCONDETECT so that connection and
  86. * reset processing can be completed for the root hub.
  87. */
  88. dev_dbg(phy->dev, "Delaying setting ENHOSTDISCONDETECT\n");
  89. PREPARE_DELAYED_WORK(&mxs_phy->enhostdiscondetect_work,
  90. mxs_phy_enhostdiscondetect_delay);
  91. schedule_delayed_work(&mxs_phy->enhostdiscondetect_work,
  92. msecs_to_jiffies(MXY_PHY_ENHOSTDISCONDETECT_DELAY));
  93. return 0;
  94. }
  95. static int mxs_phy_on_disconnect(struct usb_phy *phy, int port)
  96. {
  97. dev_dbg(phy->dev, "Disconnect on port %d\n", port);
  98. /* No need to delay before clearing ENHOSTDISCONDETECT. */
  99. dev_dbg(phy->dev, "Clearing ENHOSTDISCONDETECT\n");
  100. writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  101. phy->io_priv + HW_USBPHY_CTRL_CLR);
  102. return 0;
  103. }
  104. static int mxs_phy_probe(struct platform_device *pdev)
  105. {
  106. struct resource *res;
  107. void __iomem *base;
  108. struct clk *clk;
  109. struct mxs_phy *mxs_phy;
  110. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  111. if (!res) {
  112. dev_err(&pdev->dev, "can't get device resources\n");
  113. return -ENOENT;
  114. }
  115. base = devm_request_and_ioremap(&pdev->dev, res);
  116. if (!base)
  117. return -EBUSY;
  118. clk = devm_clk_get(&pdev->dev, NULL);
  119. if (IS_ERR(clk)) {
  120. dev_err(&pdev->dev,
  121. "can't get the clock, err=%ld", PTR_ERR(clk));
  122. return PTR_ERR(clk);
  123. }
  124. mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
  125. if (!mxs_phy) {
  126. dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
  127. return -ENOMEM;
  128. }
  129. mxs_phy->phy.io_priv = base;
  130. mxs_phy->phy.dev = &pdev->dev;
  131. mxs_phy->phy.label = DRIVER_NAME;
  132. mxs_phy->phy.init = mxs_phy_init;
  133. mxs_phy->phy.shutdown = mxs_phy_shutdown;
  134. mxs_phy->phy.notify_connect = mxs_phy_on_connect;
  135. mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
  136. ATOMIC_INIT_NOTIFIER_HEAD(&mxs_phy->phy.notifier);
  137. mxs_phy->clk = clk;
  138. platform_set_drvdata(pdev, &mxs_phy->phy);
  139. return 0;
  140. }
  141. static int __devexit mxs_phy_remove(struct platform_device *pdev)
  142. {
  143. platform_set_drvdata(pdev, NULL);
  144. return 0;
  145. }
  146. static const struct of_device_id mxs_phy_dt_ids[] = {
  147. { .compatible = "fsl,imx23-usbphy", },
  148. { /* sentinel */ }
  149. };
  150. MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
  151. static struct platform_driver mxs_phy_driver = {
  152. .probe = mxs_phy_probe,
  153. .remove = __devexit_p(mxs_phy_remove),
  154. .driver = {
  155. .name = DRIVER_NAME,
  156. .owner = THIS_MODULE,
  157. .of_match_table = mxs_phy_dt_ids,
  158. },
  159. };
  160. static int __init mxs_phy_module_init(void)
  161. {
  162. return platform_driver_register(&mxs_phy_driver);
  163. }
  164. postcore_initcall(mxs_phy_module_init);
  165. static void __exit mxs_phy_module_exit(void)
  166. {
  167. platform_driver_unregister(&mxs_phy_driver);
  168. }
  169. module_exit(mxs_phy_module_exit);
  170. MODULE_ALIAS("platform:mxs-usb-phy");
  171. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  172. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
  173. MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
  174. MODULE_LICENSE("GPL");