#include <lib/libsa/stand.h>
#include <sys/bswap.h>
#include <dev/pci/pcireg.h>
#include "boot.h"
#define NSLOTS 5
#define NPCIREGS 10
#define PCI_CONFIG_SPACE_BASE 0x80800000
#define PCI_CONFIG_SPACE(d, f) \
(u_long *)(PCI_CONFIG_SPACE_BASE | (1 << (d)) | ((f) << 8))
struct PCI_ConfigInfo {
u_long *config_addr;
u_long regs[NPCIREGS];
} PCI_slots [NSLOTS] = {
{ PCI_CONFIG_SPACE(11, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
{ PCI_CONFIG_SPACE(12, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
{ PCI_CONFIG_SPACE(13, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
{ PCI_CONFIG_SPACE(14, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
{ PCI_CONFIG_SPACE(15, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
};
#define DEVID (PCI_ID_REG >> 2)
#define CMD (PCI_COMMAND_STATUS_REG >> 2)
#define CLASS (PCI_CLASS_REG >> 2)
#define BAR_BASE (PCI_MAPREG_START >> 2)
void
enablePCI(int slot, int io, int mem, int master)
{
volatile u_char *ppci;
u_char enable = 0;
if (io)
enable |= PCI_COMMAND_IO_ENABLE;
if (mem)
enable |= PCI_COMMAND_MEM_ENABLE;
if (master)
enable |= PCI_COMMAND_MASTER_ENABLE;
ppci = (u_char *)&PCI_slots[slot].config_addr[CMD];
*ppci = enable;
__asm volatile("eieio" ::: "memory");
}
void
scanPCI(void)
{
struct PCI_ConfigInfo *pslot;
int slt, r;
for (slt = 0; slt < NSLOTS; slt++) {
pslot = &PCI_slots[slt];
for (r = 0; r < NPCIREGS; r++)
pslot->regs[r] = bswap32(pslot->config_addr[r]);
}
}
int
findPCIVga(void)
{
struct PCI_ConfigInfo *pslot;
int theSlot = -1;
int highVgaSlot = -1;
int slt;
for (slt = 0; slt < NSLOTS; slt++) {
pslot = &PCI_slots[slt];
if (pslot->regs[DEVID] != 0xffffffff) {
if (PCI_CLASS(pslot->regs[CLASS]) ==
PCI_CLASS_DISPLAY) {
highVgaSlot = slt;
if ((pslot->regs[CMD] & 0x03)) {
theSlot = slt;
}
}
}
}
if (theSlot == -1)
theSlot = highVgaSlot;
return theSlot;
}
int
PCISlotnum(u_int bus, u_int dev, u_int func)
{
u_long *tag;
int i;
if (bus != 0 ||
dev < 11 || dev > 15 ||
func > 7)
return -1;
tag = PCI_CONFIG_SPACE(dev, func);
for (i = 0; i < sizeof(PCI_slots) / sizeof(struct PCI_ConfigInfo); i++)
if (tag == PCI_slots[i].config_addr)
return i;
return -1;
}
int
PCIVendor(int slotnum)
{
struct PCI_ConfigInfo *pslot;
pslot = &PCI_slots[slotnum];
return pslot->regs[DEVID] & 0xffff;
}
u_long
PCIAddress(int slotnum, u_int bar, int type)
{
struct PCI_ConfigInfo *pslot;
if (bar >= 6)
return 0xffffffff;
pslot = &PCI_slots[slotnum];
if (pslot->regs[DEVID] == 0xffffffff ||
PCI_MAPREG_TYPE(pslot->regs[BAR_BASE + bar]) != type)
return 0xffffffff;
return PCI_MAPREG_MEM_ADDR(pslot->regs[BAR_BASE + bar]);
}
#ifdef DEBUG
void
printPCIslots(void)
{
int i;
for (i = 0; i < NSLOTS; i++) {
printf("PCI Slot number: %d", i);
printf(" Vendor ID: 0x%x\n", PCIVendor(i));
}
}
#endif