#define DMA_4CSCD
#define NO_PROG_WIDTH
#define ISA_MODE
#include <sys/types.h>
#include <sys/cpuvar.h>
#include <sys/disp.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/dma_engine.h>
#include <sys/dma_i8237A.h>
#if defined(DEBUG)
#include <sys/promif.h>
static int i8237debug = 0;
#define dprintf(x) if (i8237debug) (void)prom_printf x
#else
#define dprintf(x)
#endif
extern int EISA_chaining;
static kmutex_t dma_engine_lock;
static struct d37A_chan_reg_addr chan_addr[] = { D37A_BASE_REGS_VALUES };
static ushort_t d37A_chnl_path[] = {
DMAE_PATH_8,
DMAE_PATH_8,
DMAE_PATH_8,
DMAE_PATH_8,
0,
DMAE_PATH_16,
DMAE_PATH_16,
DMAE_PATH_16};
static ushort_t d37A_chnl_mode[] = {
DMAE_TRANS_SNGL, DMAE_TRANS_SNGL, DMAE_TRANS_SNGL, DMAE_TRANS_SNGL,
#ifdef DMA_4CSCD
DMAE_TRANS_CSCD,
#else
DMAE_TRANS_SNGL,
#endif
DMAE_TRANS_SNGL, DMAE_TRANS_SNGL, DMAE_TRANS_SNGL};
#ifdef DMA_BUF_CHAIN
static ddi_dma_cookie_t *d37A_next_cookie[] =
{0, 0, 0, 0, 0, 0, 0, 0};
#endif
#ifdef DMA_INTR
static uint_t d37A_intr(caddr_t);
#endif
static int d37A_set_mode(struct ddi_dmae_req *, int);
static int d37A_write_addr(ulong_t, int);
static ulong_t d37A_read_addr(int);
static int d37A_write_count(long, int);
static long d37A_read_count(int);
#ifdef DMA_BUF_CHAIN
static void dEISA_setchain(ddi_dma_cookie_t *cp, int chnl);
#endif
int
d37A_init(dev_info_t *dip)
{
#ifdef DMA_INTR
ddi_iblock_cookie_t iblk_cookie = 0;
int error;
if ((error = ddi_add_intr(dip, (uint_t)0, &iblk_cookie,
(ddi_idevice_cookie_t *)0, d37A_intr, (caddr_t)NULL)) !=
DDI_SUCCESS) {
if (error != DDI_INTR_NOTFOUND)
cmn_err(CE_WARN, "!d37A_init: cannot add dma intr\n");
EISA_chaining = 0;
}
mutex_init(&dma_engine_lock, NULL, MUTEX_DRIVER, (void *)iblk_cookie);
#else
mutex_init(&dma_engine_lock, NULL, MUTEX_DRIVER, NULL);
#endif
return (DDI_SUCCESS);
}
int
d37A_dma_valid(int chnl)
{
#ifdef DMA_4CSCD
if (chnl == 4)
return (0);
#endif
return (1);
}
void
d37A_dma_release(int chnl)
{
#ifdef DMA_4CSCD
if (chnl == 4)
return;
#endif
d37A_chnl_mode[chnl] = DMAE_TRANS_SNGL;
}
void
d37A_dma_disable(int chnl)
{
dprintf(("d37A_dma_disable: chnl=%d mask_reg=0x%x\n",
chnl, chan_addr[chnl].mask_reg));
outb(chan_addr[chnl].mask_reg, (chnl & 3) | DMA_SETMSK);
}
void
d37A_dma_enable(int chnl)
{
dprintf(("d37A_dma_enable: chnl=%d mask_reg=0x%x val=0x%x\n",
chnl, chan_addr[chnl].mask_reg, chnl & 3));
outb(chan_addr[chnl].mask_reg, chnl & 3);
}
uchar_t
d37A_get_best_mode(struct ddi_dmae_req *dmaereqp)
{
return (DMAE_CYCLES_2);
}
#ifdef DMA_INTR
static uint_t
d37A_intr(caddr_t arg)
{
int chnl, istate, nstate;
uint_t mask;
if ((istate = (inb(EISA_DMAIS) & 0xef)) != 0) {
chnl = 0;
nstate = istate;
mutex_enter(&dma_engine_lock);
do {
if (istate & 1) {
dEISA_setchain(d37A_next_cookie[chnl], chnl);
#ifdef DEBUG
if (chnl < 4)
mask = inb(DMAC1_ALLMASK) >> (chnl);
else
mask = inb(DMAC2_ALLMASK) >> (chnl - 4);
if (mask & 1)
prom_printf("eisa: dma buffer chaining failure chnl %d!\n", chnl);
#endif
}
chnl++;
istate >>= 1;
} while (istate);
chnl = 0;
do {
if ((nstate & 1) && d37A_next_cookie[chnl])
d37A_next_cookie[chnl] = _dmae_nxcookie(chnl);
chnl++;
nstate >>= 1;
} while (nstate);
mutex_exit(&dma_engine_lock);
return (DDI_INTR_CLAIMED);
}
return (DDI_INTR_UNCLAIMED);
}
#endif
#ifdef DMA_BUF_CHAIN
static void
dEISA_setchain(ddi_dma_cookie_t *cp, int chnl)
{
if (cp) {
dprintf(("dEISA_setchain: chnl=%d next_addr=%x count=%lx\n",
chnl, cp->dmac_address, cp->dmac_size));
(void) d37A_write_addr(cp->dmac_address, chnl);
(void) d37A_write_count(cp->dmac_size, chnl);
outb(chan_addr[chnl].scm_reg, chnl | EISA_ENCM | EISA_CMOK);
} else {
outb(chan_addr[chnl].scm_reg, chnl);
dprintf(("dEISA_setchain: chnl=%d end\n", chnl));
}
}
#endif
int
d37A_prog_chan(struct ddi_dmae_req *dmaereqp, ddi_dma_cookie_t *cp, int chnl)
{
if (d37A_chnl_mode[chnl] == DMAE_TRANS_CSCD) {
dprintf(("d37A_prog_chan err: chnl=%d in cascade mode\n",
chnl));
return (DDI_FAILURE);
}
#ifndef MEM_TO_MEM
if (dmaereqp && dmaereqp->der_dest == DMAE_DEST_MEM) {
dprintf(("d37A_prog_chan err: memory to memory mode not supported.\n"));
return (DDI_FAILURE);
}
#endif
dprintf(("d37A_prog_chan: chnl=%d dmaereq=%p\n",
chnl, (void *)dmaereqp));
if (dmaereqp) {
switch (chnl) {
case DMAE_CH0:
case DMAE_CH1:
case DMAE_CH2:
case DMAE_CH3:
#ifdef NO_PROG_WIDTH
if (dmaereqp->der_path &&
dmaereqp->der_path != DMAE_PATH_8) {
dprintf(("d37A_prog_chan err: chnl %d not programmed.\n", chnl));
return (DDI_FAILURE);
}
#endif
break;
#ifndef DMA_4CSCD
case DMAE_CH4:
#endif
case DMAE_CH5:
case DMAE_CH6:
case DMAE_CH7:
#ifdef NO_PROG_WIDTH
if (dmaereqp->der_path &&
dmaereqp->der_path != DMAE_PATH_16) {
dprintf(("d37A_prog_chan err: chnl %d not programmed.\n", chnl));
return (DDI_FAILURE);
}
#endif
break;
default:
dprintf(("d37A_prog_chan err: chnl %d not programmed.\n", chnl));
return (DDI_FAILURE);
}
} else
chnl &= 3;
mutex_enter(&dma_engine_lock);
d37A_dma_disable(chnl);
if (dmaereqp)
(void) d37A_set_mode(dmaereqp, chnl);
if (cp) {
(void) d37A_write_addr(cp->dmac_address, chnl);
(void) d37A_write_count(cp->dmac_size, chnl);
#ifdef DMA_BUF_CHAIN
if (dmaereqp && dmaereqp->der_bufprocess == DMAE_BUF_CHAIN &&
(d37A_next_cookie[chnl] = _dmae_nxcookie(chnl))) {
drv_usecwait(10);
outb(chan_addr[chnl].scm_reg, chnl | EISA_ENCM);
drv_usecwait(15);
dEISA_setchain(d37A_next_cookie[chnl], chnl);
d37A_next_cookie[chnl] = _dmae_nxcookie(chnl);
}
#endif
}
mutex_exit(&dma_engine_lock);
return (DDI_SUCCESS);
}
int
d37A_dma_swsetup(struct ddi_dmae_req *dmaereqp, ddi_dma_cookie_t *cp, int chnl)
{
if (d37A_chnl_mode[chnl] == DMAE_TRANS_CSCD) {
dprintf(("d37A_dma_swsetup err: chnl %d not programmed\n",
chnl));
return (DDI_FAILURE);
}
dprintf(("d37A_dma_swsetup: chnl=%d dmaereq=%p.\n",
chnl, (void *)dmaereqp));
if (dmaereqp->der_trans != DMAE_TRANS_BLCK)
dmaereqp->der_trans = DMAE_TRANS_BLCK;
switch (chnl) {
case DMAE_CH0:
case DMAE_CH1:
case DMAE_CH2:
case DMAE_CH3:
#ifdef NO_PROG_WIDTH
if (dmaereqp->der_path && dmaereqp->der_path != DMAE_PATH_8) {
dprintf(("d37A_dma_swsetup err: chnl %d not programmed.\n", chnl));
return (DDI_FAILURE);
}
#endif
break;
#ifndef DMA_4CSCD
case DMAE_CH4:
#endif
case DMAE_CH5:
case DMAE_CH6:
case DMAE_CH7:
#ifdef NO_PROG_WIDTH
if (dmaereqp->der_path && dmaereqp->der_path != DMAE_PATH_16) {
dprintf(("d37A_dma_swsetup err: chnl %d not programmed.\n", chnl));
return (DDI_FAILURE);
}
#endif
break;
default:
dprintf(("d37A_dma_swsetup err: chnl %d not set up.\n", chnl));
return (DDI_FAILURE);
};
mutex_enter(&dma_engine_lock);
d37A_dma_disable(chnl);
(void) d37A_set_mode(dmaereqp, chnl);
(void) d37A_write_addr(cp->dmac_address, chnl);
(void) d37A_write_count(cp->dmac_size, chnl);
#ifdef DMA_BUF_CHAIN
if (dmaereqp->der_bufprocess == DMAE_BUF_CHAIN &&
(d37A_next_cookie[chnl] = _dmae_nxcookie(chnl))) {
outb(chan_addr[chnl].scm_reg, chnl | EISA_ENCM);
dEISA_setchain(d37A_next_cookie[chnl], chnl);
d37A_next_cookie[chnl] = _dmae_nxcookie(chnl);
}
#endif
mutex_exit(&dma_engine_lock);
return (DDI_SUCCESS);
}
void
d37A_dma_swstart(int chnl)
{
dprintf(("d37A_dma_swstart: chnl=%d\n", chnl));
mutex_enter(&dma_engine_lock);
d37A_dma_enable(chnl);
outb(chan_addr[chnl].reqt_reg, DMA_SETMSK | chnl);
mutex_exit(&dma_engine_lock);
}
void
d37A_dma_stop(int chnl)
{
dprintf(("d37A_dma_stop: chnl=%d\n", chnl));
mutex_enter(&dma_engine_lock);
d37A_dma_disable(chnl);
outb(chan_addr[chnl].reqt_reg, chnl & 3);
mutex_exit(&dma_engine_lock);
}
void
d37A_get_chan_stat(int chnl, ulong_t *addressp, int *countp)
{
ulong_t taddr;
int tcount;
mutex_enter(&dma_engine_lock);
taddr = d37A_read_addr(chnl);
tcount = d37A_read_count(chnl);
mutex_exit(&dma_engine_lock);
if (addressp)
*addressp = taddr;
if (countp)
*countp = tcount;
dprintf(("d37A_get_chan_stat: chnl=%d address=%lx count=%x\n",
chnl, taddr, tcount));
}
static int
d37A_set_mode(struct ddi_dmae_req *dmaereqp, int chnl)
{
uchar_t mode = 0, emode = 0;
#ifdef ISA_MODE
#if defined(lint)
emode = emode;
#endif
mode = chnl & 3;
switch (dmaereqp->der_command) {
case DMAE_CMD_READ:
mode |= DMAMODE_READ;
break;
case DMAE_CMD_WRITE:
mode |= DMAMODE_WRITE;
break;
case DMAE_CMD_VRFY:
mode |= DMAMODE_VERF;
break;
case DMAE_CMD_TRAN:
mode |= 0x0C;
break;
default:
return (DDI_FAILURE);
}
if (dmaereqp->der_bufprocess == DMAE_BUF_AUTO)
mode |= DMAMODE_AUTO;
if (dmaereqp->der_step == DMAE_STEP_DEC)
mode |= DMAMODE_DECR;
switch (dmaereqp->der_trans) {
case DMAE_TRANS_SNGL:
mode |= DMAMODE_SINGLE;
break;
case DMAE_TRANS_BLCK:
mode |= DMAMODE_BLOCK;
break;
case DMAE_TRANS_DMND:
break;
case DMAE_TRANS_CSCD:
mode |= DMAMODE_CASC;
break;
default:
return (DDI_FAILURE);
}
d37A_chnl_mode[chnl] = dmaereqp->der_trans;
dprintf(("d37A_set_mode: chnl=%d mode_reg=0x%x mode=0x%x\n",
chnl, chan_addr[chnl].mode_reg, mode));
outb(chan_addr[chnl].mode_reg, mode);
#endif
#ifdef EISA_EXT_MODE
emode = chnl & 3;
d37A_chnl_path[chnl] = dmaereqp->der_path;
switch (dmaereqp->der_path) {
case DMAE_PATH_8:
break;
case DMAE_PATH_16:
emode |= EISA_DMA_16;
break;
case DMAE_PATH_32:
emode |= EISA_DMA_32;
break;
case DMAE_PATH_16B:
emode |= EISA_DMA_16B;
break;
default:
switch (chnl) {
case DMAE_CH0:
case DMAE_CH1:
case DMAE_CH2:
case DMAE_CH3:
d37A_chnl_path[chnl] = DMAE_PATH_8;
break;
case DMAE_CH5:
case DMAE_CH6:
case DMAE_CH7:
d37A_chnl_path[chnl] = DMAE_PATH_16;
emode |= EISA_DMA_16;
break;
}
}
emode |= (dmaereqp->der_cycles & 3) << 4;
outb(chan_addr[chnl].emode_reg, emode);
dprintf(("d37A_set_mode: chnl=%d em_reg=0x%x emode=0x%x\n",
chnl, chan_addr[chnl].emode_reg, emode));
#endif
return (DDI_SUCCESS);
}
static int
d37A_write_addr(ulong_t paddress, int chnl)
{
uchar_t *adr_byte;
dprintf(("d37A_write_addr: chnl=%d address=%lx\n", chnl, paddress));
switch (d37A_chnl_path[chnl]) {
case DMAE_PATH_8:
case DMAE_PATH_16B:
case DMAE_PATH_32:
break;
case DMAE_PATH_16:
paddress = (paddress & ~0x1ffff) | ((paddress & 0x1ffff) >> 1);
break;
default:
return (DDI_FAILURE);
}
kpreempt_disable();
outb(chan_addr[chnl].ff_reg, 0);
adr_byte = (uchar_t *)&paddress;
outb(chan_addr[chnl].addr_reg, adr_byte[0]);
outb(chan_addr[chnl].addr_reg, adr_byte[1]);
outb(chan_addr[chnl].page_reg, adr_byte[2]);
#ifdef ADDR_32
outb(chan_addr[chnl].hpage_reg, adr_byte[3]);
#endif
kpreempt_enable();
return (DDI_SUCCESS);
}
static ulong_t
d37A_read_addr(int chnl)
{
ulong_t paddress = 0;
uchar_t *adr_byte;
kpreempt_disable();
adr_byte = (uchar_t *)&paddress;
outb(chan_addr[chnl].ff_reg, 0);
adr_byte[0] = inb(chan_addr[chnl].addr_reg);
adr_byte[1] = inb(chan_addr[chnl].addr_reg);
adr_byte[2] = inb(chan_addr[chnl].page_reg);
#ifdef ADDR_32
adr_byte[3] = inb(chan_addr[chnl].hpage_reg);
#endif
kpreempt_enable();
switch (d37A_chnl_path[chnl]) {
case DMAE_PATH_8:
case DMAE_PATH_16B:
case DMAE_PATH_32:
break;
case DMAE_PATH_16:
paddress = (paddress & ~0x1ffff) | ((paddress & 0x0ffff) << 1);
break;
default:
return ((ulong_t)DDI_FAILURE);
}
dprintf(("d37A_read_addr: chnl=%d address=%lx.\n", chnl, paddress));
return (paddress);
}
static int
d37A_write_count(long count, int chnl)
{
uchar_t *count_byte;
dprintf(("d37A_write_count: chnl=%d count=0x%lx\n", chnl, count));
switch (d37A_chnl_path[chnl]) {
case DMAE_PATH_16:
count >>= 1;
case DMAE_PATH_8:
case DMAE_PATH_16B:
case DMAE_PATH_32:
--count;
break;
default:
return (DDI_FAILURE);
}
kpreempt_disable();
outb(chan_addr[chnl].ff_reg, 0);
count_byte = (uchar_t *)&count;
outb(chan_addr[chnl].cnt_reg, count_byte[0]);
outb(chan_addr[chnl].cnt_reg, count_byte[1]);
#ifdef COUNT_24
outb(chan_addr[chnl].hcnt_reg, count_byte[2]);
#endif
kpreempt_enable();
return (DDI_SUCCESS);
}
static long
d37A_read_count(int chnl)
{
long count = 0;
uchar_t *count_byte;
kpreempt_disable();
count_byte = (uchar_t *)&count;
outb(chan_addr[chnl].ff_reg, 0);
count_byte[0] = inb(chan_addr[chnl].cnt_reg);
count_byte[1] = inb(chan_addr[chnl].cnt_reg);
#ifdef COUNT_24
count_byte[2] = inb(chan_addr[chnl].hcnt_reg);
#endif
#ifdef COUNT_24
if ((ulong_t)count == 0xffffff)
#else
if ((ulong_t)count == 0xffff)
#endif
count = -1;
kpreempt_enable();
switch (d37A_chnl_path[chnl]) {
case DMAE_PATH_8:
case DMAE_PATH_16B:
case DMAE_PATH_32:
++count;
break;
case DMAE_PATH_16:
count = (count + 1) << 1;
break;
}
dprintf(("d37A_read_count: chnl=%d count=0x%lx\n", chnl, count));
return (count);
}