#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: i82365_isasubr.c,v 1.50 2024/09/08 09:36:50 rillig Exp $");
#define PCICISADEBUG
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/extent.h>
#include <sys/bus.h>
#include <sys/intr.h>
#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/pcmcia/pcmciareg.h>
#include <dev/pcmcia/pcmciavar.h>
#include <dev/pcmcia/pcmciachip.h>
#include <dev/ic/i82365reg.h>
#include <dev/ic/i82365var.h>
#include <dev/isa/i82365_isavar.h>
#include "opt_pcic_isa_alloc_iobase.h"
#include "opt_pcic_isa_alloc_iosize.h"
#include "opt_pcic_isa_intr_alloc_mask.h"
#ifndef PCIC_ISA_ALLOC_IOBASE
#define PCIC_ISA_ALLOC_IOBASE 0
#endif
#ifndef PCIC_ISA_ALLOC_IOSIZE
#define PCIC_ISA_ALLOC_IOSIZE 0
#endif
int pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
int pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
#ifndef PCIC_ISA_INTR_ALLOC_MASK
#define PCIC_ISA_INTR_ALLOC_MASK 0xffff
#endif
int pcic_isa_intr_alloc_mask = PCIC_ISA_INTR_ALLOC_MASK;
#ifndef PCIC_IRQ_PROBE
#ifdef hpcmips
#define PCIC_IRQ_PROBE 0
#else
#define PCIC_IRQ_PROBE 1
#endif
#endif
int pcic_irq_probe = PCIC_IRQ_PROBE;
#ifdef PCICISADEBUG
int pcicsubr_debug = 0;
#define DPRINTF(arg) do { if (pcicsubr_debug) printf arg ; } while (0)
#else
#define DPRINTF(arg)
#endif
void pcic_isa_probe_interrupts(struct pcic_isa_softc *, struct pcic_handle *);
static int pcic_isa_count_intr(void *);
static int
pcic_isa_count_intr(void *arg)
{
struct pcic_softc *sc;
struct pcic_isa_softc *isc;
struct pcic_handle *h;
int cscreg;
h = arg;
isc = device_private(h->ph_parent);
sc = &isc->sc_pcic;
cscreg = pcic_read(h, PCIC_CSC);
if (cscreg & PCIC_CSC_CD) {
if ((++sc->intr_detect % 20) == 0)
printf(".");
else
DPRINTF(("."));
return 1;
}
if (++sc->intr_false > 40) {
pcic_write(h, PCIC_CSC_INTR, 0);
delay(10);
}
#ifdef PCICISADEBUG
if (cscreg)
DPRINTF(("o"));
else
DPRINTF(("X"));
#endif
return cscreg ? 1 : 0;
}
void
pcic_isa_probe_interrupts(struct pcic_isa_softc *isc, struct pcic_handle *h)
{
struct pcic_softc *sc = &isc->sc_pcic;
isa_chipset_tag_t ic;
int i, j, mask, irq;
int cd, cscintr, intr, csc;
ic = isc->sc_ic;
printf("%s: controller %d detecting irqs with mask 0x%04x:",
device_xname(sc->dev), h->chip, sc->intr_mask[h->chip]);
DPRINTF(("\n"));
pcic_read(h, PCIC_CSC);
pcic_write(h, PCIC_CSC_INTR, 0);
intr = pcic_read(h, PCIC_INTR);
DPRINTF(("pcic: old intr 0x%x\n", intr));
intr &= ~(PCIC_INTR_RI_ENABLE | PCIC_INTR_ENABLE | PCIC_INTR_IRQ_MASK);
pcic_write(h, PCIC_INTR, intr);
pcic_read(h, PCIC_CSC);
cd = pcic_read(h, PCIC_CARD_DETECT);
cd |= PCIC_CARD_DETECT_SW_INTR;
mask = 0;
for (i = 0; i < 16; i++) {
if ((sc->intr_mask[h->chip] & (1 << i)) == 0)
continue;
DPRINTF(("probing irq %d: ", i));
if (isa_intr_alloc(ic, (1 << i), IST_PULSE, &irq)) {
DPRINTF(("currently allocated\n"));
continue;
}
cscintr = PCIC_CSC_INTR_CD_ENABLE;
cscintr |= (irq << PCIC_CSC_INTR_IRQ_SHIFT);
pcic_write(h, PCIC_CSC_INTR, cscintr);
delay(10);
(void) pcic_read(h, PCIC_CSC);
if ((sc->ih = isa_intr_establish(ic, irq, IST_EDGE, IPL_TTY,
pcic_isa_count_intr, h)) == NULL)
panic("cant get interrupt");
sc->intr_detect = 0;
for (j = 0; j < 40 && sc->ih; j++) {
sc->intr_false = 0;
pcic_write(h, PCIC_CARD_DETECT, cd);
delay(100);
csc = pcic_read(h, PCIC_CSC);
DPRINTF(("%s", csc ? "-" : ""));
}
DPRINTF((" total %d\n", sc->intr_detect));
if (sc->intr_detect > 37 && sc->intr_detect <= 40) {
printf("%d", i);
DPRINTF((" succeded\n"));
mask |= (1 << i);
}
if (sc->ih != NULL) {
isa_intr_disestablish(ic, sc->ih);
sc->ih = NULL;
pcic_write(h, PCIC_CSC_INTR, 0);
delay(10);
}
}
sc->intr_mask[h->chip] = mask;
printf("%s\n", sc->intr_mask[h->chip] ? "" : " none");
}
void
pcic_isa_config_interrupts(device_t self)
{
struct pcic_softc *sc;
struct pcic_isa_softc *isc;
struct pcic_handle *h;
isa_chipset_tag_t ic;
int s, i, chipmask, chipuniq;
isc = device_private(self);
sc = &isc->sc_pcic;
ic = isc->sc_ic;
chipmask = 0xffff;
for (i = 0; i < PCIC_NSLOTS; i += 2) {
if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
h = &sc->handle[i];
else if (sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP)
h = &sc->handle[i + 1];
else
continue;
sc->intr_mask[h->chip] =
PCIC_INTR_IRQ_VALIDMASK & pcic_isa_intr_alloc_mask;
if (pcic_irq_probe != 0 &&
h->vendor != PCIC_VENDOR_CIRRUS_PD67XX)
pcic_isa_probe_interrupts(isc, h);
chipmask &= sc->intr_mask[h->chip];
}
chipuniq = 1;
for (i = 0; i < PCIC_NSLOTS; i += 2) {
if ((sc->handle[i].flags & PCIC_FLAG_SOCKETP) == 0 &&
(sc->handle[i + 1].flags & PCIC_FLAG_SOCKETP) == 0)
continue;
if ((sc->intr_mask[i / 2] & ~chipmask) == 0) {
chipuniq = 0;
break;
}
}
s = splhigh();
if ((device_cfdata(self)->cf_flags & 1) == 0) {
if (sc->irq != ISA_UNKNOWN_IRQ) {
if ((chipmask & (1 << sc->irq)) == 0)
printf("%s: warning: configured irq %d not "
"detected as available\n",
device_xname(self), sc->irq);
} else if (chipmask == 0 ||
isa_intr_alloc(ic, chipmask, IST_EDGE, &sc->irq)) {
aprint_error_dev(self, "no available irq; ");
sc->irq = ISA_UNKNOWN_IRQ;
} else if ((chipmask & ~(1 << sc->irq)) == 0 && chipuniq == 0) {
aprint_error_dev(self, "can't share irq with cards; ");
sc->irq = ISA_UNKNOWN_IRQ;
}
} else {
printf("%s: ", device_xname(self));
sc->irq = ISA_UNKNOWN_IRQ;
}
if (sc->irq != ISA_UNKNOWN_IRQ) {
sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
pcic_intr, sc);
if (sc->ih == NULL) {
aprint_error_dev(self, "can't establish interrupt");
sc->irq = ISA_UNKNOWN_IRQ;
}
}
if (sc->irq == ISA_UNKNOWN_IRQ)
printf("polling for socket events\n");
else
printf("%s: using irq %d for socket events\n",
device_xname(self), sc->irq);
pcic_attach_sockets_finish(sc);
splx(s);
}
void pcic_isa_bus_width_probe(struct pcic_softc *sc, bus_space_tag_t iot,
bus_space_handle_t ioh, bus_addr_t base, uint32_t length)
{
bus_space_handle_t ioh_high;
int i, iobuswidth, tmp1, tmp2;
iobuswidth = 12;
if (bus_space_map(iot, base + 0x400, length, 0, &ioh_high)) {
aprint_error_dev(sc->dev, "can't map high i/o space\n");
return;
}
for (i = 0; i < PCIC_NSLOTS; i++) {
if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
sc->handle[i].sock + PCIC_IDENT);
tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
sc->handle[i].sock + PCIC_IDENT);
tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
if (tmp1 == tmp2)
iobuswidth = 10;
}
}
bus_space_free(iot, ioh_high, length);
if (iobuswidth == 10) {
sc->iobase = 0x300;
sc->iosize = 0x0ff;
} else {
sc->iobase = 0x400;
sc->iosize = 0xbff;
}
DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
device_xname(sc->dev), (long) sc->iobase,
(long)(sc->iobase + sc->iosize)));
if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
sc->iobase = pcic_isa_alloc_iobase;
sc->iosize = pcic_isa_alloc_iosize;
DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
"(config override)\n", device_xname(sc->dev),
(long) sc->iobase, (long)(sc->iobase + sc->iosize)));
}
}
void *
pcic_isa_chip_intr_establish(pcmcia_chipset_handle_t pch,
struct pcmcia_function *pf, int ipl, int (*fct)(void *), void *arg)
{
struct pcic_handle *h = (struct pcic_handle *) pch;
struct pcic_isa_softc *isc = device_private(h->ph_parent);
struct pcic_softc *sc = &isc->sc_pcic;
isa_chipset_tag_t ic = isc->sc_ic;
int irq, ist;
void *ih;
int reg;
if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
ist = IST_EDGE;
else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
ist = IST_PULSE;
else
ist = IST_EDGE;
if (isa_intr_alloc(ic, sc->intr_mask[h->chip], ist, &irq))
return NULL;
h->ih_irq = irq;
if (h->flags & PCIC_FLAG_ENABLED) {
reg = pcic_read(h, PCIC_INTR);
reg &= ~PCIC_INTR_IRQ_MASK;
pcic_write(h, PCIC_INTR, reg | irq);
}
if ((ih = isa_intr_establish(ic, irq, ist, ipl, fct, arg)) == NULL)
return NULL;
printf("%s: card irq %d\n", device_xname(h->pcmcia), irq);
return ih;
}
void
pcic_isa_chip_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
{
struct pcic_handle *h = (struct pcic_handle *) pch;
struct pcic_isa_softc *isc = device_private(h->ph_parent);
isa_chipset_tag_t ic = isc->sc_ic;
int reg;
isa_intr_disestablish(ic, ih);
h->ih_irq = 0;
if (h->flags & PCIC_FLAG_ENABLED) {
reg = pcic_read(h, PCIC_INTR);
reg &= ~PCIC_INTR_IRQ_MASK;
pcic_write(h, PCIC_INTR, reg);
}
}