From 2b975b4a1bb171f81ecbbd70940ac2cd3d7ae30b Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sun, 3 Jul 2016 21:31:44 +0200 Subject: [PATCH] Offer more capabilities for the drivers --- kernel/include/pci.hpp | 3 +++ kernel/src/pci.cpp | 49 ++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/kernel/include/pci.hpp b/kernel/include/pci.hpp index 30418b30..767be526 100644 --- a/kernel/include/pci.hpp +++ b/kernel/include/pci.hpp @@ -51,6 +51,9 @@ void detect_devices(); size_t number_of_devices(); device_descriptor& device(size_t index); +uint32_t read_config_dword(uint8_t bus, uint8_t device, uint8_t function, uint8_t offset); +void write_config_dword(uint8_t bus, uint8_t device, uint8_t function, uint8_t offset, uint32_t value); + } //end of namespace pci #endif diff --git a/kernel/src/pci.cpp b/kernel/src/pci.cpp index c943f0e5..4718052a 100644 --- a/kernel/src/pci.cpp +++ b/kernel/src/pci.cpp @@ -18,50 +18,37 @@ std::vector devices; #define PCI_CONFIG_ADDRESS 0xCF8 #define PCI_CONFIG_DATA 0xCFC - uint32_t pci_config_read_dword (uint8_t bus, uint8_t device, uint8_t function, uint8_t offset){ - uint32_t address = - static_cast(1 << 31) //enabled - | (uint32_t(bus) << 16) //bus number - | (uint32_t(device) << 11) //device number - | (uint32_t(function) << 8) //function number - | ((uint32_t(offset) ) & 0xfc); //Register number - - out_dword(PCI_CONFIG_ADDRESS, address); - - return in_dword(PCI_CONFIG_DATA); - } - uint16_t get_vendor_id(uint8_t bus, uint8_t device, uint8_t function){ // read "device id | vendor id" - auto large = pci_config_read_dword(bus, device, function, 0); + auto large = pci::read_config_dword(bus, device, function, 0); // extract vendor id return large; } uint16_t get_device_id(uint8_t bus, uint8_t device, uint8_t function){ // read "device id | vendor id" - auto large = pci_config_read_dword(bus, device, function, 0); + auto large = pci::read_config_dword(bus, device, function, 0); // extract device id return large >> 16; } uint8_t get_class_code(uint8_t bus, uint8_t device, uint8_t function){ // read "class code | subclass | prog if | revision id" - auto large = pci_config_read_dword(bus, device, function, 8); + auto large = pci::read_config_dword(bus, device, function, 8); // extract class code only return large >> 24 & 0xFF; } uint8_t get_subclass(uint8_t bus, uint8_t device, uint8_t function){ // read "class code | subclass | prog if | revision id" - auto large = pci_config_read_dword(bus, device, function, 8); + auto large = pci::read_config_dword(bus, device, function, 8); // extract subclass only return large >> 16 & 0xFF; } uint8_t get_header_type(uint8_t bus, uint8_t device, uint8_t function){ // read "BIST | header type | latency timer | cache line size" - auto large = pci_config_read_dword(bus, device, function, 12); + auto large = pci::read_config_dword(bus, device, function, 12); // extract header type only return large >> 16 & 0xFF; } @@ -139,3 +126,29 @@ size_t pci::number_of_devices(){ pci::device_descriptor& pci::device(size_t index){ return devices[index]; } + +uint32_t pci::read_config_dword (uint8_t bus, uint8_t device, uint8_t function, uint8_t offset){ + uint32_t address = + static_cast(1 << 31) //enabled + | (uint32_t(bus) << 16) //bus number + | (uint32_t(device) << 11) //device number + | (uint32_t(function) << 8) //function number + | ((uint32_t(offset) ) & 0xfc); //Register number + + out_dword(PCI_CONFIG_ADDRESS, address); + + return in_dword(PCI_CONFIG_DATA); +} + +void pci::write_config_dword (uint8_t bus, uint8_t device, uint8_t function, uint8_t offset, uint32_t value){ + uint32_t address = + static_cast(1 << 31) //enabled + | (uint32_t(bus) << 16) //bus number + | (uint32_t(device) << 11) //device number + | (uint32_t(function) << 8) //function number + | ((uint32_t(offset) ) & 0xfc); //Register number + + out_dword(PCI_CONFIG_ADDRESS, address); + + out_dword(PCI_CONFIG_DATA, value); +}