Home | History | Annotate | Line # | Download | only in isa
isadma.c revision 1.9
      1  1.9  mycroft /*	$NetBSD: isadma.c,v 1.9 1994/11/04 19:25:34 mycroft Exp $	*/
      2  1.8      cgd 
      3  1.3  mycroft #include <sys/param.h>
      4  1.3  mycroft #include <sys/systm.h>
      5  1.3  mycroft #include <sys/file.h>
      6  1.5  mycroft #include <sys/buf.h>
      7  1.3  mycroft #include <sys/syslog.h>
      8  1.5  mycroft #include <sys/malloc.h>
      9  1.5  mycroft #include <sys/uio.h>
     10  1.5  mycroft 
     11  1.3  mycroft #include <vm/vm.h>
     12  1.3  mycroft 
     13  1.3  mycroft #include <machine/pio.h>
     14  1.3  mycroft 
     15  1.6  mycroft #include <i386/isa/isareg.h>
     16  1.5  mycroft #include <i386/isa/dmavar.h>
     17  1.5  mycroft #include <i386/isa/dmareg.h>
     18  1.1  mycroft 
     19  1.1  mycroft /* region of physical memory known to be contiguous */
     20  1.5  mycroft vm_offset_t isaphysmem;
     21  1.5  mycroft static caddr_t dma_bounce[8];		/* XXX */
     22  1.5  mycroft static char bounced[8];		/* XXX */
     23  1.5  mycroft #define MAXDMASZ 512		/* XXX */
     24  1.1  mycroft 
     25  1.1  mycroft /* high byte of address is stored in this port for i-th dma channel */
     26  1.5  mycroft static short dmapageport[8] =
     27  1.1  mycroft 	{ 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
     28  1.1  mycroft 
     29  1.1  mycroft /*
     30  1.5  mycroft  * isa_dmacascade(): program 8237 DMA controller channel to accept
     31  1.1  mycroft  * external dma control by a board.
     32  1.1  mycroft  */
     33  1.1  mycroft void
     34  1.5  mycroft isa_dmacascade(chan)
     35  1.4  mycroft 	int chan;
     36  1.1  mycroft {
     37  1.4  mycroft 
     38  1.1  mycroft #ifdef DIAGNOSTIC
     39  1.5  mycroft 	if (chan < 0 || chan > 7)
     40  1.5  mycroft 		panic("isa_dmacascade: impossible request");
     41  1.1  mycroft #endif
     42  1.1  mycroft 
     43  1.1  mycroft 	/* set dma channel mode, and set dma channel mode */
     44  1.1  mycroft 	if ((chan & 4) == 0) {
     45  1.1  mycroft 		outb(DMA1_MODE, DMA37MD_CASCADE | chan);
     46  1.1  mycroft 		outb(DMA1_SMSK, chan);
     47  1.1  mycroft 	} else {
     48  1.1  mycroft 		outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
     49  1.1  mycroft 		outb(DMA2_SMSK, chan & 3);
     50  1.1  mycroft 	}
     51  1.1  mycroft }
     52  1.1  mycroft 
     53  1.1  mycroft /*
     54  1.5  mycroft  * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
     55  1.1  mycroft  * problems by using a bounce buffer.
     56  1.1  mycroft  */
     57  1.1  mycroft void
     58  1.5  mycroft isa_dmastart(flags, addr, nbytes, chan)
     59  1.5  mycroft 	int flags;
     60  1.1  mycroft 	caddr_t addr;
     61  1.1  mycroft 	vm_size_t nbytes;
     62  1.4  mycroft 	int chan;
     63  1.1  mycroft {
     64  1.1  mycroft 	vm_offset_t phys;
     65  1.1  mycroft 	int waport;
     66  1.1  mycroft 	caddr_t newaddr;
     67  1.1  mycroft 
     68  1.4  mycroft #ifdef DIAGNOSTIC
     69  1.5  mycroft 	if (chan < 0 || chan > 7 ||
     70  1.5  mycroft 	    ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
     71  1.5  mycroft 	    (nbytes >= (1<<16))))
     72  1.5  mycroft 		panic("isa_dmastart: impossible request");
     73  1.4  mycroft #endif
     74  1.1  mycroft 
     75  1.5  mycroft 	if (isa_dmarangecheck(addr, nbytes, chan)) {
     76  1.5  mycroft 		if (dma_bounce[chan] == 0)
     77  1.5  mycroft 			dma_bounce[chan] =
     78  1.5  mycroft 			    /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
     79  1.5  mycroft 			    (caddr_t) isaphysmem + NBPG*chan;
     80  1.5  mycroft 		bounced[chan] = 1;
     81  1.5  mycroft 		newaddr = dma_bounce[chan];
     82  1.5  mycroft 		*(int *) newaddr = 0;	/* XXX */
     83  1.1  mycroft 		/* copy bounce buffer on write */
     84  1.5  mycroft 		if ((flags & B_READ) == 0)
     85  1.1  mycroft 			bcopy(addr, newaddr, nbytes);
     86  1.1  mycroft 		addr = newaddr;
     87  1.1  mycroft 	}
     88  1.1  mycroft 
     89  1.1  mycroft 	/* translate to physical */
     90  1.7      cgd 	phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
     91  1.1  mycroft 
     92  1.1  mycroft 	if ((chan & 4) == 0) {
     93  1.1  mycroft 		/*
     94  1.1  mycroft 		 * Program one of DMA channels 0..3.  These are
     95  1.1  mycroft 		 * byte mode channels.
     96  1.1  mycroft 		 */
     97  1.1  mycroft 		/* set dma channel mode, and reset address ff */
     98  1.5  mycroft 		if (flags & B_READ)
     99  1.5  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_WRITE);
    100  1.1  mycroft 		else
    101  1.5  mycroft 			outb(DMA1_MODE, chan | DMA37MD_SINGLE | DMA37MD_READ);
    102  1.1  mycroft 		outb(DMA1_FFC, 0);
    103  1.1  mycroft 
    104  1.1  mycroft 		/* send start address */
    105  1.1  mycroft 		waport =  DMA1_CHN(chan);
    106  1.1  mycroft 		outb(waport, phys);
    107  1.1  mycroft 		outb(waport, phys>>8);
    108  1.1  mycroft 		outb(dmapageport[chan], phys>>16);
    109  1.1  mycroft 
    110  1.1  mycroft 		/* send count */
    111  1.1  mycroft 		outb(waport + 1, --nbytes);
    112  1.1  mycroft 		outb(waport + 1, nbytes>>8);
    113  1.1  mycroft 
    114  1.1  mycroft 		/* unmask channel */
    115  1.5  mycroft 		outb(DMA1_SMSK, chan | DMA37SM_CLEAR);
    116  1.1  mycroft 	} else {
    117  1.1  mycroft 		/*
    118  1.1  mycroft 		 * Program one of DMA channels 4..7.  These are
    119  1.1  mycroft 		 * word mode channels.
    120  1.1  mycroft 		 */
    121  1.1  mycroft 		/* set dma channel mode, and reset address ff */
    122  1.5  mycroft 		if (flags & B_READ)
    123  1.5  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_WRITE);
    124  1.1  mycroft 		else
    125  1.5  mycroft 			outb(DMA2_MODE, (chan & 3) | DMA37MD_SINGLE | DMA37MD_READ);
    126  1.1  mycroft 		outb(DMA2_FFC, 0);
    127  1.1  mycroft 
    128  1.1  mycroft 		/* send start address */
    129  1.1  mycroft 		waport = DMA2_CHN(chan & 3);
    130  1.1  mycroft 		outb(waport, phys>>1);
    131  1.1  mycroft 		outb(waport, phys>>9);
    132  1.1  mycroft 		outb(dmapageport[chan], phys>>16);
    133  1.1  mycroft 
    134  1.1  mycroft 		/* send count */
    135  1.1  mycroft 		nbytes >>= 1;
    136  1.1  mycroft 		outb(waport + 2, --nbytes);
    137  1.1  mycroft 		outb(waport + 2, nbytes>>8);
    138  1.1  mycroft 
    139  1.1  mycroft 		/* unmask channel */
    140  1.5  mycroft 		outb(DMA2_SMSK, (chan & 3) | DMA37SM_CLEAR);
    141  1.1  mycroft 	}
    142  1.1  mycroft }
    143  1.1  mycroft 
    144  1.1  mycroft void
    145  1.5  mycroft isa_dmaabort(chan)
    146  1.4  mycroft 	int chan;
    147  1.1  mycroft {
    148  1.1  mycroft 
    149  1.1  mycroft #ifdef DIAGNOSTIC
    150  1.5  mycroft 	if (chan < 0 || chan > 7)
    151  1.5  mycroft 		panic("isa_dmaabort: impossible request");
    152  1.1  mycroft #endif
    153  1.1  mycroft 
    154  1.1  mycroft 	bounced[chan] = 0;
    155  1.1  mycroft 
    156  1.1  mycroft 	/* mask channel */
    157  1.1  mycroft 	if ((chan & 4) == 0)
    158  1.1  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    159  1.1  mycroft 	else
    160  1.1  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    161  1.1  mycroft }
    162  1.1  mycroft 
    163  1.1  mycroft void
    164  1.5  mycroft isa_dmadone(flags, addr, nbytes, chan)
    165  1.5  mycroft 	int flags;
    166  1.5  mycroft 	caddr_t addr;
    167  1.5  mycroft 	vm_size_t nbytes;
    168  1.5  mycroft 	int chan;
    169  1.1  mycroft {
    170  1.1  mycroft 	u_char tc;
    171  1.1  mycroft 
    172  1.1  mycroft #ifdef DIAGNOSTIC
    173  1.5  mycroft 	if (chan < 0 || chan > 7)
    174  1.5  mycroft 		panic("isa_dmadone: impossible request");
    175  1.1  mycroft #endif
    176  1.1  mycroft 
    177  1.1  mycroft 	/* check that the terminal count was reached */
    178  1.1  mycroft 	if ((chan & 4) == 0)
    179  1.1  mycroft 		tc = inb(DMA1_SR) & (1 << chan);
    180  1.1  mycroft 	else
    181  1.1  mycroft 		tc = inb(DMA2_SR) & (1 << (chan & 3));
    182  1.1  mycroft 	if (tc == 0)
    183  1.1  mycroft 		/* XXX probably should panic or something */
    184  1.1  mycroft 		log(LOG_ERR, "dma channel %d not finished\n", chan);
    185  1.5  mycroft 
    186  1.9  mycroft 	/* mask channel */
    187  1.9  mycroft 	if ((chan & 4) == 0)
    188  1.9  mycroft 		outb(DMA1_SMSK, DMA37SM_SET | chan);
    189  1.9  mycroft 	else
    190  1.9  mycroft 		outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
    191  1.9  mycroft 
    192  1.1  mycroft 	/* copy bounce buffer on read */
    193  1.1  mycroft 	if (bounced[chan]) {
    194  1.5  mycroft 		bcopy(dma_bounce[chan], addr, nbytes);
    195  1.1  mycroft 		bounced[chan] = 0;
    196  1.1  mycroft 	}
    197  1.1  mycroft }
    198  1.1  mycroft 
    199  1.1  mycroft /*
    200  1.1  mycroft  * Check for problems with the address range of a DMA transfer
    201  1.1  mycroft  * (non-contiguous physical pages, outside of bus address space,
    202  1.1  mycroft  * crossing DMA page boundaries).
    203  1.1  mycroft  * Return true if special handling needed.
    204  1.1  mycroft  */
    205  1.1  mycroft int
    206  1.5  mycroft isa_dmarangecheck(va, length, chan)
    207  1.1  mycroft 	vm_offset_t va;
    208  1.4  mycroft 	u_long length;
    209  1.4  mycroft 	int chan;
    210  1.1  mycroft {
    211  1.1  mycroft 	vm_offset_t phys, priorpage = 0, endva;
    212  1.5  mycroft 	u_int dma_pgmsk = (chan & 4) ?  ~(128*1024-1) : ~(64*1024-1);
    213  1.1  mycroft 
    214  1.1  mycroft 	endva = round_page(va + length);
    215  1.1  mycroft 	for (; va < endva ; va += NBPG) {
    216  1.7      cgd 		phys = trunc_page(pmap_extract(kernel_pmap, va));
    217  1.1  mycroft 		if (phys == 0)
    218  1.5  mycroft 			panic("isa_dmacheck: no physical page present");
    219  1.5  mycroft 		if (phys >= (1<<24))
    220  1.1  mycroft 			return 1;
    221  1.1  mycroft 		if (priorpage) {
    222  1.1  mycroft 			if (priorpage + NBPG != phys)
    223  1.1  mycroft 				return 1;
    224  1.1  mycroft 			/* check if crossing a DMA page boundary */
    225  1.1  mycroft 			if ((priorpage ^ phys) & dma_pgmsk)
    226  1.1  mycroft 				return 1;
    227  1.1  mycroft 		}
    228  1.1  mycroft 		priorpage = phys;
    229  1.1  mycroft 	}
    230  1.1  mycroft 	return 0;
    231  1.5  mycroft }
    232  1.5  mycroft 
    233  1.5  mycroft /* head of queue waiting for physmem to become available */
    234  1.5  mycroft struct buf isa_physmemq;
    235  1.5  mycroft 
    236  1.5  mycroft /* blocked waiting for resource to become free for exclusive use */
    237  1.5  mycroft static isaphysmemflag;
    238  1.5  mycroft /* if waited for and call requested when free (B_CALL) */
    239  1.5  mycroft static void (*isaphysmemunblock)(); /* needs to be a list */
    240  1.5  mycroft 
    241  1.5  mycroft /*
    242  1.5  mycroft  * Allocate contiguous physical memory for transfer, returning
    243  1.5  mycroft  * a *virtual* address to region. May block waiting for resource.
    244  1.5  mycroft  * (assumed to be called at splbio())
    245  1.5  mycroft  */
    246  1.5  mycroft caddr_t
    247  1.5  mycroft isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
    248  1.5  mycroft 
    249  1.5  mycroft 	isaphysmemunblock = func;
    250  1.5  mycroft 	while (isaphysmemflag & B_BUSY) {
    251  1.5  mycroft 		isaphysmemflag |= B_WANTED;
    252  1.5  mycroft 		sleep((caddr_t)&isaphysmemflag, PRIBIO);
    253  1.5  mycroft 	}
    254  1.5  mycroft 	isaphysmemflag |= B_BUSY;
    255  1.5  mycroft 
    256  1.5  mycroft 	return((caddr_t)isaphysmem);
    257  1.5  mycroft }
    258  1.5  mycroft 
    259  1.5  mycroft /*
    260  1.5  mycroft  * Free contiguous physical memory used for transfer.
    261  1.5  mycroft  * (assumed to be called at splbio())
    262  1.5  mycroft  */
    263  1.5  mycroft void
    264  1.5  mycroft isa_freephysmem(caddr_t va, unsigned length) {
    265  1.5  mycroft 
    266  1.5  mycroft 	isaphysmemflag &= ~B_BUSY;
    267  1.5  mycroft 	if (isaphysmemflag & B_WANTED) {
    268  1.5  mycroft 		isaphysmemflag &= B_WANTED;
    269  1.5  mycroft 		wakeup((caddr_t)&isaphysmemflag);
    270  1.5  mycroft 		if (isaphysmemunblock)
    271  1.5  mycroft 			(*isaphysmemunblock)();
    272  1.5  mycroft 	}
    273  1.1  mycroft }
    274