1d3d2176aSDavid Gibson /* 2d3d2176aSDavid Gibson * Copyright (C) 2001 Dave Engebretsen, IBM Corporation 3d3d2176aSDavid Gibson * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM 4d3d2176aSDavid Gibson * 5d3d2176aSDavid Gibson * RTAS specific routines for PCI. 6d3d2176aSDavid Gibson * 7d3d2176aSDavid Gibson * Based on code from pci.c, chrp_pci.c and pSeries_pci.c 8d3d2176aSDavid Gibson * 9d3d2176aSDavid Gibson * This program is free software; you can redistribute it and/or modify 10d3d2176aSDavid Gibson * it under the terms of the GNU General Public License as published by 11d3d2176aSDavid Gibson * the Free Software Foundation; either version 2 of the License, or 12d3d2176aSDavid Gibson * (at your option) any later version. 13d3d2176aSDavid Gibson * 14d3d2176aSDavid Gibson * This program is distributed in the hope that it will be useful, 15d3d2176aSDavid Gibson * but WITHOUT ANY WARRANTY; without even the implied warranty of 16d3d2176aSDavid Gibson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17d3d2176aSDavid Gibson * GNU General Public License for more details. 18d3d2176aSDavid Gibson * 19d3d2176aSDavid Gibson * You should have received a copy of the GNU General Public License 20d3d2176aSDavid Gibson * along with this program; if not, write to the Free Software 21d3d2176aSDavid Gibson * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22d3d2176aSDavid Gibson */ 23d3d2176aSDavid Gibson 24d3d2176aSDavid Gibson #include <linux/kernel.h> 25d3d2176aSDavid Gibson #include <linux/threads.h> 26d3d2176aSDavid Gibson #include <linux/pci.h> 27d3d2176aSDavid Gibson #include <linux/string.h> 28d3d2176aSDavid Gibson #include <linux/init.h> 29d3d2176aSDavid Gibson #include <linux/bootmem.h> 30d3d2176aSDavid Gibson 31d3d2176aSDavid Gibson #include <asm/io.h> 32d3d2176aSDavid Gibson #include <asm/pgtable.h> 33d3d2176aSDavid Gibson #include <asm/irq.h> 34d3d2176aSDavid Gibson #include <asm/prom.h> 35d3d2176aSDavid Gibson #include <asm/machdep.h> 36d3d2176aSDavid Gibson #include <asm/pci-bridge.h> 37d3d2176aSDavid Gibson #include <asm/iommu.h> 38d3d2176aSDavid Gibson #include <asm/rtas.h> 39d3d2176aSDavid Gibson #include <asm/mpic.h> 40d3d2176aSDavid Gibson #include <asm/ppc-pci.h> 4168a64357SBenjamin Herrenschmidt #include <asm/eeh.h> 42d3d2176aSDavid Gibson 43d3d2176aSDavid Gibson /* RTAS tokens */ 44d3d2176aSDavid Gibson static int read_pci_config; 45d3d2176aSDavid Gibson static int write_pci_config; 46d3d2176aSDavid Gibson static int ibm_read_pci_config; 47d3d2176aSDavid Gibson static int ibm_write_pci_config; 48d3d2176aSDavid Gibson 49d3d2176aSDavid Gibson static inline int config_access_valid(struct pci_dn *dn, int where) 50d3d2176aSDavid Gibson { 51d3d2176aSDavid Gibson if (where < 256) 52d3d2176aSDavid Gibson return 1; 53d3d2176aSDavid Gibson if (where < 4096 && dn->pci_ext_config_space) 54d3d2176aSDavid Gibson return 1; 55d3d2176aSDavid Gibson 56d3d2176aSDavid Gibson return 0; 57d3d2176aSDavid Gibson } 58d3d2176aSDavid Gibson 597684b40cSLinas Vepstas int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val) 60d3d2176aSDavid Gibson { 61d3d2176aSDavid Gibson int returnval = -1; 62d3d2176aSDavid Gibson unsigned long buid, addr; 63d3d2176aSDavid Gibson int ret; 64d3d2176aSDavid Gibson 65d3d2176aSDavid Gibson if (!pdn) 66d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 67d3d2176aSDavid Gibson if (!config_access_valid(pdn, where)) 68d3d2176aSDavid Gibson return PCIBIOS_BAD_REGISTER_NUMBER; 69d3d2176aSDavid Gibson 706f3d5d3cSMichael Ellerman addr = rtas_config_addr(pdn->busno, pdn->devfn, where); 71d3d2176aSDavid Gibson buid = pdn->phb->buid; 72d3d2176aSDavid Gibson if (buid) { 73d3d2176aSDavid Gibson ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval, 74d3d2176aSDavid Gibson addr, BUID_HI(buid), BUID_LO(buid), size); 75d3d2176aSDavid Gibson } else { 76d3d2176aSDavid Gibson ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size); 77d3d2176aSDavid Gibson } 78d3d2176aSDavid Gibson *val = returnval; 79d3d2176aSDavid Gibson 80d3d2176aSDavid Gibson if (ret) 81d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 82d3d2176aSDavid Gibson 83d3d2176aSDavid Gibson if (returnval == EEH_IO_ERROR_VALUE(size) && 84d3d2176aSDavid Gibson eeh_dn_check_failure (pdn->node, NULL)) 85d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 86d3d2176aSDavid Gibson 87d3d2176aSDavid Gibson return PCIBIOS_SUCCESSFUL; 88d3d2176aSDavid Gibson } 89d3d2176aSDavid Gibson 90d3d2176aSDavid Gibson static int rtas_pci_read_config(struct pci_bus *bus, 91d3d2176aSDavid Gibson unsigned int devfn, 92d3d2176aSDavid Gibson int where, int size, u32 *val) 93d3d2176aSDavid Gibson { 94d3d2176aSDavid Gibson struct device_node *busdn, *dn; 95d3d2176aSDavid Gibson 96d3d2176aSDavid Gibson if (bus->self) 97d3d2176aSDavid Gibson busdn = pci_device_to_OF_node(bus->self); 98d3d2176aSDavid Gibson else 99d3d2176aSDavid Gibson busdn = bus->sysdata; /* must be a phb */ 100d3d2176aSDavid Gibson 101d3d2176aSDavid Gibson /* Search only direct children of the bus */ 102d3d2176aSDavid Gibson for (dn = busdn->child; dn; dn = dn->sibling) { 103d3d2176aSDavid Gibson struct pci_dn *pdn = PCI_DN(dn); 104d3d2176aSDavid Gibson if (pdn && pdn->devfn == devfn 105c6d4d5a8SNathan Lynch && of_device_is_available(dn)) 106d3d2176aSDavid Gibson return rtas_read_config(pdn, where, size, val); 107d3d2176aSDavid Gibson } 108d3d2176aSDavid Gibson 109d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 110d3d2176aSDavid Gibson } 111d3d2176aSDavid Gibson 112d3d2176aSDavid Gibson int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val) 113d3d2176aSDavid Gibson { 114d3d2176aSDavid Gibson unsigned long buid, addr; 115d3d2176aSDavid Gibson int ret; 116d3d2176aSDavid Gibson 117d3d2176aSDavid Gibson if (!pdn) 118d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 119d3d2176aSDavid Gibson if (!config_access_valid(pdn, where)) 120d3d2176aSDavid Gibson return PCIBIOS_BAD_REGISTER_NUMBER; 121d3d2176aSDavid Gibson 1226f3d5d3cSMichael Ellerman addr = rtas_config_addr(pdn->busno, pdn->devfn, where); 123d3d2176aSDavid Gibson buid = pdn->phb->buid; 124d3d2176aSDavid Gibson if (buid) { 125d3d2176aSDavid Gibson ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, 126d3d2176aSDavid Gibson BUID_HI(buid), BUID_LO(buid), size, (ulong) val); 127d3d2176aSDavid Gibson } else { 128d3d2176aSDavid Gibson ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val); 129d3d2176aSDavid Gibson } 130d3d2176aSDavid Gibson 131d3d2176aSDavid Gibson if (ret) 132d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 133d3d2176aSDavid Gibson 134d3d2176aSDavid Gibson return PCIBIOS_SUCCESSFUL; 135d3d2176aSDavid Gibson } 136d3d2176aSDavid Gibson 137d3d2176aSDavid Gibson static int rtas_pci_write_config(struct pci_bus *bus, 138d3d2176aSDavid Gibson unsigned int devfn, 139d3d2176aSDavid Gibson int where, int size, u32 val) 140d3d2176aSDavid Gibson { 141d3d2176aSDavid Gibson struct device_node *busdn, *dn; 142d3d2176aSDavid Gibson 143d3d2176aSDavid Gibson if (bus->self) 144d3d2176aSDavid Gibson busdn = pci_device_to_OF_node(bus->self); 145d3d2176aSDavid Gibson else 146d3d2176aSDavid Gibson busdn = bus->sysdata; /* must be a phb */ 147d3d2176aSDavid Gibson 148d3d2176aSDavid Gibson /* Search only direct children of the bus */ 149d3d2176aSDavid Gibson for (dn = busdn->child; dn; dn = dn->sibling) { 150d3d2176aSDavid Gibson struct pci_dn *pdn = PCI_DN(dn); 151d3d2176aSDavid Gibson if (pdn && pdn->devfn == devfn 152c6d4d5a8SNathan Lynch && of_device_is_available(dn)) 153d3d2176aSDavid Gibson return rtas_write_config(pdn, where, size, val); 154d3d2176aSDavid Gibson } 155d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 156d3d2176aSDavid Gibson } 157d3d2176aSDavid Gibson 158*1c21a293SMichael Ellerman static struct pci_ops rtas_pci_ops = { 1598674e0c9SNathan Lynch .read = rtas_pci_read_config, 1608674e0c9SNathan Lynch .write = rtas_pci_write_config, 161d3d2176aSDavid Gibson }; 162d3d2176aSDavid Gibson 163*1c21a293SMichael Ellerman static int is_python(struct device_node *dev) 164d3d2176aSDavid Gibson { 165e2eb6392SStephen Rothwell const char *model = of_get_property(dev, "model", NULL); 166d3d2176aSDavid Gibson 167d3d2176aSDavid Gibson if (model && strstr(model, "Python")) 168d3d2176aSDavid Gibson return 1; 169d3d2176aSDavid Gibson 170d3d2176aSDavid Gibson return 0; 171d3d2176aSDavid Gibson } 172d3d2176aSDavid Gibson 173cc5d0189SBenjamin Herrenschmidt static void python_countermeasures(struct device_node *dev) 174d3d2176aSDavid Gibson { 175cc5d0189SBenjamin Herrenschmidt struct resource registers; 176d3d2176aSDavid Gibson void __iomem *chip_regs; 177d3d2176aSDavid Gibson volatile u32 val; 178d3d2176aSDavid Gibson 179cc5d0189SBenjamin Herrenschmidt if (of_address_to_resource(dev, 0, ®isters)) { 180cc5d0189SBenjamin Herrenschmidt printk(KERN_ERR "Can't get address for Python workarounds !\n"); 181d3d2176aSDavid Gibson return; 182cc5d0189SBenjamin Herrenschmidt } 183d3d2176aSDavid Gibson 184d3d2176aSDavid Gibson /* Python's register file is 1 MB in size. */ 185cc5d0189SBenjamin Herrenschmidt chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000); 186d3d2176aSDavid Gibson 187d3d2176aSDavid Gibson /* 188d3d2176aSDavid Gibson * Firmware doesn't always clear this bit which is critical 189d3d2176aSDavid Gibson * for good performance - Anton 190d3d2176aSDavid Gibson */ 191d3d2176aSDavid Gibson 192d3d2176aSDavid Gibson #define PRG_CL_RESET_VALID 0x00010000 193d3d2176aSDavid Gibson 194d3d2176aSDavid Gibson val = in_be32(chip_regs + 0xf6030); 195d3d2176aSDavid Gibson if (val & PRG_CL_RESET_VALID) { 196d3d2176aSDavid Gibson printk(KERN_INFO "Python workaround: "); 197d3d2176aSDavid Gibson val &= ~PRG_CL_RESET_VALID; 198d3d2176aSDavid Gibson out_be32(chip_regs + 0xf6030, val); 199d3d2176aSDavid Gibson /* 200d3d2176aSDavid Gibson * We must read it back for changes to 201d3d2176aSDavid Gibson * take effect 202d3d2176aSDavid Gibson */ 203d3d2176aSDavid Gibson val = in_be32(chip_regs + 0xf6030); 204d3d2176aSDavid Gibson printk("reg0: %x\n", val); 205d3d2176aSDavid Gibson } 206d3d2176aSDavid Gibson 207d3d2176aSDavid Gibson iounmap(chip_regs); 208d3d2176aSDavid Gibson } 209d3d2176aSDavid Gibson 210d3d2176aSDavid Gibson void __init init_pci_config_tokens (void) 211d3d2176aSDavid Gibson { 212d3d2176aSDavid Gibson read_pci_config = rtas_token("read-pci-config"); 213d3d2176aSDavid Gibson write_pci_config = rtas_token("write-pci-config"); 214d3d2176aSDavid Gibson ibm_read_pci_config = rtas_token("ibm,read-pci-config"); 215d3d2176aSDavid Gibson ibm_write_pci_config = rtas_token("ibm,write-pci-config"); 216d3d2176aSDavid Gibson } 217d3d2176aSDavid Gibson 218d3d2176aSDavid Gibson unsigned long __devinit get_phb_buid (struct device_node *phb) 219d3d2176aSDavid Gibson { 2206506e710SBenjamin Herrenschmidt struct resource r; 221d3d2176aSDavid Gibson 2226506e710SBenjamin Herrenschmidt if (ibm_read_pci_config == -1) 223d3d2176aSDavid Gibson return 0; 2246506e710SBenjamin Herrenschmidt if (of_address_to_resource(phb, 0, &r)) 225d3d2176aSDavid Gibson return 0; 2266506e710SBenjamin Herrenschmidt return r.start; 227d3d2176aSDavid Gibson } 228d3d2176aSDavid Gibson 229d3d2176aSDavid Gibson static int phb_set_bus_ranges(struct device_node *dev, 230d3d2176aSDavid Gibson struct pci_controller *phb) 231d3d2176aSDavid Gibson { 232a7f67bdfSJeremy Kerr const int *bus_range; 233d3d2176aSDavid Gibson unsigned int len; 234d3d2176aSDavid Gibson 235e2eb6392SStephen Rothwell bus_range = of_get_property(dev, "bus-range", &len); 236d3d2176aSDavid Gibson if (bus_range == NULL || len < 2 * sizeof(int)) { 237d3d2176aSDavid Gibson return 1; 238d3d2176aSDavid Gibson } 239d3d2176aSDavid Gibson 240d3d2176aSDavid Gibson phb->first_busno = bus_range[0]; 241d3d2176aSDavid Gibson phb->last_busno = bus_range[1]; 242d3d2176aSDavid Gibson 243d3d2176aSDavid Gibson return 0; 244d3d2176aSDavid Gibson } 245d3d2176aSDavid Gibson 2464c9d2800SBenjamin Herrenschmidt int __devinit rtas_setup_phb(struct pci_controller *phb) 247d3d2176aSDavid Gibson { 24844ef3390SStephen Rothwell struct device_node *dev = phb->dn; 2494c9d2800SBenjamin Herrenschmidt 250d3d2176aSDavid Gibson if (is_python(dev)) 251cc5d0189SBenjamin Herrenschmidt python_countermeasures(dev); 252d3d2176aSDavid Gibson 253d3d2176aSDavid Gibson if (phb_set_bus_ranges(dev, phb)) 254d3d2176aSDavid Gibson return 1; 255d3d2176aSDavid Gibson 256d3d2176aSDavid Gibson phb->ops = &rtas_pci_ops; 257d3d2176aSDavid Gibson phb->buid = get_phb_buid(dev); 258d3d2176aSDavid Gibson 259d3d2176aSDavid Gibson return 0; 260d3d2176aSDavid Gibson } 261d3d2176aSDavid Gibson 26236241ce6SStephen Rothwell void __init find_and_init_phbs(void) 263d3d2176aSDavid Gibson { 264d3d2176aSDavid Gibson struct device_node *node; 265d3d2176aSDavid Gibson struct pci_controller *phb; 266d3d2176aSDavid Gibson struct device_node *root = of_find_node_by_path("/"); 267d3d2176aSDavid Gibson 26885e99b9fSStephen Rothwell for_each_child_of_node(root, node) { 269bb53bb3dSJake Moilanen if (node->type == NULL || (strcmp(node->type, "pci") != 0 && 270bb53bb3dSJake Moilanen strcmp(node->type, "pciex") != 0)) 271d3d2176aSDavid Gibson continue; 272d3d2176aSDavid Gibson 273b5166cc2SBenjamin Herrenschmidt phb = pcibios_alloc_controller(node); 274d3d2176aSDavid Gibson if (!phb) 275d3d2176aSDavid Gibson continue; 2764c9d2800SBenjamin Herrenschmidt rtas_setup_phb(phb); 277d3d2176aSDavid Gibson pci_process_bridge_OF_ranges(phb, node, 0); 2783d5134eeSBenjamin Herrenschmidt isa_bridge_find_early(phb); 279d3d2176aSDavid Gibson } 280d3d2176aSDavid Gibson 281d3d2176aSDavid Gibson of_node_put(root); 282d3d2176aSDavid Gibson pci_devs_phb_init(); 283d3d2176aSDavid Gibson 284d3d2176aSDavid Gibson /* 285d3d2176aSDavid Gibson * pci_probe_only and pci_assign_all_buses can be set via properties 286d3d2176aSDavid Gibson * in chosen. 287d3d2176aSDavid Gibson */ 288d3d2176aSDavid Gibson if (of_chosen) { 289a7f67bdfSJeremy Kerr const int *prop; 290d3d2176aSDavid Gibson 291e2eb6392SStephen Rothwell prop = of_get_property(of_chosen, 292a7f67bdfSJeremy Kerr "linux,pci-probe-only", NULL); 293d3d2176aSDavid Gibson if (prop) 294d3d2176aSDavid Gibson pci_probe_only = *prop; 295d3d2176aSDavid Gibson 296fc3fb71cSBenjamin Herrenschmidt #ifdef CONFIG_PPC32 /* Will be made generic soon */ 297e2eb6392SStephen Rothwell prop = of_get_property(of_chosen, 298d3d2176aSDavid Gibson "linux,pci-assign-all-buses", NULL); 299fc3fb71cSBenjamin Herrenschmidt if (prop && *prop) 300fc3fb71cSBenjamin Herrenschmidt ppc_pci_flags |= PPC_PCI_REASSIGN_ALL_BUS; 301fc3fb71cSBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */ 302d3d2176aSDavid Gibson } 303d3d2176aSDavid Gibson } 304d3d2176aSDavid Gibson 305d3d2176aSDavid Gibson /* RPA-specific bits for removing PHBs */ 306d3d2176aSDavid Gibson int pcibios_remove_root_bus(struct pci_controller *phb) 307d3d2176aSDavid Gibson { 308d3d2176aSDavid Gibson struct pci_bus *b = phb->bus; 309d3d2176aSDavid Gibson struct resource *res; 310d3d2176aSDavid Gibson int rc, i; 311d3d2176aSDavid Gibson 312d3d2176aSDavid Gibson res = b->resource[0]; 313d3d2176aSDavid Gibson if (!res->flags) { 314e48b1b45SHarvey Harrison printk(KERN_ERR "%s: no IO resource for PHB %s\n", __func__, 315d3d2176aSDavid Gibson b->name); 316d3d2176aSDavid Gibson return 1; 317d3d2176aSDavid Gibson } 318d3d2176aSDavid Gibson 3193d5134eeSBenjamin Herrenschmidt rc = pcibios_unmap_io_space(b); 320d3d2176aSDavid Gibson if (rc) { 321d3d2176aSDavid Gibson printk(KERN_ERR "%s: failed to unmap IO on bus %s\n", 322e48b1b45SHarvey Harrison __func__, b->name); 323d3d2176aSDavid Gibson return 1; 324d3d2176aSDavid Gibson } 325d3d2176aSDavid Gibson 326d3d2176aSDavid Gibson if (release_resource(res)) { 327d3d2176aSDavid Gibson printk(KERN_ERR "%s: failed to release IO on bus %s\n", 328e48b1b45SHarvey Harrison __func__, b->name); 329d3d2176aSDavid Gibson return 1; 330d3d2176aSDavid Gibson } 331d3d2176aSDavid Gibson 332d3d2176aSDavid Gibson for (i = 1; i < 3; ++i) { 333d3d2176aSDavid Gibson res = b->resource[i]; 334d3d2176aSDavid Gibson if (!res->flags && i == 0) { 335d3d2176aSDavid Gibson printk(KERN_ERR "%s: no MEM resource for PHB %s\n", 336e48b1b45SHarvey Harrison __func__, b->name); 337d3d2176aSDavid Gibson return 1; 338d3d2176aSDavid Gibson } 339d3d2176aSDavid Gibson if (res->flags && release_resource(res)) { 340d3d2176aSDavid Gibson printk(KERN_ERR 341d3d2176aSDavid Gibson "%s: failed to release IO %d on bus %s\n", 342e48b1b45SHarvey Harrison __func__, i, b->name); 343d3d2176aSDavid Gibson return 1; 344d3d2176aSDavid Gibson } 345d3d2176aSDavid Gibson } 346d3d2176aSDavid Gibson 347b5166cc2SBenjamin Herrenschmidt pcibios_free_controller(phb); 348d3d2176aSDavid Gibson 349d3d2176aSDavid Gibson return 0; 350d3d2176aSDavid Gibson } 351d3d2176aSDavid Gibson EXPORT_SYMBOL(pcibios_remove_root_bus); 352