Path: blob/master/arch/powerpc/platforms/maple/pci.c
10819 views
/*1* Copyright (C) 2004 Benjamin Herrenschmuidt ([email protected]),2* IBM Corp.3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License6* as published by the Free Software Foundation; either version7* 2 of the License, or (at your option) any later version.8*/910#undef DEBUG1112#include <linux/kernel.h>13#include <linux/pci.h>14#include <linux/delay.h>15#include <linux/string.h>16#include <linux/init.h>17#include <linux/bootmem.h>18#include <linux/irq.h>1920#include <asm/sections.h>21#include <asm/io.h>22#include <asm/prom.h>23#include <asm/pci-bridge.h>24#include <asm/machdep.h>25#include <asm/iommu.h>26#include <asm/ppc-pci.h>2728#include "maple.h"2930#ifdef DEBUG31#define DBG(x...) printk(x)32#else33#define DBG(x...)34#endif3536static struct pci_controller *u3_agp, *u3_ht, *u4_pcie;3738static int __init fixup_one_level_bus_range(struct device_node *node, int higher)39{40for (; node != 0;node = node->sibling) {41const int *bus_range;42const unsigned int *class_code;43int len;4445/* For PCI<->PCI bridges or CardBus bridges, we go down */46class_code = of_get_property(node, "class-code", NULL);47if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&48(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))49continue;50bus_range = of_get_property(node, "bus-range", &len);51if (bus_range != NULL && len > 2 * sizeof(int)) {52if (bus_range[1] > higher)53higher = bus_range[1];54}55higher = fixup_one_level_bus_range(node->child, higher);56}57return higher;58}5960/* This routine fixes the "bus-range" property of all bridges in the61* system since they tend to have their "last" member wrong on macs62*63* Note that the bus numbers manipulated here are OF bus numbers, they64* are not Linux bus numbers.65*/66static void __init fixup_bus_range(struct device_node *bridge)67{68int *bus_range;69struct property *prop;70int len;7172/* Lookup the "bus-range" property for the hose */73prop = of_find_property(bridge, "bus-range", &len);74if (prop == NULL || prop->value == NULL || len < 2 * sizeof(int)) {75printk(KERN_WARNING "Can't get bus-range for %s\n",76bridge->full_name);77return;78}79bus_range = prop->value;80bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);81}828384static unsigned long u3_agp_cfa0(u8 devfn, u8 off)85{86return (1 << (unsigned long)PCI_SLOT(devfn)) |87((unsigned long)PCI_FUNC(devfn) << 8) |88((unsigned long)off & 0xFCUL);89}9091static unsigned long u3_agp_cfa1(u8 bus, u8 devfn, u8 off)92{93return ((unsigned long)bus << 16) |94((unsigned long)devfn << 8) |95((unsigned long)off & 0xFCUL) |961UL;97}9899static volatile void __iomem *u3_agp_cfg_access(struct pci_controller* hose,100u8 bus, u8 dev_fn, u8 offset)101{102unsigned int caddr;103104if (bus == hose->first_busno) {105if (dev_fn < (11 << 3))106return NULL;107caddr = u3_agp_cfa0(dev_fn, offset);108} else109caddr = u3_agp_cfa1(bus, dev_fn, offset);110111/* Uninorth will return garbage if we don't read back the value ! */112do {113out_le32(hose->cfg_addr, caddr);114} while (in_le32(hose->cfg_addr) != caddr);115116offset &= 0x07;117return hose->cfg_data + offset;118}119120static int u3_agp_read_config(struct pci_bus *bus, unsigned int devfn,121int offset, int len, u32 *val)122{123struct pci_controller *hose;124volatile void __iomem *addr;125126hose = pci_bus_to_host(bus);127if (hose == NULL)128return PCIBIOS_DEVICE_NOT_FOUND;129130addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);131if (!addr)132return PCIBIOS_DEVICE_NOT_FOUND;133/*134* Note: the caller has already checked that offset is135* suitably aligned and that len is 1, 2 or 4.136*/137switch (len) {138case 1:139*val = in_8(addr);140break;141case 2:142*val = in_le16(addr);143break;144default:145*val = in_le32(addr);146break;147}148return PCIBIOS_SUCCESSFUL;149}150151static int u3_agp_write_config(struct pci_bus *bus, unsigned int devfn,152int offset, int len, u32 val)153{154struct pci_controller *hose;155volatile void __iomem *addr;156157hose = pci_bus_to_host(bus);158if (hose == NULL)159return PCIBIOS_DEVICE_NOT_FOUND;160161addr = u3_agp_cfg_access(hose, bus->number, devfn, offset);162if (!addr)163return PCIBIOS_DEVICE_NOT_FOUND;164/*165* Note: the caller has already checked that offset is166* suitably aligned and that len is 1, 2 or 4.167*/168switch (len) {169case 1:170out_8(addr, val);171break;172case 2:173out_le16(addr, val);174break;175default:176out_le32(addr, val);177break;178}179return PCIBIOS_SUCCESSFUL;180}181182static struct pci_ops u3_agp_pci_ops =183{184.read = u3_agp_read_config,185.write = u3_agp_write_config,186};187188static unsigned long u3_ht_cfa0(u8 devfn, u8 off)189{190return (devfn << 8) | off;191}192193static unsigned long u3_ht_cfa1(u8 bus, u8 devfn, u8 off)194{195return u3_ht_cfa0(devfn, off) + (bus << 16) + 0x01000000UL;196}197198static volatile void __iomem *u3_ht_cfg_access(struct pci_controller* hose,199u8 bus, u8 devfn, u8 offset)200{201if (bus == hose->first_busno) {202if (PCI_SLOT(devfn) == 0)203return NULL;204return hose->cfg_data + u3_ht_cfa0(devfn, offset);205} else206return hose->cfg_data + u3_ht_cfa1(bus, devfn, offset);207}208209static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,210int offset, int len, u32 *val)211{212struct pci_controller *hose;213volatile void __iomem *addr;214215hose = pci_bus_to_host(bus);216if (hose == NULL)217return PCIBIOS_DEVICE_NOT_FOUND;218219if (offset > 0xff)220return PCIBIOS_BAD_REGISTER_NUMBER;221222addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);223if (!addr)224return PCIBIOS_DEVICE_NOT_FOUND;225226/*227* Note: the caller has already checked that offset is228* suitably aligned and that len is 1, 2 or 4.229*/230switch (len) {231case 1:232*val = in_8(addr);233break;234case 2:235*val = in_le16(addr);236break;237default:238*val = in_le32(addr);239break;240}241return PCIBIOS_SUCCESSFUL;242}243244static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,245int offset, int len, u32 val)246{247struct pci_controller *hose;248volatile void __iomem *addr;249250hose = pci_bus_to_host(bus);251if (hose == NULL)252return PCIBIOS_DEVICE_NOT_FOUND;253254if (offset > 0xff)255return PCIBIOS_BAD_REGISTER_NUMBER;256257addr = u3_ht_cfg_access(hose, bus->number, devfn, offset);258if (!addr)259return PCIBIOS_DEVICE_NOT_FOUND;260/*261* Note: the caller has already checked that offset is262* suitably aligned and that len is 1, 2 or 4.263*/264switch (len) {265case 1:266out_8(addr, val);267break;268case 2:269out_le16(addr, val);270break;271default:272out_le32(addr, val);273break;274}275return PCIBIOS_SUCCESSFUL;276}277278static struct pci_ops u3_ht_pci_ops =279{280.read = u3_ht_read_config,281.write = u3_ht_write_config,282};283284static unsigned int u4_pcie_cfa0(unsigned int devfn, unsigned int off)285{286return (1 << PCI_SLOT(devfn)) |287(PCI_FUNC(devfn) << 8) |288((off >> 8) << 28) |289(off & 0xfcu);290}291292static unsigned int u4_pcie_cfa1(unsigned int bus, unsigned int devfn,293unsigned int off)294{295return (bus << 16) |296(devfn << 8) |297((off >> 8) << 28) |298(off & 0xfcu) | 1u;299}300301static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,302u8 bus, u8 dev_fn, int offset)303{304unsigned int caddr;305306if (bus == hose->first_busno)307caddr = u4_pcie_cfa0(dev_fn, offset);308else309caddr = u4_pcie_cfa1(bus, dev_fn, offset);310311/* Uninorth will return garbage if we don't read back the value ! */312do {313out_le32(hose->cfg_addr, caddr);314} while (in_le32(hose->cfg_addr) != caddr);315316offset &= 0x03;317return hose->cfg_data + offset;318}319320static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,321int offset, int len, u32 *val)322{323struct pci_controller *hose;324volatile void __iomem *addr;325326hose = pci_bus_to_host(bus);327if (hose == NULL)328return PCIBIOS_DEVICE_NOT_FOUND;329if (offset >= 0x1000)330return PCIBIOS_BAD_REGISTER_NUMBER;331addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);332if (!addr)333return PCIBIOS_DEVICE_NOT_FOUND;334/*335* Note: the caller has already checked that offset is336* suitably aligned and that len is 1, 2 or 4.337*/338switch (len) {339case 1:340*val = in_8(addr);341break;342case 2:343*val = in_le16(addr);344break;345default:346*val = in_le32(addr);347break;348}349return PCIBIOS_SUCCESSFUL;350}351static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,352int offset, int len, u32 val)353{354struct pci_controller *hose;355volatile void __iomem *addr;356357hose = pci_bus_to_host(bus);358if (hose == NULL)359return PCIBIOS_DEVICE_NOT_FOUND;360if (offset >= 0x1000)361return PCIBIOS_BAD_REGISTER_NUMBER;362addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);363if (!addr)364return PCIBIOS_DEVICE_NOT_FOUND;365/*366* Note: the caller has already checked that offset is367* suitably aligned and that len is 1, 2 or 4.368*/369switch (len) {370case 1:371out_8(addr, val);372break;373case 2:374out_le16(addr, val);375break;376default:377out_le32(addr, val);378break;379}380return PCIBIOS_SUCCESSFUL;381}382383static struct pci_ops u4_pcie_pci_ops =384{385.read = u4_pcie_read_config,386.write = u4_pcie_write_config,387};388389static void __init setup_u3_agp(struct pci_controller* hose)390{391/* On G5, we move AGP up to high bus number so we don't need392* to reassign bus numbers for HT. If we ever have P2P bridges393* on AGP, we'll have to move pci_assign_all_buses to the394* pci_controller structure so we enable it for AGP and not for395* HT childs.396* We hard code the address because of the different size of397* the reg address cell, we shall fix that by killing struct398* reg_property and using some accessor functions instead399*/400hose->first_busno = 0xf0;401hose->last_busno = 0xff;402hose->ops = &u3_agp_pci_ops;403hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);404hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);405406u3_agp = hose;407}408409static void __init setup_u4_pcie(struct pci_controller* hose)410{411/* We currently only implement the "non-atomic" config space, to412* be optimised later.413*/414hose->ops = &u4_pcie_pci_ops;415hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);416hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);417418u4_pcie = hose;419}420421static void __init setup_u3_ht(struct pci_controller* hose)422{423hose->ops = &u3_ht_pci_ops;424425/* We hard code the address because of the different size of426* the reg address cell, we shall fix that by killing struct427* reg_property and using some accessor functions instead428*/429hose->cfg_data = ioremap(0xf2000000, 0x02000000);430431hose->first_busno = 0;432hose->last_busno = 0xef;433434u3_ht = hose;435}436437static int __init maple_add_bridge(struct device_node *dev)438{439int len;440struct pci_controller *hose;441char* disp_name;442const int *bus_range;443int primary = 1;444445DBG("Adding PCI host bridge %s\n", dev->full_name);446447bus_range = of_get_property(dev, "bus-range", &len);448if (bus_range == NULL || len < 2 * sizeof(int)) {449printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n",450dev->full_name);451}452453hose = pcibios_alloc_controller(dev);454if (hose == NULL)455return -ENOMEM;456hose->first_busno = bus_range ? bus_range[0] : 0;457hose->last_busno = bus_range ? bus_range[1] : 0xff;458459disp_name = NULL;460if (of_device_is_compatible(dev, "u3-agp")) {461setup_u3_agp(hose);462disp_name = "U3-AGP";463primary = 0;464} else if (of_device_is_compatible(dev, "u3-ht")) {465setup_u3_ht(hose);466disp_name = "U3-HT";467primary = 1;468} else if (of_device_is_compatible(dev, "u4-pcie")) {469setup_u4_pcie(hose);470disp_name = "U4-PCIE";471primary = 0;472}473printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n",474disp_name, hose->first_busno, hose->last_busno);475476/* Interpret the "ranges" property */477/* This also maps the I/O region and sets isa_io/mem_base */478pci_process_bridge_OF_ranges(hose, dev, primary);479480/* Fixup "bus-range" OF property */481fixup_bus_range(dev);482483/* Check for legacy IOs */484isa_bridge_find_early(hose);485486return 0;487}488489490void __devinit maple_pci_irq_fixup(struct pci_dev *dev)491{492DBG(" -> maple_pci_irq_fixup\n");493494/* Fixup IRQ for PCIe host */495if (u4_pcie != NULL && dev->bus->number == 0 &&496pci_bus_to_host(dev->bus) == u4_pcie) {497printk(KERN_DEBUG "Fixup U4 PCIe IRQ\n");498dev->irq = irq_create_mapping(NULL, 1);499if (dev->irq != NO_IRQ)500irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);501}502503/* Hide AMD8111 IDE interrupt when in legacy mode so504* the driver calls pci_get_legacy_ide_irq()505*/506if (dev->vendor == PCI_VENDOR_ID_AMD &&507dev->device == PCI_DEVICE_ID_AMD_8111_IDE &&508(dev->class & 5) != 5) {509dev->irq = NO_IRQ;510}511512DBG(" <- maple_pci_irq_fixup\n");513}514515void __init maple_pci_init(void)516{517struct device_node *np, *root;518struct device_node *ht = NULL;519520/* Probe root PCI hosts, that is on U3 the AGP host and the521* HyperTransport host. That one is actually "kept" around522* and actually added last as it's resource management relies523* on the AGP resources to have been setup first524*/525root = of_find_node_by_path("/");526if (root == NULL) {527printk(KERN_CRIT "maple_find_bridges: can't find root of device tree\n");528return;529}530for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {531if (!np->type)532continue;533if (strcmp(np->type, "pci") && strcmp(np->type, "ht"))534continue;535if ((of_device_is_compatible(np, "u4-pcie") ||536of_device_is_compatible(np, "u3-agp")) &&537maple_add_bridge(np) == 0)538of_node_get(np);539540if (of_device_is_compatible(np, "u3-ht")) {541of_node_get(np);542ht = np;543}544}545of_node_put(root);546547/* Now setup the HyperTransport host if we found any548*/549if (ht && maple_add_bridge(ht) != 0)550of_node_put(ht);551552/* Setup the linkage between OF nodes and PHBs */553pci_devs_phb_init();554555/* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We556* assume there is no P2P bridge on the AGP bus, which should be a557* safe assumptions hopefully.558*/559if (u3_agp) {560struct device_node *np = u3_agp->dn;561PCI_DN(np)->busno = 0xf0;562for (np = np->child; np; np = np->sibling)563PCI_DN(np)->busno = 0xf0;564}565566/* Tell pci.c to not change any resource allocations. */567pci_probe_only = 1;568}569570int maple_pci_get_legacy_ide_irq(struct pci_dev *pdev, int channel)571{572struct device_node *np;573unsigned int defirq = channel ? 15 : 14;574unsigned int irq;575576if (pdev->vendor != PCI_VENDOR_ID_AMD ||577pdev->device != PCI_DEVICE_ID_AMD_8111_IDE)578return defirq;579580np = pci_device_to_OF_node(pdev);581if (np == NULL) {582printk("Failed to locate OF node for IDE %s\n",583pci_name(pdev));584return defirq;585}586irq = irq_of_parse_and_map(np, channel & 0x1);587if (irq == NO_IRQ) {588printk("Failed to map onboard IDE interrupt for channel %d\n",589channel);590return defirq;591}592return irq;593}594595static void __devinit quirk_ipr_msi(struct pci_dev *dev)596{597/* Something prevents MSIs from the IPR from working on Bimini,598* and the driver has no smarts to recover. So disable MSI599* on it for now. */600601if (machine_is(maple)) {602dev->no_msi = 1;603dev_info(&dev->dev, "Quirk disabled MSI\n");604}605}606DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN,607quirk_ipr_msi);608609610