#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: footbridge_pci.c,v 1.34 2022/09/27 06:36:41 skrll Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/device.h>
#define _ARM32_BUS_DMA_PRIVATE
#include <sys/bus.h>
#include <machine/intr.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <arm/footbridge/dc21285reg.h>
#include <arm/footbridge/dc21285mem.h>
#include "isa.h"
#if NISA > 0
#include <dev/isa/isavar.h>
#endif
void footbridge_pci_attach_hook(device_t, device_t,
struct pcibus_attach_args *);
int footbridge_pci_bus_maxdevs(void *, int);
pcitag_t footbridge_pci_make_tag(void *, int, int, int);
void footbridge_pci_decompose_tag(void *, pcitag_t, int *,
int *, int *);
pcireg_t footbridge_pci_conf_read(void *, pcitag_t, int);
void footbridge_pci_conf_write(void *, pcitag_t, int,
pcireg_t);
int footbridge_pci_intr_map(const struct pci_attach_args *,
pci_intr_handle_t *);
const char *footbridge_pci_intr_string(void *, pci_intr_handle_t,
char *, size_t);
void *footbridge_pci_intr_establish(void *, pci_intr_handle_t,
int, int (*)(void *), void *, const char *);
void footbridge_pci_intr_disestablish(void *, void *);
const struct evcnt *footbridge_pci_intr_evcnt(void *, pci_intr_handle_t);
struct arm32_pci_chipset footbridge_pci_chipset = {
#ifdef netwinder
.pc_attach_hook = netwinder_pci_attach_hook,
#else
.pc_attach_hook = footbridge_pci_attach_hook,
#endif
.pc_bus_maxdevs = footbridge_pci_bus_maxdevs,
.pc_make_tag = footbridge_pci_make_tag,
.pc_decompose_tag = footbridge_pci_decompose_tag,
.pc_conf_read = footbridge_pci_conf_read,
.pc_conf_write = footbridge_pci_conf_write,
.pc_intr_map = footbridge_pci_intr_map,
.pc_intr_string = footbridge_pci_intr_string,
.pc_intr_evcnt = footbridge_pci_intr_evcnt,
.pc_intr_establish = footbridge_pci_intr_establish,
.pc_intr_disestablish = footbridge_pci_intr_disestablish
};
struct arm32_dma_range footbridge_dma_ranges[1];
struct arm32_bus_dma_tag footbridge_pci_bus_dma_tag = {
._ranges = footbridge_dma_ranges,
._nranges = 1,
_BUS_DMAMAP_FUNCS,
_BUS_DMAMEM_FUNCS,
_BUS_DMATAG_FUNCS,
};
#define MAX_PCI_DEVICES 21
void
footbridge_pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
{
#ifdef PCI_DEBUG
printf("footbridge_pci_attach_hook()\n");
#endif
}
int
footbridge_pci_bus_maxdevs(void *pcv, int busno)
{
#ifdef PCI_DEBUG
printf("footbridge_pci_bus_maxdevs(pcv=%p, busno=%d)\n", pcv, busno);
#endif
return(MAX_PCI_DEVICES);
}
pcitag_t
footbridge_pci_make_tag(void *pcv, int bus, int device, int function)
{
#ifdef PCI_DEBUG
printf("footbridge_pci_make_tag(pcv=%p, bus=%d, device=%d, function=%d)\n",
pcv, bus, device, function);
#endif
return ((bus << 16) | (device << 11) | (function << 8));
}
void
footbridge_pci_decompose_tag(void *pcv, pcitag_t tag, int *busp, int *devicep, int *functionp)
{
#ifdef PCI_DEBUG
printf("footbridge_pci_decompose_tag(pcv=%p, tag=0x%08x, bp=%p, dp=%p, fp=%p)\n",
pcv, (uint32_t)tag, busp, devicep, functionp);
#endif
if (busp != NULL)
*busp = (tag >> 16) & 0xff;
if (devicep != NULL)
*devicep = (tag >> 11) & 0x1f;
if (functionp != NULL)
*functionp = (tag >> 8) & 0x7;
}
pcireg_t
footbridge_pci_conf_read(void *pcv, pcitag_t tag, int reg)
{
int bus, device, function;
u_int address;
pcireg_t data;
if ((unsigned int)reg >= PCI_CONF_SIZE)
return ((pcireg_t) -1);
footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
if (bus == 0)
address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
else
address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
(bus << 16);
address |= (function << 8) | reg;
data = *((unsigned int *)address);
#ifdef PCI_DEBUG
printf("footbridge_pci_conf_read(pcv=%p tag=0x%08x reg=0x%02x)=0x%08x\n",
pcv, (uint32_t)tag, reg, data);
#endif
return(data);
}
void
footbridge_pci_conf_write(void *pcv, pcitag_t tag, int reg, pcireg_t data)
{
int bus, device, function;
u_int address;
if ((unsigned int)reg >= PCI_CONF_SIZE)
return;
footbridge_pci_decompose_tag(pcv, tag, &bus, &device, &function);
if (bus == 0)
address = DC21285_PCI_TYPE_0_CONFIG_VBASE | (3 << 22) | (device << 11);
else
address = DC21285_PCI_TYPE_1_CONFIG_VBASE | (device << 11) |
(bus << 16);
address |= (function << 8) | reg;
#ifdef PCI_DEBUG
printf("footbridge_pci_conf_write(pcv=%p tag=0x%08x reg=0x%02x, 0x%08x)\n",
pcv, (uint32_t)tag, reg, data);
#endif
*((unsigned int *)address) = data;
}
int
footbridge_pci_intr_map(const struct pci_attach_args *pa,
pci_intr_handle_t *ihp)
{
int pin = pa->pa_intrpin, line = pa->pa_intrline;
int intr = -1;
#ifdef PCI_DEBUG
void *pcv = pa->pa_pc;
pcitag_t intrtag = pa->pa_intrtag;
int bus, device, function;
footbridge_pci_decompose_tag(pcv, intrtag, &bus, &device, &function);
printf("footbridge_pci_intr_map: pcv=%p, tag=%08x pin=%d line=%d dev=%d\n",
pcv, (uint32_t)intrtag, pin, line, device);
#endif
switch (line) {
case PCI_INTERRUPT_PIN_NONE:
case 0xff:
printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
*ihp = -1;
return(1);
break;
#ifdef cats
case PCI_INTERRUPT_PIN_A:
intr = IRQ_PCI;
break;
case PCI_INTERRUPT_PIN_B:
intr = IRQ_IN_L0;
break;
case PCI_INTERRUPT_PIN_C:
intr = IRQ_IN_L1;
break;
case PCI_INTERRUPT_PIN_D:
intr = IRQ_IN_L3;
break;
#endif
default:
if (line >= 0x40 && line <= 0x5f)
intr = line & 0x1f;
else if (line >= 0x80 && line <= 0x8f)
intr = line;
else {
printf("footbridge_pci_intr_map: out of range interrupt"
"pin %d line %d (%#x)\n", pin, line, line);
*ihp = -1;
return(1);
}
break;
}
#ifdef PCI_DEBUG
printf("pin %d, line %d mapped to int %d\n", pin, line, intr);
#endif
*ihp = intr;
return(0);
}
const char *
footbridge_pci_intr_string(void *pcv, pci_intr_handle_t ih, char *buf, size_t len)
{
#ifdef PCI_DEBUG
printf("footbridge_pci_intr_string(pcv=%p, ih=0x%" PRIx64 ")\n", pcv, ih);
#endif
if (ih == 0)
panic("footbridge_pci_intr_string: bogus handle 0x%" PRIx64, ih);
#if NISA > 0
if (ih >= 0x80 && ih <= 0x8f) {
snprintf(buf, len, "isairq %" PRIu64, (ih & 0x0f));
return buf;
}
#endif
snprintf(buf, len, "irq %" PRIu64, ih);
return buf;
}
void *
footbridge_pci_intr_establish(
void *pcv,
pci_intr_handle_t ih,
int level,
int (*func)(void *),
void *arg, const char *xname)
{
void *intr;
char buf[PCI_INTRSTR_LEN];
const char *intrstr;
#ifdef PCI_DEBUG
printf("footbridge_pci_intr_establish(pcv=%p, ih=0x%" PRIx64 ", level=%d, func=%p, arg=%p, xname=%s)\n",
pcv, ih, level, func, arg, xname);
#endif
intrstr = footbridge_pci_intr_string(pcv, ih, buf, sizeof(buf));
#if NISA > 0
if (ih >= 0x80 && ih <= 0x8d) {
intr = isa_intr_establish(NULL, (ih & 0x0f), IST_EDGE,
level, func, arg);
} else
#endif
intr = footbridge_intr_claim(ih, level, intrstr, func, arg);
return(intr);
}
void
footbridge_pci_intr_disestablish(void *pcv, void *cookie)
{
#ifdef PCI_DEBUG
printf("footbridge_pci_intr_disestablish(pcv=%p, cookie=%p)\n",
pcv, cookie);
#endif
footbridge_intr_disestablish(cookie);
}