Loading...
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.h>
13#include <linux/of_address.h>
14#include <linux/of_pci.h>
15#include <linux/pci-ecam.h>
16#include <linux/platform_device.h>
17
18static void gen_pci_unmap_cfg(void *ptr)
19{
20 pci_ecam_free((struct pci_config_window *)ptr);
21}
22
23static 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_entry *bus;
29 struct pci_config_window *cfg;
30
31 err = of_address_to_resource(dev->of_node, 0, &cfgres);
32 if (err) {
33 dev_err(dev, "missing \"reg\" property\n");
34 return ERR_PTR(err);
35 }
36
37 bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
38 if (!bus)
39 return ERR_PTR(-ENODEV);
40
41 cfg = pci_ecam_create(dev, &cfgres, bus->res, ops);
42 if (IS_ERR(cfg))
43 return cfg;
44
45 err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
46 if (err)
47 return ERR_PTR(err);
48
49 return cfg;
50}
51
52int pci_host_common_probe(struct platform_device *pdev)
53{
54 struct device *dev = &pdev->dev;
55 struct pci_host_bridge *bridge;
56 struct pci_config_window *cfg;
57 const struct pci_ecam_ops *ops;
58
59 ops = of_device_get_match_data(&pdev->dev);
60 if (!ops)
61 return -ENODEV;
62
63 bridge = devm_pci_alloc_host_bridge(dev, 0);
64 if (!bridge)
65 return -ENOMEM;
66
67 platform_set_drvdata(pdev, bridge);
68
69 of_pci_check_probe_only();
70
71 /* Parse and map our Configuration Space windows */
72 cfg = gen_pci_init(dev, bridge, ops);
73 if (IS_ERR(cfg))
74 return PTR_ERR(cfg);
75
76 /* Do not reassign resources if probe only */
77 if (!pci_has_flag(PCI_PROBE_ONLY))
78 pci_add_flags(PCI_REASSIGN_ALL_BUS);
79
80 bridge->sysdata = cfg;
81 bridge->ops = (struct pci_ops *)&ops->pci_ops;
82 bridge->msi_domain = true;
83
84 return pci_host_probe(bridge);
85}
86EXPORT_SYMBOL_GPL(pci_host_common_probe);
87
88void pci_host_common_remove(struct platform_device *pdev)
89{
90 struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
91
92 pci_lock_rescan_remove();
93 pci_stop_root_bus(bridge->bus);
94 pci_remove_root_bus(bridge->bus);
95 pci_unlock_rescan_remove();
96}
97EXPORT_SYMBOL_GPL(pci_host_common_remove);
98
99MODULE_LICENSE("GPL v2");
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/of_address.h>
12#include <linux/of_pci.h>
13#include <linux/pci-ecam.h>
14#include <linux/platform_device.h>
15
16static void gen_pci_unmap_cfg(void *ptr)
17{
18 pci_ecam_free((struct pci_config_window *)ptr);
19}
20
21static struct pci_config_window *gen_pci_init(struct device *dev,
22 struct list_head *resources, struct pci_ecam_ops *ops)
23{
24 int err;
25 struct resource cfgres;
26 struct resource *bus_range = NULL;
27 struct pci_config_window *cfg;
28
29 /* Parse our PCI ranges and request their resources */
30 err = pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
31 if (err)
32 return ERR_PTR(err);
33
34 err = of_address_to_resource(dev->of_node, 0, &cfgres);
35 if (err) {
36 dev_err(dev, "missing \"reg\" property\n");
37 goto err_out;
38 }
39
40 cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
41 if (IS_ERR(cfg)) {
42 err = PTR_ERR(cfg);
43 goto err_out;
44 }
45
46 err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
47 if (err) {
48 goto err_out;
49 }
50 return cfg;
51
52err_out:
53 pci_free_resource_list(resources);
54 return ERR_PTR(err);
55}
56
57int pci_host_common_probe(struct platform_device *pdev,
58 struct pci_ecam_ops *ops)
59{
60 struct device *dev = &pdev->dev;
61 struct pci_host_bridge *bridge;
62 struct pci_config_window *cfg;
63 struct list_head resources;
64 int ret;
65
66 bridge = devm_pci_alloc_host_bridge(dev, 0);
67 if (!bridge)
68 return -ENOMEM;
69
70 of_pci_check_probe_only();
71
72 /* Parse and map our Configuration Space windows */
73 cfg = gen_pci_init(dev, &resources, ops);
74 if (IS_ERR(cfg))
75 return PTR_ERR(cfg);
76
77 /* Do not reassign resources if probe only */
78 if (!pci_has_flag(PCI_PROBE_ONLY))
79 pci_add_flags(PCI_REASSIGN_ALL_BUS);
80
81 list_splice_init(&resources, &bridge->windows);
82 bridge->dev.parent = dev;
83 bridge->sysdata = cfg;
84 bridge->busnr = cfg->busr.start;
85 bridge->ops = &ops->pci_ops;
86 bridge->map_irq = of_irq_parse_and_map_pci;
87 bridge->swizzle_irq = pci_common_swizzle;
88
89 ret = pci_host_probe(bridge);
90 if (ret < 0) {
91 pci_free_resource_list(&resources);
92 return ret;
93 }
94
95 platform_set_drvdata(pdev, bridge->bus);
96 return 0;
97}
98
99int pci_host_common_remove(struct platform_device *pdev)
100{
101 struct pci_bus *bus = platform_get_drvdata(pdev);
102
103 pci_lock_rescan_remove();
104 pci_stop_root_bus(bus);
105 pci_remove_root_bus(bus);
106 pci_unlock_rescan_remove();
107
108 return 0;
109}