#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.24 2021/08/13 11:40:43 skrll Exp $");
#include "opt_irqstats.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#define _ARM32_BUS_DMA_PRIVATE
#include <sys/bus.h>
#include <machine/intr.h>
#include <machine/pio.h>
#include <machine/bootconfig.h>
#include <machine/isa_machdep.h>
#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isadmareg.h>
#include <dev/isa/isadmavar.h>
#include <arm/footbridge/isa/icu.h>
#include <arm/footbridge/dc21285reg.h>
#include <arm/footbridge/dc21285mem.h>
#include <uvm/uvm_extern.h>
#include "isadma.h"
static void isa_icu_init(void);
struct arm32_isa_chipset isa_chipset_tag;
void isa_strayintr(int);
void intr_calculatemasks(void);
int fakeintr(void *);
int isa_irqdispatch(void *arg);
u_int imask[NIPL];
unsigned imen;
#define AUTO_EOI_1
#define AUTO_EOI_2
static void
isa_icu_init(void)
{
outb(IO_ICU1, 0x11);
outb(IO_ICU1+1, ICU_OFFSET);
outb(IO_ICU1+1, 1 << IRQ_SLAVE);
#ifdef AUTO_EOI_1
outb(IO_ICU1+1, 2 | 1);
#else
outb(IO_ICU1+1, 1);
#endif
outb(IO_ICU1+1, 0xff);
outb(IO_ICU1, 0x68);
outb(IO_ICU1, 0x0a);
#ifdef REORDER_IRQ
outb(IO_ICU1, 0xc0 | (3 - 1));
#endif
outb(IO_ICU2, 0x11);
outb(IO_ICU2+1, ICU_OFFSET+8);
outb(IO_ICU2+1, IRQ_SLAVE);
#ifdef AUTO_EOI_2
outb(IO_ICU2+1, 2 | 1);
#else
outb(IO_ICU2+1, 1);
#endif
outb(IO_ICU2+1, 0xff);
outb(IO_ICU2, 0x68);
outb(IO_ICU2, 0x0a);
}
void
isa_strayintr(int irq)
{
static u_long strays;
if (++strays <= 5)
log(LOG_ERR, "stray interrupt %d%s\n", irq,
strays >= 5 ? "; stopped logging" : "");
}
static struct intrq isa_intrq[ICU_LEN];
void
intr_calculatemasks(void)
{
int irq, level;
struct intrq *iq;
struct intrhand *ih;
for (irq = 0; irq < ICU_LEN; irq++) {
int levels = 0;
iq = &isa_intrq[irq];
for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
ih = TAILQ_NEXT(ih, ih_list))
levels |= (1U << ih->ih_ipl);
iq->iq_levels = levels;
}
for (level = 0; level < NIPL; level++) {
int irqs = 0;
for (irq = 0; irq < ICU_LEN; irq++)
if (isa_intrq[irq].iq_levels & (1U << level))
irqs |= (1U << irq);
imask[level] = irqs;
}
imask[IPL_SCHED] |= imask[IPL_VM];
imask[IPL_HIGH] |= imask[IPL_SCHED];
for (irq = 0; irq < ICU_LEN; irq++) {
int irqs = 1 << irq;
iq = &isa_intrq[irq];
for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
ih = TAILQ_NEXT(ih, ih_list))
irqs |= imask[ih->ih_ipl];
iq->iq_mask = irqs;
}
{
int irqs = 0;
for (irq = 0; irq < ICU_LEN; irq++)
if (!TAILQ_EMPTY(&isa_intrq[irq].iq_list))
irqs |= (1U << irq);
if (irqs >= 0x100)
irqs |= 1 << IRQ_SLAVE;
imen = ~irqs;
SET_ICUS();
}
#if 0
printf("type\tmask\tlevel\thand\n");
for (irq = 0; irq < ICU_LEN; irq++) {
printf("%x\t%04x\t%x\t%p\n", intrtype[irq], intrmask[irq],
intrlevel[irq], intrhand[irq]);
}
for (level = 0; level < IPL_LEVELS; ++level)
printf("%d: %08x\n", level, imask[level]);
#endif
}
int
fakeintr(void *arg)
{
return 0;
}
#define LEGAL_IRQ(x) ((x) >= 0 && (x) < ICU_LEN && (x) != 2)
int
isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
{
int i, tmp, bestirq, count;
struct intrq *iq;
struct intrhand *ih;
if (type == IST_NONE)
panic("intr_alloc: bogus type");
bestirq = -1;
count = -1;
mask &= 0xdef8;
mask &= 0xefbf;
for (i = 0; i < ICU_LEN; i++) {
if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
continue;
iq = &isa_intrq[i];
switch(iq->iq_ist) {
case IST_NONE:
*irq = i;
return (0);
case IST_EDGE:
case IST_LEVEL:
if (type != iq->iq_ist)
continue;
tmp = 0;
TAILQ_FOREACH(ih, &(iq->iq_list), ih_list)
tmp++;
if ((bestirq == -1) || (count > tmp)) {
bestirq = i;
count = tmp;
}
break;
case IST_PULSE:
continue;
}
}
if (bestirq == -1)
return (1);
*irq = bestirq;
return (0);
}
const struct evcnt *
isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
{
return &isa_intrq[irq].iq_ev;
}
void *
isa_intr_establish(isa_chipset_tag_t ic, int irq, int type, int level, int (*ih_fun)(void *), void *ih_arg)
{
struct intrq *iq;
struct intrhand *ih;
u_int oldirqstate;
#if 0
printf("isa_intr_establish(%d, %d, %d)\n", irq, type, level);
#endif
ih = kmem_alloc(sizeof *ih, KM_SLEEP);
if (!LEGAL_IRQ(irq) || type == IST_NONE)
panic("intr_establish: bogus irq or type");
iq = &isa_intrq[irq];
switch (iq->iq_ist) {
case IST_NONE:
iq->iq_ist = type;
#if 0
printf("Setting irq %d to type %d - ", irq, type);
#endif
if (irq < 8) {
outb(0x4d0, (inb(0x4d0) & ~(1 << irq))
| ((type == IST_LEVEL) ? (1 << irq) : 0));
} else {
outb(0x4d1, (inb(0x4d1) & ~(1 << irq))
| ((type == IST_LEVEL) ? (1 << irq) : 0));
}
break;
case IST_EDGE:
case IST_LEVEL:
if (iq->iq_ist == type)
break;
case IST_PULSE:
if (type != IST_NONE)
panic("intr_establish: can't share %s with %s",
isa_intr_typename(iq->iq_ist),
isa_intr_typename(type));
break;
}
ih->ih_func = ih_fun;
ih->ih_arg = ih_arg;
ih->ih_ipl = level;
ih->ih_irq = irq;
oldirqstate = disable_interrupts(I32_bit);
TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
intr_calculatemasks();
restore_interrupts(oldirqstate);
return (ih);
}
void
isa_intr_disestablish(isa_chipset_tag_t ic, void *arg)
{
struct intrhand *ih = arg;
struct intrq *iq = &isa_intrq[ih->ih_irq];
int irq = ih->ih_irq;
u_int oldirqstate;
if (!LEGAL_IRQ(irq))
panic("intr_disestablish: bogus irq");
oldirqstate = disable_interrupts(I32_bit);
TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
intr_calculatemasks();
restore_interrupts(oldirqstate);
kmem_free(ih, sizeof(*ih));
if (TAILQ_EMPTY(&(iq->iq_list)))
iq->iq_ist = IST_NONE;
}
void
isa_intr_init(void)
{
struct intrq *iq;
int i;
for (i = 0; i < ICU_LEN; i++) {
iq = &isa_intrq[i];
TAILQ_INIT(&iq->iq_list);
snprintf(iq->iq_name, sizeof(iq->iq_name), "irq %d", i);
evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
NULL, "isa", iq->iq_name);
}
isa_icu_init();
intr_calculatemasks();
#ifndef ISA_FOOTBRIDGE_IRQ
#warning Before using isa with footbridge you must define ISA_FOOTBRIDGE_IRQ
#endif
footbridge_intr_claim(ISA_FOOTBRIDGE_IRQ, IPL_BIO, "isabus",
isa_irqdispatch, NULL);
}
#if NISADMA > 0
struct arm32_dma_range machdep_isa_dma_ranges[1];
#endif
void
isa_footbridge_init(u_int iobase, u_int membase)
{
#if NISADMA > 0
extern struct arm32_dma_range *footbridge_isa_dma_ranges;
extern int footbridge_isa_dma_nranges;
machdep_isa_dma_ranges[0].dr_sysbase = bootconfig.dram[0].address;
machdep_isa_dma_ranges[0].dr_busbase = bootconfig.dram[0].address;
machdep_isa_dma_ranges[0].dr_len = (16 * 1024 * 1024);
footbridge_isa_dma_ranges = machdep_isa_dma_ranges;
footbridge_isa_dma_nranges = 1;
#endif
isa_io_init(iobase, membase);
}
void
isa_attach_hook(device_t parent, device_t self, struct isabus_attach_args *iba)
{
iba->iba_ic = &isa_chipset_tag;
#if NISADMA > 0
isa_dma_init();
#endif
}
void
isa_detach_hook(isa_chipset_tag_t ic, device_t self)
{
#if NISADMA > 0
isa_dmadestroy(ic);
#endif
}
int
isa_irqdispatch(void *arg)
{
struct clockframe *frame = arg;
int irq;
struct intrq *iq;
struct intrhand *ih;
u_int iack;
int res = 0;
iack = *((u_int *)(DC21285_PCI_IACK_VBASE));
iack &= 0xff;
if (iack < 0x20 || iack > 0x2f) {
printf("isa_irqdispatch: %x\n", iack);
return(0);
}
irq = iack & 0x0f;
iq = &isa_intrq[irq];
iq->iq_ev.ev_count++;
for (ih = TAILQ_FIRST(&iq->iq_list); res != 1 && ih != NULL;
ih = TAILQ_NEXT(ih, ih_list)) {
res = (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
}
return res;
}
void
isa_fillw(u_int val, void *addr, size_t len)
{
if ((u_int)addr >= isa_mem_data_vaddr()
&& (u_int)addr < isa_mem_data_vaddr() + 0x100000) {
bus_size_t offset = ((u_int)addr) & 0xfffff;
bus_space_set_region_2(&isa_mem_bs_tag,
(bus_space_handle_t)isa_mem_bs_tag.bs_cookie, offset,
val, len);
} else {
u_short *ptr = addr;
while (len > 0) {
*ptr++ = val;
--len;
}
}
}