linux/drivers/pci/host/pci-versatile.c
<<
>>
Prefs
   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
  26static void __iomem *versatile_pci_base;
  27static 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
  36static u32 pci_slot_ignore;
  37
  38static 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
  54static 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
  65static 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
  71static 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;
  77        struct resource_entry *win;
  78
  79        err = of_pci_get_host_bridge_resources(np, 0, 0xff, res, &iobase);
  80        if (err)
  81                return err;
  82
  83        err = devm_request_pci_bus_resources(dev, res);
  84        if (err)
  85                goto out_release_res;
  86
  87        resource_list_for_each_entry(win, res) {
  88                struct resource *res = win->res;
  89
  90                switch (resource_type(res)) {
  91                case IORESOURCE_IO:
  92                        err = pci_remap_iospace(res, iobase);
  93                        if (err)
  94                                dev_warn(dev, "error %d: failed to map resource %pR\n",
  95                                         err, res);
  96                        break;
  97                case IORESOURCE_MEM:
  98                        res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  99
 100                        writel(res->start >> 28, PCI_IMAP(mem));
 101                        writel(PHYS_OFFSET >> 28, PCI_SMAP(mem));
 102                        mem++;
 103
 104                        break;
 105                }
 106        }
 107
 108        if (res_valid)
 109                return 0;
 110
 111        dev_err(dev, "non-prefetchable memory resource required\n");
 112        err = -EINVAL;
 113
 114out_release_res:
 115        pci_free_resource_list(res);
 116        return err;
 117}
 118
 119static int versatile_pci_probe(struct platform_device *pdev)
 120{
 121        struct resource *res;
 122        int ret, i, myslot = -1;
 123        u32 val;
 124        void __iomem *local_pci_cfg_base;
 125        struct pci_bus *bus;
 126        LIST_HEAD(pci_res);
 127
 128        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 129        versatile_pci_base = devm_ioremap_resource(&pdev->dev, res);
 130        if (IS_ERR(versatile_pci_base))
 131                return PTR_ERR(versatile_pci_base);
 132
 133        res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 134        versatile_cfg_base[0] = devm_ioremap_resource(&pdev->dev, res);
 135        if (IS_ERR(versatile_cfg_base[0]))
 136                return PTR_ERR(versatile_cfg_base[0]);
 137
 138        res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
 139        versatile_cfg_base[1] = devm_ioremap_resource(&pdev->dev, res);
 140        if (IS_ERR(versatile_cfg_base[1]))
 141                return PTR_ERR(versatile_cfg_base[1]);
 142
 143        ret = versatile_pci_parse_request_of_pci_ranges(&pdev->dev, &pci_res);
 144        if (ret)
 145                return ret;
 146
 147        /*
 148         * We need to discover the PCI core first to configure itself
 149         * before the main PCI probing is performed
 150         */
 151        for (i = 0; i < 32; i++) {
 152                if ((readl(versatile_cfg_base[0] + (i << 11) + PCI_VENDOR_ID) == VP_PCI_DEVICE_ID) &&
 153                    (readl(versatile_cfg_base[0] + (i << 11) + PCI_CLASS_REVISION) == VP_PCI_CLASS_ID)) {
 154                        myslot = i;
 155                        break;
 156                }
 157        }
 158        if (myslot == -1) {
 159                dev_err(&pdev->dev, "Cannot find PCI core!\n");
 160                return -EIO;
 161        }
 162        /*
 163         * Do not to map Versatile FPGA PCI device into memory space
 164         */
 165        pci_slot_ignore |= (1 << myslot);
 166
 167        dev_info(&pdev->dev, "PCI core found (slot %d)\n", myslot);
 168
 169        writel(myslot, PCI_SELFID);
 170        local_pci_cfg_base = versatile_cfg_base[1] + (myslot << 11);
 171
 172        val = readl(local_pci_cfg_base + PCI_COMMAND);
 173        val |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
 174        writel(val, local_pci_cfg_base + PCI_COMMAND);
 175
 176        /*
 177         * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM
 178         */
 179        writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_0);
 180        writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_1);
 181        writel(PHYS_OFFSET, local_pci_cfg_base + PCI_BASE_ADDRESS_2);
 182
 183        /*
 184         * For many years the kernel and QEMU were symbiotically buggy
 185         * in that they both assumed the same broken IRQ mapping.
 186         * QEMU therefore attempts to auto-detect old broken kernels
 187         * so that they still work on newer QEMU as they did on old
 188         * QEMU. Since we now use the correct (ie matching-hardware)
 189         * IRQ mapping we write a definitely different value to a
 190         * PCI_INTERRUPT_LINE register to tell QEMU that we expect
 191         * real hardware behaviour and it need not be backwards
 192         * compatible for us. This write is harmless on real hardware.
 193         */
 194        writel(0, versatile_cfg_base[0] + PCI_INTERRUPT_LINE);
 195
 196        pci_add_flags(PCI_ENABLE_PROC_DOMAINS);
 197        pci_add_flags(PCI_REASSIGN_ALL_BUS | PCI_REASSIGN_ALL_RSRC);
 198
 199        bus = pci_scan_root_bus(&pdev->dev, 0, &pci_versatile_ops, NULL, &pci_res);
 200        if (!bus)
 201                return -ENOMEM;
 202
 203        pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
 204        pci_assign_unassigned_bus_resources(bus);
 205        pci_bus_add_devices(bus);
 206
 207        return 0;
 208}
 209
 210static const struct of_device_id versatile_pci_of_match[] = {
 211        { .compatible = "arm,versatile-pci", },
 212        { },
 213};
 214MODULE_DEVICE_TABLE(of, versatile_pci_of_match);
 215
 216static struct platform_driver versatile_pci_driver = {
 217        .driver = {
 218                .name = "versatile-pci",
 219                .of_match_table = versatile_pci_of_match,
 220        },
 221        .probe = versatile_pci_probe,
 222};
 223module_platform_driver(versatile_pci_driver);
 224
 225MODULE_DESCRIPTION("Versatile PCI driver");
 226MODULE_LICENSE("GPL v2");
 227