improve pci driver

This commit is contained in:
Michael Shaw 2023-05-06 17:59:46 -04:00
commit 9ac63f29dd
6 changed files with 75 additions and 33 deletions

View file

@ -14,11 +14,11 @@ static void ahci_irq_handler()
void ahci_init()
{
struct pci_device *dev = pci_find_device(0x01, 0x06);
/* check interrupt number */
if (dev == NULL) {
printf(LOG_ERROR "Failed to find AHCI device\n");
return;
}
abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start);
if (!(abar->ghc & (1 << 31))) {
@ -27,12 +27,14 @@ void ahci_init()
}
/* setup device irq number */
uint8_t irq = irq_highest_free();
uint16_t tmp = pci_config_read_word(dev->bus, dev->slot, 0, 0x3C);
tmp &= 0xFF00;
uint16_t irq = irq_highest_free();
uint32_t tmp = pci_config_read_long(dev->bus, dev->slot, 0, 0x3C);
tmp &= 0xFFFFFF00;
tmp |= irq;
pci_config_write_word(dev->bus, dev->slot, 0, 0x3C, tmp);
pci_config_write_long(dev->bus, dev->slot, 0, 0x3C, tmp);
irq_map(irq, ahci_irq_handler);
tmp = pci_config_read_long(dev->bus, dev->slot, 0, 0x3C);
printf(LOG_SUCCESS "AHCI controller ready\n");
}

View file

@ -8,23 +8,20 @@
static struct pci_device *pci_devices;
static uint8_t pci_device_count;
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset)
uint32_t pci_config_read_long(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset)
{
uint32_t addr;
uint32_t lbus = bus;
uint32_t lslot = slot;
uint32_t lfunc = func;
uint16_t tmp = 0;
addr = (lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xfc) | 0x80000000;
outl(PCIPM_CONFIG_ADDRESS, addr);
tmp = (uint16_t)((inl(PCIPM_CONFIG_DATA) >> ((offset & 2) << 3)) & 0xFFFF);
return tmp;
return inl(PCIPM_CONFIG_DATA);
}
void pci_config_write_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t data)
void pci_config_write_long(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t data)
{
uint32_t addr;
uint32_t lbus = bus;
@ -40,10 +37,13 @@ void pci_config_write_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offs
static void pci_scan_bus(uint8_t bus)
{
for (uint8_t slot = 0; slot < 32; slot++) {
uint16_t vendor_id = pci_config_read_word(bus, slot, 0, 0);
uint16_t device_id = pci_config_read_word(bus, slot, 0, 2);
uint8_t class = pci_config_read_word(bus, slot, 0, 0xA);
uint8_t subclass = (pci_config_read_word(bus, slot, 0, 0xA) >> 8);
uint32_t vendor_device = pci_config_read_long(bus, slot, 0, 0x0);
uint16_t vendor_id = vendor_device >> 16;
uint16_t device_id = vendor_device & 0xFFFF;
uint32_t class_subclass = pci_config_read_long(bus, slot, 0, 0x8);
uint8_t class = (class_subclass >> 16) & 0xFF;
uint8_t subclass = (class_subclass >> 24) & 0xFF;
if (vendor_id == 0xFFFF || class == 0xFF)
continue;
@ -55,27 +55,21 @@ static void pci_scan_bus(uint8_t bus)
pci_devices[pci_device_count].class = class;
pci_devices[pci_device_count].subclass = subclass;
pci_devices[pci_device_count].bar0 = pci_config_read_word(bus, slot, 0, 0x10);
pci_devices[pci_device_count].bar0 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x12) << 16);
pci_devices[pci_device_count].bar0 = pci_config_read_long(bus, slot, 0, 0x10);
pci_devices[pci_device_count].bar1 = pci_config_read_word(bus, slot, 0, 0x14);
pci_devices[pci_device_count].bar1 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x16) << 16);
pci_devices[pci_device_count].bar1 = pci_config_read_long(bus, slot, 0, 0x14);
uint8_t header_type = pci_config_read_word(bus, slot, 0, 0xE) >> 16;
uint8_t header_type = (pci_config_read_long(bus, slot, 0, 0xC) >> 24) & 0xFF;
pci_devices[pci_device_count].header_type = header_type;
if (header_type == 0) {
pci_devices[pci_device_count].bar2 = pci_config_read_word(bus, slot, 0, 0x18);
pci_devices[pci_device_count].bar2 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x1A) << 16);
pci_devices[pci_device_count].bar2 = pci_config_read_long(bus, slot, 0, 0x18);
pci_devices[pci_device_count].bar3 = pci_config_read_word(bus, slot, 0, 0x1C);
pci_devices[pci_device_count].bar3 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x1E) << 16);
pci_devices[pci_device_count].bar3 = pci_config_read_long(bus, slot, 0, 0x1C);
pci_devices[pci_device_count].bar4 = pci_config_read_word(bus, slot, 0, 0x20);
pci_devices[pci_device_count].bar4 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x22) << 16);
pci_devices[pci_device_count].bar4 = pci_config_read_long(bus, slot, 0, 0x20);
pci_devices[pci_device_count].bar5 = pci_config_read_word(bus, slot, 0, 0x24);
pci_devices[pci_device_count].bar5 |= ((uint32_t)pci_config_read_word(bus, slot, 0, 0x26) << 16);
pci_devices[pci_device_count].bar5 = pci_config_read_long(bus, slot, 0, 0x24);
}
pci_device_count++;