Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.10
      1  1.10      matt /*	$NetBSD: pci_machdep.c,v 1.10 2001/06/06 17:42:31 matt Exp $	*/
      2   1.1  sakamoto 
      3   1.1  sakamoto /*
      4   1.1  sakamoto  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5   1.4   mycroft  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      6   1.1  sakamoto  *
      7   1.1  sakamoto  * Redistribution and use in source and binary forms, with or without
      8   1.1  sakamoto  * modification, are permitted provided that the following conditions
      9   1.1  sakamoto  * are met:
     10   1.1  sakamoto  * 1. Redistributions of source code must retain the above copyright
     11   1.1  sakamoto  *    notice, this list of conditions and the following disclaimer.
     12   1.1  sakamoto  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  sakamoto  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  sakamoto  *    documentation and/or other materials provided with the distribution.
     15   1.1  sakamoto  * 3. All advertising materials mentioning features or use of this software
     16   1.1  sakamoto  *    must display the following acknowledgement:
     17   1.4   mycroft  *	This product includes software developed by Charles M. Hannum.
     18   1.1  sakamoto  * 4. The name of the author may not be used to endorse or promote products
     19   1.1  sakamoto  *    derived from this software without specific prior written permission.
     20   1.1  sakamoto  *
     21   1.1  sakamoto  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1  sakamoto  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1  sakamoto  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1  sakamoto  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1  sakamoto  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1  sakamoto  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1  sakamoto  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1  sakamoto  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1  sakamoto  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1  sakamoto  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1  sakamoto  */
     32   1.1  sakamoto 
     33   1.1  sakamoto /*
     34   1.1  sakamoto  * Machine-specific functions for PCI autoconfiguration.
     35   1.1  sakamoto  *
     36   1.1  sakamoto  * On PCs, there are two methods of generating PCI configuration cycles.
     37   1.1  sakamoto  * We try to detect the appropriate mechanism for this machine and set
     38   1.1  sakamoto  * up a few function pointers to access the correct method directly.
     39   1.1  sakamoto  *
     40   1.1  sakamoto  * The configuration method can be hard-coded in the config file by
     41   1.1  sakamoto  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
     42   1.1  sakamoto  * as defined section 3.6.4.1, `Generating Configuration Cycles'.
     43   1.1  sakamoto  */
     44   1.1  sakamoto 
     45   1.1  sakamoto #include <sys/types.h>
     46   1.1  sakamoto #include <sys/param.h>
     47   1.9    briggs #include <sys/extent.h>
     48   1.1  sakamoto #include <sys/time.h>
     49   1.1  sakamoto #include <sys/systm.h>
     50   1.1  sakamoto #include <sys/errno.h>
     51   1.1  sakamoto #include <sys/device.h>
     52   1.1  sakamoto 
     53   1.7       mrg #include <uvm/uvm_extern.h>
     54   1.1  sakamoto 
     55  1.10      matt #define _POWERPC_BUS_DMA_PRIVATE
     56   1.2  sakamoto #include <machine/bus.h>
     57   1.1  sakamoto #include <machine/pio.h>
     58   1.1  sakamoto #include <machine/intr.h>
     59   1.1  sakamoto 
     60   1.1  sakamoto #include <dev/isa/isavar.h>
     61   1.1  sakamoto #include <dev/pci/pcivar.h>
     62   1.1  sakamoto #include <dev/pci/pcireg.h>
     63   1.9    briggs #include <dev/pci/pciconf.h>
     64   1.1  sakamoto 
     65   1.1  sakamoto #include <bebox/isa/icu.h>
     66   1.1  sakamoto 
     67  1.10      matt struct powerpc_bus_dma_tag pci_bus_dma_tag = {
     68   1.9    briggs 	0,			/* _bounce_thresh */
     69   1.9    briggs 	_bus_dmamap_create,
     70   1.9    briggs 	_bus_dmamap_destroy,
     71   1.9    briggs 	_bus_dmamap_load,
     72   1.9    briggs 	_bus_dmamap_load_mbuf,
     73   1.9    briggs 	_bus_dmamap_load_uio,
     74   1.9    briggs 	_bus_dmamap_load_raw,
     75   1.9    briggs 	_bus_dmamap_unload,
     76   1.9    briggs 	NULL,			/* _dmamap_sync */
     77   1.9    briggs 	_bus_dmamem_alloc,
     78   1.9    briggs 	_bus_dmamem_free,
     79   1.9    briggs 	_bus_dmamem_map,
     80   1.9    briggs 	_bus_dmamem_unmap,
     81   1.9    briggs 	_bus_dmamem_mmap,
     82   1.9    briggs };
     83   1.9    briggs 
     84   1.1  sakamoto #define	PCI_MODE1_ENABLE	0x80000000UL
     85   1.3  sakamoto #define	PCI_MODE1_ADDRESS_REG	(BEBOX_BUS_SPACE_IO + 0x0cf8)
     86   1.3  sakamoto #define	PCI_MODE1_DATA_REG	(BEBOX_BUS_SPACE_IO + 0x0cfc)
     87   1.1  sakamoto 
     88   1.1  sakamoto void
     89   1.1  sakamoto pci_attach_hook(parent, self, pba)
     90   1.1  sakamoto 	struct device *parent, *self;
     91   1.1  sakamoto 	struct pcibus_attach_args *pba;
     92   1.1  sakamoto {
     93   1.1  sakamoto }
     94   1.1  sakamoto 
     95   1.1  sakamoto int
     96   1.1  sakamoto pci_bus_maxdevs(pc, busno)
     97   1.1  sakamoto 	pci_chipset_tag_t pc;
     98   1.1  sakamoto 	int busno;
     99   1.1  sakamoto {
    100   1.1  sakamoto 
    101   1.1  sakamoto 	/*
    102   1.1  sakamoto 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
    103   1.1  sakamoto 	 * use, can have devices 0-32 (i.e. the `normal' range).
    104   1.1  sakamoto 	 */
    105   1.1  sakamoto 	return (32);
    106   1.1  sakamoto }
    107   1.1  sakamoto 
    108   1.1  sakamoto pcitag_t
    109   1.1  sakamoto pci_make_tag(pc, bus, device, function)
    110   1.1  sakamoto 	pci_chipset_tag_t pc;
    111   1.1  sakamoto 	int bus, device, function;
    112   1.1  sakamoto {
    113   1.1  sakamoto 	pcitag_t tag;
    114   1.1  sakamoto 
    115   1.1  sakamoto 	if (bus >= 256 || device >= 32 || function >= 8)
    116   1.1  sakamoto 		panic("pci_make_tag: bad request");
    117   1.1  sakamoto 
    118   1.1  sakamoto 	tag = PCI_MODE1_ENABLE |
    119   1.1  sakamoto 		    (bus << 16) | (device << 11) | (function << 8);
    120   1.1  sakamoto 	return tag;
    121   1.1  sakamoto }
    122   1.1  sakamoto 
    123   1.1  sakamoto void
    124   1.1  sakamoto pci_decompose_tag(pc, tag, bp, dp, fp)
    125   1.1  sakamoto 	pci_chipset_tag_t pc;
    126   1.1  sakamoto 	pcitag_t tag;
    127   1.1  sakamoto 	int *bp, *dp, *fp;
    128   1.1  sakamoto {
    129   1.1  sakamoto 
    130   1.1  sakamoto 	if (bp != NULL)
    131   1.1  sakamoto 		*bp = (tag >> 16) & 0xff;
    132   1.1  sakamoto 	if (dp != NULL)
    133   1.1  sakamoto 		*dp = (tag >> 11) & 0x1f;
    134   1.1  sakamoto 	if (fp != NULL)
    135   1.1  sakamoto 		*fp = (tag >> 8) & 0x7;
    136   1.1  sakamoto 	return;
    137   1.1  sakamoto }
    138   1.1  sakamoto 
    139   1.1  sakamoto pcireg_t
    140   1.1  sakamoto pci_conf_read(pc, tag, reg)
    141   1.1  sakamoto 	pci_chipset_tag_t pc;
    142   1.1  sakamoto 	pcitag_t tag;
    143   1.1  sakamoto 	int reg;
    144   1.1  sakamoto {
    145   1.1  sakamoto 	pcireg_t data;
    146   1.1  sakamoto 
    147   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, tag | reg);
    148   1.2  sakamoto 	data = in32rb(PCI_MODE1_DATA_REG);
    149   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, 0);
    150   1.1  sakamoto 	return data;
    151   1.1  sakamoto }
    152   1.1  sakamoto 
    153   1.1  sakamoto void
    154   1.1  sakamoto pci_conf_write(pc, tag, reg, data)
    155   1.1  sakamoto 	pci_chipset_tag_t pc;
    156   1.1  sakamoto 	pcitag_t tag;
    157   1.1  sakamoto 	int reg;
    158   1.1  sakamoto 	pcireg_t data;
    159   1.1  sakamoto {
    160   1.1  sakamoto 
    161   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, tag | reg);
    162   1.2  sakamoto 	out32rb(PCI_MODE1_DATA_REG, data);
    163   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, 0);
    164   1.1  sakamoto }
    165   1.1  sakamoto 
    166   1.1  sakamoto int
    167   1.8  sommerfe pci_intr_map(pa, ihp)
    168   1.8  sommerfe 	struct pci_attach_args *pa;
    169   1.1  sakamoto 	pci_intr_handle_t *ihp;
    170   1.1  sakamoto {
    171   1.8  sommerfe 	int pin = pa->pa_intrpin;
    172   1.8  sommerfe 	int line = pa->pa_intrline;
    173   1.1  sakamoto 
    174   1.1  sakamoto 	if (pin == 0) {
    175   1.1  sakamoto 		/* No IRQ used. */
    176   1.1  sakamoto 		goto bad;
    177   1.1  sakamoto 	}
    178   1.1  sakamoto 
    179   1.1  sakamoto 	if (pin > 4) {
    180   1.1  sakamoto 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
    181   1.1  sakamoto 		goto bad;
    182   1.1  sakamoto 	}
    183   1.1  sakamoto 
    184   1.1  sakamoto 	/*
    185   1.1  sakamoto 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
    186   1.1  sakamoto 	 * `unknown' or `no connection' on a PC.  We assume that a device with
    187   1.1  sakamoto 	 * `no connection' either doesn't have an interrupt (in which case the
    188   1.1  sakamoto 	 * pin number should be 0, and would have been noticed above), or
    189   1.1  sakamoto 	 * wasn't configured by the BIOS (in which case we punt, since there's
    190   1.1  sakamoto 	 * no real way we can know how the interrupt lines are mapped in the
    191   1.1  sakamoto 	 * hardware).
    192   1.1  sakamoto 	 *
    193   1.1  sakamoto 	 * XXX
    194   1.1  sakamoto 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
    195   1.1  sakamoto 	 * that the BIOS did its job, we also recognize that as meaning that
    196   1.1  sakamoto 	 * the BIOS has not configured the device.
    197   1.1  sakamoto 	 */
    198   1.1  sakamoto 	if (line == 0 || line == 255) {
    199   1.1  sakamoto 		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
    200   1.1  sakamoto 		goto bad;
    201   1.1  sakamoto 	} else {
    202   1.1  sakamoto 		if (line >= ICU_LEN) {
    203   1.1  sakamoto 			printf("pci_intr_map: bad interrupt line %d\n", line);
    204   1.1  sakamoto 			goto bad;
    205   1.1  sakamoto 		}
    206   1.2  sakamoto 		if (line == IRQ_SLAVE) {
    207   1.1  sakamoto 			printf("pci_intr_map: changed line 2 to line 9\n");
    208   1.1  sakamoto 			line = 9;
    209   1.1  sakamoto 		}
    210   1.1  sakamoto 	}
    211   1.1  sakamoto 
    212   1.1  sakamoto 	*ihp = line;
    213   1.1  sakamoto 	return 0;
    214   1.1  sakamoto 
    215   1.1  sakamoto bad:
    216   1.1  sakamoto 	*ihp = -1;
    217   1.1  sakamoto 	return 1;
    218   1.1  sakamoto }
    219   1.1  sakamoto 
    220   1.1  sakamoto const char *
    221   1.1  sakamoto pci_intr_string(pc, ih)
    222   1.1  sakamoto 	pci_chipset_tag_t pc;
    223   1.1  sakamoto 	pci_intr_handle_t ih;
    224   1.1  sakamoto {
    225   1.1  sakamoto 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    226   1.1  sakamoto 
    227   1.2  sakamoto 	if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
    228   1.1  sakamoto 		panic("pci_intr_string: bogus handle 0x%x\n", ih);
    229   1.1  sakamoto 
    230   1.1  sakamoto 	sprintf(irqstr, "irq %d", ih);
    231   1.1  sakamoto 	return (irqstr);
    232   1.1  sakamoto 
    233   1.5       cgd }
    234   1.5       cgd 
    235   1.5       cgd const struct evcnt *
    236   1.5       cgd pci_intr_evcnt(pc, ih)
    237   1.5       cgd 	pci_chipset_tag_t pc;
    238   1.5       cgd 	pci_intr_handle_t ih;
    239   1.5       cgd {
    240   1.5       cgd 
    241   1.5       cgd 	/* XXX for now, no evcnt parent reported */
    242   1.5       cgd 	return NULL;
    243   1.1  sakamoto }
    244   1.1  sakamoto 
    245   1.1  sakamoto void *
    246   1.1  sakamoto pci_intr_establish(pc, ih, level, func, arg)
    247   1.1  sakamoto 	pci_chipset_tag_t pc;
    248   1.1  sakamoto 	pci_intr_handle_t ih;
    249   1.1  sakamoto 	int level, (*func) __P((void *));
    250   1.1  sakamoto 	void *arg;
    251   1.1  sakamoto {
    252   1.1  sakamoto 
    253   1.2  sakamoto 	if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
    254   1.1  sakamoto 		panic("pci_intr_establish: bogus handle 0x%x\n", ih);
    255   1.1  sakamoto 
    256   1.9    briggs 	return (void *)intr_establish(ih, IST_LEVEL, level, func, arg);
    257   1.1  sakamoto }
    258   1.1  sakamoto 
    259   1.1  sakamoto void
    260   1.1  sakamoto pci_intr_disestablish(pc, cookie)
    261   1.1  sakamoto 	pci_chipset_tag_t pc;
    262   1.1  sakamoto 	void *cookie;
    263   1.1  sakamoto {
    264   1.1  sakamoto 
    265   1.9    briggs 	intr_disestablish(cookie);
    266   1.9    briggs }
    267   1.9    briggs 
    268   1.9    briggs void
    269   1.9    briggs pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int func,
    270   1.9    briggs     int swiz, int *iline)
    271   1.9    briggs {
    272   1.9    briggs 	if (bus == 0) {
    273   1.9    briggs 		switch (dev) {
    274   1.9    briggs 		case 12: /*       SCSI is bit 10, mapped to IRQ 20 */
    275   1.9    briggs 		case 13: /* PCI slot 1 is bit 11, mapped to IRQ 21 */
    276   1.9    briggs 		case 14: /* PCI slot 2 is bit 12, mapped to IRQ 22 */
    277   1.9    briggs 		case 15: /* PCI slot 3 is bit 13, mapped to IRQ 23 */
    278   1.9    briggs 			*iline = dev + 8;
    279   1.9    briggs 		}
    280   1.9    briggs 	} else {
    281   1.9    briggs 		*iline = 20 + ((swiz + dev + 1) & 3);
    282   1.9    briggs 	}
    283   1.1  sakamoto }
    284