xref: /linux/drivers/pci/controller/pci-host-common.c (revision 791c9f143c77f847232b46ee9c1c990f60825c8e)
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;
28669cbc70SRob 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 
37669cbc70SRob Herring 	bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
38669cbc70SRob Herring 	if (!bus)
39669cbc70SRob Herring 		return ERR_PTR(-ENODEV);
40669cbc70SRob Herring 
41669cbc70SRob 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 
67*791c9f14SDaire McNamara 	platform_set_drvdata(pdev, bridge);
68*791c9f14SDaire McNamara 
696e0832faSShawn Lin 	of_pci_check_probe_only();
706e0832faSShawn Lin 
716e0832faSShawn Lin 	/* Parse and map our Configuration Space windows */
72e63434f4SRob Herring 	cfg = gen_pci_init(dev, bridge, ops);
736e0832faSShawn Lin 	if (IS_ERR(cfg))
746e0832faSShawn Lin 		return PTR_ERR(cfg);
756e0832faSShawn Lin 
766e0832faSShawn Lin 	/* Do not reassign resources if probe only */
776e0832faSShawn Lin 	if (!pci_has_flag(PCI_PROBE_ONLY))
786e0832faSShawn Lin 		pci_add_flags(PCI_REASSIGN_ALL_BUS);
796e0832faSShawn Lin 
806e0832faSShawn Lin 	bridge->sysdata = cfg;
810b104773SRob Herring 	bridge->ops = (struct pci_ops *)&ops->pci_ops;
826e0832faSShawn Lin 
83e63434f4SRob Herring 	return pci_host_probe(bridge);
846e0832faSShawn Lin }
850c59c06aSRob Herring EXPORT_SYMBOL_GPL(pci_host_common_probe);
866e0832faSShawn Lin 
876e0832faSShawn Lin int pci_host_common_remove(struct platform_device *pdev)
886e0832faSShawn Lin {
89e63434f4SRob Herring 	struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
906e0832faSShawn Lin 
916e0832faSShawn Lin 	pci_lock_rescan_remove();
92e63434f4SRob Herring 	pci_stop_root_bus(bridge->bus);
93e63434f4SRob Herring 	pci_remove_root_bus(bridge->bus);
946e0832faSShawn Lin 	pci_unlock_rescan_remove();
956e0832faSShawn Lin 
966e0832faSShawn Lin 	return 0;
976e0832faSShawn Lin }
980c59c06aSRob Herring EXPORT_SYMBOL_GPL(pci_host_common_remove);
990c59c06aSRob Herring 
1000c59c06aSRob Herring MODULE_LICENSE("GPL v2");
101