Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 2004 Koninklijke Philips Electronics NV
0004  *
0005  * Conversion to platform driver and DT:
0006  * Copyright 2014 Linaro Ltd.
0007  *
0008  * 14/04/2005 Initial version, colin.king@philips.com
0009  */
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/of_address.h>
0013 #include <linux/of_pci.h>
0014 #include <linux/of_platform.h>
0015 #include <linux/pci.h>
0016 #include <linux/platform_device.h>
0017 
0018 #include "../pci.h"
0019 
0020 static void __iomem *versatile_pci_base;
0021 static void __iomem *versatile_cfg_base[2];
0022 
0023 #define PCI_IMAP(m)     (versatile_pci_base + ((m) * 4))
0024 #define PCI_SMAP(m)     (versatile_pci_base + 0x14 + ((m) * 4))
0025 #define PCI_SELFID      (versatile_pci_base + 0xc)
0026 
0027 #define VP_PCI_DEVICE_ID        0x030010ee
0028 #define VP_PCI_CLASS_ID         0x0b400000
0029 
0030 static u32 pci_slot_ignore;
0031 
0032 static int __init versatile_pci_slot_ignore(char *str)
0033 {
0034     int slot;
0035 
0036     while (get_option(&str, &slot)) {
0037         if ((slot < 0) || (slot > 31))
0038             pr_err("Illegal slot value: %d\n", slot);
0039         else
0040             pci_slot_ignore |= (1 << slot);
0041     }
0042     return 1;
0043 }
0044 __setup("pci_slot_ignore=", versatile_pci_slot_ignore);
0045 
0046 
0047 static void __iomem *versatile_map_bus(struct pci_bus *bus,
0048                        unsigned int devfn, int offset)
0049 {
0050     unsigned int busnr = bus->number;
0051 
0052     if (pci_slot_ignore & (1 << PCI_SLOT(devfn)))
0053         return NULL;
0054 
0055     return versatile_cfg_base[1] + ((busnr << 16) | (devfn << 8) | offset);
0056 }
0057 
0058 static struct pci_ops pci_versatile_ops = {
0059     .map_bus = versatile_map_bus,
0060     .read   = pci_generic_config_read32,
0061     .write  = pci_generic_config_write,
0062 };
0063 
0064 static int versatile_pci_probe(struct platform_device *pdev)
0065 {
0066     struct device *dev = &pdev->dev;
0067     struct resource *res;
0068     struct resource_entry *entry;
0069     int i, myslot = -1, mem = 1;
0070     u32 val;
0071     void __iomem *local_pci_cfg_base;
0072     struct pci_host_bridge *bridge;
0073 
0074     bridge = devm_pci_alloc_host_bridge(dev, 0);
0075     if (!bridge)
0076         return -ENOMEM;
0077 
0078     versatile_pci_base = devm_platform_ioremap_resource(pdev, 0);
0079     if (IS_ERR(versatile_pci_base))
0080         return PTR_ERR(versatile_pci_base);
0081 
0082     versatile_cfg_base[0] = devm_platform_ioremap_resource(pdev, 1);
0083     if (IS_ERR(versatile_cfg_base[0]))
0084         return PTR_ERR(versatile_cfg_base[0]);
0085 
0086     res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
0087     versatile_cfg_base[1] = devm_pci_remap_cfg_resource(dev, res);
0088     if (IS_ERR(versatile_cfg_base[1]))
0089         return PTR_ERR(versatile_cfg_base[1]);
0090 
0091     resource_list_for_each_entry(entry, &bridge->windows) {
0092         if (resource_type(entry->res) == IORESOURCE_MEM) {
0093             writel(entry->res->start >> 28, PCI_IMAP(mem));
0094             writel(__pa(PAGE_OFFSET) >> 28, PCI_SMAP(mem));
0095             mem++;
0096         }
0097     }
0098 
0099     /*
0100      * We need to discover the PCI core first to configure itself
0101      * before the main PCI probing is performed
0102      */
0103     for (i = 0; i < 32; i++) {
0104         if ((readl(versatile_cfg_base[0] + (i << 11) + PCI_VENDOR_ID) == VP_PCI_DEVICE_ID) &&
0105             (readl(versatile_cfg_base[0] + (i << 11) + PCI_CLASS_REVISION) == VP_PCI_CLASS_ID)) {
0106             myslot = i;
0107             break;
0108         }
0109     }
0110     if (myslot == -1) {
0111         dev_err(dev, "Cannot find PCI core!\n");
0112         return -EIO;
0113     }
0114     /*
0115      * Do not to map Versatile FPGA PCI device into memory space
0116      */
0117     pci_slot_ignore |= (1 << myslot);
0118 
0119     dev_info(dev, "PCI core found (slot %d)\n", myslot);
0120 
0121     writel(myslot, PCI_SELFID);
0122     local_pci_cfg_base = versatile_cfg_base[1] + (myslot << 11);
0123 
0124     val = readl(local_pci_cfg_base + PCI_COMMAND);
0125     val |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
0126     writel(val, local_pci_cfg_base + PCI_COMMAND);
0127 
0128     /*
0129      * Configure the PCI inbound memory windows to be 1:1 mapped to SDRAM
0130      */
0131     writel(__pa(PAGE_OFFSET), local_pci_cfg_base + PCI_BASE_ADDRESS_0);
0132     writel(__pa(PAGE_OFFSET), local_pci_cfg_base + PCI_BASE_ADDRESS_1);
0133     writel(__pa(PAGE_OFFSET), local_pci_cfg_base + PCI_BASE_ADDRESS_2);
0134 
0135     /*
0136      * For many years the kernel and QEMU were symbiotically buggy
0137      * in that they both assumed the same broken IRQ mapping.
0138      * QEMU therefore attempts to auto-detect old broken kernels
0139      * so that they still work on newer QEMU as they did on old
0140      * QEMU. Since we now use the correct (ie matching-hardware)
0141      * IRQ mapping we write a definitely different value to a
0142      * PCI_INTERRUPT_LINE register to tell QEMU that we expect
0143      * real hardware behaviour and it need not be backwards
0144      * compatible for us. This write is harmless on real hardware.
0145      */
0146     writel(0, versatile_cfg_base[0] + PCI_INTERRUPT_LINE);
0147 
0148     pci_add_flags(PCI_REASSIGN_ALL_BUS);
0149 
0150     bridge->ops = &pci_versatile_ops;
0151 
0152     return pci_host_probe(bridge);
0153 }
0154 
0155 static const struct of_device_id versatile_pci_of_match[] = {
0156     { .compatible = "arm,versatile-pci", },
0157     { },
0158 };
0159 MODULE_DEVICE_TABLE(of, versatile_pci_of_match);
0160 
0161 static struct platform_driver versatile_pci_driver = {
0162     .driver = {
0163         .name = "versatile-pci",
0164         .of_match_table = versatile_pci_of_match,
0165         .suppress_bind_attrs = true,
0166     },
0167     .probe = versatile_pci_probe,
0168 };
0169 module_platform_driver(versatile_pci_driver);
0170 
0171 MODULE_DESCRIPTION("Versatile PCI driver");
0172 MODULE_LICENSE("GPL v2");