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" // BX_INFO
12 #include "cmos.h" // inb_cmos
18 #define NOT_BSY_NOT_DRQ 4
21 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
23 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
24 #define DEBUGF(fmt, args...)
26 // XXX - lots of redundancy in this file.
29 await_ide(u8 when_done, u16 base, u16 timeout)
32 // for the times you're supposed to throw one away
33 u16 status = inb(base + ATA_CB_STAT);
35 status = inb(base+ATA_CB_STAT);
39 result = status & ATA_CB_STAT_BSY;
40 else if (when_done == NOT_BSY)
41 result = !(status & ATA_CB_STAT_BSY);
42 else if (when_done == NOT_BSY_DRQ)
43 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
44 else if (when_done == NOT_BSY_NOT_DRQ)
45 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
46 else if (when_done == NOT_BSY_RDY)
47 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
48 else if (when_done == TIMEOUT)
53 // mod 2048 each 16 ms
54 if (time>>16 != last) {
56 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
57 " %d time= %d timeout= %d\n"
58 , when_done, time>>11, timeout);
60 if (status & ATA_CB_STAT_ERR) {
61 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
62 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
63 , when_done, time>>11, timeout);
66 if ((timeout == 0) || ((time>>11) > timeout))
69 BX_INFO("IDE time out\n");
74 // ---------------------------------------------------------------------------
75 // ATA/ATAPI driver : software reset
76 // ---------------------------------------------------------------------------
78 // 8.2.1 Software reset - Device 0
84 u8 channel, slave, sn, sc;
90 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
91 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
95 // 8.2.1 (a) -- set SRST in DC
96 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
98 // 8.2.1 (b) -- wait for BSY
99 await_ide(BSY, iobase1, 20);
101 // 8.2.1 (f) -- clear SRST
102 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
104 type=GET_EBDA(ata.devices[device].type);
105 if (type != ATA_TYPE_NONE) {
107 // 8.2.1 (g) -- check for sc==sn==0x01
109 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
110 sc = inb(iobase1+ATA_CB_SC);
111 sn = inb(iobase1+ATA_CB_SN);
113 if ( (sc==0x01) && (sn==0x01) ) {
114 if (type == ATA_TYPE_ATA) //ATA
115 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
117 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
120 // 8.2.1 (h) -- wait for not BSY
121 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
125 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
129 insw(u16 port, u16 segment, u16 offset, u16 count)
132 for (i=0; i<count; i++) {
134 SET_FARVAR(segment, *(u16*)(offset + 2*i), d);
139 insl(u16 port, u16 segment, u16 offset, u16 count)
142 for (i=0; i<count; i++) {
144 SET_FARVAR(segment, *(u32*)(offset + 4*i), d);
149 outsw(u16 port, u16 segment, u16 offset, u16 count)
152 for (i=0; i<count; i++) {
153 u16 d = GET_FARVAR(segment, *(u16*)(offset + 2*i));
159 outsl(u16 port, u16 segment, u16 offset, u16 count)
162 for (i=0; i<count; i++) {
163 u32 d = GET_FARVAR(segment, *(u32*)(offset + 4*i));
169 // ---------------------------------------------------------------------------
170 // ATA/ATAPI driver : execute a data-in command
171 // ---------------------------------------------------------------------------
176 // 3 : expected DRQ=1
177 // 4 : no sectors left to read/verify
178 // 5 : more sectors to read/verify
179 // 6 : no sectors left to write
180 // 7 : more sectors to write
182 ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
183 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
185 u16 iobase1, iobase2, blksize;
187 u8 status, current, mode;
189 channel = device / 2;
192 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
193 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
194 mode = GET_EBDA(ata.devices[device].mode);
196 if (mode == ATA_MODE_PIO32)
201 // Reset count of transferred data
202 SET_EBDA(ata.trsfsectors,0);
203 SET_EBDA(ata.trsfbytes,0L);
206 status = inb(iobase1 + ATA_CB_STAT);
207 if (status & ATA_CB_STAT_BSY)
210 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
212 // sector will be 0 only on lba access. Convert to lba-chs
214 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
215 outb(0x00, iobase1 + ATA_CB_FR);
216 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
217 outb(lba >> 24, iobase1 + ATA_CB_SN);
218 outb(0, iobase1 + ATA_CB_CL);
219 outb(0, iobase1 + ATA_CB_CH);
221 count &= (1UL << 8) - 1;
222 lba &= (1UL << 24) - 1;
224 sector = (u16) (lba & 0x000000ffL);
225 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
226 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
229 outb(0x00, iobase1 + ATA_CB_FR);
230 outb(count, iobase1 + ATA_CB_SC);
231 outb(sector, iobase1 + ATA_CB_SN);
232 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
233 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
234 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
235 , iobase1 + ATA_CB_DH);
236 outb(command, iobase1 + ATA_CB_CMD);
238 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
239 status = inb(iobase1 + ATA_CB_STAT);
241 if (status & ATA_CB_STAT_ERR) {
242 DEBUGF("ata_cmd_data_in : read error\n");
244 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
245 DEBUGF("ata_cmd_data_in : DRQ not set (status %02x)\n"
246 , (unsigned) status);
250 // FIXME : move seg/off translation here
256 if (offset > 0xf800) {
261 if (mode == ATA_MODE_PIO32)
262 insl(iobase1, segment, offset, blksize);
264 insw(iobase1, segment, offset, blksize);
267 SET_EBDA(ata.trsfsectors,current);
269 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
270 status = inb(iobase1 + ATA_CB_STAT);
272 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
274 != ATA_CB_STAT_RDY ) {
275 DEBUGF("ata_cmd_data_in : no sectors left (status %02x)\n"
276 , (unsigned) status);
282 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
284 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
285 DEBUGF("ata_cmd_data_in : more sectors left (status %02x)\n"
286 , (unsigned) status);
293 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
297 // ---------------------------------------------------------------------------
298 // ATA/ATAPI driver : execute a data-out command
299 // ---------------------------------------------------------------------------
304 // 3 : expected DRQ=1
305 // 4 : no sectors left to read/verify
306 // 5 : more sectors to read/verify
307 // 6 : no sectors left to write
308 // 7 : more sectors to write
310 ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
311 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
313 u16 iobase1, iobase2, blksize;
315 u8 status, current, mode;
317 channel = device / 2;
320 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
321 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
322 mode = GET_EBDA(ata.devices[device].mode);
324 if (mode == ATA_MODE_PIO32)
329 // Reset count of transferred data
330 SET_EBDA(ata.trsfsectors,0);
331 SET_EBDA(ata.trsfbytes,0L);
334 status = inb(iobase1 + ATA_CB_STAT);
335 if (status & ATA_CB_STAT_BSY)
338 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
340 // sector will be 0 only on lba access. Convert to lba-chs
342 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
343 outb(0x00, iobase1 + ATA_CB_FR);
344 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
345 outb(lba >> 24, iobase1 + ATA_CB_SN);
346 outb(0, iobase1 + ATA_CB_CL);
347 outb(0, iobase1 + ATA_CB_CH);
349 count &= (1UL << 8) - 1;
350 lba &= (1UL << 24) - 1;
352 sector = (u16) (lba & 0x000000ffL);
353 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
354 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
357 outb(0x00, iobase1 + ATA_CB_FR);
358 outb(count, iobase1 + ATA_CB_SC);
359 outb(sector, iobase1 + ATA_CB_SN);
360 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
361 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
362 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
363 , iobase1 + ATA_CB_DH);
364 outb(command, iobase1 + ATA_CB_CMD);
366 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
367 status = inb(iobase1 + ATA_CB_STAT);
369 if (status & ATA_CB_STAT_ERR) {
370 DEBUGF("ata_cmd_data_out : read error\n");
372 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
373 DEBUGF("ata_cmd_data_out : DRQ not set (status %02x)\n"
374 , (unsigned) status);
378 // FIXME : move seg/off translation here
384 if (offset > 0xf800) {
389 if (mode == ATA_MODE_PIO32)
390 outsl(iobase1, segment, offset, blksize);
392 outsw(iobase1, segment, offset, blksize);
395 SET_EBDA(ata.trsfsectors,current);
397 status = inb(iobase1 + ATA_CB_STAT);
399 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
400 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
401 != ATA_CB_STAT_RDY ) {
402 DEBUGF("ata_cmd_data_out : no sectors left (status %02x)\n"
403 , (unsigned) status);
408 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
410 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
411 DEBUGF("ata_cmd_data_out : more sectors left (status %02x)\n"
412 , (unsigned) status);
419 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
423 // ---------------------------------------------------------------------------
424 // ATA/ATAPI driver : execute a packet command
425 // ---------------------------------------------------------------------------
428 // 1 : error in parameters
433 ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
434 , u32 length, u8 inout, u16 bufseg, u16 bufoff)
436 u16 iobase1, iobase2;
437 u16 lcount, lbefore, lafter, count;
439 u8 status, mode, lmode;
442 channel = device / 2;
445 // Data out is not supported yet
446 if (inout == ATA_DATA_OUT) {
447 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
451 // The header length must be even
453 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
457 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
458 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
459 mode = GET_EBDA(ata.devices[device].mode);
468 // Reset count of transferred data
469 SET_EBDA(ata.trsfsectors,0);
470 SET_EBDA(ata.trsfbytes,0L);
472 status = inb(iobase1 + ATA_CB_STAT);
473 if (status & ATA_CB_STAT_BSY)
476 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
477 outb(0x00, iobase1 + ATA_CB_FR);
478 outb(0x00, iobase1 + ATA_CB_SC);
479 outb(0x00, iobase1 + ATA_CB_SN);
480 outb(0xfff0 & 0x00ff, iobase1 + ATA_CB_CL);
481 outb(0xfff0 >> 8, iobase1 + ATA_CB_CH);
482 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
483 outb(ATA_CMD_PACKET, iobase1 + ATA_CB_CMD);
485 // Device should ok to receive command
486 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
487 status = inb(iobase1 + ATA_CB_STAT);
489 if (status & ATA_CB_STAT_ERR) {
490 DEBUGF("ata_cmd_packet : error, status is %02x\n", status);
492 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
493 DEBUGF("ata_cmd_packet : DRQ not set (status %02x)\n"
494 , (unsigned) status);
498 // Send command to device
501 outsw(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
503 if (inout == ATA_DATA_NO) {
504 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
505 status = inb(iobase1 + ATA_CB_STAT);
511 if (loops == 0) {//first time through
512 status = inb(iobase2 + ATA_CB_ASTAT);
513 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
515 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
518 status = inb(iobase1 + ATA_CB_STAT);
519 sc = inb(iobase1 + ATA_CB_SC);
521 // Check if command completed
522 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
523 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
526 if (status & ATA_CB_STAT_ERR) {
527 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
532 bufseg += (bufoff / 16);
535 // Get the byte count
536 lcount = ((u16)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
538 // adjust to read what we want
539 if (header > lcount) {
549 if (lcount > length) {
550 lafter=lcount-length;
561 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) "
562 , lbefore+lcount+lafter, lbefore, lcount, lafter);
563 DEBUGF("to 0x%04x:0x%04x\n", bufseg, bufoff);
565 // If counts not dividable by 4, use 16bits mode
567 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
568 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
569 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
571 // adds an extra byte if count are odd. before is always even
574 if ((lafter > 0) && (lafter & 0x01)) {
579 if (lmode == ATA_MODE_PIO32) {
580 lcount>>=2; lbefore>>=2; lafter>>=2;
582 lcount>>=1; lbefore>>=1; lafter>>=1;
586 for (i=0; i<lbefore; i++)
587 if (lmode == ATA_MODE_PIO32)
592 if (lmode == ATA_MODE_PIO32)
593 insl(iobase1, bufseg, bufoff, lcount);
595 insw(iobase1, bufseg, bufoff, lcount);
597 for (i=0; i<lafter; i++)
598 if (lmode == ATA_MODE_PIO32)
603 // Compute new buffer address
606 // Save transferred bytes count
608 SET_EBDA(ata.trsfbytes,transfer);
612 // Final check, device must be ready
613 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
614 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
615 != ATA_CB_STAT_RDY ) {
616 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
617 , (unsigned) status);
622 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
627 cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
629 u16 sectors = (count + 2048 - 1) / 2048;
632 memset(atacmd, 0, sizeof(atacmd));
633 atacmd[0]=0x28; // READ command
634 atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
635 atacmd[8]=(sectors & 0x00ff); // Sectors
636 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
637 atacmd[3]=(lba & 0x00ff0000) >> 16;
638 atacmd[4]=(lba & 0x0000ff00) >> 8;
639 atacmd[5]=(lba & 0x000000ff);
641 return ata_cmd_packet(device, atacmd, sizeof(atacmd)
642 , skip, count, ATA_DATA_IN
646 // ---------------------------------------------------------------------------
647 // ATA/ATAPI driver : device detection
648 // ---------------------------------------------------------------------------
653 u8 hdcount, cdcount, device, type;
655 memset(buffer, 0, sizeof(buffer));
657 #if CONFIG_MAX_ATA_INTERFACES > 0
658 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
659 SET_EBDA(ata.channels[0].iobase1,0x1f0);
660 SET_EBDA(ata.channels[0].iobase2,0x3f0);
661 SET_EBDA(ata.channels[0].irq,14);
663 #if CONFIG_MAX_ATA_INTERFACES > 1
664 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
665 SET_EBDA(ata.channels[1].iobase1,0x170);
666 SET_EBDA(ata.channels[1].iobase2,0x370);
667 SET_EBDA(ata.channels[1].irq,15);
669 #if CONFIG_MAX_ATA_INTERFACES > 2
670 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
671 SET_EBDA(ata.channels[2].iobase1,0x1e8);
672 SET_EBDA(ata.channels[2].iobase2,0x3e0);
673 SET_EBDA(ata.channels[2].irq,12);
675 #if CONFIG_MAX_ATA_INTERFACES > 3
676 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
677 SET_EBDA(ata.channels[3].iobase1,0x168);
678 SET_EBDA(ata.channels[3].iobase2,0x360);
679 SET_EBDA(ata.channels[3].irq,11);
681 #if CONFIG_MAX_ATA_INTERFACES > 4
682 #error Please fill the ATA interface informations
688 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
689 u16 iobase1, iobase2;
690 u8 channel, slave, shift;
691 u8 sc, sn, cl, ch, st;
693 channel = device / 2;
696 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
697 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
699 // Disable interrupts
700 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
703 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
704 outb(0x55, iobase1+ATA_CB_SC);
705 outb(0xaa, iobase1+ATA_CB_SN);
706 outb(0xaa, iobase1+ATA_CB_SC);
707 outb(0x55, iobase1+ATA_CB_SN);
708 outb(0x55, iobase1+ATA_CB_SC);
709 outb(0xaa, iobase1+ATA_CB_SN);
711 // If we found something
712 sc = inb(iobase1+ATA_CB_SC);
713 sn = inb(iobase1+ATA_CB_SN);
715 if ( (sc == 0x55) && (sn == 0xaa) ) {
716 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
721 // check for ATA or ATAPI
722 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
723 sc = inb(iobase1+ATA_CB_SC);
724 sn = inb(iobase1+ATA_CB_SN);
725 if ((sc==0x01) && (sn==0x01)) {
726 cl = inb(iobase1+ATA_CB_CL);
727 ch = inb(iobase1+ATA_CB_CH);
728 st = inb(iobase1+ATA_CB_STAT);
730 if ((cl==0x14) && (ch==0xeb)) {
731 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
732 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
733 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
734 } else if ((cl==0xff) && (ch==0xff)) {
735 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
740 type=GET_EBDA(ata.devices[device].type);
742 // Now we send a IDENTIFY command to ATA device
743 if(type == ATA_TYPE_ATA) {
745 u16 cylinders, heads, spt, blksize;
746 u8 translation, removable, mode;
748 //Temporary values to do the transfer
749 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
750 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
752 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
754 , GET_SEG(SS), (u32)buffer);
756 BX_PANIC("ata-detect: Failed to detect ATA device\n");
758 removable = (buffer[0] & 0x80) ? 1 : 0;
759 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
760 blksize = *(u16*)&buffer[10];
762 cylinders = *(u16*)&buffer[1*2]; // word 1
763 heads = *(u16*)&buffer[3*2]; // word 3
764 spt = *(u16*)&buffer[6*2]; // word 6
766 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
768 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
769 SET_EBDA(ata.devices[device].removable, removable);
770 SET_EBDA(ata.devices[device].mode, mode);
771 SET_EBDA(ata.devices[device].blksize, blksize);
772 SET_EBDA(ata.devices[device].pchs.heads, heads);
773 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
774 SET_EBDA(ata.devices[device].pchs.spt, spt);
775 SET_EBDA(ata.devices[device].sectors, sectors);
776 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
778 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
779 for (shift=device%4; shift>0; shift--)
783 SET_EBDA(ata.devices[device].translation, translation);
785 switch (translation) {
786 case ATA_TRANSLATION_NONE:
789 case ATA_TRANSLATION_LBA:
792 case ATA_TRANSLATION_LARGE:
795 case ATA_TRANSLATION_RECHS:
799 switch (translation) {
800 case ATA_TRANSLATION_NONE:
802 case ATA_TRANSLATION_LBA:
805 heads = sectors / 1024;
806 if (heads>128) heads = 255;
807 else if (heads>64) heads = 128;
808 else if (heads>32) heads = 64;
809 else if (heads>16) heads = 32;
811 cylinders = sectors / heads;
813 case ATA_TRANSLATION_RECHS:
814 // Take care not to overflow
816 if(cylinders>61439) cylinders=61439;
818 cylinders = (u16)((u32)(cylinders)*16/15);
820 // then go through the large bitshift process
821 case ATA_TRANSLATION_LARGE:
822 while(cylinders > 1024) {
826 // If we max out the head count
827 if (heads > 127) break;
831 // clip to 1024 cylinders in lchs
832 if (cylinders > 1024)
834 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
836 SET_EBDA(ata.devices[device].lchs.heads, heads);
837 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
838 SET_EBDA(ata.devices[device].lchs.spt, spt);
841 SET_EBDA(ata.idmap[0][hdcount], device);
845 // Now we send a IDENTIFY command to ATAPI device
846 if(type == ATA_TYPE_ATAPI) {
848 u8 type, removable, mode;
851 //Temporary values to do the transfer
852 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
853 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
855 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
857 , GET_SEG(SS), (u32)buffer);
859 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
861 type = buffer[1] & 0x1f;
862 removable = (buffer[0] & 0x80) ? 1 : 0;
863 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
866 SET_EBDA(ata.devices[device].device, type);
867 SET_EBDA(ata.devices[device].removable, removable);
868 SET_EBDA(ata.devices[device].mode, mode);
869 SET_EBDA(ata.devices[device].blksize, blksize);
872 SET_EBDA(ata.idmap[1][cdcount], device);
878 u8 c, i, version=0, model[41];
882 sizeinmb = GET_EBDA(ata.devices[device].sectors);
885 // Read ATA/ATAPI version
886 ataversion=((u16)(buffer[161])<<8) | buffer[160];
887 for(version=15;version>0;version--) {
888 if ((ataversion&(1<<version))!=0)
894 model[i*2] = buffer[(i*2)+54+1];
895 model[(i*2)+1] = buffer[(i*2)+54];
911 printf("ata%d %s: ",channel,slave?" slave":"master");
913 while ((c=model[i++]))
915 if (sizeinmb < (1UL<<16))
916 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
918 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
921 printf("ata%d %s: ",channel,slave?" slave":"master");
923 while ((c=model[i++]))
925 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
926 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
928 printf(" ATAPI-%d Device\n",version);
930 case ATA_TYPE_UNKNOWN:
931 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
936 // Store the devices counts
937 SET_EBDA(ata.hdcount, hdcount);
938 SET_EBDA(ata.cdcount, cdcount);
939 SET_BDA(disk_count, hdcount);
943 // FIXME : should use bios=cmos|auto|disable bits
944 // FIXME : should know about translation bits
945 // FIXME : move hard_drive_post here