#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uha_isa.c,v 1.42 2019/11/12 13:10:51 msaitoh Exp $");
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/bus.h>
#include <sys/intr.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isadmavar.h>
#include <dev/ic/uhareg.h>
#include <dev/ic/uhavar.h>
#define UHA_ISA_IOSIZE 16
int uha_isa_probe(device_t, cfdata_t, void *);
void uha_isa_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(uha_isa, sizeof(struct uha_softc),
uha_isa_probe, uha_isa_attach, NULL, NULL);
#ifndef DDB
#define Debugger() panic("should call debugger here (uha_isa.c)")
#endif
int u14_find(bus_space_tag_t, bus_space_handle_t, struct uha_probe_data *);
void u14_start_mbox(struct uha_softc *, struct uha_mscp *);
int u14_poll(struct uha_softc *, struct scsipi_xfer *, int);
int u14_intr(void *);
void u14_init(struct uha_softc *);
int
uha_isa_probe(device_t parent, cfdata_t match, void *aux)
{
struct isa_attach_args *ia = aux;
bus_space_tag_t iot = ia->ia_iot;
bus_space_handle_t ioh;
struct uha_probe_data upd;
int rv;
if (ia->ia_nio < 1)
return (0);
if (ia->ia_nirq < 1)
return (0);
if (ia->ia_ndrq < 1)
return (0);
if (ISA_DIRECT_CONFIG(ia))
return (0);
if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
return (0);
if (bus_space_map(iot, ia->ia_io[0].ir_addr, UHA_ISA_IOSIZE, 0, &ioh))
return (0);
rv = u14_find(iot, ioh, &upd);
bus_space_unmap(iot, ioh, UHA_ISA_IOSIZE);
if (rv) {
if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
ia->ia_irq[0].ir_irq != upd.sc_irq)
return (0);
if (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ &&
ia->ia_drq[0].ir_drq != upd.sc_drq)
return (0);
ia->ia_nio = 1;
ia->ia_io[0].ir_size = UHA_ISA_IOSIZE;
ia->ia_nirq = 1;
ia->ia_irq[0].ir_irq = upd.sc_irq;
ia->ia_ndrq = 1;
ia->ia_drq[0].ir_drq = upd.sc_drq;
ia->ia_niomem = 0;
}
return (rv);
}
void
uha_isa_attach(device_t parent, device_t self, void *aux)
{
struct isa_attach_args *ia = aux;
struct uha_softc *sc = device_private(self);
bus_space_tag_t iot = ia->ia_iot;
bus_dma_tag_t dmat = ia->ia_dmat;
bus_space_handle_t ioh;
struct uha_probe_data upd;
isa_chipset_tag_t ic = ia->ia_ic;
int error;
sc->sc_dev = self;
printf("\n");
if (bus_space_map(iot, ia->ia_io[0].ir_addr, UHA_ISA_IOSIZE, 0, &ioh)) {
aprint_error_dev(sc->sc_dev, "can't map i/o space\n");
return;
}
sc->sc_iot = iot;
sc->sc_ioh = ioh;
sc->sc_dmat = dmat;
if (!u14_find(iot, ioh, &upd)) {
aprint_error_dev(sc->sc_dev, "u14_find failed\n");
return;
}
if (upd.sc_drq != -1) {
sc->sc_dmaflags = 0;
if ((error = isa_dmacascade(ic, upd.sc_drq)) != 0) {
aprint_error_dev(sc->sc_dev, "unable to cascade DRQ, error = %d\n", error);
return;
}
} else {
sc->sc_dmaflags = ISABUS_DMA_32BIT;
}
sc->sc_ih = isa_intr_establish(ic, upd.sc_irq, IST_EDGE, IPL_BIO,
u14_intr, sc);
if (sc->sc_ih == NULL) {
aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
return;
}
sc->start_mbox = u14_start_mbox;
sc->poll = u14_poll;
sc->init = u14_init;
uha_attach(sc, &upd);
}
int
u14_find(bus_space_tag_t iot, bus_space_handle_t ioh, struct uha_probe_data *sc)
{
u_int16_t model, config;
int irq, drq;
int resetcount = 4000;
model = (bus_space_read_1(iot, ioh, U14_ID + 0) << 8) |
(bus_space_read_1(iot, ioh, U14_ID + 1) << 0);
if ((model & 0xfff0) != 0x5640)
return (0);
config = (bus_space_read_1(iot, ioh, U14_CONFIG + 0) << 8) |
(bus_space_read_1(iot, ioh, U14_CONFIG + 1) << 0);
switch (model & 0x000f) {
case 0x0000:
switch (config & U14_DMA_MASK) {
case U14_DMA_CH5:
drq = 5;
break;
case U14_DMA_CH6:
drq = 6;
break;
case U14_DMA_CH7:
drq = 7;
break;
default:
printf("u14_find: illegal drq setting %x\n",
config & U14_DMA_MASK);
return (0);
}
break;
case 0x0001:
drq = -1;
break;
default:
printf("u14_find: unknown model %x\n", model);
return (0);
}
switch (config & U14_IRQ_MASK) {
case U14_IRQ10:
irq = 10;
break;
case U14_IRQ11:
irq = 11;
break;
case U14_IRQ14:
irq = 14;
break;
case U14_IRQ15:
irq = 15;
break;
default:
printf("u14_find: illegal irq setting %x\n",
config & U14_IRQ_MASK);
return (0);
}
bus_space_write_1(iot, ioh, U14_LINT, UHA_ASRST);
while (--resetcount) {
if (bus_space_read_1(iot, ioh, U14_LINT))
break;
delay(1000);
}
if (!resetcount) {
printf("u14_find: board timed out during reset\n");
return (0);
}
if (sc) {
sc->sc_irq = irq;
sc->sc_drq = drq;
sc->sc_scsi_dev = config & U14_HOSTID_MASK;
}
return (1);
}
void
u14_start_mbox(struct uha_softc *sc, struct uha_mscp *mscp)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
int spincount = 100000;
while (--spincount) {
if ((bus_space_read_1(iot, ioh, U14_LINT) & U14_LDIP) == 0)
break;
delay(100);
}
if (!spincount) {
aprint_error_dev(sc->sc_dev, "uha_start_mbox, board not responding\n");
Debugger();
}
bus_space_write_4(iot, ioh, U14_OGMPTR,
sc->sc_dmamap_mscp->dm_segs[0].ds_addr + UHA_MSCP_OFF(mscp));
if (mscp->flags & MSCP_ABORT)
bus_space_write_1(iot, ioh, U14_LINT, U14_ABORT);
else
bus_space_write_1(iot, ioh, U14_LINT, U14_OGMFULL);
if ((mscp->xs->xs_control & XS_CTL_POLL) == 0)
callout_reset(&mscp->xs->xs_callout,
mstohz(mscp->timeout), uha_timeout, mscp);
}
int
u14_poll(struct uha_softc *sc, struct scsipi_xfer *xs, int count)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
while (count) {
if (bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP)
u14_intr(sc);
if (xs->xs_status & XS_STS_DONE)
return (0);
delay(1000);
count--;
}
return (1);
}
int
u14_intr(void *arg)
{
struct uha_softc *sc = arg;
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
struct uha_mscp *mscp;
u_char uhastat;
u_long mboxval;
#ifdef UHADEBUG
printf("%s: uhaintr ", device_xname(sc->sc_dev));
#endif
if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
return (0);
for (;;) {
uhastat = bus_space_read_1(iot, ioh, U14_SINT);
mboxval = bus_space_read_4(iot, ioh, U14_ICMPTR);
bus_space_write_1(iot, ioh, U14_SINT, U14_ICM_ACK);
#ifdef UHADEBUG
printf("status = 0x%x ", uhastat);
#else
__USE(uhastat);
#endif
mscp = uha_mscp_phys_kv(sc, mboxval);
if (!mscp) {
printf("%s: BAD MSCP RETURNED!\n",
device_xname(sc->sc_dev));
continue;
}
callout_stop(&mscp->xs->xs_callout);
uha_done(sc, mscp);
if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
return (1);
}
}
void
u14_init(struct uha_softc *sc)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
#ifdef UHADEBUG
printf("u14_init: lmask=%02x, smask=%02x\n",
bus_space_read_1(iot, ioh, U14_LMASK),
bus_space_read_1(iot, ioh, U14_SMASK));
#endif
bus_space_write_1(iot, ioh, U14_LMASK, 0xd1);
bus_space_write_1(iot, ioh, U14_SMASK, 0x91);
}