#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: asc_ioasic.c,v 1.27 2022/05/03 20:52:31 andvar Exp $");
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/scsipi/scsi_message.h>
#include <dev/ic/ncr53c9xreg.h>
#include <dev/ic/ncr53c9xvar.h>
#include <dev/tc/tcvar.h>
#include <dev/tc/ioasicvar.h>
#include <dev/tc/ioasicreg.h>
struct asc_softc {
struct ncr53c9x_softc sc_ncr53c9x;
bus_space_tag_t sc_bst;
bus_space_handle_t sc_bsh;
bus_space_handle_t sc_scsi_bsh;
bus_dma_tag_t sc_dmat;
bus_dmamap_t sc_dmamap;
uint8_t **sc_dmaaddr;
size_t *sc_dmalen;
size_t sc_dmasize;
unsigned int sc_flags;
#define ASC_ISPULLUP 0x0001
#define ASC_DMAACTIVE 0x0002
#define ASC_MAPLOADED 0x0004
};
#define ASC_READ_REG(asc, reg) \
((uint8_t)bus_space_read_4((asc)->sc_bst, (asc)->sc_scsi_bsh, \
(reg) * sizeof(uint32_t)))
#define ASC_WRITE_REG(asc, reg, val) \
bus_space_write_4((asc)->sc_bst, (asc)->sc_scsi_bsh, \
(reg) * sizeof(uint32_t), (uint8_t)(val))
static int asc_ioasic_match(device_t, cfdata_t, void *);
static void asc_ioasic_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(asc_ioasic, sizeof(struct asc_softc),
asc_ioasic_match, asc_ioasic_attach, NULL, NULL);
static uint8_t asc_read_reg(struct ncr53c9x_softc *, int);
static void asc_write_reg(struct ncr53c9x_softc *, int, u_char);
static int asc_dma_isintr(struct ncr53c9x_softc *sc);
static void asc_ioasic_reset(struct ncr53c9x_softc *);
static int asc_ioasic_intr(struct ncr53c9x_softc *);
static int asc_ioasic_setup(struct ncr53c9x_softc *,
uint8_t **, size_t *, int, size_t *);
static void asc_ioasic_go(struct ncr53c9x_softc *);
static void asc_ioasic_stop(struct ncr53c9x_softc *);
static int asc_dma_isactive(struct ncr53c9x_softc *);
static struct ncr53c9x_glue asc_ioasic_glue = {
asc_read_reg,
asc_write_reg,
asc_dma_isintr,
asc_ioasic_reset,
asc_ioasic_intr,
asc_ioasic_setup,
asc_ioasic_go,
asc_ioasic_stop,
asc_dma_isactive,
NULL,
};
static int
asc_ioasic_match(device_t parent, cfdata_t cf, void *aux)
{
struct ioasicdev_attach_args *d = aux;
if (strncmp("asc", d->iada_modname, TC_ROM_LLEN))
return 0;
return 1;
}
static void
asc_ioasic_attach(device_t parent, device_t self, void *aux)
{
struct asc_softc *asc = device_private(self);
struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x;
struct ioasicdev_attach_args *d = aux;
struct ioasic_softc *isc = device_private(parent);
sc->sc_dev = self;
sc->sc_glue = &asc_ioasic_glue;
asc->sc_bst = isc->sc_bst;
asc->sc_bsh = isc->sc_bsh;
if (bus_space_subregion(asc->sc_bst, asc->sc_bsh,
IOASIC_SLOT_12_START, 0x100, &asc->sc_scsi_bsh)) {
aprint_error(": failed to map device registers\n");
return;
}
asc->sc_dmat = isc->sc_dmat;
if (bus_dmamap_create(asc->sc_dmat, PAGE_SIZE * 2,
2, PAGE_SIZE, PAGE_SIZE, BUS_DMA_NOWAIT,
&asc->sc_dmamap)) {
aprint_error(": failed to create DMA map\n");
return;
}
sc->sc_id = 7;
sc->sc_freq = 25000000;
sc->sc_freq /= 1000000;
ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_BIO,
ncr53c9x_intr, sc);
sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
sc->sc_cfg2 = NCRCFG2_SCSI2;
sc->sc_cfg3 = 0;
sc->sc_rev = NCR_VARIANT_NCR53C94;
sc->sc_minsync = (1000 / sc->sc_freq) * 5 / 4 ;
sc->sc_maxxfer = 64 * 1024;
sc->sc_adapter.adapt_minphys = minphys;
sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
ncr53c9x_attach(sc);
}
void
asc_ioasic_reset(struct ncr53c9x_softc *sc)
{
struct asc_softc *asc = (struct asc_softc *)sc;
uint32_t ssr;
ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR);
ssr &= ~IOASIC_CSR_DMAEN_SCSI;
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr);
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_SCSI_SCR, 0);
if (asc->sc_flags & ASC_MAPLOADED)
bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
}
#define TWOPAGE(a) (PAGE_SIZE*2 - ((a) & (PAGE_SIZE-1)))
int
asc_ioasic_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
int ispullup, size_t *dmasize)
{
struct asc_softc *asc = (struct asc_softc *)sc;
uint32_t ssr, scr, *p;
size_t size;
vaddr_t cp;
NCR_DMA(("%s: start %d@%p,%s\n", device_xname(sc->sc_dev),
*asc->sc_dmalen, *asc->sc_dmaaddr, ispullup ? "IN" : "OUT"));
size = uimin(*dmasize, TWOPAGE((size_t)*addr));
asc->sc_dmaaddr = addr;
asc->sc_dmalen = len;
asc->sc_dmasize = size;
asc->sc_flags = (ispullup) ? ASC_ISPULLUP : 0;
*dmasize = size;
ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR);
ssr &= ~IOASIC_CSR_DMAEN_SCSI;
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr);
if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap,
*addr, size, NULL , BUS_DMA_NOWAIT))
panic("%s: cannot allocate DMA address",
device_xname(sc->sc_dev));
cp = (vaddr_t)*addr;
if ((cp & 7) == 0) {
scr = 0;
}
else {
p = (uint32_t *)(cp & ~7);
scr = (cp & 7) >> 1;
asc->sc_dmamap->dm_segs[0].ds_addr &= ~7;
asc->sc_dmamap->dm_segs[0].ds_len += (cp & 6);
if ((asc->sc_flags & ASC_ISPULLUP) == 0) {
scr |= 4;
asc->sc_dmamap->dm_segs[0].ds_addr += 8;
}
bus_space_write_4(asc->sc_bst, asc->sc_bsh,
IOASIC_SCSI_SDR0, p[0]);
bus_space_write_4(asc->sc_bst, asc->sc_bsh,
IOASIC_SCSI_SDR1, p[1]);
}
bus_space_write_4(asc->sc_bst, asc->sc_bsh,
IOASIC_SCSI_DMAPTR,
IOASIC_DMA_ADDR(asc->sc_dmamap->dm_segs[0].ds_addr));
bus_space_write_4(asc->sc_bst, asc->sc_bsh,
IOASIC_SCSI_NEXTPTR,
(asc->sc_dmamap->dm_nsegs == 1) ?
~0 : IOASIC_DMA_ADDR(asc->sc_dmamap->dm_segs[1].ds_addr));
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_SCSI_SCR, scr);
bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
0, size,
(ispullup) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
asc->sc_flags |= ASC_MAPLOADED;
return 0;
}
void
asc_ioasic_go(struct ncr53c9x_softc *sc)
{
struct asc_softc *asc = (struct asc_softc *)sc;
uint32_t ssr;
ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR);
if (asc->sc_flags & ASC_ISPULLUP)
ssr |= IOASIC_CSR_SCSI_DIR;
else {
ssr &= ~IOASIC_CSR_SCSI_DIR;
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr);
ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR);
}
ssr |= IOASIC_CSR_DMAEN_SCSI;
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr);
asc->sc_flags |= ASC_DMAACTIVE;
}
static int
asc_ioasic_intr(struct ncr53c9x_softc *sc)
{
struct asc_softc *asc = (struct asc_softc *)sc;
ssize_t trans, resid;
u_int tcl, tcm, ssr, scr, intr;
if ((asc->sc_flags & ASC_DMAACTIVE) == 0)
panic("%s: DMA wasn't active", __func__);
#define IOASIC_ASC_ERRORS \
(IOASIC_INTR_SCSI_PTR_LOAD|IOASIC_INTR_SCSI_OVRUN|IOASIC_INTR_SCSI_READ_E)
intr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_INTR);
if ((intr & IOASIC_ASC_ERRORS) != 0) {
intr &= ~IOASIC_ASC_ERRORS;
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_INTR, intr);
}
ssr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR);
ssr &= ~IOASIC_CSR_DMAEN_SCSI;
bus_space_write_4(asc->sc_bst, asc->sc_bsh, IOASIC_CSR, ssr);
asc->sc_flags &= ~ASC_DMAACTIVE;
if (asc->sc_dmasize == 0) {
tcl = ASC_READ_REG(asc, NCR_TCL);
tcm = ASC_READ_REG(asc, NCR_TCM);
NCR_DMA(("ioasic_intr: discarded %d bytes (tcl=%d, tcm=%d)\n",
tcl | (tcm << 8), tcl, tcm));
return 0;
}
resid = 0;
if ((asc->sc_flags & ASC_ISPULLUP) == 0 &&
(resid = (ASC_READ_REG(asc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
NCR_DMA(("ioasic_intr: empty FIFO of %d ", resid));
DELAY(1);
}
resid += (tcl = ASC_READ_REG(asc, NCR_TCL));
resid += (tcm = ASC_READ_REG(asc, NCR_TCM)) << 8;
trans = asc->sc_dmasize - resid;
if (trans < 0) {
printf("ioasic_intr: xfer (%zd) > req (%zu)\n",
trans, asc->sc_dmasize);
trans = asc->sc_dmasize;
}
NCR_DMA(("ioasic_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
tcl, tcm, trans, resid));
bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
0, asc->sc_dmasize,
(asc->sc_flags & ASC_ISPULLUP) ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
scr = bus_space_read_4(asc->sc_bst, asc->sc_bsh, IOASIC_SCSI_SCR);
if ((asc->sc_flags & ASC_ISPULLUP) && scr != 0) {
uint32_t sdr[2], ptr;
sdr[0] = bus_space_read_4(asc->sc_bst, asc->sc_bsh,
IOASIC_SCSI_SDR0);
sdr[1] = bus_space_read_4(asc->sc_bst, asc->sc_bsh,
IOASIC_SCSI_SDR1);
ptr = bus_space_read_4(asc->sc_bst, asc->sc_bsh,
IOASIC_SCSI_DMAPTR);
ptr = (ptr >> 3) & 0x1ffffffc;
scr &= IOASIC_SCR_WORD;
memcpy((void *)MIPS_PHYS_TO_KSEG0(ptr), sdr, scr << 1);
}
bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
asc->sc_flags &= ~ASC_MAPLOADED;
*asc->sc_dmalen -= trans;
*asc->sc_dmaaddr += trans;
return 0;
}
void
asc_ioasic_stop(struct ncr53c9x_softc *sc)
{
struct asc_softc *asc = (struct asc_softc *)sc;
if (asc->sc_flags & ASC_MAPLOADED) {
bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
0, asc->sc_dmasize,
(asc->sc_flags & ASC_ISPULLUP) ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
}
asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
}
static uint8_t
asc_read_reg(struct ncr53c9x_softc *sc, int reg)
{
struct asc_softc *asc = (struct asc_softc *)sc;
return ASC_READ_REG(asc, reg);
}
static void
asc_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
{
struct asc_softc *asc = (struct asc_softc *)sc;
ASC_WRITE_REG(asc, reg, val);
}
static int
asc_dma_isintr(struct ncr53c9x_softc *sc)
{
struct asc_softc *asc = (struct asc_softc *)sc;
return (ASC_READ_REG(asc, NCR_STAT) & NCRSTAT_INT) != 0;
}
static int
asc_dma_isactive(struct ncr53c9x_softc *sc)
{
struct asc_softc *asc = (struct asc_softc *)sc;
return (asc->sc_flags & ASC_DMAACTIVE) != 0;
}