blob: b6c832ba39dd6905a4640e770b69aff47e51b738 [file] [log] [blame]
Joao Pinto5a3aa2a2016-03-10 14:44:52 -06001/*
2 * PCIe RC driver for Synopsys DesignWare Core
3 *
4 * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com)
5 *
Joao Pinto9f461072016-11-15 16:10:47 +00006 * Authors: Joao Pinto <Joao.Pinto@synopsys.com>
Joao Pinto5a3aa2a2016-03-10 14:44:52 -06007 *
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>
Paul Gortmakerca8d3342016-07-02 19:13:23 -040017#include <linux/init.h>
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060018#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
25#include "pcie-designware.h"
26
27struct dw_plat_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053028 struct dw_pcie *pci;
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060029};
30
31static irqreturn_t dw_plat_pcie_msi_irq_handler(int irq, void *arg)
32{
33 struct pcie_port *pp = arg;
34
35 return dw_handle_msi_irq(pp);
36}
37
38static void dw_plat_pcie_host_init(struct pcie_port *pp)
39{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053040 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
41
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060042 dw_pcie_setup_rc(pp);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053043 dw_pcie_wait_for_link(pci);
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060044
45 if (IS_ENABLED(CONFIG_PCI_MSI))
46 dw_pcie_msi_init(pp);
47}
48
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053049static struct dw_pcie_host_ops dw_plat_pcie_host_ops = {
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060050 .host_init = dw_plat_pcie_host_init,
51};
52
53static int dw_plat_add_pcie_port(struct pcie_port *pp,
54 struct platform_device *pdev)
55{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053056 struct device *dev = &pdev->dev;
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060057 int ret;
58
59 pp->irq = platform_get_irq(pdev, 1);
60 if (pp->irq < 0)
61 return pp->irq;
62
63 if (IS_ENABLED(CONFIG_PCI_MSI)) {
64 pp->msi_irq = platform_get_irq(pdev, 0);
65 if (pp->msi_irq < 0)
66 return pp->msi_irq;
67
Bjorn Helgaas2d6054b2016-10-06 13:32:15 -050068 ret = devm_request_irq(dev, pp->msi_irq,
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060069 dw_plat_pcie_msi_irq_handler,
70 IRQF_SHARED, "dw-plat-pcie-msi", pp);
71 if (ret) {
Bjorn Helgaas2d6054b2016-10-06 13:32:15 -050072 dev_err(dev, "failed to request MSI IRQ\n");
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060073 return ret;
74 }
75 }
76
77 pp->root_bus_nr = -1;
78 pp->ops = &dw_plat_pcie_host_ops;
79
80 ret = dw_pcie_host_init(pp);
81 if (ret) {
Bjorn Helgaas2d6054b2016-10-06 13:32:15 -050082 dev_err(dev, "failed to initialize host\n");
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060083 return ret;
84 }
85
86 return 0;
87}
88
89static int dw_plat_pcie_probe(struct platform_device *pdev)
90{
Bjorn Helgaas2d6054b2016-10-06 13:32:15 -050091 struct device *dev = &pdev->dev;
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060092 struct dw_plat_pcie *dw_plat_pcie;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053093 struct dw_pcie *pci;
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060094 struct resource *res; /* Resource from DT */
95 int ret;
96
Bjorn Helgaas2d6054b2016-10-06 13:32:15 -050097 dw_plat_pcie = devm_kzalloc(dev, sizeof(*dw_plat_pcie), GFP_KERNEL);
Joao Pinto5a3aa2a2016-03-10 14:44:52 -060098 if (!dw_plat_pcie)
99 return -ENOMEM;
100
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530101 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
102 if (!pci)
103 return -ENOMEM;
104
105 pci->dev = dev;
Joao Pinto5a3aa2a2016-03-10 14:44:52 -0600106
Guenter Roeckc0464062017-02-25 02:08:12 -0800107 dw_plat_pcie->pci = pci;
108
Joao Pinto5a3aa2a2016-03-10 14:44:52 -0600109 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530110 pci->dbi_base = devm_ioremap_resource(dev, res);
111 if (IS_ERR(pci->dbi_base))
112 return PTR_ERR(pci->dbi_base);
Joao Pinto5a3aa2a2016-03-10 14:44:52 -0600113
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530114 platform_set_drvdata(pdev, dw_plat_pcie);
115
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530116 ret = dw_plat_add_pcie_port(&pci->pp, pdev);
Joao Pinto5a3aa2a2016-03-10 14:44:52 -0600117 if (ret < 0)
118 return ret;
119
Joao Pinto5a3aa2a2016-03-10 14:44:52 -0600120 return 0;
121}
122
123static const struct of_device_id dw_plat_pcie_of_match[] = {
124 { .compatible = "snps,dw-pcie", },
125 {},
126};
Joao Pinto5a3aa2a2016-03-10 14:44:52 -0600127
128static struct platform_driver dw_plat_pcie_driver = {
129 .driver = {
130 .name = "dw-pcie",
131 .of_match_table = dw_plat_pcie_of_match,
132 },
133 .probe = dw_plat_pcie_probe,
134};
Paul Gortmakerca8d3342016-07-02 19:13:23 -0400135builtin_platform_driver(dw_plat_pcie_driver);