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