SayoriOS 0.3.3

ata.h
1 #pragma once
2
3 #include "../../common.h"
4
5 #define ATA_SR_BSY 0x80
6 #define ATA_SR_DRDY 0x40
7 #define ATA_SR_DF 0x20
8 #define ATA_SR_DSC 0x10
9 #define ATA_SR_DRQ 0x08
10 #define ATA_SR_CORR 0x04
11 #define ATA_SR_IDX 0x02
12 #define ATA_SR_ERR 0x01
13
14 #define ATA_ER_BBK 0x80
15 #define ATA_ER_UNC 0x40
16 #define ATA_ER_MC 0x20
17 #define ATA_ER_IDNF 0x10
18 #define ATA_ER_MCR 0x08
19 #define ATA_ER_ABRT 0x04
20 #define ATA_ER_TK0NF 0x02
21 #define ATA_ER_AMNF 0x01
22
23 #define ATA_CMD_READ_PIO 0x20
24 #define ATA_CMD_READ_PIO_EXT 0x24
25 #define ATA_CMD_READ_DMA 0xC8
26 #define ATA_CMD_READ_DMA_EXT 0x25
27 #define ATA_CMD_WRITE_PIO 0x30
28 #define ATA_CMD_WRITE_PIO_EXT 0x34
29 #define ATA_CMD_WRITE_DMA 0xCA
30 #define ATA_CMD_WRITE_DMA_EXT 0x35
31 #define ATA_CMD_CACHE_FLUSH 0xE7
32 #define ATA_CMD_CACHE_FLUSH_EXT 0xEA
33 #define ATA_CMD_PACKET 0xA0
34 #define ATA_CMD_IDENTIFY_PACKET 0xA1
35 #define ATA_CMD_IDENTIFY 0xEC
36
37 #define ATA_IDENT_DEVICETYPE 0
38 #define ATA_IDENT_CYLINDERS 1
39 #define ATA_IDENT_HEADS 3
40 #define ATA_IDENT_SECTORS 6
41 #define ATA_IDENT_SERIAL 10
42 #define ATA_IDENT_MODEL 27
43 #define ATA_IDENT_CAPABILITIES 49
44 #define ATA_IDENT_FIELDVALID 53
45 #define ATA_IDENT_MAX_LBA 60
46 #define ATA_IDENT_COMMANDSETS 82
47 #define ATA_IDENT_MAX_LBA_EXT 100
48
49 #define ATA_MASTER 0x00
50 #define ATA_SLAVE 0x01
51
52 #define ATA_DMA_PRIMARY_CMD 0x00
53 #define ATA_DMA_PRIMARY_STATUS 0x02
54 #define ATA_DMA_PRIMARY_PRDT 0x04
55
56 #define ATA_DMA_SECONDARY_CMD 0x08
57 #define ATA_DMA_SECONDARY_STATUS 0x0A
58 #define ATA_DMA_SECONDARY_PRDT 0x0C
59
60 #define ATA_REG_DATA 0x00
61 #define ATA_REG_ERROR 0x01
62 #define ATA_REG_FEATURES 0x01
63 #define ATA_REG_SECCOUNT0 0x02
64 #define ATA_REG_LBA0 0x03
65 #define ATA_REG_LBA1 0x04
66 #define ATA_REG_LBA2 0x05
67 #define ATA_REG_HDDEVSEL 0x06
68 #define ATA_REG_COMMAND 0x07
69 #define ATA_REG_STATUS 0x07
70 #define ATA_REG_SECCOUNT1 0x08
71 #define ATA_REG_LBA3 0x09
72 #define ATA_REG_LBA4 0x0A
73 #define ATA_REG_LBA5 0x0B
74 #define ATA_REG_CONTROL 0x0C
75 #define ATA_REG_ALTSTATUS 0x0C
76 #define ATA_REG_DEVADDRESS 0x0D
77
78 // Channels:
79 #define ATA_PRIMARY 0x00
80 #define ATA_SECONDARY 0x01
81
82 // Directions:
83 #define ATA_READ 0x00
84 #define ATA_WRITE 0x013
85
86
87 #define ATA_PRIMARY_IO 0x1F0
88 #define ATA_SECONDARY_IO 0x170
89
90 #define ATA_PRIMARY_DCR_AS 0x3F6
91 #define ATA_SECONDARY_DCR_AS 0x376
92
93 #define ATA_PRIMARY_IRQ 14
94 #define ATA_SECONDARY_IRQ 15
95
96 #define PRIM_SEC(bus) ((bus) == ATA_PRIMARY?"Primary ":"Secondary")
97 #define MAST_SLV(drive) ((drive) == ATA_MASTER?"master":"slave ")
98 #define DRIVE(bus, drive) ((bus) << 1 | (drive))
99 #define ATA_PORT(bus) ((bus) == ATA_PRIMARY ? ATA_PRIMARY_IO : ATA_SECONDARY_IO)
100
102 typedef struct {
103 uint8_t drive;
104 bool online;
105 size_t capacity;
106
107 bool is_packet;
108 bool is_medium_inserted;
109 bool is_chs_addressing;
110
111 uint16_t block_size;
112
113 uint16_t cylinders;
114 uint16_t heads;
115 uint16_t sectors;
116
117 bool is_sata;
118 bool is_dma;
119
120 char* model_name;
121 char* fwversion;
122 char* serial_number;
123 } ata_drive_t;
124
125
126 void ide_select_drive(uint8_t bus, bool slave);
127 void ide_400ns_delay(uint16_t io);
128 void ide_poll(uint16_t io);
129 bool ide_poll_drq(uint16_t io);
130 bool ide_poll_bsy(uint16_t io);
131
132 void ata_read(uint8_t drive, uint8_t* buf, uint32_t location, uint32_t length) ;
133 void ata_write(uint8_t drive, const uint8_t* buf, size_t location, size_t length);
134
135 void ata_list();
136 void ata_init();
137
138 ata_drive_t* ata_get_drives();
139 void ata_check_all();
140
141 uint8_t ide_identify(uint8_t bus, uint8_t drive);
142
143 static inline void ata_set_params(uint8_t drive, uint16_t* io, uint8_t* real_drive) {
144 uint8_t _io = drive >> 1; // PRIMARY / SECONDARY
145 uint8_t _drv = drive & 1; // MASTER / SLAVE
146
147 if(_io == ATA_PRIMARY)
148 *io = ATA_PRIMARY_IO;
149 else if(_io == ATA_SECONDARY)
150 *io = ATA_SECONDARY_IO;
151
152 *real_drive = _drv;
153 }
ata_drive_t
Эта структура определяет каждый ATA диск в системе
Definition: ata.h:102
ata_drive_t::is_sata
bool is_sata
Сектора
Definition: ata.h:117
ata_drive_t::is_dma
bool is_dma
Является ли этот диск SATA устройством?
Definition: ata.h:118
ata_drive_t::heads
uint16_t heads
Цилиндры
Definition: ata.h:114
ata_drive_t::is_medium_inserted
bool is_medium_inserted
Является ли пакетным устройством?
Definition: ata.h:108
ata_drive_t::model_name
char * model_name
Поддерживает ли этот диск DMA?
Definition: ata.h:120
ata_drive_t::cylinders
uint16_t cylinders
Размер сектора
Definition: ata.h:113
ata_drive_t::sectors
uint16_t sectors
Головки
Definition: ata.h:115
ata_drive_t::block_size
uint16_t block_size
Адресация по CHS?
Definition: ata.h:111
ata_drive_t::capacity
size_t capacity
Устройство доступно?
Definition: ata.h:105
ata_drive_t::is_packet
bool is_packet
Ёмкость диска в секторах
Definition: ata.h:107