cdns3-pci-wrap.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence USBSS PCI Glue driver
  4. *
  5. * Copyright (C) 2018-2019 Cadence.
  6. *
  7. * Author: Pawel Laszczak <pawell@cadence.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/slab.h>
  15. struct cdns3_wrap {
  16. struct platform_device *plat_dev;
  17. struct resource dev_res[6];
  18. int devfn;
  19. };
  20. #define RES_IRQ_HOST_ID 0
  21. #define RES_IRQ_PERIPHERAL_ID 1
  22. #define RES_IRQ_OTG_ID 2
  23. #define RES_HOST_ID 3
  24. #define RES_DEV_ID 4
  25. #define RES_DRD_ID 5
  26. #define PCI_BAR_HOST 0
  27. #define PCI_BAR_DEV 2
  28. #define PCI_BAR_OTG 0
  29. #define PCI_DEV_FN_HOST_DEVICE 0
  30. #define PCI_DEV_FN_OTG 1
  31. #define PCI_DRIVER_NAME "cdns3-pci-usbss"
  32. #define PLAT_DRIVER_NAME "cdns-usb3"
  33. #define CDNS_VENDOR_ID 0x17cd
  34. #define CDNS_DEVICE_ID 0x0100
  35. static struct pci_dev *cdns3_get_second_fun(struct pci_dev *pdev)
  36. {
  37. struct pci_dev *func;
  38. /*
  39. * Gets the second function.
  40. * It's little tricky, but this platform has two function.
  41. * The fist keeps resources for Host/Device while the second
  42. * keeps resources for DRD/OTG.
  43. */
  44. func = pci_get_device(pdev->vendor, pdev->device, NULL);
  45. if (unlikely(!func))
  46. return NULL;
  47. if (func->devfn == pdev->devfn) {
  48. func = pci_get_device(pdev->vendor, pdev->device, func);
  49. if (unlikely(!func))
  50. return NULL;
  51. }
  52. return func;
  53. }
  54. static int cdns3_pci_probe(struct pci_dev *pdev,
  55. const struct pci_device_id *id)
  56. {
  57. struct platform_device_info plat_info;
  58. struct cdns3_wrap *wrap;
  59. struct resource *res;
  60. struct pci_dev *func;
  61. int err;
  62. /*
  63. * for GADGET/HOST PCI (devfn) function number is 0,
  64. * for OTG PCI (devfn) function number is 1
  65. */
  66. if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE &&
  67. pdev->devfn != PCI_DEV_FN_OTG))
  68. return -EINVAL;
  69. func = cdns3_get_second_fun(pdev);
  70. if (unlikely(!func))
  71. return -EINVAL;
  72. err = pcim_enable_device(pdev);
  73. if (err) {
  74. dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", err);
  75. return err;
  76. }
  77. pci_set_master(pdev);
  78. if (pci_is_enabled(func)) {
  79. wrap = pci_get_drvdata(func);
  80. } else {
  81. wrap = kzalloc(sizeof(*wrap), GFP_KERNEL);
  82. if (!wrap) {
  83. pci_disable_device(pdev);
  84. return -ENOMEM;
  85. }
  86. }
  87. res = wrap->dev_res;
  88. if (pdev->devfn == PCI_DEV_FN_HOST_DEVICE) {
  89. /* function 0: host(BAR_0) + device(BAR_1).*/
  90. dev_dbg(&pdev->dev, "Initialize Device resources\n");
  91. res[RES_DEV_ID].start = pci_resource_start(pdev, PCI_BAR_DEV);
  92. res[RES_DEV_ID].end = pci_resource_end(pdev, PCI_BAR_DEV);
  93. res[RES_DEV_ID].name = "dev";
  94. res[RES_DEV_ID].flags = IORESOURCE_MEM;
  95. dev_dbg(&pdev->dev, "USBSS-DEV physical base addr: %pa\n",
  96. &res[RES_DEV_ID].start);
  97. res[RES_HOST_ID].start = pci_resource_start(pdev, PCI_BAR_HOST);
  98. res[RES_HOST_ID].end = pci_resource_end(pdev, PCI_BAR_HOST);
  99. res[RES_HOST_ID].name = "xhci";
  100. res[RES_HOST_ID].flags = IORESOURCE_MEM;
  101. dev_dbg(&pdev->dev, "USBSS-XHCI physical base addr: %pa\n",
  102. &res[RES_HOST_ID].start);
  103. /* Interrupt for XHCI */
  104. wrap->dev_res[RES_IRQ_HOST_ID].start = pdev->irq;
  105. wrap->dev_res[RES_IRQ_HOST_ID].name = "host";
  106. wrap->dev_res[RES_IRQ_HOST_ID].flags = IORESOURCE_IRQ;
  107. /* Interrupt device. It's the same as for HOST. */
  108. wrap->dev_res[RES_IRQ_PERIPHERAL_ID].start = pdev->irq;
  109. wrap->dev_res[RES_IRQ_PERIPHERAL_ID].name = "peripheral";
  110. wrap->dev_res[RES_IRQ_PERIPHERAL_ID].flags = IORESOURCE_IRQ;
  111. } else {
  112. res[RES_DRD_ID].start = pci_resource_start(pdev, PCI_BAR_OTG);
  113. res[RES_DRD_ID].end = pci_resource_end(pdev, PCI_BAR_OTG);
  114. res[RES_DRD_ID].name = "otg";
  115. res[RES_DRD_ID].flags = IORESOURCE_MEM;
  116. dev_dbg(&pdev->dev, "USBSS-DRD physical base addr: %pa\n",
  117. &res[RES_DRD_ID].start);
  118. /* Interrupt for OTG/DRD. */
  119. wrap->dev_res[RES_IRQ_OTG_ID].start = pdev->irq;
  120. wrap->dev_res[RES_IRQ_OTG_ID].name = "otg";
  121. wrap->dev_res[RES_IRQ_OTG_ID].flags = IORESOURCE_IRQ;
  122. }
  123. if (pci_is_enabled(func)) {
  124. /* set up platform device info */
  125. memset(&plat_info, 0, sizeof(plat_info));
  126. plat_info.parent = &pdev->dev;
  127. plat_info.fwnode = pdev->dev.fwnode;
  128. plat_info.name = PLAT_DRIVER_NAME;
  129. plat_info.id = pdev->devfn;
  130. wrap->devfn = pdev->devfn;
  131. plat_info.res = wrap->dev_res;
  132. plat_info.num_res = ARRAY_SIZE(wrap->dev_res);
  133. plat_info.dma_mask = pdev->dma_mask;
  134. /* register platform device */
  135. wrap->plat_dev = platform_device_register_full(&plat_info);
  136. if (IS_ERR(wrap->plat_dev)) {
  137. pci_disable_device(pdev);
  138. kfree(wrap);
  139. return PTR_ERR(wrap->plat_dev);
  140. }
  141. }
  142. pci_set_drvdata(pdev, wrap);
  143. return err;
  144. }
  145. static void cdns3_pci_remove(struct pci_dev *pdev)
  146. {
  147. struct cdns3_wrap *wrap;
  148. struct pci_dev *func;
  149. func = cdns3_get_second_fun(pdev);
  150. wrap = (struct cdns3_wrap *)pci_get_drvdata(pdev);
  151. if (wrap->devfn == pdev->devfn)
  152. platform_device_unregister(wrap->plat_dev);
  153. if (!pci_is_enabled(func))
  154. kfree(wrap);
  155. }
  156. static const struct pci_device_id cdns3_pci_ids[] = {
  157. { PCI_DEVICE(CDNS_VENDOR_ID, CDNS_DEVICE_ID), },
  158. { 0, }
  159. };
  160. static struct pci_driver cdns3_pci_driver = {
  161. .name = PCI_DRIVER_NAME,
  162. .id_table = cdns3_pci_ids,
  163. .probe = cdns3_pci_probe,
  164. .remove = cdns3_pci_remove,
  165. };
  166. module_pci_driver(cdns3_pci_driver);
  167. MODULE_DEVICE_TABLE(pci, cdns3_pci_ids);
  168. MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
  169. MODULE_LICENSE("GPL v2");
  170. MODULE_DESCRIPTION("Cadence USBSS PCI wrapperr");