xref: /DragonOS/kernel/src/driver/disk/ata.h (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1 #pragma once
2 
3 #include <common/glib.h>
4 
5 // ======== PIO端口定义 ========
6 #define PORT_DISK0_DATA 0x1f0                     // 数据
7 #define PORT_DISK0_ERR_STATUS 0x1f1               // 错误状态
8 #define PORT_DISK0_SECTOR_CNT 0x1f2               // 操作扇区数
9 #define PORT_DISK0_LBA_7_0 0x1f3                  // 扇区号 / LBA[7:0]
10 #define PORT_DISK0_LBA_15_8 0x1f4                 // 柱面号[7:0] / LBA[15:8]
11 #define PORT_DISK0_LBA_23_16 0x1f5                // 柱面号[15:8] / LBA[23:16]
12 #define PORT_DISK0_DEVICE_CONFIGURE_REG 0x1f6 // 设备配置寄存器
13 #define PORT_DISK0_CONTROLLER_STATUS_CMD 0x1f7    // 控制器状态端口 / 控制器命令端口
14 #define PORT_DISK0_STATUS_CTRL_REG 0x3f6          // 状态寄存器 / 控制寄存器
15 
16 #define PORT_DISK1_DATA 0x170                  // 数据
17 #define PORT_DISK1_ERR_STATUS 0x171            // 错误状态
18 #define PORT_DISK1_SECTOR_CNT 0x172            // 操作扇区数
19 #define PORT_DISK1_LBA_7_0 0x173               // 扇区号 / LBA[7:0]
20 #define PORT_DISK1_LBA_15_8 0x174              // 柱面号[7:0] / LBA[15:8]
21 #define PORT_DISK1_LBA_23_16 0x175             // 柱面号[15:8] / LBA[23:16]
22 #define PORT_DISK1_DEVICE_CONFIGURE_REG 0x176  // 设备配置寄存器
23 #define PORT_DISK1_CONTROLLER_STATUS_CMD 0x177 // 控制器状态端口 / 控制器命令端口
24 #define PORT_DISK1_STATUS_CTRL_REG 0x376       // 状态寄存器 / 控制寄存器
25 
26 // ======= 状态寄存器的状态位 ==========
27 #define DISK_STATUS_BUSY (1 << 7)       // 控制器忙
28 #define DISK_STATUS_READY (1 << 6)      // 驱动器准备就绪
29 #define DISK_STATUS_SEEK (1 << 4)       // 驱动器寻道
30 #define DISK_STATUS_DATA_REQ (1 << 3)   // 数据请求
31 #define DISK_STATUS_DATA_ERROR (1 << 0) // 命令执行错误
32 
33 /**
34  * @brief 执行0xec指令返回的512bytes的硬件设备识别信息
35  * 位于ATA8-ACS中 Table-22
36  */
37 struct ata_identify_device_data
38 {
39     //	0	General configuration bit-significant information
40     unsigned short General_Config;
41 
42     //	1	Obsolete
43     unsigned short Obsolete0;
44 
45     //	2	Specific configuration
46     unsigned short Specific_Coinfig;
47 
48     //	3	Obsolete
49     unsigned short Obsolete1;
50 
51     //	4-5	Retired
52     unsigned short Retired0[2];
53 
54     //	6	Obsolete
55     unsigned short Obsolete2;
56 
57     //	7-8	Reserved for the CompactFlash Association
58     unsigned short CompactFlash[2];
59 
60     //	9	Retired
61     unsigned short Retired1;
62 
63     //	10-19	Serial number (20 ASCII characters)
64     unsigned short Serial_Number[10];
65 
66     //	20-21	Retired
67     unsigned short Retired2[2];
68 
69     //	22	Obsolete
70     unsigned short Obsolete3;
71 
72     //	23-26	Firmware revision(8 ASCII characters)
73     unsigned short Firmware_Version[4];
74 
75     //	27-46	Model number (40 ASCII characters)
76     unsigned short Model_Number[20];
77 
78     //	47	15:8 	80h
79     //		7:0  	00h=Reserved
80     //			01h-FFh = Maximumnumber of logical sectors that shall be transferred per DRQ data block on READ/WRITE MULTIPLE commands
81     unsigned short Max_logical_transferred_per_DRQ;
82 
83     //	48	Trusted Computing feature set options
84     unsigned short Trusted_Computing_feature_set_options;
85 
86     //	49	Capabilities
87     unsigned short Capabilities0;
88 
89     //	50	Capabilities
90     unsigned short Capabilities1;
91 
92     //	51-52	Obsolete
93     unsigned short Obsolete4[2];
94 
95     //	53	15:8	Free-fall Control Sensitivity
96     //		7:3 	Reserved
97     //		2 	the fields reported in word 88 are valid
98     //		1 	the fields reported in words (70:64) are valid
99     unsigned short Report_88_70to64_valid;
100 
101     //	54-58	Obsolete
102     unsigned short Obsolete5[5];
103 
104     //	59	15:9	Reserved
105     //		8	Multiple sector setting is valid
106     //		7:0	xxh current setting for number of logical sectors that shall be transferred per DRQ data block on READ/WRITE Multiple commands
107     unsigned short Mul_Sec_Setting_Valid;
108 
109     //	60-61	Total number of user addresssable logical sectors for 28bit CMD
110     unsigned short Addressable_Logical_Sectors_for_28[2];
111 
112     //	62	Obsolete
113     unsigned short Obsolete6;
114 
115     //	63	15:11	Reserved
116     //		10:8=1 	Multiword DMA mode 210 is selected
117     //		7:3 	Reserved
118     //		2:0=1 	Multiword DMA mode 210 and below are supported
119     unsigned short MultWord_DMA_Select;
120 
121     //	64	15:8	Reserved
122     //		7:0	PIO mdoes supported
123     unsigned short PIO_mode_supported;
124 
125     //	65	Minimum Multiword DMA transfer cycle time per word
126     unsigned short Min_MulWord_DMA_cycle_time_per_word;
127 
128     //	66	Manufacturer`s recommended Multiword DMA transfer cycle time
129     unsigned short Manufacture_Recommend_MulWord_DMA_cycle_time;
130 
131     //	67	Minimum PIO transfer cycle time without flow control
132     unsigned short Min_PIO_cycle_time_Flow_Control;
133 
134     //	68	Minimum PIO transfer cycle time with IORDY flow control
135     unsigned short Min_PIO_cycle_time_IOREDY_Flow_Control;
136 
137     //	69-70	Reserved
138     unsigned short Reserved1[2];
139 
140     //	71-74	Reserved for the IDENTIFY PACKET DEVICE command
141     unsigned short Reserved2[4];
142 
143     //	75	Queue depth
144     unsigned short Queue_depth;
145 
146     //	76	Serial ATA Capabilities
147     unsigned short SATA_Capabilities;
148 
149     //	77	Reserved for Serial ATA
150     unsigned short Reserved3;
151 
152     //	78	Serial ATA features Supported
153     unsigned short SATA_features_Supported;
154 
155     //	79	Serial ATA features enabled
156     unsigned short SATA_features_enabled;
157 
158     //	80	Major Version number
159     unsigned short Major_Version;
160 
161     //	81	Minor version number
162     unsigned short Minor_Version;
163 
164     //	82	Commands and feature sets supported
165     unsigned short Cmd_feature_sets_supported0;
166 
167     //	83	Commands and feature sets supported
168     unsigned short Cmd_feature_sets_supported1;
169 
170     //	84	Commands and feature sets supported
171     unsigned short Cmd_feature_sets_supported2;
172 
173     //	85	Commands and feature sets supported or enabled
174     unsigned short Cmd_feature_sets_supported3;
175 
176     //	86	Commands and feature sets supported or enabled
177     unsigned short Cmd_feature_sets_supported4;
178 
179     //	87	Commands and feature sets supported or enabled
180     unsigned short Cmd_feature_sets_supported5;
181 
182     //	88	15 	Reserved
183     //		14:8=1 	Ultra DMA mode 6543210 is selected
184     //		7 	Reserved
185     //		6:0=1 	Ultra DMA mode 6543210 and below are suported
186     unsigned short Ultra_DMA_modes;
187 
188     //	89	Time required for Normal Erase mode SECURITY ERASE UNIT command
189     unsigned short Time_required_Erase_CMD;
190 
191     //	90	Time required for an Enhanced Erase mode SECURITY ERASE UNIT command
192     unsigned short Time_required_Enhanced_CMD;
193 
194     //	91	Current APM level value
195     unsigned short Current_APM_level_Value;
196 
197     //	92	Master Password Identifier
198     unsigned short Master_Password_Identifier;
199 
200     //	93	Hardware resset result.The contents of bits (12:0) of this word shall change only during the execution of a hardware reset.
201     unsigned short HardWare_Reset_Result;
202 
203     //	94	Current AAM value
204     //		15:8 	Vendor’s recommended AAM value
205     //		7:0 	Current AAM value
206     unsigned short Current_AAM_value;
207 
208     //	95	Stream Minimum Request Size
209     unsigned short Stream_Min_Request_Size;
210 
211     //	96	Streaming Transger Time-DMA
212     unsigned short Streaming_Transger_time_DMA;
213 
214     //	97	Streaming Access Latency-DMA and PIO
215     unsigned short Streaming_Access_Latency_DMA_PIO;
216 
217     //	98-99	Streaming Performance Granularity (DWord)
218     unsigned short Streaming_Performance_Granularity[2];
219 
220     //	100-103	Total Number of User Addressable Logical Sectors for 48-bit commands (QWord)
221     unsigned short Total_user_LBA_for_48_Address_Feature_set[4];
222 
223     //	104	Streaming Transger Time-PIO
224     unsigned short Streaming_Transfer_Time_PIO;
225 
226     //	105	Reserved
227     unsigned short Reserved4;
228 
229     //	106	Physical Sector size/Logical Sector Size
230     unsigned short Physical_Logical_Sector_Size;
231 
232     //	107	Inter-seek delay for ISO-7779 acoustic testing in microseconds
233     unsigned short Inter_seek_delay;
234 
235     //	108-111	World wide name
236     unsigned short World_wide_name[4];
237 
238     //	112-115	Reserved
239     unsigned short Reserved5[4];
240 
241     //	116	Reserved for TLC
242     unsigned short Reserved6;
243 
244     //	117-118	Logical sector size (DWord)
245     unsigned short Words_per_Logical_Sector[2];
246 
247     //	119	Commands and feature sets supported (Continued from words 84:82)
248     unsigned short CMD_feature_Supported;
249 
250     //	120	Commands and feature sets supported or enabled (Continued from words 87:85)
251     unsigned short CMD_feature_Supported_enabled;
252 
253     //	121-126	Reserved for expanded supported and enabled settings
254     unsigned short Reserved7[6];
255 
256     //	127	Obsolete
257     unsigned short Obsolete7;
258 
259     //	128	Security status
260     unsigned short Security_Status;
261 
262     //	129-159	Vendor specific
263     unsigned short Vendor_Specific[31];
264 
265     //	160	CFA power mode
266     unsigned short CFA_Power_mode;
267 
268     //	161-167	Reserved for the CompactFlash Association
269     unsigned short Reserved8[7];
270 
271     //	168	Device Nominal Form Factor
272     unsigned short Dev_from_Factor;
273 
274     //	169-175	Reserved
275     unsigned short Reserved9[7];
276 
277     //	176-205	Current media serial number (ATA string)
278     unsigned short Current_Media_Serial_Number[30];
279 
280     //	206	SCT Command Transport
281     unsigned short SCT_Cmd_Transport;
282 
283     //	207-208	Reserved for CE-ATA
284     unsigned short Reserved10[2];
285 
286     //	209	Alignment of logical blocks within a physical block
287     unsigned short Alignment_Logical_blocks_within_a_physical_block;
288 
289     //	210-211	Write-Read-Verify Sector Count Mode 3 (DWord)
290     unsigned short Write_Read_Verify_Sector_Count_Mode_3[2];
291 
292     //	212-213	Write-Read-Verify Sector Count Mode 2 (DWord)
293     unsigned short Write_Read_Verify_Sector_Count_Mode_2[2];
294 
295     //	214	NV Cache Capabilities
296     unsigned short NV_Cache_Capabilities;
297 
298     //	215-216	NV Cache Size in Logical Blocks (DWord)
299     unsigned short NV_Cache_Size[2];
300 
301     //	217	Nominal media rotation rate
302     unsigned short Nominal_media_rotation_rate;
303 
304     //	218	Reserved
305     unsigned short Reserved11;
306 
307     //	219	NV Cache Options
308     unsigned short NV_Cache_Options;
309 
310     //	220	Write-Read-Verify feature set current mode
311     unsigned short Write_Read_Verify_feature_set_current_mode;
312 
313     //	221	Reserved
314     unsigned short Reserved12;
315 
316     //	222	Transport major version number.
317     //		0000h or ffffh = device does not report version
318     unsigned short Transport_Major_Version_Number;
319 
320     //	223	Transport Minor version number
321     unsigned short Transport_Minor_Version_Number;
322 
323     //	224-233	Reserved for CE-ATA
324     unsigned short Reserved13[10];
325 
326     //	234	Minimum number of 512-byte data blocks per DOWNLOAD MICROCODE command for mode 03h
327     unsigned short Mini_blocks_per_CMD;
328 
329     //	235	Maximum number of 512-byte data blocks per DOWNLOAD MICROCODE command for mode 03h
330     unsigned short Max_blocks_per_CMD;
331 
332     //	236-254	Reserved
333     unsigned short Reserved14[19];
334 
335     //	255	Integrity word
336     //		15:8	Checksum
337     //		7:0	Checksum Validity Indicator
338     unsigned short Integrity_word;
339 } __attribute__((packed));
340 
341 /**
342  * @brief 初始化ATA磁盘驱动程序
343  *
344  */
345 void ata_init();