11e76875eSOlof Johansson /* 21e76875eSOlof Johansson * Copyright (C) 2006 PA Semi, Inc 31e76875eSOlof Johansson * 41e76875eSOlof Johansson * Authors: Kip Walker, PA Semi 51e76875eSOlof Johansson * Olof Johansson, PA Semi 61e76875eSOlof Johansson * 71e76875eSOlof Johansson * Maintained by: Olof Johansson <olof@lixom.net> 81e76875eSOlof Johansson * 91e76875eSOlof Johansson * Based on arch/powerpc/platforms/maple/pci.c 101e76875eSOlof Johansson * 111e76875eSOlof Johansson * This program is free software; you can redistribute it and/or modify 121e76875eSOlof Johansson * it under the terms of the GNU General Public License version 2 as 131e76875eSOlof Johansson * published by the Free Software Foundation. 141e76875eSOlof Johansson * 151e76875eSOlof Johansson * This program is distributed in the hope that it will be useful, 161e76875eSOlof Johansson * but WITHOUT ANY WARRANTY; without even the implied warranty of 171e76875eSOlof Johansson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 181e76875eSOlof Johansson * GNU General Public License for more details. 191e76875eSOlof Johansson * 201e76875eSOlof Johansson * You should have received a copy of the GNU General Public License 211e76875eSOlof Johansson * along with this program; if not, write to the Free Software 221e76875eSOlof Johansson * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 231e76875eSOlof Johansson */ 241e76875eSOlof Johansson 251e76875eSOlof Johansson 261e76875eSOlof Johansson #include <linux/kernel.h> 271e76875eSOlof Johansson #include <linux/pci.h> 281e76875eSOlof Johansson 291e76875eSOlof Johansson #include <asm/pci-bridge.h> 301e76875eSOlof Johansson #include <asm/machdep.h> 311e76875eSOlof Johansson 321e76875eSOlof Johansson #include <asm/ppc-pci.h> 331e76875eSOlof Johansson 341e76875eSOlof Johansson #define PA_PXP_CFA(bus, devfn, off) (((bus) << 20) | ((devfn) << 12) | (off)) 351e76875eSOlof Johansson 36df7e70a2SOlof Johansson static inline int pa_pxp_offset_valid(u8 bus, u8 devfn, int offset) 37df7e70a2SOlof Johansson { 38df7e70a2SOlof Johansson /* Device 0 Function 0 is special: It's config space spans function 1 as 39df7e70a2SOlof Johansson * well, so allow larger offset. It's really a two-function device but the 40df7e70a2SOlof Johansson * second function does not probe. 41df7e70a2SOlof Johansson */ 42df7e70a2SOlof Johansson if (bus == 0 && devfn == 0) 43df7e70a2SOlof Johansson return offset < 8192; 44df7e70a2SOlof Johansson else 45df7e70a2SOlof Johansson return offset < 4096; 46df7e70a2SOlof Johansson } 471e76875eSOlof Johansson 487c84ace9SAl Viro static void volatile __iomem *pa_pxp_cfg_addr(struct pci_controller *hose, 491e76875eSOlof Johansson u8 bus, u8 devfn, int offset) 501e76875eSOlof Johansson { 517c84ace9SAl Viro return hose->cfg_data + PA_PXP_CFA(bus, devfn, offset); 521e76875eSOlof Johansson } 531e76875eSOlof Johansson 541e76875eSOlof Johansson static int pa_pxp_read_config(struct pci_bus *bus, unsigned int devfn, 551e76875eSOlof Johansson int offset, int len, u32 *val) 561e76875eSOlof Johansson { 571e76875eSOlof Johansson struct pci_controller *hose; 587c84ace9SAl Viro void volatile __iomem *addr; 591e76875eSOlof Johansson 601e76875eSOlof Johansson hose = pci_bus_to_host(bus); 611e76875eSOlof Johansson if (!hose) 621e76875eSOlof Johansson return PCIBIOS_DEVICE_NOT_FOUND; 631e76875eSOlof Johansson 64df7e70a2SOlof Johansson if (!pa_pxp_offset_valid(bus->number, devfn, offset)) 651e76875eSOlof Johansson return PCIBIOS_BAD_REGISTER_NUMBER; 661e76875eSOlof Johansson 671e76875eSOlof Johansson addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset); 681e76875eSOlof Johansson 691e76875eSOlof Johansson /* 701e76875eSOlof Johansson * Note: the caller has already checked that offset is 711e76875eSOlof Johansson * suitably aligned and that len is 1, 2 or 4. 721e76875eSOlof Johansson */ 731e76875eSOlof Johansson switch (len) { 741e76875eSOlof Johansson case 1: 757c84ace9SAl Viro *val = in_8(addr); 761e76875eSOlof Johansson break; 771e76875eSOlof Johansson case 2: 787c84ace9SAl Viro *val = in_le16(addr); 791e76875eSOlof Johansson break; 801e76875eSOlof Johansson default: 817c84ace9SAl Viro *val = in_le32(addr); 821e76875eSOlof Johansson break; 831e76875eSOlof Johansson } 841e76875eSOlof Johansson 851e76875eSOlof Johansson return PCIBIOS_SUCCESSFUL; 861e76875eSOlof Johansson } 871e76875eSOlof Johansson 881e76875eSOlof Johansson static int pa_pxp_write_config(struct pci_bus *bus, unsigned int devfn, 891e76875eSOlof Johansson int offset, int len, u32 val) 901e76875eSOlof Johansson { 911e76875eSOlof Johansson struct pci_controller *hose; 927c84ace9SAl Viro void volatile __iomem *addr; 931e76875eSOlof Johansson 941e76875eSOlof Johansson hose = pci_bus_to_host(bus); 951e76875eSOlof Johansson if (!hose) 961e76875eSOlof Johansson return PCIBIOS_DEVICE_NOT_FOUND; 971e76875eSOlof Johansson 98df7e70a2SOlof Johansson if (!pa_pxp_offset_valid(bus->number, devfn, offset)) 991e76875eSOlof Johansson return PCIBIOS_BAD_REGISTER_NUMBER; 1001e76875eSOlof Johansson 1011e76875eSOlof Johansson addr = pa_pxp_cfg_addr(hose, bus->number, devfn, offset); 1021e76875eSOlof Johansson 1031e76875eSOlof Johansson /* 1041e76875eSOlof Johansson * Note: the caller has already checked that offset is 1051e76875eSOlof Johansson * suitably aligned and that len is 1, 2 or 4. 1061e76875eSOlof Johansson */ 1071e76875eSOlof Johansson switch (len) { 1081e76875eSOlof Johansson case 1: 1097c84ace9SAl Viro out_8(addr, val); 1101e76875eSOlof Johansson break; 1111e76875eSOlof Johansson case 2: 1127c84ace9SAl Viro out_le16(addr, val); 1131e76875eSOlof Johansson break; 1141e76875eSOlof Johansson default: 1157c84ace9SAl Viro out_le32(addr, val); 1161e76875eSOlof Johansson break; 1171e76875eSOlof Johansson } 1181e76875eSOlof Johansson return PCIBIOS_SUCCESSFUL; 1191e76875eSOlof Johansson } 1201e76875eSOlof Johansson 1211e76875eSOlof Johansson static struct pci_ops pa_pxp_ops = { 1221bb8c621SNathan Lynch .read = pa_pxp_read_config, 1231bb8c621SNathan Lynch .write = pa_pxp_write_config, 1241e76875eSOlof Johansson }; 1251e76875eSOlof Johansson 1261e76875eSOlof Johansson static void __init setup_pa_pxp(struct pci_controller *hose) 1271e76875eSOlof Johansson { 1281e76875eSOlof Johansson hose->ops = &pa_pxp_ops; 1291e76875eSOlof Johansson hose->cfg_data = ioremap(0xe0000000, 0x10000000); 1301e76875eSOlof Johansson } 1311e76875eSOlof Johansson 13209b55f76SArnd Bergmann static int __init pas_add_bridge(struct device_node *dev) 1331e76875eSOlof Johansson { 1341e76875eSOlof Johansson struct pci_controller *hose; 1351e76875eSOlof Johansson 1361e76875eSOlof Johansson pr_debug("Adding PCI host bridge %s\n", dev->full_name); 1371e76875eSOlof Johansson 1381e76875eSOlof Johansson hose = pcibios_alloc_controller(dev); 1391e76875eSOlof Johansson if (!hose) 1401e76875eSOlof Johansson return -ENOMEM; 1411e76875eSOlof Johansson 1421e76875eSOlof Johansson hose->first_busno = 0; 1431e76875eSOlof Johansson hose->last_busno = 0xff; 1441e76875eSOlof Johansson 1451e76875eSOlof Johansson setup_pa_pxp(hose); 1461e76875eSOlof Johansson 1471e76875eSOlof Johansson printk(KERN_INFO "Found PA-PXP PCI host bridge.\n"); 1481e76875eSOlof Johansson 1491e76875eSOlof Johansson /* Interpret the "ranges" property */ 1501e76875eSOlof Johansson pci_process_bridge_OF_ranges(hose, dev, 1); 1511e76875eSOlof Johansson 1521e76875eSOlof Johansson return 0; 1531e76875eSOlof Johansson } 1541e76875eSOlof Johansson 1551e76875eSOlof Johansson void __init pas_pci_init(void) 1561e76875eSOlof Johansson { 1571e76875eSOlof Johansson struct device_node *np, *root; 1581e76875eSOlof Johansson 1591e76875eSOlof Johansson root = of_find_node_by_path("/"); 1601e76875eSOlof Johansson if (!root) { 1611e76875eSOlof Johansson printk(KERN_CRIT "pas_pci_init: can't find root " 1621e76875eSOlof Johansson "of device tree\n"); 1631e76875eSOlof Johansson return; 1641e76875eSOlof Johansson } 1651e76875eSOlof Johansson 1661e76875eSOlof Johansson for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) 16709b55f76SArnd Bergmann if (np->name && !strcmp(np->name, "pxp") && !pas_add_bridge(np)) 1681e76875eSOlof Johansson of_node_get(np); 1691e76875eSOlof Johansson 1701e76875eSOlof Johansson of_node_put(root); 1711e76875eSOlof Johansson 1721e76875eSOlof Johansson /* Setup the linkage between OF nodes and PHBs */ 1731e76875eSOlof Johansson pci_devs_phb_init(); 1741e76875eSOlof Johansson 1751e76875eSOlof Johansson /* Use the common resource allocation mechanism */ 1761e76875eSOlof Johansson pci_probe_only = 1; 1771e76875eSOlof Johansson } 178*68c8404cSOlof Johansson 179*68c8404cSOlof Johansson void __iomem *pasemi_pci_getcfgaddr(struct pci_dev *dev, int offset) 180*68c8404cSOlof Johansson { 181*68c8404cSOlof Johansson struct pci_controller *hose; 182*68c8404cSOlof Johansson 183*68c8404cSOlof Johansson hose = pci_bus_to_host(bus); 184*68c8404cSOlof Johansson 185*68c8404cSOlof Johansson return pa_pxp_cfg_addr(hose, hose->number, dev->devfn, int offset) 186*68c8404cSOlof Johansson } 187