xhci-plat.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/usb/xhci_pdriver.h>
  20. #include "xhci.h"
  21. #ifdef CONFIG_USB_XHCI_MTK
  22. #include <xhci-mtk.h>
  23. #endif
  24. #include "xhci-mvebu.h"
  25. #include "xhci-rcar.h"
  26. #ifdef CONFIG_SSUSB_MTK_XHCI
  27. #include "xhci-ssusb-mtk.h"
  28. #endif
  29. static struct hc_driver __read_mostly xhci_plat_hc_driver;
  30. static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  31. {
  32. /*
  33. * As of now platform drivers don't provide MSI support so we ensure
  34. * here that the generic code does not try to make a pci_dev from our
  35. * dev struct in order to setup MSI
  36. */
  37. xhci->quirks |= XHCI_PLAT;
  38. #ifdef CONFIG_USB_XHCI_MTK
  39. /*
  40. * MTK host controller gives a spurious successful event after a
  41. * short transfer. Ignore it.
  42. */
  43. xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
  44. xhci->quirks |= XHCI_LPM_SUPPORT;
  45. #endif
  46. }
  47. /* called during probe() after chip reset completes */
  48. static int xhci_plat_setup(struct usb_hcd *hcd)
  49. {
  50. struct device_node *of_node = hcd->self.controller->of_node;
  51. int ret;
  52. #ifdef CONFIG_SSUSB_MTK_XHCI
  53. struct xhci_hcd *xhci;
  54. #endif
  55. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  56. of_device_is_compatible(of_node, "renesas,xhci-r8a7791")) {
  57. ret = xhci_rcar_init_quirk(hcd);
  58. if (ret)
  59. return ret;
  60. }
  61. #ifdef CONFIG_SSUSB_MTK_XHCI
  62. ret = xhci_gen_setup(hcd, xhci_plat_quirks);
  63. if (ret)
  64. return ret;
  65. if (!usb_hcd_is_primary_hcd(hcd))
  66. return 0;
  67. xhci = hcd_to_xhci(hcd);
  68. ret = xhci_mtk_init_quirk(xhci);
  69. if (ret) {
  70. kfree(xhci);
  71. return ret;
  72. }
  73. return ret;
  74. #else
  75. return xhci_gen_setup(hcd, xhci_plat_quirks);
  76. #endif
  77. }
  78. static int xhci_plat_start(struct usb_hcd *hcd)
  79. {
  80. struct device_node *of_node = hcd->self.controller->of_node;
  81. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  82. of_device_is_compatible(of_node, "renesas,xhci-r8a7791"))
  83. xhci_rcar_start(hcd);
  84. return xhci_run(hcd);
  85. }
  86. #if defined(CONFIG_MTK_LM_MODE)
  87. #define XHCI_DMA_BIT_MASK DMA_BIT_MASK(64)
  88. #else
  89. #define XHCI_DMA_BIT_MASK DMA_BIT_MASK(32)
  90. #endif
  91. static u64 xhci_dma_mask = XHCI_DMA_BIT_MASK;
  92. static void xhci_hcd_release(struct device *dev)
  93. {
  94. ;
  95. }
  96. static int xhci_plat_probe(struct platform_device *pdev)
  97. {
  98. struct device_node *node = pdev->dev.of_node;
  99. struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev);
  100. const struct hc_driver *driver;
  101. struct xhci_hcd *xhci;
  102. struct resource *res;
  103. struct usb_hcd *hcd;
  104. struct clk *clk;
  105. int ret;
  106. int irq;
  107. if (usb_disabled())
  108. return -ENODEV;
  109. driver = &xhci_plat_hc_driver;
  110. #ifdef CONFIG_USB_XHCI_MTK /* device tree support */
  111. irq = platform_get_irq_byname(pdev, XHCI_DRIVER_NAME);
  112. if (irq < 0)
  113. return -ENODEV;
  114. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, XHCI_BASE_REGS_ADDR_RES_NAME);
  115. if (!res)
  116. return -ENODEV;
  117. pdev->dev.coherent_dma_mask = XHCI_DMA_BIT_MASK;
  118. pdev->dev.dma_mask = &xhci_dma_mask;
  119. pdev->dev.release = xhci_hcd_release;
  120. #else
  121. irq = platform_get_irq(pdev, 0);
  122. if (irq < 0)
  123. return -ENODEV;
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. if (!res)
  126. return -ENODEV;
  127. #endif
  128. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  129. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  130. if (ret)
  131. return ret;
  132. if (!pdev->dev.dma_mask)
  133. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  134. else
  135. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  136. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  137. if (!hcd)
  138. return -ENOMEM;
  139. hcd->rsrc_start = res->start;
  140. hcd->rsrc_len = resource_size(res);
  141. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  142. if (IS_ERR(hcd->regs)) {
  143. ret = PTR_ERR(hcd->regs);
  144. goto put_hcd;
  145. }
  146. /*
  147. * Not all platforms have a clk so it is not an error if the
  148. * clock does not exists.
  149. */
  150. clk = devm_clk_get(&pdev->dev, NULL);
  151. if (!IS_ERR(clk)) {
  152. ret = clk_prepare_enable(clk);
  153. if (ret)
  154. goto put_hcd;
  155. }
  156. if (of_device_is_compatible(pdev->dev.of_node,
  157. "marvell,armada-375-xhci") ||
  158. of_device_is_compatible(pdev->dev.of_node,
  159. "marvell,armada-380-xhci")) {
  160. ret = xhci_mvebu_mbus_init_quirk(pdev);
  161. if (ret)
  162. goto disable_clk;
  163. }
  164. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  165. if (ret)
  166. goto disable_clk;
  167. device_wakeup_enable(hcd->self.controller);
  168. /* USB 2.0 roothub is stored in the platform_device now. */
  169. hcd = platform_get_drvdata(pdev);
  170. xhci = hcd_to_xhci(hcd);
  171. xhci->clk = clk;
  172. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  173. dev_name(&pdev->dev), hcd);
  174. if (!xhci->shared_hcd) {
  175. ret = -ENOMEM;
  176. goto dealloc_usb2_hcd;
  177. }
  178. if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
  179. (pdata && pdata->usb3_lpm_capable))
  180. xhci->quirks |= XHCI_LPM_SUPPORT;
  181. /*
  182. * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
  183. * is called by usb_add_hcd().
  184. */
  185. *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
  186. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  187. xhci->shared_hcd->can_do_streams = 1;
  188. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  189. if (ret)
  190. goto put_usb3_hcd;
  191. #ifdef CONFIG_USB_XHCI_MTK
  192. mtk_xhci_vbus_on(pdev);
  193. #endif
  194. return 0;
  195. put_usb3_hcd:
  196. usb_put_hcd(xhci->shared_hcd);
  197. dealloc_usb2_hcd:
  198. usb_remove_hcd(hcd);
  199. disable_clk:
  200. if (!IS_ERR(clk))
  201. clk_disable_unprepare(clk);
  202. put_hcd:
  203. usb_put_hcd(hcd);
  204. return ret;
  205. }
  206. static int xhci_plat_remove(struct platform_device *dev)
  207. {
  208. struct usb_hcd *hcd = platform_get_drvdata(dev);
  209. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  210. struct clk *clk = xhci->clk;
  211. #ifdef CONFIG_USB_XHCI_MTK
  212. mtk_xhci_vbus_off(dev);
  213. #endif
  214. usb_remove_hcd(xhci->shared_hcd);
  215. usb_put_hcd(xhci->shared_hcd);
  216. usb_remove_hcd(hcd);
  217. if (!IS_ERR(clk))
  218. clk_disable_unprepare(clk);
  219. usb_put_hcd(hcd);
  220. #ifdef CONFIG_SSUSB_MTK_XHCI
  221. if (xhci->quirks & XHCI_MTK_HOST)
  222. xhci_mtk_exit_quirk(xhci);
  223. #endif
  224. kfree(xhci);
  225. return 0;
  226. }
  227. #ifdef CONFIG_PM_SLEEP
  228. static int xhci_plat_suspend(struct device *dev)
  229. {
  230. struct usb_hcd *hcd = dev_get_drvdata(dev);
  231. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  232. /*
  233. * xhci_suspend() needs `do_wakeup` to know whether host is allowed
  234. * to do wakeup during suspend. Since xhci_plat_suspend is currently
  235. * only designed for system suspend, device_may_wakeup() is enough
  236. * to dertermine whether host is allowed to do wakeup. Need to
  237. * reconsider this when xhci_plat_suspend enlarges its scope, e.g.,
  238. * also applies to runtime suspend.
  239. */
  240. return xhci_suspend(xhci, device_may_wakeup(dev));
  241. }
  242. static int xhci_plat_resume(struct device *dev)
  243. {
  244. struct usb_hcd *hcd = dev_get_drvdata(dev);
  245. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  246. return xhci_resume(xhci, 0);
  247. }
  248. static const struct dev_pm_ops xhci_plat_pm_ops = {
  249. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  250. };
  251. #define DEV_PM_OPS (&xhci_plat_pm_ops)
  252. #else
  253. #define DEV_PM_OPS NULL
  254. #endif /* CONFIG_PM */
  255. #ifdef CONFIG_OF
  256. static const struct of_device_id usb_xhci_of_match[] = {
  257. { .compatible = "generic-xhci" },
  258. { .compatible = "xhci-platform" },
  259. { .compatible = "marvell,armada-375-xhci"},
  260. { .compatible = "marvell,armada-380-xhci"},
  261. { .compatible = "renesas,xhci-r8a7790"},
  262. { .compatible = "renesas,xhci-r8a7791"},
  263. { .compatible = "mediatek,usb3_xhci"},
  264. { },
  265. };
  266. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  267. #endif
  268. static struct platform_driver usb_xhci_driver = {
  269. .probe = xhci_plat_probe,
  270. .remove = xhci_plat_remove,
  271. .driver = {
  272. .name = "xhci-hcd",
  273. .pm = DEV_PM_OPS,
  274. .of_match_table = of_match_ptr(usb_xhci_of_match),
  275. },
  276. };
  277. MODULE_ALIAS("platform:xhci-hcd");
  278. #ifdef CONFIG_USB_XHCI_MTK
  279. int xhci_register_plat(void)
  280. {
  281. xhci_init_driver(&xhci_plat_hc_driver, xhci_plat_setup);
  282. xhci_plat_hc_driver.start = xhci_plat_start;
  283. return platform_driver_register(&usb_xhci_driver);
  284. }
  285. void xhci_unregister_plat(void)
  286. {
  287. platform_driver_unregister(&usb_xhci_driver);
  288. }
  289. #else
  290. static int __init xhci_plat_init(void)
  291. {
  292. xhci_init_driver(&xhci_plat_hc_driver, xhci_plat_setup);
  293. xhci_plat_hc_driver.start = xhci_plat_start;
  294. return platform_driver_register(&usb_xhci_driver);
  295. }
  296. module_init(xhci_plat_init);
  297. static void __exit xhci_plat_exit(void)
  298. {
  299. platform_driver_unregister(&usb_xhci_driver);
  300. }
  301. module_exit(xhci_plat_exit);
  302. #endif
  303. MODULE_DESCRIPTION("xHCI Platform Host Controller Driver");
  304. MODULE_LICENSE("GPL");