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