1f8091a88SPaul Burton /* 2f8091a88SPaul Burton * This program is free software; you can redistribute it and/or modify it 3f8091a88SPaul Burton * under the terms of the GNU General Public License as published by the 4f8091a88SPaul Burton * Free Software Foundation; either version 2 of the License, or (at your 5f8091a88SPaul Burton * option) any later version. 6f8091a88SPaul Burton * 7f8091a88SPaul Burton * Copyright (C) 2003, 04, 11 Ralf Baechle (ralf@linux-mips.org) 8f8091a88SPaul Burton * Copyright (C) 2011 Wind River Systems, 9f8091a88SPaul Burton * written by Ralf Baechle (ralf@linux-mips.org) 10f8091a88SPaul Burton */ 11f8091a88SPaul Burton #include <linux/bug.h> 12f8091a88SPaul Burton #include <linux/kernel.h> 13f8091a88SPaul Burton #include <linux/mm.h> 14f8091a88SPaul Burton #include <linux/bootmem.h> 15f8091a88SPaul Burton #include <linux/export.h> 16f8091a88SPaul Burton #include <linux/init.h> 17f8091a88SPaul Burton #include <linux/types.h> 18f8091a88SPaul Burton #include <linux/pci.h> 19f8091a88SPaul Burton #include <linux/of_address.h> 20f8091a88SPaul Burton 21f8091a88SPaul Burton #include <asm/cpu-info.h> 22f8091a88SPaul Burton 23f8091a88SPaul Burton /* 24f8091a88SPaul Burton * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource 25f8091a88SPaul Burton * assignments. 26f8091a88SPaul Burton */ 27f8091a88SPaul Burton 28f8091a88SPaul Burton /* 29f8091a88SPaul Burton * The PCI controller list. 30f8091a88SPaul Burton */ 31f8091a88SPaul Burton static LIST_HEAD(controllers); 32f8091a88SPaul Burton 33f8091a88SPaul Burton static int pci_initialized; 34f8091a88SPaul Burton 35f8091a88SPaul Burton /* 36f8091a88SPaul Burton * We need to avoid collisions with `mirrored' VGA ports 37f8091a88SPaul Burton * and other strange ISA hardware, so we always want the 38f8091a88SPaul Burton * addresses to be allocated in the 0x000-0x0ff region 39f8091a88SPaul Burton * modulo 0x400. 40f8091a88SPaul Burton * 41f8091a88SPaul Burton * Why? Because some silly external IO cards only decode 42f8091a88SPaul Burton * the low 10 bits of the IO address. The 0x00-0xff region 43f8091a88SPaul Burton * is reserved for motherboard devices that decode all 16 44f8091a88SPaul Burton * bits, so it's ok to allocate at, say, 0x2800-0x28ff, 45f8091a88SPaul Burton * but we want to try to avoid allocating at 0x2900-0x2bff 46f8091a88SPaul Burton * which might have be mirrored at 0x0100-0x03ff.. 47f8091a88SPaul Burton */ 48f8091a88SPaul Burton resource_size_t 49f8091a88SPaul Burton pcibios_align_resource(void *data, const struct resource *res, 50f8091a88SPaul Burton resource_size_t size, resource_size_t align) 51f8091a88SPaul Burton { 52f8091a88SPaul Burton struct pci_dev *dev = data; 53f8091a88SPaul Burton struct pci_controller *hose = dev->sysdata; 54f8091a88SPaul Burton resource_size_t start = res->start; 55f8091a88SPaul Burton 56f8091a88SPaul Burton if (res->flags & IORESOURCE_IO) { 57f8091a88SPaul Burton /* Make sure we start at our min on all hoses */ 58f8091a88SPaul Burton if (start < PCIBIOS_MIN_IO + hose->io_resource->start) 59f8091a88SPaul Burton start = PCIBIOS_MIN_IO + hose->io_resource->start; 60f8091a88SPaul Burton 61f8091a88SPaul Burton /* 62f8091a88SPaul Burton * Put everything into 0x00-0xff region modulo 0x400 63f8091a88SPaul Burton */ 64f8091a88SPaul Burton if (start & 0x300) 65f8091a88SPaul Burton start = (start + 0x3ff) & ~0x3ff; 66f8091a88SPaul Burton } else if (res->flags & IORESOURCE_MEM) { 67f8091a88SPaul Burton /* Make sure we start at our min on all hoses */ 68f8091a88SPaul Burton if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start) 69f8091a88SPaul Burton start = PCIBIOS_MIN_MEM + hose->mem_resource->start; 70f8091a88SPaul Burton } 71f8091a88SPaul Burton 72f8091a88SPaul Burton return start; 73f8091a88SPaul Burton } 74f8091a88SPaul Burton 75f8091a88SPaul Burton static void pcibios_scanbus(struct pci_controller *hose) 76f8091a88SPaul Burton { 77f8091a88SPaul Burton static int next_busno; 78f8091a88SPaul Burton static int need_domain_info; 79f8091a88SPaul Burton LIST_HEAD(resources); 80f8091a88SPaul Burton struct pci_bus *bus; 81f8091a88SPaul Burton 82f8091a88SPaul Burton if (hose->get_busno && pci_has_flag(PCI_PROBE_ONLY)) 83f8091a88SPaul Burton next_busno = (*hose->get_busno)(); 84f8091a88SPaul Burton 85f8091a88SPaul Burton pci_add_resource_offset(&resources, 86f8091a88SPaul Burton hose->mem_resource, hose->mem_offset); 87f8091a88SPaul Burton pci_add_resource_offset(&resources, 88f8091a88SPaul Burton hose->io_resource, hose->io_offset); 89f8091a88SPaul Burton pci_add_resource_offset(&resources, 90f8091a88SPaul Burton hose->busn_resource, hose->busn_offset); 91f8091a88SPaul Burton bus = pci_scan_root_bus(NULL, next_busno, hose->pci_ops, hose, 92f8091a88SPaul Burton &resources); 93f8091a88SPaul Burton hose->bus = bus; 94f8091a88SPaul Burton 95f8091a88SPaul Burton need_domain_info = need_domain_info || pci_domain_nr(bus); 96f8091a88SPaul Burton set_pci_need_domain_info(hose, need_domain_info); 97f8091a88SPaul Burton 98f8091a88SPaul Burton if (!bus) { 99f8091a88SPaul Burton pci_free_resource_list(&resources); 100f8091a88SPaul Burton return; 101f8091a88SPaul Burton } 102f8091a88SPaul Burton 103f8091a88SPaul Burton next_busno = bus->busn_res.end + 1; 104f8091a88SPaul Burton /* Don't allow 8-bit bus number overflow inside the hose - 105f8091a88SPaul Burton reserve some space for bridges. */ 106f8091a88SPaul Burton if (next_busno > 224) { 107f8091a88SPaul Burton next_busno = 0; 108f8091a88SPaul Burton need_domain_info = 1; 109f8091a88SPaul Burton } 110f8091a88SPaul Burton 111f8091a88SPaul Burton /* 112f8091a88SPaul Burton * We insert PCI resources into the iomem_resource and 113f8091a88SPaul Burton * ioport_resource trees in either pci_bus_claim_resources() 114f8091a88SPaul Burton * or pci_bus_assign_resources(). 115f8091a88SPaul Burton */ 116f8091a88SPaul Burton if (pci_has_flag(PCI_PROBE_ONLY)) { 117f8091a88SPaul Burton pci_bus_claim_resources(bus); 118f8091a88SPaul Burton } else { 119f8091a88SPaul Burton pci_bus_size_bridges(bus); 120f8091a88SPaul Burton pci_bus_assign_resources(bus); 121f8091a88SPaul Burton } 122f8091a88SPaul Burton pci_bus_add_devices(bus); 123f8091a88SPaul Burton } 124f8091a88SPaul Burton 125f8091a88SPaul Burton #ifdef CONFIG_OF 126f8091a88SPaul Burton void pci_load_of_ranges(struct pci_controller *hose, struct device_node *node) 127f8091a88SPaul Burton { 128f8091a88SPaul Burton struct of_pci_range range; 129f8091a88SPaul Burton struct of_pci_range_parser parser; 130f8091a88SPaul Burton 131f8091a88SPaul Burton pr_info("PCI host bridge %s ranges:\n", node->full_name); 132f8091a88SPaul Burton hose->of_node = node; 133f8091a88SPaul Burton 134f8091a88SPaul Burton if (of_pci_range_parser_init(&parser, node)) 135f8091a88SPaul Burton return; 136f8091a88SPaul Burton 137f8091a88SPaul Burton for_each_of_pci_range(&parser, &range) { 138f8091a88SPaul Burton struct resource *res = NULL; 139f8091a88SPaul Burton 140f8091a88SPaul Burton switch (range.flags & IORESOURCE_TYPE_BITS) { 141f8091a88SPaul Burton case IORESOURCE_IO: 142f8091a88SPaul Burton pr_info(" IO 0x%016llx..0x%016llx\n", 143f8091a88SPaul Burton range.cpu_addr, 144f8091a88SPaul Burton range.cpu_addr + range.size - 1); 145f8091a88SPaul Burton hose->io_map_base = 146f8091a88SPaul Burton (unsigned long)ioremap(range.cpu_addr, 147f8091a88SPaul Burton range.size); 148f8091a88SPaul Burton res = hose->io_resource; 149f8091a88SPaul Burton break; 150f8091a88SPaul Burton case IORESOURCE_MEM: 151f8091a88SPaul Burton pr_info(" MEM 0x%016llx..0x%016llx\n", 152f8091a88SPaul Burton range.cpu_addr, 153f8091a88SPaul Burton range.cpu_addr + range.size - 1); 154f8091a88SPaul Burton res = hose->mem_resource; 155f8091a88SPaul Burton break; 156f8091a88SPaul Burton } 157f8091a88SPaul Burton if (res != NULL) 158f8091a88SPaul Burton of_pci_range_to_resource(&range, node, res); 159f8091a88SPaul Burton } 160f8091a88SPaul Burton } 161f8091a88SPaul Burton 162f8091a88SPaul Burton struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus) 163f8091a88SPaul Burton { 164f8091a88SPaul Burton struct pci_controller *hose = bus->sysdata; 165f8091a88SPaul Burton 166f8091a88SPaul Burton return of_node_get(hose->of_node); 167f8091a88SPaul Burton } 168f8091a88SPaul Burton #endif 169f8091a88SPaul Burton 170f8091a88SPaul Burton static DEFINE_MUTEX(pci_scan_mutex); 171f8091a88SPaul Burton 172f8091a88SPaul Burton void register_pci_controller(struct pci_controller *hose) 173f8091a88SPaul Burton { 174f8091a88SPaul Burton struct resource *parent; 175f8091a88SPaul Burton 176f8091a88SPaul Burton parent = hose->mem_resource->parent; 177f8091a88SPaul Burton if (!parent) 178f8091a88SPaul Burton parent = &iomem_resource; 179f8091a88SPaul Burton 180f8091a88SPaul Burton if (request_resource(parent, hose->mem_resource) < 0) 181f8091a88SPaul Burton goto out; 182f8091a88SPaul Burton 183f8091a88SPaul Burton parent = hose->io_resource->parent; 184f8091a88SPaul Burton if (!parent) 185f8091a88SPaul Burton parent = &ioport_resource; 186f8091a88SPaul Burton 187f8091a88SPaul Burton if (request_resource(parent, hose->io_resource) < 0) { 188f8091a88SPaul Burton release_resource(hose->mem_resource); 189f8091a88SPaul Burton goto out; 190f8091a88SPaul Burton } 191f8091a88SPaul Burton 192f8091a88SPaul Burton INIT_LIST_HEAD(&hose->list); 193*edb0b6a0SMathias Kresin list_add_tail(&hose->list, &controllers); 194f8091a88SPaul Burton 195f8091a88SPaul Burton /* 196f8091a88SPaul Burton * Do not panic here but later - this might happen before console init. 197f8091a88SPaul Burton */ 198f8091a88SPaul Burton if (!hose->io_map_base) { 199f8091a88SPaul Burton printk(KERN_WARNING 200f8091a88SPaul Burton "registering PCI controller with io_map_base unset\n"); 201f8091a88SPaul Burton } 202f8091a88SPaul Burton 203f8091a88SPaul Burton /* 204f8091a88SPaul Burton * Scan the bus if it is register after the PCI subsystem 205f8091a88SPaul Burton * initialization. 206f8091a88SPaul Burton */ 207f8091a88SPaul Burton if (pci_initialized) { 208f8091a88SPaul Burton mutex_lock(&pci_scan_mutex); 209f8091a88SPaul Burton pcibios_scanbus(hose); 210f8091a88SPaul Burton mutex_unlock(&pci_scan_mutex); 211f8091a88SPaul Burton } 212f8091a88SPaul Burton 213f8091a88SPaul Burton return; 214f8091a88SPaul Burton 215f8091a88SPaul Burton out: 216f8091a88SPaul Burton printk(KERN_WARNING 217f8091a88SPaul Burton "Skipping PCI bus scan due to resource conflict\n"); 218f8091a88SPaul Burton } 219f8091a88SPaul Burton 220f8091a88SPaul Burton static int __init pcibios_init(void) 221f8091a88SPaul Burton { 222f8091a88SPaul Burton struct pci_controller *hose; 223f8091a88SPaul Burton 224f8091a88SPaul Burton /* Scan all of the recorded PCI controllers. */ 225f8091a88SPaul Burton list_for_each_entry(hose, &controllers, list) 226f8091a88SPaul Burton pcibios_scanbus(hose); 227f8091a88SPaul Burton 228f8091a88SPaul Burton pci_fixup_irqs(pci_common_swizzle, pcibios_map_irq); 229f8091a88SPaul Burton 230f8091a88SPaul Burton pci_initialized = 1; 231f8091a88SPaul Burton 232f8091a88SPaul Burton return 0; 233f8091a88SPaul Burton } 234f8091a88SPaul Burton 235f8091a88SPaul Burton subsys_initcall(pcibios_init); 236f8091a88SPaul Burton 237f8091a88SPaul Burton static int pcibios_enable_resources(struct pci_dev *dev, int mask) 238f8091a88SPaul Burton { 239f8091a88SPaul Burton u16 cmd, old_cmd; 240f8091a88SPaul Burton int idx; 241f8091a88SPaul Burton struct resource *r; 242f8091a88SPaul Burton 243f8091a88SPaul Burton pci_read_config_word(dev, PCI_COMMAND, &cmd); 244f8091a88SPaul Burton old_cmd = cmd; 245f8091a88SPaul Burton for (idx=0; idx < PCI_NUM_RESOURCES; idx++) { 246f8091a88SPaul Burton /* Only set up the requested stuff */ 247f8091a88SPaul Burton if (!(mask & (1<<idx))) 248f8091a88SPaul Burton continue; 249f8091a88SPaul Burton 250f8091a88SPaul Burton r = &dev->resource[idx]; 251f8091a88SPaul Burton if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM))) 252f8091a88SPaul Burton continue; 253f8091a88SPaul Burton if ((idx == PCI_ROM_RESOURCE) && 254f8091a88SPaul Burton (!(r->flags & IORESOURCE_ROM_ENABLE))) 255f8091a88SPaul Burton continue; 256f8091a88SPaul Burton if (!r->start && r->end) { 257f8091a88SPaul Burton printk(KERN_ERR "PCI: Device %s not available " 258f8091a88SPaul Burton "because of resource collisions\n", 259f8091a88SPaul Burton pci_name(dev)); 260f8091a88SPaul Burton return -EINVAL; 261f8091a88SPaul Burton } 262f8091a88SPaul Burton if (r->flags & IORESOURCE_IO) 263f8091a88SPaul Burton cmd |= PCI_COMMAND_IO; 264f8091a88SPaul Burton if (r->flags & IORESOURCE_MEM) 265f8091a88SPaul Burton cmd |= PCI_COMMAND_MEMORY; 266f8091a88SPaul Burton } 267f8091a88SPaul Burton if (cmd != old_cmd) { 268f8091a88SPaul Burton printk("PCI: Enabling device %s (%04x -> %04x)\n", 269f8091a88SPaul Burton pci_name(dev), old_cmd, cmd); 270f8091a88SPaul Burton pci_write_config_word(dev, PCI_COMMAND, cmd); 271f8091a88SPaul Burton } 272f8091a88SPaul Burton return 0; 273f8091a88SPaul Burton } 274f8091a88SPaul Burton 275f8091a88SPaul Burton int pcibios_enable_device(struct pci_dev *dev, int mask) 276f8091a88SPaul Burton { 277f8091a88SPaul Burton int err; 278f8091a88SPaul Burton 279f8091a88SPaul Burton if ((err = pcibios_enable_resources(dev, mask)) < 0) 280f8091a88SPaul Burton return err; 281f8091a88SPaul Burton 282f8091a88SPaul Burton return pcibios_plat_dev_init(dev); 283f8091a88SPaul Burton } 284f8091a88SPaul Burton 285f8091a88SPaul Burton void pcibios_fixup_bus(struct pci_bus *bus) 286f8091a88SPaul Burton { 287f8091a88SPaul Burton struct pci_dev *dev = bus->self; 288f8091a88SPaul Burton 289f8091a88SPaul Burton if (pci_has_flag(PCI_PROBE_ONLY) && dev && 290f8091a88SPaul Burton (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) { 291f8091a88SPaul Burton pci_read_bridge_bases(bus); 292f8091a88SPaul Burton } 293f8091a88SPaul Burton } 294f8091a88SPaul Burton 295f8091a88SPaul Burton char * (*pcibios_plat_setup)(char *str) __initdata; 296f8091a88SPaul Burton 297f8091a88SPaul Burton char *__init pcibios_setup(char *str) 298f8091a88SPaul Burton { 299f8091a88SPaul Burton if (pcibios_plat_setup) 300f8091a88SPaul Burton return pcibios_plat_setup(str); 301f8091a88SPaul Burton return str; 302f8091a88SPaul Burton } 303