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