Path: blob/master/arch/powerpc/platforms/powermac/pci.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Support for PCI bridges found on Power Macintoshes.3*4* Copyright (C) 2003-2005 Benjamin Herrenschmuidt ([email protected])5* Copyright (C) 1997 Paul Mackerras ([email protected])6*/78#include <linux/kernel.h>9#include <linux/pci.h>10#include <linux/delay.h>11#include <linux/string.h>12#include <linux/init.h>13#include <linux/irq.h>14#include <linux/of_address.h>15#include <linux/of_irq.h>16#include <linux/of_pci.h>1718#include <asm/sections.h>19#include <asm/io.h>20#include <asm/pci-bridge.h>21#include <asm/machdep.h>22#include <asm/pmac_feature.h>23#include <asm/grackle.h>24#include <asm/ppc-pci.h>2526#include "pmac.h"2728#undef DEBUG2930#ifdef DEBUG31#define DBG(x...) printk(x)32#else33#define DBG(x...)34#endif3536/* XXX Could be per-controller, but I don't think we risk anything by37* assuming we won't have both UniNorth and Bandit */38static int has_uninorth;39#ifdef CONFIG_PPC6440static struct pci_controller *u3_agp;41#else42static int has_second_ohare;43#endif /* CONFIG_PPC64 */4445extern int pcibios_assign_bus_offset;4647struct device_node *k2_skiplist[2];4849/*50* Magic constants for enabling cache coherency in the bandit/PSX bridge.51*/52#define BANDIT_DEVID_2 853#define BANDIT_REVID 35455#define BANDIT_DEVNUM 1156#define BANDIT_MAGIC 0x5057#define BANDIT_COHERENT 0x405859static int __init fixup_one_level_bus_range(struct device_node *node, int higher)60{61for (; node; node = node->sibling) {62const int * bus_range;63const unsigned int *class_code;64int len;6566/* For PCI<->PCI bridges or CardBus bridges, we go down */67class_code = of_get_property(node, "class-code", NULL);68if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&69(*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))70continue;71bus_range = of_get_property(node, "bus-range", &len);72if (bus_range != NULL && len > 2 * sizeof(int)) {73if (bus_range[1] > higher)74higher = bus_range[1];75}76higher = fixup_one_level_bus_range(node->child, higher);77}78return higher;79}8081/* This routine fixes the "bus-range" property of all bridges in the82* system since they tend to have their "last" member wrong on macs83*84* Note that the bus numbers manipulated here are OF bus numbers, they85* are not Linux bus numbers.86*/87static void __init fixup_bus_range(struct device_node *bridge)88{89int *bus_range, len;90struct property *prop;9192/* Lookup the "bus-range" property for the hose */93prop = of_find_property(bridge, "bus-range", &len);94if (prop == NULL || prop->length < 2 * sizeof(int))95return;9697bus_range = prop->value;98bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);99}100101/*102* Apple MacRISC (U3, UniNorth, Bandit, Chaos) PCI controllers.103*104* The "Bandit" version is present in all early PCI PowerMacs,105* and up to the first ones using Grackle. Some machines may106* have 2 bandit controllers (2 PCI busses).107*108* "Chaos" is used in some "Bandit"-type machines as a bridge109* for the separate display bus. It is accessed the same110* way as bandit, but cannot be probed for devices. It therefore111* has its own config access functions.112*113* The "UniNorth" version is present in all Core99 machines114* (iBook, G4, new IMacs, and all the recent Apple machines).115* It contains 3 controllers in one ASIC.116*117* The U3 is the bridge used on G5 machines. It contains an118* AGP bus which is dealt with the old UniNorth access routines119* and a HyperTransport bus which uses its own set of access120* functions.121*/122123#define MACRISC_CFA0(devfn, off) \124((1 << (unsigned int)PCI_SLOT(dev_fn)) \125| (((unsigned int)PCI_FUNC(dev_fn)) << 8) \126| (((unsigned int)(off)) & 0xFCUL))127128#define MACRISC_CFA1(bus, devfn, off) \129((((unsigned int)(bus)) << 16) \130|(((unsigned int)(devfn)) << 8) \131|(((unsigned int)(off)) & 0xFCUL) \132|1UL)133134static void __iomem *macrisc_cfg_map_bus(struct pci_bus *bus,135unsigned int dev_fn,136int offset)137{138unsigned int caddr;139struct pci_controller *hose;140141hose = pci_bus_to_host(bus);142if (hose == NULL)143return NULL;144145if (bus->number == hose->first_busno) {146if (dev_fn < (11 << 3))147return NULL;148caddr = MACRISC_CFA0(dev_fn, offset);149} else150caddr = MACRISC_CFA1(bus->number, dev_fn, offset);151152/* Uninorth will return garbage if we don't read back the value ! */153do {154out_le32(hose->cfg_addr, caddr);155} while (in_le32(hose->cfg_addr) != caddr);156157offset &= has_uninorth ? 0x07 : 0x03;158return hose->cfg_data + offset;159}160161static struct pci_ops macrisc_pci_ops =162{163.map_bus = macrisc_cfg_map_bus,164.read = pci_generic_config_read,165.write = pci_generic_config_write,166};167168#ifdef CONFIG_PPC32169/*170* Verify that a specific (bus, dev_fn) exists on chaos171*/172static void __iomem *chaos_map_bus(struct pci_bus *bus, unsigned int devfn,173int offset)174{175struct device_node *np;176const u32 *vendor, *device;177178if (offset >= 0x100)179return NULL;180np = of_pci_find_child_device(bus->dev.of_node, devfn);181if (np == NULL)182return NULL;183184vendor = of_get_property(np, "vendor-id", NULL);185device = of_get_property(np, "device-id", NULL);186if (vendor == NULL || device == NULL)187return NULL;188189if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)190&& (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))191return NULL;192193return macrisc_cfg_map_bus(bus, devfn, offset);194}195196static struct pci_ops chaos_pci_ops =197{198.map_bus = chaos_map_bus,199.read = pci_generic_config_read,200.write = pci_generic_config_write,201};202203static void __init setup_chaos(struct pci_controller *hose,204struct resource *addr)205{206/* assume a `chaos' bridge */207hose->ops = &chaos_pci_ops;208hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);209hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);210}211#endif /* CONFIG_PPC32 */212213#ifdef CONFIG_PPC64214/*215* These versions of U3 HyperTransport config space access ops do not216* implement self-view of the HT host yet217*/218219/*220* This function deals with some "special cases" devices.221*222* 0 -> No special case223* 1 -> Skip the device but act as if the access was successful224* (return 0xff's on reads, eventually, cache config space225* accesses in a later version)226* -1 -> Hide the device (unsuccessful access)227*/228static int u3_ht_skip_device(struct pci_controller *hose,229struct pci_bus *bus, unsigned int devfn)230{231struct device_node *busdn, *dn;232int i;233234/* We only allow config cycles to devices that are in OF device-tree235* as we are apparently having some weird things going on with some236* revs of K2 on recent G5s, except for the host bridge itself, which237* is missing from the tree but we know we can probe.238*/239if (bus->self)240busdn = pci_device_to_OF_node(bus->self);241else if (devfn == 0)242return 0;243else244busdn = hose->dn;245for (dn = busdn->child; dn; dn = dn->sibling)246if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)247break;248if (dn == NULL)249return -1;250251/*252* When a device in K2 is powered down, we die on config253* cycle accesses. Fix that here.254*/255for (i=0; i<2; i++)256if (k2_skiplist[i] == dn)257return 1;258259return 0;260}261262#define U3_HT_CFA0(devfn, off) \263((((unsigned int)devfn) << 8) | offset)264#define U3_HT_CFA1(bus, devfn, off) \265(U3_HT_CFA0(devfn, off) \266+ (((unsigned int)bus) << 16) \267+ 0x01000000UL)268269static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,270u8 devfn, u8 offset, int *swap)271{272*swap = 1;273if (bus == hose->first_busno) {274if (devfn != 0)275return hose->cfg_data + U3_HT_CFA0(devfn, offset);276*swap = 0;277return ((void __iomem *)hose->cfg_addr) + (offset << 2);278} else279return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);280}281282static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,283int offset, int len, u32 *val)284{285struct pci_controller *hose;286void __iomem *addr;287int swap;288289hose = pci_bus_to_host(bus);290if (hose == NULL)291return PCIBIOS_DEVICE_NOT_FOUND;292if (offset >= 0x100)293return PCIBIOS_BAD_REGISTER_NUMBER;294addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);295if (!addr)296return PCIBIOS_DEVICE_NOT_FOUND;297298switch (u3_ht_skip_device(hose, bus, devfn)) {299case 0:300break;301case 1:302switch (len) {303case 1:304*val = 0xff; break;305case 2:306*val = 0xffff; break;307default:308*val = 0xfffffffful; break;309}310return PCIBIOS_SUCCESSFUL;311default:312return PCIBIOS_DEVICE_NOT_FOUND;313}314315/*316* Note: the caller has already checked that offset is317* suitably aligned and that len is 1, 2 or 4.318*/319switch (len) {320case 1:321*val = in_8(addr);322break;323case 2:324*val = swap ? in_le16(addr) : in_be16(addr);325break;326default:327*val = swap ? in_le32(addr) : in_be32(addr);328break;329}330return PCIBIOS_SUCCESSFUL;331}332333static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,334int offset, int len, u32 val)335{336struct pci_controller *hose;337void __iomem *addr;338int swap;339340hose = pci_bus_to_host(bus);341if (hose == NULL)342return PCIBIOS_DEVICE_NOT_FOUND;343if (offset >= 0x100)344return PCIBIOS_BAD_REGISTER_NUMBER;345addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);346if (!addr)347return PCIBIOS_DEVICE_NOT_FOUND;348349switch (u3_ht_skip_device(hose, bus, devfn)) {350case 0:351break;352case 1:353return PCIBIOS_SUCCESSFUL;354default:355return PCIBIOS_DEVICE_NOT_FOUND;356}357358/*359* Note: the caller has already checked that offset is360* suitably aligned and that len is 1, 2 or 4.361*/362switch (len) {363case 1:364out_8(addr, val);365break;366case 2:367swap ? out_le16(addr, val) : out_be16(addr, val);368break;369default:370swap ? out_le32(addr, val) : out_be32(addr, val);371break;372}373return PCIBIOS_SUCCESSFUL;374}375376static struct pci_ops u3_ht_pci_ops =377{378.read = u3_ht_read_config,379.write = u3_ht_write_config,380};381382#define U4_PCIE_CFA0(devfn, off) \383((1 << ((unsigned int)PCI_SLOT(dev_fn))) \384| (((unsigned int)PCI_FUNC(dev_fn)) << 8) \385| ((((unsigned int)(off)) >> 8) << 28) \386| (((unsigned int)(off)) & 0xfcU))387388#define U4_PCIE_CFA1(bus, devfn, off) \389((((unsigned int)(bus)) << 16) \390|(((unsigned int)(devfn)) << 8) \391| ((((unsigned int)(off)) >> 8) << 28) \392|(((unsigned int)(off)) & 0xfcU) \393|1UL)394395static void __iomem *u4_pcie_cfg_map_bus(struct pci_bus *bus,396unsigned int dev_fn,397int offset)398{399struct pci_controller *hose;400unsigned int caddr;401402if (offset >= 0x1000)403return NULL;404405hose = pci_bus_to_host(bus);406if (!hose)407return NULL;408409if (bus->number == hose->first_busno) {410caddr = U4_PCIE_CFA0(dev_fn, offset);411} else412caddr = U4_PCIE_CFA1(bus->number, dev_fn, offset);413414/* Uninorth will return garbage if we don't read back the value ! */415do {416out_le32(hose->cfg_addr, caddr);417} while (in_le32(hose->cfg_addr) != caddr);418419offset &= 0x03;420return hose->cfg_data + offset;421}422423static struct pci_ops u4_pcie_pci_ops =424{425.map_bus = u4_pcie_cfg_map_bus,426.read = pci_generic_config_read,427.write = pci_generic_config_write,428};429430static void pmac_pci_fixup_u4_of_node(struct pci_dev *dev)431{432/* Apple's device-tree "hides" the root complex virtual P2P bridge433* on U4. However, Linux sees it, causing the PCI <-> OF matching434* code to fail to properly match devices below it. This works around435* it by setting the node of the bridge to point to the PHB node,436* which is not entirely correct but fixes the matching code and437* doesn't break anything else. It's also the simplest possible fix.438*/439if (dev->dev.of_node == NULL)440dev->dev.of_node = pcibios_get_phb_of_node(dev->bus);441}442DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, 0x5b, pmac_pci_fixup_u4_of_node);443444#endif /* CONFIG_PPC64 */445446#ifdef CONFIG_PPC32447/*448* For a bandit bridge, turn on cache coherency if necessary.449* N.B. we could clean this up using the hose ops directly.450*/451static void __init init_bandit(struct pci_controller *bp)452{453unsigned int vendev, magic;454int rev;455456/* read the word at offset 0 in config space for device 11 */457out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);458udelay(2);459vendev = in_le32(bp->cfg_data);460if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +461PCI_VENDOR_ID_APPLE) {462/* read the revision id */463out_le32(bp->cfg_addr,464(1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);465udelay(2);466rev = in_8(bp->cfg_data);467if (rev != BANDIT_REVID)468printk(KERN_WARNING469"Unknown revision %d for bandit\n", rev);470} else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {471printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);472return;473}474475/* read the word at offset 0x50 */476out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);477udelay(2);478magic = in_le32(bp->cfg_data);479if ((magic & BANDIT_COHERENT) != 0)480return;481magic |= BANDIT_COHERENT;482udelay(2);483out_le32(bp->cfg_data, magic);484printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");485}486487/*488* Tweak the PCI-PCI bridge chip on the blue & white G3s.489*/490static void __init init_p2pbridge(void)491{492struct device_node *p2pbridge;493struct pci_controller* hose;494u8 bus, devfn;495u16 val;496497/* XXX it would be better here to identify the specific498PCI-PCI bridge chip we have. */499p2pbridge = of_find_node_by_name(NULL, "pci-bridge");500if (p2pbridge == NULL || !of_node_name_eq(p2pbridge->parent, "pci"))501goto done;502if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {503DBG("Can't find PCI infos for PCI<->PCI bridge\n");504goto done;505}506/* Warning: At this point, we have not yet renumbered all busses.507* So we must use OF walking to find out hose508*/509hose = pci_find_hose_for_OF_device(p2pbridge);510if (!hose) {511DBG("Can't find hose for PCI<->PCI bridge\n");512goto done;513}514if (early_read_config_word(hose, bus, devfn,515PCI_BRIDGE_CONTROL, &val) < 0) {516printk(KERN_ERR "init_p2pbridge: couldn't read bridge"517" control\n");518goto done;519}520val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;521early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);522done:523of_node_put(p2pbridge);524}525526static void __init init_second_ohare(void)527{528struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");529unsigned char bus, devfn;530unsigned short cmd;531532if (np == NULL)533return;534535/* This must run before we initialize the PICs since the second536* ohare hosts a PIC that will be accessed there.537*/538if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {539struct pci_controller* hose =540pci_find_hose_for_OF_device(np);541if (!hose) {542printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");543of_node_put(np);544return;545}546early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);547cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;548cmd &= ~PCI_COMMAND_IO;549early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);550}551has_second_ohare = 1;552of_node_put(np);553}554555/*556* Some Apple desktop machines have a NEC PD720100A USB2 controller557* on the motherboard. Open Firmware, on these, will disable the558* EHCI part of it so it behaves like a pair of OHCI's. This fixup559* code re-enables it ;)560*/561static void __init fixup_nec_usb2(void)562{563struct device_node *nec;564565for_each_node_by_name(nec, "usb") {566struct pci_controller *hose;567u32 data;568const u32 *prop;569u8 bus, devfn;570571prop = of_get_property(nec, "vendor-id", NULL);572if (prop == NULL)573continue;574if (0x1033 != *prop)575continue;576prop = of_get_property(nec, "device-id", NULL);577if (prop == NULL)578continue;579if (0x0035 != *prop)580continue;581prop = of_get_property(nec, "reg", NULL);582if (prop == NULL)583continue;584devfn = (prop[0] >> 8) & 0xff;585bus = (prop[0] >> 16) & 0xff;586if (PCI_FUNC(devfn) != 0)587continue;588hose = pci_find_hose_for_OF_device(nec);589if (!hose)590continue;591early_read_config_dword(hose, bus, devfn, 0xe4, &data);592if (data & 1UL) {593printk("Found NEC PD720100A USB2 chip with disabled"594" EHCI, fixing up...\n");595data &= ~1UL;596early_write_config_dword(hose, bus, devfn, 0xe4, data);597}598}599}600601static void __init setup_bandit(struct pci_controller *hose,602struct resource *addr)603{604hose->ops = ¯isc_pci_ops;605hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);606hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);607init_bandit(hose);608}609610static int __init setup_uninorth(struct pci_controller *hose,611struct resource *addr)612{613pci_add_flags(PCI_REASSIGN_ALL_BUS);614has_uninorth = 1;615hose->ops = ¯isc_pci_ops;616hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);617hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);618/* We "know" that the bridge at f2000000 has the PCI slots. */619return addr->start == 0xf2000000;620}621#endif /* CONFIG_PPC32 */622623#ifdef CONFIG_PPC64624static void __init setup_u3_agp(struct pci_controller* hose)625{626/* On G5, we move AGP up to high bus number so we don't need627* to reassign bus numbers for HT. If we ever have P2P bridges628* on AGP, we'll have to move pci_assign_all_busses to the629* pci_controller structure so we enable it for AGP and not for630* HT childs.631* We hard code the address because of the different size of632* the reg address cell, we shall fix that by killing struct633* reg_property and using some accessor functions instead634*/635hose->first_busno = 0xf0;636hose->last_busno = 0xff;637has_uninorth = 1;638hose->ops = ¯isc_pci_ops;639hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);640hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);641u3_agp = hose;642}643644static void __init setup_u4_pcie(struct pci_controller* hose)645{646/* We currently only implement the "non-atomic" config space, to647* be optimised later.648*/649hose->ops = &u4_pcie_pci_ops;650hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);651hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);652653/* The bus contains a bridge from root -> device, we need to654* make it visible on bus 0 so that we pick the right type655* of config cycles. If we didn't, we would have to force all656* config cycles to be type 1. So we override the "bus-range"657* property here658*/659hose->first_busno = 0x00;660hose->last_busno = 0xff;661}662663static void __init parse_region_decode(struct pci_controller *hose,664u32 decode)665{666unsigned long base, end, next = -1;667int i, cur = -1;668669/* Iterate through all bits. We ignore the last bit as this region is670* reserved for the ROM among other niceties671*/672for (i = 0; i < 31; i++) {673if ((decode & (0x80000000 >> i)) == 0)674continue;675if (i < 16) {676base = 0xf0000000 | (((u32)i) << 24);677end = base + 0x00ffffff;678} else {679base = ((u32)i-16) << 28;680end = base + 0x0fffffff;681}682if (base != next) {683if (++cur >= 3) {684printk(KERN_WARNING "PCI: Too many ranges !\n");685break;686}687hose->mem_resources[cur].flags = IORESOURCE_MEM;688hose->mem_resources[cur].name = hose->dn->full_name;689hose->mem_resources[cur].start = base;690hose->mem_resources[cur].end = end;691hose->mem_offset[cur] = 0;692DBG(" %d: 0x%08lx-0x%08lx\n", cur, base, end);693} else {694DBG(" : -0x%08lx\n", end);695hose->mem_resources[cur].end = end;696}697next = end + 1;698}699}700701static void __init setup_u3_ht(struct pci_controller* hose)702{703struct device_node *np = hose->dn;704struct resource cfg_res, self_res;705u32 decode;706707hose->ops = &u3_ht_pci_ops;708709/* Get base addresses from OF tree710*/711if (of_address_to_resource(np, 0, &cfg_res) ||712of_address_to_resource(np, 1, &self_res)) {713printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");714return;715}716717/* Map external cfg space access into cfg_data and self registers718* into cfg_addr719*/720hose->cfg_data = ioremap(cfg_res.start, 0x02000000);721hose->cfg_addr = ioremap(self_res.start, resource_size(&self_res));722723/*724* /ht node doesn't expose a "ranges" property, we read the register725* that controls the decoding logic and use that for memory regions.726* The IO region is hard coded since it is fixed in HW as well.727*/728hose->io_base_phys = 0xf4000000;729hose->pci_io_size = 0x00400000;730hose->io_resource.name = np->full_name;731hose->io_resource.start = 0;732hose->io_resource.end = 0x003fffff;733hose->io_resource.flags = IORESOURCE_IO;734hose->first_busno = 0;735hose->last_busno = 0xef;736737/* Note: fix offset when cfg_addr becomes a void * */738decode = in_be32(hose->cfg_addr + 0x80);739740DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);741742/* NOTE: The decode register setup is a bit weird... region743* 0xf8000000 for example is marked as enabled in there while it's744& actually the memory controller registers.745* That means that we are incorrectly attributing it to HT.746*747* In a similar vein, region 0xf4000000 is actually the HT IO space but748* also marked as enabled in here and 0xf9000000 is used by some other749* internal bits of the northbridge.750*751* Unfortunately, we can't just mask out those bit as we would end752* up with more regions than we can cope (linux can only cope with753* 3 memory regions for a PHB at this stage).754*755* So for now, we just do a little hack. We happen to -know- that756* Apple firmware doesn't assign things below 0xfa000000 for that757* bridge anyway so we mask out all bits we don't want.758*/759decode &= 0x003fffff;760761/* Now parse the resulting bits and build resources */762parse_region_decode(hose, decode);763}764#endif /* CONFIG_PPC64 */765766/*767* We assume that if we have a G3 powermac, we have one bridge called768* "pci" (a MPC106) and no bandit or chaos bridges, and contrariwise,769* if we have one or more bandit or chaos bridges, we don't have a MPC106.770*/771static int __init pmac_add_bridge(struct device_node *dev)772{773int len;774struct pci_controller *hose;775struct resource rsrc;776char *disp_name;777const int *bus_range;778int primary = 1;779780DBG("Adding PCI host bridge %pOF\n", dev);781782/* Fetch host bridge registers address */783of_address_to_resource(dev, 0, &rsrc);784785/* Get bus range if any */786bus_range = of_get_property(dev, "bus-range", &len);787if (bus_range == NULL || len < 2 * sizeof(int)) {788printk(KERN_WARNING "Can't get bus-range for %pOF, assume"789" bus 0\n", dev);790}791792hose = pcibios_alloc_controller(dev);793if (!hose)794return -ENOMEM;795hose->first_busno = bus_range ? bus_range[0] : 0;796hose->last_busno = bus_range ? bus_range[1] : 0xff;797hose->controller_ops = pmac_pci_controller_ops;798799disp_name = NULL;800801/* 64 bits only bridges */802#ifdef CONFIG_PPC64803if (of_device_is_compatible(dev, "u3-agp")) {804setup_u3_agp(hose);805disp_name = "U3-AGP";806primary = 0;807} else if (of_device_is_compatible(dev, "u3-ht")) {808setup_u3_ht(hose);809disp_name = "U3-HT";810primary = 1;811} else if (of_device_is_compatible(dev, "u4-pcie")) {812setup_u4_pcie(hose);813disp_name = "U4-PCIE";814primary = 0;815}816printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"817" %d->%d\n", disp_name, hose->first_busno, hose->last_busno);818#endif /* CONFIG_PPC64 */819820/* 32 bits only bridges */821#ifdef CONFIG_PPC32822if (of_device_is_compatible(dev, "uni-north")) {823primary = setup_uninorth(hose, &rsrc);824disp_name = "UniNorth";825} else if (of_node_name_eq(dev, "pci")) {826/* XXX assume this is a mpc106 (grackle) */827setup_grackle(hose);828disp_name = "Grackle (MPC106)";829} else if (of_node_name_eq(dev, "bandit")) {830setup_bandit(hose, &rsrc);831disp_name = "Bandit";832} else if (of_node_name_eq(dev, "chaos")) {833setup_chaos(hose, &rsrc);834disp_name = "Chaos";835primary = 0;836}837printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "838"Firmware bus number: %d->%d\n",839disp_name, (unsigned long long)rsrc.start, hose->first_busno,840hose->last_busno);841#endif /* CONFIG_PPC32 */842843DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",844hose, hose->cfg_addr, hose->cfg_data);845846/* Interpret the "ranges" property */847/* This also maps the I/O region and sets isa_io/mem_base */848pci_process_bridge_OF_ranges(hose, dev, primary);849850/* Fixup "bus-range" OF property */851fixup_bus_range(dev);852853/* create pci_dn's for DT nodes under this PHB */854if (IS_ENABLED(CONFIG_PPC64))855pci_devs_phb_init_dynamic(hose);856857return 0;858}859860void pmac_pci_irq_fixup(struct pci_dev *dev)861{862#ifdef CONFIG_PPC32863/* Fixup interrupt for the modem/ethernet combo controller.864* on machines with a second ohare chip.865* The number in the device tree (27) is bogus (correct for866* the ethernet-only board but not the combo ethernet/modem867* board). The real interrupt is 28 on the second controller868* -> 28+32 = 60.869*/870if (has_second_ohare &&871dev->vendor == PCI_VENDOR_ID_DEC &&872dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {873dev->irq = irq_create_mapping(NULL, 60);874irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);875}876#endif /* CONFIG_PPC32 */877}878879#ifdef CONFIG_PPC64880static int pmac_pci_root_bridge_prepare(struct pci_host_bridge *bridge)881{882struct pci_controller *hose = pci_bus_to_host(bridge->bus);883struct device_node *np, *child;884885if (hose != u3_agp)886return 0;887888/* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We889* assume there is no P2P bridge on the AGP bus, which should be a890* safe assumptions for now. We should do something better in the891* future though892*/893np = hose->dn;894PCI_DN(np)->busno = 0xf0;895for_each_child_of_node(np, child)896PCI_DN(child)->busno = 0xf0;897898return 0;899}900#endif /* CONFIG_PPC64 */901902void __init pmac_pci_init(void)903{904struct device_node *np, *root;905struct device_node *ht __maybe_unused = NULL;906907pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);908909root = of_find_node_by_path("/");910if (root == NULL) {911printk(KERN_CRIT "pmac_pci_init: can't find root "912"of device tree\n");913return;914}915for_each_child_of_node(root, np) {916if (of_node_name_eq(np, "bandit")917|| of_node_name_eq(np, "chaos")918|| of_node_name_eq(np, "pci")) {919if (pmac_add_bridge(np) == 0)920of_node_get(np);921}922if (of_node_name_eq(np, "ht")) {923of_node_get(np);924ht = np;925}926}927of_node_put(root);928929#ifdef CONFIG_PPC64930/* Probe HT last as it relies on the agp resources to be already931* setup932*/933if (ht && pmac_add_bridge(ht) != 0)934of_node_put(ht);935936ppc_md.pcibios_root_bridge_prepare = pmac_pci_root_bridge_prepare;937/* pmac_check_ht_link(); */938939#else /* CONFIG_PPC64 */940init_p2pbridge();941init_second_ohare();942fixup_nec_usb2();943944/* We are still having some issues with the Xserve G4, enabling945* some offset between bus number and domains for now when we946* assign all busses should help for now947*/948if (pci_has_flag(PCI_REASSIGN_ALL_BUS))949pcibios_assign_bus_offset = 0x10;950#endif951}952953#ifdef CONFIG_PPC32954static bool pmac_pci_enable_device_hook(struct pci_dev *dev)955{956struct device_node* node;957int updatecfg = 0;958int uninorth_child;959960node = pci_device_to_OF_node(dev);961962/* We don't want to enable USB controllers absent from the OF tree963* (iBook second controller)964*/965if (dev->vendor == PCI_VENDOR_ID_APPLE966&& dev->class == PCI_CLASS_SERIAL_USB_OHCI967&& !node) {968printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",969pci_name(dev));970return false;971}972973if (!node)974return true;975976uninorth_child = node->parent &&977of_device_is_compatible(node->parent, "uni-north");978979/* Firewire & GMAC were disabled after PCI probe, the driver is980* claiming them, we must re-enable them now.981*/982if (uninorth_child && of_node_name_eq(node, "firewire") &&983(of_device_is_compatible(node, "pci106b,18") ||984of_device_is_compatible(node, "pci106b,30") ||985of_device_is_compatible(node, "pci11c1,5811"))) {986pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);987pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);988updatecfg = 1;989}990if (uninorth_child && of_node_name_eq(node, "ethernet") &&991of_device_is_compatible(node, "gmac")) {992pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);993updatecfg = 1;994}995996/*997* Fixup various header fields on 32 bits. We don't do that on998* 64 bits as some of these have strange values behind the HT999* bridge and we must not, for example, enable MWI or set the1000* cache line size on them.1001*/1002if (updatecfg) {1003u16 cmd;10041005pci_read_config_word(dev, PCI_COMMAND, &cmd);1006cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER1007| PCI_COMMAND_INVALIDATE;1008pci_write_config_word(dev, PCI_COMMAND, cmd);1009pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);10101011pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,1012L1_CACHE_BYTES >> 2);1013}10141015return true;1016}10171018static void pmac_pci_fixup_ohci(struct pci_dev *dev)1019{1020struct device_node *node = pci_device_to_OF_node(dev);10211022/* We don't want to assign resources to USB controllers1023* absent from the OF tree (iBook second controller)1024*/1025if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)1026dev->resource[0].flags = 0;1027}1028DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);10291030/* We power down some devices after they have been probed. They'll1031* be powered back on later on1032*/1033void __init pmac_pcibios_after_init(void)1034{1035struct device_node* nd;10361037for_each_node_by_name(nd, "firewire") {1038if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||1039of_device_is_compatible(nd, "pci106b,30") ||1040of_device_is_compatible(nd, "pci11c1,5811"))1041&& of_device_is_compatible(nd->parent, "uni-north")) {1042pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);1043pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);1044}1045}1046for_each_node_by_name(nd, "ethernet") {1047if (nd->parent && of_device_is_compatible(nd, "gmac")1048&& of_device_is_compatible(nd->parent, "uni-north"))1049pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);1050}1051}10521053static void pmac_pci_fixup_cardbus(struct pci_dev *dev)1054{1055if (!machine_is(powermac))1056return;1057/*1058* Fix the interrupt routing on the various cardbus bridges1059* used on powerbooks1060*/1061if (dev->vendor != PCI_VENDOR_ID_TI)1062return;1063if (dev->device == PCI_DEVICE_ID_TI_1130 ||1064dev->device == PCI_DEVICE_ID_TI_1131) {1065u8 val;1066/* Enable PCI interrupt */1067if (pci_read_config_byte(dev, 0x91, &val) == 0)1068pci_write_config_byte(dev, 0x91, val | 0x30);1069/* Disable ISA interrupt mode */1070if (pci_read_config_byte(dev, 0x92, &val) == 0)1071pci_write_config_byte(dev, 0x92, val & ~0x06);1072}1073if (dev->device == PCI_DEVICE_ID_TI_1210 ||1074dev->device == PCI_DEVICE_ID_TI_1211 ||1075dev->device == PCI_DEVICE_ID_TI_1410 ||1076dev->device == PCI_DEVICE_ID_TI_1510) {1077u8 val;1078/* 0x8c == TI122X_IRQMUX, 2 says to route the INTA1079signal out the MFUNC0 pin */1080if (pci_read_config_byte(dev, 0x8c, &val) == 0)1081pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);1082/* Disable ISA interrupt mode */1083if (pci_read_config_byte(dev, 0x92, &val) == 0)1084pci_write_config_byte(dev, 0x92, val & ~0x06);1085}1086}10871088DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);10891090static void pmac_pci_fixup_pciata(struct pci_dev *dev)1091{1092u8 progif = 0;10931094/*1095* On PowerMacs, we try to switch any PCI ATA controller to1096* fully native mode1097*/1098if (!machine_is(powermac))1099return;11001101/* Some controllers don't have the class IDE */1102if (dev->vendor == PCI_VENDOR_ID_PROMISE)1103switch(dev->device) {1104case PCI_DEVICE_ID_PROMISE_20246:1105case PCI_DEVICE_ID_PROMISE_20262:1106case PCI_DEVICE_ID_PROMISE_20263:1107case PCI_DEVICE_ID_PROMISE_20265:1108case PCI_DEVICE_ID_PROMISE_20267:1109case PCI_DEVICE_ID_PROMISE_20268:1110case PCI_DEVICE_ID_PROMISE_20269:1111case PCI_DEVICE_ID_PROMISE_20270:1112case PCI_DEVICE_ID_PROMISE_20271:1113case PCI_DEVICE_ID_PROMISE_20275:1114case PCI_DEVICE_ID_PROMISE_20276:1115case PCI_DEVICE_ID_PROMISE_20277:1116goto good;1117}1118/* Others, check PCI class */1119if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)1120return;1121good:1122pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);1123if ((progif & 5) != 5) {1124printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",1125pci_name(dev));1126(void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);1127if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||1128(progif & 5) != 5)1129printk(KERN_ERR "Rewrite of PROGIF failed !\n");1130else {1131/* Clear IO BARs, they will be reassigned */1132pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);1133pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);1134pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);1135pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);1136}1137}1138}1139DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);1140#endif /* CONFIG_PPC32 */11411142/*1143* Disable second function on K2-SATA, it's broken1144* and disable IO BARs on first one1145*/1146static void fixup_k2_sata(struct pci_dev* dev)1147{1148int i;1149u16 cmd;11501151if (PCI_FUNC(dev->devfn) > 0) {1152pci_read_config_word(dev, PCI_COMMAND, &cmd);1153cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);1154pci_write_config_word(dev, PCI_COMMAND, cmd);1155for (i = 0; i < 6; i++) {1156dev->resource[i].start = dev->resource[i].end = 0;1157dev->resource[i].flags = 0;1158pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,11590);1160}1161} else {1162pci_read_config_word(dev, PCI_COMMAND, &cmd);1163cmd &= ~PCI_COMMAND_IO;1164pci_write_config_word(dev, PCI_COMMAND, cmd);1165for (i = 0; i < 5; i++) {1166dev->resource[i].start = dev->resource[i].end = 0;1167dev->resource[i].flags = 0;1168pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,11690);1170}1171}1172}1173DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);11741175/*1176* On U4 (aka CPC945) the PCIe root complex "P2P" bridge resource ranges aren't1177* configured by the firmware. The bridge itself seems to ignore them but it1178* causes problems with Linux which then re-assigns devices below the bridge,1179* thus changing addresses of those devices from what was in the device-tree,1180* which sucks when those are video cards using offb1181*1182* We could just mark it transparent but I prefer fixing up the resources to1183* properly show what's going on here, as I have some doubts about having them1184* badly configured potentially being an issue for DMA.1185*1186* We leave PIO alone, it seems to be fine1187*1188* Oh and there's another funny bug. The OF properties advertize the region1189* 0xf1000000..0xf1ffffff as being forwarded as memory space. But that's1190* actually not true, this region is the memory mapped config space. So we1191* also need to filter it out or we'll map things in the wrong place.1192*/1193static void fixup_u4_pcie(struct pci_dev* dev)1194{1195struct pci_controller *host = pci_bus_to_host(dev->bus);1196struct resource *region = NULL;1197u32 reg;1198int i;11991200/* Only do that on PowerMac */1201if (!machine_is(powermac))1202return;12031204/* Find the largest MMIO region */1205for (i = 0; i < 3; i++) {1206struct resource *r = &host->mem_resources[i];1207if (!(r->flags & IORESOURCE_MEM))1208continue;1209/* Skip the 0xf0xxxxxx..f2xxxxxx regions, we know they1210* are reserved by HW for other things1211*/1212if (r->start >= 0xf0000000 && r->start < 0xf3000000)1213continue;1214if (!region || resource_size(r) > resource_size(region))1215region = r;1216}1217/* Nothing found, bail */1218if (!region)1219return;12201221/* Print things out */1222printk(KERN_INFO "PCI: Fixup U4 PCIe bridge range: %pR\n", region);12231224/* Fixup bridge config space. We know it's a Mac, resource aren't1225* offset so let's just blast them as-is. We also know that they1226* fit in 32 bits1227*/1228reg = ((region->start >> 16) & 0xfff0) | (region->end & 0xfff00000);1229pci_write_config_dword(dev, PCI_MEMORY_BASE, reg);1230pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0);1231pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);1232pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0);1233}1234DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_U4_PCIE, fixup_u4_pcie);12351236#ifdef CONFIG_PPC641237static int pmac_pci_probe_mode(struct pci_bus *bus)1238{1239struct device_node *node = pci_bus_to_OF_node(bus);12401241/* We need to use normal PCI probing for the AGP bus,1242* since the device for the AGP bridge isn't in the tree.1243* Same for the PCIe host on U4 and the HT host bridge.1244*/1245if (bus->self == NULL && (of_device_is_compatible(node, "u3-agp") ||1246of_device_is_compatible(node, "u4-pcie") ||1247of_device_is_compatible(node, "u3-ht")))1248return PCI_PROBE_NORMAL;1249return PCI_PROBE_DEVTREE;1250}1251#endif /* CONFIG_PPC64 */12521253struct pci_controller_ops pmac_pci_controller_ops = {1254#ifdef CONFIG_PPC641255.probe_mode = pmac_pci_probe_mode,1256#endif1257#ifdef CONFIG_PPC321258.enable_device_hook = pmac_pci_enable_device_hook,1259#endif1260};126112621263