ohci-exynos.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. usb_put_hcd(hcd);
  88. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  89. return -EPROBE_DEFER;
  90. } else {
  91. exynos_ohci->pdata = pdata;
  92. }
  93. } else {
  94. exynos_ohci->phy = phy;
  95. exynos_ohci->otg = phy->otg;
  96. }
  97. skip_phy:
  98. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  99. if (IS_ERR(exynos_ohci->clk)) {
  100. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  101. err = PTR_ERR(exynos_ohci->clk);
  102. goto fail_clk;
  103. }
  104. err = clk_prepare_enable(exynos_ohci->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_ohci->otg)
  128. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  129. platform_set_drvdata(pdev, hcd);
  130. exynos_ohci_phy_enable(pdev);
  131. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  132. if (err) {
  133. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  134. goto fail_add_hcd;
  135. }
  136. return 0;
  137. fail_add_hcd:
  138. exynos_ohci_phy_disable(pdev);
  139. fail_io:
  140. clk_disable_unprepare(exynos_ohci->clk);
  141. fail_clk:
  142. usb_put_hcd(hcd);
  143. return err;
  144. }
  145. static int exynos_ohci_remove(struct platform_device *pdev)
  146. {
  147. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  148. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  149. usb_remove_hcd(hcd);
  150. if (exynos_ohci->otg)
  151. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  152. exynos_ohci_phy_disable(pdev);
  153. clk_disable_unprepare(exynos_ohci->clk);
  154. usb_put_hcd(hcd);
  155. return 0;
  156. }
  157. static void exynos_ohci_shutdown(struct platform_device *pdev)
  158. {
  159. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  160. if (hcd->driver->shutdown)
  161. hcd->driver->shutdown(hcd);
  162. }
  163. #ifdef CONFIG_PM
  164. static int exynos_ohci_suspend(struct device *dev)
  165. {
  166. struct usb_hcd *hcd = dev_get_drvdata(dev);
  167. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  168. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  169. struct platform_device *pdev = to_platform_device(dev);
  170. unsigned long flags;
  171. int rc = 0;
  172. /*
  173. * Root hub was already suspended. Disable irq emission and
  174. * mark HW unaccessible, bail out if RH has been resumed. Use
  175. * the spinlock to properly synchronize with possible pending
  176. * RH suspend or resume activity.
  177. */
  178. spin_lock_irqsave(&ohci->lock, flags);
  179. if (ohci->rh_state != OHCI_RH_SUSPENDED &&
  180. ohci->rh_state != OHCI_RH_HALTED) {
  181. rc = -EINVAL;
  182. goto fail;
  183. }
  184. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  185. if (exynos_ohci->otg)
  186. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  187. exynos_ohci_phy_disable(pdev);
  188. clk_disable_unprepare(exynos_ohci->clk);
  189. fail:
  190. spin_unlock_irqrestore(&ohci->lock, flags);
  191. return rc;
  192. }
  193. static int exynos_ohci_resume(struct device *dev)
  194. {
  195. struct usb_hcd *hcd = dev_get_drvdata(dev);
  196. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  197. struct platform_device *pdev = to_platform_device(dev);
  198. clk_prepare_enable(exynos_ohci->clk);
  199. if (exynos_ohci->otg)
  200. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  201. exynos_ohci_phy_enable(pdev);
  202. ohci_resume(hcd, false);
  203. return 0;
  204. }
  205. #else
  206. #define exynos_ohci_suspend NULL
  207. #define exynos_ohci_resume NULL
  208. #endif
  209. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  210. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  211. };
  212. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  213. .suspend = exynos_ohci_suspend,
  214. .resume = exynos_ohci_resume,
  215. };
  216. #ifdef CONFIG_OF
  217. static const struct of_device_id exynos_ohci_match[] = {
  218. { .compatible = "samsung,exynos4210-ohci" },
  219. { .compatible = "samsung,exynos5440-ohci" },
  220. {},
  221. };
  222. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  223. #endif
  224. static struct platform_driver exynos_ohci_driver = {
  225. .probe = exynos_ohci_probe,
  226. .remove = exynos_ohci_remove,
  227. .shutdown = exynos_ohci_shutdown,
  228. .driver = {
  229. .name = "exynos-ohci",
  230. .owner = THIS_MODULE,
  231. .pm = &exynos_ohci_pm_ops,
  232. .of_match_table = of_match_ptr(exynos_ohci_match),
  233. }
  234. };
  235. static int __init ohci_exynos_init(void)
  236. {
  237. if (usb_disabled())
  238. return -ENODEV;
  239. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  240. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  241. return platform_driver_register(&exynos_ohci_driver);
  242. }
  243. module_init(ohci_exynos_init);
  244. static void __exit ohci_exynos_cleanup(void)
  245. {
  246. platform_driver_unregister(&exynos_ohci_driver);
  247. }
  248. module_exit(ohci_exynos_cleanup);
  249. MODULE_ALIAS("platform:exynos-ohci");
  250. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  251. MODULE_LICENSE("GPL v2");