#include "opt_ddb.h"
#ifdef __m68k__
#include "opt_m68k_arch.h"
#endif
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: flsc.c,v 1.47 2019/01/08 19:41:09 jdolecek Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/queue.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 <machine/cpu.h>
#include <dev/ic/ncr53c9xreg.h>
#include <dev/ic/ncr53c9xvar.h>
#include <amiga/amiga/isr.h>
#include <amiga/dev/flscvar.h>
#include <amiga/dev/zbusvar.h>
int flscmatch(device_t, cfdata_t, void *);
void flscattach(device_t, device_t, void *);
CFATTACH_DECL_NEW(flsc, sizeof(struct flsc_softc),
flscmatch, flscattach, NULL, NULL);
uint8_t flsc_read_reg(struct ncr53c9x_softc *, int);
void flsc_write_reg(struct ncr53c9x_softc *, int, uint8_t);
int flsc_dma_isintr(struct ncr53c9x_softc *);
void flsc_dma_reset(struct ncr53c9x_softc *);
int flsc_dma_intr(struct ncr53c9x_softc *);
int flsc_dma_setup(struct ncr53c9x_softc *, uint8_t **,
size_t *, int, size_t *);
void flsc_dma_go(struct ncr53c9x_softc *);
void flsc_dma_stop(struct ncr53c9x_softc *);
int flsc_dma_isactive(struct ncr53c9x_softc *);
void flsc_clear_latched_intr(struct ncr53c9x_softc *);
struct ncr53c9x_glue flsc_glue = {
flsc_read_reg,
flsc_write_reg,
flsc_dma_isintr,
flsc_dma_reset,
flsc_dma_intr,
flsc_dma_setup,
flsc_dma_go,
flsc_dma_stop,
flsc_dma_isactive,
flsc_clear_latched_intr,
};
u_long flsc_max_dma = 1024;
extern int ser_open_speed;
extern int ncr53c9x_debug;
extern u_long scsi_nosync;
extern int shift_nosync;
int
flscmatch(device_t parent, cfdata_t cf, void *aux)
{
struct zbus_args *zap;
if (!is_a4000() && !is_a3000())
return 0;
zap = aux;
if (zap->manid == 0x2140 && zap->prodid == 11
&& iszthreepa(zap->pa))
return 1;
return 0;
}
void
flscattach(device_t parent, device_t self, void *aux)
{
struct flsc_softc *fsc = device_private(self);
struct ncr53c9x_softc *sc = &fsc->sc_ncr53c9x;
struct zbus_args *zap;
sc->sc_dev = self;
sc->sc_glue = &flsc_glue;
zap = aux;
fsc->sc_dmabase = (volatile uint8_t *)zap->va;
fsc->sc_reg = &((volatile uint8_t *)zap->va)[0x1000001];
sc->sc_freq = 40;
aprint_normal(": address %p", fsc->sc_reg);
sc->sc_id = 7;
sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
sc->sc_cfg3 = 0x08 | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
sc->sc_rev = NCR_VARIANT_FAS216;
sc->sc_minsync = 1000 / sc->sc_freq;
if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
sc->sc_minsync = 0;
sc->sc_maxxfer = 64 * 1024;
fsc->sc_portbits = 0xa0 | FLSC_PB_EDI | FLSC_PB_ESI;
fsc->sc_hardbits = fsc->sc_reg[0x40];
fsc->sc_alignbuf = (uint8_t *)((u_long)fsc->sc_unalignbuf & -4);
device_cfdata(self)->cf_flags |=
(scsi_nosync >> shift_nosync) & 0xffff;
shift_nosync += 16;
ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
shift_nosync += 16;
fsc->sc_isr.isr_intr = ncr53c9x_intr;
fsc->sc_isr.isr_arg = sc;
fsc->sc_isr.isr_ipl = 2;
add_isr(&fsc->sc_isr);
fsc->sc_reg[0x40] = fsc->sc_portbits;
sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
sc->sc_adapter.adapt_minphys = minphys;
ncr53c9x_attach(sc);
}
uint8_t
flsc_read_reg(struct ncr53c9x_softc *sc, int reg)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
return fsc->sc_reg[reg * 4];
}
void
flsc_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
struct ncr53c9x_tinfo *ti;
uint8_t v = val;
if (fsc->sc_piomode && reg == NCR_CMD &&
v == (NCRCMD_TRANS | NCRCMD_DMA)) {
v = NCRCMD_TRANS;
}
if (reg == NCR_SELID) {
ti = &sc->sc_tinfo[
sc->sc_nexus->xs->xs_periph->periph_target];
if (sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
if (ti->flags & T_SYNCMODE) {
ti->flags ^= T_SYNCMODE | T_NEGOTIATE;
} else if (ti->flags & T_NEGOTIATE) {
ti->flags ^= T_NEGOTIATE | T_SYNCHOFF;
}
} else {
if ((ti->flags & (T_SYNCMODE | T_SYNCHOFF)) ==
T_SYNCHOFF &&
sc->sc_minsync != 0)
ti->flags ^= T_NEGOTIATE | T_SYNCHOFF;
}
}
if (reg == NCR_CMD && v == NCRCMD_SETATN &&
sc->sc_flags & NCR_SYNCHNEGO &&
sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
ti = &sc->sc_tinfo[
sc->sc_nexus->xs->xs_periph->periph_target];
ti->offset = 0;
}
fsc->sc_reg[reg * 4] = v;
}
int
flsc_dma_isintr(struct ncr53c9x_softc *sc)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
unsigned int hardbits;
hardbits = fsc->sc_reg[0x40];
if ((hardbits & FLSC_HB_IACT) != 0)
return (fsc->sc_csr = 0);
if (sc->sc_state == NCR_CONNECTED || sc->sc_state == NCR_SELECTING)
fsc->sc_portbits |= FLSC_PB_LED;
else
fsc->sc_portbits &= ~FLSC_PB_LED;
if ((hardbits & FLSC_HB_CREQ) != 0 && (hardbits & FLSC_HB_MINT) == 0 &&
(fsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) != 0) {
return 1;
}
if (fsc->sc_piomode && (fsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) != 0 &&
(hardbits & FLSC_HB_MINT) == 0)
return 1;
fsc->sc_reg[0x40] = fsc->sc_portbits & ~FLSC_PB_INT_BITS;
fsc->sc_reg[0x40] = fsc->sc_portbits;
return 0;
}
void
flsc_clear_latched_intr(struct ncr53c9x_softc *sc)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
fsc->sc_reg[0x40] = fsc->sc_portbits & ~FLSC_PB_INT_BITS;
fsc->sc_reg[0x40] = fsc->sc_portbits;
}
void
flsc_dma_reset(struct ncr53c9x_softc *sc)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
struct ncr53c9x_tinfo *ti;
if (sc->sc_nexus)
ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
else
ti = &sc->sc_tinfo[1];
if (fsc->sc_active) {
printf("dmaaddr %p dmasize %d stat %x flags %x off %d ",
*fsc->sc_dmaaddr, fsc->sc_dmasize,
fsc->sc_reg[NCR_STAT * 4], ti->flags, ti->offset);
printf("per %d ff %x intr %x\n",
ti->period, fsc->sc_reg[NCR_FFLAG * 4],
fsc->sc_reg[NCR_INTR * 4]);
#ifdef DDB
Debugger();
#endif
}
fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
fsc->sc_reg[0x40] = fsc->sc_portbits;
fsc->sc_reg[0x80] = 0;
*((volatile uint32_t *)fsc->sc_dmabase) = 0;
fsc->sc_active = 0;
fsc->sc_piomode = 0;
}
int
flsc_dma_intr(struct ncr53c9x_softc *sc)
{
register struct flsc_softc *fsc = (struct flsc_softc *)sc;
uint8_t *p;
volatile uint8_t *cmdreg, *intrreg, *statreg, *fiforeg;
u_int flscphase, flscstat, flscintr;
int cnt;
NCR_DMA(("flsc_dma_intr: pio %d cnt %d int %x stat %x fifo %d ",
fsc->sc_piomode, fsc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF));
if ((fsc->sc_reg[0x40] & FLSC_HB_CREQ) == 0)
printf("flsc_dma_intr: csr %x stat %x intr %x\n", fsc->sc_csr,
sc->sc_espstat, sc->sc_espintr);
if (fsc->sc_active == 0) {
printf("flsc_intr--inactive DMA\n");
return -1;
}
if (fsc->sc_piomode == 0) {
fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
fsc->sc_reg[0x40] = fsc->sc_portbits;
fsc->sc_reg[0x80] = 0;
*((volatile uint32_t *)fsc->sc_dmabase) = 0;
cnt = fsc->sc_reg[NCR_TCL * 4];
cnt += fsc->sc_reg[NCR_TCM * 4] << 8;
cnt += fsc->sc_reg[NCR_TCH * 4] << 16;
if (!fsc->sc_datain) {
cnt += fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
fsc->sc_reg[NCR_CMD * 4] = NCRCMD_FLUSH;
}
cnt = fsc->sc_dmasize - cnt;
NCR_DMA(("DMA xferred %d\n", cnt));
if (fsc->sc_xfr_align) {
int i;
for (i = 0; i < cnt; ++i)
(*fsc->sc_dmaaddr)[i] = fsc->sc_alignbuf[i];
fsc->sc_xfr_align = 0;
}
*fsc->sc_dmaaddr += cnt;
*fsc->sc_pdmalen -= cnt;
fsc->sc_active = 0;
return 0;
}
if ((sc->sc_espintr & NCRINTR_BS) == 0) {
fsc->sc_active = 0;
fsc->sc_piomode = 0;
NCR_DMA(("no NCRINTR_BS\n"));
return 0;
}
cnt = fsc->sc_dmasize;
#if 0
if (cnt == 0) {
printf("data interrupt, but no count left.");
}
#endif
p = *fsc->sc_dmaaddr;
flscphase = sc->sc_phase;
flscstat = (u_int)sc->sc_espstat;
flscintr = (u_int)sc->sc_espintr;
cmdreg = fsc->sc_reg + NCR_CMD * 4;
fiforeg = fsc->sc_reg + NCR_FIFO * 4;
statreg = fsc->sc_reg + NCR_STAT * 4;
intrreg = fsc->sc_reg + NCR_INTR * 4;
NCR_DMA(("PIO %d datain %d phase %d stat %x intr %x\n",
cnt, fsc->sc_datain, flscphase, flscstat, flscintr));
do {
if (fsc->sc_datain) {
*p++ = *fiforeg;
cnt--;
if (flscphase == DATA_IN_PHASE) {
*cmdreg = NCRCMD_TRANS;
} else {
fsc->sc_active = 0;
}
} else {
NCR_DMA(("flsc_dma_intr: PIO out- phase %d cnt %d active %d\n", flscphase, cnt,
fsc->sc_active));
if ( (flscphase == DATA_OUT_PHASE)
|| (flscphase == MESSAGE_OUT_PHASE)) {
int n;
n = 16 - (fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF);
if (n > cnt)
n = cnt;
cnt -= n;
while (n-- > 0)
*fiforeg = *p++;
*cmdreg = NCRCMD_TRANS;
} else {
fsc->sc_active = 0;
}
}
if (fsc->sc_active && cnt) {
while ((*statreg & 0x80) == 0)
;
flscstat = *statreg;
flscintr = *intrreg;
flscphase = (flscintr & NCRINTR_DIS)
? BUSFREE_PHASE
: flscstat & PHASE_MASK;
}
} while (cnt && fsc->sc_active && (flscintr & NCRINTR_BS) != 0);
#if 1
if (fsc->sc_dmasize < 8 && cnt)
printf("flsc_dma_intr: short transfer: dmasize %d cnt %d\n",
fsc->sc_dmasize, cnt);
#endif
NCR_DMA(("flsc_dma_intr: PIO transfer [%d], %d->%d phase %d stat %x intr %x\n",
*fsc->sc_pdmalen, fsc->sc_dmasize, cnt, flscphase, flscstat, flscintr));
sc->sc_phase = flscphase;
sc->sc_espstat = (uint8_t)flscstat;
sc->sc_espintr = (uint8_t)flscintr;
*fsc->sc_dmaaddr = p;
*fsc->sc_pdmalen -= fsc->sc_dmasize - cnt;
fsc->sc_dmasize = cnt;
if (*fsc->sc_pdmalen == 0) {
sc->sc_espstat |= NCRSTAT_TC;
fsc->sc_piomode = 0;
}
return 0;
}
int
flsc_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
int datain, size_t *dmasize)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
paddr_t pa;
uint8_t *ptr;
size_t xfer;
fsc->sc_dmaaddr = addr;
fsc->sc_pdmalen = len;
fsc->sc_datain = datain;
fsc->sc_dmasize = *dmasize;
if (sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
*dmasize = fsc->sc_dmasize;
NCR_DMA(("pfsc_dma_setup: PIO %p/%d [%d]\n", *addr,
fsc->sc_dmasize, *len));
fsc->sc_piomode = 1;
if (datain == 0) {
int n;
n = fsc->sc_dmasize;
if (n > 16)
n = 16;
while (n-- > 0) {
fsc->sc_reg[NCR_FIFO * 4] = **fsc->sc_dmaaddr;
(*fsc->sc_pdmalen)--;
(*fsc->sc_dmaaddr)++;
--fsc->sc_dmasize;
}
}
return 0;
}
if (ser_open_speed > 19200 && flsc_max_dma != 0 &&
fsc->sc_dmasize > flsc_max_dma)
fsc->sc_dmasize = flsc_max_dma;
ptr = *addr;
pa = kvtop(ptr);
xfer = uimin(fsc->sc_dmasize, PAGE_SIZE - (pa & (PAGE_SIZE - 1)));
fsc->sc_xfr_align = 0;
fsc->sc_piomode = 0;
fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
fsc->sc_reg[0x40] = fsc->sc_portbits;
fsc->sc_reg[0x80] = 0;
*((volatile uint32_t *)fsc->sc_dmabase) = 0;
if (datain == 0 && fsc->sc_dmasize < 16) {
int n;
for (n = 0; n < fsc->sc_dmasize; ++n)
fsc->sc_reg[NCR_FIFO * 4] = *ptr++;
NCR_DMA(("flsc_dma_setup: %d bytes written to fifo\n", n));
fsc->sc_piomode = 1;
fsc->sc_active = 1;
*fsc->sc_pdmalen -= fsc->sc_dmasize;
*fsc->sc_dmaaddr += fsc->sc_dmasize;
*dmasize = fsc->sc_dmasize;
fsc->sc_dmasize = 0;
return 0;
}
else if (datain == 0 && (int)ptr & 3) {
int n = 4 - ((int)ptr & 3);
NCR_DMA(("flsc_dma_setup: align %d bytes written to fifo\n", n));
pa += n;
xfer -= n;
while (n--)
fsc->sc_reg[NCR_FIFO * 4] = *ptr++;
}
else if ((int)ptr & 3 || xfer & 3) {
pa = kvtop((void *)fsc->sc_alignbuf);
xfer = fsc->sc_dmasize = uimin(xfer, sizeof(fsc->sc_unalignbuf));
NCR_DMA(("flsc_dma_setup: align read by %d bytes\n", xfer));
fsc->sc_xfr_align = 1;
}
else if (fsc->sc_dmasize < 4) {
NCR_DMA(("flsc_dma_setup: read remaining %d bytes\n",
fsc->sc_dmasize));
pa = kvtop((void *)fsc->sc_alignbuf);
fsc->sc_xfr_align = 1;
}
else {
fsc->sc_dmasize &= -4;
xfer &= -4;
}
while (xfer < fsc->sc_dmasize) {
if ((pa + xfer) != kvtop(*addr + xfer))
break;
if ((fsc->sc_dmasize - xfer) < PAGE_SIZE)
xfer = fsc->sc_dmasize;
else
xfer += PAGE_SIZE;
}
fsc->sc_dmasize = xfer;
*dmasize = fsc->sc_dmasize;
fsc->sc_pa = pa;
#if defined(M68040) || defined(M68060)
if (mmutype == MMU_68040) {
if (fsc->sc_xfr_align) {
int n;
for (n = 0; n < sizeof(fsc->sc_unalignbuf); ++n)
fsc->sc_alignbuf[n] = n | 0x80;
dma_cachectl(fsc->sc_alignbuf,
sizeof(fsc->sc_unalignbuf));
}
else
dma_cachectl(*fsc->sc_dmaaddr, fsc->sc_dmasize);
}
#endif
fsc->sc_reg[0x80] = 0;
*((volatile uint32_t *)(fsc->sc_dmabase + (pa & 0x00fffffc))) = pa;
fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
fsc->sc_portbits |= FLSC_PB_ENABLE_DMA |
(fsc->sc_datain ? FLSC_PB_DMA_READ : FLSC_PB_DMA_WRITE);
fsc->sc_reg[0x40] = fsc->sc_portbits;
NCR_DMA(("flsc_dma_setup: DMA %p->%lx/%d [%d]\n",
ptr, pa, fsc->sc_dmasize, *len));
fsc->sc_active = 1;
return 0;
}
void
flsc_dma_go(struct ncr53c9x_softc *sc)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
NCR_DMA(("flsc_dma_go: datain %d size %d\n", fsc->sc_datain,
fsc->sc_dmasize));
if (sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
fsc->sc_active = 1;
return;
} else if (fsc->sc_piomode == 0) {
fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
fsc->sc_portbits |= FLSC_PB_ENABLE_DMA |
(fsc->sc_datain ? FLSC_PB_DMA_READ : FLSC_PB_DMA_WRITE);
fsc->sc_reg[0x40] = fsc->sc_portbits;
}
}
void
flsc_dma_stop(struct ncr53c9x_softc *sc)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
fsc->sc_reg[0x40] = fsc->sc_portbits;
fsc->sc_reg[0x80] = 0;
*((volatile uint32_t *)fsc->sc_dmabase) = 0;
fsc->sc_piomode = 0;
}
int
flsc_dma_isactive(struct ncr53c9x_softc *sc)
{
struct flsc_softc *fsc = (struct flsc_softc *)sc;
return fsc->sc_active;
}