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_SECTOR_SIZE 512
22 #define CDROM_SECTOR_SIZE 2048
24 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
26 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
27 #define DEBUGF(fmt, args...)
30 /****************************************************************
32 ****************************************************************/
34 // Wait for the specified ide state
36 await_ide(u8 when_done, u16 base, u16 timeout)
40 u8 status = inb(base+ATA_CB_STAT);
44 result = status & ATA_CB_STAT_BSY;
45 else if (when_done == NOT_BSY)
46 result = !(status & ATA_CB_STAT_BSY);
47 else if (when_done == NOT_BSY_DRQ)
48 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
49 else if (when_done == NOT_BSY_NOT_DRQ)
50 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
51 else if (when_done == NOT_BSY_RDY)
52 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
56 // mod 2048 each 16 ms
57 if (time>>16 != last) {
59 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
60 " %d time= %d timeout= %d\n"
61 , when_done, time>>11, timeout);
63 if (status & ATA_CB_STAT_ERR) {
64 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
65 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
66 , when_done, time>>11, timeout);
69 if (timeout == 0 || (time>>11) > timeout)
72 BX_INFO("IDE time out\n");
76 // Wait for ide state - pauses for one ata cycle first.
77 static __always_inline int
78 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
80 // Wait one PIO transfer cycle.
81 inb(iobase2 + ATA_CB_ASTAT);
83 return await_ide(when_done, iobase1, timeout);
86 // Delay for x nanoseconds
90 // XXX - how to implement ndelay?
97 ata_reset(int driveid)
100 u8 channel, slave, sn, sc;
103 channel = driveid / 2;
106 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
107 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
111 // 8.2.1 (a) -- set SRST in DC
112 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
114 // 8.2.1 (b) -- wait for BSY
115 await_ide(BSY, iobase1, 20);
117 // 8.2.1 (f) -- clear SRST
118 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
120 type=GET_EBDA(ata.devices[driveid].type);
121 if (type != ATA_TYPE_NONE) {
123 // 8.2.1 (g) -- check for sc==sn==0x01
125 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
126 sc = inb(iobase1+ATA_CB_SC);
127 sn = inb(iobase1+ATA_CB_SN);
129 if ( (sc==0x01) && (sn==0x01) ) {
130 if (type == ATA_TYPE_ATA) //ATA
131 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
133 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
136 // 8.2.1 (h) -- wait for not BSY
137 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
141 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
145 /****************************************************************
147 ****************************************************************/
156 struct ata_pio_command {
171 // Send an ata command to the drive.
173 send_cmd(int driveid, struct ata_pio_command *cmd)
175 u8 channel = driveid / 2;
176 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
177 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
179 int status = inb(iobase1 + ATA_CB_STAT);
180 if (status & ATA_CB_STAT_BSY)
183 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
184 if (cmd->command & 0x04) {
185 outb(0x00, iobase1 + ATA_CB_FR);
186 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
187 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
188 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
189 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
191 outb(cmd->feature, iobase1 + ATA_CB_FR);
192 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
193 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
194 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
195 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
196 outb(cmd->device, iobase1 + ATA_CB_DH);
197 outb(cmd->command, iobase1 + ATA_CB_CMD);
201 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
205 if (status & ATA_CB_STAT_ERR) {
206 DEBUGF("send_cmd : read error\n");
209 if (!(status & ATA_CB_STAT_DRQ)) {
210 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
211 , (unsigned) status);
219 /****************************************************************
221 ****************************************************************/
223 // Read and discard x number of bytes from an io channel.
225 insx_discard(int mode, int iobase1, int bytes)
228 if (mode == ATA_MODE_PIO32) {
230 for (i=0; i<count; i++)
234 for (i=0; i<count; i++)
239 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
240 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
241 // and/or last block may be partially transferred. This function is
242 // inlined because all the callers use different forms and because the
243 // large number of parameters would consume a lot of stack space.
244 static __always_inline int
245 ata_transfer(int driveid, int iswrite, int count, int blocksize
246 , int skipfirst, int skiplast, void *far_buffer)
248 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
249 " skipf=%d skipl=%d buf=%p\n"
250 , driveid, iswrite, count, blocksize
251 , skipfirst, skiplast, far_buffer);
253 // Reset count of transferred data
254 SET_EBDA(ata.trsfsectors, 0);
256 u8 channel = driveid / 2;
257 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
258 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
259 u8 mode = GET_EBDA(ata.devices[driveid].mode);
263 int bsize = blocksize;
264 if (skipfirst && current == 0) {
265 insx_discard(mode, iobase1, skipfirst);
268 if (skiplast && current == count-1)
272 // Write data to controller
273 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
274 if (mode == ATA_MODE_PIO32)
275 outsl_far(iobase1, far_buffer, bsize / 4);
277 outsw_far(iobase1, far_buffer, bsize / 2);
279 // Read data from controller
280 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
281 if (mode == ATA_MODE_PIO32)
282 insl_far(iobase1, far_buffer, bsize / 4);
284 insw_far(iobase1, far_buffer, bsize / 2);
288 if (skiplast && current == count-1)
289 insx_discard(mode, iobase1, skiplast);
291 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
297 SET_EBDA(ata.trsfsectors, current);
298 if (current == count)
300 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
302 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
303 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
304 , (unsigned) status);
309 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
310 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
312 status &= ~ATA_CB_STAT_DF;
313 if (status != ATA_CB_STAT_RDY ) {
314 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
315 , (unsigned) status);
320 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
325 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
327 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
328 , 0, 0, op->far_buffer);
332 ata_transfer_cdrom(const struct ata_op_s *op)
334 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
335 , 0, 0, op->far_buffer);
339 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
341 int vcount = op->count * 4 - before - after;
342 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
343 , before*512, after*512, op->far_buffer);
345 SET_EBDA(ata.trsfsectors, 0);
348 SET_EBDA(ata.trsfsectors, vcount);
353 /****************************************************************
354 * ATA hard drive functions
355 ****************************************************************/
358 send_cmd_disk(const struct ata_op_s *op, u16 command)
360 u8 slave = op->driveid % 2;
363 struct ata_pio_command cmd;
364 memset(&cmd, 0, sizeof(cmd));
366 cmd.command = command;
367 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
368 cmd.sector_count2 = op->count >> 8;
369 cmd.lba_low2 = lba >> 24;
378 cmd.sector_count = op->count;
380 cmd.lba_mid = lba >> 8;
381 cmd.lba_high = lba >> 16;
382 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
383 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
385 return send_cmd(op->driveid, &cmd);
388 // Read/write count blocks from a harddrive.
390 ata_cmd_data(int driveid, u16 command, u32 lba, u16 count, void *far_buffer)
393 op.driveid = driveid;
396 op.far_buffer = far_buffer;
398 int ret = send_cmd_disk(&op, command);
402 int iswrite = command == ATA_CMD_WRITE_SECTORS;
403 return ata_transfer_disk(&op, iswrite);
407 /****************************************************************
409 ****************************************************************/
411 // Low-level atapi command transmit function.
412 static __always_inline int
413 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
415 u8 channel = driveid / 2;
416 u8 slave = driveid % 2;
417 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
418 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
420 struct ata_pio_command cmd;
421 cmd.sector_count = 0;
424 cmd.lba_mid = blocksize;
425 cmd.lba_high = blocksize >> 8;
426 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
427 cmd.command = ATA_CMD_PACKET;
429 int ret = send_cmd(driveid, &cmd);
433 // Send command to device
434 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
436 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
443 // Low-level cdrom read atapi command transmit function.
445 send_cmd_cdrom(const struct ata_op_s *op)
448 memset(atacmd, 0, sizeof(atacmd));
450 atacmd[0]=0x28; // READ command
451 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
452 atacmd[8]=(op->count & 0x00ff);
453 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
454 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
455 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
456 atacmd[5]=(op->lba & 0x000000ff);
458 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
459 , CDROM_SECTOR_SIZE);
462 // Read sectors from the cdrom.
464 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
467 op.driveid = driveid;
470 op.far_buffer = far_buffer;
472 int ret = send_cmd_cdrom(&op);
476 return ata_transfer_cdrom(&op);
479 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
482 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
484 u32 velba = vlba + vcount - 1;
486 u32 elba = velba / 4;
487 int count = elba - lba + 1;
488 int before = vlba % 4;
489 int after = 3 - (velba % 4);
492 op.driveid = driveid;
495 op.far_buffer = far_buffer;
497 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
498 " count=%d before=%d after=%d\n"
499 , driveid, vlba, vcount, far_buffer, lba, elba
500 , count, before, after);
502 int ret = send_cmd_cdrom(&op);
506 return ata_transfer_emu(&op, before, after);
509 // Send a simple atapi command to a drive.
511 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
512 , u32 length, void *far_buffer)
514 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
518 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
522 /****************************************************************
523 * ATA detect and init
524 ****************************************************************/
527 report_model(int driveid, u8 *buffer)
533 for (i=0; i<40; i+=2) {
534 model[i] = buffer[i+54+1];
535 model[i+1] = buffer[i+54];
540 for (i=39; i>0; i--) {
541 if (model[i] != 0x20)
546 u8 channel = driveid / 2;
547 u8 slave = driveid % 2;
548 // XXX - model on stack not %cs
549 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
553 get_ata_version(u8 *buffer)
555 u16 ataversion = *(u16*)&buffer[160];
557 for (version=15; version>0; version--)
558 if (ataversion & (1<<version))
564 init_drive_atapi(int driveid)
566 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
568 // Temporary values to do the transfer
569 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
570 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
572 // Now we send a IDENTIFY command to ATAPI device
574 memset(buffer, 0, sizeof(buffer));
575 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
577 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
579 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
581 u8 type = buffer[1] & 0x1f;
582 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
583 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
584 u16 blksize = CDROM_SECTOR_SIZE;
586 SET_EBDA(ata.devices[driveid].device, type);
587 SET_EBDA(ata.devices[driveid].removable, removable);
588 SET_EBDA(ata.devices[driveid].mode, mode);
589 SET_EBDA(ata.devices[driveid].blksize, blksize);
592 u8 cdcount = GET_EBDA(ata.cdcount);
593 SET_EBDA(ata.idmap[1][cdcount], driveid);
594 SET_EBDA(ata.cdcount, ++cdcount);
596 report_model(driveid, buffer);
597 u8 version = get_ata_version(buffer);
598 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
599 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
601 printf(" ATAPI-%d Device\n", version);
605 init_drive_ata(int driveid)
607 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA);
609 // Temporary values to do the transfer
610 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
611 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
613 // Now we send a IDENTIFY command to ATA device
615 memset(buffer, 0, sizeof(buffer));
616 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
618 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
620 BX_PANIC("ata-detect: Failed to detect ATA device\n");
622 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
623 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
624 u16 blksize = *(u16*)&buffer[10];
626 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
627 u16 heads = *(u16*)&buffer[3*2]; // word 3
628 u16 spt = *(u16*)&buffer[6*2]; // word 6
630 u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
632 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD);
633 SET_EBDA(ata.devices[driveid].removable, removable);
634 SET_EBDA(ata.devices[driveid].mode, mode);
635 SET_EBDA(ata.devices[driveid].blksize, blksize);
636 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
637 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
638 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
639 SET_EBDA(ata.devices[driveid].sectors, sectors);
641 u8 channel = driveid / 2;
642 u8 slave = driveid % 2;
643 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
645 for (shift=driveid%4; shift>0; shift--)
649 SET_EBDA(ata.devices[driveid].translation, translation);
651 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
652 , channel, slave, cylinders, heads, spt);
653 switch (translation) {
654 case ATA_TRANSLATION_NONE:
657 case ATA_TRANSLATION_LBA:
661 heads = sectors / 1024;
672 cylinders = sectors / heads;
674 case ATA_TRANSLATION_RECHS:
676 // Take care not to overflow
681 cylinders = (u16)((u32)(cylinders)*16/15);
683 // then go through the large bitshift process
684 case ATA_TRANSLATION_LARGE:
685 if (translation == ATA_TRANSLATION_LARGE)
687 while (cylinders > 1024) {
691 // If we max out the head count
697 // clip to 1024 cylinders in lchs
698 if (cylinders > 1024)
700 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
702 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
703 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
704 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
707 u8 hdcount = GET_EBDA(ata.hdcount);
708 SET_EBDA(ata.idmap[0][hdcount], driveid);
709 SET_EBDA(ata.hdcount, ++hdcount);
711 u32 sizeinmb = GET_EBDA(ata.devices[driveid].sectors);
714 report_model(driveid, buffer);
715 u8 version = get_ata_version(buffer);
716 if (sizeinmb < (1 << 16))
717 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
719 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
723 init_drive_unknown(int driveid)
725 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
727 u8 channel = driveid / 2;
728 u8 slave = driveid % 2;
729 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
735 #if CONFIG_MAX_ATA_INTERFACES > 0
736 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
737 SET_EBDA(ata.channels[0].iobase1,0x1f0);
738 SET_EBDA(ata.channels[0].iobase2,0x3f0);
739 SET_EBDA(ata.channels[0].irq,14);
741 #if CONFIG_MAX_ATA_INTERFACES > 1
742 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
743 SET_EBDA(ata.channels[1].iobase1,0x170);
744 SET_EBDA(ata.channels[1].iobase2,0x370);
745 SET_EBDA(ata.channels[1].irq,15);
747 #if CONFIG_MAX_ATA_INTERFACES > 2
748 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
749 SET_EBDA(ata.channels[2].iobase1,0x1e8);
750 SET_EBDA(ata.channels[2].iobase2,0x3e0);
751 SET_EBDA(ata.channels[2].irq,12);
753 #if CONFIG_MAX_ATA_INTERFACES > 3
754 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
755 SET_EBDA(ata.channels[3].iobase1,0x168);
756 SET_EBDA(ata.channels[3].iobase2,0x360);
757 SET_EBDA(ata.channels[3].irq,11);
759 #if CONFIG_MAX_ATA_INTERFACES > 4
760 #error Please fill the ATA interface informations
765 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
766 u8 channel = driveid / 2;
767 u8 slave = driveid % 2;
769 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
770 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
772 // Disable interrupts
773 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
776 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
777 outb(0x55, iobase1+ATA_CB_SC);
778 outb(0xaa, iobase1+ATA_CB_SN);
779 outb(0xaa, iobase1+ATA_CB_SC);
780 outb(0x55, iobase1+ATA_CB_SN);
781 outb(0x55, iobase1+ATA_CB_SC);
782 outb(0xaa, iobase1+ATA_CB_SN);
784 // If we found something
785 u8 sc = inb(iobase1+ATA_CB_SC);
786 u8 sn = inb(iobase1+ATA_CB_SN);
788 if (sc != 0x55 || sn != 0xaa)
794 // check for ATA or ATAPI
795 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
796 sc = inb(iobase1+ATA_CB_SC);
797 sn = inb(iobase1+ATA_CB_SN);
798 if (sc!=0x01 || sn!=0x01) {
799 init_drive_unknown(driveid);
802 u8 cl = inb(iobase1+ATA_CB_CL);
803 u8 ch = inb(iobase1+ATA_CB_CH);
804 u8 st = inb(iobase1+ATA_CB_STAT);
806 if (cl==0x14 && ch==0xeb)
807 init_drive_atapi(driveid);
808 else if (cl==0x00 && ch==0x00 && st!=0x00)
809 init_drive_ata(driveid);
810 else if (cl==0xff && ch==0xff)
814 init_drive_unknown(driveid);
817 // Store the device count
818 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
822 // FIXME : should use bios=cmos|auto|disable bits
823 // FIXME : should know about translation bits
824 // FIXME : move hard_drive_post here
830 // hdidmap and cdidmap init.
832 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
833 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
834 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
839 fill_hdinfo(int hdnum, u8 typecmos, u8 basecmos)
841 u8 type = inb_cmos(typecmos);
846 SET_EBDA(fdpt[hdnum].precompensation, ((inb_cmos(basecmos+4) << 8)
847 | inb_cmos(basecmos+3)));
848 SET_EBDA(fdpt[hdnum].drive_control_byte, inb_cmos(basecmos+5));
849 SET_EBDA(fdpt[hdnum].landing_zone, ((inb_cmos(basecmos+7) << 8)
850 | inb_cmos(basecmos+6)));
851 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
852 u8 heads = inb_cmos(basecmos+2);
853 u8 sectors = inb_cmos(basecmos+8);
855 // no logical CHS mapping used, just physical CHS
856 // use Standard Fixed Disk Parameter Table (FDPT)
857 SET_EBDA(fdpt[hdnum].cylinders, cyl);
858 SET_EBDA(fdpt[hdnum].heads, heads);
859 SET_EBDA(fdpt[hdnum].sectors, sectors);
863 // complies with Phoenix style Translated Fixed Disk Parameter
865 SET_EBDA(fdpt[hdnum].phys_cylinders, cyl);
866 SET_EBDA(fdpt[hdnum].phys_heads, heads);
867 SET_EBDA(fdpt[hdnum].phys_sectors, sectors);
868 SET_EBDA(fdpt[hdnum].sectors, sectors);
869 SET_EBDA(fdpt[hdnum].a0h_signature, 0xa0);
873 } else if (cyl > 4096) {
876 } else if (cyl > 2048) {
880 SET_EBDA(fdpt[hdnum].cylinders, cyl);
881 SET_EBDA(fdpt[hdnum].heads, heads);
882 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
884 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
886 SET_EBDA(fdpt[hdnum].checksum, -sum);
892 outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14
893 SET_BDA(disk_count, 1);
894 SET_BDA(disk_control_byte, 0xc0);
896 // move disk geometry data from CMOS to EBDA disk parameter table(s)
897 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
898 if ((diskinfo & 0xf0) == 0xf0)
899 // Fill EBDA table for hard disk 0.
900 fill_hdinfo(0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
901 if ((diskinfo & 0x0f) == 0x0f)
902 // XXX - bochs halts on any other type
903 // Fill EBDA table for hard disk 1.
904 fill_hdinfo(1, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);