isa.c revision 765
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.7 1993/11/09 02:12:36 alm Exp $ 38 */ 39 40/* 41 * code to manage AT bus 42 * 43 * 92/08/18 Frank P. MacLachlan (fpm@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_DMA2 + 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 83int config_isadev __P((struct isa_device *, u_int *)); 84 85/* 86 * print a conflict message 87 */ 88void 89conflict(dvp, tmpdvp, item, reason, format) 90 struct isa_device *dvp, *tmpdvp; 91 int item; 92 char *reason; 93 char *format; 94{ 95 printf("%s%d not probed due to %s conflict with %s%d at ", 96 dvp->id_driver->name, dvp->id_unit, reason, 97 tmpdvp->id_driver->name, tmpdvp->id_unit); 98 printf(format, item); 99 printf("\n"); 100} 101 102/* 103 * Check to see if things are alread in use, like IRQ's, I/O addresses 104 * and Memory addresses. 105 */ 106int 107haveseen(dvp, tmpdvp) 108 struct isa_device *dvp, *tmpdvp; 109{ 110 int status = 0; 111 112 /* 113 * Only check against devices that have already been found 114 */ 115 if (tmpdvp->id_alive) { 116 /* 117 * Check for I/O address conflict. We can only check the 118 * starting address of the device against the range of the 119 * device that has already been probed since we do not 120 * know how many I/O addresses this device uses. 121 */ 122 if (tmpdvp->id_alive != -1) { 123 if ((dvp->id_iobase >= tmpdvp->id_iobase) && 124 (dvp->id_iobase <= 125 (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) { 126 conflict(dvp, tmpdvp, dvp->id_iobase, 127 "I/O address", "0x%x"); 128 status = 1; 129 } 130 } 131 /* 132 * Check for Memory address conflict. We can check for 133 * range overlap, but it will not catch all cases since the 134 * driver may adjust the msize paramater during probe, for 135 * now we just check that the starting address does not 136 * fall within any allocated region. 137 * XXX could add a second check after the probe for overlap, 138 * since at that time we would know the full range. 139 * XXX KERNBASE is a hack, we should have vaddr in the table! 140 */ 141 if(tmpdvp->id_maddr) { 142 if((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) && 143 (KERNBASE + dvp->id_maddr <= 144 (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) { 145 conflict(dvp, tmpdvp, dvp->id_maddr, "maddr", 146 "0x%x"); 147 status = 1; 148 } 149 } 150#ifndef COM_MULTIPORT 151 /* 152 * Check for IRQ conflicts. 153 */ 154 if(tmpdvp->id_irq) { 155 if (tmpdvp->id_irq == dvp->id_irq) { 156 conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1, 157 "irq", "%d"); 158 status = 1; 159 } 160 } 161#endif 162 /* 163 * Check for DRQ conflicts. 164 */ 165 if(tmpdvp->id_drq != -1) { 166 if (tmpdvp->id_drq == dvp->id_drq) { 167 conflict(dvp, tmpdvp, dvp->id_drq, 168 "drq", "%d"); 169 status = 1; 170 } 171 } 172 } 173 return (status); 174} 175 176/* 177 * Search through all the isa_devtab_* tables looking for anything that 178 * conflicts with the current device. 179 */ 180int 181haveseen_isadev(dvp) 182 struct isa_device *dvp; 183{ 184 struct isa_device *tmpdvp; 185 int status = 0; 186 187 for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) { 188 status |= haveseen(dvp, tmpdvp); 189 } 190 for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) { 191 status |= haveseen(dvp, tmpdvp); 192 } 193 for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) { 194 status |= haveseen(dvp, tmpdvp); 195 } 196 for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) { 197 status |= haveseen(dvp, tmpdvp); 198 } 199 return(status); 200} 201 202/* 203 * Configure all ISA devices 204 */ 205void 206isa_configure() { 207 struct isa_device *dvp; 208 209 enable_intr(); 210 splhigh(); 211 INTREN(IRQ_SLAVE); 212 printf("Probing for devices on the ISA bus:\n"); 213 for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) { 214 if (!haveseen_isadev(dvp)) 215 config_isadev(dvp,&ttymask); 216 } 217 for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) { 218 if (!haveseen_isadev(dvp)) 219 config_isadev(dvp,&biomask); 220 } 221 for (dvp = isa_devtab_net; dvp->id_driver; dvp++) { 222 if (!haveseen_isadev(dvp)) 223 config_isadev(dvp,&netmask); 224 } 225 for (dvp = isa_devtab_null; dvp->id_driver; dvp++) { 226 if (!haveseen_isadev(dvp)) 227 config_isadev(dvp,(u_int *) NULL); 228 } 229/* 230 * XXX We should really add the tty device to netmask when the line is 231 * switched to SLIPDISC, and then remove it when it is switched away from 232 * SLIPDISC. No need to block out ALL ttys during a splnet when only one 233 * of them is running slip. 234 */ 235#include "sl.h" 236#if NSL > 0 237 netmask |= ttymask; 238 ttymask |= netmask; 239#endif 240 /* if netmask == 0, then the loopback code can do some really 241 * bad things. 242 * workaround for this: if netmask == 0, set it to 0x8000, 243 * which is value used by splsoftclock 244 */ 245 if (netmask == 0) 246 netmask = 0x8000; /* same as for softclock from icu.s */ 247 /* biomask |= ttymask ; can some tty devices use buffers? */ 248 printf("biomask %x ttymask %x netmask %x\n", biomask, ttymask, netmask); 249 splnone(); 250} 251 252/* 253 * Configure an ISA device. 254 */ 255config_isadev(isdp, mp) 256 struct isa_device *isdp; 257 u_int *mp; 258{ 259 struct isa_driver *dp = isdp->id_driver; 260 261 if (isdp->id_maddr) { 262 extern u_int atdevbase; 263 264 isdp->id_maddr -= 0xa0000; /* XXX should be a define */ 265 isdp->id_maddr += atdevbase; 266 } 267 isdp->id_alive = (*dp->probe)(isdp); 268 if (isdp->id_alive) { 269 /* 270 * Only print the I/O address range if id_alive != -1 271 * Right now this is a temporary fix just for the new 272 * NPX code so that if it finds a 486 that can use trap 273 * 16 it will not report I/O addresses. 274 * Rod Grimes 04/26/94 275 */ 276 printf("%s%d", dp->name, isdp->id_unit); 277 if (isdp->id_alive != -1) { 278 printf(" at 0x%x", isdp->id_iobase); 279 if ((isdp->id_iobase + isdp->id_alive - 1) != 280 isdp->id_iobase) { 281 printf("-0x%x", 282 isdp->id_iobase + 283 isdp->id_alive - 1); 284 } 285 } 286 if(isdp->id_irq) 287 printf(" irq %d", ffs(isdp->id_irq) - 1); 288 if (isdp->id_drq != -1) 289 printf(" drq %d", isdp->id_drq); 290 if (isdp->id_maddr) 291 printf(" maddr 0x%x", kvtop(isdp->id_maddr)); 292 if (isdp->id_msize) 293 printf(" msize %d", isdp->id_msize); 294 if (isdp->id_flags) 295 printf(" flags 0x%x", isdp->id_flags); 296 if (isdp->id_iobase < 0x100) 297 printf(" on motherboard\n"); 298 else 299 printf(" on isa\n"); 300 301 (*dp->attach)(isdp); 302 303 if(isdp->id_irq) { 304 int intrno; 305 306 intrno = ffs(isdp->id_irq)-1; 307 setidt(ICU_OFFSET+intrno, isdp->id_intr, 308 SDT_SYS386IGT, SEL_KPL); 309 if(mp) { 310 INTRMASK(*mp,isdp->id_irq); 311 } 312 INTREN(isdp->id_irq); 313 } 314 } else { 315 printf("%s%d not found", dp->name, isdp->id_unit); 316 if (isdp->id_iobase) { 317 printf(" at 0x%x", isdp->id_iobase); 318 } 319 printf("\n"); 320 } 321} 322 323#define IDTVEC(name) __CONCAT(X,name) 324/* default interrupt vector table entries */ 325extern IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3), 326 IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7), 327 IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11), 328 IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15); 329 330static *defvec[16] = { 331 &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3), 332 &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7), 333 &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11), 334 &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) }; 335 336/* out of range default interrupt vector gate entry */ 337extern IDTVEC(intrdefault); 338 339/* 340 * Fill in default interrupt table (in case of spuruious interrupt 341 * during configuration of kernel, setup interrupt control unit 342 */ 343isa_defaultirq() { 344 int i; 345 346 /* icu vectors */ 347 for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++) 348 setidt(i, defvec[i], SDT_SYS386IGT, SEL_KPL); 349 350 /* out of range vectors */ 351 for (i = NRSVIDT; i < NIDT; i++) 352 setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL); 353 354 /* initialize 8259's */ 355 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */ 356 outb(IO_ICU1+1, NRSVIDT); /* starting at this vector index */ 357 outb(IO_ICU1+1, 1<<2); /* slave on line 2 */ 358#ifdef AUTO_EOI_1 359 outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */ 360#else 361 outb(IO_ICU1+1, 1); /* 8086 mode */ 362#endif 363 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */ 364 outb(IO_ICU1, 0x0a); /* default to IRR on read */ 365 outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */ 366 367 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */ 368 outb(IO_ICU2+1, NRSVIDT+8); /* staring at this vector index */ 369 outb(IO_ICU2+1,2); /* my slave id is 2 */ 370#ifdef AUTO_EOI_2 371 outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */ 372#else 373 outb(IO_ICU2+1,1); /* 8086 mode */ 374#endif 375 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */ 376 outb(IO_ICU2, 0x0a); /* default to IRR on read */ 377} 378 379/* region of physical memory known to be contiguous */ 380vm_offset_t isaphysmem; 381static caddr_t dma_bounce[8]; /* XXX */ 382static char bounced[8]; /* XXX */ 383#define MAXDMASZ 512 /* XXX */ 384 385/* high byte of address is stored in this port for i-th dma channel */ 386static short dmapageport[8] = 387 { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a }; 388 389/* 390 * isa_dmacascade(): program 8237 DMA controller channel to accept 391 * external dma control by a board. 392 */ 393void isa_dmacascade(unsigned chan) 394{ 395 if (chan > 7) 396 panic("isa_dmacascade: impossible request"); 397 398 /* set dma channel mode, and set dma channel mode */ 399 if ((chan & 4) == 0) { 400 outb(DMA1_MODE, DMA37MD_CASCADE | chan); 401 outb(DMA1_SMSK, chan); 402 } else { 403 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3)); 404 outb(DMA2_SMSK, chan & 3); 405 } 406} 407 408/* 409 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment 410 * problems by using a bounce buffer. 411 */ 412void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan) 413{ vm_offset_t phys; 414 int waport; 415 caddr_t newaddr; 416 417 if ( chan > 7 418 || (chan < 4 && nbytes > (1<<16)) 419 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1))) 420 panic("isa_dmastart: impossible request"); 421 422 if (isa_dmarangecheck(addr, nbytes, chan)) { 423 if (dma_bounce[chan] == 0) 424 dma_bounce[chan] = 425 /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/ 426 (caddr_t) isaphysmem + NBPG*chan; 427 bounced[chan] = 1; 428 newaddr = dma_bounce[chan]; 429 *(int *) newaddr = 0; /* XXX */ 430 431 /* copy bounce buffer on write */ 432 if (!(flags & B_READ)) 433 bcopy(addr, newaddr, nbytes); 434 addr = newaddr; 435 } 436 437 /* translate to physical */ 438 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr); 439 440 if ((chan & 4) == 0) { 441 /* 442 * Program one of DMA channels 0..3. These are 443 * byte mode channels. 444 */ 445 /* set dma channel mode, and reset address ff */ 446 if (flags & B_READ) 447 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan); 448 else 449 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan); 450 outb(DMA1_FFC, 0); 451 452 /* send start address */ 453 waport = DMA1_CHN(chan); 454 outb(waport, phys); 455 outb(waport, phys>>8); 456 outb(dmapageport[chan], phys>>16); 457 458 /* send count */ 459 outb(waport + 1, --nbytes); 460 outb(waport + 1, nbytes>>8); 461 462 /* unmask channel */ 463 outb(DMA1_SMSK, chan); 464 } else { 465 /* 466 * Program one of DMA channels 4..7. These are 467 * word mode channels. 468 */ 469 /* set dma channel mode, and reset address ff */ 470 if (flags & B_READ) 471 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3)); 472 else 473 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3)); 474 outb(DMA2_FFC, 0); 475 476 /* send start address */ 477 waport = DMA2_CHN(chan - 4); 478 outb(waport, phys>>1); 479 outb(waport, phys>>9); 480 outb(dmapageport[chan], phys>>16); 481 482 /* send count */ 483 nbytes >>= 1; 484 outb(waport + 2, --nbytes); 485 outb(waport + 2, nbytes>>8); 486 487 /* unmask channel */ 488 outb(DMA2_SMSK, chan & 3); 489 } 490} 491 492void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan) 493{ 494 495 /* copy bounce buffer on read */ 496 /*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/ 497 if (bounced[chan]) { 498 bcopy(dma_bounce[chan], addr, nbytes); 499 bounced[chan] = 0; 500 } 501} 502 503/* 504 * Check for problems with the address range of a DMA transfer 505 * (non-contiguous physical pages, outside of bus address space, 506 * crossing DMA page boundaries). 507 * Return true if special handling needed. 508 */ 509 510isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) { 511 vm_offset_t phys, priorpage = 0, endva; 512 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1); 513 514 endva = (vm_offset_t)round_page(va + length); 515 for (; va < (caddr_t) endva ; va += NBPG) { 516 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va)); 517#define ISARAM_END RAM_END 518 if (phys == 0) 519 panic("isa_dmacheck: no physical page present"); 520 if (phys > ISARAM_END) 521 return (1); 522 if (priorpage) { 523 if (priorpage + NBPG != phys) 524 return (1); 525 /* check if crossing a DMA page boundary */ 526 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk) 527 return (1); 528 } 529 priorpage = phys; 530 } 531 return (0); 532} 533 534/* head of queue waiting for physmem to become available */ 535struct buf isa_physmemq; 536 537/* blocked waiting for resource to become free for exclusive use */ 538static isaphysmemflag; 539/* if waited for and call requested when free (B_CALL) */ 540static void (*isaphysmemunblock)(); /* needs to be a list */ 541 542/* 543 * Allocate contiguous physical memory for transfer, returning 544 * a *virtual* address to region. May block waiting for resource. 545 * (assumed to be called at splbio()) 546 */ 547caddr_t 548isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) { 549 550 isaphysmemunblock = func; 551 while (isaphysmemflag & B_BUSY) { 552 isaphysmemflag |= B_WANTED; 553 tsleep(&isaphysmemflag, PRIBIO, "isaphys", 0); 554 } 555 isaphysmemflag |= B_BUSY; 556 557 return((caddr_t)isaphysmem); 558} 559 560/* 561 * Free contiguous physical memory used for transfer. 562 * (assumed to be called at splbio()) 563 */ 564void 565isa_freephysmem(caddr_t va, unsigned length) { 566 567 isaphysmemflag &= ~B_BUSY; 568 if (isaphysmemflag & B_WANTED) { 569 isaphysmemflag &= B_WANTED; 570 wakeup(&isaphysmemflag); 571 if (isaphysmemunblock) 572 (*isaphysmemunblock)(); 573 } 574} 575 576/* 577 * Handle a NMI, possibly a machine check. 578 * return true to panic system, false to ignore. 579 */ 580isa_nmi(cd) { 581 582 log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70)); 583 return(0); 584} 585 586/* 587 * Caught a stray interrupt, notify 588 */ 589isa_strayintr(d) { 590 591 /* DON'T BOTHER FOR NOW! */ 592 /* for some reason, we get bursts of intr #7, even if not enabled! */ 593 /* 594 * Well the reason you got bursts of intr #7 is because someone 595 * raised an interrupt line and dropped it before the 8259 could 596 * prioritize it. This is documented in the intel data book. This 597 * means you have BAD hardware! I have changed this so that only 598 * the first 5 get logged, then it quits logging them, and puts 599 * out a special message. rgrimes 3/25/1993 600 */ 601 extern u_long intrcnt_stray; 602 603 intrcnt_stray++; 604 if (intrcnt_stray <= 5) 605 log(LOG_ERR,"ISA strayintr %x\n", d); 606 if (intrcnt_stray == 5) 607 log(LOG_CRIT,"Too many ISA strayintr not logging any more\n"); 608} 609 610/* 611 * Wait "n" microseconds. 612 * Relies on timer 1 counting down from (TIMER_FREQ / hz) at 613 * (1 * TIMER_FREQ) Hz. 614 * Note: timer had better have been programmed before this is first used! 615 * (The standard programming causes the timer to generate a square wave and 616 * the counter is decremented twice every cycle.) 617 */ 618#define CF (1 * TIMER_FREQ) 619#define TIMER_FREQ 1193182 /* XXX - should be elsewhere */ 620 621extern int hz; /* XXX - should be elsewhere */ 622 623int DELAY(n) 624 int n; 625{ 626 int counter_limit; 627 int prev_tick; 628 int tick; 629 int ticks_left; 630 int sec; 631 int usec; 632 633#ifdef DELAYDEBUG 634 int getit_calls = 1; 635 int n1; 636 static int state = 0; 637 638 if (state == 0) { 639 state = 1; 640 for (n1 = 1; n1 <= 10000000; n1 *= 10) 641 DELAY(n1); 642 state = 2; 643 } 644 if (state == 1) 645 printf("DELAY(%d)...", n); 646#endif 647 648 /* 649 * Read the counter first, so that the rest of the setup overhead is 650 * counted. Guess the initial overhead is 20 usec (on most systems it 651 * takes about 1.5 usec for each of the i/o's in getit(). The loop 652 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The 653 * multiplications and divisions to scale the count take a while). 654 */ 655 prev_tick = getit(0, 0); 656 n -= 20; 657 658 /* 659 * Calculate (n * (CF / 1e6)) without using floating point and without 660 * any avoidable overflows. 661 */ 662 sec = n / 1000000; 663 usec = n - sec * 1000000; 664 ticks_left = sec * CF 665 + usec * (CF / 1000000) 666 + usec * ((CF % 1000000) / 1000) / 1000 667 + usec * (CF % 1000) / 1000000; 668 669 counter_limit = TIMER_FREQ / hz; 670 while (ticks_left > 0) { 671 tick = getit(0, 0); 672#ifdef DELAYDEBUG 673 ++getit_calls; 674#endif 675 if (tick > prev_tick) 676 ticks_left -= prev_tick - (tick - counter_limit); 677 else 678 ticks_left -= prev_tick - tick; 679 prev_tick = tick; 680 } 681#ifdef DELAYDEBUG 682 if (state == 1) 683 printf(" %d calls to getit() at %d usec each\n", 684 getit_calls, (n + 5) / getit_calls); 685#endif 686} 687 688getit(unit, timer) { 689 int high; 690 int low; 691 692 /* 693 * XXX - isa.h defines bogus timers. There's no such timer as 694 * IO_TIMER_2 = 0x48. There's a timer in the CMOS RAM chip but 695 * its interface is quite different. Neither timer is an 8252. 696 * We actually only call this with unit = 0 and timer = 0. It 697 * could be static... 698 */ 699 /* 700 * Protect ourself against interrupts. 701 * XXX - sysbeep() and sysbeepstop() need protection. 702 */ 703 disable_intr(); 704 /* 705 * Latch the count for 'timer' (cc00xxxx, c = counter, x = any). 706 */ 707 outb(IO_TIMER1 + 3, timer << 6); 708 709 low = inb(IO_TIMER1 + timer); 710 high = inb(IO_TIMER1 + timer); 711 enable_intr(); 712 return ((high << 8) | low); 713} 714 715static beeping; 716static 717sysbeepstop(f) 718{ 719 /* disable counter 2 */ 720 outb(0x61, inb(0x61) & 0xFC); 721 if (f) 722 timeout(sysbeepstop, 0, f); 723 else 724 beeping = 0; 725} 726 727void sysbeep(int pitch, int period) 728{ 729 730 outb(0x61, inb(0x61) | 3); /* enable counter 2 */ 731 /* 732 * XXX - move timer stuff to clock.c. 733 * Program counter 2: 734 * ccaammmb, c counter, a = access, m = mode, b = BCD 735 * 1011x110, 11 for aa = LSB then MSB, x11 for mmm = square wave. 736 */ 737 outb(0x43, 0xb6); /* set command for counter 2, 2 byte write */ 738 739 outb(0x42, pitch); 740 outb(0x42, (pitch>>8)); 741 742 if (!beeping) { 743 beeping = period; 744 timeout(sysbeepstop, period/2, period); 745 } 746} 747 748/* 749 * Pass command to keyboard controller (8042) 750 */ 751unsigned kbc_8042cmd(val) { 752 753 while (inb(KBSTATP)&KBS_IBF); 754 if (val) outb(KBCMDP, val); 755 while (inb(KBSTATP)&KBS_IBF); 756 return (inb(KBDATAP)); 757} 758 759/* 760 * find an ISA device in a given isa_devtab_* table, given 761 * the table to search, the expected id_driver entry, and the unit number. 762 * 763 * this function is defined in isa_device.h, and this location is debatable; 764 * i put it there because it's useless w/o, and directly operates on 765 * the other stuff in that file. 766 * 767 */ 768 769struct isa_device *find_isadev(table, driverp, unit) 770 struct isa_device *table; 771 struct isa_driver *driverp; 772 int unit; 773{ 774 if (driverp == NULL) /* sanity check */ 775 return NULL; 776 777 while ((table->id_driver != driverp) || (table->id_unit != unit)) { 778 if (table->id_driver == 0) 779 return NULL; 780 781 table++; 782 } 783 784 return table; 785} 786 787/* 788 * Return nonzero if a (masked) irq is pending for a given device. 789 */ 790int 791isa_irq_pending(dvp) 792 struct isa_device *dvp; 793{ 794 unsigned id_irq; 795 796 id_irq = (unsigned short) dvp->id_irq; /* XXX silly type in struct */ 797 if (id_irq & 0xff) 798 return (inb(IO_ICU1) & id_irq); 799 return (inb(IO_ICU2) & (id_irq >> 8)); 800} 801