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