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