cdnsp-pci.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence PCI Glue driver.
  4. *
  5. * Copyright (C) 2019 Cadence.
  6. *
  7. * Author: Pawel Laszczak <pawell@cadence.com>
  8. *
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/pci.h>
  16. #include "core.h"
  17. #include "gadget-export.h"
  18. #define PCI_BAR_HOST 0
  19. #define PCI_BAR_OTG 0
  20. #define PCI_BAR_DEV 2
  21. #define PCI_DEV_FN_HOST_DEVICE 0
  22. #define PCI_DEV_FN_OTG 1
  23. #define PCI_DRIVER_NAME "cdns-pci-usbssp"
  24. #define PLAT_DRIVER_NAME "cdns-usbssp"
  25. #define CHICKEN_APB_TIMEOUT_VALUE 0x1C20
  26. static struct pci_dev *cdnsp_get_second_fun(struct pci_dev *pdev)
  27. {
  28. /*
  29. * Gets the second function.
  30. * Platform has two function. The fist keeps resources for
  31. * Host/Device while the secon keeps resources for DRD/OTG.
  32. */
  33. if (pdev->device == PCI_DEVICE_ID_CDNS_USBSSP)
  34. return pci_get_device(pdev->vendor, PCI_DEVICE_ID_CDNS_USBSS, NULL);
  35. if (pdev->device == PCI_DEVICE_ID_CDNS_USBSS)
  36. return pci_get_device(pdev->vendor, PCI_DEVICE_ID_CDNS_USBSSP, NULL);
  37. return NULL;
  38. }
  39. static int cdnsp_pci_probe(struct pci_dev *pdev,
  40. const struct pci_device_id *id)
  41. {
  42. struct device *dev = &pdev->dev;
  43. struct pci_dev *func;
  44. struct resource *res;
  45. struct cdns *cdnsp;
  46. int ret;
  47. /*
  48. * For GADGET/HOST PCI (devfn) function number is 0,
  49. * for OTG PCI (devfn) function number is 1.
  50. */
  51. if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE &&
  52. pdev->devfn != PCI_DEV_FN_OTG))
  53. return -EINVAL;
  54. func = cdnsp_get_second_fun(pdev);
  55. if (!func)
  56. return -EINVAL;
  57. if (func->class == PCI_CLASS_SERIAL_USB_XHCI ||
  58. pdev->class == PCI_CLASS_SERIAL_USB_XHCI) {
  59. ret = -EINVAL;
  60. goto put_pci;
  61. }
  62. ret = pcim_enable_device(pdev);
  63. if (ret) {
  64. dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret);
  65. goto put_pci;
  66. }
  67. pci_set_master(pdev);
  68. if (pci_is_enabled(func)) {
  69. cdnsp = pci_get_drvdata(func);
  70. } else {
  71. cdnsp = kzalloc_obj(*cdnsp);
  72. if (!cdnsp) {
  73. ret = -ENOMEM;
  74. goto put_pci;
  75. }
  76. }
  77. /* For GADGET device function number is 0. */
  78. if (pdev->devfn == 0) {
  79. resource_size_t rsrc_start, rsrc_len;
  80. /* Function 0: host(BAR_0) + device(BAR_1).*/
  81. dev_dbg(dev, "Initialize resources\n");
  82. rsrc_start = pci_resource_start(pdev, PCI_BAR_DEV);
  83. rsrc_len = pci_resource_len(pdev, PCI_BAR_DEV);
  84. res = devm_request_mem_region(dev, rsrc_start, rsrc_len, "dev");
  85. if (!res) {
  86. dev_dbg(dev, "controller already in use\n");
  87. ret = -EBUSY;
  88. goto free_cdnsp;
  89. }
  90. cdnsp->dev_regs = devm_ioremap(dev, rsrc_start, rsrc_len);
  91. if (!cdnsp->dev_regs) {
  92. dev_dbg(dev, "error mapping memory\n");
  93. ret = -EFAULT;
  94. goto free_cdnsp;
  95. }
  96. cdnsp->dev_irq = pdev->irq;
  97. dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n",
  98. &rsrc_start);
  99. res = &cdnsp->xhci_res[0];
  100. res->start = pci_resource_start(pdev, PCI_BAR_HOST);
  101. res->end = pci_resource_end(pdev, PCI_BAR_HOST);
  102. res->name = "xhci";
  103. res->flags = IORESOURCE_MEM;
  104. dev_dbg(dev, "USBSS-XHCI physical base addr: %pa\n",
  105. &res->start);
  106. /* Interrupt for XHCI, */
  107. res = &cdnsp->xhci_res[1];
  108. res->start = pdev->irq;
  109. res->name = "host";
  110. res->flags = IORESOURCE_IRQ;
  111. } else {
  112. res = &cdnsp->otg_res;
  113. res->start = pci_resource_start(pdev, PCI_BAR_OTG);
  114. res->end = pci_resource_end(pdev, PCI_BAR_OTG);
  115. res->name = "otg";
  116. res->flags = IORESOURCE_MEM;
  117. dev_dbg(dev, "CDNSP-DRD physical base addr: %pa\n",
  118. &res->start);
  119. /* Interrupt for OTG/DRD. */
  120. cdnsp->otg_irq = pdev->irq;
  121. }
  122. /*
  123. * Cadence PCI based platform require some longer timeout for APB
  124. * to fixes domain clock synchronization issue after resuming
  125. * controller from L1 state.
  126. */
  127. cdnsp->override_apb_timeout = CHICKEN_APB_TIMEOUT_VALUE;
  128. pci_set_drvdata(pdev, cdnsp);
  129. if (pci_is_enabled(func)) {
  130. cdnsp->dev = dev;
  131. cdnsp->gadget_init = cdnsp_gadget_init;
  132. ret = cdns_init(cdnsp);
  133. if (ret)
  134. goto free_cdnsp;
  135. }
  136. device_wakeup_enable(&pdev->dev);
  137. if (pci_dev_run_wake(pdev))
  138. pm_runtime_put_noidle(&pdev->dev);
  139. return 0;
  140. free_cdnsp:
  141. if (!pci_is_enabled(func))
  142. kfree(cdnsp);
  143. put_pci:
  144. pci_dev_put(func);
  145. return ret;
  146. }
  147. static void cdnsp_pci_remove(struct pci_dev *pdev)
  148. {
  149. struct cdns *cdnsp;
  150. struct pci_dev *func;
  151. func = cdnsp_get_second_fun(pdev);
  152. cdnsp = (struct cdns *)pci_get_drvdata(pdev);
  153. if (pci_dev_run_wake(pdev))
  154. pm_runtime_get_noresume(&pdev->dev);
  155. if (pci_is_enabled(func)) {
  156. cdns_remove(cdnsp);
  157. } else {
  158. kfree(cdnsp);
  159. }
  160. pci_dev_put(func);
  161. }
  162. static int __maybe_unused cdnsp_pci_suspend(struct device *dev)
  163. {
  164. struct cdns *cdns = dev_get_drvdata(dev);
  165. return cdns_suspend(cdns);
  166. }
  167. static int __maybe_unused cdnsp_pci_resume(struct device *dev)
  168. {
  169. struct cdns *cdns = dev_get_drvdata(dev);
  170. unsigned long flags;
  171. int ret;
  172. spin_lock_irqsave(&cdns->lock, flags);
  173. ret = cdns_resume(cdns);
  174. spin_unlock_irqrestore(&cdns->lock, flags);
  175. cdns_set_active(cdns, 1);
  176. return ret;
  177. }
  178. static const struct dev_pm_ops cdnsp_pci_pm_ops = {
  179. SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend, cdnsp_pci_resume)
  180. };
  181. static const struct pci_device_id cdnsp_pci_ids[] = {
  182. { PCI_DEVICE(PCI_VENDOR_ID_CDNS, PCI_DEVICE_ID_CDNS_USBSSP),
  183. .class = PCI_CLASS_SERIAL_USB_DEVICE },
  184. { PCI_DEVICE(PCI_VENDOR_ID_CDNS, PCI_DEVICE_ID_CDNS_USBSSP),
  185. .class = PCI_CLASS_SERIAL_USB_CDNS },
  186. { PCI_DEVICE(PCI_VENDOR_ID_CDNS, PCI_DEVICE_ID_CDNS_USBSS),
  187. .class = PCI_CLASS_SERIAL_USB_CDNS },
  188. { 0, }
  189. };
  190. static struct pci_driver cdnsp_pci_driver = {
  191. .name = "cdnsp-pci",
  192. .id_table = cdnsp_pci_ids,
  193. .probe = cdnsp_pci_probe,
  194. .remove = cdnsp_pci_remove,
  195. .driver = {
  196. .pm = &cdnsp_pci_pm_ops,
  197. }
  198. };
  199. module_pci_driver(cdnsp_pci_driver);
  200. MODULE_DEVICE_TABLE(pci, cdnsp_pci_ids);
  201. MODULE_ALIAS("pci:cdnsp");
  202. MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
  203. MODULE_LICENSE("GPL v2");
  204. MODULE_DESCRIPTION("Cadence CDNSP PCI driver");