dwc3-pci.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_power_state(pci, PCI_D0);
  113. pci_set_master(pci);
  114. ret = dwc3_pci_register_phys(glue);
  115. if (ret) {
  116. dev_err(dev, "couldn't register PHYs\n");
  117. return ret;
  118. }
  119. dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
  120. if (!dwc3) {
  121. dev_err(dev, "couldn't allocate dwc3 device\n");
  122. ret = -ENOMEM;
  123. goto err1;
  124. }
  125. memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
  126. res[0].start = pci_resource_start(pci, 0);
  127. res[0].end = pci_resource_end(pci, 0);
  128. res[0].name = "dwc_usb3";
  129. res[0].flags = IORESOURCE_MEM;
  130. res[1].start = pci->irq;
  131. res[1].name = "dwc_usb3";
  132. res[1].flags = IORESOURCE_IRQ;
  133. ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
  134. if (ret) {
  135. dev_err(dev, "couldn't add resources to dwc3 device\n");
  136. goto err1;
  137. }
  138. pci_set_drvdata(pci, glue);
  139. dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
  140. dwc3->dev.dma_mask = dev->dma_mask;
  141. dwc3->dev.dma_parms = dev->dma_parms;
  142. dwc3->dev.parent = dev;
  143. glue->dwc3 = dwc3;
  144. ret = platform_device_add(dwc3);
  145. if (ret) {
  146. dev_err(dev, "failed to register dwc3 device\n");
  147. goto err3;
  148. }
  149. return 0;
  150. err3:
  151. pci_set_drvdata(pci, NULL);
  152. platform_device_put(dwc3);
  153. err1:
  154. pci_disable_device(pci);
  155. return ret;
  156. }
  157. static void dwc3_pci_remove(struct pci_dev *pci)
  158. {
  159. struct dwc3_pci *glue = pci_get_drvdata(pci);
  160. platform_device_unregister(glue->usb2_phy);
  161. platform_device_unregister(glue->usb3_phy);
  162. platform_device_unregister(glue->dwc3);
  163. pci_set_drvdata(pci, NULL);
  164. pci_disable_device(pci);
  165. }
  166. static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
  167. {
  168. PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
  169. PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
  170. },
  171. { } /* Terminating Entry */
  172. };
  173. MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
  174. static struct pci_driver dwc3_pci_driver = {
  175. .name = "dwc3-pci",
  176. .id_table = dwc3_pci_id_table,
  177. .probe = dwc3_pci_probe,
  178. .remove = dwc3_pci_remove,
  179. };
  180. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
  181. MODULE_LICENSE("Dual BSD/GPL");
  182. MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
  183. module_pci_driver(dwc3_pci_driver);