Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.27
      1  1.27       leo /*	$NetBSD: pci_machdep.c,v 1.27 2000/09/28 07:26:49 leo 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.24       mrg #include <uvm/uvm_extern.h>
     43   1.1       leo 
     44   1.1       leo #include <dev/pci/pcivar.h>
     45   1.1       leo #include <dev/pci/pcireg.h>
     46   1.1       leo 
     47   1.1       leo #include <machine/cpu.h>
     48   1.1       leo #include <machine/iomap.h>
     49   1.9       leo #include <machine/mfp.h>
     50  1.16       leo #include <machine/bswap.h>
     51  1.10       leo #include <machine/bus.h>
     52  1.10       leo 
     53   1.1       leo #include <atari/atari/device.h>
     54  1.17       leo #include <atari/pci/pci_vga.h>
     55   1.1       leo 
     56   1.9       leo /*
     57  1.14    thomas  * Sizes of pci memory and I/O area.
     58   1.9       leo  */
     59  1.14    thomas #define PCI_MEM_END     0x10000000      /* 256 MByte */
     60  1.14    thomas #define PCI_IO_END      0x10000000      /* 256 MByte */
     61  1.14    thomas 
     62  1.14    thomas /*
     63  1.14    thomas  * We preserve some space at the begin of the pci area for 32BIT_1M
     64  1.14    thomas  * devices and standard vga.
     65  1.14    thomas  */
     66  1.14    thomas #define PCI_MEM_START   0x00100000      /*   1 MByte */
     67  1.15    thomas #define PCI_IO_START    0x00004000      /*  16 kByte (some PCI cards allow only
     68  1.25       leo 					    I/O addresses up to 0xffff) */
     69  1.20    thomas 
     70  1.20    thomas /*
     71  1.20    thomas  * PCI memory and IO should be aligned acording to this masks
     72  1.20    thomas  */
     73  1.20    thomas #define PCI_MACHDEP_IO_ALIGN_MASK	0xffffff00
     74  1.20    thomas #define PCI_MACHDEP_MEM_ALIGN_MASK	0xfffff000
     75  1.20    thomas 
     76  1.19       leo /*
     77  1.19       leo  * Convert a PCI 'device' number to a slot number.
     78  1.19       leo  */
     79  1.19       leo #define	DEV2SLOT(dev)	(3 - dev)
     80  1.14    thomas 
     81  1.14    thomas /*
     82  1.14    thomas  * Struct to hold the memory and I/O datas of the pci devices
     83  1.14    thomas  */
     84  1.14    thomas struct pci_memreg {
     85  1.14    thomas     LIST_ENTRY(pci_memreg) link;
     86  1.14    thomas     int dev;
     87  1.14    thomas     pcitag_t tag;
     88  1.14    thomas     pcireg_t reg, address, mask;
     89  1.14    thomas     u_int32_t size;
     90  1.14    thomas     u_int32_t csr;
     91  1.14    thomas };
     92  1.14    thomas 
     93  1.14    thomas typedef LIST_HEAD(pci_memreg_head, pci_memreg) PCI_MEMREG;
     94   1.9       leo 
     95   1.1       leo int	pcibusprint __P((void *auxp, const char *));
     96   1.5       leo int	pcibusmatch __P((struct device *, struct cfdata *, void *));
     97   1.1       leo void	pcibusattach __P((struct device *, struct device *, void *));
     98   1.1       leo 
     99  1.14    thomas static void enable_pci_devices __P((void));
    100  1.14    thomas static void insert_into_list __P((PCI_MEMREG *head, struct pci_memreg *elem));
    101  1.14    thomas static int overlap_pci_areas __P((struct pci_memreg *p,
    102  1.14    thomas 	struct pci_memreg *self, u_int addr, u_int size, u_int what));
    103   1.1       leo static int pci_config_offset __P((pcitag_t));
    104   1.1       leo 
    105   1.1       leo struct cfattach pcibus_ca = {
    106   1.1       leo 	sizeof(struct device), pcibusmatch, pcibusattach
    107   1.1       leo };
    108   1.1       leo 
    109  1.27       leo /*
    110  1.27       leo  * We need some static storage to probe pci-busses for VGA cards during
    111  1.27       leo  * early console init.
    112  1.27       leo  */
    113  1.27       leo static struct atari_bus_space	bs_storage[2];	/* 1 iot, 1 memt */
    114  1.27       leo 
    115   1.1       leo int
    116   1.5       leo pcibusmatch(pdp, cfp, auxp)
    117   1.1       leo struct device	*pdp;
    118   1.5       leo struct cfdata	*cfp;
    119   1.5       leo void		*auxp;
    120   1.1       leo {
    121  1.26       leo 	static int	nmatched = 0;
    122  1.26       leo 
    123  1.26       leo 	if (strcmp((char *)auxp, "pcibus"))
    124  1.26       leo 		return (0);	/* Wrong number... */
    125  1.26       leo 
    126  1.27       leo 	if(atari_realconfig == 0)
    127  1.27       leo 		return (1);
    128  1.27       leo 
    129  1.26       leo 	if (machineid & ATARI_HADES) {
    130  1.26       leo 		/*
    131  1.26       leo 		 * The Hades has only one pci bus
    132  1.26       leo 		 */
    133  1.26       leo 		if (nmatched)
    134  1.26       leo 			return (0);
    135  1.26       leo 		nmatched++;
    136  1.26       leo 		return (1);
    137  1.26       leo 	}
    138  1.26       leo 	return (0);
    139   1.1       leo }
    140   1.1       leo 
    141   1.1       leo void
    142   1.1       leo pcibusattach(pdp, dp, auxp)
    143   1.1       leo struct device	*pdp, *dp;
    144   1.1       leo void		*auxp;
    145   1.1       leo {
    146   1.1       leo 	struct pcibus_attach_args	pba;
    147   1.1       leo 
    148   1.1       leo 	pba.pba_busname = "pci";
    149   1.4       leo 	pba.pba_pc      = NULL;
    150   1.1       leo 	pba.pba_bus     = 0;
    151   1.7       cgd 	pba.pba_flags	= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    152  1.10       leo 	pba.pba_dmat	= BUS_PCI_DMA_TAG;
    153  1.27       leo 	pba.pba_iot     = leb_alloc_bus_space_tag(&bs_storage[0]);
    154  1.27       leo 	pba.pba_memt    = leb_alloc_bus_space_tag(&bs_storage[0]);
    155  1.11       leo 	if ((pba.pba_iot == NULL) || (pba.pba_memt == NULL)) {
    156  1.11       leo 		printf("leb_alloc_bus_space_tag failed!\n");
    157  1.11       leo 		return;
    158  1.11       leo 	}
    159  1.11       leo 	pba.pba_iot->base  = PCI_IO_PHYS;
    160  1.11       leo 	pba.pba_memt->base = PCI_MEM_PHYS;
    161   1.6       leo 
    162  1.27       leo 	if (dp == NULL) {
    163  1.27       leo 		/*
    164  1.27       leo 		 * Scan the bus for a VGA-card that we support. If we
    165  1.27       leo 		 * find one, try to initialize it to a 'standard' text
    166  1.27       leo 		 * mode (80x25).
    167  1.27       leo 		 */
    168  1.27       leo 		check_for_vga();
    169  1.27       leo 		return;
    170  1.27       leo 	}
    171  1.27       leo 
    172  1.27       leo 	enable_pci_devices();
    173  1.27       leo 
    174   1.9       leo 	MFP2->mf_aer &= ~(0x27); /* PCI interrupts: HIGH -> LOW */
    175   1.9       leo 
    176   1.6       leo 	printf("\n");
    177   1.1       leo 
    178   1.1       leo 	config_found(dp, &pba, pcibusprint);
    179   1.1       leo }
    180   1.1       leo 
    181   1.1       leo int
    182   1.1       leo pcibusprint(auxp, name)
    183   1.1       leo void		*auxp;
    184   1.1       leo const char	*name;
    185   1.1       leo {
    186   1.1       leo 	if(name == NULL)
    187   1.1       leo 		return(UNCONF);
    188   1.1       leo 	return(QUIET);
    189   1.1       leo }
    190   1.1       leo 
    191   1.1       leo void
    192   1.1       leo pci_attach_hook(parent, self, pba)
    193   1.1       leo 	struct device *parent, *self;
    194   1.1       leo 	struct pcibus_attach_args *pba;
    195   1.1       leo {
    196   1.1       leo }
    197   1.1       leo 
    198   1.1       leo /*
    199   1.9       leo  * Initialize the PCI-bus. The Atari-BIOS does not do this, so....
    200  1.14    thomas  * We only disable all devices here. Memory and I/O enabling is done
    201  1.14    thomas  * later at pcibusattach.
    202   1.9       leo  */
    203   1.9       leo void
    204   1.9       leo init_pci_bus()
    205   1.9       leo {
    206   1.9       leo 	pci_chipset_tag_t	pc = NULL; /* XXX */
    207   1.9       leo 	pcitag_t		tag;
    208  1.14    thomas 	pcireg_t		csr;
    209  1.14    thomas 	int			device, id, maxndevs;
    210   1.9       leo 
    211  1.14    thomas 	tag   = 0;
    212  1.14    thomas 	id    = 0;
    213   1.9       leo 
    214   1.9       leo 	maxndevs = pci_bus_maxdevs(pc, 0);
    215   1.9       leo 
    216   1.9       leo 	for (device = 0; device < maxndevs; device++) {
    217   1.9       leo 
    218   1.9       leo 		tag = pci_make_tag(pc, 0, device, 0);
    219   1.9       leo 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
    220   1.9       leo 		if (id == 0 || id == 0xffffffff)
    221   1.9       leo 			continue;
    222   1.9       leo 
    223  1.14    thomas 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    224  1.14    thomas 		csr &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
    225  1.14    thomas 		csr &= ~PCI_COMMAND_MASTER_ENABLE;
    226  1.14    thomas 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    227  1.14    thomas 	}
    228  1.14    thomas }
    229  1.14    thomas 
    230  1.14    thomas /*
    231  1.14    thomas  * insert a new element in an existing list that the ID's (size in struct
    232  1.14    thomas  * pci_memreg) are sorted.
    233  1.14    thomas  */
    234  1.14    thomas static void
    235  1.14    thomas insert_into_list(head, elem)
    236  1.14    thomas     PCI_MEMREG *head;
    237  1.14    thomas     struct pci_memreg *elem;
    238  1.14    thomas {
    239  1.14    thomas     struct pci_memreg *p, *q;
    240  1.14    thomas 
    241  1.14    thomas     p = LIST_FIRST(head);
    242  1.14    thomas     q = NULL;
    243  1.14    thomas 
    244  1.14    thomas     for (; p != NULL && p->size < elem->size; q = p, p = LIST_NEXT(p, link));
    245  1.14    thomas 
    246  1.14    thomas     if (q == NULL) {
    247  1.14    thomas 	LIST_INSERT_HEAD(head, elem, link);
    248  1.14    thomas     } else {
    249  1.14    thomas 	LIST_INSERT_AFTER(q, elem, link);
    250  1.14    thomas     }
    251  1.14    thomas }
    252  1.14    thomas 
    253  1.14    thomas /*
    254  1.14    thomas  * Test if a new selected area overlaps with an already (probably preselected)
    255  1.14    thomas  * pci area.
    256  1.14    thomas  */
    257  1.14    thomas static int
    258  1.14    thomas overlap_pci_areas(p, self, addr, size, what)
    259  1.14    thomas     struct pci_memreg *p, *self;
    260  1.14    thomas     u_int addr, size, what;
    261  1.14    thomas {
    262  1.14    thomas     struct pci_memreg *q;
    263  1.14    thomas 
    264  1.14    thomas     if (p == NULL)
    265  1.14    thomas 	return 0;
    266  1.14    thomas 
    267  1.14    thomas     q = p;
    268  1.14    thomas     while (q != NULL) {
    269  1.14    thomas 	if ((q != self) && (q->csr & what)) {
    270  1.14    thomas 	    if ((addr >= q->address) && (addr < (q->address + q->size))) {
    271  1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    272  1.14    thomas 		printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
    273  1.14    thomas 			self->dev, self->reg, q->dev, q->reg);
    274  1.14    thomas #endif
    275  1.14    thomas 		return 1;
    276  1.14    thomas 	    }
    277  1.14    thomas 	    if ((q->address >= addr) && (q->address < (addr + size))) {
    278  1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    279  1.14    thomas 		printf("\noverlap area dev %d reg 0x%02x with dev %d reg 0x%02x",
    280  1.14    thomas 			self->dev, self->reg, q->dev, q->reg);
    281  1.14    thomas #endif
    282  1.14    thomas 		return 1;
    283  1.14    thomas 	    }
    284  1.14    thomas 	}
    285  1.14    thomas 	q = LIST_NEXT(q, link);
    286  1.14    thomas     }
    287  1.14    thomas     return 0;
    288  1.14    thomas }
    289  1.14    thomas 
    290  1.14    thomas /*
    291  1.14    thomas  * Enable memory and I/O on pci devices. Care about already enabled devices
    292  1.14    thomas  * (probabaly by the console driver).
    293  1.14    thomas  *
    294  1.14    thomas  * The idea behind the following code is:
    295  1.14    thomas  * We build a by sizes sorted list of the requirements of the different
    296  1.14    thomas  * pci devices. After that we choose the start addresses of that areas
    297  1.14    thomas  * in such a way that they are placed as closed as possible together.
    298  1.14    thomas  */
    299  1.14    thomas static void
    300  1.14    thomas enable_pci_devices()
    301  1.14    thomas {
    302  1.14    thomas     PCI_MEMREG memlist;
    303  1.14    thomas     PCI_MEMREG iolist;
    304  1.14    thomas     struct pci_memreg *p, *q;
    305  1.14    thomas     int dev, reg, id, class;
    306  1.14    thomas     pcitag_t tag;
    307  1.14    thomas     pcireg_t csr, address, mask;
    308  1.14    thomas     pci_chipset_tag_t pc;
    309  1.14    thomas     int sizecnt, membase_1m;
    310  1.14    thomas 
    311  1.14    thomas     pc = 0;
    312  1.14    thomas     csr = 0;
    313  1.14    thomas     tag = 0;
    314  1.14    thomas 
    315  1.14    thomas     LIST_INIT(&memlist);
    316  1.14    thomas     LIST_INIT(&iolist);
    317  1.14    thomas 
    318  1.14    thomas     /*
    319  1.14    thomas      * first step: go through all devices and gather memory and I/O
    320  1.14    thomas      * sizes
    321  1.14    thomas      */
    322  1.14    thomas     for (dev = 0; dev < pci_bus_maxdevs(pc,0); dev++) {
    323  1.14    thomas 
    324  1.14    thomas 	tag = pci_make_tag(pc, 0, dev, 0);
    325  1.14    thomas 	id  = pci_conf_read(pc, tag, PCI_ID_REG);
    326  1.14    thomas 	if (id == 0 || id == 0xffffffff)
    327  1.14    thomas 	    continue;
    328  1.14    thomas 
    329  1.14    thomas 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    330  1.14    thomas 
    331  1.14    thomas 	/*
    332  1.14    thomas 	 * special case: if a display card is found and memory is enabled
    333  1.14    thomas 	 * preserve 128k at 0xa0000 as vga memory.
    334  1.18       leo 	 * XXX: if a display card is found without being enabled, leave
    335  1.18       leo 	 *      it alone! You will usually only create conflicts by enabeling
    336  1.18       leo 	 *      it.
    337  1.14    thomas 	 */
    338  1.14    thomas 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    339  1.14    thomas 	switch (PCI_CLASS(class)) {
    340  1.14    thomas 	    case PCI_CLASS_PREHISTORIC:
    341  1.14    thomas 	    case PCI_CLASS_DISPLAY:
    342  1.18       leo 	      if (csr & (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
    343  1.14    thomas 		    p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
    344  1.14    thomas 				M_TEMP, M_WAITOK);
    345  1.14    thomas 		    memset(p, '\0', sizeof(struct pci_memreg));
    346  1.14    thomas 		    p->dev = dev;
    347  1.14    thomas 		    p->csr = csr;
    348  1.14    thomas 		    p->tag = tag;
    349  1.14    thomas 		    p->reg = 0;     /* there is no register about this */
    350  1.14    thomas 		    p->size = 0x20000;  /* 128kByte */
    351  1.14    thomas 		    p->mask = 0xfffe0000;
    352  1.14    thomas 		    p->address = 0xa0000;
    353  1.14    thomas 
    354  1.14    thomas 		    insert_into_list(&memlist, p);
    355  1.18       leo 	      }
    356  1.18       leo 	      else continue;
    357   1.9       leo 	}
    358   1.9       leo 
    359  1.14    thomas 	for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; reg += 4) {
    360  1.14    thomas 
    361  1.14    thomas 	    address = pci_conf_read(pc, tag, reg);
    362  1.14    thomas 	    pci_conf_write(pc, tag, reg, 0xffffffff);
    363  1.14    thomas 	    mask    = pci_conf_read(pc, tag, reg);
    364  1.14    thomas 	    pci_conf_write(pc, tag, reg, address);
    365  1.14    thomas 	    if (mask == 0)
    366  1.14    thomas 		continue; /* Register unused */
    367  1.14    thomas 
    368  1.14    thomas 	    p = (struct pci_memreg *)malloc(sizeof(struct pci_memreg),
    369  1.14    thomas 			M_TEMP, M_WAITOK);
    370  1.14    thomas 	    memset(p, '\0', sizeof(struct pci_memreg));
    371  1.14    thomas 	    p->dev = dev;
    372  1.14    thomas 	    p->csr = csr;
    373  1.14    thomas 	    p->tag = tag;
    374  1.14    thomas 	    p->reg = reg;
    375  1.14    thomas 	    p->mask = mask;
    376  1.14    thomas 	    p->address = 0;
    377  1.14    thomas 
    378  1.14    thomas 	    if (mask & PCI_MAPREG_TYPE_IO) {
    379  1.14    thomas 		p->size = PCI_MAPREG_IO_SIZE(mask);
    380  1.14    thomas 
    381  1.14    thomas 		/*
    382  1.20    thomas 		 * Align IO if necessary
    383  1.20    thomas 		 */
    384  1.20    thomas 		if (p->size < PCI_MAPREG_IO_SIZE(PCI_MACHDEP_IO_ALIGN_MASK)) {
    385  1.20    thomas 		    p->mask = PCI_MACHDEP_IO_ALIGN_MASK;
    386  1.20    thomas 		    p->size = PCI_MAPREG_IO_SIZE(p->mask);
    387  1.20    thomas 		}
    388  1.20    thomas 
    389  1.20    thomas 		/*
    390  1.14    thomas 		 * if I/O is already enabled (probably by the console driver)
    391  1.14    thomas 		 * save the address in order to take care about it later.
    392  1.14    thomas 		 */
    393  1.14    thomas 		if (csr & PCI_COMMAND_IO_ENABLE)
    394  1.14    thomas 		    p->address = address;
    395  1.14    thomas 
    396  1.14    thomas 		insert_into_list(&iolist, p);
    397  1.14    thomas 	    } else {
    398  1.14    thomas 		p->size = PCI_MAPREG_MEM_SIZE(mask);
    399  1.14    thomas 
    400  1.14    thomas 		/*
    401  1.20    thomas 		 * Align memory if necessary
    402  1.20    thomas 		 */
    403  1.20    thomas 		if (p->size < PCI_MAPREG_IO_SIZE(PCI_MACHDEP_MEM_ALIGN_MASK)) {
    404  1.20    thomas 		    p->mask = PCI_MACHDEP_MEM_ALIGN_MASK;
    405  1.20    thomas 		    p->size = PCI_MAPREG_MEM_SIZE(p->mask);
    406  1.20    thomas 		}
    407  1.20    thomas 
    408  1.20    thomas 		/*
    409  1.14    thomas 		 * if memory is already enabled (probably by the console driver)
    410  1.14    thomas 		 * save the address in order to take care about it later.
    411  1.14    thomas 		 */
    412  1.14    thomas 		if (csr & PCI_COMMAND_MEM_ENABLE)
    413  1.14    thomas 		    p->address = address;
    414  1.14    thomas 
    415  1.14    thomas 		insert_into_list(&memlist, p);
    416   1.9       leo 
    417  1.14    thomas 		if (PCI_MAPREG_MEM_TYPE(mask) == PCI_MAPREG_MEM_TYPE_64BIT)
    418  1.14    thomas 		    reg++;
    419  1.14    thomas 	    }
    420  1.14    thomas 	}
    421   1.9       leo 
    422  1.14    thomas 	/*
    423  1.14    thomas 	 * Both interrupt pin & line are set to the device (== slot)
    424  1.14    thomas 	 * number. This makes sense on the atari because the
    425  1.14    thomas 	 * individual slots are hard-wired to a specific MFP-pin.
    426  1.14    thomas 	 */
    427  1.19       leo 	csr  = (DEV2SLOT(dev) << PCI_INTERRUPT_PIN_SHIFT);
    428  1.19       leo 	csr |= (DEV2SLOT(dev) << PCI_INTERRUPT_LINE_SHIFT);
    429  1.14    thomas 	pci_conf_write(pc, tag, PCI_INTERRUPT_REG, csr);
    430  1.14    thomas     }
    431  1.14    thomas 
    432  1.14    thomas     /*
    433  1.14    thomas      * second step: calculate the memory and I/O adresses beginning from
    434  1.14    thomas      * PCI_MEM_START and PCI_IO_START. Care about already mapped areas.
    435  1.14    thomas      *
    436  1.25       leo      * begin with memory list
    437  1.14    thomas      */
    438  1.14    thomas 
    439  1.14    thomas     address = PCI_MEM_START;
    440  1.14    thomas     sizecnt = 0;
    441  1.14    thomas     membase_1m = 0;
    442  1.14    thomas     p = LIST_FIRST(&memlist);
    443  1.14    thomas     while (p != NULL) {
    444  1.14    thomas 	if (!(p->csr & PCI_COMMAND_MEM_ENABLE)) {
    445  1.14    thomas 	    if (PCI_MAPREG_MEM_TYPE(p->mask) == PCI_MAPREG_MEM_TYPE_32BIT_1M) {
    446  1.14    thomas 		if (p->size > membase_1m)
    447  1.14    thomas 		    membase_1m = p->size;
    448  1.14    thomas 		do {
    449  1.14    thomas 		    p->address = membase_1m;
    450  1.14    thomas 		    membase_1m += p->size;
    451  1.14    thomas 		} while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
    452  1.14    thomas 					   p->size, PCI_COMMAND_MEM_ENABLE));
    453  1.14    thomas 		if (membase_1m > 0x00100000) {
    454  1.14    thomas 		    /*
    455  1.14    thomas 		     * Should we panic here?
    456  1.14    thomas 		     */
    457  1.14    thomas 		    printf("\npcibus0: dev %d reg %d: memory not configured",
    458  1.14    thomas 			    p->dev, p->reg);
    459  1.14    thomas 		    p->reg = 0;
    460   1.9       leo 		}
    461  1.14    thomas 	    } else {
    462   1.9       leo 
    463  1.14    thomas 		if (sizecnt && (p->size > sizecnt))
    464  1.14    thomas 		    sizecnt = ((p->size + sizecnt) & p->mask) &
    465  1.14    thomas 			      PCI_MAPREG_MEM_ADDR_MASK;
    466  1.14    thomas 		if (sizecnt > address) {
    467  1.14    thomas 		    address = sizecnt;
    468  1.14    thomas 		    sizecnt = 0;
    469  1.14    thomas 		}
    470   1.9       leo 
    471  1.14    thomas 		do {
    472  1.14    thomas 		    p->address = address + sizecnt;
    473  1.14    thomas 		    sizecnt += p->size;
    474  1.14    thomas 		} while (overlap_pci_areas(LIST_FIRST(&memlist), p, p->address,
    475  1.14    thomas 					   p->size, PCI_COMMAND_MEM_ENABLE));
    476  1.14    thomas 
    477  1.14    thomas 		if ((address + sizecnt) > PCI_MEM_END) {
    478  1.14    thomas 		    /*
    479  1.14    thomas 		     * Should we panic here?
    480  1.14    thomas 		     */
    481  1.14    thomas 		    printf("\npcibus0: dev %d reg %d: memory not configured",
    482  1.14    thomas 			    p->dev, p->reg);
    483  1.14    thomas 		    p->reg = 0;
    484  1.14    thomas 		}
    485  1.14    thomas 	    }
    486  1.14    thomas 	    if (p->reg > 0) {
    487  1.14    thomas 		pci_conf_write(pc, p->tag, p->reg, p->address);
    488  1.14    thomas 		csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
    489  1.14    thomas 		csr |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    490  1.14    thomas 		pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
    491  1.20    thomas 		p->csr = csr;
    492  1.14    thomas 	    }
    493  1.14    thomas 	}
    494  1.14    thomas 	p = LIST_NEXT(p, link);
    495  1.14    thomas     }
    496   1.9       leo 
    497  1.14    thomas     /*
    498  1.14    thomas      * now the I/O list
    499  1.14    thomas      */
    500  1.14    thomas 
    501  1.14    thomas     address = PCI_IO_START;
    502  1.14    thomas     sizecnt = 0;
    503  1.14    thomas     p = LIST_FIRST(&iolist);
    504  1.14    thomas     while (p != NULL) {
    505  1.14    thomas 	if (!(p->csr & PCI_COMMAND_IO_ENABLE)) {
    506  1.14    thomas 
    507  1.14    thomas 	    if (sizecnt && (p->size > sizecnt))
    508  1.14    thomas 		sizecnt = ((p->size + sizecnt) & p->mask) &
    509  1.14    thomas 			  PCI_MAPREG_IO_ADDR_MASK;
    510  1.14    thomas 	    if (sizecnt > address) {
    511  1.14    thomas 		address = sizecnt;
    512  1.14    thomas 		sizecnt = 0;
    513  1.14    thomas 	    }
    514  1.14    thomas 
    515  1.14    thomas 	    do {
    516  1.14    thomas 		p->address = address + sizecnt;
    517  1.14    thomas 		sizecnt += p->size;
    518  1.14    thomas 	    } while (overlap_pci_areas(LIST_FIRST(&iolist), p, p->address,
    519  1.14    thomas 				       p->size, PCI_COMMAND_IO_ENABLE));
    520   1.9       leo 
    521  1.14    thomas 	    if ((address + sizecnt) > PCI_IO_END) {
    522   1.9       leo 		/*
    523  1.14    thomas 		 * Should we panic here?
    524   1.9       leo 		 */
    525  1.14    thomas 		printf("\npcibus0: dev %d reg %d: io not configured",
    526  1.14    thomas 			p->dev, p->reg);
    527  1.14    thomas 	    } else {
    528  1.14    thomas 		pci_conf_write(pc, p->tag, p->reg, p->address);
    529  1.14    thomas 		csr = pci_conf_read(pc, p->tag, PCI_COMMAND_STATUS_REG);
    530  1.14    thomas 		csr |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    531  1.14    thomas 		pci_conf_write(pc, p->tag, PCI_COMMAND_STATUS_REG, csr);
    532  1.20    thomas 		p->csr = csr;
    533  1.14    thomas 	    }
    534   1.9       leo 	}
    535  1.14    thomas 	p = LIST_NEXT(p, link);
    536  1.14    thomas     }
    537  1.14    thomas 
    538  1.14    thomas #ifdef DEBUG_PCI_MACHDEP
    539  1.14    thomas     printf("\nI/O List:\n");
    540  1.14    thomas     p = LIST_FIRST(&iolist);
    541  1.14    thomas 
    542  1.14    thomas     while (p != NULL) {
    543  1.14    thomas 	printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
    544  1.14    thomas 			p->reg, p->size, p->address);
    545  1.14    thomas 	p = LIST_NEXT(p, link);
    546  1.14    thomas     }
    547  1.14    thomas     printf("\nMemlist:");
    548  1.14    thomas     p = LIST_FIRST(&memlist);
    549  1.14    thomas 
    550  1.14    thomas     while (p != NULL) {
    551  1.14    thomas 	printf("\ndev: %d, reg: 0x%02x, size: 0x%08x, addr: 0x%08x", p->dev,
    552  1.14    thomas 			p->reg, p->size, p->address);
    553  1.14    thomas 	p = LIST_NEXT(p, link);
    554  1.14    thomas     }
    555  1.14    thomas #endif
    556  1.14    thomas 
    557  1.14    thomas     /*
    558  1.14    thomas      * Free the lists
    559  1.14    thomas      */
    560  1.14    thomas     p = LIST_FIRST(&iolist);
    561  1.14    thomas     while (p != NULL) {
    562  1.14    thomas 	q = p;
    563  1.14    thomas 	LIST_REMOVE(q, link);
    564  1.14    thomas 	free(p, M_WAITOK);
    565  1.14    thomas 	p = LIST_FIRST(&iolist);
    566  1.14    thomas     }
    567  1.14    thomas     p = LIST_FIRST(&memlist);
    568  1.14    thomas     while (p != NULL) {
    569  1.14    thomas 	q = p;
    570  1.14    thomas 	LIST_REMOVE(q, link);
    571  1.14    thomas 	free(p, M_WAITOK);
    572  1.14    thomas 	p = LIST_FIRST(&memlist);
    573  1.14    thomas     }
    574   1.9       leo }
    575   1.9       leo 
    576   1.9       leo /*
    577   1.1       leo  * Atari_init.c maps the config areas NBPG bytes apart....
    578   1.1       leo  */
    579   1.1       leo static int pci_config_offset(tag)
    580   1.1       leo pcitag_t	tag;
    581   1.1       leo {
    582   1.1       leo 	int	device;
    583   1.1       leo 
    584   1.1       leo 	device = (tag >> 11) & 0x1f;
    585   1.1       leo 	return(device * NBPG);
    586   1.1       leo }
    587   1.1       leo 
    588   1.1       leo int
    589   1.1       leo pci_bus_maxdevs(pc, busno)
    590   1.1       leo 	pci_chipset_tag_t pc;
    591   1.1       leo 	int busno;
    592   1.1       leo {
    593   1.1       leo 	return (4);
    594   1.1       leo }
    595   1.1       leo 
    596   1.1       leo pcitag_t
    597   1.1       leo pci_make_tag(pc, bus, device, function)
    598   1.1       leo 	pci_chipset_tag_t pc;
    599   1.1       leo 	int bus, device, function;
    600   1.1       leo {
    601   1.1       leo 	return ((bus << 16) | (device << 11) | (function << 8));
    602   1.1       leo }
    603   1.1       leo 
    604   1.1       leo pcireg_t
    605   1.1       leo pci_conf_read(pc, tag, reg)
    606   1.1       leo 	pci_chipset_tag_t pc;
    607   1.1       leo 	pcitag_t tag;
    608   1.1       leo 	int reg;
    609   1.1       leo {
    610   1.1       leo 	u_long	data;
    611   1.1       leo 
    612   1.1       leo 	data = *(u_long *)(pci_conf_addr + pci_config_offset(tag) + reg);
    613   1.9       leo 	return (bswap32(data));
    614   1.1       leo }
    615   1.1       leo 
    616   1.1       leo void
    617   1.1       leo pci_conf_write(pc, tag, reg, data)
    618   1.1       leo 	pci_chipset_tag_t pc;
    619   1.1       leo 	pcitag_t tag;
    620   1.1       leo 	int reg;
    621   1.1       leo 	pcireg_t data;
    622   1.1       leo {
    623   1.9       leo 	*((u_long *)(pci_conf_addr + pci_config_offset(tag) + reg))
    624   1.9       leo 		= bswap32(data);
    625   1.1       leo }
    626   1.1       leo 
    627   1.1       leo int
    628   1.1       leo pci_intr_map(pc, intrtag, pin, line, ihp)
    629   1.1       leo 	pci_chipset_tag_t pc;
    630   1.1       leo 	pcitag_t intrtag;
    631   1.1       leo 	int pin, line;
    632   1.1       leo 	pci_intr_handle_t *ihp;
    633   1.1       leo {
    634   1.9       leo 	/*
    635   1.9       leo 	 * According to the PCI-spec, 255 means `unknown' or `no connection'.
    636   1.9       leo 	 * Interpret this as 'no interrupt assigned'.
    637   1.9       leo 	 */
    638   1.9       leo 	if (line == 255) {
    639   1.9       leo 		*ihp = -1;
    640   1.9       leo 		return 1;
    641   1.9       leo 	}
    642   1.9       leo 
    643   1.9       leo 	/*
    644   1.9       leo 	 * Values are pretty useless because the on the Hades all interrupt
    645   1.9       leo 	 * lines for a card are tied together and hardwired to the TT-MFP
    646   1.9       leo 	 * I/O port.
    647   1.9       leo 	 */
    648   1.9       leo 	*ihp = line;
    649   1.9       leo 	return 0;
    650   1.1       leo }
    651   1.1       leo 
    652   1.1       leo const char *
    653   1.1       leo pci_intr_string(pc, ih)
    654   1.1       leo 	pci_chipset_tag_t pc;
    655   1.1       leo 	pci_intr_handle_t ih;
    656   1.1       leo {
    657   1.1       leo 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    658   1.1       leo 
    659   1.9       leo 	if (ih == -1)
    660   1.1       leo 		panic("pci_intr_string: bogus handle 0x%x\n", ih);
    661   1.1       leo 
    662   1.3  christos 	sprintf(irqstr, "irq %d", ih);
    663   1.1       leo 	return (irqstr);
    664   1.1       leo 
    665  1.22       cgd }
    666  1.22       cgd 
    667  1.22       cgd const struct evcnt *
    668  1.22       cgd pci_intr_evcnt(pc, ih)
    669  1.22       cgd 	pci_chipset_tag_t pc;
    670  1.22       cgd 	pci_intr_handle_t ih;
    671  1.22       cgd {
    672  1.22       cgd 
    673  1.22       cgd 	/* XXX for now, no evcnt parent reported */
    674  1.22       cgd 	return NULL;
    675   1.1       leo }
    676   1.1       leo 
    677   1.9       leo /*
    678   1.9       leo  * The interrupt stuff is rather ugly. On the Hades, all interrupt lines
    679   1.9       leo  * for a slot are wired together and connected to IO 0,1,2 or 5 (slots:
    680   1.9       leo  * (0-3) on the TT-MFP. The Pci-config code initializes the irq. number
    681   1.9       leo  * to the slot position.
    682   1.9       leo  */
    683   1.9       leo static pci_intr_info_t iinfo[4] = { { -1 }, { -1 }, { -1 }, { -1 } };
    684   1.9       leo 
    685   1.9       leo static int	iifun __P((int, int));
    686   1.9       leo 
    687   1.9       leo static int
    688   1.9       leo iifun(slot, sr)
    689   1.9       leo int	slot;
    690   1.9       leo int	sr;
    691   1.9       leo {
    692   1.9       leo 	pci_intr_info_t *iinfo_p;
    693   1.9       leo 	int		s;
    694   1.9       leo 
    695   1.9       leo 	iinfo_p = &iinfo[slot];
    696   1.9       leo 
    697   1.9       leo 	/*
    698   1.9       leo 	 * Disable the interrupts
    699   1.9       leo 	 */
    700   1.9       leo 	MFP2->mf_imrb  &= ~iinfo_p->imask;
    701   1.9       leo 
    702  1.12       leo 	if ((sr & PSL_IPL) >= (iinfo_p->ipl & PSL_IPL)) {
    703   1.9       leo 		/*
    704   1.9       leo 		 * We're running at a too high priority now.
    705   1.9       leo 		 */
    706   1.9       leo 		add_sicallback((si_farg)iifun, (void*)slot, 0);
    707   1.9       leo 	}
    708   1.9       leo 	else {
    709   1.9       leo 		s = splx(iinfo_p->ipl);
    710   1.9       leo 		(void) (iinfo_p->ifunc)(iinfo_p->iarg);
    711   1.9       leo 		splx(s);
    712   1.9       leo 
    713   1.9       leo 		/*
    714   1.9       leo 		 * Re-enable interrupts after handling
    715   1.9       leo 		 */
    716   1.9       leo 		MFP2->mf_imrb |= iinfo_p->imask;
    717   1.9       leo 	}
    718   1.9       leo 	return 1;
    719   1.9       leo }
    720   1.9       leo 
    721   1.1       leo void *
    722   1.9       leo pci_intr_establish(pc, ih, level, ih_fun, ih_arg)
    723   1.9       leo 	pci_chipset_tag_t	pc;
    724   1.9       leo 	pci_intr_handle_t	ih;
    725   1.9       leo 	int			level;
    726   1.9       leo 	int			(*ih_fun) __P((void *));
    727   1.9       leo 	void			*ih_arg;
    728   1.9       leo {
    729   1.9       leo 	pci_intr_info_t *iinfo_p;
    730   1.9       leo 	struct intrhand	*ihand;
    731   1.9       leo 	int		slot;
    732   1.9       leo 
    733   1.9       leo 	slot    = ih;
    734   1.9       leo 	iinfo_p = &iinfo[slot];
    735   1.9       leo 
    736   1.9       leo 	if (iinfo_p->ipl > 0)
    737   1.9       leo 	    panic("pci_intr_establish: interrupt was already established\n");
    738   1.9       leo 
    739   1.9       leo 	ihand = intr_establish((slot == 3) ? 23 : 16 + slot, USER_VEC, 0,
    740   1.9       leo 				(hw_ifun_t)iifun, (void *)slot);
    741   1.9       leo 	if (ihand != NULL) {
    742   1.9       leo 		iinfo_p->ipl   = level;
    743   1.9       leo 		iinfo_p->imask = (slot == 3) ? 0x80 : (0x01 << slot);
    744   1.9       leo 		iinfo_p->ifunc = ih_fun;
    745   1.9       leo 		iinfo_p->iarg  = ih_arg;
    746   1.9       leo 		iinfo_p->ihand = ihand;
    747   1.9       leo 
    748   1.9       leo 		/*
    749   1.9       leo 		 * Enable (unmask) the interrupt
    750   1.9       leo 		 */
    751   1.9       leo 		MFP2->mf_imrb |= iinfo_p->imask;
    752   1.9       leo 		MFP2->mf_ierb |= iinfo_p->imask;
    753   1.9       leo 		return(iinfo_p);
    754   1.9       leo 	}
    755   1.1       leo 	return NULL;
    756   1.1       leo }
    757   1.1       leo 
    758   1.1       leo void
    759   1.1       leo pci_intr_disestablish(pc, cookie)
    760   1.1       leo 	pci_chipset_tag_t pc;
    761   1.1       leo 	void *cookie;
    762   1.1       leo {
    763   1.9       leo 	pci_intr_info_t *iinfo_p = (pci_intr_info_t *)cookie;
    764   1.9       leo 
    765   1.9       leo 	if (iinfo->ipl < 0)
    766   1.9       leo 	    panic("pci_intr_disestablish: interrupt was not established\n");
    767   1.9       leo 
    768   1.9       leo 	MFP2->mf_imrb &= ~iinfo->imask;
    769   1.9       leo 	MFP2->mf_ierb &= ~iinfo->imask;
    770   1.9       leo 	(void) intr_disestablish(iinfo_p->ihand);
    771   1.9       leo 	iinfo_p->ipl = -1;
    772   1.1       leo }
    773