ohci-exynos.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/usb/phy.h>
  21. #include <linux/usb/samsung_usb_phy.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/hcd.h>
  24. #include <linux/usb/otg.h>
  25. #include "ohci.h"
  26. #define DRIVER_DESC "OHCI EXYNOS driver"
  27. static const char hcd_name[] = "ohci-exynos";
  28. static struct hc_driver __read_mostly exynos_ohci_hc_driver;
  29. #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
  30. struct exynos_ohci_hcd {
  31. struct clk *clk;
  32. struct usb_phy *phy;
  33. struct usb_otg *otg;
  34. };
  35. static void exynos_ohci_phy_enable(struct platform_device *pdev)
  36. {
  37. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  38. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  39. if (exynos_ohci->phy)
  40. usb_phy_init(exynos_ohci->phy);
  41. }
  42. static void exynos_ohci_phy_disable(struct platform_device *pdev)
  43. {
  44. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  45. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  46. if (exynos_ohci->phy)
  47. usb_phy_shutdown(exynos_ohci->phy);
  48. }
  49. static int exynos_ohci_probe(struct platform_device *pdev)
  50. {
  51. struct exynos_ohci_hcd *exynos_ohci;
  52. struct usb_hcd *hcd;
  53. struct resource *res;
  54. struct usb_phy *phy;
  55. int irq;
  56. int err;
  57. /*
  58. * Right now device-tree probed devices don't get dma_mask set.
  59. * Since shared usb code relies on it, set it here for now.
  60. * Once we move to full device tree support this will vanish off.
  61. */
  62. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  63. if (err)
  64. return err;
  65. hcd = usb_create_hcd(&exynos_ohci_hc_driver,
  66. &pdev->dev, dev_name(&pdev->dev));
  67. if (!hcd) {
  68. dev_err(&pdev->dev, "Unable to create HCD\n");
  69. return -ENOMEM;
  70. }
  71. exynos_ohci = to_exynos_ohci(hcd);
  72. if (of_device_is_compatible(pdev->dev.of_node,
  73. "samsung,exynos5440-ohci"))
  74. goto skip_phy;
  75. phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  76. if (IS_ERR(phy)) {
  77. usb_put_hcd(hcd);
  78. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  79. return -EPROBE_DEFER;
  80. } else {
  81. exynos_ohci->phy = phy;
  82. exynos_ohci->otg = phy->otg;
  83. }
  84. skip_phy:
  85. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  86. if (IS_ERR(exynos_ohci->clk)) {
  87. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  88. err = PTR_ERR(exynos_ohci->clk);
  89. goto fail_clk;
  90. }
  91. err = clk_prepare_enable(exynos_ohci->clk);
  92. if (err)
  93. goto fail_clk;
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. if (!res) {
  96. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  97. err = -ENXIO;
  98. goto fail_io;
  99. }
  100. hcd->rsrc_start = res->start;
  101. hcd->rsrc_len = resource_size(res);
  102. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  103. if (!hcd->regs) {
  104. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  105. err = -ENOMEM;
  106. goto fail_io;
  107. }
  108. irq = platform_get_irq(pdev, 0);
  109. if (!irq) {
  110. dev_err(&pdev->dev, "Failed to get IRQ\n");
  111. err = -ENODEV;
  112. goto fail_io;
  113. }
  114. if (exynos_ohci->otg)
  115. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  116. platform_set_drvdata(pdev, hcd);
  117. exynos_ohci_phy_enable(pdev);
  118. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  119. if (err) {
  120. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  121. goto fail_add_hcd;
  122. }
  123. return 0;
  124. fail_add_hcd:
  125. exynos_ohci_phy_disable(pdev);
  126. fail_io:
  127. clk_disable_unprepare(exynos_ohci->clk);
  128. fail_clk:
  129. usb_put_hcd(hcd);
  130. return err;
  131. }
  132. static int exynos_ohci_remove(struct platform_device *pdev)
  133. {
  134. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  135. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  136. usb_remove_hcd(hcd);
  137. if (exynos_ohci->otg)
  138. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  139. exynos_ohci_phy_disable(pdev);
  140. clk_disable_unprepare(exynos_ohci->clk);
  141. usb_put_hcd(hcd);
  142. return 0;
  143. }
  144. static void exynos_ohci_shutdown(struct platform_device *pdev)
  145. {
  146. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  147. if (hcd->driver->shutdown)
  148. hcd->driver->shutdown(hcd);
  149. }
  150. #ifdef CONFIG_PM
  151. static int exynos_ohci_suspend(struct device *dev)
  152. {
  153. struct usb_hcd *hcd = dev_get_drvdata(dev);
  154. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  155. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  156. struct platform_device *pdev = to_platform_device(dev);
  157. unsigned long flags;
  158. int rc = 0;
  159. /*
  160. * Root hub was already suspended. Disable irq emission and
  161. * mark HW unaccessible, bail out if RH has been resumed. Use
  162. * the spinlock to properly synchronize with possible pending
  163. * RH suspend or resume activity.
  164. */
  165. spin_lock_irqsave(&ohci->lock, flags);
  166. if (ohci->rh_state != OHCI_RH_SUSPENDED &&
  167. ohci->rh_state != OHCI_RH_HALTED) {
  168. rc = -EINVAL;
  169. goto fail;
  170. }
  171. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  172. if (exynos_ohci->otg)
  173. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  174. exynos_ohci_phy_disable(pdev);
  175. clk_disable_unprepare(exynos_ohci->clk);
  176. fail:
  177. spin_unlock_irqrestore(&ohci->lock, flags);
  178. return rc;
  179. }
  180. static int exynos_ohci_resume(struct device *dev)
  181. {
  182. struct usb_hcd *hcd = dev_get_drvdata(dev);
  183. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  184. struct platform_device *pdev = to_platform_device(dev);
  185. clk_prepare_enable(exynos_ohci->clk);
  186. if (exynos_ohci->otg)
  187. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  188. exynos_ohci_phy_enable(pdev);
  189. ohci_resume(hcd, false);
  190. return 0;
  191. }
  192. #else
  193. #define exynos_ohci_suspend NULL
  194. #define exynos_ohci_resume NULL
  195. #endif
  196. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  197. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  198. };
  199. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  200. .suspend = exynos_ohci_suspend,
  201. .resume = exynos_ohci_resume,
  202. };
  203. #ifdef CONFIG_OF
  204. static const struct of_device_id exynos_ohci_match[] = {
  205. { .compatible = "samsung,exynos4210-ohci" },
  206. { .compatible = "samsung,exynos5440-ohci" },
  207. {},
  208. };
  209. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  210. #endif
  211. static struct platform_driver exynos_ohci_driver = {
  212. .probe = exynos_ohci_probe,
  213. .remove = exynos_ohci_remove,
  214. .shutdown = exynos_ohci_shutdown,
  215. .driver = {
  216. .name = "exynos-ohci",
  217. .owner = THIS_MODULE,
  218. .pm = &exynos_ohci_pm_ops,
  219. .of_match_table = of_match_ptr(exynos_ohci_match),
  220. }
  221. };
  222. static int __init ohci_exynos_init(void)
  223. {
  224. if (usb_disabled())
  225. return -ENODEV;
  226. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  227. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  228. return platform_driver_register(&exynos_ohci_driver);
  229. }
  230. module_init(ohci_exynos_init);
  231. static void __exit ohci_exynos_cleanup(void)
  232. {
  233. platform_driver_unregister(&exynos_ohci_driver);
  234. }
  235. module_exit(ohci_exynos_cleanup);
  236. MODULE_ALIAS("platform:exynos-ohci");
  237. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  238. MODULE_LICENSE("GPL v2");