Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.41
      1   1.1      cgd /*-
      2  1.40  mycroft  * Copyright (c) 1993, 1994 Charles Hannum.
      3   1.1      cgd  * Copyright (c) 1991 The Regents of the University of California.
      4   1.1      cgd  * All rights reserved.
      5   1.1      cgd  *
      6   1.1      cgd  * This code is derived from software contributed to Berkeley by
      7   1.1      cgd  * William Jolitz.
      8   1.1      cgd  *
      9   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     10   1.1      cgd  * modification, are permitted provided that the following conditions
     11   1.1      cgd  * are met:
     12   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     14   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     17   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     18   1.1      cgd  *    must display the following acknowledgement:
     19   1.1      cgd  *	This product includes software developed by the University of
     20   1.1      cgd  *	California, Berkeley and its contributors.
     21   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     22   1.1      cgd  *    may be used to endorse or promote products derived from this software
     23   1.1      cgd  *    without specific prior written permission.
     24   1.1      cgd  *
     25   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1      cgd  * SUCH DAMAGE.
     36   1.1      cgd  *
     37  1.13      cgd  *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
     38  1.41  mycroft  *	$Id: isa.c,v 1.41 1994/03/08 13:24:57 mycroft Exp $
     39   1.1      cgd  */
     40   1.1      cgd 
     41   1.1      cgd /*
     42   1.1      cgd  * code to manage AT bus
     43   1.2      cgd  *
     44   1.2      cgd  * 92/08/18  Frank P. MacLachlan (fpm (at) crash.cts.com):
     45   1.2      cgd  * Fixed uninitialized variable problem and added code to deal
     46   1.2      cgd  * with DMA page boundaries in isa_dmarangecheck().  Fixed word
     47   1.2      cgd  * mode DMA count compution and reorganized DMA setup code in
     48   1.2      cgd  * isa_dmastart()
     49   1.1      cgd  */
     50   1.1      cgd 
     51  1.31  mycroft #include <sys/param.h>
     52  1.31  mycroft #include <sys/systm.h>
     53  1.40  mycroft #include <sys/kernel.h>
     54  1.31  mycroft #include <sys/conf.h>
     55  1.31  mycroft #include <sys/file.h>
     56  1.31  mycroft #include <sys/buf.h>
     57  1.31  mycroft #include <sys/uio.h>
     58  1.31  mycroft #include <sys/syslog.h>
     59  1.31  mycroft #include <sys/malloc.h>
     60  1.31  mycroft 
     61  1.31  mycroft #include <vm/vm.h>
     62  1.31  mycroft 
     63  1.41  mycroft #include <machine/cpu.h>
     64  1.32  mycroft #include <machine/pio.h>
     65  1.31  mycroft #include <machine/cpufunc.h>
     66  1.31  mycroft 
     67  1.31  mycroft #include <i386/isa/isa_device.h>
     68  1.31  mycroft #include <i386/isa/isa.h>
     69  1.31  mycroft #include <i386/isa/icu.h>
     70  1.31  mycroft #include <i386/isa/ic/i8237.h>
     71  1.31  mycroft #include <i386/isa/ic/i8042.h>
     72  1.31  mycroft #include <i386/isa/timerreg.h>
     73  1.31  mycroft #include <i386/isa/spkr_reg.h>
     74  1.14  deraadt 
     75  1.14  deraadt /* sorry, has to be here, no place else really suitable */
     76  1.31  mycroft #include <machine/pc/display.h>
     77  1.14  deraadt u_short *Crtat = (u_short *)MONO_BUF;
     78   1.1      cgd 
     79   1.2      cgd /*
     80  1.38  mycroft  * Register definitions for DMA controller 1 (channels 0..3):
     81  1.38  mycroft  */
     82   1.2      cgd #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     83  1.37  mycroft #define	DMA1_SR		(IO_DMA1 + 1*8)		/* status register */
     84   1.2      cgd #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     85   1.2      cgd #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     86   1.2      cgd #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     87   1.2      cgd 
     88   1.2      cgd /*
     89  1.38  mycroft  * Register definitions for DMA controller 2 (channels 4..7):
     90  1.38  mycroft  */
     91  1.30  mycroft #define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
     92  1.37  mycroft #define	DMA2_SR		(IO_DMA2 + 2*8)		/* status register */
     93   1.2      cgd #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     94   1.2      cgd #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     95   1.2      cgd #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     96   1.2      cgd 
     97  1.15      cgd int config_isadev(struct isa_device *, u_int *);
     98   1.6  deraadt void config_attach(struct isa_driver *, struct isa_device *);
     99  1.21   andrew static void sysbeepstop(int);
    100   1.1      cgd 
    101   1.1      cgd /*
    102   1.1      cgd  * Configure all ISA devices
    103   1.1      cgd  */
    104  1.21   andrew void
    105  1.30  mycroft isa_configure()
    106  1.30  mycroft {
    107   1.1      cgd 	struct isa_device *dvp;
    108   1.1      cgd 	struct isa_driver *dp;
    109   1.1      cgd 
    110  1.41  mycroft 	(void) splhigh();
    111   1.1      cgd 	INTREN(IRQ_SLAVE);
    112  1.30  mycroft 	enable_intr();
    113  1.30  mycroft 
    114  1.41  mycroft 	for (dvp = isa_devtab_tty; config_isadev(dvp, &imask[PIL_TTY]); dvp++)
    115   1.6  deraadt 		;
    116  1.41  mycroft 	for (dvp = isa_devtab_bio; config_isadev(dvp, &imask[PIL_BIO]); dvp++)
    117   1.6  deraadt 		;
    118  1.41  mycroft 	for (dvp = isa_devtab_net; config_isadev(dvp, &imask[PIL_IMP]); dvp++)
    119   1.6  deraadt 		;
    120  1.15      cgd 	for (dvp = isa_devtab_null; config_isadev(dvp, (u_int *) NULL); dvp++)
    121   1.6  deraadt 		;
    122   1.7      cgd 
    123  1.41  mycroft 	printf("biomask %x ttymask %x impmask %x\n",
    124  1.41  mycroft 	       imask[PIL_BIO], imask[PIL_TTY], imask[PIL_IMP]);
    125  1.30  mycroft 
    126  1.41  mycroft 	imask[PIL_AST] |= 0x80000000;	/* must be non-zero */
    127  1.41  mycroft 	imask[PIL_IMP] |= imask[PIL_AST];
    128  1.41  mycroft 	imask[PIL_TTY] |= imask[PIL_AST];
    129  1.41  mycroft 	imask[PIL_BIO] |= imask[PIL_AST];
    130  1.41  mycroft 	imask[PIL_CLOCK] |= imask[PIL_AST];
    131  1.26  mycroft 
    132  1.30  mycroft 	spl0();
    133   1.1      cgd }
    134   1.1      cgd 
    135   1.1      cgd /*
    136   1.1      cgd  * Configure an ISA device.
    137   1.1      cgd  */
    138  1.21   andrew int
    139   1.1      cgd config_isadev(isdp, mp)
    140   1.1      cgd 	struct isa_device *isdp;
    141  1.15      cgd 	u_int *mp;
    142   1.1      cgd {
    143   1.1      cgd 	struct isa_driver *dp;
    144   1.1      cgd 
    145   1.1      cgd 	if (dp = isdp->id_driver) {
    146   1.1      cgd 		if (isdp->id_maddr) {
    147   1.1      cgd 			extern u_int atdevbase;
    148   1.1      cgd 
    149  1.15      cgd 			isdp->id_maddr -= 0xa0000; /* XXX should be a define */
    150   1.1      cgd 			isdp->id_maddr += atdevbase;
    151   1.1      cgd 		}
    152   1.1      cgd 		isdp->id_alive = (*dp->probe)(isdp);
    153  1.11  deraadt 		if (isdp->id_irq == (u_short)-1)
    154  1.11  deraadt 			isdp->id_alive = 0;
    155  1.15      cgd 		/*
    156  1.15      cgd 		 * Only print the I/O address range if id_alive != -1
    157  1.15      cgd 		 * Right now this is a temporary fix just for the new
    158  1.15      cgd 		 * NPX code so that if it finds a 486 that can use trap
    159  1.15      cgd 		 * 16 it will not report I/O addresses.
    160  1.15      cgd 		 * Rod Grimes 04/26/94
    161  1.15      cgd 		 *
    162  1.15      cgd 		 * XXX -- cgd
    163  1.15      cgd 		 */
    164   1.1      cgd 		if (isdp->id_alive) {
    165   1.1      cgd 			printf("%s%d", dp->name, isdp->id_unit);
    166  1.27      cgd 			if (isdp->id_iobase) {
    167  1.27      cgd 				printf(" at 0x%x", isdp->id_iobase);
    168  1.27      cgd 				if ((isdp->id_iobase + isdp->id_alive - 1) !=
    169  1.27      cgd 				    isdp->id_iobase)
    170  1.27      cgd 					printf("-0x%x", isdp->id_iobase +
    171  1.27      cgd 					    isdp->id_alive - 1);
    172  1.27      cgd 			}
    173  1.11  deraadt 			if (isdp->id_irq != 0)
    174  1.15      cgd 				printf(" irq %d", ffs(isdp->id_irq)-1);
    175   1.3  deraadt 			if (isdp->id_drq != -1)
    176  1.15      cgd 				printf(" drq %d", isdp->id_drq);
    177   1.4      cgd 			if (isdp->id_maddr != 0)
    178  1.15      cgd 				printf(" maddr 0x%x", kvtop(isdp->id_maddr));
    179   1.4      cgd 			if (isdp->id_msize != 0)
    180  1.20  deraadt 				printf("-0x%x", kvtop(isdp->id_maddr) +
    181  1.20  deraadt 					isdp->id_msize - 1);
    182   1.4      cgd 			if (isdp->id_flags != 0)
    183  1.15      cgd 				printf(" flags 0x%x", isdp->id_flags);
    184  1.15      cgd 			printf(" on isa\n");
    185   1.3  deraadt 
    186   1.6  deraadt 			config_attach(dp, isdp);
    187   1.6  deraadt 
    188  1.12  deraadt 			if (isdp->id_irq) {
    189   1.1      cgd 				int intrno;
    190   1.1      cgd 
    191   1.1      cgd 				intrno = ffs(isdp->id_irq)-1;
    192  1.15      cgd 				setidt(ICU_OFFSET+intrno, isdp->id_intr,
    193  1.15      cgd 					 SDT_SYS386IGT, SEL_KPL);
    194   1.3  deraadt 				if(mp)
    195   1.3  deraadt 					INTRMASK(*mp,isdp->id_irq);
    196  1.15      cgd 				INTREN(isdp->id_irq);
    197   1.1      cgd 			}
    198   1.1      cgd 		}
    199   1.1      cgd 		return (1);
    200   1.1      cgd 	} else	return(0);
    201   1.1      cgd }
    202   1.6  deraadt 
    203   1.6  deraadt void
    204   1.6  deraadt config_attach(struct isa_driver *dp, struct isa_device *isdp)
    205   1.6  deraadt {
    206   1.6  deraadt 	extern struct isa_device isa_subdev[];
    207   1.6  deraadt 	struct isa_device *dvp;
    208   1.6  deraadt 
    209   1.6  deraadt 	if(isdp->id_masunit==-1) {
    210   1.8  deraadt 		(void)(*dp->attach)(isdp);
    211   1.6  deraadt 		return;
    212   1.6  deraadt 	}
    213   1.6  deraadt 
    214   1.6  deraadt 	if(isdp->id_masunit==0) {
    215   1.6  deraadt 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    216   1.6  deraadt 			if (dvp->id_driver != dp)
    217   1.6  deraadt 				continue;
    218  1.10  deraadt 			if (dvp->id_masunit != isdp->id_unit)
    219   1.6  deraadt 				continue;
    220   1.6  deraadt 			if (dvp->id_physid == -1)
    221   1.6  deraadt 				continue;
    222   1.8  deraadt 			dvp->id_alive = (*dp->attach)(dvp);
    223   1.6  deraadt 		}
    224   1.6  deraadt 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    225   1.6  deraadt 			if (dvp->id_driver != dp)
    226   1.6  deraadt 				continue;
    227  1.10  deraadt 			if (dvp->id_masunit != isdp->id_unit)
    228   1.6  deraadt 				continue;
    229   1.6  deraadt 			if (dvp->id_physid != -1)
    230   1.6  deraadt 				continue;
    231   1.8  deraadt 			dvp->id_alive = (*dp->attach)(dvp);
    232   1.6  deraadt 		}
    233   1.6  deraadt 		return;
    234   1.6  deraadt 	}
    235   1.6  deraadt 	printf("id_masunit has weird value\n");
    236   1.6  deraadt }
    237   1.6  deraadt 
    238   1.1      cgd 
    239   1.1      cgd #define	IDTVEC(name)	__CONCAT(X,name)
    240   1.1      cgd /* default interrupt vector table entries */
    241   1.1      cgd extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    242   1.1      cgd 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    243   1.1      cgd 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    244   1.1      cgd 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    245   1.1      cgd 
    246   1.1      cgd static *defvec[16] = {
    247   1.1      cgd 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    248   1.1      cgd 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    249   1.1      cgd 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    250   1.1      cgd 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    251   1.1      cgd 
    252   1.1      cgd /* out of range default interrupt vector gate entry */
    253   1.1      cgd extern	IDTVEC(intrdefault);
    254  1.15      cgd 
    255   1.1      cgd /*
    256   1.1      cgd  * Fill in default interrupt table (in case of spuruious interrupt
    257   1.1      cgd  * during configuration of kernel, setup interrupt control unit
    258   1.1      cgd  */
    259  1.21   andrew void
    260   1.1      cgd isa_defaultirq() {
    261   1.1      cgd 	int i;
    262   1.1      cgd 
    263   1.1      cgd 	/* icu vectors */
    264   1.1      cgd 	for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++)
    265   1.1      cgd 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    266  1.15      cgd 
    267   1.1      cgd 	/* out of range vectors */
    268   1.1      cgd 	for (i = NRSVIDT; i < NIDT; i++)
    269   1.1      cgd 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    270   1.1      cgd 
    271   1.1      cgd 	/* initialize 8259's */
    272   1.1      cgd 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    273   1.1      cgd 	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
    274   1.1      cgd 	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
    275  1.15      cgd #ifdef AUTO_EOI_1
    276  1.15      cgd 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    277  1.15      cgd #else
    278   1.1      cgd 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    279  1.15      cgd #endif
    280   1.1      cgd 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    281  1.15      cgd 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    282  1.21   andrew #ifdef REORDER_IRQ
    283  1.15      cgd 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    284  1.21   andrew #endif
    285   1.1      cgd 
    286   1.1      cgd 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    287   1.1      cgd 	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
    288   1.1      cgd 	outb(IO_ICU2+1,2);		/* my slave id is 2 */
    289  1.15      cgd #ifdef AUTO_EOI_2
    290  1.15      cgd 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    291  1.15      cgd #else
    292   1.1      cgd 	outb(IO_ICU2+1,1);		/* 8086 mode */
    293  1.15      cgd #endif
    294   1.1      cgd 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    295  1.15      cgd 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    296   1.1      cgd }
    297   1.1      cgd 
    298   1.1      cgd /* region of physical memory known to be contiguous */
    299   1.1      cgd vm_offset_t isaphysmem;
    300   1.1      cgd static caddr_t dma_bounce[8];		/* XXX */
    301   1.1      cgd static char bounced[8];		/* XXX */
    302   1.1      cgd #define MAXDMASZ 512		/* XXX */
    303   1.1      cgd 
    304   1.1      cgd /* high byte of address is stored in this port for i-th dma channel */
    305   1.1      cgd static short dmapageport[8] =
    306   1.1      cgd 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    307   1.1      cgd 
    308   1.1      cgd /*
    309   1.1      cgd  * isa_dmacascade(): program 8237 DMA controller channel to accept
    310   1.1      cgd  * external dma control by a board.
    311   1.1      cgd  */
    312  1.21   andrew void
    313  1.37  mycroft isa_dmacascade(chan)
    314  1.37  mycroft 	int chan;
    315   1.2      cgd {
    316  1.37  mycroft 
    317  1.37  mycroft #ifdef DIAGNOSTIC
    318  1.37  mycroft 	if (chan < 0 || chan > 7)
    319   1.1      cgd 		panic("isa_dmacascade: impossible request");
    320  1.37  mycroft #endif
    321   1.1      cgd 
    322   1.1      cgd 	/* set dma channel mode, and set dma channel mode */
    323   1.2      cgd 	if ((chan & 4) == 0) {
    324   1.2      cgd 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    325   1.2      cgd 		outb(DMA1_SMSK, chan);
    326   1.2      cgd 	} else {
    327   1.2      cgd 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    328   1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    329   1.2      cgd 	}
    330   1.1      cgd }
    331   1.1      cgd 
    332   1.1      cgd /*
    333   1.1      cgd  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
    334   1.1      cgd  * problems by using a bounce buffer.
    335   1.1      cgd  */
    336  1.21   andrew void
    337  1.37  mycroft isa_dmastart(flags, addr, nbytes, chan)
    338  1.37  mycroft 	int flags;
    339  1.37  mycroft 	caddr_t addr;
    340  1.37  mycroft 	vm_size_t nbytes;
    341  1.37  mycroft 	int chan;
    342  1.37  mycroft {
    343  1.37  mycroft 	vm_offset_t phys;
    344   1.2      cgd 	int waport;
    345   1.1      cgd 	caddr_t newaddr;
    346   1.1      cgd 
    347  1.37  mycroft #ifdef DIAGNOSTIC
    348  1.37  mycroft 	if (chan < 0 || chan > 7 ||
    349  1.37  mycroft 	    ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
    350  1.37  mycroft 	    (nbytes >= (1<<16))))
    351   1.1      cgd 		panic("isa_dmastart: impossible request");
    352  1.37  mycroft #endif
    353   1.1      cgd 
    354   1.2      cgd 	if (isa_dmarangecheck(addr, nbytes, chan)) {
    355   1.1      cgd 		if (dma_bounce[chan] == 0)
    356   1.1      cgd 			dma_bounce[chan] =
    357  1.37  mycroft 			    /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    358  1.37  mycroft 			    (caddr_t) isaphysmem + NBPG*chan;
    359   1.1      cgd 		bounced[chan] = 1;
    360   1.1      cgd 		newaddr = dma_bounce[chan];
    361   1.1      cgd 		*(int *) newaddr = 0;	/* XXX */
    362   1.1      cgd 		/* copy bounce buffer on write */
    363  1.37  mycroft 		if ((flags & B_READ) == 0)
    364   1.1      cgd 			bcopy(addr, newaddr, nbytes);
    365   1.1      cgd 		addr = newaddr;
    366   1.1      cgd 	}
    367   1.1      cgd 
    368   1.1      cgd 	/* translate to physical */
    369   1.1      cgd 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    370   1.1      cgd 
    371   1.2      cgd 	if ((chan & 4) == 0) {
    372   1.2      cgd 		/*
    373   1.2      cgd 		 * Program one of DMA channels 0..3.  These are
    374   1.2      cgd 		 * byte mode channels.
    375   1.2      cgd 		 */
    376   1.2      cgd 		/* set dma channel mode, and reset address ff */
    377   1.2      cgd 		if (flags & B_READ)
    378  1.37  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_WRITE);
    379   1.2      cgd 		else
    380  1.37  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_READ);
    381   1.2      cgd 		outb(DMA1_FFC, 0);
    382   1.1      cgd 
    383   1.2      cgd 		/* send start address */
    384   1.2      cgd 		waport =  DMA1_CHN(chan);
    385   1.1      cgd 		outb(waport, phys);
    386   1.1      cgd 		outb(waport, phys>>8);
    387   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    388   1.2      cgd 
    389   1.2      cgd 		/* send count */
    390   1.2      cgd 		outb(waport + 1, --nbytes);
    391   1.2      cgd 		outb(waport + 1, nbytes>>8);
    392   1.2      cgd 
    393   1.2      cgd 		/* unmask channel */
    394  1.37  mycroft 		outb(DMA1_SMSK, chan | DMA37SM_CLEAR);
    395   1.1      cgd 	} else {
    396   1.2      cgd 		/*
    397   1.2      cgd 		 * Program one of DMA channels 4..7.  These are
    398   1.2      cgd 		 * word mode channels.
    399   1.2      cgd 		 */
    400   1.2      cgd 		/* set dma channel mode, and reset address ff */
    401   1.2      cgd 		if (flags & B_READ)
    402  1.37  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_WRITE);
    403   1.2      cgd 		else
    404  1.37  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_READ);
    405   1.2      cgd 		outb(DMA2_FFC, 0);
    406   1.2      cgd 
    407   1.2      cgd 		/* send start address */
    408  1.37  mycroft 		waport = DMA2_CHN(chan & 3);
    409   1.1      cgd 		outb(waport, phys>>1);
    410   1.1      cgd 		outb(waport, phys>>9);
    411   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    412   1.1      cgd 
    413   1.2      cgd 		/* send count */
    414   1.2      cgd 		nbytes >>= 1;
    415   1.1      cgd 		outb(waport + 2, --nbytes);
    416   1.1      cgd 		outb(waport + 2, nbytes>>8);
    417   1.2      cgd 
    418   1.2      cgd 		/* unmask channel */
    419  1.37  mycroft 		outb(DMA2_SMSK, (chan & 3) | DMA37SM_CLEAR);
    420   1.1      cgd 	}
    421   1.1      cgd }
    422   1.1      cgd 
    423  1.21   andrew void
    424  1.37  mycroft isa_dmadone(flags, addr, nbytes, chan)
    425  1.37  mycroft 	int flags;
    426  1.37  mycroft 	caddr_t addr;
    427  1.37  mycroft 	vm_size_t nbytes;
    428  1.37  mycroft 	int chan;
    429   1.1      cgd {
    430  1.37  mycroft 	u_char tc;
    431  1.37  mycroft 
    432  1.37  mycroft #ifdef DIAGNOSTIC
    433  1.37  mycroft 	if (chan < 0 || chan > 7)
    434  1.37  mycroft 		panic("isa_dmadone: impossible request");
    435  1.37  mycroft #endif
    436  1.37  mycroft 
    437  1.37  mycroft 	/* check that the terminal count was reached */
    438  1.37  mycroft 	if ((chan & 4) == 0)
    439  1.37  mycroft 		tc = inb(DMA1_SR) & (1 << chan);
    440  1.37  mycroft 	else
    441  1.37  mycroft 		tc = inb(DMA2_SR) & (1 << (chan & 3));
    442  1.37  mycroft 	if (tc == 0)
    443  1.37  mycroft 		/* XXX probably should panic or something */
    444  1.37  mycroft 		log(LOG_ERR, "dma channel %d not finished\n", chan);
    445   1.1      cgd 
    446   1.1      cgd 	/* copy bounce buffer on read */
    447   1.1      cgd 	if (bounced[chan]) {
    448   1.1      cgd 		bcopy(dma_bounce[chan], addr, nbytes);
    449   1.1      cgd 		bounced[chan] = 0;
    450   1.1      cgd 	}
    451  1.37  mycroft 
    452  1.37  mycroft 	/* mask channel */
    453  1.37  mycroft 	if ((chan & 4) == 0)
    454  1.37  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    455  1.37  mycroft 	else
    456  1.37  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    457   1.1      cgd }
    458   1.1      cgd 
    459   1.1      cgd /*
    460   1.1      cgd  * Check for problems with the address range of a DMA transfer
    461   1.2      cgd  * (non-contiguous physical pages, outside of bus address space,
    462   1.2      cgd  * crossing DMA page boundaries).
    463   1.1      cgd  * Return true if special handling needed.
    464   1.1      cgd  */
    465  1.21   andrew int
    466  1.37  mycroft isa_dmarangecheck(va, length, chan)
    467  1.37  mycroft 	vm_offset_t va;
    468  1.37  mycroft 	u_long length;
    469  1.37  mycroft 	int chan;
    470  1.37  mycroft {
    471   1.2      cgd 	vm_offset_t phys, priorpage = 0, endva;
    472   1.2      cgd 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    473   1.1      cgd 
    474  1.37  mycroft 	endva = round_page(va + length);
    475  1.37  mycroft 	for (; va < endva ; va += NBPG) {
    476  1.37  mycroft 		phys = trunc_page(pmap_extract(pmap_kernel(), va));
    477   1.1      cgd 		if (phys == 0)
    478   1.1      cgd 			panic("isa_dmacheck: no physical page present");
    479  1.37  mycroft 		if (phys >= (1<<24))
    480  1.37  mycroft 			return 1;
    481   1.2      cgd 		if (priorpage) {
    482   1.2      cgd 			if (priorpage + NBPG != phys)
    483  1.37  mycroft 				return 1;
    484   1.2      cgd 			/* check if crossing a DMA page boundary */
    485  1.37  mycroft 			if ((priorpage ^ phys) & dma_pgmsk)
    486  1.37  mycroft 				return 1;
    487   1.2      cgd 		}
    488   1.1      cgd 		priorpage = phys;
    489   1.1      cgd 	}
    490  1.37  mycroft 	return 0;
    491   1.1      cgd }
    492   1.1      cgd 
    493   1.1      cgd /* head of queue waiting for physmem to become available */
    494   1.1      cgd struct buf isa_physmemq;
    495   1.1      cgd 
    496   1.1      cgd /* blocked waiting for resource to become free for exclusive use */
    497   1.1      cgd static isaphysmemflag;
    498   1.1      cgd /* if waited for and call requested when free (B_CALL) */
    499   1.1      cgd static void (*isaphysmemunblock)(); /* needs to be a list */
    500   1.1      cgd 
    501   1.1      cgd /*
    502   1.1      cgd  * Allocate contiguous physical memory for transfer, returning
    503   1.1      cgd  * a *virtual* address to region. May block waiting for resource.
    504   1.1      cgd  * (assumed to be called at splbio())
    505   1.1      cgd  */
    506   1.1      cgd caddr_t
    507   1.1      cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    508   1.1      cgd 
    509   1.1      cgd 	isaphysmemunblock = func;
    510   1.1      cgd 	while (isaphysmemflag & B_BUSY) {
    511   1.1      cgd 		isaphysmemflag |= B_WANTED;
    512  1.24  deraadt 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    513   1.1      cgd 	}
    514   1.1      cgd 	isaphysmemflag |= B_BUSY;
    515   1.1      cgd 
    516   1.1      cgd 	return((caddr_t)isaphysmem);
    517   1.1      cgd }
    518   1.1      cgd 
    519   1.1      cgd /*
    520   1.1      cgd  * Free contiguous physical memory used for transfer.
    521   1.1      cgd  * (assumed to be called at splbio())
    522   1.1      cgd  */
    523   1.1      cgd void
    524   1.1      cgd isa_freephysmem(caddr_t va, unsigned length) {
    525   1.1      cgd 
    526   1.1      cgd 	isaphysmemflag &= ~B_BUSY;
    527   1.1      cgd 	if (isaphysmemflag & B_WANTED) {
    528   1.1      cgd 		isaphysmemflag &= B_WANTED;
    529  1.24  deraadt 		wakeup((caddr_t)&isaphysmemflag);
    530   1.1      cgd 		if (isaphysmemunblock)
    531   1.1      cgd 			(*isaphysmemunblock)();
    532   1.1      cgd 	}
    533   1.1      cgd }
    534   1.1      cgd 
    535   1.1      cgd /*
    536   1.1      cgd  * Handle a NMI, possibly a machine check.
    537   1.1      cgd  * return true to panic system, false to ignore.
    538   1.1      cgd  */
    539  1.21   andrew int
    540   1.1      cgd isa_nmi(cd) {
    541   1.1      cgd 
    542   1.1      cgd 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    543   1.1      cgd 	return(0);
    544   1.1      cgd }
    545   1.1      cgd 
    546   1.1      cgd /*
    547   1.1      cgd  * Caught a stray interrupt, notify
    548   1.1      cgd  */
    549  1.21   andrew void
    550   1.1      cgd isa_strayintr(d) {
    551   1.1      cgd 
    552   1.1      cgd 	/* DON'T BOTHER FOR NOW! */
    553   1.1      cgd 	/* for some reason, we get bursts of intr #7, even if not enabled! */
    554   1.4      cgd 	/*
    555   1.4      cgd 	 * Well the reason you got bursts of intr #7 is because someone
    556   1.4      cgd 	 * raised an interrupt line and dropped it before the 8259 could
    557   1.4      cgd 	 * prioritize it.  This is documented in the intel data book.  This
    558   1.4      cgd 	 * means you have BAD hardware!  I have changed this so that only
    559  1.15      cgd 	 * the first 5 get logged, then it quits logging them, and puts
    560   1.4      cgd 	 * out a special message. rgrimes 3/25/1993
    561   1.4      cgd 	 */
    562  1.15      cgd 	extern u_long intrcnt_stray;
    563   1.4      cgd 
    564  1.15      cgd 	intrcnt_stray++;
    565  1.15      cgd 	if (intrcnt_stray <= 5)
    566   1.4      cgd 		log(LOG_ERR,"ISA strayintr %x\n", d);
    567  1.15      cgd 	if (intrcnt_stray == 5)
    568   1.4      cgd 		log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
    569   1.1      cgd }
    570   1.1      cgd 
    571   1.1      cgd /*
    572  1.15      cgd  * Wait "n" microseconds.
    573  1.40  mycroft  * Relies on timer 1 counting down from (TIMER_FREQ / hz) at TIMER_FREQ Hz.
    574  1.15      cgd  * Note: timer had better have been programmed before this is first used!
    575  1.17  mycroft  * (Note that we use `rate generator' mode, which counts at 1:1; `square
    576  1.17  mycroft  * wave' mode counts at 2:1).
    577  1.15      cgd  */
    578  1.21   andrew void
    579  1.39  mycroft delay(n)
    580  1.15      cgd 	int n;
    581  1.15      cgd {
    582  1.40  mycroft 	int limit, tick, otick;
    583  1.15      cgd 
    584  1.15      cgd 	/*
    585  1.15      cgd 	 * Read the counter first, so that the rest of the setup overhead is
    586  1.40  mycroft 	 * counted.
    587  1.15      cgd 	 */
    588  1.40  mycroft 	otick = gettick();
    589  1.15      cgd 
    590  1.40  mycroft #ifdef __GNUC__
    591  1.15      cgd 	/*
    592  1.40  mycroft 	 * Calculate ((n * TIMER_FREQ) / 1e6) using explicit assembler code so
    593  1.40  mycroft 	 * we can take advantage of the intermediate 64-bit quantity to prevent
    594  1.40  mycroft 	 * loss of significance.
    595  1.15      cgd 	 */
    596  1.40  mycroft 	n -= 5;
    597  1.40  mycroft 	if (n < 0)
    598  1.40  mycroft 		return;
    599  1.40  mycroft 	{register int m;
    600  1.40  mycroft 	__asm __volatile("mul %3"
    601  1.40  mycroft 			 : "=a" (n), "=d" (m)
    602  1.40  mycroft 			 : "0" (n), "r" (TIMER_FREQ));
    603  1.40  mycroft 	__asm __volatile("div %3"
    604  1.40  mycroft 			 : "=a" (n)
    605  1.40  mycroft 			 : "0" (n), "d" (m), "r" (1000000)
    606  1.40  mycroft 			 : "%edx");}
    607  1.40  mycroft #else
    608  1.40  mycroft 	/*
    609  1.40  mycroft 	 * Calculate ((n * TIMER_FREQ) / 1e6) without using floating point and
    610  1.40  mycroft 	 * without any avoidable overflows.
    611  1.40  mycroft 	 */
    612  1.40  mycroft 	n -= 20;
    613  1.40  mycroft 	{
    614  1.40  mycroft 		int sec = n / 1000000,
    615  1.40  mycroft 		    usec = n % 1000000;
    616  1.40  mycroft 		n = sec * TIMER_FREQ +
    617  1.40  mycroft 		    usec * (TIMER_FREQ / 1000000) +
    618  1.40  mycroft 		    usec * ((TIMER_FREQ % 1000000) / 1000) / 1000 +
    619  1.40  mycroft 		    usec * (TIMER_FREQ % 1000) / 1000000;
    620  1.40  mycroft 	}
    621  1.40  mycroft #endif
    622  1.40  mycroft 
    623  1.40  mycroft 	limit = TIMER_FREQ / hz;
    624  1.15      cgd 
    625  1.40  mycroft 	while (n > 0) {
    626  1.16  mycroft 		tick = gettick();
    627  1.40  mycroft 		if (tick > otick)
    628  1.40  mycroft 			n -= limit - (tick - otick);
    629  1.15      cgd 		else
    630  1.40  mycroft 			n -= otick - tick;
    631  1.40  mycroft 		otick = tick;
    632   1.1      cgd 	}
    633   1.1      cgd }
    634   1.1      cgd 
    635  1.21   andrew int
    636  1.40  mycroft gettick()
    637  1.40  mycroft {
    638  1.40  mycroft 	u_char lo, hi;
    639  1.15      cgd 
    640  1.40  mycroft 	/* Don't want someone screwing with the counter while we're here. */
    641  1.15      cgd 	disable_intr();
    642  1.40  mycroft 	/* Select counter 0 and latch it. */
    643  1.16  mycroft 	outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
    644  1.40  mycroft 	lo = inb(TIMER_CNTR0);
    645  1.40  mycroft 	hi = inb(TIMER_CNTR0);
    646  1.15      cgd 	enable_intr();
    647  1.40  mycroft 	return ((hi << 8) | lo);
    648   1.1      cgd }
    649   1.1      cgd 
    650   1.1      cgd static beeping;
    651  1.21   andrew static void
    652  1.21   andrew sysbeepstop(int f)
    653   1.1      cgd {
    654  1.16  mycroft 	int s = splhigh();
    655  1.16  mycroft 
    656   1.1      cgd 	/* disable counter 2 */
    657  1.16  mycroft 	disable_intr();
    658  1.28   brezak 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    659  1.16  mycroft 	enable_intr();
    660   1.1      cgd 	if (f)
    661  1.24  deraadt 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    662   1.1      cgd 	else
    663   1.1      cgd 		beeping = 0;
    664  1.16  mycroft 
    665  1.16  mycroft 	splx(s);
    666   1.1      cgd }
    667   1.1      cgd 
    668  1.21   andrew void
    669  1.21   andrew sysbeep(int pitch, int period)
    670   1.1      cgd {
    671  1.16  mycroft 	int s = splhigh();
    672  1.16  mycroft 	static int last_pitch, last_period;
    673   1.1      cgd 
    674  1.16  mycroft 	if (beeping) {
    675  1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    676  1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    677   1.1      cgd 	}
    678  1.16  mycroft 	if (!beeping || last_pitch != pitch) {
    679  1.16  mycroft 		/*
    680  1.16  mycroft 	 	* XXX - move timer stuff to clock.c.
    681  1.16  mycroft 	 	*/
    682  1.16  mycroft 		disable_intr();
    683  1.16  mycroft 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    684  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    685  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    686  1.28   brezak 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    687  1.16  mycroft 		enable_intr();
    688  1.16  mycroft 	}
    689  1.16  mycroft 	last_pitch = pitch;
    690  1.16  mycroft 	beeping = last_period = period;
    691  1.24  deraadt 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    692  1.16  mycroft 
    693  1.16  mycroft 	splx(s);
    694  1.28   brezak }
    695  1.28   brezak 
    696  1.28   brezak /*
    697  1.28   brezak  * find an ISA device in a given isa_devtab_* table, given
    698  1.28   brezak  * the table to search, the expected id_driver entry, and the unit number.
    699  1.28   brezak  *
    700  1.28   brezak  * this function is defined in isa_device.h, and this location is debatable;
    701  1.28   brezak  * i put it there because it's useless w/o, and directly operates on
    702  1.28   brezak  * the other stuff in that file.
    703  1.28   brezak  *
    704  1.28   brezak  */
    705  1.28   brezak 
    706  1.28   brezak struct isa_device *find_isadev(table, driverp, unit)
    707  1.28   brezak 	struct isa_device *table;
    708  1.28   brezak 	struct isa_driver *driverp;
    709  1.28   brezak 	int unit;
    710  1.28   brezak {
    711  1.28   brezak 	if (driverp == NULL) /* sanity check */
    712  1.28   brezak 		return NULL;
    713  1.28   brezak 
    714  1.28   brezak 	while ((table->id_driver != driverp) || (table->id_unit != unit)) {
    715  1.28   brezak 		if (table->id_driver == 0)
    716  1.28   brezak 			return NULL;
    717  1.28   brezak 
    718  1.28   brezak 		table++;
    719  1.40  mycroft 	}
    720  1.28   brezak 
    721  1.28   brezak 	return table;
    722  1.15      cgd }
    723  1.15      cgd 
    724  1.15      cgd /*
    725  1.15      cgd  * Return nonzero if a (masked) irq is pending for a given device.
    726  1.15      cgd  */
    727  1.15      cgd int
    728  1.15      cgd isa_irq_pending(dvp)
    729  1.15      cgd 	struct isa_device *dvp;
    730  1.15      cgd {
    731  1.15      cgd 	unsigned id_irq;
    732  1.15      cgd 
    733  1.15      cgd 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    734  1.15      cgd 	if (id_irq & 0xff)
    735  1.15      cgd 		return (inb(IO_ICU1) & id_irq);
    736  1.15      cgd 	return (inb(IO_ICU2) & (id_irq >> 8));
    737   1.1      cgd }
    738