Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.45
      1 /*	$NetBSD: isadma.c,v 1.45 2000/06/28 16:27:55 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Device driver for the ISA on-board DMA controller.
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/proc.h>
     47 #include <sys/device.h>
     48 #include <sys/malloc.h>
     49 
     50 #include <machine/bus.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <dev/isa/isareg.h>
     55 #include <dev/isa/isavar.h>
     56 #include <dev/isa/isadmavar.h>
     57 #include <dev/isa/isadmareg.h>
     58 
     59 struct isa_mem *isa_mem_head;
     60 
     61 /*
     62  * High byte of DMA address is stored in this DMAPG register for
     63  * the Nth DMA channel.
     64  */
     65 static int dmapageport[2][4] = {
     66 	{0x7, 0x3, 0x1, 0x2},
     67 	{0xf, 0xb, 0x9, 0xa}
     68 };
     69 
     70 static u_int8_t dmamode[] = {
     71 	/* write to device/read from device */
     72 	DMA37MD_READ | DMA37MD_SINGLE,
     73 	DMA37MD_WRITE | DMA37MD_SINGLE,
     74 
     75 	/* write to device/read from device */
     76 	DMA37MD_READ | DMA37MD_DEMAND,
     77 	DMA37MD_WRITE | DMA37MD_DEMAND,
     78 
     79 	/* write to device/read from device - DMAMODE_LOOP */
     80 	DMA37MD_READ | DMA37MD_SINGLE | DMA37MD_LOOP,
     81 	DMA37MD_WRITE | DMA37MD_SINGLE | DMA37MD_LOOP,
     82 
     83 	/* write to device/read from device - DMAMODE_LOOPDEMAND */
     84 	DMA37MD_READ | DMA37MD_DEMAND | DMA37MD_LOOP,
     85 	DMA37MD_WRITE | DMA37MD_DEMAND | DMA37MD_LOOP,
     86 };
     87 
     88 static inline void _isa_dmaunmask __P((struct isa_dma_state *, int));
     89 static inline void _isa_dmamask __P((struct isa_dma_state *, int));
     90 
     91 static inline void
     92 _isa_dmaunmask(ids, chan)
     93 	struct isa_dma_state *ids;
     94 	int chan;
     95 {
     96 	int ochan = chan & 3;
     97 
     98 	ISA_DMA_MASK_CLR(ids, chan);
     99 
    100 	/*
    101 	 * If DMA is frozen, don't unmask it now.  It will be
    102 	 * unmasked when DMA is thawed again.
    103 	 */
    104 	if (ids->ids_frozen)
    105 		return;
    106 
    107 	/* set dma channel mode, and set dma channel mode */
    108 	if ((chan & 4) == 0)
    109 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
    110 		    DMA1_SMSK, ochan | DMA37SM_CLEAR);
    111 	else
    112 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
    113 		    DMA2_SMSK, ochan | DMA37SM_CLEAR);
    114 }
    115 
    116 static inline void
    117 _isa_dmamask(ids, chan)
    118 	struct isa_dma_state *ids;
    119 	int chan;
    120 {
    121 	int ochan = chan & 3;
    122 
    123 	ISA_DMA_MASK_SET(ids, chan);
    124 
    125 	/*
    126 	 * XXX Should we avoid masking the channel if DMA is
    127 	 * XXX frozen?  It seems like what we're doing should
    128 	 * XXX be safe, and we do need to reset FFC...
    129 	 */
    130 
    131 	/* set dma channel mode, and set dma channel mode */
    132 	if ((chan & 4) == 0) {
    133 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
    134 		    DMA1_SMSK, ochan | DMA37SM_SET);
    135 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
    136 		    DMA1_FFC, 0);
    137 	} else {
    138 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
    139 		    DMA2_SMSK, ochan | DMA37SM_SET);
    140 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
    141 		    DMA2_FFC, 0);
    142 	}
    143 }
    144 
    145 /*
    146  * _isa_dmainit(): Initialize the isa_dma_state for this chipset.
    147  */
    148 void
    149 _isa_dmainit(ids, bst, dmat, dev)
    150 	struct isa_dma_state *ids;
    151 	bus_space_tag_t bst;
    152 	bus_dma_tag_t dmat;
    153 	struct device *dev;
    154 {
    155 	int chan;
    156 
    157 	ids->ids_dev = dev;
    158 
    159 	if (ids->ids_initialized) {
    160 		/*
    161 		 * Some systems may have e.g. `ofisa' (OpenFirmware
    162 		 * configuration of ISA bus) and a regular `isa'.
    163 		 * We allow both to call the initialization function,
    164 		 * and take the device name from the last caller
    165 		 * (assuming it will be the indirect ISA bus).  Since
    166 		 * `ofisa' and `isa' are the same bus with different
    167 		 * configuration mechanisms, the space and dma tags
    168 		 * must be the same!
    169 		 */
    170 		if (ids->ids_bst != bst || ids->ids_dmat != dmat)
    171 			panic("_isa_dmainit: inconsistent ISA tags");
    172 	} else {
    173 		ids->ids_bst = bst;
    174 		ids->ids_dmat = dmat;
    175 
    176 		/*
    177 		 * Map the registers used by the ISA DMA controller.
    178 		 */
    179 		if (bus_space_map(ids->ids_bst, IO_DMA1, DMA1_IOSIZE, 0,
    180 		    &ids->ids_dma1h))
    181 			panic("_isa_dmainit: unable to map DMA controller #1");
    182 		if (bus_space_map(ids->ids_bst, IO_DMA2, DMA2_IOSIZE, 0,
    183 		    &ids->ids_dma2h))
    184 			panic("_isa_dmainit: unable to map DMA controller #2");
    185 		if (bus_space_map(ids->ids_bst, IO_DMAPG, 0xf, 0,
    186 		    &ids->ids_dmapgh))
    187 			panic("_isa_dmainit: unable to map DMA page registers");
    188 
    189 		/*
    190 		 * All 8 DMA channels start out "masked".
    191 		 */
    192 		ids->ids_masked = 0xff;
    193 
    194 		/*
    195 		 * Initialize the max transfer size for each channel, if
    196 		 * it is not initialized already (i.e. by a bus-dependent
    197 		 * front-end).
    198 		 */
    199 		for (chan = 0; chan < 8; chan++) {
    200 			if (ids->ids_maxsize[chan] == 0)
    201 				ids->ids_maxsize[chan] =
    202 				    ISA_DMA_MAXSIZE_DEFAULT(chan);
    203 		}
    204 
    205 		ids->ids_initialized = 1;
    206 
    207 		/*
    208 		 * DRQ 4 is used to chain the two 8237s together; make
    209 		 * sure it's always cascaded, and that it will be unmasked
    210 		 * when DMA is thawed.
    211 		 */
    212 		_isa_dmacascade(ids, 4);
    213 	}
    214 }
    215 
    216 /*
    217  * _isa_dmacascade(): program 8237 DMA controller channel to accept
    218  * external dma control by a board.
    219  */
    220 int
    221 _isa_dmacascade(ids, chan)
    222 	struct isa_dma_state *ids;
    223 	int chan;
    224 {
    225 	int ochan = chan & 3;
    226 
    227 	if (chan < 0 || chan > 7) {
    228 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    229 		return (EINVAL);
    230 	}
    231 
    232 	if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0) {
    233 		printf("%s: DRQ %d is not free\n", ids->ids_dev->dv_xname,
    234 		    chan);
    235 		return (EAGAIN);
    236 	}
    237 
    238 	ISA_DMA_DRQ_ALLOC(ids, chan);
    239 
    240 	/* set dma channel mode, and set dma channel mode */
    241 	if ((chan & 4) == 0)
    242 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
    243 		    DMA1_MODE, ochan | DMA37MD_CASCADE);
    244 	else
    245 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
    246 		    DMA2_MODE, ochan | DMA37MD_CASCADE);
    247 
    248 	_isa_dmaunmask(ids, chan);
    249 	return (0);
    250 }
    251 
    252 bus_size_t
    253 _isa_dmamaxsize(ids, chan)
    254 	struct isa_dma_state *ids;
    255 	int chan;
    256 {
    257 
    258 	if (chan < 0 || chan > 7) {
    259 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    260 		return (0);
    261 	}
    262 
    263 	return (ids->ids_maxsize[chan]);
    264 }
    265 
    266 int
    267 _isa_dmamap_create(ids, chan, size, flags)
    268 	struct isa_dma_state *ids;
    269 	int chan;
    270 	bus_size_t size;
    271 	int flags;
    272 {
    273 	int error;
    274 
    275 	if (chan < 0 || chan > 7) {
    276 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    277 		return (EINVAL);
    278 	}
    279 
    280 	if (size > ids->ids_maxsize[chan])
    281 		return (EINVAL);
    282 
    283 	if (ISA_DMA_DRQ_ISFREE(ids, chan) == 0) {
    284 		printf("%s: drq %d is not free\n", ids->ids_dev->dv_xname,
    285 		    chan);
    286 		return (EAGAIN);
    287 	}
    288 
    289 	ISA_DMA_DRQ_ALLOC(ids, chan);
    290 
    291 	error = bus_dmamap_create(ids->ids_dmat, size, 1, size,
    292 	    ids->ids_maxsize[chan], flags, &ids->ids_dmamaps[chan]);
    293 
    294 	if (error)
    295 		ISA_DMA_DRQ_FREE(ids, chan);
    296 
    297 	return (error);
    298 }
    299 
    300 void
    301 _isa_dmamap_destroy(ids, chan)
    302 	struct isa_dma_state *ids;
    303 	int chan;
    304 {
    305 
    306 	if (chan < 0 || chan > 7) {
    307 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    308 		goto lose;
    309 	}
    310 
    311 	if (ISA_DMA_DRQ_ISFREE(ids, chan)) {
    312 		printf("%s: drq %d is already free\n",
    313 		    ids->ids_dev->dv_xname, chan);
    314 		goto lose;
    315 	}
    316 
    317 	ISA_DMA_DRQ_FREE(ids, chan);
    318 
    319 	bus_dmamap_destroy(ids->ids_dmat, ids->ids_dmamaps[chan]);
    320 	return;
    321 
    322  lose:
    323 	panic("_isa_dmamap_destroy");
    324 }
    325 
    326 /*
    327  * _isa_dmastart(): program 8237 DMA controller channel and set it
    328  * in motion.
    329  */
    330 int
    331 _isa_dmastart(ids, chan, addr, nbytes, p, flags, busdmaflags)
    332 	struct isa_dma_state *ids;
    333 	int chan;
    334 	void *addr;
    335 	bus_size_t nbytes;
    336 	struct proc *p;
    337 	int flags;
    338 	int busdmaflags;
    339 {
    340 	bus_dmamap_t dmam;
    341 	bus_addr_t dmaaddr;
    342 	int waport;
    343 	int ochan = chan & 3;
    344 	int error;
    345 
    346 	if (chan < 0 || chan > 7) {
    347 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    348 		goto lose;
    349 	}
    350 
    351 #ifdef ISADMA_DEBUG
    352 	printf("_isa_dmastart: drq %d, addr %p, nbytes 0x%lx, p %p, "
    353 	    "flags 0x%x, dmaflags 0x%x\n",
    354 	    chan, addr, nbytes, p, flags, busdmaflags);
    355 #endif
    356 
    357 	if (chan & 4) {
    358 		if (nbytes > (1 << 17) || nbytes & 1 || (u_long)addr & 1) {
    359 			printf("%s: drq %d, nbytes 0x%lx, addr %p\n",
    360 			    ids->ids_dev->dv_xname, chan, nbytes, addr);
    361 			goto lose;
    362 		}
    363 	} else {
    364 		if (nbytes > (1 << 16)) {
    365 			printf("%s: drq %d, nbytes 0x%lx\n",
    366 			    ids->ids_dev->dv_xname, chan, nbytes);
    367 			goto lose;
    368 		}
    369 	}
    370 
    371 	dmam = ids->ids_dmamaps[chan];
    372 	if (dmam == NULL)
    373 		panic("_isa_dmastart: no DMA map for chan %d\n", chan);
    374 
    375 	error = bus_dmamap_load(ids->ids_dmat, dmam, addr, nbytes,
    376 	    p, busdmaflags);
    377 	if (error)
    378 		return (error);
    379 
    380 #ifdef ISADMA_DEBUG
    381 	__asm(".globl isa_dmastart_afterload ; isa_dmastart_afterload:");
    382 #endif
    383 
    384 	if (flags & DMAMODE_READ) {
    385 		bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
    386 		    BUS_DMASYNC_PREREAD);
    387 		ids->ids_dmareads |= (1 << chan);
    388 	} else {
    389 		bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
    390 		    BUS_DMASYNC_PREWRITE);
    391 		ids->ids_dmareads &= ~(1 << chan);
    392 	}
    393 
    394 	dmaaddr = dmam->dm_segs[0].ds_addr;
    395 
    396 #ifdef ISADMA_DEBUG
    397 	printf("     dmaaddr 0x%lx\n", dmaaddr);
    398 
    399 	__asm(".globl isa_dmastart_aftersync ; isa_dmastart_aftersync:");
    400 #endif
    401 
    402 	ids->ids_dmalength[chan] = nbytes;
    403 
    404 	_isa_dmamask(ids, chan);
    405 	ids->ids_dmafinished &= ~(1 << chan);
    406 
    407 	if ((chan & 4) == 0) {
    408 		/* set dma channel mode */
    409 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h, DMA1_MODE,
    410 		    ochan | dmamode[flags]);
    411 
    412 		/* send start address */
    413 		waport = DMA1_CHN(ochan);
    414 		bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
    415 		    dmapageport[0][ochan], (dmaaddr >> 16) & 0xff);
    416 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
    417 		    dmaaddr & 0xff);
    418 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport,
    419 		    (dmaaddr >> 8) & 0xff);
    420 
    421 		/* send count */
    422 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
    423 		    (--nbytes) & 0xff);
    424 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h, waport + 1,
    425 		    (nbytes >> 8) & 0xff);
    426 	} else {
    427 		/* set dma channel mode */
    428 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h, DMA2_MODE,
    429 		    ochan | dmamode[flags]);
    430 
    431 		/* send start address */
    432 		waport = DMA2_CHN(ochan);
    433 		bus_space_write_1(ids->ids_bst, ids->ids_dmapgh,
    434 		    dmapageport[1][ochan], (dmaaddr >> 16) & 0xff);
    435 		dmaaddr >>= 1;
    436 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
    437 		    dmaaddr & 0xff);
    438 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport,
    439 		    (dmaaddr >> 8) & 0xff);
    440 
    441 		/* send count */
    442 		nbytes >>= 1;
    443 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
    444 		    (--nbytes) & 0xff);
    445 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h, waport + 2,
    446 		    (nbytes >> 8) & 0xff);
    447 	}
    448 
    449 	_isa_dmaunmask(ids, chan);
    450 	return (0);
    451 
    452  lose:
    453 	panic("_isa_dmastart");
    454 }
    455 
    456 void
    457 _isa_dmaabort(ids, chan)
    458 	struct isa_dma_state *ids;
    459 	int chan;
    460 {
    461 
    462 	if (chan < 0 || chan > 7) {
    463 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    464 		panic("_isa_dmaabort");
    465 	}
    466 
    467 	_isa_dmamask(ids, chan);
    468 	bus_dmamap_unload(ids->ids_dmat, ids->ids_dmamaps[chan]);
    469 	ids->ids_dmareads &= ~(1 << chan);
    470 }
    471 
    472 bus_size_t
    473 _isa_dmacount(ids, chan)
    474 	struct isa_dma_state *ids;
    475 	int chan;
    476 {
    477 	int waport;
    478 	bus_size_t nbytes;
    479 	int ochan = chan & 3;
    480 
    481 	if (chan < 0 || chan > 7) {
    482 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    483 		panic("isa_dmacount");
    484 	}
    485 
    486 	_isa_dmamask(ids, chan);
    487 
    488 	/*
    489 	 * We have to shift the byte count by 1.  If we're in auto-initialize
    490 	 * mode, the count may have wrapped around to the initial value.  We
    491 	 * can't use the TC bit to check for this case, so instead we compare
    492 	 * against the original byte count.
    493 	 * If we're not in auto-initialize mode, then the count will wrap to
    494 	 * -1, so we also handle that case.
    495 	 */
    496 	if ((chan & 4) == 0) {
    497 		waport = DMA1_CHN(ochan);
    498 		nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
    499 		    waport + 1) + 1;
    500 		nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma1h,
    501 		    waport + 1) << 8;
    502 		nbytes &= 0xffff;
    503 	} else {
    504 		waport = DMA2_CHN(ochan);
    505 		nbytes = bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
    506 		    waport + 2) + 1;
    507 		nbytes += bus_space_read_1(ids->ids_bst, ids->ids_dma2h,
    508 		    waport + 2) << 8;
    509 		nbytes <<= 1;
    510 		nbytes &= 0x1ffff;
    511 	}
    512 
    513 	if (nbytes == ids->ids_dmalength[chan])
    514 		nbytes = 0;
    515 
    516 	_isa_dmaunmask(ids, chan);
    517 	return (nbytes);
    518 }
    519 
    520 int
    521 _isa_dmafinished(ids, chan)
    522 	struct isa_dma_state *ids;
    523 	int chan;
    524 {
    525 
    526 	if (chan < 0 || chan > 7) {
    527 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    528 		panic("_isa_dmafinished");
    529 	}
    530 
    531 	/* check that the terminal count was reached */
    532 	if ((chan & 4) == 0)
    533 		ids->ids_dmafinished |= bus_space_read_1(ids->ids_bst,
    534 		    ids->ids_dma1h, DMA1_SR) & 0x0f;
    535 	else
    536 		ids->ids_dmafinished |= (bus_space_read_1(ids->ids_bst,
    537 		    ids->ids_dma2h, DMA2_SR) & 0x0f) << 4;
    538 
    539 	return ((ids->ids_dmafinished & (1 << chan)) != 0);
    540 }
    541 
    542 void
    543 _isa_dmadone(ids, chan)
    544 	struct isa_dma_state *ids;
    545 	int chan;
    546 {
    547 	bus_dmamap_t dmam;
    548 
    549 	if (chan < 0 || chan > 7) {
    550 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    551 		panic("_isa_dmadone");
    552 	}
    553 
    554 	dmam = ids->ids_dmamaps[chan];
    555 
    556 	_isa_dmamask(ids, chan);
    557 
    558 	if (_isa_dmafinished(ids, chan) == 0)
    559 		printf("%s: _isa_dmadone: channel %d not finished\n",
    560 		    ids->ids_dev->dv_xname, chan);
    561 
    562 	bus_dmamap_sync(ids->ids_dmat, dmam, 0, dmam->dm_mapsize,
    563 	    (ids->ids_dmareads & (1 << chan)) ? BUS_DMASYNC_POSTREAD :
    564 	    BUS_DMASYNC_POSTWRITE);
    565 
    566 	bus_dmamap_unload(ids->ids_dmat, dmam);
    567 	ids->ids_dmareads &= ~(1 << chan);
    568 }
    569 
    570 void
    571 _isa_dmafreeze(ids)
    572 	struct isa_dma_state *ids;
    573 {
    574 	int s;
    575 
    576 	s = splhigh();
    577 
    578 	if (ids->ids_frozen == 0) {
    579 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
    580 		    DMA1_MASK, 0x0f);
    581 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
    582 		    DMA2_MASK, 0x0f);
    583 	}
    584 
    585 	ids->ids_frozen++;
    586 	if (ids->ids_frozen < 1)
    587 		panic("_isa_dmafreeze: overflow");
    588 
    589 	splx(s);
    590 }
    591 
    592 void
    593 _isa_dmathaw(ids)
    594 	struct isa_dma_state *ids;
    595 {
    596 	int s;
    597 
    598 	s = splhigh();
    599 
    600 	ids->ids_frozen--;
    601 	if (ids->ids_frozen < 0)
    602 		panic("_isa_dmathaw: underflow");
    603 
    604 	if (ids->ids_frozen == 0) {
    605 		bus_space_write_1(ids->ids_bst, ids->ids_dma1h,
    606 		    DMA1_MASK, ids->ids_masked & 0x0f);
    607 		bus_space_write_1(ids->ids_bst, ids->ids_dma2h,
    608 		    DMA2_MASK, (ids->ids_masked >> 4) & 0x0f);
    609 	}
    610 
    611 	splx(s);
    612 }
    613 
    614 int
    615 _isa_dmamem_alloc(ids, chan, size, addrp, flags)
    616 	struct isa_dma_state *ids;
    617 	int chan;
    618 	bus_size_t size;
    619 	bus_addr_t *addrp;
    620 	int flags;
    621 {
    622 	bus_dma_segment_t seg;
    623 	int error, boundary, rsegs;
    624 
    625 	if (chan < 0 || chan > 7) {
    626 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    627 		panic("_isa_dmamem_alloc");
    628 	}
    629 
    630 	boundary = (chan & 4) ? (1 << 17) : (1 << 16);
    631 
    632 	size = round_page(size);
    633 
    634 	error = bus_dmamem_alloc(ids->ids_dmat, size, NBPG, boundary,
    635 	    &seg, 1, &rsegs, flags);
    636 	if (error)
    637 		return (error);
    638 
    639 	*addrp = seg.ds_addr;
    640 	return (0);
    641 }
    642 
    643 void
    644 _isa_dmamem_free(ids, chan, addr, size)
    645 	struct isa_dma_state *ids;
    646 	int chan;
    647 	bus_addr_t addr;
    648 	bus_size_t size;
    649 {
    650 	bus_dma_segment_t seg;
    651 
    652 	if (chan < 0 || chan > 7) {
    653 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    654 		panic("_isa_dmamem_free");
    655 	}
    656 
    657 	seg.ds_addr = addr;
    658 	seg.ds_len = size;
    659 
    660 	bus_dmamem_free(ids->ids_dmat, &seg, 1);
    661 }
    662 
    663 int
    664 _isa_dmamem_map(ids, chan, addr, size, kvap, flags)
    665 	struct isa_dma_state *ids;
    666 	int chan;
    667 	bus_addr_t addr;
    668 	bus_size_t size;
    669 	caddr_t *kvap;
    670 	int flags;
    671 {
    672 	bus_dma_segment_t seg;
    673 
    674 	if (chan < 0 || chan > 7) {
    675 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    676 		panic("_isa_dmamem_map");
    677 	}
    678 
    679 	seg.ds_addr = addr;
    680 	seg.ds_len = size;
    681 
    682 	return (bus_dmamem_map(ids->ids_dmat, &seg, 1, size, kvap, flags));
    683 }
    684 
    685 void
    686 _isa_dmamem_unmap(ids, chan, kva, size)
    687 	struct isa_dma_state *ids;
    688 	int chan;
    689 	caddr_t kva;
    690 	size_t size;
    691 {
    692 
    693 	if (chan < 0 || chan > 7) {
    694 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    695 		panic("_isa_dmamem_unmap");
    696 	}
    697 
    698 	bus_dmamem_unmap(ids->ids_dmat, kva, size);
    699 }
    700 
    701 paddr_t
    702 _isa_dmamem_mmap(ids, chan, addr, size, off, prot, flags)
    703 	struct isa_dma_state *ids;
    704 	int chan;
    705 	bus_addr_t addr;
    706 	bus_size_t size;
    707 	off_t off;
    708 	int prot, flags;
    709 {
    710 	bus_dma_segment_t seg;
    711 
    712 	if (chan < 0 || chan > 7) {
    713 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    714 		panic("_isa_dmamem_mmap");
    715 	}
    716 
    717 	if (off < 0)
    718 		return (-1);
    719 
    720 	seg.ds_addr = addr;
    721 	seg.ds_len = size;
    722 
    723 	return (bus_dmamem_mmap(ids->ids_dmat, &seg, 1, off, prot, flags));
    724 }
    725 
    726 int
    727 _isa_drq_isfree(ids, chan)
    728 	struct isa_dma_state *ids;
    729 	int chan;
    730 {
    731 
    732 	if (chan < 0 || chan > 7) {
    733 		printf("%s: bogus drq %d\n", ids->ids_dev->dv_xname, chan);
    734 		panic("_isa_drq_isfree");
    735 	}
    736 
    737 	return ISA_DMA_DRQ_ISFREE(ids, chan);
    738 }
    739 
    740 void *
    741 _isa_malloc(ids, chan, size, pool, flags)
    742 	struct isa_dma_state *ids;
    743 	int chan;
    744 	size_t size;
    745 	int pool;
    746 	int flags;
    747 {
    748 	bus_addr_t addr;
    749 	caddr_t kva;
    750 	int bflags;
    751 	struct isa_mem *m;
    752 
    753 	bflags = flags & M_WAITOK ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT;
    754 
    755 	if (_isa_dmamem_alloc(ids, chan, size, &addr, bflags))
    756 		return 0;
    757 	if (_isa_dmamem_map(ids, chan, addr, size, &kva, bflags)) {
    758 		_isa_dmamem_free(ids, chan, addr, size);
    759 		return 0;
    760 	}
    761 	m = malloc(sizeof(*m), pool, flags);
    762 	if (m == 0) {
    763 		_isa_dmamem_unmap(ids, chan, kva, size);
    764 		_isa_dmamem_free(ids, chan, addr, size);
    765 		return 0;
    766 	}
    767 	m->ids = ids;
    768 	m->chan = chan;
    769 	m->size = size;
    770 	m->addr = addr;
    771 	m->kva = kva;
    772 	m->next = isa_mem_head;
    773 	isa_mem_head = m;
    774 	return (void *)kva;
    775 }
    776 
    777 void
    778 _isa_free(addr, pool)
    779 	void *addr;
    780 	int pool;
    781 {
    782 	struct isa_mem **mp, *m;
    783 	caddr_t kva = (caddr_t)addr;
    784 
    785 	for(mp = &isa_mem_head; *mp && (*mp)->kva != kva;
    786 	    mp = &(*mp)->next)
    787 		;
    788 	m = *mp;
    789 	if (!m) {
    790 		printf("_isa_free: freeing unallocted memory\n");
    791 		return;
    792 	}
    793 	*mp = m->next;
    794 	_isa_dmamem_unmap(m->ids, m->chan, kva, m->size);
    795 	_isa_dmamem_free(m->ids, m->chan, m->addr, m->size);
    796 	free(m, pool);
    797 }
    798 
    799 paddr_t
    800 _isa_mappage(mem, off, prot)
    801 	void *mem;
    802 	off_t off;
    803 	int prot;
    804 {
    805 	struct isa_mem *m;
    806 
    807 	for(m = isa_mem_head; m && m->kva != (caddr_t)mem; m = m->next)
    808 		;
    809 	if (!m) {
    810 		printf("_isa_mappage: mapping unallocted memory\n");
    811 		return -1;
    812 	}
    813 	return _isa_dmamem_mmap(m->ids, m->chan, m->addr,
    814 	    m->size, off, prot, BUS_DMA_WAITOK);
    815 }
    816