Compile fixes for gcc 4.3.
[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_SECTOR_SIZE 512
22 #define CDROM_SECTOR_SIZE 2048
23
24 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
25
26 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
27 #define DEBUGF(fmt, args...)
28
29
30 /****************************************************************
31  * Helper functions
32  ****************************************************************/
33
34 // Wait for the specified ide state
35 static int
36 await_ide(u8 when_done, u16 base, u16 timeout)
37 {
38     u32 time=0, last=0;
39     for (;;) {
40         u8 status = inb(base+ATA_CB_STAT);
41         time++;
42         u8 result = 0;
43         if (when_done == BSY)
44             result = status & ATA_CB_STAT_BSY;
45         else if (when_done == NOT_BSY)
46             result = !(status & ATA_CB_STAT_BSY);
47         else if (when_done == NOT_BSY_DRQ)
48             result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
49         else if (when_done == NOT_BSY_NOT_DRQ)
50             result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
51         else if (when_done == NOT_BSY_RDY)
52             result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
53
54         if (result)
55             return status;
56         // mod 2048 each 16 ms
57         if (time>>16 != last) {
58             last = time >>16;
59             DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
60                    " %d time= %d timeout= %d\n"
61                    , when_done, time>>11, timeout);
62         }
63         if (status & ATA_CB_STAT_ERR) {
64             DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
65                    ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
66                    , when_done, time>>11, timeout);
67             return -1;
68         }
69         if (timeout == 0 || (time>>11) > timeout)
70             break;
71     }
72     BX_INFO("IDE time out\n");
73     return -1;
74 }
75
76 // Wait for ide state - pauses for one ata cycle first.
77 static __always_inline int
78 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
79 {
80     // Wait one PIO transfer cycle.
81     inb(iobase2 + ATA_CB_ASTAT);
82
83     return await_ide(when_done, iobase1, timeout);
84 }
85
86 // Delay for x nanoseconds
87 static void
88 nsleep(u32 delay)
89 {
90     // XXX - how to implement ndelay?
91     while (delay--)
92         nop();
93 }
94
95 // Reset a drive
96 void
97 ata_reset(int driveid)
98 {
99     u16 iobase1, iobase2;
100     u8  channel, slave, sn, sc;
101     u8  type;
102
103     channel = driveid / 2;
104     slave = driveid % 2;
105
106     iobase1 = GET_EBDA(ata.channels[channel].iobase1);
107     iobase2 = GET_EBDA(ata.channels[channel].iobase2);
108
109     // Reset
110
111     // 8.2.1 (a) -- set SRST in DC
112     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
113
114     // 8.2.1 (b) -- wait for BSY
115     await_ide(BSY, iobase1, 20);
116
117     // 8.2.1 (f) -- clear SRST
118     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
119
120     type=GET_EBDA(ata.devices[driveid].type);
121     if (type != ATA_TYPE_NONE) {
122
123         // 8.2.1 (g) -- check for sc==sn==0x01
124         // select device
125         outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
126         sc = inb(iobase1+ATA_CB_SC);
127         sn = inb(iobase1+ATA_CB_SN);
128
129         if ( (sc==0x01) && (sn==0x01) ) {
130             if (type == ATA_TYPE_ATA) //ATA
131                 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
132             else //ATAPI
133                 await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
134         }
135
136         // 8.2.1 (h) -- wait for not BSY
137         await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
138     }
139
140     // Enable interrupts
141     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
142 }
143
144 struct ata_pio_command {
145     u8 feature;
146     u8 sector_count;
147     u8 lba_low;
148     u8 lba_mid;
149     u8 lba_high;
150     u8 device;
151     u8 command;
152
153     u8 sector_count2;
154     u8 lba_low2;
155     u8 lba_mid2;
156     u8 lba_high2;
157 };
158
159 // Send an ata command to the drive.
160 static int
161 send_cmd(int driveid, struct ata_pio_command *cmd)
162 {
163     u8 channel = driveid / 2;
164     u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
165     u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
166
167     int status = inb(iobase1 + ATA_CB_STAT);
168     if (status & ATA_CB_STAT_BSY)
169         return 1;
170
171     outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
172     if (cmd->command & 0x04) {
173         outb(0x00, iobase1 + ATA_CB_FR);
174         outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
175         outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
176         outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
177         outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
178     }
179     outb(cmd->feature, iobase1 + ATA_CB_FR);
180     outb(cmd->sector_count, iobase1 + ATA_CB_SC);
181     outb(cmd->lba_low, iobase1 + ATA_CB_SN);
182     outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
183     outb(cmd->lba_high, iobase1 + ATA_CB_CH);
184     outb(cmd->device, iobase1 + ATA_CB_DH);
185     outb(cmd->command, iobase1 + ATA_CB_CMD);
186
187     nsleep(400);
188
189     status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
190     if (status < 0)
191         return status;
192
193     if (status & ATA_CB_STAT_ERR) {
194         DEBUGF("send_cmd : read error\n");
195         return 2;
196     }
197     if (!(status & ATA_CB_STAT_DRQ)) {
198         DEBUGF("send_cmd : DRQ not set (status %02x)\n"
199                , (unsigned) status);
200         return 3;
201     }
202
203     return 0;
204 }
205
206 // Read and discard x number of bytes from an io channel.
207 static void
208 insx_discard(int mode, int iobase1, int bytes)
209 {
210     int count, i;
211     if (mode == ATA_MODE_PIO32) {
212         count = bytes / 4;
213         for (i=0; i<count; i++)
214             inl(iobase1);
215     } else {
216         count = bytes / 2;
217         for (i=0; i<count; i++)
218             inw(iobase1);
219     }
220 }
221
222 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
223 // 'driveid'.  If 'skipfirst' or 'skiplast' is set then the first
224 // and/or last block may be partially transferred.  This function is
225 // inlined because all the callers use different forms and because the
226 // large number of parameters would consume a lot of stack space.
227 static __always_inline int
228 ata_transfer(int driveid, int iswrite, int count, int blocksize
229              , int skipfirst, int skiplast, void *far_buffer)
230 {
231     DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
232            " skipf=%d skipl=%d buf=%p\n"
233            , driveid, iswrite, count, blocksize
234            , skipfirst, skiplast, far_buffer);
235
236     // Reset count of transferred data
237     SET_EBDA(ata.trsfsectors, 0);
238
239     u8 channel  = driveid / 2;
240     u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
241     u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
242     u8 mode     = GET_EBDA(ata.devices[driveid].mode);
243     int current = 0;
244     int status;
245     for (;;) {
246         int bsize = blocksize;
247         if (skipfirst && current == 0) {
248             insx_discard(mode, iobase1, skipfirst);
249             bsize -= skipfirst;
250         }
251         if (skiplast && current == count-1)
252             bsize -= skiplast;
253
254         if (iswrite) {
255             // Write data to controller
256             DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
257             if (mode == ATA_MODE_PIO32)
258                 outsl_far(iobase1, far_buffer, bsize / 4);
259             else
260                 outsw_far(iobase1, far_buffer, bsize / 2);
261         } else {
262             // Read data from controller
263             DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
264             if (mode == ATA_MODE_PIO32)
265                 insl_far(iobase1, far_buffer, bsize / 4);
266             else
267                 insw_far(iobase1, far_buffer, bsize / 2);
268         }
269         far_buffer += bsize;
270
271         if (skiplast && current == count-1)
272             insx_discard(mode, iobase1, skiplast);
273
274         status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
275         if (status < 0)
276             // Error
277             return status;
278
279         current++;
280         SET_EBDA(ata.trsfsectors, current);
281         if (current == count)
282             break;
283         status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
284                    | ATA_CB_STAT_ERR);
285         if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
286             DEBUGF("ata_transfer : more sectors left (status %02x)\n"
287                    , (unsigned) status);
288             return 5;
289         }
290     }
291
292     status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
293                | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
294     if (!iswrite)
295         status &= ~ATA_CB_STAT_DF;
296     if (status != ATA_CB_STAT_RDY ) {
297         DEBUGF("ata_transfer : no sectors left (status %02x)\n"
298                , (unsigned) status);
299         return 4;
300     }
301
302     // Enable interrupts
303     outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
304     return 0;
305 }
306
307
308 /****************************************************************
309  * ATA hard drive functions
310  ****************************************************************/
311
312 // Read/write count blocks from a harddrive.
313 int
314 ata_cmd_data(int driveid, u16 command, u32 lba, u16 count, void *far_buffer)
315 {
316     u8 slave   = driveid % 2;
317
318     struct ata_pio_command cmd;
319     memset(&cmd, 0, sizeof(cmd));
320
321     cmd.command = command;
322     if (count >= (1<<8) || lba + count >= (1<<28)) {
323         cmd.sector_count2 = count >> 8;
324         cmd.lba_low2 = lba >> 24;
325         cmd.lba_mid2 = 0;
326         cmd.lba_high2 = 0;
327
328         cmd.command |= 0x04;
329         lba &= 0xffffff;
330     }
331
332     cmd.feature = 0;
333     cmd.sector_count = count;
334     cmd.lba_low = lba;
335     cmd.lba_mid = lba >> 8;
336     cmd.lba_high = lba >> 16;
337     cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
338                   | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
339
340     int ret = send_cmd(driveid, &cmd);
341     if (ret)
342         return ret;
343
344     int iswrite = command == ATA_CMD_WRITE_SECTORS;
345     return ata_transfer(driveid, iswrite, count, IDE_SECTOR_SIZE
346                         , 0, 0, far_buffer);
347 }
348
349
350 /****************************************************************
351  * ATAPI functions
352  ****************************************************************/
353
354 // Low-level atapi command transmit function.
355 static int
356 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
357 {
358     u8 channel = driveid / 2;
359     u8 slave = driveid % 2;
360     u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
361     u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
362
363     struct ata_pio_command cmd;
364     cmd.sector_count = 0;
365     cmd.feature = 0;
366     cmd.lba_low = 0;
367     cmd.lba_mid = blocksize;
368     cmd.lba_high = blocksize >> 8;
369     cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
370     cmd.command = ATA_CMD_PACKET;
371
372     int ret = send_cmd(driveid, &cmd);
373     if (ret)
374         return ret;
375
376     // Send command to device
377     outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
378
379     int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
380     if (status < 0)
381         return status;
382
383     return 0;
384 }
385
386 // Low-level cdrom read atapi command transmit function.
387 static __always_inline int
388 send_cdrom_cmd(int driveid, u32 lba, u16 count)
389 {
390     u8 atacmd[12];
391     memset(atacmd, 0, sizeof(atacmd));
392
393     atacmd[0]=0x28;                     // READ command
394     atacmd[7]=(count & 0xff00) >> 8;    // Sectors
395     atacmd[8]=(count & 0x00ff);         // Sectors
396     atacmd[2]=(lba & 0xff000000) >> 24; // LBA
397     atacmd[3]=(lba & 0x00ff0000) >> 16;
398     atacmd[4]=(lba & 0x0000ff00) >> 8;
399     atacmd[5]=(lba & 0x000000ff);
400
401     return send_atapi_cmd(driveid, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
402 }
403
404 // Read sectors from the cdrom.
405 int
406 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
407 {
408     int ret = send_cdrom_cmd(driveid, lba, count);
409     if (ret)
410         return ret;
411
412     return ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE, 0, 0, far_buffer);
413 }
414
415 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
416 // sectors.
417 int
418 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
419 {
420     u32 velba = vlba + vcount - 1;
421     u32 lba = vlba / 4;
422     u32 elba = velba / 4;
423     int count = elba - lba + 1;
424     int before = vlba % 4;
425     int after = 3 - (velba % 4);
426
427     DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
428            " count=%d before=%d after=%d\n"
429            , driveid, vlba, vcount, far_buffer, lba, elba
430            , count, before, after);
431
432     int ret = send_cdrom_cmd(driveid, lba, count);
433     if (ret)
434         return ret;
435
436     ret = ata_transfer(driveid, 0, count, CDROM_SECTOR_SIZE
437                        , before*512, after*512, far_buffer);
438     if (ret) {
439         SET_EBDA(ata.trsfsectors, 0);
440         return ret;
441     }
442     SET_EBDA(ata.trsfsectors, vcount);
443     return 0;
444 }
445
446 // Send a simple atapi command to a drive.
447 int
448 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
449                , u32 length, void *far_buffer)
450 {
451     int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
452     if (ret)
453         return ret;
454
455     return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
456 }
457
458
459 /****************************************************************
460  * ATA detect and init
461  ****************************************************************/
462
463 static void
464 report_model(int driveid, u8 *buffer)
465 {
466     u8 model[41];
467
468     // Read model name
469     int i;
470     for (i=0; i<40; i+=2) {
471         model[i] = buffer[i+54+1];
472         model[i+1] = buffer[i+54];
473     }
474
475     // Reformat
476     model[40] = 0x00;
477     for (i=39; i>0; i--) {
478         if (model[i] != 0x20)
479             break;
480         model[i] = 0x00;
481     }
482
483     u8 channel = driveid / 2;
484     u8 slave = driveid % 2;
485     // XXX - model on stack not %cs
486     printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
487 }
488
489 static u8
490 get_ata_version(u8 *buffer)
491 {
492     u16 ataversion = *(u16*)&buffer[160];
493     u8 version;
494     for (version=15; version>0; version--)
495         if (ataversion & (1<<version))
496             break;
497     return version;
498 }
499
500 static void
501 init_drive_atapi(int driveid)
502 {
503     SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
504
505     // Temporary values to do the transfer
506     SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
507     SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
508
509     // Now we send a IDENTIFY command to ATAPI device
510     u8 buffer[0x0200];
511     memset(buffer, 0, sizeof(buffer));
512     u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
513                            , 1, 1
514                            , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
515     if (ret != 0)
516         BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
517
518     u8 type      = buffer[1] & 0x1f;
519     u8 removable = (buffer[0] & 0x80) ? 1 : 0;
520     u8 mode      = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
521     u16 blksize  = CDROM_SECTOR_SIZE;
522
523     SET_EBDA(ata.devices[driveid].device, type);
524     SET_EBDA(ata.devices[driveid].removable, removable);
525     SET_EBDA(ata.devices[driveid].mode, mode);
526     SET_EBDA(ata.devices[driveid].blksize, blksize);
527
528     // fill cdidmap
529     u8 cdcount = GET_EBDA(ata.cdcount);
530     SET_EBDA(ata.idmap[1][cdcount], driveid);
531     SET_EBDA(ata.cdcount, ++cdcount);
532
533     report_model(driveid, buffer);
534     u8 version = get_ata_version(buffer);
535     if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
536         printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
537     else
538         printf(" ATAPI-%d Device\n", version);
539 }
540
541 static void
542 init_drive_ata(int driveid)
543 {
544     SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA);
545
546     // Temporary values to do the transfer
547     SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
548     SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
549
550     // Now we send a IDENTIFY command to ATA device
551     u8 buffer[0x0200];
552     memset(buffer, 0, sizeof(buffer));
553     u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
554                            , 1, 1
555                            , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
556     if (ret)
557         BX_PANIC("ata-detect: Failed to detect ATA device\n");
558
559     u8 removable  = (buffer[0] & 0x80) ? 1 : 0;
560     u8 mode       = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
561     u16 blksize   = *(u16*)&buffer[10];
562
563     u16 cylinders = *(u16*)&buffer[1*2]; // word 1
564     u16 heads     = *(u16*)&buffer[3*2]; // word 3
565     u16 spt       = *(u16*)&buffer[6*2]; // word 6
566
567     u32 sectors   = *(u32*)&buffer[60*2]; // word 60 and word 61
568
569     SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD);
570     SET_EBDA(ata.devices[driveid].removable, removable);
571     SET_EBDA(ata.devices[driveid].mode, mode);
572     SET_EBDA(ata.devices[driveid].blksize, blksize);
573     SET_EBDA(ata.devices[driveid].pchs.heads, heads);
574     SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
575     SET_EBDA(ata.devices[driveid].pchs.spt, spt);
576     SET_EBDA(ata.devices[driveid].sectors, sectors);
577
578     u8 channel = driveid / 2;
579     u8 slave = driveid % 2;
580     u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
581     u8 shift;
582     for (shift=driveid%4; shift>0; shift--)
583         translation >>= 2;
584     translation &= 0x03;
585
586     SET_EBDA(ata.devices[driveid].translation, translation);
587
588     BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation="
589             , channel, slave, cylinders, heads, spt);
590     switch (translation) {
591     case ATA_TRANSLATION_NONE:
592         BX_INFO("none");
593         break;
594     case ATA_TRANSLATION_LBA:
595         BX_INFO("lba");
596         spt = 63;
597         sectors /= 63;
598         heads = sectors / 1024;
599         if (heads>128)
600             heads = 255;
601         else if (heads>64)
602             heads = 128;
603         else if (heads>32)
604             heads = 64;
605         else if (heads>16)
606             heads = 32;
607         else
608             heads = 16;
609         cylinders = sectors / heads;
610         break;
611     case ATA_TRANSLATION_RECHS:
612         BX_INFO("r-echs");
613         // Take care not to overflow
614         if (heads==16) {
615             if (cylinders>61439)
616                 cylinders=61439;
617             heads=15;
618             cylinders = (u16)((u32)(cylinders)*16/15);
619         }
620         // then go through the large bitshift process
621     case ATA_TRANSLATION_LARGE:
622         if (translation == ATA_TRANSLATION_LARGE)
623             BX_INFO("large");
624         while (cylinders > 1024) {
625             cylinders >>= 1;
626             heads <<= 1;
627
628             // If we max out the head count
629             if (heads > 127)
630                 break;
631         }
632         break;
633     }
634     // clip to 1024 cylinders in lchs
635     if (cylinders > 1024)
636         cylinders = 1024;
637     BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt);
638
639     SET_EBDA(ata.devices[driveid].lchs.heads, heads);
640     SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
641     SET_EBDA(ata.devices[driveid].lchs.spt, spt);
642
643     // fill hdidmap
644     u8 hdcount = GET_EBDA(ata.hdcount);
645     SET_EBDA(ata.idmap[0][hdcount], driveid);
646     SET_EBDA(ata.hdcount, ++hdcount);
647
648     u32 sizeinmb = GET_EBDA(ata.devices[driveid].sectors);
649     sizeinmb >>= 11;
650
651     report_model(driveid, buffer);
652     u8 version = get_ata_version(buffer);
653     if (sizeinmb < (1 << 16))
654         printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, sizeinmb);
655     else
656         printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, sizeinmb >> 10);
657 }
658
659 static void
660 init_drive_unknown(int driveid)
661 {
662     SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
663
664     u8 channel = driveid / 2;
665     u8 slave = driveid % 2;
666     printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
667 }
668
669 static void
670 ata_detect()
671 {
672 #if CONFIG_MAX_ATA_INTERFACES > 0
673     SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA);
674     SET_EBDA(ata.channels[0].iobase1,0x1f0);
675     SET_EBDA(ata.channels[0].iobase2,0x3f0);
676     SET_EBDA(ata.channels[0].irq,14);
677 #endif
678 #if CONFIG_MAX_ATA_INTERFACES > 1
679     SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA);
680     SET_EBDA(ata.channels[1].iobase1,0x170);
681     SET_EBDA(ata.channels[1].iobase2,0x370);
682     SET_EBDA(ata.channels[1].irq,15);
683 #endif
684 #if CONFIG_MAX_ATA_INTERFACES > 2
685     SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA);
686     SET_EBDA(ata.channels[2].iobase1,0x1e8);
687     SET_EBDA(ata.channels[2].iobase2,0x3e0);
688     SET_EBDA(ata.channels[2].irq,12);
689 #endif
690 #if CONFIG_MAX_ATA_INTERFACES > 3
691     SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA);
692     SET_EBDA(ata.channels[3].iobase1,0x168);
693     SET_EBDA(ata.channels[3].iobase2,0x360);
694     SET_EBDA(ata.channels[3].irq,11);
695 #endif
696 #if CONFIG_MAX_ATA_INTERFACES > 4
697 #error Please fill the ATA interface informations
698 #endif
699
700     // Device detection
701     int driveid;
702     for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
703         u8 channel = driveid / 2;
704         u8 slave = driveid % 2;
705
706         u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
707         u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
708
709         // Disable interrupts
710         outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
711
712         // Look for device
713         outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
714         outb(0x55, iobase1+ATA_CB_SC);
715         outb(0xaa, iobase1+ATA_CB_SN);
716         outb(0xaa, iobase1+ATA_CB_SC);
717         outb(0x55, iobase1+ATA_CB_SN);
718         outb(0x55, iobase1+ATA_CB_SC);
719         outb(0xaa, iobase1+ATA_CB_SN);
720
721         // If we found something
722         u8 sc = inb(iobase1+ATA_CB_SC);
723         u8 sn = inb(iobase1+ATA_CB_SN);
724
725         if (sc != 0x55 || sn != 0xaa)
726             continue;
727
728         // reset the channel
729         ata_reset(driveid);
730
731         // check for ATA or ATAPI
732         outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
733         sc = inb(iobase1+ATA_CB_SC);
734         sn = inb(iobase1+ATA_CB_SN);
735         if (sc!=0x01 || sn!=0x01) {
736             init_drive_unknown(driveid);
737             continue;
738         }
739         u8 cl = inb(iobase1+ATA_CB_CL);
740         u8 ch = inb(iobase1+ATA_CB_CH);
741         u8 st = inb(iobase1+ATA_CB_STAT);
742
743         if (cl==0x14 && ch==0xeb)
744             init_drive_atapi(driveid);
745         else if (cl==0x00 && ch==0x00 && st!=0x00)
746             init_drive_ata(driveid);
747         else if (cl==0xff && ch==0xff)
748             // None
749             continue;
750         else
751             init_drive_unknown(driveid);
752     }
753
754     // Store the device count
755     SET_BDA(disk_count, GET_EBDA(ata.hdcount));
756
757     printf("\n");
758
759     // FIXME : should use bios=cmos|auto|disable bits
760     // FIXME : should know about translation bits
761     // FIXME : move hard_drive_post here
762 }
763
764 static void
765 ata_init()
766 {
767     // hdidmap  and cdidmap init.
768     u8 device;
769     for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
770         SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
771         SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
772     }
773 }
774
775 static void
776 fill_hdinfo(int hdnum, u8 typecmos, u8 basecmos)
777 {
778     u8 type = inb_cmos(typecmos);
779     if (type != 47)
780         // XXX - halt
781         return;
782
783     SET_EBDA(fdpt[hdnum].precompensation, ((inb_cmos(basecmos+4) << 8)
784                                            | inb_cmos(basecmos+3)));
785     SET_EBDA(fdpt[hdnum].drive_control_byte, inb_cmos(basecmos+5));
786     SET_EBDA(fdpt[hdnum].landing_zone, ((inb_cmos(basecmos+7) << 8)
787                                         | inb_cmos(basecmos+6)));
788     u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0);
789     u8 heads = inb_cmos(basecmos+2);
790     u8 sectors = inb_cmos(basecmos+8);
791     if (cyl < 1024) {
792         // no logical CHS mapping used, just physical CHS
793         // use Standard Fixed Disk Parameter Table (FDPT)
794         SET_EBDA(fdpt[hdnum].cylinders, cyl);
795         SET_EBDA(fdpt[hdnum].heads, heads);
796         SET_EBDA(fdpt[hdnum].sectors, sectors);
797         return;
798     }
799
800     // complies with Phoenix style Translated Fixed Disk Parameter
801     // Table (FDPT)
802     SET_EBDA(fdpt[hdnum].phys_cylinders, cyl);
803     SET_EBDA(fdpt[hdnum].phys_heads, heads);
804     SET_EBDA(fdpt[hdnum].phys_sectors, sectors);
805     SET_EBDA(fdpt[hdnum].sectors, sectors);
806     SET_EBDA(fdpt[hdnum].a0h_signature, 0xa0);
807     if (cyl > 8192) {
808         cyl >>= 4;
809         heads <<= 4;
810     } else if (cyl > 4096) {
811         cyl >>= 3;
812         heads <<= 3;
813     } else if (cyl > 2048) {
814         cyl >>= 2;
815         heads <<= 2;
816     }
817     SET_EBDA(fdpt[hdnum].cylinders, cyl);
818     SET_EBDA(fdpt[hdnum].heads, heads);
819     u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
820                                            , fdpt[hdnum]));
821     u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
822                                       , fdpt[hdnum]) - 1);
823     SET_EBDA(fdpt[hdnum].checksum, -sum);
824 }
825
826 void
827 hard_drive_setup()
828 {
829     outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14
830     SET_BDA(disk_count, 1);
831     SET_BDA(disk_control_byte, 0xc0);
832
833     // move disk geometry data from CMOS to EBDA disk parameter table(s)
834     u8 diskinfo = inb_cmos(CMOS_DISK_DATA);
835     if ((diskinfo & 0xf0) == 0xf0)
836         // Fill EBDA table for hard disk 0.
837         fill_hdinfo(0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL);
838     if ((diskinfo & 0x0f) == 0x0f)
839         // XXX - bochs halts on any other type
840         // Fill EBDA table for hard disk 1.
841         fill_hdinfo(1, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL);
842
843     if (CONFIG_ATA) {
844         ata_init();
845         ata_detect();
846     }
847 }