xhci-plat.c 5.8 KB

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