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