#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dpt_isa.c,v 1.23 2016/07/14 10:19:06 msaitoh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/bus.h>
#include <sys/intr.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isadmareg.h>
#include <dev/isa/isadmavar.h>
#include <dev/ic/dptreg.h>
#include <dev/ic/dptvar.h>
#include <dev/i2o/dptivar.h>
#define DPT_ISA_IOSIZE 16
#define DPT_ISA_MAXCCBS 16
static void dpt_isa_attach(device_t, device_t, void *);
static int dpt_isa_match(device_t, cfdata_t, void *);
static int dpt_isa_probe(struct isa_attach_args *, int);
static int dpt_isa_wait(bus_space_handle_t, bus_space_tag_t, u_int8_t,
u_int8_t);
CFATTACH_DECL_NEW(dpt_isa, sizeof(struct dpt_softc),
dpt_isa_match, dpt_isa_attach, NULL, NULL);
static const int dpt_isa_iobases[] = { 0x230, 0x330, 0x1f0, 0x170, 0 };
static int
dpt_isa_wait(bus_space_handle_t ioh, bus_space_tag_t iot, u_int8_t mask,
u_int8_t state)
{
int ms;
for (ms = 2000 * 10; ms; ms--) {
if ((bus_space_read_1(iot, ioh, HA_STATUS) & mask) == state)
return (0);
DELAY(100);
}
return (-1);
}
static int
dpt_isa_match(device_t parent, cfdata_t match, void *aux)
{
struct isa_attach_args *ia = aux;
int i;
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 (dpt_isa_probe(ia, ia->ia_io[0].ir_addr));
for (i = 0; dpt_isa_iobases[i] != 0; i++) {
if (dpt_isa_probe(ia, dpt_isa_iobases[i])) {
ia->ia_io[0].ir_addr = dpt_isa_iobases[i];
return (1);
}
}
return (0);
}
static int
dpt_isa_probe(struct isa_attach_args *ia, int iobase)
{
struct eata_cfg ec;
bus_space_handle_t ioh;
bus_space_tag_t iot;
int i, j, stat, irq, drq;
u_int16_t *p;
iot = ia->ia_iot;
if (bus_space_map(iot, iobase, DPT_ISA_IOSIZE, 0, &ioh) != 0)
return(0);
for (i = 1000; i; i--) {
if ((bus_space_read_1(iot, ioh, HA_STATUS) & HA_ST_READY) != 0)
break;
DELAY(2000);
}
if (i == 0)
goto bad;
while((((stat = bus_space_read_1(iot, ioh, HA_STATUS))
!= (HA_ST_READY|HA_ST_SEEK_COMPLETE))
&& (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
&& (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
|| (dpt_isa_wait(ioh, iot, HA_ST_BUSY, 0)))
if (bus_space_read_1(iot, ioh, HA_ERROR) != 'D' ||
bus_space_read_1(iot, ioh, HA_ERROR + 1) != 'P' ||
bus_space_read_1(iot, ioh, HA_ERROR + 2) != 'T')
goto bad;
bus_space_write_1(iot, ioh, HA_COMMAND, CP_PIO_GETCFG);
memset(&ec, 0, sizeof(ec));
i = ((uintptr_t)&((struct eata_cfg *)0)->ec_cfglen +
sizeof(ec.ec_cfglen)) >> 1;
p = (u_int16_t *)&ec;
if (dpt_isa_wait(ioh, iot, 0xFF, HA_ST_DATA_RDY))
goto bad;
while (i--)
*p++ = bus_space_read_stream_2(iot, ioh, HA_DATA);
if ((i = ec.ec_cfglen) > (sizeof(struct eata_cfg)
- (uintptr_t)(&(((struct eata_cfg *)0L)->ec_cfglen))
- sizeof(ec.ec_cfglen)))
i = sizeof(struct eata_cfg)
- (uintptr_t)(&(((struct eata_cfg *)0L)->ec_cfglen))
- sizeof(ec.ec_cfglen);
j = i + (uintptr_t)(&(((struct eata_cfg *)0L)->ec_cfglen)) +
sizeof(ec.ec_cfglen);
i >>= 1;
while (i--)
*p++ = bus_space_read_stream_2(iot, ioh, HA_DATA);
i = (512 - j + 1) >> 1;
while (i--)
bus_space_read_stream_2(iot, ioh, HA_DATA);
if ((bus_space_read_1(iot, ioh, HA_STATUS) & HA_ST_ERROR) != 0 ||
memcmp(ec.ec_eatasig, "EATA", 4) != 0 ||
(ec.ec_feat0 & (EC_F0_HBA_VALID | EC_F0_DMA_SUPPORTED)) !=
(EC_F0_HBA_VALID | EC_F0_DMA_SUPPORTED))
goto bad;
if ((drq = ia->ia_drq[0].ir_drq) == ISA_UNKNOWN_DRQ) {
int dmanum = ((ec.ec_feat1 & EC_F1_DMA_NUM_MASK) >>
EC_F1_DMA_NUM_SHIFT);
if ((ec.ec_feat0 & EC_F0_DMA_NUM_VALID) == 0 || dmanum > 3)
goto bad;
drq = "\0\7\6\5"[dmanum];
}
if ((irq = ia->ia_irq[0].ir_irq) == ISA_UNKNOWN_IRQ)
irq = ((ec.ec_feat1 & EC_F1_IRQ_NUM_MASK) >>
EC_F1_IRQ_NUM_SHIFT);
ia->ia_nio = 1;
ia->ia_io[0].ir_size = DPT_ISA_IOSIZE;
ia->ia_nirq = 1;
ia->ia_irq[0].ir_irq = irq;
ia->ia_ndrq = 1;
ia->ia_drq[0].ir_drq = drq;
ia->ia_niomem = 0;
bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
return (1);
bad:
bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
return (0);
}
static void
dpt_isa_attach(device_t parent, device_t self, void *aux)
{
struct isa_attach_args *ia;
isa_chipset_tag_t ic;
bus_space_handle_t ioh;
bus_space_tag_t iot;
struct dpt_softc *sc;
struct eata_cfg *ec;
int error;
ia = aux;
sc = device_private(self);
sc->sc_dev = self;
iot = ia->ia_iot;
ic = ia->ia_ic;
aprint_naive("\n");
aprint_normal(": ");
if ((error = bus_space_map(iot, ia->ia_io[0].ir_addr, DPT_ISA_IOSIZE,
0, &ioh)) != 0) {
aprint_error("can't map i/o space, error = %d\n", error);
return;
}
sc->sc_iot = iot;
sc->sc_ioh = ioh;
sc->sc_dmat = ia->ia_dmat;
if ((error = isa_dmacascade(ic, ia->ia_drq[0].ir_drq)) != 0) {
aprint_error("unable to cascade DRQ, error = %d\n", error);
return;
}
sc->sc_ih = isa_intr_establish(ic, ia->ia_irq[0].ir_irq, IST_EDGE,
IPL_BIO, dpt_intr, sc);
if (sc->sc_ih == NULL) {
aprint_error("can't establish interrupt\n");
return;
}
if (dpt_readcfg(sc)) {
aprint_error("readcfg failed - see dpt(4)\n");
return;
}
ec = &sc->sc_ec;
if (be16toh(*(int16_t *)ec->ec_queuedepth) > DPT_ISA_MAXCCBS)
*(int16_t *)ec->ec_queuedepth = htobe16(DPT_ISA_MAXCCBS);
if (ec->ec_maxlun == 0)
ec->ec_maxlun = 7;
if ((ec->ec_feat3 & EC_F3_MAX_TARGET_MASK) >> EC_F3_MAX_TARGET_SHIFT
== 0)
ec->ec_feat3 = (ec->ec_feat3 & ~EC_F3_MAX_TARGET_MASK) |
(7 << EC_F3_MAX_TARGET_SHIFT);
sc->sc_bustype = SI_ISA_BUS;
sc->sc_isaport = ia->ia_io[0].ir_addr;
sc->sc_isairq = ia->ia_irq[0].ir_irq;
sc->sc_isadrq = ia->ia_drq[0].ir_drq;
dpt_init(sc, NULL);
}