dwc3-pci.c 6.0 KB

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