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