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 LGPLv3 license.
8 #include "types.h" // u8
9 #include "ioport.h" // inb
10 #include "util.h" // dprintf
11 #include "cmos.h" // inb_cmos
12 #include "pic.h" // enable_hwirq
13 #include "biosvar.h" // GET_EBDA
14 #include "pci.h" // pci_find_class
15 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16 #include "pci_regs.h" // PCI_INTERRUPT_LINE
17 #include "boot.h" // add_bcv_hd
18 #include "disk.h" // struct ata_s
19 #include "atabits.h" // ATA_CB_STAT
21 #define IDE_SECTOR_SIZE 512
22 #define CDROM_SECTOR_SIZE 2048
24 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
26 struct ata_s ATA VAR16_32;
29 /****************************************************************
31 ****************************************************************/
33 // Wait for the specified ide state
35 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
37 u64 end = calc_future_tsc(timeout);
39 u8 status = inb(base+ATA_CB_STAT);
40 if ((status & mask) == flags)
42 if (rdtscll() > end) {
43 dprintf(1, "IDE time out\n");
49 // Wait for the device to be not-busy.
51 await_not_bsy(u16 base)
53 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
56 // Wait for the device to be ready.
60 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
63 // Wait for ide state - pauses for one ata cycle first.
65 pause_await_not_bsy(u16 iobase1, u16 iobase2)
67 // Wait one PIO transfer cycle.
68 inb(iobase2 + ATA_CB_ASTAT);
70 return await_not_bsy(iobase1);
73 // Wait for ide state - pause for 400ns first.
75 ndelay_await_not_bsy(u16 iobase1)
78 return await_not_bsy(iobase1);
83 ata_reset(int driveid)
85 u8 channel = driveid / 2;
86 u8 slave = driveid % 2;
87 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
88 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
90 dprintf(6, "ata_reset driveid=%d\n", driveid);
92 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
94 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
97 // wait for device to become not busy.
98 int status = await_not_bsy(iobase1);
103 u64 end = calc_future_tsc(IDE_TIMEOUT);
105 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
106 status = ndelay_await_not_bsy(iobase1);
109 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
111 // Change drive request failed to take effect - retry.
112 if (rdtscll() > end) {
113 dprintf(1, "ata_reset slave time out\n");
119 // On a user-reset request, wait for RDY if it is an ATA device.
120 u8 type=GET_GLOBAL(ATA.devices[driveid].type);
121 if (type == DTYPE_ATA)
122 status = await_rdy(iobase1);
126 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
128 dprintf(6, "ata_reset exit status=%x\n", status);
134 // Read the status from controller
135 u8 channel = driveid / 2;
136 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
137 u8 status = inb(iobase1 + ATA_CB_STAT);
138 return (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY;
142 process_ata_misc_op(struct disk_op_s *op)
144 switch (op->command) {
148 ata_reset(op->driveid);
151 return isready(op->driveid);
156 /****************************************************************
158 ****************************************************************/
160 struct ata_pio_command {
175 // Send an ata command to the drive.
177 send_cmd(int driveid, struct ata_pio_command *cmd)
179 u8 channel = driveid / 2;
180 u8 slave = driveid % 2;
181 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
182 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
184 // Disable interrupts
185 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
188 int status = await_not_bsy(iobase1);
191 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
192 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
193 u8 olddh = inb(iobase1 + ATA_CB_DH);
194 outb(newdh, iobase1 + ATA_CB_DH);
195 if ((olddh ^ newdh) & (1<<4)) {
196 // Was a device change - wait for device to become not busy.
197 status = ndelay_await_not_bsy(iobase1);
202 if (cmd->command & 0x04) {
203 outb(0x00, iobase1 + ATA_CB_FR);
204 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
205 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
206 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
207 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
209 outb(cmd->feature, iobase1 + ATA_CB_FR);
210 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
211 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
212 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
213 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
214 outb(cmd->command, iobase1 + ATA_CB_CMD);
216 status = ndelay_await_not_bsy(iobase1);
220 if (status & ATA_CB_STAT_ERR) {
221 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
222 , status, inb(iobase1 + ATA_CB_ERR));
225 if (!(status & ATA_CB_STAT_DRQ)) {
226 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
234 /****************************************************************
236 ****************************************************************/
238 // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
241 ata_transfer(struct disk_op_s *op, int iswrite, int blocksize)
243 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d buf=%p\n"
244 , op->driveid, iswrite, op->count, blocksize, op->buf_fl);
246 u8 channel = op->driveid / 2;
247 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
248 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
249 int count = op->count;
250 void *buf_fl = op->buf_fl;
254 // Write data to controller
255 dprintf(16, "Write sector id=%d dest=%p\n", op->driveid, buf_fl);
256 if (CONFIG_ATA_PIO32)
257 outsl_fl(iobase1, buf_fl, blocksize / 4);
259 outsw_fl(iobase1, buf_fl, blocksize / 2);
261 // Read data from controller
262 dprintf(16, "Read sector id=%d dest=%p\n", op->driveid, buf_fl);
263 if (CONFIG_ATA_PIO32)
264 insl_fl(iobase1, buf_fl, blocksize / 4);
266 insw_fl(iobase1, buf_fl, blocksize / 2);
270 status = pause_await_not_bsy(iobase1, iobase2);
280 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
281 if (status != ATA_CB_STAT_DRQ) {
282 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
289 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
292 status &= ~ATA_CB_STAT_DF;
294 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
299 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
304 /****************************************************************
305 * ATA hard drive functions
306 ****************************************************************/
308 // Read/write count blocks from a harddrive.
310 ata_cmd_data(struct disk_op_s *op, int iswrite, int command)
314 struct ata_pio_command cmd;
315 memset(&cmd, 0, sizeof(cmd));
317 cmd.command = command;
318 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
319 cmd.sector_count2 = op->count >> 8;
320 cmd.lba_low2 = lba >> 24;
321 cmd.lba_mid2 = lba >> 32;
322 cmd.lba_high2 = lba >> 40;
329 cmd.sector_count = op->count;
331 cmd.lba_mid = lba >> 8;
332 cmd.lba_high = lba >> 16;
333 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
335 int ret = send_cmd(op->driveid, &cmd);
338 return ata_transfer(op, iswrite, IDE_SECTOR_SIZE);
342 process_ata_op(struct disk_op_s *op)
344 switch (op->command) {
346 return ata_cmd_data(op, 0, ATA_CMD_READ_SECTORS);
348 return ata_cmd_data(op, 1, ATA_CMD_WRITE_SECTORS);
350 return process_ata_misc_op(op);
355 /****************************************************************
357 ****************************************************************/
359 // Low-level atapi command transmit function.
361 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
363 u8 channel = driveid / 2;
364 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
365 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
367 struct ata_pio_command cmd;
368 cmd.sector_count = 0;
371 cmd.lba_mid = blocksize;
372 cmd.lba_high = blocksize >> 8;
374 cmd.command = ATA_CMD_PACKET;
376 int ret = send_cmd(driveid, &cmd);
380 // Send command to device
381 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
383 int status = pause_await_not_bsy(iobase1, iobase2);
387 if (status & ATA_CB_STAT_ERR) {
388 u8 err = inb(iobase1 + ATA_CB_ERR);
391 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
395 if (!(status & ATA_CB_STAT_DRQ)) {
396 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
403 // Read sectors from the cdrom.
405 cdrom_read(struct disk_op_s *op)
408 memset(atacmd, 0, sizeof(atacmd));
409 atacmd[0]=0x28; // READ command
410 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
411 atacmd[8]=(op->count & 0x00ff);
412 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
413 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
414 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
415 atacmd[5]=(op->lba & 0x000000ff);
417 int ret = send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
418 , CDROM_SECTOR_SIZE);
422 return ata_transfer(op, 0, CDROM_SECTOR_SIZE);
426 process_atapi_op(struct disk_op_s *op)
428 switch (op->command) {
430 return cdrom_read(op);
432 return process_ata_misc_op(op);
436 // Send a simple atapi command to a drive.
438 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
439 , u32 length, void *buf_fl)
441 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
445 struct disk_op_s dop;
446 memset(&dop, 0, sizeof(dop));
447 dop.driveid = driveid;
451 return ata_transfer(&dop, 0, length);
455 /****************************************************************
456 * Disk geometry translation
457 ****************************************************************/
460 get_translation(int driveid)
462 if (! CONFIG_COREBOOT) {
463 // Emulators pass in the translation info via nvram.
464 u8 channel = driveid / 2;
465 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
466 translation >>= 2 * (driveid % 4);
471 // On COREBOOT, use a heuristic to determine translation type.
472 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
473 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
474 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
476 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
477 return TRANSLATION_NONE;
478 if (cylinders * heads <= 131072)
479 return TRANSLATION_LARGE;
480 return TRANSLATION_LBA;
484 setup_translation(int driveid)
486 u8 translation = get_translation(driveid);
487 SET_GLOBAL(ATA.devices[driveid].translation, translation);
489 u8 channel = driveid / 2;
490 u8 slave = driveid % 2;
491 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
492 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
493 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
494 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
496 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
497 , channel, slave, cylinders, heads, spt);
498 switch (translation) {
499 case TRANSLATION_NONE:
502 case TRANSLATION_LBA:
505 if (sectors > 63*255*1024) {
510 u32 sect = (u32)sectors / 63;
522 cylinders = sect / heads;
524 case TRANSLATION_RECHS:
525 dprintf(1, "r-echs");
526 // Take care not to overflow
531 cylinders = (u16)((u32)(cylinders)*16/15);
533 // then go through the large bitshift process
534 case TRANSLATION_LARGE:
535 if (translation == TRANSLATION_LARGE)
537 while (cylinders > 1024) {
541 // If we max out the head count
547 // clip to 1024 cylinders in lchs
548 if (cylinders > 1024)
550 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
552 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
553 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
554 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
558 /****************************************************************
559 * ATA detect and init
560 ****************************************************************/
562 // Extract common information from IDENTIFY commands.
564 extract_identify(int driveid, u16 *buffer)
566 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
569 char *model = ATA.devices[driveid].model;
570 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
572 for (i=0; i<maxsize/2; i++) {
573 u16 v = buffer[27+i];
575 model[i*2+1] = v & 0xff;
577 model[maxsize-1] = 0x00;
579 // Trim trailing spaces from model name.
580 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
583 // Extract ATA/ATAPI version.
584 u16 ataversion = buffer[80];
586 for (version=15; version>0; version--)
587 if (ataversion & (1<<version))
589 ATA.devices[driveid].version = version;
592 SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
596 init_drive_atapi(int driveid, u16 *buffer)
598 // Send an IDENTIFY_DEVICE_PACKET command to device
599 memset(buffer, 0, IDE_SECTOR_SIZE);
600 struct disk_op_s dop;
601 memset(&dop, 0, sizeof(dop));
602 dop.driveid = driveid;
605 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
606 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE_PACKET);
610 // Success - setup as ATAPI.
611 extract_identify(driveid, buffer);
612 SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATAPI);
613 SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
614 SET_GLOBAL(ATA.devices[driveid].sectors, (u64)-1);
615 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
617 // Report drive info to user.
618 u8 channel = driveid / 2;
619 u8 slave = driveid % 2;
620 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
621 , ATA.devices[driveid].model, ATA.devices[driveid].version
622 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
626 u8 cdcount = GET_GLOBAL(ATA.cdcount);
627 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
628 SET_GLOBAL(ATA.cdcount, cdcount+1);
635 init_drive_ata(int driveid, u16 *buffer)
637 // Send an IDENTIFY_DEVICE command to device
638 memset(buffer, 0, IDE_SECTOR_SIZE);
639 struct disk_op_s dop;
640 memset(&dop, 0, sizeof(dop));
641 dop.driveid = driveid;
644 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
645 int ret = ata_cmd_data(&dop, 0, ATA_CMD_IDENTIFY_DEVICE);
649 // Success - setup as ATA.
650 extract_identify(driveid, buffer);
651 SET_GLOBAL(ATA.devices[driveid].type, DTYPE_ATA);
652 SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
654 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
655 SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
656 SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
659 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
660 sectors = *(u64*)&buffer[100]; // word 100-103
662 sectors = *(u32*)&buffer[60]; // word 60 and word 61
663 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
665 // Setup disk geometry translation.
666 setup_translation(driveid);
668 // Report drive info to user.
669 u8 channel = driveid / 2;
670 u8 slave = driveid % 2;
671 char *model = ATA.devices[driveid].model;
672 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
673 , ATA.devices[driveid].version);
674 u64 sizeinmb = sectors >> 11;
675 if (sizeinmb < (1 << 16))
676 printf("(%u MiBytes)\n", (u32)sizeinmb);
678 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
680 // Register with bcv system.
681 add_bcv_hd(driveid, model);
687 powerup_await_non_bsy(u16 base, u64 end)
692 status = inb(base+ATA_CB_STAT);
693 if (!(status & ATA_CB_STAT_BSY))
696 if (orstatus == 0xff) {
697 dprintf(1, "powerup IDE floating\n");
700 if (rdtscll() > end) {
701 dprintf(1, "powerup IDE time out\n");
705 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
713 u64 end = calc_future_tsc(IDE_TIMEOUT);
714 int driveid, last_reset_driveid=-1;
715 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
716 u8 channel = driveid / 2;
717 u8 slave = driveid % 2;
719 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
724 int status = powerup_await_non_bsy(iobase1, end);
727 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
728 outb(newdh, iobase1+ATA_CB_DH);
730 status = powerup_await_non_bsy(iobase1, end);
734 // Check if ioport registers look valid.
735 outb(newdh, iobase1+ATA_CB_DH);
736 u8 dh = inb(iobase1+ATA_CB_DH);
737 outb(0x55, iobase1+ATA_CB_SC);
738 outb(0xaa, iobase1+ATA_CB_SN);
739 u8 sc = inb(iobase1+ATA_CB_SC);
740 u8 sn = inb(iobase1+ATA_CB_SN);
741 dprintf(6, "ata_detect drive=%d sc=%x sn=%x dh=%x\n"
742 , driveid, sc, sn, dh);
743 if (sc != 0x55 || sn != 0xaa || dh != newdh)
747 if (slave && driveid == last_reset_driveid + 1) {
748 // The drive was just reset - no need to reset it again.
751 last_reset_driveid = driveid;
756 int ret = init_drive_atapi(driveid, buffer);
758 // Found an ATAPI drive.
760 u8 st = inb(iobase1+ATA_CB_STAT);
762 // Status not set - can't be a valid drive.
766 ret = await_rdy(iobase1);
771 ret = init_drive_ata(driveid, buffer);
773 // No ATA drive found
777 u16 resetresult = buffer[93];
778 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
779 if (!slave && (resetresult & 0xdf61) == 0x4041)
780 // resetresult looks valid and device 0 is responding to
781 // device 1 requests - device 1 must not be present - skip
792 memset(&ATA, 0, sizeof(ATA));
794 // hdidmap and cdidmap init.
796 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
797 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
798 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
801 // Scan PCI bus for ATA adapters
804 foreachpci(bdf, max) {
805 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
807 if (count >= ARRAY_SIZE(ATA.channels))
810 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
811 SET_GLOBAL(ATA.channels[count].irq, irq);
812 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
814 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
818 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
819 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
824 SET_GLOBAL(ATA.channels[count].iobase1, port1);
825 SET_GLOBAL(ATA.channels[count].iobase2, port2);
826 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
827 , count, port1, port2, bdf, prog_if);
831 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
832 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
837 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
838 , count, port1, port2, bdf, prog_if);
839 SET_GLOBAL(ATA.channels[count].iobase1, port1);
840 SET_GLOBAL(ATA.channels[count].iobase2, port2);
851 dprintf(3, "init hard drives\n");
855 SET_BDA(disk_control_byte, 0xc0);
857 enable_hwirq(14, entry_76);
861 /****************************************************************
863 ****************************************************************/
865 // Fill in Fixed Disk Parameter Table (located in ebda).
867 fill_fdpt(int driveid)
872 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
873 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
874 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
876 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
877 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
878 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
880 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
881 fdpt->precompensation = 0xffff;
882 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
883 fdpt->landing_zone = npc;
884 fdpt->cylinders = nlc;
886 fdpt->sectors = nlspt;
888 if (nlc == npc && nlh == nph && nlspt == npspt)
889 // no logical CHS mapping used, just physical CHS
890 // use Standard Fixed Disk Parameter Table (FDPT)
893 // complies with Phoenix style Translated Fixed Disk Parameter
895 fdpt->phys_cylinders = npc;
896 fdpt->phys_heads = nph;
897 fdpt->phys_sectors = npspt;
898 fdpt->a0h_signature = 0xa0;
900 // Checksum structure.
901 fdpt->checksum -= checksum(fdpt, sizeof(*fdpt));
904 SET_IVT(0x41, get_ebda_seg()
905 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
907 SET_IVT(0x46, get_ebda_seg()
908 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
911 // Map a drive (that was registered via add_bcv_hd)
913 map_drive(int driveid)
916 u8 hdcount = GET_BDA(hdcount);
917 dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
918 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
919 SET_BDA(hdcount, hdcount + 1);
921 // Fill "fdpt" structure.