2 * Generic PCI host driver common code
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2014 ARM Limited
18 * Author: Will Deacon <will.deacon@arm.com>
21 #include <linux/kernel.h>
22 #include <linux/of_address.h>
23 #include <linux/of_pci.h>
24 #include <linux/pci-ecam.h>
25 #include <linux/platform_device.h>
27 static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
28 struct list_head *resources, struct resource **bus_range)
30 int err, res_valid = 0;
31 struct device_node *np = dev->of_node;
32 resource_size_t iobase;
33 struct resource_entry *win, *tmp;
35 err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
39 err = devm_request_pci_bus_resources(dev, resources);
43 resource_list_for_each_entry_safe(win, tmp, resources) {
44 struct resource *res = win->res;
46 switch (resource_type(res)) {
48 err = devm_pci_remap_iospace(dev, res, iobase);
50 dev_warn(dev, "error %d: failed to map resource %pR\n",
52 resource_list_destroy_entry(win);
56 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
67 dev_err(dev, "non-prefetchable memory resource required\n");
71 static void gen_pci_unmap_cfg(void *ptr)
73 pci_ecam_free((struct pci_config_window *)ptr);
76 static struct pci_config_window *gen_pci_init(struct device *dev,
77 struct list_head *resources, struct pci_ecam_ops *ops)
80 struct resource cfgres;
81 struct resource *bus_range = NULL;
82 struct pci_config_window *cfg;
84 /* Parse our PCI ranges and request their resources */
85 err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
89 err = of_address_to_resource(dev->of_node, 0, &cfgres);
91 dev_err(dev, "missing \"reg\" property\n");
95 cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
101 err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
103 gen_pci_unmap_cfg(cfg);
109 pci_free_resource_list(resources);
113 int pci_host_common_probe(struct platform_device *pdev,
114 struct pci_ecam_ops *ops)
117 struct device *dev = &pdev->dev;
118 struct device_node *np = dev->of_node;
119 struct pci_bus *bus, *child;
120 struct pci_config_window *cfg;
121 struct list_head resources;
123 type = of_get_property(np, "device_type", NULL);
124 if (!type || strcmp(type, "pci")) {
125 dev_err(dev, "invalid \"device_type\" %s\n", type);
129 of_pci_check_probe_only();
131 /* Parse and map our Configuration Space windows */
132 INIT_LIST_HEAD(&resources);
133 cfg = gen_pci_init(dev, &resources, ops);
137 /* Do not reassign resources if probe only */
138 if (!pci_has_flag(PCI_PROBE_ONLY))
139 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
141 bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg,
144 dev_err(dev, "Scanning rootbus failed");
148 pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
151 * We insert PCI resources into the iomem_resource and
152 * ioport_resource trees in either pci_bus_claim_resources()
153 * or pci_bus_assign_resources().
155 if (pci_has_flag(PCI_PROBE_ONLY)) {
156 pci_bus_claim_resources(bus);
158 pci_bus_size_bridges(bus);
159 pci_bus_assign_resources(bus);
161 list_for_each_entry(child, &bus->children, node)
162 pcie_bus_configure_settings(child);
165 pci_bus_add_devices(bus);