Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.21
      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.21   andrew  *	$Id: isa.c,v 1.21 1993/06/27 06:27:29 andrew 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 "rlist.h"
     59   1.1      cgd #include "machine/segments.h"
     60  1.21   andrew #include "machine/cpufunc.h"
     61   1.1      cgd #include "vm/vm.h"
     62   1.1      cgd #include "i386/isa/isa_device.h"
     63   1.1      cgd #include "i386/isa/isa.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.14  deraadt 
     69  1.14  deraadt /* sorry, has to be here, no place else really suitable */
     70  1.14  deraadt #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.2      cgd **  Register definitions for DMA controller 1 (channels 0..3):
     75   1.2      cgd */
     76   1.2      cgd #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     77   1.2      cgd #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     78   1.2      cgd #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     79   1.2      cgd #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     80   1.2      cgd 
     81   1.2      cgd /*
     82   1.2      cgd **  Register definitions for DMA controller 2 (channels 4..7):
     83   1.2      cgd */
     84   1.2      cgd #define	DMA2_CHN(c)	(IO_DMA1 + 2*(2*(c)))	/* addr reg for channel c */
     85   1.2      cgd #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     86   1.2      cgd #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     87   1.2      cgd #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     88   1.2      cgd 
     89  1.15      cgd int config_isadev(struct isa_device *, u_int *);
     90   1.6  deraadt void config_attach(struct isa_driver *, struct isa_device *);
     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.1      cgd  */
     96  1.21   andrew void
     97   1.1      cgd isa_configure() {
     98   1.1      cgd 	struct isa_device *dvp;
     99   1.1      cgd 	struct isa_driver *dp;
    100   1.1      cgd 
    101  1.15      cgd 	enable_intr();
    102   1.1      cgd 	splhigh();
    103   1.1      cgd 	INTREN(IRQ_SLAVE);
    104   1.6  deraadt 	for (dvp = isa_devtab_tty; config_isadev(dvp,&ttymask); dvp++)
    105   1.6  deraadt 		;
    106   1.6  deraadt 	for (dvp = isa_devtab_bio; config_isadev(dvp,&biomask); dvp++)
    107   1.6  deraadt 		;
    108   1.6  deraadt 	for (dvp = isa_devtab_net; config_isadev(dvp,&netmask); dvp++)
    109   1.6  deraadt 		;
    110  1.15      cgd 	for (dvp = isa_devtab_null; config_isadev(dvp, (u_int *) NULL); dvp++)
    111   1.6  deraadt 		;
    112   1.1      cgd #include "sl.h"
    113   1.1      cgd #if NSL > 0
    114   1.1      cgd 	netmask |= ttymask;
    115   1.1      cgd 	ttymask |= netmask;
    116   1.1      cgd #endif
    117   1.7      cgd 
    118   1.7      cgd 	/* and the problem is... if netmask == 0, then the loopback
    119   1.7      cgd 	 * code can do some really ugly things.
    120   1.7      cgd 	 * workaround for this: if netmask == 0, set it to 0x8000, which
    121   1.7      cgd 	 * is the value used by splsoftclock.  this is nasty, but it
    122   1.7      cgd 	 * should work until this interrupt system goes away. -- cgd
    123   1.7      cgd 	 */
    124   1.7      cgd 	if (netmask == 0)
    125   1.7      cgd 		netmask = 0x8000;	/* same as for softclock.  XXX */
    126   1.7      cgd 
    127   1.1      cgd 	/* biomask |= ttymask ;  can some tty devices use buffers? */
    128  1.15      cgd 	printf("biomask %x ttymask %x netmask %x\n", biomask, ttymask, netmask);
    129   1.1      cgd 	splnone();
    130   1.1      cgd }
    131   1.1      cgd 
    132   1.1      cgd /*
    133   1.1      cgd  * Configure an ISA device.
    134   1.1      cgd  */
    135  1.21   andrew int
    136   1.1      cgd config_isadev(isdp, mp)
    137   1.1      cgd 	struct isa_device *isdp;
    138  1.15      cgd 	u_int *mp;
    139   1.1      cgd {
    140   1.1      cgd 	struct isa_driver *dp;
    141   1.1      cgd 
    142   1.1      cgd 	if (dp = isdp->id_driver) {
    143   1.1      cgd 		if (isdp->id_maddr) {
    144   1.1      cgd 			extern u_int atdevbase;
    145   1.1      cgd 
    146  1.15      cgd 			isdp->id_maddr -= 0xa0000; /* XXX should be a define */
    147   1.1      cgd 			isdp->id_maddr += atdevbase;
    148   1.1      cgd 		}
    149   1.1      cgd 		isdp->id_alive = (*dp->probe)(isdp);
    150  1.11  deraadt 		if (isdp->id_irq == (u_short)-1)
    151  1.11  deraadt 			isdp->id_alive = 0;
    152  1.15      cgd 		/*
    153  1.15      cgd 		 * Only print the I/O address range if id_alive != -1
    154  1.15      cgd 		 * Right now this is a temporary fix just for the new
    155  1.15      cgd 		 * NPX code so that if it finds a 486 that can use trap
    156  1.15      cgd 		 * 16 it will not report I/O addresses.
    157  1.15      cgd 		 * Rod Grimes 04/26/94
    158  1.15      cgd 		 *
    159  1.15      cgd 		 * XXX -- cgd
    160  1.15      cgd 		 */
    161   1.1      cgd 		if (isdp->id_alive) {
    162   1.1      cgd 			printf("%s%d", dp->name, isdp->id_unit);
    163   1.5      cgd 			printf(" at 0x%x", isdp->id_iobase);
    164   1.5      cgd 			if ((isdp->id_iobase + isdp->id_alive - 1) !=
    165   1.5      cgd 			     isdp->id_iobase)
    166   1.5      cgd 				printf("-0x%x",
    167   1.5      cgd 				       isdp->id_iobase + isdp->id_alive - 1);
    168  1.11  deraadt 			if (isdp->id_irq != 0)
    169  1.15      cgd 				printf(" irq %d", ffs(isdp->id_irq)-1);
    170   1.3  deraadt 			if (isdp->id_drq != -1)
    171  1.15      cgd 				printf(" drq %d", isdp->id_drq);
    172   1.4      cgd 			if (isdp->id_maddr != 0)
    173  1.15      cgd 				printf(" maddr 0x%x", kvtop(isdp->id_maddr));
    174   1.4      cgd 			if (isdp->id_msize != 0)
    175  1.20  deraadt 				printf("-0x%x", kvtop(isdp->id_maddr) +
    176  1.20  deraadt 					isdp->id_msize - 1);
    177   1.4      cgd 			if (isdp->id_flags != 0)
    178  1.15      cgd 				printf(" flags 0x%x", isdp->id_flags);
    179  1.15      cgd 			printf(" on isa\n");
    180   1.3  deraadt 
    181   1.6  deraadt 			config_attach(dp, isdp);
    182   1.6  deraadt 
    183  1.12  deraadt 			if (isdp->id_irq) {
    184   1.1      cgd 				int intrno;
    185   1.1      cgd 
    186   1.1      cgd 				intrno = ffs(isdp->id_irq)-1;
    187  1.15      cgd 				setidt(ICU_OFFSET+intrno, isdp->id_intr,
    188  1.15      cgd 					 SDT_SYS386IGT, SEL_KPL);
    189   1.3  deraadt 				if(mp)
    190   1.3  deraadt 					INTRMASK(*mp,isdp->id_irq);
    191  1.15      cgd 				INTREN(isdp->id_irq);
    192   1.1      cgd 			}
    193   1.1      cgd 		}
    194   1.1      cgd 		return (1);
    195   1.1      cgd 	} else	return(0);
    196   1.1      cgd }
    197   1.6  deraadt 
    198   1.6  deraadt void
    199   1.6  deraadt config_attach(struct isa_driver *dp, struct isa_device *isdp)
    200   1.6  deraadt {
    201   1.6  deraadt 	extern struct isa_device isa_subdev[];
    202   1.6  deraadt 	struct isa_device *dvp;
    203   1.6  deraadt 
    204   1.6  deraadt 	if(isdp->id_masunit==-1) {
    205   1.8  deraadt 		(void)(*dp->attach)(isdp);
    206   1.6  deraadt 		return;
    207   1.6  deraadt 	}
    208   1.6  deraadt 
    209   1.6  deraadt 	if(isdp->id_masunit==0) {
    210   1.6  deraadt 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    211   1.6  deraadt 			if (dvp->id_driver != dp)
    212   1.6  deraadt 				continue;
    213  1.10  deraadt 			if (dvp->id_masunit != isdp->id_unit)
    214   1.6  deraadt 				continue;
    215   1.6  deraadt 			if (dvp->id_physid == -1)
    216   1.6  deraadt 				continue;
    217   1.8  deraadt 			dvp->id_alive = (*dp->attach)(dvp);
    218   1.6  deraadt 		}
    219   1.6  deraadt 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    220   1.6  deraadt 			if (dvp->id_driver != dp)
    221   1.6  deraadt 				continue;
    222  1.10  deraadt 			if (dvp->id_masunit != isdp->id_unit)
    223   1.6  deraadt 				continue;
    224   1.6  deraadt 			if (dvp->id_physid != -1)
    225   1.6  deraadt 				continue;
    226   1.8  deraadt 			dvp->id_alive = (*dp->attach)(dvp);
    227   1.6  deraadt 		}
    228   1.6  deraadt 		return;
    229   1.6  deraadt 	}
    230   1.6  deraadt 	printf("id_masunit has weird value\n");
    231   1.6  deraadt }
    232   1.6  deraadt 
    233   1.1      cgd 
    234   1.1      cgd #define	IDTVEC(name)	__CONCAT(X,name)
    235   1.1      cgd /* default interrupt vector table entries */
    236   1.1      cgd extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    237   1.1      cgd 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    238   1.1      cgd 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    239   1.1      cgd 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    240   1.1      cgd 
    241   1.1      cgd static *defvec[16] = {
    242   1.1      cgd 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    243   1.1      cgd 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    244   1.1      cgd 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    245   1.1      cgd 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    246   1.1      cgd 
    247   1.1      cgd /* out of range default interrupt vector gate entry */
    248   1.1      cgd extern	IDTVEC(intrdefault);
    249  1.15      cgd 
    250   1.1      cgd /*
    251   1.1      cgd  * Fill in default interrupt table (in case of spuruious interrupt
    252   1.1      cgd  * during configuration of kernel, setup interrupt control unit
    253   1.1      cgd  */
    254  1.21   andrew void
    255   1.1      cgd isa_defaultirq() {
    256   1.1      cgd 	int i;
    257   1.1      cgd 
    258   1.1      cgd 	/* icu vectors */
    259   1.1      cgd 	for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++)
    260   1.1      cgd 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    261  1.15      cgd 
    262   1.1      cgd 	/* out of range vectors */
    263   1.1      cgd 	for (i = NRSVIDT; i < NIDT; i++)
    264   1.1      cgd 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    265   1.1      cgd 
    266   1.1      cgd 	/* initialize 8259's */
    267   1.1      cgd 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    268   1.1      cgd 	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
    269   1.1      cgd 	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
    270  1.15      cgd #ifdef AUTO_EOI_1
    271  1.15      cgd 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    272  1.15      cgd #else
    273   1.1      cgd 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    274  1.15      cgd #endif
    275   1.1      cgd 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    276  1.15      cgd 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    277  1.21   andrew #ifdef REORDER_IRQ
    278  1.15      cgd 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    279  1.21   andrew #endif
    280   1.1      cgd 
    281   1.1      cgd 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    282   1.1      cgd 	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
    283   1.1      cgd 	outb(IO_ICU2+1,2);		/* my slave id is 2 */
    284  1.15      cgd #ifdef AUTO_EOI_2
    285  1.15      cgd 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    286  1.15      cgd #else
    287   1.1      cgd 	outb(IO_ICU2+1,1);		/* 8086 mode */
    288  1.15      cgd #endif
    289   1.1      cgd 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    290  1.15      cgd 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    291   1.1      cgd }
    292   1.1      cgd 
    293   1.1      cgd /* region of physical memory known to be contiguous */
    294   1.1      cgd vm_offset_t isaphysmem;
    295   1.1      cgd static caddr_t dma_bounce[8];		/* XXX */
    296   1.1      cgd static char bounced[8];		/* XXX */
    297   1.1      cgd #define MAXDMASZ 512		/* XXX */
    298   1.1      cgd 
    299   1.1      cgd /* high byte of address is stored in this port for i-th dma channel */
    300   1.1      cgd static short dmapageport[8] =
    301   1.1      cgd 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    302   1.1      cgd 
    303   1.1      cgd /*
    304   1.1      cgd  * isa_dmacascade(): program 8237 DMA controller channel to accept
    305   1.1      cgd  * external dma control by a board.
    306   1.1      cgd  */
    307  1.21   andrew void
    308  1.21   andrew isa_dmacascade(unsigned chan)
    309   1.2      cgd {
    310   1.1      cgd 	if (chan > 7)
    311   1.1      cgd 		panic("isa_dmacascade: impossible request");
    312   1.1      cgd 
    313   1.1      cgd 	/* set dma channel mode, and set dma channel mode */
    314   1.2      cgd 	if ((chan & 4) == 0) {
    315   1.2      cgd 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    316   1.2      cgd 		outb(DMA1_SMSK, chan);
    317   1.2      cgd 	} else {
    318   1.2      cgd 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    319   1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    320   1.2      cgd 	}
    321   1.1      cgd }
    322   1.1      cgd 
    323   1.1      cgd /*
    324   1.1      cgd  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
    325   1.1      cgd  * problems by using a bounce buffer.
    326   1.1      cgd  */
    327  1.21   andrew void
    328  1.21   andrew isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
    329   1.1      cgd {	vm_offset_t phys;
    330   1.2      cgd 	int waport;
    331   1.1      cgd 	caddr_t newaddr;
    332   1.1      cgd 
    333   1.2      cgd 	if (    chan > 7
    334   1.2      cgd 	    || (chan < 4 && nbytes > (1<<16))
    335   1.2      cgd 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    336   1.1      cgd 		panic("isa_dmastart: impossible request");
    337   1.1      cgd 
    338   1.2      cgd 	if (isa_dmarangecheck(addr, nbytes, chan)) {
    339   1.1      cgd 		if (dma_bounce[chan] == 0)
    340   1.1      cgd 			dma_bounce[chan] =
    341   1.1      cgd 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    342   1.1      cgd 				(caddr_t) isaphysmem + NBPG*chan;
    343   1.1      cgd 		bounced[chan] = 1;
    344   1.1      cgd 		newaddr = dma_bounce[chan];
    345   1.1      cgd 		*(int *) newaddr = 0;	/* XXX */
    346   1.1      cgd 
    347   1.1      cgd 		/* copy bounce buffer on write */
    348   1.1      cgd 		if (!(flags & B_READ))
    349   1.1      cgd 			bcopy(addr, newaddr, nbytes);
    350   1.1      cgd 		addr = newaddr;
    351   1.1      cgd 	}
    352   1.1      cgd 
    353   1.1      cgd 	/* translate to physical */
    354   1.1      cgd 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    355   1.1      cgd 
    356   1.2      cgd 	if ((chan & 4) == 0) {
    357   1.2      cgd 		/*
    358   1.2      cgd 		 * Program one of DMA channels 0..3.  These are
    359   1.2      cgd 		 * byte mode channels.
    360   1.2      cgd 		 */
    361   1.2      cgd 		/* set dma channel mode, and reset address ff */
    362   1.2      cgd 		if (flags & B_READ)
    363   1.2      cgd 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    364   1.2      cgd 		else
    365   1.2      cgd 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    366   1.2      cgd 		outb(DMA1_FFC, 0);
    367   1.1      cgd 
    368   1.2      cgd 		/* send start address */
    369   1.2      cgd 		waport =  DMA1_CHN(chan);
    370   1.1      cgd 		outb(waport, phys);
    371   1.1      cgd 		outb(waport, phys>>8);
    372   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    373   1.2      cgd 
    374   1.2      cgd 		/* send count */
    375   1.2      cgd 		outb(waport + 1, --nbytes);
    376   1.2      cgd 		outb(waport + 1, nbytes>>8);
    377   1.2      cgd 
    378   1.2      cgd 		/* unmask channel */
    379   1.2      cgd 		outb(DMA1_SMSK, chan);
    380   1.1      cgd 	} else {
    381   1.2      cgd 		/*
    382   1.2      cgd 		 * Program one of DMA channels 4..7.  These are
    383   1.2      cgd 		 * word mode channels.
    384   1.2      cgd 		 */
    385   1.2      cgd 		/* set dma channel mode, and reset address ff */
    386   1.2      cgd 		if (flags & B_READ)
    387   1.2      cgd 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    388   1.2      cgd 		else
    389   1.2      cgd 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    390   1.2      cgd 		outb(DMA2_FFC, 0);
    391   1.2      cgd 
    392   1.2      cgd 		/* send start address */
    393   1.2      cgd 		waport = DMA2_CHN(chan - 4);
    394   1.1      cgd 		outb(waport, phys>>1);
    395   1.1      cgd 		outb(waport, phys>>9);
    396   1.2      cgd 		outb(dmapageport[chan], phys>>16);
    397   1.1      cgd 
    398   1.2      cgd 		/* send count */
    399   1.2      cgd 		nbytes >>= 1;
    400   1.1      cgd 		outb(waport + 2, --nbytes);
    401   1.1      cgd 		outb(waport + 2, nbytes>>8);
    402   1.2      cgd 
    403   1.2      cgd 		/* unmask channel */
    404   1.2      cgd 		outb(DMA2_SMSK, chan & 3);
    405   1.1      cgd 	}
    406   1.1      cgd }
    407   1.1      cgd 
    408  1.21   andrew void
    409  1.21   andrew isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
    410   1.1      cgd {
    411   1.1      cgd 
    412   1.1      cgd 	/* copy bounce buffer on read */
    413   1.1      cgd 	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
    414   1.1      cgd 	if (bounced[chan]) {
    415   1.1      cgd 		bcopy(dma_bounce[chan], addr, nbytes);
    416   1.1      cgd 		bounced[chan] = 0;
    417   1.1      cgd 	}
    418   1.1      cgd }
    419   1.1      cgd 
    420   1.1      cgd /*
    421   1.1      cgd  * Check for problems with the address range of a DMA transfer
    422   1.2      cgd  * (non-contiguous physical pages, outside of bus address space,
    423   1.2      cgd  * crossing DMA page boundaries).
    424   1.1      cgd  * Return true if special handling needed.
    425   1.1      cgd  */
    426   1.1      cgd 
    427  1.21   andrew int
    428   1.2      cgd isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
    429   1.2      cgd 	vm_offset_t phys, priorpage = 0, endva;
    430   1.2      cgd 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    431   1.1      cgd 
    432   1.1      cgd 	endva = (vm_offset_t)round_page(va + length);
    433   1.1      cgd 	for (; va < (caddr_t) endva ; va += NBPG) {
    434   1.1      cgd 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    435   1.1      cgd #define ISARAM_END	RAM_END
    436   1.1      cgd 		if (phys == 0)
    437   1.1      cgd 			panic("isa_dmacheck: no physical page present");
    438   1.1      cgd 		if (phys > ISARAM_END)
    439   1.1      cgd 			return (1);
    440   1.2      cgd 		if (priorpage) {
    441   1.2      cgd 			if (priorpage + NBPG != phys)
    442   1.2      cgd 				return (1);
    443   1.2      cgd 			/* check if crossing a DMA page boundary */
    444   1.2      cgd 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    445   1.2      cgd 				return (1);
    446   1.2      cgd 		}
    447   1.1      cgd 		priorpage = phys;
    448   1.1      cgd 	}
    449   1.1      cgd 	return (0);
    450   1.1      cgd }
    451   1.1      cgd 
    452   1.1      cgd /* head of queue waiting for physmem to become available */
    453   1.1      cgd struct buf isa_physmemq;
    454   1.1      cgd 
    455   1.1      cgd /* blocked waiting for resource to become free for exclusive use */
    456   1.1      cgd static isaphysmemflag;
    457   1.1      cgd /* if waited for and call requested when free (B_CALL) */
    458   1.1      cgd static void (*isaphysmemunblock)(); /* needs to be a list */
    459   1.1      cgd 
    460   1.1      cgd /*
    461   1.1      cgd  * Allocate contiguous physical memory for transfer, returning
    462   1.1      cgd  * a *virtual* address to region. May block waiting for resource.
    463   1.1      cgd  * (assumed to be called at splbio())
    464   1.1      cgd  */
    465   1.1      cgd caddr_t
    466   1.1      cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    467   1.1      cgd 
    468   1.1      cgd 	isaphysmemunblock = func;
    469   1.1      cgd 	while (isaphysmemflag & B_BUSY) {
    470   1.1      cgd 		isaphysmemflag |= B_WANTED;
    471   1.1      cgd 		sleep(&isaphysmemflag, PRIBIO);
    472   1.1      cgd 	}
    473   1.1      cgd 	isaphysmemflag |= B_BUSY;
    474   1.1      cgd 
    475   1.1      cgd 	return((caddr_t)isaphysmem);
    476   1.1      cgd }
    477   1.1      cgd 
    478   1.1      cgd /*
    479   1.1      cgd  * Free contiguous physical memory used for transfer.
    480   1.1      cgd  * (assumed to be called at splbio())
    481   1.1      cgd  */
    482   1.1      cgd void
    483   1.1      cgd isa_freephysmem(caddr_t va, unsigned length) {
    484   1.1      cgd 
    485   1.1      cgd 	isaphysmemflag &= ~B_BUSY;
    486   1.1      cgd 	if (isaphysmemflag & B_WANTED) {
    487   1.1      cgd 		isaphysmemflag &= B_WANTED;
    488   1.1      cgd 		wakeup(&isaphysmemflag);
    489   1.1      cgd 		if (isaphysmemunblock)
    490   1.1      cgd 			(*isaphysmemunblock)();
    491   1.1      cgd 	}
    492   1.1      cgd }
    493   1.1      cgd 
    494   1.1      cgd /*
    495   1.1      cgd  * Handle a NMI, possibly a machine check.
    496   1.1      cgd  * return true to panic system, false to ignore.
    497   1.1      cgd  */
    498  1.21   andrew int
    499   1.1      cgd isa_nmi(cd) {
    500   1.1      cgd 
    501   1.1      cgd 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    502   1.1      cgd 	return(0);
    503   1.1      cgd }
    504   1.1      cgd 
    505   1.1      cgd /*
    506   1.1      cgd  * Caught a stray interrupt, notify
    507   1.1      cgd  */
    508  1.21   andrew void
    509   1.1      cgd isa_strayintr(d) {
    510   1.1      cgd 
    511   1.1      cgd 	/* DON'T BOTHER FOR NOW! */
    512   1.1      cgd 	/* for some reason, we get bursts of intr #7, even if not enabled! */
    513   1.4      cgd 	/*
    514   1.4      cgd 	 * Well the reason you got bursts of intr #7 is because someone
    515   1.4      cgd 	 * raised an interrupt line and dropped it before the 8259 could
    516   1.4      cgd 	 * prioritize it.  This is documented in the intel data book.  This
    517   1.4      cgd 	 * means you have BAD hardware!  I have changed this so that only
    518  1.15      cgd 	 * the first 5 get logged, then it quits logging them, and puts
    519   1.4      cgd 	 * out a special message. rgrimes 3/25/1993
    520   1.4      cgd 	 */
    521  1.15      cgd 	extern u_long intrcnt_stray;
    522   1.4      cgd 
    523  1.15      cgd 	intrcnt_stray++;
    524  1.15      cgd 	if (intrcnt_stray <= 5)
    525   1.4      cgd 		log(LOG_ERR,"ISA strayintr %x\n", d);
    526  1.15      cgd 	if (intrcnt_stray == 5)
    527   1.4      cgd 		log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
    528   1.1      cgd }
    529   1.1      cgd 
    530   1.1      cgd /*
    531  1.15      cgd  * Wait "n" microseconds.
    532  1.15      cgd  * Relies on timer 1 counting down from (TIMER_FREQ / hz) at
    533  1.17  mycroft  * (1 * TIMER_FREQ) Hz.
    534  1.15      cgd  * Note: timer had better have been programmed before this is first used!
    535  1.17  mycroft  * (Note that we use `rate generator' mode, which counts at 1:1; `square
    536  1.17  mycroft  * wave' mode counts at 2:1).
    537  1.15      cgd  */
    538  1.17  mycroft #define       CF              (1 * TIMER_FREQ)
    539  1.15      cgd 
    540  1.15      cgd extern int hz;                        /* XXX - should be elsewhere */
    541  1.15      cgd 
    542  1.21   andrew void
    543  1.21   andrew DELAY(n)
    544  1.15      cgd 	int n;
    545  1.15      cgd {
    546  1.15      cgd 	int counter_limit;
    547  1.15      cgd 	int prev_tick;
    548  1.15      cgd 	int tick;
    549  1.15      cgd 	int ticks_left;
    550  1.15      cgd 	int sec;
    551  1.15      cgd 	int usec;
    552  1.15      cgd 
    553  1.15      cgd #ifdef DELAYDEBUG
    554  1.16  mycroft 	int gettick_calls = 1;
    555  1.15      cgd 	int n1;
    556  1.15      cgd 	static int state = 0;
    557  1.15      cgd 
    558  1.15      cgd 	if (state == 0) {
    559  1.15      cgd 		state = 1;
    560  1.15      cgd 		for (n1 = 1; n1 <= 10000000; n1 *= 10)
    561  1.15      cgd 			DELAY(n1);
    562  1.15      cgd 		state = 2;
    563  1.15      cgd 	}
    564  1.15      cgd 	if (state == 1)
    565  1.15      cgd 		printf("DELAY(%d)...", n);
    566  1.15      cgd #endif
    567  1.15      cgd 
    568  1.15      cgd 	/*
    569  1.15      cgd 	 * Read the counter first, so that the rest of the setup overhead is
    570  1.15      cgd 	 * counted.  Guess the initial overhead is 20 usec (on most systems it
    571  1.16  mycroft 	 * takes about 1.5 usec for each of the i/o's in gettick().  The loop
    572  1.15      cgd 	 * takes about 6 usec on a 486/33 and 13 usec on a 386/20.  The
    573  1.15      cgd 	 * multiplications and divisions to scale the count take a while).
    574  1.15      cgd 	 */
    575  1.16  mycroft 	prev_tick = gettick();
    576  1.15      cgd 	n -= 20;
    577  1.15      cgd 
    578  1.15      cgd 	/*
    579  1.15      cgd 	 * Calculate (n * (CF / 1e6)) without using floating point and without
    580  1.15      cgd 	 * any avoidable overflows.
    581  1.15      cgd 	 */
    582  1.15      cgd 	sec = n / 1000000;
    583  1.15      cgd 	usec = n - sec * 1000000;
    584  1.15      cgd 	ticks_left = sec * CF
    585  1.15      cgd 		+ usec * (CF / 1000000)
    586  1.15      cgd 		+ usec * ((CF % 1000000) / 1000) / 1000
    587  1.15      cgd 		+ usec * (CF % 1000) / 1000000;
    588  1.15      cgd 
    589  1.15      cgd 	counter_limit = TIMER_FREQ / hz;
    590  1.15      cgd 	while (ticks_left > 0) {
    591  1.16  mycroft 		tick = gettick();
    592  1.15      cgd #ifdef DELAYDEBUG
    593  1.16  mycroft 		++gettick_calls;
    594  1.15      cgd #endif
    595  1.15      cgd 		if (tick > prev_tick)
    596  1.15      cgd 			ticks_left -= prev_tick - (tick - counter_limit);
    597  1.15      cgd 		else
    598  1.15      cgd 			ticks_left -= prev_tick - tick;
    599  1.15      cgd 		prev_tick = tick;
    600   1.1      cgd 	}
    601  1.15      cgd #ifdef DELAYDEBUG
    602  1.15      cgd 	if (state == 1)
    603  1.16  mycroft 		printf(" %d calls to gettick() at %d usec each\n",
    604  1.16  mycroft 			gettick_calls, (n + 5) / gettick_calls);
    605  1.15      cgd #endif
    606   1.1      cgd }
    607   1.1      cgd 
    608  1.21   andrew int
    609  1.16  mycroft gettick() {
    610  1.15      cgd 	int high;
    611  1.15      cgd 	int low;
    612  1.15      cgd 
    613  1.15      cgd 	/*
    614  1.15      cgd 	 * XXX - isa.h defines bogus timers.  There's no such timer as
    615  1.15      cgd 	 * IO_TIMER_2 = 0x48.  There's a timer in the CMOS RAM chip but
    616  1.15      cgd 	 * its interface is quite different.  Neither timer is an 8252.
    617  1.15      cgd 	 */
    618  1.15      cgd 	/*
    619  1.15      cgd 	 * Protect ourself against interrupts.
    620  1.15      cgd 	 * XXX - sysbeep() and sysbeepstop() need protection.
    621  1.15      cgd 	 */
    622  1.15      cgd 	disable_intr();
    623  1.15      cgd 	/*
    624  1.15      cgd 	 * Latch the count for 'timer' (cc00xxxx, c = counter, x = any).
    625  1.15      cgd 	 */
    626  1.16  mycroft 	outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
    627  1.16  mycroft 	low = inb(TIMER_CNTR0);
    628  1.16  mycroft 	high = inb(TIMER_CNTR0);
    629  1.15      cgd 	enable_intr();
    630  1.15      cgd 	return ((high << 8) | low);
    631   1.1      cgd }
    632   1.1      cgd 
    633   1.1      cgd static beeping;
    634  1.21   andrew static void
    635  1.21   andrew sysbeepstop(int f)
    636   1.1      cgd {
    637  1.16  mycroft 	int s = splhigh();
    638  1.16  mycroft 
    639   1.1      cgd 	/* disable counter 2 */
    640  1.16  mycroft 	disable_intr();
    641   1.1      cgd 	outb(0x61, inb(0x61) & 0xFC);
    642  1.16  mycroft 	enable_intr();
    643   1.1      cgd 	if (f)
    644   1.1      cgd 		timeout(sysbeepstop, 0, f);
    645   1.1      cgd 	else
    646   1.1      cgd 		beeping = 0;
    647  1.16  mycroft 
    648  1.16  mycroft 	splx(s);
    649   1.1      cgd }
    650   1.1      cgd 
    651  1.21   andrew void
    652  1.21   andrew sysbeep(int pitch, int period)
    653   1.1      cgd {
    654  1.16  mycroft 	int s = splhigh();
    655  1.16  mycroft 	static int last_pitch, last_period;
    656   1.1      cgd 
    657  1.16  mycroft 	if (beeping) {
    658  1.16  mycroft 		untimeout(sysbeepstop, last_period/2);
    659  1.16  mycroft 		untimeout(sysbeepstop, 0);
    660   1.1      cgd 	}
    661  1.16  mycroft 	if (!beeping || last_pitch != pitch) {
    662  1.16  mycroft 		/*
    663  1.16  mycroft 	 	* XXX - move timer stuff to clock.c.
    664  1.16  mycroft 	 	*/
    665  1.16  mycroft 		disable_intr();
    666  1.16  mycroft 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    667  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    668  1.19  mycroft 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    669  1.16  mycroft 		outb(0x61, inb(0x61) | 3);	/* enable counter 2 */
    670  1.16  mycroft 		enable_intr();
    671  1.16  mycroft 	}
    672  1.16  mycroft 	last_pitch = pitch;
    673  1.16  mycroft 	beeping = last_period = period;
    674  1.16  mycroft 	timeout(sysbeepstop, period/2, period);
    675  1.16  mycroft 
    676  1.16  mycroft 	splx(s);
    677   1.1      cgd }
    678   1.1      cgd 
    679   1.1      cgd /*
    680   1.1      cgd  * Pass command to keyboard controller (8042)
    681   1.1      cgd  */
    682  1.21   andrew unsigned
    683  1.21   andrew kbc_8042cmd(int val)
    684  1.21   andrew {
    685   1.1      cgd 	while (inb(KBSTATP)&KBS_IBF);
    686   1.1      cgd 	if (val) outb(KBCMDP, val);
    687   1.1      cgd 	while (inb(KBSTATP)&KBS_IBF);
    688   1.1      cgd 	return (inb(KBDATAP));
    689  1.15      cgd }
    690  1.15      cgd 
    691  1.15      cgd /*
    692  1.15      cgd  * Return nonzero if a (masked) irq is pending for a given device.
    693  1.15      cgd  */
    694  1.15      cgd int
    695  1.15      cgd isa_irq_pending(dvp)
    696  1.15      cgd 	struct isa_device *dvp;
    697  1.15      cgd {
    698  1.15      cgd 	unsigned id_irq;
    699  1.15      cgd 
    700  1.15      cgd 	id_irq = (unsigned short) dvp->id_irq;	/* XXX silly type in struct */
    701  1.15      cgd 	if (id_irq & 0xff)
    702  1.15      cgd 		return (inb(IO_ICU1) & id_irq);
    703  1.15      cgd 	return (inb(IO_ICU2) & (id_irq >> 8));
    704   1.1      cgd }
    705