710bd9eeff355108afcc0554c512e23d3461d86e
[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 BX_DEBUG_ATA BX_INFO
24
25 // XXX - lots of redundancy in this file.
26
27 static int
28 await_ide(u8 when_done, u16 base, u16 timeout)
29 {
30     u32 time=0,last=0;
31     // for the times you're supposed to throw one away
32     u16 status = inb(base + ATA_CB_STAT);
33     for (;;) {
34         status = inb(base+ATA_CB_STAT);
35         time++;
36         u8 result;
37         if (when_done == BSY)
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)
48             result = 0;
49
50         if (result)
51             return 0;
52         // mod 2048 each 16 ms
53         if (time>>16 != last) {
54             last = time >>16;
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);
56         }
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);
59             return -1;
60         }
61         if ((timeout == 0) || ((time>>11) > timeout))
62             break;
63     }
64     BX_INFO("IDE time out\n");
65     return -1;
66 }
67
68
69 // ---------------------------------------------------------------------------
70 // ATA/ATAPI driver : software reset
71 // ---------------------------------------------------------------------------
72 // ATA-3
73 // 8.2.1 Software reset - Device 0
74
75 void
76 ata_reset(u16 device)
77 {
78     u16 iobase1, iobase2;
79     u8  channel, slave, sn, sc;
80     u8  type;
81
82     channel = device / 2;
83     slave = device % 2;
84
85     iobase1 = GET_EBDA(ata.channels[channel].iobase1);
86     iobase2 = GET_EBDA(ata.channels[channel].iobase2);
87
88     // Reset
89
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);
92
93     // 8.2.1 (b) -- wait for BSY
94     await_ide(BSY, iobase1, 20);
95
96     // 8.2.1 (f) -- clear SRST
97     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
98
99     type=GET_EBDA(ata.devices[device].type);
100     if (type != ATA_TYPE_NONE) {
101
102         // 8.2.1 (g) -- check for sc==sn==0x01
103         // select device
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);
107
108         if ( (sc==0x01) && (sn==0x01) ) {
109             if (type == ATA_TYPE_ATA) //ATA
110                 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
111             else //ATAPI
112                 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
113         }
114
115         // 8.2.1 (h) -- wait for not BSY
116         await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
117     }
118
119     // Enable interrupts
120     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
121 }
122
123 static void
124 insw(u16 port, u16 segment, u16 offset, u16 count)
125 {
126     u16 i;
127     for (i=0; i<count; i++) {
128         u16 d = inw(port);
129         SET_FARVAR(segment, *(u16*)(offset + 2*i), d);
130     }
131 }
132
133 static void
134 insl(u16 port, u16 segment, u16 offset, u16 count)
135 {
136     u16 i;
137     for (i=0; i<count; i++) {
138         u32 d = inl(port);
139         SET_FARVAR(segment, *(u32*)(offset + 4*i), d);
140     }
141 }
142
143 static void
144 outsw(u16 port, u16 segment, u16 offset, u16 count)
145 {
146     u16 i;
147     for (i=0; i<count; i++) {
148         u16 d = GET_FARVAR(segment, *(u16*)(offset + 2*i));
149         outw(d, port);
150     }
151 }
152
153 static void
154 outsl(u16 port, u16 segment, u16 offset, u16 count)
155 {
156     u16 i;
157     for (i=0; i<count; i++) {
158         u32 d = GET_FARVAR(segment, *(u32*)(offset + 4*i));
159         outl(d, port);
160     }
161 }
162
163
164 // ---------------------------------------------------------------------------
165 // ATA/ATAPI driver : execute a data-in command
166 // ---------------------------------------------------------------------------
167       // returns
168       // 0 : no error
169       // 1 : BUSY bit set
170       // 2 : read error
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
176 u16
177 ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
178                 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
179 {
180     u16 iobase1, iobase2, blksize;
181     u8  channel, slave;
182     u8  status, current, mode;
183
184     channel = device / 2;
185     slave   = device % 2;
186
187     iobase1 = GET_EBDA(ata.channels[channel].iobase1);
188     iobase2 = GET_EBDA(ata.channels[channel].iobase2);
189     mode    = GET_EBDA(ata.devices[device].mode);
190     blksize = 0x200;
191     if (mode == ATA_MODE_PIO32)
192         blksize>>=2;
193     else
194         blksize>>=1;
195
196     // Reset count of transferred data
197     SET_EBDA(ata.trsfsectors,0);
198     SET_EBDA(ata.trsfbytes,0L);
199     current = 0;
200
201     status = inb(iobase1 + ATA_CB_STAT);
202     if (status & ATA_CB_STAT_BSY)
203         return 1;
204
205     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
206
207     // sector will be 0 only on lba access. Convert to lba-chs
208     if (sector == 0) {
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);
215             command |= 0x04;
216             count &= (1UL << 8) - 1;
217             lba &= (1UL << 24) - 1;
218         }
219         sector = (u16) (lba & 0x000000ffL);
220         cylinder = (u16) ((lba>>8) & 0x0000ffffL);
221         head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
222     }
223
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);
232
233     await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
234     status = inb(iobase1 + ATA_CB_STAT);
235
236     if (status & ATA_CB_STAT_ERR) {
237         BX_DEBUG_ATA("ata_cmd_data_in : read error\n");
238         return 2;
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);
242         return 3;
243     }
244
245     // FIXME : move seg/off translation here
246
247     irq_enable();
248
249     while (1) {
250
251         if (offset > 0xf800) {
252             offset -= 0x800;
253             segment += 0x80;
254         }
255
256         if (mode == ATA_MODE_PIO32)
257             insl(iobase1, segment, offset, blksize);
258         else
259             insw(iobase1, segment, offset, blksize);
260
261         current++;
262         SET_EBDA(ata.trsfsectors,current);
263         count--;
264         await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
265         status = inb(iobase1 + ATA_CB_STAT);
266         if (count == 0) {
267             if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
268                             | ATA_CB_STAT_ERR) )
269                  != ATA_CB_STAT_RDY ) {
270                 BX_DEBUG_ATA("ata_cmd_data_in : no sectors left (status %02x)\n"
271                              , (unsigned) status);
272                 return 4;
273             }
274             break;
275         }
276         else {
277             if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
278                             | ATA_CB_STAT_ERR) )
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);
282                 return 5;
283             }
284             continue;
285         }
286     }
287     // Enable interrupts
288     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
289     return 0;
290 }
291
292 // ---------------------------------------------------------------------------
293 // ATA/ATAPI driver : execute a data-out command
294 // ---------------------------------------------------------------------------
295       // returns
296       // 0 : no error
297       // 1 : BUSY bit set
298       // 2 : read error
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
304 u16
305 ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
306                  , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
307 {
308     u16 iobase1, iobase2, blksize;
309     u8  channel, slave;
310     u8  status, current, mode;
311
312     channel = device / 2;
313     slave   = device % 2;
314
315     iobase1 = GET_EBDA(ata.channels[channel].iobase1);
316     iobase2 = GET_EBDA(ata.channels[channel].iobase2);
317     mode    = GET_EBDA(ata.devices[device].mode);
318     blksize = 0x200;
319     if (mode == ATA_MODE_PIO32)
320         blksize>>=2;
321     else
322         blksize>>=1;
323
324     // Reset count of transferred data
325     SET_EBDA(ata.trsfsectors,0);
326     SET_EBDA(ata.trsfbytes,0L);
327     current = 0;
328
329     status = inb(iobase1 + ATA_CB_STAT);
330     if (status & ATA_CB_STAT_BSY)
331         return 1;
332
333     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
334
335     // sector will be 0 only on lba access. Convert to lba-chs
336     if (sector == 0) {
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);
343             command |= 0x04;
344             count &= (1UL << 8) - 1;
345             lba &= (1UL << 24) - 1;
346         }
347         sector = (u16) (lba & 0x000000ffL);
348         cylinder = (u16) ((lba>>8) & 0x0000ffffL);
349         head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
350     }
351
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);
360
361     await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
362     status = inb(iobase1 + ATA_CB_STAT);
363
364     if (status & ATA_CB_STAT_ERR) {
365         BX_DEBUG_ATA("ata_cmd_data_out : read error\n");
366         return 2;
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);
370         return 3;
371     }
372
373     // FIXME : move seg/off translation here
374
375     irq_enable();
376
377     while (1) {
378
379         if (offset > 0xf800) {
380             offset -= 0x800;
381             segment += 0x80;
382         }
383
384         if (mode == ATA_MODE_PIO32)
385             outsl(iobase1, segment, offset, blksize);
386         else
387             outsw(iobase1, segment, offset, blksize);
388
389         current++;
390         SET_EBDA(ata.trsfsectors,current);
391         count--;
392         status = inb(iobase1 + ATA_CB_STAT);
393         if (count == 0) {
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);
399                 return 6;
400             }
401             break;
402         } else {
403             if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
404                             | ATA_CB_STAT_ERR) )
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);
408                 return 7;
409             }
410             continue;
411         }
412     }
413     // Enable interrupts
414     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
415     return 0;
416 }
417
418 // ---------------------------------------------------------------------------
419 // ATA/ATAPI driver : execute a packet command
420 // ---------------------------------------------------------------------------
421       // returns
422       // 0 : no error
423       // 1 : error in parameters
424       // 2 : BUSY bit set
425       // 3 : error
426       // 4 : not ready
427 u16
428 ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
429                , u32 length, u8 inout, u16 bufseg, u16 bufoff)
430 {
431     u16 iobase1, iobase2;
432     u16 lcount, lbefore, lafter, count;
433     u8  channel, slave;
434     u8  status, mode, lmode;
435     u32 transfer;
436
437     channel = device / 2;
438     slave = device % 2;
439
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");
443         return 1;
444     }
445
446     // The header length must be even
447     if (header & 1) {
448         BX_DEBUG_ATA("ata_cmd_packet : header must be even (%04x)\n",header);
449         return 1;
450     }
451
452     iobase1 = GET_EBDA(ata.channels[channel].iobase1);
453     iobase2 = GET_EBDA(ata.channels[channel].iobase2);
454     mode    = GET_EBDA(ata.devices[device].mode);
455     transfer= 0L;
456
457     if (cmdlen < 12)
458         cmdlen=12;
459     if (cmdlen > 12)
460         cmdlen=16;
461     cmdlen>>=1;
462
463     // Reset count of transferred data
464     SET_EBDA(ata.trsfsectors,0);
465     SET_EBDA(ata.trsfbytes,0L);
466
467     status = inb(iobase1 + ATA_CB_STAT);
468     if (status & ATA_CB_STAT_BSY)
469         return 2;
470
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);
479
480     // Device should ok to receive command
481     await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
482     status = inb(iobase1 + ATA_CB_STAT);
483
484     if (status & ATA_CB_STAT_ERR) {
485         BX_DEBUG_ATA("ata_cmd_packet : error, status is %02x\n",status);
486         return 3;
487     } else if ( !(status & ATA_CB_STAT_DRQ) ) {
488         BX_DEBUG_ATA("ata_cmd_packet : DRQ not set (status %02x)\n"
489                      , (unsigned) status);
490         return 4;
491     }
492
493     // Send command to device
494     irq_enable();
495
496     outsw(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
497
498     if (inout == ATA_DATA_NO) {
499         await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
500         status = inb(iobase1 + ATA_CB_STAT);
501     } else {
502         u16 loops = 0;
503         u8 sc;
504         while (1) {
505
506             if (loops == 0) {//first time through
507                 status = inb(iobase2 + ATA_CB_ASTAT);
508                 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
509             } else
510                 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
511             loops++;
512
513             status = inb(iobase1 + ATA_CB_STAT);
514             sc = inb(iobase1 + ATA_CB_SC);
515
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))
519                 break;
520
521             if (status & ATA_CB_STAT_ERR) {
522                 BX_DEBUG_ATA("ata_cmd_packet : error (status %02x)\n",status);
523                 return 3;
524             }
525
526             // Normalize address
527             bufseg += (bufoff / 16);
528             bufoff %= 16;
529
530             // Get the byte count
531             lcount =  ((u16)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
532
533             // adjust to read what we want
534             if (header > lcount) {
535                 lbefore=lcount;
536                 header-=lcount;
537                 lcount=0;
538             } else {
539                 lbefore=header;
540                 header=0;
541                 lcount-=lbefore;
542             }
543
544             if (lcount > length) {
545                 lafter=lcount-length;
546                 lcount=length;
547                 length=0;
548             } else {
549                 lafter=0;
550                 length-=lcount;
551             }
552
553             // Save byte count
554             count = lcount;
555
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);
559
560             // If counts not dividable by 4, use 16bits mode
561             lmode = 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;
565
566             // adds an extra byte if count are odd. before is always even
567             if (lcount & 0x01) {
568                 lcount+=1;
569                 if ((lafter > 0) && (lafter & 0x01)) {
570                     lafter-=1;
571                 }
572             }
573
574             if (lmode == ATA_MODE_PIO32) {
575                 lcount>>=2; lbefore>>=2; lafter>>=2;
576             } else {
577                 lcount>>=1; lbefore>>=1; lafter>>=1;
578             }
579
580             int i;
581             for (i=0; i<lbefore; i++)
582                 if (lmode == ATA_MODE_PIO32)
583                     inl(iobase1);
584                 else
585                     inw(iobase1);
586
587             if (lmode == ATA_MODE_PIO32)
588                 insl(iobase1, bufseg, bufoff, lcount);
589             else
590                 insw(iobase1, bufseg, bufoff, lcount);
591
592             for (i=0; i<lafter; i++)
593                 if (lmode == ATA_MODE_PIO32)
594                     inl(iobase1);
595                 else
596                     inw(iobase1);
597
598             // Compute new buffer address
599             bufoff += count;
600
601             // Save transferred bytes count
602             transfer += count;
603             SET_EBDA(ata.trsfbytes,transfer);
604         }
605     }
606
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);
613         return 4;
614     }
615
616     // Enable interrupts
617     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
618     return 0;
619 }
620
621 u16
622 cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
623 {
624     u16 sectors = (count + 2048 - 1) / 2048;
625
626     u8 atacmd[12];
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);
635
636     return ata_cmd_packet(device, atacmd, sizeof(atacmd)
637                           , skip, count, ATA_DATA_IN
638                           , segment, offset);
639 }
640
641 // ---------------------------------------------------------------------------
642 // ATA/ATAPI driver : device detection
643 // ---------------------------------------------------------------------------
644
645 void
646 ata_detect()
647 {
648     u8  hdcount, cdcount, device, type;
649     u8  buffer[0x0200];
650     memset(buffer, 0, sizeof(buffer));
651
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);
657 #endif
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);
663 #endif
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);
669 #endif
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);
675 #endif
676 #if CONFIG_MAX_ATA_INTERFACES > 4
677 #error Please fill the ATA interface informations
678 #endif
679
680     // Device detection
681     hdcount=cdcount=0;
682
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;
687
688         channel = device / 2;
689         slave = device % 2;
690
691         iobase1 =GET_EBDA(ata.channels[channel].iobase1);
692         iobase2 =GET_EBDA(ata.channels[channel].iobase2);
693
694         // Disable interrupts
695         outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
696
697         // Look for device
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);
705
706         // If we found something
707         sc = inb(iobase1+ATA_CB_SC);
708         sn = inb(iobase1+ATA_CB_SN);
709
710         if ( (sc == 0x55) && (sn == 0xaa) ) {
711             SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
712
713             // reset the channel
714             ata_reset(device);
715
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);
724
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);
731                 }
732             }
733         }
734
735         type=GET_EBDA(ata.devices[device].type);
736
737         // Now we send a IDENTIFY command to ATA device
738         if(type == ATA_TYPE_ATA) {
739             u32 sectors;
740             u16 cylinders, heads, spt, blksize;
741             u8  translation, removable, mode;
742
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);
746
747             u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
748                                       , 1, 0, 0, 0, 0L
749                                       , GET_SEG(SS), (u32)buffer);
750             if (ret)
751                 BX_PANIC("ata-detect: Failed to detect ATA device\n");
752
753             removable = (buffer[0] & 0x80) ? 1 : 0;
754             mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
755             blksize   = *(u16*)&buffer[10];
756
757             cylinders = *(u16*)&buffer[1*2]; // word 1
758             heads     = *(u16*)&buffer[3*2]; // word 3
759             spt       = *(u16*)&buffer[6*2]; // word 6
760
761             sectors   = *(u32*)&buffer[60*2]; // word 60 and word 61
762
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);
772
773             translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
774             for (shift=device%4; shift>0; shift--)
775                 translation >>= 2;
776             translation &= 0x03;
777
778             SET_EBDA(ata.devices[device].translation, translation);
779
780             switch (translation) {
781             case ATA_TRANSLATION_NONE:
782                 BX_INFO("none");
783                 break;
784             case ATA_TRANSLATION_LBA:
785                 BX_INFO("lba");
786                 break;
787             case ATA_TRANSLATION_LARGE:
788                 BX_INFO("large");
789                 break;
790             case ATA_TRANSLATION_RECHS:
791                 BX_INFO("r-echs");
792                 break;
793             }
794             switch (translation) {
795             case ATA_TRANSLATION_NONE:
796                 break;
797             case ATA_TRANSLATION_LBA:
798                 spt = 63;
799                 sectors /= 63;
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;
805                 else heads=16;
806                 cylinders = sectors / heads;
807                 break;
808             case ATA_TRANSLATION_RECHS:
809                 // Take care not to overflow
810                 if (heads==16) {
811                     if(cylinders>61439) cylinders=61439;
812                     heads=15;
813                     cylinders = (u16)((u32)(cylinders)*16/15);
814                 }
815                 // then go through the large bitshift process
816             case ATA_TRANSLATION_LARGE:
817                 while(cylinders > 1024) {
818                     cylinders >>= 1;
819                     heads <<= 1;
820
821                     // If we max out the head count
822                     if (heads > 127) break;
823                 }
824                 break;
825             }
826             // clip to 1024 cylinders in lchs
827             if (cylinders > 1024)
828                 cylinders=1024;
829             BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
830
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);
834
835             // fill hdidmap
836             SET_EBDA(ata.idmap[0][hdcount], device);
837             hdcount++;
838         }
839
840         // Now we send a IDENTIFY command to ATAPI device
841         if(type == ATA_TYPE_ATAPI) {
842
843             u8  type, removable, mode;
844             u16 blksize;
845
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);
849
850             u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
851                                       , 1, 0, 0, 0, 0L
852                                       , GET_SEG(SS), (u32)buffer);
853             if (ret != 0)
854                 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
855
856             type      = buffer[1] & 0x1f;
857             removable = (buffer[0] & 0x80) ? 1 : 0;
858             mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
859             blksize   = 2048;
860
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);
865
866             // fill cdidmap
867             SET_EBDA(ata.idmap[1][cdcount], device);
868             cdcount++;
869         }
870
871         u32 sizeinmb = 0;
872         u16 ataversion;
873         u8  c, i, version=0, model[41];
874
875         switch (type) {
876         case ATA_TYPE_ATA:
877             sizeinmb = GET_EBDA(ata.devices[device].sectors);
878             sizeinmb >>= 11;
879         case ATA_TYPE_ATAPI:
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)
884                     break;
885             }
886
887             // Read model name
888             for (i=0;i<20;i++) {
889                 model[i*2] = buffer[(i*2)+54+1];
890                 model[(i*2)+1] = buffer[(i*2)+54];
891             }
892
893             // Reformat
894             model[40] = 0x00;
895             for (i=39;i>0;i--) {
896                 if (model[i]==0x20)
897                     model[i] = 0x00;
898                 else
899                     break;
900             }
901             break;
902         }
903
904         switch (type) {
905         case ATA_TYPE_ATA:
906             printf("ata%d %s: ",channel,slave?" slave":"master");
907             i=0;
908             while ((c=model[i++]))
909                 printf("%c",c);
910             if (sizeinmb < (1UL<<16))
911                 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
912             else
913                 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
914             break;
915         case ATA_TYPE_ATAPI:
916             printf("ata%d %s: ",channel,slave?" slave":"master");
917             i=0;
918             while ((c=model[i++]))
919                 printf("%c",c);
920             if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
921                 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
922             else
923                 printf(" ATAPI-%d Device\n",version);
924             break;
925         case ATA_TYPE_UNKNOWN:
926             printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
927             break;
928         }
929     }
930
931     // Store the devices counts
932     SET_EBDA(ata.hdcount, hdcount);
933     SET_EBDA(ata.cdcount, cdcount);
934     SET_BDA(disk_count, hdcount);
935
936     printf("\n");
937
938     // FIXME : should use bios=cmos|auto|disable bits
939     // FIXME : should know about translation bits
940     // FIXME : move hard_drive_post here
941 }