xhci-plat.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "xhci.h"
  22. #include "xhci-mvebu.h"
  23. #include "xhci-rcar.h"
  24. static struct hc_driver __read_mostly xhci_plat_hc_driver;
  25. static int xhci_plat_setup(struct usb_hcd *hcd);
  26. static int xhci_plat_start(struct usb_hcd *hcd);
  27. static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
  28. .extra_priv_size = sizeof(struct xhci_hcd),
  29. .reset = xhci_plat_setup,
  30. .start = xhci_plat_start,
  31. };
  32. static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  33. {
  34. /*
  35. * As of now platform drivers don't provide MSI support so we ensure
  36. * here that the generic code does not try to make a pci_dev from our
  37. * dev struct in order to setup MSI
  38. */
  39. xhci->quirks |= XHCI_PLAT;
  40. }
  41. /* called during probe() after chip reset completes */
  42. static int xhci_plat_setup(struct usb_hcd *hcd)
  43. {
  44. struct device_node *of_node = hcd->self.controller->of_node;
  45. int ret;
  46. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  47. of_device_is_compatible(of_node, "renesas,xhci-r8a7791")) {
  48. ret = xhci_rcar_init_quirk(hcd);
  49. if (ret)
  50. return ret;
  51. }
  52. return xhci_gen_setup(hcd, xhci_plat_quirks);
  53. }
  54. static int xhci_plat_start(struct usb_hcd *hcd)
  55. {
  56. struct device_node *of_node = hcd->self.controller->of_node;
  57. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  58. of_device_is_compatible(of_node, "renesas,xhci-r8a7791"))
  59. xhci_rcar_start(hcd);
  60. return xhci_run(hcd);
  61. }
  62. static int xhci_plat_probe(struct platform_device *pdev)
  63. {
  64. struct device_node *node = pdev->dev.of_node;
  65. struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev);
  66. const struct hc_driver *driver;
  67. struct xhci_hcd *xhci;
  68. struct resource *res;
  69. struct usb_hcd *hcd;
  70. struct clk *clk;
  71. int ret;
  72. int irq;
  73. if (usb_disabled())
  74. return -ENODEV;
  75. driver = &xhci_plat_hc_driver;
  76. irq = platform_get_irq(pdev, 0);
  77. if (irq < 0)
  78. return -ENODEV;
  79. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  80. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  81. if (ret)
  82. return ret;
  83. if (!pdev->dev.dma_mask)
  84. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  85. else
  86. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  87. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  88. if (!hcd)
  89. return -ENOMEM;
  90. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  91. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  92. if (IS_ERR(hcd->regs)) {
  93. ret = PTR_ERR(hcd->regs);
  94. goto put_hcd;
  95. }
  96. hcd->rsrc_start = res->start;
  97. hcd->rsrc_len = resource_size(res);
  98. /*
  99. * Not all platforms have a clk so it is not an error if the
  100. * clock does not exists.
  101. */
  102. clk = devm_clk_get(&pdev->dev, NULL);
  103. if (!IS_ERR(clk)) {
  104. ret = clk_prepare_enable(clk);
  105. if (ret)
  106. goto put_hcd;
  107. }
  108. if (of_device_is_compatible(pdev->dev.of_node,
  109. "marvell,armada-375-xhci") ||
  110. of_device_is_compatible(pdev->dev.of_node,
  111. "marvell,armada-380-xhci")) {
  112. ret = xhci_mvebu_mbus_init_quirk(pdev);
  113. if (ret)
  114. goto disable_clk;
  115. }
  116. device_wakeup_enable(hcd->self.controller);
  117. xhci = hcd_to_xhci(hcd);
  118. xhci->clk = clk;
  119. xhci->main_hcd = hcd;
  120. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  121. dev_name(&pdev->dev), hcd);
  122. if (!xhci->shared_hcd) {
  123. ret = -ENOMEM;
  124. goto disable_clk;
  125. }
  126. if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
  127. (pdata && pdata->usb3_lpm_capable))
  128. xhci->quirks |= XHCI_LPM_SUPPORT;
  129. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  130. xhci->shared_hcd->can_do_streams = 1;
  131. hcd->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
  132. if (IS_ERR(hcd->usb_phy)) {
  133. ret = PTR_ERR(hcd->usb_phy);
  134. if (ret == -EPROBE_DEFER)
  135. goto put_usb3_hcd;
  136. hcd->usb_phy = NULL;
  137. } else {
  138. ret = usb_phy_init(hcd->usb_phy);
  139. if (ret)
  140. goto put_usb3_hcd;
  141. }
  142. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  143. if (ret)
  144. goto disable_usb_phy;
  145. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  146. if (ret)
  147. goto dealloc_usb2_hcd;
  148. return 0;
  149. dealloc_usb2_hcd:
  150. usb_remove_hcd(hcd);
  151. disable_usb_phy:
  152. usb_phy_shutdown(hcd->usb_phy);
  153. put_usb3_hcd:
  154. usb_put_hcd(xhci->shared_hcd);
  155. disable_clk:
  156. if (!IS_ERR(clk))
  157. clk_disable_unprepare(clk);
  158. put_hcd:
  159. usb_put_hcd(hcd);
  160. return ret;
  161. }
  162. static int xhci_plat_remove(struct platform_device *dev)
  163. {
  164. struct usb_hcd *hcd = platform_get_drvdata(dev);
  165. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  166. struct clk *clk = xhci->clk;
  167. usb_remove_hcd(xhci->shared_hcd);
  168. usb_phy_shutdown(hcd->usb_phy);
  169. usb_remove_hcd(hcd);
  170. usb_put_hcd(xhci->shared_hcd);
  171. if (!IS_ERR(clk))
  172. clk_disable_unprepare(clk);
  173. usb_put_hcd(hcd);
  174. return 0;
  175. }
  176. #ifdef CONFIG_PM_SLEEP
  177. static int xhci_plat_suspend(struct device *dev)
  178. {
  179. struct usb_hcd *hcd = dev_get_drvdata(dev);
  180. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  181. /*
  182. * xhci_suspend() needs `do_wakeup` to know whether host is allowed
  183. * to do wakeup during suspend. Since xhci_plat_suspend is currently
  184. * only designed for system suspend, device_may_wakeup() is enough
  185. * to dertermine whether host is allowed to do wakeup. Need to
  186. * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
  187. * also applies to runtime suspend.
  188. */
  189. return xhci_suspend(xhci, device_may_wakeup(dev));
  190. }
  191. static int xhci_plat_resume(struct device *dev)
  192. {
  193. struct usb_hcd *hcd = dev_get_drvdata(dev);
  194. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  195. return xhci_resume(xhci, 0);
  196. }
  197. static const struct dev_pm_ops xhci_plat_pm_ops = {
  198. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  199. };
  200. #define DEV_PM_OPS (&xhci_plat_pm_ops)
  201. #else
  202. #define DEV_PM_OPS NULL
  203. #endif /* CONFIG_PM */
  204. #ifdef CONFIG_OF
  205. static const struct of_device_id usb_xhci_of_match[] = {
  206. { .compatible = "generic-xhci" },
  207. { .compatible = "xhci-platform" },
  208. { .compatible = "marvell,armada-375-xhci"},
  209. { .compatible = "marvell,armada-380-xhci"},
  210. { .compatible = "renesas,xhci-r8a7790"},
  211. { .compatible = "renesas,xhci-r8a7791"},
  212. { },
  213. };
  214. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  215. #endif
  216. static struct platform_driver usb_xhci_driver = {
  217. .probe = xhci_plat_probe,
  218. .remove = xhci_plat_remove,
  219. .driver = {
  220. .name = "xhci-hcd",
  221. .pm = DEV_PM_OPS,
  222. .of_match_table = of_match_ptr(usb_xhci_of_match),
  223. },
  224. };
  225. MODULE_ALIAS("platform:xhci-hcd");
  226. static int __init xhci_plat_init(void)
  227. {
  228. xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
  229. return platform_driver_register(&usb_xhci_driver);
  230. }
  231. module_init(xhci_plat_init);
  232. static void __exit xhci_plat_exit(void)
  233. {
  234. platform_driver_unregister(&usb_xhci_driver);
  235. }
  236. module_exit(xhci_plat_exit);
  237. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  238. MODULE_LICENSE("GPL");