blob: 6ce34a1deecb2cd306f93fbd3b7ed113ef6f5cd7 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Generic PCI host driver common code
4 *
5 * Copyright (C) 2014 ARM Limited
6 *
7 * Author: Will Deacon <will.deacon@arm.com>
8 */
9
10#include <linux/kernel.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020011#include <linux/module.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000012#include <linux/of_address.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include <linux/of_device.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <linux/of_pci.h>
15#include <linux/pci-ecam.h>
16#include <linux/platform_device.h>
17
18static void gen_pci_unmap_cfg(void *ptr)
19{
20 pci_ecam_free((struct pci_config_window *)ptr);
21}
22
23static struct pci_config_window *gen_pci_init(struct device *dev,
Olivier Deprez157378f2022-04-04 15:47:50 +020024 struct pci_host_bridge *bridge, const struct pci_ecam_ops *ops)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025{
26 int err;
27 struct resource cfgres;
Olivier Deprez157378f2022-04-04 15:47:50 +020028 struct resource_entry *bus;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029 struct pci_config_window *cfg;
30
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000031 err = of_address_to_resource(dev->of_node, 0, &cfgres);
32 if (err) {
33 dev_err(dev, "missing \"reg\" property\n");
Olivier Deprez157378f2022-04-04 15:47:50 +020034 return ERR_PTR(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035 }
36
Olivier Deprez157378f2022-04-04 15:47:50 +020037 bus = resource_list_first_type(&bridge->windows, IORESOURCE_BUS);
38 if (!bus)
39 return ERR_PTR(-ENODEV);
40
41 cfg = pci_ecam_create(dev, &cfgres, bus->res, ops);
42 if (IS_ERR(cfg))
43 return cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044
David Brazdil0f672f62019-12-10 10:32:29 +000045 err = devm_add_action_or_reset(dev, gen_pci_unmap_cfg, cfg);
Olivier Deprez157378f2022-04-04 15:47:50 +020046 if (err)
47 return ERR_PTR(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
Olivier Deprez157378f2022-04-04 15:47:50 +020049 return cfg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050}
51
Olivier Deprez157378f2022-04-04 15:47:50 +020052int pci_host_common_probe(struct platform_device *pdev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 struct device *dev = &pdev->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000055 struct pci_host_bridge *bridge;
56 struct pci_config_window *cfg;
Olivier Deprez157378f2022-04-04 15:47:50 +020057 const struct pci_ecam_ops *ops;
58
59 ops = of_device_get_match_data(&pdev->dev);
60 if (!ops)
61 return -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062
63 bridge = devm_pci_alloc_host_bridge(dev, 0);
64 if (!bridge)
65 return -ENOMEM;
66
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 of_pci_check_probe_only();
68
69 /* Parse and map our Configuration Space windows */
Olivier Deprez157378f2022-04-04 15:47:50 +020070 cfg = gen_pci_init(dev, bridge, ops);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071 if (IS_ERR(cfg))
72 return PTR_ERR(cfg);
73
74 /* Do not reassign resources if probe only */
75 if (!pci_has_flag(PCI_PROBE_ONLY))
76 pci_add_flags(PCI_REASSIGN_ALL_BUS);
77
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078 bridge->sysdata = cfg;
Olivier Deprez157378f2022-04-04 15:47:50 +020079 bridge->ops = (struct pci_ops *)&ops->pci_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080
Olivier Deprez157378f2022-04-04 15:47:50 +020081 platform_set_drvdata(pdev, bridge);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000082
Olivier Deprez157378f2022-04-04 15:47:50 +020083 return pci_host_probe(bridge);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084}
Olivier Deprez157378f2022-04-04 15:47:50 +020085EXPORT_SYMBOL_GPL(pci_host_common_probe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086
87int pci_host_common_remove(struct platform_device *pdev)
88{
Olivier Deprez157378f2022-04-04 15:47:50 +020089 struct pci_host_bridge *bridge = platform_get_drvdata(pdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090
91 pci_lock_rescan_remove();
Olivier Deprez157378f2022-04-04 15:47:50 +020092 pci_stop_root_bus(bridge->bus);
93 pci_remove_root_bus(bridge->bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 pci_unlock_rescan_remove();
95
96 return 0;
97}
Olivier Deprez157378f2022-04-04 15:47:50 +020098EXPORT_SYMBOL_GPL(pci_host_common_remove);
99
100MODULE_LICENSE("GPL v2");