Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.13
      1  1.13     lukem /*	$NetBSD: pci_machdep.c,v 1.13 2003/07/15 01:26:33 lukem 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.13     lukem 
     45  1.13     lukem #include <sys/cdefs.h>
     46  1.13     lukem __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.13 2003/07/15 01:26:33 lukem Exp $");
     47   1.1  sakamoto 
     48   1.1  sakamoto #include <sys/types.h>
     49   1.1  sakamoto #include <sys/param.h>
     50   1.9    briggs #include <sys/extent.h>
     51   1.1  sakamoto #include <sys/time.h>
     52   1.1  sakamoto #include <sys/systm.h>
     53   1.1  sakamoto #include <sys/errno.h>
     54   1.1  sakamoto #include <sys/device.h>
     55   1.1  sakamoto 
     56   1.7       mrg #include <uvm/uvm_extern.h>
     57   1.1  sakamoto 
     58  1.10      matt #define _POWERPC_BUS_DMA_PRIVATE
     59   1.2  sakamoto #include <machine/bus.h>
     60   1.1  sakamoto #include <machine/pio.h>
     61   1.1  sakamoto #include <machine/intr.h>
     62   1.1  sakamoto 
     63   1.1  sakamoto #include <dev/isa/isavar.h>
     64   1.1  sakamoto #include <dev/pci/pcivar.h>
     65   1.1  sakamoto #include <dev/pci/pcireg.h>
     66   1.9    briggs #include <dev/pci/pciconf.h>
     67   1.1  sakamoto 
     68   1.1  sakamoto #include <bebox/isa/icu.h>
     69   1.1  sakamoto 
     70  1.10      matt struct powerpc_bus_dma_tag pci_bus_dma_tag = {
     71   1.9    briggs 	0,			/* _bounce_thresh */
     72   1.9    briggs 	_bus_dmamap_create,
     73   1.9    briggs 	_bus_dmamap_destroy,
     74   1.9    briggs 	_bus_dmamap_load,
     75   1.9    briggs 	_bus_dmamap_load_mbuf,
     76   1.9    briggs 	_bus_dmamap_load_uio,
     77   1.9    briggs 	_bus_dmamap_load_raw,
     78   1.9    briggs 	_bus_dmamap_unload,
     79   1.9    briggs 	NULL,			/* _dmamap_sync */
     80   1.9    briggs 	_bus_dmamem_alloc,
     81   1.9    briggs 	_bus_dmamem_free,
     82   1.9    briggs 	_bus_dmamem_map,
     83   1.9    briggs 	_bus_dmamem_unmap,
     84   1.9    briggs 	_bus_dmamem_mmap,
     85   1.9    briggs };
     86   1.9    briggs 
     87   1.1  sakamoto #define	PCI_MODE1_ENABLE	0x80000000UL
     88   1.3  sakamoto #define	PCI_MODE1_ADDRESS_REG	(BEBOX_BUS_SPACE_IO + 0x0cf8)
     89   1.3  sakamoto #define	PCI_MODE1_DATA_REG	(BEBOX_BUS_SPACE_IO + 0x0cfc)
     90   1.1  sakamoto 
     91   1.1  sakamoto void
     92   1.1  sakamoto pci_attach_hook(parent, self, pba)
     93   1.1  sakamoto 	struct device *parent, *self;
     94   1.1  sakamoto 	struct pcibus_attach_args *pba;
     95   1.1  sakamoto {
     96   1.1  sakamoto }
     97   1.1  sakamoto 
     98   1.1  sakamoto int
     99   1.1  sakamoto pci_bus_maxdevs(pc, busno)
    100   1.1  sakamoto 	pci_chipset_tag_t pc;
    101   1.1  sakamoto 	int busno;
    102   1.1  sakamoto {
    103   1.1  sakamoto 
    104   1.1  sakamoto 	/*
    105   1.1  sakamoto 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
    106   1.1  sakamoto 	 * use, can have devices 0-32 (i.e. the `normal' range).
    107   1.1  sakamoto 	 */
    108   1.1  sakamoto 	return (32);
    109   1.1  sakamoto }
    110   1.1  sakamoto 
    111   1.1  sakamoto pcitag_t
    112   1.1  sakamoto pci_make_tag(pc, bus, device, function)
    113   1.1  sakamoto 	pci_chipset_tag_t pc;
    114   1.1  sakamoto 	int bus, device, function;
    115   1.1  sakamoto {
    116   1.1  sakamoto 	pcitag_t tag;
    117   1.1  sakamoto 
    118   1.1  sakamoto 	if (bus >= 256 || device >= 32 || function >= 8)
    119   1.1  sakamoto 		panic("pci_make_tag: bad request");
    120   1.1  sakamoto 
    121   1.1  sakamoto 	tag = PCI_MODE1_ENABLE |
    122   1.1  sakamoto 		    (bus << 16) | (device << 11) | (function << 8);
    123   1.1  sakamoto 	return tag;
    124   1.1  sakamoto }
    125   1.1  sakamoto 
    126   1.1  sakamoto void
    127   1.1  sakamoto pci_decompose_tag(pc, tag, bp, dp, fp)
    128   1.1  sakamoto 	pci_chipset_tag_t pc;
    129   1.1  sakamoto 	pcitag_t tag;
    130   1.1  sakamoto 	int *bp, *dp, *fp;
    131   1.1  sakamoto {
    132   1.1  sakamoto 
    133   1.1  sakamoto 	if (bp != NULL)
    134   1.1  sakamoto 		*bp = (tag >> 16) & 0xff;
    135   1.1  sakamoto 	if (dp != NULL)
    136   1.1  sakamoto 		*dp = (tag >> 11) & 0x1f;
    137   1.1  sakamoto 	if (fp != NULL)
    138   1.1  sakamoto 		*fp = (tag >> 8) & 0x7;
    139   1.1  sakamoto 	return;
    140   1.1  sakamoto }
    141   1.1  sakamoto 
    142   1.1  sakamoto pcireg_t
    143   1.1  sakamoto pci_conf_read(pc, tag, reg)
    144   1.1  sakamoto 	pci_chipset_tag_t pc;
    145   1.1  sakamoto 	pcitag_t tag;
    146   1.1  sakamoto 	int reg;
    147   1.1  sakamoto {
    148   1.1  sakamoto 	pcireg_t data;
    149   1.1  sakamoto 
    150   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, tag | reg);
    151   1.2  sakamoto 	data = in32rb(PCI_MODE1_DATA_REG);
    152   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, 0);
    153   1.1  sakamoto 	return data;
    154   1.1  sakamoto }
    155   1.1  sakamoto 
    156   1.1  sakamoto void
    157   1.1  sakamoto pci_conf_write(pc, tag, reg, data)
    158   1.1  sakamoto 	pci_chipset_tag_t pc;
    159   1.1  sakamoto 	pcitag_t tag;
    160   1.1  sakamoto 	int reg;
    161   1.1  sakamoto 	pcireg_t data;
    162   1.1  sakamoto {
    163   1.1  sakamoto 
    164   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, tag | reg);
    165   1.2  sakamoto 	out32rb(PCI_MODE1_DATA_REG, data);
    166   1.2  sakamoto 	out32rb(PCI_MODE1_ADDRESS_REG, 0);
    167   1.1  sakamoto }
    168   1.1  sakamoto 
    169   1.1  sakamoto int
    170   1.8  sommerfe pci_intr_map(pa, ihp)
    171   1.8  sommerfe 	struct pci_attach_args *pa;
    172   1.1  sakamoto 	pci_intr_handle_t *ihp;
    173   1.1  sakamoto {
    174   1.8  sommerfe 	int pin = pa->pa_intrpin;
    175   1.8  sommerfe 	int line = pa->pa_intrline;
    176   1.1  sakamoto 
    177   1.1  sakamoto 	if (pin == 0) {
    178   1.1  sakamoto 		/* No IRQ used. */
    179   1.1  sakamoto 		goto bad;
    180   1.1  sakamoto 	}
    181   1.1  sakamoto 
    182   1.1  sakamoto 	if (pin > 4) {
    183   1.1  sakamoto 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
    184   1.1  sakamoto 		goto bad;
    185   1.1  sakamoto 	}
    186   1.1  sakamoto 
    187   1.1  sakamoto 	/*
    188   1.1  sakamoto 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
    189   1.1  sakamoto 	 * `unknown' or `no connection' on a PC.  We assume that a device with
    190   1.1  sakamoto 	 * `no connection' either doesn't have an interrupt (in which case the
    191   1.1  sakamoto 	 * pin number should be 0, and would have been noticed above), or
    192   1.1  sakamoto 	 * wasn't configured by the BIOS (in which case we punt, since there's
    193   1.1  sakamoto 	 * no real way we can know how the interrupt lines are mapped in the
    194   1.1  sakamoto 	 * hardware).
    195   1.1  sakamoto 	 *
    196   1.1  sakamoto 	 * XXX
    197   1.1  sakamoto 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
    198   1.1  sakamoto 	 * that the BIOS did its job, we also recognize that as meaning that
    199   1.1  sakamoto 	 * the BIOS has not configured the device.
    200   1.1  sakamoto 	 */
    201   1.1  sakamoto 	if (line == 0 || line == 255) {
    202   1.1  sakamoto 		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
    203   1.1  sakamoto 		goto bad;
    204   1.1  sakamoto 	} else {
    205   1.1  sakamoto 		if (line >= ICU_LEN) {
    206   1.1  sakamoto 			printf("pci_intr_map: bad interrupt line %d\n", line);
    207   1.1  sakamoto 			goto bad;
    208   1.1  sakamoto 		}
    209   1.2  sakamoto 		if (line == IRQ_SLAVE) {
    210   1.1  sakamoto 			printf("pci_intr_map: changed line 2 to line 9\n");
    211   1.1  sakamoto 			line = 9;
    212   1.1  sakamoto 		}
    213   1.1  sakamoto 	}
    214   1.1  sakamoto 
    215   1.1  sakamoto 	*ihp = line;
    216   1.1  sakamoto 	return 0;
    217   1.1  sakamoto 
    218   1.1  sakamoto bad:
    219   1.1  sakamoto 	*ihp = -1;
    220   1.1  sakamoto 	return 1;
    221   1.1  sakamoto }
    222   1.1  sakamoto 
    223   1.1  sakamoto const char *
    224   1.1  sakamoto pci_intr_string(pc, ih)
    225   1.1  sakamoto 	pci_chipset_tag_t pc;
    226   1.1  sakamoto 	pci_intr_handle_t ih;
    227   1.1  sakamoto {
    228   1.1  sakamoto 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    229   1.1  sakamoto 
    230   1.2  sakamoto 	if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
    231  1.12    provos 		panic("pci_intr_string: bogus handle 0x%x", ih);
    232   1.1  sakamoto 
    233   1.1  sakamoto 	sprintf(irqstr, "irq %d", ih);
    234   1.1  sakamoto 	return (irqstr);
    235   1.1  sakamoto 
    236   1.5       cgd }
    237   1.5       cgd 
    238   1.5       cgd const struct evcnt *
    239   1.5       cgd pci_intr_evcnt(pc, ih)
    240   1.5       cgd 	pci_chipset_tag_t pc;
    241   1.5       cgd 	pci_intr_handle_t ih;
    242   1.5       cgd {
    243   1.5       cgd 
    244   1.5       cgd 	/* XXX for now, no evcnt parent reported */
    245   1.5       cgd 	return NULL;
    246   1.1  sakamoto }
    247   1.1  sakamoto 
    248   1.1  sakamoto void *
    249   1.1  sakamoto pci_intr_establish(pc, ih, level, func, arg)
    250   1.1  sakamoto 	pci_chipset_tag_t pc;
    251   1.1  sakamoto 	pci_intr_handle_t ih;
    252   1.1  sakamoto 	int level, (*func) __P((void *));
    253   1.1  sakamoto 	void *arg;
    254   1.1  sakamoto {
    255   1.1  sakamoto 
    256   1.2  sakamoto 	if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
    257  1.12    provos 		panic("pci_intr_establish: bogus handle 0x%x", ih);
    258   1.1  sakamoto 
    259   1.9    briggs 	return (void *)intr_establish(ih, IST_LEVEL, level, func, arg);
    260   1.1  sakamoto }
    261   1.1  sakamoto 
    262   1.1  sakamoto void
    263   1.1  sakamoto pci_intr_disestablish(pc, cookie)
    264   1.1  sakamoto 	pci_chipset_tag_t pc;
    265   1.1  sakamoto 	void *cookie;
    266   1.1  sakamoto {
    267   1.1  sakamoto 
    268   1.9    briggs 	intr_disestablish(cookie);
    269   1.9    briggs }
    270   1.9    briggs 
    271   1.9    briggs void
    272  1.11   thorpej pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin,
    273   1.9    briggs     int swiz, int *iline)
    274   1.9    briggs {
    275   1.9    briggs 	if (bus == 0) {
    276   1.9    briggs 		switch (dev) {
    277   1.9    briggs 		case 12: /*       SCSI is bit 10, mapped to IRQ 20 */
    278   1.9    briggs 		case 13: /* PCI slot 1 is bit 11, mapped to IRQ 21 */
    279   1.9    briggs 		case 14: /* PCI slot 2 is bit 12, mapped to IRQ 22 */
    280   1.9    briggs 		case 15: /* PCI slot 3 is bit 13, mapped to IRQ 23 */
    281   1.9    briggs 			*iline = dev + 8;
    282   1.9    briggs 		}
    283   1.9    briggs 	} else {
    284   1.9    briggs 		*iline = 20 + ((swiz + dev + 1) & 3);
    285   1.9    briggs 	}
    286   1.1  sakamoto }
    287