xhci-plat.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. .enable_device = xhci_enable_device,
  65. .update_hub_device = xhci_update_hub_device,
  66. .reset_device = xhci_discover_or_reset_device,
  67. /*
  68. * scheduling support
  69. */
  70. .get_frame_number = xhci_get_frame,
  71. /* Root hub support */
  72. .hub_control = xhci_hub_control,
  73. .hub_status_data = xhci_hub_status_data,
  74. .bus_suspend = xhci_bus_suspend,
  75. .bus_resume = xhci_bus_resume,
  76. };
  77. static int xhci_plat_probe(struct platform_device *pdev)
  78. {
  79. const struct hc_driver *driver;
  80. struct xhci_hcd *xhci;
  81. struct resource *res;
  82. struct usb_hcd *hcd;
  83. int ret;
  84. int irq;
  85. if (usb_disabled())
  86. return -ENODEV;
  87. driver = &xhci_plat_xhci_driver;
  88. irq = platform_get_irq(pdev, 0);
  89. if (irq < 0)
  90. return -ENODEV;
  91. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  92. if (!res)
  93. return -ENODEV;
  94. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  95. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  96. if (ret)
  97. return ret;
  98. if (!pdev->dev.dma_mask)
  99. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  100. else
  101. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  102. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  103. if (!hcd)
  104. return -ENOMEM;
  105. hcd->rsrc_start = res->start;
  106. hcd->rsrc_len = resource_size(res);
  107. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  108. driver->description)) {
  109. dev_dbg(&pdev->dev, "controller already in use\n");
  110. ret = -EBUSY;
  111. goto put_hcd;
  112. }
  113. hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
  114. if (!hcd->regs) {
  115. dev_dbg(&pdev->dev, "error mapping memory\n");
  116. ret = -EFAULT;
  117. goto release_mem_region;
  118. }
  119. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  120. if (ret)
  121. goto unmap_registers;
  122. device_wakeup_enable(hcd->self.controller);
  123. /* USB 2.0 roothub is stored in the platform_device now. */
  124. hcd = platform_get_drvdata(pdev);
  125. xhci = hcd_to_xhci(hcd);
  126. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  127. dev_name(&pdev->dev), hcd);
  128. if (!xhci->shared_hcd) {
  129. ret = -ENOMEM;
  130. goto dealloc_usb2_hcd;
  131. }
  132. /*
  133. * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
  134. * is called by usb_add_hcd().
  135. */
  136. *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
  137. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  138. if (ret)
  139. goto put_usb3_hcd;
  140. return 0;
  141. put_usb3_hcd:
  142. usb_put_hcd(xhci->shared_hcd);
  143. dealloc_usb2_hcd:
  144. usb_remove_hcd(hcd);
  145. unmap_registers:
  146. iounmap(hcd->regs);
  147. release_mem_region:
  148. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  149. put_hcd:
  150. usb_put_hcd(hcd);
  151. return ret;
  152. }
  153. static int xhci_plat_remove(struct platform_device *dev)
  154. {
  155. struct usb_hcd *hcd = platform_get_drvdata(dev);
  156. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  157. usb_remove_hcd(xhci->shared_hcd);
  158. usb_put_hcd(xhci->shared_hcd);
  159. usb_remove_hcd(hcd);
  160. iounmap(hcd->regs);
  161. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  162. usb_put_hcd(hcd);
  163. kfree(xhci);
  164. return 0;
  165. }
  166. #ifdef CONFIG_PM
  167. static int xhci_plat_suspend(struct device *dev)
  168. {
  169. struct usb_hcd *hcd = dev_get_drvdata(dev);
  170. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  171. return xhci_suspend(xhci);
  172. }
  173. static int xhci_plat_resume(struct device *dev)
  174. {
  175. struct usb_hcd *hcd = dev_get_drvdata(dev);
  176. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  177. return xhci_resume(xhci, 0);
  178. }
  179. static const struct dev_pm_ops xhci_plat_pm_ops = {
  180. SET_SYSTEM_SLEEP_PM_OPS(xhci_plat_suspend, xhci_plat_resume)
  181. };
  182. #define DEV_PM_OPS (&xhci_plat_pm_ops)
  183. #else
  184. #define DEV_PM_OPS NULL
  185. #endif /* CONFIG_PM */
  186. #ifdef CONFIG_OF
  187. static const struct of_device_id usb_xhci_of_match[] = {
  188. { .compatible = "xhci-platform" },
  189. { },
  190. };
  191. MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
  192. #endif
  193. static struct platform_driver usb_xhci_driver = {
  194. .probe = xhci_plat_probe,
  195. .remove = xhci_plat_remove,
  196. .driver = {
  197. .name = "xhci-hcd",
  198. .pm = DEV_PM_OPS,
  199. .of_match_table = of_match_ptr(usb_xhci_of_match),
  200. },
  201. };
  202. MODULE_ALIAS("platform:xhci-hcd");
  203. int xhci_register_plat(void)
  204. {
  205. return platform_driver_register(&usb_xhci_driver);
  206. }
  207. void xhci_unregister_plat(void)
  208. {
  209. platform_driver_unregister(&usb_xhci_driver);
  210. }