1 // Low level ATA disk access
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU GPLv3 license.
8 #include "ata.h" // ATA_*
9 #include "types.h" // u8
10 #include "ioport.h" // inb
11 #include "util.h" // dprintf
12 #include "cmos.h" // inb_cmos
13 #include "pic.h" // unmask_pic2
14 #include "biosvar.h" // GET_EBDA
20 #define NOT_BSY_NOT_DRQ 4
23 #define IDE_SECTOR_SIZE 512
24 #define CDROM_SECTOR_SIZE 2048
26 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
29 /****************************************************************
31 ****************************************************************/
33 // Wait for the specified ide state
35 await_ide(u8 when_done, u16 base, u16 timeout)
39 u8 status = inb(base+ATA_CB_STAT);
43 result = status & ATA_CB_STAT_BSY;
44 else if (when_done == NOT_BSY)
45 result = !(status & ATA_CB_STAT_BSY);
46 else if (when_done == NOT_BSY_DRQ)
47 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
48 else if (when_done == NOT_BSY_NOT_DRQ)
49 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
50 else if (when_done == NOT_BSY_RDY)
51 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
55 // mod 2048 each 16 ms
56 if (time>>16 != last) {
58 dprintf(6, "await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ"
59 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
60 , when_done, time>>11, timeout);
62 if (status & ATA_CB_STAT_ERR) {
63 dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
64 ",!BSY_!DRQ,!BSY_RDY) %d status=%x time= %d timeout= %d\n"
65 , when_done, status, time>>11, timeout);
68 if (timeout == 0 || (time>>11) > timeout)
71 dprintf(1, "IDE time out\n");
75 // Wait for ide state - pauses for one ata cycle first.
76 static __always_inline int
77 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
79 // Wait one PIO transfer cycle.
80 inb(iobase2 + ATA_CB_ASTAT);
82 return await_ide(when_done, iobase1, timeout);
85 // Delay for x nanoseconds
89 // XXX - how to implement ndelay?
94 // Wait for ide state - pause for 400ns first.
95 static __always_inline int
96 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
99 return await_ide(when_done, iobase1, timeout);
102 // Delay for x milliseconds
106 usleep(delay * 1000);
111 ata_reset(int driveid)
113 u8 channel = driveid / 2;
114 u8 slave = driveid % 2;
115 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
116 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
120 // 8.2.1 (a) -- set SRST in DC
121 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
123 // 8.2.1 (b) -- wait for BSY
124 int status = await_ide(BSY, iobase1, 20);
125 dprintf(6, "ata_reset(1) status=%x\n", status);
127 // 8.2.1 (f) -- clear SRST
128 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
130 // 8.2.1 (g) -- check for sc==sn==0x01
132 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
134 u8 sc = inb(iobase1+ATA_CB_SC);
135 u8 sn = inb(iobase1+ATA_CB_SN);
137 // For predetermined ATA drives - wait for ready.
138 if (sc==0x01 && sn==0x01) {
139 u8 type=GET_EBDA(ata.devices[driveid].type);
140 if (type == ATA_TYPE_ATA)
141 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
144 // 8.2.1 (h) -- wait for not BSY
145 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
146 dprintf(6, "ata_reset(2) status=%x\n", status);
149 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
153 /****************************************************************
155 ****************************************************************/
164 struct ata_pio_command {
179 // Send an ata command to the drive.
181 send_cmd(int driveid, struct ata_pio_command *cmd)
183 u8 channel = driveid / 2;
184 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
185 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
187 int status = inb(iobase1 + ATA_CB_STAT);
188 if (status & ATA_CB_STAT_BSY)
191 // Disable interrupts
192 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
195 u8 device = inb(iobase1 + ATA_CB_DH);
196 outb(cmd->device, iobase1 + ATA_CB_DH);
197 if ((device ^ cmd->device) & (1 << 4))
198 // Wait for device to become active.
201 if (cmd->command & 0x04) {
202 outb(0x00, iobase1 + ATA_CB_FR);
203 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
204 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
205 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
206 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
208 outb(cmd->feature, iobase1 + ATA_CB_FR);
209 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
210 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
211 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
212 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
213 outb(cmd->command, iobase1 + ATA_CB_CMD);
215 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
219 if (status & ATA_CB_STAT_ERR) {
220 dprintf(6, "send_cmd : read error\n");
223 if (!(status & ATA_CB_STAT_DRQ)) {
224 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
225 , (unsigned) status);
233 /****************************************************************
235 ****************************************************************/
237 // Read and discard x number of bytes from an io channel.
239 insx_discard(int mode, int iobase1, int bytes)
242 if (mode == ATA_MODE_PIO32) {
244 for (i=0; i<count; i++)
248 for (i=0; i<count; i++)
253 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
254 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
255 // and/or last block may be partially transferred. This function is
256 // inlined because all the callers use different forms and because the
257 // large number of parameters would consume a lot of stack space.
258 static __always_inline int
259 ata_transfer(int driveid, int iswrite, int count, int blocksize
260 , int skipfirst, int skiplast, void *far_buffer)
262 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
263 " skipf=%d skipl=%d buf=%p\n"
264 , driveid, iswrite, count, blocksize
265 , skipfirst, skiplast, far_buffer);
267 // Reset count of transferred data
268 SET_EBDA(ata.trsfsectors, 0);
270 u8 channel = driveid / 2;
271 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
272 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
273 u8 mode = GET_EBDA(ata.devices[driveid].mode);
277 int bsize = blocksize;
278 if (skipfirst && current == 0) {
279 insx_discard(mode, iobase1, skipfirst);
282 if (skiplast && current == count-1)
286 // Write data to controller
287 dprintf(16, "Write sector id=%d dest=%p\n", driveid, far_buffer);
288 if (mode == ATA_MODE_PIO32)
289 outsl_far(iobase1, far_buffer, bsize / 4);
291 outsw_far(iobase1, far_buffer, bsize / 2);
293 // Read data from controller
294 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
295 if (mode == ATA_MODE_PIO32)
296 insl_far(iobase1, far_buffer, bsize / 4);
298 insw_far(iobase1, far_buffer, bsize / 2);
302 if (skiplast && current == count-1)
303 insx_discard(mode, iobase1, skiplast);
305 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
311 SET_EBDA(ata.trsfsectors, current);
312 if (current == count)
314 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
316 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
317 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
318 , (unsigned) status);
323 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
324 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
326 status &= ~ATA_CB_STAT_DF;
327 if (status != ATA_CB_STAT_RDY ) {
328 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
329 , (unsigned) status);
334 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
339 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
341 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
342 , 0, 0, op->far_buffer);
346 ata_transfer_cdrom(const struct ata_op_s *op)
348 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
349 , 0, 0, op->far_buffer);
353 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
355 int vcount = op->count * 4 - before - after;
356 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
357 , before*512, after*512, op->far_buffer);
359 SET_EBDA(ata.trsfsectors, 0);
362 SET_EBDA(ata.trsfsectors, vcount);
367 /****************************************************************
368 * ATA hard drive functions
369 ****************************************************************/
372 send_cmd_disk(const struct ata_op_s *op, u16 command)
374 u8 slave = op->driveid % 2;
377 struct ata_pio_command cmd;
378 memset(&cmd, 0, sizeof(cmd));
380 cmd.command = command;
381 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
382 cmd.sector_count2 = op->count >> 8;
383 cmd.lba_low2 = lba >> 24;
384 cmd.lba_mid2 = lba >> 32;
385 cmd.lba_high2 = lba >> 40;
392 cmd.sector_count = op->count;
394 cmd.lba_mid = lba >> 8;
395 cmd.lba_high = lba >> 16;
396 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
397 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
399 return send_cmd(op->driveid, &cmd);
402 // Read/write count blocks from a harddrive.
404 ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
407 op.driveid = driveid;
410 op.far_buffer = far_buffer;
412 int ret = send_cmd_disk(&op, command);
416 int iswrite = command == ATA_CMD_WRITE_SECTORS;
417 return ata_transfer_disk(&op, iswrite);
421 /****************************************************************
423 ****************************************************************/
425 // Low-level atapi command transmit function.
426 static __always_inline int
427 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
429 u8 channel = driveid / 2;
430 u8 slave = driveid % 2;
431 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
432 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
434 struct ata_pio_command cmd;
435 cmd.sector_count = 0;
438 cmd.lba_mid = blocksize;
439 cmd.lba_high = blocksize >> 8;
440 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
441 cmd.command = ATA_CMD_PACKET;
443 int ret = send_cmd(driveid, &cmd);
447 // Send command to device
448 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
450 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
457 // Low-level cdrom read atapi command transmit function.
459 send_cmd_cdrom(const struct ata_op_s *op)
462 memset(atacmd, 0, sizeof(atacmd));
464 atacmd[0]=0x28; // READ command
465 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
466 atacmd[8]=(op->count & 0x00ff);
467 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
468 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
469 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
470 atacmd[5]=(op->lba & 0x000000ff);
472 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
473 , CDROM_SECTOR_SIZE);
476 // Read sectors from the cdrom.
478 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
481 op.driveid = driveid;
484 op.far_buffer = far_buffer;
486 int ret = send_cmd_cdrom(&op);
490 return ata_transfer_cdrom(&op);
493 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
496 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
498 u32 velba = vlba + vcount - 1;
500 u32 elba = velba / 4;
501 int count = elba - lba + 1;
502 int before = vlba % 4;
503 int after = 3 - (velba % 4);
506 op.driveid = driveid;
509 op.far_buffer = far_buffer;
511 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
512 " count=%d before=%d after=%d\n"
513 , driveid, vlba, vcount, far_buffer, lba, elba
514 , count, before, after);
516 int ret = send_cmd_cdrom(&op);
520 return ata_transfer_emu(&op, before, after);
523 // Send a simple atapi command to a drive.
525 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
526 , u32 length, void *far_buffer)
528 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
532 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
536 /****************************************************************
537 * ATA detect and init
538 ****************************************************************/
541 report_model(int driveid, u8 *buffer)
547 for (i=0; i<40; i+=2) {
548 model[i] = buffer[i+54+1];
549 model[i+1] = buffer[i+54];
554 for (i=39; i>0; i--) {
555 if (model[i] != 0x20)
560 u8 channel = driveid / 2;
561 u8 slave = driveid % 2;
562 // XXX - model on stack not %cs
563 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
567 get_ata_version(u8 *buffer)
569 u16 ataversion = *(u16*)&buffer[160];
571 for (version=15; version>0; version--)
572 if (ataversion & (1<<version))
578 init_drive_atapi(int driveid)
580 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
582 // Temporary values to do the transfer
583 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
584 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
586 // Now we send a IDENTIFY command to ATAPI device
588 memset(buffer, 0, sizeof(buffer));
589 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
591 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
593 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
595 u8 type = buffer[1] & 0x1f;
596 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
597 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
598 u16 blksize = CDROM_SECTOR_SIZE;
600 SET_EBDA(ata.devices[driveid].device, type);
601 SET_EBDA(ata.devices[driveid].removable, removable);
602 SET_EBDA(ata.devices[driveid].mode, mode);
603 SET_EBDA(ata.devices[driveid].blksize, blksize);
606 u8 cdcount = GET_EBDA(ata.cdcount);
607 SET_EBDA(ata.idmap[1][cdcount], driveid);
608 SET_EBDA(ata.cdcount, ++cdcount);
610 report_model(driveid, buffer);
611 u8 version = get_ata_version(buffer);
612 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
613 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
615 printf(" ATAPI-%d Device\n", version);
619 fill_fdpt(int driveid)
624 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
625 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
626 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
628 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
629 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
630 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
632 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
633 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
634 SET_EBDA(fdpt[driveid].landing_zone, npc);
635 SET_EBDA(fdpt[driveid].cylinders, nlc);
636 SET_EBDA(fdpt[driveid].heads, nlh);
637 SET_EBDA(fdpt[driveid].sectors, nlspt);
639 if (nlc == npc && nlh == nph && nlspt == npspt)
640 // no logical CHS mapping used, just physical CHS
641 // use Standard Fixed Disk Parameter Table (FDPT)
644 // complies with Phoenix style Translated Fixed Disk Parameter
646 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
647 SET_EBDA(fdpt[driveid].phys_heads, nph);
648 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
649 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
651 // Checksum structure.
652 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
654 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
655 , fdpt[driveid]) - 1);
656 SET_EBDA(fdpt[driveid].checksum, -sum);
660 get_translation(int driveid)
662 if (! CONFIG_COREBOOT) {
663 // Emulators pass in the translation info via nvram.
664 u8 channel = driveid / 2;
665 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
666 translation >>= 2 * (driveid % 4);
671 // On COREBOOT, use a heuristic to determine translation type.
672 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
673 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
674 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
676 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
677 return ATA_TRANSLATION_NONE;
678 if (cylinders * heads <= 131072)
679 return ATA_TRANSLATION_LARGE;
680 return ATA_TRANSLATION_LBA;
684 setup_translation(int driveid)
686 u8 translation = get_translation(driveid);
687 SET_EBDA(ata.devices[driveid].translation, translation);
689 u8 channel = driveid / 2;
690 u8 slave = driveid % 2;
691 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
692 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
693 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
694 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
696 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
697 , channel, slave, cylinders, heads, spt);
698 switch (translation) {
699 case ATA_TRANSLATION_NONE:
702 case ATA_TRANSLATION_LBA:
705 if (sectors > 63*255*1024) {
710 u32 sect = (u32)sectors / 63;
722 cylinders = sect / heads;
724 case ATA_TRANSLATION_RECHS:
725 dprintf(1, "r-echs");
726 // Take care not to overflow
731 cylinders = (u16)((u32)(cylinders)*16/15);
733 // then go through the large bitshift process
734 case ATA_TRANSLATION_LARGE:
735 if (translation == ATA_TRANSLATION_LARGE)
737 while (cylinders > 1024) {
741 // If we max out the head count
747 // clip to 1024 cylinders in lchs
748 if (cylinders > 1024)
750 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
752 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
753 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
754 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
758 init_drive_ata(int driveid)
760 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
762 // Temporary values to do the transfer
763 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
764 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
766 // Now we send a IDENTIFY command to ATA device
768 memset(buffer, 0, sizeof(buffer));
769 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
771 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
773 BX_PANIC("ata-detect: Failed to detect ATA device\n");
775 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
776 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
777 u16 blksize = *(u16*)&buffer[10];
779 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
780 u16 heads = *(u16*)&buffer[3*2]; // word 3
781 u16 spt = *(u16*)&buffer[6*2]; // word 6
784 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
785 sectors = *(u64*)&buffer[100*2]; // word 100-103
787 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
789 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
790 SET_EBDA(ata.devices[driveid].removable, removable);
791 SET_EBDA(ata.devices[driveid].mode, mode);
792 SET_EBDA(ata.devices[driveid].blksize, blksize);
793 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
794 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
795 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
796 SET_EBDA(ata.devices[driveid].sectors, sectors);
798 // Setup disk geometry translation.
799 setup_translation(driveid);
802 u8 hdcount = GET_EBDA(ata.hdcount);
803 SET_EBDA(ata.idmap[0][hdcount], driveid);
804 SET_EBDA(ata.hdcount, ++hdcount);
806 // Fill "fdpt" structure.
809 // Report drive info to user.
810 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
811 report_model(driveid, buffer);
812 u8 version = get_ata_version(buffer);
813 if (sizeinmb < (1 << 16))
814 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
816 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
817 , (u32)(sizeinmb >> 10));
821 init_drive_unknown(int driveid)
823 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
825 u8 channel = driveid / 2;
826 u8 slave = driveid % 2;
827 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
835 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
836 u8 channel = driveid / 2;
837 u8 slave = driveid % 2;
839 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
840 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
842 // Disable interrupts
843 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
846 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
848 outb(0x55, iobase1+ATA_CB_SC);
849 outb(0xaa, iobase1+ATA_CB_SN);
850 outb(0xaa, iobase1+ATA_CB_SC);
851 outb(0x55, iobase1+ATA_CB_SN);
852 outb(0x55, iobase1+ATA_CB_SC);
853 outb(0xaa, iobase1+ATA_CB_SN);
855 // If we found something
856 u8 sc = inb(iobase1+ATA_CB_SC);
857 u8 sn = inb(iobase1+ATA_CB_SN);
858 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
860 if (sc != 0x55 || sn != 0xaa)
866 // check for ATA or ATAPI
867 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
869 sc = inb(iobase1+ATA_CB_SC);
870 sn = inb(iobase1+ATA_CB_SN);
871 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
872 if (sc!=0x01 || sn!=0x01) {
873 init_drive_unknown(driveid);
876 u8 cl = inb(iobase1+ATA_CB_CL);
877 u8 ch = inb(iobase1+ATA_CB_CH);
878 u8 st = inb(iobase1+ATA_CB_STAT);
879 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
880 , driveid, sc, sn, cl, ch, st);
882 if (cl==0x14 && ch==0xeb)
883 init_drive_atapi(driveid);
884 else if (cl==0x00 && ch==0x00 && st!=0x00)
885 init_drive_ata(driveid);
886 else if (cl==0xff && ch==0xff)
890 init_drive_unknown(driveid);
899 // hdidmap and cdidmap init.
901 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
902 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
903 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
906 #if CONFIG_MAX_ATA_INTERFACES > 0
907 SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
908 SET_EBDA(ata.channels[0].iobase1, 0x1f0);
909 SET_EBDA(ata.channels[0].iobase2, 0x3f0);
910 SET_EBDA(ata.channels[0].irq, 14);
912 #if CONFIG_MAX_ATA_INTERFACES > 1
913 SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
914 SET_EBDA(ata.channels[1].iobase1, 0x170);
915 SET_EBDA(ata.channels[1].iobase2, 0x370);
916 SET_EBDA(ata.channels[1].irq, 15);
918 #if CONFIG_MAX_ATA_INTERFACES > 2
919 SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
920 SET_EBDA(ata.channels[2].iobase1, 0x1e8);
921 SET_EBDA(ata.channels[2].iobase2, 0x3e0);
922 SET_EBDA(ata.channels[2].irq, 12);
924 #if CONFIG_MAX_ATA_INTERFACES > 3
925 SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
926 SET_EBDA(ata.channels[3].iobase1, 0x168);
927 SET_EBDA(ata.channels[3].iobase2, 0x360);
928 SET_EBDA(ata.channels[3].irq, 11);
930 #if CONFIG_MAX_ATA_INTERFACES > 4
931 #error Please fill the ATA interface informations
941 dprintf(3, "init hard drives\n");
945 // Store the device count
946 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
948 SET_BDA(disk_control_byte, 0xc0);
950 // Enable IRQ14 (handle_76)
951 unmask_pic2(PIC2_IRQ14);