ohci-exynos.c 7.9 KB

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