ohci-exynos.c 7.1 KB

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