blob: c742881b5061a7be0cdae6b485c10b9d3816fa84 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
David Daney4e64dbe2016-03-11 15:35:55 -06002/*
Paul Gortmaker4068bd12016-08-22 17:59:48 -04003 * Generic PCI host driver common code
4 *
David Daney4e64dbe2016-03-11 15:35:55 -06005 * Copyright (C) 2014 ARM Limited
6 *
7 * Author: Will Deacon <will.deacon@arm.com>
8 */
9
10#include <linux/kernel.h>
David Daney4e64dbe2016-03-11 15:35:55 -060011#include <linux/of_address.h>
12#include <linux/of_pci.h>
Jayachandran C80955f92016-06-10 21:55:09 +020013#include <linux/pci-ecam.h>
David Daney4e64dbe2016-03-11 15:35:55 -060014#include <linux/platform_device.h>
15
Jayachandran C1958e712016-05-11 17:34:46 -050016static void gen_pci_unmap_cfg(void *ptr)
17{
18 pci_ecam_free((struct pci_config_window *)ptr);
19}
20
21static struct pci_config_window *gen_pci_init(struct device *dev,
22 struct list_head *resources, struct pci_ecam_ops *ops)
David Daney4e64dbe2016-03-11 15:35:55 -060023{
24 int err;
Jayachandran C1958e712016-05-11 17:34:46 -050025 struct resource cfgres;
26 struct resource *bus_range = NULL;
27 struct pci_config_window *cfg;
David Daney4e64dbe2016-03-11 15:35:55 -060028
Jayachandran C1958e712016-05-11 17:34:46 -050029 /* Parse our PCI ranges and request their resources */
Cyrille Pitchen3a8f77e2018-01-30 21:56:50 +010030 err = pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
Jayachandran C1958e712016-05-11 17:34:46 -050031 if (err)
Cyrille Pitchen3a8f77e2018-01-30 21:56:50 +010032 return ERR_PTR(err);
Jayachandran C1958e712016-05-11 17:34:46 -050033
34 err = of_address_to_resource(dev->of_node, 0, &cfgres);
David Daney4e64dbe2016-03-11 15:35:55 -060035 if (err) {
36 dev_err(dev, "missing \"reg\" property\n");
Jayachandran C1958e712016-05-11 17:34:46 -050037 goto err_out;
David Daney4e64dbe2016-03-11 15:35:55 -060038 }
39
Jayachandran C1958e712016-05-11 17:34:46 -050040 cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
41 if (IS_ERR(cfg)) {
42 err = PTR_ERR(cfg);
43 goto err_out;
David Daney4e64dbe2016-03-11 15:35:55 -060044 }
45
Jayachandran C1958e712016-05-11 17:34:46 -050046 err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
47 if (err) {
48 gen_pci_unmap_cfg(cfg);
49 goto err_out;
50 }
51 return cfg;
52
53err_out:
54 pci_free_resource_list(resources);
55 return ERR_PTR(err);
David Daney4e64dbe2016-03-11 15:35:55 -060056}
57
58int pci_host_common_probe(struct platform_device *pdev,
Jayachandran C1958e712016-05-11 17:34:46 -050059 struct pci_ecam_ops *ops)
David Daney4e64dbe2016-03-11 15:35:55 -060060{
David Daney4e64dbe2016-03-11 15:35:55 -060061 struct device *dev = &pdev->dev;
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -050062 struct pci_host_bridge *bridge;
Jayachandran C1958e712016-05-11 17:34:46 -050063 struct pci_config_window *cfg;
64 struct list_head resources;
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -050065 int ret;
66
67 bridge = devm_pci_alloc_host_bridge(dev, 0);
68 if (!bridge)
69 return -ENOMEM;
David Daney4e64dbe2016-03-11 15:35:55 -060070
David Daney4e64dbe2016-03-11 15:35:55 -060071 of_pci_check_probe_only();
72
David Daney4e64dbe2016-03-11 15:35:55 -060073 /* Parse and map our Configuration Space windows */
Jayachandran C1958e712016-05-11 17:34:46 -050074 cfg = gen_pci_init(dev, &resources, ops);
75 if (IS_ERR(cfg))
76 return PTR_ERR(cfg);
David Daney4e64dbe2016-03-11 15:35:55 -060077
78 /* Do not reassign resources if probe only */
79 if (!pci_has_flag(PCI_PROBE_ONLY))
Bjorn Helgaas71538842017-11-30 11:21:57 -060080 pci_add_flags(PCI_REASSIGN_ALL_BUS);
David Daney4e64dbe2016-03-11 15:35:55 -060081
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -050082 list_splice_init(&resources, &bridge->windows);
83 bridge->dev.parent = dev;
84 bridge->sysdata = cfg;
85 bridge->busnr = cfg->busr.start;
86 bridge->ops = &ops->pci_ops;
Lorenzo Pieralisi6982a062017-06-28 15:14:09 -050087 bridge->map_irq = of_irq_parse_and_map_pci;
88 bridge->swizzle_irq = pci_common_swizzle;
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -050089
Cyrille Pitchen49b8e3f2018-01-30 21:56:52 +010090 ret = pci_host_probe(bridge);
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -050091 if (ret < 0) {
Cyrille Pitchenc6dd8ec2018-01-30 21:56:51 +010092 pci_free_resource_list(&resources);
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -050093 return ret;
David Daney4e64dbe2016-03-11 15:35:55 -060094 }
95
Jan Kiszka01fcb7f2018-05-15 11:07:06 +020096 platform_set_drvdata(pdev, bridge->bus);
97 return 0;
98}
99
100int pci_host_common_remove(struct platform_device *pdev)
101{
102 struct pci_bus *bus = platform_get_drvdata(pdev);
103
104 pci_lock_rescan_remove();
105 pci_stop_root_bus(bus);
106 pci_remove_root_bus(bus);
107 pci_unlock_rescan_remove();
108
David Daney4e64dbe2016-03-11 15:35:55 -0600109 return 0;
110}