|
@@ -11,8 +11,17 @@
|
|
|
#include "timer.h"
|
|
#include "timer.h"
|
|
|
|
|
|
|
|
static unsigned short ata_base[2] = { 0x1F0, 0x170 };
|
|
static unsigned short ata_base[2] = { 0x1F0, 0x170 };
|
|
|
|
|
+/* Kept for the bring-up log only; transfers poll the command-block status
|
|
|
|
|
+ * because some controllers' alt-status BAR does not answer correctly. */
|
|
|
static unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
|
|
static unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
|
|
|
|
|
|
|
|
|
|
+/* Set when a multi-sector PIO command left DRQ pending on this channel.
|
|
|
|
|
+ * From then on use one ATA command per sector; slower, but it works on
|
|
|
|
|
+ * bridges that mishandle multi-sector PIO. */
|
|
|
|
|
+static int ata_single[2];
|
|
|
|
|
+static int ata_lba48[2];
|
|
|
|
|
+static int ata_tf_logged;
|
|
|
|
|
+
|
|
|
/* Register offsets from the I/O base. */
|
|
/* Register offsets from the I/O base. */
|
|
|
#define REG_DATA 0
|
|
#define REG_DATA 0
|
|
|
#define REG_ERROR 1
|
|
#define REG_ERROR 1
|
|
@@ -36,6 +45,16 @@ static unsigned short ata_ctrl[2] = { 0x3F6, 0x376 };
|
|
|
#define CMD_READ_EXT 0x24 /* LBA48 */
|
|
#define CMD_READ_EXT 0x24 /* LBA48 */
|
|
|
#define CMD_WRITE_EXT 0x34 /* LBA48 */
|
|
#define CMD_WRITE_EXT 0x34 /* LBA48 */
|
|
|
|
|
|
|
|
|
|
+/* A spun-down disk can take several seconds to accept its first command;
|
|
|
|
|
+ * keep a generous poll budget. */
|
|
|
|
|
+#define ATA_TIMEOUT_MS 15000
|
|
|
|
|
+
|
|
|
|
|
+/* Once the data is transferred, the drive should finish the sector quickly.
|
|
|
|
|
+ * A longer wait here would only delay the single-sector fallback. */
|
|
|
|
|
+#define ATA_COMPLETE_TIMEOUT_MS 2000
|
|
|
|
|
+
|
|
|
|
|
+static void ata_reset_channel(unsigned short base, unsigned short ctrl);
|
|
|
|
|
+
|
|
|
/* Point the primary/secondary channels at the PCI IDE interface's native
|
|
/* Point the primary/secondary channels at the PCI IDE interface's native
|
|
|
* per-channel I/O BARs when it uses native mode, keeping the legacy ports
|
|
* per-channel I/O BARs when it uses native mode, keeping the legacy ports
|
|
|
* otherwise. Runs once; without a PCI IDE function the defaults stay. */
|
|
* otherwise. Runs once; without a PCI IDE function the defaults stay. */
|
|
@@ -82,9 +101,12 @@ void ata_init_ports(void)
|
|
|
/* prog-if bit 0/2: primary/secondary native mode. */
|
|
/* prog-if bit 0/2: primary/secondary native mode. */
|
|
|
if ((prog & 0x01u) && (bar0 & 0x1u)) {
|
|
if ((prog & 0x01u) && (bar0 & 0x1u)) {
|
|
|
b0 = (unsigned short)(bar0 & 0xFFFCu);
|
|
b0 = (unsigned short)(bar0 & 0xFFFCu);
|
|
|
|
|
+ /* A native-mode BAR1 of 0 is malformed;
|
|
|
|
|
+ * the legacy control port is a safer
|
|
|
|
|
+ * fallback than b0+8. */
|
|
|
c0 = (bar1 & 0x1u)
|
|
c0 = (bar1 & 0x1u)
|
|
|
? (unsigned short)(bar1 & 0xFFFCu)
|
|
? (unsigned short)(bar1 & 0xFFFCu)
|
|
|
- : (unsigned short)(b0 + 8);
|
|
|
|
|
|
|
+ : (unsigned short)0x3F6;
|
|
|
} else {
|
|
} else {
|
|
|
b0 = 0x1F0; c0 = 0x3F6;
|
|
b0 = 0x1F0; c0 = 0x3F6;
|
|
|
}
|
|
}
|
|
@@ -92,13 +114,25 @@ void ata_init_ports(void)
|
|
|
b1 = (unsigned short)(bar2 & 0xFFFCu);
|
|
b1 = (unsigned short)(bar2 & 0xFFFCu);
|
|
|
c1 = (bar3 & 0x1u)
|
|
c1 = (bar3 & 0x1u)
|
|
|
? (unsigned short)(bar3 & 0xFFFCu)
|
|
? (unsigned short)(bar3 & 0xFFFCu)
|
|
|
- : (unsigned short)(b1 + 8);
|
|
|
|
|
|
|
+ : (unsigned short)0x376;
|
|
|
} else {
|
|
} else {
|
|
|
b1 = 0x170; c1 = 0x376;
|
|
b1 = 0x170; c1 = 0x376;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ata_base[0] = b0; ata_ctrl[0] = c0;
|
|
ata_base[0] = b0; ata_ctrl[0] = c0;
|
|
|
ata_base[1] = b1; ata_ctrl[1] = c1;
|
|
ata_base[1] = b1; ata_ctrl[1] = c1;
|
|
|
|
|
+ ata_reset_channel(ata_base[0], ata_ctrl[0]);
|
|
|
|
|
+ ata_reset_channel(ata_base[1], ata_ctrl[1]);
|
|
|
|
|
+
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "ata: ctl ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, port_inb(c0));
|
|
|
|
|
+ serial_putc(SERIAL_COM1, '/');
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, port_inb(c0 + 2));
|
|
|
|
|
+ serial_putc(SERIAL_COM1, ' ');
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, port_inb(c1));
|
|
|
|
|
+ serial_putc(SERIAL_COM1, '/');
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, port_inb(c1 + 2));
|
|
|
|
|
+ serial_putc(SERIAL_COM1, '\n');
|
|
|
|
|
|
|
|
serial_puts(SERIAL_COM1, "ata: pci ide ");
|
|
serial_puts(SERIAL_COM1, "ata: pci ide ");
|
|
|
serial_putdec(SERIAL_COM1, bus);
|
|
serial_putdec(SERIAL_COM1, bus);
|
|
@@ -124,25 +158,50 @@ void ata_init_ports(void)
|
|
|
serial_puts(SERIAL_COM1, "ata: no PCI IDE function, legacy ports\n");
|
|
serial_puts(SERIAL_COM1, "ata: no PCI IDE function, legacy ports\n");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/* Delay 400 ns by reading the alt-status register a few times. */
|
|
|
|
|
-static void ata_400ns(unsigned short ctrl)
|
|
|
|
|
|
|
+/* Delay 400 ns by reading the command-block status register a few times.
|
|
|
|
|
+ * The alt-status port is not used for polling: some IDE functions expose a
|
|
|
|
|
+ * BAR that does not answer correctly, and a bogus BSY bit there made every
|
|
|
|
|
+ * transfer time out even though the command block reported ready. */
|
|
|
|
|
+static void ata_400ns(unsigned short base)
|
|
|
{
|
|
{
|
|
|
- (void)port_inb(ctrl);
|
|
|
|
|
- (void)port_inb(ctrl);
|
|
|
|
|
- (void)port_inb(ctrl);
|
|
|
|
|
- (void)port_inb(ctrl);
|
|
|
|
|
|
|
+ (void)port_inb(base + REG_CMD);
|
|
|
|
|
+ (void)port_inb(base + REG_CMD);
|
|
|
|
|
+ (void)port_inb(base + REG_CMD);
|
|
|
|
|
+ (void)port_inb(base + REG_CMD);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static int ata_wait_not_busy(unsigned short ctrl, unsigned int timeout_ms)
|
|
|
|
|
|
|
+static int ata_wait_not_busy(unsigned short base, unsigned int timeout_ms)
|
|
|
{
|
|
{
|
|
|
unsigned int t0 = get_ticks_ms();
|
|
unsigned int t0 = get_ticks_ms();
|
|
|
while (get_ticks_ms() - t0 < timeout_ms) {
|
|
while (get_ticks_ms() - t0 < timeout_ms) {
|
|
|
- if (!(port_inb(ctrl) & SR_BSY))
|
|
|
|
|
|
|
+ if (!(port_inb(base + REG_CMD) & SR_BSY))
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/* Assert SRST for ~10 ms. This aborts a command a crashed/reloaded kernel
|
|
|
|
|
+ * left half-transferred (the drive would otherwise stay in DRQ forever and
|
|
|
|
|
+ * ignore the next IDENTIFY). Best effort: a malformed control BAR simply
|
|
|
|
|
+ * makes the reset a no-op. */
|
|
|
|
|
+static void ata_reset_channel(unsigned short base, unsigned short ctrl)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int t0;
|
|
|
|
|
+
|
|
|
|
|
+ port_outb(ctrl, 0x04); /* SRST */
|
|
|
|
|
+ t0 = get_ticks_ms();
|
|
|
|
|
+ while (get_ticks_ms() - t0 < 10)
|
|
|
|
|
+ ;
|
|
|
|
|
+ /* nIEN=1: the driver polls, so keep the drive's IRQ line quiet
|
|
|
|
|
+ * instead of leaving a masked level-triggered interrupt asserted. */
|
|
|
|
|
+ port_outb(ctrl, 0x02);
|
|
|
|
|
+ t0 = get_ticks_ms();
|
|
|
|
|
+ while ((port_inb(base + REG_CMD) & SR_BSY) &&
|
|
|
|
|
+ get_ticks_ms() - t0 < 1000)
|
|
|
|
|
+ ;
|
|
|
|
|
+ ata_400ns(base);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
|
|
static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
|
|
|
{
|
|
{
|
|
|
unsigned int t0 = get_ticks_ms();
|
|
unsigned int t0 = get_ticks_ms();
|
|
@@ -156,14 +215,37 @@ static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/* A command is done only when both BSY and DRQ are clear. Waiting for BSY
|
|
|
|
|
+ * alone can return while the last sector is still pending; the next command
|
|
|
|
|
+ * is then ignored and the drive stays in DRQ, which looks like a zeroed /
|
|
|
|
|
+ * unmountable filesystem. */
|
|
|
|
|
+static int ata_wait_idle(unsigned short base, unsigned int timeout_ms)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int t0 = get_ticks_ms();
|
|
|
|
|
+
|
|
|
|
|
+ while (get_ticks_ms() - t0 < timeout_ms) {
|
|
|
|
|
+ unsigned char st = port_inb(base + REG_CMD);
|
|
|
|
|
+
|
|
|
|
|
+ if (st & SR_ERR)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ if (!(st & (SR_BSY | SR_DRQ)))
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ return -1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
static int ata_identify(int bus, int unit, unsigned short *id)
|
|
static int ata_identify(int bus, int unit, unsigned short *id)
|
|
|
{
|
|
{
|
|
|
unsigned short base = ata_base[bus];
|
|
unsigned short base = ata_base[bus];
|
|
|
- unsigned short ctrl = ata_ctrl[bus];
|
|
|
|
|
unsigned char st;
|
|
unsigned char st;
|
|
|
unsigned int t0;
|
|
unsigned int t0;
|
|
|
unsigned int i;
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
+ /* A previous failed transfer can leave the drive waiting for PIO
|
|
|
|
|
+ * data, in which case it ignores IDENTIFY and returns zeros. */
|
|
|
|
|
+ if (ata_wait_idle(base, 1000) != 0)
|
|
|
|
|
+ ata_reset_channel(base, ata_ctrl[bus]);
|
|
|
|
|
+
|
|
|
port_outb(base + REG_DRIVE, (unsigned char)(0xA0 | (unit << 4)));
|
|
port_outb(base + REG_DRIVE, (unsigned char)(0xA0 | (unit << 4)));
|
|
|
port_outb(base + REG_COUNT, 0);
|
|
port_outb(base + REG_COUNT, 0);
|
|
|
port_outb(base + REG_LBA_LO, 0);
|
|
port_outb(base + REG_LBA_LO, 0);
|
|
@@ -171,7 +253,7 @@ static int ata_identify(int bus, int unit, unsigned short *id)
|
|
|
port_outb(base + REG_LBA_HI, 0);
|
|
port_outb(base + REG_LBA_HI, 0);
|
|
|
port_outb(base + REG_CMD, CMD_IDENTIFY);
|
|
port_outb(base + REG_CMD, CMD_IDENTIFY);
|
|
|
|
|
|
|
|
- ata_400ns(ctrl);
|
|
|
|
|
|
|
+ ata_400ns(base);
|
|
|
|
|
|
|
|
t0 = get_ticks_ms();
|
|
t0 = get_ticks_ms();
|
|
|
for (;;) {
|
|
for (;;) {
|
|
@@ -180,7 +262,7 @@ static int ata_identify(int bus, int unit, unsigned short *id)
|
|
|
return -1;
|
|
return -1;
|
|
|
if (!(st & SR_BSY))
|
|
if (!(st & SR_BSY))
|
|
|
break;
|
|
break;
|
|
|
- if (get_ticks_ms() - t0 > 4000)
|
|
|
|
|
|
|
+ if (get_ticks_ms() - t0 > ATA_TIMEOUT_MS)
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
if (!(st & SR_DRQ))
|
|
if (!(st & SR_DRQ))
|
|
@@ -228,87 +310,178 @@ int ata_identify_parse(const unsigned short *id, unsigned int *total_sectors, ch
|
|
|
int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
|
|
int ata_probe(int bus, int unit, unsigned int *total_sectors, char model[41])
|
|
|
{
|
|
{
|
|
|
static unsigned short id[256];
|
|
static unsigned short id[256];
|
|
|
|
|
+ int r;
|
|
|
|
|
|
|
|
if (ata_identify(bus, unit, id) != 0)
|
|
if (ata_identify(bus, unit, id) != 0)
|
|
|
return -1;
|
|
return -1;
|
|
|
|
|
|
|
|
- return ata_identify_parse(id, total_sectors, model);
|
|
|
|
|
|
|
+ r = ata_identify_parse(id, total_sectors, model);
|
|
|
|
|
+ if (r == 0)
|
|
|
|
|
+ ata_lba48[bus] = (id[83] & 0x0400) != 0;
|
|
|
|
|
+ return r;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static inline void ata_insw(unsigned short port, void *dst, unsigned int count)
|
|
|
|
|
|
|
+static void ata_log_failure(int write, unsigned int lba, unsigned int count,
|
|
|
|
|
+ const char *what, unsigned short base)
|
|
|
{
|
|
{
|
|
|
- __asm__ volatile ("rep insw"
|
|
|
|
|
- : "+D"(dst), "+c"(count)
|
|
|
|
|
- : "d"(port)
|
|
|
|
|
- : "memory");
|
|
|
|
|
|
|
+ unsigned char st = port_inb(base + REG_CMD);
|
|
|
|
|
+ unsigned char err = port_inb(base + REG_ERROR);
|
|
|
|
|
+
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "ata: ");
|
|
|
|
|
+ serial_puts(SERIAL_COM1, write ? "write" : "read");
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " failed (");
|
|
|
|
|
+ serial_puts(SERIAL_COM1, what);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, ") lba ");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, lba);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " count ");
|
|
|
|
|
+ serial_putdec(SERIAL_COM1, count);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " status ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, st);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " error ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, err);
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " cnt ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, port_inb(base + REG_COUNT));
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " lba ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1,
|
|
|
|
|
+ port_inb(base + REG_LBA_LO) |
|
|
|
|
|
+ (port_inb(base + REG_LBA_MID) << 8) |
|
|
|
|
|
+ (port_inb(base + REG_LBA_HI) << 16));
|
|
|
|
|
+ serial_putc(SERIAL_COM1, '\n');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static inline void ata_outsw(unsigned short port, const void *src, unsigned int count)
|
|
|
|
|
|
|
+/* Use explicit 16-bit port reads/writes instead of `rep ins/outs`. The
|
|
|
|
|
+ * string form writes through ES:EDI; the C loop uses the normal data
|
|
|
|
|
+ * segment and is the same path IDENTIFY already uses successfully on the
|
|
|
|
|
+ * real MCP79 board. A sector is only 256 words, so the speed difference
|
|
|
|
|
+ * is small compared with the drive's PIO timing. */
|
|
|
|
|
+static void ata_insw(unsigned short port, void *dst, unsigned int count)
|
|
|
{
|
|
{
|
|
|
- __asm__ volatile ("rep outsw"
|
|
|
|
|
- : "+S"(src), "+c"(count)
|
|
|
|
|
- : "d"(port)
|
|
|
|
|
- : "memory");
|
|
|
|
|
|
|
+ unsigned short *p = (unsigned short *)dst;
|
|
|
|
|
+
|
|
|
|
|
+ while (count--)
|
|
|
|
|
+ *p++ = port_inw(port);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void ata_outsw(unsigned short port, const void *src, unsigned int count)
|
|
|
|
|
+{
|
|
|
|
|
+ const unsigned short *p = (const unsigned short *)src;
|
|
|
|
|
+
|
|
|
|
|
+ while (count--)
|
|
|
|
|
+ port_outw(port, *p++);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static int ata_xfer(int bus, int unit, unsigned int lba, void *buf,
|
|
static int ata_xfer(int bus, int unit, unsigned int lba, void *buf,
|
|
|
unsigned int count, int write)
|
|
unsigned int count, int write)
|
|
|
{
|
|
{
|
|
|
unsigned short base = ata_base[bus];
|
|
unsigned short base = ata_base[bus];
|
|
|
- unsigned short ctrl = ata_ctrl[bus];
|
|
|
|
|
unsigned char *p = buf;
|
|
unsigned char *p = buf;
|
|
|
- unsigned int done = 0;
|
|
|
|
|
|
|
+ unsigned int done = 0, i;
|
|
|
|
|
+
|
|
|
|
|
+ if (ata_wait_idle(base, 1000) != 0)
|
|
|
|
|
+ ata_reset_channel(base, ata_ctrl[bus]);
|
|
|
|
|
|
|
|
while (done < count) {
|
|
while (done < count) {
|
|
|
unsigned int chunk = count - done;
|
|
unsigned int chunk = count - done;
|
|
|
int lba48;
|
|
int lba48;
|
|
|
unsigned int cur = lba + done;
|
|
unsigned int cur = lba + done;
|
|
|
|
|
|
|
|
- if (chunk > 255)
|
|
|
|
|
|
|
+ if (ata_single[bus])
|
|
|
|
|
+ chunk = 1;
|
|
|
|
|
+ else if (chunk > 255)
|
|
|
chunk = 255;
|
|
chunk = 255;
|
|
|
- lba48 = ((cur + chunk - 1) >= 0x10000000u);
|
|
|
|
|
-
|
|
|
|
|
- if (lba48) {
|
|
|
|
|
- port_outb(base + REG_DRIVE, (unsigned char)(0x40 | (unit << 4)));
|
|
|
|
|
- port_outb(base + REG_COUNT, (unsigned char)(chunk >> 8));
|
|
|
|
|
- port_outb(base + REG_LBA_LO, (unsigned char)(cur >> 24));
|
|
|
|
|
- port_outb(base + REG_LBA_MID, 0);
|
|
|
|
|
- port_outb(base + REG_LBA_HI, 0);
|
|
|
|
|
- port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
|
|
|
|
|
- port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
|
|
|
|
|
- port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
|
|
|
|
|
- port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
|
|
|
|
|
- port_outb(base + REG_CMD, (unsigned char)(write ? CMD_WRITE_EXT : CMD_READ_EXT));
|
|
|
|
|
- } else {
|
|
|
|
|
- port_outb(base + REG_DRIVE,
|
|
|
|
|
- (unsigned char)(0xE0 | (unit << 4) | ((cur >> 24) & 0x0F)));
|
|
|
|
|
- port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
|
|
|
|
|
- port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
|
|
|
|
|
- port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
|
|
|
|
|
- port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
|
|
|
|
|
- port_outb(base + REG_CMD, (unsigned char)(write ? CMD_WRITE : CMD_READ));
|
|
|
|
|
|
|
+ lba48 = ata_lba48[bus] ||
|
|
|
|
|
+ ((cur + chunk - 1) >= 0x10000000u);
|
|
|
|
|
+
|
|
|
|
|
+ {
|
|
|
|
|
+ unsigned char cmd;
|
|
|
|
|
+
|
|
|
|
|
+ if (lba48) {
|
|
|
|
|
+ port_outb(base + REG_DRIVE, (unsigned char)(0x40 | (unit << 4)));
|
|
|
|
|
+ port_outb(base + REG_COUNT, (unsigned char)(chunk >> 8));
|
|
|
|
|
+ port_outb(base + REG_LBA_LO, (unsigned char)(cur >> 24));
|
|
|
|
|
+ port_outb(base + REG_LBA_MID, 0);
|
|
|
|
|
+ port_outb(base + REG_LBA_HI, 0);
|
|
|
|
|
+ port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
|
|
|
|
|
+ port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
|
|
|
|
|
+ port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
|
|
|
|
|
+ port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
|
|
|
|
|
+ cmd = (unsigned char)(write ? CMD_WRITE_EXT : CMD_READ_EXT);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ port_outb(base + REG_DRIVE,
|
|
|
|
|
+ (unsigned char)(0xE0 | (unit << 4) | ((cur >> 24) & 0x0F)));
|
|
|
|
|
+ port_outb(base + REG_COUNT, (unsigned char)(chunk & 0xFF));
|
|
|
|
|
+ port_outb(base + REG_LBA_LO, (unsigned char)(cur & 0xFF));
|
|
|
|
|
+ port_outb(base + REG_LBA_MID, (unsigned char)((cur >> 8) & 0xFF));
|
|
|
|
|
+ port_outb(base + REG_LBA_HI, (unsigned char)((cur >> 16) & 0xFF));
|
|
|
|
|
+ cmd = (unsigned char)(write ? CMD_WRITE : CMD_READ);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!ata_tf_logged) {
|
|
|
|
|
+ ata_tf_logged = 1;
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "ata: tf cnt ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, port_inb(base + REG_COUNT));
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " lba ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1,
|
|
|
|
|
+ port_inb(base + REG_LBA_LO) |
|
|
|
|
|
+ (port_inb(base + REG_LBA_MID) << 8) |
|
|
|
|
|
+ (port_inb(base + REG_LBA_HI) << 16));
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " drv ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, port_inb(base + REG_DRIVE));
|
|
|
|
|
+ serial_puts(SERIAL_COM1, " cmd ");
|
|
|
|
|
+ serial_puthex(SERIAL_COM1, cmd);
|
|
|
|
|
+ serial_putc(SERIAL_COM1, '\n');
|
|
|
|
|
+ }
|
|
|
|
|
+ port_outb(base + REG_CMD, cmd);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- ata_400ns(ctrl);
|
|
|
|
|
- if (ata_wait_not_busy(ctrl, 4000) != 0)
|
|
|
|
|
- return -1;
|
|
|
|
|
- if (ata_wait_drq(base, 4000) != 0)
|
|
|
|
|
- return -1;
|
|
|
|
|
|
|
+ ata_400ns(base);
|
|
|
|
|
+
|
|
|
|
|
+ /* PIO transfers must wait for DRQ before *each* sector: the
|
|
|
|
|
+ * drive drops DRQ between sectors while it commits the previous
|
|
|
|
|
+ * one, and writing into the data register while DRQ is low is
|
|
|
|
|
+ * silently lost. A single rep outsw over a large chunk looked
|
|
|
|
|
+ * fine for one sector but left the drive waiting for more data
|
|
|
|
|
+ * (DRQ set, count/LBA registers advanced), so the next command
|
|
|
|
|
+ * read stale data and a large format eventually failed. */
|
|
|
|
|
+ for (i = 0; i < chunk; i++) {
|
|
|
|
|
+ if (ata_wait_not_busy(base, ATA_TIMEOUT_MS) != 0) {
|
|
|
|
|
+ ata_log_failure(write, cur + i, 1,
|
|
|
|
|
+ "busy timeout", base);
|
|
|
|
|
+ goto xfer_fail;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (ata_wait_drq(base, ATA_TIMEOUT_MS) != 0) {
|
|
|
|
|
+ ata_log_failure(write, cur + i, 1,
|
|
|
|
|
+ "drq timeout", base);
|
|
|
|
|
+ goto xfer_fail;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (write)
|
|
|
|
|
+ ata_outsw(base + REG_DATA, p + i * 512, 256);
|
|
|
|
|
+ else
|
|
|
|
|
+ ata_insw(base + REG_DATA, p + i * 512, 256);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (write) {
|
|
|
|
|
- ata_outsw(base + REG_DATA, p, chunk * 256);
|
|
|
|
|
- /* Wait for the drive to finish writing to media (BSY clears
|
|
|
|
|
- * after the data transfer) before issuing the next command. */
|
|
|
|
|
- if (ata_wait_not_busy(ctrl, 4000) != 0)
|
|
|
|
|
- return -1;
|
|
|
|
|
- if (port_inb(base + REG_CMD) & SR_ERR)
|
|
|
|
|
- return -1;
|
|
|
|
|
- } else {
|
|
|
|
|
- ata_insw(base + REG_DATA, p, chunk * 256);
|
|
|
|
|
|
|
+ /* Let the last sector finish before the next command. */
|
|
|
|
|
+ if (ata_wait_idle(base, ATA_COMPLETE_TIMEOUT_MS) != 0) {
|
|
|
|
|
+ ata_log_failure(write, cur, chunk, "completion timeout", base);
|
|
|
|
|
+ goto xfer_fail;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (port_inb(base + REG_CMD) & SR_ERR) {
|
|
|
|
|
+ ata_log_failure(write, cur, chunk, "transfer error", base);
|
|
|
|
|
+ goto xfer_fail;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
p += chunk * 512;
|
|
p += chunk * 512;
|
|
|
done += chunk;
|
|
done += chunk;
|
|
|
|
|
+ continue;
|
|
|
|
|
+
|
|
|
|
|
+xfer_fail:
|
|
|
|
|
+ ata_reset_channel(base, ata_ctrl[bus]);
|
|
|
|
|
+ if (ata_single[bus])
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ /* A multi-sector command upset this bridge; retry from the same
|
|
|
|
|
+ * sector with one ATA command per sector. */
|
|
|
|
|
+ ata_single[bus] = 1;
|
|
|
|
|
+ serial_puts(SERIAL_COM1, "ata: switching to single-sector PIO\n");
|
|
|
}
|
|
}
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
@@ -322,3 +495,28 @@ int ata_write_sectors(int bus, int unit, unsigned int lba, const void *buf, unsi
|
|
|
{
|
|
{
|
|
|
return ata_xfer(bus, unit, lba, (void *)buf, count, 1);
|
|
return ata_xfer(bus, unit, lba, (void *)buf, count, 1);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+int ata_flush(int bus, int unit)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned short base;
|
|
|
|
|
+
|
|
|
|
|
+ if (bus < 0 || bus > 1)
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ base = ata_base[bus];
|
|
|
|
|
+ if (ata_wait_idle(base, 1000) != 0)
|
|
|
|
|
+ ata_reset_channel(base, ata_ctrl[bus]);
|
|
|
|
|
+ port_outb(base + REG_DRIVE, (unsigned char)(0xA0 | (unit << 4)));
|
|
|
|
|
+ port_outb(base + REG_CMD, 0xE7); /* FLUSH CACHE */
|
|
|
|
|
+ ata_400ns(base);
|
|
|
|
|
+ if (ata_wait_idle(base, ATA_TIMEOUT_MS) != 0) {
|
|
|
|
|
+ ata_log_failure(1, 0, 0, "cache flush timeout", base);
|
|
|
|
|
+ ata_reset_channel(base, ata_ctrl[bus]);
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (port_inb(base + REG_CMD) & SR_ERR) {
|
|
|
|
|
+ ata_log_failure(1, 0, 0, "cache flush error", base);
|
|
|
|
|
+ ata_reset_channel(base, ata_ctrl[bus]);
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|