#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: si_obio.c,v 1.39 2024/12/20 23:52:00 tsutsui Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipi_debug.h>
#include <dev/scsipi/scsiconf.h>
#include <machine/autoconf.h>
#include <machine/dvma.h>
#include <dev/ic/ncr5380reg.h>
#include <dev/ic/ncr5380var.h>
#include "sireg.h"
#include "sivar.h"
#include "am9516.h"
#define UDC_WAIT_USEC 5
void si_obio_dma_setup(struct ncr5380_softc *);
void si_obio_dma_start(struct ncr5380_softc *);
void si_obio_dma_eop(struct ncr5380_softc *);
void si_obio_dma_stop(struct ncr5380_softc *);
static void si_obio_reset(struct ncr5380_softc *);
static inline void si_obio_udc_write(volatile struct si_regs *, int, int);
static inline int si_obio_udc_read(volatile struct si_regs *, int);
static int si_obio_match(device_t, cfdata_t, void *);
static void si_obio_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(si_obio, sizeof(struct si_softc),
si_obio_match, si_obio_attach, NULL, NULL);
int si_obio_options = 0x0f;
static int
si_obio_match(device_t parent, cfdata_t cf, void *aux)
{
struct confargs *ca = aux;
if (bus_peek(ca->ca_bustype, ca->ca_paddr + 1, 1) == -1)
return 0;
if (ca->ca_intpri == -1)
ca->ca_intpri = 2;
return 1;
}
static void
si_obio_attach(device_t parent, device_t self, void *args)
{
struct si_softc *sc = device_private(self);
struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
struct cfdata *cf = device_cfdata(self);
struct confargs *ca = args;
ncr_sc->sc_dev = self;
sc->sc_bst = ca->ca_bustag;
sc->sc_dmat = ca->ca_dmatag;
if (bus_space_map(sc->sc_bst, ca->ca_paddr, sizeof(struct si_regs), 0,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
sc->sc_regs = bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
BUS_DMA_NOWAIT, &sc->sc_dmap) != 0) {
aprint_error(": can't create DMA map\n");
return;
}
if (cf->cf_flags)
sc->sc_options = cf->cf_flags;
else
sc->sc_options = si_obio_options;
aprint_normal(": options=0x%x\n", sc->sc_options);
sc->sc_adapter_type = ca->ca_bustype;
ncr_sc->sc_pio_out = ncr5380_pio_out;
ncr_sc->sc_pio_in = ncr5380_pio_in;
ncr_sc->sc_dma_alloc = si_dma_alloc;
ncr_sc->sc_dma_free = si_dma_free;
ncr_sc->sc_dma_setup = si_obio_dma_setup;
ncr_sc->sc_dma_start = si_obio_dma_start;
ncr_sc->sc_dma_poll = si_dma_poll;
ncr_sc->sc_dma_eop = si_obio_dma_eop;
ncr_sc->sc_dma_stop = si_obio_dma_stop;
ncr_sc->sc_intr_on = NULL;
ncr_sc->sc_intr_off = NULL;
sc->sc_dmacmd = dvma_malloc(sizeof(struct udc_table));
isr_add_autovect(si_intr, (void *)sc, ca->ca_intpri);
si_obio_reset(ncr_sc);
si_attach(sc);
}
static void
si_obio_reset(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
volatile struct si_regs *si = sc->sc_regs;
#ifdef DEBUG
if (si_debug) {
printf("%s\n", __func__);
}
#endif
si->si_csr = 0;
delay(10);
si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN;
delay(10);
si->fifo_count = 0;
}
static inline void
si_obio_udc_write(volatile struct si_regs *si, int regnum, int value)
{
si->udc_addr = regnum;
delay(UDC_WAIT_USEC);
si->udc_data = value;
delay(UDC_WAIT_USEC);
}
static inline int
si_obio_udc_read(volatile struct si_regs *si, int regnum)
{
int value;
si->udc_addr = regnum;
delay(UDC_WAIT_USEC);
value = si->udc_data;
delay(UDC_WAIT_USEC);
return value;
}
void
si_obio_dma_setup(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct si_dma_handle *dh = sr->sr_dma_hand;
volatile struct si_regs *si = sc->sc_regs;
struct udc_table *cmd;
long data_pa, cmd_pa;
int xlen;
data_pa = dh->dh_dmaaddr;
if (data_pa & 1)
panic("%s: bad pa=0x%lx", __func__, data_pa);
xlen = dh->dh_dmalen;
sc->sc_reqlen = xlen;
#ifdef DEBUG
if (si_debug & 2) {
printf("%s: dh=%p, pa=0x%lx, xlen=0x%x\n",
__func__, dh, data_pa, xlen);
}
#endif
si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
si->si_csr &= ~SI_CSR_FIFO_RES;
si->si_csr |= SI_CSR_FIFO_RES;
if (dh->dh_flags & SIDH_OUT) {
si->si_csr |= SI_CSR_SEND;
} else {
si->si_csr &= ~SI_CSR_SEND;
}
si->fifo_count = xlen;
si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
si->si_csr &= ~SI_CSR_FIFO_RES;
si->si_csr |= SI_CSR_FIFO_RES;
#ifdef DEBUG
if (si->fifo_count != xlen) {
printf("%s: fifo_count=0x%x, xlen=0x%x\n",
__func__, si->fifo_count, xlen);
Debugger();
}
#endif
cmd = sc->sc_dmacmd;
cmd->addrh = ((data_pa & 0xFF0000) >> 8) | UDC_ADDR_INFO;
cmd->addrl = data_pa & 0xFFFF;
cmd->count = xlen / 2;
cmd->cmrh = UDC_CMR_HIGH;
if (dh->dh_flags & SIDH_OUT) {
if (xlen & 1)
cmd->count++;
cmd->cmrl = UDC_CMR_LSEND;
cmd->rsel = UDC_RSEL_SEND;
} else {
cmd->cmrl = UDC_CMR_LRECV;
cmd->rsel = UDC_RSEL_RECV;
}
cmd_pa = dvma_kvtopa(cmd, BUS_OBIO);
si_obio_udc_write(si, UDC_ADR_CAR_HIGH, (cmd_pa & 0xff0000) >> 8);
si_obio_udc_write(si, UDC_ADR_CAR_LOW, (cmd_pa & 0xffff));
si_obio_udc_write(si, UDC_ADR_MODE, UDC_MODE);
si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_CIE);
}
void
si_obio_dma_start(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct si_dma_handle *dh = sr->sr_dma_hand;
volatile struct si_regs *si = sc->sc_regs;
int s;
#ifdef DEBUG
if (si_debug & 2) {
printf("%s: sr=%p\n", __func__, sr);
}
#endif
s = splhigh();
si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_STRT_CHN);
if (dh->dh_flags & SIDH_OUT) {
*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
SCI_CLR_INTR(ncr_sc);
*ncr_sc->sci_icmd = SCI_ICMD_DATA;
*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
*ncr_sc->sci_dma_send = 0;
} else {
*ncr_sc->sci_tcmd = PHASE_DATA_IN;
SCI_CLR_INTR(ncr_sc);
*ncr_sc->sci_icmd = 0;
*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
*ncr_sc->sci_irecv = 0;
}
splx(s);
ncr_sc->sc_state |= NCR_DOINGDMA;
#ifdef DEBUG
if (si_debug & 2) {
printf("%s: started, flags=0x%x\n",
__func__, ncr_sc->sc_state);
}
#endif
}
void
si_obio_dma_eop(struct ncr5380_softc *ncr_sc)
{
}
void
si_obio_dma_stop(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct si_dma_handle *dh = sr->sr_dma_hand;
volatile struct si_regs *si = sc->sc_regs;
int resid, ntrans, tmo, udc_cnt;
if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
#ifdef DEBUG
printf("%s: DMA not running\n", __func__);
#endif
return;
}
ncr_sc->sc_state &= ~NCR_DOINGDMA;
NCR_TRACE("si_dma_stop: top, csr=0x%x\n", si->si_csr);
*ncr_sc->sci_tcmd = PHASE_INVALID;
if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
sr->sr_xs->error = XS_DRIVER_STUFFUP;
ncr_sc->sc_state |= NCR_ABORTING;
si_obio_reset(ncr_sc);
goto out;
}
if (ncr_sc->sc_state & NCR_ABORTING)
goto out;
if ((dh->dh_flags & SIDH_OUT) == 0) {
tmo = 200000;
for (;;) {
if (si->si_csr & SI_CSR_FIFO_EMPTY)
break;
if (--tmo <= 0) {
printf("si: DMA FIFO did not empty, reset\n");
ncr_sc->sc_state |= NCR_ABORTING;
goto out;
}
delay(10);
}
}
resid = si->fifo_count & 0xFFFF;
ntrans = sc->sc_reqlen - resid;
#ifdef DEBUG
if (si_debug & 2) {
printf("%s: resid=0x%x ntrans=0x%x\n",
__func__, resid, ntrans);
}
#endif
if (ntrans < MIN_DMA_LEN) {
printf("si: fifo count: 0x%x\n", resid);
ncr_sc->sc_state |= NCR_ABORTING;
goto out;
}
if (ntrans > ncr_sc->sc_datalen)
panic("%s: excess transfer", __func__);
ncr_sc->sc_dataptr += ntrans;
ncr_sc->sc_datalen -= ntrans;
if ((dh->dh_flags & SIDH_OUT) == 0) {
if (ntrans & 1) {
NCR_TRACE("si_dma_stop: leftover 1 at 0x%x\n",
(int) ncr_sc->sc_dataptr - 1);
ncr_sc->sc_dataptr[-1] =
(si->fifo_data & 0xff00) >> 8;
goto out;
}
udc_cnt = si_obio_udc_read(si, UDC_ADR_COUNT);
if (((udc_cnt * 2) - resid) == 2) {
NCR_TRACE("si_dma_stop: leftover 2 at 0x%x\n",
(int) ncr_sc->sc_dataptr - 2);
ncr_sc->sc_dataptr[-2] =
(si->fifo_data & 0xff00) >> 8;
ncr_sc->sc_dataptr[-1] =
(si->fifo_data & 0x00ff);
}
}
out:
si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
si->fifo_count = 0;
si->si_csr &= ~SI_CSR_SEND;
si->si_csr &= ~SI_CSR_FIFO_RES;
si->si_csr |= SI_CSR_FIFO_RES;
*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
*ncr_sc->sci_icmd = 0;
}