ohci-exynos.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/platform_data/usb-exynos.h>
  17. #include <plat/usb-phy.h>
  18. struct exynos_ohci_hcd {
  19. struct device *dev;
  20. struct usb_hcd *hcd;
  21. struct clk *clk;
  22. };
  23. static int ohci_exynos_reset(struct usb_hcd *hcd)
  24. {
  25. return ohci_init(hcd_to_ohci(hcd));
  26. }
  27. static int ohci_exynos_start(struct usb_hcd *hcd)
  28. {
  29. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  30. int ret;
  31. ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
  32. ret = ohci_run(ohci);
  33. if (ret < 0) {
  34. dev_err(hcd->self.controller, "can't start %s\n",
  35. hcd->self.bus_name);
  36. ohci_stop(hcd);
  37. return ret;
  38. }
  39. return 0;
  40. }
  41. static const struct hc_driver exynos_ohci_hc_driver = {
  42. .description = hcd_name,
  43. .product_desc = "EXYNOS OHCI Host Controller",
  44. .hcd_priv_size = sizeof(struct ohci_hcd),
  45. .irq = ohci_irq,
  46. .flags = HCD_MEMORY|HCD_USB11,
  47. .reset = ohci_exynos_reset,
  48. .start = ohci_exynos_start,
  49. .stop = ohci_stop,
  50. .shutdown = ohci_shutdown,
  51. .get_frame_number = ohci_get_frame,
  52. .urb_enqueue = ohci_urb_enqueue,
  53. .urb_dequeue = ohci_urb_dequeue,
  54. .endpoint_disable = ohci_endpoint_disable,
  55. .hub_status_data = ohci_hub_status_data,
  56. .hub_control = ohci_hub_control,
  57. #ifdef CONFIG_PM
  58. .bus_suspend = ohci_bus_suspend,
  59. .bus_resume = ohci_bus_resume,
  60. #endif
  61. .start_port_reset = ohci_start_port_reset,
  62. };
  63. static u64 ohci_exynos_dma_mask = DMA_BIT_MASK(32);
  64. static int exynos_ohci_probe(struct platform_device *pdev)
  65. {
  66. struct exynos4_ohci_platdata *pdata;
  67. struct exynos_ohci_hcd *exynos_ohci;
  68. struct usb_hcd *hcd;
  69. struct ohci_hcd *ohci;
  70. struct resource *res;
  71. int irq;
  72. int err;
  73. pdata = pdev->dev.platform_data;
  74. if (!pdata) {
  75. dev_err(&pdev->dev, "No platform data defined\n");
  76. return -EINVAL;
  77. }
  78. /*
  79. * Right now device-tree probed devices don't get dma_mask set.
  80. * Since shared usb code relies on it, set it here for now.
  81. * Once we move to full device tree support this will vanish off.
  82. */
  83. if (!pdev->dev.dma_mask)
  84. pdev->dev.dma_mask = &ohci_exynos_dma_mask;
  85. if (!pdev->dev.coherent_dma_mask)
  86. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  87. exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
  88. GFP_KERNEL);
  89. if (!exynos_ohci)
  90. return -ENOMEM;
  91. exynos_ohci->dev = &pdev->dev;
  92. hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
  93. dev_name(&pdev->dev));
  94. if (!hcd) {
  95. dev_err(&pdev->dev, "Unable to create HCD\n");
  96. return -ENOMEM;
  97. }
  98. exynos_ohci->hcd = hcd;
  99. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  100. if (IS_ERR(exynos_ohci->clk)) {
  101. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  102. err = PTR_ERR(exynos_ohci->clk);
  103. goto fail_clk;
  104. }
  105. err = clk_prepare_enable(exynos_ohci->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 (pdata->phy_init)
  129. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  130. ohci = hcd_to_ohci(hcd);
  131. ohci_hcd_init(ohci);
  132. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  133. if (err) {
  134. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  135. goto fail_io;
  136. }
  137. platform_set_drvdata(pdev, exynos_ohci);
  138. return 0;
  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 exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
  148. struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
  149. struct usb_hcd *hcd = exynos_ohci->hcd;
  150. usb_remove_hcd(hcd);
  151. if (pdata && pdata->phy_exit)
  152. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  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 exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
  160. struct usb_hcd *hcd = exynos_ohci->hcd;
  161. if (hcd->driver->shutdown)
  162. hcd->driver->shutdown(hcd);
  163. }
  164. #ifdef CONFIG_PM
  165. static int exynos_ohci_suspend(struct device *dev)
  166. {
  167. struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
  168. struct usb_hcd *hcd = exynos_ohci->hcd;
  169. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  170. struct platform_device *pdev = to_platform_device(dev);
  171. struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
  172. unsigned long flags;
  173. int rc = 0;
  174. /*
  175. * Root hub was already suspended. Disable irq emission and
  176. * mark HW unaccessible, bail out if RH has been resumed. Use
  177. * the spinlock to properly synchronize with possible pending
  178. * RH suspend or resume activity.
  179. */
  180. spin_lock_irqsave(&ohci->lock, flags);
  181. if (ohci->rh_state != OHCI_RH_SUSPENDED &&
  182. ohci->rh_state != OHCI_RH_HALTED) {
  183. rc = -EINVAL;
  184. goto fail;
  185. }
  186. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  187. if (pdata && pdata->phy_exit)
  188. pdata->phy_exit(pdev, S5P_USB_PHY_HOST);
  189. clk_disable_unprepare(exynos_ohci->clk);
  190. fail:
  191. spin_unlock_irqrestore(&ohci->lock, flags);
  192. return rc;
  193. }
  194. static int exynos_ohci_resume(struct device *dev)
  195. {
  196. struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
  197. struct usb_hcd *hcd = exynos_ohci->hcd;
  198. struct platform_device *pdev = to_platform_device(dev);
  199. struct exynos4_ohci_platdata *pdata = pdev->dev.platform_data;
  200. clk_prepare_enable(exynos_ohci->clk);
  201. if (pdata && pdata->phy_init)
  202. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  203. ohci_resume(hcd, false);
  204. return 0;
  205. }
  206. #else
  207. #define exynos_ohci_suspend NULL
  208. #define exynos_ohci_resume NULL
  209. #endif
  210. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  211. .suspend = exynos_ohci_suspend,
  212. .resume = exynos_ohci_resume,
  213. };
  214. #ifdef CONFIG_OF
  215. static const struct of_device_id exynos_ohci_match[] = {
  216. { .compatible = "samsung,exynos-ohci" },
  217. {},
  218. };
  219. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  220. #endif
  221. static struct platform_driver exynos_ohci_driver = {
  222. .probe = exynos_ohci_probe,
  223. .remove = exynos_ohci_remove,
  224. .shutdown = exynos_ohci_shutdown,
  225. .driver = {
  226. .name = "exynos-ohci",
  227. .owner = THIS_MODULE,
  228. .pm = &exynos_ohci_pm_ops,
  229. .of_match_table = of_match_ptr(exynos_ohci_match),
  230. }
  231. };
  232. MODULE_ALIAS("platform:exynos-ohci");
  233. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");