Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.49
      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.49  mycroft  *	$Id: isa.c,v 1.49 1994/04/07 06:50:54 mycroft Exp $
     39   1.1      cgd  */
     40   1.1      cgd 
     41   1.1      cgd /*
     42  1.49  mycroft  * Code to manage AT bus
     43   1.1      cgd  */
     44   1.1      cgd 
     45  1.31  mycroft #include <sys/param.h>
     46  1.31  mycroft #include <sys/systm.h>
     47  1.40  mycroft #include <sys/kernel.h>
     48  1.31  mycroft #include <sys/conf.h>
     49  1.31  mycroft #include <sys/file.h>
     50  1.31  mycroft #include <sys/buf.h>
     51  1.31  mycroft #include <sys/uio.h>
     52  1.31  mycroft #include <sys/syslog.h>
     53  1.31  mycroft #include <sys/malloc.h>
     54  1.46  mycroft #include <sys/device.h>
     55  1.31  mycroft 
     56  1.31  mycroft #include <vm/vm.h>
     57  1.31  mycroft 
     58  1.32  mycroft #include <machine/pio.h>
     59  1.31  mycroft #include <machine/cpufunc.h>
     60  1.31  mycroft 
     61  1.46  mycroft #include <i386/isa/isa.h>
     62  1.31  mycroft #include <i386/isa/isa_device.h>
     63  1.46  mycroft #include <i386/isa/isavar.h>
     64  1.31  mycroft #include <i386/isa/ic/i8237.h>
     65  1.31  mycroft #include <i386/isa/ic/i8042.h>
     66  1.31  mycroft #include <i386/isa/timerreg.h>
     67  1.31  mycroft #include <i386/isa/spkr_reg.h>
     68  1.14  deraadt 
     69  1.14  deraadt /* sorry, has to be here, no place else really suitable */
     70  1.31  mycroft #include <machine/pc/display.h>
     71  1.14  deraadt u_short *Crtat = (u_short *)MONO_BUF;
     72   1.1      cgd 
     73   1.2      cgd /*
     74  1.38  mycroft  * Register definitions for DMA controller 1 (channels 0..3):
     75  1.38  mycroft  */
     76   1.2      cgd #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     77  1.37  mycroft #define	DMA1_SR		(IO_DMA1 + 1*8)		/* status register */
     78   1.2      cgd #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     79   1.2      cgd #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     80   1.2      cgd #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     81   1.2      cgd 
     82   1.2      cgd /*
     83  1.38  mycroft  * Register definitions for DMA controller 2 (channels 4..7):
     84  1.38  mycroft  */
     85  1.30  mycroft #define	DMA2_CHN(c)	(IO_DMA2 + 2*(2*(c)))	/* addr reg for channel c */
     86  1.37  mycroft #define	DMA2_SR		(IO_DMA2 + 2*8)		/* status register */
     87   1.2      cgd #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     88   1.2      cgd #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     89   1.2      cgd #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     90   1.2      cgd 
     91  1.21   andrew static void sysbeepstop(int);
     92   1.1      cgd 
     93   1.1      cgd /*
     94   1.1      cgd  * Configure all ISA devices
     95  1.46  mycroft  *
     96  1.46  mycroft  * XXX This code is a hack.  It wants to be new config, but can't be until the
     97  1.46  mycroft  * interrupt system is redone.  For now, we do some gross hacks to make it look
     98  1.46  mycroft  * 99% like new config.
     99  1.46  mycroft  */
    100  1.46  mycroft static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
    101  1.46  mycroft 
    102  1.46  mycroft struct cfdata *
    103  1.46  mycroft config_search(fn, parent, aux)
    104  1.46  mycroft 	cfmatch_t fn;
    105  1.46  mycroft 	struct device *parent;
    106  1.46  mycroft 	void *aux;
    107  1.46  mycroft {
    108  1.46  mycroft 	struct cfdata *cf = 0;
    109  1.46  mycroft 	struct device *dv = 0;
    110  1.46  mycroft 	size_t devsize;
    111  1.46  mycroft 	struct cfdriver *cd;
    112  1.46  mycroft 	struct isa_device *id,
    113  1.46  mycroft 	    *idp =  parent ? (void *)parent->dv_cfdata->cf_loc : 0;
    114  1.46  mycroft 
    115  1.46  mycroft 	for (id = isa_devtab; id->id_driver; id++) {
    116  1.46  mycroft 		if (id->id_state == FSTATE_FOUND)
    117  1.46  mycroft 			continue;
    118  1.46  mycroft 		if (id->id_parent != idp)
    119  1.46  mycroft 			continue;
    120  1.46  mycroft 		cd = id->id_driver;
    121  1.46  mycroft 		if (id->id_unit < cd->cd_ndevs) {
    122  1.46  mycroft 			if (cd->cd_devs[id->id_unit] != 0)
    123  1.46  mycroft 				continue;
    124  1.46  mycroft 		} else {
    125  1.46  mycroft 			int old = cd->cd_ndevs, new;
    126  1.46  mycroft 			void **nsp;
    127  1.46  mycroft 
    128  1.46  mycroft 			if (old == 0) {
    129  1.46  mycroft 				nsp = malloc(MINALLOCSIZE, M_DEVBUF, M_NOWAIT);
    130  1.46  mycroft 				if (!nsp)
    131  1.46  mycroft 					panic("config_search: creating dev array");
    132  1.46  mycroft 				bzero(nsp, MINALLOCSIZE);
    133  1.46  mycroft 				cd->cd_ndevs = MINALLOCSIZE / sizeof(void *);
    134  1.46  mycroft 			} else {
    135  1.46  mycroft 				new = old;
    136  1.46  mycroft 				do {
    137  1.46  mycroft 					new *= 2;
    138  1.46  mycroft 				} while (new <= id->id_unit);
    139  1.46  mycroft 				cd->cd_ndevs = new;
    140  1.46  mycroft 				nsp = malloc(new * sizeof(void *), M_DEVBUF,
    141  1.46  mycroft 				    M_NOWAIT);
    142  1.46  mycroft 				if (!nsp)
    143  1.46  mycroft 					panic("config_search: expanding dev array");
    144  1.46  mycroft 				bzero(nsp, new * sizeof(void *));
    145  1.46  mycroft 				bcopy(cd->cd_devs, nsp, old * sizeof(void *));
    146  1.46  mycroft 				free(cd->cd_devs, M_DEVBUF);
    147  1.46  mycroft 			}
    148  1.46  mycroft 			cd->cd_devs = nsp;
    149  1.46  mycroft 		}
    150  1.46  mycroft 		if (!cf) {
    151  1.46  mycroft 			cf = malloc(sizeof(struct cfdata), M_DEVBUF, M_NOWAIT);				if (!cf)
    152  1.46  mycroft 				panic("config_search: creating cfdata");
    153  1.46  mycroft 		}
    154  1.46  mycroft 		cf->cf_driver = cd;
    155  1.46  mycroft 		cf->cf_unit = id->id_unit;
    156  1.46  mycroft 		cf->cf_fstate = 0;
    157  1.46  mycroft 		cf->cf_loc = (void *)id;
    158  1.46  mycroft 		cf->cf_flags = id->id_flags;
    159  1.46  mycroft 		cf->cf_parents = 0;
    160  1.46  mycroft 		cf->cf_ivstubs = 0;
    161  1.46  mycroft 		if (dv && devsize != cd->cd_devsize) {
    162  1.46  mycroft 			free(dv, M_DEVBUF);
    163  1.46  mycroft 			dv = 0;
    164  1.46  mycroft 		}
    165  1.46  mycroft 		if (!dv) {
    166  1.46  mycroft 			devsize = cd->cd_devsize;
    167  1.46  mycroft 			dv = malloc(devsize, M_DEVBUF, M_NOWAIT);
    168  1.46  mycroft 			if (!dv)
    169  1.46  mycroft 				panic("config_search: creating softc");
    170  1.46  mycroft 		}
    171  1.46  mycroft 		bzero(dv, cd->cd_devsize);
    172  1.46  mycroft 		dv->dv_class = cd->cd_class;
    173  1.46  mycroft 		dv->dv_cfdata = cf;
    174  1.46  mycroft 		dv->dv_unit = id->id_unit;
    175  1.46  mycroft 		sprintf(dv->dv_xname, "%s%d", cd->cd_name, id->id_unit);
    176  1.46  mycroft 		dv->dv_parent = parent;
    177  1.46  mycroft 		cd->cd_devs[id->id_unit] = dv;
    178  1.46  mycroft 		if (fn) {
    179  1.46  mycroft 			if ((*fn)(parent, dv, aux))
    180  1.46  mycroft 				return cf;
    181  1.46  mycroft 		} else {
    182  1.46  mycroft 			if ((*cd->cd_match)(parent, dv, aux))
    183  1.46  mycroft 				return cf;
    184  1.46  mycroft 		}
    185  1.47  mycroft 		cd->cd_devs[id->id_unit] = 0;
    186  1.46  mycroft 	}
    187  1.46  mycroft 	if (cf)
    188  1.46  mycroft 		free(cf, M_DEVBUF);
    189  1.46  mycroft 	if (dv)
    190  1.46  mycroft 		free(dv, M_DEVBUF);
    191  1.46  mycroft 	return 0;
    192  1.46  mycroft }
    193  1.46  mycroft 
    194  1.46  mycroft void
    195  1.46  mycroft config_attach(parent, cf, aux, print)
    196  1.46  mycroft 	struct device *parent;
    197  1.46  mycroft 	struct cfdata *cf;
    198  1.46  mycroft 	void *aux;
    199  1.46  mycroft 	cfprint_t print;
    200  1.46  mycroft {
    201  1.46  mycroft 	struct isa_device *id = (void *)cf->cf_loc;
    202  1.46  mycroft 	struct cfdriver *cd = cf->cf_driver;
    203  1.46  mycroft 	struct device *dv = cd->cd_devs[id->id_unit];
    204  1.46  mycroft 
    205  1.46  mycroft 	cf->cf_fstate = id->id_state = FSTATE_FOUND;
    206  1.46  mycroft 	printf("%s at %s", dv->dv_xname, parent ? parent->dv_xname : "isa0");
    207  1.46  mycroft 	if (print)
    208  1.46  mycroft 		(void) (*print)(aux, (char *)0);
    209  1.46  mycroft 	(*cd->cd_attach)(parent, dv, aux);
    210  1.46  mycroft }
    211  1.46  mycroft 
    212  1.46  mycroft int
    213  1.46  mycroft config_found(parent, aux, print)
    214  1.46  mycroft 	struct device *parent;
    215  1.46  mycroft 	void *aux;
    216  1.46  mycroft 	cfprint_t print;
    217  1.46  mycroft {
    218  1.46  mycroft 	struct cfdata *cf;
    219  1.46  mycroft 
    220  1.46  mycroft 	if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
    221  1.46  mycroft 		config_attach(parent, cf, aux, print);
    222  1.46  mycroft 		return 1;
    223  1.46  mycroft 	}
    224  1.46  mycroft 	if (print)
    225  1.46  mycroft 		printf(msgs[(*print)(aux, parent->dv_xname)]);
    226  1.46  mycroft 	return 0;
    227  1.46  mycroft }
    228  1.46  mycroft 
    229  1.46  mycroft int
    230  1.46  mycroft isaprint(aux, isa)
    231  1.46  mycroft 	void *aux;
    232  1.46  mycroft 	char *isa;
    233  1.46  mycroft {
    234  1.46  mycroft 	struct isa_attach_args *ia = aux;
    235  1.46  mycroft 
    236  1.46  mycroft 	if (ia->ia_iosize)
    237  1.46  mycroft 		printf(" port 0x%x", ia->ia_iobase);
    238  1.46  mycroft 	if (ia->ia_iosize > 1)
    239  1.46  mycroft 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    240  1.46  mycroft 	if (ia->ia_msize)
    241  1.46  mycroft 		printf(" iomem 0x%x", ia->ia_maddr - atdevbase + 0xa0000);
    242  1.46  mycroft 	if (ia->ia_msize > 1)
    243  1.46  mycroft 		printf("-0x%x",
    244  1.46  mycroft 		    ia->ia_maddr - atdevbase + 0xa0000 + ia->ia_msize - 1);
    245  1.46  mycroft 	if (ia->ia_irq)
    246  1.46  mycroft 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    247  1.46  mycroft 	if (ia->ia_drq != (u_short)-1)
    248  1.46  mycroft 		printf(" drq %d", ia->ia_drq);
    249  1.46  mycroft 	/* XXXX print flags */
    250  1.46  mycroft 	return QUIET;
    251  1.46  mycroft }
    252  1.46  mycroft 
    253  1.46  mycroft int
    254  1.46  mycroft isasubmatch(parent, self, aux)
    255  1.46  mycroft 	struct device *parent, *self;
    256  1.46  mycroft 	void *aux;
    257  1.46  mycroft {
    258  1.46  mycroft 	struct isa_device *id = (void *)self->dv_cfdata->cf_loc;
    259  1.46  mycroft 	struct isa_attach_args ia;
    260  1.46  mycroft 
    261  1.46  mycroft 	ia.ia_iobase = id->id_iobase;
    262  1.46  mycroft 	ia.ia_iosize = 0x666;
    263  1.46  mycroft 	ia.ia_irq = id->id_irq;
    264  1.46  mycroft 	ia.ia_drq = id->id_drq;
    265  1.46  mycroft 	ia.ia_maddr = id->id_maddr - 0xa0000 + atdevbase;
    266  1.46  mycroft 	ia.ia_msize = id->id_msize;
    267  1.46  mycroft 
    268  1.46  mycroft 	if (!(*id->id_driver->cd_match)(parent, self, &ia)) {
    269  1.46  mycroft 		/*
    270  1.46  mycroft 		 * If we don't do this, isa_configure() will repeatedly try to
    271  1.46  mycroft 		 * probe devices that weren't found.  But we need to be careful
    272  1.46  mycroft 		 * to do it only for the ISA bus, or we would cause things like
    273  1.46  mycroft 		 * `com0 at ast? slave ?' to not probe on the second ast.
    274  1.46  mycroft 		 */
    275  1.46  mycroft 		if (!parent)
    276  1.46  mycroft 			id->id_state = FSTATE_FOUND;
    277  1.46  mycroft 
    278  1.46  mycroft 		return 0;
    279  1.46  mycroft 	}
    280  1.46  mycroft 
    281  1.46  mycroft 	config_attach(parent, self->dv_cfdata, &ia, isaprint);
    282  1.46  mycroft 
    283  1.46  mycroft 	return 1;
    284  1.46  mycroft }
    285  1.46  mycroft 
    286  1.21   andrew void
    287  1.30  mycroft isa_configure()
    288  1.30  mycroft {
    289   1.1      cgd 
    290  1.46  mycroft 	while (config_search(isasubmatch, NULL, NULL));
    291   1.7      cgd 
    292  1.49  mycroft 	printf("biomask %x netmask %x ttymask %x\n",
    293  1.49  mycroft 	    (u_short)imask[IPL_BIO], (u_short)imask[IPL_NET],
    294  1.49  mycroft 	    (u_short)imask[IPL_TTY]);
    295  1.26  mycroft 
    296  1.30  mycroft 	spl0();
    297   1.1      cgd }
    298   1.1      cgd 
    299   1.1      cgd /* region of physical memory known to be contiguous */
    300   1.1      cgd vm_offset_t isaphysmem;
    301   1.1      cgd static caddr_t dma_bounce[8];		/* XXX */
    302   1.1      cgd static char bounced[8];		/* XXX */
    303   1.1      cgd #define MAXDMASZ 512		/* XXX */
    304   1.1      cgd 
    305   1.1      cgd /* high byte of address is stored in this port for i-th dma channel */
    306   1.1      cgd static short dmapageport[8] =
    307   1.1      cgd 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    308   1.1      cgd 
    309   1.1      cgd /*
    310   1.1      cgd  * isa_dmacascade(): program 8237 DMA controller channel to accept
    311   1.1      cgd  * external dma control by a board.
    312   1.1      cgd  */
    313  1.21   andrew void
    314  1.37  mycroft isa_dmacascade(chan)
    315  1.37  mycroft 	int chan;
    316   1.2      cgd {
    317  1.37  mycroft 
    318  1.37  mycroft #ifdef DIAGNOSTIC
    319  1.37  mycroft 	if (chan < 0 || chan > 7)
    320   1.1      cgd 		panic("isa_dmacascade: impossible request");
    321  1.37  mycroft #endif
    322   1.1      cgd 
    323   1.1      cgd 	/* set dma channel mode, and set dma channel mode */
    324   1.2      cgd 	if ((chan & 4) == 0) {
    325   1.2      cgd 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    326   1.2      cgd 		outb(DMA1_SMSK, chan);
    327   1.2      cgd 	} else {
    328   1.2      cgd 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    329   1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    330   1.2      cgd 	}
    331   1.1      cgd }
    332   1.1      cgd 
    333   1.1      cgd /*
    334   1.1      cgd  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
    335   1.1      cgd  * problems by using a bounce buffer.
    336   1.1      cgd  */
    337  1.21   andrew void
    338  1.37  mycroft isa_dmastart(flags, addr, nbytes, chan)
    339  1.37  mycroft 	int flags;
    340  1.37  mycroft 	caddr_t addr;
    341  1.37  mycroft 	vm_size_t nbytes;
    342  1.37  mycroft 	int chan;
    343  1.37  mycroft {
    344  1.37  mycroft 	vm_offset_t phys;
    345   1.2      cgd 	int waport;
    346   1.1      cgd 	caddr_t newaddr;
    347   1.1      cgd 
    348  1.37  mycroft #ifdef DIAGNOSTIC
    349  1.37  mycroft 	if (chan < 0 || chan > 7 ||
    350  1.37  mycroft 	    ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
    351  1.37  mycroft 	    (nbytes >= (1<<16))))
    352   1.1      cgd 		panic("isa_dmastart: impossible request");
    353  1.37  mycroft #endif
    354   1.1      cgd 
    355   1.2      cgd 	if (isa_dmarangecheck(addr, nbytes, chan)) {
    356   1.1      cgd 		if (dma_bounce[chan] == 0)
    357   1.1      cgd 			dma_bounce[chan] =
    358  1.37  mycroft 			    /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    359  1.37  mycroft 			    (caddr_t) isaphysmem + NBPG*chan;
    360   1.1      cgd 		bounced[chan] = 1;
    361   1.1      cgd 		newaddr = dma_bounce[chan];
    362   1.1      cgd 		*(int *) newaddr = 0;	/* XXX */
    363   1.1      cgd 		/* copy bounce buffer on write */
    364  1.37  mycroft 		if ((flags & B_READ) == 0)
    365   1.1      cgd 			bcopy(addr, newaddr, nbytes);
    366   1.1      cgd 		addr = newaddr;
    367   1.1      cgd 	}
    368   1.1      cgd 
    369   1.1      cgd 	/* translate to physical */
    370   1.1      cgd 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    371   1.1      cgd 
    372   1.2      cgd 	if ((chan & 4) == 0) {
    373   1.2      cgd 		/*
    374   1.2      cgd 		 * Program one of DMA channels 0..3.  These are
    375   1.2      cgd 		 * byte mode channels.
    376   1.2      cgd 		 */
    377   1.2      cgd 		/* set dma channel mode, and reset address ff */
    378   1.2      cgd 		if (flags & B_READ)
    379  1.37  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_WRITE);
    380   1.2      cgd 		else
    381  1.37  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_READ);
    382   1.2      cgd 		outb(DMA1_FFC, 0);
    383   1.1      cgd 
    384   1.2      cgd 		/* send start address */
    385   1.2      cgd 		waport =  DMA1_CHN(chan);
    386   1.1      cgd 		outb(waport, phys);
    387   1.1      cgd 		outb(waport, phys>>8);
    388   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    389   1.2      cgd 
    390   1.2      cgd 		/* send count */
    391   1.2      cgd 		outb(waport + 1, --nbytes);
    392   1.2      cgd 		outb(waport + 1, nbytes>>8);
    393   1.2      cgd 
    394   1.2      cgd 		/* unmask channel */
    395  1.37  mycroft 		outb(DMA1_SMSK, chan | DMA37SM_CLEAR);
    396   1.1      cgd 	} else {
    397   1.2      cgd 		/*
    398   1.2      cgd 		 * Program one of DMA channels 4..7.  These are
    399   1.2      cgd 		 * word mode channels.
    400   1.2      cgd 		 */
    401   1.2      cgd 		/* set dma channel mode, and reset address ff */
    402   1.2      cgd 		if (flags & B_READ)
    403  1.37  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_WRITE);
    404   1.2      cgd 		else
    405  1.37  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_READ);
    406   1.2      cgd 		outb(DMA2_FFC, 0);
    407   1.2      cgd 
    408   1.2      cgd 		/* send start address */
    409  1.37  mycroft 		waport = DMA2_CHN(chan & 3);
    410   1.1      cgd 		outb(waport, phys>>1);
    411   1.1      cgd 		outb(waport, phys>>9);
    412   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    413   1.1      cgd 
    414   1.2      cgd 		/* send count */
    415   1.2      cgd 		nbytes >>= 1;
    416   1.1      cgd 		outb(waport + 2, --nbytes);
    417   1.1      cgd 		outb(waport + 2, nbytes>>8);
    418   1.2      cgd 
    419   1.2      cgd 		/* unmask channel */
    420  1.37  mycroft 		outb(DMA2_SMSK, (chan & 3) | DMA37SM_CLEAR);
    421   1.1      cgd 	}
    422  1.43  mycroft }
    423  1.43  mycroft 
    424  1.43  mycroft void
    425  1.43  mycroft isa_dmaabort(chan)
    426  1.43  mycroft 	int chan;
    427  1.43  mycroft {
    428  1.43  mycroft 
    429  1.43  mycroft #ifdef DIAGNOSTIC
    430  1.43  mycroft 	if (chan < 0 || chan > 7)
    431  1.43  mycroft 		panic("isa_dmadone: impossible request");
    432  1.43  mycroft #endif
    433  1.43  mycroft 
    434  1.43  mycroft 	bounced[chan] = 0;
    435  1.43  mycroft 
    436  1.43  mycroft 	/* mask channel */
    437  1.43  mycroft 	if ((chan & 4) == 0)
    438  1.43  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    439  1.43  mycroft 	else
    440  1.43  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    441   1.1      cgd }
    442   1.1      cgd 
    443  1.21   andrew void
    444  1.37  mycroft isa_dmadone(flags, addr, nbytes, chan)
    445  1.37  mycroft 	int flags;
    446  1.37  mycroft 	caddr_t addr;
    447  1.37  mycroft 	vm_size_t nbytes;
    448  1.37  mycroft 	int chan;
    449   1.1      cgd {
    450  1.37  mycroft 	u_char tc;
    451  1.37  mycroft 
    452  1.37  mycroft #ifdef DIAGNOSTIC
    453  1.37  mycroft 	if (chan < 0 || chan > 7)
    454  1.37  mycroft 		panic("isa_dmadone: impossible request");
    455  1.37  mycroft #endif
    456  1.37  mycroft 
    457  1.37  mycroft 	/* check that the terminal count was reached */
    458  1.37  mycroft 	if ((chan & 4) == 0)
    459  1.37  mycroft 		tc = inb(DMA1_SR) & (1 << chan);
    460  1.37  mycroft 	else
    461  1.37  mycroft 		tc = inb(DMA2_SR) & (1 << (chan & 3));
    462  1.37  mycroft 	if (tc == 0)
    463  1.37  mycroft 		/* XXX probably should panic or something */
    464  1.37  mycroft 		log(LOG_ERR, "dma channel %d not finished\n", chan);
    465   1.1      cgd 
    466   1.1      cgd 	/* copy bounce buffer on read */
    467   1.1      cgd 	if (bounced[chan]) {
    468   1.1      cgd 		bcopy(dma_bounce[chan], addr, nbytes);
    469   1.1      cgd 		bounced[chan] = 0;
    470   1.1      cgd 	}
    471  1.37  mycroft 
    472  1.37  mycroft 	/* mask channel */
    473  1.37  mycroft 	if ((chan & 4) == 0)
    474  1.37  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    475  1.37  mycroft 	else
    476  1.37  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    477   1.1      cgd }
    478   1.1      cgd 
    479   1.1      cgd /*
    480   1.1      cgd  * Check for problems with the address range of a DMA transfer
    481   1.2      cgd  * (non-contiguous physical pages, outside of bus address space,
    482   1.2      cgd  * crossing DMA page boundaries).
    483   1.1      cgd  * Return true if special handling needed.
    484   1.1      cgd  */
    485  1.21   andrew int
    486  1.37  mycroft isa_dmarangecheck(va, length, chan)
    487  1.37  mycroft 	vm_offset_t va;
    488  1.37  mycroft 	u_long length;
    489  1.37  mycroft 	int chan;
    490  1.37  mycroft {
    491   1.2      cgd 	vm_offset_t phys, priorpage = 0, endva;
    492   1.2      cgd 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    493   1.1      cgd 
    494  1.37  mycroft 	endva = round_page(va + length);
    495  1.37  mycroft 	for (; va < endva ; va += NBPG) {
    496  1.37  mycroft 		phys = trunc_page(pmap_extract(pmap_kernel(), va));
    497   1.1      cgd 		if (phys == 0)
    498   1.1      cgd 			panic("isa_dmacheck: no physical page present");
    499  1.37  mycroft 		if (phys >= (1<<24))
    500  1.37  mycroft 			return 1;
    501   1.2      cgd 		if (priorpage) {
    502   1.2      cgd 			if (priorpage + NBPG != phys)
    503  1.37  mycroft 				return 1;
    504   1.2      cgd 			/* check if crossing a DMA page boundary */
    505  1.37  mycroft 			if ((priorpage ^ phys) & dma_pgmsk)
    506  1.37  mycroft 				return 1;
    507   1.2      cgd 		}
    508   1.1      cgd 		priorpage = phys;
    509   1.1      cgd 	}
    510  1.37  mycroft 	return 0;
    511   1.1      cgd }
    512   1.1      cgd 
    513   1.1      cgd /* head of queue waiting for physmem to become available */
    514   1.1      cgd struct buf isa_physmemq;
    515   1.1      cgd 
    516   1.1      cgd /* blocked waiting for resource to become free for exclusive use */
    517   1.1      cgd static isaphysmemflag;
    518   1.1      cgd /* if waited for and call requested when free (B_CALL) */
    519   1.1      cgd static void (*isaphysmemunblock)(); /* needs to be a list */
    520   1.1      cgd 
    521   1.1      cgd /*
    522   1.1      cgd  * Allocate contiguous physical memory for transfer, returning
    523   1.1      cgd  * a *virtual* address to region. May block waiting for resource.
    524   1.1      cgd  * (assumed to be called at splbio())
    525   1.1      cgd  */
    526   1.1      cgd caddr_t
    527   1.1      cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    528   1.1      cgd 
    529   1.1      cgd 	isaphysmemunblock = func;
    530   1.1      cgd 	while (isaphysmemflag & B_BUSY) {
    531   1.1      cgd 		isaphysmemflag |= B_WANTED;
    532  1.24  deraadt 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    533   1.1      cgd 	}
    534   1.1      cgd 	isaphysmemflag |= B_BUSY;
    535   1.1      cgd 
    536   1.1      cgd 	return((caddr_t)isaphysmem);
    537   1.1      cgd }
    538   1.1      cgd 
    539   1.1      cgd /*
    540   1.1      cgd  * Free contiguous physical memory used for transfer.
    541   1.1      cgd  * (assumed to be called at splbio())
    542   1.1      cgd  */
    543   1.1      cgd void
    544   1.1      cgd isa_freephysmem(caddr_t va, unsigned length) {
    545   1.1      cgd 
    546   1.1      cgd 	isaphysmemflag &= ~B_BUSY;
    547   1.1      cgd 	if (isaphysmemflag & B_WANTED) {
    548   1.1      cgd 		isaphysmemflag &= B_WANTED;
    549  1.24  deraadt 		wakeup((caddr_t)&isaphysmemflag);
    550   1.1      cgd 		if (isaphysmemunblock)
    551   1.1      cgd 			(*isaphysmemunblock)();
    552   1.1      cgd 	}
    553   1.1      cgd }
    554   1.1      cgd 
    555  1.49  mycroft static int beeping;
    556   1.1      cgd 
    557  1.21   andrew static void
    558  1.21   andrew sysbeepstop(int f)
    559   1.1      cgd {
    560  1.16  mycroft 	int s = splhigh();
    561  1.16  mycroft 
    562   1.1      cgd 	/* disable counter 2 */
    563  1.16  mycroft 	disable_intr();
    564  1.28   brezak 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    565  1.16  mycroft 	enable_intr();
    566   1.1      cgd 	if (f)
    567  1.24  deraadt 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    568   1.1      cgd 	else
    569   1.1      cgd 		beeping = 0;
    570  1.16  mycroft 
    571  1.16  mycroft 	splx(s);
    572   1.1      cgd }
    573   1.1      cgd 
    574  1.21   andrew void
    575  1.21   andrew sysbeep(int pitch, int period)
    576   1.1      cgd {
    577  1.16  mycroft 	int s = splhigh();
    578  1.16  mycroft 	static int last_pitch, last_period;
    579   1.1      cgd 
    580  1.16  mycroft 	if (beeping) {
    581  1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    582  1.24  deraadt 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    583   1.1      cgd 	}
    584  1.16  mycroft 	if (!beeping || last_pitch != pitch) {
    585  1.16  mycroft 		/*
    586  1.16  mycroft 	 	* XXX - move timer stuff to clock.c.
    587  1.16  mycroft 	 	*/
    588  1.16  mycroft 		disable_intr();
    589  1.16  mycroft 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    590  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    591  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    592  1.28   brezak 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    593  1.16  mycroft 		enable_intr();
    594  1.16  mycroft 	}
    595  1.16  mycroft 	last_pitch = pitch;
    596  1.16  mycroft 	beeping = last_period = period;
    597  1.24  deraadt 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    598  1.16  mycroft 
    599  1.16  mycroft 	splx(s);
    600   1.1      cgd }
    601