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