xhci-plat.c 11 KB

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