#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.23 2025/10/04 15:34:52 thorpej Exp $");
#if defined(DEBUG) && !defined(SPARC_PCI_DEBUG)
#define SPARC_PCI_DEBUG
#endif
#ifdef SPARC_PCI_DEBUG
#define SPDB_CONF 0x01
#define SPDB_INTR 0x04
#define SPDB_INTMAP 0x08
#define SPDB_INTFIX 0x10
#define SPDB_PROBE 0x20
int sparc_pci_debug = 0;
#define DPRINTF(l, s) do { \
if (sparc_pci_debug & (l)) \
printf s; \
} while ( 0)
#else
#define DPRINTF(l, s)
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <machine/autoconf.h>
#include <machine/ctlreg.h>
#include <sparc/sparc/asm.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pci_calls.h>
#include <dev/ofw/ofw_pci.h>
#include <sparc/sparc/msiiepreg.h>
#include <sparc/sparc/msiiepvar.h>
#define PCI_MODE1_ADDRESS_REG_PA 0x30080000
#define PCI_MODE1_DATA_REG_PA 0x300a0000
#define PCI_MODE1_DATA_REG_MASK 0x7
struct mspcic_pci_intr_wiring {
u_int mpiw_bus;
u_int mpiw_device;
u_int mpiw_function;
pci_intr_line_t mpiw_line[4];
};
static struct mspcic_pci_intr_wiring krups_pci_intr_wiring[] = {
{ 0, 0, 1, { 1, 0, 0, 0 } },
{ 0, 1, 0, { 2, 0, 0, 0 } },
};
static struct mspcic_pci_intr_wiring espresso_pci_intr_wiring[] = {
{ 0, 0, 1, { 1, 0, 0, 0 } },
{ 0, 1, 0, { 2, 0, 0, 0 } },
{ 0, 2, 0, { 6, 7, 8, 8 } },
{ 0, 2, 1, { 6, 7, 8, 8 } },
{ 0, 2, 2, { 6, 7, 8, 8 } },
{ 0, 2, 3, { 6, 7, 8, 8 } },
{ 0, 2, 4, { 6, 7, 8, 8 } },
{ 0, 2, 5, { 6, 7, 8, 8 } },
{ 0, 2, 6, { 6, 7, 8, 8 } },
{ 0, 2, 7, { 6, 7, 8, 8 } },
{ 0, 3, 0, { 7, 8, 8, 8 } },
{ 0, 3, 1, { 7, 8, 8, 8 } },
{ 0, 3, 2, { 7, 8, 8, 8 } },
{ 0, 3, 3, { 7, 8, 8, 8 } },
{ 0, 3, 4, { 7, 8, 8, 8 } },
{ 0, 3, 5, { 7, 8, 8, 8 } },
{ 0, 3, 6, { 7, 8, 8, 8 } },
{ 0, 3, 7, { 7, 8, 8, 8 } },
{ 0, 7, 0, { 4, 0, 0, 0 } },
{ 0, 16, 0, { 5, 0, 0, 0 } },
{ 0, 20, 0, { 5, 0, 0, 0 } },
};
struct mspcic_known_model {
const char *model;
struct mspcic_pci_intr_wiring *map;
int mapsize;
};
#define MSPCIC_MODEL_WIRING(name,map) \
{ name, map, sizeof(map)/sizeof(map[0]) }
static struct mspcic_known_model mspcic_known_models[] = {
MSPCIC_MODEL_WIRING("SUNW,501-4267", krups_pci_intr_wiring),
MSPCIC_MODEL_WIRING("SUNW,375-0059", espresso_pci_intr_wiring),
{ NULL, NULL, 0}
};
static struct mspcic_pci_intr_wiring *wiring_map;
static int wiring_map_size;
static int
sparc_pci_bus_get_child_devhandle(device_t dev, devhandle_t call_handle,
void *v)
{
struct pci_bus_get_child_devhandle_args *args = v;
int node = PCITAG_NODE(args->tag);
if (node != -1) {
args->devhandle = prom_node_to_devhandle(call_handle, node);
return 0;
}
return ENODEV;
}
OBP_DEVICE_CALL_REGISTER(PCI_BUS_GET_CHILD_DEVHANDLE_STR,
sparc_pci_bus_get_child_devhandle)
void
pci_attach_hook(device_t parent, device_t self,
struct pcibus_attach_args *pba)
{
struct mspcic_known_model *p;
char buf[32];
char *model;
if (pba->pba_bus != 0)
return;
model = prom_getpropstringA(prom_findroot(), "model",
buf, sizeof(buf));
if (model == NULL)
panic("pci_attach_hook: no \"model\" property");
printf(": model %s", model);
for (p = mspcic_known_models; p->model != NULL; ++p)
if (strcmp(model, p->model) == 0) {
printf(": interrupt wiring known");
wiring_map = p->map;
wiring_map_size = p->mapsize;
return;
}
printf(": don't know how interrupts are wired\n");
panic("pci_attach_hook: unknown model %s", model);
}
int
pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
{
return 32;
}
pcitag_t
pci_make_tag(pci_chipset_tag_t pc, int b, int d, int f)
{
struct mspcic_softc *sc = (struct mspcic_softc *)pc->cookie;
pcitag_t tag;
int node, len;
#ifdef SPARC_PCI_DEBUG
char name[80];
memset(name, 0, sizeof(name));
#endif
tag = PCITAG_CREATE(-1, b, d, f);
if (b >= 256 || d >= 32 || f >= 8) {
printf("pci_make_tag: bad request %d/%d/%d\n", b, d, f);
return tag;
}
for (node = OF_child(sc->sc_node); node != 0; node = OF_peer(node)) {
struct ofw_pci_register reg;
uint32_t busrange[2];
int class;
pcireg_t busdata;
pcitag_t bustag;
#ifdef SPARC_PCI_DEBUG
if (sparc_pci_debug & SPDB_PROBE) {
OF_getprop(node, "name", &name, sizeof(name));
printf("> checking node %x %s\n", node, name);
}
#endif
while ((OF_getprop(node, "bus-range", (void *)&busrange,
sizeof(busrange)) == sizeof(busrange))
&& (b >= busrange[0] && b <= busrange[1]))
{
node = OF_child(node);
#ifdef SPARC_PCI_DEBUG
if (sparc_pci_debug & SPDB_PROBE) {
OF_getprop(node, "name", &name, sizeof(name));
printf("> going down to node %x %s\n",
node, name);
}
#endif
}
len = OF_getproplen(node, "reg");
OF_getprop(node, "reg", (void *)®, sizeof(reg));
if (b > 0) {
len = OF_getproplen(node, "class-code");
if (!len)
continue;
OF_getprop(node, "class-code", &class, len);
if (IS_PCI_BRIDGE(class)) {
bustag = PCITAG_CREATE(node,
OFW_PCI_PHYS_HI_BUS(reg.phys_hi),
OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi),
OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi));
busdata = pci_conf_read(NULL, bustag,
PCI_BRIDGE_BUS_REG);
if (b != ((busdata >> 8) & 0xff))
continue;
#ifdef SPARC_PCI_DEBUG
if (sparc_pci_debug & SPDB_PROBE) {
OF_getprop(node, "name", &name,
sizeof(name));
printf("> matched device behind node "
"%x %s (bus %d)\n", node, name, b);
}
#endif
} else
continue;
} else {
if (b != OFW_PCI_PHYS_HI_BUS(reg.phys_hi))
continue;
if (d != OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi))
continue;
if (f != OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi))
continue;
}
tag = PCITAG_CREATE(node, b, d, f);
DPRINTF(SPDB_PROBE, ("> found node %x %s\n", node, name));
return tag;
}
return tag;
}
void
pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
int *bp, int *dp, int *fp)
{
if (bp != NULL)
*bp = PCITAG_BUS(tag);
if (dp != NULL)
*dp = PCITAG_DEV(tag);
if (fp != NULL)
*fp = PCITAG_FUN(tag);
}
pcireg_t
pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
uint32_t mode1_addr;
uint32_t mode1_data_reg_pa;
uint32_t val;
DPRINTF(SPDB_CONF,
("pci_conf_read: tag=%x.%x (%d/%d/%d), reg=%02x; ",
PCITAG_NODE(tag), PCITAG_OFFSET(tag),
PCITAG_BUS(tag), PCITAG_DEV(tag), PCITAG_FUN(tag),
reg));
#ifdef DIAGNOSTIC
if (reg & 0x3)
panic("pci_conf_read: reg %x unaligned", reg);
#endif
if (PCITAG_NODE(tag) == -1) {
DPRINTF(SPDB_CONF, ("\n"));
return ~0;
}
if ((unsigned int)reg >= PCI_CONF_SIZE)
return ~0;
mode1_addr = PCITAG_OFFSET(tag) | reg;
mode1_data_reg_pa = PCI_MODE1_DATA_REG_PA
| (reg & PCI_MODE1_DATA_REG_MASK);
sta(PCI_MODE1_ADDRESS_REG_PA, ASI_BYPASS, htole32(mode1_addr));
val = le32toh(lda(mode1_data_reg_pa, ASI_BYPASS));
DPRINTF(SPDB_CONF, ("reading %08x\n", val));
return val;
}
void
pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
{
uint32_t mode1_addr;
uint32_t mode1_data_reg_pa;
DPRINTF(SPDB_CONF,
("pci_conf_write: tag=%x.%x (%d/%d/%d); reg=%02x; ",
PCITAG_NODE(tag), PCITAG_OFFSET(tag),
PCITAG_BUS(tag), PCITAG_DEV(tag), PCITAG_FUN(tag),
reg));
#ifdef DIAGNOSTIC
if (reg & 0x3)
panic("pci_conf_write: reg %x unaligned", reg);
#endif
if (PCITAG_NODE(tag) == -1) {
DPRINTF(SPDB_CONF, ("\n"));
return;
}
if ((unsigned int)reg >= PCI_CONF_SIZE)
return;
mode1_addr = PCITAG_OFFSET(tag) | reg;
mode1_data_reg_pa = PCI_MODE1_DATA_REG_PA
| (reg & PCI_MODE1_DATA_REG_MASK);
DPRINTF(SPDB_CONF, ("writing %08x\n", data));
sta(PCI_MODE1_ADDRESS_REG_PA, ASI_BYPASS, htole32(mode1_addr));
sta(mode1_data_reg_pa, ASI_BYPASS, htole32(data));
}
int
pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
int i, node;
pcitag_t tag;
pcireg_t val;
pci_intr_pin_t pin;
DPRINTF(SPDB_INTMAP,
("pci_intr_map(%d/%d/%d) -> ",
pa->pa_bus, pa->pa_device, pa->pa_function));
tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device,
pa->pa_function);
node = PCITAG_NODE(tag);
val = pci_conf_read(NULL, tag, PCI_INTERRUPT_REG);
pin = PCI_INTERRUPT_PIN(val);
if (pin)
pin -= 1;
for (i = 0; i < wiring_map_size; ++i) {
struct mspcic_pci_intr_wiring *w = &wiring_map[i];
if (pa->pa_bus == w->mpiw_bus
&& pa->pa_device == w->mpiw_device
&& pa->pa_function == w->mpiw_function)
{
if (w->mpiw_line[pin] > 7) {
DPRINTF(SPDB_INTMAP, ("not mapped\n"));
return -1;
}
DPRINTF(SPDB_INTMAP, ("pin %c line %d\n", 'A' + pin,
w->mpiw_line[pin]));
*ihp = w->mpiw_line[pin];
return 0;
} else if (pa->pa_bus) {
struct ofw_pci_register reg;
OF_getprop(node, "reg", (void *)®, sizeof(reg));
if (OFW_PCI_PHYS_HI_BUS(reg.phys_hi) == w->mpiw_bus
&& OFW_PCI_PHYS_HI_DEVICE(reg.phys_hi)
== w->mpiw_device
&& OFW_PCI_PHYS_HI_FUNCTION(reg.phys_hi)
== w->mpiw_function) {
int j;
for (j = 0; j < PCI_INTERRUPT_LINE(val); j++)
pin = (pin + (pa->pa_device % 4)) % 4;
if (w->mpiw_line[pin] > 7) {
DPRINTF(SPDB_INTMAP, ("pin %c "
"not mapped\n", pin));
return -1;
}
DPRINTF(SPDB_INTMAP, ("pin %c line %d "
"via bridge (%d/%d/%d) depth %d\n",
'A' + pin, w->mpiw_line[pin],
w->mpiw_bus, w->mpiw_device,
w->mpiw_function,
PCI_INTERRUPT_LINE(val)));
*ihp = w->mpiw_line[pin];
return 0;
}
}
}
DPRINTF(SPDB_INTMAP, ("not found\n"));
return -1;
}
const char *
pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
size_t len)
{
int pil;
pil = mspcic_assigned_interrupt(ih);
snprintf(buf, len, "line %d (pil %d)", ih, pil);
return buf;
}
const struct evcnt *
pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
{
return NULL;
}
int
pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
int attr, uint64_t data)
{
switch (attr) {
case PCI_INTR_MPSAFE:
return 0;
default:
return ENODEV;
}
}
void *
pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
int level, int (*func)(void *), void *arg)
{
struct mspcic_softc *sc = (struct mspcic_softc *)pc->cookie;
void *cookie;
DPRINTF(SPDB_INTR,
("pci_intr_establish(line %d, ipl %d)\n", ih, level));
cookie = bus_intr_establish(sc->sc_memt, ih, level, func, arg);
DPRINTF(SPDB_INTR,
("pci_intr_establish: returning handle %p\n", cookie));
return cookie;
}
void
pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
{
DPRINTF(SPDB_INTR, ("pci_intr_disestablish: cookie %p\n", cookie));
panic("pci_intr_disestablish: not implemented");
}