1 // SPDX-License-Identifier: GPL-2.0
3 * Generic PCI host driver common code
5 * Copyright (C) 2014 ARM Limited
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_device.h>
14 #include <linux/of_pci.h>
15 #include <linux/pci-ecam.h>
16 #include <linux/platform_device.h>
18 static void gen_pci_unmap_cfg(void *ptr)
20 pci_ecam_free((struct pci_config_window *)ptr);
23 static struct pci_config_window *gen_pci_init(struct device *dev,
24 struct list_head *resources, const struct pci_ecam_ops *ops)
27 struct resource cfgres;
28 struct resource *bus_range = NULL;
29 struct pci_config_window *cfg;
31 /* Parse our PCI ranges and request their resources */
32 err = pci_parse_request_of_pci_ranges(dev, resources, NULL, &bus_range);
36 err = of_address_to_resource(dev->of_node, 0, &cfgres);
38 dev_err(dev, "missing \"reg\" property\n");
42 cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
48 err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
55 pci_free_resource_list(resources);
59 int pci_host_common_probe(struct platform_device *pdev)
61 struct device *dev = &pdev->dev;
62 struct pci_host_bridge *bridge;
63 struct pci_config_window *cfg;
64 struct list_head resources;
65 const struct pci_ecam_ops *ops;
68 ops = of_device_get_match_data(&pdev->dev);
72 bridge = devm_pci_alloc_host_bridge(dev, 0);
76 of_pci_check_probe_only();
78 /* Parse and map our Configuration Space windows */
79 cfg = gen_pci_init(dev, &resources, ops);
83 /* Do not reassign resources if probe only */
84 if (!pci_has_flag(PCI_PROBE_ONLY))
85 pci_add_flags(PCI_REASSIGN_ALL_BUS);
87 list_splice_init(&resources, &bridge->windows);
88 bridge->dev.parent = dev;
89 bridge->sysdata = cfg;
90 bridge->busnr = cfg->busr.start;
91 bridge->ops = (struct pci_ops *)&ops->pci_ops;
92 bridge->map_irq = of_irq_parse_and_map_pci;
93 bridge->swizzle_irq = pci_common_swizzle;
95 ret = pci_host_probe(bridge);
97 pci_free_resource_list(&resources);
101 platform_set_drvdata(pdev, bridge->bus);
104 EXPORT_SYMBOL_GPL(pci_host_common_probe);
106 int pci_host_common_remove(struct platform_device *pdev)
108 struct pci_bus *bus = platform_get_drvdata(pdev);
110 pci_lock_rescan_remove();
111 pci_stop_root_bus(bus);
112 pci_remove_root_bus(bus);
113 pci_unlock_rescan_remove();
117 EXPORT_SYMBOL_GPL(pci_host_common_remove);
119 MODULE_LICENSE("GPL v2");