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