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 // ---------------------------------------------------------------------------
130 // ATA/ATAPI driver : execute a data-in command
131 // ---------------------------------------------------------------------------
136 // 3 : expected DRQ=1
137 // 4 : no sectors left to read/verify
138 // 5 : more sectors to read/verify
139 // 6 : no sectors left to write
140 // 7 : more sectors to write
142 ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
143 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
145 DEBUGF("ata_cmd_data_in d=%d cmd=%d count=%d c=%d h=%d s=%d"
146 " lba=%d seg=%x off=%x\n"
147 , device, command, count, cylinder, head, sector
148 , lba, segment, offset);
150 u8 channel = device / 2;
151 u8 slave = device % 2;
153 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
154 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
155 u8 mode = GET_EBDA(ata.devices[device].mode);
157 // Reset count of transferred data
158 SET_EBDA(ata.trsfsectors,0);
159 SET_EBDA(ata.trsfbytes,0L);
161 u8 status = inb(iobase1 + ATA_CB_STAT);
162 if (status & ATA_CB_STAT_BSY)
165 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
167 // sector will be 0 only on lba access. Convert to lba-chs
169 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
170 outb(0x00, iobase1 + ATA_CB_FR);
171 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
172 outb(lba >> 24, iobase1 + ATA_CB_SN);
173 outb(0, iobase1 + ATA_CB_CL);
174 outb(0, iobase1 + ATA_CB_CH);
176 count &= (1UL << 8) - 1;
177 lba &= (1UL << 24) - 1;
179 sector = (u16) (lba & 0x000000ffL);
180 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
181 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
184 outb(0x00, iobase1 + ATA_CB_FR);
185 outb(count, iobase1 + ATA_CB_SC);
186 outb(sector, iobase1 + ATA_CB_SN);
187 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
188 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
189 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
190 , iobase1 + ATA_CB_DH);
191 outb(command, iobase1 + ATA_CB_CMD);
193 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
194 status = inb(iobase1 + ATA_CB_STAT);
196 if (status & ATA_CB_STAT_ERR) {
197 DEBUGF("ata_cmd_data_in : read error\n");
199 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
200 DEBUGF("ata_cmd_data_in : DRQ not set (status %02x)\n"
201 , (unsigned) status);
205 // FIXME : move seg/off translation here
211 if (offset > 0xf800) {
216 if (mode == ATA_MODE_PIO32)
217 insl_seg(iobase1, segment, offset, 512 / 4);
219 insw_seg(iobase1, segment, offset, 512 / 2);
223 SET_EBDA(ata.trsfsectors,current);
225 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
226 status = inb(iobase1 + ATA_CB_STAT);
228 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
230 != ATA_CB_STAT_RDY ) {
231 DEBUGF("ata_cmd_data_in : no sectors left (status %02x)\n"
232 , (unsigned) status);
238 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
240 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
241 DEBUGF("ata_cmd_data_in : more sectors left (status %02x)\n"
242 , (unsigned) status);
249 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
253 // ---------------------------------------------------------------------------
254 // ATA/ATAPI driver : execute a data-out command
255 // ---------------------------------------------------------------------------
260 // 3 : expected DRQ=1
261 // 4 : no sectors left to read/verify
262 // 5 : more sectors to read/verify
263 // 6 : no sectors left to write
264 // 7 : more sectors to write
266 ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
267 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
269 DEBUGF("ata_cmd_data_out d=%d cmd=%d count=%d c=%d h=%d s=%d"
270 " lba=%d seg=%x off=%x\n"
271 , device, command, count, cylinder, head, sector
272 , lba, segment, offset);
274 u8 channel = device / 2;
275 u8 slave = device % 2;
277 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
278 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
279 u8 mode = GET_EBDA(ata.devices[device].mode);
281 // Reset count of transferred data
282 SET_EBDA(ata.trsfsectors,0);
283 SET_EBDA(ata.trsfbytes,0L);
285 u8 status = inb(iobase1 + ATA_CB_STAT);
286 if (status & ATA_CB_STAT_BSY)
289 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
291 // sector will be 0 only on lba access. Convert to lba-chs
293 if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
294 outb(0x00, iobase1 + ATA_CB_FR);
295 outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
296 outb(lba >> 24, iobase1 + ATA_CB_SN);
297 outb(0, iobase1 + ATA_CB_CL);
298 outb(0, iobase1 + ATA_CB_CH);
300 count &= (1UL << 8) - 1;
301 lba &= (1UL << 24) - 1;
303 sector = (u16) (lba & 0x000000ffL);
304 cylinder = (u16) ((lba>>8) & 0x0000ffffL);
305 head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
308 outb(0x00, iobase1 + ATA_CB_FR);
309 outb(count, iobase1 + ATA_CB_SC);
310 outb(sector, iobase1 + ATA_CB_SN);
311 outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
312 outb(cylinder >> 8, iobase1 + ATA_CB_CH);
313 outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
314 , iobase1 + ATA_CB_DH);
315 outb(command, iobase1 + ATA_CB_CMD);
317 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
318 status = inb(iobase1 + ATA_CB_STAT);
320 if (status & ATA_CB_STAT_ERR) {
321 DEBUGF("ata_cmd_data_out : read error\n");
323 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
324 DEBUGF("ata_cmd_data_out : DRQ not set (status %02x)\n"
325 , (unsigned) status);
329 // FIXME : move seg/off translation here
335 if (offset > 0xf800) {
340 if (mode == ATA_MODE_PIO32)
341 outsl_seg(iobase1, segment, offset, 512 / 4);
343 outsw_seg(iobase1, segment, offset, 512 / 2);
347 SET_EBDA(ata.trsfsectors,current);
349 status = inb(iobase1 + ATA_CB_STAT);
351 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
352 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
353 != ATA_CB_STAT_RDY ) {
354 DEBUGF("ata_cmd_data_out : no sectors left (status %02x)\n"
355 , (unsigned) status);
360 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
362 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
363 DEBUGF("ata_cmd_data_out : more sectors left (status %02x)\n"
364 , (unsigned) status);
371 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
375 // ---------------------------------------------------------------------------
376 // ATA/ATAPI driver : execute a packet command
377 // ---------------------------------------------------------------------------
380 // 1 : error in parameters
385 ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
386 , u32 length, u8 inout, u16 bufseg, u16 bufoff)
388 DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d inout=%d"
390 , device, cmdlen, header, length, inout
393 u8 channel = device / 2;
394 u8 slave = device % 2;
396 // Data out is not supported yet
397 if (inout == ATA_DATA_OUT) {
398 BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
402 // The header length must be even
404 DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
408 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
409 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
410 u8 mode = GET_EBDA(ata.devices[device].mode);
418 // Reset count of transferred data
419 SET_EBDA(ata.trsfsectors,0);
420 SET_EBDA(ata.trsfbytes,0L);
422 u8 status = inb(iobase1 + ATA_CB_STAT);
423 if (status & ATA_CB_STAT_BSY)
426 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
427 outb(0x00, iobase1 + ATA_CB_FR);
428 outb(0x00, iobase1 + ATA_CB_SC);
429 outb(0x00, iobase1 + ATA_CB_SN);
430 outb(0xfff0 & 0x00ff, iobase1 + ATA_CB_CL);
431 outb(0xfff0 >> 8, iobase1 + ATA_CB_CH);
432 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
433 outb(ATA_CMD_PACKET, iobase1 + ATA_CB_CMD);
435 // Device should ok to receive command
436 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
437 status = inb(iobase1 + ATA_CB_STAT);
439 if (status & ATA_CB_STAT_ERR) {
440 DEBUGF("ata_cmd_packet : error, status is %02x\n", status);
442 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
443 DEBUGF("ata_cmd_packet : DRQ not set (status %02x)\n"
444 , (unsigned) status);
448 // Send command to device
451 outsw_seg(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
453 if (inout == ATA_DATA_NO) {
454 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
455 status = inb(iobase1 + ATA_CB_STAT);
459 if (loops == 0) {//first time through
460 status = inb(iobase2 + ATA_CB_ASTAT);
461 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
463 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
466 status = inb(iobase1 + ATA_CB_STAT);
467 inb(iobase1 + ATA_CB_SC);
469 // Check if command completed
470 if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
471 ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
474 if (status & ATA_CB_STAT_ERR) {
475 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
480 bufseg += (bufoff / 16);
483 // Get the byte count
484 u16 lcount = (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
485 + inb(iobase1 + ATA_CB_CL));
487 // adjust to read what we want
489 if (header > lcount) {
499 if (lcount > length) {
500 lafter=lcount-length;
511 DEBUGF("Trying to read %04x bytes (%04x %04x %04x) "
512 , lbefore+lcount+lafter, lbefore, lcount, lafter);
513 DEBUGF("to 0x%04x:0x%04x\n", bufseg, bufoff);
515 // If counts not dividable by 4, use 16bits mode
517 if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
518 if (lcount & 0x03) lmode=ATA_MODE_PIO16;
519 if (lafter & 0x03) lmode=ATA_MODE_PIO16;
521 // adds an extra byte if count are odd. before is always even
524 if ((lafter > 0) && (lafter & 0x01)) {
529 if (lmode == ATA_MODE_PIO32) {
530 lcount>>=2; lbefore>>=2; lafter>>=2;
532 lcount>>=1; lbefore>>=1; lafter>>=1;
536 for (i=0; i<lbefore; i++)
537 if (lmode == ATA_MODE_PIO32)
542 if (lmode == ATA_MODE_PIO32)
543 insl_seg(iobase1, bufseg, bufoff, lcount);
545 insw_seg(iobase1, bufseg, bufoff, lcount);
547 for (i=0; i<lafter; i++)
548 if (lmode == ATA_MODE_PIO32)
553 // Compute new buffer address
556 // Save transferred bytes count
557 SET_EBDA(ata.trsfsectors, loops);
561 // Final check, device must be ready
562 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
563 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
564 != ATA_CB_STAT_RDY ) {
565 DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
566 , (unsigned) status);
571 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
576 cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
578 u16 sectors = (count + 2048 - 1) / 2048;
581 memset(atacmd, 0, sizeof(atacmd));
582 atacmd[0]=0x28; // READ command
583 atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
584 atacmd[8]=(sectors & 0x00ff); // Sectors
585 atacmd[2]=(lba & 0xff000000) >> 24; // LBA
586 atacmd[3]=(lba & 0x00ff0000) >> 16;
587 atacmd[4]=(lba & 0x0000ff00) >> 8;
588 atacmd[5]=(lba & 0x000000ff);
590 return ata_cmd_packet(device, atacmd, sizeof(atacmd)
591 , skip, count, ATA_DATA_IN
595 // ---------------------------------------------------------------------------
596 // ATA/ATAPI driver : device detection
597 // ---------------------------------------------------------------------------
602 u8 hdcount, cdcount, device, type;
604 memset(buffer, 0, sizeof(buffer));
606 #if CONFIG_MAX_ATA_INTERFACES > 0
607 SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
608 SET_EBDA(ata.channels[0].iobase1,0x1f0);
609 SET_EBDA(ata.channels[0].iobase2,0x3f0);
610 SET_EBDA(ata.channels[0].irq,14);
612 #if CONFIG_MAX_ATA_INTERFACES > 1
613 SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
614 SET_EBDA(ata.channels[1].iobase1,0x170);
615 SET_EBDA(ata.channels[1].iobase2,0x370);
616 SET_EBDA(ata.channels[1].irq,15);
618 #if CONFIG_MAX_ATA_INTERFACES > 2
619 SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
620 SET_EBDA(ata.channels[2].iobase1,0x1e8);
621 SET_EBDA(ata.channels[2].iobase2,0x3e0);
622 SET_EBDA(ata.channels[2].irq,12);
624 #if CONFIG_MAX_ATA_INTERFACES > 3
625 SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
626 SET_EBDA(ata.channels[3].iobase1,0x168);
627 SET_EBDA(ata.channels[3].iobase2,0x360);
628 SET_EBDA(ata.channels[3].irq,11);
630 #if CONFIG_MAX_ATA_INTERFACES > 4
631 #error Please fill the ATA interface informations
637 for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
638 u16 iobase1, iobase2;
639 u8 channel, slave, shift;
640 u8 sc, sn, cl, ch, st;
642 channel = device / 2;
645 iobase1 =GET_EBDA(ata.channels[channel].iobase1);
646 iobase2 =GET_EBDA(ata.channels[channel].iobase2);
648 // Disable interrupts
649 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
652 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
653 outb(0x55, iobase1+ATA_CB_SC);
654 outb(0xaa, iobase1+ATA_CB_SN);
655 outb(0xaa, iobase1+ATA_CB_SC);
656 outb(0x55, iobase1+ATA_CB_SN);
657 outb(0x55, iobase1+ATA_CB_SC);
658 outb(0xaa, iobase1+ATA_CB_SN);
660 // If we found something
661 sc = inb(iobase1+ATA_CB_SC);
662 sn = inb(iobase1+ATA_CB_SN);
664 if ( (sc == 0x55) && (sn == 0xaa) ) {
665 SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
670 // check for ATA or ATAPI
671 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
672 sc = inb(iobase1+ATA_CB_SC);
673 sn = inb(iobase1+ATA_CB_SN);
674 if ((sc==0x01) && (sn==0x01)) {
675 cl = inb(iobase1+ATA_CB_CL);
676 ch = inb(iobase1+ATA_CB_CH);
677 st = inb(iobase1+ATA_CB_STAT);
679 if ((cl==0x14) && (ch==0xeb)) {
680 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
681 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
682 SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
683 } else if ((cl==0xff) && (ch==0xff)) {
684 SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
689 type=GET_EBDA(ata.devices[device].type);
691 // Now we send a IDENTIFY command to ATA device
692 if(type == ATA_TYPE_ATA) {
694 u16 cylinders, heads, spt, blksize;
695 u8 translation, removable, mode;
697 //Temporary values to do the transfer
698 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
699 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
701 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
703 , GET_SEG(SS), (u32)buffer);
705 BX_PANIC("ata-detect: Failed to detect ATA device\n");
707 removable = (buffer[0] & 0x80) ? 1 : 0;
708 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
709 blksize = *(u16*)&buffer[10];
711 cylinders = *(u16*)&buffer[1*2]; // word 1
712 heads = *(u16*)&buffer[3*2]; // word 3
713 spt = *(u16*)&buffer[6*2]; // word 6
715 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
717 SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
718 SET_EBDA(ata.devices[device].removable, removable);
719 SET_EBDA(ata.devices[device].mode, mode);
720 SET_EBDA(ata.devices[device].blksize, blksize);
721 SET_EBDA(ata.devices[device].pchs.heads, heads);
722 SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
723 SET_EBDA(ata.devices[device].pchs.spt, spt);
724 SET_EBDA(ata.devices[device].sectors, sectors);
725 BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
727 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
728 for (shift=device%4; shift>0; shift--)
732 SET_EBDA(ata.devices[device].translation, translation);
734 switch (translation) {
735 case ATA_TRANSLATION_NONE:
738 case ATA_TRANSLATION_LBA:
741 case ATA_TRANSLATION_LARGE:
744 case ATA_TRANSLATION_RECHS:
748 switch (translation) {
749 case ATA_TRANSLATION_NONE:
751 case ATA_TRANSLATION_LBA:
754 heads = sectors / 1024;
755 if (heads>128) heads = 255;
756 else if (heads>64) heads = 128;
757 else if (heads>32) heads = 64;
758 else if (heads>16) heads = 32;
760 cylinders = sectors / heads;
762 case ATA_TRANSLATION_RECHS:
763 // Take care not to overflow
765 if(cylinders>61439) cylinders=61439;
767 cylinders = (u16)((u32)(cylinders)*16/15);
769 // then go through the large bitshift process
770 case ATA_TRANSLATION_LARGE:
771 while(cylinders > 1024) {
775 // If we max out the head count
776 if (heads > 127) break;
780 // clip to 1024 cylinders in lchs
781 if (cylinders > 1024)
783 BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
785 SET_EBDA(ata.devices[device].lchs.heads, heads);
786 SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
787 SET_EBDA(ata.devices[device].lchs.spt, spt);
790 SET_EBDA(ata.idmap[0][hdcount], device);
794 // Now we send a IDENTIFY command to ATAPI device
795 if(type == ATA_TYPE_ATAPI) {
797 u8 type, removable, mode;
800 //Temporary values to do the transfer
801 SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
802 SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
804 u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
806 , GET_SEG(SS), (u32)buffer);
808 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
810 type = buffer[1] & 0x1f;
811 removable = (buffer[0] & 0x80) ? 1 : 0;
812 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
815 SET_EBDA(ata.devices[device].device, type);
816 SET_EBDA(ata.devices[device].removable, removable);
817 SET_EBDA(ata.devices[device].mode, mode);
818 SET_EBDA(ata.devices[device].blksize, blksize);
821 SET_EBDA(ata.idmap[1][cdcount], device);
827 u8 c, i, version=0, model[41];
831 sizeinmb = GET_EBDA(ata.devices[device].sectors);
834 // Read ATA/ATAPI version
835 ataversion=((u16)(buffer[161])<<8) | buffer[160];
836 for(version=15;version>0;version--) {
837 if ((ataversion&(1<<version))!=0)
843 model[i*2] = buffer[(i*2)+54+1];
844 model[(i*2)+1] = buffer[(i*2)+54];
860 printf("ata%d %s: ",channel,slave?" slave":"master");
862 while ((c=model[i++]))
864 if (sizeinmb < (1UL<<16))
865 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
867 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
870 printf("ata%d %s: ",channel,slave?" slave":"master");
872 while ((c=model[i++]))
874 if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
875 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
877 printf(" ATAPI-%d Device\n",version);
879 case ATA_TYPE_UNKNOWN:
880 printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
885 // Store the devices counts
886 SET_EBDA(ata.hdcount, hdcount);
887 SET_EBDA(ata.cdcount, cdcount);
888 SET_BDA(disk_count, hdcount);
892 // FIXME : should use bios=cmos|auto|disable bits
893 // FIXME : should know about translation bits
894 // FIXME : move hard_drive_post here