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