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
18 #include "disk.h" // struct ata_s
24 #define NOT_BSY_NOT_DRQ 4
27 #define IDE_SECTOR_SIZE 512
28 #define CDROM_SECTOR_SIZE 2048
30 #define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
32 struct ata_s ATA VAR16_32;
35 /****************************************************************
37 ****************************************************************/
39 // Wait for the specified ide state
41 await_ide(u8 when_done, u16 base, u16 timeout)
43 u64 end = calc_future_tsc(timeout);
45 u8 status = inb(base+ATA_CB_STAT);
48 result = status & ATA_CB_STAT_BSY;
49 else if (when_done == NOT_BSY)
50 result = !(status & ATA_CB_STAT_BSY);
51 else if (when_done == NOT_BSY_DRQ)
52 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ);
53 else if (when_done == NOT_BSY_NOT_DRQ)
54 result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ);
55 else if (when_done == NOT_BSY_RDY)
56 result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY);
60 if (status & ATA_CB_STAT_ERR) {
61 dprintf(1, "await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ"
62 ",!BSY_!DRQ,!BSY_RDY) %d status=%x timeout=%d\n"
63 , when_done, status, timeout);
69 dprintf(1, "IDE time out\n");
73 // Wait for ide state - pauses for one ata cycle first.
74 static __always_inline int
75 pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout)
77 // Wait one PIO transfer cycle.
78 inb(iobase2 + ATA_CB_ASTAT);
80 return await_ide(when_done, iobase1, timeout);
83 // Wait for ide state - pause for 400ns first.
84 static __always_inline int
85 ndelay_await_ide(u8 when_done, u16 iobase1, u16 timeout)
88 return await_ide(when_done, iobase1, timeout);
93 ata_reset(int driveid)
95 u8 channel = driveid / 2;
96 u8 slave = driveid % 2;
97 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
98 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
102 // 8.2.1 (a) -- set SRST in DC
103 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
105 // 8.2.1 (b) -- wait for BSY
106 int status = await_ide(BSY, iobase1, 20);
107 dprintf(6, "ata_reset(1) status=%x\n", status);
109 // 8.2.1 (f) -- clear SRST
110 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
112 // 8.2.1 (g) -- check for sc==sn==0x01
114 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
116 u8 sc = inb(iobase1+ATA_CB_SC);
117 u8 sn = inb(iobase1+ATA_CB_SN);
119 // For predetermined ATA drives - wait for ready.
120 if (sc==0x01 && sn==0x01) {
121 u8 type=GET_GLOBAL(ATA.devices[driveid].type);
122 if (type == ATA_TYPE_ATA)
123 await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT);
126 // 8.2.1 (h) -- wait for not BSY
127 status = await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
128 dprintf(6, "ata_reset(2) status=%x\n", status);
131 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
135 /****************************************************************
137 ****************************************************************/
146 struct ata_pio_command {
161 // Send an ata command to the drive.
163 send_cmd(int driveid, struct ata_pio_command *cmd)
165 u8 channel = driveid / 2;
166 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
167 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
169 int status = inb(iobase1 + ATA_CB_STAT);
170 if (status & ATA_CB_STAT_BSY)
173 // Disable interrupts
174 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
177 u8 device = inb(iobase1 + ATA_CB_DH);
178 outb(cmd->device, iobase1 + ATA_CB_DH);
179 if ((device ^ cmd->device) & (1 << 4))
180 // Wait for device to become active.
183 if (cmd->command & 0x04) {
184 outb(0x00, iobase1 + ATA_CB_FR);
185 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
186 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
187 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
188 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
190 outb(cmd->feature, iobase1 + ATA_CB_FR);
191 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
192 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
193 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
194 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
195 outb(cmd->command, iobase1 + ATA_CB_CMD);
197 status = ndelay_await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
201 if (status & ATA_CB_STAT_ERR) {
202 dprintf(6, "send_cmd : read error\n");
205 if (!(status & ATA_CB_STAT_DRQ)) {
206 dprintf(6, "send_cmd : DRQ not set (status %02x)\n"
207 , (unsigned) status);
215 /****************************************************************
217 ****************************************************************/
219 // Read and discard x number of bytes from an io channel.
221 insx_discard(int mode, int iobase1, int bytes)
224 if (mode == ATA_MODE_PIO32) {
226 for (i=0; i<count; i++)
230 for (i=0; i<count; i++)
235 // Transfer 'count' blocks (of 'blocksize' bytes) to/from drive
236 // 'driveid'. If 'skipfirst' or 'skiplast' is set then the first
237 // and/or last block may be partially transferred. This function is
238 // inlined because all the callers use different forms and because the
239 // large number of parameters would consume a lot of stack space.
240 static __always_inline int
241 ata_transfer(int driveid, int iswrite, int count, int blocksize
242 , int skipfirst, int skiplast, void *far_buffer)
244 dprintf(16, "ata_transfer id=%d write=%d count=%d bs=%d"
245 " skipf=%d skipl=%d buf=%p\n"
246 , driveid, iswrite, count, blocksize
247 , skipfirst, skiplast, far_buffer);
249 // Reset count of transferred data
250 SET_EBDA(sector_count, 0);
252 u8 channel = driveid / 2;
253 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
254 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
255 u8 mode = GET_GLOBAL(ATA.devices[driveid].mode);
259 int bsize = blocksize;
260 if (skipfirst && current == 0) {
261 insx_discard(mode, iobase1, skipfirst);
264 if (skiplast && current == count-1)
268 // Write data to controller
269 dprintf(16, "Write sector id=%d dest=%p\n", driveid, far_buffer);
270 if (mode == ATA_MODE_PIO32)
271 outsl_far(iobase1, far_buffer, bsize / 4);
273 outsw_far(iobase1, far_buffer, bsize / 2);
275 // Read data from controller
276 dprintf(16, "Read sector id=%d dest=%p\n", driveid, far_buffer);
277 if (mode == ATA_MODE_PIO32)
278 insl_far(iobase1, far_buffer, bsize / 4);
280 insw_far(iobase1, far_buffer, bsize / 2);
284 if (skiplast && current == count-1)
285 insx_discard(mode, iobase1, skiplast);
287 status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT);
293 SET_EBDA(sector_count, current);
294 if (current == count)
296 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
298 if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) {
299 dprintf(6, "ata_transfer : more sectors left (status %02x)\n"
300 , (unsigned) status);
305 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
306 | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
308 status &= ~ATA_CB_STAT_DF;
309 if (status != ATA_CB_STAT_RDY ) {
310 dprintf(6, "ata_transfer : no sectors left (status %02x)\n"
311 , (unsigned) status);
316 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
321 ata_transfer_disk(const struct ata_op_s *op, int iswrite)
323 return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE
324 , 0, 0, op->far_buffer);
328 ata_transfer_cdrom(const struct ata_op_s *op)
330 return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
331 , 0, 0, op->far_buffer);
335 ata_transfer_emu(const struct ata_op_s *op, int before, int after)
337 int vcount = op->count * 4 - before - after;
338 int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE
339 , before*512, after*512, op->far_buffer);
341 SET_EBDA(sector_count, 0);
344 SET_EBDA(sector_count, vcount);
349 /****************************************************************
350 * ATA hard drive functions
351 ****************************************************************/
354 send_cmd_disk(const struct ata_op_s *op, u16 command)
356 u8 slave = op->driveid % 2;
359 struct ata_pio_command cmd;
360 memset(&cmd, 0, sizeof(cmd));
362 cmd.command = command;
363 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
364 cmd.sector_count2 = op->count >> 8;
365 cmd.lba_low2 = lba >> 24;
366 cmd.lba_mid2 = lba >> 32;
367 cmd.lba_high2 = lba >> 40;
374 cmd.sector_count = op->count;
376 cmd.lba_mid = lba >> 8;
377 cmd.lba_high = lba >> 16;
378 cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)
379 | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA);
381 return send_cmd(op->driveid, &cmd);
384 // Read/write count blocks from a harddrive.
386 ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer)
389 op.driveid = driveid;
392 op.far_buffer = far_buffer;
394 int ret = send_cmd_disk(&op, command);
398 int iswrite = command == ATA_CMD_WRITE_SECTORS;
399 return ata_transfer_disk(&op, iswrite);
403 /****************************************************************
405 ****************************************************************/
407 // Low-level atapi command transmit function.
408 static __always_inline int
409 send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
411 u8 channel = driveid / 2;
412 u8 slave = driveid % 2;
413 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
414 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
416 struct ata_pio_command cmd;
417 cmd.sector_count = 0;
420 cmd.lba_mid = blocksize;
421 cmd.lba_high = blocksize >> 8;
422 cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
423 cmd.command = ATA_CMD_PACKET;
425 int ret = send_cmd(driveid, &cmd);
429 // Send command to device
430 outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2);
432 int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT);
439 // Low-level cdrom read atapi command transmit function.
441 send_cmd_cdrom(const struct ata_op_s *op)
444 memset(atacmd, 0, sizeof(atacmd));
446 atacmd[0]=0x28; // READ command
447 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
448 atacmd[8]=(op->count & 0x00ff);
449 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
450 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
451 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
452 atacmd[5]=(op->lba & 0x000000ff);
454 return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd)
455 , CDROM_SECTOR_SIZE);
458 // Read sectors from the cdrom.
460 cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer)
463 op.driveid = driveid;
466 op.far_buffer = far_buffer;
468 int ret = send_cmd_cdrom(&op);
472 return ata_transfer_cdrom(&op);
475 // Pretend the cdrom has 512 byte sectors (instead of 2048) and read
478 cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer)
480 u32 velba = vlba + vcount - 1;
482 u32 elba = velba / 4;
483 int count = elba - lba + 1;
484 int before = vlba % 4;
485 int after = 3 - (velba % 4);
488 op.driveid = driveid;
491 op.far_buffer = far_buffer;
493 dprintf(16, "cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d"
494 " count=%d before=%d after=%d\n"
495 , driveid, vlba, vcount, far_buffer, lba, elba
496 , count, before, after);
498 int ret = send_cmd_cdrom(&op);
502 return ata_transfer_emu(&op, before, after);
505 // Send a simple atapi command to a drive.
507 ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen
508 , u32 length, void *far_buffer)
510 int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length);
514 return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer);
518 /****************************************************************
519 * ATA detect and init
520 ****************************************************************/
523 report_model(int driveid, u8 *buffer)
529 for (i=0; i<40; i+=2) {
530 model[i] = buffer[i+54+1];
531 model[i+1] = buffer[i+54];
536 for (i=39; i>0; i--) {
537 if (model[i] != 0x20)
542 u8 channel = driveid / 2;
543 u8 slave = driveid % 2;
544 // XXX - model on stack not %cs
545 printf("ata%d %s: %s", channel, slave ? " slave" : "master", model);
549 get_ata_version(u8 *buffer)
551 u16 ataversion = *(u16*)&buffer[160];
553 for (version=15; version>0; version--)
554 if (ataversion & (1<<version))
560 init_drive_atapi(int driveid)
562 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATAPI);
564 // Temporary values to do the transfer
565 SET_GLOBAL(ATA.devices[driveid].device,ATA_DEVICE_CDROM);
566 SET_GLOBAL(ATA.devices[driveid].mode, ATA_MODE_PIO16);
568 // Now we send a IDENTIFY command to ATAPI device
570 memset(buffer, 0, sizeof(buffer));
571 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE_PACKET
573 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
575 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
577 u8 type = buffer[1] & 0x1f;
578 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
579 u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
580 u16 blksize = CDROM_SECTOR_SIZE;
582 SET_GLOBAL(ATA.devices[driveid].device, type);
583 SET_GLOBAL(ATA.devices[driveid].removable, removable);
584 SET_GLOBAL(ATA.devices[driveid].mode, mode);
585 SET_GLOBAL(ATA.devices[driveid].blksize, blksize);
588 u8 cdcount = GET_GLOBAL(ATA.cdcount);
589 SET_GLOBAL(ATA.idmap[1][cdcount], driveid);
590 SET_GLOBAL(ATA.cdcount, ++cdcount);
592 report_model(driveid, buffer);
593 u8 version = get_ata_version(buffer);
594 if (GET_GLOBAL(ATA.devices[driveid].device)==ATA_DEVICE_CDROM)
595 printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version);
597 printf(" ATAPI-%d Device\n", version);
601 fill_fdpt(int driveid)
606 u16 nlc = GET_GLOBAL(ATA.devices[driveid].lchs.cylinders);
607 u16 nlh = GET_GLOBAL(ATA.devices[driveid].lchs.heads);
608 u16 nlspt = GET_GLOBAL(ATA.devices[driveid].lchs.spt);
610 u16 npc = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
611 u16 nph = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
612 u16 npspt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
614 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
615 ebda->fdpt[driveid].precompensation = 0xffff;
616 ebda->fdpt[driveid].drive_control_byte = 0xc0 | ((nph > 8) << 3);
617 ebda->fdpt[driveid].landing_zone = npc;
618 ebda->fdpt[driveid].cylinders = nlc;
619 ebda->fdpt[driveid].heads = nlh;
620 ebda->fdpt[driveid].sectors = nlspt;
622 if (nlc == npc && nlh == nph && nlspt == npspt)
623 // no logical CHS mapping used, just physical CHS
624 // use Standard Fixed Disk Parameter Table (FDPT)
627 // complies with Phoenix style Translated Fixed Disk Parameter
629 ebda->fdpt[driveid].phys_cylinders = npc;
630 ebda->fdpt[driveid].phys_heads = nph;
631 ebda->fdpt[driveid].phys_sectors = npspt;
632 ebda->fdpt[driveid].a0h_signature = 0xa0;
634 // Checksum structure.
635 u8 sum = checksum((u8*)&ebda->fdpt[driveid], sizeof(ebda->fdpt[driveid])-1);
636 ebda->fdpt[driveid].checksum = -sum;
640 get_translation(int driveid)
642 if (! CONFIG_COREBOOT) {
643 // Emulators pass in the translation info via nvram.
644 u8 channel = driveid / 2;
645 u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
646 translation >>= 2 * (driveid % 4);
651 // On COREBOOT, use a heuristic to determine translation type.
652 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
653 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
654 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
656 if (cylinders <= 1024 && heads <= 16 && spt <= 63)
657 return ATA_TRANSLATION_NONE;
658 if (cylinders * heads <= 131072)
659 return ATA_TRANSLATION_LARGE;
660 return ATA_TRANSLATION_LBA;
664 setup_translation(int driveid)
666 u8 translation = get_translation(driveid);
667 SET_GLOBAL(ATA.devices[driveid].translation, translation);
669 u8 channel = driveid / 2;
670 u8 slave = driveid % 2;
671 u16 heads = GET_GLOBAL(ATA.devices[driveid].pchs.heads);
672 u16 cylinders = GET_GLOBAL(ATA.devices[driveid].pchs.cylinders);
673 u16 spt = GET_GLOBAL(ATA.devices[driveid].pchs.spt);
674 u64 sectors = GET_GLOBAL(ATA.devices[driveid].sectors);
676 dprintf(1, "ata%d-%d: PCHS=%u/%d/%d translation="
677 , channel, slave, cylinders, heads, spt);
678 switch (translation) {
679 case ATA_TRANSLATION_NONE:
682 case ATA_TRANSLATION_LBA:
685 if (sectors > 63*255*1024) {
690 u32 sect = (u32)sectors / 63;
702 cylinders = sect / heads;
704 case ATA_TRANSLATION_RECHS:
705 dprintf(1, "r-echs");
706 // Take care not to overflow
711 cylinders = (u16)((u32)(cylinders)*16/15);
713 // then go through the large bitshift process
714 case ATA_TRANSLATION_LARGE:
715 if (translation == ATA_TRANSLATION_LARGE)
717 while (cylinders > 1024) {
721 // If we max out the head count
727 // clip to 1024 cylinders in lchs
728 if (cylinders > 1024)
730 dprintf(1, " LCHS=%d/%d/%d\n", cylinders, heads, spt);
732 SET_GLOBAL(ATA.devices[driveid].lchs.heads, heads);
733 SET_GLOBAL(ATA.devices[driveid].lchs.cylinders, cylinders);
734 SET_GLOBAL(ATA.devices[driveid].lchs.spt, spt);
738 init_drive_ata(int driveid)
740 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_ATA);
742 // Temporary values to do the transfer
743 SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
744 SET_GLOBAL(ATA.devices[driveid].mode, ATA_MODE_PIO16);
746 // Now we send a IDENTIFY command to ATA device
748 memset(buffer, 0, sizeof(buffer));
749 u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE
751 , MAKE_FARPTR(GET_SEG(SS), (u32)buffer));
753 BX_PANIC("ata-detect: Failed to detect ATA device\n");
755 u8 removable = (buffer[0] & 0x80) ? 1 : 0;
756 u8 mode = buffer[48*2] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
757 u16 blksize = IDE_SECTOR_SIZE;
759 u16 cylinders = *(u16*)&buffer[1*2]; // word 1
760 u16 heads = *(u16*)&buffer[3*2]; // word 3
761 u16 spt = *(u16*)&buffer[6*2]; // word 6
764 if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support
765 sectors = *(u64*)&buffer[100*2]; // word 100-103
767 sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
769 SET_GLOBAL(ATA.devices[driveid].device, ATA_DEVICE_HD);
770 SET_GLOBAL(ATA.devices[driveid].removable, removable);
771 SET_GLOBAL(ATA.devices[driveid].mode, mode);
772 SET_GLOBAL(ATA.devices[driveid].blksize, blksize);
773 SET_GLOBAL(ATA.devices[driveid].pchs.heads, heads);
774 SET_GLOBAL(ATA.devices[driveid].pchs.cylinders, cylinders);
775 SET_GLOBAL(ATA.devices[driveid].pchs.spt, spt);
776 SET_GLOBAL(ATA.devices[driveid].sectors, sectors);
778 // Setup disk geometry translation.
779 setup_translation(driveid);
782 u8 hdcount = GET_GLOBAL(ATA.hdcount);
783 SET_GLOBAL(ATA.idmap[0][hdcount], driveid);
784 SET_GLOBAL(ATA.hdcount, ++hdcount);
786 // Fill "fdpt" structure.
789 // Report drive info to user.
790 u64 sizeinmb = GET_GLOBAL(ATA.devices[driveid].sectors) >> 11;
791 report_model(driveid, buffer);
792 u8 version = get_ata_version(buffer);
793 if (sizeinmb < (1 << 16))
794 printf(" ATA-%d Hard-Disk (%u MiBytes)\n", version, (u32)sizeinmb);
796 printf(" ATA-%d Hard-Disk (%u GiBytes)\n", version
797 , (u32)(sizeinmb >> 10));
801 init_drive_unknown(int driveid)
803 SET_GLOBAL(ATA.devices[driveid].type, ATA_TYPE_UNKNOWN);
805 u8 channel = driveid / 2;
806 u8 slave = driveid % 2;
807 printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master");
815 for(driveid=0; driveid<CONFIG_MAX_ATA_DEVICES; driveid++) {
816 u8 channel = driveid / 2;
817 u8 slave = driveid % 2;
819 u16 iobase1 = GET_GLOBAL(ATA.channels[channel].iobase1);
820 u16 iobase2 = GET_GLOBAL(ATA.channels[channel].iobase2);
824 // Disable interrupts
825 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
828 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
830 outb(0x55, iobase1+ATA_CB_SC);
831 outb(0xaa, iobase1+ATA_CB_SN);
832 outb(0xaa, iobase1+ATA_CB_SC);
833 outb(0x55, iobase1+ATA_CB_SN);
834 outb(0x55, iobase1+ATA_CB_SC);
835 outb(0xaa, iobase1+ATA_CB_SN);
837 // If we found something
838 u8 sc = inb(iobase1+ATA_CB_SC);
839 u8 sn = inb(iobase1+ATA_CB_SN);
840 dprintf(6, "ata_detect(1) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
842 if (sc != 0x55 || sn != 0xaa)
848 // check for ATA or ATAPI
849 outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH);
851 sc = inb(iobase1+ATA_CB_SC);
852 sn = inb(iobase1+ATA_CB_SN);
853 dprintf(6, "ata_detect(2) drive=%d sc=%x sn=%x\n", driveid, sc, sn);
854 if (sc!=0x01 || sn!=0x01) {
855 init_drive_unknown(driveid);
858 u8 cl = inb(iobase1+ATA_CB_CL);
859 u8 ch = inb(iobase1+ATA_CB_CH);
860 u8 st = inb(iobase1+ATA_CB_STAT);
861 dprintf(6, "ata_detect(3) drive=%d sc=%x sn=%x cl=%x ch=%x st=%x\n"
862 , driveid, sc, sn, cl, ch, st);
864 if (cl==0x14 && ch==0xeb)
865 init_drive_atapi(driveid);
866 else if (cl==0x00 && ch==0x00 && st!=0x00)
867 init_drive_ata(driveid);
868 else if (cl==0xff && ch==0xff)
872 init_drive_unknown(driveid);
881 // hdidmap and cdidmap init.
883 for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) {
884 SET_GLOBAL(ATA.idmap[0][device], CONFIG_MAX_ATA_DEVICES);
885 SET_GLOBAL(ATA.idmap[1][device], CONFIG_MAX_ATA_DEVICES);
888 // Scan PCI bus for ATA adapters
891 foreachpci(bdf, max) {
892 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
895 u8 irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
896 SET_GLOBAL(ATA.channels[count].irq, irq);
897 SET_GLOBAL(ATA.channels[count].pci_bdf, bdf);
899 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
903 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
904 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
909 SET_GLOBAL(ATA.channels[count].iobase1, port1);
910 SET_GLOBAL(ATA.channels[count].iobase2, port2);
911 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
912 , count, port1, port2, bdf, prog_if);
916 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
917 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
922 dprintf(1, "ATA controller %d at %x/%x (dev %x prog_if %x)\n"
923 , count, port1, port2, bdf, prog_if);
924 SET_GLOBAL(ATA.channels[count].iobase1, port1);
925 SET_GLOBAL(ATA.channels[count].iobase2, port2);
936 dprintf(3, "init hard drives\n");
940 // Store the device count
941 SET_BDA(disk_count, GET_GLOBAL(ATA.hdcount));
943 SET_BDA(disk_control_byte, 0xc0);
945 enable_hwirq(14, entry_76);