xhci-plat.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include "xhci-mvebu.h"
  22. #include "xhci-rcar.h"
  23. static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  24. {
  25. /*
  26. * As of now platform drivers don't provide MSI support so we ensure
  27. * here that the generic code does not try to make a pci_dev from our
  28. * dev struct in order to setup MSI
  29. */
  30. xhci->quirks |= XHCI_PLAT;
  31. }
  32. /* called during probe() after chip reset completes */
  33. static int xhci_plat_setup(struct usb_hcd *hcd)
  34. {
  35. struct device_node *of_node = hcd->self.controller->of_node;
  36. int ret;
  37. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  38. of_device_is_compatible(of_node, "renesas,xhci-r8a7791")) {
  39. ret = xhci_rcar_init_quirk(hcd);
  40. if (ret)
  41. return ret;
  42. }
  43. return xhci_gen_setup(hcd, xhci_plat_quirks);
  44. }
  45. static int xhci_plat_start(struct usb_hcd *hcd)
  46. {
  47. struct device_node *of_node = hcd->self.controller->of_node;
  48. if (of_device_is_compatible(of_node, "renesas,xhci-r8a7790") ||
  49. of_device_is_compatible(of_node, "renesas,xhci-r8a7791"))
  50. xhci_rcar_start(hcd);
  51. return xhci_run(hcd);
  52. }
  53. static const struct hc_driver xhci_plat_xhci_driver = {
  54. .description = "xhci-hcd",
  55. .product_desc = "xHCI Host Controller",
  56. .hcd_priv_size = sizeof(struct xhci_hcd *),
  57. /*
  58. * generic hardware linkage
  59. */
  60. .irq = xhci_irq,
  61. .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
  62. /*
  63. * basic lifecycle operations
  64. */
  65. .reset = xhci_plat_setup,
  66. .start = xhci_plat_start,
  67. .stop = xhci_stop,
  68. .shutdown = xhci_shutdown,
  69. /*
  70. * managing i/o requests and associated device resources
  71. */
  72. .urb_enqueue = xhci_urb_enqueue,
  73. .urb_dequeue = xhci_urb_dequeue,
  74. .alloc_dev = xhci_alloc_dev,
  75. .free_dev = xhci_free_dev,
  76. .alloc_streams = xhci_alloc_streams,
  77. .free_streams = xhci_free_streams,
  78. .add_endpoint = xhci_add_endpoint,
  79. .drop_endpoint = xhci_drop_endpoint,
  80. .endpoint_reset = xhci_endpoint_reset,
  81. .check_bandwidth = xhci_check_bandwidth,
  82. .reset_bandwidth = xhci_reset_bandwidth,
  83. .address_device = xhci_address_device,
  84. .enable_device = xhci_enable_device,
  85. .update_hub_device = xhci_update_hub_device,
  86. .reset_device = xhci_discover_or_reset_device,
  87. /*
  88. * scheduling support
  89. */
  90. .get_frame_number = xhci_get_frame,
  91. /* Root hub support */
  92. .hub_control = xhci_hub_control,
  93. .hub_status_data = xhci_hub_status_data,
  94. .bus_suspend = xhci_bus_suspend,
  95. .bus_resume = xhci_bus_resume,
  96. .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
  97. .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
  98. };
  99. static int xhci_plat_probe(struct platform_device *pdev)
  100. {
  101. struct device_node *node = pdev->dev.of_node;
  102. struct usb_xhci_pdata *pdata = dev_get_platdata(&pdev->dev);
  103. const struct hc_driver *driver;
  104. struct xhci_hcd *xhci;
  105. struct resource *res;
  106. struct usb_hcd *hcd;
  107. struct clk *clk;
  108. int ret;
  109. int irq;
  110. if (usb_disabled())
  111. return -ENODEV;
  112. driver = &xhci_plat_xhci_driver;
  113. irq = platform_get_irq(pdev, 0);
  114. if (irq < 0)
  115. return -ENODEV;
  116. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. if (!res)
  118. return -ENODEV;
  119. if (of_device_is_compatible(pdev->dev.of_node,
  120. "marvell,armada-375-xhci") ||
  121. of_device_is_compatible(pdev->dev.of_node,
  122. "marvell,armada-380-xhci")) {
  123. ret = xhci_mvebu_mbus_init_quirk(pdev);
  124. if (ret)
  125. return ret;
  126. }
  127. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  128. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  129. if (ret)
  130. return ret;
  131. if (!pdev->dev.dma_mask)
  132. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  133. else
  134. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  135. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  136. if (!hcd)
  137. return -ENOMEM;
  138. hcd->rsrc_start = res->start;
  139. hcd->rsrc_len = resource_size(res);
  140. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  141. if (IS_ERR(hcd->regs)) {
  142. ret = PTR_ERR(hcd->regs);
  143. goto put_hcd;
  144. }
  145. /*
  146. * Not all platforms have a clk so it is not an error if the
  147. * clock does not exists.
  148. */
  149. clk = devm_clk_get(&pdev->dev, NULL);
  150. if (!IS_ERR(clk)) {
  151. ret = clk_prepare_enable(clk);
  152. if (ret)
  153. goto put_hcd;
  154. }
  155. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  156. if (ret)
  157. goto disable_clk;
  158. device_wakeup_enable(hcd->self.controller);
  159. /* USB 2.0 roothub is stored in the platform_device now. */
  160. hcd = platform_get_drvdata(pdev);
  161. xhci = hcd_to_xhci(hcd);
  162. xhci->clk = clk;
  163. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  164. dev_name(&pdev->dev), hcd);
  165. if (!xhci->shared_hcd) {
  166. ret = -ENOMEM;
  167. goto dealloc_usb2_hcd;
  168. }
  169. if ((node && of_property_read_bool(node, "usb3-lpm-capable")) ||
  170. (pdata && pdata->usb3_lpm_capable))
  171. xhci->quirks |= XHCI_LPM_SUPPORT;
  172. /*
  173. * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
  174. * is called by usb_add_hcd().
  175. */
  176. *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
  177. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  178. xhci->shared_hcd->can_do_streams = 1;
  179. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  180. if (ret)
  181. goto put_usb3_hcd;
  182. return 0;
  183. put_usb3_hcd:
  184. usb_put_hcd(xhci->shared_hcd);
  185. dealloc_usb2_hcd:
  186. usb_remove_hcd(hcd);
  187. disable_clk:
  188. if (!IS_ERR(clk))
  189. clk_disable_unprepare(clk);
  190. put_hcd:
  191. usb_put_hcd(hcd);
  192. return ret;
  193. }
  194. static int xhci_plat_remove(struct platform_device *dev)
  195. {
  196. struct usb_hcd *hcd = platform_get_drvdata(dev);
  197. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  198. struct clk *clk = xhci->clk;
  199. usb_remove_hcd(xhci->shared_hcd);
  200. usb_put_hcd(xhci->shared_hcd);
  201. usb_remove_hcd(hcd);
  202. if (!IS_ERR(clk))
  203. clk_disable_unprepare(clk);
  204. usb_put_hcd(hcd);
  205. kfree(xhci);
  206. return 0;
  207. }
  208. #ifdef CONFIG_PM_SLEEP
  209. static int xhci_plat_suspend(struct device *dev)
  210. {
  211. struct usb_hcd *hcd = dev_get_drvdata(dev);
  212. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  213. return xhci_suspend(xhci);
  214. }
  215. static int xhci_plat_resume(struct device *dev)
  216. {
  217. struct usb_hcd *hcd = dev_get_drvdata(dev);
  218. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  219. return xhci_resume(xhci, 0);
  220. }
  221. static const struct dev_pm_ops xhci_plat_pm_ops = {
  222. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  223. };
  224. #define DEV_PM_OPS (&xhci_plat_pm_ops)
  225. #else
  226. #define DEV_PM_OPS NULL
  227. #endif /* CONFIG_PM */
  228. #ifdef CONFIG_OF
  229. static const struct of_device_id usb_xhci_of_match[] = {
  230. { .compatible = "generic-xhci" },
  231. { .compatible = "xhci-platform" },
  232. { .compatible = "marvell,armada-375-xhci"},
  233. { .compatible = "marvell,armada-380-xhci"},
  234. { .compatible = "renesas,xhci-r8a7790"},
  235. { .compatible = "renesas,xhci-r8a7791"},
  236. { },
  237. };
  238. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  239. #endif
  240. static struct platform_driver usb_xhci_driver = {
  241. .probe = xhci_plat_probe,
  242. .remove = xhci_plat_remove,
  243. .driver = {
  244. .name = "xhci-hcd",
  245. .pm = DEV_PM_OPS,
  246. .of_match_table = of_match_ptr(usb_xhci_of_match),
  247. },
  248. };
  249. MODULE_ALIAS("platform:xhci-hcd");
  250. int xhci_register_plat(void)
  251. {
  252. return platform_driver_register(&usb_xhci_driver);
  253. }
  254. void xhci_unregister_plat(void)
  255. {
  256. platform_driver_unregister(&usb_xhci_driver);
  257. }