#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: isabus.c,v 1.54 2023/12/07 03:46:10 thorpej Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/vmem_impl.h>
#include <uvm/uvm_extern.h>
#include <machine/cpu.h>
#include <machine/pio.h>
#include <machine/autoconf.h>
#include <machine/intr.h>
#include <mips/locore.h>
#include <dev/ic/i8253reg.h>
#include <dev/ic/i8259reg.h>
#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <arc/isa/isabrvar.h>
#include <arc/isa/spkrreg.h>
#include <arc/arc/timervar.h>
static int beeping;
static callout_t sysbeep_ch;
#define ISA_MEM_BTAG_COUNT VMEM_EST_BTCOUNT(1, 16)
#define ISA_IO_BTAG_COUNT VMEM_EST_BTCOUNT(1, 16)
static struct vmem isa_mem_arena_store;
static struct vmem isa_io_arena_store;
static struct vmem_btag isa_mem_btag_store[ISA_MEM_BTAG_COUNT];
static struct vmem_btag isa_io_btag_store[ISA_IO_BTAG_COUNT];
#define IRQ_SLAVE 2
static int isabrprint(void *, const char *);
extern struct arc_bus_space arc_bus_io, arc_bus_mem;
static void isabr_attach_hook(device_t , device_t,
struct isabus_attach_args *);
static void isabr_detach_hook(isa_chipset_tag_t, device_t);
static const struct evcnt *isabr_intr_evcnt(isa_chipset_tag_t, int);
static void *isabr_intr_establish(isa_chipset_tag_t, int, int, int,
int (*)(void *), void *);
static void isabr_intr_disestablish(isa_chipset_tag_t, void*);
static void isabr_initicu(void);
static void intr_calculatemasks(void);
static int fakeintr(void *a);
struct isabr_config *isabr_conf = NULL;
uint32_t imask[_IPL_N];
void
isabrattach(struct isabr_softc *sc)
{
struct isabus_attach_args iba;
callout_init(&sysbeep_ch, 0);
if (isabr_conf == NULL)
panic("isabr_conf isn't initialized");
aprint_normal("\n");
isabr_initicu();
sc->arc_isa_cs.ic_attach_hook = isabr_attach_hook;
sc->arc_isa_cs.ic_detach_hook = isabr_detach_hook;
sc->arc_isa_cs.ic_intr_evcnt = isabr_intr_evcnt;
sc->arc_isa_cs.ic_intr_establish = isabr_intr_establish;
sc->arc_isa_cs.ic_intr_disestablish = isabr_intr_disestablish;
arc_bus_space_init_arena(&arc_bus_mem, &isa_mem_arena_store,
isa_mem_btag_store, ISA_MEM_BTAG_COUNT);
arc_bus_space_init_arena(&arc_bus_io, &isa_io_arena_store,
isa_io_btag_store, ISA_IO_BTAG_COUNT);
iba.iba_iot = &arc_bus_io;
iba.iba_memt = &arc_bus_mem;
iba.iba_dmat = &sc->sc_dmat;
iba.iba_ic = &sc->arc_isa_cs;
config_found(sc->sc_dev, &iba, isabrprint,
CFARGS(.iattr = "isabus"));
}
static int
isabrprint(void *aux, const char *pnp)
{
if (pnp)
aprint_normal("isa at %s", pnp);
aprint_verbose(" isa_io_base 0x%"PRIxVADDR" isa_mem_base 0x%"PRIxVADDR,
arc_bus_io.bs_vbase, arc_bus_mem.bs_vbase);
return UNCONF;
}
#define LEGAL_IRQ(x) ((x) >= 0 && (x) < ICU_LEN && (x) != 2)
int imen;
int intrtype[ICU_LEN], intrmask[ICU_LEN], intrlevel[ICU_LEN];
struct isa_intrhand *isa_intrhand[ICU_LEN];
static int
fakeintr(void *a)
{
return 0;
}
static void
intr_calculatemasks(void)
{
int irq, level;
struct isa_intrhand *q;
for (irq = 0; irq < ICU_LEN; irq++) {
int levels = 0;
for (q = isa_intrhand[irq]; q; q = q->ih_next)
levels |= 1 << q->ih_level;
intrlevel[irq] = levels;
}
for (level = 0; level < _IPL_N; level++) {
int irqs = 0;
for (irq = 0; irq < ICU_LEN; irq++)
if (intrlevel[irq] & (1 << level))
irqs |= 1 << irq;
imask[level] = irqs;
}
imask[IPL_NONE] = 0;
imask[IPL_SOFTCLOCK] |= imask[IPL_NONE];
imask[IPL_SOFTNET] |= imask[IPL_SOFTCLOCK];
imask[IPL_VM] |= imask[IPL_SOFTNET];
imask[IPL_SCHED] |= imask[IPL_VM];
for (irq = 0; irq < ICU_LEN; irq++) {
int irqs = 1 << irq;
for (q = isa_intrhand[irq]; q; q = q->ih_next)
irqs |= imask[q->ih_level];
intrmask[irq] = irqs;
}
{
int irqs = 0;
for (irq = 0; irq < ICU_LEN; irq++)
if (isa_intrhand[irq])
irqs |= 1 << irq;
if (irqs >= 0x100)
irqs |= 1 << IRQ_SLAVE;
imen = ~irqs;
isa_outb(IO_ICU1 + PIC_OCW1, imen);
isa_outb(IO_ICU2 + PIC_OCW1, imen >> 8);
}
}
static void
isabr_attach_hook(device_t parent, device_t self,
struct isabus_attach_args *iba)
{
}
static void
isabr_detach_hook(isa_chipset_tag_t ic, device_t self)
{
}
static const struct evcnt *
isabr_intr_evcnt(isa_chipset_tag_t ic, int irq)
{
return NULL;
}
static void *
isabr_intr_establish(isa_chipset_tag_t ic, int irq, int type, int level,
int (*ih_fun)(void *), void *ih_arg)
{
struct isa_intrhand **p, *q, *ih;
static struct isa_intrhand fakehand = {NULL, fakeintr};
ih = kmem_alloc(sizeof *ih, KM_SLEEP);
if (!LEGAL_IRQ(irq) || type == IST_NONE)
panic("intr_establish: bogus irq or type");
switch (intrtype[irq]) {
case IST_NONE:
intrtype[irq] = type;
break;
case IST_EDGE:
case IST_LEVEL:
if (type == intrtype[irq])
break;
case IST_PULSE:
if (type != IST_NONE)
panic("intr_establish: can't share %s with %s",
isa_intr_typename(intrtype[irq]),
isa_intr_typename(type));
break;
}
for (p = &isa_intrhand[irq]; (q = *p) != NULL; p = &q->ih_next)
;
fakehand.ih_level = level;
*p = &fakehand;
intr_calculatemasks();
ih->ih_fun = ih_fun;
ih->ih_arg = ih_arg;
ih->ih_count = 0;
ih->ih_next = NULL;
ih->ih_level = level;
ih->ih_irq = irq;
snprintf(ih->ih_evname, sizeof(ih->ih_evname), "irq %d", irq);
evcnt_attach_dynamic(&ih->ih_evcnt, EVCNT_TYPE_INTR, NULL, "isa",
ih->ih_evname);
*p = ih;
return ih;
}
static void
isabr_intr_disestablish(isa_chipset_tag_t ic, void *arg)
{
}
uint32_t
isabr_iointr(uint32_t mask, struct clockframe *cf)
{
struct isa_intrhand *ih;
int isa_vector;
int o_imen;
isa_vector = (*isabr_conf->ic_intr_status)();
if (isa_vector < 0)
return 0;
o_imen = imen;
imen |= 1 << (isa_vector & (ICU_LEN - 1));
if (isa_vector & 0x08) {
isa_inb(IO_ICU2 + PIC_OCW1);
isa_outb(IO_ICU2 + PIC_OCW1, imen >> 8);
isa_outb(IO_ICU2 + PIC_OCW2,
OCW2_SELECT | OCW2_EOI | OCW2_SL |
OCW2_ILS((isa_vector & 7)));
isa_outb(IO_ICU1,
OCW2_SELECT | OCW2_EOI | OCW2_SL | IRQ_SLAVE);
} else {
isa_inb(IO_ICU1 + PIC_OCW1);
isa_outb(IO_ICU1 + PIC_OCW1, imen);
isa_outb(IO_ICU1 + PIC_OCW2,
OCW2_SELECT | OCW2_EOI | OCW2_SL | OCW2_ILS(isa_vector));
}
ih = isa_intrhand[isa_vector];
if (isa_vector == 0 && ih) {
last_cp0_count = mips3_cp0_count_read();
cf->sr &= ~MIPS_SR_INT_IE;
if ((*ih->ih_fun)(cf))
ih->ih_evcnt.ev_count++;
ih = ih->ih_next;
}
while (ih) {
if ((*ih->ih_fun)(ih->ih_arg))
ih->ih_evcnt.ev_count++;
ih = ih->ih_next;
}
imen = o_imen;
isa_inb(IO_ICU1 + PIC_OCW1);
isa_inb(IO_ICU2 + PIC_OCW1);
isa_outb(IO_ICU1 + PIC_OCW1, imen);
isa_outb(IO_ICU2 + PIC_OCW1, imen >> 8);
return MIPS_INT_MASK_2;
}
static void
isabr_initicu(void)
{
int i;
for (i = 0; i < ICU_LEN; i++) {
switch (i) {
case 2:
case 8:
intrtype[i] = IST_EDGE;
break;
default:
intrtype[i] = IST_NONE;
break;
}
}
isa_outb(IO_ICU1 + PIC_ICW1, ICW1_SELECT | ICW1_IC4);
isa_outb(IO_ICU1 + PIC_ICW2, 0);
isa_outb(IO_ICU1 + PIC_ICW3, ICW3_CASCADE(IRQ_SLAVE));
isa_outb(IO_ICU1 + PIC_ICW4, ICW4_8086);
isa_outb(IO_ICU1 + PIC_OCW1, 0xff);
isa_outb(IO_ICU1 + PIC_OCW3, OCW3_SELECT | OCW3_SSMM | OCW3_SMM);
isa_outb(IO_ICU1 + PIC_OCW3, OCW3_SELECT | OCW3_RR);
#ifdef REORDER_IRQ
isa_outb(IO_ICU1 + PIC_OCW2,
OCW2_SELECT | OCW2_R | OCW2_SL OCW2_ILS(3 - 1));
#endif
isa_outb(IO_ICU2 + PIC_ICW1, ICW1_SELECT | ICW1_IC4);
isa_outb(IO_ICU2 + PIC_ICW2, 8);
isa_outb(IO_ICU2 + PIC_ICW3, ICW3_SIC(IRQ_SLAVE));
isa_outb(IO_ICU2 + PIC_ICW4, ICW4_8086);
isa_outb(IO_ICU2 + PIC_OCW1, 0xff);
isa_outb(IO_ICU2 + PIC_OCW3, OCW3_SELECT | OCW3_SSMM | OCW3_SMM);
isa_outb(IO_ICU2 + PIC_OCW3, OCW3_SELECT | OCW3_RR);
}
void
sysbeepstop(void *arg)
{
int s;
s = splhigh();
isa_outb(PITAUX_PORT, isa_inb(PITAUX_PORT) & ~PIT_SPKR);
splx(s);
beeping = 0;
}
void
sysbeep(int pitch, int period)
{
static int last_pitch, last_period;
int s;
if (cold)
return;
if (beeping)
callout_stop(&sysbeep_ch);
if (!beeping || last_pitch != pitch) {
s = splhigh();
isa_outb(IO_TIMER1 + TIMER_MODE,
TIMER_SEL2 | TIMER_16BIT | TIMER_SQWAVE);
isa_outb(IO_TIMER1 + TIMER_CNTR2, TIMER_DIV(pitch) % 256);
isa_outb(IO_TIMER1 + TIMER_CNTR2, TIMER_DIV(pitch) / 256);
isa_outb(PITAUX_PORT, isa_inb(PITAUX_PORT) | PIT_SPKR);
splx(s);
}
last_pitch = pitch;
beeping = last_period = period;
callout_reset(&sysbeep_ch, period, sysbeepstop, NULL);
}
int
isa_intr_alloc(isa_chipset_tag_t c, int mask, int type, int *irq_p)
{
int irq;
int maybe_irq = -1;
int shared_depth = 0;
mask &= 0x8b28;
for (irq = 0; mask != 0; mask >>= 1, irq++) {
if ((mask & 1) == 0)
continue;
if (intrtype[irq] == IST_NONE) {
*irq_p = irq;
return 0;
}
if (type == IST_LEVEL && intrtype[irq] == IST_LEVEL) {
struct isa_intrhand *ih = isa_intrhand[irq];
int depth;
if (maybe_irq == -1) {
maybe_irq = irq;
continue;
}
for (depth = 0; ih != NULL; ih = ih->ih_next)
depth++;
if (depth < shared_depth) {
maybe_irq = irq;
shared_depth = depth;
}
}
}
if (maybe_irq != -1) {
*irq_p = maybe_irq;
return 0;
}
return 1;
}