diff --git a/dev/ahci.c b/dev/ahci.c index 9fbde99..680c691 100644 --- a/dev/ahci.c +++ b/dev/ahci.c @@ -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"); } diff --git a/dev/pci.c b/dev/pci.c index f501ef1..c330237 100644 --- a/dev/pci.c +++ b/dev/pci.c @@ -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++; diff --git a/include/dev/ahci.h b/include/dev/ahci.h index d8e0f6a..d1824ba 100644 --- a/include/dev/ahci.h +++ b/include/dev/ahci.h @@ -179,6 +179,49 @@ struct fis_set_device_bits { uint8_t errorl : 3; } PACKED; +struct hba_cmd_header { + uint8_t cmd_fis_len : 5; + uint8_t atapi : 1; + uint8_t write : 1; + uint8_t prefetch: 1; + + uint8_t reset : 1; + uint8_t bist : 1; + uint8_t clear_busy : 1; + uint8_t reserved : 1; + uint8_t pmp : 4; + + uint16_t prdt_len; + + volatile uint32_t prdb_count; + + uint32_t cmd_table_base_low; + uint32_t cmd_table_base_high; + + uint32_t reserved2[4]; +} PACKED; + +struct hba_prdt_entry { + uint32_t data_base_low; + uint32_t data_base_high; + + uint32_t reserved; + + uint32_t byte_count : 22; + uint32_t reserved2 : 9; + uint32_t interrupt : 1; +} PACKED; + +struct hba_cmd_tbl { + uint8_t cfis[64]; + + uint8_t acmd[16]; + + uint8_t reserved[48]; + + struct hba_prdt_entry prdt_entry[1]; +} PACKED; + typedef volatile struct { /* 0x00 */ uint32_t clb; diff --git a/include/dev/pci.h b/include/dev/pci.h index bcce1e9..c05a0bf 100644 --- a/include/dev/pci.h +++ b/include/dev/pci.h @@ -109,8 +109,8 @@ struct pci_header_type_1 { } PACKED; void pci_init(); -uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset); -void pci_config_write_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t data); +uint32_t pci_config_read_long(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset); +void pci_config_write_long(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t data); struct pci_device *pci_find_device(uint8_t class, uint8_t subclass); #endif /* _PCI_H_ */ diff --git a/init/irq.c b/init/irq.c index f22057f..1c0b741 100644 --- a/init/irq.c +++ b/init/irq.c @@ -104,7 +104,7 @@ void irq_unmap(uint8_t irq) int irq_highest_free() { - for (int i = 0; i < 16; i++) { + for (int i = 15; i >= 0; i--) { if (!irq_mapped[i]) return i; } diff --git a/init/main.c b/init/main.c index b0428fb..38f2403 100644 --- a/init/main.c +++ b/init/main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -67,8 +68,6 @@ void kmain(void) pci_init(); - _pic_init(); - void *kstack = buddy_alloc(KSTACK_SIZE); uintptr_t ptr = (uintptr_t)kstack + KSTACK_SIZE - 8; stack_init(ptr, ptr); @@ -76,6 +75,10 @@ void kmain(void) void kmain_post_stack_init() { + ahci_init(); + + _pic_init(); + printf("\n\n# "); yield(); }