#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: algor_p6032_intr.c,v 1.24 2020/11/14 02:23:04 thorpej Exp $");
#include "opt_ddb.h"
#define __INTR_PRIVATE
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <algor/autoconf.h>
#include <mips/locore.h>
#include <dev/ic/mc146818reg.h>
#include <algor/algor/algor_p6032reg.h>
#include <algor/algor/algor_p6032var.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/isa/isavar.h>
#define NIRQMAPS 10
const char * const p6032_intrnames[NIRQMAPS] = {
"gpin 0",
"gpin 1",
"gpin 2",
"gpin 3",
"gpin 4",
"gpin 5",
"gpio 0",
"gpio 1",
"gpio 2",
"gpio 3",
};
struct p6032_irqmap {
int irqidx;
uint32_t intbit;
uint32_t gpioiebit;
int flags;
};
#define IRQ_F_INVERT 0x01
#define IRQ_F_EDGE 0x02
#define IRQ_F_INT1 0x04
const struct p6032_irqmap p6032_irqmap[NIRQMAPS] = {
{ P6032_IRQ_GPIN0, BONITO_ICU_GPIN(0),
BONITO_GPIO_INR(0), IRQ_F_INT1 },
{ P6032_IRQ_GPIN1, BONITO_ICU_GPIN(1),
BONITO_GPIO_INR(1), IRQ_F_INT1 },
{ P6032_IRQ_GPIN2, BONITO_ICU_GPIN(2),
BONITO_GPIO_INR(2), IRQ_F_INVERT },
{ P6032_IRQ_GPIN3, BONITO_ICU_GPIN(3),
BONITO_GPIO_INR(3), 0 },
{ P6032_IRQ_GPIN4, BONITO_ICU_GPIN(4),
BONITO_GPIO_INR(4), IRQ_F_INT1 },
{ P6032_IRQ_GPIN5, BONITO_ICU_GPIN(5),
BONITO_GPIO_INR(5), IRQ_F_INT1 },
{ P6032_IRQ_GPIO0, BONITO_ICU_GPIO(0),
BONITO_GPIO_IOW(0), IRQ_F_INVERT },
{ P6032_IRQ_GPIO1, BONITO_ICU_GPIO(1),
BONITO_GPIO_IOW(1), IRQ_F_INVERT },
{ P6032_IRQ_GPIO2, BONITO_ICU_GPIO(2),
BONITO_GPIO_IOW(2), IRQ_F_INVERT },
{ P6032_IRQ_GPIO3, BONITO_ICU_GPIO(3),
BONITO_GPIO_IOW(3), IRQ_F_INVERT },
};
struct p6032_intrhead {
struct evcnt intr_count;
int intr_refcnt;
};
struct p6032_intrhead p6032_intrtab[NIRQMAPS];
#define NINTRS 2
struct p6032_cpuintr {
LIST_HEAD(, evbmips_intrhand) cintr_list;
struct evcnt cintr_count;
};
struct p6032_cpuintr p6032_cpuintrs[NINTRS];
const char * const p6032_cpuintrnames[NINTRS] = {
"int 0 (pci)",
"int 1 (isa)",
};
void *algor_p6032_intr_establish(int, int (*)(void *), void *);
void algor_p6032_intr_disestablish(void *);
int algor_p6032_pci_intr_map(const struct pci_attach_args *,
pci_intr_handle_t *);
const char *algor_p6032_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
const struct evcnt *algor_p6032_pci_intr_evcnt(void *, pci_intr_handle_t);
void *algor_p6032_pci_intr_establish(void *, pci_intr_handle_t, int,
int (*)(void *), void *);
void algor_p6032_pci_intr_disestablish(void *, void *);
void algor_p6032_pci_conf_interrupt(void *, int, int, int, int, int *);
void algor_p6032_iointr(int, vaddr_t, uint32_t);
void
algor_p6032_intr_init(struct p6032_config *acp)
{
struct bonito_config *bc = &acp->ac_bonito;
const struct p6032_irqmap *irqmap;
int i;
for (i = 0; i < NINTRS; i++) {
LIST_INIT(&p6032_cpuintrs[i].cintr_list);
evcnt_attach_dynamic(&p6032_cpuintrs[i].cintr_count,
EVCNT_TYPE_INTR, NULL, "mips", p6032_cpuintrnames[i]);
}
for (i = 0; i < __arraycount(p6032_irqmap); i++) {
irqmap = &p6032_irqmap[i];
evcnt_attach_dynamic(&p6032_intrtab[i].intr_count,
EVCNT_TYPE_INTR, NULL, "bonito", p6032_intrnames[i]);
bc->bc_gpioIE |= irqmap->gpioiebit;
if (irqmap->flags & IRQ_F_INVERT)
bc->bc_intPol |= irqmap->intbit;
if (irqmap->flags & IRQ_F_EDGE)
bc->bc_intEdge |= irqmap->intbit;
if (irqmap->flags & IRQ_F_INT1)
bc->bc_intSteer |= irqmap->intbit;
REGVAL(BONITO_INTENCLR) = irqmap->intbit;
}
REGVAL(BONITO_GPIOIE) = bc->bc_gpioIE;
REGVAL(BONITO_INTEDGE) = bc->bc_intEdge;
REGVAL(BONITO_INTSTEER) = bc->bc_intSteer;
REGVAL(BONITO_INTPOL) = bc->bc_intPol;
acp->ac_pc.pc_intr_v = NULL;
acp->ac_pc.pc_intr_map = algor_p6032_pci_intr_map;
acp->ac_pc.pc_intr_string = algor_p6032_pci_intr_string;
acp->ac_pc.pc_intr_evcnt = algor_p6032_pci_intr_evcnt;
acp->ac_pc.pc_intr_establish = algor_p6032_pci_intr_establish;
acp->ac_pc.pc_intr_disestablish = algor_p6032_pci_intr_disestablish;
acp->ac_pc.pc_conf_interrupt = algor_p6032_pci_conf_interrupt;
acp->ac_pc.pc_pciide_compat_intr_establish = NULL;
algor_intr_establish = algor_p6032_intr_establish;
algor_intr_disestablish = algor_p6032_intr_disestablish;
algor_iointr = algor_p6032_iointr;
}
void
algor_p6032_cal_timer(bus_space_tag_t st, bus_space_handle_t sh)
{
u_long ctrdiff[4], startctr, endctr, cps;
uint8_t regc;
int i;
bus_space_write_1(st, sh, 0, MC_REGB);
bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
MC_REGB_24HR);
bus_space_write_1(st, sh, 0, MC_REGA);
bus_space_write_1(st, sh, 1, MC_BASE_32_KHz | MC_RATE_16_Hz);
for (i = 0; i < 4; i++) {
led_display('h', 'z', '0' + i, ' ');
bus_space_write_1(st, sh, 0, MC_REGB);
bus_space_write_1(st, sh, 1, MC_REGB_PIE | MC_REGB_SQWE |
MC_REGB_BINARY | MC_REGB_24HR);
bus_space_write_1(st, sh, 0, MC_REGC);
startctr = mips3_cp0_count_read();
do {
regc = bus_space_read_1(st, sh, 1);
endctr = mips3_cp0_count_read();
} while ((regc & MC_REGC_IRQF) == 0);
bus_space_write_1(st, sh, 0, MC_REGB);
bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY |
MC_REGB_24HR);
ctrdiff[i] = endctr - startctr;
}
cps = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16;
curcpu()->ci_cpu_freq = cps * 2;
curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz;
curcpu()->ci_divisor_delay =
((curcpu()->ci_cpu_freq + (1000000 / 2)) / 1000000);
curcpu()->ci_cycles_per_hz /= 2;
curcpu()->ci_divisor_delay /= 2;
printf("Timer calibration: %lu cycles/sec [(%lu, %lu) * 16]\n",
cps, ctrdiff[2], ctrdiff[3]);
printf("CPU clock speed = %lu.%02luMHz "
"(hz cycles = %lu, delay divisor = %lu)\n",
curcpu()->ci_cpu_freq / 1000000,
(curcpu()->ci_cpu_freq % 1000000) / 10000,
curcpu()->ci_cycles_per_hz, curcpu()->ci_divisor_delay);
}
void *
algor_p6032_intr_establish(int irq, int (*func)(void *), void *arg)
{
const struct p6032_irqmap *irqmap;
struct evbmips_intrhand *ih;
int s;
irqmap = &p6032_irqmap[irq];
KASSERT(irq == irqmap->irqidx);
ih = kmem_alloc(sizeof(*ih), KM_SLEEP);
ih->ih_func = func;
ih->ih_arg = arg;
ih->ih_irq = 0;
ih->ih_irqmap = irqmap;
s = splhigh();
if (irqmap->flags & IRQ_F_INT1)
LIST_INSERT_HEAD(&p6032_cpuintrs[1].cintr_list, ih, ih_q);
else
LIST_INSERT_HEAD(&p6032_cpuintrs[0].cintr_list, ih, ih_q);
if (p6032_intrtab[irqmap->irqidx].intr_refcnt++ == 0)
REGVAL(BONITO_INTENSET) = irqmap->intbit;
splx(s);
return (ih);
}
void
algor_p6032_intr_disestablish(void *cookie)
{
const struct p6032_irqmap *irqmap;
struct evbmips_intrhand *ih = cookie;
int s;
irqmap = ih->ih_irqmap;
s = splhigh();
LIST_REMOVE(ih, ih_q);
if (p6032_intrtab[irqmap->irqidx].intr_refcnt-- == 1)
REGVAL(BONITO_INTENCLR) = irqmap->intbit;
splx(s);
kmem_free(ih, sizeof(*ih));
}
void
algor_p6032_iointr(int ipl, vaddr_t pc, uint32_t ipending)
{
const struct p6032_irqmap *irqmap;
struct evbmips_intrhand *ih;
int level;
uint32_t isr;
if (ipending & MIPS_INT_MASK_3) {
#ifdef DDB
printf("Debug switch -- entering debugger\n");
led_display('D','D','B',' ');
Debugger();
led_display('N','B','S','D');
#else
printf("Debug switch ignored -- "
"no debugger configured\n");
#endif
}
isr = REGVAL(BONITO_INTISR) & REGVAL(BONITO_INTEN);
for (level = 1; level >= 0; level--) {
if ((ipending & (MIPS_INT_MASK_0 << level)) == 0)
continue;
p6032_cpuintrs[level].cintr_count.ev_count++;
for (ih = LIST_FIRST(&p6032_cpuintrs[level].cintr_list);
ih != NULL; ih = LIST_NEXT(ih, ih_q)) {
irqmap = ih->ih_irqmap;
if (isr & irqmap->intbit) {
p6032_intrtab[
irqmap->irqidx].intr_count.ev_count++;
(*ih->ih_func)(ih->ih_arg);
}
}
}
}
int
algor_p6032_pci_intr_map(const struct pci_attach_args *pa,
pci_intr_handle_t *ihp)
{
static const int pciirqmap[6][4] = {
{ P6032_IRQ_GPIO0, P6032_IRQ_GPIO1,
P6032_IRQ_GPIO2, P6032_IRQ_GPIO3 },
{ P6032_IRQ_GPIO1, P6032_IRQ_GPIO2,
P6032_IRQ_GPIO3, P6032_IRQ_GPIO0 },
{ P6032_IRQ_GPIO2, P6032_IRQ_GPIO3,
P6032_IRQ_GPIO0, P6032_IRQ_GPIO1 },
{ P6032_IRQ_GPIN2, -1,
-1, -1 },
{ P6032_IRQ_GPIO0, P6032_IRQ_GPIO1,
P6032_IRQ_GPIO2, P6032_IRQ_GPIO3 },
{ P6032_IRQ_GPIO3, P6032_IRQ_GPIO0,
P6032_IRQ_GPIO1, P6032_IRQ_GPIO2 },
};
pcitag_t bustag = pa->pa_intrtag;
int buspin = pa->pa_intrpin;
pci_chipset_tag_t pc = pa->pa_pc;
int device, irq;
if (buspin == 0) {
return (1);
}
if (buspin > 4) {
printf("algor_p6032_pci_intr_map: bad interrupt pin %d\n",
buspin);
return (1);
}
pci_decompose_tag(pc, bustag, NULL, &device, NULL);
if (device < 13 || device > 18) {
printf("algor_p6032_pci_intr_map: bad device %d\n",
device);
return (1);
}
irq = pciirqmap[device - 13][buspin - 1];
if (irq == -1) {
printf("algor_p6032_pci_intr_map: no mapping for "
"device %d pin %d\n", device, buspin);
return (1);
}
*ihp = irq;
return (0);
}
const char *
algor_p6032_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
{
if (ih >= NIRQMAPS)
panic("algor_p6032_intr_string: bogus IRQ %ld", ih);
strlcpy(buf, p6032_intrnames[ih], len);
return buf;
}
const struct evcnt *
algor_p6032_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
{
return (&p6032_intrtab[ih].intr_count);
}
void *
algor_p6032_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
int (*func)(void *), void *arg)
{
if (ih >= NIRQMAPS)
panic("algor_p6032_intr_establish: bogus IRQ %ld", ih);
return (algor_p6032_intr_establish(ih, func, arg));
}
void
algor_p6032_pci_intr_disestablish(void *v, void *cookie)
{
return (algor_p6032_intr_disestablish(cookie));
}
void
algor_p6032_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
int *iline)
{
*iline = 0;
}