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