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