summaryrefslogtreecommitdiff
path: root/src/kernel/arch/i386/ata.c
blob: 916b9188f25ef2bc60af2a0d95854da31b9691f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <kernel/arch/i386/ata.h>
#include <kernel/arch/i386/port_io.h>
#include <stdbool.h>

#include <kernel/arch/io.h>

static struct {
	enum {
		DEV_UNKNOWN,
		DEV_PATA,
		DEV_PATAPI,
	} type;
	uint32_t sectors;
} ata_drives[4];

enum {
	LBAlo  = 3,
	LBAmid = 4,
	LBAhi  = 5,
	DRV    = 6,
	CMD    = 7,
	STATUS = 7,

	/* note: the OSDev wiki uses a different base port for the control port
	 *       however i can just use this offset and stuff will just work tm */
	CTRL   = 0x206,
}; // 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_400ns(void) {
	uint16_t base = ata_iobase(0); // doesn't matter
	for (int i = 0; i < 4; i++)
		port_in8(base + STATUS);
}

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_out8(ata_iobase(drive) + DRV, v);
}

static void ata_softreset(int drive) {
	uint16_t iobase = ata_iobase(drive);
	port_out8(iobase + CTRL, 4);
	port_out8(iobase + CTRL, 0);
	ata_400ns();

	uint16_t timeout = 10000;
	while (--timeout) { // TODO separate polling function
		uint8_t v = port_in8(iobase + STATUS);
		if (v & 0x80) continue; // still BSY, continue
		if (v & 0x40) break; // RDY, break
		// TODO check for ERR
	}
}

static void ata_detecttype(int drive) {
	ata_softreset(drive);
	ata_driveselect(drive, 0);
	ata_400ns();
	switch (port_in8(ata_iobase(drive) + LBAmid)) {
		case 0:
			ata_drives[drive].type = DEV_PATA;
			break;
		case 0x14:
			ata_drives[drive].type = DEV_PATAPI;
			return true;
		default:
			ata_drives[drive].type = DEV_UNKNOWN;
			return false;
	}
}

static bool ata_identify(int drive) {
	uint16_t iobase = ata_iobase(drive);
	uint16_t data[256];
	uint8_t v;

	// TODO test for float

	ata_driveselect(drive, 0);
	for (int i = 2; i < 6; i++)
		port_out8(iobase + i, 0);
	port_out8(iobase + CMD, 0xEC); // IDENTIFY

	v = port_in8(iobase + STATUS);
	if (v == 0) return false; // nonexistent drive
	while (port_in8(iobase + STATUS) & 0x80);

	/* pool until bit 3 (DRQ) or 0 (ERR) is set */
	while (!((v = port_in8(iobase + STATUS) & 0x9)));
	if (v & 1) return false; /* ERR was set, bail */

	for (int i = 0; i < 256; i++)
		data[i] = port_in16(iobase);
	ata_drives[drive].sectors = data[60] | (data[61] << 16);
	return true;
}

void ata_init(void) {
	tty_const("\n");
	for (int i = 0; i < 4; i++) {
		tty_const("probing drive ");
		_tty_var(i);
		ata_detecttype(i);
		_tty_var(ata_drives[i].type);
		// if (ata_identify(i)) {
		// 	tty_const(" - ");
		// 	_tty_var(ata_drives[i].sectors);
		// 	tty_const(" sectors (512b)");
		// }
		tty_const("\n");
	}
}