xhci-plat.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 of_device_id usb_xhci_of_match[] = {
  84. {
  85. .compatible = "generic-xhci",
  86. }, {
  87. .compatible = "xhci-platform",
  88. }, {
  89. .compatible = "marvell,armada-375-xhci",
  90. .data = &xhci_plat_marvell_armada,
  91. }, {
  92. .compatible = "marvell,armada-380-xhci",
  93. .data = &xhci_plat_marvell_armada,
  94. }, {
  95. .compatible = "renesas,xhci-r8a7790",
  96. .data = &xhci_plat_renesas_rcar_gen2,
  97. }, {
  98. .compatible = "renesas,xhci-r8a7791",
  99. .data = &xhci_plat_renesas_rcar_gen2,
  100. }, {
  101. .compatible = "renesas,xhci-r8a7793",
  102. .data = &xhci_plat_renesas_rcar_gen2,
  103. }, {
  104. .compatible = "renesas,xhci-r8a7795",
  105. .data = &xhci_plat_renesas_rcar_gen3,
  106. }, {
  107. .compatible = "renesas,rcar-gen2-xhci",
  108. .data = &xhci_plat_renesas_rcar_gen2,
  109. }, {
  110. .compatible = "renesas,rcar-gen3-xhci",
  111. .data = &xhci_plat_renesas_rcar_gen3,
  112. },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  116. #endif
  117. static int xhci_plat_probe(struct platform_device *pdev)
  118. {
  119. const struct of_device_id *match;
  120. const struct hc_driver *driver;
  121. struct xhci_hcd *xhci;
  122. struct resource *res;
  123. struct usb_hcd *hcd;
  124. struct clk *clk;
  125. int ret;
  126. int irq;
  127. if (usb_disabled())
  128. return -ENODEV;
  129. driver = &xhci_plat_hc_driver;
  130. irq = platform_get_irq(pdev, 0);
  131. if (irq < 0)
  132. return irq;
  133. /* Try to set 64-bit DMA first */
  134. if (WARN_ON(!pdev->dev.dma_mask))
  135. /* Platform did not initialize dma_mask */
  136. ret = dma_coerce_mask_and_coherent(&pdev->dev,
  137. DMA_BIT_MASK(64));
  138. else
  139. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  140. /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
  141. if (ret) {
  142. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  143. if (ret)
  144. return ret;
  145. }
  146. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  147. if (!hcd)
  148. return -ENOMEM;
  149. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  150. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  151. if (IS_ERR(hcd->regs)) {
  152. ret = PTR_ERR(hcd->regs);
  153. goto put_hcd;
  154. }
  155. hcd->rsrc_start = res->start;
  156. hcd->rsrc_len = resource_size(res);
  157. /*
  158. * Not all platforms have a clk so it is not an error if the
  159. * clock does not exists.
  160. */
  161. clk = devm_clk_get(&pdev->dev, NULL);
  162. if (!IS_ERR(clk)) {
  163. ret = clk_prepare_enable(clk);
  164. if (ret)
  165. goto put_hcd;
  166. } else if (PTR_ERR(clk) == -EPROBE_DEFER) {
  167. ret = -EPROBE_DEFER;
  168. goto put_hcd;
  169. }
  170. xhci = hcd_to_xhci(hcd);
  171. match = of_match_node(usb_xhci_of_match, pdev->dev.of_node);
  172. if (match) {
  173. const struct xhci_plat_priv *priv_match = match->data;
  174. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  175. /* Just copy data for now */
  176. if (priv_match)
  177. *priv = *priv_match;
  178. }
  179. device_wakeup_enable(hcd->self.controller);
  180. xhci->clk = clk;
  181. xhci->main_hcd = hcd;
  182. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  183. dev_name(&pdev->dev), hcd);
  184. if (!xhci->shared_hcd) {
  185. ret = -ENOMEM;
  186. goto disable_clk;
  187. }
  188. if (device_property_read_bool(&pdev->dev, "usb3-lpm-capable"))
  189. xhci->quirks |= XHCI_LPM_SUPPORT;
  190. if (device_property_read_bool(&pdev->dev, "quirk-broken-port-ped"))
  191. xhci->quirks |= XHCI_BROKEN_PORT_PED;
  192. hcd->usb_phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
  193. if (IS_ERR(hcd->usb_phy)) {
  194. ret = PTR_ERR(hcd->usb_phy);
  195. if (ret == -EPROBE_DEFER)
  196. goto put_usb3_hcd;
  197. hcd->usb_phy = NULL;
  198. } else {
  199. ret = usb_phy_init(hcd->usb_phy);
  200. if (ret)
  201. goto put_usb3_hcd;
  202. }
  203. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  204. if (ret)
  205. goto disable_usb_phy;
  206. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  207. xhci->shared_hcd->can_do_streams = 1;
  208. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  209. if (ret)
  210. goto dealloc_usb2_hcd;
  211. return 0;
  212. dealloc_usb2_hcd:
  213. usb_remove_hcd(hcd);
  214. disable_usb_phy:
  215. usb_phy_shutdown(hcd->usb_phy);
  216. put_usb3_hcd:
  217. usb_put_hcd(xhci->shared_hcd);
  218. disable_clk:
  219. if (!IS_ERR(clk))
  220. clk_disable_unprepare(clk);
  221. put_hcd:
  222. usb_put_hcd(hcd);
  223. return ret;
  224. }
  225. static int xhci_plat_remove(struct platform_device *dev)
  226. {
  227. struct usb_hcd *hcd = platform_get_drvdata(dev);
  228. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  229. struct clk *clk = xhci->clk;
  230. xhci->xhc_state |= XHCI_STATE_REMOVING;
  231. usb_remove_hcd(xhci->shared_hcd);
  232. usb_phy_shutdown(hcd->usb_phy);
  233. usb_remove_hcd(hcd);
  234. usb_put_hcd(xhci->shared_hcd);
  235. if (!IS_ERR(clk))
  236. clk_disable_unprepare(clk);
  237. usb_put_hcd(hcd);
  238. return 0;
  239. }
  240. #ifdef CONFIG_PM_SLEEP
  241. static int xhci_plat_suspend(struct device *dev)
  242. {
  243. struct usb_hcd *hcd = dev_get_drvdata(dev);
  244. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  245. /*
  246. * xhci_suspend() needs `do_wakeup` to know whether host is allowed
  247. * to do wakeup during suspend. Since xhci_plat_suspend is currently
  248. * only designed for system suspend, device_may_wakeup() is enough
  249. * to dertermine whether host is allowed to do wakeup. Need to
  250. * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
  251. * also applies to runtime suspend.
  252. */
  253. return xhci_suspend(xhci, device_may_wakeup(dev));
  254. }
  255. static int xhci_plat_resume(struct device *dev)
  256. {
  257. struct usb_hcd *hcd = dev_get_drvdata(dev);
  258. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  259. return xhci_resume(xhci, 0);
  260. }
  261. static const struct dev_pm_ops xhci_plat_pm_ops = {
  262. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  263. };
  264. #define DEV_PM_OPS (&xhci_plat_pm_ops)
  265. #else
  266. #define DEV_PM_OPS NULL
  267. #endif /* CONFIG_PM */
  268. static const struct acpi_device_id usb_xhci_acpi_match[] = {
  269. /* XHCI-compliant USB Controller */
  270. { "PNP0D10", },
  271. { }
  272. };
  273. MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
  274. static struct platform_driver usb_xhci_driver = {
  275. .probe = xhci_plat_probe,
  276. .remove = xhci_plat_remove,
  277. .driver = {
  278. .name = "xhci-hcd",
  279. .pm = DEV_PM_OPS,
  280. .of_match_table = of_match_ptr(usb_xhci_of_match),
  281. .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
  282. },
  283. };
  284. MODULE_ALIAS("platform:xhci-hcd");
  285. static int __init xhci_plat_init(void)
  286. {
  287. xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
  288. return platform_driver_register(&usb_xhci_driver);
  289. }
  290. module_init(xhci_plat_init);
  291. static void __exit xhci_plat_exit(void)
  292. {
  293. platform_driver_unregister(&usb_xhci_driver);
  294. }
  295. module_exit(xhci_plat_exit);
  296. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  297. MODULE_LICENSE("GPL");