xhci-plat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 - https://www.ti.com
  6. * Author: Sebastian Andrzej Siewior <[email protected]>
  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_suspend_quirk(struct usb_hcd *hcd)
  47. {
  48. struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
  49. if (!priv->suspend_quirk)
  50. return 0;
  51. return priv->suspend_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. .init_quirk = xhci_mvebu_a3700_init_quirk,
  90. };
  91. static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
  92. SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
  93. };
  94. static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
  95. SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
  96. };
  97. static const struct xhci_plat_priv xhci_plat_brcm = {
  98. .quirks = XHCI_RESET_ON_RESUME | XHCI_SUSPEND_RESUME_CLKS,
  99. };
  100. static const struct of_device_id usb_xhci_of_match[] = {
  101. {
  102. .compatible = "generic-xhci",
  103. }, {
  104. .compatible = "xhci-platform",
  105. }, {
  106. .compatible = "marvell,armada-375-xhci",
  107. .data = &xhci_plat_marvell_armada,
  108. }, {
  109. .compatible = "marvell,armada-380-xhci",
  110. .data = &xhci_plat_marvell_armada,
  111. }, {
  112. .compatible = "marvell,armada3700-xhci",
  113. .data = &xhci_plat_marvell_armada3700,
  114. }, {
  115. .compatible = "renesas,xhci-r8a7790",
  116. .data = &xhci_plat_renesas_rcar_gen2,
  117. }, {
  118. .compatible = "renesas,xhci-r8a7791",
  119. .data = &xhci_plat_renesas_rcar_gen2,
  120. }, {
  121. .compatible = "renesas,xhci-r8a7793",
  122. .data = &xhci_plat_renesas_rcar_gen2,
  123. }, {
  124. .compatible = "renesas,xhci-r8a7795",
  125. .data = &xhci_plat_renesas_rcar_gen3,
  126. }, {
  127. .compatible = "renesas,xhci-r8a7796",
  128. .data = &xhci_plat_renesas_rcar_gen3,
  129. }, {
  130. .compatible = "renesas,rcar-gen2-xhci",
  131. .data = &xhci_plat_renesas_rcar_gen2,
  132. }, {
  133. .compatible = "renesas,rcar-gen3-xhci",
  134. .data = &xhci_plat_renesas_rcar_gen3,
  135. }, {
  136. .compatible = "brcm,xhci-brcm-v2",
  137. .data = &xhci_plat_brcm,
  138. }, {
  139. .compatible = "brcm,bcm7445-xhci",
  140. .data = &xhci_plat_brcm,
  141. },
  142. {},
  143. };
  144. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  145. #endif
  146. static struct xhci_plat_priv_overwrite xhci_plat_vendor_overwrite;
  147. int xhci_plat_register_vendor_ops(struct xhci_vendor_ops *vendor_ops)
  148. {
  149. if (vendor_ops == NULL)
  150. return -EINVAL;
  151. xhci_plat_vendor_overwrite.vendor_ops = vendor_ops;
  152. return 0;
  153. }
  154. EXPORT_SYMBOL_GPL(xhci_plat_register_vendor_ops);
  155. static int xhci_vendor_init(struct xhci_hcd *xhci)
  156. {
  157. struct xhci_vendor_ops *ops = NULL;
  158. if (xhci_plat_vendor_overwrite.vendor_ops)
  159. ops = xhci->vendor_ops = xhci_plat_vendor_overwrite.vendor_ops;
  160. if (ops && ops->vendor_init)
  161. return ops->vendor_init(xhci);
  162. return 0;
  163. }
  164. static void xhci_vendor_cleanup(struct xhci_hcd *xhci)
  165. {
  166. struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
  167. if (ops && ops->vendor_cleanup)
  168. ops->vendor_cleanup(xhci);
  169. xhci->vendor_ops = NULL;
  170. }
  171. static int xhci_plat_probe(struct platform_device *pdev)
  172. {
  173. const struct xhci_plat_priv *priv_match;
  174. const struct hc_driver *driver;
  175. struct device *sysdev, *tmpdev;
  176. struct xhci_hcd *xhci;
  177. struct resource *res;
  178. struct usb_hcd *hcd, *usb3_hcd;
  179. int ret;
  180. int irq;
  181. struct xhci_plat_priv *priv = NULL;
  182. bool of_match;
  183. if (usb_disabled())
  184. return -ENODEV;
  185. driver = &xhci_plat_hc_driver;
  186. irq = platform_get_irq(pdev, 0);
  187. if (irq < 0)
  188. return irq;
  189. /*
  190. * sysdev must point to a device that is known to the system firmware
  191. * or PCI hardware. We handle these three cases here:
  192. * 1. xhci_plat comes from firmware
  193. * 2. xhci_plat is child of a device from firmware (dwc3-plat)
  194. * 3. xhci_plat is grandchild of a pci device (dwc3-pci)
  195. */
  196. for (sysdev = &pdev->dev; sysdev; sysdev = sysdev->parent) {
  197. if (is_of_node(sysdev->fwnode) ||
  198. is_acpi_device_node(sysdev->fwnode))
  199. break;
  200. #ifdef CONFIG_PCI
  201. else if (sysdev->bus == &pci_bus_type)
  202. break;
  203. #endif
  204. }
  205. if (!sysdev)
  206. sysdev = &pdev->dev;
  207. if (WARN_ON(!sysdev->dma_mask))
  208. /* Platform did not initialize dma_mask */
  209. ret = dma_coerce_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
  210. else
  211. ret = dma_set_mask_and_coherent(sysdev, DMA_BIT_MASK(64));
  212. if (ret)
  213. return ret;
  214. pm_runtime_set_active(&pdev->dev);
  215. pm_runtime_enable(&pdev->dev);
  216. pm_runtime_get_noresume(&pdev->dev);
  217. hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
  218. dev_name(&pdev->dev), NULL);
  219. if (!hcd) {
  220. ret = -ENOMEM;
  221. goto disable_runtime;
  222. }
  223. hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  224. if (IS_ERR(hcd->regs)) {
  225. ret = PTR_ERR(hcd->regs);
  226. goto put_hcd;
  227. }
  228. hcd->rsrc_start = res->start;
  229. hcd->rsrc_len = resource_size(res);
  230. xhci = hcd_to_xhci(hcd);
  231. xhci->allow_single_roothub = 1;
  232. /*
  233. * Not all platforms have clks so it is not an error if the
  234. * clock do not exist.
  235. */
  236. xhci->reg_clk = devm_clk_get_optional(&pdev->dev, "reg");
  237. if (IS_ERR(xhci->reg_clk)) {
  238. ret = PTR_ERR(xhci->reg_clk);
  239. goto put_hcd;
  240. }
  241. ret = clk_prepare_enable(xhci->reg_clk);
  242. if (ret)
  243. goto put_hcd;
  244. xhci->clk = devm_clk_get_optional(&pdev->dev, NULL);
  245. if (IS_ERR(xhci->clk)) {
  246. ret = PTR_ERR(xhci->clk);
  247. goto disable_reg_clk;
  248. }
  249. ret = clk_prepare_enable(xhci->clk);
  250. if (ret)
  251. goto disable_reg_clk;
  252. if (pdev->dev.of_node)
  253. priv_match = of_device_get_match_data(&pdev->dev);
  254. else
  255. priv_match = dev_get_platdata(&pdev->dev);
  256. if (priv_match) {
  257. priv = hcd_to_xhci_priv(hcd);
  258. /* Just copy data for now */
  259. *priv = *priv_match;
  260. }
  261. device_set_wakeup_capable(&pdev->dev, true);
  262. xhci->main_hcd = hcd;
  263. /* imod_interval is the interrupt moderation value in nanoseconds. */
  264. xhci->imod_interval = 40000;
  265. /* Iterate over all parent nodes for finding quirks */
  266. for (tmpdev = &pdev->dev; tmpdev; tmpdev = tmpdev->parent) {
  267. if (device_property_read_bool(tmpdev, "usb2-lpm-disable"))
  268. xhci->quirks |= XHCI_HW_LPM_DISABLE;
  269. if (device_property_read_bool(tmpdev, "usb3-lpm-capable"))
  270. xhci->quirks |= XHCI_LPM_SUPPORT;
  271. if (device_property_read_bool(tmpdev, "quirk-broken-port-ped"))
  272. xhci->quirks |= XHCI_BROKEN_PORT_PED;
  273. device_property_read_u32(tmpdev, "imod-interval-ns",
  274. &xhci->imod_interval);
  275. }
  276. /*
  277. * Drivers such as dwc3 manages PHYs themself (and rely on driver name
  278. * matching for the xhci platform device).
  279. */
  280. of_match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
  281. if (of_match) {
  282. hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
  283. if (IS_ERR(hcd->usb_phy)) {
  284. ret = PTR_ERR(hcd->usb_phy);
  285. if (ret == -EPROBE_DEFER)
  286. goto disable_clk;
  287. hcd->usb_phy = NULL;
  288. } else {
  289. ret = usb_phy_init(hcd->usb_phy);
  290. if (ret)
  291. goto disable_clk;
  292. }
  293. }
  294. ret = xhci_vendor_init(xhci);
  295. if (ret)
  296. goto disable_usb_phy;
  297. hcd->tpl_support = of_usb_host_tpl_support(sysdev->of_node);
  298. if (priv && (priv->quirks & XHCI_SKIP_PHY_INIT))
  299. hcd->skip_phy_initialization = 1;
  300. if (priv && (priv->quirks & XHCI_SG_TRB_CACHE_SIZE_QUIRK))
  301. xhci->quirks |= XHCI_SG_TRB_CACHE_SIZE_QUIRK;
  302. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  303. if (ret)
  304. goto disable_usb_phy;
  305. if (!xhci_has_one_roothub(xhci)) {
  306. xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
  307. dev_name(&pdev->dev), hcd);
  308. if (!xhci->shared_hcd) {
  309. ret = -ENOMEM;
  310. goto dealloc_usb2_hcd;
  311. }
  312. xhci->shared_hcd->tpl_support = hcd->tpl_support;
  313. }
  314. usb3_hcd = xhci_get_usb3_hcd(xhci);
  315. if (usb3_hcd && HCC_MAX_PSA(xhci->hcc_params) >= 4)
  316. usb3_hcd->can_do_streams = 1;
  317. if (xhci->shared_hcd) {
  318. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  319. if (ret)
  320. goto put_usb3_hcd;
  321. }
  322. device_enable_async_suspend(&pdev->dev);
  323. pm_runtime_put_noidle(&pdev->dev);
  324. /*
  325. * Prevent runtime pm from being on as default, users should enable
  326. * runtime pm using power/control in sysfs.
  327. */
  328. pm_runtime_forbid(&pdev->dev);
  329. return 0;
  330. put_usb3_hcd:
  331. usb_put_hcd(xhci->shared_hcd);
  332. dealloc_usb2_hcd:
  333. usb_remove_hcd(hcd);
  334. disable_usb_phy:
  335. usb_phy_shutdown(hcd->usb_phy);
  336. disable_clk:
  337. clk_disable_unprepare(xhci->clk);
  338. disable_reg_clk:
  339. clk_disable_unprepare(xhci->reg_clk);
  340. put_hcd:
  341. usb_put_hcd(hcd);
  342. disable_runtime:
  343. pm_runtime_put_noidle(&pdev->dev);
  344. pm_runtime_disable(&pdev->dev);
  345. return ret;
  346. }
  347. static int xhci_plat_remove(struct platform_device *dev)
  348. {
  349. struct usb_hcd *hcd = platform_get_drvdata(dev);
  350. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  351. struct clk *clk = xhci->clk;
  352. struct clk *reg_clk = xhci->reg_clk;
  353. struct usb_hcd *shared_hcd = xhci->shared_hcd;
  354. xhci->xhc_state |= XHCI_STATE_REMOVING;
  355. pm_runtime_get_sync(&dev->dev);
  356. if (shared_hcd) {
  357. usb_remove_hcd(shared_hcd);
  358. xhci->shared_hcd = NULL;
  359. }
  360. usb_phy_shutdown(hcd->usb_phy);
  361. usb_remove_hcd(hcd);
  362. if (shared_hcd)
  363. usb_put_hcd(shared_hcd);
  364. xhci_vendor_cleanup(xhci);
  365. clk_disable_unprepare(clk);
  366. clk_disable_unprepare(reg_clk);
  367. usb_put_hcd(hcd);
  368. pm_runtime_disable(&dev->dev);
  369. pm_runtime_put_noidle(&dev->dev);
  370. pm_runtime_set_suspended(&dev->dev);
  371. return 0;
  372. }
  373. static int __maybe_unused xhci_plat_suspend(struct device *dev)
  374. {
  375. struct usb_hcd *hcd = dev_get_drvdata(dev);
  376. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  377. int ret;
  378. if (pm_runtime_suspended(dev))
  379. pm_runtime_resume(dev);
  380. ret = xhci_priv_suspend_quirk(hcd);
  381. if (ret)
  382. return ret;
  383. /*
  384. * xhci_suspend() needs `do_wakeup` to know whether host is allowed
  385. * to do wakeup during suspend.
  386. */
  387. ret = xhci_suspend(xhci, device_may_wakeup(dev));
  388. if (ret)
  389. return ret;
  390. if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
  391. clk_disable_unprepare(xhci->clk);
  392. clk_disable_unprepare(xhci->reg_clk);
  393. }
  394. return 0;
  395. }
  396. static int __maybe_unused xhci_plat_resume(struct device *dev)
  397. {
  398. struct usb_hcd *hcd = dev_get_drvdata(dev);
  399. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  400. int ret;
  401. if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
  402. ret = clk_prepare_enable(xhci->clk);
  403. if (ret)
  404. return ret;
  405. ret = clk_prepare_enable(xhci->reg_clk);
  406. if (ret) {
  407. clk_disable_unprepare(xhci->clk);
  408. return ret;
  409. }
  410. }
  411. ret = xhci_priv_resume_quirk(hcd);
  412. if (ret)
  413. goto disable_clks;
  414. ret = xhci_resume(xhci, 0);
  415. if (ret)
  416. goto disable_clks;
  417. pm_runtime_disable(dev);
  418. pm_runtime_set_active(dev);
  419. pm_runtime_enable(dev);
  420. return 0;
  421. disable_clks:
  422. if (!device_may_wakeup(dev) && (xhci->quirks & XHCI_SUSPEND_RESUME_CLKS)) {
  423. clk_disable_unprepare(xhci->clk);
  424. clk_disable_unprepare(xhci->reg_clk);
  425. }
  426. return ret;
  427. }
  428. static int __maybe_unused xhci_plat_runtime_suspend(struct device *dev)
  429. {
  430. struct usb_hcd *hcd = dev_get_drvdata(dev);
  431. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  432. int ret;
  433. ret = xhci_priv_suspend_quirk(hcd);
  434. if (ret)
  435. return ret;
  436. return xhci_suspend(xhci, true);
  437. }
  438. static int __maybe_unused xhci_plat_runtime_resume(struct device *dev)
  439. {
  440. struct usb_hcd *hcd = dev_get_drvdata(dev);
  441. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  442. return xhci_resume(xhci, 0);
  443. }
  444. static const struct dev_pm_ops xhci_plat_pm_ops = {
  445. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  446. SET_RUNTIME_PM_OPS(xhci_plat_runtime_suspend,
  447. xhci_plat_runtime_resume,
  448. NULL)
  449. };
  450. #ifdef CONFIG_ACPI
  451. static const struct acpi_device_id usb_xhci_acpi_match[] = {
  452. /* XHCI-compliant USB Controller */
  453. { "PNP0D10", },
  454. { }
  455. };
  456. MODULE_DEVICE_TABLE(acpi, usb_xhci_acpi_match);
  457. #endif
  458. static struct platform_driver usb_xhci_driver = {
  459. .probe = xhci_plat_probe,
  460. .remove = xhci_plat_remove,
  461. .shutdown = usb_hcd_platform_shutdown,
  462. .driver = {
  463. .name = "xhci-hcd",
  464. .pm = &xhci_plat_pm_ops,
  465. .of_match_table = of_match_ptr(usb_xhci_of_match),
  466. .acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
  467. },
  468. };
  469. MODULE_ALIAS("platform:xhci-hcd");
  470. static int __init xhci_plat_init(void)
  471. {
  472. xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides);
  473. return platform_driver_register(&usb_xhci_driver);
  474. }
  475. module_init(xhci_plat_init);
  476. static void __exit xhci_plat_exit(void)
  477. {
  478. platform_driver_unregister(&usb_xhci_driver);
  479. }
  480. module_exit(xhci_plat_exit);
  481. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  482. MODULE_LICENSE("GPL");