mxs-phy.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #define DRIVER_NAME "mxs_phy"
  23. #define HW_USBPHY_PWD 0x00
  24. #define HW_USBPHY_CTRL 0x30
  25. #define HW_USBPHY_CTRL_SET 0x34
  26. #define HW_USBPHY_CTRL_CLR 0x38
  27. #define BM_USBPHY_CTRL_SFTRST BIT(31)
  28. #define BM_USBPHY_CTRL_CLKGATE BIT(30)
  29. #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
  30. #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
  31. #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
  32. struct mxs_phy {
  33. struct usb_phy phy;
  34. struct clk *clk;
  35. };
  36. #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
  37. static void mxs_phy_hw_init(struct mxs_phy *mxs_phy)
  38. {
  39. void __iomem *base = mxs_phy->phy.io_priv;
  40. stmp_reset_block(base + HW_USBPHY_CTRL);
  41. /* Power up the PHY */
  42. writel_relaxed(0, base + HW_USBPHY_PWD);
  43. /* enable FS/LS device */
  44. writel_relaxed(BM_USBPHY_CTRL_ENUTMILEVEL2 |
  45. BM_USBPHY_CTRL_ENUTMILEVEL3,
  46. base + HW_USBPHY_CTRL_SET);
  47. }
  48. static int mxs_phy_init(struct usb_phy *phy)
  49. {
  50. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  51. clk_prepare_enable(mxs_phy->clk);
  52. mxs_phy_hw_init(mxs_phy);
  53. return 0;
  54. }
  55. static void mxs_phy_shutdown(struct usb_phy *phy)
  56. {
  57. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  58. writel_relaxed(BM_USBPHY_CTRL_CLKGATE,
  59. phy->io_priv + HW_USBPHY_CTRL_SET);
  60. clk_disable_unprepare(mxs_phy->clk);
  61. }
  62. static int mxs_phy_on_connect(struct usb_phy *phy,
  63. enum usb_device_speed speed)
  64. {
  65. dev_dbg(phy->dev, "%s speed device has connected\n",
  66. (speed == USB_SPEED_HIGH) ? "high" : "non-high");
  67. if (speed == USB_SPEED_HIGH)
  68. writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  69. phy->io_priv + HW_USBPHY_CTRL_SET);
  70. return 0;
  71. }
  72. static int mxs_phy_on_disconnect(struct usb_phy *phy,
  73. enum usb_device_speed speed)
  74. {
  75. dev_dbg(phy->dev, "%s speed device has disconnected\n",
  76. (speed == USB_SPEED_HIGH) ? "high" : "non-high");
  77. if (speed == USB_SPEED_HIGH)
  78. writel_relaxed(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  79. phy->io_priv + HW_USBPHY_CTRL_CLR);
  80. return 0;
  81. }
  82. static int mxs_phy_probe(struct platform_device *pdev)
  83. {
  84. struct resource *res;
  85. void __iomem *base;
  86. struct clk *clk;
  87. struct mxs_phy *mxs_phy;
  88. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  89. if (!res) {
  90. dev_err(&pdev->dev, "can't get device resources\n");
  91. return -ENOENT;
  92. }
  93. base = devm_request_and_ioremap(&pdev->dev, res);
  94. if (!base)
  95. return -EBUSY;
  96. clk = devm_clk_get(&pdev->dev, NULL);
  97. if (IS_ERR(clk)) {
  98. dev_err(&pdev->dev,
  99. "can't get the clock, err=%ld", PTR_ERR(clk));
  100. return PTR_ERR(clk);
  101. }
  102. mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
  103. if (!mxs_phy) {
  104. dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
  105. return -ENOMEM;
  106. }
  107. mxs_phy->phy.io_priv = base;
  108. mxs_phy->phy.dev = &pdev->dev;
  109. mxs_phy->phy.label = DRIVER_NAME;
  110. mxs_phy->phy.init = mxs_phy_init;
  111. mxs_phy->phy.shutdown = mxs_phy_shutdown;
  112. mxs_phy->phy.notify_connect = mxs_phy_on_connect;
  113. mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
  114. ATOMIC_INIT_NOTIFIER_HEAD(&mxs_phy->phy.notifier);
  115. mxs_phy->clk = clk;
  116. platform_set_drvdata(pdev, &mxs_phy->phy);
  117. return 0;
  118. }
  119. static int mxs_phy_remove(struct platform_device *pdev)
  120. {
  121. platform_set_drvdata(pdev, NULL);
  122. return 0;
  123. }
  124. static const struct of_device_id mxs_phy_dt_ids[] = {
  125. { .compatible = "fsl,imx23-usbphy", },
  126. { /* sentinel */ }
  127. };
  128. MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
  129. static struct platform_driver mxs_phy_driver = {
  130. .probe = mxs_phy_probe,
  131. .remove = mxs_phy_remove,
  132. .driver = {
  133. .name = DRIVER_NAME,
  134. .owner = THIS_MODULE,
  135. .of_match_table = mxs_phy_dt_ids,
  136. },
  137. };
  138. static int __init mxs_phy_module_init(void)
  139. {
  140. return platform_driver_register(&mxs_phy_driver);
  141. }
  142. postcore_initcall(mxs_phy_module_init);
  143. static void __exit mxs_phy_module_exit(void)
  144. {
  145. platform_driver_unregister(&mxs_phy_driver);
  146. }
  147. module_exit(mxs_phy_module_exit);
  148. MODULE_ALIAS("platform:mxs-usb-phy");
  149. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  150. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
  151. MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
  152. MODULE_LICENSE("GPL");