xhci-plat.c 11 KB

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