Try to check for IDE drive 0 responding to drive 1 commands.
[seabios.git] / src / ata.c
index 4b329d2bcfdae81df0502f5ff0d903bfc0e5ddb5..3b609c5b73ad1644b1fdcaf86fdb6d71d14cc86f 100644 (file)
--- a/src/ata.c
+++ b/src/ata.c
@@ -110,7 +110,7 @@ ata_reset(int driveid)
         u64 end = calc_future_tsc(IDE_TIMEOUT);
         for (;;) {
             outb(ATA_CB_DH_DEV1, iobase1 + ATA_CB_DH);
-            status = await_not_bsy(iobase1);
+            status = ndelay_await_not_bsy(iobase1);
             if (status < 0)
                 goto done;
             if (inb(iobase1 + ATA_CB_DH) == ATA_CB_DH_DEV1)
@@ -177,7 +177,7 @@ send_cmd(int driveid, struct ata_pio_command *cmd)
     outb(newdh, iobase1 + ATA_CB_DH);
     if ((olddh ^ newdh) & (1<<4)) {
         // Was a device change - wait for device to become not busy.
-        status = await_not_bsy(iobase1);
+        status = ndelay_await_not_bsy(iobase1);
         if (status < 0)
             return status;
     }
@@ -647,11 +647,10 @@ extract_identify(int driveid, u16 *buffer)
 }
 
 static int
-init_drive_atapi(int driveid)
+init_drive_atapi(int driveid, u16 *buffer)
 {
     // Send an IDENTIFY_DEVICE_PACKET command to device
-    u16 buffer[256];
-    memset(buffer, 0, sizeof(buffer));
+    memset(buffer, 0, IDE_SECTOR_SIZE);
     struct disk_op_s dop;
     dop.driveid = driveid;
     dop.command = ATA_CMD_IDENTIFY_DEVICE_PACKET;
@@ -685,11 +684,10 @@ init_drive_atapi(int driveid)
 }
 
 static int
-init_drive_ata(int driveid)
+init_drive_ata(int driveid, u16 *buffer)
 {
     // Send an IDENTIFY_DEVICE command to device
-    u16 buffer[256];
-    memset(buffer, 0, sizeof(buffer));
+    memset(buffer, 0, IDE_SECTOR_SIZE);
     struct disk_op_s dop;
     dop.driveid = driveid;
     dop.command = ATA_CMD_IDENTIFY_DEVICE;
@@ -781,6 +779,7 @@ ata_detect()
             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, end);
         if (status < 0)
             continue;
@@ -806,23 +805,35 @@ ata_detect()
         }
 
         // check for ATAPI
-        int ret = init_drive_atapi(driveid);
-        if (!ret)
+        u16 buffer[256];
+        int ret = init_drive_atapi(driveid, buffer);
+        if (!ret) {
             // Found an ATAPI drive.
-            continue;
-
-        u8 st = inb(iobase1+ATA_CB_STAT);
-        if (!st)
-            // Status not set - can't be a valid drive.
-            continue;
-
-        // Wait for RDY.
-        ret = await_rdy(iobase1);
-        if (ret < 0)
-            continue;
+        } else {
+            u8 st = inb(iobase1+ATA_CB_STAT);
+            if (!st)
+                // Status not set - can't be a valid drive.
+                continue;
+
+            // Wait for RDY.
+            ret = await_rdy(iobase1);
+            if (ret < 0)
+                continue;
+
+            // check for ATA.
+            ret = init_drive_ata(driveid, buffer);
+            if (ret)
+                // No ATA drive found
+                continue;
+        }
 
-        // check for ATA.
-        init_drive_ata(driveid);
+        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.
+            driveid++;
     }
 
     printf("\n");