xhci-plat.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/pm_runtime.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/usb/otg.h>
  18. #include <linux/usb/msm_hsusb.h>
  19. #include "xhci.h"
  20. #define SYNOPSIS_DWC3_VENDOR 0x5533
  21. static struct usb_phy *phy;
  22. static void xhci_plat_quirks(struct device *dev, struct xhci_hcd *xhci)
  23. {
  24. struct xhci_plat_data *pdata = dev->platform_data;
  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. if (!pdata)
  32. return;
  33. if (pdata->vendor == SYNOPSIS_DWC3_VENDOR && pdata->revision < 0x230A)
  34. xhci->quirks |= XHCI_PORTSC_DELAY;
  35. if (pdata->vendor == SYNOPSIS_DWC3_VENDOR && pdata->revision == 0x250A)
  36. xhci->quirks |= XHCI_RESET_DELAY;
  37. }
  38. /* called during probe() after chip reset completes */
  39. static int xhci_plat_setup(struct usb_hcd *hcd)
  40. {
  41. return xhci_gen_setup(hcd, xhci_plat_quirks);
  42. }
  43. static void xhci_plat_phy_autosuspend(struct usb_hcd *hcd,
  44. int enable_autosuspend)
  45. {
  46. if (!phy || !phy->set_phy_autosuspend)
  47. return;
  48. usb_phy_set_autosuspend(phy, enable_autosuspend);
  49. return;
  50. }
  51. static const struct hc_driver xhci_plat_xhci_driver = {
  52. .description = "xhci-hcd",
  53. .product_desc = "xHCI Host Controller",
  54. .hcd_priv_size = sizeof(struct xhci_hcd *),
  55. /*
  56. * generic hardware linkage
  57. */
  58. .irq = xhci_irq,
  59. .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
  60. /*
  61. * basic lifecycle operations
  62. */
  63. .reset = xhci_plat_setup,
  64. .start = xhci_run,
  65. .stop = xhci_stop,
  66. .shutdown = xhci_shutdown,
  67. /*
  68. * managing i/o requests and associated device resources
  69. */
  70. .urb_enqueue = xhci_urb_enqueue,
  71. .urb_dequeue = xhci_urb_dequeue,
  72. .alloc_dev = xhci_alloc_dev,
  73. .free_dev = xhci_free_dev,
  74. .alloc_streams = xhci_alloc_streams,
  75. .free_streams = xhci_free_streams,
  76. .add_endpoint = xhci_add_endpoint,
  77. .drop_endpoint = xhci_drop_endpoint,
  78. .endpoint_reset = xhci_endpoint_reset,
  79. .check_bandwidth = xhci_check_bandwidth,
  80. .reset_bandwidth = xhci_reset_bandwidth,
  81. .address_device = xhci_address_device,
  82. .update_hub_device = xhci_update_hub_device,
  83. .reset_device = xhci_discover_or_reset_device,
  84. /*
  85. * scheduling support
  86. */
  87. .get_frame_number = xhci_get_frame,
  88. /* Root hub support */
  89. .hub_control = xhci_hub_control,
  90. .hub_status_data = xhci_hub_status_data,
  91. .bus_suspend = xhci_bus_suspend,
  92. .bus_resume = xhci_bus_resume,
  93. .set_autosuspend = xhci_plat_phy_autosuspend,
  94. };
  95. static int xhci_plat_probe(struct platform_device *pdev)
  96. {
  97. const struct hc_driver *driver;
  98. struct xhci_hcd *xhci;
  99. struct resource *res;
  100. struct usb_hcd *hcd;
  101. int ret;
  102. int irq;
  103. if (usb_disabled())
  104. return -ENODEV;
  105. driver = &xhci_plat_xhci_driver;
  106. irq = platform_get_irq(pdev, 0);
  107. if (irq < 0)
  108. return -ENODEV;
  109. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  110. if (!res)
  111. return -ENODEV;
  112. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  113. if (!hcd)
  114. return -ENOMEM;
  115. hcd_to_bus(hcd)->skip_resume = true;
  116. hcd->rsrc_start = res->start;
  117. hcd->rsrc_len = resource_size(res);
  118. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  119. driver->description)) {
  120. dev_dbg(&pdev->dev, "controller already in use\n");
  121. ret = -EBUSY;
  122. goto put_hcd;
  123. }
  124. hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
  125. if (!hcd->regs) {
  126. dev_dbg(&pdev->dev, "error mapping memory\n");
  127. ret = -EFAULT;
  128. goto release_mem_region;
  129. }
  130. pm_runtime_set_active(&pdev->dev);
  131. pm_runtime_enable(&pdev->dev);
  132. pm_runtime_get_sync(&pdev->dev);
  133. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  134. if (ret)
  135. goto unmap_registers;
  136. /* USB 2.0 roothub is stored in the platform_device now. */
  137. hcd = dev_get_drvdata(&pdev->dev);
  138. xhci = hcd_to_xhci(hcd);
  139. xhci->shared_hcd = usb_create_shared_hcd(driver, &pdev->dev,
  140. dev_name(&pdev->dev), hcd);
  141. if (!xhci->shared_hcd) {
  142. ret = -ENOMEM;
  143. goto dealloc_usb2_hcd;
  144. }
  145. hcd_to_bus(xhci->shared_hcd)->skip_resume = true;
  146. /*
  147. * Set the xHCI pointer before xhci_plat_setup() (aka hcd_driver.reset)
  148. * is called by usb_add_hcd().
  149. */
  150. *((struct xhci_hcd **) xhci->shared_hcd->hcd_priv) = xhci;
  151. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  152. if (ret)
  153. goto put_usb3_hcd;
  154. phy = usb_get_transceiver();
  155. /* Register with OTG if present, ignore USB2 OTG using other PHY */
  156. if (phy && phy->otg && !(phy->flags & ENABLE_SECONDARY_PHY)) {
  157. dev_dbg(&pdev->dev, "%s otg support available\n", __func__);
  158. ret = otg_set_host(phy->otg, &hcd->self);
  159. if (ret) {
  160. dev_err(&pdev->dev, "%s otg_set_host failed\n",
  161. __func__);
  162. usb_put_transceiver(phy);
  163. goto put_usb3_hcd;
  164. }
  165. } else {
  166. pm_runtime_no_callbacks(&pdev->dev);
  167. }
  168. pm_runtime_put(&pdev->dev);
  169. return 0;
  170. put_usb3_hcd:
  171. usb_put_hcd(xhci->shared_hcd);
  172. dealloc_usb2_hcd:
  173. usb_remove_hcd(hcd);
  174. unmap_registers:
  175. iounmap(hcd->regs);
  176. release_mem_region:
  177. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  178. put_hcd:
  179. usb_put_hcd(hcd);
  180. return ret;
  181. }
  182. static int xhci_plat_remove(struct platform_device *dev)
  183. {
  184. struct usb_hcd *hcd = platform_get_drvdata(dev);
  185. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  186. usb_remove_hcd(xhci->shared_hcd);
  187. usb_put_hcd(xhci->shared_hcd);
  188. usb_remove_hcd(hcd);
  189. iounmap(hcd->regs);
  190. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  191. usb_put_hcd(hcd);
  192. kfree(xhci);
  193. if (phy && phy->otg) {
  194. otg_set_host(phy->otg, NULL);
  195. usb_put_transceiver(phy);
  196. }
  197. return 0;
  198. }
  199. #ifdef CONFIG_PM_RUNTIME
  200. static int xhci_msm_runtime_idle(struct device *dev)
  201. {
  202. dev_dbg(dev, "xhci msm runtime idle\n");
  203. return 0;
  204. }
  205. static int xhci_msm_runtime_suspend(struct device *dev)
  206. {
  207. dev_dbg(dev, "xhci msm runtime suspend\n");
  208. /*
  209. * Notify OTG about suspend. It takes care of
  210. * putting the hardware in LPM.
  211. */
  212. if (phy)
  213. return usb_phy_set_suspend(phy, 1);
  214. return 0;
  215. }
  216. static int xhci_msm_runtime_resume(struct device *dev)
  217. {
  218. dev_dbg(dev, "xhci msm runtime resume\n");
  219. if (phy)
  220. return usb_phy_set_suspend(phy, 0);
  221. return 0;
  222. }
  223. #endif
  224. #ifdef CONFIG_PM_SLEEP
  225. static int xhci_msm_pm_suspend(struct device *dev)
  226. {
  227. dev_dbg(dev, "xhci-msm PM suspend\n");
  228. if (phy)
  229. return usb_phy_set_suspend(phy, 1);
  230. return 0;
  231. }
  232. static int xhci_msm_pm_resume(struct device *dev)
  233. {
  234. dev_dbg(dev, "xhci-msm PM resume\n");
  235. if (pm_runtime_suspended(dev))
  236. return 0;
  237. if (phy)
  238. return usb_phy_set_suspend(phy, 0);
  239. return 0;
  240. }
  241. #endif
  242. static const struct dev_pm_ops xhci_msm_dev_pm_ops = {
  243. SET_SYSTEM_SLEEP_PM_OPS(xhci_msm_pm_suspend, xhci_msm_pm_resume)
  244. SET_RUNTIME_PM_OPS(xhci_msm_runtime_suspend, xhci_msm_runtime_resume,
  245. xhci_msm_runtime_idle)
  246. };
  247. static struct platform_driver usb_xhci_driver = {
  248. .probe = xhci_plat_probe,
  249. .remove = xhci_plat_remove,
  250. .driver = {
  251. .name = "xhci-hcd",
  252. .pm = &xhci_msm_dev_pm_ops,
  253. },
  254. };
  255. MODULE_ALIAS("platform:xhci-hcd");
  256. int xhci_register_plat(void)
  257. {
  258. return platform_driver_register(&usb_xhci_driver);
  259. }
  260. void xhci_unregister_plat(void)
  261. {
  262. platform_driver_unregister(&usb_xhci_driver);
  263. }