1249ac17eSChris Zankel /* 2f30c2269SUwe Zeisberger * arch/xtensa/lib/pci-auto.c 3249ac17eSChris Zankel * 4249ac17eSChris Zankel * PCI autoconfiguration library 5249ac17eSChris Zankel * 6249ac17eSChris Zankel * Copyright (C) 2001 - 2005 Tensilica Inc. 7249ac17eSChris Zankel * 8249ac17eSChris Zankel * Chris Zankel <zankel@tensilica.com, cez@zankel.net> 9249ac17eSChris Zankel * 10249ac17eSChris Zankel * Based on work from Matt Porter <mporter@mvista.com> 11249ac17eSChris Zankel * 12249ac17eSChris Zankel * This program is free software; you can redistribute it and/or modify it 13249ac17eSChris Zankel * under the terms of the GNU General Public License as published by the 14249ac17eSChris Zankel * Free Software Foundation; either version 2 of the License, or (at your 15249ac17eSChris Zankel * option) any later version. 16249ac17eSChris Zankel */ 17249ac17eSChris Zankel 18249ac17eSChris Zankel #include <linux/kernel.h> 19249ac17eSChris Zankel #include <linux/init.h> 20249ac17eSChris Zankel #include <linux/pci.h> 21249ac17eSChris Zankel 22249ac17eSChris Zankel #include <asm/pci-bridge.h> 23249ac17eSChris Zankel 24249ac17eSChris Zankel 25249ac17eSChris Zankel /* 26249ac17eSChris Zankel * 27249ac17eSChris Zankel * Setting up a PCI 28249ac17eSChris Zankel * 29249ac17eSChris Zankel * pci_ctrl->first_busno = <first bus number (0)> 30249ac17eSChris Zankel * pci_ctrl->last_busno = <last bus number (0xff)> 31249ac17eSChris Zankel * pci_ctrl->ops = <PCI config operations> 32249ac17eSChris Zankel * pci_ctrl->map_irq = <function to return the interrupt number for a device> 33249ac17eSChris Zankel * 34249ac17eSChris Zankel * pci_ctrl->io_space.start = <IO space start address (PCI view)> 35249ac17eSChris Zankel * pci_ctrl->io_space.end = <IO space end address (PCI view)> 36249ac17eSChris Zankel * pci_ctrl->io_space.base = <IO space offset: address 0 from CPU space> 37249ac17eSChris Zankel * pci_ctrl->mem_space.start = <MEM space start address (PCI view)> 38249ac17eSChris Zankel * pci_ctrl->mem_space.end = <MEM space end address (PCI view)> 39249ac17eSChris Zankel * pci_ctrl->mem_space.base = <MEM space offset: address 0 from CPU space> 40249ac17eSChris Zankel * 41249ac17eSChris Zankel * pcibios_init_resource(&pci_ctrl->io_resource, <IO space start>, 42249ac17eSChris Zankel * <IO space end>, IORESOURCE_IO, "PCI host bridge"); 43249ac17eSChris Zankel * pcibios_init_resource(&pci_ctrl->mem_resources[0], <MEM space start>, 44249ac17eSChris Zankel * <MEM space end>, IORESOURCE_MEM, "PCI host bridge"); 45249ac17eSChris Zankel * 46249ac17eSChris Zankel * pci_ctrl->last_busno = pciauto_bus_scan(pci_ctrl,pci_ctrl->first_busno); 47249ac17eSChris Zankel * 48249ac17eSChris Zankel * int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus) 49249ac17eSChris Zankel * 50249ac17eSChris Zankel */ 51249ac17eSChris Zankel 52249ac17eSChris Zankel static int pciauto_upper_iospc; 53249ac17eSChris Zankel static int pciauto_upper_memspc; 54249ac17eSChris Zankel 55249ac17eSChris Zankel static struct pci_dev pciauto_dev; 56249ac17eSChris Zankel static struct pci_bus pciauto_bus; 57249ac17eSChris Zankel 58249ac17eSChris Zankel /* 59249ac17eSChris Zankel * Helper functions 60249ac17eSChris Zankel */ 61249ac17eSChris Zankel 62249ac17eSChris Zankel /* Initialize the bars of a PCI device. */ 63249ac17eSChris Zankel 64249ac17eSChris Zankel static void __init 65249ac17eSChris Zankel pciauto_setup_bars(struct pci_dev *dev, int bar_limit) 66249ac17eSChris Zankel { 67249ac17eSChris Zankel int bar_size; 68249ac17eSChris Zankel int bar, bar_nr; 69249ac17eSChris Zankel int *upper_limit; 70249ac17eSChris Zankel int found_mem64 = 0; 71249ac17eSChris Zankel 72249ac17eSChris Zankel for (bar = PCI_BASE_ADDRESS_0, bar_nr = 0; 73249ac17eSChris Zankel bar <= bar_limit; 74249ac17eSChris Zankel bar+=4, bar_nr++) 75249ac17eSChris Zankel { 76249ac17eSChris Zankel /* Tickle the BAR and get the size */ 77249ac17eSChris Zankel pci_write_config_dword(dev, bar, 0xffffffff); 78249ac17eSChris Zankel pci_read_config_dword(dev, bar, &bar_size); 79249ac17eSChris Zankel 80249ac17eSChris Zankel /* If BAR is not implemented go to the next BAR */ 81249ac17eSChris Zankel if (!bar_size) 82249ac17eSChris Zankel continue; 83249ac17eSChris Zankel 84249ac17eSChris Zankel /* Check the BAR type and set our address mask */ 85249ac17eSChris Zankel if (bar_size & PCI_BASE_ADDRESS_SPACE_IO) 86249ac17eSChris Zankel { 87249ac17eSChris Zankel bar_size &= PCI_BASE_ADDRESS_IO_MASK; 88249ac17eSChris Zankel upper_limit = &pciauto_upper_iospc; 89*c130d3beSMax Filippov pr_debug("PCI Autoconfig: BAR %d, I/O, ", bar_nr); 90249ac17eSChris Zankel } 91249ac17eSChris Zankel else 92249ac17eSChris Zankel { 93249ac17eSChris Zankel if ((bar_size & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == 94249ac17eSChris Zankel PCI_BASE_ADDRESS_MEM_TYPE_64) 95249ac17eSChris Zankel found_mem64 = 1; 96249ac17eSChris Zankel 97249ac17eSChris Zankel bar_size &= PCI_BASE_ADDRESS_MEM_MASK; 98249ac17eSChris Zankel upper_limit = &pciauto_upper_memspc; 99*c130d3beSMax Filippov pr_debug("PCI Autoconfig: BAR %d, Mem, ", bar_nr); 100249ac17eSChris Zankel } 101249ac17eSChris Zankel 102249ac17eSChris Zankel /* Allocate a base address (bar_size is negative!) */ 103249ac17eSChris Zankel *upper_limit = (*upper_limit + bar_size) & bar_size; 104249ac17eSChris Zankel 105249ac17eSChris Zankel /* Write it out and update our limit */ 106249ac17eSChris Zankel pci_write_config_dword(dev, bar, *upper_limit); 107249ac17eSChris Zankel 108249ac17eSChris Zankel /* 109249ac17eSChris Zankel * If we are a 64-bit decoder then increment to the 110249ac17eSChris Zankel * upper 32 bits of the bar and force it to locate 111249ac17eSChris Zankel * in the lower 4GB of memory. 112249ac17eSChris Zankel */ 113249ac17eSChris Zankel 114249ac17eSChris Zankel if (found_mem64) 115249ac17eSChris Zankel pci_write_config_dword(dev, (bar+=4), 0x00000000); 116249ac17eSChris Zankel 117*c130d3beSMax Filippov pr_debug("size=0x%x, address=0x%x\n", 118*c130d3beSMax Filippov ~bar_size + 1, *upper_limit); 119249ac17eSChris Zankel } 120249ac17eSChris Zankel } 121249ac17eSChris Zankel 122249ac17eSChris Zankel /* Initialize the interrupt number. */ 123249ac17eSChris Zankel 124249ac17eSChris Zankel static void __init 125249ac17eSChris Zankel pciauto_setup_irq(struct pci_controller* pci_ctrl,struct pci_dev *dev,int devfn) 126249ac17eSChris Zankel { 127249ac17eSChris Zankel u8 pin; 128249ac17eSChris Zankel int irq = 0; 129249ac17eSChris Zankel 130249ac17eSChris Zankel pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin); 131249ac17eSChris Zankel 132249ac17eSChris Zankel /* Fix illegal pin numbers. */ 133249ac17eSChris Zankel 134249ac17eSChris Zankel if (pin == 0 || pin > 4) 135249ac17eSChris Zankel pin = 1; 136249ac17eSChris Zankel 137249ac17eSChris Zankel if (pci_ctrl->map_irq) 138249ac17eSChris Zankel irq = pci_ctrl->map_irq(dev, PCI_SLOT(devfn), pin); 139249ac17eSChris Zankel 140249ac17eSChris Zankel if (irq == -1) 141249ac17eSChris Zankel irq = 0; 142249ac17eSChris Zankel 143*c130d3beSMax Filippov pr_debug("PCI Autoconfig: Interrupt %d, pin %d\n", irq, pin); 144249ac17eSChris Zankel 145249ac17eSChris Zankel pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); 146249ac17eSChris Zankel } 147249ac17eSChris Zankel 148249ac17eSChris Zankel 149249ac17eSChris Zankel static void __init 150249ac17eSChris Zankel pciauto_prescan_setup_bridge(struct pci_dev *dev, int current_bus, 151249ac17eSChris Zankel int sub_bus, int *iosave, int *memsave) 152249ac17eSChris Zankel { 153249ac17eSChris Zankel /* Configure bus number registers */ 154249ac17eSChris Zankel pci_write_config_byte(dev, PCI_PRIMARY_BUS, current_bus); 155249ac17eSChris Zankel pci_write_config_byte(dev, PCI_SECONDARY_BUS, sub_bus + 1); 156249ac17eSChris Zankel pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, 0xff); 157249ac17eSChris Zankel 158249ac17eSChris Zankel /* Round memory allocator to 1MB boundary */ 159249ac17eSChris Zankel pciauto_upper_memspc &= ~(0x100000 - 1); 160249ac17eSChris Zankel *memsave = pciauto_upper_memspc; 161249ac17eSChris Zankel 162249ac17eSChris Zankel /* Round I/O allocator to 4KB boundary */ 163249ac17eSChris Zankel pciauto_upper_iospc &= ~(0x1000 - 1); 164249ac17eSChris Zankel *iosave = pciauto_upper_iospc; 165249ac17eSChris Zankel 166249ac17eSChris Zankel /* Set up memory and I/O filter limits, assume 32-bit I/O space */ 167249ac17eSChris Zankel pci_write_config_word(dev, PCI_MEMORY_LIMIT, 168249ac17eSChris Zankel ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16); 169249ac17eSChris Zankel pci_write_config_byte(dev, PCI_IO_LIMIT, 170249ac17eSChris Zankel ((pciauto_upper_iospc - 1) & 0x0000f000) >> 8); 171249ac17eSChris Zankel pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16, 172249ac17eSChris Zankel ((pciauto_upper_iospc - 1) & 0xffff0000) >> 16); 173249ac17eSChris Zankel } 174249ac17eSChris Zankel 175249ac17eSChris Zankel static void __init 176249ac17eSChris Zankel pciauto_postscan_setup_bridge(struct pci_dev *dev, int current_bus, int sub_bus, 177249ac17eSChris Zankel int *iosave, int *memsave) 178249ac17eSChris Zankel { 179249ac17eSChris Zankel int cmdstat; 180249ac17eSChris Zankel 181249ac17eSChris Zankel /* Configure bus number registers */ 182249ac17eSChris Zankel pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, sub_bus); 183249ac17eSChris Zankel 184249ac17eSChris Zankel /* 185249ac17eSChris Zankel * Round memory allocator to 1MB boundary. 186249ac17eSChris Zankel * If no space used, allocate minimum. 187249ac17eSChris Zankel */ 188249ac17eSChris Zankel pciauto_upper_memspc &= ~(0x100000 - 1); 189249ac17eSChris Zankel if (*memsave == pciauto_upper_memspc) 190249ac17eSChris Zankel pciauto_upper_memspc -= 0x00100000; 191249ac17eSChris Zankel 192249ac17eSChris Zankel pci_write_config_word(dev, PCI_MEMORY_BASE, pciauto_upper_memspc >> 16); 193249ac17eSChris Zankel 194249ac17eSChris Zankel /* Allocate 1MB for pre-fretch */ 195249ac17eSChris Zankel pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, 196249ac17eSChris Zankel ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16); 197249ac17eSChris Zankel 198249ac17eSChris Zankel pciauto_upper_memspc -= 0x100000; 199249ac17eSChris Zankel 200249ac17eSChris Zankel pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, 201249ac17eSChris Zankel pciauto_upper_memspc >> 16); 202249ac17eSChris Zankel 203249ac17eSChris Zankel /* Round I/O allocator to 4KB boundary */ 204249ac17eSChris Zankel pciauto_upper_iospc &= ~(0x1000 - 1); 205249ac17eSChris Zankel if (*iosave == pciauto_upper_iospc) 206249ac17eSChris Zankel pciauto_upper_iospc -= 0x1000; 207249ac17eSChris Zankel 208249ac17eSChris Zankel pci_write_config_byte(dev, PCI_IO_BASE, 209249ac17eSChris Zankel (pciauto_upper_iospc & 0x0000f000) >> 8); 210249ac17eSChris Zankel pci_write_config_word(dev, PCI_IO_BASE_UPPER16, 211249ac17eSChris Zankel pciauto_upper_iospc >> 16); 212249ac17eSChris Zankel 213249ac17eSChris Zankel /* Enable memory and I/O accesses, enable bus master */ 214249ac17eSChris Zankel pci_read_config_dword(dev, PCI_COMMAND, &cmdstat); 215249ac17eSChris Zankel pci_write_config_dword(dev, PCI_COMMAND, 216249ac17eSChris Zankel cmdstat | 217249ac17eSChris Zankel PCI_COMMAND_IO | 218249ac17eSChris Zankel PCI_COMMAND_MEMORY | 219249ac17eSChris Zankel PCI_COMMAND_MASTER); 220249ac17eSChris Zankel } 221249ac17eSChris Zankel 222249ac17eSChris Zankel /* 223249ac17eSChris Zankel * Scan the current PCI bus. 224249ac17eSChris Zankel */ 225249ac17eSChris Zankel 226249ac17eSChris Zankel 227249ac17eSChris Zankel int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus) 228249ac17eSChris Zankel { 229249ac17eSChris Zankel int sub_bus, pci_devfn, pci_class, cmdstat, found_multi=0; 230249ac17eSChris Zankel unsigned short vid; 231249ac17eSChris Zankel unsigned char header_type; 232249ac17eSChris Zankel struct pci_dev *dev = &pciauto_dev; 233249ac17eSChris Zankel 234249ac17eSChris Zankel pciauto_dev.bus = &pciauto_bus; 235249ac17eSChris Zankel pciauto_dev.sysdata = pci_ctrl; 236249ac17eSChris Zankel pciauto_bus.ops = pci_ctrl->ops; 237249ac17eSChris Zankel 238249ac17eSChris Zankel /* 239249ac17eSChris Zankel * Fetch our I/O and memory space upper boundaries used 240249ac17eSChris Zankel * to allocated base addresses on this pci_controller. 241249ac17eSChris Zankel */ 242249ac17eSChris Zankel 243249ac17eSChris Zankel if (current_bus == pci_ctrl->first_busno) 244249ac17eSChris Zankel { 245249ac17eSChris Zankel pciauto_upper_iospc = pci_ctrl->io_resource.end + 1; 246249ac17eSChris Zankel pciauto_upper_memspc = pci_ctrl->mem_resources[0].end + 1; 247249ac17eSChris Zankel } 248249ac17eSChris Zankel 249249ac17eSChris Zankel sub_bus = current_bus; 250249ac17eSChris Zankel 251249ac17eSChris Zankel for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) 252249ac17eSChris Zankel { 253249ac17eSChris Zankel /* Skip our host bridge */ 254249ac17eSChris Zankel if ((current_bus == pci_ctrl->first_busno) && (pci_devfn == 0)) 255249ac17eSChris Zankel continue; 256249ac17eSChris Zankel 257249ac17eSChris Zankel if (PCI_FUNC(pci_devfn) && !found_multi) 258249ac17eSChris Zankel continue; 259249ac17eSChris Zankel 260249ac17eSChris Zankel pciauto_bus.number = current_bus; 261249ac17eSChris Zankel pciauto_dev.devfn = pci_devfn; 262249ac17eSChris Zankel 263249ac17eSChris Zankel /* If config space read fails from this device, move on */ 264249ac17eSChris Zankel if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type)) 265249ac17eSChris Zankel continue; 266249ac17eSChris Zankel 267249ac17eSChris Zankel if (!PCI_FUNC(pci_devfn)) 268249ac17eSChris Zankel found_multi = header_type & 0x80; 269249ac17eSChris Zankel pci_read_config_word(dev, PCI_VENDOR_ID, &vid); 270249ac17eSChris Zankel 271249ac17eSChris Zankel if (vid == 0xffff || vid == 0x0000) { 272249ac17eSChris Zankel found_multi = 0; 273249ac17eSChris Zankel continue; 274249ac17eSChris Zankel } 275249ac17eSChris Zankel 276249ac17eSChris Zankel pci_read_config_dword(dev, PCI_CLASS_REVISION, &pci_class); 277249ac17eSChris Zankel 278249ac17eSChris Zankel if ((pci_class >> 16) == PCI_CLASS_BRIDGE_PCI) { 279249ac17eSChris Zankel 280249ac17eSChris Zankel int iosave, memsave; 281249ac17eSChris Zankel 282*c130d3beSMax Filippov pr_debug("PCI Autoconfig: Found P2P bridge, device %d\n", 283249ac17eSChris Zankel PCI_SLOT(pci_devfn)); 284249ac17eSChris Zankel 285249ac17eSChris Zankel /* Allocate PCI I/O and/or memory space */ 286249ac17eSChris Zankel pciauto_setup_bars(dev, PCI_BASE_ADDRESS_1); 287249ac17eSChris Zankel 288249ac17eSChris Zankel pciauto_prescan_setup_bridge(dev, current_bus, sub_bus, 289249ac17eSChris Zankel &iosave, &memsave); 290249ac17eSChris Zankel sub_bus = pciauto_bus_scan(pci_ctrl, sub_bus+1); 291249ac17eSChris Zankel pciauto_postscan_setup_bridge(dev, current_bus, sub_bus, 292249ac17eSChris Zankel &iosave, &memsave); 293249ac17eSChris Zankel pciauto_bus.number = current_bus; 294249ac17eSChris Zankel 295249ac17eSChris Zankel continue; 296249ac17eSChris Zankel 297249ac17eSChris Zankel } 298249ac17eSChris Zankel 299249ac17eSChris Zankel /* 300249ac17eSChris Zankel * Found a peripheral, enable some standard 301249ac17eSChris Zankel * settings 302249ac17eSChris Zankel */ 303249ac17eSChris Zankel 304249ac17eSChris Zankel pci_read_config_dword(dev, PCI_COMMAND, &cmdstat); 305249ac17eSChris Zankel pci_write_config_dword(dev, PCI_COMMAND, 306249ac17eSChris Zankel cmdstat | 307249ac17eSChris Zankel PCI_COMMAND_IO | 308249ac17eSChris Zankel PCI_COMMAND_MEMORY | 309249ac17eSChris Zankel PCI_COMMAND_MASTER); 310249ac17eSChris Zankel pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x80); 311249ac17eSChris Zankel 312249ac17eSChris Zankel /* Allocate PCI I/O and/or memory space */ 313*c130d3beSMax Filippov pr_debug("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n", 314249ac17eSChris Zankel current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn)); 315249ac17eSChris Zankel 316249ac17eSChris Zankel pciauto_setup_bars(dev, PCI_BASE_ADDRESS_5); 317249ac17eSChris Zankel pciauto_setup_irq(pci_ctrl, dev, pci_devfn); 318249ac17eSChris Zankel } 319249ac17eSChris Zankel return sub_bus; 320249ac17eSChris Zankel } 321