Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.34.14.2
      1  1.34.14.2  macallan /*	$NetBSD: pci_machdep.c,v 1.34.14.2 2007/05/06 22:51:40 macallan Exp $	*/
      2        1.1    tsubai 
      3        1.1    tsubai /*
      4        1.1    tsubai  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5        1.5   mycroft  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      6        1.1    tsubai  *
      7        1.1    tsubai  * Redistribution and use in source and binary forms, with or without
      8        1.1    tsubai  * modification, are permitted provided that the following conditions
      9        1.1    tsubai  * are met:
     10        1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     11        1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     12        1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     14        1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     15        1.1    tsubai  * 3. All advertising materials mentioning features or use of this software
     16        1.1    tsubai  *    must display the following acknowledgement:
     17        1.5   mycroft  *	This product includes software developed by Charles M. Hannum.
     18        1.1    tsubai  * 4. The name of the author may not be used to endorse or promote products
     19        1.1    tsubai  *    derived from this software without specific prior written permission.
     20        1.1    tsubai  *
     21        1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22        1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23        1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24        1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25        1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26        1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27        1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28        1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29        1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30        1.1    tsubai  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31        1.1    tsubai  */
     32        1.1    tsubai 
     33        1.1    tsubai /*
     34        1.1    tsubai  * Machine-specific functions for PCI autoconfiguration.
     35        1.1    tsubai  *
     36        1.1    tsubai  * On PCs, there are two methods of generating PCI configuration cycles.
     37        1.1    tsubai  * We try to detect the appropriate mechanism for this machine and set
     38        1.1    tsubai  * up a few function pointers to access the correct method directly.
     39        1.1    tsubai  *
     40        1.1    tsubai  * The configuration method can be hard-coded in the config file by
     41        1.1    tsubai  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
     42        1.1    tsubai  * as defined section 3.6.4.1, `Generating Configuration Cycles'.
     43        1.1    tsubai  */
     44       1.21     lukem 
     45       1.21     lukem #include <sys/cdefs.h>
     46  1.34.14.2  macallan __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.34.14.2 2007/05/06 22:51:40 macallan Exp $");
     47        1.1    tsubai 
     48        1.1    tsubai #include <sys/types.h>
     49        1.1    tsubai #include <sys/param.h>
     50        1.1    tsubai #include <sys/time.h>
     51        1.1    tsubai #include <sys/systm.h>
     52        1.1    tsubai #include <sys/errno.h>
     53        1.1    tsubai #include <sys/device.h>
     54        1.1    tsubai 
     55       1.13       mrg #include <uvm/uvm_extern.h>
     56        1.1    tsubai 
     57        1.3    tsubai #define _MACPPC_BUS_DMA_PRIVATE
     58        1.3    tsubai #include <machine/bus.h>
     59        1.3    tsubai 
     60       1.33  macallan #include <machine/autoconf.h>
     61        1.1    tsubai #include <machine/pio.h>
     62        1.1    tsubai #include <machine/intr.h>
     63        1.1    tsubai 
     64        1.1    tsubai #include <dev/pci/pcivar.h>
     65        1.1    tsubai #include <dev/pci/pcireg.h>
     66       1.33  macallan #include <dev/pci/ppbreg.h>
     67       1.32  macallan #include <dev/pci/pcidevs.h>
     68        1.1    tsubai 
     69       1.10    tsubai #include <dev/ofw/openfirm.h>
     70       1.10    tsubai #include <dev/ofw/ofw_pci.h>
     71       1.10    tsubai 
     72       1.34  macallan #include "opt_macppc.h"
     73       1.34  macallan 
     74  1.34.14.2  macallan static void fixpci(int, pci_chipset_tag_t);
     75  1.34.14.2  macallan static int find_node_intr(int, u_int32_t *, u_int32_t *);
     76       1.33  macallan static void fix_cardbus_bridge(int, pci_chipset_tag_t, pcitag_t);
     77       1.10    tsubai 
     78       1.34  macallan #ifdef PB3400_CARDBUS_HACK
     79       1.34  macallan int cardbus_number = 2;
     80       1.34  macallan const char *pb3400_compat[] = {"AAPL,3400/2400", NULL};
     81       1.34  macallan #endif
     82       1.34  macallan 
     83  1.34.14.1  macallan pcitag_t genppc_pci_indirect_make_tag(void *, int, int, int);
     84  1.34.14.1  macallan void genppc_pci_indirect_decompose_tag(void *, pcitag_t, int *, int *, int *);
     85  1.34.14.1  macallan 
     86        1.3    tsubai /*
     87        1.3    tsubai  * PCI doesn't have any special needs; just use the generic versions
     88        1.3    tsubai  * of these functions.
     89        1.3    tsubai  */
     90        1.3    tsubai struct macppc_bus_dma_tag pci_bus_dma_tag = {
     91        1.3    tsubai 	0,			/* _bounce_thresh */
     92        1.3    tsubai 	_bus_dmamap_create,
     93        1.3    tsubai 	_bus_dmamap_destroy,
     94        1.3    tsubai 	_bus_dmamap_load,
     95        1.3    tsubai 	_bus_dmamap_load_mbuf,
     96        1.3    tsubai 	_bus_dmamap_load_uio,
     97        1.3    tsubai 	_bus_dmamap_load_raw,
     98        1.3    tsubai 	_bus_dmamap_unload,
     99        1.3    tsubai 	NULL,			/* _dmamap_sync */
    100        1.3    tsubai 	_bus_dmamem_alloc,
    101        1.3    tsubai 	_bus_dmamem_free,
    102        1.3    tsubai 	_bus_dmamem_map,
    103        1.3    tsubai 	_bus_dmamem_unmap,
    104        1.3    tsubai 	_bus_dmamem_mmap,
    105        1.3    tsubai };
    106        1.1    tsubai 
    107        1.1    tsubai void
    108  1.34.14.1  macallan macppc_pci_attach_hook(parent, self, pba)
    109        1.1    tsubai 	struct device *parent, *self;
    110        1.1    tsubai 	struct pcibus_attach_args *pba;
    111        1.1    tsubai {
    112       1.10    tsubai 	pci_chipset_tag_t pc = pba->pba_pc;
    113       1.10    tsubai 	int bus = pba->pba_bus;
    114       1.10    tsubai 	int node, nn, sz;
    115       1.10    tsubai 	int32_t busrange[2];
    116       1.10    tsubai 
    117  1.34.14.1  macallan 	for (node = pc->pc_node; node; node = nn) {
    118       1.10    tsubai 		sz = OF_getprop(node, "bus-range", busrange, 8);
    119       1.10    tsubai 		if (sz == 8 && busrange[0] == bus) {
    120       1.10    tsubai 			fixpci(node, pc);
    121       1.10    tsubai 			return;
    122       1.10    tsubai 		}
    123       1.10    tsubai 		if ((nn = OF_child(node)) != 0)
    124       1.10    tsubai 			continue;
    125       1.10    tsubai 		while ((nn = OF_peer(node)) == 0) {
    126       1.10    tsubai 			node = OF_parent(node);
    127  1.34.14.1  macallan 			if (node == pc->pc_node)
    128       1.10    tsubai 				return;		/* not found */
    129       1.10    tsubai 		}
    130       1.10    tsubai 	}
    131        1.1    tsubai }
    132        1.1    tsubai 
    133        1.1    tsubai void
    134  1.34.14.1  macallan macppc_pci_get_chipset_tag(pci_chipset_tag_t pc)
    135        1.1    tsubai {
    136        1.1    tsubai 
    137  1.34.14.1  macallan 	pc->pc_conf_v = (void *)pc;
    138        1.1    tsubai 
    139  1.34.14.1  macallan 	pc->pc_attach_hook = macppc_pci_attach_hook;
    140  1.34.14.1  macallan 	pc->pc_bus_maxdevs = genppc_pci_bus_maxdevs;
    141        1.1    tsubai 
    142  1.34.14.1  macallan 	pc->pc_make_tag = genppc_pci_indirect_make_tag;
    143  1.34.14.1  macallan 	pc->pc_decompose_tag = genppc_pci_indirect_decompose_tag;
    144        1.1    tsubai 
    145  1.34.14.1  macallan 	pc->pc_intr_v = (void *)pc;
    146        1.1    tsubai 
    147  1.34.14.1  macallan 	pc->pc_intr_map = genppc_pci_intr_map;
    148  1.34.14.1  macallan 	pc->pc_intr_string = genppc_pci_intr_string;
    149  1.34.14.1  macallan 	pc->pc_intr_evcnt = genppc_pci_intr_evcnt;
    150  1.34.14.1  macallan 	pc->pc_intr_establish = genppc_pci_intr_establish;
    151  1.34.14.1  macallan 	pc->pc_intr_disestablish = genppc_pci_intr_disestablish;
    152        1.1    tsubai 
    153  1.34.14.1  macallan 	pc->pc_conf_interrupt = genppc_pci_conf_interrupt;
    154  1.34.14.1  macallan 	pc->pc_conf_hook = genppc_pci_conf_hook;
    155        1.1    tsubai 
    156  1.34.14.1  macallan 	pc->pc_bus = 0;
    157  1.34.14.1  macallan 	pc->pc_node = 0;
    158  1.34.14.1  macallan 	pc->pc_memt = 0;
    159  1.34.14.1  macallan 	pc->pc_iot = 0;
    160       1.10    tsubai }
    161       1.10    tsubai 
    162       1.10    tsubai #define pcibus(x) \
    163       1.10    tsubai 	(((x) & OFW_PCI_PHYS_HI_BUSMASK) >> OFW_PCI_PHYS_HI_BUSSHIFT)
    164       1.10    tsubai #define pcidev(x) \
    165       1.10    tsubai 	(((x) & OFW_PCI_PHYS_HI_DEVICEMASK) >> OFW_PCI_PHYS_HI_DEVICESHIFT)
    166       1.10    tsubai #define pcifunc(x) \
    167       1.10    tsubai 	(((x) & OFW_PCI_PHYS_HI_FUNCTIONMASK) >> OFW_PCI_PHYS_HI_FUNCTIONSHIFT)
    168       1.10    tsubai 
    169  1.34.14.2  macallan static void
    170  1.34.14.2  macallan fixpci(int parent, pci_chipset_tag_t pc)
    171       1.10    tsubai {
    172       1.10    tsubai 	int node;
    173       1.10    tsubai 	pcitag_t tag;
    174       1.33  macallan 	pcireg_t csr, intr, id, cr;
    175       1.22      matt 	int len, i, ilen;
    176       1.10    tsubai 	int32_t irqs[4];
    177       1.10    tsubai 	struct {
    178       1.10    tsubai 		u_int32_t phys_hi, phys_mid, phys_lo;
    179       1.10    tsubai 		u_int32_t size_hi, size_lo;
    180       1.10    tsubai 	} addr[8];
    181       1.22      matt 	struct {
    182       1.22      matt 		u_int32_t phys_hi, phys_mid, phys_lo;
    183       1.22      matt 		u_int32_t icells[5];
    184       1.22      matt 	} iaddr;
    185       1.10    tsubai 
    186       1.34  macallan 	/*
    187       1.34  macallan 	 * first hack - here we make the Ethernet portion of a
    188       1.34  macallan 	 * UMAX E100 card work
    189       1.34  macallan 	 */
    190       1.34  macallan #ifdef UMAX_E100_HACK
    191       1.34  macallan 	tag = pci_make_tag(pc, 0, 17, 0);
    192       1.34  macallan 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    193       1.34  macallan 	if ((PCI_VENDOR(id) == PCI_VENDOR_DEC) &&
    194       1.34  macallan 	    (PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21140)) {
    195       1.34  macallan 		/* this could be one */
    196       1.34  macallan 		pcireg_t isp, reg;
    197       1.34  macallan 		pcitag_t tag_isp = pci_make_tag(pc, 0, 13, 0);
    198       1.34  macallan 		/*
    199       1.34  macallan 		 * here we go. We shouldn't encounter this anywhere else
    200       1.34  macallan 		 * than on a UMAX S900 with an E100 board
    201       1.34  macallan 		 * look at 00:0d:00 for a Qlogic ISP 1020 to
    202       1.34  macallan 		 * make sure we really have an E100 here
    203       1.34  macallan 		 */
    204  1.34.14.1  macallan 		aprint_debug("\nfound E100 candidate tlp");
    205       1.34  macallan 		isp = pci_conf_read(pc, tag_isp, PCI_ID_REG);
    206       1.34  macallan 		if ((PCI_VENDOR(isp) == PCI_VENDOR_QLOGIC) &&
    207       1.34  macallan 		    (PCI_PRODUCT(isp) == PCI_PRODUCT_QLOGIC_ISP1020)) {
    208       1.34  macallan 
    209       1.34  macallan 			aprint_verbose("\nenabling UMAX E100 ethernet");
    210       1.34  macallan 
    211       1.34  macallan 			pci_conf_write(pc, tag, 0x14, 0x80000000);
    212       1.34  macallan 
    213       1.34  macallan 			/* now enable MMIO and busmastering */
    214       1.34  macallan 			reg = pci_conf_read(pc, tag,
    215       1.34  macallan 			    PCI_COMMAND_STATUS_REG);
    216       1.34  macallan 			reg |= PCI_COMMAND_MEM_ENABLE |
    217       1.34  macallan 			       PCI_COMMAND_MASTER_ENABLE;
    218       1.34  macallan 			pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    219       1.34  macallan 			    reg);
    220       1.34  macallan 
    221       1.34  macallan 			/* and finally the interrupt */
    222       1.34  macallan 			reg = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    223       1.34  macallan 			reg &= ~PCI_INTERRUPT_LINE_MASK;
    224       1.34  macallan 			reg |= 23;
    225       1.34  macallan 			pci_conf_write(pc, tag, PCI_INTERRUPT_REG, reg);
    226       1.34  macallan 		}
    227       1.34  macallan 	}
    228       1.34  macallan #endif
    229       1.34  macallan 
    230       1.22      matt 	len = OF_getprop(parent, "#interrupt-cells", &ilen, sizeof(ilen));
    231       1.22      matt 	if (len < 0)
    232       1.22      matt 		ilen = 0;
    233       1.10    tsubai 	for (node = OF_child(parent); node; node = OF_peer(node)) {
    234       1.10    tsubai 		len = OF_getprop(node, "assigned-addresses", addr,
    235       1.10    tsubai 				 sizeof(addr));
    236       1.10    tsubai 		if (len < (int)sizeof(addr[0]))
    237       1.10    tsubai 			continue;
    238       1.10    tsubai 
    239       1.10    tsubai 		tag = pci_make_tag(pc, pcibus(addr[0].phys_hi),
    240       1.10    tsubai 				   pcidev(addr[0].phys_hi),
    241       1.10    tsubai 				   pcifunc(addr[0].phys_hi));
    242       1.10    tsubai 
    243       1.10    tsubai 		/*
    244       1.10    tsubai 		 * Make sure the IO and MEM enable bits are set in the CSR.
    245       1.10    tsubai 		 */
    246       1.10    tsubai 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    247       1.10    tsubai 		csr &= ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
    248       1.10    tsubai 
    249       1.10    tsubai 		for (i = 0; i < len / sizeof(addr[0]); i++) {
    250       1.10    tsubai 			switch (addr[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
    251       1.10    tsubai 			case OFW_PCI_PHYS_HI_SPACE_IO:
    252       1.10    tsubai 				csr |= PCI_COMMAND_IO_ENABLE;
    253       1.10    tsubai 				break;
    254       1.10    tsubai 
    255       1.10    tsubai 			case OFW_PCI_PHYS_HI_SPACE_MEM32:
    256       1.19  wrstuden 			case OFW_PCI_PHYS_HI_SPACE_MEM64:
    257       1.10    tsubai 				csr |= PCI_COMMAND_MEM_ENABLE;
    258       1.10    tsubai 				break;
    259       1.10    tsubai 			}
    260       1.10    tsubai 		}
    261       1.10    tsubai 
    262       1.10    tsubai 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    263       1.10    tsubai 
    264       1.10    tsubai 		/*
    265       1.10    tsubai 		 * Make sure the line register is programmed with the
    266       1.10    tsubai 		 * interrupt mapping.
    267       1.10    tsubai 		 */
    268       1.23      matt 		if (ilen == 0) {
    269       1.23      matt 			/*
    270       1.23      matt 			 * Early Apple OFW implementation don't handle
    271       1.23      matt 			 * interrupts as defined by the OFW PCI bindings.
    272       1.23      matt 			 */
    273       1.23      matt 			len = OF_getprop(node, "AAPL,interrupts", irqs, 4);
    274       1.23      matt 		} else {
    275       1.23      matt 			iaddr.phys_hi = addr[0].phys_hi;
    276       1.23      matt 			iaddr.phys_mid = addr[0].phys_mid;
    277       1.23      matt 			iaddr.phys_lo = addr[0].phys_lo;
    278       1.23      matt 			/*
    279       1.23      matt 			 * Thankfully, PCI can only have one entry in its
    280       1.23      matt 			 * "interrupts" property.
    281       1.23      matt 			 */
    282       1.23      matt 			len = OF_getprop(node, "interrupts", &iaddr.icells[0],
    283       1.23      matt 			    4*ilen);
    284       1.23      matt 			if (len != 4*ilen)
    285       1.23      matt 				continue;
    286       1.23      matt 			len = find_node_intr(node, &iaddr.phys_hi, irqs);
    287       1.23      matt 		}
    288       1.27    briggs 		if (len <= 0) {
    289       1.27    briggs 			/*
    290       1.27    briggs 			 * If we still don't have an interrupt, try one
    291       1.27    briggs 			 * more time.  This case covers devices behind the
    292       1.27    briggs 			 * PCI-PCI bridge in a UMAX S900 or similar (9500?)
    293       1.27    briggs 			 * system.  These slots all share the bridge's
    294       1.27    briggs 			 * interrupt.
    295       1.27    briggs 			 */
    296       1.27    briggs 			len = find_node_intr(node, &addr[0].phys_hi, irqs);
    297       1.27    briggs 			if (len <= 0)
    298       1.27    briggs 				continue;
    299       1.22      matt 		}
    300       1.32  macallan 
    301       1.32  macallan 		/*
    302       1.32  macallan 		 * For PowerBook 2400, 3400 and original G3:
    303       1.32  macallan 		 * check if we have a 2nd ohare PIC - if so frob the built-in
    304       1.32  macallan 		 * tlp's IRQ to 60
    305       1.32  macallan 		 * first see if we have something on bus 0 device 13 and if
    306       1.32  macallan 		 * it's a DEC 21041
    307       1.32  macallan 		 */
    308       1.32  macallan 		id = pci_conf_read(pc, tag, PCI_ID_REG);
    309       1.32  macallan 		if ((tag == pci_make_tag(pc, 0, 13, 0)) &&
    310       1.32  macallan 		    (PCI_VENDOR(id) == PCI_VENDOR_DEC) &&
    311       1.32  macallan 		    (PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21041)) {
    312       1.32  macallan 
    313       1.32  macallan 			/* now look for the 2nd ohare */
    314       1.32  macallan 			if (OF_finddevice("/bandit/pci106b,7") != -1) {
    315       1.32  macallan 
    316       1.32  macallan 				irqs[0] = 60;
    317       1.34  macallan 				aprint_verbose("\nohare: frobbing tlp IRQ to 60");
    318       1.32  macallan 			}
    319       1.32  macallan 		}
    320       1.32  macallan 
    321       1.27    briggs 		intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    322       1.27    briggs 		intr &= ~PCI_INTERRUPT_LINE_MASK;
    323       1.27    briggs 		intr |= irqs[0] & PCI_INTERRUPT_LINE_MASK;
    324       1.27    briggs 		pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
    325       1.33  macallan 
    326       1.33  macallan 		/* fix secondary bus numbers on CardBus bridges */
    327       1.33  macallan 		cr = pci_conf_read(pc, tag, PCI_CLASS_REG);
    328       1.33  macallan 		if ((PCI_CLASS(cr) == PCI_CLASS_BRIDGE) &&
    329       1.33  macallan 		    (PCI_SUBCLASS(cr) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
    330       1.33  macallan 			uint32_t bi, busid;
    331       1.33  macallan 
    332       1.33  macallan 			/*
    333       1.33  macallan 			 * we found a CardBus bridge. Check if the bus number
    334       1.33  macallan 			 * is sane
    335       1.33  macallan 			 */
    336       1.33  macallan 			bi = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
    337       1.33  macallan 			busid = bi & 0xff;
    338       1.33  macallan 			if (busid == 0) {
    339       1.33  macallan 				fix_cardbus_bridge(node, pc, tag);
    340       1.33  macallan 			}
    341       1.33  macallan 		}
    342       1.33  macallan 	}
    343       1.33  macallan }
    344       1.33  macallan 
    345       1.33  macallan static void
    346       1.33  macallan fix_cardbus_bridge(int node, pci_chipset_tag_t pc, pcitag_t tag)
    347       1.33  macallan {
    348       1.34  macallan 	uint32_t bus_number = 0xffffffff;
    349       1.33  macallan 	pcireg_t bi;
    350       1.33  macallan 	int bus, dev, fn, ih, len;
    351       1.33  macallan 	char path[256];
    352       1.33  macallan 
    353       1.34  macallan #if PB3400_CARDBUS_HACK
    354       1.34  macallan 	int root_node;
    355       1.34  macallan 
    356       1.34  macallan 	root_node = OF_finddevice("/");
    357       1.34  macallan 	if (of_compatible(root_node, pb3400_compat) != -1) {
    358       1.33  macallan 
    359       1.34  macallan 		bus_number = cardbus_number;
    360       1.34  macallan 		cardbus_number++;
    361       1.34  macallan 	} else {
    362       1.34  macallan #endif
    363       1.34  macallan 		ih = OF_open(path);
    364       1.34  macallan 		OF_call_method("load-ata", ih, 0, 0);
    365       1.34  macallan 		OF_close(ih);
    366       1.34  macallan 
    367       1.34  macallan 		OF_getprop(node, "AAPL,bus-id", &bus_number,
    368       1.34  macallan 		    sizeof(bus_number));
    369       1.34  macallan #if PB3400_CARDBUS_HACK
    370       1.34  macallan 	}
    371       1.34  macallan #endif
    372       1.34  macallan 	if (bus_number != 0xffffffff) {
    373       1.33  macallan 
    374       1.34  macallan 		len = OF_package_to_path(node, path, sizeof(path));
    375       1.34  macallan 		path[len] = 0;
    376       1.34  macallan 		aprint_verbose("\n%s: fixing bus number to %d", path, bus_number);
    377       1.33  macallan 		pci_decompose_tag(pc, tag, &bus, &dev, &fn);
    378       1.33  macallan 		bi = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
    379       1.33  macallan 		bi &= 0xff000000;
    380       1.33  macallan 		/* XXX subordinate is always 32 here */
    381       1.33  macallan 		bi |= (bus & 0xff) | (bus_number << 8) | 0x200000;
    382       1.33  macallan 		pci_conf_write(pc, tag, PPB_REG_BUSINFO, bi);
    383       1.10    tsubai 	}
    384       1.10    tsubai }
    385       1.10    tsubai 
    386       1.10    tsubai /*
    387       1.10    tsubai  * Find PCI IRQ of the node from OF tree.
    388       1.10    tsubai  */
    389  1.34.14.2  macallan static int
    390  1.34.14.2  macallan find_node_intr(int node, u_int32_t *addr, uint32_t *intr)
    391       1.10    tsubai {
    392       1.10    tsubai 	int parent, len, mlen, iparent;
    393       1.10    tsubai 	int match, i;
    394       1.22      matt 	u_int32_t map[160];
    395       1.22      matt 	const u_int32_t *mp;
    396       1.28      matt 	u_int32_t imapmask[8], maskedaddr[8];
    397       1.22      matt 	u_int32_t acells, icells;
    398       1.10    tsubai 	char name[32];
    399       1.10    tsubai 
    400       1.30   sanjayl 	/* XXXSL: 1st check for a  interrupt-parent property */
    401       1.30   sanjayl         if (OF_getprop(node, "interrupt-parent", &iparent, sizeof(iparent)) == sizeof(iparent))
    402       1.30   sanjayl 	{
    403       1.30   sanjayl 		/* How many cells to specify an interrupt ?? */
    404       1.30   sanjayl 		if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
    405       1.30   sanjayl 			return -1;
    406       1.30   sanjayl 
    407       1.30   sanjayl 		if (OF_getprop(node, "interrupts", &map, sizeof(map)) != (icells * 4))
    408       1.30   sanjayl 			return -1;
    409       1.30   sanjayl 
    410       1.30   sanjayl 		memcpy(intr, map, icells * 4);
    411       1.30   sanjayl 		return (icells * 4);
    412       1.30   sanjayl 	}
    413       1.30   sanjayl 
    414       1.10    tsubai 	parent = OF_parent(node);
    415       1.10    tsubai 	len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
    416       1.28      matt 	mlen = OF_getprop(parent, "interrupt-map-mask", imapmask,
    417       1.28      matt 	    sizeof(imapmask));
    418       1.10    tsubai 
    419       1.22      matt 	if (mlen != -1)
    420       1.22      matt 		memcpy(maskedaddr, addr, mlen);
    421       1.22      matt again:
    422       1.10    tsubai 	if (len == -1 || mlen == -1)
    423       1.10    tsubai 		goto nomap;
    424       1.10    tsubai 
    425       1.10    tsubai #ifdef DIAGNOSTIC
    426       1.28      matt 	if (mlen == sizeof(imapmask)) {
    427       1.34  macallan 		aprint_error("interrupt-map too long\n");
    428       1.10    tsubai 		return -1;
    429       1.10    tsubai 	}
    430       1.10    tsubai #endif
    431       1.10    tsubai 
    432       1.10    tsubai 	/* mask addr by "interrupt-map-mask" */
    433       1.10    tsubai 	for (i = 0; i < mlen / 4; i++)
    434       1.28      matt 		maskedaddr[i] &= imapmask[i];
    435       1.10    tsubai 
    436       1.10    tsubai 	mp = map;
    437       1.22      matt 	i = 0;
    438       1.10    tsubai 	while (len > mlen) {
    439       1.18       wiz 		match = memcmp(maskedaddr, mp, mlen);
    440       1.10    tsubai 		mp += mlen / 4;
    441       1.10    tsubai 		len -= mlen;
    442       1.10    tsubai 
    443       1.10    tsubai 		/*
    444       1.22      matt 		 * We must read "#address-cells" and "#interrupt-cells" each
    445       1.22      matt 		 * time because each interrupt-parent may be different.
    446       1.10    tsubai 		 */
    447       1.10    tsubai 		iparent = *mp++;
    448       1.10    tsubai 		len -= 4;
    449       1.25      matt 		i = OF_getprop(iparent, "#address-cells", &acells, 4);
    450       1.25      matt 		if (i <= 0)
    451       1.25      matt 			acells = 0;
    452       1.25      matt 		else if (i != 4)
    453       1.22      matt 			return -1;
    454       1.10    tsubai 		if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
    455       1.22      matt 			return -1;
    456       1.10    tsubai 
    457       1.10    tsubai 		/* Found. */
    458       1.10    tsubai 		if (match == 0) {
    459       1.22      matt 			/*
    460       1.22      matt 			 * We matched on address/interrupt, but are we done?
    461       1.22      matt 			 */
    462       1.22      matt 			if (acells == 0) { /* XXX */
    463       1.22      matt 				/*
    464       1.22      matt 				 * If we are at the interrupt controller,
    465       1.22      matt 				 * we are finally done.  Save the result and
    466       1.22      matt 				 * return.
    467       1.22      matt 				 */
    468       1.22      matt 				memcpy(intr, mp, icells * 4);
    469       1.22      matt 				return icells * 4;
    470       1.22      matt 			}
    471       1.22      matt 			/*
    472       1.22      matt 			 * We are now at an intermedia interrupt node.  We
    473       1.22      matt 			 * need to use its interrupt mask and map the
    474       1.24       wiz 			 * supplied address/interrupt via its map.
    475       1.22      matt 			 */
    476       1.22      matt 			mlen = OF_getprop(iparent, "interrupt-map-mask",
    477       1.28      matt 			    imapmask, sizeof(imapmask));
    478       1.22      matt #ifdef DIAGNOSTIC
    479       1.22      matt 			if (mlen != (acells + icells)*4) {
    480       1.34  macallan 				aprint_error("interrupt-map inconsistent (%d, %d)\n",
    481       1.22      matt 				    mlen, (acells + icells)*4);
    482       1.22      matt 				return -1;
    483       1.22      matt 			}
    484       1.22      matt #endif
    485       1.22      matt 			memcpy(maskedaddr, mp, mlen);
    486       1.22      matt 			len = OF_getprop(iparent, "interrupt-map", map,
    487       1.22      matt 			    sizeof(map));
    488       1.22      matt 			goto again;
    489       1.10    tsubai 		}
    490       1.10    tsubai 
    491       1.22      matt 		mp += (acells + icells);
    492       1.22      matt 		len -= (acells + icells) * 4;
    493       1.10    tsubai 	}
    494       1.10    tsubai 
    495       1.10    tsubai nomap:
    496       1.10    tsubai 	/*
    497       1.10    tsubai 	 * If the node has no interrupt property and the parent is a
    498       1.10    tsubai 	 * pci-bridge, use parent's interrupt.  This occurs on a PCI
    499       1.10    tsubai 	 * slot.  (e.g. AHA-3940)
    500       1.10    tsubai 	 */
    501       1.18       wiz 	memset(name, 0, sizeof(name));
    502       1.10    tsubai 	OF_getprop(parent, "name", name, sizeof(name));
    503       1.10    tsubai 	if (strcmp(name, "pci-bridge") == 0) {
    504       1.10    tsubai 		len = OF_getprop(parent, "AAPL,interrupts", intr, 4) ;
    505       1.10    tsubai 		if (len == 4)
    506       1.10    tsubai 			return len;
    507       1.22      matt #if 0
    508       1.15    tsubai 		/*
    509       1.15    tsubai 		 * XXX I don't know what is the correct local address.
    510       1.15    tsubai 		 * XXX Use the first entry for now.
    511       1.15    tsubai 		 */
    512       1.15    tsubai 		len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
    513       1.15    tsubai 		if (len >= 36) {
    514       1.15    tsubai 			addr = &map[5];
    515       1.15    tsubai 			return find_node_intr(parent, addr, intr);
    516       1.15    tsubai 		}
    517       1.22      matt #endif
    518       1.10    tsubai 	}
    519       1.10    tsubai 
    520       1.26    briggs 	/*
    521       1.26    briggs 	 * If all else fails, attempt to get AAPL, interrupts property.
    522       1.26    briggs 	 * Grackle, at least, uses this instead of above in some cases.
    523       1.26    briggs 	 */
    524       1.26    briggs 	len = OF_getprop(node, "AAPL,interrupts", intr, 4) ;
    525       1.10    tsubai 	if (len == 4)
    526       1.10    tsubai 		return len;
    527       1.10    tsubai 
    528       1.10    tsubai 	return -1;
    529        1.1    tsubai }
    530