1 // SPDX-License-Identifier: GPL-2.0
3 * Generic PCI host driver common code
5 * Copyright (C) 2014 ARM Limited
7 * Author: Will Deacon <will.deacon@arm.com>
10 #include <linux/kernel.h>
11 #include <linux/module.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>
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 pci_host_bridge
*bridge
, const struct pci_ecam_ops
*ops
)
27 struct resource cfgres
;
28 struct resource_entry
*bus
;
29 struct pci_config_window
*cfg
;
31 err
= of_address_to_resource(dev
->of_node
, 0, &cfgres
);
33 dev_err(dev
, "missing \"reg\" property\n");
37 bus
= resource_list_first_type(&bridge
->windows
, IORESOURCE_BUS
);
39 return ERR_PTR(-ENODEV
);
41 cfg
= pci_ecam_create(dev
, &cfgres
, bus
->res
, ops
);
45 err
= devm_add_action_or_reset(dev
, gen_pci_unmap_cfg
, cfg
);
52 int pci_host_common_probe(struct platform_device
*pdev
)
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
;
59 ops
= of_device_get_match_data(&pdev
->dev
);
63 bridge
= devm_pci_alloc_host_bridge(dev
, 0);
67 platform_set_drvdata(pdev
, bridge
);
69 of_pci_check_probe_only();
71 /* Parse and map our Configuration Space windows */
72 cfg
= gen_pci_init(dev
, bridge
, ops
);
76 bridge
->sysdata
= cfg
;
77 bridge
->ops
= (struct pci_ops
*)&ops
->pci_ops
;
78 bridge
->msi_domain
= true;
80 return pci_host_probe(bridge
);
82 EXPORT_SYMBOL_GPL(pci_host_common_probe
);
84 void pci_host_common_remove(struct platform_device
*pdev
)
86 struct pci_host_bridge
*bridge
= platform_get_drvdata(pdev
);
88 pci_lock_rescan_remove();
89 pci_stop_root_bus(bridge
->bus
);
90 pci_remove_root_bus(bridge
->bus
);
91 pci_unlock_rescan_remove();
93 EXPORT_SYMBOL_GPL(pci_host_common_remove
);
95 MODULE_DESCRIPTION("Generic PCI host common driver");
96 MODULE_LICENSE("GPL v2");