2009-07-20 Joe Auricchio <jauricchio@gmail.com>
[grub2/phcoder.git] / include / grub / ata.h
blobaaa2e147a498531886985bd6c6e287b8508ba3cb
1 /* ata.h - ATA disk access. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef GRUB_ATA_HEADER
21 #define GRUB_ATA_HEADER 1
23 #include <grub/misc.h>
24 #include <grub/symbol.h>
25 /* XXX: For now this only works on i386. */
26 #include <grub/cpu/io.h>
28 typedef enum
30 GRUB_ATA_CHS,
31 GRUB_ATA_LBA,
32 GRUB_ATA_LBA48
33 } grub_ata_addressing_t;
35 #define GRUB_ATA_REG_DATA 0
36 #define GRUB_ATA_REG_ERROR 1
37 #define GRUB_ATA_REG_FEATURES 1
38 #define GRUB_ATA_REG_SECTORS 2
39 #define GRUB_ATAPI_REG_IREASON 2
40 #define GRUB_ATA_REG_SECTNUM 3
41 #define GRUB_ATA_REG_CYLLSB 4
42 #define GRUB_ATA_REG_CYLMSB 5
43 #define GRUB_ATA_REG_LBALOW 3
44 #define GRUB_ATA_REG_LBAMID 4
45 #define GRUB_ATAPI_REG_CNTLOW 4
46 #define GRUB_ATA_REG_LBAHIGH 5
47 #define GRUB_ATAPI_REG_CNTHIGH 5
48 #define GRUB_ATA_REG_DISK 6
49 #define GRUB_ATA_REG_CMD 7
50 #define GRUB_ATA_REG_STATUS 7
52 #define GRUB_ATA_REG2_CONTROL 0
54 #define GRUB_ATA_STATUS_ERR 0x01
55 #define GRUB_ATA_STATUS_INDEX 0x02
56 #define GRUB_ATA_STATUS_ECC 0x04
57 #define GRUB_ATA_STATUS_DRQ 0x08
58 #define GRUB_ATA_STATUS_SEEK 0x10
59 #define GRUB_ATA_STATUS_WRERR 0x20
60 #define GRUB_ATA_STATUS_READY 0x40
61 #define GRUB_ATA_STATUS_BUSY 0x80
63 /* ATAPI interrupt reason values (I/O, D/C bits). */
64 #define GRUB_ATAPI_IREASON_MASK 0x3
65 #define GRUB_ATAPI_IREASON_DATA_OUT 0x0
66 #define GRUB_ATAPI_IREASON_CMD_OUT 0x1
67 #define GRUB_ATAPI_IREASON_DATA_IN 0x2
68 #define GRUB_ATAPI_IREASON_ERROR 0x3
70 enum grub_ata_commands
72 GRUB_ATA_CMD_CHECK_POWER_MODE = 0xe5,
73 GRUB_ATA_CMD_IDENTIFY_DEVICE = 0xec,
74 GRUB_ATA_CMD_IDENTIFY_PACKET_DEVICE = 0xa1,
75 GRUB_ATA_CMD_IDLE = 0xe3,
76 GRUB_ATA_CMD_PACKET = 0xa0,
77 GRUB_ATA_CMD_READ_SECTORS = 0x20,
78 GRUB_ATA_CMD_READ_SECTORS_EXT = 0x24,
79 GRUB_ATA_CMD_SECURITY_FREEZE_LOCK = 0xf5,
80 GRUB_ATA_CMD_SET_FEATURES = 0xef,
81 GRUB_ATA_CMD_SLEEP = 0xe6,
82 GRUB_ATA_CMD_SMART = 0xb0,
83 GRUB_ATA_CMD_STANDBY_IMMEDIATE = 0xe0,
84 GRUB_ATA_CMD_WRITE_SECTORS = 0x30,
85 GRUB_ATA_CMD_WRITE_SECTORS_EXT = 0x34,
88 enum grub_ata_timeout_milliseconds
90 GRUB_ATA_TOUT_STD = 1000, /* 1s standard timeout. */
91 GRUB_ATA_TOUT_DATA = 10000 /* 10s DATA I/O timeout. */
94 struct grub_ata_device
96 /* IDE port to use. */
97 int port;
99 /* IO addresses on which the registers for this device can be
100 found. */
101 int ioaddress;
102 int ioaddress2;
104 /* Two devices can be connected to a single cable. Use this field
105 to select device 0 (commonly known as "master") or device 1
106 (commonly known as "slave"). */
107 int device;
109 /* Addressing methods available for accessing this device. If CHS
110 is only available, use that. Otherwise use LBA, except for the
111 high sectors. In that case use LBA48. */
112 grub_ata_addressing_t addr;
114 /* Sector count. */
115 grub_uint64_t size;
117 /* CHS maximums. */
118 grub_uint16_t cylinders;
119 grub_uint16_t heads;
120 grub_uint16_t sectors_per_track;
122 /* Set to 0 for ATA, set to 1 for ATAPI. */
123 int atapi;
125 struct grub_ata_device *next;
128 grub_err_t EXPORT_FUNC(grub_ata_wait_not_busy) (struct grub_ata_device *dev,
129 int milliseconds);
130 grub_err_t EXPORT_FUNC(grub_ata_wait_drq) (struct grub_ata_device *dev,
131 int rw, int milliseconds);
132 void EXPORT_FUNC(grub_ata_pio_read) (struct grub_ata_device *dev,
133 char *buf, grub_size_t size);
135 static inline void
136 grub_ata_regset (struct grub_ata_device *dev, int reg, int val)
138 grub_outb (val, dev->ioaddress + reg);
141 static inline grub_uint8_t
142 grub_ata_regget (struct grub_ata_device *dev, int reg)
144 return grub_inb (dev->ioaddress + reg);
147 static inline void
148 grub_ata_regset2 (struct grub_ata_device *dev, int reg, int val)
150 grub_outb (val, dev->ioaddress2 + reg);
153 static inline grub_uint8_t
154 grub_ata_regget2 (struct grub_ata_device *dev, int reg)
156 return grub_inb (dev->ioaddress2 + reg);
159 static inline grub_err_t
160 grub_ata_check_ready (struct grub_ata_device *dev)
162 if (grub_ata_regget (dev, GRUB_ATA_REG_STATUS) & GRUB_ATA_STATUS_BUSY)
163 return grub_ata_wait_not_busy (dev, GRUB_ATA_TOUT_STD);
165 return GRUB_ERR_NONE;
168 #endif /* ! GRUB_ATA_HEADER */