ehci-exynos.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * SAMSUNG EXYNOS USB HOST EHCI Controller
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * Author: Jingoo Han <jg1.han@samsung.com>
  6. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/usb/phy.h>
  23. #include <linux/usb/samsung_usb_phy.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb/hcd.h>
  26. #include <linux/usb/otg.h>
  27. #include "ehci.h"
  28. #define DRIVER_DESC "EHCI EXYNOS driver"
  29. #define EHCI_INSNREG00(base) (base + 0x90)
  30. #define EHCI_INSNREG00_ENA_INCR16 (0x1 << 25)
  31. #define EHCI_INSNREG00_ENA_INCR8 (0x1 << 24)
  32. #define EHCI_INSNREG00_ENA_INCR4 (0x1 << 23)
  33. #define EHCI_INSNREG00_ENA_INCRX_ALIGN (0x1 << 22)
  34. #define EHCI_INSNREG00_ENABLE_DMA_BURST \
  35. (EHCI_INSNREG00_ENA_INCR16 | EHCI_INSNREG00_ENA_INCR8 | \
  36. EHCI_INSNREG00_ENA_INCR4 | EHCI_INSNREG00_ENA_INCRX_ALIGN)
  37. static const char hcd_name[] = "ehci-exynos";
  38. static struct hc_driver __read_mostly exynos_ehci_hc_driver;
  39. struct exynos_ehci_hcd {
  40. struct clk *clk;
  41. struct usb_phy *phy;
  42. struct usb_otg *otg;
  43. };
  44. #define to_exynos_ehci(hcd) (struct exynos_ehci_hcd *)(hcd_to_ehci(hcd)->priv)
  45. static void exynos_setup_vbus_gpio(struct platform_device *pdev)
  46. {
  47. struct device *dev = &pdev->dev;
  48. int err;
  49. int gpio;
  50. if (!dev->of_node)
  51. return;
  52. gpio = of_get_named_gpio(dev->of_node, "samsung,vbus-gpio", 0);
  53. if (!gpio_is_valid(gpio))
  54. return;
  55. err = devm_gpio_request_one(dev, gpio, GPIOF_OUT_INIT_HIGH,
  56. "ehci_vbus_gpio");
  57. if (err)
  58. dev_err(dev, "can't request ehci vbus gpio %d", gpio);
  59. }
  60. static int exynos_ehci_probe(struct platform_device *pdev)
  61. {
  62. struct exynos_ehci_hcd *exynos_ehci;
  63. struct usb_hcd *hcd;
  64. struct ehci_hcd *ehci;
  65. struct resource *res;
  66. struct usb_phy *phy;
  67. int irq;
  68. int err;
  69. /*
  70. * Right now device-tree probed devices don't get dma_mask set.
  71. * Since shared usb code relies on it, set it here for now.
  72. * Once we move to full device tree support this will vanish off.
  73. */
  74. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  75. if (err)
  76. return err;
  77. exynos_setup_vbus_gpio(pdev);
  78. hcd = usb_create_hcd(&exynos_ehci_hc_driver,
  79. &pdev->dev, dev_name(&pdev->dev));
  80. if (!hcd) {
  81. dev_err(&pdev->dev, "Unable to create HCD\n");
  82. return -ENOMEM;
  83. }
  84. exynos_ehci = to_exynos_ehci(hcd);
  85. if (of_device_is_compatible(pdev->dev.of_node,
  86. "samsung,exynos5440-ehci"))
  87. goto skip_phy;
  88. phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  89. if (IS_ERR(phy)) {
  90. usb_put_hcd(hcd);
  91. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  92. return -EPROBE_DEFER;
  93. } else {
  94. exynos_ehci->phy = phy;
  95. exynos_ehci->otg = phy->otg;
  96. }
  97. skip_phy:
  98. exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
  99. if (IS_ERR(exynos_ehci->clk)) {
  100. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  101. err = PTR_ERR(exynos_ehci->clk);
  102. goto fail_clk;
  103. }
  104. err = clk_prepare_enable(exynos_ehci->clk);
  105. if (err)
  106. goto fail_clk;
  107. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. if (!res) {
  109. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  110. err = -ENXIO;
  111. goto fail_io;
  112. }
  113. hcd->rsrc_start = res->start;
  114. hcd->rsrc_len = resource_size(res);
  115. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  116. if (!hcd->regs) {
  117. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  118. err = -ENOMEM;
  119. goto fail_io;
  120. }
  121. irq = platform_get_irq(pdev, 0);
  122. if (!irq) {
  123. dev_err(&pdev->dev, "Failed to get IRQ\n");
  124. err = -ENODEV;
  125. goto fail_io;
  126. }
  127. if (exynos_ehci->otg)
  128. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  129. if (exynos_ehci->phy)
  130. usb_phy_init(exynos_ehci->phy);
  131. ehci = hcd_to_ehci(hcd);
  132. ehci->caps = hcd->regs;
  133. /* DMA burst Enable */
  134. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  135. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  136. if (err) {
  137. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  138. goto fail_add_hcd;
  139. }
  140. platform_set_drvdata(pdev, hcd);
  141. return 0;
  142. fail_add_hcd:
  143. if (exynos_ehci->phy)
  144. usb_phy_shutdown(exynos_ehci->phy);
  145. fail_io:
  146. clk_disable_unprepare(exynos_ehci->clk);
  147. fail_clk:
  148. usb_put_hcd(hcd);
  149. return err;
  150. }
  151. static int exynos_ehci_remove(struct platform_device *pdev)
  152. {
  153. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  154. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  155. usb_remove_hcd(hcd);
  156. if (exynos_ehci->otg)
  157. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  158. if (exynos_ehci->phy)
  159. usb_phy_shutdown(exynos_ehci->phy);
  160. clk_disable_unprepare(exynos_ehci->clk);
  161. usb_put_hcd(hcd);
  162. return 0;
  163. }
  164. #ifdef CONFIG_PM
  165. static int exynos_ehci_suspend(struct device *dev)
  166. {
  167. struct usb_hcd *hcd = dev_get_drvdata(dev);
  168. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  169. bool do_wakeup = device_may_wakeup(dev);
  170. int rc;
  171. rc = ehci_suspend(hcd, do_wakeup);
  172. if (exynos_ehci->otg)
  173. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  174. if (exynos_ehci->phy)
  175. usb_phy_shutdown(exynos_ehci->phy);
  176. clk_disable_unprepare(exynos_ehci->clk);
  177. return rc;
  178. }
  179. static int exynos_ehci_resume(struct device *dev)
  180. {
  181. struct usb_hcd *hcd = dev_get_drvdata(dev);
  182. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  183. clk_prepare_enable(exynos_ehci->clk);
  184. if (exynos_ehci->otg)
  185. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  186. if (exynos_ehci->phy)
  187. usb_phy_init(exynos_ehci->phy);
  188. /* DMA burst Enable */
  189. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  190. ehci_resume(hcd, false);
  191. return 0;
  192. }
  193. #else
  194. #define exynos_ehci_suspend NULL
  195. #define exynos_ehci_resume NULL
  196. #endif
  197. static const struct dev_pm_ops exynos_ehci_pm_ops = {
  198. .suspend = exynos_ehci_suspend,
  199. .resume = exynos_ehci_resume,
  200. };
  201. #ifdef CONFIG_OF
  202. static const struct of_device_id exynos_ehci_match[] = {
  203. { .compatible = "samsung,exynos4210-ehci" },
  204. { .compatible = "samsung,exynos5440-ehci" },
  205. {},
  206. };
  207. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  208. #endif
  209. static struct platform_driver exynos_ehci_driver = {
  210. .probe = exynos_ehci_probe,
  211. .remove = exynos_ehci_remove,
  212. .shutdown = usb_hcd_platform_shutdown,
  213. .driver = {
  214. .name = "exynos-ehci",
  215. .owner = THIS_MODULE,
  216. .pm = &exynos_ehci_pm_ops,
  217. .of_match_table = of_match_ptr(exynos_ehci_match),
  218. }
  219. };
  220. static const struct ehci_driver_overrides exynos_overrides __initdata = {
  221. .extra_priv_size = sizeof(struct exynos_ehci_hcd),
  222. };
  223. static int __init ehci_exynos_init(void)
  224. {
  225. if (usb_disabled())
  226. return -ENODEV;
  227. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  228. ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
  229. return platform_driver_register(&exynos_ehci_driver);
  230. }
  231. module_init(ehci_exynos_init);
  232. static void __exit ehci_exynos_cleanup(void)
  233. {
  234. platform_driver_unregister(&exynos_ehci_driver);
  235. }
  236. module_exit(ehci_exynos_cleanup);
  237. MODULE_DESCRIPTION(DRIVER_DESC);
  238. MODULE_ALIAS("platform:exynos-ehci");
  239. MODULE_AUTHOR("Jingoo Han");
  240. MODULE_AUTHOR("Joonyoung Shim");
  241. MODULE_LICENSE("GPL v2");