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 32000u //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.
71 static __always_inline int
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.
81 static __always_inline int
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 = 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 = 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 mode, int iobase1, int bytes)
226 if (mode == ATA_MODE_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);
257 u8 mode = GET_GLOBAL(ATA.devices[driveid].mode);
261 int bsize = blocksize;
262 if (skipfirst && current == 0) {
263 insx_discard(mode, iobase1, skipfirst);
266 if (skiplast && current == count-1)
270 // Write data to controller
271 dprintf(16, "Write sector id=%d dest=%p\n", driveid, buf_fl);
272 if (mode == ATA_MODE_PIO32)
273 outsl_fl(iobase1, buf_fl, bsize / 4);
275 outsw_fl(iobase1, buf_fl, bsize / 2);
277 // Read data from controller
278 dprintf(16, "Read sector id=%d dest=%p\n", driveid, buf_fl);
279 if (mode == ATA_MODE_PIO32)
280 insl_fl(iobase1, buf_fl, bsize / 4);
282 insw_fl(iobase1, buf_fl, bsize / 2);
286 if (skiplast && current == count-1)
287 insx_discard(mode, iobase1, skiplast);
289 status = pause_await_not_bsy(iobase1, iobase2);
295 SET_EBDA(sector_count, current);
296 if (current == count)
298 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
299 if (status != ATA_CB_STAT_DRQ) {
300 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
306 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
309 status &= ~ATA_CB_STAT_DF;
311 dprintf(6, "ata_transfer : no sectors left (status %02x)\n", status);
316 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
321 ata_transfer_disk(const struct disk_op_s *op)
323 return ata_transfer(op->driveid, op->command == ATA_CMD_WRITE_SECTORS
324 , op->count, IDE_SECTOR_SIZE, 0, 0, op->buf_fl);
328 ata_transfer_cdrom(const struct disk_op_s *op)
330 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
335 ata_transfer_cdemu(const struct disk_op_s *op, int before, int after)
337 int vcount = op->count * 4 - before - after;
338 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
339 , before*512, after*512, op->buf_fl);
341 SET_EBDA(sector_count, 0);
344 SET_EBDA(sector_count, vcount);
349 /****************************************************************
350 * ATA hard drive functions
351 ****************************************************************/
354 send_cmd_disk(const struct disk_op_s *op)
358 struct ata_pio_command cmd;
359 memset(&cmd, 0, sizeof(cmd));
361 cmd.command = op->command;
362 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
363 cmd.sector_count2 = op->count >> 8;
364 cmd.lba_low2 = lba >> 24;
365 cmd.lba_mid2 = lba >> 32;
366 cmd.lba_high2 = lba >> 40;
373 cmd.sector_count = op->count;
375 cmd.lba_mid = lba >> 8;
376 cmd.lba_high = lba >> 16;
377 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
379 return send_cmd(op->driveid, &cmd);
382 // Read/write count blocks from a harddrive.
384 ata_cmd_data(struct disk_op_s *op)
386 int ret = send_cmd_disk(op);
389 return ata_transfer_disk(op);
393 /****************************************************************
395 ****************************************************************/
397 // Low-level atapi command transmit function.
398 static __always_inline int
399 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
401 u8 channel = driveid / 2;
402 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
403 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
405 struct ata_pio_command cmd;
406 cmd.sector_count = 0;
409 cmd.lba_mid = blocksize;
410 cmd.lba_high = blocksize >> 8;
412 cmd.command = ATA_CMD_PACKET;
414 int ret = send_cmd(driveid, &cmd);
418 // Send command to device
419 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
421 int status = pause_await_not_bsy(iobase1, iobase2);
425 if (status & ATA_CB_STAT_ERR) {
426 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
427 , status, inb(iobase1 + ATA_CB_ERR));
430 if (!(status & ATA_CB_STAT_DRQ)) {
431 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
438 // Low-level cdrom read atapi command transmit function.
440 send_cmd_cdrom(const struct disk_op_s *op)
443 memset(atacmd, 0, sizeof(atacmd));
445 atacmd[0]=0x28; // READ command
446 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
447 atacmd[8]=(op->count & 0x00ff);
448 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
449 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
450 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
451 atacmd[5]=(op->lba & 0x000000ff);
453 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
454 , CDROM_SECTOR_SIZE);
457 // Read sectors from the cdrom.
459 cdrom_read(struct disk_op_s *op)
461 int ret = send_cmd_cdrom(op);
465 return ata_transfer_cdrom(op);
468 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
471 cdrom_read_512(struct disk_op_s *op)
474 u32 vcount = op->count;
475 u32 lba = op->lba = vlba / 4;
476 u32 velba = vlba + vcount - 1;
477 u32 elba = velba / 4;
478 op->count = elba - lba + 1;
479 int before = vlba % 4;
480 int after = 3 - (velba % 4);
482 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
483 " count=%d before=%d after=%d\n"
484 , op->driveid, vlba, vcount, op->buf_fl, lba, elba
485 , op->count, before, after);
487 int ret = send_cmd_cdrom(op);
491 return ata_transfer_cdemu(op, before, after);
494 // Send a simple atapi command to a drive.
496 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
497 , u32 length, void *buf_fl)
499 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
503 return ata_transfer(driveid, 0, 1, length, 0, 0, buf_fl);
507 /****************************************************************
508 * Disk geometry translation
509 ****************************************************************/
512 get_translation(int driveid)
514 if (! CONFIG_COREBOOT) {
515 // Emulators pass in the translation info via nvram.
516 u8 channel = driveid / 2;
517 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
518 translation >>= 2 * (driveid % 4);
523 // On COREBOOT, use a heuristic to determine translation type.
524 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
525 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
526 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
528 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
529 return ATA_TRANSLATION_NONE;
530 if (cylinders * heads <= 131072)
531 return ATA_TRANSLATION_LARGE;
532 return ATA_TRANSLATION_LBA;
536 setup_translation(int driveid)
538 u8 translation = get_translation(driveid);
539 SET_GLOBAL(ATA.devices[driveid].translation, translation);
541 u8 channel = driveid / 2;
542 u8 slave = driveid % 2;
543 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
544 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
545 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
546 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
548 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
549 , channel, slave, cylinders, heads, spt);
550 switch (translation) {
551 case ATA_TRANSLATION_NONE:
554 case ATA_TRANSLATION_LBA:
557 if (sectors > 63*255*1024) {
562 u32 sect = (u32)sectors / 63;
574 cylinders = sect / heads;
576 case ATA_TRANSLATION_RECHS:
577 dprintf(1, "r-echs");
578 // Take care not to overflow
583 cylinders = (u16)((u32)(cylinders)*16/15);
585 // then go through the large bitshift process
586 case ATA_TRANSLATION_LARGE:
587 if (translation == ATA_TRANSLATION_LARGE)
589 while (cylinders > 1024) {
593 // If we max out the head count
599 // clip to 1024 cylinders in lchs
600 if (cylinders > 1024)
602 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
604 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
605 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
606 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
610 /****************************************************************
611 * ATA detect and init
612 ****************************************************************/
615 extract_model(int driveid, u8 *buffer)
617 char *model = ATA.devices[driveid].model;
618 int maxsize = ARRAY_SIZE(ATA.devices[driveid].model);
622 for (i=0; i<maxsize; i+=2) {
623 model[i] = buffer[i+54+1];
624 model[i+1] = buffer[i+54];
626 model[maxsize-1] = 0x00;
628 // Trim trailing spaces
629 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
634 get_ata_version(u8 *buffer)
636 u16 ataversion = *(u16*)&buffer[160];
638 for (version=15; version>0; version--)
639 if (ataversion & (1<<version))
645 init_drive_atapi(int driveid)
647 // Send an IDENTIFY_DEVICE_PACKET command to device
649 memset(buffer, 0, sizeof(buffer));
650 struct disk_op_s dop;
651 dop.driveid = driveid;
652 dop.command = ATA_CMD_IDENTIFY_DEVICE_PACKET;
655 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
656 int ret = ata_cmd_data(&dop);
660 // Success - setup as ATAPI.
661 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATAPI);
663 u8 type = buffer[1] & 0x1f;
664 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
665 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
666 u16 blksize = CDROM_SECTOR_SIZE;
668 SET_GLOBAL(ATA.devices[driveid].device, type);
669 SET_GLOBAL(ATA.devices[driveid].removable, removable);
670 SET_GLOBAL(ATA.devices[driveid].mode, mode);
671 SET_GLOBAL(ATA.devices[driveid].blksize, blksize);
674 u8 cdcount = GET_GLOBAL(ATA.cdcount);
675 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
676 SET_GLOBAL(ATA.cdcount, cdcount+1);
678 // Report drive info to user.
679 u8 channel = driveid / 2;
680 u8 slave = driveid % 2;
681 u8 version = get_ata_version(buffer);
682 extract_model(driveid, buffer);
683 printf("ata%d-%d: %s ATAPI-%d %s\n", channel, slave
684 , ATA.devices[driveid].model, version
685 , type == ATA_DEVICE_CDROM ? "CD-Rom/DVD-Rom" : "Device");
691 init_drive_ata(int driveid)
693 // Send an IDENTIFY_DEVICE command to device
695 memset(buffer, 0, sizeof(buffer));
696 struct disk_op_s dop;
697 dop.driveid = driveid;
698 dop.command = ATA_CMD_IDENTIFY_DEVICE;
701 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
702 int ret = ata_cmd_data(&dop);
706 // Success - setup as ATA.
707 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATA);
709 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
710 u8 mode = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
711 u16 blksize = IDE_SECTOR_SIZE;
713 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
714 u16 heads = *(u16*)&buffer[3*2]; // word 3
715 u16 spt = *(u16*)&buffer[6*2]; // word 6
718 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
719 sectors = *(u64*)&buffer[100*2]; // word 100-103
721 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
723 SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
724 SET_GLOBAL(ATA.devices[driveid].removable, removable);
725 SET_GLOBAL(ATA.devices[driveid].mode, mode);
726 SET_GLOBAL(ATA.devices[driveid].blksize, blksize);
727 SET_GLOBAL(ATA.devices[driveid].pchs.heads, heads);
728 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, cylinders);
729 SET_GLOBAL(ATA.devices[driveid].pchs.spt, spt);
730 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
732 // Setup disk geometry translation.
733 setup_translation(driveid);
735 // Report drive info to user.
736 u8 channel = driveid / 2;
737 u8 slave = driveid % 2;
738 u8 version = get_ata_version(buffer);
739 extract_model(driveid, buffer);
740 char *model = ATA.devices[driveid].model;
741 printf("ata%d-%d: %s ATA-%d Hard-Disk ", channel, slave, model, version);
742 u64 sizeinmb = sectors >> 11;
743 if (sizeinmb < (1 << 16))
744 printf("(%u MiBytes)\n", (u32)sizeinmb);
746 printf("(%u GiBytes)\n", (u32)(sizeinmb >> 10));
748 // Register with bcv system.
749 add_bcv_hd(driveid, model);
758 int driveid, last_reset_driveid=-1;
759 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
760 u8 channel = driveid / 2;
761 u8 slave = driveid % 2;
763 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
768 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
769 outb(0x55, iobase1+ATA_CB_SC);
770 outb(0xaa, iobase1+ATA_CB_SN);
771 outb(0xaa, iobase1+ATA_CB_SC);
772 outb(0x55, iobase1+ATA_CB_SN);
773 outb(0x55, iobase1+ATA_CB_SC);
774 outb(0xaa, iobase1+ATA_CB_SN);
776 // Check if ioport registers look valid.
777 u8 sc = inb(iobase1+ATA_CB_SC);
778 u8 sn = inb(iobase1+ATA_CB_SN);
779 dprintf(6, "ata_detect drive=%d sc=%x sn=%x\n", driveid, sc, sn);
780 if (sc != 0x55 || sn != 0xaa)
784 if (slave && driveid == last_reset_driveid + 1) {
785 // The drive was just reset - no need to reset it again.
788 last_reset_driveid = driveid;
792 int ret = init_drive_atapi(driveid);
794 // Found an ATAPI drive.
797 u8 st = inb(iobase1+ATA_CB_STAT);
799 // Status not set - can't be a valid drive.
803 ret = await_rdy(iobase1);
808 init_drive_ata(driveid);
817 memset(&ATA, 0, sizeof(ATA));
819 // hdidmap and cdidmap init.
821 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
822 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
823 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
826 // Scan PCI bus for ATA adapters
829 foreachpci(bdf, max) {
830 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
832 if (count >= ARRAY_SIZE(ATA.channels))
835 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
836 SET_GLOBAL(ATA.channels[count].irq, irq);
837 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
839 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
843 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
844 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
849 SET_GLOBAL(ATA.channels[count].iobase1, port1);
850 SET_GLOBAL(ATA.channels[count].iobase2, port2);
851 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
852 , count, port1, port2, bdf, prog_if);
856 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
857 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
862 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
863 , count, port1, port2, bdf, prog_if);
864 SET_GLOBAL(ATA.channels[count].iobase1, port1);
865 SET_GLOBAL(ATA.channels[count].iobase2, port2);
876 dprintf(3, "init hard drives\n");
880 SET_BDA(disk_control_byte, 0xc0);
882 enable_hwirq(14, entry_76);
886 /****************************************************************
888 ****************************************************************/
890 // Fill in Fixed Disk Parameter Table (located in ebda).
892 fill_fdpt(int driveid)
897 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
898 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
899 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
901 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
902 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
903 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
905 struct fdpt_s *fdpt = &get_ebda_ptr()->fdpt[driveid];
906 fdpt->precompensation = 0xffff;
907 fdpt->drive_control_byte = 0xc0 | ((nph > 8) << 3);
908 fdpt->landing_zone = npc;
909 fdpt->cylinders = nlc;
911 fdpt->sectors = nlspt;
913 if (nlc == npc && nlh == nph && nlspt == npspt)
914 // no logical CHS mapping used, just physical CHS
915 // use Standard Fixed Disk Parameter Table (FDPT)
918 // complies with Phoenix style Translated Fixed Disk Parameter
920 fdpt->phys_cylinders = npc;
921 fdpt->phys_heads = nph;
922 fdpt->phys_sectors = npspt;
923 fdpt->a0h_signature = 0xa0;
925 // Checksum structure.
926 u8 sum = checksum((u8*)fdpt, sizeof(*fdpt)-1);
927 fdpt->checksum = -sum;
930 // Map a drive (that was registered via add_bcv_hd)
932 map_drive(int driveid)
935 u8 hdcount = GET_BDA(hdcount);
936 dprintf(1, "Mapping driveid %d to %d\n", driveid, hdcount);
937 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
938 SET_BDA(hdcount, hdcount + 1);
940 // Fill "fdpt" structure.