#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dma.c,v 1.29 2023/01/06 10:28:28 tsutsui Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <machine/cpu.h>
#include <machine/iomap.h>
#include <machine/dma.h>
#include <machine/intr.h>
#define NDMA_DEV 10
typedef struct dma_entry {
TAILQ_ENTRY(dma_entry) entries;
void (*call_func)(void *);
void (*int_func)(void *);
void *softc;
int *lock_stat;
} DMA_ENTRY;
static DMA_ENTRY dmatable[NDMA_DEV];
static TAILQ_HEAD(freehead, dma_entry) dma_free;
static TAILQ_HEAD(acthead, dma_entry) dma_active;
static int must_init = 1;
int cdmaint(void *, int);
static void st_dma_init(void);
static void
st_dma_init(void)
{
int i;
TAILQ_INIT(&dma_free);
TAILQ_INIT(&dma_active);
for (i = 0; i < NDMA_DEV; i++)
TAILQ_INSERT_HEAD(&dma_free, &dmatable[i], entries);
if (intr_establish(7, USER_VEC, 0, cdmaint, NULL) == NULL)
panic("st_dma_init: Can't establish interrupt");
}
int
st_dmagrab(dma_farg int_func, dma_farg call_func, void *softc, int *lock_stat,
int rcaller, kmutex_t *interlock)
{
int s;
DMA_ENTRY *req;
if (must_init) {
st_dma_init();
must_init = 0;
}
*lock_stat = DMA_LOCK_REQ;
s = splhigh();
if ((req = TAILQ_FIRST(&dma_free)) == NULL)
panic("st_dmagrab: Too many outstanding requests");
TAILQ_REMOVE(&dma_free, req, entries);
req->call_func = call_func;
req->int_func = int_func;
req->softc = softc;
req->lock_stat = lock_stat;
TAILQ_INSERT_TAIL(&dma_active, req, entries);
if (TAILQ_FIRST(&dma_active) != req) {
if (call_func == NULL) {
do {
mtsleep(&dma_active, PRIBIO, "dmalck", 0,
interlock);
} while (*req->lock_stat != DMA_LOCK_GRANT);
splx(s);
return 1;
}
splx(s);
return 0;
}
splx(s);
*lock_stat = DMA_LOCK_GRANT;
if (rcaller || (call_func == NULL)) {
return 1;
}
(*call_func)(softc);
return 0;
}
void
st_dmafree(void *softc, int *lock_stat)
{
int s;
DMA_ENTRY *req;
s = splhigh();
if ((req = TAILQ_FIRST(&dma_active)) == NULL)
panic("st_dmafree: empty active queue");
if (req->softc != softc)
printf("Caller of st_dmafree is not lock-owner!\n");
*lock_stat = 0;
TAILQ_REMOVE(&dma_active, req, entries);
TAILQ_INSERT_HEAD(&dma_free, req, entries);
if ((req = TAILQ_FIRST(&dma_active)) != NULL) {
*req->lock_stat = DMA_LOCK_GRANT;
if (req->call_func == NULL)
wakeup((void *)&dma_active);
else {
add_sicallback((si_farg)req->call_func, req->softc, 0);
}
}
splx(s);
}
int
st_dmawanted(void)
{
return TAILQ_NEXT(TAILQ_FIRST(&dma_active), entries) != NULL;
}
int
cdmaint(void *unused, int sr)
{
dma_farg int_func;
void *softc;
if (TAILQ_FIRST(&dma_active) != NULL) {
int_func = TAILQ_FIRST(&dma_active)->int_func;
softc = TAILQ_FIRST(&dma_active)->softc;
add_sicallback((si_farg)int_func, softc, 0);
return 1;
}
return 0;
}
void
st_dmaaddr_set(void *address)
{
u_long ad = (u_long)address;
DMA->dma_addr[AD_LOW ] = (ad ) & 0xff;
DMA->dma_addr[AD_MID ] = (ad >> 8) & 0xff;
DMA->dma_addr[AD_HIGH] = (ad >>16) & 0xff;
}
u_long
st_dmaaddr_get(void)
{
u_long ad = 0;
ad = (DMA->dma_addr[AD_LOW ] & 0xff);
ad |= (DMA->dma_addr[AD_MID ] & 0xff) << 8;
ad |= (DMA->dma_addr[AD_HIGH] & 0xff) <<16;
return ad;
}
void
st_dmacomm(int mode, int nblk)
{
DMA->dma_mode = mode;
DMA->dma_mode = mode ^ DMA_WRBIT;
DMA->dma_mode = mode;
DMA->dma_data = nblk;
delay(2);
DMA->dma_mode = DMA_SCREG | (mode & DMA_WRBIT);
}