ohci-exynos.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 && 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 && 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. if (of_device_is_compatible(pdev->dev.of_node,
  109. "samsung,exynos5440-ohci"))
  110. goto skip_phy;
  111. phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  112. if (IS_ERR(phy)) {
  113. /* Fallback to pdata */
  114. if (!pdata) {
  115. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  116. return -EPROBE_DEFER;
  117. } else {
  118. exynos_ohci->pdata = pdata;
  119. }
  120. } else {
  121. exynos_ohci->phy = phy;
  122. exynos_ohci->otg = phy->otg;
  123. }
  124. skip_phy:
  125. exynos_ohci->dev = &pdev->dev;
  126. hcd = usb_create_hcd(&exynos_ohci_hc_driver, &pdev->dev,
  127. dev_name(&pdev->dev));
  128. if (!hcd) {
  129. dev_err(&pdev->dev, "Unable to create HCD\n");
  130. return -ENOMEM;
  131. }
  132. exynos_ohci->hcd = hcd;
  133. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  134. if (IS_ERR(exynos_ohci->clk)) {
  135. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  136. err = PTR_ERR(exynos_ohci->clk);
  137. goto fail_clk;
  138. }
  139. err = clk_prepare_enable(exynos_ohci->clk);
  140. if (err)
  141. goto fail_clk;
  142. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  143. if (!res) {
  144. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  145. err = -ENXIO;
  146. goto fail_io;
  147. }
  148. hcd->rsrc_start = res->start;
  149. hcd->rsrc_len = resource_size(res);
  150. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  151. if (!hcd->regs) {
  152. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  153. err = -ENOMEM;
  154. goto fail_io;
  155. }
  156. irq = platform_get_irq(pdev, 0);
  157. if (!irq) {
  158. dev_err(&pdev->dev, "Failed to get IRQ\n");
  159. err = -ENODEV;
  160. goto fail_io;
  161. }
  162. if (exynos_ohci->otg)
  163. exynos_ohci->otg->set_host(exynos_ohci->otg,
  164. &exynos_ohci->hcd->self);
  165. exynos_ohci_phy_enable(exynos_ohci);
  166. ohci = hcd_to_ohci(hcd);
  167. ohci_hcd_init(ohci);
  168. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  169. if (err) {
  170. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  171. goto fail_add_hcd;
  172. }
  173. platform_set_drvdata(pdev, exynos_ohci);
  174. return 0;
  175. fail_add_hcd:
  176. exynos_ohci_phy_disable(exynos_ohci);
  177. fail_io:
  178. clk_disable_unprepare(exynos_ohci->clk);
  179. fail_clk:
  180. usb_put_hcd(hcd);
  181. return err;
  182. }
  183. static int exynos_ohci_remove(struct platform_device *pdev)
  184. {
  185. struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
  186. struct usb_hcd *hcd = exynos_ohci->hcd;
  187. usb_remove_hcd(hcd);
  188. if (exynos_ohci->otg)
  189. exynos_ohci->otg->set_host(exynos_ohci->otg,
  190. &exynos_ohci->hcd->self);
  191. exynos_ohci_phy_disable(exynos_ohci);
  192. clk_disable_unprepare(exynos_ohci->clk);
  193. usb_put_hcd(hcd);
  194. return 0;
  195. }
  196. static void exynos_ohci_shutdown(struct platform_device *pdev)
  197. {
  198. struct exynos_ohci_hcd *exynos_ohci = platform_get_drvdata(pdev);
  199. struct usb_hcd *hcd = exynos_ohci->hcd;
  200. if (hcd->driver->shutdown)
  201. hcd->driver->shutdown(hcd);
  202. }
  203. #ifdef CONFIG_PM
  204. static int exynos_ohci_suspend(struct device *dev)
  205. {
  206. struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
  207. struct usb_hcd *hcd = exynos_ohci->hcd;
  208. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  209. unsigned long flags;
  210. int rc = 0;
  211. /*
  212. * Root hub was already suspended. Disable irq emission and
  213. * mark HW unaccessible, bail out if RH has been resumed. Use
  214. * the spinlock to properly synchronize with possible pending
  215. * RH suspend or resume activity.
  216. */
  217. spin_lock_irqsave(&ohci->lock, flags);
  218. if (ohci->rh_state != OHCI_RH_SUSPENDED &&
  219. ohci->rh_state != OHCI_RH_HALTED) {
  220. rc = -EINVAL;
  221. goto fail;
  222. }
  223. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  224. if (exynos_ohci->otg)
  225. exynos_ohci->otg->set_host(exynos_ohci->otg,
  226. &exynos_ohci->hcd->self);
  227. exynos_ohci_phy_disable(exynos_ohci);
  228. clk_disable_unprepare(exynos_ohci->clk);
  229. fail:
  230. spin_unlock_irqrestore(&ohci->lock, flags);
  231. return rc;
  232. }
  233. static int exynos_ohci_resume(struct device *dev)
  234. {
  235. struct exynos_ohci_hcd *exynos_ohci = dev_get_drvdata(dev);
  236. struct usb_hcd *hcd = exynos_ohci->hcd;
  237. clk_prepare_enable(exynos_ohci->clk);
  238. if (exynos_ohci->otg)
  239. exynos_ohci->otg->set_host(exynos_ohci->otg,
  240. &exynos_ohci->hcd->self);
  241. exynos_ohci_phy_enable(exynos_ohci);
  242. ohci_resume(hcd, false);
  243. return 0;
  244. }
  245. #else
  246. #define exynos_ohci_suspend NULL
  247. #define exynos_ohci_resume NULL
  248. #endif
  249. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  250. .suspend = exynos_ohci_suspend,
  251. .resume = exynos_ohci_resume,
  252. };
  253. #ifdef CONFIG_OF
  254. static const struct of_device_id exynos_ohci_match[] = {
  255. { .compatible = "samsung,exynos4210-ohci" },
  256. { .compatible = "samsung,exynos5440-ohci" },
  257. {},
  258. };
  259. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  260. #endif
  261. static struct platform_driver exynos_ohci_driver = {
  262. .probe = exynos_ohci_probe,
  263. .remove = exynos_ohci_remove,
  264. .shutdown = exynos_ohci_shutdown,
  265. .driver = {
  266. .name = "exynos-ohci",
  267. .owner = THIS_MODULE,
  268. .pm = &exynos_ohci_pm_ops,
  269. .of_match_table = of_match_ptr(exynos_ohci_match),
  270. }
  271. };
  272. MODULE_ALIAS("platform:exynos-ohci");
  273. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");