1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2000
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 /*
8  * Most of the following information was derived from the document
9  * "Information Technology - AT Attachment-3 Interface (ATA-3)",
10  * ANSI X3.298-1997.
11  */
12 
13 #ifndef	_ATA_H
14 #define _ATA_H
15 
16 #include <libata.h>
17 
18 /* Register addressing depends on the hardware design; for instance,
19  * 8-bit (register) and 16-bit (data) accesses might use different
20  * address spaces. This is implemented by the following definitions.
21  */
22 #ifndef CONFIG_SYS_ATA_STRIDE
23 #define CONFIG_SYS_ATA_STRIDE	1
24 #endif
25 
26 #define ATA_IO_DATA(x)	(CONFIG_SYS_ATA_DATA_OFFSET+((x) * CONFIG_SYS_ATA_STRIDE))
27 #define ATA_IO_REG(x)	(CONFIG_SYS_ATA_REG_OFFSET +((x) * CONFIG_SYS_ATA_STRIDE))
28 #define ATA_IO_ALT(x)	(CONFIG_SYS_ATA_ALT_OFFSET +((x) * CONFIG_SYS_ATA_STRIDE))
29 
30 /*
31  * I/O Register Descriptions
32  */
33 #define ATA_DATA_REG	ATA_IO_DATA(0)
34 #define ATA_ERROR_REG	ATA_IO_REG(1)
35 #define ATA_SECT_CNT	ATA_IO_REG(2)
36 #define ATA_SECT_NUM	ATA_IO_REG(3)
37 #define ATA_CYL_LOW	ATA_IO_REG(4)
38 #define ATA_CYL_HIGH	ATA_IO_REG(5)
39 #define ATA_DEV_HD	ATA_IO_REG(6)
40 #define ATA_COMMAND	ATA_IO_REG(7)
41 #define ATA_DATA_EVEN	ATA_IO_REG(8)
42 #define ATA_DATA_ODD	ATA_IO_REG(9)
43 #define ATA_STATUS	ATA_COMMAND
44 #define ATA_DEV_CTL	ATA_IO_ALT(6)
45 #define ATA_LBA_LOW	ATA_SECT_NUM
46 #define ATA_LBA_MID	ATA_CYL_LOW
47 #define ATA_LBA_HIGH	ATA_CYL_HIGH
48 #define ATA_LBA_SEL	ATA_DEV_CTL
49 
50 /*
51  * Status register bits
52  */
53 #define ATA_STAT_BUSY	0x80	/* Device Busy			*/
54 #define ATA_STAT_READY	0x40	/* Device Ready			*/
55 #define ATA_STAT_FAULT	0x20	/* Device Fault			*/
56 #define ATA_STAT_SEEK	0x10	/* Device Seek Complete		*/
57 #define ATA_STAT_DRQ	0x08	/* Data Request (ready)		*/
58 #define ATA_STAT_CORR	0x04	/* Corrected Data Error		*/
59 #define ATA_STAT_INDEX	0x02	/* Vendor specific		*/
60 #define ATA_STAT_ERR	0x01	/* Error			*/
61 
62 /*
63  * Device / Head Register Bits
64  */
65 #ifndef ATA_DEVICE
66 #define ATA_DEVICE(x)	((x & 1)<<4)
67 #endif /* ATA_DEVICE */
68 #define ATA_LBA		0xE0
69 
70 /*
71  * ATAPI Commands
72  */
73 #define ATAPI_CMD_INQUIRY 0x12
74 #define ATAPI_CMD_REQ_SENSE 0x03
75 #define ATAPI_CMD_READ_CAP 0x25
76 #define ATAPI_CMD_START_STOP 0x1B
77 #define ATAPI_CMD_READ_12 0xA8
78 
79 
80 #define ATA_GET_ERR()	inb(ATA_STATUS)
81 #define ATA_GET_STAT()	inb(ATA_STATUS)
82 #define ATA_OK_STAT(stat,good,bad)	(((stat)&((good)|(bad)))==(good))
83 #define ATA_BAD_R_STAT	(ATA_STAT_BUSY	| ATA_STAT_ERR)
84 #define ATA_BAD_W_STAT	(ATA_BAD_R_STAT	| ATA_STAT_FAULT)
85 #define ATA_BAD_STAT	(ATA_BAD_R_STAT	| ATA_STAT_DRQ)
86 #define ATA_DRIVE_READY	(ATA_READY_STAT	| ATA_STAT_SEEK)
87 #define ATA_DATA_READY	(ATA_STAT_DRQ)
88 
89 #define ATA_BLOCKSIZE	512	/* bytes */
90 #define ATA_BLOCKSHIFT	9	/* 2 ^ ATA_BLOCKSIZESHIFT = 512 */
91 #define ATA_SECTORWORDS	(512 / sizeof(uint32_t))
92 
93 #ifndef ATA_RESET_TIME
94 #define ATA_RESET_TIME	60	/* spec allows up to 31 seconds */
95 #endif
96 
97 /* ------------------------------------------------------------------------- */
98 
99 /*
100  * structure returned by ATA_CMD_IDENT, as per ANSI ATA2 rev.2f spec
101  */
102 typedef struct hd_driveid {
103 	unsigned short	config;		/* lots of obsolete bit flags */
104 	unsigned short	cyls;		/* "physical" cyls */
105 	unsigned short	reserved2;	/* reserved (word 2) */
106 	unsigned short	heads;		/* "physical" heads */
107 	unsigned short	track_bytes;	/* unformatted bytes per track */
108 	unsigned short	sector_bytes;	/* unformatted bytes per sector */
109 	unsigned short	sectors;	/* "physical" sectors per track */
110 	unsigned short	vendor0;	/* vendor unique */
111 	unsigned short	vendor1;	/* vendor unique */
112 	unsigned short	vendor2;	/* vendor unique */
113 	unsigned char	serial_no[20];	/* 0 = not_specified */
114 	unsigned short	buf_type;
115 	unsigned short	buf_size;	/* 512 byte increments; 0 = not_specified */
116 	unsigned short	ecc_bytes;	/* for r/w long cmds; 0 = not_specified */
117 	unsigned char	fw_rev[8];	/* 0 = not_specified */
118 	unsigned char	model[40];	/* 0 = not_specified */
119 	unsigned char	max_multsect;	/* 0=not_implemented */
120 	unsigned char	vendor3;	/* vendor unique */
121 	unsigned short	dword_io;	/* 0=not_implemented; 1=implemented */
122 	unsigned char	vendor4;	/* vendor unique */
123 	unsigned char	capability;	/* bits 0:DMA 1:LBA 2:IORDYsw 3:IORDYsup*/
124 	unsigned short	reserved50;	/* reserved (word 50) */
125 	unsigned char	vendor5;	/* vendor unique */
126 	unsigned char	tPIO;		/* 0=slow, 1=medium, 2=fast */
127 	unsigned char	vendor6;	/* vendor unique */
128 	unsigned char	tDMA;		/* 0=slow, 1=medium, 2=fast */
129 	unsigned short	field_valid;	/* bits 0:cur_ok 1:eide_ok */
130 	unsigned short	cur_cyls;	/* logical cylinders */
131 	unsigned short	cur_heads;	/* logical heads */
132 	unsigned short	cur_sectors;	/* logical sectors per track */
133 	unsigned short	cur_capacity0;	/* logical total sectors on drive */
134 	unsigned short	cur_capacity1;	/*  (2 words, misaligned int)     */
135 	unsigned char	multsect;	/* current multiple sector count */
136 	unsigned char	multsect_valid;	/* when (bit0==1) multsect is ok */
137 	unsigned short  lba_capacity[2];/* two words containing total number of sectors */
138 	unsigned short	dma_1word;	/* single-word dma info */
139 	unsigned short	dma_mword;	/* multiple-word dma info */
140 	unsigned short  eide_pio_modes; /* bits 0:mode3 1:mode4 */
141 	unsigned short  eide_dma_min;	/* min mword dma cycle time (ns) */
142 	unsigned short  eide_dma_time;	/* recommended mword dma cycle time (ns) */
143 	unsigned short  eide_pio;       /* min cycle time (ns), no IORDY  */
144 	unsigned short  eide_pio_iordy; /* min cycle time (ns), with IORDY */
145 	unsigned short	words69_70[2];	/* reserved words 69-70 */
146 	unsigned short	words71_74[4];	/* reserved words 71-74 */
147 	unsigned short  queue_depth;	/*  */
148 	unsigned short  words76_79[4];	/* reserved words 76-79 */
149 	unsigned short  major_rev_num;	/*  */
150 	unsigned short  minor_rev_num;	/*  */
151 	unsigned short  command_set_1;	/* bits 0:Smart 1:Security 2:Removable 3:PM */
152 	unsigned short	command_set_2;	/* bits 14:Smart Enabled 13:0 zero 10:lba48 support*/
153 	unsigned short  cfsse;		/* command set-feature supported extensions */
154 	unsigned short  cfs_enable_1;	/* command set-feature enabled */
155 	unsigned short  cfs_enable_2;	/* command set-feature enabled */
156 	unsigned short  csf_default;	/* command set-feature default */
157 	unsigned short  dma_ultra;	/*  */
158 	unsigned short	word89;		/* reserved (word 89) */
159 	unsigned short	word90;		/* reserved (word 90) */
160 	unsigned short	CurAPMvalues;	/* current APM values */
161 	unsigned short	word92;		/* reserved (word 92) */
162 	unsigned short	hw_config;	/* hardware config */
163 	unsigned short	words94_99[6];/* reserved words 94-99 */
164 	/*unsigned long long  lba48_capacity; /--* 4 16bit values containing lba 48 total number of sectors */
165 	unsigned short	lba48_capacity[4]; /* 4 16bit values containing lba 48 total number of sectors */
166 	unsigned short	words104_125[22];/* reserved words 104-125 */
167 	unsigned short	last_lun;	/* reserved (word 126) */
168 	unsigned short	word127;	/* reserved (word 127) */
169 	unsigned short	dlf;		/* device lock function
170 					 * 15:9	reserved
171 					 * 8	security level 1:max 0:high
172 					 * 7:6	reserved
173 					 * 5	enhanced erase
174 					 * 4	expire
175 					 * 3	frozen
176 					 * 2	locked
177 					 * 1	en/disabled
178 					 * 0	capability
179 					 */
180 	unsigned short  csfo;		/* current set features options
181 					 * 15:4	reserved
182 					 * 3	auto reassign
183 					 * 2	reverting
184 					 * 1	read-look-ahead
185 					 * 0	write cache
186 					 */
187 	unsigned short	words130_155[26];/* reserved vendor words 130-155 */
188 	unsigned short	word156;
189 	unsigned short	words157_159[3];/* reserved vendor words 157-159 */
190 	unsigned short	words160_162[3];/* reserved words 160-162 */
191 	unsigned short	cf_advanced_caps;
192 	unsigned short	words164_255[92];/* reserved words 164-255 */
193 } hd_driveid_t;
194 
195 
196 /*
197  * PIO Mode Configuration
198  *
199  * See ATA-3 (AT Attachment-3 Interface) documentation, Figure 14 / Table 21
200  */
201 
202 typedef struct {
203 	unsigned int	t_setup;	/* Setup  Time in [ns] or clocks	*/
204 	unsigned int	t_length;	/* Length Time in [ns] or clocks	*/
205 	unsigned int	t_hold;		/* Hold   Time in [ns] or clocks	*/
206 }
207 pio_config_t;
208 
209 #define	IDE_MAX_PIO_MODE	4	/* max suppurted PIO mode		*/
210 
211 /* ------------------------------------------------------------------------- */
212 
213 #endif /* _ATA_H */
214