1edabd38eSSaeed Bishara /* 2edabd38eSSaeed Bishara * arch/arm/mach-dove/pcie.c 3edabd38eSSaeed Bishara * 4edabd38eSSaeed Bishara * PCIe functions for Marvell Dove 88AP510 SoC 5edabd38eSSaeed Bishara * 6edabd38eSSaeed Bishara * This file is licensed under the terms of the GNU General Public 7edabd38eSSaeed Bishara * License version 2. This program is licensed "as is" without any 8edabd38eSSaeed Bishara * warranty of any kind, whether express or implied. 9edabd38eSSaeed Bishara */ 10edabd38eSSaeed Bishara 11edabd38eSSaeed Bishara #include <linux/kernel.h> 12edabd38eSSaeed Bishara #include <linux/pci.h> 13edabd38eSSaeed Bishara #include <linux/mbus.h> 14*cc22b4c1SRob Herring #include <video/vga.h> 15edabd38eSSaeed Bishara #include <asm/mach/pci.h> 16edabd38eSSaeed Bishara #include <asm/mach/arch.h> 17edabd38eSSaeed Bishara #include <asm/setup.h> 18edabd38eSSaeed Bishara #include <asm/delay.h> 19edabd38eSSaeed Bishara #include <plat/pcie.h> 20edabd38eSSaeed Bishara #include <mach/irqs.h> 21edabd38eSSaeed Bishara #include <mach/bridge-regs.h> 22edabd38eSSaeed Bishara #include "common.h" 23edabd38eSSaeed Bishara 24edabd38eSSaeed Bishara struct pcie_port { 25edabd38eSSaeed Bishara u8 index; 26edabd38eSSaeed Bishara u8 root_bus_nr; 27edabd38eSSaeed Bishara void __iomem *base; 28edabd38eSSaeed Bishara spinlock_t conf_lock; 29edabd38eSSaeed Bishara char io_space_name[16]; 30edabd38eSSaeed Bishara char mem_space_name[16]; 31edabd38eSSaeed Bishara struct resource res[2]; 32edabd38eSSaeed Bishara }; 33edabd38eSSaeed Bishara 34edabd38eSSaeed Bishara static struct pcie_port pcie_port[2]; 35edabd38eSSaeed Bishara static int num_pcie_ports; 36edabd38eSSaeed Bishara 37edabd38eSSaeed Bishara 38edabd38eSSaeed Bishara static int __init dove_pcie_setup(int nr, struct pci_sys_data *sys) 39edabd38eSSaeed Bishara { 40edabd38eSSaeed Bishara struct pcie_port *pp; 41edabd38eSSaeed Bishara 42edabd38eSSaeed Bishara if (nr >= num_pcie_ports) 43edabd38eSSaeed Bishara return 0; 44edabd38eSSaeed Bishara 45edabd38eSSaeed Bishara pp = &pcie_port[nr]; 46edabd38eSSaeed Bishara pp->root_bus_nr = sys->busnr; 47edabd38eSSaeed Bishara 48edabd38eSSaeed Bishara /* 49edabd38eSSaeed Bishara * Generic PCIe unit setup. 50edabd38eSSaeed Bishara */ 51edabd38eSSaeed Bishara orion_pcie_set_local_bus_nr(pp->base, sys->busnr); 52edabd38eSSaeed Bishara 53edabd38eSSaeed Bishara orion_pcie_setup(pp->base, &dove_mbus_dram_info); 54edabd38eSSaeed Bishara 55edabd38eSSaeed Bishara /* 56edabd38eSSaeed Bishara * IORESOURCE_IO 57edabd38eSSaeed Bishara */ 58edabd38eSSaeed Bishara snprintf(pp->io_space_name, sizeof(pp->io_space_name), 59edabd38eSSaeed Bishara "PCIe %d I/O", pp->index); 60edabd38eSSaeed Bishara pp->io_space_name[sizeof(pp->io_space_name) - 1] = 0; 61edabd38eSSaeed Bishara pp->res[0].name = pp->io_space_name; 62edabd38eSSaeed Bishara if (pp->index == 0) { 63edabd38eSSaeed Bishara pp->res[0].start = DOVE_PCIE0_IO_PHYS_BASE; 64edabd38eSSaeed Bishara pp->res[0].end = pp->res[0].start + DOVE_PCIE0_IO_SIZE - 1; 65edabd38eSSaeed Bishara } else { 66edabd38eSSaeed Bishara pp->res[0].start = DOVE_PCIE1_IO_PHYS_BASE; 67edabd38eSSaeed Bishara pp->res[0].end = pp->res[0].start + DOVE_PCIE1_IO_SIZE - 1; 68edabd38eSSaeed Bishara } 69edabd38eSSaeed Bishara pp->res[0].flags = IORESOURCE_IO; 70edabd38eSSaeed Bishara if (request_resource(&ioport_resource, &pp->res[0])) 71edabd38eSSaeed Bishara panic("Request PCIe IO resource failed\n"); 72edabd38eSSaeed Bishara sys->resource[0] = &pp->res[0]; 73edabd38eSSaeed Bishara 74edabd38eSSaeed Bishara /* 75edabd38eSSaeed Bishara * IORESOURCE_MEM 76edabd38eSSaeed Bishara */ 77edabd38eSSaeed Bishara snprintf(pp->mem_space_name, sizeof(pp->mem_space_name), 78edabd38eSSaeed Bishara "PCIe %d MEM", pp->index); 79edabd38eSSaeed Bishara pp->mem_space_name[sizeof(pp->mem_space_name) - 1] = 0; 80edabd38eSSaeed Bishara pp->res[1].name = pp->mem_space_name; 81edabd38eSSaeed Bishara if (pp->index == 0) { 82edabd38eSSaeed Bishara pp->res[1].start = DOVE_PCIE0_MEM_PHYS_BASE; 83edabd38eSSaeed Bishara pp->res[1].end = pp->res[1].start + DOVE_PCIE0_MEM_SIZE - 1; 84edabd38eSSaeed Bishara } else { 85edabd38eSSaeed Bishara pp->res[1].start = DOVE_PCIE1_MEM_PHYS_BASE; 86edabd38eSSaeed Bishara pp->res[1].end = pp->res[1].start + DOVE_PCIE1_MEM_SIZE - 1; 87edabd38eSSaeed Bishara } 88edabd38eSSaeed Bishara pp->res[1].flags = IORESOURCE_MEM; 89edabd38eSSaeed Bishara if (request_resource(&iomem_resource, &pp->res[1])) 90edabd38eSSaeed Bishara panic("Request PCIe Memory resource failed\n"); 91edabd38eSSaeed Bishara sys->resource[1] = &pp->res[1]; 92edabd38eSSaeed Bishara 93edabd38eSSaeed Bishara sys->resource[2] = NULL; 94edabd38eSSaeed Bishara 95edabd38eSSaeed Bishara return 1; 96edabd38eSSaeed Bishara } 97edabd38eSSaeed Bishara 98edabd38eSSaeed Bishara static struct pcie_port *bus_to_port(int bus) 99edabd38eSSaeed Bishara { 100edabd38eSSaeed Bishara int i; 101edabd38eSSaeed Bishara 102edabd38eSSaeed Bishara for (i = num_pcie_ports - 1; i >= 0; i--) { 103edabd38eSSaeed Bishara int rbus = pcie_port[i].root_bus_nr; 104edabd38eSSaeed Bishara if (rbus != -1 && rbus <= bus) 105edabd38eSSaeed Bishara break; 106edabd38eSSaeed Bishara } 107edabd38eSSaeed Bishara 108edabd38eSSaeed Bishara return i >= 0 ? pcie_port + i : NULL; 109edabd38eSSaeed Bishara } 110edabd38eSSaeed Bishara 111edabd38eSSaeed Bishara static int pcie_valid_config(struct pcie_port *pp, int bus, int dev) 112edabd38eSSaeed Bishara { 113edabd38eSSaeed Bishara /* 114edabd38eSSaeed Bishara * Don't go out when trying to access nonexisting devices 115edabd38eSSaeed Bishara * on the local bus. 116edabd38eSSaeed Bishara */ 117edabd38eSSaeed Bishara if (bus == pp->root_bus_nr && dev > 1) 118edabd38eSSaeed Bishara return 0; 119edabd38eSSaeed Bishara 120edabd38eSSaeed Bishara return 1; 121edabd38eSSaeed Bishara } 122edabd38eSSaeed Bishara 123edabd38eSSaeed Bishara static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, 124edabd38eSSaeed Bishara int size, u32 *val) 125edabd38eSSaeed Bishara { 126edabd38eSSaeed Bishara struct pcie_port *pp = bus_to_port(bus->number); 127edabd38eSSaeed Bishara unsigned long flags; 128edabd38eSSaeed Bishara int ret; 129edabd38eSSaeed Bishara 130edabd38eSSaeed Bishara if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) { 131edabd38eSSaeed Bishara *val = 0xffffffff; 132edabd38eSSaeed Bishara return PCIBIOS_DEVICE_NOT_FOUND; 133edabd38eSSaeed Bishara } 134edabd38eSSaeed Bishara 135edabd38eSSaeed Bishara spin_lock_irqsave(&pp->conf_lock, flags); 136edabd38eSSaeed Bishara ret = orion_pcie_rd_conf(pp->base, bus, devfn, where, size, val); 137edabd38eSSaeed Bishara spin_unlock_irqrestore(&pp->conf_lock, flags); 138edabd38eSSaeed Bishara 139edabd38eSSaeed Bishara return ret; 140edabd38eSSaeed Bishara } 141edabd38eSSaeed Bishara 142edabd38eSSaeed Bishara static int pcie_wr_conf(struct pci_bus *bus, u32 devfn, 143edabd38eSSaeed Bishara int where, int size, u32 val) 144edabd38eSSaeed Bishara { 145edabd38eSSaeed Bishara struct pcie_port *pp = bus_to_port(bus->number); 146edabd38eSSaeed Bishara unsigned long flags; 147edabd38eSSaeed Bishara int ret; 148edabd38eSSaeed Bishara 149edabd38eSSaeed Bishara if (pcie_valid_config(pp, bus->number, PCI_SLOT(devfn)) == 0) 150edabd38eSSaeed Bishara return PCIBIOS_DEVICE_NOT_FOUND; 151edabd38eSSaeed Bishara 152edabd38eSSaeed Bishara spin_lock_irqsave(&pp->conf_lock, flags); 153edabd38eSSaeed Bishara ret = orion_pcie_wr_conf(pp->base, bus, devfn, where, size, val); 154edabd38eSSaeed Bishara spin_unlock_irqrestore(&pp->conf_lock, flags); 155edabd38eSSaeed Bishara 156edabd38eSSaeed Bishara return ret; 157edabd38eSSaeed Bishara } 158edabd38eSSaeed Bishara 159edabd38eSSaeed Bishara static struct pci_ops pcie_ops = { 160edabd38eSSaeed Bishara .read = pcie_rd_conf, 161edabd38eSSaeed Bishara .write = pcie_wr_conf, 162edabd38eSSaeed Bishara }; 163edabd38eSSaeed Bishara 164edabd38eSSaeed Bishara static void __devinit rc_pci_fixup(struct pci_dev *dev) 165edabd38eSSaeed Bishara { 166edabd38eSSaeed Bishara /* 167edabd38eSSaeed Bishara * Prevent enumeration of root complex. 168edabd38eSSaeed Bishara */ 169edabd38eSSaeed Bishara if (dev->bus->parent == NULL && dev->devfn == 0) { 170edabd38eSSaeed Bishara int i; 171edabd38eSSaeed Bishara 172edabd38eSSaeed Bishara for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 173edabd38eSSaeed Bishara dev->resource[i].start = 0; 174edabd38eSSaeed Bishara dev->resource[i].end = 0; 175edabd38eSSaeed Bishara dev->resource[i].flags = 0; 176edabd38eSSaeed Bishara } 177edabd38eSSaeed Bishara } 178edabd38eSSaeed Bishara } 179edabd38eSSaeed Bishara DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup); 180edabd38eSSaeed Bishara 181edabd38eSSaeed Bishara static struct pci_bus __init * 182edabd38eSSaeed Bishara dove_pcie_scan_bus(int nr, struct pci_sys_data *sys) 183edabd38eSSaeed Bishara { 184edabd38eSSaeed Bishara struct pci_bus *bus; 185edabd38eSSaeed Bishara 186edabd38eSSaeed Bishara if (nr < num_pcie_ports) { 187edabd38eSSaeed Bishara bus = pci_scan_bus(sys->busnr, &pcie_ops, sys); 188edabd38eSSaeed Bishara } else { 189edabd38eSSaeed Bishara bus = NULL; 190edabd38eSSaeed Bishara BUG(); 191edabd38eSSaeed Bishara } 192edabd38eSSaeed Bishara 193edabd38eSSaeed Bishara return bus; 194edabd38eSSaeed Bishara } 195edabd38eSSaeed Bishara 196edabd38eSSaeed Bishara static int __init dove_pcie_map_irq(struct pci_dev *dev, u8 slot, u8 pin) 197edabd38eSSaeed Bishara { 198edabd38eSSaeed Bishara struct pcie_port *pp = bus_to_port(dev->bus->number); 199edabd38eSSaeed Bishara 200edabd38eSSaeed Bishara return pp->index ? IRQ_DOVE_PCIE1 : IRQ_DOVE_PCIE0; 201edabd38eSSaeed Bishara } 202edabd38eSSaeed Bishara 203edabd38eSSaeed Bishara static struct hw_pci dove_pci __initdata = { 204edabd38eSSaeed Bishara .nr_controllers = 2, 205edabd38eSSaeed Bishara .swizzle = pci_std_swizzle, 206edabd38eSSaeed Bishara .setup = dove_pcie_setup, 207edabd38eSSaeed Bishara .scan = dove_pcie_scan_bus, 208edabd38eSSaeed Bishara .map_irq = dove_pcie_map_irq, 209edabd38eSSaeed Bishara }; 210edabd38eSSaeed Bishara 211edabd38eSSaeed Bishara static void __init add_pcie_port(int index, unsigned long base) 212edabd38eSSaeed Bishara { 213edabd38eSSaeed Bishara printk(KERN_INFO "Dove PCIe port %d: ", index); 214edabd38eSSaeed Bishara 215edabd38eSSaeed Bishara if (orion_pcie_link_up((void __iomem *)base)) { 216edabd38eSSaeed Bishara struct pcie_port *pp = &pcie_port[num_pcie_ports++]; 217edabd38eSSaeed Bishara 218edabd38eSSaeed Bishara printk(KERN_INFO "link up\n"); 219edabd38eSSaeed Bishara 220edabd38eSSaeed Bishara pp->index = index; 221edabd38eSSaeed Bishara pp->root_bus_nr = -1; 222edabd38eSSaeed Bishara pp->base = (void __iomem *)base; 223edabd38eSSaeed Bishara spin_lock_init(&pp->conf_lock); 224edabd38eSSaeed Bishara memset(pp->res, 0, sizeof(pp->res)); 225edabd38eSSaeed Bishara } else { 226edabd38eSSaeed Bishara printk(KERN_INFO "link down, ignoring\n"); 227edabd38eSSaeed Bishara } 228edabd38eSSaeed Bishara } 229edabd38eSSaeed Bishara 230edabd38eSSaeed Bishara void __init dove_pcie_init(int init_port0, int init_port1) 231edabd38eSSaeed Bishara { 232*cc22b4c1SRob Herring vga_base = DOVE_PCIE0_MEM_PHYS_BASE; 233*cc22b4c1SRob Herring 234edabd38eSSaeed Bishara if (init_port0) 235edabd38eSSaeed Bishara add_pcie_port(0, DOVE_PCIE0_VIRT_BASE); 236edabd38eSSaeed Bishara 237edabd38eSSaeed Bishara if (init_port1) 238edabd38eSSaeed Bishara add_pcie_port(1, DOVE_PCIE1_VIRT_BASE); 239edabd38eSSaeed Bishara 240edabd38eSSaeed Bishara pci_common_init(&dove_pci); 241edabd38eSSaeed Bishara } 242