xhci-plat.c 8.7 KB

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