ohci-exynos.c 6.8 KB

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