xhci-plat.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * xhci-plat.c - xHCI host controller driver platform Bus Glue.
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  5. * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  6. *
  7. * A lot of code borrowed from the Linux xHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/usb/phy.h>
  19. #include <linux/slab.h>
  20. #include <linux/usb/xhci_pdriver.h>
  21. #include <linux/acpi.h>
  22. #include "xhci.h"
  23. #include "xhci-mvebu.h"
  24. #include "xhci-rcar.h"
  25. static struct hc_driver __read_mostly xhci_plat_hc_driver;
  26. static int xhci_plat_setup(struct usb_hcd *hcd);
  27. static int xhci_plat_start(struct usb_hcd *hcd);
  28. static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
  29. .extra_priv_size = sizeof(struct xhci_hcd),
  30. .reset = xhci_plat_setup,
  31. .start = xhci_plat_start,
  32. };
  33. static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  34. {
  35. /*
  36. * As of now platform drivers don't provide MSI support so we ensure
  37. * here that the generic code does not try to make a pci_dev from our
  38. * dev struct in order to setup MSI
  39. */
  40. xhci->quirks |= XHCI_PLAT;
  41. }
  42. /* called during probe() after chip reset completes */
  43. static int xhci_plat_setup(struct usb_hcd *hcd)
  44. {
  45. struct device_node *of_node = hcd->self.controller->of_node;
  46. int ret;
  47. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  48. of_device_is_compatible(of_node, "renesas,xhci-r8a7791")) {
  49. ret = xhci_rcar_init_quirk(hcd);
  50. if (ret)
  51. return ret;
  52. }
  53. return xhci_gen_setup(hcd, xhci_plat_quirks);
  54. }
  55. static int xhci_plat_start(struct usb_hcd *hcd)
  56. {
  57. struct device_node *of_node = hcd->self.controller->of_node;
  58. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  59. of_device_is_compatible(of_node, "renesas,xhci-r8a7791"))
  60. xhci_rcar_start(hcd);
  61. return xhci_run(hcd);
  62. }
  63. static int xhci_plat_probe(struct platform_device *pdev)
  64. {
  65. struct device_node *node = pdev->dev.of_node;
  66. struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev);
  67. const struct hc_driver *driver;
  68. struct xhci_hcd *xhci;
  69. struct resource *res;
  70. struct usb_hcd *hcd;
  71. struct clk *clk;
  72. int ret;
  73. int irq;
  74. if (usb_disabled())
  75. return -ENODEV;
  76. driver = &xhci_plat_hc_driver;
  77. irq = platform_get_irq(pdev, 0);
  78. if (irq < 0)
  79. return irq;
  80. /* Try to set 64-bit DMA first */
  81. if (WARN_ON(!pdev->dev.dma_mask))
  82. /* Platform did not initialize dma_mask */
  83. ret = dma_coerce_mask_and_coherent(&pdev->dev,
  84. DMA_BIT_MASK(64));
  85. else
  86. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  87. /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
  88. if (ret) {
  89. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  90. if (ret)
  91. return ret;
  92. }
  93. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  94. if (!hcd)
  95. return -ENOMEM;
  96. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  97. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  98. if (IS_ERR(hcd->regs)) {
  99. ret = PTR_ERR(hcd->regs);
  100. goto put_hcd;
  101. }
  102. hcd->rsrc_start = res->start;
  103. hcd->rsrc_len = resource_size(res);
  104. /*
  105. * Not all platforms have a clk so it is not an error if the
  106. * clock does not exists.
  107. */
  108. clk = devm_clk_get(&pdev->dev, NULL);
  109. if (!IS_ERR(clk)) {
  110. ret = clk_prepare_enable(clk);
  111. if (ret)
  112. goto put_hcd;
  113. } else if (PTR_ERR(clk) == -EPROBE_DEFER) {
  114. ret = -EPROBE_DEFER;
  115. goto put_hcd;
  116. }
  117. if (of_device_is_compatible(pdev->dev.of_node,
  118. "marvell,armada-375-xhci") ||
  119. of_device_is_compatible(pdev->dev.of_node,
  120. "marvell,armada-380-xhci")) {
  121. ret = xhci_mvebu_mbus_init_quirk(pdev);
  122. if (ret)
  123. goto disable_clk;
  124. }
  125. device_wakeup_enable(hcd->self.controller);
  126. xhci = hcd_to_xhci(hcd);
  127. xhci->clk = clk;
  128. xhci->main_hcd = hcd;
  129. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  130. dev_name(&pdev->dev), hcd);
  131. if (!xhci->shared_hcd) {
  132. ret = -ENOMEM;
  133. goto disable_clk;
  134. }
  135. if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
  136. (pdata && pdata->usb3_lpm_capable))
  137. xhci->quirks |= XHCI_LPM_SUPPORT;
  138. hcd->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
  139. if (IS_ERR(hcd->usb_phy)) {
  140. ret = PTR_ERR(hcd->usb_phy);
  141. if (ret == -EPROBE_DEFER)
  142. goto put_usb3_hcd;
  143. hcd->usb_phy = NULL;
  144. } else {
  145. ret = usb_phy_init(hcd->usb_phy);
  146. if (ret)
  147. goto put_usb3_hcd;
  148. }
  149. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  150. if (ret)
  151. goto disable_usb_phy;
  152. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  153. xhci->shared_hcd->can_do_streams = 1;
  154. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  155. if (ret)
  156. goto dealloc_usb2_hcd;
  157. return 0;
  158. dealloc_usb2_hcd:
  159. usb_remove_hcd(hcd);
  160. disable_usb_phy:
  161. usb_phy_shutdown(hcd->usb_phy);
  162. put_usb3_hcd:
  163. usb_put_hcd(xhci->shared_hcd);
  164. disable_clk:
  165. if (!IS_ERR(clk))
  166. clk_disable_unprepare(clk);
  167. put_hcd:
  168. usb_put_hcd(hcd);
  169. return ret;
  170. }
  171. static int xhci_plat_remove(struct platform_device *dev)
  172. {
  173. struct usb_hcd *hcd = platform_get_drvdata(dev);
  174. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  175. struct clk *clk = xhci->clk;
  176. xhci->xhc_state |= XHCI_STATE_REMOVING;
  177. usb_remove_hcd(xhci->shared_hcd);
  178. usb_phy_shutdown(hcd->usb_phy);
  179. usb_remove_hcd(hcd);
  180. usb_put_hcd(xhci->shared_hcd);
  181. if (!IS_ERR(clk))
  182. clk_disable_unprepare(clk);
  183. usb_put_hcd(hcd);
  184. return 0;
  185. }
  186. #ifdef CONFIG_PM_SLEEP
  187. static int xhci_plat_suspend(struct device *dev)
  188. {
  189. struct usb_hcd *hcd = dev_get_drvdata(dev);
  190. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  191. /*
  192. * xhci_suspend() needs `do_wakeup` to know whether host is allowed
  193. * to do wakeup during suspend. Since xhci_plat_suspend is currently
  194. * only designed for system suspend, device_may_wakeup() is enough
  195. * to dertermine whether host is allowed to do wakeup. Need to
  196. * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
  197. * also applies to runtime suspend.
  198. */
  199. return xhci_suspend(xhci, device_may_wakeup(dev));
  200. }
  201. static int xhci_plat_resume(struct device *dev)
  202. {
  203. struct usb_hcd *hcd = dev_get_drvdata(dev);
  204. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  205. return xhci_resume(xhci, 0);
  206. }
  207. static const struct dev_pm_ops xhci_plat_pm_ops = {
  208. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  209. };
  210. #define DEV_PM_OPS (&xhci_plat_pm_ops)
  211. #else
  212. #define DEV_PM_OPS NULL
  213. #endif /* CONFIG_PM */
  214. #ifdef CONFIG_OF
  215. static const struct of_device_id usb_xhci_of_match[] = {
  216. { .compatible = "generic-xhci" },
  217. { .compatible = "xhci-platform" },
  218. { .compatible = "marvell,armada-375-xhci"},
  219. { .compatible = "marvell,armada-380-xhci"},
  220. { .compatible = "renesas,xhci-r8a7790"},
  221. { .compatible = "renesas,xhci-r8a7791"},
  222. { },
  223. };
  224. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  225. #endif
  226. static const struct acpi_device_id usb_xhci_acpi_match[] = {
  227. /* XHCI-compliant USB Controller */
  228. { "PNP0D10", },
  229. { }
  230. };
  231. MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
  232. static struct platform_driver usb_xhci_driver = {
  233. .probe = xhci_plat_probe,
  234. .remove = xhci_plat_remove,
  235. .driver = {
  236. .name = "xhci-hcd",
  237. .pm = DEV_PM_OPS,
  238. .of_match_table = of_match_ptr(usb_xhci_of_match),
  239. .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
  240. },
  241. };
  242. MODULE_ALIAS("platform:xhci-hcd");
  243. static int __init xhci_plat_init(void)
  244. {
  245. xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
  246. return platform_driver_register(&usb_xhci_driver);
  247. }
  248. module_init(xhci_plat_init);
  249. static void __exit xhci_plat_exit(void)
  250. {
  251. platform_driver_unregister(&usb_xhci_driver);
  252. }
  253. module_exit(xhci_plat_exit);
  254. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  255. MODULE_LICENSE("GPL");