ohci-exynos.c 7.3 KB

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