Kevin O'Connor | c09492e | 2008-03-01 13:38:07 -0500 | [diff] [blame] | 1 | // Low level ATA disk access |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
| 6 | // This file may be distributed under the terms of the GNU GPLv3 license. |
| 7 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 8 | #include "ata.h" // ATA_* |
| 9 | #include "types.h" // u8 |
| 10 | #include "ioport.h" // inb |
| 11 | #include "util.h" // BX_INFO |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 12 | #include "cmos.h" // inb_cmos |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 13 | |
| 14 | #define TIMEOUT 0 |
| 15 | #define BSY 1 |
| 16 | #define NOT_BSY 2 |
| 17 | #define NOT_BSY_DRQ 3 |
| 18 | #define NOT_BSY_NOT_DRQ 4 |
| 19 | #define NOT_BSY_RDY 5 |
| 20 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 21 | #define IDE_SECTOR_SIZE 512 |
| 22 | #define CDROM_SECTOR_SIZE 2048 |
| 23 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 24 | #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops |
| 25 | |
Kevin O'Connor | 127cbd7 | 2008-03-08 11:34:28 -0500 | [diff] [blame] | 26 | #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args) |
| 27 | #define DEBUGF(fmt, args...) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 28 | |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 29 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 30 | /**************************************************************** |
| 31 | * Helper functions |
| 32 | ****************************************************************/ |
| 33 | |
| 34 | // Wait for the specified ide state |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 35 | static int |
| 36 | await_ide(u8 when_done, u16 base, u16 timeout) |
| 37 | { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 38 | u32 time=0, last=0; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 39 | for (;;) { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 40 | u8 status = inb(base+ATA_CB_STAT); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 41 | time++; |
Kevin O'Connor | 117fc21 | 2008-04-13 18:17:02 -0400 | [diff] [blame] | 42 | u8 result = 0; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 43 | if (when_done == BSY) |
| 44 | result = status & ATA_CB_STAT_BSY; |
| 45 | else if (when_done == NOT_BSY) |
| 46 | result = !(status & ATA_CB_STAT_BSY); |
| 47 | else if (when_done == NOT_BSY_DRQ) |
| 48 | result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ); |
| 49 | else if (when_done == NOT_BSY_NOT_DRQ) |
| 50 | result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ); |
| 51 | else if (when_done == NOT_BSY_RDY) |
| 52 | result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 53 | |
| 54 | if (result) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 55 | return status; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 56 | // mod 2048 each 16 ms |
| 57 | if (time>>16 != last) { |
| 58 | last = time >>16; |
Kevin O'Connor | 127cbd7 | 2008-03-08 11:34:28 -0500 | [diff] [blame] | 59 | DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)" |
| 60 | " %d time= %d timeout= %d\n" |
| 61 | , when_done, time>>11, timeout); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 62 | } |
| 63 | if (status & ATA_CB_STAT_ERR) { |
Kevin O'Connor | 127cbd7 | 2008-03-08 11:34:28 -0500 | [diff] [blame] | 64 | DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ" |
| 65 | ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n" |
| 66 | , when_done, time>>11, timeout); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 67 | return -1; |
| 68 | } |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 69 | if (timeout == 0 || (time>>11) > timeout) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 70 | break; |
| 71 | } |
| 72 | BX_INFO("IDE time out\n"); |
| 73 | return -1; |
| 74 | } |
| 75 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 76 | // Wait for ide state - pauses for one ata cycle first. |
Kevin O'Connor | 74f9c08 | 2008-04-13 16:57:16 -0400 | [diff] [blame] | 77 | static __always_inline int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 78 | pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout) |
| 79 | { |
| 80 | // Wait one PIO transfer cycle. |
| 81 | inb(iobase2 + ATA_CB_ASTAT); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 82 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 83 | return await_ide(when_done, iobase1, timeout); |
| 84 | } |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 85 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 86 | // Delay for x nanoseconds |
| 87 | static void |
| 88 | nsleep(u32 delay) |
| 89 | { |
| 90 | // XXX - how to implement ndelay? |
| 91 | while (delay--) |
| 92 | nop(); |
| 93 | } |
| 94 | |
| 95 | // Reset a drive |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 96 | void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 97 | ata_reset(int driveid) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 98 | { |
| 99 | u16 iobase1, iobase2; |
| 100 | u8 channel, slave, sn, sc; |
| 101 | u8 type; |
| 102 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 103 | channel = driveid / 2; |
| 104 | slave = driveid % 2; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 105 | |
| 106 | iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 107 | iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
| 108 | |
| 109 | // Reset |
| 110 | |
| 111 | // 8.2.1 (a) -- set SRST in DC |
| 112 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC); |
| 113 | |
| 114 | // 8.2.1 (b) -- wait for BSY |
| 115 | await_ide(BSY, iobase1, 20); |
| 116 | |
| 117 | // 8.2.1 (f) -- clear SRST |
| 118 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC); |
| 119 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 120 | type=GET_EBDA(ata.devices[driveid].type); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 121 | if (type != ATA_TYPE_NONE) { |
| 122 | |
| 123 | // 8.2.1 (g) -- check for sc==sn==0x01 |
| 124 | // select device |
| 125 | outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH); |
| 126 | sc = inb(iobase1+ATA_CB_SC); |
| 127 | sn = inb(iobase1+ATA_CB_SN); |
| 128 | |
| 129 | if ( (sc==0x01) && (sn==0x01) ) { |
| 130 | if (type == ATA_TYPE_ATA) //ATA |
| 131 | await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT); |
| 132 | else //ATAPI |
| 133 | await_ide(NOT_BSY, iobase1, IDE_TIMEOUT); |
| 134 | } |
| 135 | |
| 136 | // 8.2.1 (h) -- wait for not BSY |
| 137 | await_ide(NOT_BSY, iobase1, IDE_TIMEOUT); |
| 138 | } |
| 139 | |
| 140 | // Enable interrupts |
| 141 | outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); |
| 142 | } |
| 143 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 144 | |
| 145 | /**************************************************************** |
| 146 | * ATA send command |
| 147 | ****************************************************************/ |
| 148 | |
| 149 | struct ata_op_s { |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 150 | u64 lba; |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 151 | void *far_buffer; |
| 152 | u16 driveid; |
| 153 | u16 count; |
| 154 | }; |
| 155 | |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 156 | struct ata_pio_command { |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 157 | u8 feature; |
| 158 | u8 sector_count; |
| 159 | u8 lba_low; |
| 160 | u8 lba_mid; |
| 161 | u8 lba_high; |
| 162 | u8 device; |
| 163 | u8 command; |
| 164 | |
| 165 | u8 sector_count2; |
| 166 | u8 lba_low2; |
| 167 | u8 lba_mid2; |
| 168 | u8 lba_high2; |
| 169 | }; |
| 170 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 171 | // Send an ata command to the drive. |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 172 | static int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 173 | send_cmd(int driveid, struct ata_pio_command *cmd) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 174 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 175 | u8 channel = driveid / 2; |
Kevin O'Connor | 5b15fbf | 2008-03-08 23:20:41 -0500 | [diff] [blame] | 176 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 177 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 178 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 179 | int status = inb(iobase1 + ATA_CB_STAT); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 180 | if (status & ATA_CB_STAT_BSY) |
| 181 | return 1; |
| 182 | |
| 183 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 184 | if (cmd->command & 0x04) { |
| 185 | outb(0x00, iobase1 + ATA_CB_FR); |
| 186 | outb(cmd->sector_count2, iobase1 + ATA_CB_SC); |
| 187 | outb(cmd->lba_low2, iobase1 + ATA_CB_SN); |
| 188 | outb(cmd->lba_mid2, iobase1 + ATA_CB_CL); |
| 189 | outb(cmd->lba_high2, iobase1 + ATA_CB_CH); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 190 | } |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 191 | outb(cmd->feature, iobase1 + ATA_CB_FR); |
| 192 | outb(cmd->sector_count, iobase1 + ATA_CB_SC); |
| 193 | outb(cmd->lba_low, iobase1 + ATA_CB_SN); |
| 194 | outb(cmd->lba_mid, iobase1 + ATA_CB_CL); |
| 195 | outb(cmd->lba_high, iobase1 + ATA_CB_CH); |
| 196 | outb(cmd->device, iobase1 + ATA_CB_DH); |
| 197 | outb(cmd->command, iobase1 + ATA_CB_CMD); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 198 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 199 | nsleep(400); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 200 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 201 | status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT); |
| 202 | if (status < 0) |
| 203 | return status; |
| 204 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 205 | if (status & ATA_CB_STAT_ERR) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 206 | DEBUGF("send_cmd : read error\n"); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 207 | return 2; |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 208 | } |
| 209 | if (!(status & ATA_CB_STAT_DRQ)) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 210 | DEBUGF("send_cmd : DRQ not set (status %02x)\n" |
Kevin O'Connor | 127cbd7 | 2008-03-08 11:34:28 -0500 | [diff] [blame] | 211 | , (unsigned) status); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 212 | return 3; |
| 213 | } |
| 214 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 218 | |
| 219 | /**************************************************************** |
| 220 | * ATA transfers |
| 221 | ****************************************************************/ |
| 222 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 223 | // Read and discard x number of bytes from an io channel. |
| 224 | static void |
| 225 | insx_discard(int mode, int iobase1, int bytes) |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 226 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 227 | int count, i; |
| 228 | if (mode == ATA_MODE_PIO32) { |
| 229 | count = bytes / 4; |
| 230 | for (i=0; i<count; i++) |
| 231 | inl(iobase1); |
| 232 | } else { |
| 233 | count = bytes / 2; |
| 234 | for (i=0; i<count; i++) |
| 235 | inw(iobase1); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive |
| 240 | // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first |
| 241 | // and/or last block may be partially transferred. This function is |
| 242 | // inlined because all the callers use different forms and because the |
| 243 | // large number of parameters would consume a lot of stack space. |
Kevin O'Connor | 74f9c08 | 2008-04-13 16:57:16 -0400 | [diff] [blame] | 244 | static __always_inline int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 245 | ata_transfer(int driveid, int iswrite, int count, int blocksize |
| 246 | , int skipfirst, int skiplast, void *far_buffer) |
| 247 | { |
| 248 | DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d" |
| 249 | " skipf=%d skipl=%d buf=%p\n" |
| 250 | , driveid, iswrite, count, blocksize |
| 251 | , skipfirst, skiplast, far_buffer); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 252 | |
| 253 | // Reset count of transferred data |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 254 | SET_EBDA(ata.trsfsectors, 0); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 255 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 256 | u8 channel = driveid / 2; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 257 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 258 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 259 | u8 mode = GET_EBDA(ata.devices[driveid].mode); |
| 260 | int current = 0; |
| 261 | int status; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 262 | for (;;) { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 263 | int bsize = blocksize; |
| 264 | if (skipfirst && current == 0) { |
| 265 | insx_discard(mode, iobase1, skipfirst); |
| 266 | bsize -= skipfirst; |
| 267 | } |
| 268 | if (skiplast && current == count-1) |
| 269 | bsize -= skiplast; |
| 270 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 271 | if (iswrite) { |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 272 | // Write data to controller |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 273 | DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 274 | if (mode == ATA_MODE_PIO32) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 275 | outsl_far(iobase1, far_buffer, bsize / 4); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 276 | else |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 277 | outsw_far(iobase1, far_buffer, bsize / 2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 278 | } else { |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 279 | // Read data from controller |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 280 | DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 281 | if (mode == ATA_MODE_PIO32) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 282 | insl_far(iobase1, far_buffer, bsize / 4); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 283 | else |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 284 | insw_far(iobase1, far_buffer, bsize / 2); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 285 | } |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 286 | far_buffer += bsize; |
| 287 | |
| 288 | if (skiplast && current == count-1) |
| 289 | insx_discard(mode, iobase1, skiplast); |
| 290 | |
| 291 | status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT); |
| 292 | if (status < 0) |
| 293 | // Error |
| 294 | return status; |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 295 | |
| 296 | current++; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 297 | SET_EBDA(ata.trsfsectors, current); |
| 298 | if (current == count) |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 299 | break; |
| 300 | status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ |
| 301 | | ATA_CB_STAT_ERR); |
| 302 | if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 303 | DEBUGF("ata_transfer : more sectors left (status %02x)\n" |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 304 | , (unsigned) status); |
| 305 | return 5; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 306 | } |
| 307 | } |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 308 | |
| 309 | status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF |
| 310 | | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 311 | if (!iswrite) |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 312 | status &= ~ATA_CB_STAT_DF; |
| 313 | if (status != ATA_CB_STAT_RDY ) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 314 | DEBUGF("ata_transfer : no sectors left (status %02x)\n" |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 315 | , (unsigned) status); |
| 316 | return 4; |
| 317 | } |
| 318 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 319 | // Enable interrupts |
| 320 | outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); |
| 321 | return 0; |
| 322 | } |
| 323 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 324 | static noinline int |
| 325 | ata_transfer_disk(const struct ata_op_s *op, int iswrite) |
| 326 | { |
| 327 | return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE |
| 328 | , 0, 0, op->far_buffer); |
| 329 | } |
| 330 | |
| 331 | static noinline int |
| 332 | ata_transfer_cdrom(const struct ata_op_s *op) |
| 333 | { |
| 334 | return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE |
| 335 | , 0, 0, op->far_buffer); |
| 336 | } |
| 337 | |
| 338 | static noinline int |
| 339 | ata_transfer_emu(const struct ata_op_s *op, int before, int after) |
| 340 | { |
| 341 | int vcount = op->count * 4 - before - after; |
| 342 | int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE |
| 343 | , before*512, after*512, op->far_buffer); |
| 344 | if (ret) { |
| 345 | SET_EBDA(ata.trsfsectors, 0); |
| 346 | return ret; |
| 347 | } |
| 348 | SET_EBDA(ata.trsfsectors, vcount); |
| 349 | return 0; |
| 350 | } |
| 351 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 352 | |
| 353 | /**************************************************************** |
| 354 | * ATA hard drive functions |
| 355 | ****************************************************************/ |
| 356 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 357 | static noinline int |
| 358 | send_cmd_disk(const struct ata_op_s *op, u16 command) |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 359 | { |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 360 | u8 slave = op->driveid % 2; |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 361 | u64 lba = op->lba; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 362 | |
| 363 | struct ata_pio_command cmd; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 364 | memset(&cmd, 0, sizeof(cmd)); |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 365 | |
Kevin O'Connor | dfabfe0 | 2008-04-05 21:08:57 -0400 | [diff] [blame] | 366 | cmd.command = command; |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 367 | if (op->count >= (1<<8) || lba + op->count >= (1<<28)) { |
| 368 | cmd.sector_count2 = op->count >> 8; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 369 | cmd.lba_low2 = lba >> 24; |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 370 | cmd.lba_mid2 = lba >> 32; |
| 371 | cmd.lba_high2 = lba >> 40; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 372 | |
Kevin O'Connor | dfabfe0 | 2008-04-05 21:08:57 -0400 | [diff] [blame] | 373 | cmd.command |= 0x04; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 374 | lba &= 0xffffff; |
| 375 | } |
| 376 | |
| 377 | cmd.feature = 0; |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 378 | cmd.sector_count = op->count; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 379 | cmd.lba_low = lba; |
| 380 | cmd.lba_mid = lba >> 8; |
| 381 | cmd.lba_high = lba >> 16; |
| 382 | cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) |
| 383 | | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 384 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 385 | return send_cmd(op->driveid, &cmd); |
| 386 | } |
| 387 | |
| 388 | // Read/write count blocks from a harddrive. |
| 389 | __always_inline int |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 390 | ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer) |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 391 | { |
| 392 | struct ata_op_s op; |
| 393 | op.driveid = driveid; |
| 394 | op.lba = lba; |
| 395 | op.count = count; |
| 396 | op.far_buffer = far_buffer; |
| 397 | |
| 398 | int ret = send_cmd_disk(&op, command); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 399 | if (ret) |
| 400 | return ret; |
| 401 | |
| 402 | int iswrite = command == ATA_CMD_WRITE_SECTORS; |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 403 | return ata_transfer_disk(&op, iswrite); |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 404 | } |
| 405 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 406 | |
| 407 | /**************************************************************** |
| 408 | * ATAPI functions |
| 409 | ****************************************************************/ |
| 410 | |
| 411 | // Low-level atapi command transmit function. |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 412 | static __always_inline int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 413 | send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 414 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 415 | u8 channel = driveid / 2; |
| 416 | u8 slave = driveid % 2; |
Kevin O'Connor | a20c4a5 | 2008-03-09 13:32:36 -0400 | [diff] [blame] | 417 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 418 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 419 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 420 | struct ata_pio_command cmd; |
| 421 | cmd.sector_count = 0; |
| 422 | cmd.feature = 0; |
| 423 | cmd.lba_low = 0; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 424 | cmd.lba_mid = blocksize; |
| 425 | cmd.lba_high = blocksize >> 8; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 426 | cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0; |
| 427 | cmd.command = ATA_CMD_PACKET; |
| 428 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 429 | int ret = send_cmd(driveid, &cmd); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 430 | if (ret) |
| 431 | return ret; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 432 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 433 | // Send command to device |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 434 | outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 435 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 436 | int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT); |
| 437 | if (status < 0) |
| 438 | return status; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 439 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 440 | return 0; |
| 441 | } |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 442 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 443 | // Low-level cdrom read atapi command transmit function. |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 444 | static int |
| 445 | send_cmd_cdrom(const struct ata_op_s *op) |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 446 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 447 | u8 atacmd[12]; |
| 448 | memset(atacmd, 0, sizeof(atacmd)); |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 449 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 450 | atacmd[0]=0x28; // READ command |
| 451 | atacmd[7]=(op->count & 0xff00) >> 8; // Sectors |
| 452 | atacmd[8]=(op->count & 0x00ff); |
| 453 | atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA |
| 454 | atacmd[3]=(op->lba & 0x00ff0000) >> 16; |
| 455 | atacmd[4]=(op->lba & 0x0000ff00) >> 8; |
| 456 | atacmd[5]=(op->lba & 0x000000ff); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 457 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 458 | return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd) |
| 459 | , CDROM_SECTOR_SIZE); |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 460 | } |
| 461 | |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 462 | // Read sectors from the cdrom. |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 463 | __always_inline int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 464 | cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer) |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 465 | { |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 466 | struct ata_op_s op; |
| 467 | op.driveid = driveid; |
| 468 | op.lba = lba; |
| 469 | op.count = count; |
| 470 | op.far_buffer = far_buffer; |
| 471 | |
| 472 | int ret = send_cmd_cdrom(&op); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 473 | if (ret) |
| 474 | return ret; |
| 475 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 476 | return ata_transfer_cdrom(&op); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // Pretend the cdrom has 512 byte sectors (instead of 2048) and read |
| 480 | // sectors. |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 481 | __always_inline int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 482 | cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer) |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 483 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 484 | u32 velba = vlba + vcount - 1; |
| 485 | u32 lba = vlba / 4; |
| 486 | u32 elba = velba / 4; |
| 487 | int count = elba - lba + 1; |
| 488 | int before = vlba % 4; |
| 489 | int after = 3 - (velba % 4); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 490 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 491 | struct ata_op_s op; |
| 492 | op.driveid = driveid; |
| 493 | op.lba = lba; |
| 494 | op.count = count; |
| 495 | op.far_buffer = far_buffer; |
| 496 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 497 | DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d" |
| 498 | " count=%d before=%d after=%d\n" |
| 499 | , driveid, vlba, vcount, far_buffer, lba, elba |
| 500 | , count, before, after); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 501 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 502 | int ret = send_cmd_cdrom(&op); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 503 | if (ret) |
| 504 | return ret; |
| 505 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 506 | return ata_transfer_emu(&op, before, after); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 509 | // Send a simple atapi command to a drive. |
| 510 | int |
| 511 | ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen |
| 512 | , u32 length, void *far_buffer) |
| 513 | { |
| 514 | int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length); |
| 515 | if (ret) |
| 516 | return ret; |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 517 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 518 | return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /**************************************************************** |
| 523 | * ATA detect and init |
| 524 | ****************************************************************/ |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 525 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 526 | static void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 527 | report_model(int driveid, u8 *buffer) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 528 | { |
| 529 | u8 model[41]; |
| 530 | |
| 531 | // Read model name |
| 532 | int i; |
| 533 | for (i=0; i<40; i+=2) { |
| 534 | model[i] = buffer[i+54+1]; |
| 535 | model[i+1] = buffer[i+54]; |
| 536 | } |
| 537 | |
| 538 | // Reformat |
| 539 | model[40] = 0x00; |
| 540 | for (i=39; i>0; i--) { |
| 541 | if (model[i] != 0x20) |
| 542 | break; |
| 543 | model[i] = 0x00; |
| 544 | } |
| 545 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 546 | u8 channel = driveid / 2; |
| 547 | u8 slave = driveid % 2; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 548 | // XXX - model on stack not %cs |
| 549 | printf("ata%d %s: %s", channel, slave ? " slave" : "master", model); |
| 550 | } |
| 551 | |
| 552 | static u8 |
| 553 | get_ata_version(u8 *buffer) |
| 554 | { |
| 555 | u16 ataversion = *(u16*)&buffer[160]; |
| 556 | u8 version; |
| 557 | for (version=15; version>0; version--) |
| 558 | if (ataversion & (1<<version)) |
| 559 | break; |
| 560 | return version; |
| 561 | } |
| 562 | |
| 563 | static void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 564 | init_drive_atapi(int driveid) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 565 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 566 | SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 567 | |
| 568 | // Temporary values to do the transfer |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 569 | SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM); |
| 570 | SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 571 | |
| 572 | // Now we send a IDENTIFY command to ATAPI device |
| 573 | u8 buffer[0x0200]; |
| 574 | memset(buffer, 0, sizeof(buffer)); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 575 | u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 576 | , 1, 1 |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 577 | , MAKE_FARPTR(GET_SEG(SS), (u32)buffer)); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 578 | if (ret != 0) |
| 579 | BX_PANIC("ata-detect: Failed to detect ATAPI device\n"); |
| 580 | |
| 581 | u8 type = buffer[1] & 0x1f; |
| 582 | u8 removable = (buffer[0] & 0x80) ? 1 : 0; |
| 583 | u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 584 | u16 blksize = CDROM_SECTOR_SIZE; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 585 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 586 | SET_EBDA(ata.devices[driveid].device, type); |
| 587 | SET_EBDA(ata.devices[driveid].removable, removable); |
| 588 | SET_EBDA(ata.devices[driveid].mode, mode); |
| 589 | SET_EBDA(ata.devices[driveid].blksize, blksize); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 590 | |
| 591 | // fill cdidmap |
| 592 | u8 cdcount = GET_EBDA(ata.cdcount); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 593 | SET_EBDA(ata.idmap[1][cdcount], driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 594 | SET_EBDA(ata.cdcount, ++cdcount); |
| 595 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 596 | report_model(driveid, buffer); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 597 | u8 version = get_ata_version(buffer); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 598 | if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 599 | printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version); |
| 600 | else |
| 601 | printf(" ATAPI-%d Device\n", version); |
| 602 | } |
| 603 | |
| 604 | static void |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 605 | fill_fdpt(int driveid) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 606 | { |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 607 | if (driveid > 1) |
| 608 | return; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 609 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 610 | u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders); |
| 611 | u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads); |
| 612 | u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 613 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 614 | u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders); |
| 615 | u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads); |
| 616 | u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 617 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 618 | SET_EBDA(fdpt[driveid].precompensation, 0xffff); |
| 619 | SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3)); |
| 620 | SET_EBDA(fdpt[driveid].landing_zone, npc); |
| 621 | SET_EBDA(fdpt[driveid].cylinders, nlc); |
| 622 | SET_EBDA(fdpt[driveid].heads, nlh); |
| 623 | SET_EBDA(fdpt[driveid].sectors, nlspt); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 624 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 625 | if (nlc == npc && nlh == nph && nlspt == npspt) |
| 626 | // no logical CHS mapping used, just physical CHS |
| 627 | // use Standard Fixed Disk Parameter Table (FDPT) |
| 628 | return; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 629 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 630 | // complies with Phoenix style Translated Fixed Disk Parameter |
| 631 | // Table (FDPT) |
| 632 | SET_EBDA(fdpt[driveid].phys_cylinders, npc); |
| 633 | SET_EBDA(fdpt[driveid].phys_heads, nph); |
| 634 | SET_EBDA(fdpt[driveid].phys_sectors, npspt); |
| 635 | SET_EBDA(fdpt[driveid].a0h_signature, 0xa0); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 636 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 637 | // Checksum structure. |
| 638 | u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s |
| 639 | , fdpt[driveid])); |
| 640 | u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s |
| 641 | , fdpt[driveid]) - 1); |
| 642 | SET_EBDA(fdpt[driveid].checksum, -sum); |
| 643 | } |
| 644 | |
| 645 | static u8 |
| 646 | get_translation(int driveid) |
| 647 | { |
| 648 | u8 channel = driveid / 2; |
| 649 | u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2); |
| 650 | translation >>= 2 * (driveid % 4); |
| 651 | translation &= 0x03; |
| 652 | return translation; |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | setup_translation(int driveid) |
| 657 | { |
| 658 | u8 translation = get_translation(driveid); |
| 659 | SET_EBDA(ata.devices[driveid].translation, translation); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 660 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 661 | u8 channel = driveid / 2; |
| 662 | u8 slave = driveid % 2; |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 663 | u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads); |
| 664 | u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders); |
| 665 | u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt); |
| 666 | u64 sectors = GET_EBDA(ata.devices[driveid].sectors); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 667 | |
| 668 | BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=" |
| 669 | , channel, slave, cylinders, heads, spt); |
| 670 | switch (translation) { |
| 671 | case ATA_TRANSLATION_NONE: |
| 672 | BX_INFO("none"); |
| 673 | break; |
| 674 | case ATA_TRANSLATION_LBA: |
| 675 | BX_INFO("lba"); |
| 676 | spt = 63; |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 677 | if (sectors > 63*255*1024) { |
| 678 | heads = 255; |
| 679 | cylinders = 1024; |
| 680 | break; |
| 681 | } |
| 682 | u32 sect = (u32)sectors / 63; |
| 683 | heads = sect / 1024; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 684 | if (heads>128) |
| 685 | heads = 255; |
| 686 | else if (heads>64) |
| 687 | heads = 128; |
| 688 | else if (heads>32) |
| 689 | heads = 64; |
| 690 | else if (heads>16) |
| 691 | heads = 32; |
| 692 | else |
| 693 | heads = 16; |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 694 | cylinders = sect / heads; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 695 | break; |
| 696 | case ATA_TRANSLATION_RECHS: |
| 697 | BX_INFO("r-echs"); |
| 698 | // Take care not to overflow |
| 699 | if (heads==16) { |
| 700 | if (cylinders>61439) |
| 701 | cylinders=61439; |
| 702 | heads=15; |
| 703 | cylinders = (u16)((u32)(cylinders)*16/15); |
| 704 | } |
| 705 | // then go through the large bitshift process |
| 706 | case ATA_TRANSLATION_LARGE: |
| 707 | if (translation == ATA_TRANSLATION_LARGE) |
| 708 | BX_INFO("large"); |
Kevin O'Connor | dfabfe0 | 2008-04-05 21:08:57 -0400 | [diff] [blame] | 709 | while (cylinders > 1024) { |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 710 | cylinders >>= 1; |
| 711 | heads <<= 1; |
| 712 | |
| 713 | // If we max out the head count |
| 714 | if (heads > 127) |
| 715 | break; |
| 716 | } |
| 717 | break; |
| 718 | } |
| 719 | // clip to 1024 cylinders in lchs |
| 720 | if (cylinders > 1024) |
| 721 | cylinders = 1024; |
| 722 | BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt); |
| 723 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 724 | SET_EBDA(ata.devices[driveid].lchs.heads, heads); |
| 725 | SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders); |
| 726 | SET_EBDA(ata.devices[driveid].lchs.spt, spt); |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 727 | } |
| 728 | |
| 729 | static void |
| 730 | init_drive_ata(int driveid) |
| 731 | { |
| 732 | SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA); |
| 733 | |
| 734 | // Temporary values to do the transfer |
| 735 | SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD); |
| 736 | SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16); |
| 737 | |
| 738 | // Now we send a IDENTIFY command to ATA device |
| 739 | u8 buffer[0x0200]; |
| 740 | memset(buffer, 0, sizeof(buffer)); |
| 741 | u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE |
| 742 | , 1, 1 |
| 743 | , MAKE_FARPTR(GET_SEG(SS), (u32)buffer)); |
| 744 | if (ret) |
| 745 | BX_PANIC("ata-detect: Failed to detect ATA device\n"); |
| 746 | |
| 747 | u8 removable = (buffer[0] & 0x80) ? 1 : 0; |
| 748 | u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16; |
| 749 | u16 blksize = *(u16*)&buffer[10]; |
| 750 | |
| 751 | u16 cylinders = *(u16*)&buffer[1*2]; // word 1 |
| 752 | u16 heads = *(u16*)&buffer[3*2]; // word 3 |
| 753 | u16 spt = *(u16*)&buffer[6*2]; // word 6 |
| 754 | |
| 755 | u64 sectors; |
| 756 | if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support |
| 757 | sectors = *(u64*)&buffer[100*2]; // word 100-103 |
| 758 | else |
| 759 | sectors = *(u32*)&buffer[60*2]; // word 60 and word 61 |
| 760 | |
| 761 | SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD); |
| 762 | SET_EBDA(ata.devices[driveid].removable, removable); |
| 763 | SET_EBDA(ata.devices[driveid].mode, mode); |
| 764 | SET_EBDA(ata.devices[driveid].blksize, blksize); |
| 765 | SET_EBDA(ata.devices[driveid].pchs.heads, heads); |
| 766 | SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders); |
| 767 | SET_EBDA(ata.devices[driveid].pchs.spt, spt); |
| 768 | SET_EBDA(ata.devices[driveid].sectors, sectors); |
| 769 | |
| 770 | // Setup disk geometry translation. |
| 771 | setup_translation(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 772 | |
| 773 | // fill hdidmap |
| 774 | u8 hdcount = GET_EBDA(ata.hdcount); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 775 | SET_EBDA(ata.idmap[0][hdcount], driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 776 | SET_EBDA(ata.hdcount, ++hdcount); |
| 777 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 778 | // Fill "fdpt" structure. |
| 779 | fill_fdpt(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 780 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 781 | // Report drive info to user. |
| 782 | u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 783 | report_model(driveid, buffer); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 784 | u8 version = get_ata_version(buffer); |
| 785 | if (sizeinmb < (1 << 16)) |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 786 | printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 787 | else |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 788 | printf(" ATA-%d Hard-Disk (%u GBytes)\n", version |
| 789 | , (u32)(sizeinmb >> 10)); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 793 | init_drive_unknown(int driveid) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 794 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 795 | SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 796 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 797 | u8 channel = driveid / 2; |
| 798 | u8 slave = driveid % 2; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 799 | printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master"); |
| 800 | } |
| 801 | |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 802 | static void |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 803 | ata_detect() |
| 804 | { |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 805 | // Device detection |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 806 | int driveid; |
| 807 | for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) { |
| 808 | u8 channel = driveid / 2; |
| 809 | u8 slave = driveid % 2; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 810 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 811 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 812 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 813 | |
| 814 | // Disable interrupts |
| 815 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC); |
| 816 | |
| 817 | // Look for device |
| 818 | outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH); |
| 819 | outb(0x55, iobase1+ATA_CB_SC); |
| 820 | outb(0xaa, iobase1+ATA_CB_SN); |
| 821 | outb(0xaa, iobase1+ATA_CB_SC); |
| 822 | outb(0x55, iobase1+ATA_CB_SN); |
| 823 | outb(0x55, iobase1+ATA_CB_SC); |
| 824 | outb(0xaa, iobase1+ATA_CB_SN); |
| 825 | |
| 826 | // If we found something |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 827 | u8 sc = inb(iobase1+ATA_CB_SC); |
| 828 | u8 sn = inb(iobase1+ATA_CB_SN); |
| 829 | |
| 830 | if (sc != 0x55 || sn != 0xaa) |
| 831 | continue; |
| 832 | |
| 833 | // reset the channel |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 834 | ata_reset(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 835 | |
| 836 | // check for ATA or ATAPI |
| 837 | outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 838 | sc = inb(iobase1+ATA_CB_SC); |
| 839 | sn = inb(iobase1+ATA_CB_SN); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 840 | if (sc!=0x01 || sn!=0x01) { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 841 | init_drive_unknown(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 842 | continue; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 843 | } |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 844 | u8 cl = inb(iobase1+ATA_CB_CL); |
| 845 | u8 ch = inb(iobase1+ATA_CB_CH); |
| 846 | u8 st = inb(iobase1+ATA_CB_STAT); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 847 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 848 | if (cl==0x14 && ch==0xeb) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 849 | init_drive_atapi(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 850 | else if (cl==0x00 && ch==0x00 && st!=0x00) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 851 | init_drive_ata(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 852 | else if (cl==0xff && ch==0xff) |
| 853 | // None |
| 854 | continue; |
| 855 | else |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 856 | init_drive_unknown(driveid); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 857 | } |
| 858 | |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 859 | printf("\n"); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 860 | } |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 861 | |
| 862 | static void |
| 863 | ata_init() |
| 864 | { |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 865 | // hdidmap and cdidmap init. |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 866 | u8 device; |
| 867 | for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) { |
| 868 | SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES); |
| 869 | SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES); |
| 870 | } |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 871 | |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 872 | #if CONFIG_MAX_ATA_INTERFACES > 0 |
| 873 | SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA); |
| 874 | SET_EBDA(ata.channels[0].iobase1, 0x1f0); |
| 875 | SET_EBDA(ata.channels[0].iobase2, 0x3f0); |
| 876 | SET_EBDA(ata.channels[0].irq, 14); |
| 877 | #endif |
| 878 | #if CONFIG_MAX_ATA_INTERFACES > 1 |
| 879 | SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA); |
| 880 | SET_EBDA(ata.channels[1].iobase1, 0x170); |
| 881 | SET_EBDA(ata.channels[1].iobase2, 0x370); |
| 882 | SET_EBDA(ata.channels[1].irq, 15); |
| 883 | #endif |
| 884 | #if CONFIG_MAX_ATA_INTERFACES > 2 |
| 885 | SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA); |
| 886 | SET_EBDA(ata.channels[2].iobase1, 0x1e8); |
| 887 | SET_EBDA(ata.channels[2].iobase2, 0x3e0); |
| 888 | SET_EBDA(ata.channels[2].irq, 12); |
| 889 | #endif |
| 890 | #if CONFIG_MAX_ATA_INTERFACES > 3 |
| 891 | SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA); |
| 892 | SET_EBDA(ata.channels[3].iobase1, 0x168); |
| 893 | SET_EBDA(ata.channels[3].iobase2, 0x360); |
| 894 | SET_EBDA(ata.channels[3].irq, 11); |
| 895 | #endif |
| 896 | #if CONFIG_MAX_ATA_INTERFACES > 4 |
| 897 | #error Please fill the ATA interface informations |
| 898 | #endif |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | void |
| 902 | hard_drive_setup() |
| 903 | { |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame^] | 904 | if (!CONFIG_ATA) |
| 905 | return; |
| 906 | |
| 907 | ata_init(); |
| 908 | ata_detect(); |
| 909 | |
| 910 | // Store the device count |
| 911 | SET_BDA(disk_count, GET_EBDA(ata.hdcount)); |
| 912 | |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 913 | SET_BDA(disk_control_byte, 0xc0); |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 914 | } |