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