xhci-plat.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  26. {
  27. /*
  28. * As of now platform drivers don't provide MSI support so we ensure
  29. * here that the generic code does not try to make a pci_dev from our
  30. * dev struct in order to setup MSI
  31. */
  32. xhci->quirks |= XHCI_PLAT;
  33. }
  34. /* called during probe() after chip reset completes */
  35. static int xhci_plat_setup(struct usb_hcd *hcd)
  36. {
  37. struct device_node *of_node = hcd->self.controller->of_node;
  38. int ret;
  39. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  40. of_device_is_compatible(of_node, "renesas,xhci-r8a7791")) {
  41. ret = xhci_rcar_init_quirk(hcd);
  42. if (ret)
  43. return ret;
  44. }
  45. return xhci_gen_setup(hcd, xhci_plat_quirks);
  46. }
  47. static int xhci_plat_start(struct usb_hcd *hcd)
  48. {
  49. struct device_node *of_node = hcd->self.controller->of_node;
  50. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  51. of_device_is_compatible(of_node, "renesas,xhci-r8a7791"))
  52. xhci_rcar_start(hcd);
  53. return xhci_run(hcd);
  54. }
  55. static int xhci_plat_probe(struct platform_device *pdev)
  56. {
  57. struct device_node *node = pdev->dev.of_node;
  58. struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev);
  59. const struct hc_driver *driver;
  60. struct xhci_hcd *xhci;
  61. struct resource *res;
  62. struct usb_hcd *hcd;
  63. struct clk *clk;
  64. int ret;
  65. int irq;
  66. if (usb_disabled())
  67. return -ENODEV;
  68. driver = &xhci_plat_hc_driver;
  69. irq = platform_get_irq(pdev, 0);
  70. if (irq < 0)
  71. return -ENODEV;
  72. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  73. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  74. if (ret)
  75. return ret;
  76. if (!pdev->dev.dma_mask)
  77. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  78. else
  79. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  80. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  81. if (!hcd)
  82. return -ENOMEM;
  83. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  84. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  85. if (IS_ERR(hcd->regs)) {
  86. ret = PTR_ERR(hcd->regs);
  87. goto put_hcd;
  88. }
  89. hcd->rsrc_start = res->start;
  90. hcd->rsrc_len = resource_size(res);
  91. /*
  92. * Not all platforms have a clk so it is not an error if the
  93. * clock does not exists.
  94. */
  95. clk = devm_clk_get(&pdev->dev, NULL);
  96. if (!IS_ERR(clk)) {
  97. ret = clk_prepare_enable(clk);
  98. if (ret)
  99. goto put_hcd;
  100. }
  101. if (of_device_is_compatible(pdev->dev.of_node,
  102. "marvell,armada-375-xhci") ||
  103. of_device_is_compatible(pdev->dev.of_node,
  104. "marvell,armada-380-xhci")) {
  105. ret = xhci_mvebu_mbus_init_quirk(pdev);
  106. if (ret)
  107. goto disable_clk;
  108. }
  109. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  110. if (ret)
  111. goto disable_clk;
  112. device_wakeup_enable(hcd->self.controller);
  113. /* USB 2.0 roothub is stored in the platform_device now. */
  114. hcd = platform_get_drvdata(pdev);
  115. xhci = hcd_to_xhci(hcd);
  116. xhci->clk = clk;
  117. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  118. dev_name(&pdev->dev), hcd);
  119. if (!xhci->shared_hcd) {
  120. ret = -ENOMEM;
  121. goto dealloc_usb2_hcd;
  122. }
  123. if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
  124. (pdata && pdata->usb3_lpm_capable))
  125. xhci->quirks |= XHCI_LPM_SUPPORT;
  126. /*
  127. * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
  128. * is called by usb_add_hcd().
  129. */
  130. *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
  131. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  132. xhci->shared_hcd->can_do_streams = 1;
  133. hcd->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
  134. if (IS_ERR(hcd->usb_phy)) {
  135. ret = PTR_ERR(hcd->usb_phy);
  136. if (ret == -EPROBE_DEFER)
  137. goto put_usb3_hcd;
  138. hcd->usb_phy = NULL;
  139. } else {
  140. ret = usb_phy_init(hcd->usb_phy);
  141. if (ret)
  142. goto put_usb3_hcd;
  143. }
  144. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  145. if (ret)
  146. goto disable_usb_phy;
  147. return 0;
  148. disable_usb_phy:
  149. usb_phy_shutdown(hcd->usb_phy);
  150. put_usb3_hcd:
  151. usb_put_hcd(xhci->shared_hcd);
  152. dealloc_usb2_hcd:
  153. usb_remove_hcd(hcd);
  154. disable_clk:
  155. if (!IS_ERR(clk))
  156. clk_disable_unprepare(clk);
  157. put_hcd:
  158. usb_put_hcd(hcd);
  159. return ret;
  160. }
  161. static int xhci_plat_remove(struct platform_device *dev)
  162. {
  163. struct usb_hcd *hcd = platform_get_drvdata(dev);
  164. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  165. struct clk *clk = xhci->clk;
  166. usb_remove_hcd(xhci->shared_hcd);
  167. usb_phy_shutdown(hcd->usb_phy);
  168. usb_put_hcd(xhci->shared_hcd);
  169. usb_remove_hcd(hcd);
  170. if (!IS_ERR(clk))
  171. clk_disable_unprepare(clk);
  172. usb_put_hcd(hcd);
  173. kfree(xhci);
  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_setup);
  229. xhci_plat_hc_driver.start = xhci_plat_start;
  230. return platform_driver_register(&usb_xhci_driver);
  231. }
  232. module_init(xhci_plat_init);
  233. static void __exit xhci_plat_exit(void)
  234. {
  235. platform_driver_unregister(&usb_xhci_driver);
  236. }
  237. module_exit(xhci_plat_exit);
  238. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  239. MODULE_LICENSE("GPL");