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
19 #define NOT_BSY_NOT_DRQ 4
22 #define IDE_SECTOR_SIZE 512
23 #define CDROM_SECTOR_SIZE 2048
25 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
27 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
28 #define DEBUGF(fmt, args...)
31 /****************************************************************
33 ****************************************************************/
35 // Wait for the specified ide state
37 await_ide(u8 when_done, u16 base, u16 timeout)
41 u8 status = inb(base+ATA_CB_STAT);
45 result = status & ATA_CB_STAT_BSY;
46 else if (when_done == NOT_BSY)
47 result = !(status & ATA_CB_STAT_BSY);
48 else if (when_done == NOT_BSY_DRQ)
49 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
50 else if (when_done == NOT_BSY_NOT_DRQ)
51 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
52 else if (when_done == NOT_BSY_RDY)
53 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
57 // mod 2048 each 16 ms
58 if (time>>16 != last) {
60 DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)"
61 " %d time= %d timeout= %d\n"
62 , when_done, time>>11, timeout);
64 if (status & ATA_CB_STAT_ERR) {
65 DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
66 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
67 , when_done, time>>11, timeout);
70 if (timeout == 0 || (time>>11) > timeout)
73 dprintf(1, "IDE time out\n");
77 // Wait for ide state - pauses for one ata cycle first.
78 static __always_inline int
79 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
81 // Wait one PIO transfer cycle.
82 inb(iobase2 + ATA_CB_ASTAT);
84 return await_ide(when_done, iobase1, timeout);
87 // Delay for x nanoseconds
91 // XXX - how to implement ndelay?
96 // Wait for ide state - pause for 400ns first.
97 static __always_inline int
98 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
101 return await_ide(when_done, iobase1, timeout);
104 // Delay for x milliseconds
108 usleep(delay * 1000);
113 ata_reset(int driveid)
115 u8 channel = driveid / 2;
116 u8 slave = driveid % 2;
117 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
118 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
122 // 8.2.1 (a) -- set SRST in DC
123 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
125 // 8.2.1 (b) -- wait for BSY
126 int status = await_ide(BSY, iobase1, 20);
127 dprintf(6, "ata_reset(1) status=%x\n", status);
129 // 8.2.1 (f) -- clear SRST
130 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
132 // 8.2.1 (g) -- check for sc==sn==0x01
134 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
136 u8 sc = inb(iobase1+ATA_CB_SC);
137 u8 sn = inb(iobase1+ATA_CB_SN);
139 // For predetermined ATA drives - wait for ready.
140 if (sc==0x01 && sn==0x01) {
141 u8 type=GET_EBDA(ata.devices[driveid].type);
142 if (type == ATA_TYPE_ATA)
143 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
146 // 8.2.1 (h) -- wait for not BSY
147 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
148 dprintf(6, "ata_reset(2) status=%x\n", status);
151 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
155 /****************************************************************
157 ****************************************************************/
166 struct ata_pio_command {
181 // Send an ata command to the drive.
183 send_cmd(int driveid, struct ata_pio_command *cmd)
185 u8 channel = driveid / 2;
186 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
187 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
189 int status = inb(iobase1 + ATA_CB_STAT);
190 if (status & ATA_CB_STAT_BSY)
193 // Disable interrupts
194 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
197 u8 device = inb(iobase1 + ATA_CB_DH);
198 outb(cmd->device, iobase1 + ATA_CB_DH);
199 if ((device ^ cmd->device) & (1UL << 4))
200 // Wait for device to become active.
203 if (cmd->command & 0x04) {
204 outb(0x00, iobase1 + ATA_CB_FR);
205 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
206 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
207 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
208 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
210 outb(cmd->feature, iobase1 + ATA_CB_FR);
211 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
212 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
213 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
214 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
215 outb(cmd->command, iobase1 + ATA_CB_CMD);
217 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
221 if (status & ATA_CB_STAT_ERR) {
222 DEBUGF("send_cmd : read error\n");
225 if (!(status & ATA_CB_STAT_DRQ)) {
226 DEBUGF("send_cmd : DRQ not set (status %02x)\n"
227 , (unsigned) status);
235 /****************************************************************
237 ****************************************************************/
239 // Read and discard x number of bytes from an io channel.
241 insx_discard(int mode, int iobase1, int bytes)
244 if (mode == ATA_MODE_PIO32) {
246 for (i=0; i<count; i++)
250 for (i=0; i<count; i++)
255 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
256 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
257 // and/or last block may be partially transferred. This function is
258 // inlined because all the callers use different forms and because the
259 // large number of parameters would consume a lot of stack space.
260 static __always_inline int
261 ata_transfer(int driveid, int iswrite, int count, int blocksize
262 , int skipfirst, int skiplast, void *far_buffer)
264 DEBUGF("ata_transfer id=%d write=%d count=%d bs=%d"
265 " skipf=%d skipl=%d buf=%p\n"
266 , driveid, iswrite, count, blocksize
267 , skipfirst, skiplast, far_buffer);
269 // Reset count of transferred data
270 SET_EBDA(ata.trsfsectors, 0);
272 u8 channel = driveid / 2;
273 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
274 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
275 u8 mode = GET_EBDA(ata.devices[driveid].mode);
279 int bsize = blocksize;
280 if (skipfirst && current == 0) {
281 insx_discard(mode, iobase1, skipfirst);
284 if (skiplast && current == count-1)
288 // Write data to controller
289 DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer);
290 if (mode == ATA_MODE_PIO32)
291 outsl_far(iobase1, far_buffer, bsize / 4);
293 outsw_far(iobase1, far_buffer, bsize / 2);
295 // Read data from controller
296 DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer);
297 if (mode == ATA_MODE_PIO32)
298 insl_far(iobase1, far_buffer, bsize / 4);
300 insw_far(iobase1, far_buffer, bsize / 2);
304 if (skiplast && current == count-1)
305 insx_discard(mode, iobase1, skiplast);
307 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
313 SET_EBDA(ata.trsfsectors, current);
314 if (current == count)
316 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
318 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
319 DEBUGF("ata_transfer : more sectors left (status %02x)\n"
320 , (unsigned) status);
325 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
326 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
328 status &= ~ATA_CB_STAT_DF;
329 if (status != ATA_CB_STAT_RDY ) {
330 DEBUGF("ata_transfer : no sectors left (status %02x)\n"
331 , (unsigned) status);
336 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
341 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
343 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
344 , 0, 0, op->far_buffer);
348 ata_transfer_cdrom(const struct ata_op_s *op)
350 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
351 , 0, 0, op->far_buffer);
355 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
357 int vcount = op->count * 4 - before - after;
358 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
359 , before*512, after*512, op->far_buffer);
361 SET_EBDA(ata.trsfsectors, 0);
364 SET_EBDA(ata.trsfsectors, vcount);
369 /****************************************************************
370 * ATA hard drive functions
371 ****************************************************************/
374 send_cmd_disk(const struct ata_op_s *op, u16 command)
376 u8 slave = op->driveid % 2;
379 struct ata_pio_command cmd;
380 memset(&cmd, 0, sizeof(cmd));
382 cmd.command = command;
383 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
384 cmd.sector_count2 = op->count >> 8;
385 cmd.lba_low2 = lba >> 24;
386 cmd.lba_mid2 = lba >> 32;
387 cmd.lba_high2 = lba >> 40;
394 cmd.sector_count = op->count;
396 cmd.lba_mid = lba >> 8;
397 cmd.lba_high = lba >> 16;
398 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
399 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
401 return send_cmd(op->driveid, &cmd);
404 // Read/write count blocks from a harddrive.
406 ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
409 op.driveid = driveid;
412 op.far_buffer = far_buffer;
414 int ret = send_cmd_disk(&op, command);
418 int iswrite = command == ATA_CMD_WRITE_SECTORS;
419 return ata_transfer_disk(&op, iswrite);
423 /****************************************************************
425 ****************************************************************/
427 // Low-level atapi command transmit function.
428 static __always_inline int
429 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
431 u8 channel = driveid / 2;
432 u8 slave = driveid % 2;
433 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
434 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
436 struct ata_pio_command cmd;
437 cmd.sector_count = 0;
440 cmd.lba_mid = blocksize;
441 cmd.lba_high = blocksize >> 8;
442 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
443 cmd.command = ATA_CMD_PACKET;
445 int ret = send_cmd(driveid, &cmd);
449 // Send command to device
450 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
452 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
459 // Low-level cdrom read atapi command transmit function.
461 send_cmd_cdrom(const struct ata_op_s *op)
464 memset(atacmd, 0, sizeof(atacmd));
466 atacmd[0]=0x28; // READ command
467 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
468 atacmd[8]=(op->count & 0x00ff);
469 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
470 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
471 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
472 atacmd[5]=(op->lba & 0x000000ff);
474 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
475 , CDROM_SECTOR_SIZE);
478 // Read sectors from the cdrom.
480 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
483 op.driveid = driveid;
486 op.far_buffer = far_buffer;
488 int ret = send_cmd_cdrom(&op);
492 return ata_transfer_cdrom(&op);
495 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
498 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
500 u32 velba = vlba + vcount - 1;
502 u32 elba = velba / 4;
503 int count = elba - lba + 1;
504 int before = vlba % 4;
505 int after = 3 - (velba % 4);
508 op.driveid = driveid;
511 op.far_buffer = far_buffer;
513 DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
514 " count=%d before=%d after=%d\n"
515 , driveid, vlba, vcount, far_buffer, lba, elba
516 , count, before, after);
518 int ret = send_cmd_cdrom(&op);
522 return ata_transfer_emu(&op, before, after);
525 // Send a simple atapi command to a drive.
527 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
528 , u32 length, void *far_buffer)
530 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
534 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
538 /****************************************************************
539 * ATA detect and init
540 ****************************************************************/
543 report_model(int driveid, u8 *buffer)
549 for (i=0; i<40; i+=2) {
550 model[i] = buffer[i+54+1];
551 model[i+1] = buffer[i+54];
556 for (i=39; i>0; i--) {
557 if (model[i] != 0x20)
562 u8 channel = driveid / 2;
563 u8 slave = driveid % 2;
564 // XXX - model on stack not %cs
565 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
569 get_ata_version(u8 *buffer)
571 u16 ataversion = *(u16*)&buffer[160];
573 for (version=15; version>0; version--)
574 if (ataversion & (1<<version))
580 init_drive_atapi(int driveid)
582 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATAPI);
584 // Temporary values to do the transfer
585 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
586 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
588 // Now we send a IDENTIFY command to ATAPI device
590 memset(buffer, 0, sizeof(buffer));
591 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
593 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
595 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
597 u8 type = buffer[1] & 0x1f;
598 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
599 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
600 u16 blksize = CDROM_SECTOR_SIZE;
602 SET_EBDA(ata.devices[driveid].device, type);
603 SET_EBDA(ata.devices[driveid].removable, removable);
604 SET_EBDA(ata.devices[driveid].mode, mode);
605 SET_EBDA(ata.devices[driveid].blksize, blksize);
608 u8 cdcount = GET_EBDA(ata.cdcount);
609 SET_EBDA(ata.idmap[1][cdcount], driveid);
610 SET_EBDA(ata.cdcount, ++cdcount);
612 report_model(driveid, buffer);
613 u8 version = get_ata_version(buffer);
614 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
615 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
617 printf(" ATAPI-%d Device\n", version);
621 fill_fdpt(int driveid)
626 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
627 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
628 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
630 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
631 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
632 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
634 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
635 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
636 SET_EBDA(fdpt[driveid].landing_zone, npc);
637 SET_EBDA(fdpt[driveid].cylinders, nlc);
638 SET_EBDA(fdpt[driveid].heads, nlh);
639 SET_EBDA(fdpt[driveid].sectors, nlspt);
641 if (nlc == npc && nlh == nph && nlspt == npspt)
642 // no logical CHS mapping used, just physical CHS
643 // use Standard Fixed Disk Parameter Table (FDPT)
646 // complies with Phoenix style Translated Fixed Disk Parameter
648 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
649 SET_EBDA(fdpt[driveid].phys_heads, nph);
650 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
651 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
653 // Checksum structure.
654 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
656 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
657 , fdpt[driveid]) - 1);
658 SET_EBDA(fdpt[driveid].checksum, -sum);
662 get_translation(int driveid)
664 if (! CONFIG_COREBOOT) {
665 // Emulators pass in the translation info via nvram.
666 u8 channel = driveid / 2;
667 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
668 translation >>= 2 * (driveid % 4);
673 // On COREBOOT, use a heuristic to determine translation type.
674 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
675 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
676 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
678 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
679 return ATA_TRANSLATION_NONE;
680 if (cylinders * heads <= 131072)
681 return ATA_TRANSLATION_LARGE;
682 return ATA_TRANSLATION_LBA;
686 setup_translation(int driveid)
688 u8 translation = get_translation(driveid);
689 SET_EBDA(ata.devices[driveid].translation, translation);
691 u8 channel = driveid / 2;
692 u8 slave = driveid % 2;
693 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
694 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
695 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
696 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
698 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
699 , channel, slave, cylinders, heads, spt);
700 switch (translation) {
701 case ATA_TRANSLATION_NONE:
704 case ATA_TRANSLATION_LBA:
707 if (sectors > 63*255*1024) {
712 u32 sect = (u32)sectors / 63;
724 cylinders = sect / heads;
726 case ATA_TRANSLATION_RECHS:
727 dprintf(1, "r-echs");
728 // Take care not to overflow
733 cylinders = (u16)((u32)(cylinders)*16/15);
735 // then go through the large bitshift process
736 case ATA_TRANSLATION_LARGE:
737 if (translation == ATA_TRANSLATION_LARGE)
739 while (cylinders > 1024) {
743 // If we max out the head count
749 // clip to 1024 cylinders in lchs
750 if (cylinders > 1024)
752 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
754 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
755 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
756 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
760 init_drive_ata(int driveid)
762 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
764 // Temporary values to do the transfer
765 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
766 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
768 // Now we send a IDENTIFY command to ATA device
770 memset(buffer, 0, sizeof(buffer));
771 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
773 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
775 BX_PANIC("ata-detect: Failed to detect ATA device\n");
777 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
778 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
779 u16 blksize = *(u16*)&buffer[10];
781 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
782 u16 heads = *(u16*)&buffer[3*2]; // word 3
783 u16 spt = *(u16*)&buffer[6*2]; // word 6
786 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
787 sectors = *(u64*)&buffer[100*2]; // word 100-103
789 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
791 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
792 SET_EBDA(ata.devices[driveid].removable, removable);
793 SET_EBDA(ata.devices[driveid].mode, mode);
794 SET_EBDA(ata.devices[driveid].blksize, blksize);
795 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
796 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
797 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
798 SET_EBDA(ata.devices[driveid].sectors, sectors);
800 // Setup disk geometry translation.
801 setup_translation(driveid);
804 u8 hdcount = GET_EBDA(ata.hdcount);
805 SET_EBDA(ata.idmap[0][hdcount], driveid);
806 SET_EBDA(ata.hdcount, ++hdcount);
808 // Fill "fdpt" structure.
811 // Report drive info to user.
812 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
813 report_model(driveid, buffer);
814 u8 version = get_ata_version(buffer);
815 if (sizeinmb < (1 << 16))
816 printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb);
818 printf(" ATA-%d Hard-Disk (%u GBytes)\n", version
819 , (u32)(sizeinmb >> 10));
823 init_drive_unknown(int driveid)
825 SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN);
827 u8 channel = driveid / 2;
828 u8 slave = driveid % 2;
829 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
837 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
838 u8 channel = driveid / 2;
839 u8 slave = driveid % 2;
841 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
842 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
844 // Disable interrupts
845 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
848 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
850 outb(0x55, iobase1+ATA_CB_SC);
851 outb(0xaa, iobase1+ATA_CB_SN);
852 outb(0xaa, iobase1+ATA_CB_SC);
853 outb(0x55, iobase1+ATA_CB_SN);
854 outb(0x55, iobase1+ATA_CB_SC);
855 outb(0xaa, iobase1+ATA_CB_SN);
857 // If we found something
858 u8 sc = inb(iobase1+ATA_CB_SC);
859 u8 sn = inb(iobase1+ATA_CB_SN);
860 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
862 if (sc != 0x55 || sn != 0xaa)
868 // check for ATA or ATAPI
869 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
871 sc = inb(iobase1+ATA_CB_SC);
872 sn = inb(iobase1+ATA_CB_SN);
873 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
874 if (sc!=0x01 || sn!=0x01) {
875 init_drive_unknown(driveid);
878 u8 cl = inb(iobase1+ATA_CB_CL);
879 u8 ch = inb(iobase1+ATA_CB_CH);
880 u8 st = inb(iobase1+ATA_CB_STAT);
881 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
882 , driveid, sc, sn, cl, ch, st);
884 if (cl==0x14 && ch==0xeb)
885 init_drive_atapi(driveid);
886 else if (cl==0x00 && ch==0x00 && st!=0x00)
887 init_drive_ata(driveid);
888 else if (cl==0xff && ch==0xff)
892 init_drive_unknown(driveid);
901 // hdidmap and cdidmap init.
903 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
904 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
905 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
908 #if CONFIG_MAX_ATA_INTERFACES > 0
909 SET_EBDA(ata.channels[0].iface, ATA_IFACE_ISA);
910 SET_EBDA(ata.channels[0].iobase1, 0x1f0);
911 SET_EBDA(ata.channels[0].iobase2, 0x3f0);
912 SET_EBDA(ata.channels[0].irq, 14);
914 #if CONFIG_MAX_ATA_INTERFACES > 1
915 SET_EBDA(ata.channels[1].iface, ATA_IFACE_ISA);
916 SET_EBDA(ata.channels[1].iobase1, 0x170);
917 SET_EBDA(ata.channels[1].iobase2, 0x370);
918 SET_EBDA(ata.channels[1].irq, 15);
920 #if CONFIG_MAX_ATA_INTERFACES > 2
921 SET_EBDA(ata.channels[2].iface, ATA_IFACE_ISA);
922 SET_EBDA(ata.channels[2].iobase1, 0x1e8);
923 SET_EBDA(ata.channels[2].iobase2, 0x3e0);
924 SET_EBDA(ata.channels[2].irq, 12);
926 #if CONFIG_MAX_ATA_INTERFACES > 3
927 SET_EBDA(ata.channels[3].iface, ATA_IFACE_ISA);
928 SET_EBDA(ata.channels[3].iobase1, 0x168);
929 SET_EBDA(ata.channels[3].iobase2, 0x360);
930 SET_EBDA(ata.channels[3].irq, 11);
932 #if CONFIG_MAX_ATA_INTERFACES > 4
933 #error Please fill the ATA interface informations
943 dprintf(3, "init hard drives\n");
947 // Store the device count
948 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
950 SET_BDA(disk_control_byte, 0xc0);
952 // Enable IRQ14 (handle_76)
953 unmask_pic2(PIC2_IRQ14);