cbus_dma.c revision 79008
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 * $FreeBSD: head/sys/pc98/cbus/cbus_dma.c 79008 2001-06-30 05:29:11Z imp $ 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#ifdef PC98 51#include "opt_pc98.h" 52#endif 53 54#include <sys/param.h> 55#include <sys/systm.h> 56#include <sys/bus.h> 57#include <sys/kernel.h> 58#include <sys/malloc.h> 59#include <sys/lock.h> 60#include <sys/mutex.h> 61#include <sys/module.h> 62#ifdef PC98 63#include <machine/md_var.h> 64#endif 65#include <vm/vm.h> 66#include <vm/vm_param.h> 67#include <vm/pmap.h> 68#ifdef PC98 69#include <pc98/pc98/pc98.h> 70#else 71#include <i386/isa/isa.h> 72#endif 73#include <dev/ic/i8237.h> 74#include <isa/isavar.h> 75 76/* 77** Register definitions for DMA controller 1 (channels 0..3): 78*/ 79#ifdef PC98 80#define DMA1_CHN(c) (IO_DMA + (4*(c))) /* addr reg for channel c */ 81#define DMA1_SMSK (IO_DMA + 0x14) /* single mask register */ 82#define DMA1_MODE (IO_DMA + 0x16) /* mode register */ 83#define DMA1_FFC (IO_DMA + 0x18) /* clear first/last FF */ 84#else 85#define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */ 86#define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */ 87#define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */ 88#define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */ 89#endif 90 91/* 92** Register definitions for DMA controller 2 (channels 4..7): 93*/ 94#define DMA2_CHN(c) (IO_DMA2 + 2*(2*(c))) /* addr reg for channel c */ 95#define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */ 96#define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */ 97#define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */ 98 99static int isa_dmarangecheck __P((caddr_t va, u_int length, int chan)); 100 101#ifdef PC98 102static caddr_t dma_bouncebuf[4]; 103static u_int dma_bouncebufsize[4]; 104#else 105static caddr_t dma_bouncebuf[8]; 106static u_int dma_bouncebufsize[8]; 107#endif 108static u_int8_t dma_bounced = 0; 109static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */ 110static u_int8_t dma_inuse = 0; /* User for acquire/release */ 111static u_int8_t dma_auto_mode = 0; 112 113#ifdef PC98 114#define VALID_DMA_MASK (3) 115#else 116#define VALID_DMA_MASK (7) 117#endif 118 119/* high byte of address is stored in this port for i-th dma channel */ 120#ifdef PC98 121static int dmapageport[4] = { 0x27, 0x21, 0x23, 0x25 }; 122#else 123static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a }; 124#endif 125 126/* 127 * Setup a DMA channel's bounce buffer. 128 */ 129void 130isa_dmainit(chan, bouncebufsize) 131 int chan; 132 u_int bouncebufsize; 133{ 134 void *buf; 135 136#ifdef DIAGNOSTIC 137 if (chan & ~VALID_DMA_MASK) 138 panic("isa_dmainit: channel out of range"); 139 140 if (dma_bouncebuf[chan] != NULL) 141 panic("isa_dmainit: impossible request"); 142#endif 143 144 dma_bouncebufsize[chan] = bouncebufsize; 145 146 /* Try malloc() first. It works better if it works. */ 147 buf = malloc(bouncebufsize, M_DEVBUF, M_NOWAIT); 148 if (buf != NULL) { 149 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) { 150 dma_bouncebuf[chan] = buf; 151 return; 152 } 153 free(buf, M_DEVBUF); 154 } 155 buf = contigmalloc(bouncebufsize, M_DEVBUF, M_NOWAIT, 0ul, 0xfffffful, 156 1ul, chan & 4 ? 0x20000ul : 0x10000ul); 157 if (buf == NULL) 158 printf("isa_dmainit(%d, %d) failed\n", chan, bouncebufsize); 159 else 160 dma_bouncebuf[chan] = buf; 161} 162 163/* 164 * Register a DMA channel's usage. Usually called from a device driver 165 * in open() or during its initialization. 166 */ 167int 168isa_dma_acquire(chan) 169 int chan; 170{ 171#ifdef DIAGNOSTIC 172 if (chan & ~VALID_DMA_MASK) 173 panic("isa_dma_acquire: channel out of range"); 174#endif 175 176 if (dma_inuse & (1 << chan)) { 177 printf("isa_dma_acquire: channel %d already in use\n", chan); 178 return (EBUSY); 179 } 180 dma_inuse |= (1 << chan); 181 dma_auto_mode &= ~(1 << chan); 182 183 return (0); 184} 185 186/* 187 * Unregister a DMA channel's usage. Usually called from a device driver 188 * during close() or during its shutdown. 189 */ 190void 191isa_dma_release(chan) 192 int chan; 193{ 194#ifdef DIAGNOSTIC 195 if (chan & ~VALID_DMA_MASK) 196 panic("isa_dma_release: channel out of range"); 197 198 if ((dma_inuse & (1 << chan)) == 0) 199 printf("isa_dma_release: channel %d not in use\n", chan); 200#endif 201 202 if (dma_busy & (1 << chan)) { 203 dma_busy &= ~(1 << chan); 204 /* 205 * XXX We should also do "dma_bounced &= (1 << chan);" 206 * because we are acting on behalf of isa_dmadone() which 207 * was not called to end the last DMA operation. This does 208 * not matter now, but it may in the future. 209 */ 210 } 211 212 dma_inuse &= ~(1 << chan); 213 dma_auto_mode &= ~(1 << chan); 214} 215 216#ifndef PC98 217/* 218 * isa_dmacascade(): program 8237 DMA controller channel to accept 219 * external dma control by a board. 220 */ 221void 222isa_dmacascade(chan) 223 int chan; 224{ 225#ifdef DIAGNOSTIC 226 if (chan & ~VALID_DMA_MASK) 227 panic("isa_dmacascade: channel out of range"); 228#endif 229 230 /* set dma channel mode, and set dma channel mode */ 231 if ((chan & 4) == 0) { 232 outb(DMA1_MODE, DMA37MD_CASCADE | chan); 233 outb(DMA1_SMSK, chan); 234 } else { 235 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3)); 236 outb(DMA2_SMSK, chan & 3); 237 } 238} 239#endif 240 241/* 242 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment 243 * problems by using a bounce buffer. 244 */ 245void 246isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan) 247{ 248 vm_offset_t phys; 249 int waport; 250 caddr_t newaddr; 251 252#ifdef DIAGNOSTIC 253 if (chan & ~VALID_DMA_MASK) 254 panic("isa_dmastart: channel out of range"); 255 256 if ((chan < 4 && nbytes > (1<<16)) 257 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1))) 258 panic("isa_dmastart: impossible request"); 259 260 if ((dma_inuse & (1 << chan)) == 0) 261 printf("isa_dmastart: channel %d not acquired\n", chan); 262#endif 263 264#if 0 265 /* 266 * XXX This should be checked, but drivers like ad1848 only call 267 * isa_dmastart() once because they use Auto DMA mode. If we 268 * leave this in, drivers that do this will print this continuously. 269 */ 270 if (dma_busy & (1 << chan)) 271 printf("isa_dmastart: channel %d busy\n", chan); 272#endif 273 274 dma_busy |= (1 << chan); 275 276 if (isa_dmarangecheck(addr, nbytes, chan)) { 277 if (dma_bouncebuf[chan] == NULL 278 || dma_bouncebufsize[chan] < nbytes) 279 panic("isa_dmastart: bad bounce buffer"); 280 dma_bounced |= (1 << chan); 281 newaddr = dma_bouncebuf[chan]; 282 283 /* copy bounce buffer on write */ 284 if (!(flags & ISADMA_READ)) 285 bcopy(addr, newaddr, nbytes); 286 addr = newaddr; 287 } 288 289 /* translate to physical */ 290 mtx_lock(&vm_mtx); /* 291 * XXX: need to hold for longer period to 292 * ensure that mappings don't change 293 */ 294 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr); 295 mtx_unlock(&vm_mtx); 296 297 if (flags & ISADMA_RAW) { 298 dma_auto_mode |= (1 << chan); 299 } else { 300 dma_auto_mode &= ~(1 << chan); 301 } 302 303#ifdef PC98 304 if (need_pre_dma_flush) 305 wbinvd(); /* wbinvd (WB cache flush) */ 306#endif 307 308#ifndef PC98 309 if ((chan & 4) == 0) { 310 /* 311 * Program one of DMA channels 0..3. These are 312 * byte mode channels. 313 */ 314#endif 315 /* set dma channel mode, and reset address ff */ 316 317 /* If ISADMA_RAW flag is set, then use autoinitialise mode */ 318 if (flags & ISADMA_RAW) { 319 if (flags & ISADMA_READ) 320 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan); 321 else 322 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan); 323 } 324 else 325 if (flags & ISADMA_READ) 326 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan); 327 else 328 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan); 329 outb(DMA1_FFC, 0); 330 331 /* send start address */ 332 waport = DMA1_CHN(chan); 333 outb(waport, phys); 334 outb(waport, phys>>8); 335 outb(dmapageport[chan], phys>>16); 336 337 /* send count */ 338#ifdef PC98 339 outb(waport + 2, --nbytes); 340 outb(waport + 2, nbytes>>8); 341#else 342 outb(waport + 1, --nbytes); 343 outb(waport + 1, nbytes>>8); 344#endif 345 346 /* unmask channel */ 347 outb(DMA1_SMSK, chan); 348#ifndef PC98 349 } else { 350 /* 351 * Program one of DMA channels 4..7. These are 352 * word mode channels. 353 */ 354 /* set dma channel mode, and reset address ff */ 355 356 /* If ISADMA_RAW flag is set, then use autoinitialise mode */ 357 if (flags & ISADMA_RAW) { 358 if (flags & ISADMA_READ) 359 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3)); 360 else 361 outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3)); 362 } 363 else 364 if (flags & ISADMA_READ) 365 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3)); 366 else 367 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3)); 368 outb(DMA2_FFC, 0); 369 370 /* send start address */ 371 waport = DMA2_CHN(chan - 4); 372 outb(waport, phys>>1); 373 outb(waport, phys>>9); 374 outb(dmapageport[chan], phys>>16); 375 376 /* send count */ 377 nbytes >>= 1; 378 outb(waport + 2, --nbytes); 379 outb(waport + 2, nbytes>>8); 380 381 /* unmask channel */ 382 outb(DMA2_SMSK, chan & 3); 383 } 384#endif 385} 386 387void 388isa_dmadone(int flags, caddr_t addr, int nbytes, int chan) 389{ 390#ifdef PC98 391 if (flags & ISADMA_READ) { 392 /* cache flush only after reading 92/12/9 by A.Kojima */ 393 if (need_post_dma_flush) 394 invd(); 395 } 396#endif 397 398#ifdef DIAGNOSTIC 399 if (chan & ~VALID_DMA_MASK) 400 panic("isa_dmadone: channel out of range"); 401 402 if ((dma_inuse & (1 << chan)) == 0) 403 printf("isa_dmadone: channel %d not acquired\n", chan); 404#endif 405 406 if (((dma_busy & (1 << chan)) == 0) && 407 (dma_auto_mode & (1 << chan)) == 0 ) 408 printf("isa_dmadone: channel %d not busy\n", chan); 409 410#ifdef PC98 411 if ((dma_auto_mode & (1 << chan)) == 0) 412 outb(DMA1_SMSK, (chan & 3) | 4); 413#else 414 if ((dma_auto_mode & (1 << chan)) == 0) 415 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4); 416#endif 417 418 if (dma_bounced & (1 << chan)) { 419 /* copy bounce buffer on read */ 420 if (flags & ISADMA_READ) 421 bcopy(dma_bouncebuf[chan], addr, nbytes); 422 423 dma_bounced &= ~(1 << chan); 424 } 425 dma_busy &= ~(1 << chan); 426} 427 428/* 429 * Check for problems with the address range of a DMA transfer 430 * (non-contiguous physical pages, outside of bus address space, 431 * crossing DMA page boundaries). 432 * Return true if special handling needed. 433 */ 434 435static int 436isa_dmarangecheck(caddr_t va, u_int length, int chan) 437{ 438 vm_offset_t phys, priorpage = 0, endva; 439 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1); 440 441 endva = (vm_offset_t)round_page((vm_offset_t)va + length); 442 for (; va < (caddr_t) endva ; va += PAGE_SIZE) { 443 mtx_lock(&vm_mtx); 444 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va)); 445 mtx_unlock(&vm_mtx); 446#ifdef EPSON_BOUNCEDMA 447#define ISARAM_END 0xf00000 448#else 449#define ISARAM_END RAM_END 450#endif 451 if (phys == 0) 452 panic("isa_dmacheck: no physical page present"); 453 if (phys >= ISARAM_END) 454 return (1); 455 if (priorpage) { 456 if (priorpage + PAGE_SIZE != phys) 457 return (1); 458 /* check if crossing a DMA page boundary */ 459 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk) 460 return (1); 461 } 462 priorpage = phys; 463 } 464 return (0); 465} 466 467/* 468 * Query the progress of a transfer on a DMA channel. 469 * 470 * To avoid having to interrupt a transfer in progress, we sample 471 * each of the high and low databytes twice, and apply the following 472 * logic to determine the correct count. 473 * 474 * Reads are performed with interrupts disabled, thus it is to be 475 * expected that the time between reads is very small. At most 476 * one rollover in the low count byte can be expected within the 477 * four reads that are performed. 478 * 479 * There are three gaps in which a rollover can occur : 480 * 481 * - read low1 482 * gap1 483 * - read high1 484 * gap2 485 * - read low2 486 * gap3 487 * - read high2 488 * 489 * If a rollover occurs in gap1 or gap2, the low2 value will be 490 * greater than the low1 value. In this case, low2 and high2 are a 491 * corresponding pair. 492 * 493 * In any other case, low1 and high1 can be considered to be correct. 494 * 495 * The function returns the number of bytes remaining in the transfer, 496 * or -1 if the channel requested is not active. 497 * 498 */ 499int 500isa_dmastatus(int chan) 501{ 502 u_long cnt = 0; 503 int ffport, waport; 504 u_long low1, high1, low2, high2; 505 506 /* channel active? */ 507 if ((dma_inuse & (1 << chan)) == 0) { 508 printf("isa_dmastatus: channel %d not active\n", chan); 509 return(-1); 510 } 511 /* channel busy? */ 512 513 if (((dma_busy & (1 << chan)) == 0) && 514 (dma_auto_mode & (1 << chan)) == 0 ) { 515 printf("chan %d not busy\n", chan); 516 return -2 ; 517 } 518#ifdef PC98 519 ffport = DMA1_FFC; 520 waport = DMA1_CHN(chan) + 2; 521#else 522 if (chan < 4) { /* low DMA controller */ 523 ffport = DMA1_FFC; 524 waport = DMA1_CHN(chan) + 1; 525 } else { /* high DMA controller */ 526 ffport = DMA2_FFC; 527 waport = DMA2_CHN(chan - 4) + 2; 528 } 529#endif 530 531 disable_intr(); /* no interrupts Mr Jones! */ 532 outb(ffport, 0); /* clear register LSB flipflop */ 533 low1 = inb(waport); 534 high1 = inb(waport); 535 outb(ffport, 0); /* clear again */ 536 low2 = inb(waport); 537 high2 = inb(waport); 538 enable_intr(); /* enable interrupts again */ 539 540 /* 541 * Now decide if a wrap has tried to skew our results. 542 * Note that after TC, the count will read 0xffff, while we want 543 * to return zero, so we add and then mask to compensate. 544 */ 545 if (low1 >= low2) { 546 cnt = (low1 + (high1 << 8) + 1) & 0xffff; 547 } else { 548 cnt = (low2 + (high2 << 8) + 1) & 0xffff; 549 } 550 551 if (chan >= 4) /* high channels move words */ 552 cnt *= 2; 553 return(cnt); 554} 555 556/* 557 * Stop a DMA transfer currently in progress. 558 */ 559int 560isa_dmastop(int chan) 561{ 562 if ((dma_inuse & (1 << chan)) == 0) 563 printf("isa_dmastop: channel %d not acquired\n", chan); 564 565 if (((dma_busy & (1 << chan)) == 0) && 566 ((dma_auto_mode & (1 << chan)) == 0)) { 567 printf("chan %d not busy\n", chan); 568 return -2 ; 569 } 570 571 if ((chan & 4) == 0) { 572 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */); 573 } else { 574#ifndef PC98 575 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */); 576#endif 577 } 578 return(isa_dmastatus(chan)); 579} 580 581/* 582 * Attach to the ISA PnP descriptor for the AT DMA controller 583 */ 584static struct isa_pnp_id atdma_ids[] = { 585 { 0x0002d041 /* PNP0200 */, "AT DMA controller" }, 586 { 0 } 587}; 588 589static int 590atdma_probe(device_t dev) 591{ 592 int result; 593 594 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0) 595 device_quiet(dev); 596 return(result); 597} 598 599static int 600atdma_attach(device_t dev) 601{ 602 return(0); 603} 604 605static device_method_t atdma_methods[] = { 606 /* Device interface */ 607 DEVMETHOD(device_probe, atdma_probe), 608 DEVMETHOD(device_attach, atdma_attach), 609 DEVMETHOD(device_detach, bus_generic_detach), 610 DEVMETHOD(device_shutdown, bus_generic_shutdown), 611 DEVMETHOD(device_suspend, bus_generic_suspend), 612 DEVMETHOD(device_resume, bus_generic_resume), 613 { 0, 0 } 614}; 615 616static driver_t atdma_driver = { 617 "atdma", 618 atdma_methods, 619 1, /* no softc */ 620}; 621 622static devclass_t atdma_devclass; 623 624DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0); 625