ohci-exynos.c 7.9 KB

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