Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.12.38.1
      1  1.12.38.1  nisimura /*	$NetBSD: pci_machdep.c,v 1.12.38.1 2007/05/04 11:03:24 nisimura Exp $	*/
      2        1.1    briggs 
      3        1.1    briggs /*
      4        1.1    briggs  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5        1.1    briggs  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      6        1.1    briggs  *
      7        1.1    briggs  * Redistribution and use in source and binary forms, with or without
      8        1.1    briggs  * modification, are permitted provided that the following conditions
      9        1.1    briggs  * are met:
     10        1.1    briggs  * 1. Redistributions of source code must retain the above copyright
     11        1.1    briggs  *    notice, this list of conditions and the following disclaimer.
     12        1.1    briggs  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1    briggs  *    notice, this list of conditions and the following disclaimer in the
     14        1.1    briggs  *    documentation and/or other materials provided with the distribution.
     15        1.1    briggs  * 3. All advertising materials mentioning features or use of this software
     16        1.1    briggs  *    must display the following acknowledgement:
     17        1.1    briggs  *	This product includes software developed by Charles M. Hannum.
     18        1.1    briggs  * 4. The name of the author may not be used to endorse or promote products
     19        1.1    briggs  *    derived from this software without specific prior written permission.
     20        1.1    briggs  *
     21        1.1    briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22        1.1    briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23        1.1    briggs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24        1.1    briggs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25        1.1    briggs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26        1.1    briggs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27        1.1    briggs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28        1.1    briggs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29        1.1    briggs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30        1.1    briggs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31        1.1    briggs  */
     32        1.1    briggs 
     33        1.1    briggs /*
     34        1.1    briggs  * Machine-specific functions for PCI autoconfiguration.
     35        1.1    briggs  *
     36        1.1    briggs  * On PCs, there are two methods of generating PCI configuration cycles.
     37        1.1    briggs  * We try to detect the appropriate mechanism for this machine and set
     38        1.1    briggs  * up a few function pointers to access the correct method directly.
     39        1.1    briggs  *
     40        1.1    briggs  * The configuration method can be hard-coded in the config file by
     41        1.1    briggs  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
     42        1.1    briggs  * as defined section 3.6.4.1, `Generating Configuration Cycles'.
     43        1.1    briggs  */
     44       1.11     lukem 
     45       1.11     lukem #include <sys/cdefs.h>
     46  1.12.38.1  nisimura __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.12.38.1 2007/05/04 11:03:24 nisimura Exp $");
     47       1.11     lukem 
     48        1.6    briggs #include "opt_openpic.h"
     49        1.1    briggs 
     50        1.1    briggs #include <sys/types.h>
     51        1.1    briggs #include <sys/param.h>
     52        1.1    briggs #include <sys/device.h>
     53        1.1    briggs #include <sys/errno.h>
     54        1.1    briggs #include <sys/extent.h>
     55        1.1    briggs #include <sys/malloc.h>
     56        1.1    briggs #include <sys/queue.h>
     57        1.1    briggs #include <sys/systm.h>
     58        1.1    briggs #include <sys/time.h>
     59        1.1    briggs 
     60        1.1    briggs #include <uvm/uvm.h>
     61        1.1    briggs 
     62        1.4    briggs #define _POWERPC_BUS_DMA_PRIVATE
     63        1.1    briggs #include <machine/bus.h>
     64        1.1    briggs #include <machine/pio.h>
     65        1.1    briggs #include <machine/intr.h>
     66        1.8    briggs #include <machine/openpicreg.h>
     67        1.1    briggs 
     68        1.1    briggs #include <dev/isa/isavar.h>
     69        1.1    briggs #include <dev/pci/pcivar.h>
     70        1.1    briggs #include <dev/pci/pcireg.h>
     71        1.1    briggs #include <dev/pci/pciconf.h>
     72        1.1    briggs 
     73        1.4    briggs struct powerpc_bus_dma_tag pci_bus_dma_tag = {
     74        1.1    briggs 	0,			/* _bounce_thresh */
     75        1.1    briggs 	_bus_dmamap_create,
     76        1.1    briggs 	_bus_dmamap_destroy,
     77        1.1    briggs 	_bus_dmamap_load,
     78        1.1    briggs 	_bus_dmamap_load_mbuf,
     79        1.1    briggs 	_bus_dmamap_load_uio,
     80        1.1    briggs 	_bus_dmamap_load_raw,
     81        1.1    briggs 	_bus_dmamap_unload,
     82        1.1    briggs 	NULL,			/* _dmamap_sync */
     83        1.1    briggs 	_bus_dmamem_alloc,
     84        1.1    briggs 	_bus_dmamem_free,
     85        1.1    briggs 	_bus_dmamem_map,
     86        1.1    briggs 	_bus_dmamem_unmap,
     87        1.1    briggs 	_bus_dmamem_mmap,
     88        1.1    briggs };
     89        1.1    briggs 
     90        1.1    briggs #define	PCI_CONFIG_ENABLE	0x80000000UL
     91        1.1    briggs 
     92        1.1    briggs void
     93        1.1    briggs pci_attach_hook(parent, self, pba)
     94        1.1    briggs 	struct device *parent, *self;
     95        1.1    briggs 	struct pcibus_attach_args *pba;
     96        1.1    briggs {
     97        1.1    briggs }
     98        1.1    briggs 
     99        1.1    briggs int
    100        1.1    briggs pci_bus_maxdevs(pc, busno)
    101        1.1    briggs 	pci_chipset_tag_t pc;
    102        1.1    briggs 	int busno;
    103        1.1    briggs {
    104        1.1    briggs 
    105        1.1    briggs 	/*
    106        1.1    briggs 	 * Bus number is irrelevant.  Configuration Mechanism 1 is in
    107        1.1    briggs 	 * use, can have devices 0-32 (i.e. the `normal' range).
    108        1.1    briggs 	 */
    109        1.1    briggs 	return (32);
    110        1.1    briggs }
    111        1.1    briggs 
    112        1.1    briggs pcitag_t
    113        1.1    briggs pci_make_tag(pc, bus, device, function)
    114        1.1    briggs 	pci_chipset_tag_t pc;
    115        1.1    briggs 	int bus, device, function;
    116        1.1    briggs {
    117        1.1    briggs 	pcitag_t tag;
    118        1.1    briggs 
    119        1.1    briggs 	if (bus >= 256 || device >= 32 || function >= 8)
    120        1.1    briggs 		panic("pci_make_tag: bad request");
    121        1.1    briggs 
    122        1.1    briggs 	tag = PCI_CONFIG_ENABLE |
    123        1.1    briggs 		    (bus << 16) | (device << 11) | (function << 8);
    124        1.1    briggs 	return tag;
    125        1.1    briggs }
    126        1.1    briggs 
    127        1.1    briggs void
    128        1.1    briggs pci_decompose_tag(pc, tag, bp, dp, fp)
    129        1.1    briggs 	pci_chipset_tag_t pc;
    130        1.1    briggs 	pcitag_t tag;
    131        1.1    briggs 	int *bp, *dp, *fp;
    132        1.1    briggs {
    133        1.1    briggs 
    134        1.1    briggs 	if (bp != NULL)
    135        1.1    briggs 		*bp = (tag >> 16) & 0xff;
    136        1.1    briggs 	if (dp != NULL)
    137        1.1    briggs 		*dp = (tag >> 11) & 0x1f;
    138        1.1    briggs 	if (fp != NULL)
    139        1.1    briggs 		*fp = (tag >> 8) & 0x7;
    140        1.1    briggs 	return;
    141        1.1    briggs }
    142        1.1    briggs 
    143        1.1    briggs /*
    144        1.1    briggs  * The Kahlua documentation says that "reg" should be left-shifted by two
    145        1.1    briggs  * and be in bits 2-7.  Apparently not.  It doesn't work that way, and the
    146        1.1    briggs  * DINK32 ROM doesn't do it that way (I peeked at 0xfec00000 after running
    147        1.1    briggs  * the DINK32 "pcf" command).
    148        1.1    briggs  */
    149        1.1    briggs #define SP_PCI(tag, reg) ((tag) | (reg))
    150        1.1    briggs 
    151        1.1    briggs pcireg_t
    152        1.1    briggs pci_conf_read(pc, tag, reg)
    153        1.1    briggs 	pci_chipset_tag_t pc;
    154        1.1    briggs 	pcitag_t tag;
    155        1.1    briggs 	int reg;
    156        1.1    briggs {
    157        1.1    briggs 	pcireg_t data;
    158        1.1    briggs 
    159        1.1    briggs 	out32rb(SANDPOINT_PCI_CONFIG_ADDR, SP_PCI(tag,reg));
    160        1.1    briggs 	data = in32rb(SANDPOINT_PCI_CONFIG_DATA);
    161        1.1    briggs 	out32rb(SANDPOINT_PCI_CONFIG_ADDR, 0);
    162        1.1    briggs 	return data;
    163        1.1    briggs }
    164        1.1    briggs 
    165        1.1    briggs void
    166        1.1    briggs pci_conf_write(pc, tag, reg, data)
    167        1.1    briggs 	pci_chipset_tag_t pc;
    168        1.1    briggs 	pcitag_t tag;
    169        1.1    briggs 	int reg;
    170        1.1    briggs 	pcireg_t data;
    171        1.1    briggs {
    172        1.1    briggs 	out32rb(SANDPOINT_PCI_CONFIG_ADDR, SP_PCI(tag, reg));
    173        1.1    briggs 	out32rb(SANDPOINT_PCI_CONFIG_DATA, data);
    174        1.1    briggs 	out32rb(SANDPOINT_PCI_CONFIG_ADDR, 0);
    175        1.1    briggs }
    176        1.1    briggs 
    177        1.1    briggs int
    178        1.1    briggs pci_intr_map(pa, ihp)
    179        1.1    briggs 	struct pci_attach_args *pa;
    180        1.1    briggs 	pci_intr_handle_t *ihp;
    181        1.1    briggs {
    182        1.1    briggs 	int	pin = pa->pa_intrpin;
    183        1.1    briggs 	int	line = pa->pa_intrline;
    184        1.1    briggs 
    185        1.1    briggs 	if (pin == 0) {
    186        1.1    briggs 		/* No IRQ used. */
    187        1.1    briggs 		goto bad;
    188        1.1    briggs 	}
    189        1.1    briggs 
    190        1.1    briggs 	if (pin > 4) {
    191        1.1    briggs 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
    192        1.1    briggs 		goto bad;
    193        1.1    briggs 	}
    194        1.1    briggs 
    195        1.1    briggs 	/*
    196        1.1    briggs 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
    197        1.1    briggs 	 * `unknown' or `no connection' on a PC.  We assume that a device with
    198        1.1    briggs 	 * `no connection' either doesn't have an interrupt (in which case the
    199        1.1    briggs 	 * pin number should be 0, and would have been noticed above), or
    200        1.1    briggs 	 * wasn't configured by the BIOS (in which case we punt, since there's
    201        1.1    briggs 	 * no real way we can know how the interrupt lines are mapped in the
    202        1.1    briggs 	 * hardware).
    203        1.1    briggs 	 *
    204        1.1    briggs 	 * XXX
    205        1.1    briggs 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
    206        1.1    briggs 	 * that the BIOS did its job, we also recognize that as meaning that
    207        1.1    briggs 	 * the BIOS has not configured the device.
    208        1.1    briggs 	 */
    209        1.1    briggs 	if (line == 255) {
    210        1.1    briggs 		printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
    211        1.1    briggs 		goto bad;
    212        1.8    briggs 	}
    213        1.9    briggs #if defined(OPENPIC_SERIAL_MODE)
    214        1.8    briggs 	if (line == 11) {
    215        1.8    briggs 		switch (pin) {
    216        1.8    briggs 		case PCI_INTERRUPT_PIN_A:
    217        1.8    briggs 			*ihp = SANDPOINT_INTR_WINBOND_A;
    218        1.8    briggs 			break;
    219        1.8    briggs 		case PCI_INTERRUPT_PIN_B:
    220        1.8    briggs 			*ihp = SANDPOINT_INTR_WINBOND_B;
    221        1.8    briggs 			break;
    222        1.8    briggs 		case PCI_INTERRUPT_PIN_C:
    223        1.8    briggs 			*ihp = SANDPOINT_INTR_WINBOND_C;
    224        1.8    briggs 			break;
    225        1.8    briggs 		case PCI_INTERRUPT_PIN_D:
    226        1.8    briggs 			*ihp = SANDPOINT_INTR_WINBOND_D;
    227        1.8    briggs 			break;
    228        1.8    briggs 		default:
    229        1.8    briggs 			printf("pci_intr_map: bad interrupt line %d,%c\n",
    230        1.8    briggs 				line, pin + '@');
    231        1.8    briggs 			goto bad;
    232        1.8    briggs 			break;
    233        1.8    briggs 		}
    234        1.9    briggs 			*ihp = SANDPOINT_INTR_WINBOND_C;
    235        1.1    briggs 	} else {
    236        1.9    briggs #else
    237        1.9    briggs 	if (1) {
    238        1.9    briggs #endif
    239        1.1    briggs 		/*
    240        1.1    briggs 		 * Sandpoint has 4 PCI slots.
    241        1.5       wiz 		 * Sandpoint rev. X2 has them in a weird order.  Counting
    242        1.1    briggs 		 * from center out toward the edge, we have:
    243        1.1    briggs 		 * 	Slot 1 (dev 14?) (labelled 1)
    244        1.1    briggs 		 * 	Slot 0 (dev 13?) (labelled 2)
    245        1.1    briggs 		 * 	Slot 3 (dev 16)  (labelled 3)
    246        1.1    briggs 		 * 	Slot 2 (dev 15)  (labelled 4)
    247        1.1    briggs 		 * To keep things confusing, we will consistently use a zero-
    248        1.1    briggs 		 * based numbering scheme where Motorola's is usually 1-based.
    249        1.1    briggs 		 */
    250        1.1    briggs 		if (line < 13 || line > 16) {
    251        1.8    briggs 			printf("pci_intr_map: bad interrupt line %d,%c\n",
    252        1.8    briggs 				line, pin + '@');
    253        1.1    briggs 			goto bad;
    254        1.1    briggs 		}
    255        1.8    briggs 
    256        1.8    briggs 		/*
    257        1.8    briggs 		 * In the PCI configuration code, we simply assign the dev
    258        1.8    briggs 		 * number to the interrupt line.  We extract it here for the
    259        1.8    briggs 		 * interrupt, but subtract off the lowest dev (13) to get
    260        1.8    briggs 		 * the IRQ.
    261        1.8    briggs 		 */
    262        1.6    briggs #if defined(OPENPIC_SERIAL_MODE)
    263        1.8    briggs 		*ihp = line - 11;
    264        1.6    briggs #else
    265        1.8    briggs 		*ihp = line - 13;
    266        1.6    briggs #endif
    267        1.8    briggs 	}
    268        1.1    briggs 	return 0;
    269        1.1    briggs 
    270        1.1    briggs bad:
    271        1.1    briggs 	*ihp = -1;
    272        1.1    briggs 	return 1;
    273        1.1    briggs }
    274        1.1    briggs 
    275        1.1    briggs const char *
    276        1.1    briggs pci_intr_string(pc, ih)
    277        1.1    briggs 	pci_chipset_tag_t pc;
    278        1.1    briggs 	pci_intr_handle_t ih;
    279        1.1    briggs {
    280        1.1    briggs 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    281        1.1    briggs 
    282        1.1    briggs 	if (ih < 0 || ih >= ICU_LEN)
    283       1.10    provos 		panic("pci_intr_string: bogus handle 0x%x", ih);
    284        1.1    briggs 
    285        1.1    briggs 	sprintf(irqstr, "irq %d", ih);
    286        1.1    briggs 	return (irqstr);
    287        1.1    briggs 
    288        1.1    briggs }
    289        1.1    briggs 
    290        1.1    briggs const struct evcnt *
    291        1.1    briggs pci_intr_evcnt(pc, ih)
    292        1.1    briggs 	pci_chipset_tag_t pc;
    293        1.1    briggs 	pci_intr_handle_t ih;
    294        1.1    briggs {
    295        1.1    briggs 
    296        1.1    briggs 	/* XXX for now, no evcnt parent reported */
    297        1.1    briggs 	return NULL;
    298        1.1    briggs }
    299        1.1    briggs 
    300        1.1    briggs void *
    301        1.1    briggs pci_intr_establish(pc, ih, level, func, arg)
    302        1.1    briggs 	pci_chipset_tag_t pc;
    303        1.1    briggs 	pci_intr_handle_t ih;
    304        1.1    briggs 	int level, (*func) __P((void *));
    305        1.1    briggs 	void *arg;
    306        1.1    briggs {
    307        1.6    briggs #if 0
    308        1.6    briggs 	if (ih < SANDPOINT_INTR_PCI0 || ih > SANDPOINT_INTR_PCI3)
    309       1.10    provos 		panic("pci_intr_establish: bogus handle 0x%x", ih);
    310        1.6    briggs #endif
    311        1.1    briggs 
    312        1.1    briggs 	/*
    313        1.1    briggs 	 * ih is the value assigned in pci_intr_map(), above.
    314        1.1    briggs 	 * For the Sandpoint, this is the zero-based slot #,
    315        1.1    briggs 	 * configured when the bus is set up.
    316        1.1    briggs 	 */
    317        1.1    briggs 	return intr_establish(ih, IST_LEVEL, level, func, arg);
    318        1.1    briggs }
    319        1.1    briggs 
    320        1.1    briggs void
    321        1.1    briggs pci_intr_disestablish(pc, cookie)
    322        1.1    briggs 	pci_chipset_tag_t pc;
    323        1.1    briggs 	void *cookie;
    324        1.1    briggs {
    325        1.3     lukem 	intr_disestablish(cookie);
    326        1.1    briggs }
    327        1.1    briggs 
    328        1.1    briggs void
    329        1.7   thorpej pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin, int swiz,
    330        1.2    briggs     int *iline)
    331        1.1    briggs {
    332        1.2    briggs 	if (bus == 0) {
    333        1.2    briggs 		*iline = dev;
    334        1.2    briggs 	} else {
    335        1.2    briggs 		/*
    336        1.2    briggs 		 * If we are not on bus zero, we're behind a bridge, so we
    337        1.2    briggs 		 * swizzle.
    338        1.2    briggs 		 *
    339        1.2    briggs 		 * The documentation lies about this.  In slot 3 (numbering
    340        1.2    briggs 		 * from 0) aka device 16, INTD# becomes an interrupt for
    341        1.2    briggs 		 * slot 2.  INTC# becomes an interrupt for slot 1, etc.
    342        1.2    briggs 		 * In slot 2 aka device 16, INTD# becomes an interrupt for
    343        1.2    briggs 		 * slot 1, etc.
    344        1.2    briggs 		 *
    345        1.2    briggs 		 * Verified for INTD# on device 16, INTC# on device 16,
    346        1.2    briggs 		 * INTD# on device 15, INTD# on device 13, and INTC# on
    347        1.2    briggs 		 * device 14.  I presume that the rest follow the same
    348        1.2    briggs 		 * pattern.
    349        1.2    briggs 		 *
    350        1.2    briggs 		 * Slot 0 is device 13, and is the base for the rest.
    351        1.2    briggs 		 */
    352        1.2    briggs 		*iline = 13 + ((swiz + dev + 3) & 3);
    353        1.2    briggs 	}
    354        1.1    briggs }
    355