2 * Simple, generic PCI host controller driver targetting firmware-initialised
3 * systems and virtual machines (e.g. the PCI emulation provided by kvmtool).
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2014 ARM Limited
19 * Author: Will Deacon <will.deacon@arm.com>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/of_address.h>
25 #include <linux/of_pci.h>
26 #include <linux/platform_device.h>
28 struct gen_pci_cfg_bus_ops {
33 struct gen_pci_cfg_windows {
35 struct resource *bus_range;
38 struct gen_pci_cfg_bus_ops *ops;
42 struct pci_host_bridge host;
43 struct gen_pci_cfg_windows cfg;
44 struct list_head resources;
47 static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus,
51 struct gen_pci *pci = bus->sysdata;
52 resource_size_t idx = bus->number - pci->cfg.bus_range->start;
54 return pci->cfg.win[idx] + ((devfn << 8) | where);
57 static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = {
60 .map_bus = gen_pci_map_cfg_bus_cam,
61 .read = pci_generic_config_read,
62 .write = pci_generic_config_write,
66 static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
70 struct gen_pci *pci = bus->sysdata;
71 resource_size_t idx = bus->number - pci->cfg.bus_range->start;
73 return pci->cfg.win[idx] + ((devfn << 12) | where);
76 static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = {
79 .map_bus = gen_pci_map_cfg_bus_ecam,
80 .read = pci_generic_config_read,
81 .write = pci_generic_config_write,
85 static const struct of_device_id gen_pci_of_match[] = {
86 { .compatible = "pci-host-cam-generic",
87 .data = &gen_pci_cfg_cam_bus_ops },
89 { .compatible = "pci-host-ecam-generic",
90 .data = &gen_pci_cfg_ecam_bus_ops },
94 MODULE_DEVICE_TABLE(of, gen_pci_of_match);
96 static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
98 pci_free_resource_list(&pci->resources);
101 static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
103 int err, res_valid = 0;
104 struct device *dev = pci->host.dev.parent;
105 struct device_node *np = dev->of_node;
106 resource_size_t iobase;
107 struct resource_entry *win;
109 err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
114 resource_list_for_each_entry(win, &pci->resources) {
115 struct resource *parent, *res = win->res;
117 switch (resource_type(res)) {
119 parent = &ioport_resource;
120 err = pci_remap_iospace(res, iobase);
122 dev_warn(dev, "error %d: failed to map resource %pR\n",
128 parent = &iomem_resource;
129 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
132 pci->cfg.bus_range = res;
137 err = devm_request_resource(dev, parent, res);
139 goto out_release_res;
143 dev_err(dev, "non-prefetchable memory resource required\n");
145 goto out_release_res;
151 gen_pci_release_of_pci_ranges(pci);
155 static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci)
159 resource_size_t busn;
160 struct resource *bus_range;
161 struct device *dev = pci->host.dev.parent;
162 struct device_node *np = dev->of_node;
163 u32 sz = 1 << pci->cfg.ops->bus_shift;
165 err = of_address_to_resource(np, 0, &pci->cfg.res);
167 dev_err(dev, "missing \"reg\" property\n");
171 /* Limit the bus-range to fit within reg */
172 bus_max = pci->cfg.bus_range->start +
173 (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
174 pci->cfg.bus_range->end = min_t(resource_size_t,
175 pci->cfg.bus_range->end, bus_max);
177 pci->cfg.win = devm_kcalloc(dev, resource_size(pci->cfg.bus_range),
178 sizeof(*pci->cfg.win), GFP_KERNEL);
182 /* Map our Configuration Space windows */
183 if (!devm_request_mem_region(dev, pci->cfg.res.start,
184 resource_size(&pci->cfg.res),
185 "Configuration Space"))
188 bus_range = pci->cfg.bus_range;
189 for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
190 u32 idx = busn - bus_range->start;
192 pci->cfg.win[idx] = devm_ioremap(dev,
193 pci->cfg.res.start + idx * sz,
195 if (!pci->cfg.win[idx])
202 static int gen_pci_probe(struct platform_device *pdev)
206 const struct of_device_id *of_id;
207 struct device *dev = &pdev->dev;
208 struct device_node *np = dev->of_node;
209 struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
210 struct pci_bus *bus, *child;
215 type = of_get_property(np, "device_type", NULL);
216 if (!type || strcmp(type, "pci")) {
217 dev_err(dev, "invalid \"device_type\" %s\n", type);
221 of_pci_check_probe_only();
223 of_id = of_match_node(gen_pci_of_match, np);
224 pci->cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data;
225 pci->host.dev.parent = dev;
226 INIT_LIST_HEAD(&pci->host.windows);
227 INIT_LIST_HEAD(&pci->resources);
229 /* Parse our PCI ranges and request their resources */
230 err = gen_pci_parse_request_of_pci_ranges(pci);
234 /* Parse and map our Configuration Space windows */
235 err = gen_pci_parse_map_cfg_windows(pci);
237 gen_pci_release_of_pci_ranges(pci);
241 /* Do not reassign resources if probe only */
242 if (!pci_has_flag(PCI_PROBE_ONLY))
243 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
246 bus = pci_scan_root_bus(dev, pci->cfg.bus_range->start,
247 &pci->cfg.ops->ops, pci, &pci->resources);
249 dev_err(dev, "Scanning rootbus failed");
253 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
255 if (!pci_has_flag(PCI_PROBE_ONLY)) {
256 pci_bus_size_bridges(bus);
257 pci_bus_assign_resources(bus);
259 list_for_each_entry(child, &bus->children, node)
260 pcie_bus_configure_settings(child);
263 pci_bus_add_devices(bus);
267 static struct platform_driver gen_pci_driver = {
269 .name = "pci-host-generic",
270 .of_match_table = gen_pci_of_match,
272 .probe = gen_pci_probe,
274 module_platform_driver(gen_pci_driver);
276 MODULE_DESCRIPTION("Generic PCI host driver");
277 MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
278 MODULE_LICENSE("GPL v2");