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" // dprintf
12 #include "cmos.h" // inb_cmos
13 #include "pic.h" // enable_hwirq
14 #include "biosvar.h" // GET_EBDA
15 #include "pci.h" // pci_find_class
16 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
17 #include "pci_regs.h" // PCI_INTERRUPT_LINE
23 #define NOT_BSY_NOT_DRQ 4
26 #define IDE_SECTOR_SIZE 512
27 #define CDROM_SECTOR_SIZE 2048
29 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
32 /****************************************************************
34 ****************************************************************/
36 // Wait for the specified ide state
38 await_ide(u8 when_done, u16 base, u16 timeout)
40 u64 end = calc_future_tsc(timeout);
42 u8 status = inb(base+ATA_CB_STAT);
45 result = status & ATA_CB_STAT_BSY;
46 else if (when_done == NOT_BSY)
47 result = !(status & ATA_CB_STAT_BSY);
48 else if (when_done == NOT_BSY_DRQ)
49 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
50 else if (when_done == NOT_BSY_NOT_DRQ)
51 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
52 else if (when_done == NOT_BSY_RDY)
53 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
57 if (status & ATA_CB_STAT_ERR) {
58 dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
59 ",!BSY_!DRQ,!BSY_RDY) %d status=%x timeout=%d\n"
60 , when_done, status, timeout);
66 dprintf(1, "IDE time out\n");
70 // Wait for ide state - pauses for one ata cycle first.
71 static __always_inline int
72 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
74 // Wait one PIO transfer cycle.
75 inb(iobase2 + ATA_CB_ASTAT);
77 return await_ide(when_done, iobase1, timeout);
80 // Wait for ide state - pause for 400ns first.
81 static __always_inline int
82 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
85 return await_ide(when_done, iobase1, timeout);
90 ata_reset(int driveid)
92 u8 channel = driveid / 2;
93 u8 slave = driveid % 2;
94 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
95 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
99 // 8.2.1 (a) -- set SRST in DC
100 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
102 // 8.2.1 (b) -- wait for BSY
103 int status = await_ide(BSY, iobase1, 20);
104 dprintf(6, "ata_reset(1) status=%x\n", status);
106 // 8.2.1 (f) -- clear SRST
107 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
109 // 8.2.1 (g) -- check for sc==sn==0x01
111 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
113 u8 sc = inb(iobase1+ATA_CB_SC);
114 u8 sn = inb(iobase1+ATA_CB_SN);
116 // For predetermined ATA drives - wait for ready.
117 if (sc==0x01 && sn==0x01) {
118 u8 type=GET_EBDA(ata.devices[driveid].type);
119 if (type == ATA_TYPE_ATA)
120 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
123 // 8.2.1 (h) -- wait for not BSY
124 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
125 dprintf(6, "ata_reset(2) status=%x\n", status);
128 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
132 /****************************************************************
134 ****************************************************************/
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 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
164 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
166 int status = inb(iobase1 + ATA_CB_STAT);
167 if (status & ATA_CB_STAT_BSY)
170 // Disable interrupts
171 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
174 u8 device = inb(iobase1 + ATA_CB_DH);
175 outb(cmd->device, iobase1 + ATA_CB_DH);
176 if ((device ^ cmd->device) & (1 << 4))
177 // Wait for device to become active.
180 if (cmd->command & 0x04) {
181 outb(0x00, iobase1 + ATA_CB_FR);
182 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
183 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
184 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
185 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
187 outb(cmd->feature, iobase1 + ATA_CB_FR);
188 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
189 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
190 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
191 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
192 outb(cmd->command, iobase1 + ATA_CB_CMD);
194 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
198 if (status & ATA_CB_STAT_ERR) {
199 dprintf(6, "send_cmd : read error\n");
202 if (!(status & ATA_CB_STAT_DRQ)) {
203 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
204 , (unsigned) status);
212 /****************************************************************
214 ****************************************************************/
216 // Read and discard x number of bytes from an io channel.
218 insx_discard(int mode, int iobase1, int bytes)
221 if (mode == ATA_MODE_PIO32) {
223 for (i=0; i<count; i++)
227 for (i=0; i<count; i++)
232 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
233 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
234 // and/or last block may be partially transferred. This function is
235 // inlined because all the callers use different forms and because the
236 // large number of parameters would consume a lot of stack space.
237 static __always_inline int
238 ata_transfer(int driveid, int iswrite, int count, int blocksize
239 , int skipfirst, int skiplast, void *far_buffer)
241 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
242 " skipf=%d skipl=%d buf=%p\n"
243 , driveid, iswrite, count, blocksize
244 , skipfirst, skiplast, far_buffer);
246 // Reset count of transferred data
247 SET_EBDA(ata.trsfsectors, 0);
249 u8 channel = driveid / 2;
250 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
251 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
252 u8 mode = GET_EBDA(ata.devices[driveid].mode);
256 int bsize = blocksize;
257 if (skipfirst && current == 0) {
258 insx_discard(mode, iobase1, skipfirst);
261 if (skiplast && current == count-1)
265 // Write data to controller
266 dprintf(16, "Write sector id=%d dest=%p\n", driveid, far_buffer);
267 if (mode == ATA_MODE_PIO32)
268 outsl_far(iobase1, far_buffer, bsize / 4);
270 outsw_far(iobase1, far_buffer, bsize / 2);
272 // Read data from controller
273 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
274 if (mode == ATA_MODE_PIO32)
275 insl_far(iobase1, far_buffer, bsize / 4);
277 insw_far(iobase1, far_buffer, bsize / 2);
281 if (skiplast && current == count-1)
282 insx_discard(mode, iobase1, skiplast);
284 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
290 SET_EBDA(ata.trsfsectors, current);
291 if (current == count)
293 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
295 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
296 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
297 , (unsigned) status);
302 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
303 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
305 status &= ~ATA_CB_STAT_DF;
306 if (status != ATA_CB_STAT_RDY ) {
307 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
308 , (unsigned) status);
313 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
318 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
320 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
321 , 0, 0, op->far_buffer);
325 ata_transfer_cdrom(const struct ata_op_s *op)
327 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
328 , 0, 0, op->far_buffer);
332 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
334 int vcount = op->count * 4 - before - after;
335 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
336 , before*512, after*512, op->far_buffer);
338 SET_EBDA(ata.trsfsectors, 0);
341 SET_EBDA(ata.trsfsectors, vcount);
346 /****************************************************************
347 * ATA hard drive functions
348 ****************************************************************/
351 send_cmd_disk(const struct ata_op_s *op, u16 command)
353 u8 slave = op->driveid % 2;
356 struct ata_pio_command cmd;
357 memset(&cmd, 0, sizeof(cmd));
359 cmd.command = command;
360 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
361 cmd.sector_count2 = op->count >> 8;
362 cmd.lba_low2 = lba >> 24;
363 cmd.lba_mid2 = lba >> 32;
364 cmd.lba_high2 = lba >> 40;
371 cmd.sector_count = op->count;
373 cmd.lba_mid = lba >> 8;
374 cmd.lba_high = lba >> 16;
375 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
376 | ((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(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
386 op.driveid = driveid;
389 op.far_buffer = far_buffer;
391 int ret = send_cmd_disk(&op, command);
395 int iswrite = command == ATA_CMD_WRITE_SECTORS;
396 return ata_transfer_disk(&op, iswrite);
400 /****************************************************************
402 ****************************************************************/
404 // Low-level atapi command transmit function.
405 static __always_inline int
406 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
408 u8 channel = driveid / 2;
409 u8 slave = driveid % 2;
410 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
411 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
413 struct ata_pio_command cmd;
414 cmd.sector_count = 0;
417 cmd.lba_mid = blocksize;
418 cmd.lba_high = blocksize >> 8;
419 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
420 cmd.command = ATA_CMD_PACKET;
422 int ret = send_cmd(driveid, &cmd);
426 // Send command to device
427 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
429 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
436 // Low-level cdrom read atapi command transmit function.
438 send_cmd_cdrom(const struct ata_op_s *op)
441 memset(atacmd, 0, sizeof(atacmd));
443 atacmd[0]=0x28; // READ command
444 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
445 atacmd[8]=(op->count & 0x00ff);
446 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
447 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
448 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
449 atacmd[5]=(op->lba & 0x000000ff);
451 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
452 , CDROM_SECTOR_SIZE);
455 // Read sectors from the cdrom.
457 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
460 op.driveid = driveid;
463 op.far_buffer = far_buffer;
465 int ret = send_cmd_cdrom(&op);
469 return ata_transfer_cdrom(&op);
472 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
475 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
477 u32 velba = vlba + vcount - 1;
479 u32 elba = velba / 4;
480 int count = elba - lba + 1;
481 int before = vlba % 4;
482 int after = 3 - (velba % 4);
485 op.driveid = driveid;
488 op.far_buffer = far_buffer;
490 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
491 " count=%d before=%d after=%d\n"
492 , driveid, vlba, vcount, far_buffer, lba, elba
493 , count, before, after);
495 int ret = send_cmd_cdrom(&op);
499 return ata_transfer_emu(&op, before, after);
502 // Send a simple atapi command to a drive.
504 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
505 , u32 length, void *far_buffer)
507 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
511 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
515 /****************************************************************
516 * ATA detect and init
517 ****************************************************************/
520 report_model(int driveid, u8 *buffer)
526 for (i=0; i<40; i+=2) {
527 model[i] = buffer[i+54+1];
528 model[i+1] = buffer[i+54];
533 for (i=39; i>0; i--) {
534 if (model[i] != 0x20)
539 u8 channel = driveid / 2;
540 u8 slave = driveid % 2;
541 // XXX - model on stack not %cs
542 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
546 get_ata_version(u8 *buffer)
548 u16 ataversion = *(u16*)&buffer[160];
550 for (version=15; version>0; version--)
551 if (ataversion & (1<<version))
557 init_drive_atapi(int driveid)
559 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATAPI);
561 // Temporary values to do the transfer
562 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
563 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
565 // Now we send a IDENTIFY command to ATAPI device
567 memset(buffer, 0, sizeof(buffer));
568 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
570 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
572 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
574 u8 type = buffer[1] & 0x1f;
575 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
576 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
577 u16 blksize = CDROM_SECTOR_SIZE;
579 SET_EBDA(ata.devices[driveid].device, type);
580 SET_EBDA(ata.devices[driveid].removable, removable);
581 SET_EBDA(ata.devices[driveid].mode, mode);
582 SET_EBDA(ata.devices[driveid].blksize, blksize);
585 u8 cdcount = GET_EBDA(ata.cdcount);
586 SET_EBDA(ata.idmap[1][cdcount], driveid);
587 SET_EBDA(ata.cdcount, ++cdcount);
589 report_model(driveid, buffer);
590 u8 version = get_ata_version(buffer);
591 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
592 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
594 printf(" ATAPI-%d Device\n", version);
598 fill_fdpt(int driveid)
603 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
604 u16 nlc = ebda->ata.devices[driveid].lchs.cylinders;
605 u16 nlh = ebda->ata.devices[driveid].lchs.heads;
606 u16 nlspt = ebda->ata.devices[driveid].lchs.spt;
608 u16 npc = ebda->ata.devices[driveid].pchs.cylinders;
609 u16 nph = ebda->ata.devices[driveid].pchs.heads;
610 u16 npspt = ebda->ata.devices[driveid].pchs.spt;
612 ebda->fdpt[driveid].precompensation = 0xffff;
613 ebda->fdpt[driveid].drive_control_byte = 0xc0 | ((nph > 8) << 3);
614 ebda->fdpt[driveid].landing_zone = npc;
615 ebda->fdpt[driveid].cylinders = nlc;
616 ebda->fdpt[driveid].heads = nlh;
617 ebda->fdpt[driveid].sectors = nlspt;
619 if (nlc == npc && nlh == nph && nlspt == npspt)
620 // no logical CHS mapping used, just physical CHS
621 // use Standard Fixed Disk Parameter Table (FDPT)
624 // complies with Phoenix style Translated Fixed Disk Parameter
626 ebda->fdpt[driveid].phys_cylinders = npc;
627 ebda->fdpt[driveid].phys_heads = nph;
628 ebda->fdpt[driveid].phys_sectors = npspt;
629 ebda->fdpt[driveid].a0h_signature = 0xa0;
631 // Checksum structure.
632 u8 sum = checksum((u8*)&ebda->fdpt[driveid], sizeof(ebda->fdpt[driveid])-1);
633 ebda->fdpt[driveid].checksum = -sum;
637 get_translation(int driveid)
639 if (! CONFIG_COREBOOT) {
640 // Emulators pass in the translation info via nvram.
641 u8 channel = driveid / 2;
642 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
643 translation >>= 2 * (driveid % 4);
648 // On COREBOOT, use a heuristic to determine translation type.
649 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
650 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
651 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
653 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
654 return ATA_TRANSLATION_NONE;
655 if (cylinders * heads <= 131072)
656 return ATA_TRANSLATION_LARGE;
657 return ATA_TRANSLATION_LBA;
661 setup_translation(int driveid)
663 u8 translation = get_translation(driveid);
664 SET_EBDA(ata.devices[driveid].translation, translation);
666 u8 channel = driveid / 2;
667 u8 slave = driveid % 2;
668 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
669 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
670 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
671 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
673 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
674 , channel, slave, cylinders, heads, spt);
675 switch (translation) {
676 case ATA_TRANSLATION_NONE:
679 case ATA_TRANSLATION_LBA:
682 if (sectors > 63*255*1024) {
687 u32 sect = (u32)sectors / 63;
699 cylinders = sect / heads;
701 case ATA_TRANSLATION_RECHS:
702 dprintf(1, "r-echs");
703 // Take care not to overflow
708 cylinders = (u16)((u32)(cylinders)*16/15);
710 // then go through the large bitshift process
711 case ATA_TRANSLATION_LARGE:
712 if (translation == ATA_TRANSLATION_LARGE)
714 while (cylinders > 1024) {
718 // If we max out the head count
724 // clip to 1024 cylinders in lchs
725 if (cylinders > 1024)
727 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
729 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
730 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
731 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
735 init_drive_ata(int driveid)
737 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
739 // Temporary values to do the transfer
740 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
741 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
743 // Now we send a IDENTIFY command to ATA device
745 memset(buffer, 0, sizeof(buffer));
746 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
748 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
750 BX_PANIC("ata-detect: Failed to detect ATA device\n");
752 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
753 u8 mode = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
754 u16 blksize = IDE_SECTOR_SIZE;
756 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
757 u16 heads = *(u16*)&buffer[3*2]; // word 3
758 u16 spt = *(u16*)&buffer[6*2]; // word 6
761 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
762 sectors = *(u64*)&buffer[100*2]; // word 100-103
764 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
766 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
767 SET_EBDA(ata.devices[driveid].removable, removable);
768 SET_EBDA(ata.devices[driveid].mode, mode);
769 SET_EBDA(ata.devices[driveid].blksize, blksize);
770 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
771 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
772 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
773 SET_EBDA(ata.devices[driveid].sectors, sectors);
775 // Setup disk geometry translation.
776 setup_translation(driveid);
779 u8 hdcount = GET_EBDA(ata.hdcount);
780 SET_EBDA(ata.idmap[0][hdcount], driveid);
781 SET_EBDA(ata.hdcount, ++hdcount);
783 // Fill "fdpt" structure.
786 // Report drive info to user.
787 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
788 report_model(driveid, buffer);
789 u8 version = get_ata_version(buffer);
790 if (sizeinmb < (1 << 16))
791 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
793 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
794 , (u32)(sizeinmb >> 10));
798 init_drive_unknown(int driveid)
800 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_UNKNOWN);
802 u8 channel = driveid / 2;
803 u8 slave = driveid % 2;
804 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
812 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
813 u8 channel = driveid / 2;
814 u8 slave = driveid % 2;
816 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
817 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
821 // Disable interrupts
822 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
825 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
827 outb(0x55, iobase1+ATA_CB_SC);
828 outb(0xaa, iobase1+ATA_CB_SN);
829 outb(0xaa, iobase1+ATA_CB_SC);
830 outb(0x55, iobase1+ATA_CB_SN);
831 outb(0x55, iobase1+ATA_CB_SC);
832 outb(0xaa, iobase1+ATA_CB_SN);
834 // If we found something
835 u8 sc = inb(iobase1+ATA_CB_SC);
836 u8 sn = inb(iobase1+ATA_CB_SN);
837 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
839 if (sc != 0x55 || sn != 0xaa)
845 // check for ATA or ATAPI
846 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
848 sc = inb(iobase1+ATA_CB_SC);
849 sn = inb(iobase1+ATA_CB_SN);
850 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
851 if (sc!=0x01 || sn!=0x01) {
852 init_drive_unknown(driveid);
855 u8 cl = inb(iobase1+ATA_CB_CL);
856 u8 ch = inb(iobase1+ATA_CB_CH);
857 u8 st = inb(iobase1+ATA_CB_STAT);
858 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
859 , driveid, sc, sn, cl, ch, st);
861 if (cl==0x14 && ch==0xeb)
862 init_drive_atapi(driveid);
863 else if (cl==0x00 && ch==0x00 && st!=0x00)
864 init_drive_ata(driveid);
865 else if (cl==0xff && ch==0xff)
869 init_drive_unknown(driveid);
878 // hdidmap and cdidmap init.
880 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
881 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
882 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
885 // Scan PCI bus for ATA adapters
888 foreachpci(bdf, max) {
889 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
892 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
893 SET_EBDA(ata.channels[count].irq, irq);
894 SET_EBDA(ata.channels[count].pci_bdf, bdf);
896 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
900 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
901 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
906 SET_EBDA(ata.channels[count].iobase1, port1);
907 SET_EBDA(ata.channels[count].iobase2, port2);
908 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
909 , count, port1, port2, bdf, prog_if);
913 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
914 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
919 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
920 , count, port1, port2, bdf, prog_if);
921 SET_EBDA(ata.channels[count].iobase1, port1);
922 SET_EBDA(ata.channels[count].iobase2, port2);
933 dprintf(3, "init hard drives\n");
937 // Store the device count
938 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
940 SET_BDA(disk_control_byte, 0xc0);
942 enable_hwirq(14, entry_76);