ohci-exynos.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <mach/ohci.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_start(struct usb_hcd *hcd)
  24. {
  25. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  26. int ret;
  27. ohci_dbg(ohci, "ohci_exynos_start, ohci:%p", ohci);
  28. ret = ohci_init(ohci);
  29. if (ret < 0)
  30. return ret;
  31. ret = ohci_run(ohci);
  32. if (ret < 0) {
  33. dev_err(hcd->self.controller, "can't start %s\n",
  34. hcd->self.bus_name);
  35. ohci_stop(hcd);
  36. return ret;
  37. }
  38. return 0;
  39. }
  40. static const struct hc_driver exynos_ohci_hc_driver = {
  41. .description = hcd_name,
  42. .product_desc = "EXYNOS OHCI Host Controller",
  43. .hcd_priv_size = sizeof(struct ohci_hcd),
  44. .irq = ohci_irq,
  45. .flags = HCD_MEMORY|HCD_USB11,
  46. .start = ohci_exynos_start,
  47. .stop = ohci_stop,
  48. .shutdown = ohci_shutdown,
  49. .get_frame_number = ohci_get_frame,
  50. .urb_enqueue = ohci_urb_enqueue,
  51. .urb_dequeue = ohci_urb_dequeue,
  52. .endpoint_disable = ohci_endpoint_disable,
  53. .hub_status_data = ohci_hub_status_data,
  54. .hub_control = ohci_hub_control,
  55. #ifdef CONFIG_PM
  56. .bus_suspend = ohci_bus_suspend,
  57. .bus_resume = ohci_bus_resume,
  58. #endif
  59. .start_port_reset = ohci_start_port_reset,
  60. };
  61. static u64 ohci_exynos_dma_mask = DMA_BIT_MASK(32);
  62. static int __devinit exynos_ohci_probe(struct platform_device *pdev)
  63. {
  64. struct exynos4_ohci_platdata *pdata;
  65. struct exynos_ohci_hcd *exynos_ohci;
  66. struct usb_hcd *hcd;
  67. struct ohci_hcd *ohci;
  68. struct resource *res;
  69. int irq;
  70. int err;
  71. pdata = pdev->dev.platform_data;
  72. if (!pdata) {
  73. dev_err(&pdev->dev, "No platform data defined\n");
  74. return -EINVAL;
  75. }
  76. /*
  77. * Right now device-tree probed devices don't get dma_mask set.
  78. * Since shared usb code relies on it, set it here for now.
  79. * Once we move to full device tree support this will vanish off.
  80. */
  81. if (!pdev->dev.dma_mask)
  82. pdev->dev.dma_mask = &ohci_exynos_dma_mask;
  83. if (!pdev->dev.coherent_dma_mask)
  84. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  85. exynos_ohci = devm_kzalloc(&pdev->dev, sizeof(struct exynos_ohci_hcd),
  86. GFP_KERNEL);
  87. if (!exynos_ohci)
  88. return -ENOMEM;
  89. exynos_ohci->dev = &pdev->dev;
  90. hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
  91. dev_name(&pdev->dev));
  92. if (!hcd) {
  93. dev_err(&pdev->dev, "Unable to create HCD\n");
  94. return -ENOMEM;
  95. }
  96. exynos_ohci->hcd = hcd;
  97. exynos_ohci->clk = clk_get(&pdev->dev, "usbhost");
  98. if (IS_ERR(exynos_ohci->clk)) {
  99. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  100. err = PTR_ERR(exynos_ohci->clk);
  101. goto fail_clk;
  102. }
  103. err = clk_enable(exynos_ohci->clk);
  104. if (err)
  105. goto fail_clken;
  106. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  107. if (!res) {
  108. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  109. err = -ENXIO;
  110. goto fail_io;
  111. }
  112. hcd->rsrc_start = res->start;
  113. hcd->rsrc_len = resource_size(res);
  114. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  115. if (!hcd->regs) {
  116. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  117. err = -ENOMEM;
  118. goto fail_io;
  119. }
  120. irq = platform_get_irq(pdev, 0);
  121. if (!irq) {
  122. dev_err(&pdev->dev, "Failed to get IRQ\n");
  123. err = -ENODEV;
  124. goto fail_io;
  125. }
  126. if (pdata->phy_init)
  127. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  128. ohci = hcd_to_ohci(hcd);
  129. ohci_hcd_init(ohci);
  130. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  131. if (err) {
  132. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  133. goto fail_io;
  134. }
  135. platform_set_drvdata(pdev, exynos_ohci);
  136. return 0;
  137. fail_io:
  138. clk_disable(exynos_ohci->clk);
  139. fail_clken:
  140. clk_put(exynos_ohci->clk);
  141. fail_clk:
  142. usb_put_hcd(hcd);
  143. return err;
  144. }
  145. static int __devexit 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(exynos_ohci->clk);
  154. clk_put(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, S5P_USB_PHY_HOST);
  190. clk_disable(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_enable(exynos_ohci->clk);
  202. if (pdata && pdata->phy_init)
  203. pdata->phy_init(pdev, S5P_USB_PHY_HOST);
  204. /* Mark hardware accessible again as we are out of D3 state by now */
  205. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  206. ohci_finish_controller_resume(hcd);
  207. return 0;
  208. }
  209. #else
  210. #define exynos_ohci_suspend NULL
  211. #define exynos_ohci_resume NULL
  212. #endif
  213. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  214. .suspend = exynos_ohci_suspend,
  215. .resume = exynos_ohci_resume,
  216. };
  217. #ifdef CONFIG_OF
  218. static const struct of_device_id exynos_ohci_match[] = {
  219. { .compatible = "samsung,exynos-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 = __devexit_p(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. MODULE_ALIAS("platform:exynos-ohci");
  236. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");