Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.46
      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.46  mycroft  *	$Id: isa.c,v 1.46 1994/03/29 04:34:18 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.46  mycroft #include <sys/device.h>
     61  1.31  mycroft 
     62  1.31  mycroft #include <vm/vm.h>
     63  1.31  mycroft 
     64  1.42  mycroft #include <machine/segments.h>
     65  1.32  mycroft #include <machine/pio.h>
     66  1.31  mycroft #include <machine/cpufunc.h>
     67  1.31  mycroft 
     68  1.46  mycroft #include <i386/isa/isa.h>
     69  1.31  mycroft #include <i386/isa/isa_device.h>
     70  1.46  mycroft #include <i386/isa/isavar.h>
     71  1.31  mycroft #include <i386/isa/icu.h>
     72  1.31  mycroft #include <i386/isa/ic/i8237.h>
     73  1.31  mycroft #include <i386/isa/ic/i8042.h>
     74  1.31  mycroft #include <i386/isa/timerreg.h>
     75  1.31  mycroft #include <i386/isa/spkr_reg.h>
     76  1.14  deraadt 
     77  1.14  deraadt /* sorry, has to be here, no place else really suitable */
     78  1.31  mycroft #include <machine/pc/display.h>
     79  1.14  deraadt u_short *Crtat = (u_short *)MONO_BUF;
     80   1.1      cgd 
     81   1.2      cgd /*
     82  1.38  mycroft  * Register definitions for DMA controller 1 (channels 0..3):
     83  1.38  mycroft  */
     84   1.2      cgd #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     85  1.37  mycroft #define	DMA1_SR		(IO_DMA1 + 1*8)		/* status register */
     86   1.2      cgd #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     87   1.2      cgd #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     88   1.2      cgd #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     89   1.2      cgd 
     90   1.2      cgd /*
     91  1.38  mycroft  * Register definitions for DMA controller 2 (channels 4..7):
     92  1.38  mycroft  */
     93  1.30  mycroft #define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
     94  1.37  mycroft #define	DMA2_SR		(IO_DMA2 + 2*8)		/* status register */
     95   1.2      cgd #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     96   1.2      cgd #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     97   1.2      cgd #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     98   1.2      cgd 
     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.46  mycroft  *
    104  1.46  mycroft  * XXX This code is a hack.  It wants to be new config, but can't be until the
    105  1.46  mycroft  * interrupt system is redone.  For now, we do some gross hacks to make it look
    106  1.46  mycroft  * 99% like new config.
    107  1.46  mycroft  */
    108  1.46  mycroft static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    109  1.46  mycroft 
    110  1.46  mycroft struct cfdata *
    111  1.46  mycroft config_search(fn, parent, aux)
    112  1.46  mycroft 	cfmatch_t fn;
    113  1.46  mycroft 	struct device *parent;
    114  1.46  mycroft 	void *aux;
    115  1.46  mycroft {
    116  1.46  mycroft 	struct cfdata *cf = 0;
    117  1.46  mycroft 	struct device *dv = 0;
    118  1.46  mycroft 	size_t devsize;
    119  1.46  mycroft 	struct cfdriver *cd;
    120  1.46  mycroft 	struct isa_device *id,
    121  1.46  mycroft 	    *idp =  parent ? (void *)parent->dv_cfdata->cf_loc : 0;
    122  1.46  mycroft 
    123  1.46  mycroft 	for (id = isa_devtab; id->id_driver; id++) {
    124  1.46  mycroft 		if (id->id_state == FSTATE_FOUND)
    125  1.46  mycroft 			continue;
    126  1.46  mycroft 		if (id->id_parent != idp)
    127  1.46  mycroft 			continue;
    128  1.46  mycroft 		cd = id->id_driver;
    129  1.46  mycroft 		if (id->id_unit < cd->cd_ndevs) {
    130  1.46  mycroft 			if (cd->cd_devs[id->id_unit] != 0)
    131  1.46  mycroft 				continue;
    132  1.46  mycroft 		} else {
    133  1.46  mycroft 			int old = cd->cd_ndevs, new;
    134  1.46  mycroft 			void **nsp;
    135  1.46  mycroft 
    136  1.46  mycroft 			if (old == 0) {
    137  1.46  mycroft 				nsp = malloc(MINALLOCSIZE, M_DEVBUF, M_NOWAIT);
    138  1.46  mycroft 				if (!nsp)
    139  1.46  mycroft 					panic("config_search: creating dev array");
    140  1.46  mycroft 				bzero(nsp, MINALLOCSIZE);
    141  1.46  mycroft 				cd->cd_ndevs = MINALLOCSIZE / sizeof(void *);
    142  1.46  mycroft 			} else {
    143  1.46  mycroft 				new = old;
    144  1.46  mycroft 				do {
    145  1.46  mycroft 					new *= 2;
    146  1.46  mycroft 				} while (new <= id->id_unit);
    147  1.46  mycroft 				cd->cd_ndevs = new;
    148  1.46  mycroft 				nsp = malloc(new * sizeof(void *), M_DEVBUF,
    149  1.46  mycroft 				    M_NOWAIT);
    150  1.46  mycroft 				if (!nsp)
    151  1.46  mycroft 					panic("config_search: expanding dev array");
    152  1.46  mycroft 				bzero(nsp, new * sizeof(void *));
    153  1.46  mycroft 				bcopy(cd->cd_devs, nsp, old * sizeof(void *));
    154  1.46  mycroft 				free(cd->cd_devs, M_DEVBUF);
    155  1.46  mycroft 			}
    156  1.46  mycroft 			cd->cd_devs = nsp;
    157  1.46  mycroft 		}
    158  1.46  mycroft 		if (!cf) {
    159  1.46  mycroft 			cf = malloc(sizeof(struct cfdata), M_DEVBUF, M_NOWAIT);				if (!cf)
    160  1.46  mycroft 				panic("config_search: creating cfdata");
    161  1.46  mycroft 		}
    162  1.46  mycroft 		cf->cf_driver = cd;
    163  1.46  mycroft 		cf->cf_unit = id->id_unit;
    164  1.46  mycroft 		cf->cf_fstate = 0;
    165  1.46  mycroft 		cf->cf_loc = (void *)id;
    166  1.46  mycroft 		cf->cf_flags = id->id_flags;
    167  1.46  mycroft 		cf->cf_parents = 0;
    168  1.46  mycroft 		cf->cf_ivstubs = 0;
    169  1.46  mycroft 		if (dv && devsize != cd->cd_devsize) {
    170  1.46  mycroft 			free(dv, M_DEVBUF);
    171  1.46  mycroft 			dv = 0;
    172  1.46  mycroft 		}
    173  1.46  mycroft 		if (!dv) {
    174  1.46  mycroft 			devsize = cd->cd_devsize;
    175  1.46  mycroft 			dv = malloc(devsize, M_DEVBUF, M_NOWAIT);
    176  1.46  mycroft 			if (!dv)
    177  1.46  mycroft 				panic("config_search: creating softc");
    178  1.46  mycroft 		}
    179  1.46  mycroft 		bzero(dv, cd->cd_devsize);
    180  1.46  mycroft 		dv->dv_class = cd->cd_class;
    181  1.46  mycroft 		dv->dv_cfdata = cf;
    182  1.46  mycroft 		dv->dv_unit = id->id_unit;
    183  1.46  mycroft 		sprintf(dv->dv_xname, "%s%d", cd->cd_name, id->id_unit);
    184  1.46  mycroft 		dv->dv_parent = parent;
    185  1.46  mycroft 		cd->cd_devs[id->id_unit] = dv;
    186  1.46  mycroft 		if (fn) {
    187  1.46  mycroft 			if ((*fn)(parent, dv, aux))
    188  1.46  mycroft 				return cf;
    189  1.46  mycroft 		} else {
    190  1.46  mycroft 			if ((*cd->cd_match)(parent, dv, aux))
    191  1.46  mycroft 				return cf;
    192  1.46  mycroft 		}
    193  1.46  mycroft 		if (id->id_state == FSTATE_FOUND) {
    194  1.46  mycroft 			cf = 0;
    195  1.46  mycroft 			dv = 0;
    196  1.46  mycroft 		} else
    197  1.46  mycroft 			cd->cd_devs[id->id_unit] = 0;
    198  1.46  mycroft 	}
    199  1.46  mycroft 	if (cf)
    200  1.46  mycroft 		free(cf, M_DEVBUF);
    201  1.46  mycroft 	if (dv)
    202  1.46  mycroft 		free(dv, M_DEVBUF);
    203  1.46  mycroft 	return 0;
    204  1.46  mycroft }
    205  1.46  mycroft 
    206  1.46  mycroft void
    207  1.46  mycroft config_attach(parent, cf, aux, print)
    208  1.46  mycroft 	struct device *parent;
    209  1.46  mycroft 	struct cfdata *cf;
    210  1.46  mycroft 	void *aux;
    211  1.46  mycroft 	cfprint_t print;
    212  1.46  mycroft {
    213  1.46  mycroft 	struct isa_device *id = (void *)cf->cf_loc;
    214  1.46  mycroft 	struct cfdriver *cd = cf->cf_driver;
    215  1.46  mycroft 	struct device *dv = cd->cd_devs[id->id_unit];
    216  1.46  mycroft 
    217  1.46  mycroft 	cf->cf_fstate = id->id_state = FSTATE_FOUND;
    218  1.46  mycroft 	printf("%s at %s", dv->dv_xname, parent ? parent->dv_xname : "isa0");
    219  1.46  mycroft 	if (print)
    220  1.46  mycroft 		(void) (*print)(aux, (char *)0);
    221  1.46  mycroft 	(*cd->cd_attach)(parent, dv, aux);
    222  1.46  mycroft }
    223  1.46  mycroft 
    224  1.46  mycroft int
    225  1.46  mycroft config_found(parent, aux, print)
    226  1.46  mycroft 	struct device *parent;
    227  1.46  mycroft 	void *aux;
    228  1.46  mycroft 	cfprint_t print;
    229  1.46  mycroft {
    230  1.46  mycroft 	struct cfdata *cf;
    231  1.46  mycroft 
    232  1.46  mycroft 	if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
    233  1.46  mycroft 		config_attach(parent, cf, aux, print);
    234  1.46  mycroft 		return 1;
    235  1.46  mycroft 	}
    236  1.46  mycroft 	if (print)
    237  1.46  mycroft 		printf(msgs[(*print)(aux, parent->dv_xname)]);
    238  1.46  mycroft 	return 0;
    239  1.46  mycroft }
    240  1.46  mycroft 
    241  1.46  mycroft int
    242  1.46  mycroft isaprint(aux, isa)
    243  1.46  mycroft 	void *aux;
    244  1.46  mycroft 	char *isa;
    245  1.46  mycroft {
    246  1.46  mycroft 	struct isa_attach_args *ia = aux;
    247  1.46  mycroft 
    248  1.46  mycroft 	if (ia->ia_iosize)
    249  1.46  mycroft 		printf(" port 0x%x", ia->ia_iobase);
    250  1.46  mycroft 	if (ia->ia_iosize > 1)
    251  1.46  mycroft 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    252  1.46  mycroft 	if (ia->ia_msize)
    253  1.46  mycroft 		printf(" iomem 0x%x", ia->ia_maddr - atdevbase + 0xa0000);
    254  1.46  mycroft 	if (ia->ia_msize > 1)
    255  1.46  mycroft 		printf("-0x%x",
    256  1.46  mycroft 		    ia->ia_maddr - atdevbase + 0xa0000 + ia->ia_msize - 1);
    257  1.46  mycroft 	if (ia->ia_irq)
    258  1.46  mycroft 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    259  1.46  mycroft 	if (ia->ia_drq != (u_short)-1)
    260  1.46  mycroft 		printf(" drq %d", ia->ia_drq);
    261  1.46  mycroft 	/* XXXX print flags */
    262  1.46  mycroft 	return QUIET;
    263  1.46  mycroft }
    264  1.46  mycroft 
    265  1.46  mycroft int
    266  1.46  mycroft isasubmatch(parent, self, aux)
    267  1.46  mycroft 	struct device *parent, *self;
    268  1.46  mycroft 	void *aux;
    269  1.46  mycroft {
    270  1.46  mycroft 	struct isa_device *id = (void *)self->dv_cfdata->cf_loc;
    271  1.46  mycroft 	struct isa_attach_args ia;
    272  1.46  mycroft 
    273  1.46  mycroft 	ia.ia_iobase = id->id_iobase;
    274  1.46  mycroft 	ia.ia_iosize = 0x666;
    275  1.46  mycroft 	ia.ia_irq = id->id_irq;
    276  1.46  mycroft 	ia.ia_drq = id->id_drq;
    277  1.46  mycroft 	ia.ia_maddr = id->id_maddr - 0xa0000 + atdevbase;
    278  1.46  mycroft 	ia.ia_msize = id->id_msize;
    279  1.46  mycroft 
    280  1.46  mycroft 	if (!(*id->id_driver->cd_match)(parent, self, &ia)) {
    281  1.46  mycroft 		/*
    282  1.46  mycroft 		 * If we don't do this, isa_configure() will repeatedly try to
    283  1.46  mycroft 		 * probe devices that weren't found.  But we need to be careful
    284  1.46  mycroft 		 * to do it only for the ISA bus, or we would cause things like
    285  1.46  mycroft 		 * `com0 at ast? slave ?' to not probe on the second ast.
    286  1.46  mycroft 		 */
    287  1.46  mycroft 		if (!parent)
    288  1.46  mycroft 			id->id_state = FSTATE_FOUND;
    289  1.46  mycroft 
    290  1.46  mycroft 		return 0;
    291  1.46  mycroft 	}
    292  1.46  mycroft 
    293  1.46  mycroft 	config_attach(parent, self->dv_cfdata, &ia, isaprint);
    294  1.46  mycroft 
    295  1.46  mycroft 	if (id->id_irq) {
    296  1.46  mycroft 		int intrno;
    297  1.46  mycroft 
    298  1.46  mycroft 		intrno = ffs(id->id_irq) - 1;
    299  1.46  mycroft 		setidt(ICU_OFFSET+intrno, id->id_intr, SDT_SYS386IGT, SEL_KPL);
    300  1.46  mycroft 		if (id->id_mask)
    301  1.46  mycroft 			INTRMASK(*id->id_mask, id->id_irq);
    302  1.46  mycroft 		INTREN(id->id_irq);
    303  1.46  mycroft 	}
    304  1.46  mycroft 
    305  1.46  mycroft 	return 1;
    306  1.46  mycroft }
    307  1.46  mycroft 
    308  1.21   andrew void
    309  1.30  mycroft isa_configure()
    310  1.30  mycroft {
    311   1.1      cgd 
    312  1.42  mycroft 	splhigh();
    313   1.1      cgd 	INTREN(IRQ_SLAVE);
    314  1.30  mycroft 	enable_intr();
    315  1.30  mycroft 
    316  1.46  mycroft 	while (config_search(isasubmatch, NULL, NULL));
    317   1.7      cgd 
    318  1.42  mycroft 	printf("biomask %x ttymask %x netmask %x\n",
    319  1.42  mycroft 	       biomask, ttymask, netmask);
    320  1.30  mycroft 
    321  1.42  mycroft 	clockmask |= astmask;
    322  1.42  mycroft 	biomask |= astmask;
    323  1.42  mycroft 	ttymask |= astmask;
    324  1.42  mycroft 	netmask |= astmask;
    325  1.42  mycroft 	impmask = netmask | ttymask;
    326  1.26  mycroft 
    327  1.30  mycroft 	spl0();
    328   1.1      cgd }
    329   1.1      cgd 
    330   1.1      cgd #define	IDTVEC(name)	__CONCAT(X,name)
    331   1.1      cgd /* default interrupt vector table entries */
    332   1.1      cgd extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    333   1.1      cgd 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    334   1.1      cgd 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    335   1.1      cgd 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    336   1.1      cgd 
    337   1.1      cgd static *defvec[16] = {
    338   1.1      cgd 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    339   1.1      cgd 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    340   1.1      cgd 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    341   1.1      cgd 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    342   1.1      cgd 
    343   1.1      cgd /* out of range default interrupt vector gate entry */
    344   1.1      cgd extern	IDTVEC(intrdefault);
    345  1.15      cgd 
    346   1.1      cgd /*
    347   1.1      cgd  * Fill in default interrupt table (in case of spuruious interrupt
    348   1.1      cgd  * during configuration of kernel, setup interrupt control unit
    349   1.1      cgd  */
    350  1.21   andrew void
    351   1.1      cgd isa_defaultirq() {
    352   1.1      cgd 	int i;
    353   1.1      cgd 
    354   1.1      cgd 	/* icu vectors */
    355   1.1      cgd 	for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++)
    356   1.1      cgd 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    357  1.15      cgd 
    358   1.1      cgd 	/* out of range vectors */
    359   1.1      cgd 	for (i = NRSVIDT; i < NIDT; i++)
    360   1.1      cgd 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    361   1.1      cgd 
    362   1.1      cgd 	/* initialize 8259's */
    363   1.1      cgd 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    364   1.1      cgd 	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
    365   1.1      cgd 	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
    366  1.15      cgd #ifdef AUTO_EOI_1
    367  1.15      cgd 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    368  1.15      cgd #else
    369   1.1      cgd 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    370  1.15      cgd #endif
    371   1.1      cgd 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    372  1.15      cgd 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    373  1.21   andrew #ifdef REORDER_IRQ
    374  1.15      cgd 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    375  1.21   andrew #endif
    376   1.1      cgd 
    377   1.1      cgd 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    378   1.1      cgd 	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
    379   1.1      cgd 	outb(IO_ICU2+1,2);		/* my slave id is 2 */
    380  1.15      cgd #ifdef AUTO_EOI_2
    381  1.15      cgd 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    382  1.15      cgd #else
    383   1.1      cgd 	outb(IO_ICU2+1,1);		/* 8086 mode */
    384  1.15      cgd #endif
    385   1.1      cgd 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    386  1.15      cgd 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    387   1.1      cgd }
    388   1.1      cgd 
    389   1.1      cgd /* region of physical memory known to be contiguous */
    390   1.1      cgd vm_offset_t isaphysmem;
    391   1.1      cgd static caddr_t dma_bounce[8];		/* XXX */
    392   1.1      cgd static char bounced[8];		/* XXX */
    393   1.1      cgd #define MAXDMASZ 512		/* XXX */
    394   1.1      cgd 
    395   1.1      cgd /* high byte of address is stored in this port for i-th dma channel */
    396   1.1      cgd static short dmapageport[8] =
    397   1.1      cgd 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    398   1.1      cgd 
    399   1.1      cgd /*
    400   1.1      cgd  * isa_dmacascade(): program 8237 DMA controller channel to accept
    401   1.1      cgd  * external dma control by a board.
    402   1.1      cgd  */
    403  1.21   andrew void
    404  1.37  mycroft isa_dmacascade(chan)
    405  1.37  mycroft 	int chan;
    406   1.2      cgd {
    407  1.37  mycroft 
    408  1.37  mycroft #ifdef DIAGNOSTIC
    409  1.37  mycroft 	if (chan < 0 || chan > 7)
    410   1.1      cgd 		panic("isa_dmacascade: impossible request");
    411  1.37  mycroft #endif
    412   1.1      cgd 
    413   1.1      cgd 	/* set dma channel mode, and set dma channel mode */
    414   1.2      cgd 	if ((chan & 4) == 0) {
    415   1.2      cgd 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    416   1.2      cgd 		outb(DMA1_SMSK, chan);
    417   1.2      cgd 	} else {
    418   1.2      cgd 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    419   1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    420   1.2      cgd 	}
    421   1.1      cgd }
    422   1.1      cgd 
    423   1.1      cgd /*
    424   1.1      cgd  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
    425   1.1      cgd  * problems by using a bounce buffer.
    426   1.1      cgd  */
    427  1.21   andrew void
    428  1.37  mycroft isa_dmastart(flags, addr, nbytes, chan)
    429  1.37  mycroft 	int flags;
    430  1.37  mycroft 	caddr_t addr;
    431  1.37  mycroft 	vm_size_t nbytes;
    432  1.37  mycroft 	int chan;
    433  1.37  mycroft {
    434  1.37  mycroft 	vm_offset_t phys;
    435   1.2      cgd 	int waport;
    436   1.1      cgd 	caddr_t newaddr;
    437   1.1      cgd 
    438  1.37  mycroft #ifdef DIAGNOSTIC
    439  1.37  mycroft 	if (chan < 0 || chan > 7 ||
    440  1.37  mycroft 	    ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
    441  1.37  mycroft 	    (nbytes >= (1<<16))))
    442   1.1      cgd 		panic("isa_dmastart: impossible request");
    443  1.37  mycroft #endif
    444   1.1      cgd 
    445   1.2      cgd 	if (isa_dmarangecheck(addr, nbytes, chan)) {
    446   1.1      cgd 		if (dma_bounce[chan] == 0)
    447   1.1      cgd 			dma_bounce[chan] =
    448  1.37  mycroft 			    /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    449  1.37  mycroft 			    (caddr_t) isaphysmem + NBPG*chan;
    450   1.1      cgd 		bounced[chan] = 1;
    451   1.1      cgd 		newaddr = dma_bounce[chan];
    452   1.1      cgd 		*(int *) newaddr = 0;	/* XXX */
    453   1.1      cgd 		/* copy bounce buffer on write */
    454  1.37  mycroft 		if ((flags & B_READ) == 0)
    455   1.1      cgd 			bcopy(addr, newaddr, nbytes);
    456   1.1      cgd 		addr = newaddr;
    457   1.1      cgd 	}
    458   1.1      cgd 
    459   1.1      cgd 	/* translate to physical */
    460   1.1      cgd 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    461   1.1      cgd 
    462   1.2      cgd 	if ((chan & 4) == 0) {
    463   1.2      cgd 		/*
    464   1.2      cgd 		 * Program one of DMA channels 0..3.  These are
    465   1.2      cgd 		 * byte mode channels.
    466   1.2      cgd 		 */
    467   1.2      cgd 		/* set dma channel mode, and reset address ff */
    468   1.2      cgd 		if (flags & B_READ)
    469  1.37  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_WRITE);
    470   1.2      cgd 		else
    471  1.37  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_READ);
    472   1.2      cgd 		outb(DMA1_FFC, 0);
    473   1.1      cgd 
    474   1.2      cgd 		/* send start address */
    475   1.2      cgd 		waport =  DMA1_CHN(chan);
    476   1.1      cgd 		outb(waport, phys);
    477   1.1      cgd 		outb(waport, phys>>8);
    478   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    479   1.2      cgd 
    480   1.2      cgd 		/* send count */
    481   1.2      cgd 		outb(waport + 1, --nbytes);
    482   1.2      cgd 		outb(waport + 1, nbytes>>8);
    483   1.2      cgd 
    484   1.2      cgd 		/* unmask channel */
    485  1.37  mycroft 		outb(DMA1_SMSK, chan | DMA37SM_CLEAR);
    486   1.1      cgd 	} else {
    487   1.2      cgd 		/*
    488   1.2      cgd 		 * Program one of DMA channels 4..7.  These are
    489   1.2      cgd 		 * word mode channels.
    490   1.2      cgd 		 */
    491   1.2      cgd 		/* set dma channel mode, and reset address ff */
    492   1.2      cgd 		if (flags & B_READ)
    493  1.37  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_WRITE);
    494   1.2      cgd 		else
    495  1.37  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_READ);
    496   1.2      cgd 		outb(DMA2_FFC, 0);
    497   1.2      cgd 
    498   1.2      cgd 		/* send start address */
    499  1.37  mycroft 		waport = DMA2_CHN(chan & 3);
    500   1.1      cgd 		outb(waport, phys>>1);
    501   1.1      cgd 		outb(waport, phys>>9);
    502   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    503   1.1      cgd 
    504   1.2      cgd 		/* send count */
    505   1.2      cgd 		nbytes >>= 1;
    506   1.1      cgd 		outb(waport + 2, --nbytes);
    507   1.1      cgd 		outb(waport + 2, nbytes>>8);
    508   1.2      cgd 
    509   1.2      cgd 		/* unmask channel */
    510  1.37  mycroft 		outb(DMA2_SMSK, (chan & 3) | DMA37SM_CLEAR);
    511   1.1      cgd 	}
    512  1.43  mycroft }
    513  1.43  mycroft 
    514  1.43  mycroft void
    515  1.43  mycroft isa_dmaabort(chan)
    516  1.43  mycroft 	int chan;
    517  1.43  mycroft {
    518  1.43  mycroft 
    519  1.43  mycroft #ifdef DIAGNOSTIC
    520  1.43  mycroft 	if (chan < 0 || chan > 7)
    521  1.43  mycroft 		panic("isa_dmadone: impossible request");
    522  1.43  mycroft #endif
    523  1.43  mycroft 
    524  1.43  mycroft 	bounced[chan] = 0;
    525  1.43  mycroft 
    526  1.43  mycroft 	/* mask channel */
    527  1.43  mycroft 	if ((chan & 4) == 0)
    528  1.43  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    529  1.43  mycroft 	else
    530  1.43  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    531   1.1      cgd }
    532   1.1      cgd 
    533  1.21   andrew void
    534  1.37  mycroft isa_dmadone(flags, addr, nbytes, chan)
    535  1.37  mycroft 	int flags;
    536  1.37  mycroft 	caddr_t addr;
    537  1.37  mycroft 	vm_size_t nbytes;
    538  1.37  mycroft 	int chan;
    539   1.1      cgd {
    540  1.37  mycroft 	u_char tc;
    541  1.37  mycroft 
    542  1.37  mycroft #ifdef DIAGNOSTIC
    543  1.37  mycroft 	if (chan < 0 || chan > 7)
    544  1.37  mycroft 		panic("isa_dmadone: impossible request");
    545  1.37  mycroft #endif
    546  1.37  mycroft 
    547  1.37  mycroft 	/* check that the terminal count was reached */
    548  1.37  mycroft 	if ((chan & 4) == 0)
    549  1.37  mycroft 		tc = inb(DMA1_SR) & (1 << chan);
    550  1.37  mycroft 	else
    551  1.37  mycroft 		tc = inb(DMA2_SR) & (1 << (chan & 3));
    552  1.37  mycroft 	if (tc == 0)
    553  1.37  mycroft 		/* XXX probably should panic or something */
    554  1.37  mycroft 		log(LOG_ERR, "dma channel %d not finished\n", chan);
    555   1.1      cgd 
    556   1.1      cgd 	/* copy bounce buffer on read */
    557   1.1      cgd 	if (bounced[chan]) {
    558   1.1      cgd 		bcopy(dma_bounce[chan], addr, nbytes);
    559   1.1      cgd 		bounced[chan] = 0;
    560   1.1      cgd 	}
    561  1.37  mycroft 
    562  1.37  mycroft 	/* mask channel */
    563  1.37  mycroft 	if ((chan & 4) == 0)
    564  1.37  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    565  1.37  mycroft 	else
    566  1.37  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    567   1.1      cgd }
    568   1.1      cgd 
    569   1.1      cgd /*
    570   1.1      cgd  * Check for problems with the address range of a DMA transfer
    571   1.2      cgd  * (non-contiguous physical pages, outside of bus address space,
    572   1.2      cgd  * crossing DMA page boundaries).
    573   1.1      cgd  * Return true if special handling needed.
    574   1.1      cgd  */
    575  1.21   andrew int
    576  1.37  mycroft isa_dmarangecheck(va, length, chan)
    577  1.37  mycroft 	vm_offset_t va;
    578  1.37  mycroft 	u_long length;
    579  1.37  mycroft 	int chan;
    580  1.37  mycroft {
    581   1.2      cgd 	vm_offset_t phys, priorpage = 0, endva;
    582   1.2      cgd 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    583   1.1      cgd 
    584  1.37  mycroft 	endva = round_page(va + length);
    585  1.37  mycroft 	for (; va < endva ; va += NBPG) {
    586  1.37  mycroft 		phys = trunc_page(pmap_extract(pmap_kernel(), va));
    587   1.1      cgd 		if (phys == 0)
    588   1.1      cgd 			panic("isa_dmacheck: no physical page present");
    589  1.37  mycroft 		if (phys >= (1<<24))
    590  1.37  mycroft 			return 1;
    591   1.2      cgd 		if (priorpage) {
    592   1.2      cgd 			if (priorpage + NBPG != phys)
    593  1.37  mycroft 				return 1;
    594   1.2      cgd 			/* check if crossing a DMA page boundary */
    595  1.37  mycroft 			if ((priorpage ^ phys) & dma_pgmsk)
    596  1.37  mycroft 				return 1;
    597   1.2      cgd 		}
    598   1.1      cgd 		priorpage = phys;
    599   1.1      cgd 	}
    600  1.37  mycroft 	return 0;
    601   1.1      cgd }
    602   1.1      cgd 
    603   1.1      cgd /* head of queue waiting for physmem to become available */
    604   1.1      cgd struct buf isa_physmemq;
    605   1.1      cgd 
    606   1.1      cgd /* blocked waiting for resource to become free for exclusive use */
    607   1.1      cgd static isaphysmemflag;
    608   1.1      cgd /* if waited for and call requested when free (B_CALL) */
    609   1.1      cgd static void (*isaphysmemunblock)(); /* needs to be a list */
    610   1.1      cgd 
    611   1.1      cgd /*
    612   1.1      cgd  * Allocate contiguous physical memory for transfer, returning
    613   1.1      cgd  * a *virtual* address to region. May block waiting for resource.
    614   1.1      cgd  * (assumed to be called at splbio())
    615   1.1      cgd  */
    616   1.1      cgd caddr_t
    617   1.1      cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    618   1.1      cgd 
    619   1.1      cgd 	isaphysmemunblock = func;
    620   1.1      cgd 	while (isaphysmemflag & B_BUSY) {
    621   1.1      cgd 		isaphysmemflag |= B_WANTED;
    622  1.24  deraadt 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    623   1.1      cgd 	}
    624   1.1      cgd 	isaphysmemflag |= B_BUSY;
    625   1.1      cgd 
    626   1.1      cgd 	return((caddr_t)isaphysmem);
    627   1.1      cgd }
    628   1.1      cgd 
    629   1.1      cgd /*
    630   1.1      cgd  * Free contiguous physical memory used for transfer.
    631   1.1      cgd  * (assumed to be called at splbio())
    632   1.1      cgd  */
    633   1.1      cgd void
    634   1.1      cgd isa_freephysmem(caddr_t va, unsigned length) {
    635   1.1      cgd 
    636   1.1      cgd 	isaphysmemflag &= ~B_BUSY;
    637   1.1      cgd 	if (isaphysmemflag & B_WANTED) {
    638   1.1      cgd 		isaphysmemflag &= B_WANTED;
    639  1.24  deraadt 		wakeup((caddr_t)&isaphysmemflag);
    640   1.1      cgd 		if (isaphysmemunblock)
    641   1.1      cgd 			(*isaphysmemunblock)();
    642   1.1      cgd 	}
    643   1.1      cgd }
    644   1.1      cgd 
    645   1.1      cgd /*
    646   1.1      cgd  * Handle a NMI, possibly a machine check.
    647   1.1      cgd  * return true to panic system, false to ignore.
    648   1.1      cgd  */
    649  1.21   andrew int
    650   1.1      cgd isa_nmi(cd) {
    651   1.1      cgd 
    652   1.1      cgd 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    653   1.1      cgd 	return(0);
    654   1.1      cgd }
    655   1.1      cgd 
    656   1.1      cgd /*
    657   1.1      cgd  * Caught a stray interrupt, notify
    658   1.1      cgd  */
    659  1.21   andrew void
    660   1.1      cgd isa_strayintr(d) {
    661   1.1      cgd 
    662   1.1      cgd 	/* DON'T BOTHER FOR NOW! */
    663   1.1      cgd 	/* for some reason, we get bursts of intr #7, even if not enabled! */
    664   1.4      cgd 	/*
    665   1.4      cgd 	 * Well the reason you got bursts of intr #7 is because someone
    666   1.4      cgd 	 * raised an interrupt line and dropped it before the 8259 could
    667   1.4      cgd 	 * prioritize it.  This is documented in the intel data book.  This
    668   1.4      cgd 	 * means you have BAD hardware!  I have changed this so that only
    669  1.15      cgd 	 * the first 5 get logged, then it quits logging them, and puts
    670   1.4      cgd 	 * out a special message. rgrimes 3/25/1993
    671   1.4      cgd 	 */
    672  1.15      cgd 	extern u_long intrcnt_stray;
    673   1.4      cgd 
    674  1.15      cgd 	intrcnt_stray++;
    675  1.15      cgd 	if (intrcnt_stray <= 5)
    676   1.4      cgd 		log(LOG_ERR,"ISA strayintr %x\n", d);
    677  1.15      cgd 	if (intrcnt_stray == 5)
    678   1.4      cgd 		log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
    679   1.1      cgd }
    680   1.1      cgd 
    681   1.1      cgd static beeping;
    682  1.21   andrew static void
    683  1.21   andrew sysbeepstop(int f)
    684   1.1      cgd {
    685  1.16  mycroft 	int s = splhigh();
    686  1.16  mycroft 
    687   1.1      cgd 	/* disable counter 2 */
    688  1.16  mycroft 	disable_intr();
    689  1.28   brezak 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    690  1.16  mycroft 	enable_intr();
    691   1.1      cgd 	if (f)
    692  1.24  deraadt 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    693   1.1      cgd 	else
    694   1.1      cgd 		beeping = 0;
    695  1.16  mycroft 
    696  1.16  mycroft 	splx(s);
    697   1.1      cgd }
    698   1.1      cgd 
    699  1.21   andrew void
    700  1.21   andrew sysbeep(int pitch, int period)
    701   1.1      cgd {
    702  1.16  mycroft 	int s = splhigh();
    703  1.16  mycroft 	static int last_pitch, last_period;
    704   1.1      cgd 
    705  1.16  mycroft 	if (beeping) {
    706  1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    707  1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    708   1.1      cgd 	}
    709  1.16  mycroft 	if (!beeping || last_pitch != pitch) {
    710  1.16  mycroft 		/*
    711  1.16  mycroft 	 	* XXX - move timer stuff to clock.c.
    712  1.16  mycroft 	 	*/
    713  1.16  mycroft 		disable_intr();
    714  1.16  mycroft 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    715  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    716  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    717  1.28   brezak 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    718  1.16  mycroft 		enable_intr();
    719  1.16  mycroft 	}
    720  1.16  mycroft 	last_pitch = pitch;
    721  1.16  mycroft 	beeping = last_period = period;
    722  1.24  deraadt 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    723  1.16  mycroft 
    724  1.16  mycroft 	splx(s);
    725  1.15      cgd }
    726  1.15      cgd 
    727  1.15      cgd /*
    728  1.15      cgd  * Return nonzero if a (masked) irq is pending for a given device.
    729  1.15      cgd  */
    730  1.15      cgd int
    731  1.15      cgd isa_irq_pending(dvp)
    732  1.15      cgd 	struct isa_device *dvp;
    733  1.15      cgd {
    734  1.15      cgd 	unsigned id_irq;
    735  1.15      cgd 
    736  1.15      cgd 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    737  1.15      cgd 	if (id_irq & 0xff)
    738  1.15      cgd 		return (inb(IO_ICU1) & id_irq);
    739  1.15      cgd 	return (inb(IO_ICU2) & (id_irq >> 8));
    740   1.1      cgd }
    741