ohci-exynos.c 8.0 KB

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