xhci-plat.c 11 KB

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