1
// PCI config space access functions.
3
// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4
// Copyright (C) 2002 MandrakeSoft S.A.
6
// This file may be distributed under the terms of the GNU LGPLv3 license.
8
#include "output.h" // dprintf
9
#include "pci.h" // pci_config_writel
10
#include "pci_regs.h" // PCI_VENDOR_ID
11
#include "util.h" // udelay
12
#include "x86.h" // outl
14
#define PORT_PCI_CMD 0x0cf8
15
#define PORT_PCI_REBOOT 0x0cf9
16
#define PORT_PCI_DATA 0x0cfc
18
void pci_config_writel(u16 bdf, u32 addr, u32 val)
20
outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
21
outl(val, PORT_PCI_DATA);
24
void pci_config_writew(u16 bdf, u32 addr, u16 val)
26
outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
27
outw(val, PORT_PCI_DATA + (addr & 2));
30
void pci_config_writeb(u16 bdf, u32 addr, u8 val)
32
outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
33
outb(val, PORT_PCI_DATA + (addr & 3));
36
u32 pci_config_readl(u16 bdf, u32 addr)
38
outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
39
return inl(PORT_PCI_DATA);
42
u16 pci_config_readw(u16 bdf, u32 addr)
44
outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
45
return inw(PORT_PCI_DATA + (addr & 2));
48
u8 pci_config_readb(u16 bdf, u32 addr)
50
outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
51
return inb(PORT_PCI_DATA + (addr & 3));
55
pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on)
57
u16 val = pci_config_readw(bdf, addr);
58
val = (val & ~off) | on;
59
pci_config_writew(bdf, addr, val);
62
// Helper function for foreachbdf() macro - return next device
64
pci_next(int bdf, int bus)
66
if (pci_bdf_to_fn(bdf) == 0
67
&& (pci_config_readb(bdf, PCI_HEADER_TYPE) & 0x80) == 0)
68
// Last found device wasn't a multi-function device - skip to
75
if (pci_bdf_to_bus(bdf) != bus)
78
u16 v = pci_config_readw(bdf, PCI_VENDOR_ID);
79
if (v != 0x0000 && v != 0xffff)
83
if (pci_bdf_to_fn(bdf) == 0)
90
// Check if PCI is available at all
94
outl(0x80000000, PORT_PCI_CMD);
95
if (inl(PORT_PCI_CMD) != 0x80000000) {
96
dprintf(1, "Detected non-PCI system\n");
105
u8 v = inb(PORT_PCI_REBOOT) & ~6;
106
outb(v|2, PORT_PCI_REBOOT); /* Request hard reset */
108
outb(v|6, PORT_PCI_REBOOT); /* Actually do the reset */