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 BX_DEBUG_ATA BX_INFO
25 // XXX - lots of redundancy in this file.
28 await_ide(u8 when_done, u16 base, u16 timeout)
31 // for the times you're supposed to throw one away
32 u16 status = inb(base + ATA_CB_STAT);
34 status = inb(base+ATA_CB_STAT);
38 result = status & ATA_CB_STAT_BSY;
39 else if (when_done == NOT_BSY)
40 result = !(status & ATA_CB_STAT_BSY);
41 else if (when_done == NOT_BSY_DRQ)
42 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
43 else if (when_done == NOT_BSY_NOT_DRQ)
44 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
45 else if (when_done == NOT_BSY_RDY)
46 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
47 else if (when_done == TIMEOUT)
52 // mod 2048 each 16 ms
53 if (time>>16 != last) {
55 BX_DEBUG_ATA("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n",when_done,time>>11, timeout);
57 if (status & ATA_CB_STAT_ERR) {
58 BX_DEBUG_ATA("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n",when_done,time>>11, timeout);
61 if ((timeout == 0) || ((time>>11) > timeout))
64 BX_INFO("IDE time out\n");
69 // ---------------------------------------------------------------------------
70 // ATA/ATAPI driver : software reset
71 // ---------------------------------------------------------------------------
73 // 8.2.1 Software reset - Device 0
79 u8 channel, slave, sn, sc;
85 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
86 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
90 // 8.2.1 (a) -- set SRST in DC
91 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
93 // 8.2.1 (b) -- wait for BSY
94 await_ide(BSY, iobase1, 20);
96 // 8.2.1 (f) -- clear SRST
97 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
99 type=GET_EBDA(ata.devices[device].type);
100 if (type != ATA_TYPE_NONE) {
102 // 8.2.1 (g) -- check for sc==sn==0x01
104 outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
105 sc = inb(iobase1+ATA_CB_SC);
106 sn = inb(iobase1+ATA_CB_SN);
108 if ( (sc==0x01) && (sn==0x01) ) {
109 if (type == ATA_TYPE_ATA) //ATA
110 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
112 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
115 // 8.2.1 (h) -- wait for not BSY
116 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
120 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
124 insw(u16 port, u16 segment, u16 offset, u16 count)
127 for (i=0; i<count; i++) {
129 SET_FARVAR(segment, *(u16*)(offset + 2*i), d);
134 insl(u16 port, u16 segment, u16 offset, u16 count)
137 for (i=0; i<count; i++) {
139 SET_FARVAR(segment, *(u32*)(offset + 4*i), d);
144 outsw(u16 port, u16 segment, u16 offset, u16 count)
147 for (i=0; i<count; i++) {
148 u16 d = GET_FARVAR(segment, *(u16*)(offset + 2*i));
154 outsl(u16 port, u16 segment, u16 offset, u16 count)
157 for (i=0; i<count; i++) {
158 u32 d = GET_FARVAR(segment, *(u32*)(offset + 4*i));
164 // ---------------------------------------------------------------------------
165 // ATA/ATAPI driver : execute a data-in command
166 // ---------------------------------------------------------------------------
171 // 3 : expected DRQ=1
172 // 4 : no sectors left to read/verify
173 // 5 : more sectors to read/verify
174 // 6 : no sectors left to write
175 // 7 : more sectors to write
177 ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
178 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
180 u16 iobase1, iobase2, blksize;
182 u8 status, current, mode;
184 channel = device / 2;
187 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
188 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
189 mode = GET_EBDA(ata.devices[device].mode);
191 if (mode == ATA_MODE_PIO32)
196 // Reset count of transferred data
197 SET_EBDA(ata.trsfsectors,0);
198 SET_EBDA(ata.trsfbytes,0L);
201 status = inb(iobase1 + ATA_CB_STAT);
202 if (status & ATA_CB_STAT_BSY)
205 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
207 // sector will be 0 only on lba access. Convert to lba-chs
209 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
210 outb(0x00, iobase1 + ATA_CB_FR);
211 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
212 outb(lba >> 24, iobase1 + ATA_CB_SN);
213 outb(0, iobase1 + ATA_CB_CL);
214 outb(0, iobase1 + ATA_CB_CH);
216 count &= (1UL << 8) - 1;
217 lba &= (1UL << 24) - 1;
219 sector = (u16) (lba & 0x000000ffL);
220 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
221 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
224 outb(0x00, iobase1 + ATA_CB_FR);
225 outb(count, iobase1 + ATA_CB_SC);
226 outb(sector, iobase1 + ATA_CB_SN);
227 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
228 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
229 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
230 , iobase1 + ATA_CB_DH);
231 outb(command, iobase1 + ATA_CB_CMD);
233 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
234 status = inb(iobase1 + ATA_CB_STAT);
236 if (status & ATA_CB_STAT_ERR) {
237 BX_DEBUG_ATA("ata_cmd_data_in : read error\n");
239 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
240 BX_DEBUG_ATA("ata_cmd_data_in : DRQ not set (status %02x)\n"
241 , (unsigned) status);
245 // FIXME : move seg/off translation here
251 if (offset > 0xf800) {
256 if (mode == ATA_MODE_PIO32)
257 insl(iobase1, segment, offset, blksize);
259 insw(iobase1, segment, offset, blksize);
262 SET_EBDA(ata.trsfsectors,current);
264 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
265 status = inb(iobase1 + ATA_CB_STAT);
267 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
269 != ATA_CB_STAT_RDY ) {
270 BX_DEBUG_ATA("ata_cmd_data_in : no sectors left (status %02x)\n"
271 , (unsigned) status);
277 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
279 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
280 BX_DEBUG_ATA("ata_cmd_data_in : more sectors left (status %02x)\n"
281 , (unsigned) status);
288 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
292 // ---------------------------------------------------------------------------
293 // ATA/ATAPI driver : execute a data-out command
294 // ---------------------------------------------------------------------------
299 // 3 : expected DRQ=1
300 // 4 : no sectors left to read/verify
301 // 5 : more sectors to read/verify
302 // 6 : no sectors left to write
303 // 7 : more sectors to write
305 ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
306 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
308 u16 iobase1, iobase2, blksize;
310 u8 status, current, mode;
312 channel = device / 2;
315 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
316 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
317 mode = GET_EBDA(ata.devices[device].mode);
319 if (mode == ATA_MODE_PIO32)
324 // Reset count of transferred data
325 SET_EBDA(ata.trsfsectors,0);
326 SET_EBDA(ata.trsfbytes,0L);
329 status = inb(iobase1 + ATA_CB_STAT);
330 if (status & ATA_CB_STAT_BSY)
333 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
335 // sector will be 0 only on lba access. Convert to lba-chs
337 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
338 outb(0x00, iobase1 + ATA_CB_FR);
339 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
340 outb(lba >> 24, iobase1 + ATA_CB_SN);
341 outb(0, iobase1 + ATA_CB_CL);
342 outb(0, iobase1 + ATA_CB_CH);
344 count &= (1UL << 8) - 1;
345 lba &= (1UL << 24) - 1;
347 sector = (u16) (lba & 0x000000ffL);
348 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
349 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
352 outb(0x00, iobase1 + ATA_CB_FR);
353 outb(count, iobase1 + ATA_CB_SC);
354 outb(sector, iobase1 + ATA_CB_SN);
355 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
356 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
357 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
358 , iobase1 + ATA_CB_DH);
359 outb(command, iobase1 + ATA_CB_CMD);
361 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
362 status = inb(iobase1 + ATA_CB_STAT);
364 if (status & ATA_CB_STAT_ERR) {
365 BX_DEBUG_ATA("ata_cmd_data_out : read error\n");
367 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
368 BX_DEBUG_ATA("ata_cmd_data_out : DRQ not set (status %02x)\n"
369 , (unsigned) status);
373 // FIXME : move seg/off translation here
379 if (offset > 0xf800) {
384 if (mode == ATA_MODE_PIO32)
385 outsl(iobase1, segment, offset, blksize);
387 outsw(iobase1, segment, offset, blksize);
390 SET_EBDA(ata.trsfsectors,current);
392 status = inb(iobase1 + ATA_CB_STAT);
394 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
395 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
396 != ATA_CB_STAT_RDY ) {
397 BX_DEBUG_ATA("ata_cmd_data_out : no sectors left (status %02x)\n"
398 , (unsigned) status);
403 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
405 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
406 BX_DEBUG_ATA("ata_cmd_data_out : more sectors left (status %02x)\n"
407 , (unsigned) status);
414 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
418 // ---------------------------------------------------------------------------
419 // ATA/ATAPI driver : execute a packet command
420 // ---------------------------------------------------------------------------
423 // 1 : error in parameters
428 ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
429 , u32 length, u8 inout, u16 bufseg, u16 bufoff)
431 u16 iobase1, iobase2;
432 u16 lcount, lbefore, lafter, count;
434 u8 status, mode, lmode;
437 channel = device / 2;
440 // Data out is not supported yet
441 if (inout == ATA_DATA_OUT) {
442 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
446 // The header length must be even
448 BX_DEBUG_ATA("ata_cmd_packet : header must be even (%04x)\n",header);
452 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
453 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
454 mode = GET_EBDA(ata.devices[device].mode);
463 // Reset count of transferred data
464 SET_EBDA(ata.trsfsectors,0);
465 SET_EBDA(ata.trsfbytes,0L);
467 status = inb(iobase1 + ATA_CB_STAT);
468 if (status & ATA_CB_STAT_BSY)
471 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
472 outb(0x00, iobase1 + ATA_CB_FR);
473 outb(0x00, iobase1 + ATA_CB_SC);
474 outb(0x00, iobase1 + ATA_CB_SN);
475 outb(0xfff0 & 0x00ff, iobase1 + ATA_CB_CL);
476 outb(0xfff0 >> 8, iobase1 + ATA_CB_CH);
477 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
478 outb(ATA_CMD_PACKET, iobase1 + ATA_CB_CMD);
480 // Device should ok to receive command
481 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
482 status = inb(iobase1 + ATA_CB_STAT);
484 if (status & ATA_CB_STAT_ERR) {
485 BX_DEBUG_ATA("ata_cmd_packet : error, status is %02x\n",status);
487 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
488 BX_DEBUG_ATA("ata_cmd_packet : DRQ not set (status %02x)\n"
489 , (unsigned) status);
493 // Send command to device
496 outsw(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
498 if (inout == ATA_DATA_NO) {
499 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
500 status = inb(iobase1 + ATA_CB_STAT);
506 if (loops == 0) {//first time through
507 status = inb(iobase2 + ATA_CB_ASTAT);
508 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
510 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
513 status = inb(iobase1 + ATA_CB_STAT);
514 sc = inb(iobase1 + ATA_CB_SC);
516 // Check if command completed
517 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
518 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
521 if (status & ATA_CB_STAT_ERR) {
522 BX_DEBUG_ATA("ata_cmd_packet : error (status %02x)\n",status);
527 bufseg += (bufoff / 16);
530 // Get the byte count
531 lcount = ((u16)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
533 // adjust to read what we want
534 if (header > lcount) {
544 if (lcount > length) {
545 lafter=lcount-length;
556 BX_DEBUG_ATA("Trying to read %04x bytes (%04x %04x %04x) "
557 ,lbefore+lcount+lafter,lbefore,lcount,lafter);
558 BX_DEBUG_ATA("to 0x%04x:0x%04x\n",bufseg,bufoff);
560 // If counts not dividable by 4, use 16bits mode
562 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
563 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
564 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
566 // adds an extra byte if count are odd. before is always even
569 if ((lafter > 0) && (lafter & 0x01)) {
574 if (lmode == ATA_MODE_PIO32) {
575 lcount>>=2; lbefore>>=2; lafter>>=2;
577 lcount>>=1; lbefore>>=1; lafter>>=1;
581 for (i=0; i<lbefore; i++)
582 if (lmode == ATA_MODE_PIO32)
587 if (lmode == ATA_MODE_PIO32)
588 insl(iobase1, bufseg, bufoff, lcount);
590 insw(iobase1, bufseg, bufoff, lcount);
592 for (i=0; i<lafter; i++)
593 if (lmode == ATA_MODE_PIO32)
598 // Compute new buffer address
601 // Save transferred bytes count
603 SET_EBDA(ata.trsfbytes,transfer);
607 // Final check, device must be ready
608 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
609 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
610 != ATA_CB_STAT_RDY ) {
611 BX_DEBUG_ATA("ata_cmd_packet : not ready (status %02x)\n"
612 , (unsigned) status);
617 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
622 cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
624 u16 sectors = (count + 2048 - 1) / 2048;
627 memset(atacmd, 0, sizeof(atacmd));
628 atacmd[0]=0x28; // READ command
629 atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
630 atacmd[8]=(sectors & 0x00ff); // Sectors
631 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
632 atacmd[3]=(lba & 0x00ff0000) >> 16;
633 atacmd[4]=(lba & 0x0000ff00) >> 8;
634 atacmd[5]=(lba & 0x000000ff);
636 return ata_cmd_packet(device, atacmd, sizeof(atacmd)
637 , skip, count, ATA_DATA_IN
641 // ---------------------------------------------------------------------------
642 // ATA/ATAPI driver : device detection
643 // ---------------------------------------------------------------------------
648 u8 hdcount, cdcount, device, type;
650 memset(buffer, 0, sizeof(buffer));
652 #if CONFIG_MAX_ATA_INTERFACES > 0
653 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
654 SET_EBDA(ata.channels[0].iobase1,0x1f0);
655 SET_EBDA(ata.channels[0].iobase2,0x3f0);
656 SET_EBDA(ata.channels[0].irq,14);
658 #if CONFIG_MAX_ATA_INTERFACES > 1
659 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
660 SET_EBDA(ata.channels[1].iobase1,0x170);
661 SET_EBDA(ata.channels[1].iobase2,0x370);
662 SET_EBDA(ata.channels[1].irq,15);
664 #if CONFIG_MAX_ATA_INTERFACES > 2
665 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
666 SET_EBDA(ata.channels[2].iobase1,0x1e8);
667 SET_EBDA(ata.channels[2].iobase2,0x3e0);
668 SET_EBDA(ata.channels[2].irq,12);
670 #if CONFIG_MAX_ATA_INTERFACES > 3
671 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
672 SET_EBDA(ata.channels[3].iobase1,0x168);
673 SET_EBDA(ata.channels[3].iobase2,0x360);
674 SET_EBDA(ata.channels[3].irq,11);
676 #if CONFIG_MAX_ATA_INTERFACES > 4
677 #error Please fill the ATA interface informations
683 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
684 u16 iobase1, iobase2;
685 u8 channel, slave, shift;
686 u8 sc, sn, cl, ch, st;
688 channel = device / 2;
691 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
692 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
694 // Disable interrupts
695 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
698 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
699 outb(0x55, iobase1+ATA_CB_SC);
700 outb(0xaa, iobase1+ATA_CB_SN);
701 outb(0xaa, iobase1+ATA_CB_SC);
702 outb(0x55, iobase1+ATA_CB_SN);
703 outb(0x55, iobase1+ATA_CB_SC);
704 outb(0xaa, iobase1+ATA_CB_SN);
706 // If we found something
707 sc = inb(iobase1+ATA_CB_SC);
708 sn = inb(iobase1+ATA_CB_SN);
710 if ( (sc == 0x55) && (sn == 0xaa) ) {
711 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
716 // check for ATA or ATAPI
717 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
718 sc = inb(iobase1+ATA_CB_SC);
719 sn = inb(iobase1+ATA_CB_SN);
720 if ((sc==0x01) && (sn==0x01)) {
721 cl = inb(iobase1+ATA_CB_CL);
722 ch = inb(iobase1+ATA_CB_CH);
723 st = inb(iobase1+ATA_CB_STAT);
725 if ((cl==0x14) && (ch==0xeb)) {
726 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
727 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
728 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
729 } else if ((cl==0xff) && (ch==0xff)) {
730 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
735 type=GET_EBDA(ata.devices[device].type);
737 // Now we send a IDENTIFY command to ATA device
738 if(type == ATA_TYPE_ATA) {
740 u16 cylinders, heads, spt, blksize;
741 u8 translation, removable, mode;
743 //Temporary values to do the transfer
744 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
745 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
747 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
749 , GET_SEG(SS), (u32)buffer);
751 BX_PANIC("ata-detect: Failed to detect ATA device\n");
753 removable = (buffer[0] & 0x80) ? 1 : 0;
754 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
755 blksize = *(u16*)&buffer[10];
757 cylinders = *(u16*)&buffer[1*2]; // word 1
758 heads = *(u16*)&buffer[3*2]; // word 3
759 spt = *(u16*)&buffer[6*2]; // word 6
761 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
763 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
764 SET_EBDA(ata.devices[device].removable, removable);
765 SET_EBDA(ata.devices[device].mode, mode);
766 SET_EBDA(ata.devices[device].blksize, blksize);
767 SET_EBDA(ata.devices[device].pchs.heads, heads);
768 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
769 SET_EBDA(ata.devices[device].pchs.spt, spt);
770 SET_EBDA(ata.devices[device].sectors, sectors);
771 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
773 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
774 for (shift=device%4; shift>0; shift--)
778 SET_EBDA(ata.devices[device].translation, translation);
780 switch (translation) {
781 case ATA_TRANSLATION_NONE:
784 case ATA_TRANSLATION_LBA:
787 case ATA_TRANSLATION_LARGE:
790 case ATA_TRANSLATION_RECHS:
794 switch (translation) {
795 case ATA_TRANSLATION_NONE:
797 case ATA_TRANSLATION_LBA:
800 heads = sectors / 1024;
801 if (heads>128) heads = 255;
802 else if (heads>64) heads = 128;
803 else if (heads>32) heads = 64;
804 else if (heads>16) heads = 32;
806 cylinders = sectors / heads;
808 case ATA_TRANSLATION_RECHS:
809 // Take care not to overflow
811 if(cylinders>61439) cylinders=61439;
813 cylinders = (u16)((u32)(cylinders)*16/15);
815 // then go through the large bitshift process
816 case ATA_TRANSLATION_LARGE:
817 while(cylinders > 1024) {
821 // If we max out the head count
822 if (heads > 127) break;
826 // clip to 1024 cylinders in lchs
827 if (cylinders > 1024)
829 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
831 SET_EBDA(ata.devices[device].lchs.heads, heads);
832 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
833 SET_EBDA(ata.devices[device].lchs.spt, spt);
836 SET_EBDA(ata.idmap[0][hdcount], device);
840 // Now we send a IDENTIFY command to ATAPI device
841 if(type == ATA_TYPE_ATAPI) {
843 u8 type, removable, mode;
846 //Temporary values to do the transfer
847 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
848 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
850 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
852 , GET_SEG(SS), (u32)buffer);
854 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
856 type = buffer[1] & 0x1f;
857 removable = (buffer[0] & 0x80) ? 1 : 0;
858 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
861 SET_EBDA(ata.devices[device].device, type);
862 SET_EBDA(ata.devices[device].removable, removable);
863 SET_EBDA(ata.devices[device].mode, mode);
864 SET_EBDA(ata.devices[device].blksize, blksize);
867 SET_EBDA(ata.idmap[1][cdcount], device);
873 u8 c, i, version=0, model[41];
877 sizeinmb = GET_EBDA(ata.devices[device].sectors);
880 // Read ATA/ATAPI version
881 ataversion=((u16)(buffer[161])<<8) | buffer[160];
882 for(version=15;version>0;version--) {
883 if ((ataversion&(1<<version))!=0)
889 model[i*2] = buffer[(i*2)+54+1];
890 model[(i*2)+1] = buffer[(i*2)+54];
906 printf("ata%d %s: ",channel,slave?" slave":"master");
908 while ((c=model[i++]))
910 if (sizeinmb < (1UL<<16))
911 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
913 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
916 printf("ata%d %s: ",channel,slave?" slave":"master");
918 while ((c=model[i++]))
920 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
921 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
923 printf(" ATAPI-%d Device\n",version);
925 case ATA_TYPE_UNKNOWN:
926 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
931 // Store the devices counts
932 SET_EBDA(ata.hdcount, hdcount);
933 SET_EBDA(ata.cdcount, cdcount);
934 SET_BDA(disk_count, hdcount);
938 // FIXME : should use bios=cmos|auto|disable bits
939 // FIXME : should know about translation bits
940 // FIXME : move hard_drive_post here