#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nca_isa.c,v 1.22 2012/10/27 17:18:25 chs Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/buf.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/ncr5380reg.h>
#include <dev/ic/ncr5380var.h>
#include <dev/ic/ncr53c400reg.h>
struct nca_isa_softc {
struct ncr5380_softc sc_ncr5380;
void *sc_ih;
int sc_irq;
int sc_options;
};
struct nca_isa_probe_data {
int sc_reg_offset;
int sc_host_type;
};
int nca_isa_find(bus_space_tag_t, bus_space_handle_t, bus_size_t,
struct nca_isa_probe_data *);
int nca_isa_match(device_t, cfdata_t, void *);
void nca_isa_attach(device_t, device_t, void *);
int nca_isa_test(bus_space_tag_t, bus_space_handle_t, bus_size_t);
CFATTACH_DECL_NEW(nca_isa, sizeof(struct nca_isa_softc),
nca_isa_match, nca_isa_attach, NULL, NULL);
#define MAX_NCA_CONTROLLER 3
#define CTLR_NCR_5380 1
#define CTLR_NCR_53C400 2
#define CTLR_PAS16 3
#define NCA_ISA_IOSIZE 16
#define MIN_DMA_LEN 128
#define NCA_NO_DISCONNECT 0xff
#define NCA_NO_PARITY_CHK 0xff00
#define NCA_FORCE_POLLING 0x10000
int
nca_isa_test(bus_space_tag_t iot, bus_space_handle_t ioh, bus_size_t reg_offset)
{
bus_space_write_1(iot, ioh, reg_offset + C80_ICR, SCI_ICMD_RST);
bus_space_write_1(iot, ioh, reg_offset + C80_ODR, 0);
delay(500);
if (bus_space_read_1(iot, ioh, reg_offset + C80_CSBR) != SCI_BUS_RST) {
#ifdef DEBUG
printf("%s: reset status not cleared [0x%x]\n",
__func__, bus_space_read_1(iot, ioh, reg_offset+C80_CSBR));
#endif
bus_space_write_1(iot, ioh, reg_offset+C80_ICR, 0);
return 0;
}
bus_space_write_1(iot, ioh, reg_offset + C80_ICR, 0);
delay(16000);
bus_space_read_1(iot, ioh, reg_offset + C80_RPIR);
if (bus_space_read_1(iot, ioh, reg_offset + C80_BSR) & (SCI_CSR_PERR |
SCI_CSR_INT | SCI_CSR_DISC)) {
#ifdef DEBUG
printf("%s: Parity/Interrupt/Busy not cleared [0x%x]\n",
__func__, bus_space_read_1(iot, ioh, reg_offset+C80_BSR));
#endif
return 0;
}
return 1;
}
int
nca_isa_find(bus_space_tag_t iot, bus_space_handle_t ioh,
bus_size_t max_offset, struct nca_isa_probe_data *epd)
{
int cont_type;
bus_size_t base_offset, reg_offset = 0;
for (base_offset = 0; base_offset < max_offset; base_offset += 0x08) {
#ifdef DEBUG
printf("%s: testing offset 0x%x\n", __func__, (int)base_offset);
#endif
if (bus_space_read_1(iot, ioh, base_offset) == 0xff)
continue;
for (cont_type = 1; cont_type <= MAX_NCA_CONTROLLER;
cont_type++) {
switch (cont_type) {
case CTLR_NCR_5380:
reg_offset = 0;
break;
case CTLR_NCR_53C400:
bus_space_write_1(iot, ioh,
base_offset + C400_CSR,
C400_CSR_5380_ENABLE);
reg_offset = C400_5380_REG_OFFSET;
break;
case CTLR_PAS16:
reg_offset = 0;
cont_type = 0;
continue;
}
if (nca_isa_test(iot, ioh, base_offset+reg_offset)) {
epd->sc_reg_offset = base_offset;
epd->sc_host_type = cont_type;
return cont_type;
}
}
}
return 0;
}
int
nca_isa_match(device_t parent, cfdata_t cf, void *aux)
{
struct isa_attach_args *ia = aux;
bus_space_tag_t iot = ia->ia_iot;
bus_space_tag_t memt = ia->ia_memt;
bus_space_handle_t ioh;
struct nca_isa_probe_data epd;
int rv = 0;
if (ISA_DIRECT_CONFIG(ia))
return 0;
if (ia->ia_nio > 0 || ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
if (bus_space_map(iot, ia->ia_io[0].ir_addr, NCA_ISA_IOSIZE,
0, &ioh))
return 0;
rv = nca_isa_find(iot, ioh, 0x07, &epd);
bus_space_unmap(iot, ioh, NCA_ISA_IOSIZE);
if (rv) {
ia->ia_nio = 1;
ia->ia_io[0].ir_size = NCA_ISA_IOSIZE;
ia->ia_niomem = 0;
ia->ia_ndrq = 0;
}
} else if (ia->ia_niomem > 0) {
if (bus_space_map(memt, ia->ia_iomem[0].ir_addr, 0x4000,
0, &ioh))
return 0;
rv = nca_isa_find(memt, ioh, 0x03ff0, &epd);
bus_space_unmap(memt, ioh, 0x04000);
if (rv) {
ia->ia_niomem = 1;
ia->ia_iomem[0].ir_addr += epd.sc_reg_offset;
ia->ia_iomem[0].ir_size = NCA_ISA_IOSIZE;
ia->ia_nio = 0;
ia->ia_ndrq = 0;
}
}
return rv;
}
void
nca_isa_attach(device_t parent, device_t self, void *aux)
{
struct nca_isa_softc *esc = device_private(self);
struct ncr5380_softc *sc = &esc->sc_ncr5380;
struct isa_attach_args *ia = aux;
bus_space_tag_t iot = ia->ia_iot;
bus_space_handle_t ioh;
struct nca_isa_probe_data epd;
isa_chipset_tag_t ic = ia->ia_ic;
sc->sc_dev = self;
aprint_normal("\n");
if (ia->ia_nio > 0) {
iot = ia->ia_iot;
if (bus_space_map(iot, ia->ia_io[0].ir_addr, NCA_ISA_IOSIZE,
0, &ioh)) {
aprint_error_dev(self, "can't map i/o space\n");
return;
}
} else {
KASSERT(ia->ia_niomem > 0);
iot = ia->ia_memt;
if (bus_space_map(iot, ia->ia_iomem[0].ir_addr, NCA_ISA_IOSIZE,
0, &ioh)) {
aprint_error_dev(self, "can't map mem space\n");
return;
}
}
switch (nca_isa_find(iot, ioh, NCA_ISA_IOSIZE, &epd)) {
case 0:
aprint_error_dev(self, "nca_isa_find failed\n");
return;
case CTLR_NCR_5380:
aprint_normal_dev(self, "NCR 53C80 detected\n");
sc->sci_r0 = 0;
sc->sci_r1 = 1;
sc->sci_r2 = 2;
sc->sci_r3 = 3;
sc->sci_r4 = 4;
sc->sci_r5 = 5;
sc->sci_r6 = 6;
sc->sci_r7 = 7;
sc->sc_rev = NCR_VARIANT_NCR5380;
break;
case CTLR_NCR_53C400:
aprint_normal_dev(self, "NCR 53C400 detected\n");
sc->sci_r0 = C400_5380_REG_OFFSET + 0;
sc->sci_r1 = C400_5380_REG_OFFSET + 1;
sc->sci_r2 = C400_5380_REG_OFFSET + 2;
sc->sci_r3 = C400_5380_REG_OFFSET + 3;
sc->sci_r4 = C400_5380_REG_OFFSET + 4;
sc->sci_r5 = C400_5380_REG_OFFSET + 5;
sc->sci_r6 = C400_5380_REG_OFFSET + 6;
sc->sci_r7 = C400_5380_REG_OFFSET + 7;
sc->sc_rev = NCR_VARIANT_NCR53C400;
break;
case CTLR_PAS16:
aprint_normal_dev(self, "ProAudio Spectrum 16 detected\n");
sc->sc_rev = NCR_VARIANT_PAS16;
break;
}
sc->sc_pio_out = ncr5380_pio_out;
sc->sc_pio_in = ncr5380_pio_in;
sc->sc_dma_alloc = NULL;
sc->sc_dma_free = NULL;
sc->sc_dma_setup = NULL;
sc->sc_dma_start = NULL;
sc->sc_dma_poll = NULL;
sc->sc_dma_eop = NULL;
sc->sc_dma_stop = NULL;
sc->sc_intr_on = NULL;
sc->sc_intr_off = NULL;
if (ia->ia_nirq > 0 && ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ) {
esc->sc_ih = isa_intr_establish(ic, ia->ia_irq[0].ir_irq,
IST_EDGE, IPL_BIO, ncr5380_intr, esc);
if (esc->sc_ih == NULL) {
aprint_error_dev(self,
"couldn't establish interrupt\n");
return;
}
} else
sc->sc_flags |= NCR5380_FORCE_POLLING;
#if 0
esc->sc_options = 0x00000;
#else
esc->sc_options = 0x0ffff;
#endif
sc->sc_no_disconnect = (esc->sc_options & NCA_NO_DISCONNECT);
sc->sc_parity_disable = (esc->sc_options & NCA_NO_PARITY_CHK) >> 8;
if (esc->sc_options & NCA_FORCE_POLLING)
sc->sc_flags |= NCR5380_FORCE_POLLING;
sc->sc_min_dma_len = MIN_DMA_LEN;
sc->sc_regt = iot;
sc->sc_regh = ioh;
sc->sc_adapter.adapt_request = ncr5380_scsipi_request;
sc->sc_adapter.adapt_minphys = minphys;
sc->sc_channel.chan_id = 7;
ncr5380_attach(sc);
}