dwc3-pci.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include "core.h"
  46. /* FIXME define these in <linux/pci_ids.h> */
  47. #define PCI_VENDOR_ID_SYNOPSYS 0x16c3
  48. #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 0xabcd
  49. struct dwc3_pci {
  50. struct device *dev;
  51. struct platform_device *dwc3;
  52. struct platform_device *usb2_phy;
  53. struct platform_device *usb3_phy;
  54. };
  55. static int dwc3_pci_register_phys(struct dwc3_pci *glue)
  56. {
  57. struct nop_usb_xceiv_platform_data pdata;
  58. struct platform_device *pdev;
  59. int ret;
  60. memset(&pdata, 0x00, sizeof(pdata));
  61. pdev = platform_device_alloc("nop_usb_xceiv", 0);
  62. if (!pdev)
  63. return -ENOMEM;
  64. glue->usb2_phy = pdev;
  65. pdata.type = USB_PHY_TYPE_USB2;
  66. ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
  67. if (ret)
  68. goto err1;
  69. pdev = platform_device_alloc("nop_usb_xceiv", 1);
  70. if (!pdev) {
  71. ret = -ENOMEM;
  72. goto err1;
  73. }
  74. glue->usb3_phy = pdev;
  75. pdata.type = USB_PHY_TYPE_USB3;
  76. ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
  77. if (ret)
  78. goto err2;
  79. ret = platform_device_add(glue->usb2_phy);
  80. if (ret)
  81. goto err2;
  82. ret = platform_device_add(glue->usb3_phy);
  83. if (ret)
  84. goto err3;
  85. return 0;
  86. err3:
  87. platform_device_del(glue->usb2_phy);
  88. err2:
  89. platform_device_put(glue->usb3_phy);
  90. err1:
  91. platform_device_put(glue->usb2_phy);
  92. return ret;
  93. }
  94. static int dwc3_pci_probe(struct pci_dev *pci,
  95. const struct pci_device_id *id)
  96. {
  97. struct resource res[2];
  98. struct platform_device *dwc3;
  99. struct dwc3_pci *glue;
  100. int ret = -ENOMEM;
  101. struct device *dev = &pci->dev;
  102. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  103. if (!glue) {
  104. dev_err(dev, "not enough memory\n");
  105. return -ENOMEM;
  106. }
  107. glue->dev = dev;
  108. ret = pci_enable_device(pci);
  109. if (ret) {
  110. dev_err(dev, "failed to enable pci device\n");
  111. return -ENODEV;
  112. }
  113. pci_set_power_state(pci, PCI_D0);
  114. pci_set_master(pci);
  115. ret = dwc3_pci_register_phys(glue);
  116. if (ret) {
  117. dev_err(dev, "couldn't register PHYs\n");
  118. return ret;
  119. }
  120. dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
  121. if (!dwc3) {
  122. dev_err(dev, "couldn't allocate dwc3 device\n");
  123. ret = -ENOMEM;
  124. goto err1;
  125. }
  126. memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
  127. res[0].start = pci_resource_start(pci, 0);
  128. res[0].end = pci_resource_end(pci, 0);
  129. res[0].name = "dwc_usb3";
  130. res[0].flags = IORESOURCE_MEM;
  131. res[1].start = pci->irq;
  132. res[1].name = "dwc_usb3";
  133. res[1].flags = IORESOURCE_IRQ;
  134. ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
  135. if (ret) {
  136. dev_err(dev, "couldn't add resources to dwc3 device\n");
  137. goto err1;
  138. }
  139. pci_set_drvdata(pci, glue);
  140. dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
  141. dwc3->dev.dma_mask = dev->dma_mask;
  142. dwc3->dev.dma_parms = dev->dma_parms;
  143. dwc3->dev.parent = dev;
  144. glue->dwc3 = dwc3;
  145. ret = platform_device_add(dwc3);
  146. if (ret) {
  147. dev_err(dev, "failed to register dwc3 device\n");
  148. goto err3;
  149. }
  150. return 0;
  151. err3:
  152. pci_set_drvdata(pci, NULL);
  153. platform_device_put(dwc3);
  154. err1:
  155. pci_disable_device(pci);
  156. return ret;
  157. }
  158. static void dwc3_pci_remove(struct pci_dev *pci)
  159. {
  160. struct dwc3_pci *glue = pci_get_drvdata(pci);
  161. platform_device_unregister(glue->usb2_phy);
  162. platform_device_unregister(glue->usb3_phy);
  163. platform_device_unregister(glue->dwc3);
  164. pci_set_drvdata(pci, NULL);
  165. pci_disable_device(pci);
  166. }
  167. static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
  168. {
  169. PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
  170. PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
  171. },
  172. { } /* Terminating Entry */
  173. };
  174. MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
  175. static struct pci_driver dwc3_pci_driver = {
  176. .name = "dwc3-pci",
  177. .id_table = dwc3_pci_id_table,
  178. .probe = dwc3_pci_probe,
  179. .remove = dwc3_pci_remove,
  180. };
  181. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  182. MODULE_LICENSE("Dual BSD/GPL");
  183. MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
  184. module_pci_driver(dwc3_pci_driver);