dwc3-pci.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * dwc3-pci.c - PCI Specific glue layer
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/slab.h>
  41. #include <linux/pci.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/usb/otg.h>
  44. #include <linux/usb/nop-usb-xceiv.h>
  45. /* FIXME define these in <linux/pci_ids.h> */
  46. #define PCI_VENDOR_ID_SYNOPSYS 0x16c3
  47. #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
  48. struct dwc3_pci {
  49. struct device *dev;
  50. struct platform_device *dwc3;
  51. struct platform_device *usb2_phy;
  52. struct platform_device *usb3_phy;
  53. };
  54. static int dwc3_pci_register_phys(struct dwc3_pci *glue)
  55. {
  56. struct nop_usb_xceiv_platform_data pdata;
  57. struct platform_device *pdev;
  58. int ret;
  59. memset(&pdata, 0x00, sizeof(pdata));
  60. pdev = platform_device_alloc("nop_usb_xceiv", 0);
  61. if (!pdev)
  62. return -ENOMEM;
  63. glue->usb2_phy = pdev;
  64. pdata.type = USB_PHY_TYPE_USB2;
  65. ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
  66. if (ret)
  67. goto err1;
  68. pdev = platform_device_alloc("nop_usb_xceiv", 1);
  69. if (!pdev) {
  70. ret = -ENOMEM;
  71. goto err1;
  72. }
  73. glue->usb3_phy = pdev;
  74. pdata.type = USB_PHY_TYPE_USB3;
  75. ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
  76. if (ret)
  77. goto err2;
  78. ret = platform_device_add(glue->usb2_phy);
  79. if (ret)
  80. goto err2;
  81. ret = platform_device_add(glue->usb3_phy);
  82. if (ret)
  83. goto err3;
  84. return 0;
  85. err3:
  86. platform_device_del(glue->usb2_phy);
  87. err2:
  88. platform_device_put(glue->usb3_phy);
  89. err1:
  90. platform_device_put(glue->usb2_phy);
  91. return ret;
  92. }
  93. static int dwc3_pci_probe(struct pci_dev *pci,
  94. const struct pci_device_id *id)
  95. {
  96. struct resource res[2];
  97. struct platform_device *dwc3;
  98. struct dwc3_pci *glue;
  99. int ret = -ENOMEM;
  100. struct device *dev = &pci->dev;
  101. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  102. if (!glue) {
  103. dev_err(dev, "not enough memory\n");
  104. return -ENOMEM;
  105. }
  106. glue->dev = dev;
  107. ret = pci_enable_device(pci);
  108. if (ret) {
  109. dev_err(dev, "failed to enable pci device\n");
  110. return -ENODEV;
  111. }
  112. pci_set_master(pci);
  113. ret = dwc3_pci_register_phys(glue);
  114. if (ret) {
  115. dev_err(dev, "couldn't register PHYs\n");
  116. return ret;
  117. }
  118. dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
  119. if (!dwc3) {
  120. dev_err(dev, "couldn't allocate dwc3 device\n");
  121. ret = -ENOMEM;
  122. goto err1;
  123. }
  124. memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
  125. res[0].start = pci_resource_start(pci, 0);
  126. res[0].end = pci_resource_end(pci, 0);
  127. res[0].name = "dwc_usb3";
  128. res[0].flags = IORESOURCE_MEM;
  129. res[1].start = pci->irq;
  130. res[1].name = "dwc_usb3";
  131. res[1].flags = IORESOURCE_IRQ;
  132. ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
  133. if (ret) {
  134. dev_err(dev, "couldn't add resources to dwc3 device\n");
  135. goto err1;
  136. }
  137. pci_set_drvdata(pci, glue);
  138. dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
  139. dwc3->dev.dma_mask = dev->dma_mask;
  140. dwc3->dev.dma_parms = dev->dma_parms;
  141. dwc3->dev.parent = dev;
  142. glue->dwc3 = dwc3;
  143. ret = platform_device_add(dwc3);
  144. if (ret) {
  145. dev_err(dev, "failed to register dwc3 device\n");
  146. goto err3;
  147. }
  148. return 0;
  149. err3:
  150. pci_set_drvdata(pci, NULL);
  151. platform_device_put(dwc3);
  152. err1:
  153. pci_disable_device(pci);
  154. return ret;
  155. }
  156. static void dwc3_pci_remove(struct pci_dev *pci)
  157. {
  158. struct dwc3_pci *glue = pci_get_drvdata(pci);
  159. platform_device_unregister(glue->dwc3);
  160. platform_device_unregister(glue->usb2_phy);
  161. platform_device_unregister(glue->usb3_phy);
  162. pci_set_drvdata(pci, NULL);
  163. pci_disable_device(pci);
  164. }
  165. static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
  166. {
  167. PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
  168. PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
  169. },
  170. { } /* Terminating Entry */
  171. };
  172. MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
  173. #ifdef CONFIG_PM
  174. static int dwc3_pci_suspend(struct device *dev)
  175. {
  176. struct pci_dev *pci = to_pci_dev(dev);
  177. pci_disable_device(pci);
  178. return 0;
  179. }
  180. static int dwc3_pci_resume(struct device *dev)
  181. {
  182. struct pci_dev *pci = to_pci_dev(dev);
  183. int ret;
  184. ret = pci_enable_device(pci);
  185. if (ret) {
  186. dev_err(dev, "can't re-enable device --> %d\n", ret);
  187. return ret;
  188. }
  189. pci_set_master(pci);
  190. return 0;
  191. }
  192. static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
  193. SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
  194. };
  195. #define DEV_PM_OPS (&dwc3_pci_dev_pm_ops)
  196. #else
  197. #define DEV_PM_OPS NULL
  198. #endif /* CONFIG_PM */
  199. static struct pci_driver dwc3_pci_driver = {
  200. .name = "dwc3-pci",
  201. .id_table = dwc3_pci_id_table,
  202. .probe = dwc3_pci_probe,
  203. .remove = dwc3_pci_remove,
  204. .driver = {
  205. .pm = DEV_PM_OPS,
  206. },
  207. };
  208. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  209. MODULE_LICENSE("Dual BSD/GPL");
  210. MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
  211. module_pci_driver(dwc3_pci_driver);