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 cmdlen, u16 cmdseg, u16 cmdoff, 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);
494 cmdseg += (cmdoff / 16);
497 // Send command to device
500 outsw(iobase1, cmdseg, cmdoff, cmdlen);
502 if (inout == ATA_DATA_NO) {
503 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
504 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);
516 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
519 status = inb(iobase1 + ATA_CB_STAT);
520 sc = inb(iobase1 + ATA_CB_SC);
522 // Check if command completed
523 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
524 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
527 if (status & ATA_CB_STAT_ERR) {
528 BX_DEBUG_ATA("ata_cmd_packet : error (status %02x)\n",status);
533 bufseg += (bufoff / 16);
536 // Get the byte count
537 lcount = ((u16)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
539 // adjust to read what we want
552 lafter=lcount-length;
564 BX_DEBUG_ATA("Trying to read %04x bytes (%04x %04x %04x) "
565 ,lbefore+lcount+lafter,lbefore,lcount,lafter);
566 BX_DEBUG_ATA("to 0x%04x:0x%04x\n",bufseg,bufoff);
568 // If counts not dividable by 4, use 16bits mode
570 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
571 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
572 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
574 // adds an extra byte if count are odd. before is always even
577 if ((lafter > 0) && (lafter & 0x01)) {
582 if (lmode == ATA_MODE_PIO32) {
583 lcount>>=2; lbefore>>=2; lafter>>=2;
585 lcount>>=1; lbefore>>=1; lafter>>=1;
589 for (i=0; i<lbefore; i++)
590 if (lmode == ATA_MODE_PIO32)
595 if (lmode == ATA_MODE_PIO32)
596 insl(iobase1, bufoff, bufseg, lcount);
598 insw(iobase1, bufoff, bufseg, lcount);
600 for (i=0; i<lafter; i++)
601 if (lmode == ATA_MODE_PIO32)
606 // Compute new buffer address
609 // Save transferred bytes count
611 SET_EBDA(ata.trsfbytes,transfer);
615 // Final check, device must be ready
616 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
617 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
618 != ATA_CB_STAT_RDY ) {
619 BX_DEBUG_ATA("ata_cmd_packet : not ready (status %02x)\n"
620 , (unsigned) status);
625 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
629 // ---------------------------------------------------------------------------
630 // ATA/ATAPI driver : device detection
631 // ---------------------------------------------------------------------------
636 u8 hdcount, cdcount, device, type;
638 memset(buffer, 0, sizeof(buffer));
640 #if CONFIG_MAX_ATA_INTERFACES > 0
641 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
642 SET_EBDA(ata.channels[0].iobase1,0x1f0);
643 SET_EBDA(ata.channels[0].iobase2,0x3f0);
644 SET_EBDA(ata.channels[0].irq,14);
646 #if CONFIG_MAX_ATA_INTERFACES > 1
647 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
648 SET_EBDA(ata.channels[1].iobase1,0x170);
649 SET_EBDA(ata.channels[1].iobase2,0x370);
650 SET_EBDA(ata.channels[1].irq,15);
652 #if CONFIG_MAX_ATA_INTERFACES > 2
653 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
654 SET_EBDA(ata.channels[2].iobase1,0x1e8);
655 SET_EBDA(ata.channels[2].iobase2,0x3e0);
656 SET_EBDA(ata.channels[2].irq,12);
658 #if CONFIG_MAX_ATA_INTERFACES > 3
659 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
660 SET_EBDA(ata.channels[3].iobase1,0x168);
661 SET_EBDA(ata.channels[3].iobase2,0x360);
662 SET_EBDA(ata.channels[3].irq,11);
664 #if CONFIG_MAX_ATA_INTERFACES > 4
665 #error Please fill the ATA interface informations
671 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
672 u16 iobase1, iobase2;
673 u8 channel, slave, shift;
674 u8 sc, sn, cl, ch, st;
676 channel = device / 2;
679 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
680 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
682 // Disable interrupts
683 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
686 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
687 outb(0x55, iobase1+ATA_CB_SC);
688 outb(0xaa, iobase1+ATA_CB_SN);
689 outb(0xaa, iobase1+ATA_CB_SC);
690 outb(0x55, iobase1+ATA_CB_SN);
691 outb(0x55, iobase1+ATA_CB_SC);
692 outb(0xaa, iobase1+ATA_CB_SN);
694 // If we found something
695 sc = inb(iobase1+ATA_CB_SC);
696 sn = inb(iobase1+ATA_CB_SN);
698 if ( (sc == 0x55) && (sn == 0xaa) ) {
699 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
704 // check for ATA or ATAPI
705 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
706 sc = inb(iobase1+ATA_CB_SC);
707 sn = inb(iobase1+ATA_CB_SN);
708 if ((sc==0x01) && (sn==0x01)) {
709 cl = inb(iobase1+ATA_CB_CL);
710 ch = inb(iobase1+ATA_CB_CH);
711 st = inb(iobase1+ATA_CB_STAT);
713 if ((cl==0x14) && (ch==0xeb)) {
714 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
715 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
716 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
717 } else if ((cl==0xff) && (ch==0xff)) {
718 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
723 type=GET_EBDA(ata.devices[device].type);
725 // Now we send a IDENTIFY command to ATA device
726 if(type == ATA_TYPE_ATA) {
728 u16 cylinders, heads, spt, blksize;
729 u8 translation, removable, mode;
731 //Temporary values to do the transfer
732 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
733 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
735 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
737 , GET_SEG(SS), (u32)buffer);
739 BX_PANIC("ata-detect: Failed to detect ATA device\n");
741 removable = (buffer[0] & 0x80) ? 1 : 0;
742 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
743 blksize = *(u16*)&buffer[10];
745 cylinders = *(u16*)&buffer[1*2]; // word 1
746 heads = *(u16*)&buffer[3*2]; // word 3
747 spt = *(u16*)&buffer[6*2]; // word 6
749 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
751 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
752 SET_EBDA(ata.devices[device].removable, removable);
753 SET_EBDA(ata.devices[device].mode, mode);
754 SET_EBDA(ata.devices[device].blksize, blksize);
755 SET_EBDA(ata.devices[device].pchs.heads, heads);
756 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
757 SET_EBDA(ata.devices[device].pchs.spt, spt);
758 SET_EBDA(ata.devices[device].sectors, sectors);
759 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
761 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
762 for (shift=device%4; shift>0; shift--)
766 SET_EBDA(ata.devices[device].translation, translation);
768 switch (translation) {
769 case ATA_TRANSLATION_NONE:
772 case ATA_TRANSLATION_LBA:
775 case ATA_TRANSLATION_LARGE:
778 case ATA_TRANSLATION_RECHS:
782 switch (translation) {
783 case ATA_TRANSLATION_NONE:
785 case ATA_TRANSLATION_LBA:
788 heads = sectors / 1024;
789 if (heads>128) heads = 255;
790 else if (heads>64) heads = 128;
791 else if (heads>32) heads = 64;
792 else if (heads>16) heads = 32;
794 cylinders = sectors / heads;
796 case ATA_TRANSLATION_RECHS:
797 // Take care not to overflow
799 if(cylinders>61439) cylinders=61439;
801 cylinders = (u16)((u32)(cylinders)*16/15);
803 // then go through the large bitshift process
804 case ATA_TRANSLATION_LARGE:
805 while(cylinders > 1024) {
809 // If we max out the head count
810 if (heads > 127) break;
814 // clip to 1024 cylinders in lchs
815 if (cylinders > 1024)
817 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
819 SET_EBDA(ata.devices[device].lchs.heads, heads);
820 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
821 SET_EBDA(ata.devices[device].lchs.spt, spt);
824 SET_EBDA(ata.hdidmap[hdcount], device);
828 // Now we send a IDENTIFY command to ATAPI device
829 if(type == ATA_TYPE_ATAPI) {
831 u8 type, removable, mode;
834 //Temporary values to do the transfer
835 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
836 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
838 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
840 , GET_SEG(SS), (u32)buffer);
842 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
844 type = buffer[1] & 0x1f;
845 removable = (buffer[0] & 0x80) ? 1 : 0;
846 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
849 SET_EBDA(ata.devices[device].device, type);
850 SET_EBDA(ata.devices[device].removable, removable);
851 SET_EBDA(ata.devices[device].mode, mode);
852 SET_EBDA(ata.devices[device].blksize, blksize);
855 SET_EBDA(ata.cdidmap[cdcount], device);
861 u8 c, i, version=0, model[41];
865 sizeinmb = GET_EBDA(ata.devices[device].sectors);
868 // Read ATA/ATAPI version
869 ataversion=((u16)(buffer[161])<<8) | buffer[160];
870 for(version=15;version>0;version--) {
871 if ((ataversion&(1<<version))!=0)
877 model[i*2] = buffer[(i*2)+54+1];
878 model[(i*2)+1] = buffer[(i*2)+54];
894 printf("ata%d %s: ",channel,slave?" slave":"master");
896 while ((c=model[i++]))
898 if (sizeinmb < (1UL<<16))
899 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
901 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
904 printf("ata%d %s: ",channel,slave?" slave":"master");
906 while ((c=model[i++]))
908 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
909 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
911 printf(" ATAPI-%d Device\n",version);
913 case ATA_TYPE_UNKNOWN:
914 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
919 // Store the devices counts
920 SET_EBDA(ata.hdcount, hdcount);
921 SET_EBDA(ata.cdcount, cdcount);
922 SET_BDA(disk_count, hdcount);
926 // FIXME : should use bios=cmos|auto|disable bits
927 // FIXME : should know about translation bits
928 // FIXME : move hard_drive_post here