summaryrefslogtreecommitdiff
path: root/src/kernel/arch/i386/ata.c
diff options
context:
space:
mode:
authordzwdz2021-10-06 22:15:02 +0200
committerdzwdz2021-10-06 22:15:02 +0200
commit7e326b5039bff4d422f66bb8e51267f785193985 (patch)
tree2f991615c4a06ba6c3f0c90d5da896f2b058aafe /src/kernel/arch/i386/ata.c
parent0cd25153a0556b988959c10c5ecab04cbacc9506 (diff)
kernel/i386: implement part of ATA IDENTIFY
Diffstat (limited to 'src/kernel/arch/i386/ata.c')
-rw-r--r--src/kernel/arch/i386/ata.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/kernel/arch/i386/ata.c b/src/kernel/arch/i386/ata.c
new file mode 100644
index 0000000..996fa3a
--- /dev/null
+++ b/src/kernel/arch/i386/ata.c
@@ -0,0 +1,65 @@
+#include <kernel/arch/i386/ata.h>
+#include <kernel/arch/i386/port_io.h>
+#include <stdbool.h>
+
+#include <kernel/arch/io.h>
+
+enum {
+ LBAlo = 3,
+ LBAmid = 4,
+ LBAhi = 5,
+ DRV = 6,
+ CMD = 7,
+ STATUS = 7,
+}; // offsets
+
+// get I/O port base for drive
+static uint16_t ata_iobase(int drive) {
+ bool secondary = drive&2;
+ return secondary ? 0x170 : 0x1F0;
+}
+
+static void ata_driveselect(int drive, int block) {
+ uint8_t v = 0xE0;
+ if (drive&1) // slave?
+ v |= 0x10; // set drive number bit
+ // TODO account for block
+ port_outb(ata_iobase(drive) + DRV, v);
+}
+
+static bool ata_identify(int drive) {
+ uint16_t iobase = ata_iobase(drive);
+ uint8_t v;
+
+ ata_driveselect(drive, 0);
+ for (int i = 2; i < 6; i++)
+ port_outb(iobase + i, 0);
+ port_outb(iobase + CMD, 0xEC); // IDENTIFY
+
+ v = port_inb(iobase + STATUS);
+ if (v == 0) return false; // nonexistent drive
+ while (port_inb(iobase + STATUS) & 0x80);
+
+ /* check for uncomformant devices, quit early */
+ if (port_inb(iobase + LBAmid) || port_inb(iobase + LBAhi)) {
+ // TODO atapi
+ return true;
+ }
+ /* pool until bit 3 (DRQ) or 0 (ERR) is set */
+ while (!((v = port_inb(iobase + STATUS) & 0x9)));
+ if (v & 1) return false; /* ERR was set, bail */
+
+ // now I can read 512 bytes of data, TODO
+ return true;
+}
+
+void ata_init(void) {
+ tty_const("\n");
+ for (int i = 0; i < 4; i++) {
+ tty_const("probing drive ");
+ _tty_var(i);
+ if (ata_identify(i))
+ tty_const(" - exists");
+ tty_const("\n");
+ }
+}