Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.10
      1  1.10       leo /*	$NetBSD: pci_machdep.c,v 1.10 1998/03/10 11:43:11 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.1       leo  * Copyright (c) 1994 Charles 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.1       leo  *	This product includes software developed by Charles 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.1       leo 
     41   1.1       leo #include <vm/vm.h>
     42   1.1       leo #include <vm/vm_kern.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.10       leo #include <machine/bus.h>
     51  1.10       leo 
     52   1.1       leo #include <atari/atari/device.h>
     53   1.1       leo 
     54   1.9       leo /*
     55   1.9       leo  * I/O and memory we assume 'reserved' when an vga card is detected on
     56   1.9       leo  * the PCI-bus.
     57   1.9       leo  */
     58   1.9       leo #define MAX_VGA_MEM	0x1000000	/* 16 MB mem	*/
     59   1.9       leo #define MAX_VGA_IO	0x0010000	/* 64 KB io	*/
     60   1.9       leo 
     61   1.1       leo int	pcibusprint __P((void *auxp, const char *));
     62   1.5       leo int	pcibusmatch __P((struct device *, struct cfdata *, void *));
     63   1.1       leo void	pcibusattach __P((struct device *, struct device *, void *));
     64   1.1       leo 
     65   1.1       leo static int pci_config_offset __P((pcitag_t));
     66   1.1       leo 
     67   1.1       leo struct cfattach pcibus_ca = {
     68   1.1       leo 	sizeof(struct device), pcibusmatch, pcibusattach
     69   1.1       leo };
     70   1.1       leo 
     71   1.1       leo int
     72   1.5       leo pcibusmatch(pdp, cfp, auxp)
     73   1.1       leo struct device	*pdp;
     74   1.5       leo struct cfdata	*cfp;
     75   1.5       leo void		*auxp;
     76   1.1       leo {
     77   1.1       leo 	if(atari_realconfig == 0)
     78   1.1       leo 		return (0);
     79   1.1       leo 	if (strcmp((char *)auxp, "pcibus") || cfp->cf_unit != 0)
     80   1.1       leo 		return(0);
     81   1.1       leo 	return(machineid & ATARI_HADES ? 1 : 0);
     82   1.1       leo }
     83   1.1       leo 
     84   1.1       leo void
     85   1.1       leo pcibusattach(pdp, dp, auxp)
     86   1.1       leo struct device	*pdp, *dp;
     87   1.1       leo void		*auxp;
     88   1.1       leo {
     89   1.1       leo 	struct pcibus_attach_args	pba;
     90   1.1       leo 
     91   1.1       leo 	pba.pba_busname = "pci";
     92   1.4       leo 	pba.pba_pc      = NULL;
     93   1.1       leo 	pba.pba_bus     = 0;
     94   1.9       leo 	pba.pba_iot     = PCI_IO_PHYS;
     95   1.9       leo 	pba.pba_memt    = PCI_MEM_PHYS;
     96   1.7       cgd 	pba.pba_flags	= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
     97  1.10       leo 	pba.pba_dmat	= BUS_PCI_DMA_TAG;
     98   1.6       leo 
     99   1.9       leo 	MFP2->mf_aer &= ~(0x27); /* PCI interrupts: HIGH -> LOW */
    100   1.9       leo 
    101   1.6       leo 	printf("\n");
    102   1.1       leo 
    103   1.1       leo 	config_found(dp, &pba, pcibusprint);
    104   1.1       leo }
    105   1.1       leo 
    106   1.1       leo int
    107   1.1       leo pcibusprint(auxp, name)
    108   1.1       leo void		*auxp;
    109   1.1       leo const char	*name;
    110   1.1       leo {
    111   1.1       leo 	if(name == NULL)
    112   1.1       leo 		return(UNCONF);
    113   1.1       leo 	return(QUIET);
    114   1.1       leo }
    115   1.1       leo 
    116   1.1       leo void
    117   1.1       leo pci_attach_hook(parent, self, pba)
    118   1.1       leo 	struct device *parent, *self;
    119   1.1       leo 	struct pcibus_attach_args *pba;
    120   1.1       leo {
    121   1.1       leo }
    122   1.1       leo 
    123   1.1       leo /*
    124   1.9       leo  * Initialize the PCI-bus. The Atari-BIOS does not do this, so....
    125   1.9       leo  */
    126   1.9       leo void
    127   1.9       leo init_pci_bus()
    128   1.9       leo {
    129   1.9       leo 	pci_chipset_tag_t	pc = NULL; /* XXX */
    130   1.9       leo 	pcitag_t		tag;
    131   1.9       leo 	pcireg_t		csr, address, mask;
    132   1.9       leo 	int			device, id, class, maxndevs;
    133   1.9       leo 	int			reg;
    134   1.9       leo 	u_int32_t		membase, iobase;
    135   1.9       leo 
    136   1.9       leo 	tag        = 0;
    137   1.9       leo 	id = class = 0;
    138   1.9       leo 
    139   1.9       leo 	membase = iobase = 0;
    140   1.9       leo 
    141   1.9       leo 	maxndevs = pci_bus_maxdevs(pc, 0);
    142   1.9       leo 
    143   1.9       leo 	/*
    144   1.9       leo 	 * Scan the bus for prehistory (usually VGA) devices.
    145   1.9       leo 	 */
    146   1.9       leo 	for (device = 0; device < maxndevs; device++) {
    147   1.9       leo 
    148   1.9       leo 		tag = pci_make_tag(pc, 0, device, 0);
    149   1.9       leo 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
    150   1.9       leo 		if (id == 0 || id == 0xffffffff)
    151   1.9       leo 			continue;
    152   1.9       leo 		class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    153   1.9       leo 		switch (PCI_CLASS(class)) {
    154   1.9       leo 			case PCI_CLASS_PREHISTORIC:
    155   1.9       leo 			case PCI_CLASS_DISPLAY:
    156   1.9       leo 
    157   1.9       leo 				membase = MAX_VGA_MEM;
    158   1.9       leo 				iobase  = MAX_VGA_IO;
    159   1.9       leo 		}
    160   1.9       leo 	}
    161   1.9       leo 
    162   1.9       leo 	for (device = 0; device < maxndevs; device++) {
    163   1.9       leo 
    164   1.9       leo 		tag = pci_make_tag(pc, 0, device, 0);
    165   1.9       leo 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
    166   1.9       leo 		if (id == 0 || id == 0xffffffff)
    167   1.9       leo 			continue;
    168   1.9       leo 
    169   1.9       leo 		class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    170   1.9       leo 		switch (PCI_CLASS(class)) {
    171   1.9       leo 			case PCI_CLASS_PREHISTORIC:
    172   1.9       leo 			case PCI_CLASS_DISPLAY:
    173   1.9       leo 				/*
    174   1.9       leo 				 * XXX: We rely on the BIOS to do the
    175   1.9       leo 				 * right thing here. Eventually, we should
    176   1.9       leo 				 * take the initiative...
    177   1.9       leo 				 */
    178   1.9       leo 				continue;
    179   1.9       leo 		}
    180   1.9       leo 
    181   1.9       leo 		csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    182   1.9       leo 		csr &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
    183   1.9       leo 		csr &= ~PCI_COMMAND_MASTER_ENABLE;
    184   1.9       leo 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    185   1.9       leo 
    186   1.9       leo 		for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; reg += 4) {
    187   1.9       leo 		    int	size, type;
    188   1.9       leo 
    189   1.9       leo 		    address = pci_conf_read(pc, tag, reg);
    190   1.9       leo 		    pci_conf_write(pc, tag, reg, 0xffffffff);
    191   1.9       leo 		    mask    = pci_conf_read(pc, tag, reg);
    192   1.9       leo 		    pci_conf_write(pc, tag, reg, address);
    193   1.9       leo 		    if (mask == 0)
    194   1.9       leo 			continue; /* Register unused */
    195   1.9       leo 
    196   1.9       leo 		    if (mask & PCI_MAPREG_TYPE_IO) {
    197   1.9       leo 			csr |= PCI_COMMAND_IO_ENABLE;
    198   1.9       leo 			address = PCI_MAPREG_IO_ADDR(mask);
    199   1.9       leo 			mask    = (~address << 1) | 1;
    200   1.9       leo 			size    = (mask & address) & 0xffffffff;
    201   1.9       leo 			address = iobase | PCI_MAPREG_TYPE_IO;
    202   1.9       leo 			iobase += roundup(size, 4096); /* XXX */
    203   1.9       leo 		    }
    204   1.9       leo 		    else {
    205   1.9       leo 			type = PCI_MAPREG_MEM_TYPE(address);
    206   1.9       leo 			switch (type) {
    207   1.9       leo 			    case PCI_MAPREG_MEM_TYPE_32BIT:
    208   1.9       leo 				break;
    209   1.9       leo 			    case PCI_MAPREG_MEM_TYPE_64BIT:
    210   1.9       leo 				reg++;
    211   1.9       leo 			    case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    212   1.9       leo 				/*
    213   1.9       leo 				 * XXX: We can do better here!
    214   1.9       leo 				 */
    215   1.9       leo 				if (membase >= 0x100000)
    216   1.9       leo 					continue;
    217   1.9       leo 			}
    218   1.9       leo 			csr |= PCI_COMMAND_MEM_ENABLE;
    219   1.9       leo 			size = PCI_MAPREG_MEM_SIZE(mask);
    220   1.9       leo 			address = membase | PCI_MAPREG_TYPE_MEM;
    221   1.9       leo 			membase += roundup(size, 4096); /* XXX */
    222   1.9       leo 		    }
    223   1.9       leo 		    pci_conf_write(pc, tag, reg, address);
    224   1.9       leo 		}
    225   1.9       leo 		csr |= PCI_COMMAND_MASTER_ENABLE;
    226   1.9       leo 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    227   1.9       leo 
    228   1.9       leo 		/*
    229   1.9       leo 		 * Both interrupt pin & line are set to the device (== slot)
    230   1.9       leo 		 * number. This makes sense on the atari because the
    231   1.9       leo 		 * individual slots are hard-wired to a specific MFP-pin.
    232   1.9       leo 		 */
    233   1.9       leo 		csr  = (device << PCI_INTERRUPT_PIN_SHIFT);
    234   1.9       leo 		csr |= (device << PCI_INTERRUPT_LINE_SHIFT);
    235   1.9       leo 		pci_conf_write(pc, tag, PCI_INTERRUPT_REG, csr);
    236   1.9       leo 	}
    237   1.9       leo }
    238   1.9       leo 
    239   1.9       leo /*
    240   1.1       leo  * Atari_init.c maps the config areas NBPG bytes apart....
    241   1.1       leo  */
    242   1.1       leo static int pci_config_offset(tag)
    243   1.1       leo pcitag_t	tag;
    244   1.1       leo {
    245   1.1       leo 	int	device;
    246   1.1       leo 
    247   1.1       leo 	device = (tag >> 11) & 0x1f;
    248   1.1       leo 	return(device * NBPG);
    249   1.1       leo }
    250   1.1       leo 
    251   1.1       leo int
    252   1.1       leo pci_bus_maxdevs(pc, busno)
    253   1.1       leo 	pci_chipset_tag_t pc;
    254   1.1       leo 	int busno;
    255   1.1       leo {
    256   1.1       leo 	return (4);
    257   1.1       leo }
    258   1.1       leo 
    259   1.1       leo pcitag_t
    260   1.1       leo pci_make_tag(pc, bus, device, function)
    261   1.1       leo 	pci_chipset_tag_t pc;
    262   1.1       leo 	int bus, device, function;
    263   1.1       leo {
    264   1.1       leo 	return ((bus << 16) | (device << 11) | (function << 8));
    265   1.1       leo }
    266   1.1       leo 
    267   1.1       leo pcireg_t
    268   1.1       leo pci_conf_read(pc, tag, reg)
    269   1.1       leo 	pci_chipset_tag_t pc;
    270   1.1       leo 	pcitag_t tag;
    271   1.1       leo 	int reg;
    272   1.1       leo {
    273   1.1       leo 	u_long	data;
    274   1.1       leo 
    275   1.1       leo 	data = *(u_long *)(pci_conf_addr + pci_config_offset(tag) + reg);
    276   1.9       leo 	return (bswap32(data));
    277   1.1       leo }
    278   1.1       leo 
    279   1.1       leo void
    280   1.1       leo pci_conf_write(pc, tag, reg, data)
    281   1.1       leo 	pci_chipset_tag_t pc;
    282   1.1       leo 	pcitag_t tag;
    283   1.1       leo 	int reg;
    284   1.1       leo 	pcireg_t data;
    285   1.1       leo {
    286   1.9       leo 	*((u_long *)(pci_conf_addr + pci_config_offset(tag) + reg))
    287   1.9       leo 		= bswap32(data);
    288   1.1       leo }
    289   1.1       leo 
    290   1.1       leo int
    291   1.1       leo pci_intr_map(pc, intrtag, pin, line, ihp)
    292   1.1       leo 	pci_chipset_tag_t pc;
    293   1.1       leo 	pcitag_t intrtag;
    294   1.1       leo 	int pin, line;
    295   1.1       leo 	pci_intr_handle_t *ihp;
    296   1.1       leo {
    297   1.9       leo 	/*
    298   1.9       leo 	 * According to the PCI-spec, 255 means `unknown' or `no connection'.
    299   1.9       leo 	 * Interpret this as 'no interrupt assigned'.
    300   1.9       leo 	 */
    301   1.9       leo 	if (line == 255) {
    302   1.9       leo 		*ihp = -1;
    303   1.9       leo 		return 1;
    304   1.9       leo 	}
    305   1.9       leo 
    306   1.9       leo 	/*
    307   1.9       leo 	 * Values are pretty useless because the on the Hades all interrupt
    308   1.9       leo 	 * lines for a card are tied together and hardwired to the TT-MFP
    309   1.9       leo 	 * I/O port.
    310   1.9       leo 	 */
    311   1.9       leo 	*ihp = line;
    312   1.9       leo 	return 0;
    313   1.1       leo }
    314   1.1       leo 
    315   1.1       leo const char *
    316   1.1       leo pci_intr_string(pc, ih)
    317   1.1       leo 	pci_chipset_tag_t pc;
    318   1.1       leo 	pci_intr_handle_t ih;
    319   1.1       leo {
    320   1.1       leo 	static char irqstr[8];		/* 4 + 2 + NULL + sanity */
    321   1.1       leo 
    322   1.9       leo 	if (ih == -1)
    323   1.1       leo 		panic("pci_intr_string: bogus handle 0x%x\n", ih);
    324   1.1       leo 
    325   1.3  christos 	sprintf(irqstr, "irq %d", ih);
    326   1.1       leo 	return (irqstr);
    327   1.1       leo 
    328   1.1       leo }
    329   1.1       leo 
    330   1.9       leo /*
    331   1.9       leo  * The interrupt stuff is rather ugly. On the Hades, all interrupt lines
    332   1.9       leo  * for a slot are wired together and connected to IO 0,1,2 or 5 (slots:
    333   1.9       leo  * (0-3) on the TT-MFP. The Pci-config code initializes the irq. number
    334   1.9       leo  * to the slot position.
    335   1.9       leo  */
    336   1.9       leo static pci_intr_info_t iinfo[4] = { { -1 }, { -1 }, { -1 }, { -1 } };
    337   1.9       leo 
    338   1.9       leo static int	iifun __P((int, int));
    339   1.9       leo 
    340   1.9       leo static int
    341   1.9       leo iifun(slot, sr)
    342   1.9       leo int	slot;
    343   1.9       leo int	sr;
    344   1.9       leo {
    345   1.9       leo 	pci_intr_info_t *iinfo_p;
    346   1.9       leo 	int		s;
    347   1.9       leo 
    348   1.9       leo 	iinfo_p = &iinfo[slot];
    349   1.9       leo 
    350   1.9       leo 	/*
    351   1.9       leo 	 * Disable the interrupts
    352   1.9       leo 	 */
    353   1.9       leo 	MFP2->mf_imrb  &= ~iinfo_p->imask;
    354   1.9       leo 
    355   1.9       leo 	if ((sr & PSL_IPL) >= iinfo_p->ipl) {
    356   1.9       leo 		/*
    357   1.9       leo 		 * We're running at a too high priority now.
    358   1.9       leo 		 */
    359   1.9       leo 		add_sicallback((si_farg)iifun, (void*)slot, 0);
    360   1.9       leo 	}
    361   1.9       leo 	else {
    362   1.9       leo 		s = splx(iinfo_p->ipl);
    363   1.9       leo 		(void) (iinfo_p->ifunc)(iinfo_p->iarg);
    364   1.9       leo 		splx(s);
    365   1.9       leo 
    366   1.9       leo 		/*
    367   1.9       leo 		 * Re-enable interrupts after handling
    368   1.9       leo 		 */
    369   1.9       leo 		MFP2->mf_imrb |= iinfo_p->imask;
    370   1.9       leo 	}
    371   1.9       leo 	return 1;
    372   1.9       leo }
    373   1.9       leo 
    374   1.1       leo void *
    375   1.9       leo pci_intr_establish(pc, ih, level, ih_fun, ih_arg)
    376   1.9       leo 	pci_chipset_tag_t	pc;
    377   1.9       leo 	pci_intr_handle_t	ih;
    378   1.9       leo 	int			level;
    379   1.9       leo 	int			(*ih_fun) __P((void *));
    380   1.9       leo 	void			*ih_arg;
    381   1.9       leo {
    382   1.9       leo 	pci_intr_info_t *iinfo_p;
    383   1.9       leo 	struct intrhand	*ihand;
    384   1.9       leo 	int		slot;
    385   1.9       leo 
    386   1.9       leo 	slot    = ih;
    387   1.9       leo 	iinfo_p = &iinfo[slot];
    388   1.9       leo 
    389   1.9       leo 	if (iinfo_p->ipl > 0)
    390   1.9       leo 	    panic("pci_intr_establish: interrupt was already established\n");
    391   1.9       leo 
    392   1.9       leo 	ihand = intr_establish((slot == 3) ? 23 : 16 + slot, USER_VEC, 0,
    393   1.9       leo 				(hw_ifun_t)iifun, (void *)slot);
    394   1.9       leo 	if (ihand != NULL) {
    395   1.9       leo 		iinfo_p->ipl   = level;
    396   1.9       leo 		iinfo_p->imask = (slot == 3) ? 0x80 : (0x01 << slot);
    397   1.9       leo 		iinfo_p->ifunc = ih_fun;
    398   1.9       leo 		iinfo_p->iarg  = ih_arg;
    399   1.9       leo 		iinfo_p->ihand = ihand;
    400   1.9       leo 
    401   1.9       leo 		/*
    402   1.9       leo 		 * Enable (unmask) the interrupt
    403   1.9       leo 		 */
    404   1.9       leo 		MFP2->mf_imrb |= iinfo_p->imask;
    405   1.9       leo 		MFP2->mf_ierb |= iinfo_p->imask;
    406   1.9       leo 		return(iinfo_p);
    407   1.9       leo 	}
    408   1.1       leo 	return NULL;
    409   1.1       leo }
    410   1.1       leo 
    411   1.1       leo void
    412   1.1       leo pci_intr_disestablish(pc, cookie)
    413   1.1       leo 	pci_chipset_tag_t pc;
    414   1.1       leo 	void *cookie;
    415   1.1       leo {
    416   1.9       leo 	pci_intr_info_t *iinfo_p = (pci_intr_info_t *)cookie;
    417   1.9       leo 
    418   1.9       leo 	if (iinfo->ipl < 0)
    419   1.9       leo 	    panic("pci_intr_disestablish: interrupt was not established\n");
    420   1.9       leo 
    421   1.9       leo 	MFP2->mf_imrb &= ~iinfo->imask;
    422   1.9       leo 	MFP2->mf_ierb &= ~iinfo->imask;
    423   1.9       leo 	(void) intr_disestablish(iinfo_p->ihand);
    424   1.9       leo 	iinfo_p->ipl = -1;
    425   1.1       leo }
    426