Files
RockOS/kernel/drivers/atapi/ata.h
2026-06-21 16:57:16 -05:00

212 lines
7.2 KiB
C

#ifndef KATA_H
#define KATA_H
#ifndef __ASSEMBLER__
#include <stdint.h>
#include <stddef.h>
#endif
#define IDE_PRIMARY_IO_BASE 0x01F0
#define IDE_PRIMARY_CRTL_BASE 0x03F6
#define IDE_SECONDARY_IO_BASE 0x0170
#define IDE_SECONDARY_CRTL_BASE 0x0376
#define IDE_DRIVE_MASTER 0xA0
#define IDE_DRIVE_SLAVE 0xB0
#define IO_DATA_REG 0x0000
#define IO_ERROR_REG 0x0001
#define IO_FEATURE_REG 0x0001
#define IO_SECTOR_COUNT_REG 0x0002
#define IO_SECTOR_NUM_REG 0x0003
#define IO_LBA_LO_REG 0x0003
#define IO_CYL_LO_REG 0x0004
#define IO_LBA_MID_REG 0x0004
#define IO_LBA_HI_REG 0x0005
#define IO_CYL_HI_REG 0x0005
#define IO_DRIVE_SELECT_REG 0x0006
#define IO_STATUS_REG 0x0007
#define IO_CMD_REG 0x0007
#define CTRL_ALT_STATUS_REG 0x00
#define CTRL_DEV_CTRL_REG 0x00
#define CTRL_DRIVE_ADDR_REG 0x01
#define ERROR_REG_AMNF 0b00000001 // Address mark not found
#define ERROR_REG_TKZNF 0b00000010 // Track zero not found
#define ERROR_REG_ABRT 0b00000100 // Aborted command
#define ERROR_REG_MCR 0b00001000 // Media change request
#define ERROR_REG_IDNF 0b00010000 // ID not found
#define ERROR_REG_MC 0b00100000 // Media changed
#define ERROR_REG_UNC 0b01000000 // Uncorrectable data error
#define ERROR_REG_BBK 0b10000000 // Bad Block detected
#define DRIVE_SELECT_REG_BN 0b00000111
#define DRIVE_SELECT_REG_DRV 0b00010000
#define DRIVE_SELECT_REG_LBAA 0b01000000
#define STATUS_REG_ERR 0b00000001
#define STATUS_REG_IDX 0b00000010
#define STATUS_REG_CORR 0b00000100
#define STATUS_REG_DRQ 0b00001000
#define STATUS_REG_SRV 0b00010000
#define STATUS_REG_DF 0b00100000
#define STATUS_REG_RDY 0b01000000
#define STATUS_REG_BSY 0b10000000
#define DEV_CTRL_REG_nIEN 0b00000001
#define DEV_CTRL_REG_SRST 0b00000010
#define DEV_CTRL_REG_HOB 0b10000000
#define DRIVE_ADDR_REG_DS0 0b00000001
#define DRIVE_ADDR_REG_DS1 0b00000010
#define DRIVE_ADDR_REG_HEAD 0b00111100
#define DRIVE_ADDR_REG_WTG 0b01000000
#define ATAPI_CMD_IDENTIFY_PACKET 0xA1
#ifndef __ASSEMBLER__
uint8_t ident_device(uint16_t bus, uint8_t drive_select, uint8_t* buffer);
int read_atapi(uint16_t bus, uint8_t drive, uint32_t lba, uint32_t sectors, uint16_t* buf);
void send_scsi_packet(uint16_t bus, void* cmd, size_t sz);
void read_data_words(uint16_t bus, void* buf, size_t sz);
typedef struct __attribute__((packed)) {
uint16_t bus;
uint8_t device;
uint8_t type;
void* ident_info_struct;
} ide_device_t;
typedef struct {
// Word 0: General configuration bit-map
struct {
uint16_t packet_size : 2; // 00=12 bytes, 01=16 bytes
uint16_t reserved1 : 3;
uint16_t drq_response : 2; // DRQ response microsecond timing
uint16_t device_type : 5; // Device type (e.g., 0x05 for CD/DVD-ROM)
uint16_t reserved2 : 1;
uint16_t removable_media : 1; // 1 = Removable media
uint16_t command_packet_type : 2; // 10 = ATAPI device
} general_config;
uint16_t logical_cylinders;
uint16_t specific_configuration; // Word 2: Specific configuration
uint16_t logical_heads; // Word 3: Obsolete / Reserved
uint16_t retired1[2]; // Words 4-5: Retired
uint16_t logical_sectors_per_track; // Word 6: Obsolete / Reserved
uint16_t reserved_vendor[3]; // Words 7-9: Vendor specific
char serial_number[20]; // Words 10-19: Serial number (ASCII, byte-swapped)
uint16_t retired2[2]; // Words 20-21: Retired
uint16_t obsolete1; // Word 22: Obsolete
char firmware_revision[8]; // Words 23-26: Firmware revision (ASCII, byte-swapped)
char model_number[40]; // Words 27-46: Model number (ASCII, byte-swapped)
uint16_t sectors_per_interrupt; // Word 47: Bits 7:0 = Max sectors per interrupt
uint16_t reserved3; // Word 48: Reserved
// Word 49: Capabilities
struct {
uint16_t reserved1 : 8;
uint16_t dma_supported : 1; // 1 = DMA supported
uint16_t lba_supported : 1; // 1 = LBA supported
uint16_t iordy_disable : 1; // 1 = IORDY can be disabled
uint16_t iordy_supported : 1; // 1 = IORDY supported
uint16_t reserved2 : 1;
uint16_t overlap_supported : 1; // 1 = Overlap operation supported
uint16_t reserved3 : 2;
} capabilities1;
uint16_t capabilities2; // Word 50: Capabilities bit 0 = Min MDMA cycle time info
uint16_t pio_data_transfer_timing;// Word 51: Obsolete
uint16_t dma_data_transfer_timing;// Word 52: Obsolete
// Word 53: Field validity
struct {
uint16_t fields_54_58_valid : 1; // 1 = Words 54-58 are valid
uint16_t fields_64_70_valid : 1; // 1 = Words 64-70 are valid
uint16_t field_88_valid : 1; // 1 = Word 88 is valid (Ultra DMA)
uint16_t reserved : 13;
} field_validity;
uint16_t obsolete2[5]; // Words 54-58: Obsolete
uint16_t multi_sector_setting; // Word 59: Multi-sector setting
uint32_t total_addressable_sectors; // Words 60-61: Total number of user addressable sectors (LBA28)
uint16_t single_word_dma_modes;
uint16_t multi_word_dma_modes;
struct {
uint16_t pio_mode_3 : 1;
uint16_t pio_mode_4 : 1;
uint16_t reserved : 14;
} advanced_pio_modes;
uint16_t min_mwdma_cycle_time;
uint16_t rec_mwdma_cycle_time;
uint16_t min_pio_cycle_time_no_flow;
uint16_t min_pio_cycle_time_iordy;
uint16_t reserved4[2];
uint16_t packet_release_time;
uint16_t service_clear_time;
uint16_t major_revision_number;
uint16_t minor_revision_number;
uint16_t queue_depth;
uint16_t reserved5[4];
uint16_t major_version;
uint16_t minor_version;
uint16_t command_sets_supported1;
uint16_t command_sets_supported2;
uint16_t command_sets_supported3;
uint16_t command_sets_enabled1;
uint16_t command_sets_enabled2;
uint16_t command_sets_enabled3;
uint16_t ultra_dma_modes;
uint16_t reserved_erase_time[2];
uint16_t current_apm_level;
uint16_t master_password_revision;
uint16_t hardware_reset_result;
uint16_t obsolete3;
uint16_t stream_minimum_request;
uint16_t streaming_transfer_delay;
uint16_t streaming_access_latency;
uint32_t streaming_perf_granularity;
uint64_t total_user_sectors_lba48;
uint16_t streaming_transfer_delay_block;
uint16_t reserved6;
struct {
uint16_t logical_sectors_per_physical : 4;
uint16_t reserved1 : 8;
uint16_t logical_sector_longer_than_256: 1;
uint16_t physical_sector_longer_than_logical: 1;
uint16_t validation_bits : 2;
} physical_sector_size;
uint16_t inter_seek_delay;
uint16_t world_wide_name[4];
uint16_t reserved7[8];
uint16_t command_submission_delay;
uint16_t reserved8[7];
uint16_t security_status;
uint16_t vendor_specific[31];
uint16_t cfa_power_mode;
uint16_t reserved9[15];
char media_serial_number[60];
uint16_t sct_command_transport;
uint16_t reserved10[49];
} __attribute__((packed)) atapi_identify_t;
#endif
#endif