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