#include <sys/param.h>
__KERNEL_RCSID(0, "$NetBSD: icside.c,v 1.36 2023/12/20 06:13:59 thorpej Exp $");
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <machine/intr.h>
#include <machine/io.h>
#include <acorn32/podulebus/podulebus.h>
#include <acorn32/podulebus/icsidereg.h>
#include <dev/ata/atavar.h>
#include <dev/ic/wdcreg.h>
#include <dev/ic/wdcvar.h>
#include <dev/podulebus/podules.h>
struct icside_softc {
struct wdc_softc sc_wdcdev;
podule_t *sc_podule;
int sc_podule_number;
struct bus_space sc_tag;
struct podule_attach_args *sc_pa;
bus_space_tag_t sc_latchiot;
bus_space_handle_t sc_latchioh;
void *sc_shutdownhook;
struct ata_channel *sc_chp[ICSIDE_MAX_CHANNELS];
struct icside_channel {
struct ata_channel ic_channel;
void *ic_ih;
struct evcnt ic_intrcnt;
u_int ic_irqaddr;
u_int ic_irqmask;
bus_space_tag_t ic_irqiot;
bus_space_handle_t ic_irqioh;
} sc_chan[ICSIDE_MAX_CHANNELS];
struct wdc_regs sc_wdc_regs[ICSIDE_MAX_CHANNELS];
};
int icside_probe(device_t, cfdata_t, void *);
void icside_attach(device_t, device_t, void *);
int icside_intr(void *);
void icside_v6_shutdown(void *);
CFATTACH_DECL_NEW(icside, sizeof(struct icside_softc),
icside_probe, icside_attach, NULL, NULL);
bs_rm_2_proto(icside);
bs_wm_2_proto(icside);
#define MAX_CHANNELS 2
struct ide_version {
int id;
int modspace;
int channels;
const char *name;
int latchreg;
int ideregs[MAX_CHANNELS];
int auxregs[MAX_CHANNELS];
int irqregs[MAX_CHANNELS];
int irqstatregs[MAX_CHANNELS];
} const ide_versions[] = {
{ 3, 0, 2, "ARCIN V6", V6_ADDRLATCH,
{ V6_P_IDE_BASE, V6_S_IDE_BASE },
{ V6_P_AUX_BASE, V6_S_AUX_BASE },
{ V6_P_IRQ_BASE, V6_S_IRQ_BASE },
{ V6_P_IRQSTAT_BASE, V6_S_IRQSTAT_BASE }
},
{ 15, 1, 1, "ARCIN V5", -1,
{ V5_IDE_BASE, -1 },
{ V5_AUX_BASE, -1 },
{ V5_IRQ_BASE, -1 },
{ V5_IRQSTAT_BASE, -1 }
}
};
int
icside_probe(device_t parent, cfdata_t cf, void *aux)
{
struct podule_attach_args *pa = (void *)aux;
return (pa->pa_product == PODULE_ICS_IDE);
}
void
icside_attach(device_t parent, device_t self, void *aux)
{
struct icside_softc *sc = device_private(self);
struct podule_attach_args *pa = (void *)aux;
bus_space_tag_t iot;
bus_space_handle_t ioh;
const struct ide_version *ide = NULL;
u_int iobase;
int channel, i;
struct icside_channel *icp;
struct ata_channel *cp;
struct wdc_regs *wdr;
int loop;
int id;
if (pa->pa_podule_number == -1)
panic("Podule has disappeared !");
sc->sc_wdcdev.sc_atac.atac_dev = self;
sc->sc_podule_number = pa->pa_podule_number;
sc->sc_podule = pa->pa_podule;
podules[sc->sc_podule_number].attached = 1;
sc->sc_wdcdev.regs = sc->sc_wdc_regs;
iot = pa->pa_iot;
if (bus_space_map(iot, pa->pa_podule->fast_base +
ID_REGISTER_OFFSET, ID_REGISTER_SPACE, 0, &ioh)) {
aprint_error_dev(self, "cannot map ID register\n");
return;
}
for (id = 0, loop = 0; loop < 4; ++loop)
id |= (bus_space_read_1(iot, ioh, loop) & 1) << loop;
for (loop = 0; loop < sizeof(ide_versions) / sizeof(struct ide_version);
++loop) {
if (ide_versions[loop].id == id) {
ide = &ide_versions[loop];
break;
}
}
if (ide == NULL || ide->name == NULL) {
aprint_error(": rev %d is unsupported\n", id);
return;
} else
aprint_normal(": %s\n", ide->name);
if (ide->latchreg != -1) {
sc->sc_latchiot = pa->pa_iot;
if (bus_space_map(iot, pa->pa_podule->fast_base +
ide->latchreg, 1, 0, &sc->sc_latchioh)) {
aprint_error_dev(self,
"cannot map latch register\n");
return;
}
sc->sc_shutdownhook =
shutdownhook_establish(icside_v6_shutdown, sc);
}
sc->sc_tag = *pa->pa_iot;
sc->sc_tag.bs_cookie = (void *) REGISTER_SPACING_SHIFT;
sc->sc_tag.bs_rm_2 = icside_bs_rm_2;
sc->sc_tag.bs_wm_2 = icside_bs_wm_2;
sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chp;
sc->sc_wdcdev.sc_atac.atac_nchannels = ide->channels;
sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
sc->sc_wdcdev.wdc_maxdrives = 2;
sc->sc_pa = pa;
for (channel = 0; channel < ide->channels; ++channel) {
icp = &sc->sc_chan[channel];
sc->sc_wdcdev.sc_atac.atac_channels[channel] = &icp->ic_channel;
cp = &icp->ic_channel;
wdr = &sc->sc_wdc_regs[channel];
cp->ch_channel = channel;
cp->ch_atac = &sc->sc_wdcdev.sc_atac;
wdr->cmd_iot = &sc->sc_tag;
wdr->ctl_iot = &sc->sc_tag;
if (ide->modspace)
iobase = pa->pa_podule->mod_base;
else
iobase = pa->pa_podule->fast_base;
if (bus_space_map(iot, iobase + ide->ideregs[channel],
IDE_REGISTER_SPACE, 0, &wdr->cmd_baseioh))
return;
for (i = 0; i < WDC_NREG; i++) {
if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
i, i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0)
return;
}
wdc_init_shadow_regs(wdr);
if (bus_space_map(iot, iobase + ide->auxregs[channel],
AUX_REGISTER_SPACE, 0, &wdr->ctl_ioh))
return;
icp->ic_irqiot = iot;
if (bus_space_map(iot, iobase + ide->irqregs[channel],
IRQ_REGISTER_SPACE, 0, &icp->ic_irqioh))
return;
(void)bus_space_read_1(iot, icp->ic_irqioh, 0);
pa->pa_podule->irq_addr = iobase + ide->irqstatregs[channel];
pa->pa_podule->irq_mask = IRQ_STATUS_REGISTER_MASK;
icp->ic_irqaddr = pa->pa_podule->irq_addr;
icp->ic_irqmask = pa->pa_podule->irq_mask;
evcnt_attach_dynamic(&icp->ic_intrcnt, EVCNT_TYPE_INTR, NULL,
device_xname(self), "intr");
icp->ic_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
icside_intr, icp, &icp->ic_intrcnt);
if (icp->ic_ih == NULL) {
aprint_error_dev(self, "Cannot claim interrupt %d\n",
pa->pa_podule->interrupt);
continue;
}
bus_space_write_1(iot, icp->ic_irqioh, 0, 0);
wdcattach(cp);
}
}
void
icside_v6_shutdown(void *arg)
{
struct icside_softc *sc = arg;
bus_space_write_1(sc->sc_latchiot, sc->sc_latchioh, 0, 0);
}
int
icside_intr(void *arg)
{
struct icside_channel *icp = arg;
volatile u_char *intraddr = (volatile u_char *)icp->ic_irqaddr;
if ((*intraddr) & icp->ic_irqmask)
wdcintr(&icp->ic_channel);
return(0);
}