ohci-exynos.c 7.8 KB

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