Debian Codegraph @

drivers/pci/controller/cadence/pcie-cadence-plat.c

raw
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 <[email protected]>
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
15 #define CDNS_PLAT_CPU_TO_BUS_ADDR 0x0FFFFFFF
16
17 /**
18 * struct cdns_plat_pcie - private data for this PCIe platform driver
19 * @pcie: Cadence PCIe controller
20 */
21 struct cdns_plat_pcie {
22 struct cdns_pcie *pcie;
23 };
24
25 static const struct of_device_id cdns_plat_pcie_of_match[];
26
27 static u64 cdns_plat_cpu_addr_fixup(struct cdns_pcie *pcie, u64 cpu_addr)
28 {
29 return cpu_addr & CDNS_PLAT_CPU_TO_BUS_ADDR;
30 }
31
32 static const struct cdns_pcie_ops cdns_plat_ops = {
33 .cpu_addr_fixup = cdns_plat_cpu_addr_fixup,
34 };
35
36 static int cdns_plat_pcie_probe(struct platform_device *pdev)
37 {
38 const struct cdns_plat_pcie_of_data *data;
39 struct cdns_plat_pcie *cdns_plat_pcie;
40 struct device *dev = &pdev->dev;
41 struct pci_host_bridge *bridge;
42 struct cdns_pcie_ep *ep;
43 struct cdns_pcie_rc *rc;
44 int phy_count;
45 bool is_rc;
46 int ret;
47
48 data = of_device_get_match_data(dev);
49 if (!data)
50 return -EINVAL;
51
52 is_rc = data->is_rc;
53
54 pr_debug(" Started %s with is_rc: %d\n", __func__, is_rc);
55 cdns_plat_pcie = devm_kzalloc(dev, sizeof(*cdns_plat_pcie), GFP_KERNEL);
56 if (!cdns_plat_pcie)
57 return -ENOMEM;
58
59 platform_set_drvdata(pdev, cdns_plat_pcie);
60 if (is_rc) {
61 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_HOST))
62 return -ENODEV;
63
64 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc));
65 if (!bridge)
66 return -ENOMEM;
67
68 rc = pci_host_bridge_priv(bridge);
69 rc->pcie.dev = dev;
70 rc->pcie.ops = &cdns_plat_ops;
71 cdns_plat_pcie->pcie = &rc->pcie;
72
73 ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie);
74 if (ret) {
75 dev_err(dev, "failed to init phy\n");
76 return ret;
77 }
78 pm_runtime_enable(dev);
79 ret = pm_runtime_get_sync(dev);
80 if (ret < 0) {
81 dev_err(dev, "pm_runtime_get_sync() failed\n");
82 goto err_get_sync;
83 }
84
85 ret = cdns_pcie_host_setup(rc);
86 if (ret)
87 goto err_init;
88 } else {
89 if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_EP))
90 return -ENODEV;
91
92 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
93 if (!ep)
94 return -ENOMEM;
95
96 ep->pcie.dev = dev;
97 ep->pcie.ops = &cdns_plat_ops;
98 cdns_plat_pcie->pcie = &ep->pcie;
99
100 ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie);
101 if (ret) {
102 dev_err(dev, "failed to init phy\n");
103 return ret;
104 }
105
106 pm_runtime_enable(dev);
107 ret = pm_runtime_get_sync(dev);
108 if (ret < 0) {
109 dev_err(dev, "pm_runtime_get_sync() failed\n");
110 goto err_get_sync;
111 }
112
113 ret = cdns_pcie_ep_setup(ep);
114 if (ret)
115 goto err_init;
116 }
117
118 return 0;
119
120 err_init:
121 err_get_sync:
122 pm_runtime_put_sync(dev);
123 pm_runtime_disable(dev);
124 cdns_pcie_disable_phy(cdns_plat_pcie->pcie);
125 phy_count = cdns_plat_pcie->pcie->phy_count;
126 while (phy_count--)
127 device_link_del(cdns_plat_pcie->pcie->link[phy_count]);
128
129 return 0;
130 }
131
132 static void cdns_plat_pcie_shutdown(struct platform_device *pdev)
133 {
134 struct device *dev = &pdev->dev;
135 struct cdns_pcie *pcie = dev_get_drvdata(dev);
136 int ret;
137
138 ret = pm_runtime_put_sync(dev);
139 if (ret < 0)
140 dev_dbg(dev, "pm_runtime_put_sync failed\n");
141
142 pm_runtime_disable(dev);
143
144 cdns_pcie_disable_phy(pcie);
145 }
146
147 static const struct cdns_plat_pcie_of_data cdns_plat_pcie_host_of_data = {
148 .is_rc = true,
149 };
150
151 static const struct cdns_plat_pcie_of_data cdns_plat_pcie_ep_of_data = {
152 .is_rc = false,
153 };
154
155 static const struct of_device_id cdns_plat_pcie_of_match[] = {
156 {
157 .compatible = "cdns,cdns-pcie-host",
158 .data = &cdns_plat_pcie_host_of_data,
159 },
160 {
161 .compatible = "cdns,cdns-pcie-ep",
162 .data = &cdns_plat_pcie_ep_of_data,
163 },
164 {},
165 };
166
167 static struct platform_driver cdns_plat_pcie_driver = {
168 .driver = {
169 .name = "cdns-pcie",
170 .of_match_table = cdns_plat_pcie_of_match,
171 .pm = &cdns_pcie_pm_ops,
172 },
173 .probe = cdns_plat_pcie_probe,
174 .shutdown = cdns_plat_pcie_shutdown,
175 };
176 module_platform_driver(cdns_plat_pcie_driver);
177
178 MODULE_LICENSE("GPL");
179 MODULE_DESCRIPTION("Cadence PCIe controller platform driver");