pcie-designware-plat.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * PCIe RC driver for Synopsys DesignWare Core
  3. *
  4. * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * Authors: Joao Pinto <jpinto@synopsys.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/resource.h>
  22. #include <linux/signal.h>
  23. #include <linux/types.h>
  24. #include "pcie-designware.h"
  25. struct dw_plat_pcie {
  26. void __iomem *mem_base;
  27. struct pcie_port pp;
  28. };
  29. static irqreturn_t dw_plat_pcie_msi_irq_handler(int irq, void *arg)
  30. {
  31. struct pcie_port *pp = arg;
  32. return dw_handle_msi_irq(pp);
  33. }
  34. static void dw_plat_pcie_host_init(struct pcie_port *pp)
  35. {
  36. dw_pcie_setup_rc(pp);
  37. dw_pcie_wait_for_link(pp);
  38. if (IS_ENABLED(CONFIG_PCI_MSI))
  39. dw_pcie_msi_init(pp);
  40. }
  41. static struct pcie_host_ops dw_plat_pcie_host_ops = {
  42. .host_init = dw_plat_pcie_host_init,
  43. };
  44. static int dw_plat_add_pcie_port(struct pcie_port *pp,
  45. struct platform_device *pdev)
  46. {
  47. int ret;
  48. pp->irq = platform_get_irq(pdev, 1);
  49. if (pp->irq < 0)
  50. return pp->irq;
  51. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  52. pp->msi_irq = platform_get_irq(pdev, 0);
  53. if (pp->msi_irq < 0)
  54. return pp->msi_irq;
  55. ret = devm_request_irq(&pdev->dev, pp->msi_irq,
  56. dw_plat_pcie_msi_irq_handler,
  57. IRQF_SHARED, "dw-plat-pcie-msi", pp);
  58. if (ret) {
  59. dev_err(&pdev->dev, "failed to request MSI IRQ\n");
  60. return ret;
  61. }
  62. }
  63. pp->root_bus_nr = -1;
  64. pp->ops = &dw_plat_pcie_host_ops;
  65. ret = dw_pcie_host_init(pp);
  66. if (ret) {
  67. dev_err(&pdev->dev, "failed to initialize host\n");
  68. return ret;
  69. }
  70. return 0;
  71. }
  72. static int dw_plat_pcie_probe(struct platform_device *pdev)
  73. {
  74. struct dw_plat_pcie *dw_plat_pcie;
  75. struct pcie_port *pp;
  76. struct resource *res; /* Resource from DT */
  77. int ret;
  78. dw_plat_pcie = devm_kzalloc(&pdev->dev, sizeof(*dw_plat_pcie),
  79. GFP_KERNEL);
  80. if (!dw_plat_pcie)
  81. return -ENOMEM;
  82. pp = &dw_plat_pcie->pp;
  83. pp->dev = &pdev->dev;
  84. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  85. if (!res)
  86. return -ENODEV;
  87. dw_plat_pcie->mem_base = devm_ioremap_resource(&pdev->dev, res);
  88. if (IS_ERR(dw_plat_pcie->mem_base))
  89. return PTR_ERR(dw_plat_pcie->mem_base);
  90. pp->dbi_base = dw_plat_pcie->mem_base;
  91. ret = dw_plat_add_pcie_port(pp, pdev);
  92. if (ret < 0)
  93. return ret;
  94. platform_set_drvdata(pdev, dw_plat_pcie);
  95. return 0;
  96. }
  97. static const struct of_device_id dw_plat_pcie_of_match[] = {
  98. { .compatible = "snps,dw-pcie", },
  99. {},
  100. };
  101. MODULE_DEVICE_TABLE(of, dw_plat_pcie_of_match);
  102. static struct platform_driver dw_plat_pcie_driver = {
  103. .driver = {
  104. .name = "dw-pcie",
  105. .of_match_table = dw_plat_pcie_of_match,
  106. },
  107. .probe = dw_plat_pcie_probe,
  108. };
  109. module_platform_driver(dw_plat_pcie_driver);
  110. MODULE_AUTHOR("Joao Pinto <Joao.Pinto@synopsys.com>");
  111. MODULE_DESCRIPTION("Synopsys PCIe host controller glue platform driver");
  112. MODULE_LICENSE("GPL v2");