// Low level ATA disk access
//
-// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
// 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" // add_bcv_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_TIMEOUT 32000 //32 seconds max for IDE ops
-#define IDE_TIMEOUT 32000u //32 seconds max for IDE ops
+struct ata_channel_s ATA_channels[CONFIG_MAX_ATA_INTERFACES] VAR16VISIBLE;
-#define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
-#define DEBUGF(fmt, args...)
-// XXX - lots of redundancy in this file.
+/****************************************************************
+ * Helper functions
+ ****************************************************************/
-static int
-await_ide(u8 when_done, u16 base, u16 timeout)
+// Wait for the specified ide state
+static inline int
+await_ide(u8 mask, u8 flags, u16 base, u16 timeout)
{
- u32 time=0,last=0;
- // for the times you're supposed to throw one away
- u16 status = inb(base + ATA_CB_STAT);
+ u64 end = calc_future_tsc(timeout);
for (;;) {
- status = inb(base+ATA_CB_STAT);
- time++;
- u8 result;
- 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);
- else if (when_done == TIMEOUT)
- result = 0;
-
- if (result)
- return 0;
- // 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);
+ u8 status = inb(base+ATA_CB_STAT);
+ if ((status & mask) == flags)
+ return status;
+ if (check_time(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);
+}
-// ---------------------------------------------------------------------------
-// ATA/ATAPI driver : software reset
-// ---------------------------------------------------------------------------
-// ATA-3
-// 8.2.1 Software reset - Device 0
-
-void
-ata_reset(u16 device)
+// Wait for the device to be ready.
+static int
+await_rdy(u16 base)
{
- u16 iobase1, iobase2;
- u8 channel, slave, sn, sc;
- u8 type;
+ return await_ide(ATA_CB_STAT_RDY, ATA_CB_STAT_RDY, base, IDE_TIMEOUT);
+}
- channel = device / 2;
- slave = device % 2;
+// Wait for ide state - pauses for one ata cycle first.
+static inline int
+pause_await_not_bsy(u16 iobase1, u16 iobase2)
+{
+ // Wait one PIO transfer cycle.
+ inb(iobase2 + ATA_CB_ASTAT);
- iobase1 = GET_EBDA(ata.channels[channel].iobase1);
- iobase2 = GET_EBDA(ata.channels[channel].iobase2);
+ return await_not_bsy(iobase1);
+}
- // Reset
+// Wait for ide state - pause for 400ns first.
+static inline int
+ndelay_await_not_bsy(u16 iobase1)
+{
+ ndelay(400);
+ return await_not_bsy(iobase1);
+}
- // 8.2.1 (a) -- set SRST in DC
+// Reset a drive
+static void
+ata_reset(struct drive_s *drive_g)
+{
+ u8 ataid = GET_GLOBAL(drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u8 slave = ataid % 2;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
+
+ dprintf(6, "ata_reset drive=%p\n", drive_g);
+ // 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);
-
- type=GET_EBDA(ata.devices[device].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);
+ msleep(2);
+
+ // 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_time(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(drive_g->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);
}
-static void
-insw(u16 port, u16 segment, u16 offset, u16 count)
+// Check for drive RDY for 16bit interface command.
+static int
+isready(struct drive_s *drive_g)
{
- u16 i;
- for (i=0; i<count; i++) {
- u16 d = inw(port);
- SET_FARVAR(segment, *(u16*)(offset + 2*i), d);
- }
+ // Read the status from controller
+ u8 ataid = GET_GLOBAL(drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].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;
}
-static void
-insl(u16 port, u16 segment, u16 offset, u16 count)
+// Default 16bit command demuxer for ATA and ATAPI devices.
+static int
+process_ata_misc_op(struct disk_op_s *op)
{
- u16 i;
- for (i=0; i<count; i++) {
- u32 d = inl(port);
- SET_FARVAR(segment, *(u32*)(offset + 4*i), d);
+ if (!CONFIG_ATA)
+ return 0;
+
+ switch (op->command) {
+ case CMD_RESET:
+ ata_reset(op->drive_g);
+ return DISK_RET_SUCCESS;
+ case CMD_ISREADY:
+ return isready(op->drive_g);
+ case CMD_FORMAT:
+ case CMD_VERIFY:
+ case CMD_SEEK:
+ return DISK_RET_SUCCESS;
+ default:
+ op->count = 0;
+ return DISK_RET_EPARAM;
}
}
-static void
-outsw(u16 port, u16 segment, u16 offset, u16 count)
+
+/****************************************************************
+ * ATA send command
+ ****************************************************************/
+
+struct ata_pio_command {
+ u8 feature;
+ u8 sector_count;
+ u8 lba_low;
+ u8 lba_mid;
+ u8 lba_high;
+ u8 device;
+ u8 command;
+
+ u8 feature2;
+ u8 sector_count2;
+ u8 lba_low2;
+ u8 lba_mid2;
+ u8 lba_high2;
+};
+
+// Send an ata command to the drive.
+static int
+send_cmd(struct drive_s *drive_g, struct ata_pio_command *cmd)
{
- u16 i;
- for (i=0; i<count; i++) {
- u16 d = GET_FARVAR(segment, *(u16*)(offset + 2*i));
- outw(d, port);
+ u8 ataid = GET_GLOBAL(drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u8 slave = ataid % 2;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+
+ // 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;
}
-}
-static void
-outsl(u16 port, u16 segment, u16 offset, u16 count)
-{
- u16 i;
- for (i=0; i<count; i++) {
- u32 d = GET_FARVAR(segment, *(u32*)(offset + 4*i));
- outl(d, port);
+ // 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);
+ outb(cmd->lba_high2, iobase1 + ATA_CB_CH);
}
+ outb(cmd->feature, iobase1 + ATA_CB_FR);
+ outb(cmd->sector_count, iobase1 + ATA_CB_SC);
+ 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->command, iobase1 + ATA_CB_CMD);
+
+ return 0;
}
+// 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;
-// ---------------------------------------------------------------------------
-// ATA/ATAPI driver : execute a data-in command
-// ---------------------------------------------------------------------------
- // returns
- // 0 : no error
- // 1 : BUSY bit set
- // 2 : read error
- // 3 : expected DRQ=1
- // 4 : no sectors left to read/verify
- // 5 : more sectors to read/verify
- // 6 : no sectors left to write
- // 7 : more sectors to write
-u16
-ata_cmd_data_in(u16 device, u16 command, u16 count, u16 cylinder
- , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
-{
- DEBUGF("ata_cmd_data_in d=%d cmd=%d count=%d c=%d h=%d s=%d"
- " lba=%d seg=%x off=%x\n"
- , device, command, count, cylinder, head, sector
- , lba, segment, offset);
-
- u8 channel = device / 2;
- u8 slave = device % 2;
-
- u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
- u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
- u8 mode = GET_EBDA(ata.devices[device].mode);
-
- // Reset count of transferred data
- SET_EBDA(ata.trsfsectors,0);
- SET_EBDA(ata.trsfbytes,0L);
- u8 current = 0;
+ if (status & ATA_CB_STAT_ERR) {
+ 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)) {
+ dprintf(6, "send_cmd : DRQ not set (status %02x)\n", status);
+ return -5;
+ }
- u8 status = inb(iobase1 + ATA_CB_STAT);
- if (status & ATA_CB_STAT_BSY)
- return 1;
+ return 0;
+}
+// Send an ata command that does not transfer any further data.
+int
+ata_cmd_nondata(struct drive_s *drive_g, struct ata_pio_command *cmd)
+{
+ u8 ataid = GET_GLOBAL(drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
+
+ // Disable interrupts
outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
- // sector will be 0 only on lba access. Convert to lba-chs
- if (sector == 0) {
- if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
- outb(0x00, iobase1 + ATA_CB_FR);
- outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
- outb(lba >> 24, iobase1 + ATA_CB_SN);
- outb(0, iobase1 + ATA_CB_CL);
- outb(0, iobase1 + ATA_CB_CH);
- command |= 0x04;
- count &= (1UL << 8) - 1;
- lba &= (1UL << 24) - 1;
- }
- sector = (u16) (lba & 0x000000ffL);
- cylinder = (u16) ((lba>>8) & 0x0000ffffL);
- head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
+ int ret = send_cmd(drive_g, cmd);
+ if (ret)
+ goto fail;
+ ret = ndelay_await_not_bsy(iobase1);
+ if (ret < 0)
+ goto fail;
+
+ if (ret & ATA_CB_STAT_ERR) {
+ dprintf(6, "nondata cmd : read error (status=%02x err=%02x)\n"
+ , ret, inb(iobase1 + ATA_CB_ERR));
+ ret = -4;
+ goto fail;
}
-
- outb(0x00, iobase1 + ATA_CB_FR);
- outb(count, iobase1 + ATA_CB_SC);
- outb(sector, iobase1 + ATA_CB_SN);
- outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
- outb(cylinder >> 8, iobase1 + ATA_CB_CH);
- outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
- , iobase1 + ATA_CB_DH);
- outb(command, iobase1 + ATA_CB_CMD);
-
- await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
- status = inb(iobase1 + ATA_CB_STAT);
-
- if (status & ATA_CB_STAT_ERR) {
- DEBUGF("ata_cmd_data_in : read error\n");
- return 2;
- } else if ( !(status & ATA_CB_STAT_DRQ) ) {
- DEBUGF("ata_cmd_data_in : DRQ not set (status %02x)\n"
- , (unsigned) status);
- return 3;
+ if (ret & ATA_CB_STAT_DRQ) {
+ dprintf(6, "nondata cmd : DRQ set (status %02x)\n", ret);
+ ret = -5;
+ goto fail;
}
- // FIXME : move seg/off translation here
+fail:
+ // Enable interrupts
+ outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
+
+ return ret;
+}
- irq_enable();
- while (1) {
+/****************************************************************
+ * ATA PIO transfers
+ ****************************************************************/
- if (offset > 0xf800) {
- offset -= 0x800;
- segment += 0x80;
+// Transfer 'op->count' 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);
+
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
+ int count = op->count;
+ void *buf_fl = op->buf_fl;
+ int status;
+ for (;;) {
+ if (iswrite) {
+ // Write data to controller
+ 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_fl(iobase1, buf_fl, blocksize / 2);
+ } else {
+ // Read data from controller
+ 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_fl(iobase1, buf_fl, blocksize / 2);
}
+ buf_fl += blocksize;
- if (mode == ATA_MODE_PIO32)
- insl(iobase1, segment, offset, 512 / 4);
- else
- insw(iobase1, segment, offset, 512 / 2);
- offset += 512;
+ status = pause_await_not_bsy(iobase1, iobase2);
+ if (status < 0) {
+ // Error
+ op->count -= count;
+ return status;
+ }
- current++;
- SET_EBDA(ata.trsfsectors,current);
count--;
- await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
- status = inb(iobase1 + ATA_CB_STAT);
- if (count == 0) {
- if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
- | ATA_CB_STAT_ERR) )
- != ATA_CB_STAT_RDY ) {
- DEBUGF("ata_cmd_data_in : no sectors left (status %02x)\n"
- , (unsigned) status);
- return 4;
- }
+ if (!count)
break;
- }
- else {
- if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
- | ATA_CB_STAT_ERR) )
- != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
- DEBUGF("ata_cmd_data_in : more sectors left (status %02x)\n"
- , (unsigned) status);
- return 5;
- }
- continue;
+ 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;
}
}
- // Enable interrupts
- outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
+
+ 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 != 0) {
+ dprintf(6, "ata_pio_transfer : no sectors left (status %02x)\n", status);
+ return -7;
+ }
+
return 0;
}
-// ---------------------------------------------------------------------------
-// ATA/ATAPI driver : execute a data-out command
-// ---------------------------------------------------------------------------
- // returns
- // 0 : no error
- // 1 : BUSY bit set
- // 2 : read error
- // 3 : expected DRQ=1
- // 4 : no sectors left to read/verify
- // 5 : more sectors to read/verify
- // 6 : no sectors left to write
- // 7 : more sectors to write
-u16
-ata_cmd_data_out(u16 device, u16 command, u16 count, u16 cylinder
- , u16 head, u16 sector, u32 lba, u16 segment, u16 offset)
-{
- DEBUGF("ata_cmd_data_out d=%d cmd=%d count=%d c=%d h=%d s=%d"
- " lba=%d seg=%x off=%x\n"
- , device, command, count, cylinder, head, sector
- , lba, segment, offset);
-
- u8 channel = device / 2;
- u8 slave = device % 2;
-
- u16 iobase1 = GET_EBDA(ata.channels[channel].iobase1);
- u16 iobase2 = GET_EBDA(ata.channels[channel].iobase2);
- u8 mode = GET_EBDA(ata.devices[device].mode);
-
- // Reset count of transferred data
- SET_EBDA(ata.trsfsectors,0);
- SET_EBDA(ata.trsfbytes,0L);
- u8 current = 0;
- u8 status = inb(iobase1 + ATA_CB_STAT);
- if (status & ATA_CB_STAT_BSY)
- return 1;
+/****************************************************************
+ * ATA DMA transfers
+ ****************************************************************/
- outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
+#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
- // sector will be 0 only on lba access. Convert to lba-chs
- if (sector == 0) {
- if ((count >= 1 << 8) || (lba + count >= 1UL << 28)) {
- outb(0x00, iobase1 + ATA_CB_FR);
- outb((count >> 8) & 0xff, iobase1 + ATA_CB_SC);
- outb(lba >> 24, iobase1 + ATA_CB_SN);
- outb(0, iobase1 + ATA_CB_CL);
- outb(0, iobase1 + ATA_CB_CH);
- command |= 0x04;
- count &= (1UL << 8) - 1;
- lba &= (1UL << 24) - 1;
- }
- sector = (u16) (lba & 0x000000ffL);
- cylinder = (u16) ((lba>>8) & 0x0000ffffL);
- head = ((u16) ((lba>>24) & 0x0000000fL)) | ATA_CB_DH_LBA;
- }
+struct sff_dma_prd {
+ u32 buf_fl;
+ u32 count;
+};
- outb(0x00, iobase1 + ATA_CB_FR);
- outb(count, iobase1 + ATA_CB_SC);
- outb(sector, iobase1 + ATA_CB_SN);
- outb(cylinder & 0x00ff, iobase1 + ATA_CB_CL);
- outb(cylinder >> 8, iobase1 + ATA_CB_CH);
- outb((slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (u8) head
- , iobase1 + ATA_CB_DH);
- outb(command, iobase1 + ATA_CB_CMD);
-
- await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
- status = inb(iobase1 + ATA_CB_STAT);
-
- if (status & ATA_CB_STAT_ERR) {
- DEBUGF("ata_cmd_data_out : read error\n");
- return 2;
- } else if ( !(status & ATA_CB_STAT_DRQ) ) {
- DEBUGF("ata_cmd_data_out : DRQ not set (status %02x)\n"
- , (unsigned) status);
- return 3;
+// Check if DMA available and setup transfer if so.
+static int
+ata_try_dma(struct disk_op_s *op, int iswrite, int blocksize)
+{
+ if (! CONFIG_ATA_DMA)
+ return -1;
+ u32 dest = (u32)op->buf_fl;
+ if (dest & 1)
+ // Need minimum alignment of 1.
+ return -1;
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iomaster = GET_GLOBAL(ATA_channels[channel].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;
+ if (count > 0x10000)
+ count = 0x10000;
+ 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++;
}
- // FIXME : move seg/off translation here
+ // 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);
- irq_enable();
+ return 0;
+}
- while (1) {
+// Transfer data using DMA.
+static int
+ata_dma_transfer(struct disk_op_s *op)
+{
+ if (! CONFIG_ATA_DMA)
+ return -1;
+ dprintf(16, "ata_dma_transfer id=%p buf=%p\n"
+ , op->drive_g, op->buf_fl);
- if (offset > 0xf800) {
- offset -= 0x800;
- segment += 0x80;
- }
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iomaster = GET_GLOBAL(ATA_channels[channel].iomaster);
- if (mode == ATA_MODE_PIO32)
- outsl(iobase1, segment, offset, 512 / 4);
- else
- outsw(iobase1, segment, offset, 512 / 2);
- offset += 512;
+ // Start bus-master controller.
+ u8 oldcmd = inb(iomaster + BM_CMD);
+ outb(oldcmd | BM_CMD_START, iomaster + BM_CMD);
- current++;
- SET_EBDA(ata.trsfsectors,current);
- count--;
- status = inb(iobase1 + ATA_CB_STAT);
- if (count == 0) {
- if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
- | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
- != ATA_CB_STAT_RDY ) {
- DEBUGF("ata_cmd_data_out : no sectors left (status %02x)\n"
- , (unsigned) status);
- return 6;
- }
+ 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_time(end)) {
+ // Timeout.
+ warn_timeout();
break;
- } else {
- if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ
- | ATA_CB_STAT_ERR) )
- != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
- DEBUGF("ata_cmd_data_out : more sectors left (status %02x)\n"
- , (unsigned) status);
- return 7;
- }
- continue;
}
+ yield();
}
- // Enable interrupts
- outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
- return 0;
+ outb(oldcmd & ~BM_CMD_START, iomaster + BM_CMD);
+
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].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;
}
-// ---------------------------------------------------------------------------
-// ATA/ATAPI driver : execute a packet command
-// ---------------------------------------------------------------------------
- // returns
- // 0 : no error
- // 1 : error in parameters
- // 2 : BUSY bit set
- // 3 : error
- // 4 : not ready
-u16
-ata_cmd_packet(u16 device, u8 *cmdbuf, u8 cmdlen, u16 header
- , u32 length, u8 inout, u16 bufseg, u16 bufoff)
-{
- u16 iobase1, iobase2;
- u16 lcount, lbefore, lafter, count;
- u8 channel, slave;
- u8 status, mode, lmode;
- u32 transfer;
-
- channel = device / 2;
- slave = device % 2;
-
- // Data out is not supported yet
- if (inout == ATA_DATA_OUT) {
- BX_INFO("ata_cmd_packet: DATA_OUT not supported yet\n");
- return 1;
- }
- // The header length must be even
- if (header & 1) {
- DEBUGF("ata_cmd_packet : header must be even (%04x)\n", header);
- return 1;
- }
+/****************************************************************
+ * ATA hard drive functions
+ ****************************************************************/
- iobase1 = GET_EBDA(ata.channels[channel].iobase1);
- iobase2 = GET_EBDA(ata.channels[channel].iobase2);
- mode = GET_EBDA(ata.devices[device].mode);
- transfer= 0L;
+// 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)
+{
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
- if (cmdlen < 12)
- cmdlen=12;
- if (cmdlen > 12)
- cmdlen=16;
- cmdlen>>=1;
+ // Disable interrupts
+ outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
- // Reset count of transferred data
- SET_EBDA(ata.trsfsectors,0);
- SET_EBDA(ata.trsfbytes,0L);
+ int ret = send_cmd(op->drive_g, cmd);
+ if (ret)
+ goto fail;
+ ret = ata_wait_data(iobase1);
+ if (ret)
+ goto fail;
+ ret = ata_pio_transfer(op, iswrite, DISK_SECTOR_SIZE);
- status = inb(iobase1 + ATA_CB_STAT);
- if (status & ATA_CB_STAT_BSY)
- return 2;
+fail:
+ // Enable interrupts
+ outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
+ return ret;
+}
- outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
- outb(0x00, iobase1 + ATA_CB_FR);
- outb(0x00, iobase1 + ATA_CB_SC);
- outb(0x00, iobase1 + ATA_CB_SN);
- outb(0xfff0 & 0x00ff, iobase1 + ATA_CB_CL);
- outb(0xfff0 >> 8, iobase1 + ATA_CB_CH);
- outb(slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0, iobase1 + ATA_CB_DH);
- outb(ATA_CMD_PACKET, iobase1 + ATA_CB_CMD);
-
- // Device should ok to receive command
- await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
- status = inb(iobase1 + ATA_CB_STAT);
+// 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;
+ int ret = send_cmd(op->drive_g, cmd);
+ if (ret)
+ return ret;
+ return ata_dma_transfer(op);
+}
- if (status & ATA_CB_STAT_ERR) {
- DEBUGF("ata_cmd_packet : error, status is %02x\n", status);
- return 3;
- } else if ( !(status & ATA_CB_STAT_DRQ) ) {
- DEBUGF("ata_cmd_packet : DRQ not set (status %02x)\n"
- , (unsigned) status);
- return 4;
- }
+// Read/write count blocks from a harddrive.
+static int
+ata_readwrite(struct disk_op_s *op, int iswrite)
+{
+ u64 lba = op->lba;
- // Send command to device
- irq_enable();
+ int usepio = ata_try_dma(op, iswrite, DISK_SECTOR_SIZE);
+
+ struct ata_pio_command cmd;
+ memset(&cmd, 0, sizeof(cmd));
- outsw(iobase1, GET_SEG(SS), (u32)cmdbuf, cmdlen);
+ 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;
+ lba &= 0xffffff;
- if (inout == ATA_DATA_NO) {
- await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
- status = inb(iobase1 + ATA_CB_STAT);
+ 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 {
- u16 loops = 0;
- u8 sc;
- while (1) {
-
- if (loops == 0) {//first time through
- status = inb(iobase2 + ATA_CB_ASTAT);
- await_ide(NOT_BSY_DRQ, iobase1, IDE_TIMEOUT);
- } else
- await_ide(NOT_BSY, iobase1, IDE_TIMEOUT);
- loops++;
-
- status = inb(iobase1 + ATA_CB_STAT);
- sc = inb(iobase1 + ATA_CB_SC);
-
- // Check if command completed
- if(((inb(iobase1 + ATA_CB_SC)&0x7)==0x3) &&
- ((status & (ATA_CB_STAT_RDY | ATA_CB_STAT_ERR)) == ATA_CB_STAT_RDY))
- break;
+ 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);
+ }
- if (status & ATA_CB_STAT_ERR) {
- DEBUGF("ata_cmd_packet : error (status %02x)\n", status);
- return 3;
- }
+ cmd.sector_count = op->count;
+ cmd.lba_low = lba;
+ cmd.lba_mid = lba >> 8;
+ cmd.lba_high = lba >> 16;
+ cmd.device = ((lba >> 24) & 0xf) | ATA_CB_DH_LBA;
+
+ 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;
+}
- // Normalize address
- bufseg += (bufoff / 16);
- bufoff %= 16;
-
- // Get the byte count
- lcount = ((u16)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
-
- // adjust to read what we want
- if (header > lcount) {
- lbefore=lcount;
- header-=lcount;
- lcount=0;
- } else {
- lbefore=header;
- header=0;
- lcount-=lbefore;
- }
+// 16bit command demuxer for ATA harddrives.
+int
+process_ata_op(struct disk_op_s *op)
+{
+ 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);
+ }
+}
- if (lcount > length) {
- lafter=lcount-length;
- lcount=length;
- length=0;
- } else {
- lafter=0;
- length-=lcount;
- }
- // Save byte count
- count = lcount;
-
- DEBUGF("Trying to read %04x bytes (%04x %04x %04x) "
- , lbefore+lcount+lafter, lbefore, lcount, lafter);
- DEBUGF("to 0x%04x:0x%04x\n", bufseg, bufoff);
-
- // If counts not dividable by 4, use 16bits mode
- lmode = mode;
- if (lbefore & 0x03) lmode=ATA_MODE_PIO16;
- if (lcount & 0x03) lmode=ATA_MODE_PIO16;
- if (lafter & 0x03) lmode=ATA_MODE_PIO16;
-
- // adds an extra byte if count are odd. before is always even
- if (lcount & 0x01) {
- lcount+=1;
- if ((lafter > 0) && (lafter & 0x01)) {
- lafter-=1;
- }
- }
+/****************************************************************
+ * ATAPI functions
+ ****************************************************************/
- if (lmode == ATA_MODE_PIO32) {
- lcount>>=2; lbefore>>=2; lafter>>=2;
- } else {
- lcount>>=1; lbefore>>=1; lafter>>=1;
- }
-
- int i;
- for (i=0; i<lbefore; i++)
- if (lmode == ATA_MODE_PIO32)
- inl(iobase1);
- else
- inw(iobase1);
+#define CDROM_CDB_SIZE 12
- if (lmode == ATA_MODE_PIO32)
- insl(iobase1, bufseg, bufoff, lcount);
- else
- insw(iobase1, bufseg, bufoff, lcount);
+// Low-level atapi command transmit function.
+int
+atapi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
+{
+ u8 ataid = GET_GLOBAL(op->drive_g->cntl_id);
+ u8 channel = ataid / 2;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ u16 iobase2 = GET_GLOBAL(ATA_channels[channel].iobase2);
+
+ struct ata_pio_command cmd;
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.lba_mid = blocksize;
+ cmd.lba_high = blocksize >> 8;
+ cmd.command = ATA_CMD_PACKET;
+
+ // Disable interrupts
+ outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2 + ATA_CB_DC);
- for (i=0; i<lafter; i++)
- if (lmode == ATA_MODE_PIO32)
- inl(iobase1);
- else
- inw(iobase1);
+ int ret = send_cmd(op->drive_g, &cmd);
+ if (ret)
+ goto fail;
+ ret = ata_wait_data(iobase1);
+ if (ret)
+ goto fail;
- // Compute new buffer address
- bufoff += count;
+ // Send command to device
+ outsw_fl(iobase1, MAKE_FLATPTR(GET_SEG(SS), cdbcmd), CDROM_CDB_SIZE / 2);
- // Save transferred bytes count
- transfer += count;
- SET_EBDA(ata.trsfbytes,transfer);
- }
+ int status = pause_await_not_bsy(iobase1, iobase2);
+ if (status < 0) {
+ ret = status;
+ goto fail;
}
- // Final check, device must be ready
- if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF
- | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
- != ATA_CB_STAT_RDY ) {
- DEBUGF("ata_cmd_packet : not ready (status %02x)\n"
- , (unsigned) status);
- return 4;
+ 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;
+ }
+
+ ret = ata_pio_transfer(op, 0, blocksize);
+fail:
// Enable interrupts
outb(ATA_CB_DC_HD15, iobase2+ATA_CB_DC);
- return 0;
+ if (ret)
+ return DISK_RET_EBADTRACK;
+ return DISK_RET_SUCCESS;
}
-u16
-cdrom_read(u16 device, u32 lba, u32 count, u16 segment, u16 offset, u16 skip)
+// 16bit command demuxer for ATAPI cdroms.
+int
+process_atapi_op(struct disk_op_s *op)
{
- u16 sectors = (count + 2048 - 1) / 2048;
+ 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);
+ }
+}
+
- u8 atacmd[12];
- memset(atacmd, 0, sizeof(atacmd));
- atacmd[0]=0x28; // READ command
- atacmd[7]=(sectors & 0xff00) >> 8; // Sectors
- atacmd[8]=(sectors & 0x00ff); // Sectors
- atacmd[2]=(lba & 0xff000000) >> 24; // LBA
- atacmd[3]=(lba & 0x00ff0000) >> 16;
- atacmd[4]=(lba & 0x0000ff00) >> 8;
- atacmd[5]=(lba & 0x000000ff);
+/****************************************************************
+ * ATA detect and init
+ ****************************************************************/
- return ata_cmd_packet(device, atacmd, sizeof(atacmd)
- , skip, count, ATA_DATA_IN
- , segment, offset);
+// Send an identify device or identify device packet command.
+static int
+send_ata_identity(struct drive_s *drive_g, u16 *buffer, int command)
+{
+ memset(buffer, 0, DISK_SECTOR_SIZE);
+
+ struct disk_op_s dop;
+ memset(&dop, 0, sizeof(dop));
+ dop.drive_g = drive_g;
+ dop.count = 1;
+ dop.lba = 1;
+ dop.buf_fl = MAKE_FLATPTR(GET_SEG(SS), buffer);
+
+ struct ata_pio_command cmd;
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.command = command;
+
+ return ata_pio_cmd_data(&dop, 0, &cmd);
}
-// ---------------------------------------------------------------------------
-// ATA/ATAPI driver : device detection
-// ---------------------------------------------------------------------------
+// Extract the ATA/ATAPI version info.
+static int
+extract_version(u16 *buffer)
+{
+ // Extract ATA/ATAPI version.
+ u16 ataversion = buffer[80];
+ u8 version;
+ for (version=15; version>0; version--)
+ if (ataversion & (1<<version))
+ break;
+ return version;
+}
-void
-ata_detect()
-{
- u8 hdcount, cdcount, device, type;
- u8 buffer[0x0200];
- memset(buffer, 0, sizeof(buffer));
-
-#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
+// Extract common information from IDENTIFY commands.
+static void
+extract_identify(struct drive_s *drive_g, u16 *buffer)
+{
+ dprintf(3, "Identify w0=%x w2=%x\n", buffer[0], buffer[2]);
+
+ // Read model name
+ char *model = drive_g->model;
+ int maxsize = ARRAY_SIZE(drive_g->model);
+ int i;
+ for (i=0; i<maxsize/2; i++) {
+ u16 v = buffer[27+i];
+ model[i*2] = v >> 8;
+ model[i*2+1] = v & 0xff;
+ }
+ model[maxsize-1] = 0x00;
- // Device detection
- hdcount=cdcount=0;
+ // Trim trailing spaces from model name.
+ for (i=maxsize-2; i>0 && model[i] == 0x20; i--)
+ model[i] = 0x00;
- for(device=0; device<CONFIG_MAX_ATA_DEVICES; device++) {
- u16 iobase1, iobase2;
- u8 channel, slave, shift;
- u8 sc, sn, cl, ch, st;
+ // Common flags.
+ SET_GLOBAL(drive_g->removable, (buffer[0] & 0x80) ? 1 : 0);
+ SET_GLOBAL(drive_g->cntl_info, extract_version(buffer));
+}
- channel = device / 2;
- slave = device % 2;
+// Print out a description of the given atapi drive.
+void
+describe_atapi(struct drive_s *drive_g)
+{
+ u8 ataid = drive_g->cntl_id;
+ u8 channel = ataid / 2;
+ u8 slave = ataid % 2;
+ u8 version = drive_g->cntl_info;
+ int iscd = drive_g->floppy_type;
+ printf("ata%d-%d: %s ATAPI-%d %s", channel, slave
+ , drive_g->model, version
+ , (iscd ? "CD-Rom/DVD-Rom" : "Device"));
+}
- iobase1 =GET_EBDA(ata.channels[channel].iobase1);
- iobase2 =GET_EBDA(ata.channels[channel].iobase2);
+// Detect if the given drive is an atapi - initialize it if so.
+static struct drive_s *
+init_drive_atapi(struct drive_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 drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
+ if (! drive_g) {
+ warn_noalloc();
+ return NULL;
+ }
+ memset(drive_g, 0, sizeof(*drive_g));
+ SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
+ extract_identify(drive_g, buffer);
+ SET_GLOBAL(drive_g->type, DTYPE_ATAPI);
+ SET_GLOBAL(drive_g->blksize, CDROM_SECTOR_SIZE);
+ SET_GLOBAL(drive_g->sectors, (u64)-1);
+ u8 iscd = ((buffer[0] >> 8) & 0x1f) == 0x05;
+ SET_GLOBAL(drive_g->floppy_type, iscd);
+
+ // fill cdidmap
+ if (iscd)
+ map_cd_drive(drive_g);
+
+ return drive_g;
+}
- // Disable interrupts
- outb(ATA_CB_DC_HD15 | ATA_CB_DC_NIEN, iobase2+ATA_CB_DC);
+// Print out a description of the given ata drive.
+void
+describe_ata(struct drive_s *drive_g)
+{
+ u8 ataid = drive_g->cntl_id;
+ u8 channel = ataid / 2;
+ u8 slave = ataid % 2;
+ u64 sectors = drive_g->sectors;
+ u8 version = drive_g->cntl_info;
+ char *model = drive_g->model;
+ printf("ata%d-%d: %s ATA-%d Hard-Disk", channel, slave, model, version);
+ u64 sizeinmb = sectors >> 11;
+ if (sizeinmb < (1 << 16))
+ printf(" (%u MiBytes)", (u32)sizeinmb);
+ else
+ printf(" (%u GiBytes)", (u32)(sizeinmb >> 10));
+}
- // 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);
- outb(0x55, iobase1+ATA_CB_SC);
- outb(0xaa, iobase1+ATA_CB_SN);
+// Detect if the given drive is a regular ata drive - initialize it if so.
+static struct drive_s *
+init_drive_ata(struct drive_s *dummy, u16 *buffer)
+{
+ // Send an IDENTIFY_DEVICE command to device
+ int ret = send_ata_identity(dummy, buffer, ATA_CMD_IDENTIFY_DEVICE);
+ if (ret)
+ return NULL;
+
+ // Success - setup as ATA.
+ struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
+ if (! drive_g) {
+ warn_noalloc();
+ return NULL;
+ }
+ memset(drive_g, 0, sizeof(*drive_g));
+ SET_GLOBAL(drive_g->cntl_id, dummy->cntl_id);
+ extract_identify(drive_g, buffer);
+ SET_GLOBAL(drive_g->type, DTYPE_ATA);
+ SET_GLOBAL(drive_g->blksize, DISK_SECTOR_SIZE);
+
+ SET_GLOBAL(drive_g->pchs.cylinders, buffer[1]);
+ SET_GLOBAL(drive_g->pchs.heads, buffer[3]);
+ SET_GLOBAL(drive_g->pchs.spt, buffer[6]);
+
+ u64 sectors;
+ if (buffer[83] & (1 << 10)) // word 83 - lba48 support
+ sectors = *(u64*)&buffer[100]; // word 100-103
+ else
+ sectors = *(u32*)&buffer[60]; // word 60 and word 61
+ SET_GLOBAL(drive_g->sectors, sectors);
+
+ // Setup disk geometry translation.
+ setup_translation(drive_g);
+
+ // Register with bcv system.
+ add_bcv_internal(drive_g);
+
+ return drive_g;
+}
- // If we found something
- sc = inb(iobase1+ATA_CB_SC);
- sn = inb(iobase1+ATA_CB_SN);
-
- if ( (sc == 0x55) && (sn == 0xaa) ) {
- SET_EBDA(ata.devices[device].type,ATA_TYPE_UNKNOWN);
-
- // reset the channel
- ata_reset(device);
-
- // 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)) {
- cl = inb(iobase1+ATA_CB_CL);
- ch = inb(iobase1+ATA_CB_CH);
- st = inb(iobase1+ATA_CB_STAT);
-
- if ((cl==0x14) && (ch==0xeb)) {
- SET_EBDA(ata.devices[device].type,ATA_TYPE_ATAPI);
- } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
- SET_EBDA(ata.devices[device].type,ATA_TYPE_ATA);
- } else if ((cl==0xff) && (ch==0xff)) {
- SET_EBDA(ata.devices[device].type,ATA_TYPE_NONE);
- }
- }
- }
+static u64 SpinupEnd;
- type=GET_EBDA(ata.devices[device].type);
-
- // Now we send a IDENTIFY command to ATA device
- if(type == ATA_TYPE_ATA) {
- u32 sectors;
- u16 cylinders, heads, spt, blksize;
- u8 translation, removable, mode;
-
- //Temporary values to do the transfer
- SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
- SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
-
- u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE
- , 1, 0, 0, 0, 0L
- , GET_SEG(SS), (u32)buffer);
- if (ret)
- BX_PANIC("ata-detect: Failed to detect ATA device\n");
-
- removable = (buffer[0] & 0x80) ? 1 : 0;
- mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
- blksize = *(u16*)&buffer[10];
-
- cylinders = *(u16*)&buffer[1*2]; // word 1
- heads = *(u16*)&buffer[3*2]; // word 3
- spt = *(u16*)&buffer[6*2]; // word 6
-
- sectors = *(u32*)&buffer[60*2]; // word 60 and word 61
-
- SET_EBDA(ata.devices[device].device,ATA_DEVICE_HD);
- SET_EBDA(ata.devices[device].removable, removable);
- SET_EBDA(ata.devices[device].mode, mode);
- SET_EBDA(ata.devices[device].blksize, blksize);
- SET_EBDA(ata.devices[device].pchs.heads, heads);
- SET_EBDA(ata.devices[device].pchs.cylinders, cylinders);
- SET_EBDA(ata.devices[device].pchs.spt, spt);
- SET_EBDA(ata.devices[device].sectors, sectors);
- BX_INFO("ata%d-%d: PCHS=%u/%d/%d translation=", channel, slave,cylinders, heads, spt);
-
- translation = inb_cmos(CMOS_BIOS_DISKTRANSFLAG + channel/2);
- for (shift=device%4; shift>0; shift--)
- translation >>= 2;
- translation &= 0x03;
-
- SET_EBDA(ata.devices[device].translation, translation);
-
- switch (translation) {
- case ATA_TRANSLATION_NONE:
- BX_INFO("none");
- break;
- case ATA_TRANSLATION_LBA:
- BX_INFO("lba");
- break;
- case ATA_TRANSLATION_LARGE:
- BX_INFO("large");
- break;
- case ATA_TRANSLATION_RECHS:
- BX_INFO("r-echs");
- break;
- }
- switch (translation) {
- case ATA_TRANSLATION_NONE:
- break;
- case ATA_TRANSLATION_LBA:
- spt = 63;
- sectors /= 63;
- heads = sectors / 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 = sectors / heads;
- break;
- case ATA_TRANSLATION_RECHS:
- // 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:
- while(cylinders > 1024) {
- cylinders >>= 1;
- heads <<= 1;
-
- // If we max out the head count
- if (heads > 127) break;
- }
- break;
- }
- // 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[device].lchs.heads, heads);
- SET_EBDA(ata.devices[device].lchs.cylinders, cylinders);
- SET_EBDA(ata.devices[device].lchs.spt, spt);
-
- // fill hdidmap
- SET_EBDA(ata.idmap[0][hdcount], device);
- hdcount++;
+// 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(1, "powerup IDE floating\n");
+ return orstatus;
}
+ if (check_time(SpinupEnd)) {
+ warn_timeout();
+ return -1;
+ }
+ yield();
+ }
+ dprintf(6, "powerup iobase=%x st=%x\n", base, status);
+ return status;
+}
- // Now we send a IDENTIFY command to ATAPI device
- if(type == ATA_TYPE_ATAPI) {
+// Detect any drives attached to a given controller.
+static void
+ata_detect(void *data)
+{
+ struct ata_channel_s *atachannel = data;
+ int startid = (atachannel - ATA_channels) * 2;
+ struct drive_s dummy;
+ memset(&dummy, 0, sizeof(dummy));
+ // Device detection
+ int ataid, last_reset_ataid=-1;
+ for (ataid=startid; ataid<startid+2; ataid++) {
+ u8 channel = ataid / 2;
+ u8 slave = ataid % 2;
- u8 type, removable, mode;
- u16 blksize;
+ u16 iobase1 = GET_GLOBAL(ATA_channels[channel].iobase1);
+ if (!iobase1)
+ break;
- //Temporary values to do the transfer
- SET_EBDA(ata.devices[device].device,ATA_DEVICE_CDROM);
- SET_EBDA(ata.devices[device].mode, ATA_MODE_PIO16);
+ // Wait for not-bsy.
+ 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;
- u16 ret = ata_cmd_data_in(device,ATA_CMD_IDENTIFY_DEVICE_PACKET
- , 1, 0, 0, 0, 0L
- , GET_SEG(SS), (u32)buffer);
- if (ret != 0)
- BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
+ // 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);
+ u8 sc = inb(iobase1+ATA_CB_SC);
+ u8 sn = inb(iobase1+ATA_CB_SN);
+ dprintf(6, "ata_detect ataid=%d sc=%x sn=%x dh=%x\n"
+ , ataid, sc, sn, dh);
+ if (sc != 0x55 || sn != 0xaa || dh != newdh)
+ continue;
- type = buffer[1] & 0x1f;
- removable = (buffer[0] & 0x80) ? 1 : 0;
- mode = buffer[96] ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
- blksize = 2048;
+ // Prepare new drive.
+ dummy.cntl_id = ataid;
- SET_EBDA(ata.devices[device].device, type);
- SET_EBDA(ata.devices[device].removable, removable);
- SET_EBDA(ata.devices[device].mode, mode);
- SET_EBDA(ata.devices[device].blksize, blksize);
+ // reset the channel
+ if (slave && ataid == last_reset_ataid + 1) {
+ // The drive was just reset - no need to reset it again.
+ } else {
+ ata_reset(&dummy);
+ last_reset_ataid = ataid;
+ }
- // fill cdidmap
- SET_EBDA(ata.idmap[1][cdcount], device);
- cdcount++;
+ // check for ATAPI
+ u16 buffer[256];
+ struct drive_s *drive_g = init_drive_atapi(&dummy, buffer);
+ if (!drive_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.
+ drive_g = init_drive_ata(&dummy, buffer);
+ if (!drive_g)
+ // No ATA drive found
+ continue;
}
- u32 sizeinmb = 0;
- u16 ataversion;
- u8 c, i, version=0, model[41];
-
- switch (type) {
- case ATA_TYPE_ATA:
- sizeinmb = GET_EBDA(ata.devices[device].sectors);
- sizeinmb >>= 11;
- case ATA_TYPE_ATAPI:
- // Read ATA/ATAPI version
- ataversion=((u16)(buffer[161])<<8) | buffer[160];
- for(version=15;version>0;version--) {
- if ((ataversion&(1<<version))!=0)
- break;
- }
+ 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.
+ ataid++;
+ }
+}
- // Read model name
- for (i=0;i<20;i++) {
- model[i*2] = buffer[(i*2)+54+1];
- model[(i*2)+1] = buffer[(i*2)+54];
- }
+// Initialize an ata controller and detect its drives.
+static void
+init_controller(struct ata_channel_s *atachannel
+ , int bdf, int irq, u32 port1, u32 port2, u32 master)
+{
+ SET_GLOBAL(atachannel->irq, irq);
+ SET_GLOBAL(atachannel->pci_bdf, bdf);
+ SET_GLOBAL(atachannel->iobase1, port1);
+ SET_GLOBAL(atachannel->iobase2, port2);
+ SET_GLOBAL(atachannel->iomaster, master);
+ dprintf(1, "ATA controller %d at %x/%x/%x (irq %d dev %x)\n"
+ , atachannel - ATA_channels, port1, port2, master, irq, bdf);
+ run_thread(ata_detect, atachannel);
+}
- // Reformat
- model[40] = 0x00;
- for (i=39;i>0;i--) {
- if (model[i]==0x20)
- model[i] = 0x00;
- else
- break;
- }
+#define IRQ_ATA1 14
+#define IRQ_ATA2 15
+
+// Locate and init ata controllers.
+static void
+ata_init(void)
+{
+ // Scan PCI bus for ATA adapters
+ int count=0, pcicount=0;
+ int bdf, max;
+ foreachpci(bdf, max) {
+ pcicount++;
+ if (pci_config_readw(bdf, PCI_CLASS_DEVICE) != PCI_CLASS_STORAGE_IDE)
+ continue;
+ if (count >= ARRAY_SIZE(ATA_channels))
break;
+
+ u8 pciirq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
+ u8 prog_if = pci_config_readb(bdf, PCI_CLASS_PROG);
+ 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);
+ }
}
- switch (type) {
- case ATA_TYPE_ATA:
- printf("ata%d %s: ",channel,slave?" slave":"master");
- i=0;
- while ((c=model[i++]))
- printf("%c",c);
- if (sizeinmb < (1UL<<16))
- printf(" ATA-%d Hard-Disk (%u MBytes)\n", version, (u16)sizeinmb);
- else
- printf(" ATA-%d Hard-Disk (%u GBytes)\n", version, (u16)(sizeinmb>>10));
- break;
- case ATA_TYPE_ATAPI:
- printf("ata%d %s: ",channel,slave?" slave":"master");
- i=0;
- while ((c=model[i++]))
- printf("%c",c);
- if (GET_EBDA(ata.devices[device].device)==ATA_DEVICE_CDROM)
- printf(" ATAPI-%d CD-Rom/DVD-Rom\n",version);
- else
- printf(" ATAPI-%d Device\n",version);
- break;
- case ATA_TYPE_UNKNOWN:
- printf("ata%d %s: Unknown device\n",channel,slave?" slave":"master");
- break;
+ u32 port1, port2, irq;
+ if (prog_if & 1) {
+ port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & ~3;
+ port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_1) & ~3;
+ irq = pciirq;
+ } else {
+ port1 = PORT_ATA1_CMD_BASE;
+ port2 = PORT_ATA1_CTRL_BASE;
+ irq = IRQ_ATA1;
+ }
+ init_controller(&ATA_channels[count], bdf, irq, port1, port2, master);
+ count++;
+
+ if (prog_if & 4) {
+ port1 = pci_config_readl(bdf, PCI_BASE_ADDRESS_2) & ~3;
+ port2 = pci_config_readl(bdf, PCI_BASE_ADDRESS_3) & ~3;
+ irq = pciirq;
+ } else {
+ port1 = PORT_ATA2_CMD_BASE;
+ port2 = PORT_ATA2_CTRL_BASE;
+ irq = IRQ_ATA2;
}
+ init_controller(&ATA_channels[count], bdf, irq, port1, port2
+ , master ? master + 8 : 0);
+ count++;
}
- // Store the devices counts
- SET_EBDA(ata.hdcount, hdcount);
- SET_EBDA(ata.cdcount, cdcount);
- SET_BDA(disk_count, hdcount);
+ if (!CONFIG_COREBOOT && !pcicount && ARRAY_SIZE(ATA_channels) >= 2) {
+ // No PCI devices found - probably a QEMU "-M isapc" machine.
+ // Try using ISA ports for ATA controllers.
+ init_controller(&ATA_channels[0], -1, IRQ_ATA1
+ , PORT_ATA1_CMD_BASE, PORT_ATA1_CTRL_BASE, 0);
+ init_controller(&ATA_channels[1], -1, IRQ_ATA2
+ , PORT_ATA2_CMD_BASE, PORT_ATA2_CTRL_BASE, 0);
+ }
+}
+
+void
+ata_setup(void)
+{
+ ASSERT32FLAT();
+ if (!CONFIG_ATA)
+ return;
+
+ dprintf(3, "init hard drives\n");
+
+ SpinupEnd = calc_future_tsc(IDE_TIMEOUT);
+ ata_init();
- printf("\n");
+ SET_BDA(disk_control_byte, 0xc0);
- // FIXME : should use bios=cmos|auto|disable bits
- // FIXME : should know about translation bits
- // FIXME : move hard_drive_post here
+ enable_hwirq(14, entry_76);
}