1 // Low level ATA disk access
3 // Copyright (C) 2008,2009 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 LGPLv3 license.
8 #include "types.h" // u8
9 #include "ioport.h" // inb
10 #include "util.h" // dprintf
11 #include "cmos.h" // inb_cmos
12 #include "pic.h" // enable_hwirq
13 #include "biosvar.h" // GET_EBDA
14 #include "pci.h" // foreachpci
15 #include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER
16 #include "pci_regs.h" // PCI_INTERRUPT_LINE
17 #include "boot.h" // add_bcv_hd
18 #include "disk.h" // struct ata_s
19 #include "ata.h" // ATA_CB_STAT
21 #define IDE_TIMEOUT 32000 //32 seconds max for IDE ops
23 struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
26 /****************************************************************
28 ****************************************************************/
30 // Wait for the specified ide state
32 await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
34 u64 end = calc_future_tsc(timeout);
36 u8 status = inb(base+ATA_CB_STAT);
37 if ((status & mask) == flags)
39 if (check_time(end)) {
47 // Wait for the device to be not-busy.
49 await_not_bsy(u16 base)
51 return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT);
54 // Wait for the device to be ready.
58 return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
61 // Wait for ide state - pauses for one ata cycle first.
63 pause_await_not_bsy(u16 iobase1, u16 iobase2)
65 // Wait one PIO transfer cycle.
66 inb(iobase2 + ATA_CB_ASTAT);
68 return await_not_bsy(iobase1);
71 // Wait for ide state - pause for 400ns first.
73 ndelay_await_not_bsy(u16 iobase1)
76 return await_not_bsy(iobase1);
81 ata_reset(struct drive_s *drive_g)
83 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
84 u8 channel = ataid / 2;
86 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
87 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
89 dprintf(6, "ata_reset drive=%p\n", drive_g);
91 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC);
93 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
96 // wait for device to become not busy.
97 int status = await_not_bsy(iobase1);
102 u64 end = calc_future_tsc(IDE_TIMEOUT);
104 outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
105 status = ndelay_await_not_bsy(iobase1);
108 if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
110 // Change drive request failed to take effect - retry.
111 if (check_time(end)) {
117 // QEMU doesn't reset dh on reset, so set it explicitly.
118 outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
121 // On a user-reset request, wait for RDY if it is an ATA device.
122 u8 type=GET_GLOBAL(drive_g->type);
123 if (type == DTYPE_ATA)
124 status = await_rdy(iobase1);
128 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
130 dprintf(6, "ata_reset exit status=%x\n", status);
133 // Check for drive RDY for 16bit interface command.
135 isready(struct drive_s *drive_g)
137 // Read the status from controller
138 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
139 u8 channel = ataid / 2;
140 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
141 u8 status = inb(iobase1 + ATA_CB_STAT);
142 if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY)
143 return DISK_RET_SUCCESS;
144 return DISK_RET_ENOTREADY;
147 // Default 16bit command demuxer for ATA and ATAPI devices.
149 process_ata_misc_op(struct disk_op_s *op)
154 switch (op->command) {
156 ata_reset(op->drive_g);
157 return DISK_RET_SUCCESS;
159 return isready(op->drive_g);
163 return DISK_RET_SUCCESS;
166 return DISK_RET_EPARAM;
171 /****************************************************************
173 ****************************************************************/
175 struct ata_pio_command {
191 // Send an ata command to the drive.
193 send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd)
195 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
196 u8 channel = ataid / 2;
197 u8 slave = ataid % 2;
198 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
201 int status = await_not_bsy(iobase1);
204 u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1)
205 | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0));
206 u8 olddh = inb(iobase1 + ATA_CB_DH);
207 outb(newdh, iobase1 + ATA_CB_DH);
208 if ((olddh ^ newdh) & (1<<4)) {
209 // Was a device change - wait for device to become not busy.
210 status = ndelay_await_not_bsy(iobase1);
215 // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands.
216 if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) {
217 outb(cmd->feature2, iobase1 + ATA_CB_FR);
218 outb(cmd->sector_count2, iobase1 + ATA_CB_SC);
219 outb(cmd->lba_low2, iobase1 + ATA_CB_SN);
220 outb(cmd->lba_mid2, iobase1 + ATA_CB_CL);
221 outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
223 outb(cmd->feature, iobase1 + ATA_CB_FR);
224 outb(cmd->sector_count, iobase1 + ATA_CB_SC);
225 outb(cmd->lba_low, iobase1 + ATA_CB_SN);
226 outb(cmd->lba_mid, iobase1 + ATA_CB_CL);
227 outb(cmd->lba_high, iobase1 + ATA_CB_CH);
228 outb(cmd->command, iobase1 + ATA_CB_CMD);
233 // Wait for data after calling 'send_cmd'.
235 ata_wait_data(u16 iobase1)
237 int status = ndelay_await_not_bsy(iobase1);
241 if (status & ATA_CB_STAT_ERR) {
242 dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n"
243 , status, inb(iobase1 + ATA_CB_ERR));
246 if (!(status & ATA_CB_STAT_DRQ)) {
247 dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
254 // Send an ata command that does not transfer any further data.
256 ata_cmd_nondata(struct drive_s *drive_g, struct ata_pio_command *cmd)
258 u8 ataid = GET_GLOBAL(drive_g->cntl_id);
259 u8 channel = ataid / 2;
260 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
261 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
263 // Disable interrupts
264 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
266 int ret = send_cmd(drive_g, cmd);
269 ret = ndelay_await_not_bsy(iobase1);
273 if (ret & ATA_CB_STAT_ERR) {
274 dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
275 , ret, inb(iobase1 + ATA_CB_ERR));
279 if (ret & ATA_CB_STAT_DRQ) {
280 dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
287 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
293 /****************************************************************
295 ****************************************************************/
297 // Transfer 'op->count' blocks (of 'blocksize' bytes) to/from drive
300 ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize)
302 dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n"
303 , op->drive_g, iswrite, op->count, blocksize, op->buf_fl);
305 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
306 u8 channel = ataid / 2;
307 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
308 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
309 int count = op->count;
310 void *buf_fl = op->buf_fl;
314 // Write data to controller
315 dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl);
316 if (CONFIG_ATA_PIO32)
317 outsl_fl(iobase1, buf_fl, blocksize / 4);
319 outsw_fl(iobase1, buf_fl, blocksize / 2);
321 // Read data from controller
322 dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl);
323 if (CONFIG_ATA_PIO32)
324 insl_fl(iobase1, buf_fl, blocksize / 4);
326 insw_fl(iobase1, buf_fl, blocksize / 2);
330 status = pause_await_not_bsy(iobase1, iobase2);
340 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR);
341 if (status != ATA_CB_STAT_DRQ) {
342 dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n"
349 status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
352 status &= ~ATA_CB_STAT_DF;
354 dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
362 /****************************************************************
364 ****************************************************************/
367 #define BM_CMD_MEMWRITE 0x08
368 #define BM_CMD_START 0x01
370 #define BM_STATUS_IRQ 0x04
371 #define BM_STATUS_ERROR 0x02
372 #define BM_STATUS_ACTIVE 0x01
380 // Check if DMA available and setup transfer if so.
382 ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
384 if (! CONFIG_ATA_DMA)
386 u32 dest = (u32)op->buf_fl;
388 // Need minimum alignment of 1.
390 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
391 u8 channel = ataid / 2;
392 u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster);
395 u32 bytes = op->count * blocksize;
399 // Build PRD dma structure.
400 struct sff_dma_prd *dma = MAKE_FLATPTR(
402 , (void*)offsetof(struct extended_bios_data_area_s, extra_stack));
403 struct sff_dma_prd *origdma = dma;
405 if (dma >= &origdma[16])
406 // Too many descriptors..
411 u32 max = 0x10000 - (dest & 0xffff);
415 SET_FLATPTR(dma->buf_fl, dest);
420 dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count);
422 SET_FLATPTR(dma->count, count);
426 // Program bus-master controller.
427 outl((u32)origdma, iomaster + BM_TABLE);
428 u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START);
429 outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD);
430 outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS);
435 // Transfer data using DMA.
437 ata_dma_transfer(struct disk_op_s *op)
439 if (! CONFIG_ATA_DMA)
441 dprintf(16, "ata_dma_transfer id=%p buf=%p\n"
442 , op->drive_g, op->buf_fl);
444 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
445 u8 channel = ataid / 2;
446 u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster);
448 // Start bus-master controller.
449 u8 oldcmd = inb(iomaster + BM_CMD);
450 outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
452 u64 end = calc_future_tsc(IDE_TIMEOUT);
455 status = inb(iomaster + BM_STATUS);
456 if (status & BM_STATUS_IRQ)
458 // Transfer in progress
459 if (check_time(end)) {
466 outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
468 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
469 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
470 int idestatus = pause_await_not_bsy(iobase1, iobase2);
472 if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ
474 && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ
475 | ATA_CB_STAT_ERR)) == 0x00)
479 dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus
480 , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR));
486 /****************************************************************
487 * ATA hard drive functions
488 ****************************************************************/
490 // Transfer data to harddrive using PIO protocol.
492 ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd)
494 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
495 u8 channel = ataid / 2;
496 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
497 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
499 // Disable interrupts
500 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
502 int ret = send_cmd(op->drive_g, cmd);
505 ret = ata_wait_data(iobase1);
508 ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
512 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
516 // Transfer data to harddrive using DMA protocol.
518 ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd)
520 if (! CONFIG_ATA_DMA)
522 int ret = send_cmd(op->drive_g, cmd);
525 return ata_dma_transfer(op);
528 // Read/write count blocks from a harddrive.
530 ata_readwrite(struct disk_op_s *op, int iswrite)
534 int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
536 struct ata_pio_command cmd;
537 memset(&cmd, 0, sizeof(cmd));
539 if (op->count >= (1<<8) || lba + op->count >= (1<<28)) {
540 cmd.sector_count2 = op->count >> 8;
541 cmd.lba_low2 = lba >> 24;
542 cmd.lba_mid2 = lba >> 32;
543 cmd.lba_high2 = lba >> 40;
547 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT
548 : ATA_CMD_READ_SECTORS_EXT);
550 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT
551 : ATA_CMD_READ_DMA_EXT);
554 cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS
555 : ATA_CMD_READ_SECTORS);
557 cmd.command = (iswrite ? ATA_CMD_WRITE_DMA
561 cmd.sector_count = op->count;
563 cmd.lba_mid = lba >> 8;
564 cmd.lba_high = lba >> 16;
565 cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
569 ret = ata_pio_cmd_data(op, iswrite, &cmd);
571 ret = ata_dma_cmd_data(op, &cmd);
573 return DISK_RET_EBADTRACK;
574 return DISK_RET_SUCCESS;
577 // 16bit command demuxer for ATA harddrives.
579 process_ata_op(struct disk_op_s *op)
584 switch (op->command) {
586 return ata_readwrite(op, 0);
588 return ata_readwrite(op, 1);
590 return process_ata_misc_op(op);
595 /****************************************************************
597 ****************************************************************/
599 // Low-level atapi command transmit function.
601 atapi_cmd_data(struct disk_op_s *op, u8 *cmdbuf, u8 cmdlen, u16 blocksize)
603 u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
604 u8 channel = ataid / 2;
605 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
606 u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
608 struct ata_pio_command cmd;
609 memset(&cmd, 0, sizeof(cmd));
610 cmd.lba_mid = blocksize;
611 cmd.lba_high = blocksize >> 8;
612 cmd.command = ATA_CMD_PACKET;
614 // Disable interrupts
615 outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
617 int ret = send_cmd(op->drive_g, &cmd);
620 ret = ata_wait_data(iobase1);
624 // Send command to device
625 outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cmdbuf), cmdlen / 2);
627 int status = pause_await_not_bsy(iobase1, iobase2);
633 if (status & ATA_CB_STAT_ERR) {
634 u8 err = inb(iobase1 + ATA_CB_ERR);
637 dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n"
642 if (!(status & ATA_CB_STAT_DRQ)) {
643 dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status);
648 ret = ata_pio_transfer(op, 0, blocksize);
652 outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
656 // Read sectors from the cdrom.
658 cdrom_read(struct disk_op_s *op)
661 memset(atacmd, 0, sizeof(atacmd));
662 atacmd[0]=0x28; // READ command
663 atacmd[7]=(op->count & 0xff00) >> 8; // Sectors
664 atacmd[8]=(op->count & 0x00ff);
665 atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA
666 atacmd[3]=(op->lba & 0x00ff0000) >> 16;
667 atacmd[4]=(op->lba & 0x0000ff00) >> 8;
668 atacmd[5]=(op->lba & 0x000000ff);
670 return atapi_cmd_data(op, atacmd, sizeof(atacmd), CDROM_SECTOR_SIZE);
673 // 16bit command demuxer for ATAPI cdroms.
675 process_atapi_op(struct disk_op_s *op)
678 switch (op->command) {
680 ret = cdrom_read(op);
682 return DISK_RET_EBADTRACK;
683 return DISK_RET_SUCCESS;
686 return DISK_RET_EWRITEPROTECT;
688 return process_ata_misc_op(op);
692 // Send a simple atapi command to a drive.
694 ata_cmd_packet(struct drive_s *drive_g, u8 *cmdbuf, u8 cmdlen
695 , u32 length, void *buf_fl)
697 struct disk_op_s dop;
698 memset(&dop, 0, sizeof(dop));
699 dop.drive_g = drive_g;
703 return atapi_cmd_data(&dop, cmdbuf, cmdlen, length);
707 /****************************************************************
708 * ATA detect and init
709 ****************************************************************/
711 // Send an identify device or identify device packet command.
713 send_ata_identity(struct drive_s *drive_g, u16 *buffer, int command)
715 memset(buffer, 0, DISK_SECTOR_SIZE);
717 struct disk_op_s dop;
718 memset(&dop, 0, sizeof(dop));
719 dop.drive_g = drive_g;
722 dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
724 struct ata_pio_command cmd;
725 memset(&cmd, 0, sizeof(cmd));
726 cmd.command = command;
728 return ata_pio_cmd_data(&dop, 0, &cmd);
731 // Extract the ATA/ATAPI version info.
733 extract_version(u16 *buffer)
735 // Extract ATA/ATAPI version.
736 u16 ataversion = buffer[80];
738 for (version=15; version>0; version--)
739 if (ataversion & (1<<version))
744 // Extract common information from IDENTIFY commands.
746 extract_identify(struct drive_s *drive_g, u16 *buffer)
748 dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
751 char *model = drive_g->model;
752 int maxsize = ARRAY_SIZE(drive_g->model);
754 for (i=0; i<maxsize/2; i++) {
755 u16 v = buffer[27+i];
757 model[i*2+1] = v & 0xff;
759 model[maxsize-1] = 0x00;
761 // Trim trailing spaces from model name.
762 for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
766 SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0);
767 SET_GLOBAL(drive_g->cntl_info, extract_version(buffer));
770 // Print out a description of the given atapi drive.
772 describe_atapi(struct drive_s *drive_g)
774 u8 ataid = drive_g->cntl_id;
775 u8 channel = ataid / 2;
776 u8 slave = ataid % 2;
777 u8 version = drive_g->cntl_info;
778 int iscd = drive_g->floppy_type;
779 printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
780 , drive_g->model, version
781 , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
784 // Detect if the given drive is an atapi - initialize it if so.
785 static struct drive_s *
786 init_drive_atapi(struct drive_s *dummy, u16 *buffer)
788 // Send an IDENTIFY_DEVICE_PACKET command to device
789 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE);
793 // Success - setup as ATAPI.
794 struct drive_s *drive_g = allocDrive();
797 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
798 extract_identify(drive_g, buffer);
799 SET_GLOBAL(drive_g->type, DTYPE_ATAPI);
800 SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE);
801 SET_GLOBAL(drive_g->sectors, (u64)-1);
802 u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
803 SET_GLOBAL(drive_g->floppy_type, iscd);
807 map_cd_drive(drive_g);
812 // Print out a description of the given ata drive.
814 describe_ata(struct drive_s *drive_g)
816 u8 ataid = drive_g->cntl_id;
817 u8 channel = ataid / 2;
818 u8 slave = ataid % 2;
819 u64 sectors = drive_g->sectors;
820 u8 version = drive_g->cntl_info;
821 char *model = drive_g->model;
822 printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
823 u64 sizeinmb = sectors >> 11;
824 if (sizeinmb < (1 << 16))
825 printf(" (%u MiBytes)", (u32)sizeinmb);
827 printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
830 // Detect if the given drive is a regular ata drive - initialize it if so.
831 static struct drive_s *
832 init_drive_ata(struct drive_s *dummy, u16 *buffer)
834 // Send an IDENTIFY_DEVICE command to device
835 int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
839 // Success - setup as ATA.
840 struct drive_s *drive_g = allocDrive();
843 SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
844 extract_identify(drive_g, buffer);
845 SET_GLOBAL(drive_g->type, DTYPE_ATA);
846 SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE);
848 SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]);
849 SET_GLOBAL(drive_g->pchs.heads, buffer[3]);
850 SET_GLOBAL(drive_g->pchs.spt, buffer[6]);
853 if (buffer[83] & (1 << 10)) // word 83 - lba48 support
854 sectors = *(u64*)&buffer[100]; // word 100-103
856 sectors = *(u32*)&buffer[60]; // word 60 and word 61
857 SET_GLOBAL(drive_g->sectors, sectors);
859 // Setup disk geometry translation.
860 setup_translation(drive_g);
862 // Register with bcv system.
863 add_bcv_internal(drive_g);
868 static u64 SpinupEnd;
870 // Wait for non-busy status and check for "floating bus" condition.
872 powerup_await_non_bsy(u16 base)
877 status = inb(base+ATA_CB_STAT);
878 if (!(status & ATA_CB_STAT_BSY))
881 if (orstatus == 0xff) {
882 dprintf(1, "powerup IDE floating\n");
885 if (check_time(SpinupEnd)) {
891 dprintf(6, "powerup iobase=%x st=%x\n", base, status);
895 // Detect any drives attached to a given controller.
897 ata_detect(void *data)
899 struct ata_channel_s *atachannel = data;
900 int startid = (atachannel - ATA_channels) * 2;
901 struct drive_s dummy;
902 memset(&dummy, 0, sizeof(dummy));
904 int ataid, last_reset_ataid=-1;
905 for (ataid=startid; ataid<startid+2; ataid++) {
906 u8 channel = ataid / 2;
907 u8 slave = ataid % 2;
909 u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
914 int status = powerup_await_non_bsy(iobase1);
917 u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0;
918 outb(newdh, iobase1+ATA_CB_DH);
920 status = powerup_await_non_bsy(iobase1);
924 // Check if ioport registers look valid.
925 outb(newdh, iobase1+ATA_CB_DH);
926 u8 dh = inb(iobase1+ATA_CB_DH);
927 outb(0x55, iobase1+ATA_CB_SC);
928 outb(0xaa, iobase1+ATA_CB_SN);
929 u8 sc = inb(iobase1+ATA_CB_SC);
930 u8 sn = inb(iobase1+ATA_CB_SN);
931 dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
932 , ataid, sc, sn, dh);
933 if (sc != 0x55 || sn != 0xaa || dh != newdh)
936 // Prepare new drive.
937 dummy.cntl_id = ataid;
940 if (slave && ataid == last_reset_ataid + 1) {
941 // The drive was just reset - no need to reset it again.
944 last_reset_ataid = ataid;
949 struct drive_s *drive_g = init_drive_atapi(&dummy, buffer);
951 // Didn't find an ATAPI drive - look for ATA drive.
952 u8 st = inb(iobase1+ATA_CB_STAT);
954 // Status not set - can't be a valid drive.
958 int ret = await_rdy(iobase1);
963 drive_g = init_drive_ata(&dummy, buffer);
965 // No ATA drive found
969 u16 resetresult = buffer[93];
970 dprintf(6, "ata_detect resetresult=%04x\n", resetresult);
971 if (!slave && (resetresult & 0xdf61) == 0x4041)
972 // resetresult looks valid and device 0 is responding to
973 // device 1 requests - device 1 must not be present - skip
979 // Initialize an ata controller and detect its drives.
981 init_controller(struct ata_channel_s *atachannel
982 , int bdf, int irq, u32 port1, u32 port2, u32 master)
984 SET_GLOBAL(atachannel->irq, irq);
985 SET_GLOBAL(atachannel->pci_bdf, bdf);
986 SET_GLOBAL(atachannel->iobase1, port1);
987 SET_GLOBAL(atachannel->iobase2, port2);
988 SET_GLOBAL(atachannel->iomaster, master);
989 dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
990 , atachannel - ATA_channels, port1, port2, master, irq, bdf);
991 run_thread(ata_detect, atachannel);
997 // Locate and init ata controllers.
1001 // Scan PCI bus for ATA adapters
1002 int count=0, pcicount=0;
1004 foreachpci(bdf, max) {
1006 if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
1008 if (count >= ARRAY_SIZE(ATA_channels))
1011 u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
1012 u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
1014 if (CONFIG_ATA_DMA && prog_if & 0x80) {
1015 // Check for bus-mastering.
1016 u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4);
1017 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
1018 master = bar & PCI_BASE_ADDRESS_IO_MASK;
1019 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
1023 u32 port1, port2, irq;
1025 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
1026 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
1029 port1 = PORT_ATA1_CMD_BASE;
1030 port2 = PORT_ATA1_CTRL_BASE;
1033 init_controller(&ATA_channels[count], bdf, irq, port1, port2, master);
1037 port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
1038 port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
1041 port1 = PORT_ATA2_CMD_BASE;
1042 port2 = PORT_ATA2_CTRL_BASE;
1045 init_controller(&ATA_channels[count], bdf, irq, port1, port2
1046 , master ? master + 8 : 0);
1050 if (!CONFIG_COREBOOT && !pcicount && ARRAY_SIZE(ATA_channels) >= 2) {
1051 // No PCI devices found - probably a QEMU "-M isapc" machine.
1052 // Try using ISA ports for ATA controllers.
1053 init_controller(&ATA_channels[0], -1, IRQ_ATA1
1054 , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
1055 init_controller(&ATA_channels[1], -1, IRQ_ATA2
1056 , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
1067 dprintf(3, "init hard drives\n");
1069 SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
1072 SET_BDA(disk_control_byte, 0xc0);
1074 enable_hwirq(14, entry_76);