Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 Koninklijke Philips Electronics NV |
| 3 | * |
| 4 | * Conversion to platform driver and DT: |
| 5 | * Copyright 2014 Linaro Ltd. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * 14/04/2005 Initial version, colin.king@philips.com |
| 17 | */ |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/of_address.h> |
| 21 | #include <linux/of_pci.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | |
| 26 | static void __iomem *versatile_pci_base; |
| 27 | static void __iomem *versatile_cfg_base[2]; |
| 28 | |
| 29 | #define PCI_IMAP(m) (versatile_pci_base + ((m) * 4)) |
| 30 | #define PCI_SMAP(m) (versatile_pci_base + 0x14 + ((m) * 4)) |
| 31 | #define PCI_SELFID (versatile_pci_base + 0xc) |
| 32 | |
| 33 | #define VP_PCI_DEVICE_ID 0x030010ee |
| 34 | #define VP_PCI_CLASS_ID 0x0b400000 |
| 35 | |
| 36 | static u32 pci_slot_ignore; |
| 37 | |
| 38 | static int __init versatile_pci_slot_ignore(char *str) |
| 39 | { |
| 40 | int retval; |
| 41 | int slot; |
| 42 | |
| 43 | while ((retval = get_option(&str, &slot))) { |
| 44 | if ((slot < 0) || (slot > 31)) |
| 45 | pr_err("Illegal slot value: %d\n", slot); |
| 46 | else |
| 47 | pci_slot_ignore |= (1 << slot); |
| 48 | } |
| 49 | return 1; |
| 50 | } |
| 51 | __setup("pci_slot_ignore=", versatile_pci_slot_ignore); |
| 52 | |
| 53 | |
| 54 | static void __iomem *versatile_map_bus(struct pci_bus *bus, |
| 55 | unsigned int devfn, int offset) |
| 56 | { |
| 57 | unsigned int busnr = bus->number; |
| 58 | |
| 59 | if (pci_slot_ignore & (1 << PCI_SLOT(devfn))) |
| 60 | return NULL; |
| 61 | |
| 62 | return versatile_cfg_base[1] + ((busnr << 16) | (devfn << 8) | offset); |
| 63 | } |
| 64 | |
| 65 | static struct pci_ops pci_versatile_ops = { |
| 66 | .map_bus = versatile_map_bus, |
| 67 | .read = pci_generic_config_read32, |
| 68 | .write = pci_generic_config_write, |
| 69 | }; |
| 70 | |
| 71 | static int versatile_pci_parse_request_of_pci_ranges(struct device *dev, |
| 72 | struct list_head *res) |
| 73 | { |
| 74 | int err, mem = 1, res_valid = 0; |
| 75 | struct device_node *np = dev->of_node; |
| 76 | resource_size_t iobase; |
Lorenzo Pieralisi | 53f4f7e | 2016-08-15 17:50:43 +0100 | [diff] [blame] | 77 | struct resource_entry *win, *tmp; |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 78 | |
| 79 | err = of_pci_get_host_bridge_resources(np, 0, 0xff, res, &iobase); |
| 80 | if (err) |
| 81 | return err; |
| 82 | |
Bjorn Helgaas | 2fbb353 | 2016-05-31 12:09:28 -0500 | [diff] [blame] | 83 | err = devm_request_pci_bus_resources(dev, res); |
| 84 | if (err) |
| 85 | goto out_release_res; |
| 86 | |
Lorenzo Pieralisi | 53f4f7e | 2016-08-15 17:50:43 +0100 | [diff] [blame] | 87 | resource_list_for_each_entry_safe(win, tmp, res) { |
Bjorn Helgaas | 2fbb353 | 2016-05-31 12:09:28 -0500 | [diff] [blame] | 88 | struct resource *res = win->res; |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 89 | |
| 90 | switch (resource_type(res)) { |
| 91 | case IORESOURCE_IO: |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 92 | err = pci_remap_iospace(res, iobase); |
Lorenzo Pieralisi | 53f4f7e | 2016-08-15 17:50:43 +0100 | [diff] [blame] | 93 | if (err) { |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 94 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
| 95 | err, res); |
Lorenzo Pieralisi | 53f4f7e | 2016-08-15 17:50:43 +0100 | [diff] [blame] | 96 | resource_list_destroy_entry(win); |
| 97 | } |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 98 | break; |
| 99 | case IORESOURCE_MEM: |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 100 | res_valid |= !(res->flags & IORESOURCE_PREFETCH); |
| 101 | |
| 102 | writel(res->start >> 28, PCI_IMAP(mem)); |
| 103 | writel(PHYS_OFFSET >> 28, PCI_SMAP(mem)); |
| 104 | mem++; |
| 105 | |
| 106 | break; |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 107 | } |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 108 | } |
| 109 | |
Bjorn Helgaas | da6163a | 2016-05-28 18:31:26 -0500 | [diff] [blame] | 110 | if (res_valid) |
| 111 | return 0; |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 112 | |
Bjorn Helgaas | da6163a | 2016-05-28 18:31:26 -0500 | [diff] [blame] | 113 | dev_err(dev, "non-prefetchable memory resource required\n"); |
| 114 | err = -EINVAL; |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 115 | |
| 116 | out_release_res: |
| 117 | pci_free_resource_list(res); |
| 118 | return err; |
| 119 | } |
| 120 | |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 121 | static int versatile_pci_probe(struct platform_device *pdev) |
| 122 | { |
| 123 | struct resource *res; |
| 124 | int ret, i, myslot = -1; |
| 125 | u32 val; |
| 126 | void __iomem *local_pci_cfg_base; |
| 127 | struct pci_bus *bus; |
| 128 | LIST_HEAD(pci_res); |
| 129 | |
| 130 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 131 | versatile_pci_base = devm_ioremap_resource(&pdev->dev, res); |
Jisheng Zhang | 8735816 | 2015-04-03 21:17:05 +0800 | [diff] [blame] | 132 | if (IS_ERR(versatile_pci_base)) |
| 133 | return PTR_ERR(versatile_pci_base); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 134 | |
| 135 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 136 | versatile_cfg_base[0] = devm_ioremap_resource(&pdev->dev, res); |
Jisheng Zhang | 8735816 | 2015-04-03 21:17:05 +0800 | [diff] [blame] | 137 | if (IS_ERR(versatile_cfg_base[0])) |
| 138 | return PTR_ERR(versatile_cfg_base[0]); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 139 | |
| 140 | res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 141 | versatile_cfg_base[1] = devm_ioremap_resource(&pdev->dev, res); |
Jisheng Zhang | 8735816 | 2015-04-03 21:17:05 +0800 | [diff] [blame] | 142 | if (IS_ERR(versatile_cfg_base[1])) |
| 143 | return PTR_ERR(versatile_cfg_base[1]); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 144 | |
| 145 | ret = versatile_pci_parse_request_of_pci_ranges(&pdev->dev, &pci_res); |
| 146 | if (ret) |
| 147 | return ret; |
| 148 | |
| 149 | /* |
| 150 | * We need to discover the PCI core first to configure itself |
| 151 | * before the main PCI probing is performed |
| 152 | */ |
| 153 | for (i = 0; i < 32; i++) { |
| 154 | if ((readl(versatile_cfg_base[0] + (i << 11) + PCI_VENDOR_ID) == VP_PCI_DEVICE_ID) && |
| 155 | (readl(versatile_cfg_base[0] + (i << 11) + PCI_CLASS_REVISION) == VP_PCI_CLASS_ID)) { |
| 156 | myslot = i; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | if (myslot == -1) { |
| 161 | dev_err(&pdev->dev, "Cannot find PCI core!\n"); |
| 162 | return -EIO; |
| 163 | } |
| 164 | /* |
| 165 | * Do not to map Versatile FPGA PCI device into memory space |
| 166 | */ |
| 167 | pci_slot_ignore |= (1 << myslot); |
| 168 | |
| 169 | dev_info(&pdev->dev, "PCI core found (slot %d)\n", myslot); |
| 170 | |
| 171 | writel(myslot, PCI_SELFID); |
| 172 | local_pci_cfg_base = versatile_cfg_base[1] + (myslot << 11); |
| 173 | |
| 174 | val = readl(local_pci_cfg_base + PCI_COMMAND); |
| 175 | val |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE; |
| 176 | writel(val, local_pci_cfg_base + PCI_COMMAND); |
| 177 | |
| 178 | /* |
| 179 | * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM |
| 180 | */ |
| 181 | writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_0); |
| 182 | writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_1); |
| 183 | writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_2); |
| 184 | |
| 185 | /* |
| 186 | * For many years the kernel and QEMU were symbiotically buggy |
| 187 | * in that they both assumed the same broken IRQ mapping. |
| 188 | * QEMU therefore attempts to auto-detect old broken kernels |
| 189 | * so that they still work on newer QEMU as they did on old |
| 190 | * QEMU. Since we now use the correct (ie matching-hardware) |
| 191 | * IRQ mapping we write a definitely different value to a |
| 192 | * PCI_INTERRUPT_LINE register to tell QEMU that we expect |
| 193 | * real hardware behaviour and it need not be backwards |
| 194 | * compatible for us. This write is harmless on real hardware. |
| 195 | */ |
| 196 | writel(0, versatile_cfg_base[0] + PCI_INTERRUPT_LINE); |
| 197 | |
| 198 | pci_add_flags(PCI_ENABLE_PROC_DOMAINS); |
| 199 | pci_add_flags(PCI_REASSIGN_ALL_BUS | PCI_REASSIGN_ALL_RSRC); |
| 200 | |
Lorenzo Pieralisi | a574795 | 2015-11-09 18:57:39 +0000 | [diff] [blame] | 201 | bus = pci_scan_root_bus(&pdev->dev, 0, &pci_versatile_ops, NULL, &pci_res); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 202 | if (!bus) |
| 203 | return -ENOMEM; |
| 204 | |
| 205 | pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci); |
| 206 | pci_assign_unassigned_bus_resources(bus); |
Yijing Wang | b97ea28 | 2015-03-16 11:18:56 +0800 | [diff] [blame] | 207 | pci_bus_add_devices(bus); |
Rob Herring | b7e7817 | 2015-01-28 10:16:18 -0600 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static const struct of_device_id versatile_pci_of_match[] = { |
| 213 | { .compatible = "arm,versatile-pci", }, |
| 214 | { }, |
| 215 | }; |
| 216 | MODULE_DEVICE_TABLE(of, versatile_pci_of_match); |
| 217 | |
| 218 | static struct platform_driver versatile_pci_driver = { |
| 219 | .driver = { |
| 220 | .name = "versatile-pci", |
| 221 | .of_match_table = versatile_pci_of_match, |
| 222 | }, |
| 223 | .probe = versatile_pci_probe, |
| 224 | }; |
| 225 | module_platform_driver(versatile_pci_driver); |
| 226 | |
| 227 | MODULE_DESCRIPTION("Versatile PCI driver"); |
| 228 | MODULE_LICENSE("GPL v2"); |