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