xhci-plat.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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-plat.h"
  24. #include "xhci-mvebu.h"
  25. #include "xhci-rcar.h"
  26. static struct hc_driver __read_mostly xhci_plat_hc_driver;
  27. static int xhci_plat_setup(struct usb_hcd *hcd);
  28. static int xhci_plat_start(struct usb_hcd *hcd);
  29. static const struct xhci_driver_overrides xhci_plat_overrides __initconst = {
  30. .extra_priv_size = sizeof(struct xhci_plat_priv),
  31. .reset = xhci_plat_setup,
  32. .start = xhci_plat_start,
  33. };
  34. static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  35. {
  36. /*
  37. * As of now platform drivers don't provide MSI support so we ensure
  38. * here that the generic code does not try to make a pci_dev from our
  39. * dev struct in order to setup MSI
  40. */
  41. xhci->quirks |= XHCI_PLAT;
  42. }
  43. /* called during probe() after chip reset completes */
  44. static int xhci_plat_setup(struct usb_hcd *hcd)
  45. {
  46. int ret;
  47. if (xhci_plat_type_is(hcd, XHCI_PLAT_TYPE_RENESAS_RCAR_GEN2) ||
  48. xhci_plat_type_is(hcd, XHCI_PLAT_TYPE_RENESAS_RCAR_GEN3)) {
  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. if (xhci_plat_type_is(hcd, XHCI_PLAT_TYPE_RENESAS_RCAR_GEN2) ||
  58. xhci_plat_type_is(hcd, XHCI_PLAT_TYPE_RENESAS_RCAR_GEN3))
  59. xhci_rcar_start(hcd);
  60. return xhci_run(hcd);
  61. }
  62. #ifdef CONFIG_OF
  63. static const struct xhci_plat_priv xhci_plat_marvell_armada = {
  64. .type = XHCI_PLAT_TYPE_MARVELL_ARMADA,
  65. };
  66. static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
  67. .type = XHCI_PLAT_TYPE_RENESAS_RCAR_GEN2,
  68. .firmware_name = XHCI_RCAR_FIRMWARE_NAME_V1,
  69. };
  70. static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
  71. .type = XHCI_PLAT_TYPE_RENESAS_RCAR_GEN3,
  72. .firmware_name = XHCI_RCAR_FIRMWARE_NAME_V2,
  73. };
  74. static const struct of_device_id usb_xhci_of_match[] = {
  75. {
  76. .compatible = "generic-xhci",
  77. }, {
  78. .compatible = "xhci-platform",
  79. }, {
  80. .compatible = "marvell,armada-375-xhci",
  81. .data = &xhci_plat_marvell_armada,
  82. }, {
  83. .compatible = "marvell,armada-380-xhci",
  84. .data = &xhci_plat_marvell_armada,
  85. }, {
  86. .compatible = "renesas,xhci-r8a7790",
  87. .data = &xhci_plat_renesas_rcar_gen2,
  88. }, {
  89. .compatible = "renesas,xhci-r8a7791",
  90. .data = &xhci_plat_renesas_rcar_gen2,
  91. }, {
  92. .compatible = "renesas,xhci-r8a7793",
  93. .data = &xhci_plat_renesas_rcar_gen2,
  94. }, {
  95. .compatible = "renesas,xhci-r8a7795",
  96. .data = &xhci_plat_renesas_rcar_gen3,
  97. }, {
  98. .compatible = "renesas,rcar-gen2-xhci",
  99. .data = &xhci_plat_renesas_rcar_gen2,
  100. }, {
  101. .compatible = "renesas,rcar-gen3-xhci",
  102. .data = &xhci_plat_renesas_rcar_gen3,
  103. },
  104. {},
  105. };
  106. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  107. #endif
  108. static int xhci_plat_probe(struct platform_device *pdev)
  109. {
  110. struct device_node *node = pdev->dev.of_node;
  111. struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev);
  112. const struct of_device_id *match;
  113. const struct hc_driver *driver;
  114. struct xhci_hcd *xhci;
  115. struct resource *res;
  116. struct usb_hcd *hcd;
  117. struct clk *clk;
  118. int ret;
  119. int irq;
  120. if (usb_disabled())
  121. return -ENODEV;
  122. driver = &xhci_plat_hc_driver;
  123. irq = platform_get_irq(pdev, 0);
  124. if (irq < 0)
  125. return -ENODEV;
  126. /* Try to set 64-bit DMA first */
  127. if (WARN_ON(!pdev->dev.dma_mask))
  128. /* Platform did not initialize dma_mask */
  129. ret = dma_coerce_mask_and_coherent(&pdev->dev,
  130. DMA_BIT_MASK(64));
  131. else
  132. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  133. /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
  134. if (ret) {
  135. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  136. if (ret)
  137. return ret;
  138. }
  139. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  140. if (!hcd)
  141. return -ENOMEM;
  142. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  143. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  144. if (IS_ERR(hcd->regs)) {
  145. ret = PTR_ERR(hcd->regs);
  146. goto put_hcd;
  147. }
  148. hcd->rsrc_start = res->start;
  149. hcd->rsrc_len = resource_size(res);
  150. /*
  151. * Not all platforms have a clk so it is not an error if the
  152. * clock does not exists.
  153. */
  154. clk = devm_clk_get(&pdev->dev, NULL);
  155. if (!IS_ERR(clk)) {
  156. ret = clk_prepare_enable(clk);
  157. if (ret)
  158. goto put_hcd;
  159. }
  160. xhci = hcd_to_xhci(hcd);
  161. match = of_match_node(usb_xhci_of_match, node);
  162. if (match) {
  163. const struct xhci_plat_priv *priv_match = match->data;
  164. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  165. /* Just copy data for now */
  166. if (priv_match)
  167. *priv = *priv_match;
  168. }
  169. if (xhci_plat_type_is(hcd, XHCI_PLAT_TYPE_MARVELL_ARMADA)) {
  170. ret = xhci_mvebu_mbus_init_quirk(pdev);
  171. if (ret)
  172. goto disable_clk;
  173. }
  174. device_wakeup_enable(hcd->self.controller);
  175. xhci->clk = clk;
  176. xhci->main_hcd = hcd;
  177. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  178. dev_name(&pdev->dev), hcd);
  179. if (!xhci->shared_hcd) {
  180. ret = -ENOMEM;
  181. goto disable_clk;
  182. }
  183. if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
  184. (pdata && pdata->usb3_lpm_capable))
  185. xhci->quirks |= XHCI_LPM_SUPPORT;
  186. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  187. xhci->shared_hcd->can_do_streams = 1;
  188. hcd->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
  189. if (IS_ERR(hcd->usb_phy)) {
  190. ret = PTR_ERR(hcd->usb_phy);
  191. if (ret == -EPROBE_DEFER)
  192. goto put_usb3_hcd;
  193. hcd->usb_phy = NULL;
  194. } else {
  195. ret = usb_phy_init(hcd->usb_phy);
  196. if (ret)
  197. goto put_usb3_hcd;
  198. }
  199. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  200. if (ret)
  201. goto disable_usb_phy;
  202. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  203. if (ret)
  204. goto dealloc_usb2_hcd;
  205. return 0;
  206. dealloc_usb2_hcd:
  207. usb_remove_hcd(hcd);
  208. disable_usb_phy:
  209. usb_phy_shutdown(hcd->usb_phy);
  210. put_usb3_hcd:
  211. usb_put_hcd(xhci->shared_hcd);
  212. disable_clk:
  213. if (!IS_ERR(clk))
  214. clk_disable_unprepare(clk);
  215. put_hcd:
  216. usb_put_hcd(hcd);
  217. return ret;
  218. }
  219. static int xhci_plat_remove(struct platform_device *dev)
  220. {
  221. struct usb_hcd *hcd = platform_get_drvdata(dev);
  222. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  223. struct clk *clk = xhci->clk;
  224. usb_remove_hcd(xhci->shared_hcd);
  225. usb_phy_shutdown(hcd->usb_phy);
  226. usb_remove_hcd(hcd);
  227. usb_put_hcd(xhci->shared_hcd);
  228. if (!IS_ERR(clk))
  229. clk_disable_unprepare(clk);
  230. usb_put_hcd(hcd);
  231. return 0;
  232. }
  233. #ifdef CONFIG_PM_SLEEP
  234. static int xhci_plat_suspend(struct device *dev)
  235. {
  236. struct usb_hcd *hcd = dev_get_drvdata(dev);
  237. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  238. /*
  239. * xhci_suspend() needs `do_wakeup` to know whether host is allowed
  240. * to do wakeup during suspend. Since xhci_plat_suspend is currently
  241. * only designed for system suspend, device_may_wakeup() is enough
  242. * to dertermine whether host is allowed to do wakeup. Need to
  243. * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
  244. * also applies to runtime suspend.
  245. */
  246. return xhci_suspend(xhci, device_may_wakeup(dev));
  247. }
  248. static int xhci_plat_resume(struct device *dev)
  249. {
  250. struct usb_hcd *hcd = dev_get_drvdata(dev);
  251. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  252. return xhci_resume(xhci, 0);
  253. }
  254. static const struct dev_pm_ops xhci_plat_pm_ops = {
  255. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  256. };
  257. #define DEV_PM_OPS (&xhci_plat_pm_ops)
  258. #else
  259. #define DEV_PM_OPS NULL
  260. #endif /* CONFIG_PM */
  261. static const struct acpi_device_id usb_xhci_acpi_match[] = {
  262. /* XHCI-compliant USB Controller */
  263. { "PNP0D10", },
  264. { }
  265. };
  266. MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
  267. static struct platform_driver usb_xhci_driver = {
  268. .probe = xhci_plat_probe,
  269. .remove = xhci_plat_remove,
  270. .driver = {
  271. .name = "xhci-hcd",
  272. .pm = DEV_PM_OPS,
  273. .of_match_table = of_match_ptr(usb_xhci_of_match),
  274. .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
  275. },
  276. };
  277. MODULE_ALIAS("platform:xhci-hcd");
  278. static int __init xhci_plat_init(void)
  279. {
  280. xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
  281. return platform_driver_register(&usb_xhci_driver);
  282. }
  283. module_init(xhci_plat_init);
  284. static void __exit xhci_plat_exit(void)
  285. {
  286. platform_driver_unregister(&usb_xhci_driver);
  287. }
  288. module_exit(xhci_plat_exit);
  289. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  290. MODULE_LICENSE("GPL");