pcie-cadence-plat.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence PCIe platform driver.
  4. *
  5. * Copyright (c) 2019, Cadence Design Systems
  6. * Author: Tom Joseph <tjoseph@cadence.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/of.h>
  10. #include <linux/of_pci.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include "pcie-cadence.h"
  14. #define CDNS_PLAT_CPU_TO_BUS_ADDR 0x0FFFFFFF
  15. /**
  16. * struct cdns_plat_pcie - private data for this PCIe platform driver
  17. * @pcie: Cadence PCIe controller
  18. */
  19. struct cdns_plat_pcie {
  20. struct cdns_pcie *pcie;
  21. };
  22. struct cdns_plat_pcie_of_data {
  23. bool is_rc;
  24. };
  25. static const struct of_device_id cdns_plat_pcie_of_match[];
  26. static u64 cdns_plat_cpu_addr_fixup(struct cdns_pcie *pcie, u64 cpu_addr)
  27. {
  28. return cpu_addr & CDNS_PLAT_CPU_TO_BUS_ADDR;
  29. }
  30. static const struct cdns_pcie_ops cdns_plat_ops = {
  31. .cpu_addr_fixup = cdns_plat_cpu_addr_fixup,
  32. };
  33. static int cdns_plat_pcie_probe(struct platform_device *pdev)
  34. {
  35. const struct cdns_plat_pcie_of_data *data;
  36. struct cdns_plat_pcie *cdns_plat_pcie;
  37. struct device *dev = &pdev->dev;
  38. struct pci_host_bridge *bridge;
  39. struct cdns_pcie_ep *ep;
  40. struct cdns_pcie_rc *rc;
  41. int phy_count;
  42. bool is_rc;
  43. int ret;
  44. data = of_device_get_match_data(dev);
  45. if (!data)
  46. return -EINVAL;
  47. is_rc = data->is_rc;
  48. pr_debug(" Started %s with is_rc: %d\n", __func__, is_rc);
  49. cdns_plat_pcie = devm_kzalloc(dev, sizeof(*cdns_plat_pcie), GFP_KERNEL);
  50. if (!cdns_plat_pcie)
  51. return -ENOMEM;
  52. platform_set_drvdata(pdev, cdns_plat_pcie);
  53. if (is_rc) {
  54. if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_HOST))
  55. return -ENODEV;
  56. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
  57. if (!bridge)
  58. return -ENOMEM;
  59. rc = pci_host_bridge_priv(bridge);
  60. rc->pcie.dev = dev;
  61. rc->pcie.ops = &cdns_plat_ops;
  62. cdns_plat_pcie->pcie = &rc->pcie;
  63. ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie);
  64. if (ret) {
  65. dev_err(dev, "failed to init phy\n");
  66. return ret;
  67. }
  68. pm_runtime_enable(dev);
  69. ret = pm_runtime_get_sync(dev);
  70. if (ret < 0) {
  71. dev_err(dev, "pm_runtime_get_sync() failed\n");
  72. goto err_get_sync;
  73. }
  74. ret = cdns_pcie_host_setup(rc);
  75. if (ret)
  76. goto err_init;
  77. } else {
  78. if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_EP))
  79. return -ENODEV;
  80. ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
  81. if (!ep)
  82. return -ENOMEM;
  83. ep->pcie.dev = dev;
  84. ep->pcie.ops = &cdns_plat_ops;
  85. cdns_plat_pcie->pcie = &ep->pcie;
  86. ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie);
  87. if (ret) {
  88. dev_err(dev, "failed to init phy\n");
  89. return ret;
  90. }
  91. pm_runtime_enable(dev);
  92. ret = pm_runtime_get_sync(dev);
  93. if (ret < 0) {
  94. dev_err(dev, "pm_runtime_get_sync() failed\n");
  95. goto err_get_sync;
  96. }
  97. ret = cdns_pcie_ep_setup(ep);
  98. if (ret)
  99. goto err_init;
  100. }
  101. return 0;
  102. err_init:
  103. err_get_sync:
  104. pm_runtime_put_sync(dev);
  105. pm_runtime_disable(dev);
  106. cdns_pcie_disable_phy(cdns_plat_pcie->pcie);
  107. phy_count = cdns_plat_pcie->pcie->phy_count;
  108. while (phy_count--)
  109. device_link_del(cdns_plat_pcie->pcie->link[phy_count]);
  110. return 0;
  111. }
  112. static void cdns_plat_pcie_shutdown(struct platform_device *pdev)
  113. {
  114. struct device *dev = &pdev->dev;
  115. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  116. int ret;
  117. ret = pm_runtime_put_sync(dev);
  118. if (ret < 0)
  119. dev_dbg(dev, "pm_runtime_put_sync failed\n");
  120. pm_runtime_disable(dev);
  121. cdns_pcie_disable_phy(pcie);
  122. }
  123. static const struct cdns_plat_pcie_of_data cdns_plat_pcie_host_of_data = {
  124. .is_rc = true,
  125. };
  126. static const struct cdns_plat_pcie_of_data cdns_plat_pcie_ep_of_data = {
  127. .is_rc = false,
  128. };
  129. static const struct of_device_id cdns_plat_pcie_of_match[] = {
  130. {
  131. .compatible = "cdns,cdns-pcie-host",
  132. .data = &cdns_plat_pcie_host_of_data,
  133. },
  134. {
  135. .compatible = "cdns,cdns-pcie-ep",
  136. .data = &cdns_plat_pcie_ep_of_data,
  137. },
  138. {},
  139. };
  140. static struct platform_driver cdns_plat_pcie_driver = {
  141. .driver = {
  142. .name = "cdns-pcie",
  143. .of_match_table = cdns_plat_pcie_of_match,
  144. .pm = &cdns_pcie_pm_ops,
  145. },
  146. .probe = cdns_plat_pcie_probe,
  147. .shutdown = cdns_plat_pcie_shutdown,
  148. };
  149. builtin_platform_driver(cdns_plat_pcie_driver);