Kevin O'Connor | c09492e | 2008-03-01 13:38:07 -0500 | [diff] [blame] | 1 | // Low level ATA disk access |
| 2 | // |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | c09492e | 2008-03-01 13:38:07 -0500 | [diff] [blame] | 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 | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 14 | #include "pci.h" // foreachpci |
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 | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 19 | #include "ata.h" // ATA_CB_STAT |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 20 | |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 21 | #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 22 | |
Kevin O'Connor | 372e071 | 2009-09-09 09:51:31 -0400 | [diff] [blame] | 23 | struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE; |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 24 | |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 25 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 26 | /**************************************************************** |
| 27 | * Helper functions |
| 28 | ****************************************************************/ |
| 29 | |
| 30 | // Wait for the specified ide state |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 31 | static inline int |
| 32 | await_ide(u8 mask, u8 flags, u16 base, u16 timeout) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 33 | { |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 34 | u64 end = calc_future_tsc(timeout); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 35 | for (;;) { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 36 | u8 status = inb(base+ATA_CB_STAT); |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 37 | if ((status & mask) == flags) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 38 | return status; |
Kevin O'Connor | 89eb624 | 2009-10-22 22:30:37 -0400 | [diff] [blame] | 39 | if (check_time(end)) { |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 40 | dprintf(1, "IDE time out\n"); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 41 | return -1; |
| 42 | } |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 43 | yield(); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 44 | } |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Wait for the device to be not-busy. |
| 48 | static int |
| 49 | await_not_bsy(u16 base) |
| 50 | { |
| 51 | return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT); |
| 52 | } |
| 53 | |
| 54 | // Wait for the device to be ready. |
| 55 | static int |
| 56 | await_rdy(u16 base) |
| 57 | { |
| 58 | 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] | 59 | } |
| 60 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 61 | // Wait for ide state - pauses for one ata cycle first. |
Kevin O'Connor | a9caeae | 2009-03-07 00:09:52 -0500 | [diff] [blame] | 62 | static inline int |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 63 | pause_await_not_bsy(u16 iobase1, u16 iobase2) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 64 | { |
| 65 | // Wait one PIO transfer cycle. |
| 66 | inb(iobase2 + ATA_CB_ASTAT); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 67 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 68 | return await_not_bsy(iobase1); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 69 | } |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 70 | |
Kevin O'Connor | ef2822a | 2008-06-07 15:23:11 -0400 | [diff] [blame] | 71 | // Wait for ide state - pause for 400ns first. |
Kevin O'Connor | a9caeae | 2009-03-07 00:09:52 -0500 | [diff] [blame] | 72 | static inline int |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 73 | ndelay_await_not_bsy(u16 iobase1) |
Kevin O'Connor | ef2822a | 2008-06-07 15:23:11 -0400 | [diff] [blame] | 74 | { |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 75 | ndelay(400); |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 76 | return await_not_bsy(iobase1); |
Kevin O'Connor | ef2822a | 2008-06-07 15:23:11 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 79 | // Reset a drive |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 80 | static void |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 81 | ata_reset(struct drive_s *drive_g) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 82 | { |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 83 | u8 ataid = GET_GLOBAL(drive_g->cntl_id); |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 84 | u8 channel = ataid / 2; |
| 85 | u8 slave = ataid % 2; |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 86 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
| 87 | u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 88 | |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 89 | dprintf(6, "ata_reset drive=%p\n", drive_g); |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 90 | // Pulse SRST |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 91 | 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] | 92 | udelay(5); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 93 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC); |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 94 | msleep(2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 95 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 96 | // wait for device to become not busy. |
| 97 | int status = await_not_bsy(iobase1); |
| 98 | if (status < 0) |
| 99 | goto done; |
| 100 | if (slave) { |
| 101 | // Change device. |
| 102 | u64 end = calc_future_tsc(IDE_TIMEOUT); |
| 103 | for (;;) { |
| 104 | outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH); |
Kevin O'Connor | c946eca | 2009-05-24 13:55:01 -0400 | [diff] [blame] | 105 | status = ndelay_await_not_bsy(iobase1); |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 106 | if (status < 0) |
| 107 | goto done; |
| 108 | if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1) |
| 109 | break; |
| 110 | // Change drive request failed to take effect - retry. |
Kevin O'Connor | 89eb624 | 2009-10-22 22:30:37 -0400 | [diff] [blame] | 111 | if (check_time(end)) { |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 112 | dprintf(1, "ata_reset slave time out\n"); |
| 113 | goto done; |
| 114 | } |
| 115 | } |
Kevin O'Connor | f5624d2 | 2009-08-18 22:17:57 -0400 | [diff] [blame] | 116 | } else { |
| 117 | // QEMU doesn't reset dh on reset, so set it explicitly. |
| 118 | outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH); |
Kevin O'Connor | 2e3eeeb | 2008-06-12 22:29:30 -0400 | [diff] [blame] | 119 | } |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 120 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 121 | // On a user-reset request, wait for RDY if it is an ATA device. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 122 | u8 type=GET_GLOBAL(drive_g->type); |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 123 | if (type == DTYPE_ATA) |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 124 | status = await_rdy(iobase1); |
Kevin O'Connor | 3e1b649 | 2008-05-26 15:06:40 -0400 | [diff] [blame] | 125 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 126 | done: |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 127 | // Enable interrupts |
| 128 | outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 129 | |
| 130 | dprintf(6, "ata_reset exit status=%x\n", status); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 133 | // Check for drive RDY for 16bit interface command. |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 134 | static int |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 135 | isready(struct drive_s *drive_g) |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 136 | { |
| 137 | // Read the status from controller |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 138 | u8 ataid = GET_GLOBAL(drive_g->cntl_id); |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 139 | u8 channel = ataid / 2; |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 140 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 141 | u8 status = inb(iobase1 + ATA_CB_STAT); |
Kevin O'Connor | 126eac6 | 2009-08-16 13:32:24 -0400 | [diff] [blame] | 142 | if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY) |
| 143 | return DISK_RET_SUCCESS; |
| 144 | return DISK_RET_ENOTREADY; |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 147 | // Default 16bit command demuxer for ATA and ATAPI devices. |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 148 | static int |
| 149 | process_ata_misc_op(struct disk_op_s *op) |
| 150 | { |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 151 | if (!CONFIG_ATA) |
| 152 | return 0; |
| 153 | |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 154 | switch (op->command) { |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 155 | case CMD_RESET: |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 156 | ata_reset(op->drive_g); |
Kevin O'Connor | 126eac6 | 2009-08-16 13:32:24 -0400 | [diff] [blame] | 157 | return DISK_RET_SUCCESS; |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 158 | case CMD_ISREADY: |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 159 | return isready(op->drive_g); |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 160 | case CMD_FORMAT: |
| 161 | case CMD_VERIFY: |
| 162 | case CMD_SEEK: |
| 163 | return DISK_RET_SUCCESS; |
| 164 | default: |
| 165 | op->count = 0; |
| 166 | return DISK_RET_EPARAM; |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 170 | |
| 171 | /**************************************************************** |
| 172 | * ATA send command |
| 173 | ****************************************************************/ |
| 174 | |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 175 | struct ata_pio_command { |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 176 | u8 feature; |
| 177 | u8 sector_count; |
| 178 | u8 lba_low; |
| 179 | u8 lba_mid; |
| 180 | u8 lba_high; |
| 181 | u8 device; |
| 182 | u8 command; |
| 183 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 184 | u8 feature2; |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 185 | u8 sector_count2; |
| 186 | u8 lba_low2; |
| 187 | u8 lba_mid2; |
| 188 | u8 lba_high2; |
| 189 | }; |
| 190 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 191 | // Send an ata command to the drive. |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 192 | static int |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 193 | send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 194 | { |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 195 | u8 ataid = GET_GLOBAL(drive_g->cntl_id); |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 196 | u8 channel = ataid / 2; |
| 197 | u8 slave = ataid % 2; |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 198 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
Kevin O'Connor | ef2822a | 2008-06-07 15:23:11 -0400 | [diff] [blame] | 199 | |
| 200 | // Select device |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 201 | int status = await_not_bsy(iobase1); |
| 202 | if (status < 0) |
| 203 | return status; |
| 204 | u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1) |
| 205 | | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)); |
| 206 | u8 olddh = inb(iobase1 + ATA_CB_DH); |
| 207 | outb(newdh, iobase1 + ATA_CB_DH); |
| 208 | if ((olddh ^ newdh) & (1<<4)) { |
| 209 | // Was a device change - wait for device to become not busy. |
Kevin O'Connor | c946eca | 2009-05-24 13:55:01 -0400 | [diff] [blame] | 210 | status = ndelay_await_not_bsy(iobase1); |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 211 | if (status < 0) |
| 212 | return status; |
| 213 | } |
Kevin O'Connor | ef2822a | 2008-06-07 15:23:11 -0400 | [diff] [blame] | 214 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 215 | // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands. |
| 216 | if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) { |
| 217 | outb(cmd->feature2, iobase1 + ATA_CB_FR); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 218 | outb(cmd->sector_count2, iobase1 + ATA_CB_SC); |
| 219 | outb(cmd->lba_low2, iobase1 + ATA_CB_SN); |
| 220 | outb(cmd->lba_mid2, iobase1 + ATA_CB_CL); |
| 221 | outb(cmd->lba_high2, iobase1 + ATA_CB_CH); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 222 | } |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 223 | outb(cmd->feature, iobase1 + ATA_CB_FR); |
| 224 | outb(cmd->sector_count, iobase1 + ATA_CB_SC); |
| 225 | outb(cmd->lba_low, iobase1 + ATA_CB_SN); |
| 226 | outb(cmd->lba_mid, iobase1 + ATA_CB_CL); |
| 227 | outb(cmd->lba_high, iobase1 + ATA_CB_CH); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 228 | outb(cmd->command, iobase1 + ATA_CB_CMD); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 229 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | // Wait for data after calling 'send_cmd'. |
| 234 | static int |
| 235 | ata_wait_data(u16 iobase1) |
| 236 | { |
| 237 | int status = ndelay_await_not_bsy(iobase1); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 238 | if (status < 0) |
| 239 | return status; |
| 240 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 241 | if (status & ATA_CB_STAT_ERR) { |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 242 | dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n" |
| 243 | , status, inb(iobase1 + ATA_CB_ERR)); |
Kevin O'Connor | a05223c | 2008-06-28 12:15:57 -0400 | [diff] [blame] | 244 | return -4; |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 245 | } |
| 246 | if (!(status & ATA_CB_STAT_DRQ)) { |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 247 | dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status); |
Kevin O'Connor | a05223c | 2008-06-28 12:15:57 -0400 | [diff] [blame] | 248 | return -5; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 254 | // Send an ata command that does not transfer any further data. |
| 255 | int |
| 256 | ata_cmd_nondata(struct drive_s *drive_g, struct ata_pio_command *cmd) |
| 257 | { |
| 258 | u8 ataid = GET_GLOBAL(drive_g->cntl_id); |
| 259 | u8 channel = ataid / 2; |
| 260 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
| 261 | u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); |
| 262 | |
| 263 | // Disable interrupts |
| 264 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); |
| 265 | |
| 266 | int ret = send_cmd(drive_g, cmd); |
| 267 | if (ret) |
| 268 | goto fail; |
| 269 | ret = ndelay_await_not_bsy(iobase1); |
| 270 | if (ret < 0) |
| 271 | goto fail; |
| 272 | |
| 273 | if (ret & ATA_CB_STAT_ERR) { |
| 274 | dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n" |
| 275 | , ret, inb(iobase1 + ATA_CB_ERR)); |
| 276 | ret = -4; |
| 277 | goto fail; |
| 278 | } |
| 279 | if (ret & ATA_CB_STAT_DRQ) { |
| 280 | dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret); |
| 281 | ret = -5; |
| 282 | goto fail; |
| 283 | } |
| 284 | |
| 285 | fail: |
| 286 | // Enable interrupts |
| 287 | outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); |
| 288 | |
| 289 | return ret; |
| 290 | } |
| 291 | |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 292 | |
| 293 | /**************************************************************** |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 294 | * ATA PIO transfers |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 295 | ****************************************************************/ |
| 296 | |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 297 | // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 298 | // 'op->drive_g'. |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 299 | static int |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 300 | ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize) |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 301 | { |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 302 | dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n" |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 303 | , op->drive_g, iswrite, op->count, blocksize, op->buf_fl); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 304 | |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 305 | u8 ataid = GET_GLOBAL(op->drive_g->cntl_id); |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 306 | u8 channel = ataid / 2; |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 307 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
| 308 | u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 309 | int count = op->count; |
| 310 | void *buf_fl = op->buf_fl; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 311 | int status; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 312 | for (;;) { |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 313 | if (iswrite) { |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 314 | // Write data to controller |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 315 | dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl); |
Kevin O'Connor | 32945af | 2009-02-27 21:23:01 -0500 | [diff] [blame] | 316 | if (CONFIG_ATA_PIO32) |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 317 | outsl_fl(iobase1, buf_fl, blocksize / 4); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 318 | else |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 319 | outsw_fl(iobase1, buf_fl, blocksize / 2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 320 | } else { |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 321 | // Read data from controller |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 322 | dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl); |
Kevin O'Connor | 32945af | 2009-02-27 21:23:01 -0500 | [diff] [blame] | 323 | if (CONFIG_ATA_PIO32) |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 324 | insl_fl(iobase1, buf_fl, blocksize / 4); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 325 | else |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 326 | insw_fl(iobase1, buf_fl, blocksize / 2); |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 327 | } |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 328 | buf_fl += blocksize; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 329 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 330 | status = pause_await_not_bsy(iobase1, iobase2); |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 331 | if (status < 0) { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 332 | // Error |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 333 | op->count -= count; |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 334 | return status; |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 335 | } |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 336 | |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 337 | count--; |
| 338 | if (!count) |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 339 | break; |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 340 | status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR); |
| 341 | if (status != ATA_CB_STAT_DRQ) { |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 342 | dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n" |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 343 | , status); |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 344 | op->count -= count; |
Kevin O'Connor | a05223c | 2008-06-28 12:15:57 -0400 | [diff] [blame] | 345 | return -6; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 346 | } |
| 347 | } |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 348 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 349 | status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ |
| 350 | | ATA_CB_STAT_ERR); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 351 | if (!iswrite) |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 352 | status &= ~ATA_CB_STAT_DF; |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 353 | if (status != 0) { |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 354 | dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status); |
Kevin O'Connor | a05223c | 2008-06-28 12:15:57 -0400 | [diff] [blame] | 355 | return -7; |
Kevin O'Connor | 3a04963 | 2008-03-11 11:48:04 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 361 | |
| 362 | /**************************************************************** |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 363 | * ATA DMA transfers |
| 364 | ****************************************************************/ |
| 365 | |
| 366 | #define BM_CMD 0 |
| 367 | #define BM_CMD_MEMWRITE 0x08 |
| 368 | #define BM_CMD_START 0x01 |
| 369 | #define BM_STATUS 2 |
| 370 | #define BM_STATUS_IRQ 0x04 |
| 371 | #define BM_STATUS_ERROR 0x02 |
| 372 | #define BM_STATUS_ACTIVE 0x01 |
| 373 | #define BM_TABLE 4 |
| 374 | |
| 375 | struct sff_dma_prd { |
| 376 | u32 buf_fl; |
| 377 | u32 count; |
| 378 | }; |
| 379 | |
| 380 | // Check if DMA available and setup transfer if so. |
| 381 | static int |
| 382 | ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize) |
| 383 | { |
| 384 | u32 dest = (u32)op->buf_fl; |
| 385 | if (dest & 1) |
| 386 | // Need minimum alignment of 1. |
| 387 | return -1; |
| 388 | u8 ataid = GET_GLOBAL(op->drive_g->cntl_id); |
| 389 | u8 channel = ataid / 2; |
| 390 | u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster); |
| 391 | if (! iomaster) |
| 392 | return -1; |
| 393 | u32 bytes = op->count * blocksize; |
| 394 | if (! bytes) |
| 395 | return -1; |
| 396 | |
| 397 | // Build PRD dma structure. |
| 398 | struct sff_dma_prd *dma = MAKE_FLATPTR( |
| 399 | get_ebda_seg() |
| 400 | , (void*)offsetof(struct extended_bios_data_area_s, extra_stack)); |
| 401 | struct sff_dma_prd *origdma = dma; |
| 402 | while (bytes) { |
| 403 | if (dma >= &origdma[16]) |
| 404 | // Too many descriptors.. |
| 405 | return -1; |
| 406 | u32 count = bytes; |
| 407 | if (count > 0x10000) |
| 408 | count = 0x10000; |
| 409 | u32 max = 0x10000 - (dest & 0xffff); |
| 410 | if (count > max) |
| 411 | count = max; |
| 412 | |
| 413 | SET_FLATPTR(dma->buf_fl, dest); |
| 414 | bytes -= count; |
| 415 | if (!bytes) |
| 416 | // Last descriptor. |
| 417 | count |= 1<<31; |
| 418 | dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count); |
| 419 | dest += count; |
| 420 | SET_FLATPTR(dma->count, count); |
| 421 | dma++; |
| 422 | } |
| 423 | |
| 424 | // Program bus-master controller. |
| 425 | outl((u32)origdma, iomaster + BM_TABLE); |
| 426 | u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START); |
| 427 | outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD); |
| 428 | outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS); |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | // Transfer data using DMA. |
| 434 | static int |
| 435 | ata_dma_transfer(struct disk_op_s *op) |
| 436 | { |
| 437 | dprintf(16, "ata_dma_transfer id=%p buf=%p\n" |
| 438 | , op->drive_g, op->buf_fl); |
| 439 | |
| 440 | u8 ataid = GET_GLOBAL(op->drive_g->cntl_id); |
| 441 | u8 channel = ataid / 2; |
| 442 | u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster); |
| 443 | |
| 444 | // Start bus-master controller. |
| 445 | u8 oldcmd = inb(iomaster + BM_CMD); |
| 446 | outb(oldcmd | BM_CMD_START, iomaster + BM_CMD); |
| 447 | |
| 448 | u64 end = calc_future_tsc(IDE_TIMEOUT); |
| 449 | u8 status; |
| 450 | for (;;) { |
| 451 | status = inb(iomaster + BM_STATUS); |
| 452 | if (status & BM_STATUS_IRQ) |
| 453 | break; |
| 454 | // Transfer in progress |
| 455 | if (check_time(end)) { |
| 456 | // Timeout. |
| 457 | dprintf(1, "IDE DMA timeout\n"); |
| 458 | break; |
| 459 | } |
| 460 | yield(); |
| 461 | } |
| 462 | outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD); |
| 463 | |
| 464 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
| 465 | u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); |
| 466 | int idestatus = pause_await_not_bsy(iobase1, iobase2); |
| 467 | |
| 468 | if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ |
| 469 | && idestatus >= 0x00 |
| 470 | && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ |
| 471 | | ATA_CB_STAT_ERR)) == 0x00) |
| 472 | // Success. |
| 473 | return 0; |
| 474 | |
| 475 | dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus |
| 476 | , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR)); |
| 477 | op->count = 0; |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /**************************************************************** |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 483 | * ATA hard drive functions |
| 484 | ****************************************************************/ |
| 485 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 486 | // Transfer data to harddrive using PIO protocol. |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 487 | static int |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 488 | ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd) |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 489 | { |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 490 | u8 ataid = GET_GLOBAL(op->drive_g->cntl_id); |
| 491 | u8 channel = ataid / 2; |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 492 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 493 | u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 494 | |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 495 | // Disable interrupts |
| 496 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); |
| 497 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 498 | int ret = send_cmd(op->drive_g, cmd); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 499 | if (ret) |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 500 | goto fail; |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 501 | ret = ata_wait_data(iobase1); |
| 502 | if (ret) |
| 503 | goto fail; |
| 504 | ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE); |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 505 | |
| 506 | fail: |
| 507 | // Enable interrupts |
| 508 | outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); |
| 509 | return ret; |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 512 | // Transfer data to harddrive using DMA protocol. |
| 513 | static int |
| 514 | ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd) |
| 515 | { |
| 516 | int ret = send_cmd(op->drive_g, cmd); |
| 517 | if (ret) |
| 518 | return ret; |
| 519 | return ata_dma_transfer(op); |
| 520 | } |
| 521 | |
| 522 | // Read/write count blocks from a harddrive. |
| 523 | static int |
| 524 | ata_readwrite(struct disk_op_s *op, int iswrite) |
| 525 | { |
| 526 | u64 lba = op->lba; |
| 527 | |
| 528 | int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE); |
| 529 | |
| 530 | struct ata_pio_command cmd; |
| 531 | memset(&cmd, 0, sizeof(cmd)); |
| 532 | |
| 533 | if (op->count >= (1<<8) || lba + op->count >= (1<<28)) { |
| 534 | cmd.sector_count2 = op->count >> 8; |
| 535 | cmd.lba_low2 = lba >> 24; |
| 536 | cmd.lba_mid2 = lba >> 32; |
| 537 | cmd.lba_high2 = lba >> 40; |
| 538 | lba &= 0xffffff; |
| 539 | |
| 540 | if (usepio) |
| 541 | cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT |
| 542 | : ATA_CMD_READ_SECTORS_EXT); |
| 543 | else |
| 544 | cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT |
| 545 | : ATA_CMD_READ_DMA_EXT); |
| 546 | } else { |
| 547 | if (usepio) |
| 548 | cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS |
| 549 | : ATA_CMD_READ_SECTORS); |
| 550 | else |
| 551 | cmd.command = (iswrite ? ATA_CMD_WRITE_DMA |
| 552 | : ATA_CMD_READ_DMA); |
| 553 | } |
| 554 | |
| 555 | cmd.sector_count = op->count; |
| 556 | cmd.lba_low = lba; |
| 557 | cmd.lba_mid = lba >> 8; |
| 558 | cmd.lba_high = lba >> 16; |
| 559 | cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA; |
| 560 | |
| 561 | int ret; |
| 562 | if (usepio) |
| 563 | ret = ata_pio_cmd_data(op, iswrite, &cmd); |
| 564 | else |
| 565 | ret = ata_dma_cmd_data(op, &cmd); |
| 566 | if (ret) |
| 567 | return DISK_RET_EBADTRACK; |
| 568 | return DISK_RET_SUCCESS; |
| 569 | } |
| 570 | |
| 571 | // 16bit command demuxer for ATA harddrives. |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 572 | int |
| 573 | process_ata_op(struct disk_op_s *op) |
| 574 | { |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 575 | if (!CONFIG_ATA) |
| 576 | return 0; |
| 577 | |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 578 | switch (op->command) { |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 579 | case CMD_READ: |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 580 | return ata_readwrite(op, 0); |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 581 | case CMD_WRITE: |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 582 | return ata_readwrite(op, 1); |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 583 | default: |
| 584 | return process_ata_misc_op(op); |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 585 | } |
Kevin O'Connor | f888f8c | 2008-03-23 00:04:54 -0400 | [diff] [blame] | 586 | } |
| 587 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 588 | |
| 589 | /**************************************************************** |
| 590 | * ATAPI functions |
| 591 | ****************************************************************/ |
| 592 | |
| 593 | // Low-level atapi command transmit function. |
Kevin O'Connor | a9caeae | 2009-03-07 00:09:52 -0500 | [diff] [blame] | 594 | static int |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 595 | atapi_cmd_data(struct disk_op_s *op, u8 *cmdbuf, u8 cmdlen, u16 blocksize) |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 596 | { |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 597 | u8 ataid = GET_GLOBAL(op->drive_g->cntl_id); |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 598 | u8 channel = ataid / 2; |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 599 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
| 600 | u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 601 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 602 | struct ata_pio_command cmd; |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 603 | memset(&cmd, 0, sizeof(cmd)); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 604 | cmd.lba_mid = blocksize; |
| 605 | cmd.lba_high = blocksize >> 8; |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 606 | cmd.command = ATA_CMD_PACKET; |
| 607 | |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 608 | // Disable interrupts |
| 609 | outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); |
| 610 | |
| 611 | int ret = send_cmd(op->drive_g, &cmd); |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 612 | if (ret) |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 613 | goto fail; |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 614 | ret = ata_wait_data(iobase1); |
| 615 | if (ret) |
| 616 | goto fail; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 617 | |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 618 | // Send command to device |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 619 | outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2); |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 620 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 621 | int status = pause_await_not_bsy(iobase1, iobase2); |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 622 | if (status < 0) { |
| 623 | ret = status; |
| 624 | goto fail; |
| 625 | } |
Kevin O'Connor | 1fcf144 | 2008-03-11 19:42:41 -0400 | [diff] [blame] | 626 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 627 | if (status & ATA_CB_STAT_ERR) { |
Kevin O'Connor | b30c400 | 2009-05-05 21:47:20 -0400 | [diff] [blame] | 628 | u8 err = inb(iobase1 + ATA_CB_ERR); |
| 629 | // skip "Not Ready" |
| 630 | if (err != 0x20) |
| 631 | dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n" |
| 632 | , status, err); |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 633 | ret = -2; |
| 634 | goto fail; |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 635 | } |
| 636 | if (!(status & ATA_CB_STAT_DRQ)) { |
| 637 | dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status); |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 638 | ret = -3; |
| 639 | goto fail; |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 640 | } |
| 641 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 642 | ret = ata_pio_transfer(op, 0, blocksize); |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 643 | |
| 644 | fail: |
| 645 | // Enable interrupts |
| 646 | outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); |
| 647 | return ret; |
Kevin O'Connor | 3491e8b | 2008-02-29 00:22:27 -0500 | [diff] [blame] | 648 | } |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 649 | |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 650 | // Read sectors from the cdrom. |
| 651 | int |
| 652 | cdrom_read(struct disk_op_s *op) |
Kevin O'Connor | 180a959 | 2008-03-04 22:50:53 -0500 | [diff] [blame] | 653 | { |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 654 | u8 atacmd[12]; |
| 655 | memset(atacmd, 0, sizeof(atacmd)); |
Kevin O'Connor | ee55c76 | 2008-05-13 00:18:20 -0400 | [diff] [blame] | 656 | atacmd[0]=0x28; // READ command |
| 657 | atacmd[7]=(op->count & 0xff00) >> 8; // Sectors |
| 658 | atacmd[8]=(op->count & 0x00ff); |
| 659 | atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA |
| 660 | atacmd[3]=(op->lba & 0x00ff0000) >> 16; |
| 661 | atacmd[4]=(op->lba & 0x0000ff00) >> 8; |
| 662 | atacmd[5]=(op->lba & 0x000000ff); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 663 | |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 664 | return atapi_cmd_data(op, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE); |
Kevin O'Connor | aa2590c | 2008-03-22 23:13:24 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 667 | // 16bit command demuxer for ATAPI cdroms. |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 668 | int |
| 669 | process_atapi_op(struct disk_op_s *op) |
| 670 | { |
Kevin O'Connor | 126eac6 | 2009-08-16 13:32:24 -0400 | [diff] [blame] | 671 | int ret; |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 672 | switch (op->command) { |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 673 | case CMD_READ: |
Kevin O'Connor | 126eac6 | 2009-08-16 13:32:24 -0400 | [diff] [blame] | 674 | ret = cdrom_read(op); |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 675 | if (ret) |
| 676 | return DISK_RET_EBADTRACK; |
| 677 | return DISK_RET_SUCCESS; |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 678 | case CMD_FORMAT: |
| 679 | case CMD_WRITE: |
| 680 | return DISK_RET_EWRITEPROTECT; |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 681 | default: |
| 682 | return process_ata_misc_op(op); |
Kevin O'Connor | 3f6c278 | 2009-08-09 19:17:11 -0400 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 686 | // Send a simple atapi command to a drive. |
| 687 | int |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 688 | ata_cmd_packet(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 689 | , u32 length, void *buf_fl) |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 690 | { |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 691 | struct disk_op_s dop; |
| 692 | memset(&dop, 0, sizeof(dop)); |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 693 | dop.drive_g = drive_g; |
Kevin O'Connor | 9ae1e9b | 2009-08-09 18:06:40 -0400 | [diff] [blame] | 694 | dop.count = 1; |
| 695 | dop.buf_fl = buf_fl; |
| 696 | |
Kevin O'Connor | 42bc394 | 2009-11-20 17:28:19 -0500 | [diff] [blame] | 697 | return atapi_cmd_data(&dop, cmdbuf, cmdlen, length); |
Kevin O'Connor | a6b9f71 | 2008-03-29 12:53:57 -0400 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | |
| 701 | /**************************************************************** |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 702 | * ATA detect and init |
| 703 | ****************************************************************/ |
| 704 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 705 | // Send an identify device or identify device packet command. |
| 706 | static int |
| 707 | send_ata_identity(struct drive_s *drive_g, u16 *buffer, int command) |
| 708 | { |
| 709 | memset(buffer, 0, DISK_SECTOR_SIZE); |
| 710 | |
| 711 | struct disk_op_s dop; |
| 712 | memset(&dop, 0, sizeof(dop)); |
| 713 | dop.drive_g = drive_g; |
| 714 | dop.count = 1; |
| 715 | dop.lba = 1; |
| 716 | dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer); |
| 717 | |
| 718 | struct ata_pio_command cmd; |
| 719 | memset(&cmd, 0, sizeof(cmd)); |
| 720 | cmd.command = command; |
| 721 | |
| 722 | return ata_pio_cmd_data(&dop, 0, &cmd); |
| 723 | } |
| 724 | |
Kevin O'Connor | f2d48a3 | 2009-08-11 20:58:11 -0400 | [diff] [blame] | 725 | // Extract the ATA/ATAPI version info. |
| 726 | static int |
| 727 | extract_version(u16 *buffer) |
| 728 | { |
| 729 | // Extract ATA/ATAPI version. |
| 730 | u16 ataversion = buffer[80]; |
| 731 | u8 version; |
| 732 | for (version=15; version>0; version--) |
| 733 | if (ataversion & (1<<version)) |
| 734 | break; |
| 735 | return version; |
| 736 | } |
| 737 | |
Kevin O'Connor | 160f71c | 2009-02-11 22:46:29 -0500 | [diff] [blame] | 738 | // Extract common information from IDENTIFY commands. |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 739 | static void |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 740 | extract_identify(struct drive_s *drive_g, u16 *buffer) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 741 | { |
Kevin O'Connor | 160f71c | 2009-02-11 22:46:29 -0500 | [diff] [blame] | 742 | 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] | 743 | |
| 744 | // Read model name |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 745 | char *model = drive_g->model; |
| 746 | int maxsize = ARRAY_SIZE(drive_g->model); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 747 | int i; |
Kevin O'Connor | ab51560 | 2009-02-11 22:22:15 -0500 | [diff] [blame] | 748 | for (i=0; i<maxsize/2; i++) { |
| 749 | u16 v = buffer[27+i]; |
| 750 | model[i*2] = v >> 8; |
| 751 | model[i*2+1] = v & 0xff; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 752 | } |
| 753 | model[maxsize-1] = 0x00; |
| 754 | |
Kevin O'Connor | 160f71c | 2009-02-11 22:46:29 -0500 | [diff] [blame] | 755 | // Trim trailing spaces from model name. |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 756 | for (i=maxsize-2; i>0 && model[i] == 0x20; i--) |
| 757 | model[i] = 0x00; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 758 | |
Kevin O'Connor | 160f71c | 2009-02-11 22:46:29 -0500 | [diff] [blame] | 759 | // Common flags. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 760 | SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0); |
| 761 | SET_GLOBAL(drive_g->cntl_info, extract_version(buffer)); |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 762 | } |
| 763 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 764 | // Print out a description of the given atapi drive. |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 765 | void |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 766 | describe_atapi(struct drive_s *drive_g) |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 767 | { |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 768 | u8 ataid = drive_g->cntl_id; |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 769 | u8 channel = ataid / 2; |
| 770 | u8 slave = ataid % 2; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 771 | u8 version = drive_g->cntl_info; |
| 772 | int iscd = drive_g->floppy_type; |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 773 | printf("ata%d-%d: %s ATAPI-%d %s", channel, slave |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 774 | , drive_g->model, version |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 775 | , (iscd ? "CD-Rom/DVD-Rom" : "Device")); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 776 | } |
| 777 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 778 | // Detect if the given drive is an atapi - initialize it if so. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 779 | static struct drive_s * |
| 780 | init_drive_atapi(struct drive_s *dummy, u16 *buffer) |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 781 | { |
| 782 | // Send an IDENTIFY_DEVICE_PACKET command to device |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 783 | int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 784 | if (ret) |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 785 | return NULL; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 786 | |
| 787 | // Success - setup as ATAPI. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 788 | struct drive_s *drive_g = allocDrive(); |
| 789 | if (! drive_g) |
| 790 | return NULL; |
| 791 | SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id); |
| 792 | extract_identify(drive_g, buffer); |
| 793 | SET_GLOBAL(drive_g->type, DTYPE_ATAPI); |
| 794 | SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE); |
| 795 | SET_GLOBAL(drive_g->sectors, (u64)-1); |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 796 | u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 797 | SET_GLOBAL(drive_g->floppy_type, iscd); |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 798 | |
| 799 | // fill cdidmap |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 800 | if (iscd) |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 801 | map_cd_drive(drive_g); |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 802 | |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 803 | return drive_g; |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 804 | } |
| 805 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 806 | // Print out a description of the given ata drive. |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 807 | void |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 808 | describe_ata(struct drive_s *drive_g) |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 809 | { |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 810 | u8 ataid = drive_g->cntl_id; |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 811 | u8 channel = ataid / 2; |
| 812 | u8 slave = ataid % 2; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 813 | u64 sectors = drive_g->sectors; |
| 814 | u8 version = drive_g->cntl_info; |
| 815 | char *model = drive_g->model; |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 816 | printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version); |
| 817 | u64 sizeinmb = sectors >> 11; |
| 818 | if (sizeinmb < (1 << 16)) |
| 819 | printf(" (%u MiBytes)", (u32)sizeinmb); |
| 820 | else |
| 821 | printf(" (%u GiBytes)", (u32)(sizeinmb >> 10)); |
| 822 | } |
| 823 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 824 | // Detect if the given drive is a regular ata drive - initialize it if so. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 825 | static struct drive_s * |
| 826 | init_drive_ata(struct drive_s *dummy, u16 *buffer) |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 827 | { |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 828 | // Send an IDENTIFY_DEVICE command to device |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 829 | int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE); |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 830 | if (ret) |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 831 | return NULL; |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 832 | |
| 833 | // Success - setup as ATA. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 834 | struct drive_s *drive_g = allocDrive(); |
| 835 | if (! drive_g) |
| 836 | return NULL; |
| 837 | SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id); |
| 838 | extract_identify(drive_g, buffer); |
| 839 | SET_GLOBAL(drive_g->type, DTYPE_ATA); |
| 840 | SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE); |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 841 | |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 842 | SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]); |
| 843 | SET_GLOBAL(drive_g->pchs.heads, buffer[3]); |
| 844 | SET_GLOBAL(drive_g->pchs.spt, buffer[6]); |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 845 | |
| 846 | u64 sectors; |
Kevin O'Connor | ab51560 | 2009-02-11 22:22:15 -0500 | [diff] [blame] | 847 | if (buffer[83] & (1 << 10)) // word 83 - lba48 support |
| 848 | sectors = *(u64*)&buffer[100]; // word 100-103 |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 849 | else |
Kevin O'Connor | ab51560 | 2009-02-11 22:22:15 -0500 | [diff] [blame] | 850 | sectors = *(u32*)&buffer[60]; // word 60 and word 61 |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 851 | SET_GLOBAL(drive_g->sectors, sectors); |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 852 | |
| 853 | // Setup disk geometry translation. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 854 | setup_translation(drive_g); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 855 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 856 | // Register with bcv system. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 857 | add_bcv_internal(drive_g); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 858 | |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 859 | return drive_g; |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 860 | } |
| 861 | |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 862 | static u64 SpinupEnd; |
| 863 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 864 | // Wait for non-busy status and check for "floating bus" condition. |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 865 | static int |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 866 | powerup_await_non_bsy(u16 base) |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 867 | { |
| 868 | u8 orstatus = 0; |
| 869 | u8 status; |
| 870 | for (;;) { |
| 871 | status = inb(base+ATA_CB_STAT); |
| 872 | if (!(status & ATA_CB_STAT_BSY)) |
| 873 | break; |
| 874 | orstatus |= status; |
| 875 | if (orstatus == 0xff) { |
| 876 | dprintf(1, "powerup IDE floating\n"); |
| 877 | return orstatus; |
| 878 | } |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 879 | if (check_time(SpinupEnd)) { |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 880 | dprintf(1, "powerup IDE time out\n"); |
| 881 | return -1; |
| 882 | } |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 883 | yield(); |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 884 | } |
| 885 | dprintf(6, "powerup iobase=%x st=%x\n", base, status); |
| 886 | return status; |
| 887 | } |
| 888 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 889 | // Detect any drives attached to a given controller. |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 890 | static void |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 891 | ata_detect(void *data) |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 892 | { |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 893 | struct ata_channel_s *atachannel = data; |
| 894 | int startid = (atachannel - ATA_channels) * 2; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 895 | struct drive_s dummy; |
| 896 | memset(&dummy, 0, sizeof(dummy)); |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 897 | // Device detection |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 898 | int ataid, last_reset_ataid=-1; |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 899 | for (ataid=startid; ataid<startid+2; ataid++) { |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 900 | u8 channel = ataid / 2; |
| 901 | u8 slave = ataid % 2; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 902 | |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 903 | u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1); |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 904 | if (!iobase1) |
| 905 | break; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 906 | |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 907 | // Wait for not-bsy. |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 908 | int status = powerup_await_non_bsy(iobase1); |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 909 | if (status < 0) |
| 910 | continue; |
| 911 | u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0; |
| 912 | outb(newdh, iobase1+ATA_CB_DH); |
Kevin O'Connor | c946eca | 2009-05-24 13:55:01 -0400 | [diff] [blame] | 913 | ndelay(400); |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 914 | status = powerup_await_non_bsy(iobase1); |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 915 | if (status < 0) |
| 916 | continue; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 917 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 918 | // Check if ioport registers look valid. |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 919 | outb(newdh, iobase1+ATA_CB_DH); |
| 920 | u8 dh = inb(iobase1+ATA_CB_DH); |
| 921 | outb(0x55, iobase1+ATA_CB_SC); |
| 922 | outb(0xaa, iobase1+ATA_CB_SN); |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 923 | u8 sc = inb(iobase1+ATA_CB_SC); |
| 924 | u8 sn = inb(iobase1+ATA_CB_SN); |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 925 | dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n" |
| 926 | , ataid, sc, sn, dh); |
Kevin O'Connor | 425f212 | 2009-04-18 12:23:00 -0400 | [diff] [blame] | 927 | if (sc != 0x55 || sn != 0xaa || dh != newdh) |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 928 | continue; |
| 929 | |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 930 | // Prepare new drive. |
| 931 | dummy.cntl_id = ataid; |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 932 | |
Kevin O'Connor | aafa657 | 2008-03-13 19:57:49 -0400 | [diff] [blame] | 933 | // reset the channel |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 934 | if (slave && ataid == last_reset_ataid + 1) { |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 935 | // The drive was just reset - no need to reset it again. |
| 936 | } else { |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 937 | ata_reset(&dummy); |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 938 | last_reset_ataid = ataid; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 939 | } |
| 940 | |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 941 | // check for ATAPI |
Kevin O'Connor | c79637b | 2009-06-10 20:33:57 -0400 | [diff] [blame] | 942 | u16 buffer[256]; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 943 | struct drive_s *drive_g = init_drive_atapi(&dummy, buffer); |
| 944 | if (!drive_g) { |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 945 | // Didn't find an ATAPI drive - look for ATA drive. |
Kevin O'Connor | c79637b | 2009-06-10 20:33:57 -0400 | [diff] [blame] | 946 | u8 st = inb(iobase1+ATA_CB_STAT); |
| 947 | if (!st) |
| 948 | // Status not set - can't be a valid drive. |
| 949 | continue; |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 950 | |
Kevin O'Connor | c79637b | 2009-06-10 20:33:57 -0400 | [diff] [blame] | 951 | // Wait for RDY. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 952 | int ret = await_rdy(iobase1); |
Kevin O'Connor | c79637b | 2009-06-10 20:33:57 -0400 | [diff] [blame] | 953 | if (ret < 0) |
| 954 | continue; |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 955 | |
Kevin O'Connor | c79637b | 2009-06-10 20:33:57 -0400 | [diff] [blame] | 956 | // check for ATA. |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 957 | drive_g = init_drive_ata(&dummy, buffer); |
| 958 | if (!drive_g) |
Kevin O'Connor | c79637b | 2009-06-10 20:33:57 -0400 | [diff] [blame] | 959 | // No ATA drive found |
| 960 | continue; |
| 961 | } |
Kevin O'Connor | 580e332 | 2009-02-06 22:36:53 -0500 | [diff] [blame] | 962 | |
Kevin O'Connor | c79637b | 2009-06-10 20:33:57 -0400 | [diff] [blame] | 963 | u16 resetresult = buffer[93]; |
| 964 | dprintf(6, "ata_detect resetresult=%04x\n", resetresult); |
| 965 | if (!slave && (resetresult & 0xdf61) == 0x4041) |
| 966 | // resetresult looks valid and device 0 is responding to |
| 967 | // device 1 requests - device 1 must not be present - skip |
| 968 | // detection. |
Kevin O'Connor | b114436 | 2009-08-11 20:43:38 -0400 | [diff] [blame] | 969 | ataid++; |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 970 | } |
Kevin O'Connor | 15aee2e | 2008-03-01 13:34:04 -0500 | [diff] [blame] | 971 | } |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 972 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 973 | // Initialize an ata controller and detect its drives. |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 974 | static void |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 975 | init_controller(struct ata_channel_s *atachannel |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 976 | , int bdf, int irq, u32 port1, u32 port2, u32 master) |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 977 | { |
| 978 | SET_GLOBAL(atachannel->irq, irq); |
| 979 | SET_GLOBAL(atachannel->pci_bdf, bdf); |
| 980 | SET_GLOBAL(atachannel->iobase1, port1); |
| 981 | SET_GLOBAL(atachannel->iobase2, port2); |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 982 | SET_GLOBAL(atachannel->iomaster, master); |
| 983 | dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n" |
| 984 | , atachannel - ATA_channels, port1, port2, master, irq, bdf); |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 985 | run_thread(ata_detect, atachannel); |
| 986 | } |
| 987 | |
Kevin O'Connor | 525219b | 2009-12-05 13:36:18 -0500 | [diff] [blame] | 988 | #define IRQ_ATA1 14 |
| 989 | #define IRQ_ATA2 15 |
| 990 | |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 991 | // Locate and init ata controllers. |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 992 | static void |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 993 | ata_init() |
| 994 | { |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 995 | // Scan PCI bus for ATA adapters |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 996 | int count=0, pcicount=0; |
Kevin O'Connor | 53ab0b6 | 2008-12-04 19:22:49 -0500 | [diff] [blame] | 997 | int bdf, max; |
Kevin O'Connor | 4132e02 | 2008-12-04 19:39:10 -0500 | [diff] [blame] | 998 | foreachpci(bdf, max) { |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 999 | pcicount++; |
Kevin O'Connor | 53ab0b6 | 2008-12-04 19:22:49 -0500 | [diff] [blame] | 1000 | 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] | 1001 | continue; |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 1002 | if (count >= ARRAY_SIZE(ATA_channels)) |
Kevin O'Connor | 89f6763 | 2009-02-11 20:16:49 -0500 | [diff] [blame] | 1003 | break; |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 1004 | |
Kevin O'Connor | 525219b | 2009-12-05 13:36:18 -0500 | [diff] [blame] | 1005 | u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE); |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 1006 | u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG); |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 1007 | int master = 0; |
| 1008 | if (prog_if & 0x80) { |
| 1009 | // Check for bus-mastering. |
| 1010 | u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4); |
| 1011 | if (bar & PCI_BASE_ADDRESS_SPACE_IO) { |
| 1012 | master = bar & PCI_BASE_ADDRESS_IO_MASK; |
| 1013 | pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER); |
| 1014 | } |
| 1015 | } |
| 1016 | |
Kevin O'Connor | 525219b | 2009-12-05 13:36:18 -0500 | [diff] [blame] | 1017 | u32 port1, port2, irq; |
Kevin O'Connor | 53ab0b6 | 2008-12-04 19:22:49 -0500 | [diff] [blame] | 1018 | if (prog_if & 1) { |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 1019 | port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3; |
| 1020 | port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3; |
Kevin O'Connor | 525219b | 2009-12-05 13:36:18 -0500 | [diff] [blame] | 1021 | irq = pciirq; |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 1022 | } else { |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 1023 | port1 = PORT_ATA1_CMD_BASE; |
| 1024 | port2 = PORT_ATA1_CTRL_BASE; |
Kevin O'Connor | 525219b | 2009-12-05 13:36:18 -0500 | [diff] [blame] | 1025 | irq = IRQ_ATA1; |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 1026 | } |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 1027 | init_controller(&ATA_channels[count], bdf, irq, port1, port2, master); |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 1028 | count++; |
| 1029 | |
Kevin O'Connor | 53ab0b6 | 2008-12-04 19:22:49 -0500 | [diff] [blame] | 1030 | if (prog_if & 4) { |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 1031 | port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3; |
| 1032 | port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3; |
Kevin O'Connor | 525219b | 2009-12-05 13:36:18 -0500 | [diff] [blame] | 1033 | irq = pciirq; |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 1034 | } else { |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 1035 | port1 = PORT_ATA2_CMD_BASE; |
| 1036 | port2 = PORT_ATA2_CTRL_BASE; |
Kevin O'Connor | 525219b | 2009-12-05 13:36:18 -0500 | [diff] [blame] | 1037 | irq = IRQ_ATA2; |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 1038 | } |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 1039 | init_controller(&ATA_channels[count], bdf, irq, port1, port2 |
| 1040 | , master ? master + 8 : 0); |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 1041 | count++; |
| 1042 | } |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 1043 | |
| 1044 | if (!CONFIG_COREBOOT && !pcicount && ARRAY_SIZE(ATA_channels) >= 2) { |
| 1045 | // No PCI devices found - probably a QEMU "-M isapc" machine. |
| 1046 | // Try using ISA ports for ATA controllers. |
Kevin O'Connor | 14021f2 | 2009-12-26 23:21:38 -0500 | [diff] [blame] | 1047 | init_controller(&ATA_channels[0], -1, IRQ_ATA1 |
| 1048 | , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0); |
| 1049 | init_controller(&ATA_channels[1], -1, IRQ_ATA2 |
| 1050 | , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0); |
Kevin O'Connor | 4ccb231 | 2009-12-05 11:25:09 -0500 | [diff] [blame] | 1051 | } |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | void |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 1055 | ata_setup() |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 1056 | { |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 1057 | if (!CONFIG_ATA) |
| 1058 | return; |
| 1059 | |
Kevin O'Connor | 35192dd | 2008-06-08 19:18:33 -0400 | [diff] [blame] | 1060 | dprintf(3, "init hard drives\n"); |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 1061 | |
| 1062 | SpinupEnd = calc_future_tsc(IDE_TIMEOUT); |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 1063 | ata_init(); |
Kevin O'Connor | c143761 | 2008-05-18 01:43:07 -0400 | [diff] [blame] | 1064 | |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 1065 | SET_BDA(disk_control_byte, 0xc0); |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 1066 | |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 1067 | enable_hwirq(14, entry_76); |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 1068 | } |