Use LBA disk access methods always; don't use CHS methods.
[seabios.git] / src / ata.c
1 // Low level ATA disk access
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
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
13
14 #define TIMEOUT 0
15 #define BSY 1
16 #define NOT_BSY 2
17 #define NOT_BSY_DRQ 3
18 #define NOT_BSY_NOT_DRQ 4
19 #define NOT_BSY_RDY 5
20
21 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
22
23 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
24 #define DEBUGF(fmt, args...)
25
26 // XXX - lots of redundancy in this file.
27
28 static int
29 await_ide(u8 when_done, u16 base, u16 timeout)
30 {
31     u32 time=0, last=0;
32     // for the times you're supposed to throw one away
33     u16 status = inb(base + ATA_CB_STAT);
34     for (;;) {
35         status = inb(base+ATA_CB_STAT);
36         time++;
37         u8 result;
38         if (when_done == BSY)
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)
49             result = 0;
50
51         if (result)
52             return 0;
53         // mod 2048 each 16 ms
54         if (time>>16 != last) {
55             last = time >>16;
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);
59         }
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);
64             return -1;
65         }
66         if ((timeout == 0) || ((time>>11) > timeout))
67             break;
68     }
69     BX_INFO("IDE time out\n");
70     return -1;
71 }
72
73
74 // ---------------------------------------------------------------------------
75 // ATA/ATAPI driver : software reset
76 // ---------------------------------------------------------------------------
77 // ATA-3
78 // 8.2.1 Software reset - Device 0
79
80 void
81 ata_reset(u16 device)
82 {
83     u16 iobase1, iobase2;
84     u8  channel, slave, sn, sc;
85     u8  type;
86
87     channel = device / 2;
88     slave = device % 2;
89
90     iobase1 = GET_EBDA(ata.channels[channel].iobase1);
91     iobase2 = GET_EBDA(ata.channels[channel].iobase2);
92
93     // Reset
94
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);
97
98     // 8.2.1 (b) -- wait for BSY
99     await_ide(BSY, iobase1, 20);
100
101     // 8.2.1 (f) -- clear SRST
102     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
103
104     type=GET_EBDA(ata.devices[device].type);
105     if (type != ATA_TYPE_NONE) {
106
107         // 8.2.1 (g) -- check for sc==sn==0x01
108         // select device
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);
112
113         if ( (sc==0x01) && (sn==0x01) ) {
114             if (type == ATA_TYPE_ATA) //ATA
115                 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
116             else //ATAPI
117                 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
118         }
119
120         // 8.2.1 (h) -- wait for not BSY
121         await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
122     }
123
124     // Enable interrupts
125     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
126 }
127
128
129 // ---------------------------------------------------------------------------
130 // ATA/ATAPI driver : execute a data command
131 // ---------------------------------------------------------------------------
132       // returns
133       // 0 : no error
134       // 1 : BUSY bit set
135       // 2 : read error
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
141
142 static int
143 send_cmd(struct ata_pio_command *cmd)
144 {
145     u16 biosid = cmd->biosid;
146     u8 channel = biosid / 2;
147     u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
148     u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
149
150     u8 status = inb(iobase1 + ATA_CB_STAT);
151     if (status & ATA_CB_STAT_BSY)
152         return 1;
153
154     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
155     if (cmd->command & 0x04) {
156         outb(0x00, iobase1 + ATA_CB_FR);
157         outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
158         outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
159         outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
160         outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
161     }
162     outb(cmd->feature, iobase1 + ATA_CB_FR);
163     outb(cmd->sector_count, iobase1 + ATA_CB_SC);
164     outb(cmd->lba_low, iobase1 + ATA_CB_SN);
165     outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
166     outb(cmd->lba_high, iobase1 + ATA_CB_CH);
167     outb(cmd->device, iobase1 + ATA_CB_DH);
168     outb(cmd->command, iobase1 + ATA_CB_CMD);
169
170     await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
171
172     status = inb(iobase1 + ATA_CB_STAT);
173     if (status & ATA_CB_STAT_ERR) {
174         DEBUGF("send_cmd : read error\n");
175         return 2;
176     }
177     if (!(status & ATA_CB_STAT_DRQ)) {
178         DEBUGF("send_cmd : DRQ not set (status %02x)\n"
179                , (unsigned) status);
180         return 3;
181     }
182
183     return 0;
184 }
185
186 int
187 ata_transfer(struct ata_pio_command *cmd)
188 {
189     DEBUGF("ata_transfer id=%d cmd=%d lba=%d count=%d buf=%p\n"
190            , cmd->biosid, cmd->command
191            , (cmd->lba_high << 16) | (cmd->lba_mid << 8) | cmd->lba_low
192            , cmd->sector_count, cmd->far_buffer);
193
194     // Reset count of transferred data
195     SET_EBDA(ata.trsfsectors,0);
196     SET_EBDA(ata.trsfbytes,0L);
197
198     int ret = send_cmd(cmd);
199     if (ret)
200         return ret;
201
202     u16 biosid = cmd->biosid;
203     u8 channel  = biosid / 2;
204     u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
205     u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
206     u8 mode     = GET_EBDA(ata.devices[biosid].mode);
207     int iswrite = (cmd->command & ~0x40) == ATA_CMD_WRITE_SECTORS;
208     u8 current = 0;
209     u16 count = cmd->sector_count;
210     u8 status;
211     void *far_buffer = cmd->far_buffer;
212     for (;;) {
213         if (iswrite) {
214             // Write data to controller
215             DEBUGF("Write sector id=%d dest=%p\n", biosid, far_buffer);
216             if (mode == ATA_MODE_PIO32)
217                 outsl_far(iobase1, far_buffer, 512 / 4);
218             else
219                 outsw_far(iobase1, far_buffer, 512 / 2);
220         } else {
221             // Read data from controller
222             DEBUGF("Read sector id=%d dest=%p\n", biosid, far_buffer);
223             if (mode == ATA_MODE_PIO32)
224                 insl_far(iobase1, far_buffer, 512 / 4);
225             else
226                 insw_far(iobase1, far_buffer, 512 / 2);
227             await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
228         }
229         far_buffer += 512;
230
231         current++;
232         SET_EBDA(ata.trsfsectors,current);
233         count--;
234         status = inb(iobase1 + ATA_CB_STAT);
235         if (count == 0)
236             break;
237         status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
238                    | ATA_CB_STAT_ERR);
239         if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
240             DEBUGF("ata_transfer : more sectors left (status %02x)\n"
241                    , (unsigned) status);
242             return 5;
243         }
244     }
245
246     status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
247                | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
248     if (!iswrite)
249         status &= ~ATA_CB_STAT_DF;
250     if (status != ATA_CB_STAT_RDY ) {
251         DEBUGF("ata_transfer : no sectors left (status %02x)\n"
252                , (unsigned) status);
253         return 4;
254     }
255
256     // Enable interrupts
257     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
258     return 0;
259 }
260
261 // ---------------------------------------------------------------------------
262 // ATA/ATAPI driver : execute a packet command
263 // ---------------------------------------------------------------------------
264       // returns
265       // 0 : no error
266       // 1 : error in parameters
267       // 2 : BUSY bit set
268       // 3 : error
269       // 4 : not ready
270 int
271 ata_cmd_packet(u16 biosid, u8 *cmdbuf, u8 cmdlen
272                , u16 header, u32 length, void *far_buffer)
273 {
274     DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d buf=%p\n"
275            , biosid, cmdlen, header, length, far_buffer);
276
277     u8 channel = biosid / 2;
278     u8 slave = biosid % 2;
279
280     // The header length must be even
281     if (header & 1) {
282         DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
283         return 1;
284     }
285
286     u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
287     u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
288     u8 mode     = GET_EBDA(ata.devices[biosid].mode);
289
290     struct ata_pio_command cmd;
291     cmd.sector_count = 0;
292     cmd.feature = 0;
293     cmd.lba_low = 0;
294     cmd.lba_mid = 0xf0;
295     cmd.lba_high = 0xff;
296     cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
297     cmd.command = ATA_CMD_PACKET;
298
299     cmd.biosid = biosid;
300     int ret = send_cmd(&cmd);
301     if (ret)
302         return ret;
303
304     // Reset count of transferred data
305     SET_EBDA(ata.trsfsectors,0);
306     SET_EBDA(ata.trsfbytes,0L);
307
308     // Send command to device
309     outsw_far(iobase1, MAKE_32_PTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
310
311     u8 status;
312     u16 loops = 0;
313     for (;;) {
314         if (loops == 0) {//first time through
315             status = inb(iobase2 + ATA_CB_ASTAT);
316             await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
317         } else
318             await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
319         loops++;
320
321         status = inb(iobase1 + ATA_CB_STAT);
322         inb(iobase1 + ATA_CB_SC);
323
324         // Check if command completed
325         if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
326            ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
327             break;
328
329         if (status & ATA_CB_STAT_ERR) {
330             DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
331             return 3;
332         }
333
334         // Get the byte count
335         u16 lcount =  (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
336                        + inb(iobase1 + ATA_CB_CL));
337
338         // adjust to read what we want
339         u16 lbefore, lafter;
340         if (header > lcount) {
341             lbefore=lcount;
342             header-=lcount;
343             lcount=0;
344         } else {
345             lbefore=header;
346             header=0;
347             lcount-=lbefore;
348         }
349
350         if (lcount > length) {
351             lafter=lcount-length;
352             lcount=length;
353             length=0;
354         } else {
355             lafter=0;
356             length-=lcount;
357         }
358
359         // Save byte count
360         u16 count = lcount;
361
362         DEBUGF("Trying to read %04x bytes (%04x %04x %04x) to %p\n"
363                , lbefore+lcount+lafter, lbefore, lcount, lafter, far_buffer);
364
365         // If counts not dividable by 4, use 16bits mode
366         u8 lmode = mode;
367         if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
368         if (lcount  & 0x03) lmode=ATA_MODE_PIO16;
369         if (lafter  & 0x03) lmode=ATA_MODE_PIO16;
370
371         // adds an extra byte if count are odd. before is always even
372         if (lcount & 0x01) {
373             lcount+=1;
374             if ((lafter > 0) && (lafter & 0x01)) {
375                 lafter-=1;
376             }
377         }
378
379         if (lmode == ATA_MODE_PIO32) {
380             lcount>>=2; lbefore>>=2; lafter>>=2;
381         } else {
382             lcount>>=1; lbefore>>=1; lafter>>=1;
383         }
384
385         int i;
386         for (i=0; i<lbefore; i++)
387             if (lmode == ATA_MODE_PIO32)
388                 inl(iobase1);
389             else
390                 inw(iobase1);
391
392         if (lmode == ATA_MODE_PIO32)
393             insl_far(iobase1, far_buffer, lcount);
394         else
395             insw_far(iobase1, far_buffer, lcount);
396
397         for (i=0; i<lafter; i++)
398             if (lmode == ATA_MODE_PIO32)
399                 inl(iobase1);
400             else
401                 inw(iobase1);
402
403         // Compute new buffer address
404         far_buffer += count;
405
406         // Save transferred bytes count
407         SET_EBDA(ata.trsfsectors, loops);
408     }
409
410     // Final check, device must be ready
411     if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
412                     | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
413          != ATA_CB_STAT_RDY ) {
414         DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
415                , (unsigned) status);
416         return 4;
417     }
418
419     // Enable interrupts
420     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
421     return 0;
422 }
423
424 int
425 cdrom_read(u16 biosid, u32 lba, u32 count, void *far_buffer, u16 skip)
426 {
427     u16 sectors = (count + 2048 - 1) / 2048;
428
429     u8 atacmd[12];
430     memset(atacmd, 0, sizeof(atacmd));
431     atacmd[0]=0x28;                      // READ command
432     atacmd[7]=(sectors & 0xff00) >> 8;   // Sectors
433     atacmd[8]=(sectors & 0x00ff);        // Sectors
434     atacmd[2]=(lba & 0xff000000) >> 24;  // LBA
435     atacmd[3]=(lba & 0x00ff0000) >> 16;
436     atacmd[4]=(lba & 0x0000ff00) >> 8;
437     atacmd[5]=(lba & 0x000000ff);
438
439     return ata_cmd_packet(biosid, atacmd, sizeof(atacmd)
440                           , skip, count, far_buffer);
441 }
442
443 // ---------------------------------------------------------------------------
444 // ATA/ATAPI driver : device detection
445 // ---------------------------------------------------------------------------
446
447 void
448 ata_detect()
449 {
450     u8  hdcount, cdcount, device, type;
451     u8  buffer[0x0200];
452     memset(buffer, 0, sizeof(buffer));
453
454 #if CONFIG_MAX_ATA_INTERFACES > 0
455     SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
456     SET_EBDA(ata.channels[0].iobase1,0x1f0);
457     SET_EBDA(ata.channels[0].iobase2,0x3f0);
458     SET_EBDA(ata.channels[0].irq,14);
459 #endif
460 #if CONFIG_MAX_ATA_INTERFACES > 1
461     SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
462     SET_EBDA(ata.channels[1].iobase1,0x170);
463     SET_EBDA(ata.channels[1].iobase2,0x370);
464     SET_EBDA(ata.channels[1].irq,15);
465 #endif
466 #if CONFIG_MAX_ATA_INTERFACES > 2
467     SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
468     SET_EBDA(ata.channels[2].iobase1,0x1e8);
469     SET_EBDA(ata.channels[2].iobase2,0x3e0);
470     SET_EBDA(ata.channels[2].irq,12);
471 #endif
472 #if CONFIG_MAX_ATA_INTERFACES > 3
473     SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
474     SET_EBDA(ata.channels[3].iobase1,0x168);
475     SET_EBDA(ata.channels[3].iobase2,0x360);
476     SET_EBDA(ata.channels[3].irq,11);
477 #endif
478 #if CONFIG_MAX_ATA_INTERFACES > 4
479 #error Please fill the ATA interface informations
480 #endif
481
482     // Device detection
483     hdcount=cdcount=0;
484
485     for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
486         u16 iobase1, iobase2;
487         u8  channel, slave, shift;
488         u8  sc, sn, cl, ch, st;
489
490         channel = device / 2;
491         slave = device % 2;
492
493         iobase1 =GET_EBDA(ata.channels[channel].iobase1);
494         iobase2 =GET_EBDA(ata.channels[channel].iobase2);
495
496         // Disable interrupts
497         outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
498
499         // Look for device
500         outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
501         outb(0x55, iobase1+ATA_CB_SC);
502         outb(0xaa, iobase1+ATA_CB_SN);
503         outb(0xaa, iobase1+ATA_CB_SC);
504         outb(0x55, iobase1+ATA_CB_SN);
505         outb(0x55, iobase1+ATA_CB_SC);
506         outb(0xaa, iobase1+ATA_CB_SN);
507
508         // If we found something
509         sc = inb(iobase1+ATA_CB_SC);
510         sn = inb(iobase1+ATA_CB_SN);
511
512         if ( (sc == 0x55) && (sn == 0xaa) ) {
513             SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
514
515             // reset the channel
516             ata_reset(device);
517
518             // check for ATA or ATAPI
519             outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
520             sc = inb(iobase1+ATA_CB_SC);
521             sn = inb(iobase1+ATA_CB_SN);
522             if ((sc==0x01) && (sn==0x01)) {
523                 cl = inb(iobase1+ATA_CB_CL);
524                 ch = inb(iobase1+ATA_CB_CH);
525                 st = inb(iobase1+ATA_CB_STAT);
526
527                 if ((cl==0x14) && (ch==0xeb)) {
528                     SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
529                 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
530                     SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
531                 } else if ((cl==0xff) && (ch==0xff)) {
532                     SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
533                 }
534             }
535         }
536
537         type=GET_EBDA(ata.devices[device].type);
538
539         // Now we send a IDENTIFY command to ATA device
540         if(type == ATA_TYPE_ATA) {
541             u32 sectors;
542             u16 cylinders, heads, spt, blksize;
543             u8  translation, removable, mode;
544
545             //Temporary values to do the transfer
546             SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
547             SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
548
549             u16 ret = ata_cmd_data(device, ATA_CMD_IDENTIFY_DEVICE
550                                    , 1, 1
551                                    , MAKE_32_PTR(GET_SEG(SS), (u32)buffer));
552             if (ret)
553                 BX_PANIC("ata-detect: Failed to detect ATA device\n");
554
555             removable = (buffer[0] & 0x80) ? 1 : 0;
556             mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
557             blksize   = *(u16*)&buffer[10];
558
559             cylinders = *(u16*)&buffer[1*2]; // word 1
560             heads     = *(u16*)&buffer[3*2]; // word 3
561             spt       = *(u16*)&buffer[6*2]; // word 6
562
563             sectors   = *(u32*)&buffer[60*2]; // word 60 and word 61
564
565             SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
566             SET_EBDA(ata.devices[device].removable, removable);
567             SET_EBDA(ata.devices[device].mode, mode);
568             SET_EBDA(ata.devices[device].blksize, blksize);
569             SET_EBDA(ata.devices[device].pchs.heads, heads);
570             SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
571             SET_EBDA(ata.devices[device].pchs.spt, spt);
572             SET_EBDA(ata.devices[device].sectors, sectors);
573             BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
574                     , channel, slave,cylinders, heads, spt);
575
576             translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
577             for (shift=device%4; shift>0; shift--)
578                 translation >>= 2;
579             translation &= 0x03;
580
581             SET_EBDA(ata.devices[device].translation, translation);
582
583             switch (translation) {
584             case ATA_TRANSLATION_NONE:
585                 BX_INFO("none");
586                 break;
587             case ATA_TRANSLATION_LBA:
588                 BX_INFO("lba");
589                 break;
590             case ATA_TRANSLATION_LARGE:
591                 BX_INFO("large");
592                 break;
593             case ATA_TRANSLATION_RECHS:
594                 BX_INFO("r-echs");
595                 break;
596             }
597             switch (translation) {
598             case ATA_TRANSLATION_NONE:
599                 break;
600             case ATA_TRANSLATION_LBA:
601                 spt = 63;
602                 sectors /= 63;
603                 heads = sectors / 1024;
604                 if (heads>128) heads = 255;
605                 else if (heads>64) heads = 128;
606                 else if (heads>32) heads = 64;
607                 else if (heads>16) heads = 32;
608                 else heads=16;
609                 cylinders = sectors / heads;
610                 break;
611             case ATA_TRANSLATION_RECHS:
612                 // Take care not to overflow
613                 if (heads==16) {
614                     if(cylinders>61439) cylinders=61439;
615                     heads=15;
616                     cylinders = (u16)((u32)(cylinders)*16/15);
617                 }
618                 // then go through the large bitshift process
619             case ATA_TRANSLATION_LARGE:
620                 while(cylinders > 1024) {
621                     cylinders >>= 1;
622                     heads <<= 1;
623
624                     // If we max out the head count
625                     if (heads > 127) break;
626                 }
627                 break;
628             }
629             // clip to 1024 cylinders in lchs
630             if (cylinders > 1024)
631                 cylinders=1024;
632             BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
633
634             SET_EBDA(ata.devices[device].lchs.heads, heads);
635             SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
636             SET_EBDA(ata.devices[device].lchs.spt, spt);
637
638             // fill hdidmap
639             SET_EBDA(ata.idmap[0][hdcount], device);
640             hdcount++;
641         }
642
643         // Now we send a IDENTIFY command to ATAPI device
644         if(type == ATA_TYPE_ATAPI) {
645
646             u8  type, removable, mode;
647             u16 blksize;
648
649             //Temporary values to do the transfer
650             SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
651             SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
652
653             u16 ret = ata_cmd_data(device, ATA_CMD_IDENTIFY_DEVICE_PACKET
654                                    , 1, 1
655                                    , MAKE_32_PTR(GET_SEG(SS), (u32)buffer));
656             if (ret != 0)
657                 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
658
659             type      = buffer[1] & 0x1f;
660             removable = (buffer[0] & 0x80) ? 1 : 0;
661             mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
662             blksize   = 2048;
663
664             SET_EBDA(ata.devices[device].device, type);
665             SET_EBDA(ata.devices[device].removable, removable);
666             SET_EBDA(ata.devices[device].mode, mode);
667             SET_EBDA(ata.devices[device].blksize, blksize);
668
669             // fill cdidmap
670             SET_EBDA(ata.idmap[1][cdcount], device);
671             cdcount++;
672         }
673
674         u32 sizeinmb = 0;
675         u16 ataversion;
676         u8  c, i, version=0, model[41];
677
678         switch (type) {
679         case ATA_TYPE_ATA:
680             sizeinmb = GET_EBDA(ata.devices[device].sectors);
681             sizeinmb >>= 11;
682         case ATA_TYPE_ATAPI:
683             // Read ATA/ATAPI version
684             ataversion=((u16)(buffer[161])<<8) | buffer[160];
685             for(version=15;version>0;version--) {
686                 if ((ataversion&(1<<version))!=0)
687                     break;
688             }
689
690             // Read model name
691             for (i=0;i<20;i++) {
692                 model[i*2] = buffer[(i*2)+54+1];
693                 model[(i*2)+1] = buffer[(i*2)+54];
694             }
695
696             // Reformat
697             model[40] = 0x00;
698             for (i=39;i>0;i--) {
699                 if (model[i]==0x20)
700                     model[i] = 0x00;
701                 else
702                     break;
703             }
704             break;
705         }
706
707         switch (type) {
708         case ATA_TYPE_ATA:
709             printf("ata%d %s: ",channel,slave?" slave":"master");
710             i=0;
711             while ((c=model[i++]))
712                 printf("%c",c);
713             if (sizeinmb < (1UL<<16))
714                 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
715             else
716                 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
717             break;
718         case ATA_TYPE_ATAPI:
719             printf("ata%d %s: ",channel,slave?" slave":"master");
720             i=0;
721             while ((c=model[i++]))
722                 printf("%c",c);
723             if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
724                 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
725             else
726                 printf(" ATAPI-%d Device\n",version);
727             break;
728         case ATA_TYPE_UNKNOWN:
729             printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
730             break;
731         }
732     }
733
734     // Store the devices counts
735     SET_EBDA(ata.hdcount, hdcount);
736     SET_EBDA(ata.cdcount, cdcount);
737     SET_BDA(disk_count, hdcount);
738
739     printf("\n");
740
741     // FIXME : should use bios=cmos|auto|disable bits
742     // FIXME : should know about translation bits
743     // FIXME : move hard_drive_post here
744 }