#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/extent.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/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>
#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;
char pcic_isa_intr_list[] = {
3, 4, 14, 9, 5, 12, 10, 11, 15, 13, 7, 1, 6, 2, 0, 8
};
struct pcic_ranges pcic_isa_addr[] = {
{ 0x340, 0x030 },
{ 0x300, 0x030 },
{ 0x390, 0x020 },
{ 0x400, 0xbff },
{ 0, 0 },
};
#ifdef PCICISADEBUG
int pcicsubr_debug = 1 ;
#define DPRINTF(arg) if (pcicsubr_debug) printf arg;
#else
#define DPRINTF(arg)
#endif
static int pcic_intr_seen;
void
pcic_isa_bus_width_probe(struct pcic_softc *sc, bus_space_tag_t iot,
bus_space_handle_t ioh, bus_addr_t base, u_int32_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)) {
printf("%s: can't map high i/o space\n", sc->dev.dv_xname);
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_unmap(iot, ioh_high, length);
sc->ranges = pcic_isa_addr;
if (iobuswidth == 10) {
sc->iobase = 0x000;
sc->iosize = 0x400;
} else {
sc->iobase = 0x0000;
sc->iosize = 0x1000;
}
DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
sc->dev.dv_xname, (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", sc->dev.dv_xname, (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,
char *xname)
{
struct pcic_handle *h = (struct pcic_handle *)pch;
struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
isa_chipset_tag_t ic = sc->intr_est;
int irq, ist, reg;
if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
ist = IST_LEVEL;
else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
ist = IST_PULSE;
else
ist = IST_EDGE;
irq = pcic_intr_find(sc, ist);
if (!irq)
return (NULL);
h->ih_irq = irq;
reg = pcic_read(h, PCIC_INTR);
reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
pcic_write(h, PCIC_INTR, reg | irq);
return isa_intr_establish(ic, irq, ist, ipl, fct, arg,
h->pcmcia->dv_xname);
}
void
pcic_isa_chip_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
{
struct pcic_handle *h = (struct pcic_handle *) pch;
struct pcic_softc *sc = (struct pcic_softc *)(h->ph_parent);
isa_chipset_tag_t ic = sc->intr_est;
int reg;
h->ih_irq = 0;
isa_intr_disestablish(ic, ih);
reg = pcic_read(h, PCIC_INTR);
reg &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
pcic_write(h, PCIC_INTR, reg);
}
const char *
pcic_isa_chip_intr_string(pcmcia_chipset_handle_t pch, void *ih)
{
struct pcic_handle *h = (struct pcic_handle *)pch;
static char irqstr[64];
if (ih == NULL)
snprintf(irqstr, sizeof(irqstr), "couldn't establish interrupt");
else
snprintf(irqstr, sizeof(irqstr), "irq %d", h->ih_irq);
return (irqstr);
}
int
pcic_intr_probe(void *v)
{
pcic_intr_seen = 1;
return (1);
}
int
pcic_intr_find(struct pcic_softc *sc, int ist)
{
struct pcic_handle *ph = &sc->handle[0];
isa_chipset_tag_t ic = sc->intr_est;
int i, tickle, check, irq, chosen_irq = 0, csc_touched = 0;
void *ih;
u_int8_t saved_csc_intr;
for (tickle = 1; tickle >= 0; tickle--) {
if (tickle)
saved_csc_intr = pcic_read(ph, PCIC_CSC_INTR);
for (check = 2; check; check--) {
for (i = 0; i < 16; i++) {
irq = pcic_isa_intr_list[i];
if (((1 << irq) &
PCIC_CSC_INTR_IRQ_VALIDMASK) == 0)
continue;
if (isa_intr_check(ic, irq, ist) < check)
continue;
if (!tickle) {
chosen_irq = irq;
goto out;
}
ih = isa_intr_establish(ic, irq, ist,
IPL_VM | IPL_MPSAFE, pcic_intr_probe,
0, sc->dev.dv_xname);
if (ih == NULL)
continue;
pcic_intr_seen = 0;
pcic_write(ph, PCIC_CSC_INTR,
(saved_csc_intr & ~PCIC_CSC_INTR_IRQ_MASK)
| PCIC_CSC_INTR_CD_ENABLE
| (irq << PCIC_CSC_INTR_IRQ_SHIFT));
csc_touched = 1;
pcic_write(ph, PCIC_CARD_DETECT,
pcic_read(ph, PCIC_CARD_DETECT) |
PCIC_CARD_DETECT_SW_INTR);
delay(10000);
pcic_read(ph, PCIC_CSC);
isa_intr_disestablish(ic, ih);
if (pcic_intr_seen) {
chosen_irq = irq;
goto out;
}
}
}
}
out:
if (csc_touched)
pcic_write(ph, PCIC_CSC_INTR, saved_csc_intr);
return (chosen_irq);
}