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
15 #include "pci.h" // pci_find_class
21 #define NOT_BSY_NOT_DRQ 4
24 #define IDE_SECTOR_SIZE 512
25 #define CDROM_SECTOR_SIZE 2048
27 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
30 /****************************************************************
32 ****************************************************************/
34 // Wait for the specified ide state
36 await_ide(u8 when_done, u16 base, u16 timeout)
40 u8 status = inb(base+ATA_CB_STAT);
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);
56 // mod 2048 each 16 ms
57 if (time>>16 != last) {
59 dprintf(6, "await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ"
60 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
61 , when_done, time>>11, timeout);
63 if (status & ATA_CB_STAT_ERR) {
64 dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
65 ",!BSY_!DRQ,!BSY_RDY) %d status=%x time= %d timeout= %d\n"
66 , when_done, status, time>>11, timeout);
69 if (timeout == 0 || (time>>11) > timeout)
72 dprintf(1, "IDE time out\n");
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)
80 // Wait one PIO transfer cycle.
81 inb(iobase2 + ATA_CB_ASTAT);
83 return await_ide(when_done, iobase1, timeout);
86 // Delay for x nanoseconds
90 // XXX - how to implement ndelay?
95 // Wait for ide state - pause for 400ns first.
96 static __always_inline int
97 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
100 return await_ide(when_done, iobase1, timeout);
103 // Delay for x milliseconds
107 usleep(delay * 1000);
112 ata_reset(int driveid)
114 u8 channel = driveid / 2;
115 u8 slave = driveid % 2;
116 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
117 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
121 // 8.2.1 (a) -- set SRST in DC
122 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
124 // 8.2.1 (b) -- wait for BSY
125 int status = await_ide(BSY, iobase1, 20);
126 dprintf(6, "ata_reset(1) status=%x\n", status);
128 // 8.2.1 (f) -- clear SRST
129 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
131 // 8.2.1 (g) -- check for sc==sn==0x01
133 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
135 u8 sc = inb(iobase1+ATA_CB_SC);
136 u8 sn = inb(iobase1+ATA_CB_SN);
138 // For predetermined ATA drives - wait for ready.
139 if (sc==0x01 && sn==0x01) {
140 u8 type=GET_EBDA(ata.devices[driveid].type);
141 if (type == ATA_TYPE_ATA)
142 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
145 // 8.2.1 (h) -- wait for not BSY
146 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
147 dprintf(6, "ata_reset(2) status=%x\n", status);
150 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
154 /****************************************************************
156 ****************************************************************/
165 struct ata_pio_command {
180 // Send an ata command to the drive.
182 send_cmd(int driveid, struct ata_pio_command *cmd)
184 u8 channel = driveid / 2;
185 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
186 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
188 int status = inb(iobase1 + ATA_CB_STAT);
189 if (status & ATA_CB_STAT_BSY)
192 // Disable interrupts
193 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
196 u8 device = inb(iobase1 + ATA_CB_DH);
197 outb(cmd->device, iobase1 + ATA_CB_DH);
198 if ((device ^ cmd->device) & (1 << 4))
199 // Wait for device to become active.
202 if (cmd->command & 0x04) {
203 outb(0x00, iobase1 + ATA_CB_FR);
204 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
205 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
206 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
207 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
209 outb(cmd->feature, iobase1 + ATA_CB_FR);
210 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
211 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
212 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
213 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
214 outb(cmd->command, iobase1 + ATA_CB_CMD);
216 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
220 if (status & ATA_CB_STAT_ERR) {
221 dprintf(6, "send_cmd : read error\n");
224 if (!(status & ATA_CB_STAT_DRQ)) {
225 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
226 , (unsigned) status);
234 /****************************************************************
236 ****************************************************************/
238 // Read and discard x number of bytes from an io channel.
240 insx_discard(int mode, int iobase1, int bytes)
243 if (mode == ATA_MODE_PIO32) {
245 for (i=0; i<count; i++)
249 for (i=0; i<count; i++)
254 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
255 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
256 // and/or last block may be partially transferred. This function is
257 // inlined because all the callers use different forms and because the
258 // large number of parameters would consume a lot of stack space.
259 static __always_inline int
260 ata_transfer(int driveid, int iswrite, int count, int blocksize
261 , int skipfirst, int skiplast, void *far_buffer)
263 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
264 " skipf=%d skipl=%d buf=%p\n"
265 , driveid, iswrite, count, blocksize
266 , skipfirst, skiplast, far_buffer);
268 // Reset count of transferred data
269 SET_EBDA(ata.trsfsectors, 0);
271 u8 channel = driveid / 2;
272 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
273 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
274 u8 mode = GET_EBDA(ata.devices[driveid].mode);
278 int bsize = blocksize;
279 if (skipfirst && current == 0) {
280 insx_discard(mode, iobase1, skipfirst);
283 if (skiplast && current == count-1)
287 // Write data to controller
288 dprintf(16, "Write sector id=%d dest=%p\n", driveid, far_buffer);
289 if (mode == ATA_MODE_PIO32)
290 outsl_far(iobase1, far_buffer, bsize / 4);
292 outsw_far(iobase1, far_buffer, bsize / 2);
294 // Read data from controller
295 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
296 if (mode == ATA_MODE_PIO32)
297 insl_far(iobase1, far_buffer, bsize / 4);
299 insw_far(iobase1, far_buffer, bsize / 2);
303 if (skiplast && current == count-1)
304 insx_discard(mode, iobase1, skiplast);
306 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
312 SET_EBDA(ata.trsfsectors, current);
313 if (current == count)
315 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
317 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
318 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
319 , (unsigned) status);
324 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
325 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
327 status &= ~ATA_CB_STAT_DF;
328 if (status != ATA_CB_STAT_RDY ) {
329 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
330 , (unsigned) status);
335 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
340 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
342 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
343 , 0, 0, op->far_buffer);
347 ata_transfer_cdrom(const struct ata_op_s *op)
349 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
350 , 0, 0, op->far_buffer);
354 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
356 int vcount = op->count * 4 - before - after;
357 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
358 , before*512, after*512, op->far_buffer);
360 SET_EBDA(ata.trsfsectors, 0);
363 SET_EBDA(ata.trsfsectors, vcount);
368 /****************************************************************
369 * ATA hard drive functions
370 ****************************************************************/
373 send_cmd_disk(const struct ata_op_s *op, u16 command)
375 u8 slave = op->driveid % 2;
378 struct ata_pio_command cmd;
379 memset(&cmd, 0, sizeof(cmd));
381 cmd.command = command;
382 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
383 cmd.sector_count2 = op->count >> 8;
384 cmd.lba_low2 = lba >> 24;
385 cmd.lba_mid2 = lba >> 32;
386 cmd.lba_high2 = lba >> 40;
393 cmd.sector_count = op->count;
395 cmd.lba_mid = lba >> 8;
396 cmd.lba_high = lba >> 16;
397 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
398 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
400 return send_cmd(op->driveid, &cmd);
403 // Read/write count blocks from a harddrive.
405 ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
408 op.driveid = driveid;
411 op.far_buffer = far_buffer;
413 int ret = send_cmd_disk(&op, command);
417 int iswrite = command == ATA_CMD_WRITE_SECTORS;
418 return ata_transfer_disk(&op, iswrite);
422 /****************************************************************
424 ****************************************************************/
426 // Low-level atapi command transmit function.
427 static __always_inline int
428 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
430 u8 channel = driveid / 2;
431 u8 slave = driveid % 2;
432 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
433 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
435 struct ata_pio_command cmd;
436 cmd.sector_count = 0;
439 cmd.lba_mid = blocksize;
440 cmd.lba_high = blocksize >> 8;
441 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
442 cmd.command = ATA_CMD_PACKET;
444 int ret = send_cmd(driveid, &cmd);
448 // Send command to device
449 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
451 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
458 // Low-level cdrom read atapi command transmit function.
460 send_cmd_cdrom(const struct ata_op_s *op)
463 memset(atacmd, 0, sizeof(atacmd));
465 atacmd[0]=0x28; // READ command
466 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
467 atacmd[8]=(op->count & 0x00ff);
468 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
469 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
470 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
471 atacmd[5]=(op->lba & 0x000000ff);
473 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
474 , CDROM_SECTOR_SIZE);
477 // Read sectors from the cdrom.
479 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
482 op.driveid = driveid;
485 op.far_buffer = far_buffer;
487 int ret = send_cmd_cdrom(&op);
491 return ata_transfer_cdrom(&op);
494 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
497 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
499 u32 velba = vlba + vcount - 1;
501 u32 elba = velba / 4;
502 int count = elba - lba + 1;
503 int before = vlba % 4;
504 int after = 3 - (velba % 4);
507 op.driveid = driveid;
510 op.far_buffer = far_buffer;
512 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
513 " count=%d before=%d after=%d\n"
514 , driveid, vlba, vcount, far_buffer, lba, elba
515 , count, before, after);
517 int ret = send_cmd_cdrom(&op);
521 return ata_transfer_emu(&op, before, after);
524 // Send a simple atapi command to a drive.
526 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
527 , u32 length, void *far_buffer)
529 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
533 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
537 /****************************************************************
538 * ATA detect and init
539 ****************************************************************/
542 report_model(int driveid, u8 *buffer)
548 for (i=0; i<40; i+=2) {
549 model[i] = buffer[i+54+1];
550 model[i+1] = buffer[i+54];
555 for (i=39; i>0; i--) {
556 if (model[i] != 0x20)
561 u8 channel = driveid / 2;
562 u8 slave = driveid % 2;
563 // XXX - model on stack not %cs
564 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
568 get_ata_version(u8 *buffer)
570 u16 ataversion = *(u16*)&buffer[160];
572 for (version=15; version>0; version--)
573 if (ataversion & (1<<version))
579 init_drive_atapi(int driveid)
581 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATAPI);
583 // Temporary values to do the transfer
584 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
585 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
587 // Now we send a IDENTIFY command to ATAPI device
589 memset(buffer, 0, sizeof(buffer));
590 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
592 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
594 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
596 u8 type = buffer[1] & 0x1f;
597 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
598 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
599 u16 blksize = CDROM_SECTOR_SIZE;
601 SET_EBDA(ata.devices[driveid].device, type);
602 SET_EBDA(ata.devices[driveid].removable, removable);
603 SET_EBDA(ata.devices[driveid].mode, mode);
604 SET_EBDA(ata.devices[driveid].blksize, blksize);
607 u8 cdcount = GET_EBDA(ata.cdcount);
608 SET_EBDA(ata.idmap[1][cdcount], driveid);
609 SET_EBDA(ata.cdcount, ++cdcount);
611 report_model(driveid, buffer);
612 u8 version = get_ata_version(buffer);
613 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
614 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
616 printf(" ATAPI-%d Device\n", version);
620 fill_fdpt(int driveid)
625 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
626 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
627 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
629 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
630 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
631 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
633 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
634 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
635 SET_EBDA(fdpt[driveid].landing_zone, npc);
636 SET_EBDA(fdpt[driveid].cylinders, nlc);
637 SET_EBDA(fdpt[driveid].heads, nlh);
638 SET_EBDA(fdpt[driveid].sectors, nlspt);
640 if (nlc == npc && nlh == nph && nlspt == npspt)
641 // no logical CHS mapping used, just physical CHS
642 // use Standard Fixed Disk Parameter Table (FDPT)
645 // complies with Phoenix style Translated Fixed Disk Parameter
647 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
648 SET_EBDA(fdpt[driveid].phys_heads, nph);
649 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
650 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
652 // Checksum structure.
653 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
655 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
656 , fdpt[driveid]) - 1);
657 SET_EBDA(fdpt[driveid].checksum, -sum);
661 get_translation(int driveid)
663 if (! CONFIG_COREBOOT) {
664 // Emulators pass in the translation info via nvram.
665 u8 channel = driveid / 2;
666 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
667 translation >>= 2 * (driveid % 4);
672 // On COREBOOT, use a heuristic to determine translation type.
673 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
674 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
675 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
677 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
678 return ATA_TRANSLATION_NONE;
679 if (cylinders * heads <= 131072)
680 return ATA_TRANSLATION_LARGE;
681 return ATA_TRANSLATION_LBA;
685 setup_translation(int driveid)
687 u8 translation = get_translation(driveid);
688 SET_EBDA(ata.devices[driveid].translation, translation);
690 u8 channel = driveid / 2;
691 u8 slave = driveid % 2;
692 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
693 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
694 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
695 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
697 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
698 , channel, slave, cylinders, heads, spt);
699 switch (translation) {
700 case ATA_TRANSLATION_NONE:
703 case ATA_TRANSLATION_LBA:
706 if (sectors > 63*255*1024) {
711 u32 sect = (u32)sectors / 63;
723 cylinders = sect / heads;
725 case ATA_TRANSLATION_RECHS:
726 dprintf(1, "r-echs");
727 // Take care not to overflow
732 cylinders = (u16)((u32)(cylinders)*16/15);
734 // then go through the large bitshift process
735 case ATA_TRANSLATION_LARGE:
736 if (translation == ATA_TRANSLATION_LARGE)
738 while (cylinders > 1024) {
742 // If we max out the head count
748 // clip to 1024 cylinders in lchs
749 if (cylinders > 1024)
751 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
753 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
754 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
755 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
759 init_drive_ata(int driveid)
761 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
763 // Temporary values to do the transfer
764 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
765 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
767 // Now we send a IDENTIFY command to ATA device
769 memset(buffer, 0, sizeof(buffer));
770 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
772 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
774 BX_PANIC("ata-detect: Failed to detect ATA device\n");
776 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
777 u8 mode = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
778 u16 blksize = IDE_SECTOR_SIZE;
780 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
781 u16 heads = *(u16*)&buffer[3*2]; // word 3
782 u16 spt = *(u16*)&buffer[6*2]; // word 6
785 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
786 sectors = *(u64*)&buffer[100*2]; // word 100-103
788 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
790 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
791 SET_EBDA(ata.devices[driveid].removable, removable);
792 SET_EBDA(ata.devices[driveid].mode, mode);
793 SET_EBDA(ata.devices[driveid].blksize, blksize);
794 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
795 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
796 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
797 SET_EBDA(ata.devices[driveid].sectors, sectors);
799 // Setup disk geometry translation.
800 setup_translation(driveid);
803 u8 hdcount = GET_EBDA(ata.hdcount);
804 SET_EBDA(ata.idmap[0][hdcount], driveid);
805 SET_EBDA(ata.hdcount, ++hdcount);
807 // Fill "fdpt" structure.
810 // Report drive info to user.
811 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
812 report_model(driveid, buffer);
813 u8 version = get_ata_version(buffer);
814 if (sizeinmb < (1 << 16))
815 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
817 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
818 , (u32)(sizeinmb >> 10));
822 init_drive_unknown(int driveid)
824 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_UNKNOWN);
826 u8 channel = driveid / 2;
827 u8 slave = driveid % 2;
828 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
836 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
837 u8 channel = driveid / 2;
838 u8 slave = driveid % 2;
840 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
841 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
845 // Disable interrupts
846 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
849 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
851 outb(0x55, iobase1+ATA_CB_SC);
852 outb(0xaa, iobase1+ATA_CB_SN);
853 outb(0xaa, iobase1+ATA_CB_SC);
854 outb(0x55, iobase1+ATA_CB_SN);
855 outb(0x55, iobase1+ATA_CB_SC);
856 outb(0xaa, iobase1+ATA_CB_SN);
858 // If we found something
859 u8 sc = inb(iobase1+ATA_CB_SC);
860 u8 sn = inb(iobase1+ATA_CB_SN);
861 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
863 if (sc != 0x55 || sn != 0xaa)
869 // check for ATA or ATAPI
870 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
872 sc = inb(iobase1+ATA_CB_SC);
873 sn = inb(iobase1+ATA_CB_SN);
874 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
875 if (sc!=0x01 || sn!=0x01) {
876 init_drive_unknown(driveid);
879 u8 cl = inb(iobase1+ATA_CB_CL);
880 u8 ch = inb(iobase1+ATA_CB_CH);
881 u8 st = inb(iobase1+ATA_CB_STAT);
882 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
883 , driveid, sc, sn, cl, ch, st);
885 if (cl==0x14 && ch==0xeb)
886 init_drive_atapi(driveid);
887 else if (cl==0x00 && ch==0x00 && st!=0x00)
888 init_drive_ata(driveid);
889 else if (cl==0xff && ch==0xff)
893 init_drive_unknown(driveid);
902 // hdidmap and cdidmap init.
904 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
905 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
906 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
909 // Scan PCI bus for ATA adapters
910 int count=0, index=0;
911 u16 classid = 0x0180; // SATA first
912 while (count<CONFIG_MAX_ATA_INTERFACES-1) {
914 int ret = pci_find_class(classid, index, &d);
916 if (classid == 0x0101)
919 classid = 0x0101; // PATA controllers
925 u8 irq = pci_config_readb(d, PCI_INTERRUPT_LINE);
926 SET_EBDA(ata.channels[count].irq, irq);
927 SET_EBDA(ata.channels[count].pci_bdf, pci_to_bdf(d));
929 u8 prog_if = pci_config_readb(d, PCI_CLASS_PROG);
932 if (classid != 0x0101 || prog_if & 1) {
933 port1 = pci_config_readl(d, PCI_BASE_ADDR_0) & ~3;
934 port2 = pci_config_readl(d, PCI_BASE_ADDR_1) & ~3;
939 SET_EBDA(ata.channels[count].iobase1, port1);
940 SET_EBDA(ata.channels[count].iobase2, port2);
941 dprintf(1, "ATA controller %d at %x/%x\n", count, port1, port2);
944 if (classid != 0x0101 || prog_if & 4) {
945 port1 = pci_config_readl(d, PCI_BASE_ADDR_2) & ~3;
946 port2 = pci_config_readl(d, PCI_BASE_ADDR_3) & ~3;
951 dprintf(1, "ATA controller %d at %x/%x\n", count, port1, port2);
952 SET_EBDA(ata.channels[count].iobase1, port1);
953 SET_EBDA(ata.channels[count].iobase2, port2);
964 dprintf(3, "init hard drives\n");
968 // Store the device count
969 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
971 SET_BDA(disk_control_byte, 0xc0);
973 // Enable IRQ14 (handle_76)
974 unmask_pic2(PIC2_IRQ14);