dwc3-pci.c 5.6 KB

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