1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Common library for PCI host controller drivers
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
18 #include "pci-host-common.h"
19
gen_pci_unmap_cfg(void * ptr)20 static void gen_pci_unmap_cfg(void *ptr)
21 {
22 pci_ecam_free((struct pci_config_window *)ptr);
23 }
24
pci_host_common_ecam_create(struct device * dev,struct pci_host_bridge * bridge,const struct pci_ecam_ops * ops)25 struct pci_config_window *pci_host_common_ecam_create(struct device *dev,
26 struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops)
27 {
28 int err;
29 struct resource cfgres;
30 struct resource_entry *bus;
31 struct pci_config_window *cfg;
32
33 err = of_address_to_resource(dev->of_node, 0, &cfgres);
34 if (err) {
35 dev_err(dev, "missing \"reg\" property\n");
36 return ERR_PTR(err);
37 }
38
39 bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
40 if (!bus)
41 return ERR_PTR(-ENODEV);
42
43 cfg = pci_ecam_create(dev, &cfgres, bus->res, ops);
44 if (IS_ERR(cfg))
45 return cfg;
46
47 err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
48 if (err)
49 return ERR_PTR(err);
50
51 return cfg;
52 }
53 EXPORT_SYMBOL_GPL(pci_host_common_ecam_create);
54
pci_host_common_init(struct platform_device * pdev,const struct pci_ecam_ops * ops)55 int pci_host_common_init(struct platform_device *pdev,
56 const struct pci_ecam_ops *ops)
57 {
58 struct device *dev = &pdev->dev;
59 struct pci_host_bridge *bridge;
60 struct pci_config_window *cfg;
61
62 bridge = devm_pci_alloc_host_bridge(dev, 0);
63 if (!bridge)
64 return -ENOMEM;
65
66 of_pci_check_probe_only();
67
68 platform_set_drvdata(pdev, bridge);
69
70 /* Parse and map our Configuration Space windows */
71 cfg = pci_host_common_ecam_create(dev, bridge, ops);
72 if (IS_ERR(cfg))
73 return PTR_ERR(cfg);
74
75 bridge->sysdata = cfg;
76 bridge->ops = (struct pci_ops *)&ops->pci_ops;
77 bridge->enable_device = ops->enable_device;
78 bridge->disable_device = ops->disable_device;
79 bridge->msi_domain = true;
80
81 return pci_host_probe(bridge);
82 }
83 EXPORT_SYMBOL_GPL(pci_host_common_init);
84
pci_host_common_probe(struct platform_device * pdev)85 int pci_host_common_probe(struct platform_device *pdev)
86 {
87 const struct pci_ecam_ops *ops;
88
89 ops = of_device_get_match_data(&pdev->dev);
90 if (!ops)
91 return -ENODEV;
92
93 return pci_host_common_init(pdev, ops);
94 }
95 EXPORT_SYMBOL_GPL(pci_host_common_probe);
96
pci_host_common_remove(struct platform_device * pdev)97 void pci_host_common_remove(struct platform_device *pdev)
98 {
99 struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
100
101 pci_lock_rescan_remove();
102 pci_stop_root_bus(bridge->bus);
103 pci_remove_root_bus(bridge->bus);
104 pci_unlock_rescan_remove();
105 }
106 EXPORT_SYMBOL_GPL(pci_host_common_remove);
107
108 MODULE_DESCRIPTION("Common library for PCI host controller drivers");
109 MODULE_LICENSE("GPL v2");
110