ata.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Most of the following information was derived from the document
  25. * "Information Technology - AT Attachment-3 Interface (ATA-3)"
  26. * which can be found at:
  27. * http://www.dt.wdc.com/ata/ata-3/ata3r5v.zip
  28. * ftp://poctok.iae.nsk.su/pub/asm/Documents/IDE/ATA3R5V.ZIP
  29. * ftp://ftp.fee.vutbr.cz/pub/doc/io/ata/ata-3/ata3r5v.zip
  30. */
  31. #ifndef _ATA_H
  32. #define _ATA_H
  33. /* Register addressing depends on the hardware design; for instance,
  34. * 8-bit (register) and 16-bit (data) accesses might use different
  35. * address spaces. This is implemented by the following definitions.
  36. */
  37. #ifndef CFG_ATA_STRIDE
  38. #define CFG_ATA_STRIDE 1
  39. #endif
  40. #define ATA_IO_DATA(x) (CFG_ATA_DATA_OFFSET+((x) * CFG_ATA_STRIDE))
  41. #define ATA_IO_REG(x) (CFG_ATA_REG_OFFSET +((x) * CFG_ATA_STRIDE))
  42. #define ATA_IO_ALT(x) (CFG_ATA_ALT_OFFSET +((x) * CFG_ATA_STRIDE))
  43. /*
  44. * I/O Register Descriptions
  45. */
  46. #define ATA_DATA_REG ATA_IO_DATA(0)
  47. #define ATA_ERROR_REG ATA_IO_REG(1)
  48. #define ATA_SECT_CNT ATA_IO_REG(2)
  49. #define ATA_SECT_NUM ATA_IO_REG(3)
  50. #define ATA_CYL_LOW ATA_IO_REG(4)
  51. #define ATA_CYL_HIGH ATA_IO_REG(5)
  52. #define ATA_DEV_HD ATA_IO_REG(6)
  53. #define ATA_COMMAND ATA_IO_REG(7)
  54. #define ATA_DATA_EVEN ATA_IO_REG(8)
  55. #define ATA_DATA_ODD ATA_IO_REG(9)
  56. #define ATA_STATUS ATA_COMMAND
  57. #define ATA_DEV_CTL ATA_IO_ALT(6)
  58. #define ATA_LBA_LOW ATA_SECT_NUM
  59. #define ATA_LBA_MID ATA_CYL_LOW
  60. #define ATA_LBA_HIGH ATA_CYL_HIGH
  61. #define ATA_LBA_SEL ATA_DEV_CTL
  62. /*
  63. * Status register bits
  64. */
  65. #define ATA_STAT_BUSY 0x80 /* Device Busy */
  66. #define ATA_STAT_READY 0x40 /* Device Ready */
  67. #define ATA_STAT_FAULT 0x20 /* Device Fault */
  68. #define ATA_STAT_SEEK 0x10 /* Device Seek Complete */
  69. #define ATA_STAT_DRQ 0x08 /* Data Request (ready) */
  70. #define ATA_STAT_CORR 0x04 /* Corrected Data Error */
  71. #define ATA_STAT_INDEX 0x02 /* Vendor specific */
  72. #define ATA_STAT_ERR 0x01 /* Error */
  73. /*
  74. * Device / Head Register Bits
  75. */
  76. #define ATA_DEVICE(x) ((x & 1)<<4)
  77. #define ATA_LBA 0xE0
  78. enum {
  79. ATA_MAX_DEVICES = 1, /* per bus/port */
  80. ATA_MAX_PRD = 256, /* we could make these 256/256 */
  81. ATA_SECT_SIZE = 256, /*256 words per sector */
  82. /* bits in ATA command block registers */
  83. ATA_HOB = (1 << 7), /* LBA48 selector */
  84. ATA_NIEN = (1 << 1), /* disable-irq flag */
  85. /*ATA_LBA = (1 << 6), */ /* LBA28 selector */
  86. ATA_DEV1 = (1 << 4), /* Select Device 1 (slave) */
  87. ATA_DEVICE_OBS = (1 << 7) | (1 << 5), /* obs bits in dev reg */
  88. ATA_DEVCTL_OBS = (1 << 3), /* obsolete bit in devctl reg */
  89. ATA_BUSY = (1 << 7), /* BSY status bit */
  90. ATA_DRDY = (1 << 6), /* device ready */
  91. ATA_DF = (1 << 5), /* device fault */
  92. ATA_DRQ = (1 << 3), /* data request i/o */
  93. ATA_ERR = (1 << 0), /* have an error */
  94. ATA_SRST = (1 << 2), /* software reset */
  95. ATA_ABORTED = (1 << 2), /* command aborted */
  96. /* ATA command block registers */
  97. ATA_REG_DATA = 0x00,
  98. ATA_REG_ERR = 0x01,
  99. ATA_REG_NSECT = 0x02,
  100. ATA_REG_LBAL = 0x03,
  101. ATA_REG_LBAM = 0x04,
  102. ATA_REG_LBAH = 0x05,
  103. ATA_REG_DEVICE = 0x06,
  104. ATA_REG_STATUS = 0x07,
  105. ATA_PCI_CTL_OFS = 0x02,
  106. /* and their aliases */
  107. ATA_REG_FEATURE = ATA_REG_ERR,
  108. ATA_REG_CMD = ATA_REG_STATUS,
  109. ATA_REG_BYTEL = ATA_REG_LBAM,
  110. ATA_REG_BYTEH = ATA_REG_LBAH,
  111. ATA_REG_DEVSEL = ATA_REG_DEVICE,
  112. ATA_REG_IRQ = ATA_REG_NSECT,
  113. /* SETFEATURES stuff */
  114. SETFEATURES_XFER = 0x03,
  115. XFER_UDMA_7 = 0x47,
  116. XFER_UDMA_6 = 0x46,
  117. XFER_UDMA_5 = 0x45,
  118. XFER_UDMA_4 = 0x44,
  119. XFER_UDMA_3 = 0x43,
  120. XFER_UDMA_2 = 0x42,
  121. XFER_UDMA_1 = 0x41,
  122. XFER_UDMA_0 = 0x40,
  123. XFER_MW_DMA_2 = 0x22,
  124. XFER_MW_DMA_1 = 0x21,
  125. XFER_MW_DMA_0 = 0x20,
  126. XFER_PIO_4 = 0x0C,
  127. XFER_PIO_3 = 0x0B,
  128. XFER_PIO_2 = 0x0A,
  129. XFER_PIO_1 = 0x09,
  130. XFER_PIO_0 = 0x08,
  131. XFER_SW_DMA_2 = 0x12,
  132. XFER_SW_DMA_1 = 0x11,
  133. XFER_SW_DMA_0 = 0x10,
  134. XFER_PIO_SLOW = 0x00
  135. };
  136. /*
  137. * ATA Commands (only mandatory commands listed here)
  138. */
  139. #define ATA_CMD_READ 0x20 /* Read Sectors (with retries) */
  140. #define ATA_CMD_READN 0x21 /* Read Sectors ( no retries) */
  141. #define ATA_CMD_WRITE 0x30 /* Write Sectores (with retries)*/
  142. #define ATA_CMD_WRITEN 0x31 /* Write Sectors ( no retries)*/
  143. #define ATA_CMD_VRFY 0x40 /* Read Verify (with retries) */
  144. #define ATA_CMD_VRFYN 0x41 /* Read verify ( no retries) */
  145. #define ATA_CMD_SEEK 0x70 /* Seek */
  146. #define ATA_CMD_DIAG 0x90 /* Execute Device Diagnostic */
  147. #define ATA_CMD_INIT 0x91 /* Initialize Device Parameters */
  148. #define ATA_CMD_RD_MULT 0xC4 /* Read Multiple */
  149. #define ATA_CMD_WR_MULT 0xC5 /* Write Multiple */
  150. #define ATA_CMD_SETMULT 0xC6 /* Set Multiple Mode */
  151. #define ATA_CMD_RD_DMA 0xC8 /* Read DMA (with retries) */
  152. #define ATA_CMD_RD_DMAN 0xC9 /* Read DMS ( no retries) */
  153. #define ATA_CMD_WR_DMA 0xCA /* Write DMA (with retries) */
  154. #define ATA_CMD_WR_DMAN 0xCB /* Write DMA ( no retires) */
  155. #define ATA_CMD_IDENT 0xEC /* Identify Device */
  156. #define ATA_CMD_SETF 0xEF /* Set Features */
  157. #define ATA_CMD_CHK_PWR 0xE5 /* Check Power Mode */
  158. #define ATA_CMD_READ_EXT 0x24 /* Read Sectors (with retries) with 48bit addressing */
  159. #define ATA_CMD_WRITE_EXT 0x34 /* Write Sectores (with retries) with 48bit addressing */
  160. #define ATA_CMD_VRFY_EXT 0x42 /* Read Verify (with retries) with 48bit addressing */
  161. /*
  162. * ATAPI Commands
  163. */
  164. #define ATAPI_CMD_IDENT 0xA1 /* Identify AT Atachment Packed Interface Device */
  165. #define ATAPI_CMD_PACKET 0xA0 /* Packed Command */
  166. #define ATAPI_CMD_INQUIRY 0x12
  167. #define ATAPI_CMD_REQ_SENSE 0x03
  168. #define ATAPI_CMD_READ_CAP 0x25
  169. #define ATAPI_CMD_START_STOP 0x1B
  170. #define ATAPI_CMD_READ_12 0xA8
  171. #define ATA_GET_ERR() inb(ATA_STATUS)
  172. #define ATA_GET_STAT() inb(ATA_STATUS)
  173. #define ATA_OK_STAT(stat,good,bad) (((stat)&((good)|(bad)))==(good))
  174. #define ATA_BAD_R_STAT (ATA_STAT_BUSY | ATA_STAT_ERR)
  175. #define ATA_BAD_W_STAT (ATA_BAD_R_STAT | ATA_STAT_FAULT)
  176. #define ATA_BAD_STAT (ATA_BAD_R_STAT | ATA_STAT_DRQ)
  177. #define ATA_DRIVE_READY (ATA_READY_STAT | ATA_STAT_SEEK)
  178. #define ATA_DATA_READY (ATA_STAT_DRQ)
  179. #define ATA_BLOCKSIZE 512 /* bytes */
  180. #define ATA_BLOCKSHIFT 9 /* 2 ^ ATA_BLOCKSIZESHIFT = 512 */
  181. #define ATA_SECTORWORDS (512 / sizeof(unsigned long))
  182. #ifndef ATA_RESET_TIME
  183. #define ATA_RESET_TIME 60 /* spec allows up to 31 seconds */
  184. #endif
  185. /* ------------------------------------------------------------------------- */
  186. /*
  187. * structure returned by ATA_CMD_IDENT, as per ANSI ATA2 rev.2f spec
  188. */
  189. typedef struct hd_driveid {
  190. unsigned short config; /* lots of obsolete bit flags */
  191. unsigned short cyls; /* "physical" cyls */
  192. unsigned short reserved2; /* reserved (word 2) */
  193. unsigned short heads; /* "physical" heads */
  194. unsigned short track_bytes; /* unformatted bytes per track */
  195. unsigned short sector_bytes; /* unformatted bytes per sector */
  196. unsigned short sectors; /* "physical" sectors per track */
  197. unsigned short vendor0; /* vendor unique */
  198. unsigned short vendor1; /* vendor unique */
  199. unsigned short vendor2; /* vendor unique */
  200. unsigned char serial_no[20]; /* 0 = not_specified */
  201. unsigned short buf_type;
  202. unsigned short buf_size; /* 512 byte increments; 0 = not_specified */
  203. unsigned short ecc_bytes; /* for r/w long cmds; 0 = not_specified */
  204. unsigned char fw_rev[8]; /* 0 = not_specified */
  205. unsigned char model[40]; /* 0 = not_specified */
  206. unsigned char max_multsect; /* 0=not_implemented */
  207. unsigned char vendor3; /* vendor unique */
  208. unsigned short dword_io; /* 0=not_implemented; 1=implemented */
  209. unsigned char vendor4; /* vendor unique */
  210. unsigned char capability; /* bits 0:DMA 1:LBA 2:IORDYsw 3:IORDYsup*/
  211. unsigned short reserved50; /* reserved (word 50) */
  212. unsigned char vendor5; /* vendor unique */
  213. unsigned char tPIO; /* 0=slow, 1=medium, 2=fast */
  214. unsigned char vendor6; /* vendor unique */
  215. unsigned char tDMA; /* 0=slow, 1=medium, 2=fast */
  216. unsigned short field_valid; /* bits 0:cur_ok 1:eide_ok */
  217. unsigned short cur_cyls; /* logical cylinders */
  218. unsigned short cur_heads; /* logical heads */
  219. unsigned short cur_sectors; /* logical sectors per track */
  220. unsigned short cur_capacity0; /* logical total sectors on drive */
  221. unsigned short cur_capacity1; /* (2 words, misaligned int) */
  222. unsigned char multsect; /* current multiple sector count */
  223. unsigned char multsect_valid; /* when (bit0==1) multsect is ok */
  224. unsigned int lba_capacity; /* total number of sectors */
  225. unsigned short dma_1word; /* single-word dma info */
  226. unsigned short dma_mword; /* multiple-word dma info */
  227. unsigned short eide_pio_modes; /* bits 0:mode3 1:mode4 */
  228. unsigned short eide_dma_min; /* min mword dma cycle time (ns) */
  229. unsigned short eide_dma_time; /* recommended mword dma cycle time (ns) */
  230. unsigned short eide_pio; /* min cycle time (ns), no IORDY */
  231. unsigned short eide_pio_iordy; /* min cycle time (ns), with IORDY */
  232. unsigned short words69_70[2]; /* reserved words 69-70 */
  233. unsigned short words71_74[4]; /* reserved words 71-74 */
  234. unsigned short queue_depth; /* */
  235. unsigned short words76_79[4]; /* reserved words 76-79 */
  236. unsigned short major_rev_num; /* */
  237. unsigned short minor_rev_num; /* */
  238. unsigned short command_set_1; /* bits 0:Smart 1:Security 2:Removable 3:PM */
  239. unsigned short command_set_2; /* bits 14:Smart Enabled 13:0 zero 10:lba48 support*/
  240. unsigned short cfsse; /* command set-feature supported extensions */
  241. unsigned short cfs_enable_1; /* command set-feature enabled */
  242. unsigned short cfs_enable_2; /* command set-feature enabled */
  243. unsigned short csf_default; /* command set-feature default */
  244. unsigned short dma_ultra; /* */
  245. unsigned short word89; /* reserved (word 89) */
  246. unsigned short word90; /* reserved (word 90) */
  247. unsigned short CurAPMvalues; /* current APM values */
  248. unsigned short word92; /* reserved (word 92) */
  249. unsigned short hw_config; /* hardware config */
  250. unsigned short words94_99[6];/* reserved words 94-99 */
  251. /*unsigned long long lba48_capacity; /--* 4 16bit values containing lba 48 total number of sectors */
  252. unsigned short lba48_capacity[4]; /* 4 16bit values containing lba 48 total number of sectors */
  253. unsigned short words104_125[22];/* reserved words 104-125 */
  254. unsigned short last_lun; /* reserved (word 126) */
  255. unsigned short word127; /* reserved (word 127) */
  256. unsigned short dlf; /* device lock function
  257. * 15:9 reserved
  258. * 8 security level 1:max 0:high
  259. * 7:6 reserved
  260. * 5 enhanced erase
  261. * 4 expire
  262. * 3 frozen
  263. * 2 locked
  264. * 1 en/disabled
  265. * 0 capability
  266. */
  267. unsigned short csfo; /* current set features options
  268. * 15:4 reserved
  269. * 3 auto reassign
  270. * 2 reverting
  271. * 1 read-look-ahead
  272. * 0 write cache
  273. */
  274. unsigned short words130_155[26];/* reserved vendor words 130-155 */
  275. unsigned short word156;
  276. unsigned short words157_159[3];/* reserved vendor words 157-159 */
  277. unsigned short words160_255[95];/* reserved words 160-255 */
  278. } hd_driveid_t;
  279. /*
  280. * PIO Mode Configuration
  281. *
  282. * See ATA-3 (AT Attachment-3 Interface) documentation, Figure 14 / Table 21
  283. */
  284. typedef struct {
  285. unsigned int t_setup; /* Setup Time in [ns] or clocks */
  286. unsigned int t_length; /* Length Time in [ns] or clocks */
  287. unsigned int t_hold; /* Hold Time in [ns] or clocks */
  288. }
  289. pio_config_t;
  290. #define IDE_MAX_PIO_MODE 4 /* max suppurted PIO mode */
  291. /* ------------------------------------------------------------------------- */
  292. #endif /* _ATA_H */