6b8bd86710a5df252d427b14a082a317176d1288
[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-in 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 u16
142 ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
143                 , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
144 {
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);
149
150     u8 channel = device / 2;
151     u8 slave   = device % 2;
152
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);
156
157     // Reset count of transferred data
158     SET_EBDA(ata.trsfsectors,0);
159     SET_EBDA(ata.trsfbytes,0L);
160
161     u8 status = inb(iobase1 + ATA_CB_STAT);
162     if (status & ATA_CB_STAT_BSY)
163         return 1;
164
165     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
166
167     // sector will be 0 only on lba access. Convert to lba-chs
168     if (sector == 0) {
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);
175             command |= 0x04;
176             count &= (1UL << 8) - 1;
177             lba &= (1UL << 24) - 1;
178         }
179         sector = (u16) (lba & 0x000000ffL);
180         cylinder = (u16) ((lba>>8) & 0x0000ffffL);
181         head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
182     }
183
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);
192
193     await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
194     status = inb(iobase1 + ATA_CB_STAT);
195
196     if (status & ATA_CB_STAT_ERR) {
197         DEBUGF("ata_cmd_data_in : read error\n");
198         return 2;
199     } else if ( !(status & ATA_CB_STAT_DRQ) ) {
200         DEBUGF("ata_cmd_data_in : DRQ not set (status %02x)\n"
201                , (unsigned) status);
202         return 3;
203     }
204
205     // FIXME : move seg/off translation here
206
207     irq_enable();
208
209     u8 current = 0;
210     while (1) {
211         if (offset > 0xf800) {
212             offset -= 0x800;
213             segment += 0x80;
214         }
215
216         if (mode == ATA_MODE_PIO32)
217             insl_seg(iobase1, segment, offset, 512 / 4);
218         else
219             insw_seg(iobase1, segment, offset, 512 / 2);
220         offset += 512;
221
222         current++;
223         SET_EBDA(ata.trsfsectors,current);
224         count--;
225         await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
226         status = inb(iobase1 + ATA_CB_STAT);
227         if (count == 0) {
228             if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
229                             | ATA_CB_STAT_ERR) )
230                  != ATA_CB_STAT_RDY ) {
231                 DEBUGF("ata_cmd_data_in : no sectors left (status %02x)\n"
232                        , (unsigned) status);
233                 return 4;
234             }
235             break;
236         }
237         else {
238             if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
239                             | ATA_CB_STAT_ERR) )
240                  != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
241                 DEBUGF("ata_cmd_data_in : more sectors left (status %02x)\n"
242                        , (unsigned) status);
243                 return 5;
244             }
245             continue;
246         }
247     }
248     // Enable interrupts
249     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
250     return 0;
251 }
252
253 // ---------------------------------------------------------------------------
254 // ATA/ATAPI driver : execute a data-out command
255 // ---------------------------------------------------------------------------
256       // returns
257       // 0 : no error
258       // 1 : BUSY bit set
259       // 2 : read error
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
265 u16
266 ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
267                  , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
268 {
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);
273
274     u8 channel = device / 2;
275     u8 slave   = device % 2;
276
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);
280
281     // Reset count of transferred data
282     SET_EBDA(ata.trsfsectors,0);
283     SET_EBDA(ata.trsfbytes,0L);
284
285     u8 status = inb(iobase1 + ATA_CB_STAT);
286     if (status & ATA_CB_STAT_BSY)
287         return 1;
288
289     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
290
291     // sector will be 0 only on lba access. Convert to lba-chs
292     if (sector == 0) {
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);
299             command |= 0x04;
300             count &= (1UL << 8) - 1;
301             lba &= (1UL << 24) - 1;
302         }
303         sector = (u16) (lba & 0x000000ffL);
304         cylinder = (u16) ((lba>>8) & 0x0000ffffL);
305         head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
306     }
307
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);
316
317     await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
318     status = inb(iobase1 + ATA_CB_STAT);
319
320     if (status & ATA_CB_STAT_ERR) {
321         DEBUGF("ata_cmd_data_out : read error\n");
322         return 2;
323     } else if ( !(status & ATA_CB_STAT_DRQ) ) {
324         DEBUGF("ata_cmd_data_out : DRQ not set (status %02x)\n"
325                , (unsigned) status);
326         return 3;
327     }
328
329     // FIXME : move seg/off translation here
330
331     irq_enable();
332
333     u8 current = 0;
334     while (1) {
335         if (offset > 0xf800) {
336             offset -= 0x800;
337             segment += 0x80;
338         }
339
340         if (mode == ATA_MODE_PIO32)
341             outsl_seg(iobase1, segment, offset, 512 / 4);
342         else
343             outsw_seg(iobase1, segment, offset, 512 / 2);
344         offset += 512;
345
346         current++;
347         SET_EBDA(ata.trsfsectors,current);
348         count--;
349         status = inb(iobase1 + ATA_CB_STAT);
350         if (count == 0) {
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);
356                 return 6;
357             }
358             break;
359         } else {
360             if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
361                             | ATA_CB_STAT_ERR) )
362                  != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
363                 DEBUGF("ata_cmd_data_out : more sectors left (status %02x)\n"
364                        , (unsigned) status);
365                 return 7;
366             }
367             continue;
368         }
369     }
370     // Enable interrupts
371     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
372     return 0;
373 }
374
375 // ---------------------------------------------------------------------------
376 // ATA/ATAPI driver : execute a packet command
377 // ---------------------------------------------------------------------------
378       // returns
379       // 0 : no error
380       // 1 : error in parameters
381       // 2 : BUSY bit set
382       // 3 : error
383       // 4 : not ready
384 u16
385 ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
386                , u32 length, u8 inout, u16 bufseg, u16 bufoff)
387 {
388     DEBUGF("ata_cmd_packet d=%d cmdlen=%d h=%d l=%d inout=%d"
389            " seg=%x off=%x\n"
390            , device, cmdlen, header, length, inout
391            , bufseg, bufoff);
392
393     u8 channel = device / 2;
394     u8 slave = device % 2;
395
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");
399         return 1;
400     }
401
402     // The header length must be even
403     if (header & 1) {
404         DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
405         return 1;
406     }
407
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);
411
412     if (cmdlen < 12)
413         cmdlen=12;
414     if (cmdlen > 12)
415         cmdlen=16;
416     cmdlen>>=1;
417
418     // Reset count of transferred data
419     SET_EBDA(ata.trsfsectors,0);
420     SET_EBDA(ata.trsfbytes,0L);
421
422     u8 status = inb(iobase1 + ATA_CB_STAT);
423     if (status & ATA_CB_STAT_BSY)
424         return 2;
425
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);
434
435     // Device should ok to receive command
436     await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
437     status = inb(iobase1 + ATA_CB_STAT);
438
439     if (status & ATA_CB_STAT_ERR) {
440         DEBUGF("ata_cmd_packet : error, status is %02x\n", status);
441         return 3;
442     } else if ( !(status & ATA_CB_STAT_DRQ) ) {
443         DEBUGF("ata_cmd_packet : DRQ not set (status %02x)\n"
444                , (unsigned) status);
445         return 4;
446     }
447
448     // Send command to device
449     irq_enable();
450
451     outsw_seg(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
452
453     if (inout == ATA_DATA_NO) {
454         await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
455         status = inb(iobase1 + ATA_CB_STAT);
456     } else {
457         u16 loops = 0;
458         while (1) {
459             if (loops == 0) {//first time through
460                 status = inb(iobase2 + ATA_CB_ASTAT);
461                 await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
462             } else
463                 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
464             loops++;
465
466             status = inb(iobase1 + ATA_CB_STAT);
467             inb(iobase1 + ATA_CB_SC);
468
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))
472                 break;
473
474             if (status & ATA_CB_STAT_ERR) {
475                 DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
476                 return 3;
477             }
478
479             // Normalize address
480             bufseg += (bufoff / 16);
481             bufoff %= 16;
482
483             // Get the byte count
484             u16 lcount =  (((u16)(inb(iobase1 + ATA_CB_CH))<<8)
485                            + inb(iobase1 + ATA_CB_CL));
486
487             // adjust to read what we want
488             u16 lbefore, lafter;
489             if (header > lcount) {
490                 lbefore=lcount;
491                 header-=lcount;
492                 lcount=0;
493             } else {
494                 lbefore=header;
495                 header=0;
496                 lcount-=lbefore;
497             }
498
499             if (lcount > length) {
500                 lafter=lcount-length;
501                 lcount=length;
502                 length=0;
503             } else {
504                 lafter=0;
505                 length-=lcount;
506             }
507
508             // Save byte count
509             u16 count = lcount;
510
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);
514
515             // If counts not dividable by 4, use 16bits mode
516             u8 lmode = 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;
520
521             // adds an extra byte if count are odd. before is always even
522             if (lcount & 0x01) {
523                 lcount+=1;
524                 if ((lafter > 0) && (lafter & 0x01)) {
525                     lafter-=1;
526                 }
527             }
528
529             if (lmode == ATA_MODE_PIO32) {
530                 lcount>>=2; lbefore>>=2; lafter>>=2;
531             } else {
532                 lcount>>=1; lbefore>>=1; lafter>>=1;
533             }
534
535             int i;
536             for (i=0; i<lbefore; i++)
537                 if (lmode == ATA_MODE_PIO32)
538                     inl(iobase1);
539                 else
540                     inw(iobase1);
541
542             if (lmode == ATA_MODE_PIO32)
543                 insl_seg(iobase1, bufseg, bufoff, lcount);
544             else
545                 insw_seg(iobase1, bufseg, bufoff, lcount);
546
547             for (i=0; i<lafter; i++)
548                 if (lmode == ATA_MODE_PIO32)
549                     inl(iobase1);
550                 else
551                     inw(iobase1);
552
553             // Compute new buffer address
554             bufoff += count;
555
556             // Save transferred bytes count
557             SET_EBDA(ata.trsfsectors, loops);
558         }
559     }
560
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);
567         return 4;
568     }
569
570     // Enable interrupts
571     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
572     return 0;
573 }
574
575 u16
576 cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
577 {
578     u16 sectors = (count + 2048 - 1) / 2048;
579
580     u8 atacmd[12];
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);
589
590     return ata_cmd_packet(device, atacmd, sizeof(atacmd)
591                           , skip, count, ATA_DATA_IN
592                           , segment, offset);
593 }
594
595 // ---------------------------------------------------------------------------
596 // ATA/ATAPI driver : device detection
597 // ---------------------------------------------------------------------------
598
599 void
600 ata_detect()
601 {
602     u8  hdcount, cdcount, device, type;
603     u8  buffer[0x0200];
604     memset(buffer, 0, sizeof(buffer));
605
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);
611 #endif
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);
617 #endif
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);
623 #endif
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);
629 #endif
630 #if CONFIG_MAX_ATA_INTERFACES > 4
631 #error Please fill the ATA interface informations
632 #endif
633
634     // Device detection
635     hdcount=cdcount=0;
636
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;
641
642         channel = device / 2;
643         slave = device % 2;
644
645         iobase1 =GET_EBDA(ata.channels[channel].iobase1);
646         iobase2 =GET_EBDA(ata.channels[channel].iobase2);
647
648         // Disable interrupts
649         outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
650
651         // Look for device
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);
659
660         // If we found something
661         sc = inb(iobase1+ATA_CB_SC);
662         sn = inb(iobase1+ATA_CB_SN);
663
664         if ( (sc == 0x55) && (sn == 0xaa) ) {
665             SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
666
667             // reset the channel
668             ata_reset(device);
669
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);
678
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);
685                 }
686             }
687         }
688
689         type=GET_EBDA(ata.devices[device].type);
690
691         // Now we send a IDENTIFY command to ATA device
692         if(type == ATA_TYPE_ATA) {
693             u32 sectors;
694             u16 cylinders, heads, spt, blksize;
695             u8  translation, removable, mode;
696
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);
700
701             u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
702                                       , 1, 0, 0, 0, 0L
703                                       , GET_SEG(SS), (u32)buffer);
704             if (ret)
705                 BX_PANIC("ata-detect: Failed to detect ATA device\n");
706
707             removable = (buffer[0] & 0x80) ? 1 : 0;
708             mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
709             blksize   = *(u16*)&buffer[10];
710
711             cylinders = *(u16*)&buffer[1*2]; // word 1
712             heads     = *(u16*)&buffer[3*2]; // word 3
713             spt       = *(u16*)&buffer[6*2]; // word 6
714
715             sectors   = *(u32*)&buffer[60*2]; // word 60 and word 61
716
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);
726
727             translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
728             for (shift=device%4; shift>0; shift--)
729                 translation >>= 2;
730             translation &= 0x03;
731
732             SET_EBDA(ata.devices[device].translation, translation);
733
734             switch (translation) {
735             case ATA_TRANSLATION_NONE:
736                 BX_INFO("none");
737                 break;
738             case ATA_TRANSLATION_LBA:
739                 BX_INFO("lba");
740                 break;
741             case ATA_TRANSLATION_LARGE:
742                 BX_INFO("large");
743                 break;
744             case ATA_TRANSLATION_RECHS:
745                 BX_INFO("r-echs");
746                 break;
747             }
748             switch (translation) {
749             case ATA_TRANSLATION_NONE:
750                 break;
751             case ATA_TRANSLATION_LBA:
752                 spt = 63;
753                 sectors /= 63;
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;
759                 else heads=16;
760                 cylinders = sectors / heads;
761                 break;
762             case ATA_TRANSLATION_RECHS:
763                 // Take care not to overflow
764                 if (heads==16) {
765                     if(cylinders>61439) cylinders=61439;
766                     heads=15;
767                     cylinders = (u16)((u32)(cylinders)*16/15);
768                 }
769                 // then go through the large bitshift process
770             case ATA_TRANSLATION_LARGE:
771                 while(cylinders > 1024) {
772                     cylinders >>= 1;
773                     heads <<= 1;
774
775                     // If we max out the head count
776                     if (heads > 127) break;
777                 }
778                 break;
779             }
780             // clip to 1024 cylinders in lchs
781             if (cylinders > 1024)
782                 cylinders=1024;
783             BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
784
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);
788
789             // fill hdidmap
790             SET_EBDA(ata.idmap[0][hdcount], device);
791             hdcount++;
792         }
793
794         // Now we send a IDENTIFY command to ATAPI device
795         if(type == ATA_TYPE_ATAPI) {
796
797             u8  type, removable, mode;
798             u16 blksize;
799
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);
803
804             u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
805                                       , 1, 0, 0, 0, 0L
806                                       , GET_SEG(SS), (u32)buffer);
807             if (ret != 0)
808                 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
809
810             type      = buffer[1] & 0x1f;
811             removable = (buffer[0] & 0x80) ? 1 : 0;
812             mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
813             blksize   = 2048;
814
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);
819
820             // fill cdidmap
821             SET_EBDA(ata.idmap[1][cdcount], device);
822             cdcount++;
823         }
824
825         u32 sizeinmb = 0;
826         u16 ataversion;
827         u8  c, i, version=0, model[41];
828
829         switch (type) {
830         case ATA_TYPE_ATA:
831             sizeinmb = GET_EBDA(ata.devices[device].sectors);
832             sizeinmb >>= 11;
833         case ATA_TYPE_ATAPI:
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)
838                     break;
839             }
840
841             // Read model name
842             for (i=0;i<20;i++) {
843                 model[i*2] = buffer[(i*2)+54+1];
844                 model[(i*2)+1] = buffer[(i*2)+54];
845             }
846
847             // Reformat
848             model[40] = 0x00;
849             for (i=39;i>0;i--) {
850                 if (model[i]==0x20)
851                     model[i] = 0x00;
852                 else
853                     break;
854             }
855             break;
856         }
857
858         switch (type) {
859         case ATA_TYPE_ATA:
860             printf("ata%d %s: ",channel,slave?" slave":"master");
861             i=0;
862             while ((c=model[i++]))
863                 printf("%c",c);
864             if (sizeinmb < (1UL<<16))
865                 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
866             else
867                 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
868             break;
869         case ATA_TYPE_ATAPI:
870             printf("ata%d %s: ",channel,slave?" slave":"master");
871             i=0;
872             while ((c=model[i++]))
873                 printf("%c",c);
874             if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
875                 printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
876             else
877                 printf(" ATAPI-%d Device\n",version);
878             break;
879         case ATA_TYPE_UNKNOWN:
880             printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
881             break;
882         }
883     }
884
885     // Store the devices counts
886     SET_EBDA(ata.hdcount, hdcount);
887     SET_EBDA(ata.cdcount, cdcount);
888     SET_BDA(disk_count, hdcount);
889
890     printf("\n");
891
892     // FIXME : should use bios=cmos|auto|disable bits
893     // FIXME : should know about translation bits
894     // FIXME : move hard_drive_post here
895 }