Home | History | Annotate | Line # | Download | only in pci
apecs_pci.c revision 1.3
      1  1.3  cgd /*	$NetBSD: apecs_pci.c,v 1.3 1995/08/03 01:16:57 cgd Exp $	*/
      2  1.1  cgd 
      3  1.1  cgd /*
      4  1.3  cgd  * Copyright (c) 1995 Carnegie-Mellon University.
      5  1.1  cgd  * All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * Author: Chris G. Demetriou
      8  1.1  cgd  *
      9  1.1  cgd  * Permission to use, copy, modify and distribute this software and
     10  1.1  cgd  * its documentation is hereby granted, provided that both the copyright
     11  1.1  cgd  * notice and this permission notice appear in all copies of the
     12  1.1  cgd  * software, derivative works or modified versions, and any portions
     13  1.1  cgd  * thereof, and that both notices appear in supporting documentation.
     14  1.1  cgd  *
     15  1.1  cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  1.1  cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  1.1  cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  1.1  cgd  *
     19  1.1  cgd  * Carnegie Mellon requests users of this software to return to
     20  1.1  cgd  *
     21  1.1  cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  1.1  cgd  *  School of Computer Science
     23  1.1  cgd  *  Carnegie Mellon University
     24  1.1  cgd  *  Pittsburgh PA 15213-3890
     25  1.1  cgd  *
     26  1.1  cgd  * any improvements or extensions that they make and grant Carnegie the
     27  1.1  cgd  * rights to redistribute these changes.
     28  1.1  cgd  */
     29  1.1  cgd 
     30  1.1  cgd #include <sys/param.h>
     31  1.1  cgd #include <sys/systm.h>
     32  1.1  cgd #include <sys/kernel.h>
     33  1.1  cgd #include <sys/device.h>
     34  1.1  cgd #include <vm/vm.h>
     35  1.1  cgd 
     36  1.1  cgd #include <machine/autoconf.h>
     37  1.1  cgd #include <machine/pio.h>
     38  1.1  cgd 
     39  1.1  cgd #include <dev/isa/isavar.h>
     40  1.1  cgd 
     41  1.1  cgd #include <dev/pci/pcireg.h>
     42  1.1  cgd #include <dev/pci/pcivar.h>
     43  1.1  cgd #include <alpha/pci/pci_chipset.h>
     44  1.1  cgd #include <alpha/pci/apecsreg.h>
     45  1.1  cgd 
     46  1.1  cgd void	 apecs_setup __P((void));
     47  1.1  cgd pcitag_t apecs_make_tag __P((int, int, int));
     48  1.1  cgd pcireg_t apecs_conf_read __P((pcitag_t, int));
     49  1.1  cgd void	 apecs_conf_write __P((pcitag_t, int, pcireg_t));
     50  1.2  cgd int	 apecs_map_io __P((pcitag_t, int, int *));
     51  1.1  cgd int	 apecs_map_mem __P((pcitag_t, int, vm_offset_t *, vm_offset_t *));
     52  1.1  cgd int	 apecs_pcidma_map __P((caddr_t, vm_size_t, vm_offset_t *));
     53  1.1  cgd void	 apecs_pcidma_unmap __P((caddr_t, vm_size_t, int, vm_offset_t *));
     54  1.1  cgd 
     55  1.1  cgd struct pci_cs_fcns apecs_p1e_cs_fcns = {	/* XXX WHAT'S DIFFERENT? */
     56  1.1  cgd 	apecs_setup,
     57  1.1  cgd 	apecs_make_tag,
     58  1.1  cgd 	apecs_conf_read,
     59  1.1  cgd 	apecs_conf_write,
     60  1.2  cgd 	apecs_map_io,
     61  1.1  cgd 	apecs_map_mem,
     62  1.1  cgd 	apecs_pcidma_map,
     63  1.1  cgd 	apecs_pcidma_unmap,
     64  1.1  cgd };
     65  1.1  cgd 
     66  1.1  cgd struct pci_cs_fcns apecs_p2e_cs_fcns = {	/* XXX WHAT'S DIFFERENT? */
     67  1.1  cgd 	apecs_setup,
     68  1.1  cgd 	apecs_make_tag,
     69  1.1  cgd 	apecs_conf_read,
     70  1.1  cgd 	apecs_conf_write,
     71  1.2  cgd 	apecs_map_io,
     72  1.1  cgd 	apecs_map_mem,
     73  1.1  cgd 	apecs_pcidma_map,
     74  1.1  cgd 	apecs_pcidma_unmap,
     75  1.1  cgd };
     76  1.1  cgd 
     77  1.1  cgd #define	REGVAL(r)	(*(u_int32_t *)phystok0seg(r))
     78  1.1  cgd 
     79  1.1  cgd void
     80  1.1  cgd apecs_setup()
     81  1.1  cgd {
     82  1.1  cgd 
     83  1.1  cgd 	/*
     84  1.1  cgd 	 * Set up PCI bus mastering DMA windows on the APECS chip.
     85  1.1  cgd 	 *
     86  1.1  cgd 	 * What the PROM wants:
     87  1.1  cgd 	 *	a 1G direct-mapped window that maps the PCI address
     88  1.1  cgd 	 *	space from 4G -> 5G to memory addresses 0 -> 1G,
     89  1.1  cgd 	 *	set up in window two.
     90  1.1  cgd 	 *
     91  1.1  cgd 	 * What we want:
     92  1.1  cgd 	 *	a 1G direct-mapped window that maps the PCI address
     93  1.1  cgd 	 *	space from 0 -> 1G to memory addresses 0 -> 1G.
     94  1.1  cgd 	 *
     95  1.1  cgd 	 * Unless we satisfy the PROM, we can't live through a reboot.
     96  1.1  cgd 	 * If we don't do what we want, I have to write more code.
     97  1.1  cgd 
     98  1.1  cgd 	 * So:
     99  1.1  cgd 	 *	Leave window two alone, map window 1 the way I want it.
    100  1.1  cgd 	 *
    101  1.1  cgd 	 * XXX verify that windows don't overlap
    102  1.1  cgd 	 * XXX be trickier
    103  1.1  cgd 	 * XXX magic numbers
    104  1.1  cgd 	 */
    105  1.1  cgd 
    106  1.1  cgd #if 0 /* should be routine to dump regs */
    107  1.1  cgd 	printf("old base1  was 0x%x\n", REGVAL(EPIC_PCI_BASE_1));
    108  1.1  cgd 	printf("old mask1  was 0x%x\n", REGVAL(EPIC_PCI_MASK_1));
    109  1.1  cgd 	printf("old tbase1 was 0x%x\n", REGVAL(EPIC_TBASE_1));
    110  1.1  cgd 
    111  1.1  cgd 	printf("old base2  was 0x%x\n", REGVAL(EPIC_PCI_BASE_2));
    112  1.1  cgd 	printf("old mask2  was 0x%x\n", REGVAL(EPIC_PCI_MASK_2));
    113  1.1  cgd 	printf("old tbase2 was 0x%x\n", REGVAL(EPIC_TBASE_2));
    114  1.1  cgd #endif
    115  1.1  cgd 
    116  1.1  cgd #if 0 /* XXX STUPID PROM; MUST LEAVE WINDOW 2 ALONE.  See above */
    117  1.1  cgd         /* Turn off DMA window enables in PCI Base Reg 2. */
    118  1.1  cgd         REGVAL(EPIC_PCI_BASE_2) = 0;
    119  1.1  cgd 
    120  1.1  cgd         /* Set up Translated Base Register 2; translate to sybBus addr 0. */
    121  1.1  cgd 	REGVAL(EPIC_TBASE_2) = 0;
    122  1.1  cgd 
    123  1.1  cgd 	/* Set up PCI mask register 2; map 1G space. */
    124  1.1  cgd 	REGVAL(EPIC_PCI_MASK_2) = 0x3ff00000;
    125  1.1  cgd 
    126  1.1  cgd 	/* Enable window 2; from PCI address 4G, direct mapped. */
    127  1.1  cgd 	REGVAL(EPIC_PCI_BASE_2) = 0x40080000;
    128  1.1  cgd #endif /* STUPID PROM */
    129  1.1  cgd 
    130  1.1  cgd         /* Turn off DMA window enables in PCI Base Reg 1. */
    131  1.1  cgd         REGVAL(EPIC_PCI_BASE_1) = 0;
    132  1.1  cgd 
    133  1.1  cgd         /* Set up Translated Base Register 1; translate to sybBus addr 0. */
    134  1.1  cgd { /* XXX */
    135  1.1  cgd extern struct sgmapent *sgmap;
    136  1.1  cgd 	REGVAL(EPIC_TBASE_1) = vtophys(sgmap) >> 1;
    137  1.1  cgd } /* XXX */
    138  1.1  cgd 
    139  1.1  cgd 	/* Set up PCI mask register 1; map 8MB space. */
    140  1.1  cgd 	REGVAL(EPIC_PCI_MASK_1) = 0x00700000;
    141  1.1  cgd 
    142  1.1  cgd 	/* Enable window 1; from PCI address 8MB, direct mapped. */
    143  1.1  cgd 	REGVAL(EPIC_PCI_BASE_1) = 0x008c0000;
    144  1.1  cgd 
    145  1.1  cgd 	/*
    146  1.1  cgd 	 * Should set up HAXR1 and HAXR2...  However, the PROM again
    147  1.1  cgd 	 * wants them where they're set to be...
    148  1.1  cgd 	 */
    149  1.1  cgd #if 0
    150  1.1  cgd 	printf("old haxr0  was 0x%x\n", REGVAL(EPIC_HAXR0));
    151  1.1  cgd 	printf("old haxr1  was 0x%x\n", REGVAL(EPIC_HAXR1));
    152  1.1  cgd 	printf("old haxr2  was 0x%x\n", REGVAL(EPIC_HAXR2));
    153  1.1  cgd #endif
    154  1.1  cgd 
    155  1.1  cgd #if 0 /* XXX STUPID PROM */
    156  1.1  cgd 	/* HAXR0 is wired zero; no op. */
    157  1.1  cgd 	REGVAL(EPIC_HAXR0) = 0;
    158  1.1  cgd 
    159  1.1  cgd 	/* HAXR1: maps PCI memory space above 16M.  16M -> 2G+16M. */
    160  1.1  cgd 	REGVAL(EPIC_HAXR1) = 0x80000000;
    161  1.1  cgd 
    162  1.1  cgd 	/* HAXR2: maps PCI I/O space above 256K.  256K -> 256k. */
    163  1.1  cgd 	REGVAL(EPIC_HAXR2) = 0;
    164  1.1  cgd #endif
    165  1.1  cgd }
    166  1.1  cgd 
    167  1.1  cgd pcitag_t
    168  1.1  cgd apecs_make_tag(bus, device, function)
    169  1.1  cgd 	int bus, device, function;
    170  1.1  cgd {
    171  1.1  cgd 	pcitag_t tag;
    172  1.1  cgd 
    173  1.1  cgd 	if (bus >= 256 || device >= 32 || function >= 8)
    174  1.1  cgd 		panic("apecs_make_tag: bad request");
    175  1.1  cgd 
    176  1.1  cgd 	tag = (bus << 21) | (device << 16) | (function << 13);
    177  1.1  cgd #if 0
    178  1.1  cgd 	printf("apecs_make_tag: bus %d, device %d, function %d -> 0x%lx\n", bus,
    179  1.1  cgd 	    device, function, tag);
    180  1.1  cgd #endif
    181  1.1  cgd 	return tag;
    182  1.1  cgd }
    183  1.1  cgd 
    184  1.1  cgd pcireg_t
    185  1.1  cgd apecs_conf_read(tag, offset)
    186  1.1  cgd 	pcitag_t tag;
    187  1.1  cgd 	int offset;					/* XXX */
    188  1.1  cgd {
    189  1.1  cgd 	pcireg_t *datap, data;
    190  1.1  cgd 	int reg = offset >> 2;				/* XXX */
    191  1.1  cgd 
    192  1.1  cgd 	if ((tag & 0x1fe00000) != 0) {
    193  1.1  cgd 		panic("apecs_conf_read: bus != 0?");
    194  1.1  cgd 	}
    195  1.1  cgd 	/* XXX FILL IN HAXR2 bits. */
    196  1.1  cgd 
    197  1.1  cgd 	datap = (pcireg_t *)
    198  1.1  cgd 	    phystok0seg(APECS_PCI_CONF | tag | reg << 7 | 0 << 5 | 0x3 << 3);
    199  1.1  cgd 	if (badaddr(datap, sizeof *datap))
    200  1.1  cgd 		return ((pcireg_t)-1);
    201  1.1  cgd 	data = *datap;
    202  1.1  cgd #if 0
    203  1.1  cgd 	printf("apecs_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p\n", tag, reg,
    204  1.1  cgd 	    data, datap);
    205  1.1  cgd #endif
    206  1.1  cgd 	return data;
    207  1.1  cgd }
    208  1.1  cgd 
    209  1.1  cgd void
    210  1.1  cgd apecs_conf_write(tag, offset, data)
    211  1.1  cgd 	pcitag_t tag;
    212  1.1  cgd 	int offset;					/* XXX */
    213  1.1  cgd 	pcireg_t data;
    214  1.1  cgd {
    215  1.1  cgd 	pcireg_t *datap;
    216  1.1  cgd 	int reg = offset >> 2;				/* XXX */
    217  1.1  cgd 
    218  1.1  cgd 	if ((tag & 0x1fe00000) != 0) {
    219  1.1  cgd 		panic("apecs_conf_read: bus != 0?");
    220  1.1  cgd 	}
    221  1.1  cgd 	/* XXX FILL IN HAXR2 bits. */
    222  1.1  cgd 
    223  1.1  cgd 	datap = (pcireg_t *)
    224  1.1  cgd 	    phystok0seg(APECS_PCI_CONF | tag | reg << 7 | 0 << 5 | 0x3 << 3);
    225  1.1  cgd #if 0
    226  1.1  cgd 	printf("apecs_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
    227  1.1  cgd 	    reg, data, datap);
    228  1.1  cgd #endif
    229  1.1  cgd 	*datap = data;
    230  1.1  cgd }
    231  1.1  cgd 
    232  1.2  cgd int
    233  1.2  cgd apecs_map_io(tag, reg, iobasep)
    234  1.2  cgd 	pcitag_t tag;
    235  1.2  cgd 	int reg;
    236  1.2  cgd 	int *iobasep;
    237  1.2  cgd {
    238  1.2  cgd 	pcireg_t data;
    239  1.2  cgd 	int pci_iobase;
    240  1.2  cgd 
    241  1.2  cgd 	if (reg < PCI_MAP_REG_START || reg >= PCI_MAP_REG_END || (reg & 3))
    242  1.2  cgd 		panic("apecs_map_io: bad request");
    243  1.2  cgd 
    244  1.2  cgd 	data = pci_conf_read(tag, reg);
    245  1.2  cgd 
    246  1.2  cgd 	if ((data & PCI_MAP_IO) == 0)
    247  1.2  cgd 		panic("apecs_map_io: attempt to I/O map an memory region");
    248  1.1  cgd 
    249  1.2  cgd 	/* figure out where it was mapped... */
    250  1.2  cgd 	pci_iobase = data & PCI_MAP_MEMORY_ADDRESS_MASK; /* PCI I/O addr */
    251  1.2  cgd 
    252  1.2  cgd 	return (pci_iobase);
    253  1.2  cgd }
    254  1.1  cgd 
    255  1.1  cgd int
    256  1.1  cgd apecs_map_mem(tag, reg, vap, pap)
    257  1.1  cgd 	pcitag_t tag;
    258  1.1  cgd 	int reg;
    259  1.1  cgd 	vm_offset_t *vap, *pap;
    260  1.1  cgd {
    261  1.1  cgd 	pcireg_t data;
    262  1.1  cgd 	vm_offset_t pci_pa, sb_pa;
    263  1.1  cgd 
    264  1.1  cgd 	if (reg < PCI_MAP_REG_START || reg >= PCI_MAP_REG_END || (reg & 3))
    265  1.1  cgd 		panic("apecs_map_mem: bad request");
    266  1.1  cgd 
    267  1.1  cgd 	/*
    268  1.1  cgd 	 * "HERE WE GO AGAIN!!!"
    269  1.1  cgd 	 *
    270  1.1  cgd 	 * The PROM has already mapped the device for us.  The PROM is
    271  1.1  cgd 	 * our friend.  We wouldn't want to make the PROM unhappy.
    272  1.1  cgd 	 *
    273  1.1  cgd 	 * So, we take the address that's been assigned (already) to
    274  1.1  cgd 	 * the register, and figure out what physical and virtual addresses
    275  1.1  cgd 	 * go with it...
    276  1.1  cgd 	 */
    277  1.1  cgd 	/*
    278  1.1  cgd 	 * Section 6.2.5.1, `Address Maps', says that a device which wants 2^n
    279  1.1  cgd 	 * bytes of memory will hardwire the bottom n bits of the address to 0.
    280  1.1  cgd 	 * As recommended, we write all 1s and see what we get back.
    281  1.1  cgd 	 */
    282  1.1  cgd 	data = pci_conf_read(tag, reg);
    283  1.1  cgd 
    284  1.1  cgd 	if (data & PCI_MAP_IO)
    285  1.1  cgd 		panic("apecs_map_mem: attempt to memory map an I/O region");
    286  1.1  cgd 
    287  1.1  cgd 	switch (data & PCI_MAP_MEMORY_TYPE_MASK) {
    288  1.1  cgd 	case PCI_MAP_MEMORY_TYPE_32BIT:
    289  1.1  cgd 		break;
    290  1.1  cgd 	case PCI_MAP_MEMORY_TYPE_32BIT_1M:
    291  1.1  cgd 		printf("apecs_map_mem: attempt to map restricted 32-bit region\n");
    292  1.1  cgd 		return EOPNOTSUPP;
    293  1.1  cgd 	case PCI_MAP_MEMORY_TYPE_64BIT:
    294  1.1  cgd 		printf("apecs_map_mem: attempt to map 64-bit region\n");
    295  1.1  cgd 		return EOPNOTSUPP;
    296  1.1  cgd 	default:
    297  1.1  cgd 		printf("apecs_map_mem: reserved mapping type\n");
    298  1.1  cgd 		return EINVAL;
    299  1.1  cgd 	}
    300  1.1  cgd 
    301  1.2  cgd 	/* figure out where it was mapped... */
    302  1.2  cgd 	pci_pa = data & PCI_MAP_MEMORY_ADDRESS_MASK;	/* PCI bus address */
    303  1.2  cgd 
    304  1.2  cgd 	/* calcluate sysBus address -- should be a better way to get space */
    305  1.1  cgd 	if (data & PCI_MAP_MEMORY_CACHABLE) {
    306  1.2  cgd 		/* Dense space */
    307  1.2  cgd 		sb_pa = (pci_pa & 0xffffffff) | (3L << 32);	/* XXX */
    308  1.2  cgd 	} else {
    309  1.2  cgd 		/* Sparse space */
    310  1.2  cgd 		sb_pa = ((pci_pa & 0x7ffffff) << 5) | (2L << 32); /* XXX */
    311  1.1  cgd 	}
    312  1.1  cgd 
    313  1.1  cgd 	/* and tell the driver. */
    314  1.1  cgd 	*vap = phystok0seg(sb_pa);
    315  1.1  cgd 	*pap = pci_pa;
    316  1.1  cgd 
    317  1.1  cgd #if 0
    318  1.1  cgd 	printf("pci_map_mem: memory mapped at 0x%lx\n", *pap);
    319  1.1  cgd 	printf("pci_map_mem: virtual 0x%lx\n", *vap);
    320  1.1  cgd #endif
    321  1.1  cgd 
    322  1.1  cgd 	return 0;
    323  1.1  cgd }
    324  1.1  cgd 
    325  1.1  cgd int
    326  1.1  cgd apecs_pcidma_map(addr, size, mappings)
    327  1.1  cgd 	caddr_t addr;
    328  1.1  cgd 	vm_size_t size;
    329  1.1  cgd 	vm_offset_t *mappings;
    330  1.1  cgd {
    331  1.1  cgd 	vm_offset_t va;
    332  1.1  cgd 	long todo;
    333  1.1  cgd 	int i;
    334  1.1  cgd 
    335  1.1  cgd 	i = 0;
    336  1.1  cgd 	va = (vm_offset_t)addr;
    337  1.1  cgd 	todo = size;
    338  1.1  cgd 
    339  1.1  cgd 	while (todo > 0) {
    340  1.1  cgd 		mappings[i] = vtophys(va) | 0x40000000;
    341  1.1  cgd #if 0
    342  1.1  cgd 		printf("a_pd_m mapping %d: %lx -> %lx -> %lx\n", i, va,
    343  1.1  cgd 		    vtophys(va), mappings[i]);
    344  1.1  cgd #endif
    345  1.1  cgd 		i++;
    346  1.1  cgd 		todo -= PAGE_SIZE - (va - trunc_page(va));
    347  1.1  cgd 		va += PAGE_SIZE - (va - trunc_page(va));
    348  1.1  cgd 	}
    349  1.1  cgd 	return (i);
    350  1.1  cgd }
    351  1.1  cgd 
    352  1.1  cgd void
    353  1.1  cgd apecs_pcidma_unmap(addr, size, nmappings, mappings)
    354  1.1  cgd 	caddr_t addr;
    355  1.1  cgd 	vm_size_t size;
    356  1.1  cgd 	int nmappings;
    357  1.1  cgd 	vm_offset_t *mappings;
    358  1.1  cgd {
    359  1.1  cgd 
    360  1.1  cgd 	/* maybe XXX if diagnostic, check that mapping happened. */
    361  1.1  cgd 	printf("apecs_pcidma_unmap: nada\n");
    362  1.1  cgd }
    363