blob: 884eae3eb55ed8a0399e3ecb94ca61bc89a3695a [file] [log] [blame]
Kevin O'Connorc09492e2008-03-01 13:38:07 -05001// Low level ATA disk access
2//
Kevin O'Connorc892b132009-08-11 21:59:37 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorc09492e2008-03-01 13:38:07 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorc09492e2008-03-01 13:38:07 -05007
Kevin O'Connor3491e8b2008-02-29 00:22:27 -05008#include "types.h" // u8
9#include "ioport.h" // inb
Kevin O'Connorac8df8c2008-05-24 23:46:33 -040010#include "util.h" // dprintf
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050011#include "cmos.h" // inb_cmos
Kevin O'Connord21c0892008-11-26 17:02:43 -050012#include "pic.h" // enable_hwirq
Kevin O'Connor9521e262008-07-04 13:04:29 -040013#include "biosvar.h" // GET_EBDA
Kevin O'Connor51fd0a12009-09-12 13:20:14 -040014#include "pci.h" // foreachpci
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050015#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16#include "pci_regs.h" // PCI_INTERRUPT_LINE
Kevin O'Connor0a924122009-02-08 19:43:47 -050017#include "boot.h" // add_bcv_hd
Kevin O'Connor609da232008-12-28 23:18:57 -050018#include "disk.h" // struct ata_s
Kevin O'Connorc892b132009-08-11 21:59:37 -040019#include "ata.h" // ATA_CB_STAT
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050020
Kevin O'Connora6b9f712008-03-29 12:53:57 -040021#define IDE_SECTOR_SIZE 512
22#define CDROM_SECTOR_SIZE 2048
23
Kevin O'Connor425f2122009-04-18 12:23:00 -040024#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050025
Kevin O'Connor372e0712009-09-09 09:51:31 -040026struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
Kevin O'Connor609da232008-12-28 23:18:57 -050027
Kevin O'Connor15aee2e2008-03-01 13:34:04 -050028
Kevin O'Connora6b9f712008-03-29 12:53:57 -040029/****************************************************************
30 * Helper functions
31 ****************************************************************/
32
33// Wait for the specified ide state
Kevin O'Connor580e3322009-02-06 22:36:53 -050034static inline int
35await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050036{
Kevin O'Connor4e6c9702008-12-13 10:45:50 -050037 u64 end = calc_future_tsc(timeout);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050038 for (;;) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -040039 u8 status = inb(base+ATA_CB_STAT);
Kevin O'Connor580e3322009-02-06 22:36:53 -050040 if ((status & mask) == flags)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040041 return status;
Kevin O'Connorf3587592009-02-15 13:02:56 -050042 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -050043 dprintf(1, "IDE time out\n");
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050044 return -1;
45 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050046 }
Kevin O'Connor580e3322009-02-06 22:36:53 -050047}
48
49// Wait for the device to be not-busy.
50static int
51await_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.
57static int
58await_rdy(u16 base)
59{
60 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050061}
62
Kevin O'Connora6b9f712008-03-29 12:53:57 -040063// Wait for ide state - pauses for one ata cycle first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050064static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050065pause_await_not_bsy(u16 iobase1, u16 iobase2)
Kevin O'Connora6b9f712008-03-29 12:53:57 -040066{
67 // Wait one PIO transfer cycle.
68 inb(iobase2 + ATA_CB_ASTAT);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050069
Kevin O'Connor580e3322009-02-06 22:36:53 -050070 return await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -040071}
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050072
Kevin O'Connoref2822a2008-06-07 15:23:11 -040073// Wait for ide state - pause for 400ns first.
Kevin O'Connora9caeae2009-03-07 00:09:52 -050074static inline int
Kevin O'Connor580e3322009-02-06 22:36:53 -050075ndelay_await_not_bsy(u16 iobase1)
Kevin O'Connoref2822a2008-06-07 15:23:11 -040076{
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050077 ndelay(400);
Kevin O'Connor580e3322009-02-06 22:36:53 -050078 return await_not_bsy(iobase1);
Kevin O'Connoref2822a2008-06-07 15:23:11 -040079}
80
Kevin O'Connora6b9f712008-03-29 12:53:57 -040081// Reset a drive
Kevin O'Connorb1144362009-08-11 20:43:38 -040082static void
Kevin O'Connora6b9f712008-03-29 12:53:57 -040083ata_reset(int driveid)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050084{
Kevin O'Connorc892b132009-08-11 21:59:37 -040085 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -040086 u8 channel = ataid / 2;
87 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -040088 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
89 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050090
Kevin O'Connor580e3322009-02-06 22:36:53 -050091 dprintf(6, "ata_reset driveid=%d\n", driveid);
92 // Pulse SRST
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050093 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -050094 udelay(5);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050095 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -050096 mdelay(2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -050097
Kevin O'Connor580e3322009-02-06 22:36:53 -050098 // wait for device to become not busy.
99 int status = await_not_bsy(iobase1);
100 if (status < 0)
101 goto done;
102 if (slave) {
103 // Change device.
104 u64 end = calc_future_tsc(IDE_TIMEOUT);
105 for (;;) {
106 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400107 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500108 if (status < 0)
109 goto done;
110 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
111 break;
112 // Change drive request failed to take effect - retry.
Kevin O'Connorf3587592009-02-15 13:02:56 -0500113 if (rdtscll() > end) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500114 dprintf(1, "ata_reset slave time out\n");
115 goto done;
116 }
117 }
Kevin O'Connorf5624d22009-08-18 22:17:57 -0400118 } else {
119 // QEMU doesn't reset dh on reset, so set it explicitly.
120 outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
Kevin O'Connor2e3eeeb2008-06-12 22:29:30 -0400121 }
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500122
Kevin O'Connor580e3322009-02-06 22:36:53 -0500123 // On a user-reset request, wait for RDY if it is an ATA device.
Kevin O'Connorc892b132009-08-11 21:59:37 -0400124 u8 type=GET_GLOBAL(Drives.drives[driveid].type);
Kevin O'Connor42337662009-08-10 00:06:37 -0400125 if (type == DTYPE_ATA)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500126 status = await_rdy(iobase1);
Kevin O'Connor3e1b6492008-05-26 15:06:40 -0400127
Kevin O'Connor580e3322009-02-06 22:36:53 -0500128done:
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500129 // Enable interrupts
130 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500131
132 dprintf(6, "ata_reset exit status=%x\n", status);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500133}
134
Kevin O'Connor42337662009-08-10 00:06:37 -0400135static int
136isready(int driveid)
137{
138 // Read the status from controller
Kevin O'Connorc892b132009-08-11 21:59:37 -0400139 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400140 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400141 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400142 u8 status = inb(iobase1 + ATA_CB_STAT);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400143 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
144 return DISK_RET_SUCCESS;
145 return DISK_RET_ENOTREADY;
Kevin O'Connor42337662009-08-10 00:06:37 -0400146}
147
148static int
149process_ata_misc_op(struct disk_op_s *op)
150{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400151 if (!CONFIG_ATA)
152 return 0;
153
Kevin O'Connor42337662009-08-10 00:06:37 -0400154 switch (op->command) {
Kevin O'Connor42337662009-08-10 00:06:37 -0400155 case CMD_RESET:
156 ata_reset(op->driveid);
Kevin O'Connor126eac62009-08-16 13:32:24 -0400157 return DISK_RET_SUCCESS;
Kevin O'Connor42337662009-08-10 00:06:37 -0400158 case CMD_ISREADY:
159 return isready(op->driveid);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400160 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'Connor42337662009-08-10 00:06:37 -0400167 }
168}
169
Kevin O'Connoree55c762008-05-13 00:18:20 -0400170
171/****************************************************************
172 * ATA send command
173 ****************************************************************/
174
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400175struct ata_pio_command {
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400176 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
184 u8 sector_count2;
185 u8 lba_low2;
186 u8 lba_mid2;
187 u8 lba_high2;
188};
189
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400190// Send an ata command to the drive.
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400191static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400192send_cmd(int driveid, struct ata_pio_command *cmd)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500193{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400194 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400195 u8 channel = ataid / 2;
196 u8 slave = ataid % 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400197 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
198 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500199
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400200 // Disable interrupts
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500201 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400202
203 // Select device
Kevin O'Connor580e3322009-02-06 22:36:53 -0500204 int status = await_not_bsy(iobase1);
205 if (status < 0)
206 return status;
207 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
208 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
209 u8 olddh = inb(iobase1 + ATA_CB_DH);
210 outb(newdh, iobase1 + ATA_CB_DH);
211 if ((olddh ^ newdh) & (1<<4)) {
212 // Was a device change - wait for device to become not busy.
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400213 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500214 if (status < 0)
215 return status;
216 }
Kevin O'Connoref2822a2008-06-07 15:23:11 -0400217
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400218 if (cmd->command & 0x04) {
219 outb(0x00, iobase1 + ATA_CB_FR);
220 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
221 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
222 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
223 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500224 }
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400225 outb(cmd->feature, iobase1 + ATA_CB_FR);
226 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
227 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
228 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
229 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400230 outb(cmd->command, iobase1 + ATA_CB_CMD);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500231
Kevin O'Connor580e3322009-02-06 22:36:53 -0500232 status = ndelay_await_not_bsy(iobase1);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400233 if (status < 0)
234 return status;
235
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500236 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500237 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
238 , status, inb(iobase1 + ATA_CB_ERR));
Kevin O'Connora05223c2008-06-28 12:15:57 -0400239 return -4;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400240 }
241 if (!(status & ATA_CB_STAT_DRQ)) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500242 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400243 return -5;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500244 }
245
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400246 return 0;
247}
248
Kevin O'Connoree55c762008-05-13 00:18:20 -0400249
250/****************************************************************
251 * ATA transfers
252 ****************************************************************/
253
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400254// Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
255// 'op->driveid'.
256static int
257ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400258{
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400259 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
260 , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400261
Kevin O'Connorc892b132009-08-11 21:59:37 -0400262 u8 ataid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400263 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400264 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
265 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400266 int count = op->count;
267 void *buf_fl = op->buf_fl;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400268 int status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400269 for (;;) {
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400270 if (iswrite) {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400271 // Write data to controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400272 dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500273 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400274 outsl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400275 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400276 outsw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500277 } else {
Kevin O'Connor3a049632008-03-11 11:48:04 -0400278 // Read data from controller
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400279 dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
Kevin O'Connor32945af2009-02-27 21:23:01 -0500280 if (CONFIG_ATA_PIO32)
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400281 insl_fl(iobase1, buf_fl, blocksize / 4);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400282 else
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400283 insw_fl(iobase1, buf_fl, blocksize / 2);
Kevin O'Connor3a049632008-03-11 11:48:04 -0400284 }
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400285 buf_fl += blocksize;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400286
Kevin O'Connor580e3322009-02-06 22:36:53 -0500287 status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400288 if (status < 0) {
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400289 // Error
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400290 op->count -= count;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400291 return status;
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400292 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400293
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400294 count--;
295 if (!count)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400296 break;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500297 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
298 if (status != ATA_CB_STAT_DRQ) {
Kevin O'Connora05223c2008-06-28 12:15:57 -0400299 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
Kevin O'Connor580e3322009-02-06 22:36:53 -0500300 , status);
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400301 op->count -= count;
Kevin O'Connora05223c2008-06-28 12:15:57 -0400302 return -6;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500303 }
304 }
Kevin O'Connor3a049632008-03-11 11:48:04 -0400305
Kevin O'Connor580e3322009-02-06 22:36:53 -0500306 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
307 | ATA_CB_STAT_ERR);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400308 if (!iswrite)
Kevin O'Connor3a049632008-03-11 11:48:04 -0400309 status &= ~ATA_CB_STAT_DF;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500310 if (status != 0) {
311 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
Kevin O'Connora05223c2008-06-28 12:15:57 -0400312 return -7;
Kevin O'Connor3a049632008-03-11 11:48:04 -0400313 }
314
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500315 // Enable interrupts
316 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
317 return 0;
318}
319
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400320
321/****************************************************************
322 * ATA hard drive functions
323 ****************************************************************/
324
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400325// Read/write count blocks from a harddrive.
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400326static int
327ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400328{
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400329 u64 lba = op->lba;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400330
331 struct ata_pio_command cmd;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400332 memset(&cmd, 0, sizeof(cmd));
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400333
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400334 cmd.command = command;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400335 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
336 cmd.sector_count2 = op->count >> 8;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400337 cmd.lba_low2 = lba >> 24;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -0400338 cmd.lba_mid2 = lba >> 32;
339 cmd.lba_high2 = lba >> 40;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400340
Kevin O'Connordfabfe02008-04-05 21:08:57 -0400341 cmd.command |= 0x04;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400342 lba &= 0xffffff;
343 }
344
345 cmd.feature = 0;
Kevin O'Connoree55c762008-05-13 00:18:20 -0400346 cmd.sector_count = op->count;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400347 cmd.lba_low = lba;
348 cmd.lba_mid = lba >> 8;
349 cmd.lba_high = lba >> 16;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500350 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400351
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400352 int ret = send_cmd(op->driveid, &cmd);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400353 if (ret)
354 return ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400355 return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
356}
357
358int
359process_ata_op(struct disk_op_s *op)
360{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400361 if (!CONFIG_ATA)
362 return 0;
363
Kevin O'Connor126eac62009-08-16 13:32:24 -0400364 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400365 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400366 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400367 ret = ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
368 break;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400369 case CMD_WRITE:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400370 ret = ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
371 break;
Kevin O'Connor42337662009-08-10 00:06:37 -0400372 default:
373 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400374 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400375 if (ret)
376 return DISK_RET_EBADTRACK;
377 return DISK_RET_SUCCESS;
Kevin O'Connorf888f8c2008-03-23 00:04:54 -0400378}
379
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400380
381/****************************************************************
382 * ATAPI functions
383 ****************************************************************/
384
385// Low-level atapi command transmit function.
Kevin O'Connora9caeae2009-03-07 00:09:52 -0500386static int
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400387send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500388{
Kevin O'Connorc892b132009-08-11 21:59:37 -0400389 u8 ataid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400390 u8 channel = ataid / 2;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400391 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
392 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500393
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400394 struct ata_pio_command cmd;
395 cmd.sector_count = 0;
396 cmd.feature = 0;
397 cmd.lba_low = 0;
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400398 cmd.lba_mid = blocksize;
399 cmd.lba_high = blocksize >> 8;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500400 cmd.device = 0;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400401 cmd.command = ATA_CMD_PACKET;
402
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400403 int ret = send_cmd(driveid, &cmd);
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400404 if (ret)
405 return ret;
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500406
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400407 // Send command to device
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500408 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500409
Kevin O'Connor580e3322009-02-06 22:36:53 -0500410 int status = pause_await_not_bsy(iobase1, iobase2);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400411 if (status < 0)
412 return status;
Kevin O'Connor1fcf1442008-03-11 19:42:41 -0400413
Kevin O'Connor580e3322009-02-06 22:36:53 -0500414 if (status & ATA_CB_STAT_ERR) {
Kevin O'Connorb30c4002009-05-05 21:47:20 -0400415 u8 err = inb(iobase1 + ATA_CB_ERR);
416 // skip "Not Ready"
417 if (err != 0x20)
418 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
419 , status, err);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500420 return -2;
421 }
422 if (!(status & ATA_CB_STAT_DRQ)) {
423 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
424 return -3;
425 }
426
Kevin O'Connor3491e8b2008-02-29 00:22:27 -0500427 return 0;
428}
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500429
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400430// Read sectors from the cdrom.
431int
432cdrom_read(struct disk_op_s *op)
Kevin O'Connor180a9592008-03-04 22:50:53 -0500433{
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400434 u8 atacmd[12];
435 memset(atacmd, 0, sizeof(atacmd));
Kevin O'Connoree55c762008-05-13 00:18:20 -0400436 atacmd[0]=0x28; // READ command
437 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
438 atacmd[8]=(op->count & 0x00ff);
439 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
440 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
441 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
442 atacmd[5]=(op->lba & 0x000000ff);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400443
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400444 int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
445 , CDROM_SECTOR_SIZE);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400446 if (ret)
447 return ret;
448
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400449 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400450}
451
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400452int
453process_atapi_op(struct disk_op_s *op)
454{
Kevin O'Connor126eac62009-08-16 13:32:24 -0400455 int ret;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400456 switch (op->command) {
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400457 case CMD_READ:
Kevin O'Connor126eac62009-08-16 13:32:24 -0400458 ret = cdrom_read(op);
459 break;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400460 case CMD_FORMAT:
461 case CMD_WRITE:
462 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor42337662009-08-10 00:06:37 -0400463 default:
464 return process_ata_misc_op(op);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400465 }
Kevin O'Connor126eac62009-08-16 13:32:24 -0400466 if (ret)
467 return DISK_RET_EBADTRACK;
468 return DISK_RET_SUCCESS;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400469}
470
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400471// Send a simple atapi command to a drive.
472int
473ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500474 , u32 length, void *buf_fl)
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400475{
476 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
477 if (ret)
478 return ret;
Kevin O'Connoraa2590c2008-03-22 23:13:24 -0400479
Kevin O'Connor9ae1e9b2009-08-09 18:06:40 -0400480 struct disk_op_s dop;
481 memset(&dop, 0, sizeof(dop));
482 dop.driveid = driveid;
483 dop.count = 1;
484 dop.buf_fl = buf_fl;
485
486 return ata_transfer(&dop, 0, length);
Kevin O'Connora6b9f712008-03-29 12:53:57 -0400487}
488
489
490/****************************************************************
Kevin O'Connor0a924122009-02-08 19:43:47 -0500491 * ATA detect and init
492 ****************************************************************/
493
Kevin O'Connorf2d48a32009-08-11 20:58:11 -0400494// Extract the ATA/ATAPI version info.
495static int
496extract_version(u16 *buffer)
497{
498 // Extract ATA/ATAPI version.
499 u16 ataversion = buffer[80];
500 u8 version;
501 for (version=15; version>0; version--)
502 if (ataversion & (1<<version))
503 break;
504 return version;
505}
506
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500507// Extract common information from IDENTIFY commands.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500508static void
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500509extract_identify(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500510{
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500511 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500512
513 // Read model name
Kevin O'Connorc892b132009-08-11 21:59:37 -0400514 char *model = Drives.drives[driveid].model;
515 int maxsize = ARRAY_SIZE(Drives.drives[driveid].model);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500516 int i;
Kevin O'Connorab515602009-02-11 22:22:15 -0500517 for (i=0; i<maxsize/2; i++) {
518 u16 v = buffer[27+i];
519 model[i*2] = v >> 8;
520 model[i*2+1] = v & 0xff;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500521 }
522 model[maxsize-1] = 0x00;
523
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500524 // Trim trailing spaces from model name.
Kevin O'Connor0a924122009-02-08 19:43:47 -0500525 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
526 model[i] = 0x00;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500527
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500528 // Common flags.
Kevin O'Connorc892b132009-08-11 21:59:37 -0400529 SET_GLOBAL(Drives.drives[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400530 SET_GLOBAL(Drives.drives[driveid].cntl_info, extract_version(buffer));
531}
532
533void
534describe_atapi(int driveid)
535{
536 u8 ataid = Drives.drives[driveid].cntl_id;
537 u8 channel = ataid / 2;
538 u8 slave = ataid % 2;
539 u8 version = Drives.drives[driveid].cntl_info;
540 int iscd = Drives.drives[driveid].floppy_type;
541 printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
542 , Drives.drives[driveid].model, version
543 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500544}
545
546static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400547init_drive_atapi(int driveid, u16 *buffer)
Kevin O'Connor0a924122009-02-08 19:43:47 -0500548{
549 // Send an IDENTIFY_DEVICE_PACKET command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400550 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500551 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400552 memset(&dop, 0, sizeof(dop));
Kevin O'Connor0a924122009-02-08 19:43:47 -0500553 dop.driveid = driveid;
Kevin O'Connor0a924122009-02-08 19:43:47 -0500554 dop.count = 1;
555 dop.lba = 1;
556 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400557 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500558 if (ret)
559 return ret;
560
561 // Success - setup as ATAPI.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500562 extract_identify(driveid, buffer);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400563 SET_GLOBAL(Drives.drives[driveid].type, DTYPE_ATAPI);
564 SET_GLOBAL(Drives.drives[driveid].blksize, CDROM_SECTOR_SIZE);
565 SET_GLOBAL(Drives.drives[driveid].sectors, (u64)-1);
Kevin O'Connor42337662009-08-10 00:06:37 -0400566 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400567 SET_GLOBAL(Drives.drives[driveid].floppy_type, iscd);
Kevin O'Connor42337662009-08-10 00:06:37 -0400568
569 // fill cdidmap
Kevin O'Connorc892b132009-08-11 21:59:37 -0400570 if (iscd)
571 map_cd_drive(driveid);
Kevin O'Connor0a924122009-02-08 19:43:47 -0500572
573 return 0;
574}
575
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400576void
577describe_ata(int driveid)
578{
579 u8 ataid = Drives.drives[driveid].cntl_id;
580 u8 channel = ataid / 2;
581 u8 slave = ataid % 2;
582 u64 sectors = Drives.drives[driveid].sectors;
583 u8 version = Drives.drives[driveid].cntl_info;
584 char *model = Drives.drives[driveid].model;
585 printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
586 u64 sizeinmb = sectors >> 11;
587 if (sizeinmb < (1 << 16))
588 printf(" (%u MiBytes)", (u32)sizeinmb);
589 else
590 printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
591}
592
Kevin O'Connor580e3322009-02-06 22:36:53 -0500593static int
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400594init_drive_ata(int driveid, u16 *buffer)
Kevin O'Connorc1437612008-05-18 01:43:07 -0400595{
Kevin O'Connor580e3322009-02-06 22:36:53 -0500596 // Send an IDENTIFY_DEVICE command to device
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400597 memset(buffer, 0, IDE_SECTOR_SIZE);
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500598 struct disk_op_s dop;
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400599 memset(&dop, 0, sizeof(dop));
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500600 dop.driveid = driveid;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500601 dop.count = 1;
602 dop.lba = 1;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500603 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
Kevin O'Connor3f6c2782009-08-09 19:17:11 -0400604 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400605 if (ret)
Kevin O'Connor580e3322009-02-06 22:36:53 -0500606 return ret;
607
608 // Success - setup as ATA.
Kevin O'Connor160f71c2009-02-11 22:46:29 -0500609 extract_identify(driveid, buffer);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400610 SET_GLOBAL(Drives.drives[driveid].type, DTYPE_ATA);
611 SET_GLOBAL(Drives.drives[driveid].blksize, IDE_SECTOR_SIZE);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400612
Kevin O'Connorc892b132009-08-11 21:59:37 -0400613 SET_GLOBAL(Drives.drives[driveid].pchs.cylinders, buffer[1]);
614 SET_GLOBAL(Drives.drives[driveid].pchs.heads, buffer[3]);
615 SET_GLOBAL(Drives.drives[driveid].pchs.spt, buffer[6]);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400616
617 u64 sectors;
Kevin O'Connorab515602009-02-11 22:22:15 -0500618 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
619 sectors = *(u64*)&buffer[100]; // word 100-103
Kevin O'Connorc1437612008-05-18 01:43:07 -0400620 else
Kevin O'Connorab515602009-02-11 22:22:15 -0500621 sectors = *(u32*)&buffer[60]; // word 60 and word 61
Kevin O'Connorc892b132009-08-11 21:59:37 -0400622 SET_GLOBAL(Drives.drives[driveid].sectors, sectors);
Kevin O'Connorc1437612008-05-18 01:43:07 -0400623
624 // Setup disk geometry translation.
625 setup_translation(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400626
Kevin O'Connor0a924122009-02-08 19:43:47 -0500627 // Register with bcv system.
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400628 add_bcv_internal(driveid);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400629
Kevin O'Connor580e3322009-02-06 22:36:53 -0500630 return 0;
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400631}
632
Kevin O'Connor425f2122009-04-18 12:23:00 -0400633static int
634powerup_await_non_bsy(u16 base, u64 end)
635{
636 u8 orstatus = 0;
637 u8 status;
638 for (;;) {
639 status = inb(base+ATA_CB_STAT);
640 if (!(status & ATA_CB_STAT_BSY))
641 break;
642 orstatus |= status;
643 if (orstatus == 0xff) {
644 dprintf(1, "powerup IDE floating\n");
645 return orstatus;
646 }
647 if (rdtscll() > end) {
648 dprintf(1, "powerup IDE time out\n");
649 return -1;
650 }
651 }
652 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
653 return status;
654}
655
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400656static void
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500657ata_detect()
658{
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500659 // Device detection
Kevin O'Connor425f2122009-04-18 12:23:00 -0400660 u64 end = calc_future_tsc(IDE_TIMEOUT);
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400661 int ataid, last_reset_ataid=-1;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400662 for (ataid=0; ataid<CONFIG_MAX_ATA_INTERFACES*2; ataid++) {
663 u8 channel = ataid / 2;
664 u8 slave = ataid % 2;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500665
Kevin O'Connorc892b132009-08-11 21:59:37 -0400666 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400667 if (!iobase1)
668 break;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500669
Kevin O'Connor425f2122009-04-18 12:23:00 -0400670 // Wait for not-bsy.
671 int status = powerup_await_non_bsy(iobase1, end);
672 if (status < 0)
673 continue;
674 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
675 outb(newdh, iobase1+ATA_CB_DH);
Kevin O'Connorc946eca2009-05-24 13:55:01 -0400676 ndelay(400);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400677 status = powerup_await_non_bsy(iobase1, end);
678 if (status < 0)
679 continue;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500680
Kevin O'Connor580e3322009-02-06 22:36:53 -0500681 // Check if ioport registers look valid.
Kevin O'Connor425f2122009-04-18 12:23:00 -0400682 outb(newdh, iobase1+ATA_CB_DH);
683 u8 dh = inb(iobase1+ATA_CB_DH);
684 outb(0x55, iobase1+ATA_CB_SC);
685 outb(0xaa, iobase1+ATA_CB_SN);
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400686 u8 sc = inb(iobase1+ATA_CB_SC);
687 u8 sn = inb(iobase1+ATA_CB_SN);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400688 dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
689 , ataid, sc, sn, dh);
Kevin O'Connor425f2122009-04-18 12:23:00 -0400690 if (sc != 0x55 || sn != 0xaa || dh != newdh)
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400691 continue;
692
Kevin O'Connorb1144362009-08-11 20:43:38 -0400693 // Prepare new driveid.
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400694 u8 driveid = GET_GLOBAL(Drives.drivecount);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400695 if (driveid >= ARRAY_SIZE(Drives.drives))
Kevin O'Connorb1144362009-08-11 20:43:38 -0400696 break;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400697 memset(&Drives.drives[driveid], 0, sizeof(Drives.drives[0]));
698 Drives.drives[driveid].cntl_id = ataid;
Kevin O'Connorb1144362009-08-11 20:43:38 -0400699
Kevin O'Connoraafa6572008-03-13 19:57:49 -0400700 // reset the channel
Kevin O'Connorb1144362009-08-11 20:43:38 -0400701 if (slave && ataid == last_reset_ataid + 1) {
Kevin O'Connor580e3322009-02-06 22:36:53 -0500702 // The drive was just reset - no need to reset it again.
703 } else {
704 ata_reset(driveid);
Kevin O'Connorb1144362009-08-11 20:43:38 -0400705 last_reset_ataid = ataid;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500706 }
707
Kevin O'Connor580e3322009-02-06 22:36:53 -0500708 // check for ATAPI
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400709 u16 buffer[256];
710 int ret = init_drive_atapi(driveid, buffer);
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400711 if (ret) {
712 // Didn't find an ATAPI drive - look for ATA drive.
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400713 u8 st = inb(iobase1+ATA_CB_STAT);
714 if (!st)
715 // Status not set - can't be a valid drive.
716 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500717
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400718 // Wait for RDY.
719 ret = await_rdy(iobase1);
720 if (ret < 0)
721 continue;
Kevin O'Connor580e3322009-02-06 22:36:53 -0500722
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400723 // check for ATA.
724 ret = init_drive_ata(driveid, buffer);
725 if (ret)
726 // No ATA drive found
727 continue;
728 }
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400729 SET_GLOBAL(Drives.drivecount, driveid+1);
Kevin O'Connor580e3322009-02-06 22:36:53 -0500730
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400731 // Report drive info to user.
732 describe_drive(driveid);
733 printf("\n");
734
Kevin O'Connorc79637b2009-06-10 20:33:57 -0400735 u16 resetresult = buffer[93];
736 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
737 if (!slave && (resetresult & 0xdf61) == 0x4041)
738 // resetresult looks valid and device 0 is responding to
739 // device 1 requests - device 1 must not be present - skip
740 // detection.
Kevin O'Connorb1144362009-08-11 20:43:38 -0400741 ataid++;
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500742 }
743
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500744 printf("\n");
Kevin O'Connor15aee2e2008-03-01 13:34:04 -0500745}
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400746
747static void
748ata_init()
749{
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400750 // Scan PCI bus for ATA adapters
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500751 int count=0;
752 int bdf, max;
Kevin O'Connor4132e022008-12-04 19:39:10 -0500753 foreachpci(bdf, max) {
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500754 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400755 continue;
Kevin O'Connorc892b132009-08-11 21:59:37 -0400756 if (count >= ARRAY_SIZE(ATA_channels))
Kevin O'Connor89f67632009-02-11 20:16:49 -0500757 break;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400758
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500759 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400760 SET_GLOBAL(ATA_channels[count].irq, irq);
761 SET_GLOBAL(ATA_channels[count].pci_bdf, bdf);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400762
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500763 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400764 u32 port1, port2;
765
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500766 if (prog_if & 1) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500767 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
768 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400769 } else {
770 port1 = 0x1f0;
771 port2 = 0x3f0;
772 }
Kevin O'Connorc892b132009-08-11 21:59:37 -0400773 SET_GLOBAL(ATA_channels[count].iobase1, port1);
774 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500775 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
776 , count, port1, port2, bdf, prog_if);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400777 count++;
778
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500779 if (prog_if & 4) {
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500780 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
781 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400782 } else {
783 port1 = 0x170;
784 port2 = 0x370;
785 }
Kevin O'Connor53ab0b62008-12-04 19:22:49 -0500786 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
787 , count, port1, port2, bdf, prog_if);
Kevin O'Connorc892b132009-08-11 21:59:37 -0400788 SET_GLOBAL(ATA_channels[count].iobase1, port1);
789 SET_GLOBAL(ATA_channels[count].iobase2, port2);
Kevin O'Connor53236cc2008-08-31 11:16:33 -0400790 count++;
791 }
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400792}
793
794void
Kevin O'Connorc892b132009-08-11 21:59:37 -0400795ata_setup()
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400796{
Kevin O'Connorc1437612008-05-18 01:43:07 -0400797 if (!CONFIG_ATA)
798 return;
799
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400800 dprintf(3, "init hard drives\n");
Kevin O'Connorc1437612008-05-18 01:43:07 -0400801 ata_init();
802 ata_detect();
803
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400804 SET_BDA(disk_control_byte, 0xc0);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400805
Kevin O'Connord21c0892008-11-26 17:02:43 -0500806 enable_hwirq(14, entry_76);
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400807}