begin ahci driver

This commit is contained in:
Michael Shaw 2023-05-04 21:47:48 -04:00
commit 87146087ca
7 changed files with 529 additions and 2 deletions

226
include/dev/ahci.h Normal file
View file

@ -0,0 +1,226 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _AHCI_H_
#define _AHCI_H_
#include <kernel/common.h>
#include <dev/pci.h>
/* Frame information structure (FIS) */
#define FIS_REG_H2D 0x27
#define FIS_REG_D2H 0x34
#define FIS_DMA_ACT 0x39
#define FIS_DMA_SETUP 0x41
#define FIS_DATA 0x46
#define FIS_BIST 0x58
#define FIS_PIO_SETUP 0x5F
#define FIS_DEV_BITS 0xA1
#define ATA_CMD_IDENT 0xEC
struct fis_reg_h2d {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 3;
uint8_t cmd_mode : 1; /* high: command, low: control */
uint8_t command;
uint8_t feature_low;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t feature_high;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t icc;
uint8_t control;
/* 0x10 */
uint32_t reserved2;
} PACKED;
struct fis_reg_d2h {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 2;
uint8_t intr : 1;
uint8_t reserved2 : 1;
uint8_t status;
uint8_t error;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t feature_high;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t icc;
uint8_t control;
/* 0x10 */
uint32_t reserved3;
} PACKED;
struct fis_data {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 4;
uint8_t reserved2[2];
/* 0x04 */
uint32_t data[1]; /* Variable length */
} PACKED;
struct fis_dma_setup {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 1;
uint8_t data_dir : 1;
uint8_t intr : 1;
uint8_t auto_act : 1;
uint16_t reserved2;
/* 0x04 */
uint64_t dma_buffer_id;
/* 0x0C */
uint32_t reserved3;
/* 0x10 */
uint32_t dma_buffer_offset;
/* 0x14 */
uint32_t transfer_count;
/* 0x18 */
uint32_t reserved4;
} PACKED;
struct fis_pio_setup {
/* 0x00 */
uint8_t fis_type;
uint8_t pmult : 4;
uint8_t reserved : 1;
uint8_t data_dir : 1;
uint8_t intr : 1;
uint8_t reserved2 : 1;
uint8_t status;
uint8_t error;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t reserved3;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t reserved4;
uint8_t e_status;
/* 0x10 */
uint16_t transfer_count;
uint16_t reserved5;
} PACKED;
typedef volatile struct {
/* 0x00 */
uint32_t clb;
uint32_t clbu;
uint32_t fb;
uint32_t fbu;
/* 0x10 */
uint32_t is;
uint32_t ie;
uint32_t cmd;
uint32_t reserved;
/* 0x20 */
uint32_t tfd;
uint32_t sig;
uint32_t ssts;
uint32_t sctl;
/* 0x30 */
uint32_t serr;
uint32_t sact;
uint32_t ci;
uint32_t sntf;
/* 0x40 */
uint32_t fbs;
uint32_t reserved1[11];
uint32_t vendor[4];
} PACKED hbaport_t;
typedef volatile struct {
/* 0x00 */
uint32_t cap;
uint32_t ghc;
uint32_t is;
uint32_t pi;
uint32_t vs;
/* 0x14 */
uint32_t ccc_ctl;
uint32_t ccc_ports;
uint32_t em_loc;
uint32_t em_ctl;
uint32_t cap2;
/* 0x28 */
uint32_t bohc;
/* 0x2C */
uint8_t reserved[0x74];
/* 0xA0 */
char vendor[0x60];
/* 0x100 */
hbaport_t ports[32];
} PACKED hbamem_t;
void ahci_init();
#endif /* _AHCI_H_ */

View file

@ -17,9 +17,99 @@ struct pci_device {
uint16_t vendor_id;
uint16_t device_id;
uint8_t header_type;
paddr32_t bar0;
paddr32_t bar1;
paddr32_t bar2;
paddr32_t bar3;
paddr32_t bar4;
paddr32_t bar5;
};
struct pci_header_base {
uint16_t vendor_id;
uint16_t device_id;
uint16_t command;
uint16_t status;
uint8_t revision_id;
uint8_t prog_if;
uint8_t subclass;
uint8_t class;
uint8_t cache_line_size;
uint8_t latency_timer;
uint8_t header_type;
uint8_t bist;
} PACKED;
struct pci_header_type_0 {
struct pci_header_base base;
paddr32_t bar0;
paddr32_t bar1;
paddr32_t bar2;
paddr32_t bar3;
paddr32_t bar4;
paddr32_t bar5;
uint32_t cardbus_cis_pointer;
uint16_t subsystem_vendor_id;
uint16_t subsystem_id;
uint32_t expansion_rom_base_address;
uint8_t capabilities_pointer;
uint8_t reserved[7];
uint8_t interrupt_line;
uint8_t interrupt_pin;
uint8_t min_grant;
uint8_t max_latency;
} PACKED;
struct pci_header_type_1 {
struct pci_header_base base;
paddr32_t bar0;
paddr32_t bar1;
uint8_t primary_bus_number;
uint8_t secondary_bus_number;
uint8_t subordinate_bus_number;
uint8_t secondary_latency_timer;
uint8_t io_base;
uint8_t io_limit;
uint16_t secondary_status;
uint16_t memory_base;
uint16_t memory_limit;
uint16_t prefetchable_memory_base;
uint16_t prefetchable_memory_limit;
uint32_t prefetchable_base_upper;
uint32_t prefetchable_limit_upper;
uint16_t io_base_upper;
uint16_t io_limit_upper;
uint8_t capabilities_pointer;
uint8_t reserved[3];
uint32_t expansion_rom_base_address;
uint8_t interrupt_line;
uint8_t interrupt_pin;
uint16_t bridge_control;
} PACKED;
void pci_init();
uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
struct pci_device *pci_find_device(uint8_t class, uint8_t subclass);
#endif /* _PCI_H_ */