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
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 dprintf(1, "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?
95 // Wait for ide state - pause for 400ns first.
96 static __always_inline int
97 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
100 return await_ide(when_done, iobase1, timeout);
103 // Delay for x milliseconds
107 usleep(delay * 1000);
112 ata_reset(int driveid)
114 u8 channel = driveid / 2;
115 u8 slave = driveid % 2;
116 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
117 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
121 // 8.2.1 (a) -- set SRST in DC
122 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
124 // 8.2.1 (b) -- wait for BSY
125 int status = await_ide(BSY, iobase1, 20);
126 dprintf(6, "ata_reset(1) status=%x\n", status);
128 // 8.2.1 (f) -- clear SRST
129 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
131 // 8.2.1 (g) -- check for sc==sn==0x01
133 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
136 // 8.2.1 (h) -- wait for not BSY
137 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
138 dprintf(6, "ata_reset(2) status=%x\n", status);
141 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
145 /****************************************************************
147 ****************************************************************/
156 struct ata_pio_command {
171 // Send an ata command to the drive.
173 send_cmd(int driveid, struct ata_pio_command *cmd)
175 u8 channel = driveid / 2;
176 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
177 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
179 int status = inb(iobase1 + ATA_CB_STAT);
180 if (status & ATA_CB_STAT_BSY)
183 // Disable interrupts
184 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
187 u8 device = inb(iobase1 + ATA_CB_DH);
188 outb(cmd->device, iobase1 + ATA_CB_DH);
189 if ((device ^ cmd->device) & (1UL << 4))
190 // Wait for device to become active.
193 if (cmd->command & 0x04) {
194 outb(0x00, iobase1 + ATA_CB_FR);
195 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
196 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
197 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
198 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
200 outb(cmd->feature, iobase1 + ATA_CB_FR);
201 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
202 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
203 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
204 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
205 outb(cmd->command, iobase1 + ATA_CB_CMD);
207 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
211 if (status & ATA_CB_STAT_ERR) {
212 DEBUGF("send_cmd : read error\n");
215 if (!(status & ATA_CB_STAT_DRQ)) {
216 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
217 , (unsigned) status);
225 /****************************************************************
227 ****************************************************************/
229 // Read and discard x number of bytes from an io channel.
231 insx_discard(int mode, int iobase1, int bytes)
234 if (mode == ATA_MODE_PIO32) {
236 for (i=0; i<count; i++)
240 for (i=0; i<count; i++)
245 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
246 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
247 // and/or last block may be partially transferred. This function is
248 // inlined because all the callers use different forms and because the
249 // large number of parameters would consume a lot of stack space.
250 static __always_inline int
251 ata_transfer(int driveid, int iswrite, int count, int blocksize
252 , int skipfirst, int skiplast, void *far_buffer)
254 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
255 " skipf=%d skipl=%d buf=%p\n"
256 , driveid, iswrite, count, blocksize
257 , skipfirst, skiplast, far_buffer);
259 // Reset count of transferred data
260 SET_EBDA(ata.trsfsectors, 0);
262 u8 channel = driveid / 2;
263 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
264 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
265 u8 mode = GET_EBDA(ata.devices[driveid].mode);
269 int bsize = blocksize;
270 if (skipfirst && current == 0) {
271 insx_discard(mode, iobase1, skipfirst);
274 if (skiplast && current == count-1)
278 // Write data to controller
279 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
280 if (mode == ATA_MODE_PIO32)
281 outsl_far(iobase1, far_buffer, bsize / 4);
283 outsw_far(iobase1, far_buffer, bsize / 2);
285 // Read data from controller
286 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
287 if (mode == ATA_MODE_PIO32)
288 insl_far(iobase1, far_buffer, bsize / 4);
290 insw_far(iobase1, far_buffer, bsize / 2);
294 if (skiplast && current == count-1)
295 insx_discard(mode, iobase1, skiplast);
297 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
303 SET_EBDA(ata.trsfsectors, current);
304 if (current == count)
306 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
308 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
309 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
310 , (unsigned) status);
315 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
316 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
318 status &= ~ATA_CB_STAT_DF;
319 if (status != ATA_CB_STAT_RDY ) {
320 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
321 , (unsigned) status);
326 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
331 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
333 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
334 , 0, 0, op->far_buffer);
338 ata_transfer_cdrom(const struct ata_op_s *op)
340 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
341 , 0, 0, op->far_buffer);
345 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
347 int vcount = op->count * 4 - before - after;
348 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
349 , before*512, after*512, op->far_buffer);
351 SET_EBDA(ata.trsfsectors, 0);
354 SET_EBDA(ata.trsfsectors, vcount);
359 /****************************************************************
360 * ATA hard drive functions
361 ****************************************************************/
364 send_cmd_disk(const struct ata_op_s *op, u16 command)
366 u8 slave = op->driveid % 2;
369 struct ata_pio_command cmd;
370 memset(&cmd, 0, sizeof(cmd));
372 cmd.command = command;
373 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
374 cmd.sector_count2 = op->count >> 8;
375 cmd.lba_low2 = lba >> 24;
376 cmd.lba_mid2 = lba >> 32;
377 cmd.lba_high2 = lba >> 40;
384 cmd.sector_count = op->count;
386 cmd.lba_mid = lba >> 8;
387 cmd.lba_high = lba >> 16;
388 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
389 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
391 return send_cmd(op->driveid, &cmd);
394 // Read/write count blocks from a harddrive.
396 ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
399 op.driveid = driveid;
402 op.far_buffer = far_buffer;
404 int ret = send_cmd_disk(&op, command);
408 int iswrite = command == ATA_CMD_WRITE_SECTORS;
409 return ata_transfer_disk(&op, iswrite);
413 /****************************************************************
415 ****************************************************************/
417 // Low-level atapi command transmit function.
418 static __always_inline int
419 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
421 u8 channel = driveid / 2;
422 u8 slave = driveid % 2;
423 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
424 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
426 struct ata_pio_command cmd;
427 cmd.sector_count = 0;
430 cmd.lba_mid = blocksize;
431 cmd.lba_high = blocksize >> 8;
432 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
433 cmd.command = ATA_CMD_PACKET;
435 int ret = send_cmd(driveid, &cmd);
439 // Send command to device
440 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
442 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
449 // Low-level cdrom read atapi command transmit function.
451 send_cmd_cdrom(const struct ata_op_s *op)
454 memset(atacmd, 0, sizeof(atacmd));
456 atacmd[0]=0x28; // READ command
457 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
458 atacmd[8]=(op->count & 0x00ff);
459 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
460 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
461 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
462 atacmd[5]=(op->lba & 0x000000ff);
464 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
465 , CDROM_SECTOR_SIZE);
468 // Read sectors from the cdrom.
470 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
473 op.driveid = driveid;
476 op.far_buffer = far_buffer;
478 int ret = send_cmd_cdrom(&op);
482 return ata_transfer_cdrom(&op);
485 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
488 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
490 u32 velba = vlba + vcount - 1;
492 u32 elba = velba / 4;
493 int count = elba - lba + 1;
494 int before = vlba % 4;
495 int after = 3 - (velba % 4);
498 op.driveid = driveid;
501 op.far_buffer = far_buffer;
503 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
504 " count=%d before=%d after=%d\n"
505 , driveid, vlba, vcount, far_buffer, lba, elba
506 , count, before, after);
508 int ret = send_cmd_cdrom(&op);
512 return ata_transfer_emu(&op, before, after);
515 // Send a simple atapi command to a drive.
517 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
518 , u32 length, void *far_buffer)
520 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
524 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
528 /****************************************************************
529 * ATA detect and init
530 ****************************************************************/
533 report_model(int driveid, u8 *buffer)
539 for (i=0; i<40; i+=2) {
540 model[i] = buffer[i+54+1];
541 model[i+1] = buffer[i+54];
546 for (i=39; i>0; i--) {
547 if (model[i] != 0x20)
552 u8 channel = driveid / 2;
553 u8 slave = driveid % 2;
554 // XXX - model on stack not %cs
555 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
559 get_ata_version(u8 *buffer)
561 u16 ataversion = *(u16*)&buffer[160];
563 for (version=15; version>0; version--)
564 if (ataversion & (1<<version))
570 init_drive_atapi(int driveid)
572 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
574 // Temporary values to do the transfer
575 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
576 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
578 // Now we send a IDENTIFY command to ATAPI device
580 memset(buffer, 0, sizeof(buffer));
581 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
583 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
585 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
587 u8 type = buffer[1] & 0x1f;
588 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
589 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
590 u16 blksize = CDROM_SECTOR_SIZE;
592 SET_EBDA(ata.devices[driveid].device, type);
593 SET_EBDA(ata.devices[driveid].removable, removable);
594 SET_EBDA(ata.devices[driveid].mode, mode);
595 SET_EBDA(ata.devices[driveid].blksize, blksize);
598 u8 cdcount = GET_EBDA(ata.cdcount);
599 SET_EBDA(ata.idmap[1][cdcount], driveid);
600 SET_EBDA(ata.cdcount, ++cdcount);
602 report_model(driveid, buffer);
603 u8 version = get_ata_version(buffer);
604 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
605 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
607 printf(" ATAPI-%d Device\n", version);
611 fill_fdpt(int driveid)
616 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
617 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
618 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
620 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
621 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
622 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
624 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
625 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
626 SET_EBDA(fdpt[driveid].landing_zone, npc);
627 SET_EBDA(fdpt[driveid].cylinders, nlc);
628 SET_EBDA(fdpt[driveid].heads, nlh);
629 SET_EBDA(fdpt[driveid].sectors, nlspt);
631 if (nlc == npc && nlh == nph && nlspt == npspt)
632 // no logical CHS mapping used, just physical CHS
633 // use Standard Fixed Disk Parameter Table (FDPT)
636 // complies with Phoenix style Translated Fixed Disk Parameter
638 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
639 SET_EBDA(fdpt[driveid].phys_heads, nph);
640 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
641 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
643 // Checksum structure.
644 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
646 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
647 , fdpt[driveid]) - 1);
648 SET_EBDA(fdpt[driveid].checksum, -sum);
652 get_translation(int driveid)
654 if (! CONFIG_COREBOOT) {
655 // Emulators pass in the translation info via nvram.
656 u8 channel = driveid / 2;
657 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
658 translation >>= 2 * (driveid % 4);
663 // On COREBOOT, use a heuristic to determine translation type.
664 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
665 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
666 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
668 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
669 return ATA_TRANSLATION_NONE;
670 if (cylinders * heads <= 131072)
671 return ATA_TRANSLATION_LARGE;
672 return ATA_TRANSLATION_LBA;
676 setup_translation(int driveid)
678 u8 translation = get_translation(driveid);
679 SET_EBDA(ata.devices[driveid].translation, translation);
681 u8 channel = driveid / 2;
682 u8 slave = driveid % 2;
683 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
684 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
685 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
686 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
688 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
689 , channel, slave, cylinders, heads, spt);
690 switch (translation) {
691 case ATA_TRANSLATION_NONE:
694 case ATA_TRANSLATION_LBA:
697 if (sectors > 63*255*1024) {
702 u32 sect = (u32)sectors / 63;
714 cylinders = sect / heads;
716 case ATA_TRANSLATION_RECHS:
717 dprintf(1, "r-echs");
718 // Take care not to overflow
723 cylinders = (u16)((u32)(cylinders)*16/15);
725 // then go through the large bitshift process
726 case ATA_TRANSLATION_LARGE:
727 if (translation == ATA_TRANSLATION_LARGE)
729 while (cylinders > 1024) {
733 // If we max out the head count
739 // clip to 1024 cylinders in lchs
740 if (cylinders > 1024)
742 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
744 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
745 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
746 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
750 init_drive_ata(int driveid)
752 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
754 // Temporary values to do the transfer
755 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
756 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
758 // Now we send a IDENTIFY command to ATA device
760 memset(buffer, 0, sizeof(buffer));
761 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
763 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
765 BX_PANIC("ata-detect: Failed to detect ATA device\n");
767 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
768 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
769 u16 blksize = *(u16*)&buffer[10];
771 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
772 u16 heads = *(u16*)&buffer[3*2]; // word 3
773 u16 spt = *(u16*)&buffer[6*2]; // word 6
776 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
777 sectors = *(u64*)&buffer[100*2]; // word 100-103
779 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
781 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
782 SET_EBDA(ata.devices[driveid].removable, removable);
783 SET_EBDA(ata.devices[driveid].mode, mode);
784 SET_EBDA(ata.devices[driveid].blksize, blksize);
785 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
786 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
787 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
788 SET_EBDA(ata.devices[driveid].sectors, sectors);
790 // Setup disk geometry translation.
791 setup_translation(driveid);
794 u8 hdcount = GET_EBDA(ata.hdcount);
795 SET_EBDA(ata.idmap[0][hdcount], driveid);
796 SET_EBDA(ata.hdcount, ++hdcount);
798 // Fill "fdpt" structure.
801 // Report drive info to user.
802 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
803 report_model(driveid, buffer);
804 u8 version = get_ata_version(buffer);
805 if (sizeinmb < (1 << 16))
806 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb);
808 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version
809 , (u32)(sizeinmb >> 10));
813 init_drive_unknown(int driveid)
815 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
817 u8 channel = driveid / 2;
818 u8 slave = driveid % 2;
819 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
827 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
828 u8 channel = driveid / 2;
829 u8 slave = driveid % 2;
831 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
832 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
834 // Disable interrupts
835 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
838 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
840 outb(0x55, iobase1+ATA_CB_SC);
841 outb(0xaa, iobase1+ATA_CB_SN);
842 outb(0xaa, iobase1+ATA_CB_SC);
843 outb(0x55, iobase1+ATA_CB_SN);
844 outb(0x55, iobase1+ATA_CB_SC);
845 outb(0xaa, iobase1+ATA_CB_SN);
847 // If we found something
848 u8 sc = inb(iobase1+ATA_CB_SC);
849 u8 sn = inb(iobase1+ATA_CB_SN);
850 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
852 if (sc != 0x55 || sn != 0xaa)
858 // check for ATA or ATAPI
859 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
861 sc = inb(iobase1+ATA_CB_SC);
862 sn = inb(iobase1+ATA_CB_SN);
863 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
864 if (sc!=0x01 || sn!=0x01) {
865 init_drive_unknown(driveid);
868 u8 cl = inb(iobase1+ATA_CB_CL);
869 u8 ch = inb(iobase1+ATA_CB_CH);
870 u8 st = inb(iobase1+ATA_CB_STAT);
871 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
872 , driveid, sc, sn, cl, ch, st);
874 if (cl==0x14 && ch==0xeb)
875 init_drive_atapi(driveid);
876 else if (cl==0x00 && ch==0x00 && st!=0x00)
877 init_drive_ata(driveid);
878 else if (cl==0xff && ch==0xff)
882 init_drive_unknown(driveid);
891 // hdidmap and cdidmap init.
893 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
894 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
895 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
898 #if CONFIG_MAX_ATA_INTERFACES > 0
899 SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
900 SET_EBDA(ata.channels[0].iobase1, 0x1f0);
901 SET_EBDA(ata.channels[0].iobase2, 0x3f0);
902 SET_EBDA(ata.channels[0].irq, 14);
904 #if CONFIG_MAX_ATA_INTERFACES > 1
905 SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
906 SET_EBDA(ata.channels[1].iobase1, 0x170);
907 SET_EBDA(ata.channels[1].iobase2, 0x370);
908 SET_EBDA(ata.channels[1].irq, 15);
910 #if CONFIG_MAX_ATA_INTERFACES > 2
911 SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
912 SET_EBDA(ata.channels[2].iobase1, 0x1e8);
913 SET_EBDA(ata.channels[2].iobase2, 0x3e0);
914 SET_EBDA(ata.channels[2].irq, 12);
916 #if CONFIG_MAX_ATA_INTERFACES > 3
917 SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
918 SET_EBDA(ata.channels[3].iobase1, 0x168);
919 SET_EBDA(ata.channels[3].iobase2, 0x360);
920 SET_EBDA(ata.channels[3].irq, 11);
922 #if CONFIG_MAX_ATA_INTERFACES > 4
923 #error Please fill the ATA interface informations
936 // Store the device count
937 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
939 SET_BDA(disk_control_byte, 0xc0);