Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.28.2.2
      1  1.28.2.2      yamt /*	$NetBSD: pci_machdep.c,v 1.28.2.2 2007/02/26 09:07:23 yamt 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.28.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.28.2.2 2007/02/26 09:07:23 yamt 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.28.2.2      yamt #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.28.2.2      yamt #include <dev/pci/ppbreg.h>
     67  1.28.2.1      yamt #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.28.2.2      yamt #include "opt_macppc.h"
     73  1.28.2.2      yamt 
     74      1.10    tsubai static void fixpci __P((int, pci_chipset_tag_t));
     75      1.10    tsubai static int find_node_intr __P((int, u_int32_t *, u_int32_t *));
     76  1.28.2.2      yamt static void fix_cardbus_bridge(int, pci_chipset_tag_t, pcitag_t);
     77  1.28.2.2      yamt 
     78  1.28.2.2      yamt #ifdef PB3400_CARDBUS_HACK
     79  1.28.2.2      yamt int cardbus_number = 2;
     80  1.28.2.2      yamt const char *pb3400_compat[] = {"AAPL,3400/2400", NULL};
     81  1.28.2.2      yamt #endif
     82      1.10    tsubai 
     83       1.3    tsubai /*
     84       1.3    tsubai  * PCI doesn't have any special needs; just use the generic versions
     85       1.3    tsubai  * of these functions.
     86       1.3    tsubai  */
     87       1.3    tsubai struct macppc_bus_dma_tag pci_bus_dma_tag = {
     88       1.3    tsubai 	0,			/* _bounce_thresh */
     89       1.3    tsubai 	_bus_dmamap_create,
     90       1.3    tsubai 	_bus_dmamap_destroy,
     91       1.3    tsubai 	_bus_dmamap_load,
     92       1.3    tsubai 	_bus_dmamap_load_mbuf,
     93       1.3    tsubai 	_bus_dmamap_load_uio,
     94       1.3    tsubai 	_bus_dmamap_load_raw,
     95       1.3    tsubai 	_bus_dmamap_unload,
     96       1.3    tsubai 	NULL,			/* _dmamap_sync */
     97       1.3    tsubai 	_bus_dmamem_alloc,
     98       1.3    tsubai 	_bus_dmamem_free,
     99       1.3    tsubai 	_bus_dmamem_map,
    100       1.3    tsubai 	_bus_dmamem_unmap,
    101       1.3    tsubai 	_bus_dmamem_mmap,
    102       1.3    tsubai };
    103       1.1    tsubai 
    104       1.1    tsubai void
    105       1.1    tsubai pci_attach_hook(parent, self, pba)
    106       1.1    tsubai 	struct device *parent, *self;
    107       1.1    tsubai 	struct pcibus_attach_args *pba;
    108       1.1    tsubai {
    109      1.10    tsubai 	pci_chipset_tag_t pc = pba->pba_pc;
    110      1.10    tsubai 	int bus = pba->pba_bus;
    111      1.10    tsubai 	int node, nn, sz;
    112      1.10    tsubai 	int32_t busrange[2];
    113      1.10    tsubai 
    114      1.10    tsubai 	for (node = pc->node; node; node = nn) {
    115      1.10    tsubai 		sz = OF_getprop(node, "bus-range", busrange, 8);
    116      1.10    tsubai 		if (sz == 8 && busrange[0] == bus) {
    117      1.10    tsubai 			fixpci(node, pc);
    118      1.10    tsubai 			return;
    119      1.10    tsubai 		}
    120      1.10    tsubai 		if ((nn = OF_child(node)) != 0)
    121      1.10    tsubai 			continue;
    122      1.10    tsubai 		while ((nn = OF_peer(node)) == 0) {
    123      1.10    tsubai 			node = OF_parent(node);
    124      1.10    tsubai 			if (node == pc->node)
    125      1.10    tsubai 				return;		/* not found */
    126      1.10    tsubai 		}
    127      1.10    tsubai 	}
    128       1.1    tsubai }
    129       1.1    tsubai 
    130       1.1    tsubai int
    131       1.1    tsubai pci_bus_maxdevs(pc, busno)
    132       1.1    tsubai 	pci_chipset_tag_t pc;
    133       1.1    tsubai 	int busno;
    134       1.1    tsubai {
    135       1.1    tsubai 
    136       1.1    tsubai 	/*
    137       1.1    tsubai 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
    138       1.1    tsubai 	 * use, can have devices 0-32 (i.e. the `normal' range).
    139       1.1    tsubai 	 */
    140       1.3    tsubai 	return 32;
    141       1.1    tsubai }
    142       1.1    tsubai 
    143       1.1    tsubai pcitag_t
    144       1.1    tsubai pci_make_tag(pc, bus, device, function)
    145       1.1    tsubai 	pci_chipset_tag_t pc;
    146       1.1    tsubai 	int bus, device, function;
    147       1.1    tsubai {
    148       1.1    tsubai 	pcitag_t tag;
    149       1.1    tsubai 
    150       1.1    tsubai 	if (bus >= 256 || device >= 32 || function >= 8)
    151       1.1    tsubai 		panic("pci_make_tag: bad request");
    152       1.1    tsubai 
    153       1.9   thorpej 	/* XXX magic number */
    154       1.3    tsubai 	tag = 0x80000000 | (bus << 16) | (device << 11) | (function << 8);
    155       1.1    tsubai 
    156       1.1    tsubai 	return tag;
    157       1.1    tsubai }
    158       1.1    tsubai 
    159       1.1    tsubai void
    160       1.1    tsubai pci_decompose_tag(pc, tag, bp, dp, fp)
    161       1.1    tsubai 	pci_chipset_tag_t pc;
    162       1.1    tsubai 	pcitag_t tag;
    163       1.1    tsubai 	int *bp, *dp, *fp;
    164       1.1    tsubai {
    165       1.9   thorpej 
    166       1.3    tsubai 	if (bp != NULL)
    167       1.3    tsubai 		*bp = (tag >> 16) & 0xff;
    168       1.3    tsubai 	if (dp != NULL)
    169       1.3    tsubai 		*dp = (tag >> 11) & 0x1f;
    170       1.3    tsubai 	if (fp != NULL)
    171       1.9   thorpej 		*fp = (tag >> 8) & 0x07;
    172       1.1    tsubai }
    173       1.1    tsubai 
    174       1.1    tsubai pcireg_t
    175       1.1    tsubai pci_conf_read(pc, tag, reg)
    176       1.1    tsubai 	pci_chipset_tag_t pc;
    177       1.1    tsubai 	pcitag_t tag;
    178       1.1    tsubai 	int reg;
    179       1.1    tsubai {
    180       1.2    tsubai 
    181      1.10    tsubai 	return (*pc->conf_read)(pc, tag, reg);
    182       1.1    tsubai }
    183       1.1    tsubai 
    184       1.1    tsubai void
    185       1.1    tsubai pci_conf_write(pc, tag, reg, data)
    186       1.1    tsubai 	pci_chipset_tag_t pc;
    187       1.1    tsubai 	pcitag_t tag;
    188       1.1    tsubai 	int reg;
    189       1.1    tsubai 	pcireg_t data;
    190       1.1    tsubai {
    191       1.2    tsubai 
    192      1.10    tsubai 	(*pc->conf_write)(pc, tag, reg, data);
    193       1.1    tsubai }
    194       1.1    tsubai 
    195       1.1    tsubai int
    196      1.14  sommerfe pci_intr_map(pa, ihp)
    197      1.14  sommerfe 	struct pci_attach_args *pa;
    198       1.1    tsubai 	pci_intr_handle_t *ihp;
    199       1.1    tsubai {
    200      1.14  sommerfe 	int pin = pa->pa_intrpin;
    201      1.14  sommerfe 	int line = pa->pa_intrline;
    202  1.28.2.1      yamt 
    203  1.28.2.1      yamt #if DEBUG
    204  1.28.2.1      yamt 	printf("%s: pin: %d, line: %d\n", __FUNCTION__, pin, line);
    205  1.28.2.1      yamt #endif
    206       1.1    tsubai 
    207       1.1    tsubai 	if (pin == 0) {
    208       1.1    tsubai 		/* No IRQ used. */
    209  1.28.2.2      yamt 		aprint_error("pci_intr_map: interrupt pin %d\n", pin);
    210       1.1    tsubai 		goto bad;
    211       1.1    tsubai 	}
    212       1.1    tsubai 
    213       1.1    tsubai 	if (pin > 4) {
    214  1.28.2.2      yamt 		aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
    215       1.1    tsubai 		goto bad;
    216       1.1    tsubai 	}
    217       1.1    tsubai 
    218       1.1    tsubai 	/*
    219       1.1    tsubai 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
    220       1.1    tsubai 	 * `unknown' or `no connection' on a PC.  We assume that a device with
    221       1.1    tsubai 	 * `no connection' either doesn't have an interrupt (in which case the
    222       1.1    tsubai 	 * pin number should be 0, and would have been noticed above), or
    223       1.1    tsubai 	 * wasn't configured by the BIOS (in which case we punt, since there's
    224       1.1    tsubai 	 * no real way we can know how the interrupt lines are mapped in the
    225       1.1    tsubai 	 * hardware).
    226       1.1    tsubai 	 *
    227       1.1    tsubai 	 * XXX
    228       1.1    tsubai 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
    229       1.1    tsubai 	 * that the BIOS did its job, we also recognize that as meaning that
    230       1.1    tsubai 	 * the BIOS has not configured the device.
    231       1.1    tsubai 	 */
    232       1.1    tsubai 	if (line == 0 || line == 255) {
    233  1.28.2.2      yamt 		aprint_error("pci_intr_map: no mapping for pin %c\n", '@' + pin);
    234       1.1    tsubai 		goto bad;
    235       1.1    tsubai 	} else {
    236       1.1    tsubai 		if (line >= ICU_LEN) {
    237  1.28.2.2      yamt 			aprint_error("pci_intr_map: bad interrupt line %d\n", line);
    238       1.1    tsubai 			goto bad;
    239       1.1    tsubai 		}
    240       1.1    tsubai 	}
    241       1.1    tsubai 
    242       1.1    tsubai 	*ihp = line;
    243       1.1    tsubai 	return 0;
    244       1.1    tsubai 
    245       1.1    tsubai bad:
    246       1.1    tsubai 	*ihp = -1;
    247       1.1    tsubai 	return 1;
    248       1.1    tsubai }
    249       1.1    tsubai 
    250       1.1    tsubai const char *
    251       1.1    tsubai pci_intr_string(pc, ih)
    252       1.1    tsubai 	pci_chipset_tag_t pc;
    253       1.1    tsubai 	pci_intr_handle_t ih;
    254       1.1    tsubai {
    255       1.1    tsubai 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    256       1.1    tsubai 
    257       1.1    tsubai 	if (ih == 0 || ih >= ICU_LEN)
    258      1.20    provos 		panic("pci_intr_string: bogus handle 0x%x", ih);
    259       1.1    tsubai 
    260       1.1    tsubai 	sprintf(irqstr, "irq %d", ih);
    261       1.1    tsubai 	return (irqstr);
    262       1.1    tsubai 
    263      1.11       cgd }
    264      1.11       cgd 
    265      1.11       cgd const struct evcnt *
    266      1.11       cgd pci_intr_evcnt(pc, ih)
    267      1.11       cgd 	pci_chipset_tag_t pc;
    268      1.11       cgd 	pci_intr_handle_t ih;
    269      1.11       cgd {
    270      1.11       cgd 
    271      1.11       cgd 	/* XXX for now, no evcnt parent reported */
    272      1.11       cgd 	return NULL;
    273       1.1    tsubai }
    274       1.1    tsubai 
    275       1.1    tsubai void *
    276       1.1    tsubai pci_intr_establish(pc, ih, level, func, arg)
    277       1.1    tsubai 	pci_chipset_tag_t pc;
    278       1.1    tsubai 	pci_intr_handle_t ih;
    279       1.1    tsubai 	int level, (*func) __P((void *));
    280       1.1    tsubai 	void *arg;
    281       1.1    tsubai {
    282       1.1    tsubai 
    283       1.1    tsubai 	if (ih == 0 || ih >= ICU_LEN)
    284      1.20    provos 		panic("pci_intr_establish: bogus handle 0x%x", ih);
    285       1.1    tsubai 
    286       1.1    tsubai 	return intr_establish(ih, IST_LEVEL, level, func, arg);
    287       1.1    tsubai }
    288       1.1    tsubai 
    289       1.1    tsubai void
    290       1.1    tsubai pci_intr_disestablish(pc, cookie)
    291       1.1    tsubai 	pci_chipset_tag_t pc;
    292       1.1    tsubai 	void *cookie;
    293       1.1    tsubai {
    294       1.1    tsubai 
    295       1.1    tsubai 	intr_disestablish(cookie);
    296      1.10    tsubai }
    297      1.10    tsubai 
    298      1.10    tsubai #define pcibus(x) \
    299      1.10    tsubai 	(((x) & OFW_PCI_PHYS_HI_BUSMASK) >> OFW_PCI_PHYS_HI_BUSSHIFT)
    300      1.10    tsubai #define pcidev(x) \
    301      1.10    tsubai 	(((x) & OFW_PCI_PHYS_HI_DEVICEMASK) >> OFW_PCI_PHYS_HI_DEVICESHIFT)
    302      1.10    tsubai #define pcifunc(x) \
    303      1.10    tsubai 	(((x) & OFW_PCI_PHYS_HI_FUNCTIONMASK) >> OFW_PCI_PHYS_HI_FUNCTIONSHIFT)
    304      1.10    tsubai 
    305      1.10    tsubai void
    306      1.10    tsubai fixpci(parent, pc)
    307      1.10    tsubai 	int parent;
    308      1.10    tsubai 	pci_chipset_tag_t pc;
    309      1.10    tsubai {
    310      1.10    tsubai 	int node;
    311      1.10    tsubai 	pcitag_t tag;
    312  1.28.2.2      yamt 	pcireg_t csr, intr, id, cr;
    313      1.22      matt 	int len, i, ilen;
    314      1.10    tsubai 	int32_t irqs[4];
    315      1.10    tsubai 	struct {
    316      1.10    tsubai 		u_int32_t phys_hi, phys_mid, phys_lo;
    317      1.10    tsubai 		u_int32_t size_hi, size_lo;
    318      1.10    tsubai 	} addr[8];
    319      1.22      matt 	struct {
    320      1.22      matt 		u_int32_t phys_hi, phys_mid, phys_lo;
    321      1.22      matt 		u_int32_t icells[5];
    322      1.22      matt 	} iaddr;
    323      1.10    tsubai 
    324  1.28.2.2      yamt 	/*
    325  1.28.2.2      yamt 	 * first hack - here we make the Ethernet portion of a
    326  1.28.2.2      yamt 	 * UMAX E100 card work
    327  1.28.2.2      yamt 	 */
    328  1.28.2.2      yamt #ifdef UMAX_E100_HACK
    329  1.28.2.2      yamt 	tag = pci_make_tag(pc, 0, 17, 0);
    330  1.28.2.2      yamt 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    331  1.28.2.2      yamt 	if ((PCI_VENDOR(id) == PCI_VENDOR_DEC) &&
    332  1.28.2.2      yamt 	    (PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21140)) {
    333  1.28.2.2      yamt 		/* this could be one */
    334  1.28.2.2      yamt 		pcireg_t isp, reg;
    335  1.28.2.2      yamt 		pcitag_t tag_isp = pci_make_tag(pc, 0, 13, 0);
    336  1.28.2.2      yamt 		/*
    337  1.28.2.2      yamt 		 * here we go. We shouldn't encounter this anywhere else
    338  1.28.2.2      yamt 		 * than on a UMAX S900 with an E100 board
    339  1.28.2.2      yamt 		 * look at 00:0d:00 for a Qlogic ISP 1020 to
    340  1.28.2.2      yamt 		 * make sure we really have an E100 here
    341  1.28.2.2      yamt 		 */
    342  1.28.2.2      yamt 		printf("\nfound E100 candidate tlp");
    343  1.28.2.2      yamt 		isp = pci_conf_read(pc, tag_isp, PCI_ID_REG);
    344  1.28.2.2      yamt 		if ((PCI_VENDOR(isp) == PCI_VENDOR_QLOGIC) &&
    345  1.28.2.2      yamt 		    (PCI_PRODUCT(isp) == PCI_PRODUCT_QLOGIC_ISP1020)) {
    346  1.28.2.2      yamt 
    347  1.28.2.2      yamt 			aprint_verbose("\nenabling UMAX E100 ethernet");
    348  1.28.2.2      yamt 
    349  1.28.2.2      yamt 			pci_conf_write(pc, tag, 0x14, 0x80000000);
    350  1.28.2.2      yamt 
    351  1.28.2.2      yamt 			/* now enable MMIO and busmastering */
    352  1.28.2.2      yamt 			reg = pci_conf_read(pc, tag,
    353  1.28.2.2      yamt 			    PCI_COMMAND_STATUS_REG);
    354  1.28.2.2      yamt 			reg |= PCI_COMMAND_MEM_ENABLE |
    355  1.28.2.2      yamt 			       PCI_COMMAND_MASTER_ENABLE;
    356  1.28.2.2      yamt 			pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
    357  1.28.2.2      yamt 			    reg);
    358  1.28.2.2      yamt 
    359  1.28.2.2      yamt 			/* and finally the interrupt */
    360  1.28.2.2      yamt 			reg = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    361  1.28.2.2      yamt 			reg &= ~PCI_INTERRUPT_LINE_MASK;
    362  1.28.2.2      yamt 			reg |= 23;
    363  1.28.2.2      yamt 			pci_conf_write(pc, tag, PCI_INTERRUPT_REG, reg);
    364  1.28.2.2      yamt 		}
    365  1.28.2.2      yamt 	}
    366  1.28.2.2      yamt #endif
    367  1.28.2.2      yamt 
    368      1.22      matt 	len = OF_getprop(parent, "#interrupt-cells", &ilen, sizeof(ilen));
    369      1.22      matt 	if (len < 0)
    370      1.22      matt 		ilen = 0;
    371      1.10    tsubai 	for (node = OF_child(parent); node; node = OF_peer(node)) {
    372      1.10    tsubai 		len = OF_getprop(node, "assigned-addresses", addr,
    373      1.10    tsubai 				 sizeof(addr));
    374      1.10    tsubai 		if (len < (int)sizeof(addr[0]))
    375      1.10    tsubai 			continue;
    376      1.10    tsubai 
    377      1.10    tsubai 		tag = pci_make_tag(pc, pcibus(addr[0].phys_hi),
    378      1.10    tsubai 				   pcidev(addr[0].phys_hi),
    379      1.10    tsubai 				   pcifunc(addr[0].phys_hi));
    380      1.10    tsubai 
    381      1.10    tsubai 		/*
    382      1.10    tsubai 		 * Make sure the IO and MEM enable bits are set in the CSR.
    383      1.10    tsubai 		 */
    384      1.10    tsubai 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    385      1.10    tsubai 		csr &= ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
    386      1.10    tsubai 
    387      1.10    tsubai 		for (i = 0; i < len / sizeof(addr[0]); i++) {
    388      1.10    tsubai 			switch (addr[i].phys_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
    389      1.10    tsubai 			case OFW_PCI_PHYS_HI_SPACE_IO:
    390      1.10    tsubai 				csr |= PCI_COMMAND_IO_ENABLE;
    391      1.10    tsubai 				break;
    392      1.10    tsubai 
    393      1.10    tsubai 			case OFW_PCI_PHYS_HI_SPACE_MEM32:
    394      1.19  wrstuden 			case OFW_PCI_PHYS_HI_SPACE_MEM64:
    395      1.10    tsubai 				csr |= PCI_COMMAND_MEM_ENABLE;
    396      1.10    tsubai 				break;
    397      1.10    tsubai 			}
    398      1.10    tsubai 		}
    399      1.10    tsubai 
    400      1.10    tsubai 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    401      1.10    tsubai 
    402      1.10    tsubai 		/*
    403      1.10    tsubai 		 * Make sure the line register is programmed with the
    404      1.10    tsubai 		 * interrupt mapping.
    405      1.10    tsubai 		 */
    406      1.23      matt 		if (ilen == 0) {
    407      1.23      matt 			/*
    408      1.23      matt 			 * Early Apple OFW implementation don't handle
    409      1.23      matt 			 * interrupts as defined by the OFW PCI bindings.
    410      1.23      matt 			 */
    411      1.23      matt 			len = OF_getprop(node, "AAPL,interrupts", irqs, 4);
    412      1.23      matt 		} else {
    413      1.23      matt 			iaddr.phys_hi = addr[0].phys_hi;
    414      1.23      matt 			iaddr.phys_mid = addr[0].phys_mid;
    415      1.23      matt 			iaddr.phys_lo = addr[0].phys_lo;
    416      1.23      matt 			/*
    417      1.23      matt 			 * Thankfully, PCI can only have one entry in its
    418      1.23      matt 			 * "interrupts" property.
    419      1.23      matt 			 */
    420      1.23      matt 			len = OF_getprop(node, "interrupts", &iaddr.icells[0],
    421      1.23      matt 			    4*ilen);
    422      1.23      matt 			if (len != 4*ilen)
    423      1.23      matt 				continue;
    424      1.23      matt 			len = find_node_intr(node, &iaddr.phys_hi, irqs);
    425      1.23      matt 		}
    426      1.27    briggs 		if (len <= 0) {
    427      1.27    briggs 			/*
    428      1.27    briggs 			 * If we still don't have an interrupt, try one
    429      1.27    briggs 			 * more time.  This case covers devices behind the
    430      1.27    briggs 			 * PCI-PCI bridge in a UMAX S900 or similar (9500?)
    431      1.27    briggs 			 * system.  These slots all share the bridge's
    432      1.27    briggs 			 * interrupt.
    433      1.27    briggs 			 */
    434      1.27    briggs 			len = find_node_intr(node, &addr[0].phys_hi, irqs);
    435      1.27    briggs 			if (len <= 0)
    436      1.27    briggs 				continue;
    437      1.22      matt 		}
    438  1.28.2.1      yamt 
    439  1.28.2.1      yamt 		/*
    440  1.28.2.1      yamt 		 * For PowerBook 2400, 3400 and original G3:
    441  1.28.2.1      yamt 		 * check if we have a 2nd ohare PIC - if so frob the built-in
    442  1.28.2.1      yamt 		 * tlp's IRQ to 60
    443  1.28.2.1      yamt 		 * first see if we have something on bus 0 device 13 and if
    444  1.28.2.1      yamt 		 * it's a DEC 21041
    445  1.28.2.1      yamt 		 */
    446  1.28.2.1      yamt 		id = pci_conf_read(pc, tag, PCI_ID_REG);
    447  1.28.2.1      yamt 		if ((tag == pci_make_tag(pc, 0, 13, 0)) &&
    448  1.28.2.1      yamt 		    (PCI_VENDOR(id) == PCI_VENDOR_DEC) &&
    449  1.28.2.1      yamt 		    (PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21041)) {
    450  1.28.2.1      yamt 
    451  1.28.2.1      yamt 			/* now look for the 2nd ohare */
    452  1.28.2.1      yamt 			if (OF_finddevice("/bandit/pci106b,7") != -1) {
    453  1.28.2.1      yamt 
    454  1.28.2.1      yamt 				irqs[0] = 60;
    455  1.28.2.2      yamt 				aprint_verbose("\nohare: frobbing tlp IRQ to 60");
    456  1.28.2.1      yamt 			}
    457  1.28.2.1      yamt 		}
    458  1.28.2.1      yamt 
    459      1.27    briggs 		intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    460      1.27    briggs 		intr &= ~PCI_INTERRUPT_LINE_MASK;
    461      1.27    briggs 		intr |= irqs[0] & PCI_INTERRUPT_LINE_MASK;
    462      1.27    briggs 		pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
    463  1.28.2.2      yamt 
    464  1.28.2.2      yamt 		/* fix secondary bus numbers on CardBus bridges */
    465  1.28.2.2      yamt 		cr = pci_conf_read(pc, tag, PCI_CLASS_REG);
    466  1.28.2.2      yamt 		if ((PCI_CLASS(cr) == PCI_CLASS_BRIDGE) &&
    467  1.28.2.2      yamt 		    (PCI_SUBCLASS(cr) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
    468  1.28.2.2      yamt 			uint32_t bi, busid;
    469  1.28.2.2      yamt 
    470  1.28.2.2      yamt 			/*
    471  1.28.2.2      yamt 			 * we found a CardBus bridge. Check if the bus number
    472  1.28.2.2      yamt 			 * is sane
    473  1.28.2.2      yamt 			 */
    474  1.28.2.2      yamt 			bi = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
    475  1.28.2.2      yamt 			busid = bi & 0xff;
    476  1.28.2.2      yamt 			if (busid == 0) {
    477  1.28.2.2      yamt 				fix_cardbus_bridge(node, pc, tag);
    478  1.28.2.2      yamt 			}
    479  1.28.2.2      yamt 		}
    480  1.28.2.2      yamt 	}
    481  1.28.2.2      yamt }
    482  1.28.2.2      yamt 
    483  1.28.2.2      yamt static void
    484  1.28.2.2      yamt fix_cardbus_bridge(int node, pci_chipset_tag_t pc, pcitag_t tag)
    485  1.28.2.2      yamt {
    486  1.28.2.2      yamt 	uint32_t bus_number = 0xffffffff;
    487  1.28.2.2      yamt 	pcireg_t bi;
    488  1.28.2.2      yamt 	int bus, dev, fn, ih, len;
    489  1.28.2.2      yamt 	char path[256];
    490  1.28.2.2      yamt 
    491  1.28.2.2      yamt #if PB3400_CARDBUS_HACK
    492  1.28.2.2      yamt 	int root_node;
    493  1.28.2.2      yamt 
    494  1.28.2.2      yamt 	root_node = OF_finddevice("/");
    495  1.28.2.2      yamt 	if (of_compatible(root_node, pb3400_compat) != -1) {
    496  1.28.2.2      yamt 
    497  1.28.2.2      yamt 		bus_number = cardbus_number;
    498  1.28.2.2      yamt 		cardbus_number++;
    499  1.28.2.2      yamt 	} else {
    500  1.28.2.2      yamt #endif
    501  1.28.2.2      yamt 		ih = OF_open(path);
    502  1.28.2.2      yamt 		OF_call_method("load-ata", ih, 0, 0);
    503  1.28.2.2      yamt 		OF_close(ih);
    504  1.28.2.2      yamt 
    505  1.28.2.2      yamt 		OF_getprop(node, "AAPL,bus-id", &bus_number,
    506  1.28.2.2      yamt 		    sizeof(bus_number));
    507  1.28.2.2      yamt #if PB3400_CARDBUS_HACK
    508  1.28.2.2      yamt 	}
    509  1.28.2.2      yamt #endif
    510  1.28.2.2      yamt 	if (bus_number != 0xffffffff) {
    511  1.28.2.2      yamt 
    512  1.28.2.2      yamt 		len = OF_package_to_path(node, path, sizeof(path));
    513  1.28.2.2      yamt 		path[len] = 0;
    514  1.28.2.2      yamt 		aprint_verbose("\n%s: fixing bus number to %d", path, bus_number);
    515  1.28.2.2      yamt 		pci_decompose_tag(pc, tag, &bus, &dev, &fn);
    516  1.28.2.2      yamt 		bi = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
    517  1.28.2.2      yamt 		bi &= 0xff000000;
    518  1.28.2.2      yamt 		/* XXX subordinate is always 32 here */
    519  1.28.2.2      yamt 		bi |= (bus & 0xff) | (bus_number << 8) | 0x200000;
    520  1.28.2.2      yamt 		pci_conf_write(pc, tag, PPB_REG_BUSINFO, bi);
    521      1.10    tsubai 	}
    522      1.10    tsubai }
    523      1.10    tsubai 
    524      1.10    tsubai /*
    525      1.10    tsubai  * Find PCI IRQ of the node from OF tree.
    526      1.10    tsubai  */
    527      1.10    tsubai int
    528      1.10    tsubai find_node_intr(node, addr, intr)
    529      1.10    tsubai 	int node;
    530      1.10    tsubai 	u_int32_t *addr, *intr;
    531      1.10    tsubai {
    532      1.10    tsubai 	int parent, len, mlen, iparent;
    533      1.10    tsubai 	int match, i;
    534      1.22      matt 	u_int32_t map[160];
    535      1.22      matt 	const u_int32_t *mp;
    536      1.28      matt 	u_int32_t imapmask[8], maskedaddr[8];
    537      1.22      matt 	u_int32_t acells, icells;
    538      1.10    tsubai 	char name[32];
    539      1.10    tsubai 
    540  1.28.2.1      yamt 	/* XXXSL: 1st check for a  interrupt-parent property */
    541  1.28.2.1      yamt         if (OF_getprop(node, "interrupt-parent", &iparent, sizeof(iparent)) == sizeof(iparent))
    542  1.28.2.1      yamt 	{
    543  1.28.2.1      yamt 		/* How many cells to specify an interrupt ?? */
    544  1.28.2.1      yamt 		if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
    545  1.28.2.1      yamt 			return -1;
    546  1.28.2.1      yamt 
    547  1.28.2.1      yamt 		if (OF_getprop(node, "interrupts", &map, sizeof(map)) != (icells * 4))
    548  1.28.2.1      yamt 			return -1;
    549  1.28.2.1      yamt 
    550  1.28.2.1      yamt 		memcpy(intr, map, icells * 4);
    551  1.28.2.1      yamt 		return (icells * 4);
    552  1.28.2.1      yamt 	}
    553  1.28.2.1      yamt 
    554      1.10    tsubai 	parent = OF_parent(node);
    555      1.10    tsubai 	len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
    556      1.28      matt 	mlen = OF_getprop(parent, "interrupt-map-mask", imapmask,
    557      1.28      matt 	    sizeof(imapmask));
    558      1.10    tsubai 
    559      1.22      matt 	if (mlen != -1)
    560      1.22      matt 		memcpy(maskedaddr, addr, mlen);
    561      1.22      matt again:
    562      1.10    tsubai 	if (len == -1 || mlen == -1)
    563      1.10    tsubai 		goto nomap;
    564      1.10    tsubai 
    565      1.10    tsubai #ifdef DIAGNOSTIC
    566      1.28      matt 	if (mlen == sizeof(imapmask)) {
    567  1.28.2.2      yamt 		aprint_error("interrupt-map too long\n");
    568      1.10    tsubai 		return -1;
    569      1.10    tsubai 	}
    570      1.10    tsubai #endif
    571      1.10    tsubai 
    572      1.10    tsubai 	/* mask addr by "interrupt-map-mask" */
    573      1.10    tsubai 	for (i = 0; i < mlen / 4; i++)
    574      1.28      matt 		maskedaddr[i] &= imapmask[i];
    575      1.10    tsubai 
    576      1.10    tsubai 	mp = map;
    577      1.22      matt 	i = 0;
    578      1.10    tsubai 	while (len > mlen) {
    579      1.18       wiz 		match = memcmp(maskedaddr, mp, mlen);
    580      1.10    tsubai 		mp += mlen / 4;
    581      1.10    tsubai 		len -= mlen;
    582      1.10    tsubai 
    583      1.10    tsubai 		/*
    584      1.22      matt 		 * We must read "#address-cells" and "#interrupt-cells" each
    585      1.22      matt 		 * time because each interrupt-parent may be different.
    586      1.10    tsubai 		 */
    587      1.10    tsubai 		iparent = *mp++;
    588      1.10    tsubai 		len -= 4;
    589      1.25      matt 		i = OF_getprop(iparent, "#address-cells", &acells, 4);
    590      1.25      matt 		if (i <= 0)
    591      1.25      matt 			acells = 0;
    592      1.25      matt 		else if (i != 4)
    593      1.22      matt 			return -1;
    594      1.10    tsubai 		if (OF_getprop(iparent, "#interrupt-cells", &icells, 4) != 4)
    595      1.22      matt 			return -1;
    596      1.10    tsubai 
    597      1.10    tsubai 		/* Found. */
    598      1.10    tsubai 		if (match == 0) {
    599      1.22      matt 			/*
    600      1.22      matt 			 * We matched on address/interrupt, but are we done?
    601      1.22      matt 			 */
    602      1.22      matt 			if (acells == 0) { /* XXX */
    603      1.22      matt 				/*
    604      1.22      matt 				 * If we are at the interrupt controller,
    605      1.22      matt 				 * we are finally done.  Save the result and
    606      1.22      matt 				 * return.
    607      1.22      matt 				 */
    608      1.22      matt 				memcpy(intr, mp, icells * 4);
    609      1.22      matt 				return icells * 4;
    610      1.22      matt 			}
    611      1.22      matt 			/*
    612      1.22      matt 			 * We are now at an intermedia interrupt node.  We
    613      1.22      matt 			 * need to use its interrupt mask and map the
    614      1.24       wiz 			 * supplied address/interrupt via its map.
    615      1.22      matt 			 */
    616      1.22      matt 			mlen = OF_getprop(iparent, "interrupt-map-mask",
    617      1.28      matt 			    imapmask, sizeof(imapmask));
    618      1.22      matt #ifdef DIAGNOSTIC
    619      1.22      matt 			if (mlen != (acells + icells)*4) {
    620  1.28.2.2      yamt 				aprint_error("interrupt-map inconsistent (%d, %d)\n",
    621      1.22      matt 				    mlen, (acells + icells)*4);
    622      1.22      matt 				return -1;
    623      1.22      matt 			}
    624      1.22      matt #endif
    625      1.22      matt 			memcpy(maskedaddr, mp, mlen);
    626      1.22      matt 			len = OF_getprop(iparent, "interrupt-map", map,
    627      1.22      matt 			    sizeof(map));
    628      1.22      matt 			goto again;
    629      1.10    tsubai 		}
    630      1.10    tsubai 
    631      1.22      matt 		mp += (acells + icells);
    632      1.22      matt 		len -= (acells + icells) * 4;
    633      1.10    tsubai 	}
    634      1.10    tsubai 
    635      1.10    tsubai nomap:
    636      1.10    tsubai 	/*
    637      1.10    tsubai 	 * If the node has no interrupt property and the parent is a
    638      1.10    tsubai 	 * pci-bridge, use parent's interrupt.  This occurs on a PCI
    639      1.10    tsubai 	 * slot.  (e.g. AHA-3940)
    640      1.10    tsubai 	 */
    641      1.18       wiz 	memset(name, 0, sizeof(name));
    642      1.10    tsubai 	OF_getprop(parent, "name", name, sizeof(name));
    643      1.10    tsubai 	if (strcmp(name, "pci-bridge") == 0) {
    644      1.10    tsubai 		len = OF_getprop(parent, "AAPL,interrupts", intr, 4) ;
    645      1.10    tsubai 		if (len == 4)
    646      1.10    tsubai 			return len;
    647      1.22      matt #if 0
    648      1.15    tsubai 		/*
    649      1.15    tsubai 		 * XXX I don't know what is the correct local address.
    650      1.15    tsubai 		 * XXX Use the first entry for now.
    651      1.15    tsubai 		 */
    652      1.15    tsubai 		len = OF_getprop(parent, "interrupt-map", map, sizeof(map));
    653      1.15    tsubai 		if (len >= 36) {
    654      1.15    tsubai 			addr = &map[5];
    655      1.15    tsubai 			return find_node_intr(parent, addr, intr);
    656      1.15    tsubai 		}
    657      1.22      matt #endif
    658      1.10    tsubai 	}
    659      1.10    tsubai 
    660      1.26    briggs 	/*
    661      1.26    briggs 	 * If all else fails, attempt to get AAPL, interrupts property.
    662      1.26    briggs 	 * Grackle, at least, uses this instead of above in some cases.
    663      1.26    briggs 	 */
    664      1.26    briggs 	len = OF_getprop(node, "AAPL,interrupts", intr, 4) ;
    665      1.10    tsubai 	if (len == 4)
    666      1.10    tsubai 		return len;
    667      1.10    tsubai 
    668      1.10    tsubai 	return -1;
    669       1.1    tsubai }
    670