X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Fata.c;h=76e4f20d2f9cd6b35a078d9e2200a8ef21969c7d;hb=34203cdf8a89c747e221005850a4558252235360;hp=e8a4a287df3d265673ebbf71763fd25c5fa7e305;hpb=1bb3b5c25284cb98f40f245f5d621721c6959fe1;p=seabios.git diff --git a/src/ata.c b/src/ata.c index e8a4a28..76e4f20 100644 --- a/src/ata.c +++ b/src/ata.c @@ -1,30 +1,25 @@ // Low level ATA disk access // -// Copyright (C) 2008 Kevin O'Connor +// Copyright (C) 2008,2009 Kevin O'Connor // Copyright (C) 2002 MandrakeSoft S.A. // -// This file may be distributed under the terms of the GNU GPLv3 license. +// This file may be distributed under the terms of the GNU LGPLv3 license. -#include "ata.h" // ATA_* #include "types.h" // u8 #include "ioport.h" // inb -#include "util.h" // BX_INFO +#include "util.h" // dprintf #include "cmos.h" // inb_cmos +#include "pic.h" // enable_hwirq +#include "biosvar.h" // GET_EBDA +#include "pci.h" // foreachpci +#include "pci_ids.h" // PCI_CLASS_STORAGE_OTHER +#include "pci_regs.h" // PCI_INTERRUPT_LINE +#include "boot.h" // boot_add_hd +#include "disk.h" // struct ata_s +#include "ata.h" // ATA_CB_STAT +#include "blockcmd.h" // CDB_CMD_READ_10 -#define TIMEOUT 0 -#define BSY 1 -#define NOT_BSY 2 -#define NOT_BSY_DRQ 3 -#define NOT_BSY_NOT_DRQ 4 -#define NOT_BSY_RDY 5 - -#define IDE_SECTOR_SIZE 512 -#define CDROM_SECTOR_SIZE 2048 - -#define IDE_TIMEOUT 32000u //32 seconds max for IDE ops - -#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args) -#define DEBUGF(fmt, args...) +#define IDE_TIMEOUT 32000 //32 seconds max for IDE ops /**************************************************************** @@ -32,113 +27,143 @@ ****************************************************************/ // Wait for the specified ide state -static int -await_ide(u8 when_done, u16 base, u16 timeout) +static inline int +await_ide(u8 mask, u8 flags, u16 base, u16 timeout) { - u32 time=0, last=0; + u64 end = calc_future_tsc(timeout); for (;;) { u8 status = inb(base+ATA_CB_STAT); - time++; - u8 result = 0; - if (when_done == BSY) - result = status & ATA_CB_STAT_BSY; - else if (when_done == NOT_BSY) - result = !(status & ATA_CB_STAT_BSY); - else if (when_done == NOT_BSY_DRQ) - result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_DRQ); - else if (when_done == NOT_BSY_NOT_DRQ) - result = !(status & ATA_CB_STAT_BSY) && !(status & ATA_CB_STAT_DRQ); - else if (when_done == NOT_BSY_RDY) - result = !(status & ATA_CB_STAT_BSY) && (status & ATA_CB_STAT_RDY); - - if (result) + if ((status & mask) == flags) return status; - // mod 2048 each 16 ms - if (time>>16 != last) { - last = time >>16; - DEBUGF("await_ide: (TIMEOUT,BSY,!BSY,!BSY_DRQ,!BSY_!DRQ,!BSY_RDY)" - " %d time= %d timeout= %d\n" - , when_done, time>>11, timeout); - } - if (status & ATA_CB_STAT_ERR) { - DEBUGF("await_ide: ERROR (TIMEOUT,BSY,!BSY,!BSY_DRQ" - ",!BSY_!DRQ,!BSY_RDY) %d time= %d timeout= %d\n" - , when_done, time>>11, timeout); + if (check_tsc(end)) { + warn_timeout(); return -1; } - if (timeout == 0 || (time>>11) > timeout) - break; + yield(); } - BX_INFO("IDE time out\n"); - return -1; +} + +// Wait for the device to be not-busy. +static int +await_not_bsy(u16 base) +{ + return await_ide(ATA_CB_STAT_BSY, 0, base, IDE_TIMEOUT); +} + +// Wait for the device to be ready. +static int +await_rdy(u16 base) +{ + return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT); } // Wait for ide state - pauses for one ata cycle first. -static __always_inline int -pause_await_ide(u8 when_done, u16 iobase1, u16 iobase2, u16 timeout) +static inline int +pause_await_not_bsy(u16 iobase1, u16 iobase2) { // Wait one PIO transfer cycle. inb(iobase2 + ATA_CB_ASTAT); - return await_ide(when_done, iobase1, timeout); + return await_not_bsy(iobase1); } -// Delay for x nanoseconds -static void -nsleep(u32 delay) +// Wait for ide state - pause for 400ns first. +static inline int +ndelay_await_not_bsy(u16 iobase1) { - // XXX - how to implement ndelay? - while (delay--) - nop(); + ndelay(400); + return await_not_bsy(iobase1); } // Reset a drive -void -ata_reset(int driveid) +static void +ata_reset(struct atadrive_s *adrive_g) { - u16 iobase1, iobase2; - u8 channel, slave, sn, sc; - u8 type; - - channel = driveid / 2; - slave = driveid % 2; - - iobase1 = GET_EBDA(ata.channels[channel].iobase1); - iobase2 = GET_EBDA(ata.channels[channel].iobase2); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u8 slave = GET_GLOBAL(adrive_g->slave); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); - // Reset - - // 8.2.1 (a) -- set SRST in DC + dprintf(6, "ata_reset drive=%p\n", &adrive_g->drive); + // Pulse SRST outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST, iobase2+ATA_CB_DC); - - // 8.2.1 (b) -- wait for BSY - await_ide(BSY, iobase1, 20); - - // 8.2.1 (f) -- clear SRST + udelay(5); outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC); + msleep(2); - type=GET_EBDA(ata.devices[driveid].type); - if (type != ATA_TYPE_NONE) { - - // 8.2.1 (g) -- check for sc==sn==0x01 - // select device - outb(slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0, iobase1+ATA_CB_DH); - sc = inb(iobase1+ATA_CB_SC); - sn = inb(iobase1+ATA_CB_SN); - - if ( (sc==0x01) && (sn==0x01) ) { - if (type == ATA_TYPE_ATA) //ATA - await_ide(NOT_BSY_RDY, iobase1, IDE_TIMEOUT); - else //ATAPI - await_ide(NOT_BSY, iobase1, IDE_TIMEOUT); + // wait for device to become not busy. + int status = await_not_bsy(iobase1); + if (status < 0) + goto done; + if (slave) { + // Change device. + u64 end = calc_future_tsc(IDE_TIMEOUT); + for (;;) { + outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH); + status = ndelay_await_not_bsy(iobase1); + if (status < 0) + goto done; + if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1) + break; + // Change drive request failed to take effect - retry. + if (check_tsc(end)) { + warn_timeout(); + goto done; + } } - - // 8.2.1 (h) -- wait for not BSY - await_ide(NOT_BSY, iobase1, IDE_TIMEOUT); + } else { + // QEMU doesn't reset dh on reset, so set it explicitly. + outb(ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH); } + // On a user-reset request, wait for RDY if it is an ATA device. + u8 type=GET_GLOBAL(adrive_g->drive.type); + if (type == DTYPE_ATA) + status = await_rdy(iobase1); + +done: // Enable interrupts outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); + + dprintf(6, "ata_reset exit status=%x\n", status); +} + +// Check for drive RDY for 16bit interface command. +static int +isready(struct atadrive_s *adrive_g) +{ + // Read the status from controller + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u8 status = inb(iobase1 + ATA_CB_STAT); + if ((status & (ATA_CB_STAT_BSY|ATA_CB_STAT_RDY)) == ATA_CB_STAT_RDY) + return DISK_RET_SUCCESS; + return DISK_RET_ENOTREADY; +} + +// Default 16bit command demuxer for ATA and ATAPI devices. +static int +process_ata_misc_op(struct disk_op_s *op) +{ + if (!CONFIG_ATA) + return 0; + + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + switch (op->command) { + case CMD_RESET: + ata_reset(adrive_g); + return DISK_RET_SUCCESS; + case CMD_ISREADY: + return isready(adrive_g); + case CMD_FORMAT: + case CMD_VERIFY: + case CMD_SEEK: + return DISK_RET_SUCCESS; + default: + op->count = 0; + return DISK_RET_EPARAM; + } } @@ -146,13 +171,6 @@ ata_reset(int driveid) * ATA send command ****************************************************************/ -struct ata_op_s { - u64 lba; - void *far_buffer; - u16 driveid; - u16 count; -}; - struct ata_pio_command { u8 feature; u8 sector_count; @@ -162,6 +180,7 @@ struct ata_pio_command { u8 device; u8 command; + u8 feature2; u8 sector_count2; u8 lba_low2; u8 lba_mid2; @@ -170,19 +189,30 @@ struct ata_pio_command { // Send an ata command to the drive. static int -send_cmd(int driveid, struct ata_pio_command *cmd) +send_cmd(struct atadrive_s *adrive_g, struct ata_pio_command *cmd) { - u8 channel = driveid / 2; - u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); - u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u8 slave = GET_GLOBAL(adrive_g->slave); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); - int status = inb(iobase1 + ATA_CB_STAT); - if (status & ATA_CB_STAT_BSY) - return 1; + // Select device + int status = await_not_bsy(iobase1); + if (status < 0) + return status; + u8 newdh = ((cmd->device & ~ATA_CB_DH_DEV1) + | (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0)); + u8 olddh = inb(iobase1 + ATA_CB_DH); + outb(newdh, iobase1 + ATA_CB_DH); + if ((olddh ^ newdh) & (1<<4)) { + // Was a device change - wait for device to become not busy. + status = ndelay_await_not_bsy(iobase1); + if (status < 0) + return status; + } - outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); - if (cmd->command & 0x04) { - outb(0x00, iobase1 + ATA_CB_FR); + // Check for ATA_CMD_(READ|WRITE)_(SECTORS|DMA)_EXT commands. + if ((cmd->command & ~0x11) == ATA_CMD_READ_SECTORS_EXT) { + outb(cmd->feature2, iobase1 + ATA_CB_FR); outb(cmd->sector_count2, iobase1 + ATA_CB_SC); outb(cmd->lba_low2, iobase1 + ATA_CB_SN); outb(cmd->lba_mid2, iobase1 + ATA_CB_CL); @@ -193,160 +223,260 @@ send_cmd(int driveid, struct ata_pio_command *cmd) outb(cmd->lba_low, iobase1 + ATA_CB_SN); outb(cmd->lba_mid, iobase1 + ATA_CB_CL); outb(cmd->lba_high, iobase1 + ATA_CB_CH); - outb(cmd->device, iobase1 + ATA_CB_DH); outb(cmd->command, iobase1 + ATA_CB_CMD); - nsleep(400); + return 0; +} - status = await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT); +// Wait for data after calling 'send_cmd'. +static int +ata_wait_data(u16 iobase1) +{ + int status = ndelay_await_not_bsy(iobase1); if (status < 0) return status; if (status & ATA_CB_STAT_ERR) { - DEBUGF("send_cmd : read error\n"); - return 2; + dprintf(6, "send_cmd : read error (status=%02x err=%02x)\n" + , status, inb(iobase1 + ATA_CB_ERR)); + return -4; } if (!(status & ATA_CB_STAT_DRQ)) { - DEBUGF("send_cmd : DRQ not set (status %02x)\n" - , (unsigned) status); - return 3; + dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status); + return -5; } return 0; } +// Send an ata command that does not transfer any further data. +int +ata_cmd_nondata(struct atadrive_s *adrive_g, struct ata_pio_command *cmd) +{ + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); -/**************************************************************** - * ATA transfers - ****************************************************************/ + // Disable interrupts + outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); -// Read and discard x number of bytes from an io channel. -static void -insx_discard(int mode, int iobase1, int bytes) -{ - int count, i; - if (mode == ATA_MODE_PIO32) { - count = bytes / 4; - for (i=0; icount' blocks (of 'blocksize' bytes) to/from drive +// 'op->drive_g'. +static int +ata_pio_transfer(struct disk_op_s *op, int iswrite, int blocksize) +{ + dprintf(16, "ata_pio_transfer id=%p write=%d count=%d bs=%d buf=%p\n" + , op->drive_g, iswrite, op->count, blocksize, op->buf_fl); + + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); + int count = op->count; + void *buf_fl = op->buf_fl; int status; for (;;) { - int bsize = blocksize; - if (skipfirst && current == 0) { - insx_discard(mode, iobase1, skipfirst); - bsize -= skipfirst; - } - if (skiplast && current == count-1) - bsize -= skiplast; - if (iswrite) { // Write data to controller - DEBUGF("Write sector id=%d dest=%p\n", driveid, far_buffer); - if (mode == ATA_MODE_PIO32) - outsl_far(iobase1, far_buffer, bsize / 4); + dprintf(16, "Write sector id=%p dest=%p\n", op->drive_g, buf_fl); + if (CONFIG_ATA_PIO32) + outsl_fl(iobase1, buf_fl, blocksize / 4); else - outsw_far(iobase1, far_buffer, bsize / 2); + outsw_fl(iobase1, buf_fl, blocksize / 2); } else { // Read data from controller - DEBUGF("Read sector id=%d dest=%p\n", driveid, far_buffer); - if (mode == ATA_MODE_PIO32) - insl_far(iobase1, far_buffer, bsize / 4); + dprintf(16, "Read sector id=%p dest=%p\n", op->drive_g, buf_fl); + if (CONFIG_ATA_PIO32) + insl_fl(iobase1, buf_fl, blocksize / 4); else - insw_far(iobase1, far_buffer, bsize / 2); + insw_fl(iobase1, buf_fl, blocksize / 2); } - far_buffer += bsize; - - if (skiplast && current == count-1) - insx_discard(mode, iobase1, skiplast); + buf_fl += blocksize; - status = pause_await_ide(NOT_BSY, iobase1, iobase2, IDE_TIMEOUT); - if (status < 0) + status = pause_await_not_bsy(iobase1, iobase2); + if (status < 0) { // Error + op->count -= count; return status; + } - current++; - SET_EBDA(ata.trsfsectors, current); - if (current == count) + count--; + if (!count) break; - status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ - | ATA_CB_STAT_ERR); - if (status != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ)) { - DEBUGF("ata_transfer : more sectors left (status %02x)\n" - , (unsigned) status); - return 5; + status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR); + if (status != ATA_CB_STAT_DRQ) { + dprintf(6, "ata_pio_transfer : more sectors left (status %02x)\n" + , status); + op->count -= count; + return -6; } } - status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF - | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR); + status &= (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ + | ATA_CB_STAT_ERR); if (!iswrite) status &= ~ATA_CB_STAT_DF; - if (status != ATA_CB_STAT_RDY ) { - DEBUGF("ata_transfer : no sectors left (status %02x)\n" - , (unsigned) status); - return 4; + if (status != 0) { + dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status); + return -7; } - // Enable interrupts - outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); return 0; } -static noinline int -ata_transfer_disk(const struct ata_op_s *op, int iswrite) -{ - return ata_transfer(op->driveid, iswrite, op->count, IDE_SECTOR_SIZE - , 0, 0, op->far_buffer); -} -static noinline int -ata_transfer_cdrom(const struct ata_op_s *op) +/**************************************************************** + * ATA DMA transfers + ****************************************************************/ + +#define BM_CMD 0 +#define BM_CMD_MEMWRITE 0x08 +#define BM_CMD_START 0x01 +#define BM_STATUS 2 +#define BM_STATUS_IRQ 0x04 +#define BM_STATUS_ERROR 0x02 +#define BM_STATUS_ACTIVE 0x01 +#define BM_TABLE 4 + +struct sff_dma_prd { + u32 buf_fl; + u32 count; +}; + +// Check if DMA available and setup transfer if so. +static int +ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize) { - return ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE - , 0, 0, op->far_buffer); + if (! CONFIG_ATA_DMA) + return -1; + u32 dest = (u32)op->buf_fl; + if (dest & 1) + // Need minimum alignment of 1. + return -1; + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster); + if (! iomaster) + return -1; + u32 bytes = op->count * blocksize; + if (! bytes) + return -1; + + // Build PRD dma structure. + struct sff_dma_prd *dma = MAKE_FLATPTR( + get_ebda_seg() + , (void*)offsetof(struct extended_bios_data_area_s, extra_stack)); + struct sff_dma_prd *origdma = dma; + while (bytes) { + if (dma >= &origdma[16]) + // Too many descriptors.. + return -1; + u32 count = bytes; + u32 max = 0x10000 - (dest & 0xffff); + if (count > max) + count = max; + + SET_FLATPTR(dma->buf_fl, dest); + bytes -= count; + if (!bytes) + // Last descriptor. + count |= 1<<31; + dprintf(16, "dma@%p: %08x %08x\n", dma, dest, count); + dest += count; + SET_FLATPTR(dma->count, count); + dma++; + } + + // Program bus-master controller. + outl((u32)origdma, iomaster + BM_TABLE); + u8 oldcmd = inb(iomaster + BM_CMD) & ~(BM_CMD_MEMWRITE|BM_CMD_START); + outb(oldcmd | (iswrite ? 0x00 : BM_CMD_MEMWRITE), iomaster + BM_CMD); + outb(BM_STATUS_ERROR|BM_STATUS_IRQ, iomaster + BM_STATUS); + + return 0; } -static noinline int -ata_transfer_emu(const struct ata_op_s *op, int before, int after) +// Transfer data using DMA. +static int +ata_dma_transfer(struct disk_op_s *op) { - int vcount = op->count * 4 - before - after; - int ret = ata_transfer(op->driveid, 0, op->count, CDROM_SECTOR_SIZE - , before*512, after*512, op->far_buffer); - if (ret) { - SET_EBDA(ata.trsfsectors, 0); - return ret; + if (! CONFIG_ATA_DMA) + return -1; + dprintf(16, "ata_dma_transfer id=%p buf=%p\n", op->drive_g, op->buf_fl); + + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iomaster = GET_GLOBALFLAT(chan_gf->iomaster); + + // Start bus-master controller. + u8 oldcmd = inb(iomaster + BM_CMD); + outb(oldcmd | BM_CMD_START, iomaster + BM_CMD); + + u64 end = calc_future_tsc(IDE_TIMEOUT); + u8 status; + for (;;) { + status = inb(iomaster + BM_STATUS); + if (status & BM_STATUS_IRQ) + break; + // Transfer in progress + if (check_tsc(end)) { + // Timeout. + warn_timeout(); + break; + } + yield(); } - SET_EBDA(ata.trsfsectors, vcount); - return 0; + outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD); + + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); + int idestatus = pause_await_not_bsy(iobase1, iobase2); + + if ((status & (BM_STATUS_IRQ|BM_STATUS_ACTIVE)) == BM_STATUS_IRQ + && idestatus >= 0x00 + && (idestatus & (ATA_CB_STAT_BSY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ + | ATA_CB_STAT_ERR)) == 0x00) + // Success. + return 0; + + dprintf(6, "IDE DMA error (dma=%x ide=%x/%x/%x)\n", status, idestatus + , inb(iobase2 + ATA_CB_ASTAT), inb(iobase1 + ATA_CB_ERR)); + op->count = 0; + return -1; } @@ -354,53 +484,111 @@ ata_transfer_emu(const struct ata_op_s *op, int before, int after) * ATA hard drive functions ****************************************************************/ -static noinline int -send_cmd_disk(const struct ata_op_s *op, u16 command) +// Transfer data to harddrive using PIO protocol. +static int +ata_pio_cmd_data(struct disk_op_s *op, int iswrite, struct ata_pio_command *cmd) +{ + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); + + // Disable interrupts + outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); + + int ret = send_cmd(adrive_g, cmd); + if (ret) + goto fail; + ret = ata_wait_data(iobase1); + if (ret) + goto fail; + ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE); + +fail: + // Enable interrupts + outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); + return ret; +} + +// Transfer data to harddrive using DMA protocol. +static int +ata_dma_cmd_data(struct disk_op_s *op, struct ata_pio_command *cmd) +{ + if (! CONFIG_ATA_DMA) + return -1; + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + int ret = send_cmd(adrive_g, cmd); + if (ret) + return ret; + return ata_dma_transfer(op); +} + +// Read/write count blocks from a harddrive. +static int +ata_readwrite(struct disk_op_s *op, int iswrite) { - u8 slave = op->driveid % 2; u64 lba = op->lba; + int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE); + struct ata_pio_command cmd; memset(&cmd, 0, sizeof(cmd)); - cmd.command = command; if (op->count >= (1<<8) || lba + op->count >= (1<<28)) { cmd.sector_count2 = op->count >> 8; cmd.lba_low2 = lba >> 24; cmd.lba_mid2 = lba >> 32; cmd.lba_high2 = lba >> 40; - - cmd.command |= 0x04; lba &= 0xffffff; + + if (usepio) + cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS_EXT + : ATA_CMD_READ_SECTORS_EXT); + else + cmd.command = (iswrite ? ATA_CMD_WRITE_DMA_EXT + : ATA_CMD_READ_DMA_EXT); + } else { + if (usepio) + cmd.command = (iswrite ? ATA_CMD_WRITE_SECTORS + : ATA_CMD_READ_SECTORS); + else + cmd.command = (iswrite ? ATA_CMD_WRITE_DMA + : ATA_CMD_READ_DMA); } - cmd.feature = 0; cmd.sector_count = op->count; cmd.lba_low = lba; cmd.lba_mid = lba >> 8; cmd.lba_high = lba >> 16; - cmd.device = ((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) - | ((lba >> 24) & 0xf) | ATA_CB_DH_LBA); + cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA; - return send_cmd(op->driveid, &cmd); + int ret; + if (usepio) + ret = ata_pio_cmd_data(op, iswrite, &cmd); + else + ret = ata_dma_cmd_data(op, &cmd); + if (ret) + return DISK_RET_EBADTRACK; + return DISK_RET_SUCCESS; } -// Read/write count blocks from a harddrive. -__always_inline int -ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer) +// 16bit command demuxer for ATA harddrives. +int +process_ata_op(struct disk_op_s *op) { - struct ata_op_s op; - op.driveid = driveid; - op.lba = lba; - op.count = count; - op.far_buffer = far_buffer; - - int ret = send_cmd_disk(&op, command); - if (ret) - return ret; - - int iswrite = command == ATA_CMD_WRITE_SECTORS; - return ata_transfer_disk(&op, iswrite); + if (!CONFIG_ATA) + return 0; + + switch (op->command) { + case CMD_READ: + return ata_readwrite(op, 0); + case CMD_WRITE: + return ata_readwrite(op, 1); + default: + return process_ata_misc_op(op); + } } @@ -408,114 +596,86 @@ ata_cmd_data(int driveid, u16 command, u64 lba, u16 count, void *far_buffer) * ATAPI functions ****************************************************************/ +#define CDROM_CDB_SIZE 12 + // Low-level atapi command transmit function. -static __always_inline int -send_atapi_cmd(int driveid, u8 *cmdbuf, u8 cmdlen, u16 blocksize) +int +atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) { - u8 channel = driveid / 2; - u8 slave = driveid % 2; - u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1); - u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2); + if (! CONFIG_ATA) + return 0; + + struct atadrive_s *adrive_g = container_of( + op->drive_g, struct atadrive_s, drive); + struct ata_channel_s *chan_gf = GET_GLOBAL(adrive_g->chan_gf); + u16 iobase1 = GET_GLOBALFLAT(chan_gf->iobase1); + u16 iobase2 = GET_GLOBALFLAT(chan_gf->iobase2); struct ata_pio_command cmd; - cmd.sector_count = 0; - cmd.feature = 0; - cmd.lba_low = 0; + memset(&cmd, 0, sizeof(cmd)); cmd.lba_mid = blocksize; cmd.lba_high = blocksize >> 8; - cmd.device = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0; cmd.command = ATA_CMD_PACKET; - int ret = send_cmd(driveid, &cmd); - if (ret) - return ret; - - // Send command to device - outsw_far(iobase1, MAKE_FARPTR(GET_SEG(SS), (u32)cmdbuf), cmdlen / 2); - - int status = pause_await_ide(NOT_BSY_DRQ, iobase1, iobase2, IDE_TIMEOUT); - if (status < 0) - return status; - - return 0; -} - -// Low-level cdrom read atapi command transmit function. -static int -send_cmd_cdrom(const struct ata_op_s *op) -{ - u8 atacmd[12]; - memset(atacmd, 0, sizeof(atacmd)); - - atacmd[0]=0x28; // READ command - atacmd[7]=(op->count & 0xff00) >> 8; // Sectors - atacmd[8]=(op->count & 0x00ff); - atacmd[2]=(op->lba & 0xff000000) >> 24; // LBA - atacmd[3]=(op->lba & 0x00ff0000) >> 16; - atacmd[4]=(op->lba & 0x0000ff00) >> 8; - atacmd[5]=(op->lba & 0x000000ff); - - return send_atapi_cmd(op->driveid, atacmd, sizeof(atacmd) - , CDROM_SECTOR_SIZE); -} - -// Read sectors from the cdrom. -__always_inline int -cdrom_read(int driveid, u32 lba, u32 count, void *far_buffer) -{ - struct ata_op_s op; - op.driveid = driveid; - op.lba = lba; - op.count = count; - op.far_buffer = far_buffer; + // Disable interrupts + outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC); - int ret = send_cmd_cdrom(&op); + int ret = send_cmd(adrive_g, &cmd); if (ret) - return ret; + goto fail; + ret = ata_wait_data(iobase1); + if (ret) + goto fail; - return ata_transfer_cdrom(&op); -} + // Send command to device + outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2); -// Pretend the cdrom has 512 byte sectors (instead of 2048) and read -// sectors. -__always_inline int -cdrom_read_512(int driveid, u32 vlba, u32 vcount, void *far_buffer) -{ - u32 velba = vlba + vcount - 1; - u32 lba = vlba / 4; - u32 elba = velba / 4; - int count = elba - lba + 1; - int before = vlba % 4; - int after = 3 - (velba % 4); + int status = pause_await_not_bsy(iobase1, iobase2); + if (status < 0) { + ret = status; + goto fail; + } - struct ata_op_s op; - op.driveid = driveid; - op.lba = lba; - op.count = count; - op.far_buffer = far_buffer; + if (status & ATA_CB_STAT_ERR) { + u8 err = inb(iobase1 + ATA_CB_ERR); + // skip "Not Ready" + if (err != 0x20) + dprintf(6, "send_atapi_cmd : read error (status=%02x err=%02x)\n" + , status, err); + ret = -2; + goto fail; + } + if (!(status & ATA_CB_STAT_DRQ)) { + dprintf(6, "send_atapi_cmd : DRQ not set (status %02x)\n", status); + ret = -3; + goto fail; + } - DEBUGF("cdrom_read_512: id=%d vlba=%d vcount=%d buf=%p lba=%d elba=%d" - " count=%d before=%d after=%d\n" - , driveid, vlba, vcount, far_buffer, lba, elba - , count, before, after); + ret = ata_pio_transfer(op, 0, blocksize); - int ret = send_cmd_cdrom(&op); +fail: + // Enable interrupts + outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC); if (ret) - return ret; - - return ata_transfer_emu(&op, before, after); + return DISK_RET_EBADTRACK; + return DISK_RET_SUCCESS; } -// Send a simple atapi command to a drive. +// 16bit command demuxer for ATAPI cdroms. int -ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen - , u32 length, void *far_buffer) +process_atapi_op(struct disk_op_s *op) { - int ret = send_atapi_cmd(driveid, cmdbuf, cmdlen, length); - if (ret) - return ret; - - return ata_transfer(driveid, 0, 1, length, 0, 0, far_buffer); + if (!CONFIG_ATA) + return 0; + switch (op->command) { + case CMD_READ: + return cdb_read(op); + case CMD_FORMAT: + case CMD_WRITE: + return DISK_RET_EWRITEPROTECT; + default: + return process_ata_misc_op(op); + } } @@ -523,36 +683,32 @@ ata_cmd_packet(int driveid, u8 *cmdbuf, u8 cmdlen * ATA detect and init ****************************************************************/ -static void -report_model(int driveid, u8 *buffer) +// Send an identify device or identify device packet command. +static int +send_ata_identity(struct atadrive_s *adrive_g, u16 *buffer, int command) { - u8 model[41]; + memset(buffer, 0, DISK_SECTOR_SIZE); - // Read model name - int i; - for (i=0; i<40; i+=2) { - model[i] = buffer[i+54+1]; - model[i+1] = buffer[i+54]; - } + struct disk_op_s dop; + memset(&dop, 0, sizeof(dop)); + dop.drive_g = &adrive_g->drive; + dop.count = 1; + dop.lba = 1; + dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer); - // Reformat - model[40] = 0x00; - for (i=39; i>0; i--) { - if (model[i] != 0x20) - break; - model[i] = 0x00; - } + struct ata_pio_command cmd; + memset(&cmd, 0, sizeof(cmd)); + cmd.command = command; - u8 channel = driveid / 2; - u8 slave = driveid % 2; - // XXX - model on stack not %cs - printf("ata%d %s: %s", channel, slave ? " slave" : "master", model); + return ata_pio_cmd_data(&dop, 0, &cmd); } -static u8 -get_ata_version(u8 *buffer) +// Extract the ATA/ATAPI version info. +int +ata_extract_version(u16 *buffer) { - u16 ataversion = *(u16*)&buffer[160]; + // Extract ATA/ATAPI version. + u16 ataversion = buffer[80]; u8 version; for (version=15; version>0; version--) if (ataversion & (1<chan_gf = dummy->chan_gf; + adrive_g->slave = dummy->slave; + adrive_g->drive.cntl_id = adrive_g->chan_gf->chanid * 2 + dummy->slave; + adrive_g->drive.removable = (buffer[0] & 0x80) ? 1 : 0; + return adrive_g; +} - SET_EBDA(ata.devices[driveid].device, type); - SET_EBDA(ata.devices[driveid].removable, removable); - SET_EBDA(ata.devices[driveid].mode, mode); - SET_EBDA(ata.devices[driveid].blksize, blksize); +// Detect if the given drive is an atapi - initialize it if so. +static struct atadrive_s * +init_drive_atapi(struct atadrive_s *dummy, u16 *buffer) +{ + // Send an IDENTIFY_DEVICE_PACKET command to device + int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_PACKET_DEVICE); + if (ret) + return NULL; + + // Success - setup as ATAPI. + struct atadrive_s *adrive_g = init_atadrive(dummy, buffer); + if (!adrive_g) + return NULL; + adrive_g->drive.type = DTYPE_ATAPI; + adrive_g->drive.blksize = CDROM_SECTOR_SIZE; + adrive_g->drive.sectors = (u64)-1; + u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05; + char model[MAXMODEL+1]; + char *desc = znprintf(MAXDESCSIZE + , "DVD/CD [ata%d-%d: %s ATAPI-%d %s]" + , adrive_g->chan_gf->chanid, adrive_g->slave + , ata_extract_model(model, MAXMODEL, buffer) + , ata_extract_version(buffer) + , (iscd ? "DVD/CD" : "Device")); + dprintf(1, "%s\n", desc); // fill cdidmap - u8 cdcount = GET_EBDA(ata.cdcount); - SET_EBDA(ata.idmap[1][cdcount], driveid); - SET_EBDA(ata.cdcount, ++cdcount); - - report_model(driveid, buffer); - u8 version = get_ata_version(buffer); - if (GET_EBDA(ata.devices[driveid].device)==ATA_DEVICE_CDROM) - printf(" ATAPI-%d CD-Rom/DVD-Rom\n", version); - else - printf(" ATAPI-%d Device\n", version); + if (iscd) { + int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp, + adrive_g->chan_gf->chanid, + adrive_g->slave); + boot_add_cd(&adrive_g->drive, desc, prio); + } + + return adrive_g; } -static void -init_drive_ata(int driveid) +// Detect if the given drive is a regular ata drive - initialize it if so. +static struct atadrive_s * +init_drive_ata(struct atadrive_s *dummy, u16 *buffer) { - SET_EBDA(ata.devices[driveid].type,ATA_TYPE_ATA); - - // Temporary values to do the transfer - SET_EBDA(ata.devices[driveid].device, ATA_DEVICE_HD); - SET_EBDA(ata.devices[driveid].mode, ATA_MODE_PIO16); - - // Now we send a IDENTIFY command to ATA device - u8 buffer[0x0200]; - memset(buffer, 0, sizeof(buffer)); - u16 ret = ata_cmd_data(driveid, ATA_CMD_IDENTIFY_DEVICE - , 1, 1 - , MAKE_FARPTR(GET_SEG(SS), (u32)buffer)); + // Send an IDENTIFY_DEVICE command to device + int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE); if (ret) - BX_PANIC("ata-detect: Failed to detect ATA device\n"); + return NULL; - u8 removable = (buffer[0] & 0x80) ? 1 : 0; - u8 mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16; - u16 blksize = *(u16*)&buffer[10]; + // Success - setup as ATA. + struct atadrive_s *adrive_g = init_atadrive(dummy, buffer); + if (!adrive_g) + return NULL; + adrive_g->drive.type = DTYPE_ATA; + adrive_g->drive.blksize = DISK_SECTOR_SIZE; - u16 cylinders = *(u16*)&buffer[1*2]; // word 1 - u16 heads = *(u16*)&buffer[3*2]; // word 3 - u16 spt = *(u16*)&buffer[6*2]; // word 6 + adrive_g->drive.pchs.cylinders = buffer[1]; + adrive_g->drive.pchs.heads = buffer[3]; + adrive_g->drive.pchs.spt = buffer[6]; u64 sectors; - if (*(u16*)&buffer[83*2] & (1 << 10)) // word 83 - lba48 support - sectors = *(u64*)&buffer[100*2]; // word 100-103 + if (buffer[83] & (1 << 10)) // word 83 - lba48 support + sectors = *(u64*)&buffer[100]; // word 100-103 else - sectors = *(u32*)&buffer[60*2]; // word 60 and word 61 - - SET_EBDA(ata.devices[driveid].device,ATA_DEVICE_HD); - SET_EBDA(ata.devices[driveid].removable, removable); - SET_EBDA(ata.devices[driveid].mode, mode); - SET_EBDA(ata.devices[driveid].blksize, blksize); - SET_EBDA(ata.devices[driveid].pchs.heads, heads); - SET_EBDA(ata.devices[driveid].pchs.cylinders, cylinders); - SET_EBDA(ata.devices[driveid].pchs.spt, spt); - SET_EBDA(ata.devices[driveid].sectors, sectors); - - u8 channel = driveid / 2; - u8 slave = driveid % 2; - u8 translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2); - u8 shift; - for (shift=driveid%4; shift>0; shift--) - translation >>= 2; - translation &= 0x03; - - SET_EBDA(ata.devices[driveid].translation, translation); - - BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=" - , channel, slave, cylinders, heads, spt); - switch (translation) { - case ATA_TRANSLATION_NONE: - BX_INFO("none"); - break; - case ATA_TRANSLATION_LBA: - BX_INFO("lba"); - spt = 63; - if (sectors > 63*255*1024) { - heads = 255; - cylinders = 1024; + sectors = *(u32*)&buffer[60]; // word 60 and word 61 + adrive_g->drive.sectors = sectors; + u64 adjsize = sectors >> 11; + char adjprefix = 'M'; + if (adjsize >= (1 << 16)) { + adjsize >>= 10; + adjprefix = 'G'; + } + char model[MAXMODEL+1]; + char *desc = znprintf(MAXDESCSIZE + , "ata%d-%d: %s ATA-%d Hard-Disk (%u %ciBytes)" + , adrive_g->chan_gf->chanid, adrive_g->slave + , ata_extract_model(model, MAXMODEL, buffer) + , ata_extract_version(buffer) + , (u32)adjsize, adjprefix); + dprintf(1, "%s\n", desc); + + int prio = bootprio_find_ata_device(adrive_g->chan_gf->pci_tmp, + adrive_g->chan_gf->chanid, + adrive_g->slave); + // Register with bcv system. + boot_add_hd(&adrive_g->drive, desc, prio); + + return adrive_g; +} + +static u64 SpinupEnd; + +// Wait for non-busy status and check for "floating bus" condition. +static int +powerup_await_non_bsy(u16 base) +{ + u8 orstatus = 0; + u8 status; + for (;;) { + status = inb(base+ATA_CB_STAT); + if (!(status & ATA_CB_STAT_BSY)) break; + orstatus |= status; + if (orstatus == 0xff) { + dprintf(4, "powerup IDE floating\n"); + return orstatus; } - u32 sect = (u32)sectors / 63; - heads = sect / 1024; - if (heads>128) - heads = 255; - else if (heads>64) - heads = 128; - else if (heads>32) - heads = 64; - else if (heads>16) - heads = 32; - else - heads = 16; - cylinders = sect / heads; - break; - case ATA_TRANSLATION_RECHS: - BX_INFO("r-echs"); - // Take care not to overflow - if (heads==16) { - if (cylinders>61439) - cylinders=61439; - heads=15; - cylinders = (u16)((u32)(cylinders)*16/15); - } - // then go through the large bitshift process - case ATA_TRANSLATION_LARGE: - if (translation == ATA_TRANSLATION_LARGE) - BX_INFO("large"); - while (cylinders > 1024) { - cylinders >>= 1; - heads <<= 1; - - // If we max out the head count - if (heads > 127) - break; + if (check_tsc(SpinupEnd)) { + warn_timeout(); + return -1; } - break; + yield(); } - // clip to 1024 cylinders in lchs - if (cylinders > 1024) - cylinders = 1024; - BX_INFO(" LCHS=%d/%d/%d\n", cylinders, heads, spt); - - SET_EBDA(ata.devices[driveid].lchs.heads, heads); - SET_EBDA(ata.devices[driveid].lchs.cylinders, cylinders); - SET_EBDA(ata.devices[driveid].lchs.spt, spt); - - // fill hdidmap - u8 hdcount = GET_EBDA(ata.hdcount); - SET_EBDA(ata.idmap[0][hdcount], driveid); - SET_EBDA(ata.hdcount, ++hdcount); - - u64 sizeinmb = GET_EBDA(ata.devices[driveid].sectors); - sizeinmb >>= 11; - - report_model(driveid, buffer); - u8 version = get_ata_version(buffer); - if (sizeinmb < (1 << 16)) - printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u32)sizeinmb); - else - printf(" ATA-%d Hard-Disk (%u GBytes)\n", version - , (u32)(sizeinmb >> 10)); + dprintf(6, "powerup iobase=%x st=%x\n", base, status); + return status; } +// Detect any drives attached to a given controller. static void -init_drive_unknown(int driveid) +ata_detect(void *data) { - SET_EBDA(ata.devices[driveid].type,ATA_TYPE_UNKNOWN); - - u8 channel = driveid / 2; - u8 slave = driveid % 2; - printf("ata%d %s: Unknown device\n", channel, slave ? " slave" : "master"); -} - -static void -ata_detect() -{ -#if CONFIG_MAX_ATA_INTERFACES > 0 - SET_EBDA(ata.channels[0].iface,ATA_IFACE_ISA); - SET_EBDA(ata.channels[0].iobase1,0x1f0); - SET_EBDA(ata.channels[0].iobase2,0x3f0); - SET_EBDA(ata.channels[0].irq,14); -#endif -#if CONFIG_MAX_ATA_INTERFACES > 1 - SET_EBDA(ata.channels[1].iface,ATA_IFACE_ISA); - SET_EBDA(ata.channels[1].iobase1,0x170); - SET_EBDA(ata.channels[1].iobase2,0x370); - SET_EBDA(ata.channels[1].irq,15); -#endif -#if CONFIG_MAX_ATA_INTERFACES > 2 - SET_EBDA(ata.channels[2].iface,ATA_IFACE_ISA); - SET_EBDA(ata.channels[2].iobase1,0x1e8); - SET_EBDA(ata.channels[2].iobase2,0x3e0); - SET_EBDA(ata.channels[2].irq,12); -#endif -#if CONFIG_MAX_ATA_INTERFACES > 3 - SET_EBDA(ata.channels[3].iface,ATA_IFACE_ISA); - SET_EBDA(ata.channels[3].iobase1,0x168); - SET_EBDA(ata.channels[3].iobase2,0x360); - SET_EBDA(ata.channels[3].irq,11); -#endif -#if CONFIG_MAX_ATA_INTERFACES > 4 -#error Please fill the ATA interface informations -#endif - + struct ata_channel_s *chan_gf = data; + struct atadrive_s dummy; + memset(&dummy, 0, sizeof(dummy)); + dummy.chan_gf = chan_gf; // Device detection - int driveid; - for(driveid=0; driveidiobase1; + int status = powerup_await_non_bsy(iobase1); + if (status < 0) + continue; + u8 newdh = slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0; + outb(newdh, iobase1+ATA_CB_DH); + ndelay(400); + status = powerup_await_non_bsy(iobase1); + if (status < 0) + continue; - // Look for device - outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH); - outb(0x55, iobase1+ATA_CB_SC); - outb(0xaa, iobase1+ATA_CB_SN); - outb(0xaa, iobase1+ATA_CB_SC); - outb(0x55, iobase1+ATA_CB_SN); + // Check if ioport registers look valid. + outb(newdh, iobase1+ATA_CB_DH); + u8 dh = inb(iobase1+ATA_CB_DH); outb(0x55, iobase1+ATA_CB_SC); outb(0xaa, iobase1+ATA_CB_SN); - - // If we found something u8 sc = inb(iobase1+ATA_CB_SC); u8 sn = inb(iobase1+ATA_CB_SN); - - if (sc != 0x55 || sn != 0xaa) + dprintf(6, "ata_detect ata%d-%d: sc=%x sn=%x dh=%x\n" + , chan_gf->chanid, slave, sc, sn, dh); + if (sc != 0x55 || sn != 0xaa || dh != newdh) continue; + // Prepare new drive. + dummy.slave = slave; + // reset the channel - ata_reset(driveid); - - // check for ATA or ATAPI - outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1+ATA_CB_DH); - sc = inb(iobase1+ATA_CB_SC); - sn = inb(iobase1+ATA_CB_SN); - if (sc!=0x01 || sn!=0x01) { - init_drive_unknown(driveid); - continue; + if (!didreset) { + ata_reset(&dummy); + didreset = 1; } - u8 cl = inb(iobase1+ATA_CB_CL); - u8 ch = inb(iobase1+ATA_CB_CH); - u8 st = inb(iobase1+ATA_CB_STAT); - - if (cl==0x14 && ch==0xeb) - init_drive_atapi(driveid); - else if (cl==0x00 && ch==0x00 && st!=0x00) - init_drive_ata(driveid); - else if (cl==0xff && ch==0xff) - // None - continue; - else - init_drive_unknown(driveid); - } - // Store the device count - SET_BDA(disk_count, GET_EBDA(ata.hdcount)); + // check for ATAPI + u16 buffer[256]; + struct atadrive_s *adrive_g = init_drive_atapi(&dummy, buffer); + if (!adrive_g) { + // Didn't find an ATAPI drive - look for ATA drive. + u8 st = inb(iobase1+ATA_CB_STAT); + if (!st) + // Status not set - can't be a valid drive. + continue; + + // Wait for RDY. + int ret = await_rdy(iobase1); + if (ret < 0) + continue; + + // check for ATA. + adrive_g = init_drive_ata(&dummy, buffer); + if (!adrive_g) + // No ATA drive found + continue; + } - printf("\n"); + u16 resetresult = buffer[93]; + dprintf(6, "ata_detect resetresult=%04x\n", resetresult); + if (!slave && (resetresult & 0xdf61) == 0x4041) + // resetresult looks valid and device 0 is responding to + // device 1 requests - device 1 must not be present - skip + // detection. + break; + } +} - // FIXME : should use bios=cmos|auto|disable bits - // FIXME : should know about translation bits - // FIXME : move hard_drive_post here +// Initialize an ata controller and detect its drives. +static void +init_controller(struct pci_device *pci, int irq + , u32 port1, u32 port2, u32 master) +{ + static int chanid = 0; + struct ata_channel_s *chan_gf = malloc_fseg(sizeof(*chan_gf)); + if (!chan_gf) { + warn_noalloc(); + return; + } + chan_gf->chanid = chanid++; + chan_gf->irq = irq; + chan_gf->pci_bdf = pci ? pci->bdf : -1; + chan_gf->pci_tmp = pci; + chan_gf->iobase1 = port1; + chan_gf->iobase2 = port2; + chan_gf->iomaster = master; + dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n" + , chanid, port1, port2, master, irq, chan_gf->pci_bdf); + run_thread(ata_detect, chan_gf); } +#define IRQ_ATA1 14 +#define IRQ_ATA2 15 + +// Handle controllers on an ATA PCI device. static void -ata_init() +init_pciata(struct pci_device *pci, u8 prog_if) { - // hdidmap and cdidmap init. - u8 device; - for (device=0; device < CONFIG_MAX_ATA_DEVICES; device++) { - SET_EBDA(ata.idmap[0][device], CONFIG_MAX_ATA_DEVICES); - SET_EBDA(ata.idmap[1][device], CONFIG_MAX_ATA_DEVICES); + pci->have_driver = 1; + u16 bdf = pci->bdf; + u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE); + int master = 0; + if (CONFIG_ATA_DMA && prog_if & 0x80) { + // Check for bus-mastering. + u32 bar = pci_config_readl(bdf, PCI_BASE_ADDRESS_4); + if (bar & PCI_BASE_ADDRESS_SPACE_IO) { + master = bar & PCI_BASE_ADDRESS_IO_MASK; + pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER); + } + } + + u32 port1, port2, irq; + if (prog_if & 1) { + port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0) + & PCI_BASE_ADDRESS_IO_MASK); + port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_1) + & PCI_BASE_ADDRESS_IO_MASK); + irq = pciirq; + } else { + port1 = PORT_ATA1_CMD_BASE; + port2 = PORT_ATA1_CTRL_BASE; + irq = IRQ_ATA1; } + init_controller(pci, irq, port1, port2, master); + + if (prog_if & 4) { + port1 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_2) + & PCI_BASE_ADDRESS_IO_MASK); + port2 = (pci_config_readl(bdf, PCI_BASE_ADDRESS_3) + & PCI_BASE_ADDRESS_IO_MASK); + irq = pciirq; + } else { + port1 = PORT_ATA2_CMD_BASE; + port2 = PORT_ATA2_CTRL_BASE; + irq = IRQ_ATA2; + } + init_controller(pci, irq, port1, port2, master ? master + 8 : 0); +} + +static void +found_genericata(struct pci_device *pci, void *arg) +{ + init_pciata(pci, pci->prog_if); } static void -fill_hdinfo(int hdnum, u8 typecmos, u8 basecmos) +found_compatibleahci(struct pci_device *pci, void *arg) { - u8 type = inb_cmos(typecmos); - if (type != 47) - // XXX - halt + if (CONFIG_AHCI) + // Already handled directly via native ahci interface. return; + init_pciata(pci, 0x8f); +} + +static const struct pci_device_id pci_ata_tbl[] = { + PCI_DEVICE_CLASS(PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_IDE + , found_genericata), + PCI_DEVICE(PCI_VENDOR_ID_ATI, 0x4391, found_compatibleahci), + PCI_DEVICE_END, +}; - SET_EBDA(fdpt[hdnum].precompensation, ((inb_cmos(basecmos+4) << 8) - | inb_cmos(basecmos+3))); - SET_EBDA(fdpt[hdnum].drive_control_byte, inb_cmos(basecmos+5)); - SET_EBDA(fdpt[hdnum].landing_zone, ((inb_cmos(basecmos+7) << 8) - | inb_cmos(basecmos+6))); - u16 cyl = (inb_cmos(basecmos+1) << 8) | inb_cmos(basecmos+0); - u8 heads = inb_cmos(basecmos+2); - u8 sectors = inb_cmos(basecmos+8); - if (cyl < 1024) { - // no logical CHS mapping used, just physical CHS - // use Standard Fixed Disk Parameter Table (FDPT) - SET_EBDA(fdpt[hdnum].cylinders, cyl); - SET_EBDA(fdpt[hdnum].heads, heads); - SET_EBDA(fdpt[hdnum].sectors, sectors); +// Locate and init ata controllers. +static void +ata_init(void) +{ + if (!CONFIG_COREBOOT && !PCIDevices) { + // No PCI devices found - probably a QEMU "-M isapc" machine. + // Try using ISA ports for ATA controllers. + init_controller(NULL, IRQ_ATA1 + , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0); + init_controller(NULL, IRQ_ATA2 + , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0); return; } - // complies with Phoenix style Translated Fixed Disk Parameter - // Table (FDPT) - SET_EBDA(fdpt[hdnum].phys_cylinders, cyl); - SET_EBDA(fdpt[hdnum].phys_heads, heads); - SET_EBDA(fdpt[hdnum].phys_sectors, sectors); - SET_EBDA(fdpt[hdnum].sectors, sectors); - SET_EBDA(fdpt[hdnum].a0h_signature, 0xa0); - if (cyl > 8192) { - cyl >>= 4; - heads <<= 4; - } else if (cyl > 4096) { - cyl >>= 3; - heads <<= 3; - } else if (cyl > 2048) { - cyl >>= 2; - heads <<= 2; + // Scan PCI bus for ATA adapters + struct pci_device *pci; + foreachpci(pci) { + pci_init_device(pci_ata_tbl, pci, NULL); } - SET_EBDA(fdpt[hdnum].cylinders, cyl); - SET_EBDA(fdpt[hdnum].heads, heads); - u8 *p = MAKE_FARPTR(SEG_EBDA, offsetof(struct extended_bios_data_area_s - , fdpt[hdnum])); - u8 sum = checksum(p, FIELD_SIZEOF(struct extended_bios_data_area_s - , fdpt[hdnum]) - 1); - SET_EBDA(fdpt[hdnum].checksum, -sum); } void -hard_drive_setup() +ata_setup(void) { - outb(0x0a, PORT_HD_DATA); // 0000 1010 = reserved, disable IRQ 14 - SET_BDA(disk_count, 1); + ASSERT32FLAT(); + if (!CONFIG_ATA) + return; + + dprintf(3, "init hard drives\n"); + + SpinupEnd = calc_future_tsc(IDE_TIMEOUT); + ata_init(); + SET_BDA(disk_control_byte, 0xc0); - // move disk geometry data from CMOS to EBDA disk parameter table(s) - u8 diskinfo = inb_cmos(CMOS_DISK_DATA); - if ((diskinfo & 0xf0) == 0xf0) - // Fill EBDA table for hard disk 0. - fill_hdinfo(0, CMOS_DISK_DRIVE1_TYPE, CMOS_DISK_DRIVE1_CYL); - if ((diskinfo & 0x0f) == 0x0f) - // XXX - bochs halts on any other type - // Fill EBDA table for hard disk 1. - fill_hdinfo(1, CMOS_DISK_DRIVE2_TYPE, CMOS_DISK_DRIVE2_CYL); - - if (CONFIG_ATA) { - ata_init(); - ata_detect(); - } + enable_hwirq(14, FUNC16(entry_76)); }