xhci-plat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xhci-plat.c - xHCI host controller driver platform Bus Glue.
  4. *
  5. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  6. * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  7. *
  8. * A lot of code borrowed from the Linux xHCI driver.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/usb/phy.h>
  18. #include <linux/slab.h>
  19. #include <linux/acpi.h>
  20. #include <linux/usb/of.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_plat_setup(struct usb_hcd *hcd)
  40. {
  41. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  42. if (!priv->plat_setup)
  43. return 0;
  44. return priv->plat_setup(hcd);
  45. }
  46. static int xhci_priv_init_quirk(struct usb_hcd *hcd)
  47. {
  48. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  49. if (!priv->init_quirk)
  50. return 0;
  51. return priv->init_quirk(hcd);
  52. }
  53. static int xhci_priv_resume_quirk(struct usb_hcd *hcd)
  54. {
  55. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  56. if (!priv->resume_quirk)
  57. return 0;
  58. return priv->resume_quirk(hcd);
  59. }
  60. static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  61. {
  62. struct xhci_plat_priv *priv = xhci_to_priv(xhci);
  63. /*
  64. * As of now platform drivers don't provide MSI support so we ensure
  65. * here that the generic code does not try to make a pci_dev from our
  66. * dev struct in order to setup MSI
  67. */
  68. xhci->quirks |= XHCI_PLAT | priv->quirks;
  69. }
  70. /* called during probe() after chip reset completes */
  71. static int xhci_plat_setup(struct usb_hcd *hcd)
  72. {
  73. int ret;
  74. ret = xhci_priv_init_quirk(hcd);
  75. if (ret)
  76. return ret;
  77. return xhci_gen_setup(hcd, xhci_plat_quirks);
  78. }
  79. static int xhci_plat_start(struct usb_hcd *hcd)
  80. {
  81. xhci_priv_plat_start(hcd);
  82. return xhci_run(hcd);
  83. }
  84. #ifdef CONFIG_OF
  85. static const struct xhci_plat_priv xhci_plat_marvell_armada = {
  86. .init_quirk = xhci_mvebu_mbus_init_quirk,
  87. };
  88. static const struct xhci_plat_priv xhci_plat_marvell_armada3700 = {
  89. .plat_setup = xhci_mvebu_a3700_plat_setup,
  90. .init_quirk = xhci_mvebu_a3700_init_quirk,
  91. };
  92. static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
  93. SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
  94. };
  95. static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
  96. SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
  97. };
  98. static const struct of_device_id usb_xhci_of_match[] = {
  99. {
  100. .compatible = "generic-xhci",
  101. }, {
  102. .compatible = "xhci-platform",
  103. }, {
  104. .compatible = "marvell,armada-375-xhci",
  105. .data = &xhci_plat_marvell_armada,
  106. }, {
  107. .compatible = "marvell,armada-380-xhci",
  108. .data = &xhci_plat_marvell_armada,
  109. }, {
  110. .compatible = "marvell,armada3700-xhci",
  111. .data = &xhci_plat_marvell_armada3700,
  112. }, {
  113. .compatible = "renesas,xhci-r8a7790",
  114. .data = &xhci_plat_renesas_rcar_gen2,
  115. }, {
  116. .compatible = "renesas,xhci-r8a7791",
  117. .data = &xhci_plat_renesas_rcar_gen2,
  118. }, {
  119. .compatible = "renesas,xhci-r8a7793",
  120. .data = &xhci_plat_renesas_rcar_gen2,
  121. }, {
  122. .compatible = "renesas,xhci-r8a7795",
  123. .data = &xhci_plat_renesas_rcar_gen3,
  124. }, {
  125. .compatible = "renesas,xhci-r8a7796",
  126. .data = &xhci_plat_renesas_rcar_gen3,
  127. }, {
  128. .compatible = "renesas,rcar-gen2-xhci",
  129. .data = &xhci_plat_renesas_rcar_gen2,
  130. }, {
  131. .compatible = "renesas,rcar-gen3-xhci",
  132. .data = &xhci_plat_renesas_rcar_gen3,
  133. },
  134. {},
  135. };
  136. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  137. #endif
  138. static int xhci_plat_probe(struct platform_device *pdev)
  139. {
  140. const struct xhci_plat_priv *priv_match;
  141. const struct hc_driver *driver;
  142. struct device *sysdev, *tmpdev;
  143. struct xhci_hcd *xhci;
  144. struct resource *res;
  145. struct usb_hcd *hcd;
  146. int ret;
  147. int irq;
  148. struct xhci_plat_priv *priv = NULL;
  149. if (usb_disabled())
  150. return -ENODEV;
  151. driver = &xhci_plat_hc_driver;
  152. irq = platform_get_irq(pdev, 0);
  153. if (irq < 0)
  154. return irq;
  155. /*
  156. * sysdev must point to a device that is known to the system firmware
  157. * or PCI hardware. We handle these three cases here:
  158. * 1. xhci_plat comes from firmware
  159. * 2. xhci_plat is child of a device from firmware (dwc3-plat)
  160. * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
  161. */
  162. for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
  163. if (is_of_node(sysdev->fwnode) ||
  164. is_acpi_device_node(sysdev->fwnode))
  165. break;
  166. #ifdef CONFIG_PCI
  167. else if (sysdev->bus == &pci_bus_type)
  168. break;
  169. #endif
  170. }
  171. if (!sysdev)
  172. sysdev = &pdev->dev;
  173. /* Try to set 64-bit DMA first */
  174. if (WARN_ON(!sysdev->dma_mask))
  175. /* Platform did not initialize dma_mask */
  176. ret = dma_coerce_mask_and_coherent(sysdev,
  177. DMA_BIT_MASK(64));
  178. else
  179. ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
  180. /* If seting 64-bit DMA mask fails, fall back to 32-bit DMA mask */
  181. if (ret) {
  182. ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(32));
  183. if (ret)
  184. return ret;
  185. }
  186. pm_runtime_set_active(&pdev->dev);
  187. pm_runtime_enable(&pdev->dev);
  188. pm_runtime_get_noresume(&pdev->dev);
  189. hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
  190. dev_name(&pdev->dev), NULL);
  191. if (!hcd) {
  192. ret = -ENOMEM;
  193. goto disable_runtime;
  194. }
  195. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  196. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  197. if (IS_ERR(hcd->regs)) {
  198. ret = PTR_ERR(hcd->regs);
  199. goto put_hcd;
  200. }
  201. hcd->rsrc_start = res->start;
  202. hcd->rsrc_len = resource_size(res);
  203. xhci = hcd_to_xhci(hcd);
  204. /*
  205. * Not all platforms have clks so it is not an error if the
  206. * clock do not exist.
  207. */
  208. xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
  209. if (IS_ERR(xhci->reg_clk)) {
  210. ret = PTR_ERR(xhci->reg_clk);
  211. goto put_hcd;
  212. }
  213. ret = clk_prepare_enable(xhci->reg_clk);
  214. if (ret)
  215. goto put_hcd;
  216. xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
  217. if (IS_ERR(xhci->clk)) {
  218. ret = PTR_ERR(xhci->clk);
  219. goto disable_reg_clk;
  220. }
  221. ret = clk_prepare_enable(xhci->clk);
  222. if (ret)
  223. goto disable_reg_clk;
  224. priv_match = of_device_get_match_data(&pdev->dev);
  225. if (priv_match) {
  226. priv = hcd_to_xhci_priv(hcd);
  227. /* Just copy data for now */
  228. if (priv_match)
  229. *priv = *priv_match;
  230. }
  231. device_wakeup_enable(hcd->self.controller);
  232. xhci->main_hcd = hcd;
  233. xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
  234. dev_name(&pdev->dev), hcd);
  235. if (!xhci->shared_hcd) {
  236. ret = -ENOMEM;
  237. goto disable_clk;
  238. }
  239. /* imod_interval is the interrupt moderation value in nanoseconds. */
  240. xhci->imod_interval = 40000;
  241. /* Iterate over all parent nodes for finding quirks */
  242. for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
  243. if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
  244. xhci->quirks |= XHCI_HW_LPM_DISABLE;
  245. if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
  246. xhci->quirks |= XHCI_LPM_SUPPORT;
  247. if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
  248. xhci->quirks |= XHCI_BROKEN_PORT_PED;
  249. device_property_read_u32(tmpdev, "imod-interval-ns",
  250. &xhci->imod_interval);
  251. }
  252. hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
  253. if (IS_ERR(hcd->usb_phy)) {
  254. ret = PTR_ERR(hcd->usb_phy);
  255. if (ret == -EPROBE_DEFER)
  256. goto put_usb3_hcd;
  257. hcd->usb_phy = NULL;
  258. } else {
  259. ret = usb_phy_init(hcd->usb_phy);
  260. if (ret)
  261. goto put_usb3_hcd;
  262. }
  263. hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
  264. xhci->shared_hcd->tpl_support = hcd->tpl_support;
  265. if (priv) {
  266. ret = xhci_priv_plat_setup(hcd);
  267. if (ret)
  268. goto disable_usb_phy;
  269. }
  270. if ((xhci->quirks & XHCI_SKIP_PHY_INIT) || (priv && (priv->quirks & XHCI_SKIP_PHY_INIT)))
  271. hcd->skip_phy_initialization = 1;
  272. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  273. if (ret)
  274. goto disable_usb_phy;
  275. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  276. xhci->shared_hcd->can_do_streams = 1;
  277. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  278. if (ret)
  279. goto dealloc_usb2_hcd;
  280. device_enable_async_suspend(&pdev->dev);
  281. pm_runtime_put_noidle(&pdev->dev);
  282. /*
  283. * Prevent runtime pm from being on as default, users should enable
  284. * runtime pm using power/control in sysfs.
  285. */
  286. pm_runtime_forbid(&pdev->dev);
  287. return 0;
  288. dealloc_usb2_hcd:
  289. usb_remove_hcd(hcd);
  290. disable_usb_phy:
  291. usb_phy_shutdown(hcd->usb_phy);
  292. put_usb3_hcd:
  293. usb_put_hcd(xhci->shared_hcd);
  294. disable_clk:
  295. clk_disable_unprepare(xhci->clk);
  296. disable_reg_clk:
  297. clk_disable_unprepare(xhci->reg_clk);
  298. put_hcd:
  299. usb_put_hcd(hcd);
  300. disable_runtime:
  301. pm_runtime_put_noidle(&pdev->dev);
  302. pm_runtime_disable(&pdev->dev);
  303. return ret;
  304. }
  305. static int xhci_plat_remove(struct platform_device *dev)
  306. {
  307. struct usb_hcd *hcd = platform_get_drvdata(dev);
  308. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  309. struct clk *clk = xhci->clk;
  310. struct clk *reg_clk = xhci->reg_clk;
  311. struct usb_hcd *shared_hcd = xhci->shared_hcd;
  312. pm_runtime_get_sync(&dev->dev);
  313. xhci->xhc_state |= XHCI_STATE_REMOVING;
  314. usb_remove_hcd(shared_hcd);
  315. xhci->shared_hcd = NULL;
  316. usb_phy_shutdown(hcd->usb_phy);
  317. usb_remove_hcd(hcd);
  318. usb_put_hcd(shared_hcd);
  319. clk_disable_unprepare(clk);
  320. clk_disable_unprepare(reg_clk);
  321. usb_put_hcd(hcd);
  322. pm_runtime_disable(&dev->dev);
  323. pm_runtime_put_noidle(&dev->dev);
  324. pm_runtime_set_suspended(&dev->dev);
  325. return 0;
  326. }
  327. static int __maybe_unused xhci_plat_suspend(struct device *dev)
  328. {
  329. struct usb_hcd *hcd = dev_get_drvdata(dev);
  330. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  331. /*
  332. * xhci_suspend() needs `do_wakeup` to know whether host is allowed
  333. * to do wakeup during suspend. Since xhci_plat_suspend is currently
  334. * only designed for system suspend, device_may_wakeup() is enough
  335. * to dertermine whether host is allowed to do wakeup. Need to
  336. * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
  337. * also applies to runtime suspend.
  338. */
  339. return xhci_suspend(xhci, device_may_wakeup(dev));
  340. }
  341. static int __maybe_unused xhci_plat_resume(struct device *dev)
  342. {
  343. struct usb_hcd *hcd = dev_get_drvdata(dev);
  344. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  345. int ret;
  346. ret = xhci_priv_resume_quirk(hcd);
  347. if (ret)
  348. return ret;
  349. return xhci_resume(xhci, 0);
  350. }
  351. static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
  352. {
  353. struct usb_hcd *hcd = dev_get_drvdata(dev);
  354. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  355. return xhci_suspend(xhci, true);
  356. }
  357. static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
  358. {
  359. struct usb_hcd *hcd = dev_get_drvdata(dev);
  360. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  361. return xhci_resume(xhci, 0);
  362. }
  363. static const struct dev_pm_ops xhci_plat_pm_ops = {
  364. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  365. SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
  366. xhci_plat_runtime_resume,
  367. NULL)
  368. };
  369. static const struct acpi_device_id usb_xhci_acpi_match[] = {
  370. /* XHCI-compliant USB Controller */
  371. { "PNP0D10", },
  372. { }
  373. };
  374. MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
  375. static struct platform_driver usb_xhci_driver = {
  376. .probe = xhci_plat_probe,
  377. .remove = xhci_plat_remove,
  378. .shutdown = usb_hcd_platform_shutdown,
  379. .driver = {
  380. .name = "xhci-hcd",
  381. .pm = &xhci_plat_pm_ops,
  382. .of_match_table = of_match_ptr(usb_xhci_of_match),
  383. .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
  384. },
  385. };
  386. MODULE_ALIAS("platform:xhci-hcd");
  387. static int __init xhci_plat_init(void)
  388. {
  389. xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
  390. return platform_driver_register(&usb_xhci_driver);
  391. }
  392. module_init(xhci_plat_init);
  393. static void __exit xhci_plat_exit(void)
  394. {
  395. platform_driver_unregister(&usb_xhci_driver);
  396. }
  397. module_exit(xhci_plat_exit);
  398. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  399. MODULE_LICENSE("GPL");