David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 1 | /* |
Paul Gortmaker | 4068bd1 | 2016-08-22 17:59:48 -0400 | [diff] [blame] | 2 | * Generic PCI host driver common code |
| 3 | * |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 4 | * 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 Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 22 | #include <linux/of_address.h> |
| 23 | #include <linux/of_pci.h> |
Jayachandran C | 80955f9 | 2016-06-10 21:55:09 +0200 | [diff] [blame] | 24 | #include <linux/pci-ecam.h> |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 27 | static int gen_pci_parse_request_of_pci_ranges(struct device *dev, |
| 28 | struct list_head *resources, struct resource **bus_range) |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 29 | { |
| 30 | int err, res_valid = 0; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 31 | struct device_node *np = dev->of_node; |
| 32 | resource_size_t iobase; |
Lorenzo Pieralisi | 43281ed | 2016-08-15 17:50:45 +0100 | [diff] [blame] | 33 | struct resource_entry *win, *tmp; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 34 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 35 | err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 36 | if (err) |
| 37 | return err; |
| 38 | |
Bjorn Helgaas | b7f957a | 2016-05-31 12:05:05 -0500 | [diff] [blame] | 39 | err = devm_request_pci_bus_resources(dev, resources); |
| 40 | if (err) |
Bjorn Helgaas | 5aa182a | 2016-05-28 18:28:51 -0500 | [diff] [blame] | 41 | return err; |
Bjorn Helgaas | b7f957a | 2016-05-31 12:05:05 -0500 | [diff] [blame] | 42 | |
Lorenzo Pieralisi | 43281ed | 2016-08-15 17:50:45 +0100 | [diff] [blame] | 43 | resource_list_for_each_entry_safe(win, tmp, resources) { |
Bjorn Helgaas | b7f957a | 2016-05-31 12:05:05 -0500 | [diff] [blame] | 44 | struct resource *res = win->res; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 45 | |
| 46 | switch (resource_type(res)) { |
| 47 | case IORESOURCE_IO: |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 48 | err = pci_remap_iospace(res, iobase); |
Lorenzo Pieralisi | 43281ed | 2016-08-15 17:50:45 +0100 | [diff] [blame] | 49 | if (err) { |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 50 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
| 51 | err, res); |
Lorenzo Pieralisi | 43281ed | 2016-08-15 17:50:45 +0100 | [diff] [blame] | 52 | resource_list_destroy_entry(win); |
| 53 | } |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 54 | break; |
| 55 | case IORESOURCE_MEM: |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 56 | res_valid |= !(res->flags & IORESOURCE_PREFETCH); |
| 57 | break; |
| 58 | case IORESOURCE_BUS: |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 59 | *bus_range = res; |
Bjorn Helgaas | 5aa182a | 2016-05-28 18:28:51 -0500 | [diff] [blame] | 60 | break; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 61 | } |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 62 | } |
| 63 | |
Bjorn Helgaas | 5aa182a | 2016-05-28 18:28:51 -0500 | [diff] [blame] | 64 | if (res_valid) |
| 65 | return 0; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 66 | |
Bjorn Helgaas | 5aa182a | 2016-05-28 18:28:51 -0500 | [diff] [blame] | 67 | dev_err(dev, "non-prefetchable memory resource required\n"); |
| 68 | return -EINVAL; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 69 | } |
| 70 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 71 | static void gen_pci_unmap_cfg(void *ptr) |
| 72 | { |
| 73 | pci_ecam_free((struct pci_config_window *)ptr); |
| 74 | } |
| 75 | |
| 76 | static struct pci_config_window *gen_pci_init(struct device *dev, |
| 77 | struct list_head *resources, struct pci_ecam_ops *ops) |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 78 | { |
| 79 | int err; |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 80 | struct resource cfgres; |
| 81 | struct resource *bus_range = NULL; |
| 82 | struct pci_config_window *cfg; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 83 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 84 | /* 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 Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 90 | if (err) { |
| 91 | dev_err(dev, "missing \"reg\" property\n"); |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 92 | goto err_out; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 93 | } |
| 94 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 95 | cfg = pci_ecam_create(dev, &cfgres, bus_range, ops); |
| 96 | if (IS_ERR(cfg)) { |
| 97 | err = PTR_ERR(cfg); |
| 98 | goto err_out; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 99 | } |
| 100 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 101 | 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 | |
| 108 | err_out: |
| 109 | pci_free_resource_list(resources); |
| 110 | return ERR_PTR(err); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | int pci_host_common_probe(struct platform_device *pdev, |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 114 | struct pci_ecam_ops *ops) |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 115 | { |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 116 | const char *type; |
| 117 | struct device *dev = &pdev->dev; |
| 118 | struct device_node *np = dev->of_node; |
| 119 | struct pci_bus *bus, *child; |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 120 | struct pci_config_window *cfg; |
| 121 | struct list_head resources; |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 122 | |
| 123 | type = of_get_property(np, "device_type", NULL); |
| 124 | if (!type || strcmp(type, "pci")) { |
| 125 | dev_err(dev, "invalid \"device_type\" %s\n", type); |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | of_pci_check_probe_only(); |
| 130 | |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 131 | /* Parse and map our Configuration Space windows */ |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 132 | INIT_LIST_HEAD(&resources); |
| 133 | cfg = gen_pci_init(dev, &resources, ops); |
| 134 | if (IS_ERR(cfg)) |
| 135 | return PTR_ERR(cfg); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 136 | |
| 137 | /* Do not reassign resources if probe only */ |
| 138 | if (!pci_has_flag(PCI_PROBE_ONLY)) |
| 139 | pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS); |
| 140 | |
Jayachandran C | 1958e71 | 2016-05-11 17:34:46 -0500 | [diff] [blame] | 141 | bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg, |
| 142 | &resources); |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 143 | if (!bus) { |
| 144 | dev_err(dev, "Scanning rootbus failed"); |
| 145 | return -ENODEV; |
| 146 | } |
| 147 | |
Dongdong Liu | 3fb5561 | 2017-01-12 14:28:24 +0800 | [diff] [blame] | 148 | #ifdef CONFIG_ARM |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 149 | pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci); |
Dongdong Liu | 3fb5561 | 2017-01-12 14:28:24 +0800 | [diff] [blame] | 150 | #endif |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 151 | |
Lorenzo Pieralisi | dcce0f1 | 2016-06-08 12:04:48 +0100 | [diff] [blame] | 152 | /* |
| 153 | * We insert PCI resources into the iomem_resource and |
| 154 | * ioport_resource trees in either pci_bus_claim_resources() |
| 155 | * or pci_bus_assign_resources(). |
| 156 | */ |
| 157 | if (pci_has_flag(PCI_PROBE_ONLY)) { |
| 158 | pci_bus_claim_resources(bus); |
| 159 | } else { |
David Daney | 4e64dbe | 2016-03-11 15:35:55 -0600 | [diff] [blame] | 160 | pci_bus_size_bridges(bus); |
| 161 | pci_bus_assign_resources(bus); |
| 162 | |
| 163 | list_for_each_entry(child, &bus->children, node) |
| 164 | pcie_bus_configure_settings(child); |
| 165 | } |
| 166 | |
| 167 | pci_bus_add_devices(bus); |
| 168 | return 0; |
| 169 | } |