diff options
author | Freya Murphy <freya@freyacat.org> | 2024-02-04 14:19:54 -0500 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-02-04 14:19:54 -0500 |
commit | 1b09896afcf562d199d4df8d671601bba2b1f081 (patch) | |
tree | b4ee4ae8e6e1ff5c5b27fe97509752b17fae330b /src/arch/x86_common/drivers/pci.c | |
parent | fix acpi on uefi, kprint fixes (diff) | |
download | corn-1b09896afcf562d199d4df8d671601bba2b1f081.tar.gz corn-1b09896afcf562d199d4df8d671601bba2b1f081.tar.bz2 corn-1b09896afcf562d199d4df8d671601bba2b1f081.zip |
Diffstat (limited to 'src/arch/x86_common/drivers/pci.c')
-rw-r--r-- | src/arch/x86_common/drivers/pci.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/arch/x86_common/drivers/pci.c b/src/arch/x86_common/drivers/pci.c new file mode 100644 index 0000000..579562a --- /dev/null +++ b/src/arch/x86_common/drivers/pci.c @@ -0,0 +1,57 @@ +#include <pci.h> +#include <bindings.h> + +#define PCI_INTERNAL +#include <sys/pci.h> + +#define CONF_ADDR 0xCF8 +#define CONF_DATA 0xCFC + +uint32_t pci_sys_rcfg_d(struct pci_device dev, uint8_t offset) { + uint32_t addr = 0x80000000; + addr |= ((uint32_t)dev.bus) << 16; + addr |= ((uint32_t)dev.device) << 11; + addr |= ((uint32_t)dev.function) << 8; + addr |= offset & 0xFC; + + outl(CONF_ADDR, addr); + uint32_t in = inl(CONF_DATA); + return in; +} + +uint16_t pci_sys_rcfg_w(struct pci_device dev, uint8_t offset) { + uint32_t dword = pci_sys_rcfg_d(dev, offset); + return (uint16_t)((dword >> ((offset & 2) * 8)) & 0xFFFF); +} + +uint8_t pci_sys_rcfg_b(struct pci_device dev, uint8_t offset) { + uint32_t dword = pci_sys_rcfg_d(dev, offset); + return (uint8_t)((dword >> ((offset & 3) * 8)) & 0xFF); +} + +void pci_sys_wcfg_d(struct pci_device dev, uint8_t offset, uint32_t dword) { + uint32_t addr = 0x80000000; + addr |= ((uint32_t)dev.bus) << 16; + addr |= ((uint32_t)dev.device) << 11; + addr |= ((uint32_t)dev.function) << 8; + addr |= offset & 0xFC; + + outl(CONF_ADDR, addr); + outl(CONF_DATA, dword); +} + +void pci_sys_wcfg_w(struct pci_device dev, uint8_t offset, uint16_t word) { + size_t shift = (offset & 2) * 8; + uint32_t dword = pci_sys_rcfg_d(dev, offset); + dword &= ~(0xFFFF << shift); + dword |= word << shift; + pci_sys_wcfg_d(dev, offset, dword); +} + +void pci_sys_wcfg_b(struct pci_device dev, uint8_t offset, uint8_t byte) { + size_t shift = (offset & 3) * 8; + uint32_t dword = pci_sys_rcfg_d(dev, offset); + dword &= ~(0xFF << shift); + dword |= byte << shift; + pci_sys_wcfg_d(dev, offset, dword); +} |