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" // enable_hwirq
14 #include "biosvar.h" // GET_EBDA
15 #include "pci.h" // pci_find_class
16 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
17 #include "pci_regs.h" // PCI_INTERRUPT_LINE
23 #define NOT_BSY_NOT_DRQ 4
26 #define IDE_SECTOR_SIZE 512
27 #define CDROM_SECTOR_SIZE 2048
29 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
32 /****************************************************************
34 ****************************************************************/
36 // Wait for the specified ide state
38 await_ide(u8 when_done, u16 base, u16 timeout)
42 u8 status = inb(base+ATA_CB_STAT);
46 result = status & ATA_CB_STAT_BSY;
47 else if (when_done == NOT_BSY)
48 result = !(status & ATA_CB_STAT_BSY);
49 else if (when_done == NOT_BSY_DRQ)
50 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
51 else if (when_done == NOT_BSY_NOT_DRQ)
52 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
53 else if (when_done == NOT_BSY_RDY)
54 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
58 // mod 2048 each 16 ms
59 if (time>>16 != last) {
61 dprintf(6, "await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ"
62 ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n"
63 , when_done, time>>11, timeout);
65 if (status & ATA_CB_STAT_ERR) {
66 dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
67 ",!BSY_!DRQ,!BSY_RDY) %d status=%x time= %d timeout= %d\n"
68 , when_done, status, time>>11, timeout);
71 if (timeout == 0 || (time>>11) > timeout)
74 dprintf(1, "IDE time out\n");
78 // Wait for ide state - pauses for one ata cycle first.
79 static __always_inline int
80 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
82 // Wait one PIO transfer cycle.
83 inb(iobase2 + ATA_CB_ASTAT);
85 return await_ide(when_done, iobase1, timeout);
88 // Wait for ide state - pause for 400ns first.
89 static __always_inline int
90 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
93 return await_ide(when_done, iobase1, timeout);
98 ata_reset(int driveid)
100 u8 channel = driveid / 2;
101 u8 slave = driveid % 2;
102 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
103 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
107 // 8.2.1 (a) -- set SRST in DC
108 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
110 // 8.2.1 (b) -- wait for BSY
111 int status = await_ide(BSY, iobase1, 20);
112 dprintf(6, "ata_reset(1) status=%x\n", status);
114 // 8.2.1 (f) -- clear SRST
115 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
117 // 8.2.1 (g) -- check for sc==sn==0x01
119 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
121 u8 sc = inb(iobase1+ATA_CB_SC);
122 u8 sn = inb(iobase1+ATA_CB_SN);
124 // For predetermined ATA drives - wait for ready.
125 if (sc==0x01 && sn==0x01) {
126 u8 type=GET_EBDA(ata.devices[driveid].type);
127 if (type == ATA_TYPE_ATA)
128 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
131 // 8.2.1 (h) -- wait for not BSY
132 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
133 dprintf(6, "ata_reset(2) status=%x\n", status);
136 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
140 /****************************************************************
142 ****************************************************************/
151 struct ata_pio_command {
166 // Send an ata command to the drive.
168 send_cmd(int driveid, struct ata_pio_command *cmd)
170 u8 channel = driveid / 2;
171 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
172 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
174 int status = inb(iobase1 + ATA_CB_STAT);
175 if (status & ATA_CB_STAT_BSY)
178 // Disable interrupts
179 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
182 u8 device = inb(iobase1 + ATA_CB_DH);
183 outb(cmd->device, iobase1 + ATA_CB_DH);
184 if ((device ^ cmd->device) & (1 << 4))
185 // Wait for device to become active.
188 if (cmd->command & 0x04) {
189 outb(0x00, iobase1 + ATA_CB_FR);
190 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
191 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
192 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
193 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
195 outb(cmd->feature, iobase1 + ATA_CB_FR);
196 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
197 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
198 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
199 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
200 outb(cmd->command, iobase1 + ATA_CB_CMD);
202 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
206 if (status & ATA_CB_STAT_ERR) {
207 dprintf(6, "send_cmd : read error\n");
210 if (!(status & ATA_CB_STAT_DRQ)) {
211 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
212 , (unsigned) status);
220 /****************************************************************
222 ****************************************************************/
224 // Read and discard x number of bytes from an io channel.
226 insx_discard(int mode, int iobase1, int bytes)
229 if (mode == ATA_MODE_PIO32) {
231 for (i=0; i<count; i++)
235 for (i=0; i<count; i++)
240 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
241 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
242 // and/or last block may be partially transferred. This function is
243 // inlined because all the callers use different forms and because the
244 // large number of parameters would consume a lot of stack space.
245 static __always_inline int
246 ata_transfer(int driveid, int iswrite, int count, int blocksize
247 , int skipfirst, int skiplast, void *far_buffer)
249 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
250 " skipf=%d skipl=%d buf=%p\n"
251 , driveid, iswrite, count, blocksize
252 , skipfirst, skiplast, far_buffer);
254 // Reset count of transferred data
255 SET_EBDA(ata.trsfsectors, 0);
257 u8 channel = driveid / 2;
258 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
259 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
260 u8 mode = GET_EBDA(ata.devices[driveid].mode);
264 int bsize = blocksize;
265 if (skipfirst && current == 0) {
266 insx_discard(mode, iobase1, skipfirst);
269 if (skiplast && current == count-1)
273 // Write data to controller
274 dprintf(16, "Write sector id=%d dest=%p\n", driveid, far_buffer);
275 if (mode == ATA_MODE_PIO32)
276 outsl_far(iobase1, far_buffer, bsize / 4);
278 outsw_far(iobase1, far_buffer, bsize / 2);
280 // Read data from controller
281 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
282 if (mode == ATA_MODE_PIO32)
283 insl_far(iobase1, far_buffer, bsize / 4);
285 insw_far(iobase1, far_buffer, bsize / 2);
289 if (skiplast && current == count-1)
290 insx_discard(mode, iobase1, skiplast);
292 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
298 SET_EBDA(ata.trsfsectors, current);
299 if (current == count)
301 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
303 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
304 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
305 , (unsigned) status);
310 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
311 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
313 status &= ~ATA_CB_STAT_DF;
314 if (status != ATA_CB_STAT_RDY ) {
315 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
316 , (unsigned) status);
321 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
326 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
328 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
329 , 0, 0, op->far_buffer);
333 ata_transfer_cdrom(const struct ata_op_s *op)
335 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
336 , 0, 0, op->far_buffer);
340 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
342 int vcount = op->count * 4 - before - after;
343 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
344 , before*512, after*512, op->far_buffer);
346 SET_EBDA(ata.trsfsectors, 0);
349 SET_EBDA(ata.trsfsectors, vcount);
354 /****************************************************************
355 * ATA hard drive functions
356 ****************************************************************/
359 send_cmd_disk(const struct ata_op_s *op, u16 command)
361 u8 slave = op->driveid % 2;
364 struct ata_pio_command cmd;
365 memset(&cmd, 0, sizeof(cmd));
367 cmd.command = command;
368 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
369 cmd.sector_count2 = op->count >> 8;
370 cmd.lba_low2 = lba >> 24;
371 cmd.lba_mid2 = lba >> 32;
372 cmd.lba_high2 = lba >> 40;
379 cmd.sector_count = op->count;
381 cmd.lba_mid = lba >> 8;
382 cmd.lba_high = lba >> 16;
383 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
384 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
386 return send_cmd(op->driveid, &cmd);
389 // Read/write count blocks from a harddrive.
391 ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
394 op.driveid = driveid;
397 op.far_buffer = far_buffer;
399 int ret = send_cmd_disk(&op, command);
403 int iswrite = command == ATA_CMD_WRITE_SECTORS;
404 return ata_transfer_disk(&op, iswrite);
408 /****************************************************************
410 ****************************************************************/
412 // Low-level atapi command transmit function.
413 static __always_inline int
414 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
416 u8 channel = driveid / 2;
417 u8 slave = driveid % 2;
418 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
419 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
421 struct ata_pio_command cmd;
422 cmd.sector_count = 0;
425 cmd.lba_mid = blocksize;
426 cmd.lba_high = blocksize >> 8;
427 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
428 cmd.command = ATA_CMD_PACKET;
430 int ret = send_cmd(driveid, &cmd);
434 // Send command to device
435 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
437 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
444 // Low-level cdrom read atapi command transmit function.
446 send_cmd_cdrom(const struct ata_op_s *op)
449 memset(atacmd, 0, sizeof(atacmd));
451 atacmd[0]=0x28; // READ command
452 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
453 atacmd[8]=(op->count & 0x00ff);
454 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
455 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
456 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
457 atacmd[5]=(op->lba & 0x000000ff);
459 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
460 , CDROM_SECTOR_SIZE);
463 // Read sectors from the cdrom.
465 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
468 op.driveid = driveid;
471 op.far_buffer = far_buffer;
473 int ret = send_cmd_cdrom(&op);
477 return ata_transfer_cdrom(&op);
480 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
483 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
485 u32 velba = vlba + vcount - 1;
487 u32 elba = velba / 4;
488 int count = elba - lba + 1;
489 int before = vlba % 4;
490 int after = 3 - (velba % 4);
493 op.driveid = driveid;
496 op.far_buffer = far_buffer;
498 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
499 " count=%d before=%d after=%d\n"
500 , driveid, vlba, vcount, far_buffer, lba, elba
501 , count, before, after);
503 int ret = send_cmd_cdrom(&op);
507 return ata_transfer_emu(&op, before, after);
510 // Send a simple atapi command to a drive.
512 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
513 , u32 length, void *far_buffer)
515 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
519 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
523 /****************************************************************
524 * ATA detect and init
525 ****************************************************************/
528 report_model(int driveid, u8 *buffer)
534 for (i=0; i<40; i+=2) {
535 model[i] = buffer[i+54+1];
536 model[i+1] = buffer[i+54];
541 for (i=39; i>0; i--) {
542 if (model[i] != 0x20)
547 u8 channel = driveid / 2;
548 u8 slave = driveid % 2;
549 // XXX - model on stack not %cs
550 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
554 get_ata_version(u8 *buffer)
556 u16 ataversion = *(u16*)&buffer[160];
558 for (version=15; version>0; version--)
559 if (ataversion & (1<<version))
565 init_drive_atapi(int driveid)
567 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATAPI);
569 // Temporary values to do the transfer
570 SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_CDROM);
571 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
573 // Now we send a IDENTIFY command to ATAPI device
575 memset(buffer, 0, sizeof(buffer));
576 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
578 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
580 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
582 u8 type = buffer[1] & 0x1f;
583 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
584 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
585 u16 blksize = CDROM_SECTOR_SIZE;
587 SET_EBDA(ata.devices[driveid].device, type);
588 SET_EBDA(ata.devices[driveid].removable, removable);
589 SET_EBDA(ata.devices[driveid].mode, mode);
590 SET_EBDA(ata.devices[driveid].blksize, blksize);
593 u8 cdcount = GET_EBDA(ata.cdcount);
594 SET_EBDA(ata.idmap[1][cdcount], driveid);
595 SET_EBDA(ata.cdcount, ++cdcount);
597 report_model(driveid, buffer);
598 u8 version = get_ata_version(buffer);
599 if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM)
600 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
602 printf(" ATAPI-%d Device\n", version);
606 fill_fdpt(int driveid)
611 u16 nlc = GET_EBDA(ata.devices[driveid].lchs.cylinders);
612 u16 nlh = GET_EBDA(ata.devices[driveid].lchs.heads);
613 u16 nlspt = GET_EBDA(ata.devices[driveid].lchs.spt);
615 u16 npc = GET_EBDA(ata.devices[driveid].pchs.cylinders);
616 u16 nph = GET_EBDA(ata.devices[driveid].pchs.heads);
617 u16 npspt = GET_EBDA(ata.devices[driveid].pchs.spt);
619 SET_EBDA(fdpt[driveid].precompensation, 0xffff);
620 SET_EBDA(fdpt[driveid].drive_control_byte, 0xc0 | ((nph > 8) << 3));
621 SET_EBDA(fdpt[driveid].landing_zone, npc);
622 SET_EBDA(fdpt[driveid].cylinders, nlc);
623 SET_EBDA(fdpt[driveid].heads, nlh);
624 SET_EBDA(fdpt[driveid].sectors, nlspt);
626 if (nlc == npc && nlh == nph && nlspt == npspt)
627 // no logical CHS mapping used, just physical CHS
628 // use Standard Fixed Disk Parameter Table (FDPT)
631 // complies with Phoenix style Translated Fixed Disk Parameter
633 SET_EBDA(fdpt[driveid].phys_cylinders, npc);
634 SET_EBDA(fdpt[driveid].phys_heads, nph);
635 SET_EBDA(fdpt[driveid].phys_sectors, npspt);
636 SET_EBDA(fdpt[driveid].a0h_signature, 0xa0);
638 // Checksum structure.
639 u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s
641 u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s
642 , fdpt[driveid]) - 1);
643 SET_EBDA(fdpt[driveid].checksum, -sum);
647 get_translation(int driveid)
649 if (! CONFIG_COREBOOT) {
650 // Emulators pass in the translation info via nvram.
651 u8 channel = driveid / 2;
652 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
653 translation >>= 2 * (driveid % 4);
658 // On COREBOOT, use a heuristic to determine translation type.
659 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
660 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
661 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
663 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
664 return ATA_TRANSLATION_NONE;
665 if (cylinders * heads <= 131072)
666 return ATA_TRANSLATION_LARGE;
667 return ATA_TRANSLATION_LBA;
671 setup_translation(int driveid)
673 u8 translation = get_translation(driveid);
674 SET_EBDA(ata.devices[driveid].translation, translation);
676 u8 channel = driveid / 2;
677 u8 slave = driveid % 2;
678 u16 heads = GET_EBDA(ata.devices[driveid].pchs.heads);
679 u16 cylinders = GET_EBDA(ata.devices[driveid].pchs.cylinders);
680 u16 spt = GET_EBDA(ata.devices[driveid].pchs.spt);
681 u64 sectors = GET_EBDA(ata.devices[driveid].sectors);
683 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
684 , channel, slave, cylinders, heads, spt);
685 switch (translation) {
686 case ATA_TRANSLATION_NONE:
689 case ATA_TRANSLATION_LBA:
692 if (sectors > 63*255*1024) {
697 u32 sect = (u32)sectors / 63;
709 cylinders = sect / heads;
711 case ATA_TRANSLATION_RECHS:
712 dprintf(1, "r-echs");
713 // Take care not to overflow
718 cylinders = (u16)((u32)(cylinders)*16/15);
720 // then go through the large bitshift process
721 case ATA_TRANSLATION_LARGE:
722 if (translation == ATA_TRANSLATION_LARGE)
724 while (cylinders > 1024) {
728 // If we max out the head count
734 // clip to 1024 cylinders in lchs
735 if (cylinders > 1024)
737 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
739 SET_EBDA(ata.devices[driveid].lchs.heads, heads);
740 SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders);
741 SET_EBDA(ata.devices[driveid].lchs.spt, spt);
745 init_drive_ata(int driveid)
747 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_ATA);
749 // Temporary values to do the transfer
750 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
751 SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16);
753 // Now we send a IDENTIFY command to ATA device
755 memset(buffer, 0, sizeof(buffer));
756 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
758 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
760 BX_PANIC("ata-detect: Failed to detect ATA device\n");
762 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
763 u8 mode = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
764 u16 blksize = IDE_SECTOR_SIZE;
766 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
767 u16 heads = *(u16*)&buffer[3*2]; // word 3
768 u16 spt = *(u16*)&buffer[6*2]; // word 6
771 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
772 sectors = *(u64*)&buffer[100*2]; // word 100-103
774 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
776 SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD);
777 SET_EBDA(ata.devices[driveid].removable, removable);
778 SET_EBDA(ata.devices[driveid].mode, mode);
779 SET_EBDA(ata.devices[driveid].blksize, blksize);
780 SET_EBDA(ata.devices[driveid].pchs.heads, heads);
781 SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders);
782 SET_EBDA(ata.devices[driveid].pchs.spt, spt);
783 SET_EBDA(ata.devices[driveid].sectors, sectors);
785 // Setup disk geometry translation.
786 setup_translation(driveid);
789 u8 hdcount = GET_EBDA(ata.hdcount);
790 SET_EBDA(ata.idmap[0][hdcount], driveid);
791 SET_EBDA(ata.hdcount, ++hdcount);
793 // Fill "fdpt" structure.
796 // Report drive info to user.
797 u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors) >> 11;
798 report_model(driveid, buffer);
799 u8 version = get_ata_version(buffer);
800 if (sizeinmb < (1 << 16))
801 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
803 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
804 , (u32)(sizeinmb >> 10));
808 init_drive_unknown(int driveid)
810 SET_EBDA(ata.devices[driveid].type, ATA_TYPE_UNKNOWN);
812 u8 channel = driveid / 2;
813 u8 slave = driveid % 2;
814 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
822 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
823 u8 channel = driveid / 2;
824 u8 slave = driveid % 2;
826 u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
827 u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
831 // Disable interrupts
832 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
835 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
837 outb(0x55, iobase1+ATA_CB_SC);
838 outb(0xaa, iobase1+ATA_CB_SN);
839 outb(0xaa, iobase1+ATA_CB_SC);
840 outb(0x55, iobase1+ATA_CB_SN);
841 outb(0x55, iobase1+ATA_CB_SC);
842 outb(0xaa, iobase1+ATA_CB_SN);
844 // If we found something
845 u8 sc = inb(iobase1+ATA_CB_SC);
846 u8 sn = inb(iobase1+ATA_CB_SN);
847 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
849 if (sc != 0x55 || sn != 0xaa)
855 // check for ATA or ATAPI
856 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
858 sc = inb(iobase1+ATA_CB_SC);
859 sn = inb(iobase1+ATA_CB_SN);
860 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
861 if (sc!=0x01 || sn!=0x01) {
862 init_drive_unknown(driveid);
865 u8 cl = inb(iobase1+ATA_CB_CL);
866 u8 ch = inb(iobase1+ATA_CB_CH);
867 u8 st = inb(iobase1+ATA_CB_STAT);
868 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
869 , driveid, sc, sn, cl, ch, st);
871 if (cl==0x14 && ch==0xeb)
872 init_drive_atapi(driveid);
873 else if (cl==0x00 && ch==0x00 && st!=0x00)
874 init_drive_ata(driveid);
875 else if (cl==0xff && ch==0xff)
879 init_drive_unknown(driveid);
888 // hdidmap and cdidmap init.
890 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
891 SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
892 SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
895 // Scan PCI bus for ATA adapters
897 u16 classid = PCI_CLASS_STORAGE_OTHER; // SATA first
898 while (count<CONFIG_MAX_ATA_INTERFACES-1) {
899 bdf = pci_find_class(classid, bdf+1);
901 if (classid == PCI_CLASS_STORAGE_IDE)
904 classid = PCI_CLASS_STORAGE_IDE; // PATA controllers
909 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
910 SET_EBDA(ata.channels[count].irq, irq);
911 SET_EBDA(ata.channels[count].pci_bdf, bdf);
913 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
916 if (classid != PCI_CLASS_STORAGE_IDE || prog_if & 1) {
917 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
918 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
923 SET_EBDA(ata.channels[count].iobase1, port1);
924 SET_EBDA(ata.channels[count].iobase2, port2);
925 dprintf(1, "ATA controller %d at %x/%x (dev %x class %x/%x)\n"
926 , count, port1, port2, bdf, classid, prog_if);
929 if (classid != PCI_CLASS_STORAGE_IDE || prog_if & 4) {
930 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
931 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
936 dprintf(1, "ATA controller %d at %x/%x (dev %x class %x/%x)\n"
937 , count, port1, port2, bdf, classid, prog_if);
938 SET_EBDA(ata.channels[count].iobase1, port1);
939 SET_EBDA(ata.channels[count].iobase2, port2);
950 dprintf(3, "init hard drives\n");
954 // Store the device count
955 SET_BDA(disk_count, GET_EBDA(ata.hdcount));
957 SET_BDA(disk_control_byte, 0xc0);
959 enable_hwirq(14, entry_76);