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
25 #define NOT_BSY_NOT_DRQ 4
28 #define IDE_SECTOR_SIZE 512
29 #define CDROM_SECTOR_SIZE 2048
31 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
33 struct ata_s ATA VAR16_32;
36 /****************************************************************
38 ****************************************************************/
40 // Wait for the specified ide state
42 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
44 u64 end = calc_future_tsc(timeout);
46 u8 status = inb(base+ATA_CB_STAT);
47 if ((status & mask) == flags)
49 if (rdtscll() > end) {
50 dprintf(1, "IDE time out\n");
56 // Wait for the device to be not-busy.
58 await_not_bsy(u16 base)
60 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
63 // Wait for the device to be ready.
67 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
70 // Wait for ide state - pauses for one ata cycle first.
72 pause_await_not_bsy(u16 iobase1, u16 iobase2)
74 // Wait one PIO transfer cycle.
75 inb(iobase2 + ATA_CB_ASTAT);
77 return await_not_bsy(iobase1);
80 // Wait for ide state - pause for 400ns first.
82 ndelay_await_not_bsy(u16 iobase1)
85 return await_not_bsy(iobase1);
90 ata_reset(int driveid)
92 u8 channel = driveid / 2;
93 u8 slave = driveid % 2;
94 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
95 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
97 dprintf(6, "ata_reset driveid=%d\n", driveid);
99 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
101 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
104 // wait for device to become not busy.
105 int status = await_not_bsy(iobase1);
110 u64 end = calc_future_tsc(IDE_TIMEOUT);
112 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
113 status = ndelay_await_not_bsy(iobase1);
116 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
118 // Change drive request failed to take effect - retry.
119 if (rdtscll() > end) {
120 dprintf(1, "ata_reset slave time out\n");
126 // On a user-reset request, wait for RDY if it is an ATA device.
127 u8 type=GET_GLOBAL(ATA.devices[driveid].type);
128 if (type == ATA_TYPE_ATA)
129 status = await_rdy(iobase1);
133 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
135 dprintf(6, "ata_reset exit status=%x\n", status);
139 /****************************************************************
141 ****************************************************************/
143 struct ata_pio_command {
158 // Send an ata command to the drive.
160 send_cmd(int driveid, struct ata_pio_command *cmd)
162 u8 channel = driveid / 2;
163 u8 slave = driveid % 2;
164 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
165 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
167 // Disable interrupts
168 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
171 int status = await_not_bsy(iobase1);
174 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
175 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
176 u8 olddh = inb(iobase1 + ATA_CB_DH);
177 outb(newdh, iobase1 + ATA_CB_DH);
178 if ((olddh ^ newdh) & (1<<4)) {
179 // Was a device change - wait for device to become not busy.
180 status = ndelay_await_not_bsy(iobase1);
185 if (cmd->command & 0x04) {
186 outb(0x00, iobase1 + ATA_CB_FR);
187 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
188 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
189 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
190 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
192 outb(cmd->feature, iobase1 + ATA_CB_FR);
193 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
194 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
195 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
196 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
197 outb(cmd->command, iobase1 + ATA_CB_CMD);
199 status = ndelay_await_not_bsy(iobase1);
203 if (status & ATA_CB_STAT_ERR) {
204 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
205 , status, inb(iobase1 + ATA_CB_ERR));
208 if (!(status & ATA_CB_STAT_DRQ)) {
209 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
217 /****************************************************************
219 ****************************************************************/
221 // Read and discard x number of bytes from an io channel.
223 insx_discard(int iobase1, int bytes)
226 if (CONFIG_ATA_PIO32) {
228 for (i=0; i<count; i++)
232 for (i=0; i<count; i++)
237 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
238 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
239 // and/or last block may be partially transferred. This function is
240 // inlined because all the callers use different forms and because the
241 // large number of parameters would consume a lot of stack space.
242 static __always_inline int
243 ata_transfer(int driveid, int iswrite, int count, int blocksize
244 , int skipfirst, int skiplast, void *buf_fl)
246 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
247 " skipf=%d skipl=%d buf=%p\n"
248 , driveid, iswrite, count, blocksize
249 , skipfirst, skiplast, buf_fl);
251 // Reset count of transferred data
252 SET_EBDA(sector_count, 0);
254 u8 channel = driveid / 2;
255 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
256 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
260 int bsize = blocksize;
261 if (skipfirst && current == 0) {
262 insx_discard(iobase1, skipfirst);
265 if (skiplast && current == count-1)
269 // Write data to controller
270 dprintf(16, "Write sector id=%d dest=%p\n", driveid, buf_fl);
271 if (CONFIG_ATA_PIO32)
272 outsl_fl(iobase1, buf_fl, bsize / 4);
274 outsw_fl(iobase1, buf_fl, bsize / 2);
276 // Read data from controller
277 dprintf(16, "Read sector id=%d dest=%p\n", driveid, buf_fl);
278 if (CONFIG_ATA_PIO32)
279 insl_fl(iobase1, buf_fl, bsize / 4);
281 insw_fl(iobase1, buf_fl, bsize / 2);
285 if (skiplast && current == count-1)
286 insx_discard(iobase1, skiplast);
288 status = pause_await_not_bsy(iobase1, iobase2);
294 SET_EBDA(sector_count, current);
295 if (current == count)
297 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
298 if (status != ATA_CB_STAT_DRQ) {
299 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
305 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
308 status &= ~ATA_CB_STAT_DF;
310 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
315 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
320 ata_transfer_disk(const struct disk_op_s *op)
322 return ata_transfer(op->driveid, op->command == ATA_CMD_WRITE_SECTORS
323 , op->count, IDE_SECTOR_SIZE, 0, 0, op->buf_fl);
327 ata_transfer_cdrom(const struct disk_op_s *op)
329 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
334 ata_transfer_cdemu(const struct disk_op_s *op, int before, int after)
336 int vcount = op->count * 4 - before - after;
337 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
338 , before*512, after*512, op->buf_fl);
340 SET_EBDA(sector_count, 0);
343 SET_EBDA(sector_count, vcount);
348 /****************************************************************
349 * ATA hard drive functions
350 ****************************************************************/
353 send_cmd_disk(const struct disk_op_s *op)
357 struct ata_pio_command cmd;
358 memset(&cmd, 0, sizeof(cmd));
360 cmd.command = op->command;
361 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
362 cmd.sector_count2 = op->count >> 8;
363 cmd.lba_low2 = lba >> 24;
364 cmd.lba_mid2 = lba >> 32;
365 cmd.lba_high2 = lba >> 40;
372 cmd.sector_count = op->count;
374 cmd.lba_mid = lba >> 8;
375 cmd.lba_high = lba >> 16;
376 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
378 return send_cmd(op->driveid, &cmd);
381 // Read/write count blocks from a harddrive.
383 ata_cmd_data(struct disk_op_s *op)
385 int ret = send_cmd_disk(op);
388 return ata_transfer_disk(op);
392 /****************************************************************
394 ****************************************************************/
396 // Low-level atapi command transmit function.
398 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
400 u8 channel = driveid / 2;
401 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
402 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
404 struct ata_pio_command cmd;
405 cmd.sector_count = 0;
408 cmd.lba_mid = blocksize;
409 cmd.lba_high = blocksize >> 8;
411 cmd.command = ATA_CMD_PACKET;
413 int ret = send_cmd(driveid, &cmd);
417 // Send command to device
418 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
420 int status = pause_await_not_bsy(iobase1, iobase2);
424 if (status & ATA_CB_STAT_ERR) {
425 u8 err = inb(iobase1 + ATA_CB_ERR);
428 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
432 if (!(status & ATA_CB_STAT_DRQ)) {
433 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
440 // Low-level cdrom read atapi command transmit function.
442 send_cmd_cdrom(const struct disk_op_s *op)
445 memset(atacmd, 0, sizeof(atacmd));
447 atacmd[0]=0x28; // READ command
448 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
449 atacmd[8]=(op->count & 0x00ff);
450 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
451 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
452 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
453 atacmd[5]=(op->lba & 0x000000ff);
455 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
456 , CDROM_SECTOR_SIZE);
459 // Read sectors from the cdrom.
461 cdrom_read(struct disk_op_s *op)
463 int ret = send_cmd_cdrom(op);
467 return ata_transfer_cdrom(op);
470 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
473 cdrom_read_512(struct disk_op_s *op)
476 u32 vcount = op->count;
477 u32 lba = op->lba = vlba / 4;
478 u32 velba = vlba + vcount - 1;
479 u32 elba = velba / 4;
480 op->count = elba - lba + 1;
481 int before = vlba % 4;
482 int after = 3 - (velba % 4);
484 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
485 " count=%d before=%d after=%d\n"
486 , op->driveid, vlba, vcount, op->buf_fl, lba, elba
487 , op->count, before, after);
489 int ret = send_cmd_cdrom(op);
493 return ata_transfer_cdemu(op, before, after);
496 // Send a simple atapi command to a drive.
498 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
499 , u32 length, void *buf_fl)
501 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
505 return ata_transfer(driveid, 0, 1, length, 0, 0, buf_fl);
509 /****************************************************************
510 * Disk geometry translation
511 ****************************************************************/
514 get_translation(int driveid)
516 if (! CONFIG_COREBOOT) {
517 // Emulators pass in the translation info via nvram.
518 u8 channel = driveid / 2;
519 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
520 translation >>= 2 * (driveid % 4);
525 // On COREBOOT, use a heuristic to determine translation type.
526 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
527 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
528 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
530 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
531 return ATA_TRANSLATION_NONE;
532 if (cylinders * heads <= 131072)
533 return ATA_TRANSLATION_LARGE;
534 return ATA_TRANSLATION_LBA;
538 setup_translation(int driveid)
540 u8 translation = get_translation(driveid);
541 SET_GLOBAL(ATA.devices[driveid].translation, translation);
543 u8 channel = driveid / 2;
544 u8 slave = driveid % 2;
545 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
546 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
547 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
548 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
550 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
551 , channel, slave, cylinders, heads, spt);
552 switch (translation) {
553 case ATA_TRANSLATION_NONE:
556 case ATA_TRANSLATION_LBA:
559 if (sectors > 63*255*1024) {
564 u32 sect = (u32)sectors / 63;
576 cylinders = sect / heads;
578 case ATA_TRANSLATION_RECHS:
579 dprintf(1, "r-echs");
580 // Take care not to overflow
585 cylinders = (u16)((u32)(cylinders)*16/15);
587 // then go through the large bitshift process
588 case ATA_TRANSLATION_LARGE:
589 if (translation == ATA_TRANSLATION_LARGE)
591 while (cylinders > 1024) {
595 // If we max out the head count
601 // clip to 1024 cylinders in lchs
602 if (cylinders > 1024)
604 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
606 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
607 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
608 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
612 /****************************************************************
613 * ATA detect and init
614 ****************************************************************/
616 // Extract common information from IDENTIFY commands.
618 extract_identify(int driveid, u16 *buffer)
620 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
623 char *model = ATA.devices[driveid].model;
624 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
626 for (i=0; i<maxsize/2; i++) {
627 u16 v = buffer[27+i];
629 model[i*2+1] = v & 0xff;
631 model[maxsize-1] = 0x00;
633 // Trim trailing spaces from model name.
634 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
637 // Extract ATA/ATAPI version.
638 u16 ataversion = buffer[80];
640 for (version=15; version>0; version--)
641 if (ataversion & (1<<version))
643 ATA.devices[driveid].version = version;
646 SET_GLOBAL(ATA.devices[driveid].removable, (buffer[0] & 0x80) ? 1 : 0);
650 init_drive_atapi(int driveid)
652 // Send an IDENTIFY_DEVICE_PACKET command to device
654 memset(buffer, 0, sizeof(buffer));
655 struct disk_op_s dop;
656 dop.driveid = driveid;
657 dop.command = ATA_CMD_IDENTIFY_DEVICE_PACKET;
660 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
661 int ret = ata_cmd_data(&dop);
665 // Success - setup as ATAPI.
666 extract_identify(driveid, buffer);
667 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATAPI);
668 SET_GLOBAL(ATA.devices[driveid].device, (buffer[0] >> 8) & 0x1f);
669 SET_GLOBAL(ATA.devices[driveid].blksize, CDROM_SECTOR_SIZE);
672 u8 cdcount = GET_GLOBAL(ATA.cdcount);
673 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
674 SET_GLOBAL(ATA.cdcount, cdcount+1);
676 // Report drive info to user.
677 u8 channel = driveid / 2;
678 u8 slave = driveid % 2;
679 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
680 , ATA.devices[driveid].model, ATA.devices[driveid].version
681 , (ATA.devices[driveid].device == ATA_DEVICE_CDROM
682 ? "CD-Rom/DVD-Rom" : "Device"));
688 init_drive_ata(int driveid)
690 // Send an IDENTIFY_DEVICE command to device
692 memset(buffer, 0, sizeof(buffer));
693 struct disk_op_s dop;
694 dop.driveid = driveid;
695 dop.command = ATA_CMD_IDENTIFY_DEVICE;
698 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
699 int ret = ata_cmd_data(&dop);
703 // Success - setup as ATA.
704 extract_identify(driveid, buffer);
705 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATA);
706 SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
707 SET_GLOBAL(ATA.devices[driveid].blksize, IDE_SECTOR_SIZE);
709 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, buffer[1]);
710 SET_GLOBAL(ATA.devices[driveid].pchs.heads, buffer[3]);
711 SET_GLOBAL(ATA.devices[driveid].pchs.spt, buffer[6]);
714 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
715 sectors = *(u64*)&buffer[100]; // word 100-103
717 sectors = *(u32*)&buffer[60]; // word 60 and word 61
718 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
720 // Setup disk geometry translation.
721 setup_translation(driveid);
723 // Report drive info to user.
724 u8 channel = driveid / 2;
725 u8 slave = driveid % 2;
726 char *model = ATA.devices[driveid].model;
727 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model
728 , ATA.devices[driveid].version);
729 u64 sizeinmb = sectors >> 11;
730 if (sizeinmb < (1 << 16))
731 printf("(%u MiBytes)\n", (u32)sizeinmb);
733 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
735 // Register with bcv system.
736 add_bcv_hd(driveid, model);
742 powerup_await_non_bsy(u16 base, u64 end)
747 status = inb(base+ATA_CB_STAT);
748 if (!(status & ATA_CB_STAT_BSY))
751 if (orstatus == 0xff) {
752 dprintf(1, "powerup IDE floating\n");
755 if (rdtscll() > end) {
756 dprintf(1, "powerup IDE time out\n");
760 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
768 u64 end = calc_future_tsc(IDE_TIMEOUT);
769 int driveid, last_reset_driveid=-1;
770 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
771 u8 channel = driveid / 2;
772 u8 slave = driveid % 2;
774 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
779 int status = powerup_await_non_bsy(iobase1, end);
782 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
783 outb(newdh, iobase1+ATA_CB_DH);
785 status = powerup_await_non_bsy(iobase1, end);
789 // Check if ioport registers look valid.
790 outb(newdh, iobase1+ATA_CB_DH);
791 u8 dh = inb(iobase1+ATA_CB_DH);
792 outb(0x55, iobase1+ATA_CB_SC);
793 outb(0xaa, iobase1+ATA_CB_SN);
794 u8 sc = inb(iobase1+ATA_CB_SC);
795 u8 sn = inb(iobase1+ATA_CB_SN);
796 dprintf(6, "ata_detect drive=%d sc=%x sn=%x dh=%x\n"
797 , driveid, sc, sn, dh);
798 if (sc != 0x55 || sn != 0xaa || dh != newdh)
802 if (slave && driveid == last_reset_driveid + 1) {
803 // The drive was just reset - no need to reset it again.
806 last_reset_driveid = driveid;
810 int ret = init_drive_atapi(driveid);
812 // Found an ATAPI drive.
815 u8 st = inb(iobase1+ATA_CB_STAT);
817 // Status not set - can't be a valid drive.
821 ret = await_rdy(iobase1);
826 init_drive_ata(driveid);
835 memset(&ATA, 0, sizeof(ATA));
837 // hdidmap and cdidmap init.
839 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
840 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
841 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
844 // Scan PCI bus for ATA adapters
847 foreachpci(bdf, max) {
848 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
850 if (count >= ARRAY_SIZE(ATA.channels))
853 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
854 SET_GLOBAL(ATA.channels[count].irq, irq);
855 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
857 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
861 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
862 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
867 SET_GLOBAL(ATA.channels[count].iobase1, port1);
868 SET_GLOBAL(ATA.channels[count].iobase2, port2);
869 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
870 , count, port1, port2, bdf, prog_if);
874 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
875 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
880 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
881 , count, port1, port2, bdf, prog_if);
882 SET_GLOBAL(ATA.channels[count].iobase1, port1);
883 SET_GLOBAL(ATA.channels[count].iobase2, port2);
894 dprintf(3, "init hard drives\n");
898 SET_BDA(disk_control_byte, 0xc0);
900 enable_hwirq(14, entry_76);
904 /****************************************************************
906 ****************************************************************/
908 // Fill in Fixed Disk Parameter Table (located in ebda).
910 fill_fdpt(int driveid)
915 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
916 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
917 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
919 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
920 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
921 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
923 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
924 fdpt->precompensation = 0xffff;
925 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
926 fdpt->landing_zone = npc;
927 fdpt->cylinders = nlc;
929 fdpt->sectors = nlspt;
931 if (nlc == npc && nlh == nph && nlspt == npspt)
932 // no logical CHS mapping used, just physical CHS
933 // use Standard Fixed Disk Parameter Table (FDPT)
936 // complies with Phoenix style Translated Fixed Disk Parameter
938 fdpt->phys_cylinders = npc;
939 fdpt->phys_heads = nph;
940 fdpt->phys_sectors = npspt;
941 fdpt->a0h_signature = 0xa0;
943 // Checksum structure.
944 u8 sum = checksum(fdpt, sizeof(*fdpt)-1);
945 fdpt->checksum = -sum;
948 // Map a drive (that was registered via add_bcv_hd)
950 map_drive(int driveid)
953 u8 hdcount = GET_BDA(hdcount);
954 dprintf(3, "Mapping driveid %d to %d\n", driveid, hdcount);
955 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
956 SET_BDA(hdcount, hdcount + 1);
958 // Fill "fdpt" structure.