0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/of_address.h>
0013 #include <linux/of_device.h>
0014 #include <linux/of_pci.h>
0015 #include <linux/pci-ecam.h>
0016 #include <linux/platform_device.h>
0017
0018 static void gen_pci_unmap_cfg(void *ptr)
0019 {
0020 pci_ecam_free((struct pci_config_window *)ptr);
0021 }
0022
0023 static struct pci_config_window *gen_pci_init(struct device *dev,
0024 struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops)
0025 {
0026 int err;
0027 struct resource cfgres;
0028 struct resource_entry *bus;
0029 struct pci_config_window *cfg;
0030
0031 err = of_address_to_resource(dev->of_node, 0, &cfgres);
0032 if (err) {
0033 dev_err(dev, "missing \"reg\" property\n");
0034 return ERR_PTR(err);
0035 }
0036
0037 bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
0038 if (!bus)
0039 return ERR_PTR(-ENODEV);
0040
0041 cfg = pci_ecam_create(dev, &cfgres, bus->res, ops);
0042 if (IS_ERR(cfg))
0043 return cfg;
0044
0045 err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
0046 if (err)
0047 return ERR_PTR(err);
0048
0049 return cfg;
0050 }
0051
0052 int pci_host_common_probe(struct platform_device *pdev)
0053 {
0054 struct device *dev = &pdev->dev;
0055 struct pci_host_bridge *bridge;
0056 struct pci_config_window *cfg;
0057 const struct pci_ecam_ops *ops;
0058
0059 ops = of_device_get_match_data(&pdev->dev);
0060 if (!ops)
0061 return -ENODEV;
0062
0063 bridge = devm_pci_alloc_host_bridge(dev, 0);
0064 if (!bridge)
0065 return -ENOMEM;
0066
0067 platform_set_drvdata(pdev, bridge);
0068
0069 of_pci_check_probe_only();
0070
0071
0072 cfg = gen_pci_init(dev, bridge, ops);
0073 if (IS_ERR(cfg))
0074 return PTR_ERR(cfg);
0075
0076
0077 if (!pci_has_flag(PCI_PROBE_ONLY))
0078 pci_add_flags(PCI_REASSIGN_ALL_BUS);
0079
0080 bridge->sysdata = cfg;
0081 bridge->ops = (struct pci_ops *)&ops->pci_ops;
0082 bridge->msi_domain = true;
0083
0084 return pci_host_probe(bridge);
0085 }
0086 EXPORT_SYMBOL_GPL(pci_host_common_probe);
0087
0088 int pci_host_common_remove(struct platform_device *pdev)
0089 {
0090 struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
0091
0092 pci_lock_rescan_remove();
0093 pci_stop_root_bus(bridge->bus);
0094 pci_remove_root_bus(bridge->bus);
0095 pci_unlock_rescan_remove();
0096
0097 return 0;
0098 }
0099 EXPORT_SYMBOL_GPL(pci_host_common_remove);
0100
0101 MODULE_LICENSE("GPL v2");