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