#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.39 2026/01/09 22:54:34 jmcneill Exp $");
#include "opt_pci.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/extent.h>
#include <sys/kmem.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <sys/time.h>
#define _POWERPC_BUS_DMA_PRIVATE
#include <sys/bus.h>
#include <machine/intr.h>
#include <machine/pio.h>
#include <dev/isa/isavar.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pciconf.h>
#include <dev/pci/pcidevs.h>
struct powerpc_bus_dma_tag pci_bus_dma_tag = {
0, 0,
_bus_dmamap_create,
_bus_dmamap_destroy,
_bus_dmamap_load,
_bus_dmamap_load_mbuf,
_bus_dmamap_load_uio,
_bus_dmamap_load_raw,
_bus_dmamap_unload,
NULL,
_bus_dmamem_alloc,
_bus_dmamem_free,
_bus_dmamem_map,
_bus_dmamem_unmap,
_bus_dmamem_mmap,
};
static int brdtype;
#define BRD_SANDPOINTX2 2
#define BRD_SANDPOINTX3 3
#define BRD_ENCOREPP1 10
#define BRD_KUROBOX 100
#define BRD_QNAPTS 101
#define BRD_SYNOLOGY 102
#define BRD_STORCENTER 103
#define BRD_DLINKDSM 104
#define BRD_NH230NAS 105
#define BRD_UNKNOWN -1
#define PCI_CONFIG_ENABLE 0x80000000UL
void
pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
{
pcitag_t tag;
pcireg_t dev11, dev22, dev15, dev13, dev16;
tag = pci_make_tag(pba->pba_pc, pba->pba_bus, 11, 0);
dev11 = pci_conf_read(pba->pba_pc, tag, PCI_CLASS_REG);
if (PCI_CLASS(dev11) == PCI_CLASS_BRIDGE) {
brdtype = BRD_SANDPOINTX3;
return;
}
tag = pci_make_tag(pba->pba_pc, pba->pba_bus, 22, 0);
dev22 = pci_conf_read(pba->pba_pc, tag, PCI_CLASS_REG);
if (PCI_CLASS(dev22) == PCI_CLASS_BRIDGE) {
brdtype = BRD_ENCOREPP1;
return;
}
tag = pci_make_tag(pba->pba_pc, pba->pba_bus, 11, 0);
dev11 = pci_conf_read(pba->pba_pc, tag, PCI_CLASS_REG);
if (PCI_CLASS(dev11) == PCI_CLASS_NETWORK) {
brdtype = BRD_KUROBOX;
return;
}
tag = pci_make_tag(pba->pba_pc, pba->pba_bus, 15, 0);
dev15 = pci_conf_read(pba->pba_pc, tag, PCI_ID_REG);
if (PCI_VENDOR(dev15) == PCI_VENDOR_MARVELL) {
brdtype = BRD_SYNOLOGY;
return;
}
tag = pci_make_tag(pba->pba_pc, pba->pba_bus, 13, 0);
dev13 = pci_conf_read(pba->pba_pc, tag, PCI_ID_REG);
if (PCI_VENDOR(dev13) == PCI_VENDOR_VIATECH) {
brdtype = BRD_STORCENTER;
return;
}
tag = pci_make_tag(pba->pba_pc, pba->pba_bus, 16, 0);
dev16 = pci_conf_read(pba->pba_pc, tag, PCI_ID_REG);
if (PCI_VENDOR(dev16) == PCI_VENDOR_ACARD) {
brdtype = BRD_DLINKDSM;
return;
}
if (PCI_VENDOR(dev16) == PCI_VENDOR_ITE
|| PCI_VENDOR(dev16) == PCI_VENDOR_CMDTECH) {
brdtype = BRD_NH230NAS;
return;
}
if (PCI_VENDOR(dev15) == PCI_VENDOR_INTEL
|| PCI_VENDOR(dev15) == PCI_VENDOR_REALTEK) {
brdtype = BRD_QNAPTS;
return;
}
brdtype = BRD_UNKNOWN;
}
int
pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
{
return 32;
}
pcitag_t
pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
{
pcitag_t tag;
if (bus >= 256 || device >= 32 || function >= 8)
panic("pci_make_tag: bad request");
tag = PCI_CONFIG_ENABLE |
(bus << 16) | (device << 11) | (function << 8);
return tag;
}
void
pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
int *bp, int *dp, int *fp)
{
if (bp != NULL)
*bp = (tag >> 16) & 0xff;
if (dp != NULL)
*dp = (tag >> 11) & 0x1f;
if (fp != NULL)
*fp = (tag >> 8) & 0x7;
return;
}
pcireg_t
pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
pcireg_t data;
if ((unsigned int)reg >= PCI_CONF_SIZE)
return (pcireg_t) -1;
out32rb(SANDPOINT_PCI_CONFIG_ADDR, tag | reg);
data = in32rb(SANDPOINT_PCI_CONFIG_DATA);
out32rb(SANDPOINT_PCI_CONFIG_ADDR, 0);
return data;
}
void
pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
{
if ((unsigned int)reg >= PCI_CONF_SIZE)
return;
out32rb(SANDPOINT_PCI_CONFIG_ADDR, tag | reg);
out32rb(SANDPOINT_PCI_CONFIG_DATA, data);
out32rb(SANDPOINT_PCI_CONFIG_ADDR, 0);
}
int
pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
int pin = pa->pa_intrpin;
int line = pa->pa_intrline;
if (pin == 0)
goto bad;
if (pin > 4) {
aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
goto bad;
}
if (line == 255) {
aprint_error("pci_intr_map: no mapping for pin %c\n",
'@' + pin);
goto bad;
}
#ifdef EPIC_DEBUGIRQ
printf("line %d, pin %c", line, pin + '@');
#endif
switch (brdtype) {
case BRD_SANDPOINTX3:
if (line == 11
&& pa->pa_function == 1 && pa->pa_bus == 0) {
*ihp = 8;
break;
}
if (line < 13 || line > 16) {
aprint_error("pci_intr_map: bad interrupt line %d,%c\n",
line, pin + '@');
goto bad;
}
line -= 13;
*ihp = 2 + line;
break;
case BRD_SANDPOINTX2:
if (line == 11
&& pa->pa_function == 1 && pa->pa_bus == 0) {
*ihp = 2;
break;
}
if (line < 13 || line > 16) {
aprint_error("pci_intr_map: bad interrupt line %d,%c\n",
line, pin + '@');
goto bad;
}
line -= 13; pin -= 1;
*ihp = (line + pin) & 03;
break;
case BRD_ENCOREPP1:
if (pa->pa_device != 25)
goto bad;
*ihp = 2;
break;
case BRD_KUROBOX:
*ihp = (line == 13) ? 4 : line - 11;
break;
case BRD_QNAPTS:
*ihp = line - 13;
break;
case BRD_SYNOLOGY:
*ihp = (line == 12) ? 4 : line - 13;
break;
case BRD_DLINKDSM:
*ihp = (line < 15) ? line - 13 : line - 12;
if (line == 14 && pin == 3)
*ihp += 1;
break;
case BRD_NH230NAS:
*ihp = (line == 16) ? 2 :
(line == 15) ? 1 :
(line == 14) ? 3 : 0;
break;
case BRD_STORCENTER:
*ihp = (line == 15) ? 0 :
(line == 13) ? 1 : 1 + pin;
break;
default:
*ihp = line - 12;
#if defined(DIAGNOSTIC) || defined(DEBUG)
printf("pci_intr_map: line %d, pin %c for unknown board"
" mapped to irq %d\n", line, pin + '@', *ihp);
#endif
break;
}
#ifdef EPIC_DEBUGIRQ
printf(" = EPIC %d\n", *ihp);
#endif
return 0;
bad:
*ihp = -1;
return 1;
}
const char *
pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
size_t len)
{
if (ih < 0 || ih >= OPENPIC_ICU)
panic("pci_intr_string: bogus handle 0x%x", ih);
snprintf(buf, len, "irq %d", ih + I8259_ICU);
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)
{
return pci_intr_establish_xname(pc, ih, level, func, arg, NULL);
}
void *
pci_intr_establish_xname(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
int (*func)(void *), void *arg, const char *xname)
{
int type;
if (brdtype == BRD_STORCENTER && ih == 1) {
type = IST_EDGE;
} else
type = IST_LEVEL;
return intr_establish_xname(ih + I8259_ICU, type, level, func, arg,
xname);
}
void
pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
{
intr_disestablish(cookie);
}
#if defined(PCI_NETBSD_CONFIGURE)
void
pci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev,
int pin, int swiz, int *iline)
{
if (bus == 0) {
*iline = dev;
} else {
*iline = 13 + ((swiz + dev + 3) & 3);
}
}
#endif
pci_intr_type_t
pci_intr_type(pci_chipset_tag_t pc, pci_intr_handle_t ih)
{
return PCI_INTR_TYPE_INTX;
}
int
pci_intr_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int *counts, pci_intr_type_t max_type)
{
if (counts != NULL && counts[PCI_INTR_TYPE_INTX] == 0)
return EINVAL;
return pci_intx_alloc(pa, ihps);
}
void
pci_intr_release(pci_chipset_tag_t pc, pci_intr_handle_t *pih, int count)
{
kmem_free(pih, sizeof(*pih));
}
int
pci_intx_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihpp)
{
pci_intr_handle_t *ihp;
ihp = kmem_alloc(sizeof(*ihp), KM_SLEEP);
if (pci_intr_map(pa, ihp)) {
kmem_free(ihp, sizeof(*ihp));
return EINVAL;
}
*ihpp = ihp;
return 0;
}
int
pci_msi_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int *count)
{
return EOPNOTSUPP;
}
int
pci_msi_alloc_exact(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int count)
{
return EOPNOTSUPP;
}
int
pci_msix_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int *count)
{
return EOPNOTSUPP;
}
int
pci_msix_alloc_exact(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int count)
{
return EOPNOTSUPP;
}
int
pci_msix_alloc_map(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
u_int *table_indexes, int count)
{
return EOPNOTSUPP;
}