Path: blob/master/arch/powerpc/platforms/powermac/pci.c
10818 views
/*1* Support for PCI bridges found on Power Macintoshes.2*3* Copyright (C) 2003-2005 Benjamin Herrenschmuidt ([email protected])4* Copyright (C) 1997 Paul Mackerras ([email protected])5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public License8* as published by the Free Software Foundation; either version9* 2 of the License, or (at your option) any later version.10*/1112#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/pmac_feature.h>26#include <asm/grackle.h>27#include <asm/ppc-pci.h>2829#undef DEBUG3031#ifdef DEBUG32#define DBG(x...) printk(x)33#else34#define DBG(x...)35#endif3637/* XXX Could be per-controller, but I don't think we risk anything by38* assuming we won't have both UniNorth and Bandit */39static int has_uninorth;40#ifdef CONFIG_PPC6441static struct pci_controller *u3_agp;42#else43static int has_second_ohare;44#endif /* CONFIG_PPC64 */4546extern int pcibios_assign_bus_offset;4748struct device_node *k2_skiplist[2];4950/*51* Magic constants for enabling cache coherency in the bandit/PSX bridge.52*/53#define BANDIT_DEVID_2 854#define BANDIT_REVID 35556#define BANDIT_DEVNUM 1157#define BANDIT_MAGIC 0x5058#define BANDIT_COHERENT 0x405960static int __init fixup_one_level_bus_range(struct device_node *node, int higher)61{62for (; node != 0;node = node->sibling) {63const int * bus_range;64const unsigned int *class_code;65int len;6667/* For PCI<->PCI bridges or CardBus bridges, we go down */68class_code = of_get_property(node, "class-code", NULL);69if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&70(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))71continue;72bus_range = of_get_property(node, "bus-range", &len);73if (bus_range != NULL && len > 2 * sizeof(int)) {74if (bus_range[1] > higher)75higher = bus_range[1];76}77higher = fixup_one_level_bus_range(node->child, higher);78}79return higher;80}8182/* This routine fixes the "bus-range" property of all bridges in the83* system since they tend to have their "last" member wrong on macs84*85* Note that the bus numbers manipulated here are OF bus numbers, they86* are not Linux bus numbers.87*/88static void __init fixup_bus_range(struct device_node *bridge)89{90int *bus_range, len;91struct property *prop;9293/* Lookup the "bus-range" property for the hose */94prop = of_find_property(bridge, "bus-range", &len);95if (prop == NULL || prop->length < 2 * sizeof(int))96return;9798bus_range = prop->value;99bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);100}101102/*103* Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.104*105* The "Bandit" version is present in all early PCI PowerMacs,106* and up to the first ones using Grackle. Some machines may107* have 2 bandit controllers (2 PCI busses).108*109* "Chaos" is used in some "Bandit"-type machines as a bridge110* for the separate display bus. It is accessed the same111* way as bandit, but cannot be probed for devices. It therefore112* has its own config access functions.113*114* The "UniNorth" version is present in all Core99 machines115* (iBook, G4, new IMacs, and all the recent Apple machines).116* It contains 3 controllers in one ASIC.117*118* The U3 is the bridge used on G5 machines. It contains an119* AGP bus which is dealt with the old UniNorth access routines120* and a HyperTransport bus which uses its own set of access121* functions.122*/123124#define MACRISC_CFA0(devfn, off) \125((1 << (unsigned int)PCI_SLOT(dev_fn)) \126| (((unsigned int)PCI_FUNC(dev_fn)) << 8) \127| (((unsigned int)(off)) & 0xFCUL))128129#define MACRISC_CFA1(bus, devfn, off) \130((((unsigned int)(bus)) << 16) \131|(((unsigned int)(devfn)) << 8) \132|(((unsigned int)(off)) & 0xFCUL) \133|1UL)134135static volatile void __iomem *macrisc_cfg_access(struct pci_controller* hose,136u8 bus, u8 dev_fn, u8 offset)137{138unsigned int caddr;139140if (bus == hose->first_busno) {141if (dev_fn < (11 << 3))142return NULL;143caddr = MACRISC_CFA0(dev_fn, offset);144} else145caddr = MACRISC_CFA1(bus, dev_fn, offset);146147/* Uninorth will return garbage if we don't read back the value ! */148do {149out_le32(hose->cfg_addr, caddr);150} while (in_le32(hose->cfg_addr) != caddr);151152offset &= has_uninorth ? 0x07 : 0x03;153return hose->cfg_data + offset;154}155156static int macrisc_read_config(struct pci_bus *bus, unsigned int devfn,157int offset, int len, u32 *val)158{159struct pci_controller *hose;160volatile void __iomem *addr;161162hose = pci_bus_to_host(bus);163if (hose == NULL)164return PCIBIOS_DEVICE_NOT_FOUND;165if (offset >= 0x100)166return PCIBIOS_BAD_REGISTER_NUMBER;167addr = macrisc_cfg_access(hose, bus->number, devfn, offset);168if (!addr)169return PCIBIOS_DEVICE_NOT_FOUND;170/*171* Note: the caller has already checked that offset is172* suitably aligned and that len is 1, 2 or 4.173*/174switch (len) {175case 1:176*val = in_8(addr);177break;178case 2:179*val = in_le16(addr);180break;181default:182*val = in_le32(addr);183break;184}185return PCIBIOS_SUCCESSFUL;186}187188static int macrisc_write_config(struct pci_bus *bus, unsigned int devfn,189int offset, int len, u32 val)190{191struct pci_controller *hose;192volatile void __iomem *addr;193194hose = pci_bus_to_host(bus);195if (hose == NULL)196return PCIBIOS_DEVICE_NOT_FOUND;197if (offset >= 0x100)198return PCIBIOS_BAD_REGISTER_NUMBER;199addr = macrisc_cfg_access(hose, bus->number, devfn, offset);200if (!addr)201return PCIBIOS_DEVICE_NOT_FOUND;202/*203* Note: the caller has already checked that offset is204* suitably aligned and that len is 1, 2 or 4.205*/206switch (len) {207case 1:208out_8(addr, val);209break;210case 2:211out_le16(addr, val);212break;213default:214out_le32(addr, val);215break;216}217return PCIBIOS_SUCCESSFUL;218}219220static struct pci_ops macrisc_pci_ops =221{222.read = macrisc_read_config,223.write = macrisc_write_config,224};225226#ifdef CONFIG_PPC32227/*228* Verify that a specific (bus, dev_fn) exists on chaos229*/230static int chaos_validate_dev(struct pci_bus *bus, int devfn, int offset)231{232struct device_node *np;233const u32 *vendor, *device;234235if (offset >= 0x100)236return PCIBIOS_BAD_REGISTER_NUMBER;237np = pci_busdev_to_OF_node(bus, devfn);238if (np == NULL)239return PCIBIOS_DEVICE_NOT_FOUND;240241vendor = of_get_property(np, "vendor-id", NULL);242device = of_get_property(np, "device-id", NULL);243if (vendor == NULL || device == NULL)244return PCIBIOS_DEVICE_NOT_FOUND;245246if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)247&& (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))248return PCIBIOS_BAD_REGISTER_NUMBER;249250return PCIBIOS_SUCCESSFUL;251}252253static int254chaos_read_config(struct pci_bus *bus, unsigned int devfn, int offset,255int len, u32 *val)256{257int result = chaos_validate_dev(bus, devfn, offset);258if (result == PCIBIOS_BAD_REGISTER_NUMBER)259*val = ~0U;260if (result != PCIBIOS_SUCCESSFUL)261return result;262return macrisc_read_config(bus, devfn, offset, len, val);263}264265static int266chaos_write_config(struct pci_bus *bus, unsigned int devfn, int offset,267int len, u32 val)268{269int result = chaos_validate_dev(bus, devfn, offset);270if (result != PCIBIOS_SUCCESSFUL)271return result;272return macrisc_write_config(bus, devfn, offset, len, val);273}274275static struct pci_ops chaos_pci_ops =276{277.read = chaos_read_config,278.write = chaos_write_config,279};280281static void __init setup_chaos(struct pci_controller *hose,282struct resource *addr)283{284/* assume a `chaos' bridge */285hose->ops = &chaos_pci_ops;286hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);287hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);288}289#endif /* CONFIG_PPC32 */290291#ifdef CONFIG_PPC64292/*293* These versions of U3 HyperTransport config space access ops do not294* implement self-view of the HT host yet295*/296297/*298* This function deals with some "special cases" devices.299*300* 0 -> No special case301* 1 -> Skip the device but act as if the access was successful302* (return 0xff's on reads, eventually, cache config space303* accesses in a later version)304* -1 -> Hide the device (unsuccessful access)305*/306static int u3_ht_skip_device(struct pci_controller *hose,307struct pci_bus *bus, unsigned int devfn)308{309struct device_node *busdn, *dn;310int i;311312/* We only allow config cycles to devices that are in OF device-tree313* as we are apparently having some weird things going on with some314* revs of K2 on recent G5s, except for the host bridge itself, which315* is missing from the tree but we know we can probe.316*/317if (bus->self)318busdn = pci_device_to_OF_node(bus->self);319else if (devfn == 0)320return 0;321else322busdn = hose->dn;323for (dn = busdn->child; dn; dn = dn->sibling)324if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)325break;326if (dn == NULL)327return -1;328329/*330* When a device in K2 is powered down, we die on config331* cycle accesses. Fix that here.332*/333for (i=0; i<2; i++)334if (k2_skiplist[i] == dn)335return 1;336337return 0;338}339340#define U3_HT_CFA0(devfn, off) \341((((unsigned int)devfn) << 8) | offset)342#define U3_HT_CFA1(bus, devfn, off) \343(U3_HT_CFA0(devfn, off) \344+ (((unsigned int)bus) << 16) \345+ 0x01000000UL)346347static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,348u8 devfn, u8 offset, int *swap)349{350*swap = 1;351if (bus == hose->first_busno) {352if (devfn != 0)353return hose->cfg_data + U3_HT_CFA0(devfn, offset);354*swap = 0;355return ((void __iomem *)hose->cfg_addr) + (offset << 2);356} else357return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);358}359360static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,361int offset, int len, u32 *val)362{363struct pci_controller *hose;364void __iomem *addr;365int swap;366367hose = pci_bus_to_host(bus);368if (hose == NULL)369return PCIBIOS_DEVICE_NOT_FOUND;370if (offset >= 0x100)371return PCIBIOS_BAD_REGISTER_NUMBER;372addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);373if (!addr)374return PCIBIOS_DEVICE_NOT_FOUND;375376switch (u3_ht_skip_device(hose, bus, devfn)) {377case 0:378break;379case 1:380switch (len) {381case 1:382*val = 0xff; break;383case 2:384*val = 0xffff; break;385default:386*val = 0xfffffffful; break;387}388return PCIBIOS_SUCCESSFUL;389default:390return PCIBIOS_DEVICE_NOT_FOUND;391}392393/*394* Note: the caller has already checked that offset is395* suitably aligned and that len is 1, 2 or 4.396*/397switch (len) {398case 1:399*val = in_8(addr);400break;401case 2:402*val = swap ? in_le16(addr) : in_be16(addr);403break;404default:405*val = swap ? in_le32(addr) : in_be32(addr);406break;407}408return PCIBIOS_SUCCESSFUL;409}410411static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,412int offset, int len, u32 val)413{414struct pci_controller *hose;415void __iomem *addr;416int swap;417418hose = pci_bus_to_host(bus);419if (hose == NULL)420return PCIBIOS_DEVICE_NOT_FOUND;421if (offset >= 0x100)422return PCIBIOS_BAD_REGISTER_NUMBER;423addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);424if (!addr)425return PCIBIOS_DEVICE_NOT_FOUND;426427switch (u3_ht_skip_device(hose, bus, devfn)) {428case 0:429break;430case 1:431return PCIBIOS_SUCCESSFUL;432default:433return PCIBIOS_DEVICE_NOT_FOUND;434}435436/*437* Note: the caller has already checked that offset is438* suitably aligned and that len is 1, 2 or 4.439*/440switch (len) {441case 1:442out_8(addr, val);443break;444case 2:445swap ? out_le16(addr, val) : out_be16(addr, val);446break;447default:448swap ? out_le32(addr, val) : out_be32(addr, val);449break;450}451return PCIBIOS_SUCCESSFUL;452}453454static struct pci_ops u3_ht_pci_ops =455{456.read = u3_ht_read_config,457.write = u3_ht_write_config,458};459460#define U4_PCIE_CFA0(devfn, off) \461((1 << ((unsigned int)PCI_SLOT(dev_fn))) \462| (((unsigned int)PCI_FUNC(dev_fn)) << 8) \463| ((((unsigned int)(off)) >> 8) << 28) \464| (((unsigned int)(off)) & 0xfcU))465466#define U4_PCIE_CFA1(bus, devfn, off) \467((((unsigned int)(bus)) << 16) \468|(((unsigned int)(devfn)) << 8) \469| ((((unsigned int)(off)) >> 8) << 28) \470|(((unsigned int)(off)) & 0xfcU) \471|1UL)472473static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose,474u8 bus, u8 dev_fn, int offset)475{476unsigned int caddr;477478if (bus == hose->first_busno) {479caddr = U4_PCIE_CFA0(dev_fn, offset);480} else481caddr = U4_PCIE_CFA1(bus, dev_fn, offset);482483/* Uninorth will return garbage if we don't read back the value ! */484do {485out_le32(hose->cfg_addr, caddr);486} while (in_le32(hose->cfg_addr) != caddr);487488offset &= 0x03;489return hose->cfg_data + offset;490}491492static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn,493int offset, int len, u32 *val)494{495struct pci_controller *hose;496volatile void __iomem *addr;497498hose = pci_bus_to_host(bus);499if (hose == NULL)500return PCIBIOS_DEVICE_NOT_FOUND;501if (offset >= 0x1000)502return PCIBIOS_BAD_REGISTER_NUMBER;503addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);504if (!addr)505return PCIBIOS_DEVICE_NOT_FOUND;506/*507* Note: the caller has already checked that offset is508* suitably aligned and that len is 1, 2 or 4.509*/510switch (len) {511case 1:512*val = in_8(addr);513break;514case 2:515*val = in_le16(addr);516break;517default:518*val = in_le32(addr);519break;520}521return PCIBIOS_SUCCESSFUL;522}523524static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn,525int offset, int len, u32 val)526{527struct pci_controller *hose;528volatile void __iomem *addr;529530hose = pci_bus_to_host(bus);531if (hose == NULL)532return PCIBIOS_DEVICE_NOT_FOUND;533if (offset >= 0x1000)534return PCIBIOS_BAD_REGISTER_NUMBER;535addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset);536if (!addr)537return PCIBIOS_DEVICE_NOT_FOUND;538/*539* Note: the caller has already checked that offset is540* suitably aligned and that len is 1, 2 or 4.541*/542switch (len) {543case 1:544out_8(addr, val);545break;546case 2:547out_le16(addr, val);548break;549default:550out_le32(addr, val);551break;552}553return PCIBIOS_SUCCESSFUL;554}555556static struct pci_ops u4_pcie_pci_ops =557{558.read = u4_pcie_read_config,559.write = u4_pcie_write_config,560};561562#endif /* CONFIG_PPC64 */563564#ifdef CONFIG_PPC32565/*566* For a bandit bridge, turn on cache coherency if necessary.567* N.B. we could clean this up using the hose ops directly.568*/569static void __init init_bandit(struct pci_controller *bp)570{571unsigned int vendev, magic;572int rev;573574/* read the word at offset 0 in config space for device 11 */575out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);576udelay(2);577vendev = in_le32(bp->cfg_data);578if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +579PCI_VENDOR_ID_APPLE) {580/* read the revision id */581out_le32(bp->cfg_addr,582(1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);583udelay(2);584rev = in_8(bp->cfg_data);585if (rev != BANDIT_REVID)586printk(KERN_WARNING587"Unknown revision %d for bandit\n", rev);588} else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {589printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);590return;591}592593/* read the word at offset 0x50 */594out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);595udelay(2);596magic = in_le32(bp->cfg_data);597if ((magic & BANDIT_COHERENT) != 0)598return;599magic |= BANDIT_COHERENT;600udelay(2);601out_le32(bp->cfg_data, magic);602printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");603}604605/*606* Tweak the PCI-PCI bridge chip on the blue & white G3s.607*/608static void __init init_p2pbridge(void)609{610struct device_node *p2pbridge;611struct pci_controller* hose;612u8 bus, devfn;613u16 val;614615/* XXX it would be better here to identify the specific616PCI-PCI bridge chip we have. */617p2pbridge = of_find_node_by_name(NULL, "pci-bridge");618if (p2pbridge == NULL619|| p2pbridge->parent == NULL620|| strcmp(p2pbridge->parent->name, "pci") != 0)621goto done;622if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {623DBG("Can't find PCI infos for PCI<->PCI bridge\n");624goto done;625}626/* Warning: At this point, we have not yet renumbered all busses.627* So we must use OF walking to find out hose628*/629hose = pci_find_hose_for_OF_device(p2pbridge);630if (!hose) {631DBG("Can't find hose for PCI<->PCI bridge\n");632goto done;633}634if (early_read_config_word(hose, bus, devfn,635PCI_BRIDGE_CONTROL, &val) < 0) {636printk(KERN_ERR "init_p2pbridge: couldn't read bridge"637" control\n");638goto done;639}640val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;641early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);642done:643of_node_put(p2pbridge);644}645646static void __init init_second_ohare(void)647{648struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");649unsigned char bus, devfn;650unsigned short cmd;651652if (np == NULL)653return;654655/* This must run before we initialize the PICs since the second656* ohare hosts a PIC that will be accessed there.657*/658if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {659struct pci_controller* hose =660pci_find_hose_for_OF_device(np);661if (!hose) {662printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");663of_node_put(np);664return;665}666early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);667cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;668cmd &= ~PCI_COMMAND_IO;669early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);670}671has_second_ohare = 1;672of_node_put(np);673}674675/*676* Some Apple desktop machines have a NEC PD720100A USB2 controller677* on the motherboard. Open Firmware, on these, will disable the678* EHCI part of it so it behaves like a pair of OHCI's. This fixup679* code re-enables it ;)680*/681static void __init fixup_nec_usb2(void)682{683struct device_node *nec;684685for (nec = NULL; (nec = of_find_node_by_name(nec, "usb")) != NULL;) {686struct pci_controller *hose;687u32 data;688const u32 *prop;689u8 bus, devfn;690691prop = of_get_property(nec, "vendor-id", NULL);692if (prop == NULL)693continue;694if (0x1033 != *prop)695continue;696prop = of_get_property(nec, "device-id", NULL);697if (prop == NULL)698continue;699if (0x0035 != *prop)700continue;701prop = of_get_property(nec, "reg", NULL);702if (prop == NULL)703continue;704devfn = (prop[0] >> 8) & 0xff;705bus = (prop[0] >> 16) & 0xff;706if (PCI_FUNC(devfn) != 0)707continue;708hose = pci_find_hose_for_OF_device(nec);709if (!hose)710continue;711early_read_config_dword(hose, bus, devfn, 0xe4, &data);712if (data & 1UL) {713printk("Found NEC PD720100A USB2 chip with disabled"714" EHCI, fixing up...\n");715data &= ~1UL;716early_write_config_dword(hose, bus, devfn, 0xe4, data);717}718}719}720721static void __init setup_bandit(struct pci_controller *hose,722struct resource *addr)723{724hose->ops = ¯isc_pci_ops;725hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);726hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);727init_bandit(hose);728}729730static int __init setup_uninorth(struct pci_controller *hose,731struct resource *addr)732{733ppc_pci_add_flags(PPC_PCI_REASSIGN_ALL_BUS);734has_uninorth = 1;735hose->ops = ¯isc_pci_ops;736hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);737hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);738/* We "know" that the bridge at f2000000 has the PCI slots. */739return addr->start == 0xf2000000;740}741#endif /* CONFIG_PPC32 */742743#ifdef CONFIG_PPC64744static void __init setup_u3_agp(struct pci_controller* hose)745{746/* On G5, we move AGP up to high bus number so we don't need747* to reassign bus numbers for HT. If we ever have P2P bridges748* on AGP, we'll have to move pci_assign_all_busses to the749* pci_controller structure so we enable it for AGP and not for750* HT childs.751* We hard code the address because of the different size of752* the reg address cell, we shall fix that by killing struct753* reg_property and using some accessor functions instead754*/755hose->first_busno = 0xf0;756hose->last_busno = 0xff;757has_uninorth = 1;758hose->ops = ¯isc_pci_ops;759hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);760hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);761u3_agp = hose;762}763764static void __init setup_u4_pcie(struct pci_controller* hose)765{766/* We currently only implement the "non-atomic" config space, to767* be optimised later.768*/769hose->ops = &u4_pcie_pci_ops;770hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);771hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);772773/* The bus contains a bridge from root -> device, we need to774* make it visible on bus 0 so that we pick the right type775* of config cycles. If we didn't, we would have to force all776* config cycles to be type 1. So we override the "bus-range"777* property here778*/779hose->first_busno = 0x00;780hose->last_busno = 0xff;781}782783static void __init parse_region_decode(struct pci_controller *hose,784u32 decode)785{786unsigned long base, end, next = -1;787int i, cur = -1;788789/* Iterate through all bits. We ignore the last bit as this region is790* reserved for the ROM among other niceties791*/792for (i = 0; i < 31; i++) {793if ((decode & (0x80000000 >> i)) == 0)794continue;795if (i < 16) {796base = 0xf0000000 | (((u32)i) << 24);797end = base + 0x00ffffff;798} else {799base = ((u32)i-16) << 28;800end = base + 0x0fffffff;801}802if (base != next) {803if (++cur >= 3) {804printk(KERN_WARNING "PCI: Too many ranges !\n");805break;806}807hose->mem_resources[cur].flags = IORESOURCE_MEM;808hose->mem_resources[cur].name = hose->dn->full_name;809hose->mem_resources[cur].start = base;810hose->mem_resources[cur].end = end;811DBG(" %d: 0x%08lx-0x%08lx\n", cur, base, end);812} else {813DBG(" : -0x%08lx\n", end);814hose->mem_resources[cur].end = end;815}816next = end + 1;817}818}819820static void __init setup_u3_ht(struct pci_controller* hose)821{822struct device_node *np = hose->dn;823struct resource cfg_res, self_res;824u32 decode;825826hose->ops = &u3_ht_pci_ops;827828/* Get base addresses from OF tree829*/830if (of_address_to_resource(np, 0, &cfg_res) ||831of_address_to_resource(np, 1, &self_res)) {832printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");833return;834}835836/* Map external cfg space access into cfg_data and self registers837* into cfg_addr838*/839hose->cfg_data = ioremap(cfg_res.start, 0x02000000);840hose->cfg_addr = ioremap(self_res.start,841self_res.end - self_res.start + 1);842843/*844* /ht node doesn't expose a "ranges" property, we read the register845* that controls the decoding logic and use that for memory regions.846* The IO region is hard coded since it is fixed in HW as well.847*/848hose->io_base_phys = 0xf4000000;849hose->pci_io_size = 0x00400000;850hose->io_resource.name = np->full_name;851hose->io_resource.start = 0;852hose->io_resource.end = 0x003fffff;853hose->io_resource.flags = IORESOURCE_IO;854hose->pci_mem_offset = 0;855hose->first_busno = 0;856hose->last_busno = 0xef;857858/* Note: fix offset when cfg_addr becomes a void * */859decode = in_be32(hose->cfg_addr + 0x80);860861DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);862863/* NOTE: The decode register setup is a bit weird... region864* 0xf8000000 for example is marked as enabled in there while it's865& actually the memory controller registers.866* That means that we are incorrectly attributing it to HT.867*868* In a similar vein, region 0xf4000000 is actually the HT IO space but869* also marked as enabled in here and 0xf9000000 is used by some other870* internal bits of the northbridge.871*872* Unfortunately, we can't just mask out those bit as we would end873* up with more regions than we can cope (linux can only cope with874* 3 memory regions for a PHB at this stage).875*876* So for now, we just do a little hack. We happen to -know- that877* Apple firmware doesn't assign things below 0xfa000000 for that878* bridge anyway so we mask out all bits we don't want.879*/880decode &= 0x003fffff;881882/* Now parse the resulting bits and build resources */883parse_region_decode(hose, decode);884}885#endif /* CONFIG_PPC64 */886887/*888* We assume that if we have a G3 powermac, we have one bridge called889* "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,890* if we have one or more bandit or chaos bridges, we don't have a MPC106.891*/892static int __init pmac_add_bridge(struct device_node *dev)893{894int len;895struct pci_controller *hose;896struct resource rsrc;897char *disp_name;898const int *bus_range;899int primary = 1, has_address = 0;900901DBG("Adding PCI host bridge %s\n", dev->full_name);902903/* Fetch host bridge registers address */904has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);905906/* Get bus range if any */907bus_range = of_get_property(dev, "bus-range", &len);908if (bus_range == NULL || len < 2 * sizeof(int)) {909printk(KERN_WARNING "Can't get bus-range for %s, assume"910" bus 0\n", dev->full_name);911}912913hose = pcibios_alloc_controller(dev);914if (!hose)915return -ENOMEM;916hose->first_busno = bus_range ? bus_range[0] : 0;917hose->last_busno = bus_range ? bus_range[1] : 0xff;918919disp_name = NULL;920921/* 64 bits only bridges */922#ifdef CONFIG_PPC64923if (of_device_is_compatible(dev, "u3-agp")) {924setup_u3_agp(hose);925disp_name = "U3-AGP";926primary = 0;927} else if (of_device_is_compatible(dev, "u3-ht")) {928setup_u3_ht(hose);929disp_name = "U3-HT";930primary = 1;931} else if (of_device_is_compatible(dev, "u4-pcie")) {932setup_u4_pcie(hose);933disp_name = "U4-PCIE";934primary = 0;935}936printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"937" %d->%d\n", disp_name, hose->first_busno, hose->last_busno);938#endif /* CONFIG_PPC64 */939940/* 32 bits only bridges */941#ifdef CONFIG_PPC32942if (of_device_is_compatible(dev, "uni-north")) {943primary = setup_uninorth(hose, &rsrc);944disp_name = "UniNorth";945} else if (strcmp(dev->name, "pci") == 0) {946/* XXX assume this is a mpc106 (grackle) */947setup_grackle(hose);948disp_name = "Grackle (MPC106)";949} else if (strcmp(dev->name, "bandit") == 0) {950setup_bandit(hose, &rsrc);951disp_name = "Bandit";952} else if (strcmp(dev->name, "chaos") == 0) {953setup_chaos(hose, &rsrc);954disp_name = "Chaos";955primary = 0;956}957printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "958"Firmware bus number: %d->%d\n",959disp_name, (unsigned long long)rsrc.start, hose->first_busno,960hose->last_busno);961#endif /* CONFIG_PPC32 */962963DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",964hose, hose->cfg_addr, hose->cfg_data);965966/* Interpret the "ranges" property */967/* This also maps the I/O region and sets isa_io/mem_base */968pci_process_bridge_OF_ranges(hose, dev, primary);969970/* Fixup "bus-range" OF property */971fixup_bus_range(dev);972973return 0;974}975976void __devinit pmac_pci_irq_fixup(struct pci_dev *dev)977{978#ifdef CONFIG_PPC32979/* Fixup interrupt for the modem/ethernet combo controller.980* on machines with a second ohare chip.981* The number in the device tree (27) is bogus (correct for982* the ethernet-only board but not the combo ethernet/modem983* board). The real interrupt is 28 on the second controller984* -> 28+32 = 60.985*/986if (has_second_ohare &&987dev->vendor == PCI_VENDOR_ID_DEC &&988dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {989dev->irq = irq_create_mapping(NULL, 60);990irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);991}992#endif /* CONFIG_PPC32 */993}994995void __init pmac_pci_init(void)996{997struct device_node *np, *root;998struct device_node *ht = NULL;9991000ppc_pci_set_flags(PPC_PCI_CAN_SKIP_ISA_ALIGN);10011002root = of_find_node_by_path("/");1003if (root == NULL) {1004printk(KERN_CRIT "pmac_pci_init: can't find root "1005"of device tree\n");1006return;1007}1008for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {1009if (np->name == NULL)1010continue;1011if (strcmp(np->name, "bandit") == 01012|| strcmp(np->name, "chaos") == 01013|| strcmp(np->name, "pci") == 0) {1014if (pmac_add_bridge(np) == 0)1015of_node_get(np);1016}1017if (strcmp(np->name, "ht") == 0) {1018of_node_get(np);1019ht = np;1020}1021}1022of_node_put(root);10231024#ifdef CONFIG_PPC641025/* Probe HT last as it relies on the agp resources to be already1026* setup1027*/1028if (ht && pmac_add_bridge(ht) != 0)1029of_node_put(ht);10301031/* Setup the linkage between OF nodes and PHBs */1032pci_devs_phb_init();10331034/* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We1035* assume there is no P2P bridge on the AGP bus, which should be a1036* safe assumptions for now. We should do something better in the1037* future though1038*/1039if (u3_agp) {1040struct device_node *np = u3_agp->dn;1041PCI_DN(np)->busno = 0xf0;1042for (np = np->child; np; np = np->sibling)1043PCI_DN(np)->busno = 0xf0;1044}1045/* pmac_check_ht_link(); */10461047/* We can allocate missing resources if any */1048pci_probe_only = 0;10491050#else /* CONFIG_PPC64 */1051init_p2pbridge();1052init_second_ohare();1053fixup_nec_usb2();10541055/* We are still having some issues with the Xserve G4, enabling1056* some offset between bus number and domains for now when we1057* assign all busses should help for now1058*/1059if (ppc_pci_has_flag(PPC_PCI_REASSIGN_ALL_BUS))1060pcibios_assign_bus_offset = 0x10;1061#endif1062}10631064#ifdef CONFIG_PPC321065int pmac_pci_enable_device_hook(struct pci_dev *dev)1066{1067struct device_node* node;1068int updatecfg = 0;1069int uninorth_child;10701071node = pci_device_to_OF_node(dev);10721073/* We don't want to enable USB controllers absent from the OF tree1074* (iBook second controller)1075*/1076if (dev->vendor == PCI_VENDOR_ID_APPLE1077&& dev->class == PCI_CLASS_SERIAL_USB_OHCI1078&& !node) {1079printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",1080pci_name(dev));1081return -EINVAL;1082}10831084if (!node)1085return 0;10861087uninorth_child = node->parent &&1088of_device_is_compatible(node->parent, "uni-north");10891090/* Firewire & GMAC were disabled after PCI probe, the driver is1091* claiming them, we must re-enable them now.1092*/1093if (uninorth_child && !strcmp(node->name, "firewire") &&1094(of_device_is_compatible(node, "pci106b,18") ||1095of_device_is_compatible(node, "pci106b,30") ||1096of_device_is_compatible(node, "pci11c1,5811"))) {1097pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);1098pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);1099updatecfg = 1;1100}1101if (uninorth_child && !strcmp(node->name, "ethernet") &&1102of_device_is_compatible(node, "gmac")) {1103pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);1104updatecfg = 1;1105}11061107/*1108* Fixup various header fields on 32 bits. We don't do that on1109* 64 bits as some of these have strange values behind the HT1110* bridge and we must not, for example, enable MWI or set the1111* cache line size on them.1112*/1113if (updatecfg) {1114u16 cmd;11151116pci_read_config_word(dev, PCI_COMMAND, &cmd);1117cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER1118| PCI_COMMAND_INVALIDATE;1119pci_write_config_word(dev, PCI_COMMAND, cmd);1120pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);11211122pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,1123L1_CACHE_BYTES >> 2);1124}11251126return 0;1127}11281129void __devinit pmac_pci_fixup_ohci(struct pci_dev *dev)1130{1131struct device_node *node = pci_device_to_OF_node(dev);11321133/* We don't want to assign resources to USB controllers1134* absent from the OF tree (iBook second controller)1135*/1136if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)1137dev->resource[0].flags = 0;1138}1139DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);11401141/* We power down some devices after they have been probed. They'll1142* be powered back on later on1143*/1144void __init pmac_pcibios_after_init(void)1145{1146struct device_node* nd;11471148for_each_node_by_name(nd, "firewire") {1149if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||1150of_device_is_compatible(nd, "pci106b,30") ||1151of_device_is_compatible(nd, "pci11c1,5811"))1152&& of_device_is_compatible(nd->parent, "uni-north")) {1153pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);1154pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);1155}1156}1157for_each_node_by_name(nd, "ethernet") {1158if (nd->parent && of_device_is_compatible(nd, "gmac")1159&& of_device_is_compatible(nd->parent, "uni-north"))1160pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);1161}1162}11631164void pmac_pci_fixup_cardbus(struct pci_dev* dev)1165{1166if (!machine_is(powermac))1167return;1168/*1169* Fix the interrupt routing on the various cardbus bridges1170* used on powerbooks1171*/1172if (dev->vendor != PCI_VENDOR_ID_TI)1173return;1174if (dev->device == PCI_DEVICE_ID_TI_1130 ||1175dev->device == PCI_DEVICE_ID_TI_1131) {1176u8 val;1177/* Enable PCI interrupt */1178if (pci_read_config_byte(dev, 0x91, &val) == 0)1179pci_write_config_byte(dev, 0x91, val | 0x30);1180/* Disable ISA interrupt mode */1181if (pci_read_config_byte(dev, 0x92, &val) == 0)1182pci_write_config_byte(dev, 0x92, val & ~0x06);1183}1184if (dev->device == PCI_DEVICE_ID_TI_1210 ||1185dev->device == PCI_DEVICE_ID_TI_1211 ||1186dev->device == PCI_DEVICE_ID_TI_1410 ||1187dev->device == PCI_DEVICE_ID_TI_1510) {1188u8 val;1189/* 0x8c == TI122X_IRQMUX, 2 says to route the INTA1190signal out the MFUNC0 pin */1191if (pci_read_config_byte(dev, 0x8c, &val) == 0)1192pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);1193/* Disable ISA interrupt mode */1194if (pci_read_config_byte(dev, 0x92, &val) == 0)1195pci_write_config_byte(dev, 0x92, val & ~0x06);1196}1197}11981199DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);12001201void pmac_pci_fixup_pciata(struct pci_dev* dev)1202{1203u8 progif = 0;12041205/*1206* On PowerMacs, we try to switch any PCI ATA controller to1207* fully native mode1208*/1209if (!machine_is(powermac))1210return;12111212/* Some controllers don't have the class IDE */1213if (dev->vendor == PCI_VENDOR_ID_PROMISE)1214switch(dev->device) {1215case PCI_DEVICE_ID_PROMISE_20246:1216case PCI_DEVICE_ID_PROMISE_20262:1217case PCI_DEVICE_ID_PROMISE_20263:1218case PCI_DEVICE_ID_PROMISE_20265:1219case PCI_DEVICE_ID_PROMISE_20267:1220case PCI_DEVICE_ID_PROMISE_20268:1221case PCI_DEVICE_ID_PROMISE_20269:1222case PCI_DEVICE_ID_PROMISE_20270:1223case PCI_DEVICE_ID_PROMISE_20271:1224case PCI_DEVICE_ID_PROMISE_20275:1225case PCI_DEVICE_ID_PROMISE_20276:1226case PCI_DEVICE_ID_PROMISE_20277:1227goto good;1228}1229/* Others, check PCI class */1230if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)1231return;1232good:1233pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);1234if ((progif & 5) != 5) {1235printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",1236pci_name(dev));1237(void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);1238if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||1239(progif & 5) != 5)1240printk(KERN_ERR "Rewrite of PROGIF failed !\n");1241else {1242/* Clear IO BARs, they will be reassigned */1243pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);1244pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);1245pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);1246pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);1247}1248}1249}1250DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);1251#endif /* CONFIG_PPC32 */12521253/*1254* Disable second function on K2-SATA, it's broken1255* and disable IO BARs on first one1256*/1257static void fixup_k2_sata(struct pci_dev* dev)1258{1259int i;1260u16 cmd;12611262if (PCI_FUNC(dev->devfn) > 0) {1263pci_read_config_word(dev, PCI_COMMAND, &cmd);1264cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);1265pci_write_config_word(dev, PCI_COMMAND, cmd);1266for (i = 0; i < 6; i++) {1267dev->resource[i].start = dev->resource[i].end = 0;1268dev->resource[i].flags = 0;1269pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,12700);1271}1272} else {1273pci_read_config_word(dev, PCI_COMMAND, &cmd);1274cmd &= ~PCI_COMMAND_IO;1275pci_write_config_word(dev, PCI_COMMAND, cmd);1276for (i = 0; i < 5; i++) {1277dev->resource[i].start = dev->resource[i].end = 0;1278dev->resource[i].flags = 0;1279pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,12800);1281}1282}1283}1284DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);12851286/*1287* On U4 (aka CPC945) the PCIe root complex "P2P" bridge resource ranges aren't1288* configured by the firmware. The bridge itself seems to ignore them but it1289* causes problems with Linux which then re-assigns devices below the bridge,1290* thus changing addresses of those devices from what was in the device-tree,1291* which sucks when those are video cards using offb1292*1293* We could just mark it transparent but I prefer fixing up the resources to1294* properly show what's going on here, as I have some doubts about having them1295* badly configured potentially being an issue for DMA.1296*1297* We leave PIO alone, it seems to be fine1298*1299* Oh and there's another funny bug. The OF properties advertize the region1300* 0xf1000000..0xf1ffffff as being forwarded as memory space. But that's1301* actually not true, this region is the memory mapped config space. So we1302* also need to filter it out or we'll map things in the wrong place.1303*/1304static void fixup_u4_pcie(struct pci_dev* dev)1305{1306struct pci_controller *host = pci_bus_to_host(dev->bus);1307struct resource *region = NULL;1308u32 reg;1309int i;13101311/* Only do that on PowerMac */1312if (!machine_is(powermac))1313return;13141315/* Find the largest MMIO region */1316for (i = 0; i < 3; i++) {1317struct resource *r = &host->mem_resources[i];1318if (!(r->flags & IORESOURCE_MEM))1319continue;1320/* Skip the 0xf0xxxxxx..f2xxxxxx regions, we know they1321* are reserved by HW for other things1322*/1323if (r->start >= 0xf0000000 && r->start < 0xf3000000)1324continue;1325if (!region || (r->end - r->start) >1326(region->end - region->start))1327region = r;1328}1329/* Nothing found, bail */1330if (region == 0)1331return;13321333/* Print things out */1334printk(KERN_INFO "PCI: Fixup U4 PCIe bridge range: %pR\n", region);13351336/* Fixup bridge config space. We know it's a Mac, resource aren't1337* offset so let's just blast them as-is. We also know that they1338* fit in 32 bits1339*/1340reg = ((region->start >> 16) & 0xfff0) | (region->end & 0xfff00000);1341pci_write_config_dword(dev, PCI_MEMORY_BASE, reg);1342pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0);1343pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);1344pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0);1345}1346DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_U4_PCIE, fixup_u4_pcie);134713481349