blob: 44a47d4f0b8f294fef5c2b2482b0a16856849f01 [file] [log] [blame]
David Daney4e64dbe2016-03-11 15:35:55 -06001/*
Paul Gortmaker4068bd12016-08-22 17:59:48 -04002 * Generic PCI host driver common code
3 *
David Daney4e64dbe2016-03-11 15:35:55 -06004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Copyright (C) 2014 ARM Limited
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 */
20
21#include <linux/kernel.h>
David Daney4e64dbe2016-03-11 15:35:55 -060022#include <linux/of_address.h>
23#include <linux/of_pci.h>
Jayachandran C80955f92016-06-10 21:55:09 +020024#include <linux/pci-ecam.h>
David Daney4e64dbe2016-03-11 15:35:55 -060025#include <linux/platform_device.h>
26
Jayachandran C1958e712016-05-11 17:34:46 -050027static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
28 struct list_head *resources, struct resource **bus_range)
David Daney4e64dbe2016-03-11 15:35:55 -060029{
30 int err, res_valid = 0;
David Daney4e64dbe2016-03-11 15:35:55 -060031 struct device_node *np = dev->of_node;
32 resource_size_t iobase;
Lorenzo Pieralisi43281ed2016-08-15 17:50:45 +010033 struct resource_entry *win, *tmp;
David Daney4e64dbe2016-03-11 15:35:55 -060034
Jayachandran C1958e712016-05-11 17:34:46 -050035 err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
David Daney4e64dbe2016-03-11 15:35:55 -060036 if (err)
37 return err;
38
Bjorn Helgaasb7f957a2016-05-31 12:05:05 -050039 err = devm_request_pci_bus_resources(dev, resources);
40 if (err)
Bjorn Helgaas5aa182a2016-05-28 18:28:51 -050041 return err;
Bjorn Helgaasb7f957a2016-05-31 12:05:05 -050042
Lorenzo Pieralisi43281ed2016-08-15 17:50:45 +010043 resource_list_for_each_entry_safe(win, tmp, resources) {
Bjorn Helgaasb7f957a2016-05-31 12:05:05 -050044 struct resource *res = win->res;
David Daney4e64dbe2016-03-11 15:35:55 -060045
46 switch (resource_type(res)) {
47 case IORESOURCE_IO:
David Daney4e64dbe2016-03-11 15:35:55 -060048 err = pci_remap_iospace(res, iobase);
Lorenzo Pieralisi43281ed2016-08-15 17:50:45 +010049 if (err) {
David Daney4e64dbe2016-03-11 15:35:55 -060050 dev_warn(dev, "error %d: failed to map resource %pR\n",
51 err, res);
Lorenzo Pieralisi43281ed2016-08-15 17:50:45 +010052 resource_list_destroy_entry(win);
53 }
David Daney4e64dbe2016-03-11 15:35:55 -060054 break;
55 case IORESOURCE_MEM:
David Daney4e64dbe2016-03-11 15:35:55 -060056 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
57 break;
58 case IORESOURCE_BUS:
Jayachandran C1958e712016-05-11 17:34:46 -050059 *bus_range = res;
Bjorn Helgaas5aa182a2016-05-28 18:28:51 -050060 break;
David Daney4e64dbe2016-03-11 15:35:55 -060061 }
David Daney4e64dbe2016-03-11 15:35:55 -060062 }
63
Bjorn Helgaas5aa182a2016-05-28 18:28:51 -050064 if (res_valid)
65 return 0;
David Daney4e64dbe2016-03-11 15:35:55 -060066
Bjorn Helgaas5aa182a2016-05-28 18:28:51 -050067 dev_err(dev, "non-prefetchable memory resource required\n");
68 return -EINVAL;
David Daney4e64dbe2016-03-11 15:35:55 -060069}
70
Jayachandran C1958e712016-05-11 17:34:46 -050071static void gen_pci_unmap_cfg(void *ptr)
72{
73 pci_ecam_free((struct pci_config_window *)ptr);
74}
75
76static struct pci_config_window *gen_pci_init(struct device *dev,
77 struct list_head *resources, struct pci_ecam_ops *ops)
David Daney4e64dbe2016-03-11 15:35:55 -060078{
79 int err;
Jayachandran C1958e712016-05-11 17:34:46 -050080 struct resource cfgres;
81 struct resource *bus_range = NULL;
82 struct pci_config_window *cfg;
David Daney4e64dbe2016-03-11 15:35:55 -060083
Jayachandran C1958e712016-05-11 17:34:46 -050084 /* Parse our PCI ranges and request their resources */
85 err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
86 if (err)
87 goto err_out;
88
89 err = of_address_to_resource(dev->of_node, 0, &cfgres);
David Daney4e64dbe2016-03-11 15:35:55 -060090 if (err) {
91 dev_err(dev, "missing \"reg\" property\n");
Jayachandran C1958e712016-05-11 17:34:46 -050092 goto err_out;
David Daney4e64dbe2016-03-11 15:35:55 -060093 }
94
Jayachandran C1958e712016-05-11 17:34:46 -050095 cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
96 if (IS_ERR(cfg)) {
97 err = PTR_ERR(cfg);
98 goto err_out;
David Daney4e64dbe2016-03-11 15:35:55 -060099 }
100
Jayachandran C1958e712016-05-11 17:34:46 -0500101 err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
102 if (err) {
103 gen_pci_unmap_cfg(cfg);
104 goto err_out;
105 }
106 return cfg;
107
108err_out:
109 pci_free_resource_list(resources);
110 return ERR_PTR(err);
David Daney4e64dbe2016-03-11 15:35:55 -0600111}
112
113int pci_host_common_probe(struct platform_device *pdev,
Jayachandran C1958e712016-05-11 17:34:46 -0500114 struct pci_ecam_ops *ops)
David Daney4e64dbe2016-03-11 15:35:55 -0600115{
David Daney4e64dbe2016-03-11 15:35:55 -0600116 const char *type;
117 struct device *dev = &pdev->dev;
118 struct device_node *np = dev->of_node;
119 struct pci_bus *bus, *child;
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -0500120 struct pci_host_bridge *bridge;
Jayachandran C1958e712016-05-11 17:34:46 -0500121 struct pci_config_window *cfg;
122 struct list_head resources;
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -0500123 int ret;
124
125 bridge = devm_pci_alloc_host_bridge(dev, 0);
126 if (!bridge)
127 return -ENOMEM;
David Daney4e64dbe2016-03-11 15:35:55 -0600128
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
David Daney4e64dbe2016-03-11 15:35:55 -0600137 /* Parse and map our Configuration Space windows */
Jayachandran C1958e712016-05-11 17:34:46 -0500138 INIT_LIST_HEAD(&resources);
139 cfg = gen_pci_init(dev, &resources, ops);
140 if (IS_ERR(cfg))
141 return PTR_ERR(cfg);
David Daney4e64dbe2016-03-11 15:35:55 -0600142
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
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -0500147 list_splice_init(&resources, &bridge->windows);
148 bridge->dev.parent = dev;
149 bridge->sysdata = cfg;
150 bridge->busnr = cfg->busr.start;
151 bridge->ops = &ops->pci_ops;
Lorenzo Pieralisi6982a062017-06-28 15:14:09 -0500152 bridge->map_irq = of_irq_parse_and_map_pci;
153 bridge->swizzle_irq = pci_common_swizzle;
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -0500154
155 ret = pci_scan_root_bus_bridge(bridge);
156 if (ret < 0) {
157 dev_err(dev, "Scanning root bridge failed");
158 return ret;
David Daney4e64dbe2016-03-11 15:35:55 -0600159 }
160
Lorenzo Pieralisi4246a862017-06-28 15:14:00 -0500161 bus = bridge->bus;
162
Lorenzo Pieralisidcce0f1532016-06-08 12:04:48 +0100163 /*
164 * We insert PCI resources into the iomem_resource and
165 * ioport_resource trees in either pci_bus_claim_resources()
166 * or pci_bus_assign_resources().
167 */
168 if (pci_has_flag(PCI_PROBE_ONLY)) {
169 pci_bus_claim_resources(bus);
170 } else {
David Daney4e64dbe2016-03-11 15:35:55 -0600171 pci_bus_size_bridges(bus);
172 pci_bus_assign_resources(bus);
173
174 list_for_each_entry(child, &bus->children, node)
175 pcie_bus_configure_settings(child);
176 }
177
178 pci_bus_add_devices(bus);
179 return 0;
180}