Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.5
      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.5  mycroft  *	$Id: isa.c,v 1.28.2.5 1993/10/09 08:49:17 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.5  mycroft 	printf("biomask %x ttymask %x netmask %x impmask %x astmask %s\n",
    137  1.28.2.5  mycroft 	       biomask, ttymask, netmask, impmask, astmask);
    138  1.28.2.2  mycroft 	splnone();
    139  1.28.2.2  mycroft }
    140  1.28.2.2  mycroft 
    141  1.28.2.2  mycroft /*
    142  1.28.2.2  mycroft  * isaattach (above) iterates this function over all the sub-devices that
    143  1.28.2.2  mycroft  * were configured at the ``isa'' device.  Our job is to provide defaults
    144  1.28.2.2  mycroft  * and do some internal/external representation conversion (some of which
    145  1.28.2.2  mycroft  * is scheduled to get cleaned up later), then call the device's probe
    146  1.28.2.2  mycroft  * function.  If this says the device is there, we try to reserve ISA
    147  1.28.2.2  mycroft  * bus memory and ports for the device, and attach it.
    148  1.28.2.2  mycroft  *
    149  1.28.2.2  mycroft  * Note that we always return 0, but our ultimate caller (isaattach)
    150  1.28.2.2  mycroft  * does not care that we never `match' anything.  (In fact, our return
    151  1.28.2.2  mycroft  * value is entirely irrelevant; this function is run entirely for its
    152  1.28.2.2  mycroft  * side effects.)
    153  1.28.2.2  mycroft  */
    154  1.28.2.1  mycroft static int
    155  1.28.2.2  mycroft isasubmatch(isa, cf, aux)
    156  1.28.2.2  mycroft 	struct device *isa;
    157  1.28.2.1  mycroft 	struct cfdata *cf;
    158  1.28.2.1  mycroft 	void *aux;
    159  1.28.2.1  mycroft {
    160  1.28.2.2  mycroft 	struct isa_attach_args ia;
    161  1.28.2.1  mycroft 
    162  1.28.2.1  mycroft #ifdef DIAGNOSTIC
    163  1.28.2.2  mycroft 	if (cf->cf_driver->cd_match == NULL) {
    164  1.28.2.2  mycroft 		/* we really ought to add printf formats to panic(). */
    165  1.28.2.1  mycroft 		printf("isasubmatch: no match function for `%s' device\n",
    166  1.28.2.1  mycroft 			cf->cf_driver->cd_name);
    167  1.28.2.1  mycroft 		panic("isasubmatch: no match function\n");
    168  1.28.2.1  mycroft 	}
    169  1.28.2.1  mycroft #endif
    170  1.28.2.1  mycroft 
    171  1.28.2.2  mycroft 	/* Init the info needed in the device probe routine. */
    172  1.28.2.2  mycroft 	ia.ia_iobase = cf->cf_iobase;
    173  1.28.2.2  mycroft 	ia.ia_iosize = cf->cf_iosize;
    174  1.28.2.2  mycroft 	if (cf->cf_irq == -1)
    175  1.28.2.2  mycroft 		ia.ia_irq = IRQUNK;
    176  1.28.2.2  mycroft 	else
    177  1.28.2.2  mycroft 		ia.ia_irq = 1 << cf->cf_irq;
    178  1.28.2.2  mycroft 	ia.ia_drq = cf->cf_drq;
    179  1.28.2.2  mycroft 	ia.ia_maddr = (caddr_t)cf->cf_maddr;
    180  1.28.2.2  mycroft 	ia.ia_msize = cf->cf_msize;
    181  1.28.2.1  mycroft 
    182  1.28.2.2  mycroft 	/* If driver says it's not there, believe it. */
    183  1.28.2.2  mycroft 	if (!cf->cf_driver->cd_match(isa, cf, &ia))
    184  1.28.2.1  mycroft 		return 0;
    185  1.28.2.1  mycroft 
    186  1.28.2.2  mycroft 	/* Driver says it is there.  Try to reserve ports and memory. */
    187  1.28.2.2  mycroft 	if (isa_reserveports(ia.ia_iobase, ia.ia_iosize)) {
    188  1.28.2.2  mycroft 		if (isa_reservemem(ia.ia_maddr, ia.ia_msize))
    189  1.28.2.2  mycroft 			config_attach(isa, cf, &ia, isaprint);	/* victory! */
    190  1.28.2.2  mycroft 		else
    191  1.28.2.2  mycroft 			isa_unreserveports(ia.ia_iobase, ia.ia_iosize);
    192  1.28.2.1  mycroft 	}
    193  1.28.2.1  mycroft 
    194  1.28.2.2  mycroft 	/* In any case, move on to next config entry. */
    195  1.28.2.2  mycroft 	return 0;
    196  1.28.2.1  mycroft }
    197  1.28.2.1  mycroft 
    198  1.28.2.2  mycroft /*
    199  1.28.2.2  mycroft  * Called indirectly via config_attach (see above).  Config has already
    200  1.28.2.2  mycroft  * printed, e.g., "wdc0 at isa0".  We can ignore our ``isaname'' arg
    201  1.28.2.2  mycroft  * (it is always NULL, by definition) and just extend the line with
    202  1.28.2.2  mycroft  * the standard info (port(s), irq, drq, and iomem).
    203  1.28.2.2  mycroft  */
    204  1.28.2.2  mycroft static int
    205  1.28.2.1  mycroft isaprint(aux, isaname)
    206  1.28.2.1  mycroft 	void *aux;
    207  1.28.2.1  mycroft 	char *isaname;
    208  1.28.2.1  mycroft {
    209  1.28.2.1  mycroft 	struct isa_attach_args *ia = aux;
    210  1.28.2.1  mycroft 
    211  1.28.2.1  mycroft 	if (ia->ia_iosize)
    212  1.28.2.1  mycroft 		printf(" port 0x%x", ia->ia_iobase);
    213  1.28.2.1  mycroft 	if (ia->ia_iosize > 1)
    214  1.28.2.1  mycroft 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    215  1.28.2.3  mycroft #ifdef DIAGNOSTIC
    216  1.28.2.3  mycroft 	if (ia->ia_irq == IRQUNK)
    217  1.28.2.4  mycroft 		printf(" THIS IS A BUG ->");
    218  1.28.2.3  mycroft #endif
    219  1.28.2.3  mycroft 	if (ia->ia_irq != IRQNONE)
    220  1.28.2.1  mycroft 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    221  1.28.2.1  mycroft 	if (ia->ia_drq != DRQUNK)
    222  1.28.2.1  mycroft 		printf(" drq %d", ia->ia_drq);
    223  1.28.2.1  mycroft 	if (ia->ia_msize)
    224  1.28.2.1  mycroft 		printf(" iomem 0x%x", ia->ia_maddr);
    225  1.28.2.1  mycroft 	if (ia->ia_msize > 1)
    226  1.28.2.1  mycroft 		printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
    227  1.28.2.1  mycroft 	/* XXXX need to print flags */
    228  1.28.2.2  mycroft 	return QUIET;	/* actually, our return value is irrelevant. */
    229       1.1      cgd }
    230       1.1      cgd 
    231       1.1      cgd 
    232       1.1      cgd #define	IDTVEC(name)	__CONCAT(X,name)
    233       1.1      cgd /* default interrupt vector table entries */
    234       1.1      cgd extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    235       1.1      cgd 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    236       1.1      cgd 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    237       1.1      cgd 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    238       1.1      cgd 
    239       1.1      cgd static *defvec[16] = {
    240       1.1      cgd 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    241       1.1      cgd 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    242       1.1      cgd 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    243       1.1      cgd 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    244       1.1      cgd 
    245       1.1      cgd /* out of range default interrupt vector gate entry */
    246       1.1      cgd extern	IDTVEC(intrdefault);
    247      1.15      cgd 
    248       1.1      cgd /*
    249  1.28.2.1  mycroft  * Fill in default interrupt table, and mask all interrupts.
    250       1.1      cgd  */
    251      1.21   andrew void
    252  1.28.2.1  mycroft isa_defaultirq()
    253  1.28.2.1  mycroft {
    254       1.1      cgd 	int i;
    255       1.1      cgd 
    256       1.1      cgd 	/* icu vectors */
    257  1.28.2.1  mycroft 	for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
    258       1.1      cgd 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    259      1.15      cgd 
    260       1.1      cgd 	/* out of range vectors */
    261  1.28.2.1  mycroft 	for (; i < NIDT; i++)
    262       1.1      cgd 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    263       1.1      cgd 
    264       1.1      cgd 	/* initialize 8259's */
    265       1.1      cgd 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    266  1.28.2.1  mycroft 	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
    267  1.28.2.1  mycroft 	outb(IO_ICU1+1, IRQ_SLAVE);
    268      1.15      cgd #ifdef AUTO_EOI_1
    269      1.15      cgd 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    270      1.15      cgd #else
    271       1.1      cgd 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    272      1.15      cgd #endif
    273       1.1      cgd 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    274      1.15      cgd 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    275      1.21   andrew #ifdef REORDER_IRQ
    276      1.15      cgd 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    277      1.21   andrew #endif
    278       1.1      cgd 
    279       1.1      cgd 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    280  1.28.2.1  mycroft 	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
    281  1.28.2.1  mycroft 	outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
    282      1.15      cgd #ifdef AUTO_EOI_2
    283      1.15      cgd 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    284      1.15      cgd #else
    285  1.28.2.1  mycroft 	outb(IO_ICU2+1, 1);		/* 8086 mode */
    286      1.15      cgd #endif
    287       1.1      cgd 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    288      1.15      cgd 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    289       1.1      cgd }
    290       1.1      cgd 
    291  1.28.2.1  mycroft 
    292  1.28.2.1  mycroft /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
    293  1.28.2.1  mycroft 
    294  1.28.2.1  mycroft int
    295  1.28.2.1  mycroft config_isadev(isdp, mp)
    296  1.28.2.1  mycroft 	struct isa_device *isdp;
    297  1.28.2.1  mycroft 	u_int *mp;
    298  1.28.2.1  mycroft {
    299  1.28.2.1  mycroft 	if (isdp->id_irq) {
    300  1.28.2.1  mycroft 		int intrno;
    301  1.28.2.1  mycroft 
    302  1.28.2.1  mycroft 		intrno = ffs(isdp->id_irq)-1;
    303  1.28.2.1  mycroft 		setidt(ICU_OFFSET+intrno, isdp->id_intr,
    304  1.28.2.1  mycroft 			 SDT_SYS386IGT, SEL_KPL);
    305  1.28.2.1  mycroft 		if(mp)
    306  1.28.2.1  mycroft 			INTRMASK(*mp,isdp->id_irq);
    307  1.28.2.1  mycroft 		INTREN(isdp->id_irq);
    308  1.28.2.1  mycroft 	}
    309  1.28.2.1  mycroft }
    310  1.28.2.1  mycroft 
    311  1.28.2.1  mycroft 
    312       1.1      cgd /* region of physical memory known to be contiguous */
    313       1.1      cgd vm_offset_t isaphysmem;
    314  1.28.2.1  mycroft static caddr_t bouncebuf[8];		/* XXX */
    315  1.28.2.1  mycroft static caddr_t bounced[8];		/* XXX */
    316  1.28.2.1  mycroft static vm_size_t bouncesize[8];		/* XXX */
    317  1.28.2.1  mycroft #define MAXDMASZ 512			/* XXX */
    318       1.1      cgd 
    319       1.1      cgd /* high byte of address is stored in this port for i-th dma channel */
    320       1.1      cgd static short dmapageport[8] =
    321       1.1      cgd 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    322       1.1      cgd 
    323       1.1      cgd /*
    324       1.1      cgd  * isa_dmacascade(): program 8237 DMA controller channel to accept
    325       1.1      cgd  * external dma control by a board.
    326       1.1      cgd  */
    327      1.21   andrew void
    328  1.28.2.1  mycroft at_dma_cascade(chan)
    329  1.28.2.1  mycroft 	unsigned chan;
    330       1.2      cgd {
    331  1.28.2.1  mycroft #ifdef DEBUG
    332       1.1      cgd 	if (chan > 7)
    333  1.28.2.1  mycroft 		panic("at_dma_cascade: impossible request");
    334  1.28.2.1  mycroft #endif
    335       1.1      cgd 
    336       1.1      cgd 	/* set dma channel mode, and set dma channel mode */
    337       1.2      cgd 	if ((chan & 4) == 0) {
    338       1.2      cgd 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    339       1.2      cgd 		outb(DMA1_SMSK, chan);
    340       1.2      cgd 	} else {
    341       1.2      cgd 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    342       1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    343       1.2      cgd 	}
    344       1.1      cgd }
    345       1.1      cgd 
    346       1.1      cgd /*
    347  1.28.2.1  mycroft  * at_dma(): program 8237 DMA controller channel, avoid page alignment
    348       1.1      cgd  * problems by using a bounce buffer.
    349       1.1      cgd  */
    350      1.21   andrew void
    351  1.28.2.1  mycroft at_dma(flags, addr, nbytes, chan)
    352  1.28.2.1  mycroft 	int read;
    353  1.28.2.1  mycroft 	caddr_t addr;
    354  1.28.2.1  mycroft 	vm_size_t nbytes;
    355  1.28.2.1  mycroft 	unsigned chan;
    356  1.28.2.1  mycroft {
    357  1.28.2.1  mycroft 	vm_offset_t phys;
    358       1.2      cgd 	int waport;
    359       1.1      cgd 	caddr_t newaddr;
    360       1.1      cgd 
    361  1.28.2.1  mycroft ppp	if (    chan > 7
    362       1.2      cgd 	    || (chan < 4 && nbytes > (1<<16))
    363       1.2      cgd 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    364       1.1      cgd 		panic("isa_dmastart: impossible request");
    365       1.1      cgd 
    366  1.28.2.1  mycroft 	if (at_dma_rangecheck(addr, nbytes, chan)) {
    367  1.28.2.1  mycroft 		panic("bounce buffers don't work yet\n");
    368  1.28.2.1  mycroft 		/* XXX totally braindead; NBPG is not enough */
    369  1.28.2.1  mycroft 		if (bouncebuf[chan] == 0)
    370  1.28.2.1  mycroft 			bouncebuf[chan] =
    371       1.1      cgd 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    372       1.1      cgd 				(caddr_t) isaphysmem + NBPG*chan;
    373  1.28.2.1  mycroft 		bouncesize[chan] = nbytes;
    374  1.28.2.1  mycroft 		newaddr = bouncebuf[chan];
    375       1.1      cgd 		/* copy bounce buffer on write */
    376  1.28.2.1  mycroft 		if (!read)
    377       1.1      cgd 			bcopy(addr, newaddr, nbytes);
    378  1.28.2.1  mycroft 		else
    379  1.28.2.1  mycroft 			bounced[chan] = addr;
    380       1.1      cgd 		addr = newaddr;
    381       1.1      cgd 	}
    382       1.1      cgd 
    383       1.1      cgd 	/* translate to physical */
    384       1.1      cgd 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    385       1.1      cgd 
    386       1.2      cgd 	if ((chan & 4) == 0) {
    387       1.2      cgd 		/*
    388       1.2      cgd 		 * Program one of DMA channels 0..3.  These are
    389       1.2      cgd 		 * byte mode channels.
    390       1.2      cgd 		 */
    391       1.2      cgd 		/* set dma channel mode, and reset address ff */
    392  1.28.2.1  mycroft 		if (read)
    393       1.2      cgd 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    394       1.2      cgd 		else
    395       1.2      cgd 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    396       1.2      cgd 		outb(DMA1_FFC, 0);
    397       1.1      cgd 
    398       1.2      cgd 		/* send start address */
    399       1.2      cgd 		waport =  DMA1_CHN(chan);
    400       1.1      cgd 		outb(waport, phys);
    401       1.1      cgd 		outb(waport, phys>>8);
    402       1.2      cgd 		outb(dmapageport[chan], phys>>16);
    403       1.2      cgd 
    404       1.2      cgd 		/* send count */
    405       1.2      cgd 		outb(waport + 1, --nbytes);
    406       1.2      cgd 		outb(waport + 1, nbytes>>8);
    407       1.2      cgd 
    408       1.2      cgd 		/* unmask channel */
    409       1.2      cgd 		outb(DMA1_SMSK, chan);
    410       1.1      cgd 	} else {
    411       1.2      cgd 		/*
    412       1.2      cgd 		 * Program one of DMA channels 4..7.  These are
    413       1.2      cgd 		 * word mode channels.
    414       1.2      cgd 		 */
    415       1.2      cgd 		/* set dma channel mode, and reset address ff */
    416  1.28.2.1  mycroft 		if (read)
    417       1.2      cgd 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    418       1.2      cgd 		else
    419       1.2      cgd 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    420       1.2      cgd 		outb(DMA2_FFC, 0);
    421       1.2      cgd 
    422       1.2      cgd 		/* send start address */
    423       1.2      cgd 		waport = DMA2_CHN(chan - 4);
    424       1.1      cgd 		outb(waport, phys>>1);
    425       1.1      cgd 		outb(waport, phys>>9);
    426       1.2      cgd 		outb(dmapageport[chan], phys>>16);
    427       1.1      cgd 
    428       1.2      cgd 		/* send count */
    429       1.2      cgd 		nbytes >>= 1;
    430       1.1      cgd 		outb(waport + 2, --nbytes);
    431       1.1      cgd 		outb(waport + 2, nbytes>>8);
    432       1.2      cgd 
    433       1.2      cgd 		/* unmask channel */
    434       1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    435       1.1      cgd 	}
    436       1.1      cgd }
    437       1.1      cgd 
    438      1.21   andrew void
    439  1.28.2.1  mycroft at_dma_terminate(int flags, caddr_t addr, int nbytes, int chan)
    440       1.1      cgd {
    441       1.1      cgd 
    442       1.1      cgd 	if (bounced[chan]) {
    443  1.28.2.1  mycroft 		bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
    444       1.1      cgd 		bounced[chan] = 0;
    445       1.1      cgd 	}
    446       1.1      cgd }
    447       1.1      cgd 
    448       1.1      cgd /*
    449       1.1      cgd  * Check for problems with the address range of a DMA transfer
    450       1.2      cgd  * (non-contiguous physical pages, outside of bus address space,
    451       1.2      cgd  * crossing DMA page boundaries).
    452       1.1      cgd  * Return true if special handling needed.
    453       1.1      cgd  */
    454       1.1      cgd 
    455      1.21   andrew int
    456  1.28.2.1  mycroft at_dma_rangecheck(caddr_t va, unsigned length, unsigned chan) {
    457       1.2      cgd 	vm_offset_t phys, priorpage = 0, endva;
    458       1.2      cgd 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    459       1.1      cgd 
    460       1.1      cgd 	endva = (vm_offset_t)round_page(va + length);
    461       1.1      cgd 	for (; va < (caddr_t) endva ; va += NBPG) {
    462       1.1      cgd 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    463       1.1      cgd 		if (phys == 0)
    464       1.1      cgd 			panic("isa_dmacheck: no physical page present");
    465  1.28.2.1  mycroft 		if (phys > physmem)
    466       1.1      cgd 			return (1);
    467       1.2      cgd 		if (priorpage) {
    468       1.2      cgd 			if (priorpage + NBPG != phys)
    469       1.2      cgd 				return (1);
    470       1.2      cgd 			/* check if crossing a DMA page boundary */
    471       1.2      cgd 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    472       1.2      cgd 				return (1);
    473       1.2      cgd 		}
    474       1.1      cgd 		priorpage = phys;
    475       1.1      cgd 	}
    476       1.1      cgd 	return (0);
    477       1.1      cgd }
    478       1.1      cgd 
    479       1.1      cgd /* head of queue waiting for physmem to become available */
    480       1.1      cgd struct buf isa_physmemq;
    481       1.1      cgd 
    482       1.1      cgd /* blocked waiting for resource to become free for exclusive use */
    483       1.1      cgd static isaphysmemflag;
    484       1.1      cgd /* if waited for and call requested when free (B_CALL) */
    485  1.28.2.1  mycroft static void (*isaphysmemunblock)(); /* XXX needs to be a list */
    486       1.1      cgd 
    487       1.1      cgd /*
    488       1.1      cgd  * Allocate contiguous physical memory for transfer, returning
    489       1.1      cgd  * a *virtual* address to region. May block waiting for resource.
    490       1.1      cgd  * (assumed to be called at splbio())
    491       1.1      cgd  */
    492       1.1      cgd caddr_t
    493       1.1      cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    494       1.1      cgd 
    495       1.1      cgd 	isaphysmemunblock = func;
    496       1.1      cgd 	while (isaphysmemflag & B_BUSY) {
    497       1.1      cgd 		isaphysmemflag |= B_WANTED;
    498      1.24  deraadt 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    499       1.1      cgd 	}
    500       1.1      cgd 	isaphysmemflag |= B_BUSY;
    501       1.1      cgd 
    502       1.1      cgd 	return((caddr_t)isaphysmem);
    503       1.1      cgd }
    504       1.1      cgd 
    505       1.1      cgd /*
    506       1.1      cgd  * Free contiguous physical memory used for transfer.
    507       1.1      cgd  * (assumed to be called at splbio())
    508       1.1      cgd  */
    509       1.1      cgd void
    510       1.1      cgd isa_freephysmem(caddr_t va, unsigned length) {
    511       1.1      cgd 
    512       1.1      cgd 	isaphysmemflag &= ~B_BUSY;
    513       1.1      cgd 	if (isaphysmemflag & B_WANTED) {
    514       1.1      cgd 		isaphysmemflag &= B_WANTED;
    515      1.24  deraadt 		wakeup((caddr_t)&isaphysmemflag);
    516       1.1      cgd 		if (isaphysmemunblock)
    517       1.1      cgd 			(*isaphysmemunblock)();
    518       1.1      cgd 	}
    519       1.1      cgd }
    520       1.1      cgd 
    521       1.1      cgd /*
    522       1.1      cgd  * Handle a NMI, possibly a machine check.
    523       1.1      cgd  * return true to panic system, false to ignore.
    524       1.1      cgd  */
    525      1.21   andrew int
    526       1.1      cgd isa_nmi(cd) {
    527       1.1      cgd 
    528       1.1      cgd 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    529       1.1      cgd 	return(0);
    530       1.1      cgd }
    531       1.1      cgd 
    532       1.1      cgd /*
    533       1.1      cgd  * Caught a stray interrupt, notify
    534       1.1      cgd  */
    535      1.21   andrew void
    536  1.28.2.1  mycroft isa_strayintr(d)
    537  1.28.2.1  mycroft 	int d;
    538  1.28.2.1  mycroft {
    539       1.1      cgd 
    540       1.4      cgd 	/*
    541  1.28.2.1  mycroft 	 * Stray level 7 interrupts occur when someone raises an interrupt
    542  1.28.2.1  mycroft 	 * and then drops it before the CPU acknowledges it.  This means
    543  1.28.2.1  mycroft 	 * either the device is screwed or something is cli'ing too long.
    544       1.4      cgd 	 */
    545      1.15      cgd 	extern u_long intrcnt_stray;
    546       1.4      cgd 
    547      1.15      cgd 	intrcnt_stray++;
    548      1.15      cgd 	if (intrcnt_stray <= 5)
    549  1.28.2.1  mycroft 		log(LOG_ERR, "stray interrupt %d\n", d);
    550      1.15      cgd 	if (intrcnt_stray == 5)
    551  1.28.2.1  mycroft 		log(LOG_CRIT,"too many stray interrupts; stopped logging\n");
    552       1.1      cgd }
    553       1.1      cgd 
    554       1.1      cgd static beeping;
    555      1.21   andrew static void
    556      1.21   andrew sysbeepstop(int f)
    557       1.1      cgd {
    558      1.16  mycroft 	int s = splhigh();
    559      1.16  mycroft 
    560       1.1      cgd 	/* disable counter 2 */
    561      1.16  mycroft 	disable_intr();
    562      1.28   brezak 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    563      1.16  mycroft 	enable_intr();
    564       1.1      cgd 	if (f)
    565      1.24  deraadt 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    566       1.1      cgd 	else
    567       1.1      cgd 		beeping = 0;
    568      1.16  mycroft 
    569      1.16  mycroft 	splx(s);
    570       1.1      cgd }
    571       1.1      cgd 
    572      1.21   andrew void
    573      1.21   andrew sysbeep(int pitch, int period)
    574       1.1      cgd {
    575      1.16  mycroft 	int s = splhigh();
    576      1.16  mycroft 	static int last_pitch, last_period;
    577       1.1      cgd 
    578      1.16  mycroft 	if (beeping) {
    579      1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    580      1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    581       1.1      cgd 	}
    582      1.16  mycroft 	if (!beeping || last_pitch != pitch) {
    583      1.16  mycroft 		/*
    584      1.16  mycroft 	 	* XXX - move timer stuff to clock.c.
    585      1.16  mycroft 	 	*/
    586      1.16  mycroft 		disable_intr();
    587      1.16  mycroft 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    588      1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    589      1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    590      1.28   brezak 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    591      1.16  mycroft 		enable_intr();
    592      1.16  mycroft 	}
    593      1.16  mycroft 	last_pitch = pitch;
    594      1.16  mycroft 	beeping = last_period = period;
    595      1.24  deraadt 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    596      1.16  mycroft 
    597      1.16  mycroft 	splx(s);
    598       1.1      cgd }
    599       1.1      cgd 
    600       1.1      cgd /*
    601       1.1      cgd  * Pass command to keyboard controller (8042)
    602       1.1      cgd  */
    603      1.21   andrew unsigned
    604      1.21   andrew kbc_8042cmd(int val)
    605      1.21   andrew {
    606       1.1      cgd 	while (inb(KBSTATP)&KBS_IBF);
    607       1.1      cgd 	if (val) outb(KBCMDP, val);
    608       1.1      cgd 	while (inb(KBSTATP)&KBS_IBF);
    609       1.1      cgd 	return (inb(KBDATAP));
    610      1.28   brezak }
    611      1.28   brezak 
    612      1.28   brezak /*
    613      1.28   brezak  * find an ISA device in a given isa_devtab_* table, given
    614      1.28   brezak  * the table to search, the expected id_driver entry, and the unit number.
    615      1.28   brezak  *
    616      1.28   brezak  * this function is defined in isa_device.h, and this location is debatable;
    617      1.28   brezak  * i put it there because it's useless w/o, and directly operates on
    618      1.28   brezak  * the other stuff in that file.
    619      1.28   brezak  *
    620      1.28   brezak  */
    621      1.28   brezak 
    622      1.28   brezak struct isa_device *find_isadev(table, driverp, unit)
    623      1.28   brezak 	struct isa_device *table;
    624      1.28   brezak 	struct isa_driver *driverp;
    625      1.28   brezak 	int unit;
    626      1.28   brezak {
    627      1.28   brezak 	if (driverp == NULL) /* sanity check */
    628      1.28   brezak 		return NULL;
    629      1.28   brezak 
    630      1.28   brezak 	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
    631      1.28   brezak 		if (table->id_driver == 0)
    632      1.28   brezak 			return NULL;
    633      1.28   brezak 
    634      1.28   brezak 		table++;
    635      1.28   brezak         }
    636      1.28   brezak 
    637      1.28   brezak 	return table;
    638      1.15      cgd }
    639      1.15      cgd 
    640      1.15      cgd /*
    641      1.15      cgd  * Return nonzero if a (masked) irq is pending for a given device.
    642      1.15      cgd  */
    643      1.15      cgd int
    644      1.15      cgd isa_irq_pending(dvp)
    645      1.15      cgd 	struct isa_device *dvp;
    646      1.15      cgd {
    647      1.15      cgd 	unsigned id_irq;
    648      1.15      cgd 
    649      1.15      cgd 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    650      1.15      cgd 	if (id_irq & 0xff)
    651      1.15      cgd 		return (inb(IO_ICU1) & id_irq);
    652      1.15      cgd 	return (inb(IO_ICU2) & (id_irq >> 8));
    653       1.1      cgd }
    654