#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: si.c,v 1.65 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/kmem.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/bus.h>
#include <machine/dvma.h>
#include <dev/ic/ncr5380reg.h>
#include <dev/ic/ncr5380var.h>
#include "sireg.h"
#include "sivar.h"
#define MIN_DMA_LEN 128
int si_debug = 0;
#ifdef DEBUG
#endif
int si_dma_intr_timo = 500;
static void si_minphys(struct buf *);
void
si_attach(struct si_softc *sc)
{
struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
volatile struct si_regs *regs = sc->sc_regs;
int i;
ncr_sc->sc_no_disconnect =
(sc->sc_options & SI_NO_DISCONNECT);
ncr_sc->sc_parity_disable =
(sc->sc_options & SI_NO_PARITY_CHK) >> 8;
if (sc->sc_options & SI_FORCE_POLLING)
ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
#if 1
if (sc->sc_options & SI_DISABLE_DMA) {
ncr_sc->sc_dma_alloc = NULL;
}
#endif
ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
ncr_sc->sci_r0 = ®s->sci.sci_r0;
ncr_sc->sci_r1 = ®s->sci.sci_r1;
ncr_sc->sci_r2 = ®s->sci.sci_r2;
ncr_sc->sci_r3 = ®s->sci.sci_r3;
ncr_sc->sci_r4 = ®s->sci.sci_r4;
ncr_sc->sci_r5 = ®s->sci.sci_r5;
ncr_sc->sci_r6 = ®s->sci.sci_r6;
ncr_sc->sci_r7 = ®s->sci.sci_r7;
ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
i = SCI_OPENINGS * sizeof(struct si_dma_handle);
sc->sc_dma = kmem_alloc(i, KM_SLEEP);
for (i = 0; i < SCI_OPENINGS; i++)
sc->sc_dma[i].dh_flags = 0;
ncr_sc->sc_channel.chan_id = 7;
ncr_sc->sc_adapter.adapt_minphys = si_minphys;
ncr5380_attach(ncr_sc);
}
static void
si_minphys(struct buf *bp)
{
if (bp->b_bcount > MAX_DMA_LEN) {
#ifdef DEBUG
if (si_debug) {
printf("%s len = 0x%x.\n", __func__, bp->b_bcount);
Debugger();
}
#endif
bp->b_bcount = MAX_DMA_LEN;
}
minphys(bp);
}
#define CSR_WANT (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR )
int
si_intr(void *arg)
{
struct si_softc *sc = arg;
volatile struct si_regs *si = sc->sc_regs;
int dma_error, claimed;
u_short csr;
claimed = 0;
dma_error = 0;
csr = si->si_csr;
NCR_TRACE("si_intr: csr=0x%x\n", csr);
if (csr & SI_CSR_DMA_CONFLICT) {
dma_error |= SI_CSR_DMA_CONFLICT;
printf("%s: DMA conflict\n", __func__);
}
if (csr & SI_CSR_DMA_BUS_ERR) {
dma_error |= SI_CSR_DMA_BUS_ERR;
printf("%s: DMA bus error\n", __func__);
}
if (dma_error) {
if (sc->ncr_sc.sc_state & NCR_DOINGDMA)
sc->ncr_sc.sc_state |= NCR_ABORTING;
csr |= SI_CSR_DMA_IP;
}
if (csr & (SI_CSR_SBC_IP | SI_CSR_DMA_IP)) {
claimed = ncr5380_intr(&sc->ncr_sc);
#ifdef DEBUG
if (!claimed) {
printf("%s: spurious from SBC\n", __func__);
if (si_debug & 4)
Debugger();
}
#endif
claimed = 1;
}
return claimed;
}
void
si_dma_alloc(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct scsipi_xfer *xs = sr->sr_xs;
struct si_dma_handle *dh;
int i, xlen;
void *addr;
#ifdef DIAGNOSTIC
if (sr->sr_dma_hand != NULL)
panic("%s: already have DMA handle", __func__);
#endif
addr = ncr_sc->sc_dataptr;
xlen = ncr_sc->sc_datalen;
if (((vaddr_t)addr & 1) || (xlen & 1)) {
printf("%s: misaligned.\n", __func__);
return;
}
if (xlen < MIN_DMA_LEN)
panic("%s: xlen=0x%x", __func__, xlen);
if (xlen > MAX_DMA_LEN) {
printf("%s: excessive xlen=0x%x\n", __func__, xlen);
Debugger();
ncr_sc->sc_datalen = xlen = MAX_DMA_LEN;
}
for (i = 0; i < SCI_OPENINGS; i++) {
if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
goto found;
}
panic("si: no free DMA handles.");
found:
dh = &sc->sc_dma[i];
dh->dh_flags = SIDH_BUSY;
if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, addr, xlen, NULL,
BUS_DMA_NOWAIT) != 0)
panic("%s: can't load dmamap", device_xname(ncr_sc->sc_dev));
dh->dh_dmaaddr = sc->sc_dmap->dm_segs[0].ds_addr;
dh->dh_dmalen = xlen;
if (xs->xs_control & XS_CTL_DATA_OUT)
dh->dh_flags |= SIDH_OUT;
bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, dh->dh_dmalen,
(dh->dh_flags & SIDH_OUT) == 0 ?
BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
#if 0
if (xs->bp && (xs->bp->b_flags & B_PHYS))
dh->dh_flags |= SIDH_PHYS;
#endif
sr->sr_dma_hand = dh;
return;
}
void
si_dma_free(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;
#ifdef DIAGNOSTIC
if (dh == NULL)
panic("%s: no DMA handle", __func__);
#endif
if (ncr_sc->sc_state & NCR_DOINGDMA)
panic("%s: free while in progress", __func__);
if (dh->dh_flags & SIDH_BUSY) {
bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, dh->dh_dmalen,
(dh->dh_flags & SIDH_OUT) == 0 ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
dh->dh_dmaaddr = 0;
dh->dh_flags = 0;
}
sr->sr_dma_hand = NULL;
}
#define CSR_MASK (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)
#define POLL_TIMO 50000
void
si_dma_poll(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
volatile struct si_regs *si = sc->sc_regs;
int tmo;
if (ncr_sc->sc_state & NCR_ABORTING)
return;
tmo = POLL_TIMO;
for (;;) {
if (si->si_csr & CSR_MASK)
break;
if (--tmo <= 0) {
printf("si: DMA timeout (while polling)\n");
sr->sr_flags |= SR_OVERDUE;
break;
}
delay(100);
}
NCR_TRACE("si_dma_poll: waited %d\n",
POLL_TIMO - tmo);
#ifdef DEBUG
if (si_debug & 2) {
printf("%s: done, csr=0x%x\n", __func__, si->si_csr);
}
#endif
}