pci.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * pci.c - DesignWare HS OTG Controller PCI driver
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The names of the above-listed copyright holders may not be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * ALTERNATIVELY, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") as published by the Free Software
  22. * Foundation; either version 2 of the License, or (at your option) any
  23. * later version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * Provides the initialization and cleanup entry points for the DWC_otg PCI
  39. * driver
  40. */
  41. #include <linux/kernel.h>
  42. #include <linux/module.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/io.h>
  47. #include <linux/slab.h>
  48. #include <linux/pci.h>
  49. #include <linux/usb.h>
  50. #include <linux/usb/hcd.h>
  51. #include <linux/usb/ch11.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/usb/usb_phy_generic.h>
  54. #define PCI_PRODUCT_ID_HAPS_HSOTG 0xabc0
  55. static const char dwc2_driver_name[] = "dwc2-pci";
  56. struct dwc2_pci_glue {
  57. struct platform_device *dwc2;
  58. struct platform_device *phy;
  59. };
  60. static int dwc2_pci_quirks(struct pci_dev *pdev, struct platform_device *dwc2)
  61. {
  62. if (pdev->vendor == PCI_VENDOR_ID_SYNOPSYS &&
  63. pdev->device == PCI_PRODUCT_ID_HAPS_HSOTG) {
  64. struct property_entry properties[] = {
  65. { },
  66. };
  67. return platform_device_add_properties(dwc2, properties);
  68. }
  69. return 0;
  70. }
  71. /**
  72. * dwc2_pci_probe() - Provides the cleanup entry points for the DWC_otg PCI
  73. * driver
  74. *
  75. * @pci: The programming view of DWC_otg PCI
  76. */
  77. static void dwc2_pci_remove(struct pci_dev *pci)
  78. {
  79. struct dwc2_pci_glue *glue = pci_get_drvdata(pci);
  80. platform_device_unregister(glue->dwc2);
  81. usb_phy_generic_unregister(glue->phy);
  82. pci_set_drvdata(pci, NULL);
  83. }
  84. static int dwc2_pci_probe(struct pci_dev *pci,
  85. const struct pci_device_id *id)
  86. {
  87. struct resource res[2];
  88. struct platform_device *dwc2;
  89. struct platform_device *phy;
  90. int ret;
  91. struct device *dev = &pci->dev;
  92. struct dwc2_pci_glue *glue;
  93. ret = pcim_enable_device(pci);
  94. if (ret) {
  95. dev_err(dev, "failed to enable pci device\n");
  96. return -ENODEV;
  97. }
  98. pci_set_master(pci);
  99. phy = usb_phy_generic_register();
  100. if (IS_ERR(phy)) {
  101. dev_err(dev, "error registering generic PHY (%ld)\n",
  102. PTR_ERR(phy));
  103. return PTR_ERR(phy);
  104. }
  105. dwc2 = platform_device_alloc("dwc2", PLATFORM_DEVID_AUTO);
  106. if (!dwc2) {
  107. dev_err(dev, "couldn't allocate dwc2 device\n");
  108. ret = -ENOMEM;
  109. goto err;
  110. }
  111. memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
  112. res[0].start = pci_resource_start(pci, 0);
  113. res[0].end = pci_resource_end(pci, 0);
  114. res[0].name = "dwc2";
  115. res[0].flags = IORESOURCE_MEM;
  116. res[1].start = pci->irq;
  117. res[1].name = "dwc2";
  118. res[1].flags = IORESOURCE_IRQ;
  119. ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res));
  120. if (ret) {
  121. dev_err(dev, "couldn't add resources to dwc2 device\n");
  122. goto err;
  123. }
  124. dwc2->dev.parent = dev;
  125. ret = dwc2_pci_quirks(pci, dwc2);
  126. if (ret)
  127. goto err;
  128. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  129. if (!glue) {
  130. ret = -ENOMEM;
  131. goto err;
  132. }
  133. ret = platform_device_add(dwc2);
  134. if (ret) {
  135. dev_err(dev, "failed to register dwc2 device\n");
  136. goto err;
  137. }
  138. glue->phy = phy;
  139. glue->dwc2 = dwc2;
  140. pci_set_drvdata(pci, glue);
  141. return 0;
  142. err:
  143. usb_phy_generic_unregister(phy);
  144. platform_device_put(dwc2);
  145. return ret;
  146. }
  147. static const struct pci_device_id dwc2_pci_ids[] = {
  148. {
  149. PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS, PCI_PRODUCT_ID_HAPS_HSOTG),
  150. },
  151. {
  152. PCI_DEVICE(PCI_VENDOR_ID_STMICRO,
  153. PCI_DEVICE_ID_STMICRO_USB_OTG),
  154. },
  155. { /* end: all zeroes */ }
  156. };
  157. MODULE_DEVICE_TABLE(pci, dwc2_pci_ids);
  158. static struct pci_driver dwc2_pci_driver = {
  159. .name = dwc2_driver_name,
  160. .id_table = dwc2_pci_ids,
  161. .probe = dwc2_pci_probe,
  162. .remove = dwc2_pci_remove,
  163. };
  164. module_pci_driver(dwc2_pci_driver);
  165. MODULE_DESCRIPTION("DESIGNWARE HS OTG PCI Bus Glue");
  166. MODULE_AUTHOR("Synopsys, Inc.");
  167. MODULE_LICENSE("Dual BSD/GPL");