#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: iq80310_pci.c,v 1.16 2019/03/01 09:25:59 msaitoh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <machine/autoconf.h>
#include <sys/bus.h>
#include <evbarm/iq80310/iq80310reg.h>
#include <evbarm/iq80310/iq80310var.h>
#include <arm/xscale/i80312reg.h>
#include <arm/xscale/i80312var.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/ppbreg.h>
int iq80310_pci_intr_map(const struct pci_attach_args *,
pci_intr_handle_t *);
const char *iq80310_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
const struct evcnt *iq80310_pci_intr_evcnt(void *, pci_intr_handle_t);
void *iq80310_pci_intr_establish(void *, pci_intr_handle_t,
int, int (*func)(void *), void *, const char *);
void iq80310_pci_intr_disestablish(void *, void *);
void
iq80310_pci_init(pci_chipset_tag_t pc, void *cookie)
{
pc->pc_intr_v = cookie;
pc->pc_intr_map = iq80310_pci_intr_map;
pc->pc_intr_string = iq80310_pci_intr_string;
pc->pc_intr_evcnt = iq80310_pci_intr_evcnt;
pc->pc_intr_establish = iq80310_pci_intr_establish;
pc->pc_intr_disestablish = iq80310_pci_intr_disestablish;
}
#if defined(IOP310_TEAMASA_NPWR)
int
iq80310_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
struct i80312_softc *sc = pa->pa_pc->pc_intr_v;
pcireg_t reg;
int sbus;
reg = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PCI_BRIDGE_BUS_REG);
sbus = PCI_BRIDGE_BUS_NUM_SECONDARY(reg);
if (pa->pa_bus != sbus) {
printf("iq80310_pci_intr_map: %d/%d/%d not on Secondary bus\n",
pa->pa_bus, pa->pa_device, pa->pa_function);
return (1);
}
switch (pa->pa_device) {
case 5:
*ihp = XINT3_IRQ(2);
break;
case 6:
*ihp = XINT3_IRQ(1);
break;
case 7:
*ihp = XINT3_IRQ(4);
break;
default:
printf("iq80310_pci_intr_map: no mapping for %d/%d/%d\n",
pa->pa_bus, pa->pa_device, pa->pa_function);
return (1);
}
return (0);
}
#else
int
iq80310_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
struct i80312_softc *sc = pa->pa_pc->pc_intr_v;
pcitag_t tag;
pcireg_t reg;
int sbus, pbus;
reg = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PCI_BRIDGE_BUS_REG);
pbus = PCI_BRIDGE_BUS_NUM_PRIMARY(reg);
sbus = PCI_BRIDGE_BUS_NUM_SECONDARY(reg);
if (pa->pa_bus == pbus) {
printf("iq80310_pci_intr_map: can't map interrupts on "
"Primary bus\n");
return (1);
}
tag = pci_make_tag(pa->pa_pc, sbus, 7, 0);
reg = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
PCI_VENDOR(reg) == 0) {
printf("iq80310_pci_intr_map: PPB not found at %d/%d/%d ??\n",
sbus, 7, 0);
goto pinmap;
}
reg = pci_conf_read(pa->pa_pc, tag, PCI_CLASS_REG);
if (PCI_CLASS(reg) != PCI_CLASS_BRIDGE ||
PCI_SUBCLASS(reg) != PCI_SUBCLASS_BRIDGE_PCI) {
printf("iq80310_pci_intr_map: %d/%d/%d isn't a PPB ??\n",
sbus, 7, 0);
goto pinmap;
}
reg = pci_conf_read(pa->pa_pc, tag, PCI_BRIDGE_BUS_REG);
sbus = PCI_BRIDGE_BUS_NUM_SECONDARY(reg);
if (pa->pa_bus == sbus && pa->pa_device == 0 &&
pa->pa_function == 0) {
*ihp = XINT3_IRQ(XINT3_ETHERNET);
return (0);
}
pinmap:
if (pa->pa_intrpin == 0) {
return (1);
}
if (pa->pa_intrpin > 4) {
printf("iq80310_pci_intr_map: bad interrupt pin %d\n",
pa->pa_intrpin);
return (1);
}
if (pa->pa_intrpin == 4) {
*ihp = XINT3_IRQ(XINT3_SINTD);
return (0);
}
if (0)
*ihp = XINT3_IRQ(XINT3_SINTD);
else
*ihp = XINT0_IRQ(pa->pa_intrpin - 1);
return (0);
}
#endif
const char *
iq80310_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
{
snprintf(buf, len, "iq80310 irq %" PRIu64, ih);
return buf;
}
const struct evcnt *
iq80310_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
{
return (NULL);
}
void *
iq80310_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
int (*func)(void *), void *arg, const char *xname)
{
return (iq80310_intr_establish(ih, ipl, func, arg));
}
void
iq80310_pci_intr_disestablish(void *v, void *cookie)
{
iq80310_intr_disestablish(cookie);
}