1 // Low level ATA disk access
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "ata.h" // ATA_*
9 #include "types.h" // u8
10 #include "ioport.h" // inb
11 #include "util.h" // BX_INFO
12 #include "cmos.h" // inb_cmos
18 #define NOT_BSY_NOT_DRQ 4
21 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
23 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
24 #define DEBUGF(fmt, args...)
26 // XXX - lots of redundancy in this file.
29 await_ide(u8 when_done, u16 base, u16 timeout)
32 // for the times you're supposed to throw one away
33 u16 status = inb(base + ATA_CB_STAT);
35 status = inb(base+ATA_CB_STAT);
39 result = status & ATA_CB_STAT_BSY;
40 else if (when_done == NOT_BSY)
41 result = !(status & ATA_CB_STAT_BSY);
42 else if (when_done == NOT_BSY_DRQ)
43 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
44 else if (when_done == NOT_BSY_NOT_DRQ)
45 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
46 else if (when_done == NOT_BSY_RDY)
47 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
48 else if (when_done == TIMEOUT)
53 // mod 2048 each 16 ms
54 if (time>>16 != last) {
56 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
57 " %d time= %d timeout= %d\n"
58 , when_done, time>>11, timeout);
60 if (status & ATA_CB_STAT_ERR) {
61 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
62 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
63 , when_done, time>>11, timeout);
66 if ((timeout == 0) || ((time>>11) > timeout))
69 BX_INFO("IDE time out\n");
74 // ---------------------------------------------------------------------------
75 // ATA/ATAPI driver : software reset
76 // ---------------------------------------------------------------------------
78 // 8.2.1 Software reset - Device 0
84 u8 channel, slave, sn, sc;
90 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
91 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
95 // 8.2.1 (a) -- set SRST in DC
96 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
98 // 8.2.1 (b) -- wait for BSY
99 await_ide(BSY, iobase1, 20);
101 // 8.2.1 (f) -- clear SRST
102 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
104 type=GET_EBDA(ata.devices[device].type);
105 if (type != ATA_TYPE_NONE) {
107 // 8.2.1 (g) -- check for sc==sn==0x01
109 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
110 sc = inb(iobase1+ATA_CB_SC);
111 sn = inb(iobase1+ATA_CB_SN);
113 if ( (sc==0x01) && (sn==0x01) ) {
114 if (type == ATA_TYPE_ATA) //ATA
115 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
117 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
120 // 8.2.1 (h) -- wait for not BSY
121 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
125 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
129 // ---------------------------------------------------------------------------
130 // ATA/ATAPI driver : execute a data command
131 // ---------------------------------------------------------------------------
136 // 3 : expected DRQ=1
137 // 4 : no sectors left to read/verify
138 // 5 : more sectors to read/verify
139 // 6 : no sectors left to write
140 // 7 : more sectors to write
142 struct ata_pio_command {
161 send_cmd(struct ata_pio_command *cmd)
163 u16 biosid = cmd->biosid;
164 u8 channel = biosid / 2;
165 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
166 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
168 u8 status = inb(iobase1 + ATA_CB_STAT);
169 if (status & ATA_CB_STAT_BSY)
172 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
173 if (cmd->command & 0x04) {
174 outb(0x00, iobase1 + ATA_CB_FR);
175 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
176 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
177 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
178 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
180 outb(cmd->feature, iobase1 + ATA_CB_FR);
181 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
182 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
183 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
184 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
185 outb(cmd->device, iobase1 + ATA_CB_DH);
186 outb(cmd->command, iobase1 + ATA_CB_CMD);
188 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
190 status = inb(iobase1 + ATA_CB_STAT);
191 if (status & ATA_CB_STAT_ERR) {
192 DEBUGF("send_cmd : read error\n");
195 if (!(status & ATA_CB_STAT_DRQ)) {
196 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
197 , (unsigned) status);
205 ata_transfer(struct ata_pio_command *cmd)
207 DEBUGF("ata_transfer id=%d cmd=%d lba=%d count=%d buf=%p\n"
208 , cmd->biosid, cmd->command
209 , (cmd->lba_high << 16) | (cmd->lba_mid << 8) | cmd->lba_low
210 , cmd->sector_count, cmd->far_buffer);
212 // Reset count of transferred data
213 SET_EBDA(ata.trsfsectors,0);
214 SET_EBDA(ata.trsfbytes,0L);
216 int ret = send_cmd(cmd);
220 u16 biosid = cmd->biosid;
221 u8 channel = biosid / 2;
222 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
223 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
224 u8 mode = GET_EBDA(ata.devices[biosid].mode);
225 int iswrite = (cmd->command & ~0x40) == ATA_CMD_WRITE_SECTORS;
227 u16 count = cmd->sector_count;
229 void *far_buffer = cmd->far_buffer;
232 // Write data to controller
233 DEBUGF("Write sector id=%d dest=%p\n", biosid, far_buffer);
234 if (mode == ATA_MODE_PIO32)
235 outsl_far(iobase1, far_buffer, 512 / 4);
237 outsw_far(iobase1, far_buffer, 512 / 2);
239 // Read data from controller
240 DEBUGF("Read sector id=%d dest=%p\n", biosid, far_buffer);
241 if (mode == ATA_MODE_PIO32)
242 insl_far(iobase1, far_buffer, 512 / 4);
244 insw_far(iobase1, far_buffer, 512 / 2);
245 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
250 SET_EBDA(ata.trsfsectors,current);
252 status = inb(iobase1 + ATA_CB_STAT);
255 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
257 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
258 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
259 , (unsigned) status);
264 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
265 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
267 status &= ~ATA_CB_STAT_DF;
268 if (status != ATA_CB_STAT_RDY ) {
269 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
270 , (unsigned) status);
275 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
280 ata_cmd_data(u16 biosid, u16 command, u32 lba, u16 count, void *far_buffer)
282 u8 slave = biosid % 2;
284 struct ata_pio_command cmd;
285 cmd.far_buffer = far_buffer;
288 if (count >= (1<<8) || lba + count >= (1<<28)) {
289 cmd.sector_count2 = count >> 8;
290 cmd.lba_low2 = lba >> 24;
299 cmd.sector_count = count;
301 cmd.lba_mid = lba >> 8;
302 cmd.lba_high = lba >> 16;
303 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
304 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
305 cmd.command = command;
306 return ata_transfer(&cmd);
309 // ---------------------------------------------------------------------------
310 // ATA/ATAPI driver : execute a packet command
311 // ---------------------------------------------------------------------------
314 // 1 : error in parameters
319 __ata_cmd_packet(u16 biosid, u8 *cmdbuf, u8 cmdlen
320 , u16 header, u32 length, void *far_buffer)
322 DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d buf=%p\n"
323 , biosid, cmdlen, header, length, far_buffer);
325 u8 channel = biosid / 2;
326 u8 slave = biosid % 2;
328 // The header length must be even
330 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
334 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
335 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
336 u8 mode = GET_EBDA(ata.devices[biosid].mode);
338 struct ata_pio_command cmd;
339 cmd.sector_count = 0;
344 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
345 cmd.command = ATA_CMD_PACKET;
348 int ret = send_cmd(&cmd);
352 // Reset count of transferred data
353 SET_EBDA(ata.trsfsectors,0);
354 SET_EBDA(ata.trsfbytes,0L);
356 // Send command to device
357 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
362 if (loops == 0) {//first time through
363 status = inb(iobase2 + ATA_CB_ASTAT);
364 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
366 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
369 status = inb(iobase1 + ATA_CB_STAT);
370 inb(iobase1 + ATA_CB_SC);
372 // Check if command completed
373 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
374 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
377 if (status & ATA_CB_STAT_ERR) {
378 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
382 // Get the byte count
383 u16 lcount = (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
384 + inb(iobase1 + ATA_CB_CL));
386 // adjust to read what we want
388 if (header > lcount) {
398 if (lcount > length) {
399 lafter=lcount-length;
410 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) to %p\n"
411 , lbefore+lcount+lafter, lbefore, lcount, lafter, far_buffer);
413 // If counts not dividable by 4, use 16bits mode
415 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
416 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
417 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
419 // adds an extra byte if count are odd. before is always even
422 if ((lafter > 0) && (lafter & 0x01)) {
427 if (lmode == ATA_MODE_PIO32) {
428 lcount>>=2; lbefore>>=2; lafter>>=2;
430 lcount>>=1; lbefore>>=1; lafter>>=1;
434 for (i=0; i<lbefore; i++)
435 if (lmode == ATA_MODE_PIO32)
440 if (lmode == ATA_MODE_PIO32)
441 insl_far(iobase1, far_buffer, lcount);
443 insw_far(iobase1, far_buffer, lcount);
445 for (i=0; i<lafter; i++)
446 if (lmode == ATA_MODE_PIO32)
451 // Compute new buffer address
454 // Save transferred bytes count
455 SET_EBDA(ata.trsfsectors, loops);
458 // Final check, device must be ready
459 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
460 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
461 != ATA_CB_STAT_RDY ) {
462 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
463 , (unsigned) status);
468 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
473 ata_cmd_packet(u16 biosid, u8 *cmdbuf, u8 cmdlen
474 , u32 length, void *far_buffer)
476 return __ata_cmd_packet(biosid, cmdbuf, cmdlen, 0, length, far_buffer);
480 build_cdrom_cmd(u8 *atacmd, u32 lba, u16 count)
482 memset(atacmd, 0, 12);
483 atacmd[0]=0x28; // READ command
484 atacmd[7]=(count & 0xff00) >> 8; // Sectors
485 atacmd[8]=(count & 0x00ff); // Sectors
486 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
487 atacmd[3]=(lba & 0x00ff0000) >> 16;
488 atacmd[4]=(lba & 0x0000ff00) >> 8;
489 atacmd[5]=(lba & 0x000000ff);
492 // Read sectors from the cdrom.
494 cdrom_read(u16 biosid, u32 lba, u32 count, void *far_buffer)
497 build_cdrom_cmd(atacmd, lba, count);
498 return ata_cmd_packet(biosid, atacmd, sizeof(atacmd)
499 , count*2048, far_buffer);
502 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
505 cdrom_read_512(u16 biosid, u32 vlba, u32 count, void *far_buffer)
508 u16 before = vlba % 4;
509 u32 elba = (vlba + count - 1) / 4;
512 build_cdrom_cmd(atacmd, slba, elba - slba + 1);
514 int status = __ata_cmd_packet(biosid, atacmd, sizeof(atacmd)
515 , before*512, count*512, far_buffer);
517 SET_EBDA(ata.trsfsectors, 0);
520 SET_EBDA(ata.trsfsectors, count);
525 // ---------------------------------------------------------------------------
526 // ATA/ATAPI driver : device detection
527 // ---------------------------------------------------------------------------
530 report_model(u8 devid, u8 *buffer)
536 for (i=0; i<40; i+=2) {
537 model[i] = buffer[i+54+1];
538 model[i+1] = buffer[i+54];
543 for (i=39; i>0; i--) {
544 if (model[i] != 0x20)
549 u8 channel = devid / 2;
550 u8 slave = devid % 2;
551 // XXX - model on stack not %cs
552 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
556 get_ata_version(u8 *buffer)
558 u16 ataversion = *(u16*)&buffer[160];
560 for (version=15; version>0; version--)
561 if (ataversion & (1<<version))
567 init_drive_atapi(u8 devid)
569 SET_EBDA(ata.devices[devid].type,ATA_TYPE_ATAPI);
571 // Temporary values to do the transfer
572 SET_EBDA(ata.devices[devid].device,ATA_DEVICE_CDROM);
573 SET_EBDA(ata.devices[devid].mode, ATA_MODE_PIO16);
575 // Now we send a IDENTIFY command to ATAPI device
577 memset(buffer, 0, sizeof(buffer));
578 u16 ret = ata_cmd_data(devid, ATA_CMD_IDENTIFY_DEVICE_PACKET
580 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
582 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
584 u8 type = buffer[1] & 0x1f;
585 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
586 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
589 SET_EBDA(ata.devices[devid].device, type);
590 SET_EBDA(ata.devices[devid].removable, removable);
591 SET_EBDA(ata.devices[devid].mode, mode);
592 SET_EBDA(ata.devices[devid].blksize, blksize);
595 u8 cdcount = GET_EBDA(ata.cdcount);
596 SET_EBDA(ata.idmap[1][cdcount], devid);
597 SET_EBDA(ata.cdcount, ++cdcount);
599 report_model(devid, buffer);
600 u8 version = get_ata_version(buffer);
601 if (GET_EBDA(ata.devices[devid].device)==ATA_DEVICE_CDROM)
602 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
604 printf(" ATAPI-%d Device\n", version);
608 init_drive_ata(u8 devid)
610 SET_EBDA(ata.devices[devid].type,ATA_TYPE_ATA);
612 // Temporary values to do the transfer
613 SET_EBDA(ata.devices[devid].device, ATA_DEVICE_HD);
614 SET_EBDA(ata.devices[devid].mode, ATA_MODE_PIO16);
616 // Now we send a IDENTIFY command to ATA device
618 memset(buffer, 0, sizeof(buffer));
619 u16 ret = ata_cmd_data(devid, ATA_CMD_IDENTIFY_DEVICE
621 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
623 BX_PANIC("ata-detect: Failed to detect ATA device\n");
625 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
626 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
627 u16 blksize = *(u16*)&buffer[10];
629 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
630 u16 heads = *(u16*)&buffer[3*2]; // word 3
631 u16 spt = *(u16*)&buffer[6*2]; // word 6
633 u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
635 SET_EBDA(ata.devices[devid].device,ATA_DEVICE_HD);
636 SET_EBDA(ata.devices[devid].removable, removable);
637 SET_EBDA(ata.devices[devid].mode, mode);
638 SET_EBDA(ata.devices[devid].blksize, blksize);
639 SET_EBDA(ata.devices[devid].pchs.heads, heads);
640 SET_EBDA(ata.devices[devid].pchs.cylinders, cylinders);
641 SET_EBDA(ata.devices[devid].pchs.spt, spt);
642 SET_EBDA(ata.devices[devid].sectors, sectors);
644 u8 channel = devid / 2;
645 u8 slave = devid % 2;
646 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
648 for (shift=devid%4; shift>0; shift--)
652 SET_EBDA(ata.devices[devid].translation, translation);
654 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
655 , channel, slave, cylinders, heads, spt);
656 switch (translation) {
657 case ATA_TRANSLATION_NONE:
660 case ATA_TRANSLATION_LBA:
664 heads = sectors / 1024;
675 cylinders = sectors / heads;
677 case ATA_TRANSLATION_RECHS:
679 // Take care not to overflow
684 cylinders = (u16)((u32)(cylinders)*16/15);
686 // then go through the large bitshift process
687 case ATA_TRANSLATION_LARGE:
688 if (translation == ATA_TRANSLATION_LARGE)
690 while(cylinders > 1024) {
694 // If we max out the head count
700 // clip to 1024 cylinders in lchs
701 if (cylinders > 1024)
703 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
705 SET_EBDA(ata.devices[devid].lchs.heads, heads);
706 SET_EBDA(ata.devices[devid].lchs.cylinders, cylinders);
707 SET_EBDA(ata.devices[devid].lchs.spt, spt);
710 u8 hdcount = GET_EBDA(ata.hdcount);
711 SET_EBDA(ata.idmap[0][hdcount], devid);
712 SET_EBDA(ata.hdcount, ++hdcount);
714 u32 sizeinmb = GET_EBDA(ata.devices[devid].sectors);
717 report_model(devid, buffer);
718 u8 version = get_ata_version(buffer);
719 if (sizeinmb < (1 << 16))
720 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
722 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
726 init_drive_unknown(u8 devid)
728 SET_EBDA(ata.devices[devid].type,ATA_TYPE_UNKNOWN);
730 u8 channel = devid / 2;
731 u8 slave = devid % 2;
732 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
738 #if CONFIG_MAX_ATA_INTERFACES > 0
739 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
740 SET_EBDA(ata.channels[0].iobase1,0x1f0);
741 SET_EBDA(ata.channels[0].iobase2,0x3f0);
742 SET_EBDA(ata.channels[0].irq,14);
744 #if CONFIG_MAX_ATA_INTERFACES > 1
745 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
746 SET_EBDA(ata.channels[1].iobase1,0x170);
747 SET_EBDA(ata.channels[1].iobase2,0x370);
748 SET_EBDA(ata.channels[1].irq,15);
750 #if CONFIG_MAX_ATA_INTERFACES > 2
751 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
752 SET_EBDA(ata.channels[2].iobase1,0x1e8);
753 SET_EBDA(ata.channels[2].iobase2,0x3e0);
754 SET_EBDA(ata.channels[2].irq,12);
756 #if CONFIG_MAX_ATA_INTERFACES > 3
757 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
758 SET_EBDA(ata.channels[3].iobase1,0x168);
759 SET_EBDA(ata.channels[3].iobase2,0x360);
760 SET_EBDA(ata.channels[3].irq,11);
762 #if CONFIG_MAX_ATA_INTERFACES > 4
763 #error Please fill the ATA interface informations
768 for(devid=0; devid<CONFIG_MAX_ATA_DEVICES; devid++) {
769 u8 channel = devid / 2;
770 u8 slave = devid % 2;
772 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
773 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
775 // Disable interrupts
776 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
779 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
780 outb(0x55, iobase1+ATA_CB_SC);
781 outb(0xaa, iobase1+ATA_CB_SN);
782 outb(0xaa, iobase1+ATA_CB_SC);
783 outb(0x55, iobase1+ATA_CB_SN);
784 outb(0x55, iobase1+ATA_CB_SC);
785 outb(0xaa, iobase1+ATA_CB_SN);
787 // If we found something
788 u8 sc = inb(iobase1+ATA_CB_SC);
789 u8 sn = inb(iobase1+ATA_CB_SN);
791 if (sc != 0x55 || sn != 0xaa)
797 // check for ATA or ATAPI
798 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
799 sc = inb(iobase1+ATA_CB_SC);
800 sn = inb(iobase1+ATA_CB_SN);
801 if (sc!=0x01 || sn!=0x01) {
802 init_drive_unknown(devid);
805 u8 cl = inb(iobase1+ATA_CB_CL);
806 u8 ch = inb(iobase1+ATA_CB_CH);
807 u8 st = inb(iobase1+ATA_CB_STAT);
809 if (cl==0x14 && ch==0xeb)
810 init_drive_atapi(devid);
811 else if (cl==0x00 && ch==0x00 && st!=0x00)
812 init_drive_ata(devid);
813 else if (cl==0xff && ch==0xff)
817 init_drive_unknown(devid);
820 // Store the device count
821 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
825 // FIXME : should use bios=cmos|auto|disable bits
826 // FIXME : should know about translation bits
827 // FIXME : move hard_drive_post here