xhci-plat.c 6.9 KB

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