isa.c revision 33108
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.109 1998/01/31 03:29:00 eivind 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 "opt_diagnostic.h" 51 52#include <sys/param.h> 53#include <sys/systm.h> 54#include <sys/buf.h> 55#include <sys/malloc.h> 56#include <machine/ipl.h> 57#include <machine/md_var.h> 58#ifdef APIC_IO 59#include <machine/smp.h> 60#endif /* APIC_IO */ 61#include <vm/vm.h> 62#include <vm/vm_param.h> 63#include <vm/pmap.h> 64#include <i386/isa/isa_device.h> 65#include <i386/isa/intr_machdep.h> 66#include <i386/isa/isa.h> 67#include <i386/isa/ic/i8237.h> 68 69#include <sys/interrupt.h> 70 71#include "pnp.h" 72#if NPNP > 0 73#include <i386/isa/pnp.h> 74#endif 75 76/* 77** Register definitions for DMA controller 1 (channels 0..3): 78*/ 79#define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */ 80#define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */ 81#define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */ 82#define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */ 83 84/* 85** Register definitions for DMA controller 2 (channels 4..7): 86*/ 87#define DMA2_CHN(c) (IO_DMA2 + 2*(2*(c))) /* addr reg for channel c */ 88#define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */ 89#define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */ 90#define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */ 91 92static void config_isadev __P((struct isa_device *isdp, u_int *mp)); 93static void config_isadev_c __P((struct isa_device *isdp, u_int *mp, 94 int reconfig)); 95static void conflict __P((struct isa_device *dvp, struct isa_device *tmpdvp, 96 int item, char const *whatnot, char const *reason, 97 char const *format)); 98static int haveseen __P((struct isa_device *dvp, struct isa_device *tmpdvp, 99 u_int checkbits)); 100static int isa_dmarangecheck __P((caddr_t va, u_int length, int chan)); 101 102/* 103 * print a conflict message 104 */ 105static void 106conflict(dvp, tmpdvp, item, whatnot, reason, format) 107 struct isa_device *dvp; 108 struct isa_device *tmpdvp; 109 int item; 110 char const *whatnot; 111 char const *reason; 112 char const *format; 113{ 114 printf("%s%d not %sed due to %s conflict with %s%d at ", 115 dvp->id_driver->name, dvp->id_unit, whatnot, reason, 116 tmpdvp->id_driver->name, tmpdvp->id_unit); 117 printf(format, item); 118 printf("\n"); 119} 120 121/* 122 * Check to see if things are already in use, like IRQ's, I/O addresses 123 * and Memory addresses. 124 */ 125static int 126haveseen(dvp, tmpdvp, checkbits) 127 struct isa_device *dvp; 128 struct isa_device *tmpdvp; 129 u_int checkbits; 130{ 131 /* 132 * Ignore all conflicts except IRQ ones if conflicts are allowed. 133 */ 134 if (dvp->id_conflicts) 135 checkbits &= ~(CC_DRQ | CC_IOADDR | CC_MEMADDR); 136 /* 137 * Only check against devices that have already been found. 138 */ 139 if (tmpdvp->id_alive) { 140 char const *whatnot; 141 142 /* 143 * Check for device driver & unit conflict; just drop probing 144 * a device which has already probed true. This is usually 145 * not strictly a conflict, but rather the case of somebody 146 * having specified several mutually exclusive configurations 147 * for a single device. 148 */ 149 if (tmpdvp->id_driver == dvp->id_driver && 150 tmpdvp->id_unit == dvp->id_unit) { 151 return 1; 152 } 153 154 whatnot = checkbits & CC_ATTACH ? "attach" : "prob"; 155 /* 156 * Check for I/O address conflict. We can only check the 157 * starting address of the device against the range of the 158 * device that has already been probed since we do not 159 * know how many I/O addresses this device uses. 160 */ 161 if (checkbits & CC_IOADDR && tmpdvp->id_alive != -1) { 162 if ((dvp->id_iobase >= tmpdvp->id_iobase) && 163 (dvp->id_iobase <= 164 (tmpdvp->id_iobase + tmpdvp->id_alive - 1))) { 165 conflict(dvp, tmpdvp, dvp->id_iobase, whatnot, 166 "I/O address", "0x%x"); 167 return 1; 168 } 169 } 170 /* 171 * Check for Memory address conflict. We can check for 172 * range overlap, but it will not catch all cases since the 173 * driver may adjust the msize paramater during probe, for 174 * now we just check that the starting address does not 175 * fall within any allocated region. 176 * XXX could add a second check after the probe for overlap, 177 * since at that time we would know the full range. 178 * XXX KERNBASE is a hack, we should have vaddr in the table! 179 */ 180 if (checkbits & CC_MEMADDR && tmpdvp->id_maddr) { 181 if ((KERNBASE + dvp->id_maddr >= tmpdvp->id_maddr) && 182 (KERNBASE + dvp->id_maddr <= 183 (tmpdvp->id_maddr + tmpdvp->id_msize - 1))) { 184 conflict(dvp, tmpdvp, (int)dvp->id_maddr, 185 whatnot, "maddr", "0x%x"); 186 return 1; 187 } 188 } 189 /* 190 * Check for IRQ conflicts. 191 */ 192 if (checkbits & CC_IRQ && tmpdvp->id_irq) { 193 if (tmpdvp->id_irq == dvp->id_irq) { 194 conflict(dvp, tmpdvp, ffs(dvp->id_irq) - 1, 195 whatnot, "irq", "%d"); 196 return 1; 197 } 198 } 199 /* 200 * Check for DRQ conflicts. 201 */ 202 if (checkbits & CC_DRQ && tmpdvp->id_drq != -1) { 203 if (tmpdvp->id_drq == dvp->id_drq) { 204 conflict(dvp, tmpdvp, dvp->id_drq, whatnot, 205 "drq", "%d"); 206 return 1; 207 } 208 } 209 } 210 return 0; 211} 212 213#ifdef RESOURCE_CHECK 214#include <sys/drvresource.h> 215 216static int 217checkone (struct isa_device *dvp, int type, addr_t low, addr_t high, 218 char *resname, char *resfmt, int attaching) 219{ 220 int result = 0; 221 if (bootverbose) { 222 if (low == high) 223 printf("\tcheck %s: 0x%x\n", resname, low); 224 else 225 printf("\tcheck %s: 0x%x to 0x%x\n", 226 resname, low, high); 227 } 228 if (resource_check(type, RESF_NONE, low, high) != NULL) { 229 char *whatnot = attaching ? "attach" : "prob"; 230 static struct isa_device dummydev; 231 static struct isa_driver dummydrv; 232 struct isa_device *tmpdvp = &dummydev; 233 234 dummydev.id_driver = &dummydrv; 235 dummydev.id_unit = 0; 236 dummydrv.name = "pci"; 237 conflict(dvp, tmpdvp, low, whatnot, resname, resfmt); 238 result = 1; 239 } else if (attaching) { 240 if (low == high) 241 printf("\tregister %s: 0x%x\n", resname, low); 242 else 243 printf("\tregister %s: 0x%x to 0x%x\n", 244 resname, low, high); 245 resource_claim(dvp, type, RESF_NONE, low, high); 246 } 247 return (result); 248} 249 250static int 251check_pciconflict(struct isa_device *dvp, int checkbits) 252{ 253 int result = 0; 254 int attaching = (checkbits & CC_ATTACH) != 0; 255 256 if (checkbits & CC_MEMADDR) { 257 long maddr = dvp->id_maddr; 258 long msize = dvp->id_msize; 259 if (msize > 0) { 260 if (checkone(dvp, REST_MEM, maddr, maddr + msize - 1, 261 "maddr", "0x%x", attaching) != 0) { 262 result = 1; 263 attaching = 0; 264 } 265 } 266 } 267 if (checkbits & CC_IOADDR) { 268 unsigned iobase = dvp->id_iobase; 269 unsigned iosize = dvp->id_alive; 270 if (iosize == -1) 271 iosize = 1; /* XXX can't do much about this ... */ 272 if (iosize > 0) { 273 if (checkone(dvp, REST_PORT, iobase, iobase + iosize -1, 274 "I/O address", "0x%x", attaching) != 0) { 275 result = 1; 276 attaching = 0; 277 } 278 } 279 } 280 if (checkbits & CC_IRQ) { 281 int irq = ffs(dvp->id_irq) - 1; 282 if (irq >= 0) { 283 if (checkone(dvp, REST_INT, irq, irq, 284 "irq", "%d", attaching) != 0) { 285 result = 1; 286 attaching = 0; 287 } 288 } 289 } 290 if (checkbits & CC_DRQ) { 291 int drq = dvp->id_drq; 292 if (drq >= 0) { 293 if (checkone(dvp, REST_DMA, drq, drq, 294 "drq", "%d", attaching) != 0) { 295 result = 1; 296 attaching = 0; 297 } 298 } 299 } 300 if (result != 0) 301 resource_free (dvp); 302 return (result); 303} 304#endif /* RESOURCE_CHECK */ 305 306/* 307 * Search through all the isa_devtab_* tables looking for anything that 308 * conflicts with the current device. 309 */ 310int 311haveseen_isadev(dvp, checkbits) 312 struct isa_device *dvp; 313 u_int checkbits; 314{ 315#if NPNP > 0 316 struct pnp_dlist_node *nod; 317#endif 318 struct isa_device *tmpdvp; 319 int status = 0; 320 321 for (tmpdvp = isa_devtab_tty; tmpdvp->id_driver; tmpdvp++) { 322 status |= haveseen(dvp, tmpdvp, checkbits); 323 if (status) 324 return status; 325 } 326 for (tmpdvp = isa_devtab_bio; tmpdvp->id_driver; tmpdvp++) { 327 status |= haveseen(dvp, tmpdvp, checkbits); 328 if (status) 329 return status; 330 } 331 for (tmpdvp = isa_devtab_net; tmpdvp->id_driver; tmpdvp++) { 332 status |= haveseen(dvp, tmpdvp, checkbits); 333 if (status) 334 return status; 335 } 336 for (tmpdvp = isa_devtab_cam; tmpdvp->id_driver; tmpdvp++) { 337 status |= haveseen(dvp, tmpdvp, checkbits); 338 if (status) 339 return status; 340 } 341 for (tmpdvp = isa_devtab_null; tmpdvp->id_driver; tmpdvp++) { 342 status |= haveseen(dvp, tmpdvp, checkbits); 343 if (status) 344 return status; 345 } 346#if NPNP > 0 347 for (nod = pnp_device_list; nod != NULL; nod = nod->next) 348 if (status |= haveseen(dvp, &(nod->dev), checkbits)) 349 return status; 350#endif 351#ifdef RESOURCE_CHECK 352 if (!dvp->id_conflicts) 353 status = check_pciconflict(dvp, checkbits); 354 else if (bootverbose) 355 printf("\tnot checking for resource conflicts ...\n"); 356#endif /* RESOURCE_CHECK */ 357 return(status); 358} 359 360/* 361 * Configure all ISA devices 362 */ 363void 364isa_configure() 365{ 366 struct isa_device *dvp; 367 368 printf("Probing for devices on the ISA bus:\n"); 369 /* First probe all the sensitive probes */ 370 for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 371 if (dvp->id_driver->sensitive_hw) 372 config_isadev(dvp, &tty_imask); 373 for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 374 if (dvp->id_driver->sensitive_hw) 375 config_isadev(dvp, &bio_imask); 376 for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 377 if (dvp->id_driver->sensitive_hw) 378 config_isadev(dvp, &net_imask); 379 for (dvp = isa_devtab_cam; dvp->id_driver; dvp++) 380 if (dvp->id_driver->sensitive_hw) 381 config_isadev(dvp, &cam_imask); 382 for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 383 if (dvp->id_driver->sensitive_hw) 384 config_isadev(dvp, (u_int *)NULL); 385 386 /* Then all the bad ones */ 387 for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 388 if (!dvp->id_driver->sensitive_hw) 389 config_isadev(dvp, &tty_imask); 390 for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 391 if (!dvp->id_driver->sensitive_hw) 392 config_isadev(dvp, &bio_imask); 393 for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 394 if (!dvp->id_driver->sensitive_hw) 395 config_isadev(dvp, &net_imask); 396 for (dvp = isa_devtab_cam; dvp->id_driver; dvp++) 397 if (!dvp->id_driver->sensitive_hw) 398 config_isadev(dvp, &cam_imask); 399 for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 400 if (!dvp->id_driver->sensitive_hw) 401 config_isadev(dvp, (u_int *)NULL); 402 403 bio_imask |= SWI_CLOCK_MASK; 404 net_imask |= SWI_NET_MASK; 405 tty_imask |= SWI_TTY_MASK; 406 407/* 408 * XXX we should really add the tty device to net_imask when the line is 409 * switched to SLIPDISC, and then remove it when it is switched away from 410 * SLIPDISC. No need to block out ALL ttys during a splimp when only one 411 * of them is running slip. 412 * 413 * XXX actually, blocking all ttys during a splimp doesn't matter so much 414 * with sio because the serial interrupt layer doesn't use tty_imask. Only 415 * non-serial ttys suffer. It's more stupid that ALL 'net's are blocked 416 * during spltty. 417 */ 418#include "sl.h" 419#if NSL > 0 420 net_imask |= tty_imask; 421 tty_imask = net_imask; 422#endif 423 424 /* bio_imask |= tty_imask ; can some tty devices use buffers? */ 425 426 if (bootverbose) 427 printf("imasks: bio %x, tty %x, net %x\n", 428 bio_imask, tty_imask, net_imask); 429 430 /* 431 * Finish initializing intr_mask[]. Note that the partly 432 * constructed masks aren't actually used since we're at splhigh. 433 * For fully dynamic initialization, register_intr() and 434 * icu_unset() will have to adjust the masks for _all_ 435 * interrupts and for tty_imask, etc. 436 */ 437 for (dvp = isa_devtab_tty; dvp->id_driver; dvp++) 438 register_imask(dvp, tty_imask); 439 for (dvp = isa_devtab_bio; dvp->id_driver; dvp++) 440 register_imask(dvp, bio_imask); 441 for (dvp = isa_devtab_net; dvp->id_driver; dvp++) 442 register_imask(dvp, net_imask); 443 for (dvp = isa_devtab_cam; dvp->id_driver; dvp++) 444 register_imask(dvp, cam_imask); 445 for (dvp = isa_devtab_null; dvp->id_driver; dvp++) 446 register_imask(dvp, SWI_CLOCK_MASK); 447} 448 449/* 450 * Configure an ISA device. 451 */ 452static void 453config_isadev(isdp, mp) 454 struct isa_device *isdp; 455 u_int *mp; 456{ 457 config_isadev_c(isdp, mp, 0); 458} 459 460void 461reconfig_isadev(isdp, mp) 462 struct isa_device *isdp; 463 u_int *mp; 464{ 465 config_isadev_c(isdp, mp, 1); 466} 467 468static void 469config_isadev_c(isdp, mp, reconfig) 470 struct isa_device *isdp; 471 u_int *mp; 472 int reconfig; 473{ 474 u_int checkbits; 475 int id_alive; 476 int last_alive; 477 struct isa_driver *dp = isdp->id_driver; 478 479 if (!isdp->id_enabled) { 480 if (bootverbose) 481 printf("%s%d: disabled, not probed.\n", dp->name, isdp->id_unit); 482 return; 483 } 484 checkbits = CC_DRQ | CC_IOADDR | CC_MEMADDR; 485 if (!reconfig && haveseen_isadev(isdp, checkbits)) 486 return; 487 if (!reconfig && isdp->id_maddr) { 488 isdp->id_maddr -= ISA_HOLE_START; 489 isdp->id_maddr += atdevbase; 490 } 491 if (reconfig) { 492 last_alive = isdp->id_alive; 493 isdp->id_reconfig = 1; 494 } 495 else { 496 last_alive = 0; 497 isdp->id_reconfig = 0; 498 } 499 id_alive = (*dp->probe)(isdp); 500 if (id_alive) { 501 /* 502 * Only print the I/O address range if id_alive != -1 503 * Right now this is a temporary fix just for the new 504 * NPX code so that if it finds a 486 that can use trap 505 * 16 it will not report I/O addresses. 506 * Rod Grimes 04/26/94 507 */ 508 if (!isdp->id_reconfig) { 509 printf("%s%d", dp->name, isdp->id_unit); 510 if (id_alive != -1) { 511 if (isdp->id_iobase == -1) 512 printf(" at ?"); 513 else { 514 printf(" at 0x%x", isdp->id_iobase); 515 if (isdp->id_iobase + id_alive - 1 != 516 isdp->id_iobase) { 517 printf("-0x%x", 518 isdp->id_iobase + id_alive - 1); 519 } 520 } 521 } 522 if (isdp->id_irq) 523 printf(" irq %d", ffs(isdp->id_irq) - 1); 524 if (isdp->id_drq != -1) 525 printf(" drq %d", isdp->id_drq); 526 if (isdp->id_maddr) 527 printf(" maddr 0x%lx", kvtop(isdp->id_maddr)); 528 if (isdp->id_msize) 529 printf(" msize %d", isdp->id_msize); 530 if (isdp->id_flags) 531 printf(" flags 0x%x", isdp->id_flags); 532 if (isdp->id_iobase && !(isdp->id_iobase & 0xf300)) { 533 printf(" on motherboard"); 534 } else if (isdp->id_iobase >= 0x1000 && 535 !(isdp->id_iobase & 0x300)) { 536 printf (" on eisa slot %d", 537 isdp->id_iobase >> 12); 538 } else { 539 printf (" on isa"); 540 } 541 printf("\n"); 542 /* 543 * Check for conflicts again. The driver may have 544 * changed *dvp. We should weaken the early check 545 * since the driver may have been able to change 546 * *dvp to avoid conflicts if given a chance. We 547 * already skip the early check for IRQs and force 548 * a check for IRQs in the next group of checks. 549 */ 550 checkbits |= CC_IRQ; 551 if (haveseen_isadev(isdp, checkbits)) 552 return; 553 isdp->id_alive = id_alive; 554 } 555 (*dp->attach)(isdp); 556 if (isdp->id_irq) { 557#ifdef APIC_IO 558 /* 559 * Some motherboards use upper IRQs for traditional 560 * ISA INTerrupt sources. In particular we have 561 * seen the secondary IDE connected to IRQ20. 562 * This code detects and fixes this situation. 563 */ 564 u_int apic_mask; 565 int rirq; 566 567 apic_mask = isa_apic_mask(isdp->id_irq); 568 if (apic_mask != isdp->id_irq) { 569 rirq = ffs(isdp->id_irq) - 1; 570 isdp->id_irq = apic_mask; 571 undirect_isa_irq(rirq); /* free for ISA */ 572 } 573#endif /* APIC_IO */ 574 register_intr(ffs(isdp->id_irq) - 1, isdp->id_id, 575 isdp->id_ri_flags, isdp->id_intr, 576 mp, isdp->id_unit); 577 } 578 } else { 579 if (isdp->id_reconfig) { 580 (*dp->attach)(isdp); /* reconfiguration attach */ 581 } 582 if (!last_alive) { 583 if (!isdp->id_reconfig) { 584 printf("%s%d not found", 585 dp->name, isdp->id_unit); 586 if (isdp->id_iobase != -1) 587 printf(" at 0x%x", isdp->id_iobase); 588 printf("\n"); 589 } 590 } else { 591#if 0 592 /* This code has not been tested.... */ 593 if (isdp->id_irq) { 594 icu_unset(ffs(isdp->id_irq) - 1, 595 isdp->id_intr); 596 if (mp) 597 INTRUNMASK(*mp, isdp->id_irq); 598 } 599#else 600 printf ("icu_unset() not supported here ...\n"); 601#endif 602 } 603 } 604} 605 606static caddr_t dma_bouncebuf[8]; 607static u_int dma_bouncebufsize[8]; 608static u_int8_t dma_bounced = 0; 609static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */ 610static u_int8_t dma_inuse = 0; /* User for acquire/release */ 611static u_int8_t dma_auto_mode = 0; 612 613#define VALID_DMA_MASK (7) 614 615/* high byte of address is stored in this port for i-th dma channel */ 616static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a }; 617 618/* 619 * Setup a DMA channel's bounce buffer. 620 */ 621void 622isa_dmainit(chan, bouncebufsize) 623 int chan; 624 u_int bouncebufsize; 625{ 626 void *buf; 627 628#ifdef DIAGNOSTIC 629 if (chan & ~VALID_DMA_MASK) 630 panic("isa_dmainit: channel out of range"); 631 632 if (dma_bouncebuf[chan] != NULL) 633 panic("isa_dmainit: impossible request"); 634#endif 635 636 dma_bouncebufsize[chan] = bouncebufsize; 637 638 /* Try malloc() first. It works better if it works. */ 639 buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT); 640 if (buf != NULL) { 641 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) { 642 dma_bouncebuf[chan] = buf; 643 return; 644 } 645 free(buf, M_DEVBUF); 646 } 647 buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful, 648 1ul, chan & 4 ? 0x20000ul : 0x10000ul); 649 if (buf == NULL) 650 printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize); 651 else 652 dma_bouncebuf[chan] = buf; 653} 654 655/* 656 * Register a DMA channel's usage. Usually called from a device driver 657 * in open() or during it's initialization. 658 */ 659int 660isa_dma_acquire(chan) 661 int chan; 662{ 663#ifdef DIAGNOSTIC 664 if (chan & ~VALID_DMA_MASK) 665 panic("isa_dma_acquire: channel out of range"); 666#endif 667 668 if (dma_inuse & (1 << chan)) { 669 printf("isa_dma_acquire: channel %d already in use\n", chan); 670 return (EBUSY); 671 } 672 dma_inuse |= (1 << chan); 673 dma_auto_mode &= ~(1 << chan); 674 675 return (0); 676} 677 678/* 679 * Unregister a DMA channel's usage. Usually called from a device driver 680 * during close() or during it's shutdown. 681 */ 682void 683isa_dma_release(chan) 684 int chan; 685{ 686#ifdef DIAGNOSTIC 687 if (chan & ~VALID_DMA_MASK) 688 panic("isa_dma_release: channel out of range"); 689 690 if ((dma_inuse & (1 << chan)) == 0) 691 printf("isa_dma_release: channel %d not in use\n", chan); 692#endif 693 694 if (dma_busy & (1 << chan)) { 695 dma_busy &= ~(1 << chan); 696 /* 697 * XXX We should also do "dma_bounced &= (1 << chan);" 698 * because we are acting on behalf of isa_dmadone() which 699 * was not called to end the last DMA operation. This does 700 * not matter now, but it may in the future. 701 */ 702 } 703 704 dma_inuse &= ~(1 << chan); 705 dma_auto_mode &= ~(1 << chan); 706} 707 708/* 709 * isa_dmacascade(): program 8237 DMA controller channel to accept 710 * external dma control by a board. 711 */ 712void 713isa_dmacascade(chan) 714 int chan; 715{ 716#ifdef DIAGNOSTIC 717 if (chan & ~VALID_DMA_MASK) 718 panic("isa_dmacascade: channel out of range"); 719#endif 720 721 /* set dma channel mode, and set dma channel mode */ 722 if ((chan & 4) == 0) { 723 outb(DMA1_MODE, DMA37MD_CASCADE | chan); 724 outb(DMA1_SMSK, chan); 725 } else { 726 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3)); 727 outb(DMA2_SMSK, chan & 3); 728 } 729} 730 731/* 732 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment 733 * problems by using a bounce buffer. 734 */ 735void 736isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan) 737{ 738 vm_offset_t phys; 739 int waport; 740 caddr_t newaddr; 741 742#ifdef DIAGNOSTIC 743 if (chan & ~VALID_DMA_MASK) 744 panic("isa_dmastart: channel out of range"); 745 746 if ((chan < 4 && nbytes > (1<<16)) 747 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1))) 748 panic("isa_dmastart: impossible request"); 749 750 if ((dma_inuse & (1 << chan)) == 0) 751 printf("isa_dmastart: channel %d not acquired\n", chan); 752#endif 753 754#if 0 755 /* 756 * XXX This should be checked, but drivers like ad1848 only call 757 * isa_dmastart() once because they use Auto DMA mode. If we 758 * leave this in, drivers that do this will print this continuously. 759 */ 760 if (dma_busy & (1 << chan)) 761 printf("isa_dmastart: channel %d busy\n", chan); 762#endif 763 764 dma_busy |= (1 << chan); 765 766 if (isa_dmarangecheck(addr, nbytes, chan)) { 767 if (dma_bouncebuf[chan] == NULL 768 || dma_bouncebufsize[chan] < nbytes) 769 panic("isa_dmastart: bad bounce buffer"); 770 dma_bounced |= (1 << chan); 771 newaddr = dma_bouncebuf[chan]; 772 773 /* copy bounce buffer on write */ 774 if (!(flags & B_READ)) 775 bcopy(addr, newaddr, nbytes); 776 addr = newaddr; 777 } 778 779 /* translate to physical */ 780 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr); 781 782 if (flags & B_RAW) { 783 dma_auto_mode |= (1 << chan); 784 } else { 785 dma_auto_mode &= ~(1 << chan); 786 } 787 788 if ((chan & 4) == 0) { 789 /* 790 * Program one of DMA channels 0..3. These are 791 * byte mode channels. 792 */ 793 /* set dma channel mode, and reset address ff */ 794 795 /* If B_RAW flag is set, then use autoinitialise mode */ 796 if (flags & B_RAW) { 797 if (flags & B_READ) 798 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan); 799 else 800 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan); 801 } 802 else 803 if (flags & B_READ) 804 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan); 805 else 806 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan); 807 outb(DMA1_FFC, 0); 808 809 /* send start address */ 810 waport = DMA1_CHN(chan); 811 outb(waport, phys); 812 outb(waport, phys>>8); 813 outb(dmapageport[chan], phys>>16); 814 815 /* send count */ 816 outb(waport + 1, --nbytes); 817 outb(waport + 1, nbytes>>8); 818 819 /* unmask channel */ 820 outb(DMA1_SMSK, chan); 821 } else { 822 /* 823 * Program one of DMA channels 4..7. These are 824 * word mode channels. 825 */ 826 /* set dma channel mode, and reset address ff */ 827 828 /* If B_RAW flag is set, then use autoinitialise mode */ 829 if (flags & B_RAW) { 830 if (flags & B_READ) 831 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3)); 832 else 833 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3)); 834 } 835 else 836 if (flags & B_READ) 837 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3)); 838 else 839 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3)); 840 outb(DMA2_FFC, 0); 841 842 /* send start address */ 843 waport = DMA2_CHN(chan - 4); 844 outb(waport, phys>>1); 845 outb(waport, phys>>9); 846 outb(dmapageport[chan], phys>>16); 847 848 /* send count */ 849 nbytes >>= 1; 850 outb(waport + 2, --nbytes); 851 outb(waport + 2, nbytes>>8); 852 853 /* unmask channel */ 854 outb(DMA2_SMSK, chan & 3); 855 } 856} 857 858void 859isa_dmadone(int flags, caddr_t addr, int nbytes, int chan) 860{ 861#ifdef DIAGNOSTIC 862 if (chan & ~VALID_DMA_MASK) 863 panic("isa_dmadone: channel out of range"); 864 865 if ((dma_inuse & (1 << chan)) == 0) 866 printf("isa_dmadone: channel %d not acquired\n", chan); 867#endif 868 869 if (((dma_busy & (1 << chan)) == 0) && 870 (dma_auto_mode & (1 << chan)) == 0 ) 871 printf("isa_dmadone: channel %d not busy\n", chan); 872 873 874 if (dma_bounced & (1 << chan)) { 875 /* copy bounce buffer on read */ 876 if (flags & B_READ) 877 bcopy(dma_bouncebuf[chan], addr, nbytes); 878 879 dma_bounced &= ~(1 << chan); 880 } 881 dma_busy &= ~(1 << chan); 882} 883 884/* 885 * Check for problems with the address range of a DMA transfer 886 * (non-contiguous physical pages, outside of bus address space, 887 * crossing DMA page boundaries). 888 * Return true if special handling needed. 889 */ 890 891static int 892isa_dmarangecheck(caddr_t va, u_int length, int chan) 893{ 894 vm_offset_t phys, priorpage = 0, endva; 895 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1); 896 897 endva = (vm_offset_t)round_page(va + length); 898 for (; va < (caddr_t) endva ; va += PAGE_SIZE) { 899 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va)); 900#define ISARAM_END RAM_END 901 if (phys == 0) 902 panic("isa_dmacheck: no physical page present"); 903 if (phys >= ISARAM_END) 904 return (1); 905 if (priorpage) { 906 if (priorpage + PAGE_SIZE != phys) 907 return (1); 908 /* check if crossing a DMA page boundary */ 909 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk) 910 return (1); 911 } 912 priorpage = phys; 913 } 914 return (0); 915} 916 917/* 918 * Query the progress of a transfer on a DMA channel. 919 * 920 * To avoid having to interrupt a transfer in progress, we sample 921 * each of the high and low databytes twice, and apply the following 922 * logic to determine the correct count. 923 * 924 * Reads are performed with interrupts disabled, thus it is to be 925 * expected that the time between reads is very small. At most 926 * one rollover in the low count byte can be expected within the 927 * four reads that are performed. 928 * 929 * There are three gaps in which a rollover can occur : 930 * 931 * - read low1 932 * gap1 933 * - read high1 934 * gap2 935 * - read low2 936 * gap3 937 * - read high2 938 * 939 * If a rollover occurs in gap1 or gap2, the low2 value will be 940 * greater than the low1 value. In this case, low2 and high2 are a 941 * corresponding pair. 942 * 943 * In any other case, low1 and high1 can be considered to be correct. 944 * 945 * The function returns the number of bytes remaining in the transfer, 946 * or -1 if the channel requested is not active. 947 * 948 */ 949int 950isa_dmastatus(int chan) 951{ 952 u_long cnt = 0; 953 int ffport, waport; 954 u_long low1, high1, low2, high2; 955 956 /* channel active? */ 957 if ((dma_inuse & (1 << chan)) == 0) { 958 printf("isa_dmastatus: channel %d not active\n", chan); 959 return(-1); 960 } 961 /* channel busy? */ 962 963 if (((dma_busy & (1 << chan)) == 0) && 964 (dma_auto_mode & (1 << chan)) == 0 ) { 965 printf("chan %d not busy\n", chan); 966 return -2 ; 967 } 968 if (chan < 4) { /* low DMA controller */ 969 ffport = DMA1_FFC; 970 waport = DMA1_CHN(chan) + 1; 971 } else { /* high DMA controller */ 972 ffport = DMA2_FFC; 973 waport = DMA2_CHN(chan - 4) + 2; 974 } 975 976 disable_intr(); /* no interrupts Mr Jones! */ 977 outb(ffport, 0); /* clear register LSB flipflop */ 978 low1 = inb(waport); 979 high1 = inb(waport); 980 outb(ffport, 0); /* clear again */ 981 low2 = inb(waport); 982 high2 = inb(waport); 983 enable_intr(); /* enable interrupts again */ 984 985 /* 986 * Now decide if a wrap has tried to skew our results. 987 * Note that after TC, the count will read 0xffff, while we want 988 * to return zero, so we add and then mask to compensate. 989 */ 990 if (low1 >= low2) { 991 cnt = (low1 + (high1 << 8) + 1) & 0xffff; 992 } else { 993 cnt = (low2 + (high2 << 8) + 1) & 0xffff; 994 } 995 996 if (chan >= 4) /* high channels move words */ 997 cnt *= 2; 998 return(cnt); 999} 1000 1001/* 1002 * Stop a DMA transfer currently in progress. 1003 */ 1004int 1005isa_dmastop(int chan) 1006{ 1007 if ((dma_inuse & (1 << chan)) == 0) 1008 printf("isa_dmastop: channel %d not acquired\n", chan); 1009 1010 if (((dma_busy & (1 << chan)) == 0) && 1011 ((dma_auto_mode & (1 << chan)) == 0)) { 1012 printf("chan %d not busy\n", chan); 1013 return -2 ; 1014 } 1015 1016 if ((chan & 4) == 0) { 1017 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */); 1018 } else { 1019 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */); 1020 } 1021 return(isa_dmastatus(chan)); 1022} 1023 1024/* 1025 * Find the highest priority enabled display device. Since we can't 1026 * distinguish display devices from ttys, depend on display devices 1027 * being sensitive and before sensitive non-display devices (if any) 1028 * in isa_devtab_tty. 1029 * 1030 * XXX we should add capability flags IAMDISPLAY and ISUPPORTCONSOLES. 1031 */ 1032struct isa_device * 1033find_display() 1034{ 1035 struct isa_device *dvp; 1036 1037 for (dvp = isa_devtab_tty; dvp->id_driver != NULL; dvp++) 1038 if (dvp->id_driver->sensitive_hw && dvp->id_enabled) 1039 return (dvp); 1040 return (NULL); 1041} 1042 1043/* 1044 * find an ISA device in a given isa_devtab_* table, given 1045 * the table to search, the expected id_driver entry, and the unit number. 1046 * 1047 * this function is defined in isa_device.h, and this location is debatable; 1048 * i put it there because it's useless w/o, and directly operates on 1049 * the other stuff in that file. 1050 * 1051 */ 1052 1053struct isa_device * 1054find_isadev(table, driverp, unit) 1055 struct isa_device *table; 1056 struct isa_driver *driverp; 1057 int unit; 1058{ 1059 if (driverp == NULL) /* sanity check */ 1060 return (NULL); 1061 1062 while ((table->id_driver != driverp) || (table->id_unit != unit)) { 1063 if (table->id_driver == 0) 1064 return NULL; 1065 1066 table++; 1067 } 1068 1069 return (table); 1070} 1071