xhci-plat.c 6.4 KB

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