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