Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.7
      1 /*	$NetBSD: pci_machdep.c,v 1.7 1999/02/04 14:54:00 tsubai Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1994 Charles M. 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 M. 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 #define _MACPPC_BUS_DMA_PRIVATE
     56 #include <machine/bus.h>
     57 
     58 #include <machine/bus.h>
     59 #include <machine/pio.h>
     60 #include <machine/intr.h>
     61 
     62 #include <dev/pci/pcivar.h>
     63 #include <dev/pci/pcireg.h>
     64 
     65 /*
     66  * PCI doesn't have any special needs; just use the generic versions
     67  * of these functions.
     68  */
     69 struct macppc_bus_dma_tag pci_bus_dma_tag = {
     70 	0,			/* _bounce_thresh */
     71 	_bus_dmamap_create,
     72 	_bus_dmamap_destroy,
     73 	_bus_dmamap_load,
     74 	_bus_dmamap_load_mbuf,
     75 	_bus_dmamap_load_uio,
     76 	_bus_dmamap_load_raw,
     77 	_bus_dmamap_unload,
     78 	NULL,			/* _dmamap_sync */
     79 	_bus_dmamem_alloc,
     80 	_bus_dmamem_free,
     81 	_bus_dmamem_map,
     82 	_bus_dmamem_unmap,
     83 	_bus_dmamem_mmap,
     84 };
     85 
     86 void
     87 pci_attach_hook(parent, self, pba)
     88 	struct device *parent, *self;
     89 	struct pcibus_attach_args *pba;
     90 {
     91 }
     92 
     93 int
     94 pci_bus_maxdevs(pc, busno)
     95 	pci_chipset_tag_t pc;
     96 	int busno;
     97 {
     98 
     99 	/*
    100 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
    101 	 * use, can have devices 0-32 (i.e. the `normal' range).
    102 	 */
    103 	return 32;
    104 }
    105 
    106 pcitag_t
    107 pci_make_tag(pc, bus, device, function)
    108 	pci_chipset_tag_t pc;
    109 	int bus, device, function;
    110 {
    111 	pcitag_t tag;
    112 
    113 	if (bus >= 256 || device >= 32 || function >= 8)
    114 		panic("pci_make_tag: bad request");
    115 
    116 	tag = 0x80000000 | (bus << 16) | (device << 11) | (function << 8);
    117 
    118 	return tag;
    119 }
    120 
    121 void
    122 pci_decompose_tag(pc, tag, bp, dp, fp)
    123 	pci_chipset_tag_t pc;
    124 	pcitag_t tag;
    125 	int *bp, *dp, *fp;
    126 {
    127 	if (bp != NULL)
    128 		*bp = (tag >> 16) & 0xff;
    129 	if (dp != NULL)
    130 		*dp = (tag >> 11) & 0x1f;
    131 	if (fp != NULL)
    132 		*fp = (tag >> 8) & 0x7;
    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 pci_bridge *r;
    145 
    146 	if (pc == PCI_CHIPSET_MPC106) {
    147 		r = &pci_bridges[0];
    148 
    149 		out32rb(r->addr, tag | reg);
    150 		data = in32rb(r->data);
    151 		out32rb(r->addr, 0);
    152 	} else {
    153 		int bus, dev, func;
    154 
    155 		pci_decompose_tag(pc, tag, &bus, &dev, &func);
    156 
    157 		r = &pci_bridges[pc];
    158 
    159 		/*
    160 		 * bandit's minimum device number of the first bus is 11.
    161 		 * So we behave as if there is no device when dev < 11.
    162 		 */
    163 		if (func > 7)
    164 			panic("pci_conf_read: func > 7");
    165 
    166 		if (bus == r->bus) {
    167 			if (dev < 11) {
    168 				if (reg == PCI_ID_REG)
    169 					return 0xffffffff;
    170 				else
    171 					panic("pci_conf_read: dev < 11");
    172 			}
    173 			out32rb(r->addr, (1 << dev) | (func << 8) | reg);
    174 		} else
    175 			out32rb(r->addr, tag | reg | 1);
    176 		DELAY(10);
    177 		data = in32rb(r->data);
    178 		DELAY(10);
    179 		out32rb(r->addr, 0);
    180 		DELAY(10);
    181 	}
    182 
    183 	return data;
    184 }
    185 
    186 void
    187 pci_conf_write(pc, tag, reg, data)
    188 	pci_chipset_tag_t pc;
    189 	pcitag_t tag;
    190 	int reg;
    191 	pcireg_t data;
    192 {
    193 	struct pci_bridge *r = &pci_bridges[pc];
    194 
    195 	if (pc == PCI_CHIPSET_MPC106) {
    196 		r = &pci_bridges[0];
    197 
    198 		out32rb(r->addr, tag | reg);
    199 		out32rb(r->data, data);
    200 		out32rb(r->addr, 0);
    201 	} else {
    202 		int bus, dev, func;
    203 
    204 		pci_decompose_tag(pc, tag, &bus, &dev, &func);
    205 		r = &pci_bridges[pc];
    206 
    207 		if (func > 7)
    208 			panic("pci_conf_write: func > 7");
    209 
    210 		if (bus == r->bus) {
    211 			if (dev < 11)
    212 				panic("pci_conf_write: dev < 11");
    213 			out32rb(r->addr, (1 << dev) | (func << 8) | reg);
    214 		} else
    215 			out32rb(r->addr, tag | reg | 1);
    216 		DELAY(10);
    217 		out32rb(r->data, data);
    218 		DELAY(10);
    219 		out32rb(r->addr, 0);
    220 		DELAY(10);
    221 	}
    222 }
    223 
    224 int
    225 pci_intr_map(pc, intrtag, pin, line, ihp)
    226 	pci_chipset_tag_t pc;
    227 	pcitag_t intrtag;
    228 	int pin, line;
    229 	pci_intr_handle_t *ihp;
    230 {
    231 
    232 	if (pin == 0) {
    233 		/* No IRQ used. */
    234 		goto bad;
    235 	}
    236 
    237 	if (pin > 4) {
    238 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
    239 		goto bad;
    240 	}
    241 
    242 	/*
    243 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
    244 	 * `unknown' or `no connection' on a PC.  We assume that a device with
    245 	 * `no connection' either doesn't have an interrupt (in which case the
    246 	 * pin number should be 0, and would have been noticed above), or
    247 	 * wasn't configured by the BIOS (in which case we punt, since there's
    248 	 * no real way we can know how the interrupt lines are mapped in the
    249 	 * hardware).
    250 	 *
    251 	 * XXX
    252 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
    253 	 * that the BIOS did its job, we also recognize that as meaning that
    254 	 * the BIOS has not configured the device.
    255 	 */
    256 	if (line == 0 || line == 255) {
    257 		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
    258 		goto bad;
    259 	} else {
    260 		if (line >= ICU_LEN) {
    261 			printf("pci_intr_map: bad interrupt line %d\n", line);
    262 			goto bad;
    263 		}
    264 	}
    265 
    266 	*ihp = line;
    267 	return 0;
    268 
    269 bad:
    270 	*ihp = -1;
    271 	return 1;
    272 }
    273 
    274 const char *
    275 pci_intr_string(pc, ih)
    276 	pci_chipset_tag_t pc;
    277 	pci_intr_handle_t ih;
    278 {
    279 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    280 
    281 	if (ih == 0 || ih >= ICU_LEN)
    282 		panic("pci_intr_string: bogus handle 0x%x\n", ih);
    283 
    284 	sprintf(irqstr, "irq %d", ih);
    285 	return (irqstr);
    286 
    287 }
    288 
    289 extern void * intr_establish();
    290 extern void intr_disestablish();
    291 
    292 void *
    293 pci_intr_establish(pc, ih, level, func, arg)
    294 	pci_chipset_tag_t pc;
    295 	pci_intr_handle_t ih;
    296 	int level, (*func) __P((void *));
    297 	void *arg;
    298 {
    299 
    300 	if (ih == 0 || ih >= ICU_LEN)
    301 		panic("pci_intr_establish: bogus handle 0x%x\n", ih);
    302 
    303 	return intr_establish(ih, IST_LEVEL, level, func, arg);
    304 }
    305 
    306 void
    307 pci_intr_disestablish(pc, cookie)
    308 	pci_chipset_tag_t pc;
    309 	void *cookie;
    310 {
    311 
    312 	intr_disestablish(cookie);
    313 }
    314