Linux 4.16.11
[linux/fpc-iii.git] / drivers / pci / dwc / pcie-designware-plat.c
blobebdf28bcd67df0e052bd21e8325aae8418ecce13
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCIe RC driver for Synopsys DesignWare Core
5 * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com)
7 * Authors: Joao Pinto <Joao.Pinto@synopsys.com>
8 */
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/gpio.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/of_gpio.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/resource.h>
19 #include <linux/signal.h>
20 #include <linux/types.h>
22 #include "pcie-designware.h"
24 struct dw_plat_pcie {
25 struct dw_pcie *pci;
28 static irqreturn_t dw_plat_pcie_msi_irq_handler(int irq, void *arg)
30 struct pcie_port *pp = arg;
32 return dw_handle_msi_irq(pp);
35 static int dw_plat_pcie_host_init(struct pcie_port *pp)
37 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
39 dw_pcie_setup_rc(pp);
40 dw_pcie_wait_for_link(pci);
42 if (IS_ENABLED(CONFIG_PCI_MSI))
43 dw_pcie_msi_init(pp);
45 return 0;
48 static const struct dw_pcie_host_ops dw_plat_pcie_host_ops = {
49 .host_init = dw_plat_pcie_host_init,
52 static int dw_plat_add_pcie_port(struct pcie_port *pp,
53 struct platform_device *pdev)
55 struct device *dev = &pdev->dev;
56 int ret;
58 pp->irq = platform_get_irq(pdev, 1);
59 if (pp->irq < 0)
60 return pp->irq;
62 if (IS_ENABLED(CONFIG_PCI_MSI)) {
63 pp->msi_irq = platform_get_irq(pdev, 0);
64 if (pp->msi_irq < 0)
65 return pp->msi_irq;
67 ret = devm_request_irq(dev, pp->msi_irq,
68 dw_plat_pcie_msi_irq_handler,
69 IRQF_SHARED | IRQF_NO_THREAD,
70 "dw-plat-pcie-msi", pp);
71 if (ret) {
72 dev_err(dev, "failed to request MSI IRQ\n");
73 return ret;
77 pp->root_bus_nr = -1;
78 pp->ops = &dw_plat_pcie_host_ops;
80 ret = dw_pcie_host_init(pp);
81 if (ret) {
82 dev_err(dev, "failed to initialize host\n");
83 return ret;
86 return 0;
89 static const struct dw_pcie_ops dw_pcie_ops = {
92 static int dw_plat_pcie_probe(struct platform_device *pdev)
94 struct device *dev = &pdev->dev;
95 struct dw_plat_pcie *dw_plat_pcie;
96 struct dw_pcie *pci;
97 struct resource *res; /* Resource from DT */
98 int ret;
100 dw_plat_pcie = devm_kzalloc(dev, sizeof(*dw_plat_pcie), GFP_KERNEL);
101 if (!dw_plat_pcie)
102 return -ENOMEM;
104 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
105 if (!pci)
106 return -ENOMEM;
108 pci->dev = dev;
109 pci->ops = &dw_pcie_ops;
111 dw_plat_pcie->pci = pci;
113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114 pci->dbi_base = devm_ioremap_resource(dev, res);
115 if (IS_ERR(pci->dbi_base))
116 return PTR_ERR(pci->dbi_base);
118 platform_set_drvdata(pdev, dw_plat_pcie);
120 ret = dw_plat_add_pcie_port(&pci->pp, pdev);
121 if (ret < 0)
122 return ret;
124 return 0;
127 static const struct of_device_id dw_plat_pcie_of_match[] = {
128 { .compatible = "snps,dw-pcie", },
132 static struct platform_driver dw_plat_pcie_driver = {
133 .driver = {
134 .name = "dw-pcie",
135 .of_match_table = dw_plat_pcie_of_match,
136 .suppress_bind_attrs = true,
138 .probe = dw_plat_pcie_probe,
140 builtin_platform_driver(dw_plat_pcie_driver);