PCI: host-common: Use struct pci_host_bridge.windows list directly
[platform/kernel/linux-starfive.git] / drivers / pci / controller / pci-host-common.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic PCI host driver common code
4  *
5  * Copyright (C) 2014 ARM Limited
6  *
7  * Author: Will Deacon <will.deacon@arm.com>
8  */
9
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>
17
18 static void gen_pci_unmap_cfg(void *ptr)
19 {
20         pci_ecam_free((struct pci_config_window *)ptr);
21 }
22
23 static struct pci_config_window *gen_pci_init(struct device *dev,
24                 struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops)
25 {
26         int err;
27         struct resource cfgres;
28         struct resource *bus_range = NULL;
29         struct pci_config_window *cfg;
30
31         /* Parse our PCI ranges and request their resources */
32         err = pci_parse_request_of_pci_ranges(dev, &bridge->windows, NULL, &bus_range);
33         if (err)
34                 return ERR_PTR(err);
35
36         err = of_address_to_resource(dev->of_node, 0, &cfgres);
37         if (err) {
38                 dev_err(dev, "missing \"reg\" property\n");
39                 return ERR_PTR(err);
40         }
41
42         cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
43         if (IS_ERR(cfg))
44                 return cfg;
45
46         err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
47         if (err)
48                 return ERR_PTR(err);
49
50         return cfg;
51 }
52
53 int pci_host_common_probe(struct platform_device *pdev)
54 {
55         struct device *dev = &pdev->dev;
56         struct pci_host_bridge *bridge;
57         struct pci_config_window *cfg;
58         const struct pci_ecam_ops *ops;
59
60         ops = of_device_get_match_data(&pdev->dev);
61         if (!ops)
62                 return -ENODEV;
63
64         bridge = devm_pci_alloc_host_bridge(dev, 0);
65         if (!bridge)
66                 return -ENOMEM;
67
68         of_pci_check_probe_only();
69
70         /* Parse and map our Configuration Space windows */
71         cfg = gen_pci_init(dev, bridge, ops);
72         if (IS_ERR(cfg))
73                 return PTR_ERR(cfg);
74
75         /* Do not reassign resources if probe only */
76         if (!pci_has_flag(PCI_PROBE_ONLY))
77                 pci_add_flags(PCI_REASSIGN_ALL_BUS);
78
79         bridge->dev.parent = dev;
80         bridge->sysdata = cfg;
81         bridge->busnr = cfg->busr.start;
82         bridge->ops = (struct pci_ops *)&ops->pci_ops;
83         bridge->map_irq = of_irq_parse_and_map_pci;
84         bridge->swizzle_irq = pci_common_swizzle;
85
86         platform_set_drvdata(pdev, bridge);
87
88         return pci_host_probe(bridge);
89 }
90 EXPORT_SYMBOL_GPL(pci_host_common_probe);
91
92 int pci_host_common_remove(struct platform_device *pdev)
93 {
94         struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
95
96         pci_lock_rescan_remove();
97         pci_stop_root_bus(bridge->bus);
98         pci_remove_root_bus(bridge->bus);
99         pci_unlock_rescan_remove();
100
101         return 0;
102 }
103 EXPORT_SYMBOL_GPL(pci_host_common_remove);
104
105 MODULE_LICENSE("GPL v2");