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 | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 144 | struct ata_pio_command { |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 145 | u8 feature; |
| 146 | u8 sector_count; |
| 147 | u8 lba_low; |
| 148 | u8 lba_mid; |
| 149 | u8 lba_high; |
| 150 | u8 device; |
| 151 | u8 command; |
| 152 | |
| 153 | u8 sector_count2; |
| 154 | u8 lba_low2; |
| 155 | u8 lba_mid2; |
| 156 | u8 lba_high2; |
| 157 | }; |
| 158 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 159 | // Send an ata command to the drive. |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 160 | static int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 161 | send_cmd(int driveid, struct ata_pio_command *cmd) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 162 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 163 | u8 channel = driveid / 2; |
Kevin O'Connor | 5b15fbf | 2008-03-08 23:20:41 -0500 | [diff] [blame] | 164 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 165 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 166 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 167 | int status = inb(iobase1 + ATA_CB_STAT); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 168 | if (status & ATA_CB_STAT_BSY) |
| 169 | return 1; |
| 170 | |
| 171 | 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] | 172 | if (cmd->command & 0x04) { |
| 173 | outb(0x00, iobase1 + ATA_CB_FR); |
| 174 | outb(cmd->sector_count2, iobase1 + ATA_CB_SC); |
| 175 | outb(cmd->lba_low2, iobase1 + ATA_CB_SN); |
| 176 | outb(cmd->lba_mid2, iobase1 + ATA_CB_CL); |
| 177 | outb(cmd->lba_high2, iobase1 + ATA_CB_CH); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 178 | } |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 179 | outb(cmd->feature, iobase1 + ATA_CB_FR); |
| 180 | outb(cmd->sector_count, iobase1 + ATA_CB_SC); |
| 181 | outb(cmd->lba_low, iobase1 + ATA_CB_SN); |
| 182 | outb(cmd->lba_mid, iobase1 + ATA_CB_CL); |
| 183 | outb(cmd->lba_high, iobase1 + ATA_CB_CH); |
| 184 | outb(cmd->device, iobase1 + ATA_CB_DH); |
| 185 | outb(cmd->command, iobase1 + ATA_CB_CMD); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 186 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 187 | nsleep(400); |
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 | status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT); |
| 190 | if (status < 0) |
| 191 | return status; |
| 192 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 193 | if (status & ATA_CB_STAT_ERR) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 194 | DEBUGF("send_cmd : read error\n"); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 195 | return 2; |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 196 | } |
| 197 | if (!(status & ATA_CB_STAT_DRQ)) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 198 | DEBUGF("send_cmd : DRQ not set (status %02x)\n" |
Kevin O'Connor | 127cbd7 | 2008-03-08 11:34:28 -0500 | [diff] [blame] | 199 | , (unsigned) status); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 200 | return 3; |
| 201 | } |
| 202 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 206 | // Read and discard x number of bytes from an io channel. |
| 207 | static void |
| 208 | insx_discard(int mode, int iobase1, int bytes) |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 209 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 210 | int count, i; |
| 211 | if (mode == ATA_MODE_PIO32) { |
| 212 | count = bytes / 4; |
| 213 | for (i=0; i<count; i++) |
| 214 | inl(iobase1); |
| 215 | } else { |
| 216 | count = bytes / 2; |
| 217 | for (i=0; i<count; i++) |
| 218 | inw(iobase1); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive |
| 223 | // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first |
| 224 | // and/or last block may be partially transferred. This function is |
| 225 | // inlined because all the callers use different forms and because the |
| 226 | // large number of parameters would consume a lot of stack space. |
Kevin O'Connor | 74f9c08 | 2008-04-13 16:57:16 -0400 | [diff] [blame] | 227 | static __always_inline int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 228 | ata_transfer(int driveid, int iswrite, int count, int blocksize |
| 229 | , int skipfirst, int skiplast, void *far_buffer) |
| 230 | { |
| 231 | DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d" |
| 232 | " skipf=%d skipl=%d buf=%p\n" |
| 233 | , driveid, iswrite, count, blocksize |
| 234 | , skipfirst, skiplast, far_buffer); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 235 | |
| 236 | // Reset count of transferred data |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 237 | SET_EBDA(ata.trsfsectors, 0); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 238 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 239 | u8 channel = driveid / 2; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 240 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 241 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 242 | u8 mode = GET_EBDA(ata.devices[driveid].mode); |
| 243 | int current = 0; |
| 244 | int status; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 245 | for (;;) { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 246 | int bsize = blocksize; |
| 247 | if (skipfirst && current == 0) { |
| 248 | insx_discard(mode, iobase1, skipfirst); |
| 249 | bsize -= skipfirst; |
| 250 | } |
| 251 | if (skiplast && current == count-1) |
| 252 | bsize -= skiplast; |
| 253 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 254 | if (iswrite) { |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 255 | // Write data to controller |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 256 | DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 257 | if (mode == ATA_MODE_PIO32) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 258 | outsl_far(iobase1, far_buffer, bsize / 4); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 259 | else |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 260 | outsw_far(iobase1, far_buffer, bsize / 2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 261 | } else { |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 262 | // Read data from controller |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 263 | DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 264 | if (mode == ATA_MODE_PIO32) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 265 | insl_far(iobase1, far_buffer, bsize / 4); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 266 | else |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 267 | insw_far(iobase1, far_buffer, bsize / 2); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 268 | } |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 269 | far_buffer += bsize; |
| 270 | |
| 271 | if (skiplast && current == count-1) |
| 272 | insx_discard(mode, iobase1, skiplast); |
| 273 | |
| 274 | status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT); |
| 275 | if (status < 0) |
| 276 | // Error |
| 277 | return status; |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 278 | |
| 279 | current++; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 280 | SET_EBDA(ata.trsfsectors, current); |
| 281 | if (current == count) |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 282 | break; |
| 283 | status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ |
| 284 | | ATA_CB_STAT_ERR); |
| 285 | if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 286 | DEBUGF("ata_transfer : more sectors left (status %02x)\n" |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 287 | , (unsigned) status); |
| 288 | return 5; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 289 | } |
| 290 | } |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 291 | |
| 292 | status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF |
| 293 | | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 294 | if (!iswrite) |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 295 | status &= ~ATA_CB_STAT_DF; |
| 296 | if (status != ATA_CB_STAT_RDY ) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 297 | DEBUGF("ata_transfer : no sectors left (status %02x)\n" |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 298 | , (unsigned) status); |
| 299 | return 4; |
| 300 | } |
| 301 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 302 | // Enable interrupts |
| 303 | outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); |
| 304 | return 0; |
| 305 | } |
| 306 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 307 | |
| 308 | /**************************************************************** |
| 309 | * ATA hard drive functions |
| 310 | ****************************************************************/ |
| 311 | |
| 312 | // Read/write count blocks from a harddrive. |
| 313 | int |
| 314 | 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] | 315 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 316 | u8 slave = driveid % 2; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 317 | |
| 318 | struct ata_pio_command cmd; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 319 | memset(&cmd, 0, sizeof(cmd)); |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 320 | |
Kevin O'Connor | dfabfe0 | 2008-04-05 21:08:57 -0400 | [diff] [blame] | 321 | cmd.command = command; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 322 | if (count >= (1<<8) || lba + count >= (1<<28)) { |
| 323 | cmd.sector_count2 = count >> 8; |
| 324 | cmd.lba_low2 = lba >> 24; |
| 325 | cmd.lba_mid2 = 0; |
| 326 | cmd.lba_high2 = 0; |
| 327 | |
Kevin O'Connor | dfabfe0 | 2008-04-05 21:08:57 -0400 | [diff] [blame] | 328 | cmd.command |= 0x04; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 329 | lba &= 0xffffff; |
| 330 | } |
| 331 | |
| 332 | cmd.feature = 0; |
| 333 | cmd.sector_count = count; |
| 334 | cmd.lba_low = lba; |
| 335 | cmd.lba_mid = lba >> 8; |
| 336 | cmd.lba_high = lba >> 16; |
| 337 | cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) |
| 338 | | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 339 | |
| 340 | int ret = send_cmd(driveid, &cmd); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | |
| 344 | int iswrite = command == ATA_CMD_WRITE_SECTORS; |
| 345 | return ata_transfer(driveid, iswrite, count, IDE_SECTOR_SIZE |
| 346 | , 0, 0, far_buffer); |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 349 | |
| 350 | /**************************************************************** |
| 351 | * ATAPI functions |
| 352 | ****************************************************************/ |
| 353 | |
| 354 | // Low-level atapi command transmit function. |
| 355 | static int |
| 356 | send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 357 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 358 | u8 channel = driveid / 2; |
| 359 | u8 slave = driveid % 2; |
Kevin O'Connor | a20c4a5 | 2008-03-09 13:32:36 -0400 | [diff] [blame] | 360 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 361 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 362 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 363 | struct ata_pio_command cmd; |
| 364 | cmd.sector_count = 0; |
| 365 | cmd.feature = 0; |
| 366 | cmd.lba_low = 0; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 367 | cmd.lba_mid = blocksize; |
| 368 | cmd.lba_high = blocksize >> 8; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 369 | cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0; |
| 370 | cmd.command = ATA_CMD_PACKET; |
| 371 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 372 | int ret = send_cmd(driveid, &cmd); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 373 | if (ret) |
| 374 | return ret; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 375 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 376 | // Send command to device |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 377 | 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] | 378 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 379 | int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT); |
| 380 | if (status < 0) |
| 381 | return status; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 382 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 383 | return 0; |
| 384 | } |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 385 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 386 | // Low-level cdrom read atapi command transmit function. |
Kevin O'Connor | 74f9c08 | 2008-04-13 16:57:16 -0400 | [diff] [blame] | 387 | static __always_inline int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 388 | send_cdrom_cmd(int driveid, u32 lba, u16 count) |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 389 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 390 | u8 atacmd[12]; |
| 391 | memset(atacmd, 0, sizeof(atacmd)); |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 392 | |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 393 | atacmd[0]=0x28; // READ command |
| 394 | atacmd[7]=(count & 0xff00) >> 8; // Sectors |
| 395 | atacmd[8]=(count & 0x00ff); // Sectors |
| 396 | atacmd[2]=(lba & 0xff000000) >> 24; // LBA |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 397 | atacmd[3]=(lba & 0x00ff0000) >> 16; |
| 398 | atacmd[4]=(lba & 0x0000ff00) >> 8; |
| 399 | atacmd[5]=(lba & 0x000000ff); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 400 | |
| 401 | return send_atapi_cmd(driveid, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE); |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 404 | // Read sectors from the cdrom. |
| 405 | int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 406 | cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer) |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 407 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 408 | int ret = send_cdrom_cmd(driveid, lba, count); |
| 409 | if (ret) |
| 410 | return ret; |
| 411 | |
| 412 | 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] | 413 | } |
| 414 | |
| 415 | // Pretend the cdrom has 512 byte sectors (instead of 2048) and read |
| 416 | // sectors. |
| 417 | int |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 418 | 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] | 419 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 420 | u32 velba = vlba + vcount - 1; |
| 421 | u32 lba = vlba / 4; |
| 422 | u32 elba = velba / 4; |
| 423 | int count = elba - lba + 1; |
| 424 | int before = vlba % 4; |
| 425 | int after = 3 - (velba % 4); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 426 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 427 | DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d" |
| 428 | " count=%d before=%d after=%d\n" |
| 429 | , driveid, vlba, vcount, far_buffer, lba, elba |
| 430 | , count, before, after); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 431 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 432 | int ret = send_cdrom_cmd(driveid, lba, count); |
| 433 | if (ret) |
| 434 | return ret; |
| 435 | |
| 436 | ret = ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE |
| 437 | , before*512, after*512, far_buffer); |
| 438 | if (ret) { |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 439 | SET_EBDA(ata.trsfsectors, 0); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 440 | return ret; |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 441 | } |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 442 | SET_EBDA(ata.trsfsectors, vcount); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 446 | // Send a simple atapi command to a drive. |
| 447 | int |
| 448 | ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen |
| 449 | , u32 length, void *far_buffer) |
| 450 | { |
| 451 | int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length); |
| 452 | if (ret) |
| 453 | return ret; |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 454 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 455 | return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | /**************************************************************** |
| 460 | * ATA detect and init |
| 461 | ****************************************************************/ |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 462 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 463 | static void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 464 | report_model(int driveid, u8 *buffer) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 465 | { |
| 466 | u8 model[41]; |
| 467 | |
| 468 | // Read model name |
| 469 | int i; |
| 470 | for (i=0; i<40; i+=2) { |
| 471 | model[i] = buffer[i+54+1]; |
| 472 | model[i+1] = buffer[i+54]; |
| 473 | } |
| 474 | |
| 475 | // Reformat |
| 476 | model[40] = 0x00; |
| 477 | for (i=39; i>0; i--) { |
| 478 | if (model[i] != 0x20) |
| 479 | break; |
| 480 | model[i] = 0x00; |
| 481 | } |
| 482 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 483 | u8 channel = driveid / 2; |
| 484 | u8 slave = driveid % 2; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 485 | // XXX - model on stack not %cs |
| 486 | printf("ata%d %s: %s", channel, slave ? " slave" : "master", model); |
| 487 | } |
| 488 | |
| 489 | static u8 |
| 490 | get_ata_version(u8 *buffer) |
| 491 | { |
| 492 | u16 ataversion = *(u16*)&buffer[160]; |
| 493 | u8 version; |
| 494 | for (version=15; version>0; version--) |
| 495 | if (ataversion & (1<<version)) |
| 496 | break; |
| 497 | return version; |
| 498 | } |
| 499 | |
| 500 | static void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 501 | init_drive_atapi(int driveid) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 502 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 503 | SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 504 | |
| 505 | // Temporary values to do the transfer |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 506 | SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM); |
| 507 | SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 508 | |
| 509 | // Now we send a IDENTIFY command to ATAPI device |
| 510 | u8 buffer[0x0200]; |
| 511 | memset(buffer, 0, sizeof(buffer)); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 512 | u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 513 | , 1, 1 |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 514 | , MAKE_FARPTR(GET_SEG(SS), (u32)buffer)); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 515 | if (ret != 0) |
| 516 | BX_PANIC("ata-detect: Failed to detect ATAPI device\n"); |
| 517 | |
| 518 | u8 type = buffer[1] & 0x1f; |
| 519 | u8 removable = (buffer[0] & 0x80) ? 1 : 0; |
| 520 | u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 521 | u16 blksize = CDROM_SECTOR_SIZE; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 522 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 523 | SET_EBDA(ata.devices[driveid].device, type); |
| 524 | SET_EBDA(ata.devices[driveid].removable, removable); |
| 525 | SET_EBDA(ata.devices[driveid].mode, mode); |
| 526 | SET_EBDA(ata.devices[driveid].blksize, blksize); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 527 | |
| 528 | // fill cdidmap |
| 529 | u8 cdcount = GET_EBDA(ata.cdcount); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 530 | SET_EBDA(ata.idmap[1][cdcount], driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 531 | SET_EBDA(ata.cdcount, ++cdcount); |
| 532 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 533 | report_model(driveid, buffer); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 534 | u8 version = get_ata_version(buffer); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 535 | if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 536 | printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version); |
| 537 | else |
| 538 | printf(" ATAPI-%d Device\n", version); |
| 539 | } |
| 540 | |
| 541 | static void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 542 | init_drive_ata(int driveid) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 543 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 544 | SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 545 | |
| 546 | // Temporary values to do the transfer |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 547 | SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD); |
| 548 | SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 549 | |
| 550 | // Now we send a IDENTIFY command to ATA device |
| 551 | u8 buffer[0x0200]; |
| 552 | memset(buffer, 0, sizeof(buffer)); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 553 | u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 554 | , 1, 1 |
Kevin O'Connor | 070231b | 2008-03-22 20:44:37 -0400 | [diff] [blame] | 555 | , MAKE_FARPTR(GET_SEG(SS), (u32)buffer)); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 556 | if (ret) |
| 557 | BX_PANIC("ata-detect: Failed to detect ATA device\n"); |
| 558 | |
| 559 | u8 removable = (buffer[0] & 0x80) ? 1 : 0; |
| 560 | u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16; |
| 561 | u16 blksize = *(u16*)&buffer[10]; |
| 562 | |
| 563 | u16 cylinders = *(u16*)&buffer[1*2]; // word 1 |
| 564 | u16 heads = *(u16*)&buffer[3*2]; // word 3 |
| 565 | u16 spt = *(u16*)&buffer[6*2]; // word 6 |
| 566 | |
| 567 | u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61 |
| 568 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 569 | SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD); |
| 570 | SET_EBDA(ata.devices[driveid].removable, removable); |
| 571 | SET_EBDA(ata.devices[driveid].mode, mode); |
| 572 | SET_EBDA(ata.devices[driveid].blksize, blksize); |
| 573 | SET_EBDA(ata.devices[driveid].pchs.heads, heads); |
| 574 | SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders); |
| 575 | SET_EBDA(ata.devices[driveid].pchs.spt, spt); |
| 576 | SET_EBDA(ata.devices[driveid].sectors, sectors); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 577 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 578 | u8 channel = driveid / 2; |
| 579 | u8 slave = driveid % 2; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 580 | u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2); |
| 581 | u8 shift; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 582 | for (shift=driveid%4; shift>0; shift--) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 583 | translation >>= 2; |
| 584 | translation &= 0x03; |
| 585 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 586 | SET_EBDA(ata.devices[driveid].translation, translation); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 587 | |
| 588 | BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=" |
| 589 | , channel, slave, cylinders, heads, spt); |
| 590 | switch (translation) { |
| 591 | case ATA_TRANSLATION_NONE: |
| 592 | BX_INFO("none"); |
| 593 | break; |
| 594 | case ATA_TRANSLATION_LBA: |
| 595 | BX_INFO("lba"); |
| 596 | spt = 63; |
| 597 | sectors /= 63; |
| 598 | heads = sectors / 1024; |
| 599 | if (heads>128) |
| 600 | heads = 255; |
| 601 | else if (heads>64) |
| 602 | heads = 128; |
| 603 | else if (heads>32) |
| 604 | heads = 64; |
| 605 | else if (heads>16) |
| 606 | heads = 32; |
| 607 | else |
| 608 | heads = 16; |
| 609 | cylinders = sectors / heads; |
| 610 | break; |
| 611 | case ATA_TRANSLATION_RECHS: |
| 612 | BX_INFO("r-echs"); |
| 613 | // Take care not to overflow |
| 614 | if (heads==16) { |
| 615 | if (cylinders>61439) |
| 616 | cylinders=61439; |
| 617 | heads=15; |
| 618 | cylinders = (u16)((u32)(cylinders)*16/15); |
| 619 | } |
| 620 | // then go through the large bitshift process |
| 621 | case ATA_TRANSLATION_LARGE: |
| 622 | if (translation == ATA_TRANSLATION_LARGE) |
| 623 | BX_INFO("large"); |
Kevin O'Connor | dfabfe0 | 2008-04-05 21:08:57 -0400 | [diff] [blame] | 624 | while (cylinders > 1024) { |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 625 | cylinders >>= 1; |
| 626 | heads <<= 1; |
| 627 | |
| 628 | // If we max out the head count |
| 629 | if (heads > 127) |
| 630 | break; |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | // clip to 1024 cylinders in lchs |
| 635 | if (cylinders > 1024) |
| 636 | cylinders = 1024; |
| 637 | BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt); |
| 638 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 639 | SET_EBDA(ata.devices[driveid].lchs.heads, heads); |
| 640 | SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders); |
| 641 | SET_EBDA(ata.devices[driveid].lchs.spt, spt); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 642 | |
| 643 | // fill hdidmap |
| 644 | u8 hdcount = GET_EBDA(ata.hdcount); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 645 | SET_EBDA(ata.idmap[0][hdcount], driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 646 | SET_EBDA(ata.hdcount, ++hdcount); |
| 647 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 648 | u32 sizeinmb = GET_EBDA(ata.devices[driveid].sectors); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 649 | sizeinmb >>= 11; |
| 650 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 651 | report_model(driveid, buffer); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 652 | u8 version = get_ata_version(buffer); |
| 653 | if (sizeinmb < (1 << 16)) |
| 654 | printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb); |
| 655 | else |
| 656 | printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10); |
| 657 | } |
| 658 | |
| 659 | static void |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 660 | init_drive_unknown(int driveid) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 661 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 662 | SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN); |
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 | u8 channel = driveid / 2; |
| 665 | u8 slave = driveid % 2; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 666 | printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master"); |
| 667 | } |
| 668 | |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 669 | static void |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 670 | ata_detect() |
| 671 | { |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 672 | #if CONFIG_MAX_ATA_INTERFACES > 0 |
| 673 | SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA); |
| 674 | SET_EBDA(ata.channels[0].iobase1,0x1f0); |
| 675 | SET_EBDA(ata.channels[0].iobase2,0x3f0); |
| 676 | SET_EBDA(ata.channels[0].irq,14); |
| 677 | #endif |
| 678 | #if CONFIG_MAX_ATA_INTERFACES > 1 |
| 679 | SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA); |
| 680 | SET_EBDA(ata.channels[1].iobase1,0x170); |
| 681 | SET_EBDA(ata.channels[1].iobase2,0x370); |
| 682 | SET_EBDA(ata.channels[1].irq,15); |
| 683 | #endif |
| 684 | #if CONFIG_MAX_ATA_INTERFACES > 2 |
| 685 | SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA); |
| 686 | SET_EBDA(ata.channels[2].iobase1,0x1e8); |
| 687 | SET_EBDA(ata.channels[2].iobase2,0x3e0); |
| 688 | SET_EBDA(ata.channels[2].irq,12); |
| 689 | #endif |
| 690 | #if CONFIG_MAX_ATA_INTERFACES > 3 |
| 691 | SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA); |
| 692 | SET_EBDA(ata.channels[3].iobase1,0x168); |
| 693 | SET_EBDA(ata.channels[3].iobase2,0x360); |
| 694 | SET_EBDA(ata.channels[3].irq,11); |
| 695 | #endif |
| 696 | #if CONFIG_MAX_ATA_INTERFACES > 4 |
| 697 | #error Please fill the ATA interface informations |
| 698 | #endif |
| 699 | |
| 700 | // Device detection |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 701 | int driveid; |
| 702 | for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) { |
| 703 | u8 channel = driveid / 2; |
| 704 | u8 slave = driveid % 2; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 705 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 706 | u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); |
| 707 | u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 708 | |
| 709 | // Disable interrupts |
| 710 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC); |
| 711 | |
| 712 | // Look for device |
| 713 | outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH); |
| 714 | outb(0x55, iobase1+ATA_CB_SC); |
| 715 | outb(0xaa, iobase1+ATA_CB_SN); |
| 716 | outb(0xaa, iobase1+ATA_CB_SC); |
| 717 | outb(0x55, iobase1+ATA_CB_SN); |
| 718 | outb(0x55, iobase1+ATA_CB_SC); |
| 719 | outb(0xaa, iobase1+ATA_CB_SN); |
| 720 | |
| 721 | // If we found something |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 722 | u8 sc = inb(iobase1+ATA_CB_SC); |
| 723 | u8 sn = inb(iobase1+ATA_CB_SN); |
| 724 | |
| 725 | if (sc != 0x55 || sn != 0xaa) |
| 726 | continue; |
| 727 | |
| 728 | // reset the channel |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 729 | ata_reset(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 730 | |
| 731 | // check for ATA or ATAPI |
| 732 | 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] | 733 | sc = inb(iobase1+ATA_CB_SC); |
| 734 | sn = inb(iobase1+ATA_CB_SN); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 735 | if (sc!=0x01 || sn!=0x01) { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 736 | init_drive_unknown(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 737 | continue; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 738 | } |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 739 | u8 cl = inb(iobase1+ATA_CB_CL); |
| 740 | u8 ch = inb(iobase1+ATA_CB_CH); |
| 741 | u8 st = inb(iobase1+ATA_CB_STAT); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 742 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 743 | if (cl==0x14 && ch==0xeb) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 744 | init_drive_atapi(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 745 | else if (cl==0x00 && ch==0x00 && st!=0x00) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 746 | init_drive_ata(driveid); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 747 | else if (cl==0xff && ch==0xff) |
| 748 | // None |
| 749 | continue; |
| 750 | else |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 751 | init_drive_unknown(driveid); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 752 | } |
| 753 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 754 | // Store the device count |
| 755 | SET_BDA(disk_count, GET_EBDA(ata.hdcount)); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 756 | |
| 757 | printf("\n"); |
| 758 | |
| 759 | // FIXME : should use bios=cmos|auto|disable bits |
| 760 | // FIXME : should know about translation bits |
| 761 | // FIXME : move hard_drive_post here |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 762 | } |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 763 | |
| 764 | static void |
| 765 | ata_init() |
| 766 | { |
| 767 | // hdidmap and cdidmap init. |
| 768 | u8 device; |
| 769 | for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) { |
| 770 | SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES); |
| 771 | SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | static void |
| 776 | fill_hdinfo(int hdnum, u8 typecmos, u8 basecmos) |
| 777 | { |
| 778 | u8 type = inb_cmos(typecmos); |
| 779 | if (type != 47) |
| 780 | // XXX - halt |
| 781 | return; |
| 782 | |
| 783 | SET_EBDA(fdpt[hdnum].precompensation, ((inb_cmos(basecmos+4) << 8) |
| 784 | | inb_cmos(basecmos+3))); |
| 785 | SET_EBDA(fdpt[hdnum].drive_control_byte, inb_cmos(basecmos+5)); |
| 786 | SET_EBDA(fdpt[hdnum].landing_zone, ((inb_cmos(basecmos+7) << 8) |
| 787 | | inb_cmos(basecmos+6))); |
| 788 | u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0); |
| 789 | u8 heads = inb_cmos(basecmos+2); |
| 790 | u8 sectors = inb_cmos(basecmos+8); |
| 791 | if (cyl < 1024) { |
| 792 | // no logical CHS mapping used, just physical CHS |
| 793 | // use Standard Fixed Disk Parameter Table (FDPT) |
| 794 | SET_EBDA(fdpt[hdnum].cylinders, cyl); |
| 795 | SET_EBDA(fdpt[hdnum].heads, heads); |
| 796 | SET_EBDA(fdpt[hdnum].sectors, sectors); |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | // complies with Phoenix style Translated Fixed Disk Parameter |
| 801 | // Table (FDPT) |
| 802 | SET_EBDA(fdpt[hdnum].phys_cylinders, cyl); |
| 803 | SET_EBDA(fdpt[hdnum].phys_heads, heads); |
| 804 | SET_EBDA(fdpt[hdnum].phys_sectors, sectors); |
| 805 | SET_EBDA(fdpt[hdnum].sectors, sectors); |
| 806 | SET_EBDA(fdpt[hdnum].a0h_signature, 0xa0); |
| 807 | if (cyl > 8192) { |
| 808 | cyl >>= 4; |
| 809 | heads <<= 4; |
| 810 | } else if (cyl > 4096) { |
| 811 | cyl >>= 3; |
| 812 | heads <<= 3; |
| 813 | } else if (cyl > 2048) { |
| 814 | cyl >>= 2; |
| 815 | heads <<= 2; |
| 816 | } |
| 817 | SET_EBDA(fdpt[hdnum].cylinders, cyl); |
| 818 | SET_EBDA(fdpt[hdnum].heads, heads); |
| 819 | u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s |
| 820 | , fdpt[hdnum])); |
| 821 | u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s |
| 822 | , fdpt[hdnum]) - 1); |
| 823 | SET_EBDA(fdpt[hdnum].checksum, -sum); |
| 824 | } |
| 825 | |
| 826 | void |
| 827 | hard_drive_setup() |
| 828 | { |
| 829 | outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14 |
| 830 | SET_BDA(disk_count, 1); |
| 831 | SET_BDA(disk_control_byte, 0xc0); |
| 832 | |
| 833 | // move disk geometry data from CMOS to EBDA disk parameter table(s) |
| 834 | u8 diskinfo = inb_cmos(CMOS_DISK_DATA); |
| 835 | if ((diskinfo & 0xf0) == 0xf0) |
| 836 | // Fill EBDA table for hard disk 0. |
| 837 | fill_hdinfo(0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL); |
| 838 | if ((diskinfo & 0x0f) == 0x0f) |
| 839 | // XXX - bochs halts on any other type |
| 840 | // Fill EBDA table for hard disk 1. |
| 841 | fill_hdinfo(1, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL); |
| 842 | |
| 843 | if (CONFIG_ATA) { |
| 844 | ata_init(); |
| 845 | ata_detect(); |
| 846 | } |
| 847 | } |