xhci-plat.c 14 KB

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