1997-03-21 05:17:11 +03:00
|
|
|
/* $NetBSD: isadma.c,v 1.23 1997/03/21 02:17:11 mycroft Exp $ */
|
1994-10-27 07:14:23 +03:00
|
|
|
|
1993-10-17 08:34:23 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/file.h>
|
1994-04-23 02:58:50 +04:00
|
|
|
#include <sys/buf.h>
|
1993-10-17 08:34:23 +03:00
|
|
|
#include <sys/syslog.h>
|
1994-04-23 02:58:50 +04:00
|
|
|
#include <sys/malloc.h>
|
1996-04-30 00:02:32 +04:00
|
|
|
#include <sys/proc.h>
|
1994-04-23 02:58:50 +04:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
1993-10-17 08:34:23 +03:00
|
|
|
#include <vm/vm.h>
|
|
|
|
|
|
|
|
#include <machine/pio.h>
|
|
|
|
|
1995-04-17 16:06:30 +04:00
|
|
|
#include <dev/isa/isareg.h>
|
|
|
|
#include <dev/isa/isadmavar.h>
|
|
|
|
#include <dev/isa/isadmareg.h>
|
1993-10-14 08:22:57 +03:00
|
|
|
|
|
|
|
/* region of physical memory known to be contiguous */
|
1994-04-23 02:58:50 +04:00
|
|
|
vm_offset_t isaphysmem;
|
|
|
|
static caddr_t dma_bounce[8]; /* XXX */
|
|
|
|
static char bounced[8]; /* XXX */
|
|
|
|
#define MAXDMASZ 512 /* XXX */
|
1996-02-22 09:21:48 +03:00
|
|
|
static u_int8_t dma_finished;
|
1993-10-14 08:22:57 +03:00
|
|
|
|
|
|
|
/* high byte of address is stored in this port for i-th dma channel */
|
1996-04-01 00:51:43 +04:00
|
|
|
static int dmapageport[2][4] = {
|
|
|
|
{0x87, 0x83, 0x81, 0x82},
|
|
|
|
{0x8f, 0x8b, 0x89, 0x8a}
|
1996-03-01 07:08:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static u_int8_t dmamode[4] = {
|
|
|
|
DMA37MD_READ | DMA37MD_SINGLE,
|
1996-03-01 07:35:27 +03:00
|
|
|
DMA37MD_WRITE | DMA37MD_SINGLE,
|
|
|
|
DMA37MD_READ | DMA37MD_LOOP,
|
|
|
|
DMA37MD_WRITE | DMA37MD_LOOP
|
1996-03-01 07:08:13 +03:00
|
|
|
};
|
1993-10-14 08:22:57 +03:00
|
|
|
|
1996-04-30 00:02:32 +04:00
|
|
|
int isa_dmarangecheck __P((vm_offset_t, u_long, int));
|
|
|
|
caddr_t isa_allocphysmem __P((caddr_t, unsigned, void (*)(void)));
|
|
|
|
void isa_freephysmem __P((caddr_t, unsigned));
|
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
inline void
|
|
|
|
isa_dmaunmask(chan)
|
|
|
|
int chan;
|
|
|
|
{
|
|
|
|
int ochan = chan & 3;
|
|
|
|
|
|
|
|
#ifdef ISADMA_DEBUG
|
|
|
|
if (chan < 0 || chan > 7)
|
|
|
|
panic("isa_dmacascade: impossible request");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* set dma channel mode, and set dma channel mode */
|
|
|
|
if ((chan & 4) == 0)
|
|
|
|
outb(DMA1_SMSK, ochan | DMA37SM_CLEAR);
|
|
|
|
else
|
|
|
|
outb(DMA2_SMSK, ochan | DMA37SM_CLEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
isa_dmamask(chan)
|
|
|
|
int chan;
|
|
|
|
{
|
|
|
|
int ochan = chan & 3;
|
|
|
|
|
|
|
|
#ifdef ISADMA_DEBUG
|
|
|
|
if (chan < 0 || chan > 7)
|
|
|
|
panic("isa_dmacascade: impossible request");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* set dma channel mode, and set dma channel mode */
|
|
|
|
if ((chan & 4) == 0) {
|
|
|
|
outb(DMA1_SMSK, ochan | DMA37SM_SET);
|
|
|
|
outb(DMA1_FFC, 0);
|
|
|
|
} else {
|
|
|
|
outb(DMA2_SMSK, ochan | DMA37SM_SET);
|
|
|
|
outb(DMA2_FFC, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-10-22 23:24:14 +03:00
|
|
|
/*
|
1994-04-23 02:58:50 +04:00
|
|
|
* isa_dmacascade(): program 8237 DMA controller channel to accept
|
1993-10-14 08:22:57 +03:00
|
|
|
* external dma control by a board.
|
|
|
|
*/
|
|
|
|
void
|
1994-04-23 02:58:50 +04:00
|
|
|
isa_dmacascade(chan)
|
1993-10-22 23:24:14 +03:00
|
|
|
int chan;
|
1993-10-14 08:22:57 +03:00
|
|
|
{
|
1997-03-21 05:17:11 +03:00
|
|
|
int ochan = chan & 3;
|
1993-10-22 23:24:14 +03:00
|
|
|
|
1996-02-20 07:17:05 +03:00
|
|
|
#ifdef ISADMA_DEBUG
|
1994-04-23 02:58:50 +04:00
|
|
|
if (chan < 0 || chan > 7)
|
|
|
|
panic("isa_dmacascade: impossible request");
|
1993-10-14 08:22:57 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* set dma channel mode, and set dma channel mode */
|
1997-03-21 05:17:11 +03:00
|
|
|
if ((chan & 4) == 0)
|
|
|
|
outb(DMA1_MODE, ochan | DMA37MD_CASCADE);
|
|
|
|
else
|
|
|
|
outb(DMA2_MODE, ochan | DMA37MD_CASCADE);
|
1996-03-01 07:08:13 +03:00
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
isa_dmaunmask(chan);
|
1993-10-14 08:22:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-04-23 02:58:50 +04:00
|
|
|
* isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
|
1993-10-14 08:22:57 +03:00
|
|
|
* problems by using a bounce buffer.
|
|
|
|
*/
|
|
|
|
void
|
1994-04-23 02:58:50 +04:00
|
|
|
isa_dmastart(flags, addr, nbytes, chan)
|
|
|
|
int flags;
|
1993-10-14 08:22:57 +03:00
|
|
|
caddr_t addr;
|
|
|
|
vm_size_t nbytes;
|
1993-10-22 23:24:14 +03:00
|
|
|
int chan;
|
1993-10-14 08:22:57 +03:00
|
|
|
{
|
|
|
|
vm_offset_t phys;
|
|
|
|
int waport;
|
|
|
|
caddr_t newaddr;
|
1997-03-21 05:17:11 +03:00
|
|
|
int ochan = chan & 3;
|
1993-10-14 08:22:57 +03:00
|
|
|
|
1996-02-20 07:17:05 +03:00
|
|
|
#ifdef ISADMA_DEBUG
|
1994-04-23 02:58:50 +04:00
|
|
|
if (chan < 0 || chan > 7 ||
|
|
|
|
((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
|
|
|
|
(nbytes >= (1<<16))))
|
|
|
|
panic("isa_dmastart: impossible request");
|
1993-10-22 23:24:14 +03:00
|
|
|
#endif
|
1993-10-14 08:22:57 +03:00
|
|
|
|
1996-04-30 00:02:32 +04:00
|
|
|
if (isa_dmarangecheck((vm_offset_t) addr, nbytes, chan)) {
|
1994-04-23 02:58:50 +04:00
|
|
|
if (dma_bounce[chan] == 0)
|
|
|
|
dma_bounce[chan] =
|
|
|
|
/*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
|
|
|
|
(caddr_t) isaphysmem + NBPG*chan;
|
|
|
|
bounced[chan] = 1;
|
|
|
|
newaddr = dma_bounce[chan];
|
|
|
|
*(int *) newaddr = 0; /* XXX */
|
1993-10-14 08:22:57 +03:00
|
|
|
/* copy bounce buffer on write */
|
1996-03-01 07:08:13 +03:00
|
|
|
if ((flags & DMAMODE_READ) == 0)
|
1993-10-14 08:22:57 +03:00
|
|
|
bcopy(addr, newaddr, nbytes);
|
|
|
|
addr = newaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* translate to physical */
|
1995-04-10 17:08:28 +04:00
|
|
|
phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
|
1993-10-14 08:22:57 +03:00
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
isa_dmamask(chan);
|
1996-02-22 09:21:48 +03:00
|
|
|
dma_finished &= ~(1 << chan);
|
|
|
|
|
1993-10-14 08:22:57 +03:00
|
|
|
if ((chan & 4) == 0) {
|
1997-03-21 05:17:11 +03:00
|
|
|
/* set dma channel mode */
|
|
|
|
outb(DMA1_MODE, ochan | dmamode[flags]);
|
1993-10-14 08:22:57 +03:00
|
|
|
|
|
|
|
/* send start address */
|
1997-03-21 05:17:11 +03:00
|
|
|
waport = DMA1_CHN(ochan);
|
|
|
|
outb(dmapageport[0][ochan], phys>>16);
|
1993-10-14 08:22:57 +03:00
|
|
|
outb(waport, phys);
|
|
|
|
outb(waport, phys>>8);
|
|
|
|
|
|
|
|
/* send count */
|
|
|
|
outb(waport + 1, --nbytes);
|
|
|
|
outb(waport + 1, nbytes>>8);
|
|
|
|
} else {
|
1997-03-21 05:17:11 +03:00
|
|
|
/* set dma channel mode */
|
|
|
|
outb(DMA2_MODE, ochan | dmamode[flags]);
|
1993-10-14 08:22:57 +03:00
|
|
|
|
|
|
|
/* send start address */
|
1997-03-21 05:17:11 +03:00
|
|
|
waport = DMA2_CHN(ochan);
|
|
|
|
outb(dmapageport[1][ochan], phys>>16);
|
1996-03-01 07:08:13 +03:00
|
|
|
phys >>= 1;
|
|
|
|
outb(waport, phys);
|
|
|
|
outb(waport, phys>>8);
|
1993-10-14 08:22:57 +03:00
|
|
|
|
|
|
|
/* send count */
|
|
|
|
nbytes >>= 1;
|
|
|
|
outb(waport + 2, --nbytes);
|
|
|
|
outb(waport + 2, nbytes>>8);
|
|
|
|
}
|
1997-03-21 05:17:11 +03:00
|
|
|
|
|
|
|
isa_dmaunmask(chan);
|
1993-10-14 08:22:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-04-23 02:58:50 +04:00
|
|
|
isa_dmaabort(chan)
|
1993-10-22 23:24:14 +03:00
|
|
|
int chan;
|
1993-10-14 08:22:57 +03:00
|
|
|
{
|
|
|
|
|
1996-02-20 07:17:05 +03:00
|
|
|
#ifdef ISADMA_DEBUG
|
1994-04-23 02:58:50 +04:00
|
|
|
if (chan < 0 || chan > 7)
|
|
|
|
panic("isa_dmaabort: impossible request");
|
1993-10-14 08:22:57 +03:00
|
|
|
#endif
|
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
isa_dmamask(chan);
|
1997-03-21 03:00:21 +03:00
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
bounced[chan] = 0;
|
1997-03-21 03:00:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vm_size_t
|
|
|
|
isa_dmacount(chan)
|
|
|
|
int chan;
|
|
|
|
{
|
|
|
|
int waport;
|
|
|
|
vm_size_t nbytes;
|
1997-03-21 05:17:11 +03:00
|
|
|
int ochan = chan & 3;
|
1997-03-21 03:00:21 +03:00
|
|
|
|
|
|
|
#ifdef ISADMA_DEBUG
|
|
|
|
if (chan < 0 || chan > 7)
|
|
|
|
panic("isa_dmafinished: impossible request");
|
|
|
|
#endif
|
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
isa_dmamask(chan);
|
1997-03-21 03:00:21 +03:00
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
/* check that the terminal count was reached */
|
|
|
|
if (!isa_dmafinished(chan)) {
|
|
|
|
/* read count */
|
|
|
|
if ((chan & 4) == 0) {
|
|
|
|
waport = DMA1_CHN(ochan);
|
1997-03-21 03:00:21 +03:00
|
|
|
nbytes = inb(waport + 1) + 1;
|
|
|
|
nbytes += inb(waport + 1) << 8;
|
1997-03-21 05:17:11 +03:00
|
|
|
} else {
|
|
|
|
waport = DMA2_CHN(ochan);
|
1997-03-21 03:00:21 +03:00
|
|
|
nbytes = inb(waport + 2) + 1;
|
|
|
|
nbytes += inb(waport + 2) << 8;
|
|
|
|
nbytes <<= 1;
|
1997-03-21 05:17:11 +03:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
nbytes = 0;
|
1997-03-21 03:00:21 +03:00
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
isa_dmaunmask(chan);
|
1997-03-21 03:00:21 +03:00
|
|
|
return (nbytes);
|
1993-10-14 08:22:57 +03:00
|
|
|
}
|
|
|
|
|
1996-02-20 07:17:05 +03:00
|
|
|
int
|
|
|
|
isa_dmafinished(chan)
|
1994-04-23 02:58:50 +04:00
|
|
|
int chan;
|
1993-10-14 08:22:57 +03:00
|
|
|
{
|
|
|
|
|
1996-02-20 07:17:05 +03:00
|
|
|
#ifdef ISADMA_DEBUG
|
1994-04-23 02:58:50 +04:00
|
|
|
if (chan < 0 || chan > 7)
|
1996-02-20 07:17:05 +03:00
|
|
|
panic("isa_dmafinished: impossible request");
|
1993-10-14 08:22:57 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check that the terminal count was reached */
|
|
|
|
if ((chan & 4) == 0)
|
1996-02-22 09:21:48 +03:00
|
|
|
dma_finished |= inb(DMA1_SR) & 0x0f;
|
1993-10-14 08:22:57 +03:00
|
|
|
else
|
1996-02-22 09:21:48 +03:00
|
|
|
dma_finished |= (inb(DMA2_SR) & 0x0f) << 4;
|
|
|
|
|
|
|
|
return ((dma_finished & (1 << chan)) != 0);
|
1996-02-20 07:17:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isa_dmadone(flags, addr, nbytes, chan)
|
|
|
|
int flags;
|
|
|
|
caddr_t addr;
|
|
|
|
vm_size_t nbytes;
|
|
|
|
int chan;
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef ISADMA_DEBUG
|
|
|
|
if (chan < 0 || chan > 7)
|
|
|
|
panic("isa_dmadone: impossible request");
|
|
|
|
#endif
|
1994-04-23 02:58:50 +04:00
|
|
|
|
1997-03-21 05:17:11 +03:00
|
|
|
isa_dmamask(chan);
|
|
|
|
|
1996-02-22 09:21:48 +03:00
|
|
|
if (!isa_dmafinished(chan))
|
1996-10-13 05:37:04 +04:00
|
|
|
printf("isa_dmadone: channel %d not finished\n", chan);
|
1996-02-22 09:21:48 +03:00
|
|
|
|
1994-11-04 22:25:34 +03:00
|
|
|
/* copy bounce buffer on read */
|
|
|
|
if (bounced[chan]) {
|
|
|
|
bcopy(dma_bounce[chan], addr, nbytes);
|
|
|
|
bounced[chan] = 0;
|
|
|
|
}
|
1993-10-14 08:22:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for problems with the address range of a DMA transfer
|
|
|
|
* (non-contiguous physical pages, outside of bus address space,
|
|
|
|
* crossing DMA page boundaries).
|
|
|
|
* Return true if special handling needed.
|
|
|
|
*/
|
|
|
|
int
|
1994-04-23 02:58:50 +04:00
|
|
|
isa_dmarangecheck(va, length, chan)
|
1993-10-14 08:22:57 +03:00
|
|
|
vm_offset_t va;
|
1993-10-22 23:24:14 +03:00
|
|
|
u_long length;
|
|
|
|
int chan;
|
1993-10-14 08:22:57 +03:00
|
|
|
{
|
|
|
|
vm_offset_t phys, priorpage = 0, endva;
|
1994-04-23 02:58:50 +04:00
|
|
|
u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
|
1993-10-14 08:22:57 +03:00
|
|
|
|
|
|
|
endva = round_page(va + length);
|
|
|
|
for (; va < endva ; va += NBPG) {
|
1995-04-10 17:08:28 +04:00
|
|
|
phys = trunc_page(pmap_extract(pmap_kernel(), va));
|
1993-10-14 08:22:57 +03:00
|
|
|
if (phys == 0)
|
1994-04-23 02:58:50 +04:00
|
|
|
panic("isa_dmacheck: no physical page present");
|
|
|
|
if (phys >= (1<<24))
|
1993-10-14 08:22:57 +03:00
|
|
|
return 1;
|
|
|
|
if (priorpage) {
|
|
|
|
if (priorpage + NBPG != phys)
|
|
|
|
return 1;
|
|
|
|
/* check if crossing a DMA page boundary */
|
|
|
|
if ((priorpage ^ phys) & dma_pgmsk)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
priorpage = phys;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1994-04-23 02:58:50 +04:00
|
|
|
|
|
|
|
/* head of queue waiting for physmem to become available */
|
|
|
|
struct buf isa_physmemq;
|
|
|
|
|
|
|
|
/* blocked waiting for resource to become free for exclusive use */
|
|
|
|
static isaphysmemflag;
|
|
|
|
/* if waited for and call requested when free (B_CALL) */
|
1996-04-30 00:02:32 +04:00
|
|
|
static void (*isaphysmemunblock) __P((void)); /* needs to be a list */
|
1994-04-23 02:58:50 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate contiguous physical memory for transfer, returning
|
|
|
|
* a *virtual* address to region. May block waiting for resource.
|
|
|
|
* (assumed to be called at splbio())
|
|
|
|
*/
|
|
|
|
caddr_t
|
1996-04-30 00:02:32 +04:00
|
|
|
isa_allocphysmem(ca, length, func)
|
|
|
|
caddr_t ca;
|
|
|
|
unsigned length;
|
|
|
|
void (*func) __P((void));
|
|
|
|
{
|
1994-04-23 02:58:50 +04:00
|
|
|
|
|
|
|
isaphysmemunblock = func;
|
|
|
|
while (isaphysmemflag & B_BUSY) {
|
|
|
|
isaphysmemflag |= B_WANTED;
|
|
|
|
sleep((caddr_t)&isaphysmemflag, PRIBIO);
|
|
|
|
}
|
|
|
|
isaphysmemflag |= B_BUSY;
|
|
|
|
|
|
|
|
return((caddr_t)isaphysmem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free contiguous physical memory used for transfer.
|
|
|
|
* (assumed to be called at splbio())
|
|
|
|
*/
|
|
|
|
void
|
1996-04-30 00:02:32 +04:00
|
|
|
isa_freephysmem(va, length)
|
|
|
|
caddr_t va;
|
|
|
|
unsigned length;
|
|
|
|
{
|
1994-04-23 02:58:50 +04:00
|
|
|
|
|
|
|
isaphysmemflag &= ~B_BUSY;
|
|
|
|
if (isaphysmemflag & B_WANTED) {
|
|
|
|
isaphysmemflag &= B_WANTED;
|
|
|
|
wakeup((caddr_t)&isaphysmemflag);
|
|
|
|
if (isaphysmemunblock)
|
|
|
|
(*isaphysmemunblock)();
|
|
|
|
}
|
|
|
|
}
|