Commit | Line | Data |
---|---|---|
4e64dbe2 DD |
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> | |
80955f9e | 23 | #include <linux/pci-ecam.h> |
4e64dbe2 DD |
24 | #include <linux/platform_device.h> |
25 | ||
1958e717 J |
26 | static int gen_pci_parse_request_of_pci_ranges(struct device *dev, |
27 | struct list_head *resources, struct resource **bus_range) | |
4e64dbe2 DD |
28 | { |
29 | int err, res_valid = 0; | |
4e64dbe2 DD |
30 | struct device_node *np = dev->of_node; |
31 | resource_size_t iobase; | |
32 | struct resource_entry *win; | |
33 | ||
1958e717 | 34 | err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase); |
4e64dbe2 DD |
35 | if (err) |
36 | return err; | |
37 | ||
b7f957ac BH |
38 | err = devm_request_pci_bus_resources(dev, resources); |
39 | if (err) | |
5aa182a2 | 40 | return err; |
b7f957ac | 41 | |
1958e717 | 42 | resource_list_for_each_entry(win, resources) { |
b7f957ac | 43 | struct resource *res = win->res; |
4e64dbe2 DD |
44 | |
45 | switch (resource_type(res)) { | |
46 | case IORESOURCE_IO: | |
4e64dbe2 | 47 | err = pci_remap_iospace(res, iobase); |
5aa182a2 | 48 | if (err) |
4e64dbe2 DD |
49 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
50 | err, res); | |
4e64dbe2 DD |
51 | break; |
52 | case IORESOURCE_MEM: | |
4e64dbe2 DD |
53 | res_valid |= !(res->flags & IORESOURCE_PREFETCH); |
54 | break; | |
55 | case IORESOURCE_BUS: | |
1958e717 | 56 | *bus_range = res; |
5aa182a2 | 57 | break; |
4e64dbe2 | 58 | } |
4e64dbe2 DD |
59 | } |
60 | ||
5aa182a2 BH |
61 | if (res_valid) |
62 | return 0; | |
4e64dbe2 | 63 | |
5aa182a2 BH |
64 | dev_err(dev, "non-prefetchable memory resource required\n"); |
65 | return -EINVAL; | |
4e64dbe2 DD |
66 | } |
67 | ||
1958e717 J |
68 | static void gen_pci_unmap_cfg(void *ptr) |
69 | { | |
70 | pci_ecam_free((struct pci_config_window *)ptr); | |
71 | } | |
72 | ||
73 | static struct pci_config_window *gen_pci_init(struct device *dev, | |
74 | struct list_head *resources, struct pci_ecam_ops *ops) | |
4e64dbe2 DD |
75 | { |
76 | int err; | |
1958e717 J |
77 | struct resource cfgres; |
78 | struct resource *bus_range = NULL; | |
79 | struct pci_config_window *cfg; | |
4e64dbe2 | 80 | |
1958e717 J |
81 | /* Parse our PCI ranges and request their resources */ |
82 | err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range); | |
83 | if (err) | |
84 | goto err_out; | |
85 | ||
86 | err = of_address_to_resource(dev->of_node, 0, &cfgres); | |
4e64dbe2 DD |
87 | if (err) { |
88 | dev_err(dev, "missing \"reg\" property\n"); | |
1958e717 | 89 | goto err_out; |
4e64dbe2 DD |
90 | } |
91 | ||
1958e717 J |
92 | cfg = pci_ecam_create(dev, &cfgres, bus_range, ops); |
93 | if (IS_ERR(cfg)) { | |
94 | err = PTR_ERR(cfg); | |
95 | goto err_out; | |
4e64dbe2 DD |
96 | } |
97 | ||
1958e717 J |
98 | err = devm_add_action(dev, gen_pci_unmap_cfg, cfg); |
99 | if (err) { | |
100 | gen_pci_unmap_cfg(cfg); | |
101 | goto err_out; | |
102 | } | |
103 | return cfg; | |
104 | ||
105 | err_out: | |
106 | pci_free_resource_list(resources); | |
107 | return ERR_PTR(err); | |
4e64dbe2 DD |
108 | } |
109 | ||
110 | int pci_host_common_probe(struct platform_device *pdev, | |
1958e717 | 111 | struct pci_ecam_ops *ops) |
4e64dbe2 | 112 | { |
4e64dbe2 DD |
113 | const char *type; |
114 | struct device *dev = &pdev->dev; | |
115 | struct device_node *np = dev->of_node; | |
116 | struct pci_bus *bus, *child; | |
1958e717 J |
117 | struct pci_config_window *cfg; |
118 | struct list_head resources; | |
4e64dbe2 DD |
119 | |
120 | type = of_get_property(np, "device_type", NULL); | |
121 | if (!type || strcmp(type, "pci")) { | |
122 | dev_err(dev, "invalid \"device_type\" %s\n", type); | |
123 | return -EINVAL; | |
124 | } | |
125 | ||
126 | of_pci_check_probe_only(); | |
127 | ||
4e64dbe2 | 128 | /* Parse and map our Configuration Space windows */ |
1958e717 J |
129 | INIT_LIST_HEAD(&resources); |
130 | cfg = gen_pci_init(dev, &resources, ops); | |
131 | if (IS_ERR(cfg)) | |
132 | return PTR_ERR(cfg); | |
4e64dbe2 DD |
133 | |
134 | /* Do not reassign resources if probe only */ | |
135 | if (!pci_has_flag(PCI_PROBE_ONLY)) | |
136 | pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS); | |
137 | ||
1958e717 J |
138 | bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg, |
139 | &resources); | |
4e64dbe2 DD |
140 | if (!bus) { |
141 | dev_err(dev, "Scanning rootbus failed"); | |
142 | return -ENODEV; | |
143 | } | |
144 | ||
145 | pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci); | |
146 | ||
dcce0f15 LP |
147 | /* |
148 | * We insert PCI resources into the iomem_resource and | |
149 | * ioport_resource trees in either pci_bus_claim_resources() | |
150 | * or pci_bus_assign_resources(). | |
151 | */ | |
152 | if (pci_has_flag(PCI_PROBE_ONLY)) { | |
153 | pci_bus_claim_resources(bus); | |
154 | } else { | |
4e64dbe2 DD |
155 | pci_bus_size_bridges(bus); |
156 | pci_bus_assign_resources(bus); | |
157 | ||
158 | list_for_each_entry(child, &bus->children, node) | |
159 | pcie_bus_configure_settings(child); | |
160 | } | |
161 | ||
162 | pci_bus_add_devices(bus); | |
163 | return 0; | |
164 | } | |
165 | ||
166 | MODULE_DESCRIPTION("Generic PCI host driver common code"); | |
167 | MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>"); | |
168 | MODULE_LICENSE("GPL v2"); |