Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.14
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * William Jolitz.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	from: @(#)isa.c	7.2 (Berkeley) 5/13/91
     37  *	$Id: isa.c,v 1.14 1993/05/28 09:10:52 deraadt Exp $
     38  */
     39 
     40 /*
     41  * code to manage AT bus
     42  *
     43  * 92/08/18  Frank P. MacLachlan (fpm (at) crash.cts.com):
     44  * Fixed uninitialized variable problem and added code to deal
     45  * with DMA page boundaries in isa_dmarangecheck().  Fixed word
     46  * mode DMA count compution and reorganized DMA setup code in
     47  * isa_dmastart()
     48  */
     49 
     50 #include "param.h"
     51 #include "systm.h"
     52 #include "conf.h"
     53 #include "file.h"
     54 #include "buf.h"
     55 #include "uio.h"
     56 #include "syslog.h"
     57 #include "malloc.h"
     58 #include "rlist.h"
     59 #include "machine/segments.h"
     60 #include "vm/vm.h"
     61 #include "i386/isa/isa_device.h"
     62 #include "i386/isa/isa.h"
     63 #include "i386/isa/icu.h"
     64 #include "i386/isa/ic/i8237.h"
     65 #include "i386/isa/ic/i8042.h"
     66 
     67 /* sorry, has to be here, no place else really suitable */
     68 #include "machine/pc/display.h"
     69 u_short *Crtat = (u_short *)MONO_BUF;
     70 
     71 /*
     72 **  Register definitions for DMA controller 1 (channels 0..3):
     73 */
     74 #define	DMA1_CHN(c)	(IO_DMA1 + 1*(2*(c)))	/* addr reg for channel c */
     75 #define	DMA1_SMSK	(IO_DMA1 + 1*10)	/* single mask register */
     76 #define	DMA1_MODE	(IO_DMA1 + 1*11)	/* mode register */
     77 #define	DMA1_FFC	(IO_DMA1 + 1*12)	/* clear first/last FF */
     78 
     79 /*
     80 **  Register definitions for DMA controller 2 (channels 4..7):
     81 */
     82 #define	DMA2_CHN(c)	(IO_DMA1 + 2*(2*(c)))	/* addr reg for channel c */
     83 #define	DMA2_SMSK	(IO_DMA2 + 2*10)	/* single mask register */
     84 #define	DMA2_MODE	(IO_DMA2 + 2*11)	/* mode register */
     85 #define	DMA2_FFC	(IO_DMA2 + 2*12)	/* clear first/last FF */
     86 
     87 int config_isadev(struct isa_device *, u_short *);
     88 void config_attach(struct isa_driver *, struct isa_device *);
     89 
     90 /*
     91  * Configure all ISA devices
     92  */
     93 isa_configure() {
     94 	struct isa_device *dvp;
     95 	struct isa_driver *dp;
     96 
     97 	splhigh();
     98 	INTREN(IRQ_SLAVE);
     99 	for (dvp = isa_devtab_tty; config_isadev(dvp,&ttymask); dvp++)
    100 		;
    101 	for (dvp = isa_devtab_bio; config_isadev(dvp,&biomask); dvp++)
    102 		;
    103 	for (dvp = isa_devtab_net; config_isadev(dvp,&netmask); dvp++)
    104 		;
    105 	for (dvp = isa_devtab_null; config_isadev(dvp,0); dvp++)
    106 		;
    107 #include "sl.h"
    108 #if NSL > 0
    109 	netmask |= ttymask;
    110 	ttymask |= netmask;
    111 #endif
    112 
    113 	/* and the problem is... if netmask == 0, then the loopback
    114 	 * code can do some really ugly things.
    115 	 * workaround for this: if netmask == 0, set it to 0x8000, which
    116 	 * is the value used by splsoftclock.  this is nasty, but it
    117 	 * should work until this interrupt system goes away. -- cgd
    118 	 */
    119 	if (netmask == 0)
    120 		netmask = 0x8000;	/* same as for softclock.  XXX */
    121 
    122 	/* biomask |= ttymask ;  can some tty devices use buffers? */
    123 	/* printf("biomask %x ttymask %x netmask %x\n", biomask, ttymask, netmask); */
    124 	splnone();
    125 }
    126 
    127 /*
    128  * Configure an ISA device.
    129  */
    130 config_isadev(isdp, mp)
    131 	struct isa_device *isdp;
    132 	u_short *mp;
    133 {
    134 	struct isa_driver *dp;
    135 
    136 	if (dp = isdp->id_driver) {
    137 		if (isdp->id_maddr) {
    138 			extern u_int atdevbase;
    139 
    140 			isdp->id_maddr -= 0xa0000;
    141 			isdp->id_maddr += atdevbase;
    142 		}
    143 		isdp->id_alive = (*dp->probe)(isdp);
    144 		if (isdp->id_irq == (u_short)-1)
    145 			isdp->id_alive = 0;
    146 		if (isdp->id_alive) {
    147 			printf("%s%d", dp->name, isdp->id_unit);
    148 			printf(" at 0x%x", isdp->id_iobase);
    149 			if ((isdp->id_iobase + isdp->id_alive - 1) !=
    150 			     isdp->id_iobase)
    151 				printf("-0x%x",
    152 				       isdp->id_iobase + isdp->id_alive - 1);
    153 			printf(" ");
    154 			if (isdp->id_irq != 0)
    155 				printf("irq %d ", ffs(isdp->id_irq)-1);
    156 			if (isdp->id_drq != -1)
    157 				printf("drq %d ", isdp->id_drq);
    158 			if (isdp->id_maddr != 0)
    159 				printf("maddr 0x%x ", kvtop(isdp->id_maddr));
    160 			if (isdp->id_msize != 0)
    161 				printf("msize %d ", isdp->id_msize);
    162 			if (isdp->id_flags != 0)
    163 				printf("flags 0x%x ", isdp->id_flags);
    164 			printf("on isa\n");
    165 
    166 			config_attach(dp, isdp);
    167 
    168 			if (isdp->id_irq) {
    169 				int intrno;
    170 
    171 				intrno = ffs(isdp->id_irq)-1;
    172 				INTREN(isdp->id_irq);
    173 				if(mp)
    174 					INTRMASK(*mp,isdp->id_irq);
    175 				setidt(ICU_OFFSET+intrno, isdp->id_intr,
    176 					 SDT_SYS386IGT, SEL_KPL);
    177 			}
    178 		}
    179 		return (1);
    180 	} else	return(0);
    181 }
    182 
    183 void
    184 config_attach(struct isa_driver *dp, struct isa_device *isdp)
    185 {
    186 	extern struct isa_device isa_subdev[];
    187 	struct isa_device *dvp;
    188 
    189 	if(isdp->id_masunit==-1) {
    190 		(void)(*dp->attach)(isdp);
    191 		return;
    192 	}
    193 
    194 	if(isdp->id_masunit==0) {
    195 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    196 			if (dvp->id_driver != dp)
    197 				continue;
    198 			if (dvp->id_masunit != isdp->id_unit)
    199 				continue;
    200 			if (dvp->id_physid == -1)
    201 				continue;
    202 			dvp->id_alive = (*dp->attach)(dvp);
    203 		}
    204 		for(dvp = isa_subdev; dvp->id_driver; dvp++) {
    205 			if (dvp->id_driver != dp)
    206 				continue;
    207 			if (dvp->id_masunit != isdp->id_unit)
    208 				continue;
    209 			if (dvp->id_physid != -1)
    210 				continue;
    211 			dvp->id_alive = (*dp->attach)(dvp);
    212 		}
    213 		return;
    214 	}
    215 	printf("id_masunit has weird value\n");
    216 }
    217 
    218 
    219 #define	IDTVEC(name)	__CONCAT(X,name)
    220 /* default interrupt vector table entries */
    221 extern	IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
    222 	IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
    223 	IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
    224 	IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
    225 
    226 static *defvec[16] = {
    227 	&IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
    228 	&IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
    229 	&IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
    230 	&IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
    231 
    232 /* out of range default interrupt vector gate entry */
    233 extern	IDTVEC(intrdefault);
    234 
    235 /*
    236  * Fill in default interrupt table (in case of spuruious interrupt
    237  * during configuration of kernel, setup interrupt control unit
    238  */
    239 isa_defaultirq() {
    240 	int i;
    241 
    242 	/* icu vectors */
    243 	for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++)
    244 		setidt(i, defvec[i],  SDT_SYS386IGT, SEL_KPL);
    245 
    246 	/* out of range vectors */
    247 	for (i = NRSVIDT; i < NIDT; i++)
    248 		setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
    249 
    250 	/* clear npx intr latch */
    251 	outb(0xf1,0);
    252 
    253 	/* initialize 8259's */
    254 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    255 	outb(IO_ICU1+1, NRSVIDT);	/* starting at this vector index */
    256 	outb(IO_ICU1+1, 1<<2);		/* slave on line 2 */
    257 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    258 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    259 	outb(IO_ICU1, 2);		/* default to ISR on read */
    260 
    261 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    262 	outb(IO_ICU2+1, NRSVIDT+8);	/* staring at this vector index */
    263 	outb(IO_ICU2+1,2);		/* my slave id is 2 */
    264 	outb(IO_ICU2+1,1);		/* 8086 mode */
    265 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    266 	outb(IO_ICU2, 2);		/* default to ISR on read */
    267 }
    268 
    269 /* region of physical memory known to be contiguous */
    270 vm_offset_t isaphysmem;
    271 static caddr_t dma_bounce[8];		/* XXX */
    272 static char bounced[8];		/* XXX */
    273 #define MAXDMASZ 512		/* XXX */
    274 
    275 /* high byte of address is stored in this port for i-th dma channel */
    276 static short dmapageport[8] =
    277 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
    278 
    279 /*
    280  * isa_dmacascade(): program 8237 DMA controller channel to accept
    281  * external dma control by a board.
    282  */
    283 void isa_dmacascade(unsigned chan)
    284 {
    285 	if (chan > 7)
    286 		panic("isa_dmacascade: impossible request");
    287 
    288 	/* set dma channel mode, and set dma channel mode */
    289 	if ((chan & 4) == 0) {
    290 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
    291 		outb(DMA1_SMSK, chan);
    292 	} else {
    293 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
    294 		outb(DMA2_SMSK, chan & 3);
    295 	}
    296 }
    297 
    298 /*
    299  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
    300  * problems by using a bounce buffer.
    301  */
    302 void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
    303 {	vm_offset_t phys;
    304 	int waport;
    305 	caddr_t newaddr;
    306 
    307 	if (    chan > 7
    308 	    || (chan < 4 && nbytes > (1<<16))
    309 	    || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
    310 		panic("isa_dmastart: impossible request");
    311 
    312 	if (isa_dmarangecheck(addr, nbytes, chan)) {
    313 		if (dma_bounce[chan] == 0)
    314 			dma_bounce[chan] =
    315 				/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
    316 				(caddr_t) isaphysmem + NBPG*chan;
    317 		bounced[chan] = 1;
    318 		newaddr = dma_bounce[chan];
    319 		*(int *) newaddr = 0;	/* XXX */
    320 
    321 		/* copy bounce buffer on write */
    322 		if (!(flags & B_READ))
    323 			bcopy(addr, newaddr, nbytes);
    324 		addr = newaddr;
    325 	}
    326 
    327 	/* translate to physical */
    328 	phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
    329 
    330 	if ((chan & 4) == 0) {
    331 		/*
    332 		 * Program one of DMA channels 0..3.  These are
    333 		 * byte mode channels.
    334 		 */
    335 		/* set dma channel mode, and reset address ff */
    336 		if (flags & B_READ)
    337 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
    338 		else
    339 			outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
    340 		outb(DMA1_FFC, 0);
    341 
    342 		/* send start address */
    343 		waport =  DMA1_CHN(chan);
    344 		outb(waport, phys);
    345 		outb(waport, phys>>8);
    346 		outb(dmapageport[chan], phys>>16);
    347 
    348 		/* send count */
    349 		outb(waport + 1, --nbytes);
    350 		outb(waport + 1, nbytes>>8);
    351 
    352 		/* unmask channel */
    353 		outb(DMA1_SMSK, chan);
    354 	} else {
    355 		/*
    356 		 * Program one of DMA channels 4..7.  These are
    357 		 * word mode channels.
    358 		 */
    359 		/* set dma channel mode, and reset address ff */
    360 		if (flags & B_READ)
    361 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
    362 		else
    363 			outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
    364 		outb(DMA2_FFC, 0);
    365 
    366 		/* send start address */
    367 		waport = DMA2_CHN(chan - 4);
    368 		outb(waport, phys>>1);
    369 		outb(waport, phys>>9);
    370 		outb(dmapageport[chan], phys>>16);
    371 
    372 		/* send count */
    373 		nbytes >>= 1;
    374 		outb(waport + 2, --nbytes);
    375 		outb(waport + 2, nbytes>>8);
    376 
    377 		/* unmask channel */
    378 		outb(DMA2_SMSK, chan & 3);
    379 	}
    380 }
    381 
    382 void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
    383 {
    384 
    385 	/* copy bounce buffer on read */
    386 	/*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
    387 	if (bounced[chan]) {
    388 		bcopy(dma_bounce[chan], addr, nbytes);
    389 		bounced[chan] = 0;
    390 	}
    391 }
    392 
    393 /*
    394  * Check for problems with the address range of a DMA transfer
    395  * (non-contiguous physical pages, outside of bus address space,
    396  * crossing DMA page boundaries).
    397  * Return true if special handling needed.
    398  */
    399 
    400 isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
    401 	vm_offset_t phys, priorpage = 0, endva;
    402 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    403 
    404 	endva = (vm_offset_t)round_page(va + length);
    405 	for (; va < (caddr_t) endva ; va += NBPG) {
    406 		phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
    407 #define ISARAM_END	RAM_END
    408 		if (phys == 0)
    409 			panic("isa_dmacheck: no physical page present");
    410 		if (phys > ISARAM_END)
    411 			return (1);
    412 		if (priorpage) {
    413 			if (priorpage + NBPG != phys)
    414 				return (1);
    415 			/* check if crossing a DMA page boundary */
    416 			if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
    417 				return (1);
    418 		}
    419 		priorpage = phys;
    420 	}
    421 	return (0);
    422 }
    423 
    424 /* head of queue waiting for physmem to become available */
    425 struct buf isa_physmemq;
    426 
    427 /* blocked waiting for resource to become free for exclusive use */
    428 static isaphysmemflag;
    429 /* if waited for and call requested when free (B_CALL) */
    430 static void (*isaphysmemunblock)(); /* needs to be a list */
    431 
    432 /*
    433  * Allocate contiguous physical memory for transfer, returning
    434  * a *virtual* address to region. May block waiting for resource.
    435  * (assumed to be called at splbio())
    436  */
    437 caddr_t
    438 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    439 
    440 	isaphysmemunblock = func;
    441 	while (isaphysmemflag & B_BUSY) {
    442 		isaphysmemflag |= B_WANTED;
    443 		sleep(&isaphysmemflag, PRIBIO);
    444 	}
    445 	isaphysmemflag |= B_BUSY;
    446 
    447 	return((caddr_t)isaphysmem);
    448 }
    449 
    450 /*
    451  * Free contiguous physical memory used for transfer.
    452  * (assumed to be called at splbio())
    453  */
    454 void
    455 isa_freephysmem(caddr_t va, unsigned length) {
    456 
    457 	isaphysmemflag &= ~B_BUSY;
    458 	if (isaphysmemflag & B_WANTED) {
    459 		isaphysmemflag &= B_WANTED;
    460 		wakeup(&isaphysmemflag);
    461 		if (isaphysmemunblock)
    462 			(*isaphysmemunblock)();
    463 	}
    464 }
    465 
    466 /*
    467  * Handle a NMI, possibly a machine check.
    468  * return true to panic system, false to ignore.
    469  */
    470 isa_nmi(cd) {
    471 
    472 	log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    473 	return(0);
    474 }
    475 
    476 /*
    477  * Caught a stray interrupt, notify
    478  */
    479 isa_strayintr(d) {
    480 
    481 	/* DON'T BOTHER FOR NOW! */
    482 	/* for some reason, we get bursts of intr #7, even if not enabled! */
    483 	/*
    484 	 * Well the reason you got bursts of intr #7 is because someone
    485 	 * raised an interrupt line and dropped it before the 8259 could
    486 	 * prioritize it.  This is documented in the intel data book.  This
    487 	 * means you have BAD hardware!  I have changed this so that only
    488 	 * the first 10 get logged, then it quits logging them, and puts
    489 	 * out a special message. rgrimes 3/25/1993
    490 	 */
    491 	extern u_long isa_stray_intrcnt;
    492 
    493 	isa_stray_intrcnt++;
    494 	if (isa_stray_intrcnt <= 10)
    495 		log(LOG_ERR,"ISA strayintr %x\n", d);
    496 	if (isa_stray_intrcnt == 10)
    497 		log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
    498 }
    499 
    500 /*
    501  * Wait "n" microseconds. Relies on timer 0 to have 1Mhz clock, regardless
    502  * of processor board speed. Note: timer had better have been programmed
    503  * before this is first used!
    504  */
    505 DELAY(n) {
    506 	int tick = getit(0,0) & 1;
    507 
    508 	while (n--) {
    509 		/* wait approximately 1 micro second */
    510 		while (tick == getit(0,0) & 1) ;
    511 
    512 		tick = getit(0,0) & 1;
    513 	}
    514 }
    515 
    516 getit(unit, timer) {
    517 	int port = (unit ? IO_TIMER2 : IO_TIMER1) + timer, val;
    518 
    519 	val = inb(port);
    520 	val = (inb(port) << 8) + val;
    521 	return (val);
    522 }
    523 
    524 extern int hz;
    525 
    526 static beeping;
    527 static
    528 sysbeepstop(f)
    529 {
    530 	/* disable counter 2 */
    531 	outb(0x61, inb(0x61) & 0xFC);
    532 	if (f)
    533 		timeout(sysbeepstop, 0, f);
    534 	else
    535 		beeping = 0;
    536 }
    537 
    538 void sysbeep(int pitch, int period)
    539 {
    540 
    541 	outb(0x61, inb(0x61) | 3);	/* enable counter 2 */
    542 	outb(0x43, 0xb6);	/* set command for counter 2, 2 byte write */
    543 
    544 	outb(0x42, pitch);
    545 	outb(0x42, (pitch>>8));
    546 
    547 	if (!beeping) {
    548 		beeping = period;
    549 		timeout(sysbeepstop, period/2, period);
    550 	}
    551 }
    552 
    553 /*
    554  * Pass command to keyboard controller (8042)
    555  */
    556 unsigned kbc_8042cmd(val) {
    557 
    558 	while (inb(KBSTATP)&KBS_IBF);
    559 	if (val) outb(KBCMDP, val);
    560 	while (inb(KBSTATP)&KBS_IBF);
    561 	return (inb(KBDATAP));
    562 }
    563