]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/pci/host/pci-host-common.c
PCI: generic: Request host bridge window resources with core function
[karo-tx-linux.git] / drivers / pci / host / pci-host-common.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright (C) 2014 ARM Limited
15  *
16  * Author: Will Deacon <will.deacon@arm.com>
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_pci.h>
23 #include <linux/platform_device.h>
24
25 #include "../ecam.h"
26
27 static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
28                        struct list_head *resources, struct resource **bus_range)
29 {
30         int err, res_valid = 0;
31         struct device_node *np = dev->of_node;
32         resource_size_t iobase;
33         struct resource_entry *win;
34
35         err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
36         if (err)
37                 return err;
38
39         err = devm_request_pci_bus_resources(dev, resources);
40         if (err)
41                 goto out_release_res;
42
43         resource_list_for_each_entry(win, resources) {
44                 struct resource *res = win->res;
45
46                 switch (resource_type(res)) {
47                 case IORESOURCE_IO:
48                         err = pci_remap_iospace(res, iobase);
49                         if (err) {
50                                 dev_warn(dev, "error %d: failed to map resource %pR\n",
51                                          err, res);
52                                 continue;
53                         }
54                         break;
55                 case IORESOURCE_MEM:
56                         res_valid |= !(res->flags & IORESOURCE_PREFETCH);
57                         break;
58                 case IORESOURCE_BUS:
59                         *bus_range = res;
60                 default:
61                         continue;
62                 }
63         }
64
65         if (!res_valid) {
66                 dev_err(dev, "non-prefetchable memory resource required\n");
67                 err = -EINVAL;
68                 goto out_release_res;
69         }
70
71         return 0;
72
73 out_release_res:
74         return err;
75 }
76
77 static void gen_pci_unmap_cfg(void *ptr)
78 {
79         pci_ecam_free((struct pci_config_window *)ptr);
80 }
81
82 static struct pci_config_window *gen_pci_init(struct device *dev,
83                 struct list_head *resources, struct pci_ecam_ops *ops)
84 {
85         int err;
86         struct resource cfgres;
87         struct resource *bus_range = NULL;
88         struct pci_config_window *cfg;
89
90         /* Parse our PCI ranges and request their resources */
91         err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
92         if (err)
93                 goto err_out;
94
95         err = of_address_to_resource(dev->of_node, 0, &cfgres);
96         if (err) {
97                 dev_err(dev, "missing \"reg\" property\n");
98                 goto err_out;
99         }
100
101         cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
102         if (IS_ERR(cfg)) {
103                 err = PTR_ERR(cfg);
104                 goto err_out;
105         }
106
107         err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
108         if (err) {
109                 gen_pci_unmap_cfg(cfg);
110                 goto err_out;
111         }
112         return cfg;
113
114 err_out:
115         pci_free_resource_list(resources);
116         return ERR_PTR(err);
117 }
118
119 int pci_host_common_probe(struct platform_device *pdev,
120                           struct pci_ecam_ops *ops)
121 {
122         const char *type;
123         struct device *dev = &pdev->dev;
124         struct device_node *np = dev->of_node;
125         struct pci_bus *bus, *child;
126         struct pci_config_window *cfg;
127         struct list_head resources;
128
129         type = of_get_property(np, "device_type", NULL);
130         if (!type || strcmp(type, "pci")) {
131                 dev_err(dev, "invalid \"device_type\" %s\n", type);
132                 return -EINVAL;
133         }
134
135         of_pci_check_probe_only();
136
137         /* Parse and map our Configuration Space windows */
138         INIT_LIST_HEAD(&resources);
139         cfg = gen_pci_init(dev, &resources, ops);
140         if (IS_ERR(cfg))
141                 return PTR_ERR(cfg);
142
143         /* Do not reassign resources if probe only */
144         if (!pci_has_flag(PCI_PROBE_ONLY))
145                 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
146
147         bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg,
148                                 &resources);
149         if (!bus) {
150                 dev_err(dev, "Scanning rootbus failed");
151                 return -ENODEV;
152         }
153
154         pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
155
156         if (!pci_has_flag(PCI_PROBE_ONLY)) {
157                 pci_bus_size_bridges(bus);
158                 pci_bus_assign_resources(bus);
159
160                 list_for_each_entry(child, &bus->children, node)
161                         pcie_bus_configure_settings(child);
162         }
163
164         pci_bus_add_devices(bus);
165         return 0;
166 }
167
168 MODULE_DESCRIPTION("Generic PCI host driver common code");
169 MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
170 MODULE_LICENSE("GPL v2");