xref: /linux/drivers/pci/controller/pci-host-common.c (revision 669cbc708122fc7a02282058a09f096200cee090)
16e0832faSShawn Lin // SPDX-License-Identifier: GPL-2.0
26e0832faSShawn Lin /*
36e0832faSShawn Lin  * Generic PCI host driver common code
46e0832faSShawn Lin  *
56e0832faSShawn Lin  * Copyright (C) 2014 ARM Limited
66e0832faSShawn Lin  *
76e0832faSShawn Lin  * Author: Will Deacon <will.deacon@arm.com>
86e0832faSShawn Lin  */
96e0832faSShawn Lin 
106e0832faSShawn Lin #include <linux/kernel.h>
110c59c06aSRob Herring #include <linux/module.h>
126e0832faSShawn Lin #include <linux/of_address.h>
13b2f75a41SRob Herring #include <linux/of_device.h>
146e0832faSShawn Lin #include <linux/of_pci.h>
156e0832faSShawn Lin #include <linux/pci-ecam.h>
166e0832faSShawn Lin #include <linux/platform_device.h>
176e0832faSShawn Lin 
186e0832faSShawn Lin static void gen_pci_unmap_cfg(void *ptr)
196e0832faSShawn Lin {
206e0832faSShawn Lin 	pci_ecam_free((struct pci_config_window *)ptr);
216e0832faSShawn Lin }
226e0832faSShawn Lin 
236e0832faSShawn Lin static struct pci_config_window *gen_pci_init(struct device *dev,
24e63434f4SRob Herring 		struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops)
256e0832faSShawn Lin {
266e0832faSShawn Lin 	int err;
276e0832faSShawn Lin 	struct resource cfgres;
28*669cbc70SRob Herring 	struct resource_entry *bus;
296e0832faSShawn Lin 	struct pci_config_window *cfg;
306e0832faSShawn Lin 
316e0832faSShawn Lin 	err = of_address_to_resource(dev->of_node, 0, &cfgres);
326e0832faSShawn Lin 	if (err) {
336e0832faSShawn Lin 		dev_err(dev, "missing \"reg\" property\n");
34e63434f4SRob Herring 		return ERR_PTR(err);
356e0832faSShawn Lin 	}
366e0832faSShawn Lin 
37*669cbc70SRob Herring 	bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
38*669cbc70SRob Herring 	if (!bus)
39*669cbc70SRob Herring 		return ERR_PTR(-ENODEV);
40*669cbc70SRob Herring 
41*669cbc70SRob Herring 	cfg = pci_ecam_create(dev, &cfgres, bus->res, ops);
42e63434f4SRob Herring 	if (IS_ERR(cfg))
436e0832faSShawn Lin 		return cfg;
446e0832faSShawn Lin 
45e63434f4SRob Herring 	err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
46e63434f4SRob Herring 	if (err)
476e0832faSShawn Lin 		return ERR_PTR(err);
48e63434f4SRob Herring 
49e63434f4SRob Herring 	return cfg;
506e0832faSShawn Lin }
516e0832faSShawn Lin 
52b2f75a41SRob Herring int pci_host_common_probe(struct platform_device *pdev)
536e0832faSShawn Lin {
546e0832faSShawn Lin 	struct device *dev = &pdev->dev;
556e0832faSShawn Lin 	struct pci_host_bridge *bridge;
566e0832faSShawn Lin 	struct pci_config_window *cfg;
57b2f75a41SRob Herring 	const struct pci_ecam_ops *ops;
586e0832faSShawn Lin 
59b2f75a41SRob Herring 	ops = of_device_get_match_data(&pdev->dev);
60b2f75a41SRob Herring 	if (!ops)
61b2f75a41SRob Herring 		return -ENODEV;
62b2f75a41SRob Herring 
636e0832faSShawn Lin 	bridge = devm_pci_alloc_host_bridge(dev, 0);
646e0832faSShawn Lin 	if (!bridge)
656e0832faSShawn Lin 		return -ENOMEM;
666e0832faSShawn Lin 
676e0832faSShawn Lin 	of_pci_check_probe_only();
686e0832faSShawn Lin 
696e0832faSShawn Lin 	/* Parse and map our Configuration Space windows */
70e63434f4SRob Herring 	cfg = gen_pci_init(dev, bridge, ops);
716e0832faSShawn Lin 	if (IS_ERR(cfg))
726e0832faSShawn Lin 		return PTR_ERR(cfg);
736e0832faSShawn Lin 
746e0832faSShawn Lin 	/* Do not reassign resources if probe only */
756e0832faSShawn Lin 	if (!pci_has_flag(PCI_PROBE_ONLY))
766e0832faSShawn Lin 		pci_add_flags(PCI_REASSIGN_ALL_BUS);
776e0832faSShawn Lin 
786e0832faSShawn Lin 	bridge->sysdata = cfg;
790b104773SRob Herring 	bridge->ops = (struct pci_ops *)&ops->pci_ops;
806e0832faSShawn Lin 	bridge->map_irq = of_irq_parse_and_map_pci;
816e0832faSShawn Lin 	bridge->swizzle_irq = pci_common_swizzle;
826e0832faSShawn Lin 
83e63434f4SRob Herring 	platform_set_drvdata(pdev, bridge);
846e0832faSShawn Lin 
85e63434f4SRob Herring 	return pci_host_probe(bridge);
866e0832faSShawn Lin }
870c59c06aSRob Herring EXPORT_SYMBOL_GPL(pci_host_common_probe);
886e0832faSShawn Lin 
896e0832faSShawn Lin int pci_host_common_remove(struct platform_device *pdev)
906e0832faSShawn Lin {
91e63434f4SRob Herring 	struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
926e0832faSShawn Lin 
936e0832faSShawn Lin 	pci_lock_rescan_remove();
94e63434f4SRob Herring 	pci_stop_root_bus(bridge->bus);
95e63434f4SRob Herring 	pci_remove_root_bus(bridge->bus);
966e0832faSShawn Lin 	pci_unlock_rescan_remove();
976e0832faSShawn Lin 
986e0832faSShawn Lin 	return 0;
996e0832faSShawn Lin }
1000c59c06aSRob Herring EXPORT_SYMBOL_GPL(pci_host_common_remove);
1010c59c06aSRob Herring 
1020c59c06aSRob Herring MODULE_LICENSE("GPL v2");
103