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);
144 struct ata_pio_command {
159 // Send an ata command to the drive.
161 send_cmd(int driveid, struct ata_pio_command *cmd)
163 u8 channel = driveid / 2;
164 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
165 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
167 int status = inb(iobase1 + ATA_CB_STAT);
168 if (status & ATA_CB_STAT_BSY)
171 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
172 if (cmd->command & 0x04) {
173 outb(0x00, iobase1 + ATA_CB_FR);
174 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
175 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
176 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
177 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
179 outb(cmd->feature, iobase1 + ATA_CB_FR);
180 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
181 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
182 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
183 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
184 outb(cmd->device, iobase1 + ATA_CB_DH);
185 outb(cmd->command, iobase1 + ATA_CB_CMD);
189 status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
193 if (status & ATA_CB_STAT_ERR) {
194 DEBUGF("send_cmd : read error\n");
197 if (!(status & ATA_CB_STAT_DRQ)) {
198 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
199 , (unsigned) status);
206 // Read and discard x number of bytes from an io channel.
208 insx_discard(int mode, int iobase1, int bytes)
211 if (mode == ATA_MODE_PIO32) {
213 for (i=0; i<count; i++)
217 for (i=0; i<count; i++)
222 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
223 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
224 // and/or last block may be partially transferred. This function is
225 // inlined because all the callers use different forms and because the
226 // large number of parameters would consume a lot of stack space.
227 static __always_inline int
228 ata_transfer(int driveid, int iswrite, int count, int blocksize
229 , int skipfirst, int skiplast, void *far_buffer)
231 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
232 " skipf=%d skipl=%d buf=%p\n"
233 , driveid, iswrite, count, blocksize
234 , skipfirst, skiplast, far_buffer);
236 // Reset count of transferred data
237 SET_EBDA(ata.trsfsectors, 0);
239 u8 channel = driveid / 2;
240 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
241 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
242 u8 mode = GET_EBDA(ata.devices[driveid].mode);
246 int bsize = blocksize;
247 if (skipfirst && current == 0) {
248 insx_discard(mode, iobase1, skipfirst);
251 if (skiplast && current == count-1)
255 // Write data to controller
256 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
257 if (mode == ATA_MODE_PIO32)
258 outsl_far(iobase1, far_buffer, bsize / 4);
260 outsw_far(iobase1, far_buffer, bsize / 2);
262 // Read data from controller
263 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
264 if (mode == ATA_MODE_PIO32)
265 insl_far(iobase1, far_buffer, bsize / 4);
267 insw_far(iobase1, far_buffer, bsize / 2);
271 if (skiplast && current == count-1)
272 insx_discard(mode, iobase1, skiplast);
274 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
280 SET_EBDA(ata.trsfsectors, current);
281 if (current == count)
283 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
285 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
286 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
287 , (unsigned) status);
292 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
293 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
295 status &= ~ATA_CB_STAT_DF;
296 if (status != ATA_CB_STAT_RDY ) {
297 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
298 , (unsigned) status);
303 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
308 /****************************************************************
309 * ATA hard drive functions
310 ****************************************************************/
312 // Read/write count blocks from a harddrive.
314 ata_cmd_data(int driveid, u16 command, u32 lba, u16 count, void *far_buffer)
316 u8 slave = driveid % 2;
318 struct ata_pio_command cmd;
319 memset(&cmd, 0, sizeof(cmd));
321 cmd.command = command;
322 if (count >= (1<<8) || lba + count >= (1<<28)) {
323 cmd.sector_count2 = count >> 8;
324 cmd.lba_low2 = lba >> 24;
333 cmd.sector_count = count;
335 cmd.lba_mid = lba >> 8;
336 cmd.lba_high = lba >> 16;
337 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
338 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
340 int ret = send_cmd(driveid, &cmd);
344 int iswrite = command == ATA_CMD_WRITE_SECTORS;
345 return ata_transfer(driveid, iswrite, count, IDE_SECTOR_SIZE
350 /****************************************************************
352 ****************************************************************/
354 // Low-level atapi command transmit function.
356 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
358 u8 channel = driveid / 2;
359 u8 slave = driveid % 2;
360 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
361 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
363 struct ata_pio_command cmd;
364 cmd.sector_count = 0;
367 cmd.lba_mid = blocksize;
368 cmd.lba_high = blocksize >> 8;
369 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
370 cmd.command = ATA_CMD_PACKET;
372 int ret = send_cmd(driveid, &cmd);
376 // Send command to device
377 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
379 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
386 // Low-level cdrom read atapi command transmit function.
387 static __always_inline int
388 send_cdrom_cmd(int driveid, u32 lba, u16 count)
391 memset(atacmd, 0, sizeof(atacmd));
393 atacmd[0]=0x28; // READ command
394 atacmd[7]=(count & 0xff00) >> 8; // Sectors
395 atacmd[8]=(count & 0x00ff); // Sectors
396 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
397 atacmd[3]=(lba & 0x00ff0000) >> 16;
398 atacmd[4]=(lba & 0x0000ff00) >> 8;
399 atacmd[5]=(lba & 0x000000ff);
401 return send_atapi_cmd(driveid, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
404 // Read sectors from the cdrom.
406 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
408 int ret = send_cdrom_cmd(driveid, lba, count);
412 return ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE, 0, 0, far_buffer);
415 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
418 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
420 u32 velba = vlba + vcount - 1;
422 u32 elba = velba / 4;
423 int count = elba - lba + 1;
424 int before = vlba % 4;
425 int after = 3 - (velba % 4);
427 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
428 " count=%d before=%d after=%d\n"
429 , driveid, vlba, vcount, far_buffer, lba, elba
430 , count, before, after);
432 int ret = send_cdrom_cmd(driveid, lba, count);
436 ret = ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE
437 , before*512, after*512, far_buffer);
439 SET_EBDA(ata.trsfsectors, 0);
442 SET_EBDA(ata.trsfsectors, vcount);
446 // Send a simple atapi command to a drive.
448 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
449 , u32 length, void *far_buffer)
451 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
455 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
459 /****************************************************************
460 * ATA detect and init
461 ****************************************************************/
464 report_model(int driveid, u8 *buffer)
470 for (i=0; i<40; i+=2) {
471 model[i] = buffer[i+54+1];
472 model[i+1] = buffer[i+54];
477 for (i=39; i>0; i--) {
478 if (model[i] != 0x20)
483 u8 channel = driveid / 2;
484 u8 slave = driveid % 2;
485 // XXX - model on stack not %cs
486 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
490 get_ata_version(u8 *buffer)
492 u16 ataversion = *(u16*)&buffer[160];
494 for (version=15; version>0; version--)
495 if (ataversion & (1<<version))
501 init_drive_atapi(int driveid)
503 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
505 // Temporary values to do the transfer
506 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
507 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
509 // Now we send a IDENTIFY command to ATAPI device
511 memset(buffer, 0, sizeof(buffer));
512 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
514 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
516 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
518 u8 type = buffer[1] & 0x1f;
519 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
520 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
521 u16 blksize = CDROM_SECTOR_SIZE;
523 SET_EBDA(ata.devices[driveid].device, type);
524 SET_EBDA(ata.devices[driveid].removable, removable);
525 SET_EBDA(ata.devices[driveid].mode, mode);
526 SET_EBDA(ata.devices[driveid].blksize, blksize);
529 u8 cdcount = GET_EBDA(ata.cdcount);
530 SET_EBDA(ata.idmap[1][cdcount], driveid);
531 SET_EBDA(ata.cdcount, ++cdcount);
533 report_model(driveid, buffer);
534 u8 version = get_ata_version(buffer);
535 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
536 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
538 printf(" ATAPI-%d Device\n", version);
542 init_drive_ata(int driveid)
544 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA);
546 // Temporary values to do the transfer
547 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
548 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
550 // Now we send a IDENTIFY command to ATA device
552 memset(buffer, 0, sizeof(buffer));
553 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
555 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
557 BX_PANIC("ata-detect: Failed to detect ATA device\n");
559 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
560 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
561 u16 blksize = *(u16*)&buffer[10];
563 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
564 u16 heads = *(u16*)&buffer[3*2]; // word 3
565 u16 spt = *(u16*)&buffer[6*2]; // word 6
567 u32 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
569 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD);
570 SET_EBDA(ata.devices[driveid].removable, removable);
571 SET_EBDA(ata.devices[driveid].mode, mode);
572 SET_EBDA(ata.devices[driveid].blksize, blksize);
573 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
574 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
575 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
576 SET_EBDA(ata.devices[driveid].sectors, sectors);
578 u8 channel = driveid / 2;
579 u8 slave = driveid % 2;
580 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
582 for (shift=driveid%4; shift>0; shift--)
586 SET_EBDA(ata.devices[driveid].translation, translation);
588 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
589 , channel, slave, cylinders, heads, spt);
590 switch (translation) {
591 case ATA_TRANSLATION_NONE:
594 case ATA_TRANSLATION_LBA:
598 heads = sectors / 1024;
609 cylinders = sectors / heads;
611 case ATA_TRANSLATION_RECHS:
613 // Take care not to overflow
618 cylinders = (u16)((u32)(cylinders)*16/15);
620 // then go through the large bitshift process
621 case ATA_TRANSLATION_LARGE:
622 if (translation == ATA_TRANSLATION_LARGE)
624 while (cylinders > 1024) {
628 // If we max out the head count
634 // clip to 1024 cylinders in lchs
635 if (cylinders > 1024)
637 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
639 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
640 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
641 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
644 u8 hdcount = GET_EBDA(ata.hdcount);
645 SET_EBDA(ata.idmap[0][hdcount], driveid);
646 SET_EBDA(ata.hdcount, ++hdcount);
648 u32 sizeinmb = GET_EBDA(ata.devices[driveid].sectors);
651 report_model(driveid, buffer);
652 u8 version = get_ata_version(buffer);
653 if (sizeinmb < (1 << 16))
654 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
656 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
660 init_drive_unknown(int driveid)
662 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
664 u8 channel = driveid / 2;
665 u8 slave = driveid % 2;
666 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
672 #if CONFIG_MAX_ATA_INTERFACES > 0
673 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
674 SET_EBDA(ata.channels[0].iobase1,0x1f0);
675 SET_EBDA(ata.channels[0].iobase2,0x3f0);
676 SET_EBDA(ata.channels[0].irq,14);
678 #if CONFIG_MAX_ATA_INTERFACES > 1
679 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
680 SET_EBDA(ata.channels[1].iobase1,0x170);
681 SET_EBDA(ata.channels[1].iobase2,0x370);
682 SET_EBDA(ata.channels[1].irq,15);
684 #if CONFIG_MAX_ATA_INTERFACES > 2
685 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
686 SET_EBDA(ata.channels[2].iobase1,0x1e8);
687 SET_EBDA(ata.channels[2].iobase2,0x3e0);
688 SET_EBDA(ata.channels[2].irq,12);
690 #if CONFIG_MAX_ATA_INTERFACES > 3
691 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
692 SET_EBDA(ata.channels[3].iobase1,0x168);
693 SET_EBDA(ata.channels[3].iobase2,0x360);
694 SET_EBDA(ata.channels[3].irq,11);
696 #if CONFIG_MAX_ATA_INTERFACES > 4
697 #error Please fill the ATA interface informations
702 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
703 u8 channel = driveid / 2;
704 u8 slave = driveid % 2;
706 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
707 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
709 // Disable interrupts
710 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
713 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
714 outb(0x55, iobase1+ATA_CB_SC);
715 outb(0xaa, iobase1+ATA_CB_SN);
716 outb(0xaa, iobase1+ATA_CB_SC);
717 outb(0x55, iobase1+ATA_CB_SN);
718 outb(0x55, iobase1+ATA_CB_SC);
719 outb(0xaa, iobase1+ATA_CB_SN);
721 // If we found something
722 u8 sc = inb(iobase1+ATA_CB_SC);
723 u8 sn = inb(iobase1+ATA_CB_SN);
725 if (sc != 0x55 || sn != 0xaa)
731 // check for ATA or ATAPI
732 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
733 sc = inb(iobase1+ATA_CB_SC);
734 sn = inb(iobase1+ATA_CB_SN);
735 if (sc!=0x01 || sn!=0x01) {
736 init_drive_unknown(driveid);
739 u8 cl = inb(iobase1+ATA_CB_CL);
740 u8 ch = inb(iobase1+ATA_CB_CH);
741 u8 st = inb(iobase1+ATA_CB_STAT);
743 if (cl==0x14 && ch==0xeb)
744 init_drive_atapi(driveid);
745 else if (cl==0x00 && ch==0x00 && st!=0x00)
746 init_drive_ata(driveid);
747 else if (cl==0xff && ch==0xff)
751 init_drive_unknown(driveid);
754 // Store the device count
755 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
759 // FIXME : should use bios=cmos|auto|disable bits
760 // FIXME : should know about translation bits
761 // FIXME : move hard_drive_post here
767 // hdidmap and cdidmap init.
769 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
770 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
771 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
776 fill_hdinfo(int hdnum, u8 typecmos, u8 basecmos)
778 u8 type = inb_cmos(typecmos);
783 SET_EBDA(fdpt[hdnum].precompensation, ((inb_cmos(basecmos+4) << 8)
784 | inb_cmos(basecmos+3)));
785 SET_EBDA(fdpt[hdnum].drive_control_byte, inb_cmos(basecmos+5));
786 SET_EBDA(fdpt[hdnum].landing_zone, ((inb_cmos(basecmos+7) << 8)
787 | inb_cmos(basecmos+6)));
788 u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
789 u8 heads = inb_cmos(basecmos+2);
790 u8 sectors = inb_cmos(basecmos+8);
792 // no logical CHS mapping used, just physical CHS
793 // use Standard Fixed Disk Parameter Table (FDPT)
794 SET_EBDA(fdpt[hdnum].cylinders, cyl);
795 SET_EBDA(fdpt[hdnum].heads, heads);
796 SET_EBDA(fdpt[hdnum].sectors, sectors);
800 // complies with Phoenix style Translated Fixed Disk Parameter
802 SET_EBDA(fdpt[hdnum].phys_cylinders, cyl);
803 SET_EBDA(fdpt[hdnum].phys_heads, heads);
804 SET_EBDA(fdpt[hdnum].phys_sectors, sectors);
805 SET_EBDA(fdpt[hdnum].sectors, sectors);
806 SET_EBDA(fdpt[hdnum].a0h_signature, 0xa0);
810 } else if (cyl > 4096) {
813 } else if (cyl > 2048) {
817 SET_EBDA(fdpt[hdnum].cylinders, cyl);
818 SET_EBDA(fdpt[hdnum].heads, heads);
819 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
821 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
823 SET_EBDA(fdpt[hdnum].checksum, -sum);
829 outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14
830 SET_BDA(disk_count, 1);
831 SET_BDA(disk_control_byte, 0xc0);
833 // move disk geometry data from CMOS to EBDA disk parameter table(s)
834 u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
835 if ((diskinfo & 0xf0) == 0xf0)
836 // Fill EBDA table for hard disk 0.
837 fill_hdinfo(0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
838 if ((diskinfo & 0x0f) == 0x0f)
839 // XXX - bochs halts on any other type
840 // Fill EBDA table for hard disk 1.
841 fill_hdinfo(1, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);