ehci-exynos.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (!pdev->dev.dma_mask)
  75. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  76. if (!pdev->dev.coherent_dma_mask)
  77. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  78. exynos_setup_vbus_gpio(pdev);
  79. hcd = usb_create_hcd(&exynos_ehci_hc_driver,
  80. &pdev->dev, dev_name(&pdev->dev));
  81. if (!hcd) {
  82. dev_err(&pdev->dev, "Unable to create HCD\n");
  83. return -ENOMEM;
  84. }
  85. exynos_ehci = to_exynos_ehci(hcd);
  86. if (of_device_is_compatible(pdev->dev.of_node,
  87. "samsung,exynos5440-ehci"))
  88. goto skip_phy;
  89. phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  90. if (IS_ERR(phy)) {
  91. usb_put_hcd(hcd);
  92. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  93. return -EPROBE_DEFER;
  94. } else {
  95. exynos_ehci->phy = phy;
  96. exynos_ehci->otg = phy->otg;
  97. }
  98. skip_phy:
  99. exynos_ehci->clk = devm_clk_get(&pdev->dev, "usbhost");
  100. if (IS_ERR(exynos_ehci->clk)) {
  101. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  102. err = PTR_ERR(exynos_ehci->clk);
  103. goto fail_clk;
  104. }
  105. err = clk_prepare_enable(exynos_ehci->clk);
  106. if (err)
  107. goto fail_clk;
  108. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. if (!res) {
  110. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  111. err = -ENXIO;
  112. goto fail_io;
  113. }
  114. hcd->rsrc_start = res->start;
  115. hcd->rsrc_len = resource_size(res);
  116. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  117. if (!hcd->regs) {
  118. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  119. err = -ENOMEM;
  120. goto fail_io;
  121. }
  122. irq = platform_get_irq(pdev, 0);
  123. if (!irq) {
  124. dev_err(&pdev->dev, "Failed to get IRQ\n");
  125. err = -ENODEV;
  126. goto fail_io;
  127. }
  128. if (exynos_ehci->otg)
  129. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  130. if (exynos_ehci->phy)
  131. usb_phy_init(exynos_ehci->phy);
  132. ehci = hcd_to_ehci(hcd);
  133. ehci->caps = hcd->regs;
  134. /* DMA burst Enable */
  135. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  136. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  137. if (err) {
  138. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  139. goto fail_add_hcd;
  140. }
  141. platform_set_drvdata(pdev, hcd);
  142. return 0;
  143. fail_add_hcd:
  144. if (exynos_ehci->phy)
  145. usb_phy_shutdown(exynos_ehci->phy);
  146. fail_io:
  147. clk_disable_unprepare(exynos_ehci->clk);
  148. fail_clk:
  149. usb_put_hcd(hcd);
  150. return err;
  151. }
  152. static int exynos_ehci_remove(struct platform_device *pdev)
  153. {
  154. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  155. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  156. usb_remove_hcd(hcd);
  157. if (exynos_ehci->otg)
  158. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  159. if (exynos_ehci->phy)
  160. usb_phy_shutdown(exynos_ehci->phy);
  161. clk_disable_unprepare(exynos_ehci->clk);
  162. usb_put_hcd(hcd);
  163. return 0;
  164. }
  165. #ifdef CONFIG_PM
  166. static int exynos_ehci_suspend(struct device *dev)
  167. {
  168. struct usb_hcd *hcd = dev_get_drvdata(dev);
  169. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  170. bool do_wakeup = device_may_wakeup(dev);
  171. int rc;
  172. rc = ehci_suspend(hcd, do_wakeup);
  173. if (exynos_ehci->otg)
  174. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  175. if (exynos_ehci->phy)
  176. usb_phy_shutdown(exynos_ehci->phy);
  177. clk_disable_unprepare(exynos_ehci->clk);
  178. return rc;
  179. }
  180. static int exynos_ehci_resume(struct device *dev)
  181. {
  182. struct usb_hcd *hcd = dev_get_drvdata(dev);
  183. struct exynos_ehci_hcd *exynos_ehci = to_exynos_ehci(hcd);
  184. clk_prepare_enable(exynos_ehci->clk);
  185. if (exynos_ehci->otg)
  186. exynos_ehci->otg->set_host(exynos_ehci->otg, &hcd->self);
  187. if (exynos_ehci->phy)
  188. usb_phy_init(exynos_ehci->phy);
  189. /* DMA burst Enable */
  190. writel(EHCI_INSNREG00_ENABLE_DMA_BURST, EHCI_INSNREG00(hcd->regs));
  191. ehci_resume(hcd, false);
  192. return 0;
  193. }
  194. #else
  195. #define exynos_ehci_suspend NULL
  196. #define exynos_ehci_resume NULL
  197. #endif
  198. static const struct dev_pm_ops exynos_ehci_pm_ops = {
  199. .suspend = exynos_ehci_suspend,
  200. .resume = exynos_ehci_resume,
  201. };
  202. #ifdef CONFIG_OF
  203. static const struct of_device_id exynos_ehci_match[] = {
  204. { .compatible = "samsung,exynos4210-ehci" },
  205. { .compatible = "samsung,exynos5440-ehci" },
  206. {},
  207. };
  208. MODULE_DEVICE_TABLE(of, exynos_ehci_match);
  209. #endif
  210. static struct platform_driver exynos_ehci_driver = {
  211. .probe = exynos_ehci_probe,
  212. .remove = exynos_ehci_remove,
  213. .shutdown = usb_hcd_platform_shutdown,
  214. .driver = {
  215. .name = "exynos-ehci",
  216. .owner = THIS_MODULE,
  217. .pm = &exynos_ehci_pm_ops,
  218. .of_match_table = of_match_ptr(exynos_ehci_match),
  219. }
  220. };
  221. static const struct ehci_driver_overrides exynos_overrides __initdata = {
  222. .extra_priv_size = sizeof(struct exynos_ehci_hcd),
  223. };
  224. static int __init ehci_exynos_init(void)
  225. {
  226. if (usb_disabled())
  227. return -ENODEV;
  228. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  229. ehci_init_driver(&exynos_ehci_hc_driver, &exynos_overrides);
  230. return platform_driver_register(&exynos_ehci_driver);
  231. }
  232. module_init(ehci_exynos_init);
  233. static void __exit ehci_exynos_cleanup(void)
  234. {
  235. platform_driver_unregister(&exynos_ehci_driver);
  236. }
  237. module_exit(ehci_exynos_cleanup);
  238. MODULE_DESCRIPTION(DRIVER_DESC);
  239. MODULE_ALIAS("platform:exynos-ehci");
  240. MODULE_AUTHOR("Jingoo Han");
  241. MODULE_AUTHOR("Joonyoung Shim");
  242. MODULE_LICENSE("GPL v2");