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