Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.14
      1  1.14    thomas /*	$NetBSD: pci_machdep.c,v 1.14 1998/12/20 14:24:52 thomas Exp $	*/
      2   1.1       leo 
      3   1.1       leo /*
      4   1.1       leo  * Copyright (c) 1996 Leo Weppelman.  All rights reserved.
      5   1.7       cgd  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
      6  1.13   mycroft  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      7   1.1       leo  *
      8   1.1       leo  * Redistribution and use in source and binary forms, with or without
      9   1.1       leo  * modification, are permitted provided that the following conditions
     10   1.1       leo  * are met:
     11   1.1       leo  * 1. Redistributions of source code must retain the above copyright
     12   1.1       leo  *    notice, this list of conditions and the following disclaimer.
     13   1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       leo  *    documentation and/or other materials provided with the distribution.
     16   1.1       leo  * 3. All advertising materials mentioning features or use of this software
     17   1.1       leo  *    must display the following acknowledgement:
     18  1.13   mycroft  *	This product includes software developed by Charles M. Hannum.
     19   1.1       leo  * 4. The name of the author may not be used to endorse or promote products
     20   1.1       leo  *    derived from this software without specific prior written permission.
     21   1.1       leo  *
     22   1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1       leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1       leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1       leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1       leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1       leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1       leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1       leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       leo  */
     33   1.1       leo 
     34   1.1       leo #include <sys/types.h>
     35   1.1       leo #include <sys/param.h>
     36   1.1       leo #include <sys/time.h>
     37   1.1       leo #include <sys/systm.h>
     38   1.1       leo #include <sys/errno.h>
     39   1.1       leo #include <sys/device.h>
     40  1.14    thomas #include <sys/malloc.h>
     41   1.1       leo 
     42   1.1       leo #include <vm/vm.h>
     43   1.1       leo #include <vm/vm_kern.h>
     44   1.1       leo 
     45   1.1       leo #include <dev/pci/pcivar.h>
     46   1.1       leo #include <dev/pci/pcireg.h>
     47   1.1       leo 
     48   1.1       leo #include <machine/cpu.h>
     49   1.1       leo #include <machine/iomap.h>
     50   1.9       leo #include <machine/mfp.h>
     51  1.10       leo #include <machine/bus.h>
     52  1.10       leo 
     53   1.1       leo #include <atari/atari/device.h>
     54   1.1       leo 
     55   1.9       leo /*
     56  1.14    thomas  * Sizes of pci memory and I/O area.
     57   1.9       leo  */
     58  1.14    thomas #define PCI_MEM_END     0x10000000      /* 256 MByte */
     59  1.14    thomas #define PCI_IO_END      0x10000000      /* 256 MByte */
     60  1.14    thomas 
     61  1.14    thomas /*
     62  1.14    thomas  * We preserve some space at the begin of the pci area for 32BIT_1M
     63  1.14    thomas  * devices and standard vga.
     64  1.14    thomas  */
     65  1.14    thomas #define PCI_MEM_START   0x00100000      /*   1 MByte */
     66  1.14    thomas #define PCI_IO_START    0x00010000      /*  64 kByte */
     67  1.14    thomas 
     68  1.14    thomas /*
     69  1.14    thomas  * Struct to hold the memory and I/O datas of the pci devices
     70  1.14    thomas  */
     71  1.14    thomas struct pci_memreg {
     72  1.14    thomas     LIST_ENTRY(pci_memreg) link;
     73  1.14    thomas     int dev;
     74  1.14    thomas     pcitag_t tag;
     75  1.14    thomas     pcireg_t reg, address, mask;
     76  1.14    thomas     u_int32_t size;
     77  1.14    thomas     u_int32_t csr;
     78  1.14    thomas };
     79  1.14    thomas 
     80  1.14    thomas typedef LIST_HEAD(pci_memreg_head, pci_memreg) PCI_MEMREG;
     81   1.9       leo 
     82   1.1       leo int	pcibusprint __P((void *auxp, const char *));
     83   1.5       leo int	pcibusmatch __P((struct device *, struct cfdata *, void *));
     84   1.1       leo void	pcibusattach __P((struct device *, struct device *, void *));
     85   1.1       leo 
     86  1.14    thomas static void enable_pci_devices __P((void));
     87  1.14    thomas static void insert_into_list __P((PCI_MEMREG *head, struct pci_memreg *elem));
     88  1.14    thomas static int overlap_pci_areas __P((struct pci_memreg *p,
     89  1.14    thomas 	struct pci_memreg *self, u_int addr, u_int size, u_int what));
     90   1.1       leo static int pci_config_offset __P((pcitag_t));
     91   1.1       leo 
     92   1.1       leo struct cfattach pcibus_ca = {
     93   1.1       leo 	sizeof(struct device), pcibusmatch, pcibusattach
     94   1.1       leo };
     95   1.1       leo 
     96   1.1       leo int
     97   1.5       leo pcibusmatch(pdp, cfp, auxp)
     98   1.1       leo struct device	*pdp;
     99   1.5       leo struct cfdata	*cfp;
    100   1.5       leo void		*auxp;
    101   1.1       leo {
    102   1.1       leo 	if(atari_realconfig == 0)
    103   1.1       leo 		return (0);
    104   1.1       leo 	if (strcmp((char *)auxp, "pcibus") || cfp->cf_unit != 0)
    105   1.1       leo 		return(0);
    106   1.1       leo 	return(machineid & ATARI_HADES ? 1 : 0);
    107   1.1       leo }
    108   1.1       leo 
    109   1.1       leo void
    110   1.1       leo pcibusattach(pdp, dp, auxp)
    111   1.1       leo struct device	*pdp, *dp;
    112   1.1       leo void		*auxp;
    113   1.1       leo {
    114   1.1       leo 	struct pcibus_attach_args	pba;
    115  1.11       leo 	bus_space_tag_t			leb_alloc_bus_space_tag __P((void));
    116  1.11       leo 
    117   1.1       leo 
    118  1.14    thomas 	enable_pci_devices();
    119  1.14    thomas 
    120   1.1       leo 	pba.pba_busname = "pci";
    121   1.4       leo 	pba.pba_pc      = NULL;
    122   1.1       leo 	pba.pba_bus     = 0;
    123   1.7       cgd 	pba.pba_flags	= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    124  1.10       leo 	pba.pba_dmat	= BUS_PCI_DMA_TAG;
    125  1.11       leo 	pba.pba_iot     = leb_alloc_bus_space_tag();
    126  1.11       leo 	pba.pba_memt    = leb_alloc_bus_space_tag();
    127  1.11       leo 	if ((pba.pba_iot == NULL) || (pba.pba_memt == NULL)) {
    128  1.11       leo 		printf("leb_alloc_bus_space_tag failed!\n");
    129  1.11       leo 		return;
    130  1.11       leo 	}
    131  1.11       leo 	pba.pba_iot->base  = PCI_IO_PHYS;
    132  1.11       leo 	pba.pba_memt->base = PCI_MEM_PHYS;
    133   1.6       leo 
    134   1.9       leo 	MFP2->mf_aer &= ~(0x27); /* PCI interrupts: HIGH -> LOW */
    135   1.9       leo 
    136   1.6       leo 	printf("\n");
    137   1.1       leo 
    138   1.1       leo 	config_found(dp, &pba, pcibusprint);
    139   1.1       leo }
    140   1.1       leo 
    141   1.1       leo int
    142   1.1       leo pcibusprint(auxp, name)
    143   1.1       leo void		*auxp;
    144   1.1       leo const char	*name;
    145   1.1       leo {
    146   1.1       leo 	if(name == NULL)
    147   1.1       leo 		return(UNCONF);
    148   1.1       leo 	return(QUIET);
    149   1.1       leo }
    150   1.1       leo 
    151   1.1       leo void
    152   1.1       leo pci_attach_hook(parent, self, pba)
    153   1.1       leo 	struct device *parent, *self;
    154   1.1       leo 	struct pcibus_attach_args *pba;
    155   1.1       leo {
    156   1.1       leo }
    157   1.1       leo 
    158   1.1       leo /*
    159   1.9       leo  * Initialize the PCI-bus. The Atari-BIOS does not do this, so....
    160  1.14    thomas  * We only disable all devices here. Memory and I/O enabling is done
    161  1.14    thomas  * later at pcibusattach.
    162   1.9       leo  */
    163   1.9       leo void
    164   1.9       leo init_pci_bus()
    165   1.9       leo {
    166   1.9       leo 	pci_chipset_tag_t	pc = NULL; /* XXX */
    167   1.9       leo 	pcitag_t		tag;
    168  1.14    thomas 	pcireg_t		csr;
    169  1.14    thomas 	int			device, id, maxndevs;
    170   1.9       leo 
    171  1.14    thomas 	tag   = 0;
    172  1.14    thomas 	id    = 0;
    173   1.9       leo 
    174   1.9       leo 	maxndevs = pci_bus_maxdevs(pc, 0);
    175   1.9       leo 
    176   1.9       leo 	for (device = 0; device < maxndevs; device++) {
    177   1.9       leo 
    178   1.9       leo 		tag = pci_make_tag(pc, 0, device, 0);
    179   1.9       leo 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
    180   1.9       leo 		if (id == 0 || id == 0xffffffff)
    181   1.9       leo 			continue;
    182   1.9       leo 
    183  1.14    thomas 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    184  1.14    thomas 		csr &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
    185  1.14    thomas 		csr &= ~PCI_COMMAND_MASTER_ENABLE;
    186  1.14    thomas 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    187  1.14    thomas 	}
    188  1.14    thomas }
    189  1.14    thomas 
    190  1.14    thomas /*
    191  1.14    thomas  * insert a new element in an existing list that the ID's (size in struct
    192  1.14    thomas  * pci_memreg) are sorted.
    193  1.14    thomas  */
    194  1.14    thomas static void
    195  1.14    thomas insert_into_list(head, elem)
    196  1.14    thomas     PCI_MEMREG *head;
    197  1.14    thomas     struct pci_memreg *elem;
    198  1.14    thomas {
    199  1.14    thomas     struct pci_memreg *p, *q;
    200  1.14    thomas 
    201  1.14    thomas     p = LIST_FIRST(head);
    202  1.14    thomas     q = NULL;
    203  1.14    thomas 
    204  1.14    thomas     for (; p != NULL && p->size < elem->size; q = p, p = LIST_NEXT(p, link));
    205  1.14    thomas 
    206  1.14    thomas     if (q == NULL) {
    207  1.14    thomas 	LIST_INSERT_HEAD(head, elem, link);
    208  1.14    thomas     } else {
    209  1.14    thomas 	LIST_INSERT_AFTER(q, elem, link);
    210  1.14    thomas     }
    211  1.14    thomas }
    212  1.14    thomas 
    213  1.14    thomas /*
    214  1.14    thomas  * Test if a new selected area overlaps with an already (probably preselected)
    215  1.14    thomas  * pci area.
    216  1.14    thomas  */
    217  1.14    thomas static int
    218  1.14    thomas overlap_pci_areas(p, self, addr, size, what)
    219  1.14    thomas     struct pci_memreg *p, *self;
    220  1.14    thomas     u_int addr, size, what;
    221  1.14    thomas {
    222  1.14    thomas     struct pci_memreg *q;
    223  1.14    thomas 
    224  1.14    thomas     if (p == NULL)
    225  1.14    thomas 	return 0;
    226  1.14    thomas 
    227  1.14    thomas     q = p;
    228  1.14    thomas     while (q != NULL) {
    229  1.14    thomas 	if ((q != self) && (q->csr & what)) {
    230  1.14    thomas 	    if ((addr >= q->address) && (addr < (q->address + q->size))) {
    231  1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    232  1.14    thomas 		printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
    233  1.14    thomas 			self->dev, self->reg, q->dev, q->reg);
    234  1.14    thomas #endif
    235  1.14    thomas 		return 1;
    236  1.14    thomas 	    }
    237  1.14    thomas 	    if ((q->address >= addr) && (q->address < (addr + size))) {
    238  1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    239  1.14    thomas 		printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
    240  1.14    thomas 			self->dev, self->reg, q->dev, q->reg);
    241  1.14    thomas #endif
    242  1.14    thomas 		return 1;
    243  1.14    thomas 	    }
    244  1.14    thomas 	}
    245  1.14    thomas 	q = LIST_NEXT(q, link);
    246  1.14    thomas     }
    247  1.14    thomas     return 0;
    248  1.14    thomas }
    249  1.14    thomas 
    250  1.14    thomas /*
    251  1.14    thomas  * Enable memory and I/O on pci devices. Care about already enabled devices
    252  1.14    thomas  * (probabaly by the console driver).
    253  1.14    thomas  *
    254  1.14    thomas  * The idea behind the following code is:
    255  1.14    thomas  * We build a by sizes sorted list of the requirements of the different
    256  1.14    thomas  * pci devices. After that we choose the start addresses of that areas
    257  1.14    thomas  * in such a way that they are placed as closed as possible together.
    258  1.14    thomas  */
    259  1.14    thomas static void
    260  1.14    thomas enable_pci_devices()
    261  1.14    thomas {
    262  1.14    thomas     PCI_MEMREG memlist;
    263  1.14    thomas     PCI_MEMREG iolist;
    264  1.14    thomas     struct pci_memreg *p, *q;
    265  1.14    thomas     int dev, reg, id, class;
    266  1.14    thomas     pcitag_t tag;
    267  1.14    thomas     pcireg_t csr, address, mask;
    268  1.14    thomas     pci_chipset_tag_t pc;
    269  1.14    thomas     int sizecnt, membase_1m;
    270  1.14    thomas 
    271  1.14    thomas     pc = 0;
    272  1.14    thomas     csr = 0;
    273  1.14    thomas     tag = 0;
    274  1.14    thomas 
    275  1.14    thomas     LIST_INIT(&memlist);
    276  1.14    thomas     LIST_INIT(&iolist);
    277  1.14    thomas 
    278  1.14    thomas     /*
    279  1.14    thomas      * first step: go through all devices and gather memory and I/O
    280  1.14    thomas      * sizes
    281  1.14    thomas      */
    282  1.14    thomas     for (dev = 0; dev < pci_bus_maxdevs(pc,0); dev++) {
    283  1.14    thomas 
    284  1.14    thomas 	tag = pci_make_tag(pc, 0, dev, 0);
    285  1.14    thomas 	id  = pci_conf_read(pc, tag, PCI_ID_REG);
    286  1.14    thomas 	if (id == 0 || id == 0xffffffff)
    287  1.14    thomas 	    continue;
    288  1.14    thomas 
    289  1.14    thomas 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    290  1.14    thomas 
    291  1.14    thomas 	/*
    292  1.14    thomas 	 * special case: if a display card is found and memory is enabled
    293  1.14    thomas 	 * preserve 128k at 0xa0000 as vga memory.
    294  1.14    thomas 	 */
    295  1.14    thomas 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    296  1.14    thomas 	switch (PCI_CLASS(class)) {
    297  1.14    thomas 	    case PCI_CLASS_PREHISTORIC:
    298  1.14    thomas 	    case PCI_CLASS_DISPLAY:
    299  1.14    thomas 		if (csr & (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
    300  1.14    thomas 		    p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
    301  1.14    thomas 				M_TEMP, M_WAITOK);
    302  1.14    thomas 		    memset(p, '\0', sizeof(struct pci_memreg));
    303  1.14    thomas 		    p->dev = dev;
    304  1.14    thomas 		    p->csr = csr;
    305  1.14    thomas 		    p->tag = tag;
    306  1.14    thomas 		    p->reg = 0;     /* there is no register about this */
    307  1.14    thomas 		    p->size = 0x20000;  /* 128kByte */
    308  1.14    thomas 		    p->mask = 0xfffe0000;
    309  1.14    thomas 		    p->address = 0xa0000;
    310  1.14    thomas 
    311  1.14    thomas 		    insert_into_list(&memlist, p);
    312   1.9       leo 		}
    313   1.9       leo 	}
    314   1.9       leo 
    315  1.14    thomas 	for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; reg += 4) {
    316  1.14    thomas 
    317  1.14    thomas 	    address = pci_conf_read(pc, tag, reg);
    318  1.14    thomas 	    pci_conf_write(pc, tag, reg, 0xffffffff);
    319  1.14    thomas 	    mask    = pci_conf_read(pc, tag, reg);
    320  1.14    thomas 	    pci_conf_write(pc, tag, reg, address);
    321  1.14    thomas 	    if (mask == 0)
    322  1.14    thomas 		continue; /* Register unused */
    323  1.14    thomas 
    324  1.14    thomas 	    p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
    325  1.14    thomas 			M_TEMP, M_WAITOK);
    326  1.14    thomas 	    memset(p, '\0', sizeof(struct pci_memreg));
    327  1.14    thomas 	    p->dev = dev;
    328  1.14    thomas 	    p->csr = csr;
    329  1.14    thomas 	    p->tag = tag;
    330  1.14    thomas 	    p->reg = reg;
    331  1.14    thomas 	    p->mask = mask;
    332  1.14    thomas 	    p->address = 0;
    333  1.14    thomas 
    334  1.14    thomas 	    if (mask & PCI_MAPREG_TYPE_IO) {
    335  1.14    thomas 		p->size = PCI_MAPREG_IO_SIZE(mask);
    336  1.14    thomas 
    337  1.14    thomas 		/*
    338  1.14    thomas 		 * if I/O is already enabled (probably by the console driver)
    339  1.14    thomas 		 * save the address in order to take care about it later.
    340  1.14    thomas 		 */
    341  1.14    thomas 		if (csr & PCI_COMMAND_IO_ENABLE)
    342  1.14    thomas 		    p->address = address;
    343  1.14    thomas 
    344  1.14    thomas 		insert_into_list(&iolist, p);
    345  1.14    thomas 	    } else {
    346  1.14    thomas 		p->size = PCI_MAPREG_MEM_SIZE(mask);
    347  1.14    thomas 
    348  1.14    thomas 		/*
    349  1.14    thomas 		 * if memory is already enabled (probably by the console driver)
    350  1.14    thomas 		 * save the address in order to take care about it later.
    351  1.14    thomas 		 */
    352  1.14    thomas 		if (csr & PCI_COMMAND_MEM_ENABLE)
    353  1.14    thomas 		    p->address = address;
    354  1.14    thomas 
    355  1.14    thomas 		insert_into_list(&memlist, p);
    356   1.9       leo 
    357  1.14    thomas 		if (PCI_MAPREG_MEM_TYPE(mask) == PCI_MAPREG_MEM_TYPE_64BIT)
    358  1.14    thomas 		    reg++;
    359  1.14    thomas 	    }
    360  1.14    thomas 	}
    361   1.9       leo 
    362  1.14    thomas 	/*
    363  1.14    thomas 	 * Both interrupt pin & line are set to the device (== slot)
    364  1.14    thomas 	 * number. This makes sense on the atari because the
    365  1.14    thomas 	 * individual slots are hard-wired to a specific MFP-pin.
    366  1.14    thomas 	 */
    367  1.14    thomas 	csr  = (dev << PCI_INTERRUPT_PIN_SHIFT);
    368  1.14    thomas 	csr |= (dev << PCI_INTERRUPT_LINE_SHIFT);
    369  1.14    thomas 	pci_conf_write(pc, tag, PCI_INTERRUPT_REG, csr);
    370  1.14    thomas     }
    371  1.14    thomas 
    372  1.14    thomas     /*
    373  1.14    thomas      * second step: calculate the memory and I/O adresses beginning from
    374  1.14    thomas      * PCI_MEM_START and PCI_IO_START. Care about already mapped areas.
    375  1.14    thomas      *
    376  1.14    thomas      * beginn with memory list
    377  1.14    thomas      */
    378  1.14    thomas 
    379  1.14    thomas     address = PCI_MEM_START;
    380  1.14    thomas     sizecnt = 0;
    381  1.14    thomas     membase_1m = 0;
    382  1.14    thomas     p = LIST_FIRST(&memlist);
    383  1.14    thomas     while (p != NULL) {
    384  1.14    thomas 	if (!(p->csr & PCI_COMMAND_MEM_ENABLE)) {
    385  1.14    thomas 	    if (PCI_MAPREG_MEM_TYPE(p->mask) == PCI_MAPREG_MEM_TYPE_32BIT_1M) {
    386  1.14    thomas 		if (p->size > membase_1m)
    387  1.14    thomas 		    membase_1m = p->size;
    388  1.14    thomas 		do {
    389  1.14    thomas 		    p->address = membase_1m;
    390  1.14    thomas 		    membase_1m += p->size;
    391  1.14    thomas 		} while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
    392  1.14    thomas 					   p->size, PCI_COMMAND_MEM_ENABLE));
    393  1.14    thomas 		if (membase_1m > 0x00100000) {
    394  1.14    thomas 		    /*
    395  1.14    thomas 		     * Should we panic here?
    396  1.14    thomas 		     */
    397  1.14    thomas 		    printf("\npcibus0: dev %d reg %d: memory not configured",
    398  1.14    thomas 			    p->dev, p->reg);
    399  1.14    thomas 		    p->reg = 0;
    400   1.9       leo 		}
    401  1.14    thomas 	    } else {
    402   1.9       leo 
    403  1.14    thomas 		if (sizecnt && (p->size > sizecnt))
    404  1.14    thomas 		    sizecnt = ((p->size + sizecnt) & p->mask) &
    405  1.14    thomas 			      PCI_MAPREG_MEM_ADDR_MASK;
    406  1.14    thomas 		if (sizecnt > address) {
    407  1.14    thomas 		    address = sizecnt;
    408  1.14    thomas 		    sizecnt = 0;
    409  1.14    thomas 		}
    410   1.9       leo 
    411  1.14    thomas 		do {
    412  1.14    thomas 		    p->address = address + sizecnt;
    413  1.14    thomas 		    sizecnt += p->size;
    414  1.14    thomas 		} while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
    415  1.14    thomas 					   p->size, PCI_COMMAND_MEM_ENABLE));
    416  1.14    thomas 
    417  1.14    thomas 		if ((address + sizecnt) > PCI_MEM_END) {
    418  1.14    thomas 		    /*
    419  1.14    thomas 		     * Should we panic here?
    420  1.14    thomas 		     */
    421  1.14    thomas 		    printf("\npcibus0: dev %d reg %d: memory not configured",
    422  1.14    thomas 			    p->dev, p->reg);
    423  1.14    thomas 		    p->reg = 0;
    424  1.14    thomas 		}
    425  1.14    thomas 	    }
    426  1.14    thomas 	    if (p->reg > 0) {
    427  1.14    thomas 		pci_conf_write(pc, p->tag, p->reg, p->address);
    428  1.14    thomas 		csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
    429  1.14    thomas 		csr |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    430  1.14    thomas 		pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
    431  1.14    thomas 	    }
    432  1.14    thomas 	}
    433  1.14    thomas 	p = LIST_NEXT(p, link);
    434  1.14    thomas     }
    435   1.9       leo 
    436  1.14    thomas     /*
    437  1.14    thomas      * now the I/O list
    438  1.14    thomas      */
    439  1.14    thomas 
    440  1.14    thomas     address = PCI_IO_START;
    441  1.14    thomas     sizecnt = 0;
    442  1.14    thomas     p = LIST_FIRST(&iolist);
    443  1.14    thomas     while (p != NULL) {
    444  1.14    thomas 	if (!(p->csr & PCI_COMMAND_IO_ENABLE)) {
    445  1.14    thomas 
    446  1.14    thomas 	    if (sizecnt && (p->size > sizecnt))
    447  1.14    thomas 		sizecnt = ((p->size + sizecnt) & p->mask) &
    448  1.14    thomas 			  PCI_MAPREG_IO_ADDR_MASK;
    449  1.14    thomas 	    if (sizecnt > address) {
    450  1.14    thomas 		address = sizecnt;
    451  1.14    thomas 		sizecnt = 0;
    452  1.14    thomas 	    }
    453  1.14    thomas 
    454  1.14    thomas 	    do {
    455  1.14    thomas 		p->address = address + sizecnt;
    456  1.14    thomas 		sizecnt += p->size;
    457  1.14    thomas 	    } while (overlap_pci_areas(LIST_FIRST(&iolist), p, p->address,
    458  1.14    thomas 				       p->size, PCI_COMMAND_IO_ENABLE));
    459   1.9       leo 
    460  1.14    thomas 	    if ((address + sizecnt) > PCI_IO_END) {
    461   1.9       leo 		/*
    462  1.14    thomas 		 * Should we panic here?
    463   1.9       leo 		 */
    464  1.14    thomas 		printf("\npcibus0: dev %d reg %d: io not configured",
    465  1.14    thomas 			p->dev, p->reg);
    466  1.14    thomas 	    } else {
    467  1.14    thomas 		pci_conf_write(pc, p->tag, p->reg, p->address);
    468  1.14    thomas 		csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
    469  1.14    thomas 		csr |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    470  1.14    thomas 		pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
    471  1.14    thomas 	    }
    472   1.9       leo 	}
    473  1.14    thomas 	p = LIST_NEXT(p, link);
    474  1.14    thomas     }
    475  1.14    thomas 
    476  1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    477  1.14    thomas     printf("\nI/O List:\n");
    478  1.14    thomas     p = LIST_FIRST(&iolist);
    479  1.14    thomas 
    480  1.14    thomas     while (p != NULL) {
    481  1.14    thomas 	printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
    482  1.14    thomas 			p->reg, p->size, p->address);
    483  1.14    thomas 	p = LIST_NEXT(p, link);
    484  1.14    thomas     }
    485  1.14    thomas     printf("\nMemlist:");
    486  1.14    thomas     p = LIST_FIRST(&memlist);
    487  1.14    thomas 
    488  1.14    thomas     while (p != NULL) {
    489  1.14    thomas 	printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
    490  1.14    thomas 			p->reg, p->size, p->address);
    491  1.14    thomas 	p = LIST_NEXT(p, link);
    492  1.14    thomas     }
    493  1.14    thomas #endif
    494  1.14    thomas 
    495  1.14    thomas     /*
    496  1.14    thomas      * Free the lists
    497  1.14    thomas      */
    498  1.14    thomas     p = LIST_FIRST(&iolist);
    499  1.14    thomas     while (p != NULL) {
    500  1.14    thomas 	q = p;
    501  1.14    thomas 	LIST_REMOVE(q, link);
    502  1.14    thomas 	free(p, M_WAITOK);
    503  1.14    thomas 	p = LIST_FIRST(&iolist);
    504  1.14    thomas     }
    505  1.14    thomas     p = LIST_FIRST(&memlist);
    506  1.14    thomas     while (p != NULL) {
    507  1.14    thomas 	q = p;
    508  1.14    thomas 	LIST_REMOVE(q, link);
    509  1.14    thomas 	free(p, M_WAITOK);
    510  1.14    thomas 	p = LIST_FIRST(&memlist);
    511  1.14    thomas     }
    512   1.9       leo }
    513   1.9       leo 
    514   1.9       leo /*
    515   1.1       leo  * Atari_init.c maps the config areas NBPG bytes apart....
    516   1.1       leo  */
    517   1.1       leo static int pci_config_offset(tag)
    518   1.1       leo pcitag_t	tag;
    519   1.1       leo {
    520   1.1       leo 	int	device;
    521   1.1       leo 
    522   1.1       leo 	device = (tag >> 11) & 0x1f;
    523   1.1       leo 	return(device * NBPG);
    524   1.1       leo }
    525   1.1       leo 
    526   1.1       leo int
    527   1.1       leo pci_bus_maxdevs(pc, busno)
    528   1.1       leo 	pci_chipset_tag_t pc;
    529   1.1       leo 	int busno;
    530   1.1       leo {
    531   1.1       leo 	return (4);
    532   1.1       leo }
    533   1.1       leo 
    534   1.1       leo pcitag_t
    535   1.1       leo pci_make_tag(pc, bus, device, function)
    536   1.1       leo 	pci_chipset_tag_t pc;
    537   1.1       leo 	int bus, device, function;
    538   1.1       leo {
    539   1.1       leo 	return ((bus << 16) | (device << 11) | (function << 8));
    540   1.1       leo }
    541   1.1       leo 
    542   1.1       leo pcireg_t
    543   1.1       leo pci_conf_read(pc, tag, reg)
    544   1.1       leo 	pci_chipset_tag_t pc;
    545   1.1       leo 	pcitag_t tag;
    546   1.1       leo 	int reg;
    547   1.1       leo {
    548   1.1       leo 	u_long	data;
    549   1.1       leo 
    550   1.1       leo 	data = *(u_long *)(pci_conf_addr + pci_config_offset(tag) + reg);
    551   1.9       leo 	return (bswap32(data));
    552   1.1       leo }
    553   1.1       leo 
    554   1.1       leo void
    555   1.1       leo pci_conf_write(pc, tag, reg, data)
    556   1.1       leo 	pci_chipset_tag_t pc;
    557   1.1       leo 	pcitag_t tag;
    558   1.1       leo 	int reg;
    559   1.1       leo 	pcireg_t data;
    560   1.1       leo {
    561   1.9       leo 	*((u_long *)(pci_conf_addr + pci_config_offset(tag) + reg))
    562   1.9       leo 		= bswap32(data);
    563   1.1       leo }
    564   1.1       leo 
    565   1.1       leo int
    566   1.1       leo pci_intr_map(pc, intrtag, pin, line, ihp)
    567   1.1       leo 	pci_chipset_tag_t pc;
    568   1.1       leo 	pcitag_t intrtag;
    569   1.1       leo 	int pin, line;
    570   1.1       leo 	pci_intr_handle_t *ihp;
    571   1.1       leo {
    572   1.9       leo 	/*
    573   1.9       leo 	 * According to the PCI-spec, 255 means `unknown' or `no connection'.
    574   1.9       leo 	 * Interpret this as 'no interrupt assigned'.
    575   1.9       leo 	 */
    576   1.9       leo 	if (line == 255) {
    577   1.9       leo 		*ihp = -1;
    578   1.9       leo 		return 1;
    579   1.9       leo 	}
    580   1.9       leo 
    581   1.9       leo 	/*
    582   1.9       leo 	 * Values are pretty useless because the on the Hades all interrupt
    583   1.9       leo 	 * lines for a card are tied together and hardwired to the TT-MFP
    584   1.9       leo 	 * I/O port.
    585   1.9       leo 	 */
    586   1.9       leo 	*ihp = line;
    587   1.9       leo 	return 0;
    588   1.1       leo }
    589   1.1       leo 
    590   1.1       leo const char *
    591   1.1       leo pci_intr_string(pc, ih)
    592   1.1       leo 	pci_chipset_tag_t pc;
    593   1.1       leo 	pci_intr_handle_t ih;
    594   1.1       leo {
    595   1.1       leo 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    596   1.1       leo 
    597   1.9       leo 	if (ih == -1)
    598   1.1       leo 		panic("pci_intr_string: bogus handle 0x%x\n", ih);
    599   1.1       leo 
    600   1.3  christos 	sprintf(irqstr, "irq %d", ih);
    601   1.1       leo 	return (irqstr);
    602   1.1       leo 
    603   1.1       leo }
    604   1.1       leo 
    605   1.9       leo /*
    606   1.9       leo  * The interrupt stuff is rather ugly. On the Hades, all interrupt lines
    607   1.9       leo  * for a slot are wired together and connected to IO 0,1,2 or 5 (slots:
    608   1.9       leo  * (0-3) on the TT-MFP. The Pci-config code initializes the irq. number
    609   1.9       leo  * to the slot position.
    610   1.9       leo  */
    611   1.9       leo static pci_intr_info_t iinfo[4] = { { -1 }, { -1 }, { -1 }, { -1 } };
    612   1.9       leo 
    613   1.9       leo static int	iifun __P((int, int));
    614   1.9       leo 
    615   1.9       leo static int
    616   1.9       leo iifun(slot, sr)
    617   1.9       leo int	slot;
    618   1.9       leo int	sr;
    619   1.9       leo {
    620   1.9       leo 	pci_intr_info_t *iinfo_p;
    621   1.9       leo 	int		s;
    622   1.9       leo 
    623   1.9       leo 	iinfo_p = &iinfo[slot];
    624   1.9       leo 
    625   1.9       leo 	/*
    626   1.9       leo 	 * Disable the interrupts
    627   1.9       leo 	 */
    628   1.9       leo 	MFP2->mf_imrb  &= ~iinfo_p->imask;
    629   1.9       leo 
    630  1.12       leo 	if ((sr & PSL_IPL) >= (iinfo_p->ipl & PSL_IPL)) {
    631   1.9       leo 		/*
    632   1.9       leo 		 * We're running at a too high priority now.
    633   1.9       leo 		 */
    634   1.9       leo 		add_sicallback((si_farg)iifun, (void*)slot, 0);
    635   1.9       leo 	}
    636   1.9       leo 	else {
    637   1.9       leo 		s = splx(iinfo_p->ipl);
    638   1.9       leo 		(void) (iinfo_p->ifunc)(iinfo_p->iarg);
    639   1.9       leo 		splx(s);
    640   1.9       leo 
    641   1.9       leo 		/*
    642   1.9       leo 		 * Re-enable interrupts after handling
    643   1.9       leo 		 */
    644   1.9       leo 		MFP2->mf_imrb |= iinfo_p->imask;
    645   1.9       leo 	}
    646   1.9       leo 	return 1;
    647   1.9       leo }
    648   1.9       leo 
    649   1.1       leo void *
    650   1.9       leo pci_intr_establish(pc, ih, level, ih_fun, ih_arg)
    651   1.9       leo 	pci_chipset_tag_t	pc;
    652   1.9       leo 	pci_intr_handle_t	ih;
    653   1.9       leo 	int			level;
    654   1.9       leo 	int			(*ih_fun) __P((void *));
    655   1.9       leo 	void			*ih_arg;
    656   1.9       leo {
    657   1.9       leo 	pci_intr_info_t *iinfo_p;
    658   1.9       leo 	struct intrhand	*ihand;
    659   1.9       leo 	int		slot;
    660   1.9       leo 
    661   1.9       leo 	slot    = ih;
    662   1.9       leo 	iinfo_p = &iinfo[slot];
    663   1.9       leo 
    664   1.9       leo 	if (iinfo_p->ipl > 0)
    665   1.9       leo 	    panic("pci_intr_establish: interrupt was already established\n");
    666   1.9       leo 
    667   1.9       leo 	ihand = intr_establish((slot == 3) ? 23 : 16 + slot, USER_VEC, 0,
    668   1.9       leo 				(hw_ifun_t)iifun, (void *)slot);
    669   1.9       leo 	if (ihand != NULL) {
    670   1.9       leo 		iinfo_p->ipl   = level;
    671   1.9       leo 		iinfo_p->imask = (slot == 3) ? 0x80 : (0x01 << slot);
    672   1.9       leo 		iinfo_p->ifunc = ih_fun;
    673   1.9       leo 		iinfo_p->iarg  = ih_arg;
    674   1.9       leo 		iinfo_p->ihand = ihand;
    675   1.9       leo 
    676   1.9       leo 		/*
    677   1.9       leo 		 * Enable (unmask) the interrupt
    678   1.9       leo 		 */
    679   1.9       leo 		MFP2->mf_imrb |= iinfo_p->imask;
    680   1.9       leo 		MFP2->mf_ierb |= iinfo_p->imask;
    681   1.9       leo 		return(iinfo_p);
    682   1.9       leo 	}
    683   1.1       leo 	return NULL;
    684   1.1       leo }
    685   1.1       leo 
    686   1.1       leo void
    687   1.1       leo pci_intr_disestablish(pc, cookie)
    688   1.1       leo 	pci_chipset_tag_t pc;
    689   1.1       leo 	void *cookie;
    690   1.1       leo {
    691   1.9       leo 	pci_intr_info_t *iinfo_p = (pci_intr_info_t *)cookie;
    692   1.9       leo 
    693   1.9       leo 	if (iinfo->ipl < 0)
    694   1.9       leo 	    panic("pci_intr_disestablish: interrupt was not established\n");
    695   1.9       leo 
    696   1.9       leo 	MFP2->mf_imrb &= ~iinfo->imask;
    697   1.9       leo 	MFP2->mf_ierb &= ~iinfo->imask;
    698   1.9       leo 	(void) intr_disestablish(iinfo_p->ihand);
    699   1.9       leo 	iinfo_p->ipl = -1;
    700   1.1       leo }
    701