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