Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.2
      1       1.1      cgd /*-
      2       1.1      cgd  * Copyright (c) 1991 The Regents of the University of California.
      3       1.1      cgd  * All rights reserved.
      4       1.1      cgd  *
      5       1.1      cgd  * This code is derived from software contributed to Berkeley by
      6       1.1      cgd  * William Jolitz.
      7       1.1      cgd  *
      8       1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9       1.1      cgd  * modification, are permitted provided that the following conditions
     10       1.1      cgd  * are met:
     11       1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12       1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13       1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15       1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16       1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17       1.1      cgd  *    must display the following acknowledgement:
     18       1.1      cgd  *	This product includes software developed by the University of
     19       1.1      cgd  *	California, Berkeley and its contributors.
     20       1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21       1.1      cgd  *    may be used to endorse or promote products derived from this software
     22       1.1      cgd  *    without specific prior written permission.
     23       1.1      cgd  *
     24       1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25       1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26       1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27       1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28       1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29       1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30       1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31       1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32       1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33       1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.1      cgd  * SUCH DAMAGE.
     35       1.1      cgd  *
     36      1.13      cgd  *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
     37  1.28.2.2  mycroft  *	$Id: isa.c,v 1.28.2.2 1993/09/24 08:49:13 mycroft Exp $
     38       1.1      cgd  */
     39       1.1      cgd 
     40       1.1      cgd /*
     41       1.1      cgd  * code to manage AT bus
     42       1.2      cgd  *
     43       1.2      cgd  * 92/08/18  Frank P. MacLachlan (fpm (at) crash.cts.com):
     44       1.2      cgd  * Fixed uninitialized variable problem and added code to deal
     45       1.2      cgd  * with DMA page boundaries in isa_dmarangecheck().  Fixed word
     46       1.2      cgd  * mode DMA count compution and reorganized DMA setup code in
     47       1.2      cgd  * isa_dmastart()
     48       1.1      cgd  */
     49       1.1      cgd 
     50       1.1      cgd #include "param.h"
     51       1.1      cgd #include "systm.h"
     52       1.1      cgd #include "conf.h"
     53       1.1      cgd #include "file.h"
     54       1.1      cgd #include "buf.h"
     55       1.1      cgd #include "uio.h"
     56       1.1      cgd #include "syslog.h"
     57       1.1      cgd #include "malloc.h"
     58  1.28.2.2  mycroft #include "machine/cpu.h"
     59      1.21   andrew #include "machine/cpufunc.h"
     60  1.28.2.1  mycroft #include "sys/device.h"
     61       1.1      cgd #include "vm/vm.h"
     62       1.1      cgd #include "i386/isa/isa.h"
     63  1.28.2.1  mycroft #include "i386/isa/isavar.h"
     64       1.1      cgd #include "i386/isa/icu.h"
     65       1.1      cgd #include "i386/isa/ic/i8237.h"
     66       1.1      cgd #include "i386/isa/ic/i8042.h"
     67      1.16  mycroft #include "i386/isa/timerreg.h"
     68      1.28   brezak #include "i386/isa/spkr_reg.h"
     69      1.14  deraadt 
     70       1.2      cgd /*
     71       1.2      cgd **  Register definitions for DMA controller 1 (channels 0..3):
     72       1.2      cgd */
     73       1.2      cgd #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     74       1.2      cgd #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     75       1.2      cgd #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     76       1.2      cgd #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     77       1.2      cgd 
     78       1.2      cgd /*
     79       1.2      cgd **  Register definitions for DMA controller 2 (channels 4..7):
     80       1.2      cgd */
     81       1.2      cgd #define	DMA2_CHN(c)	(IO_DMA1 + 2*(2*(c)))	/* addr reg for channel c */
     82       1.2      cgd #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     83       1.2      cgd #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     84       1.2      cgd #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     85       1.2      cgd 
     86  1.28.2.1  mycroft isa_type isa_bustype;				/* type of bus */
     87       1.1      cgd 
     88  1.28.2.1  mycroft static int isaprobe __P((struct device *, struct cfdata *, void *));
     89  1.28.2.2  mycroft static void isaattach __P((struct device *, struct device *, void *));
     90  1.28.2.2  mycroft static int isasubmatch __P((struct device *, struct cfdata *, void *));
     91  1.28.2.1  mycroft 
     92  1.28.2.1  mycroft struct cfdriver isacd =
     93  1.28.2.2  mycroft     { NULL, "isa", isaprobe, isaattach, DV_DULL, sizeof(struct isa_softc) };
     94  1.28.2.1  mycroft 
     95  1.28.2.2  mycroft void isa_defaultirq __P((void));
     96  1.28.2.2  mycroft static int isaprint __P((void *, char *));
     97  1.28.2.2  mycroft 
     98  1.28.2.2  mycroft /*
     99  1.28.2.2  mycroft  * We think there might be an ISA bus here.  Check it out.
    100  1.28.2.2  mycroft  */
    101  1.28.2.2  mycroft static int
    102  1.28.2.1  mycroft isaprobe(parent, cf, aux)
    103  1.28.2.1  mycroft 	struct device *parent;
    104  1.28.2.1  mycroft 	struct cfdata *cf;
    105  1.28.2.1  mycroft 	void *aux;
    106  1.28.2.1  mycroft {
    107  1.28.2.1  mycroft 
    108  1.28.2.1  mycroft 	/* XXX should do a real probe */
    109  1.28.2.1  mycroft 	isa_bustype = BUS_ISA;
    110  1.28.2.1  mycroft 	return 1;
    111  1.28.2.1  mycroft }
    112  1.28.2.1  mycroft 
    113  1.28.2.2  mycroft /*
    114  1.28.2.2  mycroft  * Probe succeeded, someone called config_attach.  Now we have to
    115  1.28.2.2  mycroft  * probe the ISA bus itself in our turn.
    116  1.28.2.2  mycroft  */
    117  1.28.2.2  mycroft static void
    118  1.28.2.2  mycroft isaattach(parent, self, aux)
    119  1.28.2.2  mycroft 	struct device *parent, *self;
    120  1.28.2.2  mycroft 	void *aux;
    121  1.28.2.2  mycroft {
    122  1.28.2.2  mycroft 
    123  1.28.2.2  mycroft 	/* should print ISA/EISA & bus clock frequency and anything else
    124  1.28.2.2  mycroft 	   we can figure out? */
    125  1.28.2.2  mycroft 	printf("\n");
    126  1.28.2.2  mycroft 
    127  1.28.2.2  mycroft 	isa_defaultirq();
    128  1.28.2.2  mycroft 
    129  1.28.2.2  mycroft 	enable_intr();
    130  1.28.2.2  mycroft 	splhigh();
    131  1.28.2.2  mycroft 	intr_enable(IRQ_SLAVE);
    132  1.28.2.2  mycroft 
    133  1.28.2.2  mycroft 	/* Iterate ``isasubmatch'' over all devices configured here. */
    134  1.28.2.2  mycroft 	(void)config_search(isasubmatch, self, (void *)NULL);
    135  1.28.2.2  mycroft 
    136  1.28.2.2  mycroft 	/* and the problem is... if netmask == 0, then the loopback
    137  1.28.2.2  mycroft 	 * code can do some really ugly things.
    138  1.28.2.2  mycroft 	 * workaround for this: if netmask == 0, set it to 0x8000, which
    139  1.28.2.2  mycroft 	 * is the value used by splsoftclock.  this is nasty, but it
    140  1.28.2.2  mycroft 	 * should work until this interrupt system goes away. -- cgd
    141  1.28.2.2  mycroft 	 */
    142  1.28.2.2  mycroft 	if (netmask == 0)
    143  1.28.2.2  mycroft 		netmask = 0x8000;	/* same as for softclock.  XXXX */
    144  1.28.2.2  mycroft 
    145  1.28.2.2  mycroft 	printf("biomask %x ttymask %x netmask %x impmask %x\n",
    146  1.28.2.2  mycroft 	       biomask, ttymask, netmask, impmask);
    147  1.28.2.2  mycroft 	splnone();
    148  1.28.2.2  mycroft }
    149  1.28.2.2  mycroft 
    150  1.28.2.2  mycroft /*
    151  1.28.2.2  mycroft  * isaattach (above) iterates this function over all the sub-devices that
    152  1.28.2.2  mycroft  * were configured at the ``isa'' device.  Our job is to provide defaults
    153  1.28.2.2  mycroft  * and do some internal/external representation conversion (some of which
    154  1.28.2.2  mycroft  * is scheduled to get cleaned up later), then call the device's probe
    155  1.28.2.2  mycroft  * function.  If this says the device is there, we try to reserve ISA
    156  1.28.2.2  mycroft  * bus memory and ports for the device, and attach it.
    157  1.28.2.2  mycroft  *
    158  1.28.2.2  mycroft  * Note that we always return 0, but our ultimate caller (isaattach)
    159  1.28.2.2  mycroft  * does not care that we never `match' anything.  (In fact, our return
    160  1.28.2.2  mycroft  * value is entirely irrelevant; this function is run entirely for its
    161  1.28.2.2  mycroft  * side effects.)
    162  1.28.2.2  mycroft  */
    163  1.28.2.1  mycroft static int
    164  1.28.2.2  mycroft isasubmatch(isa, cf, aux)
    165  1.28.2.2  mycroft 	struct device *isa;
    166  1.28.2.1  mycroft 	struct cfdata *cf;
    167  1.28.2.1  mycroft 	void *aux;
    168  1.28.2.1  mycroft {
    169  1.28.2.2  mycroft 	struct isa_attach_args ia;
    170  1.28.2.1  mycroft 
    171  1.28.2.1  mycroft #ifdef DIAGNOSTIC
    172  1.28.2.2  mycroft 	if (cf->cf_driver->cd_match == NULL) {
    173  1.28.2.2  mycroft 		/* we really ought to add printf formats to panic(). */
    174  1.28.2.1  mycroft 		printf("isasubmatch: no match function for `%s' device\n",
    175  1.28.2.1  mycroft 			cf->cf_driver->cd_name);
    176  1.28.2.1  mycroft 		panic("isasubmatch: no match function\n");
    177  1.28.2.1  mycroft 	}
    178  1.28.2.1  mycroft #endif
    179  1.28.2.1  mycroft 
    180  1.28.2.2  mycroft 	/* Init the info needed in the device probe routine. */
    181  1.28.2.2  mycroft 	ia.ia_iobase = cf->cf_iobase;
    182  1.28.2.2  mycroft 	ia.ia_iosize = cf->cf_iosize;
    183  1.28.2.2  mycroft 	if (cf->cf_irq == -1)
    184  1.28.2.2  mycroft 		ia.ia_irq = IRQUNK;
    185  1.28.2.2  mycroft 	else
    186  1.28.2.2  mycroft 		ia.ia_irq = 1 << cf->cf_irq;
    187  1.28.2.2  mycroft 	ia.ia_drq = cf->cf_drq;
    188  1.28.2.2  mycroft 	ia.ia_maddr = (caddr_t)cf->cf_maddr;
    189  1.28.2.2  mycroft 	ia.ia_msize = cf->cf_msize;
    190  1.28.2.1  mycroft 
    191  1.28.2.2  mycroft 	/* If driver says it's not there, believe it. */
    192  1.28.2.2  mycroft 	if (!cf->cf_driver->cd_match(isa, cf, &ia))
    193  1.28.2.1  mycroft 		return 0;
    194  1.28.2.1  mycroft 
    195  1.28.2.2  mycroft 	/* Driver says it is there.  Try to reserve ports and memory. */
    196  1.28.2.2  mycroft 	if (isa_reserveports(ia.ia_iobase, ia.ia_iosize)) {
    197  1.28.2.2  mycroft 		if (isa_reservemem(ia.ia_maddr, ia.ia_msize))
    198  1.28.2.2  mycroft 			config_attach(isa, cf, &ia, isaprint);	/* victory! */
    199  1.28.2.2  mycroft 		else
    200  1.28.2.2  mycroft 			isa_unreserveports(ia.ia_iobase, ia.ia_iosize);
    201  1.28.2.1  mycroft 	}
    202  1.28.2.1  mycroft 
    203  1.28.2.2  mycroft 	/* In any case, move on to next config entry. */
    204  1.28.2.2  mycroft 	return 0;
    205  1.28.2.1  mycroft }
    206  1.28.2.1  mycroft 
    207  1.28.2.2  mycroft /*
    208  1.28.2.2  mycroft  * Called indirectly via config_attach (see above).  Config has already
    209  1.28.2.2  mycroft  * printed, e.g., "wdc0 at isa0".  We can ignore our ``isaname'' arg
    210  1.28.2.2  mycroft  * (it is always NULL, by definition) and just extend the line with
    211  1.28.2.2  mycroft  * the standard info (port(s), irq, drq, and iomem).
    212  1.28.2.2  mycroft  */
    213  1.28.2.2  mycroft static int
    214  1.28.2.1  mycroft isaprint(aux, isaname)
    215  1.28.2.1  mycroft 	void *aux;
    216  1.28.2.1  mycroft 	char *isaname;
    217  1.28.2.1  mycroft {
    218  1.28.2.1  mycroft 	struct isa_attach_args *ia = aux;
    219  1.28.2.1  mycroft 
    220  1.28.2.1  mycroft 	if (ia->ia_iosize)
    221  1.28.2.1  mycroft 		printf(" port 0x%x", ia->ia_iobase);
    222  1.28.2.1  mycroft 	if (ia->ia_iosize > 1)
    223  1.28.2.1  mycroft 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    224  1.28.2.1  mycroft 	if (ia->ia_irq != IRQUNK)
    225  1.28.2.1  mycroft 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    226  1.28.2.1  mycroft 	if (ia->ia_drq != DRQUNK)
    227  1.28.2.1  mycroft 		printf(" drq %d", ia->ia_drq);
    228  1.28.2.1  mycroft 	if (ia->ia_msize)
    229  1.28.2.1  mycroft 		printf(" iomem 0x%x", ia->ia_maddr);
    230  1.28.2.1  mycroft 	if (ia->ia_msize > 1)
    231  1.28.2.1  mycroft 		printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
    232  1.28.2.1  mycroft 	/* XXXX need to print flags */
    233  1.28.2.2  mycroft 	return QUIET;	/* actually, our return value is irrelevant. */
    234       1.1      cgd }
    235       1.1      cgd 
    236       1.1      cgd 
    237       1.1      cgd #define	IDTVEC(name)	__CONCAT(X,name)
    238       1.1      cgd /* default interrupt vector table entries */
    239       1.1      cgd extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    240       1.1      cgd 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    241       1.1      cgd 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    242       1.1      cgd 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    243       1.1      cgd 
    244       1.1      cgd static *defvec[16] = {
    245       1.1      cgd 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    246       1.1      cgd 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    247       1.1      cgd 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    248       1.1      cgd 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    249       1.1      cgd 
    250       1.1      cgd /* out of range default interrupt vector gate entry */
    251       1.1      cgd extern	IDTVEC(intrdefault);
    252      1.15      cgd 
    253       1.1      cgd /*
    254  1.28.2.1  mycroft  * Fill in default interrupt table, and mask all interrupts.
    255       1.1      cgd  */
    256      1.21   andrew void
    257  1.28.2.1  mycroft isa_defaultirq()
    258  1.28.2.1  mycroft {
    259       1.1      cgd 	int i;
    260       1.1      cgd 
    261       1.1      cgd 	/* icu vectors */
    262  1.28.2.1  mycroft 	for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
    263       1.1      cgd 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    264      1.15      cgd 
    265       1.1      cgd 	/* out of range vectors */
    266  1.28.2.1  mycroft 	for (; i < NIDT; i++)
    267       1.1      cgd 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    268       1.1      cgd 
    269       1.1      cgd 	/* initialize 8259's */
    270       1.1      cgd 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    271  1.28.2.1  mycroft 	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
    272  1.28.2.1  mycroft 	outb(IO_ICU1+1, IRQ_SLAVE);
    273      1.15      cgd #ifdef AUTO_EOI_1
    274      1.15      cgd 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    275      1.15      cgd #else
    276       1.1      cgd 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    277      1.15      cgd #endif
    278       1.1      cgd 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    279      1.15      cgd 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    280      1.21   andrew #ifdef REORDER_IRQ
    281      1.15      cgd 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    282      1.21   andrew #endif
    283       1.1      cgd 
    284       1.1      cgd 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    285  1.28.2.1  mycroft 	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
    286  1.28.2.1  mycroft 	outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
    287      1.15      cgd #ifdef AUTO_EOI_2
    288      1.15      cgd 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    289      1.15      cgd #else
    290  1.28.2.1  mycroft 	outb(IO_ICU2+1, 1);		/* 8086 mode */
    291      1.15      cgd #endif
    292       1.1      cgd 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    293      1.15      cgd 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    294       1.1      cgd }
    295       1.1      cgd 
    296  1.28.2.1  mycroft 
    297  1.28.2.1  mycroft /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
    298  1.28.2.1  mycroft 
    299  1.28.2.1  mycroft int
    300  1.28.2.1  mycroft config_isadev(isdp, mp)
    301  1.28.2.1  mycroft 	struct isa_device *isdp;
    302  1.28.2.1  mycroft 	u_int *mp;
    303  1.28.2.1  mycroft {
    304  1.28.2.1  mycroft 	if (isdp->id_irq) {
    305  1.28.2.1  mycroft 		int intrno;
    306  1.28.2.1  mycroft 
    307  1.28.2.1  mycroft 		intrno = ffs(isdp->id_irq)-1;
    308  1.28.2.1  mycroft 		setidt(ICU_OFFSET+intrno, isdp->id_intr,
    309  1.28.2.1  mycroft 			 SDT_SYS386IGT, SEL_KPL);
    310  1.28.2.1  mycroft 		if(mp)
    311  1.28.2.1  mycroft 			INTRMASK(*mp,isdp->id_irq);
    312  1.28.2.1  mycroft 		INTREN(isdp->id_irq);
    313  1.28.2.1  mycroft 	}
    314  1.28.2.1  mycroft }
    315  1.28.2.1  mycroft 
    316  1.28.2.1  mycroft 
    317       1.1      cgd /* region of physical memory known to be contiguous */
    318       1.1      cgd vm_offset_t isaphysmem;
    319  1.28.2.1  mycroft static caddr_t bouncebuf[8];		/* XXX */
    320  1.28.2.1  mycroft static caddr_t bounced[8];		/* XXX */
    321  1.28.2.1  mycroft static vm_size_t bouncesize[8];		/* XXX */
    322  1.28.2.1  mycroft #define MAXDMASZ 512			/* XXX */
    323       1.1      cgd 
    324       1.1      cgd /* high byte of address is stored in this port for i-th dma channel */
    325       1.1      cgd static short dmapageport[8] =
    326       1.1      cgd 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    327       1.1      cgd 
    328       1.1      cgd /*
    329       1.1      cgd  * isa_dmacascade(): program 8237 DMA controller channel to accept
    330       1.1      cgd  * external dma control by a board.
    331       1.1      cgd  */
    332      1.21   andrew void
    333  1.28.2.1  mycroft at_dma_cascade(chan)
    334  1.28.2.1  mycroft 	unsigned chan;
    335       1.2      cgd {
    336  1.28.2.1  mycroft #ifdef DEBUG
    337       1.1      cgd 	if (chan > 7)
    338  1.28.2.1  mycroft 		panic("at_dma_cascade: impossible request");
    339  1.28.2.1  mycroft #endif
    340       1.1      cgd 
    341       1.1      cgd 	/* set dma channel mode, and set dma channel mode */
    342       1.2      cgd 	if ((chan & 4) == 0) {
    343       1.2      cgd 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    344       1.2      cgd 		outb(DMA1_SMSK, chan);
    345       1.2      cgd 	} else {
    346       1.2      cgd 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    347       1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    348       1.2      cgd 	}
    349       1.1      cgd }
    350       1.1      cgd 
    351       1.1      cgd /*
    352  1.28.2.1  mycroft  * at_dma(): program 8237 DMA controller channel, avoid page alignment
    353       1.1      cgd  * problems by using a bounce buffer.
    354       1.1      cgd  */
    355      1.21   andrew void
    356  1.28.2.1  mycroft at_dma(flags, addr, nbytes, chan)
    357  1.28.2.1  mycroft 	int read;
    358  1.28.2.1  mycroft 	caddr_t addr;
    359  1.28.2.1  mycroft 	vm_size_t nbytes;
    360  1.28.2.1  mycroft 	unsigned chan;
    361  1.28.2.1  mycroft {
    362  1.28.2.1  mycroft 	vm_offset_t phys;
    363       1.2      cgd 	int waport;
    364       1.1      cgd 	caddr_t newaddr;
    365       1.1      cgd 
    366  1.28.2.1  mycroft ppp	if (    chan > 7
    367       1.2      cgd 	    || (chan < 4 && nbytes > (1<<16))
    368       1.2      cgd 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    369       1.1      cgd 		panic("isa_dmastart: impossible request");
    370       1.1      cgd 
    371  1.28.2.1  mycroft 	if (at_dma_rangecheck(addr, nbytes, chan)) {
    372  1.28.2.1  mycroft 		panic("bounce buffers don't work yet\n");
    373  1.28.2.1  mycroft 		/* XXX totally braindead; NBPG is not enough */
    374  1.28.2.1  mycroft 		if (bouncebuf[chan] == 0)
    375  1.28.2.1  mycroft 			bouncebuf[chan] =
    376       1.1      cgd 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    377       1.1      cgd 				(caddr_t) isaphysmem + NBPG*chan;
    378  1.28.2.1  mycroft 		bouncesize[chan] = nbytes;
    379  1.28.2.1  mycroft 		newaddr = bouncebuf[chan];
    380       1.1      cgd 		/* copy bounce buffer on write */
    381  1.28.2.1  mycroft 		if (!read)
    382       1.1      cgd 			bcopy(addr, newaddr, nbytes);
    383  1.28.2.1  mycroft 		else
    384  1.28.2.1  mycroft 			bounced[chan] = addr;
    385       1.1      cgd 		addr = newaddr;
    386       1.1      cgd 	}
    387       1.1      cgd 
    388       1.1      cgd 	/* translate to physical */
    389       1.1      cgd 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    390       1.1      cgd 
    391       1.2      cgd 	if ((chan & 4) == 0) {
    392       1.2      cgd 		/*
    393       1.2      cgd 		 * Program one of DMA channels 0..3.  These are
    394       1.2      cgd 		 * byte mode channels.
    395       1.2      cgd 		 */
    396       1.2      cgd 		/* set dma channel mode, and reset address ff */
    397  1.28.2.1  mycroft 		if (read)
    398       1.2      cgd 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    399       1.2      cgd 		else
    400       1.2      cgd 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    401       1.2      cgd 		outb(DMA1_FFC, 0);
    402       1.1      cgd 
    403       1.2      cgd 		/* send start address */
    404       1.2      cgd 		waport =  DMA1_CHN(chan);
    405       1.1      cgd 		outb(waport, phys);
    406       1.1      cgd 		outb(waport, phys>>8);
    407       1.2      cgd 		outb(dmapageport[chan], phys>>16);
    408       1.2      cgd 
    409       1.2      cgd 		/* send count */
    410       1.2      cgd 		outb(waport + 1, --nbytes);
    411       1.2      cgd 		outb(waport + 1, nbytes>>8);
    412       1.2      cgd 
    413       1.2      cgd 		/* unmask channel */
    414       1.2      cgd 		outb(DMA1_SMSK, chan);
    415       1.1      cgd 	} else {
    416       1.2      cgd 		/*
    417       1.2      cgd 		 * Program one of DMA channels 4..7.  These are
    418       1.2      cgd 		 * word mode channels.
    419       1.2      cgd 		 */
    420       1.2      cgd 		/* set dma channel mode, and reset address ff */
    421  1.28.2.1  mycroft 		if (read)
    422       1.2      cgd 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    423       1.2      cgd 		else
    424       1.2      cgd 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    425       1.2      cgd 		outb(DMA2_FFC, 0);
    426       1.2      cgd 
    427       1.2      cgd 		/* send start address */
    428       1.2      cgd 		waport = DMA2_CHN(chan - 4);
    429       1.1      cgd 		outb(waport, phys>>1);
    430       1.1      cgd 		outb(waport, phys>>9);
    431       1.2      cgd 		outb(dmapageport[chan], phys>>16);
    432       1.1      cgd 
    433       1.2      cgd 		/* send count */
    434       1.2      cgd 		nbytes >>= 1;
    435       1.1      cgd 		outb(waport + 2, --nbytes);
    436       1.1      cgd 		outb(waport + 2, nbytes>>8);
    437       1.2      cgd 
    438       1.2      cgd 		/* unmask channel */
    439       1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    440       1.1      cgd 	}
    441       1.1      cgd }
    442       1.1      cgd 
    443      1.21   andrew void
    444  1.28.2.1  mycroft at_dma_terminate(int flags, caddr_t addr, int nbytes, int chan)
    445       1.1      cgd {
    446       1.1      cgd 
    447       1.1      cgd 	if (bounced[chan]) {
    448  1.28.2.1  mycroft 		bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
    449       1.1      cgd 		bounced[chan] = 0;
    450       1.1      cgd 	}
    451       1.1      cgd }
    452       1.1      cgd 
    453       1.1      cgd /*
    454       1.1      cgd  * Check for problems with the address range of a DMA transfer
    455       1.2      cgd  * (non-contiguous physical pages, outside of bus address space,
    456       1.2      cgd  * crossing DMA page boundaries).
    457       1.1      cgd  * Return true if special handling needed.
    458       1.1      cgd  */
    459       1.1      cgd 
    460      1.21   andrew int
    461  1.28.2.1  mycroft at_dma_rangecheck(caddr_t va, unsigned length, unsigned chan) {
    462       1.2      cgd 	vm_offset_t phys, priorpage = 0, endva;
    463       1.2      cgd 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    464       1.1      cgd 
    465       1.1      cgd 	endva = (vm_offset_t)round_page(va + length);
    466       1.1      cgd 	for (; va < (caddr_t) endva ; va += NBPG) {
    467       1.1      cgd 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    468       1.1      cgd 		if (phys == 0)
    469       1.1      cgd 			panic("isa_dmacheck: no physical page present");
    470  1.28.2.1  mycroft 		if (phys > physmem)
    471       1.1      cgd 			return (1);
    472       1.2      cgd 		if (priorpage) {
    473       1.2      cgd 			if (priorpage + NBPG != phys)
    474       1.2      cgd 				return (1);
    475       1.2      cgd 			/* check if crossing a DMA page boundary */
    476       1.2      cgd 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    477       1.2      cgd 				return (1);
    478       1.2      cgd 		}
    479       1.1      cgd 		priorpage = phys;
    480       1.1      cgd 	}
    481       1.1      cgd 	return (0);
    482       1.1      cgd }
    483       1.1      cgd 
    484       1.1      cgd /* head of queue waiting for physmem to become available */
    485       1.1      cgd struct buf isa_physmemq;
    486       1.1      cgd 
    487       1.1      cgd /* blocked waiting for resource to become free for exclusive use */
    488       1.1      cgd static isaphysmemflag;
    489       1.1      cgd /* if waited for and call requested when free (B_CALL) */
    490  1.28.2.1  mycroft static void (*isaphysmemunblock)(); /* XXX needs to be a list */
    491       1.1      cgd 
    492       1.1      cgd /*
    493       1.1      cgd  * Allocate contiguous physical memory for transfer, returning
    494       1.1      cgd  * a *virtual* address to region. May block waiting for resource.
    495       1.1      cgd  * (assumed to be called at splbio())
    496       1.1      cgd  */
    497       1.1      cgd caddr_t
    498       1.1      cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    499       1.1      cgd 
    500       1.1      cgd 	isaphysmemunblock = func;
    501       1.1      cgd 	while (isaphysmemflag & B_BUSY) {
    502       1.1      cgd 		isaphysmemflag |= B_WANTED;
    503      1.24  deraadt 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    504       1.1      cgd 	}
    505       1.1      cgd 	isaphysmemflag |= B_BUSY;
    506       1.1      cgd 
    507       1.1      cgd 	return((caddr_t)isaphysmem);
    508       1.1      cgd }
    509       1.1      cgd 
    510       1.1      cgd /*
    511       1.1      cgd  * Free contiguous physical memory used for transfer.
    512       1.1      cgd  * (assumed to be called at splbio())
    513       1.1      cgd  */
    514       1.1      cgd void
    515       1.1      cgd isa_freephysmem(caddr_t va, unsigned length) {
    516       1.1      cgd 
    517       1.1      cgd 	isaphysmemflag &= ~B_BUSY;
    518       1.1      cgd 	if (isaphysmemflag & B_WANTED) {
    519       1.1      cgd 		isaphysmemflag &= B_WANTED;
    520      1.24  deraadt 		wakeup((caddr_t)&isaphysmemflag);
    521       1.1      cgd 		if (isaphysmemunblock)
    522       1.1      cgd 			(*isaphysmemunblock)();
    523       1.1      cgd 	}
    524       1.1      cgd }
    525       1.1      cgd 
    526       1.1      cgd /*
    527       1.1      cgd  * Handle a NMI, possibly a machine check.
    528       1.1      cgd  * return true to panic system, false to ignore.
    529       1.1      cgd  */
    530      1.21   andrew int
    531       1.1      cgd isa_nmi(cd) {
    532       1.1      cgd 
    533       1.1      cgd 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    534       1.1      cgd 	return(0);
    535       1.1      cgd }
    536       1.1      cgd 
    537       1.1      cgd /*
    538       1.1      cgd  * Caught a stray interrupt, notify
    539       1.1      cgd  */
    540      1.21   andrew void
    541  1.28.2.1  mycroft isa_strayintr(d)
    542  1.28.2.1  mycroft 	int d;
    543  1.28.2.1  mycroft {
    544       1.1      cgd 
    545       1.4      cgd 	/*
    546  1.28.2.1  mycroft 	 * Stray level 7 interrupts occur when someone raises an interrupt
    547  1.28.2.1  mycroft 	 * and then drops it before the CPU acknowledges it.  This means
    548  1.28.2.1  mycroft 	 * either the device is screwed or something is cli'ing too long.
    549       1.4      cgd 	 */
    550      1.15      cgd 	extern u_long intrcnt_stray;
    551       1.4      cgd 
    552      1.15      cgd 	intrcnt_stray++;
    553      1.15      cgd 	if (intrcnt_stray <= 5)
    554  1.28.2.1  mycroft 		log(LOG_ERR, "stray interrupt %d\n", d);
    555      1.15      cgd 	if (intrcnt_stray == 5)
    556  1.28.2.1  mycroft 		log(LOG_CRIT,"too many stray interrupts; stopped logging\n");
    557       1.1      cgd }
    558       1.1      cgd 
    559       1.1      cgd static beeping;
    560      1.21   andrew static void
    561      1.21   andrew sysbeepstop(int f)
    562       1.1      cgd {
    563      1.16  mycroft 	int s = splhigh();
    564      1.16  mycroft 
    565       1.1      cgd 	/* disable counter 2 */
    566      1.16  mycroft 	disable_intr();
    567      1.28   brezak 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    568      1.16  mycroft 	enable_intr();
    569       1.1      cgd 	if (f)
    570      1.24  deraadt 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    571       1.1      cgd 	else
    572       1.1      cgd 		beeping = 0;
    573      1.16  mycroft 
    574      1.16  mycroft 	splx(s);
    575       1.1      cgd }
    576       1.1      cgd 
    577      1.21   andrew void
    578      1.21   andrew sysbeep(int pitch, int period)
    579       1.1      cgd {
    580      1.16  mycroft 	int s = splhigh();
    581      1.16  mycroft 	static int last_pitch, last_period;
    582       1.1      cgd 
    583      1.16  mycroft 	if (beeping) {
    584      1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    585      1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    586       1.1      cgd 	}
    587      1.16  mycroft 	if (!beeping || last_pitch != pitch) {
    588      1.16  mycroft 		/*
    589      1.16  mycroft 	 	* XXX - move timer stuff to clock.c.
    590      1.16  mycroft 	 	*/
    591      1.16  mycroft 		disable_intr();
    592      1.16  mycroft 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    593      1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    594      1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    595      1.28   brezak 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    596      1.16  mycroft 		enable_intr();
    597      1.16  mycroft 	}
    598      1.16  mycroft 	last_pitch = pitch;
    599      1.16  mycroft 	beeping = last_period = period;
    600      1.24  deraadt 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    601      1.16  mycroft 
    602      1.16  mycroft 	splx(s);
    603       1.1      cgd }
    604       1.1      cgd 
    605       1.1      cgd /*
    606       1.1      cgd  * Pass command to keyboard controller (8042)
    607       1.1      cgd  */
    608      1.21   andrew unsigned
    609      1.21   andrew kbc_8042cmd(int val)
    610      1.21   andrew {
    611       1.1      cgd 	while (inb(KBSTATP)&KBS_IBF);
    612       1.1      cgd 	if (val) outb(KBCMDP, val);
    613       1.1      cgd 	while (inb(KBSTATP)&KBS_IBF);
    614       1.1      cgd 	return (inb(KBDATAP));
    615      1.28   brezak }
    616      1.28   brezak 
    617      1.28   brezak /*
    618      1.28   brezak  * find an ISA device in a given isa_devtab_* table, given
    619      1.28   brezak  * the table to search, the expected id_driver entry, and the unit number.
    620      1.28   brezak  *
    621      1.28   brezak  * this function is defined in isa_device.h, and this location is debatable;
    622      1.28   brezak  * i put it there because it's useless w/o, and directly operates on
    623      1.28   brezak  * the other stuff in that file.
    624      1.28   brezak  *
    625      1.28   brezak  */
    626      1.28   brezak 
    627      1.28   brezak struct isa_device *find_isadev(table, driverp, unit)
    628      1.28   brezak 	struct isa_device *table;
    629      1.28   brezak 	struct isa_driver *driverp;
    630      1.28   brezak 	int unit;
    631      1.28   brezak {
    632      1.28   brezak 	if (driverp == NULL) /* sanity check */
    633      1.28   brezak 		return NULL;
    634      1.28   brezak 
    635      1.28   brezak 	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
    636      1.28   brezak 		if (table->id_driver == 0)
    637      1.28   brezak 			return NULL;
    638      1.28   brezak 
    639      1.28   brezak 		table++;
    640      1.28   brezak         }
    641      1.28   brezak 
    642      1.28   brezak 	return table;
    643      1.15      cgd }
    644      1.15      cgd 
    645      1.15      cgd /*
    646      1.15      cgd  * Return nonzero if a (masked) irq is pending for a given device.
    647      1.15      cgd  */
    648      1.15      cgd int
    649      1.15      cgd isa_irq_pending(dvp)
    650      1.15      cgd 	struct isa_device *dvp;
    651      1.15      cgd {
    652      1.15      cgd 	unsigned id_irq;
    653      1.15      cgd 
    654      1.15      cgd 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    655      1.15      cgd 	if (id_irq & 0xff)
    656      1.15      cgd 		return (inb(IO_ICU1) & id_irq);
    657      1.15      cgd 	return (inb(IO_ICU2) & (id_irq >> 8));
    658       1.1      cgd }
    659