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 30d3d2176aSDavid Gibson #include <asm/io.h> 31d3d2176aSDavid Gibson #include <asm/pgtable.h> 32d3d2176aSDavid Gibson #include <asm/irq.h> 33d3d2176aSDavid Gibson #include <asm/prom.h> 34d3d2176aSDavid Gibson #include <asm/machdep.h> 35d3d2176aSDavid Gibson #include <asm/pci-bridge.h> 36d3d2176aSDavid Gibson #include <asm/iommu.h> 37d3d2176aSDavid Gibson #include <asm/rtas.h> 38d3d2176aSDavid Gibson #include <asm/mpic.h> 39d3d2176aSDavid Gibson #include <asm/ppc-pci.h> 4068a64357SBenjamin Herrenschmidt #include <asm/eeh.h> 41d3d2176aSDavid Gibson 42d3d2176aSDavid Gibson /* RTAS tokens */ 43d3d2176aSDavid Gibson static int read_pci_config; 44d3d2176aSDavid Gibson static int write_pci_config; 45d3d2176aSDavid Gibson static int ibm_read_pci_config; 46d3d2176aSDavid Gibson static int ibm_write_pci_config; 47d3d2176aSDavid Gibson 48d3d2176aSDavid Gibson static inline int config_access_valid(struct pci_dn *dn, int where) 49d3d2176aSDavid Gibson { 50d3d2176aSDavid Gibson if (where < 256) 51d3d2176aSDavid Gibson return 1; 52d3d2176aSDavid Gibson if (where < 4096 && dn->pci_ext_config_space) 53d3d2176aSDavid Gibson return 1; 54d3d2176aSDavid Gibson 55d3d2176aSDavid Gibson return 0; 56d3d2176aSDavid Gibson } 57d3d2176aSDavid Gibson 587684b40cSLinas Vepstas int rtas_read_config(struct pci_dn *pdn, int where, int size, u32 *val) 59d3d2176aSDavid Gibson { 60d3d2176aSDavid Gibson int returnval = -1; 61d3d2176aSDavid Gibson unsigned long buid, addr; 62d3d2176aSDavid Gibson int ret; 63d3d2176aSDavid Gibson 64d3d2176aSDavid Gibson if (!pdn) 65d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 66d3d2176aSDavid Gibson if (!config_access_valid(pdn, where)) 67d3d2176aSDavid Gibson return PCIBIOS_BAD_REGISTER_NUMBER; 683409eb4eSGavin Shan #ifdef CONFIG_EEH 693409eb4eSGavin Shan if (pdn->edev && pdn->edev->pe && 703409eb4eSGavin Shan (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED)) 713409eb4eSGavin Shan return PCIBIOS_SET_FAILED; 723409eb4eSGavin Shan #endif 73d3d2176aSDavid Gibson 746f3d5d3cSMichael Ellerman addr = rtas_config_addr(pdn->busno, pdn->devfn, where); 75d3d2176aSDavid Gibson buid = pdn->phb->buid; 76d3d2176aSDavid Gibson if (buid) { 77d3d2176aSDavid Gibson ret = rtas_call(ibm_read_pci_config, 4, 2, &returnval, 78d3d2176aSDavid Gibson addr, BUID_HI(buid), BUID_LO(buid), size); 79d3d2176aSDavid Gibson } else { 80d3d2176aSDavid Gibson ret = rtas_call(read_pci_config, 2, 2, &returnval, addr, size); 81d3d2176aSDavid Gibson } 82d3d2176aSDavid Gibson *val = returnval; 83d3d2176aSDavid Gibson 84d3d2176aSDavid Gibson if (ret) 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 { 94d0914f50SGavin Shan struct pci_dn *pdn; 95d0914f50SGavin Shan int ret; 96d3d2176aSDavid Gibson 97d0914f50SGavin Shan *val = 0xFFFFFFFF; 98d3d2176aSDavid Gibson 99*f9df74dfSBryant G. Ly pdn = pci_get_pdn_by_devfn(bus, devfn); 100d0914f50SGavin Shan 101*f9df74dfSBryant G. Ly /* Validity of pdn is checked in here */ 102d0914f50SGavin Shan ret = rtas_read_config(pdn, where, size, val); 103d0914f50SGavin Shan if (*val == EEH_IO_ERROR_VALUE(size) && 104c6406d8fSGavin Shan eeh_dev_check_failure(pdn_to_eeh_dev(pdn))) 105d0914f50SGavin Shan return PCIBIOS_DEVICE_NOT_FOUND; 106d0914f50SGavin Shan 107d0914f50SGavin Shan return ret; 108d3d2176aSDavid Gibson } 109d3d2176aSDavid Gibson 110d3d2176aSDavid Gibson int rtas_write_config(struct pci_dn *pdn, int where, int size, u32 val) 111d3d2176aSDavid Gibson { 112d3d2176aSDavid Gibson unsigned long buid, addr; 113d3d2176aSDavid Gibson int ret; 114d3d2176aSDavid Gibson 115d3d2176aSDavid Gibson if (!pdn) 116d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 117d3d2176aSDavid Gibson if (!config_access_valid(pdn, where)) 118d3d2176aSDavid Gibson return PCIBIOS_BAD_REGISTER_NUMBER; 1193409eb4eSGavin Shan #ifdef CONFIG_EEH 1203409eb4eSGavin Shan if (pdn->edev && pdn->edev->pe && 1213409eb4eSGavin Shan (pdn->edev->pe->state & EEH_PE_CFG_BLOCKED)) 1223409eb4eSGavin Shan return PCIBIOS_SET_FAILED; 1233409eb4eSGavin Shan #endif 124d3d2176aSDavid Gibson 1256f3d5d3cSMichael Ellerman addr = rtas_config_addr(pdn->busno, pdn->devfn, where); 126d3d2176aSDavid Gibson buid = pdn->phb->buid; 127d3d2176aSDavid Gibson if (buid) { 128d3d2176aSDavid Gibson ret = rtas_call(ibm_write_pci_config, 5, 1, NULL, addr, 129d3d2176aSDavid Gibson BUID_HI(buid), BUID_LO(buid), size, (ulong) val); 130d3d2176aSDavid Gibson } else { 131d3d2176aSDavid Gibson ret = rtas_call(write_pci_config, 3, 1, NULL, addr, size, (ulong)val); 132d3d2176aSDavid Gibson } 133d3d2176aSDavid Gibson 134d3d2176aSDavid Gibson if (ret) 135d3d2176aSDavid Gibson return PCIBIOS_DEVICE_NOT_FOUND; 136d3d2176aSDavid Gibson 137d3d2176aSDavid Gibson return PCIBIOS_SUCCESSFUL; 138d3d2176aSDavid Gibson } 139d3d2176aSDavid Gibson 140d3d2176aSDavid Gibson static int rtas_pci_write_config(struct pci_bus *bus, 141d3d2176aSDavid Gibson unsigned int devfn, 142d3d2176aSDavid Gibson int where, int size, u32 val) 143d3d2176aSDavid Gibson { 144d0914f50SGavin Shan struct pci_dn *pdn; 145d3d2176aSDavid Gibson 146*f9df74dfSBryant G. Ly pdn = pci_get_pdn_by_devfn(bus, devfn); 147d0914f50SGavin Shan 148*f9df74dfSBryant G. Ly /* Validity of pdn is checked in here. */ 1493409eb4eSGavin Shan return rtas_write_config(pdn, where, size, val); 150d3d2176aSDavid Gibson } 151d3d2176aSDavid Gibson 1521c21a293SMichael Ellerman static struct pci_ops rtas_pci_ops = { 1538674e0c9SNathan Lynch .read = rtas_pci_read_config, 1548674e0c9SNathan Lynch .write = rtas_pci_write_config, 155d3d2176aSDavid Gibson }; 156d3d2176aSDavid Gibson 1571c21a293SMichael Ellerman static int is_python(struct device_node *dev) 158d3d2176aSDavid Gibson { 159e2eb6392SStephen Rothwell const char *model = of_get_property(dev, "model", NULL); 160d3d2176aSDavid Gibson 161d3d2176aSDavid Gibson if (model && strstr(model, "Python")) 162d3d2176aSDavid Gibson return 1; 163d3d2176aSDavid Gibson 164d3d2176aSDavid Gibson return 0; 165d3d2176aSDavid Gibson } 166d3d2176aSDavid Gibson 167cc5d0189SBenjamin Herrenschmidt static void python_countermeasures(struct device_node *dev) 168d3d2176aSDavid Gibson { 169cc5d0189SBenjamin Herrenschmidt struct resource registers; 170d3d2176aSDavid Gibson void __iomem *chip_regs; 171d3d2176aSDavid Gibson volatile u32 val; 172d3d2176aSDavid Gibson 173cc5d0189SBenjamin Herrenschmidt if (of_address_to_resource(dev, 0, ®isters)) { 174cc5d0189SBenjamin Herrenschmidt printk(KERN_ERR "Can't get address for Python workarounds !\n"); 175d3d2176aSDavid Gibson return; 176cc5d0189SBenjamin Herrenschmidt } 177d3d2176aSDavid Gibson 178d3d2176aSDavid Gibson /* Python's register file is 1 MB in size. */ 179cc5d0189SBenjamin Herrenschmidt chip_regs = ioremap(registers.start & ~(0xfffffUL), 0x100000); 180d3d2176aSDavid Gibson 181d3d2176aSDavid Gibson /* 182d3d2176aSDavid Gibson * Firmware doesn't always clear this bit which is critical 183d3d2176aSDavid Gibson * for good performance - Anton 184d3d2176aSDavid Gibson */ 185d3d2176aSDavid Gibson 186d3d2176aSDavid Gibson #define PRG_CL_RESET_VALID 0x00010000 187d3d2176aSDavid Gibson 188d3d2176aSDavid Gibson val = in_be32(chip_regs + 0xf6030); 189d3d2176aSDavid Gibson if (val & PRG_CL_RESET_VALID) { 190d3d2176aSDavid Gibson printk(KERN_INFO "Python workaround: "); 191d3d2176aSDavid Gibson val &= ~PRG_CL_RESET_VALID; 192d3d2176aSDavid Gibson out_be32(chip_regs + 0xf6030, val); 193d3d2176aSDavid Gibson /* 194d3d2176aSDavid Gibson * We must read it back for changes to 195d3d2176aSDavid Gibson * take effect 196d3d2176aSDavid Gibson */ 197d3d2176aSDavid Gibson val = in_be32(chip_regs + 0xf6030); 198d3d2176aSDavid Gibson printk("reg0: %x\n", val); 199d3d2176aSDavid Gibson } 200d3d2176aSDavid Gibson 201d3d2176aSDavid Gibson iounmap(chip_regs); 202d3d2176aSDavid Gibson } 203d3d2176aSDavid Gibson 204d3d2176aSDavid Gibson void __init init_pci_config_tokens(void) 205d3d2176aSDavid Gibson { 206d3d2176aSDavid Gibson read_pci_config = rtas_token("read-pci-config"); 207d3d2176aSDavid Gibson write_pci_config = rtas_token("write-pci-config"); 208d3d2176aSDavid Gibson ibm_read_pci_config = rtas_token("ibm,read-pci-config"); 209d3d2176aSDavid Gibson ibm_write_pci_config = rtas_token("ibm,write-pci-config"); 210d3d2176aSDavid Gibson } 211d3d2176aSDavid Gibson 212cad5cef6SGreg Kroah-Hartman unsigned long get_phb_buid(struct device_node *phb) 213d3d2176aSDavid Gibson { 2146506e710SBenjamin Herrenschmidt struct resource r; 215d3d2176aSDavid Gibson 2166506e710SBenjamin Herrenschmidt if (ibm_read_pci_config == -1) 217d3d2176aSDavid Gibson return 0; 2186506e710SBenjamin Herrenschmidt if (of_address_to_resource(phb, 0, &r)) 219d3d2176aSDavid Gibson return 0; 2206506e710SBenjamin Herrenschmidt return r.start; 221d3d2176aSDavid Gibson } 222d3d2176aSDavid Gibson 223d3d2176aSDavid Gibson static int phb_set_bus_ranges(struct device_node *dev, 224d3d2176aSDavid Gibson struct pci_controller *phb) 225d3d2176aSDavid Gibson { 226cf059965SCedric Le Goater const __be32 *bus_range; 227d3d2176aSDavid Gibson unsigned int len; 228d3d2176aSDavid Gibson 229e2eb6392SStephen Rothwell bus_range = of_get_property(dev, "bus-range", &len); 230d3d2176aSDavid Gibson if (bus_range == NULL || len < 2 * sizeof(int)) { 231d3d2176aSDavid Gibson return 1; 232d3d2176aSDavid Gibson } 233d3d2176aSDavid Gibson 234cf059965SCedric Le Goater phb->first_busno = be32_to_cpu(bus_range[0]); 235cf059965SCedric Le Goater phb->last_busno = be32_to_cpu(bus_range[1]); 236d3d2176aSDavid Gibson 237d3d2176aSDavid Gibson return 0; 238d3d2176aSDavid Gibson } 239d3d2176aSDavid Gibson 240cad5cef6SGreg Kroah-Hartman int rtas_setup_phb(struct pci_controller *phb) 241d3d2176aSDavid Gibson { 24244ef3390SStephen Rothwell struct device_node *dev = phb->dn; 2434c9d2800SBenjamin Herrenschmidt 244d3d2176aSDavid Gibson if (is_python(dev)) 245cc5d0189SBenjamin Herrenschmidt python_countermeasures(dev); 246d3d2176aSDavid Gibson 247d3d2176aSDavid Gibson if (phb_set_bus_ranges(dev, phb)) 248d3d2176aSDavid Gibson return 1; 249d3d2176aSDavid Gibson 250d3d2176aSDavid Gibson phb->ops = &rtas_pci_ops; 251d3d2176aSDavid Gibson phb->buid = get_phb_buid(dev); 252d3d2176aSDavid Gibson 253d3d2176aSDavid Gibson return 0; 254d3d2176aSDavid Gibson } 255